e1000_mac.c revision 169240
1/*******************************************************************************
2
3  Copyright (c) 2001-2007, Intel Corporation
4  All rights reserved.
5
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8
9   1. Redistributions of source code must retain the above copyright notice,
10      this list of conditions and the following disclaimer.
11
12   2. Redistributions in binary form must reproduce the above copyright
13      notice, this list of conditions and the following disclaimer in the
14      documentation and/or other materials provided with the distribution.
15
16   3. Neither the name of the Intel Corporation nor the names of its
17      contributors may be used to endorse or promote products derived from
18      this software without specific prior written permission.
19
20  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  POSSIBILITY OF SUCH DAMAGE.
31
32*******************************************************************************/
33$FreeBSD: head/sys/dev/em/e1000_mac.c 169240 2007-05-04 00:00:12Z jfv $
34
35
36#include "e1000_mac.h"
37
38/**
39 *  e1000_remove_device_generic - Free device specific structure
40 *  @hw - pointer to the HW structure
41 *
42 *  If a device specific structure was allocated, this function will
43 *  free it.
44 **/
45void
46e1000_remove_device_generic(struct e1000_hw *hw)
47{
48	DEBUGFUNC("e1000_remove_device_generic");
49
50	/* Freeing the dev_spec member of e1000_hw structure */
51	e1000_free_dev_spec_struct(hw);
52}
53
54/**
55 *  e1000_get_bus_info_pci_generic - Get PCI(x) bus information
56 *  @hw - pointer to the HW structure
57 *
58 *  Determines and stores the system bus information for a particular
59 *  network interface.  The following bus information is determined and stored:
60 *  bus speed, bus width, type (PCI/PCIx), and PCI(-x) function.
61 **/
62s32
63e1000_get_bus_info_pci_generic(struct e1000_hw *hw)
64{
65	struct e1000_bus_info *bus = &hw->bus;
66	u32 status = E1000_READ_REG(hw, E1000_STATUS);
67	s32 ret_val = E1000_SUCCESS;
68	u16 pci_header_type;
69
70	DEBUGFUNC("e1000_get_bus_info_pci_generic");
71
72	/* PCI or PCI-X? */
73	bus->type = (status & E1000_STATUS_PCIX_MODE)
74			? e1000_bus_type_pcix
75			: e1000_bus_type_pci;
76
77	/* Bus speed */
78	if (bus->type == e1000_bus_type_pci) {
79		bus->speed = (status & E1000_STATUS_PCI66)
80		             ? e1000_bus_speed_66
81		             : e1000_bus_speed_33;
82	} else {
83		switch (status & E1000_STATUS_PCIX_SPEED) {
84		case E1000_STATUS_PCIX_SPEED_66:
85			bus->speed = e1000_bus_speed_66;
86			break;
87		case E1000_STATUS_PCIX_SPEED_100:
88			bus->speed = e1000_bus_speed_100;
89			break;
90		case E1000_STATUS_PCIX_SPEED_133:
91			bus->speed = e1000_bus_speed_133;
92			break;
93		default:
94			bus->speed = e1000_bus_speed_reserved;
95			break;
96		}
97	}
98
99	/* Bus width */
100	bus->width = (status & E1000_STATUS_BUS64)
101	             ? e1000_bus_width_64
102	             : e1000_bus_width_32;
103
104	/* Which PCI(-X) function? */
105	e1000_read_pci_cfg(hw, PCI_HEADER_TYPE_REGISTER, &pci_header_type);
106	if (pci_header_type & PCI_HEADER_TYPE_MULTIFUNC)
107		bus->func = (status & E1000_STATUS_FUNC_MASK)
108		            >> E1000_STATUS_FUNC_SHIFT;
109	else
110		bus->func = 0;
111
112	return ret_val;
113}
114
115/**
116 *  e1000_get_bus_info_pcie_generic - Get PCIe bus information
117 *  @hw - pointer to the HW structure
118 *
119 *  Determines and stores the system bus information for a particular
120 *  network interface.  The following bus information is determined and stored:
121 *  bus speed, bus width, type (PCIe), and PCIe function.
122 **/
123s32
124e1000_get_bus_info_pcie_generic(struct e1000_hw *hw)
125{
126	struct e1000_bus_info *bus = &hw->bus;
127	s32 ret_val;
128	u32 status;
129	u16 pcie_link_status, pci_header_type;
130
131	DEBUGFUNC("e1000_get_bus_info_pcie_generic");
132
133	bus->type = e1000_bus_type_pci_express;
134	bus->speed = e1000_bus_speed_2500;
135
136	ret_val = e1000_read_pcie_cap_reg(hw,
137	                                  PCIE_LINK_STATUS,
138	                                  &pcie_link_status);
139	if (ret_val)
140		bus->width = e1000_bus_width_unknown;
141	else
142		bus->width = (e1000_bus_width)((pcie_link_status &
143		                                PCIE_LINK_WIDTH_MASK) >>
144		                               PCIE_LINK_WIDTH_SHIFT);
145
146	e1000_read_pci_cfg(hw, PCI_HEADER_TYPE_REGISTER, &pci_header_type);
147	if (pci_header_type & PCI_HEADER_TYPE_MULTIFUNC) {
148		status = E1000_READ_REG(hw, E1000_STATUS);
149		bus->func = (status & E1000_STATUS_FUNC_MASK)
150		            >> E1000_STATUS_FUNC_SHIFT;
151	} else
152		bus->func = 0;
153
154	return E1000_SUCCESS;
155}
156
157/**
158 *  e1000_clear_vfta_generic - Clear VLAN filter table
159 *  @hw - pointer to the HW structure
160 *
161 *  Clears the register array which contains the VLAN filter table by
162 *  setting all the values to 0.
163 **/
164void
165e1000_clear_vfta_generic(struct e1000_hw *hw)
166{
167	u32 offset;
168
169	DEBUGFUNC("e1000_clear_vfta_generic");
170
171	for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
172		E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, 0);
173		E1000_WRITE_FLUSH(hw);
174	}
175}
176
177/**
178 *  e1000_write_vfta_generic - Write value to VLAN filter table
179 *  @hw - pointer to the HW structure
180 *  @offset - register offset in VLAN filter table
181 *  @value - register value written to VLAN filter table
182 *
183 *  Writes value at the given offset in the register array which stores
184 *  the VLAN filter table.
185 **/
186void
187e1000_write_vfta_generic(struct e1000_hw *hw, u32 offset, u32 value)
188{
189	DEBUGFUNC("e1000_write_vfta_generic");
190
191	E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, value);
192	E1000_WRITE_FLUSH(hw);
193}
194
195/**
196 *  e1000_init_rx_addrs_generic - Initialize receive address's
197 *  @hw - pointer to the HW structure
198 *  @rar_count - receive address registers
199 *
200 *  Setups the receive address registers by setting the base receive address
201 *  register to the devices MAC address and clearing all the other receive
202 *  address registers to 0.
203 **/
204void
205e1000_init_rx_addrs_generic(struct e1000_hw *hw, u16 rar_count)
206{
207	u32 i;
208
209	DEBUGFUNC("e1000_init_rx_addrs_generic");
210
211	/* Setup the receive address */
212	DEBUGOUT("Programming MAC Address into RAR[0]\n");
213
214	e1000_rar_set_generic(hw, hw->mac.addr, 0);
215
216	/* Zero out the other (rar_entry_count - 1) receive addresses */
217	DEBUGOUT1("Clearing RAR[1-%u]\n", rar_count-1);
218	for (i = 1; i < rar_count; i++) {
219		E1000_WRITE_REG_ARRAY(hw, E1000_RA, (i << 1), 0);
220		E1000_WRITE_FLUSH(hw);
221		E1000_WRITE_REG_ARRAY(hw, E1000_RA, ((i << 1) + 1), 0);
222		E1000_WRITE_FLUSH(hw);
223	}
224}
225
226/**
227 *  e1000_rar_set_generic - Set receive address register
228 *  @hw - pointer to the HW structure
229 *  @addr - pointer to the receive address
230 *  @index - receive address array register
231 *
232 *  Sets the receive address array register at index to the address passed
233 *  in by addr.
234 **/
235void
236e1000_rar_set_generic(struct e1000_hw *hw, u8 *addr, u32 index)
237{
238	u32 rar_low, rar_high;
239
240	DEBUGFUNC("e1000_rar_set_generic");
241
242	/* HW expects these in little endian so we reverse the byte order
243	 * from network order (big endian) to little endian
244	 */
245	rar_low = ((u32) addr[0] |
246		   ((u32) addr[1] << 8) |
247		    ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
248
249	rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
250
251	if (!hw->mac.disable_av)
252		rar_high |= E1000_RAH_AV;
253
254	E1000_WRITE_REG_ARRAY(hw, E1000_RA, (index << 1), rar_low);
255	E1000_WRITE_REG_ARRAY(hw, E1000_RA, ((index << 1) + 1), rar_high);
256}
257
258/**
259 *  e1000_mta_set_generic - Set multicast filter table address
260 *  @hw - pointer to the HW structure
261 *  @hash_value - determines the MTA register and bit to set
262 *
263 *  The multicast table address is a register array of 32-bit registers.
264 *  The hash_value is used to determine what register the bit is in, the
265 *  current value is read, the new bit is OR'd in and the new value is
266 *  written back into the register.
267 **/
268void
269e1000_mta_set_generic(struct e1000_hw *hw, u32 hash_value)
270{
271	u32 hash_bit, hash_reg, mta;
272
273	DEBUGFUNC("e1000_mta_set_generic");
274	/* The MTA is a register array of 32-bit registers. It is
275	 * treated like an array of (32*mta_reg_count) bits.  We want to
276	 * set bit BitArray[hash_value]. So we figure out what register
277	 * the bit is in, read it, OR in the new bit, then write
278	 * back the new value.  The (hw->mac.mta_reg_count - 1) serves as a
279	 * mask to bits 31:5 of the hash value which gives us the
280	 * register we're modifying.  The hash bit within that register
281	 * is determined by the lower 5 bits of the hash value.
282	 */
283	hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
284	hash_bit = hash_value & 0x1F;
285
286	mta = E1000_READ_REG_ARRAY(hw, E1000_MTA, hash_reg);
287
288	mta |= (1 << hash_bit);
289
290	E1000_WRITE_REG_ARRAY(hw, E1000_MTA, hash_reg, mta);
291	E1000_WRITE_FLUSH(hw);
292}
293
294/**
295 *  e1000_mc_addr_list_update_generic - Update Multicast addresses
296 *  @hw - pointer to the HW structure
297 *  @mc_addr_list - array of multicast addresses to program
298 *  @mc_addr_count - number of multicast addresses to program
299 *  @rar_used_count - the first RAR register free to program
300 *  @rar_count - total number of supported Receive Address Registers
301 *
302 *  Updates the Receive Address Registers and Multicast Table Array.
303 *  The caller must have a packed mc_addr_list of multicast addresses.
304 *  The parameter rar_count will usually be hw->mac.rar_entry_count
305 *  unless there are workarounds that change this.
306 **/
307void
308e1000_mc_addr_list_update_generic(struct e1000_hw *hw,
309                                  u8 *mc_addr_list, u32 mc_addr_count,
310                                  u32 rar_used_count, u32 rar_count)
311{
312	u32 hash_value;
313	u32 i;
314
315	DEBUGFUNC("e1000_mc_addr_list_update_generic");
316
317	/* Load the first set of multicast addresses into the exact
318	 * filters (RAR).  If there are not enough to fill the RAR
319	 * array, clear the filters.
320	 */
321	for (i = rar_used_count; i < rar_count; i++) {
322		if (mc_addr_count) {
323			e1000_rar_set(hw, mc_addr_list, i);
324			mc_addr_count--;
325			mc_addr_list += ETH_ADDR_LEN;
326		} else {
327			E1000_WRITE_REG_ARRAY(hw, E1000_RA, i << 1, 0);
328			E1000_WRITE_FLUSH(hw);
329			E1000_WRITE_REG_ARRAY(hw, E1000_RA, (i << 1) + 1, 0);
330			E1000_WRITE_FLUSH(hw);
331		}
332	}
333
334	/* Clear the old settings from the MTA */
335	DEBUGOUT("Clearing MTA\n");
336	for (i = 0; i < hw->mac.mta_reg_count; i++) {
337		E1000_WRITE_REG_ARRAY(hw, E1000_MTA, i, 0);
338		E1000_WRITE_FLUSH(hw);
339	}
340
341	/* Load any remaining multicast addresses into the hash table. */
342	for (; mc_addr_count > 0; mc_addr_count--) {
343		hash_value = e1000_hash_mc_addr(hw, mc_addr_list);
344		DEBUGOUT1("Hash value = 0x%03X\n", hash_value);
345		e1000_mta_set(hw, hash_value);
346		mc_addr_list += ETH_ADDR_LEN;
347	}
348}
349
350/**
351 *  e1000_hash_mc_addr_generic - Generate a multicast hash value
352 *  @hw - pointer to the HW structure
353 *  @mc_addr - pointer to a multicast address
354 *
355 *  Generates a multicast address hash value which is used to determine
356 *  the multicast filter table array address and new table value.  See
357 *  e1000_mta_set_generic()
358 **/
359u32
360e1000_hash_mc_addr_generic(struct e1000_hw *hw, u8 *mc_addr)
361{
362	u32 hash_value, hash_mask;
363	u8 bit_shift = 0;
364
365	DEBUGFUNC("e1000_hash_mc_addr_generic");
366
367	/* Register count multiplied by bits per register */
368	hash_mask = (hw->mac.mta_reg_count * 32) - 1;
369
370	/* For a mc_filter_type of 0, bit_shift is the number of left-shifts
371	 * where 0xFF would still fall within the hash mask. */
372	while (hash_mask >> bit_shift != 0xFF)
373		bit_shift++;
374
375	/* The portion of the address that is used for the hash table
376	 * is determined by the mc_filter_type setting.
377	 * The algorithm is such that there is a total of 8 bits of shifting.
378	 * The bit_shift for a mc_filter_type of 0 represents the number of
379	 * left-shifts where the MSB of mc_addr[5] would still fall within
380	 * the hash_mask.  Case 0 does this exactly.  Since there are a total
381	 * of 8 bits of shifting, then mc_addr[4] will shift right the
382	 * remaining number of bits. Thus 8 - bit_shift.  The rest of the
383	 * cases are a variation of this algorithm...essentially raising the
384	 * number of bits to shift mc_addr[5] left, while still keeping the
385	 * 8-bit shifting total.
386	 */
387	/* For example, given the following Destination MAC Address and an
388	 * mta register count of 128 (thus a 4096-bit vector and 0xFFF mask),
389	 * we can see that the bit_shift for case 0 is 4.  These are the hash
390	 * values resulting from each mc_filter_type...
391	 * [0] [1] [2] [3] [4] [5]
392	 * 01  AA  00  12  34  56
393	 * LSB                 MSB
394	 *
395	 * case 0: hash_value = ((0x34 >> 4) | (0x56 << 4)) & 0xFFF = 0x563
396	 * case 1: hash_value = ((0x34 >> 3) | (0x56 << 5)) & 0xFFF = 0xAC6
397	 * case 2: hash_value = ((0x34 >> 2) | (0x56 << 6)) & 0xFFF = 0x163
398	 * case 3: hash_value = ((0x34 >> 0) | (0x56 << 8)) & 0xFFF = 0x634
399	 */
400	switch (hw->mac.mc_filter_type) {
401		default:
402		case 0:
403			break;
404		case 1:
405			bit_shift += 1;
406			break;
407		case 2:
408			bit_shift += 2;
409			break;
410		case 3:
411			bit_shift += 4;
412			break;
413	}
414
415	hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
416	                          (((u16) mc_addr[5]) << bit_shift)));
417
418	return hash_value;
419}
420
421/**
422 *  e1000_pcix_mmrbc_workaround_generic - Fix incorrect MMRBC value
423 *  @hw - pointer to the HW structure
424 *
425 *  In certain situations, a system BIOS may report that the PCIx maximum
426 *  memory read byte count (MMRBC) value is higher than than the actual
427 *  value. We check the PCIx command regsiter with the current PCIx status
428 *  regsiter.
429 **/
430void
431e1000_pcix_mmrbc_workaround_generic(struct e1000_hw *hw)
432{
433	u16 cmd_mmrbc;
434	u16 pcix_cmd;
435	u16 pcix_stat_hi_word;
436	u16 stat_mmrbc;
437
438	DEBUGFUNC("e1000_pcix_mmrbc_workaround_generic");
439
440	/* Workaround for PCI-X issue when BIOS sets MMRBC incorrectly */
441	if (hw->bus.type != e1000_bus_type_pcix)
442		return;
443
444	e1000_read_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd);
445	e1000_read_pci_cfg(hw, PCIX_STATUS_REGISTER_HI, &pcix_stat_hi_word);
446	cmd_mmrbc = (pcix_cmd & PCIX_COMMAND_MMRBC_MASK) >>
447		    PCIX_COMMAND_MMRBC_SHIFT;
448	stat_mmrbc = (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
449		     PCIX_STATUS_HI_MMRBC_SHIFT;
450	if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
451		stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
452	if (cmd_mmrbc > stat_mmrbc) {
453		pcix_cmd &= ~PCIX_COMMAND_MMRBC_MASK;
454		pcix_cmd |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
455		e1000_write_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd);
456	}
457}
458
459/**
460 *  e1000_clear_hw_cntrs_base_generic - Clear base hardware counters
461 *  @hw - pointer to the HW structure
462 *
463 *  Clears the base hardware counters by reading the counter registers.
464 **/
465void
466e1000_clear_hw_cntrs_base_generic(struct e1000_hw *hw)
467{
468	volatile u32 temp;
469
470	DEBUGFUNC("e1000_clear_hw_cntrs_base_generic");
471
472	temp = E1000_READ_REG(hw, E1000_CRCERRS);
473	temp = E1000_READ_REG(hw, E1000_SYMERRS);
474	temp = E1000_READ_REG(hw, E1000_MPC);
475	temp = E1000_READ_REG(hw, E1000_SCC);
476	temp = E1000_READ_REG(hw, E1000_ECOL);
477	temp = E1000_READ_REG(hw, E1000_MCC);
478	temp = E1000_READ_REG(hw, E1000_LATECOL);
479	temp = E1000_READ_REG(hw, E1000_COLC);
480	temp = E1000_READ_REG(hw, E1000_DC);
481	temp = E1000_READ_REG(hw, E1000_SEC);
482	temp = E1000_READ_REG(hw, E1000_RLEC);
483	temp = E1000_READ_REG(hw, E1000_XONRXC);
484	temp = E1000_READ_REG(hw, E1000_XONTXC);
485	temp = E1000_READ_REG(hw, E1000_XOFFRXC);
486	temp = E1000_READ_REG(hw, E1000_XOFFTXC);
487	temp = E1000_READ_REG(hw, E1000_FCRUC);
488	temp = E1000_READ_REG(hw, E1000_GPRC);
489	temp = E1000_READ_REG(hw, E1000_BPRC);
490	temp = E1000_READ_REG(hw, E1000_MPRC);
491	temp = E1000_READ_REG(hw, E1000_GPTC);
492	temp = E1000_READ_REG(hw, E1000_GORCL);
493	temp = E1000_READ_REG(hw, E1000_GORCH);
494	temp = E1000_READ_REG(hw, E1000_GOTCL);
495	temp = E1000_READ_REG(hw, E1000_GOTCH);
496	temp = E1000_READ_REG(hw, E1000_RNBC);
497	temp = E1000_READ_REG(hw, E1000_RUC);
498	temp = E1000_READ_REG(hw, E1000_RFC);
499	temp = E1000_READ_REG(hw, E1000_ROC);
500	temp = E1000_READ_REG(hw, E1000_RJC);
501	temp = E1000_READ_REG(hw, E1000_TORL);
502	temp = E1000_READ_REG(hw, E1000_TORH);
503	temp = E1000_READ_REG(hw, E1000_TOTL);
504	temp = E1000_READ_REG(hw, E1000_TOTH);
505	temp = E1000_READ_REG(hw, E1000_TPR);
506	temp = E1000_READ_REG(hw, E1000_TPT);
507	temp = E1000_READ_REG(hw, E1000_MPTC);
508	temp = E1000_READ_REG(hw, E1000_BPTC);
509}
510
511/**
512 *  e1000_check_for_copper_link_generic - Check for link (Copper)
513 *  @hw - pointer to the HW structure
514 *
515 *  Checks to see of the link status of the hardware has changed.  If a
516 *  change in link status has been detected, then we read the PHY registers
517 *  to get the current speed/duplex if link exists.
518 **/
519s32
520e1000_check_for_copper_link_generic(struct e1000_hw *hw)
521{
522	struct e1000_mac_info *mac = &hw->mac;
523	s32 ret_val;
524	boolean_t link;
525
526	DEBUGFUNC("e1000_check_for_copper_link");
527
528	/* We only want to go out to the PHY registers to see if Auto-Neg
529	 * has completed and/or if our link status has changed.  The
530	 * get_link_status flag is set upon receiving a Link Status
531	 * Change or Rx Sequence Error interrupt.
532	 */
533	if (!mac->get_link_status) {
534		ret_val = E1000_SUCCESS;
535		goto out;
536	}
537
538	/* First we want to see if the MII Status Register reports
539	 * link.  If so, then we want to get the current speed/duplex
540	 * of the PHY.
541	 */
542	ret_val = e1000_phy_has_link_generic(hw, 1, 0, &link);
543	if (ret_val)
544		goto out;
545
546	if (!link)
547		goto out; /* No link detected */
548
549	mac->get_link_status = FALSE;
550
551	/* Check if there was DownShift, must be checked
552	 * immediately after link-up */
553	e1000_check_downshift_generic(hw);
554
555	/* If we are forcing speed/duplex, then we simply return since
556	 * we have already determined whether we have link or not.
557	 */
558	if (!mac->autoneg) {
559		ret_val = -E1000_ERR_CONFIG;
560		goto out;
561	}
562
563	/* Auto-Neg is enabled.  Auto Speed Detection takes care
564	 * of MAC speed/duplex configuration.  So we only need to
565	 * configure Collision Distance in the MAC.
566	 */
567	e1000_config_collision_dist_generic(hw);
568
569	/* Configure Flow Control now that Auto-Neg has completed.
570	 * First, we need to restore the desired flow control
571	 * settings because we may have had to re-autoneg with a
572	 * different link partner.
573	 */
574	ret_val = e1000_config_fc_after_link_up_generic(hw);
575	if (ret_val) {
576		DEBUGOUT("Error configuring flow control\n");
577	}
578
579out:
580	return ret_val;
581}
582
583/**
584 *  e1000_check_for_fiber_link_generic - Check for link (Fiber)
585 *  @hw - pointer to the HW structure
586 *
587 *  Checks for link up on the hardware.  If link is not up and we have
588 *  a signal, then we need to force link up.
589 **/
590s32
591e1000_check_for_fiber_link_generic(struct e1000_hw *hw)
592{
593	struct e1000_mac_info *mac = &hw->mac;
594	u32 rxcw;
595	u32 ctrl;
596	u32 status;
597	s32 ret_val = E1000_SUCCESS;
598
599	DEBUGFUNC("e1000_check_for_fiber_link_generic");
600
601	ctrl = E1000_READ_REG(hw, E1000_CTRL);
602	status = E1000_READ_REG(hw, E1000_STATUS);
603	rxcw = E1000_READ_REG(hw, E1000_RXCW);
604
605	/* If we don't have link (auto-negotiation failed or link partner
606	 * cannot auto-negotiate), the cable is plugged in (we have signal),
607	 * and our link partner is not trying to auto-negotiate with us (we
608	 * are receiving idles or data), we need to force link up. We also
609	 * need to give auto-negotiation time to complete, in case the cable
610	 * was just plugged in. The autoneg_failed flag does this.
611	 */
612	/* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
613	if ((ctrl & E1000_CTRL_SWDPIN1) && (!(status & E1000_STATUS_LU)) &&
614	    (!(rxcw & E1000_RXCW_C))) {
615		if (mac->autoneg_failed == 0) {
616			mac->autoneg_failed = 1;
617			goto out;
618		}
619		DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\n");
620
621		/* Disable auto-negotiation in the TXCW register */
622		E1000_WRITE_REG(hw, E1000_TXCW, (mac->txcw & ~E1000_TXCW_ANE));
623
624		/* Force link-up and also force full-duplex. */
625		ctrl = E1000_READ_REG(hw, E1000_CTRL);
626		ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
627		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
628
629		/* Configure Flow Control after forcing link up. */
630		ret_val = e1000_config_fc_after_link_up_generic(hw);
631		if (ret_val) {
632			DEBUGOUT("Error configuring flow control\n");
633			goto out;
634		}
635	} else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
636		/* If we are forcing link and we are receiving /C/ ordered
637		 * sets, re-enable auto-negotiation in the TXCW register
638		 * and disable forced link in the Device Control register
639		 * in an attempt to auto-negotiate with our link partner.
640		 */
641		DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\n");
642		E1000_WRITE_REG(hw, E1000_TXCW, mac->txcw);
643		E1000_WRITE_REG(hw, E1000_CTRL, (ctrl & ~E1000_CTRL_SLU));
644
645		mac->serdes_has_link = TRUE;
646	}
647
648out:
649	return ret_val;
650}
651
652/**
653 *  e1000_check_for_serdes_link_generic - Check for link (Serdes)
654 *  @hw - pointer to the HW structure
655 *
656 *  Checks for link up on the hardware.  If link is not up and we have
657 *  a signal, then we need to force link up.
658 **/
659s32
660e1000_check_for_serdes_link_generic(struct e1000_hw *hw)
661{
662	struct e1000_mac_info *mac = &hw->mac;
663	u32 rxcw;
664	u32 ctrl;
665	u32 status;
666	s32 ret_val = E1000_SUCCESS;
667
668	DEBUGFUNC("e1000_check_for_serdes_link_generic");
669
670	ctrl = E1000_READ_REG(hw, E1000_CTRL);
671	status = E1000_READ_REG(hw, E1000_STATUS);
672	rxcw = E1000_READ_REG(hw, E1000_RXCW);
673
674	/* If we don't have link (auto-negotiation failed or link partner
675	 * cannot auto-negotiate), and our link partner is not trying to
676	 * auto-negotiate with us (we are receiving idles or data),
677	 * we need to force link up. We also need to give auto-negotiation
678	 * time to complete.
679	 */
680	/* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
681	if ((!(status & E1000_STATUS_LU)) && (!(rxcw & E1000_RXCW_C))) {
682		if (mac->autoneg_failed == 0) {
683			mac->autoneg_failed = 1;
684			goto out;
685		}
686		DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\n");
687
688		/* Disable auto-negotiation in the TXCW register */
689		E1000_WRITE_REG(hw, E1000_TXCW, (mac->txcw & ~E1000_TXCW_ANE));
690
691		/* Force link-up and also force full-duplex. */
692		ctrl = E1000_READ_REG(hw, E1000_CTRL);
693		ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
694		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
695
696		/* Configure Flow Control after forcing link up. */
697		ret_val = e1000_config_fc_after_link_up_generic(hw);
698		if (ret_val) {
699			DEBUGOUT("Error configuring flow control\n");
700			goto out;
701		}
702	} else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
703		/* If we are forcing link and we are receiving /C/ ordered
704		 * sets, re-enable auto-negotiation in the TXCW register
705		 * and disable forced link in the Device Control register
706		 * in an attempt to auto-negotiate with our link partner.
707		 */
708		DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\n");
709		E1000_WRITE_REG(hw, E1000_TXCW, mac->txcw);
710		E1000_WRITE_REG(hw, E1000_CTRL, (ctrl & ~E1000_CTRL_SLU));
711
712		mac->serdes_has_link = TRUE;
713	} else if (!(E1000_TXCW_ANE & E1000_READ_REG(hw, E1000_TXCW))) {
714		/* If we force link for non-auto-negotiation switch, check
715		 * link status based on MAC synchronization for internal
716		 * serdes media type.
717		 */
718		/* SYNCH bit and IV bit are sticky. */
719		usec_delay(10);
720		if (E1000_RXCW_SYNCH & E1000_READ_REG(hw, E1000_RXCW)) {
721			if (!(rxcw & E1000_RXCW_IV)) {
722				mac->serdes_has_link = TRUE;
723				DEBUGOUT("SERDES: Link is up.\n");
724			}
725		} else {
726			mac->serdes_has_link = FALSE;
727			DEBUGOUT("SERDES: Link is down.\n");
728		}
729	}
730
731	if (E1000_TXCW_ANE & E1000_READ_REG(hw, E1000_TXCW)) {
732		status = E1000_READ_REG(hw, E1000_STATUS);
733		mac->serdes_has_link = (status & E1000_STATUS_LU)
734					? TRUE
735					: FALSE;
736	}
737
738out:
739	return ret_val;
740}
741
742/**
743 *  e1000_setup_link_generic - Setup flow control and link settings
744 *  @hw - pointer to the HW structure
745 *
746 *  Determines which flow control settings to use, then configures flow
747 *  control.  Calls the appropriate media-specific link configuration
748 *  function.  Assuming the adapter has a valid link partner, a valid link
749 *  should be established.  Assumes the hardware has previously been reset
750 *  and the transmitter and receiver are not enabled.
751 **/
752s32
753e1000_setup_link_generic(struct e1000_hw *hw)
754{
755	struct e1000_mac_info *mac = &hw->mac;
756	struct e1000_functions *func = &hw->func;
757	s32 ret_val = E1000_SUCCESS;
758
759	DEBUGFUNC("e1000_setup_link_generic");
760
761	/* In the case of the phy reset being blocked, we already have a link.
762	 * We do not need to set it up again.
763	 */
764	if (e1000_check_reset_block(hw))
765		goto out;
766
767	ret_val = e1000_set_default_fc_generic(hw);
768	if (ret_val)
769		goto out;
770
771	/* We want to save off the original Flow Control configuration just
772	 * in case we get disconnected and then reconnected into a different
773	 * hub or switch with different Flow Control capabilities.
774	 */
775	mac->original_fc = mac->fc;
776
777	DEBUGOUT1("After fix-ups FlowControl is now = %x\n", mac->fc);
778
779	/* Call the necessary media_type subroutine to configure the link. */
780	ret_val = func->setup_physical_interface(hw);
781	if (ret_val)
782		goto out;
783
784	/* Initialize the flow control address, type, and PAUSE timer
785	 * registers to their default values.  This is done even if flow
786	 * control is disabled, because it does not hurt anything to
787	 * initialize these registers.
788	 */
789	DEBUGOUT("Initializing the Flow Control address, type and timer regs\n");
790	E1000_WRITE_REG(hw, E1000_FCT, FLOW_CONTROL_TYPE);
791	E1000_WRITE_REG(hw, E1000_FCAH, FLOW_CONTROL_ADDRESS_HIGH);
792	E1000_WRITE_REG(hw, E1000_FCAL, FLOW_CONTROL_ADDRESS_LOW);
793
794	E1000_WRITE_REG(hw, E1000_FCTTV, mac->fc_pause_time);
795
796	ret_val = e1000_set_fc_watermarks_generic(hw);
797
798out:
799	return ret_val;
800}
801
802/**
803 *  e1000_setup_fiber_serdes_link_generic - Setup link for fiber/serdes
804 *  @hw - pointer to the HW structure
805 *
806 *  Configures collision distance and flow control for fiber and serdes
807 *  links.  Upon successful setup, poll for link.
808 **/
809s32
810e1000_setup_fiber_serdes_link_generic(struct e1000_hw *hw)
811{
812	u32 ctrl;
813	s32 ret_val = E1000_SUCCESS;
814
815	DEBUGFUNC("e1000_setup_fiber_serdes_link_generic");
816
817	ctrl = E1000_READ_REG(hw, E1000_CTRL);
818
819	/* Take the link out of reset */
820	ctrl &= ~E1000_CTRL_LRST;
821
822	e1000_config_collision_dist_generic(hw);
823
824	ret_val = e1000_commit_fc_settings_generic(hw);
825	if (ret_val)
826		goto out;
827
828	/* Since auto-negotiation is enabled, take the link out of reset (the
829	 * link will be in reset, because we previously reset the chip). This
830	 * will restart auto-negotiation.  If auto-negotiation is successful
831	 * then the link-up status bit will be set and the flow control enable
832	 * bits (RFCE and TFCE) will be set according to their negotiated value.
833	 */
834	DEBUGOUT("Auto-negotiation enabled\n");
835
836	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
837	E1000_WRITE_FLUSH(hw);
838	msec_delay(1);
839
840	/* For these adapters, the SW defineable pin 1 is set when the optics
841	 * detect a signal.  If we have a signal, then poll for a "Link-Up"
842	 * indication.
843	 */
844	if (hw->media_type == e1000_media_type_internal_serdes ||
845	    (E1000_READ_REG(hw, E1000_CTRL) & E1000_CTRL_SWDPIN1)) {
846		ret_val = e1000_poll_fiber_serdes_link_generic(hw);
847	} else {
848		DEBUGOUT("No signal detected\n");
849	}
850
851out:
852	return ret_val;
853}
854
855/**
856 *  e1000_config_collision_dist_generic - Configure collision distance
857 *  @hw - pointer to the HW structure
858 *
859 *  Configures the collision distance to the default value and is used
860 *  during link setup. Currently no func pointer exists and all
861 *  implementations are handled in the generic version of this function.
862 **/
863void
864e1000_config_collision_dist_generic(struct e1000_hw *hw)
865{
866	u32 tctl;
867
868	DEBUGFUNC("e1000_config_collision_dist_generic");
869
870	tctl = E1000_READ_REG(hw, E1000_TCTL);
871
872	tctl &= ~E1000_TCTL_COLD;
873	tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
874
875	E1000_WRITE_REG(hw, E1000_TCTL, tctl);
876	E1000_WRITE_FLUSH(hw);
877}
878
879/**
880 *  e1000_poll_fiber_serdes_link_generic - Poll for link up
881 *  @hw - pointer to the HW structure
882 *
883 *  Polls for link up by reading the status register, if link fails to come
884 *  up with auto-negotiation, then the link is forced if a signal is detected.
885 **/
886s32
887e1000_poll_fiber_serdes_link_generic(struct e1000_hw *hw)
888{
889	struct e1000_mac_info *mac = &hw->mac;
890	u32 i, status;
891	s32 ret_val = E1000_SUCCESS;
892
893	DEBUGFUNC("e1000_poll_fiber_serdes_link_generic");
894
895	/* If we have a signal (the cable is plugged in, or assumed true for
896	 * serdes media) then poll for a "Link-Up" indication in the Device
897	 * Status Register.  Time-out if a link isn't seen in 500 milliseconds
898	 * seconds (Auto-negotiation should complete in less than 500
899	 * milliseconds even if the other end is doing it in SW).
900	 */
901	for (i = 0; i < FIBER_LINK_UP_LIMIT; i++) {
902		msec_delay(10);
903		status = E1000_READ_REG(hw, E1000_STATUS);
904		if (status & E1000_STATUS_LU)
905			break;
906	}
907	if (i == FIBER_LINK_UP_LIMIT) {
908		DEBUGOUT("Never got a valid link from auto-neg!!!\n");
909		mac->autoneg_failed = 1;
910		/* AutoNeg failed to achieve a link, so we'll call
911		 * mac->check_for_link. This routine will force the
912		 * link up if we detect a signal. This will allow us to
913		 * communicate with non-autonegotiating link partners.
914		 */
915		ret_val = e1000_check_for_link(hw);
916		if (ret_val) {
917			DEBUGOUT("Error while checking for link\n");
918			goto out;
919		}
920		mac->autoneg_failed = 0;
921	} else {
922		mac->autoneg_failed = 0;
923		DEBUGOUT("Valid Link Found\n");
924	}
925
926out:
927	return ret_val;
928}
929
930/**
931 *  e1000_commit_fc_settings_generic - Configure flow control
932 *  @hw - pointer to the HW structure
933 *
934 *  Write the flow control settings to the Transmit Config Word Register (TXCW)
935 *  base on the flow control settings in e1000_mac_info.
936 **/
937s32
938e1000_commit_fc_settings_generic(struct e1000_hw *hw)
939{
940	struct e1000_mac_info *mac = &hw->mac;
941	u32 txcw;
942	s32 ret_val = E1000_SUCCESS;
943
944	DEBUGFUNC("e1000_commit_fc_settings_generic");
945
946	/* Check for a software override of the flow control settings, and
947	 * setup the device accordingly.  If auto-negotiation is enabled, then
948	 * software will have to set the "PAUSE" bits to the correct value in
949	 * the Transmit Config Word Register (TXCW) and re-start auto-
950	 * negotiation.  However, if auto-negotiation is disabled, then
951	 * software will have to manually configure the two flow control enable
952	 * bits in the CTRL register.
953	 *
954	 * The possible values of the "fc" parameter are:
955	 *      0:  Flow control is completely disabled
956	 *      1:  Rx flow control is enabled (we can receive pause frames,
957	 *          but not send pause frames).
958	 *      2:  Tx flow control is enabled (we can send pause frames but we
959	 *          do not support receiving pause frames).
960	 *      3:  Both Rx and TX flow control (symmetric) are enabled.
961	 */
962	switch (mac->fc) {
963	case e1000_fc_none:
964		/* Flow control completely disabled by a software over-ride. */
965		txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
966		break;
967	case e1000_fc_rx_pause:
968		/* RX Flow control is enabled and TX Flow control is disabled
969		 * by a software over-ride. Since there really isn't a way to
970		 * advertise that we are capable of RX Pause ONLY, we will
971		 * advertise that we support both symmetric and asymmetric RX
972		 * PAUSE.  Later, we will disable the adapter's ability to send
973		 * PAUSE frames.
974		 */
975		txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
976		break;
977	case e1000_fc_tx_pause:
978		/* TX Flow control is enabled, and RX Flow control is disabled,
979		 * by a software over-ride.
980		 */
981		txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
982		break;
983	case e1000_fc_full:
984		/* Flow control (both RX and TX) is enabled by a software
985		 * over-ride.
986		 */
987		txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
988		break;
989	default:
990		DEBUGOUT("Flow control param set incorrectly\n");
991		ret_val = -E1000_ERR_CONFIG;
992		goto out;
993		break;
994	}
995
996	E1000_WRITE_REG(hw, E1000_TXCW, txcw);
997	mac->txcw = txcw;
998
999out:
1000	return ret_val;
1001}
1002
1003/**
1004 *  e1000_set_fc_watermarks_generic - Set flow control high/low watermarks
1005 *  @hw - pointer to the HW structure
1006 *
1007 *  Sets the flow control high/low threshold (watermark) registers.  If
1008 *  flow control XON frame transmission is enabled, then set XON frame
1009 *  tansmission as well.
1010 **/
1011s32
1012e1000_set_fc_watermarks_generic(struct e1000_hw *hw)
1013{
1014	struct e1000_mac_info *mac = &hw->mac;
1015	s32 ret_val = E1000_SUCCESS;
1016	u32 fcrtl = 0, fcrth = 0;
1017
1018	DEBUGFUNC("e1000_set_fc_watermarks_generic");
1019
1020	/* Set the flow control receive threshold registers.  Normally,
1021	 * these registers will be set to a default threshold that may be
1022	 * adjusted later by the driver's runtime code.  However, if the
1023	 * ability to transmit pause frames is not enabled, then these
1024	 * registers will be set to 0.
1025	 */
1026	if (mac->fc & e1000_fc_tx_pause) {
1027		/* We need to set up the Receive Threshold high and low water
1028		 * marks as well as (optionally) enabling the transmission of
1029		 * XON frames.
1030		 */
1031		fcrtl = mac->fc_low_water;
1032		if (mac->fc_send_xon)
1033			fcrtl |= E1000_FCRTL_XONE;
1034
1035		fcrth = mac->fc_high_water;
1036	}
1037	E1000_WRITE_REG(hw, E1000_FCRTL, fcrtl);
1038	E1000_WRITE_REG(hw, E1000_FCRTH, fcrth);
1039
1040	return ret_val;
1041}
1042
1043/**
1044 *  e1000_set_default_fc_generic - Set flow control default values
1045 *  @hw - pointer to the HW structure
1046 *
1047 *  Read the EEPROM for the default values for flow control and store the
1048 *  values.
1049 **/
1050s32
1051e1000_set_default_fc_generic(struct e1000_hw *hw)
1052{
1053	struct e1000_mac_info *mac = &hw->mac;
1054	s32 ret_val = E1000_SUCCESS;
1055	u16 nvm_data;
1056
1057	DEBUGFUNC("e1000_set_default_fc_generic");
1058
1059	if (mac->fc != e1000_fc_default)
1060		goto out;
1061
1062	/* Read and store word 0x0F of the EEPROM. This word contains bits
1063	 * that determine the hardware's default PAUSE (flow control) mode,
1064	 * a bit that determines whether the HW defaults to enabling or
1065	 * disabling auto-negotiation, and the direction of the
1066	 * SW defined pins. If there is no SW over-ride of the flow
1067	 * control setting, then the variable hw->fc will
1068	 * be initialized based on a value in the EEPROM.
1069	 */
1070	ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &nvm_data);
1071
1072	if (ret_val) {
1073		DEBUGOUT("NVM Read Error\n");
1074		goto out;
1075	}
1076
1077	if ((nvm_data & NVM_WORD0F_PAUSE_MASK) == 0)
1078		mac->fc = e1000_fc_none;
1079	else if ((nvm_data & NVM_WORD0F_PAUSE_MASK) ==
1080		 NVM_WORD0F_ASM_DIR)
1081		mac->fc = e1000_fc_tx_pause;
1082	else
1083		mac->fc = e1000_fc_full;
1084
1085out:
1086	return ret_val;
1087}
1088
1089/**
1090 *  e1000_force_mac_fc_generic - Force the MAC's flow control settings
1091 *  @hw - pointer to the HW structure
1092 *
1093 *  Force the MAC's flow control settings.  Sets the TFCE and RFCE bits in the
1094 *  device control register to reflect the adapter settings.  TFCE and RFCE
1095 *  need to be explicitly set by software when a copper PHY is used because
1096 *  autonegotiation is managed by the PHY rather than the MAC.  Software must
1097 *  also configure these bits when link is forced on a fiber connection.
1098 **/
1099s32
1100e1000_force_mac_fc_generic(struct e1000_hw *hw)
1101{
1102	struct e1000_mac_info *mac = &hw->mac;
1103	u32 ctrl;
1104	s32 ret_val = E1000_SUCCESS;
1105
1106	DEBUGFUNC("e1000_force_mac_fc_generic");
1107
1108	ctrl = E1000_READ_REG(hw, E1000_CTRL);
1109
1110	/* Because we didn't get link via the internal auto-negotiation
1111	 * mechanism (we either forced link or we got link via PHY
1112	 * auto-neg), we have to manually enable/disable transmit an
1113	 * receive flow control.
1114	 *
1115	 * The "Case" statement below enables/disable flow control
1116	 * according to the "mac->fc" parameter.
1117	 *
1118	 * The possible values of the "fc" parameter are:
1119	 *      0:  Flow control is completely disabled
1120	 *      1:  Rx flow control is enabled (we can receive pause
1121	 *          frames but not send pause frames).
1122	 *      2:  Tx flow control is enabled (we can send pause frames
1123	 *          frames but we do not receive pause frames).
1124	 *      3:  Both Rx and TX flow control (symmetric) is enabled.
1125	 *  other:  No other values should be possible at this point.
1126	 */
1127	DEBUGOUT1("mac->fc = %u\n", mac->fc);
1128
1129	switch (mac->fc) {
1130	case e1000_fc_none:
1131		ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
1132		break;
1133	case e1000_fc_rx_pause:
1134		ctrl &= (~E1000_CTRL_TFCE);
1135		ctrl |= E1000_CTRL_RFCE;
1136		break;
1137	case e1000_fc_tx_pause:
1138		ctrl &= (~E1000_CTRL_RFCE);
1139		ctrl |= E1000_CTRL_TFCE;
1140		break;
1141	case e1000_fc_full:
1142		ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
1143		break;
1144	default:
1145		DEBUGOUT("Flow control param set incorrectly\n");
1146		ret_val = -E1000_ERR_CONFIG;
1147		goto out;
1148	}
1149
1150	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
1151
1152out:
1153	return ret_val;
1154}
1155
1156/**
1157 *  e1000_config_fc_after_link_up_generic - Configures flow control after link
1158 *  @hw - pointer to the HW structure
1159 *
1160 *  Checks the status of auto-negotiation after link up to ensure that the
1161 *  speed and duplex were not forced.  If the link needed to be forced, then
1162 *  flow control needs to be forced also.  If auto-negotiation is enabled
1163 *  and did not fail, then we configure flow control based on our link
1164 *  partner.
1165 **/
1166s32
1167e1000_config_fc_after_link_up_generic(struct e1000_hw *hw)
1168{
1169	struct e1000_mac_info *mac = &hw->mac;
1170	s32 ret_val = E1000_SUCCESS;
1171	u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
1172	u16 speed, duplex;
1173
1174	DEBUGFUNC("e1000_config_fc_after_link_up_generic");
1175
1176	/* Check for the case where we have fiber media and auto-neg failed
1177	 * so we had to force link.  In this case, we need to force the
1178	 * configuration of the MAC to match the "fc" parameter.
1179	 */
1180	if (mac->autoneg_failed) {
1181		if (hw->media_type == e1000_media_type_fiber ||
1182		    hw->media_type == e1000_media_type_internal_serdes)
1183			ret_val = e1000_force_mac_fc_generic(hw);
1184	} else {
1185		if (hw->media_type == e1000_media_type_copper)
1186			ret_val = e1000_force_mac_fc_generic(hw);
1187	}
1188
1189	if (ret_val) {
1190		DEBUGOUT("Error forcing flow control settings\n");
1191		goto out;
1192	}
1193
1194	/* Check for the case where we have copper media and auto-neg is
1195	 * enabled.  In this case, we need to check and see if Auto-Neg
1196	 * has completed, and if so, how the PHY and link partner has
1197	 * flow control configured.
1198	 */
1199	if ((hw->media_type == e1000_media_type_copper) && mac->autoneg) {
1200		/* Read the MII Status Register and check to see if AutoNeg
1201		 * has completed.  We read this twice because this reg has
1202		 * some "sticky" (latched) bits.
1203		 */
1204		ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1205		if (ret_val)
1206			goto out;
1207		ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1208		if (ret_val)
1209			goto out;
1210
1211		if (!(mii_status_reg & MII_SR_AUTONEG_COMPLETE)) {
1212			DEBUGOUT("Copper PHY and Auto Neg "
1213			         "has not completed.\n");
1214			goto out;
1215		}
1216
1217		/* The AutoNeg process has completed, so we now need to
1218		 * read both the Auto Negotiation Advertisement
1219		 * Register (Address 4) and the Auto_Negotiation Base
1220		 * Page Ability Register (Address 5) to determine how
1221		 * flow control was negotiated.
1222		 */
1223		ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV,
1224					    &mii_nway_adv_reg);
1225		if (ret_val)
1226			goto out;
1227		ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY,
1228					    &mii_nway_lp_ability_reg);
1229		if (ret_val)
1230			goto out;
1231
1232		/* Two bits in the Auto Negotiation Advertisement Register
1233		 * (Address 4) and two bits in the Auto Negotiation Base
1234		 * Page Ability Register (Address 5) determine flow control
1235		 * for both the PHY and the link partner.  The following
1236		 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
1237		 * 1999, describes these PAUSE resolution bits and how flow
1238		 * control is determined based upon these settings.
1239		 * NOTE:  DC = Don't Care
1240		 *
1241		 *   LOCAL DEVICE  |   LINK PARTNER
1242		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
1243		 *-------|---------|-------|---------|--------------------
1244		 *   0   |    0    |  DC   |   DC    | e1000_fc_none
1245		 *   0   |    1    |   0   |   DC    | e1000_fc_none
1246		 *   0   |    1    |   1   |    0    | e1000_fc_none
1247		 *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1248		 *   1   |    0    |   0   |   DC    | e1000_fc_none
1249		 *   1   |   DC    |   1   |   DC    | e1000_fc_full
1250		 *   1   |    1    |   0   |    0    | e1000_fc_none
1251		 *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1252		 *
1253		 */
1254		/* Are both PAUSE bits set to 1?  If so, this implies
1255		 * Symmetric Flow Control is enabled at both ends.  The
1256		 * ASM_DIR bits are irrelevant per the spec.
1257		 *
1258		 * For Symmetric Flow Control:
1259		 *
1260		 *   LOCAL DEVICE  |   LINK PARTNER
1261		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1262		 *-------|---------|-------|---------|--------------------
1263		 *   1   |   DC    |   1   |   DC    | E1000_fc_full
1264		 *
1265		 */
1266		if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1267		    (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
1268			/* Now we need to check if the user selected RX ONLY
1269			 * of pause frames.  In this case, we had to advertise
1270			 * FULL flow control because we could not advertise RX
1271			 * ONLY. Hence, we must now check to see if we need to
1272			 * turn OFF  the TRANSMISSION of PAUSE frames.
1273			 */
1274			if (mac->original_fc == e1000_fc_full) {
1275				mac->fc = e1000_fc_full;
1276				DEBUGOUT("Flow Control = FULL.\r\n");
1277			} else {
1278				mac->fc = e1000_fc_rx_pause;
1279				DEBUGOUT("Flow Control = "
1280				         "RX PAUSE frames only.\r\n");
1281			}
1282		}
1283		/* For receiving PAUSE frames ONLY.
1284		 *
1285		 *   LOCAL DEVICE  |   LINK PARTNER
1286		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1287		 *-------|---------|-------|---------|--------------------
1288		 *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1289		 *
1290		 */
1291		else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1292		          (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1293		          (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1294		          (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
1295			mac->fc = e1000_fc_tx_pause;
1296			DEBUGOUT("Flow Control = TX PAUSE frames only.\r\n");
1297		}
1298		/* For transmitting PAUSE frames ONLY.
1299		 *
1300		 *   LOCAL DEVICE  |   LINK PARTNER
1301		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1302		 *-------|---------|-------|---------|--------------------
1303		 *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1304		 *
1305		 */
1306		else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1307		         (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1308		         !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1309		         (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
1310			mac->fc = e1000_fc_rx_pause;
1311			DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
1312		}
1313		/* Per the IEEE spec, at this point flow control should be
1314		 * disabled.  However, we want to consider that we could
1315		 * be connected to a legacy switch that doesn't advertise
1316		 * desired flow control, but can be forced on the link
1317		 * partner.  So if we advertised no flow control, that is
1318		 * what we will resolve to.  If we advertised some kind of
1319		 * receive capability (Rx Pause Only or Full Flow Control)
1320		 * and the link partner advertised none, we will configure
1321		 * ourselves to enable Rx Flow Control only.  We can do
1322		 * this safely for two reasons:  If the link partner really
1323		 * didn't want flow control enabled, and we enable Rx, no
1324		 * harm done since we won't be receiving any PAUSE frames
1325		 * anyway.  If the intent on the link partner was to have
1326		 * flow control enabled, then by us enabling RX only, we
1327		 * can at least receive pause frames and process them.
1328		 * This is a good idea because in most cases, since we are
1329		 * predominantly a server NIC, more times than not we will
1330		 * be asked to delay transmission of packets than asking
1331		 * our link partner to pause transmission of frames.
1332		 */
1333		else if ((mac->original_fc == e1000_fc_none ||
1334		          mac->original_fc == e1000_fc_tx_pause) ||
1335		         mac->fc_strict_ieee) {
1336			mac->fc = e1000_fc_none;
1337			DEBUGOUT("Flow Control = NONE.\r\n");
1338		} else {
1339			mac->fc = e1000_fc_rx_pause;
1340			DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
1341		}
1342
1343		/* Now we need to do one last check...  If we auto-
1344		 * negotiated to HALF DUPLEX, flow control should not be
1345		 * enabled per IEEE 802.3 spec.
1346		 */
1347		ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
1348		if (ret_val) {
1349			DEBUGOUT("Error getting link speed and duplex\n");
1350			goto out;
1351		}
1352
1353		if (duplex == HALF_DUPLEX)
1354			mac->fc = e1000_fc_none;
1355
1356		/* Now we call a subroutine to actually force the MAC
1357		 * controller to use the correct flow control settings.
1358		 */
1359		ret_val = e1000_force_mac_fc_generic(hw);
1360		if (ret_val) {
1361			DEBUGOUT("Error forcing flow control settings\n");
1362			goto out;
1363		}
1364	}
1365
1366out:
1367	return ret_val;
1368}
1369
1370/**
1371 *  e1000_get_speed_and_duplex_copper_generic - Retreive current speed/duplex
1372 *  @hw - pointer to the HW structure
1373 *  @speed - stores the current speed
1374 *  @duplex - stores the current duplex
1375 *
1376 *  Read the status register for the current speed/duplex and store the current
1377 *  speed and duplex for copper connections.
1378 **/
1379s32
1380e1000_get_speed_and_duplex_copper_generic(struct e1000_hw *hw, u16 *speed,
1381                                          u16 *duplex)
1382{
1383	u32 status;
1384
1385	DEBUGFUNC("e1000_get_speed_and_duplex_copper_generic");
1386
1387	status = E1000_READ_REG(hw, E1000_STATUS);
1388	if (status & E1000_STATUS_SPEED_1000) {
1389		*speed = SPEED_1000;
1390		DEBUGOUT("1000 Mbs, ");
1391	} else if (status & E1000_STATUS_SPEED_100) {
1392		*speed = SPEED_100;
1393		DEBUGOUT("100 Mbs, ");
1394	} else {
1395		*speed = SPEED_10;
1396		DEBUGOUT("10 Mbs, ");
1397	}
1398
1399	if (status & E1000_STATUS_FD) {
1400		*duplex = FULL_DUPLEX;
1401		DEBUGOUT("Full Duplex\n");
1402	} else {
1403		*duplex = HALF_DUPLEX;
1404		DEBUGOUT("Half Duplex\n");
1405	}
1406
1407	return E1000_SUCCESS;
1408}
1409
1410/**
1411 *  e1000_get_speed_and_duplex_fiber_generic - Retreive current speed/duplex
1412 *  @hw - pointer to the HW structure
1413 *  @speed - stores the current speed
1414 *  @duplex - stores the current duplex
1415 *
1416 *  Sets the speed and duplex to gigabit full duplex (the only possible option)
1417 *  for fiber/serdes links.
1418 **/
1419s32
1420e1000_get_speed_and_duplex_fiber_serdes_generic(struct e1000_hw *hw, u16 *speed,
1421                                                u16 *duplex)
1422{
1423	DEBUGFUNC("e1000_get_speed_and_duplex_fiber_serdes_generic");
1424
1425	*speed = SPEED_1000;
1426	*duplex = FULL_DUPLEX;
1427
1428	return E1000_SUCCESS;
1429}
1430
1431/**
1432 *  e1000_get_hw_semaphore_generic - Acquire hardware semaphore
1433 *  @hw - pointer to the HW structure
1434 *
1435 *  Request a hardware semaphore by setting the firmware semaphore bit, once
1436 *  bit has been set, semaphore has been acquired.
1437 **/
1438s32
1439e1000_get_hw_semaphore_generic(struct e1000_hw *hw)
1440{
1441	u32 swsm;
1442	s32 ret_val = E1000_SUCCESS;
1443	s32 timeout = hw->nvm.word_size + 1;
1444	s32 i = 0;
1445
1446	DEBUGFUNC("e1000_get_hw_semaphore_generic");
1447
1448	/* Get the FW semaphore. */
1449	for (i = 0; i < timeout; i++) {
1450		swsm = E1000_READ_REG(hw, E1000_SWSM);
1451		E1000_WRITE_REG(hw, E1000_SWSM, swsm | E1000_SWSM_SWESMBI);
1452
1453		/* Semaphore acquired if bit latched */
1454		if (E1000_READ_REG(hw, E1000_SWSM) & E1000_SWSM_SWESMBI)
1455			break;
1456
1457		usec_delay(50);
1458	}
1459
1460	if (i == timeout) {
1461		/* Release semaphores */
1462		e1000_put_hw_semaphore_generic(hw);
1463		DEBUGOUT("Driver can't access the NVM\n");
1464		ret_val = -E1000_ERR_NVM;
1465		goto out;
1466	}
1467
1468out:
1469	return ret_val;
1470}
1471
1472/**
1473 *  e1000_put_hw_semaphore_generic - Release hardware semaphore
1474 *  @hw - pointer to the HW structure
1475 *
1476 *  Release hardware semaphore by clearing in the firmware semaphore bit.
1477 **/
1478void
1479e1000_put_hw_semaphore_generic(struct e1000_hw *hw)
1480{
1481	u32 swsm;
1482
1483	DEBUGFUNC("e1000_put_hw_semaphore_generic");
1484
1485	swsm = E1000_READ_REG(hw, E1000_SWSM);
1486
1487	swsm &= ~E1000_SWSM_SWESMBI;
1488
1489	E1000_WRITE_REG(hw, E1000_SWSM, swsm);
1490}
1491
1492/**
1493 *  e1000_get_auto_rd_done_generic - Check for auto read completion
1494 *  @hw - pointer to the HW structure
1495 *
1496 *  Check EEPROM for Auto Read done bit.
1497 **/
1498s32
1499e1000_get_auto_rd_done_generic(struct e1000_hw *hw)
1500{
1501	s32 i = 0;
1502	s32 ret_val = E1000_SUCCESS;
1503
1504	DEBUGFUNC("e1000_get_auto_rd_done_generic");
1505
1506	while (i < AUTO_READ_DONE_TIMEOUT) {
1507		if (E1000_READ_REG(hw, E1000_EECD) & E1000_EECD_AUTO_RD)
1508			break;
1509		msec_delay(1);
1510		i++;
1511	}
1512
1513	if (i == AUTO_READ_DONE_TIMEOUT) {
1514		DEBUGOUT("Auto read by HW from NVM has not completed.\n");
1515		ret_val = -E1000_ERR_RESET;
1516		goto out;
1517	}
1518
1519out:
1520	return ret_val;
1521}
1522
1523/**
1524 *  e1000_valid_led_default_generic - Verify a valid default LED config
1525 *  @hw - pointer to the HW structure
1526 *  @data - pointer to the NVM (EEPROM)
1527 *
1528 *  Read the EEPROM for the current default LED configuration.  If the
1529 *  LED configuration is not valid, set to a valid LED configuration.
1530 **/
1531s32
1532e1000_valid_led_default_generic(struct e1000_hw *hw, u16 *data)
1533{
1534	s32 ret_val;
1535
1536	DEBUGFUNC("e1000_valid_led_default_generic");
1537
1538	ret_val = e1000_read_nvm(hw, NVM_ID_LED_SETTINGS, 1, data);
1539	if (ret_val) {
1540		DEBUGOUT("NVM Read Error\n");
1541		goto out;
1542	}
1543
1544	if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF)
1545		*data = ID_LED_DEFAULT;
1546
1547out:
1548	return ret_val;
1549}
1550
1551/**
1552 *  e1000_id_led_init_generic -
1553 *  @hw - pointer to the HW structure
1554 *
1555 **/
1556s32
1557e1000_id_led_init_generic(struct e1000_hw * hw)
1558{
1559	struct e1000_mac_info *mac = &hw->mac;
1560	s32 ret_val;
1561	const u32 ledctl_mask = 0x000000FF;
1562	const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
1563	const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
1564	u16 data, i, temp;
1565	const u16 led_mask = 0x0F;
1566
1567	DEBUGFUNC("e1000_id_led_init_generic");
1568
1569	ret_val = hw->func.valid_led_default(hw, &data);
1570	if (ret_val)
1571		goto out;
1572
1573	mac->ledctl_default = E1000_READ_REG(hw, E1000_LEDCTL);
1574	mac->ledctl_mode1 = mac->ledctl_default;
1575	mac->ledctl_mode2 = mac->ledctl_default;
1576
1577	for (i = 0; i < 4; i++) {
1578		temp = (data >> (i << 2)) & led_mask;
1579		switch (temp) {
1580		case ID_LED_ON1_DEF2:
1581		case ID_LED_ON1_ON2:
1582		case ID_LED_ON1_OFF2:
1583			mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1584			mac->ledctl_mode1 |= ledctl_on << (i << 3);
1585			break;
1586		case ID_LED_OFF1_DEF2:
1587		case ID_LED_OFF1_ON2:
1588		case ID_LED_OFF1_OFF2:
1589			mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1590			mac->ledctl_mode1 |= ledctl_off << (i << 3);
1591			break;
1592		default:
1593			/* Do nothing */
1594			break;
1595		}
1596		switch (temp) {
1597		case ID_LED_DEF1_ON2:
1598		case ID_LED_ON1_ON2:
1599		case ID_LED_OFF1_ON2:
1600			mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1601			mac->ledctl_mode2 |= ledctl_on << (i << 3);
1602			break;
1603		case ID_LED_DEF1_OFF2:
1604		case ID_LED_ON1_OFF2:
1605		case ID_LED_OFF1_OFF2:
1606			mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1607			mac->ledctl_mode2 |= ledctl_off << (i << 3);
1608			break;
1609		default:
1610			/* Do nothing */
1611			break;
1612		}
1613	}
1614
1615out:
1616	return ret_val;
1617}
1618
1619/**
1620 *  e1000_setup_led_generic - Configures SW controllable LED
1621 *  @hw - pointer to the HW structure
1622 *
1623 *  This prepares the SW controllable LED for use and saves the current state
1624 *  of the LED so it can be later restored.
1625 **/
1626s32
1627e1000_setup_led_generic(struct e1000_hw *hw)
1628{
1629	u32 ledctl;
1630	s32 ret_val = E1000_SUCCESS;
1631
1632	DEBUGFUNC("e1000_setup_led_generic");
1633
1634	if (hw->func.setup_led != e1000_setup_led_generic) {
1635		ret_val = -E1000_ERR_CONFIG;
1636		goto out;
1637	}
1638
1639	if (hw->media_type == e1000_media_type_fiber) {
1640		ledctl = E1000_READ_REG(hw, E1000_LEDCTL);
1641		hw->mac.ledctl_default = ledctl;
1642		/* Turn off LED0 */
1643		ledctl &= ~(E1000_LEDCTL_LED0_IVRT |
1644		            E1000_LEDCTL_LED0_BLINK |
1645		            E1000_LEDCTL_LED0_MODE_MASK);
1646		ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
1647		           E1000_LEDCTL_LED0_MODE_SHIFT);
1648		E1000_WRITE_REG(hw, E1000_LEDCTL, ledctl);
1649	} else if (hw->media_type == e1000_media_type_copper) {
1650		E1000_WRITE_REG(hw, E1000_LEDCTL, hw->mac.ledctl_mode1);
1651	}
1652
1653out:
1654	return ret_val;
1655}
1656
1657/**
1658 *  e1000_cleanup_led_generic - Set LED config to default operation
1659 *  @hw - pointer to the HW structure
1660 *
1661 *  Remove the current LED configuration and set the LED configuration
1662 *  to the default value, saved from the EEPROM.
1663 **/
1664s32
1665e1000_cleanup_led_generic(struct e1000_hw *hw)
1666{
1667	s32 ret_val = E1000_SUCCESS;
1668
1669	DEBUGFUNC("e1000_cleanup_led_generic");
1670
1671	if (hw->func.cleanup_led != e1000_cleanup_led_generic) {
1672		ret_val = -E1000_ERR_CONFIG;
1673		goto out;
1674	}
1675
1676	E1000_WRITE_REG(hw, E1000_LEDCTL, hw->mac.ledctl_default);
1677
1678out:
1679	return ret_val;
1680}
1681
1682/**
1683 *  e1000_blink_led_generic - Blink LED
1684 *  @hw - pointer to the HW structure
1685 *
1686 *  Blink the led's which are set to be on.
1687 **/
1688s32
1689e1000_blink_led_generic(struct e1000_hw *hw)
1690{
1691	u32 ledctl_blink = 0;
1692	u32 i;
1693
1694	DEBUGFUNC("e1000_blink_led_generic");
1695
1696	if (hw->media_type == e1000_media_type_fiber) {
1697		/* always blink LED0 for PCI-E fiber */
1698		ledctl_blink = E1000_LEDCTL_LED0_BLINK |
1699		     (E1000_LEDCTL_MODE_LED_ON << E1000_LEDCTL_LED0_MODE_SHIFT);
1700	} else {
1701		/* set the blink bit for each LED that's "on" (0x0E)
1702		 * in ledctl_mode2 */
1703		ledctl_blink = hw->mac.ledctl_mode2;
1704		for (i = 0; i < 4; i++)
1705			if (((hw->mac.ledctl_mode2 >> (i * 8)) & 0xFF) ==
1706			    E1000_LEDCTL_MODE_LED_ON)
1707				ledctl_blink |= (E1000_LEDCTL_LED0_BLINK <<
1708				                 (i * 8));
1709	}
1710
1711	E1000_WRITE_REG(hw, E1000_LEDCTL, ledctl_blink);
1712
1713	return E1000_SUCCESS;
1714}
1715
1716/**
1717 *  e1000_led_on_generic - Turn LED on
1718 *  @hw - pointer to the HW structure
1719 *
1720 *  Turn LED on.
1721 **/
1722s32
1723e1000_led_on_generic(struct e1000_hw *hw)
1724{
1725	u32 ctrl;
1726
1727	DEBUGFUNC("e1000_led_on_generic");
1728
1729	switch (hw->media_type) {
1730	case e1000_media_type_fiber:
1731		ctrl = E1000_READ_REG(hw, E1000_CTRL);
1732		ctrl &= ~E1000_CTRL_SWDPIN0;
1733		ctrl |= E1000_CTRL_SWDPIO0;
1734		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
1735		break;
1736	case e1000_media_type_copper:
1737		E1000_WRITE_REG(hw, E1000_LEDCTL, hw->mac.ledctl_mode2);
1738		break;
1739	default:
1740		break;
1741	}
1742
1743	return E1000_SUCCESS;
1744}
1745
1746/**
1747 *  e1000_led_off_generic - Turn LED off
1748 *  @hw - pointer to the HW structure
1749 *
1750 *  Turn LED off.
1751 **/
1752s32
1753e1000_led_off_generic(struct e1000_hw *hw)
1754{
1755	u32 ctrl;
1756
1757	DEBUGFUNC("e1000_led_off_generic");
1758
1759	switch (hw->media_type) {
1760	case e1000_media_type_fiber:
1761		ctrl = E1000_READ_REG(hw, E1000_CTRL);
1762		ctrl |= E1000_CTRL_SWDPIN0;
1763		ctrl |= E1000_CTRL_SWDPIO0;
1764		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
1765		break;
1766	case e1000_media_type_copper:
1767		E1000_WRITE_REG(hw, E1000_LEDCTL, hw->mac.ledctl_mode1);
1768		break;
1769	default:
1770		break;
1771	}
1772
1773	return E1000_SUCCESS;
1774}
1775
1776/**
1777 *  e1000_set_pcie_no_snoop_generic - Set PCI-express capabilities
1778 *  @hw - pointer to the HW structure
1779 *  @no_snoop - bitmap of snoop events
1780 *
1781 *  Set the PCI-express register to snoop for events enabled in 'no_snoop'.
1782 **/
1783void
1784e1000_set_pcie_no_snoop_generic(struct e1000_hw *hw, u32 no_snoop)
1785{
1786	u32 gcr;
1787
1788	DEBUGFUNC("e1000_set_pcie_no_snoop_generic");
1789
1790	if (hw->bus.type != e1000_bus_type_pci_express)
1791		goto out;
1792
1793	if (no_snoop) {
1794		gcr = E1000_READ_REG(hw, E1000_GCR);
1795		gcr &= ~(PCIE_NO_SNOOP_ALL);
1796		gcr |= no_snoop;
1797		E1000_WRITE_REG(hw, E1000_GCR, gcr);
1798	}
1799out:
1800	return;
1801}
1802
1803/**
1804 *  e1000_disable_pcie_master_generic - Disables PCI-express master access
1805 *  @hw - pointer to the HW structure
1806 *
1807 *  Returns 0 (E1000_SUCCESS) if successful, else returns -10
1808 *  (-E1000_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not casued
1809 *  the master requests to be disabled.
1810 *
1811 *  Disables PCI-Express master access and verifies there are no pending
1812 *  requests.
1813 **/
1814s32
1815e1000_disable_pcie_master_generic(struct e1000_hw *hw)
1816{
1817	u32 ctrl;
1818	s32 timeout = MASTER_DISABLE_TIMEOUT;
1819	s32 ret_val = E1000_SUCCESS;
1820
1821	DEBUGFUNC("e1000_disable_pcie_master_generic");
1822
1823	if (hw->bus.type != e1000_bus_type_pci_express)
1824		goto out;
1825
1826	ctrl = E1000_READ_REG(hw, E1000_CTRL);
1827	ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
1828	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
1829
1830	while (timeout) {
1831		if (!(E1000_READ_REG(hw, E1000_STATUS) &
1832		      E1000_STATUS_GIO_MASTER_ENABLE))
1833			break;
1834		usec_delay(100);
1835		timeout--;
1836	}
1837
1838	if (!timeout) {
1839		DEBUGOUT("Master requests are pending.\n");
1840		ret_val = -E1000_ERR_MASTER_REQUESTS_PENDING;
1841		goto out;
1842	}
1843
1844out:
1845	return ret_val;
1846}
1847
1848/**
1849 *  e1000_reset_adaptive_generic - Reset Adaptive Interframe Spacing
1850 *  @hw - pointer to the HW structure
1851 *
1852 *  Reset the Adaptive Interframe Spacing throttle to default values.
1853 **/
1854void
1855e1000_reset_adaptive_generic(struct e1000_hw *hw)
1856{
1857	struct e1000_mac_info *mac = &hw->mac;
1858
1859	DEBUGFUNC("e1000_reset_adaptive_generic");
1860
1861	if (!mac->adaptive_ifs) {
1862		DEBUGOUT("Not in Adaptive IFS mode!\n");
1863		goto out;
1864	}
1865
1866	if (!mac->ifs_params_forced) {
1867		mac->current_ifs_val = 0;
1868		mac->ifs_min_val = IFS_MIN;
1869		mac->ifs_max_val = IFS_MAX;
1870		mac->ifs_step_size = IFS_STEP;
1871		mac->ifs_ratio = IFS_RATIO;
1872	}
1873
1874	mac->in_ifs_mode = FALSE;
1875	E1000_WRITE_REG(hw, E1000_AIT, 0);
1876out:
1877	return;
1878}
1879
1880/**
1881 *  e1000_update_adaptive_generic - Update Adaptive Interframe Spacing
1882 *  @hw - pointer to the HW structure
1883 *
1884 *  Update the Adaptive Interframe Spacing Throttle value based on the
1885 *  time between transmitted packets and time between collisions.
1886 **/
1887void
1888e1000_update_adaptive_generic(struct e1000_hw *hw)
1889{
1890	struct e1000_mac_info *mac = &hw->mac;
1891
1892	DEBUGFUNC("e1000_update_adaptive_generic");
1893
1894	if (!mac->adaptive_ifs) {
1895		DEBUGOUT("Not in Adaptive IFS mode!\n");
1896		goto out;
1897	}
1898
1899	if ((mac->collision_delta * mac->ifs_ratio) > mac->tx_packet_delta) {
1900		if (mac->tx_packet_delta > MIN_NUM_XMITS) {
1901			mac->in_ifs_mode = TRUE;
1902			if (mac->current_ifs_val < mac->ifs_max_val) {
1903				if (!mac->current_ifs_val)
1904					mac->current_ifs_val = mac->ifs_min_val;
1905				else
1906					mac->current_ifs_val +=
1907						mac->ifs_step_size;
1908				E1000_WRITE_REG(hw, E1000_AIT, mac->current_ifs_val);
1909			}
1910		}
1911	} else {
1912		if (mac->in_ifs_mode &&
1913		    (mac->tx_packet_delta <= MIN_NUM_XMITS)) {
1914			mac->current_ifs_val = 0;
1915			mac->in_ifs_mode = FALSE;
1916			E1000_WRITE_REG(hw, E1000_AIT, 0);
1917		}
1918	}
1919out:
1920	return;
1921}
1922
1923/**
1924 *  e1000_validate_mdi_setting_generic - Verify MDI/MDIx settings
1925 *  @hw - pointer to the HW structure
1926 *
1927 *  Verify that when not using auto-negotitation that MDI/MDIx is correctly
1928 *  set, which is forced to MDI mode only.
1929 **/
1930s32
1931e1000_validate_mdi_setting_generic(struct e1000_hw *hw)
1932{
1933	s32 ret_val = E1000_SUCCESS;
1934
1935	DEBUGFUNC("e1000_validate_mdi_setting_generic");
1936
1937	if (!hw->mac.autoneg && (hw->phy.mdix == 0 || hw->phy.mdix == 3)) {
1938		DEBUGOUT("Invalid MDI setting detected\n");
1939		hw->phy.mdix = 1;
1940		ret_val = -E1000_ERR_CONFIG;
1941		goto out;
1942	}
1943
1944out:
1945	return ret_val;
1946}
1947
1948/**
1949 *  e1000_write_8bit_ctrl_reg_generic - Write a 8bit CTRL register
1950 *  @hw - pointer to the HW structure
1951 *  @reg - 32bit register offset such as E1000_SCTL
1952 *  @offset - register offset to write to
1953 *  @data - data to write at register offset
1954 *
1955 *  Writes an address/data control type register.  There are several of these
1956 *  and they all have the format address << 8 | data and bit 31 is polled for
1957 *  completion.
1958 **/
1959s32
1960e1000_write_8bit_ctrl_reg_generic(struct e1000_hw *hw, u32 reg,
1961                                  u32 offset, u8 data)
1962{
1963	u32 i, regvalue = 0;
1964	s32 ret_val = E1000_SUCCESS;
1965
1966	DEBUGFUNC("e1000_write_8bit_ctrl_reg_generic");
1967
1968	/* Set up the address and data */
1969	regvalue = ((u32)data) | (offset << E1000_GEN_CTL_ADDRESS_SHIFT);
1970	E1000_WRITE_REG(hw, reg, regvalue);
1971
1972	/* Poll the ready bit to see if the MDI read completed */
1973	for (i = 0; i < E1000_GEN_POLL_TIMEOUT; i++) {
1974		usec_delay(5);
1975		regvalue = E1000_READ_REG(hw, reg);
1976		if (regvalue & E1000_GEN_CTL_READY)
1977			break;
1978	}
1979	if (!(regvalue & E1000_GEN_CTL_READY)) {
1980		DEBUGOUT1("Reg %08x did not indicate ready\n", reg);
1981		ret_val = -E1000_ERR_PHY;
1982		goto out;
1983	}
1984
1985out:
1986	return ret_val;
1987}
1988