blob: 7ccd5f3cef61971c7a5493af5b9f0b2cd9c6829c [file] [log] [blame]
Auke Kokbc7f75f2007-09-17 12:30:59 -07001/*******************************************************************************
2
3 Intel PRO/1000 Linux driver
Bruce Allanf5e261e2012-01-01 16:00:03 +00004 Copyright(c) 1999 - 2012 Intel Corporation.
Auke Kokbc7f75f2007-09-17 12:30:59 -07005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
23 Linux NICS <linux.nics@intel.com>
24 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27*******************************************************************************/
28
Bruce Allan8544b9f2010-03-24 12:55:30 +000029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Auke Kokbc7f75f2007-09-17 12:30:59 -070031#include <linux/module.h>
32#include <linux/types.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/vmalloc.h>
36#include <linux/pagemap.h>
37#include <linux/delay.h>
38#include <linux/netdevice.h>
Bruce Allan9fb7a5f2011-07-29 05:52:51 +000039#include <linux/interrupt.h>
Auke Kokbc7f75f2007-09-17 12:30:59 -070040#include <linux/tcp.h>
41#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Auke Kokbc7f75f2007-09-17 12:30:59 -070043#include <net/checksum.h>
44#include <net/ip6_checksum.h>
45#include <linux/mii.h>
46#include <linux/ethtool.h>
47#include <linux/if_vlan.h>
48#include <linux/cpu.h>
49#include <linux/smp.h>
Linus Torvalds7e0bb712011-10-25 15:18:39 +020050#include <linux/pm_qos.h>
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +000051#include <linux/pm_runtime.h>
Jesse Brandeburg111b9dc2009-02-10 12:51:20 +000052#include <linux/aer.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040053#include <linux/prefetch.h>
Auke Kokbc7f75f2007-09-17 12:30:59 -070054
55#include "e1000.h"
56
Bruce Allanb3ccf262011-05-19 01:53:41 +000057#define DRV_EXTRAVERSION "-k"
Bruce Allanc14c6432010-06-16 13:28:34 +000058
Bruce Allan058e8ed2011-12-16 00:47:04 +000059#define DRV_VERSION "1.9.5" DRV_EXTRAVERSION
Auke Kokbc7f75f2007-09-17 12:30:59 -070060char e1000e_driver_name[] = "e1000e";
61const char e1000e_driver_version[] = DRV_VERSION;
62
Bruce Allan78cd29d2011-03-24 03:09:03 +000063static void e1000e_disable_aspm(struct pci_dev *pdev, u16 state);
64
Auke Kokbc7f75f2007-09-17 12:30:59 -070065static const struct e1000_info *e1000_info_tbl[] = {
66 [board_82571] = &e1000_82571_info,
67 [board_82572] = &e1000_82572_info,
68 [board_82573] = &e1000_82573_info,
Bruce Allan4662e822008-08-26 18:37:06 -070069 [board_82574] = &e1000_82574_info,
Alexander Duyck8c81c9c2009-03-19 01:12:27 +000070 [board_82583] = &e1000_82583_info,
Auke Kokbc7f75f2007-09-17 12:30:59 -070071 [board_80003es2lan] = &e1000_es2_info,
72 [board_ich8lan] = &e1000_ich8_info,
73 [board_ich9lan] = &e1000_ich9_info,
Bruce Allanf4187b52008-08-26 18:36:50 -070074 [board_ich10lan] = &e1000_ich10_info,
Bruce Allana4f58f52009-06-02 11:29:18 +000075 [board_pchlan] = &e1000_pch_info,
Bruce Alland3738bb2010-06-16 13:27:28 +000076 [board_pch2lan] = &e1000_pch2_info,
Auke Kokbc7f75f2007-09-17 12:30:59 -070077};
78
Taku Izumi84f4ee92010-04-27 14:39:08 +000079struct e1000_reg_info {
80 u32 ofs;
81 char *name;
82};
83
Bruce Allanaf667a22010-12-31 06:10:01 +000084#define E1000_RDFH 0x02410 /* Rx Data FIFO Head - RW */
85#define E1000_RDFT 0x02418 /* Rx Data FIFO Tail - RW */
86#define E1000_RDFHS 0x02420 /* Rx Data FIFO Head Saved - RW */
87#define E1000_RDFTS 0x02428 /* Rx Data FIFO Tail Saved - RW */
88#define E1000_RDFPC 0x02430 /* Rx Data FIFO Packet Count - RW */
Taku Izumi84f4ee92010-04-27 14:39:08 +000089
Bruce Allanaf667a22010-12-31 06:10:01 +000090#define E1000_TDFH 0x03410 /* Tx Data FIFO Head - RW */
91#define E1000_TDFT 0x03418 /* Tx Data FIFO Tail - RW */
92#define E1000_TDFHS 0x03420 /* Tx Data FIFO Head Saved - RW */
93#define E1000_TDFTS 0x03428 /* Tx Data FIFO Tail Saved - RW */
94#define E1000_TDFPC 0x03430 /* Tx Data FIFO Packet Count - RW */
Taku Izumi84f4ee92010-04-27 14:39:08 +000095
96static const struct e1000_reg_info e1000_reg_info_tbl[] = {
97
98 /* General Registers */
99 {E1000_CTRL, "CTRL"},
100 {E1000_STATUS, "STATUS"},
101 {E1000_CTRL_EXT, "CTRL_EXT"},
102
103 /* Interrupt Registers */
104 {E1000_ICR, "ICR"},
105
Bruce Allanaf667a22010-12-31 06:10:01 +0000106 /* Rx Registers */
Taku Izumi84f4ee92010-04-27 14:39:08 +0000107 {E1000_RCTL, "RCTL"},
108 {E1000_RDLEN, "RDLEN"},
109 {E1000_RDH, "RDH"},
110 {E1000_RDT, "RDT"},
111 {E1000_RDTR, "RDTR"},
112 {E1000_RXDCTL(0), "RXDCTL"},
113 {E1000_ERT, "ERT"},
114 {E1000_RDBAL, "RDBAL"},
115 {E1000_RDBAH, "RDBAH"},
116 {E1000_RDFH, "RDFH"},
117 {E1000_RDFT, "RDFT"},
118 {E1000_RDFHS, "RDFHS"},
119 {E1000_RDFTS, "RDFTS"},
120 {E1000_RDFPC, "RDFPC"},
121
Bruce Allanaf667a22010-12-31 06:10:01 +0000122 /* Tx Registers */
Taku Izumi84f4ee92010-04-27 14:39:08 +0000123 {E1000_TCTL, "TCTL"},
124 {E1000_TDBAL, "TDBAL"},
125 {E1000_TDBAH, "TDBAH"},
126 {E1000_TDLEN, "TDLEN"},
127 {E1000_TDH, "TDH"},
128 {E1000_TDT, "TDT"},
129 {E1000_TIDV, "TIDV"},
130 {E1000_TXDCTL(0), "TXDCTL"},
131 {E1000_TADV, "TADV"},
132 {E1000_TARC(0), "TARC"},
133 {E1000_TDFH, "TDFH"},
134 {E1000_TDFT, "TDFT"},
135 {E1000_TDFHS, "TDFHS"},
136 {E1000_TDFTS, "TDFTS"},
137 {E1000_TDFPC, "TDFPC"},
138
139 /* List Terminator */
Bruce Allanf36bb6c2012-01-31 06:38:04 +0000140 {0, NULL}
Taku Izumi84f4ee92010-04-27 14:39:08 +0000141};
142
143/*
144 * e1000_regdump - register printout routine
145 */
146static void e1000_regdump(struct e1000_hw *hw, struct e1000_reg_info *reginfo)
147{
148 int n = 0;
149 char rname[16];
150 u32 regs[8];
151
152 switch (reginfo->ofs) {
153 case E1000_RXDCTL(0):
154 for (n = 0; n < 2; n++)
155 regs[n] = __er32(hw, E1000_RXDCTL(n));
156 break;
157 case E1000_TXDCTL(0):
158 for (n = 0; n < 2; n++)
159 regs[n] = __er32(hw, E1000_TXDCTL(n));
160 break;
161 case E1000_TARC(0):
162 for (n = 0; n < 2; n++)
163 regs[n] = __er32(hw, E1000_TARC(n));
164 break;
165 default:
Jeff Kirsheref456f82011-11-03 11:40:28 +0000166 pr_info("%-15s %08x\n",
167 reginfo->name, __er32(hw, reginfo->ofs));
Taku Izumi84f4ee92010-04-27 14:39:08 +0000168 return;
169 }
170
171 snprintf(rname, 16, "%s%s", reginfo->name, "[0-1]");
Jeff Kirsheref456f82011-11-03 11:40:28 +0000172 pr_info("%-15s %08x %08x\n", rname, regs[0], regs[1]);
Taku Izumi84f4ee92010-04-27 14:39:08 +0000173}
174
Taku Izumi84f4ee92010-04-27 14:39:08 +0000175/*
Bruce Allanaf667a22010-12-31 06:10:01 +0000176 * e1000e_dump - Print registers, Tx-ring and Rx-ring
Taku Izumi84f4ee92010-04-27 14:39:08 +0000177 */
178static void e1000e_dump(struct e1000_adapter *adapter)
179{
180 struct net_device *netdev = adapter->netdev;
181 struct e1000_hw *hw = &adapter->hw;
182 struct e1000_reg_info *reginfo;
183 struct e1000_ring *tx_ring = adapter->tx_ring;
184 struct e1000_tx_desc *tx_desc;
Bruce Allanaf667a22010-12-31 06:10:01 +0000185 struct my_u0 {
Bruce Allane885d762012-01-31 06:37:32 +0000186 __le64 a;
187 __le64 b;
Bruce Allanaf667a22010-12-31 06:10:01 +0000188 } *u0;
Taku Izumi84f4ee92010-04-27 14:39:08 +0000189 struct e1000_buffer *buffer_info;
190 struct e1000_ring *rx_ring = adapter->rx_ring;
191 union e1000_rx_desc_packet_split *rx_desc_ps;
Bruce Allan5f450212011-07-22 06:21:46 +0000192 union e1000_rx_desc_extended *rx_desc;
Bruce Allanaf667a22010-12-31 06:10:01 +0000193 struct my_u1 {
Bruce Allane885d762012-01-31 06:37:32 +0000194 __le64 a;
195 __le64 b;
196 __le64 c;
197 __le64 d;
Bruce Allanaf667a22010-12-31 06:10:01 +0000198 } *u1;
Taku Izumi84f4ee92010-04-27 14:39:08 +0000199 u32 staterr;
200 int i = 0;
201
202 if (!netif_msg_hw(adapter))
203 return;
204
205 /* Print netdevice Info */
206 if (netdev) {
207 dev_info(&adapter->pdev->dev, "Net device Info\n");
Jeff Kirsheref456f82011-11-03 11:40:28 +0000208 pr_info("Device Name state trans_start last_rx\n");
209 pr_info("%-15s %016lX %016lX %016lX\n",
210 netdev->name, netdev->state, netdev->trans_start,
211 netdev->last_rx);
Taku Izumi84f4ee92010-04-27 14:39:08 +0000212 }
213
214 /* Print Registers */
215 dev_info(&adapter->pdev->dev, "Register Dump\n");
Jeff Kirsheref456f82011-11-03 11:40:28 +0000216 pr_info(" Register Name Value\n");
Taku Izumi84f4ee92010-04-27 14:39:08 +0000217 for (reginfo = (struct e1000_reg_info *)e1000_reg_info_tbl;
218 reginfo->name; reginfo++) {
219 e1000_regdump(hw, reginfo);
220 }
221
Bruce Allanaf667a22010-12-31 06:10:01 +0000222 /* Print Tx Ring Summary */
Taku Izumi84f4ee92010-04-27 14:39:08 +0000223 if (!netdev || !netif_running(netdev))
Bruce Allanfe1e9802012-01-31 06:37:54 +0000224 return;
Taku Izumi84f4ee92010-04-27 14:39:08 +0000225
Bruce Allanaf667a22010-12-31 06:10:01 +0000226 dev_info(&adapter->pdev->dev, "Tx Ring Summary\n");
Jeff Kirsheref456f82011-11-03 11:40:28 +0000227 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
Taku Izumi84f4ee92010-04-27 14:39:08 +0000228 buffer_info = &tx_ring->buffer_info[tx_ring->next_to_clean];
Jeff Kirsheref456f82011-11-03 11:40:28 +0000229 pr_info(" %5d %5X %5X %016llX %04X %3X %016llX\n",
230 0, tx_ring->next_to_use, tx_ring->next_to_clean,
231 (unsigned long long)buffer_info->dma,
232 buffer_info->length,
233 buffer_info->next_to_watch,
234 (unsigned long long)buffer_info->time_stamp);
Taku Izumi84f4ee92010-04-27 14:39:08 +0000235
Bruce Allanaf667a22010-12-31 06:10:01 +0000236 /* Print Tx Ring */
Taku Izumi84f4ee92010-04-27 14:39:08 +0000237 if (!netif_msg_tx_done(adapter))
238 goto rx_ring_summary;
239
Bruce Allanaf667a22010-12-31 06:10:01 +0000240 dev_info(&adapter->pdev->dev, "Tx Ring Dump\n");
Taku Izumi84f4ee92010-04-27 14:39:08 +0000241
242 /* Transmit Descriptor Formats - DEXT[29] is 0 (Legacy) or 1 (Extended)
243 *
244 * Legacy Transmit Descriptor
245 * +--------------------------------------------------------------+
246 * 0 | Buffer Address [63:0] (Reserved on Write Back) |
247 * +--------------------------------------------------------------+
248 * 8 | Special | CSS | Status | CMD | CSO | Length |
249 * +--------------------------------------------------------------+
250 * 63 48 47 36 35 32 31 24 23 16 15 0
251 *
252 * Extended Context Descriptor (DTYP=0x0) for TSO or checksum offload
253 * 63 48 47 40 39 32 31 16 15 8 7 0
254 * +----------------------------------------------------------------+
255 * 0 | TUCSE | TUCS0 | TUCSS | IPCSE | IPCS0 | IPCSS |
256 * +----------------------------------------------------------------+
257 * 8 | MSS | HDRLEN | RSV | STA | TUCMD | DTYP | PAYLEN |
258 * +----------------------------------------------------------------+
259 * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
260 *
261 * Extended Data Descriptor (DTYP=0x1)
262 * +----------------------------------------------------------------+
263 * 0 | Buffer Address [63:0] |
264 * +----------------------------------------------------------------+
265 * 8 | VLAN tag | POPTS | Rsvd | Status | Command | DTYP | DTALEN |
266 * +----------------------------------------------------------------+
267 * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
268 */
Jeff Kirsheref456f82011-11-03 11:40:28 +0000269 pr_info("Tl[desc] [address 63:0 ] [SpeCssSCmCsLen] [bi->dma ] leng ntw timestamp bi->skb <-- Legacy format\n");
270 pr_info("Tc[desc] [Ce CoCsIpceCoS] [MssHlRSCm0Plen] [bi->dma ] leng ntw timestamp bi->skb <-- Ext Context format\n");
271 pr_info("Td[desc] [address 63:0 ] [VlaPoRSCm1Dlen] [bi->dma ] leng ntw timestamp bi->skb <-- Ext Data format\n");
Taku Izumi84f4ee92010-04-27 14:39:08 +0000272 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
Jeff Kirsheref456f82011-11-03 11:40:28 +0000273 const char *next_desc;
Taku Izumi84f4ee92010-04-27 14:39:08 +0000274 tx_desc = E1000_TX_DESC(*tx_ring, i);
275 buffer_info = &tx_ring->buffer_info[i];
276 u0 = (struct my_u0 *)tx_desc;
Taku Izumi84f4ee92010-04-27 14:39:08 +0000277 if (i == tx_ring->next_to_use && i == tx_ring->next_to_clean)
Jeff Kirsheref456f82011-11-03 11:40:28 +0000278 next_desc = " NTC/U";
Taku Izumi84f4ee92010-04-27 14:39:08 +0000279 else if (i == tx_ring->next_to_use)
Jeff Kirsheref456f82011-11-03 11:40:28 +0000280 next_desc = " NTU";
Taku Izumi84f4ee92010-04-27 14:39:08 +0000281 else if (i == tx_ring->next_to_clean)
Jeff Kirsheref456f82011-11-03 11:40:28 +0000282 next_desc = " NTC";
Taku Izumi84f4ee92010-04-27 14:39:08 +0000283 else
Jeff Kirsheref456f82011-11-03 11:40:28 +0000284 next_desc = "";
285 pr_info("T%c[0x%03X] %016llX %016llX %016llX %04X %3X %016llX %p%s\n",
286 (!(le64_to_cpu(u0->b) & (1 << 29)) ? 'l' :
287 ((le64_to_cpu(u0->b) & (1 << 20)) ? 'd' : 'c')),
288 i,
289 (unsigned long long)le64_to_cpu(u0->a),
290 (unsigned long long)le64_to_cpu(u0->b),
291 (unsigned long long)buffer_info->dma,
292 buffer_info->length, buffer_info->next_to_watch,
293 (unsigned long long)buffer_info->time_stamp,
294 buffer_info->skb, next_desc);
Taku Izumi84f4ee92010-04-27 14:39:08 +0000295
296 if (netif_msg_pktdata(adapter) && buffer_info->dma != 0)
297 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS,
Bruce Allanaf667a22010-12-31 06:10:01 +0000298 16, 1, phys_to_virt(buffer_info->dma),
299 buffer_info->length, true);
Taku Izumi84f4ee92010-04-27 14:39:08 +0000300 }
301
Bruce Allanaf667a22010-12-31 06:10:01 +0000302 /* Print Rx Ring Summary */
Taku Izumi84f4ee92010-04-27 14:39:08 +0000303rx_ring_summary:
Bruce Allanaf667a22010-12-31 06:10:01 +0000304 dev_info(&adapter->pdev->dev, "Rx Ring Summary\n");
Jeff Kirsheref456f82011-11-03 11:40:28 +0000305 pr_info("Queue [NTU] [NTC]\n");
306 pr_info(" %5d %5X %5X\n",
307 0, rx_ring->next_to_use, rx_ring->next_to_clean);
Taku Izumi84f4ee92010-04-27 14:39:08 +0000308
Bruce Allanaf667a22010-12-31 06:10:01 +0000309 /* Print Rx Ring */
Taku Izumi84f4ee92010-04-27 14:39:08 +0000310 if (!netif_msg_rx_status(adapter))
Bruce Allanfe1e9802012-01-31 06:37:54 +0000311 return;
Taku Izumi84f4ee92010-04-27 14:39:08 +0000312
Bruce Allanaf667a22010-12-31 06:10:01 +0000313 dev_info(&adapter->pdev->dev, "Rx Ring Dump\n");
Taku Izumi84f4ee92010-04-27 14:39:08 +0000314 switch (adapter->rx_ps_pages) {
315 case 1:
316 case 2:
317 case 3:
318 /* [Extended] Packet Split Receive Descriptor Format
319 *
320 * +-----------------------------------------------------+
321 * 0 | Buffer Address 0 [63:0] |
322 * +-----------------------------------------------------+
323 * 8 | Buffer Address 1 [63:0] |
324 * +-----------------------------------------------------+
325 * 16 | Buffer Address 2 [63:0] |
326 * +-----------------------------------------------------+
327 * 24 | Buffer Address 3 [63:0] |
328 * +-----------------------------------------------------+
329 */
Jeff Kirsheref456f82011-11-03 11:40:28 +0000330 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");
Taku Izumi84f4ee92010-04-27 14:39:08 +0000331 /* [Extended] Receive Descriptor (Write-Back) Format
332 *
333 * 63 48 47 32 31 13 12 8 7 4 3 0
334 * +------------------------------------------------------+
335 * 0 | Packet | IP | Rsvd | MRQ | Rsvd | MRQ RSS |
336 * | Checksum | Ident | | Queue | | Type |
337 * +------------------------------------------------------+
338 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
339 * +------------------------------------------------------+
340 * 63 48 47 32 31 20 19 0
341 */
Jeff Kirsheref456f82011-11-03 11:40:28 +0000342 pr_info("RWB[desc] [ck ipid mrqhsh] [vl l0 ee es] [ l3 l2 l1 hs] [reserved ] ---------------- [bi->skb] <-- Ext Rx Write-Back format\n");
Taku Izumi84f4ee92010-04-27 14:39:08 +0000343 for (i = 0; i < rx_ring->count; i++) {
Jeff Kirsheref456f82011-11-03 11:40:28 +0000344 const char *next_desc;
Taku Izumi84f4ee92010-04-27 14:39:08 +0000345 buffer_info = &rx_ring->buffer_info[i];
346 rx_desc_ps = E1000_RX_DESC_PS(*rx_ring, i);
347 u1 = (struct my_u1 *)rx_desc_ps;
348 staterr =
Bruce Allanaf667a22010-12-31 06:10:01 +0000349 le32_to_cpu(rx_desc_ps->wb.middle.status_error);
Jeff Kirsheref456f82011-11-03 11:40:28 +0000350
351 if (i == rx_ring->next_to_use)
352 next_desc = " NTU";
353 else if (i == rx_ring->next_to_clean)
354 next_desc = " NTC";
355 else
356 next_desc = "";
357
Taku Izumi84f4ee92010-04-27 14:39:08 +0000358 if (staterr & E1000_RXD_STAT_DD) {
359 /* Descriptor Done */
Jeff Kirsheref456f82011-11-03 11:40:28 +0000360 pr_info("%s[0x%03X] %016llX %016llX %016llX %016llX ---------------- %p%s\n",
361 "RWB", i,
362 (unsigned long long)le64_to_cpu(u1->a),
363 (unsigned long long)le64_to_cpu(u1->b),
364 (unsigned long long)le64_to_cpu(u1->c),
365 (unsigned long long)le64_to_cpu(u1->d),
366 buffer_info->skb, next_desc);
Taku Izumi84f4ee92010-04-27 14:39:08 +0000367 } else {
Jeff Kirsheref456f82011-11-03 11:40:28 +0000368 pr_info("%s[0x%03X] %016llX %016llX %016llX %016llX %016llX %p%s\n",
369 "R ", i,
370 (unsigned long long)le64_to_cpu(u1->a),
371 (unsigned long long)le64_to_cpu(u1->b),
372 (unsigned long long)le64_to_cpu(u1->c),
373 (unsigned long long)le64_to_cpu(u1->d),
374 (unsigned long long)buffer_info->dma,
375 buffer_info->skb, next_desc);
Taku Izumi84f4ee92010-04-27 14:39:08 +0000376
377 if (netif_msg_pktdata(adapter))
378 print_hex_dump(KERN_INFO, "",
379 DUMP_PREFIX_ADDRESS, 16, 1,
380 phys_to_virt(buffer_info->dma),
381 adapter->rx_ps_bsize0, true);
382 }
Taku Izumi84f4ee92010-04-27 14:39:08 +0000383 }
384 break;
385 default:
386 case 0:
Bruce Allan5f450212011-07-22 06:21:46 +0000387 /* Extended Receive Descriptor (Read) Format
Taku Izumi84f4ee92010-04-27 14:39:08 +0000388 *
Bruce Allan5f450212011-07-22 06:21:46 +0000389 * +-----------------------------------------------------+
390 * 0 | Buffer Address [63:0] |
391 * +-----------------------------------------------------+
392 * 8 | Reserved |
393 * +-----------------------------------------------------+
Taku Izumi84f4ee92010-04-27 14:39:08 +0000394 */
Jeff Kirsheref456f82011-11-03 11:40:28 +0000395 pr_info("R [desc] [buf addr 63:0 ] [reserved 63:0 ] [bi->dma ] [bi->skb] <-- Ext (Read) format\n");
Bruce Allan5f450212011-07-22 06:21:46 +0000396 /* Extended Receive Descriptor (Write-Back) Format
397 *
398 * 63 48 47 32 31 24 23 4 3 0
399 * +------------------------------------------------------+
400 * | RSS Hash | | | |
401 * 0 +-------------------+ Rsvd | Reserved | MRQ RSS |
402 * | Packet | IP | | | Type |
403 * | Checksum | Ident | | | |
404 * +------------------------------------------------------+
405 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
406 * +------------------------------------------------------+
407 * 63 48 47 32 31 20 19 0
408 */
Jeff Kirsheref456f82011-11-03 11:40:28 +0000409 pr_info("RWB[desc] [cs ipid mrq] [vt ln xe xs] [bi->skb] <-- Ext (Write-Back) format\n");
Bruce Allan5f450212011-07-22 06:21:46 +0000410
411 for (i = 0; i < rx_ring->count; i++) {
Jeff Kirsheref456f82011-11-03 11:40:28 +0000412 const char *next_desc;
413
Taku Izumi84f4ee92010-04-27 14:39:08 +0000414 buffer_info = &rx_ring->buffer_info[i];
Bruce Allan5f450212011-07-22 06:21:46 +0000415 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
416 u1 = (struct my_u1 *)rx_desc;
417 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Jeff Kirsheref456f82011-11-03 11:40:28 +0000418
419 if (i == rx_ring->next_to_use)
420 next_desc = " NTU";
421 else if (i == rx_ring->next_to_clean)
422 next_desc = " NTC";
423 else
424 next_desc = "";
425
Bruce Allan5f450212011-07-22 06:21:46 +0000426 if (staterr & E1000_RXD_STAT_DD) {
427 /* Descriptor Done */
Jeff Kirsheref456f82011-11-03 11:40:28 +0000428 pr_info("%s[0x%03X] %016llX %016llX ---------------- %p%s\n",
429 "RWB", i,
430 (unsigned long long)le64_to_cpu(u1->a),
431 (unsigned long long)le64_to_cpu(u1->b),
432 buffer_info->skb, next_desc);
Bruce Allan5f450212011-07-22 06:21:46 +0000433 } else {
Jeff Kirsheref456f82011-11-03 11:40:28 +0000434 pr_info("%s[0x%03X] %016llX %016llX %016llX %p%s\n",
435 "R ", i,
436 (unsigned long long)le64_to_cpu(u1->a),
437 (unsigned long long)le64_to_cpu(u1->b),
438 (unsigned long long)buffer_info->dma,
439 buffer_info->skb, next_desc);
Bruce Allan5f450212011-07-22 06:21:46 +0000440
441 if (netif_msg_pktdata(adapter))
442 print_hex_dump(KERN_INFO, "",
443 DUMP_PREFIX_ADDRESS, 16,
444 1,
445 phys_to_virt
446 (buffer_info->dma),
447 adapter->rx_buffer_len,
448 true);
449 }
Taku Izumi84f4ee92010-04-27 14:39:08 +0000450 }
451 }
Taku Izumi84f4ee92010-04-27 14:39:08 +0000452}
453
Auke Kokbc7f75f2007-09-17 12:30:59 -0700454/**
455 * e1000_desc_unused - calculate if we have unused descriptors
456 **/
457static int e1000_desc_unused(struct e1000_ring *ring)
458{
459 if (ring->next_to_clean > ring->next_to_use)
460 return ring->next_to_clean - ring->next_to_use - 1;
461
462 return ring->count + ring->next_to_clean - ring->next_to_use - 1;
463}
464
465/**
Bruce Allanad680762008-03-28 09:15:03 -0700466 * e1000_receive_skb - helper function to handle Rx indications
Auke Kokbc7f75f2007-09-17 12:30:59 -0700467 * @adapter: board private structure
468 * @status: descriptor status field as written by hardware
469 * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
470 * @skb: pointer to sk_buff to be indicated to stack
471 **/
472static void e1000_receive_skb(struct e1000_adapter *adapter,
Bruce Allanaf667a22010-12-31 06:10:01 +0000473 struct net_device *netdev, struct sk_buff *skb,
Al Viroa39fe742007-12-11 19:50:34 +0000474 u8 status, __le16 vlan)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700475{
Jeff Kirsher86d70e52011-03-25 16:01:01 +0000476 u16 tag = le16_to_cpu(vlan);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700477 skb->protocol = eth_type_trans(skb, netdev);
478
Jeff Kirsher86d70e52011-03-25 16:01:01 +0000479 if (status & E1000_RXD_STAT_VP)
480 __vlan_hwaccel_put_tag(skb, tag);
481
482 napi_gro_receive(&adapter->napi, skb);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700483}
484
485/**
Bruce Allanaf667a22010-12-31 06:10:01 +0000486 * e1000_rx_checksum - Receive Checksum Offload
Bruce Allanafd12932012-01-05 00:34:05 +0000487 * @adapter: board private structure
488 * @status_err: receive descriptor status and error fields
489 * @csum: receive descriptor csum field
490 * @sk_buff: socket buffer with received data
Auke Kokbc7f75f2007-09-17 12:30:59 -0700491 **/
492static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
Bruce Allanafd12932012-01-05 00:34:05 +0000493 __le16 csum, struct sk_buff *skb)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700494{
495 u16 status = (u16)status_err;
496 u8 errors = (u8)(status_err >> 24);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700497
498 skb_checksum_none_assert(skb);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700499
Bruce Allanafd12932012-01-05 00:34:05 +0000500 /* Rx checksum disabled */
501 if (!(adapter->netdev->features & NETIF_F_RXCSUM))
502 return;
503
Auke Kokbc7f75f2007-09-17 12:30:59 -0700504 /* Ignore Checksum bit is set */
505 if (status & E1000_RXD_STAT_IXSM)
506 return;
Bruce Allanafd12932012-01-05 00:34:05 +0000507
Auke Kokbc7f75f2007-09-17 12:30:59 -0700508 /* TCP/UDP checksum error bit is set */
509 if (errors & E1000_RXD_ERR_TCPE) {
510 /* let the stack verify checksum errors */
511 adapter->hw_csum_err++;
512 return;
513 }
514
515 /* TCP/UDP Checksum has not been calculated */
516 if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
517 return;
518
519 /* It must be a TCP or UDP packet with a valid checksum */
520 if (status & E1000_RXD_STAT_TCPCS) {
521 /* TCP checksum is good */
522 skb->ip_summed = CHECKSUM_UNNECESSARY;
523 } else {
Bruce Allanad680762008-03-28 09:15:03 -0700524 /*
525 * IP fragment with UDP payload
526 * Hardware complements the payload checksum, so we undo it
Auke Kokbc7f75f2007-09-17 12:30:59 -0700527 * and then put the value in host order for further stack use.
528 */
Bruce Allanafd12932012-01-05 00:34:05 +0000529 __sum16 sum = (__force __sum16)swab16((__force u16)csum);
Al Viroa39fe742007-12-11 19:50:34 +0000530 skb->csum = csum_unfold(~sum);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700531 skb->ip_summed = CHECKSUM_COMPLETE;
532 }
533 adapter->hw_csum_good++;
534}
535
536/**
David S. Miller823dcd22011-08-20 10:39:12 -0700537 * e1000e_update_tail_wa - helper function for e1000e_update_[rt]dt_wa()
538 * @hw: pointer to the HW structure
539 * @tail: address of tail descriptor register
540 * @i: value to write to tail descriptor register
541 *
542 * When updating the tail register, the ME could be accessing Host CSR
543 * registers at the same time. Normally, this is handled in h/w by an
544 * arbiter but on some parts there is a bug that acknowledges Host accesses
545 * later than it should which could result in the descriptor register to
546 * have an incorrect value. Workaround this by checking the FWSM register
547 * which has bit 24 set while ME is accessing Host CSR registers, wait
548 * if it is set and try again a number of times.
549 **/
Bruce Allanc5083cf2011-12-16 00:45:40 +0000550static inline s32 e1000e_update_tail_wa(struct e1000_hw *hw, void __iomem *tail,
David S. Miller823dcd22011-08-20 10:39:12 -0700551 unsigned int i)
552{
553 unsigned int j = 0;
554
555 while ((j++ < E1000_ICH_FWSM_PCIM2PCI_COUNT) &&
556 (er32(FWSM) & E1000_ICH_FWSM_PCIM2PCI))
557 udelay(50);
558
559 writel(i, tail);
560
561 if ((j == E1000_ICH_FWSM_PCIM2PCI_COUNT) && (i != readl(tail)))
562 return E1000_ERR_SWFW_SYNC;
563
564 return 0;
565}
566
Bruce Allan55aa6982011-12-16 00:45:45 +0000567static void e1000e_update_rdt_wa(struct e1000_ring *rx_ring, unsigned int i)
David S. Miller823dcd22011-08-20 10:39:12 -0700568{
Bruce Allan55aa6982011-12-16 00:45:45 +0000569 struct e1000_adapter *adapter = rx_ring->adapter;
David S. Miller823dcd22011-08-20 10:39:12 -0700570 struct e1000_hw *hw = &adapter->hw;
571
Bruce Allan55aa6982011-12-16 00:45:45 +0000572 if (e1000e_update_tail_wa(hw, rx_ring->tail, i)) {
David S. Miller823dcd22011-08-20 10:39:12 -0700573 u32 rctl = er32(RCTL);
574 ew32(RCTL, rctl & ~E1000_RCTL_EN);
575 e_err("ME firmware caused invalid RDT - resetting\n");
576 schedule_work(&adapter->reset_task);
577 }
578}
579
Bruce Allan55aa6982011-12-16 00:45:45 +0000580static void e1000e_update_tdt_wa(struct e1000_ring *tx_ring, unsigned int i)
David S. Miller823dcd22011-08-20 10:39:12 -0700581{
Bruce Allan55aa6982011-12-16 00:45:45 +0000582 struct e1000_adapter *adapter = tx_ring->adapter;
David S. Miller823dcd22011-08-20 10:39:12 -0700583 struct e1000_hw *hw = &adapter->hw;
584
Bruce Allan55aa6982011-12-16 00:45:45 +0000585 if (e1000e_update_tail_wa(hw, tx_ring->tail, i)) {
David S. Miller823dcd22011-08-20 10:39:12 -0700586 u32 tctl = er32(TCTL);
587 ew32(TCTL, tctl & ~E1000_TCTL_EN);
588 e_err("ME firmware caused invalid TDT - resetting\n");
589 schedule_work(&adapter->reset_task);
590 }
591}
592
593/**
Bruce Allan5f450212011-07-22 06:21:46 +0000594 * e1000_alloc_rx_buffers - Replace used receive buffers
Bruce Allan55aa6982011-12-16 00:45:45 +0000595 * @rx_ring: Rx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -0700596 **/
Bruce Allan55aa6982011-12-16 00:45:45 +0000597static void e1000_alloc_rx_buffers(struct e1000_ring *rx_ring,
Jeff Kirsherc2fed992011-07-12 16:10:12 +0000598 int cleaned_count, gfp_t gfp)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700599{
Bruce Allan55aa6982011-12-16 00:45:45 +0000600 struct e1000_adapter *adapter = rx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700601 struct net_device *netdev = adapter->netdev;
602 struct pci_dev *pdev = adapter->pdev;
Bruce Allan5f450212011-07-22 06:21:46 +0000603 union e1000_rx_desc_extended *rx_desc;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700604 struct e1000_buffer *buffer_info;
605 struct sk_buff *skb;
606 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +0000607 unsigned int bufsz = adapter->rx_buffer_len;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700608
609 i = rx_ring->next_to_use;
610 buffer_info = &rx_ring->buffer_info[i];
611
612 while (cleaned_count--) {
613 skb = buffer_info->skb;
614 if (skb) {
615 skb_trim(skb, 0);
616 goto map_skb;
617 }
618
Jeff Kirsherc2fed992011-07-12 16:10:12 +0000619 skb = __netdev_alloc_skb_ip_align(netdev, bufsz, gfp);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700620 if (!skb) {
621 /* Better luck next round */
622 adapter->alloc_rx_buff_failed++;
623 break;
624 }
625
Auke Kokbc7f75f2007-09-17 12:30:59 -0700626 buffer_info->skb = skb;
627map_skb:
Nick Nunley0be3f552010-04-27 13:09:05 +0000628 buffer_info->dma = dma_map_single(&pdev->dev, skb->data,
Auke Kokbc7f75f2007-09-17 12:30:59 -0700629 adapter->rx_buffer_len,
Nick Nunley0be3f552010-04-27 13:09:05 +0000630 DMA_FROM_DEVICE);
631 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Bruce Allanaf667a22010-12-31 06:10:01 +0000632 dev_err(&pdev->dev, "Rx DMA map failed\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700633 adapter->rx_dma_failed++;
634 break;
635 }
636
Bruce Allan5f450212011-07-22 06:21:46 +0000637 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
638 rx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700639
Tom Herbert50849d72010-05-05 14:02:49 +0000640 if (unlikely(!(i & (E1000_RX_BUFFER_WRITE - 1)))) {
641 /*
642 * Force memory writes to complete before letting h/w
643 * know there are new descriptors to fetch. (Only
644 * applicable for weak-ordered memory model archs,
645 * such as IA-64).
646 */
647 wmb();
David S. Miller823dcd22011-08-20 10:39:12 -0700648 if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
Bruce Allan55aa6982011-12-16 00:45:45 +0000649 e1000e_update_rdt_wa(rx_ring, i);
David S. Miller823dcd22011-08-20 10:39:12 -0700650 else
Bruce Allanc5083cf2011-12-16 00:45:40 +0000651 writel(i, rx_ring->tail);
Tom Herbert50849d72010-05-05 14:02:49 +0000652 }
Auke Kokbc7f75f2007-09-17 12:30:59 -0700653 i++;
654 if (i == rx_ring->count)
655 i = 0;
656 buffer_info = &rx_ring->buffer_info[i];
657 }
658
Tom Herbert50849d72010-05-05 14:02:49 +0000659 rx_ring->next_to_use = i;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700660}
661
662/**
663 * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
Bruce Allan55aa6982011-12-16 00:45:45 +0000664 * @rx_ring: Rx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -0700665 **/
Bruce Allan55aa6982011-12-16 00:45:45 +0000666static void e1000_alloc_rx_buffers_ps(struct e1000_ring *rx_ring,
Jeff Kirsherc2fed992011-07-12 16:10:12 +0000667 int cleaned_count, gfp_t gfp)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700668{
Bruce Allan55aa6982011-12-16 00:45:45 +0000669 struct e1000_adapter *adapter = rx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700670 struct net_device *netdev = adapter->netdev;
671 struct pci_dev *pdev = adapter->pdev;
672 union e1000_rx_desc_packet_split *rx_desc;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700673 struct e1000_buffer *buffer_info;
674 struct e1000_ps_page *ps_page;
675 struct sk_buff *skb;
676 unsigned int i, j;
677
678 i = rx_ring->next_to_use;
679 buffer_info = &rx_ring->buffer_info[i];
680
681 while (cleaned_count--) {
682 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
683
684 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
Auke Kok47f44e42007-10-25 13:57:44 -0700685 ps_page = &buffer_info->ps_pages[j];
686 if (j >= adapter->rx_ps_pages) {
687 /* all unused desc entries get hw null ptr */
Bruce Allanaf667a22010-12-31 06:10:01 +0000688 rx_desc->read.buffer_addr[j + 1] =
689 ~cpu_to_le64(0);
Auke Kok47f44e42007-10-25 13:57:44 -0700690 continue;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700691 }
Auke Kok47f44e42007-10-25 13:57:44 -0700692 if (!ps_page->page) {
Jeff Kirsherc2fed992011-07-12 16:10:12 +0000693 ps_page->page = alloc_page(gfp);
Auke Kok47f44e42007-10-25 13:57:44 -0700694 if (!ps_page->page) {
695 adapter->alloc_rx_buff_failed++;
696 goto no_buffers;
697 }
Nick Nunley0be3f552010-04-27 13:09:05 +0000698 ps_page->dma = dma_map_page(&pdev->dev,
699 ps_page->page,
700 0, PAGE_SIZE,
701 DMA_FROM_DEVICE);
702 if (dma_mapping_error(&pdev->dev,
703 ps_page->dma)) {
Auke Kok47f44e42007-10-25 13:57:44 -0700704 dev_err(&adapter->pdev->dev,
Bruce Allanaf667a22010-12-31 06:10:01 +0000705 "Rx DMA page map failed\n");
Auke Kok47f44e42007-10-25 13:57:44 -0700706 adapter->rx_dma_failed++;
707 goto no_buffers;
708 }
709 }
710 /*
711 * Refresh the desc even if buffer_addrs
712 * didn't change because each write-back
713 * erases this info.
714 */
Bruce Allanaf667a22010-12-31 06:10:01 +0000715 rx_desc->read.buffer_addr[j + 1] =
716 cpu_to_le64(ps_page->dma);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700717 }
718
Jeff Kirsherc2fed992011-07-12 16:10:12 +0000719 skb = __netdev_alloc_skb_ip_align(netdev,
720 adapter->rx_ps_bsize0,
721 gfp);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700722
723 if (!skb) {
724 adapter->alloc_rx_buff_failed++;
725 break;
726 }
727
Auke Kokbc7f75f2007-09-17 12:30:59 -0700728 buffer_info->skb = skb;
Nick Nunley0be3f552010-04-27 13:09:05 +0000729 buffer_info->dma = dma_map_single(&pdev->dev, skb->data,
Auke Kokbc7f75f2007-09-17 12:30:59 -0700730 adapter->rx_ps_bsize0,
Nick Nunley0be3f552010-04-27 13:09:05 +0000731 DMA_FROM_DEVICE);
732 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
Bruce Allanaf667a22010-12-31 06:10:01 +0000733 dev_err(&pdev->dev, "Rx DMA map failed\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700734 adapter->rx_dma_failed++;
735 /* cleanup skb */
736 dev_kfree_skb_any(skb);
737 buffer_info->skb = NULL;
738 break;
739 }
740
741 rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
742
Tom Herbert50849d72010-05-05 14:02:49 +0000743 if (unlikely(!(i & (E1000_RX_BUFFER_WRITE - 1)))) {
744 /*
745 * Force memory writes to complete before letting h/w
746 * know there are new descriptors to fetch. (Only
747 * applicable for weak-ordered memory model archs,
748 * such as IA-64).
749 */
750 wmb();
David S. Miller823dcd22011-08-20 10:39:12 -0700751 if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
Bruce Allan55aa6982011-12-16 00:45:45 +0000752 e1000e_update_rdt_wa(rx_ring, i << 1);
David S. Miller823dcd22011-08-20 10:39:12 -0700753 else
Bruce Allanc5083cf2011-12-16 00:45:40 +0000754 writel(i << 1, rx_ring->tail);
Tom Herbert50849d72010-05-05 14:02:49 +0000755 }
756
Auke Kokbc7f75f2007-09-17 12:30:59 -0700757 i++;
758 if (i == rx_ring->count)
759 i = 0;
760 buffer_info = &rx_ring->buffer_info[i];
761 }
762
763no_buffers:
Tom Herbert50849d72010-05-05 14:02:49 +0000764 rx_ring->next_to_use = i;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700765}
766
767/**
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700768 * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
Bruce Allan55aa6982011-12-16 00:45:45 +0000769 * @rx_ring: Rx descriptor ring
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700770 * @cleaned_count: number of buffers to allocate this pass
771 **/
772
Bruce Allan55aa6982011-12-16 00:45:45 +0000773static void e1000_alloc_jumbo_rx_buffers(struct e1000_ring *rx_ring,
Jeff Kirsherc2fed992011-07-12 16:10:12 +0000774 int cleaned_count, gfp_t gfp)
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700775{
Bruce Allan55aa6982011-12-16 00:45:45 +0000776 struct e1000_adapter *adapter = rx_ring->adapter;
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700777 struct net_device *netdev = adapter->netdev;
778 struct pci_dev *pdev = adapter->pdev;
Bruce Allan5f450212011-07-22 06:21:46 +0000779 union e1000_rx_desc_extended *rx_desc;
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700780 struct e1000_buffer *buffer_info;
781 struct sk_buff *skb;
782 unsigned int i;
Eric Dumazet89d71a62009-10-13 05:34:20 +0000783 unsigned int bufsz = 256 - 16 /* for skb_reserve */;
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700784
785 i = rx_ring->next_to_use;
786 buffer_info = &rx_ring->buffer_info[i];
787
788 while (cleaned_count--) {
789 skb = buffer_info->skb;
790 if (skb) {
791 skb_trim(skb, 0);
792 goto check_page;
793 }
794
Jeff Kirsherc2fed992011-07-12 16:10:12 +0000795 skb = __netdev_alloc_skb_ip_align(netdev, bufsz, gfp);
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700796 if (unlikely(!skb)) {
797 /* Better luck next round */
798 adapter->alloc_rx_buff_failed++;
799 break;
800 }
801
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700802 buffer_info->skb = skb;
803check_page:
804 /* allocate a new page if necessary */
805 if (!buffer_info->page) {
Jeff Kirsherc2fed992011-07-12 16:10:12 +0000806 buffer_info->page = alloc_page(gfp);
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700807 if (unlikely(!buffer_info->page)) {
808 adapter->alloc_rx_buff_failed++;
809 break;
810 }
811 }
812
813 if (!buffer_info->dma)
Nick Nunley0be3f552010-04-27 13:09:05 +0000814 buffer_info->dma = dma_map_page(&pdev->dev,
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700815 buffer_info->page, 0,
816 PAGE_SIZE,
Nick Nunley0be3f552010-04-27 13:09:05 +0000817 DMA_FROM_DEVICE);
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700818
Bruce Allan5f450212011-07-22 06:21:46 +0000819 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
820 rx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700821
822 if (unlikely(++i == rx_ring->count))
823 i = 0;
824 buffer_info = &rx_ring->buffer_info[i];
825 }
826
827 if (likely(rx_ring->next_to_use != i)) {
828 rx_ring->next_to_use = i;
829 if (unlikely(i-- == 0))
830 i = (rx_ring->count - 1);
831
832 /* Force memory writes to complete before letting h/w
833 * know there are new descriptors to fetch. (Only
834 * applicable for weak-ordered memory model archs,
835 * such as IA-64). */
836 wmb();
David S. Miller823dcd22011-08-20 10:39:12 -0700837 if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
Bruce Allan55aa6982011-12-16 00:45:45 +0000838 e1000e_update_rdt_wa(rx_ring, i);
David S. Miller823dcd22011-08-20 10:39:12 -0700839 else
Bruce Allanc5083cf2011-12-16 00:45:40 +0000840 writel(i, rx_ring->tail);
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700841 }
842}
843
Bruce Allan70495a52012-01-11 01:26:50 +0000844static inline void e1000_rx_hash(struct net_device *netdev, __le32 rss,
845 struct sk_buff *skb)
846{
847 if (netdev->features & NETIF_F_RXHASH)
848 skb->rxhash = le32_to_cpu(rss);
849}
850
Bruce Allan97ac8ca2008-04-29 09:16:05 -0700851/**
Bruce Allan55aa6982011-12-16 00:45:45 +0000852 * e1000_clean_rx_irq - Send received data up the network stack
853 * @rx_ring: Rx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -0700854 *
855 * the return value indicates whether actual cleaning was done, there
856 * is no guarantee that everything was cleaned
857 **/
Bruce Allan55aa6982011-12-16 00:45:45 +0000858static bool e1000_clean_rx_irq(struct e1000_ring *rx_ring, int *work_done,
859 int work_to_do)
Auke Kokbc7f75f2007-09-17 12:30:59 -0700860{
Bruce Allan55aa6982011-12-16 00:45:45 +0000861 struct e1000_adapter *adapter = rx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700862 struct net_device *netdev = adapter->netdev;
863 struct pci_dev *pdev = adapter->pdev;
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000864 struct e1000_hw *hw = &adapter->hw;
Bruce Allan5f450212011-07-22 06:21:46 +0000865 union e1000_rx_desc_extended *rx_desc, *next_rxd;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700866 struct e1000_buffer *buffer_info, *next_buffer;
Bruce Allan5f450212011-07-22 06:21:46 +0000867 u32 length, staterr;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700868 unsigned int i;
869 int cleaned_count = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000870 bool cleaned = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700871 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
872
873 i = rx_ring->next_to_clean;
Bruce Allan5f450212011-07-22 06:21:46 +0000874 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
875 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700876 buffer_info = &rx_ring->buffer_info[i];
877
Bruce Allan5f450212011-07-22 06:21:46 +0000878 while (staterr & E1000_RXD_STAT_DD) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700879 struct sk_buff *skb;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700880
881 if (*work_done >= work_to_do)
882 break;
883 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +0000884 rmb(); /* read descriptor and rx_buffer_info after status DD */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700885
Auke Kokbc7f75f2007-09-17 12:30:59 -0700886 skb = buffer_info->skb;
887 buffer_info->skb = NULL;
888
889 prefetch(skb->data - NET_IP_ALIGN);
890
891 i++;
892 if (i == rx_ring->count)
893 i = 0;
Bruce Allan5f450212011-07-22 06:21:46 +0000894 next_rxd = E1000_RX_DESC_EXT(*rx_ring, i);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700895 prefetch(next_rxd);
896
897 next_buffer = &rx_ring->buffer_info[i];
898
Rusty Russell3db1cd52011-12-19 13:56:45 +0000899 cleaned = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700900 cleaned_count++;
Nick Nunley0be3f552010-04-27 13:09:05 +0000901 dma_unmap_single(&pdev->dev,
Auke Kokbc7f75f2007-09-17 12:30:59 -0700902 buffer_info->dma,
903 adapter->rx_buffer_len,
Nick Nunley0be3f552010-04-27 13:09:05 +0000904 DMA_FROM_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700905 buffer_info->dma = 0;
906
Bruce Allan5f450212011-07-22 06:21:46 +0000907 length = le16_to_cpu(rx_desc->wb.upper.length);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700908
Jesse Brandeburgb94b5022010-01-19 14:15:59 +0000909 /*
910 * !EOP means multiple descriptors were used to store a single
911 * packet, if that's the case we need to toss it. In fact, we
912 * need to toss every packet with the EOP bit clear and the
913 * next frame that _does_ have the EOP bit set, as it is by
914 * definition only a frame fragment
915 */
Bruce Allan5f450212011-07-22 06:21:46 +0000916 if (unlikely(!(staterr & E1000_RXD_STAT_EOP)))
Jesse Brandeburgb94b5022010-01-19 14:15:59 +0000917 adapter->flags2 |= FLAG2_IS_DISCARDING;
918
919 if (adapter->flags2 & FLAG2_IS_DISCARDING) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700920 /* All receives must fit into a single buffer */
Bruce Allan3bb99fe2009-11-20 23:25:07 +0000921 e_dbg("Receive packet consumed multiple buffers\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -0700922 /* recycle */
923 buffer_info->skb = skb;
Bruce Allan5f450212011-07-22 06:21:46 +0000924 if (staterr & E1000_RXD_STAT_EOP)
Jesse Brandeburgb94b5022010-01-19 14:15:59 +0000925 adapter->flags2 &= ~FLAG2_IS_DISCARDING;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700926 goto next_desc;
927 }
928
Bruce Allan5f450212011-07-22 06:21:46 +0000929 if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
Auke Kokbc7f75f2007-09-17 12:30:59 -0700930 /* recycle */
931 buffer_info->skb = skb;
932 goto next_desc;
933 }
934
Jeff Kirshereb7c3ad2008-11-14 06:45:23 +0000935 /* adjust length to remove Ethernet CRC */
936 if (!(adapter->flags2 & FLAG2_CRC_STRIPPING))
937 length -= 4;
938
Auke Kokbc7f75f2007-09-17 12:30:59 -0700939 total_rx_bytes += length;
940 total_rx_packets++;
941
Bruce Allanad680762008-03-28 09:15:03 -0700942 /*
943 * code added for copybreak, this should improve
Auke Kokbc7f75f2007-09-17 12:30:59 -0700944 * performance for small packets with large amounts
Bruce Allanad680762008-03-28 09:15:03 -0700945 * of reassembly being done in the stack
946 */
Auke Kokbc7f75f2007-09-17 12:30:59 -0700947 if (length < copybreak) {
948 struct sk_buff *new_skb =
Eric Dumazet89d71a62009-10-13 05:34:20 +0000949 netdev_alloc_skb_ip_align(netdev, length);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700950 if (new_skb) {
Bruce Allan808ff672008-08-08 18:35:56 -0700951 skb_copy_to_linear_data_offset(new_skb,
952 -NET_IP_ALIGN,
953 (skb->data -
954 NET_IP_ALIGN),
955 (length +
956 NET_IP_ALIGN));
Auke Kokbc7f75f2007-09-17 12:30:59 -0700957 /* save the skb in buffer_info as good */
958 buffer_info->skb = skb;
959 skb = new_skb;
960 }
961 /* else just continue with the old one */
962 }
963 /* end copybreak code */
964 skb_put(skb, length);
965
966 /* Receive Checksum Offload */
Bruce Allan5f450212011-07-22 06:21:46 +0000967 e1000_rx_checksum(adapter, staterr,
Bruce Allanafd12932012-01-05 00:34:05 +0000968 rx_desc->wb.lower.hi_dword.csum_ip.csum, skb);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700969
Bruce Allan70495a52012-01-11 01:26:50 +0000970 e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
971
Bruce Allan5f450212011-07-22 06:21:46 +0000972 e1000_receive_skb(adapter, netdev, skb, staterr,
973 rx_desc->wb.upper.vlan);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700974
975next_desc:
Bruce Allan5f450212011-07-22 06:21:46 +0000976 rx_desc->wb.upper.status_error &= cpu_to_le32(~0xFF);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700977
978 /* return some buffers to hardware, one at a time is too slow */
979 if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
Bruce Allan55aa6982011-12-16 00:45:45 +0000980 adapter->alloc_rx_buf(rx_ring, cleaned_count,
Jeff Kirsherc2fed992011-07-12 16:10:12 +0000981 GFP_ATOMIC);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700982 cleaned_count = 0;
983 }
984
985 /* use prefetched values */
986 rx_desc = next_rxd;
987 buffer_info = next_buffer;
Bruce Allan5f450212011-07-22 06:21:46 +0000988
989 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700990 }
991 rx_ring->next_to_clean = i;
992
993 cleaned_count = e1000_desc_unused(rx_ring);
994 if (cleaned_count)
Bruce Allan55aa6982011-12-16 00:45:45 +0000995 adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
Auke Kokbc7f75f2007-09-17 12:30:59 -0700996
Auke Kokbc7f75f2007-09-17 12:30:59 -0700997 adapter->total_rx_bytes += total_rx_bytes;
Bruce Allan7c257692008-04-23 11:09:00 -0700998 adapter->total_rx_packets += total_rx_packets;
Auke Kokbc7f75f2007-09-17 12:30:59 -0700999 return cleaned;
1000}
1001
Bruce Allan55aa6982011-12-16 00:45:45 +00001002static void e1000_put_txbuf(struct e1000_ring *tx_ring,
1003 struct e1000_buffer *buffer_info)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001004{
Bruce Allan55aa6982011-12-16 00:45:45 +00001005 struct e1000_adapter *adapter = tx_ring->adapter;
1006
Alexander Duyck03b13202009-12-02 16:45:31 +00001007 if (buffer_info->dma) {
1008 if (buffer_info->mapped_as_page)
Nick Nunley0be3f552010-04-27 13:09:05 +00001009 dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
1010 buffer_info->length, DMA_TO_DEVICE);
Alexander Duyck03b13202009-12-02 16:45:31 +00001011 else
Nick Nunley0be3f552010-04-27 13:09:05 +00001012 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1013 buffer_info->length, DMA_TO_DEVICE);
Alexander Duyck03b13202009-12-02 16:45:31 +00001014 buffer_info->dma = 0;
1015 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001016 if (buffer_info->skb) {
1017 dev_kfree_skb_any(buffer_info->skb);
1018 buffer_info->skb = NULL;
1019 }
Alexander Duyck1b7719c2009-03-19 01:12:50 +00001020 buffer_info->time_stamp = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001021}
1022
Bruce Allan41cec6f2009-11-20 23:28:56 +00001023static void e1000_print_hw_hang(struct work_struct *work)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001024{
Bruce Allan41cec6f2009-11-20 23:28:56 +00001025 struct e1000_adapter *adapter = container_of(work,
1026 struct e1000_adapter,
1027 print_hang_task);
Jeff Kirsher09357b02011-11-18 14:25:00 +00001028 struct net_device *netdev = adapter->netdev;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001029 struct e1000_ring *tx_ring = adapter->tx_ring;
1030 unsigned int i = tx_ring->next_to_clean;
1031 unsigned int eop = tx_ring->buffer_info[i].next_to_watch;
1032 struct e1000_tx_desc *eop_desc = E1000_TX_DESC(*tx_ring, eop);
Bruce Allan41cec6f2009-11-20 23:28:56 +00001033 struct e1000_hw *hw = &adapter->hw;
1034 u16 phy_status, phy_1000t_status, phy_ext_status;
1035 u16 pci_status;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001036
Jesse Brandeburg615b32a2011-02-02 10:19:45 +00001037 if (test_bit(__E1000_DOWN, &adapter->state))
1038 return;
1039
Jeff Kirsher09357b02011-11-18 14:25:00 +00001040 if (!adapter->tx_hang_recheck &&
1041 (adapter->flags2 & FLAG2_DMA_BURST)) {
1042 /* May be block on write-back, flush and detect again
1043 * flush pending descriptor writebacks to memory
1044 */
1045 ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
1046 /* execute the writes immediately */
1047 e1e_flush();
1048 adapter->tx_hang_recheck = true;
1049 return;
1050 }
1051 /* Real hang detected */
1052 adapter->tx_hang_recheck = false;
1053 netif_stop_queue(netdev);
1054
Bruce Allan41cec6f2009-11-20 23:28:56 +00001055 e1e_rphy(hw, PHY_STATUS, &phy_status);
1056 e1e_rphy(hw, PHY_1000T_STATUS, &phy_1000t_status);
1057 e1e_rphy(hw, PHY_EXT_STATUS, &phy_ext_status);
1058
1059 pci_read_config_word(adapter->pdev, PCI_STATUS, &pci_status);
1060
1061 /* detected Hardware unit hang */
1062 e_err("Detected Hardware Unit Hang:\n"
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001063 " TDH <%x>\n"
1064 " TDT <%x>\n"
1065 " next_to_use <%x>\n"
1066 " next_to_clean <%x>\n"
1067 "buffer_info[next_to_clean]:\n"
1068 " time_stamp <%lx>\n"
1069 " next_to_watch <%x>\n"
1070 " jiffies <%lx>\n"
Bruce Allan41cec6f2009-11-20 23:28:56 +00001071 " next_to_watch.status <%x>\n"
1072 "MAC Status <%x>\n"
1073 "PHY Status <%x>\n"
1074 "PHY 1000BASE-T Status <%x>\n"
1075 "PHY Extended Status <%x>\n"
1076 "PCI Status <%x>\n",
Bruce Allanc5083cf2011-12-16 00:45:40 +00001077 readl(tx_ring->head),
1078 readl(tx_ring->tail),
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001079 tx_ring->next_to_use,
1080 tx_ring->next_to_clean,
1081 tx_ring->buffer_info[eop].time_stamp,
1082 eop,
1083 jiffies,
Bruce Allan41cec6f2009-11-20 23:28:56 +00001084 eop_desc->upper.fields.status,
1085 er32(STATUS),
1086 phy_status,
1087 phy_1000t_status,
1088 phy_ext_status,
1089 pci_status);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001090}
1091
1092/**
1093 * e1000_clean_tx_irq - Reclaim resources after transmit completes
Bruce Allan55aa6982011-12-16 00:45:45 +00001094 * @tx_ring: Tx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07001095 *
1096 * the return value indicates whether actual cleaning was done, there
1097 * is no guarantee that everything was cleaned
1098 **/
Bruce Allan55aa6982011-12-16 00:45:45 +00001099static bool e1000_clean_tx_irq(struct e1000_ring *tx_ring)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001100{
Bruce Allan55aa6982011-12-16 00:45:45 +00001101 struct e1000_adapter *adapter = tx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001102 struct net_device *netdev = adapter->netdev;
1103 struct e1000_hw *hw = &adapter->hw;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001104 struct e1000_tx_desc *tx_desc, *eop_desc;
1105 struct e1000_buffer *buffer_info;
1106 unsigned int i, eop;
1107 unsigned int count = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001108 unsigned int total_tx_bytes = 0, total_tx_packets = 0;
Tom Herbert3f0cfa32011-11-28 16:33:16 +00001109 unsigned int bytes_compl = 0, pkts_compl = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001110
1111 i = tx_ring->next_to_clean;
1112 eop = tx_ring->buffer_info[i].next_to_watch;
1113 eop_desc = E1000_TX_DESC(*tx_ring, eop);
1114
Alexander Duyck12d04a32009-03-25 22:05:03 +00001115 while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
1116 (count < tx_ring->count)) {
Jesse Brandeburga86043c2009-04-16 16:59:28 +00001117 bool cleaned = false;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00001118 rmb(); /* read buffer_info after eop_desc */
Jesse Brandeburga86043c2009-04-16 16:59:28 +00001119 for (; !cleaned; count++) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001120 tx_desc = E1000_TX_DESC(*tx_ring, i);
1121 buffer_info = &tx_ring->buffer_info[i];
1122 cleaned = (i == eop);
1123
1124 if (cleaned) {
Tom Herbert9ed318d2010-05-05 14:02:27 +00001125 total_tx_packets += buffer_info->segs;
1126 total_tx_bytes += buffer_info->bytecount;
Tom Herbert3f0cfa32011-11-28 16:33:16 +00001127 if (buffer_info->skb) {
1128 bytes_compl += buffer_info->skb->len;
1129 pkts_compl++;
1130 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07001131 }
1132
Bruce Allan55aa6982011-12-16 00:45:45 +00001133 e1000_put_txbuf(tx_ring, buffer_info);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001134 tx_desc->upper.data = 0;
1135
1136 i++;
1137 if (i == tx_ring->count)
1138 i = 0;
1139 }
1140
Terry Loftindac87612010-04-09 10:29:49 +00001141 if (i == tx_ring->next_to_use)
1142 break;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001143 eop = tx_ring->buffer_info[i].next_to_watch;
1144 eop_desc = E1000_TX_DESC(*tx_ring, eop);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001145 }
1146
1147 tx_ring->next_to_clean = i;
1148
Tom Herbert3f0cfa32011-11-28 16:33:16 +00001149 netdev_completed_queue(netdev, pkts_compl, bytes_compl);
1150
Auke Kokbc7f75f2007-09-17 12:30:59 -07001151#define TX_WAKE_THRESHOLD 32
Jesse Brandeburga86043c2009-04-16 16:59:28 +00001152 if (count && netif_carrier_ok(netdev) &&
1153 e1000_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001154 /* Make sure that anybody stopping the queue after this
1155 * sees the new next_to_clean.
1156 */
1157 smp_mb();
1158
1159 if (netif_queue_stopped(netdev) &&
1160 !(test_bit(__E1000_DOWN, &adapter->state))) {
1161 netif_wake_queue(netdev);
1162 ++adapter->restart_queue;
1163 }
1164 }
1165
1166 if (adapter->detect_tx_hung) {
Bruce Allan41cec6f2009-11-20 23:28:56 +00001167 /*
1168 * Detect a transmit hang in hardware, this serializes the
1169 * check with the clearing of time_stamp and movement of i
1170 */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001171 adapter->detect_tx_hung = false;
Alexander Duyck12d04a32009-03-25 22:05:03 +00001172 if (tx_ring->buffer_info[i].time_stamp &&
1173 time_after(jiffies, tx_ring->buffer_info[i].time_stamp
Joe Perches8e95a202009-12-03 07:58:21 +00001174 + (adapter->tx_timeout_factor * HZ)) &&
Jeff Kirsher09357b02011-11-18 14:25:00 +00001175 !(er32(STATUS) & E1000_STATUS_TXOFF))
Bruce Allan41cec6f2009-11-20 23:28:56 +00001176 schedule_work(&adapter->print_hang_task);
Jeff Kirsher09357b02011-11-18 14:25:00 +00001177 else
1178 adapter->tx_hang_recheck = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001179 }
1180 adapter->total_tx_bytes += total_tx_bytes;
1181 adapter->total_tx_packets += total_tx_packets;
Eric Dumazet807540b2010-09-23 05:40:09 +00001182 return count < tx_ring->count;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001183}
1184
1185/**
Auke Kokbc7f75f2007-09-17 12:30:59 -07001186 * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
Bruce Allan55aa6982011-12-16 00:45:45 +00001187 * @rx_ring: Rx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07001188 *
1189 * the return value indicates whether actual cleaning was done, there
1190 * is no guarantee that everything was cleaned
1191 **/
Bruce Allan55aa6982011-12-16 00:45:45 +00001192static bool e1000_clean_rx_irq_ps(struct e1000_ring *rx_ring, int *work_done,
1193 int work_to_do)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001194{
Bruce Allan55aa6982011-12-16 00:45:45 +00001195 struct e1000_adapter *adapter = rx_ring->adapter;
Bruce Allan3bb99fe2009-11-20 23:25:07 +00001196 struct e1000_hw *hw = &adapter->hw;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001197 union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
1198 struct net_device *netdev = adapter->netdev;
1199 struct pci_dev *pdev = adapter->pdev;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001200 struct e1000_buffer *buffer_info, *next_buffer;
1201 struct e1000_ps_page *ps_page;
1202 struct sk_buff *skb;
1203 unsigned int i, j;
1204 u32 length, staterr;
1205 int cleaned_count = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001206 bool cleaned = false;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001207 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1208
1209 i = rx_ring->next_to_clean;
1210 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
1211 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
1212 buffer_info = &rx_ring->buffer_info[i];
1213
1214 while (staterr & E1000_RXD_STAT_DD) {
1215 if (*work_done >= work_to_do)
1216 break;
1217 (*work_done)++;
1218 skb = buffer_info->skb;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00001219 rmb(); /* read descriptor and rx_buffer_info after status DD */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001220
1221 /* in the packet split case this is header only */
1222 prefetch(skb->data - NET_IP_ALIGN);
1223
1224 i++;
1225 if (i == rx_ring->count)
1226 i = 0;
1227 next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
1228 prefetch(next_rxd);
1229
1230 next_buffer = &rx_ring->buffer_info[i];
1231
Rusty Russell3db1cd52011-12-19 13:56:45 +00001232 cleaned = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001233 cleaned_count++;
Nick Nunley0be3f552010-04-27 13:09:05 +00001234 dma_unmap_single(&pdev->dev, buffer_info->dma,
Bruce Allanaf667a22010-12-31 06:10:01 +00001235 adapter->rx_ps_bsize0, DMA_FROM_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001236 buffer_info->dma = 0;
1237
Bruce Allanaf667a22010-12-31 06:10:01 +00001238 /* see !EOP comment in other Rx routine */
Jesse Brandeburgb94b5022010-01-19 14:15:59 +00001239 if (!(staterr & E1000_RXD_STAT_EOP))
1240 adapter->flags2 |= FLAG2_IS_DISCARDING;
1241
1242 if (adapter->flags2 & FLAG2_IS_DISCARDING) {
Jeff Kirsheref456f82011-11-03 11:40:28 +00001243 e_dbg("Packet Split buffers didn't pick up the full packet\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001244 dev_kfree_skb_irq(skb);
Jesse Brandeburgb94b5022010-01-19 14:15:59 +00001245 if (staterr & E1000_RXD_STAT_EOP)
1246 adapter->flags2 &= ~FLAG2_IS_DISCARDING;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001247 goto next_desc;
1248 }
1249
1250 if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
1251 dev_kfree_skb_irq(skb);
1252 goto next_desc;
1253 }
1254
1255 length = le16_to_cpu(rx_desc->wb.middle.length0);
1256
1257 if (!length) {
Jeff Kirsheref456f82011-11-03 11:40:28 +00001258 e_dbg("Last part of the packet spanning multiple descriptors\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07001259 dev_kfree_skb_irq(skb);
1260 goto next_desc;
1261 }
1262
1263 /* Good Receive */
1264 skb_put(skb, length);
1265
1266 {
Bruce Allan0e15df42012-01-31 06:37:11 +00001267 /*
1268 * this looks ugly, but it seems compiler issues make
1269 * it more efficient than reusing j
1270 */
1271 int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001272
Bruce Allanad680762008-03-28 09:15:03 -07001273 /*
Bruce Allan0e15df42012-01-31 06:37:11 +00001274 * page alloc/put takes too long and effects small
1275 * packet throughput, so unsplit small packets and
1276 * save the alloc/put only valid in softirq (napi)
1277 * context to call kmap_*
Bruce Allanad680762008-03-28 09:15:03 -07001278 */
Bruce Allan0e15df42012-01-31 06:37:11 +00001279 if (l1 && (l1 <= copybreak) &&
1280 ((length + l1) <= adapter->rx_ps_bsize0)) {
1281 u8 *vaddr;
Auke Kok140a7482007-10-25 13:57:58 -07001282
Bruce Allan0e15df42012-01-31 06:37:11 +00001283 ps_page = &buffer_info->ps_pages[0];
Jeff Kirshereb7c3ad2008-11-14 06:45:23 +00001284
Bruce Allan0e15df42012-01-31 06:37:11 +00001285 /*
1286 * there is no documentation about how to call
1287 * kmap_atomic, so we can't hold the mapping
1288 * very long
1289 */
1290 dma_sync_single_for_cpu(&pdev->dev,
1291 ps_page->dma,
1292 PAGE_SIZE,
1293 DMA_FROM_DEVICE);
1294 vaddr = kmap_atomic(ps_page->page,
1295 KM_SKB_DATA_SOFTIRQ);
1296 memcpy(skb_tail_pointer(skb), vaddr, l1);
1297 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
1298 dma_sync_single_for_device(&pdev->dev,
1299 ps_page->dma,
1300 PAGE_SIZE,
1301 DMA_FROM_DEVICE);
1302
1303 /* remove the CRC */
1304 if (!(adapter->flags2 & FLAG2_CRC_STRIPPING))
1305 l1 -= 4;
1306
1307 skb_put(skb, l1);
1308 goto copydone;
1309 } /* if */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001310 }
1311
1312 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
1313 length = le16_to_cpu(rx_desc->wb.upper.length[j]);
1314 if (!length)
1315 break;
1316
Auke Kok47f44e42007-10-25 13:57:44 -07001317 ps_page = &buffer_info->ps_pages[j];
Nick Nunley0be3f552010-04-27 13:09:05 +00001318 dma_unmap_page(&pdev->dev, ps_page->dma, PAGE_SIZE,
1319 DMA_FROM_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001320 ps_page->dma = 0;
1321 skb_fill_page_desc(skb, j, ps_page->page, 0, length);
1322 ps_page->page = NULL;
1323 skb->len += length;
1324 skb->data_len += length;
Eric Dumazet98a045d2011-10-13 08:03:36 +00001325 skb->truesize += PAGE_SIZE;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001326 }
1327
Jeff Kirshereb7c3ad2008-11-14 06:45:23 +00001328 /* strip the ethernet crc, problem is we're using pages now so
1329 * this whole operation can get a little cpu intensive
1330 */
1331 if (!(adapter->flags2 & FLAG2_CRC_STRIPPING))
1332 pskb_trim(skb, skb->len - 4);
1333
Auke Kokbc7f75f2007-09-17 12:30:59 -07001334copydone:
1335 total_rx_bytes += skb->len;
1336 total_rx_packets++;
1337
Bruce Allanafd12932012-01-05 00:34:05 +00001338 e1000_rx_checksum(adapter, staterr,
1339 rx_desc->wb.lower.hi_dword.csum_ip.csum, skb);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001340
Bruce Allan70495a52012-01-11 01:26:50 +00001341 e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
1342
Auke Kokbc7f75f2007-09-17 12:30:59 -07001343 if (rx_desc->wb.upper.header_status &
1344 cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP))
1345 adapter->rx_hdr_split++;
1346
1347 e1000_receive_skb(adapter, netdev, skb,
1348 staterr, rx_desc->wb.middle.vlan);
1349
1350next_desc:
1351 rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
1352 buffer_info->skb = NULL;
1353
1354 /* return some buffers to hardware, one at a time is too slow */
1355 if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
Bruce Allan55aa6982011-12-16 00:45:45 +00001356 adapter->alloc_rx_buf(rx_ring, cleaned_count,
Jeff Kirsherc2fed992011-07-12 16:10:12 +00001357 GFP_ATOMIC);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001358 cleaned_count = 0;
1359 }
1360
1361 /* use prefetched values */
1362 rx_desc = next_rxd;
1363 buffer_info = next_buffer;
1364
1365 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
1366 }
1367 rx_ring->next_to_clean = i;
1368
1369 cleaned_count = e1000_desc_unused(rx_ring);
1370 if (cleaned_count)
Bruce Allan55aa6982011-12-16 00:45:45 +00001371 adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001372
Auke Kokbc7f75f2007-09-17 12:30:59 -07001373 adapter->total_rx_bytes += total_rx_bytes;
Bruce Allan7c257692008-04-23 11:09:00 -07001374 adapter->total_rx_packets += total_rx_packets;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001375 return cleaned;
1376}
1377
1378/**
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001379 * e1000_consume_page - helper function
1380 **/
1381static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
1382 u16 length)
1383{
1384 bi->page = NULL;
1385 skb->len += length;
1386 skb->data_len += length;
Eric Dumazet98a045d2011-10-13 08:03:36 +00001387 skb->truesize += PAGE_SIZE;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001388}
1389
1390/**
1391 * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
1392 * @adapter: board private structure
1393 *
1394 * the return value indicates whether actual cleaning was done, there
1395 * is no guarantee that everything was cleaned
1396 **/
Bruce Allan55aa6982011-12-16 00:45:45 +00001397static bool e1000_clean_jumbo_rx_irq(struct e1000_ring *rx_ring, int *work_done,
1398 int work_to_do)
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001399{
Bruce Allan55aa6982011-12-16 00:45:45 +00001400 struct e1000_adapter *adapter = rx_ring->adapter;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001401 struct net_device *netdev = adapter->netdev;
1402 struct pci_dev *pdev = adapter->pdev;
Bruce Allan5f450212011-07-22 06:21:46 +00001403 union e1000_rx_desc_extended *rx_desc, *next_rxd;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001404 struct e1000_buffer *buffer_info, *next_buffer;
Bruce Allan5f450212011-07-22 06:21:46 +00001405 u32 length, staterr;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001406 unsigned int i;
1407 int cleaned_count = 0;
1408 bool cleaned = false;
1409 unsigned int total_rx_bytes=0, total_rx_packets=0;
1410
1411 i = rx_ring->next_to_clean;
Bruce Allan5f450212011-07-22 06:21:46 +00001412 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
1413 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001414 buffer_info = &rx_ring->buffer_info[i];
1415
Bruce Allan5f450212011-07-22 06:21:46 +00001416 while (staterr & E1000_RXD_STAT_DD) {
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001417 struct sk_buff *skb;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001418
1419 if (*work_done >= work_to_do)
1420 break;
1421 (*work_done)++;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +00001422 rmb(); /* read descriptor and rx_buffer_info after status DD */
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001423
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001424 skb = buffer_info->skb;
1425 buffer_info->skb = NULL;
1426
1427 ++i;
1428 if (i == rx_ring->count)
1429 i = 0;
Bruce Allan5f450212011-07-22 06:21:46 +00001430 next_rxd = E1000_RX_DESC_EXT(*rx_ring, i);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001431 prefetch(next_rxd);
1432
1433 next_buffer = &rx_ring->buffer_info[i];
1434
1435 cleaned = true;
1436 cleaned_count++;
Nick Nunley0be3f552010-04-27 13:09:05 +00001437 dma_unmap_page(&pdev->dev, buffer_info->dma, PAGE_SIZE,
1438 DMA_FROM_DEVICE);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001439 buffer_info->dma = 0;
1440
Bruce Allan5f450212011-07-22 06:21:46 +00001441 length = le16_to_cpu(rx_desc->wb.upper.length);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001442
1443 /* errors is only valid for DD + EOP descriptors */
Bruce Allan5f450212011-07-22 06:21:46 +00001444 if (unlikely((staterr & E1000_RXD_STAT_EOP) &&
1445 (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK))) {
1446 /* recycle both page and skb */
1447 buffer_info->skb = skb;
1448 /* an error means any chain goes out the window too */
1449 if (rx_ring->rx_skb_top)
1450 dev_kfree_skb_irq(rx_ring->rx_skb_top);
1451 rx_ring->rx_skb_top = NULL;
1452 goto next_desc;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001453 }
1454
Bruce Allanf0f1a172010-12-11 05:53:32 +00001455#define rxtop (rx_ring->rx_skb_top)
Bruce Allan5f450212011-07-22 06:21:46 +00001456 if (!(staterr & E1000_RXD_STAT_EOP)) {
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001457 /* this descriptor is only the beginning (or middle) */
1458 if (!rxtop) {
1459 /* this is the beginning of a chain */
1460 rxtop = skb;
1461 skb_fill_page_desc(rxtop, 0, buffer_info->page,
1462 0, length);
1463 } else {
1464 /* this is the middle of a chain */
1465 skb_fill_page_desc(rxtop,
1466 skb_shinfo(rxtop)->nr_frags,
1467 buffer_info->page, 0, length);
1468 /* re-use the skb, only consumed the page */
1469 buffer_info->skb = skb;
1470 }
1471 e1000_consume_page(buffer_info, rxtop, length);
1472 goto next_desc;
1473 } else {
1474 if (rxtop) {
1475 /* end of the chain */
1476 skb_fill_page_desc(rxtop,
1477 skb_shinfo(rxtop)->nr_frags,
1478 buffer_info->page, 0, length);
1479 /* re-use the current skb, we only consumed the
1480 * page */
1481 buffer_info->skb = skb;
1482 skb = rxtop;
1483 rxtop = NULL;
1484 e1000_consume_page(buffer_info, skb, length);
1485 } else {
1486 /* no chain, got EOP, this buf is the packet
1487 * copybreak to save the put_page/alloc_page */
1488 if (length <= copybreak &&
1489 skb_tailroom(skb) >= length) {
1490 u8 *vaddr;
1491 vaddr = kmap_atomic(buffer_info->page,
1492 KM_SKB_DATA_SOFTIRQ);
1493 memcpy(skb_tail_pointer(skb), vaddr,
1494 length);
1495 kunmap_atomic(vaddr,
1496 KM_SKB_DATA_SOFTIRQ);
1497 /* re-use the page, so don't erase
1498 * buffer_info->page */
1499 skb_put(skb, length);
1500 } else {
1501 skb_fill_page_desc(skb, 0,
1502 buffer_info->page, 0,
1503 length);
1504 e1000_consume_page(buffer_info, skb,
1505 length);
1506 }
1507 }
1508 }
1509
1510 /* Receive Checksum Offload XXX recompute due to CRC strip? */
Bruce Allan5f450212011-07-22 06:21:46 +00001511 e1000_rx_checksum(adapter, staterr,
Bruce Allanafd12932012-01-05 00:34:05 +00001512 rx_desc->wb.lower.hi_dword.csum_ip.csum, skb);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001513
Bruce Allan70495a52012-01-11 01:26:50 +00001514 e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
1515
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001516 /* probably a little skewed due to removing CRC */
1517 total_rx_bytes += skb->len;
1518 total_rx_packets++;
1519
1520 /* eth type trans needs skb->data to point to something */
1521 if (!pskb_may_pull(skb, ETH_HLEN)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -07001522 e_err("pskb_may_pull failed.\n");
Bruce Allanef5ab892011-02-10 08:17:21 +00001523 dev_kfree_skb_irq(skb);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001524 goto next_desc;
1525 }
1526
Bruce Allan5f450212011-07-22 06:21:46 +00001527 e1000_receive_skb(adapter, netdev, skb, staterr,
1528 rx_desc->wb.upper.vlan);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001529
1530next_desc:
Bruce Allan5f450212011-07-22 06:21:46 +00001531 rx_desc->wb.upper.status_error &= cpu_to_le32(~0xFF);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001532
1533 /* return some buffers to hardware, one at a time is too slow */
1534 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
Bruce Allan55aa6982011-12-16 00:45:45 +00001535 adapter->alloc_rx_buf(rx_ring, cleaned_count,
Jeff Kirsherc2fed992011-07-12 16:10:12 +00001536 GFP_ATOMIC);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001537 cleaned_count = 0;
1538 }
1539
1540 /* use prefetched values */
1541 rx_desc = next_rxd;
1542 buffer_info = next_buffer;
Bruce Allan5f450212011-07-22 06:21:46 +00001543
1544 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001545 }
1546 rx_ring->next_to_clean = i;
1547
1548 cleaned_count = e1000_desc_unused(rx_ring);
1549 if (cleaned_count)
Bruce Allan55aa6982011-12-16 00:45:45 +00001550 adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001551
1552 adapter->total_rx_bytes += total_rx_bytes;
1553 adapter->total_rx_packets += total_rx_packets;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001554 return cleaned;
1555}
1556
1557/**
Auke Kokbc7f75f2007-09-17 12:30:59 -07001558 * e1000_clean_rx_ring - Free Rx Buffers per Queue
Bruce Allan55aa6982011-12-16 00:45:45 +00001559 * @rx_ring: Rx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07001560 **/
Bruce Allan55aa6982011-12-16 00:45:45 +00001561static void e1000_clean_rx_ring(struct e1000_ring *rx_ring)
Auke Kokbc7f75f2007-09-17 12:30:59 -07001562{
Bruce Allan55aa6982011-12-16 00:45:45 +00001563 struct e1000_adapter *adapter = rx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001564 struct e1000_buffer *buffer_info;
1565 struct e1000_ps_page *ps_page;
1566 struct pci_dev *pdev = adapter->pdev;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001567 unsigned int i, j;
1568
1569 /* Free all the Rx ring sk_buffs */
1570 for (i = 0; i < rx_ring->count; i++) {
1571 buffer_info = &rx_ring->buffer_info[i];
1572 if (buffer_info->dma) {
1573 if (adapter->clean_rx == e1000_clean_rx_irq)
Nick Nunley0be3f552010-04-27 13:09:05 +00001574 dma_unmap_single(&pdev->dev, buffer_info->dma,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001575 adapter->rx_buffer_len,
Nick Nunley0be3f552010-04-27 13:09:05 +00001576 DMA_FROM_DEVICE);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001577 else if (adapter->clean_rx == e1000_clean_jumbo_rx_irq)
Nick Nunley0be3f552010-04-27 13:09:05 +00001578 dma_unmap_page(&pdev->dev, buffer_info->dma,
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001579 PAGE_SIZE,
Nick Nunley0be3f552010-04-27 13:09:05 +00001580 DMA_FROM_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001581 else if (adapter->clean_rx == e1000_clean_rx_irq_ps)
Nick Nunley0be3f552010-04-27 13:09:05 +00001582 dma_unmap_single(&pdev->dev, buffer_info->dma,
Auke Kokbc7f75f2007-09-17 12:30:59 -07001583 adapter->rx_ps_bsize0,
Nick Nunley0be3f552010-04-27 13:09:05 +00001584 DMA_FROM_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001585 buffer_info->dma = 0;
1586 }
1587
Bruce Allan97ac8ca2008-04-29 09:16:05 -07001588 if (buffer_info->page) {
1589 put_page(buffer_info->page);
1590 buffer_info->page = NULL;
1591 }
1592
Auke Kokbc7f75f2007-09-17 12:30:59 -07001593 if (buffer_info->skb) {
1594 dev_kfree_skb(buffer_info->skb);
1595 buffer_info->skb = NULL;
1596 }
1597
1598 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
Auke Kok47f44e42007-10-25 13:57:44 -07001599 ps_page = &buffer_info->ps_pages[j];
Auke Kokbc7f75f2007-09-17 12:30:59 -07001600 if (!ps_page->page)
1601 break;
Nick Nunley0be3f552010-04-27 13:09:05 +00001602 dma_unmap_page(&pdev->dev, ps_page->dma, PAGE_SIZE,
1603 DMA_FROM_DEVICE);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001604 ps_page->dma = 0;
1605 put_page(ps_page->page);
1606 ps_page->page = NULL;
1607 }
1608 }
1609
1610 /* there also may be some cached data from a chained receive */
1611 if (rx_ring->rx_skb_top) {
1612 dev_kfree_skb(rx_ring->rx_skb_top);
1613 rx_ring->rx_skb_top = NULL;
1614 }
1615
Auke Kokbc7f75f2007-09-17 12:30:59 -07001616 /* Zero out the descriptor ring */
1617 memset(rx_ring->desc, 0, rx_ring->size);
1618
1619 rx_ring->next_to_clean = 0;
1620 rx_ring->next_to_use = 0;
Jesse Brandeburgb94b5022010-01-19 14:15:59 +00001621 adapter->flags2 &= ~FLAG2_IS_DISCARDING;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001622
Bruce Allanc5083cf2011-12-16 00:45:40 +00001623 writel(0, rx_ring->head);
1624 writel(0, rx_ring->tail);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001625}
1626
Jesse Brandeburga8f88ff2008-10-02 16:33:25 -07001627static void e1000e_downshift_workaround(struct work_struct *work)
1628{
1629 struct e1000_adapter *adapter = container_of(work,
1630 struct e1000_adapter, downshift_task);
1631
Jesse Brandeburg615b32a2011-02-02 10:19:45 +00001632 if (test_bit(__E1000_DOWN, &adapter->state))
1633 return;
1634
Jesse Brandeburga8f88ff2008-10-02 16:33:25 -07001635 e1000e_gig_downshift_workaround_ich8lan(&adapter->hw);
1636}
1637
Auke Kokbc7f75f2007-09-17 12:30:59 -07001638/**
1639 * e1000_intr_msi - Interrupt Handler
1640 * @irq: interrupt number
1641 * @data: pointer to a network interface device structure
1642 **/
1643static irqreturn_t e1000_intr_msi(int irq, void *data)
1644{
1645 struct net_device *netdev = data;
1646 struct e1000_adapter *adapter = netdev_priv(netdev);
1647 struct e1000_hw *hw = &adapter->hw;
1648 u32 icr = er32(ICR);
1649
Bruce Allanad680762008-03-28 09:15:03 -07001650 /*
1651 * read ICR disables interrupts using IAM
1652 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001653
dave graham573cca82009-02-10 12:52:05 +00001654 if (icr & E1000_ICR_LSC) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001655 hw->mac.get_link_status = 1;
Bruce Allanad680762008-03-28 09:15:03 -07001656 /*
1657 * ICH8 workaround-- Call gig speed drop workaround on cable
1658 * disconnect (LSC) before accessing any PHY registers
1659 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001660 if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
1661 (!(er32(STATUS) & E1000_STATUS_LU)))
Jesse Brandeburga8f88ff2008-10-02 16:33:25 -07001662 schedule_work(&adapter->downshift_task);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001663
Bruce Allanad680762008-03-28 09:15:03 -07001664 /*
1665 * 80003ES2LAN workaround-- For packet buffer work-around on
Auke Kokbc7f75f2007-09-17 12:30:59 -07001666 * link down event; disable receives here in the ISR and reset
Bruce Allanad680762008-03-28 09:15:03 -07001667 * adapter in watchdog
1668 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001669 if (netif_carrier_ok(netdev) &&
1670 adapter->flags & FLAG_RX_NEEDS_RESTART) {
1671 /* disable receives */
1672 u32 rctl = er32(RCTL);
1673 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001674 adapter->flags |= FLAG_RX_RESTART_NOW;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001675 }
1676 /* guard against interrupt when we're going down */
1677 if (!test_bit(__E1000_DOWN, &adapter->state))
1678 mod_timer(&adapter->watchdog_timer, jiffies + 1);
1679 }
1680
Ben Hutchings288379f2009-01-19 16:43:59 -08001681 if (napi_schedule_prep(&adapter->napi)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001682 adapter->total_tx_bytes = 0;
1683 adapter->total_tx_packets = 0;
1684 adapter->total_rx_bytes = 0;
1685 adapter->total_rx_packets = 0;
Ben Hutchings288379f2009-01-19 16:43:59 -08001686 __napi_schedule(&adapter->napi);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001687 }
1688
1689 return IRQ_HANDLED;
1690}
1691
1692/**
1693 * e1000_intr - Interrupt Handler
1694 * @irq: interrupt number
1695 * @data: pointer to a network interface device structure
1696 **/
1697static irqreturn_t e1000_intr(int irq, void *data)
1698{
1699 struct net_device *netdev = data;
1700 struct e1000_adapter *adapter = netdev_priv(netdev);
1701 struct e1000_hw *hw = &adapter->hw;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001702 u32 rctl, icr = er32(ICR);
Bruce Allan4662e822008-08-26 18:37:06 -07001703
Bruce Allana68ea772009-11-20 23:23:16 +00001704 if (!icr || test_bit(__E1000_DOWN, &adapter->state))
Auke Kokbc7f75f2007-09-17 12:30:59 -07001705 return IRQ_NONE; /* Not our interrupt */
1706
Bruce Allanad680762008-03-28 09:15:03 -07001707 /*
1708 * IMS will not auto-mask if INT_ASSERTED is not set, and if it is
1709 * not set, then the adapter didn't send an interrupt
1710 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001711 if (!(icr & E1000_ICR_INT_ASSERTED))
1712 return IRQ_NONE;
1713
Bruce Allanad680762008-03-28 09:15:03 -07001714 /*
1715 * Interrupt Auto-Mask...upon reading ICR,
1716 * interrupts are masked. No need for the
1717 * IMC write
1718 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001719
dave graham573cca82009-02-10 12:52:05 +00001720 if (icr & E1000_ICR_LSC) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001721 hw->mac.get_link_status = 1;
Bruce Allanad680762008-03-28 09:15:03 -07001722 /*
1723 * ICH8 workaround-- Call gig speed drop workaround on cable
1724 * disconnect (LSC) before accessing any PHY registers
1725 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07001726 if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
1727 (!(er32(STATUS) & E1000_STATUS_LU)))
Jesse Brandeburga8f88ff2008-10-02 16:33:25 -07001728 schedule_work(&adapter->downshift_task);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001729
Bruce Allanad680762008-03-28 09:15:03 -07001730 /*
1731 * 80003ES2LAN workaround--
Auke Kokbc7f75f2007-09-17 12:30:59 -07001732 * For packet buffer work-around on link down event;
1733 * disable receives here in the ISR and
1734 * reset adapter in watchdog
1735 */
1736 if (netif_carrier_ok(netdev) &&
1737 (adapter->flags & FLAG_RX_NEEDS_RESTART)) {
1738 /* disable receives */
1739 rctl = er32(RCTL);
1740 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Jeff Kirsher318a94d2008-03-28 09:15:16 -07001741 adapter->flags |= FLAG_RX_RESTART_NOW;
Auke Kokbc7f75f2007-09-17 12:30:59 -07001742 }
1743 /* guard against interrupt when we're going down */
1744 if (!test_bit(__E1000_DOWN, &adapter->state))
1745 mod_timer(&adapter->watchdog_timer, jiffies + 1);
1746 }
1747
Ben Hutchings288379f2009-01-19 16:43:59 -08001748 if (napi_schedule_prep(&adapter->napi)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07001749 adapter->total_tx_bytes = 0;
1750 adapter->total_tx_packets = 0;
1751 adapter->total_rx_bytes = 0;
1752 adapter->total_rx_packets = 0;
Ben Hutchings288379f2009-01-19 16:43:59 -08001753 __napi_schedule(&adapter->napi);
Auke Kokbc7f75f2007-09-17 12:30:59 -07001754 }
1755
1756 return IRQ_HANDLED;
1757}
1758
Bruce Allan4662e822008-08-26 18:37:06 -07001759static irqreturn_t e1000_msix_other(int irq, void *data)
1760{
1761 struct net_device *netdev = data;
1762 struct e1000_adapter *adapter = netdev_priv(netdev);
1763 struct e1000_hw *hw = &adapter->hw;
1764 u32 icr = er32(ICR);
1765
1766 if (!(icr & E1000_ICR_INT_ASSERTED)) {
Jesse Brandeburga3c69fe2009-03-25 22:05:41 +00001767 if (!test_bit(__E1000_DOWN, &adapter->state))
1768 ew32(IMS, E1000_IMS_OTHER);
Bruce Allan4662e822008-08-26 18:37:06 -07001769 return IRQ_NONE;
1770 }
1771
1772 if (icr & adapter->eiac_mask)
1773 ew32(ICS, (icr & adapter->eiac_mask));
1774
1775 if (icr & E1000_ICR_OTHER) {
1776 if (!(icr & E1000_ICR_LSC))
1777 goto no_link_interrupt;
1778 hw->mac.get_link_status = 1;
1779 /* guard against interrupt when we're going down */
1780 if (!test_bit(__E1000_DOWN, &adapter->state))
1781 mod_timer(&adapter->watchdog_timer, jiffies + 1);
1782 }
1783
1784no_link_interrupt:
Jesse Brandeburga3c69fe2009-03-25 22:05:41 +00001785 if (!test_bit(__E1000_DOWN, &adapter->state))
1786 ew32(IMS, E1000_IMS_LSC | E1000_IMS_OTHER);
Bruce Allan4662e822008-08-26 18:37:06 -07001787
1788 return IRQ_HANDLED;
1789}
1790
1791
1792static irqreturn_t e1000_intr_msix_tx(int irq, void *data)
1793{
1794 struct net_device *netdev = data;
1795 struct e1000_adapter *adapter = netdev_priv(netdev);
1796 struct e1000_hw *hw = &adapter->hw;
1797 struct e1000_ring *tx_ring = adapter->tx_ring;
1798
1799
1800 adapter->total_tx_bytes = 0;
1801 adapter->total_tx_packets = 0;
1802
Bruce Allan55aa6982011-12-16 00:45:45 +00001803 if (!e1000_clean_tx_irq(tx_ring))
Bruce Allan4662e822008-08-26 18:37:06 -07001804 /* Ring was not completely cleaned, so fire another interrupt */
1805 ew32(ICS, tx_ring->ims_val);
1806
1807 return IRQ_HANDLED;
1808}
1809
1810static irqreturn_t e1000_intr_msix_rx(int irq, void *data)
1811{
1812 struct net_device *netdev = data;
1813 struct e1000_adapter *adapter = netdev_priv(netdev);
Bruce Allan55aa6982011-12-16 00:45:45 +00001814 struct e1000_ring *rx_ring = adapter->rx_ring;
Bruce Allan4662e822008-08-26 18:37:06 -07001815
1816 /* Write the ITR value calculated at the end of the
1817 * previous interrupt.
1818 */
Bruce Allan55aa6982011-12-16 00:45:45 +00001819 if (rx_ring->set_itr) {
1820 writel(1000000000 / (rx_ring->itr_val * 256),
1821 rx_ring->itr_register);
1822 rx_ring->set_itr = 0;
Bruce Allan4662e822008-08-26 18:37:06 -07001823 }
1824
Ben Hutchings288379f2009-01-19 16:43:59 -08001825 if (napi_schedule_prep(&adapter->napi)) {
Bruce Allan4662e822008-08-26 18:37:06 -07001826 adapter->total_rx_bytes = 0;
1827 adapter->total_rx_packets = 0;
Ben Hutchings288379f2009-01-19 16:43:59 -08001828 __napi_schedule(&adapter->napi);
Bruce Allan4662e822008-08-26 18:37:06 -07001829 }
1830 return IRQ_HANDLED;
1831}
1832
1833/**
1834 * e1000_configure_msix - Configure MSI-X hardware
1835 *
1836 * e1000_configure_msix sets up the hardware to properly
1837 * generate MSI-X interrupts.
1838 **/
1839static void e1000_configure_msix(struct e1000_adapter *adapter)
1840{
1841 struct e1000_hw *hw = &adapter->hw;
1842 struct e1000_ring *rx_ring = adapter->rx_ring;
1843 struct e1000_ring *tx_ring = adapter->tx_ring;
1844 int vector = 0;
1845 u32 ctrl_ext, ivar = 0;
1846
1847 adapter->eiac_mask = 0;
1848
1849 /* Workaround issue with spurious interrupts on 82574 in MSI-X mode */
1850 if (hw->mac.type == e1000_82574) {
1851 u32 rfctl = er32(RFCTL);
1852 rfctl |= E1000_RFCTL_ACK_DIS;
1853 ew32(RFCTL, rfctl);
1854 }
1855
1856#define E1000_IVAR_INT_ALLOC_VALID 0x8
1857 /* Configure Rx vector */
1858 rx_ring->ims_val = E1000_IMS_RXQ0;
1859 adapter->eiac_mask |= rx_ring->ims_val;
1860 if (rx_ring->itr_val)
1861 writel(1000000000 / (rx_ring->itr_val * 256),
Bruce Allanc5083cf2011-12-16 00:45:40 +00001862 rx_ring->itr_register);
Bruce Allan4662e822008-08-26 18:37:06 -07001863 else
Bruce Allanc5083cf2011-12-16 00:45:40 +00001864 writel(1, rx_ring->itr_register);
Bruce Allan4662e822008-08-26 18:37:06 -07001865 ivar = E1000_IVAR_INT_ALLOC_VALID | vector;
1866
1867 /* Configure Tx vector */
1868 tx_ring->ims_val = E1000_IMS_TXQ0;
1869 vector++;
1870 if (tx_ring->itr_val)
1871 writel(1000000000 / (tx_ring->itr_val * 256),
Bruce Allanc5083cf2011-12-16 00:45:40 +00001872 tx_ring->itr_register);
Bruce Allan4662e822008-08-26 18:37:06 -07001873 else
Bruce Allanc5083cf2011-12-16 00:45:40 +00001874 writel(1, tx_ring->itr_register);
Bruce Allan4662e822008-08-26 18:37:06 -07001875 adapter->eiac_mask |= tx_ring->ims_val;
1876 ivar |= ((E1000_IVAR_INT_ALLOC_VALID | vector) << 8);
1877
1878 /* set vector for Other Causes, e.g. link changes */
1879 vector++;
1880 ivar |= ((E1000_IVAR_INT_ALLOC_VALID | vector) << 16);
1881 if (rx_ring->itr_val)
1882 writel(1000000000 / (rx_ring->itr_val * 256),
1883 hw->hw_addr + E1000_EITR_82574(vector));
1884 else
1885 writel(1, hw->hw_addr + E1000_EITR_82574(vector));
1886
1887 /* Cause Tx interrupts on every write back */
1888 ivar |= (1 << 31);
1889
1890 ew32(IVAR, ivar);
1891
1892 /* enable MSI-X PBA support */
1893 ctrl_ext = er32(CTRL_EXT);
1894 ctrl_ext |= E1000_CTRL_EXT_PBA_CLR;
1895
1896 /* Auto-Mask Other interrupts upon ICR read */
1897#define E1000_EIAC_MASK_82574 0x01F00000
1898 ew32(IAM, ~E1000_EIAC_MASK_82574 | E1000_IMS_OTHER);
1899 ctrl_ext |= E1000_CTRL_EXT_EIAME;
1900 ew32(CTRL_EXT, ctrl_ext);
1901 e1e_flush();
1902}
1903
1904void e1000e_reset_interrupt_capability(struct e1000_adapter *adapter)
1905{
1906 if (adapter->msix_entries) {
1907 pci_disable_msix(adapter->pdev);
1908 kfree(adapter->msix_entries);
1909 adapter->msix_entries = NULL;
1910 } else if (adapter->flags & FLAG_MSI_ENABLED) {
1911 pci_disable_msi(adapter->pdev);
1912 adapter->flags &= ~FLAG_MSI_ENABLED;
1913 }
Bruce Allan4662e822008-08-26 18:37:06 -07001914}
1915
1916/**
1917 * e1000e_set_interrupt_capability - set MSI or MSI-X if supported
1918 *
1919 * Attempt to configure interrupts using the best available
1920 * capabilities of the hardware and kernel.
1921 **/
1922void e1000e_set_interrupt_capability(struct e1000_adapter *adapter)
1923{
1924 int err;
Jeff Kirsher8e86acd2010-08-02 14:27:23 +00001925 int i;
Bruce Allan4662e822008-08-26 18:37:06 -07001926
1927 switch (adapter->int_mode) {
1928 case E1000E_INT_MODE_MSIX:
1929 if (adapter->flags & FLAG_HAS_MSIX) {
Jeff Kirsher8e86acd2010-08-02 14:27:23 +00001930 adapter->num_vectors = 3; /* RxQ0, TxQ0 and other */
1931 adapter->msix_entries = kcalloc(adapter->num_vectors,
Bruce Allan4662e822008-08-26 18:37:06 -07001932 sizeof(struct msix_entry),
1933 GFP_KERNEL);
1934 if (adapter->msix_entries) {
Jeff Kirsher8e86acd2010-08-02 14:27:23 +00001935 for (i = 0; i < adapter->num_vectors; i++)
Bruce Allan4662e822008-08-26 18:37:06 -07001936 adapter->msix_entries[i].entry = i;
1937
1938 err = pci_enable_msix(adapter->pdev,
1939 adapter->msix_entries,
Jeff Kirsher8e86acd2010-08-02 14:27:23 +00001940 adapter->num_vectors);
Bruce Allanb1cdfea2010-12-11 05:53:47 +00001941 if (err == 0)
Bruce Allan4662e822008-08-26 18:37:06 -07001942 return;
1943 }
1944 /* MSI-X failed, so fall through and try MSI */
Jeff Kirsheref456f82011-11-03 11:40:28 +00001945 e_err("Failed to initialize MSI-X interrupts. Falling back to MSI interrupts.\n");
Bruce Allan4662e822008-08-26 18:37:06 -07001946 e1000e_reset_interrupt_capability(adapter);
1947 }
1948 adapter->int_mode = E1000E_INT_MODE_MSI;
1949 /* Fall through */
1950 case E1000E_INT_MODE_MSI:
1951 if (!pci_enable_msi(adapter->pdev)) {
1952 adapter->flags |= FLAG_MSI_ENABLED;
1953 } else {
1954 adapter->int_mode = E1000E_INT_MODE_LEGACY;
Jeff Kirsheref456f82011-11-03 11:40:28 +00001955 e_err("Failed to initialize MSI interrupts. Falling back to legacy interrupts.\n");
Bruce Allan4662e822008-08-26 18:37:06 -07001956 }
1957 /* Fall through */
1958 case E1000E_INT_MODE_LEGACY:
1959 /* Don't do anything; this is the system default */
1960 break;
1961 }
Jeff Kirsher8e86acd2010-08-02 14:27:23 +00001962
1963 /* store the number of vectors being used */
1964 adapter->num_vectors = 1;
Bruce Allan4662e822008-08-26 18:37:06 -07001965}
1966
1967/**
1968 * e1000_request_msix - Initialize MSI-X interrupts
1969 *
1970 * e1000_request_msix allocates MSI-X vectors and requests interrupts from the
1971 * kernel.
1972 **/
1973static int e1000_request_msix(struct e1000_adapter *adapter)
1974{
1975 struct net_device *netdev = adapter->netdev;
1976 int err = 0, vector = 0;
1977
1978 if (strlen(netdev->name) < (IFNAMSIZ - 5))
Bruce Allan79f5e842011-01-19 04:20:59 +00001979 snprintf(adapter->rx_ring->name,
1980 sizeof(adapter->rx_ring->name) - 1,
1981 "%s-rx-0", netdev->name);
Bruce Allan4662e822008-08-26 18:37:06 -07001982 else
1983 memcpy(adapter->rx_ring->name, netdev->name, IFNAMSIZ);
1984 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -08001985 e1000_intr_msix_rx, 0, adapter->rx_ring->name,
Bruce Allan4662e822008-08-26 18:37:06 -07001986 netdev);
1987 if (err)
1988 goto out;
Bruce Allanc5083cf2011-12-16 00:45:40 +00001989 adapter->rx_ring->itr_register = adapter->hw.hw_addr +
1990 E1000_EITR_82574(vector);
Bruce Allan4662e822008-08-26 18:37:06 -07001991 adapter->rx_ring->itr_val = adapter->itr;
1992 vector++;
1993
1994 if (strlen(netdev->name) < (IFNAMSIZ - 5))
Bruce Allan79f5e842011-01-19 04:20:59 +00001995 snprintf(adapter->tx_ring->name,
1996 sizeof(adapter->tx_ring->name) - 1,
1997 "%s-tx-0", netdev->name);
Bruce Allan4662e822008-08-26 18:37:06 -07001998 else
1999 memcpy(adapter->tx_ring->name, netdev->name, IFNAMSIZ);
2000 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -08002001 e1000_intr_msix_tx, 0, adapter->tx_ring->name,
Bruce Allan4662e822008-08-26 18:37:06 -07002002 netdev);
2003 if (err)
2004 goto out;
Bruce Allanc5083cf2011-12-16 00:45:40 +00002005 adapter->tx_ring->itr_register = adapter->hw.hw_addr +
2006 E1000_EITR_82574(vector);
Bruce Allan4662e822008-08-26 18:37:06 -07002007 adapter->tx_ring->itr_val = adapter->itr;
2008 vector++;
2009
2010 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -08002011 e1000_msix_other, 0, netdev->name, netdev);
Bruce Allan4662e822008-08-26 18:37:06 -07002012 if (err)
2013 goto out;
2014
2015 e1000_configure_msix(adapter);
2016 return 0;
2017out:
2018 return err;
2019}
2020
Bruce Allanf8d59f72008-08-08 18:36:11 -07002021/**
2022 * e1000_request_irq - initialize interrupts
2023 *
2024 * Attempts to configure interrupts using the best available
2025 * capabilities of the hardware and kernel.
2026 **/
Auke Kokbc7f75f2007-09-17 12:30:59 -07002027static int e1000_request_irq(struct e1000_adapter *adapter)
2028{
2029 struct net_device *netdev = adapter->netdev;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002030 int err;
2031
Bruce Allan4662e822008-08-26 18:37:06 -07002032 if (adapter->msix_entries) {
2033 err = e1000_request_msix(adapter);
2034 if (!err)
2035 return err;
2036 /* fall back to MSI */
2037 e1000e_reset_interrupt_capability(adapter);
2038 adapter->int_mode = E1000E_INT_MODE_MSI;
2039 e1000e_set_interrupt_capability(adapter);
2040 }
2041 if (adapter->flags & FLAG_MSI_ENABLED) {
Joe Perchesa0607fd2009-11-18 23:29:17 -08002042 err = request_irq(adapter->pdev->irq, e1000_intr_msi, 0,
Bruce Allan4662e822008-08-26 18:37:06 -07002043 netdev->name, netdev);
2044 if (!err)
2045 return err;
2046
2047 /* fall back to legacy interrupt */
2048 e1000e_reset_interrupt_capability(adapter);
2049 adapter->int_mode = E1000E_INT_MODE_LEGACY;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002050 }
2051
Joe Perchesa0607fd2009-11-18 23:29:17 -08002052 err = request_irq(adapter->pdev->irq, e1000_intr, IRQF_SHARED,
Bruce Allan4662e822008-08-26 18:37:06 -07002053 netdev->name, netdev);
2054 if (err)
Bruce Allanf8d59f72008-08-08 18:36:11 -07002055 e_err("Unable to allocate interrupt, Error: %d\n", err);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002056
2057 return err;
2058}
2059
2060static void e1000_free_irq(struct e1000_adapter *adapter)
2061{
2062 struct net_device *netdev = adapter->netdev;
2063
Bruce Allan4662e822008-08-26 18:37:06 -07002064 if (adapter->msix_entries) {
2065 int vector = 0;
2066
2067 free_irq(adapter->msix_entries[vector].vector, netdev);
2068 vector++;
2069
2070 free_irq(adapter->msix_entries[vector].vector, netdev);
2071 vector++;
2072
2073 /* Other Causes interrupt vector */
2074 free_irq(adapter->msix_entries[vector].vector, netdev);
2075 return;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002076 }
Bruce Allan4662e822008-08-26 18:37:06 -07002077
2078 free_irq(adapter->pdev->irq, netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002079}
2080
2081/**
2082 * e1000_irq_disable - Mask off interrupt generation on the NIC
2083 **/
2084static void e1000_irq_disable(struct e1000_adapter *adapter)
2085{
2086 struct e1000_hw *hw = &adapter->hw;
2087
Auke Kokbc7f75f2007-09-17 12:30:59 -07002088 ew32(IMC, ~0);
Bruce Allan4662e822008-08-26 18:37:06 -07002089 if (adapter->msix_entries)
2090 ew32(EIAC_82574, 0);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002091 e1e_flush();
Jeff Kirsher8e86acd2010-08-02 14:27:23 +00002092
2093 if (adapter->msix_entries) {
2094 int i;
2095 for (i = 0; i < adapter->num_vectors; i++)
2096 synchronize_irq(adapter->msix_entries[i].vector);
2097 } else {
2098 synchronize_irq(adapter->pdev->irq);
2099 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07002100}
2101
2102/**
2103 * e1000_irq_enable - Enable default interrupt generation settings
2104 **/
2105static void e1000_irq_enable(struct e1000_adapter *adapter)
2106{
2107 struct e1000_hw *hw = &adapter->hw;
2108
Bruce Allan4662e822008-08-26 18:37:06 -07002109 if (adapter->msix_entries) {
2110 ew32(EIAC_82574, adapter->eiac_mask & E1000_EIAC_MASK_82574);
2111 ew32(IMS, adapter->eiac_mask | E1000_IMS_OTHER | E1000_IMS_LSC);
2112 } else {
2113 ew32(IMS, IMS_ENABLE_MASK);
2114 }
Jesse Brandeburg74ef9c32008-03-21 11:06:52 -07002115 e1e_flush();
Auke Kokbc7f75f2007-09-17 12:30:59 -07002116}
2117
2118/**
Bruce Allan31dbe5b2011-01-06 14:29:52 +00002119 * e1000e_get_hw_control - get control of the h/w from f/w
Auke Kokbc7f75f2007-09-17 12:30:59 -07002120 * @adapter: address of board private structure
2121 *
Bruce Allan31dbe5b2011-01-06 14:29:52 +00002122 * e1000e_get_hw_control sets {CTRL_EXT|SWSM}:DRV_LOAD bit.
Auke Kokbc7f75f2007-09-17 12:30:59 -07002123 * For ASF and Pass Through versions of f/w this means that
2124 * the driver is loaded. For AMT version (only with 82573)
2125 * of the f/w this means that the network i/f is open.
2126 **/
Bruce Allan31dbe5b2011-01-06 14:29:52 +00002127void e1000e_get_hw_control(struct e1000_adapter *adapter)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002128{
2129 struct e1000_hw *hw = &adapter->hw;
2130 u32 ctrl_ext;
2131 u32 swsm;
2132
2133 /* Let firmware know the driver has taken over */
2134 if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
2135 swsm = er32(SWSM);
2136 ew32(SWSM, swsm | E1000_SWSM_DRV_LOAD);
2137 } else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
2138 ctrl_ext = er32(CTRL_EXT);
Bruce Allanad680762008-03-28 09:15:03 -07002139 ew32(CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002140 }
2141}
2142
2143/**
Bruce Allan31dbe5b2011-01-06 14:29:52 +00002144 * e1000e_release_hw_control - release control of the h/w to f/w
Auke Kokbc7f75f2007-09-17 12:30:59 -07002145 * @adapter: address of board private structure
2146 *
Bruce Allan31dbe5b2011-01-06 14:29:52 +00002147 * e1000e_release_hw_control resets {CTRL_EXT|SWSM}:DRV_LOAD bit.
Auke Kokbc7f75f2007-09-17 12:30:59 -07002148 * For ASF and Pass Through versions of f/w this means that the
2149 * driver is no longer loaded. For AMT version (only with 82573) i
2150 * of the f/w this means that the network i/f is closed.
2151 *
2152 **/
Bruce Allan31dbe5b2011-01-06 14:29:52 +00002153void e1000e_release_hw_control(struct e1000_adapter *adapter)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002154{
2155 struct e1000_hw *hw = &adapter->hw;
2156 u32 ctrl_ext;
2157 u32 swsm;
2158
2159 /* Let firmware taken over control of h/w */
2160 if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
2161 swsm = er32(SWSM);
2162 ew32(SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
2163 } else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
2164 ctrl_ext = er32(CTRL_EXT);
Bruce Allanad680762008-03-28 09:15:03 -07002165 ew32(CTRL_EXT, ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002166 }
2167}
2168
Auke Kokbc7f75f2007-09-17 12:30:59 -07002169/**
2170 * @e1000_alloc_ring - allocate memory for a ring structure
2171 **/
2172static int e1000_alloc_ring_dma(struct e1000_adapter *adapter,
2173 struct e1000_ring *ring)
2174{
2175 struct pci_dev *pdev = adapter->pdev;
2176
2177 ring->desc = dma_alloc_coherent(&pdev->dev, ring->size, &ring->dma,
2178 GFP_KERNEL);
2179 if (!ring->desc)
2180 return -ENOMEM;
2181
2182 return 0;
2183}
2184
2185/**
2186 * e1000e_setup_tx_resources - allocate Tx resources (Descriptors)
Bruce Allan55aa6982011-12-16 00:45:45 +00002187 * @tx_ring: Tx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07002188 *
2189 * Return 0 on success, negative on failure
2190 **/
Bruce Allan55aa6982011-12-16 00:45:45 +00002191int e1000e_setup_tx_resources(struct e1000_ring *tx_ring)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002192{
Bruce Allan55aa6982011-12-16 00:45:45 +00002193 struct e1000_adapter *adapter = tx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002194 int err = -ENOMEM, size;
2195
2196 size = sizeof(struct e1000_buffer) * tx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002197 tx_ring->buffer_info = vzalloc(size);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002198 if (!tx_ring->buffer_info)
2199 goto err;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002200
2201 /* round up to nearest 4K */
2202 tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
2203 tx_ring->size = ALIGN(tx_ring->size, 4096);
2204
2205 err = e1000_alloc_ring_dma(adapter, tx_ring);
2206 if (err)
2207 goto err;
2208
2209 tx_ring->next_to_use = 0;
2210 tx_ring->next_to_clean = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002211
2212 return 0;
2213err:
2214 vfree(tx_ring->buffer_info);
Jeff Kirsher44defeb2008-08-04 17:20:41 -07002215 e_err("Unable to allocate memory for the transmit descriptor ring\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002216 return err;
2217}
2218
2219/**
2220 * e1000e_setup_rx_resources - allocate Rx resources (Descriptors)
Bruce Allan55aa6982011-12-16 00:45:45 +00002221 * @rx_ring: Rx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07002222 *
2223 * Returns 0 on success, negative on failure
2224 **/
Bruce Allan55aa6982011-12-16 00:45:45 +00002225int e1000e_setup_rx_resources(struct e1000_ring *rx_ring)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002226{
Bruce Allan55aa6982011-12-16 00:45:45 +00002227 struct e1000_adapter *adapter = rx_ring->adapter;
Auke Kok47f44e42007-10-25 13:57:44 -07002228 struct e1000_buffer *buffer_info;
2229 int i, size, desc_len, err = -ENOMEM;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002230
2231 size = sizeof(struct e1000_buffer) * rx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002232 rx_ring->buffer_info = vzalloc(size);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002233 if (!rx_ring->buffer_info)
2234 goto err;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002235
Auke Kok47f44e42007-10-25 13:57:44 -07002236 for (i = 0; i < rx_ring->count; i++) {
2237 buffer_info = &rx_ring->buffer_info[i];
2238 buffer_info->ps_pages = kcalloc(PS_PAGE_BUFFERS,
2239 sizeof(struct e1000_ps_page),
2240 GFP_KERNEL);
2241 if (!buffer_info->ps_pages)
2242 goto err_pages;
2243 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07002244
2245 desc_len = sizeof(union e1000_rx_desc_packet_split);
2246
2247 /* Round up to nearest 4K */
2248 rx_ring->size = rx_ring->count * desc_len;
2249 rx_ring->size = ALIGN(rx_ring->size, 4096);
2250
2251 err = e1000_alloc_ring_dma(adapter, rx_ring);
2252 if (err)
Auke Kok47f44e42007-10-25 13:57:44 -07002253 goto err_pages;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002254
2255 rx_ring->next_to_clean = 0;
2256 rx_ring->next_to_use = 0;
2257 rx_ring->rx_skb_top = NULL;
2258
2259 return 0;
Auke Kok47f44e42007-10-25 13:57:44 -07002260
2261err_pages:
2262 for (i = 0; i < rx_ring->count; i++) {
2263 buffer_info = &rx_ring->buffer_info[i];
2264 kfree(buffer_info->ps_pages);
2265 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07002266err:
2267 vfree(rx_ring->buffer_info);
Bruce Allane9262442010-11-24 06:02:06 +00002268 e_err("Unable to allocate memory for the receive descriptor ring\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07002269 return err;
2270}
2271
2272/**
2273 * e1000_clean_tx_ring - Free Tx Buffers
Bruce Allan55aa6982011-12-16 00:45:45 +00002274 * @tx_ring: Tx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07002275 **/
Bruce Allan55aa6982011-12-16 00:45:45 +00002276static void e1000_clean_tx_ring(struct e1000_ring *tx_ring)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002277{
Bruce Allan55aa6982011-12-16 00:45:45 +00002278 struct e1000_adapter *adapter = tx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002279 struct e1000_buffer *buffer_info;
2280 unsigned long size;
2281 unsigned int i;
2282
2283 for (i = 0; i < tx_ring->count; i++) {
2284 buffer_info = &tx_ring->buffer_info[i];
Bruce Allan55aa6982011-12-16 00:45:45 +00002285 e1000_put_txbuf(tx_ring, buffer_info);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002286 }
2287
Tom Herbert3f0cfa32011-11-28 16:33:16 +00002288 netdev_reset_queue(adapter->netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002289 size = sizeof(struct e1000_buffer) * tx_ring->count;
2290 memset(tx_ring->buffer_info, 0, size);
2291
2292 memset(tx_ring->desc, 0, tx_ring->size);
2293
2294 tx_ring->next_to_use = 0;
2295 tx_ring->next_to_clean = 0;
2296
Bruce Allanc5083cf2011-12-16 00:45:40 +00002297 writel(0, tx_ring->head);
2298 writel(0, tx_ring->tail);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002299}
2300
2301/**
2302 * e1000e_free_tx_resources - Free Tx Resources per Queue
Bruce Allan55aa6982011-12-16 00:45:45 +00002303 * @tx_ring: Tx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07002304 *
2305 * Free all transmit software resources
2306 **/
Bruce Allan55aa6982011-12-16 00:45:45 +00002307void e1000e_free_tx_resources(struct e1000_ring *tx_ring)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002308{
Bruce Allan55aa6982011-12-16 00:45:45 +00002309 struct e1000_adapter *adapter = tx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002310 struct pci_dev *pdev = adapter->pdev;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002311
Bruce Allan55aa6982011-12-16 00:45:45 +00002312 e1000_clean_tx_ring(tx_ring);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002313
2314 vfree(tx_ring->buffer_info);
2315 tx_ring->buffer_info = NULL;
2316
2317 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2318 tx_ring->dma);
2319 tx_ring->desc = NULL;
2320}
2321
2322/**
2323 * e1000e_free_rx_resources - Free Rx Resources
Bruce Allan55aa6982011-12-16 00:45:45 +00002324 * @rx_ring: Rx descriptor ring
Auke Kokbc7f75f2007-09-17 12:30:59 -07002325 *
2326 * Free all receive software resources
2327 **/
Bruce Allan55aa6982011-12-16 00:45:45 +00002328void e1000e_free_rx_resources(struct e1000_ring *rx_ring)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002329{
Bruce Allan55aa6982011-12-16 00:45:45 +00002330 struct e1000_adapter *adapter = rx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002331 struct pci_dev *pdev = adapter->pdev;
Auke Kok47f44e42007-10-25 13:57:44 -07002332 int i;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002333
Bruce Allan55aa6982011-12-16 00:45:45 +00002334 e1000_clean_rx_ring(rx_ring);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002335
Bruce Allanb1cdfea2010-12-11 05:53:47 +00002336 for (i = 0; i < rx_ring->count; i++)
Auke Kok47f44e42007-10-25 13:57:44 -07002337 kfree(rx_ring->buffer_info[i].ps_pages);
Auke Kok47f44e42007-10-25 13:57:44 -07002338
Auke Kokbc7f75f2007-09-17 12:30:59 -07002339 vfree(rx_ring->buffer_info);
2340 rx_ring->buffer_info = NULL;
2341
Auke Kokbc7f75f2007-09-17 12:30:59 -07002342 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2343 rx_ring->dma);
2344 rx_ring->desc = NULL;
2345}
2346
2347/**
2348 * e1000_update_itr - update the dynamic ITR value based on statistics
Auke Kok489815c2008-02-21 15:11:07 -08002349 * @adapter: pointer to adapter
2350 * @itr_setting: current adapter->itr
2351 * @packets: the number of packets during this measurement interval
2352 * @bytes: the number of bytes during this measurement interval
2353 *
Auke Kokbc7f75f2007-09-17 12:30:59 -07002354 * Stores a new ITR value based on packets and byte
2355 * counts during the last interrupt. The advantage of per interrupt
2356 * computation is faster updates and more accurate ITR for the current
2357 * traffic pattern. Constants in this function were computed
2358 * based on theoretical maximum wire speed and thresholds were set based
2359 * on testing data as well as attempting to minimize response time
Bruce Allan4662e822008-08-26 18:37:06 -07002360 * while increasing bulk throughput. This functionality is controlled
2361 * by the InterruptThrottleRate module parameter.
Auke Kokbc7f75f2007-09-17 12:30:59 -07002362 **/
2363static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
2364 u16 itr_setting, int packets,
2365 int bytes)
2366{
2367 unsigned int retval = itr_setting;
2368
2369 if (packets == 0)
2370 goto update_itr_done;
2371
2372 switch (itr_setting) {
2373 case lowest_latency:
2374 /* handle TSO and jumbo frames */
2375 if (bytes/packets > 8000)
2376 retval = bulk_latency;
Bruce Allanb1cdfea2010-12-11 05:53:47 +00002377 else if ((packets < 5) && (bytes > 512))
Auke Kokbc7f75f2007-09-17 12:30:59 -07002378 retval = low_latency;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002379 break;
2380 case low_latency: /* 50 usec aka 20000 ints/s */
2381 if (bytes > 10000) {
2382 /* this if handles the TSO accounting */
Bruce Allanb1cdfea2010-12-11 05:53:47 +00002383 if (bytes/packets > 8000)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002384 retval = bulk_latency;
Bruce Allanb1cdfea2010-12-11 05:53:47 +00002385 else if ((packets < 10) || ((bytes/packets) > 1200))
Auke Kokbc7f75f2007-09-17 12:30:59 -07002386 retval = bulk_latency;
Bruce Allanb1cdfea2010-12-11 05:53:47 +00002387 else if ((packets > 35))
Auke Kokbc7f75f2007-09-17 12:30:59 -07002388 retval = lowest_latency;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002389 } else if (bytes/packets > 2000) {
2390 retval = bulk_latency;
2391 } else if (packets <= 2 && bytes < 512) {
2392 retval = lowest_latency;
2393 }
2394 break;
2395 case bulk_latency: /* 250 usec aka 4000 ints/s */
2396 if (bytes > 25000) {
Bruce Allanb1cdfea2010-12-11 05:53:47 +00002397 if (packets > 35)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002398 retval = low_latency;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002399 } else if (bytes < 6000) {
2400 retval = low_latency;
2401 }
2402 break;
2403 }
2404
2405update_itr_done:
2406 return retval;
2407}
2408
2409static void e1000_set_itr(struct e1000_adapter *adapter)
2410{
2411 struct e1000_hw *hw = &adapter->hw;
2412 u16 current_itr;
2413 u32 new_itr = adapter->itr;
2414
2415 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2416 if (adapter->link_speed != SPEED_1000) {
2417 current_itr = 0;
2418 new_itr = 4000;
2419 goto set_itr_now;
2420 }
2421
Bruce Allan828bac82010-09-29 21:39:37 +00002422 if (adapter->flags2 & FLAG2_DISABLE_AIM) {
2423 new_itr = 0;
2424 goto set_itr_now;
2425 }
2426
Auke Kokbc7f75f2007-09-17 12:30:59 -07002427 adapter->tx_itr = e1000_update_itr(adapter,
2428 adapter->tx_itr,
2429 adapter->total_tx_packets,
2430 adapter->total_tx_bytes);
2431 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2432 if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
2433 adapter->tx_itr = low_latency;
2434
2435 adapter->rx_itr = e1000_update_itr(adapter,
2436 adapter->rx_itr,
2437 adapter->total_rx_packets,
2438 adapter->total_rx_bytes);
2439 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2440 if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2441 adapter->rx_itr = low_latency;
2442
2443 current_itr = max(adapter->rx_itr, adapter->tx_itr);
2444
2445 switch (current_itr) {
2446 /* counts and packets in update_itr are dependent on these numbers */
2447 case lowest_latency:
2448 new_itr = 70000;
2449 break;
2450 case low_latency:
2451 new_itr = 20000; /* aka hwitr = ~200 */
2452 break;
2453 case bulk_latency:
2454 new_itr = 4000;
2455 break;
2456 default:
2457 break;
2458 }
2459
2460set_itr_now:
2461 if (new_itr != adapter->itr) {
Bruce Allanad680762008-03-28 09:15:03 -07002462 /*
2463 * this attempts to bias the interrupt rate towards Bulk
Auke Kokbc7f75f2007-09-17 12:30:59 -07002464 * by adding intermediate steps when interrupt rate is
Bruce Allanad680762008-03-28 09:15:03 -07002465 * increasing
2466 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07002467 new_itr = new_itr > adapter->itr ?
2468 min(adapter->itr + (new_itr >> 2), new_itr) :
2469 new_itr;
2470 adapter->itr = new_itr;
Bruce Allan4662e822008-08-26 18:37:06 -07002471 adapter->rx_ring->itr_val = new_itr;
2472 if (adapter->msix_entries)
2473 adapter->rx_ring->set_itr = 1;
2474 else
Bruce Allan828bac82010-09-29 21:39:37 +00002475 if (new_itr)
2476 ew32(ITR, 1000000000 / (new_itr * 256));
2477 else
2478 ew32(ITR, 0);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002479 }
2480}
2481
2482/**
Bruce Allan4662e822008-08-26 18:37:06 -07002483 * e1000_alloc_queues - Allocate memory for all rings
2484 * @adapter: board private structure to initialize
2485 **/
2486static int __devinit e1000_alloc_queues(struct e1000_adapter *adapter)
2487{
Bruce Allan55aa6982011-12-16 00:45:45 +00002488 int size = sizeof(struct e1000_ring);
2489
2490 adapter->tx_ring = kzalloc(size, GFP_KERNEL);
Bruce Allan4662e822008-08-26 18:37:06 -07002491 if (!adapter->tx_ring)
2492 goto err;
Bruce Allan55aa6982011-12-16 00:45:45 +00002493 adapter->tx_ring->count = adapter->tx_ring_count;
2494 adapter->tx_ring->adapter = adapter;
Bruce Allan4662e822008-08-26 18:37:06 -07002495
Bruce Allan55aa6982011-12-16 00:45:45 +00002496 adapter->rx_ring = kzalloc(size, GFP_KERNEL);
Bruce Allan4662e822008-08-26 18:37:06 -07002497 if (!adapter->rx_ring)
2498 goto err;
Bruce Allan55aa6982011-12-16 00:45:45 +00002499 adapter->rx_ring->count = adapter->rx_ring_count;
2500 adapter->rx_ring->adapter = adapter;
Bruce Allan4662e822008-08-26 18:37:06 -07002501
2502 return 0;
2503err:
2504 e_err("Unable to allocate memory for queues\n");
2505 kfree(adapter->rx_ring);
2506 kfree(adapter->tx_ring);
2507 return -ENOMEM;
2508}
2509
2510/**
Auke Kokbc7f75f2007-09-17 12:30:59 -07002511 * e1000_clean - NAPI Rx polling callback
Bruce Allanad680762008-03-28 09:15:03 -07002512 * @napi: struct associated with this polling callback
Auke Kok489815c2008-02-21 15:11:07 -08002513 * @budget: amount of packets driver is allowed to process this poll
Auke Kokbc7f75f2007-09-17 12:30:59 -07002514 **/
2515static int e1000_clean(struct napi_struct *napi, int budget)
2516{
2517 struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
Bruce Allan4662e822008-08-26 18:37:06 -07002518 struct e1000_hw *hw = &adapter->hw;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002519 struct net_device *poll_dev = adapter->netdev;
Andy Gospodarek679e8a02009-06-18 11:57:37 +00002520 int tx_cleaned = 1, work_done = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002521
Wang Chen4cf16532008-11-12 23:38:14 -08002522 adapter = netdev_priv(poll_dev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002523
Bruce Allan4662e822008-08-26 18:37:06 -07002524 if (adapter->msix_entries &&
2525 !(adapter->rx_ring->ims_val & adapter->tx_ring->ims_val))
2526 goto clean_rx;
2527
Bruce Allan55aa6982011-12-16 00:45:45 +00002528 tx_cleaned = e1000_clean_tx_irq(adapter->tx_ring);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002529
Bruce Allan4662e822008-08-26 18:37:06 -07002530clean_rx:
Bruce Allan55aa6982011-12-16 00:45:45 +00002531 adapter->clean_rx(adapter->rx_ring, &work_done, budget);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002532
Alexander Duyck12d04a32009-03-25 22:05:03 +00002533 if (!tx_cleaned)
David S. Millerd2c7ddd2008-01-15 22:43:24 -08002534 work_done = budget;
2535
David S. Miller53e52c72008-01-07 21:06:12 -08002536 /* If budget not fully consumed, exit the polling mode */
2537 if (work_done < budget) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07002538 if (adapter->itr_setting & 3)
2539 e1000_set_itr(adapter);
Ben Hutchings288379f2009-01-19 16:43:59 -08002540 napi_complete(napi);
Jesse Brandeburga3c69fe2009-03-25 22:05:41 +00002541 if (!test_bit(__E1000_DOWN, &adapter->state)) {
2542 if (adapter->msix_entries)
2543 ew32(IMS, adapter->rx_ring->ims_val);
2544 else
2545 e1000_irq_enable(adapter);
2546 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07002547 }
2548
2549 return work_done;
2550}
2551
Jiri Pirko8e586132011-12-08 19:52:37 -05002552static int e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002553{
2554 struct e1000_adapter *adapter = netdev_priv(netdev);
2555 struct e1000_hw *hw = &adapter->hw;
2556 u32 vfta, index;
2557
2558 /* don't update vlan cookie if already programmed */
2559 if ((adapter->hw.mng_cookie.status &
2560 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
2561 (vid == adapter->mng_vlan_id))
Jiri Pirko8e586132011-12-08 19:52:37 -05002562 return 0;
Bruce Allancaaddaf2009-12-01 15:46:43 +00002563
Auke Kokbc7f75f2007-09-17 12:30:59 -07002564 /* add VID to filter table */
Bruce Allancaaddaf2009-12-01 15:46:43 +00002565 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2566 index = (vid >> 5) & 0x7F;
2567 vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
2568 vfta |= (1 << (vid & 0x1F));
2569 hw->mac.ops.write_vfta(hw, index, vfta);
2570 }
Jeff Kirsher86d70e52011-03-25 16:01:01 +00002571
2572 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05002573
2574 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002575}
2576
Jiri Pirko8e586132011-12-08 19:52:37 -05002577static int e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002578{
2579 struct e1000_adapter *adapter = netdev_priv(netdev);
2580 struct e1000_hw *hw = &adapter->hw;
2581 u32 vfta, index;
2582
Auke Kokbc7f75f2007-09-17 12:30:59 -07002583 if ((adapter->hw.mng_cookie.status &
2584 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
2585 (vid == adapter->mng_vlan_id)) {
2586 /* release control to f/w */
Bruce Allan31dbe5b2011-01-06 14:29:52 +00002587 e1000e_release_hw_control(adapter);
Jiri Pirko8e586132011-12-08 19:52:37 -05002588 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002589 }
2590
2591 /* remove VID from filter table */
Bruce Allancaaddaf2009-12-01 15:46:43 +00002592 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2593 index = (vid >> 5) & 0x7F;
2594 vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
2595 vfta &= ~(1 << (vid & 0x1F));
2596 hw->mac.ops.write_vfta(hw, index, vfta);
2597 }
Jeff Kirsher86d70e52011-03-25 16:01:01 +00002598
2599 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05002600
2601 return 0;
Jeff Kirsher86d70e52011-03-25 16:01:01 +00002602}
2603
2604/**
2605 * e1000e_vlan_filter_disable - helper to disable hw VLAN filtering
2606 * @adapter: board private structure to initialize
2607 **/
2608static void e1000e_vlan_filter_disable(struct e1000_adapter *adapter)
2609{
2610 struct net_device *netdev = adapter->netdev;
2611 struct e1000_hw *hw = &adapter->hw;
2612 u32 rctl;
2613
2614 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2615 /* disable VLAN receive filtering */
2616 rctl = er32(RCTL);
2617 rctl &= ~(E1000_RCTL_VFE | E1000_RCTL_CFIEN);
2618 ew32(RCTL, rctl);
2619
2620 if (adapter->mng_vlan_id != (u16)E1000_MNG_VLAN_NONE) {
2621 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
2622 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
2623 }
2624 }
2625}
2626
2627/**
2628 * e1000e_vlan_filter_enable - helper to enable HW VLAN filtering
2629 * @adapter: board private structure to initialize
2630 **/
2631static void e1000e_vlan_filter_enable(struct e1000_adapter *adapter)
2632{
2633 struct e1000_hw *hw = &adapter->hw;
2634 u32 rctl;
2635
2636 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2637 /* enable VLAN receive filtering */
2638 rctl = er32(RCTL);
2639 rctl |= E1000_RCTL_VFE;
2640 rctl &= ~E1000_RCTL_CFIEN;
2641 ew32(RCTL, rctl);
2642 }
2643}
2644
2645/**
2646 * e1000e_vlan_strip_enable - helper to disable HW VLAN stripping
2647 * @adapter: board private structure to initialize
2648 **/
2649static void e1000e_vlan_strip_disable(struct e1000_adapter *adapter)
2650{
2651 struct e1000_hw *hw = &adapter->hw;
2652 u32 ctrl;
2653
2654 /* disable VLAN tag insert/strip */
2655 ctrl = er32(CTRL);
2656 ctrl &= ~E1000_CTRL_VME;
2657 ew32(CTRL, ctrl);
2658}
2659
2660/**
2661 * e1000e_vlan_strip_enable - helper to enable HW VLAN stripping
2662 * @adapter: board private structure to initialize
2663 **/
2664static void e1000e_vlan_strip_enable(struct e1000_adapter *adapter)
2665{
2666 struct e1000_hw *hw = &adapter->hw;
2667 u32 ctrl;
2668
2669 /* enable VLAN tag insert/strip */
2670 ctrl = er32(CTRL);
2671 ctrl |= E1000_CTRL_VME;
2672 ew32(CTRL, ctrl);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002673}
2674
2675static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
2676{
2677 struct net_device *netdev = adapter->netdev;
2678 u16 vid = adapter->hw.mng_cookie.vlan_id;
2679 u16 old_vid = adapter->mng_vlan_id;
2680
Jeff Kirsher86d70e52011-03-25 16:01:01 +00002681 if (adapter->hw.mng_cookie.status &
2682 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
2683 e1000_vlan_rx_add_vid(netdev, vid);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002684 adapter->mng_vlan_id = vid;
2685 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07002686
Jeff Kirsher86d70e52011-03-25 16:01:01 +00002687 if ((old_vid != (u16)E1000_MNG_VLAN_NONE) && (vid != old_vid))
2688 e1000_vlan_rx_kill_vid(netdev, old_vid);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002689}
2690
2691static void e1000_restore_vlan(struct e1000_adapter *adapter)
2692{
2693 u16 vid;
2694
Jeff Kirsher86d70e52011-03-25 16:01:01 +00002695 e1000_vlan_rx_add_vid(adapter->netdev, 0);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002696
Jeff Kirsher86d70e52011-03-25 16:01:01 +00002697 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002698 e1000_vlan_rx_add_vid(adapter->netdev, vid);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002699}
2700
Bruce Allancd791612010-05-10 14:59:51 +00002701static void e1000_init_manageability_pt(struct e1000_adapter *adapter)
Auke Kokbc7f75f2007-09-17 12:30:59 -07002702{
2703 struct e1000_hw *hw = &adapter->hw;
Bruce Allancd791612010-05-10 14:59:51 +00002704 u32 manc, manc2h, mdef, i, j;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002705
2706 if (!(adapter->flags & FLAG_MNG_PT_ENABLED))
2707 return;
2708
2709 manc = er32(MANC);
2710
Bruce Allanad680762008-03-28 09:15:03 -07002711 /*
2712 * enable receiving management packets to the host. this will probably
Auke Kokbc7f75f2007-09-17 12:30:59 -07002713 * generate destination unreachable messages from the host OS, but
Bruce Allanad680762008-03-28 09:15:03 -07002714 * the packets will be handled on SMBUS
2715 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07002716 manc |= E1000_MANC_EN_MNG2HOST;
2717 manc2h = er32(MANC2H);
Bruce Allancd791612010-05-10 14:59:51 +00002718
2719 switch (hw->mac.type) {
2720 default:
2721 manc2h |= (E1000_MANC2H_PORT_623 | E1000_MANC2H_PORT_664);
2722 break;
2723 case e1000_82574:
2724 case e1000_82583:
2725 /*
2726 * Check if IPMI pass-through decision filter already exists;
2727 * if so, enable it.
2728 */
2729 for (i = 0, j = 0; i < 8; i++) {
2730 mdef = er32(MDEF(i));
2731
2732 /* Ignore filters with anything other than IPMI ports */
Dan Carpenter3b21b502010-06-02 13:43:15 +00002733 if (mdef & ~(E1000_MDEF_PORT_623 | E1000_MDEF_PORT_664))
Bruce Allancd791612010-05-10 14:59:51 +00002734 continue;
2735
2736 /* Enable this decision filter in MANC2H */
2737 if (mdef)
2738 manc2h |= (1 << i);
2739
2740 j |= mdef;
2741 }
2742
2743 if (j == (E1000_MDEF_PORT_623 | E1000_MDEF_PORT_664))
2744 break;
2745
2746 /* Create new decision filter in an empty filter */
2747 for (i = 0, j = 0; i < 8; i++)
2748 if (er32(MDEF(i)) == 0) {
2749 ew32(MDEF(i), (E1000_MDEF_PORT_623 |
2750 E1000_MDEF_PORT_664));
2751 manc2h |= (1 << 1);
2752 j++;
2753 break;
2754 }
2755
2756 if (!j)
2757 e_warn("Unable to create IPMI pass-through filter\n");
2758 break;
2759 }
2760
Auke Kokbc7f75f2007-09-17 12:30:59 -07002761 ew32(MANC2H, manc2h);
2762 ew32(MANC, manc);
2763}
2764
2765/**
Bruce Allanaf667a22010-12-31 06:10:01 +00002766 * e1000_configure_tx - Configure Transmit Unit after Reset
Auke Kokbc7f75f2007-09-17 12:30:59 -07002767 * @adapter: board private structure
2768 *
2769 * Configure the Tx unit of the MAC after a reset.
2770 **/
2771static void e1000_configure_tx(struct e1000_adapter *adapter)
2772{
2773 struct e1000_hw *hw = &adapter->hw;
2774 struct e1000_ring *tx_ring = adapter->tx_ring;
2775 u64 tdba;
Bruce Allanc550b122011-12-16 00:46:17 +00002776 u32 tdlen, tarc;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002777
2778 /* Setup the HW Tx Head and Tail descriptor pointers */
2779 tdba = tx_ring->dma;
2780 tdlen = tx_ring->count * sizeof(struct e1000_tx_desc);
Yang Hongyang284901a2009-04-06 19:01:15 -07002781 ew32(TDBAL, (tdba & DMA_BIT_MASK(32)));
Auke Kokbc7f75f2007-09-17 12:30:59 -07002782 ew32(TDBAH, (tdba >> 32));
2783 ew32(TDLEN, tdlen);
2784 ew32(TDH, 0);
2785 ew32(TDT, 0);
Bruce Allanc5083cf2011-12-16 00:45:40 +00002786 tx_ring->head = adapter->hw.hw_addr + E1000_TDH;
2787 tx_ring->tail = adapter->hw.hw_addr + E1000_TDT;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002788
Auke Kokbc7f75f2007-09-17 12:30:59 -07002789 /* Set the Tx Interrupt Delay register */
2790 ew32(TIDV, adapter->tx_int_delay);
Bruce Allanad680762008-03-28 09:15:03 -07002791 /* Tx irq moderation */
Auke Kokbc7f75f2007-09-17 12:30:59 -07002792 ew32(TADV, adapter->tx_abs_int_delay);
2793
Jesse Brandeburg3a3b7582010-09-29 21:38:49 +00002794 if (adapter->flags2 & FLAG2_DMA_BURST) {
2795 u32 txdctl = er32(TXDCTL(0));
2796 txdctl &= ~(E1000_TXDCTL_PTHRESH | E1000_TXDCTL_HTHRESH |
2797 E1000_TXDCTL_WTHRESH);
2798 /*
2799 * set up some performance related parameters to encourage the
2800 * hardware to use the bus more efficiently in bursts, depends
2801 * on the tx_int_delay to be enabled,
2802 * wthresh = 5 ==> burst write a cacheline (64 bytes) at a time
2803 * hthresh = 1 ==> prefetch when one or more available
2804 * pthresh = 0x1f ==> prefetch if internal cache 31 or less
2805 * BEWARE: this seems to work but should be considered first if
Bruce Allanaf667a22010-12-31 06:10:01 +00002806 * there are Tx hangs or other Tx related bugs
Jesse Brandeburg3a3b7582010-09-29 21:38:49 +00002807 */
2808 txdctl |= E1000_TXDCTL_DMA_BURST_ENABLE;
2809 ew32(TXDCTL(0), txdctl);
Jesse Brandeburg3a3b7582010-09-29 21:38:49 +00002810 }
Bruce Allan56032be2011-12-16 00:46:01 +00002811 /* erratum work around: set txdctl the same for both queues */
2812 ew32(TXDCTL(1), er32(TXDCTL(0)));
Jesse Brandeburg3a3b7582010-09-29 21:38:49 +00002813
Auke Kokbc7f75f2007-09-17 12:30:59 -07002814 if (adapter->flags & FLAG_TARC_SPEED_MODE_BIT) {
Jeff Kirshere9ec2c02008-04-02 13:48:13 -07002815 tarc = er32(TARC(0));
Bruce Allanad680762008-03-28 09:15:03 -07002816 /*
2817 * set the speed mode bit, we'll clear it if we're not at
2818 * gigabit link later
2819 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07002820#define SPEED_MODE_BIT (1 << 21)
2821 tarc |= SPEED_MODE_BIT;
Jeff Kirshere9ec2c02008-04-02 13:48:13 -07002822 ew32(TARC(0), tarc);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002823 }
2824
2825 /* errata: program both queues to unweighted RR */
2826 if (adapter->flags & FLAG_TARC_SET_BIT_ZERO) {
Jeff Kirshere9ec2c02008-04-02 13:48:13 -07002827 tarc = er32(TARC(0));
Auke Kokbc7f75f2007-09-17 12:30:59 -07002828 tarc |= 1;
Jeff Kirshere9ec2c02008-04-02 13:48:13 -07002829 ew32(TARC(0), tarc);
2830 tarc = er32(TARC(1));
Auke Kokbc7f75f2007-09-17 12:30:59 -07002831 tarc |= 1;
Jeff Kirshere9ec2c02008-04-02 13:48:13 -07002832 ew32(TARC(1), tarc);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002833 }
2834
Auke Kokbc7f75f2007-09-17 12:30:59 -07002835 /* Setup Transmit Descriptor Settings for eop descriptor */
2836 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
2837
2838 /* only set IDE if we are delaying interrupts using the timers */
2839 if (adapter->tx_int_delay)
2840 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
2841
2842 /* enable Report Status bit */
2843 adapter->txd_cmd |= E1000_TXD_CMD_RS;
2844
Simon Hormanedfea6e62009-06-08 14:28:36 +00002845 e1000e_config_collision_dist(hw);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002846}
2847
2848/**
2849 * e1000_setup_rctl - configure the receive control registers
2850 * @adapter: Board private structure
2851 **/
2852#define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
2853 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
2854static void e1000_setup_rctl(struct e1000_adapter *adapter)
2855{
2856 struct e1000_hw *hw = &adapter->hw;
2857 u32 rctl, rfctl;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002858 u32 pages = 0;
2859
Bruce Allana1ce6472010-09-22 17:16:40 +00002860 /* Workaround Si errata on 82579 - configure jumbo frame flow */
2861 if (hw->mac.type == e1000_pch2lan) {
2862 s32 ret_val;
2863
2864 if (adapter->netdev->mtu > ETH_DATA_LEN)
2865 ret_val = e1000_lv_jumbo_workaround_ich8lan(hw, true);
2866 else
2867 ret_val = e1000_lv_jumbo_workaround_ich8lan(hw, false);
Bruce Allandd93f952011-01-06 14:29:48 +00002868
2869 if (ret_val)
2870 e_dbg("failed to enable jumbo frame workaround mode\n");
Bruce Allana1ce6472010-09-22 17:16:40 +00002871 }
2872
Auke Kokbc7f75f2007-09-17 12:30:59 -07002873 /* Program MC offset vector base */
2874 rctl = er32(RCTL);
2875 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
2876 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
2877 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
2878 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
2879
2880 /* Do not Store bad packets */
2881 rctl &= ~E1000_RCTL_SBP;
2882
2883 /* Enable Long Packet receive */
2884 if (adapter->netdev->mtu <= ETH_DATA_LEN)
2885 rctl &= ~E1000_RCTL_LPE;
2886 else
2887 rctl |= E1000_RCTL_LPE;
2888
Jeff Kirshereb7c3ad2008-11-14 06:45:23 +00002889 /* Some systems expect that the CRC is included in SMBUS traffic. The
2890 * hardware strips the CRC before sending to both SMBUS (BMC) and to
2891 * host memory when this is enabled
2892 */
2893 if (adapter->flags2 & FLAG2_CRC_STRIPPING)
2894 rctl |= E1000_RCTL_SECRC;
Auke Kok5918bd82008-02-12 15:20:24 -08002895
Bruce Allana4f58f52009-06-02 11:29:18 +00002896 /* Workaround Si errata on 82577 PHY - configure IPG for jumbos */
2897 if ((hw->phy.type == e1000_phy_82577) && (rctl & E1000_RCTL_LPE)) {
2898 u16 phy_data;
2899
2900 e1e_rphy(hw, PHY_REG(770, 26), &phy_data);
2901 phy_data &= 0xfff8;
2902 phy_data |= (1 << 2);
2903 e1e_wphy(hw, PHY_REG(770, 26), phy_data);
2904
2905 e1e_rphy(hw, 22, &phy_data);
2906 phy_data &= 0x0fff;
2907 phy_data |= (1 << 14);
2908 e1e_wphy(hw, 0x10, 0x2823);
2909 e1e_wphy(hw, 0x11, 0x0003);
2910 e1e_wphy(hw, 22, phy_data);
2911 }
2912
Auke Kokbc7f75f2007-09-17 12:30:59 -07002913 /* Setup buffer sizes */
2914 rctl &= ~E1000_RCTL_SZ_4096;
2915 rctl |= E1000_RCTL_BSEX;
2916 switch (adapter->rx_buffer_len) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07002917 case 2048:
2918 default:
2919 rctl |= E1000_RCTL_SZ_2048;
2920 rctl &= ~E1000_RCTL_BSEX;
2921 break;
2922 case 4096:
2923 rctl |= E1000_RCTL_SZ_4096;
2924 break;
2925 case 8192:
2926 rctl |= E1000_RCTL_SZ_8192;
2927 break;
2928 case 16384:
2929 rctl |= E1000_RCTL_SZ_16384;
2930 break;
2931 }
2932
Bruce Allan5f450212011-07-22 06:21:46 +00002933 /* Enable Extended Status in all Receive Descriptors */
2934 rfctl = er32(RFCTL);
2935 rfctl |= E1000_RFCTL_EXTEN;
2936
Auke Kokbc7f75f2007-09-17 12:30:59 -07002937 /*
2938 * 82571 and greater support packet-split where the protocol
2939 * header is placed in skb->data and the packet data is
2940 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
2941 * In the case of a non-split, skb->data is linearly filled,
2942 * followed by the page buffers. Therefore, skb->data is
2943 * sized to hold the largest protocol header.
2944 *
2945 * allocations using alloc_page take too long for regular MTU
2946 * so only enable packet split for jumbo frames
2947 *
2948 * Using pages when the page size is greater than 16k wastes
2949 * a lot of memory, since we allocate 3 pages at all times
2950 * per packet.
2951 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07002952 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
Bruce Allan79d4e902011-12-16 00:46:27 +00002953 if ((pages <= 3) && (PAGE_SIZE <= 16384) && (rctl & E1000_RCTL_LPE))
Auke Kokbc7f75f2007-09-17 12:30:59 -07002954 adapter->rx_ps_pages = pages;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07002955 else
2956 adapter->rx_ps_pages = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002957
2958 if (adapter->rx_ps_pages) {
Bruce Allan90da0662011-01-06 07:02:53 +00002959 u32 psrctl = 0;
2960
Bruce Allanad680762008-03-28 09:15:03 -07002961 /*
2962 * disable packet split support for IPv6 extension headers,
2963 * because some malformed IPv6 headers can hang the Rx
2964 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07002965 rfctl |= (E1000_RFCTL_IPV6_EX_DIS |
2966 E1000_RFCTL_NEW_IPV6_EXT_DIS);
2967
Auke Kok140a7482007-10-25 13:57:58 -07002968 /* Enable Packet split descriptors */
2969 rctl |= E1000_RCTL_DTYP_PS;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002970
2971 psrctl |= adapter->rx_ps_bsize0 >>
2972 E1000_PSRCTL_BSIZE0_SHIFT;
2973
2974 switch (adapter->rx_ps_pages) {
2975 case 3:
2976 psrctl |= PAGE_SIZE <<
2977 E1000_PSRCTL_BSIZE3_SHIFT;
2978 case 2:
2979 psrctl |= PAGE_SIZE <<
2980 E1000_PSRCTL_BSIZE2_SHIFT;
2981 case 1:
2982 psrctl |= PAGE_SIZE >>
2983 E1000_PSRCTL_BSIZE1_SHIFT;
2984 break;
2985 }
2986
2987 ew32(PSRCTL, psrctl);
2988 }
2989
Bruce Allan5f450212011-07-22 06:21:46 +00002990 ew32(RFCTL, rfctl);
Auke Kokbc7f75f2007-09-17 12:30:59 -07002991 ew32(RCTL, rctl);
Jeff Kirsher318a94d2008-03-28 09:15:16 -07002992 /* just started the receive unit, no need to restart */
2993 adapter->flags &= ~FLAG_RX_RESTART_NOW;
Auke Kokbc7f75f2007-09-17 12:30:59 -07002994}
2995
2996/**
2997 * e1000_configure_rx - Configure Receive Unit after Reset
2998 * @adapter: board private structure
2999 *
3000 * Configure the Rx unit of the MAC after a reset.
3001 **/
3002static void e1000_configure_rx(struct e1000_adapter *adapter)
3003{
3004 struct e1000_hw *hw = &adapter->hw;
3005 struct e1000_ring *rx_ring = adapter->rx_ring;
3006 u64 rdba;
3007 u32 rdlen, rctl, rxcsum, ctrl_ext;
3008
3009 if (adapter->rx_ps_pages) {
3010 /* this is a 32 byte descriptor */
3011 rdlen = rx_ring->count *
Bruce Allanaf667a22010-12-31 06:10:01 +00003012 sizeof(union e1000_rx_desc_packet_split);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003013 adapter->clean_rx = e1000_clean_rx_irq_ps;
3014 adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
Bruce Allan97ac8ca2008-04-29 09:16:05 -07003015 } else if (adapter->netdev->mtu > ETH_FRAME_LEN + ETH_FCS_LEN) {
Bruce Allan5f450212011-07-22 06:21:46 +00003016 rdlen = rx_ring->count * sizeof(union e1000_rx_desc_extended);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07003017 adapter->clean_rx = e1000_clean_jumbo_rx_irq;
3018 adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003019 } else {
Bruce Allan5f450212011-07-22 06:21:46 +00003020 rdlen = rx_ring->count * sizeof(union e1000_rx_desc_extended);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003021 adapter->clean_rx = e1000_clean_rx_irq;
3022 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
3023 }
3024
3025 /* disable receives while setting up the descriptors */
3026 rctl = er32(RCTL);
David S. Miller823dcd22011-08-20 10:39:12 -07003027 if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
3028 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003029 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00003030 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003031
Jesse Brandeburg3a3b7582010-09-29 21:38:49 +00003032 if (adapter->flags2 & FLAG2_DMA_BURST) {
3033 /*
3034 * set the writeback threshold (only takes effect if the RDTR
3035 * is set). set GRAN=1 and write back up to 0x4 worth, and
Bruce Allanaf667a22010-12-31 06:10:01 +00003036 * enable prefetching of 0x20 Rx descriptors
Jesse Brandeburg3a3b7582010-09-29 21:38:49 +00003037 * granularity = 01
3038 * wthresh = 04,
3039 * hthresh = 04,
3040 * pthresh = 0x20
3041 */
3042 ew32(RXDCTL(0), E1000_RXDCTL_DMA_BURST_ENABLE);
3043 ew32(RXDCTL(1), E1000_RXDCTL_DMA_BURST_ENABLE);
3044
3045 /*
3046 * override the delay timers for enabling bursting, only if
3047 * the value was not set by the user via module options
3048 */
3049 if (adapter->rx_int_delay == DEFAULT_RDTR)
3050 adapter->rx_int_delay = BURST_RDTR;
3051 if (adapter->rx_abs_int_delay == DEFAULT_RADV)
3052 adapter->rx_abs_int_delay = BURST_RADV;
3053 }
3054
Auke Kokbc7f75f2007-09-17 12:30:59 -07003055 /* set the Receive Delay Timer Register */
3056 ew32(RDTR, adapter->rx_int_delay);
3057
3058 /* irq moderation */
3059 ew32(RADV, adapter->rx_abs_int_delay);
Bruce Allan828bac82010-09-29 21:39:37 +00003060 if ((adapter->itr_setting != 0) && (adapter->itr != 0))
Bruce Allanad680762008-03-28 09:15:03 -07003061 ew32(ITR, 1000000000 / (adapter->itr * 256));
Auke Kokbc7f75f2007-09-17 12:30:59 -07003062
3063 ctrl_ext = er32(CTRL_EXT);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003064 /* Auto-Mask interrupts upon ICR access */
3065 ctrl_ext |= E1000_CTRL_EXT_IAME;
3066 ew32(IAM, 0xffffffff);
3067 ew32(CTRL_EXT, ctrl_ext);
3068 e1e_flush();
3069
Bruce Allanad680762008-03-28 09:15:03 -07003070 /*
3071 * Setup the HW Rx Head and Tail Descriptor Pointers and
3072 * the Base and Length of the Rx Descriptor Ring
3073 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07003074 rdba = rx_ring->dma;
Yang Hongyang284901a2009-04-06 19:01:15 -07003075 ew32(RDBAL, (rdba & DMA_BIT_MASK(32)));
Auke Kokbc7f75f2007-09-17 12:30:59 -07003076 ew32(RDBAH, (rdba >> 32));
3077 ew32(RDLEN, rdlen);
3078 ew32(RDH, 0);
3079 ew32(RDT, 0);
Bruce Allanc5083cf2011-12-16 00:45:40 +00003080 rx_ring->head = adapter->hw.hw_addr + E1000_RDH;
3081 rx_ring->tail = adapter->hw.hw_addr + E1000_RDT;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003082
3083 /* Enable Receive Checksum Offload for TCP and UDP */
3084 rxcsum = er32(RXCSUM);
Bruce Allandc221292011-08-19 03:23:48 +00003085 if (adapter->netdev->features & NETIF_F_RXCSUM) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07003086 rxcsum |= E1000_RXCSUM_TUOFL;
3087
Bruce Allanad680762008-03-28 09:15:03 -07003088 /*
3089 * IPv4 payload checksum for UDP fragments must be
3090 * used in conjunction with packet-split.
3091 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07003092 if (adapter->rx_ps_pages)
3093 rxcsum |= E1000_RXCSUM_IPPCSE;
3094 } else {
3095 rxcsum &= ~E1000_RXCSUM_TUOFL;
3096 /* no need to clear IPPCSE as it defaults to 0 */
3097 }
3098 ew32(RXCSUM, rxcsum);
3099
Bruce Allan79d4e902011-12-16 00:46:27 +00003100 if (adapter->hw.mac.type == e1000_pch2lan) {
3101 /*
3102 * With jumbo frames, excessive C-state transition
3103 * latencies result in dropped transactions.
3104 */
Bruce Allan53ec5492009-11-20 23:27:40 +00003105 if (adapter->netdev->mtu > ETH_DATA_LEN) {
3106 u32 rxdctl = er32(RXDCTL(0));
3107 ew32(RXDCTL(0), rxdctl | 0x3);
Bruce Allanaf667a22010-12-31 06:10:01 +00003108 pm_qos_update_request(&adapter->netdev->pm_qos_req, 55);
Bruce Allan53ec5492009-11-20 23:27:40 +00003109 } else {
Bruce Allanaf667a22010-12-31 06:10:01 +00003110 pm_qos_update_request(&adapter->netdev->pm_qos_req,
3111 PM_QOS_DEFAULT_VALUE);
Bruce Allan53ec5492009-11-20 23:27:40 +00003112 }
Bruce Allan97ac8ca2008-04-29 09:16:05 -07003113 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07003114
3115 /* Enable Receives */
3116 ew32(RCTL, rctl);
3117}
3118
3119/**
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00003120 * e1000e_write_mc_addr_list - write multicast addresses to MTA
Auke Kokbc7f75f2007-09-17 12:30:59 -07003121 * @netdev: network interface device structure
3122 *
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00003123 * Writes multicast address list to the MTA hash table.
3124 * Returns: -ENOMEM on failure
3125 * 0 on no addresses written
3126 * X on writing X addresses to MTA
3127 */
3128static int e1000e_write_mc_addr_list(struct net_device *netdev)
Auke Kokbc7f75f2007-09-17 12:30:59 -07003129{
3130 struct e1000_adapter *adapter = netdev_priv(netdev);
3131 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003132 struct netdev_hw_addr *ha;
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00003133 u8 *mta_list;
3134 int i;
3135
3136 if (netdev_mc_empty(netdev)) {
3137 /* nothing to program, so clear mc list */
3138 hw->mac.ops.update_mc_addr_list(hw, NULL, 0);
3139 return 0;
3140 }
3141
3142 mta_list = kzalloc(netdev_mc_count(netdev) * ETH_ALEN, GFP_ATOMIC);
3143 if (!mta_list)
3144 return -ENOMEM;
3145
3146 /* update_mc_addr_list expects a packed array of only addresses. */
3147 i = 0;
3148 netdev_for_each_mc_addr(ha, netdev)
3149 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
3150
3151 hw->mac.ops.update_mc_addr_list(hw, mta_list, i);
3152 kfree(mta_list);
3153
3154 return netdev_mc_count(netdev);
3155}
3156
3157/**
3158 * e1000e_write_uc_addr_list - write unicast addresses to RAR table
3159 * @netdev: network interface device structure
3160 *
3161 * Writes unicast address list to the RAR table.
3162 * Returns: -ENOMEM on failure/insufficient address space
3163 * 0 on no addresses written
3164 * X on writing X addresses to the RAR table
3165 **/
3166static int e1000e_write_uc_addr_list(struct net_device *netdev)
3167{
3168 struct e1000_adapter *adapter = netdev_priv(netdev);
3169 struct e1000_hw *hw = &adapter->hw;
3170 unsigned int rar_entries = hw->mac.rar_entry_count;
3171 int count = 0;
3172
3173 /* save a rar entry for our hardware address */
3174 rar_entries--;
3175
3176 /* save a rar entry for the LAA workaround */
3177 if (adapter->flags & FLAG_RESET_OVERWRITES_LAA)
3178 rar_entries--;
3179
3180 /* return ENOMEM indicating insufficient memory for addresses */
3181 if (netdev_uc_count(netdev) > rar_entries)
3182 return -ENOMEM;
3183
3184 if (!netdev_uc_empty(netdev) && rar_entries) {
3185 struct netdev_hw_addr *ha;
3186
3187 /*
3188 * write the addresses in reverse order to avoid write
3189 * combining
3190 */
3191 netdev_for_each_uc_addr(ha, netdev) {
3192 if (!rar_entries)
3193 break;
3194 e1000e_rar_set(hw, ha->addr, rar_entries--);
3195 count++;
3196 }
3197 }
3198
3199 /* zero out the remaining RAR entries not used above */
3200 for (; rar_entries > 0; rar_entries--) {
3201 ew32(RAH(rar_entries), 0);
3202 ew32(RAL(rar_entries), 0);
3203 }
3204 e1e_flush();
3205
3206 return count;
3207}
3208
3209/**
3210 * e1000e_set_rx_mode - secondary unicast, Multicast and Promiscuous mode set
3211 * @netdev: network interface device structure
3212 *
3213 * The ndo_set_rx_mode entry point is called whenever the unicast or multicast
3214 * address list or the network interface flags are updated. This routine is
3215 * responsible for configuring the hardware for proper unicast, multicast,
3216 * promiscuous mode, and all-multi behavior.
3217 **/
3218static void e1000e_set_rx_mode(struct net_device *netdev)
3219{
3220 struct e1000_adapter *adapter = netdev_priv(netdev);
3221 struct e1000_hw *hw = &adapter->hw;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003222 u32 rctl;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003223
3224 /* Check for Promiscuous and All Multicast modes */
Auke Kokbc7f75f2007-09-17 12:30:59 -07003225 rctl = er32(RCTL);
3226
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00003227 /* clear the affected bits */
3228 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
3229
Auke Kokbc7f75f2007-09-17 12:30:59 -07003230 if (netdev->flags & IFF_PROMISC) {
3231 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Jeff Kirsher86d70e52011-03-25 16:01:01 +00003232 /* Do not hardware filter VLANs in promisc mode */
3233 e1000e_vlan_filter_disable(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003234 } else {
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00003235 int count;
Patrick McHardy746b9f02008-07-16 20:15:45 -07003236 if (netdev->flags & IFF_ALLMULTI) {
3237 rctl |= E1000_RCTL_MPE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07003238 } else {
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00003239 /*
3240 * Write addresses to the MTA, if the attempt fails
3241 * then we should just turn on promiscuous mode so
3242 * that we can at least receive multicast traffic
3243 */
3244 count = e1000e_write_mc_addr_list(netdev);
3245 if (count < 0)
3246 rctl |= E1000_RCTL_MPE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07003247 }
Jeff Kirsher86d70e52011-03-25 16:01:01 +00003248 e1000e_vlan_filter_enable(adapter);
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00003249 /*
3250 * Write addresses to available RAR registers, if there is not
3251 * sufficient space to store all the addresses then enable
3252 * unicast promiscuous mode
3253 */
3254 count = e1000e_write_uc_addr_list(netdev);
3255 if (count < 0)
3256 rctl |= E1000_RCTL_UPE;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003257 }
3258
3259 ew32(RCTL, rctl);
3260
Jeff Kirsher86d70e52011-03-25 16:01:01 +00003261 if (netdev->features & NETIF_F_HW_VLAN_RX)
3262 e1000e_vlan_strip_enable(adapter);
3263 else
3264 e1000e_vlan_strip_disable(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003265}
3266
Bruce Allan70495a52012-01-11 01:26:50 +00003267static void e1000e_setup_rss_hash(struct e1000_adapter *adapter)
3268{
3269 struct e1000_hw *hw = &adapter->hw;
3270 u32 mrqc, rxcsum;
3271 int i;
3272 static const u32 rsskey[10] = {
3273 0xda565a6d, 0xc20e5b25, 0x3d256741, 0xb08fa343, 0xcb2bcad0,
3274 0xb4307bae, 0xa32dcb77, 0x0cf23080, 0x3bb7426a, 0xfa01acbe
3275 };
3276
3277 /* Fill out hash function seed */
3278 for (i = 0; i < 10; i++)
3279 ew32(RSSRK(i), rsskey[i]);
3280
3281 /* Direct all traffic to queue 0 */
3282 for (i = 0; i < 32; i++)
3283 ew32(RETA(i), 0);
3284
3285 /*
3286 * Disable raw packet checksumming so that RSS hash is placed in
3287 * descriptor on writeback.
3288 */
3289 rxcsum = er32(RXCSUM);
3290 rxcsum |= E1000_RXCSUM_PCSD;
3291
3292 ew32(RXCSUM, rxcsum);
3293
3294 mrqc = (E1000_MRQC_RSS_FIELD_IPV4 |
3295 E1000_MRQC_RSS_FIELD_IPV4_TCP |
3296 E1000_MRQC_RSS_FIELD_IPV6 |
3297 E1000_MRQC_RSS_FIELD_IPV6_TCP |
3298 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
3299
3300 ew32(MRQC, mrqc);
3301}
3302
Auke Kokbc7f75f2007-09-17 12:30:59 -07003303/**
Bruce Allanad680762008-03-28 09:15:03 -07003304 * e1000_configure - configure the hardware for Rx and Tx
Auke Kokbc7f75f2007-09-17 12:30:59 -07003305 * @adapter: private board structure
3306 **/
3307static void e1000_configure(struct e1000_adapter *adapter)
3308{
Bruce Allan55aa6982011-12-16 00:45:45 +00003309 struct e1000_ring *rx_ring = adapter->rx_ring;
3310
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00003311 e1000e_set_rx_mode(adapter->netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003312
3313 e1000_restore_vlan(adapter);
Bruce Allancd791612010-05-10 14:59:51 +00003314 e1000_init_manageability_pt(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003315
3316 e1000_configure_tx(adapter);
Bruce Allan70495a52012-01-11 01:26:50 +00003317
3318 if (adapter->netdev->features & NETIF_F_RXHASH)
3319 e1000e_setup_rss_hash(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003320 e1000_setup_rctl(adapter);
3321 e1000_configure_rx(adapter);
Bruce Allan55aa6982011-12-16 00:45:45 +00003322 adapter->alloc_rx_buf(rx_ring, e1000_desc_unused(rx_ring), GFP_KERNEL);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003323}
3324
3325/**
3326 * e1000e_power_up_phy - restore link in case the phy was powered down
3327 * @adapter: address of board private structure
3328 *
3329 * The phy may be powered down to save power and turn off link when the
3330 * driver is unloaded and wake on lan is not enabled (among others)
3331 * *** this routine MUST be followed by a call to e1000e_reset ***
3332 **/
3333void e1000e_power_up_phy(struct e1000_adapter *adapter)
3334{
Bruce Allan17f208d2009-12-01 15:47:22 +00003335 if (adapter->hw.phy.ops.power_up)
3336 adapter->hw.phy.ops.power_up(&adapter->hw);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003337
3338 adapter->hw.mac.ops.setup_link(&adapter->hw);
3339}
3340
3341/**
3342 * e1000_power_down_phy - Power down the PHY
3343 *
Bruce Allan17f208d2009-12-01 15:47:22 +00003344 * Power down the PHY so no link is implied when interface is down.
3345 * The PHY cannot be powered down if management or WoL is active.
Auke Kokbc7f75f2007-09-17 12:30:59 -07003346 */
3347static void e1000_power_down_phy(struct e1000_adapter *adapter)
3348{
Auke Kokbc7f75f2007-09-17 12:30:59 -07003349 /* WoL is enabled */
Auke Kok23b66e22008-02-11 09:25:51 -08003350 if (adapter->wol)
Auke Kokbc7f75f2007-09-17 12:30:59 -07003351 return;
3352
Bruce Allan17f208d2009-12-01 15:47:22 +00003353 if (adapter->hw.phy.ops.power_down)
3354 adapter->hw.phy.ops.power_down(&adapter->hw);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003355}
3356
3357/**
3358 * e1000e_reset - bring the hardware into a known good state
3359 *
3360 * This function boots the hardware and enables some settings that
3361 * require a configuration cycle of the hardware - those cannot be
3362 * set/changed during runtime. After reset the device needs to be
Bruce Allanad680762008-03-28 09:15:03 -07003363 * properly configured for Rx, Tx etc.
Auke Kokbc7f75f2007-09-17 12:30:59 -07003364 */
3365void e1000e_reset(struct e1000_adapter *adapter)
3366{
3367 struct e1000_mac_info *mac = &adapter->hw.mac;
Jeff Kirsher318a94d2008-03-28 09:15:16 -07003368 struct e1000_fc_info *fc = &adapter->hw.fc;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003369 struct e1000_hw *hw = &adapter->hw;
3370 u32 tx_space, min_tx_space, min_rx_space;
Jeff Kirsher318a94d2008-03-28 09:15:16 -07003371 u32 pba = adapter->pba;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003372 u16 hwm;
3373
Bruce Allanad680762008-03-28 09:15:03 -07003374 /* reset Packet Buffer Allocation to default */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07003375 ew32(PBA, pba);
Auke Kokdf762462007-10-25 13:57:53 -07003376
Jeff Kirsher318a94d2008-03-28 09:15:16 -07003377 if (adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) {
Bruce Allanad680762008-03-28 09:15:03 -07003378 /*
3379 * To maintain wire speed transmits, the Tx FIFO should be
Auke Kokbc7f75f2007-09-17 12:30:59 -07003380 * large enough to accommodate two full transmit packets,
3381 * rounded up to the next 1KB and expressed in KB. Likewise,
3382 * the Rx FIFO should be large enough to accommodate at least
3383 * one full receive packet and is similarly rounded up and
Bruce Allanad680762008-03-28 09:15:03 -07003384 * expressed in KB.
3385 */
Auke Kokdf762462007-10-25 13:57:53 -07003386 pba = er32(PBA);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003387 /* upper 16 bits has Tx packet buffer allocation size in KB */
Auke Kokdf762462007-10-25 13:57:53 -07003388 tx_space = pba >> 16;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003389 /* lower 16 bits has Rx packet buffer allocation size in KB */
Auke Kokdf762462007-10-25 13:57:53 -07003390 pba &= 0xffff;
Bruce Allanad680762008-03-28 09:15:03 -07003391 /*
Bruce Allanaf667a22010-12-31 06:10:01 +00003392 * the Tx fifo also stores 16 bytes of information about the Tx
Bruce Allanad680762008-03-28 09:15:03 -07003393 * but don't include ethernet FCS because hardware appends it
Jeff Kirsher318a94d2008-03-28 09:15:16 -07003394 */
3395 min_tx_space = (adapter->max_frame_size +
Auke Kokbc7f75f2007-09-17 12:30:59 -07003396 sizeof(struct e1000_tx_desc) -
3397 ETH_FCS_LEN) * 2;
3398 min_tx_space = ALIGN(min_tx_space, 1024);
3399 min_tx_space >>= 10;
3400 /* software strips receive CRC, so leave room for it */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07003401 min_rx_space = adapter->max_frame_size;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003402 min_rx_space = ALIGN(min_rx_space, 1024);
3403 min_rx_space >>= 10;
3404
Bruce Allanad680762008-03-28 09:15:03 -07003405 /*
3406 * If current Tx allocation is less than the min Tx FIFO size,
Auke Kokbc7f75f2007-09-17 12:30:59 -07003407 * and the min Tx FIFO size is less than the current Rx FIFO
Bruce Allanad680762008-03-28 09:15:03 -07003408 * allocation, take space away from current Rx allocation
3409 */
Auke Kokdf762462007-10-25 13:57:53 -07003410 if ((tx_space < min_tx_space) &&
3411 ((min_tx_space - tx_space) < pba)) {
3412 pba -= min_tx_space - tx_space;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003413
Bruce Allanad680762008-03-28 09:15:03 -07003414 /*
Bruce Allanaf667a22010-12-31 06:10:01 +00003415 * if short on Rx space, Rx wins and must trump Tx
Bruce Allanad680762008-03-28 09:15:03 -07003416 * adjustment or use Early Receive if available
3417 */
Bruce Allan79d4e902011-12-16 00:46:27 +00003418 if (pba < min_rx_space)
Auke Kokdf762462007-10-25 13:57:53 -07003419 pba = min_rx_space;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003420 }
Auke Kokdf762462007-10-25 13:57:53 -07003421
3422 ew32(PBA, pba);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003423 }
3424
Bruce Allanad680762008-03-28 09:15:03 -07003425 /*
3426 * flow control settings
3427 *
Bruce Allan38eb3942009-11-19 12:34:20 +00003428 * The high water mark must be low enough to fit one full frame
Auke Kokbc7f75f2007-09-17 12:30:59 -07003429 * (or the size used for early receive) above it in the Rx FIFO.
3430 * Set it to the lower of:
3431 * - 90% of the Rx FIFO size, and
Bruce Allan38eb3942009-11-19 12:34:20 +00003432 * - the full Rx FIFO size minus one full frame
Bruce Allanad680762008-03-28 09:15:03 -07003433 */
Bruce Alland3738bb2010-06-16 13:27:28 +00003434 if (adapter->flags & FLAG_DISABLE_FC_PAUSE_TIME)
3435 fc->pause_time = 0xFFFF;
3436 else
3437 fc->pause_time = E1000_FC_PAUSE_TIME;
3438 fc->send_xon = 1;
3439 fc->current_mode = fc->requested_mode;
3440
3441 switch (hw->mac.type) {
Bruce Allan79d4e902011-12-16 00:46:27 +00003442 case e1000_ich9lan:
3443 case e1000_ich10lan:
3444 if (adapter->netdev->mtu > ETH_DATA_LEN) {
3445 pba = 14;
3446 ew32(PBA, pba);
3447 fc->high_water = 0x2800;
3448 fc->low_water = fc->high_water - 8;
3449 break;
3450 }
3451 /* fall-through */
Bruce Alland3738bb2010-06-16 13:27:28 +00003452 default:
Bruce Allan79d4e902011-12-16 00:46:27 +00003453 hwm = min(((pba << 10) * 9 / 10),
3454 ((pba << 10) - adapter->max_frame_size));
Bruce Alland3738bb2010-06-16 13:27:28 +00003455
3456 fc->high_water = hwm & E1000_FCRTH_RTH; /* 8-byte granularity */
3457 fc->low_water = fc->high_water - 8;
3458 break;
3459 case e1000_pchlan:
Bruce Allan38eb3942009-11-19 12:34:20 +00003460 /*
3461 * Workaround PCH LOM adapter hangs with certain network
3462 * loads. If hangs persist, try disabling Tx flow control.
3463 */
3464 if (adapter->netdev->mtu > ETH_DATA_LEN) {
3465 fc->high_water = 0x3500;
3466 fc->low_water = 0x1500;
3467 } else {
3468 fc->high_water = 0x5000;
3469 fc->low_water = 0x3000;
3470 }
Bruce Allana3055952010-05-10 15:02:12 +00003471 fc->refresh_time = 0x1000;
Bruce Alland3738bb2010-06-16 13:27:28 +00003472 break;
3473 case e1000_pch2lan:
3474 fc->high_water = 0x05C20;
3475 fc->low_water = 0x05048;
3476 fc->pause_time = 0x0650;
3477 fc->refresh_time = 0x0400;
Bruce Allan828bac82010-09-29 21:39:37 +00003478 if (adapter->netdev->mtu > ETH_DATA_LEN) {
3479 pba = 14;
3480 ew32(PBA, pba);
3481 }
Bruce Alland3738bb2010-06-16 13:27:28 +00003482 break;
Bruce Allan38eb3942009-11-19 12:34:20 +00003483 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07003484
Bruce Allan828bac82010-09-29 21:39:37 +00003485 /*
3486 * Disable Adaptive Interrupt Moderation if 2 full packets cannot
Bruce Allan79d4e902011-12-16 00:46:27 +00003487 * fit in receive buffer.
Bruce Allan828bac82010-09-29 21:39:37 +00003488 */
3489 if (adapter->itr_setting & 0x3) {
Bruce Allan79d4e902011-12-16 00:46:27 +00003490 if ((adapter->max_frame_size * 2) > (pba << 10)) {
Bruce Allan828bac82010-09-29 21:39:37 +00003491 if (!(adapter->flags2 & FLAG2_DISABLE_AIM)) {
3492 dev_info(&adapter->pdev->dev,
3493 "Interrupt Throttle Rate turned off\n");
3494 adapter->flags2 |= FLAG2_DISABLE_AIM;
3495 ew32(ITR, 0);
3496 }
3497 } else if (adapter->flags2 & FLAG2_DISABLE_AIM) {
3498 dev_info(&adapter->pdev->dev,
3499 "Interrupt Throttle Rate turned on\n");
3500 adapter->flags2 &= ~FLAG2_DISABLE_AIM;
3501 adapter->itr = 20000;
3502 ew32(ITR, 1000000000 / (adapter->itr * 256));
3503 }
3504 }
3505
Auke Kokbc7f75f2007-09-17 12:30:59 -07003506 /* Allow time for pending master requests to run */
3507 mac->ops.reset_hw(hw);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07003508
3509 /*
3510 * For parts with AMT enabled, let the firmware know
3511 * that the network interface is in control
3512 */
Jesse Brandeburgc43bc57e2008-08-04 17:21:40 -07003513 if (adapter->flags & FLAG_HAS_AMT)
Bruce Allan31dbe5b2011-01-06 14:29:52 +00003514 e1000e_get_hw_control(adapter);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07003515
Auke Kokbc7f75f2007-09-17 12:30:59 -07003516 ew32(WUC, 0);
3517
3518 if (mac->ops.init_hw(hw))
Jeff Kirsher44defeb2008-08-04 17:20:41 -07003519 e_err("Hardware Error\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07003520
3521 e1000_update_mng_vlan(adapter);
3522
3523 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
3524 ew32(VET, ETH_P_8021Q);
3525
3526 e1000e_reset_adaptive(hw);
Bruce Allan31dbe5b2011-01-06 14:29:52 +00003527
3528 if (!netif_running(adapter->netdev) &&
3529 !test_bit(__E1000_TESTING, &adapter->state)) {
3530 e1000_power_down_phy(adapter);
3531 return;
3532 }
3533
Auke Kokbc7f75f2007-09-17 12:30:59 -07003534 e1000_get_phy_info(hw);
3535
Bruce Allan918d7192009-06-02 11:28:20 +00003536 if ((adapter->flags & FLAG_HAS_SMART_POWER_DOWN) &&
3537 !(adapter->flags & FLAG_SMART_POWER_DOWN)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07003538 u16 phy_data = 0;
Bruce Allanad680762008-03-28 09:15:03 -07003539 /*
3540 * speed up time to link by disabling smart power down, ignore
Auke Kokbc7f75f2007-09-17 12:30:59 -07003541 * the return value of this function because there is nothing
Bruce Allanad680762008-03-28 09:15:03 -07003542 * different we would do if it failed
3543 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07003544 e1e_rphy(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
3545 phy_data &= ~IGP02E1000_PM_SPD;
3546 e1e_wphy(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
3547 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07003548}
3549
3550int e1000e_up(struct e1000_adapter *adapter)
3551{
3552 struct e1000_hw *hw = &adapter->hw;
3553
3554 /* hardware has been reset, we need to reload some things */
3555 e1000_configure(adapter);
3556
3557 clear_bit(__E1000_DOWN, &adapter->state);
3558
Bruce Allan4662e822008-08-26 18:37:06 -07003559 if (adapter->msix_entries)
3560 e1000_configure_msix(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003561 e1000_irq_enable(adapter);
3562
Bruce Allan400484f2011-05-13 07:20:03 +00003563 netif_start_queue(adapter->netdev);
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00003564
Auke Kokbc7f75f2007-09-17 12:30:59 -07003565 /* fire a link change interrupt to start the watchdog */
Bruce Allan52a9b232010-05-10 15:00:10 +00003566 if (adapter->msix_entries)
3567 ew32(ICS, E1000_ICS_LSC | E1000_ICR_OTHER);
3568 else
3569 ew32(ICS, E1000_ICS_LSC);
3570
Auke Kokbc7f75f2007-09-17 12:30:59 -07003571 return 0;
3572}
3573
Jesse Brandeburg713b3c92011-02-02 10:19:50 +00003574static void e1000e_flush_descriptors(struct e1000_adapter *adapter)
3575{
3576 struct e1000_hw *hw = &adapter->hw;
3577
3578 if (!(adapter->flags2 & FLAG2_DMA_BURST))
3579 return;
3580
3581 /* flush pending descriptor writebacks to memory */
3582 ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
3583 ew32(RDTR, adapter->rx_int_delay | E1000_RDTR_FPD);
3584
3585 /* execute the writes immediately */
3586 e1e_flush();
3587}
3588
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00003589static void e1000e_update_stats(struct e1000_adapter *adapter);
3590
Auke Kokbc7f75f2007-09-17 12:30:59 -07003591void e1000e_down(struct e1000_adapter *adapter)
3592{
3593 struct net_device *netdev = adapter->netdev;
3594 struct e1000_hw *hw = &adapter->hw;
3595 u32 tctl, rctl;
3596
Bruce Allanad680762008-03-28 09:15:03 -07003597 /*
3598 * signal that we're down so the interrupt handler does not
3599 * reschedule our watchdog timer
3600 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07003601 set_bit(__E1000_DOWN, &adapter->state);
3602
3603 /* disable receives in the hardware */
3604 rctl = er32(RCTL);
David S. Miller823dcd22011-08-20 10:39:12 -07003605 if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
3606 ew32(RCTL, rctl & ~E1000_RCTL_EN);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003607 /* flush and sleep below */
3608
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00003609 netif_stop_queue(netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003610
3611 /* disable transmits in the hardware */
3612 tctl = er32(TCTL);
3613 tctl &= ~E1000_TCTL_EN;
3614 ew32(TCTL, tctl);
David S. Miller823dcd22011-08-20 10:39:12 -07003615
Auke Kokbc7f75f2007-09-17 12:30:59 -07003616 /* flush both disables and wait for them to finish */
3617 e1e_flush();
Bruce Allan1bba4382011-03-19 00:27:20 +00003618 usleep_range(10000, 20000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003619
Auke Kokbc7f75f2007-09-17 12:30:59 -07003620 e1000_irq_disable(adapter);
3621
3622 del_timer_sync(&adapter->watchdog_timer);
3623 del_timer_sync(&adapter->phy_info_timer);
3624
Auke Kokbc7f75f2007-09-17 12:30:59 -07003625 netif_carrier_off(netdev);
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00003626
3627 spin_lock(&adapter->stats64_lock);
3628 e1000e_update_stats(adapter);
3629 spin_unlock(&adapter->stats64_lock);
3630
Bruce Allan400484f2011-05-13 07:20:03 +00003631 e1000e_flush_descriptors(adapter);
Bruce Allan55aa6982011-12-16 00:45:45 +00003632 e1000_clean_tx_ring(adapter->tx_ring);
3633 e1000_clean_rx_ring(adapter->rx_ring);
Bruce Allan400484f2011-05-13 07:20:03 +00003634
Auke Kokbc7f75f2007-09-17 12:30:59 -07003635 adapter->link_speed = 0;
3636 adapter->link_duplex = 0;
3637
Jeff Kirsher52cc3082008-06-24 17:01:29 -07003638 if (!pci_channel_offline(adapter->pdev))
3639 e1000e_reset(adapter);
Jesse Brandeburg713b3c92011-02-02 10:19:50 +00003640
Auke Kokbc7f75f2007-09-17 12:30:59 -07003641 /*
3642 * TODO: for power management, we could drop the link and
3643 * pci_disable_device here.
3644 */
3645}
3646
3647void e1000e_reinit_locked(struct e1000_adapter *adapter)
3648{
3649 might_sleep();
3650 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
Bruce Allan1bba4382011-03-19 00:27:20 +00003651 usleep_range(1000, 2000);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003652 e1000e_down(adapter);
3653 e1000e_up(adapter);
3654 clear_bit(__E1000_RESETTING, &adapter->state);
3655}
3656
3657/**
3658 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
3659 * @adapter: board private structure to initialize
3660 *
3661 * e1000_sw_init initializes the Adapter private data structure.
3662 * Fields are initialized based on PCI device information and
3663 * OS network device settings (MTU size).
3664 **/
3665static int __devinit e1000_sw_init(struct e1000_adapter *adapter)
3666{
Auke Kokbc7f75f2007-09-17 12:30:59 -07003667 struct net_device *netdev = adapter->netdev;
3668
3669 adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
3670 adapter->rx_ps_bsize0 = 128;
Jeff Kirsher318a94d2008-03-28 09:15:16 -07003671 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
3672 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
Bruce Allan55aa6982011-12-16 00:45:45 +00003673 adapter->tx_ring_count = E1000_DEFAULT_TXD;
3674 adapter->rx_ring_count = E1000_DEFAULT_RXD;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003675
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00003676 spin_lock_init(&adapter->stats64_lock);
3677
Bruce Allan4662e822008-08-26 18:37:06 -07003678 e1000e_set_interrupt_capability(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003679
Bruce Allan4662e822008-08-26 18:37:06 -07003680 if (e1000_alloc_queues(adapter))
3681 return -ENOMEM;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003682
Auke Kokbc7f75f2007-09-17 12:30:59 -07003683 /* Explicitly disable IRQ since the NIC can be in any state. */
Auke Kokbc7f75f2007-09-17 12:30:59 -07003684 e1000_irq_disable(adapter);
3685
Auke Kokbc7f75f2007-09-17 12:30:59 -07003686 set_bit(__E1000_DOWN, &adapter->state);
3687 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003688}
3689
3690/**
Bruce Allanf8d59f72008-08-08 18:36:11 -07003691 * e1000_intr_msi_test - Interrupt Handler
3692 * @irq: interrupt number
3693 * @data: pointer to a network interface device structure
3694 **/
3695static irqreturn_t e1000_intr_msi_test(int irq, void *data)
3696{
3697 struct net_device *netdev = data;
3698 struct e1000_adapter *adapter = netdev_priv(netdev);
3699 struct e1000_hw *hw = &adapter->hw;
3700 u32 icr = er32(ICR);
3701
Bruce Allan3bb99fe2009-11-20 23:25:07 +00003702 e_dbg("icr is %08X\n", icr);
Bruce Allanf8d59f72008-08-08 18:36:11 -07003703 if (icr & E1000_ICR_RXSEQ) {
3704 adapter->flags &= ~FLAG_MSI_TEST_FAILED;
3705 wmb();
3706 }
3707
3708 return IRQ_HANDLED;
3709}
3710
3711/**
3712 * e1000_test_msi_interrupt - Returns 0 for successful test
3713 * @adapter: board private struct
3714 *
3715 * code flow taken from tg3.c
3716 **/
3717static int e1000_test_msi_interrupt(struct e1000_adapter *adapter)
3718{
3719 struct net_device *netdev = adapter->netdev;
3720 struct e1000_hw *hw = &adapter->hw;
3721 int err;
3722
3723 /* poll_enable hasn't been called yet, so don't need disable */
3724 /* clear any pending events */
3725 er32(ICR);
3726
3727 /* free the real vector and request a test handler */
3728 e1000_free_irq(adapter);
Bruce Allan4662e822008-08-26 18:37:06 -07003729 e1000e_reset_interrupt_capability(adapter);
Bruce Allanf8d59f72008-08-08 18:36:11 -07003730
3731 /* Assume that the test fails, if it succeeds then the test
3732 * MSI irq handler will unset this flag */
3733 adapter->flags |= FLAG_MSI_TEST_FAILED;
3734
3735 err = pci_enable_msi(adapter->pdev);
3736 if (err)
3737 goto msi_test_failed;
3738
Joe Perchesa0607fd2009-11-18 23:29:17 -08003739 err = request_irq(adapter->pdev->irq, e1000_intr_msi_test, 0,
Bruce Allanf8d59f72008-08-08 18:36:11 -07003740 netdev->name, netdev);
3741 if (err) {
3742 pci_disable_msi(adapter->pdev);
3743 goto msi_test_failed;
3744 }
3745
3746 wmb();
3747
3748 e1000_irq_enable(adapter);
3749
3750 /* fire an unusual interrupt on the test handler */
3751 ew32(ICS, E1000_ICS_RXSEQ);
3752 e1e_flush();
3753 msleep(50);
3754
3755 e1000_irq_disable(adapter);
3756
3757 rmb();
3758
3759 if (adapter->flags & FLAG_MSI_TEST_FAILED) {
Bruce Allan4662e822008-08-26 18:37:06 -07003760 adapter->int_mode = E1000E_INT_MODE_LEGACY;
Jean Delvare068e8a32010-09-12 22:45:39 +00003761 e_info("MSI interrupt test failed, using legacy interrupt.\n");
Bruce Allan24b706b2012-01-31 06:37:22 +00003762 } else {
Jean Delvare068e8a32010-09-12 22:45:39 +00003763 e_dbg("MSI interrupt test succeeded!\n");
Bruce Allan24b706b2012-01-31 06:37:22 +00003764 }
Bruce Allanf8d59f72008-08-08 18:36:11 -07003765
3766 free_irq(adapter->pdev->irq, netdev);
3767 pci_disable_msi(adapter->pdev);
3768
Bruce Allanf8d59f72008-08-08 18:36:11 -07003769msi_test_failed:
Bruce Allan4662e822008-08-26 18:37:06 -07003770 e1000e_set_interrupt_capability(adapter);
Jean Delvare068e8a32010-09-12 22:45:39 +00003771 return e1000_request_irq(adapter);
Bruce Allanf8d59f72008-08-08 18:36:11 -07003772}
3773
3774/**
3775 * e1000_test_msi - Returns 0 if MSI test succeeds or INTx mode is restored
3776 * @adapter: board private struct
3777 *
3778 * code flow taken from tg3.c, called with e1000 interrupts disabled.
3779 **/
3780static int e1000_test_msi(struct e1000_adapter *adapter)
3781{
3782 int err;
3783 u16 pci_cmd;
3784
3785 if (!(adapter->flags & FLAG_MSI_ENABLED))
3786 return 0;
3787
3788 /* disable SERR in case the MSI write causes a master abort */
3789 pci_read_config_word(adapter->pdev, PCI_COMMAND, &pci_cmd);
Dean Nelson36f24072010-06-29 18:12:05 +00003790 if (pci_cmd & PCI_COMMAND_SERR)
3791 pci_write_config_word(adapter->pdev, PCI_COMMAND,
3792 pci_cmd & ~PCI_COMMAND_SERR);
Bruce Allanf8d59f72008-08-08 18:36:11 -07003793
3794 err = e1000_test_msi_interrupt(adapter);
3795
Dean Nelson36f24072010-06-29 18:12:05 +00003796 /* re-enable SERR */
3797 if (pci_cmd & PCI_COMMAND_SERR) {
3798 pci_read_config_word(adapter->pdev, PCI_COMMAND, &pci_cmd);
3799 pci_cmd |= PCI_COMMAND_SERR;
3800 pci_write_config_word(adapter->pdev, PCI_COMMAND, pci_cmd);
3801 }
Bruce Allanf8d59f72008-08-08 18:36:11 -07003802
Bruce Allanf8d59f72008-08-08 18:36:11 -07003803 return err;
3804}
3805
3806/**
Auke Kokbc7f75f2007-09-17 12:30:59 -07003807 * e1000_open - Called when a network interface is made active
3808 * @netdev: network interface device structure
3809 *
3810 * Returns 0 on success, negative value on failure
3811 *
3812 * The open entry point is called when a network interface is made
3813 * active by the system (IFF_UP). At this point all resources needed
3814 * for transmit and receive operations are allocated, the interrupt
3815 * handler is registered with the OS, the watchdog timer is started,
3816 * and the stack is notified that the interface is ready.
3817 **/
3818static int e1000_open(struct net_device *netdev)
3819{
3820 struct e1000_adapter *adapter = netdev_priv(netdev);
3821 struct e1000_hw *hw = &adapter->hw;
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00003822 struct pci_dev *pdev = adapter->pdev;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003823 int err;
3824
3825 /* disallow open during test */
3826 if (test_bit(__E1000_TESTING, &adapter->state))
3827 return -EBUSY;
3828
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00003829 pm_runtime_get_sync(&pdev->dev);
3830
Jesse Brandeburg9c563d22009-04-17 20:44:34 +00003831 netif_carrier_off(netdev);
3832
Auke Kokbc7f75f2007-09-17 12:30:59 -07003833 /* allocate transmit descriptors */
Bruce Allan55aa6982011-12-16 00:45:45 +00003834 err = e1000e_setup_tx_resources(adapter->tx_ring);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003835 if (err)
3836 goto err_setup_tx;
3837
3838 /* allocate receive descriptors */
Bruce Allan55aa6982011-12-16 00:45:45 +00003839 err = e1000e_setup_rx_resources(adapter->rx_ring);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003840 if (err)
3841 goto err_setup_rx;
3842
Bruce Allan11b08be2010-05-10 14:59:31 +00003843 /*
3844 * If AMT is enabled, let the firmware know that the network
3845 * interface is now open and reset the part to a known state.
3846 */
3847 if (adapter->flags & FLAG_HAS_AMT) {
Bruce Allan31dbe5b2011-01-06 14:29:52 +00003848 e1000e_get_hw_control(adapter);
Bruce Allan11b08be2010-05-10 14:59:31 +00003849 e1000e_reset(adapter);
3850 }
3851
Auke Kokbc7f75f2007-09-17 12:30:59 -07003852 e1000e_power_up_phy(adapter);
3853
3854 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
3855 if ((adapter->hw.mng_cookie.status &
3856 E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
3857 e1000_update_mng_vlan(adapter);
3858
Bruce Allan79d4e902011-12-16 00:46:27 +00003859 /* DMA latency requirement to workaround jumbo issue */
3860 if (adapter->hw.mac.type == e1000_pch2lan)
Linus Torvalds6ba74012010-08-04 11:47:58 -07003861 pm_qos_add_request(&adapter->netdev->pm_qos_req,
3862 PM_QOS_CPU_DMA_LATENCY,
3863 PM_QOS_DEFAULT_VALUE);
Florian Micklerc128ec22010-08-02 14:27:00 +00003864
Bruce Allanad680762008-03-28 09:15:03 -07003865 /*
Bruce Allanad680762008-03-28 09:15:03 -07003866 * before we allocate an interrupt, we must be ready to handle it.
Auke Kokbc7f75f2007-09-17 12:30:59 -07003867 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
3868 * as soon as we call pci_request_irq, so we have to setup our
Bruce Allanad680762008-03-28 09:15:03 -07003869 * clean_rx handler before we do so.
3870 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07003871 e1000_configure(adapter);
3872
3873 err = e1000_request_irq(adapter);
3874 if (err)
3875 goto err_req_irq;
3876
Bruce Allanf8d59f72008-08-08 18:36:11 -07003877 /*
3878 * Work around PCIe errata with MSI interrupts causing some chipsets to
3879 * ignore e1000e MSI messages, which means we need to test our MSI
3880 * interrupt now
3881 */
Bruce Allan4662e822008-08-26 18:37:06 -07003882 if (adapter->int_mode != E1000E_INT_MODE_LEGACY) {
Bruce Allanf8d59f72008-08-08 18:36:11 -07003883 err = e1000_test_msi(adapter);
3884 if (err) {
3885 e_err("Interrupt allocation failed\n");
3886 goto err_req_irq;
3887 }
3888 }
3889
Auke Kokbc7f75f2007-09-17 12:30:59 -07003890 /* From here on the code is the same as e1000e_up() */
3891 clear_bit(__E1000_DOWN, &adapter->state);
3892
3893 napi_enable(&adapter->napi);
3894
3895 e1000_irq_enable(adapter);
3896
Jeff Kirsher09357b02011-11-18 14:25:00 +00003897 adapter->tx_hang_recheck = false;
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00003898 netif_start_queue(netdev);
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07003899
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00003900 adapter->idle_check = true;
3901 pm_runtime_put(&pdev->dev);
3902
Auke Kokbc7f75f2007-09-17 12:30:59 -07003903 /* fire a link status change interrupt to start the watchdog */
Bruce Allan52a9b232010-05-10 15:00:10 +00003904 if (adapter->msix_entries)
3905 ew32(ICS, E1000_ICS_LSC | E1000_ICR_OTHER);
3906 else
3907 ew32(ICS, E1000_ICS_LSC);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003908
3909 return 0;
3910
3911err_req_irq:
Bruce Allan31dbe5b2011-01-06 14:29:52 +00003912 e1000e_release_hw_control(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003913 e1000_power_down_phy(adapter);
Bruce Allan55aa6982011-12-16 00:45:45 +00003914 e1000e_free_rx_resources(adapter->rx_ring);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003915err_setup_rx:
Bruce Allan55aa6982011-12-16 00:45:45 +00003916 e1000e_free_tx_resources(adapter->tx_ring);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003917err_setup_tx:
3918 e1000e_reset(adapter);
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00003919 pm_runtime_put_sync(&pdev->dev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003920
3921 return err;
3922}
3923
3924/**
3925 * e1000_close - Disables a network interface
3926 * @netdev: network interface device structure
3927 *
3928 * Returns 0, this is not allowed to fail
3929 *
3930 * The close entry point is called when an interface is de-activated
3931 * by the OS. The hardware is still under the drivers control, but
3932 * needs to be disabled. A global MAC reset is issued to stop the
3933 * hardware, and all transmit and receive resources are freed.
3934 **/
3935static int e1000_close(struct net_device *netdev)
3936{
3937 struct e1000_adapter *adapter = netdev_priv(netdev);
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00003938 struct pci_dev *pdev = adapter->pdev;
Auke Kokbc7f75f2007-09-17 12:30:59 -07003939
3940 WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00003941
3942 pm_runtime_get_sync(&pdev->dev);
3943
Bruce Allan5f4a7802011-11-29 08:09:19 +00003944 napi_disable(&adapter->napi);
3945
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00003946 if (!test_bit(__E1000_DOWN, &adapter->state)) {
3947 e1000e_down(adapter);
3948 e1000_free_irq(adapter);
3949 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07003950 e1000_power_down_phy(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003951
Bruce Allan55aa6982011-12-16 00:45:45 +00003952 e1000e_free_tx_resources(adapter->tx_ring);
3953 e1000e_free_rx_resources(adapter->rx_ring);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003954
Bruce Allanad680762008-03-28 09:15:03 -07003955 /*
3956 * kill manageability vlan ID if supported, but not if a vlan with
3957 * the same ID is registered on the host OS (let 8021q kill it)
3958 */
Jeff Kirsher86d70e52011-03-25 16:01:01 +00003959 if (adapter->hw.mng_cookie.status &
3960 E1000_MNG_DHCP_COOKIE_STATUS_VLAN)
Auke Kokbc7f75f2007-09-17 12:30:59 -07003961 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
3962
Bruce Allanad680762008-03-28 09:15:03 -07003963 /*
3964 * If AMT is enabled, let the firmware know that the network
3965 * interface is now closed
3966 */
Bruce Allan31dbe5b2011-01-06 14:29:52 +00003967 if ((adapter->flags & FLAG_HAS_AMT) &&
3968 !test_bit(__E1000_TESTING, &adapter->state))
3969 e1000e_release_hw_control(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07003970
Bruce Allan79d4e902011-12-16 00:46:27 +00003971 if (adapter->hw.mac.type == e1000_pch2lan)
Linus Torvalds6ba74012010-08-04 11:47:58 -07003972 pm_qos_remove_request(&adapter->netdev->pm_qos_req);
Florian Micklerc128ec22010-08-02 14:27:00 +00003973
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00003974 pm_runtime_put_sync(&pdev->dev);
3975
Auke Kokbc7f75f2007-09-17 12:30:59 -07003976 return 0;
3977}
3978/**
3979 * e1000_set_mac - Change the Ethernet Address of the NIC
3980 * @netdev: network interface device structure
3981 * @p: pointer to an address structure
3982 *
3983 * Returns 0 on success, negative on failure
3984 **/
3985static int e1000_set_mac(struct net_device *netdev, void *p)
3986{
3987 struct e1000_adapter *adapter = netdev_priv(netdev);
3988 struct sockaddr *addr = p;
3989
3990 if (!is_valid_ether_addr(addr->sa_data))
3991 return -EADDRNOTAVAIL;
3992
3993 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3994 memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
3995
3996 e1000e_rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
3997
3998 if (adapter->flags & FLAG_RESET_OVERWRITES_LAA) {
3999 /* activate the work around */
4000 e1000e_set_laa_state_82571(&adapter->hw, 1);
4001
Bruce Allanad680762008-03-28 09:15:03 -07004002 /*
4003 * Hold a copy of the LAA in RAR[14] This is done so that
Auke Kokbc7f75f2007-09-17 12:30:59 -07004004 * between the time RAR[0] gets clobbered and the time it
4005 * gets fixed (in e1000_watchdog), the actual LAA is in one
4006 * of the RARs and no incoming packets directed to this port
4007 * are dropped. Eventually the LAA will be in RAR[0] and
Bruce Allanad680762008-03-28 09:15:03 -07004008 * RAR[14]
4009 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004010 e1000e_rar_set(&adapter->hw,
4011 adapter->hw.mac.addr,
4012 adapter->hw.mac.rar_entry_count - 1);
4013 }
4014
4015 return 0;
4016}
4017
Jesse Brandeburga8f88ff2008-10-02 16:33:25 -07004018/**
4019 * e1000e_update_phy_task - work thread to update phy
4020 * @work: pointer to our work struct
4021 *
4022 * this worker thread exists because we must acquire a
4023 * semaphore to read the phy, which we could msleep while
4024 * waiting for it, and we can't msleep in a timer.
4025 **/
4026static void e1000e_update_phy_task(struct work_struct *work)
4027{
4028 struct e1000_adapter *adapter = container_of(work,
4029 struct e1000_adapter, update_phy_task);
Jesse Brandeburg615b32a2011-02-02 10:19:45 +00004030
4031 if (test_bit(__E1000_DOWN, &adapter->state))
4032 return;
4033
Jesse Brandeburga8f88ff2008-10-02 16:33:25 -07004034 e1000_get_phy_info(&adapter->hw);
4035}
4036
Bruce Allanad680762008-03-28 09:15:03 -07004037/*
4038 * Need to wait a few seconds after link up to get diagnostic information from
4039 * the phy
4040 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004041static void e1000_update_phy_info(unsigned long data)
4042{
4043 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
Jesse Brandeburg615b32a2011-02-02 10:19:45 +00004044
4045 if (test_bit(__E1000_DOWN, &adapter->state))
4046 return;
4047
Jesse Brandeburga8f88ff2008-10-02 16:33:25 -07004048 schedule_work(&adapter->update_phy_task);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004049}
4050
4051/**
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004052 * e1000e_update_phy_stats - Update the PHY statistics counters
4053 * @adapter: board private structure
Bruce Allan2b6b1682011-05-13 07:20:09 +00004054 *
4055 * Read/clear the upper 16-bit PHY registers and read/accumulate lower
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004056 **/
4057static void e1000e_update_phy_stats(struct e1000_adapter *adapter)
4058{
4059 struct e1000_hw *hw = &adapter->hw;
4060 s32 ret_val;
4061 u16 phy_data;
4062
4063 ret_val = hw->phy.ops.acquire(hw);
4064 if (ret_val)
4065 return;
4066
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004067 /*
4068 * A page set is expensive so check if already on desired page.
4069 * If not, set to the page with the PHY status registers.
4070 */
Bruce Allan2b6b1682011-05-13 07:20:09 +00004071 hw->phy.addr = 1;
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004072 ret_val = e1000e_read_phy_reg_mdic(hw, IGP01E1000_PHY_PAGE_SELECT,
4073 &phy_data);
4074 if (ret_val)
4075 goto release;
Bruce Allan2b6b1682011-05-13 07:20:09 +00004076 if (phy_data != (HV_STATS_PAGE << IGP_PAGE_SHIFT)) {
4077 ret_val = hw->phy.ops.set_page(hw,
4078 HV_STATS_PAGE << IGP_PAGE_SHIFT);
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004079 if (ret_val)
4080 goto release;
4081 }
4082
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004083 /* Single Collision Count */
Bruce Allan2b6b1682011-05-13 07:20:09 +00004084 hw->phy.ops.read_reg_page(hw, HV_SCC_UPPER, &phy_data);
4085 ret_val = hw->phy.ops.read_reg_page(hw, HV_SCC_LOWER, &phy_data);
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004086 if (!ret_val)
4087 adapter->stats.scc += phy_data;
4088
4089 /* Excessive Collision Count */
Bruce Allan2b6b1682011-05-13 07:20:09 +00004090 hw->phy.ops.read_reg_page(hw, HV_ECOL_UPPER, &phy_data);
4091 ret_val = hw->phy.ops.read_reg_page(hw, HV_ECOL_LOWER, &phy_data);
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004092 if (!ret_val)
4093 adapter->stats.ecol += phy_data;
4094
4095 /* Multiple Collision Count */
Bruce Allan2b6b1682011-05-13 07:20:09 +00004096 hw->phy.ops.read_reg_page(hw, HV_MCC_UPPER, &phy_data);
4097 ret_val = hw->phy.ops.read_reg_page(hw, HV_MCC_LOWER, &phy_data);
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004098 if (!ret_val)
4099 adapter->stats.mcc += phy_data;
4100
4101 /* Late Collision Count */
Bruce Allan2b6b1682011-05-13 07:20:09 +00004102 hw->phy.ops.read_reg_page(hw, HV_LATECOL_UPPER, &phy_data);
4103 ret_val = hw->phy.ops.read_reg_page(hw, HV_LATECOL_LOWER, &phy_data);
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004104 if (!ret_val)
4105 adapter->stats.latecol += phy_data;
4106
4107 /* Collision Count - also used for adaptive IFS */
Bruce Allan2b6b1682011-05-13 07:20:09 +00004108 hw->phy.ops.read_reg_page(hw, HV_COLC_UPPER, &phy_data);
4109 ret_val = hw->phy.ops.read_reg_page(hw, HV_COLC_LOWER, &phy_data);
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004110 if (!ret_val)
4111 hw->mac.collision_delta = phy_data;
4112
4113 /* Defer Count */
Bruce Allan2b6b1682011-05-13 07:20:09 +00004114 hw->phy.ops.read_reg_page(hw, HV_DC_UPPER, &phy_data);
4115 ret_val = hw->phy.ops.read_reg_page(hw, HV_DC_LOWER, &phy_data);
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004116 if (!ret_val)
4117 adapter->stats.dc += phy_data;
4118
4119 /* Transmit with no CRS */
Bruce Allan2b6b1682011-05-13 07:20:09 +00004120 hw->phy.ops.read_reg_page(hw, HV_TNCRS_UPPER, &phy_data);
4121 ret_val = hw->phy.ops.read_reg_page(hw, HV_TNCRS_LOWER, &phy_data);
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004122 if (!ret_val)
4123 adapter->stats.tncrs += phy_data;
4124
4125release:
4126 hw->phy.ops.release(hw);
4127}
4128
4129/**
Auke Kokbc7f75f2007-09-17 12:30:59 -07004130 * e1000e_update_stats - Update the board statistics counters
4131 * @adapter: board private structure
4132 **/
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00004133static void e1000e_update_stats(struct e1000_adapter *adapter)
Auke Kokbc7f75f2007-09-17 12:30:59 -07004134{
Ajit Khaparde7274c202009-10-07 02:44:26 +00004135 struct net_device *netdev = adapter->netdev;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004136 struct e1000_hw *hw = &adapter->hw;
4137 struct pci_dev *pdev = adapter->pdev;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004138
4139 /*
4140 * Prevent stats update while adapter is being reset, or if the pci
4141 * connection is down.
4142 */
4143 if (adapter->link_speed == 0)
4144 return;
4145 if (pci_channel_offline(pdev))
4146 return;
4147
Auke Kokbc7f75f2007-09-17 12:30:59 -07004148 adapter->stats.crcerrs += er32(CRCERRS);
4149 adapter->stats.gprc += er32(GPRC);
Bruce Allan7c257692008-04-23 11:09:00 -07004150 adapter->stats.gorc += er32(GORCL);
4151 er32(GORCH); /* Clear gorc */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004152 adapter->stats.bprc += er32(BPRC);
4153 adapter->stats.mprc += er32(MPRC);
4154 adapter->stats.roc += er32(ROC);
4155
Auke Kokbc7f75f2007-09-17 12:30:59 -07004156 adapter->stats.mpc += er32(MPC);
Bruce Allana4f58f52009-06-02 11:29:18 +00004157
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004158 /* Half-duplex statistics */
4159 if (adapter->link_duplex == HALF_DUPLEX) {
4160 if (adapter->flags2 & FLAG2_HAS_PHY_STATS) {
4161 e1000e_update_phy_stats(adapter);
4162 } else {
4163 adapter->stats.scc += er32(SCC);
4164 adapter->stats.ecol += er32(ECOL);
4165 adapter->stats.mcc += er32(MCC);
4166 adapter->stats.latecol += er32(LATECOL);
4167 adapter->stats.dc += er32(DC);
Bruce Allana4f58f52009-06-02 11:29:18 +00004168
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004169 hw->mac.collision_delta = er32(COLC);
Bruce Allana4f58f52009-06-02 11:29:18 +00004170
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004171 if ((hw->mac.type != e1000_82574) &&
4172 (hw->mac.type != e1000_82583))
4173 adapter->stats.tncrs += er32(TNCRS);
4174 }
4175 adapter->stats.colc += hw->mac.collision_delta;
Bruce Allana4f58f52009-06-02 11:29:18 +00004176 }
Bruce Allan8c7bbb92010-06-16 13:26:41 +00004177
Auke Kokbc7f75f2007-09-17 12:30:59 -07004178 adapter->stats.xonrxc += er32(XONRXC);
4179 adapter->stats.xontxc += er32(XONTXC);
4180 adapter->stats.xoffrxc += er32(XOFFRXC);
4181 adapter->stats.xofftxc += er32(XOFFTXC);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004182 adapter->stats.gptc += er32(GPTC);
Bruce Allan7c257692008-04-23 11:09:00 -07004183 adapter->stats.gotc += er32(GOTCL);
4184 er32(GOTCH); /* Clear gotc */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004185 adapter->stats.rnbc += er32(RNBC);
4186 adapter->stats.ruc += er32(RUC);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004187
4188 adapter->stats.mptc += er32(MPTC);
4189 adapter->stats.bptc += er32(BPTC);
4190
4191 /* used for adaptive IFS */
4192
4193 hw->mac.tx_packet_delta = er32(TPT);
4194 adapter->stats.tpt += hw->mac.tx_packet_delta;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004195
4196 adapter->stats.algnerrc += er32(ALGNERRC);
4197 adapter->stats.rxerrc += er32(RXERRC);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004198 adapter->stats.cexterr += er32(CEXTERR);
4199 adapter->stats.tsctc += er32(TSCTC);
4200 adapter->stats.tsctfc += er32(TSCTFC);
4201
Auke Kokbc7f75f2007-09-17 12:30:59 -07004202 /* Fill out the OS statistics structure */
Ajit Khaparde7274c202009-10-07 02:44:26 +00004203 netdev->stats.multicast = adapter->stats.mprc;
4204 netdev->stats.collisions = adapter->stats.colc;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004205
4206 /* Rx Errors */
4207
Bruce Allanad680762008-03-28 09:15:03 -07004208 /*
4209 * RLEC on some newer hardware can be incorrect so build
4210 * our own version based on RUC and ROC
4211 */
Ajit Khaparde7274c202009-10-07 02:44:26 +00004212 netdev->stats.rx_errors = adapter->stats.rxerrc +
Auke Kokbc7f75f2007-09-17 12:30:59 -07004213 adapter->stats.crcerrs + adapter->stats.algnerrc +
4214 adapter->stats.ruc + adapter->stats.roc +
4215 adapter->stats.cexterr;
Ajit Khaparde7274c202009-10-07 02:44:26 +00004216 netdev->stats.rx_length_errors = adapter->stats.ruc +
Auke Kokbc7f75f2007-09-17 12:30:59 -07004217 adapter->stats.roc;
Ajit Khaparde7274c202009-10-07 02:44:26 +00004218 netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
4219 netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
4220 netdev->stats.rx_missed_errors = adapter->stats.mpc;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004221
4222 /* Tx Errors */
Ajit Khaparde7274c202009-10-07 02:44:26 +00004223 netdev->stats.tx_errors = adapter->stats.ecol +
Auke Kokbc7f75f2007-09-17 12:30:59 -07004224 adapter->stats.latecol;
Ajit Khaparde7274c202009-10-07 02:44:26 +00004225 netdev->stats.tx_aborted_errors = adapter->stats.ecol;
4226 netdev->stats.tx_window_errors = adapter->stats.latecol;
4227 netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004228
4229 /* Tx Dropped needs to be maintained elsewhere */
4230
Auke Kokbc7f75f2007-09-17 12:30:59 -07004231 /* Management Stats */
4232 adapter->stats.mgptc += er32(MGTPTC);
4233 adapter->stats.mgprc += er32(MGTPRC);
4234 adapter->stats.mgpdc += er32(MGTPDC);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004235}
4236
Bruce Allan7c257692008-04-23 11:09:00 -07004237/**
4238 * e1000_phy_read_status - Update the PHY register status snapshot
4239 * @adapter: board private structure
4240 **/
4241static void e1000_phy_read_status(struct e1000_adapter *adapter)
4242{
4243 struct e1000_hw *hw = &adapter->hw;
4244 struct e1000_phy_regs *phy = &adapter->phy_regs;
Bruce Allan7c257692008-04-23 11:09:00 -07004245
4246 if ((er32(STATUS) & E1000_STATUS_LU) &&
4247 (adapter->hw.phy.media_type == e1000_media_type_copper)) {
Bruce Allan90da0662011-01-06 07:02:53 +00004248 int ret_val;
4249
Bruce Allan7c257692008-04-23 11:09:00 -07004250 ret_val = e1e_rphy(hw, PHY_CONTROL, &phy->bmcr);
4251 ret_val |= e1e_rphy(hw, PHY_STATUS, &phy->bmsr);
4252 ret_val |= e1e_rphy(hw, PHY_AUTONEG_ADV, &phy->advertise);
4253 ret_val |= e1e_rphy(hw, PHY_LP_ABILITY, &phy->lpa);
4254 ret_val |= e1e_rphy(hw, PHY_AUTONEG_EXP, &phy->expansion);
4255 ret_val |= e1e_rphy(hw, PHY_1000T_CTRL, &phy->ctrl1000);
4256 ret_val |= e1e_rphy(hw, PHY_1000T_STATUS, &phy->stat1000);
4257 ret_val |= e1e_rphy(hw, PHY_EXT_STATUS, &phy->estatus);
4258 if (ret_val)
Jeff Kirsher44defeb2008-08-04 17:20:41 -07004259 e_warn("Error reading PHY register\n");
Bruce Allan7c257692008-04-23 11:09:00 -07004260 } else {
4261 /*
4262 * Do not read PHY registers if link is not up
4263 * Set values to typical power-on defaults
4264 */
4265 phy->bmcr = (BMCR_SPEED1000 | BMCR_ANENABLE | BMCR_FULLDPLX);
4266 phy->bmsr = (BMSR_100FULL | BMSR_100HALF | BMSR_10FULL |
4267 BMSR_10HALF | BMSR_ESTATEN | BMSR_ANEGCAPABLE |
4268 BMSR_ERCAP);
4269 phy->advertise = (ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP |
4270 ADVERTISE_ALL | ADVERTISE_CSMA);
4271 phy->lpa = 0;
4272 phy->expansion = EXPANSION_ENABLENPAGE;
4273 phy->ctrl1000 = ADVERTISE_1000FULL;
4274 phy->stat1000 = 0;
4275 phy->estatus = (ESTATUS_1000_TFULL | ESTATUS_1000_THALF);
4276 }
Bruce Allan7c257692008-04-23 11:09:00 -07004277}
4278
Auke Kokbc7f75f2007-09-17 12:30:59 -07004279static void e1000_print_link_info(struct e1000_adapter *adapter)
4280{
Auke Kokbc7f75f2007-09-17 12:30:59 -07004281 struct e1000_hw *hw = &adapter->hw;
4282 u32 ctrl = er32(CTRL);
4283
Bruce Allan8f12fe82008-11-21 16:54:43 -08004284 /* Link status message must follow this format for user tools */
Jeff Kirsheref456f82011-11-03 11:40:28 +00004285 printk(KERN_INFO "e1000e: %s NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
4286 adapter->netdev->name,
4287 adapter->link_speed,
4288 adapter->link_duplex == FULL_DUPLEX ? "Full" : "Half",
4289 (ctrl & E1000_CTRL_TFCE) && (ctrl & E1000_CTRL_RFCE) ? "Rx/Tx" :
4290 (ctrl & E1000_CTRL_RFCE) ? "Rx" :
4291 (ctrl & E1000_CTRL_TFCE) ? "Tx" : "None");
Auke Kokbc7f75f2007-09-17 12:30:59 -07004292}
4293
Bruce Allan0c6bdb32010-06-17 18:58:43 +00004294static bool e1000e_has_link(struct e1000_adapter *adapter)
Jeff Kirsher318a94d2008-03-28 09:15:16 -07004295{
4296 struct e1000_hw *hw = &adapter->hw;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004297 bool link_active = false;
Jeff Kirsher318a94d2008-03-28 09:15:16 -07004298 s32 ret_val = 0;
4299
4300 /*
4301 * get_link_status is set on LSC (link status) interrupt or
4302 * Rx sequence error interrupt. get_link_status will stay
4303 * false until the check_for_link establishes link
4304 * for copper adapters ONLY
4305 */
4306 switch (hw->phy.media_type) {
4307 case e1000_media_type_copper:
4308 if (hw->mac.get_link_status) {
4309 ret_val = hw->mac.ops.check_for_link(hw);
4310 link_active = !hw->mac.get_link_status;
4311 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004312 link_active = true;
Jeff Kirsher318a94d2008-03-28 09:15:16 -07004313 }
4314 break;
4315 case e1000_media_type_fiber:
4316 ret_val = hw->mac.ops.check_for_link(hw);
4317 link_active = !!(er32(STATUS) & E1000_STATUS_LU);
4318 break;
4319 case e1000_media_type_internal_serdes:
4320 ret_val = hw->mac.ops.check_for_link(hw);
4321 link_active = adapter->hw.mac.serdes_has_link;
4322 break;
4323 default:
4324 case e1000_media_type_unknown:
4325 break;
4326 }
4327
4328 if ((ret_val == E1000_ERR_PHY) && (hw->phy.type == e1000_phy_igp_3) &&
4329 (er32(CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
4330 /* See e1000_kmrn_lock_loss_workaround_ich8lan() */
Jeff Kirsher44defeb2008-08-04 17:20:41 -07004331 e_info("Gigabit has been disabled, downgrading speed\n");
Jeff Kirsher318a94d2008-03-28 09:15:16 -07004332 }
4333
4334 return link_active;
4335}
4336
4337static void e1000e_enable_receives(struct e1000_adapter *adapter)
4338{
4339 /* make sure the receive unit is started */
4340 if ((adapter->flags & FLAG_RX_NEEDS_RESTART) &&
4341 (adapter->flags & FLAG_RX_RESTART_NOW)) {
4342 struct e1000_hw *hw = &adapter->hw;
4343 u32 rctl = er32(RCTL);
4344 ew32(RCTL, rctl | E1000_RCTL_EN);
4345 adapter->flags &= ~FLAG_RX_RESTART_NOW;
4346 }
4347}
4348
Carolyn Wybornyff10e132010-10-28 00:59:53 +00004349static void e1000e_check_82574_phy_workaround(struct e1000_adapter *adapter)
4350{
4351 struct e1000_hw *hw = &adapter->hw;
4352
4353 /*
4354 * With 82574 controllers, PHY needs to be checked periodically
4355 * for hung state and reset, if two calls return true
4356 */
4357 if (e1000_check_phy_82574(hw))
4358 adapter->phy_hang_count++;
4359 else
4360 adapter->phy_hang_count = 0;
4361
4362 if (adapter->phy_hang_count > 1) {
4363 adapter->phy_hang_count = 0;
4364 schedule_work(&adapter->reset_task);
4365 }
4366}
4367
Auke Kokbc7f75f2007-09-17 12:30:59 -07004368/**
4369 * e1000_watchdog - Timer Call-back
4370 * @data: pointer to adapter cast into an unsigned long
4371 **/
4372static void e1000_watchdog(unsigned long data)
4373{
4374 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
4375
4376 /* Do the rest outside of interrupt context */
4377 schedule_work(&adapter->watchdog_task);
4378
4379 /* TODO: make this use queue_delayed_work() */
4380}
4381
4382static void e1000_watchdog_task(struct work_struct *work)
4383{
4384 struct e1000_adapter *adapter = container_of(work,
4385 struct e1000_adapter, watchdog_task);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004386 struct net_device *netdev = adapter->netdev;
4387 struct e1000_mac_info *mac = &adapter->hw.mac;
Bruce Allan75eb0fa2008-11-21 16:53:51 -08004388 struct e1000_phy_info *phy = &adapter->hw.phy;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004389 struct e1000_ring *tx_ring = adapter->tx_ring;
4390 struct e1000_hw *hw = &adapter->hw;
4391 u32 link, tctl;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004392
Jesse Brandeburg615b32a2011-02-02 10:19:45 +00004393 if (test_bit(__E1000_DOWN, &adapter->state))
4394 return;
4395
David S. Millerb405e8d2010-02-04 22:31:41 -08004396 link = e1000e_has_link(adapter);
Jeff Kirsher318a94d2008-03-28 09:15:16 -07004397 if ((netif_carrier_ok(netdev)) && link) {
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00004398 /* Cancel scheduled suspend requests. */
4399 pm_runtime_resume(netdev->dev.parent);
4400
Jeff Kirsher318a94d2008-03-28 09:15:16 -07004401 e1000e_enable_receives(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004402 goto link_up;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004403 }
4404
4405 if ((e1000e_enable_tx_pkt_filtering(hw)) &&
4406 (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id))
4407 e1000_update_mng_vlan(adapter);
4408
Auke Kokbc7f75f2007-09-17 12:30:59 -07004409 if (link) {
4410 if (!netif_carrier_ok(netdev)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004411 bool txb2b = true;
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00004412
4413 /* Cancel scheduled suspend requests. */
4414 pm_runtime_resume(netdev->dev.parent);
4415
Jeff Kirsher318a94d2008-03-28 09:15:16 -07004416 /* update snapshot of PHY registers on LSC */
Bruce Allan7c257692008-04-23 11:09:00 -07004417 e1000_phy_read_status(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004418 mac->ops.get_link_up_info(&adapter->hw,
4419 &adapter->link_speed,
4420 &adapter->link_duplex);
4421 e1000_print_link_info(adapter);
Bruce Allanad680762008-03-28 09:15:03 -07004422 /*
Bruce Allanf4187b52008-08-26 18:36:50 -07004423 * On supported PHYs, check for duplex mismatch only
4424 * if link has autonegotiated at 10/100 half
4425 */
4426 if ((hw->phy.type == e1000_phy_igp_3 ||
4427 hw->phy.type == e1000_phy_bm) &&
4428 (hw->mac.autoneg == true) &&
4429 (adapter->link_speed == SPEED_10 ||
4430 adapter->link_speed == SPEED_100) &&
4431 (adapter->link_duplex == HALF_DUPLEX)) {
4432 u16 autoneg_exp;
4433
4434 e1e_rphy(hw, PHY_AUTONEG_EXP, &autoneg_exp);
4435
4436 if (!(autoneg_exp & NWAY_ER_LP_NWAY_CAPS))
Jeff Kirsheref456f82011-11-03 11:40:28 +00004437 e_info("Autonegotiated half duplex but link partner cannot autoneg. Try forcing full duplex if link gets many collisions.\n");
Bruce Allanf4187b52008-08-26 18:36:50 -07004438 }
4439
Emil Tantilovf49c57e2010-03-24 12:55:02 +00004440 /* adjust timeout factor according to speed/duplex */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004441 adapter->tx_timeout_factor = 1;
4442 switch (adapter->link_speed) {
4443 case SPEED_10:
Rusty Russell3db1cd52011-12-19 13:56:45 +00004444 txb2b = false;
Bruce Allan10f1b492008-08-08 18:36:01 -07004445 adapter->tx_timeout_factor = 16;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004446 break;
4447 case SPEED_100:
Rusty Russell3db1cd52011-12-19 13:56:45 +00004448 txb2b = false;
Bruce Allan4c86e0b2009-11-19 12:35:26 +00004449 adapter->tx_timeout_factor = 10;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004450 break;
4451 }
4452
Bruce Allanad680762008-03-28 09:15:03 -07004453 /*
4454 * workaround: re-program speed mode bit after
4455 * link-up event
4456 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004457 if ((adapter->flags & FLAG_TARC_SPEED_MODE_BIT) &&
4458 !txb2b) {
4459 u32 tarc0;
Jeff Kirshere9ec2c02008-04-02 13:48:13 -07004460 tarc0 = er32(TARC(0));
Auke Kokbc7f75f2007-09-17 12:30:59 -07004461 tarc0 &= ~SPEED_MODE_BIT;
Jeff Kirshere9ec2c02008-04-02 13:48:13 -07004462 ew32(TARC(0), tarc0);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004463 }
4464
Bruce Allanad680762008-03-28 09:15:03 -07004465 /*
4466 * disable TSO for pcie and 10/100 speeds, to avoid
4467 * some hardware issues
4468 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004469 if (!(adapter->flags & FLAG_TSO_FORCE)) {
4470 switch (adapter->link_speed) {
4471 case SPEED_10:
4472 case SPEED_100:
Jeff Kirsher44defeb2008-08-04 17:20:41 -07004473 e_info("10/100 speed: disabling TSO\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07004474 netdev->features &= ~NETIF_F_TSO;
4475 netdev->features &= ~NETIF_F_TSO6;
4476 break;
4477 case SPEED_1000:
4478 netdev->features |= NETIF_F_TSO;
4479 netdev->features |= NETIF_F_TSO6;
4480 break;
4481 default:
4482 /* oops */
4483 break;
4484 }
4485 }
4486
Bruce Allanad680762008-03-28 09:15:03 -07004487 /*
4488 * enable transmits in the hardware, need to do this
4489 * after setting TARC(0)
4490 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004491 tctl = er32(TCTL);
4492 tctl |= E1000_TCTL_EN;
4493 ew32(TCTL, tctl);
4494
Bruce Allan75eb0fa2008-11-21 16:53:51 -08004495 /*
4496 * Perform any post-link-up configuration before
4497 * reporting link up.
4498 */
4499 if (phy->ops.cfg_on_link_up)
4500 phy->ops.cfg_on_link_up(hw);
4501
Auke Kokbc7f75f2007-09-17 12:30:59 -07004502 netif_carrier_on(netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004503
4504 if (!test_bit(__E1000_DOWN, &adapter->state))
4505 mod_timer(&adapter->phy_info_timer,
4506 round_jiffies(jiffies + 2 * HZ));
Auke Kokbc7f75f2007-09-17 12:30:59 -07004507 }
4508 } else {
4509 if (netif_carrier_ok(netdev)) {
4510 adapter->link_speed = 0;
4511 adapter->link_duplex = 0;
Bruce Allan8f12fe82008-11-21 16:54:43 -08004512 /* Link status message must follow this format */
4513 printk(KERN_INFO "e1000e: %s NIC Link is Down\n",
4514 adapter->netdev->name);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004515 netif_carrier_off(netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004516 if (!test_bit(__E1000_DOWN, &adapter->state))
4517 mod_timer(&adapter->phy_info_timer,
4518 round_jiffies(jiffies + 2 * HZ));
4519
4520 if (adapter->flags & FLAG_RX_NEEDS_RESTART)
4521 schedule_work(&adapter->reset_task);
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00004522 else
4523 pm_schedule_suspend(netdev->dev.parent,
4524 LINK_TIMEOUT);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004525 }
4526 }
4527
4528link_up:
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00004529 spin_lock(&adapter->stats64_lock);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004530 e1000e_update_stats(adapter);
4531
4532 mac->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
4533 adapter->tpt_old = adapter->stats.tpt;
4534 mac->collision_delta = adapter->stats.colc - adapter->colc_old;
4535 adapter->colc_old = adapter->stats.colc;
4536
Bruce Allan7c257692008-04-23 11:09:00 -07004537 adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
4538 adapter->gorc_old = adapter->stats.gorc;
4539 adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
4540 adapter->gotc_old = adapter->stats.gotc;
Flavio Leitner2084b112011-04-05 04:27:43 +00004541 spin_unlock(&adapter->stats64_lock);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004542
4543 e1000e_update_adaptive(&adapter->hw);
4544
Bruce Allan90da0662011-01-06 07:02:53 +00004545 if (!netif_carrier_ok(netdev) &&
4546 (e1000_desc_unused(tx_ring) + 1 < tx_ring->count)) {
4547 /*
4548 * We've lost link, so the controller stops DMA,
4549 * but we've got queued Tx work that's never going
4550 * to get done, so reset controller to flush Tx.
4551 * (Do the reset outside of interrupt context).
4552 */
Bruce Allan90da0662011-01-06 07:02:53 +00004553 schedule_work(&adapter->reset_task);
4554 /* return immediately since reset is imminent */
4555 return;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004556 }
4557
Jesse Brandeburgeab2abf2010-05-04 22:26:03 +00004558 /* Simple mode for Interrupt Throttle Rate (ITR) */
4559 if (adapter->itr_setting == 4) {
4560 /*
4561 * Symmetric Tx/Rx gets a reduced ITR=2000;
4562 * Total asymmetrical Tx or Rx gets ITR=8000;
4563 * everyone else is between 2000-8000.
4564 */
4565 u32 goc = (adapter->gotc + adapter->gorc) / 10000;
4566 u32 dif = (adapter->gotc > adapter->gorc ?
4567 adapter->gotc - adapter->gorc :
4568 adapter->gorc - adapter->gotc) / 10000;
4569 u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
4570
4571 ew32(ITR, 1000000000 / (itr * 256));
4572 }
4573
Bruce Allanad680762008-03-28 09:15:03 -07004574 /* Cause software interrupt to ensure Rx ring is cleaned */
Bruce Allan4662e822008-08-26 18:37:06 -07004575 if (adapter->msix_entries)
4576 ew32(ICS, adapter->rx_ring->ims_val);
4577 else
4578 ew32(ICS, E1000_ICS_RXDMT0);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004579
Jesse Brandeburg713b3c92011-02-02 10:19:50 +00004580 /* flush pending descriptors to memory before detecting Tx hang */
4581 e1000e_flush_descriptors(adapter);
4582
Auke Kokbc7f75f2007-09-17 12:30:59 -07004583 /* Force detection of hung controller every watchdog period */
Rusty Russell3db1cd52011-12-19 13:56:45 +00004584 adapter->detect_tx_hung = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004585
Bruce Allanad680762008-03-28 09:15:03 -07004586 /*
4587 * With 82571 controllers, LAA may be overwritten due to controller
4588 * reset from the other port. Set the appropriate LAA in RAR[0]
4589 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004590 if (e1000e_get_laa_state_82571(hw))
4591 e1000e_rar_set(hw, adapter->hw.mac.addr, 0);
4592
Carolyn Wybornyff10e132010-10-28 00:59:53 +00004593 if (adapter->flags2 & FLAG2_CHECK_PHY_HANG)
4594 e1000e_check_82574_phy_workaround(adapter);
4595
Auke Kokbc7f75f2007-09-17 12:30:59 -07004596 /* Reset the timer */
4597 if (!test_bit(__E1000_DOWN, &adapter->state))
4598 mod_timer(&adapter->watchdog_timer,
4599 round_jiffies(jiffies + 2 * HZ));
4600}
4601
4602#define E1000_TX_FLAGS_CSUM 0x00000001
4603#define E1000_TX_FLAGS_VLAN 0x00000002
4604#define E1000_TX_FLAGS_TSO 0x00000004
4605#define E1000_TX_FLAGS_IPV4 0x00000008
4606#define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
4607#define E1000_TX_FLAGS_VLAN_SHIFT 16
4608
Bruce Allan55aa6982011-12-16 00:45:45 +00004609static int e1000_tso(struct e1000_ring *tx_ring, struct sk_buff *skb)
Auke Kokbc7f75f2007-09-17 12:30:59 -07004610{
Auke Kokbc7f75f2007-09-17 12:30:59 -07004611 struct e1000_context_desc *context_desc;
4612 struct e1000_buffer *buffer_info;
4613 unsigned int i;
4614 u32 cmd_length = 0;
4615 u16 ipcse = 0, tucse, mss;
4616 u8 ipcss, ipcso, tucss, tucso, hdr_len;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004617
Bruce Allan3d5e33c2009-11-20 23:27:03 +00004618 if (!skb_is_gso(skb))
4619 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004620
Bruce Allan3d5e33c2009-11-20 23:27:03 +00004621 if (skb_header_cloned(skb)) {
Bruce Allan90da0662011-01-06 07:02:53 +00004622 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4623
Bruce Allan3d5e33c2009-11-20 23:27:03 +00004624 if (err)
4625 return err;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004626 }
4627
Bruce Allan3d5e33c2009-11-20 23:27:03 +00004628 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
4629 mss = skb_shinfo(skb)->gso_size;
4630 if (skb->protocol == htons(ETH_P_IP)) {
4631 struct iphdr *iph = ip_hdr(skb);
4632 iph->tot_len = 0;
4633 iph->check = 0;
4634 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
4635 0, IPPROTO_TCP, 0);
4636 cmd_length = E1000_TXD_CMD_IP;
4637 ipcse = skb_transport_offset(skb) - 1;
Sridhar Samudrala8e1e8a42010-01-23 02:02:21 -08004638 } else if (skb_is_gso_v6(skb)) {
Bruce Allan3d5e33c2009-11-20 23:27:03 +00004639 ipv6_hdr(skb)->payload_len = 0;
4640 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4641 &ipv6_hdr(skb)->daddr,
4642 0, IPPROTO_TCP, 0);
4643 ipcse = 0;
4644 }
4645 ipcss = skb_network_offset(skb);
4646 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
4647 tucss = skb_transport_offset(skb);
4648 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
4649 tucse = 0;
4650
4651 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
4652 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
4653
4654 i = tx_ring->next_to_use;
4655 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
4656 buffer_info = &tx_ring->buffer_info[i];
4657
4658 context_desc->lower_setup.ip_fields.ipcss = ipcss;
4659 context_desc->lower_setup.ip_fields.ipcso = ipcso;
4660 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
4661 context_desc->upper_setup.tcp_fields.tucss = tucss;
4662 context_desc->upper_setup.tcp_fields.tucso = tucso;
4663 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
4664 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
4665 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
4666 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
4667
4668 buffer_info->time_stamp = jiffies;
4669 buffer_info->next_to_watch = i;
4670
4671 i++;
4672 if (i == tx_ring->count)
4673 i = 0;
4674 tx_ring->next_to_use = i;
4675
4676 return 1;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004677}
4678
Bruce Allan55aa6982011-12-16 00:45:45 +00004679static bool e1000_tx_csum(struct e1000_ring *tx_ring, struct sk_buff *skb)
Auke Kokbc7f75f2007-09-17 12:30:59 -07004680{
Bruce Allan55aa6982011-12-16 00:45:45 +00004681 struct e1000_adapter *adapter = tx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004682 struct e1000_context_desc *context_desc;
4683 struct e1000_buffer *buffer_info;
4684 unsigned int i;
4685 u8 css;
Dave Grahamaf807c82008-10-09 14:28:58 -07004686 u32 cmd_len = E1000_TXD_CMD_DEXT;
Arthur Jones5f66f202009-03-19 01:13:08 +00004687 __be16 protocol;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004688
Dave Grahamaf807c82008-10-09 14:28:58 -07004689 if (skb->ip_summed != CHECKSUM_PARTIAL)
4690 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004691
Arthur Jones5f66f202009-03-19 01:13:08 +00004692 if (skb->protocol == cpu_to_be16(ETH_P_8021Q))
4693 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
4694 else
4695 protocol = skb->protocol;
4696
Arthur Jones3f518392009-03-20 15:56:35 -07004697 switch (protocol) {
Harvey Harrison09640e62009-02-01 00:45:17 -08004698 case cpu_to_be16(ETH_P_IP):
Dave Grahamaf807c82008-10-09 14:28:58 -07004699 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
4700 cmd_len |= E1000_TXD_CMD_TCP;
4701 break;
Harvey Harrison09640e62009-02-01 00:45:17 -08004702 case cpu_to_be16(ETH_P_IPV6):
Dave Grahamaf807c82008-10-09 14:28:58 -07004703 /* XXX not handling all IPV6 headers */
4704 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
4705 cmd_len |= E1000_TXD_CMD_TCP;
4706 break;
4707 default:
4708 if (unlikely(net_ratelimit()))
Arthur Jones5f66f202009-03-19 01:13:08 +00004709 e_warn("checksum_partial proto=%x!\n",
4710 be16_to_cpu(protocol));
Dave Grahamaf807c82008-10-09 14:28:58 -07004711 break;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004712 }
4713
Michał Mirosław0d0b1672010-12-14 15:24:08 +00004714 css = skb_checksum_start_offset(skb);
Dave Grahamaf807c82008-10-09 14:28:58 -07004715
4716 i = tx_ring->next_to_use;
4717 buffer_info = &tx_ring->buffer_info[i];
4718 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
4719
4720 context_desc->lower_setup.ip_config = 0;
4721 context_desc->upper_setup.tcp_fields.tucss = css;
4722 context_desc->upper_setup.tcp_fields.tucso =
4723 css + skb->csum_offset;
4724 context_desc->upper_setup.tcp_fields.tucse = 0;
4725 context_desc->tcp_seg_setup.data = 0;
4726 context_desc->cmd_and_length = cpu_to_le32(cmd_len);
4727
4728 buffer_info->time_stamp = jiffies;
4729 buffer_info->next_to_watch = i;
4730
4731 i++;
4732 if (i == tx_ring->count)
4733 i = 0;
4734 tx_ring->next_to_use = i;
4735
4736 return 1;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004737}
4738
4739#define E1000_MAX_PER_TXD 8192
4740#define E1000_MAX_TXD_PWR 12
4741
Bruce Allan55aa6982011-12-16 00:45:45 +00004742static int e1000_tx_map(struct e1000_ring *tx_ring, struct sk_buff *skb,
4743 unsigned int first, unsigned int max_per_txd,
4744 unsigned int nr_frags, unsigned int mss)
Auke Kokbc7f75f2007-09-17 12:30:59 -07004745{
Bruce Allan55aa6982011-12-16 00:45:45 +00004746 struct e1000_adapter *adapter = tx_ring->adapter;
Alexander Duyck03b13202009-12-02 16:45:31 +00004747 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck1b7719c2009-03-19 01:12:50 +00004748 struct e1000_buffer *buffer_info;
Jesse Brandeburg8ddc9512009-03-02 16:02:53 -08004749 unsigned int len = skb_headlen(skb);
Alexander Duyck03b13202009-12-02 16:45:31 +00004750 unsigned int offset = 0, size, count = 0, i;
Tom Herbert9ed318d2010-05-05 14:02:27 +00004751 unsigned int f, bytecount, segs;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004752
4753 i = tx_ring->next_to_use;
4754
4755 while (len) {
Alexander Duyck1b7719c2009-03-19 01:12:50 +00004756 buffer_info = &tx_ring->buffer_info[i];
Auke Kokbc7f75f2007-09-17 12:30:59 -07004757 size = min(len, max_per_txd);
4758
Auke Kokbc7f75f2007-09-17 12:30:59 -07004759 buffer_info->length = size;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004760 buffer_info->time_stamp = jiffies;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004761 buffer_info->next_to_watch = i;
Nick Nunley0be3f552010-04-27 13:09:05 +00004762 buffer_info->dma = dma_map_single(&pdev->dev,
4763 skb->data + offset,
Bruce Allanaf667a22010-12-31 06:10:01 +00004764 size, DMA_TO_DEVICE);
Alexander Duyck03b13202009-12-02 16:45:31 +00004765 buffer_info->mapped_as_page = false;
Nick Nunley0be3f552010-04-27 13:09:05 +00004766 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck03b13202009-12-02 16:45:31 +00004767 goto dma_error;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004768
4769 len -= size;
4770 offset += size;
Alexander Duyck03b13202009-12-02 16:45:31 +00004771 count++;
Alexander Duyck1b7719c2009-03-19 01:12:50 +00004772
4773 if (len) {
4774 i++;
4775 if (i == tx_ring->count)
4776 i = 0;
4777 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07004778 }
4779
4780 for (f = 0; f < nr_frags; f++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00004781 const struct skb_frag_struct *frag;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004782
4783 frag = &skb_shinfo(skb)->frags[f];
Eric Dumazet9e903e02011-10-18 21:00:24 +00004784 len = skb_frag_size(frag);
Ian Campbell877749b2011-08-29 23:18:26 +00004785 offset = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004786
4787 while (len) {
Alexander Duyck1b7719c2009-03-19 01:12:50 +00004788 i++;
4789 if (i == tx_ring->count)
4790 i = 0;
4791
Auke Kokbc7f75f2007-09-17 12:30:59 -07004792 buffer_info = &tx_ring->buffer_info[i];
4793 size = min(len, max_per_txd);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004794
4795 buffer_info->length = size;
4796 buffer_info->time_stamp = jiffies;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004797 buffer_info->next_to_watch = i;
Ian Campbell877749b2011-08-29 23:18:26 +00004798 buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
4799 offset, size, DMA_TO_DEVICE);
Alexander Duyck03b13202009-12-02 16:45:31 +00004800 buffer_info->mapped_as_page = true;
Nick Nunley0be3f552010-04-27 13:09:05 +00004801 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
Alexander Duyck03b13202009-12-02 16:45:31 +00004802 goto dma_error;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004803
4804 len -= size;
4805 offset += size;
4806 count++;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004807 }
4808 }
4809
Bruce Allanaf667a22010-12-31 06:10:01 +00004810 segs = skb_shinfo(skb)->gso_segs ? : 1;
Tom Herbert9ed318d2010-05-05 14:02:27 +00004811 /* multiply data chunks by size of headers */
4812 bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
4813
Auke Kokbc7f75f2007-09-17 12:30:59 -07004814 tx_ring->buffer_info[i].skb = skb;
Tom Herbert9ed318d2010-05-05 14:02:27 +00004815 tx_ring->buffer_info[i].segs = segs;
4816 tx_ring->buffer_info[i].bytecount = bytecount;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004817 tx_ring->buffer_info[first].next_to_watch = i;
4818
4819 return count;
Alexander Duyck03b13202009-12-02 16:45:31 +00004820
4821dma_error:
Bruce Allanaf667a22010-12-31 06:10:01 +00004822 dev_err(&pdev->dev, "Tx DMA map failed\n");
Alexander Duyck03b13202009-12-02 16:45:31 +00004823 buffer_info->dma = 0;
Roel Kluinc1fa3472010-01-19 14:21:45 +00004824 if (count)
Alexander Duyck03b13202009-12-02 16:45:31 +00004825 count--;
Roel Kluinc1fa3472010-01-19 14:21:45 +00004826
4827 while (count--) {
Bruce Allanaf667a22010-12-31 06:10:01 +00004828 if (i == 0)
Alexander Duyck03b13202009-12-02 16:45:31 +00004829 i += tx_ring->count;
Roel Kluinc1fa3472010-01-19 14:21:45 +00004830 i--;
Alexander Duyck03b13202009-12-02 16:45:31 +00004831 buffer_info = &tx_ring->buffer_info[i];
Bruce Allan55aa6982011-12-16 00:45:45 +00004832 e1000_put_txbuf(tx_ring, buffer_info);
Alexander Duyck03b13202009-12-02 16:45:31 +00004833 }
4834
4835 return 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004836}
4837
Bruce Allan55aa6982011-12-16 00:45:45 +00004838static void e1000_tx_queue(struct e1000_ring *tx_ring, int tx_flags, int count)
Auke Kokbc7f75f2007-09-17 12:30:59 -07004839{
Bruce Allan55aa6982011-12-16 00:45:45 +00004840 struct e1000_adapter *adapter = tx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004841 struct e1000_tx_desc *tx_desc = NULL;
4842 struct e1000_buffer *buffer_info;
4843 u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
4844 unsigned int i;
4845
4846 if (tx_flags & E1000_TX_FLAGS_TSO) {
4847 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
4848 E1000_TXD_CMD_TSE;
4849 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
4850
4851 if (tx_flags & E1000_TX_FLAGS_IPV4)
4852 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
4853 }
4854
4855 if (tx_flags & E1000_TX_FLAGS_CSUM) {
4856 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
4857 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
4858 }
4859
4860 if (tx_flags & E1000_TX_FLAGS_VLAN) {
4861 txd_lower |= E1000_TXD_CMD_VLE;
4862 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
4863 }
4864
4865 i = tx_ring->next_to_use;
4866
Bruce Allan36b973d2010-11-24 07:42:43 +00004867 do {
Auke Kokbc7f75f2007-09-17 12:30:59 -07004868 buffer_info = &tx_ring->buffer_info[i];
4869 tx_desc = E1000_TX_DESC(*tx_ring, i);
4870 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
4871 tx_desc->lower.data =
4872 cpu_to_le32(txd_lower | buffer_info->length);
4873 tx_desc->upper.data = cpu_to_le32(txd_upper);
4874
4875 i++;
4876 if (i == tx_ring->count)
4877 i = 0;
Bruce Allan36b973d2010-11-24 07:42:43 +00004878 } while (--count > 0);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004879
4880 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
4881
Bruce Allanad680762008-03-28 09:15:03 -07004882 /*
4883 * Force memory writes to complete before letting h/w
Auke Kokbc7f75f2007-09-17 12:30:59 -07004884 * know there are new descriptors to fetch. (Only
4885 * applicable for weak-ordered memory model archs,
Bruce Allanad680762008-03-28 09:15:03 -07004886 * such as IA-64).
4887 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004888 wmb();
4889
4890 tx_ring->next_to_use = i;
David S. Miller823dcd22011-08-20 10:39:12 -07004891
4892 if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
Bruce Allan55aa6982011-12-16 00:45:45 +00004893 e1000e_update_tdt_wa(tx_ring, i);
David S. Miller823dcd22011-08-20 10:39:12 -07004894 else
Bruce Allanc5083cf2011-12-16 00:45:40 +00004895 writel(i, tx_ring->tail);
David S. Miller823dcd22011-08-20 10:39:12 -07004896
Bruce Allanad680762008-03-28 09:15:03 -07004897 /*
4898 * we need this if more than one processor can write to our tail
4899 * at a time, it synchronizes IO on IA64/Altix systems
4900 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004901 mmiowb();
4902}
4903
4904#define MINIMUM_DHCP_PACKET_SIZE 282
4905static int e1000_transfer_dhcp_info(struct e1000_adapter *adapter,
4906 struct sk_buff *skb)
4907{
4908 struct e1000_hw *hw = &adapter->hw;
4909 u16 length, offset;
4910
4911 if (vlan_tx_tag_present(skb)) {
Joe Perches8e95a202009-12-03 07:58:21 +00004912 if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
4913 (adapter->hw.mng_cookie.status &
Auke Kokbc7f75f2007-09-17 12:30:59 -07004914 E1000_MNG_DHCP_COOKIE_STATUS_VLAN)))
4915 return 0;
4916 }
4917
4918 if (skb->len <= MINIMUM_DHCP_PACKET_SIZE)
4919 return 0;
4920
4921 if (((struct ethhdr *) skb->data)->h_proto != htons(ETH_P_IP))
4922 return 0;
4923
4924 {
4925 const struct iphdr *ip = (struct iphdr *)((u8 *)skb->data+14);
4926 struct udphdr *udp;
4927
4928 if (ip->protocol != IPPROTO_UDP)
4929 return 0;
4930
4931 udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
4932 if (ntohs(udp->dest) != 67)
4933 return 0;
4934
4935 offset = (u8 *)udp + 8 - skb->data;
4936 length = skb->len - offset;
4937 return e1000e_mng_write_dhcp_info(hw, (u8 *)udp + 8, length);
4938 }
4939
4940 return 0;
4941}
4942
Bruce Allan55aa6982011-12-16 00:45:45 +00004943static int __e1000_maybe_stop_tx(struct e1000_ring *tx_ring, int size)
Auke Kokbc7f75f2007-09-17 12:30:59 -07004944{
Bruce Allan55aa6982011-12-16 00:45:45 +00004945 struct e1000_adapter *adapter = tx_ring->adapter;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004946
Bruce Allan55aa6982011-12-16 00:45:45 +00004947 netif_stop_queue(adapter->netdev);
Bruce Allanad680762008-03-28 09:15:03 -07004948 /*
4949 * Herbert's original patch had:
Auke Kokbc7f75f2007-09-17 12:30:59 -07004950 * smp_mb__after_netif_stop_queue();
Bruce Allanad680762008-03-28 09:15:03 -07004951 * but since that doesn't exist yet, just open code it.
4952 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07004953 smp_mb();
4954
Bruce Allanad680762008-03-28 09:15:03 -07004955 /*
4956 * We need to check again in a case another CPU has just
4957 * made room available.
4958 */
Bruce Allan55aa6982011-12-16 00:45:45 +00004959 if (e1000_desc_unused(tx_ring) < size)
Auke Kokbc7f75f2007-09-17 12:30:59 -07004960 return -EBUSY;
4961
4962 /* A reprieve! */
Bruce Allan55aa6982011-12-16 00:45:45 +00004963 netif_start_queue(adapter->netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004964 ++adapter->restart_queue;
4965 return 0;
4966}
4967
Bruce Allan55aa6982011-12-16 00:45:45 +00004968static int e1000_maybe_stop_tx(struct e1000_ring *tx_ring, int size)
Auke Kokbc7f75f2007-09-17 12:30:59 -07004969{
Bruce Allan55aa6982011-12-16 00:45:45 +00004970 if (e1000_desc_unused(tx_ring) >= size)
Auke Kokbc7f75f2007-09-17 12:30:59 -07004971 return 0;
Bruce Allan55aa6982011-12-16 00:45:45 +00004972 return __e1000_maybe_stop_tx(tx_ring, size);
Auke Kokbc7f75f2007-09-17 12:30:59 -07004973}
4974
Bruce Allan0e15df42012-01-31 06:37:11 +00004975#define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1)
Stephen Hemminger3b29a562009-08-31 19:50:55 +00004976static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
4977 struct net_device *netdev)
Auke Kokbc7f75f2007-09-17 12:30:59 -07004978{
4979 struct e1000_adapter *adapter = netdev_priv(netdev);
4980 struct e1000_ring *tx_ring = adapter->tx_ring;
4981 unsigned int first;
4982 unsigned int max_per_txd = E1000_MAX_PER_TXD;
4983 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
4984 unsigned int tx_flags = 0;
Eric Dumazete743d312010-04-14 15:59:40 -07004985 unsigned int len = skb_headlen(skb);
Auke Kok4e6c7092007-10-05 14:15:23 -07004986 unsigned int nr_frags;
4987 unsigned int mss;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004988 int count = 0;
4989 int tso;
4990 unsigned int f;
Auke Kokbc7f75f2007-09-17 12:30:59 -07004991
4992 if (test_bit(__E1000_DOWN, &adapter->state)) {
4993 dev_kfree_skb_any(skb);
4994 return NETDEV_TX_OK;
4995 }
4996
4997 if (skb->len <= 0) {
4998 dev_kfree_skb_any(skb);
4999 return NETDEV_TX_OK;
5000 }
5001
5002 mss = skb_shinfo(skb)->gso_size;
Bruce Allanad680762008-03-28 09:15:03 -07005003 /*
5004 * The controller does a simple calculation to
Auke Kokbc7f75f2007-09-17 12:30:59 -07005005 * make sure there is enough room in the FIFO before
5006 * initiating the DMA for each buffer. The calc is:
5007 * 4 = ceil(buffer len/mss). To make sure we don't
5008 * overrun the FIFO, adjust the max buffer len if mss
Bruce Allanad680762008-03-28 09:15:03 -07005009 * drops.
5010 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07005011 if (mss) {
5012 u8 hdr_len;
5013 max_per_txd = min(mss << 2, max_per_txd);
5014 max_txd_pwr = fls(max_per_txd) - 1;
5015
Bruce Allanad680762008-03-28 09:15:03 -07005016 /*
5017 * TSO Workaround for 82571/2/3 Controllers -- if skb->data
5018 * points to just header, pull a few bytes of payload from
5019 * frags into skb->data
5020 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07005021 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Bruce Allanad680762008-03-28 09:15:03 -07005022 /*
5023 * we do this workaround for ES2LAN, but it is un-necessary,
5024 * avoiding it could save a lot of cycles
5025 */
Auke Kok4e6c7092007-10-05 14:15:23 -07005026 if (skb->data_len && (hdr_len == len)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07005027 unsigned int pull_size;
5028
Bruce Allana2a5b322012-01-31 06:37:17 +00005029 pull_size = min_t(unsigned int, 4, skb->data_len);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005030 if (!__pskb_pull_tail(skb, pull_size)) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -07005031 e_err("__pskb_pull_tail failed.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07005032 dev_kfree_skb_any(skb);
5033 return NETDEV_TX_OK;
5034 }
Eric Dumazete743d312010-04-14 15:59:40 -07005035 len = skb_headlen(skb);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005036 }
5037 }
5038
5039 /* reserve a descriptor for the offload context */
5040 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
5041 count++;
5042 count++;
5043
5044 count += TXD_USE_COUNT(len, max_txd_pwr);
5045
5046 nr_frags = skb_shinfo(skb)->nr_frags;
5047 for (f = 0; f < nr_frags; f++)
Eric Dumazet9e903e02011-10-18 21:00:24 +00005048 count += TXD_USE_COUNT(skb_frag_size(&skb_shinfo(skb)->frags[f]),
Auke Kokbc7f75f2007-09-17 12:30:59 -07005049 max_txd_pwr);
5050
5051 if (adapter->hw.mac.tx_pkt_filtering)
5052 e1000_transfer_dhcp_info(adapter, skb);
5053
Bruce Allanad680762008-03-28 09:15:03 -07005054 /*
5055 * need: count + 2 desc gap to keep tail from touching
5056 * head, otherwise try next time
5057 */
Bruce Allan55aa6982011-12-16 00:45:45 +00005058 if (e1000_maybe_stop_tx(tx_ring, count + 2))
Auke Kokbc7f75f2007-09-17 12:30:59 -07005059 return NETDEV_TX_BUSY;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005060
Jesse Grosseab6d182010-10-20 13:56:03 +00005061 if (vlan_tx_tag_present(skb)) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07005062 tx_flags |= E1000_TX_FLAGS_VLAN;
5063 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
5064 }
5065
5066 first = tx_ring->next_to_use;
5067
Bruce Allan55aa6982011-12-16 00:45:45 +00005068 tso = e1000_tso(tx_ring, skb);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005069 if (tso < 0) {
5070 dev_kfree_skb_any(skb);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005071 return NETDEV_TX_OK;
5072 }
5073
5074 if (tso)
5075 tx_flags |= E1000_TX_FLAGS_TSO;
Bruce Allan55aa6982011-12-16 00:45:45 +00005076 else if (e1000_tx_csum(tx_ring, skb))
Auke Kokbc7f75f2007-09-17 12:30:59 -07005077 tx_flags |= E1000_TX_FLAGS_CSUM;
5078
Bruce Allanad680762008-03-28 09:15:03 -07005079 /*
5080 * Old method was to assume IPv4 packet by default if TSO was enabled.
Auke Kokbc7f75f2007-09-17 12:30:59 -07005081 * 82571 hardware supports TSO capabilities for IPv6 as well...
Bruce Allanad680762008-03-28 09:15:03 -07005082 * no longer assume, we must.
5083 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07005084 if (skb->protocol == htons(ETH_P_IP))
5085 tx_flags |= E1000_TX_FLAGS_IPV4;
5086
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005087 /* if count is 0 then mapping error has occurred */
Bruce Allan55aa6982011-12-16 00:45:45 +00005088 count = e1000_tx_map(tx_ring, skb, first, max_per_txd, nr_frags, mss);
Alexander Duyck1b7719c2009-03-19 01:12:50 +00005089 if (count) {
Tom Herbert3f0cfa32011-11-28 16:33:16 +00005090 netdev_sent_queue(netdev, skb->len);
Bruce Allan55aa6982011-12-16 00:45:45 +00005091 e1000_tx_queue(tx_ring, tx_flags, count);
Alexander Duyck1b7719c2009-03-19 01:12:50 +00005092 /* Make sure there is space in the ring for the next send. */
Bruce Allan55aa6982011-12-16 00:45:45 +00005093 e1000_maybe_stop_tx(tx_ring, MAX_SKB_FRAGS + 2);
Alexander Duyck1b7719c2009-03-19 01:12:50 +00005094
5095 } else {
Auke Kokbc7f75f2007-09-17 12:30:59 -07005096 dev_kfree_skb_any(skb);
Alexander Duyck1b7719c2009-03-19 01:12:50 +00005097 tx_ring->buffer_info[first].time_stamp = 0;
5098 tx_ring->next_to_use = first;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005099 }
5100
Auke Kokbc7f75f2007-09-17 12:30:59 -07005101 return NETDEV_TX_OK;
5102}
5103
5104/**
5105 * e1000_tx_timeout - Respond to a Tx Hang
5106 * @netdev: network interface device structure
5107 **/
5108static void e1000_tx_timeout(struct net_device *netdev)
5109{
5110 struct e1000_adapter *adapter = netdev_priv(netdev);
5111
5112 /* Do the reset outside of interrupt context */
5113 adapter->tx_timeout_count++;
5114 schedule_work(&adapter->reset_task);
5115}
5116
5117static void e1000_reset_task(struct work_struct *work)
5118{
5119 struct e1000_adapter *adapter;
5120 adapter = container_of(work, struct e1000_adapter, reset_task);
5121
Jesse Brandeburg615b32a2011-02-02 10:19:45 +00005122 /* don't run the task if already down */
5123 if (test_bit(__E1000_DOWN, &adapter->state))
5124 return;
5125
Carolyn Wybornyaffa9df2010-10-28 00:59:55 +00005126 if (!((adapter->flags & FLAG_RX_NEEDS_RESTART) &&
5127 (adapter->flags & FLAG_RX_RESTART_NOW))) {
5128 e1000e_dump(adapter);
5129 e_err("Reset adapter\n");
5130 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07005131 e1000e_reinit_locked(adapter);
5132}
5133
5134/**
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00005135 * e1000_get_stats64 - Get System Network Statistics
Auke Kokbc7f75f2007-09-17 12:30:59 -07005136 * @netdev: network interface device structure
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00005137 * @stats: rtnl_link_stats64 pointer
Auke Kokbc7f75f2007-09-17 12:30:59 -07005138 *
5139 * Returns the address of the device statistics structure.
Auke Kokbc7f75f2007-09-17 12:30:59 -07005140 **/
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00005141struct rtnl_link_stats64 *e1000e_get_stats64(struct net_device *netdev,
5142 struct rtnl_link_stats64 *stats)
Auke Kokbc7f75f2007-09-17 12:30:59 -07005143{
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00005144 struct e1000_adapter *adapter = netdev_priv(netdev);
5145
5146 memset(stats, 0, sizeof(struct rtnl_link_stats64));
5147 spin_lock(&adapter->stats64_lock);
5148 e1000e_update_stats(adapter);
5149 /* Fill out the OS statistics structure */
5150 stats->rx_bytes = adapter->stats.gorc;
5151 stats->rx_packets = adapter->stats.gprc;
5152 stats->tx_bytes = adapter->stats.gotc;
5153 stats->tx_packets = adapter->stats.gptc;
5154 stats->multicast = adapter->stats.mprc;
5155 stats->collisions = adapter->stats.colc;
5156
5157 /* Rx Errors */
5158
5159 /*
5160 * RLEC on some newer hardware can be incorrect so build
5161 * our own version based on RUC and ROC
5162 */
5163 stats->rx_errors = adapter->stats.rxerrc +
5164 adapter->stats.crcerrs + adapter->stats.algnerrc +
5165 adapter->stats.ruc + adapter->stats.roc +
5166 adapter->stats.cexterr;
5167 stats->rx_length_errors = adapter->stats.ruc +
5168 adapter->stats.roc;
5169 stats->rx_crc_errors = adapter->stats.crcerrs;
5170 stats->rx_frame_errors = adapter->stats.algnerrc;
5171 stats->rx_missed_errors = adapter->stats.mpc;
5172
5173 /* Tx Errors */
5174 stats->tx_errors = adapter->stats.ecol +
5175 adapter->stats.latecol;
5176 stats->tx_aborted_errors = adapter->stats.ecol;
5177 stats->tx_window_errors = adapter->stats.latecol;
5178 stats->tx_carrier_errors = adapter->stats.tncrs;
5179
5180 /* Tx Dropped needs to be maintained elsewhere */
5181
5182 spin_unlock(&adapter->stats64_lock);
5183 return stats;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005184}
5185
5186/**
5187 * e1000_change_mtu - Change the Maximum Transfer Unit
5188 * @netdev: network interface device structure
5189 * @new_mtu: new value for maximum frame size
5190 *
5191 * Returns 0 on success, negative on failure
5192 **/
5193static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
5194{
5195 struct e1000_adapter *adapter = netdev_priv(netdev);
5196 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
5197
Bruce Allan2adc55c2009-06-02 11:28:58 +00005198 /* Jumbo frame support */
Bruce Allan70495a52012-01-11 01:26:50 +00005199 if (max_frame > ETH_FRAME_LEN + ETH_FCS_LEN) {
5200 if (!(adapter->flags & FLAG_HAS_JUMBO_FRAMES)) {
5201 e_err("Jumbo Frames not supported.\n");
5202 return -EINVAL;
5203 }
5204
5205 /*
5206 * IP payload checksum (enabled with jumbos/packet-split when
5207 * Rx checksum is enabled) and generation of RSS hash is
5208 * mutually exclusive in the hardware.
5209 */
5210 if ((netdev->features & NETIF_F_RXCSUM) &&
5211 (netdev->features & NETIF_F_RXHASH)) {
5212 e_err("Jumbo frames cannot be enabled when both receive checksum offload and receive hashing are enabled. Disable one of the receive offload features before enabling jumbos.\n");
5213 return -EINVAL;
5214 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07005215 }
5216
Bruce Allan2adc55c2009-06-02 11:28:58 +00005217 /* Supported frame sizes */
5218 if ((new_mtu < ETH_ZLEN + ETH_FCS_LEN + VLAN_HLEN) ||
5219 (max_frame > adapter->max_hw_frame_size)) {
5220 e_err("Unsupported MTU setting\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07005221 return -EINVAL;
5222 }
5223
Bruce Allana1ce6472010-09-22 17:16:40 +00005224 /* Jumbo frame workaround on 82579 requires CRC be stripped */
5225 if ((adapter->hw.mac.type == e1000_pch2lan) &&
5226 !(adapter->flags2 & FLAG2_CRC_STRIPPING) &&
5227 (new_mtu > ETH_DATA_LEN)) {
Jeff Kirsheref456f82011-11-03 11:40:28 +00005228 e_err("Jumbo Frames not supported on 82579 when CRC stripping is disabled.\n");
Bruce Allana1ce6472010-09-22 17:16:40 +00005229 return -EINVAL;
5230 }
5231
Bruce Allan6f461f62010-04-27 03:33:04 +00005232 /* 82573 Errata 17 */
5233 if (((adapter->hw.mac.type == e1000_82573) ||
5234 (adapter->hw.mac.type == e1000_82574)) &&
5235 (max_frame > ETH_FRAME_LEN + ETH_FCS_LEN)) {
5236 adapter->flags2 |= FLAG2_DISABLE_ASPM_L1;
5237 e1000e_disable_aspm(adapter->pdev, PCIE_LINK_STATE_L1);
5238 }
5239
Auke Kokbc7f75f2007-09-17 12:30:59 -07005240 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
Bruce Allan1bba4382011-03-19 00:27:20 +00005241 usleep_range(1000, 2000);
Bruce Allan610c9922009-11-19 12:35:45 +00005242 /* e1000e_down -> e1000e_reset dependent on max_frame_size & mtu */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07005243 adapter->max_frame_size = max_frame;
Bruce Allan610c9922009-11-19 12:35:45 +00005244 e_info("changing MTU from %d to %d\n", netdev->mtu, new_mtu);
5245 netdev->mtu = new_mtu;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005246 if (netif_running(netdev))
5247 e1000e_down(adapter);
5248
Bruce Allanad680762008-03-28 09:15:03 -07005249 /*
5250 * NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
Auke Kokbc7f75f2007-09-17 12:30:59 -07005251 * means we reserve 2 more, this pushes us to allocate from the next
5252 * larger slab size.
Bruce Allanad680762008-03-28 09:15:03 -07005253 * i.e. RXBUFFER_2048 --> size-4096 slab
Bruce Allan97ac8ca2008-04-29 09:16:05 -07005254 * However with the new *_jumbo_rx* routines, jumbo receives will use
5255 * fragmented skbs
Bruce Allanad680762008-03-28 09:15:03 -07005256 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07005257
Jesse Brandeburg99261462010-01-22 22:56:16 +00005258 if (max_frame <= 2048)
Auke Kokbc7f75f2007-09-17 12:30:59 -07005259 adapter->rx_buffer_len = 2048;
5260 else
5261 adapter->rx_buffer_len = 4096;
5262
5263 /* adjust allocation if LPE protects us, and we aren't using SBP */
5264 if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
5265 (max_frame == ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN))
5266 adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN
Bruce Allanad680762008-03-28 09:15:03 -07005267 + ETH_FCS_LEN;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005268
Auke Kokbc7f75f2007-09-17 12:30:59 -07005269 if (netif_running(netdev))
5270 e1000e_up(adapter);
5271 else
5272 e1000e_reset(adapter);
5273
5274 clear_bit(__E1000_RESETTING, &adapter->state);
5275
5276 return 0;
5277}
5278
5279static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
5280 int cmd)
5281{
5282 struct e1000_adapter *adapter = netdev_priv(netdev);
5283 struct mii_ioctl_data *data = if_mii(ifr);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005284
Jeff Kirsher318a94d2008-03-28 09:15:16 -07005285 if (adapter->hw.phy.media_type != e1000_media_type_copper)
Auke Kokbc7f75f2007-09-17 12:30:59 -07005286 return -EOPNOTSUPP;
5287
5288 switch (cmd) {
5289 case SIOCGMIIPHY:
5290 data->phy_id = adapter->hw.phy.addr;
5291 break;
5292 case SIOCGMIIREG:
Bruce Allanb16a0022009-11-20 23:24:30 +00005293 e1000_phy_read_status(adapter);
5294
Bruce Allan7c257692008-04-23 11:09:00 -07005295 switch (data->reg_num & 0x1F) {
5296 case MII_BMCR:
5297 data->val_out = adapter->phy_regs.bmcr;
5298 break;
5299 case MII_BMSR:
5300 data->val_out = adapter->phy_regs.bmsr;
5301 break;
5302 case MII_PHYSID1:
5303 data->val_out = (adapter->hw.phy.id >> 16);
5304 break;
5305 case MII_PHYSID2:
5306 data->val_out = (adapter->hw.phy.id & 0xFFFF);
5307 break;
5308 case MII_ADVERTISE:
5309 data->val_out = adapter->phy_regs.advertise;
5310 break;
5311 case MII_LPA:
5312 data->val_out = adapter->phy_regs.lpa;
5313 break;
5314 case MII_EXPANSION:
5315 data->val_out = adapter->phy_regs.expansion;
5316 break;
5317 case MII_CTRL1000:
5318 data->val_out = adapter->phy_regs.ctrl1000;
5319 break;
5320 case MII_STAT1000:
5321 data->val_out = adapter->phy_regs.stat1000;
5322 break;
5323 case MII_ESTATUS:
5324 data->val_out = adapter->phy_regs.estatus;
5325 break;
5326 default:
Auke Kokbc7f75f2007-09-17 12:30:59 -07005327 return -EIO;
5328 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07005329 break;
5330 case SIOCSMIIREG:
5331 default:
5332 return -EOPNOTSUPP;
5333 }
5334 return 0;
5335}
5336
5337static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
5338{
5339 switch (cmd) {
5340 case SIOCGMIIPHY:
5341 case SIOCGMIIREG:
5342 case SIOCSMIIREG:
5343 return e1000_mii_ioctl(netdev, ifr, cmd);
5344 default:
5345 return -EOPNOTSUPP;
5346 }
5347}
5348
Bruce Allana4f58f52009-06-02 11:29:18 +00005349static int e1000_init_phy_wakeup(struct e1000_adapter *adapter, u32 wufc)
5350{
5351 struct e1000_hw *hw = &adapter->hw;
5352 u32 i, mac_reg;
Bruce Allan2b6b1682011-05-13 07:20:09 +00005353 u16 phy_reg, wuc_enable;
Bruce Allana4f58f52009-06-02 11:29:18 +00005354 int retval = 0;
5355
5356 /* copy MAC RARs to PHY RARs */
Bruce Alland3738bb2010-06-16 13:27:28 +00005357 e1000_copy_rx_addrs_to_phy_ich8lan(hw);
Bruce Allana4f58f52009-06-02 11:29:18 +00005358
Bruce Allan2b6b1682011-05-13 07:20:09 +00005359 retval = hw->phy.ops.acquire(hw);
5360 if (retval) {
5361 e_err("Could not acquire PHY\n");
5362 return retval;
5363 }
5364
5365 /* Enable access to wakeup registers on and set page to BM_WUC_PAGE */
5366 retval = e1000_enable_phy_wakeup_reg_access_bm(hw, &wuc_enable);
5367 if (retval)
5368 goto out;
5369
5370 /* copy MAC MTA to PHY MTA - only needed for pchlan */
Bruce Allana4f58f52009-06-02 11:29:18 +00005371 for (i = 0; i < adapter->hw.mac.mta_reg_count; i++) {
5372 mac_reg = E1000_READ_REG_ARRAY(hw, E1000_MTA, i);
Bruce Allan2b6b1682011-05-13 07:20:09 +00005373 hw->phy.ops.write_reg_page(hw, BM_MTA(i),
5374 (u16)(mac_reg & 0xFFFF));
5375 hw->phy.ops.write_reg_page(hw, BM_MTA(i) + 1,
5376 (u16)((mac_reg >> 16) & 0xFFFF));
Bruce Allana4f58f52009-06-02 11:29:18 +00005377 }
5378
5379 /* configure PHY Rx Control register */
Bruce Allan2b6b1682011-05-13 07:20:09 +00005380 hw->phy.ops.read_reg_page(&adapter->hw, BM_RCTL, &phy_reg);
Bruce Allana4f58f52009-06-02 11:29:18 +00005381 mac_reg = er32(RCTL);
5382 if (mac_reg & E1000_RCTL_UPE)
5383 phy_reg |= BM_RCTL_UPE;
5384 if (mac_reg & E1000_RCTL_MPE)
5385 phy_reg |= BM_RCTL_MPE;
5386 phy_reg &= ~(BM_RCTL_MO_MASK);
5387 if (mac_reg & E1000_RCTL_MO_3)
5388 phy_reg |= (((mac_reg & E1000_RCTL_MO_3) >> E1000_RCTL_MO_SHIFT)
5389 << BM_RCTL_MO_SHIFT);
5390 if (mac_reg & E1000_RCTL_BAM)
5391 phy_reg |= BM_RCTL_BAM;
5392 if (mac_reg & E1000_RCTL_PMCF)
5393 phy_reg |= BM_RCTL_PMCF;
5394 mac_reg = er32(CTRL);
5395 if (mac_reg & E1000_CTRL_RFCE)
5396 phy_reg |= BM_RCTL_RFCE;
Bruce Allan2b6b1682011-05-13 07:20:09 +00005397 hw->phy.ops.write_reg_page(&adapter->hw, BM_RCTL, phy_reg);
Bruce Allana4f58f52009-06-02 11:29:18 +00005398
5399 /* enable PHY wakeup in MAC register */
5400 ew32(WUFC, wufc);
5401 ew32(WUC, E1000_WUC_PHY_WAKE | E1000_WUC_PME_EN);
5402
5403 /* configure and enable PHY wakeup in PHY registers */
Bruce Allan2b6b1682011-05-13 07:20:09 +00005404 hw->phy.ops.write_reg_page(&adapter->hw, BM_WUFC, wufc);
5405 hw->phy.ops.write_reg_page(&adapter->hw, BM_WUC, E1000_WUC_PME_EN);
Bruce Allana4f58f52009-06-02 11:29:18 +00005406
5407 /* activate PHY wakeup */
Bruce Allan2b6b1682011-05-13 07:20:09 +00005408 wuc_enable |= BM_WUC_ENABLE_BIT | BM_WUC_HOST_WU_BIT;
5409 retval = e1000_disable_phy_wakeup_reg_access_bm(hw, &wuc_enable);
Bruce Allana4f58f52009-06-02 11:29:18 +00005410 if (retval)
5411 e_err("Could not set PHY Host Wakeup bit\n");
5412out:
Bruce Allan94d81862009-11-20 23:25:26 +00005413 hw->phy.ops.release(hw);
Bruce Allana4f58f52009-06-02 11:29:18 +00005414
5415 return retval;
5416}
5417
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00005418static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake,
5419 bool runtime)
Auke Kokbc7f75f2007-09-17 12:30:59 -07005420{
5421 struct net_device *netdev = pci_get_drvdata(pdev);
5422 struct e1000_adapter *adapter = netdev_priv(netdev);
5423 struct e1000_hw *hw = &adapter->hw;
5424 u32 ctrl, ctrl_ext, rctl, status;
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00005425 /* Runtime suspend should only enable wakeup for link changes */
5426 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005427 int retval = 0;
5428
5429 netif_device_detach(netdev);
5430
5431 if (netif_running(netdev)) {
5432 WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
5433 e1000e_down(adapter);
5434 e1000_free_irq(adapter);
5435 }
Bruce Allan4662e822008-08-26 18:37:06 -07005436 e1000e_reset_interrupt_capability(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005437
5438 retval = pci_save_state(pdev);
5439 if (retval)
5440 return retval;
5441
5442 status = er32(STATUS);
5443 if (status & E1000_STATUS_LU)
5444 wufc &= ~E1000_WUFC_LNKC;
5445
5446 if (wufc) {
5447 e1000_setup_rctl(adapter);
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00005448 e1000e_set_rx_mode(netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005449
5450 /* turn on all-multi mode if wake on multicast is enabled */
5451 if (wufc & E1000_WUFC_MC) {
5452 rctl = er32(RCTL);
5453 rctl |= E1000_RCTL_MPE;
5454 ew32(RCTL, rctl);
5455 }
5456
5457 ctrl = er32(CTRL);
5458 /* advertise wake from D3Cold */
5459 #define E1000_CTRL_ADVD3WUC 0x00100000
5460 /* phy power management enable */
5461 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
Bruce Allana4f58f52009-06-02 11:29:18 +00005462 ctrl |= E1000_CTRL_ADVD3WUC;
5463 if (!(adapter->flags2 & FLAG2_HAS_PHY_WAKEUP))
5464 ctrl |= E1000_CTRL_EN_PHY_PWR_MGMT;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005465 ew32(CTRL, ctrl);
5466
Jeff Kirsher318a94d2008-03-28 09:15:16 -07005467 if (adapter->hw.phy.media_type == e1000_media_type_fiber ||
5468 adapter->hw.phy.media_type ==
5469 e1000_media_type_internal_serdes) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07005470 /* keep the laser running in D3 */
5471 ctrl_ext = er32(CTRL_EXT);
Bruce Allan93a23f42009-12-08 07:27:41 +00005472 ctrl_ext |= E1000_CTRL_EXT_SDP3_DATA;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005473 ew32(CTRL_EXT, ctrl_ext);
5474 }
5475
Bruce Allan97ac8ca2008-04-29 09:16:05 -07005476 if (adapter->flags & FLAG_IS_ICH)
Bruce Allan99730e42011-05-13 07:19:48 +00005477 e1000_suspend_workarounds_ich8lan(&adapter->hw);
Bruce Allan97ac8ca2008-04-29 09:16:05 -07005478
Auke Kokbc7f75f2007-09-17 12:30:59 -07005479 /* Allow time for pending master requests to run */
5480 e1000e_disable_pcie_master(&adapter->hw);
5481
Bruce Allan82776a42009-08-14 14:35:33 +00005482 if (adapter->flags2 & FLAG2_HAS_PHY_WAKEUP) {
Bruce Allana4f58f52009-06-02 11:29:18 +00005483 /* enable wakeup by the PHY */
5484 retval = e1000_init_phy_wakeup(adapter, wufc);
5485 if (retval)
5486 return retval;
5487 } else {
5488 /* enable wakeup by the MAC */
5489 ew32(WUFC, wufc);
5490 ew32(WUC, E1000_WUC_PME_EN);
5491 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07005492 } else {
5493 ew32(WUC, 0);
5494 ew32(WUFC, 0);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005495 }
5496
Rafael J. Wysocki4f9de722009-04-15 17:43:43 +00005497 *enable_wake = !!wufc;
5498
Auke Kokbc7f75f2007-09-17 12:30:59 -07005499 /* make sure adapter isn't asleep if manageability is enabled */
Bruce Allan82776a42009-08-14 14:35:33 +00005500 if ((adapter->flags & FLAG_MNG_PT_ENABLED) ||
5501 (hw->mac.ops.check_mng_mode(hw)))
Rafael J. Wysocki4f9de722009-04-15 17:43:43 +00005502 *enable_wake = true;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005503
5504 if (adapter->hw.phy.type == e1000_phy_igp_3)
5505 e1000e_igp3_phy_powerdown_workaround_ich8lan(&adapter->hw);
5506
Bruce Allanad680762008-03-28 09:15:03 -07005507 /*
5508 * Release control of h/w to f/w. If f/w is AMT enabled, this
5509 * would have already happened in close and is redundant.
5510 */
Bruce Allan31dbe5b2011-01-06 14:29:52 +00005511 e1000e_release_hw_control(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005512
5513 pci_disable_device(pdev);
5514
Rafael J. Wysocki4f9de722009-04-15 17:43:43 +00005515 return 0;
5516}
5517
5518static void e1000_power_off(struct pci_dev *pdev, bool sleep, bool wake)
5519{
5520 if (sleep && wake) {
5521 pci_prepare_to_sleep(pdev);
5522 return;
5523 }
5524
5525 pci_wake_from_d3(pdev, wake);
5526 pci_set_power_state(pdev, PCI_D3hot);
5527}
5528
5529static void e1000_complete_shutdown(struct pci_dev *pdev, bool sleep,
5530 bool wake)
5531{
5532 struct net_device *netdev = pci_get_drvdata(pdev);
5533 struct e1000_adapter *adapter = netdev_priv(netdev);
5534
Alexander Duyck005cbdf2008-11-21 16:49:10 -08005535 /*
5536 * The pci-e switch on some quad port adapters will report a
5537 * correctable error when the MAC transitions from D0 to D3. To
5538 * prevent this we need to mask off the correctable errors on the
5539 * downstream port of the pci-e switch.
5540 */
5541 if (adapter->flags & FLAG_IS_QUAD_PORT) {
5542 struct pci_dev *us_dev = pdev->bus->self;
Jon Mason353064d2011-06-27 07:43:47 +00005543 int pos = pci_pcie_cap(us_dev);
Alexander Duyck005cbdf2008-11-21 16:49:10 -08005544 u16 devctl;
5545
5546 pci_read_config_word(us_dev, pos + PCI_EXP_DEVCTL, &devctl);
5547 pci_write_config_word(us_dev, pos + PCI_EXP_DEVCTL,
5548 (devctl & ~PCI_EXP_DEVCTL_CERE));
5549
Rafael J. Wysocki4f9de722009-04-15 17:43:43 +00005550 e1000_power_off(pdev, sleep, wake);
Alexander Duyck005cbdf2008-11-21 16:49:10 -08005551
5552 pci_write_config_word(us_dev, pos + PCI_EXP_DEVCTL, devctl);
5553 } else {
Rafael J. Wysocki4f9de722009-04-15 17:43:43 +00005554 e1000_power_off(pdev, sleep, wake);
Alexander Duyck005cbdf2008-11-21 16:49:10 -08005555 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07005556}
5557
Bruce Allan6f461f62010-04-27 03:33:04 +00005558#ifdef CONFIG_PCIEASPM
5559static void __e1000e_disable_aspm(struct pci_dev *pdev, u16 state)
5560{
Yinghai Lu9f728f52011-05-12 17:11:47 -07005561 pci_disable_link_state_locked(pdev, state);
Bruce Allan6f461f62010-04-27 03:33:04 +00005562}
5563#else
5564static void __e1000e_disable_aspm(struct pci_dev *pdev, u16 state)
Auke Kok1eae4eb2007-10-31 15:22:00 -07005565{
5566 int pos;
Bruce Allan6f461f62010-04-27 03:33:04 +00005567 u16 reg16;
Auke Kok1eae4eb2007-10-31 15:22:00 -07005568
5569 /*
Bruce Allan6f461f62010-04-27 03:33:04 +00005570 * Both device and parent should have the same ASPM setting.
5571 * Disable ASPM in downstream component first and then upstream.
Auke Kok1eae4eb2007-10-31 15:22:00 -07005572 */
Bruce Allan6f461f62010-04-27 03:33:04 +00005573 pos = pci_pcie_cap(pdev);
5574 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
5575 reg16 &= ~state;
5576 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
5577
Anton Blanchard0c75ba22010-04-28 21:46:06 +00005578 if (!pdev->bus->self)
5579 return;
5580
Bruce Allan6f461f62010-04-27 03:33:04 +00005581 pos = pci_pcie_cap(pdev->bus->self);
5582 pci_read_config_word(pdev->bus->self, pos + PCI_EXP_LNKCTL, &reg16);
5583 reg16 &= ~state;
5584 pci_write_config_word(pdev->bus->self, pos + PCI_EXP_LNKCTL, reg16);
5585}
5586#endif
Bruce Allan78cd29d2011-03-24 03:09:03 +00005587static void e1000e_disable_aspm(struct pci_dev *pdev, u16 state)
Bruce Allan6f461f62010-04-27 03:33:04 +00005588{
5589 dev_info(&pdev->dev, "Disabling ASPM %s %s\n",
5590 (state & PCIE_LINK_STATE_L0S) ? "L0s" : "",
5591 (state & PCIE_LINK_STATE_L1) ? "L1" : "");
5592
5593 __e1000e_disable_aspm(pdev, state);
Auke Kok1eae4eb2007-10-31 15:22:00 -07005594}
5595
Rafael J. Wysockiaa338602011-02-11 00:06:54 +01005596#ifdef CONFIG_PM
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00005597static bool e1000e_pm_ready(struct e1000_adapter *adapter)
Rafael J. Wysocki4f9de722009-04-15 17:43:43 +00005598{
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00005599 return !!adapter->tx_ring->buffer_info;
Rafael J. Wysocki4f9de722009-04-15 17:43:43 +00005600}
5601
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00005602static int __e1000_resume(struct pci_dev *pdev)
Auke Kokbc7f75f2007-09-17 12:30:59 -07005603{
5604 struct net_device *netdev = pci_get_drvdata(pdev);
5605 struct e1000_adapter *adapter = netdev_priv(netdev);
5606 struct e1000_hw *hw = &adapter->hw;
Bruce Allan78cd29d2011-03-24 03:09:03 +00005607 u16 aspm_disable_flag = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005608 u32 err;
5609
Bruce Allan78cd29d2011-03-24 03:09:03 +00005610 if (adapter->flags2 & FLAG2_DISABLE_ASPM_L0S)
5611 aspm_disable_flag = PCIE_LINK_STATE_L0S;
5612 if (adapter->flags2 & FLAG2_DISABLE_ASPM_L1)
5613 aspm_disable_flag |= PCIE_LINK_STATE_L1;
5614 if (aspm_disable_flag)
5615 e1000e_disable_aspm(pdev, aspm_disable_flag);
5616
Auke Kokbc7f75f2007-09-17 12:30:59 -07005617 pci_set_power_state(pdev, PCI_D0);
5618 pci_restore_state(pdev);
Bruce Allan28b8f042010-01-07 16:30:56 +00005619 pci_save_state(pdev);
Taku Izumi6e4f6f62008-06-20 11:57:02 +09005620
Bruce Allan4662e822008-08-26 18:37:06 -07005621 e1000e_set_interrupt_capability(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005622 if (netif_running(netdev)) {
5623 err = e1000_request_irq(adapter);
5624 if (err)
5625 return err;
5626 }
5627
Bruce Allan99730e42011-05-13 07:19:48 +00005628 if (hw->mac.type == e1000_pch2lan)
5629 e1000_resume_workarounds_pchlan(&adapter->hw);
5630
Auke Kokbc7f75f2007-09-17 12:30:59 -07005631 e1000e_power_up_phy(adapter);
Bruce Allana4f58f52009-06-02 11:29:18 +00005632
5633 /* report the system wakeup cause from S3/S4 */
5634 if (adapter->flags2 & FLAG2_HAS_PHY_WAKEUP) {
5635 u16 phy_data;
5636
5637 e1e_rphy(&adapter->hw, BM_WUS, &phy_data);
5638 if (phy_data) {
5639 e_info("PHY Wakeup cause - %s\n",
5640 phy_data & E1000_WUS_EX ? "Unicast Packet" :
5641 phy_data & E1000_WUS_MC ? "Multicast Packet" :
5642 phy_data & E1000_WUS_BC ? "Broadcast Packet" :
5643 phy_data & E1000_WUS_MAG ? "Magic Packet" :
Jeff Kirsheref456f82011-11-03 11:40:28 +00005644 phy_data & E1000_WUS_LNKC ?
5645 "Link Status Change" : "other");
Bruce Allana4f58f52009-06-02 11:29:18 +00005646 }
5647 e1e_wphy(&adapter->hw, BM_WUS, ~0);
5648 } else {
5649 u32 wus = er32(WUS);
5650 if (wus) {
5651 e_info("MAC Wakeup cause - %s\n",
5652 wus & E1000_WUS_EX ? "Unicast Packet" :
5653 wus & E1000_WUS_MC ? "Multicast Packet" :
5654 wus & E1000_WUS_BC ? "Broadcast Packet" :
5655 wus & E1000_WUS_MAG ? "Magic Packet" :
5656 wus & E1000_WUS_LNKC ? "Link Status Change" :
5657 "other");
5658 }
5659 ew32(WUS, ~0);
5660 }
5661
Auke Kokbc7f75f2007-09-17 12:30:59 -07005662 e1000e_reset(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005663
Bruce Allancd791612010-05-10 14:59:51 +00005664 e1000_init_manageability_pt(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005665
5666 if (netif_running(netdev))
5667 e1000e_up(adapter);
5668
5669 netif_device_attach(netdev);
5670
Bruce Allanad680762008-03-28 09:15:03 -07005671 /*
5672 * If the controller has AMT, do not set DRV_LOAD until the interface
Auke Kokbc7f75f2007-09-17 12:30:59 -07005673 * is up. For all other cases, let the f/w know that the h/w is now
Bruce Allanad680762008-03-28 09:15:03 -07005674 * under the control of the driver.
5675 */
Jesse Brandeburgc43bc57e2008-08-04 17:21:40 -07005676 if (!(adapter->flags & FLAG_HAS_AMT))
Bruce Allan31dbe5b2011-01-06 14:29:52 +00005677 e1000e_get_hw_control(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005678
5679 return 0;
5680}
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00005681
Rafael J. Wysockia0340162010-03-17 23:12:24 -07005682#ifdef CONFIG_PM_SLEEP
5683static int e1000_suspend(struct device *dev)
5684{
5685 struct pci_dev *pdev = to_pci_dev(dev);
5686 int retval;
5687 bool wake;
5688
5689 retval = __e1000_shutdown(pdev, &wake, false);
5690 if (!retval)
5691 e1000_complete_shutdown(pdev, true, wake);
5692
5693 return retval;
5694}
5695
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00005696static int e1000_resume(struct device *dev)
5697{
5698 struct pci_dev *pdev = to_pci_dev(dev);
5699 struct net_device *netdev = pci_get_drvdata(pdev);
5700 struct e1000_adapter *adapter = netdev_priv(netdev);
5701
5702 if (e1000e_pm_ready(adapter))
5703 adapter->idle_check = true;
5704
5705 return __e1000_resume(pdev);
5706}
Rafael J. Wysockia0340162010-03-17 23:12:24 -07005707#endif /* CONFIG_PM_SLEEP */
5708
5709#ifdef CONFIG_PM_RUNTIME
5710static int e1000_runtime_suspend(struct device *dev)
5711{
5712 struct pci_dev *pdev = to_pci_dev(dev);
5713 struct net_device *netdev = pci_get_drvdata(pdev);
5714 struct e1000_adapter *adapter = netdev_priv(netdev);
5715
5716 if (e1000e_pm_ready(adapter)) {
5717 bool wake;
5718
5719 __e1000_shutdown(pdev, &wake, true);
5720 }
5721
5722 return 0;
5723}
5724
5725static int e1000_idle(struct device *dev)
5726{
5727 struct pci_dev *pdev = to_pci_dev(dev);
5728 struct net_device *netdev = pci_get_drvdata(pdev);
5729 struct e1000_adapter *adapter = netdev_priv(netdev);
5730
5731 if (!e1000e_pm_ready(adapter))
5732 return 0;
5733
5734 if (adapter->idle_check) {
5735 adapter->idle_check = false;
5736 if (!e1000e_has_link(adapter))
5737 pm_schedule_suspend(dev, MSEC_PER_SEC);
5738 }
5739
5740 return -EBUSY;
5741}
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00005742
5743static int e1000_runtime_resume(struct device *dev)
5744{
5745 struct pci_dev *pdev = to_pci_dev(dev);
5746 struct net_device *netdev = pci_get_drvdata(pdev);
5747 struct e1000_adapter *adapter = netdev_priv(netdev);
5748
5749 if (!e1000e_pm_ready(adapter))
5750 return 0;
5751
5752 adapter->idle_check = !dev->power.runtime_auto;
5753 return __e1000_resume(pdev);
5754}
Rafael J. Wysockia0340162010-03-17 23:12:24 -07005755#endif /* CONFIG_PM_RUNTIME */
Rafael J. Wysockiaa338602011-02-11 00:06:54 +01005756#endif /* CONFIG_PM */
Auke Kokbc7f75f2007-09-17 12:30:59 -07005757
5758static void e1000_shutdown(struct pci_dev *pdev)
5759{
Rafael J. Wysocki4f9de722009-04-15 17:43:43 +00005760 bool wake = false;
5761
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00005762 __e1000_shutdown(pdev, &wake, false);
Rafael J. Wysocki4f9de722009-04-15 17:43:43 +00005763
5764 if (system_state == SYSTEM_POWER_OFF)
5765 e1000_complete_shutdown(pdev, false, wake);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005766}
5767
5768#ifdef CONFIG_NET_POLL_CONTROLLER
Dongdong Deng147b2c82010-11-16 19:50:15 -08005769
5770static irqreturn_t e1000_intr_msix(int irq, void *data)
5771{
5772 struct net_device *netdev = data;
5773 struct e1000_adapter *adapter = netdev_priv(netdev);
Dongdong Deng147b2c82010-11-16 19:50:15 -08005774
5775 if (adapter->msix_entries) {
Bruce Allan90da0662011-01-06 07:02:53 +00005776 int vector, msix_irq;
5777
Dongdong Deng147b2c82010-11-16 19:50:15 -08005778 vector = 0;
5779 msix_irq = adapter->msix_entries[vector].vector;
5780 disable_irq(msix_irq);
5781 e1000_intr_msix_rx(msix_irq, netdev);
5782 enable_irq(msix_irq);
5783
5784 vector++;
5785 msix_irq = adapter->msix_entries[vector].vector;
5786 disable_irq(msix_irq);
5787 e1000_intr_msix_tx(msix_irq, netdev);
5788 enable_irq(msix_irq);
5789
5790 vector++;
5791 msix_irq = adapter->msix_entries[vector].vector;
5792 disable_irq(msix_irq);
5793 e1000_msix_other(msix_irq, netdev);
5794 enable_irq(msix_irq);
5795 }
5796
5797 return IRQ_HANDLED;
5798}
5799
Auke Kokbc7f75f2007-09-17 12:30:59 -07005800/*
5801 * Polling 'interrupt' - used by things like netconsole to send skbs
5802 * without having to re-enable interrupts. It's not called while
5803 * the interrupt routine is executing.
5804 */
5805static void e1000_netpoll(struct net_device *netdev)
5806{
5807 struct e1000_adapter *adapter = netdev_priv(netdev);
5808
Dongdong Deng147b2c82010-11-16 19:50:15 -08005809 switch (adapter->int_mode) {
5810 case E1000E_INT_MODE_MSIX:
5811 e1000_intr_msix(adapter->pdev->irq, netdev);
5812 break;
5813 case E1000E_INT_MODE_MSI:
5814 disable_irq(adapter->pdev->irq);
5815 e1000_intr_msi(adapter->pdev->irq, netdev);
5816 enable_irq(adapter->pdev->irq);
5817 break;
5818 default: /* E1000E_INT_MODE_LEGACY */
5819 disable_irq(adapter->pdev->irq);
5820 e1000_intr(adapter->pdev->irq, netdev);
5821 enable_irq(adapter->pdev->irq);
5822 break;
5823 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07005824}
5825#endif
5826
5827/**
5828 * e1000_io_error_detected - called when PCI error is detected
5829 * @pdev: Pointer to PCI device
5830 * @state: The current pci connection state
5831 *
5832 * This function is called after a PCI bus error affecting
5833 * this device has been detected.
5834 */
5835static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
5836 pci_channel_state_t state)
5837{
5838 struct net_device *netdev = pci_get_drvdata(pdev);
5839 struct e1000_adapter *adapter = netdev_priv(netdev);
5840
5841 netif_device_detach(netdev);
5842
Mike Masonc93b5a72009-06-30 12:45:53 +00005843 if (state == pci_channel_io_perm_failure)
5844 return PCI_ERS_RESULT_DISCONNECT;
5845
Auke Kokbc7f75f2007-09-17 12:30:59 -07005846 if (netif_running(netdev))
5847 e1000e_down(adapter);
5848 pci_disable_device(pdev);
5849
5850 /* Request a slot slot reset. */
5851 return PCI_ERS_RESULT_NEED_RESET;
5852}
5853
5854/**
5855 * e1000_io_slot_reset - called after the pci bus has been reset.
5856 * @pdev: Pointer to PCI device
5857 *
5858 * Restart the card from scratch, as if from a cold-boot. Implementation
5859 * resembles the first-half of the e1000_resume routine.
5860 */
5861static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
5862{
5863 struct net_device *netdev = pci_get_drvdata(pdev);
5864 struct e1000_adapter *adapter = netdev_priv(netdev);
5865 struct e1000_hw *hw = &adapter->hw;
Bruce Allan78cd29d2011-03-24 03:09:03 +00005866 u16 aspm_disable_flag = 0;
Taku Izumi6e4f6f62008-06-20 11:57:02 +09005867 int err;
Jesse Brandeburg111b9dc2009-02-10 12:51:20 +00005868 pci_ers_result_t result;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005869
Bruce Allan78cd29d2011-03-24 03:09:03 +00005870 if (adapter->flags2 & FLAG2_DISABLE_ASPM_L0S)
5871 aspm_disable_flag = PCIE_LINK_STATE_L0S;
Bruce Allan6f461f62010-04-27 03:33:04 +00005872 if (adapter->flags2 & FLAG2_DISABLE_ASPM_L1)
Bruce Allan78cd29d2011-03-24 03:09:03 +00005873 aspm_disable_flag |= PCIE_LINK_STATE_L1;
5874 if (aspm_disable_flag)
5875 e1000e_disable_aspm(pdev, aspm_disable_flag);
5876
Bruce Allanf0f422e2008-08-04 17:21:53 -07005877 err = pci_enable_device_mem(pdev);
Taku Izumi6e4f6f62008-06-20 11:57:02 +09005878 if (err) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07005879 dev_err(&pdev->dev,
5880 "Cannot re-enable PCI device after reset.\n");
Jesse Brandeburg111b9dc2009-02-10 12:51:20 +00005881 result = PCI_ERS_RESULT_DISCONNECT;
5882 } else {
5883 pci_set_master(pdev);
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00005884 pdev->state_saved = true;
Jesse Brandeburg111b9dc2009-02-10 12:51:20 +00005885 pci_restore_state(pdev);
5886
5887 pci_enable_wake(pdev, PCI_D3hot, 0);
5888 pci_enable_wake(pdev, PCI_D3cold, 0);
5889
5890 e1000e_reset(adapter);
5891 ew32(WUS, ~0);
5892 result = PCI_ERS_RESULT_RECOVERED;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005893 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07005894
Jesse Brandeburg111b9dc2009-02-10 12:51:20 +00005895 pci_cleanup_aer_uncorrect_error_status(pdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005896
Jesse Brandeburg111b9dc2009-02-10 12:51:20 +00005897 return result;
Auke Kokbc7f75f2007-09-17 12:30:59 -07005898}
5899
5900/**
5901 * e1000_io_resume - called when traffic can start flowing again.
5902 * @pdev: Pointer to PCI device
5903 *
5904 * This callback is called when the error recovery driver tells us that
5905 * its OK to resume normal operation. Implementation resembles the
5906 * second-half of the e1000_resume routine.
5907 */
5908static void e1000_io_resume(struct pci_dev *pdev)
5909{
5910 struct net_device *netdev = pci_get_drvdata(pdev);
5911 struct e1000_adapter *adapter = netdev_priv(netdev);
5912
Bruce Allancd791612010-05-10 14:59:51 +00005913 e1000_init_manageability_pt(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005914
5915 if (netif_running(netdev)) {
5916 if (e1000e_up(adapter)) {
5917 dev_err(&pdev->dev,
5918 "can't bring device back up after reset\n");
5919 return;
5920 }
5921 }
5922
5923 netif_device_attach(netdev);
5924
Bruce Allanad680762008-03-28 09:15:03 -07005925 /*
5926 * If the controller has AMT, do not set DRV_LOAD until the interface
Auke Kokbc7f75f2007-09-17 12:30:59 -07005927 * is up. For all other cases, let the f/w know that the h/w is now
Bruce Allanad680762008-03-28 09:15:03 -07005928 * under the control of the driver.
5929 */
Jesse Brandeburgc43bc57e2008-08-04 17:21:40 -07005930 if (!(adapter->flags & FLAG_HAS_AMT))
Bruce Allan31dbe5b2011-01-06 14:29:52 +00005931 e1000e_get_hw_control(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005932
5933}
5934
5935static void e1000_print_device_info(struct e1000_adapter *adapter)
5936{
5937 struct e1000_hw *hw = &adapter->hw;
5938 struct net_device *netdev = adapter->netdev;
Bruce Allan073287c2010-11-24 06:01:51 +00005939 u32 ret_val;
5940 u8 pba_str[E1000_PBANUM_LENGTH];
Auke Kokbc7f75f2007-09-17 12:30:59 -07005941
5942 /* print bus type/speed/width info */
Bruce Allana5cc7642011-03-19 00:31:23 +00005943 e_info("(PCI Express:2.5GT/s:%s) %pM\n",
Jeff Kirsher44defeb2008-08-04 17:20:41 -07005944 /* bus width */
5945 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
5946 "Width x1"),
5947 /* MAC address */
Johannes Berg7c510e42008-10-27 17:47:26 -07005948 netdev->dev_addr);
Jeff Kirsher44defeb2008-08-04 17:20:41 -07005949 e_info("Intel(R) PRO/%s Network Connection\n",
5950 (hw->phy.type == e1000_phy_ife) ? "10/100" : "1000");
Bruce Allan073287c2010-11-24 06:01:51 +00005951 ret_val = e1000_read_pba_string_generic(hw, pba_str,
5952 E1000_PBANUM_LENGTH);
5953 if (ret_val)
Bruce Allanf2315bf2011-12-16 00:46:59 +00005954 strlcpy((char *)pba_str, "Unknown", sizeof(pba_str));
Bruce Allan073287c2010-11-24 06:01:51 +00005955 e_info("MAC: %d, PHY: %d, PBA No: %s\n",
5956 hw->mac.type, hw->phy.type, pba_str);
Auke Kokbc7f75f2007-09-17 12:30:59 -07005957}
5958
Auke Kok10aa4c02008-08-04 17:21:20 -07005959static void e1000_eeprom_checks(struct e1000_adapter *adapter)
5960{
5961 struct e1000_hw *hw = &adapter->hw;
5962 int ret_val;
5963 u16 buf = 0;
5964
5965 if (hw->mac.type != e1000_82573)
5966 return;
5967
5968 ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &buf);
Bruce Allane885d762012-01-31 06:37:32 +00005969 le16_to_cpus(&buf);
5970 if (!ret_val && (!(buf & (1 << 0)))) {
Auke Kok10aa4c02008-08-04 17:21:20 -07005971 /* Deep Smart Power Down (DSPD) */
Frans Pop6c2a9ef2008-09-22 14:52:22 -07005972 dev_warn(&adapter->pdev->dev,
5973 "Warning: detected DSPD enabled in EEPROM\n");
Auke Kok10aa4c02008-08-04 17:21:20 -07005974 }
Auke Kok10aa4c02008-08-04 17:21:20 -07005975}
5976
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005977static int e1000_set_features(struct net_device *netdev,
Bruce Allan70495a52012-01-11 01:26:50 +00005978 netdev_features_t features)
Bruce Allandc221292011-08-19 03:23:48 +00005979{
5980 struct e1000_adapter *adapter = netdev_priv(netdev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005981 netdev_features_t changed = features ^ netdev->features;
Bruce Allandc221292011-08-19 03:23:48 +00005982
5983 if (changed & (NETIF_F_TSO | NETIF_F_TSO6))
5984 adapter->flags |= FLAG_TSO_FORCE;
5985
5986 if (!(changed & (NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX |
Bruce Allan70495a52012-01-11 01:26:50 +00005987 NETIF_F_RXCSUM | NETIF_F_RXHASH)))
Bruce Allandc221292011-08-19 03:23:48 +00005988 return 0;
5989
Bruce Allan70495a52012-01-11 01:26:50 +00005990 /*
5991 * IP payload checksum (enabled with jumbos/packet-split when Rx
5992 * checksum is enabled) and generation of RSS hash is mutually
5993 * exclusive in the hardware.
5994 */
5995 if (adapter->rx_ps_pages &&
5996 (features & NETIF_F_RXCSUM) && (features & NETIF_F_RXHASH)) {
5997 e_err("Enabling both receive checksum offload and receive hashing is not possible with jumbo frames. Disable jumbos or enable only one of the receive offload features.\n");
5998 return -EINVAL;
5999 }
6000
6001 netdev->features = features;
6002
Bruce Allandc221292011-08-19 03:23:48 +00006003 if (netif_running(netdev))
6004 e1000e_reinit_locked(adapter);
6005 else
6006 e1000e_reset(adapter);
6007
6008 return 0;
6009}
6010
Stephen Hemminger651c2462008-11-19 21:57:48 -08006011static const struct net_device_ops e1000e_netdev_ops = {
6012 .ndo_open = e1000_open,
6013 .ndo_stop = e1000_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08006014 .ndo_start_xmit = e1000_xmit_frame,
Jeff Kirsher67fd4fc2011-01-07 05:12:09 +00006015 .ndo_get_stats64 = e1000e_get_stats64,
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00006016 .ndo_set_rx_mode = e1000e_set_rx_mode,
Stephen Hemminger651c2462008-11-19 21:57:48 -08006017 .ndo_set_mac_address = e1000_set_mac,
6018 .ndo_change_mtu = e1000_change_mtu,
6019 .ndo_do_ioctl = e1000_ioctl,
6020 .ndo_tx_timeout = e1000_tx_timeout,
6021 .ndo_validate_addr = eth_validate_addr,
6022
Stephen Hemminger651c2462008-11-19 21:57:48 -08006023 .ndo_vlan_rx_add_vid = e1000_vlan_rx_add_vid,
6024 .ndo_vlan_rx_kill_vid = e1000_vlan_rx_kill_vid,
6025#ifdef CONFIG_NET_POLL_CONTROLLER
6026 .ndo_poll_controller = e1000_netpoll,
6027#endif
Bruce Allandc221292011-08-19 03:23:48 +00006028 .ndo_set_features = e1000_set_features,
Stephen Hemminger651c2462008-11-19 21:57:48 -08006029};
6030
Auke Kokbc7f75f2007-09-17 12:30:59 -07006031/**
6032 * e1000_probe - Device Initialization Routine
6033 * @pdev: PCI device information struct
6034 * @ent: entry in e1000_pci_tbl
6035 *
6036 * Returns 0 on success, negative on failure
6037 *
6038 * e1000_probe initializes an adapter identified by a pci_dev structure.
6039 * The OS initialization, configuring of the adapter private structure,
6040 * and a hardware reset occur.
6041 **/
6042static int __devinit e1000_probe(struct pci_dev *pdev,
6043 const struct pci_device_id *ent)
6044{
6045 struct net_device *netdev;
6046 struct e1000_adapter *adapter;
6047 struct e1000_hw *hw;
6048 const struct e1000_info *ei = e1000_info_tbl[ent->driver_data];
Becky Brucef47e81f2008-05-01 18:03:11 -05006049 resource_size_t mmio_start, mmio_len;
6050 resource_size_t flash_start, flash_len;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006051
6052 static int cards_found;
Bruce Allan78cd29d2011-03-24 03:09:03 +00006053 u16 aspm_disable_flag = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006054 int i, err, pci_using_dac;
6055 u16 eeprom_data = 0;
6056 u16 eeprom_apme_mask = E1000_EEPROM_APME;
6057
Bruce Allan78cd29d2011-03-24 03:09:03 +00006058 if (ei->flags2 & FLAG2_DISABLE_ASPM_L0S)
6059 aspm_disable_flag = PCIE_LINK_STATE_L0S;
Bruce Allan6f461f62010-04-27 03:33:04 +00006060 if (ei->flags2 & FLAG2_DISABLE_ASPM_L1)
Bruce Allan78cd29d2011-03-24 03:09:03 +00006061 aspm_disable_flag |= PCIE_LINK_STATE_L1;
6062 if (aspm_disable_flag)
6063 e1000e_disable_aspm(pdev, aspm_disable_flag);
Taku Izumi6e4f6f62008-06-20 11:57:02 +09006064
Bruce Allanf0f422e2008-08-04 17:21:53 -07006065 err = pci_enable_device_mem(pdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006066 if (err)
6067 return err;
6068
6069 pci_using_dac = 0;
Nick Nunley0be3f552010-04-27 13:09:05 +00006070 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kokbc7f75f2007-09-17 12:30:59 -07006071 if (!err) {
Nick Nunley0be3f552010-04-27 13:09:05 +00006072 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kokbc7f75f2007-09-17 12:30:59 -07006073 if (!err)
6074 pci_using_dac = 1;
6075 } else {
Nick Nunley0be3f552010-04-27 13:09:05 +00006076 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kokbc7f75f2007-09-17 12:30:59 -07006077 if (err) {
Nick Nunley0be3f552010-04-27 13:09:05 +00006078 err = dma_set_coherent_mask(&pdev->dev,
6079 DMA_BIT_MASK(32));
Auke Kokbc7f75f2007-09-17 12:30:59 -07006080 if (err) {
Jeff Kirsheref456f82011-11-03 11:40:28 +00006081 dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07006082 goto err_dma;
6083 }
6084 }
6085 }
6086
Arjan van de Vene8de1482008-10-22 19:55:31 -07006087 err = pci_request_selected_regions_exclusive(pdev,
Bruce Allanf0f422e2008-08-04 17:21:53 -07006088 pci_select_bars(pdev, IORESOURCE_MEM),
6089 e1000e_driver_name);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006090 if (err)
6091 goto err_pci_reg;
6092
Xiaotian Feng68eac462009-08-14 14:35:52 +00006093 /* AER (Advanced Error Reporting) hooks */
Frans Pop19d5afd2009-10-02 10:04:12 -07006094 pci_enable_pcie_error_reporting(pdev);
Xiaotian Feng68eac462009-08-14 14:35:52 +00006095
Auke Kokbc7f75f2007-09-17 12:30:59 -07006096 pci_set_master(pdev);
Bruce Allan438b3652008-11-21 16:51:33 -08006097 /* PCI config space info */
6098 err = pci_save_state(pdev);
6099 if (err)
6100 goto err_alloc_etherdev;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006101
6102 err = -ENOMEM;
6103 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
6104 if (!netdev)
6105 goto err_alloc_etherdev;
6106
Auke Kokbc7f75f2007-09-17 12:30:59 -07006107 SET_NETDEV_DEV(netdev, &pdev->dev);
6108
Tom Herbertf85e4df2010-05-05 14:03:32 +00006109 netdev->irq = pdev->irq;
6110
Auke Kokbc7f75f2007-09-17 12:30:59 -07006111 pci_set_drvdata(pdev, netdev);
6112 adapter = netdev_priv(netdev);
6113 hw = &adapter->hw;
6114 adapter->netdev = netdev;
6115 adapter->pdev = pdev;
6116 adapter->ei = ei;
6117 adapter->pba = ei->pba;
6118 adapter->flags = ei->flags;
Jeff Kirshereb7c3ad2008-11-14 06:45:23 +00006119 adapter->flags2 = ei->flags2;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006120 adapter->hw.adapter = adapter;
6121 adapter->hw.mac.type = ei->mac;
Bruce Allan2adc55c2009-06-02 11:28:58 +00006122 adapter->max_hw_frame_size = ei->max_hw_frame_size;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006123 adapter->msg_enable = (1 << NETIF_MSG_DRV | NETIF_MSG_PROBE) - 1;
6124
6125 mmio_start = pci_resource_start(pdev, 0);
6126 mmio_len = pci_resource_len(pdev, 0);
6127
6128 err = -EIO;
6129 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
6130 if (!adapter->hw.hw_addr)
6131 goto err_ioremap;
6132
6133 if ((adapter->flags & FLAG_HAS_FLASH) &&
6134 (pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
6135 flash_start = pci_resource_start(pdev, 1);
6136 flash_len = pci_resource_len(pdev, 1);
6137 adapter->hw.flash_address = ioremap(flash_start, flash_len);
6138 if (!adapter->hw.flash_address)
6139 goto err_flashmap;
6140 }
6141
6142 /* construct the net_device struct */
Stephen Hemminger651c2462008-11-19 21:57:48 -08006143 netdev->netdev_ops = &e1000e_netdev_ops;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006144 e1000e_set_ethtool_ops(netdev);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006145 netdev->watchdog_timeo = 5 * HZ;
6146 netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
Bruce Allanf2315bf2011-12-16 00:46:59 +00006147 strlcpy(netdev->name, pci_name(pdev), sizeof(netdev->name));
Auke Kokbc7f75f2007-09-17 12:30:59 -07006148
6149 netdev->mem_start = mmio_start;
6150 netdev->mem_end = mmio_start + mmio_len;
6151
6152 adapter->bd_number = cards_found++;
6153
Bruce Allan4662e822008-08-26 18:37:06 -07006154 e1000e_check_options(adapter);
6155
Auke Kokbc7f75f2007-09-17 12:30:59 -07006156 /* setup adapter struct */
6157 err = e1000_sw_init(adapter);
6158 if (err)
6159 goto err_sw_init;
6160
Auke Kokbc7f75f2007-09-17 12:30:59 -07006161 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
6162 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
6163 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
6164
Jeff Kirsher69e3fd82008-04-02 13:48:18 -07006165 err = ei->get_variants(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006166 if (err)
6167 goto err_hw_init;
6168
Bruce Allan4a770352008-10-01 17:18:35 -07006169 if ((adapter->flags & FLAG_IS_ICH) &&
6170 (adapter->flags & FLAG_READ_ONLY_NVM))
6171 e1000e_write_protect_nvm_ich8lan(&adapter->hw);
6172
Auke Kokbc7f75f2007-09-17 12:30:59 -07006173 hw->mac.ops.get_bus_info(&adapter->hw);
6174
Jeff Kirsher318a94d2008-03-28 09:15:16 -07006175 adapter->hw.phy.autoneg_wait_to_complete = 0;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006176
6177 /* Copper options */
Jeff Kirsher318a94d2008-03-28 09:15:16 -07006178 if (adapter->hw.phy.media_type == e1000_media_type_copper) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07006179 adapter->hw.phy.mdix = AUTO_ALL_MODES;
6180 adapter->hw.phy.disable_polarity_correction = 0;
6181 adapter->hw.phy.ms_type = e1000_ms_hw_default;
6182 }
6183
6184 if (e1000_check_reset_block(&adapter->hw))
Jeff Kirsher44defeb2008-08-04 17:20:41 -07006185 e_info("PHY reset is blocked due to SOL/IDER session.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07006186
Bruce Allandc221292011-08-19 03:23:48 +00006187 /* Set initial default active device features */
6188 netdev->features = (NETIF_F_SG |
6189 NETIF_F_HW_VLAN_RX |
6190 NETIF_F_HW_VLAN_TX |
6191 NETIF_F_TSO |
6192 NETIF_F_TSO6 |
Bruce Allan70495a52012-01-11 01:26:50 +00006193 NETIF_F_RXHASH |
Bruce Allandc221292011-08-19 03:23:48 +00006194 NETIF_F_RXCSUM |
6195 NETIF_F_HW_CSUM);
6196
6197 /* Set user-changeable features (subset of all device features) */
6198 netdev->hw_features = netdev->features;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006199
6200 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER)
6201 netdev->features |= NETIF_F_HW_VLAN_FILTER;
6202
Bruce Allandc221292011-08-19 03:23:48 +00006203 netdev->vlan_features |= (NETIF_F_SG |
6204 NETIF_F_TSO |
6205 NETIF_F_TSO6 |
6206 NETIF_F_HW_CSUM);
Jeff Kirshera5136e22008-06-05 04:07:28 -07006207
Jesse Brandeburgef9b9652011-11-04 05:47:06 +00006208 netdev->priv_flags |= IFF_UNICAST_FLT;
6209
Yi Zou7b872a52010-09-22 17:57:58 +00006210 if (pci_using_dac) {
Auke Kokbc7f75f2007-09-17 12:30:59 -07006211 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00006212 netdev->vlan_features |= NETIF_F_HIGHDMA;
6213 }
Auke Kokbc7f75f2007-09-17 12:30:59 -07006214
Auke Kokbc7f75f2007-09-17 12:30:59 -07006215 if (e1000e_enable_mng_pass_thru(&adapter->hw))
6216 adapter->flags |= FLAG_MNG_PT_ENABLED;
6217
Bruce Allanad680762008-03-28 09:15:03 -07006218 /*
6219 * before reading the NVM, reset the controller to
6220 * put the device in a known good starting state
6221 */
Auke Kokbc7f75f2007-09-17 12:30:59 -07006222 adapter->hw.mac.ops.reset_hw(&adapter->hw);
6223
6224 /*
6225 * systems with ASPM and others may see the checksum fail on the first
6226 * attempt. Let's give it a few tries
6227 */
6228 for (i = 0;; i++) {
6229 if (e1000_validate_nvm_checksum(&adapter->hw) >= 0)
6230 break;
6231 if (i == 2) {
Jeff Kirsher44defeb2008-08-04 17:20:41 -07006232 e_err("The NVM Checksum Is Not Valid\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07006233 err = -EIO;
6234 goto err_eeprom;
6235 }
6236 }
6237
Auke Kok10aa4c02008-08-04 17:21:20 -07006238 e1000_eeprom_checks(adapter);
6239
Bruce Allan608f8a02010-01-13 02:04:58 +00006240 /* copy the MAC address */
Auke Kokbc7f75f2007-09-17 12:30:59 -07006241 if (e1000e_read_mac_addr(&adapter->hw))
Jeff Kirsher44defeb2008-08-04 17:20:41 -07006242 e_err("NVM Read Error while reading MAC address\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07006243
6244 memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
6245 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
6246
6247 if (!is_valid_ether_addr(netdev->perm_addr)) {
Johannes Berg7c510e42008-10-27 17:47:26 -07006248 e_err("Invalid MAC Address: %pM\n", netdev->perm_addr);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006249 err = -EIO;
6250 goto err_eeprom;
6251 }
6252
6253 init_timer(&adapter->watchdog_timer);
Joe Perchesc061b182010-08-23 18:20:03 +00006254 adapter->watchdog_timer.function = e1000_watchdog;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006255 adapter->watchdog_timer.data = (unsigned long) adapter;
6256
6257 init_timer(&adapter->phy_info_timer);
Joe Perchesc061b182010-08-23 18:20:03 +00006258 adapter->phy_info_timer.function = e1000_update_phy_info;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006259 adapter->phy_info_timer.data = (unsigned long) adapter;
6260
6261 INIT_WORK(&adapter->reset_task, e1000_reset_task);
6262 INIT_WORK(&adapter->watchdog_task, e1000_watchdog_task);
Jesse Brandeburga8f88ff2008-10-02 16:33:25 -07006263 INIT_WORK(&adapter->downshift_task, e1000e_downshift_workaround);
6264 INIT_WORK(&adapter->update_phy_task, e1000e_update_phy_task);
Bruce Allan41cec6f2009-11-20 23:28:56 +00006265 INIT_WORK(&adapter->print_hang_task, e1000_print_hw_hang);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006266
Auke Kokbc7f75f2007-09-17 12:30:59 -07006267 /* Initialize link parameters. User can change them with ethtool */
6268 adapter->hw.mac.autoneg = 1;
Rusty Russell3db1cd52011-12-19 13:56:45 +00006269 adapter->fc_autoneg = true;
Bruce Allan5c48ef3e22008-11-21 16:57:36 -08006270 adapter->hw.fc.requested_mode = e1000_fc_default;
6271 adapter->hw.fc.current_mode = e1000_fc_default;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006272 adapter->hw.phy.autoneg_advertised = 0x2f;
6273
6274 /* ring size defaults */
6275 adapter->rx_ring->count = 256;
6276 adapter->tx_ring->count = 256;
6277
6278 /*
6279 * Initial Wake on LAN setting - If APM wake is enabled in
6280 * the EEPROM, enable the ACPI Magic Packet filter
6281 */
6282 if (adapter->flags & FLAG_APME_IN_WUC) {
6283 /* APME bit in EEPROM is mapped to WUC.APME */
6284 eeprom_data = er32(WUC);
6285 eeprom_apme_mask = E1000_WUC_APME;
Bruce Allan4def99b2011-02-02 09:30:36 +00006286 if ((hw->mac.type > e1000_ich10lan) &&
6287 (eeprom_data & E1000_WUC_PHY_WAKE))
Bruce Allana4f58f52009-06-02 11:29:18 +00006288 adapter->flags2 |= FLAG2_HAS_PHY_WAKEUP;
Auke Kokbc7f75f2007-09-17 12:30:59 -07006289 } else if (adapter->flags & FLAG_APME_IN_CTRL3) {
6290 if (adapter->flags & FLAG_APME_CHECK_PORT_B &&
6291 (adapter->hw.bus.func == 1))
6292 e1000_read_nvm(&adapter->hw,
6293 NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
6294 else
6295 e1000_read_nvm(&adapter->hw,
6296 NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
6297 }
6298
6299 /* fetch WoL from EEPROM */
6300 if (eeprom_data & eeprom_apme_mask)
6301 adapter->eeprom_wol |= E1000_WUFC_MAG;
6302
6303 /*
6304 * now that we have the eeprom settings, apply the special cases
6305 * where the eeprom may be wrong or the board simply won't support
6306 * wake on lan on a particular port
6307 */
6308 if (!(adapter->flags & FLAG_HAS_WOL))
6309 adapter->eeprom_wol = 0;
6310
6311 /* initialize the wol settings based on the eeprom settings */
6312 adapter->wol = adapter->eeprom_wol;
\"Rafael J. Wysocki\6ff68022008-11-12 09:52:32 +00006313 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006314
Bruce Allan84527592008-11-21 17:00:22 -08006315 /* save off EEPROM version number */
6316 e1000_read_nvm(&adapter->hw, 5, 1, &adapter->eeprom_vers);
6317
Auke Kokbc7f75f2007-09-17 12:30:59 -07006318 /* reset the hardware with the new settings */
6319 e1000e_reset(adapter);
6320
Bruce Allanad680762008-03-28 09:15:03 -07006321 /*
6322 * If the controller has AMT, do not set DRV_LOAD until the interface
Auke Kokbc7f75f2007-09-17 12:30:59 -07006323 * is up. For all other cases, let the f/w know that the h/w is now
Bruce Allanad680762008-03-28 09:15:03 -07006324 * under the control of the driver.
6325 */
Jesse Brandeburgc43bc57e2008-08-04 17:21:40 -07006326 if (!(adapter->flags & FLAG_HAS_AMT))
Bruce Allan31dbe5b2011-01-06 14:29:52 +00006327 e1000e_get_hw_control(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006328
Bruce Allanf2315bf2011-12-16 00:46:59 +00006329 strlcpy(netdev->name, "eth%d", sizeof(netdev->name));
Auke Kokbc7f75f2007-09-17 12:30:59 -07006330 err = register_netdev(netdev);
6331 if (err)
6332 goto err_register;
6333
Jesse Brandeburg9c563d22009-04-17 20:44:34 +00006334 /* carrier off reporting is important to ethtool even BEFORE open */
6335 netif_carrier_off(netdev);
6336
Auke Kokbc7f75f2007-09-17 12:30:59 -07006337 e1000_print_device_info(adapter);
6338
Alan Sternf3ec4f82010-06-08 15:23:51 -04006339 if (pci_dev_run_wake(pdev))
6340 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00006341
Auke Kokbc7f75f2007-09-17 12:30:59 -07006342 return 0;
6343
6344err_register:
Jesse Brandeburgc43bc57e2008-08-04 17:21:40 -07006345 if (!(adapter->flags & FLAG_HAS_AMT))
Bruce Allan31dbe5b2011-01-06 14:29:52 +00006346 e1000e_release_hw_control(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006347err_eeprom:
6348 if (!e1000_check_reset_block(&adapter->hw))
6349 e1000_phy_hw_reset(&adapter->hw);
Jesse Brandeburgc43bc57e2008-08-04 17:21:40 -07006350err_hw_init:
Auke Kokbc7f75f2007-09-17 12:30:59 -07006351 kfree(adapter->tx_ring);
6352 kfree(adapter->rx_ring);
6353err_sw_init:
Jesse Brandeburgc43bc57e2008-08-04 17:21:40 -07006354 if (adapter->hw.flash_address)
6355 iounmap(adapter->hw.flash_address);
Jeff Kirshere82f54b2008-11-14 06:45:07 +00006356 e1000e_reset_interrupt_capability(adapter);
Jesse Brandeburgc43bc57e2008-08-04 17:21:40 -07006357err_flashmap:
Auke Kokbc7f75f2007-09-17 12:30:59 -07006358 iounmap(adapter->hw.hw_addr);
6359err_ioremap:
6360 free_netdev(netdev);
6361err_alloc_etherdev:
Bruce Allanf0f422e2008-08-04 17:21:53 -07006362 pci_release_selected_regions(pdev,
6363 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kokbc7f75f2007-09-17 12:30:59 -07006364err_pci_reg:
6365err_dma:
6366 pci_disable_device(pdev);
6367 return err;
6368}
6369
6370/**
6371 * e1000_remove - Device Removal Routine
6372 * @pdev: PCI device information struct
6373 *
6374 * e1000_remove is called by the PCI subsystem to alert the driver
6375 * that it should release a PCI device. The could be caused by a
6376 * Hot-Plug event, or because the driver is going to be removed from
6377 * memory.
6378 **/
6379static void __devexit e1000_remove(struct pci_dev *pdev)
6380{
6381 struct net_device *netdev = pci_get_drvdata(pdev);
6382 struct e1000_adapter *adapter = netdev_priv(netdev);
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00006383 bool down = test_bit(__E1000_DOWN, &adapter->state);
6384
Bruce Allanad680762008-03-28 09:15:03 -07006385 /*
Tejun Heo23f333a2010-12-12 16:45:14 +01006386 * The timers may be rescheduled, so explicitly disable them
6387 * from being rescheduled.
Bruce Allanad680762008-03-28 09:15:03 -07006388 */
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00006389 if (!down)
6390 set_bit(__E1000_DOWN, &adapter->state);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006391 del_timer_sync(&adapter->watchdog_timer);
6392 del_timer_sync(&adapter->phy_info_timer);
6393
Bruce Allan41cec6f2009-11-20 23:28:56 +00006394 cancel_work_sync(&adapter->reset_task);
6395 cancel_work_sync(&adapter->watchdog_task);
6396 cancel_work_sync(&adapter->downshift_task);
6397 cancel_work_sync(&adapter->update_phy_task);
6398 cancel_work_sync(&adapter->print_hang_task);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006399
Bruce Allan17f208d2009-12-01 15:47:22 +00006400 if (!(netdev->flags & IFF_UP))
6401 e1000_power_down_phy(adapter);
6402
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00006403 /* Don't lie to e1000_close() down the road. */
6404 if (!down)
6405 clear_bit(__E1000_DOWN, &adapter->state);
Bruce Allan17f208d2009-12-01 15:47:22 +00006406 unregister_netdev(netdev);
6407
Alan Sternf3ec4f82010-06-08 15:23:51 -04006408 if (pci_dev_run_wake(pdev))
6409 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00006410
Bruce Allanad680762008-03-28 09:15:03 -07006411 /*
6412 * Release control of h/w to f/w. If f/w is AMT enabled, this
6413 * would have already happened in close and is redundant.
6414 */
Bruce Allan31dbe5b2011-01-06 14:29:52 +00006415 e1000e_release_hw_control(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006416
Bruce Allan4662e822008-08-26 18:37:06 -07006417 e1000e_reset_interrupt_capability(adapter);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006418 kfree(adapter->tx_ring);
6419 kfree(adapter->rx_ring);
6420
6421 iounmap(adapter->hw.hw_addr);
6422 if (adapter->hw.flash_address)
6423 iounmap(adapter->hw.flash_address);
Bruce Allanf0f422e2008-08-04 17:21:53 -07006424 pci_release_selected_regions(pdev,
6425 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kokbc7f75f2007-09-17 12:30:59 -07006426
6427 free_netdev(netdev);
6428
Jesse Brandeburg111b9dc2009-02-10 12:51:20 +00006429 /* AER disable */
Frans Pop19d5afd2009-10-02 10:04:12 -07006430 pci_disable_pcie_error_reporting(pdev);
Jesse Brandeburg111b9dc2009-02-10 12:51:20 +00006431
Auke Kokbc7f75f2007-09-17 12:30:59 -07006432 pci_disable_device(pdev);
6433}
6434
6435/* PCI Error Recovery (ERS) */
6436static struct pci_error_handlers e1000_err_handler = {
6437 .error_detected = e1000_io_error_detected,
6438 .slot_reset = e1000_io_slot_reset,
6439 .resume = e1000_io_resume,
6440};
6441
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00006442static DEFINE_PCI_DEVICE_TABLE(e1000_pci_tbl) = {
Auke Kokbc7f75f2007-09-17 12:30:59 -07006443 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_COPPER), board_82571 },
6444 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_FIBER), board_82571 },
6445 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER), board_82571 },
6446 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER_LP), board_82571 },
6447 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_FIBER), board_82571 },
6448 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES), board_82571 },
Auke Kok040babf2007-10-31 15:22:05 -07006449 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_DUAL), board_82571 },
6450 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_QUAD), board_82571 },
6451 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571PT_QUAD_COPPER), board_82571 },
Bruce Allanad680762008-03-28 09:15:03 -07006452
Auke Kokbc7f75f2007-09-17 12:30:59 -07006453 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI), board_82572 },
6454 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_COPPER), board_82572 },
6455 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_FIBER), board_82572 },
6456 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_SERDES), board_82572 },
Bruce Allanad680762008-03-28 09:15:03 -07006457
Auke Kokbc7f75f2007-09-17 12:30:59 -07006458 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E), board_82573 },
6459 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E_IAMT), board_82573 },
6460 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573L), board_82573 },
Bruce Allanad680762008-03-28 09:15:03 -07006461
Bruce Allan4662e822008-08-26 18:37:06 -07006462 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82574L), board_82574 },
Bruce Allanbef28b12009-03-24 23:28:02 -07006463 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82574LA), board_82574 },
Alexander Duyck8c81c9c2009-03-19 01:12:27 +00006464 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82583V), board_82583 },
Bruce Allan4662e822008-08-26 18:37:06 -07006465
Auke Kokbc7f75f2007-09-17 12:30:59 -07006466 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_DPT),
6467 board_80003es2lan },
6468 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_SPT),
6469 board_80003es2lan },
6470 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_DPT),
6471 board_80003es2lan },
6472 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_SPT),
6473 board_80003es2lan },
Bruce Allanad680762008-03-28 09:15:03 -07006474
Auke Kokbc7f75f2007-09-17 12:30:59 -07006475 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE), board_ich8lan },
6476 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_G), board_ich8lan },
6477 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_GT), board_ich8lan },
6478 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_AMT), board_ich8lan },
6479 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_C), board_ich8lan },
6480 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M), board_ich8lan },
6481 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M_AMT), board_ich8lan },
Bruce Allan9e135a22009-12-01 15:50:31 +00006482 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_82567V_3), board_ich8lan },
Bruce Allanad680762008-03-28 09:15:03 -07006483
Auke Kokbc7f75f2007-09-17 12:30:59 -07006484 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE), board_ich9lan },
6485 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_G), board_ich9lan },
6486 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_GT), board_ich9lan },
6487 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_AMT), board_ich9lan },
6488 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_C), board_ich9lan },
Bruce Allan2f15f9d2008-08-26 18:36:36 -07006489 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_BM), board_ich9lan },
Bruce Allan97ac8ca2008-04-29 09:16:05 -07006490 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M), board_ich9lan },
6491 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_AMT), board_ich9lan },
6492 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_V), board_ich9lan },
6493
6494 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LM), board_ich9lan },
6495 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LF), board_ich9lan },
6496 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_V), board_ich9lan },
Auke Kokbc7f75f2007-09-17 12:30:59 -07006497
Bruce Allanf4187b52008-08-26 18:36:50 -07006498 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_LM), board_ich10lan },
6499 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_LF), board_ich10lan },
Bruce Allan10df0b92010-05-10 15:02:52 +00006500 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_V), board_ich10lan },
Bruce Allanf4187b52008-08-26 18:36:50 -07006501
Bruce Allana4f58f52009-06-02 11:29:18 +00006502 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_M_HV_LM), board_pchlan },
6503 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_M_HV_LC), board_pchlan },
6504 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_D_HV_DM), board_pchlan },
6505 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_D_HV_DC), board_pchlan },
6506
Bruce Alland3738bb2010-06-16 13:27:28 +00006507 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH2_LV_LM), board_pch2lan },
6508 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH2_LV_V), board_pch2lan },
6509
Bruce Allanf36bb6c2012-01-31 06:38:04 +00006510 { 0, 0, 0, 0, 0, 0, 0 } /* terminate list */
Auke Kokbc7f75f2007-09-17 12:30:59 -07006511};
6512MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
6513
Rafael J. Wysockiaa338602011-02-11 00:06:54 +01006514#ifdef CONFIG_PM
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00006515static const struct dev_pm_ops e1000_pm_ops = {
Rafael J. Wysockia0340162010-03-17 23:12:24 -07006516 SET_SYSTEM_SLEEP_PM_OPS(e1000_suspend, e1000_resume)
6517 SET_RUNTIME_PM_OPS(e1000_runtime_suspend,
6518 e1000_runtime_resume, e1000_idle)
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00006519};
David S. Millere50208a2010-03-16 23:36:24 -07006520#endif
Rafael J. Wysocki23606cf2010-03-14 14:35:17 +00006521
Auke Kokbc7f75f2007-09-17 12:30:59 -07006522/* PCI Device API Driver */
6523static struct pci_driver e1000_driver = {
6524 .name = e1000e_driver_name,
6525 .id_table = e1000_pci_tbl,
6526 .probe = e1000_probe,
6527 .remove = __devexit_p(e1000_remove),
Rafael J. Wysockiaa338602011-02-11 00:06:54 +01006528#ifdef CONFIG_PM
Bruce Allanf36bb6c2012-01-31 06:38:04 +00006529 .driver = {
6530 .pm = &e1000_pm_ops,
6531 },
Auke Kokbc7f75f2007-09-17 12:30:59 -07006532#endif
6533 .shutdown = e1000_shutdown,
6534 .err_handler = &e1000_err_handler
6535};
6536
6537/**
6538 * e1000_init_module - Driver Registration Routine
6539 *
6540 * e1000_init_module is the first routine called when the driver is
6541 * loaded. All it does is register with the PCI subsystem.
6542 **/
6543static int __init e1000_init_module(void)
6544{
6545 int ret;
Bruce Allan8544b9f2010-03-24 12:55:30 +00006546 pr_info("Intel(R) PRO/1000 Network Driver - %s\n",
6547 e1000e_driver_version);
Bruce Allanf5e261e2012-01-01 16:00:03 +00006548 pr_info("Copyright(c) 1999 - 2012 Intel Corporation.\n");
Auke Kokbc7f75f2007-09-17 12:30:59 -07006549 ret = pci_register_driver(&e1000_driver);
Bruce Allan53ec5492009-11-20 23:27:40 +00006550
Auke Kokbc7f75f2007-09-17 12:30:59 -07006551 return ret;
6552}
6553module_init(e1000_init_module);
6554
6555/**
6556 * e1000_exit_module - Driver Exit Cleanup Routine
6557 *
6558 * e1000_exit_module is called just before the driver is removed
6559 * from memory.
6560 **/
6561static void __exit e1000_exit_module(void)
6562{
6563 pci_unregister_driver(&e1000_driver);
Auke Kokbc7f75f2007-09-17 12:30:59 -07006564}
6565module_exit(e1000_exit_module);
6566
6567
6568MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
6569MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
6570MODULE_LICENSE("GPL");
6571MODULE_VERSION(DRV_VERSION);
6572
6573/* e1000_main.c */