xref: /linux/drivers/net/ethernet/intel/e1000e/netdev.c (revision 1d227fcc72223cbdd34d0ce13541cbaab5e0d72f)
1ae06c70bSJeff Kirsher // SPDX-License-Identifier: GPL-2.0
251dce24bSJeff Kirsher /* Copyright(c) 1999 - 2018 Intel Corporation. */
3dee1ad47SJeff Kirsher 
4dee1ad47SJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5dee1ad47SJeff Kirsher 
6dee1ad47SJeff Kirsher #include <linux/module.h>
7dee1ad47SJeff Kirsher #include <linux/types.h>
8dee1ad47SJeff Kirsher #include <linux/init.h>
9dee1ad47SJeff Kirsher #include <linux/pci.h>
10dee1ad47SJeff Kirsher #include <linux/vmalloc.h>
11dee1ad47SJeff Kirsher #include <linux/pagemap.h>
12dee1ad47SJeff Kirsher #include <linux/delay.h>
13dee1ad47SJeff Kirsher #include <linux/netdevice.h>
14dee1ad47SJeff Kirsher #include <linux/interrupt.h>
15dee1ad47SJeff Kirsher #include <linux/tcp.h>
16dee1ad47SJeff Kirsher #include <linux/ipv6.h>
17dee1ad47SJeff Kirsher #include <linux/slab.h>
18dee1ad47SJeff Kirsher #include <net/checksum.h>
19dee1ad47SJeff Kirsher #include <net/ip6_checksum.h>
20dee1ad47SJeff Kirsher #include <linux/ethtool.h>
21dee1ad47SJeff Kirsher #include <linux/if_vlan.h>
22dee1ad47SJeff Kirsher #include <linux/cpu.h>
23dee1ad47SJeff Kirsher #include <linux/smp.h>
247e0bb71eSLinus Torvalds #include <linux/pm_qos.h>
25dee1ad47SJeff Kirsher #include <linux/pm_runtime.h>
26dee1ad47SJeff Kirsher #include <linux/prefetch.h>
27ccf8b940SChen Yu #include <linux/suspend.h>
28dee1ad47SJeff Kirsher 
29dee1ad47SJeff Kirsher #include "e1000.h"
307bab8828SSasha Neftin #define CREATE_TRACE_POINTS
317bab8828SSasha Neftin #include "e1000e_trace.h"
32dee1ad47SJeff Kirsher 
33dee1ad47SJeff Kirsher char e1000e_driver_name[] = "e1000e";
34dee1ad47SJeff Kirsher 
35b3f4d599Sstephen hemminger #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
36b3f4d599Sstephen hemminger static int debug = -1;
37b3f4d599Sstephen hemminger module_param(debug, int, 0);
38b3f4d599Sstephen hemminger MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
39b3f4d599Sstephen hemminger 
40dee1ad47SJeff Kirsher static const struct e1000_info *e1000_info_tbl[] = {
41dee1ad47SJeff Kirsher 	[board_82571]		= &e1000_82571_info,
42dee1ad47SJeff Kirsher 	[board_82572]		= &e1000_82572_info,
43dee1ad47SJeff Kirsher 	[board_82573]		= &e1000_82573_info,
44dee1ad47SJeff Kirsher 	[board_82574]		= &e1000_82574_info,
45dee1ad47SJeff Kirsher 	[board_82583]		= &e1000_82583_info,
46dee1ad47SJeff Kirsher 	[board_80003es2lan]	= &e1000_es2_info,
47dee1ad47SJeff Kirsher 	[board_ich8lan]		= &e1000_ich8_info,
48dee1ad47SJeff Kirsher 	[board_ich9lan]		= &e1000_ich9_info,
49dee1ad47SJeff Kirsher 	[board_ich10lan]	= &e1000_ich10_info,
50dee1ad47SJeff Kirsher 	[board_pchlan]		= &e1000_pch_info,
51dee1ad47SJeff Kirsher 	[board_pch2lan]		= &e1000_pch2_info,
522fbe4526SBruce Allan 	[board_pch_lpt]		= &e1000_pch_lpt_info,
5379849ebcSDavid Ertman 	[board_pch_spt]		= &e1000_pch_spt_info,
543a3173b9SSasha Neftin 	[board_pch_cnp]		= &e1000_pch_cnp_info,
55280db5d4SSasha Neftin 	[board_pch_tgp]		= &e1000_pch_tgp_info,
5668defd52SSasha Neftin 	[board_pch_adp]		= &e1000_pch_adp_info,
57db2d737dSSasha Neftin 	[board_pch_mtp]		= &e1000_pch_mtp_info,
58dee1ad47SJeff Kirsher };
59dee1ad47SJeff Kirsher 
60dee1ad47SJeff Kirsher struct e1000_reg_info {
61dee1ad47SJeff Kirsher 	u32 ofs;
62dee1ad47SJeff Kirsher 	char *name;
63dee1ad47SJeff Kirsher };
64dee1ad47SJeff Kirsher 
65dee1ad47SJeff Kirsher static const struct e1000_reg_info e1000_reg_info_tbl[] = {
66dee1ad47SJeff Kirsher 	/* General Registers */
67dee1ad47SJeff Kirsher 	{E1000_CTRL, "CTRL"},
68dee1ad47SJeff Kirsher 	{E1000_STATUS, "STATUS"},
69dee1ad47SJeff Kirsher 	{E1000_CTRL_EXT, "CTRL_EXT"},
70dee1ad47SJeff Kirsher 
71dee1ad47SJeff Kirsher 	/* Interrupt Registers */
72dee1ad47SJeff Kirsher 	{E1000_ICR, "ICR"},
73dee1ad47SJeff Kirsher 
74dee1ad47SJeff Kirsher 	/* Rx Registers */
75dee1ad47SJeff Kirsher 	{E1000_RCTL, "RCTL"},
761e36052eSBruce Allan 	{E1000_RDLEN(0), "RDLEN"},
771e36052eSBruce Allan 	{E1000_RDH(0), "RDH"},
781e36052eSBruce Allan 	{E1000_RDT(0), "RDT"},
79dee1ad47SJeff Kirsher 	{E1000_RDTR, "RDTR"},
80dee1ad47SJeff Kirsher 	{E1000_RXDCTL(0), "RXDCTL"},
81dee1ad47SJeff Kirsher 	{E1000_ERT, "ERT"},
821e36052eSBruce Allan 	{E1000_RDBAL(0), "RDBAL"},
831e36052eSBruce Allan 	{E1000_RDBAH(0), "RDBAH"},
84dee1ad47SJeff Kirsher 	{E1000_RDFH, "RDFH"},
85dee1ad47SJeff Kirsher 	{E1000_RDFT, "RDFT"},
86dee1ad47SJeff Kirsher 	{E1000_RDFHS, "RDFHS"},
87dee1ad47SJeff Kirsher 	{E1000_RDFTS, "RDFTS"},
88dee1ad47SJeff Kirsher 	{E1000_RDFPC, "RDFPC"},
89dee1ad47SJeff Kirsher 
90dee1ad47SJeff Kirsher 	/* Tx Registers */
91dee1ad47SJeff Kirsher 	{E1000_TCTL, "TCTL"},
921e36052eSBruce Allan 	{E1000_TDBAL(0), "TDBAL"},
931e36052eSBruce Allan 	{E1000_TDBAH(0), "TDBAH"},
941e36052eSBruce Allan 	{E1000_TDLEN(0), "TDLEN"},
951e36052eSBruce Allan 	{E1000_TDH(0), "TDH"},
961e36052eSBruce Allan 	{E1000_TDT(0), "TDT"},
97dee1ad47SJeff Kirsher 	{E1000_TIDV, "TIDV"},
98dee1ad47SJeff Kirsher 	{E1000_TXDCTL(0), "TXDCTL"},
99dee1ad47SJeff Kirsher 	{E1000_TADV, "TADV"},
100dee1ad47SJeff Kirsher 	{E1000_TARC(0), "TARC"},
101dee1ad47SJeff Kirsher 	{E1000_TDFH, "TDFH"},
102dee1ad47SJeff Kirsher 	{E1000_TDFT, "TDFT"},
103dee1ad47SJeff Kirsher 	{E1000_TDFHS, "TDFHS"},
104dee1ad47SJeff Kirsher 	{E1000_TDFTS, "TDFTS"},
105dee1ad47SJeff Kirsher 	{E1000_TDFPC, "TDFPC"},
106dee1ad47SJeff Kirsher 
107dee1ad47SJeff Kirsher 	/* List Terminator */
108f36bb6caSBruce Allan 	{0, NULL}
109dee1ad47SJeff Kirsher };
110dee1ad47SJeff Kirsher 
111e921eb1aSBruce Allan /**
112c6f3148cSAndi Kleen  * __ew32_prepare - prepare to write to MAC CSR register on certain parts
113c6f3148cSAndi Kleen  * @hw: pointer to the HW structure
114c6f3148cSAndi Kleen  *
115c6f3148cSAndi Kleen  * When updating the MAC CSR registers, the Manageability Engine (ME) could
116c6f3148cSAndi Kleen  * be accessing the registers at the same time.  Normally, this is handled in
117c6f3148cSAndi Kleen  * h/w by an arbiter but on some parts there is a bug that acknowledges Host
118c6f3148cSAndi Kleen  * accesses later than it should which could result in the register to have
119c6f3148cSAndi Kleen  * an incorrect value.  Workaround this by checking the FWSM register which
120c6f3148cSAndi Kleen  * has bit 24 set while ME is accessing MAC CSR registers, wait if it is set
121c6f3148cSAndi Kleen  * and try again a number of times.
122c6f3148cSAndi Kleen  **/
__ew32_prepare(struct e1000_hw * hw)123d601afcaSPunit Agrawal static void __ew32_prepare(struct e1000_hw *hw)
124c6f3148cSAndi Kleen {
125c6f3148cSAndi Kleen 	s32 i = E1000_ICH_FWSM_PCIM2PCI_COUNT;
126c6f3148cSAndi Kleen 
127c6f3148cSAndi Kleen 	while ((er32(FWSM) & E1000_ICH_FWSM_PCIM2PCI) && --i)
128c6f3148cSAndi Kleen 		udelay(50);
129c6f3148cSAndi Kleen }
130c6f3148cSAndi Kleen 
__ew32(struct e1000_hw * hw,unsigned long reg,u32 val)131c6f3148cSAndi Kleen void __ew32(struct e1000_hw *hw, unsigned long reg, u32 val)
132c6f3148cSAndi Kleen {
133c6f3148cSAndi Kleen 	if (hw->adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
134c6f3148cSAndi Kleen 		__ew32_prepare(hw);
135c6f3148cSAndi Kleen 
136c6f3148cSAndi Kleen 	writel(val, hw->hw_addr + reg);
137c6f3148cSAndi Kleen }
138c6f3148cSAndi Kleen 
139c6f3148cSAndi Kleen /**
140dee1ad47SJeff Kirsher  * e1000_regdump - register printout routine
141e921eb1aSBruce Allan  * @hw: pointer to the HW structure
142e921eb1aSBruce Allan  * @reginfo: pointer to the register info table
143e921eb1aSBruce Allan  **/
e1000_regdump(struct e1000_hw * hw,struct e1000_reg_info * reginfo)144dee1ad47SJeff Kirsher static void e1000_regdump(struct e1000_hw *hw, struct e1000_reg_info *reginfo)
145dee1ad47SJeff Kirsher {
146dee1ad47SJeff Kirsher 	int n = 0;
147dee1ad47SJeff Kirsher 	char rname[16];
148dee1ad47SJeff Kirsher 	u32 regs[8];
149dee1ad47SJeff Kirsher 
150dee1ad47SJeff Kirsher 	switch (reginfo->ofs) {
151dee1ad47SJeff Kirsher 	case E1000_RXDCTL(0):
152dee1ad47SJeff Kirsher 		for (n = 0; n < 2; n++)
153dee1ad47SJeff Kirsher 			regs[n] = __er32(hw, E1000_RXDCTL(n));
154dee1ad47SJeff Kirsher 		break;
155dee1ad47SJeff Kirsher 	case E1000_TXDCTL(0):
156dee1ad47SJeff Kirsher 		for (n = 0; n < 2; n++)
157dee1ad47SJeff Kirsher 			regs[n] = __er32(hw, E1000_TXDCTL(n));
158dee1ad47SJeff Kirsher 		break;
159dee1ad47SJeff Kirsher 	case E1000_TARC(0):
160dee1ad47SJeff Kirsher 		for (n = 0; n < 2; n++)
161dee1ad47SJeff Kirsher 			regs[n] = __er32(hw, E1000_TARC(n));
162dee1ad47SJeff Kirsher 		break;
163dee1ad47SJeff Kirsher 	default:
164ef456f85SJeff Kirsher 		pr_info("%-15s %08x\n",
165dee1ad47SJeff Kirsher 			reginfo->name, __er32(hw, reginfo->ofs));
166dee1ad47SJeff Kirsher 		return;
167dee1ad47SJeff Kirsher 	}
168dee1ad47SJeff Kirsher 
169dee1ad47SJeff Kirsher 	snprintf(rname, 16, "%s%s", reginfo->name, "[0-1]");
170ef456f85SJeff Kirsher 	pr_info("%-15s %08x %08x\n", rname, regs[0], regs[1]);
171dee1ad47SJeff Kirsher }
172dee1ad47SJeff Kirsher 
e1000e_dump_ps_pages(struct e1000_adapter * adapter,struct e1000_buffer * bi)173f0c5dadfSEmil Tantilov static void e1000e_dump_ps_pages(struct e1000_adapter *adapter,
174f0c5dadfSEmil Tantilov 				 struct e1000_buffer *bi)
175f0c5dadfSEmil Tantilov {
176f0c5dadfSEmil Tantilov 	int i;
177f0c5dadfSEmil Tantilov 	struct e1000_ps_page *ps_page;
178f0c5dadfSEmil Tantilov 
179f0c5dadfSEmil Tantilov 	for (i = 0; i < adapter->rx_ps_pages; i++) {
180f0c5dadfSEmil Tantilov 		ps_page = &bi->ps_pages[i];
181f0c5dadfSEmil Tantilov 
182f0c5dadfSEmil Tantilov 		if (ps_page->page) {
183f0c5dadfSEmil Tantilov 			pr_info("packet dump for ps_page %d:\n", i);
184f0c5dadfSEmil Tantilov 			print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS,
185f0c5dadfSEmil Tantilov 				       16, 1, page_address(ps_page->page),
186f0c5dadfSEmil Tantilov 				       PAGE_SIZE, true);
187f0c5dadfSEmil Tantilov 		}
188f0c5dadfSEmil Tantilov 	}
189f0c5dadfSEmil Tantilov }
190f0c5dadfSEmil Tantilov 
191e921eb1aSBruce Allan /**
192dee1ad47SJeff Kirsher  * e1000e_dump - Print registers, Tx-ring and Rx-ring
193e921eb1aSBruce Allan  * @adapter: board private structure
194e921eb1aSBruce Allan  **/
e1000e_dump(struct e1000_adapter * adapter)195dee1ad47SJeff Kirsher static void e1000e_dump(struct e1000_adapter *adapter)
196dee1ad47SJeff Kirsher {
197dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
198dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
199dee1ad47SJeff Kirsher 	struct e1000_reg_info *reginfo;
200dee1ad47SJeff Kirsher 	struct e1000_ring *tx_ring = adapter->tx_ring;
201dee1ad47SJeff Kirsher 	struct e1000_tx_desc *tx_desc;
202dee1ad47SJeff Kirsher 	struct my_u0 {
203e885d762SBruce Allan 		__le64 a;
204e885d762SBruce Allan 		__le64 b;
205dee1ad47SJeff Kirsher 	} *u0;
206dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
207dee1ad47SJeff Kirsher 	struct e1000_ring *rx_ring = adapter->rx_ring;
208dee1ad47SJeff Kirsher 	union e1000_rx_desc_packet_split *rx_desc_ps;
2095f450212SBruce Allan 	union e1000_rx_desc_extended *rx_desc;
210dee1ad47SJeff Kirsher 	struct my_u1 {
211e885d762SBruce Allan 		__le64 a;
212e885d762SBruce Allan 		__le64 b;
213e885d762SBruce Allan 		__le64 c;
214e885d762SBruce Allan 		__le64 d;
215dee1ad47SJeff Kirsher 	} *u1;
216dee1ad47SJeff Kirsher 	u32 staterr;
217dee1ad47SJeff Kirsher 	int i = 0;
218dee1ad47SJeff Kirsher 
219dee1ad47SJeff Kirsher 	if (!netif_msg_hw(adapter))
220dee1ad47SJeff Kirsher 		return;
221dee1ad47SJeff Kirsher 
222dee1ad47SJeff Kirsher 	/* Print netdevice Info */
223dee1ad47SJeff Kirsher 	if (netdev) {
224dee1ad47SJeff Kirsher 		dev_info(&adapter->pdev->dev, "Net device Info\n");
2254a7c9726STobias Klauser 		pr_info("Device Name     state            trans_start\n");
2264a7c9726STobias Klauser 		pr_info("%-15s %016lX %016lX\n", netdev->name,
2274a7c9726STobias Klauser 			netdev->state, dev_trans_start(netdev));
228dee1ad47SJeff Kirsher 	}
229dee1ad47SJeff Kirsher 
230dee1ad47SJeff Kirsher 	/* Print Registers */
231dee1ad47SJeff Kirsher 	dev_info(&adapter->pdev->dev, "Register Dump\n");
232ef456f85SJeff Kirsher 	pr_info(" Register Name   Value\n");
233dee1ad47SJeff Kirsher 	for (reginfo = (struct e1000_reg_info *)e1000_reg_info_tbl;
234dee1ad47SJeff Kirsher 	     reginfo->name; reginfo++) {
235dee1ad47SJeff Kirsher 		e1000_regdump(hw, reginfo);
236dee1ad47SJeff Kirsher 	}
237dee1ad47SJeff Kirsher 
238dee1ad47SJeff Kirsher 	/* Print Tx Ring Summary */
239dee1ad47SJeff Kirsher 	if (!netdev || !netif_running(netdev))
240fe1e980fSBruce Allan 		return;
241dee1ad47SJeff Kirsher 
242dee1ad47SJeff Kirsher 	dev_info(&adapter->pdev->dev, "Tx Ring Summary\n");
243ef456f85SJeff Kirsher 	pr_info("Queue [NTU] [NTC] [bi(ntc)->dma  ] leng ntw timestamp\n");
244dee1ad47SJeff Kirsher 	buffer_info = &tx_ring->buffer_info[tx_ring->next_to_clean];
245ef456f85SJeff Kirsher 	pr_info(" %5d %5X %5X %016llX %04X %3X %016llX\n",
246dee1ad47SJeff Kirsher 		0, tx_ring->next_to_use, tx_ring->next_to_clean,
247dee1ad47SJeff Kirsher 		(unsigned long long)buffer_info->dma,
248dee1ad47SJeff Kirsher 		buffer_info->length,
249dee1ad47SJeff Kirsher 		buffer_info->next_to_watch,
250dee1ad47SJeff Kirsher 		(unsigned long long)buffer_info->time_stamp);
251dee1ad47SJeff Kirsher 
252dee1ad47SJeff Kirsher 	/* Print Tx Ring */
253dee1ad47SJeff Kirsher 	if (!netif_msg_tx_done(adapter))
254dee1ad47SJeff Kirsher 		goto rx_ring_summary;
255dee1ad47SJeff Kirsher 
256dee1ad47SJeff Kirsher 	dev_info(&adapter->pdev->dev, "Tx Ring Dump\n");
257dee1ad47SJeff Kirsher 
258dee1ad47SJeff Kirsher 	/* Transmit Descriptor Formats - DEXT[29] is 0 (Legacy) or 1 (Extended)
259dee1ad47SJeff Kirsher 	 *
260dee1ad47SJeff Kirsher 	 * Legacy Transmit Descriptor
261dee1ad47SJeff Kirsher 	 *   +--------------------------------------------------------------+
262dee1ad47SJeff Kirsher 	 * 0 |         Buffer Address [63:0] (Reserved on Write Back)       |
263dee1ad47SJeff Kirsher 	 *   +--------------------------------------------------------------+
264dee1ad47SJeff Kirsher 	 * 8 | Special  |    CSS     | Status |  CMD    |  CSO   |  Length  |
265dee1ad47SJeff Kirsher 	 *   +--------------------------------------------------------------+
266dee1ad47SJeff Kirsher 	 *   63       48 47        36 35    32 31     24 23    16 15        0
267dee1ad47SJeff Kirsher 	 *
268dee1ad47SJeff Kirsher 	 * Extended Context Descriptor (DTYP=0x0) for TSO or checksum offload
269dee1ad47SJeff Kirsher 	 *   63      48 47    40 39       32 31             16 15    8 7      0
270dee1ad47SJeff Kirsher 	 *   +----------------------------------------------------------------+
271dee1ad47SJeff Kirsher 	 * 0 |  TUCSE  | TUCS0  |   TUCSS   |     IPCSE       | IPCS0 | IPCSS |
272dee1ad47SJeff Kirsher 	 *   +----------------------------------------------------------------+
273dee1ad47SJeff Kirsher 	 * 8 |   MSS   | HDRLEN | RSV | STA | TUCMD | DTYP |      PAYLEN      |
274dee1ad47SJeff Kirsher 	 *   +----------------------------------------------------------------+
275dee1ad47SJeff Kirsher 	 *   63      48 47    40 39 36 35 32 31   24 23  20 19                0
276dee1ad47SJeff Kirsher 	 *
277dee1ad47SJeff Kirsher 	 * Extended Data Descriptor (DTYP=0x1)
278dee1ad47SJeff Kirsher 	 *   +----------------------------------------------------------------+
279dee1ad47SJeff Kirsher 	 * 0 |                     Buffer Address [63:0]                      |
280dee1ad47SJeff Kirsher 	 *   +----------------------------------------------------------------+
281dee1ad47SJeff Kirsher 	 * 8 | VLAN tag |  POPTS  | Rsvd | Status | Command | DTYP |  DTALEN  |
282dee1ad47SJeff Kirsher 	 *   +----------------------------------------------------------------+
283dee1ad47SJeff Kirsher 	 *   63       48 47     40 39  36 35    32 31     24 23  20 19        0
284dee1ad47SJeff Kirsher 	 */
285ef456f85SJeff Kirsher 	pr_info("Tl[desc]     [address 63:0  ] [SpeCssSCmCsLen] [bi->dma       ] leng  ntw timestamp        bi->skb <-- Legacy format\n");
286ef456f85SJeff Kirsher 	pr_info("Tc[desc]     [Ce CoCsIpceCoS] [MssHlRSCm0Plen] [bi->dma       ] leng  ntw timestamp        bi->skb <-- Ext Context format\n");
287ef456f85SJeff Kirsher 	pr_info("Td[desc]     [address 63:0  ] [VlaPoRSCm1Dlen] [bi->dma       ] leng  ntw timestamp        bi->skb <-- Ext Data format\n");
288dee1ad47SJeff Kirsher 	for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
289ef456f85SJeff Kirsher 		const char *next_desc;
290dee1ad47SJeff Kirsher 		tx_desc = E1000_TX_DESC(*tx_ring, i);
291dee1ad47SJeff Kirsher 		buffer_info = &tx_ring->buffer_info[i];
292dee1ad47SJeff Kirsher 		u0 = (struct my_u0 *)tx_desc;
293ef456f85SJeff Kirsher 		if (i == tx_ring->next_to_use && i == tx_ring->next_to_clean)
294ef456f85SJeff Kirsher 			next_desc = " NTC/U";
295ef456f85SJeff Kirsher 		else if (i == tx_ring->next_to_use)
296ef456f85SJeff Kirsher 			next_desc = " NTU";
297ef456f85SJeff Kirsher 		else if (i == tx_ring->next_to_clean)
298ef456f85SJeff Kirsher 			next_desc = " NTC";
299ef456f85SJeff Kirsher 		else
300ef456f85SJeff Kirsher 			next_desc = "";
301ef456f85SJeff Kirsher 		pr_info("T%c[0x%03X]    %016llX %016llX %016llX %04X  %3X %016llX %p%s\n",
30218dd2392SJacob Keller 			(!(le64_to_cpu(u0->b) & BIT(29)) ? 'l' :
30318dd2392SJacob Keller 			 ((le64_to_cpu(u0->b) & BIT(20)) ? 'd' : 'c')),
304ef456f85SJeff Kirsher 			i,
305dee1ad47SJeff Kirsher 			(unsigned long long)le64_to_cpu(u0->a),
306dee1ad47SJeff Kirsher 			(unsigned long long)le64_to_cpu(u0->b),
307dee1ad47SJeff Kirsher 			(unsigned long long)buffer_info->dma,
308dee1ad47SJeff Kirsher 			buffer_info->length, buffer_info->next_to_watch,
309dee1ad47SJeff Kirsher 			(unsigned long long)buffer_info->time_stamp,
310ef456f85SJeff Kirsher 			buffer_info->skb, next_desc);
311dee1ad47SJeff Kirsher 
312f0c5dadfSEmil Tantilov 		if (netif_msg_pktdata(adapter) && buffer_info->skb)
313dee1ad47SJeff Kirsher 			print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS,
314f0c5dadfSEmil Tantilov 				       16, 1, buffer_info->skb->data,
315f0c5dadfSEmil Tantilov 				       buffer_info->skb->len, true);
316dee1ad47SJeff Kirsher 	}
317dee1ad47SJeff Kirsher 
318dee1ad47SJeff Kirsher 	/* Print Rx Ring Summary */
319dee1ad47SJeff Kirsher rx_ring_summary:
320dee1ad47SJeff Kirsher 	dev_info(&adapter->pdev->dev, "Rx Ring Summary\n");
321ef456f85SJeff Kirsher 	pr_info("Queue [NTU] [NTC]\n");
322ef456f85SJeff Kirsher 	pr_info(" %5d %5X %5X\n",
323ef456f85SJeff Kirsher 		0, rx_ring->next_to_use, rx_ring->next_to_clean);
324dee1ad47SJeff Kirsher 
325dee1ad47SJeff Kirsher 	/* Print Rx Ring */
326dee1ad47SJeff Kirsher 	if (!netif_msg_rx_status(adapter))
327fe1e980fSBruce Allan 		return;
328dee1ad47SJeff Kirsher 
329dee1ad47SJeff Kirsher 	dev_info(&adapter->pdev->dev, "Rx Ring Dump\n");
330dee1ad47SJeff Kirsher 	switch (adapter->rx_ps_pages) {
331dee1ad47SJeff Kirsher 	case 1:
332dee1ad47SJeff Kirsher 	case 2:
333dee1ad47SJeff Kirsher 	case 3:
334dee1ad47SJeff Kirsher 		/* [Extended] Packet Split Receive Descriptor Format
335dee1ad47SJeff Kirsher 		 *
336dee1ad47SJeff Kirsher 		 *    +-----------------------------------------------------+
337dee1ad47SJeff Kirsher 		 *  0 |                Buffer Address 0 [63:0]              |
338dee1ad47SJeff Kirsher 		 *    +-----------------------------------------------------+
339dee1ad47SJeff Kirsher 		 *  8 |                Buffer Address 1 [63:0]              |
340dee1ad47SJeff Kirsher 		 *    +-----------------------------------------------------+
341dee1ad47SJeff Kirsher 		 * 16 |                Buffer Address 2 [63:0]              |
342dee1ad47SJeff Kirsher 		 *    +-----------------------------------------------------+
343dee1ad47SJeff Kirsher 		 * 24 |                Buffer Address 3 [63:0]              |
344dee1ad47SJeff Kirsher 		 *    +-----------------------------------------------------+
345dee1ad47SJeff Kirsher 		 */
346ef456f85SJeff Kirsher 		pr_info("R  [desc]      [buffer 0 63:0 ] [buffer 1 63:0 ] [buffer 2 63:0 ] [buffer 3 63:0 ] [bi->dma       ] [bi->skb] <-- Ext Pkt Split format\n");
347dee1ad47SJeff Kirsher 		/* [Extended] Receive Descriptor (Write-Back) Format
348dee1ad47SJeff Kirsher 		 *
349dee1ad47SJeff Kirsher 		 *   63       48 47    32 31     13 12    8 7    4 3        0
350dee1ad47SJeff Kirsher 		 *   +------------------------------------------------------+
351dee1ad47SJeff Kirsher 		 * 0 | Packet   | IP     |  Rsvd   | MRQ   | Rsvd | MRQ RSS |
352dee1ad47SJeff Kirsher 		 *   | Checksum | Ident  |         | Queue |      |  Type   |
353dee1ad47SJeff Kirsher 		 *   +------------------------------------------------------+
354dee1ad47SJeff Kirsher 		 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
355dee1ad47SJeff Kirsher 		 *   +------------------------------------------------------+
356dee1ad47SJeff Kirsher 		 *   63       48 47    32 31            20 19               0
357dee1ad47SJeff Kirsher 		 */
358ef456f85SJeff Kirsher 		pr_info("RWB[desc]      [ck ipid mrqhsh] [vl   l0 ee  es] [ l3  l2  l1 hs] [reserved      ] ---------------- [bi->skb] <-- Ext Rx Write-Back format\n");
359dee1ad47SJeff Kirsher 		for (i = 0; i < rx_ring->count; i++) {
360ef456f85SJeff Kirsher 			const char *next_desc;
361dee1ad47SJeff Kirsher 			buffer_info = &rx_ring->buffer_info[i];
362dee1ad47SJeff Kirsher 			rx_desc_ps = E1000_RX_DESC_PS(*rx_ring, i);
363dee1ad47SJeff Kirsher 			u1 = (struct my_u1 *)rx_desc_ps;
364dee1ad47SJeff Kirsher 			staterr =
365dee1ad47SJeff Kirsher 			    le32_to_cpu(rx_desc_ps->wb.middle.status_error);
366ef456f85SJeff Kirsher 
367ef456f85SJeff Kirsher 			if (i == rx_ring->next_to_use)
368ef456f85SJeff Kirsher 				next_desc = " NTU";
369ef456f85SJeff Kirsher 			else if (i == rx_ring->next_to_clean)
370ef456f85SJeff Kirsher 				next_desc = " NTC";
371ef456f85SJeff Kirsher 			else
372ef456f85SJeff Kirsher 				next_desc = "";
373ef456f85SJeff Kirsher 
374dee1ad47SJeff Kirsher 			if (staterr & E1000_RXD_STAT_DD) {
375dee1ad47SJeff Kirsher 				/* Descriptor Done */
376ef456f85SJeff Kirsher 				pr_info("%s[0x%03X]     %016llX %016llX %016llX %016llX ---------------- %p%s\n",
377ef456f85SJeff Kirsher 					"RWB", i,
378dee1ad47SJeff Kirsher 					(unsigned long long)le64_to_cpu(u1->a),
379dee1ad47SJeff Kirsher 					(unsigned long long)le64_to_cpu(u1->b),
380dee1ad47SJeff Kirsher 					(unsigned long long)le64_to_cpu(u1->c),
381dee1ad47SJeff Kirsher 					(unsigned long long)le64_to_cpu(u1->d),
382ef456f85SJeff Kirsher 					buffer_info->skb, next_desc);
383dee1ad47SJeff Kirsher 			} else {
384ef456f85SJeff Kirsher 				pr_info("%s[0x%03X]     %016llX %016llX %016llX %016llX %016llX %p%s\n",
385ef456f85SJeff Kirsher 					"R  ", i,
386dee1ad47SJeff Kirsher 					(unsigned long long)le64_to_cpu(u1->a),
387dee1ad47SJeff Kirsher 					(unsigned long long)le64_to_cpu(u1->b),
388dee1ad47SJeff Kirsher 					(unsigned long long)le64_to_cpu(u1->c),
389dee1ad47SJeff Kirsher 					(unsigned long long)le64_to_cpu(u1->d),
390dee1ad47SJeff Kirsher 					(unsigned long long)buffer_info->dma,
391ef456f85SJeff Kirsher 					buffer_info->skb, next_desc);
392dee1ad47SJeff Kirsher 
393dee1ad47SJeff Kirsher 				if (netif_msg_pktdata(adapter))
394f0c5dadfSEmil Tantilov 					e1000e_dump_ps_pages(adapter,
395f0c5dadfSEmil Tantilov 							     buffer_info);
396dee1ad47SJeff Kirsher 			}
397dee1ad47SJeff Kirsher 		}
398dee1ad47SJeff Kirsher 		break;
399dee1ad47SJeff Kirsher 	default:
400dee1ad47SJeff Kirsher 	case 0:
4015f450212SBruce Allan 		/* Extended Receive Descriptor (Read) Format
402dee1ad47SJeff Kirsher 		 *
403dee1ad47SJeff Kirsher 		 *   +-----------------------------------------------------+
4045f450212SBruce Allan 		 * 0 |                Buffer Address [63:0]                |
405dee1ad47SJeff Kirsher 		 *   +-----------------------------------------------------+
4065f450212SBruce Allan 		 * 8 |                      Reserved                       |
407dee1ad47SJeff Kirsher 		 *   +-----------------------------------------------------+
408dee1ad47SJeff Kirsher 		 */
409ef456f85SJeff Kirsher 		pr_info("R  [desc]      [buf addr 63:0 ] [reserved 63:0 ] [bi->dma       ] [bi->skb] <-- Ext (Read) format\n");
4105f450212SBruce Allan 		/* Extended Receive Descriptor (Write-Back) Format
4115f450212SBruce Allan 		 *
4125f450212SBruce Allan 		 *   63       48 47    32 31    24 23            4 3        0
4135f450212SBruce Allan 		 *   +------------------------------------------------------+
4145f450212SBruce Allan 		 *   |     RSS Hash      |        |               |         |
4155f450212SBruce Allan 		 * 0 +-------------------+  Rsvd  |   Reserved    | MRQ RSS |
4165f450212SBruce Allan 		 *   | Packet   | IP     |        |               |  Type   |
4175f450212SBruce Allan 		 *   | Checksum | Ident  |        |               |         |
4185f450212SBruce Allan 		 *   +------------------------------------------------------+
4195f450212SBruce Allan 		 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
4205f450212SBruce Allan 		 *   +------------------------------------------------------+
4215f450212SBruce Allan 		 *   63       48 47    32 31            20 19               0
4225f450212SBruce Allan 		 */
423ef456f85SJeff Kirsher 		pr_info("RWB[desc]      [cs ipid    mrq] [vt   ln xe  xs] [bi->skb] <-- Ext (Write-Back) format\n");
4245f450212SBruce Allan 
4255f450212SBruce Allan 		for (i = 0; i < rx_ring->count; i++) {
426ef456f85SJeff Kirsher 			const char *next_desc;
427ef456f85SJeff Kirsher 
428dee1ad47SJeff Kirsher 			buffer_info = &rx_ring->buffer_info[i];
4295f450212SBruce Allan 			rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
4305f450212SBruce Allan 			u1 = (struct my_u1 *)rx_desc;
4315f450212SBruce Allan 			staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
432ef456f85SJeff Kirsher 
433ef456f85SJeff Kirsher 			if (i == rx_ring->next_to_use)
434ef456f85SJeff Kirsher 				next_desc = " NTU";
435ef456f85SJeff Kirsher 			else if (i == rx_ring->next_to_clean)
436ef456f85SJeff Kirsher 				next_desc = " NTC";
437ef456f85SJeff Kirsher 			else
438ef456f85SJeff Kirsher 				next_desc = "";
439ef456f85SJeff Kirsher 
4405f450212SBruce Allan 			if (staterr & E1000_RXD_STAT_DD) {
4415f450212SBruce Allan 				/* Descriptor Done */
442ef456f85SJeff Kirsher 				pr_info("%s[0x%03X]     %016llX %016llX ---------------- %p%s\n",
443ef456f85SJeff Kirsher 					"RWB", i,
4445f450212SBruce Allan 					(unsigned long long)le64_to_cpu(u1->a),
4455f450212SBruce Allan 					(unsigned long long)le64_to_cpu(u1->b),
446ef456f85SJeff Kirsher 					buffer_info->skb, next_desc);
4475f450212SBruce Allan 			} else {
448ef456f85SJeff Kirsher 				pr_info("%s[0x%03X]     %016llX %016llX %016llX %p%s\n",
449ef456f85SJeff Kirsher 					"R  ", i,
4505f450212SBruce Allan 					(unsigned long long)le64_to_cpu(u1->a),
4515f450212SBruce Allan 					(unsigned long long)le64_to_cpu(u1->b),
452dee1ad47SJeff Kirsher 					(unsigned long long)buffer_info->dma,
453ef456f85SJeff Kirsher 					buffer_info->skb, next_desc);
4545f450212SBruce Allan 
455f0c5dadfSEmil Tantilov 				if (netif_msg_pktdata(adapter) &&
456f0c5dadfSEmil Tantilov 				    buffer_info->skb)
4575f450212SBruce Allan 					print_hex_dump(KERN_INFO, "",
4585f450212SBruce Allan 						       DUMP_PREFIX_ADDRESS, 16,
4595f450212SBruce Allan 						       1,
460f0c5dadfSEmil Tantilov 						       buffer_info->skb->data,
4615f450212SBruce Allan 						       adapter->rx_buffer_len,
4625f450212SBruce Allan 						       true);
4635f450212SBruce Allan 			}
464dee1ad47SJeff Kirsher 		}
465dee1ad47SJeff Kirsher 	}
466dee1ad47SJeff Kirsher }
467dee1ad47SJeff Kirsher 
468dee1ad47SJeff Kirsher /**
469dee1ad47SJeff Kirsher  * e1000_desc_unused - calculate if we have unused descriptors
470b50f7bcaSJesse Brandeburg  * @ring: pointer to ring struct to perform calculation on
471dee1ad47SJeff Kirsher  **/
e1000_desc_unused(struct e1000_ring * ring)472dee1ad47SJeff Kirsher static int e1000_desc_unused(struct e1000_ring *ring)
473dee1ad47SJeff Kirsher {
474dee1ad47SJeff Kirsher 	if (ring->next_to_clean > ring->next_to_use)
475dee1ad47SJeff Kirsher 		return ring->next_to_clean - ring->next_to_use - 1;
476dee1ad47SJeff Kirsher 
477dee1ad47SJeff Kirsher 	return ring->count + ring->next_to_clean - ring->next_to_use - 1;
478dee1ad47SJeff Kirsher }
479dee1ad47SJeff Kirsher 
480dee1ad47SJeff Kirsher /**
481b67e1913SBruce Allan  * e1000e_systim_to_hwtstamp - convert system time value to hw time stamp
482b67e1913SBruce Allan  * @adapter: board private structure
483b67e1913SBruce Allan  * @hwtstamps: time stamp structure to update
484b67e1913SBruce Allan  * @systim: unsigned 64bit system time value.
485b67e1913SBruce Allan  *
486b67e1913SBruce Allan  * Convert the system time value stored in the RX/TXSTMP registers into a
487b67e1913SBruce Allan  * hwtstamp which can be used by the upper level time stamping functions.
488b67e1913SBruce Allan  *
489b67e1913SBruce Allan  * The 'systim_lock' spinlock is used to protect the consistency of the
490b67e1913SBruce Allan  * system time value. This is needed because reading the 64 bit time
491b67e1913SBruce Allan  * value involves reading two 32 bit registers. The first read latches the
492b67e1913SBruce Allan  * value.
493b67e1913SBruce Allan  **/
e1000e_systim_to_hwtstamp(struct e1000_adapter * adapter,struct skb_shared_hwtstamps * hwtstamps,u64 systim)494b67e1913SBruce Allan static void e1000e_systim_to_hwtstamp(struct e1000_adapter *adapter,
495b67e1913SBruce Allan 				      struct skb_shared_hwtstamps *hwtstamps,
496b67e1913SBruce Allan 				      u64 systim)
497b67e1913SBruce Allan {
498b67e1913SBruce Allan 	u64 ns;
499b67e1913SBruce Allan 	unsigned long flags;
500b67e1913SBruce Allan 
501b67e1913SBruce Allan 	spin_lock_irqsave(&adapter->systim_lock, flags);
502b67e1913SBruce Allan 	ns = timecounter_cyc2time(&adapter->tc, systim);
503b67e1913SBruce Allan 	spin_unlock_irqrestore(&adapter->systim_lock, flags);
504b67e1913SBruce Allan 
505b67e1913SBruce Allan 	memset(hwtstamps, 0, sizeof(*hwtstamps));
506b67e1913SBruce Allan 	hwtstamps->hwtstamp = ns_to_ktime(ns);
507b67e1913SBruce Allan }
508b67e1913SBruce Allan 
509b67e1913SBruce Allan /**
510b67e1913SBruce Allan  * e1000e_rx_hwtstamp - utility function which checks for Rx time stamp
511b67e1913SBruce Allan  * @adapter: board private structure
512b67e1913SBruce Allan  * @status: descriptor extended error and status field
513b67e1913SBruce Allan  * @skb: particular skb to include time stamp
514b67e1913SBruce Allan  *
515b67e1913SBruce Allan  * If the time stamp is valid, convert it into the timecounter ns value
516b67e1913SBruce Allan  * and store that result into the shhwtstamps structure which is passed
517b67e1913SBruce Allan  * up the network stack.
518b67e1913SBruce Allan  **/
e1000e_rx_hwtstamp(struct e1000_adapter * adapter,u32 status,struct sk_buff * skb)519b67e1913SBruce Allan static void e1000e_rx_hwtstamp(struct e1000_adapter *adapter, u32 status,
520b67e1913SBruce Allan 			       struct sk_buff *skb)
521b67e1913SBruce Allan {
522b67e1913SBruce Allan 	struct e1000_hw *hw = &adapter->hw;
523b67e1913SBruce Allan 	u64 rxstmp;
524b67e1913SBruce Allan 
525b67e1913SBruce Allan 	if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP) ||
526b67e1913SBruce Allan 	    !(status & E1000_RXDEXT_STATERR_TST) ||
527b67e1913SBruce Allan 	    !(er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
528b67e1913SBruce Allan 		return;
529b67e1913SBruce Allan 
530b67e1913SBruce Allan 	/* The Rx time stamp registers contain the time stamp.  No other
531b67e1913SBruce Allan 	 * received packet will be time stamped until the Rx time stamp
532b67e1913SBruce Allan 	 * registers are read.  Because only one packet can be time stamped
533b67e1913SBruce Allan 	 * at a time, the register values must belong to this packet and
534b67e1913SBruce Allan 	 * therefore none of the other additional attributes need to be
535b67e1913SBruce Allan 	 * compared.
536b67e1913SBruce Allan 	 */
537b67e1913SBruce Allan 	rxstmp = (u64)er32(RXSTMPL);
538b67e1913SBruce Allan 	rxstmp |= (u64)er32(RXSTMPH) << 32;
539b67e1913SBruce Allan 	e1000e_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), rxstmp);
540b67e1913SBruce Allan 
541b67e1913SBruce Allan 	adapter->flags2 &= ~FLAG2_CHECK_RX_HWTSTAMP;
542b67e1913SBruce Allan }
543b67e1913SBruce Allan 
544b67e1913SBruce Allan /**
545dee1ad47SJeff Kirsher  * e1000_receive_skb - helper function to handle Rx indications
546dee1ad47SJeff Kirsher  * @adapter: board private structure
547b50f7bcaSJesse Brandeburg  * @netdev: pointer to netdev struct
548b67e1913SBruce Allan  * @staterr: descriptor extended error and status field as written by hardware
549dee1ad47SJeff Kirsher  * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
550dee1ad47SJeff Kirsher  * @skb: pointer to sk_buff to be indicated to stack
551dee1ad47SJeff Kirsher  **/
e1000_receive_skb(struct e1000_adapter * adapter,struct net_device * netdev,struct sk_buff * skb,u32 staterr,__le16 vlan)552dee1ad47SJeff Kirsher static void e1000_receive_skb(struct e1000_adapter *adapter,
553dee1ad47SJeff Kirsher 			      struct net_device *netdev, struct sk_buff *skb,
554b67e1913SBruce Allan 			      u32 staterr, __le16 vlan)
555dee1ad47SJeff Kirsher {
556dee1ad47SJeff Kirsher 	u16 tag = le16_to_cpu(vlan);
557b67e1913SBruce Allan 
558b67e1913SBruce Allan 	e1000e_rx_hwtstamp(adapter, staterr, skb);
559b67e1913SBruce Allan 
560dee1ad47SJeff Kirsher 	skb->protocol = eth_type_trans(skb, netdev);
561dee1ad47SJeff Kirsher 
562b67e1913SBruce Allan 	if (staterr & E1000_RXD_STAT_VP)
56386a9bad3SPatrick McHardy 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tag);
564dee1ad47SJeff Kirsher 
565dee1ad47SJeff Kirsher 	napi_gro_receive(&adapter->napi, skb);
566dee1ad47SJeff Kirsher }
567dee1ad47SJeff Kirsher 
568dee1ad47SJeff Kirsher /**
569dee1ad47SJeff Kirsher  * e1000_rx_checksum - Receive Checksum Offload
570dee1ad47SJeff Kirsher  * @adapter: board private structure
571dee1ad47SJeff Kirsher  * @status_err: receive descriptor status and error fields
572b50f7bcaSJesse Brandeburg  * @skb: socket buffer with received data
573dee1ad47SJeff Kirsher  **/
e1000_rx_checksum(struct e1000_adapter * adapter,u32 status_err,struct sk_buff * skb)574dee1ad47SJeff Kirsher static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
5752e1706f2SBruce Allan 			      struct sk_buff *skb)
576dee1ad47SJeff Kirsher {
577dee1ad47SJeff Kirsher 	u16 status = (u16)status_err;
578dee1ad47SJeff Kirsher 	u8 errors = (u8)(status_err >> 24);
579dee1ad47SJeff Kirsher 
580dee1ad47SJeff Kirsher 	skb_checksum_none_assert(skb);
581dee1ad47SJeff Kirsher 
582afd12939SBruce Allan 	/* Rx checksum disabled */
583afd12939SBruce Allan 	if (!(adapter->netdev->features & NETIF_F_RXCSUM))
584afd12939SBruce Allan 		return;
585afd12939SBruce Allan 
586dee1ad47SJeff Kirsher 	/* Ignore Checksum bit is set */
587dee1ad47SJeff Kirsher 	if (status & E1000_RXD_STAT_IXSM)
588dee1ad47SJeff Kirsher 		return;
589afd12939SBruce Allan 
5902e1706f2SBruce Allan 	/* TCP/UDP checksum error bit or IP checksum error bit is set */
5912e1706f2SBruce Allan 	if (errors & (E1000_RXD_ERR_TCPE | E1000_RXD_ERR_IPE)) {
592dee1ad47SJeff Kirsher 		/* let the stack verify checksum errors */
593dee1ad47SJeff Kirsher 		adapter->hw_csum_err++;
594dee1ad47SJeff Kirsher 		return;
595dee1ad47SJeff Kirsher 	}
596dee1ad47SJeff Kirsher 
597dee1ad47SJeff Kirsher 	/* TCP/UDP Checksum has not been calculated */
598dee1ad47SJeff Kirsher 	if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
599dee1ad47SJeff Kirsher 		return;
600dee1ad47SJeff Kirsher 
601dee1ad47SJeff Kirsher 	/* It must be a TCP or UDP packet with a valid checksum */
602dee1ad47SJeff Kirsher 	skb->ip_summed = CHECKSUM_UNNECESSARY;
603dee1ad47SJeff Kirsher 	adapter->hw_csum_good++;
604dee1ad47SJeff Kirsher }
605dee1ad47SJeff Kirsher 
e1000e_update_rdt_wa(struct e1000_ring * rx_ring,unsigned int i)60655aa6985SBruce Allan static void e1000e_update_rdt_wa(struct e1000_ring *rx_ring, unsigned int i)
607823dcd25SDavid S. Miller {
60855aa6985SBruce Allan 	struct e1000_adapter *adapter = rx_ring->adapter;
609823dcd25SDavid S. Miller 	struct e1000_hw *hw = &adapter->hw;
610823dcd25SDavid S. Miller 
611d601afcaSPunit Agrawal 	__ew32_prepare(hw);
612bdc125f7SBruce Allan 	writel(i, rx_ring->tail);
613bdc125f7SBruce Allan 
614d601afcaSPunit Agrawal 	if (unlikely(i != readl(rx_ring->tail))) {
615823dcd25SDavid S. Miller 		u32 rctl = er32(RCTL);
6166cf08d1cSDavid Ertman 
617823dcd25SDavid S. Miller 		ew32(RCTL, rctl & ~E1000_RCTL_EN);
618823dcd25SDavid S. Miller 		e_err("ME firmware caused invalid RDT - resetting\n");
619823dcd25SDavid S. Miller 		schedule_work(&adapter->reset_task);
620823dcd25SDavid S. Miller 	}
621823dcd25SDavid S. Miller }
622823dcd25SDavid S. Miller 
e1000e_update_tdt_wa(struct e1000_ring * tx_ring,unsigned int i)62355aa6985SBruce Allan static void e1000e_update_tdt_wa(struct e1000_ring *tx_ring, unsigned int i)
624823dcd25SDavid S. Miller {
62555aa6985SBruce Allan 	struct e1000_adapter *adapter = tx_ring->adapter;
626823dcd25SDavid S. Miller 	struct e1000_hw *hw = &adapter->hw;
627823dcd25SDavid S. Miller 
628d601afcaSPunit Agrawal 	__ew32_prepare(hw);
629bdc125f7SBruce Allan 	writel(i, tx_ring->tail);
630bdc125f7SBruce Allan 
631d601afcaSPunit Agrawal 	if (unlikely(i != readl(tx_ring->tail))) {
632823dcd25SDavid S. Miller 		u32 tctl = er32(TCTL);
6336cf08d1cSDavid Ertman 
634823dcd25SDavid S. Miller 		ew32(TCTL, tctl & ~E1000_TCTL_EN);
635823dcd25SDavid S. Miller 		e_err("ME firmware caused invalid TDT - resetting\n");
636823dcd25SDavid S. Miller 		schedule_work(&adapter->reset_task);
637823dcd25SDavid S. Miller 	}
638823dcd25SDavid S. Miller }
639823dcd25SDavid S. Miller 
640823dcd25SDavid S. Miller /**
6415f450212SBruce Allan  * e1000_alloc_rx_buffers - Replace used receive buffers
64255aa6985SBruce Allan  * @rx_ring: Rx descriptor ring
643b50f7bcaSJesse Brandeburg  * @cleaned_count: number to reallocate
644b50f7bcaSJesse Brandeburg  * @gfp: flags for allocation
645dee1ad47SJeff Kirsher  **/
e1000_alloc_rx_buffers(struct e1000_ring * rx_ring,int cleaned_count,gfp_t gfp)64655aa6985SBruce Allan static void e1000_alloc_rx_buffers(struct e1000_ring *rx_ring,
647dee1ad47SJeff Kirsher 				   int cleaned_count, gfp_t gfp)
648dee1ad47SJeff Kirsher {
64955aa6985SBruce Allan 	struct e1000_adapter *adapter = rx_ring->adapter;
650dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
651dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
6525f450212SBruce Allan 	union e1000_rx_desc_extended *rx_desc;
653dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
654dee1ad47SJeff Kirsher 	struct sk_buff *skb;
655dee1ad47SJeff Kirsher 	unsigned int i;
656dee1ad47SJeff Kirsher 	unsigned int bufsz = adapter->rx_buffer_len;
657dee1ad47SJeff Kirsher 
658dee1ad47SJeff Kirsher 	i = rx_ring->next_to_use;
659dee1ad47SJeff Kirsher 	buffer_info = &rx_ring->buffer_info[i];
660dee1ad47SJeff Kirsher 
661dee1ad47SJeff Kirsher 	while (cleaned_count--) {
662dee1ad47SJeff Kirsher 		skb = buffer_info->skb;
663dee1ad47SJeff Kirsher 		if (skb) {
664dee1ad47SJeff Kirsher 			skb_trim(skb, 0);
665dee1ad47SJeff Kirsher 			goto map_skb;
666dee1ad47SJeff Kirsher 		}
667dee1ad47SJeff Kirsher 
668dee1ad47SJeff Kirsher 		skb = __netdev_alloc_skb_ip_align(netdev, bufsz, gfp);
669dee1ad47SJeff Kirsher 		if (!skb) {
670dee1ad47SJeff Kirsher 			/* Better luck next round */
671dee1ad47SJeff Kirsher 			adapter->alloc_rx_buff_failed++;
672dee1ad47SJeff Kirsher 			break;
673dee1ad47SJeff Kirsher 		}
674dee1ad47SJeff Kirsher 
675dee1ad47SJeff Kirsher 		buffer_info->skb = skb;
676dee1ad47SJeff Kirsher map_skb:
677dee1ad47SJeff Kirsher 		buffer_info->dma = dma_map_single(&pdev->dev, skb->data,
678dee1ad47SJeff Kirsher 						  adapter->rx_buffer_len,
679dee1ad47SJeff Kirsher 						  DMA_FROM_DEVICE);
680dee1ad47SJeff Kirsher 		if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
681dee1ad47SJeff Kirsher 			dev_err(&pdev->dev, "Rx DMA map failed\n");
682dee1ad47SJeff Kirsher 			adapter->rx_dma_failed++;
683dee1ad47SJeff Kirsher 			break;
684dee1ad47SJeff Kirsher 		}
685dee1ad47SJeff Kirsher 
6865f450212SBruce Allan 		rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
6875f450212SBruce Allan 		rx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
688dee1ad47SJeff Kirsher 
689dee1ad47SJeff Kirsher 		if (unlikely(!(i & (E1000_RX_BUFFER_WRITE - 1)))) {
690e921eb1aSBruce Allan 			/* Force memory writes to complete before letting h/w
691dee1ad47SJeff Kirsher 			 * know there are new descriptors to fetch.  (Only
692dee1ad47SJeff Kirsher 			 * applicable for weak-ordered memory model archs,
693dee1ad47SJeff Kirsher 			 * such as IA-64).
694dee1ad47SJeff Kirsher 			 */
695dee1ad47SJeff Kirsher 			wmb();
696823dcd25SDavid S. Miller 			if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
69755aa6985SBruce Allan 				e1000e_update_rdt_wa(rx_ring, i);
698823dcd25SDavid S. Miller 			else
699c5083cf6SBruce Allan 				writel(i, rx_ring->tail);
700dee1ad47SJeff Kirsher 		}
701dee1ad47SJeff Kirsher 		i++;
702dee1ad47SJeff Kirsher 		if (i == rx_ring->count)
703dee1ad47SJeff Kirsher 			i = 0;
704dee1ad47SJeff Kirsher 		buffer_info = &rx_ring->buffer_info[i];
705dee1ad47SJeff Kirsher 	}
706dee1ad47SJeff Kirsher 
707dee1ad47SJeff Kirsher 	rx_ring->next_to_use = i;
708dee1ad47SJeff Kirsher }
709dee1ad47SJeff Kirsher 
710dee1ad47SJeff Kirsher /**
711dee1ad47SJeff Kirsher  * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
71255aa6985SBruce Allan  * @rx_ring: Rx descriptor ring
713b50f7bcaSJesse Brandeburg  * @cleaned_count: number to reallocate
714b50f7bcaSJesse Brandeburg  * @gfp: flags for allocation
715dee1ad47SJeff Kirsher  **/
e1000_alloc_rx_buffers_ps(struct e1000_ring * rx_ring,int cleaned_count,gfp_t gfp)71655aa6985SBruce Allan static void e1000_alloc_rx_buffers_ps(struct e1000_ring *rx_ring,
717dee1ad47SJeff Kirsher 				      int cleaned_count, gfp_t gfp)
718dee1ad47SJeff Kirsher {
71955aa6985SBruce Allan 	struct e1000_adapter *adapter = rx_ring->adapter;
720dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
721dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
722dee1ad47SJeff Kirsher 	union e1000_rx_desc_packet_split *rx_desc;
723dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
724dee1ad47SJeff Kirsher 	struct e1000_ps_page *ps_page;
725dee1ad47SJeff Kirsher 	struct sk_buff *skb;
726dee1ad47SJeff Kirsher 	unsigned int i, j;
727dee1ad47SJeff Kirsher 
728dee1ad47SJeff Kirsher 	i = rx_ring->next_to_use;
729dee1ad47SJeff Kirsher 	buffer_info = &rx_ring->buffer_info[i];
730dee1ad47SJeff Kirsher 
731dee1ad47SJeff Kirsher 	while (cleaned_count--) {
732dee1ad47SJeff Kirsher 		rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
733dee1ad47SJeff Kirsher 
734dee1ad47SJeff Kirsher 		for (j = 0; j < PS_PAGE_BUFFERS; j++) {
735dee1ad47SJeff Kirsher 			ps_page = &buffer_info->ps_pages[j];
736dee1ad47SJeff Kirsher 			if (j >= adapter->rx_ps_pages) {
737dee1ad47SJeff Kirsher 				/* all unused desc entries get hw null ptr */
738dee1ad47SJeff Kirsher 				rx_desc->read.buffer_addr[j + 1] =
739dee1ad47SJeff Kirsher 				    ~cpu_to_le64(0);
740dee1ad47SJeff Kirsher 				continue;
741dee1ad47SJeff Kirsher 			}
742dee1ad47SJeff Kirsher 			if (!ps_page->page) {
743dee1ad47SJeff Kirsher 				ps_page->page = alloc_page(gfp);
744dee1ad47SJeff Kirsher 				if (!ps_page->page) {
745dee1ad47SJeff Kirsher 					adapter->alloc_rx_buff_failed++;
746dee1ad47SJeff Kirsher 					goto no_buffers;
747dee1ad47SJeff Kirsher 				}
748dee1ad47SJeff Kirsher 				ps_page->dma = dma_map_page(&pdev->dev,
749dee1ad47SJeff Kirsher 							    ps_page->page,
750dee1ad47SJeff Kirsher 							    0, PAGE_SIZE,
751dee1ad47SJeff Kirsher 							    DMA_FROM_DEVICE);
752dee1ad47SJeff Kirsher 				if (dma_mapping_error(&pdev->dev,
753dee1ad47SJeff Kirsher 						      ps_page->dma)) {
754dee1ad47SJeff Kirsher 					dev_err(&adapter->pdev->dev,
755dee1ad47SJeff Kirsher 						"Rx DMA page map failed\n");
756dee1ad47SJeff Kirsher 					adapter->rx_dma_failed++;
757dee1ad47SJeff Kirsher 					goto no_buffers;
758dee1ad47SJeff Kirsher 				}
759dee1ad47SJeff Kirsher 			}
760e921eb1aSBruce Allan 			/* Refresh the desc even if buffer_addrs
761dee1ad47SJeff Kirsher 			 * didn't change because each write-back
762dee1ad47SJeff Kirsher 			 * erases this info.
763dee1ad47SJeff Kirsher 			 */
764dee1ad47SJeff Kirsher 			rx_desc->read.buffer_addr[j + 1] =
765dee1ad47SJeff Kirsher 			    cpu_to_le64(ps_page->dma);
766dee1ad47SJeff Kirsher 		}
767dee1ad47SJeff Kirsher 
768e5fe2541SBruce Allan 		skb = __netdev_alloc_skb_ip_align(netdev, adapter->rx_ps_bsize0,
769dee1ad47SJeff Kirsher 						  gfp);
770dee1ad47SJeff Kirsher 
771dee1ad47SJeff Kirsher 		if (!skb) {
772dee1ad47SJeff Kirsher 			adapter->alloc_rx_buff_failed++;
773dee1ad47SJeff Kirsher 			break;
774dee1ad47SJeff Kirsher 		}
775dee1ad47SJeff Kirsher 
776dee1ad47SJeff Kirsher 		buffer_info->skb = skb;
777dee1ad47SJeff Kirsher 		buffer_info->dma = dma_map_single(&pdev->dev, skb->data,
778dee1ad47SJeff Kirsher 						  adapter->rx_ps_bsize0,
779dee1ad47SJeff Kirsher 						  DMA_FROM_DEVICE);
780dee1ad47SJeff Kirsher 		if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
781dee1ad47SJeff Kirsher 			dev_err(&pdev->dev, "Rx DMA map failed\n");
782dee1ad47SJeff Kirsher 			adapter->rx_dma_failed++;
783dee1ad47SJeff Kirsher 			/* cleanup skb */
784dee1ad47SJeff Kirsher 			dev_kfree_skb_any(skb);
785dee1ad47SJeff Kirsher 			buffer_info->skb = NULL;
786dee1ad47SJeff Kirsher 			break;
787dee1ad47SJeff Kirsher 		}
788dee1ad47SJeff Kirsher 
789dee1ad47SJeff Kirsher 		rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
790dee1ad47SJeff Kirsher 
791dee1ad47SJeff Kirsher 		if (unlikely(!(i & (E1000_RX_BUFFER_WRITE - 1)))) {
792e921eb1aSBruce Allan 			/* Force memory writes to complete before letting h/w
793dee1ad47SJeff Kirsher 			 * know there are new descriptors to fetch.  (Only
794dee1ad47SJeff Kirsher 			 * applicable for weak-ordered memory model archs,
795dee1ad47SJeff Kirsher 			 * such as IA-64).
796dee1ad47SJeff Kirsher 			 */
797dee1ad47SJeff Kirsher 			wmb();
798823dcd25SDavid S. Miller 			if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
79955aa6985SBruce Allan 				e1000e_update_rdt_wa(rx_ring, i << 1);
800823dcd25SDavid S. Miller 			else
801c5083cf6SBruce Allan 				writel(i << 1, rx_ring->tail);
802dee1ad47SJeff Kirsher 		}
803dee1ad47SJeff Kirsher 
804dee1ad47SJeff Kirsher 		i++;
805dee1ad47SJeff Kirsher 		if (i == rx_ring->count)
806dee1ad47SJeff Kirsher 			i = 0;
807dee1ad47SJeff Kirsher 		buffer_info = &rx_ring->buffer_info[i];
808dee1ad47SJeff Kirsher 	}
809dee1ad47SJeff Kirsher 
810dee1ad47SJeff Kirsher no_buffers:
811dee1ad47SJeff Kirsher 	rx_ring->next_to_use = i;
812dee1ad47SJeff Kirsher }
813dee1ad47SJeff Kirsher 
814dee1ad47SJeff Kirsher /**
815dee1ad47SJeff Kirsher  * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
81655aa6985SBruce Allan  * @rx_ring: Rx descriptor ring
817dee1ad47SJeff Kirsher  * @cleaned_count: number of buffers to allocate this pass
818b50f7bcaSJesse Brandeburg  * @gfp: flags for allocation
819dee1ad47SJeff Kirsher  **/
820dee1ad47SJeff Kirsher 
e1000_alloc_jumbo_rx_buffers(struct e1000_ring * rx_ring,int cleaned_count,gfp_t gfp)82155aa6985SBruce Allan static void e1000_alloc_jumbo_rx_buffers(struct e1000_ring *rx_ring,
822dee1ad47SJeff Kirsher 					 int cleaned_count, gfp_t gfp)
823dee1ad47SJeff Kirsher {
82455aa6985SBruce Allan 	struct e1000_adapter *adapter = rx_ring->adapter;
825dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
826dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
8275f450212SBruce Allan 	union e1000_rx_desc_extended *rx_desc;
828dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
829dee1ad47SJeff Kirsher 	struct sk_buff *skb;
830dee1ad47SJeff Kirsher 	unsigned int i;
8312a2293b9SBruce Allan 	unsigned int bufsz = 256 - 16;	/* for skb_reserve */
832dee1ad47SJeff Kirsher 
833dee1ad47SJeff Kirsher 	i = rx_ring->next_to_use;
834dee1ad47SJeff Kirsher 	buffer_info = &rx_ring->buffer_info[i];
835dee1ad47SJeff Kirsher 
836dee1ad47SJeff Kirsher 	while (cleaned_count--) {
837dee1ad47SJeff Kirsher 		skb = buffer_info->skb;
838dee1ad47SJeff Kirsher 		if (skb) {
839dee1ad47SJeff Kirsher 			skb_trim(skb, 0);
840dee1ad47SJeff Kirsher 			goto check_page;
841dee1ad47SJeff Kirsher 		}
842dee1ad47SJeff Kirsher 
843dee1ad47SJeff Kirsher 		skb = __netdev_alloc_skb_ip_align(netdev, bufsz, gfp);
844dee1ad47SJeff Kirsher 		if (unlikely(!skb)) {
845dee1ad47SJeff Kirsher 			/* Better luck next round */
846dee1ad47SJeff Kirsher 			adapter->alloc_rx_buff_failed++;
847dee1ad47SJeff Kirsher 			break;
848dee1ad47SJeff Kirsher 		}
849dee1ad47SJeff Kirsher 
850dee1ad47SJeff Kirsher 		buffer_info->skb = skb;
851dee1ad47SJeff Kirsher check_page:
852dee1ad47SJeff Kirsher 		/* allocate a new page if necessary */
853dee1ad47SJeff Kirsher 		if (!buffer_info->page) {
854dee1ad47SJeff Kirsher 			buffer_info->page = alloc_page(gfp);
855dee1ad47SJeff Kirsher 			if (unlikely(!buffer_info->page)) {
856dee1ad47SJeff Kirsher 				adapter->alloc_rx_buff_failed++;
857dee1ad47SJeff Kirsher 				break;
858dee1ad47SJeff Kirsher 			}
859dee1ad47SJeff Kirsher 		}
860dee1ad47SJeff Kirsher 
86137287faeSChristoph Paasch 		if (!buffer_info->dma) {
862dee1ad47SJeff Kirsher 			buffer_info->dma = dma_map_page(&pdev->dev,
863dee1ad47SJeff Kirsher 							buffer_info->page, 0,
864dee1ad47SJeff Kirsher 							PAGE_SIZE,
865dee1ad47SJeff Kirsher 							DMA_FROM_DEVICE);
86637287faeSChristoph Paasch 			if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
86737287faeSChristoph Paasch 				adapter->alloc_rx_buff_failed++;
86837287faeSChristoph Paasch 				break;
86937287faeSChristoph Paasch 			}
87037287faeSChristoph Paasch 		}
871dee1ad47SJeff Kirsher 
8725f450212SBruce Allan 		rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
8735f450212SBruce Allan 		rx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
874dee1ad47SJeff Kirsher 
875dee1ad47SJeff Kirsher 		if (unlikely(++i == rx_ring->count))
876dee1ad47SJeff Kirsher 			i = 0;
877dee1ad47SJeff Kirsher 		buffer_info = &rx_ring->buffer_info[i];
878dee1ad47SJeff Kirsher 	}
879dee1ad47SJeff Kirsher 
880dee1ad47SJeff Kirsher 	if (likely(rx_ring->next_to_use != i)) {
881dee1ad47SJeff Kirsher 		rx_ring->next_to_use = i;
882dee1ad47SJeff Kirsher 		if (unlikely(i-- == 0))
883dee1ad47SJeff Kirsher 			i = (rx_ring->count - 1);
884dee1ad47SJeff Kirsher 
885dee1ad47SJeff Kirsher 		/* Force memory writes to complete before letting h/w
886dee1ad47SJeff Kirsher 		 * know there are new descriptors to fetch.  (Only
887dee1ad47SJeff Kirsher 		 * applicable for weak-ordered memory model archs,
888e921eb1aSBruce Allan 		 * such as IA-64).
889e921eb1aSBruce Allan 		 */
890dee1ad47SJeff Kirsher 		wmb();
891823dcd25SDavid S. Miller 		if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
89255aa6985SBruce Allan 			e1000e_update_rdt_wa(rx_ring, i);
893823dcd25SDavid S. Miller 		else
894c5083cf6SBruce Allan 			writel(i, rx_ring->tail);
895dee1ad47SJeff Kirsher 	}
896dee1ad47SJeff Kirsher }
897dee1ad47SJeff Kirsher 
e1000_rx_hash(struct net_device * netdev,__le32 rss,struct sk_buff * skb)89870495a50SBruce Allan static inline void e1000_rx_hash(struct net_device *netdev, __le32 rss,
89970495a50SBruce Allan 				 struct sk_buff *skb)
90070495a50SBruce Allan {
90170495a50SBruce Allan 	if (netdev->features & NETIF_F_RXHASH)
902e25909bcSTom Herbert 		skb_set_hash(skb, le32_to_cpu(rss), PKT_HASH_TYPE_L3);
90370495a50SBruce Allan }
90470495a50SBruce Allan 
905dee1ad47SJeff Kirsher /**
90655aa6985SBruce Allan  * e1000_clean_rx_irq - Send received data up the network stack
90755aa6985SBruce Allan  * @rx_ring: Rx descriptor ring
908b50f7bcaSJesse Brandeburg  * @work_done: output parameter for indicating completed work
909b50f7bcaSJesse Brandeburg  * @work_to_do: how many packets we can clean
910dee1ad47SJeff Kirsher  *
911dee1ad47SJeff Kirsher  * the return value indicates whether actual cleaning was done, there
912dee1ad47SJeff Kirsher  * is no guarantee that everything was cleaned
913dee1ad47SJeff Kirsher  **/
e1000_clean_rx_irq(struct e1000_ring * rx_ring,int * work_done,int work_to_do)91455aa6985SBruce Allan static bool e1000_clean_rx_irq(struct e1000_ring *rx_ring, int *work_done,
91555aa6985SBruce Allan 			       int work_to_do)
916dee1ad47SJeff Kirsher {
91755aa6985SBruce Allan 	struct e1000_adapter *adapter = rx_ring->adapter;
918dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
919dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
920dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
9215f450212SBruce Allan 	union e1000_rx_desc_extended *rx_desc, *next_rxd;
922dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info, *next_buffer;
9235f450212SBruce Allan 	u32 length, staterr;
924dee1ad47SJeff Kirsher 	unsigned int i;
925dee1ad47SJeff Kirsher 	int cleaned_count = 0;
9263db1cd5cSRusty Russell 	bool cleaned = false;
927dee1ad47SJeff Kirsher 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
928dee1ad47SJeff Kirsher 
929dee1ad47SJeff Kirsher 	i = rx_ring->next_to_clean;
9305f450212SBruce Allan 	rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
9315f450212SBruce Allan 	staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
932dee1ad47SJeff Kirsher 	buffer_info = &rx_ring->buffer_info[i];
933dee1ad47SJeff Kirsher 
9345f450212SBruce Allan 	while (staterr & E1000_RXD_STAT_DD) {
935dee1ad47SJeff Kirsher 		struct sk_buff *skb;
936dee1ad47SJeff Kirsher 
937dee1ad47SJeff Kirsher 		if (*work_done >= work_to_do)
938dee1ad47SJeff Kirsher 			break;
939dee1ad47SJeff Kirsher 		(*work_done)++;
940837a1dbaSAlexander Duyck 		dma_rmb();	/* read descriptor and rx_buffer_info after status DD */
941dee1ad47SJeff Kirsher 
942dee1ad47SJeff Kirsher 		skb = buffer_info->skb;
943dee1ad47SJeff Kirsher 		buffer_info->skb = NULL;
944dee1ad47SJeff Kirsher 
945dee1ad47SJeff Kirsher 		prefetch(skb->data - NET_IP_ALIGN);
946dee1ad47SJeff Kirsher 
947dee1ad47SJeff Kirsher 		i++;
948dee1ad47SJeff Kirsher 		if (i == rx_ring->count)
949dee1ad47SJeff Kirsher 			i = 0;
9505f450212SBruce Allan 		next_rxd = E1000_RX_DESC_EXT(*rx_ring, i);
951dee1ad47SJeff Kirsher 		prefetch(next_rxd);
952dee1ad47SJeff Kirsher 
953dee1ad47SJeff Kirsher 		next_buffer = &rx_ring->buffer_info[i];
954dee1ad47SJeff Kirsher 
9553db1cd5cSRusty Russell 		cleaned = true;
956dee1ad47SJeff Kirsher 		cleaned_count++;
957e5fe2541SBruce Allan 		dma_unmap_single(&pdev->dev, buffer_info->dma,
958e5fe2541SBruce Allan 				 adapter->rx_buffer_len, DMA_FROM_DEVICE);
959dee1ad47SJeff Kirsher 		buffer_info->dma = 0;
960dee1ad47SJeff Kirsher 
9615f450212SBruce Allan 		length = le16_to_cpu(rx_desc->wb.upper.length);
962dee1ad47SJeff Kirsher 
963e921eb1aSBruce Allan 		/* !EOP means multiple descriptors were used to store a single
964dee1ad47SJeff Kirsher 		 * packet, if that's the case we need to toss it.  In fact, we
965dee1ad47SJeff Kirsher 		 * need to toss every packet with the EOP bit clear and the
966dee1ad47SJeff Kirsher 		 * next frame that _does_ have the EOP bit set, as it is by
967dee1ad47SJeff Kirsher 		 * definition only a frame fragment
968dee1ad47SJeff Kirsher 		 */
9695f450212SBruce Allan 		if (unlikely(!(staterr & E1000_RXD_STAT_EOP)))
970dee1ad47SJeff Kirsher 			adapter->flags2 |= FLAG2_IS_DISCARDING;
971dee1ad47SJeff Kirsher 
972dee1ad47SJeff Kirsher 		if (adapter->flags2 & FLAG2_IS_DISCARDING) {
973dee1ad47SJeff Kirsher 			/* All receives must fit into a single buffer */
974dee1ad47SJeff Kirsher 			e_dbg("Receive packet consumed multiple buffers\n");
975dee1ad47SJeff Kirsher 			/* recycle */
976dee1ad47SJeff Kirsher 			buffer_info->skb = skb;
9775f450212SBruce Allan 			if (staterr & E1000_RXD_STAT_EOP)
978dee1ad47SJeff Kirsher 				adapter->flags2 &= ~FLAG2_IS_DISCARDING;
979dee1ad47SJeff Kirsher 			goto next_desc;
980dee1ad47SJeff Kirsher 		}
981dee1ad47SJeff Kirsher 
982cf955e6cSBen Greear 		if (unlikely((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) &&
983cf955e6cSBen Greear 			     !(netdev->features & NETIF_F_RXALL))) {
984dee1ad47SJeff Kirsher 			/* recycle */
985dee1ad47SJeff Kirsher 			buffer_info->skb = skb;
986dee1ad47SJeff Kirsher 			goto next_desc;
987dee1ad47SJeff Kirsher 		}
988dee1ad47SJeff Kirsher 
989dee1ad47SJeff Kirsher 		/* adjust length to remove Ethernet CRC */
9900184039aSBen Greear 		if (!(adapter->flags2 & FLAG2_CRC_STRIPPING)) {
9910184039aSBen Greear 			/* If configured to store CRC, don't subtract FCS,
9920184039aSBen Greear 			 * but keep the FCS bytes out of the total_rx_bytes
9930184039aSBen Greear 			 * counter
9940184039aSBen Greear 			 */
9950184039aSBen Greear 			if (netdev->features & NETIF_F_RXFCS)
9960184039aSBen Greear 				total_rx_bytes -= 4;
9970184039aSBen Greear 			else
998dee1ad47SJeff Kirsher 				length -= 4;
9990184039aSBen Greear 		}
1000dee1ad47SJeff Kirsher 
1001dee1ad47SJeff Kirsher 		total_rx_bytes += length;
1002dee1ad47SJeff Kirsher 		total_rx_packets++;
1003dee1ad47SJeff Kirsher 
1004e921eb1aSBruce Allan 		/* code added for copybreak, this should improve
1005dee1ad47SJeff Kirsher 		 * performance for small packets with large amounts
1006dee1ad47SJeff Kirsher 		 * of reassembly being done in the stack
1007dee1ad47SJeff Kirsher 		 */
1008dee1ad47SJeff Kirsher 		if (length < copybreak) {
1009dee1ad47SJeff Kirsher 			struct sk_buff *new_skb =
101067fd893eSAlexander Duyck 				napi_alloc_skb(&adapter->napi, length);
1011dee1ad47SJeff Kirsher 			if (new_skb) {
1012dee1ad47SJeff Kirsher 				skb_copy_to_linear_data_offset(new_skb,
1013dee1ad47SJeff Kirsher 							       -NET_IP_ALIGN,
1014dee1ad47SJeff Kirsher 							       (skb->data -
1015dee1ad47SJeff Kirsher 								NET_IP_ALIGN),
1016dee1ad47SJeff Kirsher 							       (length +
1017dee1ad47SJeff Kirsher 								NET_IP_ALIGN));
1018dee1ad47SJeff Kirsher 				/* save the skb in buffer_info as good */
1019dee1ad47SJeff Kirsher 				buffer_info->skb = skb;
1020dee1ad47SJeff Kirsher 				skb = new_skb;
1021dee1ad47SJeff Kirsher 			}
1022dee1ad47SJeff Kirsher 			/* else just continue with the old one */
1023dee1ad47SJeff Kirsher 		}
1024dee1ad47SJeff Kirsher 		/* end copybreak code */
1025dee1ad47SJeff Kirsher 		skb_put(skb, length);
1026dee1ad47SJeff Kirsher 
1027dee1ad47SJeff Kirsher 		/* Receive Checksum Offload */
10282e1706f2SBruce Allan 		e1000_rx_checksum(adapter, staterr, skb);
1029dee1ad47SJeff Kirsher 
103070495a50SBruce Allan 		e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
103170495a50SBruce Allan 
10325f450212SBruce Allan 		e1000_receive_skb(adapter, netdev, skb, staterr,
10335f450212SBruce Allan 				  rx_desc->wb.upper.vlan);
1034dee1ad47SJeff Kirsher 
1035dee1ad47SJeff Kirsher next_desc:
10365f450212SBruce Allan 		rx_desc->wb.upper.status_error &= cpu_to_le32(~0xFF);
1037dee1ad47SJeff Kirsher 
1038dee1ad47SJeff Kirsher 		/* return some buffers to hardware, one at a time is too slow */
1039dee1ad47SJeff Kirsher 		if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
104055aa6985SBruce Allan 			adapter->alloc_rx_buf(rx_ring, cleaned_count,
1041dee1ad47SJeff Kirsher 					      GFP_ATOMIC);
1042dee1ad47SJeff Kirsher 			cleaned_count = 0;
1043dee1ad47SJeff Kirsher 		}
1044dee1ad47SJeff Kirsher 
1045dee1ad47SJeff Kirsher 		/* use prefetched values */
1046dee1ad47SJeff Kirsher 		rx_desc = next_rxd;
1047dee1ad47SJeff Kirsher 		buffer_info = next_buffer;
10485f450212SBruce Allan 
10495f450212SBruce Allan 		staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
1050dee1ad47SJeff Kirsher 	}
1051dee1ad47SJeff Kirsher 	rx_ring->next_to_clean = i;
1052dee1ad47SJeff Kirsher 
1053dee1ad47SJeff Kirsher 	cleaned_count = e1000_desc_unused(rx_ring);
1054dee1ad47SJeff Kirsher 	if (cleaned_count)
105555aa6985SBruce Allan 		adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
1056dee1ad47SJeff Kirsher 
1057dee1ad47SJeff Kirsher 	adapter->total_rx_bytes += total_rx_bytes;
1058dee1ad47SJeff Kirsher 	adapter->total_rx_packets += total_rx_packets;
1059dee1ad47SJeff Kirsher 	return cleaned;
1060dee1ad47SJeff Kirsher }
1061dee1ad47SJeff Kirsher 
e1000_put_txbuf(struct e1000_ring * tx_ring,struct e1000_buffer * buffer_info,bool drop)106255aa6985SBruce Allan static void e1000_put_txbuf(struct e1000_ring *tx_ring,
1063377b6273SFlorian Fainelli 			    struct e1000_buffer *buffer_info,
1064377b6273SFlorian Fainelli 			    bool drop)
1065dee1ad47SJeff Kirsher {
106655aa6985SBruce Allan 	struct e1000_adapter *adapter = tx_ring->adapter;
106755aa6985SBruce Allan 
1068dee1ad47SJeff Kirsher 	if (buffer_info->dma) {
1069dee1ad47SJeff Kirsher 		if (buffer_info->mapped_as_page)
1070dee1ad47SJeff Kirsher 			dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
1071dee1ad47SJeff Kirsher 				       buffer_info->length, DMA_TO_DEVICE);
1072dee1ad47SJeff Kirsher 		else
1073dee1ad47SJeff Kirsher 			dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1074dee1ad47SJeff Kirsher 					 buffer_info->length, DMA_TO_DEVICE);
1075dee1ad47SJeff Kirsher 		buffer_info->dma = 0;
1076dee1ad47SJeff Kirsher 	}
1077dee1ad47SJeff Kirsher 	if (buffer_info->skb) {
1078377b6273SFlorian Fainelli 		if (drop)
1079dee1ad47SJeff Kirsher 			dev_kfree_skb_any(buffer_info->skb);
1080377b6273SFlorian Fainelli 		else
1081377b6273SFlorian Fainelli 			dev_consume_skb_any(buffer_info->skb);
1082dee1ad47SJeff Kirsher 		buffer_info->skb = NULL;
1083dee1ad47SJeff Kirsher 	}
1084dee1ad47SJeff Kirsher 	buffer_info->time_stamp = 0;
1085dee1ad47SJeff Kirsher }
1086dee1ad47SJeff Kirsher 
e1000_print_hw_hang(struct work_struct * work)1087dee1ad47SJeff Kirsher static void e1000_print_hw_hang(struct work_struct *work)
1088dee1ad47SJeff Kirsher {
1089dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = container_of(work,
1090dee1ad47SJeff Kirsher 						     struct e1000_adapter,
1091dee1ad47SJeff Kirsher 						     print_hang_task);
109209357b00SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1093dee1ad47SJeff Kirsher 	struct e1000_ring *tx_ring = adapter->tx_ring;
1094dee1ad47SJeff Kirsher 	unsigned int i = tx_ring->next_to_clean;
1095dee1ad47SJeff Kirsher 	unsigned int eop = tx_ring->buffer_info[i].next_to_watch;
1096dee1ad47SJeff Kirsher 	struct e1000_tx_desc *eop_desc = E1000_TX_DESC(*tx_ring, eop);
1097dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
1098dee1ad47SJeff Kirsher 	u16 phy_status, phy_1000t_status, phy_ext_status;
1099dee1ad47SJeff Kirsher 	u16 pci_status;
1100dee1ad47SJeff Kirsher 
1101dee1ad47SJeff Kirsher 	if (test_bit(__E1000_DOWN, &adapter->state))
1102dee1ad47SJeff Kirsher 		return;
1103dee1ad47SJeff Kirsher 
1104e5fe2541SBruce Allan 	if (!adapter->tx_hang_recheck && (adapter->flags2 & FLAG2_DMA_BURST)) {
1105e921eb1aSBruce Allan 		/* May be block on write-back, flush and detect again
110609357b00SJeff Kirsher 		 * flush pending descriptor writebacks to memory
110709357b00SJeff Kirsher 		 */
110809357b00SJeff Kirsher 		ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
110909357b00SJeff Kirsher 		/* execute the writes immediately */
111009357b00SJeff Kirsher 		e1e_flush();
1111e921eb1aSBruce Allan 		/* Due to rare timing issues, write to TIDV again to ensure
1112bf03085fSMatthew Vick 		 * the write is successful
1113bf03085fSMatthew Vick 		 */
1114bf03085fSMatthew Vick 		ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
1115bf03085fSMatthew Vick 		/* execute the writes immediately */
1116bf03085fSMatthew Vick 		e1e_flush();
111709357b00SJeff Kirsher 		adapter->tx_hang_recheck = true;
111809357b00SJeff Kirsher 		return;
111909357b00SJeff Kirsher 	}
112009357b00SJeff Kirsher 	adapter->tx_hang_recheck = false;
1121d9554e96SDavid Ertman 
1122d9554e96SDavid Ertman 	if (er32(TDH(0)) == er32(TDT(0))) {
1123d9554e96SDavid Ertman 		e_dbg("false hang detected, ignoring\n");
1124d9554e96SDavid Ertman 		return;
1125d9554e96SDavid Ertman 	}
1126d9554e96SDavid Ertman 
1127d9554e96SDavid Ertman 	/* Real hang detected */
112809357b00SJeff Kirsher 	netif_stop_queue(netdev);
112909357b00SJeff Kirsher 
1130c2ade1a4SBruce Allan 	e1e_rphy(hw, MII_BMSR, &phy_status);
1131c2ade1a4SBruce Allan 	e1e_rphy(hw, MII_STAT1000, &phy_1000t_status);
1132c2ade1a4SBruce Allan 	e1e_rphy(hw, MII_ESTATUS, &phy_ext_status);
1133dee1ad47SJeff Kirsher 
1134dee1ad47SJeff Kirsher 	pci_read_config_word(adapter->pdev, PCI_STATUS, &pci_status);
1135dee1ad47SJeff Kirsher 
1136dee1ad47SJeff Kirsher 	/* detected Hardware unit hang */
1137dee1ad47SJeff Kirsher 	e_err("Detected Hardware Unit Hang:\n"
1138dee1ad47SJeff Kirsher 	      "  TDH                  <%x>\n"
1139dee1ad47SJeff Kirsher 	      "  TDT                  <%x>\n"
1140dee1ad47SJeff Kirsher 	      "  next_to_use          <%x>\n"
1141dee1ad47SJeff Kirsher 	      "  next_to_clean        <%x>\n"
1142dee1ad47SJeff Kirsher 	      "buffer_info[next_to_clean]:\n"
1143dee1ad47SJeff Kirsher 	      "  time_stamp           <%lx>\n"
1144dee1ad47SJeff Kirsher 	      "  next_to_watch        <%x>\n"
1145dee1ad47SJeff Kirsher 	      "  jiffies              <%lx>\n"
1146dee1ad47SJeff Kirsher 	      "  next_to_watch.status <%x>\n"
1147dee1ad47SJeff Kirsher 	      "MAC Status             <%x>\n"
1148dee1ad47SJeff Kirsher 	      "PHY Status             <%x>\n"
1149dee1ad47SJeff Kirsher 	      "PHY 1000BASE-T Status  <%x>\n"
1150dee1ad47SJeff Kirsher 	      "PHY Extended Status    <%x>\n"
1151dee1ad47SJeff Kirsher 	      "PCI Status             <%x>\n",
1152e5fe2541SBruce Allan 	      readl(tx_ring->head), readl(tx_ring->tail), tx_ring->next_to_use,
1153e5fe2541SBruce Allan 	      tx_ring->next_to_clean, tx_ring->buffer_info[eop].time_stamp,
1154e5fe2541SBruce Allan 	      eop, jiffies, eop_desc->upper.fields.status, er32(STATUS),
1155e5fe2541SBruce Allan 	      phy_status, phy_1000t_status, phy_ext_status, pci_status);
11567c0427eeSBruce Allan 
1157d9554e96SDavid Ertman 	e1000e_dump(adapter);
1158d9554e96SDavid Ertman 
11597c0427eeSBruce Allan 	/* Suggest workaround for known h/w issue */
11607c0427eeSBruce Allan 	if ((hw->mac.type == e1000_pchlan) && (er32(CTRL) & E1000_CTRL_TFCE))
11617c0427eeSBruce Allan 		e_err("Try turning off Tx pause (flow control) via ethtool\n");
1162dee1ad47SJeff Kirsher }
1163dee1ad47SJeff Kirsher 
1164dee1ad47SJeff Kirsher /**
1165b67e1913SBruce Allan  * e1000e_tx_hwtstamp_work - check for Tx time stamp
1166b67e1913SBruce Allan  * @work: pointer to work struct
1167b67e1913SBruce Allan  *
1168b67e1913SBruce Allan  * This work function polls the TSYNCTXCTL valid bit to determine when a
1169b67e1913SBruce Allan  * timestamp has been taken for the current stored skb.  The timestamp must
1170b67e1913SBruce Allan  * be for this skb because only one such packet is allowed in the queue.
1171b67e1913SBruce Allan  */
e1000e_tx_hwtstamp_work(struct work_struct * work)1172b67e1913SBruce Allan static void e1000e_tx_hwtstamp_work(struct work_struct *work)
1173b67e1913SBruce Allan {
1174b67e1913SBruce Allan 	struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
1175b67e1913SBruce Allan 						     tx_hwtstamp_work);
1176b67e1913SBruce Allan 	struct e1000_hw *hw = &adapter->hw;
1177b67e1913SBruce Allan 
1178b67e1913SBruce Allan 	if (er32(TSYNCTXCTL) & E1000_TSYNCTXCTL_VALID) {
11795012863bSJacob Keller 		struct sk_buff *skb = adapter->tx_hwtstamp_skb;
1180b67e1913SBruce Allan 		struct skb_shared_hwtstamps shhwtstamps;
1181b67e1913SBruce Allan 		u64 txstmp;
1182b67e1913SBruce Allan 
1183b67e1913SBruce Allan 		txstmp = er32(TXSTMPL);
1184b67e1913SBruce Allan 		txstmp |= (u64)er32(TXSTMPH) << 32;
1185b67e1913SBruce Allan 
1186b67e1913SBruce Allan 		e1000e_systim_to_hwtstamp(adapter, &shhwtstamps, txstmp);
1187b67e1913SBruce Allan 
11885012863bSJacob Keller 		/* Clear the global tx_hwtstamp_skb pointer and force writes
11895012863bSJacob Keller 		 * prior to notifying the stack of a Tx timestamp.
11905012863bSJacob Keller 		 */
1191b67e1913SBruce Allan 		adapter->tx_hwtstamp_skb = NULL;
11925012863bSJacob Keller 		wmb(); /* force write prior to skb_tstamp_tx */
11935012863bSJacob Keller 
11945012863bSJacob Keller 		skb_tstamp_tx(skb, &shhwtstamps);
1195377b6273SFlorian Fainelli 		dev_consume_skb_any(skb);
119659c871c5SJakub Kicinski 	} else if (time_after(jiffies, adapter->tx_hwtstamp_start
119759c871c5SJakub Kicinski 			      + adapter->tx_timeout_factor * HZ)) {
119859c871c5SJakub Kicinski 		dev_kfree_skb_any(adapter->tx_hwtstamp_skb);
119959c871c5SJakub Kicinski 		adapter->tx_hwtstamp_skb = NULL;
120059c871c5SJakub Kicinski 		adapter->tx_hwtstamp_timeouts++;
1201c5ffe7e1SJakub Kicinski 		e_warn("clearing Tx timestamp hang\n");
1202b67e1913SBruce Allan 	} else {
1203b67e1913SBruce Allan 		/* reschedule to check later */
1204b67e1913SBruce Allan 		schedule_work(&adapter->tx_hwtstamp_work);
1205b67e1913SBruce Allan 	}
1206b67e1913SBruce Allan }
1207b67e1913SBruce Allan 
1208b67e1913SBruce Allan /**
1209dee1ad47SJeff Kirsher  * e1000_clean_tx_irq - Reclaim resources after transmit completes
121055aa6985SBruce Allan  * @tx_ring: Tx descriptor ring
1211dee1ad47SJeff Kirsher  *
1212dee1ad47SJeff Kirsher  * the return value indicates whether actual cleaning was done, there
1213dee1ad47SJeff Kirsher  * is no guarantee that everything was cleaned
1214dee1ad47SJeff Kirsher  **/
e1000_clean_tx_irq(struct e1000_ring * tx_ring)121555aa6985SBruce Allan static bool e1000_clean_tx_irq(struct e1000_ring *tx_ring)
1216dee1ad47SJeff Kirsher {
121755aa6985SBruce Allan 	struct e1000_adapter *adapter = tx_ring->adapter;
1218dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1219dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
1220dee1ad47SJeff Kirsher 	struct e1000_tx_desc *tx_desc, *eop_desc;
1221dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
1222dee1ad47SJeff Kirsher 	unsigned int i, eop;
1223dee1ad47SJeff Kirsher 	unsigned int count = 0;
1224dee1ad47SJeff Kirsher 	unsigned int total_tx_bytes = 0, total_tx_packets = 0;
12253f0cfa3bSTom Herbert 	unsigned int bytes_compl = 0, pkts_compl = 0;
1226dee1ad47SJeff Kirsher 
1227dee1ad47SJeff Kirsher 	i = tx_ring->next_to_clean;
1228dee1ad47SJeff Kirsher 	eop = tx_ring->buffer_info[i].next_to_watch;
1229dee1ad47SJeff Kirsher 	eop_desc = E1000_TX_DESC(*tx_ring, eop);
1230dee1ad47SJeff Kirsher 
1231dee1ad47SJeff Kirsher 	while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
1232dee1ad47SJeff Kirsher 	       (count < tx_ring->count)) {
1233dee1ad47SJeff Kirsher 		bool cleaned = false;
12346cf08d1cSDavid Ertman 
1235837a1dbaSAlexander Duyck 		dma_rmb();		/* read buffer_info after eop_desc */
1236dee1ad47SJeff Kirsher 		for (; !cleaned; count++) {
1237dee1ad47SJeff Kirsher 			tx_desc = E1000_TX_DESC(*tx_ring, i);
1238dee1ad47SJeff Kirsher 			buffer_info = &tx_ring->buffer_info[i];
1239dee1ad47SJeff Kirsher 			cleaned = (i == eop);
1240dee1ad47SJeff Kirsher 
1241dee1ad47SJeff Kirsher 			if (cleaned) {
1242dee1ad47SJeff Kirsher 				total_tx_packets += buffer_info->segs;
1243dee1ad47SJeff Kirsher 				total_tx_bytes += buffer_info->bytecount;
12443f0cfa3bSTom Herbert 				if (buffer_info->skb) {
12453f0cfa3bSTom Herbert 					bytes_compl += buffer_info->skb->len;
12463f0cfa3bSTom Herbert 					pkts_compl++;
12473f0cfa3bSTom Herbert 				}
1248dee1ad47SJeff Kirsher 			}
1249dee1ad47SJeff Kirsher 
1250377b6273SFlorian Fainelli 			e1000_put_txbuf(tx_ring, buffer_info, false);
1251dee1ad47SJeff Kirsher 			tx_desc->upper.data = 0;
1252dee1ad47SJeff Kirsher 
1253dee1ad47SJeff Kirsher 			i++;
1254dee1ad47SJeff Kirsher 			if (i == tx_ring->count)
1255dee1ad47SJeff Kirsher 				i = 0;
1256dee1ad47SJeff Kirsher 		}
1257dee1ad47SJeff Kirsher 
1258dee1ad47SJeff Kirsher 		if (i == tx_ring->next_to_use)
1259dee1ad47SJeff Kirsher 			break;
1260dee1ad47SJeff Kirsher 		eop = tx_ring->buffer_info[i].next_to_watch;
1261dee1ad47SJeff Kirsher 		eop_desc = E1000_TX_DESC(*tx_ring, eop);
1262dee1ad47SJeff Kirsher 	}
1263dee1ad47SJeff Kirsher 
1264dee1ad47SJeff Kirsher 	tx_ring->next_to_clean = i;
1265dee1ad47SJeff Kirsher 
12663f0cfa3bSTom Herbert 	netdev_completed_queue(netdev, pkts_compl, bytes_compl);
12673f0cfa3bSTom Herbert 
1268dee1ad47SJeff Kirsher #define TX_WAKE_THRESHOLD 32
1269dee1ad47SJeff Kirsher 	if (count && netif_carrier_ok(netdev) &&
1270dee1ad47SJeff Kirsher 	    e1000_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD) {
1271dee1ad47SJeff Kirsher 		/* Make sure that anybody stopping the queue after this
1272dee1ad47SJeff Kirsher 		 * sees the new next_to_clean.
1273dee1ad47SJeff Kirsher 		 */
1274dee1ad47SJeff Kirsher 		smp_mb();
1275dee1ad47SJeff Kirsher 
1276dee1ad47SJeff Kirsher 		if (netif_queue_stopped(netdev) &&
1277dee1ad47SJeff Kirsher 		    !(test_bit(__E1000_DOWN, &adapter->state))) {
1278dee1ad47SJeff Kirsher 			netif_wake_queue(netdev);
1279dee1ad47SJeff Kirsher 			++adapter->restart_queue;
1280dee1ad47SJeff Kirsher 		}
1281dee1ad47SJeff Kirsher 	}
1282dee1ad47SJeff Kirsher 
1283dee1ad47SJeff Kirsher 	if (adapter->detect_tx_hung) {
1284e921eb1aSBruce Allan 		/* Detect a transmit hang in hardware, this serializes the
1285dee1ad47SJeff Kirsher 		 * check with the clearing of time_stamp and movement of i
1286dee1ad47SJeff Kirsher 		 */
12873db1cd5cSRusty Russell 		adapter->detect_tx_hung = false;
1288dee1ad47SJeff Kirsher 		if (tx_ring->buffer_info[i].time_stamp &&
1289dee1ad47SJeff Kirsher 		    time_after(jiffies, tx_ring->buffer_info[i].time_stamp
1290dee1ad47SJeff Kirsher 			       + (adapter->tx_timeout_factor * HZ)) &&
129109357b00SJeff Kirsher 		    !(er32(STATUS) & E1000_STATUS_TXOFF))
1292dee1ad47SJeff Kirsher 			schedule_work(&adapter->print_hang_task);
129309357b00SJeff Kirsher 		else
129409357b00SJeff Kirsher 			adapter->tx_hang_recheck = false;
1295dee1ad47SJeff Kirsher 	}
1296dee1ad47SJeff Kirsher 	adapter->total_tx_bytes += total_tx_bytes;
1297dee1ad47SJeff Kirsher 	adapter->total_tx_packets += total_tx_packets;
1298dee1ad47SJeff Kirsher 	return count < tx_ring->count;
1299dee1ad47SJeff Kirsher }
1300dee1ad47SJeff Kirsher 
1301dee1ad47SJeff Kirsher /**
1302dee1ad47SJeff Kirsher  * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
130355aa6985SBruce Allan  * @rx_ring: Rx descriptor ring
1304b50f7bcaSJesse Brandeburg  * @work_done: output parameter for indicating completed work
1305b50f7bcaSJesse Brandeburg  * @work_to_do: how many packets we can clean
1306dee1ad47SJeff Kirsher  *
1307dee1ad47SJeff Kirsher  * the return value indicates whether actual cleaning was done, there
1308dee1ad47SJeff Kirsher  * is no guarantee that everything was cleaned
1309dee1ad47SJeff Kirsher  **/
e1000_clean_rx_irq_ps(struct e1000_ring * rx_ring,int * work_done,int work_to_do)131055aa6985SBruce Allan static bool e1000_clean_rx_irq_ps(struct e1000_ring *rx_ring, int *work_done,
131155aa6985SBruce Allan 				  int work_to_do)
1312dee1ad47SJeff Kirsher {
131355aa6985SBruce Allan 	struct e1000_adapter *adapter = rx_ring->adapter;
1314dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
1315dee1ad47SJeff Kirsher 	union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
1316dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1317dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
1318dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info, *next_buffer;
1319dee1ad47SJeff Kirsher 	struct e1000_ps_page *ps_page;
1320dee1ad47SJeff Kirsher 	struct sk_buff *skb;
1321dee1ad47SJeff Kirsher 	unsigned int i, j;
1322dee1ad47SJeff Kirsher 	u32 length, staterr;
1323dee1ad47SJeff Kirsher 	int cleaned_count = 0;
13243db1cd5cSRusty Russell 	bool cleaned = false;
1325dee1ad47SJeff Kirsher 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1326dee1ad47SJeff Kirsher 
1327dee1ad47SJeff Kirsher 	i = rx_ring->next_to_clean;
1328dee1ad47SJeff Kirsher 	rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
1329dee1ad47SJeff Kirsher 	staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
1330dee1ad47SJeff Kirsher 	buffer_info = &rx_ring->buffer_info[i];
1331dee1ad47SJeff Kirsher 
1332dee1ad47SJeff Kirsher 	while (staterr & E1000_RXD_STAT_DD) {
1333dee1ad47SJeff Kirsher 		if (*work_done >= work_to_do)
1334dee1ad47SJeff Kirsher 			break;
1335dee1ad47SJeff Kirsher 		(*work_done)++;
1336dee1ad47SJeff Kirsher 		skb = buffer_info->skb;
1337837a1dbaSAlexander Duyck 		dma_rmb();	/* read descriptor and rx_buffer_info after status DD */
1338dee1ad47SJeff Kirsher 
1339dee1ad47SJeff Kirsher 		/* in the packet split case this is header only */
1340dee1ad47SJeff Kirsher 		prefetch(skb->data - NET_IP_ALIGN);
1341dee1ad47SJeff Kirsher 
1342dee1ad47SJeff Kirsher 		i++;
1343dee1ad47SJeff Kirsher 		if (i == rx_ring->count)
1344dee1ad47SJeff Kirsher 			i = 0;
1345dee1ad47SJeff Kirsher 		next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
1346dee1ad47SJeff Kirsher 		prefetch(next_rxd);
1347dee1ad47SJeff Kirsher 
1348dee1ad47SJeff Kirsher 		next_buffer = &rx_ring->buffer_info[i];
1349dee1ad47SJeff Kirsher 
13503db1cd5cSRusty Russell 		cleaned = true;
1351dee1ad47SJeff Kirsher 		cleaned_count++;
1352dee1ad47SJeff Kirsher 		dma_unmap_single(&pdev->dev, buffer_info->dma,
1353dee1ad47SJeff Kirsher 				 adapter->rx_ps_bsize0, DMA_FROM_DEVICE);
1354dee1ad47SJeff Kirsher 		buffer_info->dma = 0;
1355dee1ad47SJeff Kirsher 
1356dee1ad47SJeff Kirsher 		/* see !EOP comment in other Rx routine */
1357dee1ad47SJeff Kirsher 		if (!(staterr & E1000_RXD_STAT_EOP))
1358dee1ad47SJeff Kirsher 			adapter->flags2 |= FLAG2_IS_DISCARDING;
1359dee1ad47SJeff Kirsher 
1360dee1ad47SJeff Kirsher 		if (adapter->flags2 & FLAG2_IS_DISCARDING) {
1361ef456f85SJeff Kirsher 			e_dbg("Packet Split buffers didn't pick up the full packet\n");
1362dee1ad47SJeff Kirsher 			dev_kfree_skb_irq(skb);
1363dee1ad47SJeff Kirsher 			if (staterr & E1000_RXD_STAT_EOP)
1364dee1ad47SJeff Kirsher 				adapter->flags2 &= ~FLAG2_IS_DISCARDING;
1365dee1ad47SJeff Kirsher 			goto next_desc;
1366dee1ad47SJeff Kirsher 		}
1367dee1ad47SJeff Kirsher 
1368cf955e6cSBen Greear 		if (unlikely((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) &&
1369cf955e6cSBen Greear 			     !(netdev->features & NETIF_F_RXALL))) {
1370dee1ad47SJeff Kirsher 			dev_kfree_skb_irq(skb);
1371dee1ad47SJeff Kirsher 			goto next_desc;
1372dee1ad47SJeff Kirsher 		}
1373dee1ad47SJeff Kirsher 
1374dee1ad47SJeff Kirsher 		length = le16_to_cpu(rx_desc->wb.middle.length0);
1375dee1ad47SJeff Kirsher 
1376dee1ad47SJeff Kirsher 		if (!length) {
1377ef456f85SJeff Kirsher 			e_dbg("Last part of the packet spanning multiple descriptors\n");
1378dee1ad47SJeff Kirsher 			dev_kfree_skb_irq(skb);
1379dee1ad47SJeff Kirsher 			goto next_desc;
1380dee1ad47SJeff Kirsher 		}
1381dee1ad47SJeff Kirsher 
1382dee1ad47SJeff Kirsher 		/* Good Receive */
1383dee1ad47SJeff Kirsher 		skb_put(skb, length);
1384dee1ad47SJeff Kirsher 
1385dee1ad47SJeff Kirsher 		{
1386e921eb1aSBruce Allan 			/* this looks ugly, but it seems compiler issues make
13870e15df49SBruce Allan 			 * it more efficient than reusing j
1388dee1ad47SJeff Kirsher 			 */
1389dee1ad47SJeff Kirsher 			int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
1390dee1ad47SJeff Kirsher 
1391e921eb1aSBruce Allan 			/* page alloc/put takes too long and effects small
13920e15df49SBruce Allan 			 * packet throughput, so unsplit small packets and
1393ab400b0dSAnirudh Venkataramanan 			 * save the alloc/put
1394dee1ad47SJeff Kirsher 			 */
1395dee1ad47SJeff Kirsher 			if (l1 && (l1 <= copybreak) &&
1396dee1ad47SJeff Kirsher 			    ((length + l1) <= adapter->rx_ps_bsize0)) {
1397dee1ad47SJeff Kirsher 				ps_page = &buffer_info->ps_pages[0];
1398dee1ad47SJeff Kirsher 
13990e15df49SBruce Allan 				dma_sync_single_for_cpu(&pdev->dev,
14000e15df49SBruce Allan 							ps_page->dma,
14010e15df49SBruce Allan 							PAGE_SIZE,
14020e15df49SBruce Allan 							DMA_FROM_DEVICE);
1403ab400b0dSAnirudh Venkataramanan 				memcpy(skb_tail_pointer(skb),
1404ab400b0dSAnirudh Venkataramanan 				       page_address(ps_page->page), l1);
14050e15df49SBruce Allan 				dma_sync_single_for_device(&pdev->dev,
14060e15df49SBruce Allan 							   ps_page->dma,
14070e15df49SBruce Allan 							   PAGE_SIZE,
14080e15df49SBruce Allan 							   DMA_FROM_DEVICE);
1409dee1ad47SJeff Kirsher 
1410dee1ad47SJeff Kirsher 				/* remove the CRC */
14110184039aSBen Greear 				if (!(adapter->flags2 & FLAG2_CRC_STRIPPING)) {
14120184039aSBen Greear 					if (!(netdev->features & NETIF_F_RXFCS))
1413dee1ad47SJeff Kirsher 						l1 -= 4;
14140184039aSBen Greear 				}
1415dee1ad47SJeff Kirsher 
1416dee1ad47SJeff Kirsher 				skb_put(skb, l1);
1417dee1ad47SJeff Kirsher 				goto copydone;
1418dee1ad47SJeff Kirsher 			}	/* if */
1419dee1ad47SJeff Kirsher 		}
1420dee1ad47SJeff Kirsher 
1421dee1ad47SJeff Kirsher 		for (j = 0; j < PS_PAGE_BUFFERS; j++) {
1422dee1ad47SJeff Kirsher 			length = le16_to_cpu(rx_desc->wb.upper.length[j]);
1423dee1ad47SJeff Kirsher 			if (!length)
1424dee1ad47SJeff Kirsher 				break;
1425dee1ad47SJeff Kirsher 
1426dee1ad47SJeff Kirsher 			ps_page = &buffer_info->ps_pages[j];
1427dee1ad47SJeff Kirsher 			dma_unmap_page(&pdev->dev, ps_page->dma, PAGE_SIZE,
1428dee1ad47SJeff Kirsher 				       DMA_FROM_DEVICE);
1429dee1ad47SJeff Kirsher 			ps_page->dma = 0;
1430dee1ad47SJeff Kirsher 			skb_fill_page_desc(skb, j, ps_page->page, 0, length);
1431dee1ad47SJeff Kirsher 			ps_page->page = NULL;
1432dee1ad47SJeff Kirsher 			skb->len += length;
1433dee1ad47SJeff Kirsher 			skb->data_len += length;
143498a045d7SEric Dumazet 			skb->truesize += PAGE_SIZE;
1435dee1ad47SJeff Kirsher 		}
1436dee1ad47SJeff Kirsher 
1437dee1ad47SJeff Kirsher 		/* strip the ethernet crc, problem is we're using pages now so
1438dee1ad47SJeff Kirsher 		 * this whole operation can get a little cpu intensive
1439dee1ad47SJeff Kirsher 		 */
14400184039aSBen Greear 		if (!(adapter->flags2 & FLAG2_CRC_STRIPPING)) {
14410184039aSBen Greear 			if (!(netdev->features & NETIF_F_RXFCS))
1442dee1ad47SJeff Kirsher 				pskb_trim(skb, skb->len - 4);
14430184039aSBen Greear 		}
1444dee1ad47SJeff Kirsher 
1445dee1ad47SJeff Kirsher copydone:
1446dee1ad47SJeff Kirsher 		total_rx_bytes += skb->len;
1447dee1ad47SJeff Kirsher 		total_rx_packets++;
1448dee1ad47SJeff Kirsher 
14492e1706f2SBruce Allan 		e1000_rx_checksum(adapter, staterr, skb);
1450dee1ad47SJeff Kirsher 
145170495a50SBruce Allan 		e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
145270495a50SBruce Allan 
1453dee1ad47SJeff Kirsher 		if (rx_desc->wb.upper.header_status &
1454dee1ad47SJeff Kirsher 		    cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP))
1455dee1ad47SJeff Kirsher 			adapter->rx_hdr_split++;
1456dee1ad47SJeff Kirsher 
1457b67e1913SBruce Allan 		e1000_receive_skb(adapter, netdev, skb, staterr,
1458b67e1913SBruce Allan 				  rx_desc->wb.middle.vlan);
1459dee1ad47SJeff Kirsher 
1460dee1ad47SJeff Kirsher next_desc:
1461dee1ad47SJeff Kirsher 		rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
1462dee1ad47SJeff Kirsher 		buffer_info->skb = NULL;
1463dee1ad47SJeff Kirsher 
1464dee1ad47SJeff Kirsher 		/* return some buffers to hardware, one at a time is too slow */
1465dee1ad47SJeff Kirsher 		if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
146655aa6985SBruce Allan 			adapter->alloc_rx_buf(rx_ring, cleaned_count,
1467dee1ad47SJeff Kirsher 					      GFP_ATOMIC);
1468dee1ad47SJeff Kirsher 			cleaned_count = 0;
1469dee1ad47SJeff Kirsher 		}
1470dee1ad47SJeff Kirsher 
1471dee1ad47SJeff Kirsher 		/* use prefetched values */
1472dee1ad47SJeff Kirsher 		rx_desc = next_rxd;
1473dee1ad47SJeff Kirsher 		buffer_info = next_buffer;
1474dee1ad47SJeff Kirsher 
1475dee1ad47SJeff Kirsher 		staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
1476dee1ad47SJeff Kirsher 	}
1477dee1ad47SJeff Kirsher 	rx_ring->next_to_clean = i;
1478dee1ad47SJeff Kirsher 
1479dee1ad47SJeff Kirsher 	cleaned_count = e1000_desc_unused(rx_ring);
1480dee1ad47SJeff Kirsher 	if (cleaned_count)
148155aa6985SBruce Allan 		adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
1482dee1ad47SJeff Kirsher 
1483dee1ad47SJeff Kirsher 	adapter->total_rx_bytes += total_rx_bytes;
1484dee1ad47SJeff Kirsher 	adapter->total_rx_packets += total_rx_packets;
1485dee1ad47SJeff Kirsher 	return cleaned;
1486dee1ad47SJeff Kirsher }
1487dee1ad47SJeff Kirsher 
e1000_consume_page(struct e1000_buffer * bi,struct sk_buff * skb,u16 length)1488dee1ad47SJeff Kirsher static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
1489dee1ad47SJeff Kirsher 			       u16 length)
1490dee1ad47SJeff Kirsher {
1491dee1ad47SJeff Kirsher 	bi->page = NULL;
1492dee1ad47SJeff Kirsher 	skb->len += length;
1493dee1ad47SJeff Kirsher 	skb->data_len += length;
149498a045d7SEric Dumazet 	skb->truesize += PAGE_SIZE;
1495dee1ad47SJeff Kirsher }
1496dee1ad47SJeff Kirsher 
1497dee1ad47SJeff Kirsher /**
1498dee1ad47SJeff Kirsher  * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
1499b50f7bcaSJesse Brandeburg  * @rx_ring: Rx descriptor ring
1500b50f7bcaSJesse Brandeburg  * @work_done: output parameter for indicating completed work
1501b50f7bcaSJesse Brandeburg  * @work_to_do: how many packets we can clean
1502dee1ad47SJeff Kirsher  *
1503dee1ad47SJeff Kirsher  * the return value indicates whether actual cleaning was done, there
1504dee1ad47SJeff Kirsher  * is no guarantee that everything was cleaned
1505dee1ad47SJeff Kirsher  **/
e1000_clean_jumbo_rx_irq(struct e1000_ring * rx_ring,int * work_done,int work_to_do)150655aa6985SBruce Allan static bool e1000_clean_jumbo_rx_irq(struct e1000_ring *rx_ring, int *work_done,
150755aa6985SBruce Allan 				     int work_to_do)
1508dee1ad47SJeff Kirsher {
150955aa6985SBruce Allan 	struct e1000_adapter *adapter = rx_ring->adapter;
1510dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1511dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
15125f450212SBruce Allan 	union e1000_rx_desc_extended *rx_desc, *next_rxd;
1513dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info, *next_buffer;
15145f450212SBruce Allan 	u32 length, staterr;
1515dee1ad47SJeff Kirsher 	unsigned int i;
1516dee1ad47SJeff Kirsher 	int cleaned_count = 0;
1517dee1ad47SJeff Kirsher 	bool cleaned = false;
1518dee1ad47SJeff Kirsher 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
151917e813ecSBruce Allan 	struct skb_shared_info *shinfo;
1520dee1ad47SJeff Kirsher 
1521dee1ad47SJeff Kirsher 	i = rx_ring->next_to_clean;
15225f450212SBruce Allan 	rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
15235f450212SBruce Allan 	staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
1524dee1ad47SJeff Kirsher 	buffer_info = &rx_ring->buffer_info[i];
1525dee1ad47SJeff Kirsher 
15265f450212SBruce Allan 	while (staterr & E1000_RXD_STAT_DD) {
1527dee1ad47SJeff Kirsher 		struct sk_buff *skb;
1528dee1ad47SJeff Kirsher 
1529dee1ad47SJeff Kirsher 		if (*work_done >= work_to_do)
1530dee1ad47SJeff Kirsher 			break;
1531dee1ad47SJeff Kirsher 		(*work_done)++;
1532837a1dbaSAlexander Duyck 		dma_rmb();	/* read descriptor and rx_buffer_info after status DD */
1533dee1ad47SJeff Kirsher 
1534dee1ad47SJeff Kirsher 		skb = buffer_info->skb;
1535dee1ad47SJeff Kirsher 		buffer_info->skb = NULL;
1536dee1ad47SJeff Kirsher 
1537dee1ad47SJeff Kirsher 		++i;
1538dee1ad47SJeff Kirsher 		if (i == rx_ring->count)
1539dee1ad47SJeff Kirsher 			i = 0;
15405f450212SBruce Allan 		next_rxd = E1000_RX_DESC_EXT(*rx_ring, i);
1541dee1ad47SJeff Kirsher 		prefetch(next_rxd);
1542dee1ad47SJeff Kirsher 
1543dee1ad47SJeff Kirsher 		next_buffer = &rx_ring->buffer_info[i];
1544dee1ad47SJeff Kirsher 
1545dee1ad47SJeff Kirsher 		cleaned = true;
1546dee1ad47SJeff Kirsher 		cleaned_count++;
1547dee1ad47SJeff Kirsher 		dma_unmap_page(&pdev->dev, buffer_info->dma, PAGE_SIZE,
1548dee1ad47SJeff Kirsher 			       DMA_FROM_DEVICE);
1549dee1ad47SJeff Kirsher 		buffer_info->dma = 0;
1550dee1ad47SJeff Kirsher 
15515f450212SBruce Allan 		length = le16_to_cpu(rx_desc->wb.upper.length);
1552dee1ad47SJeff Kirsher 
1553dee1ad47SJeff Kirsher 		/* errors is only valid for DD + EOP descriptors */
15545f450212SBruce Allan 		if (unlikely((staterr & E1000_RXD_STAT_EOP) &&
1555cf955e6cSBen Greear 			     ((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) &&
1556cf955e6cSBen Greear 			      !(netdev->features & NETIF_F_RXALL)))) {
1557dee1ad47SJeff Kirsher 			/* recycle both page and skb */
1558dee1ad47SJeff Kirsher 			buffer_info->skb = skb;
15595f450212SBruce Allan 			/* an error means any chain goes out the window too */
1560dee1ad47SJeff Kirsher 			if (rx_ring->rx_skb_top)
1561dee1ad47SJeff Kirsher 				dev_kfree_skb_irq(rx_ring->rx_skb_top);
1562dee1ad47SJeff Kirsher 			rx_ring->rx_skb_top = NULL;
1563dee1ad47SJeff Kirsher 			goto next_desc;
1564dee1ad47SJeff Kirsher 		}
1565dee1ad47SJeff Kirsher #define rxtop (rx_ring->rx_skb_top)
15665f450212SBruce Allan 		if (!(staterr & E1000_RXD_STAT_EOP)) {
1567dee1ad47SJeff Kirsher 			/* this descriptor is only the beginning (or middle) */
1568dee1ad47SJeff Kirsher 			if (!rxtop) {
1569dee1ad47SJeff Kirsher 				/* this is the beginning of a chain */
1570dee1ad47SJeff Kirsher 				rxtop = skb;
1571dee1ad47SJeff Kirsher 				skb_fill_page_desc(rxtop, 0, buffer_info->page,
1572dee1ad47SJeff Kirsher 						   0, length);
1573dee1ad47SJeff Kirsher 			} else {
1574dee1ad47SJeff Kirsher 				/* this is the middle of a chain */
157517e813ecSBruce Allan 				shinfo = skb_shinfo(rxtop);
157617e813ecSBruce Allan 				skb_fill_page_desc(rxtop, shinfo->nr_frags,
157717e813ecSBruce Allan 						   buffer_info->page, 0,
157817e813ecSBruce Allan 						   length);
1579dee1ad47SJeff Kirsher 				/* re-use the skb, only consumed the page */
1580dee1ad47SJeff Kirsher 				buffer_info->skb = skb;
1581dee1ad47SJeff Kirsher 			}
1582dee1ad47SJeff Kirsher 			e1000_consume_page(buffer_info, rxtop, length);
1583dee1ad47SJeff Kirsher 			goto next_desc;
1584dee1ad47SJeff Kirsher 		} else {
1585dee1ad47SJeff Kirsher 			if (rxtop) {
1586dee1ad47SJeff Kirsher 				/* end of the chain */
158717e813ecSBruce Allan 				shinfo = skb_shinfo(rxtop);
158817e813ecSBruce Allan 				skb_fill_page_desc(rxtop, shinfo->nr_frags,
158917e813ecSBruce Allan 						   buffer_info->page, 0,
159017e813ecSBruce Allan 						   length);
1591dee1ad47SJeff Kirsher 				/* re-use the current skb, we only consumed the
1592e921eb1aSBruce Allan 				 * page
1593e921eb1aSBruce Allan 				 */
1594dee1ad47SJeff Kirsher 				buffer_info->skb = skb;
1595dee1ad47SJeff Kirsher 				skb = rxtop;
1596dee1ad47SJeff Kirsher 				rxtop = NULL;
1597dee1ad47SJeff Kirsher 				e1000_consume_page(buffer_info, skb, length);
1598dee1ad47SJeff Kirsher 			} else {
1599dee1ad47SJeff Kirsher 				/* no chain, got EOP, this buf is the packet
1600e921eb1aSBruce Allan 				 * copybreak to save the put_page/alloc_page
1601e921eb1aSBruce Allan 				 */
1602dee1ad47SJeff Kirsher 				if (length <= copybreak &&
1603dee1ad47SJeff Kirsher 				    skb_tailroom(skb) >= length) {
1604ab400b0dSAnirudh Venkataramanan 					memcpy(skb_tail_pointer(skb),
1605ab400b0dSAnirudh Venkataramanan 					       page_address(buffer_info->page),
1606dee1ad47SJeff Kirsher 					       length);
1607dee1ad47SJeff Kirsher 					/* re-use the page, so don't erase
1608e921eb1aSBruce Allan 					 * buffer_info->page
1609e921eb1aSBruce Allan 					 */
1610dee1ad47SJeff Kirsher 					skb_put(skb, length);
1611dee1ad47SJeff Kirsher 				} else {
1612dee1ad47SJeff Kirsher 					skb_fill_page_desc(skb, 0,
1613dee1ad47SJeff Kirsher 							   buffer_info->page, 0,
1614dee1ad47SJeff Kirsher 							   length);
1615dee1ad47SJeff Kirsher 					e1000_consume_page(buffer_info, skb,
1616dee1ad47SJeff Kirsher 							   length);
1617dee1ad47SJeff Kirsher 				}
1618dee1ad47SJeff Kirsher 			}
1619dee1ad47SJeff Kirsher 		}
1620dee1ad47SJeff Kirsher 
16212e1706f2SBruce Allan 		/* Receive Checksum Offload */
16222e1706f2SBruce Allan 		e1000_rx_checksum(adapter, staterr, skb);
1623dee1ad47SJeff Kirsher 
162470495a50SBruce Allan 		e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
162570495a50SBruce Allan 
1626dee1ad47SJeff Kirsher 		/* probably a little skewed due to removing CRC */
1627dee1ad47SJeff Kirsher 		total_rx_bytes += skb->len;
1628dee1ad47SJeff Kirsher 		total_rx_packets++;
1629dee1ad47SJeff Kirsher 
1630dee1ad47SJeff Kirsher 		/* eth type trans needs skb->data to point to something */
1631dee1ad47SJeff Kirsher 		if (!pskb_may_pull(skb, ETH_HLEN)) {
1632dee1ad47SJeff Kirsher 			e_err("pskb_may_pull failed.\n");
1633dee1ad47SJeff Kirsher 			dev_kfree_skb_irq(skb);
1634dee1ad47SJeff Kirsher 			goto next_desc;
1635dee1ad47SJeff Kirsher 		}
1636dee1ad47SJeff Kirsher 
16375f450212SBruce Allan 		e1000_receive_skb(adapter, netdev, skb, staterr,
16385f450212SBruce Allan 				  rx_desc->wb.upper.vlan);
1639dee1ad47SJeff Kirsher 
1640dee1ad47SJeff Kirsher next_desc:
16415f450212SBruce Allan 		rx_desc->wb.upper.status_error &= cpu_to_le32(~0xFF);
1642dee1ad47SJeff Kirsher 
1643dee1ad47SJeff Kirsher 		/* return some buffers to hardware, one at a time is too slow */
1644dee1ad47SJeff Kirsher 		if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
164555aa6985SBruce Allan 			adapter->alloc_rx_buf(rx_ring, cleaned_count,
1646dee1ad47SJeff Kirsher 					      GFP_ATOMIC);
1647dee1ad47SJeff Kirsher 			cleaned_count = 0;
1648dee1ad47SJeff Kirsher 		}
1649dee1ad47SJeff Kirsher 
1650dee1ad47SJeff Kirsher 		/* use prefetched values */
1651dee1ad47SJeff Kirsher 		rx_desc = next_rxd;
1652dee1ad47SJeff Kirsher 		buffer_info = next_buffer;
16535f450212SBruce Allan 
16545f450212SBruce Allan 		staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
1655dee1ad47SJeff Kirsher 	}
1656dee1ad47SJeff Kirsher 	rx_ring->next_to_clean = i;
1657dee1ad47SJeff Kirsher 
1658dee1ad47SJeff Kirsher 	cleaned_count = e1000_desc_unused(rx_ring);
1659dee1ad47SJeff Kirsher 	if (cleaned_count)
166055aa6985SBruce Allan 		adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
1661dee1ad47SJeff Kirsher 
1662dee1ad47SJeff Kirsher 	adapter->total_rx_bytes += total_rx_bytes;
1663dee1ad47SJeff Kirsher 	adapter->total_rx_packets += total_rx_packets;
1664dee1ad47SJeff Kirsher 	return cleaned;
1665dee1ad47SJeff Kirsher }
1666dee1ad47SJeff Kirsher 
1667dee1ad47SJeff Kirsher /**
1668dee1ad47SJeff Kirsher  * e1000_clean_rx_ring - Free Rx Buffers per Queue
166955aa6985SBruce Allan  * @rx_ring: Rx descriptor ring
1670dee1ad47SJeff Kirsher  **/
e1000_clean_rx_ring(struct e1000_ring * rx_ring)167155aa6985SBruce Allan static void e1000_clean_rx_ring(struct e1000_ring *rx_ring)
1672dee1ad47SJeff Kirsher {
167355aa6985SBruce Allan 	struct e1000_adapter *adapter = rx_ring->adapter;
1674dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
1675dee1ad47SJeff Kirsher 	struct e1000_ps_page *ps_page;
1676dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
1677dee1ad47SJeff Kirsher 	unsigned int i, j;
1678dee1ad47SJeff Kirsher 
1679dee1ad47SJeff Kirsher 	/* Free all the Rx ring sk_buffs */
1680dee1ad47SJeff Kirsher 	for (i = 0; i < rx_ring->count; i++) {
1681dee1ad47SJeff Kirsher 		buffer_info = &rx_ring->buffer_info[i];
1682dee1ad47SJeff Kirsher 		if (buffer_info->dma) {
1683dee1ad47SJeff Kirsher 			if (adapter->clean_rx == e1000_clean_rx_irq)
1684dee1ad47SJeff Kirsher 				dma_unmap_single(&pdev->dev, buffer_info->dma,
1685dee1ad47SJeff Kirsher 						 adapter->rx_buffer_len,
1686dee1ad47SJeff Kirsher 						 DMA_FROM_DEVICE);
1687dee1ad47SJeff Kirsher 			else if (adapter->clean_rx == e1000_clean_jumbo_rx_irq)
1688dee1ad47SJeff Kirsher 				dma_unmap_page(&pdev->dev, buffer_info->dma,
1689f0ff4398SBruce Allan 					       PAGE_SIZE, DMA_FROM_DEVICE);
1690dee1ad47SJeff Kirsher 			else if (adapter->clean_rx == e1000_clean_rx_irq_ps)
1691dee1ad47SJeff Kirsher 				dma_unmap_single(&pdev->dev, buffer_info->dma,
1692dee1ad47SJeff Kirsher 						 adapter->rx_ps_bsize0,
1693dee1ad47SJeff Kirsher 						 DMA_FROM_DEVICE);
1694dee1ad47SJeff Kirsher 			buffer_info->dma = 0;
1695dee1ad47SJeff Kirsher 		}
1696dee1ad47SJeff Kirsher 
1697dee1ad47SJeff Kirsher 		if (buffer_info->page) {
1698dee1ad47SJeff Kirsher 			put_page(buffer_info->page);
1699dee1ad47SJeff Kirsher 			buffer_info->page = NULL;
1700dee1ad47SJeff Kirsher 		}
1701dee1ad47SJeff Kirsher 
1702dee1ad47SJeff Kirsher 		if (buffer_info->skb) {
1703dee1ad47SJeff Kirsher 			dev_kfree_skb(buffer_info->skb);
1704dee1ad47SJeff Kirsher 			buffer_info->skb = NULL;
1705dee1ad47SJeff Kirsher 		}
1706dee1ad47SJeff Kirsher 
1707dee1ad47SJeff Kirsher 		for (j = 0; j < PS_PAGE_BUFFERS; j++) {
1708dee1ad47SJeff Kirsher 			ps_page = &buffer_info->ps_pages[j];
1709dee1ad47SJeff Kirsher 			if (!ps_page->page)
1710dee1ad47SJeff Kirsher 				break;
1711dee1ad47SJeff Kirsher 			dma_unmap_page(&pdev->dev, ps_page->dma, PAGE_SIZE,
1712dee1ad47SJeff Kirsher 				       DMA_FROM_DEVICE);
1713dee1ad47SJeff Kirsher 			ps_page->dma = 0;
1714dee1ad47SJeff Kirsher 			put_page(ps_page->page);
1715dee1ad47SJeff Kirsher 			ps_page->page = NULL;
1716dee1ad47SJeff Kirsher 		}
1717dee1ad47SJeff Kirsher 	}
1718dee1ad47SJeff Kirsher 
1719dee1ad47SJeff Kirsher 	/* there also may be some cached data from a chained receive */
1720dee1ad47SJeff Kirsher 	if (rx_ring->rx_skb_top) {
1721dee1ad47SJeff Kirsher 		dev_kfree_skb(rx_ring->rx_skb_top);
1722dee1ad47SJeff Kirsher 		rx_ring->rx_skb_top = NULL;
1723dee1ad47SJeff Kirsher 	}
1724dee1ad47SJeff Kirsher 
1725dee1ad47SJeff Kirsher 	/* Zero out the descriptor ring */
1726dee1ad47SJeff Kirsher 	memset(rx_ring->desc, 0, rx_ring->size);
1727dee1ad47SJeff Kirsher 
1728dee1ad47SJeff Kirsher 	rx_ring->next_to_clean = 0;
1729dee1ad47SJeff Kirsher 	rx_ring->next_to_use = 0;
1730dee1ad47SJeff Kirsher 	adapter->flags2 &= ~FLAG2_IS_DISCARDING;
1731dee1ad47SJeff Kirsher }
1732dee1ad47SJeff Kirsher 
e1000e_downshift_workaround(struct work_struct * work)1733dee1ad47SJeff Kirsher static void e1000e_downshift_workaround(struct work_struct *work)
1734dee1ad47SJeff Kirsher {
1735dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = container_of(work,
173617e813ecSBruce Allan 						     struct e1000_adapter,
173717e813ecSBruce Allan 						     downshift_task);
1738dee1ad47SJeff Kirsher 
1739dee1ad47SJeff Kirsher 	if (test_bit(__E1000_DOWN, &adapter->state))
1740dee1ad47SJeff Kirsher 		return;
1741dee1ad47SJeff Kirsher 
1742dee1ad47SJeff Kirsher 	e1000e_gig_downshift_workaround_ich8lan(&adapter->hw);
1743dee1ad47SJeff Kirsher }
1744dee1ad47SJeff Kirsher 
1745dee1ad47SJeff Kirsher /**
1746dee1ad47SJeff Kirsher  * e1000_intr_msi - Interrupt Handler
1747dee1ad47SJeff Kirsher  * @irq: interrupt number
1748dee1ad47SJeff Kirsher  * @data: pointer to a network interface device structure
1749dee1ad47SJeff Kirsher  **/
e1000_intr_msi(int __always_unused irq,void * data)17508bb62869SBruce Allan static irqreturn_t e1000_intr_msi(int __always_unused irq, void *data)
1751dee1ad47SJeff Kirsher {
1752dee1ad47SJeff Kirsher 	struct net_device *netdev = data;
1753dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
1754dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
1755dee1ad47SJeff Kirsher 	u32 icr = er32(ICR);
1756dee1ad47SJeff Kirsher 
1757e921eb1aSBruce Allan 	/* read ICR disables interrupts using IAM */
1758dee1ad47SJeff Kirsher 	if (icr & E1000_ICR_LSC) {
1759f92518ddSBruce Allan 		hw->mac.get_link_status = true;
1760e921eb1aSBruce Allan 		/* ICH8 workaround-- Call gig speed drop workaround on cable
1761dee1ad47SJeff Kirsher 		 * disconnect (LSC) before accessing any PHY registers
1762dee1ad47SJeff Kirsher 		 */
1763dee1ad47SJeff Kirsher 		if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
1764dee1ad47SJeff Kirsher 		    (!(er32(STATUS) & E1000_STATUS_LU)))
1765dee1ad47SJeff Kirsher 			schedule_work(&adapter->downshift_task);
1766dee1ad47SJeff Kirsher 
1767e921eb1aSBruce Allan 		/* 80003ES2LAN workaround-- For packet buffer work-around on
1768dee1ad47SJeff Kirsher 		 * link down event; disable receives here in the ISR and reset
1769dee1ad47SJeff Kirsher 		 * adapter in watchdog
1770dee1ad47SJeff Kirsher 		 */
1771dee1ad47SJeff Kirsher 		if (netif_carrier_ok(netdev) &&
1772dee1ad47SJeff Kirsher 		    adapter->flags & FLAG_RX_NEEDS_RESTART) {
1773dee1ad47SJeff Kirsher 			/* disable receives */
1774dee1ad47SJeff Kirsher 			u32 rctl = er32(RCTL);
17756cf08d1cSDavid Ertman 
1776dee1ad47SJeff Kirsher 			ew32(RCTL, rctl & ~E1000_RCTL_EN);
177712d43f7dSBruce Allan 			adapter->flags |= FLAG_RESTART_NOW;
1778dee1ad47SJeff Kirsher 		}
1779dee1ad47SJeff Kirsher 		/* guard against interrupt when we're going down */
1780dee1ad47SJeff Kirsher 		if (!test_bit(__E1000_DOWN, &adapter->state))
1781d5ad7a6aSJeff Kirsher 			mod_timer(&adapter->watchdog_timer, jiffies + 1);
1782dee1ad47SJeff Kirsher 	}
1783dee1ad47SJeff Kirsher 
178494fb848bSBruce Allan 	/* Reset on uncorrectable ECC error */
1785c8744f44SSasha Neftin 	if ((icr & E1000_ICR_ECCER) && (hw->mac.type >= e1000_pch_lpt)) {
178694fb848bSBruce Allan 		u32 pbeccsts = er32(PBECCSTS);
178794fb848bSBruce Allan 
178894fb848bSBruce Allan 		adapter->corr_errors +=
178994fb848bSBruce Allan 		    pbeccsts & E1000_PBECCSTS_CORR_ERR_CNT_MASK;
179094fb848bSBruce Allan 		adapter->uncorr_errors +=
1791b9a45254SJesse Brandeburg 		    FIELD_GET(E1000_PBECCSTS_UNCORR_ERR_CNT_MASK, pbeccsts);
179294fb848bSBruce Allan 
179394fb848bSBruce Allan 		/* Do the reset outside of interrupt context */
179494fb848bSBruce Allan 		schedule_work(&adapter->reset_task);
179594fb848bSBruce Allan 
179694fb848bSBruce Allan 		/* return immediately since reset is imminent */
179794fb848bSBruce Allan 		return IRQ_HANDLED;
179894fb848bSBruce Allan 	}
179994fb848bSBruce Allan 
1800dee1ad47SJeff Kirsher 	if (napi_schedule_prep(&adapter->napi)) {
1801dee1ad47SJeff Kirsher 		adapter->total_tx_bytes = 0;
1802dee1ad47SJeff Kirsher 		adapter->total_tx_packets = 0;
1803dee1ad47SJeff Kirsher 		adapter->total_rx_bytes = 0;
1804dee1ad47SJeff Kirsher 		adapter->total_rx_packets = 0;
1805dee1ad47SJeff Kirsher 		__napi_schedule(&adapter->napi);
1806dee1ad47SJeff Kirsher 	}
1807dee1ad47SJeff Kirsher 
1808dee1ad47SJeff Kirsher 	return IRQ_HANDLED;
1809dee1ad47SJeff Kirsher }
1810dee1ad47SJeff Kirsher 
1811dee1ad47SJeff Kirsher /**
1812dee1ad47SJeff Kirsher  * e1000_intr - Interrupt Handler
1813dee1ad47SJeff Kirsher  * @irq: interrupt number
1814dee1ad47SJeff Kirsher  * @data: pointer to a network interface device structure
1815dee1ad47SJeff Kirsher  **/
e1000_intr(int __always_unused irq,void * data)18168bb62869SBruce Allan static irqreturn_t e1000_intr(int __always_unused irq, void *data)
1817dee1ad47SJeff Kirsher {
1818dee1ad47SJeff Kirsher 	struct net_device *netdev = data;
1819dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
1820dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
1821dee1ad47SJeff Kirsher 	u32 rctl, icr = er32(ICR);
1822dee1ad47SJeff Kirsher 
1823dee1ad47SJeff Kirsher 	if (!icr || test_bit(__E1000_DOWN, &adapter->state))
1824dee1ad47SJeff Kirsher 		return IRQ_NONE;	/* Not our interrupt */
1825dee1ad47SJeff Kirsher 
1826e921eb1aSBruce Allan 	/* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
1827dee1ad47SJeff Kirsher 	 * not set, then the adapter didn't send an interrupt
1828dee1ad47SJeff Kirsher 	 */
1829dee1ad47SJeff Kirsher 	if (!(icr & E1000_ICR_INT_ASSERTED))
1830dee1ad47SJeff Kirsher 		return IRQ_NONE;
1831dee1ad47SJeff Kirsher 
1832e921eb1aSBruce Allan 	/* Interrupt Auto-Mask...upon reading ICR,
1833dee1ad47SJeff Kirsher 	 * interrupts are masked.  No need for the
1834dee1ad47SJeff Kirsher 	 * IMC write
1835dee1ad47SJeff Kirsher 	 */
1836dee1ad47SJeff Kirsher 
1837dee1ad47SJeff Kirsher 	if (icr & E1000_ICR_LSC) {
1838f92518ddSBruce Allan 		hw->mac.get_link_status = true;
1839e921eb1aSBruce Allan 		/* ICH8 workaround-- Call gig speed drop workaround on cable
1840dee1ad47SJeff Kirsher 		 * disconnect (LSC) before accessing any PHY registers
1841dee1ad47SJeff Kirsher 		 */
1842dee1ad47SJeff Kirsher 		if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
1843dee1ad47SJeff Kirsher 		    (!(er32(STATUS) & E1000_STATUS_LU)))
1844dee1ad47SJeff Kirsher 			schedule_work(&adapter->downshift_task);
1845dee1ad47SJeff Kirsher 
1846e921eb1aSBruce Allan 		/* 80003ES2LAN workaround--
1847dee1ad47SJeff Kirsher 		 * For packet buffer work-around on link down event;
1848dee1ad47SJeff Kirsher 		 * disable receives here in the ISR and
1849dee1ad47SJeff Kirsher 		 * reset adapter in watchdog
1850dee1ad47SJeff Kirsher 		 */
1851dee1ad47SJeff Kirsher 		if (netif_carrier_ok(netdev) &&
1852dee1ad47SJeff Kirsher 		    (adapter->flags & FLAG_RX_NEEDS_RESTART)) {
1853dee1ad47SJeff Kirsher 			/* disable receives */
1854dee1ad47SJeff Kirsher 			rctl = er32(RCTL);
1855dee1ad47SJeff Kirsher 			ew32(RCTL, rctl & ~E1000_RCTL_EN);
185612d43f7dSBruce Allan 			adapter->flags |= FLAG_RESTART_NOW;
1857dee1ad47SJeff Kirsher 		}
1858dee1ad47SJeff Kirsher 		/* guard against interrupt when we're going down */
1859dee1ad47SJeff Kirsher 		if (!test_bit(__E1000_DOWN, &adapter->state))
1860d5ad7a6aSJeff Kirsher 			mod_timer(&adapter->watchdog_timer, jiffies + 1);
1861dee1ad47SJeff Kirsher 	}
1862dee1ad47SJeff Kirsher 
186394fb848bSBruce Allan 	/* Reset on uncorrectable ECC error */
1864c8744f44SSasha Neftin 	if ((icr & E1000_ICR_ECCER) && (hw->mac.type >= e1000_pch_lpt)) {
186594fb848bSBruce Allan 		u32 pbeccsts = er32(PBECCSTS);
186694fb848bSBruce Allan 
186794fb848bSBruce Allan 		adapter->corr_errors +=
186894fb848bSBruce Allan 		    pbeccsts & E1000_PBECCSTS_CORR_ERR_CNT_MASK;
186994fb848bSBruce Allan 		adapter->uncorr_errors +=
1870b9a45254SJesse Brandeburg 		    FIELD_GET(E1000_PBECCSTS_UNCORR_ERR_CNT_MASK, pbeccsts);
187194fb848bSBruce Allan 
187294fb848bSBruce Allan 		/* Do the reset outside of interrupt context */
187394fb848bSBruce Allan 		schedule_work(&adapter->reset_task);
187494fb848bSBruce Allan 
187594fb848bSBruce Allan 		/* return immediately since reset is imminent */
187694fb848bSBruce Allan 		return IRQ_HANDLED;
187794fb848bSBruce Allan 	}
187894fb848bSBruce Allan 
1879dee1ad47SJeff Kirsher 	if (napi_schedule_prep(&adapter->napi)) {
1880dee1ad47SJeff Kirsher 		adapter->total_tx_bytes = 0;
1881dee1ad47SJeff Kirsher 		adapter->total_tx_packets = 0;
1882dee1ad47SJeff Kirsher 		adapter->total_rx_bytes = 0;
1883dee1ad47SJeff Kirsher 		adapter->total_rx_packets = 0;
1884dee1ad47SJeff Kirsher 		__napi_schedule(&adapter->napi);
1885dee1ad47SJeff Kirsher 	}
1886dee1ad47SJeff Kirsher 
1887dee1ad47SJeff Kirsher 	return IRQ_HANDLED;
1888dee1ad47SJeff Kirsher }
1889dee1ad47SJeff Kirsher 
e1000_msix_other(int __always_unused irq,void * data)18908bb62869SBruce Allan static irqreturn_t e1000_msix_other(int __always_unused irq, void *data)
1891dee1ad47SJeff Kirsher {
1892dee1ad47SJeff Kirsher 	struct net_device *netdev = data;
1893dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
1894dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
1895116f4a64SBenjamin Poirier 	u32 icr = er32(ICR);
1896745d0bd3SBenjamin Poirier 
1897361a954eSBenjamin Poirier 	if (icr & adapter->eiac_mask)
1898361a954eSBenjamin Poirier 		ew32(ICS, (icr & adapter->eiac_mask));
1899361a954eSBenjamin Poirier 
19004aea7a5cSBenjamin Poirier 	if (icr & E1000_ICR_LSC) {
19014aea7a5cSBenjamin Poirier 		hw->mac.get_link_status = true;
19024aea7a5cSBenjamin Poirier 		/* guard against interrupt when we're going down */
19034aea7a5cSBenjamin Poirier 		if (!test_bit(__E1000_DOWN, &adapter->state))
1904d5ad7a6aSJeff Kirsher 			mod_timer(&adapter->watchdog_timer, jiffies + 1);
19054aea7a5cSBenjamin Poirier 	}
19064aea7a5cSBenjamin Poirier 
19071f0ea197SBenjamin Poirier 	if (!test_bit(__E1000_DOWN, &adapter->state))
1908116f4a64SBenjamin Poirier 		ew32(IMS, E1000_IMS_OTHER | IMS_OTHER_MASK);
1909dee1ad47SJeff Kirsher 
1910dee1ad47SJeff Kirsher 	return IRQ_HANDLED;
1911dee1ad47SJeff Kirsher }
1912dee1ad47SJeff Kirsher 
e1000_intr_msix_tx(int __always_unused irq,void * data)19138bb62869SBruce Allan static irqreturn_t e1000_intr_msix_tx(int __always_unused irq, void *data)
1914dee1ad47SJeff Kirsher {
1915dee1ad47SJeff Kirsher 	struct net_device *netdev = data;
1916dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
1917dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
1918dee1ad47SJeff Kirsher 	struct e1000_ring *tx_ring = adapter->tx_ring;
1919dee1ad47SJeff Kirsher 
1920dee1ad47SJeff Kirsher 	adapter->total_tx_bytes = 0;
1921dee1ad47SJeff Kirsher 	adapter->total_tx_packets = 0;
1922dee1ad47SJeff Kirsher 
192355aa6985SBruce Allan 	if (!e1000_clean_tx_irq(tx_ring))
1924dee1ad47SJeff Kirsher 		/* Ring was not completely cleaned, so fire another interrupt */
1925dee1ad47SJeff Kirsher 		ew32(ICS, tx_ring->ims_val);
1926dee1ad47SJeff Kirsher 
19270a8047acSBenjamin Poirier 	if (!test_bit(__E1000_DOWN, &adapter->state))
19280a8047acSBenjamin Poirier 		ew32(IMS, adapter->tx_ring->ims_val);
19290a8047acSBenjamin Poirier 
1930dee1ad47SJeff Kirsher 	return IRQ_HANDLED;
1931dee1ad47SJeff Kirsher }
1932dee1ad47SJeff Kirsher 
e1000_intr_msix_rx(int __always_unused irq,void * data)19338bb62869SBruce Allan static irqreturn_t e1000_intr_msix_rx(int __always_unused irq, void *data)
1934dee1ad47SJeff Kirsher {
1935dee1ad47SJeff Kirsher 	struct net_device *netdev = data;
1936dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
193755aa6985SBruce Allan 	struct e1000_ring *rx_ring = adapter->rx_ring;
1938dee1ad47SJeff Kirsher 
1939dee1ad47SJeff Kirsher 	/* Write the ITR value calculated at the end of the
1940dee1ad47SJeff Kirsher 	 * previous interrupt.
1941dee1ad47SJeff Kirsher 	 */
194255aa6985SBruce Allan 	if (rx_ring->set_itr) {
1943b77ac46bSDmitry Fleytman 		u32 itr = rx_ring->itr_val ?
1944b77ac46bSDmitry Fleytman 			  1000000000 / (rx_ring->itr_val * 256) : 0;
1945b77ac46bSDmitry Fleytman 
1946b77ac46bSDmitry Fleytman 		writel(itr, rx_ring->itr_register);
194755aa6985SBruce Allan 		rx_ring->set_itr = 0;
1948dee1ad47SJeff Kirsher 	}
1949dee1ad47SJeff Kirsher 
1950dee1ad47SJeff Kirsher 	if (napi_schedule_prep(&adapter->napi)) {
1951dee1ad47SJeff Kirsher 		adapter->total_rx_bytes = 0;
1952dee1ad47SJeff Kirsher 		adapter->total_rx_packets = 0;
1953dee1ad47SJeff Kirsher 		__napi_schedule(&adapter->napi);
1954dee1ad47SJeff Kirsher 	}
1955dee1ad47SJeff Kirsher 	return IRQ_HANDLED;
1956dee1ad47SJeff Kirsher }
1957dee1ad47SJeff Kirsher 
1958dee1ad47SJeff Kirsher /**
1959dee1ad47SJeff Kirsher  * e1000_configure_msix - Configure MSI-X hardware
1960b50f7bcaSJesse Brandeburg  * @adapter: board private structure
1961dee1ad47SJeff Kirsher  *
1962dee1ad47SJeff Kirsher  * e1000_configure_msix sets up the hardware to properly
1963dee1ad47SJeff Kirsher  * generate MSI-X interrupts.
1964dee1ad47SJeff Kirsher  **/
e1000_configure_msix(struct e1000_adapter * adapter)1965dee1ad47SJeff Kirsher static void e1000_configure_msix(struct e1000_adapter *adapter)
1966dee1ad47SJeff Kirsher {
1967dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
1968dee1ad47SJeff Kirsher 	struct e1000_ring *rx_ring = adapter->rx_ring;
1969dee1ad47SJeff Kirsher 	struct e1000_ring *tx_ring = adapter->tx_ring;
1970dee1ad47SJeff Kirsher 	int vector = 0;
1971dee1ad47SJeff Kirsher 	u32 ctrl_ext, ivar = 0;
1972dee1ad47SJeff Kirsher 
1973dee1ad47SJeff Kirsher 	adapter->eiac_mask = 0;
1974dee1ad47SJeff Kirsher 
1975dee1ad47SJeff Kirsher 	/* Workaround issue with spurious interrupts on 82574 in MSI-X mode */
1976dee1ad47SJeff Kirsher 	if (hw->mac.type == e1000_82574) {
1977dee1ad47SJeff Kirsher 		u32 rfctl = er32(RFCTL);
19786cf08d1cSDavid Ertman 
1979dee1ad47SJeff Kirsher 		rfctl |= E1000_RFCTL_ACK_DIS;
1980dee1ad47SJeff Kirsher 		ew32(RFCTL, rfctl);
1981dee1ad47SJeff Kirsher 	}
1982dee1ad47SJeff Kirsher 
1983dee1ad47SJeff Kirsher 	/* Configure Rx vector */
1984dee1ad47SJeff Kirsher 	rx_ring->ims_val = E1000_IMS_RXQ0;
1985dee1ad47SJeff Kirsher 	adapter->eiac_mask |= rx_ring->ims_val;
1986dee1ad47SJeff Kirsher 	if (rx_ring->itr_val)
1987dee1ad47SJeff Kirsher 		writel(1000000000 / (rx_ring->itr_val * 256),
1988c5083cf6SBruce Allan 		       rx_ring->itr_register);
1989dee1ad47SJeff Kirsher 	else
1990c5083cf6SBruce Allan 		writel(1, rx_ring->itr_register);
1991dee1ad47SJeff Kirsher 	ivar = E1000_IVAR_INT_ALLOC_VALID | vector;
1992dee1ad47SJeff Kirsher 
1993dee1ad47SJeff Kirsher 	/* Configure Tx vector */
1994dee1ad47SJeff Kirsher 	tx_ring->ims_val = E1000_IMS_TXQ0;
1995dee1ad47SJeff Kirsher 	vector++;
1996dee1ad47SJeff Kirsher 	if (tx_ring->itr_val)
1997dee1ad47SJeff Kirsher 		writel(1000000000 / (tx_ring->itr_val * 256),
1998c5083cf6SBruce Allan 		       tx_ring->itr_register);
1999dee1ad47SJeff Kirsher 	else
2000c5083cf6SBruce Allan 		writel(1, tx_ring->itr_register);
2001dee1ad47SJeff Kirsher 	adapter->eiac_mask |= tx_ring->ims_val;
2002dee1ad47SJeff Kirsher 	ivar |= ((E1000_IVAR_INT_ALLOC_VALID | vector) << 8);
2003dee1ad47SJeff Kirsher 
2004dee1ad47SJeff Kirsher 	/* set vector for Other Causes, e.g. link changes */
2005dee1ad47SJeff Kirsher 	vector++;
2006dee1ad47SJeff Kirsher 	ivar |= ((E1000_IVAR_INT_ALLOC_VALID | vector) << 16);
2007dee1ad47SJeff Kirsher 	if (rx_ring->itr_val)
2008dee1ad47SJeff Kirsher 		writel(1000000000 / (rx_ring->itr_val * 256),
2009dee1ad47SJeff Kirsher 		       hw->hw_addr + E1000_EITR_82574(vector));
2010dee1ad47SJeff Kirsher 	else
2011dee1ad47SJeff Kirsher 		writel(1, hw->hw_addr + E1000_EITR_82574(vector));
2012dee1ad47SJeff Kirsher 
2013dee1ad47SJeff Kirsher 	/* Cause Tx interrupts on every write back */
201418dd2392SJacob Keller 	ivar |= BIT(31);
2015dee1ad47SJeff Kirsher 
2016dee1ad47SJeff Kirsher 	ew32(IVAR, ivar);
2017dee1ad47SJeff Kirsher 
2018dee1ad47SJeff Kirsher 	/* enable MSI-X PBA support */
20190a8047acSBenjamin Poirier 	ctrl_ext = er32(CTRL_EXT) & ~E1000_CTRL_EXT_IAME;
20200a8047acSBenjamin Poirier 	ctrl_ext |= E1000_CTRL_EXT_PBA_CLR | E1000_CTRL_EXT_EIAME;
2021dee1ad47SJeff Kirsher 	ew32(CTRL_EXT, ctrl_ext);
2022dee1ad47SJeff Kirsher 	e1e_flush();
2023dee1ad47SJeff Kirsher }
2024dee1ad47SJeff Kirsher 
e1000e_reset_interrupt_capability(struct e1000_adapter * adapter)2025dee1ad47SJeff Kirsher void e1000e_reset_interrupt_capability(struct e1000_adapter *adapter)
2026dee1ad47SJeff Kirsher {
2027dee1ad47SJeff Kirsher 	if (adapter->msix_entries) {
2028dee1ad47SJeff Kirsher 		pci_disable_msix(adapter->pdev);
2029dee1ad47SJeff Kirsher 		kfree(adapter->msix_entries);
2030dee1ad47SJeff Kirsher 		adapter->msix_entries = NULL;
2031dee1ad47SJeff Kirsher 	} else if (adapter->flags & FLAG_MSI_ENABLED) {
2032dee1ad47SJeff Kirsher 		pci_disable_msi(adapter->pdev);
2033dee1ad47SJeff Kirsher 		adapter->flags &= ~FLAG_MSI_ENABLED;
2034dee1ad47SJeff Kirsher 	}
2035dee1ad47SJeff Kirsher }
2036dee1ad47SJeff Kirsher 
2037dee1ad47SJeff Kirsher /**
2038dee1ad47SJeff Kirsher  * e1000e_set_interrupt_capability - set MSI or MSI-X if supported
2039b50f7bcaSJesse Brandeburg  * @adapter: board private structure
2040dee1ad47SJeff Kirsher  *
2041dee1ad47SJeff Kirsher  * Attempt to configure interrupts using the best available
2042dee1ad47SJeff Kirsher  * capabilities of the hardware and kernel.
2043dee1ad47SJeff Kirsher  **/
e1000e_set_interrupt_capability(struct e1000_adapter * adapter)2044dee1ad47SJeff Kirsher void e1000e_set_interrupt_capability(struct e1000_adapter *adapter)
2045dee1ad47SJeff Kirsher {
2046dee1ad47SJeff Kirsher 	int err;
2047dee1ad47SJeff Kirsher 	int i;
2048dee1ad47SJeff Kirsher 
2049dee1ad47SJeff Kirsher 	switch (adapter->int_mode) {
2050dee1ad47SJeff Kirsher 	case E1000E_INT_MODE_MSIX:
2051dee1ad47SJeff Kirsher 		if (adapter->flags & FLAG_HAS_MSIX) {
2052dee1ad47SJeff Kirsher 			adapter->num_vectors = 3; /* RxQ0, TxQ0 and other */
2053dee1ad47SJeff Kirsher 			adapter->msix_entries = kcalloc(adapter->num_vectors,
205417e813ecSBruce Allan 							sizeof(struct
205517e813ecSBruce Allan 							       msix_entry),
2056dee1ad47SJeff Kirsher 							GFP_KERNEL);
2057dee1ad47SJeff Kirsher 			if (adapter->msix_entries) {
20580cc7c959SAlexander Gordeev 				struct e1000_adapter *a = adapter;
20590cc7c959SAlexander Gordeev 
2060dee1ad47SJeff Kirsher 				for (i = 0; i < adapter->num_vectors; i++)
2061dee1ad47SJeff Kirsher 					adapter->msix_entries[i].entry = i;
2062dee1ad47SJeff Kirsher 
20630cc7c959SAlexander Gordeev 				err = pci_enable_msix_range(a->pdev,
20640cc7c959SAlexander Gordeev 							    a->msix_entries,
20650cc7c959SAlexander Gordeev 							    a->num_vectors,
20660cc7c959SAlexander Gordeev 							    a->num_vectors);
20670cc7c959SAlexander Gordeev 				if (err > 0)
2068dee1ad47SJeff Kirsher 					return;
2069dee1ad47SJeff Kirsher 			}
2070dee1ad47SJeff Kirsher 			/* MSI-X failed, so fall through and try MSI */
2071ef456f85SJeff Kirsher 			e_err("Failed to initialize MSI-X interrupts.  Falling back to MSI interrupts.\n");
2072dee1ad47SJeff Kirsher 			e1000e_reset_interrupt_capability(adapter);
2073dee1ad47SJeff Kirsher 		}
2074dee1ad47SJeff Kirsher 		adapter->int_mode = E1000E_INT_MODE_MSI;
20755463fce6SJeff Kirsher 		fallthrough;
2076dee1ad47SJeff Kirsher 	case E1000E_INT_MODE_MSI:
2077dee1ad47SJeff Kirsher 		if (!pci_enable_msi(adapter->pdev)) {
2078dee1ad47SJeff Kirsher 			adapter->flags |= FLAG_MSI_ENABLED;
2079dee1ad47SJeff Kirsher 		} else {
2080dee1ad47SJeff Kirsher 			adapter->int_mode = E1000E_INT_MODE_LEGACY;
2081ef456f85SJeff Kirsher 			e_err("Failed to initialize MSI interrupts.  Falling back to legacy interrupts.\n");
2082dee1ad47SJeff Kirsher 		}
20835463fce6SJeff Kirsher 		fallthrough;
2084dee1ad47SJeff Kirsher 	case E1000E_INT_MODE_LEGACY:
2085dee1ad47SJeff Kirsher 		/* Don't do anything; this is the system default */
2086dee1ad47SJeff Kirsher 		break;
2087dee1ad47SJeff Kirsher 	}
2088dee1ad47SJeff Kirsher 
2089dee1ad47SJeff Kirsher 	/* store the number of vectors being used */
2090dee1ad47SJeff Kirsher 	adapter->num_vectors = 1;
2091dee1ad47SJeff Kirsher }
2092dee1ad47SJeff Kirsher 
2093dee1ad47SJeff Kirsher /**
2094dee1ad47SJeff Kirsher  * e1000_request_msix - Initialize MSI-X interrupts
2095b50f7bcaSJesse Brandeburg  * @adapter: board private structure
2096dee1ad47SJeff Kirsher  *
2097dee1ad47SJeff Kirsher  * e1000_request_msix allocates MSI-X vectors and requests interrupts from the
2098dee1ad47SJeff Kirsher  * kernel.
2099dee1ad47SJeff Kirsher  **/
e1000_request_msix(struct e1000_adapter * adapter)2100dee1ad47SJeff Kirsher static int e1000_request_msix(struct e1000_adapter *adapter)
2101dee1ad47SJeff Kirsher {
2102dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2103dee1ad47SJeff Kirsher 	int err = 0, vector = 0;
2104dee1ad47SJeff Kirsher 
2105dee1ad47SJeff Kirsher 	if (strlen(netdev->name) < (IFNAMSIZ - 5))
2106dee1ad47SJeff Kirsher 		snprintf(adapter->rx_ring->name,
2107dee1ad47SJeff Kirsher 			 sizeof(adapter->rx_ring->name) - 1,
2108135e7245SFlorian Fainelli 			 "%.14s-rx-0", netdev->name);
2109dee1ad47SJeff Kirsher 	else
2110dee1ad47SJeff Kirsher 		memcpy(adapter->rx_ring->name, netdev->name, IFNAMSIZ);
2111dee1ad47SJeff Kirsher 	err = request_irq(adapter->msix_entries[vector].vector,
2112dee1ad47SJeff Kirsher 			  e1000_intr_msix_rx, 0, adapter->rx_ring->name,
2113dee1ad47SJeff Kirsher 			  netdev);
2114dee1ad47SJeff Kirsher 	if (err)
21155015e53aSBruce Allan 		return err;
2116c5083cf6SBruce Allan 	adapter->rx_ring->itr_register = adapter->hw.hw_addr +
2117c5083cf6SBruce Allan 	    E1000_EITR_82574(vector);
2118dee1ad47SJeff Kirsher 	adapter->rx_ring->itr_val = adapter->itr;
2119dee1ad47SJeff Kirsher 	vector++;
2120dee1ad47SJeff Kirsher 
2121dee1ad47SJeff Kirsher 	if (strlen(netdev->name) < (IFNAMSIZ - 5))
2122dee1ad47SJeff Kirsher 		snprintf(adapter->tx_ring->name,
2123dee1ad47SJeff Kirsher 			 sizeof(adapter->tx_ring->name) - 1,
2124135e7245SFlorian Fainelli 			 "%.14s-tx-0", netdev->name);
2125dee1ad47SJeff Kirsher 	else
2126dee1ad47SJeff Kirsher 		memcpy(adapter->tx_ring->name, netdev->name, IFNAMSIZ);
2127dee1ad47SJeff Kirsher 	err = request_irq(adapter->msix_entries[vector].vector,
2128dee1ad47SJeff Kirsher 			  e1000_intr_msix_tx, 0, adapter->tx_ring->name,
2129dee1ad47SJeff Kirsher 			  netdev);
2130dee1ad47SJeff Kirsher 	if (err)
21315015e53aSBruce Allan 		return err;
2132c5083cf6SBruce Allan 	adapter->tx_ring->itr_register = adapter->hw.hw_addr +
2133c5083cf6SBruce Allan 	    E1000_EITR_82574(vector);
2134dee1ad47SJeff Kirsher 	adapter->tx_ring->itr_val = adapter->itr;
2135dee1ad47SJeff Kirsher 	vector++;
2136dee1ad47SJeff Kirsher 
2137dee1ad47SJeff Kirsher 	err = request_irq(adapter->msix_entries[vector].vector,
2138dee1ad47SJeff Kirsher 			  e1000_msix_other, 0, netdev->name, netdev);
2139dee1ad47SJeff Kirsher 	if (err)
21405015e53aSBruce Allan 		return err;
2141dee1ad47SJeff Kirsher 
2142dee1ad47SJeff Kirsher 	e1000_configure_msix(adapter);
21435015e53aSBruce Allan 
2144dee1ad47SJeff Kirsher 	return 0;
2145dee1ad47SJeff Kirsher }
2146dee1ad47SJeff Kirsher 
2147dee1ad47SJeff Kirsher /**
2148dee1ad47SJeff Kirsher  * e1000_request_irq - initialize interrupts
2149b50f7bcaSJesse Brandeburg  * @adapter: board private structure
2150dee1ad47SJeff Kirsher  *
2151dee1ad47SJeff Kirsher  * Attempts to configure interrupts using the best available
2152dee1ad47SJeff Kirsher  * capabilities of the hardware and kernel.
2153dee1ad47SJeff Kirsher  **/
e1000_request_irq(struct e1000_adapter * adapter)2154dee1ad47SJeff Kirsher static int e1000_request_irq(struct e1000_adapter *adapter)
2155dee1ad47SJeff Kirsher {
2156dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2157dee1ad47SJeff Kirsher 	int err;
2158dee1ad47SJeff Kirsher 
2159dee1ad47SJeff Kirsher 	if (adapter->msix_entries) {
2160dee1ad47SJeff Kirsher 		err = e1000_request_msix(adapter);
2161dee1ad47SJeff Kirsher 		if (!err)
2162dee1ad47SJeff Kirsher 			return err;
2163dee1ad47SJeff Kirsher 		/* fall back to MSI */
2164dee1ad47SJeff Kirsher 		e1000e_reset_interrupt_capability(adapter);
2165dee1ad47SJeff Kirsher 		adapter->int_mode = E1000E_INT_MODE_MSI;
2166dee1ad47SJeff Kirsher 		e1000e_set_interrupt_capability(adapter);
2167dee1ad47SJeff Kirsher 	}
2168dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_MSI_ENABLED) {
2169dee1ad47SJeff Kirsher 		err = request_irq(adapter->pdev->irq, e1000_intr_msi, 0,
2170dee1ad47SJeff Kirsher 				  netdev->name, netdev);
2171dee1ad47SJeff Kirsher 		if (!err)
2172dee1ad47SJeff Kirsher 			return err;
2173dee1ad47SJeff Kirsher 
2174dee1ad47SJeff Kirsher 		/* fall back to legacy interrupt */
2175dee1ad47SJeff Kirsher 		e1000e_reset_interrupt_capability(adapter);
2176dee1ad47SJeff Kirsher 		adapter->int_mode = E1000E_INT_MODE_LEGACY;
2177dee1ad47SJeff Kirsher 	}
2178dee1ad47SJeff Kirsher 
2179dee1ad47SJeff Kirsher 	err = request_irq(adapter->pdev->irq, e1000_intr, IRQF_SHARED,
2180dee1ad47SJeff Kirsher 			  netdev->name, netdev);
2181dee1ad47SJeff Kirsher 	if (err)
2182dee1ad47SJeff Kirsher 		e_err("Unable to allocate interrupt, Error: %d\n", err);
2183dee1ad47SJeff Kirsher 
2184dee1ad47SJeff Kirsher 	return err;
2185dee1ad47SJeff Kirsher }
2186dee1ad47SJeff Kirsher 
e1000_free_irq(struct e1000_adapter * adapter)2187dee1ad47SJeff Kirsher static void e1000_free_irq(struct e1000_adapter *adapter)
2188dee1ad47SJeff Kirsher {
2189dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2190dee1ad47SJeff Kirsher 
2191dee1ad47SJeff Kirsher 	if (adapter->msix_entries) {
2192dee1ad47SJeff Kirsher 		int vector = 0;
2193dee1ad47SJeff Kirsher 
2194dee1ad47SJeff Kirsher 		free_irq(adapter->msix_entries[vector].vector, netdev);
2195dee1ad47SJeff Kirsher 		vector++;
2196dee1ad47SJeff Kirsher 
2197dee1ad47SJeff Kirsher 		free_irq(adapter->msix_entries[vector].vector, netdev);
2198dee1ad47SJeff Kirsher 		vector++;
2199dee1ad47SJeff Kirsher 
2200dee1ad47SJeff Kirsher 		/* Other Causes interrupt vector */
2201dee1ad47SJeff Kirsher 		free_irq(adapter->msix_entries[vector].vector, netdev);
2202dee1ad47SJeff Kirsher 		return;
2203dee1ad47SJeff Kirsher 	}
2204dee1ad47SJeff Kirsher 
2205dee1ad47SJeff Kirsher 	free_irq(adapter->pdev->irq, netdev);
2206dee1ad47SJeff Kirsher }
2207dee1ad47SJeff Kirsher 
2208dee1ad47SJeff Kirsher /**
2209dee1ad47SJeff Kirsher  * e1000_irq_disable - Mask off interrupt generation on the NIC
2210b50f7bcaSJesse Brandeburg  * @adapter: board private structure
2211dee1ad47SJeff Kirsher  **/
e1000_irq_disable(struct e1000_adapter * adapter)2212dee1ad47SJeff Kirsher static void e1000_irq_disable(struct e1000_adapter *adapter)
2213dee1ad47SJeff Kirsher {
2214dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2215dee1ad47SJeff Kirsher 
2216dee1ad47SJeff Kirsher 	ew32(IMC, ~0);
2217dee1ad47SJeff Kirsher 	if (adapter->msix_entries)
2218dee1ad47SJeff Kirsher 		ew32(EIAC_82574, 0);
2219dee1ad47SJeff Kirsher 	e1e_flush();
2220dee1ad47SJeff Kirsher 
2221dee1ad47SJeff Kirsher 	if (adapter->msix_entries) {
2222dee1ad47SJeff Kirsher 		int i;
22236cf08d1cSDavid Ertman 
2224dee1ad47SJeff Kirsher 		for (i = 0; i < adapter->num_vectors; i++)
2225dee1ad47SJeff Kirsher 			synchronize_irq(adapter->msix_entries[i].vector);
2226dee1ad47SJeff Kirsher 	} else {
2227dee1ad47SJeff Kirsher 		synchronize_irq(adapter->pdev->irq);
2228dee1ad47SJeff Kirsher 	}
2229dee1ad47SJeff Kirsher }
2230dee1ad47SJeff Kirsher 
2231dee1ad47SJeff Kirsher /**
2232dee1ad47SJeff Kirsher  * e1000_irq_enable - Enable default interrupt generation settings
2233b50f7bcaSJesse Brandeburg  * @adapter: board private structure
2234dee1ad47SJeff Kirsher  **/
e1000_irq_enable(struct e1000_adapter * adapter)2235dee1ad47SJeff Kirsher static void e1000_irq_enable(struct e1000_adapter *adapter)
2236dee1ad47SJeff Kirsher {
2237dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2238dee1ad47SJeff Kirsher 
2239dee1ad47SJeff Kirsher 	if (adapter->msix_entries) {
2240dee1ad47SJeff Kirsher 		ew32(EIAC_82574, adapter->eiac_mask & E1000_EIAC_MASK_82574);
2241116f4a64SBenjamin Poirier 		ew32(IMS, adapter->eiac_mask | E1000_IMS_OTHER |
2242116f4a64SBenjamin Poirier 		     IMS_OTHER_MASK);
2243c8744f44SSasha Neftin 	} else if (hw->mac.type >= e1000_pch_lpt) {
224494fb848bSBruce Allan 		ew32(IMS, IMS_ENABLE_MASK | E1000_IMS_ECCER);
2245dee1ad47SJeff Kirsher 	} else {
2246dee1ad47SJeff Kirsher 		ew32(IMS, IMS_ENABLE_MASK);
2247dee1ad47SJeff Kirsher 	}
2248dee1ad47SJeff Kirsher 	e1e_flush();
2249dee1ad47SJeff Kirsher }
2250dee1ad47SJeff Kirsher 
2251dee1ad47SJeff Kirsher /**
2252dee1ad47SJeff Kirsher  * e1000e_get_hw_control - get control of the h/w from f/w
2253dee1ad47SJeff Kirsher  * @adapter: address of board private structure
2254dee1ad47SJeff Kirsher  *
2255dee1ad47SJeff Kirsher  * e1000e_get_hw_control sets {CTRL_EXT|SWSM}:DRV_LOAD bit.
2256dee1ad47SJeff Kirsher  * For ASF and Pass Through versions of f/w this means that
2257dee1ad47SJeff Kirsher  * the driver is loaded. For AMT version (only with 82573)
2258dee1ad47SJeff Kirsher  * of the f/w this means that the network i/f is open.
2259dee1ad47SJeff Kirsher  **/
e1000e_get_hw_control(struct e1000_adapter * adapter)2260dee1ad47SJeff Kirsher void e1000e_get_hw_control(struct e1000_adapter *adapter)
2261dee1ad47SJeff Kirsher {
2262dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2263dee1ad47SJeff Kirsher 	u32 ctrl_ext;
2264dee1ad47SJeff Kirsher 	u32 swsm;
2265dee1ad47SJeff Kirsher 
2266dee1ad47SJeff Kirsher 	/* Let firmware know the driver has taken over */
2267dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
2268dee1ad47SJeff Kirsher 		swsm = er32(SWSM);
2269dee1ad47SJeff Kirsher 		ew32(SWSM, swsm | E1000_SWSM_DRV_LOAD);
2270dee1ad47SJeff Kirsher 	} else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
2271dee1ad47SJeff Kirsher 		ctrl_ext = er32(CTRL_EXT);
2272dee1ad47SJeff Kirsher 		ew32(CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
2273dee1ad47SJeff Kirsher 	}
2274dee1ad47SJeff Kirsher }
2275dee1ad47SJeff Kirsher 
2276dee1ad47SJeff Kirsher /**
2277dee1ad47SJeff Kirsher  * e1000e_release_hw_control - release control of the h/w to f/w
2278dee1ad47SJeff Kirsher  * @adapter: address of board private structure
2279dee1ad47SJeff Kirsher  *
2280dee1ad47SJeff Kirsher  * e1000e_release_hw_control resets {CTRL_EXT|SWSM}:DRV_LOAD bit.
2281dee1ad47SJeff Kirsher  * For ASF and Pass Through versions of f/w this means that the
2282dee1ad47SJeff Kirsher  * driver is no longer loaded. For AMT version (only with 82573) i
2283dee1ad47SJeff Kirsher  * of the f/w this means that the network i/f is closed.
2284dee1ad47SJeff Kirsher  *
2285dee1ad47SJeff Kirsher  **/
e1000e_release_hw_control(struct e1000_adapter * adapter)2286dee1ad47SJeff Kirsher void e1000e_release_hw_control(struct e1000_adapter *adapter)
2287dee1ad47SJeff Kirsher {
2288dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2289dee1ad47SJeff Kirsher 	u32 ctrl_ext;
2290dee1ad47SJeff Kirsher 	u32 swsm;
2291dee1ad47SJeff Kirsher 
2292dee1ad47SJeff Kirsher 	/* Let firmware taken over control of h/w */
2293dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
2294dee1ad47SJeff Kirsher 		swsm = er32(SWSM);
2295dee1ad47SJeff Kirsher 		ew32(SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
2296dee1ad47SJeff Kirsher 	} else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
2297dee1ad47SJeff Kirsher 		ctrl_ext = er32(CTRL_EXT);
2298dee1ad47SJeff Kirsher 		ew32(CTRL_EXT, ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
2299dee1ad47SJeff Kirsher 	}
2300dee1ad47SJeff Kirsher }
2301dee1ad47SJeff Kirsher 
2302dee1ad47SJeff Kirsher /**
230349ce9c2cSBen Hutchings  * e1000_alloc_ring_dma - allocate memory for a ring structure
2304b50f7bcaSJesse Brandeburg  * @adapter: board private structure
2305b50f7bcaSJesse Brandeburg  * @ring: ring struct for which to allocate dma
2306dee1ad47SJeff Kirsher  **/
e1000_alloc_ring_dma(struct e1000_adapter * adapter,struct e1000_ring * ring)2307dee1ad47SJeff Kirsher static int e1000_alloc_ring_dma(struct e1000_adapter *adapter,
2308dee1ad47SJeff Kirsher 				struct e1000_ring *ring)
2309dee1ad47SJeff Kirsher {
2310dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
2311dee1ad47SJeff Kirsher 
2312750afb08SLuis Chamberlain 	ring->desc = dma_alloc_coherent(&pdev->dev, ring->size, &ring->dma,
2313dee1ad47SJeff Kirsher 					GFP_KERNEL);
2314dee1ad47SJeff Kirsher 	if (!ring->desc)
2315dee1ad47SJeff Kirsher 		return -ENOMEM;
2316dee1ad47SJeff Kirsher 
2317dee1ad47SJeff Kirsher 	return 0;
2318dee1ad47SJeff Kirsher }
2319dee1ad47SJeff Kirsher 
2320dee1ad47SJeff Kirsher /**
2321dee1ad47SJeff Kirsher  * e1000e_setup_tx_resources - allocate Tx resources (Descriptors)
232255aa6985SBruce Allan  * @tx_ring: Tx descriptor ring
2323dee1ad47SJeff Kirsher  *
2324dee1ad47SJeff Kirsher  * Return 0 on success, negative on failure
2325dee1ad47SJeff Kirsher  **/
e1000e_setup_tx_resources(struct e1000_ring * tx_ring)232655aa6985SBruce Allan int e1000e_setup_tx_resources(struct e1000_ring *tx_ring)
2327dee1ad47SJeff Kirsher {
232855aa6985SBruce Allan 	struct e1000_adapter *adapter = tx_ring->adapter;
2329dee1ad47SJeff Kirsher 	int err = -ENOMEM, size;
2330dee1ad47SJeff Kirsher 
2331dee1ad47SJeff Kirsher 	size = sizeof(struct e1000_buffer) * tx_ring->count;
2332dee1ad47SJeff Kirsher 	tx_ring->buffer_info = vzalloc(size);
2333dee1ad47SJeff Kirsher 	if (!tx_ring->buffer_info)
2334dee1ad47SJeff Kirsher 		goto err;
2335dee1ad47SJeff Kirsher 
2336dee1ad47SJeff Kirsher 	/* round up to nearest 4K */
2337dee1ad47SJeff Kirsher 	tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
2338dee1ad47SJeff Kirsher 	tx_ring->size = ALIGN(tx_ring->size, 4096);
2339dee1ad47SJeff Kirsher 
2340dee1ad47SJeff Kirsher 	err = e1000_alloc_ring_dma(adapter, tx_ring);
2341dee1ad47SJeff Kirsher 	if (err)
2342dee1ad47SJeff Kirsher 		goto err;
2343dee1ad47SJeff Kirsher 
2344dee1ad47SJeff Kirsher 	tx_ring->next_to_use = 0;
2345dee1ad47SJeff Kirsher 	tx_ring->next_to_clean = 0;
2346dee1ad47SJeff Kirsher 
2347dee1ad47SJeff Kirsher 	return 0;
2348dee1ad47SJeff Kirsher err:
2349dee1ad47SJeff Kirsher 	vfree(tx_ring->buffer_info);
2350dee1ad47SJeff Kirsher 	e_err("Unable to allocate memory for the transmit descriptor ring\n");
2351dee1ad47SJeff Kirsher 	return err;
2352dee1ad47SJeff Kirsher }
2353dee1ad47SJeff Kirsher 
2354dee1ad47SJeff Kirsher /**
2355dee1ad47SJeff Kirsher  * e1000e_setup_rx_resources - allocate Rx resources (Descriptors)
235655aa6985SBruce Allan  * @rx_ring: Rx descriptor ring
2357dee1ad47SJeff Kirsher  *
2358dee1ad47SJeff Kirsher  * Returns 0 on success, negative on failure
2359dee1ad47SJeff Kirsher  **/
e1000e_setup_rx_resources(struct e1000_ring * rx_ring)236055aa6985SBruce Allan int e1000e_setup_rx_resources(struct e1000_ring *rx_ring)
2361dee1ad47SJeff Kirsher {
236255aa6985SBruce Allan 	struct e1000_adapter *adapter = rx_ring->adapter;
2363dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
2364dee1ad47SJeff Kirsher 	int i, size, desc_len, err = -ENOMEM;
2365dee1ad47SJeff Kirsher 
2366dee1ad47SJeff Kirsher 	size = sizeof(struct e1000_buffer) * rx_ring->count;
2367dee1ad47SJeff Kirsher 	rx_ring->buffer_info = vzalloc(size);
2368dee1ad47SJeff Kirsher 	if (!rx_ring->buffer_info)
2369dee1ad47SJeff Kirsher 		goto err;
2370dee1ad47SJeff Kirsher 
2371dee1ad47SJeff Kirsher 	for (i = 0; i < rx_ring->count; i++) {
2372dee1ad47SJeff Kirsher 		buffer_info = &rx_ring->buffer_info[i];
2373dee1ad47SJeff Kirsher 		buffer_info->ps_pages = kcalloc(PS_PAGE_BUFFERS,
2374dee1ad47SJeff Kirsher 						sizeof(struct e1000_ps_page),
2375dee1ad47SJeff Kirsher 						GFP_KERNEL);
2376dee1ad47SJeff Kirsher 		if (!buffer_info->ps_pages)
2377dee1ad47SJeff Kirsher 			goto err_pages;
2378dee1ad47SJeff Kirsher 	}
2379dee1ad47SJeff Kirsher 
2380dee1ad47SJeff Kirsher 	desc_len = sizeof(union e1000_rx_desc_packet_split);
2381dee1ad47SJeff Kirsher 
2382dee1ad47SJeff Kirsher 	/* Round up to nearest 4K */
2383dee1ad47SJeff Kirsher 	rx_ring->size = rx_ring->count * desc_len;
2384dee1ad47SJeff Kirsher 	rx_ring->size = ALIGN(rx_ring->size, 4096);
2385dee1ad47SJeff Kirsher 
2386dee1ad47SJeff Kirsher 	err = e1000_alloc_ring_dma(adapter, rx_ring);
2387dee1ad47SJeff Kirsher 	if (err)
2388dee1ad47SJeff Kirsher 		goto err_pages;
2389dee1ad47SJeff Kirsher 
2390dee1ad47SJeff Kirsher 	rx_ring->next_to_clean = 0;
2391dee1ad47SJeff Kirsher 	rx_ring->next_to_use = 0;
2392dee1ad47SJeff Kirsher 	rx_ring->rx_skb_top = NULL;
2393dee1ad47SJeff Kirsher 
2394dee1ad47SJeff Kirsher 	return 0;
2395dee1ad47SJeff Kirsher 
2396dee1ad47SJeff Kirsher err_pages:
2397dee1ad47SJeff Kirsher 	for (i = 0; i < rx_ring->count; i++) {
2398dee1ad47SJeff Kirsher 		buffer_info = &rx_ring->buffer_info[i];
2399dee1ad47SJeff Kirsher 		kfree(buffer_info->ps_pages);
2400dee1ad47SJeff Kirsher 	}
2401dee1ad47SJeff Kirsher err:
2402dee1ad47SJeff Kirsher 	vfree(rx_ring->buffer_info);
2403dee1ad47SJeff Kirsher 	e_err("Unable to allocate memory for the receive descriptor ring\n");
2404dee1ad47SJeff Kirsher 	return err;
2405dee1ad47SJeff Kirsher }
2406dee1ad47SJeff Kirsher 
2407dee1ad47SJeff Kirsher /**
2408dee1ad47SJeff Kirsher  * e1000_clean_tx_ring - Free Tx Buffers
240955aa6985SBruce Allan  * @tx_ring: Tx descriptor ring
2410dee1ad47SJeff Kirsher  **/
e1000_clean_tx_ring(struct e1000_ring * tx_ring)241155aa6985SBruce Allan static void e1000_clean_tx_ring(struct e1000_ring *tx_ring)
2412dee1ad47SJeff Kirsher {
241355aa6985SBruce Allan 	struct e1000_adapter *adapter = tx_ring->adapter;
2414dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
2415dee1ad47SJeff Kirsher 	unsigned long size;
2416dee1ad47SJeff Kirsher 	unsigned int i;
2417dee1ad47SJeff Kirsher 
2418dee1ad47SJeff Kirsher 	for (i = 0; i < tx_ring->count; i++) {
2419dee1ad47SJeff Kirsher 		buffer_info = &tx_ring->buffer_info[i];
2420377b6273SFlorian Fainelli 		e1000_put_txbuf(tx_ring, buffer_info, false);
2421dee1ad47SJeff Kirsher 	}
2422dee1ad47SJeff Kirsher 
24233f0cfa3bSTom Herbert 	netdev_reset_queue(adapter->netdev);
2424dee1ad47SJeff Kirsher 	size = sizeof(struct e1000_buffer) * tx_ring->count;
2425dee1ad47SJeff Kirsher 	memset(tx_ring->buffer_info, 0, size);
2426dee1ad47SJeff Kirsher 
2427dee1ad47SJeff Kirsher 	memset(tx_ring->desc, 0, tx_ring->size);
2428dee1ad47SJeff Kirsher 
2429dee1ad47SJeff Kirsher 	tx_ring->next_to_use = 0;
2430dee1ad47SJeff Kirsher 	tx_ring->next_to_clean = 0;
2431dee1ad47SJeff Kirsher }
2432dee1ad47SJeff Kirsher 
2433dee1ad47SJeff Kirsher /**
2434dee1ad47SJeff Kirsher  * e1000e_free_tx_resources - Free Tx Resources per Queue
243555aa6985SBruce Allan  * @tx_ring: Tx descriptor ring
2436dee1ad47SJeff Kirsher  *
2437dee1ad47SJeff Kirsher  * Free all transmit software resources
2438dee1ad47SJeff Kirsher  **/
e1000e_free_tx_resources(struct e1000_ring * tx_ring)243955aa6985SBruce Allan void e1000e_free_tx_resources(struct e1000_ring *tx_ring)
2440dee1ad47SJeff Kirsher {
244155aa6985SBruce Allan 	struct e1000_adapter *adapter = tx_ring->adapter;
2442dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
2443dee1ad47SJeff Kirsher 
244455aa6985SBruce Allan 	e1000_clean_tx_ring(tx_ring);
2445dee1ad47SJeff Kirsher 
2446dee1ad47SJeff Kirsher 	vfree(tx_ring->buffer_info);
2447dee1ad47SJeff Kirsher 	tx_ring->buffer_info = NULL;
2448dee1ad47SJeff Kirsher 
2449dee1ad47SJeff Kirsher 	dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2450dee1ad47SJeff Kirsher 			  tx_ring->dma);
2451dee1ad47SJeff Kirsher 	tx_ring->desc = NULL;
2452dee1ad47SJeff Kirsher }
2453dee1ad47SJeff Kirsher 
2454dee1ad47SJeff Kirsher /**
2455dee1ad47SJeff Kirsher  * e1000e_free_rx_resources - Free Rx Resources
245655aa6985SBruce Allan  * @rx_ring: Rx descriptor ring
2457dee1ad47SJeff Kirsher  *
2458dee1ad47SJeff Kirsher  * Free all receive software resources
2459dee1ad47SJeff Kirsher  **/
e1000e_free_rx_resources(struct e1000_ring * rx_ring)246055aa6985SBruce Allan void e1000e_free_rx_resources(struct e1000_ring *rx_ring)
2461dee1ad47SJeff Kirsher {
246255aa6985SBruce Allan 	struct e1000_adapter *adapter = rx_ring->adapter;
2463dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
2464dee1ad47SJeff Kirsher 	int i;
2465dee1ad47SJeff Kirsher 
246655aa6985SBruce Allan 	e1000_clean_rx_ring(rx_ring);
2467dee1ad47SJeff Kirsher 
2468dee1ad47SJeff Kirsher 	for (i = 0; i < rx_ring->count; i++)
2469dee1ad47SJeff Kirsher 		kfree(rx_ring->buffer_info[i].ps_pages);
2470dee1ad47SJeff Kirsher 
2471dee1ad47SJeff Kirsher 	vfree(rx_ring->buffer_info);
2472dee1ad47SJeff Kirsher 	rx_ring->buffer_info = NULL;
2473dee1ad47SJeff Kirsher 
2474dee1ad47SJeff Kirsher 	dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2475dee1ad47SJeff Kirsher 			  rx_ring->dma);
2476dee1ad47SJeff Kirsher 	rx_ring->desc = NULL;
2477dee1ad47SJeff Kirsher }
2478dee1ad47SJeff Kirsher 
2479dee1ad47SJeff Kirsher /**
2480dee1ad47SJeff Kirsher  * e1000_update_itr - update the dynamic ITR value based on statistics
2481dee1ad47SJeff Kirsher  * @itr_setting: current adapter->itr
2482dee1ad47SJeff Kirsher  * @packets: the number of packets during this measurement interval
2483dee1ad47SJeff Kirsher  * @bytes: the number of bytes during this measurement interval
2484dee1ad47SJeff Kirsher  *
2485dee1ad47SJeff Kirsher  *      Stores a new ITR value based on packets and byte
2486dee1ad47SJeff Kirsher  *      counts during the last interrupt.  The advantage of per interrupt
2487dee1ad47SJeff Kirsher  *      computation is faster updates and more accurate ITR for the current
2488dee1ad47SJeff Kirsher  *      traffic pattern.  Constants in this function were computed
2489dee1ad47SJeff Kirsher  *      based on theoretical maximum wire speed and thresholds were set based
2490dee1ad47SJeff Kirsher  *      on testing data as well as attempting to minimize response time
2491dee1ad47SJeff Kirsher  *      while increasing bulk throughput.  This functionality is controlled
2492dee1ad47SJeff Kirsher  *      by the InterruptThrottleRate module parameter.
2493dee1ad47SJeff Kirsher  **/
e1000_update_itr(u16 itr_setting,int packets,int bytes)24948bb62869SBruce Allan static unsigned int e1000_update_itr(u16 itr_setting, int packets, int bytes)
2495dee1ad47SJeff Kirsher {
2496dee1ad47SJeff Kirsher 	unsigned int retval = itr_setting;
2497dee1ad47SJeff Kirsher 
2498dee1ad47SJeff Kirsher 	if (packets == 0)
24995015e53aSBruce Allan 		return itr_setting;
2500dee1ad47SJeff Kirsher 
2501dee1ad47SJeff Kirsher 	switch (itr_setting) {
2502dee1ad47SJeff Kirsher 	case lowest_latency:
2503dee1ad47SJeff Kirsher 		/* handle TSO and jumbo frames */
2504dee1ad47SJeff Kirsher 		if (bytes / packets > 8000)
2505dee1ad47SJeff Kirsher 			retval = bulk_latency;
2506dee1ad47SJeff Kirsher 		else if ((packets < 5) && (bytes > 512))
2507dee1ad47SJeff Kirsher 			retval = low_latency;
2508dee1ad47SJeff Kirsher 		break;
2509dee1ad47SJeff Kirsher 	case low_latency:	/* 50 usec aka 20000 ints/s */
2510dee1ad47SJeff Kirsher 		if (bytes > 10000) {
2511dee1ad47SJeff Kirsher 			/* this if handles the TSO accounting */
2512dee1ad47SJeff Kirsher 			if (bytes / packets > 8000)
2513dee1ad47SJeff Kirsher 				retval = bulk_latency;
2514dee1ad47SJeff Kirsher 			else if ((packets < 10) || ((bytes / packets) > 1200))
2515dee1ad47SJeff Kirsher 				retval = bulk_latency;
2516dee1ad47SJeff Kirsher 			else if ((packets > 35))
2517dee1ad47SJeff Kirsher 				retval = lowest_latency;
2518dee1ad47SJeff Kirsher 		} else if (bytes / packets > 2000) {
2519dee1ad47SJeff Kirsher 			retval = bulk_latency;
2520dee1ad47SJeff Kirsher 		} else if (packets <= 2 && bytes < 512) {
2521dee1ad47SJeff Kirsher 			retval = lowest_latency;
2522dee1ad47SJeff Kirsher 		}
2523dee1ad47SJeff Kirsher 		break;
2524dee1ad47SJeff Kirsher 	case bulk_latency:	/* 250 usec aka 4000 ints/s */
2525dee1ad47SJeff Kirsher 		if (bytes > 25000) {
2526dee1ad47SJeff Kirsher 			if (packets > 35)
2527dee1ad47SJeff Kirsher 				retval = low_latency;
2528dee1ad47SJeff Kirsher 		} else if (bytes < 6000) {
2529dee1ad47SJeff Kirsher 			retval = low_latency;
2530dee1ad47SJeff Kirsher 		}
2531dee1ad47SJeff Kirsher 		break;
2532dee1ad47SJeff Kirsher 	}
2533dee1ad47SJeff Kirsher 
2534dee1ad47SJeff Kirsher 	return retval;
2535dee1ad47SJeff Kirsher }
2536dee1ad47SJeff Kirsher 
e1000_set_itr(struct e1000_adapter * adapter)2537dee1ad47SJeff Kirsher static void e1000_set_itr(struct e1000_adapter *adapter)
2538dee1ad47SJeff Kirsher {
2539dee1ad47SJeff Kirsher 	u16 current_itr;
2540dee1ad47SJeff Kirsher 	u32 new_itr = adapter->itr;
2541dee1ad47SJeff Kirsher 
2542dee1ad47SJeff Kirsher 	/* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2543dee1ad47SJeff Kirsher 	if (adapter->link_speed != SPEED_1000) {
2544dee1ad47SJeff Kirsher 		new_itr = 4000;
2545dee1ad47SJeff Kirsher 		goto set_itr_now;
2546dee1ad47SJeff Kirsher 	}
2547dee1ad47SJeff Kirsher 
2548dee1ad47SJeff Kirsher 	if (adapter->flags2 & FLAG2_DISABLE_AIM) {
2549dee1ad47SJeff Kirsher 		new_itr = 0;
2550dee1ad47SJeff Kirsher 		goto set_itr_now;
2551dee1ad47SJeff Kirsher 	}
2552dee1ad47SJeff Kirsher 
25538bb62869SBruce Allan 	adapter->tx_itr = e1000_update_itr(adapter->tx_itr,
2554dee1ad47SJeff Kirsher 					   adapter->total_tx_packets,
2555dee1ad47SJeff Kirsher 					   adapter->total_tx_bytes);
2556dee1ad47SJeff Kirsher 	/* conservative mode (itr 3) eliminates the lowest_latency setting */
2557dee1ad47SJeff Kirsher 	if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
2558dee1ad47SJeff Kirsher 		adapter->tx_itr = low_latency;
2559dee1ad47SJeff Kirsher 
25608bb62869SBruce Allan 	adapter->rx_itr = e1000_update_itr(adapter->rx_itr,
2561dee1ad47SJeff Kirsher 					   adapter->total_rx_packets,
2562dee1ad47SJeff Kirsher 					   adapter->total_rx_bytes);
2563dee1ad47SJeff Kirsher 	/* conservative mode (itr 3) eliminates the lowest_latency setting */
2564dee1ad47SJeff Kirsher 	if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2565dee1ad47SJeff Kirsher 		adapter->rx_itr = low_latency;
2566dee1ad47SJeff Kirsher 
2567dee1ad47SJeff Kirsher 	current_itr = max(adapter->rx_itr, adapter->tx_itr);
2568dee1ad47SJeff Kirsher 
2569dee1ad47SJeff Kirsher 	/* counts and packets in update_itr are dependent on these numbers */
257033550cecSBruce Allan 	switch (current_itr) {
2571dee1ad47SJeff Kirsher 	case lowest_latency:
2572dee1ad47SJeff Kirsher 		new_itr = 70000;
2573dee1ad47SJeff Kirsher 		break;
2574dee1ad47SJeff Kirsher 	case low_latency:
2575dee1ad47SJeff Kirsher 		new_itr = 20000;	/* aka hwitr = ~200 */
2576dee1ad47SJeff Kirsher 		break;
2577dee1ad47SJeff Kirsher 	case bulk_latency:
2578dee1ad47SJeff Kirsher 		new_itr = 4000;
2579dee1ad47SJeff Kirsher 		break;
2580dee1ad47SJeff Kirsher 	default:
2581dee1ad47SJeff Kirsher 		break;
2582dee1ad47SJeff Kirsher 	}
2583dee1ad47SJeff Kirsher 
2584dee1ad47SJeff Kirsher set_itr_now:
2585dee1ad47SJeff Kirsher 	if (new_itr != adapter->itr) {
2586e921eb1aSBruce Allan 		/* this attempts to bias the interrupt rate towards Bulk
2587dee1ad47SJeff Kirsher 		 * by adding intermediate steps when interrupt rate is
2588dee1ad47SJeff Kirsher 		 * increasing
2589dee1ad47SJeff Kirsher 		 */
2590dee1ad47SJeff Kirsher 		new_itr = new_itr > adapter->itr ?
2591f0ff4398SBruce Allan 		    min(adapter->itr + (new_itr >> 2), new_itr) : new_itr;
2592dee1ad47SJeff Kirsher 		adapter->itr = new_itr;
2593dee1ad47SJeff Kirsher 		adapter->rx_ring->itr_val = new_itr;
2594dee1ad47SJeff Kirsher 		if (adapter->msix_entries)
2595dee1ad47SJeff Kirsher 			adapter->rx_ring->set_itr = 1;
2596dee1ad47SJeff Kirsher 		else
2597e3d14b08SBruce Allan 			e1000e_write_itr(adapter, new_itr);
2598dee1ad47SJeff Kirsher 	}
2599dee1ad47SJeff Kirsher }
2600dee1ad47SJeff Kirsher 
2601dee1ad47SJeff Kirsher /**
260222a4cca2SMatthew Vick  * e1000e_write_itr - write the ITR value to the appropriate registers
260322a4cca2SMatthew Vick  * @adapter: address of board private structure
260422a4cca2SMatthew Vick  * @itr: new ITR value to program
260522a4cca2SMatthew Vick  *
260622a4cca2SMatthew Vick  * e1000e_write_itr determines if the adapter is in MSI-X mode
260722a4cca2SMatthew Vick  * and, if so, writes the EITR registers with the ITR value.
260822a4cca2SMatthew Vick  * Otherwise, it writes the ITR value into the ITR register.
260922a4cca2SMatthew Vick  **/
e1000e_write_itr(struct e1000_adapter * adapter,u32 itr)261022a4cca2SMatthew Vick void e1000e_write_itr(struct e1000_adapter *adapter, u32 itr)
261122a4cca2SMatthew Vick {
261222a4cca2SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
261322a4cca2SMatthew Vick 	u32 new_itr = itr ? 1000000000 / (itr * 256) : 0;
261422a4cca2SMatthew Vick 
261522a4cca2SMatthew Vick 	if (adapter->msix_entries) {
261622a4cca2SMatthew Vick 		int vector;
261722a4cca2SMatthew Vick 
261822a4cca2SMatthew Vick 		for (vector = 0; vector < adapter->num_vectors; vector++)
261922a4cca2SMatthew Vick 			writel(new_itr, hw->hw_addr + E1000_EITR_82574(vector));
262022a4cca2SMatthew Vick 	} else {
262122a4cca2SMatthew Vick 		ew32(ITR, new_itr);
262222a4cca2SMatthew Vick 	}
262322a4cca2SMatthew Vick }
262422a4cca2SMatthew Vick 
262522a4cca2SMatthew Vick /**
2626dee1ad47SJeff Kirsher  * e1000_alloc_queues - Allocate memory for all rings
2627dee1ad47SJeff Kirsher  * @adapter: board private structure to initialize
2628dee1ad47SJeff Kirsher  **/
e1000_alloc_queues(struct e1000_adapter * adapter)26299f9a12f8SBill Pemberton static int e1000_alloc_queues(struct e1000_adapter *adapter)
2630dee1ad47SJeff Kirsher {
263155aa6985SBruce Allan 	int size = sizeof(struct e1000_ring);
263255aa6985SBruce Allan 
263355aa6985SBruce Allan 	adapter->tx_ring = kzalloc(size, GFP_KERNEL);
2634dee1ad47SJeff Kirsher 	if (!adapter->tx_ring)
2635dee1ad47SJeff Kirsher 		goto err;
263655aa6985SBruce Allan 	adapter->tx_ring->count = adapter->tx_ring_count;
263755aa6985SBruce Allan 	adapter->tx_ring->adapter = adapter;
2638dee1ad47SJeff Kirsher 
263955aa6985SBruce Allan 	adapter->rx_ring = kzalloc(size, GFP_KERNEL);
2640dee1ad47SJeff Kirsher 	if (!adapter->rx_ring)
2641dee1ad47SJeff Kirsher 		goto err;
264255aa6985SBruce Allan 	adapter->rx_ring->count = adapter->rx_ring_count;
264355aa6985SBruce Allan 	adapter->rx_ring->adapter = adapter;
2644dee1ad47SJeff Kirsher 
2645dee1ad47SJeff Kirsher 	return 0;
2646dee1ad47SJeff Kirsher err:
2647dee1ad47SJeff Kirsher 	e_err("Unable to allocate memory for queues\n");
2648dee1ad47SJeff Kirsher 	kfree(adapter->rx_ring);
2649dee1ad47SJeff Kirsher 	kfree(adapter->tx_ring);
2650dee1ad47SJeff Kirsher 	return -ENOMEM;
2651dee1ad47SJeff Kirsher }
2652dee1ad47SJeff Kirsher 
2653dee1ad47SJeff Kirsher /**
2654c58c8a78SBruce Allan  * e1000e_poll - NAPI Rx polling callback
2655dee1ad47SJeff Kirsher  * @napi: struct associated with this polling callback
26560bcd952fSJesse Brandeburg  * @budget: number of packets driver is allowed to process this poll
2657dee1ad47SJeff Kirsher  **/
e1000e_poll(struct napi_struct * napi,int budget)26580bcd952fSJesse Brandeburg static int e1000e_poll(struct napi_struct *napi, int budget)
2659dee1ad47SJeff Kirsher {
2660c58c8a78SBruce Allan 	struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter,
2661c58c8a78SBruce Allan 						     napi);
2662dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2663dee1ad47SJeff Kirsher 	struct net_device *poll_dev = adapter->netdev;
2664dee1ad47SJeff Kirsher 	int tx_cleaned = 1, work_done = 0;
2665dee1ad47SJeff Kirsher 
2666dee1ad47SJeff Kirsher 	adapter = netdev_priv(poll_dev);
2667dee1ad47SJeff Kirsher 
2668c58c8a78SBruce Allan 	if (!adapter->msix_entries ||
2669c58c8a78SBruce Allan 	    (adapter->rx_ring->ims_val & adapter->tx_ring->ims_val))
267055aa6985SBruce Allan 		tx_cleaned = e1000_clean_tx_irq(adapter->tx_ring);
2671dee1ad47SJeff Kirsher 
26720bcd952fSJesse Brandeburg 	adapter->clean_rx(adapter->rx_ring, &work_done, budget);
2673dee1ad47SJeff Kirsher 
26740bcd952fSJesse Brandeburg 	if (!tx_cleaned || work_done == budget)
26750bcd952fSJesse Brandeburg 		return budget;
2676dee1ad47SJeff Kirsher 
26770bcd952fSJesse Brandeburg 	/* Exit the polling mode, but don't re-enable interrupts if stack might
26780bcd952fSJesse Brandeburg 	 * poll us due to busy-polling
26790bcd952fSJesse Brandeburg 	 */
26800bcd952fSJesse Brandeburg 	if (likely(napi_complete_done(napi, work_done))) {
2681dee1ad47SJeff Kirsher 		if (adapter->itr_setting & 3)
2682dee1ad47SJeff Kirsher 			e1000_set_itr(adapter);
2683dee1ad47SJeff Kirsher 		if (!test_bit(__E1000_DOWN, &adapter->state)) {
2684dee1ad47SJeff Kirsher 			if (adapter->msix_entries)
26851f0ea197SBenjamin Poirier 				ew32(IMS, adapter->rx_ring->ims_val);
2686dee1ad47SJeff Kirsher 			else
2687dee1ad47SJeff Kirsher 				e1000_irq_enable(adapter);
2688dee1ad47SJeff Kirsher 		}
2689dee1ad47SJeff Kirsher 	}
2690dee1ad47SJeff Kirsher 
2691dee1ad47SJeff Kirsher 	return work_done;
2692dee1ad47SJeff Kirsher }
2693dee1ad47SJeff Kirsher 
e1000_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)269480d5c368SPatrick McHardy static int e1000_vlan_rx_add_vid(struct net_device *netdev,
2695603cdca9SBruce Allan 				 __always_unused __be16 proto, u16 vid)
2696dee1ad47SJeff Kirsher {
2697dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
2698dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2699dee1ad47SJeff Kirsher 	u32 vfta, index;
2700dee1ad47SJeff Kirsher 
2701dee1ad47SJeff Kirsher 	/* don't update vlan cookie if already programmed */
2702dee1ad47SJeff Kirsher 	if ((adapter->hw.mng_cookie.status &
2703dee1ad47SJeff Kirsher 	     E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
2704dee1ad47SJeff Kirsher 	    (vid == adapter->mng_vlan_id))
27058e586137SJiri Pirko 		return 0;
2706dee1ad47SJeff Kirsher 
2707dee1ad47SJeff Kirsher 	/* add VID to filter table */
2708dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2709dee1ad47SJeff Kirsher 		index = (vid >> 5) & 0x7F;
2710dee1ad47SJeff Kirsher 		vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
271118dd2392SJacob Keller 		vfta |= BIT((vid & 0x1F));
2712dee1ad47SJeff Kirsher 		hw->mac.ops.write_vfta(hw, index, vfta);
2713dee1ad47SJeff Kirsher 	}
2714dee1ad47SJeff Kirsher 
2715dee1ad47SJeff Kirsher 	set_bit(vid, adapter->active_vlans);
27168e586137SJiri Pirko 
27178e586137SJiri Pirko 	return 0;
2718dee1ad47SJeff Kirsher }
2719dee1ad47SJeff Kirsher 
e1000_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)272080d5c368SPatrick McHardy static int e1000_vlan_rx_kill_vid(struct net_device *netdev,
2721603cdca9SBruce Allan 				  __always_unused __be16 proto, u16 vid)
2722dee1ad47SJeff Kirsher {
2723dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
2724dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2725dee1ad47SJeff Kirsher 	u32 vfta, index;
2726dee1ad47SJeff Kirsher 
2727dee1ad47SJeff Kirsher 	if ((adapter->hw.mng_cookie.status &
2728dee1ad47SJeff Kirsher 	     E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
2729dee1ad47SJeff Kirsher 	    (vid == adapter->mng_vlan_id)) {
2730dee1ad47SJeff Kirsher 		/* release control to f/w */
2731dee1ad47SJeff Kirsher 		e1000e_release_hw_control(adapter);
27328e586137SJiri Pirko 		return 0;
2733dee1ad47SJeff Kirsher 	}
2734dee1ad47SJeff Kirsher 
2735dee1ad47SJeff Kirsher 	/* remove VID from filter table */
2736dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2737dee1ad47SJeff Kirsher 		index = (vid >> 5) & 0x7F;
2738dee1ad47SJeff Kirsher 		vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
273918dd2392SJacob Keller 		vfta &= ~BIT((vid & 0x1F));
2740dee1ad47SJeff Kirsher 		hw->mac.ops.write_vfta(hw, index, vfta);
2741dee1ad47SJeff Kirsher 	}
2742dee1ad47SJeff Kirsher 
2743dee1ad47SJeff Kirsher 	clear_bit(vid, adapter->active_vlans);
27448e586137SJiri Pirko 
27458e586137SJiri Pirko 	return 0;
2746dee1ad47SJeff Kirsher }
2747dee1ad47SJeff Kirsher 
2748dee1ad47SJeff Kirsher /**
2749dee1ad47SJeff Kirsher  * e1000e_vlan_filter_disable - helper to disable hw VLAN filtering
2750dee1ad47SJeff Kirsher  * @adapter: board private structure to initialize
2751dee1ad47SJeff Kirsher  **/
e1000e_vlan_filter_disable(struct e1000_adapter * adapter)2752dee1ad47SJeff Kirsher static void e1000e_vlan_filter_disable(struct e1000_adapter *adapter)
2753dee1ad47SJeff Kirsher {
2754dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2755dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2756dee1ad47SJeff Kirsher 	u32 rctl;
2757dee1ad47SJeff Kirsher 
2758dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2759dee1ad47SJeff Kirsher 		/* disable VLAN receive filtering */
2760dee1ad47SJeff Kirsher 		rctl = er32(RCTL);
2761dee1ad47SJeff Kirsher 		rctl &= ~(E1000_RCTL_VFE | E1000_RCTL_CFIEN);
2762dee1ad47SJeff Kirsher 		ew32(RCTL, rctl);
2763dee1ad47SJeff Kirsher 
2764dee1ad47SJeff Kirsher 		if (adapter->mng_vlan_id != (u16)E1000_MNG_VLAN_NONE) {
276580d5c368SPatrick McHardy 			e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q),
276680d5c368SPatrick McHardy 					       adapter->mng_vlan_id);
2767dee1ad47SJeff Kirsher 			adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
2768dee1ad47SJeff Kirsher 		}
2769dee1ad47SJeff Kirsher 	}
2770dee1ad47SJeff Kirsher }
2771dee1ad47SJeff Kirsher 
2772dee1ad47SJeff Kirsher /**
2773dee1ad47SJeff Kirsher  * e1000e_vlan_filter_enable - helper to enable HW VLAN filtering
2774dee1ad47SJeff Kirsher  * @adapter: board private structure to initialize
2775dee1ad47SJeff Kirsher  **/
e1000e_vlan_filter_enable(struct e1000_adapter * adapter)2776dee1ad47SJeff Kirsher static void e1000e_vlan_filter_enable(struct e1000_adapter *adapter)
2777dee1ad47SJeff Kirsher {
2778dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2779dee1ad47SJeff Kirsher 	u32 rctl;
2780dee1ad47SJeff Kirsher 
2781dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2782dee1ad47SJeff Kirsher 		/* enable VLAN receive filtering */
2783dee1ad47SJeff Kirsher 		rctl = er32(RCTL);
2784dee1ad47SJeff Kirsher 		rctl |= E1000_RCTL_VFE;
2785dee1ad47SJeff Kirsher 		rctl &= ~E1000_RCTL_CFIEN;
2786dee1ad47SJeff Kirsher 		ew32(RCTL, rctl);
2787dee1ad47SJeff Kirsher 	}
2788dee1ad47SJeff Kirsher }
2789dee1ad47SJeff Kirsher 
2790dee1ad47SJeff Kirsher /**
2791889ad456SJarod Wilson  * e1000e_vlan_strip_disable - helper to disable HW VLAN stripping
2792dee1ad47SJeff Kirsher  * @adapter: board private structure to initialize
2793dee1ad47SJeff Kirsher  **/
e1000e_vlan_strip_disable(struct e1000_adapter * adapter)2794dee1ad47SJeff Kirsher static void e1000e_vlan_strip_disable(struct e1000_adapter *adapter)
2795dee1ad47SJeff Kirsher {
2796dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2797dee1ad47SJeff Kirsher 	u32 ctrl;
2798dee1ad47SJeff Kirsher 
2799dee1ad47SJeff Kirsher 	/* disable VLAN tag insert/strip */
2800dee1ad47SJeff Kirsher 	ctrl = er32(CTRL);
2801dee1ad47SJeff Kirsher 	ctrl &= ~E1000_CTRL_VME;
2802dee1ad47SJeff Kirsher 	ew32(CTRL, ctrl);
2803dee1ad47SJeff Kirsher }
2804dee1ad47SJeff Kirsher 
2805dee1ad47SJeff Kirsher /**
2806dee1ad47SJeff Kirsher  * e1000e_vlan_strip_enable - helper to enable HW VLAN stripping
2807dee1ad47SJeff Kirsher  * @adapter: board private structure to initialize
2808dee1ad47SJeff Kirsher  **/
e1000e_vlan_strip_enable(struct e1000_adapter * adapter)2809dee1ad47SJeff Kirsher static void e1000e_vlan_strip_enable(struct e1000_adapter *adapter)
2810dee1ad47SJeff Kirsher {
2811dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2812dee1ad47SJeff Kirsher 	u32 ctrl;
2813dee1ad47SJeff Kirsher 
2814dee1ad47SJeff Kirsher 	/* enable VLAN tag insert/strip */
2815dee1ad47SJeff Kirsher 	ctrl = er32(CTRL);
2816dee1ad47SJeff Kirsher 	ctrl |= E1000_CTRL_VME;
2817dee1ad47SJeff Kirsher 	ew32(CTRL, ctrl);
2818dee1ad47SJeff Kirsher }
2819dee1ad47SJeff Kirsher 
e1000_update_mng_vlan(struct e1000_adapter * adapter)2820dee1ad47SJeff Kirsher static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
2821dee1ad47SJeff Kirsher {
2822dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2823dee1ad47SJeff Kirsher 	u16 vid = adapter->hw.mng_cookie.vlan_id;
2824dee1ad47SJeff Kirsher 	u16 old_vid = adapter->mng_vlan_id;
2825dee1ad47SJeff Kirsher 
2826e5fe2541SBruce Allan 	if (adapter->hw.mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
282780d5c368SPatrick McHardy 		e1000_vlan_rx_add_vid(netdev, htons(ETH_P_8021Q), vid);
2828dee1ad47SJeff Kirsher 		adapter->mng_vlan_id = vid;
2829dee1ad47SJeff Kirsher 	}
2830dee1ad47SJeff Kirsher 
2831dee1ad47SJeff Kirsher 	if ((old_vid != (u16)E1000_MNG_VLAN_NONE) && (vid != old_vid))
283280d5c368SPatrick McHardy 		e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q), old_vid);
2833dee1ad47SJeff Kirsher }
2834dee1ad47SJeff Kirsher 
e1000_restore_vlan(struct e1000_adapter * adapter)2835dee1ad47SJeff Kirsher static void e1000_restore_vlan(struct e1000_adapter *adapter)
2836dee1ad47SJeff Kirsher {
2837dee1ad47SJeff Kirsher 	u16 vid;
2838dee1ad47SJeff Kirsher 
283980d5c368SPatrick McHardy 	e1000_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), 0);
2840dee1ad47SJeff Kirsher 
2841dee1ad47SJeff Kirsher 	for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
284280d5c368SPatrick McHardy 	    e1000_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
2843dee1ad47SJeff Kirsher }
2844dee1ad47SJeff Kirsher 
e1000_init_manageability_pt(struct e1000_adapter * adapter)2845dee1ad47SJeff Kirsher static void e1000_init_manageability_pt(struct e1000_adapter *adapter)
2846dee1ad47SJeff Kirsher {
2847dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2848dee1ad47SJeff Kirsher 	u32 manc, manc2h, mdef, i, j;
2849dee1ad47SJeff Kirsher 
2850dee1ad47SJeff Kirsher 	if (!(adapter->flags & FLAG_MNG_PT_ENABLED))
2851dee1ad47SJeff Kirsher 		return;
2852dee1ad47SJeff Kirsher 
2853dee1ad47SJeff Kirsher 	manc = er32(MANC);
2854dee1ad47SJeff Kirsher 
2855e921eb1aSBruce Allan 	/* enable receiving management packets to the host. this will probably
2856dee1ad47SJeff Kirsher 	 * generate destination unreachable messages from the host OS, but
2857dee1ad47SJeff Kirsher 	 * the packets will be handled on SMBUS
2858dee1ad47SJeff Kirsher 	 */
2859dee1ad47SJeff Kirsher 	manc |= E1000_MANC_EN_MNG2HOST;
2860dee1ad47SJeff Kirsher 	manc2h = er32(MANC2H);
2861dee1ad47SJeff Kirsher 
2862dee1ad47SJeff Kirsher 	switch (hw->mac.type) {
2863dee1ad47SJeff Kirsher 	default:
2864dee1ad47SJeff Kirsher 		manc2h |= (E1000_MANC2H_PORT_623 | E1000_MANC2H_PORT_664);
2865dee1ad47SJeff Kirsher 		break;
2866dee1ad47SJeff Kirsher 	case e1000_82574:
2867dee1ad47SJeff Kirsher 	case e1000_82583:
2868e921eb1aSBruce Allan 		/* Check if IPMI pass-through decision filter already exists;
2869dee1ad47SJeff Kirsher 		 * if so, enable it.
2870dee1ad47SJeff Kirsher 		 */
2871dee1ad47SJeff Kirsher 		for (i = 0, j = 0; i < 8; i++) {
2872dee1ad47SJeff Kirsher 			mdef = er32(MDEF(i));
2873dee1ad47SJeff Kirsher 
2874dee1ad47SJeff Kirsher 			/* Ignore filters with anything other than IPMI ports */
2875dee1ad47SJeff Kirsher 			if (mdef & ~(E1000_MDEF_PORT_623 | E1000_MDEF_PORT_664))
2876dee1ad47SJeff Kirsher 				continue;
2877dee1ad47SJeff Kirsher 
2878dee1ad47SJeff Kirsher 			/* Enable this decision filter in MANC2H */
2879dee1ad47SJeff Kirsher 			if (mdef)
288018dd2392SJacob Keller 				manc2h |= BIT(i);
2881dee1ad47SJeff Kirsher 
2882dee1ad47SJeff Kirsher 			j |= mdef;
2883dee1ad47SJeff Kirsher 		}
2884dee1ad47SJeff Kirsher 
2885dee1ad47SJeff Kirsher 		if (j == (E1000_MDEF_PORT_623 | E1000_MDEF_PORT_664))
2886dee1ad47SJeff Kirsher 			break;
2887dee1ad47SJeff Kirsher 
2888dee1ad47SJeff Kirsher 		/* Create new decision filter in an empty filter */
2889dee1ad47SJeff Kirsher 		for (i = 0, j = 0; i < 8; i++)
2890dee1ad47SJeff Kirsher 			if (er32(MDEF(i)) == 0) {
2891dee1ad47SJeff Kirsher 				ew32(MDEF(i), (E1000_MDEF_PORT_623 |
2892dee1ad47SJeff Kirsher 					       E1000_MDEF_PORT_664));
289318dd2392SJacob Keller 				manc2h |= BIT(1);
2894dee1ad47SJeff Kirsher 				j++;
2895dee1ad47SJeff Kirsher 				break;
2896dee1ad47SJeff Kirsher 			}
2897dee1ad47SJeff Kirsher 
2898dee1ad47SJeff Kirsher 		if (!j)
2899dee1ad47SJeff Kirsher 			e_warn("Unable to create IPMI pass-through filter\n");
2900dee1ad47SJeff Kirsher 		break;
2901dee1ad47SJeff Kirsher 	}
2902dee1ad47SJeff Kirsher 
2903dee1ad47SJeff Kirsher 	ew32(MANC2H, manc2h);
2904dee1ad47SJeff Kirsher 	ew32(MANC, manc);
2905dee1ad47SJeff Kirsher }
2906dee1ad47SJeff Kirsher 
2907dee1ad47SJeff Kirsher /**
2908dee1ad47SJeff Kirsher  * e1000_configure_tx - Configure Transmit Unit after Reset
2909dee1ad47SJeff Kirsher  * @adapter: board private structure
2910dee1ad47SJeff Kirsher  *
2911dee1ad47SJeff Kirsher  * Configure the Tx unit of the MAC after a reset.
2912dee1ad47SJeff Kirsher  **/
e1000_configure_tx(struct e1000_adapter * adapter)2913dee1ad47SJeff Kirsher static void e1000_configure_tx(struct e1000_adapter *adapter)
2914dee1ad47SJeff Kirsher {
2915dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
2916dee1ad47SJeff Kirsher 	struct e1000_ring *tx_ring = adapter->tx_ring;
2917dee1ad47SJeff Kirsher 	u64 tdba;
2918e7e834aaSDavid Ertman 	u32 tdlen, tctl, tarc;
2919dee1ad47SJeff Kirsher 
2920dee1ad47SJeff Kirsher 	/* Setup the HW Tx Head and Tail descriptor pointers */
2921dee1ad47SJeff Kirsher 	tdba = tx_ring->dma;
2922dee1ad47SJeff Kirsher 	tdlen = tx_ring->count * sizeof(struct e1000_tx_desc);
29231e36052eSBruce Allan 	ew32(TDBAL(0), (tdba & DMA_BIT_MASK(32)));
29241e36052eSBruce Allan 	ew32(TDBAH(0), (tdba >> 32));
29251e36052eSBruce Allan 	ew32(TDLEN(0), tdlen);
29261e36052eSBruce Allan 	ew32(TDH(0), 0);
29271e36052eSBruce Allan 	ew32(TDT(0), 0);
29281e36052eSBruce Allan 	tx_ring->head = adapter->hw.hw_addr + E1000_TDH(0);
29291e36052eSBruce Allan 	tx_ring->tail = adapter->hw.hw_addr + E1000_TDT(0);
2930dee1ad47SJeff Kirsher 
29310845d45eSJia-Ju Bai 	writel(0, tx_ring->head);
29320845d45eSJia-Ju Bai 	if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
29330845d45eSJia-Ju Bai 		e1000e_update_tdt_wa(tx_ring, 0);
29340845d45eSJia-Ju Bai 	else
29350845d45eSJia-Ju Bai 		writel(0, tx_ring->tail);
29360845d45eSJia-Ju Bai 
2937dee1ad47SJeff Kirsher 	/* Set the Tx Interrupt Delay register */
2938dee1ad47SJeff Kirsher 	ew32(TIDV, adapter->tx_int_delay);
2939dee1ad47SJeff Kirsher 	/* Tx irq moderation */
2940dee1ad47SJeff Kirsher 	ew32(TADV, adapter->tx_abs_int_delay);
2941dee1ad47SJeff Kirsher 
2942dee1ad47SJeff Kirsher 	if (adapter->flags2 & FLAG2_DMA_BURST) {
2943dee1ad47SJeff Kirsher 		u32 txdctl = er32(TXDCTL(0));
29446cf08d1cSDavid Ertman 
2945dee1ad47SJeff Kirsher 		txdctl &= ~(E1000_TXDCTL_PTHRESH | E1000_TXDCTL_HTHRESH |
2946dee1ad47SJeff Kirsher 			    E1000_TXDCTL_WTHRESH);
2947e921eb1aSBruce Allan 		/* set up some performance related parameters to encourage the
2948dee1ad47SJeff Kirsher 		 * hardware to use the bus more efficiently in bursts, depends
2949dee1ad47SJeff Kirsher 		 * on the tx_int_delay to be enabled,
29508edc0e62SHiroaki SHIMODA 		 * wthresh = 1 ==> burst write is disabled to avoid Tx stalls
2951dee1ad47SJeff Kirsher 		 * hthresh = 1 ==> prefetch when one or more available
2952dee1ad47SJeff Kirsher 		 * pthresh = 0x1f ==> prefetch if internal cache 31 or less
2953dee1ad47SJeff Kirsher 		 * BEWARE: this seems to work but should be considered first if
2954dee1ad47SJeff Kirsher 		 * there are Tx hangs or other Tx related bugs
2955dee1ad47SJeff Kirsher 		 */
2956dee1ad47SJeff Kirsher 		txdctl |= E1000_TXDCTL_DMA_BURST_ENABLE;
2957dee1ad47SJeff Kirsher 		ew32(TXDCTL(0), txdctl);
2958dee1ad47SJeff Kirsher 	}
295956032be7SBruce Allan 	/* erratum work around: set txdctl the same for both queues */
296056032be7SBruce Allan 	ew32(TXDCTL(1), er32(TXDCTL(0)));
2961dee1ad47SJeff Kirsher 
2962e7e834aaSDavid Ertman 	/* Program the Transmit Control Register */
2963e7e834aaSDavid Ertman 	tctl = er32(TCTL);
2964e7e834aaSDavid Ertman 	tctl &= ~E1000_TCTL_CT;
2965e7e834aaSDavid Ertman 	tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2966e7e834aaSDavid Ertman 		(E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2967e7e834aaSDavid Ertman 
2968dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_TARC_SPEED_MODE_BIT) {
2969dee1ad47SJeff Kirsher 		tarc = er32(TARC(0));
2970e921eb1aSBruce Allan 		/* set the speed mode bit, we'll clear it if we're not at
2971dee1ad47SJeff Kirsher 		 * gigabit link later
2972dee1ad47SJeff Kirsher 		 */
297318dd2392SJacob Keller #define SPEED_MODE_BIT BIT(21)
2974dee1ad47SJeff Kirsher 		tarc |= SPEED_MODE_BIT;
2975dee1ad47SJeff Kirsher 		ew32(TARC(0), tarc);
2976dee1ad47SJeff Kirsher 	}
2977dee1ad47SJeff Kirsher 
2978dee1ad47SJeff Kirsher 	/* errata: program both queues to unweighted RR */
2979dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_TARC_SET_BIT_ZERO) {
2980dee1ad47SJeff Kirsher 		tarc = er32(TARC(0));
2981dee1ad47SJeff Kirsher 		tarc |= 1;
2982dee1ad47SJeff Kirsher 		ew32(TARC(0), tarc);
2983dee1ad47SJeff Kirsher 		tarc = er32(TARC(1));
2984dee1ad47SJeff Kirsher 		tarc |= 1;
2985dee1ad47SJeff Kirsher 		ew32(TARC(1), tarc);
2986dee1ad47SJeff Kirsher 	}
2987dee1ad47SJeff Kirsher 
2988dee1ad47SJeff Kirsher 	/* Setup Transmit Descriptor Settings for eop descriptor */
2989dee1ad47SJeff Kirsher 	adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
2990dee1ad47SJeff Kirsher 
2991dee1ad47SJeff Kirsher 	/* only set IDE if we are delaying interrupts using the timers */
2992dee1ad47SJeff Kirsher 	if (adapter->tx_int_delay)
2993dee1ad47SJeff Kirsher 		adapter->txd_cmd |= E1000_TXD_CMD_IDE;
2994dee1ad47SJeff Kirsher 
2995dee1ad47SJeff Kirsher 	/* enable Report Status bit */
2996dee1ad47SJeff Kirsher 	adapter->txd_cmd |= E1000_TXD_CMD_RS;
2997dee1ad47SJeff Kirsher 
2998e7e834aaSDavid Ertman 	ew32(TCTL, tctl);
2999e7e834aaSDavid Ertman 
300057cde763SBruce Allan 	hw->mac.ops.config_collision_dist(hw);
300179849ebcSDavid Ertman 
3002b10effb9SSasha Neftin 	/* SPT and KBL Si errata workaround to avoid data corruption */
3003b10effb9SSasha Neftin 	if (hw->mac.type == e1000_pch_spt) {
300479849ebcSDavid Ertman 		u32 reg_val;
300579849ebcSDavid Ertman 
300679849ebcSDavid Ertman 		reg_val = er32(IOSFPC);
300779849ebcSDavid Ertman 		reg_val |= E1000_RCTL_RDMTS_HEX;
300879849ebcSDavid Ertman 		ew32(IOSFPC, reg_val);
300979849ebcSDavid Ertman 
301079849ebcSDavid Ertman 		reg_val = er32(TARC(0));
3011c0f4b163SSasha Neftin 		/* SPT and KBL Si errata workaround to avoid Tx hang.
3012c0f4b163SSasha Neftin 		 * Dropping the number of outstanding requests from
3013c0f4b163SSasha Neftin 		 * 3 to 2 in order to avoid a buffer overrun.
3014c0f4b163SSasha Neftin 		 */
3015c0f4b163SSasha Neftin 		reg_val &= ~E1000_TARC0_CB_MULTIQ_3_REQ;
3016c0f4b163SSasha Neftin 		reg_val |= E1000_TARC0_CB_MULTIQ_2_REQ;
301779849ebcSDavid Ertman 		ew32(TARC(0), reg_val);
301879849ebcSDavid Ertman 	}
3019dee1ad47SJeff Kirsher }
3020dee1ad47SJeff Kirsher 
3021b50f7bcaSJesse Brandeburg #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
3022b50f7bcaSJesse Brandeburg 			   (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
3023b50f7bcaSJesse Brandeburg 
3024dee1ad47SJeff Kirsher /**
3025dee1ad47SJeff Kirsher  * e1000_setup_rctl - configure the receive control registers
3026dee1ad47SJeff Kirsher  * @adapter: Board private structure
3027dee1ad47SJeff Kirsher  **/
e1000_setup_rctl(struct e1000_adapter * adapter)3028dee1ad47SJeff Kirsher static void e1000_setup_rctl(struct e1000_adapter *adapter)
3029dee1ad47SJeff Kirsher {
3030dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
3031dee1ad47SJeff Kirsher 	u32 rctl, rfctl;
3032dee1ad47SJeff Kirsher 	u32 pages = 0;
3033dee1ad47SJeff Kirsher 
3034b20a7744SDavid Ertman 	/* Workaround Si errata on PCHx - configure jumbo frame flow.
3035b20a7744SDavid Ertman 	 * If jumbo frames not set, program related MAC/PHY registers
3036b20a7744SDavid Ertman 	 * to h/w defaults
3037b20a7744SDavid Ertman 	 */
3038b20a7744SDavid Ertman 	if (hw->mac.type >= e1000_pch2lan) {
3039b20a7744SDavid Ertman 		s32 ret_val;
3040b20a7744SDavid Ertman 
3041b20a7744SDavid Ertman 		if (adapter->netdev->mtu > ETH_DATA_LEN)
3042b20a7744SDavid Ertman 			ret_val = e1000_lv_jumbo_workaround_ich8lan(hw, true);
3043b20a7744SDavid Ertman 		else
3044b20a7744SDavid Ertman 			ret_val = e1000_lv_jumbo_workaround_ich8lan(hw, false);
3045b20a7744SDavid Ertman 
3046b20a7744SDavid Ertman 		if (ret_val)
3047b20a7744SDavid Ertman 			e_dbg("failed to enable|disable jumbo frame workaround mode\n");
3048b20a7744SDavid Ertman 	}
3049dee1ad47SJeff Kirsher 
3050dee1ad47SJeff Kirsher 	/* Program MC offset vector base */
3051dee1ad47SJeff Kirsher 	rctl = er32(RCTL);
3052dee1ad47SJeff Kirsher 	rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
3053dee1ad47SJeff Kirsher 	rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
3054dee1ad47SJeff Kirsher 	    E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
3055dee1ad47SJeff Kirsher 	    (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
3056dee1ad47SJeff Kirsher 
3057dee1ad47SJeff Kirsher 	/* Do not Store bad packets */
3058dee1ad47SJeff Kirsher 	rctl &= ~E1000_RCTL_SBP;
3059dee1ad47SJeff Kirsher 
3060dee1ad47SJeff Kirsher 	/* Enable Long Packet receive */
3061dee1ad47SJeff Kirsher 	if (adapter->netdev->mtu <= ETH_DATA_LEN)
3062dee1ad47SJeff Kirsher 		rctl &= ~E1000_RCTL_LPE;
3063dee1ad47SJeff Kirsher 	else
3064dee1ad47SJeff Kirsher 		rctl |= E1000_RCTL_LPE;
3065dee1ad47SJeff Kirsher 
3066dee1ad47SJeff Kirsher 	/* Some systems expect that the CRC is included in SMBUS traffic. The
3067dee1ad47SJeff Kirsher 	 * hardware strips the CRC before sending to both SMBUS (BMC) and to
3068dee1ad47SJeff Kirsher 	 * host memory when this is enabled
3069dee1ad47SJeff Kirsher 	 */
3070dee1ad47SJeff Kirsher 	if (adapter->flags2 & FLAG2_CRC_STRIPPING)
3071dee1ad47SJeff Kirsher 		rctl |= E1000_RCTL_SECRC;
3072dee1ad47SJeff Kirsher 
3073dee1ad47SJeff Kirsher 	/* Workaround Si errata on 82577 PHY - configure IPG for jumbos */
3074dee1ad47SJeff Kirsher 	if ((hw->phy.type == e1000_phy_82577) && (rctl & E1000_RCTL_LPE)) {
3075dee1ad47SJeff Kirsher 		u16 phy_data;
3076dee1ad47SJeff Kirsher 
3077dee1ad47SJeff Kirsher 		e1e_rphy(hw, PHY_REG(770, 26), &phy_data);
3078dee1ad47SJeff Kirsher 		phy_data &= 0xfff8;
307918dd2392SJacob Keller 		phy_data |= BIT(2);
3080dee1ad47SJeff Kirsher 		e1e_wphy(hw, PHY_REG(770, 26), phy_data);
3081dee1ad47SJeff Kirsher 
3082dee1ad47SJeff Kirsher 		e1e_rphy(hw, 22, &phy_data);
3083dee1ad47SJeff Kirsher 		phy_data &= 0x0fff;
308418dd2392SJacob Keller 		phy_data |= BIT(14);
3085dee1ad47SJeff Kirsher 		e1e_wphy(hw, 0x10, 0x2823);
3086dee1ad47SJeff Kirsher 		e1e_wphy(hw, 0x11, 0x0003);
3087dee1ad47SJeff Kirsher 		e1e_wphy(hw, 22, phy_data);
3088dee1ad47SJeff Kirsher 	}
3089dee1ad47SJeff Kirsher 
3090dee1ad47SJeff Kirsher 	/* Setup buffer sizes */
3091dee1ad47SJeff Kirsher 	rctl &= ~E1000_RCTL_SZ_4096;
3092dee1ad47SJeff Kirsher 	rctl |= E1000_RCTL_BSEX;
3093dee1ad47SJeff Kirsher 	switch (adapter->rx_buffer_len) {
3094dee1ad47SJeff Kirsher 	case 2048:
3095dee1ad47SJeff Kirsher 	default:
3096dee1ad47SJeff Kirsher 		rctl |= E1000_RCTL_SZ_2048;
3097dee1ad47SJeff Kirsher 		rctl &= ~E1000_RCTL_BSEX;
3098dee1ad47SJeff Kirsher 		break;
3099dee1ad47SJeff Kirsher 	case 4096:
3100dee1ad47SJeff Kirsher 		rctl |= E1000_RCTL_SZ_4096;
3101dee1ad47SJeff Kirsher 		break;
3102dee1ad47SJeff Kirsher 	case 8192:
3103dee1ad47SJeff Kirsher 		rctl |= E1000_RCTL_SZ_8192;
3104dee1ad47SJeff Kirsher 		break;
3105dee1ad47SJeff Kirsher 	case 16384:
3106dee1ad47SJeff Kirsher 		rctl |= E1000_RCTL_SZ_16384;
3107dee1ad47SJeff Kirsher 		break;
3108dee1ad47SJeff Kirsher 	}
3109dee1ad47SJeff Kirsher 
31105f450212SBruce Allan 	/* Enable Extended Status in all Receive Descriptors */
31115f450212SBruce Allan 	rfctl = er32(RFCTL);
31125f450212SBruce Allan 	rfctl |= E1000_RFCTL_EXTEN;
3113f6bd5577SMatthew Vick 	ew32(RFCTL, rfctl);
31145f450212SBruce Allan 
3115e921eb1aSBruce Allan 	/* 82571 and greater support packet-split where the protocol
3116dee1ad47SJeff Kirsher 	 * header is placed in skb->data and the packet data is
3117dee1ad47SJeff Kirsher 	 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
3118dee1ad47SJeff Kirsher 	 * In the case of a non-split, skb->data is linearly filled,
3119dee1ad47SJeff Kirsher 	 * followed by the page buffers.  Therefore, skb->data is
3120dee1ad47SJeff Kirsher 	 * sized to hold the largest protocol header.
3121dee1ad47SJeff Kirsher 	 *
3122dee1ad47SJeff Kirsher 	 * allocations using alloc_page take too long for regular MTU
3123dee1ad47SJeff Kirsher 	 * so only enable packet split for jumbo frames
3124dee1ad47SJeff Kirsher 	 *
3125dee1ad47SJeff Kirsher 	 * Using pages when the page size is greater than 16k wastes
3126dee1ad47SJeff Kirsher 	 * a lot of memory, since we allocate 3 pages at all times
3127dee1ad47SJeff Kirsher 	 * per packet.
3128dee1ad47SJeff Kirsher 	 */
3129dee1ad47SJeff Kirsher 	pages = PAGE_USE_COUNT(adapter->netdev->mtu);
313079d4e908SBruce Allan 	if ((pages <= 3) && (PAGE_SIZE <= 16384) && (rctl & E1000_RCTL_LPE))
3131dee1ad47SJeff Kirsher 		adapter->rx_ps_pages = pages;
3132dee1ad47SJeff Kirsher 	else
3133dee1ad47SJeff Kirsher 		adapter->rx_ps_pages = 0;
3134dee1ad47SJeff Kirsher 
3135dee1ad47SJeff Kirsher 	if (adapter->rx_ps_pages) {
3136dee1ad47SJeff Kirsher 		u32 psrctl = 0;
3137dee1ad47SJeff Kirsher 
3138dee1ad47SJeff Kirsher 		/* Enable Packet split descriptors */
3139dee1ad47SJeff Kirsher 		rctl |= E1000_RCTL_DTYP_PS;
3140dee1ad47SJeff Kirsher 
3141e5fe2541SBruce Allan 		psrctl |= adapter->rx_ps_bsize0 >> E1000_PSRCTL_BSIZE0_SHIFT;
3142dee1ad47SJeff Kirsher 
3143dee1ad47SJeff Kirsher 		switch (adapter->rx_ps_pages) {
3144dee1ad47SJeff Kirsher 		case 3:
3145e5fe2541SBruce Allan 			psrctl |= PAGE_SIZE << E1000_PSRCTL_BSIZE3_SHIFT;
31465463fce6SJeff Kirsher 			fallthrough;
3147dee1ad47SJeff Kirsher 		case 2:
3148e5fe2541SBruce Allan 			psrctl |= PAGE_SIZE << E1000_PSRCTL_BSIZE2_SHIFT;
31495463fce6SJeff Kirsher 			fallthrough;
3150dee1ad47SJeff Kirsher 		case 1:
3151e5fe2541SBruce Allan 			psrctl |= PAGE_SIZE >> E1000_PSRCTL_BSIZE1_SHIFT;
3152dee1ad47SJeff Kirsher 			break;
3153dee1ad47SJeff Kirsher 		}
3154dee1ad47SJeff Kirsher 
3155dee1ad47SJeff Kirsher 		ew32(PSRCTL, psrctl);
3156dee1ad47SJeff Kirsher 	}
3157dee1ad47SJeff Kirsher 
3158cf955e6cSBen Greear 	/* This is useful for sniffing bad packets. */
3159cf955e6cSBen Greear 	if (adapter->netdev->features & NETIF_F_RXALL) {
3160cf955e6cSBen Greear 		/* UPE and MPE will be handled by normal PROMISC logic
3161e921eb1aSBruce Allan 		 * in e1000e_set_rx_mode
3162e921eb1aSBruce Allan 		 */
3163cf955e6cSBen Greear 		rctl |= (E1000_RCTL_SBP |	/* Receive bad packets */
3164cf955e6cSBen Greear 			 E1000_RCTL_BAM |	/* RX All Bcast Pkts */
3165cf955e6cSBen Greear 			 E1000_RCTL_PMCF);	/* RX All MAC Ctrl Pkts */
3166cf955e6cSBen Greear 
3167cf955e6cSBen Greear 		rctl &= ~(E1000_RCTL_VFE |	/* Disable VLAN filter */
3168cf955e6cSBen Greear 			  E1000_RCTL_DPF |	/* Allow filtered pause */
3169cf955e6cSBen Greear 			  E1000_RCTL_CFIEN);	/* Dis VLAN CFIEN Filter */
3170cf955e6cSBen Greear 		/* Do not mess with E1000_CTRL_VME, it affects transmit as well,
3171cf955e6cSBen Greear 		 * and that breaks VLANs.
3172cf955e6cSBen Greear 		 */
3173cf955e6cSBen Greear 	}
3174cf955e6cSBen Greear 
3175dee1ad47SJeff Kirsher 	ew32(RCTL, rctl);
3176dee1ad47SJeff Kirsher 	/* just started the receive unit, no need to restart */
317712d43f7dSBruce Allan 	adapter->flags &= ~FLAG_RESTART_NOW;
3178dee1ad47SJeff Kirsher }
3179dee1ad47SJeff Kirsher 
3180dee1ad47SJeff Kirsher /**
3181dee1ad47SJeff Kirsher  * e1000_configure_rx - Configure Receive Unit after Reset
3182dee1ad47SJeff Kirsher  * @adapter: board private structure
3183dee1ad47SJeff Kirsher  *
3184dee1ad47SJeff Kirsher  * Configure the Rx unit of the MAC after a reset.
3185dee1ad47SJeff Kirsher  **/
e1000_configure_rx(struct e1000_adapter * adapter)3186dee1ad47SJeff Kirsher static void e1000_configure_rx(struct e1000_adapter *adapter)
3187dee1ad47SJeff Kirsher {
3188dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
3189dee1ad47SJeff Kirsher 	struct e1000_ring *rx_ring = adapter->rx_ring;
3190dee1ad47SJeff Kirsher 	u64 rdba;
3191dee1ad47SJeff Kirsher 	u32 rdlen, rctl, rxcsum, ctrl_ext;
3192dee1ad47SJeff Kirsher 
3193dee1ad47SJeff Kirsher 	if (adapter->rx_ps_pages) {
3194dee1ad47SJeff Kirsher 		/* this is a 32 byte descriptor */
3195dee1ad47SJeff Kirsher 		rdlen = rx_ring->count *
3196dee1ad47SJeff Kirsher 		    sizeof(union e1000_rx_desc_packet_split);
3197dee1ad47SJeff Kirsher 		adapter->clean_rx = e1000_clean_rx_irq_ps;
3198dee1ad47SJeff Kirsher 		adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
3199dee1ad47SJeff Kirsher 	} else if (adapter->netdev->mtu > ETH_FRAME_LEN + ETH_FCS_LEN) {
32005f450212SBruce Allan 		rdlen = rx_ring->count * sizeof(union e1000_rx_desc_extended);
3201dee1ad47SJeff Kirsher 		adapter->clean_rx = e1000_clean_jumbo_rx_irq;
3202dee1ad47SJeff Kirsher 		adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
3203dee1ad47SJeff Kirsher 	} else {
32045f450212SBruce Allan 		rdlen = rx_ring->count * sizeof(union e1000_rx_desc_extended);
3205dee1ad47SJeff Kirsher 		adapter->clean_rx = e1000_clean_rx_irq;
3206dee1ad47SJeff Kirsher 		adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
3207dee1ad47SJeff Kirsher 	}
3208dee1ad47SJeff Kirsher 
3209dee1ad47SJeff Kirsher 	/* disable receives while setting up the descriptors */
3210dee1ad47SJeff Kirsher 	rctl = er32(RCTL);
3211823dcd25SDavid S. Miller 	if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
3212dee1ad47SJeff Kirsher 		ew32(RCTL, rctl & ~E1000_RCTL_EN);
3213dee1ad47SJeff Kirsher 	e1e_flush();
3214ab6973aeSArjan van de Ven 	usleep_range(10000, 11000);
3215dee1ad47SJeff Kirsher 
3216dee1ad47SJeff Kirsher 	if (adapter->flags2 & FLAG2_DMA_BURST) {
3217e921eb1aSBruce Allan 		/* set the writeback threshold (only takes effect if the RDTR
3218dee1ad47SJeff Kirsher 		 * is set). set GRAN=1 and write back up to 0x4 worth, and
3219dee1ad47SJeff Kirsher 		 * enable prefetching of 0x20 Rx descriptors
3220dee1ad47SJeff Kirsher 		 * granularity = 01
3221dee1ad47SJeff Kirsher 		 * wthresh = 04,
3222dee1ad47SJeff Kirsher 		 * hthresh = 04,
3223dee1ad47SJeff Kirsher 		 * pthresh = 0x20
3224dee1ad47SJeff Kirsher 		 */
3225dee1ad47SJeff Kirsher 		ew32(RXDCTL(0), E1000_RXDCTL_DMA_BURST_ENABLE);
3226dee1ad47SJeff Kirsher 		ew32(RXDCTL(1), E1000_RXDCTL_DMA_BURST_ENABLE);
3227dee1ad47SJeff Kirsher 	}
3228dee1ad47SJeff Kirsher 
3229dee1ad47SJeff Kirsher 	/* set the Receive Delay Timer Register */
3230dee1ad47SJeff Kirsher 	ew32(RDTR, adapter->rx_int_delay);
3231dee1ad47SJeff Kirsher 
3232dee1ad47SJeff Kirsher 	/* irq moderation */
3233dee1ad47SJeff Kirsher 	ew32(RADV, adapter->rx_abs_int_delay);
3234dee1ad47SJeff Kirsher 	if ((adapter->itr_setting != 0) && (adapter->itr != 0))
323522a4cca2SMatthew Vick 		e1000e_write_itr(adapter, adapter->itr);
3236dee1ad47SJeff Kirsher 
3237dee1ad47SJeff Kirsher 	ctrl_ext = er32(CTRL_EXT);
3238dee1ad47SJeff Kirsher 	/* Auto-Mask interrupts upon ICR access */
3239dee1ad47SJeff Kirsher 	ctrl_ext |= E1000_CTRL_EXT_IAME;
3240dee1ad47SJeff Kirsher 	ew32(IAM, 0xffffffff);
3241dee1ad47SJeff Kirsher 	ew32(CTRL_EXT, ctrl_ext);
3242dee1ad47SJeff Kirsher 	e1e_flush();
3243dee1ad47SJeff Kirsher 
3244e921eb1aSBruce Allan 	/* Setup the HW Rx Head and Tail Descriptor Pointers and
3245dee1ad47SJeff Kirsher 	 * the Base and Length of the Rx Descriptor Ring
3246dee1ad47SJeff Kirsher 	 */
3247dee1ad47SJeff Kirsher 	rdba = rx_ring->dma;
32481e36052eSBruce Allan 	ew32(RDBAL(0), (rdba & DMA_BIT_MASK(32)));
32491e36052eSBruce Allan 	ew32(RDBAH(0), (rdba >> 32));
32501e36052eSBruce Allan 	ew32(RDLEN(0), rdlen);
32511e36052eSBruce Allan 	ew32(RDH(0), 0);
32521e36052eSBruce Allan 	ew32(RDT(0), 0);
32531e36052eSBruce Allan 	rx_ring->head = adapter->hw.hw_addr + E1000_RDH(0);
32541e36052eSBruce Allan 	rx_ring->tail = adapter->hw.hw_addr + E1000_RDT(0);
3255dee1ad47SJeff Kirsher 
32560845d45eSJia-Ju Bai 	writel(0, rx_ring->head);
32570845d45eSJia-Ju Bai 	if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
32580845d45eSJia-Ju Bai 		e1000e_update_rdt_wa(rx_ring, 0);
32590845d45eSJia-Ju Bai 	else
32600845d45eSJia-Ju Bai 		writel(0, rx_ring->tail);
32610845d45eSJia-Ju Bai 
3262dee1ad47SJeff Kirsher 	/* Enable Receive Checksum Offload for TCP and UDP */
3263dee1ad47SJeff Kirsher 	rxcsum = er32(RXCSUM);
32642e1706f2SBruce Allan 	if (adapter->netdev->features & NETIF_F_RXCSUM)
3265dee1ad47SJeff Kirsher 		rxcsum |= E1000_RXCSUM_TUOFL;
32662e1706f2SBruce Allan 	else
3267dee1ad47SJeff Kirsher 		rxcsum &= ~E1000_RXCSUM_TUOFL;
3268dee1ad47SJeff Kirsher 	ew32(RXCSUM, rxcsum);
3269dee1ad47SJeff Kirsher 
32703e35d991SBruce Allan 	/* With jumbo frames, excessive C-state transition latencies result
32713e35d991SBruce Allan 	 * in dropped transactions.
3272dee1ad47SJeff Kirsher 	 */
3273dee1ad47SJeff Kirsher 	if (adapter->netdev->mtu > ETH_DATA_LEN) {
32743e35d991SBruce Allan 		u32 lat =
32753e35d991SBruce Allan 		    ((er32(PBA) & E1000_PBA_RXA_MASK) * 1024 -
32763e35d991SBruce Allan 		     adapter->max_frame_size) * 8 / 1000;
32773e35d991SBruce Allan 
32783e35d991SBruce Allan 		if (adapter->flags & FLAG_IS_ICH) {
3279dee1ad47SJeff Kirsher 			u32 rxdctl = er32(RXDCTL(0));
32806cf08d1cSDavid Ertman 
3281b701cacdSMatt Turner 			ew32(RXDCTL(0), rxdctl | 0x3 | BIT(8));
32823e35d991SBruce Allan 		}
32833e35d991SBruce Allan 
32848299b006SMatt Turner 		dev_info(&adapter->pdev->dev,
32858299b006SMatt Turner 			 "Some CPU C-states have been disabled in order to enable jumbo frames\n");
328681e95ad7SRafael J. Wysocki 		cpu_latency_qos_update_request(&adapter->pm_qos_req, lat);
3287dee1ad47SJeff Kirsher 	} else {
328881e95ad7SRafael J. Wysocki 		cpu_latency_qos_update_request(&adapter->pm_qos_req,
3289dee1ad47SJeff Kirsher 					       PM_QOS_DEFAULT_VALUE);
3290dee1ad47SJeff Kirsher 	}
3291dee1ad47SJeff Kirsher 
3292dee1ad47SJeff Kirsher 	/* Enable Receives */
3293dee1ad47SJeff Kirsher 	ew32(RCTL, rctl);
3294dee1ad47SJeff Kirsher }
3295dee1ad47SJeff Kirsher 
3296dee1ad47SJeff Kirsher /**
3297ef9b965aSJesse Brandeburg  * e1000e_write_mc_addr_list - write multicast addresses to MTA
3298dee1ad47SJeff Kirsher  * @netdev: network interface device structure
3299dee1ad47SJeff Kirsher  *
3300ef9b965aSJesse Brandeburg  * Writes multicast address list to the MTA hash table.
3301ef9b965aSJesse Brandeburg  * Returns: -ENOMEM on failure
3302ef9b965aSJesse Brandeburg  *                0 on no addresses written
3303ef9b965aSJesse Brandeburg  *                X on writing X addresses to MTA
3304ef9b965aSJesse Brandeburg  */
e1000e_write_mc_addr_list(struct net_device * netdev)3305ef9b965aSJesse Brandeburg static int e1000e_write_mc_addr_list(struct net_device *netdev)
3306dee1ad47SJeff Kirsher {
3307dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
3308dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
3309dee1ad47SJeff Kirsher 	struct netdev_hw_addr *ha;
3310dee1ad47SJeff Kirsher 	u8 *mta_list;
3311ef9b965aSJesse Brandeburg 	int i;
3312dee1ad47SJeff Kirsher 
3313ef9b965aSJesse Brandeburg 	if (netdev_mc_empty(netdev)) {
3314ef9b965aSJesse Brandeburg 		/* nothing to program, so clear mc list */
3315ef9b965aSJesse Brandeburg 		hw->mac.ops.update_mc_addr_list(hw, NULL, 0);
3316ef9b965aSJesse Brandeburg 		return 0;
3317dee1ad47SJeff Kirsher 	}
3318dee1ad47SJeff Kirsher 
33196396bb22SKees Cook 	mta_list = kcalloc(netdev_mc_count(netdev), ETH_ALEN, GFP_ATOMIC);
3320dee1ad47SJeff Kirsher 	if (!mta_list)
3321ef9b965aSJesse Brandeburg 		return -ENOMEM;
3322dee1ad47SJeff Kirsher 
3323ef9b965aSJesse Brandeburg 	/* update_mc_addr_list expects a packed array of only addresses. */
3324ef9b965aSJesse Brandeburg 	i = 0;
3325dee1ad47SJeff Kirsher 	netdev_for_each_mc_addr(ha, netdev)
3326dee1ad47SJeff Kirsher 	    memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
3327dee1ad47SJeff Kirsher 
3328ef9b965aSJesse Brandeburg 	hw->mac.ops.update_mc_addr_list(hw, mta_list, i);
3329dee1ad47SJeff Kirsher 	kfree(mta_list);
3330ef9b965aSJesse Brandeburg 
3331ef9b965aSJesse Brandeburg 	return netdev_mc_count(netdev);
3332ef9b965aSJesse Brandeburg }
3333ef9b965aSJesse Brandeburg 
3334ef9b965aSJesse Brandeburg /**
3335ef9b965aSJesse Brandeburg  * e1000e_write_uc_addr_list - write unicast addresses to RAR table
3336ef9b965aSJesse Brandeburg  * @netdev: network interface device structure
3337ef9b965aSJesse Brandeburg  *
3338ef9b965aSJesse Brandeburg  * Writes unicast address list to the RAR table.
3339ef9b965aSJesse Brandeburg  * Returns: -ENOMEM on failure/insufficient address space
3340ef9b965aSJesse Brandeburg  *                0 on no addresses written
3341ef9b965aSJesse Brandeburg  *                X on writing X addresses to the RAR table
3342ef9b965aSJesse Brandeburg  **/
e1000e_write_uc_addr_list(struct net_device * netdev)3343ef9b965aSJesse Brandeburg static int e1000e_write_uc_addr_list(struct net_device *netdev)
3344ef9b965aSJesse Brandeburg {
3345ef9b965aSJesse Brandeburg 	struct e1000_adapter *adapter = netdev_priv(netdev);
3346ef9b965aSJesse Brandeburg 	struct e1000_hw *hw = &adapter->hw;
3347b3e5bf1fSDavid Ertman 	unsigned int rar_entries;
3348ef9b965aSJesse Brandeburg 	int count = 0;
3349ef9b965aSJesse Brandeburg 
3350b3e5bf1fSDavid Ertman 	rar_entries = hw->mac.ops.rar_get_count(hw);
3351b3e5bf1fSDavid Ertman 
3352ef9b965aSJesse Brandeburg 	/* save a rar entry for our hardware address */
3353ef9b965aSJesse Brandeburg 	rar_entries--;
3354ef9b965aSJesse Brandeburg 
3355ef9b965aSJesse Brandeburg 	/* save a rar entry for the LAA workaround */
3356ef9b965aSJesse Brandeburg 	if (adapter->flags & FLAG_RESET_OVERWRITES_LAA)
3357ef9b965aSJesse Brandeburg 		rar_entries--;
3358ef9b965aSJesse Brandeburg 
3359ef9b965aSJesse Brandeburg 	/* return ENOMEM indicating insufficient memory for addresses */
3360ef9b965aSJesse Brandeburg 	if (netdev_uc_count(netdev) > rar_entries)
3361ef9b965aSJesse Brandeburg 		return -ENOMEM;
3362ef9b965aSJesse Brandeburg 
3363ef9b965aSJesse Brandeburg 	if (!netdev_uc_empty(netdev) && rar_entries) {
3364ef9b965aSJesse Brandeburg 		struct netdev_hw_addr *ha;
3365ef9b965aSJesse Brandeburg 
3366e921eb1aSBruce Allan 		/* write the addresses in reverse order to avoid write
3367ef9b965aSJesse Brandeburg 		 * combining
3368ef9b965aSJesse Brandeburg 		 */
3369ef9b965aSJesse Brandeburg 		netdev_for_each_uc_addr(ha, netdev) {
3370847042a6SBrian Walsh 			int ret_val;
3371b3e5bf1fSDavid Ertman 
3372ef9b965aSJesse Brandeburg 			if (!rar_entries)
3373ef9b965aSJesse Brandeburg 				break;
3374847042a6SBrian Walsh 			ret_val = hw->mac.ops.rar_set(hw, ha->addr, rar_entries--);
3375847042a6SBrian Walsh 			if (ret_val < 0)
3376b3e5bf1fSDavid Ertman 				return -ENOMEM;
3377ef9b965aSJesse Brandeburg 			count++;
3378ef9b965aSJesse Brandeburg 		}
3379ef9b965aSJesse Brandeburg 	}
3380ef9b965aSJesse Brandeburg 
3381ef9b965aSJesse Brandeburg 	/* zero out the remaining RAR entries not used above */
3382ef9b965aSJesse Brandeburg 	for (; rar_entries > 0; rar_entries--) {
3383ef9b965aSJesse Brandeburg 		ew32(RAH(rar_entries), 0);
3384ef9b965aSJesse Brandeburg 		ew32(RAL(rar_entries), 0);
3385ef9b965aSJesse Brandeburg 	}
3386ef9b965aSJesse Brandeburg 	e1e_flush();
3387ef9b965aSJesse Brandeburg 
3388ef9b965aSJesse Brandeburg 	return count;
3389ef9b965aSJesse Brandeburg }
3390ef9b965aSJesse Brandeburg 
3391ef9b965aSJesse Brandeburg /**
3392ef9b965aSJesse Brandeburg  * e1000e_set_rx_mode - secondary unicast, Multicast and Promiscuous mode set
3393ef9b965aSJesse Brandeburg  * @netdev: network interface device structure
3394ef9b965aSJesse Brandeburg  *
3395ef9b965aSJesse Brandeburg  * The ndo_set_rx_mode entry point is called whenever the unicast or multicast
3396ef9b965aSJesse Brandeburg  * address list or the network interface flags are updated.  This routine is
3397ef9b965aSJesse Brandeburg  * responsible for configuring the hardware for proper unicast, multicast,
3398ef9b965aSJesse Brandeburg  * promiscuous mode, and all-multi behavior.
3399ef9b965aSJesse Brandeburg  **/
e1000e_set_rx_mode(struct net_device * netdev)3400ef9b965aSJesse Brandeburg static void e1000e_set_rx_mode(struct net_device *netdev)
3401ef9b965aSJesse Brandeburg {
3402ef9b965aSJesse Brandeburg 	struct e1000_adapter *adapter = netdev_priv(netdev);
3403ef9b965aSJesse Brandeburg 	struct e1000_hw *hw = &adapter->hw;
3404ef9b965aSJesse Brandeburg 	u32 rctl;
3405ef9b965aSJesse Brandeburg 
340663eb48f1SDavid Ertman 	if (pm_runtime_suspended(netdev->dev.parent))
340763eb48f1SDavid Ertman 		return;
340863eb48f1SDavid Ertman 
3409ef9b965aSJesse Brandeburg 	/* Check for Promiscuous and All Multicast modes */
3410ef9b965aSJesse Brandeburg 	rctl = er32(RCTL);
3411ef9b965aSJesse Brandeburg 
3412ef9b965aSJesse Brandeburg 	/* clear the affected bits */
3413ef9b965aSJesse Brandeburg 	rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
3414ef9b965aSJesse Brandeburg 
3415ef9b965aSJesse Brandeburg 	if (netdev->flags & IFF_PROMISC) {
3416ef9b965aSJesse Brandeburg 		rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
3417ef9b965aSJesse Brandeburg 		/* Do not hardware filter VLANs in promisc mode */
3418ef9b965aSJesse Brandeburg 		e1000e_vlan_filter_disable(adapter);
3419ef9b965aSJesse Brandeburg 	} else {
3420ef9b965aSJesse Brandeburg 		int count;
34213d3a1676SBruce Allan 
3422ef9b965aSJesse Brandeburg 		if (netdev->flags & IFF_ALLMULTI) {
3423ef9b965aSJesse Brandeburg 			rctl |= E1000_RCTL_MPE;
3424dee1ad47SJeff Kirsher 		} else {
3425e921eb1aSBruce Allan 			/* Write addresses to the MTA, if the attempt fails
3426ef9b965aSJesse Brandeburg 			 * then we should just turn on promiscuous mode so
3427ef9b965aSJesse Brandeburg 			 * that we can at least receive multicast traffic
3428dee1ad47SJeff Kirsher 			 */
3429ef9b965aSJesse Brandeburg 			count = e1000e_write_mc_addr_list(netdev);
3430ef9b965aSJesse Brandeburg 			if (count < 0)
3431ef9b965aSJesse Brandeburg 				rctl |= E1000_RCTL_MPE;
3432dee1ad47SJeff Kirsher 		}
3433ef9b965aSJesse Brandeburg 		e1000e_vlan_filter_enable(adapter);
3434e921eb1aSBruce Allan 		/* Write addresses to available RAR registers, if there is not
3435ef9b965aSJesse Brandeburg 		 * sufficient space to store all the addresses then enable
3436ef9b965aSJesse Brandeburg 		 * unicast promiscuous mode
3437ef9b965aSJesse Brandeburg 		 */
3438ef9b965aSJesse Brandeburg 		count = e1000e_write_uc_addr_list(netdev);
3439ef9b965aSJesse Brandeburg 		if (count < 0)
3440ef9b965aSJesse Brandeburg 			rctl |= E1000_RCTL_UPE;
3441ef9b965aSJesse Brandeburg 	}
3442ef9b965aSJesse Brandeburg 
3443ef9b965aSJesse Brandeburg 	ew32(RCTL, rctl);
3444dee1ad47SJeff Kirsher 
344583808641SJarod Wilson 	if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3446dee1ad47SJeff Kirsher 		e1000e_vlan_strip_enable(adapter);
3447dee1ad47SJeff Kirsher 	else
3448dee1ad47SJeff Kirsher 		e1000e_vlan_strip_disable(adapter);
3449dee1ad47SJeff Kirsher }
3450dee1ad47SJeff Kirsher 
e1000e_setup_rss_hash(struct e1000_adapter * adapter)345170495a50SBruce Allan static void e1000e_setup_rss_hash(struct e1000_adapter *adapter)
345270495a50SBruce Allan {
345370495a50SBruce Allan 	struct e1000_hw *hw = &adapter->hw;
345470495a50SBruce Allan 	u32 mrqc, rxcsum;
34555c8d19daSEric Dumazet 	u32 rss_key[10];
345670495a50SBruce Allan 	int i;
345770495a50SBruce Allan 
34585c8d19daSEric Dumazet 	netdev_rss_key_fill(rss_key, sizeof(rss_key));
345970495a50SBruce Allan 	for (i = 0; i < 10; i++)
34605c8d19daSEric Dumazet 		ew32(RSSRK(i), rss_key[i]);
346170495a50SBruce Allan 
346270495a50SBruce Allan 	/* Direct all traffic to queue 0 */
346370495a50SBruce Allan 	for (i = 0; i < 32; i++)
346470495a50SBruce Allan 		ew32(RETA(i), 0);
346570495a50SBruce Allan 
3466e921eb1aSBruce Allan 	/* Disable raw packet checksumming so that RSS hash is placed in
346770495a50SBruce Allan 	 * descriptor on writeback.
346870495a50SBruce Allan 	 */
346970495a50SBruce Allan 	rxcsum = er32(RXCSUM);
347070495a50SBruce Allan 	rxcsum |= E1000_RXCSUM_PCSD;
347170495a50SBruce Allan 
347270495a50SBruce Allan 	ew32(RXCSUM, rxcsum);
347370495a50SBruce Allan 
347470495a50SBruce Allan 	mrqc = (E1000_MRQC_RSS_FIELD_IPV4 |
347570495a50SBruce Allan 		E1000_MRQC_RSS_FIELD_IPV4_TCP |
347670495a50SBruce Allan 		E1000_MRQC_RSS_FIELD_IPV6 |
347770495a50SBruce Allan 		E1000_MRQC_RSS_FIELD_IPV6_TCP |
347870495a50SBruce Allan 		E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
347970495a50SBruce Allan 
348070495a50SBruce Allan 	ew32(MRQC, mrqc);
348170495a50SBruce Allan }
348270495a50SBruce Allan 
3483dee1ad47SJeff Kirsher /**
3484b67e1913SBruce Allan  * e1000e_get_base_timinca - get default SYSTIM time increment attributes
3485b67e1913SBruce Allan  * @adapter: board private structure
3486b67e1913SBruce Allan  * @timinca: pointer to returned time increment attributes
3487b67e1913SBruce Allan  *
3488b67e1913SBruce Allan  * Get attributes for incrementing the System Time Register SYSTIML/H at
3489b67e1913SBruce Allan  * the default base frequency, and set the cyclecounter shift value.
3490b67e1913SBruce Allan  **/
e1000e_get_base_timinca(struct e1000_adapter * adapter,u32 * timinca)3491d89777bfSBruce Allan s32 e1000e_get_base_timinca(struct e1000_adapter *adapter, u32 *timinca)
3492b67e1913SBruce Allan {
3493b67e1913SBruce Allan 	struct e1000_hw *hw = &adapter->hw;
3494b67e1913SBruce Allan 	u32 incvalue, incperiod, shift;
3495b67e1913SBruce Allan 
349679849ebcSDavid Ertman 	/* Make sure clock is enabled on I217/I218/I219  before checking
349779849ebcSDavid Ertman 	 * the frequency
349879849ebcSDavid Ertman 	 */
3499c8744f44SSasha Neftin 	if ((hw->mac.type >= e1000_pch_lpt) &&
3500b67e1913SBruce Allan 	    !(er32(TSYNCTXCTL) & E1000_TSYNCTXCTL_ENABLED) &&
3501b67e1913SBruce Allan 	    !(er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_ENABLED)) {
3502b67e1913SBruce Allan 		u32 fextnvm7 = er32(FEXTNVM7);
3503b67e1913SBruce Allan 
350418dd2392SJacob Keller 		if (!(fextnvm7 & BIT(0))) {
350518dd2392SJacob Keller 			ew32(FEXTNVM7, fextnvm7 | BIT(0));
3506b67e1913SBruce Allan 			e1e_flush();
3507b67e1913SBruce Allan 		}
3508b67e1913SBruce Allan 	}
3509b67e1913SBruce Allan 
3510b67e1913SBruce Allan 	switch (hw->mac.type) {
3511b67e1913SBruce Allan 	case e1000_pch2lan:
35125313eeccSBernd Faust 		/* Stable 96MHz frequency */
351368fe1d5dSSasha Neftin 		incperiod = INCPERIOD_96MHZ;
351468fe1d5dSSasha Neftin 		incvalue = INCVALUE_96MHZ;
351568fe1d5dSSasha Neftin 		shift = INCVALUE_SHIFT_96MHZ;
351668fe1d5dSSasha Neftin 		adapter->cc.shift = shift + INCPERIOD_SHIFT_96MHZ;
35175313eeccSBernd Faust 		break;
3518b67e1913SBruce Allan 	case e1000_pch_lpt:
351983129b37SYanir Lubetkin 		if (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI) {
3520b67e1913SBruce Allan 			/* Stable 96MHz frequency */
352168fe1d5dSSasha Neftin 			incperiod = INCPERIOD_96MHZ;
352268fe1d5dSSasha Neftin 			incvalue = INCVALUE_96MHZ;
352368fe1d5dSSasha Neftin 			shift = INCVALUE_SHIFT_96MHZ;
352468fe1d5dSSasha Neftin 			adapter->cc.shift = shift + INCPERIOD_SHIFT_96MHZ;
352583129b37SYanir Lubetkin 		} else {
352683129b37SYanir Lubetkin 			/* Stable 25MHz frequency */
352768fe1d5dSSasha Neftin 			incperiod = INCPERIOD_25MHZ;
352868fe1d5dSSasha Neftin 			incvalue = INCVALUE_25MHZ;
352968fe1d5dSSasha Neftin 			shift = INCVALUE_SHIFT_25MHZ;
353083129b37SYanir Lubetkin 			adapter->cc.shift = shift;
353183129b37SYanir Lubetkin 		}
353283129b37SYanir Lubetkin 		break;
353383129b37SYanir Lubetkin 	case e1000_pch_spt:
353483129b37SYanir Lubetkin 		/* Stable 24MHz frequency */
353568fe1d5dSSasha Neftin 		incperiod = INCPERIOD_24MHZ;
353668fe1d5dSSasha Neftin 		incvalue = INCVALUE_24MHZ;
353768fe1d5dSSasha Neftin 		shift = INCVALUE_SHIFT_24MHZ;
353883129b37SYanir Lubetkin 		adapter->cc.shift = shift;
3539b67e1913SBruce Allan 		break;
354068fe1d5dSSasha Neftin 	case e1000_pch_cnp:
3541fb776f5dSSasha Neftin 	case e1000_pch_tgp:
354259e46688SSasha Neftin 	case e1000_pch_adp:
3543cc23f4f0SSasha Neftin 	case e1000_pch_mtp:
3544820b8ff6SSasha Neftin 	case e1000_pch_lnp:
35450c9183ceSSasha Neftin 	case e1000_pch_ptp:
35461fe4f45eSSasha Neftin 	case e1000_pch_nvp:
354768fe1d5dSSasha Neftin 		if (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI) {
354868fe1d5dSSasha Neftin 			/* Stable 24MHz frequency */
354968fe1d5dSSasha Neftin 			incperiod = INCPERIOD_24MHZ;
355068fe1d5dSSasha Neftin 			incvalue = INCVALUE_24MHZ;
355168fe1d5dSSasha Neftin 			shift = INCVALUE_SHIFT_24MHZ;
355268fe1d5dSSasha Neftin 			adapter->cc.shift = shift;
355368fe1d5dSSasha Neftin 		} else {
355468fe1d5dSSasha Neftin 			/* Stable 38400KHz frequency */
355568fe1d5dSSasha Neftin 			incperiod = INCPERIOD_38400KHZ;
355668fe1d5dSSasha Neftin 			incvalue = INCVALUE_38400KHZ;
355768fe1d5dSSasha Neftin 			shift = INCVALUE_SHIFT_38400KHZ;
355868fe1d5dSSasha Neftin 			adapter->cc.shift = shift;
355968fe1d5dSSasha Neftin 		}
356068fe1d5dSSasha Neftin 		break;
3561b67e1913SBruce Allan 	case e1000_82574:
3562b67e1913SBruce Allan 	case e1000_82583:
3563b67e1913SBruce Allan 		/* Stable 25MHz frequency */
356468fe1d5dSSasha Neftin 		incperiod = INCPERIOD_25MHZ;
356568fe1d5dSSasha Neftin 		incvalue = INCVALUE_25MHZ;
356668fe1d5dSSasha Neftin 		shift = INCVALUE_SHIFT_25MHZ;
3567b67e1913SBruce Allan 		adapter->cc.shift = shift;
3568b67e1913SBruce Allan 		break;
3569b67e1913SBruce Allan 	default:
3570b67e1913SBruce Allan 		return -EINVAL;
3571b67e1913SBruce Allan 	}
3572b67e1913SBruce Allan 
3573b67e1913SBruce Allan 	*timinca = ((incperiod << E1000_TIMINCA_INCPERIOD_SHIFT) |
3574b67e1913SBruce Allan 		    ((incvalue << shift) & E1000_TIMINCA_INCVALUE_MASK));
3575b67e1913SBruce Allan 
3576b67e1913SBruce Allan 	return 0;
3577b67e1913SBruce Allan }
3578b67e1913SBruce Allan 
3579b67e1913SBruce Allan /**
3580b67e1913SBruce Allan  * e1000e_config_hwtstamp - configure the hwtstamp registers and enable/disable
3581b67e1913SBruce Allan  * @adapter: board private structure
3582b50f7bcaSJesse Brandeburg  * @config: timestamp configuration
3583b67e1913SBruce Allan  *
3584b67e1913SBruce Allan  * Outgoing time stamping can be enabled and disabled. Play nice and
3585b67e1913SBruce Allan  * disable it when requested, although it shouldn't cause any overhead
3586b67e1913SBruce Allan  * when no packet needs it. At most one packet in the queue may be
3587b67e1913SBruce Allan  * marked for time stamping, otherwise it would be impossible to tell
3588b67e1913SBruce Allan  * for sure to which packet the hardware time stamp belongs.
3589b67e1913SBruce Allan  *
3590b67e1913SBruce Allan  * Incoming time stamping has to be configured via the hardware filters.
3591b67e1913SBruce Allan  * Not all combinations are supported, in particular event type has to be
3592b67e1913SBruce Allan  * specified. Matching the kind of event packet is not supported, with the
3593b67e1913SBruce Allan  * exception of "all V2 events regardless of level 2 or 4".
3594b67e1913SBruce Allan  **/
e1000e_config_hwtstamp(struct e1000_adapter * adapter,struct hwtstamp_config * config)359562d7e3a2SBen Hutchings static int e1000e_config_hwtstamp(struct e1000_adapter *adapter,
359662d7e3a2SBen Hutchings 				  struct hwtstamp_config *config)
3597b67e1913SBruce Allan {
3598b67e1913SBruce Allan 	struct e1000_hw *hw = &adapter->hw;
3599b67e1913SBruce Allan 	u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
3600b67e1913SBruce Allan 	u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
3601d89777bfSBruce Allan 	u32 rxmtrl = 0;
3602d89777bfSBruce Allan 	u16 rxudp = 0;
3603d89777bfSBruce Allan 	bool is_l4 = false;
3604d89777bfSBruce Allan 	bool is_l2 = false;
3605b67e1913SBruce Allan 	u32 regval;
3606b67e1913SBruce Allan 
3607b67e1913SBruce Allan 	if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
3608b67e1913SBruce Allan 		return -EINVAL;
3609b67e1913SBruce Allan 
3610b67e1913SBruce Allan 	switch (config->tx_type) {
3611b67e1913SBruce Allan 	case HWTSTAMP_TX_OFF:
3612b67e1913SBruce Allan 		tsync_tx_ctl = 0;
3613b67e1913SBruce Allan 		break;
3614b67e1913SBruce Allan 	case HWTSTAMP_TX_ON:
3615b67e1913SBruce Allan 		break;
3616b67e1913SBruce Allan 	default:
3617b67e1913SBruce Allan 		return -ERANGE;
3618b67e1913SBruce Allan 	}
3619b67e1913SBruce Allan 
3620b67e1913SBruce Allan 	switch (config->rx_filter) {
3621b67e1913SBruce Allan 	case HWTSTAMP_FILTER_NONE:
3622b67e1913SBruce Allan 		tsync_rx_ctl = 0;
3623b67e1913SBruce Allan 		break;
3624d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3625d89777bfSBruce Allan 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
3626d89777bfSBruce Allan 		rxmtrl = E1000_RXMTRL_PTP_V1_SYNC_MESSAGE;
3627d89777bfSBruce Allan 		is_l4 = true;
3628d89777bfSBruce Allan 		break;
3629d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3630d89777bfSBruce Allan 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
3631d89777bfSBruce Allan 		rxmtrl = E1000_RXMTRL_PTP_V1_DELAY_REQ_MESSAGE;
3632d89777bfSBruce Allan 		is_l4 = true;
3633d89777bfSBruce Allan 		break;
3634d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3635d89777bfSBruce Allan 		/* Also time stamps V2 L2 Path Delay Request/Response */
3636d89777bfSBruce Allan 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_V2;
3637d89777bfSBruce Allan 		rxmtrl = E1000_RXMTRL_PTP_V2_SYNC_MESSAGE;
3638d89777bfSBruce Allan 		is_l2 = true;
3639d89777bfSBruce Allan 		break;
3640d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3641d89777bfSBruce Allan 		/* Also time stamps V2 L2 Path Delay Request/Response. */
3642d89777bfSBruce Allan 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_V2;
3643d89777bfSBruce Allan 		rxmtrl = E1000_RXMTRL_PTP_V2_DELAY_REQ_MESSAGE;
3644d89777bfSBruce Allan 		is_l2 = true;
3645d89777bfSBruce Allan 		break;
3646d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
36475463fce6SJeff Kirsher 		/* Hardware cannot filter just V2 L4 Sync messages */
36485463fce6SJeff Kirsher 		fallthrough;
3649d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
3650d89777bfSBruce Allan 		/* Also time stamps V2 Path Delay Request/Response. */
3651d89777bfSBruce Allan 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
3652d89777bfSBruce Allan 		rxmtrl = E1000_RXMTRL_PTP_V2_SYNC_MESSAGE;
3653d89777bfSBruce Allan 		is_l2 = true;
3654d89777bfSBruce Allan 		is_l4 = true;
3655d89777bfSBruce Allan 		break;
3656d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
36575463fce6SJeff Kirsher 		/* Hardware cannot filter just V2 L4 Delay Request messages */
36585463fce6SJeff Kirsher 		fallthrough;
3659d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3660d89777bfSBruce Allan 		/* Also time stamps V2 Path Delay Request/Response. */
3661d89777bfSBruce Allan 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
3662d89777bfSBruce Allan 		rxmtrl = E1000_RXMTRL_PTP_V2_DELAY_REQ_MESSAGE;
3663d89777bfSBruce Allan 		is_l2 = true;
3664d89777bfSBruce Allan 		is_l4 = true;
3665d89777bfSBruce Allan 		break;
3666d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3667d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
36685463fce6SJeff Kirsher 		/* Hardware cannot filter just V2 L4 or L2 Event messages */
36695463fce6SJeff Kirsher 		fallthrough;
3670d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
3671d89777bfSBruce Allan 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
3672d89777bfSBruce Allan 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
3673d89777bfSBruce Allan 		is_l2 = true;
3674d89777bfSBruce Allan 		is_l4 = true;
3675d89777bfSBruce Allan 		break;
3676d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3677d89777bfSBruce Allan 		/* For V1, the hardware can only filter Sync messages or
3678d89777bfSBruce Allan 		 * Delay Request messages but not both so fall-through to
3679d89777bfSBruce Allan 		 * time stamp all packets.
3680d89777bfSBruce Allan 		 */
36815463fce6SJeff Kirsher 		fallthrough;
3682e3412575SMiroslav Lichvar 	case HWTSTAMP_FILTER_NTP_ALL:
3683b67e1913SBruce Allan 	case HWTSTAMP_FILTER_ALL:
3684d89777bfSBruce Allan 		is_l2 = true;
3685d89777bfSBruce Allan 		is_l4 = true;
3686b67e1913SBruce Allan 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
3687b67e1913SBruce Allan 		config->rx_filter = HWTSTAMP_FILTER_ALL;
3688b67e1913SBruce Allan 		break;
3689b67e1913SBruce Allan 	default:
3690b67e1913SBruce Allan 		return -ERANGE;
3691b67e1913SBruce Allan 	}
3692b67e1913SBruce Allan 
369362d7e3a2SBen Hutchings 	adapter->hwtstamp_config = *config;
369462d7e3a2SBen Hutchings 
3695b67e1913SBruce Allan 	/* enable/disable Tx h/w time stamping */
3696b67e1913SBruce Allan 	regval = er32(TSYNCTXCTL);
3697b67e1913SBruce Allan 	regval &= ~E1000_TSYNCTXCTL_ENABLED;
3698b67e1913SBruce Allan 	regval |= tsync_tx_ctl;
3699b67e1913SBruce Allan 	ew32(TSYNCTXCTL, regval);
3700b67e1913SBruce Allan 	if ((er32(TSYNCTXCTL) & E1000_TSYNCTXCTL_ENABLED) !=
3701b67e1913SBruce Allan 	    (regval & E1000_TSYNCTXCTL_ENABLED)) {
3702b67e1913SBruce Allan 		e_err("Timesync Tx Control register not set as expected\n");
3703b67e1913SBruce Allan 		return -EAGAIN;
3704b67e1913SBruce Allan 	}
3705b67e1913SBruce Allan 
3706b67e1913SBruce Allan 	/* enable/disable Rx h/w time stamping */
3707b67e1913SBruce Allan 	regval = er32(TSYNCRXCTL);
3708b67e1913SBruce Allan 	regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
3709b67e1913SBruce Allan 	regval |= tsync_rx_ctl;
3710b67e1913SBruce Allan 	ew32(TSYNCRXCTL, regval);
3711b67e1913SBruce Allan 	if ((er32(TSYNCRXCTL) & (E1000_TSYNCRXCTL_ENABLED |
3712b67e1913SBruce Allan 				 E1000_TSYNCRXCTL_TYPE_MASK)) !=
3713b67e1913SBruce Allan 	    (regval & (E1000_TSYNCRXCTL_ENABLED |
3714b67e1913SBruce Allan 		       E1000_TSYNCRXCTL_TYPE_MASK))) {
3715b67e1913SBruce Allan 		e_err("Timesync Rx Control register not set as expected\n");
3716b67e1913SBruce Allan 		return -EAGAIN;
3717b67e1913SBruce Allan 	}
3718b67e1913SBruce Allan 
3719d89777bfSBruce Allan 	/* L2: define ethertype filter for time stamped packets */
3720d89777bfSBruce Allan 	if (is_l2)
3721d89777bfSBruce Allan 		rxmtrl |= ETH_P_1588;
3722d89777bfSBruce Allan 
3723d89777bfSBruce Allan 	/* define which PTP packets get time stamped */
3724d89777bfSBruce Allan 	ew32(RXMTRL, rxmtrl);
3725d89777bfSBruce Allan 
3726d89777bfSBruce Allan 	/* Filter by destination port */
3727d89777bfSBruce Allan 	if (is_l4) {
3728d89777bfSBruce Allan 		rxudp = PTP_EV_PORT;
3729d89777bfSBruce Allan 		cpu_to_be16s(&rxudp);
3730d89777bfSBruce Allan 	}
3731d89777bfSBruce Allan 	ew32(RXUDP, rxudp);
3732d89777bfSBruce Allan 
3733d89777bfSBruce Allan 	e1e_flush();
3734d89777bfSBruce Allan 
3735b67e1913SBruce Allan 	/* Clear TSYNCRXCTL_VALID & TSYNCTXCTL_VALID bit */
373670806a7fSBruce Allan 	er32(RXSTMPH);
373770806a7fSBruce Allan 	er32(TXSTMPH);
3738b67e1913SBruce Allan 
3739b67e1913SBruce Allan 	return 0;
3740b67e1913SBruce Allan }
3741b67e1913SBruce Allan 
3742b67e1913SBruce Allan /**
3743dee1ad47SJeff Kirsher  * e1000_configure - configure the hardware for Rx and Tx
3744dee1ad47SJeff Kirsher  * @adapter: private board structure
3745dee1ad47SJeff Kirsher  **/
e1000_configure(struct e1000_adapter * adapter)3746dee1ad47SJeff Kirsher static void e1000_configure(struct e1000_adapter *adapter)
3747dee1ad47SJeff Kirsher {
374855aa6985SBruce Allan 	struct e1000_ring *rx_ring = adapter->rx_ring;
374955aa6985SBruce Allan 
3750ef9b965aSJesse Brandeburg 	e1000e_set_rx_mode(adapter->netdev);
3751dee1ad47SJeff Kirsher 
3752dee1ad47SJeff Kirsher 	e1000_restore_vlan(adapter);
3753dee1ad47SJeff Kirsher 	e1000_init_manageability_pt(adapter);
3754dee1ad47SJeff Kirsher 
3755dee1ad47SJeff Kirsher 	e1000_configure_tx(adapter);
375670495a50SBruce Allan 
375770495a50SBruce Allan 	if (adapter->netdev->features & NETIF_F_RXHASH)
375870495a50SBruce Allan 		e1000e_setup_rss_hash(adapter);
3759dee1ad47SJeff Kirsher 	e1000_setup_rctl(adapter);
3760dee1ad47SJeff Kirsher 	e1000_configure_rx(adapter);
376155aa6985SBruce Allan 	adapter->alloc_rx_buf(rx_ring, e1000_desc_unused(rx_ring), GFP_KERNEL);
3762dee1ad47SJeff Kirsher }
3763dee1ad47SJeff Kirsher 
3764dee1ad47SJeff Kirsher /**
3765dee1ad47SJeff Kirsher  * e1000e_power_up_phy - restore link in case the phy was powered down
3766dee1ad47SJeff Kirsher  * @adapter: address of board private structure
3767dee1ad47SJeff Kirsher  *
3768dee1ad47SJeff Kirsher  * The phy may be powered down to save power and turn off link when the
3769dee1ad47SJeff Kirsher  * driver is unloaded and wake on lan is not enabled (among others)
3770dee1ad47SJeff Kirsher  * *** this routine MUST be followed by a call to e1000e_reset ***
3771dee1ad47SJeff Kirsher  **/
e1000e_power_up_phy(struct e1000_adapter * adapter)3772dee1ad47SJeff Kirsher void e1000e_power_up_phy(struct e1000_adapter *adapter)
3773dee1ad47SJeff Kirsher {
3774dee1ad47SJeff Kirsher 	if (adapter->hw.phy.ops.power_up)
3775dee1ad47SJeff Kirsher 		adapter->hw.phy.ops.power_up(&adapter->hw);
3776dee1ad47SJeff Kirsher 
3777dee1ad47SJeff Kirsher 	adapter->hw.mac.ops.setup_link(&adapter->hw);
3778dee1ad47SJeff Kirsher }
3779dee1ad47SJeff Kirsher 
3780dee1ad47SJeff Kirsher /**
3781dee1ad47SJeff Kirsher  * e1000_power_down_phy - Power down the PHY
3782b50f7bcaSJesse Brandeburg  * @adapter: board private structure
3783dee1ad47SJeff Kirsher  *
3784dee1ad47SJeff Kirsher  * Power down the PHY so no link is implied when interface is down.
3785dee1ad47SJeff Kirsher  * The PHY cannot be powered down if management or WoL is active.
3786dee1ad47SJeff Kirsher  */
e1000_power_down_phy(struct e1000_adapter * adapter)3787dee1ad47SJeff Kirsher static void e1000_power_down_phy(struct e1000_adapter *adapter)
3788dee1ad47SJeff Kirsher {
3789dee1ad47SJeff Kirsher 	if (adapter->hw.phy.ops.power_down)
3790dee1ad47SJeff Kirsher 		adapter->hw.phy.ops.power_down(&adapter->hw);
3791dee1ad47SJeff Kirsher }
3792dee1ad47SJeff Kirsher 
3793dee1ad47SJeff Kirsher /**
3794ad851fbbSYanir Lubetkin  * e1000_flush_tx_ring - remove all descriptors from the tx_ring
3795b50f7bcaSJesse Brandeburg  * @adapter: board private structure
3796ad851fbbSYanir Lubetkin  *
3797ad851fbbSYanir Lubetkin  * We want to clear all pending descriptors from the TX ring.
3798ad851fbbSYanir Lubetkin  * zeroing happens when the HW reads the regs. We  assign the ring itself as
3799ad851fbbSYanir Lubetkin  * the data of the next descriptor. We don't care about the data we are about
3800ad851fbbSYanir Lubetkin  * to reset the HW.
3801ad851fbbSYanir Lubetkin  */
e1000_flush_tx_ring(struct e1000_adapter * adapter)3802ad851fbbSYanir Lubetkin static void e1000_flush_tx_ring(struct e1000_adapter *adapter)
3803ad851fbbSYanir Lubetkin {
3804ad851fbbSYanir Lubetkin 	struct e1000_hw *hw = &adapter->hw;
3805ad851fbbSYanir Lubetkin 	struct e1000_ring *tx_ring = adapter->tx_ring;
3806ad851fbbSYanir Lubetkin 	struct e1000_tx_desc *tx_desc = NULL;
3807ad851fbbSYanir Lubetkin 	u32 tdt, tctl, txd_lower = E1000_TXD_CMD_IFCS;
3808ad851fbbSYanir Lubetkin 	u16 size = 512;
3809ad851fbbSYanir Lubetkin 
3810ad851fbbSYanir Lubetkin 	tctl = er32(TCTL);
3811ad851fbbSYanir Lubetkin 	ew32(TCTL, tctl | E1000_TCTL_EN);
3812ad851fbbSYanir Lubetkin 	tdt = er32(TDT(0));
3813ad851fbbSYanir Lubetkin 	BUG_ON(tdt != tx_ring->next_to_use);
3814ad851fbbSYanir Lubetkin 	tx_desc =  E1000_TX_DESC(*tx_ring, tx_ring->next_to_use);
381599fe61b2SBen Dooks (Codethink) 	tx_desc->buffer_addr = cpu_to_le64(tx_ring->dma);
3816ad851fbbSYanir Lubetkin 
3817ad851fbbSYanir Lubetkin 	tx_desc->lower.data = cpu_to_le32(txd_lower | size);
3818ad851fbbSYanir Lubetkin 	tx_desc->upper.data = 0;
3819ad851fbbSYanir Lubetkin 	/* flush descriptors to memory before notifying the HW */
3820ad851fbbSYanir Lubetkin 	wmb();
3821ad851fbbSYanir Lubetkin 	tx_ring->next_to_use++;
3822ad851fbbSYanir Lubetkin 	if (tx_ring->next_to_use == tx_ring->count)
3823ad851fbbSYanir Lubetkin 		tx_ring->next_to_use = 0;
3824ad851fbbSYanir Lubetkin 	ew32(TDT(0), tx_ring->next_to_use);
3825ad851fbbSYanir Lubetkin 	usleep_range(200, 250);
3826ad851fbbSYanir Lubetkin }
3827ad851fbbSYanir Lubetkin 
3828ad851fbbSYanir Lubetkin /**
3829ad851fbbSYanir Lubetkin  * e1000_flush_rx_ring - remove all descriptors from the rx_ring
3830b50f7bcaSJesse Brandeburg  * @adapter: board private structure
3831ad851fbbSYanir Lubetkin  *
3832ad851fbbSYanir Lubetkin  * Mark all descriptors in the RX ring as consumed and disable the rx ring
3833ad851fbbSYanir Lubetkin  */
e1000_flush_rx_ring(struct e1000_adapter * adapter)3834ad851fbbSYanir Lubetkin static void e1000_flush_rx_ring(struct e1000_adapter *adapter)
3835ad851fbbSYanir Lubetkin {
3836ad851fbbSYanir Lubetkin 	u32 rctl, rxdctl;
3837ad851fbbSYanir Lubetkin 	struct e1000_hw *hw = &adapter->hw;
3838ad851fbbSYanir Lubetkin 
3839ad851fbbSYanir Lubetkin 	rctl = er32(RCTL);
3840ad851fbbSYanir Lubetkin 	ew32(RCTL, rctl & ~E1000_RCTL_EN);
3841ad851fbbSYanir Lubetkin 	e1e_flush();
3842ad851fbbSYanir Lubetkin 	usleep_range(100, 150);
3843ad851fbbSYanir Lubetkin 
3844ad851fbbSYanir Lubetkin 	rxdctl = er32(RXDCTL(0));
3845ad851fbbSYanir Lubetkin 	/* zero the lower 14 bits (prefetch and host thresholds) */
3846ad851fbbSYanir Lubetkin 	rxdctl &= 0xffffc000;
3847ad851fbbSYanir Lubetkin 
3848ad851fbbSYanir Lubetkin 	/* update thresholds: prefetch threshold to 31, host threshold to 1
3849ad851fbbSYanir Lubetkin 	 * and make sure the granularity is "descriptors" and not "cache lines"
3850ad851fbbSYanir Lubetkin 	 */
385118dd2392SJacob Keller 	rxdctl |= (0x1F | BIT(8) | E1000_RXDCTL_THRESH_UNIT_DESC);
3852ad851fbbSYanir Lubetkin 
3853ad851fbbSYanir Lubetkin 	ew32(RXDCTL(0), rxdctl);
3854ad851fbbSYanir Lubetkin 	/* momentarily enable the RX ring for the changes to take effect */
3855ad851fbbSYanir Lubetkin 	ew32(RCTL, rctl | E1000_RCTL_EN);
3856ad851fbbSYanir Lubetkin 	e1e_flush();
3857ad851fbbSYanir Lubetkin 	usleep_range(100, 150);
3858ad851fbbSYanir Lubetkin 	ew32(RCTL, rctl & ~E1000_RCTL_EN);
3859ad851fbbSYanir Lubetkin }
3860ad851fbbSYanir Lubetkin 
3861ad851fbbSYanir Lubetkin /**
3862ad851fbbSYanir Lubetkin  * e1000_flush_desc_rings - remove all descriptors from the descriptor rings
3863b50f7bcaSJesse Brandeburg  * @adapter: board private structure
3864ad851fbbSYanir Lubetkin  *
3865ad851fbbSYanir Lubetkin  * In i219, the descriptor rings must be emptied before resetting the HW
3866ad851fbbSYanir Lubetkin  * or before changing the device state to D3 during runtime (runtime PM).
3867ad851fbbSYanir Lubetkin  *
3868ad851fbbSYanir Lubetkin  * Failure to do this will cause the HW to enter a unit hang state which can
3869ad851fbbSYanir Lubetkin  * only be released by PCI reset on the device
3870ad851fbbSYanir Lubetkin  *
3871ad851fbbSYanir Lubetkin  */
3872ad851fbbSYanir Lubetkin 
e1000_flush_desc_rings(struct e1000_adapter * adapter)3873ad851fbbSYanir Lubetkin static void e1000_flush_desc_rings(struct e1000_adapter *adapter)
3874ad851fbbSYanir Lubetkin {
3875ff917429SYanir Lubetkin 	u16 hang_state;
3876ad851fbbSYanir Lubetkin 	u32 fext_nvm11, tdlen;
3877ad851fbbSYanir Lubetkin 	struct e1000_hw *hw = &adapter->hw;
3878ad851fbbSYanir Lubetkin 
3879ad851fbbSYanir Lubetkin 	/* First, disable MULR fix in FEXTNVM11 */
3880ad851fbbSYanir Lubetkin 	fext_nvm11 = er32(FEXTNVM11);
3881ad851fbbSYanir Lubetkin 	fext_nvm11 |= E1000_FEXTNVM11_DISABLE_MULR_FIX;
3882ad851fbbSYanir Lubetkin 	ew32(FEXTNVM11, fext_nvm11);
3883ad851fbbSYanir Lubetkin 	/* do nothing if we're not in faulty state, or if the queue is empty */
3884ad851fbbSYanir Lubetkin 	tdlen = er32(TDLEN(0));
3885ff917429SYanir Lubetkin 	pci_read_config_word(adapter->pdev, PCICFG_DESC_RING_STATUS,
3886ff917429SYanir Lubetkin 			     &hang_state);
3887ff917429SYanir Lubetkin 	if (!(hang_state & FLUSH_DESC_REQUIRED) || !tdlen)
3888ad851fbbSYanir Lubetkin 		return;
3889ad851fbbSYanir Lubetkin 	e1000_flush_tx_ring(adapter);
3890ad851fbbSYanir Lubetkin 	/* recheck, maybe the fault is caused by the rx ring */
3891ff917429SYanir Lubetkin 	pci_read_config_word(adapter->pdev, PCICFG_DESC_RING_STATUS,
3892ff917429SYanir Lubetkin 			     &hang_state);
3893ff917429SYanir Lubetkin 	if (hang_state & FLUSH_DESC_REQUIRED)
3894ad851fbbSYanir Lubetkin 		e1000_flush_rx_ring(adapter);
3895ad851fbbSYanir Lubetkin }
3896ad851fbbSYanir Lubetkin 
3897ad851fbbSYanir Lubetkin /**
3898aa524b66SJacob Keller  * e1000e_systim_reset - reset the timesync registers after a hardware reset
3899aa524b66SJacob Keller  * @adapter: board private structure
3900aa524b66SJacob Keller  *
3901aa524b66SJacob Keller  * When the MAC is reset, all hardware bits for timesync will be reset to the
3902aa524b66SJacob Keller  * default values. This function will restore the settings last in place.
3903aa524b66SJacob Keller  * Since the clock SYSTIME registers are reset, we will simply restore the
3904aa524b66SJacob Keller  * cyclecounter to the kernel real clock time.
3905aa524b66SJacob Keller  **/
e1000e_systim_reset(struct e1000_adapter * adapter)3906aa524b66SJacob Keller static void e1000e_systim_reset(struct e1000_adapter *adapter)
3907aa524b66SJacob Keller {
3908aa524b66SJacob Keller 	struct ptp_clock_info *info = &adapter->ptp_clock_info;
3909aa524b66SJacob Keller 	struct e1000_hw *hw = &adapter->hw;
3910aa524b66SJacob Keller 	unsigned long flags;
3911aa524b66SJacob Keller 	u32 timinca;
3912aa524b66SJacob Keller 	s32 ret_val;
3913aa524b66SJacob Keller 
3914aa524b66SJacob Keller 	if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
3915aa524b66SJacob Keller 		return;
3916aa524b66SJacob Keller 
3917abab010fSJacob Keller 	if (info->adjfine) {
3918aa524b66SJacob Keller 		/* restore the previous ptp frequency delta */
3919abab010fSJacob Keller 		ret_val = info->adjfine(info, adapter->ptp_delta);
3920aa524b66SJacob Keller 	} else {
3921aa524b66SJacob Keller 		/* set the default base frequency if no adjustment possible */
3922aa524b66SJacob Keller 		ret_val = e1000e_get_base_timinca(adapter, &timinca);
3923aa524b66SJacob Keller 		if (!ret_val)
3924aa524b66SJacob Keller 			ew32(TIMINCA, timinca);
3925aa524b66SJacob Keller 	}
3926aa524b66SJacob Keller 
3927aa524b66SJacob Keller 	if (ret_val) {
3928aa524b66SJacob Keller 		dev_warn(&adapter->pdev->dev,
3929aa524b66SJacob Keller 			 "Failed to restore TIMINCA clock rate delta: %d\n",
3930aa524b66SJacob Keller 			 ret_val);
3931aa524b66SJacob Keller 		return;
3932aa524b66SJacob Keller 	}
3933aa524b66SJacob Keller 
3934aa524b66SJacob Keller 	/* reset the systim ns time counter */
3935aa524b66SJacob Keller 	spin_lock_irqsave(&adapter->systim_lock, flags);
3936aa524b66SJacob Keller 	timecounter_init(&adapter->tc, &adapter->cc,
3937aa524b66SJacob Keller 			 ktime_to_ns(ktime_get_real()));
3938aa524b66SJacob Keller 	spin_unlock_irqrestore(&adapter->systim_lock, flags);
3939aa524b66SJacob Keller 
3940aa524b66SJacob Keller 	/* restore the previous hwtstamp configuration settings */
3941aa524b66SJacob Keller 	e1000e_config_hwtstamp(adapter, &adapter->hwtstamp_config);
3942aa524b66SJacob Keller }
3943aa524b66SJacob Keller 
3944aa524b66SJacob Keller /**
3945dee1ad47SJeff Kirsher  * e1000e_reset - bring the hardware into a known good state
3946b50f7bcaSJesse Brandeburg  * @adapter: board private structure
3947dee1ad47SJeff Kirsher  *
3948dee1ad47SJeff Kirsher  * This function boots the hardware and enables some settings that
3949dee1ad47SJeff Kirsher  * require a configuration cycle of the hardware - those cannot be
3950dee1ad47SJeff Kirsher  * set/changed during runtime. After reset the device needs to be
3951dee1ad47SJeff Kirsher  * properly configured for Rx, Tx etc.
3952dee1ad47SJeff Kirsher  */
e1000e_reset(struct e1000_adapter * adapter)3953dee1ad47SJeff Kirsher void e1000e_reset(struct e1000_adapter *adapter)
3954dee1ad47SJeff Kirsher {
3955dee1ad47SJeff Kirsher 	struct e1000_mac_info *mac = &adapter->hw.mac;
3956dee1ad47SJeff Kirsher 	struct e1000_fc_info *fc = &adapter->hw.fc;
3957dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
3958dee1ad47SJeff Kirsher 	u32 tx_space, min_tx_space, min_rx_space;
3959dee1ad47SJeff Kirsher 	u32 pba = adapter->pba;
3960dee1ad47SJeff Kirsher 	u16 hwm;
3961dee1ad47SJeff Kirsher 
3962dee1ad47SJeff Kirsher 	/* reset Packet Buffer Allocation to default */
3963dee1ad47SJeff Kirsher 	ew32(PBA, pba);
3964dee1ad47SJeff Kirsher 
39658084b86dSAlexander Duyck 	if (adapter->max_frame_size > (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)) {
3966e921eb1aSBruce Allan 		/* To maintain wire speed transmits, the Tx FIFO should be
3967dee1ad47SJeff Kirsher 		 * large enough to accommodate two full transmit packets,
3968dee1ad47SJeff Kirsher 		 * rounded up to the next 1KB and expressed in KB.  Likewise,
3969dee1ad47SJeff Kirsher 		 * the Rx FIFO should be large enough to accommodate at least
3970dee1ad47SJeff Kirsher 		 * one full receive packet and is similarly rounded up and
3971dee1ad47SJeff Kirsher 		 * expressed in KB.
3972dee1ad47SJeff Kirsher 		 */
3973dee1ad47SJeff Kirsher 		pba = er32(PBA);
3974dee1ad47SJeff Kirsher 		/* upper 16 bits has Tx packet buffer allocation size in KB */
3975dee1ad47SJeff Kirsher 		tx_space = pba >> 16;
3976dee1ad47SJeff Kirsher 		/* lower 16 bits has Rx packet buffer allocation size in KB */
3977dee1ad47SJeff Kirsher 		pba &= 0xffff;
3978e921eb1aSBruce Allan 		/* the Tx fifo also stores 16 bytes of information about the Tx
3979dee1ad47SJeff Kirsher 		 * but don't include ethernet FCS because hardware appends it
3980dee1ad47SJeff Kirsher 		 */
3981dee1ad47SJeff Kirsher 		min_tx_space = (adapter->max_frame_size +
3982e5fe2541SBruce Allan 				sizeof(struct e1000_tx_desc) - ETH_FCS_LEN) * 2;
3983dee1ad47SJeff Kirsher 		min_tx_space = ALIGN(min_tx_space, 1024);
3984dee1ad47SJeff Kirsher 		min_tx_space >>= 10;
3985dee1ad47SJeff Kirsher 		/* software strips receive CRC, so leave room for it */
3986dee1ad47SJeff Kirsher 		min_rx_space = adapter->max_frame_size;
3987dee1ad47SJeff Kirsher 		min_rx_space = ALIGN(min_rx_space, 1024);
3988dee1ad47SJeff Kirsher 		min_rx_space >>= 10;
3989dee1ad47SJeff Kirsher 
3990e921eb1aSBruce Allan 		/* If current Tx allocation is less than the min Tx FIFO size,
3991dee1ad47SJeff Kirsher 		 * and the min Tx FIFO size is less than the current Rx FIFO
3992dee1ad47SJeff Kirsher 		 * allocation, take space away from current Rx allocation
3993dee1ad47SJeff Kirsher 		 */
3994dee1ad47SJeff Kirsher 		if ((tx_space < min_tx_space) &&
3995dee1ad47SJeff Kirsher 		    ((min_tx_space - tx_space) < pba)) {
3996dee1ad47SJeff Kirsher 			pba -= min_tx_space - tx_space;
3997dee1ad47SJeff Kirsher 
3998e921eb1aSBruce Allan 			/* if short on Rx space, Rx wins and must trump Tx
3999419e551cSBruce Allan 			 * adjustment
4000dee1ad47SJeff Kirsher 			 */
400179d4e908SBruce Allan 			if (pba < min_rx_space)
4002dee1ad47SJeff Kirsher 				pba = min_rx_space;
4003dee1ad47SJeff Kirsher 		}
4004dee1ad47SJeff Kirsher 
4005dee1ad47SJeff Kirsher 		ew32(PBA, pba);
4006dee1ad47SJeff Kirsher 	}
4007dee1ad47SJeff Kirsher 
4008e921eb1aSBruce Allan 	/* flow control settings
4009dee1ad47SJeff Kirsher 	 *
4010dee1ad47SJeff Kirsher 	 * The high water mark must be low enough to fit one full frame
4011dee1ad47SJeff Kirsher 	 * (or the size used for early receive) above it in the Rx FIFO.
4012dee1ad47SJeff Kirsher 	 * Set it to the lower of:
4013dee1ad47SJeff Kirsher 	 * - 90% of the Rx FIFO size, and
4014dee1ad47SJeff Kirsher 	 * - the full Rx FIFO size minus one full frame
4015dee1ad47SJeff Kirsher 	 */
4016dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_DISABLE_FC_PAUSE_TIME)
4017dee1ad47SJeff Kirsher 		fc->pause_time = 0xFFFF;
4018dee1ad47SJeff Kirsher 	else
4019dee1ad47SJeff Kirsher 		fc->pause_time = E1000_FC_PAUSE_TIME;
4020b20caa80SBruce Allan 	fc->send_xon = true;
4021dee1ad47SJeff Kirsher 	fc->current_mode = fc->requested_mode;
4022dee1ad47SJeff Kirsher 
4023dee1ad47SJeff Kirsher 	switch (hw->mac.type) {
402479d4e908SBruce Allan 	case e1000_ich9lan:
402579d4e908SBruce Allan 	case e1000_ich10lan:
402679d4e908SBruce Allan 		if (adapter->netdev->mtu > ETH_DATA_LEN) {
402779d4e908SBruce Allan 			pba = 14;
402879d4e908SBruce Allan 			ew32(PBA, pba);
402979d4e908SBruce Allan 			fc->high_water = 0x2800;
403079d4e908SBruce Allan 			fc->low_water = fc->high_water - 8;
403179d4e908SBruce Allan 			break;
403279d4e908SBruce Allan 		}
40335463fce6SJeff Kirsher 		fallthrough;
4034dee1ad47SJeff Kirsher 	default:
4035dee1ad47SJeff Kirsher 		hwm = min(((pba << 10) * 9 / 10),
4036dee1ad47SJeff Kirsher 			  ((pba << 10) - adapter->max_frame_size));
4037dee1ad47SJeff Kirsher 
4038dee1ad47SJeff Kirsher 		fc->high_water = hwm & E1000_FCRTH_RTH;	/* 8-byte granularity */
4039dee1ad47SJeff Kirsher 		fc->low_water = fc->high_water - 8;
4040dee1ad47SJeff Kirsher 		break;
4041dee1ad47SJeff Kirsher 	case e1000_pchlan:
4042e921eb1aSBruce Allan 		/* Workaround PCH LOM adapter hangs with certain network
4043dee1ad47SJeff Kirsher 		 * loads.  If hangs persist, try disabling Tx flow control.
4044dee1ad47SJeff Kirsher 		 */
4045dee1ad47SJeff Kirsher 		if (adapter->netdev->mtu > ETH_DATA_LEN) {
4046dee1ad47SJeff Kirsher 			fc->high_water = 0x3500;
4047dee1ad47SJeff Kirsher 			fc->low_water = 0x1500;
4048dee1ad47SJeff Kirsher 		} else {
4049dee1ad47SJeff Kirsher 			fc->high_water = 0x5000;
4050dee1ad47SJeff Kirsher 			fc->low_water = 0x3000;
4051dee1ad47SJeff Kirsher 		}
4052dee1ad47SJeff Kirsher 		fc->refresh_time = 0x1000;
4053dee1ad47SJeff Kirsher 		break;
4054dee1ad47SJeff Kirsher 	case e1000_pch2lan:
40552fbe4526SBruce Allan 	case e1000_pch_lpt:
405679849ebcSDavid Ertman 	case e1000_pch_spt:
4057c8744f44SSasha Neftin 	case e1000_pch_cnp:
4058fb776f5dSSasha Neftin 	case e1000_pch_tgp:
405959e46688SSasha Neftin 	case e1000_pch_adp:
4060cc23f4f0SSasha Neftin 	case e1000_pch_mtp:
4061820b8ff6SSasha Neftin 	case e1000_pch_lnp:
40620c9183ceSSasha Neftin 	case e1000_pch_ptp:
40631fe4f45eSSasha Neftin 	case e1000_pch_nvp:
4064f74dc880SMiguel Bernal Marin 		fc->refresh_time = 0xFFFF;
4065f74dc880SMiguel Bernal Marin 		fc->pause_time = 0xFFFF;
4066347b5201SBruce Allan 
4067347b5201SBruce Allan 		if (adapter->netdev->mtu <= ETH_DATA_LEN) {
4068dee1ad47SJeff Kirsher 			fc->high_water = 0x05C20;
4069dee1ad47SJeff Kirsher 			fc->low_water = 0x05048;
4070347b5201SBruce Allan 			break;
4071dee1ad47SJeff Kirsher 		}
4072347b5201SBruce Allan 
4073ce345e08SBruce Allan 		pba = 14;
4074ce345e08SBruce Allan 		ew32(PBA, pba);
4075347b5201SBruce Allan 		fc->high_water = ((pba << 10) * 9 / 10) & E1000_FCRTH_RTH;
4076347b5201SBruce Allan 		fc->low_water = ((pba << 10) * 8 / 10) & E1000_FCRTL_RTL;
4077dee1ad47SJeff Kirsher 		break;
4078dee1ad47SJeff Kirsher 	}
4079dee1ad47SJeff Kirsher 
4080e921eb1aSBruce Allan 	/* Alignment of Tx data is on an arbitrary byte boundary with the
4081d821a4c4SBruce Allan 	 * maximum size per Tx descriptor limited only to the transmit
4082d821a4c4SBruce Allan 	 * allocation of the packet buffer minus 96 bytes with an upper
4083d821a4c4SBruce Allan 	 * limit of 24KB due to receive synchronization limitations.
4084d821a4c4SBruce Allan 	 */
4085d821a4c4SBruce Allan 	adapter->tx_fifo_limit = min_t(u32, ((er32(PBA) >> 16) << 10) - 96,
4086d821a4c4SBruce Allan 				       24 << 10);
4087d821a4c4SBruce Allan 
4088e921eb1aSBruce Allan 	/* Disable Adaptive Interrupt Moderation if 2 full packets cannot
408979d4e908SBruce Allan 	 * fit in receive buffer.
4090dee1ad47SJeff Kirsher 	 */
4091dee1ad47SJeff Kirsher 	if (adapter->itr_setting & 0x3) {
409279d4e908SBruce Allan 		if ((adapter->max_frame_size * 2) > (pba << 10)) {
4093dee1ad47SJeff Kirsher 			if (!(adapter->flags2 & FLAG2_DISABLE_AIM)) {
4094dee1ad47SJeff Kirsher 				dev_info(&adapter->pdev->dev,
409517e813ecSBruce Allan 					 "Interrupt Throttle Rate off\n");
4096dee1ad47SJeff Kirsher 				adapter->flags2 |= FLAG2_DISABLE_AIM;
409722a4cca2SMatthew Vick 				e1000e_write_itr(adapter, 0);
4098dee1ad47SJeff Kirsher 			}
4099dee1ad47SJeff Kirsher 		} else if (adapter->flags2 & FLAG2_DISABLE_AIM) {
4100dee1ad47SJeff Kirsher 			dev_info(&adapter->pdev->dev,
410117e813ecSBruce Allan 				 "Interrupt Throttle Rate on\n");
4102dee1ad47SJeff Kirsher 			adapter->flags2 &= ~FLAG2_DISABLE_AIM;
4103dee1ad47SJeff Kirsher 			adapter->itr = 20000;
410422a4cca2SMatthew Vick 			e1000e_write_itr(adapter, adapter->itr);
4105dee1ad47SJeff Kirsher 		}
4106dee1ad47SJeff Kirsher 	}
4107dee1ad47SJeff Kirsher 
4108c8744f44SSasha Neftin 	if (hw->mac.type >= e1000_pch_spt)
41090ffc5646SYanir Lubetkin 		e1000_flush_desc_rings(adapter);
4110dee1ad47SJeff Kirsher 	/* Allow time for pending master requests to run */
4111dee1ad47SJeff Kirsher 	mac->ops.reset_hw(hw);
4112dee1ad47SJeff Kirsher 
4113e921eb1aSBruce Allan 	/* For parts with AMT enabled, let the firmware know
4114dee1ad47SJeff Kirsher 	 * that the network interface is in control
4115dee1ad47SJeff Kirsher 	 */
4116dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_HAS_AMT)
4117dee1ad47SJeff Kirsher 		e1000e_get_hw_control(adapter);
4118dee1ad47SJeff Kirsher 
4119dee1ad47SJeff Kirsher 	ew32(WUC, 0);
4120dee1ad47SJeff Kirsher 
4121dee1ad47SJeff Kirsher 	if (mac->ops.init_hw(hw))
4122dee1ad47SJeff Kirsher 		e_err("Hardware Error\n");
4123dee1ad47SJeff Kirsher 
4124dee1ad47SJeff Kirsher 	e1000_update_mng_vlan(adapter);
4125dee1ad47SJeff Kirsher 
4126dee1ad47SJeff Kirsher 	/* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
4127dee1ad47SJeff Kirsher 	ew32(VET, ETH_P_8021Q);
4128dee1ad47SJeff Kirsher 
4129dee1ad47SJeff Kirsher 	e1000e_reset_adaptive(hw);
4130dee1ad47SJeff Kirsher 
4131aa524b66SJacob Keller 	/* restore systim and hwtstamp settings */
4132aa524b66SJacob Keller 	e1000e_systim_reset(adapter);
4133b67e1913SBruce Allan 
4134d495bcb8SBruce Allan 	/* Set EEE advertisement as appropriate */
4135d495bcb8SBruce Allan 	if (adapter->flags2 & FLAG2_HAS_EEE) {
4136d495bcb8SBruce Allan 		s32 ret_val;
4137d495bcb8SBruce Allan 		u16 adv_addr;
4138d495bcb8SBruce Allan 
4139d495bcb8SBruce Allan 		switch (hw->phy.type) {
4140d495bcb8SBruce Allan 		case e1000_phy_82579:
4141d495bcb8SBruce Allan 			adv_addr = I82579_EEE_ADVERTISEMENT;
4142d495bcb8SBruce Allan 			break;
4143d495bcb8SBruce Allan 		case e1000_phy_i217:
4144d495bcb8SBruce Allan 			adv_addr = I217_EEE_ADVERTISEMENT;
4145d495bcb8SBruce Allan 			break;
4146d495bcb8SBruce Allan 		default:
4147d495bcb8SBruce Allan 			dev_err(&adapter->pdev->dev,
4148d495bcb8SBruce Allan 				"Invalid PHY type setting EEE advertisement\n");
4149d495bcb8SBruce Allan 			return;
4150d495bcb8SBruce Allan 		}
4151d495bcb8SBruce Allan 
4152d495bcb8SBruce Allan 		ret_val = hw->phy.ops.acquire(hw);
4153d495bcb8SBruce Allan 		if (ret_val) {
4154d495bcb8SBruce Allan 			dev_err(&adapter->pdev->dev,
4155d495bcb8SBruce Allan 				"EEE advertisement - unable to acquire PHY\n");
4156d495bcb8SBruce Allan 			return;
4157d495bcb8SBruce Allan 		}
4158d495bcb8SBruce Allan 
4159d495bcb8SBruce Allan 		e1000_write_emi_reg_locked(hw, adv_addr,
4160d495bcb8SBruce Allan 					   hw->dev_spec.ich8lan.eee_disable ?
4161d495bcb8SBruce Allan 					   0 : adapter->eee_advert);
4162d495bcb8SBruce Allan 
4163d495bcb8SBruce Allan 		hw->phy.ops.release(hw);
4164d495bcb8SBruce Allan 	}
4165d495bcb8SBruce Allan 
4166dee1ad47SJeff Kirsher 	if (!netif_running(adapter->netdev) &&
416728002099SDavid Ertman 	    !test_bit(__E1000_TESTING, &adapter->state))
4168dee1ad47SJeff Kirsher 		e1000_power_down_phy(adapter);
4169dee1ad47SJeff Kirsher 
4170dee1ad47SJeff Kirsher 	e1000_get_phy_info(hw);
4171dee1ad47SJeff Kirsher 
4172dee1ad47SJeff Kirsher 	if ((adapter->flags & FLAG_HAS_SMART_POWER_DOWN) &&
4173dee1ad47SJeff Kirsher 	    !(adapter->flags & FLAG_SMART_POWER_DOWN)) {
4174dee1ad47SJeff Kirsher 		u16 phy_data = 0;
4175e921eb1aSBruce Allan 		/* speed up time to link by disabling smart power down, ignore
4176dee1ad47SJeff Kirsher 		 * the return value of this function because there is nothing
4177dee1ad47SJeff Kirsher 		 * different we would do if it failed
4178dee1ad47SJeff Kirsher 		 */
4179dee1ad47SJeff Kirsher 		e1e_rphy(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
4180dee1ad47SJeff Kirsher 		phy_data &= ~IGP02E1000_PM_SPD;
4181dee1ad47SJeff Kirsher 		e1e_wphy(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
4182dee1ad47SJeff Kirsher 	}
4183c8744f44SSasha Neftin 	if (hw->mac.type >= e1000_pch_spt && adapter->int_mode == 0) {
4184ec945cfbSYanir Lubetkin 		u32 reg;
4185ec945cfbSYanir Lubetkin 
4186ec945cfbSYanir Lubetkin 		/* Fextnvm7 @ 0xe4[2] = 1 */
4187ec945cfbSYanir Lubetkin 		reg = er32(FEXTNVM7);
4188ec945cfbSYanir Lubetkin 		reg |= E1000_FEXTNVM7_SIDE_CLK_UNGATE;
4189ec945cfbSYanir Lubetkin 		ew32(FEXTNVM7, reg);
4190ec945cfbSYanir Lubetkin 		/* Fextnvm9 @ 0x5bb4[13:12] = 11 */
4191ec945cfbSYanir Lubetkin 		reg = er32(FEXTNVM9);
4192ec945cfbSYanir Lubetkin 		reg |= E1000_FEXTNVM9_IOSFSB_CLKGATE_DIS |
4193ec945cfbSYanir Lubetkin 		       E1000_FEXTNVM9_IOSFSB_CLKREQ_DIS;
4194ec945cfbSYanir Lubetkin 		ew32(FEXTNVM9, reg);
4195ec945cfbSYanir Lubetkin 	}
4196ec945cfbSYanir Lubetkin 
4197dee1ad47SJeff Kirsher }
4198dee1ad47SJeff Kirsher 
4199a61cfe4fSBenjamin Poirier /**
4200a61cfe4fSBenjamin Poirier  * e1000e_trigger_lsc - trigger an LSC interrupt
4201c4dc8dc3SBaozhu Ni  * @adapter: board private structure
4202a61cfe4fSBenjamin Poirier  *
4203a61cfe4fSBenjamin Poirier  * Fire a link status change interrupt to start the watchdog.
4204a61cfe4fSBenjamin Poirier  **/
e1000e_trigger_lsc(struct e1000_adapter * adapter)4205a61cfe4fSBenjamin Poirier static void e1000e_trigger_lsc(struct e1000_adapter *adapter)
4206dee1ad47SJeff Kirsher {
4207dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
4208dee1ad47SJeff Kirsher 
4209a61cfe4fSBenjamin Poirier 	if (adapter->msix_entries)
42104aea7a5cSBenjamin Poirier 		ew32(ICS, E1000_ICS_LSC | E1000_ICS_OTHER);
4211a61cfe4fSBenjamin Poirier 	else
4212a61cfe4fSBenjamin Poirier 		ew32(ICS, E1000_ICS_LSC);
4213a61cfe4fSBenjamin Poirier }
4214a61cfe4fSBenjamin Poirier 
e1000e_up(struct e1000_adapter * adapter)4215a61cfe4fSBenjamin Poirier void e1000e_up(struct e1000_adapter *adapter)
4216a61cfe4fSBenjamin Poirier {
4217dee1ad47SJeff Kirsher 	/* hardware has been reset, we need to reload some things */
4218dee1ad47SJeff Kirsher 	e1000_configure(adapter);
4219dee1ad47SJeff Kirsher 
4220dee1ad47SJeff Kirsher 	clear_bit(__E1000_DOWN, &adapter->state);
4221dee1ad47SJeff Kirsher 
4222dee1ad47SJeff Kirsher 	if (adapter->msix_entries)
4223dee1ad47SJeff Kirsher 		e1000_configure_msix(adapter);
4224dee1ad47SJeff Kirsher 	e1000_irq_enable(adapter);
4225dee1ad47SJeff Kirsher 
4226d17ba0f6SKonstantin Khlebnikov 	/* Tx queue started by watchdog timer when link is up */
4227dee1ad47SJeff Kirsher 
4228a61cfe4fSBenjamin Poirier 	e1000e_trigger_lsc(adapter);
4229dee1ad47SJeff Kirsher }
4230dee1ad47SJeff Kirsher 
e1000e_flush_descriptors(struct e1000_adapter * adapter)4231dee1ad47SJeff Kirsher static void e1000e_flush_descriptors(struct e1000_adapter *adapter)
4232dee1ad47SJeff Kirsher {
4233dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
4234dee1ad47SJeff Kirsher 
4235dee1ad47SJeff Kirsher 	if (!(adapter->flags2 & FLAG2_DMA_BURST))
4236dee1ad47SJeff Kirsher 		return;
4237dee1ad47SJeff Kirsher 
4238dee1ad47SJeff Kirsher 	/* flush pending descriptor writebacks to memory */
4239dee1ad47SJeff Kirsher 	ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
4240dee1ad47SJeff Kirsher 	ew32(RDTR, adapter->rx_int_delay | E1000_RDTR_FPD);
4241dee1ad47SJeff Kirsher 
4242dee1ad47SJeff Kirsher 	/* execute the writes immediately */
4243dee1ad47SJeff Kirsher 	e1e_flush();
4244bf03085fSMatthew Vick 
4245e921eb1aSBruce Allan 	/* due to rare timing issues, write to TIDV/RDTR again to ensure the
4246bf03085fSMatthew Vick 	 * write is successful
4247bf03085fSMatthew Vick 	 */
4248bf03085fSMatthew Vick 	ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
4249bf03085fSMatthew Vick 	ew32(RDTR, adapter->rx_int_delay | E1000_RDTR_FPD);
4250bf03085fSMatthew Vick 
4251bf03085fSMatthew Vick 	/* execute the writes immediately */
4252bf03085fSMatthew Vick 	e1e_flush();
4253dee1ad47SJeff Kirsher }
4254dee1ad47SJeff Kirsher 
4255dee1ad47SJeff Kirsher static void e1000e_update_stats(struct e1000_adapter *adapter);
4256dee1ad47SJeff Kirsher 
425728002099SDavid Ertman /**
425828002099SDavid Ertman  * e1000e_down - quiesce the device and optionally reset the hardware
425928002099SDavid Ertman  * @adapter: board private structure
426028002099SDavid Ertman  * @reset: boolean flag to reset the hardware or not
426128002099SDavid Ertman  */
e1000e_down(struct e1000_adapter * adapter,bool reset)426228002099SDavid Ertman void e1000e_down(struct e1000_adapter *adapter, bool reset)
4263dee1ad47SJeff Kirsher {
4264dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
4265dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
4266dee1ad47SJeff Kirsher 	u32 tctl, rctl;
4267dee1ad47SJeff Kirsher 
4268e921eb1aSBruce Allan 	/* signal that we're down so the interrupt handler does not
4269dee1ad47SJeff Kirsher 	 * reschedule our watchdog timer
4270dee1ad47SJeff Kirsher 	 */
4271dee1ad47SJeff Kirsher 	set_bit(__E1000_DOWN, &adapter->state);
4272dee1ad47SJeff Kirsher 
4273a60a132eSEliezer Tamir 	netif_carrier_off(netdev);
4274a60a132eSEliezer Tamir 
4275dee1ad47SJeff Kirsher 	/* disable receives in the hardware */
4276dee1ad47SJeff Kirsher 	rctl = er32(RCTL);
4277823dcd25SDavid S. Miller 	if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
4278dee1ad47SJeff Kirsher 		ew32(RCTL, rctl & ~E1000_RCTL_EN);
4279dee1ad47SJeff Kirsher 	/* flush and sleep below */
4280dee1ad47SJeff Kirsher 
4281dee1ad47SJeff Kirsher 	netif_stop_queue(netdev);
4282dee1ad47SJeff Kirsher 
4283dee1ad47SJeff Kirsher 	/* disable transmits in the hardware */
4284dee1ad47SJeff Kirsher 	tctl = er32(TCTL);
4285dee1ad47SJeff Kirsher 	tctl &= ~E1000_TCTL_EN;
4286dee1ad47SJeff Kirsher 	ew32(TCTL, tctl);
4287823dcd25SDavid S. Miller 
4288dee1ad47SJeff Kirsher 	/* flush both disables and wait for them to finish */
4289dee1ad47SJeff Kirsher 	e1e_flush();
4290ab6973aeSArjan van de Ven 	usleep_range(10000, 11000);
4291dee1ad47SJeff Kirsher 
4292dee1ad47SJeff Kirsher 	e1000_irq_disable(adapter);
4293dee1ad47SJeff Kirsher 
4294a3b87a4cSBruce Allan 	napi_synchronize(&adapter->napi);
4295a3b87a4cSBruce Allan 
4296d5ad7a6aSJeff Kirsher 	del_timer_sync(&adapter->watchdog_timer);
4297dee1ad47SJeff Kirsher 	del_timer_sync(&adapter->phy_info_timer);
4298dee1ad47SJeff Kirsher 
4299dee1ad47SJeff Kirsher 	spin_lock(&adapter->stats64_lock);
4300dee1ad47SJeff Kirsher 	e1000e_update_stats(adapter);
4301dee1ad47SJeff Kirsher 	spin_unlock(&adapter->stats64_lock);
4302dee1ad47SJeff Kirsher 
4303dee1ad47SJeff Kirsher 	e1000e_flush_descriptors(adapter);
4304dee1ad47SJeff Kirsher 
4305dee1ad47SJeff Kirsher 	adapter->link_speed = 0;
4306dee1ad47SJeff Kirsher 	adapter->link_duplex = 0;
4307dee1ad47SJeff Kirsher 
4308da1e2046SBruce Allan 	/* Disable Si errata workaround on PCHx for jumbo frame flow */
4309da1e2046SBruce Allan 	if ((hw->mac.type >= e1000_pch2lan) &&
4310da1e2046SBruce Allan 	    (adapter->netdev->mtu > ETH_DATA_LEN) &&
4311da1e2046SBruce Allan 	    e1000_lv_jumbo_workaround_ich8lan(hw, false))
4312da1e2046SBruce Allan 		e_dbg("failed to disable jumbo frame workaround mode\n");
4313da1e2046SBruce Allan 
43140ffc5646SYanir Lubetkin 	if (!pci_channel_offline(adapter->pdev)) {
43150ffc5646SYanir Lubetkin 		if (reset)
4316dee1ad47SJeff Kirsher 			e1000e_reset(adapter);
4317c8744f44SSasha Neftin 		else if (hw->mac.type >= e1000_pch_spt)
43180ffc5646SYanir Lubetkin 			e1000_flush_desc_rings(adapter);
43190ffc5646SYanir Lubetkin 	}
43200ffc5646SYanir Lubetkin 	e1000_clean_tx_ring(adapter->tx_ring);
43210ffc5646SYanir Lubetkin 	e1000_clean_rx_ring(adapter->rx_ring);
4322dee1ad47SJeff Kirsher }
4323dee1ad47SJeff Kirsher 
e1000e_reinit_locked(struct e1000_adapter * adapter)4324dee1ad47SJeff Kirsher void e1000e_reinit_locked(struct e1000_adapter *adapter)
4325dee1ad47SJeff Kirsher {
4326dee1ad47SJeff Kirsher 	might_sleep();
4327dee1ad47SJeff Kirsher 	while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
4328ab6973aeSArjan van de Ven 		usleep_range(1000, 1100);
432928002099SDavid Ertman 	e1000e_down(adapter, true);
4330dee1ad47SJeff Kirsher 	e1000e_up(adapter);
4331dee1ad47SJeff Kirsher 	clear_bit(__E1000_RESETTING, &adapter->state);
4332dee1ad47SJeff Kirsher }
4333dee1ad47SJeff Kirsher 
4334dee1ad47SJeff Kirsher /**
43350be5b96cSJarod Wilson  * e1000e_sanitize_systim - sanitize raw cycle counter reads
43360be5b96cSJarod Wilson  * @hw: pointer to the HW structure
433798942d70SMiroslav Lichvar  * @systim: PHC time value read, sanitized and returned
433898942d70SMiroslav Lichvar  * @sts: structure to hold system time before and after reading SYSTIML,
433998942d70SMiroslav Lichvar  * may be NULL
43400be5b96cSJarod Wilson  *
43410be5b96cSJarod Wilson  * Errata for 82574/82583 possible bad bits read from SYSTIMH/L:
43420be5b96cSJarod Wilson  * check to see that the time is incrementing at a reasonable
43430be5b96cSJarod Wilson  * rate and is a multiple of incvalue.
43440be5b96cSJarod Wilson  **/
e1000e_sanitize_systim(struct e1000_hw * hw,u64 systim,struct ptp_system_timestamp * sts)434598942d70SMiroslav Lichvar static u64 e1000e_sanitize_systim(struct e1000_hw *hw, u64 systim,
434698942d70SMiroslav Lichvar 				  struct ptp_system_timestamp *sts)
43470be5b96cSJarod Wilson {
43480be5b96cSJarod Wilson 	u64 time_delta, rem, temp;
4349a5a1d1c2SThomas Gleixner 	u64 systim_next;
43500be5b96cSJarod Wilson 	u32 incvalue;
43510be5b96cSJarod Wilson 	int i;
43520be5b96cSJarod Wilson 
43530be5b96cSJarod Wilson 	incvalue = er32(TIMINCA) & E1000_TIMINCA_INCVALUE_MASK;
43540be5b96cSJarod Wilson 	for (i = 0; i < E1000_MAX_82574_SYSTIM_REREADS; i++) {
43550be5b96cSJarod Wilson 		/* latch SYSTIMH on read of SYSTIML */
435698942d70SMiroslav Lichvar 		ptp_read_system_prets(sts);
4357a5a1d1c2SThomas Gleixner 		systim_next = (u64)er32(SYSTIML);
435898942d70SMiroslav Lichvar 		ptp_read_system_postts(sts);
4359a5a1d1c2SThomas Gleixner 		systim_next |= (u64)er32(SYSTIMH) << 32;
43600be5b96cSJarod Wilson 
43610be5b96cSJarod Wilson 		time_delta = systim_next - systim;
43620be5b96cSJarod Wilson 		temp = time_delta;
43630be5b96cSJarod Wilson 		/* VMWare users have seen incvalue of zero, don't div / 0 */
43640be5b96cSJarod Wilson 		rem = incvalue ? do_div(temp, incvalue) : (time_delta != 0);
43650be5b96cSJarod Wilson 
43660be5b96cSJarod Wilson 		systim = systim_next;
43670be5b96cSJarod Wilson 
43680be5b96cSJarod Wilson 		if ((time_delta < E1000_82574_SYSTIM_EPSILON) && (rem == 0))
43690be5b96cSJarod Wilson 			break;
43700be5b96cSJarod Wilson 	}
43710be5b96cSJarod Wilson 
43720be5b96cSJarod Wilson 	return systim;
43730be5b96cSJarod Wilson }
43740be5b96cSJarod Wilson 
43750be5b96cSJarod Wilson /**
437698942d70SMiroslav Lichvar  * e1000e_read_systim - read SYSTIM register
437798942d70SMiroslav Lichvar  * @adapter: board private structure
437898942d70SMiroslav Lichvar  * @sts: structure which will contain system time before and after reading
437998942d70SMiroslav Lichvar  * SYSTIML, may be NULL
4380b67e1913SBruce Allan  **/
e1000e_read_systim(struct e1000_adapter * adapter,struct ptp_system_timestamp * sts)438198942d70SMiroslav Lichvar u64 e1000e_read_systim(struct e1000_adapter *adapter,
438298942d70SMiroslav Lichvar 		       struct ptp_system_timestamp *sts)
4383b67e1913SBruce Allan {
4384b67e1913SBruce Allan 	struct e1000_hw *hw = &adapter->hw;
438598942d70SMiroslav Lichvar 	u32 systimel, systimel_2, systimeh;
4386a5a1d1c2SThomas Gleixner 	u64 systim;
438737b12910SRaanan Avargil 	/* SYSTIMH latching upon SYSTIML read does not work well.
438837b12910SRaanan Avargil 	 * This means that if SYSTIML overflows after we read it but before
438937b12910SRaanan Avargil 	 * we read SYSTIMH, the value of SYSTIMH has been incremented and we
439037b12910SRaanan Avargil 	 * will experience a huge non linear increment in the systime value
439137b12910SRaanan Avargil 	 * to fix that we test for overflow and if true, we re-read systime.
439283129b37SYanir Lubetkin 	 */
439398942d70SMiroslav Lichvar 	ptp_read_system_prets(sts);
4394ab507c9aSDenys Vlasenko 	systimel = er32(SYSTIML);
439598942d70SMiroslav Lichvar 	ptp_read_system_postts(sts);
439637b12910SRaanan Avargil 	systimeh = er32(SYSTIMH);
4397ab507c9aSDenys Vlasenko 	/* Is systimel is so large that overflow is possible? */
4398ab507c9aSDenys Vlasenko 	if (systimel >= (u32)0xffffffff - E1000_TIMINCA_INCVALUE_MASK) {
439998942d70SMiroslav Lichvar 		ptp_read_system_prets(sts);
440098942d70SMiroslav Lichvar 		systimel_2 = er32(SYSTIML);
440198942d70SMiroslav Lichvar 		ptp_read_system_postts(sts);
4402ab507c9aSDenys Vlasenko 		if (systimel > systimel_2) {
440337b12910SRaanan Avargil 			/* There was an overflow, read again SYSTIMH, and use
440437b12910SRaanan Avargil 			 * systimel_2
440537b12910SRaanan Avargil 			 */
440637b12910SRaanan Avargil 			systimeh = er32(SYSTIMH);
4407ab507c9aSDenys Vlasenko 			systimel = systimel_2;
440837b12910SRaanan Avargil 		}
4409ab507c9aSDenys Vlasenko 	}
4410a5a1d1c2SThomas Gleixner 	systim = (u64)systimel;
4411a5a1d1c2SThomas Gleixner 	systim |= (u64)systimeh << 32;
4412b67e1913SBruce Allan 
44130be5b96cSJarod Wilson 	if (adapter->flags2 & FLAG2_CHECK_SYSTIM_OVERFLOW)
441498942d70SMiroslav Lichvar 		systim = e1000e_sanitize_systim(hw, systim, sts);
44155e7ff970STodd Fujinaka 
4416b67e1913SBruce Allan 	return systim;
4417b67e1913SBruce Allan }
4418b67e1913SBruce Allan 
4419b67e1913SBruce Allan /**
442098942d70SMiroslav Lichvar  * e1000e_cyclecounter_read - read raw cycle counter (used by time counter)
442198942d70SMiroslav Lichvar  * @cc: cyclecounter structure
442298942d70SMiroslav Lichvar  **/
e1000e_cyclecounter_read(const struct cyclecounter * cc)442398942d70SMiroslav Lichvar static u64 e1000e_cyclecounter_read(const struct cyclecounter *cc)
442498942d70SMiroslav Lichvar {
442598942d70SMiroslav Lichvar 	struct e1000_adapter *adapter = container_of(cc, struct e1000_adapter,
442698942d70SMiroslav Lichvar 						     cc);
442798942d70SMiroslav Lichvar 
442898942d70SMiroslav Lichvar 	return e1000e_read_systim(adapter, NULL);
442998942d70SMiroslav Lichvar }
443098942d70SMiroslav Lichvar 
443198942d70SMiroslav Lichvar /**
4432dee1ad47SJeff Kirsher  * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
4433dee1ad47SJeff Kirsher  * @adapter: board private structure to initialize
4434dee1ad47SJeff Kirsher  *
4435dee1ad47SJeff Kirsher  * e1000_sw_init initializes the Adapter private data structure.
4436dee1ad47SJeff Kirsher  * Fields are initialized based on PCI device information and
4437dee1ad47SJeff Kirsher  * OS network device settings (MTU size).
4438dee1ad47SJeff Kirsher  **/
e1000_sw_init(struct e1000_adapter * adapter)44399f9a12f8SBill Pemberton static int e1000_sw_init(struct e1000_adapter *adapter)
4440dee1ad47SJeff Kirsher {
4441dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
4442dee1ad47SJeff Kirsher 
44438084b86dSAlexander Duyck 	adapter->rx_buffer_len = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
4444dee1ad47SJeff Kirsher 	adapter->rx_ps_bsize0 = 128;
44458084b86dSAlexander Duyck 	adapter->max_frame_size = netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
4446dee1ad47SJeff Kirsher 	adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
444755aa6985SBruce Allan 	adapter->tx_ring_count = E1000_DEFAULT_TXD;
444855aa6985SBruce Allan 	adapter->rx_ring_count = E1000_DEFAULT_RXD;
4449dee1ad47SJeff Kirsher 
4450dee1ad47SJeff Kirsher 	spin_lock_init(&adapter->stats64_lock);
4451dee1ad47SJeff Kirsher 
4452dee1ad47SJeff Kirsher 	e1000e_set_interrupt_capability(adapter);
4453dee1ad47SJeff Kirsher 
4454dee1ad47SJeff Kirsher 	if (e1000_alloc_queues(adapter))
4455dee1ad47SJeff Kirsher 		return -ENOMEM;
4456dee1ad47SJeff Kirsher 
4457b67e1913SBruce Allan 	/* Setup hardware time stamping cyclecounter */
4458b67e1913SBruce Allan 	if (adapter->flags & FLAG_HAS_HW_TIMESTAMP) {
4459b67e1913SBruce Allan 		adapter->cc.read = e1000e_cyclecounter_read;
44604d045b4cSRichard Cochran 		adapter->cc.mask = CYCLECOUNTER_MASK(64);
4461b67e1913SBruce Allan 		adapter->cc.mult = 1;
4462b67e1913SBruce Allan 		/* cc.shift set in e1000e_get_base_tininca() */
4463b67e1913SBruce Allan 
4464b67e1913SBruce Allan 		spin_lock_init(&adapter->systim_lock);
4465b67e1913SBruce Allan 		INIT_WORK(&adapter->tx_hwtstamp_work, e1000e_tx_hwtstamp_work);
4466b67e1913SBruce Allan 	}
4467b67e1913SBruce Allan 
4468dee1ad47SJeff Kirsher 	/* Explicitly disable IRQ since the NIC can be in any state. */
4469dee1ad47SJeff Kirsher 	e1000_irq_disable(adapter);
4470dee1ad47SJeff Kirsher 
4471dee1ad47SJeff Kirsher 	set_bit(__E1000_DOWN, &adapter->state);
4472dee1ad47SJeff Kirsher 	return 0;
4473dee1ad47SJeff Kirsher }
4474dee1ad47SJeff Kirsher 
4475dee1ad47SJeff Kirsher /**
4476dee1ad47SJeff Kirsher  * e1000_intr_msi_test - Interrupt Handler
4477dee1ad47SJeff Kirsher  * @irq: interrupt number
4478dee1ad47SJeff Kirsher  * @data: pointer to a network interface device structure
4479dee1ad47SJeff Kirsher  **/
e1000_intr_msi_test(int __always_unused irq,void * data)44808bb62869SBruce Allan static irqreturn_t e1000_intr_msi_test(int __always_unused irq, void *data)
4481dee1ad47SJeff Kirsher {
4482dee1ad47SJeff Kirsher 	struct net_device *netdev = data;
4483dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
4484dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
4485dee1ad47SJeff Kirsher 	u32 icr = er32(ICR);
4486dee1ad47SJeff Kirsher 
4487dee1ad47SJeff Kirsher 	e_dbg("icr is %08X\n", icr);
4488dee1ad47SJeff Kirsher 	if (icr & E1000_ICR_RXSEQ) {
4489dee1ad47SJeff Kirsher 		adapter->flags &= ~FLAG_MSI_TEST_FAILED;
4490e921eb1aSBruce Allan 		/* Force memory writes to complete before acknowledging the
4491bc76329dSBruce Allan 		 * interrupt is handled.
4492bc76329dSBruce Allan 		 */
4493dee1ad47SJeff Kirsher 		wmb();
4494dee1ad47SJeff Kirsher 	}
4495dee1ad47SJeff Kirsher 
4496dee1ad47SJeff Kirsher 	return IRQ_HANDLED;
4497dee1ad47SJeff Kirsher }
4498dee1ad47SJeff Kirsher 
4499dee1ad47SJeff Kirsher /**
4500dee1ad47SJeff Kirsher  * e1000_test_msi_interrupt - Returns 0 for successful test
4501dee1ad47SJeff Kirsher  * @adapter: board private struct
4502dee1ad47SJeff Kirsher  *
4503dee1ad47SJeff Kirsher  * code flow taken from tg3.c
4504dee1ad47SJeff Kirsher  **/
e1000_test_msi_interrupt(struct e1000_adapter * adapter)4505dee1ad47SJeff Kirsher static int e1000_test_msi_interrupt(struct e1000_adapter *adapter)
4506dee1ad47SJeff Kirsher {
4507dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
4508dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
4509dee1ad47SJeff Kirsher 	int err;
4510dee1ad47SJeff Kirsher 
4511dee1ad47SJeff Kirsher 	/* poll_enable hasn't been called yet, so don't need disable */
4512dee1ad47SJeff Kirsher 	/* clear any pending events */
4513dee1ad47SJeff Kirsher 	er32(ICR);
4514dee1ad47SJeff Kirsher 
4515dee1ad47SJeff Kirsher 	/* free the real vector and request a test handler */
4516dee1ad47SJeff Kirsher 	e1000_free_irq(adapter);
4517dee1ad47SJeff Kirsher 	e1000e_reset_interrupt_capability(adapter);
4518dee1ad47SJeff Kirsher 
4519dee1ad47SJeff Kirsher 	/* Assume that the test fails, if it succeeds then the test
4520e921eb1aSBruce Allan 	 * MSI irq handler will unset this flag
4521e921eb1aSBruce Allan 	 */
4522dee1ad47SJeff Kirsher 	adapter->flags |= FLAG_MSI_TEST_FAILED;
4523dee1ad47SJeff Kirsher 
4524dee1ad47SJeff Kirsher 	err = pci_enable_msi(adapter->pdev);
4525dee1ad47SJeff Kirsher 	if (err)
4526dee1ad47SJeff Kirsher 		goto msi_test_failed;
4527dee1ad47SJeff Kirsher 
4528dee1ad47SJeff Kirsher 	err = request_irq(adapter->pdev->irq, e1000_intr_msi_test, 0,
4529dee1ad47SJeff Kirsher 			  netdev->name, netdev);
4530dee1ad47SJeff Kirsher 	if (err) {
4531dee1ad47SJeff Kirsher 		pci_disable_msi(adapter->pdev);
4532dee1ad47SJeff Kirsher 		goto msi_test_failed;
4533dee1ad47SJeff Kirsher 	}
4534dee1ad47SJeff Kirsher 
4535e921eb1aSBruce Allan 	/* Force memory writes to complete before enabling and firing an
4536bc76329dSBruce Allan 	 * interrupt.
4537bc76329dSBruce Allan 	 */
4538dee1ad47SJeff Kirsher 	wmb();
4539dee1ad47SJeff Kirsher 
4540dee1ad47SJeff Kirsher 	e1000_irq_enable(adapter);
4541dee1ad47SJeff Kirsher 
4542dee1ad47SJeff Kirsher 	/* fire an unusual interrupt on the test handler */
4543dee1ad47SJeff Kirsher 	ew32(ICS, E1000_ICS_RXSEQ);
4544dee1ad47SJeff Kirsher 	e1e_flush();
4545569a3affSPrasanna S Panchamukhi 	msleep(100);
4546dee1ad47SJeff Kirsher 
4547dee1ad47SJeff Kirsher 	e1000_irq_disable(adapter);
4548dee1ad47SJeff Kirsher 
4549bc76329dSBruce Allan 	rmb();			/* read flags after interrupt has been fired */
4550dee1ad47SJeff Kirsher 
4551dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_MSI_TEST_FAILED) {
4552dee1ad47SJeff Kirsher 		adapter->int_mode = E1000E_INT_MODE_LEGACY;
4553dee1ad47SJeff Kirsher 		e_info("MSI interrupt test failed, using legacy interrupt.\n");
455424b706b2SBruce Allan 	} else {
4555dee1ad47SJeff Kirsher 		e_dbg("MSI interrupt test succeeded!\n");
455624b706b2SBruce Allan 	}
4557dee1ad47SJeff Kirsher 
4558dee1ad47SJeff Kirsher 	free_irq(adapter->pdev->irq, netdev);
4559dee1ad47SJeff Kirsher 	pci_disable_msi(adapter->pdev);
4560dee1ad47SJeff Kirsher 
4561dee1ad47SJeff Kirsher msi_test_failed:
4562dee1ad47SJeff Kirsher 	e1000e_set_interrupt_capability(adapter);
4563dee1ad47SJeff Kirsher 	return e1000_request_irq(adapter);
4564dee1ad47SJeff Kirsher }
4565dee1ad47SJeff Kirsher 
4566dee1ad47SJeff Kirsher /**
4567dee1ad47SJeff Kirsher  * e1000_test_msi - Returns 0 if MSI test succeeds or INTx mode is restored
4568dee1ad47SJeff Kirsher  * @adapter: board private struct
4569dee1ad47SJeff Kirsher  *
4570dee1ad47SJeff Kirsher  * code flow taken from tg3.c, called with e1000 interrupts disabled.
4571dee1ad47SJeff Kirsher  **/
e1000_test_msi(struct e1000_adapter * adapter)4572dee1ad47SJeff Kirsher static int e1000_test_msi(struct e1000_adapter *adapter)
4573dee1ad47SJeff Kirsher {
4574dee1ad47SJeff Kirsher 	int err;
4575dee1ad47SJeff Kirsher 	u16 pci_cmd;
4576dee1ad47SJeff Kirsher 
4577dee1ad47SJeff Kirsher 	if (!(adapter->flags & FLAG_MSI_ENABLED))
4578dee1ad47SJeff Kirsher 		return 0;
4579dee1ad47SJeff Kirsher 
4580dee1ad47SJeff Kirsher 	/* disable SERR in case the MSI write causes a master abort */
4581dee1ad47SJeff Kirsher 	pci_read_config_word(adapter->pdev, PCI_COMMAND, &pci_cmd);
4582dee1ad47SJeff Kirsher 	if (pci_cmd & PCI_COMMAND_SERR)
4583dee1ad47SJeff Kirsher 		pci_write_config_word(adapter->pdev, PCI_COMMAND,
4584dee1ad47SJeff Kirsher 				      pci_cmd & ~PCI_COMMAND_SERR);
4585dee1ad47SJeff Kirsher 
4586dee1ad47SJeff Kirsher 	err = e1000_test_msi_interrupt(adapter);
4587dee1ad47SJeff Kirsher 
4588dee1ad47SJeff Kirsher 	/* re-enable SERR */
4589dee1ad47SJeff Kirsher 	if (pci_cmd & PCI_COMMAND_SERR) {
4590dee1ad47SJeff Kirsher 		pci_read_config_word(adapter->pdev, PCI_COMMAND, &pci_cmd);
4591dee1ad47SJeff Kirsher 		pci_cmd |= PCI_COMMAND_SERR;
4592dee1ad47SJeff Kirsher 		pci_write_config_word(adapter->pdev, PCI_COMMAND, pci_cmd);
4593dee1ad47SJeff Kirsher 	}
4594dee1ad47SJeff Kirsher 
4595dee1ad47SJeff Kirsher 	return err;
4596dee1ad47SJeff Kirsher }
4597dee1ad47SJeff Kirsher 
4598dee1ad47SJeff Kirsher /**
4599d5ea45daSStefan Assmann  * e1000e_open - Called when a network interface is made active
4600dee1ad47SJeff Kirsher  * @netdev: network interface device structure
4601dee1ad47SJeff Kirsher  *
4602dee1ad47SJeff Kirsher  * Returns 0 on success, negative value on failure
4603dee1ad47SJeff Kirsher  *
4604dee1ad47SJeff Kirsher  * The open entry point is called when a network interface is made
4605dee1ad47SJeff Kirsher  * active by the system (IFF_UP).  At this point all resources needed
4606dee1ad47SJeff Kirsher  * for transmit and receive operations are allocated, the interrupt
4607dee1ad47SJeff Kirsher  * handler is registered with the OS, the watchdog timer is started,
4608dee1ad47SJeff Kirsher  * and the stack is notified that the interface is ready.
4609dee1ad47SJeff Kirsher  **/
e1000e_open(struct net_device * netdev)4610d5ea45daSStefan Assmann int e1000e_open(struct net_device *netdev)
4611dee1ad47SJeff Kirsher {
4612dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
4613dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
4614dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
4615dee1ad47SJeff Kirsher 	int err;
4616dee1ad47SJeff Kirsher 
4617dee1ad47SJeff Kirsher 	/* disallow open during test */
4618dee1ad47SJeff Kirsher 	if (test_bit(__E1000_TESTING, &adapter->state))
4619dee1ad47SJeff Kirsher 		return -EBUSY;
4620dee1ad47SJeff Kirsher 
4621dee1ad47SJeff Kirsher 	pm_runtime_get_sync(&pdev->dev);
4622dee1ad47SJeff Kirsher 
4623dee1ad47SJeff Kirsher 	netif_carrier_off(netdev);
4624d17ba0f6SKonstantin Khlebnikov 	netif_stop_queue(netdev);
4625dee1ad47SJeff Kirsher 
4626dee1ad47SJeff Kirsher 	/* allocate transmit descriptors */
462755aa6985SBruce Allan 	err = e1000e_setup_tx_resources(adapter->tx_ring);
4628dee1ad47SJeff Kirsher 	if (err)
4629dee1ad47SJeff Kirsher 		goto err_setup_tx;
4630dee1ad47SJeff Kirsher 
4631dee1ad47SJeff Kirsher 	/* allocate receive descriptors */
463255aa6985SBruce Allan 	err = e1000e_setup_rx_resources(adapter->rx_ring);
4633dee1ad47SJeff Kirsher 	if (err)
4634dee1ad47SJeff Kirsher 		goto err_setup_rx;
4635dee1ad47SJeff Kirsher 
4636e921eb1aSBruce Allan 	/* If AMT is enabled, let the firmware know that the network
4637dee1ad47SJeff Kirsher 	 * interface is now open and reset the part to a known state.
4638dee1ad47SJeff Kirsher 	 */
4639dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_HAS_AMT) {
4640dee1ad47SJeff Kirsher 		e1000e_get_hw_control(adapter);
4641dee1ad47SJeff Kirsher 		e1000e_reset(adapter);
4642dee1ad47SJeff Kirsher 	}
4643dee1ad47SJeff Kirsher 
4644dee1ad47SJeff Kirsher 	e1000e_power_up_phy(adapter);
4645dee1ad47SJeff Kirsher 
4646dee1ad47SJeff Kirsher 	adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
4647e5fe2541SBruce Allan 	if ((adapter->hw.mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
4648dee1ad47SJeff Kirsher 		e1000_update_mng_vlan(adapter);
4649dee1ad47SJeff Kirsher 
465079d4e908SBruce Allan 	/* DMA latency requirement to workaround jumbo issue */
465181e95ad7SRafael J. Wysocki 	cpu_latency_qos_add_request(&adapter->pm_qos_req, PM_QOS_DEFAULT_VALUE);
4652dee1ad47SJeff Kirsher 
4653e921eb1aSBruce Allan 	/* before we allocate an interrupt, we must be ready to handle it.
4654dee1ad47SJeff Kirsher 	 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
4655dee1ad47SJeff Kirsher 	 * as soon as we call pci_request_irq, so we have to setup our
4656dee1ad47SJeff Kirsher 	 * clean_rx handler before we do so.
4657dee1ad47SJeff Kirsher 	 */
4658dee1ad47SJeff Kirsher 	e1000_configure(adapter);
4659dee1ad47SJeff Kirsher 
4660dee1ad47SJeff Kirsher 	err = e1000_request_irq(adapter);
4661dee1ad47SJeff Kirsher 	if (err)
4662dee1ad47SJeff Kirsher 		goto err_req_irq;
4663dee1ad47SJeff Kirsher 
4664e921eb1aSBruce Allan 	/* Work around PCIe errata with MSI interrupts causing some chipsets to
4665dee1ad47SJeff Kirsher 	 * ignore e1000e MSI messages, which means we need to test our MSI
4666dee1ad47SJeff Kirsher 	 * interrupt now
4667dee1ad47SJeff Kirsher 	 */
4668dee1ad47SJeff Kirsher 	if (adapter->int_mode != E1000E_INT_MODE_LEGACY) {
4669dee1ad47SJeff Kirsher 		err = e1000_test_msi(adapter);
4670dee1ad47SJeff Kirsher 		if (err) {
4671dee1ad47SJeff Kirsher 			e_err("Interrupt allocation failed\n");
4672dee1ad47SJeff Kirsher 			goto err_req_irq;
4673dee1ad47SJeff Kirsher 		}
4674dee1ad47SJeff Kirsher 	}
4675dee1ad47SJeff Kirsher 
4676dee1ad47SJeff Kirsher 	/* From here on the code is the same as e1000e_up() */
4677dee1ad47SJeff Kirsher 	clear_bit(__E1000_DOWN, &adapter->state);
4678dee1ad47SJeff Kirsher 
4679dee1ad47SJeff Kirsher 	napi_enable(&adapter->napi);
4680dee1ad47SJeff Kirsher 
4681dee1ad47SJeff Kirsher 	e1000_irq_enable(adapter);
4682dee1ad47SJeff Kirsher 
468309357b00SJeff Kirsher 	adapter->tx_hang_recheck = false;
4684dee1ad47SJeff Kirsher 
468566148babSKonstantin Khlebnikov 	hw->mac.get_link_status = true;
4686dee1ad47SJeff Kirsher 	pm_runtime_put(&pdev->dev);
4687dee1ad47SJeff Kirsher 
4688a61cfe4fSBenjamin Poirier 	e1000e_trigger_lsc(adapter);
4689dee1ad47SJeff Kirsher 
4690dee1ad47SJeff Kirsher 	return 0;
4691dee1ad47SJeff Kirsher 
4692dee1ad47SJeff Kirsher err_req_irq:
469381e95ad7SRafael J. Wysocki 	cpu_latency_qos_remove_request(&adapter->pm_qos_req);
4694dee1ad47SJeff Kirsher 	e1000e_release_hw_control(adapter);
4695dee1ad47SJeff Kirsher 	e1000_power_down_phy(adapter);
469655aa6985SBruce Allan 	e1000e_free_rx_resources(adapter->rx_ring);
4697dee1ad47SJeff Kirsher err_setup_rx:
469855aa6985SBruce Allan 	e1000e_free_tx_resources(adapter->tx_ring);
4699dee1ad47SJeff Kirsher err_setup_tx:
4700dee1ad47SJeff Kirsher 	e1000e_reset(adapter);
4701dee1ad47SJeff Kirsher 	pm_runtime_put_sync(&pdev->dev);
4702dee1ad47SJeff Kirsher 
4703dee1ad47SJeff Kirsher 	return err;
4704dee1ad47SJeff Kirsher }
4705dee1ad47SJeff Kirsher 
4706dee1ad47SJeff Kirsher /**
4707d5ea45daSStefan Assmann  * e1000e_close - Disables a network interface
4708dee1ad47SJeff Kirsher  * @netdev: network interface device structure
4709dee1ad47SJeff Kirsher  *
4710dee1ad47SJeff Kirsher  * Returns 0, this is not allowed to fail
4711dee1ad47SJeff Kirsher  *
4712dee1ad47SJeff Kirsher  * The close entry point is called when an interface is de-activated
4713dee1ad47SJeff Kirsher  * by the OS.  The hardware is still under the drivers control, but
4714dee1ad47SJeff Kirsher  * needs to be disabled.  A global MAC reset is issued to stop the
4715dee1ad47SJeff Kirsher  * hardware, and all transmit and receive resources are freed.
4716dee1ad47SJeff Kirsher  **/
e1000e_close(struct net_device * netdev)4717d5ea45daSStefan Assmann int e1000e_close(struct net_device *netdev)
4718dee1ad47SJeff Kirsher {
4719dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
4720dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
4721bb9e44d0SBruce Allan 	int count = E1000_CHECK_RESET_COUNT;
4722bb9e44d0SBruce Allan 
4723bb9e44d0SBruce Allan 	while (test_bit(__E1000_RESETTING, &adapter->state) && count--)
4724ab6973aeSArjan van de Ven 		usleep_range(10000, 11000);
4725dee1ad47SJeff Kirsher 
4726dee1ad47SJeff Kirsher 	WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
4727dee1ad47SJeff Kirsher 
4728dee1ad47SJeff Kirsher 	pm_runtime_get_sync(&pdev->dev);
4729dee1ad47SJeff Kirsher 
4730a7023819SAlexander Duyck 	if (netif_device_present(netdev)) {
473128002099SDavid Ertman 		e1000e_down(adapter, true);
4732dee1ad47SJeff Kirsher 		e1000_free_irq(adapter);
473363eb48f1SDavid Ertman 
473463eb48f1SDavid Ertman 		/* Link status message must follow this format */
4735c557a4b3SAlexander Duyck 		netdev_info(netdev, "NIC Link is Down\n");
4736dee1ad47SJeff Kirsher 	}
4737a3b87a4cSBruce Allan 
4738a3b87a4cSBruce Allan 	napi_disable(&adapter->napi);
4739a3b87a4cSBruce Allan 
474055aa6985SBruce Allan 	e1000e_free_tx_resources(adapter->tx_ring);
474155aa6985SBruce Allan 	e1000e_free_rx_resources(adapter->rx_ring);
4742dee1ad47SJeff Kirsher 
4743e921eb1aSBruce Allan 	/* kill manageability vlan ID if supported, but not if a vlan with
4744dee1ad47SJeff Kirsher 	 * the same ID is registered on the host OS (let 8021q kill it)
4745dee1ad47SJeff Kirsher 	 */
4746e5fe2541SBruce Allan 	if (adapter->hw.mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN)
474780d5c368SPatrick McHardy 		e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q),
474880d5c368SPatrick McHardy 				       adapter->mng_vlan_id);
4749dee1ad47SJeff Kirsher 
4750e921eb1aSBruce Allan 	/* If AMT is enabled, let the firmware know that the network
4751dee1ad47SJeff Kirsher 	 * interface is now closed
4752dee1ad47SJeff Kirsher 	 */
4753dee1ad47SJeff Kirsher 	if ((adapter->flags & FLAG_HAS_AMT) &&
4754dee1ad47SJeff Kirsher 	    !test_bit(__E1000_TESTING, &adapter->state))
4755dee1ad47SJeff Kirsher 		e1000e_release_hw_control(adapter);
4756dee1ad47SJeff Kirsher 
475781e95ad7SRafael J. Wysocki 	cpu_latency_qos_remove_request(&adapter->pm_qos_req);
4758dee1ad47SJeff Kirsher 
4759dee1ad47SJeff Kirsher 	pm_runtime_put_sync(&pdev->dev);
4760dee1ad47SJeff Kirsher 
4761dee1ad47SJeff Kirsher 	return 0;
4762dee1ad47SJeff Kirsher }
4763fc830b78SBruce Allan 
4764dee1ad47SJeff Kirsher /**
4765dee1ad47SJeff Kirsher  * e1000_set_mac - Change the Ethernet Address of the NIC
4766dee1ad47SJeff Kirsher  * @netdev: network interface device structure
4767dee1ad47SJeff Kirsher  * @p: pointer to an address structure
4768dee1ad47SJeff Kirsher  *
4769dee1ad47SJeff Kirsher  * Returns 0 on success, negative on failure
4770dee1ad47SJeff Kirsher  **/
e1000_set_mac(struct net_device * netdev,void * p)4771dee1ad47SJeff Kirsher static int e1000_set_mac(struct net_device *netdev, void *p)
4772dee1ad47SJeff Kirsher {
4773dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
477469e1e019SBruce Allan 	struct e1000_hw *hw = &adapter->hw;
4775dee1ad47SJeff Kirsher 	struct sockaddr *addr = p;
4776dee1ad47SJeff Kirsher 
4777dee1ad47SJeff Kirsher 	if (!is_valid_ether_addr(addr->sa_data))
4778dee1ad47SJeff Kirsher 		return -EADDRNOTAVAIL;
4779dee1ad47SJeff Kirsher 
4780a05e4c0aSJakub Kicinski 	eth_hw_addr_set(netdev, addr->sa_data);
4781dee1ad47SJeff Kirsher 	memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
4782dee1ad47SJeff Kirsher 
478369e1e019SBruce Allan 	hw->mac.ops.rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
4784dee1ad47SJeff Kirsher 
4785dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_RESET_OVERWRITES_LAA) {
4786dee1ad47SJeff Kirsher 		/* activate the work around */
4787dee1ad47SJeff Kirsher 		e1000e_set_laa_state_82571(&adapter->hw, 1);
4788dee1ad47SJeff Kirsher 
4789e921eb1aSBruce Allan 		/* Hold a copy of the LAA in RAR[14] This is done so that
4790dee1ad47SJeff Kirsher 		 * between the time RAR[0] gets clobbered  and the time it
4791dee1ad47SJeff Kirsher 		 * gets fixed (in e1000_watchdog), the actual LAA is in one
4792dee1ad47SJeff Kirsher 		 * of the RARs and no incoming packets directed to this port
4793dee1ad47SJeff Kirsher 		 * are dropped. Eventually the LAA will be in RAR[0] and
4794dee1ad47SJeff Kirsher 		 * RAR[14]
4795dee1ad47SJeff Kirsher 		 */
479669e1e019SBruce Allan 		hw->mac.ops.rar_set(&adapter->hw, adapter->hw.mac.addr,
4797dee1ad47SJeff Kirsher 				    adapter->hw.mac.rar_entry_count - 1);
4798dee1ad47SJeff Kirsher 	}
4799dee1ad47SJeff Kirsher 
4800dee1ad47SJeff Kirsher 	return 0;
4801dee1ad47SJeff Kirsher }
4802dee1ad47SJeff Kirsher 
4803dee1ad47SJeff Kirsher /**
4804dee1ad47SJeff Kirsher  * e1000e_update_phy_task - work thread to update phy
4805dee1ad47SJeff Kirsher  * @work: pointer to our work struct
4806dee1ad47SJeff Kirsher  *
4807dee1ad47SJeff Kirsher  * this worker thread exists because we must acquire a
4808dee1ad47SJeff Kirsher  * semaphore to read the phy, which we could msleep while
4809dee1ad47SJeff Kirsher  * waiting for it, and we can't msleep in a timer.
4810dee1ad47SJeff Kirsher  **/
e1000e_update_phy_task(struct work_struct * work)4811dee1ad47SJeff Kirsher static void e1000e_update_phy_task(struct work_struct *work)
4812dee1ad47SJeff Kirsher {
4813dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = container_of(work,
481417e813ecSBruce Allan 						     struct e1000_adapter,
481517e813ecSBruce Allan 						     update_phy_task);
4816a03206edSDavid Ertman 	struct e1000_hw *hw = &adapter->hw;
4817dee1ad47SJeff Kirsher 
4818dee1ad47SJeff Kirsher 	if (test_bit(__E1000_DOWN, &adapter->state))
4819dee1ad47SJeff Kirsher 		return;
4820dee1ad47SJeff Kirsher 
4821a03206edSDavid Ertman 	e1000_get_phy_info(hw);
4822a03206edSDavid Ertman 
4823a03206edSDavid Ertman 	/* Enable EEE on 82579 after link up */
482450844bb7SDavid Ertman 	if (hw->phy.type >= e1000_phy_82579)
4825a03206edSDavid Ertman 		e1000_set_eee_pchlan(hw);
4826dee1ad47SJeff Kirsher }
4827dee1ad47SJeff Kirsher 
4828e921eb1aSBruce Allan /**
4829e921eb1aSBruce Allan  * e1000_update_phy_info - timre call-back to update PHY info
4830b50f7bcaSJesse Brandeburg  * @t: pointer to timer_list containing private info adapter
4831e921eb1aSBruce Allan  *
4832dee1ad47SJeff Kirsher  * Need to wait a few seconds after link up to get diagnostic information from
4833dee1ad47SJeff Kirsher  * the phy
4834e921eb1aSBruce Allan  **/
e1000_update_phy_info(struct timer_list * t)483526566eaeSKees Cook static void e1000_update_phy_info(struct timer_list *t)
4836dee1ad47SJeff Kirsher {
483726566eaeSKees Cook 	struct e1000_adapter *adapter = from_timer(adapter, t, phy_info_timer);
4838dee1ad47SJeff Kirsher 
4839dee1ad47SJeff Kirsher 	if (test_bit(__E1000_DOWN, &adapter->state))
4840dee1ad47SJeff Kirsher 		return;
4841dee1ad47SJeff Kirsher 
4842dee1ad47SJeff Kirsher 	schedule_work(&adapter->update_phy_task);
4843dee1ad47SJeff Kirsher }
4844dee1ad47SJeff Kirsher 
4845dee1ad47SJeff Kirsher /**
4846dee1ad47SJeff Kirsher  * e1000e_update_phy_stats - Update the PHY statistics counters
4847dee1ad47SJeff Kirsher  * @adapter: board private structure
4848dee1ad47SJeff Kirsher  *
4849dee1ad47SJeff Kirsher  * Read/clear the upper 16-bit PHY registers and read/accumulate lower
4850dee1ad47SJeff Kirsher  **/
e1000e_update_phy_stats(struct e1000_adapter * adapter)4851dee1ad47SJeff Kirsher static void e1000e_update_phy_stats(struct e1000_adapter *adapter)
4852dee1ad47SJeff Kirsher {
4853dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
4854dee1ad47SJeff Kirsher 	s32 ret_val;
4855dee1ad47SJeff Kirsher 	u16 phy_data;
4856dee1ad47SJeff Kirsher 
4857dee1ad47SJeff Kirsher 	ret_val = hw->phy.ops.acquire(hw);
4858dee1ad47SJeff Kirsher 	if (ret_val)
4859dee1ad47SJeff Kirsher 		return;
4860dee1ad47SJeff Kirsher 
4861e921eb1aSBruce Allan 	/* A page set is expensive so check if already on desired page.
4862dee1ad47SJeff Kirsher 	 * If not, set to the page with the PHY status registers.
4863dee1ad47SJeff Kirsher 	 */
4864dee1ad47SJeff Kirsher 	hw->phy.addr = 1;
4865dee1ad47SJeff Kirsher 	ret_val = e1000e_read_phy_reg_mdic(hw, IGP01E1000_PHY_PAGE_SELECT,
4866dee1ad47SJeff Kirsher 					   &phy_data);
4867dee1ad47SJeff Kirsher 	if (ret_val)
4868dee1ad47SJeff Kirsher 		goto release;
4869dee1ad47SJeff Kirsher 	if (phy_data != (HV_STATS_PAGE << IGP_PAGE_SHIFT)) {
4870dee1ad47SJeff Kirsher 		ret_val = hw->phy.ops.set_page(hw,
4871dee1ad47SJeff Kirsher 					       HV_STATS_PAGE << IGP_PAGE_SHIFT);
4872dee1ad47SJeff Kirsher 		if (ret_val)
4873dee1ad47SJeff Kirsher 			goto release;
4874dee1ad47SJeff Kirsher 	}
4875dee1ad47SJeff Kirsher 
4876dee1ad47SJeff Kirsher 	/* Single Collision Count */
4877dee1ad47SJeff Kirsher 	hw->phy.ops.read_reg_page(hw, HV_SCC_UPPER, &phy_data);
4878dee1ad47SJeff Kirsher 	ret_val = hw->phy.ops.read_reg_page(hw, HV_SCC_LOWER, &phy_data);
4879dee1ad47SJeff Kirsher 	if (!ret_val)
4880dee1ad47SJeff Kirsher 		adapter->stats.scc += phy_data;
4881dee1ad47SJeff Kirsher 
4882dee1ad47SJeff Kirsher 	/* Excessive Collision Count */
4883dee1ad47SJeff Kirsher 	hw->phy.ops.read_reg_page(hw, HV_ECOL_UPPER, &phy_data);
4884dee1ad47SJeff Kirsher 	ret_val = hw->phy.ops.read_reg_page(hw, HV_ECOL_LOWER, &phy_data);
4885dee1ad47SJeff Kirsher 	if (!ret_val)
4886dee1ad47SJeff Kirsher 		adapter->stats.ecol += phy_data;
4887dee1ad47SJeff Kirsher 
4888dee1ad47SJeff Kirsher 	/* Multiple Collision Count */
4889dee1ad47SJeff Kirsher 	hw->phy.ops.read_reg_page(hw, HV_MCC_UPPER, &phy_data);
4890dee1ad47SJeff Kirsher 	ret_val = hw->phy.ops.read_reg_page(hw, HV_MCC_LOWER, &phy_data);
4891dee1ad47SJeff Kirsher 	if (!ret_val)
4892dee1ad47SJeff Kirsher 		adapter->stats.mcc += phy_data;
4893dee1ad47SJeff Kirsher 
4894dee1ad47SJeff Kirsher 	/* Late Collision Count */
4895dee1ad47SJeff Kirsher 	hw->phy.ops.read_reg_page(hw, HV_LATECOL_UPPER, &phy_data);
4896dee1ad47SJeff Kirsher 	ret_val = hw->phy.ops.read_reg_page(hw, HV_LATECOL_LOWER, &phy_data);
4897dee1ad47SJeff Kirsher 	if (!ret_val)
4898dee1ad47SJeff Kirsher 		adapter->stats.latecol += phy_data;
4899dee1ad47SJeff Kirsher 
4900dee1ad47SJeff Kirsher 	/* Collision Count - also used for adaptive IFS */
4901dee1ad47SJeff Kirsher 	hw->phy.ops.read_reg_page(hw, HV_COLC_UPPER, &phy_data);
4902dee1ad47SJeff Kirsher 	ret_val = hw->phy.ops.read_reg_page(hw, HV_COLC_LOWER, &phy_data);
4903dee1ad47SJeff Kirsher 	if (!ret_val)
4904dee1ad47SJeff Kirsher 		hw->mac.collision_delta = phy_data;
4905dee1ad47SJeff Kirsher 
4906dee1ad47SJeff Kirsher 	/* Defer Count */
4907dee1ad47SJeff Kirsher 	hw->phy.ops.read_reg_page(hw, HV_DC_UPPER, &phy_data);
4908dee1ad47SJeff Kirsher 	ret_val = hw->phy.ops.read_reg_page(hw, HV_DC_LOWER, &phy_data);
4909dee1ad47SJeff Kirsher 	if (!ret_val)
4910dee1ad47SJeff Kirsher 		adapter->stats.dc += phy_data;
4911dee1ad47SJeff Kirsher 
4912dee1ad47SJeff Kirsher 	/* Transmit with no CRS */
4913dee1ad47SJeff Kirsher 	hw->phy.ops.read_reg_page(hw, HV_TNCRS_UPPER, &phy_data);
4914dee1ad47SJeff Kirsher 	ret_val = hw->phy.ops.read_reg_page(hw, HV_TNCRS_LOWER, &phy_data);
4915dee1ad47SJeff Kirsher 	if (!ret_val)
4916dee1ad47SJeff Kirsher 		adapter->stats.tncrs += phy_data;
4917dee1ad47SJeff Kirsher 
4918dee1ad47SJeff Kirsher release:
4919dee1ad47SJeff Kirsher 	hw->phy.ops.release(hw);
4920dee1ad47SJeff Kirsher }
4921dee1ad47SJeff Kirsher 
4922dee1ad47SJeff Kirsher /**
4923dee1ad47SJeff Kirsher  * e1000e_update_stats - Update the board statistics counters
4924dee1ad47SJeff Kirsher  * @adapter: board private structure
4925dee1ad47SJeff Kirsher  **/
e1000e_update_stats(struct e1000_adapter * adapter)4926dee1ad47SJeff Kirsher static void e1000e_update_stats(struct e1000_adapter *adapter)
4927dee1ad47SJeff Kirsher {
4928dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
4929dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
4930dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
4931dee1ad47SJeff Kirsher 
4932e921eb1aSBruce Allan 	/* Prevent stats update while adapter is being reset, or if the pci
4933dee1ad47SJeff Kirsher 	 * connection is down.
4934dee1ad47SJeff Kirsher 	 */
4935dee1ad47SJeff Kirsher 	if (adapter->link_speed == 0)
4936dee1ad47SJeff Kirsher 		return;
4937dee1ad47SJeff Kirsher 	if (pci_channel_offline(pdev))
4938dee1ad47SJeff Kirsher 		return;
4939dee1ad47SJeff Kirsher 
4940dee1ad47SJeff Kirsher 	adapter->stats.crcerrs += er32(CRCERRS);
4941dee1ad47SJeff Kirsher 	adapter->stats.gprc += er32(GPRC);
4942dee1ad47SJeff Kirsher 	adapter->stats.gorc += er32(GORCL);
4943dee1ad47SJeff Kirsher 	er32(GORCH);		/* Clear gorc */
4944dee1ad47SJeff Kirsher 	adapter->stats.bprc += er32(BPRC);
4945dee1ad47SJeff Kirsher 	adapter->stats.mprc += er32(MPRC);
4946dee1ad47SJeff Kirsher 	adapter->stats.roc += er32(ROC);
4947dee1ad47SJeff Kirsher 
4948dee1ad47SJeff Kirsher 	adapter->stats.mpc += er32(MPC);
4949dee1ad47SJeff Kirsher 
4950dee1ad47SJeff Kirsher 	/* Half-duplex statistics */
4951dee1ad47SJeff Kirsher 	if (adapter->link_duplex == HALF_DUPLEX) {
4952dee1ad47SJeff Kirsher 		if (adapter->flags2 & FLAG2_HAS_PHY_STATS) {
4953dee1ad47SJeff Kirsher 			e1000e_update_phy_stats(adapter);
4954dee1ad47SJeff Kirsher 		} else {
4955dee1ad47SJeff Kirsher 			adapter->stats.scc += er32(SCC);
4956dee1ad47SJeff Kirsher 			adapter->stats.ecol += er32(ECOL);
4957dee1ad47SJeff Kirsher 			adapter->stats.mcc += er32(MCC);
4958dee1ad47SJeff Kirsher 			adapter->stats.latecol += er32(LATECOL);
4959dee1ad47SJeff Kirsher 			adapter->stats.dc += er32(DC);
4960dee1ad47SJeff Kirsher 
4961dee1ad47SJeff Kirsher 			hw->mac.collision_delta = er32(COLC);
4962dee1ad47SJeff Kirsher 
4963dee1ad47SJeff Kirsher 			if ((hw->mac.type != e1000_82574) &&
4964dee1ad47SJeff Kirsher 			    (hw->mac.type != e1000_82583))
4965dee1ad47SJeff Kirsher 				adapter->stats.tncrs += er32(TNCRS);
4966dee1ad47SJeff Kirsher 		}
4967dee1ad47SJeff Kirsher 		adapter->stats.colc += hw->mac.collision_delta;
4968dee1ad47SJeff Kirsher 	}
4969dee1ad47SJeff Kirsher 
4970dee1ad47SJeff Kirsher 	adapter->stats.xonrxc += er32(XONRXC);
4971dee1ad47SJeff Kirsher 	adapter->stats.xontxc += er32(XONTXC);
4972dee1ad47SJeff Kirsher 	adapter->stats.xoffrxc += er32(XOFFRXC);
4973dee1ad47SJeff Kirsher 	adapter->stats.xofftxc += er32(XOFFTXC);
4974dee1ad47SJeff Kirsher 	adapter->stats.gptc += er32(GPTC);
4975dee1ad47SJeff Kirsher 	adapter->stats.gotc += er32(GOTCL);
4976dee1ad47SJeff Kirsher 	er32(GOTCH);		/* Clear gotc */
4977dee1ad47SJeff Kirsher 	adapter->stats.rnbc += er32(RNBC);
4978dee1ad47SJeff Kirsher 	adapter->stats.ruc += er32(RUC);
4979dee1ad47SJeff Kirsher 
4980dee1ad47SJeff Kirsher 	adapter->stats.mptc += er32(MPTC);
4981dee1ad47SJeff Kirsher 	adapter->stats.bptc += er32(BPTC);
4982dee1ad47SJeff Kirsher 
4983dee1ad47SJeff Kirsher 	/* used for adaptive IFS */
4984dee1ad47SJeff Kirsher 
4985dee1ad47SJeff Kirsher 	hw->mac.tx_packet_delta = er32(TPT);
4986dee1ad47SJeff Kirsher 	adapter->stats.tpt += hw->mac.tx_packet_delta;
4987dee1ad47SJeff Kirsher 
4988dee1ad47SJeff Kirsher 	adapter->stats.algnerrc += er32(ALGNERRC);
4989dee1ad47SJeff Kirsher 	adapter->stats.rxerrc += er32(RXERRC);
4990dee1ad47SJeff Kirsher 	adapter->stats.cexterr += er32(CEXTERR);
4991dee1ad47SJeff Kirsher 	adapter->stats.tsctc += er32(TSCTC);
4992dee1ad47SJeff Kirsher 	adapter->stats.tsctfc += er32(TSCTFC);
4993dee1ad47SJeff Kirsher 
4994dee1ad47SJeff Kirsher 	/* Fill out the OS statistics structure */
4995dee1ad47SJeff Kirsher 	netdev->stats.multicast = adapter->stats.mprc;
4996dee1ad47SJeff Kirsher 	netdev->stats.collisions = adapter->stats.colc;
4997dee1ad47SJeff Kirsher 
4998dee1ad47SJeff Kirsher 	/* Rx Errors */
4999dee1ad47SJeff Kirsher 
5000e921eb1aSBruce Allan 	/* RLEC on some newer hardware can be incorrect so build
5001dee1ad47SJeff Kirsher 	 * our own version based on RUC and ROC
5002dee1ad47SJeff Kirsher 	 */
5003dee1ad47SJeff Kirsher 	netdev->stats.rx_errors = adapter->stats.rxerrc +
5004dee1ad47SJeff Kirsher 	    adapter->stats.crcerrs + adapter->stats.algnerrc +
5005f0ff4398SBruce Allan 	    adapter->stats.ruc + adapter->stats.roc + adapter->stats.cexterr;
5006dee1ad47SJeff Kirsher 	netdev->stats.rx_length_errors = adapter->stats.ruc +
5007dee1ad47SJeff Kirsher 	    adapter->stats.roc;
5008dee1ad47SJeff Kirsher 	netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
5009dee1ad47SJeff Kirsher 	netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
5010dee1ad47SJeff Kirsher 	netdev->stats.rx_missed_errors = adapter->stats.mpc;
5011dee1ad47SJeff Kirsher 
5012dee1ad47SJeff Kirsher 	/* Tx Errors */
5013f0ff4398SBruce Allan 	netdev->stats.tx_errors = adapter->stats.ecol + adapter->stats.latecol;
5014dee1ad47SJeff Kirsher 	netdev->stats.tx_aborted_errors = adapter->stats.ecol;
5015dee1ad47SJeff Kirsher 	netdev->stats.tx_window_errors = adapter->stats.latecol;
5016dee1ad47SJeff Kirsher 	netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
5017dee1ad47SJeff Kirsher 
5018dee1ad47SJeff Kirsher 	/* Tx Dropped needs to be maintained elsewhere */
5019dee1ad47SJeff Kirsher 
5020dee1ad47SJeff Kirsher 	/* Management Stats */
5021dee1ad47SJeff Kirsher 	adapter->stats.mgptc += er32(MGTPTC);
5022dee1ad47SJeff Kirsher 	adapter->stats.mgprc += er32(MGTPRC);
5023dee1ad47SJeff Kirsher 	adapter->stats.mgpdc += er32(MGTPDC);
502494fb848bSBruce Allan 
502594fb848bSBruce Allan 	/* Correctable ECC Errors */
5026c8744f44SSasha Neftin 	if (hw->mac.type >= e1000_pch_lpt) {
502794fb848bSBruce Allan 		u32 pbeccsts = er32(PBECCSTS);
50286cf08d1cSDavid Ertman 
502994fb848bSBruce Allan 		adapter->corr_errors +=
503094fb848bSBruce Allan 		    pbeccsts & E1000_PBECCSTS_CORR_ERR_CNT_MASK;
503194fb848bSBruce Allan 		adapter->uncorr_errors +=
5032b9a45254SJesse Brandeburg 		    FIELD_GET(E1000_PBECCSTS_UNCORR_ERR_CNT_MASK, pbeccsts);
503394fb848bSBruce Allan 	}
5034dee1ad47SJeff Kirsher }
5035dee1ad47SJeff Kirsher 
5036dee1ad47SJeff Kirsher /**
5037dee1ad47SJeff Kirsher  * e1000_phy_read_status - Update the PHY register status snapshot
5038dee1ad47SJeff Kirsher  * @adapter: board private structure
5039dee1ad47SJeff Kirsher  **/
e1000_phy_read_status(struct e1000_adapter * adapter)5040dee1ad47SJeff Kirsher static void e1000_phy_read_status(struct e1000_adapter *adapter)
5041dee1ad47SJeff Kirsher {
5042dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
5043dee1ad47SJeff Kirsher 	struct e1000_phy_regs *phy = &adapter->phy_regs;
5044dee1ad47SJeff Kirsher 
504597390ab8SBruce Allan 	if (!pm_runtime_suspended((&adapter->pdev->dev)->parent) &&
504697390ab8SBruce Allan 	    (er32(STATUS) & E1000_STATUS_LU) &&
5047dee1ad47SJeff Kirsher 	    (adapter->hw.phy.media_type == e1000_media_type_copper)) {
5048dee1ad47SJeff Kirsher 		int ret_val;
5049dee1ad47SJeff Kirsher 
5050c2ade1a4SBruce Allan 		ret_val = e1e_rphy(hw, MII_BMCR, &phy->bmcr);
5051c2ade1a4SBruce Allan 		ret_val |= e1e_rphy(hw, MII_BMSR, &phy->bmsr);
5052c2ade1a4SBruce Allan 		ret_val |= e1e_rphy(hw, MII_ADVERTISE, &phy->advertise);
5053c2ade1a4SBruce Allan 		ret_val |= e1e_rphy(hw, MII_LPA, &phy->lpa);
5054c2ade1a4SBruce Allan 		ret_val |= e1e_rphy(hw, MII_EXPANSION, &phy->expansion);
5055c2ade1a4SBruce Allan 		ret_val |= e1e_rphy(hw, MII_CTRL1000, &phy->ctrl1000);
5056c2ade1a4SBruce Allan 		ret_val |= e1e_rphy(hw, MII_STAT1000, &phy->stat1000);
5057c2ade1a4SBruce Allan 		ret_val |= e1e_rphy(hw, MII_ESTATUS, &phy->estatus);
5058dee1ad47SJeff Kirsher 		if (ret_val)
5059dee1ad47SJeff Kirsher 			e_warn("Error reading PHY register\n");
5060dee1ad47SJeff Kirsher 	} else {
5061e921eb1aSBruce Allan 		/* Do not read PHY registers if link is not up
5062dee1ad47SJeff Kirsher 		 * Set values to typical power-on defaults
5063dee1ad47SJeff Kirsher 		 */
5064dee1ad47SJeff Kirsher 		phy->bmcr = (BMCR_SPEED1000 | BMCR_ANENABLE | BMCR_FULLDPLX);
5065dee1ad47SJeff Kirsher 		phy->bmsr = (BMSR_100FULL | BMSR_100HALF | BMSR_10FULL |
5066dee1ad47SJeff Kirsher 			     BMSR_10HALF | BMSR_ESTATEN | BMSR_ANEGCAPABLE |
5067dee1ad47SJeff Kirsher 			     BMSR_ERCAP);
5068dee1ad47SJeff Kirsher 		phy->advertise = (ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP |
5069dee1ad47SJeff Kirsher 				  ADVERTISE_ALL | ADVERTISE_CSMA);
5070dee1ad47SJeff Kirsher 		phy->lpa = 0;
5071dee1ad47SJeff Kirsher 		phy->expansion = EXPANSION_ENABLENPAGE;
5072dee1ad47SJeff Kirsher 		phy->ctrl1000 = ADVERTISE_1000FULL;
5073dee1ad47SJeff Kirsher 		phy->stat1000 = 0;
5074dee1ad47SJeff Kirsher 		phy->estatus = (ESTATUS_1000_TFULL | ESTATUS_1000_THALF);
5075dee1ad47SJeff Kirsher 	}
5076dee1ad47SJeff Kirsher }
5077dee1ad47SJeff Kirsher 
e1000_print_link_info(struct e1000_adapter * adapter)5078dee1ad47SJeff Kirsher static void e1000_print_link_info(struct e1000_adapter *adapter)
5079dee1ad47SJeff Kirsher {
5080dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
5081dee1ad47SJeff Kirsher 	u32 ctrl = er32(CTRL);
5082dee1ad47SJeff Kirsher 
5083dee1ad47SJeff Kirsher 	/* Link status message must follow this format for user tools */
5084c557a4b3SAlexander Duyck 	netdev_info(adapter->netdev,
5085c557a4b3SAlexander Duyck 		    "NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
5086c557a4b3SAlexander Duyck 		    adapter->link_speed,
5087ef456f85SJeff Kirsher 		    adapter->link_duplex == FULL_DUPLEX ? "Full" : "Half",
5088ef456f85SJeff Kirsher 		    (ctrl & E1000_CTRL_TFCE) && (ctrl & E1000_CTRL_RFCE) ? "Rx/Tx" :
5089ef456f85SJeff Kirsher 		    (ctrl & E1000_CTRL_RFCE) ? "Rx" :
5090ef456f85SJeff Kirsher 		    (ctrl & E1000_CTRL_TFCE) ? "Tx" : "None");
5091dee1ad47SJeff Kirsher }
5092dee1ad47SJeff Kirsher 
e1000e_has_link(struct e1000_adapter * adapter)5093dee1ad47SJeff Kirsher static bool e1000e_has_link(struct e1000_adapter *adapter)
5094dee1ad47SJeff Kirsher {
5095dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
50963db1cd5cSRusty Russell 	bool link_active = false;
5097dee1ad47SJeff Kirsher 	s32 ret_val = 0;
5098dee1ad47SJeff Kirsher 
5099e921eb1aSBruce Allan 	/* get_link_status is set on LSC (link status) interrupt or
5100dee1ad47SJeff Kirsher 	 * Rx sequence error interrupt.  get_link_status will stay
510165a29da1SBenjamin Poirier 	 * true until the check_for_link establishes link
5102dee1ad47SJeff Kirsher 	 * for copper adapters ONLY
5103dee1ad47SJeff Kirsher 	 */
5104dee1ad47SJeff Kirsher 	switch (hw->phy.media_type) {
5105dee1ad47SJeff Kirsher 	case e1000_media_type_copper:
5106dee1ad47SJeff Kirsher 		if (hw->mac.get_link_status) {
5107dee1ad47SJeff Kirsher 			ret_val = hw->mac.ops.check_for_link(hw);
51083016e0a0SBenjamin Poirier 			link_active = !hw->mac.get_link_status;
5109dee1ad47SJeff Kirsher 		} else {
51103db1cd5cSRusty Russell 			link_active = true;
5111dee1ad47SJeff Kirsher 		}
5112dee1ad47SJeff Kirsher 		break;
5113dee1ad47SJeff Kirsher 	case e1000_media_type_fiber:
5114dee1ad47SJeff Kirsher 		ret_val = hw->mac.ops.check_for_link(hw);
5115dee1ad47SJeff Kirsher 		link_active = !!(er32(STATUS) & E1000_STATUS_LU);
5116dee1ad47SJeff Kirsher 		break;
5117dee1ad47SJeff Kirsher 	case e1000_media_type_internal_serdes:
5118dee1ad47SJeff Kirsher 		ret_val = hw->mac.ops.check_for_link(hw);
511965a29da1SBenjamin Poirier 		link_active = hw->mac.serdes_has_link;
5120dee1ad47SJeff Kirsher 		break;
5121dee1ad47SJeff Kirsher 	default:
5122dee1ad47SJeff Kirsher 	case e1000_media_type_unknown:
5123dee1ad47SJeff Kirsher 		break;
5124dee1ad47SJeff Kirsher 	}
5125dee1ad47SJeff Kirsher 
5126d3509f8bSBenjamin Poirier 	if ((ret_val == -E1000_ERR_PHY) && (hw->phy.type == e1000_phy_igp_3) &&
5127dee1ad47SJeff Kirsher 	    (er32(CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
5128dee1ad47SJeff Kirsher 		/* See e1000_kmrn_lock_loss_workaround_ich8lan() */
5129dee1ad47SJeff Kirsher 		e_info("Gigabit has been disabled, downgrading speed\n");
5130dee1ad47SJeff Kirsher 	}
5131dee1ad47SJeff Kirsher 
5132dee1ad47SJeff Kirsher 	return link_active;
5133dee1ad47SJeff Kirsher }
5134dee1ad47SJeff Kirsher 
e1000e_enable_receives(struct e1000_adapter * adapter)5135dee1ad47SJeff Kirsher static void e1000e_enable_receives(struct e1000_adapter *adapter)
5136dee1ad47SJeff Kirsher {
5137dee1ad47SJeff Kirsher 	/* make sure the receive unit is started */
5138dee1ad47SJeff Kirsher 	if ((adapter->flags & FLAG_RX_NEEDS_RESTART) &&
513912d43f7dSBruce Allan 	    (adapter->flags & FLAG_RESTART_NOW)) {
5140dee1ad47SJeff Kirsher 		struct e1000_hw *hw = &adapter->hw;
5141dee1ad47SJeff Kirsher 		u32 rctl = er32(RCTL);
51426cf08d1cSDavid Ertman 
5143dee1ad47SJeff Kirsher 		ew32(RCTL, rctl | E1000_RCTL_EN);
514412d43f7dSBruce Allan 		adapter->flags &= ~FLAG_RESTART_NOW;
5145dee1ad47SJeff Kirsher 	}
5146dee1ad47SJeff Kirsher }
5147dee1ad47SJeff Kirsher 
e1000e_check_82574_phy_workaround(struct e1000_adapter * adapter)5148dee1ad47SJeff Kirsher static void e1000e_check_82574_phy_workaround(struct e1000_adapter *adapter)
5149dee1ad47SJeff Kirsher {
5150dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
5151dee1ad47SJeff Kirsher 
5152e921eb1aSBruce Allan 	/* With 82574 controllers, PHY needs to be checked periodically
5153dee1ad47SJeff Kirsher 	 * for hung state and reset, if two calls return true
5154dee1ad47SJeff Kirsher 	 */
5155dee1ad47SJeff Kirsher 	if (e1000_check_phy_82574(hw))
5156dee1ad47SJeff Kirsher 		adapter->phy_hang_count++;
5157dee1ad47SJeff Kirsher 	else
5158dee1ad47SJeff Kirsher 		adapter->phy_hang_count = 0;
5159dee1ad47SJeff Kirsher 
5160dee1ad47SJeff Kirsher 	if (adapter->phy_hang_count > 1) {
5161dee1ad47SJeff Kirsher 		adapter->phy_hang_count = 0;
5162d9554e96SDavid Ertman 		e_dbg("PHY appears hung - resetting\n");
5163dee1ad47SJeff Kirsher 		schedule_work(&adapter->reset_task);
5164dee1ad47SJeff Kirsher 	}
5165dee1ad47SJeff Kirsher }
5166dee1ad47SJeff Kirsher 
5167d5ad7a6aSJeff Kirsher /**
5168d5ad7a6aSJeff Kirsher  * e1000_watchdog - Timer Call-back
5169b50f7bcaSJesse Brandeburg  * @t: pointer to timer_list containing private info adapter
5170d5ad7a6aSJeff Kirsher  **/
e1000_watchdog(struct timer_list * t)5171d5ad7a6aSJeff Kirsher static void e1000_watchdog(struct timer_list *t)
5172d5ad7a6aSJeff Kirsher {
5173d5ad7a6aSJeff Kirsher 	struct e1000_adapter *adapter = from_timer(adapter, t, watchdog_timer);
5174d5ad7a6aSJeff Kirsher 
5175d5ad7a6aSJeff Kirsher 	/* Do the rest outside of interrupt context */
5176d5ad7a6aSJeff Kirsher 	schedule_work(&adapter->watchdog_task);
5177d5ad7a6aSJeff Kirsher 
5178d5ad7a6aSJeff Kirsher 	/* TODO: make this use queue_delayed_work() */
5179d5ad7a6aSJeff Kirsher }
5180d5ad7a6aSJeff Kirsher 
e1000_watchdog_task(struct work_struct * work)5181dee1ad47SJeff Kirsher static void e1000_watchdog_task(struct work_struct *work)
5182dee1ad47SJeff Kirsher {
5183dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = container_of(work,
518417e813ecSBruce Allan 						     struct e1000_adapter,
5185d5ad7a6aSJeff Kirsher 						     watchdog_task);
5186dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
5187dee1ad47SJeff Kirsher 	struct e1000_mac_info *mac = &adapter->hw.mac;
5188dee1ad47SJeff Kirsher 	struct e1000_phy_info *phy = &adapter->hw.phy;
5189dee1ad47SJeff Kirsher 	struct e1000_ring *tx_ring = adapter->tx_ring;
5190def4ec6dSVitaly Lifshits 	u32 dmoff_exit_timeout = 100, tries = 0;
5191dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
5192def4ec6dSVitaly Lifshits 	u32 link, tctl, pcim_state;
5193dee1ad47SJeff Kirsher 
5194dee1ad47SJeff Kirsher 	if (test_bit(__E1000_DOWN, &adapter->state))
5195dee1ad47SJeff Kirsher 		return;
5196dee1ad47SJeff Kirsher 
5197dee1ad47SJeff Kirsher 	link = e1000e_has_link(adapter);
5198dee1ad47SJeff Kirsher 	if ((netif_carrier_ok(netdev)) && link) {
5199dee1ad47SJeff Kirsher 		/* Cancel scheduled suspend requests. */
5200dee1ad47SJeff Kirsher 		pm_runtime_resume(netdev->dev.parent);
5201dee1ad47SJeff Kirsher 
5202dee1ad47SJeff Kirsher 		e1000e_enable_receives(adapter);
5203dee1ad47SJeff Kirsher 		goto link_up;
5204dee1ad47SJeff Kirsher 	}
5205dee1ad47SJeff Kirsher 
5206dee1ad47SJeff Kirsher 	if ((e1000e_enable_tx_pkt_filtering(hw)) &&
5207dee1ad47SJeff Kirsher 	    (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id))
5208dee1ad47SJeff Kirsher 		e1000_update_mng_vlan(adapter);
5209dee1ad47SJeff Kirsher 
5210dee1ad47SJeff Kirsher 	if (link) {
5211dee1ad47SJeff Kirsher 		if (!netif_carrier_ok(netdev)) {
52123db1cd5cSRusty Russell 			bool txb2b = true;
5213dee1ad47SJeff Kirsher 
5214dee1ad47SJeff Kirsher 			/* Cancel scheduled suspend requests. */
5215dee1ad47SJeff Kirsher 			pm_runtime_resume(netdev->dev.parent);
5216dee1ad47SJeff Kirsher 
5217def4ec6dSVitaly Lifshits 			/* Checking if MAC is in DMoff state*/
52182e7256f1SSasha Neftin 			if (er32(FWSM) & E1000_ICH_FWSM_FW_VALID) {
5219def4ec6dSVitaly Lifshits 				pcim_state = er32(STATUS);
5220def4ec6dSVitaly Lifshits 				while (pcim_state & E1000_STATUS_PCIM_STATE) {
5221def4ec6dSVitaly Lifshits 					if (tries++ == dmoff_exit_timeout) {
5222def4ec6dSVitaly Lifshits 						e_dbg("Error in exiting dmoff\n");
5223def4ec6dSVitaly Lifshits 						break;
5224def4ec6dSVitaly Lifshits 					}
5225def4ec6dSVitaly Lifshits 					usleep_range(10000, 20000);
5226def4ec6dSVitaly Lifshits 					pcim_state = er32(STATUS);
5227def4ec6dSVitaly Lifshits 
5228def4ec6dSVitaly Lifshits 					/* Checking if MAC exited DMoff state */
5229def4ec6dSVitaly Lifshits 					if (!(pcim_state & E1000_STATUS_PCIM_STATE))
5230def4ec6dSVitaly Lifshits 						e1000_phy_hw_reset(&adapter->hw);
5231def4ec6dSVitaly Lifshits 				}
52322e7256f1SSasha Neftin 			}
5233def4ec6dSVitaly Lifshits 
5234dee1ad47SJeff Kirsher 			/* update snapshot of PHY registers on LSC */
5235dee1ad47SJeff Kirsher 			e1000_phy_read_status(adapter);
5236dee1ad47SJeff Kirsher 			mac->ops.get_link_up_info(&adapter->hw,
5237dee1ad47SJeff Kirsher 						  &adapter->link_speed,
5238dee1ad47SJeff Kirsher 						  &adapter->link_duplex);
5239dee1ad47SJeff Kirsher 			e1000_print_link_info(adapter);
5240e792cd91SKoki Sanagi 
5241e792cd91SKoki Sanagi 			/* check if SmartSpeed worked */
5242e792cd91SKoki Sanagi 			e1000e_check_downshift(hw);
5243e792cd91SKoki Sanagi 			if (phy->speed_downgraded)
5244e792cd91SKoki Sanagi 				netdev_warn(netdev,
5245e792cd91SKoki Sanagi 					    "Link Speed was downgraded by SmartSpeed\n");
5246e792cd91SKoki Sanagi 
5247e921eb1aSBruce Allan 			/* On supported PHYs, check for duplex mismatch only
5248dee1ad47SJeff Kirsher 			 * if link has autonegotiated at 10/100 half
5249dee1ad47SJeff Kirsher 			 */
5250dee1ad47SJeff Kirsher 			if ((hw->phy.type == e1000_phy_igp_3 ||
5251dee1ad47SJeff Kirsher 			     hw->phy.type == e1000_phy_bm) &&
5252138953bbSDavid Ertman 			    hw->mac.autoneg &&
5253dee1ad47SJeff Kirsher 			    (adapter->link_speed == SPEED_10 ||
5254dee1ad47SJeff Kirsher 			     adapter->link_speed == SPEED_100) &&
5255dee1ad47SJeff Kirsher 			    (adapter->link_duplex == HALF_DUPLEX)) {
5256dee1ad47SJeff Kirsher 				u16 autoneg_exp;
5257dee1ad47SJeff Kirsher 
5258c2ade1a4SBruce Allan 				e1e_rphy(hw, MII_EXPANSION, &autoneg_exp);
5259dee1ad47SJeff Kirsher 
5260c2ade1a4SBruce Allan 				if (!(autoneg_exp & EXPANSION_NWAY))
5261ef456f85SJeff Kirsher 					e_info("Autonegotiated half duplex but link partner cannot autoneg.  Try forcing full duplex if link gets many collisions.\n");
5262dee1ad47SJeff Kirsher 			}
5263dee1ad47SJeff Kirsher 
5264dee1ad47SJeff Kirsher 			/* adjust timeout factor according to speed/duplex */
5265dee1ad47SJeff Kirsher 			adapter->tx_timeout_factor = 1;
5266dee1ad47SJeff Kirsher 			switch (adapter->link_speed) {
5267dee1ad47SJeff Kirsher 			case SPEED_10:
52683db1cd5cSRusty Russell 				txb2b = false;
5269dee1ad47SJeff Kirsher 				adapter->tx_timeout_factor = 16;
5270dee1ad47SJeff Kirsher 				break;
5271dee1ad47SJeff Kirsher 			case SPEED_100:
52723db1cd5cSRusty Russell 				txb2b = false;
5273dee1ad47SJeff Kirsher 				adapter->tx_timeout_factor = 10;
5274dee1ad47SJeff Kirsher 				break;
5275dee1ad47SJeff Kirsher 			}
5276dee1ad47SJeff Kirsher 
5277e921eb1aSBruce Allan 			/* workaround: re-program speed mode bit after
5278dee1ad47SJeff Kirsher 			 * link-up event
5279dee1ad47SJeff Kirsher 			 */
5280dee1ad47SJeff Kirsher 			if ((adapter->flags & FLAG_TARC_SPEED_MODE_BIT) &&
5281dee1ad47SJeff Kirsher 			    !txb2b) {
5282dee1ad47SJeff Kirsher 				u32 tarc0;
52836cf08d1cSDavid Ertman 
5284dee1ad47SJeff Kirsher 				tarc0 = er32(TARC(0));
5285dee1ad47SJeff Kirsher 				tarc0 &= ~SPEED_MODE_BIT;
5286dee1ad47SJeff Kirsher 				ew32(TARC(0), tarc0);
5287dee1ad47SJeff Kirsher 			}
5288dee1ad47SJeff Kirsher 
5289e921eb1aSBruce Allan 			/* enable transmits in the hardware, need to do this
5290dee1ad47SJeff Kirsher 			 * after setting TARC(0)
5291dee1ad47SJeff Kirsher 			 */
5292dee1ad47SJeff Kirsher 			tctl = er32(TCTL);
5293dee1ad47SJeff Kirsher 			tctl |= E1000_TCTL_EN;
5294dee1ad47SJeff Kirsher 			ew32(TCTL, tctl);
5295dee1ad47SJeff Kirsher 
5296e921eb1aSBruce Allan 			/* Perform any post-link-up configuration before
5297dee1ad47SJeff Kirsher 			 * reporting link up.
5298dee1ad47SJeff Kirsher 			 */
5299dee1ad47SJeff Kirsher 			if (phy->ops.cfg_on_link_up)
5300dee1ad47SJeff Kirsher 				phy->ops.cfg_on_link_up(hw);
5301dee1ad47SJeff Kirsher 
5302d17ba0f6SKonstantin Khlebnikov 			netif_wake_queue(netdev);
5303dee1ad47SJeff Kirsher 			netif_carrier_on(netdev);
5304dee1ad47SJeff Kirsher 
5305dee1ad47SJeff Kirsher 			if (!test_bit(__E1000_DOWN, &adapter->state))
5306dee1ad47SJeff Kirsher 				mod_timer(&adapter->phy_info_timer,
5307dee1ad47SJeff Kirsher 					  round_jiffies(jiffies + 2 * HZ));
5308dee1ad47SJeff Kirsher 		}
5309dee1ad47SJeff Kirsher 	} else {
5310dee1ad47SJeff Kirsher 		if (netif_carrier_ok(netdev)) {
5311dee1ad47SJeff Kirsher 			adapter->link_speed = 0;
5312dee1ad47SJeff Kirsher 			adapter->link_duplex = 0;
5313dee1ad47SJeff Kirsher 			/* Link status message must follow this format */
5314c557a4b3SAlexander Duyck 			netdev_info(netdev, "NIC Link is Down\n");
5315dee1ad47SJeff Kirsher 			netif_carrier_off(netdev);
5316d17ba0f6SKonstantin Khlebnikov 			netif_stop_queue(netdev);
5317dee1ad47SJeff Kirsher 			if (!test_bit(__E1000_DOWN, &adapter->state))
5318dee1ad47SJeff Kirsher 				mod_timer(&adapter->phy_info_timer,
5319dee1ad47SJeff Kirsher 					  round_jiffies(jiffies + 2 * HZ));
5320dee1ad47SJeff Kirsher 
5321d9554e96SDavid Ertman 			/* 8000ES2LAN requires a Rx packet buffer work-around
5322d9554e96SDavid Ertman 			 * on link down event; reset the controller to flush
5323d9554e96SDavid Ertman 			 * the Rx packet buffer.
532412d43f7dSBruce Allan 			 */
5325caff422eSKonstantin Khlebnikov 			if (adapter->flags & FLAG_RX_NEEDS_RESTART)
532612d43f7dSBruce Allan 				adapter->flags |= FLAG_RESTART_NOW;
5327dee1ad47SJeff Kirsher 			else
5328dee1ad47SJeff Kirsher 				pm_schedule_suspend(netdev->dev.parent,
5329dee1ad47SJeff Kirsher 						    LINK_TIMEOUT);
5330dee1ad47SJeff Kirsher 		}
5331dee1ad47SJeff Kirsher 	}
5332dee1ad47SJeff Kirsher 
5333dee1ad47SJeff Kirsher link_up:
5334dee1ad47SJeff Kirsher 	spin_lock(&adapter->stats64_lock);
5335dee1ad47SJeff Kirsher 	e1000e_update_stats(adapter);
5336dee1ad47SJeff Kirsher 
5337dee1ad47SJeff Kirsher 	mac->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
5338dee1ad47SJeff Kirsher 	adapter->tpt_old = adapter->stats.tpt;
5339dee1ad47SJeff Kirsher 	mac->collision_delta = adapter->stats.colc - adapter->colc_old;
5340dee1ad47SJeff Kirsher 	adapter->colc_old = adapter->stats.colc;
5341dee1ad47SJeff Kirsher 
5342dee1ad47SJeff Kirsher 	adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
5343dee1ad47SJeff Kirsher 	adapter->gorc_old = adapter->stats.gorc;
5344dee1ad47SJeff Kirsher 	adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
5345dee1ad47SJeff Kirsher 	adapter->gotc_old = adapter->stats.gotc;
5346dee1ad47SJeff Kirsher 	spin_unlock(&adapter->stats64_lock);
5347dee1ad47SJeff Kirsher 
5348caff422eSKonstantin Khlebnikov 	/* If the link is lost the controller stops DMA, but
5349caff422eSKonstantin Khlebnikov 	 * if there is queued Tx work it cannot be done.  So
5350caff422eSKonstantin Khlebnikov 	 * reset the controller to flush the Tx packet buffers.
5351caff422eSKonstantin Khlebnikov 	 */
5352caff422eSKonstantin Khlebnikov 	if (!netif_carrier_ok(netdev) &&
5353caff422eSKonstantin Khlebnikov 	    (e1000_desc_unused(tx_ring) + 1 < tx_ring->count))
5354caff422eSKonstantin Khlebnikov 		adapter->flags |= FLAG_RESTART_NOW;
5355caff422eSKonstantin Khlebnikov 
5356d9554e96SDavid Ertman 	/* If reset is necessary, do it outside of interrupt context. */
535712d43f7dSBruce Allan 	if (adapter->flags & FLAG_RESTART_NOW) {
5358dee1ad47SJeff Kirsher 		schedule_work(&adapter->reset_task);
5359dee1ad47SJeff Kirsher 		/* return immediately since reset is imminent */
5360dee1ad47SJeff Kirsher 		return;
5361dee1ad47SJeff Kirsher 	}
5362dee1ad47SJeff Kirsher 
536312d43f7dSBruce Allan 	e1000e_update_adaptive(&adapter->hw);
536412d43f7dSBruce Allan 
5365dee1ad47SJeff Kirsher 	/* Simple mode for Interrupt Throttle Rate (ITR) */
5366dee1ad47SJeff Kirsher 	if (adapter->itr_setting == 4) {
5367e921eb1aSBruce Allan 		/* Symmetric Tx/Rx gets a reduced ITR=2000;
5368dee1ad47SJeff Kirsher 		 * Total asymmetrical Tx or Rx gets ITR=8000;
5369dee1ad47SJeff Kirsher 		 * everyone else is between 2000-8000.
5370dee1ad47SJeff Kirsher 		 */
5371dee1ad47SJeff Kirsher 		u32 goc = (adapter->gotc + adapter->gorc) / 10000;
5372dee1ad47SJeff Kirsher 		u32 dif = (adapter->gotc > adapter->gorc ?
5373dee1ad47SJeff Kirsher 			   adapter->gotc - adapter->gorc :
5374dee1ad47SJeff Kirsher 			   adapter->gorc - adapter->gotc) / 10000;
5375dee1ad47SJeff Kirsher 		u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
5376dee1ad47SJeff Kirsher 
537722a4cca2SMatthew Vick 		e1000e_write_itr(adapter, itr);
5378dee1ad47SJeff Kirsher 	}
5379dee1ad47SJeff Kirsher 
5380dee1ad47SJeff Kirsher 	/* Cause software interrupt to ensure Rx ring is cleaned */
5381dee1ad47SJeff Kirsher 	if (adapter->msix_entries)
5382dee1ad47SJeff Kirsher 		ew32(ICS, adapter->rx_ring->ims_val);
5383dee1ad47SJeff Kirsher 	else
5384dee1ad47SJeff Kirsher 		ew32(ICS, E1000_ICS_RXDMT0);
5385dee1ad47SJeff Kirsher 
5386dee1ad47SJeff Kirsher 	/* flush pending descriptors to memory before detecting Tx hang */
5387dee1ad47SJeff Kirsher 	e1000e_flush_descriptors(adapter);
5388dee1ad47SJeff Kirsher 
5389dee1ad47SJeff Kirsher 	/* Force detection of hung controller every watchdog period */
53903db1cd5cSRusty Russell 	adapter->detect_tx_hung = true;
5391dee1ad47SJeff Kirsher 
5392e921eb1aSBruce Allan 	/* With 82571 controllers, LAA may be overwritten due to controller
5393dee1ad47SJeff Kirsher 	 * reset from the other port. Set the appropriate LAA in RAR[0]
5394dee1ad47SJeff Kirsher 	 */
5395dee1ad47SJeff Kirsher 	if (e1000e_get_laa_state_82571(hw))
539669e1e019SBruce Allan 		hw->mac.ops.rar_set(hw, adapter->hw.mac.addr, 0);
5397dee1ad47SJeff Kirsher 
5398dee1ad47SJeff Kirsher 	if (adapter->flags2 & FLAG2_CHECK_PHY_HANG)
5399dee1ad47SJeff Kirsher 		e1000e_check_82574_phy_workaround(adapter);
5400dee1ad47SJeff Kirsher 
5401b67e1913SBruce Allan 	/* Clear valid timestamp stuck in RXSTMPL/H due to a Rx error */
5402b67e1913SBruce Allan 	if (adapter->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE) {
5403b67e1913SBruce Allan 		if ((adapter->flags2 & FLAG2_CHECK_RX_HWTSTAMP) &&
5404b67e1913SBruce Allan 		    (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID)) {
5405b67e1913SBruce Allan 			er32(RXSTMPH);
5406b67e1913SBruce Allan 			adapter->rx_hwtstamp_cleared++;
5407b67e1913SBruce Allan 		} else {
5408b67e1913SBruce Allan 			adapter->flags2 |= FLAG2_CHECK_RX_HWTSTAMP;
5409b67e1913SBruce Allan 		}
5410b67e1913SBruce Allan 	}
5411b67e1913SBruce Allan 
5412dee1ad47SJeff Kirsher 	/* Reset the timer */
5413dee1ad47SJeff Kirsher 	if (!test_bit(__E1000_DOWN, &adapter->state))
5414d5ad7a6aSJeff Kirsher 		mod_timer(&adapter->watchdog_timer,
5415d5ad7a6aSJeff Kirsher 			  round_jiffies(jiffies + 2 * HZ));
5416dee1ad47SJeff Kirsher }
5417dee1ad47SJeff Kirsher 
5418dee1ad47SJeff Kirsher #define E1000_TX_FLAGS_CSUM		0x00000001
5419dee1ad47SJeff Kirsher #define E1000_TX_FLAGS_VLAN		0x00000002
5420dee1ad47SJeff Kirsher #define E1000_TX_FLAGS_TSO		0x00000004
5421dee1ad47SJeff Kirsher #define E1000_TX_FLAGS_IPV4		0x00000008
5422943146deSBen Greear #define E1000_TX_FLAGS_NO_FCS		0x00000010
5423b67e1913SBruce Allan #define E1000_TX_FLAGS_HWTSTAMP		0x00000020
5424dee1ad47SJeff Kirsher #define E1000_TX_FLAGS_VLAN_MASK	0xffff0000
5425dee1ad47SJeff Kirsher #define E1000_TX_FLAGS_VLAN_SHIFT	16
5426dee1ad47SJeff Kirsher 
e1000_tso(struct e1000_ring * tx_ring,struct sk_buff * skb,__be16 protocol)542747ccd1edSVlad Yasevich static int e1000_tso(struct e1000_ring *tx_ring, struct sk_buff *skb,
542847ccd1edSVlad Yasevich 		     __be16 protocol)
5429dee1ad47SJeff Kirsher {
5430dee1ad47SJeff Kirsher 	struct e1000_context_desc *context_desc;
5431dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
5432dee1ad47SJeff Kirsher 	unsigned int i;
5433dee1ad47SJeff Kirsher 	u32 cmd_length = 0;
543470443ae9SBruce Allan 	u16 ipcse = 0, mss;
5435dee1ad47SJeff Kirsher 	u8 ipcss, ipcso, tucss, tucso, hdr_len;
5436bcf1f57fSFrancois Romieu 	int err;
5437dee1ad47SJeff Kirsher 
5438dee1ad47SJeff Kirsher 	if (!skb_is_gso(skb))
5439dee1ad47SJeff Kirsher 		return 0;
5440dee1ad47SJeff Kirsher 
5441bcf1f57fSFrancois Romieu 	err = skb_cow_head(skb, 0);
5442bcf1f57fSFrancois Romieu 	if (err < 0)
5443dee1ad47SJeff Kirsher 		return err;
5444dee1ad47SJeff Kirsher 
5445504148feSEric Dumazet 	hdr_len = skb_tcp_all_headers(skb);
5446dee1ad47SJeff Kirsher 	mss = skb_shinfo(skb)->gso_size;
544747ccd1edSVlad Yasevich 	if (protocol == htons(ETH_P_IP)) {
5448dee1ad47SJeff Kirsher 		struct iphdr *iph = ip_hdr(skb);
5449dee1ad47SJeff Kirsher 		iph->tot_len = 0;
5450dee1ad47SJeff Kirsher 		iph->check = 0;
5451dee1ad47SJeff Kirsher 		tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
5452dee1ad47SJeff Kirsher 							 0, IPPROTO_TCP, 0);
5453dee1ad47SJeff Kirsher 		cmd_length = E1000_TXD_CMD_IP;
5454dee1ad47SJeff Kirsher 		ipcse = skb_transport_offset(skb) - 1;
5455dee1ad47SJeff Kirsher 	} else if (skb_is_gso_v6(skb)) {
54562b316fbcSHeiner Kallweit 		tcp_v6_gso_csum_prep(skb);
5457dee1ad47SJeff Kirsher 		ipcse = 0;
5458dee1ad47SJeff Kirsher 	}
5459dee1ad47SJeff Kirsher 	ipcss = skb_network_offset(skb);
5460dee1ad47SJeff Kirsher 	ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
5461dee1ad47SJeff Kirsher 	tucss = skb_transport_offset(skb);
5462dee1ad47SJeff Kirsher 	tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
5463dee1ad47SJeff Kirsher 
5464dee1ad47SJeff Kirsher 	cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
5465dee1ad47SJeff Kirsher 		       E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
5466dee1ad47SJeff Kirsher 
5467dee1ad47SJeff Kirsher 	i = tx_ring->next_to_use;
5468dee1ad47SJeff Kirsher 	context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
5469dee1ad47SJeff Kirsher 	buffer_info = &tx_ring->buffer_info[i];
5470dee1ad47SJeff Kirsher 
5471dee1ad47SJeff Kirsher 	context_desc->lower_setup.ip_fields.ipcss = ipcss;
5472dee1ad47SJeff Kirsher 	context_desc->lower_setup.ip_fields.ipcso = ipcso;
5473dee1ad47SJeff Kirsher 	context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
5474dee1ad47SJeff Kirsher 	context_desc->upper_setup.tcp_fields.tucss = tucss;
5475dee1ad47SJeff Kirsher 	context_desc->upper_setup.tcp_fields.tucso = tucso;
547670443ae9SBruce Allan 	context_desc->upper_setup.tcp_fields.tucse = 0;
5477dee1ad47SJeff Kirsher 	context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
5478dee1ad47SJeff Kirsher 	context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
5479dee1ad47SJeff Kirsher 	context_desc->cmd_and_length = cpu_to_le32(cmd_length);
5480dee1ad47SJeff Kirsher 
5481dee1ad47SJeff Kirsher 	buffer_info->time_stamp = jiffies;
5482dee1ad47SJeff Kirsher 	buffer_info->next_to_watch = i;
5483dee1ad47SJeff Kirsher 
5484dee1ad47SJeff Kirsher 	i++;
5485dee1ad47SJeff Kirsher 	if (i == tx_ring->count)
5486dee1ad47SJeff Kirsher 		i = 0;
5487dee1ad47SJeff Kirsher 	tx_ring->next_to_use = i;
5488dee1ad47SJeff Kirsher 
5489dee1ad47SJeff Kirsher 	return 1;
5490dee1ad47SJeff Kirsher }
5491dee1ad47SJeff Kirsher 
e1000_tx_csum(struct e1000_ring * tx_ring,struct sk_buff * skb,__be16 protocol)549247ccd1edSVlad Yasevich static bool e1000_tx_csum(struct e1000_ring *tx_ring, struct sk_buff *skb,
549347ccd1edSVlad Yasevich 			  __be16 protocol)
5494dee1ad47SJeff Kirsher {
549555aa6985SBruce Allan 	struct e1000_adapter *adapter = tx_ring->adapter;
5496dee1ad47SJeff Kirsher 	struct e1000_context_desc *context_desc;
5497dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
5498dee1ad47SJeff Kirsher 	unsigned int i;
5499dee1ad47SJeff Kirsher 	u8 css;
5500dee1ad47SJeff Kirsher 	u32 cmd_len = E1000_TXD_CMD_DEXT;
5501dee1ad47SJeff Kirsher 
5502dee1ad47SJeff Kirsher 	if (skb->ip_summed != CHECKSUM_PARTIAL)
55033992c8edSDavid Ertman 		return false;
5504dee1ad47SJeff Kirsher 
5505dee1ad47SJeff Kirsher 	switch (protocol) {
5506dee1ad47SJeff Kirsher 	case cpu_to_be16(ETH_P_IP):
5507dee1ad47SJeff Kirsher 		if (ip_hdr(skb)->protocol == IPPROTO_TCP)
5508dee1ad47SJeff Kirsher 			cmd_len |= E1000_TXD_CMD_TCP;
5509dee1ad47SJeff Kirsher 		break;
5510dee1ad47SJeff Kirsher 	case cpu_to_be16(ETH_P_IPV6):
5511dee1ad47SJeff Kirsher 		/* XXX not handling all IPV6 headers */
5512dee1ad47SJeff Kirsher 		if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
5513dee1ad47SJeff Kirsher 			cmd_len |= E1000_TXD_CMD_TCP;
5514dee1ad47SJeff Kirsher 		break;
5515dee1ad47SJeff Kirsher 	default:
5516dee1ad47SJeff Kirsher 		if (unlikely(net_ratelimit()))
5517dee1ad47SJeff Kirsher 			e_warn("checksum_partial proto=%x!\n",
5518dee1ad47SJeff Kirsher 			       be16_to_cpu(protocol));
5519dee1ad47SJeff Kirsher 		break;
5520dee1ad47SJeff Kirsher 	}
5521dee1ad47SJeff Kirsher 
5522dee1ad47SJeff Kirsher 	css = skb_checksum_start_offset(skb);
5523dee1ad47SJeff Kirsher 
5524dee1ad47SJeff Kirsher 	i = tx_ring->next_to_use;
5525dee1ad47SJeff Kirsher 	buffer_info = &tx_ring->buffer_info[i];
5526dee1ad47SJeff Kirsher 	context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
5527dee1ad47SJeff Kirsher 
5528dee1ad47SJeff Kirsher 	context_desc->lower_setup.ip_config = 0;
5529dee1ad47SJeff Kirsher 	context_desc->upper_setup.tcp_fields.tucss = css;
5530f0ff4398SBruce Allan 	context_desc->upper_setup.tcp_fields.tucso = css + skb->csum_offset;
5531dee1ad47SJeff Kirsher 	context_desc->upper_setup.tcp_fields.tucse = 0;
5532dee1ad47SJeff Kirsher 	context_desc->tcp_seg_setup.data = 0;
5533dee1ad47SJeff Kirsher 	context_desc->cmd_and_length = cpu_to_le32(cmd_len);
5534dee1ad47SJeff Kirsher 
5535dee1ad47SJeff Kirsher 	buffer_info->time_stamp = jiffies;
5536dee1ad47SJeff Kirsher 	buffer_info->next_to_watch = i;
5537dee1ad47SJeff Kirsher 
5538dee1ad47SJeff Kirsher 	i++;
5539dee1ad47SJeff Kirsher 	if (i == tx_ring->count)
5540dee1ad47SJeff Kirsher 		i = 0;
5541dee1ad47SJeff Kirsher 	tx_ring->next_to_use = i;
5542dee1ad47SJeff Kirsher 
55433992c8edSDavid Ertman 	return true;
5544dee1ad47SJeff Kirsher }
5545dee1ad47SJeff Kirsher 
e1000_tx_map(struct e1000_ring * tx_ring,struct sk_buff * skb,unsigned int first,unsigned int max_per_txd,unsigned int nr_frags)554655aa6985SBruce Allan static int e1000_tx_map(struct e1000_ring *tx_ring, struct sk_buff *skb,
554755aa6985SBruce Allan 			unsigned int first, unsigned int max_per_txd,
5548d821a4c4SBruce Allan 			unsigned int nr_frags)
5549dee1ad47SJeff Kirsher {
555055aa6985SBruce Allan 	struct e1000_adapter *adapter = tx_ring->adapter;
5551dee1ad47SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
5552dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
5553dee1ad47SJeff Kirsher 	unsigned int len = skb_headlen(skb);
5554dee1ad47SJeff Kirsher 	unsigned int offset = 0, size, count = 0, i;
5555dee1ad47SJeff Kirsher 	unsigned int f, bytecount, segs;
5556dee1ad47SJeff Kirsher 
5557dee1ad47SJeff Kirsher 	i = tx_ring->next_to_use;
5558dee1ad47SJeff Kirsher 
5559dee1ad47SJeff Kirsher 	while (len) {
5560dee1ad47SJeff Kirsher 		buffer_info = &tx_ring->buffer_info[i];
5561dee1ad47SJeff Kirsher 		size = min(len, max_per_txd);
5562dee1ad47SJeff Kirsher 
5563dee1ad47SJeff Kirsher 		buffer_info->length = size;
5564dee1ad47SJeff Kirsher 		buffer_info->time_stamp = jiffies;
5565dee1ad47SJeff Kirsher 		buffer_info->next_to_watch = i;
5566dee1ad47SJeff Kirsher 		buffer_info->dma = dma_map_single(&pdev->dev,
5567dee1ad47SJeff Kirsher 						  skb->data + offset,
5568dee1ad47SJeff Kirsher 						  size, DMA_TO_DEVICE);
5569dee1ad47SJeff Kirsher 		buffer_info->mapped_as_page = false;
5570dee1ad47SJeff Kirsher 		if (dma_mapping_error(&pdev->dev, buffer_info->dma))
5571dee1ad47SJeff Kirsher 			goto dma_error;
5572dee1ad47SJeff Kirsher 
5573dee1ad47SJeff Kirsher 		len -= size;
5574dee1ad47SJeff Kirsher 		offset += size;
5575dee1ad47SJeff Kirsher 		count++;
5576dee1ad47SJeff Kirsher 
5577dee1ad47SJeff Kirsher 		if (len) {
5578dee1ad47SJeff Kirsher 			i++;
5579dee1ad47SJeff Kirsher 			if (i == tx_ring->count)
5580dee1ad47SJeff Kirsher 				i = 0;
5581dee1ad47SJeff Kirsher 		}
5582dee1ad47SJeff Kirsher 	}
5583dee1ad47SJeff Kirsher 
5584dee1ad47SJeff Kirsher 	for (f = 0; f < nr_frags; f++) {
5585d7840976SMatthew Wilcox (Oracle) 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
5586dee1ad47SJeff Kirsher 
55879e903e08SEric Dumazet 		len = skb_frag_size(frag);
5588877749bfSIan Campbell 		offset = 0;
5589dee1ad47SJeff Kirsher 
5590dee1ad47SJeff Kirsher 		while (len) {
5591dee1ad47SJeff Kirsher 			i++;
5592dee1ad47SJeff Kirsher 			if (i == tx_ring->count)
5593dee1ad47SJeff Kirsher 				i = 0;
5594dee1ad47SJeff Kirsher 
5595dee1ad47SJeff Kirsher 			buffer_info = &tx_ring->buffer_info[i];
5596dee1ad47SJeff Kirsher 			size = min(len, max_per_txd);
5597dee1ad47SJeff Kirsher 
5598dee1ad47SJeff Kirsher 			buffer_info->length = size;
5599dee1ad47SJeff Kirsher 			buffer_info->time_stamp = jiffies;
5600dee1ad47SJeff Kirsher 			buffer_info->next_to_watch = i;
5601877749bfSIan Campbell 			buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
560217e813ecSBruce Allan 							    offset, size,
560317e813ecSBruce Allan 							    DMA_TO_DEVICE);
5604dee1ad47SJeff Kirsher 			buffer_info->mapped_as_page = true;
5605dee1ad47SJeff Kirsher 			if (dma_mapping_error(&pdev->dev, buffer_info->dma))
5606dee1ad47SJeff Kirsher 				goto dma_error;
5607dee1ad47SJeff Kirsher 
5608dee1ad47SJeff Kirsher 			len -= size;
5609dee1ad47SJeff Kirsher 			offset += size;
5610dee1ad47SJeff Kirsher 			count++;
5611dee1ad47SJeff Kirsher 		}
5612dee1ad47SJeff Kirsher 	}
5613dee1ad47SJeff Kirsher 
5614dee1ad47SJeff Kirsher 	segs = skb_shinfo(skb)->gso_segs ? : 1;
5615dee1ad47SJeff Kirsher 	/* multiply data chunks by size of headers */
5616dee1ad47SJeff Kirsher 	bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
5617dee1ad47SJeff Kirsher 
5618dee1ad47SJeff Kirsher 	tx_ring->buffer_info[i].skb = skb;
5619dee1ad47SJeff Kirsher 	tx_ring->buffer_info[i].segs = segs;
5620dee1ad47SJeff Kirsher 	tx_ring->buffer_info[i].bytecount = bytecount;
5621dee1ad47SJeff Kirsher 	tx_ring->buffer_info[first].next_to_watch = i;
5622dee1ad47SJeff Kirsher 
5623dee1ad47SJeff Kirsher 	return count;
5624dee1ad47SJeff Kirsher 
5625dee1ad47SJeff Kirsher dma_error:
5626dee1ad47SJeff Kirsher 	dev_err(&pdev->dev, "Tx DMA map failed\n");
5627dee1ad47SJeff Kirsher 	buffer_info->dma = 0;
5628dee1ad47SJeff Kirsher 	if (count)
5629dee1ad47SJeff Kirsher 		count--;
5630dee1ad47SJeff Kirsher 
5631dee1ad47SJeff Kirsher 	while (count--) {
5632dee1ad47SJeff Kirsher 		if (i == 0)
5633dee1ad47SJeff Kirsher 			i += tx_ring->count;
5634dee1ad47SJeff Kirsher 		i--;
5635dee1ad47SJeff Kirsher 		buffer_info = &tx_ring->buffer_info[i];
5636377b6273SFlorian Fainelli 		e1000_put_txbuf(tx_ring, buffer_info, true);
5637dee1ad47SJeff Kirsher 	}
5638dee1ad47SJeff Kirsher 
5639dee1ad47SJeff Kirsher 	return 0;
5640dee1ad47SJeff Kirsher }
5641dee1ad47SJeff Kirsher 
e1000_tx_queue(struct e1000_ring * tx_ring,int tx_flags,int count)564255aa6985SBruce Allan static void e1000_tx_queue(struct e1000_ring *tx_ring, int tx_flags, int count)
5643dee1ad47SJeff Kirsher {
564455aa6985SBruce Allan 	struct e1000_adapter *adapter = tx_ring->adapter;
5645dee1ad47SJeff Kirsher 	struct e1000_tx_desc *tx_desc = NULL;
5646dee1ad47SJeff Kirsher 	struct e1000_buffer *buffer_info;
5647dee1ad47SJeff Kirsher 	u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
5648dee1ad47SJeff Kirsher 	unsigned int i;
5649dee1ad47SJeff Kirsher 
5650dee1ad47SJeff Kirsher 	if (tx_flags & E1000_TX_FLAGS_TSO) {
5651dee1ad47SJeff Kirsher 		txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
5652dee1ad47SJeff Kirsher 		    E1000_TXD_CMD_TSE;
5653dee1ad47SJeff Kirsher 		txd_upper |= E1000_TXD_POPTS_TXSM << 8;
5654dee1ad47SJeff Kirsher 
5655dee1ad47SJeff Kirsher 		if (tx_flags & E1000_TX_FLAGS_IPV4)
5656dee1ad47SJeff Kirsher 			txd_upper |= E1000_TXD_POPTS_IXSM << 8;
5657dee1ad47SJeff Kirsher 	}
5658dee1ad47SJeff Kirsher 
5659dee1ad47SJeff Kirsher 	if (tx_flags & E1000_TX_FLAGS_CSUM) {
5660dee1ad47SJeff Kirsher 		txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
5661dee1ad47SJeff Kirsher 		txd_upper |= E1000_TXD_POPTS_TXSM << 8;
5662dee1ad47SJeff Kirsher 	}
5663dee1ad47SJeff Kirsher 
5664dee1ad47SJeff Kirsher 	if (tx_flags & E1000_TX_FLAGS_VLAN) {
5665dee1ad47SJeff Kirsher 		txd_lower |= E1000_TXD_CMD_VLE;
5666dee1ad47SJeff Kirsher 		txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
5667dee1ad47SJeff Kirsher 	}
5668dee1ad47SJeff Kirsher 
5669943146deSBen Greear 	if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
5670943146deSBen Greear 		txd_lower &= ~(E1000_TXD_CMD_IFCS);
5671943146deSBen Greear 
5672b67e1913SBruce Allan 	if (unlikely(tx_flags & E1000_TX_FLAGS_HWTSTAMP)) {
5673b67e1913SBruce Allan 		txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
5674b67e1913SBruce Allan 		txd_upper |= E1000_TXD_EXTCMD_TSTAMP;
5675b67e1913SBruce Allan 	}
5676b67e1913SBruce Allan 
5677dee1ad47SJeff Kirsher 	i = tx_ring->next_to_use;
5678dee1ad47SJeff Kirsher 
5679dee1ad47SJeff Kirsher 	do {
5680dee1ad47SJeff Kirsher 		buffer_info = &tx_ring->buffer_info[i];
5681dee1ad47SJeff Kirsher 		tx_desc = E1000_TX_DESC(*tx_ring, i);
5682dee1ad47SJeff Kirsher 		tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
5683f0ff4398SBruce Allan 		tx_desc->lower.data = cpu_to_le32(txd_lower |
5684f0ff4398SBruce Allan 						  buffer_info->length);
5685dee1ad47SJeff Kirsher 		tx_desc->upper.data = cpu_to_le32(txd_upper);
5686dee1ad47SJeff Kirsher 
5687dee1ad47SJeff Kirsher 		i++;
5688dee1ad47SJeff Kirsher 		if (i == tx_ring->count)
5689dee1ad47SJeff Kirsher 			i = 0;
5690dee1ad47SJeff Kirsher 	} while (--count > 0);
5691dee1ad47SJeff Kirsher 
5692dee1ad47SJeff Kirsher 	tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
5693dee1ad47SJeff Kirsher 
5694943146deSBen Greear 	/* txd_cmd re-enables FCS, so we'll re-disable it here as desired. */
5695943146deSBen Greear 	if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
5696943146deSBen Greear 		tx_desc->lower.data &= ~(cpu_to_le32(E1000_TXD_CMD_IFCS));
5697943146deSBen Greear 
5698e921eb1aSBruce Allan 	/* Force memory writes to complete before letting h/w
5699dee1ad47SJeff Kirsher 	 * know there are new descriptors to fetch.  (Only
5700dee1ad47SJeff Kirsher 	 * applicable for weak-ordered memory model archs,
5701dee1ad47SJeff Kirsher 	 * such as IA-64).
5702dee1ad47SJeff Kirsher 	 */
5703dee1ad47SJeff Kirsher 	wmb();
5704dee1ad47SJeff Kirsher 
5705dee1ad47SJeff Kirsher 	tx_ring->next_to_use = i;
5706dee1ad47SJeff Kirsher }
5707dee1ad47SJeff Kirsher 
5708dee1ad47SJeff Kirsher #define MINIMUM_DHCP_PACKET_SIZE 282
e1000_transfer_dhcp_info(struct e1000_adapter * adapter,struct sk_buff * skb)5709dee1ad47SJeff Kirsher static int e1000_transfer_dhcp_info(struct e1000_adapter *adapter,
5710dee1ad47SJeff Kirsher 				    struct sk_buff *skb)
5711dee1ad47SJeff Kirsher {
5712dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
5713dee1ad47SJeff Kirsher 	u16 length, offset;
5714dee1ad47SJeff Kirsher 
5715df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb) &&
5716df8a39deSJiri Pirko 	    !((skb_vlan_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
5717dee1ad47SJeff Kirsher 	      (adapter->hw.mng_cookie.status &
5718dee1ad47SJeff Kirsher 	       E1000_MNG_DHCP_COOKIE_STATUS_VLAN)))
5719dee1ad47SJeff Kirsher 		return 0;
5720dee1ad47SJeff Kirsher 
5721dee1ad47SJeff Kirsher 	if (skb->len <= MINIMUM_DHCP_PACKET_SIZE)
5722dee1ad47SJeff Kirsher 		return 0;
5723dee1ad47SJeff Kirsher 
5724dee1ad47SJeff Kirsher 	if (((struct ethhdr *)skb->data)->h_proto != htons(ETH_P_IP))
5725dee1ad47SJeff Kirsher 		return 0;
5726dee1ad47SJeff Kirsher 
5727dee1ad47SJeff Kirsher 	{
5728dee1ad47SJeff Kirsher 		const struct iphdr *ip = (struct iphdr *)((u8 *)skb->data + 14);
5729dee1ad47SJeff Kirsher 		struct udphdr *udp;
5730dee1ad47SJeff Kirsher 
5731dee1ad47SJeff Kirsher 		if (ip->protocol != IPPROTO_UDP)
5732dee1ad47SJeff Kirsher 			return 0;
5733dee1ad47SJeff Kirsher 
5734dee1ad47SJeff Kirsher 		udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
5735dee1ad47SJeff Kirsher 		if (ntohs(udp->dest) != 67)
5736dee1ad47SJeff Kirsher 			return 0;
5737dee1ad47SJeff Kirsher 
5738dee1ad47SJeff Kirsher 		offset = (u8 *)udp + 8 - skb->data;
5739dee1ad47SJeff Kirsher 		length = skb->len - offset;
5740dee1ad47SJeff Kirsher 		return e1000e_mng_write_dhcp_info(hw, (u8 *)udp + 8, length);
5741dee1ad47SJeff Kirsher 	}
5742dee1ad47SJeff Kirsher 
5743dee1ad47SJeff Kirsher 	return 0;
5744dee1ad47SJeff Kirsher }
5745dee1ad47SJeff Kirsher 
__e1000_maybe_stop_tx(struct e1000_ring * tx_ring,int size)574655aa6985SBruce Allan static int __e1000_maybe_stop_tx(struct e1000_ring *tx_ring, int size)
5747dee1ad47SJeff Kirsher {
574855aa6985SBruce Allan 	struct e1000_adapter *adapter = tx_ring->adapter;
5749dee1ad47SJeff Kirsher 
575055aa6985SBruce Allan 	netif_stop_queue(adapter->netdev);
5751e921eb1aSBruce Allan 	/* Herbert's original patch had:
5752dee1ad47SJeff Kirsher 	 *  smp_mb__after_netif_stop_queue();
5753dee1ad47SJeff Kirsher 	 * but since that doesn't exist yet, just open code it.
5754dee1ad47SJeff Kirsher 	 */
5755dee1ad47SJeff Kirsher 	smp_mb();
5756dee1ad47SJeff Kirsher 
5757e921eb1aSBruce Allan 	/* We need to check again in a case another CPU has just
5758dee1ad47SJeff Kirsher 	 * made room available.
5759dee1ad47SJeff Kirsher 	 */
576055aa6985SBruce Allan 	if (e1000_desc_unused(tx_ring) < size)
5761dee1ad47SJeff Kirsher 		return -EBUSY;
5762dee1ad47SJeff Kirsher 
5763dee1ad47SJeff Kirsher 	/* A reprieve! */
576455aa6985SBruce Allan 	netif_start_queue(adapter->netdev);
5765dee1ad47SJeff Kirsher 	++adapter->restart_queue;
5766dee1ad47SJeff Kirsher 	return 0;
5767dee1ad47SJeff Kirsher }
5768dee1ad47SJeff Kirsher 
e1000_maybe_stop_tx(struct e1000_ring * tx_ring,int size)576955aa6985SBruce Allan static int e1000_maybe_stop_tx(struct e1000_ring *tx_ring, int size)
5770dee1ad47SJeff Kirsher {
5771d821a4c4SBruce Allan 	BUG_ON(size > tx_ring->count);
5772d821a4c4SBruce Allan 
577355aa6985SBruce Allan 	if (e1000_desc_unused(tx_ring) >= size)
5774dee1ad47SJeff Kirsher 		return 0;
577555aa6985SBruce Allan 	return __e1000_maybe_stop_tx(tx_ring, size);
5776dee1ad47SJeff Kirsher }
5777dee1ad47SJeff Kirsher 
e1000_xmit_frame(struct sk_buff * skb,struct net_device * netdev)5778dee1ad47SJeff Kirsher static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
5779dee1ad47SJeff Kirsher 				    struct net_device *netdev)
5780dee1ad47SJeff Kirsher {
5781dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
5782dee1ad47SJeff Kirsher 	struct e1000_ring *tx_ring = adapter->tx_ring;
5783dee1ad47SJeff Kirsher 	unsigned int first;
5784dee1ad47SJeff Kirsher 	unsigned int tx_flags = 0;
5785dee1ad47SJeff Kirsher 	unsigned int len = skb_headlen(skb);
5786dee1ad47SJeff Kirsher 	unsigned int nr_frags;
5787dee1ad47SJeff Kirsher 	unsigned int mss;
5788dee1ad47SJeff Kirsher 	int count = 0;
5789dee1ad47SJeff Kirsher 	int tso;
5790dee1ad47SJeff Kirsher 	unsigned int f;
579147ccd1edSVlad Yasevich 	__be16 protocol = vlan_get_protocol(skb);
5792dee1ad47SJeff Kirsher 
5793dee1ad47SJeff Kirsher 	if (test_bit(__E1000_DOWN, &adapter->state)) {
5794dee1ad47SJeff Kirsher 		dev_kfree_skb_any(skb);
5795dee1ad47SJeff Kirsher 		return NETDEV_TX_OK;
5796dee1ad47SJeff Kirsher 	}
5797dee1ad47SJeff Kirsher 
5798dee1ad47SJeff Kirsher 	if (skb->len <= 0) {
5799dee1ad47SJeff Kirsher 		dev_kfree_skb_any(skb);
5800dee1ad47SJeff Kirsher 		return NETDEV_TX_OK;
5801dee1ad47SJeff Kirsher 	}
5802dee1ad47SJeff Kirsher 
5803e921eb1aSBruce Allan 	/* The minimum packet size with TCTL.PSP set is 17 bytes so
58046e97c170STushar Dave 	 * pad skb in order to meet this minimum size requirement
58056e97c170STushar Dave 	 */
5806a94d9e22SAlexander Duyck 	if (skb_put_padto(skb, 17))
58076e97c170STushar Dave 		return NETDEV_TX_OK;
58086e97c170STushar Dave 
5809dee1ad47SJeff Kirsher 	mss = skb_shinfo(skb)->gso_size;
5810dee1ad47SJeff Kirsher 	if (mss) {
5811dee1ad47SJeff Kirsher 		u8 hdr_len;
5812dee1ad47SJeff Kirsher 
5813e921eb1aSBruce Allan 		/* TSO Workaround for 82571/2/3 Controllers -- if skb->data
5814dee1ad47SJeff Kirsher 		 * points to just header, pull a few bytes of payload from
5815dee1ad47SJeff Kirsher 		 * frags into skb->data
5816dee1ad47SJeff Kirsher 		 */
5817504148feSEric Dumazet 		hdr_len = skb_tcp_all_headers(skb);
5818e921eb1aSBruce Allan 		/* we do this workaround for ES2LAN, but it is un-necessary,
5819dee1ad47SJeff Kirsher 		 * avoiding it could save a lot of cycles
5820dee1ad47SJeff Kirsher 		 */
5821dee1ad47SJeff Kirsher 		if (skb->data_len && (hdr_len == len)) {
5822dee1ad47SJeff Kirsher 			unsigned int pull_size;
5823dee1ad47SJeff Kirsher 
5824a2a5b323SBruce Allan 			pull_size = min_t(unsigned int, 4, skb->data_len);
5825dee1ad47SJeff Kirsher 			if (!__pskb_pull_tail(skb, pull_size)) {
5826dee1ad47SJeff Kirsher 				e_err("__pskb_pull_tail failed.\n");
5827dee1ad47SJeff Kirsher 				dev_kfree_skb_any(skb);
5828dee1ad47SJeff Kirsher 				return NETDEV_TX_OK;
5829dee1ad47SJeff Kirsher 			}
5830dee1ad47SJeff Kirsher 			len = skb_headlen(skb);
5831dee1ad47SJeff Kirsher 		}
5832dee1ad47SJeff Kirsher 	}
5833dee1ad47SJeff Kirsher 
5834dee1ad47SJeff Kirsher 	/* reserve a descriptor for the offload context */
5835dee1ad47SJeff Kirsher 	if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
5836dee1ad47SJeff Kirsher 		count++;
5837dee1ad47SJeff Kirsher 	count++;
5838dee1ad47SJeff Kirsher 
5839d821a4c4SBruce Allan 	count += DIV_ROUND_UP(len, adapter->tx_fifo_limit);
5840dee1ad47SJeff Kirsher 
5841dee1ad47SJeff Kirsher 	nr_frags = skb_shinfo(skb)->nr_frags;
5842dee1ad47SJeff Kirsher 	for (f = 0; f < nr_frags; f++)
5843d821a4c4SBruce Allan 		count += DIV_ROUND_UP(skb_frag_size(&skb_shinfo(skb)->frags[f]),
5844d821a4c4SBruce Allan 				      adapter->tx_fifo_limit);
5845dee1ad47SJeff Kirsher 
5846dee1ad47SJeff Kirsher 	if (adapter->hw.mac.tx_pkt_filtering)
5847dee1ad47SJeff Kirsher 		e1000_transfer_dhcp_info(adapter, skb);
5848dee1ad47SJeff Kirsher 
5849e921eb1aSBruce Allan 	/* need: count + 2 desc gap to keep tail from touching
5850dee1ad47SJeff Kirsher 	 * head, otherwise try next time
5851dee1ad47SJeff Kirsher 	 */
585255aa6985SBruce Allan 	if (e1000_maybe_stop_tx(tx_ring, count + 2))
5853dee1ad47SJeff Kirsher 		return NETDEV_TX_BUSY;
5854dee1ad47SJeff Kirsher 
5855df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
5856dee1ad47SJeff Kirsher 		tx_flags |= E1000_TX_FLAGS_VLAN;
5857df8a39deSJiri Pirko 		tx_flags |= (skb_vlan_tag_get(skb) <<
5858df8a39deSJiri Pirko 			     E1000_TX_FLAGS_VLAN_SHIFT);
5859dee1ad47SJeff Kirsher 	}
5860dee1ad47SJeff Kirsher 
5861dee1ad47SJeff Kirsher 	first = tx_ring->next_to_use;
5862dee1ad47SJeff Kirsher 
586347ccd1edSVlad Yasevich 	tso = e1000_tso(tx_ring, skb, protocol);
5864dee1ad47SJeff Kirsher 	if (tso < 0) {
5865dee1ad47SJeff Kirsher 		dev_kfree_skb_any(skb);
5866dee1ad47SJeff Kirsher 		return NETDEV_TX_OK;
5867dee1ad47SJeff Kirsher 	}
5868dee1ad47SJeff Kirsher 
5869dee1ad47SJeff Kirsher 	if (tso)
5870dee1ad47SJeff Kirsher 		tx_flags |= E1000_TX_FLAGS_TSO;
587147ccd1edSVlad Yasevich 	else if (e1000_tx_csum(tx_ring, skb, protocol))
5872dee1ad47SJeff Kirsher 		tx_flags |= E1000_TX_FLAGS_CSUM;
5873dee1ad47SJeff Kirsher 
5874e921eb1aSBruce Allan 	/* Old method was to assume IPv4 packet by default if TSO was enabled.
5875dee1ad47SJeff Kirsher 	 * 82571 hardware supports TSO capabilities for IPv6 as well...
5876dee1ad47SJeff Kirsher 	 * no longer assume, we must.
5877dee1ad47SJeff Kirsher 	 */
587847ccd1edSVlad Yasevich 	if (protocol == htons(ETH_P_IP))
5879dee1ad47SJeff Kirsher 		tx_flags |= E1000_TX_FLAGS_IPV4;
5880dee1ad47SJeff Kirsher 
5881943146deSBen Greear 	if (unlikely(skb->no_fcs))
5882943146deSBen Greear 		tx_flags |= E1000_TX_FLAGS_NO_FCS;
5883943146deSBen Greear 
5884dee1ad47SJeff Kirsher 	/* if count is 0 then mapping error has occurred */
5885d821a4c4SBruce Allan 	count = e1000_tx_map(tx_ring, skb, first, adapter->tx_fifo_limit,
5886d821a4c4SBruce Allan 			     nr_frags);
5887dee1ad47SJeff Kirsher 	if (count) {
58886930895dSMathias Koehrer 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
5889cff57141SJacob Keller 		    (adapter->flags & FLAG_HAS_HW_TIMESTAMP)) {
5890cff57141SJacob Keller 			if (!adapter->tx_hwtstamp_skb) {
5891b67e1913SBruce Allan 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
5892b67e1913SBruce Allan 				tx_flags |= E1000_TX_FLAGS_HWTSTAMP;
5893b67e1913SBruce Allan 				adapter->tx_hwtstamp_skb = skb_get(skb);
589459c871c5SJakub Kicinski 				adapter->tx_hwtstamp_start = jiffies;
5895b67e1913SBruce Allan 				schedule_work(&adapter->tx_hwtstamp_work);
5896cff57141SJacob Keller 			} else {
5897cff57141SJacob Keller 				adapter->tx_hwtstamp_skipped++;
5898cff57141SJacob Keller 			}
5899b67e1913SBruce Allan 		}
590080be3129SWillem de Bruijn 
590174abc9b1SMiroslav Lichvar 		skb_tx_timestamp(skb);
590274abc9b1SMiroslav Lichvar 
59033f0cfa3bSTom Herbert 		netdev_sent_queue(netdev, skb->len);
590455aa6985SBruce Allan 		e1000_tx_queue(tx_ring, tx_flags, count);
5905dee1ad47SJeff Kirsher 		/* Make sure there is space in the ring for the next send. */
5906d821a4c4SBruce Allan 		e1000_maybe_stop_tx(tx_ring,
5907eed913f6SAkihiko Odaki 				    ((MAX_SKB_FRAGS + 1) *
5908d821a4c4SBruce Allan 				     DIV_ROUND_UP(PAGE_SIZE,
5909eed913f6SAkihiko Odaki 						  adapter->tx_fifo_limit) + 4));
5910472f31f5SFlorian Westphal 
59116b16f9eeSFlorian Westphal 		if (!netdev_xmit_more() ||
5912472f31f5SFlorian Westphal 		    netif_xmit_stopped(netdev_get_tx_queue(netdev, 0))) {
5913472f31f5SFlorian Westphal 			if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
5914472f31f5SFlorian Westphal 				e1000e_update_tdt_wa(tx_ring,
5915472f31f5SFlorian Westphal 						     tx_ring->next_to_use);
5916472f31f5SFlorian Westphal 			else
5917472f31f5SFlorian Westphal 				writel(tx_ring->next_to_use, tx_ring->tail);
5918472f31f5SFlorian Westphal 		}
5919dee1ad47SJeff Kirsher 	} else {
5920dee1ad47SJeff Kirsher 		dev_kfree_skb_any(skb);
5921dee1ad47SJeff Kirsher 		tx_ring->buffer_info[first].time_stamp = 0;
5922dee1ad47SJeff Kirsher 		tx_ring->next_to_use = first;
5923dee1ad47SJeff Kirsher 	}
5924dee1ad47SJeff Kirsher 
5925dee1ad47SJeff Kirsher 	return NETDEV_TX_OK;
5926dee1ad47SJeff Kirsher }
5927dee1ad47SJeff Kirsher 
5928dee1ad47SJeff Kirsher /**
5929dee1ad47SJeff Kirsher  * e1000_tx_timeout - Respond to a Tx Hang
5930dee1ad47SJeff Kirsher  * @netdev: network interface device structure
5931b50f7bcaSJesse Brandeburg  * @txqueue: index of the hung queue (unused)
5932dee1ad47SJeff Kirsher  **/
e1000_tx_timeout(struct net_device * netdev,unsigned int __always_unused txqueue)5933b50f7bcaSJesse Brandeburg static void e1000_tx_timeout(struct net_device *netdev, unsigned int __always_unused txqueue)
5934dee1ad47SJeff Kirsher {
5935dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
5936dee1ad47SJeff Kirsher 
5937dee1ad47SJeff Kirsher 	/* Do the reset outside of interrupt context */
5938dee1ad47SJeff Kirsher 	adapter->tx_timeout_count++;
5939dee1ad47SJeff Kirsher 	schedule_work(&adapter->reset_task);
5940dee1ad47SJeff Kirsher }
5941dee1ad47SJeff Kirsher 
e1000_reset_task(struct work_struct * work)5942dee1ad47SJeff Kirsher static void e1000_reset_task(struct work_struct *work)
5943dee1ad47SJeff Kirsher {
5944dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter;
5945dee1ad47SJeff Kirsher 	adapter = container_of(work, struct e1000_adapter, reset_task);
5946dee1ad47SJeff Kirsher 
594721f857f0SVitaly Lifshits 	rtnl_lock();
5948dee1ad47SJeff Kirsher 	/* don't run the task if already down */
594921f857f0SVitaly Lifshits 	if (test_bit(__E1000_DOWN, &adapter->state)) {
595021f857f0SVitaly Lifshits 		rtnl_unlock();
5951dee1ad47SJeff Kirsher 		return;
595221f857f0SVitaly Lifshits 	}
5953dee1ad47SJeff Kirsher 
595412d43f7dSBruce Allan 	if (!(adapter->flags & FLAG_RESTART_NOW)) {
5955dee1ad47SJeff Kirsher 		e1000e_dump(adapter);
595612d43f7dSBruce Allan 		e_err("Reset adapter unexpectedly\n");
5957dee1ad47SJeff Kirsher 	}
5958dee1ad47SJeff Kirsher 	e1000e_reinit_locked(adapter);
595921f857f0SVitaly Lifshits 	rtnl_unlock();
5960dee1ad47SJeff Kirsher }
5961dee1ad47SJeff Kirsher 
5962dee1ad47SJeff Kirsher /**
596339da2cacSSasha Neftin  * e1000e_get_stats64 - Get System Network Statistics
5964dee1ad47SJeff Kirsher  * @netdev: network interface device structure
5965dee1ad47SJeff Kirsher  * @stats: rtnl_link_stats64 pointer
5966dee1ad47SJeff Kirsher  *
5967dee1ad47SJeff Kirsher  * Returns the address of the device statistics structure.
5968dee1ad47SJeff Kirsher  **/
e1000e_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)5969bc1f4470Sstephen hemminger void e1000e_get_stats64(struct net_device *netdev,
5970dee1ad47SJeff Kirsher 			struct rtnl_link_stats64 *stats)
5971dee1ad47SJeff Kirsher {
5972dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
5973dee1ad47SJeff Kirsher 
5974dee1ad47SJeff Kirsher 	spin_lock(&adapter->stats64_lock);
5975dee1ad47SJeff Kirsher 	e1000e_update_stats(adapter);
5976dee1ad47SJeff Kirsher 	/* Fill out the OS statistics structure */
5977dee1ad47SJeff Kirsher 	stats->rx_bytes = adapter->stats.gorc;
5978dee1ad47SJeff Kirsher 	stats->rx_packets = adapter->stats.gprc;
5979dee1ad47SJeff Kirsher 	stats->tx_bytes = adapter->stats.gotc;
5980dee1ad47SJeff Kirsher 	stats->tx_packets = adapter->stats.gptc;
5981dee1ad47SJeff Kirsher 	stats->multicast = adapter->stats.mprc;
5982dee1ad47SJeff Kirsher 	stats->collisions = adapter->stats.colc;
5983dee1ad47SJeff Kirsher 
5984dee1ad47SJeff Kirsher 	/* Rx Errors */
5985dee1ad47SJeff Kirsher 
5986e921eb1aSBruce Allan 	/* RLEC on some newer hardware can be incorrect so build
5987dee1ad47SJeff Kirsher 	 * our own version based on RUC and ROC
5988dee1ad47SJeff Kirsher 	 */
5989dee1ad47SJeff Kirsher 	stats->rx_errors = adapter->stats.rxerrc +
5990dee1ad47SJeff Kirsher 	    adapter->stats.crcerrs + adapter->stats.algnerrc +
5991f0ff4398SBruce Allan 	    adapter->stats.ruc + adapter->stats.roc + adapter->stats.cexterr;
5992f0ff4398SBruce Allan 	stats->rx_length_errors = adapter->stats.ruc + adapter->stats.roc;
5993dee1ad47SJeff Kirsher 	stats->rx_crc_errors = adapter->stats.crcerrs;
5994dee1ad47SJeff Kirsher 	stats->rx_frame_errors = adapter->stats.algnerrc;
5995dee1ad47SJeff Kirsher 	stats->rx_missed_errors = adapter->stats.mpc;
5996dee1ad47SJeff Kirsher 
5997dee1ad47SJeff Kirsher 	/* Tx Errors */
5998f0ff4398SBruce Allan 	stats->tx_errors = adapter->stats.ecol + adapter->stats.latecol;
5999dee1ad47SJeff Kirsher 	stats->tx_aborted_errors = adapter->stats.ecol;
6000dee1ad47SJeff Kirsher 	stats->tx_window_errors = adapter->stats.latecol;
6001dee1ad47SJeff Kirsher 	stats->tx_carrier_errors = adapter->stats.tncrs;
6002dee1ad47SJeff Kirsher 
6003dee1ad47SJeff Kirsher 	/* Tx Dropped needs to be maintained elsewhere */
6004dee1ad47SJeff Kirsher 
6005dee1ad47SJeff Kirsher 	spin_unlock(&adapter->stats64_lock);
6006dee1ad47SJeff Kirsher }
6007dee1ad47SJeff Kirsher 
6008dee1ad47SJeff Kirsher /**
6009dee1ad47SJeff Kirsher  * e1000_change_mtu - Change the Maximum Transfer Unit
6010dee1ad47SJeff Kirsher  * @netdev: network interface device structure
6011dee1ad47SJeff Kirsher  * @new_mtu: new value for maximum frame size
6012dee1ad47SJeff Kirsher  *
6013dee1ad47SJeff Kirsher  * Returns 0 on success, negative on failure
6014dee1ad47SJeff Kirsher  **/
e1000_change_mtu(struct net_device * netdev,int new_mtu)6015dee1ad47SJeff Kirsher static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
6016dee1ad47SJeff Kirsher {
6017dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
60188084b86dSAlexander Duyck 	int max_frame = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
6019dee1ad47SJeff Kirsher 
6020dee1ad47SJeff Kirsher 	/* Jumbo frame support */
602191c527a5SJarod Wilson 	if ((new_mtu > ETH_DATA_LEN) &&
60222e1706f2SBruce Allan 	    !(adapter->flags & FLAG_HAS_JUMBO_FRAMES)) {
6023dee1ad47SJeff Kirsher 		e_err("Jumbo Frames not supported.\n");
6024dee1ad47SJeff Kirsher 		return -EINVAL;
6025dee1ad47SJeff Kirsher 	}
6026dee1ad47SJeff Kirsher 
60272fbe4526SBruce Allan 	/* Jumbo frame workaround on 82579 and newer requires CRC be stripped */
60282fbe4526SBruce Allan 	if ((adapter->hw.mac.type >= e1000_pch2lan) &&
6029dee1ad47SJeff Kirsher 	    !(adapter->flags2 & FLAG2_CRC_STRIPPING) &&
6030dee1ad47SJeff Kirsher 	    (new_mtu > ETH_DATA_LEN)) {
60312fbe4526SBruce Allan 		e_err("Jumbo Frames not supported on this device when CRC stripping is disabled.\n");
6032dee1ad47SJeff Kirsher 		return -EINVAL;
6033dee1ad47SJeff Kirsher 	}
6034dee1ad47SJeff Kirsher 
6035dee1ad47SJeff Kirsher 	while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
6036ab6973aeSArjan van de Ven 		usleep_range(1000, 1100);
6037dee1ad47SJeff Kirsher 	/* e1000e_down -> e1000e_reset dependent on max_frame_size & mtu */
6038dee1ad47SJeff Kirsher 	adapter->max_frame_size = max_frame;
603912299132SFlorian Fainelli 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
604012299132SFlorian Fainelli 		   netdev->mtu, new_mtu);
60411eb2cdedSEric Dumazet 	WRITE_ONCE(netdev->mtu, new_mtu);
604263eb48f1SDavid Ertman 
604363eb48f1SDavid Ertman 	pm_runtime_get_sync(netdev->dev.parent);
604463eb48f1SDavid Ertman 
6045dee1ad47SJeff Kirsher 	if (netif_running(netdev))
604628002099SDavid Ertman 		e1000e_down(adapter, true);
6047dee1ad47SJeff Kirsher 
6048e921eb1aSBruce Allan 	/* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
6049dee1ad47SJeff Kirsher 	 * means we reserve 2 more, this pushes us to allocate from the next
6050dee1ad47SJeff Kirsher 	 * larger slab size.
6051dee1ad47SJeff Kirsher 	 * i.e. RXBUFFER_2048 --> size-4096 slab
6052dee1ad47SJeff Kirsher 	 * However with the new *_jumbo_rx* routines, jumbo receives will use
6053dee1ad47SJeff Kirsher 	 * fragmented skbs
6054dee1ad47SJeff Kirsher 	 */
6055dee1ad47SJeff Kirsher 
6056dee1ad47SJeff Kirsher 	if (max_frame <= 2048)
6057dee1ad47SJeff Kirsher 		adapter->rx_buffer_len = 2048;
6058dee1ad47SJeff Kirsher 	else
6059dee1ad47SJeff Kirsher 		adapter->rx_buffer_len = 4096;
6060dee1ad47SJeff Kirsher 
6061dee1ad47SJeff Kirsher 	/* adjust allocation if LPE protects us, and we aren't using SBP */
60628084b86dSAlexander Duyck 	if (max_frame <= (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN))
60638084b86dSAlexander Duyck 		adapter->rx_buffer_len = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
6064dee1ad47SJeff Kirsher 
6065dee1ad47SJeff Kirsher 	if (netif_running(netdev))
6066dee1ad47SJeff Kirsher 		e1000e_up(adapter);
6067dee1ad47SJeff Kirsher 	else
6068dee1ad47SJeff Kirsher 		e1000e_reset(adapter);
6069dee1ad47SJeff Kirsher 
607063eb48f1SDavid Ertman 	pm_runtime_put_sync(netdev->dev.parent);
607163eb48f1SDavid Ertman 
6072dee1ad47SJeff Kirsher 	clear_bit(__E1000_RESETTING, &adapter->state);
6073dee1ad47SJeff Kirsher 
6074dee1ad47SJeff Kirsher 	return 0;
6075dee1ad47SJeff Kirsher }
6076dee1ad47SJeff Kirsher 
e1000_mii_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)6077dee1ad47SJeff Kirsher static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
6078dee1ad47SJeff Kirsher 			   int cmd)
6079dee1ad47SJeff Kirsher {
6080dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
6081dee1ad47SJeff Kirsher 	struct mii_ioctl_data *data = if_mii(ifr);
6082dee1ad47SJeff Kirsher 
6083dee1ad47SJeff Kirsher 	if (adapter->hw.phy.media_type != e1000_media_type_copper)
6084dee1ad47SJeff Kirsher 		return -EOPNOTSUPP;
6085dee1ad47SJeff Kirsher 
6086dee1ad47SJeff Kirsher 	switch (cmd) {
6087dee1ad47SJeff Kirsher 	case SIOCGMIIPHY:
6088dee1ad47SJeff Kirsher 		data->phy_id = adapter->hw.phy.addr;
6089dee1ad47SJeff Kirsher 		break;
6090dee1ad47SJeff Kirsher 	case SIOCGMIIREG:
6091dee1ad47SJeff Kirsher 		e1000_phy_read_status(adapter);
6092dee1ad47SJeff Kirsher 
6093dee1ad47SJeff Kirsher 		switch (data->reg_num & 0x1F) {
6094dee1ad47SJeff Kirsher 		case MII_BMCR:
6095dee1ad47SJeff Kirsher 			data->val_out = adapter->phy_regs.bmcr;
6096dee1ad47SJeff Kirsher 			break;
6097dee1ad47SJeff Kirsher 		case MII_BMSR:
6098dee1ad47SJeff Kirsher 			data->val_out = adapter->phy_regs.bmsr;
6099dee1ad47SJeff Kirsher 			break;
6100dee1ad47SJeff Kirsher 		case MII_PHYSID1:
6101dee1ad47SJeff Kirsher 			data->val_out = (adapter->hw.phy.id >> 16);
6102dee1ad47SJeff Kirsher 			break;
6103dee1ad47SJeff Kirsher 		case MII_PHYSID2:
6104dee1ad47SJeff Kirsher 			data->val_out = (adapter->hw.phy.id & 0xFFFF);
6105dee1ad47SJeff Kirsher 			break;
6106dee1ad47SJeff Kirsher 		case MII_ADVERTISE:
6107dee1ad47SJeff Kirsher 			data->val_out = adapter->phy_regs.advertise;
6108dee1ad47SJeff Kirsher 			break;
6109dee1ad47SJeff Kirsher 		case MII_LPA:
6110dee1ad47SJeff Kirsher 			data->val_out = adapter->phy_regs.lpa;
6111dee1ad47SJeff Kirsher 			break;
6112dee1ad47SJeff Kirsher 		case MII_EXPANSION:
6113dee1ad47SJeff Kirsher 			data->val_out = adapter->phy_regs.expansion;
6114dee1ad47SJeff Kirsher 			break;
6115dee1ad47SJeff Kirsher 		case MII_CTRL1000:
6116dee1ad47SJeff Kirsher 			data->val_out = adapter->phy_regs.ctrl1000;
6117dee1ad47SJeff Kirsher 			break;
6118dee1ad47SJeff Kirsher 		case MII_STAT1000:
6119dee1ad47SJeff Kirsher 			data->val_out = adapter->phy_regs.stat1000;
6120dee1ad47SJeff Kirsher 			break;
6121dee1ad47SJeff Kirsher 		case MII_ESTATUS:
6122dee1ad47SJeff Kirsher 			data->val_out = adapter->phy_regs.estatus;
6123dee1ad47SJeff Kirsher 			break;
6124dee1ad47SJeff Kirsher 		default:
6125dee1ad47SJeff Kirsher 			return -EIO;
6126dee1ad47SJeff Kirsher 		}
6127dee1ad47SJeff Kirsher 		break;
6128dee1ad47SJeff Kirsher 	case SIOCSMIIREG:
6129dee1ad47SJeff Kirsher 	default:
6130dee1ad47SJeff Kirsher 		return -EOPNOTSUPP;
6131dee1ad47SJeff Kirsher 	}
6132dee1ad47SJeff Kirsher 	return 0;
6133dee1ad47SJeff Kirsher }
6134dee1ad47SJeff Kirsher 
6135b67e1913SBruce Allan /**
613639da2cacSSasha Neftin  * e1000e_hwtstamp_set - control hardware time stamping
6137b67e1913SBruce Allan  * @netdev: network interface device structure
6138b50f7bcaSJesse Brandeburg  * @ifr: interface request
6139b67e1913SBruce Allan  *
6140b67e1913SBruce Allan  * Outgoing time stamping can be enabled and disabled. Play nice and
6141b67e1913SBruce Allan  * disable it when requested, although it shouldn't cause any overhead
6142b67e1913SBruce Allan  * when no packet needs it. At most one packet in the queue may be
6143b67e1913SBruce Allan  * marked for time stamping, otherwise it would be impossible to tell
6144b67e1913SBruce Allan  * for sure to which packet the hardware time stamp belongs.
6145b67e1913SBruce Allan  *
6146b67e1913SBruce Allan  * Incoming time stamping has to be configured via the hardware filters.
6147b67e1913SBruce Allan  * Not all combinations are supported, in particular event type has to be
6148b67e1913SBruce Allan  * specified. Matching the kind of event packet is not supported, with the
6149b67e1913SBruce Allan  * exception of "all V2 events regardless of level 2 or 4".
6150b67e1913SBruce Allan  **/
e1000e_hwtstamp_set(struct net_device * netdev,struct ifreq * ifr)61514e8cff64SBen Hutchings static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
6152b67e1913SBruce Allan {
6153b67e1913SBruce Allan 	struct e1000_adapter *adapter = netdev_priv(netdev);
6154b67e1913SBruce Allan 	struct hwtstamp_config config;
6155b67e1913SBruce Allan 	int ret_val;
6156b67e1913SBruce Allan 
6157b67e1913SBruce Allan 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
6158b67e1913SBruce Allan 		return -EFAULT;
6159b67e1913SBruce Allan 
616062d7e3a2SBen Hutchings 	ret_val = e1000e_config_hwtstamp(adapter, &config);
6161b67e1913SBruce Allan 	if (ret_val)
6162b67e1913SBruce Allan 		return ret_val;
6163b67e1913SBruce Allan 
6164d89777bfSBruce Allan 	switch (config.rx_filter) {
6165d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
6166d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
6167d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
6168d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
6169d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
6170d89777bfSBruce Allan 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
6171d89777bfSBruce Allan 		/* With V2 type filters which specify a Sync or Delay Request,
6172d89777bfSBruce Allan 		 * Path Delay Request/Response messages are also time stamped
6173d89777bfSBruce Allan 		 * by hardware so notify the caller the requested packets plus
6174d89777bfSBruce Allan 		 * some others are time stamped.
6175d89777bfSBruce Allan 		 */
6176d89777bfSBruce Allan 		config.rx_filter = HWTSTAMP_FILTER_SOME;
6177d89777bfSBruce Allan 		break;
6178d89777bfSBruce Allan 	default:
6179d89777bfSBruce Allan 		break;
6180d89777bfSBruce Allan 	}
6181d89777bfSBruce Allan 
6182b67e1913SBruce Allan 	return copy_to_user(ifr->ifr_data, &config,
6183b67e1913SBruce Allan 			    sizeof(config)) ? -EFAULT : 0;
6184b67e1913SBruce Allan }
6185b67e1913SBruce Allan 
e1000e_hwtstamp_get(struct net_device * netdev,struct ifreq * ifr)61864e8cff64SBen Hutchings static int e1000e_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
61874e8cff64SBen Hutchings {
61884e8cff64SBen Hutchings 	struct e1000_adapter *adapter = netdev_priv(netdev);
61894e8cff64SBen Hutchings 
61904e8cff64SBen Hutchings 	return copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
61914e8cff64SBen Hutchings 			    sizeof(adapter->hwtstamp_config)) ? -EFAULT : 0;
61924e8cff64SBen Hutchings }
61934e8cff64SBen Hutchings 
e1000_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)6194dee1ad47SJeff Kirsher static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6195dee1ad47SJeff Kirsher {
6196dee1ad47SJeff Kirsher 	switch (cmd) {
6197dee1ad47SJeff Kirsher 	case SIOCGMIIPHY:
6198dee1ad47SJeff Kirsher 	case SIOCGMIIREG:
6199dee1ad47SJeff Kirsher 	case SIOCSMIIREG:
6200dee1ad47SJeff Kirsher 		return e1000_mii_ioctl(netdev, ifr, cmd);
6201b67e1913SBruce Allan 	case SIOCSHWTSTAMP:
62024e8cff64SBen Hutchings 		return e1000e_hwtstamp_set(netdev, ifr);
62034e8cff64SBen Hutchings 	case SIOCGHWTSTAMP:
62044e8cff64SBen Hutchings 		return e1000e_hwtstamp_get(netdev, ifr);
6205dee1ad47SJeff Kirsher 	default:
6206dee1ad47SJeff Kirsher 		return -EOPNOTSUPP;
6207dee1ad47SJeff Kirsher 	}
6208dee1ad47SJeff Kirsher }
6209dee1ad47SJeff Kirsher 
e1000_init_phy_wakeup(struct e1000_adapter * adapter,u32 wufc)6210dee1ad47SJeff Kirsher static int e1000_init_phy_wakeup(struct e1000_adapter *adapter, u32 wufc)
6211dee1ad47SJeff Kirsher {
6212dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
621374f350eeSDavid Ertman 	u32 i, mac_reg, wuc;
6214dee1ad47SJeff Kirsher 	u16 phy_reg, wuc_enable;
621570806a7fSBruce Allan 	int retval;
6216dee1ad47SJeff Kirsher 
6217dee1ad47SJeff Kirsher 	/* copy MAC RARs to PHY RARs */
6218dee1ad47SJeff Kirsher 	e1000_copy_rx_addrs_to_phy_ich8lan(hw);
6219dee1ad47SJeff Kirsher 
6220dee1ad47SJeff Kirsher 	retval = hw->phy.ops.acquire(hw);
6221dee1ad47SJeff Kirsher 	if (retval) {
6222dee1ad47SJeff Kirsher 		e_err("Could not acquire PHY\n");
6223dee1ad47SJeff Kirsher 		return retval;
6224dee1ad47SJeff Kirsher 	}
6225dee1ad47SJeff Kirsher 
6226dee1ad47SJeff Kirsher 	/* Enable access to wakeup registers on and set page to BM_WUC_PAGE */
6227dee1ad47SJeff Kirsher 	retval = e1000_enable_phy_wakeup_reg_access_bm(hw, &wuc_enable);
6228dee1ad47SJeff Kirsher 	if (retval)
622975ce1532SBruce Allan 		goto release;
6230dee1ad47SJeff Kirsher 
6231dee1ad47SJeff Kirsher 	/* copy MAC MTA to PHY MTA - only needed for pchlan */
6232dee1ad47SJeff Kirsher 	for (i = 0; i < adapter->hw.mac.mta_reg_count; i++) {
6233dee1ad47SJeff Kirsher 		mac_reg = E1000_READ_REG_ARRAY(hw, E1000_MTA, i);
6234dee1ad47SJeff Kirsher 		hw->phy.ops.write_reg_page(hw, BM_MTA(i),
6235dee1ad47SJeff Kirsher 					   (u16)(mac_reg & 0xFFFF));
6236dee1ad47SJeff Kirsher 		hw->phy.ops.write_reg_page(hw, BM_MTA(i) + 1,
6237dee1ad47SJeff Kirsher 					   (u16)((mac_reg >> 16) & 0xFFFF));
6238dee1ad47SJeff Kirsher 	}
6239dee1ad47SJeff Kirsher 
6240dee1ad47SJeff Kirsher 	/* configure PHY Rx Control register */
6241dee1ad47SJeff Kirsher 	hw->phy.ops.read_reg_page(&adapter->hw, BM_RCTL, &phy_reg);
6242dee1ad47SJeff Kirsher 	mac_reg = er32(RCTL);
6243dee1ad47SJeff Kirsher 	if (mac_reg & E1000_RCTL_UPE)
6244dee1ad47SJeff Kirsher 		phy_reg |= BM_RCTL_UPE;
6245dee1ad47SJeff Kirsher 	if (mac_reg & E1000_RCTL_MPE)
6246dee1ad47SJeff Kirsher 		phy_reg |= BM_RCTL_MPE;
6247dee1ad47SJeff Kirsher 	phy_reg &= ~(BM_RCTL_MO_MASK);
6248dee1ad47SJeff Kirsher 	if (mac_reg & E1000_RCTL_MO_3)
6249b9a45254SJesse Brandeburg 		phy_reg |= (FIELD_GET(E1000_RCTL_MO_3, mac_reg)
6250dee1ad47SJeff Kirsher 			    << BM_RCTL_MO_SHIFT);
6251dee1ad47SJeff Kirsher 	if (mac_reg & E1000_RCTL_BAM)
6252dee1ad47SJeff Kirsher 		phy_reg |= BM_RCTL_BAM;
6253dee1ad47SJeff Kirsher 	if (mac_reg & E1000_RCTL_PMCF)
6254dee1ad47SJeff Kirsher 		phy_reg |= BM_RCTL_PMCF;
6255dee1ad47SJeff Kirsher 	mac_reg = er32(CTRL);
6256dee1ad47SJeff Kirsher 	if (mac_reg & E1000_CTRL_RFCE)
6257dee1ad47SJeff Kirsher 		phy_reg |= BM_RCTL_RFCE;
6258dee1ad47SJeff Kirsher 	hw->phy.ops.write_reg_page(&adapter->hw, BM_RCTL, phy_reg);
6259dee1ad47SJeff Kirsher 
626074f350eeSDavid Ertman 	wuc = E1000_WUC_PME_EN;
626174f350eeSDavid Ertman 	if (wufc & (E1000_WUFC_MAG | E1000_WUFC_LNKC))
626274f350eeSDavid Ertman 		wuc |= E1000_WUC_APME;
626374f350eeSDavid Ertman 
6264dee1ad47SJeff Kirsher 	/* enable PHY wakeup in MAC register */
6265dee1ad47SJeff Kirsher 	ew32(WUFC, wufc);
626674f350eeSDavid Ertman 	ew32(WUC, (E1000_WUC_PHY_WAKE | E1000_WUC_APMPME |
626774f350eeSDavid Ertman 		   E1000_WUC_PME_STATUS | wuc));
6268dee1ad47SJeff Kirsher 
6269dee1ad47SJeff Kirsher 	/* configure and enable PHY wakeup in PHY registers */
6270dee1ad47SJeff Kirsher 	hw->phy.ops.write_reg_page(&adapter->hw, BM_WUFC, wufc);
627174f350eeSDavid Ertman 	hw->phy.ops.write_reg_page(&adapter->hw, BM_WUC, wuc);
6272dee1ad47SJeff Kirsher 
6273dee1ad47SJeff Kirsher 	/* activate PHY wakeup */
6274dee1ad47SJeff Kirsher 	wuc_enable |= BM_WUC_ENABLE_BIT | BM_WUC_HOST_WU_BIT;
6275dee1ad47SJeff Kirsher 	retval = e1000_disable_phy_wakeup_reg_access_bm(hw, &wuc_enable);
6276dee1ad47SJeff Kirsher 	if (retval)
6277dee1ad47SJeff Kirsher 		e_err("Could not set PHY Host Wakeup bit\n");
627875ce1532SBruce Allan release:
6279dee1ad47SJeff Kirsher 	hw->phy.ops.release(hw);
6280dee1ad47SJeff Kirsher 
6281dee1ad47SJeff Kirsher 	return retval;
6282dee1ad47SJeff Kirsher }
6283dee1ad47SJeff Kirsher 
e1000e_flush_lpic(struct pci_dev * pdev)62842a7e19afSDavid Ertman static void e1000e_flush_lpic(struct pci_dev *pdev)
62852a7e19afSDavid Ertman {
62862a7e19afSDavid Ertman 	struct net_device *netdev = pci_get_drvdata(pdev);
62872a7e19afSDavid Ertman 	struct e1000_adapter *adapter = netdev_priv(netdev);
62882a7e19afSDavid Ertman 	struct e1000_hw *hw = &adapter->hw;
62892a7e19afSDavid Ertman 	u32 ret_val;
62902a7e19afSDavid Ertman 
62912a7e19afSDavid Ertman 	pm_runtime_get_sync(netdev->dev.parent);
62922a7e19afSDavid Ertman 
62932a7e19afSDavid Ertman 	ret_val = hw->phy.ops.acquire(hw);
62942a7e19afSDavid Ertman 	if (ret_val)
62952a7e19afSDavid Ertman 		goto fl_out;
62962a7e19afSDavid Ertman 
62972a7e19afSDavid Ertman 	pr_info("EEE TX LPI TIMER: %08X\n",
62982a7e19afSDavid Ertman 		er32(LPIC) >> E1000_LPIC_LPIET_SHIFT);
62992a7e19afSDavid Ertman 
63002a7e19afSDavid Ertman 	hw->phy.ops.release(hw);
63012a7e19afSDavid Ertman 
63022a7e19afSDavid Ertman fl_out:
63032a7e19afSDavid Ertman 	pm_runtime_put_sync(netdev->dev.parent);
63042a7e19afSDavid Ertman }
63052a7e19afSDavid Ertman 
6306f15bb6ddSSasha Neftin /* S0ix implementation */
e1000e_s0ix_entry_flow(struct e1000_adapter * adapter)6307f15bb6ddSSasha Neftin static void e1000e_s0ix_entry_flow(struct e1000_adapter *adapter)
6308f15bb6ddSSasha Neftin {
6309f15bb6ddSSasha Neftin 	struct e1000_hw *hw = &adapter->hw;
6310f15bb6ddSSasha Neftin 	u32 mac_data;
6311f15bb6ddSSasha Neftin 	u16 phy_data;
6312f15bb6ddSSasha Neftin 
6313cad014b7SSasha Neftin 	if (er32(FWSM) & E1000_ICH_FWSM_FW_VALID &&
6314cad014b7SSasha Neftin 	    hw->mac.type >= e1000_pch_adp) {
63153e55d231SSasha Neftin 		/* Request ME configure the device for S0ix */
63163e55d231SSasha Neftin 		mac_data = er32(H2ME);
63173e55d231SSasha Neftin 		mac_data |= E1000_H2ME_START_DPG;
63183e55d231SSasha Neftin 		mac_data &= ~E1000_H2ME_EXIT_DPG;
63197bab8828SSasha Neftin 		trace_e1000e_trace_mac_register(mac_data);
63203e55d231SSasha Neftin 		ew32(H2ME, mac_data);
63213e55d231SSasha Neftin 	} else {
63223e55d231SSasha Neftin 		/* Request driver configure the device to S0ix */
6323f15bb6ddSSasha Neftin 		/* Disable the periodic inband message,
6324f15bb6ddSSasha Neftin 		 * don't request PCIe clock in K1 page770_17[10:9] = 10b
6325f15bb6ddSSasha Neftin 		 */
6326f15bb6ddSSasha Neftin 		e1e_rphy(hw, HV_PM_CTRL, &phy_data);
6327f15bb6ddSSasha Neftin 		phy_data &= ~HV_PM_CTRL_K1_CLK_REQ;
6328f15bb6ddSSasha Neftin 		phy_data |= BIT(10);
6329f15bb6ddSSasha Neftin 		e1e_wphy(hw, HV_PM_CTRL, phy_data);
6330f15bb6ddSSasha Neftin 
6331f15bb6ddSSasha Neftin 		/* Make sure we don't exit K1 every time a new packet arrives
6332f15bb6ddSSasha Neftin 		 * 772_29[5] = 1 CS_Mode_Stay_In_K1
6333f15bb6ddSSasha Neftin 		 */
6334f15bb6ddSSasha Neftin 		e1e_rphy(hw, I217_CGFREG, &phy_data);
6335f15bb6ddSSasha Neftin 		phy_data |= BIT(5);
6336f15bb6ddSSasha Neftin 		e1e_wphy(hw, I217_CGFREG, phy_data);
6337f15bb6ddSSasha Neftin 
6338f15bb6ddSSasha Neftin 		/* Change the MAC/PHY interface to SMBus
6339f15bb6ddSSasha Neftin 		 * Force the SMBus in PHY page769_23[0] = 1
6340f15bb6ddSSasha Neftin 		 * Force the SMBus in MAC CTRL_EXT[11] = 1
6341f15bb6ddSSasha Neftin 		 */
6342f15bb6ddSSasha Neftin 		e1e_rphy(hw, CV_SMB_CTRL, &phy_data);
6343f15bb6ddSSasha Neftin 		phy_data |= CV_SMB_CTRL_FORCE_SMBUS;
6344f15bb6ddSSasha Neftin 		e1e_wphy(hw, CV_SMB_CTRL, phy_data);
6345f15bb6ddSSasha Neftin 		mac_data = er32(CTRL_EXT);
6346f15bb6ddSSasha Neftin 		mac_data |= E1000_CTRL_EXT_FORCE_SMBUS;
6347f15bb6ddSSasha Neftin 		ew32(CTRL_EXT, mac_data);
6348f15bb6ddSSasha Neftin 
6349f15bb6ddSSasha Neftin 		/* DFT control: PHY bit: page769_20[0] = 1
63503ad3e28cSSasha Neftin 		 * page769_20[7] - PHY PLL stop
63513ad3e28cSSasha Neftin 		 * page769_20[8] - PHY go to the electrical idle
63523ad3e28cSSasha Neftin 		 * page769_20[9] - PHY serdes disable
6353f15bb6ddSSasha Neftin 		 * Gate PPW via EXTCNF_CTRL - set 0x0F00[7] = 1
6354f15bb6ddSSasha Neftin 		 */
6355f15bb6ddSSasha Neftin 		e1e_rphy(hw, I82579_DFT_CTRL, &phy_data);
6356f15bb6ddSSasha Neftin 		phy_data |= BIT(0);
63573ad3e28cSSasha Neftin 		phy_data |= BIT(7);
63583ad3e28cSSasha Neftin 		phy_data |= BIT(8);
63593ad3e28cSSasha Neftin 		phy_data |= BIT(9);
6360f15bb6ddSSasha Neftin 		e1e_wphy(hw, I82579_DFT_CTRL, phy_data);
6361f15bb6ddSSasha Neftin 
6362f15bb6ddSSasha Neftin 		mac_data = er32(EXTCNF_CTRL);
6363f15bb6ddSSasha Neftin 		mac_data |= E1000_EXTCNF_CTRL_GATE_PHY_CFG;
6364f15bb6ddSSasha Neftin 		ew32(EXTCNF_CTRL, mac_data);
6365f15bb6ddSSasha Neftin 
6366632fbd5eSVitaly Lifshits 		/* Disable disconnected cable conditioning for Power Gating */
6367632fbd5eSVitaly Lifshits 		mac_data = er32(DPGFR);
6368632fbd5eSVitaly Lifshits 		mac_data |= BIT(2);
6369632fbd5eSVitaly Lifshits 		ew32(DPGFR, mac_data);
6370632fbd5eSVitaly Lifshits 
6371c93a6f62SDima Ruinskiy 		/* Enable the Dynamic Clock Gating in the DMA and MAC */
6372c93a6f62SDima Ruinskiy 		mac_data = er32(CTRL_EXT);
6373c93a6f62SDima Ruinskiy 		mac_data |= E1000_CTRL_EXT_DMA_DYN_CLK_EN;
6374c93a6f62SDima Ruinskiy 		ew32(CTRL_EXT, mac_data);
6375c93a6f62SDima Ruinskiy 	}
6376c93a6f62SDima Ruinskiy 
6377c93a6f62SDima Ruinskiy 	/* Enable the Dynamic Power Gating in the MAC */
6378c93a6f62SDima Ruinskiy 	mac_data = er32(FEXTNVM7);
6379c93a6f62SDima Ruinskiy 	mac_data |= BIT(22);
6380c93a6f62SDima Ruinskiy 	ew32(FEXTNVM7, mac_data);
6381c93a6f62SDima Ruinskiy 
6382632fbd5eSVitaly Lifshits 	/* Don't wake from dynamic Power Gating with clock request */
6383632fbd5eSVitaly Lifshits 	mac_data = er32(FEXTNVM12);
6384632fbd5eSVitaly Lifshits 	mac_data |= BIT(12);
6385632fbd5eSVitaly Lifshits 	ew32(FEXTNVM12, mac_data);
6386632fbd5eSVitaly Lifshits 
6387632fbd5eSVitaly Lifshits 	/* Ungate PGCB clock */
6388632fbd5eSVitaly Lifshits 	mac_data = er32(FEXTNVM9);
6389a379b01cSVitaly Lifshits 	mac_data &= ~BIT(28);
6390632fbd5eSVitaly Lifshits 	ew32(FEXTNVM9, mac_data);
6391632fbd5eSVitaly Lifshits 
6392632fbd5eSVitaly Lifshits 	/* Enable K1 off to enable mPHY Power Gating */
6393632fbd5eSVitaly Lifshits 	mac_data = er32(FEXTNVM6);
6394632fbd5eSVitaly Lifshits 	mac_data |= BIT(31);
6395a379b01cSVitaly Lifshits 	ew32(FEXTNVM6, mac_data);
6396632fbd5eSVitaly Lifshits 
6397632fbd5eSVitaly Lifshits 	/* Enable mPHY power gating for any link and speed */
6398632fbd5eSVitaly Lifshits 	mac_data = er32(FEXTNVM8);
6399632fbd5eSVitaly Lifshits 	mac_data |= BIT(9);
6400632fbd5eSVitaly Lifshits 	ew32(FEXTNVM8, mac_data);
6401632fbd5eSVitaly Lifshits 
6402f15bb6ddSSasha Neftin 	/* No MAC DPG gating SLP_S0 in modern standby
6403f15bb6ddSSasha Neftin 	 * Switch the logic of the lanphypc to use PMC counter
6404f15bb6ddSSasha Neftin 	 */
6405f15bb6ddSSasha Neftin 	mac_data = er32(FEXTNVM5);
6406f15bb6ddSSasha Neftin 	mac_data |= BIT(7);
6407f15bb6ddSSasha Neftin 	ew32(FEXTNVM5, mac_data);
6408f15bb6ddSSasha Neftin 
64093e55d231SSasha Neftin 	/* Disable the time synchronization clock */
64103e55d231SSasha Neftin 	mac_data = er32(FEXTNVM7);
64113e55d231SSasha Neftin 	mac_data |= BIT(31);
64123e55d231SSasha Neftin 	mac_data &= ~BIT(0);
64133e55d231SSasha Neftin 	ew32(FEXTNVM7, mac_data);
64143e55d231SSasha Neftin 
64153e55d231SSasha Neftin 	/* Dynamic Power Gating Enable */
64163e55d231SSasha Neftin 	mac_data = er32(CTRL_EXT);
64173e55d231SSasha Neftin 	mac_data |= BIT(3);
64183e55d231SSasha Neftin 	ew32(CTRL_EXT, mac_data);
64193e55d231SSasha Neftin 
64203e55d231SSasha Neftin 	/* Check MAC Tx/Rx packet buffer pointers.
64213e55d231SSasha Neftin 	 * Reset MAC Tx/Rx packet buffer pointers to suppress any
64223e55d231SSasha Neftin 	 * pending traffic indication that would prevent power gating.
64233e55d231SSasha Neftin 	 */
64243e55d231SSasha Neftin 	mac_data = er32(TDFH);
64253e55d231SSasha Neftin 	if (mac_data)
64263e55d231SSasha Neftin 		ew32(TDFH, 0);
64273e55d231SSasha Neftin 	mac_data = er32(TDFT);
64283e55d231SSasha Neftin 	if (mac_data)
64293e55d231SSasha Neftin 		ew32(TDFT, 0);
64303e55d231SSasha Neftin 	mac_data = er32(TDFHS);
64313e55d231SSasha Neftin 	if (mac_data)
64323e55d231SSasha Neftin 		ew32(TDFHS, 0);
64333e55d231SSasha Neftin 	mac_data = er32(TDFTS);
64343e55d231SSasha Neftin 	if (mac_data)
64353e55d231SSasha Neftin 		ew32(TDFTS, 0);
64363e55d231SSasha Neftin 	mac_data = er32(TDFPC);
64373e55d231SSasha Neftin 	if (mac_data)
64383e55d231SSasha Neftin 		ew32(TDFPC, 0);
64393e55d231SSasha Neftin 	mac_data = er32(RDFH);
64403e55d231SSasha Neftin 	if (mac_data)
64413e55d231SSasha Neftin 		ew32(RDFH, 0);
64423e55d231SSasha Neftin 	mac_data = er32(RDFT);
64433e55d231SSasha Neftin 	if (mac_data)
64443e55d231SSasha Neftin 		ew32(RDFT, 0);
64453e55d231SSasha Neftin 	mac_data = er32(RDFHS);
64463e55d231SSasha Neftin 	if (mac_data)
64473e55d231SSasha Neftin 		ew32(RDFHS, 0);
64483e55d231SSasha Neftin 	mac_data = er32(RDFTS);
64493e55d231SSasha Neftin 	if (mac_data)
64503e55d231SSasha Neftin 		ew32(RDFTS, 0);
64513e55d231SSasha Neftin 	mac_data = er32(RDFPC);
64523e55d231SSasha Neftin 	if (mac_data)
64533e55d231SSasha Neftin 		ew32(RDFPC, 0);
64543e55d231SSasha Neftin }
64553e55d231SSasha Neftin 
e1000e_s0ix_exit_flow(struct e1000_adapter * adapter)6456f15bb6ddSSasha Neftin static void e1000e_s0ix_exit_flow(struct e1000_adapter *adapter)
6457f15bb6ddSSasha Neftin {
6458f15bb6ddSSasha Neftin 	struct e1000_hw *hw = &adapter->hw;
6459ef407b86SSasha Neftin 	bool firmware_bug = false;
6460f15bb6ddSSasha Neftin 	u32 mac_data;
6461f15bb6ddSSasha Neftin 	u16 phy_data;
6462ef407b86SSasha Neftin 	u32 i = 0;
6463f15bb6ddSSasha Neftin 
6464cad014b7SSasha Neftin 	if (er32(FWSM) & E1000_ICH_FWSM_FW_VALID &&
6465cad014b7SSasha Neftin 	    hw->mac.type >= e1000_pch_adp) {
6466b49feacbSSasha Neftin 		/* Keep the GPT clock enabled for CSME */
6467b49feacbSSasha Neftin 		mac_data = er32(FEXTNVM);
6468b49feacbSSasha Neftin 		mac_data |= BIT(3);
6469b49feacbSSasha Neftin 		ew32(FEXTNVM, mac_data);
64703e55d231SSasha Neftin 		/* Request ME unconfigure the device from S0ix */
64713e55d231SSasha Neftin 		mac_data = er32(H2ME);
64723e55d231SSasha Neftin 		mac_data &= ~E1000_H2ME_START_DPG;
64733e55d231SSasha Neftin 		mac_data |= E1000_H2ME_EXIT_DPG;
64747bab8828SSasha Neftin 		trace_e1000e_trace_mac_register(mac_data);
64753e55d231SSasha Neftin 		ew32(H2ME, mac_data);
6476ef407b86SSasha Neftin 
6477ef407b86SSasha Neftin 		/* Poll up to 2.5 seconds for ME to unconfigure DPG.
6478ef407b86SSasha Neftin 		 * If this takes more than 1 second, show a warning indicating a
6479ef407b86SSasha Neftin 		 * firmware bug
6480ef407b86SSasha Neftin 		 */
6481ef407b86SSasha Neftin 		while (!(er32(EXFWSM) & E1000_EXFWSM_DPG_EXIT_DONE)) {
6482ef407b86SSasha Neftin 			if (i > 100 && !firmware_bug)
6483ef407b86SSasha Neftin 				firmware_bug = true;
6484ef407b86SSasha Neftin 
6485ef407b86SSasha Neftin 			if (i++ == 250) {
6486ef407b86SSasha Neftin 				e_dbg("Timeout (firmware bug): %d msec\n",
6487ef407b86SSasha Neftin 				      i * 10);
6488ef407b86SSasha Neftin 				break;
6489ef407b86SSasha Neftin 			}
6490ef407b86SSasha Neftin 
6491ef407b86SSasha Neftin 			usleep_range(10000, 11000);
6492ef407b86SSasha Neftin 		}
6493ef407b86SSasha Neftin 		if (firmware_bug)
6494ef407b86SSasha Neftin 			e_warn("DPG_EXIT_DONE took %d msec. This is a firmware bug\n",
6495ef407b86SSasha Neftin 			       i * 10);
6496ef407b86SSasha Neftin 		else
6497ef407b86SSasha Neftin 			e_dbg("DPG_EXIT_DONE cleared after %d msec\n", i * 10);
64983e55d231SSasha Neftin 	} else {
64993e55d231SSasha Neftin 		/* Request driver unconfigure the device from S0ix */
65003e55d231SSasha Neftin 
6501632fbd5eSVitaly Lifshits 		/* Cancel disable disconnected cable conditioning
6502632fbd5eSVitaly Lifshits 		 * for Power Gating
6503632fbd5eSVitaly Lifshits 		 */
6504632fbd5eSVitaly Lifshits 		mac_data = er32(DPGFR);
6505632fbd5eSVitaly Lifshits 		mac_data &= ~BIT(2);
6506632fbd5eSVitaly Lifshits 		ew32(DPGFR, mac_data);
6507632fbd5eSVitaly Lifshits 
6508f15bb6ddSSasha Neftin 		/* Disable the Dynamic Clock Gating in the DMA and MAC */
6509f15bb6ddSSasha Neftin 		mac_data = er32(CTRL_EXT);
6510f15bb6ddSSasha Neftin 		mac_data &= 0xFFF7FFFF;
6511f15bb6ddSSasha Neftin 		ew32(CTRL_EXT, mac_data);
6512f15bb6ddSSasha Neftin 
6513f15bb6ddSSasha Neftin 		/* Enable the periodic inband message,
6514f15bb6ddSSasha Neftin 		 * Request PCIe clock in K1 page770_17[10:9] =01b
6515f15bb6ddSSasha Neftin 		 */
6516f15bb6ddSSasha Neftin 		e1e_rphy(hw, HV_PM_CTRL, &phy_data);
6517f15bb6ddSSasha Neftin 		phy_data &= 0xFBFF;
6518f15bb6ddSSasha Neftin 		phy_data |= HV_PM_CTRL_K1_CLK_REQ;
6519f15bb6ddSSasha Neftin 		e1e_wphy(hw, HV_PM_CTRL, phy_data);
6520f15bb6ddSSasha Neftin 
6521f15bb6ddSSasha Neftin 		/* Return back configuration
6522f15bb6ddSSasha Neftin 		 * 772_29[5] = 0 CS_Mode_Stay_In_K1
6523f15bb6ddSSasha Neftin 		 */
6524f15bb6ddSSasha Neftin 		e1e_rphy(hw, I217_CGFREG, &phy_data);
6525f15bb6ddSSasha Neftin 		phy_data &= 0xFFDF;
6526f15bb6ddSSasha Neftin 		e1e_wphy(hw, I217_CGFREG, phy_data);
6527f15bb6ddSSasha Neftin 
6528f15bb6ddSSasha Neftin 		/* Change the MAC/PHY interface to Kumeran
6529f15bb6ddSSasha Neftin 		 * Unforce the SMBus in PHY page769_23[0] = 0
6530f15bb6ddSSasha Neftin 		 * Unforce the SMBus in MAC CTRL_EXT[11] = 0
6531f15bb6ddSSasha Neftin 		 */
6532f15bb6ddSSasha Neftin 		e1e_rphy(hw, CV_SMB_CTRL, &phy_data);
6533f15bb6ddSSasha Neftin 		phy_data &= ~CV_SMB_CTRL_FORCE_SMBUS;
6534f15bb6ddSSasha Neftin 		e1e_wphy(hw, CV_SMB_CTRL, phy_data);
6535f15bb6ddSSasha Neftin 		mac_data = er32(CTRL_EXT);
6536f15bb6ddSSasha Neftin 		mac_data &= ~E1000_CTRL_EXT_FORCE_SMBUS;
6537f15bb6ddSSasha Neftin 		ew32(CTRL_EXT, mac_data);
6538f15bb6ddSSasha Neftin 	}
6539f15bb6ddSSasha Neftin 
65403e55d231SSasha Neftin 	/* Disable Dynamic Power Gating */
65413e55d231SSasha Neftin 	mac_data = er32(CTRL_EXT);
65423e55d231SSasha Neftin 	mac_data &= 0xFFFFFFF7;
65433e55d231SSasha Neftin 	ew32(CTRL_EXT, mac_data);
65443e55d231SSasha Neftin 
65453e55d231SSasha Neftin 	/* Enable the time synchronization clock */
65463e55d231SSasha Neftin 	mac_data = er32(FEXTNVM7);
65473e55d231SSasha Neftin 	mac_data &= ~BIT(31);
65483e55d231SSasha Neftin 	mac_data |= BIT(0);
65493e55d231SSasha Neftin 	ew32(FEXTNVM7, mac_data);
6550c93a6f62SDima Ruinskiy 
6551c93a6f62SDima Ruinskiy 	/* Disable the Dynamic Power Gating in the MAC */
6552c93a6f62SDima Ruinskiy 	mac_data = er32(FEXTNVM7);
6553c93a6f62SDima Ruinskiy 	mac_data &= 0xFFBFFFFF;
6554c93a6f62SDima Ruinskiy 	ew32(FEXTNVM7, mac_data);
6555c93a6f62SDima Ruinskiy 
6556c93a6f62SDima Ruinskiy 	/* Disable mPHY power gating for any link and speed */
6557c93a6f62SDima Ruinskiy 	mac_data = er32(FEXTNVM8);
6558c93a6f62SDima Ruinskiy 	mac_data &= ~BIT(9);
6559c93a6f62SDima Ruinskiy 	ew32(FEXTNVM8, mac_data);
6560c93a6f62SDima Ruinskiy 
6561c93a6f62SDima Ruinskiy 	/* Disable K1 off */
6562c93a6f62SDima Ruinskiy 	mac_data = er32(FEXTNVM6);
6563c93a6f62SDima Ruinskiy 	mac_data &= ~BIT(31);
6564c93a6f62SDima Ruinskiy 	ew32(FEXTNVM6, mac_data);
6565c93a6f62SDima Ruinskiy 
6566c93a6f62SDima Ruinskiy 	/* Disable Ungate PGCB clock */
6567c93a6f62SDima Ruinskiy 	mac_data = er32(FEXTNVM9);
6568c93a6f62SDima Ruinskiy 	mac_data |= BIT(28);
6569c93a6f62SDima Ruinskiy 	ew32(FEXTNVM9, mac_data);
6570c93a6f62SDima Ruinskiy 
6571c93a6f62SDima Ruinskiy 	/* Cancel not waking from dynamic
6572c93a6f62SDima Ruinskiy 	 * Power Gating with clock request
6573c93a6f62SDima Ruinskiy 	 */
6574c93a6f62SDima Ruinskiy 	mac_data = er32(FEXTNVM12);
6575c93a6f62SDima Ruinskiy 	mac_data &= ~BIT(12);
6576c93a6f62SDima Ruinskiy 	ew32(FEXTNVM12, mac_data);
6577c93a6f62SDima Ruinskiy 
6578c93a6f62SDima Ruinskiy 	/* Revert the lanphypc logic to use the internal Gbe counter
6579c93a6f62SDima Ruinskiy 	 * and not the PMC counter
6580c93a6f62SDima Ruinskiy 	 */
6581c93a6f62SDima Ruinskiy 	mac_data = er32(FEXTNVM5);
6582c93a6f62SDima Ruinskiy 	mac_data &= 0xFFFFFF7F;
6583c93a6f62SDima Ruinskiy 	ew32(FEXTNVM5, mac_data);
65843e55d231SSasha Neftin }
65853e55d231SSasha Neftin 
e1000e_pm_freeze(struct device * dev)658628002099SDavid Ertman static int e1000e_pm_freeze(struct device *dev)
6587dee1ad47SJeff Kirsher {
6588ee2e80c1SChuhong Yuan 	struct net_device *netdev = dev_get_drvdata(dev);
6589dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
6590a7023819SAlexander Duyck 	bool present;
6591dee1ad47SJeff Kirsher 
6592a7023819SAlexander Duyck 	rtnl_lock();
6593a7023819SAlexander Duyck 
6594a7023819SAlexander Duyck 	present = netif_device_present(netdev);
6595dee1ad47SJeff Kirsher 	netif_device_detach(netdev);
6596dee1ad47SJeff Kirsher 
6597a7023819SAlexander Duyck 	if (present && netif_running(netdev)) {
6598bb9e44d0SBruce Allan 		int count = E1000_CHECK_RESET_COUNT;
6599bb9e44d0SBruce Allan 
6600bb9e44d0SBruce Allan 		while (test_bit(__E1000_RESETTING, &adapter->state) && count--)
6601ab6973aeSArjan van de Ven 			usleep_range(10000, 11000);
6602bb9e44d0SBruce Allan 
6603dee1ad47SJeff Kirsher 		WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
660428002099SDavid Ertman 
660528002099SDavid Ertman 		/* Quiesce the device without resetting the hardware */
660628002099SDavid Ertman 		e1000e_down(adapter, false);
6607dee1ad47SJeff Kirsher 		e1000_free_irq(adapter);
66087e54d9d0Skhalidm 	}
6609a7023819SAlexander Duyck 	rtnl_unlock();
6610a7023819SAlexander Duyck 
66119f47a48eSJeff Kirsher 	e1000e_reset_interrupt_capability(adapter);
6612dee1ad47SJeff Kirsher 
661328002099SDavid Ertman 	/* Allow time for pending master requests to run */
661428002099SDavid Ertman 	e1000e_disable_pcie_master(&adapter->hw);
661528002099SDavid Ertman 
661628002099SDavid Ertman 	return 0;
661728002099SDavid Ertman }
661828002099SDavid Ertman 
__e1000_shutdown(struct pci_dev * pdev,bool runtime)661928002099SDavid Ertman static int __e1000_shutdown(struct pci_dev *pdev, bool runtime)
662028002099SDavid Ertman {
662128002099SDavid Ertman 	struct net_device *netdev = pci_get_drvdata(pdev);
662228002099SDavid Ertman 	struct e1000_adapter *adapter = netdev_priv(netdev);
662328002099SDavid Ertman 	struct e1000_hw *hw = &adapter->hw;
66246bf6be11SChen Yu 	u32 ctrl, ctrl_ext, rctl, status, wufc;
662528002099SDavid Ertman 	int retval = 0;
662628002099SDavid Ertman 
66276bf6be11SChen Yu 	/* Runtime suspend should only enable wakeup for link changes */
66286bf6be11SChen Yu 	if (runtime)
66296bf6be11SChen Yu 		wufc = E1000_WUFC_LNKC;
66306bf6be11SChen Yu 	else if (device_may_wakeup(&pdev->dev))
66316bf6be11SChen Yu 		wufc = adapter->wol;
66326bf6be11SChen Yu 	else
66336bf6be11SChen Yu 		wufc = 0;
66346bf6be11SChen Yu 
6635dee1ad47SJeff Kirsher 	status = er32(STATUS);
6636dee1ad47SJeff Kirsher 	if (status & E1000_STATUS_LU)
6637dee1ad47SJeff Kirsher 		wufc &= ~E1000_WUFC_LNKC;
6638dee1ad47SJeff Kirsher 
6639dee1ad47SJeff Kirsher 	if (wufc) {
6640dee1ad47SJeff Kirsher 		e1000_setup_rctl(adapter);
6641ef9b965aSJesse Brandeburg 		e1000e_set_rx_mode(netdev);
6642dee1ad47SJeff Kirsher 
6643dee1ad47SJeff Kirsher 		/* turn on all-multi mode if wake on multicast is enabled */
6644dee1ad47SJeff Kirsher 		if (wufc & E1000_WUFC_MC) {
6645dee1ad47SJeff Kirsher 			rctl = er32(RCTL);
6646dee1ad47SJeff Kirsher 			rctl |= E1000_RCTL_MPE;
6647dee1ad47SJeff Kirsher 			ew32(RCTL, rctl);
6648dee1ad47SJeff Kirsher 		}
6649dee1ad47SJeff Kirsher 
6650dee1ad47SJeff Kirsher 		ctrl = er32(CTRL);
6651dee1ad47SJeff Kirsher 		ctrl |= E1000_CTRL_ADVD3WUC;
6652dee1ad47SJeff Kirsher 		if (!(adapter->flags2 & FLAG2_HAS_PHY_WAKEUP))
6653dee1ad47SJeff Kirsher 			ctrl |= E1000_CTRL_EN_PHY_PWR_MGMT;
6654dee1ad47SJeff Kirsher 		ew32(CTRL, ctrl);
6655dee1ad47SJeff Kirsher 
6656dee1ad47SJeff Kirsher 		if (adapter->hw.phy.media_type == e1000_media_type_fiber ||
6657dee1ad47SJeff Kirsher 		    adapter->hw.phy.media_type ==
6658dee1ad47SJeff Kirsher 		    e1000_media_type_internal_serdes) {
6659dee1ad47SJeff Kirsher 			/* keep the laser running in D3 */
6660dee1ad47SJeff Kirsher 			ctrl_ext = er32(CTRL_EXT);
6661dee1ad47SJeff Kirsher 			ctrl_ext |= E1000_CTRL_EXT_SDP3_DATA;
6662dee1ad47SJeff Kirsher 			ew32(CTRL_EXT, ctrl_ext);
6663dee1ad47SJeff Kirsher 		}
6664dee1ad47SJeff Kirsher 
666563eb48f1SDavid Ertman 		if (!runtime)
666663eb48f1SDavid Ertman 			e1000e_power_up_phy(adapter);
666763eb48f1SDavid Ertman 
6668dee1ad47SJeff Kirsher 		if (adapter->flags & FLAG_IS_ICH)
6669dee1ad47SJeff Kirsher 			e1000_suspend_workarounds_ich8lan(&adapter->hw);
6670dee1ad47SJeff Kirsher 
6671dee1ad47SJeff Kirsher 		if (adapter->flags2 & FLAG2_HAS_PHY_WAKEUP) {
6672dee1ad47SJeff Kirsher 			/* enable wakeup by the PHY */
6673dee1ad47SJeff Kirsher 			retval = e1000_init_phy_wakeup(adapter, wufc);
66740a6ad4d9SVitaly Lifshits 			if (retval) {
66750a6ad4d9SVitaly Lifshits 				e_err("Failed to enable wakeup\n");
66760a6ad4d9SVitaly Lifshits 				goto skip_phy_configurations;
66770a6ad4d9SVitaly Lifshits 			}
6678dee1ad47SJeff Kirsher 		} else {
6679dee1ad47SJeff Kirsher 			/* enable wakeup by the MAC */
6680dee1ad47SJeff Kirsher 			ew32(WUFC, wufc);
6681dee1ad47SJeff Kirsher 			ew32(WUC, E1000_WUC_PME_EN);
6682dee1ad47SJeff Kirsher 		}
6683dee1ad47SJeff Kirsher 	} else {
6684dee1ad47SJeff Kirsher 		ew32(WUC, 0);
6685dee1ad47SJeff Kirsher 		ew32(WUFC, 0);
668628002099SDavid Ertman 
668728002099SDavid Ertman 		e1000_power_down_phy(adapter);
6688dee1ad47SJeff Kirsher 	}
6689dee1ad47SJeff Kirsher 
669074f350eeSDavid Ertman 	if (adapter->hw.phy.type == e1000_phy_igp_3) {
6691dee1ad47SJeff Kirsher 		e1000e_igp3_phy_powerdown_workaround_ich8lan(&adapter->hw);
6692c8744f44SSasha Neftin 	} else if (hw->mac.type >= e1000_pch_lpt) {
6693662200e3SVitaly Lifshits 		if (wufc && !(wufc & (E1000_WUFC_EX | E1000_WUFC_MC | E1000_WUFC_BC))) {
669474f350eeSDavid Ertman 			/* ULP does not support wake from unicast, multicast
669574f350eeSDavid Ertman 			 * or broadcast.
669674f350eeSDavid Ertman 			 */
669774f350eeSDavid Ertman 			retval = e1000_enable_ulp_lpt_lp(hw, !runtime);
66980a6ad4d9SVitaly Lifshits 			if (retval) {
66990a6ad4d9SVitaly Lifshits 				e_err("Failed to enable ULP\n");
67000a6ad4d9SVitaly Lifshits 				goto skip_phy_configurations;
67010a6ad4d9SVitaly Lifshits 			}
670274f350eeSDavid Ertman 		}
6703662200e3SVitaly Lifshits 	}
670474f350eeSDavid Ertman 
6705f5ac7445SRaanan Avargil 	/* Ensure that the appropriate bits are set in LPI_CTRL
6706f5ac7445SRaanan Avargil 	 * for EEE in Sx
6707f5ac7445SRaanan Avargil 	 */
6708f5ac7445SRaanan Avargil 	if ((hw->phy.type >= e1000_phy_i217) &&
6709f5ac7445SRaanan Avargil 	    adapter->eee_advert && hw->dev_spec.ich8lan.eee_lp_ability) {
6710f5ac7445SRaanan Avargil 		u16 lpi_ctrl = 0;
6711f5ac7445SRaanan Avargil 
6712f5ac7445SRaanan Avargil 		retval = hw->phy.ops.acquire(hw);
6713f5ac7445SRaanan Avargil 		if (!retval) {
6714f5ac7445SRaanan Avargil 			retval = e1e_rphy_locked(hw, I82579_LPI_CTRL,
6715f5ac7445SRaanan Avargil 						 &lpi_ctrl);
6716f5ac7445SRaanan Avargil 			if (!retval) {
6717f5ac7445SRaanan Avargil 				if (adapter->eee_advert &
6718f5ac7445SRaanan Avargil 				    hw->dev_spec.ich8lan.eee_lp_ability &
6719f5ac7445SRaanan Avargil 				    I82579_EEE_100_SUPPORTED)
6720f5ac7445SRaanan Avargil 					lpi_ctrl |= I82579_LPI_CTRL_100_ENABLE;
6721f5ac7445SRaanan Avargil 				if (adapter->eee_advert &
6722f5ac7445SRaanan Avargil 				    hw->dev_spec.ich8lan.eee_lp_ability &
6723f5ac7445SRaanan Avargil 				    I82579_EEE_1000_SUPPORTED)
6724f5ac7445SRaanan Avargil 					lpi_ctrl |= I82579_LPI_CTRL_1000_ENABLE;
6725f5ac7445SRaanan Avargil 
6726f5ac7445SRaanan Avargil 				retval = e1e_wphy_locked(hw, I82579_LPI_CTRL,
6727f5ac7445SRaanan Avargil 							 lpi_ctrl);
6728f5ac7445SRaanan Avargil 			}
6729f5ac7445SRaanan Avargil 		}
6730f5ac7445SRaanan Avargil 		hw->phy.ops.release(hw);
6731f5ac7445SRaanan Avargil 	}
6732dee1ad47SJeff Kirsher 
67330a6ad4d9SVitaly Lifshits skip_phy_configurations:
6734e921eb1aSBruce Allan 	/* Release control of h/w to f/w.  If f/w is AMT enabled, this
6735dee1ad47SJeff Kirsher 	 * would have already happened in close and is redundant.
6736dee1ad47SJeff Kirsher 	 */
6737dee1ad47SJeff Kirsher 	e1000e_release_hw_control(adapter);
6738dee1ad47SJeff Kirsher 
673924b41c97SDean Nelson 	pci_clear_master(pdev);
674024b41c97SDean Nelson 
6741e921eb1aSBruce Allan 	/* The pci-e switch on some quad port adapters will report a
6742dee1ad47SJeff Kirsher 	 * correctable error when the MAC transitions from D0 to D3.  To
6743dee1ad47SJeff Kirsher 	 * prevent this we need to mask off the correctable errors on the
6744dee1ad47SJeff Kirsher 	 * downstream port of the pci-e switch.
6745e8c254c5SLi Zhang 	 *
6746e8c254c5SLi Zhang 	 * We don't have the associated upstream bridge while assigning
6747e8c254c5SLi Zhang 	 * the PCI device into guest. For example, the KVM on power is
6748e8c254c5SLi Zhang 	 * one of the cases.
6749dee1ad47SJeff Kirsher 	 */
6750dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_IS_QUAD_PORT) {
6751dee1ad47SJeff Kirsher 		struct pci_dev *us_dev = pdev->bus->self;
6752dee1ad47SJeff Kirsher 		u16 devctl;
6753dee1ad47SJeff Kirsher 
6754e8c254c5SLi Zhang 		if (!us_dev)
6755e8c254c5SLi Zhang 			return 0;
6756e8c254c5SLi Zhang 
6757f8c0fcacSJiang Liu 		pcie_capability_read_word(us_dev, PCI_EXP_DEVCTL, &devctl);
6758f8c0fcacSJiang Liu 		pcie_capability_write_word(us_dev, PCI_EXP_DEVCTL,
6759dee1ad47SJeff Kirsher 					   (devctl & ~PCI_EXP_DEVCTL_CERE));
6760dee1ad47SJeff Kirsher 
676166148babSKonstantin Khlebnikov 		pci_save_state(pdev);
676266148babSKonstantin Khlebnikov 		pci_prepare_to_sleep(pdev);
6763dee1ad47SJeff Kirsher 
6764f8c0fcacSJiang Liu 		pcie_capability_write_word(us_dev, PCI_EXP_DEVCTL, devctl);
6765dee1ad47SJeff Kirsher 	}
676666148babSKonstantin Khlebnikov 
676766148babSKonstantin Khlebnikov 	return 0;
6768dee1ad47SJeff Kirsher }
6769dee1ad47SJeff Kirsher 
677013129d9bSCarolyn Wyborny /**
6771beb0a152SYanir Lubetkin  * __e1000e_disable_aspm - Disable ASPM states
677213129d9bSCarolyn Wyborny  * @pdev: pointer to PCI device struct
677313129d9bSCarolyn Wyborny  * @state: bit-mask of ASPM states to disable
6774beb0a152SYanir Lubetkin  * @locked: indication if this context holds pci_bus_sem locked.
677513129d9bSCarolyn Wyborny  *
677613129d9bSCarolyn Wyborny  * Some devices *must* have certain ASPM states disabled per hardware errata.
677713129d9bSCarolyn Wyborny  **/
__e1000e_disable_aspm(struct pci_dev * pdev,u16 state,int locked)6778beb0a152SYanir Lubetkin static void __e1000e_disable_aspm(struct pci_dev *pdev, u16 state, int locked)
6779dee1ad47SJeff Kirsher {
678013129d9bSCarolyn Wyborny 	struct pci_dev *parent = pdev->bus->self;
678113129d9bSCarolyn Wyborny 	u16 aspm_dis_mask = 0;
678213129d9bSCarolyn Wyborny 	u16 pdev_aspmc, parent_aspmc;
6783ffe0b2ffSBjorn Helgaas 
678413129d9bSCarolyn Wyborny 	switch (state) {
678513129d9bSCarolyn Wyborny 	case PCIE_LINK_STATE_L0S:
678613129d9bSCarolyn Wyborny 	case PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1:
678713129d9bSCarolyn Wyborny 		aspm_dis_mask |= PCI_EXP_LNKCTL_ASPM_L0S;
67885463fce6SJeff Kirsher 		fallthrough; /* can't have L1 without L0s */
678913129d9bSCarolyn Wyborny 	case PCIE_LINK_STATE_L1:
679013129d9bSCarolyn Wyborny 		aspm_dis_mask |= PCI_EXP_LNKCTL_ASPM_L1;
679113129d9bSCarolyn Wyborny 		break;
679213129d9bSCarolyn Wyborny 	default:
679313129d9bSCarolyn Wyborny 		return;
679413129d9bSCarolyn Wyborny 	}
679513129d9bSCarolyn Wyborny 
679613129d9bSCarolyn Wyborny 	pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &pdev_aspmc);
679713129d9bSCarolyn Wyborny 	pdev_aspmc &= PCI_EXP_LNKCTL_ASPMC;
679813129d9bSCarolyn Wyborny 
679913129d9bSCarolyn Wyborny 	if (parent) {
680013129d9bSCarolyn Wyborny 		pcie_capability_read_word(parent, PCI_EXP_LNKCTL,
680113129d9bSCarolyn Wyborny 					  &parent_aspmc);
680213129d9bSCarolyn Wyborny 		parent_aspmc &= PCI_EXP_LNKCTL_ASPMC;
680313129d9bSCarolyn Wyborny 	}
680413129d9bSCarolyn Wyborny 
680513129d9bSCarolyn Wyborny 	/* Nothing to do if the ASPM states to be disabled already are */
680613129d9bSCarolyn Wyborny 	if (!(pdev_aspmc & aspm_dis_mask) &&
680713129d9bSCarolyn Wyborny 	    (!parent || !(parent_aspmc & aspm_dis_mask)))
680813129d9bSCarolyn Wyborny 		return;
680913129d9bSCarolyn Wyborny 
681013129d9bSCarolyn Wyborny 	dev_info(&pdev->dev, "Disabling ASPM %s %s\n",
681113129d9bSCarolyn Wyborny 		 (aspm_dis_mask & pdev_aspmc & PCI_EXP_LNKCTL_ASPM_L0S) ?
681213129d9bSCarolyn Wyborny 		 "L0s" : "",
681313129d9bSCarolyn Wyborny 		 (aspm_dis_mask & pdev_aspmc & PCI_EXP_LNKCTL_ASPM_L1) ?
681413129d9bSCarolyn Wyborny 		 "L1" : "");
681513129d9bSCarolyn Wyborny 
681613129d9bSCarolyn Wyborny #ifdef CONFIG_PCIEASPM
6817beb0a152SYanir Lubetkin 	if (locked)
681813129d9bSCarolyn Wyborny 		pci_disable_link_state_locked(pdev, state);
6819beb0a152SYanir Lubetkin 	else
6820beb0a152SYanir Lubetkin 		pci_disable_link_state(pdev, state);
682113129d9bSCarolyn Wyborny 
682213129d9bSCarolyn Wyborny 	/* Double-check ASPM control.  If not disabled by the above, the
682313129d9bSCarolyn Wyborny 	 * BIOS is preventing that from happening (or CONFIG_PCIEASPM is
682413129d9bSCarolyn Wyborny 	 * not enabled); override by writing PCI config space directly.
682513129d9bSCarolyn Wyborny 	 */
682613129d9bSCarolyn Wyborny 	pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &pdev_aspmc);
682713129d9bSCarolyn Wyborny 	pdev_aspmc &= PCI_EXP_LNKCTL_ASPMC;
682813129d9bSCarolyn Wyborny 
682913129d9bSCarolyn Wyborny 	if (!(aspm_dis_mask & pdev_aspmc))
683013129d9bSCarolyn Wyborny 		return;
683113129d9bSCarolyn Wyborny #endif
6832ffe0b2ffSBjorn Helgaas 
6833e921eb1aSBruce Allan 	/* Both device and parent should have the same ASPM setting.
6834dee1ad47SJeff Kirsher 	 * Disable ASPM in downstream component first and then upstream.
6835dee1ad47SJeff Kirsher 	 */
683613129d9bSCarolyn Wyborny 	pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL, aspm_dis_mask);
6837dee1ad47SJeff Kirsher 
683813129d9bSCarolyn Wyborny 	if (parent)
683913129d9bSCarolyn Wyborny 		pcie_capability_clear_word(parent, PCI_EXP_LNKCTL,
684013129d9bSCarolyn Wyborny 					   aspm_dis_mask);
6841dee1ad47SJeff Kirsher }
6842dee1ad47SJeff Kirsher 
6843beb0a152SYanir Lubetkin /**
6844beb0a152SYanir Lubetkin  * e1000e_disable_aspm - Disable ASPM states.
6845beb0a152SYanir Lubetkin  * @pdev: pointer to PCI device struct
6846beb0a152SYanir Lubetkin  * @state: bit-mask of ASPM states to disable
6847beb0a152SYanir Lubetkin  *
6848beb0a152SYanir Lubetkin  * This function acquires the pci_bus_sem!
6849beb0a152SYanir Lubetkin  * Some devices *must* have certain ASPM states disabled per hardware errata.
6850beb0a152SYanir Lubetkin  **/
e1000e_disable_aspm(struct pci_dev * pdev,u16 state)6851beb0a152SYanir Lubetkin static void e1000e_disable_aspm(struct pci_dev *pdev, u16 state)
6852beb0a152SYanir Lubetkin {
6853beb0a152SYanir Lubetkin 	__e1000e_disable_aspm(pdev, state, 0);
6854beb0a152SYanir Lubetkin }
6855beb0a152SYanir Lubetkin 
6856beb0a152SYanir Lubetkin /**
685739da2cacSSasha Neftin  * e1000e_disable_aspm_locked - Disable ASPM states.
6858beb0a152SYanir Lubetkin  * @pdev: pointer to PCI device struct
6859beb0a152SYanir Lubetkin  * @state: bit-mask of ASPM states to disable
6860beb0a152SYanir Lubetkin  *
6861beb0a152SYanir Lubetkin  * This function must be called with pci_bus_sem acquired!
6862beb0a152SYanir Lubetkin  * Some devices *must* have certain ASPM states disabled per hardware errata.
6863beb0a152SYanir Lubetkin  **/
e1000e_disable_aspm_locked(struct pci_dev * pdev,u16 state)6864beb0a152SYanir Lubetkin static void e1000e_disable_aspm_locked(struct pci_dev *pdev, u16 state)
6865beb0a152SYanir Lubetkin {
6866beb0a152SYanir Lubetkin 	__e1000e_disable_aspm(pdev, state, 1);
6867beb0a152SYanir Lubetkin }
6868beb0a152SYanir Lubetkin 
e1000e_pm_thaw(struct device * dev)6869a7023819SAlexander Duyck static int e1000e_pm_thaw(struct device *dev)
6870a7023819SAlexander Duyck {
6871a7023819SAlexander Duyck 	struct net_device *netdev = dev_get_drvdata(dev);
6872a7023819SAlexander Duyck 	struct e1000_adapter *adapter = netdev_priv(netdev);
6873a7023819SAlexander Duyck 	int rc = 0;
6874a7023819SAlexander Duyck 
6875a7023819SAlexander Duyck 	e1000e_set_interrupt_capability(adapter);
6876a7023819SAlexander Duyck 
6877a7023819SAlexander Duyck 	rtnl_lock();
6878a7023819SAlexander Duyck 	if (netif_running(netdev)) {
6879a7023819SAlexander Duyck 		rc = e1000_request_irq(adapter);
6880a7023819SAlexander Duyck 		if (rc)
6881a7023819SAlexander Duyck 			goto err_irq;
6882a7023819SAlexander Duyck 
6883a7023819SAlexander Duyck 		e1000e_up(adapter);
6884a7023819SAlexander Duyck 	}
6885a7023819SAlexander Duyck 
6886a7023819SAlexander Duyck 	netif_device_attach(netdev);
6887a7023819SAlexander Duyck err_irq:
6888a7023819SAlexander Duyck 	rtnl_unlock();
6889a7023819SAlexander Duyck 
6890a7023819SAlexander Duyck 	return rc;
6891a7023819SAlexander Duyck }
6892a7023819SAlexander Duyck 
__e1000_resume(struct pci_dev * pdev)6893dee1ad47SJeff Kirsher static int __e1000_resume(struct pci_dev *pdev)
6894dee1ad47SJeff Kirsher {
6895dee1ad47SJeff Kirsher 	struct net_device *netdev = pci_get_drvdata(pdev);
6896dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
6897dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
6898dee1ad47SJeff Kirsher 	u16 aspm_disable_flag = 0;
6899dee1ad47SJeff Kirsher 
6900dee1ad47SJeff Kirsher 	if (adapter->flags2 & FLAG2_DISABLE_ASPM_L0S)
6901dee1ad47SJeff Kirsher 		aspm_disable_flag = PCIE_LINK_STATE_L0S;
6902dee1ad47SJeff Kirsher 	if (adapter->flags2 & FLAG2_DISABLE_ASPM_L1)
6903dee1ad47SJeff Kirsher 		aspm_disable_flag |= PCIE_LINK_STATE_L1;
6904dee1ad47SJeff Kirsher 	if (aspm_disable_flag)
69052758f9edSRaanan Avargil 		e1000e_disable_aspm(pdev, aspm_disable_flag);
6906dee1ad47SJeff Kirsher 
690766148babSKonstantin Khlebnikov 	pci_set_master(pdev);
6908dee1ad47SJeff Kirsher 
69092fbe4526SBruce Allan 	if (hw->mac.type >= e1000_pch2lan)
6910dee1ad47SJeff Kirsher 		e1000_resume_workarounds_pchlan(&adapter->hw);
6911dee1ad47SJeff Kirsher 
6912dee1ad47SJeff Kirsher 	e1000e_power_up_phy(adapter);
6913dee1ad47SJeff Kirsher 
6914dee1ad47SJeff Kirsher 	/* report the system wakeup cause from S3/S4 */
6915dee1ad47SJeff Kirsher 	if (adapter->flags2 & FLAG2_HAS_PHY_WAKEUP) {
6916dee1ad47SJeff Kirsher 		u16 phy_data;
6917dee1ad47SJeff Kirsher 
6918dee1ad47SJeff Kirsher 		e1e_rphy(&adapter->hw, BM_WUS, &phy_data);
6919dee1ad47SJeff Kirsher 		if (phy_data) {
6920dee1ad47SJeff Kirsher 			e_info("PHY Wakeup cause - %s\n",
6921dee1ad47SJeff Kirsher 			       phy_data & E1000_WUS_EX ? "Unicast Packet" :
6922dee1ad47SJeff Kirsher 			       phy_data & E1000_WUS_MC ? "Multicast Packet" :
6923dee1ad47SJeff Kirsher 			       phy_data & E1000_WUS_BC ? "Broadcast Packet" :
6924dee1ad47SJeff Kirsher 			       phy_data & E1000_WUS_MAG ? "Magic Packet" :
6925ef456f85SJeff Kirsher 			       phy_data & E1000_WUS_LNKC ?
6926ef456f85SJeff Kirsher 			       "Link Status Change" : "other");
6927dee1ad47SJeff Kirsher 		}
6928dee1ad47SJeff Kirsher 		e1e_wphy(&adapter->hw, BM_WUS, ~0);
6929dee1ad47SJeff Kirsher 	} else {
6930dee1ad47SJeff Kirsher 		u32 wus = er32(WUS);
69316cf08d1cSDavid Ertman 
6932dee1ad47SJeff Kirsher 		if (wus) {
6933dee1ad47SJeff Kirsher 			e_info("MAC Wakeup cause - %s\n",
6934dee1ad47SJeff Kirsher 			       wus & E1000_WUS_EX ? "Unicast Packet" :
6935dee1ad47SJeff Kirsher 			       wus & E1000_WUS_MC ? "Multicast Packet" :
6936dee1ad47SJeff Kirsher 			       wus & E1000_WUS_BC ? "Broadcast Packet" :
6937dee1ad47SJeff Kirsher 			       wus & E1000_WUS_MAG ? "Magic Packet" :
6938dee1ad47SJeff Kirsher 			       wus & E1000_WUS_LNKC ? "Link Status Change" :
6939dee1ad47SJeff Kirsher 			       "other");
6940dee1ad47SJeff Kirsher 		}
6941dee1ad47SJeff Kirsher 		ew32(WUS, ~0);
6942dee1ad47SJeff Kirsher 	}
6943dee1ad47SJeff Kirsher 
6944dee1ad47SJeff Kirsher 	e1000e_reset(adapter);
6945dee1ad47SJeff Kirsher 
6946dee1ad47SJeff Kirsher 	e1000_init_manageability_pt(adapter);
6947dee1ad47SJeff Kirsher 
6948e921eb1aSBruce Allan 	/* If the controller has AMT, do not set DRV_LOAD until the interface
6949dee1ad47SJeff Kirsher 	 * is up.  For all other cases, let the f/w know that the h/w is now
6950dee1ad47SJeff Kirsher 	 * under the control of the driver.
6951dee1ad47SJeff Kirsher 	 */
6952dee1ad47SJeff Kirsher 	if (!(adapter->flags & FLAG_HAS_AMT))
6953dee1ad47SJeff Kirsher 		e1000e_get_hw_control(adapter);
6954dee1ad47SJeff Kirsher 
6955dee1ad47SJeff Kirsher 	return 0;
6956dee1ad47SJeff Kirsher }
6957dee1ad47SJeff Kirsher 
e1000e_pm_prepare(struct device * dev)695875a3f93bSJesse Brandeburg static int e1000e_pm_prepare(struct device *dev)
6959ccf8b940SChen Yu {
6960ccf8b940SChen Yu 	return pm_runtime_suspended(dev) &&
6961ccf8b940SChen Yu 		pm_suspend_via_firmware();
6962ccf8b940SChen Yu }
6963ccf8b940SChen Yu 
e1000e_pm_suspend(struct device * dev)696475a3f93bSJesse Brandeburg static int e1000e_pm_suspend(struct device *dev)
6965dee1ad47SJeff Kirsher {
6966f15bb6ddSSasha Neftin 	struct net_device *netdev = pci_get_drvdata(to_pci_dev(dev));
6967f15bb6ddSSasha Neftin 	struct e1000_adapter *adapter = netdev_priv(netdev);
6968dee1ad47SJeff Kirsher 	struct pci_dev *pdev = to_pci_dev(dev);
6969833521ebSChris Wilson 	int rc;
6970dee1ad47SJeff Kirsher 
69712a7e19afSDavid Ertman 	e1000e_flush_lpic(pdev);
69722a7e19afSDavid Ertman 
697328002099SDavid Ertman 	e1000e_pm_freeze(dev);
697428002099SDavid Ertman 
6975833521ebSChris Wilson 	rc = __e1000_shutdown(pdev, false);
69760a6ad4d9SVitaly Lifshits 	if (!rc) {
6977f15bb6ddSSasha Neftin 		/* Introduce S0ix implementation */
69783c98cbf2SMario Limonciello 		if (adapter->flags2 & FLAG2_ENABLE_S0IX_FLOWS)
6979f15bb6ddSSasha Neftin 			e1000e_s0ix_entry_flow(adapter);
6980808e0d88SMario Limonciello 	}
6981f15bb6ddSSasha Neftin 
69820a6ad4d9SVitaly Lifshits 	return 0;
6983dee1ad47SJeff Kirsher }
6984dee1ad47SJeff Kirsher 
e1000e_pm_resume(struct device * dev)698575a3f93bSJesse Brandeburg static int e1000e_pm_resume(struct device *dev)
6986dee1ad47SJeff Kirsher {
6987f15bb6ddSSasha Neftin 	struct net_device *netdev = pci_get_drvdata(to_pci_dev(dev));
6988f15bb6ddSSasha Neftin 	struct e1000_adapter *adapter = netdev_priv(netdev);
6989dee1ad47SJeff Kirsher 	struct pci_dev *pdev = to_pci_dev(dev);
699028002099SDavid Ertman 	int rc;
6991dee1ad47SJeff Kirsher 
6992f15bb6ddSSasha Neftin 	/* Introduce S0ix implementation */
69933c98cbf2SMario Limonciello 	if (adapter->flags2 & FLAG2_ENABLE_S0IX_FLOWS)
6994f15bb6ddSSasha Neftin 		e1000e_s0ix_exit_flow(adapter);
6995f15bb6ddSSasha Neftin 
699628002099SDavid Ertman 	rc = __e1000_resume(pdev);
699728002099SDavid Ertman 	if (rc)
699828002099SDavid Ertman 		return rc;
6999dee1ad47SJeff Kirsher 
700028002099SDavid Ertman 	return e1000e_pm_thaw(dev);
7001dee1ad47SJeff Kirsher }
7002dee1ad47SJeff Kirsher 
e1000e_pm_runtime_idle(struct device * dev)7003880e6269SArnd Bergmann static __maybe_unused int e1000e_pm_runtime_idle(struct device *dev)
7004dee1ad47SJeff Kirsher {
7005ee2e80c1SChuhong Yuan 	struct net_device *netdev = dev_get_drvdata(dev);
7006dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
70072116bc25SDavid Ertman 	u16 eee_lp;
7008dee1ad47SJeff Kirsher 
70092116bc25SDavid Ertman 	eee_lp = adapter->hw.dev_spec.ich8lan.eee_lp_ability;
70102116bc25SDavid Ertman 
70112116bc25SDavid Ertman 	if (!e1000e_has_link(adapter)) {
70122116bc25SDavid Ertman 		adapter->hw.dev_spec.ich8lan.eee_lp_ability = eee_lp;
701363eb48f1SDavid Ertman 		pm_schedule_suspend(dev, 5 * MSEC_PER_SEC);
70142116bc25SDavid Ertman 	}
7015dee1ad47SJeff Kirsher 
7016dee1ad47SJeff Kirsher 	return -EBUSY;
7017dee1ad47SJeff Kirsher }
7018dee1ad47SJeff Kirsher 
e1000e_pm_runtime_resume(struct device * dev)701975a3f93bSJesse Brandeburg static int e1000e_pm_runtime_resume(struct device *dev)
702063eb48f1SDavid Ertman {
702163eb48f1SDavid Ertman 	struct pci_dev *pdev = to_pci_dev(dev);
702263eb48f1SDavid Ertman 	struct net_device *netdev = pci_get_drvdata(pdev);
702363eb48f1SDavid Ertman 	struct e1000_adapter *adapter = netdev_priv(netdev);
702463eb48f1SDavid Ertman 	int rc;
702563eb48f1SDavid Ertman 
7026d1470851SKai-Heng Feng 	pdev->pme_poll = true;
7027d1470851SKai-Heng Feng 
702863eb48f1SDavid Ertman 	rc = __e1000_resume(pdev);
702963eb48f1SDavid Ertman 	if (rc)
703063eb48f1SDavid Ertman 		return rc;
703163eb48f1SDavid Ertman 
703263eb48f1SDavid Ertman 	if (netdev->flags & IFF_UP)
7033386164d9SAlexander Duyck 		e1000e_up(adapter);
703463eb48f1SDavid Ertman 
703563eb48f1SDavid Ertman 	return rc;
703663eb48f1SDavid Ertman }
703763eb48f1SDavid Ertman 
e1000e_pm_runtime_suspend(struct device * dev)703875a3f93bSJesse Brandeburg static int e1000e_pm_runtime_suspend(struct device *dev)
7039dee1ad47SJeff Kirsher {
7040dee1ad47SJeff Kirsher 	struct pci_dev *pdev = to_pci_dev(dev);
7041dee1ad47SJeff Kirsher 	struct net_device *netdev = pci_get_drvdata(pdev);
7042dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
7043dee1ad47SJeff Kirsher 
704463eb48f1SDavid Ertman 	if (netdev->flags & IFF_UP) {
704563eb48f1SDavid Ertman 		int count = E1000_CHECK_RESET_COUNT;
7046dee1ad47SJeff Kirsher 
704763eb48f1SDavid Ertman 		while (test_bit(__E1000_RESETTING, &adapter->state) && count--)
7048ab6973aeSArjan van de Ven 			usleep_range(10000, 11000);
704963eb48f1SDavid Ertman 
705063eb48f1SDavid Ertman 		WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
705163eb48f1SDavid Ertman 
705263eb48f1SDavid Ertman 		/* Down the device without resetting the hardware */
705363eb48f1SDavid Ertman 		e1000e_down(adapter, false);
705463eb48f1SDavid Ertman 	}
705563eb48f1SDavid Ertman 
705663eb48f1SDavid Ertman 	if (__e1000_shutdown(pdev, true)) {
705763eb48f1SDavid Ertman 		e1000e_pm_runtime_resume(dev);
705863eb48f1SDavid Ertman 		return -EBUSY;
705963eb48f1SDavid Ertman 	}
706063eb48f1SDavid Ertman 
706163eb48f1SDavid Ertman 	return 0;
7062dee1ad47SJeff Kirsher }
7063dee1ad47SJeff Kirsher 
e1000_shutdown(struct pci_dev * pdev)7064dee1ad47SJeff Kirsher static void e1000_shutdown(struct pci_dev *pdev)
7065dee1ad47SJeff Kirsher {
70662a7e19afSDavid Ertman 	e1000e_flush_lpic(pdev);
70672a7e19afSDavid Ertman 
706828002099SDavid Ertman 	e1000e_pm_freeze(&pdev->dev);
706928002099SDavid Ertman 
707066148babSKonstantin Khlebnikov 	__e1000_shutdown(pdev, false);
7071dee1ad47SJeff Kirsher }
7072dee1ad47SJeff Kirsher 
7073dee1ad47SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
7074dee1ad47SJeff Kirsher 
e1000_intr_msix(int __always_unused irq,void * data)70758bb62869SBruce Allan static irqreturn_t e1000_intr_msix(int __always_unused irq, void *data)
7076dee1ad47SJeff Kirsher {
7077dee1ad47SJeff Kirsher 	struct net_device *netdev = data;
7078dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
7079dee1ad47SJeff Kirsher 
7080dee1ad47SJeff Kirsher 	if (adapter->msix_entries) {
7081dee1ad47SJeff Kirsher 		int vector, msix_irq;
7082dee1ad47SJeff Kirsher 
7083dee1ad47SJeff Kirsher 		vector = 0;
7084dee1ad47SJeff Kirsher 		msix_irq = adapter->msix_entries[vector].vector;
7085fd8e597bSKonstantin Khlebnikov 		if (disable_hardirq(msix_irq))
7086dee1ad47SJeff Kirsher 			e1000_intr_msix_rx(msix_irq, netdev);
7087dee1ad47SJeff Kirsher 		enable_irq(msix_irq);
7088dee1ad47SJeff Kirsher 
7089dee1ad47SJeff Kirsher 		vector++;
7090dee1ad47SJeff Kirsher 		msix_irq = adapter->msix_entries[vector].vector;
7091fd8e597bSKonstantin Khlebnikov 		if (disable_hardirq(msix_irq))
7092dee1ad47SJeff Kirsher 			e1000_intr_msix_tx(msix_irq, netdev);
7093dee1ad47SJeff Kirsher 		enable_irq(msix_irq);
7094dee1ad47SJeff Kirsher 
7095dee1ad47SJeff Kirsher 		vector++;
7096dee1ad47SJeff Kirsher 		msix_irq = adapter->msix_entries[vector].vector;
7097fd8e597bSKonstantin Khlebnikov 		if (disable_hardirq(msix_irq))
7098dee1ad47SJeff Kirsher 			e1000_msix_other(msix_irq, netdev);
7099dee1ad47SJeff Kirsher 		enable_irq(msix_irq);
7100dee1ad47SJeff Kirsher 	}
7101dee1ad47SJeff Kirsher 
7102dee1ad47SJeff Kirsher 	return IRQ_HANDLED;
7103dee1ad47SJeff Kirsher }
7104dee1ad47SJeff Kirsher 
7105e921eb1aSBruce Allan /**
7106e921eb1aSBruce Allan  * e1000_netpoll
7107e921eb1aSBruce Allan  * @netdev: network interface device structure
7108e921eb1aSBruce Allan  *
7109dee1ad47SJeff Kirsher  * Polling 'interrupt' - used by things like netconsole to send skbs
7110dee1ad47SJeff Kirsher  * without having to re-enable interrupts. It's not called while
7111dee1ad47SJeff Kirsher  * the interrupt routine is executing.
7112dee1ad47SJeff Kirsher  */
e1000_netpoll(struct net_device * netdev)7113dee1ad47SJeff Kirsher static void e1000_netpoll(struct net_device *netdev)
7114dee1ad47SJeff Kirsher {
7115dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
7116dee1ad47SJeff Kirsher 
7117dee1ad47SJeff Kirsher 	switch (adapter->int_mode) {
7118dee1ad47SJeff Kirsher 	case E1000E_INT_MODE_MSIX:
7119dee1ad47SJeff Kirsher 		e1000_intr_msix(adapter->pdev->irq, netdev);
7120dee1ad47SJeff Kirsher 		break;
7121dee1ad47SJeff Kirsher 	case E1000E_INT_MODE_MSI:
712231119129SWANG Cong 		if (disable_hardirq(adapter->pdev->irq))
7123dee1ad47SJeff Kirsher 			e1000_intr_msi(adapter->pdev->irq, netdev);
7124dee1ad47SJeff Kirsher 		enable_irq(adapter->pdev->irq);
7125dee1ad47SJeff Kirsher 		break;
7126dee1ad47SJeff Kirsher 	default:		/* E1000E_INT_MODE_LEGACY */
712731119129SWANG Cong 		if (disable_hardirq(adapter->pdev->irq))
7128dee1ad47SJeff Kirsher 			e1000_intr(adapter->pdev->irq, netdev);
7129dee1ad47SJeff Kirsher 		enable_irq(adapter->pdev->irq);
7130dee1ad47SJeff Kirsher 		break;
7131dee1ad47SJeff Kirsher 	}
7132dee1ad47SJeff Kirsher }
7133dee1ad47SJeff Kirsher #endif
7134dee1ad47SJeff Kirsher 
7135dee1ad47SJeff Kirsher /**
7136dee1ad47SJeff Kirsher  * e1000_io_error_detected - called when PCI error is detected
7137dee1ad47SJeff Kirsher  * @pdev: Pointer to PCI device
7138dee1ad47SJeff Kirsher  * @state: The current pci connection state
7139dee1ad47SJeff Kirsher  *
7140dee1ad47SJeff Kirsher  * This function is called after a PCI bus error affecting
7141dee1ad47SJeff Kirsher  * this device has been detected.
7142dee1ad47SJeff Kirsher  */
e1000_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)7143dee1ad47SJeff Kirsher static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
7144dee1ad47SJeff Kirsher 						pci_channel_state_t state)
7145dee1ad47SJeff Kirsher {
7146a7023819SAlexander Duyck 	e1000e_pm_freeze(&pdev->dev);
7147dee1ad47SJeff Kirsher 
7148dee1ad47SJeff Kirsher 	if (state == pci_channel_io_perm_failure)
7149dee1ad47SJeff Kirsher 		return PCI_ERS_RESULT_DISCONNECT;
7150dee1ad47SJeff Kirsher 
7151dee1ad47SJeff Kirsher 	pci_disable_device(pdev);
7152dee1ad47SJeff Kirsher 
7153800b74a5SHao Chen 	/* Request a slot reset. */
7154dee1ad47SJeff Kirsher 	return PCI_ERS_RESULT_NEED_RESET;
7155dee1ad47SJeff Kirsher }
7156dee1ad47SJeff Kirsher 
7157dee1ad47SJeff Kirsher /**
7158dee1ad47SJeff Kirsher  * e1000_io_slot_reset - called after the pci bus has been reset.
7159dee1ad47SJeff Kirsher  * @pdev: Pointer to PCI device
7160dee1ad47SJeff Kirsher  *
7161dee1ad47SJeff Kirsher  * Restart the card from scratch, as if from a cold-boot. Implementation
716228002099SDavid Ertman  * resembles the first-half of the e1000e_pm_resume routine.
7163dee1ad47SJeff Kirsher  */
e1000_io_slot_reset(struct pci_dev * pdev)7164dee1ad47SJeff Kirsher static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
7165dee1ad47SJeff Kirsher {
7166dee1ad47SJeff Kirsher 	struct net_device *netdev = pci_get_drvdata(pdev);
7167dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
7168dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
7169dee1ad47SJeff Kirsher 	u16 aspm_disable_flag = 0;
7170dee1ad47SJeff Kirsher 	int err;
7171dee1ad47SJeff Kirsher 	pci_ers_result_t result;
7172dee1ad47SJeff Kirsher 
7173dee1ad47SJeff Kirsher 	if (adapter->flags2 & FLAG2_DISABLE_ASPM_L0S)
7174dee1ad47SJeff Kirsher 		aspm_disable_flag = PCIE_LINK_STATE_L0S;
7175dee1ad47SJeff Kirsher 	if (adapter->flags2 & FLAG2_DISABLE_ASPM_L1)
7176dee1ad47SJeff Kirsher 		aspm_disable_flag |= PCIE_LINK_STATE_L1;
7177dee1ad47SJeff Kirsher 	if (aspm_disable_flag)
71782758f9edSRaanan Avargil 		e1000e_disable_aspm_locked(pdev, aspm_disable_flag);
7179dee1ad47SJeff Kirsher 
7180dee1ad47SJeff Kirsher 	err = pci_enable_device_mem(pdev);
7181dee1ad47SJeff Kirsher 	if (err) {
7182dee1ad47SJeff Kirsher 		dev_err(&pdev->dev,
7183dee1ad47SJeff Kirsher 			"Cannot re-enable PCI device after reset.\n");
7184dee1ad47SJeff Kirsher 		result = PCI_ERS_RESULT_DISCONNECT;
7185dee1ad47SJeff Kirsher 	} else {
7186dee1ad47SJeff Kirsher 		pdev->state_saved = true;
7187dee1ad47SJeff Kirsher 		pci_restore_state(pdev);
718866148babSKonstantin Khlebnikov 		pci_set_master(pdev);
7189dee1ad47SJeff Kirsher 
7190dee1ad47SJeff Kirsher 		pci_enable_wake(pdev, PCI_D3hot, 0);
7191dee1ad47SJeff Kirsher 		pci_enable_wake(pdev, PCI_D3cold, 0);
7192dee1ad47SJeff Kirsher 
7193dee1ad47SJeff Kirsher 		e1000e_reset(adapter);
7194dee1ad47SJeff Kirsher 		ew32(WUS, ~0);
7195dee1ad47SJeff Kirsher 		result = PCI_ERS_RESULT_RECOVERED;
7196dee1ad47SJeff Kirsher 	}
7197dee1ad47SJeff Kirsher 
7198dee1ad47SJeff Kirsher 	return result;
7199dee1ad47SJeff Kirsher }
7200dee1ad47SJeff Kirsher 
7201dee1ad47SJeff Kirsher /**
7202dee1ad47SJeff Kirsher  * e1000_io_resume - called when traffic can start flowing again.
7203dee1ad47SJeff Kirsher  * @pdev: Pointer to PCI device
7204dee1ad47SJeff Kirsher  *
7205dee1ad47SJeff Kirsher  * This callback is called when the error recovery driver tells us that
7206dee1ad47SJeff Kirsher  * its OK to resume normal operation. Implementation resembles the
720728002099SDavid Ertman  * second-half of the e1000e_pm_resume routine.
7208dee1ad47SJeff Kirsher  */
e1000_io_resume(struct pci_dev * pdev)7209dee1ad47SJeff Kirsher static void e1000_io_resume(struct pci_dev *pdev)
7210dee1ad47SJeff Kirsher {
7211dee1ad47SJeff Kirsher 	struct net_device *netdev = pci_get_drvdata(pdev);
7212dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
7213dee1ad47SJeff Kirsher 
7214dee1ad47SJeff Kirsher 	e1000_init_manageability_pt(adapter);
7215dee1ad47SJeff Kirsher 
7216a7023819SAlexander Duyck 	e1000e_pm_thaw(&pdev->dev);
7217dee1ad47SJeff Kirsher 
7218e921eb1aSBruce Allan 	/* If the controller has AMT, do not set DRV_LOAD until the interface
7219dee1ad47SJeff Kirsher 	 * is up.  For all other cases, let the f/w know that the h/w is now
7220dee1ad47SJeff Kirsher 	 * under the control of the driver.
7221dee1ad47SJeff Kirsher 	 */
7222dee1ad47SJeff Kirsher 	if (!(adapter->flags & FLAG_HAS_AMT))
7223dee1ad47SJeff Kirsher 		e1000e_get_hw_control(adapter);
7224dee1ad47SJeff Kirsher }
7225dee1ad47SJeff Kirsher 
e1000_print_device_info(struct e1000_adapter * adapter)7226dee1ad47SJeff Kirsher static void e1000_print_device_info(struct e1000_adapter *adapter)
7227dee1ad47SJeff Kirsher {
7228dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
7229dee1ad47SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
7230dee1ad47SJeff Kirsher 	u32 ret_val;
7231dee1ad47SJeff Kirsher 	u8 pba_str[E1000_PBANUM_LENGTH];
7232dee1ad47SJeff Kirsher 
7233dee1ad47SJeff Kirsher 	/* print bus type/speed/width info */
7234dee1ad47SJeff Kirsher 	e_info("(PCI Express:2.5GT/s:%s) %pM\n",
7235dee1ad47SJeff Kirsher 	       /* bus width */
7236dee1ad47SJeff Kirsher 	       ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
7237dee1ad47SJeff Kirsher 		"Width x1"),
7238dee1ad47SJeff Kirsher 	       /* MAC address */
7239dee1ad47SJeff Kirsher 	       netdev->dev_addr);
7240dee1ad47SJeff Kirsher 	e_info("Intel(R) PRO/%s Network Connection\n",
7241dee1ad47SJeff Kirsher 	       (hw->phy.type == e1000_phy_ife) ? "10/100" : "1000");
7242dee1ad47SJeff Kirsher 	ret_val = e1000_read_pba_string_generic(hw, pba_str,
7243dee1ad47SJeff Kirsher 						E1000_PBANUM_LENGTH);
7244dee1ad47SJeff Kirsher 	if (ret_val)
7245f029c781SWolfram Sang 		strscpy((char *)pba_str, "Unknown", sizeof(pba_str));
7246dee1ad47SJeff Kirsher 	e_info("MAC: %d, PHY: %d, PBA No: %s\n",
7247dee1ad47SJeff Kirsher 	       hw->mac.type, hw->phy.type, pba_str);
7248dee1ad47SJeff Kirsher }
7249dee1ad47SJeff Kirsher 
e1000_eeprom_checks(struct e1000_adapter * adapter)7250dee1ad47SJeff Kirsher static void e1000_eeprom_checks(struct e1000_adapter *adapter)
7251dee1ad47SJeff Kirsher {
7252dee1ad47SJeff Kirsher 	struct e1000_hw *hw = &adapter->hw;
7253dee1ad47SJeff Kirsher 	int ret_val;
7254dee1ad47SJeff Kirsher 	u16 buf = 0;
7255dee1ad47SJeff Kirsher 
7256dee1ad47SJeff Kirsher 	if (hw->mac.type != e1000_82573)
7257dee1ad47SJeff Kirsher 		return;
7258dee1ad47SJeff Kirsher 
7259dee1ad47SJeff Kirsher 	ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &buf);
7260e885d762SBruce Allan 	le16_to_cpus(&buf);
726118dd2392SJacob Keller 	if (!ret_val && (!(buf & BIT(0)))) {
7262dee1ad47SJeff Kirsher 		/* Deep Smart Power Down (DSPD) */
7263dee1ad47SJeff Kirsher 		dev_warn(&adapter->pdev->dev,
7264dee1ad47SJeff Kirsher 			 "Warning: detected DSPD enabled in EEPROM\n");
7265dee1ad47SJeff Kirsher 	}
7266dee1ad47SJeff Kirsher }
7267dee1ad47SJeff Kirsher 
e1000_fix_features(struct net_device * netdev,netdev_features_t features)726855e7fe5bSAlexander Duyck static netdev_features_t e1000_fix_features(struct net_device *netdev,
726955e7fe5bSAlexander Duyck 					    netdev_features_t features)
727055e7fe5bSAlexander Duyck {
727155e7fe5bSAlexander Duyck 	struct e1000_adapter *adapter = netdev_priv(netdev);
727255e7fe5bSAlexander Duyck 	struct e1000_hw *hw = &adapter->hw;
727355e7fe5bSAlexander Duyck 
727455e7fe5bSAlexander Duyck 	/* Jumbo frame workaround on 82579 and newer requires CRC be stripped */
727555e7fe5bSAlexander Duyck 	if ((hw->mac.type >= e1000_pch2lan) && (netdev->mtu > ETH_DATA_LEN))
727655e7fe5bSAlexander Duyck 		features &= ~NETIF_F_RXFCS;
727755e7fe5bSAlexander Duyck 
727883808641SJarod Wilson 	/* Since there is no support for separate Rx/Tx vlan accel
727983808641SJarod Wilson 	 * enable/disable make sure Tx flag is always in same state as Rx.
728083808641SJarod Wilson 	 */
728183808641SJarod Wilson 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
728283808641SJarod Wilson 		features |= NETIF_F_HW_VLAN_CTAG_TX;
728383808641SJarod Wilson 	else
728483808641SJarod Wilson 		features &= ~NETIF_F_HW_VLAN_CTAG_TX;
728583808641SJarod Wilson 
728655e7fe5bSAlexander Duyck 	return features;
728755e7fe5bSAlexander Duyck }
728855e7fe5bSAlexander Duyck 
e1000_set_features(struct net_device * netdev,netdev_features_t features)7289c8f44affSMichał Mirosław static int e1000_set_features(struct net_device *netdev,
7290c8f44affSMichał Mirosław 			      netdev_features_t features)
7291dc221294SBruce Allan {
7292dc221294SBruce Allan 	struct e1000_adapter *adapter = netdev_priv(netdev);
7293c8f44affSMichał Mirosław 	netdev_features_t changed = features ^ netdev->features;
7294dc221294SBruce Allan 
7295dc221294SBruce Allan 	if (changed & (NETIF_F_TSO | NETIF_F_TSO6))
7296dc221294SBruce Allan 		adapter->flags |= FLAG_TSO_FORCE;
7297dc221294SBruce Allan 
7298f646968fSPatrick McHardy 	if (!(changed & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7299cf955e6cSBen Greear 			 NETIF_F_RXCSUM | NETIF_F_RXHASH | NETIF_F_RXFCS |
7300cf955e6cSBen Greear 			 NETIF_F_RXALL)))
7301dc221294SBruce Allan 		return 0;
7302dc221294SBruce Allan 
73030184039aSBen Greear 	if (changed & NETIF_F_RXFCS) {
73040184039aSBen Greear 		if (features & NETIF_F_RXFCS) {
73050184039aSBen Greear 			adapter->flags2 &= ~FLAG2_CRC_STRIPPING;
73060184039aSBen Greear 		} else {
73070184039aSBen Greear 			/* We need to take it back to defaults, which might mean
73080184039aSBen Greear 			 * stripping is still disabled at the adapter level.
73090184039aSBen Greear 			 */
73100184039aSBen Greear 			if (adapter->flags2 & FLAG2_DFLT_CRC_STRIPPING)
73110184039aSBen Greear 				adapter->flags2 |= FLAG2_CRC_STRIPPING;
73120184039aSBen Greear 			else
73130184039aSBen Greear 				adapter->flags2 &= ~FLAG2_CRC_STRIPPING;
73140184039aSBen Greear 		}
73150184039aSBen Greear 	}
73160184039aSBen Greear 
731770495a50SBruce Allan 	netdev->features = features;
731870495a50SBruce Allan 
7319dc221294SBruce Allan 	if (netif_running(netdev))
7320dc221294SBruce Allan 		e1000e_reinit_locked(adapter);
7321dc221294SBruce Allan 	else
7322dc221294SBruce Allan 		e1000e_reset(adapter);
7323dc221294SBruce Allan 
7324b0ddfe2bSSerhey Popovych 	return 1;
7325dc221294SBruce Allan }
7326dc221294SBruce Allan 
7327dee1ad47SJeff Kirsher static const struct net_device_ops e1000e_netdev_ops = {
7328d5ea45daSStefan Assmann 	.ndo_open		= e1000e_open,
7329d5ea45daSStefan Assmann 	.ndo_stop		= e1000e_close,
7330dee1ad47SJeff Kirsher 	.ndo_start_xmit		= e1000_xmit_frame,
7331dee1ad47SJeff Kirsher 	.ndo_get_stats64	= e1000e_get_stats64,
7332ef9b965aSJesse Brandeburg 	.ndo_set_rx_mode	= e1000e_set_rx_mode,
7333dee1ad47SJeff Kirsher 	.ndo_set_mac_address	= e1000_set_mac,
7334dee1ad47SJeff Kirsher 	.ndo_change_mtu		= e1000_change_mtu,
7335a7605370SArnd Bergmann 	.ndo_eth_ioctl		= e1000_ioctl,
7336dee1ad47SJeff Kirsher 	.ndo_tx_timeout		= e1000_tx_timeout,
7337dee1ad47SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
7338dee1ad47SJeff Kirsher 
7339dee1ad47SJeff Kirsher 	.ndo_vlan_rx_add_vid	= e1000_vlan_rx_add_vid,
7340dee1ad47SJeff Kirsher 	.ndo_vlan_rx_kill_vid	= e1000_vlan_rx_kill_vid,
7341dee1ad47SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
7342dee1ad47SJeff Kirsher 	.ndo_poll_controller	= e1000_netpoll,
7343dee1ad47SJeff Kirsher #endif
7344dc221294SBruce Allan 	.ndo_set_features = e1000_set_features,
734555e7fe5bSAlexander Duyck 	.ndo_fix_features = e1000_fix_features,
7346f2701b18SToshiaki Makita 	.ndo_features_check	= passthru_features_check,
7347dee1ad47SJeff Kirsher };
7348dee1ad47SJeff Kirsher 
7349dee1ad47SJeff Kirsher /**
7350dee1ad47SJeff Kirsher  * e1000_probe - Device Initialization Routine
7351dee1ad47SJeff Kirsher  * @pdev: PCI device information struct
7352dee1ad47SJeff Kirsher  * @ent: entry in e1000_pci_tbl
7353dee1ad47SJeff Kirsher  *
7354dee1ad47SJeff Kirsher  * Returns 0 on success, negative on failure
7355dee1ad47SJeff Kirsher  *
7356dee1ad47SJeff Kirsher  * e1000_probe initializes an adapter identified by a pci_dev structure.
7357dee1ad47SJeff Kirsher  * The OS initialization, configuring of the adapter private structure,
7358dee1ad47SJeff Kirsher  * and a hardware reset occur.
7359dee1ad47SJeff Kirsher  **/
e1000_probe(struct pci_dev * pdev,const struct pci_device_id * ent)73601dd06ae8SGreg Kroah-Hartman static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7361dee1ad47SJeff Kirsher {
7362dee1ad47SJeff Kirsher 	struct net_device *netdev;
7363dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter;
7364dee1ad47SJeff Kirsher 	struct e1000_hw *hw;
7365dee1ad47SJeff Kirsher 	const struct e1000_info *ei = e1000_info_tbl[ent->driver_data];
7366dee1ad47SJeff Kirsher 	resource_size_t mmio_start, mmio_len;
7367dee1ad47SJeff Kirsher 	resource_size_t flash_start, flash_len;
7368dee1ad47SJeff Kirsher 	static int cards_found;
7369dee1ad47SJeff Kirsher 	u16 aspm_disable_flag = 0;
7370dee1ad47SJeff Kirsher 	u16 eeprom_data = 0;
7371dee1ad47SJeff Kirsher 	u16 eeprom_apme_mask = E1000_EEPROM_APME;
7372a34a42d8SChristophe JAILLET 	int bars, i, err;
7373847042a6SBrian Walsh 	s32 ret_val = 0;
7374dee1ad47SJeff Kirsher 
7375dee1ad47SJeff Kirsher 	if (ei->flags2 & FLAG2_DISABLE_ASPM_L0S)
7376dee1ad47SJeff Kirsher 		aspm_disable_flag = PCIE_LINK_STATE_L0S;
7377dee1ad47SJeff Kirsher 	if (ei->flags2 & FLAG2_DISABLE_ASPM_L1)
7378dee1ad47SJeff Kirsher 		aspm_disable_flag |= PCIE_LINK_STATE_L1;
7379dee1ad47SJeff Kirsher 	if (aspm_disable_flag)
7380dee1ad47SJeff Kirsher 		e1000e_disable_aspm(pdev, aspm_disable_flag);
7381dee1ad47SJeff Kirsher 
7382dee1ad47SJeff Kirsher 	err = pci_enable_device_mem(pdev);
7383dee1ad47SJeff Kirsher 	if (err)
7384dee1ad47SJeff Kirsher 		return err;
7385dee1ad47SJeff Kirsher 
7386718a39ebSRussell King 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
7387dee1ad47SJeff Kirsher 	if (err) {
7388f0ff4398SBruce Allan 		dev_err(&pdev->dev,
7389f0ff4398SBruce Allan 			"No usable DMA configuration, aborting\n");
7390dee1ad47SJeff Kirsher 		goto err_dma;
7391dee1ad47SJeff Kirsher 	}
7392dee1ad47SJeff Kirsher 
739317e813ecSBruce Allan 	bars = pci_select_bars(pdev, IORESOURCE_MEM);
739417e813ecSBruce Allan 	err = pci_request_selected_regions_exclusive(pdev, bars,
7395dee1ad47SJeff Kirsher 						     e1000e_driver_name);
7396dee1ad47SJeff Kirsher 	if (err)
7397dee1ad47SJeff Kirsher 		goto err_pci_reg;
7398dee1ad47SJeff Kirsher 
7399dee1ad47SJeff Kirsher 	pci_set_master(pdev);
7400dee1ad47SJeff Kirsher 	/* PCI config space info */
7401dee1ad47SJeff Kirsher 	err = pci_save_state(pdev);
7402dee1ad47SJeff Kirsher 	if (err)
7403dee1ad47SJeff Kirsher 		goto err_alloc_etherdev;
7404dee1ad47SJeff Kirsher 
7405dee1ad47SJeff Kirsher 	err = -ENOMEM;
7406dee1ad47SJeff Kirsher 	netdev = alloc_etherdev(sizeof(struct e1000_adapter));
7407dee1ad47SJeff Kirsher 	if (!netdev)
7408dee1ad47SJeff Kirsher 		goto err_alloc_etherdev;
7409dee1ad47SJeff Kirsher 
7410dee1ad47SJeff Kirsher 	SET_NETDEV_DEV(netdev, &pdev->dev);
7411dee1ad47SJeff Kirsher 
7412dee1ad47SJeff Kirsher 	netdev->irq = pdev->irq;
7413dee1ad47SJeff Kirsher 
7414dee1ad47SJeff Kirsher 	pci_set_drvdata(pdev, netdev);
7415dee1ad47SJeff Kirsher 	adapter = netdev_priv(netdev);
7416dee1ad47SJeff Kirsher 	hw = &adapter->hw;
7417dee1ad47SJeff Kirsher 	adapter->netdev = netdev;
7418dee1ad47SJeff Kirsher 	adapter->pdev = pdev;
7419dee1ad47SJeff Kirsher 	adapter->ei = ei;
7420dee1ad47SJeff Kirsher 	adapter->pba = ei->pba;
7421dee1ad47SJeff Kirsher 	adapter->flags = ei->flags;
7422dee1ad47SJeff Kirsher 	adapter->flags2 = ei->flags2;
7423dee1ad47SJeff Kirsher 	adapter->hw.adapter = adapter;
7424dee1ad47SJeff Kirsher 	adapter->hw.mac.type = ei->mac;
7425dee1ad47SJeff Kirsher 	adapter->max_hw_frame_size = ei->max_hw_frame_size;
7426b3f4d599Sstephen hemminger 	adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
7427dee1ad47SJeff Kirsher 
7428dee1ad47SJeff Kirsher 	mmio_start = pci_resource_start(pdev, 0);
7429dee1ad47SJeff Kirsher 	mmio_len = pci_resource_len(pdev, 0);
7430dee1ad47SJeff Kirsher 
7431dee1ad47SJeff Kirsher 	err = -EIO;
7432dee1ad47SJeff Kirsher 	adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
7433dee1ad47SJeff Kirsher 	if (!adapter->hw.hw_addr)
7434dee1ad47SJeff Kirsher 		goto err_ioremap;
7435dee1ad47SJeff Kirsher 
7436dee1ad47SJeff Kirsher 	if ((adapter->flags & FLAG_HAS_FLASH) &&
74371103a631SYanir Lubetkin 	    (pci_resource_flags(pdev, 1) & IORESOURCE_MEM) &&
74381103a631SYanir Lubetkin 	    (hw->mac.type < e1000_pch_spt)) {
7439dee1ad47SJeff Kirsher 		flash_start = pci_resource_start(pdev, 1);
7440dee1ad47SJeff Kirsher 		flash_len = pci_resource_len(pdev, 1);
7441dee1ad47SJeff Kirsher 		adapter->hw.flash_address = ioremap(flash_start, flash_len);
7442dee1ad47SJeff Kirsher 		if (!adapter->hw.flash_address)
7443dee1ad47SJeff Kirsher 			goto err_flashmap;
7444dee1ad47SJeff Kirsher 	}
7445dee1ad47SJeff Kirsher 
7446d495bcb8SBruce Allan 	/* Set default EEE advertisement */
7447d495bcb8SBruce Allan 	if (adapter->flags2 & FLAG2_HAS_EEE)
7448d495bcb8SBruce Allan 		adapter->eee_advert = MDIO_EEE_100TX | MDIO_EEE_1000T;
7449d495bcb8SBruce Allan 
7450dee1ad47SJeff Kirsher 	/* construct the net_device struct */
7451dee1ad47SJeff Kirsher 	netdev->netdev_ops = &e1000e_netdev_ops;
7452dee1ad47SJeff Kirsher 	e1000e_set_ethtool_ops(netdev);
7453dee1ad47SJeff Kirsher 	netdev->watchdog_timeo = 5 * HZ;
7454b48b89f9SJakub Kicinski 	netif_napi_add(netdev, &adapter->napi, e1000e_poll);
7455f029c781SWolfram Sang 	strscpy(netdev->name, pci_name(pdev), sizeof(netdev->name));
7456dee1ad47SJeff Kirsher 
7457dee1ad47SJeff Kirsher 	netdev->mem_start = mmio_start;
7458dee1ad47SJeff Kirsher 	netdev->mem_end = mmio_start + mmio_len;
7459dee1ad47SJeff Kirsher 
7460dee1ad47SJeff Kirsher 	adapter->bd_number = cards_found++;
7461dee1ad47SJeff Kirsher 
7462dee1ad47SJeff Kirsher 	e1000e_check_options(adapter);
7463dee1ad47SJeff Kirsher 
7464dee1ad47SJeff Kirsher 	/* setup adapter struct */
7465dee1ad47SJeff Kirsher 	err = e1000_sw_init(adapter);
7466dee1ad47SJeff Kirsher 	if (err)
7467dee1ad47SJeff Kirsher 		goto err_sw_init;
7468dee1ad47SJeff Kirsher 
7469dee1ad47SJeff Kirsher 	memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
7470dee1ad47SJeff Kirsher 	memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
7471dee1ad47SJeff Kirsher 	memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
7472dee1ad47SJeff Kirsher 
7473dee1ad47SJeff Kirsher 	err = ei->get_variants(adapter);
7474dee1ad47SJeff Kirsher 	if (err)
7475dee1ad47SJeff Kirsher 		goto err_hw_init;
7476dee1ad47SJeff Kirsher 
7477dee1ad47SJeff Kirsher 	if ((adapter->flags & FLAG_IS_ICH) &&
7478152c0a97SYanir Lubetkin 	    (adapter->flags & FLAG_READ_ONLY_NVM) &&
7479152c0a97SYanir Lubetkin 	    (hw->mac.type < e1000_pch_spt))
7480dee1ad47SJeff Kirsher 		e1000e_write_protect_nvm_ich8lan(&adapter->hw);
7481dee1ad47SJeff Kirsher 
7482dee1ad47SJeff Kirsher 	hw->mac.ops.get_bus_info(&adapter->hw);
7483dee1ad47SJeff Kirsher 
7484dee1ad47SJeff Kirsher 	adapter->hw.phy.autoneg_wait_to_complete = 0;
7485dee1ad47SJeff Kirsher 
7486dee1ad47SJeff Kirsher 	/* Copper options */
7487dee1ad47SJeff Kirsher 	if (adapter->hw.phy.media_type == e1000_media_type_copper) {
7488dee1ad47SJeff Kirsher 		adapter->hw.phy.mdix = AUTO_ALL_MODES;
7489dee1ad47SJeff Kirsher 		adapter->hw.phy.disable_polarity_correction = 0;
7490dee1ad47SJeff Kirsher 		adapter->hw.phy.ms_type = e1000_ms_hw_default;
7491dee1ad47SJeff Kirsher 	}
7492dee1ad47SJeff Kirsher 
7493470a5420SBruce Allan 	if (hw->phy.ops.check_reset_block && hw->phy.ops.check_reset_block(hw))
7494185095fbSBruce Allan 		dev_info(&pdev->dev,
7495185095fbSBruce Allan 			 "PHY reset is blocked due to SOL/IDER session.\n");
7496dee1ad47SJeff Kirsher 
7497dc221294SBruce Allan 	/* Set initial default active device features */
7498dc221294SBruce Allan 	netdev->features = (NETIF_F_SG |
7499f646968fSPatrick McHardy 			    NETIF_F_HW_VLAN_CTAG_RX |
7500f646968fSPatrick McHardy 			    NETIF_F_HW_VLAN_CTAG_TX |
7501dc221294SBruce Allan 			    NETIF_F_TSO |
7502dc221294SBruce Allan 			    NETIF_F_TSO6 |
750370495a50SBruce Allan 			    NETIF_F_RXHASH |
7504dc221294SBruce Allan 			    NETIF_F_RXCSUM |
7505dc221294SBruce Allan 			    NETIF_F_HW_CSUM);
7506dc221294SBruce Allan 
750767d47b95SSebastian Basierski 	/* disable TSO for pcie and 10/100 speeds to avoid
750867d47b95SSebastian Basierski 	 * some hardware issues and for i219 to fix transfer
750967d47b95SSebastian Basierski 	 * speed being capped at 60%
751067d47b95SSebastian Basierski 	 */
751167d47b95SSebastian Basierski 	if (!(adapter->flags & FLAG_TSO_FORCE)) {
751267d47b95SSebastian Basierski 		switch (adapter->link_speed) {
751367d47b95SSebastian Basierski 		case SPEED_10:
751467d47b95SSebastian Basierski 		case SPEED_100:
751567d47b95SSebastian Basierski 			e_info("10/100 speed: disabling TSO\n");
751667d47b95SSebastian Basierski 			netdev->features &= ~NETIF_F_TSO;
751767d47b95SSebastian Basierski 			netdev->features &= ~NETIF_F_TSO6;
751867d47b95SSebastian Basierski 			break;
751967d47b95SSebastian Basierski 		case SPEED_1000:
752067d47b95SSebastian Basierski 			netdev->features |= NETIF_F_TSO;
752167d47b95SSebastian Basierski 			netdev->features |= NETIF_F_TSO6;
752267d47b95SSebastian Basierski 			break;
752367d47b95SSebastian Basierski 		default:
752467d47b95SSebastian Basierski 			/* oops */
752567d47b95SSebastian Basierski 			break;
752667d47b95SSebastian Basierski 		}
752767d47b95SSebastian Basierski 		if (hw->mac.type == e1000_pch_spt) {
752867d47b95SSebastian Basierski 			netdev->features &= ~NETIF_F_TSO;
752967d47b95SSebastian Basierski 			netdev->features &= ~NETIF_F_TSO6;
753067d47b95SSebastian Basierski 		}
753167d47b95SSebastian Basierski 	}
753267d47b95SSebastian Basierski 
7533dc221294SBruce Allan 	/* Set user-changeable features (subset of all device features) */
7534dc221294SBruce Allan 	netdev->hw_features = netdev->features;
75350184039aSBen Greear 	netdev->hw_features |= NETIF_F_RXFCS;
7536943146deSBen Greear 	netdev->priv_flags |= IFF_SUPP_NOFCS;
7537cf955e6cSBen Greear 	netdev->hw_features |= NETIF_F_RXALL;
7538dee1ad47SJeff Kirsher 
7539dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER)
7540f646968fSPatrick McHardy 		netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
7541dee1ad47SJeff Kirsher 
7542dc221294SBruce Allan 	netdev->vlan_features |= (NETIF_F_SG |
7543dc221294SBruce Allan 				  NETIF_F_TSO |
7544dc221294SBruce Allan 				  NETIF_F_TSO6 |
7545dc221294SBruce Allan 				  NETIF_F_HW_CSUM);
7546dee1ad47SJeff Kirsher 
7547ef9b965aSJesse Brandeburg 	netdev->priv_flags |= IFF_UNICAST_FLT;
7548ef9b965aSJesse Brandeburg 
7549dee1ad47SJeff Kirsher 	netdev->features |= NETIF_F_HIGHDMA;
7550dee1ad47SJeff Kirsher 	netdev->vlan_features |= NETIF_F_HIGHDMA;
7551dee1ad47SJeff Kirsher 
755291c527a5SJarod Wilson 	/* MTU range: 68 - max_hw_frame_size */
755391c527a5SJarod Wilson 	netdev->min_mtu = ETH_MIN_MTU;
755491c527a5SJarod Wilson 	netdev->max_mtu = adapter->max_hw_frame_size -
755591c527a5SJarod Wilson 			  (VLAN_ETH_HLEN + ETH_FCS_LEN);
755691c527a5SJarod Wilson 
7557dee1ad47SJeff Kirsher 	if (e1000e_enable_mng_pass_thru(&adapter->hw))
7558dee1ad47SJeff Kirsher 		adapter->flags |= FLAG_MNG_PT_ENABLED;
7559dee1ad47SJeff Kirsher 
7560e921eb1aSBruce Allan 	/* before reading the NVM, reset the controller to
7561dee1ad47SJeff Kirsher 	 * put the device in a known good starting state
7562dee1ad47SJeff Kirsher 	 */
7563dee1ad47SJeff Kirsher 	adapter->hw.mac.ops.reset_hw(&adapter->hw);
7564dee1ad47SJeff Kirsher 
7565e921eb1aSBruce Allan 	/* systems with ASPM and others may see the checksum fail on the first
7566dee1ad47SJeff Kirsher 	 * attempt. Let's give it a few tries
7567dee1ad47SJeff Kirsher 	 */
7568dee1ad47SJeff Kirsher 	for (i = 0;; i++) {
7569dee1ad47SJeff Kirsher 		if (e1000_validate_nvm_checksum(&adapter->hw) >= 0)
7570dee1ad47SJeff Kirsher 			break;
7571dee1ad47SJeff Kirsher 		if (i == 2) {
7572185095fbSBruce Allan 			dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
7573dee1ad47SJeff Kirsher 			err = -EIO;
7574dee1ad47SJeff Kirsher 			goto err_eeprom;
7575dee1ad47SJeff Kirsher 		}
7576dee1ad47SJeff Kirsher 	}
7577dee1ad47SJeff Kirsher 
7578dee1ad47SJeff Kirsher 	e1000_eeprom_checks(adapter);
7579dee1ad47SJeff Kirsher 
7580dee1ad47SJeff Kirsher 	/* copy the MAC address */
7581dee1ad47SJeff Kirsher 	if (e1000e_read_mac_addr(&adapter->hw))
7582185095fbSBruce Allan 		dev_err(&pdev->dev,
7583185095fbSBruce Allan 			"NVM Read Error while reading MAC address\n");
7584dee1ad47SJeff Kirsher 
7585a05e4c0aSJakub Kicinski 	eth_hw_addr_set(netdev, adapter->hw.mac.addr);
7586dee1ad47SJeff Kirsher 
7587aaeb6cdfSJiri Pirko 	if (!is_valid_ether_addr(netdev->dev_addr)) {
7588185095fbSBruce Allan 		dev_err(&pdev->dev, "Invalid MAC Address: %pM\n",
7589aaeb6cdfSJiri Pirko 			netdev->dev_addr);
7590dee1ad47SJeff Kirsher 		err = -EIO;
7591dee1ad47SJeff Kirsher 		goto err_eeprom;
7592dee1ad47SJeff Kirsher 	}
7593dee1ad47SJeff Kirsher 
7594d5ad7a6aSJeff Kirsher 	timer_setup(&adapter->watchdog_timer, e1000_watchdog, 0);
759526566eaeSKees Cook 	timer_setup(&adapter->phy_info_timer, e1000_update_phy_info, 0);
7596dee1ad47SJeff Kirsher 
7597dee1ad47SJeff Kirsher 	INIT_WORK(&adapter->reset_task, e1000_reset_task);
7598d5ad7a6aSJeff Kirsher 	INIT_WORK(&adapter->watchdog_task, e1000_watchdog_task);
7599dee1ad47SJeff Kirsher 	INIT_WORK(&adapter->downshift_task, e1000e_downshift_workaround);
7600dee1ad47SJeff Kirsher 	INIT_WORK(&adapter->update_phy_task, e1000e_update_phy_task);
7601dee1ad47SJeff Kirsher 	INIT_WORK(&adapter->print_hang_task, e1000_print_hw_hang);
7602dee1ad47SJeff Kirsher 
7603dee1ad47SJeff Kirsher 	/* Initialize link parameters. User can change them with ethtool */
7604dee1ad47SJeff Kirsher 	adapter->hw.mac.autoneg = 1;
76053db1cd5cSRusty Russell 	adapter->fc_autoneg = true;
7606dee1ad47SJeff Kirsher 	adapter->hw.fc.requested_mode = e1000_fc_default;
7607dee1ad47SJeff Kirsher 	adapter->hw.fc.current_mode = e1000_fc_default;
7608dee1ad47SJeff Kirsher 	adapter->hw.phy.autoneg_advertised = 0x2f;
7609dee1ad47SJeff Kirsher 
7610e921eb1aSBruce Allan 	/* Initial Wake on LAN setting - If APM wake is enabled in
7611dee1ad47SJeff Kirsher 	 * the EEPROM, enable the ACPI Magic Packet filter
7612dee1ad47SJeff Kirsher 	 */
7613dee1ad47SJeff Kirsher 	if (adapter->flags & FLAG_APME_IN_WUC) {
7614dee1ad47SJeff Kirsher 		/* APME bit in EEPROM is mapped to WUC.APME */
7615dee1ad47SJeff Kirsher 		eeprom_data = er32(WUC);
7616dee1ad47SJeff Kirsher 		eeprom_apme_mask = E1000_WUC_APME;
7617dee1ad47SJeff Kirsher 		if ((hw->mac.type > e1000_ich10lan) &&
7618dee1ad47SJeff Kirsher 		    (eeprom_data & E1000_WUC_PHY_WAKE))
7619dee1ad47SJeff Kirsher 			adapter->flags2 |= FLAG2_HAS_PHY_WAKEUP;
7620dee1ad47SJeff Kirsher 	} else if (adapter->flags & FLAG_APME_IN_CTRL3) {
7621dee1ad47SJeff Kirsher 		if (adapter->flags & FLAG_APME_CHECK_PORT_B &&
7622dee1ad47SJeff Kirsher 		    (adapter->hw.bus.func == 1))
7623847042a6SBrian Walsh 			ret_val = e1000_read_nvm(&adapter->hw,
7624491a04d2SDavid Ertman 					      NVM_INIT_CONTROL3_PORT_B,
76253d3a1676SBruce Allan 					      1, &eeprom_data);
7626dee1ad47SJeff Kirsher 		else
7627847042a6SBrian Walsh 			ret_val = e1000_read_nvm(&adapter->hw,
7628491a04d2SDavid Ertman 					      NVM_INIT_CONTROL3_PORT_A,
76293d3a1676SBruce Allan 					      1, &eeprom_data);
7630dee1ad47SJeff Kirsher 	}
7631dee1ad47SJeff Kirsher 
7632dee1ad47SJeff Kirsher 	/* fetch WoL from EEPROM */
7633847042a6SBrian Walsh 	if (ret_val)
7634847042a6SBrian Walsh 		e_dbg("NVM read error getting WoL initial values: %d\n", ret_val);
7635491a04d2SDavid Ertman 	else if (eeprom_data & eeprom_apme_mask)
7636dee1ad47SJeff Kirsher 		adapter->eeprom_wol |= E1000_WUFC_MAG;
7637dee1ad47SJeff Kirsher 
7638e921eb1aSBruce Allan 	/* now that we have the eeprom settings, apply the special cases
7639dee1ad47SJeff Kirsher 	 * where the eeprom may be wrong or the board simply won't support
7640dee1ad47SJeff Kirsher 	 * wake on lan on a particular port
7641dee1ad47SJeff Kirsher 	 */
7642dee1ad47SJeff Kirsher 	if (!(adapter->flags & FLAG_HAS_WOL))
7643dee1ad47SJeff Kirsher 		adapter->eeprom_wol = 0;
7644dee1ad47SJeff Kirsher 
7645dee1ad47SJeff Kirsher 	/* initialize the wol settings based on the eeprom settings */
7646dee1ad47SJeff Kirsher 	adapter->wol = adapter->eeprom_wol;
764766148babSKonstantin Khlebnikov 
764866148babSKonstantin Khlebnikov 	/* make sure adapter isn't asleep if manageability is enabled */
764966148babSKonstantin Khlebnikov 	if (adapter->wol || (adapter->flags & FLAG_MNG_PT_ENABLED) ||
765066148babSKonstantin Khlebnikov 	    (hw->mac.ops.check_mng_mode(hw)))
765166148babSKonstantin Khlebnikov 		device_wakeup_enable(&pdev->dev);
7652dee1ad47SJeff Kirsher 
7653dee1ad47SJeff Kirsher 	/* save off EEPROM version number */
7654847042a6SBrian Walsh 	ret_val = e1000_read_nvm(&adapter->hw, 5, 1, &adapter->eeprom_vers);
7655491a04d2SDavid Ertman 
7656847042a6SBrian Walsh 	if (ret_val) {
7657847042a6SBrian Walsh 		e_dbg("NVM read error getting EEPROM version: %d\n", ret_val);
7658491a04d2SDavid Ertman 		adapter->eeprom_vers = 0;
7659491a04d2SDavid Ertman 	}
7660dee1ad47SJeff Kirsher 
7661aa524b66SJacob Keller 	/* init PTP hardware clock */
7662aa524b66SJacob Keller 	e1000e_ptp_init(adapter);
7663aa524b66SJacob Keller 
7664dee1ad47SJeff Kirsher 	/* reset the hardware with the new settings */
7665dee1ad47SJeff Kirsher 	e1000e_reset(adapter);
7666dee1ad47SJeff Kirsher 
7667e921eb1aSBruce Allan 	/* If the controller has AMT, do not set DRV_LOAD until the interface
7668dee1ad47SJeff Kirsher 	 * is up.  For all other cases, let the f/w know that the h/w is now
7669dee1ad47SJeff Kirsher 	 * under the control of the driver.
7670dee1ad47SJeff Kirsher 	 */
7671dee1ad47SJeff Kirsher 	if (!(adapter->flags & FLAG_HAS_AMT))
7672dee1ad47SJeff Kirsher 		e1000e_get_hw_control(adapter);
7673dee1ad47SJeff Kirsher 
76743c98cbf2SMario Limonciello 	if (hw->mac.type >= e1000_pch_cnp)
76753c98cbf2SMario Limonciello 		adapter->flags2 |= FLAG2_ENABLE_S0IX_FLOWS;
76763c98cbf2SMario Limonciello 
7677f029c781SWolfram Sang 	strscpy(netdev->name, "eth%d", sizeof(netdev->name));
7678dee1ad47SJeff Kirsher 	err = register_netdev(netdev);
7679dee1ad47SJeff Kirsher 	if (err)
7680dee1ad47SJeff Kirsher 		goto err_register;
7681dee1ad47SJeff Kirsher 
7682dee1ad47SJeff Kirsher 	/* carrier off reporting is important to ethtool even BEFORE open */
7683dee1ad47SJeff Kirsher 	netif_carrier_off(netdev);
7684dee1ad47SJeff Kirsher 
7685dee1ad47SJeff Kirsher 	e1000_print_device_info(adapter);
7686dee1ad47SJeff Kirsher 
7687ccf8b940SChen Yu 	dev_pm_set_driver_flags(&pdev->dev, DPM_FLAG_SMART_PREPARE);
768859f58708SKai-Heng Feng 
7689d1470851SKai-Heng Feng 	if (pci_dev_run_wake(pdev))
7690dee1ad47SJeff Kirsher 		pm_runtime_put_noidle(&pdev->dev);
7691dee1ad47SJeff Kirsher 
7692dee1ad47SJeff Kirsher 	return 0;
7693dee1ad47SJeff Kirsher 
7694dee1ad47SJeff Kirsher err_register:
7695dee1ad47SJeff Kirsher 	if (!(adapter->flags & FLAG_HAS_AMT))
7696dee1ad47SJeff Kirsher 		e1000e_release_hw_control(adapter);
7697dee1ad47SJeff Kirsher err_eeprom:
7698470a5420SBruce Allan 	if (hw->phy.ops.check_reset_block && !hw->phy.ops.check_reset_block(hw))
7699dee1ad47SJeff Kirsher 		e1000_phy_hw_reset(&adapter->hw);
7700dee1ad47SJeff Kirsher err_hw_init:
7701dee1ad47SJeff Kirsher 	kfree(adapter->tx_ring);
7702dee1ad47SJeff Kirsher 	kfree(adapter->rx_ring);
7703dee1ad47SJeff Kirsher err_sw_init:
77041103a631SYanir Lubetkin 	if ((adapter->hw.flash_address) && (hw->mac.type < e1000_pch_spt))
7705dee1ad47SJeff Kirsher 		iounmap(adapter->hw.flash_address);
7706dee1ad47SJeff Kirsher 	e1000e_reset_interrupt_capability(adapter);
7707dee1ad47SJeff Kirsher err_flashmap:
7708dee1ad47SJeff Kirsher 	iounmap(adapter->hw.hw_addr);
7709dee1ad47SJeff Kirsher err_ioremap:
7710dee1ad47SJeff Kirsher 	free_netdev(netdev);
7711dee1ad47SJeff Kirsher err_alloc_etherdev:
771256d766d6SJohannes Thumshirn 	pci_release_mem_regions(pdev);
7713dee1ad47SJeff Kirsher err_pci_reg:
7714dee1ad47SJeff Kirsher err_dma:
7715dee1ad47SJeff Kirsher 	pci_disable_device(pdev);
7716dee1ad47SJeff Kirsher 	return err;
7717dee1ad47SJeff Kirsher }
7718dee1ad47SJeff Kirsher 
7719dee1ad47SJeff Kirsher /**
7720dee1ad47SJeff Kirsher  * e1000_remove - Device Removal Routine
7721dee1ad47SJeff Kirsher  * @pdev: PCI device information struct
7722dee1ad47SJeff Kirsher  *
7723dee1ad47SJeff Kirsher  * e1000_remove is called by the PCI subsystem to alert the driver
7724e0bc64d3STree Davies  * that it should release a PCI device.  This could be caused by a
7725dee1ad47SJeff Kirsher  * Hot-Plug event, or because the driver is going to be removed from
7726dee1ad47SJeff Kirsher  * memory.
7727dee1ad47SJeff Kirsher  **/
e1000_remove(struct pci_dev * pdev)77289f9a12f8SBill Pemberton static void e1000_remove(struct pci_dev *pdev)
7729dee1ad47SJeff Kirsher {
7730dee1ad47SJeff Kirsher 	struct net_device *netdev = pci_get_drvdata(pdev);
7731dee1ad47SJeff Kirsher 	struct e1000_adapter *adapter = netdev_priv(netdev);
7732dee1ad47SJeff Kirsher 
7733d89777bfSBruce Allan 	e1000e_ptp_remove(adapter);
7734d89777bfSBruce Allan 
7735e921eb1aSBruce Allan 	/* The timers may be rescheduled, so explicitly disable them
7736dee1ad47SJeff Kirsher 	 * from being rescheduled.
7737dee1ad47SJeff Kirsher 	 */
7738dee1ad47SJeff Kirsher 	set_bit(__E1000_DOWN, &adapter->state);
7739d5ad7a6aSJeff Kirsher 	del_timer_sync(&adapter->watchdog_timer);
7740dee1ad47SJeff Kirsher 	del_timer_sync(&adapter->phy_info_timer);
7741dee1ad47SJeff Kirsher 
7742dee1ad47SJeff Kirsher 	cancel_work_sync(&adapter->reset_task);
7743d5ad7a6aSJeff Kirsher 	cancel_work_sync(&adapter->watchdog_task);
7744dee1ad47SJeff Kirsher 	cancel_work_sync(&adapter->downshift_task);
7745dee1ad47SJeff Kirsher 	cancel_work_sync(&adapter->update_phy_task);
7746dee1ad47SJeff Kirsher 	cancel_work_sync(&adapter->print_hang_task);
7747dee1ad47SJeff Kirsher 
7748b67e1913SBruce Allan 	if (adapter->flags & FLAG_HAS_HW_TIMESTAMP) {
7749b67e1913SBruce Allan 		cancel_work_sync(&adapter->tx_hwtstamp_work);
7750b67e1913SBruce Allan 		if (adapter->tx_hwtstamp_skb) {
7751377b6273SFlorian Fainelli 			dev_consume_skb_any(adapter->tx_hwtstamp_skb);
7752b67e1913SBruce Allan 			adapter->tx_hwtstamp_skb = NULL;
7753b67e1913SBruce Allan 		}
7754b67e1913SBruce Allan 	}
7755b67e1913SBruce Allan 
7756dee1ad47SJeff Kirsher 	unregister_netdev(netdev);
7757dee1ad47SJeff Kirsher 
7758dee1ad47SJeff Kirsher 	if (pci_dev_run_wake(pdev))
7759dee1ad47SJeff Kirsher 		pm_runtime_get_noresume(&pdev->dev);
7760dee1ad47SJeff Kirsher 
7761e921eb1aSBruce Allan 	/* Release control of h/w to f/w.  If f/w is AMT enabled, this
7762dee1ad47SJeff Kirsher 	 * would have already happened in close and is redundant.
7763dee1ad47SJeff Kirsher 	 */
7764dee1ad47SJeff Kirsher 	e1000e_release_hw_control(adapter);
7765dee1ad47SJeff Kirsher 
7766dee1ad47SJeff Kirsher 	e1000e_reset_interrupt_capability(adapter);
7767dee1ad47SJeff Kirsher 	kfree(adapter->tx_ring);
7768dee1ad47SJeff Kirsher 	kfree(adapter->rx_ring);
7769dee1ad47SJeff Kirsher 
7770dee1ad47SJeff Kirsher 	iounmap(adapter->hw.hw_addr);
77711103a631SYanir Lubetkin 	if ((adapter->hw.flash_address) &&
77721103a631SYanir Lubetkin 	    (adapter->hw.mac.type < e1000_pch_spt))
7773dee1ad47SJeff Kirsher 		iounmap(adapter->hw.flash_address);
777456d766d6SJohannes Thumshirn 	pci_release_mem_regions(pdev);
7775dee1ad47SJeff Kirsher 
7776dee1ad47SJeff Kirsher 	free_netdev(netdev);
7777dee1ad47SJeff Kirsher 
7778dee1ad47SJeff Kirsher 	pci_disable_device(pdev);
7779dee1ad47SJeff Kirsher }
7780dee1ad47SJeff Kirsher 
7781dee1ad47SJeff Kirsher /* PCI Error Recovery (ERS) */
77823646f0e5SStephen Hemminger static const struct pci_error_handlers e1000_err_handler = {
7783dee1ad47SJeff Kirsher 	.error_detected = e1000_io_error_detected,
7784dee1ad47SJeff Kirsher 	.slot_reset = e1000_io_slot_reset,
7785dee1ad47SJeff Kirsher 	.resume = e1000_io_resume,
7786dee1ad47SJeff Kirsher };
7787dee1ad47SJeff Kirsher 
77880e8e842bSDavid Ertman static const struct pci_device_id e1000_pci_tbl[] = {
7789dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_COPPER), board_82571 },
7790dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_FIBER), board_82571 },
7791dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER), board_82571 },
7792c29c3ba5SBruce Allan 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER_LP),
7793c29c3ba5SBruce Allan 	  board_82571 },
7794dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_FIBER), board_82571 },
7795dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES), board_82571 },
7796dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_DUAL), board_82571 },
7797dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_QUAD), board_82571 },
7798dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571PT_QUAD_COPPER), board_82571 },
7799dee1ad47SJeff Kirsher 
7800dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI), board_82572 },
7801dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_COPPER), board_82572 },
7802dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_FIBER), board_82572 },
7803dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_SERDES), board_82572 },
7804dee1ad47SJeff Kirsher 
7805dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E), board_82573 },
7806dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E_IAMT), board_82573 },
7807dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82573L), board_82573 },
7808dee1ad47SJeff Kirsher 
7809dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82574L), board_82574 },
7810dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82574LA), board_82574 },
7811dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82583V), board_82583 },
7812dee1ad47SJeff Kirsher 
7813dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_DPT),
7814dee1ad47SJeff Kirsher 	  board_80003es2lan },
7815dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_SPT),
7816dee1ad47SJeff Kirsher 	  board_80003es2lan },
7817dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_DPT),
7818dee1ad47SJeff Kirsher 	  board_80003es2lan },
7819dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_SPT),
7820dee1ad47SJeff Kirsher 	  board_80003es2lan },
7821dee1ad47SJeff Kirsher 
7822dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE), board_ich8lan },
7823dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_G), board_ich8lan },
7824dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_GT), board_ich8lan },
7825dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_AMT), board_ich8lan },
7826dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_C), board_ich8lan },
7827dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M), board_ich8lan },
7828dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M_AMT), board_ich8lan },
7829dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_82567V_3), board_ich8lan },
7830dee1ad47SJeff Kirsher 
7831dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE), board_ich9lan },
7832dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_G), board_ich9lan },
7833dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_GT), board_ich9lan },
7834dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_AMT), board_ich9lan },
7835dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_C), board_ich9lan },
7836dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_BM), board_ich9lan },
7837dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M), board_ich9lan },
7838dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_AMT), board_ich9lan },
7839dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_V), board_ich9lan },
7840dee1ad47SJeff Kirsher 
7841dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LM), board_ich9lan },
7842dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LF), board_ich9lan },
7843dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_V), board_ich9lan },
7844dee1ad47SJeff Kirsher 
7845dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_LM), board_ich10lan },
7846dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_LF), board_ich10lan },
7847dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_V), board_ich10lan },
7848dee1ad47SJeff Kirsher 
7849dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_M_HV_LM), board_pchlan },
7850dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_M_HV_LC), board_pchlan },
7851dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_D_HV_DM), board_pchlan },
7852dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_D_HV_DC), board_pchlan },
7853dee1ad47SJeff Kirsher 
7854dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH2_LV_LM), board_pch2lan },
7855dee1ad47SJeff Kirsher 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH2_LV_V), board_pch2lan },
7856dee1ad47SJeff Kirsher 
78572fbe4526SBruce Allan 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LPT_I217_LM), board_pch_lpt },
78582fbe4526SBruce Allan 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LPT_I217_V), board_pch_lpt },
785916e310aeSBruce Allan 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LPTLP_I218_LM), board_pch_lpt },
786016e310aeSBruce Allan 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LPTLP_I218_V), board_pch_lpt },
786191a3d82fSBruce Allan 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_I218_LM2), board_pch_lpt },
786291a3d82fSBruce Allan 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_I218_V2), board_pch_lpt },
786391a3d82fSBruce Allan 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_I218_LM3), board_pch_lpt },
786491a3d82fSBruce Allan 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_I218_V3), board_pch_lpt },
786579849ebcSDavid Ertman 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_LM), board_pch_spt },
786679849ebcSDavid Ertman 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_V), board_pch_spt },
786779849ebcSDavid Ertman 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_LM2), board_pch_spt },
786879849ebcSDavid Ertman 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_V2), board_pch_spt },
7869f3ed935dSRaanan Avargil 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LBG_I219_LM3), board_pch_spt },
78709cd34b3aSRaanan Avargil 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_LM4), board_pch_spt },
78719cd34b3aSRaanan Avargil 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_V4), board_pch_spt },
78729cd34b3aSRaanan Avargil 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_LM5), board_pch_spt },
78739cd34b3aSRaanan Avargil 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_V5), board_pch_spt },
78743a3173b9SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CNP_I219_LM6), board_pch_cnp },
78753a3173b9SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CNP_I219_V6), board_pch_cnp },
78763a3173b9SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CNP_I219_LM7), board_pch_cnp },
78773a3173b9SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CNP_I219_V7), board_pch_cnp },
787848f76b68SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ICP_I219_LM8), board_pch_cnp },
787948f76b68SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ICP_I219_V8), board_pch_cnp },
788048f76b68SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ICP_I219_LM9), board_pch_cnp },
788148f76b68SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ICP_I219_V9), board_pch_cnp },
7882914ee9c4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_LM10), board_pch_cnp },
7883914ee9c4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_V10), board_pch_cnp },
7884914ee9c4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_LM11), board_pch_cnp },
7885914ee9c4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_V11), board_pch_cnp },
7886914ee9c4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_LM12), board_pch_spt },
7887914ee9c4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_V12), board_pch_spt },
7888280db5d4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_TGP_I219_LM13), board_pch_tgp },
7889280db5d4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_TGP_I219_V13), board_pch_tgp },
7890280db5d4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_TGP_I219_LM14), board_pch_tgp },
7891280db5d4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_TGP_I219_V14), board_pch_tgp },
7892280db5d4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_TGP_I219_LM15), board_pch_tgp },
7893280db5d4SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_TGP_I219_V15), board_pch_tgp },
789468defd52SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_RPL_I219_LM23), board_pch_adp },
789568defd52SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_RPL_I219_V23), board_pch_adp },
789668defd52SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ADP_I219_LM16), board_pch_adp },
789768defd52SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ADP_I219_V16), board_pch_adp },
789868defd52SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ADP_I219_LM17), board_pch_adp },
789968defd52SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ADP_I219_V17), board_pch_adp },
790068defd52SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_RPL_I219_LM22), board_pch_adp },
790168defd52SSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_RPL_I219_V22), board_pch_adp },
7902*9d9e5347SVitaly Lifshits 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ADP_I219_LM19), board_pch_adp },
7903*9d9e5347SVitaly Lifshits 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ADP_I219_V19), board_pch_adp },
7904db2d737dSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_MTP_I219_LM18), board_pch_mtp },
7905db2d737dSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_MTP_I219_V18), board_pch_mtp },
7906db2d737dSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LNP_I219_LM20), board_pch_mtp },
7907db2d737dSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LNP_I219_V20), board_pch_mtp },
7908db2d737dSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LNP_I219_LM21), board_pch_mtp },
7909db2d737dSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LNP_I219_V21), board_pch_mtp },
79100c9183ceSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ARL_I219_LM24), board_pch_mtp },
79110c9183ceSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ARL_I219_V24), board_pch_mtp },
79120c9183ceSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_PTP_I219_LM25), board_pch_mtp },
79130c9183ceSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_PTP_I219_V25), board_pch_mtp },
79140c9183ceSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_PTP_I219_LM26), board_pch_mtp },
79150c9183ceSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_PTP_I219_V26), board_pch_mtp },
79160c9183ceSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_PTP_I219_LM27), board_pch_mtp },
79170c9183ceSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_PTP_I219_V27), board_pch_mtp },
79181fe4f45eSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_NVL_I219_LM29), board_pch_mtp },
79191fe4f45eSSasha Neftin 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_NVL_I219_V29), board_pch_mtp },
79202fbe4526SBruce Allan 
7921f36bb6caSBruce Allan 	{ 0, 0, 0, 0, 0, 0, 0 }	/* terminate list */
7922dee1ad47SJeff Kirsher };
7923dee1ad47SJeff Kirsher MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
7924dee1ad47SJeff Kirsher 
792575a3f93bSJesse Brandeburg static const struct dev_pm_ops e1000e_pm_ops = {
7926ccf8b940SChen Yu 	.prepare	= e1000e_pm_prepare,
792728002099SDavid Ertman 	.suspend	= e1000e_pm_suspend,
792828002099SDavid Ertman 	.resume		= e1000e_pm_resume,
792928002099SDavid Ertman 	.freeze		= e1000e_pm_freeze,
793028002099SDavid Ertman 	.thaw		= e1000e_pm_thaw,
793128002099SDavid Ertman 	.poweroff	= e1000e_pm_suspend,
793228002099SDavid Ertman 	.restore	= e1000e_pm_resume,
793375a3f93bSJesse Brandeburg 	RUNTIME_PM_OPS(e1000e_pm_runtime_suspend, e1000e_pm_runtime_resume,
793463eb48f1SDavid Ertman 		       e1000e_pm_runtime_idle)
7935dee1ad47SJeff Kirsher };
7936dee1ad47SJeff Kirsher 
7937dee1ad47SJeff Kirsher /* PCI Device API Driver */
7938dee1ad47SJeff Kirsher static struct pci_driver e1000_driver = {
7939dee1ad47SJeff Kirsher 	.name     = e1000e_driver_name,
7940dee1ad47SJeff Kirsher 	.id_table = e1000_pci_tbl,
7941dee1ad47SJeff Kirsher 	.probe    = e1000_probe,
79429f9a12f8SBill Pemberton 	.remove   = e1000_remove,
794375a3f93bSJesse Brandeburg 	.driver.pm = pm_ptr(&e1000e_pm_ops),
7944dee1ad47SJeff Kirsher 	.shutdown = e1000_shutdown,
7945dee1ad47SJeff Kirsher 	.err_handler = &e1000_err_handler
7946dee1ad47SJeff Kirsher };
7947dee1ad47SJeff Kirsher 
7948dee1ad47SJeff Kirsher /**
7949dee1ad47SJeff Kirsher  * e1000_init_module - Driver Registration Routine
7950dee1ad47SJeff Kirsher  *
7951dee1ad47SJeff Kirsher  * e1000_init_module is the first routine called when the driver is
7952dee1ad47SJeff Kirsher  * loaded. All it does is register with the PCI subsystem.
7953dee1ad47SJeff Kirsher  **/
e1000_init_module(void)7954dee1ad47SJeff Kirsher static int __init e1000_init_module(void)
7955dee1ad47SJeff Kirsher {
795634a2a3b8SJeff Kirsher 	pr_info("Intel(R) PRO/1000 Network Driver\n");
7957529498cdSYanir Lubetkin 	pr_info("Copyright(c) 1999 - 2015 Intel Corporation.\n");
7958dee1ad47SJeff Kirsher 
79595a5e889cSJean Sacren 	return pci_register_driver(&e1000_driver);
7960dee1ad47SJeff Kirsher }
7961dee1ad47SJeff Kirsher module_init(e1000_init_module);
7962dee1ad47SJeff Kirsher 
7963dee1ad47SJeff Kirsher /**
7964dee1ad47SJeff Kirsher  * e1000_exit_module - Driver Exit Cleanup Routine
7965dee1ad47SJeff Kirsher  *
7966dee1ad47SJeff Kirsher  * e1000_exit_module is called just before the driver is removed
7967dee1ad47SJeff Kirsher  * from memory.
7968dee1ad47SJeff Kirsher  **/
e1000_exit_module(void)7969dee1ad47SJeff Kirsher static void __exit e1000_exit_module(void)
7970dee1ad47SJeff Kirsher {
7971dee1ad47SJeff Kirsher 	pci_unregister_driver(&e1000_driver);
7972dee1ad47SJeff Kirsher }
7973dee1ad47SJeff Kirsher module_exit(e1000_exit_module);
7974dee1ad47SJeff Kirsher 
7975dee1ad47SJeff Kirsher MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
797698674ebeSJesse Brandeburg MODULE_LICENSE("GPL v2");
7977dee1ad47SJeff Kirsher 
797806c24b91SBruce Allan /* netdev.c */
7979