xref: /linux/drivers/net/ethernet/intel/e1000e/mac.c (revision 4f9786035f9e519db41375818e1d0b5f20da2f10)
1ae06c70bSJeff Kirsher // SPDX-License-Identifier: GPL-2.0
251dce24bSJeff Kirsher /* Copyright(c) 1999 - 2018 Intel Corporation. */
3bc7f75faSAuke Kok 
44c39e768SIlpo Järvinen #include <linux/bitfield.h>
54c39e768SIlpo Järvinen 
6bc7f75faSAuke Kok #include "e1000.h"
7bc7f75faSAuke Kok 
8bc7f75faSAuke Kok /**
9bc7f75faSAuke Kok  *  e1000e_get_bus_info_pcie - Get PCIe bus information
10bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
11bc7f75faSAuke Kok  *
12bc7f75faSAuke Kok  *  Determines and stores the system bus information for a particular
13bc7f75faSAuke Kok  *  network interface.  The following bus information is determined and stored:
14bc7f75faSAuke Kok  *  bus speed, bus width, type (PCIe), and PCIe function.
15bc7f75faSAuke Kok  **/
e1000e_get_bus_info_pcie(struct e1000_hw * hw)16bc7f75faSAuke Kok s32 e1000e_get_bus_info_pcie(struct e1000_hw *hw)
17bc7f75faSAuke Kok {
18bf88f7d9SIlpo Järvinen 	struct pci_dev *pdev = hw->adapter->pdev;
19f4d2dd4cSBruce Allan 	struct e1000_mac_info *mac = &hw->mac;
20bc7f75faSAuke Kok 	struct e1000_bus_info *bus = &hw->bus;
21bf88f7d9SIlpo Järvinen 	u16 pcie_link_status;
22bc7f75faSAuke Kok 
23bf88f7d9SIlpo Järvinen 	if (!pci_pcie_cap(pdev)) {
24bc7f75faSAuke Kok 		bus->width = e1000_bus_width_unknown;
25bc7f75faSAuke Kok 	} else {
26bf88f7d9SIlpo Järvinen 		pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &pcie_link_status);
274c39e768SIlpo Järvinen 		bus->width = (enum e1000_bus_width)FIELD_GET(PCI_EXP_LNKSTA_NLW,
284c39e768SIlpo Järvinen 							     pcie_link_status);
29bc7f75faSAuke Kok 	}
30bc7f75faSAuke Kok 
31f4d2dd4cSBruce Allan 	mac->ops.set_lan_id(hw);
32bc7f75faSAuke Kok 
33bc7f75faSAuke Kok 	return 0;
34bc7f75faSAuke Kok }
35bc7f75faSAuke Kok 
36bc7f75faSAuke Kok /**
37f4d2dd4cSBruce Allan  *  e1000_set_lan_id_multi_port_pcie - Set LAN id for PCIe multiple port devices
38f4d2dd4cSBruce Allan  *
39f4d2dd4cSBruce Allan  *  @hw: pointer to the HW structure
40f4d2dd4cSBruce Allan  *
41f4d2dd4cSBruce Allan  *  Determines the LAN function id by reading memory-mapped registers
42f4d2dd4cSBruce Allan  *  and swaps the port value if requested.
43f4d2dd4cSBruce Allan  **/
e1000_set_lan_id_multi_port_pcie(struct e1000_hw * hw)44f4d2dd4cSBruce Allan void e1000_set_lan_id_multi_port_pcie(struct e1000_hw *hw)
45f4d2dd4cSBruce Allan {
46f4d2dd4cSBruce Allan 	struct e1000_bus_info *bus = &hw->bus;
47f4d2dd4cSBruce Allan 	u32 reg;
48f4d2dd4cSBruce Allan 
49e921eb1aSBruce Allan 	/* The status register reports the correct function number
50f4d2dd4cSBruce Allan 	 * for the device regardless of function swap state.
51f4d2dd4cSBruce Allan 	 */
52f4d2dd4cSBruce Allan 	reg = er32(STATUS);
53b9a45254SJesse Brandeburg 	bus->func = FIELD_GET(E1000_STATUS_FUNC_MASK, reg);
54f4d2dd4cSBruce Allan }
55f4d2dd4cSBruce Allan 
56f4d2dd4cSBruce Allan /**
57f4d2dd4cSBruce Allan  *  e1000_set_lan_id_single_port - Set LAN id for a single port device
58f4d2dd4cSBruce Allan  *  @hw: pointer to the HW structure
59f4d2dd4cSBruce Allan  *
60f4d2dd4cSBruce Allan  *  Sets the LAN function id to zero for a single port device.
61f4d2dd4cSBruce Allan  **/
e1000_set_lan_id_single_port(struct e1000_hw * hw)62f4d2dd4cSBruce Allan void e1000_set_lan_id_single_port(struct e1000_hw *hw)
63f4d2dd4cSBruce Allan {
64f4d2dd4cSBruce Allan 	struct e1000_bus_info *bus = &hw->bus;
65f4d2dd4cSBruce Allan 
66f4d2dd4cSBruce Allan 	bus->func = 0;
67f4d2dd4cSBruce Allan }
68f4d2dd4cSBruce Allan 
69f4d2dd4cSBruce Allan /**
70caaddaf8SBruce Allan  *  e1000_clear_vfta_generic - Clear VLAN filter table
71caaddaf8SBruce Allan  *  @hw: pointer to the HW structure
72caaddaf8SBruce Allan  *
73caaddaf8SBruce Allan  *  Clears the register array which contains the VLAN filter table by
74caaddaf8SBruce Allan  *  setting all the values to 0.
75caaddaf8SBruce Allan  **/
e1000_clear_vfta_generic(struct e1000_hw * hw)76caaddaf8SBruce Allan void e1000_clear_vfta_generic(struct e1000_hw *hw)
77caaddaf8SBruce Allan {
78caaddaf8SBruce Allan 	u32 offset;
79caaddaf8SBruce Allan 
80caaddaf8SBruce Allan 	for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
81caaddaf8SBruce Allan 		E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, 0);
82caaddaf8SBruce Allan 		e1e_flush();
83caaddaf8SBruce Allan 	}
84caaddaf8SBruce Allan }
85caaddaf8SBruce Allan 
86caaddaf8SBruce Allan /**
87caaddaf8SBruce Allan  *  e1000_write_vfta_generic - Write value to VLAN filter table
88bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
89bc7f75faSAuke Kok  *  @offset: register offset in VLAN filter table
90bc7f75faSAuke Kok  *  @value: register value written to VLAN filter table
91bc7f75faSAuke Kok  *
92bc7f75faSAuke Kok  *  Writes value at the given offset in the register array which stores
93bc7f75faSAuke Kok  *  the VLAN filter table.
94bc7f75faSAuke Kok  **/
e1000_write_vfta_generic(struct e1000_hw * hw,u32 offset,u32 value)95caaddaf8SBruce Allan void e1000_write_vfta_generic(struct e1000_hw *hw, u32 offset, u32 value)
96bc7f75faSAuke Kok {
97bc7f75faSAuke Kok 	E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, value);
98bc7f75faSAuke Kok 	e1e_flush();
99bc7f75faSAuke Kok }
100bc7f75faSAuke Kok 
101bc7f75faSAuke Kok /**
102bc7f75faSAuke Kok  *  e1000e_init_rx_addrs - Initialize receive address's
103bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
104bc7f75faSAuke Kok  *  @rar_count: receive address registers
105bc7f75faSAuke Kok  *
106d64a6f4dSBruce Allan  *  Setup the receive address registers by setting the base receive address
107bc7f75faSAuke Kok  *  register to the devices MAC address and clearing all the other receive
108bc7f75faSAuke Kok  *  address registers to 0.
109bc7f75faSAuke Kok  **/
e1000e_init_rx_addrs(struct e1000_hw * hw,u16 rar_count)110bc7f75faSAuke Kok void e1000e_init_rx_addrs(struct e1000_hw *hw, u16 rar_count)
111bc7f75faSAuke Kok {
112bc7f75faSAuke Kok 	u32 i;
113b7a9216cSBruce Allan 	u8 mac_addr[ETH_ALEN] = { 0 };
114bc7f75faSAuke Kok 
115bc7f75faSAuke Kok 	/* Setup the receive address */
1163bb99fe2SBruce Allan 	e_dbg("Programming MAC Address into RAR[0]\n");
117bc7f75faSAuke Kok 
11869e1e019SBruce Allan 	hw->mac.ops.rar_set(hw, hw->mac.addr, 0);
119bc7f75faSAuke Kok 
120bc7f75faSAuke Kok 	/* Zero out the other (rar_entry_count - 1) receive addresses */
1213bb99fe2SBruce Allan 	e_dbg("Clearing RAR[1-%u]\n", rar_count - 1);
122b7a9216cSBruce Allan 	for (i = 1; i < rar_count; i++)
12369e1e019SBruce Allan 		hw->mac.ops.rar_set(hw, mac_addr, i);
124bc7f75faSAuke Kok }
125bc7f75faSAuke Kok 
126bc7f75faSAuke Kok /**
127608f8a0dSBruce Allan  *  e1000_check_alt_mac_addr_generic - Check for alternate MAC addr
128608f8a0dSBruce Allan  *  @hw: pointer to the HW structure
129608f8a0dSBruce Allan  *
130608f8a0dSBruce Allan  *  Checks the nvm for an alternate MAC address.  An alternate MAC address
131608f8a0dSBruce Allan  *  can be setup by pre-boot software and must be treated like a permanent
132608f8a0dSBruce Allan  *  address and must override the actual permanent MAC address. If an
133608f8a0dSBruce Allan  *  alternate MAC address is found it is programmed into RAR0, replacing
134608f8a0dSBruce Allan  *  the permanent address that was installed into RAR0 by the Si on reset.
135608f8a0dSBruce Allan  *  This function will return SUCCESS unless it encounters an error while
136608f8a0dSBruce Allan  *  reading the EEPROM.
137608f8a0dSBruce Allan  **/
e1000_check_alt_mac_addr_generic(struct e1000_hw * hw)138608f8a0dSBruce Allan s32 e1000_check_alt_mac_addr_generic(struct e1000_hw *hw)
139608f8a0dSBruce Allan {
140608f8a0dSBruce Allan 	u32 i;
14170806a7fSBruce Allan 	s32 ret_val;
142608f8a0dSBruce Allan 	u16 offset, nvm_alt_mac_addr_offset, nvm_data;
143608f8a0dSBruce Allan 	u8 alt_mac_addr[ETH_ALEN];
144608f8a0dSBruce Allan 
1451aef70efSBruce Allan 	ret_val = e1000_read_nvm(hw, NVM_COMPAT, 1, &nvm_data);
1461aef70efSBruce Allan 	if (ret_val)
1475015e53aSBruce Allan 		return ret_val;
1481aef70efSBruce Allan 
1494bcf053bSBruce Allan 	/* not supported on 82573 */
1504bcf053bSBruce Allan 	if (hw->mac.type == e1000_82573)
1515015e53aSBruce Allan 		return 0;
1521aef70efSBruce Allan 
153608f8a0dSBruce Allan 	ret_val = e1000_read_nvm(hw, NVM_ALT_MAC_ADDR_PTR, 1,
154608f8a0dSBruce Allan 				 &nvm_alt_mac_addr_offset);
155608f8a0dSBruce Allan 	if (ret_val) {
156608f8a0dSBruce Allan 		e_dbg("NVM Read Error\n");
1575015e53aSBruce Allan 		return ret_val;
158608f8a0dSBruce Allan 	}
159608f8a0dSBruce Allan 
160244735f6SBruce Allan 	if ((nvm_alt_mac_addr_offset == 0xFFFF) ||
161244735f6SBruce Allan 	    (nvm_alt_mac_addr_offset == 0x0000))
162608f8a0dSBruce Allan 		/* There is no Alternate MAC Address */
1635015e53aSBruce Allan 		return 0;
164608f8a0dSBruce Allan 
165608f8a0dSBruce Allan 	if (hw->bus.func == E1000_FUNC_1)
166608f8a0dSBruce Allan 		nvm_alt_mac_addr_offset += E1000_ALT_MAC_ADDRESS_OFFSET_LAN1;
167608f8a0dSBruce Allan 	for (i = 0; i < ETH_ALEN; i += 2) {
168608f8a0dSBruce Allan 		offset = nvm_alt_mac_addr_offset + (i >> 1);
169608f8a0dSBruce Allan 		ret_val = e1000_read_nvm(hw, offset, 1, &nvm_data);
170608f8a0dSBruce Allan 		if (ret_val) {
171608f8a0dSBruce Allan 			e_dbg("NVM Read Error\n");
1725015e53aSBruce Allan 			return ret_val;
173608f8a0dSBruce Allan 		}
174608f8a0dSBruce Allan 
175608f8a0dSBruce Allan 		alt_mac_addr[i] = (u8)(nvm_data & 0xFF);
176608f8a0dSBruce Allan 		alt_mac_addr[i + 1] = (u8)(nvm_data >> 8);
177608f8a0dSBruce Allan 	}
178608f8a0dSBruce Allan 
179608f8a0dSBruce Allan 	/* if multicast bit is set, the alternate address will not be used */
1803e714ad3STobias Klauser 	if (is_multicast_ether_addr(alt_mac_addr)) {
181608f8a0dSBruce Allan 		e_dbg("Ignoring Alternate Mac Address with MC bit set\n");
1825015e53aSBruce Allan 		return 0;
183608f8a0dSBruce Allan 	}
184608f8a0dSBruce Allan 
185e921eb1aSBruce Allan 	/* We have a valid alternate MAC address, and we want to treat it the
186608f8a0dSBruce Allan 	 * same as the normal permanent MAC address stored by the HW into the
187608f8a0dSBruce Allan 	 * RAR. Do this by mapping this address into RAR0.
188608f8a0dSBruce Allan 	 */
18969e1e019SBruce Allan 	hw->mac.ops.rar_set(hw, alt_mac_addr, 0);
190608f8a0dSBruce Allan 
1915015e53aSBruce Allan 	return 0;
192608f8a0dSBruce Allan }
193608f8a0dSBruce Allan 
e1000e_rar_get_count_generic(struct e1000_hw * hw)194b3e5bf1fSDavid Ertman u32 e1000e_rar_get_count_generic(struct e1000_hw *hw)
195b3e5bf1fSDavid Ertman {
196b3e5bf1fSDavid Ertman 	return hw->mac.rar_entry_count;
197b3e5bf1fSDavid Ertman }
198b3e5bf1fSDavid Ertman 
199608f8a0dSBruce Allan /**
20069e1e019SBruce Allan  *  e1000e_rar_set_generic - Set receive address register
201bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
202bc7f75faSAuke Kok  *  @addr: pointer to the receive address
203bc7f75faSAuke Kok  *  @index: receive address array register
204bc7f75faSAuke Kok  *
205bc7f75faSAuke Kok  *  Sets the receive address array register at index to the address passed
206bc7f75faSAuke Kok  *  in by addr.
207bc7f75faSAuke Kok  **/
e1000e_rar_set_generic(struct e1000_hw * hw,u8 * addr,u32 index)208b3e5bf1fSDavid Ertman int e1000e_rar_set_generic(struct e1000_hw *hw, u8 *addr, u32 index)
209bc7f75faSAuke Kok {
210bc7f75faSAuke Kok 	u32 rar_low, rar_high;
211bc7f75faSAuke Kok 
212e921eb1aSBruce Allan 	/* HW expects these in little endian so we reverse the byte order
213bc7f75faSAuke Kok 	 * from network order (big endian) to little endian
214bc7f75faSAuke Kok 	 */
215fe2ddfb5SBruce Allan 	rar_low = ((u32)addr[0] | ((u32)addr[1] << 8) |
216bc7f75faSAuke Kok 		   ((u32)addr[2] << 16) | ((u32)addr[3] << 24));
217bc7f75faSAuke Kok 
218bc7f75faSAuke Kok 	rar_high = ((u32)addr[4] | ((u32)addr[5] << 8));
219bc7f75faSAuke Kok 
220b7a9216cSBruce Allan 	/* If MAC address zero, no need to set the AV bit */
221b7a9216cSBruce Allan 	if (rar_low || rar_high)
222bc7f75faSAuke Kok 		rar_high |= E1000_RAH_AV;
223bc7f75faSAuke Kok 
224e921eb1aSBruce Allan 	/* Some bridges will combine consecutive 32-bit writes into
225b7a9216cSBruce Allan 	 * a single burst write, which will malfunction on some parts.
226b7a9216cSBruce Allan 	 * The flushes avoid this.
227b7a9216cSBruce Allan 	 */
228b7a9216cSBruce Allan 	ew32(RAL(index), rar_low);
229b7a9216cSBruce Allan 	e1e_flush();
230b7a9216cSBruce Allan 	ew32(RAH(index), rar_high);
231b7a9216cSBruce Allan 	e1e_flush();
232b3e5bf1fSDavid Ertman 
233b3e5bf1fSDavid Ertman 	return 0;
234bc7f75faSAuke Kok }
235bc7f75faSAuke Kok 
236bc7f75faSAuke Kok /**
237bc7f75faSAuke Kok  *  e1000_hash_mc_addr - Generate a multicast hash value
238bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
239bc7f75faSAuke Kok  *  @mc_addr: pointer to a multicast address
240bc7f75faSAuke Kok  *
241bc7f75faSAuke Kok  *  Generates a multicast address hash value which is used to determine
242b2a50e1aSBruce Allan  *  the multicast filter table array address and new table value.
243bc7f75faSAuke Kok  **/
e1000_hash_mc_addr(struct e1000_hw * hw,u8 * mc_addr)244bc7f75faSAuke Kok static u32 e1000_hash_mc_addr(struct e1000_hw *hw, u8 *mc_addr)
245bc7f75faSAuke Kok {
246bc7f75faSAuke Kok 	u32 hash_value, hash_mask;
247bc7f75faSAuke Kok 	u8 bit_shift = 0;
248bc7f75faSAuke Kok 
249bc7f75faSAuke Kok 	/* Register count multiplied by bits per register */
250bc7f75faSAuke Kok 	hash_mask = (hw->mac.mta_reg_count * 32) - 1;
251bc7f75faSAuke Kok 
252e921eb1aSBruce Allan 	/* For a mc_filter_type of 0, bit_shift is the number of left-shifts
253ad68076eSBruce Allan 	 * where 0xFF would still fall within the hash mask.
254ad68076eSBruce Allan 	 */
255bc7f75faSAuke Kok 	while (hash_mask >> bit_shift != 0xFF)
256bc7f75faSAuke Kok 		bit_shift++;
257bc7f75faSAuke Kok 
258e921eb1aSBruce Allan 	/* The portion of the address that is used for the hash table
259bc7f75faSAuke Kok 	 * is determined by the mc_filter_type setting.
260bc7f75faSAuke Kok 	 * The algorithm is such that there is a total of 8 bits of shifting.
261bc7f75faSAuke Kok 	 * The bit_shift for a mc_filter_type of 0 represents the number of
262bc7f75faSAuke Kok 	 * left-shifts where the MSB of mc_addr[5] would still fall within
263bc7f75faSAuke Kok 	 * the hash_mask.  Case 0 does this exactly.  Since there are a total
264bc7f75faSAuke Kok 	 * of 8 bits of shifting, then mc_addr[4] will shift right the
265bc7f75faSAuke Kok 	 * remaining number of bits. Thus 8 - bit_shift.  The rest of the
266bc7f75faSAuke Kok 	 * cases are a variation of this algorithm...essentially raising the
267bc7f75faSAuke Kok 	 * number of bits to shift mc_addr[5] left, while still keeping the
268bc7f75faSAuke Kok 	 * 8-bit shifting total.
269ad68076eSBruce Allan 	 *
270ad68076eSBruce Allan 	 * For example, given the following Destination MAC Address and an
271bc7f75faSAuke Kok 	 * mta register count of 128 (thus a 4096-bit vector and 0xFFF mask),
272bc7f75faSAuke Kok 	 * we can see that the bit_shift for case 0 is 4.  These are the hash
273bc7f75faSAuke Kok 	 * values resulting from each mc_filter_type...
274bc7f75faSAuke Kok 	 * [0] [1] [2] [3] [4] [5]
275bc7f75faSAuke Kok 	 * 01  AA  00  12  34  56
276bc7f75faSAuke Kok 	 * LSB           MSB
277bc7f75faSAuke Kok 	 *
278bc7f75faSAuke Kok 	 * case 0: hash_value = ((0x34 >> 4) | (0x56 << 4)) & 0xFFF = 0x563
279bc7f75faSAuke Kok 	 * case 1: hash_value = ((0x34 >> 3) | (0x56 << 5)) & 0xFFF = 0xAC6
280bc7f75faSAuke Kok 	 * case 2: hash_value = ((0x34 >> 2) | (0x56 << 6)) & 0xFFF = 0x163
281bc7f75faSAuke Kok 	 * case 3: hash_value = ((0x34 >> 0) | (0x56 << 8)) & 0xFFF = 0x634
282bc7f75faSAuke Kok 	 */
283bc7f75faSAuke Kok 	switch (hw->mac.mc_filter_type) {
284bc7f75faSAuke Kok 	default:
285bc7f75faSAuke Kok 	case 0:
286bc7f75faSAuke Kok 		break;
287bc7f75faSAuke Kok 	case 1:
288bc7f75faSAuke Kok 		bit_shift += 1;
289bc7f75faSAuke Kok 		break;
290bc7f75faSAuke Kok 	case 2:
291bc7f75faSAuke Kok 		bit_shift += 2;
292bc7f75faSAuke Kok 		break;
293bc7f75faSAuke Kok 	case 3:
294bc7f75faSAuke Kok 		bit_shift += 4;
295bc7f75faSAuke Kok 		break;
296bc7f75faSAuke Kok 	}
297bc7f75faSAuke Kok 
298bc7f75faSAuke Kok 	hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
299bc7f75faSAuke Kok 				   (((u16)mc_addr[5]) << bit_shift)));
300bc7f75faSAuke Kok 
301bc7f75faSAuke Kok 	return hash_value;
302bc7f75faSAuke Kok }
303bc7f75faSAuke Kok 
304bc7f75faSAuke Kok /**
305e2de3eb6SJeff Kirsher  *  e1000e_update_mc_addr_list_generic - Update Multicast addresses
306bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
307bc7f75faSAuke Kok  *  @mc_addr_list: array of multicast addresses to program
308bc7f75faSAuke Kok  *  @mc_addr_count: number of multicast addresses to program
309bc7f75faSAuke Kok  *
310ab8932f3SBruce Allan  *  Updates entire Multicast Table Array.
311bc7f75faSAuke Kok  *  The caller must have a packed mc_addr_list of multicast addresses.
312bc7f75faSAuke Kok  **/
e1000e_update_mc_addr_list_generic(struct e1000_hw * hw,u8 * mc_addr_list,u32 mc_addr_count)313e2de3eb6SJeff Kirsher void e1000e_update_mc_addr_list_generic(struct e1000_hw *hw,
314ab8932f3SBruce Allan 					u8 *mc_addr_list, u32 mc_addr_count)
315bc7f75faSAuke Kok {
316ab8932f3SBruce Allan 	u32 hash_value, hash_bit, hash_reg;
317ab8932f3SBruce Allan 	int i;
318a72d2b2cSJesse Brandeburg 
319ab8932f3SBruce Allan 	/* clear mta_shadow */
320ab8932f3SBruce Allan 	memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
321bc7f75faSAuke Kok 
322ab8932f3SBruce Allan 	/* update mta_shadow from mc_addr_list */
323ab8932f3SBruce Allan 	for (i = 0; (u32)i < mc_addr_count; i++) {
324bc7f75faSAuke Kok 		hash_value = e1000_hash_mc_addr(hw, mc_addr_list);
325ab8932f3SBruce Allan 
326a72d2b2cSJesse Brandeburg 		hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
327a72d2b2cSJesse Brandeburg 		hash_bit = hash_value & 0x1F;
328ab8932f3SBruce Allan 
32918dd2392SJacob Keller 		hw->mac.mta_shadow[hash_reg] |= BIT(hash_bit);
330ab8932f3SBruce Allan 		mc_addr_list += (ETH_ALEN);
331bc7f75faSAuke Kok 	}
332a72d2b2cSJesse Brandeburg 
333ab8932f3SBruce Allan 	/* replace the entire MTA table */
334*13e22972SGerhard Engleder 	for (i = hw->mac.mta_reg_count - 1; i >= 0; i--) {
335ab8932f3SBruce Allan 		E1000_WRITE_REG_ARRAY(hw, E1000_MTA, i, hw->mac.mta_shadow[i]);
336*13e22972SGerhard Engleder 
337*13e22972SGerhard Engleder 		if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
338*13e22972SGerhard Engleder 			/*
339*13e22972SGerhard Engleder 			 * Do not queue up too many posted writes to prevent
340*13e22972SGerhard Engleder 			 * increased latency for other devices on the
341*13e22972SGerhard Engleder 			 * interconnect. Flush after each 8th posted write,
342*13e22972SGerhard Engleder 			 * to keep additional execution time low while still
343*13e22972SGerhard Engleder 			 * preventing increased latency.
344*13e22972SGerhard Engleder 			 */
345*13e22972SGerhard Engleder 			if (!(i % 8) && i)
346*13e22972SGerhard Engleder 				e1e_flush();
347*13e22972SGerhard Engleder 		}
348*13e22972SGerhard Engleder 	}
349a72d2b2cSJesse Brandeburg 	e1e_flush();
350bc7f75faSAuke Kok }
351bc7f75faSAuke Kok 
352bc7f75faSAuke Kok /**
353bc7f75faSAuke Kok  *  e1000e_clear_hw_cntrs_base - Clear base hardware counters
354bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
355bc7f75faSAuke Kok  *
356bc7f75faSAuke Kok  *  Clears the base hardware counters by reading the counter registers.
357bc7f75faSAuke Kok  **/
e1000e_clear_hw_cntrs_base(struct e1000_hw * hw)358bc7f75faSAuke Kok void e1000e_clear_hw_cntrs_base(struct e1000_hw *hw)
359bc7f75faSAuke Kok {
36099673d9bSBruce Allan 	er32(CRCERRS);
36199673d9bSBruce Allan 	er32(SYMERRS);
36299673d9bSBruce Allan 	er32(MPC);
36399673d9bSBruce Allan 	er32(SCC);
36499673d9bSBruce Allan 	er32(ECOL);
36599673d9bSBruce Allan 	er32(MCC);
36699673d9bSBruce Allan 	er32(LATECOL);
36799673d9bSBruce Allan 	er32(COLC);
36899673d9bSBruce Allan 	er32(DC);
36999673d9bSBruce Allan 	er32(SEC);
37099673d9bSBruce Allan 	er32(RLEC);
37199673d9bSBruce Allan 	er32(XONRXC);
37299673d9bSBruce Allan 	er32(XONTXC);
37399673d9bSBruce Allan 	er32(XOFFRXC);
37499673d9bSBruce Allan 	er32(XOFFTXC);
37599673d9bSBruce Allan 	er32(FCRUC);
37699673d9bSBruce Allan 	er32(GPRC);
37799673d9bSBruce Allan 	er32(BPRC);
37899673d9bSBruce Allan 	er32(MPRC);
37999673d9bSBruce Allan 	er32(GPTC);
38099673d9bSBruce Allan 	er32(GORCL);
38199673d9bSBruce Allan 	er32(GORCH);
38299673d9bSBruce Allan 	er32(GOTCL);
38399673d9bSBruce Allan 	er32(GOTCH);
38499673d9bSBruce Allan 	er32(RNBC);
38599673d9bSBruce Allan 	er32(RUC);
38699673d9bSBruce Allan 	er32(RFC);
38799673d9bSBruce Allan 	er32(ROC);
38899673d9bSBruce Allan 	er32(RJC);
38999673d9bSBruce Allan 	er32(TORL);
39099673d9bSBruce Allan 	er32(TORH);
39199673d9bSBruce Allan 	er32(TOTL);
39299673d9bSBruce Allan 	er32(TOTH);
39399673d9bSBruce Allan 	er32(TPR);
39499673d9bSBruce Allan 	er32(TPT);
39599673d9bSBruce Allan 	er32(MPTC);
39699673d9bSBruce Allan 	er32(BPTC);
397bc7f75faSAuke Kok }
398bc7f75faSAuke Kok 
399bc7f75faSAuke Kok /**
400bc7f75faSAuke Kok  *  e1000e_check_for_copper_link - Check for link (Copper)
401bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
402bc7f75faSAuke Kok  *
403bc7f75faSAuke Kok  *  Checks to see of the link status of the hardware has changed.  If a
404bc7f75faSAuke Kok  *  change in link status has been detected, then we read the PHY registers
405bc7f75faSAuke Kok  *  to get the current speed/duplex if link exists.
406bc7f75faSAuke Kok  **/
e1000e_check_for_copper_link(struct e1000_hw * hw)407bc7f75faSAuke Kok s32 e1000e_check_for_copper_link(struct e1000_hw *hw)
408bc7f75faSAuke Kok {
409bc7f75faSAuke Kok 	struct e1000_mac_info *mac = &hw->mac;
410bc7f75faSAuke Kok 	s32 ret_val;
411bc7f75faSAuke Kok 	bool link;
412bc7f75faSAuke Kok 
413e921eb1aSBruce Allan 	/* We only want to go out to the PHY registers to see if Auto-Neg
414bc7f75faSAuke Kok 	 * has completed and/or if our link status has changed.  The
415bc7f75faSAuke Kok 	 * get_link_status flag is set upon receiving a Link Status
416bc7f75faSAuke Kok 	 * Change or Rx Sequence Error interrupt.
417bc7f75faSAuke Kok 	 */
418bc7f75faSAuke Kok 	if (!mac->get_link_status)
4193016e0a0SBenjamin Poirier 		return 0;
420e2710dbfSBenjamin Poirier 	mac->get_link_status = false;
421bc7f75faSAuke Kok 
422e921eb1aSBruce Allan 	/* First we want to see if the MII Status Register reports
423bc7f75faSAuke Kok 	 * link.  If so, then we want to get the current speed/duplex
424bc7f75faSAuke Kok 	 * of the PHY.
425bc7f75faSAuke Kok 	 */
426bc7f75faSAuke Kok 	ret_val = e1000e_phy_has_link_generic(hw, 1, 0, &link);
427e2710dbfSBenjamin Poirier 	if (ret_val || !link)
428e2710dbfSBenjamin Poirier 		goto out;
429bc7f75faSAuke Kok 
430e921eb1aSBruce Allan 	/* Check if there was DownShift, must be checked
431ad68076eSBruce Allan 	 * immediately after link-up
432ad68076eSBruce Allan 	 */
433bc7f75faSAuke Kok 	e1000e_check_downshift(hw);
434bc7f75faSAuke Kok 
435e921eb1aSBruce Allan 	/* If we are forcing speed/duplex, then we simply return since
436bc7f75faSAuke Kok 	 * we have already determined whether we have link or not.
437bc7f75faSAuke Kok 	 */
4387eb61d81SBruce Allan 	if (!mac->autoneg)
4393016e0a0SBenjamin Poirier 		return -E1000_ERR_CONFIG;
440bc7f75faSAuke Kok 
441e921eb1aSBruce Allan 	/* Auto-Neg is enabled.  Auto Speed Detection takes care
442bc7f75faSAuke Kok 	 * of MAC speed/duplex configuration.  So we only need to
443bc7f75faSAuke Kok 	 * configure Collision Distance in the MAC.
444bc7f75faSAuke Kok 	 */
44557cde763SBruce Allan 	mac->ops.config_collision_dist(hw);
446bc7f75faSAuke Kok 
447e921eb1aSBruce Allan 	/* Configure Flow Control now that Auto-Neg has completed.
448bc7f75faSAuke Kok 	 * First, we need to restore the desired flow control
449bc7f75faSAuke Kok 	 * settings because we may have had to re-autoneg with a
450bc7f75faSAuke Kok 	 * different link partner.
451bc7f75faSAuke Kok 	 */
452bc7f75faSAuke Kok 	ret_val = e1000e_config_fc_after_link_up(hw);
4533016e0a0SBenjamin Poirier 	if (ret_val)
4543bb99fe2SBruce Allan 		e_dbg("Error configuring flow control\n");
455bc7f75faSAuke Kok 
4563016e0a0SBenjamin Poirier 	return ret_val;
457e2710dbfSBenjamin Poirier 
458e2710dbfSBenjamin Poirier out:
459e2710dbfSBenjamin Poirier 	mac->get_link_status = true;
460e2710dbfSBenjamin Poirier 	return ret_val;
46119110cfbSBenjamin Poirier }
46219110cfbSBenjamin Poirier 
463bc7f75faSAuke Kok /**
464bc7f75faSAuke Kok  *  e1000e_check_for_fiber_link - Check for link (Fiber)
465bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
466bc7f75faSAuke Kok  *
467bc7f75faSAuke Kok  *  Checks for link up on the hardware.  If link is not up and we have
468bc7f75faSAuke Kok  *  a signal, then we need to force link up.
469bc7f75faSAuke Kok  **/
e1000e_check_for_fiber_link(struct e1000_hw * hw)470bc7f75faSAuke Kok s32 e1000e_check_for_fiber_link(struct e1000_hw *hw)
471bc7f75faSAuke Kok {
472bc7f75faSAuke Kok 	struct e1000_mac_info *mac = &hw->mac;
473bc7f75faSAuke Kok 	u32 rxcw;
474bc7f75faSAuke Kok 	u32 ctrl;
475bc7f75faSAuke Kok 	u32 status;
476bc7f75faSAuke Kok 	s32 ret_val;
477bc7f75faSAuke Kok 
478bc7f75faSAuke Kok 	ctrl = er32(CTRL);
479bc7f75faSAuke Kok 	status = er32(STATUS);
480bc7f75faSAuke Kok 	rxcw = er32(RXCW);
481bc7f75faSAuke Kok 
482e921eb1aSBruce Allan 	/* If we don't have link (auto-negotiation failed or link partner
483bc7f75faSAuke Kok 	 * cannot auto-negotiate), the cable is plugged in (we have signal),
484bc7f75faSAuke Kok 	 * and our link partner is not trying to auto-negotiate with us (we
485bc7f75faSAuke Kok 	 * are receiving idles or data), we need to force link up. We also
486bc7f75faSAuke Kok 	 * need to give auto-negotiation time to complete, in case the cable
487bc7f75faSAuke Kok 	 * was just plugged in. The autoneg_failed flag does this.
488bc7f75faSAuke Kok 	 */
489bc7f75faSAuke Kok 	/* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
490668018d7SBruce Allan 	if ((ctrl & E1000_CTRL_SWDPIN1) && !(status & E1000_STATUS_LU) &&
491668018d7SBruce Allan 	    !(rxcw & E1000_RXCW_C)) {
49207914ee3SBruce Allan 		if (!mac->autoneg_failed) {
49307914ee3SBruce Allan 			mac->autoneg_failed = true;
494bc7f75faSAuke Kok 			return 0;
495bc7f75faSAuke Kok 		}
496af667a29SBruce Allan 		e_dbg("NOT Rx'ing /C/, disable AutoNeg and force link.\n");
497bc7f75faSAuke Kok 
498bc7f75faSAuke Kok 		/* Disable auto-negotiation in the TXCW register */
499bc7f75faSAuke Kok 		ew32(TXCW, (mac->txcw & ~E1000_TXCW_ANE));
500bc7f75faSAuke Kok 
501bc7f75faSAuke Kok 		/* Force link-up and also force full-duplex. */
502bc7f75faSAuke Kok 		ctrl = er32(CTRL);
503bc7f75faSAuke Kok 		ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
504bc7f75faSAuke Kok 		ew32(CTRL, ctrl);
505bc7f75faSAuke Kok 
506bc7f75faSAuke Kok 		/* Configure Flow Control after forcing link up. */
507bc7f75faSAuke Kok 		ret_val = e1000e_config_fc_after_link_up(hw);
508bc7f75faSAuke Kok 		if (ret_val) {
5093bb99fe2SBruce Allan 			e_dbg("Error configuring flow control\n");
510bc7f75faSAuke Kok 			return ret_val;
511bc7f75faSAuke Kok 		}
512bc7f75faSAuke Kok 	} else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
513e921eb1aSBruce Allan 		/* If we are forcing link and we are receiving /C/ ordered
514bc7f75faSAuke Kok 		 * sets, re-enable auto-negotiation in the TXCW register
515bc7f75faSAuke Kok 		 * and disable forced link in the Device Control register
516bc7f75faSAuke Kok 		 * in an attempt to auto-negotiate with our link partner.
517bc7f75faSAuke Kok 		 */
518af667a29SBruce Allan 		e_dbg("Rx'ing /C/, enable AutoNeg and stop forcing link.\n");
519bc7f75faSAuke Kok 		ew32(TXCW, mac->txcw);
520bc7f75faSAuke Kok 		ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
521bc7f75faSAuke Kok 
522612e244cSAlex Chiang 		mac->serdes_has_link = true;
523bc7f75faSAuke Kok 	}
524bc7f75faSAuke Kok 
525bc7f75faSAuke Kok 	return 0;
526bc7f75faSAuke Kok }
527bc7f75faSAuke Kok 
528bc7f75faSAuke Kok /**
529bc7f75faSAuke Kok  *  e1000e_check_for_serdes_link - Check for link (Serdes)
530bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
531bc7f75faSAuke Kok  *
532bc7f75faSAuke Kok  *  Checks for link up on the hardware.  If link is not up and we have
533bc7f75faSAuke Kok  *  a signal, then we need to force link up.
534bc7f75faSAuke Kok  **/
e1000e_check_for_serdes_link(struct e1000_hw * hw)535bc7f75faSAuke Kok s32 e1000e_check_for_serdes_link(struct e1000_hw *hw)
536bc7f75faSAuke Kok {
537bc7f75faSAuke Kok 	struct e1000_mac_info *mac = &hw->mac;
538bc7f75faSAuke Kok 	u32 rxcw;
539bc7f75faSAuke Kok 	u32 ctrl;
540bc7f75faSAuke Kok 	u32 status;
541bc7f75faSAuke Kok 	s32 ret_val;
542bc7f75faSAuke Kok 
543bc7f75faSAuke Kok 	ctrl = er32(CTRL);
544bc7f75faSAuke Kok 	status = er32(STATUS);
545bc7f75faSAuke Kok 	rxcw = er32(RXCW);
546bc7f75faSAuke Kok 
547e921eb1aSBruce Allan 	/* If we don't have link (auto-negotiation failed or link partner
548bc7f75faSAuke Kok 	 * cannot auto-negotiate), and our link partner is not trying to
549bc7f75faSAuke Kok 	 * auto-negotiate with us (we are receiving idles or data),
550bc7f75faSAuke Kok 	 * we need to force link up. We also need to give auto-negotiation
551bc7f75faSAuke Kok 	 * time to complete.
552bc7f75faSAuke Kok 	 */
553bc7f75faSAuke Kok 	/* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
554668018d7SBruce Allan 	if (!(status & E1000_STATUS_LU) && !(rxcw & E1000_RXCW_C)) {
55507914ee3SBruce Allan 		if (!mac->autoneg_failed) {
55607914ee3SBruce Allan 			mac->autoneg_failed = true;
557bc7f75faSAuke Kok 			return 0;
558bc7f75faSAuke Kok 		}
559af667a29SBruce Allan 		e_dbg("NOT Rx'ing /C/, disable AutoNeg and force link.\n");
560bc7f75faSAuke Kok 
561bc7f75faSAuke Kok 		/* Disable auto-negotiation in the TXCW register */
562bc7f75faSAuke Kok 		ew32(TXCW, (mac->txcw & ~E1000_TXCW_ANE));
563bc7f75faSAuke Kok 
564bc7f75faSAuke Kok 		/* Force link-up and also force full-duplex. */
565bc7f75faSAuke Kok 		ctrl = er32(CTRL);
566bc7f75faSAuke Kok 		ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
567bc7f75faSAuke Kok 		ew32(CTRL, ctrl);
568bc7f75faSAuke Kok 
569bc7f75faSAuke Kok 		/* Configure Flow Control after forcing link up. */
570bc7f75faSAuke Kok 		ret_val = e1000e_config_fc_after_link_up(hw);
571bc7f75faSAuke Kok 		if (ret_val) {
5723bb99fe2SBruce Allan 			e_dbg("Error configuring flow control\n");
573bc7f75faSAuke Kok 			return ret_val;
574bc7f75faSAuke Kok 		}
575bc7f75faSAuke Kok 	} else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
576e921eb1aSBruce Allan 		/* If we are forcing link and we are receiving /C/ ordered
577bc7f75faSAuke Kok 		 * sets, re-enable auto-negotiation in the TXCW register
578bc7f75faSAuke Kok 		 * and disable forced link in the Device Control register
579bc7f75faSAuke Kok 		 * in an attempt to auto-negotiate with our link partner.
580bc7f75faSAuke Kok 		 */
581af667a29SBruce Allan 		e_dbg("Rx'ing /C/, enable AutoNeg and stop forcing link.\n");
582bc7f75faSAuke Kok 		ew32(TXCW, mac->txcw);
583bc7f75faSAuke Kok 		ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
584bc7f75faSAuke Kok 
585612e244cSAlex Chiang 		mac->serdes_has_link = true;
586bc7f75faSAuke Kok 	} else if (!(E1000_TXCW_ANE & er32(TXCW))) {
587e921eb1aSBruce Allan 		/* If we force link for non-auto-negotiation switch, check
588bc7f75faSAuke Kok 		 * link status based on MAC synchronization for internal
589bc7f75faSAuke Kok 		 * serdes media type.
590bc7f75faSAuke Kok 		 */
591bc7f75faSAuke Kok 		/* SYNCH bit and IV bit are sticky. */
592ce43a216SBruce Allan 		usleep_range(10, 20);
59363dcf3d3SBruce Allan 		rxcw = er32(RXCW);
59463dcf3d3SBruce Allan 		if (rxcw & E1000_RXCW_SYNCH) {
595bc7f75faSAuke Kok 			if (!(rxcw & E1000_RXCW_IV)) {
59663dcf3d3SBruce Allan 				mac->serdes_has_link = true;
5973bb99fe2SBruce Allan 				e_dbg("SERDES: Link up - forced.\n");
598bc7f75faSAuke Kok 			}
599bc7f75faSAuke Kok 		} else {
60063dcf3d3SBruce Allan 			mac->serdes_has_link = false;
6013bb99fe2SBruce Allan 			e_dbg("SERDES: Link down - force failed.\n");
602bc7f75faSAuke Kok 		}
603bc7f75faSAuke Kok 	}
604bc7f75faSAuke Kok 
605bc7f75faSAuke Kok 	if (E1000_TXCW_ANE & er32(TXCW)) {
606bc7f75faSAuke Kok 		status = er32(STATUS);
60763dcf3d3SBruce Allan 		if (status & E1000_STATUS_LU) {
60863dcf3d3SBruce Allan 			/* SYNCH bit and IV bit are sticky, so reread rxcw. */
609ce43a216SBruce Allan 			usleep_range(10, 20);
61063dcf3d3SBruce Allan 			rxcw = er32(RXCW);
61163dcf3d3SBruce Allan 			if (rxcw & E1000_RXCW_SYNCH) {
61263dcf3d3SBruce Allan 				if (!(rxcw & E1000_RXCW_IV)) {
61363dcf3d3SBruce Allan 					mac->serdes_has_link = true;
614434f1392SBruce Allan 					e_dbg("SERDES: Link up - autoneg completed successfully.\n");
61563dcf3d3SBruce Allan 				} else {
61663dcf3d3SBruce Allan 					mac->serdes_has_link = false;
617434f1392SBruce Allan 					e_dbg("SERDES: Link down - invalid codewords detected in autoneg.\n");
61863dcf3d3SBruce Allan 				}
61963dcf3d3SBruce Allan 			} else {
62063dcf3d3SBruce Allan 				mac->serdes_has_link = false;
6213bb99fe2SBruce Allan 				e_dbg("SERDES: Link down - no sync.\n");
62263dcf3d3SBruce Allan 			}
62363dcf3d3SBruce Allan 		} else {
62463dcf3d3SBruce Allan 			mac->serdes_has_link = false;
6253bb99fe2SBruce Allan 			e_dbg("SERDES: Link down - autoneg failed\n");
62663dcf3d3SBruce Allan 		}
627bc7f75faSAuke Kok 	}
628bc7f75faSAuke Kok 
629bc7f75faSAuke Kok 	return 0;
630bc7f75faSAuke Kok }
631bc7f75faSAuke Kok 
632bc7f75faSAuke Kok /**
633bc7f75faSAuke Kok  *  e1000_set_default_fc_generic - Set flow control default values
634bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
635bc7f75faSAuke Kok  *
636bc7f75faSAuke Kok  *  Read the EEPROM for the default values for flow control and store the
637bc7f75faSAuke Kok  *  values.
638bc7f75faSAuke Kok  **/
e1000_set_default_fc_generic(struct e1000_hw * hw)639bc7f75faSAuke Kok static s32 e1000_set_default_fc_generic(struct e1000_hw *hw)
640bc7f75faSAuke Kok {
641bc7f75faSAuke Kok 	s32 ret_val;
642bc7f75faSAuke Kok 	u16 nvm_data;
643bc7f75faSAuke Kok 
644e921eb1aSBruce Allan 	/* Read and store word 0x0F of the EEPROM. This word contains bits
645bc7f75faSAuke Kok 	 * that determine the hardware's default PAUSE (flow control) mode,
646bc7f75faSAuke Kok 	 * a bit that determines whether the HW defaults to enabling or
647bc7f75faSAuke Kok 	 * disabling auto-negotiation, and the direction of the
648bc7f75faSAuke Kok 	 * SW defined pins. If there is no SW over-ride of the flow
649bc7f75faSAuke Kok 	 * control setting, then the variable hw->fc will
650bc7f75faSAuke Kok 	 * be initialized based on a value in the EEPROM.
651bc7f75faSAuke Kok 	 */
652bc7f75faSAuke Kok 	ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &nvm_data);
653bc7f75faSAuke Kok 
654bc7f75faSAuke Kok 	if (ret_val) {
6553bb99fe2SBruce Allan 		e_dbg("NVM Read Error\n");
656bc7f75faSAuke Kok 		return ret_val;
657bc7f75faSAuke Kok 	}
658bc7f75faSAuke Kok 
65904499ec4SBruce Allan 	if (!(nvm_data & NVM_WORD0F_PAUSE_MASK))
6605c48ef3eSBruce Allan 		hw->fc.requested_mode = e1000_fc_none;
661fe2ddfb5SBruce Allan 	else if ((nvm_data & NVM_WORD0F_PAUSE_MASK) == NVM_WORD0F_ASM_DIR)
6625c48ef3eSBruce Allan 		hw->fc.requested_mode = e1000_fc_tx_pause;
663bc7f75faSAuke Kok 	else
6645c48ef3eSBruce Allan 		hw->fc.requested_mode = e1000_fc_full;
665bc7f75faSAuke Kok 
666bc7f75faSAuke Kok 	return 0;
667bc7f75faSAuke Kok }
668bc7f75faSAuke Kok 
669bc7f75faSAuke Kok /**
6701a46b40fSBruce Allan  *  e1000e_setup_link_generic - Setup flow control and link settings
671bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
672bc7f75faSAuke Kok  *
673bc7f75faSAuke Kok  *  Determines which flow control settings to use, then configures flow
674bc7f75faSAuke Kok  *  control.  Calls the appropriate media-specific link configuration
675bc7f75faSAuke Kok  *  function.  Assuming the adapter has a valid link partner, a valid link
676bc7f75faSAuke Kok  *  should be established.  Assumes the hardware has previously been reset
677bc7f75faSAuke Kok  *  and the transmitter and receiver are not enabled.
678bc7f75faSAuke Kok  **/
e1000e_setup_link_generic(struct e1000_hw * hw)6791a46b40fSBruce Allan s32 e1000e_setup_link_generic(struct e1000_hw *hw)
680bc7f75faSAuke Kok {
681bc7f75faSAuke Kok 	s32 ret_val;
682bc7f75faSAuke Kok 
683e921eb1aSBruce Allan 	/* In the case of the phy reset being blocked, we already have a link.
684bc7f75faSAuke Kok 	 * We do not need to set it up again.
685bc7f75faSAuke Kok 	 */
686470a5420SBruce Allan 	if (hw->phy.ops.check_reset_block && hw->phy.ops.check_reset_block(hw))
687bc7f75faSAuke Kok 		return 0;
688bc7f75faSAuke Kok 
689e921eb1aSBruce Allan 	/* If requested flow control is set to default, set flow control
6905c48ef3eSBruce Allan 	 * based on the EEPROM flow control settings.
691309af40bSAuke Kok 	 */
6925c48ef3eSBruce Allan 	if (hw->fc.requested_mode == e1000_fc_default) {
693bc7f75faSAuke Kok 		ret_val = e1000_set_default_fc_generic(hw);
694bc7f75faSAuke Kok 		if (ret_val)
695bc7f75faSAuke Kok 			return ret_val;
696309af40bSAuke Kok 	}
697bc7f75faSAuke Kok 
698e921eb1aSBruce Allan 	/* Save off the requested flow control mode for use later.  Depending
6995c48ef3eSBruce Allan 	 * on the link partner's capabilities, we may or may not use this mode.
700bc7f75faSAuke Kok 	 */
7015c48ef3eSBruce Allan 	hw->fc.current_mode = hw->fc.requested_mode;
702bc7f75faSAuke Kok 
703fe2ddfb5SBruce Allan 	e_dbg("After fix-ups FlowControl is now = %x\n", hw->fc.current_mode);
704bc7f75faSAuke Kok 
705bc7f75faSAuke Kok 	/* Call the necessary media_type subroutine to configure the link. */
7060d37678eSBruce Allan 	ret_val = hw->mac.ops.setup_physical_interface(hw);
707bc7f75faSAuke Kok 	if (ret_val)
708bc7f75faSAuke Kok 		return ret_val;
709bc7f75faSAuke Kok 
710e921eb1aSBruce Allan 	/* Initialize the flow control address, type, and PAUSE timer
711bc7f75faSAuke Kok 	 * registers to their default values.  This is done even if flow
712bc7f75faSAuke Kok 	 * control is disabled, because it does not hurt anything to
713bc7f75faSAuke Kok 	 * initialize these registers.
714bc7f75faSAuke Kok 	 */
7153bb99fe2SBruce Allan 	e_dbg("Initializing the Flow Control address, type and timer regs\n");
716bc7f75faSAuke Kok 	ew32(FCT, FLOW_CONTROL_TYPE);
717bc7f75faSAuke Kok 	ew32(FCAH, FLOW_CONTROL_ADDRESS_HIGH);
718bc7f75faSAuke Kok 	ew32(FCAL, FLOW_CONTROL_ADDRESS_LOW);
719bc7f75faSAuke Kok 
720318a94d6SJeff Kirsher 	ew32(FCTTV, hw->fc.pause_time);
721bc7f75faSAuke Kok 
722bc7f75faSAuke Kok 	return e1000e_set_fc_watermarks(hw);
723bc7f75faSAuke Kok }
724bc7f75faSAuke Kok 
725bc7f75faSAuke Kok /**
726bc7f75faSAuke Kok  *  e1000_commit_fc_settings_generic - Configure flow control
727bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
728bc7f75faSAuke Kok  *
729bc7f75faSAuke Kok  *  Write the flow control settings to the Transmit Config Word Register (TXCW)
730bc7f75faSAuke Kok  *  base on the flow control settings in e1000_mac_info.
731bc7f75faSAuke Kok  **/
e1000_commit_fc_settings_generic(struct e1000_hw * hw)732bc7f75faSAuke Kok static s32 e1000_commit_fc_settings_generic(struct e1000_hw *hw)
733bc7f75faSAuke Kok {
734bc7f75faSAuke Kok 	struct e1000_mac_info *mac = &hw->mac;
735bc7f75faSAuke Kok 	u32 txcw;
736bc7f75faSAuke Kok 
737e921eb1aSBruce Allan 	/* Check for a software override of the flow control settings, and
738bc7f75faSAuke Kok 	 * setup the device accordingly.  If auto-negotiation is enabled, then
739bc7f75faSAuke Kok 	 * software will have to set the "PAUSE" bits to the correct value in
740bc7f75faSAuke Kok 	 * the Transmit Config Word Register (TXCW) and re-start auto-
741bc7f75faSAuke Kok 	 * negotiation.  However, if auto-negotiation is disabled, then
742bc7f75faSAuke Kok 	 * software will have to manually configure the two flow control enable
743bc7f75faSAuke Kok 	 * bits in the CTRL register.
744bc7f75faSAuke Kok 	 *
745bc7f75faSAuke Kok 	 * The possible values of the "fc" parameter are:
746bc7f75faSAuke Kok 	 *      0:  Flow control is completely disabled
747bc7f75faSAuke Kok 	 *      1:  Rx flow control is enabled (we can receive pause frames,
748bc7f75faSAuke Kok 	 *          but not send pause frames).
749bc7f75faSAuke Kok 	 *      2:  Tx flow control is enabled (we can send pause frames but we
750bc7f75faSAuke Kok 	 *          do not support receiving pause frames).
751ad68076eSBruce Allan 	 *      3:  Both Rx and Tx flow control (symmetric) are enabled.
752bc7f75faSAuke Kok 	 */
7535c48ef3eSBruce Allan 	switch (hw->fc.current_mode) {
754bc7f75faSAuke Kok 	case e1000_fc_none:
755bc7f75faSAuke Kok 		/* Flow control completely disabled by a software over-ride. */
756bc7f75faSAuke Kok 		txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
757bc7f75faSAuke Kok 		break;
758bc7f75faSAuke Kok 	case e1000_fc_rx_pause:
759e921eb1aSBruce Allan 		/* Rx Flow control is enabled and Tx Flow control is disabled
760bc7f75faSAuke Kok 		 * by a software over-ride. Since there really isn't a way to
761ad68076eSBruce Allan 		 * advertise that we are capable of Rx Pause ONLY, we will
762ad68076eSBruce Allan 		 * advertise that we support both symmetric and asymmetric Rx
763bc7f75faSAuke Kok 		 * PAUSE.  Later, we will disable the adapter's ability to send
764bc7f75faSAuke Kok 		 * PAUSE frames.
765bc7f75faSAuke Kok 		 */
766bc7f75faSAuke Kok 		txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
767bc7f75faSAuke Kok 		break;
768bc7f75faSAuke Kok 	case e1000_fc_tx_pause:
769e921eb1aSBruce Allan 		/* Tx Flow control is enabled, and Rx Flow control is disabled,
770bc7f75faSAuke Kok 		 * by a software over-ride.
771bc7f75faSAuke Kok 		 */
772bc7f75faSAuke Kok 		txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
773bc7f75faSAuke Kok 		break;
774bc7f75faSAuke Kok 	case e1000_fc_full:
775e921eb1aSBruce Allan 		/* Flow control (both Rx and Tx) is enabled by a software
776bc7f75faSAuke Kok 		 * over-ride.
777bc7f75faSAuke Kok 		 */
778bc7f75faSAuke Kok 		txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
779bc7f75faSAuke Kok 		break;
780bc7f75faSAuke Kok 	default:
7813bb99fe2SBruce Allan 		e_dbg("Flow control param set incorrectly\n");
782bc7f75faSAuke Kok 		return -E1000_ERR_CONFIG;
783bc7f75faSAuke Kok 	}
784bc7f75faSAuke Kok 
785bc7f75faSAuke Kok 	ew32(TXCW, txcw);
786bc7f75faSAuke Kok 	mac->txcw = txcw;
787bc7f75faSAuke Kok 
788bc7f75faSAuke Kok 	return 0;
789bc7f75faSAuke Kok }
790bc7f75faSAuke Kok 
791bc7f75faSAuke Kok /**
792bc7f75faSAuke Kok  *  e1000_poll_fiber_serdes_link_generic - Poll for link up
793bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
794bc7f75faSAuke Kok  *
795bc7f75faSAuke Kok  *  Polls for link up by reading the status register, if link fails to come
796bc7f75faSAuke Kok  *  up with auto-negotiation, then the link is forced if a signal is detected.
797bc7f75faSAuke Kok  **/
e1000_poll_fiber_serdes_link_generic(struct e1000_hw * hw)798bc7f75faSAuke Kok static s32 e1000_poll_fiber_serdes_link_generic(struct e1000_hw *hw)
799bc7f75faSAuke Kok {
800bc7f75faSAuke Kok 	struct e1000_mac_info *mac = &hw->mac;
801bc7f75faSAuke Kok 	u32 i, status;
802bc7f75faSAuke Kok 	s32 ret_val;
803bc7f75faSAuke Kok 
804e921eb1aSBruce Allan 	/* If we have a signal (the cable is plugged in, or assumed true for
805bc7f75faSAuke Kok 	 * serdes media) then poll for a "Link-Up" indication in the Device
806bc7f75faSAuke Kok 	 * Status Register.  Time-out if a link isn't seen in 500 milliseconds
807bc7f75faSAuke Kok 	 * seconds (Auto-negotiation should complete in less than 500
808bc7f75faSAuke Kok 	 * milliseconds even if the other end is doing it in SW).
809bc7f75faSAuke Kok 	 */
810bc7f75faSAuke Kok 	for (i = 0; i < FIBER_LINK_UP_LIMIT; i++) {
811ab6973aeSArjan van de Ven 		usleep_range(10000, 11000);
812bc7f75faSAuke Kok 		status = er32(STATUS);
813bc7f75faSAuke Kok 		if (status & E1000_STATUS_LU)
814bc7f75faSAuke Kok 			break;
815bc7f75faSAuke Kok 	}
816bc7f75faSAuke Kok 	if (i == FIBER_LINK_UP_LIMIT) {
8173bb99fe2SBruce Allan 		e_dbg("Never got a valid link from auto-neg!!!\n");
81807914ee3SBruce Allan 		mac->autoneg_failed = true;
819e921eb1aSBruce Allan 		/* AutoNeg failed to achieve a link, so we'll call
820bc7f75faSAuke Kok 		 * mac->check_for_link. This routine will force the
821bc7f75faSAuke Kok 		 * link up if we detect a signal. This will allow us to
822bc7f75faSAuke Kok 		 * communicate with non-autonegotiating link partners.
823bc7f75faSAuke Kok 		 */
824bc7f75faSAuke Kok 		ret_val = mac->ops.check_for_link(hw);
825bc7f75faSAuke Kok 		if (ret_val) {
8263bb99fe2SBruce Allan 			e_dbg("Error while checking for link\n");
827bc7f75faSAuke Kok 			return ret_val;
828bc7f75faSAuke Kok 		}
82907914ee3SBruce Allan 		mac->autoneg_failed = false;
830bc7f75faSAuke Kok 	} else {
83107914ee3SBruce Allan 		mac->autoneg_failed = false;
8323bb99fe2SBruce Allan 		e_dbg("Valid Link Found\n");
833bc7f75faSAuke Kok 	}
834bc7f75faSAuke Kok 
835bc7f75faSAuke Kok 	return 0;
836bc7f75faSAuke Kok }
837bc7f75faSAuke Kok 
838bc7f75faSAuke Kok /**
839bc7f75faSAuke Kok  *  e1000e_setup_fiber_serdes_link - Setup link for fiber/serdes
840bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
841bc7f75faSAuke Kok  *
842bc7f75faSAuke Kok  *  Configures collision distance and flow control for fiber and serdes
843bc7f75faSAuke Kok  *  links.  Upon successful setup, poll for link.
844bc7f75faSAuke Kok  **/
e1000e_setup_fiber_serdes_link(struct e1000_hw * hw)845bc7f75faSAuke Kok s32 e1000e_setup_fiber_serdes_link(struct e1000_hw *hw)
846bc7f75faSAuke Kok {
847bc7f75faSAuke Kok 	u32 ctrl;
848bc7f75faSAuke Kok 	s32 ret_val;
849bc7f75faSAuke Kok 
850bc7f75faSAuke Kok 	ctrl = er32(CTRL);
851bc7f75faSAuke Kok 
852bc7f75faSAuke Kok 	/* Take the link out of reset */
853bc7f75faSAuke Kok 	ctrl &= ~E1000_CTRL_LRST;
854bc7f75faSAuke Kok 
85557cde763SBruce Allan 	hw->mac.ops.config_collision_dist(hw);
856bc7f75faSAuke Kok 
857bc7f75faSAuke Kok 	ret_val = e1000_commit_fc_settings_generic(hw);
858bc7f75faSAuke Kok 	if (ret_val)
859bc7f75faSAuke Kok 		return ret_val;
860bc7f75faSAuke Kok 
861e921eb1aSBruce Allan 	/* Since auto-negotiation is enabled, take the link out of reset (the
862bc7f75faSAuke Kok 	 * link will be in reset, because we previously reset the chip). This
863bc7f75faSAuke Kok 	 * will restart auto-negotiation.  If auto-negotiation is successful
864bc7f75faSAuke Kok 	 * then the link-up status bit will be set and the flow control enable
865bc7f75faSAuke Kok 	 * bits (RFCE and TFCE) will be set according to their negotiated value.
866bc7f75faSAuke Kok 	 */
8673bb99fe2SBruce Allan 	e_dbg("Auto-negotiation enabled\n");
868bc7f75faSAuke Kok 
869bc7f75faSAuke Kok 	ew32(CTRL, ctrl);
870bc7f75faSAuke Kok 	e1e_flush();
8711bba4386SBruce Allan 	usleep_range(1000, 2000);
872bc7f75faSAuke Kok 
873e921eb1aSBruce Allan 	/* For these adapters, the SW definable pin 1 is set when the optics
874bc7f75faSAuke Kok 	 * detect a signal.  If we have a signal, then poll for a "Link-Up"
875bc7f75faSAuke Kok 	 * indication.
876bc7f75faSAuke Kok 	 */
877318a94d6SJeff Kirsher 	if (hw->phy.media_type == e1000_media_type_internal_serdes ||
878bc7f75faSAuke Kok 	    (er32(CTRL) & E1000_CTRL_SWDPIN1)) {
879bc7f75faSAuke Kok 		ret_val = e1000_poll_fiber_serdes_link_generic(hw);
880bc7f75faSAuke Kok 	} else {
8813bb99fe2SBruce Allan 		e_dbg("No signal detected\n");
882bc7f75faSAuke Kok 	}
883bc7f75faSAuke Kok 
8842a31b37aSBruce Allan 	return ret_val;
885bc7f75faSAuke Kok }
886bc7f75faSAuke Kok 
887bc7f75faSAuke Kok /**
88857cde763SBruce Allan  *  e1000e_config_collision_dist_generic - Configure collision distance
889bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
890bc7f75faSAuke Kok  *
891bc7f75faSAuke Kok  *  Configures the collision distance to the default value and is used
89257cde763SBruce Allan  *  during link setup.
893bc7f75faSAuke Kok  **/
e1000e_config_collision_dist_generic(struct e1000_hw * hw)89457cde763SBruce Allan void e1000e_config_collision_dist_generic(struct e1000_hw *hw)
895bc7f75faSAuke Kok {
896bc7f75faSAuke Kok 	u32 tctl;
897bc7f75faSAuke Kok 
898bc7f75faSAuke Kok 	tctl = er32(TCTL);
899bc7f75faSAuke Kok 
900bc7f75faSAuke Kok 	tctl &= ~E1000_TCTL_COLD;
901bc7f75faSAuke Kok 	tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
902bc7f75faSAuke Kok 
903bc7f75faSAuke Kok 	ew32(TCTL, tctl);
904bc7f75faSAuke Kok 	e1e_flush();
905bc7f75faSAuke Kok }
906bc7f75faSAuke Kok 
907bc7f75faSAuke Kok /**
908bc7f75faSAuke Kok  *  e1000e_set_fc_watermarks - Set flow control high/low watermarks
909bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
910bc7f75faSAuke Kok  *
911bc7f75faSAuke Kok  *  Sets the flow control high/low threshold (watermark) registers.  If
912bc7f75faSAuke Kok  *  flow control XON frame transmission is enabled, then set XON frame
913ad68076eSBruce Allan  *  transmission as well.
914bc7f75faSAuke Kok  **/
e1000e_set_fc_watermarks(struct e1000_hw * hw)915bc7f75faSAuke Kok s32 e1000e_set_fc_watermarks(struct e1000_hw *hw)
916bc7f75faSAuke Kok {
917bc7f75faSAuke Kok 	u32 fcrtl = 0, fcrth = 0;
918bc7f75faSAuke Kok 
919e921eb1aSBruce Allan 	/* Set the flow control receive threshold registers.  Normally,
920bc7f75faSAuke Kok 	 * these registers will be set to a default threshold that may be
921bc7f75faSAuke Kok 	 * adjusted later by the driver's runtime code.  However, if the
922bc7f75faSAuke Kok 	 * ability to transmit pause frames is not enabled, then these
923bc7f75faSAuke Kok 	 * registers will be set to 0.
924bc7f75faSAuke Kok 	 */
9255c48ef3eSBruce Allan 	if (hw->fc.current_mode & e1000_fc_tx_pause) {
926e921eb1aSBruce Allan 		/* We need to set up the Receive Threshold high and low water
927bc7f75faSAuke Kok 		 * marks as well as (optionally) enabling the transmission of
928bc7f75faSAuke Kok 		 * XON frames.
929bc7f75faSAuke Kok 		 */
930318a94d6SJeff Kirsher 		fcrtl = hw->fc.low_water;
931b20caa80SBruce Allan 		if (hw->fc.send_xon)
932bc7f75faSAuke Kok 			fcrtl |= E1000_FCRTL_XONE;
933b20caa80SBruce Allan 
934318a94d6SJeff Kirsher 		fcrth = hw->fc.high_water;
935bc7f75faSAuke Kok 	}
936bc7f75faSAuke Kok 	ew32(FCRTL, fcrtl);
937bc7f75faSAuke Kok 	ew32(FCRTH, fcrth);
938bc7f75faSAuke Kok 
939bc7f75faSAuke Kok 	return 0;
940bc7f75faSAuke Kok }
941bc7f75faSAuke Kok 
942bc7f75faSAuke Kok /**
943bc7f75faSAuke Kok  *  e1000e_force_mac_fc - Force the MAC's flow control settings
944bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
945bc7f75faSAuke Kok  *
946bc7f75faSAuke Kok  *  Force the MAC's flow control settings.  Sets the TFCE and RFCE bits in the
947bc7f75faSAuke Kok  *  device control register to reflect the adapter settings.  TFCE and RFCE
948bc7f75faSAuke Kok  *  need to be explicitly set by software when a copper PHY is used because
949bc7f75faSAuke Kok  *  autonegotiation is managed by the PHY rather than the MAC.  Software must
950bc7f75faSAuke Kok  *  also configure these bits when link is forced on a fiber connection.
951bc7f75faSAuke Kok  **/
e1000e_force_mac_fc(struct e1000_hw * hw)952bc7f75faSAuke Kok s32 e1000e_force_mac_fc(struct e1000_hw *hw)
953bc7f75faSAuke Kok {
954bc7f75faSAuke Kok 	u32 ctrl;
955bc7f75faSAuke Kok 
956bc7f75faSAuke Kok 	ctrl = er32(CTRL);
957bc7f75faSAuke Kok 
958e921eb1aSBruce Allan 	/* Because we didn't get link via the internal auto-negotiation
959bc7f75faSAuke Kok 	 * mechanism (we either forced link or we got link via PHY
960bc7f75faSAuke Kok 	 * auto-neg), we have to manually enable/disable transmit an
961bc7f75faSAuke Kok 	 * receive flow control.
962bc7f75faSAuke Kok 	 *
963bc7f75faSAuke Kok 	 * The "Case" statement below enables/disable flow control
9645c48ef3eSBruce Allan 	 * according to the "hw->fc.current_mode" parameter.
965bc7f75faSAuke Kok 	 *
966bc7f75faSAuke Kok 	 * The possible values of the "fc" parameter are:
967bc7f75faSAuke Kok 	 *      0:  Flow control is completely disabled
968bc7f75faSAuke Kok 	 *      1:  Rx flow control is enabled (we can receive pause
969bc7f75faSAuke Kok 	 *          frames but not send pause frames).
970bc7f75faSAuke Kok 	 *      2:  Tx flow control is enabled (we can send pause frames
971e2ef1c2dSJilin Yuan 	 *          but we do not receive pause frames).
972ad68076eSBruce Allan 	 *      3:  Both Rx and Tx flow control (symmetric) is enabled.
973bc7f75faSAuke Kok 	 *  other:  No other values should be possible at this point.
974bc7f75faSAuke Kok 	 */
9753bb99fe2SBruce Allan 	e_dbg("hw->fc.current_mode = %u\n", hw->fc.current_mode);
976bc7f75faSAuke Kok 
9775c48ef3eSBruce Allan 	switch (hw->fc.current_mode) {
978bc7f75faSAuke Kok 	case e1000_fc_none:
979bc7f75faSAuke Kok 		ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
980bc7f75faSAuke Kok 		break;
981bc7f75faSAuke Kok 	case e1000_fc_rx_pause:
982bc7f75faSAuke Kok 		ctrl &= (~E1000_CTRL_TFCE);
983bc7f75faSAuke Kok 		ctrl |= E1000_CTRL_RFCE;
984bc7f75faSAuke Kok 		break;
985bc7f75faSAuke Kok 	case e1000_fc_tx_pause:
986bc7f75faSAuke Kok 		ctrl &= (~E1000_CTRL_RFCE);
987bc7f75faSAuke Kok 		ctrl |= E1000_CTRL_TFCE;
988bc7f75faSAuke Kok 		break;
989bc7f75faSAuke Kok 	case e1000_fc_full:
990bc7f75faSAuke Kok 		ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
991bc7f75faSAuke Kok 		break;
992bc7f75faSAuke Kok 	default:
9933bb99fe2SBruce Allan 		e_dbg("Flow control param set incorrectly\n");
994bc7f75faSAuke Kok 		return -E1000_ERR_CONFIG;
995bc7f75faSAuke Kok 	}
996bc7f75faSAuke Kok 
997bc7f75faSAuke Kok 	ew32(CTRL, ctrl);
998bc7f75faSAuke Kok 
999bc7f75faSAuke Kok 	return 0;
1000bc7f75faSAuke Kok }
1001bc7f75faSAuke Kok 
1002bc7f75faSAuke Kok /**
1003bc7f75faSAuke Kok  *  e1000e_config_fc_after_link_up - Configures flow control after link
1004bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1005bc7f75faSAuke Kok  *
1006bc7f75faSAuke Kok  *  Checks the status of auto-negotiation after link up to ensure that the
1007bc7f75faSAuke Kok  *  speed and duplex were not forced.  If the link needed to be forced, then
1008bc7f75faSAuke Kok  *  flow control needs to be forced also.  If auto-negotiation is enabled
1009bc7f75faSAuke Kok  *  and did not fail, then we configure flow control based on our link
1010bc7f75faSAuke Kok  *  partner.
1011bc7f75faSAuke Kok  **/
e1000e_config_fc_after_link_up(struct e1000_hw * hw)1012bc7f75faSAuke Kok s32 e1000e_config_fc_after_link_up(struct e1000_hw *hw)
1013bc7f75faSAuke Kok {
1014bc7f75faSAuke Kok 	struct e1000_mac_info *mac = &hw->mac;
1015bc7f75faSAuke Kok 	s32 ret_val = 0;
10161241f29fSBruce Allan 	u32 pcs_status_reg, pcs_adv_reg, pcs_lp_ability_reg, pcs_ctrl_reg;
1017bc7f75faSAuke Kok 	u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
1018bc7f75faSAuke Kok 	u16 speed, duplex;
1019bc7f75faSAuke Kok 
1020e921eb1aSBruce Allan 	/* Check for the case where we have fiber media and auto-neg failed
1021bc7f75faSAuke Kok 	 * so we had to force link.  In this case, we need to force the
1022bc7f75faSAuke Kok 	 * configuration of the MAC to match the "fc" parameter.
1023bc7f75faSAuke Kok 	 */
1024bc7f75faSAuke Kok 	if (mac->autoneg_failed) {
1025318a94d6SJeff Kirsher 		if (hw->phy.media_type == e1000_media_type_fiber ||
1026318a94d6SJeff Kirsher 		    hw->phy.media_type == e1000_media_type_internal_serdes)
1027bc7f75faSAuke Kok 			ret_val = e1000e_force_mac_fc(hw);
1028bc7f75faSAuke Kok 	} else {
1029318a94d6SJeff Kirsher 		if (hw->phy.media_type == e1000_media_type_copper)
1030bc7f75faSAuke Kok 			ret_val = e1000e_force_mac_fc(hw);
1031bc7f75faSAuke Kok 	}
1032bc7f75faSAuke Kok 
1033bc7f75faSAuke Kok 	if (ret_val) {
10343bb99fe2SBruce Allan 		e_dbg("Error forcing flow control settings\n");
1035bc7f75faSAuke Kok 		return ret_val;
1036bc7f75faSAuke Kok 	}
1037bc7f75faSAuke Kok 
1038e921eb1aSBruce Allan 	/* Check for the case where we have copper media and auto-neg is
1039bc7f75faSAuke Kok 	 * enabled.  In this case, we need to check and see if Auto-Neg
1040bc7f75faSAuke Kok 	 * has completed, and if so, how the PHY and link partner has
1041bc7f75faSAuke Kok 	 * flow control configured.
1042bc7f75faSAuke Kok 	 */
1043318a94d6SJeff Kirsher 	if ((hw->phy.media_type == e1000_media_type_copper) && mac->autoneg) {
1044e921eb1aSBruce Allan 		/* Read the MII Status Register and check to see if AutoNeg
1045bc7f75faSAuke Kok 		 * has completed.  We read this twice because this reg has
1046bc7f75faSAuke Kok 		 * some "sticky" (latched) bits.
1047bc7f75faSAuke Kok 		 */
1048c2ade1a4SBruce Allan 		ret_val = e1e_rphy(hw, MII_BMSR, &mii_status_reg);
1049bc7f75faSAuke Kok 		if (ret_val)
1050bc7f75faSAuke Kok 			return ret_val;
1051c2ade1a4SBruce Allan 		ret_val = e1e_rphy(hw, MII_BMSR, &mii_status_reg);
1052bc7f75faSAuke Kok 		if (ret_val)
1053bc7f75faSAuke Kok 			return ret_val;
1054bc7f75faSAuke Kok 
1055c2ade1a4SBruce Allan 		if (!(mii_status_reg & BMSR_ANEGCOMPLETE)) {
1056434f1392SBruce Allan 			e_dbg("Copper PHY and Auto Neg has not completed.\n");
1057bc7f75faSAuke Kok 			return ret_val;
1058bc7f75faSAuke Kok 		}
1059bc7f75faSAuke Kok 
1060e921eb1aSBruce Allan 		/* The AutoNeg process has completed, so we now need to
1061bc7f75faSAuke Kok 		 * read both the Auto Negotiation Advertisement
1062bc7f75faSAuke Kok 		 * Register (Address 4) and the Auto_Negotiation Base
1063bc7f75faSAuke Kok 		 * Page Ability Register (Address 5) to determine how
1064bc7f75faSAuke Kok 		 * flow control was negotiated.
1065bc7f75faSAuke Kok 		 */
1066c2ade1a4SBruce Allan 		ret_val = e1e_rphy(hw, MII_ADVERTISE, &mii_nway_adv_reg);
1067bc7f75faSAuke Kok 		if (ret_val)
1068bc7f75faSAuke Kok 			return ret_val;
1069c2ade1a4SBruce Allan 		ret_val = e1e_rphy(hw, MII_LPA, &mii_nway_lp_ability_reg);
1070bc7f75faSAuke Kok 		if (ret_val)
1071bc7f75faSAuke Kok 			return ret_val;
1072bc7f75faSAuke Kok 
1073e921eb1aSBruce Allan 		/* Two bits in the Auto Negotiation Advertisement Register
1074bc7f75faSAuke Kok 		 * (Address 4) and two bits in the Auto Negotiation Base
1075bc7f75faSAuke Kok 		 * Page Ability Register (Address 5) determine flow control
1076bc7f75faSAuke Kok 		 * for both the PHY and the link partner.  The following
1077bc7f75faSAuke Kok 		 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
1078bc7f75faSAuke Kok 		 * 1999, describes these PAUSE resolution bits and how flow
1079bc7f75faSAuke Kok 		 * control is determined based upon these settings.
1080bc7f75faSAuke Kok 		 * NOTE:  DC = Don't Care
1081bc7f75faSAuke Kok 		 *
1082bc7f75faSAuke Kok 		 *   LOCAL DEVICE  |   LINK PARTNER
1083bc7f75faSAuke Kok 		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
1084bc7f75faSAuke Kok 		 *-------|---------|-------|---------|--------------------
1085bc7f75faSAuke Kok 		 *   0   |    0    |  DC   |   DC    | e1000_fc_none
1086bc7f75faSAuke Kok 		 *   0   |    1    |   0   |   DC    | e1000_fc_none
1087bc7f75faSAuke Kok 		 *   0   |    1    |   1   |    0    | e1000_fc_none
1088bc7f75faSAuke Kok 		 *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1089bc7f75faSAuke Kok 		 *   1   |    0    |   0   |   DC    | e1000_fc_none
1090bc7f75faSAuke Kok 		 *   1   |   DC    |   1   |   DC    | e1000_fc_full
1091bc7f75faSAuke Kok 		 *   1   |    1    |   0   |    0    | e1000_fc_none
1092bc7f75faSAuke Kok 		 *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1093bc7f75faSAuke Kok 		 *
1094ad68076eSBruce Allan 		 * Are both PAUSE bits set to 1?  If so, this implies
1095bc7f75faSAuke Kok 		 * Symmetric Flow Control is enabled at both ends.  The
1096bc7f75faSAuke Kok 		 * ASM_DIR bits are irrelevant per the spec.
1097bc7f75faSAuke Kok 		 *
1098bc7f75faSAuke Kok 		 * For Symmetric Flow Control:
1099bc7f75faSAuke Kok 		 *
1100bc7f75faSAuke Kok 		 *   LOCAL DEVICE  |   LINK PARTNER
1101bc7f75faSAuke Kok 		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1102bc7f75faSAuke Kok 		 *-------|---------|-------|---------|--------------------
1103bc7f75faSAuke Kok 		 *   1   |   DC    |   1   |   DC    | E1000_fc_full
1104bc7f75faSAuke Kok 		 *
1105bc7f75faSAuke Kok 		 */
1106c2ade1a4SBruce Allan 		if ((mii_nway_adv_reg & ADVERTISE_PAUSE_CAP) &&
1107c2ade1a4SBruce Allan 		    (mii_nway_lp_ability_reg & LPA_PAUSE_CAP)) {
1108e921eb1aSBruce Allan 			/* Now we need to check if the user selected Rx ONLY
1109bc7f75faSAuke Kok 			 * of pause frames.  In this case, we had to advertise
1110ad68076eSBruce Allan 			 * FULL flow control because we could not advertise Rx
1111bc7f75faSAuke Kok 			 * ONLY. Hence, we must now check to see if we need to
1112bc7f75faSAuke Kok 			 * turn OFF the TRANSMISSION of PAUSE frames.
1113bc7f75faSAuke Kok 			 */
11145c48ef3eSBruce Allan 			if (hw->fc.requested_mode == e1000_fc_full) {
11155c48ef3eSBruce Allan 				hw->fc.current_mode = e1000_fc_full;
1116434f1392SBruce Allan 				e_dbg("Flow Control = FULL.\n");
1117bc7f75faSAuke Kok 			} else {
11185c48ef3eSBruce Allan 				hw->fc.current_mode = e1000_fc_rx_pause;
1119434f1392SBruce Allan 				e_dbg("Flow Control = Rx PAUSE frames only.\n");
1120bc7f75faSAuke Kok 			}
1121bc7f75faSAuke Kok 		}
1122e921eb1aSBruce Allan 		/* For receiving PAUSE frames ONLY.
1123bc7f75faSAuke Kok 		 *
1124bc7f75faSAuke Kok 		 *   LOCAL DEVICE  |   LINK PARTNER
1125bc7f75faSAuke Kok 		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1126bc7f75faSAuke Kok 		 *-------|---------|-------|---------|--------------------
1127bc7f75faSAuke Kok 		 *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1128bc7f75faSAuke Kok 		 */
1129c2ade1a4SBruce Allan 		else if (!(mii_nway_adv_reg & ADVERTISE_PAUSE_CAP) &&
1130c2ade1a4SBruce Allan 			 (mii_nway_adv_reg & ADVERTISE_PAUSE_ASYM) &&
1131c2ade1a4SBruce Allan 			 (mii_nway_lp_ability_reg & LPA_PAUSE_CAP) &&
1132c2ade1a4SBruce Allan 			 (mii_nway_lp_ability_reg & LPA_PAUSE_ASYM)) {
11335c48ef3eSBruce Allan 			hw->fc.current_mode = e1000_fc_tx_pause;
1134434f1392SBruce Allan 			e_dbg("Flow Control = Tx PAUSE frames only.\n");
1135bc7f75faSAuke Kok 		}
1136e921eb1aSBruce Allan 		/* For transmitting PAUSE frames ONLY.
1137bc7f75faSAuke Kok 		 *
1138bc7f75faSAuke Kok 		 *   LOCAL DEVICE  |   LINK PARTNER
1139bc7f75faSAuke Kok 		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1140bc7f75faSAuke Kok 		 *-------|---------|-------|---------|--------------------
1141bc7f75faSAuke Kok 		 *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1142bc7f75faSAuke Kok 		 */
1143c2ade1a4SBruce Allan 		else if ((mii_nway_adv_reg & ADVERTISE_PAUSE_CAP) &&
1144c2ade1a4SBruce Allan 			 (mii_nway_adv_reg & ADVERTISE_PAUSE_ASYM) &&
1145c2ade1a4SBruce Allan 			 !(mii_nway_lp_ability_reg & LPA_PAUSE_CAP) &&
1146c2ade1a4SBruce Allan 			 (mii_nway_lp_ability_reg & LPA_PAUSE_ASYM)) {
11475c48ef3eSBruce Allan 			hw->fc.current_mode = e1000_fc_rx_pause;
1148434f1392SBruce Allan 			e_dbg("Flow Control = Rx PAUSE frames only.\n");
1149de92d84eSJesse Brandeburg 		} else {
1150e921eb1aSBruce Allan 			/* Per the IEEE spec, at this point flow control
1151de92d84eSJesse Brandeburg 			 * should be disabled.
1152bc7f75faSAuke Kok 			 */
11535c48ef3eSBruce Allan 			hw->fc.current_mode = e1000_fc_none;
1154434f1392SBruce Allan 			e_dbg("Flow Control = NONE.\n");
1155bc7f75faSAuke Kok 		}
1156bc7f75faSAuke Kok 
1157e921eb1aSBruce Allan 		/* Now we need to do one last check...  If we auto-
1158bc7f75faSAuke Kok 		 * negotiated to HALF DUPLEX, flow control should not be
1159bc7f75faSAuke Kok 		 * enabled per IEEE 802.3 spec.
1160bc7f75faSAuke Kok 		 */
1161bc7f75faSAuke Kok 		ret_val = mac->ops.get_link_up_info(hw, &speed, &duplex);
1162bc7f75faSAuke Kok 		if (ret_val) {
11633bb99fe2SBruce Allan 			e_dbg("Error getting link speed and duplex\n");
1164bc7f75faSAuke Kok 			return ret_val;
1165bc7f75faSAuke Kok 		}
1166bc7f75faSAuke Kok 
1167bc7f75faSAuke Kok 		if (duplex == HALF_DUPLEX)
11685c48ef3eSBruce Allan 			hw->fc.current_mode = e1000_fc_none;
1169bc7f75faSAuke Kok 
1170e921eb1aSBruce Allan 		/* Now we call a subroutine to actually force the MAC
1171bc7f75faSAuke Kok 		 * controller to use the correct flow control settings.
1172bc7f75faSAuke Kok 		 */
1173bc7f75faSAuke Kok 		ret_val = e1000e_force_mac_fc(hw);
1174bc7f75faSAuke Kok 		if (ret_val) {
11753bb99fe2SBruce Allan 			e_dbg("Error forcing flow control settings\n");
1176bc7f75faSAuke Kok 			return ret_val;
1177bc7f75faSAuke Kok 		}
1178bc7f75faSAuke Kok 	}
1179bc7f75faSAuke Kok 
11801241f29fSBruce Allan 	/* Check for the case where we have SerDes media and auto-neg is
11811241f29fSBruce Allan 	 * enabled.  In this case, we need to check and see if Auto-Neg
11821241f29fSBruce Allan 	 * has completed, and if so, how the PHY and link partner has
11831241f29fSBruce Allan 	 * flow control configured.
11841241f29fSBruce Allan 	 */
11851241f29fSBruce Allan 	if ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
11861241f29fSBruce Allan 	    mac->autoneg) {
11871241f29fSBruce Allan 		/* Read the PCS_LSTS and check to see if AutoNeg
11881241f29fSBruce Allan 		 * has completed.
11891241f29fSBruce Allan 		 */
11901241f29fSBruce Allan 		pcs_status_reg = er32(PCS_LSTAT);
11911241f29fSBruce Allan 
11921241f29fSBruce Allan 		if (!(pcs_status_reg & E1000_PCS_LSTS_AN_COMPLETE)) {
11931241f29fSBruce Allan 			e_dbg("PCS Auto Neg has not completed.\n");
11941241f29fSBruce Allan 			return ret_val;
11951241f29fSBruce Allan 		}
11961241f29fSBruce Allan 
11971241f29fSBruce Allan 		/* The AutoNeg process has completed, so we now need to
11981241f29fSBruce Allan 		 * read both the Auto Negotiation Advertisement
11991241f29fSBruce Allan 		 * Register (PCS_ANADV) and the Auto_Negotiation Base
12001241f29fSBruce Allan 		 * Page Ability Register (PCS_LPAB) to determine how
12011241f29fSBruce Allan 		 * flow control was negotiated.
12021241f29fSBruce Allan 		 */
12031241f29fSBruce Allan 		pcs_adv_reg = er32(PCS_ANADV);
12041241f29fSBruce Allan 		pcs_lp_ability_reg = er32(PCS_LPAB);
12051241f29fSBruce Allan 
12061241f29fSBruce Allan 		/* Two bits in the Auto Negotiation Advertisement Register
12071241f29fSBruce Allan 		 * (PCS_ANADV) and two bits in the Auto Negotiation Base
12081241f29fSBruce Allan 		 * Page Ability Register (PCS_LPAB) determine flow control
12091241f29fSBruce Allan 		 * for both the PHY and the link partner.  The following
12101241f29fSBruce Allan 		 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
12111241f29fSBruce Allan 		 * 1999, describes these PAUSE resolution bits and how flow
12121241f29fSBruce Allan 		 * control is determined based upon these settings.
12131241f29fSBruce Allan 		 * NOTE:  DC = Don't Care
12141241f29fSBruce Allan 		 *
12151241f29fSBruce Allan 		 *   LOCAL DEVICE  |   LINK PARTNER
12161241f29fSBruce Allan 		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
12171241f29fSBruce Allan 		 *-------|---------|-------|---------|--------------------
12181241f29fSBruce Allan 		 *   0   |    0    |  DC   |   DC    | e1000_fc_none
12191241f29fSBruce Allan 		 *   0   |    1    |   0   |   DC    | e1000_fc_none
12201241f29fSBruce Allan 		 *   0   |    1    |   1   |    0    | e1000_fc_none
12211241f29fSBruce Allan 		 *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
12221241f29fSBruce Allan 		 *   1   |    0    |   0   |   DC    | e1000_fc_none
12231241f29fSBruce Allan 		 *   1   |   DC    |   1   |   DC    | e1000_fc_full
12241241f29fSBruce Allan 		 *   1   |    1    |   0   |    0    | e1000_fc_none
12251241f29fSBruce Allan 		 *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
12261241f29fSBruce Allan 		 *
12271241f29fSBruce Allan 		 * Are both PAUSE bits set to 1?  If so, this implies
12281241f29fSBruce Allan 		 * Symmetric Flow Control is enabled at both ends.  The
12291241f29fSBruce Allan 		 * ASM_DIR bits are irrelevant per the spec.
12301241f29fSBruce Allan 		 *
12311241f29fSBruce Allan 		 * For Symmetric Flow Control:
12321241f29fSBruce Allan 		 *
12331241f29fSBruce Allan 		 *   LOCAL DEVICE  |   LINK PARTNER
12341241f29fSBruce Allan 		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
12351241f29fSBruce Allan 		 *-------|---------|-------|---------|--------------------
12361241f29fSBruce Allan 		 *   1   |   DC    |   1   |   DC    | e1000_fc_full
12371241f29fSBruce Allan 		 *
12381241f29fSBruce Allan 		 */
12391241f29fSBruce Allan 		if ((pcs_adv_reg & E1000_TXCW_PAUSE) &&
12401241f29fSBruce Allan 		    (pcs_lp_ability_reg & E1000_TXCW_PAUSE)) {
12411241f29fSBruce Allan 			/* Now we need to check if the user selected Rx ONLY
12421241f29fSBruce Allan 			 * of pause frames.  In this case, we had to advertise
12431241f29fSBruce Allan 			 * FULL flow control because we could not advertise Rx
12441241f29fSBruce Allan 			 * ONLY. Hence, we must now check to see if we need to
12451241f29fSBruce Allan 			 * turn OFF the TRANSMISSION of PAUSE frames.
12461241f29fSBruce Allan 			 */
12471241f29fSBruce Allan 			if (hw->fc.requested_mode == e1000_fc_full) {
12481241f29fSBruce Allan 				hw->fc.current_mode = e1000_fc_full;
12491241f29fSBruce Allan 				e_dbg("Flow Control = FULL.\n");
12501241f29fSBruce Allan 			} else {
12511241f29fSBruce Allan 				hw->fc.current_mode = e1000_fc_rx_pause;
12521241f29fSBruce Allan 				e_dbg("Flow Control = Rx PAUSE frames only.\n");
12531241f29fSBruce Allan 			}
12541241f29fSBruce Allan 		}
12551241f29fSBruce Allan 		/* For receiving PAUSE frames ONLY.
12561241f29fSBruce Allan 		 *
12571241f29fSBruce Allan 		 *   LOCAL DEVICE  |   LINK PARTNER
12581241f29fSBruce Allan 		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
12591241f29fSBruce Allan 		 *-------|---------|-------|---------|--------------------
12601241f29fSBruce Allan 		 *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
12611241f29fSBruce Allan 		 */
12621241f29fSBruce Allan 		else if (!(pcs_adv_reg & E1000_TXCW_PAUSE) &&
12631241f29fSBruce Allan 			 (pcs_adv_reg & E1000_TXCW_ASM_DIR) &&
12641241f29fSBruce Allan 			 (pcs_lp_ability_reg & E1000_TXCW_PAUSE) &&
12651241f29fSBruce Allan 			 (pcs_lp_ability_reg & E1000_TXCW_ASM_DIR)) {
12661241f29fSBruce Allan 			hw->fc.current_mode = e1000_fc_tx_pause;
12671241f29fSBruce Allan 			e_dbg("Flow Control = Tx PAUSE frames only.\n");
12681241f29fSBruce Allan 		}
12691241f29fSBruce Allan 		/* For transmitting PAUSE frames ONLY.
12701241f29fSBruce Allan 		 *
12711241f29fSBruce Allan 		 *   LOCAL DEVICE  |   LINK PARTNER
12721241f29fSBruce Allan 		 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
12731241f29fSBruce Allan 		 *-------|---------|-------|---------|--------------------
12741241f29fSBruce Allan 		 *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
12751241f29fSBruce Allan 		 */
12761241f29fSBruce Allan 		else if ((pcs_adv_reg & E1000_TXCW_PAUSE) &&
12771241f29fSBruce Allan 			 (pcs_adv_reg & E1000_TXCW_ASM_DIR) &&
12781241f29fSBruce Allan 			 !(pcs_lp_ability_reg & E1000_TXCW_PAUSE) &&
12791241f29fSBruce Allan 			 (pcs_lp_ability_reg & E1000_TXCW_ASM_DIR)) {
12801241f29fSBruce Allan 			hw->fc.current_mode = e1000_fc_rx_pause;
12811241f29fSBruce Allan 			e_dbg("Flow Control = Rx PAUSE frames only.\n");
12821241f29fSBruce Allan 		} else {
12831241f29fSBruce Allan 			/* Per the IEEE spec, at this point flow control
12841241f29fSBruce Allan 			 * should be disabled.
12851241f29fSBruce Allan 			 */
12861241f29fSBruce Allan 			hw->fc.current_mode = e1000_fc_none;
12871241f29fSBruce Allan 			e_dbg("Flow Control = NONE.\n");
12881241f29fSBruce Allan 		}
12891241f29fSBruce Allan 
12901241f29fSBruce Allan 		/* Now we call a subroutine to actually force the MAC
12911241f29fSBruce Allan 		 * controller to use the correct flow control settings.
12921241f29fSBruce Allan 		 */
12931241f29fSBruce Allan 		pcs_ctrl_reg = er32(PCS_LCTL);
12941241f29fSBruce Allan 		pcs_ctrl_reg |= E1000_PCS_LCTL_FORCE_FCTRL;
12951241f29fSBruce Allan 		ew32(PCS_LCTL, pcs_ctrl_reg);
12961241f29fSBruce Allan 
12971241f29fSBruce Allan 		ret_val = e1000e_force_mac_fc(hw);
12981241f29fSBruce Allan 		if (ret_val) {
12991241f29fSBruce Allan 			e_dbg("Error forcing flow control settings\n");
13001241f29fSBruce Allan 			return ret_val;
13011241f29fSBruce Allan 		}
13021241f29fSBruce Allan 	}
13031241f29fSBruce Allan 
1304bc7f75faSAuke Kok 	return 0;
1305bc7f75faSAuke Kok }
1306bc7f75faSAuke Kok 
1307bc7f75faSAuke Kok /**
1308489815ceSAuke Kok  *  e1000e_get_speed_and_duplex_copper - Retrieve current speed/duplex
1309bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1310bc7f75faSAuke Kok  *  @speed: stores the current speed
1311bc7f75faSAuke Kok  *  @duplex: stores the current duplex
1312bc7f75faSAuke Kok  *
1313bc7f75faSAuke Kok  *  Read the status register for the current speed/duplex and store the current
1314bc7f75faSAuke Kok  *  speed and duplex for copper connections.
1315bc7f75faSAuke Kok  **/
e1000e_get_speed_and_duplex_copper(struct e1000_hw * hw,u16 * speed,u16 * duplex)1316fe2ddfb5SBruce Allan s32 e1000e_get_speed_and_duplex_copper(struct e1000_hw *hw, u16 *speed,
1317fe2ddfb5SBruce Allan 				       u16 *duplex)
1318bc7f75faSAuke Kok {
1319bc7f75faSAuke Kok 	u32 status;
1320bc7f75faSAuke Kok 
1321bc7f75faSAuke Kok 	status = er32(STATUS);
13222c73e1feSJoe Perches 	if (status & E1000_STATUS_SPEED_1000)
1323bc7f75faSAuke Kok 		*speed = SPEED_1000;
13242c73e1feSJoe Perches 	else if (status & E1000_STATUS_SPEED_100)
1325bc7f75faSAuke Kok 		*speed = SPEED_100;
13262c73e1feSJoe Perches 	else
1327bc7f75faSAuke Kok 		*speed = SPEED_10;
1328bc7f75faSAuke Kok 
13292c73e1feSJoe Perches 	if (status & E1000_STATUS_FD)
1330bc7f75faSAuke Kok 		*duplex = FULL_DUPLEX;
13312c73e1feSJoe Perches 	else
1332bc7f75faSAuke Kok 		*duplex = HALF_DUPLEX;
13332c73e1feSJoe Perches 
13342c73e1feSJoe Perches 	e_dbg("%u Mbps, %s Duplex\n",
13352c73e1feSJoe Perches 	      *speed == SPEED_1000 ? 1000 : *speed == SPEED_100 ? 100 : 10,
13362c73e1feSJoe Perches 	      *duplex == FULL_DUPLEX ? "Full" : "Half");
1337bc7f75faSAuke Kok 
1338bc7f75faSAuke Kok 	return 0;
1339bc7f75faSAuke Kok }
1340bc7f75faSAuke Kok 
1341bc7f75faSAuke Kok /**
1342489815ceSAuke Kok  *  e1000e_get_speed_and_duplex_fiber_serdes - Retrieve current speed/duplex
1343bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1344bc7f75faSAuke Kok  *  @speed: stores the current speed
1345bc7f75faSAuke Kok  *  @duplex: stores the current duplex
1346bc7f75faSAuke Kok  *
1347bc7f75faSAuke Kok  *  Sets the speed and duplex to gigabit full duplex (the only possible option)
1348bc7f75faSAuke Kok  *  for fiber/serdes links.
1349bc7f75faSAuke Kok  **/
e1000e_get_speed_and_duplex_fiber_serdes(struct e1000_hw __always_unused * hw,u16 * speed,u16 * duplex)13508bb62869SBruce Allan s32 e1000e_get_speed_and_duplex_fiber_serdes(struct e1000_hw __always_unused
13518bb62869SBruce Allan 					     *hw, u16 *speed, u16 *duplex)
1352bc7f75faSAuke Kok {
1353bc7f75faSAuke Kok 	*speed = SPEED_1000;
1354bc7f75faSAuke Kok 	*duplex = FULL_DUPLEX;
1355bc7f75faSAuke Kok 
1356bc7f75faSAuke Kok 	return 0;
1357bc7f75faSAuke Kok }
1358bc7f75faSAuke Kok 
1359bc7f75faSAuke Kok /**
1360bc7f75faSAuke Kok  *  e1000e_get_hw_semaphore - Acquire hardware semaphore
1361bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1362bc7f75faSAuke Kok  *
1363bc7f75faSAuke Kok  *  Acquire the HW semaphore to access the PHY or NVM
1364bc7f75faSAuke Kok  **/
e1000e_get_hw_semaphore(struct e1000_hw * hw)1365bc7f75faSAuke Kok s32 e1000e_get_hw_semaphore(struct e1000_hw *hw)
1366bc7f75faSAuke Kok {
1367bc7f75faSAuke Kok 	u32 swsm;
1368bc7f75faSAuke Kok 	s32 timeout = hw->nvm.word_size + 1;
1369bc7f75faSAuke Kok 	s32 i = 0;
1370bc7f75faSAuke Kok 
1371bc7f75faSAuke Kok 	/* Get the SW semaphore */
1372bc7f75faSAuke Kok 	while (i < timeout) {
1373bc7f75faSAuke Kok 		swsm = er32(SWSM);
1374bc7f75faSAuke Kok 		if (!(swsm & E1000_SWSM_SMBI))
1375bc7f75faSAuke Kok 			break;
1376bc7f75faSAuke Kok 
13772e05f756SJia-Ju Bai 		udelay(100);
1378bc7f75faSAuke Kok 		i++;
1379bc7f75faSAuke Kok 	}
1380bc7f75faSAuke Kok 
1381bc7f75faSAuke Kok 	if (i == timeout) {
13823bb99fe2SBruce Allan 		e_dbg("Driver can't access device - SMBI bit is set.\n");
1383bc7f75faSAuke Kok 		return -E1000_ERR_NVM;
1384bc7f75faSAuke Kok 	}
1385bc7f75faSAuke Kok 
1386bc7f75faSAuke Kok 	/* Get the FW semaphore. */
1387bc7f75faSAuke Kok 	for (i = 0; i < timeout; i++) {
1388bc7f75faSAuke Kok 		swsm = er32(SWSM);
1389bc7f75faSAuke Kok 		ew32(SWSM, swsm | E1000_SWSM_SWESMBI);
1390bc7f75faSAuke Kok 
1391bc7f75faSAuke Kok 		/* Semaphore acquired if bit latched */
1392bc7f75faSAuke Kok 		if (er32(SWSM) & E1000_SWSM_SWESMBI)
1393bc7f75faSAuke Kok 			break;
1394bc7f75faSAuke Kok 
13952e05f756SJia-Ju Bai 		udelay(100);
1396bc7f75faSAuke Kok 	}
1397bc7f75faSAuke Kok 
1398bc7f75faSAuke Kok 	if (i == timeout) {
1399bc7f75faSAuke Kok 		/* Release semaphores */
1400bc7f75faSAuke Kok 		e1000e_put_hw_semaphore(hw);
14013bb99fe2SBruce Allan 		e_dbg("Driver can't access the NVM\n");
1402bc7f75faSAuke Kok 		return -E1000_ERR_NVM;
1403bc7f75faSAuke Kok 	}
1404bc7f75faSAuke Kok 
1405bc7f75faSAuke Kok 	return 0;
1406bc7f75faSAuke Kok }
1407bc7f75faSAuke Kok 
1408bc7f75faSAuke Kok /**
1409bc7f75faSAuke Kok  *  e1000e_put_hw_semaphore - Release hardware semaphore
1410bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1411bc7f75faSAuke Kok  *
1412bc7f75faSAuke Kok  *  Release hardware semaphore used to access the PHY or NVM
1413bc7f75faSAuke Kok  **/
e1000e_put_hw_semaphore(struct e1000_hw * hw)1414bc7f75faSAuke Kok void e1000e_put_hw_semaphore(struct e1000_hw *hw)
1415bc7f75faSAuke Kok {
1416bc7f75faSAuke Kok 	u32 swsm;
1417bc7f75faSAuke Kok 
1418bc7f75faSAuke Kok 	swsm = er32(SWSM);
1419bc7f75faSAuke Kok 	swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1420bc7f75faSAuke Kok 	ew32(SWSM, swsm);
1421bc7f75faSAuke Kok }
1422bc7f75faSAuke Kok 
1423bc7f75faSAuke Kok /**
1424bc7f75faSAuke Kok  *  e1000e_get_auto_rd_done - Check for auto read completion
1425bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1426bc7f75faSAuke Kok  *
1427bc7f75faSAuke Kok  *  Check EEPROM for Auto Read done bit.
1428bc7f75faSAuke Kok  **/
e1000e_get_auto_rd_done(struct e1000_hw * hw)1429bc7f75faSAuke Kok s32 e1000e_get_auto_rd_done(struct e1000_hw *hw)
1430bc7f75faSAuke Kok {
1431bc7f75faSAuke Kok 	s32 i = 0;
1432bc7f75faSAuke Kok 
1433bc7f75faSAuke Kok 	while (i < AUTO_READ_DONE_TIMEOUT) {
1434bc7f75faSAuke Kok 		if (er32(EECD) & E1000_EECD_AUTO_RD)
1435bc7f75faSAuke Kok 			break;
14361bba4386SBruce Allan 		usleep_range(1000, 2000);
1437bc7f75faSAuke Kok 		i++;
1438bc7f75faSAuke Kok 	}
1439bc7f75faSAuke Kok 
1440bc7f75faSAuke Kok 	if (i == AUTO_READ_DONE_TIMEOUT) {
14413bb99fe2SBruce Allan 		e_dbg("Auto read by HW from NVM has not completed.\n");
1442bc7f75faSAuke Kok 		return -E1000_ERR_RESET;
1443bc7f75faSAuke Kok 	}
1444bc7f75faSAuke Kok 
1445bc7f75faSAuke Kok 	return 0;
1446bc7f75faSAuke Kok }
1447bc7f75faSAuke Kok 
1448bc7f75faSAuke Kok /**
1449bc7f75faSAuke Kok  *  e1000e_valid_led_default - Verify a valid default LED config
1450bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1451bc7f75faSAuke Kok  *  @data: pointer to the NVM (EEPROM)
1452bc7f75faSAuke Kok  *
1453bc7f75faSAuke Kok  *  Read the EEPROM for the current default LED configuration.  If the
1454bc7f75faSAuke Kok  *  LED configuration is not valid, set to a valid LED configuration.
1455bc7f75faSAuke Kok  **/
e1000e_valid_led_default(struct e1000_hw * hw,u16 * data)1456bc7f75faSAuke Kok s32 e1000e_valid_led_default(struct e1000_hw *hw, u16 *data)
1457bc7f75faSAuke Kok {
1458bc7f75faSAuke Kok 	s32 ret_val;
1459bc7f75faSAuke Kok 
1460bc7f75faSAuke Kok 	ret_val = e1000_read_nvm(hw, NVM_ID_LED_SETTINGS, 1, data);
1461bc7f75faSAuke Kok 	if (ret_val) {
14623bb99fe2SBruce Allan 		e_dbg("NVM Read Error\n");
1463bc7f75faSAuke Kok 		return ret_val;
1464bc7f75faSAuke Kok 	}
1465bc7f75faSAuke Kok 
1466bc7f75faSAuke Kok 	if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF)
1467bc7f75faSAuke Kok 		*data = ID_LED_DEFAULT;
1468bc7f75faSAuke Kok 
1469bc7f75faSAuke Kok 	return 0;
1470bc7f75faSAuke Kok }
1471bc7f75faSAuke Kok 
1472bc7f75faSAuke Kok /**
1473d1964eb1SBruce Allan  *  e1000e_id_led_init_generic -
1474bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1475bc7f75faSAuke Kok  *
1476bc7f75faSAuke Kok  **/
e1000e_id_led_init_generic(struct e1000_hw * hw)1477d1964eb1SBruce Allan s32 e1000e_id_led_init_generic(struct e1000_hw *hw)
1478bc7f75faSAuke Kok {
1479bc7f75faSAuke Kok 	struct e1000_mac_info *mac = &hw->mac;
1480bc7f75faSAuke Kok 	s32 ret_val;
1481bc7f75faSAuke Kok 	const u32 ledctl_mask = 0x000000FF;
1482bc7f75faSAuke Kok 	const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
1483bc7f75faSAuke Kok 	const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
1484bc7f75faSAuke Kok 	u16 data, i, temp;
1485bc7f75faSAuke Kok 	const u16 led_mask = 0x0F;
1486bc7f75faSAuke Kok 
1487bc7f75faSAuke Kok 	ret_val = hw->nvm.ops.valid_led_default(hw, &data);
1488bc7f75faSAuke Kok 	if (ret_val)
1489bc7f75faSAuke Kok 		return ret_val;
1490bc7f75faSAuke Kok 
1491bc7f75faSAuke Kok 	mac->ledctl_default = er32(LEDCTL);
1492bc7f75faSAuke Kok 	mac->ledctl_mode1 = mac->ledctl_default;
1493bc7f75faSAuke Kok 	mac->ledctl_mode2 = mac->ledctl_default;
1494bc7f75faSAuke Kok 
1495bc7f75faSAuke Kok 	for (i = 0; i < 4; i++) {
1496bc7f75faSAuke Kok 		temp = (data >> (i << 2)) & led_mask;
1497bc7f75faSAuke Kok 		switch (temp) {
1498bc7f75faSAuke Kok 		case ID_LED_ON1_DEF2:
1499bc7f75faSAuke Kok 		case ID_LED_ON1_ON2:
1500bc7f75faSAuke Kok 		case ID_LED_ON1_OFF2:
1501bc7f75faSAuke Kok 			mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1502bc7f75faSAuke Kok 			mac->ledctl_mode1 |= ledctl_on << (i << 3);
1503bc7f75faSAuke Kok 			break;
1504bc7f75faSAuke Kok 		case ID_LED_OFF1_DEF2:
1505bc7f75faSAuke Kok 		case ID_LED_OFF1_ON2:
1506bc7f75faSAuke Kok 		case ID_LED_OFF1_OFF2:
1507bc7f75faSAuke Kok 			mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1508bc7f75faSAuke Kok 			mac->ledctl_mode1 |= ledctl_off << (i << 3);
1509bc7f75faSAuke Kok 			break;
1510bc7f75faSAuke Kok 		default:
1511bc7f75faSAuke Kok 			/* Do nothing */
1512bc7f75faSAuke Kok 			break;
1513bc7f75faSAuke Kok 		}
1514bc7f75faSAuke Kok 		switch (temp) {
1515bc7f75faSAuke Kok 		case ID_LED_DEF1_ON2:
1516bc7f75faSAuke Kok 		case ID_LED_ON1_ON2:
1517bc7f75faSAuke Kok 		case ID_LED_OFF1_ON2:
1518bc7f75faSAuke Kok 			mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1519bc7f75faSAuke Kok 			mac->ledctl_mode2 |= ledctl_on << (i << 3);
1520bc7f75faSAuke Kok 			break;
1521bc7f75faSAuke Kok 		case ID_LED_DEF1_OFF2:
1522bc7f75faSAuke Kok 		case ID_LED_ON1_OFF2:
1523bc7f75faSAuke Kok 		case ID_LED_OFF1_OFF2:
1524bc7f75faSAuke Kok 			mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1525bc7f75faSAuke Kok 			mac->ledctl_mode2 |= ledctl_off << (i << 3);
1526bc7f75faSAuke Kok 			break;
1527bc7f75faSAuke Kok 		default:
1528bc7f75faSAuke Kok 			/* Do nothing */
1529bc7f75faSAuke Kok 			break;
1530bc7f75faSAuke Kok 		}
1531bc7f75faSAuke Kok 	}
1532bc7f75faSAuke Kok 
1533bc7f75faSAuke Kok 	return 0;
1534bc7f75faSAuke Kok }
1535bc7f75faSAuke Kok 
1536bc7f75faSAuke Kok /**
1537a4f58f54SBruce Allan  *  e1000e_setup_led_generic - Configures SW controllable LED
1538a4f58f54SBruce Allan  *  @hw: pointer to the HW structure
1539a4f58f54SBruce Allan  *
1540a4f58f54SBruce Allan  *  This prepares the SW controllable LED for use and saves the current state
1541a4f58f54SBruce Allan  *  of the LED so it can be later restored.
1542a4f58f54SBruce Allan  **/
e1000e_setup_led_generic(struct e1000_hw * hw)1543a4f58f54SBruce Allan s32 e1000e_setup_led_generic(struct e1000_hw *hw)
1544a4f58f54SBruce Allan {
1545a4f58f54SBruce Allan 	u32 ledctl;
1546a4f58f54SBruce Allan 
1547b1cdfeadSBruce Allan 	if (hw->mac.ops.setup_led != e1000e_setup_led_generic)
1548a4f58f54SBruce Allan 		return -E1000_ERR_CONFIG;
1549a4f58f54SBruce Allan 
1550a4f58f54SBruce Allan 	if (hw->phy.media_type == e1000_media_type_fiber) {
1551a4f58f54SBruce Allan 		ledctl = er32(LEDCTL);
1552a4f58f54SBruce Allan 		hw->mac.ledctl_default = ledctl;
1553a4f58f54SBruce Allan 		/* Turn off LED0 */
1554fe2ddfb5SBruce Allan 		ledctl &= ~(E1000_LEDCTL_LED0_IVRT | E1000_LEDCTL_LED0_BLINK |
1555a4f58f54SBruce Allan 			    E1000_LEDCTL_LED0_MODE_MASK);
1556a4f58f54SBruce Allan 		ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
1557a4f58f54SBruce Allan 			   E1000_LEDCTL_LED0_MODE_SHIFT);
1558a4f58f54SBruce Allan 		ew32(LEDCTL, ledctl);
1559a4f58f54SBruce Allan 	} else if (hw->phy.media_type == e1000_media_type_copper) {
1560a4f58f54SBruce Allan 		ew32(LEDCTL, hw->mac.ledctl_mode1);
1561a4f58f54SBruce Allan 	}
1562a4f58f54SBruce Allan 
1563a4f58f54SBruce Allan 	return 0;
1564a4f58f54SBruce Allan }
1565a4f58f54SBruce Allan 
1566a4f58f54SBruce Allan /**
1567bc7f75faSAuke Kok  *  e1000e_cleanup_led_generic - Set LED config to default operation
1568bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1569bc7f75faSAuke Kok  *
1570bc7f75faSAuke Kok  *  Remove the current LED configuration and set the LED configuration
1571bc7f75faSAuke Kok  *  to the default value, saved from the EEPROM.
1572bc7f75faSAuke Kok  **/
e1000e_cleanup_led_generic(struct e1000_hw * hw)1573bc7f75faSAuke Kok s32 e1000e_cleanup_led_generic(struct e1000_hw *hw)
1574bc7f75faSAuke Kok {
1575bc7f75faSAuke Kok 	ew32(LEDCTL, hw->mac.ledctl_default);
1576bc7f75faSAuke Kok 	return 0;
1577bc7f75faSAuke Kok }
1578bc7f75faSAuke Kok 
1579bc7f75faSAuke Kok /**
1580dbf80dcbSBruce Allan  *  e1000e_blink_led_generic - Blink LED
1581bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1582bc7f75faSAuke Kok  *
1583489815ceSAuke Kok  *  Blink the LEDs which are set to be on.
1584bc7f75faSAuke Kok  **/
e1000e_blink_led_generic(struct e1000_hw * hw)1585dbf80dcbSBruce Allan s32 e1000e_blink_led_generic(struct e1000_hw *hw)
1586bc7f75faSAuke Kok {
1587bc7f75faSAuke Kok 	u32 ledctl_blink = 0;
1588bc7f75faSAuke Kok 	u32 i;
1589bc7f75faSAuke Kok 
1590318a94d6SJeff Kirsher 	if (hw->phy.media_type == e1000_media_type_fiber) {
1591bc7f75faSAuke Kok 		/* always blink LED0 for PCI-E fiber */
1592bc7f75faSAuke Kok 		ledctl_blink = E1000_LEDCTL_LED0_BLINK |
1593bc7f75faSAuke Kok 		    (E1000_LEDCTL_MODE_LED_ON << E1000_LEDCTL_LED0_MODE_SHIFT);
1594bc7f75faSAuke Kok 	} else {
159586a80eabSBruce Allan 		/* Set the blink bit for each LED that's "on" (0x0E)
159686a80eabSBruce Allan 		 * (or "off" if inverted) in ledctl_mode2.  The blink
159786a80eabSBruce Allan 		 * logic in hardware only works when mode is set to "on"
159886a80eabSBruce Allan 		 * so it must be changed accordingly when the mode is
159986a80eabSBruce Allan 		 * "off" and inverted.
1600ad68076eSBruce Allan 		 */
1601bc7f75faSAuke Kok 		ledctl_blink = hw->mac.ledctl_mode2;
160286a80eabSBruce Allan 		for (i = 0; i < 32; i += 8) {
160386a80eabSBruce Allan 			u32 mode = (hw->mac.ledctl_mode2 >> i) &
160486a80eabSBruce Allan 			    E1000_LEDCTL_LED0_MODE_MASK;
160586a80eabSBruce Allan 			u32 led_default = hw->mac.ledctl_default >> i;
160686a80eabSBruce Allan 
160786a80eabSBruce Allan 			if ((!(led_default & E1000_LEDCTL_LED0_IVRT) &&
160886a80eabSBruce Allan 			     (mode == E1000_LEDCTL_MODE_LED_ON)) ||
160986a80eabSBruce Allan 			    ((led_default & E1000_LEDCTL_LED0_IVRT) &&
161086a80eabSBruce Allan 			     (mode == E1000_LEDCTL_MODE_LED_OFF))) {
161186a80eabSBruce Allan 				ledctl_blink &=
161286a80eabSBruce Allan 				    ~(E1000_LEDCTL_LED0_MODE_MASK << i);
161386a80eabSBruce Allan 				ledctl_blink |= (E1000_LEDCTL_LED0_BLINK |
161486a80eabSBruce Allan 						 E1000_LEDCTL_MODE_LED_ON) << i;
161586a80eabSBruce Allan 			}
161686a80eabSBruce Allan 		}
1617bc7f75faSAuke Kok 	}
1618bc7f75faSAuke Kok 
1619bc7f75faSAuke Kok 	ew32(LEDCTL, ledctl_blink);
1620bc7f75faSAuke Kok 
1621bc7f75faSAuke Kok 	return 0;
1622bc7f75faSAuke Kok }
1623bc7f75faSAuke Kok 
1624bc7f75faSAuke Kok /**
1625bc7f75faSAuke Kok  *  e1000e_led_on_generic - Turn LED on
1626bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1627bc7f75faSAuke Kok  *
1628bc7f75faSAuke Kok  *  Turn LED on.
1629bc7f75faSAuke Kok  **/
e1000e_led_on_generic(struct e1000_hw * hw)1630bc7f75faSAuke Kok s32 e1000e_led_on_generic(struct e1000_hw *hw)
1631bc7f75faSAuke Kok {
1632bc7f75faSAuke Kok 	u32 ctrl;
1633bc7f75faSAuke Kok 
1634318a94d6SJeff Kirsher 	switch (hw->phy.media_type) {
1635bc7f75faSAuke Kok 	case e1000_media_type_fiber:
1636bc7f75faSAuke Kok 		ctrl = er32(CTRL);
1637bc7f75faSAuke Kok 		ctrl &= ~E1000_CTRL_SWDPIN0;
1638bc7f75faSAuke Kok 		ctrl |= E1000_CTRL_SWDPIO0;
1639bc7f75faSAuke Kok 		ew32(CTRL, ctrl);
1640bc7f75faSAuke Kok 		break;
1641bc7f75faSAuke Kok 	case e1000_media_type_copper:
1642bc7f75faSAuke Kok 		ew32(LEDCTL, hw->mac.ledctl_mode2);
1643bc7f75faSAuke Kok 		break;
1644bc7f75faSAuke Kok 	default:
1645bc7f75faSAuke Kok 		break;
1646bc7f75faSAuke Kok 	}
1647bc7f75faSAuke Kok 
1648bc7f75faSAuke Kok 	return 0;
1649bc7f75faSAuke Kok }
1650bc7f75faSAuke Kok 
1651bc7f75faSAuke Kok /**
1652bc7f75faSAuke Kok  *  e1000e_led_off_generic - Turn LED off
1653bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1654bc7f75faSAuke Kok  *
1655bc7f75faSAuke Kok  *  Turn LED off.
1656bc7f75faSAuke Kok  **/
e1000e_led_off_generic(struct e1000_hw * hw)1657bc7f75faSAuke Kok s32 e1000e_led_off_generic(struct e1000_hw *hw)
1658bc7f75faSAuke Kok {
1659bc7f75faSAuke Kok 	u32 ctrl;
1660bc7f75faSAuke Kok 
1661318a94d6SJeff Kirsher 	switch (hw->phy.media_type) {
1662bc7f75faSAuke Kok 	case e1000_media_type_fiber:
1663bc7f75faSAuke Kok 		ctrl = er32(CTRL);
1664bc7f75faSAuke Kok 		ctrl |= E1000_CTRL_SWDPIN0;
1665bc7f75faSAuke Kok 		ctrl |= E1000_CTRL_SWDPIO0;
1666bc7f75faSAuke Kok 		ew32(CTRL, ctrl);
1667bc7f75faSAuke Kok 		break;
1668bc7f75faSAuke Kok 	case e1000_media_type_copper:
1669bc7f75faSAuke Kok 		ew32(LEDCTL, hw->mac.ledctl_mode1);
1670bc7f75faSAuke Kok 		break;
1671bc7f75faSAuke Kok 	default:
1672bc7f75faSAuke Kok 		break;
1673bc7f75faSAuke Kok 	}
1674bc7f75faSAuke Kok 
1675bc7f75faSAuke Kok 	return 0;
1676bc7f75faSAuke Kok }
1677bc7f75faSAuke Kok 
1678bc7f75faSAuke Kok /**
1679bc7f75faSAuke Kok  *  e1000e_set_pcie_no_snoop - Set PCI-express capabilities
1680bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1681bc7f75faSAuke Kok  *  @no_snoop: bitmap of snoop events
1682bc7f75faSAuke Kok  *
1683bc7f75faSAuke Kok  *  Set the PCI-express register to snoop for events enabled in 'no_snoop'.
1684bc7f75faSAuke Kok  **/
e1000e_set_pcie_no_snoop(struct e1000_hw * hw,u32 no_snoop)1685bc7f75faSAuke Kok void e1000e_set_pcie_no_snoop(struct e1000_hw *hw, u32 no_snoop)
1686bc7f75faSAuke Kok {
1687bc7f75faSAuke Kok 	u32 gcr;
1688bc7f75faSAuke Kok 
1689bc7f75faSAuke Kok 	if (no_snoop) {
1690bc7f75faSAuke Kok 		gcr = er32(GCR);
1691bc7f75faSAuke Kok 		gcr &= ~(PCIE_NO_SNOOP_ALL);
1692bc7f75faSAuke Kok 		gcr |= no_snoop;
1693bc7f75faSAuke Kok 		ew32(GCR, gcr);
1694bc7f75faSAuke Kok 	}
1695bc7f75faSAuke Kok }
1696bc7f75faSAuke Kok 
1697bc7f75faSAuke Kok /**
1698bc7f75faSAuke Kok  *  e1000e_disable_pcie_master - Disables PCI-express master access
1699bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1700bc7f75faSAuke Kok  *
1701bc7f75faSAuke Kok  *  Returns 0 if successful, else returns -10
1702489815ceSAuke Kok  *  (-E1000_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not caused
1703bc7f75faSAuke Kok  *  the master requests to be disabled.
1704bc7f75faSAuke Kok  *
1705bc7f75faSAuke Kok  *  Disables PCI-Express master access and verifies there are no pending
1706bc7f75faSAuke Kok  *  requests.
1707bc7f75faSAuke Kok  **/
e1000e_disable_pcie_master(struct e1000_hw * hw)1708bc7f75faSAuke Kok s32 e1000e_disable_pcie_master(struct e1000_hw *hw)
1709bc7f75faSAuke Kok {
1710bc7f75faSAuke Kok 	u32 ctrl;
1711bc7f75faSAuke Kok 	s32 timeout = MASTER_DISABLE_TIMEOUT;
1712bc7f75faSAuke Kok 
1713bc7f75faSAuke Kok 	ctrl = er32(CTRL);
1714bc7f75faSAuke Kok 	ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
1715bc7f75faSAuke Kok 	ew32(CTRL, ctrl);
1716bc7f75faSAuke Kok 
1717bc7f75faSAuke Kok 	while (timeout) {
1718fe2ddfb5SBruce Allan 		if (!(er32(STATUS) & E1000_STATUS_GIO_MASTER_ENABLE))
1719bc7f75faSAuke Kok 			break;
1720ce43a216SBruce Allan 		usleep_range(100, 200);
1721bc7f75faSAuke Kok 		timeout--;
1722bc7f75faSAuke Kok 	}
1723bc7f75faSAuke Kok 
1724bc7f75faSAuke Kok 	if (!timeout) {
17253bb99fe2SBruce Allan 		e_dbg("Master requests are pending.\n");
1726bc7f75faSAuke Kok 		return -E1000_ERR_MASTER_REQUESTS_PENDING;
1727bc7f75faSAuke Kok 	}
1728bc7f75faSAuke Kok 
1729bc7f75faSAuke Kok 	return 0;
1730bc7f75faSAuke Kok }
1731bc7f75faSAuke Kok 
1732bc7f75faSAuke Kok /**
1733bc7f75faSAuke Kok  *  e1000e_reset_adaptive - Reset Adaptive Interframe Spacing
1734bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1735bc7f75faSAuke Kok  *
1736bc7f75faSAuke Kok  *  Reset the Adaptive Interframe Spacing throttle to default values.
1737bc7f75faSAuke Kok  **/
e1000e_reset_adaptive(struct e1000_hw * hw)1738bc7f75faSAuke Kok void e1000e_reset_adaptive(struct e1000_hw *hw)
1739bc7f75faSAuke Kok {
1740bc7f75faSAuke Kok 	struct e1000_mac_info *mac = &hw->mac;
1741bc7f75faSAuke Kok 
1742f464ba87SBruce Allan 	if (!mac->adaptive_ifs) {
1743f464ba87SBruce Allan 		e_dbg("Not in Adaptive IFS mode!\n");
1744fe1e980fSBruce Allan 		return;
1745f464ba87SBruce Allan 	}
1746f464ba87SBruce Allan 
1747bc7f75faSAuke Kok 	mac->current_ifs_val = 0;
1748bc7f75faSAuke Kok 	mac->ifs_min_val = IFS_MIN;
1749bc7f75faSAuke Kok 	mac->ifs_max_val = IFS_MAX;
1750bc7f75faSAuke Kok 	mac->ifs_step_size = IFS_STEP;
1751bc7f75faSAuke Kok 	mac->ifs_ratio = IFS_RATIO;
1752bc7f75faSAuke Kok 
1753564ea9bbSBruce Allan 	mac->in_ifs_mode = false;
1754bc7f75faSAuke Kok 	ew32(AIT, 0);
1755bc7f75faSAuke Kok }
1756bc7f75faSAuke Kok 
1757bc7f75faSAuke Kok /**
1758bc7f75faSAuke Kok  *  e1000e_update_adaptive - Update Adaptive Interframe Spacing
1759bc7f75faSAuke Kok  *  @hw: pointer to the HW structure
1760bc7f75faSAuke Kok  *
1761bc7f75faSAuke Kok  *  Update the Adaptive Interframe Spacing Throttle value based on the
1762bc7f75faSAuke Kok  *  time between transmitted packets and time between collisions.
1763bc7f75faSAuke Kok  **/
e1000e_update_adaptive(struct e1000_hw * hw)1764bc7f75faSAuke Kok void e1000e_update_adaptive(struct e1000_hw *hw)
1765bc7f75faSAuke Kok {
1766bc7f75faSAuke Kok 	struct e1000_mac_info *mac = &hw->mac;
1767bc7f75faSAuke Kok 
1768f464ba87SBruce Allan 	if (!mac->adaptive_ifs) {
1769f464ba87SBruce Allan 		e_dbg("Not in Adaptive IFS mode!\n");
1770fe1e980fSBruce Allan 		return;
1771f464ba87SBruce Allan 	}
1772f464ba87SBruce Allan 
1773bc7f75faSAuke Kok 	if ((mac->collision_delta * mac->ifs_ratio) > mac->tx_packet_delta) {
1774bc7f75faSAuke Kok 		if (mac->tx_packet_delta > MIN_NUM_XMITS) {
1775564ea9bbSBruce Allan 			mac->in_ifs_mode = true;
1776bc7f75faSAuke Kok 			if (mac->current_ifs_val < mac->ifs_max_val) {
1777bc7f75faSAuke Kok 				if (!mac->current_ifs_val)
1778bc7f75faSAuke Kok 					mac->current_ifs_val = mac->ifs_min_val;
1779bc7f75faSAuke Kok 				else
1780bc7f75faSAuke Kok 					mac->current_ifs_val +=
1781bc7f75faSAuke Kok 					    mac->ifs_step_size;
1782ad68076eSBruce Allan 				ew32(AIT, mac->current_ifs_val);
1783bc7f75faSAuke Kok 			}
1784bc7f75faSAuke Kok 		}
1785bc7f75faSAuke Kok 	} else {
1786bc7f75faSAuke Kok 		if (mac->in_ifs_mode &&
1787bc7f75faSAuke Kok 		    (mac->tx_packet_delta <= MIN_NUM_XMITS)) {
1788bc7f75faSAuke Kok 			mac->current_ifs_val = 0;
1789564ea9bbSBruce Allan 			mac->in_ifs_mode = false;
1790bc7f75faSAuke Kok 			ew32(AIT, 0);
1791bc7f75faSAuke Kok 		}
1792bc7f75faSAuke Kok 	}
1793bc7f75faSAuke Kok }
1794