devices/e1000e/netdev-3.4-ethercat.c
branchstable-1.5
changeset 2491 5e9221a78855
child 2492 d7b1a62709af
equal deleted inserted replaced
2490:6ad972f38438 2491:5e9221a78855
       
     1 /*******************************************************************************
       
     2 
       
     3   Intel PRO/1000 Linux driver
       
     4   Copyright(c) 1999 - 2012 Intel Corporation.
       
     5 
       
     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   vim: noexpandtab
       
    28 
       
    29 *******************************************************************************/
       
    30 
       
    31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
       
    32 
       
    33 #include <linux/module.h>
       
    34 #include <linux/types.h>
       
    35 #include <linux/init.h>
       
    36 #include <linux/pci.h>
       
    37 #include <linux/vmalloc.h>
       
    38 #include <linux/pagemap.h>
       
    39 #include <linux/delay.h>
       
    40 #include <linux/netdevice.h>
       
    41 #include <linux/interrupt.h>
       
    42 #include <linux/tcp.h>
       
    43 #include <linux/ipv6.h>
       
    44 #include <linux/slab.h>
       
    45 #include <net/checksum.h>
       
    46 #include <net/ip6_checksum.h>
       
    47 #include <linux/mii.h>
       
    48 #include <linux/ethtool.h>
       
    49 #include <linux/if_vlan.h>
       
    50 #include <linux/cpu.h>
       
    51 #include <linux/smp.h>
       
    52 #include <linux/pm_qos.h>
       
    53 #include <linux/pm_runtime.h>
       
    54 #include <linux/aer.h>
       
    55 #include <linux/prefetch.h>
       
    56 
       
    57 #include "e1000-3.4-ethercat.h"
       
    58 
       
    59 #define DRV_EXTRAVERSION "-k-EtherCAT"
       
    60 
       
    61 #define DRV_VERSION "1.9.5" DRV_EXTRAVERSION
       
    62 char e1000e_driver_name[] = "ec_e1000e";
       
    63 const char e1000e_driver_version[] = DRV_VERSION;
       
    64 
       
    65 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
       
    66 static int debug = -1;
       
    67 module_param(debug, int, 0);
       
    68 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
       
    69 
       
    70 static void e1000e_disable_aspm(struct pci_dev *pdev, u16 state);
       
    71 
       
    72 static const struct e1000_info *e1000_info_tbl[] = {
       
    73 	[board_82571]		= &e1000_82571_info,
       
    74 	[board_82572]		= &e1000_82572_info,
       
    75 	[board_82573]		= &e1000_82573_info,
       
    76 	[board_82574]		= &e1000_82574_info,
       
    77 	[board_82583]		= &e1000_82583_info,
       
    78 	[board_80003es2lan]	= &e1000_es2_info,
       
    79 	[board_ich8lan]		= &e1000_ich8_info,
       
    80 	[board_ich9lan]		= &e1000_ich9_info,
       
    81 	[board_ich10lan]	= &e1000_ich10_info,
       
    82 	[board_pchlan]		= &e1000_pch_info,
       
    83 	[board_pch2lan]		= &e1000_pch2_info,
       
    84 };
       
    85 
       
    86 struct e1000_reg_info {
       
    87 	u32 ofs;
       
    88 	char *name;
       
    89 };
       
    90 
       
    91 #define E1000_RDFH	0x02410	/* Rx Data FIFO Head - RW */
       
    92 #define E1000_RDFT	0x02418	/* Rx Data FIFO Tail - RW */
       
    93 #define E1000_RDFHS	0x02420	/* Rx Data FIFO Head Saved - RW */
       
    94 #define E1000_RDFTS	0x02428	/* Rx Data FIFO Tail Saved - RW */
       
    95 #define E1000_RDFPC	0x02430	/* Rx Data FIFO Packet Count - RW */
       
    96 
       
    97 #define E1000_TDFH	0x03410	/* Tx Data FIFO Head - RW */
       
    98 #define E1000_TDFT	0x03418	/* Tx Data FIFO Tail - RW */
       
    99 #define E1000_TDFHS	0x03420	/* Tx Data FIFO Head Saved - RW */
       
   100 #define E1000_TDFTS	0x03428	/* Tx Data FIFO Tail Saved - RW */
       
   101 #define E1000_TDFPC	0x03430	/* Tx Data FIFO Packet Count - RW */
       
   102 
       
   103 static const struct e1000_reg_info e1000_reg_info_tbl[] = {
       
   104 
       
   105 	/* General Registers */
       
   106 	{E1000_CTRL, "CTRL"},
       
   107 	{E1000_STATUS, "STATUS"},
       
   108 	{E1000_CTRL_EXT, "CTRL_EXT"},
       
   109 
       
   110 	/* Interrupt Registers */
       
   111 	{E1000_ICR, "ICR"},
       
   112 
       
   113 	/* Rx Registers */
       
   114 	{E1000_RCTL, "RCTL"},
       
   115 	{E1000_RDLEN, "RDLEN"},
       
   116 	{E1000_RDH, "RDH"},
       
   117 	{E1000_RDT, "RDT"},
       
   118 	{E1000_RDTR, "RDTR"},
       
   119 	{E1000_RXDCTL(0), "RXDCTL"},
       
   120 	{E1000_ERT, "ERT"},
       
   121 	{E1000_RDBAL, "RDBAL"},
       
   122 	{E1000_RDBAH, "RDBAH"},
       
   123 	{E1000_RDFH, "RDFH"},
       
   124 	{E1000_RDFT, "RDFT"},
       
   125 	{E1000_RDFHS, "RDFHS"},
       
   126 	{E1000_RDFTS, "RDFTS"},
       
   127 	{E1000_RDFPC, "RDFPC"},
       
   128 
       
   129 	/* Tx Registers */
       
   130 	{E1000_TCTL, "TCTL"},
       
   131 	{E1000_TDBAL, "TDBAL"},
       
   132 	{E1000_TDBAH, "TDBAH"},
       
   133 	{E1000_TDLEN, "TDLEN"},
       
   134 	{E1000_TDH, "TDH"},
       
   135 	{E1000_TDT, "TDT"},
       
   136 	{E1000_TIDV, "TIDV"},
       
   137 	{E1000_TXDCTL(0), "TXDCTL"},
       
   138 	{E1000_TADV, "TADV"},
       
   139 	{E1000_TARC(0), "TARC"},
       
   140 	{E1000_TDFH, "TDFH"},
       
   141 	{E1000_TDFT, "TDFT"},
       
   142 	{E1000_TDFHS, "TDFHS"},
       
   143 	{E1000_TDFTS, "TDFTS"},
       
   144 	{E1000_TDFPC, "TDFPC"},
       
   145 
       
   146 	/* List Terminator */
       
   147 	{0, NULL}
       
   148 };
       
   149 
       
   150 /*
       
   151  * e1000_regdump - register printout routine
       
   152  */
       
   153 static void e1000_regdump(struct e1000_hw *hw, struct e1000_reg_info *reginfo)
       
   154 {
       
   155 	int n = 0;
       
   156 	char rname[16];
       
   157 	u32 regs[8];
       
   158 
       
   159 	switch (reginfo->ofs) {
       
   160 	case E1000_RXDCTL(0):
       
   161 		for (n = 0; n < 2; n++)
       
   162 			regs[n] = __er32(hw, E1000_RXDCTL(n));
       
   163 		break;
       
   164 	case E1000_TXDCTL(0):
       
   165 		for (n = 0; n < 2; n++)
       
   166 			regs[n] = __er32(hw, E1000_TXDCTL(n));
       
   167 		break;
       
   168 	case E1000_TARC(0):
       
   169 		for (n = 0; n < 2; n++)
       
   170 			regs[n] = __er32(hw, E1000_TARC(n));
       
   171 		break;
       
   172 	default:
       
   173 		pr_info("%-15s %08x\n",
       
   174 			reginfo->name, __er32(hw, reginfo->ofs));
       
   175 		return;
       
   176 	}
       
   177 
       
   178 	snprintf(rname, 16, "%s%s", reginfo->name, "[0-1]");
       
   179 	pr_info("%-15s %08x %08x\n", rname, regs[0], regs[1]);
       
   180 }
       
   181 
       
   182 /*
       
   183  * e1000e_dump - Print registers, Tx-ring and Rx-ring
       
   184  */
       
   185 static void e1000e_dump(struct e1000_adapter *adapter)
       
   186 {
       
   187 	struct net_device *netdev = adapter->netdev;
       
   188 	struct e1000_hw *hw = &adapter->hw;
       
   189 	struct e1000_reg_info *reginfo;
       
   190 	struct e1000_ring *tx_ring = adapter->tx_ring;
       
   191 	struct e1000_tx_desc *tx_desc;
       
   192 	struct my_u0 {
       
   193 		__le64 a;
       
   194 		__le64 b;
       
   195 	} *u0;
       
   196 	struct e1000_buffer *buffer_info;
       
   197 	struct e1000_ring *rx_ring = adapter->rx_ring;
       
   198 	union e1000_rx_desc_packet_split *rx_desc_ps;
       
   199 	union e1000_rx_desc_extended *rx_desc;
       
   200 	struct my_u1 {
       
   201 		__le64 a;
       
   202 		__le64 b;
       
   203 		__le64 c;
       
   204 		__le64 d;
       
   205 	} *u1;
       
   206 	u32 staterr;
       
   207 	int i = 0;
       
   208 
       
   209 	if (!netif_msg_hw(adapter))
       
   210 		return;
       
   211 
       
   212 	/* Print netdevice Info */
       
   213 	if (netdev) {
       
   214 		dev_info(&adapter->pdev->dev, "Net device Info\n");
       
   215 		pr_info("Device Name     state            trans_start      last_rx\n");
       
   216 		pr_info("%-15s %016lX %016lX %016lX\n",
       
   217 			netdev->name, netdev->state, netdev->trans_start,
       
   218 			netdev->last_rx);
       
   219 	}
       
   220 
       
   221 	/* Print Registers */
       
   222 	dev_info(&adapter->pdev->dev, "Register Dump\n");
       
   223 	pr_info(" Register Name   Value\n");
       
   224 	for (reginfo = (struct e1000_reg_info *)e1000_reg_info_tbl;
       
   225 	     reginfo->name; reginfo++) {
       
   226 		e1000_regdump(hw, reginfo);
       
   227 	}
       
   228 
       
   229 	/* Print Tx Ring Summary */
       
   230 	if (!netdev || !netif_running(netdev))
       
   231 		return;
       
   232 
       
   233 	dev_info(&adapter->pdev->dev, "Tx Ring Summary\n");
       
   234 	pr_info("Queue [NTU] [NTC] [bi(ntc)->dma  ] leng ntw timestamp\n");
       
   235 	buffer_info = &tx_ring->buffer_info[tx_ring->next_to_clean];
       
   236 	pr_info(" %5d %5X %5X %016llX %04X %3X %016llX\n",
       
   237 		0, tx_ring->next_to_use, tx_ring->next_to_clean,
       
   238 		(unsigned long long)buffer_info->dma,
       
   239 		buffer_info->length,
       
   240 		buffer_info->next_to_watch,
       
   241 		(unsigned long long)buffer_info->time_stamp);
       
   242 
       
   243 	/* Print Tx Ring */
       
   244 	if (!netif_msg_tx_done(adapter))
       
   245 		goto rx_ring_summary;
       
   246 
       
   247 	dev_info(&adapter->pdev->dev, "Tx Ring Dump\n");
       
   248 
       
   249 	/* Transmit Descriptor Formats - DEXT[29] is 0 (Legacy) or 1 (Extended)
       
   250 	 *
       
   251 	 * Legacy Transmit Descriptor
       
   252 	 *   +--------------------------------------------------------------+
       
   253 	 * 0 |         Buffer Address [63:0] (Reserved on Write Back)       |
       
   254 	 *   +--------------------------------------------------------------+
       
   255 	 * 8 | Special  |    CSS     | Status |  CMD    |  CSO   |  Length  |
       
   256 	 *   +--------------------------------------------------------------+
       
   257 	 *   63       48 47        36 35    32 31     24 23    16 15        0
       
   258 	 *
       
   259 	 * Extended Context Descriptor (DTYP=0x0) for TSO or checksum offload
       
   260 	 *   63      48 47    40 39       32 31             16 15    8 7      0
       
   261 	 *   +----------------------------------------------------------------+
       
   262 	 * 0 |  TUCSE  | TUCS0  |   TUCSS   |     IPCSE       | IPCS0 | IPCSS |
       
   263 	 *   +----------------------------------------------------------------+
       
   264 	 * 8 |   MSS   | HDRLEN | RSV | STA | TUCMD | DTYP |      PAYLEN      |
       
   265 	 *   +----------------------------------------------------------------+
       
   266 	 *   63      48 47    40 39 36 35 32 31   24 23  20 19                0
       
   267 	 *
       
   268 	 * Extended Data Descriptor (DTYP=0x1)
       
   269 	 *   +----------------------------------------------------------------+
       
   270 	 * 0 |                     Buffer Address [63:0]                      |
       
   271 	 *   +----------------------------------------------------------------+
       
   272 	 * 8 | VLAN tag |  POPTS  | Rsvd | Status | Command | DTYP |  DTALEN  |
       
   273 	 *   +----------------------------------------------------------------+
       
   274 	 *   63       48 47     40 39  36 35    32 31     24 23  20 19        0
       
   275 	 */
       
   276 	pr_info("Tl[desc]     [address 63:0  ] [SpeCssSCmCsLen] [bi->dma       ] leng  ntw timestamp        bi->skb <-- Legacy format\n");
       
   277 	pr_info("Tc[desc]     [Ce CoCsIpceCoS] [MssHlRSCm0Plen] [bi->dma       ] leng  ntw timestamp        bi->skb <-- Ext Context format\n");
       
   278 	pr_info("Td[desc]     [address 63:0  ] [VlaPoRSCm1Dlen] [bi->dma       ] leng  ntw timestamp        bi->skb <-- Ext Data format\n");
       
   279 	for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
       
   280 		const char *next_desc;
       
   281 		tx_desc = E1000_TX_DESC(*tx_ring, i);
       
   282 		buffer_info = &tx_ring->buffer_info[i];
       
   283 		u0 = (struct my_u0 *)tx_desc;
       
   284 		if (i == tx_ring->next_to_use && i == tx_ring->next_to_clean)
       
   285 			next_desc = " NTC/U";
       
   286 		else if (i == tx_ring->next_to_use)
       
   287 			next_desc = " NTU";
       
   288 		else if (i == tx_ring->next_to_clean)
       
   289 			next_desc = " NTC";
       
   290 		else
       
   291 			next_desc = "";
       
   292 		pr_info("T%c[0x%03X]    %016llX %016llX %016llX %04X  %3X %016llX %p%s\n",
       
   293 			(!(le64_to_cpu(u0->b) & (1 << 29)) ? 'l' :
       
   294 			 ((le64_to_cpu(u0->b) & (1 << 20)) ? 'd' : 'c')),
       
   295 			i,
       
   296 			(unsigned long long)le64_to_cpu(u0->a),
       
   297 			(unsigned long long)le64_to_cpu(u0->b),
       
   298 			(unsigned long long)buffer_info->dma,
       
   299 			buffer_info->length, buffer_info->next_to_watch,
       
   300 			(unsigned long long)buffer_info->time_stamp,
       
   301 			buffer_info->skb, next_desc);
       
   302 
       
   303 		if (netif_msg_pktdata(adapter) && buffer_info->dma != 0)
       
   304 			print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS,
       
   305 				       16, 1, phys_to_virt(buffer_info->dma),
       
   306 				       buffer_info->length, true);
       
   307 	}
       
   308 
       
   309 	/* Print Rx Ring Summary */
       
   310 rx_ring_summary:
       
   311 	dev_info(&adapter->pdev->dev, "Rx Ring Summary\n");
       
   312 	pr_info("Queue [NTU] [NTC]\n");
       
   313 	pr_info(" %5d %5X %5X\n",
       
   314 		0, rx_ring->next_to_use, rx_ring->next_to_clean);
       
   315 
       
   316 	/* Print Rx Ring */
       
   317 	if (!netif_msg_rx_status(adapter))
       
   318 		return;
       
   319 
       
   320 	dev_info(&adapter->pdev->dev, "Rx Ring Dump\n");
       
   321 	switch (adapter->rx_ps_pages) {
       
   322 	case 1:
       
   323 	case 2:
       
   324 	case 3:
       
   325 		/* [Extended] Packet Split Receive Descriptor Format
       
   326 		 *
       
   327 		 *    +-----------------------------------------------------+
       
   328 		 *  0 |                Buffer Address 0 [63:0]              |
       
   329 		 *    +-----------------------------------------------------+
       
   330 		 *  8 |                Buffer Address 1 [63:0]              |
       
   331 		 *    +-----------------------------------------------------+
       
   332 		 * 16 |                Buffer Address 2 [63:0]              |
       
   333 		 *    +-----------------------------------------------------+
       
   334 		 * 24 |                Buffer Address 3 [63:0]              |
       
   335 		 *    +-----------------------------------------------------+
       
   336 		 */
       
   337 		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");
       
   338 		/* [Extended] Receive Descriptor (Write-Back) Format
       
   339 		 *
       
   340 		 *   63       48 47    32 31     13 12    8 7    4 3        0
       
   341 		 *   +------------------------------------------------------+
       
   342 		 * 0 | Packet   | IP     |  Rsvd   | MRQ   | Rsvd | MRQ RSS |
       
   343 		 *   | Checksum | Ident  |         | Queue |      |  Type   |
       
   344 		 *   +------------------------------------------------------+
       
   345 		 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
       
   346 		 *   +------------------------------------------------------+
       
   347 		 *   63       48 47    32 31            20 19               0
       
   348 		 */
       
   349 		pr_info("RWB[desc]      [ck ipid mrqhsh] [vl   l0 ee  es] [ l3  l2  l1 hs] [reserved      ] ---------------- [bi->skb] <-- Ext Rx Write-Back format\n");
       
   350 		for (i = 0; i < rx_ring->count; i++) {
       
   351 			const char *next_desc;
       
   352 			buffer_info = &rx_ring->buffer_info[i];
       
   353 			rx_desc_ps = E1000_RX_DESC_PS(*rx_ring, i);
       
   354 			u1 = (struct my_u1 *)rx_desc_ps;
       
   355 			staterr =
       
   356 			    le32_to_cpu(rx_desc_ps->wb.middle.status_error);
       
   357 
       
   358 			if (i == rx_ring->next_to_use)
       
   359 				next_desc = " NTU";
       
   360 			else if (i == rx_ring->next_to_clean)
       
   361 				next_desc = " NTC";
       
   362 			else
       
   363 				next_desc = "";
       
   364 
       
   365 			if (staterr & E1000_RXD_STAT_DD) {
       
   366 				/* Descriptor Done */
       
   367 				pr_info("%s[0x%03X]     %016llX %016llX %016llX %016llX ---------------- %p%s\n",
       
   368 					"RWB", i,
       
   369 					(unsigned long long)le64_to_cpu(u1->a),
       
   370 					(unsigned long long)le64_to_cpu(u1->b),
       
   371 					(unsigned long long)le64_to_cpu(u1->c),
       
   372 					(unsigned long long)le64_to_cpu(u1->d),
       
   373 					buffer_info->skb, next_desc);
       
   374 			} else {
       
   375 				pr_info("%s[0x%03X]     %016llX %016llX %016llX %016llX %016llX %p%s\n",
       
   376 					"R  ", i,
       
   377 					(unsigned long long)le64_to_cpu(u1->a),
       
   378 					(unsigned long long)le64_to_cpu(u1->b),
       
   379 					(unsigned long long)le64_to_cpu(u1->c),
       
   380 					(unsigned long long)le64_to_cpu(u1->d),
       
   381 					(unsigned long long)buffer_info->dma,
       
   382 					buffer_info->skb, next_desc);
       
   383 
       
   384 				if (netif_msg_pktdata(adapter))
       
   385 					print_hex_dump(KERN_INFO, "",
       
   386 						DUMP_PREFIX_ADDRESS, 16, 1,
       
   387 						phys_to_virt(buffer_info->dma),
       
   388 						adapter->rx_ps_bsize0, true);
       
   389 			}
       
   390 		}
       
   391 		break;
       
   392 	default:
       
   393 	case 0:
       
   394 		/* Extended Receive Descriptor (Read) Format
       
   395 		 *
       
   396 		 *   +-----------------------------------------------------+
       
   397 		 * 0 |                Buffer Address [63:0]                |
       
   398 		 *   +-----------------------------------------------------+
       
   399 		 * 8 |                      Reserved                       |
       
   400 		 *   +-----------------------------------------------------+
       
   401 		 */
       
   402 		pr_info("R  [desc]      [buf addr 63:0 ] [reserved 63:0 ] [bi->dma       ] [bi->skb] <-- Ext (Read) format\n");
       
   403 		/* Extended Receive Descriptor (Write-Back) Format
       
   404 		 *
       
   405 		 *   63       48 47    32 31    24 23            4 3        0
       
   406 		 *   +------------------------------------------------------+
       
   407 		 *   |     RSS Hash      |        |               |         |
       
   408 		 * 0 +-------------------+  Rsvd  |   Reserved    | MRQ RSS |
       
   409 		 *   | Packet   | IP     |        |               |  Type   |
       
   410 		 *   | Checksum | Ident  |        |               |         |
       
   411 		 *   +------------------------------------------------------+
       
   412 		 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
       
   413 		 *   +------------------------------------------------------+
       
   414 		 *   63       48 47    32 31            20 19               0
       
   415 		 */
       
   416 		pr_info("RWB[desc]      [cs ipid    mrq] [vt   ln xe  xs] [bi->skb] <-- Ext (Write-Back) format\n");
       
   417 
       
   418 		for (i = 0; i < rx_ring->count; i++) {
       
   419 			const char *next_desc;
       
   420 
       
   421 			buffer_info = &rx_ring->buffer_info[i];
       
   422 			rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
       
   423 			u1 = (struct my_u1 *)rx_desc;
       
   424 			staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
       
   425 
       
   426 			if (i == rx_ring->next_to_use)
       
   427 				next_desc = " NTU";
       
   428 			else if (i == rx_ring->next_to_clean)
       
   429 				next_desc = " NTC";
       
   430 			else
       
   431 				next_desc = "";
       
   432 
       
   433 			if (staterr & E1000_RXD_STAT_DD) {
       
   434 				/* Descriptor Done */
       
   435 				pr_info("%s[0x%03X]     %016llX %016llX ---------------- %p%s\n",
       
   436 					"RWB", i,
       
   437 					(unsigned long long)le64_to_cpu(u1->a),
       
   438 					(unsigned long long)le64_to_cpu(u1->b),
       
   439 					buffer_info->skb, next_desc);
       
   440 			} else {
       
   441 				pr_info("%s[0x%03X]     %016llX %016llX %016llX %p%s\n",
       
   442 					"R  ", i,
       
   443 					(unsigned long long)le64_to_cpu(u1->a),
       
   444 					(unsigned long long)le64_to_cpu(u1->b),
       
   445 					(unsigned long long)buffer_info->dma,
       
   446 					buffer_info->skb, next_desc);
       
   447 
       
   448 				if (netif_msg_pktdata(adapter))
       
   449 					print_hex_dump(KERN_INFO, "",
       
   450 						       DUMP_PREFIX_ADDRESS, 16,
       
   451 						       1,
       
   452 						       phys_to_virt
       
   453 						       (buffer_info->dma),
       
   454 						       adapter->rx_buffer_len,
       
   455 						       true);
       
   456 			}
       
   457 		}
       
   458 	}
       
   459 }
       
   460 
       
   461 /**
       
   462  * e1000_desc_unused - calculate if we have unused descriptors
       
   463  **/
       
   464 static int e1000_desc_unused(struct e1000_ring *ring)
       
   465 {
       
   466 	if (ring->next_to_clean > ring->next_to_use)
       
   467 		return ring->next_to_clean - ring->next_to_use - 1;
       
   468 
       
   469 	return ring->count + ring->next_to_clean - ring->next_to_use - 1;
       
   470 }
       
   471 
       
   472 /**
       
   473  * e1000_receive_skb - helper function to handle Rx indications
       
   474  * @adapter: board private structure
       
   475  * @status: descriptor status field as written by hardware
       
   476  * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
       
   477  * @skb: pointer to sk_buff to be indicated to stack
       
   478  **/
       
   479 static void e1000_receive_skb(struct e1000_adapter *adapter,
       
   480 			      struct net_device *netdev, struct sk_buff *skb,
       
   481 			      u8 status, __le16 vlan)
       
   482 {
       
   483 	u16 tag = le16_to_cpu(vlan);
       
   484 	skb->protocol = eth_type_trans(skb, netdev);
       
   485 
       
   486 	if (status & E1000_RXD_STAT_VP)
       
   487 		__vlan_hwaccel_put_tag(skb, tag);
       
   488 
       
   489 	napi_gro_receive(&adapter->napi, skb);
       
   490 }
       
   491 
       
   492 /**
       
   493  * e1000_rx_checksum - Receive Checksum Offload
       
   494  * @adapter: board private structure
       
   495  * @status_err: receive descriptor status and error fields
       
   496  * @csum: receive descriptor csum field
       
   497  * @sk_buff: socket buffer with received data
       
   498  **/
       
   499 static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
       
   500 			      struct sk_buff *skb)
       
   501 {
       
   502 	u16 status = (u16)status_err;
       
   503 	u8 errors = (u8)(status_err >> 24);
       
   504 
       
   505 	skb_checksum_none_assert(skb);
       
   506 
       
   507 	/* Rx checksum disabled */
       
   508 	if (!(adapter->netdev->features & NETIF_F_RXCSUM))
       
   509 		return;
       
   510 
       
   511 	/* Ignore Checksum bit is set */
       
   512 	if (status & E1000_RXD_STAT_IXSM)
       
   513 		return;
       
   514 
       
   515 	/* TCP/UDP checksum error bit or IP checksum error bit is set */
       
   516 	if (errors & (E1000_RXD_ERR_TCPE | E1000_RXD_ERR_IPE)) {
       
   517 		/* let the stack verify checksum errors */
       
   518 		adapter->hw_csum_err++;
       
   519 		return;
       
   520 	}
       
   521 
       
   522 	/* TCP/UDP Checksum has not been calculated */
       
   523 	if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
       
   524 		return;
       
   525 
       
   526 	/* It must be a TCP or UDP packet with a valid checksum */
       
   527 	skb->ip_summed = CHECKSUM_UNNECESSARY;
       
   528 	adapter->hw_csum_good++;
       
   529 }
       
   530 
       
   531 /**
       
   532  * e1000e_update_tail_wa - helper function for e1000e_update_[rt]dt_wa()
       
   533  * @hw: pointer to the HW structure
       
   534  * @tail: address of tail descriptor register
       
   535  * @i: value to write to tail descriptor register
       
   536  *
       
   537  * When updating the tail register, the ME could be accessing Host CSR
       
   538  * registers at the same time.  Normally, this is handled in h/w by an
       
   539  * arbiter but on some parts there is a bug that acknowledges Host accesses
       
   540  * later than it should which could result in the descriptor register to
       
   541  * have an incorrect value.  Workaround this by checking the FWSM register
       
   542  * which has bit 24 set while ME is accessing Host CSR registers, wait
       
   543  * if it is set and try again a number of times.
       
   544  **/
       
   545 static inline s32 e1000e_update_tail_wa(struct e1000_hw *hw, void __iomem *tail,
       
   546 					unsigned int i)
       
   547 {
       
   548 	unsigned int j = 0;
       
   549 
       
   550 	while ((j++ < E1000_ICH_FWSM_PCIM2PCI_COUNT) &&
       
   551 	       (er32(FWSM) & E1000_ICH_FWSM_PCIM2PCI))
       
   552 		udelay(50);
       
   553 
       
   554 	writel(i, tail);
       
   555 
       
   556 	if ((j == E1000_ICH_FWSM_PCIM2PCI_COUNT) && (i != readl(tail)))
       
   557 		return E1000_ERR_SWFW_SYNC;
       
   558 
       
   559 	return 0;
       
   560 }
       
   561 
       
   562 static void e1000e_update_rdt_wa(struct e1000_ring *rx_ring, unsigned int i)
       
   563 {
       
   564 	struct e1000_adapter *adapter = rx_ring->adapter;
       
   565 	struct e1000_hw *hw = &adapter->hw;
       
   566 
       
   567 	if (e1000e_update_tail_wa(hw, rx_ring->tail, i)) {
       
   568 		u32 rctl = er32(RCTL);
       
   569 		ew32(RCTL, rctl & ~E1000_RCTL_EN);
       
   570 		e_err("ME firmware caused invalid RDT - resetting\n");
       
   571 		schedule_work(&adapter->reset_task);
       
   572 	}
       
   573 }
       
   574 
       
   575 static void e1000e_update_tdt_wa(struct e1000_ring *tx_ring, unsigned int i)
       
   576 {
       
   577 	struct e1000_adapter *adapter = tx_ring->adapter;
       
   578 	struct e1000_hw *hw = &adapter->hw;
       
   579 
       
   580 	if (e1000e_update_tail_wa(hw, tx_ring->tail, i)) {
       
   581 		u32 tctl = er32(TCTL);
       
   582 		ew32(TCTL, tctl & ~E1000_TCTL_EN);
       
   583 		e_err("ME firmware caused invalid TDT - resetting\n");
       
   584 		schedule_work(&adapter->reset_task);
       
   585 	}
       
   586 }
       
   587 
       
   588 /**
       
   589  * e1000_alloc_rx_buffers - Replace used receive buffers
       
   590  * @rx_ring: Rx descriptor ring
       
   591  **/
       
   592 static void e1000_alloc_rx_buffers(struct e1000_ring *rx_ring,
       
   593 				   int cleaned_count, gfp_t gfp)
       
   594 {
       
   595 	struct e1000_adapter *adapter = rx_ring->adapter;
       
   596 	struct net_device *netdev = adapter->netdev;
       
   597 	struct pci_dev *pdev = adapter->pdev;
       
   598 	union e1000_rx_desc_extended *rx_desc;
       
   599 	struct e1000_buffer *buffer_info;
       
   600 	struct sk_buff *skb;
       
   601 	unsigned int i;
       
   602 	unsigned int bufsz = adapter->rx_buffer_len;
       
   603 
       
   604 	i = rx_ring->next_to_use;
       
   605 	buffer_info = &rx_ring->buffer_info[i];
       
   606 
       
   607 	while (cleaned_count--) {
       
   608 		skb = buffer_info->skb;
       
   609 		if (skb) {
       
   610 			skb_trim(skb, 0);
       
   611 			goto map_skb;
       
   612 		}
       
   613 
       
   614 		skb = __netdev_alloc_skb_ip_align(netdev, bufsz, gfp);
       
   615 		if (!skb) {
       
   616 			/* Better luck next round */
       
   617 			adapter->alloc_rx_buff_failed++;
       
   618 			break;
       
   619 		}
       
   620 
       
   621 		buffer_info->skb = skb;
       
   622 map_skb:
       
   623 		buffer_info->dma = dma_map_single(&pdev->dev, skb->data,
       
   624 						  adapter->rx_buffer_len,
       
   625 						  DMA_FROM_DEVICE);
       
   626 		if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
       
   627 			dev_err(&pdev->dev, "Rx DMA map failed\n");
       
   628 			adapter->rx_dma_failed++;
       
   629 			break;
       
   630 		}
       
   631 
       
   632 		rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
       
   633 		rx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
       
   634 
       
   635 		if (unlikely(!(i & (E1000_RX_BUFFER_WRITE - 1)))) {
       
   636 			/*
       
   637 			 * Force memory writes to complete before letting h/w
       
   638 			 * know there are new descriptors to fetch.  (Only
       
   639 			 * applicable for weak-ordered memory model archs,
       
   640 			 * such as IA-64).
       
   641 			 */
       
   642 			wmb();
       
   643 			if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
       
   644 				e1000e_update_rdt_wa(rx_ring, i);
       
   645 			else
       
   646 				writel(i, rx_ring->tail);
       
   647 		}
       
   648 		i++;
       
   649 		if (i == rx_ring->count)
       
   650 			i = 0;
       
   651 		buffer_info = &rx_ring->buffer_info[i];
       
   652 	}
       
   653 
       
   654 	rx_ring->next_to_use = i;
       
   655 }
       
   656 
       
   657 /**
       
   658  * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
       
   659  * @rx_ring: Rx descriptor ring
       
   660  **/
       
   661 static void e1000_alloc_rx_buffers_ps(struct e1000_ring *rx_ring,
       
   662 				      int cleaned_count, gfp_t gfp)
       
   663 {
       
   664 	struct e1000_adapter *adapter = rx_ring->adapter;
       
   665 	struct net_device *netdev = adapter->netdev;
       
   666 	struct pci_dev *pdev = adapter->pdev;
       
   667 	union e1000_rx_desc_packet_split *rx_desc;
       
   668 	struct e1000_buffer *buffer_info;
       
   669 	struct e1000_ps_page *ps_page;
       
   670 	struct sk_buff *skb;
       
   671 	unsigned int i, j;
       
   672 
       
   673 	i = rx_ring->next_to_use;
       
   674 	buffer_info = &rx_ring->buffer_info[i];
       
   675 
       
   676 	while (cleaned_count--) {
       
   677 		rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
       
   678 
       
   679 		for (j = 0; j < PS_PAGE_BUFFERS; j++) {
       
   680 			ps_page = &buffer_info->ps_pages[j];
       
   681 			if (j >= adapter->rx_ps_pages) {
       
   682 				/* all unused desc entries get hw null ptr */
       
   683 				rx_desc->read.buffer_addr[j + 1] =
       
   684 				    ~cpu_to_le64(0);
       
   685 				continue;
       
   686 			}
       
   687 			if (!ps_page->page) {
       
   688 				ps_page->page = alloc_page(gfp);
       
   689 				if (!ps_page->page) {
       
   690 					adapter->alloc_rx_buff_failed++;
       
   691 					goto no_buffers;
       
   692 				}
       
   693 				ps_page->dma = dma_map_page(&pdev->dev,
       
   694 							    ps_page->page,
       
   695 							    0, PAGE_SIZE,
       
   696 							    DMA_FROM_DEVICE);
       
   697 				if (dma_mapping_error(&pdev->dev,
       
   698 						      ps_page->dma)) {
       
   699 					dev_err(&adapter->pdev->dev,
       
   700 						"Rx DMA page map failed\n");
       
   701 					adapter->rx_dma_failed++;
       
   702 					goto no_buffers;
       
   703 				}
       
   704 			}
       
   705 			/*
       
   706 			 * Refresh the desc even if buffer_addrs
       
   707 			 * didn't change because each write-back
       
   708 			 * erases this info.
       
   709 			 */
       
   710 			rx_desc->read.buffer_addr[j + 1] =
       
   711 			    cpu_to_le64(ps_page->dma);
       
   712 		}
       
   713 
       
   714 		skb = __netdev_alloc_skb_ip_align(netdev,
       
   715 						  adapter->rx_ps_bsize0,
       
   716 						  gfp);
       
   717 
       
   718 		if (!skb) {
       
   719 			adapter->alloc_rx_buff_failed++;
       
   720 			break;
       
   721 		}
       
   722 
       
   723 		buffer_info->skb = skb;
       
   724 		buffer_info->dma = dma_map_single(&pdev->dev, skb->data,
       
   725 						  adapter->rx_ps_bsize0,
       
   726 						  DMA_FROM_DEVICE);
       
   727 		if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
       
   728 			dev_err(&pdev->dev, "Rx DMA map failed\n");
       
   729 			adapter->rx_dma_failed++;
       
   730 			/* cleanup skb */
       
   731 			dev_kfree_skb_any(skb);
       
   732 			buffer_info->skb = NULL;
       
   733 			break;
       
   734 		}
       
   735 
       
   736 		rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
       
   737 
       
   738 		if (unlikely(!(i & (E1000_RX_BUFFER_WRITE - 1)))) {
       
   739 			/*
       
   740 			 * Force memory writes to complete before letting h/w
       
   741 			 * know there are new descriptors to fetch.  (Only
       
   742 			 * applicable for weak-ordered memory model archs,
       
   743 			 * such as IA-64).
       
   744 			 */
       
   745 			wmb();
       
   746 			if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
       
   747 				e1000e_update_rdt_wa(rx_ring, i << 1);
       
   748 			else
       
   749 				writel(i << 1, rx_ring->tail);
       
   750 		}
       
   751 
       
   752 		i++;
       
   753 		if (i == rx_ring->count)
       
   754 			i = 0;
       
   755 		buffer_info = &rx_ring->buffer_info[i];
       
   756 	}
       
   757 
       
   758 no_buffers:
       
   759 	rx_ring->next_to_use = i;
       
   760 }
       
   761 
       
   762 /**
       
   763  * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
       
   764  * @rx_ring: Rx descriptor ring
       
   765  * @cleaned_count: number of buffers to allocate this pass
       
   766  **/
       
   767 
       
   768 static void e1000_alloc_jumbo_rx_buffers(struct e1000_ring *rx_ring,
       
   769 					 int cleaned_count, gfp_t gfp)
       
   770 {
       
   771 	struct e1000_adapter *adapter = rx_ring->adapter;
       
   772 	struct net_device *netdev = adapter->netdev;
       
   773 	struct pci_dev *pdev = adapter->pdev;
       
   774 	union e1000_rx_desc_extended *rx_desc;
       
   775 	struct e1000_buffer *buffer_info;
       
   776 	struct sk_buff *skb;
       
   777 	unsigned int i;
       
   778 	unsigned int bufsz = 256 - 16 /* for skb_reserve */;
       
   779 
       
   780 	i = rx_ring->next_to_use;
       
   781 	buffer_info = &rx_ring->buffer_info[i];
       
   782 
       
   783 	while (cleaned_count--) {
       
   784 		skb = buffer_info->skb;
       
   785 		if (skb) {
       
   786 			skb_trim(skb, 0);
       
   787 			goto check_page;
       
   788 		}
       
   789 
       
   790 		skb = __netdev_alloc_skb_ip_align(netdev, bufsz, gfp);
       
   791 		if (unlikely(!skb)) {
       
   792 			/* Better luck next round */
       
   793 			adapter->alloc_rx_buff_failed++;
       
   794 			break;
       
   795 		}
       
   796 
       
   797 		buffer_info->skb = skb;
       
   798 check_page:
       
   799 		/* allocate a new page if necessary */
       
   800 		if (!buffer_info->page) {
       
   801 			buffer_info->page = alloc_page(gfp);
       
   802 			if (unlikely(!buffer_info->page)) {
       
   803 				adapter->alloc_rx_buff_failed++;
       
   804 				break;
       
   805 			}
       
   806 		}
       
   807 
       
   808 		if (!buffer_info->dma)
       
   809 			buffer_info->dma = dma_map_page(&pdev->dev,
       
   810 			                                buffer_info->page, 0,
       
   811 			                                PAGE_SIZE,
       
   812 							DMA_FROM_DEVICE);
       
   813 
       
   814 		rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
       
   815 		rx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
       
   816 
       
   817 		if (unlikely(++i == rx_ring->count))
       
   818 			i = 0;
       
   819 		buffer_info = &rx_ring->buffer_info[i];
       
   820 	}
       
   821 
       
   822 	if (likely(rx_ring->next_to_use != i)) {
       
   823 		rx_ring->next_to_use = i;
       
   824 		if (unlikely(i-- == 0))
       
   825 			i = (rx_ring->count - 1);
       
   826 
       
   827 		/* Force memory writes to complete before letting h/w
       
   828 		 * know there are new descriptors to fetch.  (Only
       
   829 		 * applicable for weak-ordered memory model archs,
       
   830 		 * such as IA-64). */
       
   831 		wmb();
       
   832 		if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
       
   833 			e1000e_update_rdt_wa(rx_ring, i);
       
   834 		else
       
   835 			writel(i, rx_ring->tail);
       
   836 	}
       
   837 }
       
   838 
       
   839 static inline void e1000_rx_hash(struct net_device *netdev, __le32 rss,
       
   840 				 struct sk_buff *skb)
       
   841 {
       
   842 	if (netdev->features & NETIF_F_RXHASH)
       
   843 		skb->rxhash = le32_to_cpu(rss);
       
   844 }
       
   845 
       
   846 /**
       
   847  * e1000_clean_rx_irq - Send received data up the network stack
       
   848  * @rx_ring: Rx descriptor ring
       
   849  *
       
   850  * the return value indicates whether actual cleaning was done, there
       
   851  * is no guarantee that everything was cleaned
       
   852  **/
       
   853 static bool e1000_clean_rx_irq(struct e1000_ring *rx_ring, int *work_done,
       
   854 			       int work_to_do)
       
   855 {
       
   856 	struct e1000_adapter *adapter = rx_ring->adapter;
       
   857 	struct net_device *netdev = adapter->netdev;
       
   858 	struct pci_dev *pdev = adapter->pdev;
       
   859 	struct e1000_hw *hw = &adapter->hw;
       
   860 	union e1000_rx_desc_extended *rx_desc, *next_rxd;
       
   861 	struct e1000_buffer *buffer_info, *next_buffer;
       
   862 	u32 length, staterr;
       
   863 	unsigned int i;
       
   864 	int cleaned_count = 0;
       
   865 	bool cleaned = false;
       
   866 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
       
   867 
       
   868 	i = rx_ring->next_to_clean;
       
   869 	rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
       
   870 	staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
       
   871 	buffer_info = &rx_ring->buffer_info[i];
       
   872 
       
   873 	while (staterr & E1000_RXD_STAT_DD) {
       
   874 		struct sk_buff *skb;
       
   875 
       
   876 		if (*work_done >= work_to_do)
       
   877 			break;
       
   878 		(*work_done)++;
       
   879 		rmb();	/* read descriptor and rx_buffer_info after status DD */
       
   880 
       
   881 		skb = buffer_info->skb;
       
   882 
       
   883 		if (!adapter->ecdev)
       
   884 			buffer_info->skb = NULL;
       
   885 
       
   886 		prefetch(skb->data - NET_IP_ALIGN);
       
   887 
       
   888 		i++;
       
   889 		if (i == rx_ring->count)
       
   890 			i = 0;
       
   891 		next_rxd = E1000_RX_DESC_EXT(*rx_ring, i);
       
   892 		prefetch(next_rxd);
       
   893 
       
   894 		next_buffer = &rx_ring->buffer_info[i];
       
   895 
       
   896 		cleaned = true;
       
   897 		cleaned_count++;
       
   898 		dma_unmap_single(&pdev->dev,
       
   899 				 buffer_info->dma,
       
   900 				 adapter->rx_buffer_len,
       
   901 				 DMA_FROM_DEVICE);
       
   902 		buffer_info->dma = 0;
       
   903 
       
   904 		length = le16_to_cpu(rx_desc->wb.upper.length);
       
   905 
       
   906 		/*
       
   907 		 * !EOP means multiple descriptors were used to store a single
       
   908 		 * packet, if that's the case we need to toss it.  In fact, we
       
   909 		 * need to toss every packet with the EOP bit clear and the
       
   910 		 * next frame that _does_ have the EOP bit set, as it is by
       
   911 		 * definition only a frame fragment
       
   912 		 */
       
   913 		if (unlikely(!(staterr & E1000_RXD_STAT_EOP)))
       
   914 			adapter->flags2 |= FLAG2_IS_DISCARDING;
       
   915 
       
   916 		if (adapter->flags2 & FLAG2_IS_DISCARDING) {
       
   917 			/* All receives must fit into a single buffer */
       
   918 			e_dbg("Receive packet consumed multiple buffers\n");
       
   919 			/* recycle */
       
   920 			buffer_info->skb = skb;
       
   921 			if (staterr & E1000_RXD_STAT_EOP)
       
   922 				adapter->flags2 &= ~FLAG2_IS_DISCARDING;
       
   923 			goto next_desc;
       
   924 		}
       
   925 
       
   926 		if (unlikely(!adapter->ecdev &&
       
   927 					(staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) &&
       
   928 			     !(netdev->features & NETIF_F_RXALL))) {
       
   929 			/* recycle */
       
   930 			buffer_info->skb = skb;
       
   931 			goto next_desc;
       
   932 		}
       
   933 
       
   934 		/* adjust length to remove Ethernet CRC */
       
   935 		if (!(adapter->flags2 & FLAG2_CRC_STRIPPING)) {
       
   936 			/* If configured to store CRC, don't subtract FCS,
       
   937 			 * but keep the FCS bytes out of the total_rx_bytes
       
   938 			 * counter
       
   939 			 */
       
   940 			if (netdev->features & NETIF_F_RXFCS)
       
   941 				total_rx_bytes -= 4;
       
   942 			else
       
   943 				length -= 4;
       
   944 		}
       
   945 
       
   946 		total_rx_bytes += length;
       
   947 		total_rx_packets++;
       
   948 
       
   949 		/*
       
   950 		 * code added for copybreak, this should improve
       
   951 		 * performance for small packets with large amounts
       
   952 		 * of reassembly being done in the stack
       
   953 		 */
       
   954 		if (!adapter->ecdev && length < copybreak) {
       
   955 			struct sk_buff *new_skb =
       
   956 			    netdev_alloc_skb_ip_align(netdev, length);
       
   957 			if (new_skb) {
       
   958 				skb_copy_to_linear_data_offset(new_skb,
       
   959 							       -NET_IP_ALIGN,
       
   960 							       (skb->data -
       
   961 								NET_IP_ALIGN),
       
   962 							       (length +
       
   963 								NET_IP_ALIGN));
       
   964 				/* save the skb in buffer_info as good */
       
   965 				buffer_info->skb = skb;
       
   966 				skb = new_skb;
       
   967 			}
       
   968 			/* else just continue with the old one */
       
   969 		}
       
   970 		/* end copybreak code */
       
   971 		skb_put(skb, length);
       
   972 
       
   973 		/* Receive Checksum Offload */
       
   974 		e1000_rx_checksum(adapter, staterr, skb);
       
   975 
       
   976 		e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
       
   977 
       
   978 		if (adapter->ecdev) {
       
   979 			ecdev_receive(adapter->ecdev, skb->data, length);
       
   980 			adapter->ec_watchdog_jiffies = jiffies;
       
   981 		} else {
       
   982 		    e1000_receive_skb(adapter, netdev, skb, staterr,
       
   983 				      rx_desc->wb.upper.vlan);
       
   984 		}
       
   985 
       
   986 next_desc:
       
   987 		rx_desc->wb.upper.status_error &= cpu_to_le32(~0xFF);
       
   988 
       
   989 		/* return some buffers to hardware, one at a time is too slow */
       
   990 		if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
       
   991 			adapter->alloc_rx_buf(rx_ring, cleaned_count,
       
   992 					      GFP_ATOMIC);
       
   993 			cleaned_count = 0;
       
   994 		}
       
   995 
       
   996 		/* use prefetched values */
       
   997 		rx_desc = next_rxd;
       
   998 		buffer_info = next_buffer;
       
   999 
       
  1000 		staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
       
  1001 	}
       
  1002 	rx_ring->next_to_clean = i;
       
  1003 
       
  1004 	cleaned_count = e1000_desc_unused(rx_ring);
       
  1005 	if (cleaned_count)
       
  1006 		adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
       
  1007 
       
  1008 	adapter->total_rx_bytes += total_rx_bytes;
       
  1009 	adapter->total_rx_packets += total_rx_packets;
       
  1010 	return cleaned;
       
  1011 }
       
  1012 
       
  1013 static void e1000_put_txbuf(struct e1000_ring *tx_ring,
       
  1014 			    struct e1000_buffer *buffer_info)
       
  1015 {
       
  1016 	struct e1000_adapter *adapter = tx_ring->adapter;
       
  1017 
       
  1018 	if (adapter->ecdev) {
       
  1019 		return;
       
  1020 	}
       
  1021 
       
  1022 	if (buffer_info->dma) {
       
  1023 		if (buffer_info->mapped_as_page)
       
  1024 			dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
       
  1025 				       buffer_info->length, DMA_TO_DEVICE);
       
  1026 		else
       
  1027 			dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
       
  1028 					 buffer_info->length, DMA_TO_DEVICE);
       
  1029 		buffer_info->dma = 0;
       
  1030 	}
       
  1031 	if (buffer_info->skb) {
       
  1032 		dev_kfree_skb_any(buffer_info->skb);
       
  1033 		buffer_info->skb = NULL;
       
  1034 	}
       
  1035 	buffer_info->time_stamp = 0;
       
  1036 }
       
  1037 
       
  1038 static void e1000_print_hw_hang(struct work_struct *work)
       
  1039 {
       
  1040 	struct e1000_adapter *adapter = container_of(work,
       
  1041 	                                             struct e1000_adapter,
       
  1042 	                                             print_hang_task);
       
  1043 	struct net_device *netdev = adapter->netdev;
       
  1044 	struct e1000_ring *tx_ring = adapter->tx_ring;
       
  1045 	unsigned int i = tx_ring->next_to_clean;
       
  1046 	unsigned int eop = tx_ring->buffer_info[i].next_to_watch;
       
  1047 	struct e1000_tx_desc *eop_desc = E1000_TX_DESC(*tx_ring, eop);
       
  1048 	struct e1000_hw *hw = &adapter->hw;
       
  1049 	u16 phy_status, phy_1000t_status, phy_ext_status;
       
  1050 	u16 pci_status;
       
  1051 
       
  1052 	if (test_bit(__E1000_DOWN, &adapter->state))
       
  1053 		return;
       
  1054 
       
  1055 	if (!adapter->tx_hang_recheck &&
       
  1056 	    (adapter->flags2 & FLAG2_DMA_BURST)) {
       
  1057 		/* May be block on write-back, flush and detect again
       
  1058 		 * flush pending descriptor writebacks to memory
       
  1059 		 */
       
  1060 		ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
       
  1061 		/* execute the writes immediately */
       
  1062 		e1e_flush();
       
  1063 		/*
       
  1064 		 * Due to rare timing issues, write to TIDV again to ensure
       
  1065 		 * the write is successful
       
  1066 		 */
       
  1067 		ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
       
  1068 		/* execute the writes immediately */
       
  1069 		e1e_flush();
       
  1070 		adapter->tx_hang_recheck = true;
       
  1071 		return;
       
  1072 	}
       
  1073 	/* Real hang detected */
       
  1074 	adapter->tx_hang_recheck = false;
       
  1075 	if (!adapter->ecdev) {
       
  1076 		netif_stop_queue(netdev);
       
  1077 	}
       
  1078 
       
  1079 	e1e_rphy(hw, PHY_STATUS, &phy_status);
       
  1080 	e1e_rphy(hw, PHY_1000T_STATUS, &phy_1000t_status);
       
  1081 	e1e_rphy(hw, PHY_EXT_STATUS, &phy_ext_status);
       
  1082 
       
  1083 	pci_read_config_word(adapter->pdev, PCI_STATUS, &pci_status);
       
  1084 
       
  1085 	/* detected Hardware unit hang */
       
  1086 	e_err("Detected Hardware Unit Hang:\n"
       
  1087 	      "  TDH                  <%x>\n"
       
  1088 	      "  TDT                  <%x>\n"
       
  1089 	      "  next_to_use          <%x>\n"
       
  1090 	      "  next_to_clean        <%x>\n"
       
  1091 	      "buffer_info[next_to_clean]:\n"
       
  1092 	      "  time_stamp           <%lx>\n"
       
  1093 	      "  next_to_watch        <%x>\n"
       
  1094 	      "  jiffies              <%lx>\n"
       
  1095 	      "  next_to_watch.status <%x>\n"
       
  1096 	      "MAC Status             <%x>\n"
       
  1097 	      "PHY Status             <%x>\n"
       
  1098 	      "PHY 1000BASE-T Status  <%x>\n"
       
  1099 	      "PHY Extended Status    <%x>\n"
       
  1100 	      "PCI Status             <%x>\n",
       
  1101 	      readl(tx_ring->head),
       
  1102 	      readl(tx_ring->tail),
       
  1103 	      tx_ring->next_to_use,
       
  1104 	      tx_ring->next_to_clean,
       
  1105 	      tx_ring->buffer_info[eop].time_stamp,
       
  1106 	      eop,
       
  1107 	      jiffies,
       
  1108 	      eop_desc->upper.fields.status,
       
  1109 	      er32(STATUS),
       
  1110 	      phy_status,
       
  1111 	      phy_1000t_status,
       
  1112 	      phy_ext_status,
       
  1113 	      pci_status);
       
  1114 }
       
  1115 
       
  1116 /**
       
  1117  * e1000_clean_tx_irq - Reclaim resources after transmit completes
       
  1118  * @tx_ring: Tx descriptor ring
       
  1119  *
       
  1120  * the return value indicates whether actual cleaning was done, there
       
  1121  * is no guarantee that everything was cleaned
       
  1122  **/
       
  1123 static bool e1000_clean_tx_irq(struct e1000_ring *tx_ring)
       
  1124 {
       
  1125 	struct e1000_adapter *adapter = tx_ring->adapter;
       
  1126 	struct net_device *netdev = adapter->netdev;
       
  1127 	struct e1000_hw *hw = &adapter->hw;
       
  1128 	struct e1000_tx_desc *tx_desc, *eop_desc;
       
  1129 	struct e1000_buffer *buffer_info;
       
  1130 	unsigned int i, eop;
       
  1131 	unsigned int count = 0;
       
  1132 	unsigned int total_tx_bytes = 0, total_tx_packets = 0;
       
  1133 	unsigned int bytes_compl = 0, pkts_compl = 0;
       
  1134 
       
  1135 	i = tx_ring->next_to_clean;
       
  1136 	eop = tx_ring->buffer_info[i].next_to_watch;
       
  1137 	eop_desc = E1000_TX_DESC(*tx_ring, eop);
       
  1138 
       
  1139 	while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
       
  1140 	       (count < tx_ring->count)) {
       
  1141 		bool cleaned = false;
       
  1142 		rmb(); /* read buffer_info after eop_desc */
       
  1143 		for (; !cleaned; count++) {
       
  1144 			tx_desc = E1000_TX_DESC(*tx_ring, i);
       
  1145 			buffer_info = &tx_ring->buffer_info[i];
       
  1146 			cleaned = (i == eop);
       
  1147 
       
  1148 			if (cleaned) {
       
  1149 				total_tx_packets += buffer_info->segs;
       
  1150 				total_tx_bytes += buffer_info->bytecount;
       
  1151 				if (buffer_info->skb) {
       
  1152 					bytes_compl += buffer_info->skb->len;
       
  1153 					pkts_compl++;
       
  1154 				}
       
  1155 			}
       
  1156 
       
  1157 			e1000_put_txbuf(tx_ring, buffer_info);
       
  1158 			tx_desc->upper.data = 0;
       
  1159 
       
  1160 			i++;
       
  1161 			if (i == tx_ring->count)
       
  1162 				i = 0;
       
  1163 		}
       
  1164 
       
  1165 		if (i == tx_ring->next_to_use)
       
  1166 			break;
       
  1167 		eop = tx_ring->buffer_info[i].next_to_watch;
       
  1168 		eop_desc = E1000_TX_DESC(*tx_ring, eop);
       
  1169 	}
       
  1170 
       
  1171 	tx_ring->next_to_clean = i;
       
  1172 
       
  1173 	netdev_completed_queue(netdev, pkts_compl, bytes_compl);
       
  1174 
       
  1175 #define TX_WAKE_THRESHOLD 32
       
  1176 	if (!adapter->ecdev && count && netif_carrier_ok(netdev) &&
       
  1177 	    e1000_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD) {
       
  1178 		/* Make sure that anybody stopping the queue after this
       
  1179 		 * sees the new next_to_clean.
       
  1180 		 */
       
  1181 		smp_mb();
       
  1182 
       
  1183 		if (netif_queue_stopped(netdev) &&
       
  1184 		    !(test_bit(__E1000_DOWN, &adapter->state))) {
       
  1185 			netif_wake_queue(netdev);
       
  1186 			++adapter->restart_queue;
       
  1187 		}
       
  1188 	}
       
  1189 
       
  1190 	if (!adapter->ecdev && adapter->detect_tx_hung) {
       
  1191 		/*
       
  1192 		 * Detect a transmit hang in hardware, this serializes the
       
  1193 		 * check with the clearing of time_stamp and movement of i
       
  1194 		 */
       
  1195 		adapter->detect_tx_hung = false;
       
  1196 		if (tx_ring->buffer_info[i].time_stamp &&
       
  1197 		    time_after(jiffies, tx_ring->buffer_info[i].time_stamp
       
  1198 			       + (adapter->tx_timeout_factor * HZ)) &&
       
  1199 		    !(er32(STATUS) & E1000_STATUS_TXOFF))
       
  1200 			schedule_work(&adapter->print_hang_task);
       
  1201 		else
       
  1202 			adapter->tx_hang_recheck = false;
       
  1203 	}
       
  1204 	adapter->total_tx_bytes += total_tx_bytes;
       
  1205 	adapter->total_tx_packets += total_tx_packets;
       
  1206 	return count < tx_ring->count;
       
  1207 }
       
  1208 
       
  1209 /**
       
  1210  * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
       
  1211  * @rx_ring: Rx descriptor ring
       
  1212  *
       
  1213  * the return value indicates whether actual cleaning was done, there
       
  1214  * is no guarantee that everything was cleaned
       
  1215  **/
       
  1216 static bool e1000_clean_rx_irq_ps(struct e1000_ring *rx_ring, int *work_done,
       
  1217 				  int work_to_do)
       
  1218 {
       
  1219 	struct e1000_adapter *adapter = rx_ring->adapter;
       
  1220 	struct e1000_hw *hw = &adapter->hw;
       
  1221 	union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
       
  1222 	struct net_device *netdev = adapter->netdev;
       
  1223 	struct pci_dev *pdev = adapter->pdev;
       
  1224 	struct e1000_buffer *buffer_info, *next_buffer;
       
  1225 	struct e1000_ps_page *ps_page;
       
  1226 	struct sk_buff *skb;
       
  1227 	unsigned int i, j;
       
  1228 	u32 length, staterr;
       
  1229 	int cleaned_count = 0;
       
  1230 	bool cleaned = false;
       
  1231 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
       
  1232 
       
  1233 	i = rx_ring->next_to_clean;
       
  1234 	rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
       
  1235 	staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
       
  1236 	buffer_info = &rx_ring->buffer_info[i];
       
  1237 
       
  1238 	while (staterr & E1000_RXD_STAT_DD) {
       
  1239 		if (*work_done >= work_to_do)
       
  1240 			break;
       
  1241 		(*work_done)++;
       
  1242 		skb = buffer_info->skb;
       
  1243 		rmb();	/* read descriptor and rx_buffer_info after status DD */
       
  1244 
       
  1245 		/* in the packet split case this is header only */
       
  1246 		prefetch(skb->data - NET_IP_ALIGN);
       
  1247 
       
  1248 		i++;
       
  1249 		if (i == rx_ring->count)
       
  1250 			i = 0;
       
  1251 		next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
       
  1252 		prefetch(next_rxd);
       
  1253 
       
  1254 		next_buffer = &rx_ring->buffer_info[i];
       
  1255 
       
  1256 		cleaned = true;
       
  1257 		cleaned_count++;
       
  1258 		dma_unmap_single(&pdev->dev, buffer_info->dma,
       
  1259 				 adapter->rx_ps_bsize0, DMA_FROM_DEVICE);
       
  1260 		buffer_info->dma = 0;
       
  1261 
       
  1262 		/* see !EOP comment in other Rx routine */
       
  1263 		if (!(staterr & E1000_RXD_STAT_EOP))
       
  1264 			adapter->flags2 |= FLAG2_IS_DISCARDING;
       
  1265 
       
  1266 		if (adapter->flags2 & FLAG2_IS_DISCARDING) {
       
  1267 			e_dbg("Packet Split buffers didn't pick up the full packet\n");
       
  1268 			if (!adapter->ecdev) {
       
  1269 				dev_kfree_skb_irq(skb);
       
  1270 			}
       
  1271 			if (staterr & E1000_RXD_STAT_EOP)
       
  1272 				adapter->flags2 &= ~FLAG2_IS_DISCARDING;
       
  1273 			goto next_desc;
       
  1274 		}
       
  1275 
       
  1276 		if (unlikely((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) &&
       
  1277 			     !(netdev->features & NETIF_F_RXALL))) {
       
  1278 			if (!adapter->ecdev) {
       
  1279 				dev_kfree_skb_irq(skb);
       
  1280 			}
       
  1281 			goto next_desc;
       
  1282 		}
       
  1283 
       
  1284 		length = le16_to_cpu(rx_desc->wb.middle.length0);
       
  1285 
       
  1286 		if (!length) {
       
  1287 			e_dbg("Last part of the packet spanning multiple descriptors\n");
       
  1288 			if (!adapter->ecdev) {
       
  1289 				dev_kfree_skb_irq(skb);
       
  1290 			}
       
  1291 			goto next_desc;
       
  1292 		}
       
  1293 
       
  1294 		/* Good Receive */
       
  1295 		skb_put(skb, length);
       
  1296 
       
  1297 		{
       
  1298 			/*
       
  1299 			 * this looks ugly, but it seems compiler issues make
       
  1300 			 * it more efficient than reusing j
       
  1301 			 */
       
  1302 			int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
       
  1303 
       
  1304 			/*
       
  1305 			 * page alloc/put takes too long and effects small
       
  1306 			 * packet throughput, so unsplit small packets and
       
  1307 			 * save the alloc/put only valid in softirq (napi)
       
  1308 			 * context to call kmap_*
       
  1309 			 */
       
  1310 			if (l1 && (l1 <= copybreak) &&
       
  1311 			    ((length + l1) <= adapter->rx_ps_bsize0)) {
       
  1312 				u8 *vaddr;
       
  1313 
       
  1314 				ps_page = &buffer_info->ps_pages[0];
       
  1315 
       
  1316 				/*
       
  1317 				 * there is no documentation about how to call
       
  1318 				 * kmap_atomic, so we can't hold the mapping
       
  1319 				 * very long
       
  1320 				 */
       
  1321 				dma_sync_single_for_cpu(&pdev->dev,
       
  1322 							ps_page->dma,
       
  1323 							PAGE_SIZE,
       
  1324 							DMA_FROM_DEVICE);
       
  1325 				vaddr = kmap_atomic(ps_page->page);
       
  1326 				memcpy(skb_tail_pointer(skb), vaddr, l1);
       
  1327 				kunmap_atomic(vaddr);
       
  1328 				dma_sync_single_for_device(&pdev->dev,
       
  1329 							   ps_page->dma,
       
  1330 							   PAGE_SIZE,
       
  1331 							   DMA_FROM_DEVICE);
       
  1332 
       
  1333 				/* remove the CRC */
       
  1334 				if (!(adapter->flags2 & FLAG2_CRC_STRIPPING)) {
       
  1335 					if (!(netdev->features & NETIF_F_RXFCS))
       
  1336 						l1 -= 4;
       
  1337 				}
       
  1338 
       
  1339 				skb_put(skb, l1);
       
  1340 				goto copydone;
       
  1341 			} /* if */
       
  1342 		}
       
  1343 
       
  1344 		for (j = 0; j < PS_PAGE_BUFFERS; j++) {
       
  1345 			length = le16_to_cpu(rx_desc->wb.upper.length[j]);
       
  1346 			if (!length)
       
  1347 				break;
       
  1348 
       
  1349 			ps_page = &buffer_info->ps_pages[j];
       
  1350 			dma_unmap_page(&pdev->dev, ps_page->dma, PAGE_SIZE,
       
  1351 				       DMA_FROM_DEVICE);
       
  1352 			ps_page->dma = 0;
       
  1353 			skb_fill_page_desc(skb, j, ps_page->page, 0, length);
       
  1354 			ps_page->page = NULL;
       
  1355 			skb->len += length;
       
  1356 			skb->data_len += length;
       
  1357 			skb->truesize += PAGE_SIZE;
       
  1358 		}
       
  1359 
       
  1360 		/* strip the ethernet crc, problem is we're using pages now so
       
  1361 		 * this whole operation can get a little cpu intensive
       
  1362 		 */
       
  1363 		if (!(adapter->flags2 & FLAG2_CRC_STRIPPING)) {
       
  1364 			if (!(netdev->features & NETIF_F_RXFCS))
       
  1365 				pskb_trim(skb, skb->len - 4);
       
  1366 		}
       
  1367 
       
  1368 copydone:
       
  1369 		total_rx_bytes += skb->len;
       
  1370 		total_rx_packets++;
       
  1371 
       
  1372 		e1000_rx_checksum(adapter, staterr, skb);
       
  1373 
       
  1374 		e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
       
  1375 
       
  1376 		if (rx_desc->wb.upper.header_status &
       
  1377 			   cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP))
       
  1378 			adapter->rx_hdr_split++;
       
  1379 
       
  1380 		if (adapter->ecdev) {
       
  1381 			ecdev_receive(adapter->ecdev, skb->data, length);
       
  1382 			adapter->ec_watchdog_jiffies = jiffies;
       
  1383 		} else {
       
  1384 			e1000_receive_skb(adapter, netdev, skb,
       
  1385 					  staterr, rx_desc->wb.middle.vlan);
       
  1386 		}
       
  1387 
       
  1388 next_desc:
       
  1389 		rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
       
  1390 		if (!adapter->ecdev) buffer_info->skb = NULL;
       
  1391 
       
  1392 		/* return some buffers to hardware, one at a time is too slow */
       
  1393 		if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
       
  1394 			adapter->alloc_rx_buf(rx_ring, cleaned_count,
       
  1395 					      GFP_ATOMIC);
       
  1396 			cleaned_count = 0;
       
  1397 		}
       
  1398 
       
  1399 		/* use prefetched values */
       
  1400 		rx_desc = next_rxd;
       
  1401 		buffer_info = next_buffer;
       
  1402 
       
  1403 		staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
       
  1404 	}
       
  1405 	rx_ring->next_to_clean = i;
       
  1406 
       
  1407 	cleaned_count = e1000_desc_unused(rx_ring);
       
  1408 	if (cleaned_count)
       
  1409 		adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
       
  1410 
       
  1411 	adapter->total_rx_bytes += total_rx_bytes;
       
  1412 	adapter->total_rx_packets += total_rx_packets;
       
  1413 	return cleaned;
       
  1414 }
       
  1415 
       
  1416 /**
       
  1417  * e1000_consume_page - helper function
       
  1418  **/
       
  1419 static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
       
  1420                                u16 length)
       
  1421 {
       
  1422 	bi->page = NULL;
       
  1423 	skb->len += length;
       
  1424 	skb->data_len += length;
       
  1425 	skb->truesize += PAGE_SIZE;
       
  1426 }
       
  1427 
       
  1428 /**
       
  1429  * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
       
  1430  * @adapter: board private structure
       
  1431  *
       
  1432  * the return value indicates whether actual cleaning was done, there
       
  1433  * is no guarantee that everything was cleaned
       
  1434  **/
       
  1435 static bool e1000_clean_jumbo_rx_irq(struct e1000_ring *rx_ring, int *work_done,
       
  1436 				     int work_to_do)
       
  1437 {
       
  1438 	struct e1000_adapter *adapter = rx_ring->adapter;
       
  1439 	struct net_device *netdev = adapter->netdev;
       
  1440 	struct pci_dev *pdev = adapter->pdev;
       
  1441 	union e1000_rx_desc_extended *rx_desc, *next_rxd;
       
  1442 	struct e1000_buffer *buffer_info, *next_buffer;
       
  1443 	u32 length, staterr;
       
  1444 	unsigned int i;
       
  1445 	int cleaned_count = 0;
       
  1446 	bool cleaned = false;
       
  1447 	unsigned int total_rx_bytes=0, total_rx_packets=0;
       
  1448 
       
  1449 	i = rx_ring->next_to_clean;
       
  1450 	rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
       
  1451 	staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
       
  1452 	buffer_info = &rx_ring->buffer_info[i];
       
  1453 
       
  1454 	while (staterr & E1000_RXD_STAT_DD) {
       
  1455 		struct sk_buff *skb;
       
  1456 
       
  1457 		if (*work_done >= work_to_do)
       
  1458 			break;
       
  1459 		(*work_done)++;
       
  1460 		rmb();	/* read descriptor and rx_buffer_info after status DD */
       
  1461 
       
  1462 		skb = buffer_info->skb;
       
  1463 
       
  1464 		if (!adapter->ecdev)
       
  1465 			buffer_info->skb = NULL;
       
  1466 
       
  1467 		++i;
       
  1468 		if (i == rx_ring->count)
       
  1469 			i = 0;
       
  1470 		next_rxd = E1000_RX_DESC_EXT(*rx_ring, i);
       
  1471 		prefetch(next_rxd);
       
  1472 
       
  1473 		next_buffer = &rx_ring->buffer_info[i];
       
  1474 
       
  1475 		cleaned = true;
       
  1476 		cleaned_count++;
       
  1477 		dma_unmap_page(&pdev->dev, buffer_info->dma, PAGE_SIZE,
       
  1478 			       DMA_FROM_DEVICE);
       
  1479 		buffer_info->dma = 0;
       
  1480 
       
  1481 		length = le16_to_cpu(rx_desc->wb.upper.length);
       
  1482 
       
  1483 		/* errors is only valid for DD + EOP descriptors */
       
  1484 		if (unlikely((staterr & E1000_RXD_STAT_EOP) &&
       
  1485 			     ((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) &&
       
  1486 			      !(netdev->features & NETIF_F_RXALL)))) {
       
  1487 			/* recycle both page and skb */
       
  1488 			buffer_info->skb = skb;
       
  1489 			/* an error means any chain goes out the window too */
       
  1490 			if (!adapter->ecdev && rx_ring->rx_skb_top) {
       
  1491 				dev_kfree_skb_irq(rx_ring->rx_skb_top);
       
  1492 			}
       
  1493 			rx_ring->rx_skb_top = NULL;
       
  1494 			goto next_desc;
       
  1495 		}
       
  1496 
       
  1497 #define rxtop (rx_ring->rx_skb_top)
       
  1498 		if (!(staterr & E1000_RXD_STAT_EOP)) {
       
  1499 			/* this descriptor is only the beginning (or middle) */
       
  1500 			if (!rxtop) {
       
  1501 				/* this is the beginning of a chain */
       
  1502 				rxtop = skb;
       
  1503 				skb_fill_page_desc(rxtop, 0, buffer_info->page,
       
  1504 				                   0, length);
       
  1505 			} else {
       
  1506 				/* this is the middle of a chain */
       
  1507 				skb_fill_page_desc(rxtop,
       
  1508 				    skb_shinfo(rxtop)->nr_frags,
       
  1509 				    buffer_info->page, 0, length);
       
  1510 				/* re-use the skb, only consumed the page */
       
  1511 				buffer_info->skb = skb;
       
  1512 			}
       
  1513 			e1000_consume_page(buffer_info, rxtop, length);
       
  1514 			goto next_desc;
       
  1515 		} else {
       
  1516 			if (rxtop) {
       
  1517 				/* end of the chain */
       
  1518 				skb_fill_page_desc(rxtop,
       
  1519 				    skb_shinfo(rxtop)->nr_frags,
       
  1520 				    buffer_info->page, 0, length);
       
  1521 				/* re-use the current skb, we only consumed the
       
  1522 				 * page */
       
  1523 				buffer_info->skb = skb;
       
  1524 				skb = rxtop;
       
  1525 				rxtop = NULL;
       
  1526 				e1000_consume_page(buffer_info, skb, length);
       
  1527 			} else {
       
  1528 				/* no chain, got EOP, this buf is the packet
       
  1529 				 * copybreak to save the put_page/alloc_page */
       
  1530 				if (length <= copybreak &&
       
  1531 				    skb_tailroom(skb) >= length) {
       
  1532 					u8 *vaddr;
       
  1533 					vaddr = kmap_atomic(buffer_info->page);
       
  1534 					memcpy(skb_tail_pointer(skb), vaddr,
       
  1535 					       length);
       
  1536 					kunmap_atomic(vaddr);
       
  1537 					/* re-use the page, so don't erase
       
  1538 					 * buffer_info->page */
       
  1539 					skb_put(skb, length);
       
  1540 				} else {
       
  1541 					skb_fill_page_desc(skb, 0,
       
  1542 					                   buffer_info->page, 0,
       
  1543 				                           length);
       
  1544 					e1000_consume_page(buffer_info, skb,
       
  1545 					                   length);
       
  1546 				}
       
  1547 			}
       
  1548 		}
       
  1549 
       
  1550 		/* Receive Checksum Offload */
       
  1551 		e1000_rx_checksum(adapter, staterr, skb);
       
  1552 
       
  1553 		e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
       
  1554 
       
  1555 		/* probably a little skewed due to removing CRC */
       
  1556 		total_rx_bytes += skb->len;
       
  1557 		total_rx_packets++;
       
  1558 
       
  1559 		/* eth type trans needs skb->data to point to something */
       
  1560 		if (!adapter->ecdev && !pskb_may_pull(skb, ETH_HLEN)) {
       
  1561 			e_err("pskb_may_pull failed.\n");
       
  1562 			dev_kfree_skb_irq(skb);
       
  1563 			goto next_desc;
       
  1564 		}
       
  1565 
       
  1566 		if (adapter->ecdev) {
       
  1567 			ecdev_receive(adapter->ecdev, skb->data, length);
       
  1568 			adapter->ec_watchdog_jiffies = jiffies;
       
  1569 		} else {
       
  1570 			e1000_receive_skb(adapter, netdev, skb, staterr,
       
  1571 					  rx_desc->wb.upper.vlan);
       
  1572 		}
       
  1573 
       
  1574 
       
  1575 next_desc:
       
  1576 		rx_desc->wb.upper.status_error &= cpu_to_le32(~0xFF);
       
  1577 
       
  1578 		/* return some buffers to hardware, one at a time is too slow */
       
  1579 		if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
       
  1580 			adapter->alloc_rx_buf(rx_ring, cleaned_count,
       
  1581 					      GFP_ATOMIC);
       
  1582 			cleaned_count = 0;
       
  1583 		}
       
  1584 
       
  1585 		/* use prefetched values */
       
  1586 		rx_desc = next_rxd;
       
  1587 		buffer_info = next_buffer;
       
  1588 
       
  1589 		staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
       
  1590 	}
       
  1591 	rx_ring->next_to_clean = i;
       
  1592 
       
  1593 	cleaned_count = e1000_desc_unused(rx_ring);
       
  1594 	if (cleaned_count)
       
  1595 		adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
       
  1596 
       
  1597 	adapter->total_rx_bytes += total_rx_bytes;
       
  1598 	adapter->total_rx_packets += total_rx_packets;
       
  1599 	return cleaned;
       
  1600 }
       
  1601 
       
  1602 /**
       
  1603  * e1000_clean_rx_ring - Free Rx Buffers per Queue
       
  1604  * @rx_ring: Rx descriptor ring
       
  1605  **/
       
  1606 static void e1000_clean_rx_ring(struct e1000_ring *rx_ring)
       
  1607 {
       
  1608 	struct e1000_adapter *adapter = rx_ring->adapter;
       
  1609 	struct e1000_buffer *buffer_info;
       
  1610 	struct e1000_ps_page *ps_page;
       
  1611 	struct pci_dev *pdev = adapter->pdev;
       
  1612 	unsigned int i, j;
       
  1613 
       
  1614 	/* Free all the Rx ring sk_buffs */
       
  1615 	for (i = 0; i < rx_ring->count; i++) {
       
  1616 		buffer_info = &rx_ring->buffer_info[i];
       
  1617 		if (buffer_info->dma) {
       
  1618 			if (adapter->clean_rx == e1000_clean_rx_irq)
       
  1619 				dma_unmap_single(&pdev->dev, buffer_info->dma,
       
  1620 						 adapter->rx_buffer_len,
       
  1621 						 DMA_FROM_DEVICE);
       
  1622 			else if (adapter->clean_rx == e1000_clean_jumbo_rx_irq)
       
  1623 				dma_unmap_page(&pdev->dev, buffer_info->dma,
       
  1624 				               PAGE_SIZE,
       
  1625 					       DMA_FROM_DEVICE);
       
  1626 			else if (adapter->clean_rx == e1000_clean_rx_irq_ps)
       
  1627 				dma_unmap_single(&pdev->dev, buffer_info->dma,
       
  1628 						 adapter->rx_ps_bsize0,
       
  1629 						 DMA_FROM_DEVICE);
       
  1630 			buffer_info->dma = 0;
       
  1631 		}
       
  1632 
       
  1633 		if (buffer_info->page) {
       
  1634 			put_page(buffer_info->page);
       
  1635 			buffer_info->page = NULL;
       
  1636 		}
       
  1637 
       
  1638 		if (buffer_info->skb) {
       
  1639 			dev_kfree_skb(buffer_info->skb);
       
  1640 			buffer_info->skb = NULL;
       
  1641 		}
       
  1642 
       
  1643 		for (j = 0; j < PS_PAGE_BUFFERS; j++) {
       
  1644 			ps_page = &buffer_info->ps_pages[j];
       
  1645 			if (!ps_page->page)
       
  1646 				break;
       
  1647 			dma_unmap_page(&pdev->dev, ps_page->dma, PAGE_SIZE,
       
  1648 				       DMA_FROM_DEVICE);
       
  1649 			ps_page->dma = 0;
       
  1650 			put_page(ps_page->page);
       
  1651 			ps_page->page = NULL;
       
  1652 		}
       
  1653 	}
       
  1654 
       
  1655 	/* there also may be some cached data from a chained receive */
       
  1656 	if (rx_ring->rx_skb_top) {
       
  1657 		dev_kfree_skb(rx_ring->rx_skb_top);
       
  1658 		rx_ring->rx_skb_top = NULL;
       
  1659 	}
       
  1660 
       
  1661 	/* Zero out the descriptor ring */
       
  1662 	memset(rx_ring->desc, 0, rx_ring->size);
       
  1663 
       
  1664 	rx_ring->next_to_clean = 0;
       
  1665 	rx_ring->next_to_use = 0;
       
  1666 	adapter->flags2 &= ~FLAG2_IS_DISCARDING;
       
  1667 
       
  1668 	writel(0, rx_ring->head);
       
  1669 	writel(0, rx_ring->tail);
       
  1670 }
       
  1671 
       
  1672 static void e1000e_downshift_workaround(struct work_struct *work)
       
  1673 {
       
  1674 	struct e1000_adapter *adapter = container_of(work,
       
  1675 					struct e1000_adapter, downshift_task);
       
  1676 
       
  1677 	if (test_bit(__E1000_DOWN, &adapter->state))
       
  1678 		return;
       
  1679 
       
  1680 	e1000e_gig_downshift_workaround_ich8lan(&adapter->hw);
       
  1681 }
       
  1682 
       
  1683 /**
       
  1684  * e1000_intr_msi - Interrupt Handler
       
  1685  * @irq: interrupt number
       
  1686  * @data: pointer to a network interface device structure
       
  1687  **/
       
  1688 static irqreturn_t e1000_intr_msi(int irq, void *data)
       
  1689 {
       
  1690 	struct net_device *netdev = data;
       
  1691 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  1692 	struct e1000_hw *hw = &adapter->hw;
       
  1693 	u32 icr = er32(ICR);
       
  1694 
       
  1695 	if (adapter->ecdev) {
       
  1696 		int ec_work_done = 0;
       
  1697 		adapter->clean_rx(adapter->rx_ring, &ec_work_done, 100);
       
  1698 		e1000_clean_tx_irq(adapter->tx_ring);
       
  1699 		return IRQ_HANDLED;
       
  1700 	}
       
  1701 	/*
       
  1702 	 * read ICR disables interrupts using IAM
       
  1703 	 */
       
  1704 
       
  1705 	if (!adapter->ecdev && icr & E1000_ICR_LSC) {
       
  1706 		hw->mac.get_link_status = true;
       
  1707 		/*
       
  1708 		 * ICH8 workaround-- Call gig speed drop workaround on cable
       
  1709 		 * disconnect (LSC) before accessing any PHY registers
       
  1710 		 */
       
  1711 		if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
       
  1712 		    (!(er32(STATUS) & E1000_STATUS_LU)))
       
  1713 			schedule_work(&adapter->downshift_task);
       
  1714 
       
  1715 		/*
       
  1716 		 * 80003ES2LAN workaround-- For packet buffer work-around on
       
  1717 		 * link down event; disable receives here in the ISR and reset
       
  1718 		 * adapter in watchdog
       
  1719 		 */
       
  1720 		if (netif_carrier_ok(netdev) &&
       
  1721 		    adapter->flags & FLAG_RX_NEEDS_RESTART) {
       
  1722 			/* disable receives */
       
  1723 			u32 rctl = er32(RCTL);
       
  1724 			ew32(RCTL, rctl & ~E1000_RCTL_EN);
       
  1725 			adapter->flags |= FLAG_RX_RESTART_NOW;
       
  1726 		}
       
  1727 		/* guard against interrupt when we're going down */
       
  1728 		if (!test_bit(__E1000_DOWN, &adapter->state))
       
  1729 			mod_timer(&adapter->watchdog_timer, jiffies + 1);
       
  1730 	}
       
  1731 
       
  1732 	if (napi_schedule_prep(&adapter->napi)) {
       
  1733 		adapter->total_tx_bytes = 0;
       
  1734 		adapter->total_tx_packets = 0;
       
  1735 		adapter->total_rx_bytes = 0;
       
  1736 		adapter->total_rx_packets = 0;
       
  1737 		__napi_schedule(&adapter->napi);
       
  1738 	}
       
  1739 
       
  1740 	return IRQ_HANDLED;
       
  1741 }
       
  1742 
       
  1743 /**
       
  1744  * e1000_intr - Interrupt Handler
       
  1745  * @irq: interrupt number
       
  1746  * @data: pointer to a network interface device structure
       
  1747  **/
       
  1748 static irqreturn_t e1000_intr(int irq, void *data)
       
  1749 {
       
  1750 	struct net_device *netdev = data;
       
  1751 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  1752 	struct e1000_hw *hw = &adapter->hw;
       
  1753 	u32 rctl, icr = er32(ICR);
       
  1754 
       
  1755 	if (!icr || test_bit(__E1000_DOWN, &adapter->state))
       
  1756 		return IRQ_NONE;  /* Not our interrupt */
       
  1757 
       
  1758 	/*
       
  1759 	 * IMS will not auto-mask if INT_ASSERTED is not set, and if it is
       
  1760 	 * not set, then the adapter didn't send an interrupt
       
  1761 	 */
       
  1762 	if (!adapter->ecdev && !(icr & E1000_ICR_INT_ASSERTED))
       
  1763 		return IRQ_NONE;
       
  1764 
       
  1765 	/*
       
  1766 	 * Interrupt Auto-Mask...upon reading ICR,
       
  1767 	 * interrupts are masked.  No need for the
       
  1768 	 * IMC write
       
  1769 	 */
       
  1770 
       
  1771 	if (icr & E1000_ICR_LSC) {
       
  1772 		hw->mac.get_link_status = true;
       
  1773 		/*
       
  1774 		 * ICH8 workaround-- Call gig speed drop workaround on cable
       
  1775 		 * disconnect (LSC) before accessing any PHY registers
       
  1776 		 */
       
  1777 		if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
       
  1778 		    (!(er32(STATUS) & E1000_STATUS_LU)))
       
  1779 			schedule_work(&adapter->downshift_task);
       
  1780 
       
  1781 		/*
       
  1782 		 * 80003ES2LAN workaround--
       
  1783 		 * For packet buffer work-around on link down event;
       
  1784 		 * disable receives here in the ISR and
       
  1785 		 * reset adapter in watchdog
       
  1786 		 */
       
  1787 		if (netif_carrier_ok(netdev) &&
       
  1788 		    (adapter->flags & FLAG_RX_NEEDS_RESTART)) {
       
  1789 			/* disable receives */
       
  1790 			rctl = er32(RCTL);
       
  1791 			ew32(RCTL, rctl & ~E1000_RCTL_EN);
       
  1792 			adapter->flags |= FLAG_RX_RESTART_NOW;
       
  1793 		}
       
  1794 		/* guard against interrupt when we're going down */
       
  1795 		if (!test_bit(__E1000_DOWN, &adapter->state))
       
  1796 			mod_timer(&adapter->watchdog_timer, jiffies + 1);
       
  1797 	}
       
  1798 
       
  1799 	if (adapter->ecdev) {
       
  1800 		int ec_work_done = 0;
       
  1801 		adapter->clean_rx(adapter->rx_ring, &ec_work_done, 100);
       
  1802 		e1000_clean_tx_irq(adapter->tx_ring);
       
  1803 		return IRQ_HANDLED;
       
  1804 	}
       
  1805 
       
  1806 	if (napi_schedule_prep(&adapter->napi)) {
       
  1807 		adapter->total_tx_bytes = 0;
       
  1808 		adapter->total_tx_packets = 0;
       
  1809 		adapter->total_rx_bytes = 0;
       
  1810 		adapter->total_rx_packets = 0;
       
  1811 		__napi_schedule(&adapter->napi);
       
  1812 	}
       
  1813 
       
  1814 	return IRQ_HANDLED;
       
  1815 }
       
  1816 
       
  1817 static irqreturn_t e1000_msix_other(int irq, void *data)
       
  1818 {
       
  1819 	struct net_device *netdev = data;
       
  1820 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  1821 	struct e1000_hw *hw = &adapter->hw;
       
  1822 	u32 icr = er32(ICR);
       
  1823 
       
  1824 	if (!(icr & E1000_ICR_INT_ASSERTED)) {
       
  1825 		if (!test_bit(__E1000_DOWN, &adapter->state))
       
  1826 			ew32(IMS, E1000_IMS_OTHER);
       
  1827 		return IRQ_NONE;
       
  1828 	}
       
  1829 
       
  1830 	if (icr & adapter->eiac_mask)
       
  1831 		ew32(ICS, (icr & adapter->eiac_mask));
       
  1832 
       
  1833 	if (icr & E1000_ICR_OTHER) {
       
  1834 		if (!(icr & E1000_ICR_LSC))
       
  1835 			goto no_link_interrupt;
       
  1836 		hw->mac.get_link_status = true;
       
  1837 		/* guard against interrupt when we're going down */
       
  1838 		if (!adapter->ecdev && !test_bit(__E1000_DOWN, &adapter->state))
       
  1839 			mod_timer(&adapter->watchdog_timer, jiffies + 1);
       
  1840 	}
       
  1841 
       
  1842 no_link_interrupt:
       
  1843 	if (!test_bit(__E1000_DOWN, &adapter->state))
       
  1844 		ew32(IMS, E1000_IMS_LSC | E1000_IMS_OTHER);
       
  1845 
       
  1846 	return IRQ_HANDLED;
       
  1847 }
       
  1848 
       
  1849 
       
  1850 static irqreturn_t e1000_intr_msix_tx(int irq, void *data)
       
  1851 {
       
  1852 	struct net_device *netdev = data;
       
  1853 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  1854 	struct e1000_hw *hw = &adapter->hw;
       
  1855 	struct e1000_ring *tx_ring = adapter->tx_ring;
       
  1856 
       
  1857 
       
  1858 	adapter->total_tx_bytes = 0;
       
  1859 	adapter->total_tx_packets = 0;
       
  1860 
       
  1861 	if (!e1000_clean_tx_irq(tx_ring))
       
  1862 		/* Ring was not completely cleaned, so fire another interrupt */
       
  1863 		ew32(ICS, tx_ring->ims_val);
       
  1864 
       
  1865 	return IRQ_HANDLED;
       
  1866 }
       
  1867 
       
  1868 static irqreturn_t e1000_intr_msix_rx(int irq, void *data)
       
  1869 {
       
  1870 	struct net_device *netdev = data;
       
  1871 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  1872 	struct e1000_ring *rx_ring = adapter->rx_ring;
       
  1873 
       
  1874 	/* Write the ITR value calculated at the end of the
       
  1875 	 * previous interrupt.
       
  1876 	 */
       
  1877 	if (rx_ring->set_itr) {
       
  1878 		writel(1000000000 / (rx_ring->itr_val * 256),
       
  1879 		       rx_ring->itr_register);
       
  1880 		rx_ring->set_itr = 0;
       
  1881 	}
       
  1882 
       
  1883 	if (adapter->ecdev) {
       
  1884 		int ec_work_done = 0;
       
  1885 		adapter->clean_rx(adapter->rx_ring, &ec_work_done, 100);
       
  1886 	} else {
       
  1887 		if (napi_schedule_prep(&adapter->napi)) {
       
  1888 			adapter->total_rx_bytes = 0;
       
  1889 			adapter->total_rx_packets = 0;
       
  1890 			__napi_schedule(&adapter->napi);
       
  1891 		}
       
  1892 	}
       
  1893 	return IRQ_HANDLED;
       
  1894 }
       
  1895 
       
  1896 /**
       
  1897  * e1000_configure_msix - Configure MSI-X hardware
       
  1898  *
       
  1899  * e1000_configure_msix sets up the hardware to properly
       
  1900  * generate MSI-X interrupts.
       
  1901  **/
       
  1902 static void e1000_configure_msix(struct e1000_adapter *adapter)
       
  1903 {
       
  1904 	struct e1000_hw *hw = &adapter->hw;
       
  1905 	struct e1000_ring *rx_ring = adapter->rx_ring;
       
  1906 	struct e1000_ring *tx_ring = adapter->tx_ring;
       
  1907 	int vector = 0;
       
  1908 	u32 ctrl_ext, ivar = 0;
       
  1909 
       
  1910 	adapter->eiac_mask = 0;
       
  1911 
       
  1912 	/* Workaround issue with spurious interrupts on 82574 in MSI-X mode */
       
  1913 	if (hw->mac.type == e1000_82574) {
       
  1914 		u32 rfctl = er32(RFCTL);
       
  1915 		rfctl |= E1000_RFCTL_ACK_DIS;
       
  1916 		ew32(RFCTL, rfctl);
       
  1917 	}
       
  1918 
       
  1919 #define E1000_IVAR_INT_ALLOC_VALID	0x8
       
  1920 	/* Configure Rx vector */
       
  1921 	rx_ring->ims_val = E1000_IMS_RXQ0;
       
  1922 	adapter->eiac_mask |= rx_ring->ims_val;
       
  1923 	if (rx_ring->itr_val)
       
  1924 		writel(1000000000 / (rx_ring->itr_val * 256),
       
  1925 		       rx_ring->itr_register);
       
  1926 	else
       
  1927 		writel(1, rx_ring->itr_register);
       
  1928 	ivar = E1000_IVAR_INT_ALLOC_VALID | vector;
       
  1929 
       
  1930 	/* Configure Tx vector */
       
  1931 	tx_ring->ims_val = E1000_IMS_TXQ0;
       
  1932 	vector++;
       
  1933 	if (tx_ring->itr_val)
       
  1934 		writel(1000000000 / (tx_ring->itr_val * 256),
       
  1935 		       tx_ring->itr_register);
       
  1936 	else
       
  1937 		writel(1, tx_ring->itr_register);
       
  1938 	adapter->eiac_mask |= tx_ring->ims_val;
       
  1939 	ivar |= ((E1000_IVAR_INT_ALLOC_VALID | vector) << 8);
       
  1940 
       
  1941 	/* set vector for Other Causes, e.g. link changes */
       
  1942 	vector++;
       
  1943 	ivar |= ((E1000_IVAR_INT_ALLOC_VALID | vector) << 16);
       
  1944 	if (rx_ring->itr_val)
       
  1945 		writel(1000000000 / (rx_ring->itr_val * 256),
       
  1946 		       hw->hw_addr + E1000_EITR_82574(vector));
       
  1947 	else
       
  1948 		writel(1, hw->hw_addr + E1000_EITR_82574(vector));
       
  1949 
       
  1950 	/* Cause Tx interrupts on every write back */
       
  1951 	ivar |= (1 << 31);
       
  1952 
       
  1953 	ew32(IVAR, ivar);
       
  1954 
       
  1955 	/* enable MSI-X PBA support */
       
  1956 	ctrl_ext = er32(CTRL_EXT);
       
  1957 	ctrl_ext |= E1000_CTRL_EXT_PBA_CLR;
       
  1958 
       
  1959 	/* Auto-Mask Other interrupts upon ICR read */
       
  1960 #define E1000_EIAC_MASK_82574   0x01F00000
       
  1961 	ew32(IAM, ~E1000_EIAC_MASK_82574 | E1000_IMS_OTHER);
       
  1962 	ctrl_ext |= E1000_CTRL_EXT_EIAME;
       
  1963 	ew32(CTRL_EXT, ctrl_ext);
       
  1964 	e1e_flush();
       
  1965 }
       
  1966 
       
  1967 void e1000e_reset_interrupt_capability(struct e1000_adapter *adapter)
       
  1968 {
       
  1969 	if (adapter->msix_entries) {
       
  1970 		pci_disable_msix(adapter->pdev);
       
  1971 		kfree(adapter->msix_entries);
       
  1972 		adapter->msix_entries = NULL;
       
  1973 	} else if (adapter->flags & FLAG_MSI_ENABLED) {
       
  1974 		pci_disable_msi(adapter->pdev);
       
  1975 		adapter->flags &= ~FLAG_MSI_ENABLED;
       
  1976 	}
       
  1977 }
       
  1978 
       
  1979 /**
       
  1980  * e1000e_set_interrupt_capability - set MSI or MSI-X if supported
       
  1981  *
       
  1982  * Attempt to configure interrupts using the best available
       
  1983  * capabilities of the hardware and kernel.
       
  1984  **/
       
  1985 void e1000e_set_interrupt_capability(struct e1000_adapter *adapter)
       
  1986 {
       
  1987 	int err;
       
  1988 	int i;
       
  1989 
       
  1990 	switch (adapter->int_mode) {
       
  1991 	case E1000E_INT_MODE_MSIX:
       
  1992 		if (adapter->flags & FLAG_HAS_MSIX) {
       
  1993 			adapter->num_vectors = 3; /* RxQ0, TxQ0 and other */
       
  1994 			adapter->msix_entries = kcalloc(adapter->num_vectors,
       
  1995 						      sizeof(struct msix_entry),
       
  1996 						      GFP_KERNEL);
       
  1997 			if (adapter->msix_entries) {
       
  1998 				for (i = 0; i < adapter->num_vectors; i++)
       
  1999 					adapter->msix_entries[i].entry = i;
       
  2000 
       
  2001 				err = pci_enable_msix(adapter->pdev,
       
  2002 						      adapter->msix_entries,
       
  2003 						      adapter->num_vectors);
       
  2004 				if (err == 0)
       
  2005 					return;
       
  2006 			}
       
  2007 			/* MSI-X failed, so fall through and try MSI */
       
  2008 			e_err("Failed to initialize MSI-X interrupts.  Falling back to MSI interrupts.\n");
       
  2009 			e1000e_reset_interrupt_capability(adapter);
       
  2010 		}
       
  2011 		adapter->int_mode = E1000E_INT_MODE_MSI;
       
  2012 		/* Fall through */
       
  2013 	case E1000E_INT_MODE_MSI:
       
  2014 		if (!pci_enable_msi(adapter->pdev)) {
       
  2015 			adapter->flags |= FLAG_MSI_ENABLED;
       
  2016 		} else {
       
  2017 			adapter->int_mode = E1000E_INT_MODE_LEGACY;
       
  2018 			e_err("Failed to initialize MSI interrupts.  Falling back to legacy interrupts.\n");
       
  2019 		}
       
  2020 		/* Fall through */
       
  2021 	case E1000E_INT_MODE_LEGACY:
       
  2022 		/* Don't do anything; this is the system default */
       
  2023 		break;
       
  2024 	}
       
  2025 
       
  2026 	/* store the number of vectors being used */
       
  2027 	adapter->num_vectors = 1;
       
  2028 }
       
  2029 
       
  2030 /**
       
  2031  * e1000_request_msix - Initialize MSI-X interrupts
       
  2032  *
       
  2033  * e1000_request_msix allocates MSI-X vectors and requests interrupts from the
       
  2034  * kernel.
       
  2035  **/
       
  2036 static int e1000_request_msix(struct e1000_adapter *adapter)
       
  2037 {
       
  2038 	struct net_device *netdev = adapter->netdev;
       
  2039 	int err = 0, vector = 0;
       
  2040 
       
  2041 	if (strlen(netdev->name) < (IFNAMSIZ - 5))
       
  2042 		snprintf(adapter->rx_ring->name,
       
  2043 			 sizeof(adapter->rx_ring->name) - 1,
       
  2044 			 "%s-rx-0", netdev->name);
       
  2045 	else
       
  2046 		memcpy(adapter->rx_ring->name, netdev->name, IFNAMSIZ);
       
  2047 	err = request_irq(adapter->msix_entries[vector].vector,
       
  2048 			  e1000_intr_msix_rx, 0, adapter->rx_ring->name,
       
  2049 			  netdev);
       
  2050 	if (err)
       
  2051 		return err;
       
  2052 	adapter->rx_ring->itr_register = adapter->hw.hw_addr +
       
  2053 	    E1000_EITR_82574(vector);
       
  2054 	adapter->rx_ring->itr_val = adapter->itr;
       
  2055 	vector++;
       
  2056 
       
  2057 	if (strlen(netdev->name) < (IFNAMSIZ - 5))
       
  2058 		snprintf(adapter->tx_ring->name,
       
  2059 			 sizeof(adapter->tx_ring->name) - 1,
       
  2060 			 "%s-tx-0", netdev->name);
       
  2061 	else
       
  2062 		memcpy(adapter->tx_ring->name, netdev->name, IFNAMSIZ);
       
  2063 	err = request_irq(adapter->msix_entries[vector].vector,
       
  2064 			  e1000_intr_msix_tx, 0, adapter->tx_ring->name,
       
  2065 			  netdev);
       
  2066 	if (err)
       
  2067 		return err;
       
  2068 	adapter->tx_ring->itr_register = adapter->hw.hw_addr +
       
  2069 	    E1000_EITR_82574(vector);
       
  2070 	adapter->tx_ring->itr_val = adapter->itr;
       
  2071 	vector++;
       
  2072 
       
  2073 	err = request_irq(adapter->msix_entries[vector].vector,
       
  2074 			  e1000_msix_other, 0, netdev->name, netdev);
       
  2075 	if (err)
       
  2076 		return err;
       
  2077 
       
  2078 	e1000_configure_msix(adapter);
       
  2079 
       
  2080 	return 0;
       
  2081 }
       
  2082 
       
  2083 /**
       
  2084  * e1000_request_irq - initialize interrupts
       
  2085  *
       
  2086  * Attempts to configure interrupts using the best available
       
  2087  * capabilities of the hardware and kernel.
       
  2088  **/
       
  2089 static int e1000_request_irq(struct e1000_adapter *adapter)
       
  2090 {
       
  2091 	struct net_device *netdev = adapter->netdev;
       
  2092 	int err;
       
  2093 
       
  2094 	if (adapter->ecdev)
       
  2095 		return 0;
       
  2096 
       
  2097 	if (adapter->msix_entries) {
       
  2098 		err = e1000_request_msix(adapter);
       
  2099 		if (!err)
       
  2100 			return err;
       
  2101 		/* fall back to MSI */
       
  2102 		e1000e_reset_interrupt_capability(adapter);
       
  2103 		adapter->int_mode = E1000E_INT_MODE_MSI;
       
  2104 		e1000e_set_interrupt_capability(adapter);
       
  2105 	}
       
  2106 	if (adapter->flags & FLAG_MSI_ENABLED) {
       
  2107 		err = request_irq(adapter->pdev->irq, e1000_intr_msi, 0,
       
  2108 				  netdev->name, netdev);
       
  2109 		if (!err)
       
  2110 			return err;
       
  2111 
       
  2112 		/* fall back to legacy interrupt */
       
  2113 		e1000e_reset_interrupt_capability(adapter);
       
  2114 		adapter->int_mode = E1000E_INT_MODE_LEGACY;
       
  2115 	}
       
  2116 
       
  2117 	err = request_irq(adapter->pdev->irq, e1000_intr, IRQF_SHARED,
       
  2118 			  netdev->name, netdev);
       
  2119 	if (err)
       
  2120 		e_err("Unable to allocate interrupt, Error: %d\n", err);
       
  2121 
       
  2122 	return err;
       
  2123 }
       
  2124 
       
  2125 static void e1000_free_irq(struct e1000_adapter *adapter)
       
  2126 {
       
  2127 	struct net_device *netdev = adapter->netdev;
       
  2128 
       
  2129 	if (adapter->ecdev)
       
  2130 		return;
       
  2131 
       
  2132 	if (adapter->msix_entries) {
       
  2133 		int vector = 0;
       
  2134 
       
  2135 		free_irq(adapter->msix_entries[vector].vector, netdev);
       
  2136 		vector++;
       
  2137 
       
  2138 		free_irq(adapter->msix_entries[vector].vector, netdev);
       
  2139 		vector++;
       
  2140 
       
  2141 		/* Other Causes interrupt vector */
       
  2142 		free_irq(adapter->msix_entries[vector].vector, netdev);
       
  2143 		return;
       
  2144 	}
       
  2145 
       
  2146 	free_irq(adapter->pdev->irq, netdev);
       
  2147 }
       
  2148 
       
  2149 /**
       
  2150  * e1000_irq_disable - Mask off interrupt generation on the NIC
       
  2151  **/
       
  2152 static void e1000_irq_disable(struct e1000_adapter *adapter)
       
  2153 {
       
  2154 	struct e1000_hw *hw = &adapter->hw;
       
  2155 
       
  2156 	if (adapter->ecdev)
       
  2157 		return;
       
  2158 
       
  2159 	ew32(IMC, ~0);
       
  2160 	if (adapter->msix_entries)
       
  2161 		ew32(EIAC_82574, 0);
       
  2162 	e1e_flush();
       
  2163 
       
  2164 	if (adapter->msix_entries) {
       
  2165 		int i;
       
  2166 		for (i = 0; i < adapter->num_vectors; i++)
       
  2167 			synchronize_irq(adapter->msix_entries[i].vector);
       
  2168 	} else {
       
  2169 		synchronize_irq(adapter->pdev->irq);
       
  2170 	}
       
  2171 }
       
  2172 
       
  2173 /**
       
  2174  * e1000_irq_enable - Enable default interrupt generation settings
       
  2175  **/
       
  2176 static void e1000_irq_enable(struct e1000_adapter *adapter)
       
  2177 {
       
  2178 	struct e1000_hw *hw = &adapter->hw;
       
  2179 
       
  2180 	if (adapter->ecdev)
       
  2181 		return;
       
  2182 
       
  2183 	if (adapter->msix_entries) {
       
  2184 		ew32(EIAC_82574, adapter->eiac_mask & E1000_EIAC_MASK_82574);
       
  2185 		ew32(IMS, adapter->eiac_mask | E1000_IMS_OTHER | E1000_IMS_LSC);
       
  2186 	} else {
       
  2187 		ew32(IMS, IMS_ENABLE_MASK);
       
  2188 	}
       
  2189 	e1e_flush();
       
  2190 }
       
  2191 
       
  2192 /**
       
  2193  * e1000e_get_hw_control - get control of the h/w from f/w
       
  2194  * @adapter: address of board private structure
       
  2195  *
       
  2196  * e1000e_get_hw_control sets {CTRL_EXT|SWSM}:DRV_LOAD bit.
       
  2197  * For ASF and Pass Through versions of f/w this means that
       
  2198  * the driver is loaded. For AMT version (only with 82573)
       
  2199  * of the f/w this means that the network i/f is open.
       
  2200  **/
       
  2201 void e1000e_get_hw_control(struct e1000_adapter *adapter)
       
  2202 {
       
  2203 	struct e1000_hw *hw = &adapter->hw;
       
  2204 	u32 ctrl_ext;
       
  2205 	u32 swsm;
       
  2206 
       
  2207 	/* Let firmware know the driver has taken over */
       
  2208 	if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
       
  2209 		swsm = er32(SWSM);
       
  2210 		ew32(SWSM, swsm | E1000_SWSM_DRV_LOAD);
       
  2211 	} else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
       
  2212 		ctrl_ext = er32(CTRL_EXT);
       
  2213 		ew32(CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
       
  2214 	}
       
  2215 }
       
  2216 
       
  2217 /**
       
  2218  * e1000e_release_hw_control - release control of the h/w to f/w
       
  2219  * @adapter: address of board private structure
       
  2220  *
       
  2221  * e1000e_release_hw_control resets {CTRL_EXT|SWSM}:DRV_LOAD bit.
       
  2222  * For ASF and Pass Through versions of f/w this means that the
       
  2223  * driver is no longer loaded. For AMT version (only with 82573) i
       
  2224  * of the f/w this means that the network i/f is closed.
       
  2225  *
       
  2226  **/
       
  2227 void e1000e_release_hw_control(struct e1000_adapter *adapter)
       
  2228 {
       
  2229 	struct e1000_hw *hw = &adapter->hw;
       
  2230 	u32 ctrl_ext;
       
  2231 	u32 swsm;
       
  2232 
       
  2233 	/* Let firmware taken over control of h/w */
       
  2234 	if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
       
  2235 		swsm = er32(SWSM);
       
  2236 		ew32(SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
       
  2237 	} else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
       
  2238 		ctrl_ext = er32(CTRL_EXT);
       
  2239 		ew32(CTRL_EXT, ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
       
  2240 	}
       
  2241 }
       
  2242 
       
  2243 /**
       
  2244  * @e1000_alloc_ring - allocate memory for a ring structure
       
  2245  **/
       
  2246 static int e1000_alloc_ring_dma(struct e1000_adapter *adapter,
       
  2247 				struct e1000_ring *ring)
       
  2248 {
       
  2249 	struct pci_dev *pdev = adapter->pdev;
       
  2250 
       
  2251 	ring->desc = dma_alloc_coherent(&pdev->dev, ring->size, &ring->dma,
       
  2252 					GFP_KERNEL);
       
  2253 	if (!ring->desc)
       
  2254 		return -ENOMEM;
       
  2255 
       
  2256 	return 0;
       
  2257 }
       
  2258 
       
  2259 /**
       
  2260  * e1000e_setup_tx_resources - allocate Tx resources (Descriptors)
       
  2261  * @tx_ring: Tx descriptor ring
       
  2262  *
       
  2263  * Return 0 on success, negative on failure
       
  2264  **/
       
  2265 int e1000e_setup_tx_resources(struct e1000_ring *tx_ring)
       
  2266 {
       
  2267 	struct e1000_adapter *adapter = tx_ring->adapter;
       
  2268 	int err = -ENOMEM, size;
       
  2269 
       
  2270 	size = sizeof(struct e1000_buffer) * tx_ring->count;
       
  2271 	tx_ring->buffer_info = vzalloc(size);
       
  2272 	if (!tx_ring->buffer_info)
       
  2273 		goto err;
       
  2274 
       
  2275 	/* round up to nearest 4K */
       
  2276 	tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
       
  2277 	tx_ring->size = ALIGN(tx_ring->size, 4096);
       
  2278 
       
  2279 	err = e1000_alloc_ring_dma(adapter, tx_ring);
       
  2280 	if (err)
       
  2281 		goto err;
       
  2282 
       
  2283 	tx_ring->next_to_use = 0;
       
  2284 	tx_ring->next_to_clean = 0;
       
  2285 
       
  2286 	return 0;
       
  2287 err:
       
  2288 	vfree(tx_ring->buffer_info);
       
  2289 	e_err("Unable to allocate memory for the transmit descriptor ring\n");
       
  2290 	return err;
       
  2291 }
       
  2292 
       
  2293 /**
       
  2294  * e1000e_setup_rx_resources - allocate Rx resources (Descriptors)
       
  2295  * @rx_ring: Rx descriptor ring
       
  2296  *
       
  2297  * Returns 0 on success, negative on failure
       
  2298  **/
       
  2299 int e1000e_setup_rx_resources(struct e1000_ring *rx_ring)
       
  2300 {
       
  2301 	struct e1000_adapter *adapter = rx_ring->adapter;
       
  2302 	struct e1000_buffer *buffer_info;
       
  2303 	int i, size, desc_len, err = -ENOMEM;
       
  2304 
       
  2305 	size = sizeof(struct e1000_buffer) * rx_ring->count;
       
  2306 	rx_ring->buffer_info = vzalloc(size);
       
  2307 	if (!rx_ring->buffer_info)
       
  2308 		goto err;
       
  2309 
       
  2310 	for (i = 0; i < rx_ring->count; i++) {
       
  2311 		buffer_info = &rx_ring->buffer_info[i];
       
  2312 		buffer_info->ps_pages = kcalloc(PS_PAGE_BUFFERS,
       
  2313 						sizeof(struct e1000_ps_page),
       
  2314 						GFP_KERNEL);
       
  2315 		if (!buffer_info->ps_pages)
       
  2316 			goto err_pages;
       
  2317 	}
       
  2318 
       
  2319 	desc_len = sizeof(union e1000_rx_desc_packet_split);
       
  2320 
       
  2321 	/* Round up to nearest 4K */
       
  2322 	rx_ring->size = rx_ring->count * desc_len;
       
  2323 	rx_ring->size = ALIGN(rx_ring->size, 4096);
       
  2324 
       
  2325 	err = e1000_alloc_ring_dma(adapter, rx_ring);
       
  2326 	if (err)
       
  2327 		goto err_pages;
       
  2328 
       
  2329 	rx_ring->next_to_clean = 0;
       
  2330 	rx_ring->next_to_use = 0;
       
  2331 	rx_ring->rx_skb_top = NULL;
       
  2332 
       
  2333 	return 0;
       
  2334 
       
  2335 err_pages:
       
  2336 	for (i = 0; i < rx_ring->count; i++) {
       
  2337 		buffer_info = &rx_ring->buffer_info[i];
       
  2338 		kfree(buffer_info->ps_pages);
       
  2339 	}
       
  2340 err:
       
  2341 	vfree(rx_ring->buffer_info);
       
  2342 	e_err("Unable to allocate memory for the receive descriptor ring\n");
       
  2343 	return err;
       
  2344 }
       
  2345 
       
  2346 /**
       
  2347  * e1000_clean_tx_ring - Free Tx Buffers
       
  2348  * @tx_ring: Tx descriptor ring
       
  2349  **/
       
  2350 static void e1000_clean_tx_ring(struct e1000_ring *tx_ring)
       
  2351 {
       
  2352 	struct e1000_adapter *adapter = tx_ring->adapter;
       
  2353 	struct e1000_buffer *buffer_info;
       
  2354 	unsigned long size;
       
  2355 	unsigned int i;
       
  2356 
       
  2357 	for (i = 0; i < tx_ring->count; i++) {
       
  2358 		buffer_info = &tx_ring->buffer_info[i];
       
  2359 		e1000_put_txbuf(tx_ring, buffer_info);
       
  2360 	}
       
  2361 
       
  2362 	netdev_reset_queue(adapter->netdev);
       
  2363 	size = sizeof(struct e1000_buffer) * tx_ring->count;
       
  2364 	memset(tx_ring->buffer_info, 0, size);
       
  2365 
       
  2366 	memset(tx_ring->desc, 0, tx_ring->size);
       
  2367 
       
  2368 	tx_ring->next_to_use = 0;
       
  2369 	tx_ring->next_to_clean = 0;
       
  2370 
       
  2371 	writel(0, tx_ring->head);
       
  2372 	writel(0, tx_ring->tail);
       
  2373 }
       
  2374 
       
  2375 /**
       
  2376  * e1000e_free_tx_resources - Free Tx Resources per Queue
       
  2377  * @tx_ring: Tx descriptor ring
       
  2378  *
       
  2379  * Free all transmit software resources
       
  2380  **/
       
  2381 void e1000e_free_tx_resources(struct e1000_ring *tx_ring)
       
  2382 {
       
  2383 	struct e1000_adapter *adapter = tx_ring->adapter;
       
  2384 	struct pci_dev *pdev = adapter->pdev;
       
  2385 
       
  2386 	e1000_clean_tx_ring(tx_ring);
       
  2387 
       
  2388 	vfree(tx_ring->buffer_info);
       
  2389 	tx_ring->buffer_info = NULL;
       
  2390 
       
  2391 	dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
       
  2392 			  tx_ring->dma);
       
  2393 	tx_ring->desc = NULL;
       
  2394 }
       
  2395 
       
  2396 /**
       
  2397  * e1000e_free_rx_resources - Free Rx Resources
       
  2398  * @rx_ring: Rx descriptor ring
       
  2399  *
       
  2400  * Free all receive software resources
       
  2401  **/
       
  2402 void e1000e_free_rx_resources(struct e1000_ring *rx_ring)
       
  2403 {
       
  2404 	struct e1000_adapter *adapter = rx_ring->adapter;
       
  2405 	struct pci_dev *pdev = adapter->pdev;
       
  2406 	int i;
       
  2407 
       
  2408 	e1000_clean_rx_ring(rx_ring);
       
  2409 
       
  2410 	for (i = 0; i < rx_ring->count; i++)
       
  2411 		kfree(rx_ring->buffer_info[i].ps_pages);
       
  2412 
       
  2413 	vfree(rx_ring->buffer_info);
       
  2414 	rx_ring->buffer_info = NULL;
       
  2415 
       
  2416 	dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
       
  2417 			  rx_ring->dma);
       
  2418 	rx_ring->desc = NULL;
       
  2419 }
       
  2420 
       
  2421 /**
       
  2422  * e1000_update_itr - update the dynamic ITR value based on statistics
       
  2423  * @adapter: pointer to adapter
       
  2424  * @itr_setting: current adapter->itr
       
  2425  * @packets: the number of packets during this measurement interval
       
  2426  * @bytes: the number of bytes during this measurement interval
       
  2427  *
       
  2428  *      Stores a new ITR value based on packets and byte
       
  2429  *      counts during the last interrupt.  The advantage of per interrupt
       
  2430  *      computation is faster updates and more accurate ITR for the current
       
  2431  *      traffic pattern.  Constants in this function were computed
       
  2432  *      based on theoretical maximum wire speed and thresholds were set based
       
  2433  *      on testing data as well as attempting to minimize response time
       
  2434  *      while increasing bulk throughput.  This functionality is controlled
       
  2435  *      by the InterruptThrottleRate module parameter.
       
  2436  **/
       
  2437 static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
       
  2438 				     u16 itr_setting, int packets,
       
  2439 				     int bytes)
       
  2440 {
       
  2441 	unsigned int retval = itr_setting;
       
  2442 
       
  2443 	if (packets == 0)
       
  2444 		return itr_setting;
       
  2445 
       
  2446 	switch (itr_setting) {
       
  2447 	case lowest_latency:
       
  2448 		/* handle TSO and jumbo frames */
       
  2449 		if (bytes/packets > 8000)
       
  2450 			retval = bulk_latency;
       
  2451 		else if ((packets < 5) && (bytes > 512))
       
  2452 			retval = low_latency;
       
  2453 		break;
       
  2454 	case low_latency:  /* 50 usec aka 20000 ints/s */
       
  2455 		if (bytes > 10000) {
       
  2456 			/* this if handles the TSO accounting */
       
  2457 			if (bytes/packets > 8000)
       
  2458 				retval = bulk_latency;
       
  2459 			else if ((packets < 10) || ((bytes/packets) > 1200))
       
  2460 				retval = bulk_latency;
       
  2461 			else if ((packets > 35))
       
  2462 				retval = lowest_latency;
       
  2463 		} else if (bytes/packets > 2000) {
       
  2464 			retval = bulk_latency;
       
  2465 		} else if (packets <= 2 && bytes < 512) {
       
  2466 			retval = lowest_latency;
       
  2467 		}
       
  2468 		break;
       
  2469 	case bulk_latency: /* 250 usec aka 4000 ints/s */
       
  2470 		if (bytes > 25000) {
       
  2471 			if (packets > 35)
       
  2472 				retval = low_latency;
       
  2473 		} else if (bytes < 6000) {
       
  2474 			retval = low_latency;
       
  2475 		}
       
  2476 		break;
       
  2477 	}
       
  2478 
       
  2479 	return retval;
       
  2480 }
       
  2481 
       
  2482 static void e1000_set_itr(struct e1000_adapter *adapter)
       
  2483 {
       
  2484 	struct e1000_hw *hw = &adapter->hw;
       
  2485 	u16 current_itr;
       
  2486 	u32 new_itr = adapter->itr;
       
  2487 
       
  2488 	/* for non-gigabit speeds, just fix the interrupt rate at 4000 */
       
  2489 	if (adapter->link_speed != SPEED_1000) {
       
  2490 		current_itr = 0;
       
  2491 		new_itr = 4000;
       
  2492 		goto set_itr_now;
       
  2493 	}
       
  2494 
       
  2495 	if (adapter->flags2 & FLAG2_DISABLE_AIM) {
       
  2496 		new_itr = 0;
       
  2497 		goto set_itr_now;
       
  2498 	}
       
  2499 
       
  2500 	adapter->tx_itr = e1000_update_itr(adapter,
       
  2501 				    adapter->tx_itr,
       
  2502 				    adapter->total_tx_packets,
       
  2503 				    adapter->total_tx_bytes);
       
  2504 	/* conservative mode (itr 3) eliminates the lowest_latency setting */
       
  2505 	if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
       
  2506 		adapter->tx_itr = low_latency;
       
  2507 
       
  2508 	adapter->rx_itr = e1000_update_itr(adapter,
       
  2509 				    adapter->rx_itr,
       
  2510 				    adapter->total_rx_packets,
       
  2511 				    adapter->total_rx_bytes);
       
  2512 	/* conservative mode (itr 3) eliminates the lowest_latency setting */
       
  2513 	if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
       
  2514 		adapter->rx_itr = low_latency;
       
  2515 
       
  2516 	current_itr = max(adapter->rx_itr, adapter->tx_itr);
       
  2517 
       
  2518 	switch (current_itr) {
       
  2519 	/* counts and packets in update_itr are dependent on these numbers */
       
  2520 	case lowest_latency:
       
  2521 		new_itr = 70000;
       
  2522 		break;
       
  2523 	case low_latency:
       
  2524 		new_itr = 20000; /* aka hwitr = ~200 */
       
  2525 		break;
       
  2526 	case bulk_latency:
       
  2527 		new_itr = 4000;
       
  2528 		break;
       
  2529 	default:
       
  2530 		break;
       
  2531 	}
       
  2532 
       
  2533 set_itr_now:
       
  2534 	if (new_itr != adapter->itr) {
       
  2535 		/*
       
  2536 		 * this attempts to bias the interrupt rate towards Bulk
       
  2537 		 * by adding intermediate steps when interrupt rate is
       
  2538 		 * increasing
       
  2539 		 */
       
  2540 		new_itr = new_itr > adapter->itr ?
       
  2541 			     min(adapter->itr + (new_itr >> 2), new_itr) :
       
  2542 			     new_itr;
       
  2543 		adapter->itr = new_itr;
       
  2544 		adapter->rx_ring->itr_val = new_itr;
       
  2545 		if (adapter->msix_entries)
       
  2546 			adapter->rx_ring->set_itr = 1;
       
  2547 		else
       
  2548 			if (new_itr)
       
  2549 				ew32(ITR, 1000000000 / (new_itr * 256));
       
  2550 			else
       
  2551 				ew32(ITR, 0);
       
  2552 	}
       
  2553 }
       
  2554 
       
  2555 /**
       
  2556  * e1000_alloc_queues - Allocate memory for all rings
       
  2557  * @adapter: board private structure to initialize
       
  2558  **/
       
  2559 static int __devinit e1000_alloc_queues(struct e1000_adapter *adapter)
       
  2560 {
       
  2561 	int size = sizeof(struct e1000_ring);
       
  2562 
       
  2563 	adapter->tx_ring = kzalloc(size, GFP_KERNEL);
       
  2564 	if (!adapter->tx_ring)
       
  2565 		goto err;
       
  2566 	adapter->tx_ring->count = adapter->tx_ring_count;
       
  2567 	adapter->tx_ring->adapter = adapter;
       
  2568 
       
  2569 	adapter->rx_ring = kzalloc(size, GFP_KERNEL);
       
  2570 	if (!adapter->rx_ring)
       
  2571 		goto err;
       
  2572 	adapter->rx_ring->count = adapter->rx_ring_count;
       
  2573 	adapter->rx_ring->adapter = adapter;
       
  2574 
       
  2575 	return 0;
       
  2576 err:
       
  2577 	e_err("Unable to allocate memory for queues\n");
       
  2578 	kfree(adapter->rx_ring);
       
  2579 	kfree(adapter->tx_ring);
       
  2580 	return -ENOMEM;
       
  2581 }
       
  2582 
       
  2583 /**
       
  2584  * e1000_clean - NAPI Rx polling callback
       
  2585  * @napi: struct associated with this polling callback
       
  2586  * @budget: amount of packets driver is allowed to process this poll
       
  2587  **/
       
  2588 static int e1000_clean(struct napi_struct *napi, int budget)
       
  2589 {
       
  2590 	struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
       
  2591 	struct e1000_hw *hw = &adapter->hw;
       
  2592 	struct net_device *poll_dev = adapter->netdev;
       
  2593 	int tx_cleaned = 1, work_done = 0;
       
  2594 
       
  2595 	adapter = netdev_priv(poll_dev);
       
  2596 
       
  2597 	if (adapter->msix_entries &&
       
  2598 	    !(adapter->rx_ring->ims_val & adapter->tx_ring->ims_val))
       
  2599 		goto clean_rx;
       
  2600 
       
  2601 	tx_cleaned = e1000_clean_tx_irq(adapter->tx_ring);
       
  2602 
       
  2603 clean_rx:
       
  2604 	adapter->clean_rx(adapter->rx_ring, &work_done, budget);
       
  2605 
       
  2606 	if (!tx_cleaned)
       
  2607 		work_done = budget;
       
  2608 
       
  2609 	/* If budget not fully consumed, exit the polling mode */
       
  2610 	if (work_done < budget) {
       
  2611 		if (adapter->itr_setting & 3)
       
  2612 			e1000_set_itr(adapter);
       
  2613 		napi_complete(napi);
       
  2614 		if (!test_bit(__E1000_DOWN, &adapter->state)) {
       
  2615 			if (adapter->msix_entries)
       
  2616 				ew32(IMS, adapter->rx_ring->ims_val);
       
  2617 			else
       
  2618 				e1000_irq_enable(adapter);
       
  2619 		}
       
  2620 	}
       
  2621 
       
  2622 	return work_done;
       
  2623 }
       
  2624 
       
  2625 static int e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
       
  2626 {
       
  2627 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  2628 	struct e1000_hw *hw = &adapter->hw;
       
  2629 	u32 vfta, index;
       
  2630 
       
  2631 	/* don't update vlan cookie if already programmed */
       
  2632 	if ((adapter->hw.mng_cookie.status &
       
  2633 	     E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
       
  2634 	    (vid == adapter->mng_vlan_id))
       
  2635 		return 0;
       
  2636 
       
  2637 	/* add VID to filter table */
       
  2638 	if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
       
  2639 		index = (vid >> 5) & 0x7F;
       
  2640 		vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
       
  2641 		vfta |= (1 << (vid & 0x1F));
       
  2642 		hw->mac.ops.write_vfta(hw, index, vfta);
       
  2643 	}
       
  2644 
       
  2645 	set_bit(vid, adapter->active_vlans);
       
  2646 
       
  2647 	return 0;
       
  2648 }
       
  2649 
       
  2650 static int e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
       
  2651 {
       
  2652 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  2653 	struct e1000_hw *hw = &adapter->hw;
       
  2654 	u32 vfta, index;
       
  2655 
       
  2656 	if ((adapter->hw.mng_cookie.status &
       
  2657 	     E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
       
  2658 	    (vid == adapter->mng_vlan_id)) {
       
  2659 		/* release control to f/w */
       
  2660 		e1000e_release_hw_control(adapter);
       
  2661 		return 0;
       
  2662 	}
       
  2663 
       
  2664 	/* remove VID from filter table */
       
  2665 	if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
       
  2666 		index = (vid >> 5) & 0x7F;
       
  2667 		vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
       
  2668 		vfta &= ~(1 << (vid & 0x1F));
       
  2669 		hw->mac.ops.write_vfta(hw, index, vfta);
       
  2670 	}
       
  2671 
       
  2672 	clear_bit(vid, adapter->active_vlans);
       
  2673 
       
  2674 	return 0;
       
  2675 }
       
  2676 
       
  2677 /**
       
  2678  * e1000e_vlan_filter_disable - helper to disable hw VLAN filtering
       
  2679  * @adapter: board private structure to initialize
       
  2680  **/
       
  2681 static void e1000e_vlan_filter_disable(struct e1000_adapter *adapter)
       
  2682 {
       
  2683 	struct net_device *netdev = adapter->netdev;
       
  2684 	struct e1000_hw *hw = &adapter->hw;
       
  2685 	u32 rctl;
       
  2686 
       
  2687 	if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
       
  2688 		/* disable VLAN receive filtering */
       
  2689 		rctl = er32(RCTL);
       
  2690 		rctl &= ~(E1000_RCTL_VFE | E1000_RCTL_CFIEN);
       
  2691 		ew32(RCTL, rctl);
       
  2692 
       
  2693 		if (adapter->mng_vlan_id != (u16)E1000_MNG_VLAN_NONE) {
       
  2694 			e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
       
  2695 			adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
       
  2696 		}
       
  2697 	}
       
  2698 }
       
  2699 
       
  2700 /**
       
  2701  * e1000e_vlan_filter_enable - helper to enable HW VLAN filtering
       
  2702  * @adapter: board private structure to initialize
       
  2703  **/
       
  2704 static void e1000e_vlan_filter_enable(struct e1000_adapter *adapter)
       
  2705 {
       
  2706 	struct e1000_hw *hw = &adapter->hw;
       
  2707 	u32 rctl;
       
  2708 
       
  2709 	if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
       
  2710 		/* enable VLAN receive filtering */
       
  2711 		rctl = er32(RCTL);
       
  2712 		rctl |= E1000_RCTL_VFE;
       
  2713 		rctl &= ~E1000_RCTL_CFIEN;
       
  2714 		ew32(RCTL, rctl);
       
  2715 	}
       
  2716 }
       
  2717 
       
  2718 /**
       
  2719  * e1000e_vlan_strip_enable - helper to disable HW VLAN stripping
       
  2720  * @adapter: board private structure to initialize
       
  2721  **/
       
  2722 static void e1000e_vlan_strip_disable(struct e1000_adapter *adapter)
       
  2723 {
       
  2724 	struct e1000_hw *hw = &adapter->hw;
       
  2725 	u32 ctrl;
       
  2726 
       
  2727 	/* disable VLAN tag insert/strip */
       
  2728 	ctrl = er32(CTRL);
       
  2729 	ctrl &= ~E1000_CTRL_VME;
       
  2730 	ew32(CTRL, ctrl);
       
  2731 }
       
  2732 
       
  2733 /**
       
  2734  * e1000e_vlan_strip_enable - helper to enable HW VLAN stripping
       
  2735  * @adapter: board private structure to initialize
       
  2736  **/
       
  2737 static void e1000e_vlan_strip_enable(struct e1000_adapter *adapter)
       
  2738 {
       
  2739 	struct e1000_hw *hw = &adapter->hw;
       
  2740 	u32 ctrl;
       
  2741 
       
  2742 	/* enable VLAN tag insert/strip */
       
  2743 	ctrl = er32(CTRL);
       
  2744 	ctrl |= E1000_CTRL_VME;
       
  2745 	ew32(CTRL, ctrl);
       
  2746 }
       
  2747 
       
  2748 static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
       
  2749 {
       
  2750 	struct net_device *netdev = adapter->netdev;
       
  2751 	u16 vid = adapter->hw.mng_cookie.vlan_id;
       
  2752 	u16 old_vid = adapter->mng_vlan_id;
       
  2753 
       
  2754 	if (adapter->hw.mng_cookie.status &
       
  2755 	    E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
       
  2756 		e1000_vlan_rx_add_vid(netdev, vid);
       
  2757 		adapter->mng_vlan_id = vid;
       
  2758 	}
       
  2759 
       
  2760 	if ((old_vid != (u16)E1000_MNG_VLAN_NONE) && (vid != old_vid))
       
  2761 		e1000_vlan_rx_kill_vid(netdev, old_vid);
       
  2762 }
       
  2763 
       
  2764 static void e1000_restore_vlan(struct e1000_adapter *adapter)
       
  2765 {
       
  2766 	u16 vid;
       
  2767 
       
  2768 	e1000_vlan_rx_add_vid(adapter->netdev, 0);
       
  2769 
       
  2770 	for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
       
  2771 		e1000_vlan_rx_add_vid(adapter->netdev, vid);
       
  2772 }
       
  2773 
       
  2774 static void e1000_init_manageability_pt(struct e1000_adapter *adapter)
       
  2775 {
       
  2776 	struct e1000_hw *hw = &adapter->hw;
       
  2777 	u32 manc, manc2h, mdef, i, j;
       
  2778 
       
  2779 	if (!(adapter->flags & FLAG_MNG_PT_ENABLED))
       
  2780 		return;
       
  2781 
       
  2782 	manc = er32(MANC);
       
  2783 
       
  2784 	/*
       
  2785 	 * enable receiving management packets to the host. this will probably
       
  2786 	 * generate destination unreachable messages from the host OS, but
       
  2787 	 * the packets will be handled on SMBUS
       
  2788 	 */
       
  2789 	manc |= E1000_MANC_EN_MNG2HOST;
       
  2790 	manc2h = er32(MANC2H);
       
  2791 
       
  2792 	switch (hw->mac.type) {
       
  2793 	default:
       
  2794 		manc2h |= (E1000_MANC2H_PORT_623 | E1000_MANC2H_PORT_664);
       
  2795 		break;
       
  2796 	case e1000_82574:
       
  2797 	case e1000_82583:
       
  2798 		/*
       
  2799 		 * Check if IPMI pass-through decision filter already exists;
       
  2800 		 * if so, enable it.
       
  2801 		 */
       
  2802 		for (i = 0, j = 0; i < 8; i++) {
       
  2803 			mdef = er32(MDEF(i));
       
  2804 
       
  2805 			/* Ignore filters with anything other than IPMI ports */
       
  2806 			if (mdef & ~(E1000_MDEF_PORT_623 | E1000_MDEF_PORT_664))
       
  2807 				continue;
       
  2808 
       
  2809 			/* Enable this decision filter in MANC2H */
       
  2810 			if (mdef)
       
  2811 				manc2h |= (1 << i);
       
  2812 
       
  2813 			j |= mdef;
       
  2814 		}
       
  2815 
       
  2816 		if (j == (E1000_MDEF_PORT_623 | E1000_MDEF_PORT_664))
       
  2817 			break;
       
  2818 
       
  2819 		/* Create new decision filter in an empty filter */
       
  2820 		for (i = 0, j = 0; i < 8; i++)
       
  2821 			if (er32(MDEF(i)) == 0) {
       
  2822 				ew32(MDEF(i), (E1000_MDEF_PORT_623 |
       
  2823 					       E1000_MDEF_PORT_664));
       
  2824 				manc2h |= (1 << 1);
       
  2825 				j++;
       
  2826 				break;
       
  2827 			}
       
  2828 
       
  2829 		if (!j)
       
  2830 			e_warn("Unable to create IPMI pass-through filter\n");
       
  2831 		break;
       
  2832 	}
       
  2833 
       
  2834 	ew32(MANC2H, manc2h);
       
  2835 	ew32(MANC, manc);
       
  2836 }
       
  2837 
       
  2838 /**
       
  2839  * e1000_configure_tx - Configure Transmit Unit after Reset
       
  2840  * @adapter: board private structure
       
  2841  *
       
  2842  * Configure the Tx unit of the MAC after a reset.
       
  2843  **/
       
  2844 static void e1000_configure_tx(struct e1000_adapter *adapter)
       
  2845 {
       
  2846 	struct e1000_hw *hw = &adapter->hw;
       
  2847 	struct e1000_ring *tx_ring = adapter->tx_ring;
       
  2848 	u64 tdba;
       
  2849 	u32 tdlen, tarc;
       
  2850 
       
  2851 	/* Setup the HW Tx Head and Tail descriptor pointers */
       
  2852 	tdba = tx_ring->dma;
       
  2853 	tdlen = tx_ring->count * sizeof(struct e1000_tx_desc);
       
  2854 	ew32(TDBAL, (tdba & DMA_BIT_MASK(32)));
       
  2855 	ew32(TDBAH, (tdba >> 32));
       
  2856 	ew32(TDLEN, tdlen);
       
  2857 	ew32(TDH, 0);
       
  2858 	ew32(TDT, 0);
       
  2859 	tx_ring->head = adapter->hw.hw_addr + E1000_TDH;
       
  2860 	tx_ring->tail = adapter->hw.hw_addr + E1000_TDT;
       
  2861 
       
  2862 	/* Set the Tx Interrupt Delay register */
       
  2863 	ew32(TIDV, adapter->tx_int_delay);
       
  2864 	/* Tx irq moderation */
       
  2865 	ew32(TADV, adapter->tx_abs_int_delay);
       
  2866 
       
  2867 	if (adapter->flags2 & FLAG2_DMA_BURST) {
       
  2868 		u32 txdctl = er32(TXDCTL(0));
       
  2869 		txdctl &= ~(E1000_TXDCTL_PTHRESH | E1000_TXDCTL_HTHRESH |
       
  2870 			    E1000_TXDCTL_WTHRESH);
       
  2871 		/*
       
  2872 		 * set up some performance related parameters to encourage the
       
  2873 		 * hardware to use the bus more efficiently in bursts, depends
       
  2874 		 * on the tx_int_delay to be enabled,
       
  2875 		 * wthresh = 1 ==> burst write is disabled to avoid Tx stalls
       
  2876 		 * hthresh = 1 ==> prefetch when one or more available
       
  2877 		 * pthresh = 0x1f ==> prefetch if internal cache 31 or less
       
  2878 		 * BEWARE: this seems to work but should be considered first if
       
  2879 		 * there are Tx hangs or other Tx related bugs
       
  2880 		 */
       
  2881 		txdctl |= E1000_TXDCTL_DMA_BURST_ENABLE;
       
  2882 		ew32(TXDCTL(0), txdctl);
       
  2883 	}
       
  2884 	/* erratum work around: set txdctl the same for both queues */
       
  2885 	ew32(TXDCTL(1), er32(TXDCTL(0)));
       
  2886 
       
  2887 	if (adapter->flags & FLAG_TARC_SPEED_MODE_BIT) {
       
  2888 		tarc = er32(TARC(0));
       
  2889 		/*
       
  2890 		 * set the speed mode bit, we'll clear it if we're not at
       
  2891 		 * gigabit link later
       
  2892 		 */
       
  2893 #define SPEED_MODE_BIT (1 << 21)
       
  2894 		tarc |= SPEED_MODE_BIT;
       
  2895 		ew32(TARC(0), tarc);
       
  2896 	}
       
  2897 
       
  2898 	/* errata: program both queues to unweighted RR */
       
  2899 	if (adapter->flags & FLAG_TARC_SET_BIT_ZERO) {
       
  2900 		tarc = er32(TARC(0));
       
  2901 		tarc |= 1;
       
  2902 		ew32(TARC(0), tarc);
       
  2903 		tarc = er32(TARC(1));
       
  2904 		tarc |= 1;
       
  2905 		ew32(TARC(1), tarc);
       
  2906 	}
       
  2907 
       
  2908 	/* Setup Transmit Descriptor Settings for eop descriptor */
       
  2909 	adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
       
  2910 
       
  2911 	/* only set IDE if we are delaying interrupts using the timers */
       
  2912 	if (adapter->tx_int_delay)
       
  2913 		adapter->txd_cmd |= E1000_TXD_CMD_IDE;
       
  2914 
       
  2915 	/* enable Report Status bit */
       
  2916 	adapter->txd_cmd |= E1000_TXD_CMD_RS;
       
  2917 
       
  2918 	hw->mac.ops.config_collision_dist(hw);
       
  2919 }
       
  2920 
       
  2921 /**
       
  2922  * e1000_setup_rctl - configure the receive control registers
       
  2923  * @adapter: Board private structure
       
  2924  **/
       
  2925 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
       
  2926 			   (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
       
  2927 static void e1000_setup_rctl(struct e1000_adapter *adapter)
       
  2928 {
       
  2929 	struct e1000_hw *hw = &adapter->hw;
       
  2930 	u32 rctl, rfctl;
       
  2931 	u32 pages = 0;
       
  2932 
       
  2933 	/* Workaround Si errata on 82579 - configure jumbo frame flow */
       
  2934 	if (hw->mac.type == e1000_pch2lan) {
       
  2935 		s32 ret_val __attribute__ ((unused));
       
  2936 
       
  2937 		if (adapter->netdev->mtu > ETH_DATA_LEN)
       
  2938 			ret_val = e1000_lv_jumbo_workaround_ich8lan(hw, true);
       
  2939 		else
       
  2940 			ret_val = e1000_lv_jumbo_workaround_ich8lan(hw, false);
       
  2941 
       
  2942 		if (ret_val)
       
  2943 			e_dbg("failed to enable jumbo frame workaround mode\n");
       
  2944 	}
       
  2945 
       
  2946 	/* Program MC offset vector base */
       
  2947 	rctl = er32(RCTL);
       
  2948 	rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
       
  2949 	rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
       
  2950 		E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
       
  2951 		(adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
       
  2952 
       
  2953 	/* Do not Store bad packets */
       
  2954 	rctl &= ~E1000_RCTL_SBP;
       
  2955 
       
  2956 	/* Enable Long Packet receive */
       
  2957 	if (adapter->netdev->mtu <= ETH_DATA_LEN)
       
  2958 		rctl &= ~E1000_RCTL_LPE;
       
  2959 	else
       
  2960 		rctl |= E1000_RCTL_LPE;
       
  2961 
       
  2962 	/* Some systems expect that the CRC is included in SMBUS traffic. The
       
  2963 	 * hardware strips the CRC before sending to both SMBUS (BMC) and to
       
  2964 	 * host memory when this is enabled
       
  2965 	 */
       
  2966 	if (adapter->flags2 & FLAG2_CRC_STRIPPING)
       
  2967 		rctl |= E1000_RCTL_SECRC;
       
  2968 
       
  2969 	/* Workaround Si errata on 82577 PHY - configure IPG for jumbos */
       
  2970 	if ((hw->phy.type == e1000_phy_82577) && (rctl & E1000_RCTL_LPE)) {
       
  2971 		u16 phy_data;
       
  2972 
       
  2973 		e1e_rphy(hw, PHY_REG(770, 26), &phy_data);
       
  2974 		phy_data &= 0xfff8;
       
  2975 		phy_data |= (1 << 2);
       
  2976 		e1e_wphy(hw, PHY_REG(770, 26), phy_data);
       
  2977 
       
  2978 		e1e_rphy(hw, 22, &phy_data);
       
  2979 		phy_data &= 0x0fff;
       
  2980 		phy_data |= (1 << 14);
       
  2981 		e1e_wphy(hw, 0x10, 0x2823);
       
  2982 		e1e_wphy(hw, 0x11, 0x0003);
       
  2983 		e1e_wphy(hw, 22, phy_data);
       
  2984 	}
       
  2985 
       
  2986 	/* Setup buffer sizes */
       
  2987 	rctl &= ~E1000_RCTL_SZ_4096;
       
  2988 	rctl |= E1000_RCTL_BSEX;
       
  2989 	switch (adapter->rx_buffer_len) {
       
  2990 	case 2048:
       
  2991 	default:
       
  2992 		rctl |= E1000_RCTL_SZ_2048;
       
  2993 		rctl &= ~E1000_RCTL_BSEX;
       
  2994 		break;
       
  2995 	case 4096:
       
  2996 		rctl |= E1000_RCTL_SZ_4096;
       
  2997 		break;
       
  2998 	case 8192:
       
  2999 		rctl |= E1000_RCTL_SZ_8192;
       
  3000 		break;
       
  3001 	case 16384:
       
  3002 		rctl |= E1000_RCTL_SZ_16384;
       
  3003 		break;
       
  3004 	}
       
  3005 
       
  3006 	/* Enable Extended Status in all Receive Descriptors */
       
  3007 	rfctl = er32(RFCTL);
       
  3008 	rfctl |= E1000_RFCTL_EXTEN;
       
  3009 
       
  3010 	/*
       
  3011 	 * 82571 and greater support packet-split where the protocol
       
  3012 	 * header is placed in skb->data and the packet data is
       
  3013 	 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
       
  3014 	 * In the case of a non-split, skb->data is linearly filled,
       
  3015 	 * followed by the page buffers.  Therefore, skb->data is
       
  3016 	 * sized to hold the largest protocol header.
       
  3017 	 *
       
  3018 	 * allocations using alloc_page take too long for regular MTU
       
  3019 	 * so only enable packet split for jumbo frames
       
  3020 	 *
       
  3021 	 * Using pages when the page size is greater than 16k wastes
       
  3022 	 * a lot of memory, since we allocate 3 pages at all times
       
  3023 	 * per packet.
       
  3024 	 */
       
  3025 	pages = PAGE_USE_COUNT(adapter->netdev->mtu);
       
  3026 	if ((pages <= 3) && (PAGE_SIZE <= 16384) && (rctl & E1000_RCTL_LPE))
       
  3027 		adapter->rx_ps_pages = pages;
       
  3028 	else
       
  3029 		adapter->rx_ps_pages = 0;
       
  3030 
       
  3031 	if (adapter->rx_ps_pages) {
       
  3032 		u32 psrctl = 0;
       
  3033 
       
  3034 		/*
       
  3035 		 * disable packet split support for IPv6 extension headers,
       
  3036 		 * because some malformed IPv6 headers can hang the Rx
       
  3037 		 */
       
  3038 		rfctl |= (E1000_RFCTL_IPV6_EX_DIS |
       
  3039 			  E1000_RFCTL_NEW_IPV6_EXT_DIS);
       
  3040 
       
  3041 		/* Enable Packet split descriptors */
       
  3042 		rctl |= E1000_RCTL_DTYP_PS;
       
  3043 
       
  3044 		psrctl |= adapter->rx_ps_bsize0 >>
       
  3045 			E1000_PSRCTL_BSIZE0_SHIFT;
       
  3046 
       
  3047 		switch (adapter->rx_ps_pages) {
       
  3048 		case 3:
       
  3049 			psrctl |= PAGE_SIZE <<
       
  3050 				E1000_PSRCTL_BSIZE3_SHIFT;
       
  3051 		case 2:
       
  3052 			psrctl |= PAGE_SIZE <<
       
  3053 				E1000_PSRCTL_BSIZE2_SHIFT;
       
  3054 		case 1:
       
  3055 			psrctl |= PAGE_SIZE >>
       
  3056 				E1000_PSRCTL_BSIZE1_SHIFT;
       
  3057 			break;
       
  3058 		}
       
  3059 
       
  3060 		ew32(PSRCTL, psrctl);
       
  3061 	}
       
  3062 
       
  3063 	/* This is useful for sniffing bad packets. */
       
  3064 	if (adapter->netdev->features & NETIF_F_RXALL) {
       
  3065 		/* UPE and MPE will be handled by normal PROMISC logic
       
  3066 		 * in e1000e_set_rx_mode */
       
  3067 		rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
       
  3068 			 E1000_RCTL_BAM | /* RX All Bcast Pkts */
       
  3069 			 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
       
  3070 
       
  3071 		rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
       
  3072 			  E1000_RCTL_DPF | /* Allow filtered pause */
       
  3073 			  E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
       
  3074 		/* Do not mess with E1000_CTRL_VME, it affects transmit as well,
       
  3075 		 * and that breaks VLANs.
       
  3076 		 */
       
  3077 	}
       
  3078 
       
  3079 	ew32(RFCTL, rfctl);
       
  3080 	ew32(RCTL, rctl);
       
  3081 	/* just started the receive unit, no need to restart */
       
  3082 	adapter->flags &= ~FLAG_RX_RESTART_NOW;
       
  3083 }
       
  3084 
       
  3085 /**
       
  3086  * e1000_configure_rx - Configure Receive Unit after Reset
       
  3087  * @adapter: board private structure
       
  3088  *
       
  3089  * Configure the Rx unit of the MAC after a reset.
       
  3090  **/
       
  3091 static void e1000_configure_rx(struct e1000_adapter *adapter)
       
  3092 {
       
  3093 	struct e1000_hw *hw = &adapter->hw;
       
  3094 	struct e1000_ring *rx_ring = adapter->rx_ring;
       
  3095 	u64 rdba;
       
  3096 	u32 rdlen, rctl, rxcsum, ctrl_ext;
       
  3097 
       
  3098 	if (adapter->rx_ps_pages) {
       
  3099 		/* this is a 32 byte descriptor */
       
  3100 		rdlen = rx_ring->count *
       
  3101 		    sizeof(union e1000_rx_desc_packet_split);
       
  3102 		adapter->clean_rx = e1000_clean_rx_irq_ps;
       
  3103 		adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
       
  3104 	} else if (adapter->netdev->mtu > ETH_FRAME_LEN + ETH_FCS_LEN) {
       
  3105 		rdlen = rx_ring->count * sizeof(union e1000_rx_desc_extended);
       
  3106 		adapter->clean_rx = e1000_clean_jumbo_rx_irq;
       
  3107 		adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
       
  3108 	} else {
       
  3109 		rdlen = rx_ring->count * sizeof(union e1000_rx_desc_extended);
       
  3110 		adapter->clean_rx = e1000_clean_rx_irq;
       
  3111 		adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
       
  3112 	}
       
  3113 
       
  3114 	/* disable receives while setting up the descriptors */
       
  3115 	rctl = er32(RCTL);
       
  3116 	if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
       
  3117 		ew32(RCTL, rctl & ~E1000_RCTL_EN);
       
  3118 	e1e_flush();
       
  3119 	usleep_range(10000, 20000);
       
  3120 
       
  3121 	if (adapter->flags2 & FLAG2_DMA_BURST) {
       
  3122 		/*
       
  3123 		 * set the writeback threshold (only takes effect if the RDTR
       
  3124 		 * is set). set GRAN=1 and write back up to 0x4 worth, and
       
  3125 		 * enable prefetching of 0x20 Rx descriptors
       
  3126 		 * granularity = 01
       
  3127 		 * wthresh = 04,
       
  3128 		 * hthresh = 04,
       
  3129 		 * pthresh = 0x20
       
  3130 		 */
       
  3131 		ew32(RXDCTL(0), E1000_RXDCTL_DMA_BURST_ENABLE);
       
  3132 		ew32(RXDCTL(1), E1000_RXDCTL_DMA_BURST_ENABLE);
       
  3133 
       
  3134 		/*
       
  3135 		 * override the delay timers for enabling bursting, only if
       
  3136 		 * the value was not set by the user via module options
       
  3137 		 */
       
  3138 		if (adapter->rx_int_delay == DEFAULT_RDTR)
       
  3139 			adapter->rx_int_delay = BURST_RDTR;
       
  3140 		if (adapter->rx_abs_int_delay == DEFAULT_RADV)
       
  3141 			adapter->rx_abs_int_delay = BURST_RADV;
       
  3142 	}
       
  3143 
       
  3144 	/* set the Receive Delay Timer Register */
       
  3145 	ew32(RDTR, adapter->rx_int_delay);
       
  3146 
       
  3147 	/* irq moderation */
       
  3148 	ew32(RADV, adapter->rx_abs_int_delay);
       
  3149 	if ((adapter->itr_setting != 0) && (adapter->itr != 0))
       
  3150 		ew32(ITR, 1000000000 / (adapter->itr * 256));
       
  3151 
       
  3152 	ctrl_ext = er32(CTRL_EXT);
       
  3153 	/* Auto-Mask interrupts upon ICR access */
       
  3154 	ctrl_ext |= E1000_CTRL_EXT_IAME;
       
  3155 	ew32(IAM, 0xffffffff);
       
  3156 	ew32(CTRL_EXT, ctrl_ext);
       
  3157 	e1e_flush();
       
  3158 
       
  3159 	/*
       
  3160 	 * Setup the HW Rx Head and Tail Descriptor Pointers and
       
  3161 	 * the Base and Length of the Rx Descriptor Ring
       
  3162 	 */
       
  3163 	rdba = rx_ring->dma;
       
  3164 	ew32(RDBAL, (rdba & DMA_BIT_MASK(32)));
       
  3165 	ew32(RDBAH, (rdba >> 32));
       
  3166 	ew32(RDLEN, rdlen);
       
  3167 	ew32(RDH, 0);
       
  3168 	ew32(RDT, 0);
       
  3169 	rx_ring->head = adapter->hw.hw_addr + E1000_RDH;
       
  3170 	rx_ring->tail = adapter->hw.hw_addr + E1000_RDT;
       
  3171 
       
  3172 	/* Enable Receive Checksum Offload for TCP and UDP */
       
  3173 	rxcsum = er32(RXCSUM);
       
  3174 	if (adapter->netdev->features & NETIF_F_RXCSUM)
       
  3175 		rxcsum |= E1000_RXCSUM_TUOFL;
       
  3176 	else
       
  3177 		rxcsum &= ~E1000_RXCSUM_TUOFL;
       
  3178 	ew32(RXCSUM, rxcsum);
       
  3179 
       
  3180 	if (adapter->hw.mac.type == e1000_pch2lan) {
       
  3181 		/*
       
  3182 		 * With jumbo frames, excessive C-state transition
       
  3183 		 * latencies result in dropped transactions.
       
  3184 		 */
       
  3185 		if (adapter->netdev->mtu > ETH_DATA_LEN) {
       
  3186 			u32 rxdctl = er32(RXDCTL(0));
       
  3187 			ew32(RXDCTL(0), rxdctl | 0x3);
       
  3188 			pm_qos_update_request(&adapter->netdev->pm_qos_req, 55);
       
  3189 		} else {
       
  3190 			pm_qos_update_request(&adapter->netdev->pm_qos_req,
       
  3191 					      PM_QOS_DEFAULT_VALUE);
       
  3192 		}
       
  3193 	}
       
  3194 
       
  3195 	/* Enable Receives */
       
  3196 	ew32(RCTL, rctl);
       
  3197 }
       
  3198 
       
  3199 /**
       
  3200  * e1000e_write_mc_addr_list - write multicast addresses to MTA
       
  3201  * @netdev: network interface device structure
       
  3202  *
       
  3203  * Writes multicast address list to the MTA hash table.
       
  3204  * Returns: -ENOMEM on failure
       
  3205  *                0 on no addresses written
       
  3206  *                X on writing X addresses to MTA
       
  3207  */
       
  3208 static int e1000e_write_mc_addr_list(struct net_device *netdev)
       
  3209 {
       
  3210 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3211 	struct e1000_hw *hw = &adapter->hw;
       
  3212 	struct netdev_hw_addr *ha;
       
  3213 	u8 *mta_list;
       
  3214 	int i;
       
  3215 
       
  3216 	if (netdev_mc_empty(netdev)) {
       
  3217 		/* nothing to program, so clear mc list */
       
  3218 		hw->mac.ops.update_mc_addr_list(hw, NULL, 0);
       
  3219 		return 0;
       
  3220 	}
       
  3221 
       
  3222 	mta_list = kzalloc(netdev_mc_count(netdev) * ETH_ALEN, GFP_ATOMIC);
       
  3223 	if (!mta_list)
       
  3224 		return -ENOMEM;
       
  3225 
       
  3226 	/* update_mc_addr_list expects a packed array of only addresses. */
       
  3227 	i = 0;
       
  3228 	netdev_for_each_mc_addr(ha, netdev)
       
  3229 		memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
       
  3230 
       
  3231 	hw->mac.ops.update_mc_addr_list(hw, mta_list, i);
       
  3232 	kfree(mta_list);
       
  3233 
       
  3234 	return netdev_mc_count(netdev);
       
  3235 }
       
  3236 
       
  3237 /**
       
  3238  * e1000e_write_uc_addr_list - write unicast addresses to RAR table
       
  3239  * @netdev: network interface device structure
       
  3240  *
       
  3241  * Writes unicast address list to the RAR table.
       
  3242  * Returns: -ENOMEM on failure/insufficient address space
       
  3243  *                0 on no addresses written
       
  3244  *                X on writing X addresses to the RAR table
       
  3245  **/
       
  3246 static int e1000e_write_uc_addr_list(struct net_device *netdev)
       
  3247 {
       
  3248 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3249 	struct e1000_hw *hw = &adapter->hw;
       
  3250 	unsigned int rar_entries = hw->mac.rar_entry_count;
       
  3251 	int count = 0;
       
  3252 
       
  3253 	/* save a rar entry for our hardware address */
       
  3254 	rar_entries--;
       
  3255 
       
  3256 	/* save a rar entry for the LAA workaround */
       
  3257 	if (adapter->flags & FLAG_RESET_OVERWRITES_LAA)
       
  3258 		rar_entries--;
       
  3259 
       
  3260 	/* return ENOMEM indicating insufficient memory for addresses */
       
  3261 	if (netdev_uc_count(netdev) > rar_entries)
       
  3262 		return -ENOMEM;
       
  3263 
       
  3264 	if (!netdev_uc_empty(netdev) && rar_entries) {
       
  3265 		struct netdev_hw_addr *ha;
       
  3266 
       
  3267 		/*
       
  3268 		 * write the addresses in reverse order to avoid write
       
  3269 		 * combining
       
  3270 		 */
       
  3271 		netdev_for_each_uc_addr(ha, netdev) {
       
  3272 			if (!rar_entries)
       
  3273 				break;
       
  3274 			e1000e_rar_set(hw, ha->addr, rar_entries--);
       
  3275 			count++;
       
  3276 		}
       
  3277 	}
       
  3278 
       
  3279 	/* zero out the remaining RAR entries not used above */
       
  3280 	for (; rar_entries > 0; rar_entries--) {
       
  3281 		ew32(RAH(rar_entries), 0);
       
  3282 		ew32(RAL(rar_entries), 0);
       
  3283 	}
       
  3284 	e1e_flush();
       
  3285 
       
  3286 	return count;
       
  3287 }
       
  3288 
       
  3289 /**
       
  3290  * e1000e_set_rx_mode - secondary unicast, Multicast and Promiscuous mode set
       
  3291  * @netdev: network interface device structure
       
  3292  *
       
  3293  * The ndo_set_rx_mode entry point is called whenever the unicast or multicast
       
  3294  * address list or the network interface flags are updated.  This routine is
       
  3295  * responsible for configuring the hardware for proper unicast, multicast,
       
  3296  * promiscuous mode, and all-multi behavior.
       
  3297  **/
       
  3298 static void e1000e_set_rx_mode(struct net_device *netdev)
       
  3299 {
       
  3300 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3301 	struct e1000_hw *hw = &adapter->hw;
       
  3302 	u32 rctl;
       
  3303 
       
  3304 	/* Check for Promiscuous and All Multicast modes */
       
  3305 	rctl = er32(RCTL);
       
  3306 
       
  3307 	/* clear the affected bits */
       
  3308 	rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
       
  3309 
       
  3310 	if (netdev->flags & IFF_PROMISC) {
       
  3311 		rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
       
  3312 		/* Do not hardware filter VLANs in promisc mode */
       
  3313 		e1000e_vlan_filter_disable(adapter);
       
  3314 	} else {
       
  3315 		int count;
       
  3316 
       
  3317 		if (netdev->flags & IFF_ALLMULTI) {
       
  3318 			rctl |= E1000_RCTL_MPE;
       
  3319 		} else {
       
  3320 			/*
       
  3321 			 * Write addresses to the MTA, if the attempt fails
       
  3322 			 * then we should just turn on promiscuous mode so
       
  3323 			 * that we can at least receive multicast traffic
       
  3324 			 */
       
  3325 			count = e1000e_write_mc_addr_list(netdev);
       
  3326 			if (count < 0)
       
  3327 				rctl |= E1000_RCTL_MPE;
       
  3328 		}
       
  3329 		e1000e_vlan_filter_enable(adapter);
       
  3330 		/*
       
  3331 		 * Write addresses to available RAR registers, if there is not
       
  3332 		 * sufficient space to store all the addresses then enable
       
  3333 		 * unicast promiscuous mode
       
  3334 		 */
       
  3335 		count = e1000e_write_uc_addr_list(netdev);
       
  3336 		if (count < 0)
       
  3337 			rctl |= E1000_RCTL_UPE;
       
  3338 	}
       
  3339 
       
  3340 	ew32(RCTL, rctl);
       
  3341 
       
  3342 	if (netdev->features & NETIF_F_HW_VLAN_RX)
       
  3343 		e1000e_vlan_strip_enable(adapter);
       
  3344 	else
       
  3345 		e1000e_vlan_strip_disable(adapter);
       
  3346 }
       
  3347 
       
  3348 static void e1000e_setup_rss_hash(struct e1000_adapter *adapter)
       
  3349 {
       
  3350 	struct e1000_hw *hw = &adapter->hw;
       
  3351 	u32 mrqc, rxcsum;
       
  3352 	int i;
       
  3353 	static const u32 rsskey[10] = {
       
  3354 		0xda565a6d, 0xc20e5b25, 0x3d256741, 0xb08fa343, 0xcb2bcad0,
       
  3355 		0xb4307bae, 0xa32dcb77, 0x0cf23080, 0x3bb7426a, 0xfa01acbe
       
  3356 	};
       
  3357 
       
  3358 	/* Fill out hash function seed */
       
  3359 	for (i = 0; i < 10; i++)
       
  3360 		ew32(RSSRK(i), rsskey[i]);
       
  3361 
       
  3362 	/* Direct all traffic to queue 0 */
       
  3363 	for (i = 0; i < 32; i++)
       
  3364 		ew32(RETA(i), 0);
       
  3365 
       
  3366 	/*
       
  3367 	 * Disable raw packet checksumming so that RSS hash is placed in
       
  3368 	 * descriptor on writeback.
       
  3369 	 */
       
  3370 	rxcsum = er32(RXCSUM);
       
  3371 	rxcsum |= E1000_RXCSUM_PCSD;
       
  3372 
       
  3373 	ew32(RXCSUM, rxcsum);
       
  3374 
       
  3375 	mrqc = (E1000_MRQC_RSS_FIELD_IPV4 |
       
  3376 		E1000_MRQC_RSS_FIELD_IPV4_TCP |
       
  3377 		E1000_MRQC_RSS_FIELD_IPV6 |
       
  3378 		E1000_MRQC_RSS_FIELD_IPV6_TCP |
       
  3379 		E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
       
  3380 
       
  3381 	ew32(MRQC, mrqc);
       
  3382 }
       
  3383 
       
  3384 /**
       
  3385  * e1000_configure - configure the hardware for Rx and Tx
       
  3386  * @adapter: private board structure
       
  3387  **/
       
  3388 static void e1000_configure(struct e1000_adapter *adapter)
       
  3389 {
       
  3390 	struct e1000_ring *rx_ring = adapter->rx_ring;
       
  3391 
       
  3392 	e1000e_set_rx_mode(adapter->netdev);
       
  3393 
       
  3394 	e1000_restore_vlan(adapter);
       
  3395 	e1000_init_manageability_pt(adapter);
       
  3396 
       
  3397 	e1000_configure_tx(adapter);
       
  3398 
       
  3399 	if (adapter->netdev->features & NETIF_F_RXHASH)
       
  3400 		e1000e_setup_rss_hash(adapter);
       
  3401 	e1000_setup_rctl(adapter);
       
  3402 	e1000_configure_rx(adapter);
       
  3403 	if (adapter->ecdev) {
       
  3404 		adapter->alloc_rx_buf(rx_ring, adapter->rx_ring->count, GFP_KERNEL);
       
  3405 	} else {
       
  3406 		adapter->alloc_rx_buf(rx_ring, e1000_desc_unused(rx_ring),
       
  3407 				GFP_KERNEL);
       
  3408 	}
       
  3409 }
       
  3410 
       
  3411 /**
       
  3412  * e1000e_power_up_phy - restore link in case the phy was powered down
       
  3413  * @adapter: address of board private structure
       
  3414  *
       
  3415  * The phy may be powered down to save power and turn off link when the
       
  3416  * driver is unloaded and wake on lan is not enabled (among others)
       
  3417  * *** this routine MUST be followed by a call to e1000e_reset ***
       
  3418  **/
       
  3419 void e1000e_power_up_phy(struct e1000_adapter *adapter)
       
  3420 {
       
  3421 	if (adapter->hw.phy.ops.power_up)
       
  3422 		adapter->hw.phy.ops.power_up(&adapter->hw);
       
  3423 
       
  3424 	adapter->hw.mac.ops.setup_link(&adapter->hw);
       
  3425 }
       
  3426 
       
  3427 /**
       
  3428  * e1000_power_down_phy - Power down the PHY
       
  3429  *
       
  3430  * Power down the PHY so no link is implied when interface is down.
       
  3431  * The PHY cannot be powered down if management or WoL is active.
       
  3432  */
       
  3433 static void e1000_power_down_phy(struct e1000_adapter *adapter)
       
  3434 {
       
  3435 	/* WoL is enabled */
       
  3436 	if (adapter->wol)
       
  3437 		return;
       
  3438 
       
  3439 	if (adapter->hw.phy.ops.power_down)
       
  3440 		adapter->hw.phy.ops.power_down(&adapter->hw);
       
  3441 }
       
  3442 
       
  3443 /**
       
  3444  * e1000e_reset - bring the hardware into a known good state
       
  3445  *
       
  3446  * This function boots the hardware and enables some settings that
       
  3447  * require a configuration cycle of the hardware - those cannot be
       
  3448  * set/changed during runtime. After reset the device needs to be
       
  3449  * properly configured for Rx, Tx etc.
       
  3450  */
       
  3451 void e1000e_reset(struct e1000_adapter *adapter)
       
  3452 {
       
  3453 	struct e1000_mac_info *mac = &adapter->hw.mac;
       
  3454 	struct e1000_fc_info *fc = &adapter->hw.fc;
       
  3455 	struct e1000_hw *hw = &adapter->hw;
       
  3456 	u32 tx_space, min_tx_space, min_rx_space;
       
  3457 	u32 pba = adapter->pba;
       
  3458 	u16 hwm;
       
  3459 
       
  3460 	/* reset Packet Buffer Allocation to default */
       
  3461 	ew32(PBA, pba);
       
  3462 
       
  3463 	if (adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) {
       
  3464 		/*
       
  3465 		 * To maintain wire speed transmits, the Tx FIFO should be
       
  3466 		 * large enough to accommodate two full transmit packets,
       
  3467 		 * rounded up to the next 1KB and expressed in KB.  Likewise,
       
  3468 		 * the Rx FIFO should be large enough to accommodate at least
       
  3469 		 * one full receive packet and is similarly rounded up and
       
  3470 		 * expressed in KB.
       
  3471 		 */
       
  3472 		pba = er32(PBA);
       
  3473 		/* upper 16 bits has Tx packet buffer allocation size in KB */
       
  3474 		tx_space = pba >> 16;
       
  3475 		/* lower 16 bits has Rx packet buffer allocation size in KB */
       
  3476 		pba &= 0xffff;
       
  3477 		/*
       
  3478 		 * the Tx fifo also stores 16 bytes of information about the Tx
       
  3479 		 * but don't include ethernet FCS because hardware appends it
       
  3480 		 */
       
  3481 		min_tx_space = (adapter->max_frame_size +
       
  3482 				sizeof(struct e1000_tx_desc) -
       
  3483 				ETH_FCS_LEN) * 2;
       
  3484 		min_tx_space = ALIGN(min_tx_space, 1024);
       
  3485 		min_tx_space >>= 10;
       
  3486 		/* software strips receive CRC, so leave room for it */
       
  3487 		min_rx_space = adapter->max_frame_size;
       
  3488 		min_rx_space = ALIGN(min_rx_space, 1024);
       
  3489 		min_rx_space >>= 10;
       
  3490 
       
  3491 		/*
       
  3492 		 * If current Tx allocation is less than the min Tx FIFO size,
       
  3493 		 * and the min Tx FIFO size is less than the current Rx FIFO
       
  3494 		 * allocation, take space away from current Rx allocation
       
  3495 		 */
       
  3496 		if ((tx_space < min_tx_space) &&
       
  3497 		    ((min_tx_space - tx_space) < pba)) {
       
  3498 			pba -= min_tx_space - tx_space;
       
  3499 
       
  3500 			/*
       
  3501 			 * if short on Rx space, Rx wins and must trump Tx
       
  3502 			 * adjustment or use Early Receive if available
       
  3503 			 */
       
  3504 			if (pba < min_rx_space)
       
  3505 				pba = min_rx_space;
       
  3506 		}
       
  3507 
       
  3508 		ew32(PBA, pba);
       
  3509 	}
       
  3510 
       
  3511 	/*
       
  3512 	 * flow control settings
       
  3513 	 *
       
  3514 	 * The high water mark must be low enough to fit one full frame
       
  3515 	 * (or the size used for early receive) above it in the Rx FIFO.
       
  3516 	 * Set it to the lower of:
       
  3517 	 * - 90% of the Rx FIFO size, and
       
  3518 	 * - the full Rx FIFO size minus one full frame
       
  3519 	 */
       
  3520 	if (adapter->flags & FLAG_DISABLE_FC_PAUSE_TIME)
       
  3521 		fc->pause_time = 0xFFFF;
       
  3522 	else
       
  3523 		fc->pause_time = E1000_FC_PAUSE_TIME;
       
  3524 	fc->send_xon = true;
       
  3525 	fc->current_mode = fc->requested_mode;
       
  3526 
       
  3527 	switch (hw->mac.type) {
       
  3528 	case e1000_ich9lan:
       
  3529 	case e1000_ich10lan:
       
  3530 		if (adapter->netdev->mtu > ETH_DATA_LEN) {
       
  3531 			pba = 14;
       
  3532 			ew32(PBA, pba);
       
  3533 			fc->high_water = 0x2800;
       
  3534 			fc->low_water = fc->high_water - 8;
       
  3535 			break;
       
  3536 		}
       
  3537 		/* fall-through */
       
  3538 	default:
       
  3539 		hwm = min(((pba << 10) * 9 / 10),
       
  3540 			  ((pba << 10) - adapter->max_frame_size));
       
  3541 
       
  3542 		fc->high_water = hwm & E1000_FCRTH_RTH; /* 8-byte granularity */
       
  3543 		fc->low_water = fc->high_water - 8;
       
  3544 		break;
       
  3545 	case e1000_pchlan:
       
  3546 		/*
       
  3547 		 * Workaround PCH LOM adapter hangs with certain network
       
  3548 		 * loads.  If hangs persist, try disabling Tx flow control.
       
  3549 		 */
       
  3550 		if (adapter->netdev->mtu > ETH_DATA_LEN) {
       
  3551 			fc->high_water = 0x3500;
       
  3552 			fc->low_water  = 0x1500;
       
  3553 		} else {
       
  3554 			fc->high_water = 0x5000;
       
  3555 			fc->low_water  = 0x3000;
       
  3556 		}
       
  3557 		fc->refresh_time = 0x1000;
       
  3558 		break;
       
  3559 	case e1000_pch2lan:
       
  3560 		fc->high_water = 0x05C20;
       
  3561 		fc->low_water = 0x05048;
       
  3562 		fc->pause_time = 0x0650;
       
  3563 		fc->refresh_time = 0x0400;
       
  3564 		if (adapter->netdev->mtu > ETH_DATA_LEN) {
       
  3565 			pba = 14;
       
  3566 			ew32(PBA, pba);
       
  3567 		}
       
  3568 		break;
       
  3569 	}
       
  3570 
       
  3571 	/*
       
  3572 	 * Disable Adaptive Interrupt Moderation if 2 full packets cannot
       
  3573 	 * fit in receive buffer.
       
  3574 	 */
       
  3575 	if (adapter->itr_setting & 0x3) {
       
  3576 		if ((adapter->max_frame_size * 2) > (pba << 10)) {
       
  3577 			if (!(adapter->flags2 & FLAG2_DISABLE_AIM)) {
       
  3578 				dev_info(&adapter->pdev->dev,
       
  3579 					"Interrupt Throttle Rate turned off\n");
       
  3580 				adapter->flags2 |= FLAG2_DISABLE_AIM;
       
  3581 				ew32(ITR, 0);
       
  3582 			}
       
  3583 		} else if (adapter->flags2 & FLAG2_DISABLE_AIM) {
       
  3584 			dev_info(&adapter->pdev->dev,
       
  3585 				 "Interrupt Throttle Rate turned on\n");
       
  3586 			adapter->flags2 &= ~FLAG2_DISABLE_AIM;
       
  3587 			adapter->itr = 20000;
       
  3588 			ew32(ITR, 1000000000 / (adapter->itr * 256));
       
  3589 		}
       
  3590 	}
       
  3591 
       
  3592 	/* Allow time for pending master requests to run */
       
  3593 	mac->ops.reset_hw(hw);
       
  3594 
       
  3595 	/*
       
  3596 	 * For parts with AMT enabled, let the firmware know
       
  3597 	 * that the network interface is in control
       
  3598 	 */
       
  3599 	if (adapter->flags & FLAG_HAS_AMT)
       
  3600 		e1000e_get_hw_control(adapter);
       
  3601 
       
  3602 	ew32(WUC, 0);
       
  3603 
       
  3604 	if (mac->ops.init_hw(hw))
       
  3605 		e_err("Hardware Error\n");
       
  3606 
       
  3607 	e1000_update_mng_vlan(adapter);
       
  3608 
       
  3609 	/* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
       
  3610 	ew32(VET, ETH_P_8021Q);
       
  3611 
       
  3612 	e1000e_reset_adaptive(hw);
       
  3613 
       
  3614 	if (!netif_running(adapter->netdev) &&
       
  3615 	    !test_bit(__E1000_TESTING, &adapter->state)) {
       
  3616 		e1000_power_down_phy(adapter);
       
  3617 		return;
       
  3618 	}
       
  3619 
       
  3620 	e1000_get_phy_info(hw);
       
  3621 
       
  3622 	if ((adapter->flags & FLAG_HAS_SMART_POWER_DOWN) &&
       
  3623 	    !(adapter->flags & FLAG_SMART_POWER_DOWN)) {
       
  3624 		u16 phy_data = 0;
       
  3625 		/*
       
  3626 		 * speed up time to link by disabling smart power down, ignore
       
  3627 		 * the return value of this function because there is nothing
       
  3628 		 * different we would do if it failed
       
  3629 		 */
       
  3630 		e1e_rphy(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
       
  3631 		phy_data &= ~IGP02E1000_PM_SPD;
       
  3632 		e1e_wphy(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
       
  3633 	}
       
  3634 }
       
  3635 
       
  3636 int e1000e_up(struct e1000_adapter *adapter)
       
  3637 {
       
  3638 	struct e1000_hw *hw = &adapter->hw;
       
  3639 
       
  3640 	/* hardware has been reset, we need to reload some things */
       
  3641 	e1000_configure(adapter);
       
  3642 
       
  3643 	clear_bit(__E1000_DOWN, &adapter->state);
       
  3644 
       
  3645 	if (adapter->msix_entries)
       
  3646 		e1000_configure_msix(adapter);
       
  3647 
       
  3648 	if (!adapter->ecdev) {
       
  3649 		e1000_irq_enable(adapter);
       
  3650 
       
  3651 		netif_start_queue(adapter->netdev);
       
  3652 
       
  3653 		/* fire a link change interrupt to start the watchdog */
       
  3654 		if (adapter->msix_entries)
       
  3655 			ew32(ICS, E1000_ICS_LSC | E1000_ICR_OTHER);
       
  3656 		else
       
  3657 			ew32(ICS, E1000_ICS_LSC);
       
  3658 	}
       
  3659 
       
  3660 	return 0;
       
  3661 }
       
  3662 
       
  3663 static void e1000e_flush_descriptors(struct e1000_adapter *adapter)
       
  3664 {
       
  3665 	struct e1000_hw *hw = &adapter->hw;
       
  3666 
       
  3667 	if (!(adapter->flags2 & FLAG2_DMA_BURST))
       
  3668 		return;
       
  3669 
       
  3670 	/* flush pending descriptor writebacks to memory */
       
  3671 	ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
       
  3672 	ew32(RDTR, adapter->rx_int_delay | E1000_RDTR_FPD);
       
  3673 
       
  3674 	/* execute the writes immediately */
       
  3675 	e1e_flush();
       
  3676 
       
  3677 	/*
       
  3678 	 * due to rare timing issues, write to TIDV/RDTR again to ensure the
       
  3679 	 * write is successful
       
  3680 	 */
       
  3681 	ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
       
  3682 	ew32(RDTR, adapter->rx_int_delay | E1000_RDTR_FPD);
       
  3683 
       
  3684 	/* execute the writes immediately */
       
  3685 	e1e_flush();
       
  3686 }
       
  3687 
       
  3688 static void e1000e_update_stats(struct e1000_adapter *adapter);
       
  3689 
       
  3690 void e1000e_down(struct e1000_adapter *adapter)
       
  3691 {
       
  3692 	struct net_device *netdev = adapter->netdev;
       
  3693 	struct e1000_hw *hw = &adapter->hw;
       
  3694 	u32 tctl, rctl;
       
  3695 
       
  3696 	/*
       
  3697 	 * signal that we're down so the interrupt handler does not
       
  3698 	 * reschedule our watchdog timer
       
  3699 	 */
       
  3700 	set_bit(__E1000_DOWN, &adapter->state);
       
  3701 
       
  3702 	/* disable receives in the hardware */
       
  3703 	rctl = er32(RCTL);
       
  3704 	if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
       
  3705 		ew32(RCTL, rctl & ~E1000_RCTL_EN);
       
  3706 	/* flush and sleep below */
       
  3707 
       
  3708 	if (!adapter->ecdev)
       
  3709 		netif_stop_queue(netdev);
       
  3710 
       
  3711 	/* disable transmits in the hardware */
       
  3712 	tctl = er32(TCTL);
       
  3713 	tctl &= ~E1000_TCTL_EN;
       
  3714 	ew32(TCTL, tctl);
       
  3715 
       
  3716 	/* flush both disables and wait for them to finish */
       
  3717 	e1e_flush();
       
  3718 	usleep_range(10000, 20000);
       
  3719 
       
  3720 	if (!adapter->ecdev) {
       
  3721 		e1000_irq_disable(adapter);
       
  3722 
       
  3723 		del_timer_sync(&adapter->watchdog_timer);
       
  3724 		del_timer_sync(&adapter->phy_info_timer);
       
  3725 	}
       
  3726 
       
  3727 	if (adapter->ecdev) {
       
  3728 		ecdev_set_link(adapter->ecdev, 0);
       
  3729 	}
       
  3730 	else {
       
  3731 		netif_carrier_off(netdev);
       
  3732 	}
       
  3733 
       
  3734 	spin_lock(&adapter->stats64_lock);
       
  3735 	e1000e_update_stats(adapter);
       
  3736 	spin_unlock(&adapter->stats64_lock);
       
  3737 
       
  3738 	e1000e_flush_descriptors(adapter);
       
  3739 	e1000_clean_tx_ring(adapter->tx_ring);
       
  3740 	e1000_clean_rx_ring(adapter->rx_ring);
       
  3741 
       
  3742 	adapter->link_speed = 0;
       
  3743 	adapter->link_duplex = 0;
       
  3744 
       
  3745 	if (!pci_channel_offline(adapter->pdev))
       
  3746 		e1000e_reset(adapter);
       
  3747 
       
  3748 	/*
       
  3749 	 * TODO: for power management, we could drop the link and
       
  3750 	 * pci_disable_device here.
       
  3751 	 */
       
  3752 }
       
  3753 
       
  3754 void e1000e_reinit_locked(struct e1000_adapter *adapter)
       
  3755 {
       
  3756 	might_sleep();
       
  3757 	while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
       
  3758 		usleep_range(1000, 2000);
       
  3759 	e1000e_down(adapter);
       
  3760 	e1000e_up(adapter);
       
  3761 	clear_bit(__E1000_RESETTING, &adapter->state);
       
  3762 }
       
  3763 
       
  3764 /**
       
  3765  * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
       
  3766  * @adapter: board private structure to initialize
       
  3767  *
       
  3768  * e1000_sw_init initializes the Adapter private data structure.
       
  3769  * Fields are initialized based on PCI device information and
       
  3770  * OS network device settings (MTU size).
       
  3771  **/
       
  3772 static int __devinit e1000_sw_init(struct e1000_adapter *adapter)
       
  3773 {
       
  3774 	struct net_device *netdev = adapter->netdev;
       
  3775 
       
  3776 	adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
       
  3777 	adapter->rx_ps_bsize0 = 128;
       
  3778 	adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
       
  3779 	adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
       
  3780 	adapter->tx_ring_count = E1000_DEFAULT_TXD;
       
  3781 	adapter->rx_ring_count = E1000_DEFAULT_RXD;
       
  3782 
       
  3783 	spin_lock_init(&adapter->stats64_lock);
       
  3784 
       
  3785 	e1000e_set_interrupt_capability(adapter);
       
  3786 
       
  3787 	if (e1000_alloc_queues(adapter))
       
  3788 		return -ENOMEM;
       
  3789 
       
  3790 	/* Explicitly disable IRQ since the NIC can be in any state. */
       
  3791 	e1000_irq_disable(adapter);
       
  3792 
       
  3793 	set_bit(__E1000_DOWN, &adapter->state);
       
  3794 	return 0;
       
  3795 }
       
  3796 
       
  3797 /**
       
  3798  * e1000_intr_msi_test - Interrupt Handler
       
  3799  * @irq: interrupt number
       
  3800  * @data: pointer to a network interface device structure
       
  3801  **/
       
  3802 static irqreturn_t e1000_intr_msi_test(int irq, void *data)
       
  3803 {
       
  3804 	struct net_device *netdev = data;
       
  3805 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3806 	struct e1000_hw *hw = &adapter->hw;
       
  3807 	u32 icr = er32(ICR);
       
  3808 
       
  3809 	e_dbg("icr is %08X\n", icr);
       
  3810 	if (icr & E1000_ICR_RXSEQ) {
       
  3811 		adapter->flags &= ~FLAG_MSI_TEST_FAILED;
       
  3812 		wmb();
       
  3813 	}
       
  3814 
       
  3815 	return IRQ_HANDLED;
       
  3816 }
       
  3817 
       
  3818 /**
       
  3819  * e1000_test_msi_interrupt - Returns 0 for successful test
       
  3820  * @adapter: board private struct
       
  3821  *
       
  3822  * code flow taken from tg3.c
       
  3823  **/
       
  3824 static int e1000_test_msi_interrupt(struct e1000_adapter *adapter)
       
  3825 {
       
  3826 	struct net_device *netdev = adapter->netdev;
       
  3827 	struct e1000_hw *hw = &adapter->hw;
       
  3828 	int err;
       
  3829 
       
  3830 	/* poll_enable hasn't been called yet, so don't need disable */
       
  3831 	/* clear any pending events */
       
  3832 	er32(ICR);
       
  3833 
       
  3834 	/* free the real vector and request a test handler */
       
  3835 	e1000_free_irq(adapter);
       
  3836 	e1000e_reset_interrupt_capability(adapter);
       
  3837 
       
  3838 	/* Assume that the test fails, if it succeeds then the test
       
  3839 	 * MSI irq handler will unset this flag */
       
  3840 	adapter->flags |= FLAG_MSI_TEST_FAILED;
       
  3841 
       
  3842 	err = pci_enable_msi(adapter->pdev);
       
  3843 	if (err)
       
  3844 		goto msi_test_failed;
       
  3845 
       
  3846 	err = request_irq(adapter->pdev->irq, e1000_intr_msi_test, 0,
       
  3847 			  netdev->name, netdev);
       
  3848 	if (err) {
       
  3849 		pci_disable_msi(adapter->pdev);
       
  3850 		goto msi_test_failed;
       
  3851 	}
       
  3852 
       
  3853 	wmb();
       
  3854 
       
  3855 	e1000_irq_enable(adapter);
       
  3856 
       
  3857 	/* fire an unusual interrupt on the test handler */
       
  3858 	ew32(ICS, E1000_ICS_RXSEQ);
       
  3859 	e1e_flush();
       
  3860 	msleep(100);
       
  3861 
       
  3862 	e1000_irq_disable(adapter);
       
  3863 
       
  3864 	rmb();
       
  3865 
       
  3866 	if (adapter->flags & FLAG_MSI_TEST_FAILED) {
       
  3867 		adapter->int_mode = E1000E_INT_MODE_LEGACY;
       
  3868 		e_info("MSI interrupt test failed, using legacy interrupt.\n");
       
  3869 	} else {
       
  3870 		e_dbg("MSI interrupt test succeeded!\n");
       
  3871 	}
       
  3872 
       
  3873 	free_irq(adapter->pdev->irq, netdev);
       
  3874 	pci_disable_msi(adapter->pdev);
       
  3875 
       
  3876 msi_test_failed:
       
  3877 	e1000e_set_interrupt_capability(adapter);
       
  3878 	return e1000_request_irq(adapter);
       
  3879 }
       
  3880 
       
  3881 /**
       
  3882  * e1000_test_msi - Returns 0 if MSI test succeeds or INTx mode is restored
       
  3883  * @adapter: board private struct
       
  3884  *
       
  3885  * code flow taken from tg3.c, called with e1000 interrupts disabled.
       
  3886  **/
       
  3887 static int e1000_test_msi(struct e1000_adapter *adapter)
       
  3888 {
       
  3889 	int err;
       
  3890 	u16 pci_cmd;
       
  3891 
       
  3892 	if (!(adapter->flags & FLAG_MSI_ENABLED))
       
  3893 		return 0;
       
  3894 
       
  3895 	/* disable SERR in case the MSI write causes a master abort */
       
  3896 	pci_read_config_word(adapter->pdev, PCI_COMMAND, &pci_cmd);
       
  3897 	if (pci_cmd & PCI_COMMAND_SERR)
       
  3898 		pci_write_config_word(adapter->pdev, PCI_COMMAND,
       
  3899 				      pci_cmd & ~PCI_COMMAND_SERR);
       
  3900 
       
  3901 	err = e1000_test_msi_interrupt(adapter);
       
  3902 
       
  3903 	/* re-enable SERR */
       
  3904 	if (pci_cmd & PCI_COMMAND_SERR) {
       
  3905 		pci_read_config_word(adapter->pdev, PCI_COMMAND, &pci_cmd);
       
  3906 		pci_cmd |= PCI_COMMAND_SERR;
       
  3907 		pci_write_config_word(adapter->pdev, PCI_COMMAND, pci_cmd);
       
  3908 	}
       
  3909 
       
  3910 	return err;
       
  3911 }
       
  3912 
       
  3913 /**
       
  3914  * e1000_open - Called when a network interface is made active
       
  3915  * @netdev: network interface device structure
       
  3916  *
       
  3917  * Returns 0 on success, negative value on failure
       
  3918  *
       
  3919  * The open entry point is called when a network interface is made
       
  3920  * active by the system (IFF_UP).  At this point all resources needed
       
  3921  * for transmit and receive operations are allocated, the interrupt
       
  3922  * handler is registered with the OS, the watchdog timer is started,
       
  3923  * and the stack is notified that the interface is ready.
       
  3924  **/
       
  3925 static int e1000_open(struct net_device *netdev)
       
  3926 {
       
  3927 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3928 	struct e1000_hw *hw = &adapter->hw;
       
  3929 	struct pci_dev *pdev = adapter->pdev;
       
  3930 	int err;
       
  3931 
       
  3932 	/* disallow open during test */
       
  3933 	if (test_bit(__E1000_TESTING, &adapter->state))
       
  3934 		return -EBUSY;
       
  3935 
       
  3936 	pm_runtime_get_sync(&pdev->dev);
       
  3937 
       
  3938 	if (adapter->ecdev) {
       
  3939 		ecdev_set_link(adapter->ecdev, 0);
       
  3940 	} else {
       
  3941 		netif_carrier_off(netdev);
       
  3942 	}
       
  3943 
       
  3944 	/* allocate transmit descriptors */
       
  3945 	err = e1000e_setup_tx_resources(adapter->tx_ring);
       
  3946 	if (err)
       
  3947 		goto err_setup_tx;
       
  3948 
       
  3949 	/* allocate receive descriptors */
       
  3950 	err = e1000e_setup_rx_resources(adapter->rx_ring);
       
  3951 	if (err)
       
  3952 		goto err_setup_rx;
       
  3953 
       
  3954 	/*
       
  3955 	 * If AMT is enabled, let the firmware know that the network
       
  3956 	 * interface is now open and reset the part to a known state.
       
  3957 	 */
       
  3958 	if (adapter->flags & FLAG_HAS_AMT) {
       
  3959 		e1000e_get_hw_control(adapter);
       
  3960 		e1000e_reset(adapter);
       
  3961 	}
       
  3962 
       
  3963 	e1000e_power_up_phy(adapter);
       
  3964 
       
  3965 	adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
       
  3966 	if ((adapter->hw.mng_cookie.status &
       
  3967 	     E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
       
  3968 		e1000_update_mng_vlan(adapter);
       
  3969 
       
  3970 	/* DMA latency requirement to workaround jumbo issue */
       
  3971 	if (adapter->hw.mac.type == e1000_pch2lan)
       
  3972 		pm_qos_add_request(&adapter->netdev->pm_qos_req,
       
  3973 				   PM_QOS_CPU_DMA_LATENCY,
       
  3974 				   PM_QOS_DEFAULT_VALUE);
       
  3975 
       
  3976 	/*
       
  3977 	 * before we allocate an interrupt, we must be ready to handle it.
       
  3978 	 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
       
  3979 	 * as soon as we call pci_request_irq, so we have to setup our
       
  3980 	 * clean_rx handler before we do so.
       
  3981 	 */
       
  3982 	e1000_configure(adapter);
       
  3983 
       
  3984 	err = e1000_request_irq(adapter);
       
  3985 	if (err)
       
  3986 		goto err_req_irq;
       
  3987 
       
  3988 	/*
       
  3989 	 * Work around PCIe errata with MSI interrupts causing some chipsets to
       
  3990 	 * ignore e1000e MSI messages, which means we need to test our MSI
       
  3991 	 * interrupt now
       
  3992 	 */
       
  3993 	if (adapter->int_mode != E1000E_INT_MODE_LEGACY) {
       
  3994 		err = e1000_test_msi(adapter);
       
  3995 		if (err) {
       
  3996 			e_err("Interrupt allocation failed\n");
       
  3997 			goto err_req_irq;
       
  3998 		}
       
  3999 	}
       
  4000 
       
  4001 	/* From here on the code is the same as e1000e_up() */
       
  4002 	clear_bit(__E1000_DOWN, &adapter->state);
       
  4003 
       
  4004 	if (!adapter->ecdev) {
       
  4005 		napi_enable(&adapter->napi);
       
  4006 
       
  4007 		e1000_irq_enable(adapter);
       
  4008 	}
       
  4009 
       
  4010 	adapter->tx_hang_recheck = false;
       
  4011 
       
  4012 	if (!adapter->ecdev) {
       
  4013 		netif_start_queue(netdev);
       
  4014 	}
       
  4015 
       
  4016 	adapter->idle_check = true;
       
  4017 	pm_runtime_put(&pdev->dev);
       
  4018 
       
  4019 	/* fire a link status change interrupt to start the watchdog */
       
  4020 	if (adapter->msix_entries)
       
  4021 		ew32(ICS, E1000_ICS_LSC | E1000_ICR_OTHER);
       
  4022 	else
       
  4023 		ew32(ICS, E1000_ICS_LSC);
       
  4024 
       
  4025 	return 0;
       
  4026 
       
  4027 err_req_irq:
       
  4028 	e1000e_release_hw_control(adapter);
       
  4029 	e1000_power_down_phy(adapter);
       
  4030 	e1000e_free_rx_resources(adapter->rx_ring);
       
  4031 err_setup_rx:
       
  4032 	e1000e_free_tx_resources(adapter->tx_ring);
       
  4033 err_setup_tx:
       
  4034 	e1000e_reset(adapter);
       
  4035 	pm_runtime_put_sync(&pdev->dev);
       
  4036 
       
  4037 	return err;
       
  4038 }
       
  4039 
       
  4040 /**
       
  4041  * e1000_close - Disables a network interface
       
  4042  * @netdev: network interface device structure
       
  4043  *
       
  4044  * Returns 0, this is not allowed to fail
       
  4045  *
       
  4046  * The close entry point is called when an interface is de-activated
       
  4047  * by the OS.  The hardware is still under the drivers control, but
       
  4048  * needs to be disabled.  A global MAC reset is issued to stop the
       
  4049  * hardware, and all transmit and receive resources are freed.
       
  4050  **/
       
  4051 static int e1000_close(struct net_device *netdev)
       
  4052 {
       
  4053 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  4054 	struct pci_dev *pdev = adapter->pdev;
       
  4055 	int count = E1000_CHECK_RESET_COUNT;
       
  4056 
       
  4057 	while (test_bit(__E1000_RESETTING, &adapter->state) && count--)
       
  4058 		usleep_range(10000, 20000);
       
  4059 
       
  4060 	WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
       
  4061 
       
  4062 	pm_runtime_get_sync(&pdev->dev);
       
  4063 
       
  4064 	napi_disable(&adapter->napi);
       
  4065 
       
  4066 	if (!test_bit(__E1000_DOWN, &adapter->state)) {
       
  4067 		e1000e_down(adapter);
       
  4068 		e1000_free_irq(adapter);
       
  4069 	}
       
  4070 	e1000_power_down_phy(adapter);
       
  4071 
       
  4072 	e1000e_free_tx_resources(adapter->tx_ring);
       
  4073 	e1000e_free_rx_resources(adapter->rx_ring);
       
  4074 
       
  4075 	/*
       
  4076 	 * kill manageability vlan ID if supported, but not if a vlan with
       
  4077 	 * the same ID is registered on the host OS (let 8021q kill it)
       
  4078 	 */
       
  4079 	if (adapter->hw.mng_cookie.status &
       
  4080 	    E1000_MNG_DHCP_COOKIE_STATUS_VLAN)
       
  4081 		e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
       
  4082 
       
  4083 	/*
       
  4084 	 * If AMT is enabled, let the firmware know that the network
       
  4085 	 * interface is now closed
       
  4086 	 */
       
  4087 	if ((adapter->flags & FLAG_HAS_AMT) &&
       
  4088 	    !test_bit(__E1000_TESTING, &adapter->state))
       
  4089 		e1000e_release_hw_control(adapter);
       
  4090 
       
  4091 	if (adapter->hw.mac.type == e1000_pch2lan)
       
  4092 		pm_qos_remove_request(&adapter->netdev->pm_qos_req);
       
  4093 
       
  4094 	pm_runtime_put_sync(&pdev->dev);
       
  4095 
       
  4096 	return 0;
       
  4097 }
       
  4098 /**
       
  4099  * e1000_set_mac - Change the Ethernet Address of the NIC
       
  4100  * @netdev: network interface device structure
       
  4101  * @p: pointer to an address structure
       
  4102  *
       
  4103  * Returns 0 on success, negative on failure
       
  4104  **/
       
  4105 static int e1000_set_mac(struct net_device *netdev, void *p)
       
  4106 {
       
  4107 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  4108 	struct sockaddr *addr = p;
       
  4109 
       
  4110 	if (!is_valid_ether_addr(addr->sa_data))
       
  4111 		return -EADDRNOTAVAIL;
       
  4112 
       
  4113 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
       
  4114 	memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
       
  4115 
       
  4116 	e1000e_rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
       
  4117 
       
  4118 	if (adapter->flags & FLAG_RESET_OVERWRITES_LAA) {
       
  4119 		/* activate the work around */
       
  4120 		e1000e_set_laa_state_82571(&adapter->hw, 1);
       
  4121 
       
  4122 		/*
       
  4123 		 * Hold a copy of the LAA in RAR[14] This is done so that
       
  4124 		 * between the time RAR[0] gets clobbered  and the time it
       
  4125 		 * gets fixed (in e1000_watchdog), the actual LAA is in one
       
  4126 		 * of the RARs and no incoming packets directed to this port
       
  4127 		 * are dropped. Eventually the LAA will be in RAR[0] and
       
  4128 		 * RAR[14]
       
  4129 		 */
       
  4130 		e1000e_rar_set(&adapter->hw,
       
  4131 			      adapter->hw.mac.addr,
       
  4132 			      adapter->hw.mac.rar_entry_count - 1);
       
  4133 	}
       
  4134 
       
  4135 	return 0;
       
  4136 }
       
  4137 
       
  4138 /**
       
  4139  * e1000e_update_phy_task - work thread to update phy
       
  4140  * @work: pointer to our work struct
       
  4141  *
       
  4142  * this worker thread exists because we must acquire a
       
  4143  * semaphore to read the phy, which we could msleep while
       
  4144  * waiting for it, and we can't msleep in a timer.
       
  4145  **/
       
  4146 static void e1000e_update_phy_task(struct work_struct *work)
       
  4147 {
       
  4148 	struct e1000_adapter *adapter = container_of(work,
       
  4149 					struct e1000_adapter, update_phy_task);
       
  4150 
       
  4151 	if (test_bit(__E1000_DOWN, &adapter->state))
       
  4152 		return;
       
  4153 
       
  4154 	e1000_get_phy_info(&adapter->hw);
       
  4155 }
       
  4156 
       
  4157 /*
       
  4158  * Need to wait a few seconds after link up to get diagnostic information from
       
  4159  * the phy
       
  4160  */
       
  4161 static void e1000_update_phy_info(unsigned long data)
       
  4162 {
       
  4163 	struct e1000_adapter *adapter = (struct e1000_adapter *) data;
       
  4164 
       
  4165 	if (test_bit(__E1000_DOWN, &adapter->state))
       
  4166 		return;
       
  4167 
       
  4168 	schedule_work(&adapter->update_phy_task);
       
  4169 }
       
  4170 
       
  4171 /**
       
  4172  * e1000e_update_phy_stats - Update the PHY statistics counters
       
  4173  * @adapter: board private structure
       
  4174  *
       
  4175  * Read/clear the upper 16-bit PHY registers and read/accumulate lower
       
  4176  **/
       
  4177 static void e1000e_update_phy_stats(struct e1000_adapter *adapter)
       
  4178 {
       
  4179 	struct e1000_hw *hw = &adapter->hw;
       
  4180 	s32 ret_val;
       
  4181 	u16 phy_data;
       
  4182 
       
  4183 	ret_val = hw->phy.ops.acquire(hw);
       
  4184 	if (ret_val)
       
  4185 		return;
       
  4186 
       
  4187 	/*
       
  4188 	 * A page set is expensive so check if already on desired page.
       
  4189 	 * If not, set to the page with the PHY status registers.
       
  4190 	 */
       
  4191 	hw->phy.addr = 1;
       
  4192 	ret_val = e1000e_read_phy_reg_mdic(hw, IGP01E1000_PHY_PAGE_SELECT,
       
  4193 					   &phy_data);
       
  4194 	if (ret_val)
       
  4195 		goto release;
       
  4196 	if (phy_data != (HV_STATS_PAGE << IGP_PAGE_SHIFT)) {
       
  4197 		ret_val = hw->phy.ops.set_page(hw,
       
  4198 					       HV_STATS_PAGE << IGP_PAGE_SHIFT);
       
  4199 		if (ret_val)
       
  4200 			goto release;
       
  4201 	}
       
  4202 
       
  4203 	/* Single Collision Count */
       
  4204 	hw->phy.ops.read_reg_page(hw, HV_SCC_UPPER, &phy_data);
       
  4205 	ret_val = hw->phy.ops.read_reg_page(hw, HV_SCC_LOWER, &phy_data);
       
  4206 	if (!ret_val)
       
  4207 		adapter->stats.scc += phy_data;
       
  4208 
       
  4209 	/* Excessive Collision Count */
       
  4210 	hw->phy.ops.read_reg_page(hw, HV_ECOL_UPPER, &phy_data);
       
  4211 	ret_val = hw->phy.ops.read_reg_page(hw, HV_ECOL_LOWER, &phy_data);
       
  4212 	if (!ret_val)
       
  4213 		adapter->stats.ecol += phy_data;
       
  4214 
       
  4215 	/* Multiple Collision Count */
       
  4216 	hw->phy.ops.read_reg_page(hw, HV_MCC_UPPER, &phy_data);
       
  4217 	ret_val = hw->phy.ops.read_reg_page(hw, HV_MCC_LOWER, &phy_data);
       
  4218 	if (!ret_val)
       
  4219 		adapter->stats.mcc += phy_data;
       
  4220 
       
  4221 	/* Late Collision Count */
       
  4222 	hw->phy.ops.read_reg_page(hw, HV_LATECOL_UPPER, &phy_data);
       
  4223 	ret_val = hw->phy.ops.read_reg_page(hw, HV_LATECOL_LOWER, &phy_data);
       
  4224 	if (!ret_val)
       
  4225 		adapter->stats.latecol += phy_data;
       
  4226 
       
  4227 	/* Collision Count - also used for adaptive IFS */
       
  4228 	hw->phy.ops.read_reg_page(hw, HV_COLC_UPPER, &phy_data);
       
  4229 	ret_val = hw->phy.ops.read_reg_page(hw, HV_COLC_LOWER, &phy_data);
       
  4230 	if (!ret_val)
       
  4231 		hw->mac.collision_delta = phy_data;
       
  4232 
       
  4233 	/* Defer Count */
       
  4234 	hw->phy.ops.read_reg_page(hw, HV_DC_UPPER, &phy_data);
       
  4235 	ret_val = hw->phy.ops.read_reg_page(hw, HV_DC_LOWER, &phy_data);
       
  4236 	if (!ret_val)
       
  4237 		adapter->stats.dc += phy_data;
       
  4238 
       
  4239 	/* Transmit with no CRS */
       
  4240 	hw->phy.ops.read_reg_page(hw, HV_TNCRS_UPPER, &phy_data);
       
  4241 	ret_val = hw->phy.ops.read_reg_page(hw, HV_TNCRS_LOWER, &phy_data);
       
  4242 	if (!ret_val)
       
  4243 		adapter->stats.tncrs += phy_data;
       
  4244 
       
  4245 release:
       
  4246 	hw->phy.ops.release(hw);
       
  4247 }
       
  4248 
       
  4249 /**
       
  4250  * e1000e_update_stats - Update the board statistics counters
       
  4251  * @adapter: board private structure
       
  4252  **/
       
  4253 static void e1000e_update_stats(struct e1000_adapter *adapter)
       
  4254 {
       
  4255 	struct net_device *netdev = adapter->netdev;
       
  4256 	struct e1000_hw *hw = &adapter->hw;
       
  4257 	struct pci_dev *pdev = adapter->pdev;
       
  4258 
       
  4259 	/*
       
  4260 	 * Prevent stats update while adapter is being reset, or if the pci
       
  4261 	 * connection is down.
       
  4262 	 */
       
  4263 	if (adapter->link_speed == 0)
       
  4264 		return;
       
  4265 	if (pci_channel_offline(pdev))
       
  4266 		return;
       
  4267 
       
  4268 	adapter->stats.crcerrs += er32(CRCERRS);
       
  4269 	adapter->stats.gprc += er32(GPRC);
       
  4270 	adapter->stats.gorc += er32(GORCL);
       
  4271 	er32(GORCH); /* Clear gorc */
       
  4272 	adapter->stats.bprc += er32(BPRC);
       
  4273 	adapter->stats.mprc += er32(MPRC);
       
  4274 	adapter->stats.roc += er32(ROC);
       
  4275 
       
  4276 	adapter->stats.mpc += er32(MPC);
       
  4277 
       
  4278 	/* Half-duplex statistics */
       
  4279 	if (adapter->link_duplex == HALF_DUPLEX) {
       
  4280 		if (adapter->flags2 & FLAG2_HAS_PHY_STATS) {
       
  4281 			e1000e_update_phy_stats(adapter);
       
  4282 		} else {
       
  4283 			adapter->stats.scc += er32(SCC);
       
  4284 			adapter->stats.ecol += er32(ECOL);
       
  4285 			adapter->stats.mcc += er32(MCC);
       
  4286 			adapter->stats.latecol += er32(LATECOL);
       
  4287 			adapter->stats.dc += er32(DC);
       
  4288 
       
  4289 			hw->mac.collision_delta = er32(COLC);
       
  4290 
       
  4291 			if ((hw->mac.type != e1000_82574) &&
       
  4292 			    (hw->mac.type != e1000_82583))
       
  4293 				adapter->stats.tncrs += er32(TNCRS);
       
  4294 		}
       
  4295 		adapter->stats.colc += hw->mac.collision_delta;
       
  4296 	}
       
  4297 
       
  4298 	adapter->stats.xonrxc += er32(XONRXC);
       
  4299 	adapter->stats.xontxc += er32(XONTXC);
       
  4300 	adapter->stats.xoffrxc += er32(XOFFRXC);
       
  4301 	adapter->stats.xofftxc += er32(XOFFTXC);
       
  4302 	adapter->stats.gptc += er32(GPTC);
       
  4303 	adapter->stats.gotc += er32(GOTCL);
       
  4304 	er32(GOTCH); /* Clear gotc */
       
  4305 	adapter->stats.rnbc += er32(RNBC);
       
  4306 	adapter->stats.ruc += er32(RUC);
       
  4307 
       
  4308 	adapter->stats.mptc += er32(MPTC);
       
  4309 	adapter->stats.bptc += er32(BPTC);
       
  4310 
       
  4311 	/* used for adaptive IFS */
       
  4312 
       
  4313 	hw->mac.tx_packet_delta = er32(TPT);
       
  4314 	adapter->stats.tpt += hw->mac.tx_packet_delta;
       
  4315 
       
  4316 	adapter->stats.algnerrc += er32(ALGNERRC);
       
  4317 	adapter->stats.rxerrc += er32(RXERRC);
       
  4318 	adapter->stats.cexterr += er32(CEXTERR);
       
  4319 	adapter->stats.tsctc += er32(TSCTC);
       
  4320 	adapter->stats.tsctfc += er32(TSCTFC);
       
  4321 
       
  4322 	/* Fill out the OS statistics structure */
       
  4323 	netdev->stats.multicast = adapter->stats.mprc;
       
  4324 	netdev->stats.collisions = adapter->stats.colc;
       
  4325 
       
  4326 	/* Rx Errors */
       
  4327 
       
  4328 	/*
       
  4329 	 * RLEC on some newer hardware can be incorrect so build
       
  4330 	 * our own version based on RUC and ROC
       
  4331 	 */
       
  4332 	netdev->stats.rx_errors = adapter->stats.rxerrc +
       
  4333 		adapter->stats.crcerrs + adapter->stats.algnerrc +
       
  4334 		adapter->stats.ruc + adapter->stats.roc +
       
  4335 		adapter->stats.cexterr;
       
  4336 	netdev->stats.rx_length_errors = adapter->stats.ruc +
       
  4337 					      adapter->stats.roc;
       
  4338 	netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
       
  4339 	netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
       
  4340 	netdev->stats.rx_missed_errors = adapter->stats.mpc;
       
  4341 
       
  4342 	/* Tx Errors */
       
  4343 	netdev->stats.tx_errors = adapter->stats.ecol +
       
  4344 				       adapter->stats.latecol;
       
  4345 	netdev->stats.tx_aborted_errors = adapter->stats.ecol;
       
  4346 	netdev->stats.tx_window_errors = adapter->stats.latecol;
       
  4347 	netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
       
  4348 
       
  4349 	/* Tx Dropped needs to be maintained elsewhere */
       
  4350 
       
  4351 	/* Management Stats */
       
  4352 	adapter->stats.mgptc += er32(MGTPTC);
       
  4353 	adapter->stats.mgprc += er32(MGTPRC);
       
  4354 	adapter->stats.mgpdc += er32(MGTPDC);
       
  4355 }
       
  4356 
       
  4357 /**
       
  4358  * e1000_phy_read_status - Update the PHY register status snapshot
       
  4359  * @adapter: board private structure
       
  4360  **/
       
  4361 static void e1000_phy_read_status(struct e1000_adapter *adapter)
       
  4362 {
       
  4363 	struct e1000_hw *hw = &adapter->hw;
       
  4364 	struct e1000_phy_regs *phy = &adapter->phy_regs;
       
  4365 
       
  4366 	if ((er32(STATUS) & E1000_STATUS_LU) &&
       
  4367 	    (adapter->hw.phy.media_type == e1000_media_type_copper)) {
       
  4368 		int ret_val;
       
  4369 
       
  4370 		ret_val  = e1e_rphy(hw, PHY_CONTROL, &phy->bmcr);
       
  4371 		ret_val |= e1e_rphy(hw, PHY_STATUS, &phy->bmsr);
       
  4372 		ret_val |= e1e_rphy(hw, PHY_AUTONEG_ADV, &phy->advertise);
       
  4373 		ret_val |= e1e_rphy(hw, PHY_LP_ABILITY, &phy->lpa);
       
  4374 		ret_val |= e1e_rphy(hw, PHY_AUTONEG_EXP, &phy->expansion);
       
  4375 		ret_val |= e1e_rphy(hw, PHY_1000T_CTRL, &phy->ctrl1000);
       
  4376 		ret_val |= e1e_rphy(hw, PHY_1000T_STATUS, &phy->stat1000);
       
  4377 		ret_val |= e1e_rphy(hw, PHY_EXT_STATUS, &phy->estatus);
       
  4378 		if (ret_val)
       
  4379 			e_warn("Error reading PHY register\n");
       
  4380 	} else {
       
  4381 		/*
       
  4382 		 * Do not read PHY registers if link is not up
       
  4383 		 * Set values to typical power-on defaults
       
  4384 		 */
       
  4385 		phy->bmcr = (BMCR_SPEED1000 | BMCR_ANENABLE | BMCR_FULLDPLX);
       
  4386 		phy->bmsr = (BMSR_100FULL | BMSR_100HALF | BMSR_10FULL |
       
  4387 			     BMSR_10HALF | BMSR_ESTATEN | BMSR_ANEGCAPABLE |
       
  4388 			     BMSR_ERCAP);
       
  4389 		phy->advertise = (ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP |
       
  4390 				  ADVERTISE_ALL | ADVERTISE_CSMA);
       
  4391 		phy->lpa = 0;
       
  4392 		phy->expansion = EXPANSION_ENABLENPAGE;
       
  4393 		phy->ctrl1000 = ADVERTISE_1000FULL;
       
  4394 		phy->stat1000 = 0;
       
  4395 		phy->estatus = (ESTATUS_1000_TFULL | ESTATUS_1000_THALF);
       
  4396 	}
       
  4397 }
       
  4398 
       
  4399 static void e1000_print_link_info(struct e1000_adapter *adapter)
       
  4400 {
       
  4401 	struct e1000_hw *hw = &adapter->hw;
       
  4402 	u32 ctrl = er32(CTRL);
       
  4403 
       
  4404 	/* Link status message must follow this format for user tools */
       
  4405 	printk(KERN_INFO "e1000e: %s NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
       
  4406 		adapter->netdev->name,
       
  4407 		adapter->link_speed,
       
  4408 		adapter->link_duplex == FULL_DUPLEX ? "Full" : "Half",
       
  4409 		(ctrl & E1000_CTRL_TFCE) && (ctrl & E1000_CTRL_RFCE) ? "Rx/Tx" :
       
  4410 		(ctrl & E1000_CTRL_RFCE) ? "Rx" :
       
  4411 		(ctrl & E1000_CTRL_TFCE) ? "Tx" : "None");
       
  4412 }
       
  4413 
       
  4414 static bool e1000e_has_link(struct e1000_adapter *adapter)
       
  4415 {
       
  4416 	struct e1000_hw *hw = &adapter->hw;
       
  4417 	bool link_active = false;
       
  4418 	s32 ret_val = 0;
       
  4419 
       
  4420 	/*
       
  4421 	 * get_link_status is set on LSC (link status) interrupt or
       
  4422 	 * Rx sequence error interrupt.  get_link_status will stay
       
  4423 	 * false until the check_for_link establishes link
       
  4424 	 * for copper adapters ONLY
       
  4425 	 */
       
  4426 	switch (hw->phy.media_type) {
       
  4427 	case e1000_media_type_copper:
       
  4428 		if (hw->mac.get_link_status) {
       
  4429 			ret_val = hw->mac.ops.check_for_link(hw);
       
  4430 			link_active = !hw->mac.get_link_status;
       
  4431 		} else {
       
  4432 			link_active = true;
       
  4433 		}
       
  4434 		break;
       
  4435 	case e1000_media_type_fiber:
       
  4436 		ret_val = hw->mac.ops.check_for_link(hw);
       
  4437 		link_active = !!(er32(STATUS) & E1000_STATUS_LU);
       
  4438 		break;
       
  4439 	case e1000_media_type_internal_serdes:
       
  4440 		ret_val = hw->mac.ops.check_for_link(hw);
       
  4441 		link_active = adapter->hw.mac.serdes_has_link;
       
  4442 		break;
       
  4443 	default:
       
  4444 	case e1000_media_type_unknown:
       
  4445 		break;
       
  4446 	}
       
  4447 
       
  4448 	if ((ret_val == E1000_ERR_PHY) && (hw->phy.type == e1000_phy_igp_3) &&
       
  4449 	    (er32(CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
       
  4450 		/* See e1000_kmrn_lock_loss_workaround_ich8lan() */
       
  4451 		e_info("Gigabit has been disabled, downgrading speed\n");
       
  4452 	}
       
  4453 
       
  4454 	return link_active;
       
  4455 }
       
  4456 
       
  4457 static void e1000e_enable_receives(struct e1000_adapter *adapter)
       
  4458 {
       
  4459 	/* make sure the receive unit is started */
       
  4460 	if ((adapter->flags & FLAG_RX_NEEDS_RESTART) &&
       
  4461 	    (adapter->flags & FLAG_RX_RESTART_NOW)) {
       
  4462 		struct e1000_hw *hw = &adapter->hw;
       
  4463 		u32 rctl = er32(RCTL);
       
  4464 		ew32(RCTL, rctl | E1000_RCTL_EN);
       
  4465 		adapter->flags &= ~FLAG_RX_RESTART_NOW;
       
  4466 	}
       
  4467 }
       
  4468 
       
  4469 static void e1000e_check_82574_phy_workaround(struct e1000_adapter *adapter)
       
  4470 {
       
  4471 	struct e1000_hw *hw = &adapter->hw;
       
  4472 
       
  4473 	/*
       
  4474 	 * With 82574 controllers, PHY needs to be checked periodically
       
  4475 	 * for hung state and reset, if two calls return true
       
  4476 	 */
       
  4477 	if (e1000_check_phy_82574(hw))
       
  4478 		adapter->phy_hang_count++;
       
  4479 	else
       
  4480 		adapter->phy_hang_count = 0;
       
  4481 
       
  4482 	if (adapter->phy_hang_count > 1) {
       
  4483 		adapter->phy_hang_count = 0;
       
  4484 		schedule_work(&adapter->reset_task);
       
  4485 	}
       
  4486 }
       
  4487 
       
  4488 /**
       
  4489  * e1000_watchdog - Timer Call-back
       
  4490  * @data: pointer to adapter cast into an unsigned long
       
  4491  **/
       
  4492 static void e1000_watchdog(unsigned long data)
       
  4493 {
       
  4494 	struct e1000_adapter *adapter = (struct e1000_adapter *) data;
       
  4495 
       
  4496 	/* Do the rest outside of interrupt context */
       
  4497 	schedule_work(&adapter->watchdog_task);
       
  4498 
       
  4499 	/* TODO: make this use queue_delayed_work() */
       
  4500 }
       
  4501 
       
  4502 static void e1000_watchdog_task(struct work_struct *work)
       
  4503 {
       
  4504 	struct e1000_adapter *adapter = container_of(work,
       
  4505 					struct e1000_adapter, watchdog_task);
       
  4506 	struct net_device *netdev = adapter->netdev;
       
  4507 	struct e1000_mac_info *mac = &adapter->hw.mac;
       
  4508 	struct e1000_phy_info *phy = &adapter->hw.phy;
       
  4509 	struct e1000_ring *tx_ring = adapter->tx_ring;
       
  4510 	struct e1000_hw *hw = &adapter->hw;
       
  4511 	u32 link, tctl;
       
  4512 
       
  4513 	if (test_bit(__E1000_DOWN, &adapter->state))
       
  4514 		return;
       
  4515 
       
  4516 	link = e1000e_has_link(adapter);
       
  4517 	if ((adapter->ecdev && (ecdev_get_link(adapter->ecdev)) && link)
       
  4518 			|| (!adapter->ecdev && (netif_carrier_ok(netdev)) && link)) {
       
  4519 		if (!adapter->ecdev) {
       
  4520 			/* Cancel scheduled suspend requests. */
       
  4521 			pm_runtime_resume(netdev->dev.parent);
       
  4522 		}
       
  4523 
       
  4524 		e1000e_enable_receives(adapter);
       
  4525 		goto link_up;
       
  4526 	}
       
  4527 
       
  4528 	if ((e1000e_enable_tx_pkt_filtering(hw)) &&
       
  4529 	    (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id))
       
  4530 		e1000_update_mng_vlan(adapter);
       
  4531 
       
  4532 	if (link) {
       
  4533 		if ((adapter->ecdev && !ecdev_get_link(adapter->ecdev))
       
  4534 				|| (!adapter->ecdev && !netif_carrier_ok(netdev))) {
       
  4535 			bool txb2b = true;
       
  4536 
       
  4537 			/* Cancel scheduled suspend requests. */
       
  4538 			pm_runtime_resume(netdev->dev.parent);
       
  4539 
       
  4540 			/* update snapshot of PHY registers on LSC */
       
  4541 			e1000_phy_read_status(adapter);
       
  4542 			mac->ops.get_link_up_info(&adapter->hw,
       
  4543 						   &adapter->link_speed,
       
  4544 						   &adapter->link_duplex);
       
  4545 			e1000_print_link_info(adapter);
       
  4546 			/*
       
  4547 			 * On supported PHYs, check for duplex mismatch only
       
  4548 			 * if link has autonegotiated at 10/100 half
       
  4549 			 */
       
  4550 			if ((hw->phy.type == e1000_phy_igp_3 ||
       
  4551 			     hw->phy.type == e1000_phy_bm) &&
       
  4552 			    (hw->mac.autoneg == true) &&
       
  4553 			    (adapter->link_speed == SPEED_10 ||
       
  4554 			     adapter->link_speed == SPEED_100) &&
       
  4555 			    (adapter->link_duplex == HALF_DUPLEX)) {
       
  4556 				u16 autoneg_exp;
       
  4557 
       
  4558 				e1e_rphy(hw, PHY_AUTONEG_EXP, &autoneg_exp);
       
  4559 
       
  4560 				if (!(autoneg_exp & NWAY_ER_LP_NWAY_CAPS))
       
  4561 					e_info("Autonegotiated half duplex but link partner cannot autoneg.  Try forcing full duplex if link gets many collisions.\n");
       
  4562 			}
       
  4563 
       
  4564 			/* adjust timeout factor according to speed/duplex */
       
  4565 			adapter->tx_timeout_factor = 1;
       
  4566 			switch (adapter->link_speed) {
       
  4567 			case SPEED_10:
       
  4568 				txb2b = false;
       
  4569 				adapter->tx_timeout_factor = 16;
       
  4570 				break;
       
  4571 			case SPEED_100:
       
  4572 				txb2b = false;
       
  4573 				adapter->tx_timeout_factor = 10;
       
  4574 				break;
       
  4575 			}
       
  4576 
       
  4577 			/*
       
  4578 			 * workaround: re-program speed mode bit after
       
  4579 			 * link-up event
       
  4580 			 */
       
  4581 			if ((adapter->flags & FLAG_TARC_SPEED_MODE_BIT) &&
       
  4582 			    !txb2b) {
       
  4583 				u32 tarc0;
       
  4584 				tarc0 = er32(TARC(0));
       
  4585 				tarc0 &= ~SPEED_MODE_BIT;
       
  4586 				ew32(TARC(0), tarc0);
       
  4587 			}
       
  4588 
       
  4589 			/*
       
  4590 			 * disable TSO for pcie and 10/100 speeds, to avoid
       
  4591 			 * some hardware issues
       
  4592 			 */
       
  4593 			if (!(adapter->flags & FLAG_TSO_FORCE)) {
       
  4594 				switch (adapter->link_speed) {
       
  4595 				case SPEED_10:
       
  4596 				case SPEED_100:
       
  4597 					e_info("10/100 speed: disabling TSO\n");
       
  4598 					netdev->features &= ~NETIF_F_TSO;
       
  4599 					netdev->features &= ~NETIF_F_TSO6;
       
  4600 					break;
       
  4601 				case SPEED_1000:
       
  4602 					netdev->features |= NETIF_F_TSO;
       
  4603 					netdev->features |= NETIF_F_TSO6;
       
  4604 					break;
       
  4605 				default:
       
  4606 					/* oops */
       
  4607 					break;
       
  4608 				}
       
  4609 			}
       
  4610 
       
  4611 			/*
       
  4612 			 * enable transmits in the hardware, need to do this
       
  4613 			 * after setting TARC(0)
       
  4614 			 */
       
  4615 			tctl = er32(TCTL);
       
  4616 			tctl |= E1000_TCTL_EN;
       
  4617 			ew32(TCTL, tctl);
       
  4618 
       
  4619                         /*
       
  4620 			 * Perform any post-link-up configuration before
       
  4621 			 * reporting link up.
       
  4622 			 */
       
  4623 			if (phy->ops.cfg_on_link_up)
       
  4624 				phy->ops.cfg_on_link_up(hw);
       
  4625 
       
  4626 			if (adapter->ecdev)
       
  4627 				ecdev_set_link(adapter->ecdev, 1);
       
  4628 			else
       
  4629 				netif_carrier_on(netdev);
       
  4630 
       
  4631 			if (!adapter->ecdev && !test_bit(__E1000_DOWN, &adapter->state))
       
  4632 				mod_timer(&adapter->phy_info_timer,
       
  4633 					  round_jiffies(jiffies + 2 * HZ));
       
  4634 		}
       
  4635 	} else {
       
  4636 		if ((adapter->ecdev && ecdev_get_link(adapter->ecdev))
       
  4637 				|| (!adapter->ecdev && netif_carrier_ok(netdev))) {
       
  4638 			adapter->link_speed = 0;
       
  4639 			adapter->link_duplex = 0;
       
  4640 			/* Link status message must follow this format */
       
  4641 			printk(KERN_INFO "e1000e: %s NIC Link is Down\n",
       
  4642 			       adapter->netdev->name);
       
  4643 			if (adapter->ecdev)
       
  4644 				ecdev_set_link(adapter->ecdev, 0);
       
  4645 			else
       
  4646 				netif_carrier_off(netdev);
       
  4647 			if (!adapter->ecdev && !test_bit(__E1000_DOWN, &adapter->state))
       
  4648 				mod_timer(&adapter->phy_info_timer,
       
  4649 					  round_jiffies(jiffies + 2 * HZ));
       
  4650 
       
  4651 			if (adapter->flags & FLAG_RX_NEEDS_RESTART)
       
  4652 				schedule_work(&adapter->reset_task);
       
  4653 			else
       
  4654 				pm_schedule_suspend(netdev->dev.parent,
       
  4655 							LINK_TIMEOUT);
       
  4656 		}
       
  4657 	}
       
  4658 
       
  4659 link_up:
       
  4660 	spin_lock(&adapter->stats64_lock);
       
  4661 	e1000e_update_stats(adapter);
       
  4662 
       
  4663 	mac->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
       
  4664 	adapter->tpt_old = adapter->stats.tpt;
       
  4665 	mac->collision_delta = adapter->stats.colc - adapter->colc_old;
       
  4666 	adapter->colc_old = adapter->stats.colc;
       
  4667 
       
  4668 	adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
       
  4669 	adapter->gorc_old = adapter->stats.gorc;
       
  4670 	adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
       
  4671 	adapter->gotc_old = adapter->stats.gotc;
       
  4672 	spin_unlock(&adapter->stats64_lock);
       
  4673 
       
  4674 	e1000e_update_adaptive(&adapter->hw);
       
  4675 
       
  4676 	if ((adapter->ecdev && !ecdev_get_link(adapter->ecdev))
       
  4677 			|| (!adapter->ecdev && (!netif_carrier_ok(netdev) &&
       
  4678 	    	(e1000_desc_unused(tx_ring) + 1 < tx_ring->count)))) {
       
  4679 		/*
       
  4680 		 * We've lost link, so the controller stops DMA,
       
  4681 		 * but we've got queued Tx work that's never going
       
  4682 		 * to get done, so reset controller to flush Tx.
       
  4683 		 * (Do the reset outside of interrupt context).
       
  4684 		 */
       
  4685 		schedule_work(&adapter->reset_task);
       
  4686 		/* return immediately since reset is imminent */
       
  4687 		return;
       
  4688 	}
       
  4689 
       
  4690 	/* Simple mode for Interrupt Throttle Rate (ITR) */
       
  4691 	if (adapter->itr_setting == 4) {
       
  4692 		/*
       
  4693 		 * Symmetric Tx/Rx gets a reduced ITR=2000;
       
  4694 		 * Total asymmetrical Tx or Rx gets ITR=8000;
       
  4695 		 * everyone else is between 2000-8000.
       
  4696 		 */
       
  4697 		u32 goc = (adapter->gotc + adapter->gorc) / 10000;
       
  4698 		u32 dif = (adapter->gotc > adapter->gorc ?
       
  4699 			    adapter->gotc - adapter->gorc :
       
  4700 			    adapter->gorc - adapter->gotc) / 10000;
       
  4701 		u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
       
  4702 
       
  4703 		ew32(ITR, 1000000000 / (itr * 256));
       
  4704 	}
       
  4705 
       
  4706 	/* Cause software interrupt to ensure Rx ring is cleaned */
       
  4707 	if (adapter->msix_entries)
       
  4708 		ew32(ICS, adapter->rx_ring->ims_val);
       
  4709 	else
       
  4710 		ew32(ICS, E1000_ICS_RXDMT0);
       
  4711 
       
  4712 	/* flush pending descriptors to memory before detecting Tx hang */
       
  4713 	e1000e_flush_descriptors(adapter);
       
  4714 
       
  4715 	/* Force detection of hung controller every watchdog period */
       
  4716 	adapter->detect_tx_hung = true;
       
  4717 
       
  4718 	/*
       
  4719 	 * With 82571 controllers, LAA may be overwritten due to controller
       
  4720 	 * reset from the other port. Set the appropriate LAA in RAR[0]
       
  4721 	 */
       
  4722 	if (e1000e_get_laa_state_82571(hw))
       
  4723 		e1000e_rar_set(hw, adapter->hw.mac.addr, 0);
       
  4724 
       
  4725 	if (adapter->flags2 & FLAG2_CHECK_PHY_HANG)
       
  4726 		e1000e_check_82574_phy_workaround(adapter);
       
  4727 
       
  4728 	/* Reset the timer */
       
  4729 	if (!adapter->ecdev && !test_bit(__E1000_DOWN, &adapter->state))
       
  4730 		mod_timer(&adapter->watchdog_timer,
       
  4731 			  round_jiffies(jiffies + 2 * HZ));
       
  4732 }
       
  4733 
       
  4734 #define E1000_TX_FLAGS_CSUM		0x00000001
       
  4735 #define E1000_TX_FLAGS_VLAN		0x00000002
       
  4736 #define E1000_TX_FLAGS_TSO		0x00000004
       
  4737 #define E1000_TX_FLAGS_IPV4		0x00000008
       
  4738 #define E1000_TX_FLAGS_NO_FCS		0x00000010
       
  4739 #define E1000_TX_FLAGS_VLAN_MASK	0xffff0000
       
  4740 #define E1000_TX_FLAGS_VLAN_SHIFT	16
       
  4741 
       
  4742 static int e1000_tso(struct e1000_ring *tx_ring, struct sk_buff *skb)
       
  4743 {
       
  4744 	struct e1000_context_desc *context_desc;
       
  4745 	struct e1000_buffer *buffer_info;
       
  4746 	unsigned int i;
       
  4747 	u32 cmd_length = 0;
       
  4748 	u16 ipcse = 0, tucse, mss;
       
  4749 	u8 ipcss, ipcso, tucss, tucso, hdr_len;
       
  4750 
       
  4751 	if (!skb_is_gso(skb))
       
  4752 		return 0;
       
  4753 
       
  4754 	if (skb_header_cloned(skb)) {
       
  4755 		int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
       
  4756 
       
  4757 		if (err)
       
  4758 			return err;
       
  4759 	}
       
  4760 
       
  4761 	hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
       
  4762 	mss = skb_shinfo(skb)->gso_size;
       
  4763 	if (skb->protocol == htons(ETH_P_IP)) {
       
  4764 		struct iphdr *iph = ip_hdr(skb);
       
  4765 		iph->tot_len = 0;
       
  4766 		iph->check = 0;
       
  4767 		tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
       
  4768 		                                         0, IPPROTO_TCP, 0);
       
  4769 		cmd_length = E1000_TXD_CMD_IP;
       
  4770 		ipcse = skb_transport_offset(skb) - 1;
       
  4771 	} else if (skb_is_gso_v6(skb)) {
       
  4772 		ipv6_hdr(skb)->payload_len = 0;
       
  4773 		tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
       
  4774 		                                       &ipv6_hdr(skb)->daddr,
       
  4775 		                                       0, IPPROTO_TCP, 0);
       
  4776 		ipcse = 0;
       
  4777 	}
       
  4778 	ipcss = skb_network_offset(skb);
       
  4779 	ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
       
  4780 	tucss = skb_transport_offset(skb);
       
  4781 	tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
       
  4782 	tucse = 0;
       
  4783 
       
  4784 	cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
       
  4785 	               E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
       
  4786 
       
  4787 	i = tx_ring->next_to_use;
       
  4788 	context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
       
  4789 	buffer_info = &tx_ring->buffer_info[i];
       
  4790 
       
  4791 	context_desc->lower_setup.ip_fields.ipcss  = ipcss;
       
  4792 	context_desc->lower_setup.ip_fields.ipcso  = ipcso;
       
  4793 	context_desc->lower_setup.ip_fields.ipcse  = cpu_to_le16(ipcse);
       
  4794 	context_desc->upper_setup.tcp_fields.tucss = tucss;
       
  4795 	context_desc->upper_setup.tcp_fields.tucso = tucso;
       
  4796 	context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
       
  4797 	context_desc->tcp_seg_setup.fields.mss     = cpu_to_le16(mss);
       
  4798 	context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
       
  4799 	context_desc->cmd_and_length = cpu_to_le32(cmd_length);
       
  4800 
       
  4801 	buffer_info->time_stamp = jiffies;
       
  4802 	buffer_info->next_to_watch = i;
       
  4803 
       
  4804 	i++;
       
  4805 	if (i == tx_ring->count)
       
  4806 		i = 0;
       
  4807 	tx_ring->next_to_use = i;
       
  4808 
       
  4809 	return 1;
       
  4810 }
       
  4811 
       
  4812 static bool e1000_tx_csum(struct e1000_ring *tx_ring, struct sk_buff *skb)
       
  4813 {
       
  4814 	struct e1000_adapter *adapter = tx_ring->adapter;
       
  4815 	struct e1000_context_desc *context_desc;
       
  4816 	struct e1000_buffer *buffer_info;
       
  4817 	unsigned int i;
       
  4818 	u8 css;
       
  4819 	u32 cmd_len = E1000_TXD_CMD_DEXT;
       
  4820 	__be16 protocol;
       
  4821 
       
  4822 	if (skb->ip_summed != CHECKSUM_PARTIAL)
       
  4823 		return 0;
       
  4824 
       
  4825 	if (skb->protocol == cpu_to_be16(ETH_P_8021Q))
       
  4826 		protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
       
  4827 	else
       
  4828 		protocol = skb->protocol;
       
  4829 
       
  4830 	switch (protocol) {
       
  4831 	case cpu_to_be16(ETH_P_IP):
       
  4832 		if (ip_hdr(skb)->protocol == IPPROTO_TCP)
       
  4833 			cmd_len |= E1000_TXD_CMD_TCP;
       
  4834 		break;
       
  4835 	case cpu_to_be16(ETH_P_IPV6):
       
  4836 		/* XXX not handling all IPV6 headers */
       
  4837 		if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
       
  4838 			cmd_len |= E1000_TXD_CMD_TCP;
       
  4839 		break;
       
  4840 	default:
       
  4841 		if (unlikely(net_ratelimit()))
       
  4842 			e_warn("checksum_partial proto=%x!\n",
       
  4843 			       be16_to_cpu(protocol));
       
  4844 		break;
       
  4845 	}
       
  4846 
       
  4847 	css = skb_checksum_start_offset(skb);
       
  4848 
       
  4849 	i = tx_ring->next_to_use;
       
  4850 	buffer_info = &tx_ring->buffer_info[i];
       
  4851 	context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
       
  4852 
       
  4853 	context_desc->lower_setup.ip_config = 0;
       
  4854 	context_desc->upper_setup.tcp_fields.tucss = css;
       
  4855 	context_desc->upper_setup.tcp_fields.tucso =
       
  4856 				css + skb->csum_offset;
       
  4857 	context_desc->upper_setup.tcp_fields.tucse = 0;
       
  4858 	context_desc->tcp_seg_setup.data = 0;
       
  4859 	context_desc->cmd_and_length = cpu_to_le32(cmd_len);
       
  4860 
       
  4861 	buffer_info->time_stamp = jiffies;
       
  4862 	buffer_info->next_to_watch = i;
       
  4863 
       
  4864 	i++;
       
  4865 	if (i == tx_ring->count)
       
  4866 		i = 0;
       
  4867 	tx_ring->next_to_use = i;
       
  4868 
       
  4869 	return 1;
       
  4870 }
       
  4871 
       
  4872 #define E1000_MAX_PER_TXD	8192
       
  4873 #define E1000_MAX_TXD_PWR	12
       
  4874 
       
  4875 static int e1000_tx_map(struct e1000_ring *tx_ring, struct sk_buff *skb,
       
  4876 			unsigned int first, unsigned int max_per_txd,
       
  4877 			unsigned int nr_frags, unsigned int mss)
       
  4878 {
       
  4879 	struct e1000_adapter *adapter = tx_ring->adapter;
       
  4880 	struct pci_dev *pdev = adapter->pdev;
       
  4881 	struct e1000_buffer *buffer_info;
       
  4882 	unsigned int len = skb_headlen(skb);
       
  4883 	unsigned int offset = 0, size, count = 0, i;
       
  4884 	unsigned int f, bytecount, segs;
       
  4885 
       
  4886 	i = tx_ring->next_to_use;
       
  4887 
       
  4888 	while (len) {
       
  4889 		buffer_info = &tx_ring->buffer_info[i];
       
  4890 		size = min(len, max_per_txd);
       
  4891 
       
  4892 		buffer_info->length = size;
       
  4893 		buffer_info->time_stamp = jiffies;
       
  4894 		buffer_info->next_to_watch = i;
       
  4895 		buffer_info->dma = dma_map_single(&pdev->dev,
       
  4896 						  skb->data + offset,
       
  4897 						  size, DMA_TO_DEVICE);
       
  4898 		buffer_info->mapped_as_page = false;
       
  4899 		if (dma_mapping_error(&pdev->dev, buffer_info->dma))
       
  4900 			goto dma_error;
       
  4901 
       
  4902 		len -= size;
       
  4903 		offset += size;
       
  4904 		count++;
       
  4905 
       
  4906 		if (len) {
       
  4907 			i++;
       
  4908 			if (i == tx_ring->count)
       
  4909 				i = 0;
       
  4910 		}
       
  4911 	}
       
  4912 
       
  4913 	for (f = 0; f < nr_frags; f++) {
       
  4914 		const struct skb_frag_struct *frag;
       
  4915 
       
  4916 		frag = &skb_shinfo(skb)->frags[f];
       
  4917 		len = skb_frag_size(frag);
       
  4918 		offset = 0;
       
  4919 
       
  4920 		while (len) {
       
  4921 			i++;
       
  4922 			if (i == tx_ring->count)
       
  4923 				i = 0;
       
  4924 
       
  4925 			buffer_info = &tx_ring->buffer_info[i];
       
  4926 			size = min(len, max_per_txd);
       
  4927 
       
  4928 			buffer_info->length = size;
       
  4929 			buffer_info->time_stamp = jiffies;
       
  4930 			buffer_info->next_to_watch = i;
       
  4931 			buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
       
  4932 						offset, size, DMA_TO_DEVICE);
       
  4933 			buffer_info->mapped_as_page = true;
       
  4934 			if (dma_mapping_error(&pdev->dev, buffer_info->dma))
       
  4935 				goto dma_error;
       
  4936 
       
  4937 			len -= size;
       
  4938 			offset += size;
       
  4939 			count++;
       
  4940 		}
       
  4941 	}
       
  4942 
       
  4943 	segs = skb_shinfo(skb)->gso_segs ? : 1;
       
  4944 	/* multiply data chunks by size of headers */
       
  4945 	bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
       
  4946 
       
  4947 	tx_ring->buffer_info[i].skb = skb;
       
  4948 	tx_ring->buffer_info[i].segs = segs;
       
  4949 	tx_ring->buffer_info[i].bytecount = bytecount;
       
  4950 	tx_ring->buffer_info[first].next_to_watch = i;
       
  4951 
       
  4952 	return count;
       
  4953 
       
  4954 dma_error:
       
  4955 	dev_err(&pdev->dev, "Tx DMA map failed\n");
       
  4956 	buffer_info->dma = 0;
       
  4957 	if (count)
       
  4958 		count--;
       
  4959 
       
  4960 	while (count--) {
       
  4961 		if (i == 0)
       
  4962 			i += tx_ring->count;
       
  4963 		i--;
       
  4964 		buffer_info = &tx_ring->buffer_info[i];
       
  4965 		e1000_put_txbuf(tx_ring, buffer_info);
       
  4966 	}
       
  4967 
       
  4968 	return 0;
       
  4969 }
       
  4970 
       
  4971 static void e1000_tx_queue(struct e1000_ring *tx_ring, int tx_flags, int count)
       
  4972 {
       
  4973 	struct e1000_adapter *adapter = tx_ring->adapter;
       
  4974 	struct e1000_tx_desc *tx_desc = NULL;
       
  4975 	struct e1000_buffer *buffer_info;
       
  4976 	u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
       
  4977 	unsigned int i;
       
  4978 
       
  4979 	if (tx_flags & E1000_TX_FLAGS_TSO) {
       
  4980 		txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
       
  4981 			     E1000_TXD_CMD_TSE;
       
  4982 		txd_upper |= E1000_TXD_POPTS_TXSM << 8;
       
  4983 
       
  4984 		if (tx_flags & E1000_TX_FLAGS_IPV4)
       
  4985 			txd_upper |= E1000_TXD_POPTS_IXSM << 8;
       
  4986 	}
       
  4987 
       
  4988 	if (tx_flags & E1000_TX_FLAGS_CSUM) {
       
  4989 		txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
       
  4990 		txd_upper |= E1000_TXD_POPTS_TXSM << 8;
       
  4991 	}
       
  4992 
       
  4993 	if (tx_flags & E1000_TX_FLAGS_VLAN) {
       
  4994 		txd_lower |= E1000_TXD_CMD_VLE;
       
  4995 		txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
       
  4996 	}
       
  4997 
       
  4998 	if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
       
  4999 		txd_lower &= ~(E1000_TXD_CMD_IFCS);
       
  5000 
       
  5001 	i = tx_ring->next_to_use;
       
  5002 
       
  5003 	do {
       
  5004 		buffer_info = &tx_ring->buffer_info[i];
       
  5005 		tx_desc = E1000_TX_DESC(*tx_ring, i);
       
  5006 		tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
       
  5007 		tx_desc->lower.data =
       
  5008 			cpu_to_le32(txd_lower | buffer_info->length);
       
  5009 		tx_desc->upper.data = cpu_to_le32(txd_upper);
       
  5010 
       
  5011 		i++;
       
  5012 		if (i == tx_ring->count)
       
  5013 			i = 0;
       
  5014 	} while (--count > 0);
       
  5015 
       
  5016 	tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
       
  5017 
       
  5018 	/* txd_cmd re-enables FCS, so we'll re-disable it here as desired. */
       
  5019 	if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
       
  5020 		tx_desc->lower.data &= ~(cpu_to_le32(E1000_TXD_CMD_IFCS));
       
  5021 
       
  5022 	/*
       
  5023 	 * Force memory writes to complete before letting h/w
       
  5024 	 * know there are new descriptors to fetch.  (Only
       
  5025 	 * applicable for weak-ordered memory model archs,
       
  5026 	 * such as IA-64).
       
  5027 	 */
       
  5028 	wmb();
       
  5029 
       
  5030 	tx_ring->next_to_use = i;
       
  5031 
       
  5032 	if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
       
  5033 		e1000e_update_tdt_wa(tx_ring, i);
       
  5034 	else
       
  5035 		writel(i, tx_ring->tail);
       
  5036 
       
  5037 	/*
       
  5038 	 * we need this if more than one processor can write to our tail
       
  5039 	 * at a time, it synchronizes IO on IA64/Altix systems
       
  5040 	 */
       
  5041 	mmiowb();
       
  5042 }
       
  5043 
       
  5044 #define MINIMUM_DHCP_PACKET_SIZE 282
       
  5045 static int e1000_transfer_dhcp_info(struct e1000_adapter *adapter,
       
  5046 				    struct sk_buff *skb)
       
  5047 {
       
  5048 	struct e1000_hw *hw =  &adapter->hw;
       
  5049 	u16 length, offset;
       
  5050 
       
  5051 	if (vlan_tx_tag_present(skb)) {
       
  5052 		if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
       
  5053 		    (adapter->hw.mng_cookie.status &
       
  5054 			E1000_MNG_DHCP_COOKIE_STATUS_VLAN)))
       
  5055 			return 0;
       
  5056 	}
       
  5057 
       
  5058 	if (skb->len <= MINIMUM_DHCP_PACKET_SIZE)
       
  5059 		return 0;
       
  5060 
       
  5061 	if (((struct ethhdr *) skb->data)->h_proto != htons(ETH_P_IP))
       
  5062 		return 0;
       
  5063 
       
  5064 	{
       
  5065 		const struct iphdr *ip = (struct iphdr *)((u8 *)skb->data+14);
       
  5066 		struct udphdr *udp;
       
  5067 
       
  5068 		if (ip->protocol != IPPROTO_UDP)
       
  5069 			return 0;
       
  5070 
       
  5071 		udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
       
  5072 		if (ntohs(udp->dest) != 67)
       
  5073 			return 0;
       
  5074 
       
  5075 		offset = (u8 *)udp + 8 - skb->data;
       
  5076 		length = skb->len - offset;
       
  5077 		return e1000e_mng_write_dhcp_info(hw, (u8 *)udp + 8, length);
       
  5078 	}
       
  5079 
       
  5080 	return 0;
       
  5081 }
       
  5082 
       
  5083 static int __e1000_maybe_stop_tx(struct e1000_ring *tx_ring, int size)
       
  5084 {
       
  5085 	struct e1000_adapter *adapter = tx_ring->adapter;
       
  5086 
       
  5087 	netif_stop_queue(adapter->netdev);
       
  5088 	/*
       
  5089 	 * Herbert's original patch had:
       
  5090 	 *  smp_mb__after_netif_stop_queue();
       
  5091 	 * but since that doesn't exist yet, just open code it.
       
  5092 	 */
       
  5093 	smp_mb();
       
  5094 
       
  5095 	/*
       
  5096 	 * We need to check again in a case another CPU has just
       
  5097 	 * made room available.
       
  5098 	 */
       
  5099 	if (e1000_desc_unused(tx_ring) < size)
       
  5100 		return -EBUSY;
       
  5101 
       
  5102 	/* A reprieve! */
       
  5103 	netif_start_queue(adapter->netdev);
       
  5104 	++adapter->restart_queue;
       
  5105 	return 0;
       
  5106 }
       
  5107 
       
  5108 static int e1000_maybe_stop_tx(struct e1000_ring *tx_ring, int size)
       
  5109 {
       
  5110 	if (e1000_desc_unused(tx_ring) >= size)
       
  5111 		return 0;
       
  5112 	return __e1000_maybe_stop_tx(tx_ring, size);
       
  5113 }
       
  5114 
       
  5115 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1)
       
  5116 static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
       
  5117 				    struct net_device *netdev)
       
  5118 {
       
  5119 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5120 	struct e1000_ring *tx_ring = adapter->tx_ring;
       
  5121 	unsigned int first;
       
  5122 	unsigned int max_per_txd = E1000_MAX_PER_TXD;
       
  5123 	unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
       
  5124 	unsigned int tx_flags = 0;
       
  5125 	unsigned int len = skb_headlen(skb);
       
  5126 	unsigned int nr_frags;
       
  5127 	unsigned int mss;
       
  5128 	int count = 0;
       
  5129 	int tso;
       
  5130 	unsigned int f;
       
  5131 
       
  5132 	if (test_bit(__E1000_DOWN, &adapter->state)) {
       
  5133 		if (!adapter->ecdev)
       
  5134 			dev_kfree_skb_any(skb);
       
  5135 		return NETDEV_TX_OK;
       
  5136 	}
       
  5137 
       
  5138 	if (skb->len <= 0) {
       
  5139 		if (!adapter->ecdev)
       
  5140 			dev_kfree_skb_any(skb);
       
  5141 		return NETDEV_TX_OK;
       
  5142 	}
       
  5143 
       
  5144 	mss = skb_shinfo(skb)->gso_size;
       
  5145 	/*
       
  5146 	 * The controller does a simple calculation to
       
  5147 	 * make sure there is enough room in the FIFO before
       
  5148 	 * initiating the DMA for each buffer.  The calc is:
       
  5149 	 * 4 = ceil(buffer len/mss).  To make sure we don't
       
  5150 	 * overrun the FIFO, adjust the max buffer len if mss
       
  5151 	 * drops.
       
  5152 	 */
       
  5153 	if (mss) {
       
  5154 		u8 hdr_len;
       
  5155 		max_per_txd = min(mss << 2, max_per_txd);
       
  5156 		max_txd_pwr = fls(max_per_txd) - 1;
       
  5157 
       
  5158 		/*
       
  5159 		 * TSO Workaround for 82571/2/3 Controllers -- if skb->data
       
  5160 		 * points to just header, pull a few bytes of payload from
       
  5161 		 * frags into skb->data
       
  5162 		 */
       
  5163 		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
       
  5164 		/*
       
  5165 		 * we do this workaround for ES2LAN, but it is un-necessary,
       
  5166 		 * avoiding it could save a lot of cycles
       
  5167 		 */
       
  5168 		if (skb->data_len && (hdr_len == len)) {
       
  5169 			unsigned int pull_size;
       
  5170 
       
  5171 			pull_size = min_t(unsigned int, 4, skb->data_len);
       
  5172 			if (!__pskb_pull_tail(skb, pull_size)) {
       
  5173 				e_err("__pskb_pull_tail failed.\n");
       
  5174 				if (!adapter->ecdev)
       
  5175 					dev_kfree_skb_any(skb);
       
  5176 				return NETDEV_TX_OK;
       
  5177 			}
       
  5178 			len = skb_headlen(skb);
       
  5179 		}
       
  5180 	}
       
  5181 
       
  5182 	/* reserve a descriptor for the offload context */
       
  5183 	if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
       
  5184 		count++;
       
  5185 	count++;
       
  5186 
       
  5187 	count += TXD_USE_COUNT(len, max_txd_pwr);
       
  5188 
       
  5189 	nr_frags = skb_shinfo(skb)->nr_frags;
       
  5190 	for (f = 0; f < nr_frags; f++)
       
  5191 		count += TXD_USE_COUNT(skb_frag_size(&skb_shinfo(skb)->frags[f]),
       
  5192 				       max_txd_pwr);
       
  5193 
       
  5194 	if (adapter->hw.mac.tx_pkt_filtering)
       
  5195 		e1000_transfer_dhcp_info(adapter, skb);
       
  5196 
       
  5197 	/*
       
  5198 	 * need: count + 2 desc gap to keep tail from touching
       
  5199 	 * head, otherwise try next time
       
  5200 	 */
       
  5201 	if (!adapter->ecdev && e1000_maybe_stop_tx(tx_ring, count + 2))
       
  5202 		return NETDEV_TX_BUSY;
       
  5203 
       
  5204 	if (vlan_tx_tag_present(skb)) {
       
  5205 		tx_flags |= E1000_TX_FLAGS_VLAN;
       
  5206 		tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
       
  5207 	}
       
  5208 
       
  5209 	first = tx_ring->next_to_use;
       
  5210 
       
  5211 	tso = e1000_tso(tx_ring, skb);
       
  5212 	if (tso < 0) {
       
  5213 		if (!adapter->ecdev)
       
  5214 			dev_kfree_skb_any(skb);
       
  5215 		return NETDEV_TX_OK;
       
  5216 	}
       
  5217 
       
  5218 	if (tso)
       
  5219 		tx_flags |= E1000_TX_FLAGS_TSO;
       
  5220 	else if (e1000_tx_csum(tx_ring, skb))
       
  5221 		tx_flags |= E1000_TX_FLAGS_CSUM;
       
  5222 
       
  5223 	/*
       
  5224 	 * Old method was to assume IPv4 packet by default if TSO was enabled.
       
  5225 	 * 82571 hardware supports TSO capabilities for IPv6 as well...
       
  5226 	 * no longer assume, we must.
       
  5227 	 */
       
  5228 	if (skb->protocol == htons(ETH_P_IP))
       
  5229 		tx_flags |= E1000_TX_FLAGS_IPV4;
       
  5230 
       
  5231 	if (unlikely(skb->no_fcs))
       
  5232 		tx_flags |= E1000_TX_FLAGS_NO_FCS;
       
  5233 
       
  5234 	/* if count is 0 then mapping error has occurred */
       
  5235 	count = e1000_tx_map(tx_ring, skb, first, max_per_txd, nr_frags, mss);
       
  5236 	if (count) {
       
  5237 		netdev_sent_queue(netdev, skb->len);
       
  5238 		e1000_tx_queue(tx_ring, tx_flags, count);
       
  5239 		/* Make sure there is space in the ring for the next send. */
       
  5240 		if (!adapter->ecdev) {
       
  5241 			e1000_maybe_stop_tx(tx_ring, MAX_SKB_FRAGS + 2);
       
  5242 		}
       
  5243 
       
  5244 	} else {
       
  5245 		if (!adapter->ecdev) {
       
  5246 			dev_kfree_skb_any(skb);
       
  5247 		}
       
  5248 		tx_ring->buffer_info[first].time_stamp = 0;
       
  5249 		tx_ring->next_to_use = first;
       
  5250 	}
       
  5251 
       
  5252 	return NETDEV_TX_OK;
       
  5253 }
       
  5254 
       
  5255 /**
       
  5256  * e1000_tx_timeout - Respond to a Tx Hang
       
  5257  * @netdev: network interface device structure
       
  5258  **/
       
  5259 static void e1000_tx_timeout(struct net_device *netdev)
       
  5260 {
       
  5261 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5262 
       
  5263 	/* Do the reset outside of interrupt context */
       
  5264 	adapter->tx_timeout_count++;
       
  5265 	schedule_work(&adapter->reset_task);
       
  5266 }
       
  5267 
       
  5268 static void e1000_reset_task(struct work_struct *work)
       
  5269 {
       
  5270 	struct e1000_adapter *adapter;
       
  5271 	adapter = container_of(work, struct e1000_adapter, reset_task);
       
  5272 
       
  5273 	/* don't run the task if already down */
       
  5274 	if (test_bit(__E1000_DOWN, &adapter->state))
       
  5275 		return;
       
  5276 
       
  5277 	if (!((adapter->flags & FLAG_RX_NEEDS_RESTART) &&
       
  5278 	      (adapter->flags & FLAG_RX_RESTART_NOW))) {
       
  5279 		e1000e_dump(adapter);
       
  5280 		e_err("Reset adapter\n");
       
  5281 	}
       
  5282 	e1000e_reinit_locked(adapter);
       
  5283 }
       
  5284 
       
  5285 /**
       
  5286  * e1000_get_stats64 - Get System Network Statistics
       
  5287  * @netdev: network interface device structure
       
  5288  * @stats: rtnl_link_stats64 pointer
       
  5289  *
       
  5290  * Returns the address of the device statistics structure.
       
  5291  **/
       
  5292 struct rtnl_link_stats64 *e1000e_get_stats64(struct net_device *netdev,
       
  5293                                              struct rtnl_link_stats64 *stats)
       
  5294 {
       
  5295 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5296 
       
  5297 	memset(stats, 0, sizeof(struct rtnl_link_stats64));
       
  5298 	spin_lock(&adapter->stats64_lock);
       
  5299 	e1000e_update_stats(adapter);
       
  5300 	/* Fill out the OS statistics structure */
       
  5301 	stats->rx_bytes = adapter->stats.gorc;
       
  5302 	stats->rx_packets = adapter->stats.gprc;
       
  5303 	stats->tx_bytes = adapter->stats.gotc;
       
  5304 	stats->tx_packets = adapter->stats.gptc;
       
  5305 	stats->multicast = adapter->stats.mprc;
       
  5306 	stats->collisions = adapter->stats.colc;
       
  5307 
       
  5308 	/* Rx Errors */
       
  5309 
       
  5310 	/*
       
  5311 	 * RLEC on some newer hardware can be incorrect so build
       
  5312 	 * our own version based on RUC and ROC
       
  5313 	 */
       
  5314 	stats->rx_errors = adapter->stats.rxerrc +
       
  5315 		adapter->stats.crcerrs + adapter->stats.algnerrc +
       
  5316 		adapter->stats.ruc + adapter->stats.roc +
       
  5317 		adapter->stats.cexterr;
       
  5318 	stats->rx_length_errors = adapter->stats.ruc +
       
  5319 					      adapter->stats.roc;
       
  5320 	stats->rx_crc_errors = adapter->stats.crcerrs;
       
  5321 	stats->rx_frame_errors = adapter->stats.algnerrc;
       
  5322 	stats->rx_missed_errors = adapter->stats.mpc;
       
  5323 
       
  5324 	/* Tx Errors */
       
  5325 	stats->tx_errors = adapter->stats.ecol +
       
  5326 				       adapter->stats.latecol;
       
  5327 	stats->tx_aborted_errors = adapter->stats.ecol;
       
  5328 	stats->tx_window_errors = adapter->stats.latecol;
       
  5329 	stats->tx_carrier_errors = adapter->stats.tncrs;
       
  5330 
       
  5331 	/* Tx Dropped needs to be maintained elsewhere */
       
  5332 
       
  5333 	spin_unlock(&adapter->stats64_lock);
       
  5334 	return stats;
       
  5335 }
       
  5336 
       
  5337 /**
       
  5338  * e1000_change_mtu - Change the Maximum Transfer Unit
       
  5339  * @netdev: network interface device structure
       
  5340  * @new_mtu: new value for maximum frame size
       
  5341  *
       
  5342  * Returns 0 on success, negative on failure
       
  5343  **/
       
  5344 static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
       
  5345 {
       
  5346 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5347 	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
       
  5348 
       
  5349 	if (adapter->ecdev)
       
  5350 		return -EBUSY;
       
  5351 
       
  5352 	/* Jumbo frame support */
       
  5353 	if ((max_frame > ETH_FRAME_LEN + ETH_FCS_LEN) &&
       
  5354 	    !(adapter->flags & FLAG_HAS_JUMBO_FRAMES)) {
       
  5355 		e_err("Jumbo Frames not supported.\n");
       
  5356 		return -EINVAL;
       
  5357 	}
       
  5358 
       
  5359 	/* Supported frame sizes */
       
  5360 	if ((new_mtu < ETH_ZLEN + ETH_FCS_LEN + VLAN_HLEN) ||
       
  5361 	    (max_frame > adapter->max_hw_frame_size)) {
       
  5362 		e_err("Unsupported MTU setting\n");
       
  5363 		return -EINVAL;
       
  5364 	}
       
  5365 
       
  5366 	/* Jumbo frame workaround on 82579 requires CRC be stripped */
       
  5367 	if ((adapter->hw.mac.type == e1000_pch2lan) &&
       
  5368 	    !(adapter->flags2 & FLAG2_CRC_STRIPPING) &&
       
  5369 	    (new_mtu > ETH_DATA_LEN)) {
       
  5370 		e_err("Jumbo Frames not supported on 82579 when CRC stripping is disabled.\n");
       
  5371 		return -EINVAL;
       
  5372 	}
       
  5373 
       
  5374 	while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
       
  5375 		usleep_range(1000, 2000);
       
  5376 	/* e1000e_down -> e1000e_reset dependent on max_frame_size & mtu */
       
  5377 	adapter->max_frame_size = max_frame;
       
  5378 	e_info("changing MTU from %d to %d\n", netdev->mtu, new_mtu);
       
  5379 	netdev->mtu = new_mtu;
       
  5380 	if (netif_running(netdev))
       
  5381 		e1000e_down(adapter);
       
  5382 
       
  5383 	/*
       
  5384 	 * NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
       
  5385 	 * means we reserve 2 more, this pushes us to allocate from the next
       
  5386 	 * larger slab size.
       
  5387 	 * i.e. RXBUFFER_2048 --> size-4096 slab
       
  5388 	 * However with the new *_jumbo_rx* routines, jumbo receives will use
       
  5389 	 * fragmented skbs
       
  5390 	 */
       
  5391 
       
  5392 	if (max_frame <= 2048)
       
  5393 		adapter->rx_buffer_len = 2048;
       
  5394 	else
       
  5395 		adapter->rx_buffer_len = 4096;
       
  5396 
       
  5397 	/* adjust allocation if LPE protects us, and we aren't using SBP */
       
  5398 	if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
       
  5399 	     (max_frame == ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN))
       
  5400 		adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN
       
  5401 					 + ETH_FCS_LEN;
       
  5402 
       
  5403 	if (netif_running(netdev))
       
  5404 		e1000e_up(adapter);
       
  5405 	else
       
  5406 		e1000e_reset(adapter);
       
  5407 
       
  5408 	clear_bit(__E1000_RESETTING, &adapter->state);
       
  5409 
       
  5410 	return 0;
       
  5411 }
       
  5412 
       
  5413 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
       
  5414 			   int cmd)
       
  5415 {
       
  5416 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5417 	struct mii_ioctl_data *data = if_mii(ifr);
       
  5418 
       
  5419 	if (adapter->hw.phy.media_type != e1000_media_type_copper)
       
  5420 		return -EOPNOTSUPP;
       
  5421 
       
  5422 	switch (cmd) {
       
  5423 	case SIOCGMIIPHY:
       
  5424 		data->phy_id = adapter->hw.phy.addr;
       
  5425 		break;
       
  5426 	case SIOCGMIIREG:
       
  5427 		e1000_phy_read_status(adapter);
       
  5428 
       
  5429 		switch (data->reg_num & 0x1F) {
       
  5430 		case MII_BMCR:
       
  5431 			data->val_out = adapter->phy_regs.bmcr;
       
  5432 			break;
       
  5433 		case MII_BMSR:
       
  5434 			data->val_out = adapter->phy_regs.bmsr;
       
  5435 			break;
       
  5436 		case MII_PHYSID1:
       
  5437 			data->val_out = (adapter->hw.phy.id >> 16);
       
  5438 			break;
       
  5439 		case MII_PHYSID2:
       
  5440 			data->val_out = (adapter->hw.phy.id & 0xFFFF);
       
  5441 			break;
       
  5442 		case MII_ADVERTISE:
       
  5443 			data->val_out = adapter->phy_regs.advertise;
       
  5444 			break;
       
  5445 		case MII_LPA:
       
  5446 			data->val_out = adapter->phy_regs.lpa;
       
  5447 			break;
       
  5448 		case MII_EXPANSION:
       
  5449 			data->val_out = adapter->phy_regs.expansion;
       
  5450 			break;
       
  5451 		case MII_CTRL1000:
       
  5452 			data->val_out = adapter->phy_regs.ctrl1000;
       
  5453 			break;
       
  5454 		case MII_STAT1000:
       
  5455 			data->val_out = adapter->phy_regs.stat1000;
       
  5456 			break;
       
  5457 		case MII_ESTATUS:
       
  5458 			data->val_out = adapter->phy_regs.estatus;
       
  5459 			break;
       
  5460 		default:
       
  5461 			return -EIO;
       
  5462 		}
       
  5463 		break;
       
  5464 	case SIOCSMIIREG:
       
  5465 	default:
       
  5466 		return -EOPNOTSUPP;
       
  5467 	}
       
  5468 	return 0;
       
  5469 }
       
  5470 
       
  5471 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
       
  5472 {
       
  5473 	switch (cmd) {
       
  5474 	case SIOCGMIIPHY:
       
  5475 	case SIOCGMIIREG:
       
  5476 	case SIOCSMIIREG:
       
  5477 		return e1000_mii_ioctl(netdev, ifr, cmd);
       
  5478 	default:
       
  5479 		return -EOPNOTSUPP;
       
  5480 	}
       
  5481 }
       
  5482 
       
  5483 static int e1000_init_phy_wakeup(struct e1000_adapter *adapter, u32 wufc)
       
  5484 {
       
  5485 	struct e1000_hw *hw = &adapter->hw;
       
  5486 	u32 i, mac_reg;
       
  5487 	u16 phy_reg, wuc_enable;
       
  5488 	int retval = 0;
       
  5489 
       
  5490 	/* copy MAC RARs to PHY RARs */
       
  5491 	e1000_copy_rx_addrs_to_phy_ich8lan(hw);
       
  5492 
       
  5493 	retval = hw->phy.ops.acquire(hw);
       
  5494 	if (retval) {
       
  5495 		e_err("Could not acquire PHY\n");
       
  5496 		return retval;
       
  5497 	}
       
  5498 
       
  5499 	/* Enable access to wakeup registers on and set page to BM_WUC_PAGE */
       
  5500 	retval = e1000_enable_phy_wakeup_reg_access_bm(hw, &wuc_enable);
       
  5501 	if (retval)
       
  5502 		goto release;
       
  5503 
       
  5504 	/* copy MAC MTA to PHY MTA - only needed for pchlan */
       
  5505 	for (i = 0; i < adapter->hw.mac.mta_reg_count; i++) {
       
  5506 		mac_reg = E1000_READ_REG_ARRAY(hw, E1000_MTA, i);
       
  5507 		hw->phy.ops.write_reg_page(hw, BM_MTA(i),
       
  5508 					   (u16)(mac_reg & 0xFFFF));
       
  5509 		hw->phy.ops.write_reg_page(hw, BM_MTA(i) + 1,
       
  5510 					   (u16)((mac_reg >> 16) & 0xFFFF));
       
  5511 	}
       
  5512 
       
  5513 	/* configure PHY Rx Control register */
       
  5514 	hw->phy.ops.read_reg_page(&adapter->hw, BM_RCTL, &phy_reg);
       
  5515 	mac_reg = er32(RCTL);
       
  5516 	if (mac_reg & E1000_RCTL_UPE)
       
  5517 		phy_reg |= BM_RCTL_UPE;
       
  5518 	if (mac_reg & E1000_RCTL_MPE)
       
  5519 		phy_reg |= BM_RCTL_MPE;
       
  5520 	phy_reg &= ~(BM_RCTL_MO_MASK);
       
  5521 	if (mac_reg & E1000_RCTL_MO_3)
       
  5522 		phy_reg |= (((mac_reg & E1000_RCTL_MO_3) >> E1000_RCTL_MO_SHIFT)
       
  5523 				<< BM_RCTL_MO_SHIFT);
       
  5524 	if (mac_reg & E1000_RCTL_BAM)
       
  5525 		phy_reg |= BM_RCTL_BAM;
       
  5526 	if (mac_reg & E1000_RCTL_PMCF)
       
  5527 		phy_reg |= BM_RCTL_PMCF;
       
  5528 	mac_reg = er32(CTRL);
       
  5529 	if (mac_reg & E1000_CTRL_RFCE)
       
  5530 		phy_reg |= BM_RCTL_RFCE;
       
  5531 	hw->phy.ops.write_reg_page(&adapter->hw, BM_RCTL, phy_reg);
       
  5532 
       
  5533 	/* enable PHY wakeup in MAC register */
       
  5534 	ew32(WUFC, wufc);
       
  5535 	ew32(WUC, E1000_WUC_PHY_WAKE | E1000_WUC_PME_EN);
       
  5536 
       
  5537 	/* configure and enable PHY wakeup in PHY registers */
       
  5538 	hw->phy.ops.write_reg_page(&adapter->hw, BM_WUFC, wufc);
       
  5539 	hw->phy.ops.write_reg_page(&adapter->hw, BM_WUC, E1000_WUC_PME_EN);
       
  5540 
       
  5541 	/* activate PHY wakeup */
       
  5542 	wuc_enable |= BM_WUC_ENABLE_BIT | BM_WUC_HOST_WU_BIT;
       
  5543 	retval = e1000_disable_phy_wakeup_reg_access_bm(hw, &wuc_enable);
       
  5544 	if (retval)
       
  5545 		e_err("Could not set PHY Host Wakeup bit\n");
       
  5546 release:
       
  5547 	hw->phy.ops.release(hw);
       
  5548 
       
  5549 	return retval;
       
  5550 }
       
  5551 
       
  5552 static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake,
       
  5553 			    bool runtime)
       
  5554 {
       
  5555 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5556 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5557 	struct e1000_hw *hw = &adapter->hw;
       
  5558 	u32 ctrl, ctrl_ext, rctl, status;
       
  5559 	/* Runtime suspend should only enable wakeup for link changes */
       
  5560 	u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
       
  5561 	int retval = 0;
       
  5562 
       
  5563 	netif_device_detach(netdev);
       
  5564 
       
  5565 	if (netif_running(netdev)) {
       
  5566 		int count = E1000_CHECK_RESET_COUNT;
       
  5567 
       
  5568 		while (test_bit(__E1000_RESETTING, &adapter->state) && count--)
       
  5569 			usleep_range(10000, 20000);
       
  5570 
       
  5571 		WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
       
  5572 		e1000e_down(adapter);
       
  5573 		e1000_free_irq(adapter);
       
  5574 	}
       
  5575 	e1000e_reset_interrupt_capability(adapter);
       
  5576 
       
  5577 	retval = pci_save_state(pdev);
       
  5578 	if (retval)
       
  5579 		return retval;
       
  5580 
       
  5581 	status = er32(STATUS);
       
  5582 	if (status & E1000_STATUS_LU)
       
  5583 		wufc &= ~E1000_WUFC_LNKC;
       
  5584 
       
  5585 	if (wufc) {
       
  5586 		e1000_setup_rctl(adapter);
       
  5587 		e1000e_set_rx_mode(netdev);
       
  5588 
       
  5589 		/* turn on all-multi mode if wake on multicast is enabled */
       
  5590 		if (wufc & E1000_WUFC_MC) {
       
  5591 			rctl = er32(RCTL);
       
  5592 			rctl |= E1000_RCTL_MPE;
       
  5593 			ew32(RCTL, rctl);
       
  5594 		}
       
  5595 
       
  5596 		ctrl = er32(CTRL);
       
  5597 		/* advertise wake from D3Cold */
       
  5598 		#define E1000_CTRL_ADVD3WUC 0x00100000
       
  5599 		/* phy power management enable */
       
  5600 		#define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
       
  5601 		ctrl |= E1000_CTRL_ADVD3WUC;
       
  5602 		if (!(adapter->flags2 & FLAG2_HAS_PHY_WAKEUP))
       
  5603 			ctrl |= E1000_CTRL_EN_PHY_PWR_MGMT;
       
  5604 		ew32(CTRL, ctrl);
       
  5605 
       
  5606 		if (adapter->hw.phy.media_type == e1000_media_type_fiber ||
       
  5607 		    adapter->hw.phy.media_type ==
       
  5608 		    e1000_media_type_internal_serdes) {
       
  5609 			/* keep the laser running in D3 */
       
  5610 			ctrl_ext = er32(CTRL_EXT);
       
  5611 			ctrl_ext |= E1000_CTRL_EXT_SDP3_DATA;
       
  5612 			ew32(CTRL_EXT, ctrl_ext);
       
  5613 		}
       
  5614 
       
  5615 		if (adapter->flags & FLAG_IS_ICH)
       
  5616 			e1000_suspend_workarounds_ich8lan(&adapter->hw);
       
  5617 
       
  5618 		/* Allow time for pending master requests to run */
       
  5619 		e1000e_disable_pcie_master(&adapter->hw);
       
  5620 
       
  5621 		if (adapter->flags2 & FLAG2_HAS_PHY_WAKEUP) {
       
  5622 			/* enable wakeup by the PHY */
       
  5623 			retval = e1000_init_phy_wakeup(adapter, wufc);
       
  5624 			if (retval)
       
  5625 				return retval;
       
  5626 		} else {
       
  5627 			/* enable wakeup by the MAC */
       
  5628 			ew32(WUFC, wufc);
       
  5629 			ew32(WUC, E1000_WUC_PME_EN);
       
  5630 		}
       
  5631 	} else {
       
  5632 		ew32(WUC, 0);
       
  5633 		ew32(WUFC, 0);
       
  5634 	}
       
  5635 
       
  5636 	*enable_wake = !!wufc;
       
  5637 
       
  5638 	/* make sure adapter isn't asleep if manageability is enabled */
       
  5639 	if ((adapter->flags & FLAG_MNG_PT_ENABLED) ||
       
  5640 	    (hw->mac.ops.check_mng_mode(hw)))
       
  5641 		*enable_wake = true;
       
  5642 
       
  5643 	if (adapter->hw.phy.type == e1000_phy_igp_3)
       
  5644 		e1000e_igp3_phy_powerdown_workaround_ich8lan(&adapter->hw);
       
  5645 
       
  5646 	/*
       
  5647 	 * Release control of h/w to f/w.  If f/w is AMT enabled, this
       
  5648 	 * would have already happened in close and is redundant.
       
  5649 	 */
       
  5650 	e1000e_release_hw_control(adapter);
       
  5651 
       
  5652 	pci_disable_device(pdev);
       
  5653 
       
  5654 	return 0;
       
  5655 }
       
  5656 
       
  5657 static void e1000_power_off(struct pci_dev *pdev, bool sleep, bool wake)
       
  5658 {
       
  5659 	if (sleep && wake) {
       
  5660 		pci_prepare_to_sleep(pdev);
       
  5661 		return;
       
  5662 	}
       
  5663 
       
  5664 	pci_wake_from_d3(pdev, wake);
       
  5665 	pci_set_power_state(pdev, PCI_D3hot);
       
  5666 }
       
  5667 
       
  5668 static void e1000_complete_shutdown(struct pci_dev *pdev, bool sleep,
       
  5669                                     bool wake)
       
  5670 {
       
  5671 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5672 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5673 
       
  5674 	/*
       
  5675 	 * The pci-e switch on some quad port adapters will report a
       
  5676 	 * correctable error when the MAC transitions from D0 to D3.  To
       
  5677 	 * prevent this we need to mask off the correctable errors on the
       
  5678 	 * downstream port of the pci-e switch.
       
  5679 	 */
       
  5680 	if (adapter->flags & FLAG_IS_QUAD_PORT) {
       
  5681 		struct pci_dev *us_dev = pdev->bus->self;
       
  5682 		int pos = pci_pcie_cap(us_dev);
       
  5683 		u16 devctl;
       
  5684 
       
  5685 		pci_read_config_word(us_dev, pos + PCI_EXP_DEVCTL, &devctl);
       
  5686 		pci_write_config_word(us_dev, pos + PCI_EXP_DEVCTL,
       
  5687 		                      (devctl & ~PCI_EXP_DEVCTL_CERE));
       
  5688 
       
  5689 		e1000_power_off(pdev, sleep, wake);
       
  5690 
       
  5691 		pci_write_config_word(us_dev, pos + PCI_EXP_DEVCTL, devctl);
       
  5692 	} else {
       
  5693 		e1000_power_off(pdev, sleep, wake);
       
  5694 	}
       
  5695 }
       
  5696 
       
  5697 #ifdef CONFIG_PCIEASPM
       
  5698 static void __e1000e_disable_aspm(struct pci_dev *pdev, u16 state)
       
  5699 {
       
  5700 	pci_disable_link_state_locked(pdev, state);
       
  5701 }
       
  5702 #else
       
  5703 static void __e1000e_disable_aspm(struct pci_dev *pdev, u16 state)
       
  5704 {
       
  5705 	int pos;
       
  5706 	u16 reg16;
       
  5707 
       
  5708 	/*
       
  5709 	 * Both device and parent should have the same ASPM setting.
       
  5710 	 * Disable ASPM in downstream component first and then upstream.
       
  5711 	 */
       
  5712 	pos = pci_pcie_cap(pdev);
       
  5713 	pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
       
  5714 	reg16 &= ~state;
       
  5715 	pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
       
  5716 
       
  5717 	if (!pdev->bus->self)
       
  5718 		return;
       
  5719 
       
  5720 	pos = pci_pcie_cap(pdev->bus->self);
       
  5721 	pci_read_config_word(pdev->bus->self, pos + PCI_EXP_LNKCTL, &reg16);
       
  5722 	reg16 &= ~state;
       
  5723 	pci_write_config_word(pdev->bus->self, pos + PCI_EXP_LNKCTL, reg16);
       
  5724 }
       
  5725 #endif
       
  5726 static void e1000e_disable_aspm(struct pci_dev *pdev, u16 state)
       
  5727 {
       
  5728 	dev_info(&pdev->dev, "Disabling ASPM %s %s\n",
       
  5729 		 (state & PCIE_LINK_STATE_L0S) ? "L0s" : "",
       
  5730 		 (state & PCIE_LINK_STATE_L1) ? "L1" : "");
       
  5731 
       
  5732 	__e1000e_disable_aspm(pdev, state);
       
  5733 }
       
  5734 
       
  5735 #ifdef CONFIG_PM
       
  5736 static bool e1000e_pm_ready(struct e1000_adapter *adapter)
       
  5737 {
       
  5738 	return !!adapter->tx_ring->buffer_info;
       
  5739 }
       
  5740 
       
  5741 static int __e1000_resume(struct pci_dev *pdev)
       
  5742 {
       
  5743 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5744 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5745 	struct e1000_hw *hw = &adapter->hw;
       
  5746 	u16 aspm_disable_flag = 0;
       
  5747 	u32 err;
       
  5748 
       
  5749 	if (adapter->ecdev)
       
  5750 		return -EBUSY;
       
  5751 
       
  5752 	if (adapter->flags2 & FLAG2_DISABLE_ASPM_L0S)
       
  5753 		aspm_disable_flag = PCIE_LINK_STATE_L0S;
       
  5754 	if (adapter->flags2 & FLAG2_DISABLE_ASPM_L1)
       
  5755 		aspm_disable_flag |= PCIE_LINK_STATE_L1;
       
  5756 	if (aspm_disable_flag)
       
  5757 		e1000e_disable_aspm(pdev, aspm_disable_flag);
       
  5758 
       
  5759 	pci_set_power_state(pdev, PCI_D0);
       
  5760 	pci_restore_state(pdev);
       
  5761 	pci_save_state(pdev);
       
  5762 
       
  5763 	e1000e_set_interrupt_capability(adapter);
       
  5764 	if (netif_running(netdev)) {
       
  5765 		err = e1000_request_irq(adapter);
       
  5766 		if (err)
       
  5767 			return err;
       
  5768 	}
       
  5769 
       
  5770 	if (hw->mac.type == e1000_pch2lan)
       
  5771 		e1000_resume_workarounds_pchlan(&adapter->hw);
       
  5772 
       
  5773 	e1000e_power_up_phy(adapter);
       
  5774 
       
  5775 	/* report the system wakeup cause from S3/S4 */
       
  5776 	if (adapter->flags2 & FLAG2_HAS_PHY_WAKEUP) {
       
  5777 		u16 phy_data;
       
  5778 
       
  5779 		e1e_rphy(&adapter->hw, BM_WUS, &phy_data);
       
  5780 		if (phy_data) {
       
  5781 			e_info("PHY Wakeup cause - %s\n",
       
  5782 				phy_data & E1000_WUS_EX ? "Unicast Packet" :
       
  5783 				phy_data & E1000_WUS_MC ? "Multicast Packet" :
       
  5784 				phy_data & E1000_WUS_BC ? "Broadcast Packet" :
       
  5785 				phy_data & E1000_WUS_MAG ? "Magic Packet" :
       
  5786 				phy_data & E1000_WUS_LNKC ?
       
  5787 				"Link Status Change" : "other");
       
  5788 		}
       
  5789 		e1e_wphy(&adapter->hw, BM_WUS, ~0);
       
  5790 	} else {
       
  5791 		u32 wus = er32(WUS);
       
  5792 		if (wus) {
       
  5793 			e_info("MAC Wakeup cause - %s\n",
       
  5794 				wus & E1000_WUS_EX ? "Unicast Packet" :
       
  5795 				wus & E1000_WUS_MC ? "Multicast Packet" :
       
  5796 				wus & E1000_WUS_BC ? "Broadcast Packet" :
       
  5797 				wus & E1000_WUS_MAG ? "Magic Packet" :
       
  5798 				wus & E1000_WUS_LNKC ? "Link Status Change" :
       
  5799 				"other");
       
  5800 		}
       
  5801 		ew32(WUS, ~0);
       
  5802 	}
       
  5803 
       
  5804 	e1000e_reset(adapter);
       
  5805 
       
  5806 	e1000_init_manageability_pt(adapter);
       
  5807 
       
  5808 	if (netif_running(netdev))
       
  5809 		e1000e_up(adapter);
       
  5810 
       
  5811 	netif_device_attach(netdev);
       
  5812 
       
  5813 	/*
       
  5814 	 * If the controller has AMT, do not set DRV_LOAD until the interface
       
  5815 	 * is up.  For all other cases, let the f/w know that the h/w is now
       
  5816 	 * under the control of the driver.
       
  5817 	 */
       
  5818 	if (!(adapter->flags & FLAG_HAS_AMT))
       
  5819 		e1000e_get_hw_control(adapter);
       
  5820 
       
  5821 	return 0;
       
  5822 }
       
  5823 
       
  5824 #ifdef CONFIG_PM_SLEEP
       
  5825 static int e1000_suspend(struct device *dev)
       
  5826 {
       
  5827 	struct pci_dev *pdev = to_pci_dev(dev);
       
  5828 	int retval;
       
  5829 	bool wake;
       
  5830 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5831 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5832 
       
  5833 	if (adapter->ecdev)
       
  5834 		return -EBUSY;
       
  5835 
       
  5836 	retval = __e1000_shutdown(pdev, &wake, false);
       
  5837 	if (!retval)
       
  5838 		e1000_complete_shutdown(pdev, true, wake);
       
  5839 
       
  5840 	return retval;
       
  5841 }
       
  5842 
       
  5843 static int e1000_resume(struct device *dev)
       
  5844 {
       
  5845 	struct pci_dev *pdev = to_pci_dev(dev);
       
  5846 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5847 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5848 
       
  5849 	if (e1000e_pm_ready(adapter))
       
  5850 		adapter->idle_check = true;
       
  5851 
       
  5852 	return __e1000_resume(pdev);
       
  5853 }
       
  5854 #endif /* CONFIG_PM_SLEEP */
       
  5855 
       
  5856 #ifdef CONFIG_PM_RUNTIME
       
  5857 static int e1000_runtime_suspend(struct device *dev)
       
  5858 {
       
  5859 	struct pci_dev *pdev = to_pci_dev(dev);
       
  5860 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5861 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5862 
       
  5863 	if (e1000e_pm_ready(adapter)) {
       
  5864 		bool wake;
       
  5865 
       
  5866 		__e1000_shutdown(pdev, &wake, true);
       
  5867 	}
       
  5868 
       
  5869 	return 0;
       
  5870 }
       
  5871 
       
  5872 static int e1000_idle(struct device *dev)
       
  5873 {
       
  5874 	struct pci_dev *pdev = to_pci_dev(dev);
       
  5875 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5876 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5877 
       
  5878 	if (!e1000e_pm_ready(adapter))
       
  5879 		return 0;
       
  5880 
       
  5881 	if (adapter->idle_check) {
       
  5882 		adapter->idle_check = false;
       
  5883 		if (!e1000e_has_link(adapter))
       
  5884 			pm_schedule_suspend(dev, MSEC_PER_SEC);
       
  5885 	}
       
  5886 
       
  5887 	return -EBUSY;
       
  5888 }
       
  5889 
       
  5890 static int e1000_runtime_resume(struct device *dev)
       
  5891 {
       
  5892 	struct pci_dev *pdev = to_pci_dev(dev);
       
  5893 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5894 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5895 
       
  5896 	if (!e1000e_pm_ready(adapter))
       
  5897 		return 0;
       
  5898 
       
  5899 	adapter->idle_check = !dev->power.runtime_auto;
       
  5900 	return __e1000_resume(pdev);
       
  5901 }
       
  5902 #endif /* CONFIG_PM_RUNTIME */
       
  5903 #endif /* CONFIG_PM */
       
  5904 
       
  5905 static void e1000_shutdown(struct pci_dev *pdev)
       
  5906 {
       
  5907 	bool wake = false;
       
  5908 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5909 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5910 
       
  5911 	if (adapter->ecdev)
       
  5912 		return;
       
  5913 
       
  5914 	__e1000_shutdown(pdev, &wake, false);
       
  5915 
       
  5916 	if (system_state == SYSTEM_POWER_OFF)
       
  5917 		e1000_complete_shutdown(pdev, false, wake);
       
  5918 }
       
  5919 
       
  5920 #ifdef CONFIG_NET_POLL_CONTROLLER
       
  5921 
       
  5922 static irqreturn_t e1000_intr_msix(int irq, void *data)
       
  5923 {
       
  5924 	struct net_device *netdev = data;
       
  5925 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5926 
       
  5927 	if (adapter->msix_entries) {
       
  5928 		int vector, msix_irq;
       
  5929 
       
  5930 		vector = 0;
       
  5931 		msix_irq = adapter->msix_entries[vector].vector;
       
  5932 		disable_irq(msix_irq);
       
  5933 		e1000_intr_msix_rx(msix_irq, netdev);
       
  5934 		enable_irq(msix_irq);
       
  5935 
       
  5936 		vector++;
       
  5937 		msix_irq = adapter->msix_entries[vector].vector;
       
  5938 		disable_irq(msix_irq);
       
  5939 		e1000_intr_msix_tx(msix_irq, netdev);
       
  5940 		enable_irq(msix_irq);
       
  5941 
       
  5942 		vector++;
       
  5943 		msix_irq = adapter->msix_entries[vector].vector;
       
  5944 		disable_irq(msix_irq);
       
  5945 		e1000_msix_other(msix_irq, netdev);
       
  5946 		enable_irq(msix_irq);
       
  5947 	}
       
  5948 
       
  5949 	return IRQ_HANDLED;
       
  5950 }
       
  5951 
       
  5952 /*
       
  5953  * Polling 'interrupt' - used by things like netconsole to send skbs
       
  5954  * without having to re-enable interrupts. It's not called while
       
  5955  * the interrupt routine is executing.
       
  5956  */
       
  5957 static void e1000_netpoll(struct net_device *netdev)
       
  5958 {
       
  5959 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5960 
       
  5961 	switch (adapter->int_mode) {
       
  5962 	case E1000E_INT_MODE_MSIX:
       
  5963 		e1000_intr_msix(adapter->pdev->irq, netdev);
       
  5964 		break;
       
  5965 	case E1000E_INT_MODE_MSI:
       
  5966 		disable_irq(adapter->pdev->irq);
       
  5967 		e1000_intr_msi(adapter->pdev->irq, netdev);
       
  5968 		enable_irq(adapter->pdev->irq);
       
  5969 		break;
       
  5970 	default: /* E1000E_INT_MODE_LEGACY */
       
  5971 		disable_irq(adapter->pdev->irq);
       
  5972 		e1000_intr(adapter->pdev->irq, netdev);
       
  5973 		enable_irq(adapter->pdev->irq);
       
  5974 		break;
       
  5975 	}
       
  5976 }
       
  5977 #endif
       
  5978 
       
  5979 /**
       
  5980  * e1000_io_error_detected - called when PCI error is detected
       
  5981  * @pdev: Pointer to PCI device
       
  5982  * @state: The current pci connection state
       
  5983  *
       
  5984  * This function is called after a PCI bus error affecting
       
  5985  * this device has been detected.
       
  5986  */
       
  5987 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
       
  5988 						pci_channel_state_t state)
       
  5989 {
       
  5990 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5991 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5992 
       
  5993 	netif_device_detach(netdev);
       
  5994 
       
  5995 	if (state == pci_channel_io_perm_failure)
       
  5996 		return PCI_ERS_RESULT_DISCONNECT;
       
  5997 
       
  5998 	if (netif_running(netdev))
       
  5999 		e1000e_down(adapter);
       
  6000 	pci_disable_device(pdev);
       
  6001 
       
  6002 	/* Request a slot slot reset. */
       
  6003 	return PCI_ERS_RESULT_NEED_RESET;
       
  6004 }
       
  6005 
       
  6006 /**
       
  6007  * e1000_io_slot_reset - called after the pci bus has been reset.
       
  6008  * @pdev: Pointer to PCI device
       
  6009  *
       
  6010  * Restart the card from scratch, as if from a cold-boot. Implementation
       
  6011  * resembles the first-half of the e1000_resume routine.
       
  6012  */
       
  6013 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
       
  6014 {
       
  6015 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  6016 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  6017 	struct e1000_hw *hw = &adapter->hw;
       
  6018 	u16 aspm_disable_flag = 0;
       
  6019 	int err;
       
  6020 	pci_ers_result_t result;
       
  6021 
       
  6022 	if (adapter->flags2 & FLAG2_DISABLE_ASPM_L0S)
       
  6023 		aspm_disable_flag = PCIE_LINK_STATE_L0S;
       
  6024 	if (adapter->flags2 & FLAG2_DISABLE_ASPM_L1)
       
  6025 		aspm_disable_flag |= PCIE_LINK_STATE_L1;
       
  6026 	if (aspm_disable_flag)
       
  6027 		e1000e_disable_aspm(pdev, aspm_disable_flag);
       
  6028 
       
  6029 	err = pci_enable_device_mem(pdev);
       
  6030 	if (err) {
       
  6031 		dev_err(&pdev->dev,
       
  6032 			"Cannot re-enable PCI device after reset.\n");
       
  6033 		result = PCI_ERS_RESULT_DISCONNECT;
       
  6034 	} else {
       
  6035 		pci_set_master(pdev);
       
  6036 		pdev->state_saved = true;
       
  6037 		pci_restore_state(pdev);
       
  6038 
       
  6039 		pci_enable_wake(pdev, PCI_D3hot, 0);
       
  6040 		pci_enable_wake(pdev, PCI_D3cold, 0);
       
  6041 
       
  6042 		e1000e_reset(adapter);
       
  6043 		ew32(WUS, ~0);
       
  6044 		result = PCI_ERS_RESULT_RECOVERED;
       
  6045 	}
       
  6046 
       
  6047 	pci_cleanup_aer_uncorrect_error_status(pdev);
       
  6048 
       
  6049 	return result;
       
  6050 }
       
  6051 
       
  6052 /**
       
  6053  * e1000_io_resume - called when traffic can start flowing again.
       
  6054  * @pdev: Pointer to PCI device
       
  6055  *
       
  6056  * This callback is called when the error recovery driver tells us that
       
  6057  * its OK to resume normal operation. Implementation resembles the
       
  6058  * second-half of the e1000_resume routine.
       
  6059  */
       
  6060 static void e1000_io_resume(struct pci_dev *pdev)
       
  6061 {
       
  6062 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  6063 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  6064 
       
  6065 	e1000_init_manageability_pt(adapter);
       
  6066 
       
  6067 	if (netif_running(netdev)) {
       
  6068 		if (e1000e_up(adapter)) {
       
  6069 			dev_err(&pdev->dev,
       
  6070 				"can't bring device back up after reset\n");
       
  6071 			return;
       
  6072 		}
       
  6073 	}
       
  6074 
       
  6075 	netif_device_attach(netdev);
       
  6076 
       
  6077 	/*
       
  6078 	 * If the controller has AMT, do not set DRV_LOAD until the interface
       
  6079 	 * is up.  For all other cases, let the f/w know that the h/w is now
       
  6080 	 * under the control of the driver.
       
  6081 	 */
       
  6082 	if (!(adapter->flags & FLAG_HAS_AMT))
       
  6083 		e1000e_get_hw_control(adapter);
       
  6084 
       
  6085 }
       
  6086 
       
  6087 static void e1000_print_device_info(struct e1000_adapter *adapter)
       
  6088 {
       
  6089 	struct e1000_hw *hw = &adapter->hw;
       
  6090 	struct net_device *netdev = adapter->netdev;
       
  6091 	u32 ret_val;
       
  6092 	u8 pba_str[E1000_PBANUM_LENGTH];
       
  6093 
       
  6094 	/* print bus type/speed/width info */
       
  6095 	e_info("(PCI Express:2.5GT/s:%s) %pM\n",
       
  6096 	       /* bus width */
       
  6097 	       ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
       
  6098 	        "Width x1"),
       
  6099 	       /* MAC address */
       
  6100 	       netdev->dev_addr);
       
  6101 	e_info("Intel(R) PRO/%s Network Connection\n",
       
  6102 	       (hw->phy.type == e1000_phy_ife) ? "10/100" : "1000");
       
  6103 	ret_val = e1000_read_pba_string_generic(hw, pba_str,
       
  6104 						E1000_PBANUM_LENGTH);
       
  6105 	if (ret_val)
       
  6106 		strlcpy((char *)pba_str, "Unknown", sizeof(pba_str));
       
  6107 	e_info("MAC: %d, PHY: %d, PBA No: %s\n",
       
  6108 	       hw->mac.type, hw->phy.type, pba_str);
       
  6109 }
       
  6110 
       
  6111 static void e1000_eeprom_checks(struct e1000_adapter *adapter)
       
  6112 {
       
  6113 	struct e1000_hw *hw = &adapter->hw;
       
  6114 	int ret_val;
       
  6115 	u16 buf = 0;
       
  6116 
       
  6117 	if (hw->mac.type != e1000_82573)
       
  6118 		return;
       
  6119 
       
  6120 	ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &buf);
       
  6121 	le16_to_cpus(&buf);
       
  6122 	if (!ret_val && (!(buf & (1 << 0)))) {
       
  6123 		/* Deep Smart Power Down (DSPD) */
       
  6124 		dev_warn(&adapter->pdev->dev,
       
  6125 			 "Warning: detected DSPD enabled in EEPROM\n");
       
  6126 	}
       
  6127 }
       
  6128 
       
  6129 static int e1000_set_features(struct net_device *netdev,
       
  6130 			      netdev_features_t features)
       
  6131 {
       
  6132 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  6133 	netdev_features_t changed = features ^ netdev->features;
       
  6134 
       
  6135 	if (changed & (NETIF_F_TSO | NETIF_F_TSO6))
       
  6136 		adapter->flags |= FLAG_TSO_FORCE;
       
  6137 
       
  6138 	if (!(changed & (NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX |
       
  6139 			 NETIF_F_RXCSUM | NETIF_F_RXHASH | NETIF_F_RXFCS |
       
  6140 			 NETIF_F_RXALL)))
       
  6141 		return 0;
       
  6142 
       
  6143 	if (changed & NETIF_F_RXFCS) {
       
  6144 		if (features & NETIF_F_RXFCS) {
       
  6145 			adapter->flags2 &= ~FLAG2_CRC_STRIPPING;
       
  6146 		} else {
       
  6147 			/* We need to take it back to defaults, which might mean
       
  6148 			 * stripping is still disabled at the adapter level.
       
  6149 			 */
       
  6150 			if (adapter->flags2 & FLAG2_DFLT_CRC_STRIPPING)
       
  6151 				adapter->flags2 |= FLAG2_CRC_STRIPPING;
       
  6152 			else
       
  6153 				adapter->flags2 &= ~FLAG2_CRC_STRIPPING;
       
  6154 		}
       
  6155 	}
       
  6156 
       
  6157 	netdev->features = features;
       
  6158 
       
  6159 	if (netif_running(netdev))
       
  6160 		e1000e_reinit_locked(adapter);
       
  6161 	else
       
  6162 		e1000e_reset(adapter);
       
  6163 
       
  6164 	return 0;
       
  6165 }
       
  6166 
       
  6167 static const struct net_device_ops e1000e_netdev_ops = {
       
  6168 	.ndo_open		= e1000_open,
       
  6169 	.ndo_stop		= e1000_close,
       
  6170 	.ndo_start_xmit		= e1000_xmit_frame,
       
  6171 	.ndo_get_stats64	= e1000e_get_stats64,
       
  6172 	.ndo_set_rx_mode	= e1000e_set_rx_mode,
       
  6173 	.ndo_set_mac_address	= e1000_set_mac,
       
  6174 	.ndo_change_mtu		= e1000_change_mtu,
       
  6175 	.ndo_do_ioctl		= e1000_ioctl,
       
  6176 	.ndo_tx_timeout		= e1000_tx_timeout,
       
  6177 	.ndo_validate_addr	= eth_validate_addr,
       
  6178 
       
  6179 	.ndo_vlan_rx_add_vid	= e1000_vlan_rx_add_vid,
       
  6180 	.ndo_vlan_rx_kill_vid	= e1000_vlan_rx_kill_vid,
       
  6181 #ifdef CONFIG_NET_POLL_CONTROLLER
       
  6182 	.ndo_poll_controller	= e1000_netpoll,
       
  6183 #endif
       
  6184 	.ndo_set_features = e1000_set_features,
       
  6185 };
       
  6186 
       
  6187 /**
       
  6188  * ec_poll - Ethercat poll Routine
       
  6189  * @netdev: net device structure
       
  6190  *
       
  6191  * This function can never fail.
       
  6192  *
       
  6193  **/
       
  6194 void ec_poll(struct net_device *netdev)
       
  6195 {
       
  6196 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  6197 
       
  6198 	if (jiffies - adapter->ec_watchdog_jiffies >= 2 * HZ) {
       
  6199 		e1000_watchdog((unsigned long) adapter);
       
  6200 		adapter->ec_watchdog_jiffies = jiffies;
       
  6201 	}
       
  6202 
       
  6203 #ifdef CONFIG_PCI_MSI
       
  6204 	e1000_intr_msi(0,netdev);
       
  6205 #else
       
  6206 	e1000_intr(0,netdev);
       
  6207 #endif
       
  6208 }
       
  6209 
       
  6210 /**
       
  6211  * e1000_probe - Device Initialization Routine
       
  6212  * @pdev: PCI device information struct
       
  6213  * @ent: entry in e1000_pci_tbl
       
  6214  *
       
  6215  * Returns 0 on success, negative on failure
       
  6216  *
       
  6217  * e1000_probe initializes an adapter identified by a pci_dev structure.
       
  6218  * The OS initialization, configuring of the adapter private structure,
       
  6219  * and a hardware reset occur.
       
  6220  **/
       
  6221 static int __devinit e1000_probe(struct pci_dev *pdev,
       
  6222 				 const struct pci_device_id *ent)
       
  6223 {
       
  6224 	struct net_device *netdev;
       
  6225 	struct e1000_adapter *adapter;
       
  6226 	struct e1000_hw *hw;
       
  6227 	const struct e1000_info *ei = e1000_info_tbl[ent->driver_data];
       
  6228 	resource_size_t mmio_start, mmio_len;
       
  6229 	resource_size_t flash_start, flash_len;
       
  6230 	static int cards_found;
       
  6231 	u16 aspm_disable_flag = 0;
       
  6232 	int i, err, pci_using_dac;
       
  6233 	u16 eeprom_data = 0;
       
  6234 	u16 eeprom_apme_mask = E1000_EEPROM_APME;
       
  6235 
       
  6236 	if (ei->flags2 & FLAG2_DISABLE_ASPM_L0S)
       
  6237 		aspm_disable_flag = PCIE_LINK_STATE_L0S;
       
  6238 	if (ei->flags2 & FLAG2_DISABLE_ASPM_L1)
       
  6239 		aspm_disable_flag |= PCIE_LINK_STATE_L1;
       
  6240 	if (aspm_disable_flag)
       
  6241 		e1000e_disable_aspm(pdev, aspm_disable_flag);
       
  6242 
       
  6243 	err = pci_enable_device_mem(pdev);
       
  6244 	if (err)
       
  6245 		return err;
       
  6246 
       
  6247 	pci_using_dac = 0;
       
  6248 	err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
       
  6249 	if (!err) {
       
  6250 		err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
       
  6251 		if (!err)
       
  6252 			pci_using_dac = 1;
       
  6253 	} else {
       
  6254 		err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
       
  6255 		if (err) {
       
  6256 			err = dma_set_coherent_mask(&pdev->dev,
       
  6257 						    DMA_BIT_MASK(32));
       
  6258 			if (err) {
       
  6259 				dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
       
  6260 				goto err_dma;
       
  6261 			}
       
  6262 		}
       
  6263 	}
       
  6264 
       
  6265 	err = pci_request_selected_regions_exclusive(pdev,
       
  6266 	                                  pci_select_bars(pdev, IORESOURCE_MEM),
       
  6267 	                                  e1000e_driver_name);
       
  6268 	if (err)
       
  6269 		goto err_pci_reg;
       
  6270 
       
  6271 	/* AER (Advanced Error Reporting) hooks */
       
  6272 	pci_enable_pcie_error_reporting(pdev);
       
  6273 
       
  6274 	pci_set_master(pdev);
       
  6275 	/* PCI config space info */
       
  6276 	err = pci_save_state(pdev);
       
  6277 	if (err)
       
  6278 		goto err_alloc_etherdev;
       
  6279 
       
  6280 	err = -ENOMEM;
       
  6281 	netdev = alloc_etherdev(sizeof(struct e1000_adapter));
       
  6282 	if (!netdev)
       
  6283 		goto err_alloc_etherdev;
       
  6284 
       
  6285 	SET_NETDEV_DEV(netdev, &pdev->dev);
       
  6286 
       
  6287 	netdev->irq = pdev->irq;
       
  6288 
       
  6289 	pci_set_drvdata(pdev, netdev);
       
  6290 	adapter = netdev_priv(netdev);
       
  6291 	hw = &adapter->hw;
       
  6292 	adapter->netdev = netdev;
       
  6293 	adapter->pdev = pdev;
       
  6294 	adapter->ei = ei;
       
  6295 	adapter->pba = ei->pba;
       
  6296 	adapter->flags = ei->flags;
       
  6297 	adapter->flags2 = ei->flags2;
       
  6298 	adapter->hw.adapter = adapter;
       
  6299 	adapter->hw.mac.type = ei->mac;
       
  6300 	adapter->max_hw_frame_size = ei->max_hw_frame_size;
       
  6301 	adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
       
  6302 
       
  6303 	mmio_start = pci_resource_start(pdev, 0);
       
  6304 	mmio_len = pci_resource_len(pdev, 0);
       
  6305 
       
  6306 	err = -EIO;
       
  6307 	adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
       
  6308 	if (!adapter->hw.hw_addr)
       
  6309 		goto err_ioremap;
       
  6310 
       
  6311 	if ((adapter->flags & FLAG_HAS_FLASH) &&
       
  6312 	    (pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
       
  6313 		flash_start = pci_resource_start(pdev, 1);
       
  6314 		flash_len = pci_resource_len(pdev, 1);
       
  6315 		adapter->hw.flash_address = ioremap(flash_start, flash_len);
       
  6316 		if (!adapter->hw.flash_address)
       
  6317 			goto err_flashmap;
       
  6318 	}
       
  6319 
       
  6320 	/* construct the net_device struct */
       
  6321 	netdev->netdev_ops		= &e1000e_netdev_ops;
       
  6322 	e1000e_set_ethtool_ops(netdev);
       
  6323 	netdev->watchdog_timeo		= 5 * HZ;
       
  6324 	netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
       
  6325 	strlcpy(netdev->name, pci_name(pdev), sizeof(netdev->name));
       
  6326 
       
  6327 	netdev->mem_start = mmio_start;
       
  6328 	netdev->mem_end = mmio_start + mmio_len;
       
  6329 
       
  6330 	adapter->bd_number = cards_found++;
       
  6331 
       
  6332 	e1000e_check_options(adapter);
       
  6333 
       
  6334 	/* setup adapter struct */
       
  6335 	err = e1000_sw_init(adapter);
       
  6336 	if (err)
       
  6337 		goto err_sw_init;
       
  6338 
       
  6339 	memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
       
  6340 	memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
       
  6341 	memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
       
  6342 
       
  6343 	err = ei->get_variants(adapter);
       
  6344 	if (err)
       
  6345 		goto err_hw_init;
       
  6346 
       
  6347 	if ((adapter->flags & FLAG_IS_ICH) &&
       
  6348 	    (adapter->flags & FLAG_READ_ONLY_NVM))
       
  6349 		e1000e_write_protect_nvm_ich8lan(&adapter->hw);
       
  6350 
       
  6351 	hw->mac.ops.get_bus_info(&adapter->hw);
       
  6352 
       
  6353 	adapter->hw.phy.autoneg_wait_to_complete = 0;
       
  6354 
       
  6355 	/* Copper options */
       
  6356 	if (adapter->hw.phy.media_type == e1000_media_type_copper) {
       
  6357 		adapter->hw.phy.mdix = AUTO_ALL_MODES;
       
  6358 		adapter->hw.phy.disable_polarity_correction = 0;
       
  6359 		adapter->hw.phy.ms_type = e1000_ms_hw_default;
       
  6360 	}
       
  6361 
       
  6362 	if (hw->phy.ops.check_reset_block && hw->phy.ops.check_reset_block(hw))
       
  6363 		e_info("PHY reset is blocked due to SOL/IDER session.\n");
       
  6364 
       
  6365 	/* Set initial default active device features */
       
  6366 	netdev->features = (NETIF_F_SG |
       
  6367 			    NETIF_F_HW_VLAN_RX |
       
  6368 			    NETIF_F_HW_VLAN_TX |
       
  6369 			    NETIF_F_TSO |
       
  6370 			    NETIF_F_TSO6 |
       
  6371 			    NETIF_F_RXHASH |
       
  6372 			    NETIF_F_RXCSUM |
       
  6373 			    NETIF_F_HW_CSUM);
       
  6374 
       
  6375 	/* Set user-changeable features (subset of all device features) */
       
  6376 	netdev->hw_features = netdev->features;
       
  6377 	netdev->hw_features |= NETIF_F_RXFCS;
       
  6378 	netdev->priv_flags |= IFF_SUPP_NOFCS;
       
  6379 	netdev->hw_features |= NETIF_F_RXALL;
       
  6380 
       
  6381 	if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER)
       
  6382 		netdev->features |= NETIF_F_HW_VLAN_FILTER;
       
  6383 
       
  6384 	netdev->vlan_features |= (NETIF_F_SG |
       
  6385 				  NETIF_F_TSO |
       
  6386 				  NETIF_F_TSO6 |
       
  6387 				  NETIF_F_HW_CSUM);
       
  6388 
       
  6389 	netdev->priv_flags |= IFF_UNICAST_FLT;
       
  6390 
       
  6391 	if (pci_using_dac) {
       
  6392 		netdev->features |= NETIF_F_HIGHDMA;
       
  6393 		netdev->vlan_features |= NETIF_F_HIGHDMA;
       
  6394 	}
       
  6395 
       
  6396 	if (e1000e_enable_mng_pass_thru(&adapter->hw))
       
  6397 		adapter->flags |= FLAG_MNG_PT_ENABLED;
       
  6398 
       
  6399 	/*
       
  6400 	 * before reading the NVM, reset the controller to
       
  6401 	 * put the device in a known good starting state
       
  6402 	 */
       
  6403 	adapter->hw.mac.ops.reset_hw(&adapter->hw);
       
  6404 
       
  6405 	/*
       
  6406 	 * systems with ASPM and others may see the checksum fail on the first
       
  6407 	 * attempt. Let's give it a few tries
       
  6408 	 */
       
  6409 	for (i = 0;; i++) {
       
  6410 		if (e1000_validate_nvm_checksum(&adapter->hw) >= 0)
       
  6411 			break;
       
  6412 		if (i == 2) {
       
  6413 			e_err("The NVM Checksum Is Not Valid\n");
       
  6414 			err = -EIO;
       
  6415 			goto err_eeprom;
       
  6416 		}
       
  6417 	}
       
  6418 
       
  6419 	e1000_eeprom_checks(adapter);
       
  6420 
       
  6421 	/* copy the MAC address */
       
  6422 	if (e1000e_read_mac_addr(&adapter->hw))
       
  6423 		e_err("NVM Read Error while reading MAC address\n");
       
  6424 
       
  6425 	memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
       
  6426 	memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
       
  6427 
       
  6428 	if (!is_valid_ether_addr(netdev->perm_addr)) {
       
  6429 		e_err("Invalid MAC Address: %pM\n", netdev->perm_addr);
       
  6430 		err = -EIO;
       
  6431 		goto err_eeprom;
       
  6432 	}
       
  6433 
       
  6434 	init_timer(&adapter->watchdog_timer);
       
  6435 	adapter->watchdog_timer.function = e1000_watchdog;
       
  6436 	adapter->watchdog_timer.data = (unsigned long) adapter;
       
  6437 
       
  6438 	init_timer(&adapter->phy_info_timer);
       
  6439 	adapter->phy_info_timer.function = e1000_update_phy_info;
       
  6440 	adapter->phy_info_timer.data = (unsigned long) adapter;
       
  6441 
       
  6442 	INIT_WORK(&adapter->reset_task, e1000_reset_task);
       
  6443 	INIT_WORK(&adapter->watchdog_task, e1000_watchdog_task);
       
  6444 	INIT_WORK(&adapter->downshift_task, e1000e_downshift_workaround);
       
  6445 	INIT_WORK(&adapter->update_phy_task, e1000e_update_phy_task);
       
  6446 	INIT_WORK(&adapter->print_hang_task, e1000_print_hw_hang);
       
  6447 
       
  6448 	/* Initialize link parameters. User can change them with ethtool */
       
  6449 	adapter->hw.mac.autoneg = 1;
       
  6450 	adapter->fc_autoneg = true;
       
  6451 	adapter->hw.fc.requested_mode = e1000_fc_default;
       
  6452 	adapter->hw.fc.current_mode = e1000_fc_default;
       
  6453 	adapter->hw.phy.autoneg_advertised = 0x2f;
       
  6454 
       
  6455 	/* ring size defaults */
       
  6456 	adapter->rx_ring->count = 256;
       
  6457 	adapter->tx_ring->count = 256;
       
  6458 
       
  6459 	/*
       
  6460 	 * Initial Wake on LAN setting - If APM wake is enabled in
       
  6461 	 * the EEPROM, enable the ACPI Magic Packet filter
       
  6462 	 */
       
  6463 	if (adapter->flags & FLAG_APME_IN_WUC) {
       
  6464 		/* APME bit in EEPROM is mapped to WUC.APME */
       
  6465 		eeprom_data = er32(WUC);
       
  6466 		eeprom_apme_mask = E1000_WUC_APME;
       
  6467 		if ((hw->mac.type > e1000_ich10lan) &&
       
  6468 		    (eeprom_data & E1000_WUC_PHY_WAKE))
       
  6469 			adapter->flags2 |= FLAG2_HAS_PHY_WAKEUP;
       
  6470 	} else if (adapter->flags & FLAG_APME_IN_CTRL3) {
       
  6471 		if (adapter->flags & FLAG_APME_CHECK_PORT_B &&
       
  6472 		    (adapter->hw.bus.func == 1))
       
  6473 			e1000_read_nvm(&adapter->hw, NVM_INIT_CONTROL3_PORT_B,
       
  6474 				       1, &eeprom_data);
       
  6475 		else
       
  6476 			e1000_read_nvm(&adapter->hw, NVM_INIT_CONTROL3_PORT_A,
       
  6477 				       1, &eeprom_data);
       
  6478 	}
       
  6479 
       
  6480 	/* fetch WoL from EEPROM */
       
  6481 	if (eeprom_data & eeprom_apme_mask)
       
  6482 		adapter->eeprom_wol |= E1000_WUFC_MAG;
       
  6483 
       
  6484 	/*
       
  6485 	 * now that we have the eeprom settings, apply the special cases
       
  6486 	 * where the eeprom may be wrong or the board simply won't support
       
  6487 	 * wake on lan on a particular port
       
  6488 	 */
       
  6489 	if (!(adapter->flags & FLAG_HAS_WOL))
       
  6490 		adapter->eeprom_wol = 0;
       
  6491 
       
  6492 	/* initialize the wol settings based on the eeprom settings */
       
  6493 	adapter->wol = adapter->eeprom_wol;
       
  6494 	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
       
  6495 
       
  6496 	/* save off EEPROM version number */
       
  6497 	e1000_read_nvm(&adapter->hw, 5, 1, &adapter->eeprom_vers);
       
  6498 
       
  6499 	/* reset the hardware with the new settings */
       
  6500 	e1000e_reset(adapter);
       
  6501 
       
  6502 	/*
       
  6503 	 * If the controller has AMT, do not set DRV_LOAD until the interface
       
  6504 	 * is up.  For all other cases, let the f/w know that the h/w is now
       
  6505 	 * under the control of the driver.
       
  6506 	 */
       
  6507 	if (!(adapter->flags & FLAG_HAS_AMT))
       
  6508 		e1000e_get_hw_control(adapter);
       
  6509 
       
  6510 	adapter->ecdev = ecdev_offer(netdev, ec_poll, THIS_MODULE);
       
  6511 	if (adapter->ecdev) {
       
  6512 		if (ecdev_open(adapter->ecdev)) {
       
  6513 			ecdev_withdraw(adapter->ecdev);
       
  6514 			goto err_register;
       
  6515 		}
       
  6516 	} else {
       
  6517 		strlcpy(netdev->name, "eth%d", sizeof(netdev->name));
       
  6518 		err = register_netdev(netdev);
       
  6519 		if (err)
       
  6520 			goto err_register;
       
  6521 
       
  6522 		/* carrier off reporting is important to ethtool even BEFORE open */
       
  6523 		netif_carrier_off(netdev);
       
  6524 	}
       
  6525 
       
  6526 	e1000_print_device_info(adapter);
       
  6527 
       
  6528 	if (pci_dev_run_wake(pdev))
       
  6529 		pm_runtime_put_noidle(&pdev->dev);
       
  6530 
       
  6531 	return 0;
       
  6532 
       
  6533 err_register:
       
  6534 	if (!(adapter->flags & FLAG_HAS_AMT))
       
  6535 		e1000e_release_hw_control(adapter);
       
  6536 err_eeprom:
       
  6537 	if (hw->phy.ops.check_reset_block && !hw->phy.ops.check_reset_block(hw))
       
  6538 		e1000_phy_hw_reset(&adapter->hw);
       
  6539 err_hw_init:
       
  6540 	kfree(adapter->tx_ring);
       
  6541 	kfree(adapter->rx_ring);
       
  6542 err_sw_init:
       
  6543 	if (adapter->hw.flash_address)
       
  6544 		iounmap(adapter->hw.flash_address);
       
  6545 	e1000e_reset_interrupt_capability(adapter);
       
  6546 err_flashmap:
       
  6547 	iounmap(adapter->hw.hw_addr);
       
  6548 err_ioremap:
       
  6549 	free_netdev(netdev);
       
  6550 err_alloc_etherdev:
       
  6551 	pci_release_selected_regions(pdev,
       
  6552 	                             pci_select_bars(pdev, IORESOURCE_MEM));
       
  6553 err_pci_reg:
       
  6554 err_dma:
       
  6555 	pci_disable_device(pdev);
       
  6556 	return err;
       
  6557 }
       
  6558 
       
  6559 /**
       
  6560  * e1000_remove - Device Removal Routine
       
  6561  * @pdev: PCI device information struct
       
  6562  *
       
  6563  * e1000_remove is called by the PCI subsystem to alert the driver
       
  6564  * that it should release a PCI device.  The could be caused by a
       
  6565  * Hot-Plug event, or because the driver is going to be removed from
       
  6566  * memory.
       
  6567  **/
       
  6568 static void __devexit e1000_remove(struct pci_dev *pdev)
       
  6569 {
       
  6570 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  6571 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  6572 	bool down = test_bit(__E1000_DOWN, &adapter->state);
       
  6573 
       
  6574 	/*
       
  6575 	 * The timers may be rescheduled, so explicitly disable them
       
  6576 	 * from being rescheduled.
       
  6577 	 */
       
  6578 	if (!down)
       
  6579 		set_bit(__E1000_DOWN, &adapter->state);
       
  6580 	del_timer_sync(&adapter->watchdog_timer);
       
  6581 	del_timer_sync(&adapter->phy_info_timer);
       
  6582 
       
  6583 	cancel_work_sync(&adapter->reset_task);
       
  6584 	cancel_work_sync(&adapter->watchdog_task);
       
  6585 	cancel_work_sync(&adapter->downshift_task);
       
  6586 	cancel_work_sync(&adapter->update_phy_task);
       
  6587 	cancel_work_sync(&adapter->print_hang_task);
       
  6588 
       
  6589 	if (!(netdev->flags & IFF_UP))
       
  6590 		e1000_power_down_phy(adapter);
       
  6591 
       
  6592 	/* Don't lie to e1000_close() down the road. */
       
  6593 	if (!down)
       
  6594 		clear_bit(__E1000_DOWN, &adapter->state);
       
  6595 
       
  6596 	if (adapter->ecdev) {
       
  6597 		ecdev_close(adapter->ecdev);
       
  6598 		ecdev_withdraw(adapter->ecdev);
       
  6599 	} else {
       
  6600 		unregister_netdev(netdev);
       
  6601 	}
       
  6602 
       
  6603 	if (pci_dev_run_wake(pdev))
       
  6604 		pm_runtime_get_noresume(&pdev->dev);
       
  6605 
       
  6606 	/*
       
  6607 	 * Release control of h/w to f/w.  If f/w is AMT enabled, this
       
  6608 	 * would have already happened in close and is redundant.
       
  6609 	 */
       
  6610 	e1000e_release_hw_control(adapter);
       
  6611 
       
  6612 	e1000e_reset_interrupt_capability(adapter);
       
  6613 	kfree(adapter->tx_ring);
       
  6614 	kfree(adapter->rx_ring);
       
  6615 
       
  6616 	iounmap(adapter->hw.hw_addr);
       
  6617 	if (adapter->hw.flash_address)
       
  6618 		iounmap(adapter->hw.flash_address);
       
  6619 	pci_release_selected_regions(pdev,
       
  6620 	                             pci_select_bars(pdev, IORESOURCE_MEM));
       
  6621 
       
  6622 	free_netdev(netdev);
       
  6623 
       
  6624 	/* AER disable */
       
  6625 	pci_disable_pcie_error_reporting(pdev);
       
  6626 
       
  6627 	pci_disable_device(pdev);
       
  6628 }
       
  6629 
       
  6630 /* PCI Error Recovery (ERS) */
       
  6631 static struct pci_error_handlers e1000_err_handler = {
       
  6632 	.error_detected = e1000_io_error_detected,
       
  6633 	.slot_reset = e1000_io_slot_reset,
       
  6634 	.resume = e1000_io_resume,
       
  6635 };
       
  6636 
       
  6637 static DEFINE_PCI_DEVICE_TABLE(e1000_pci_tbl) = {
       
  6638 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_COPPER), board_82571 },
       
  6639 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_FIBER), board_82571 },
       
  6640 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER), board_82571 },
       
  6641 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER_LP), board_82571 },
       
  6642 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_FIBER), board_82571 },
       
  6643 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES), board_82571 },
       
  6644 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_DUAL), board_82571 },
       
  6645 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_QUAD), board_82571 },
       
  6646 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82571PT_QUAD_COPPER), board_82571 },
       
  6647 
       
  6648 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI), board_82572 },
       
  6649 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_COPPER), board_82572 },
       
  6650 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_FIBER), board_82572 },
       
  6651 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_SERDES), board_82572 },
       
  6652 
       
  6653 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E), board_82573 },
       
  6654 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E_IAMT), board_82573 },
       
  6655 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82573L), board_82573 },
       
  6656 
       
  6657 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82574L), board_82574 },
       
  6658 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82574LA), board_82574 },
       
  6659 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_82583V), board_82583 },
       
  6660 
       
  6661 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_DPT),
       
  6662 	  board_80003es2lan },
       
  6663 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_SPT),
       
  6664 	  board_80003es2lan },
       
  6665 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_DPT),
       
  6666 	  board_80003es2lan },
       
  6667 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_SPT),
       
  6668 	  board_80003es2lan },
       
  6669 
       
  6670 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE), board_ich8lan },
       
  6671 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_G), board_ich8lan },
       
  6672 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_GT), board_ich8lan },
       
  6673 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_AMT), board_ich8lan },
       
  6674 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_C), board_ich8lan },
       
  6675 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M), board_ich8lan },
       
  6676 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M_AMT), board_ich8lan },
       
  6677 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_82567V_3), board_ich8lan },
       
  6678 
       
  6679 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE), board_ich9lan },
       
  6680 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_G), board_ich9lan },
       
  6681 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_GT), board_ich9lan },
       
  6682 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_AMT), board_ich9lan },
       
  6683 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_C), board_ich9lan },
       
  6684 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_BM), board_ich9lan },
       
  6685 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M), board_ich9lan },
       
  6686 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_AMT), board_ich9lan },
       
  6687 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_V), board_ich9lan },
       
  6688 
       
  6689 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LM), board_ich9lan },
       
  6690 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LF), board_ich9lan },
       
  6691 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_V), board_ich9lan },
       
  6692 
       
  6693 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_LM), board_ich10lan },
       
  6694 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_LF), board_ich10lan },
       
  6695 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_V), board_ich10lan },
       
  6696 
       
  6697 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_M_HV_LM), board_pchlan },
       
  6698 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_M_HV_LC), board_pchlan },
       
  6699 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_D_HV_DM), board_pchlan },
       
  6700 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_D_HV_DC), board_pchlan },
       
  6701 
       
  6702 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH2_LV_LM), board_pch2lan },
       
  6703 	{ PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH2_LV_V), board_pch2lan },
       
  6704 
       
  6705 	{ 0, 0, 0, 0, 0, 0, 0 }	/* terminate list */
       
  6706 };
       
  6707 //MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
       
  6708 
       
  6709 #ifdef CONFIG_PM
       
  6710 static const struct dev_pm_ops e1000_pm_ops = {
       
  6711 	SET_SYSTEM_SLEEP_PM_OPS(e1000_suspend, e1000_resume)
       
  6712 	SET_RUNTIME_PM_OPS(e1000_runtime_suspend,
       
  6713 				e1000_runtime_resume, e1000_idle)
       
  6714 };
       
  6715 #endif
       
  6716 
       
  6717 /* PCI Device API Driver */
       
  6718 static struct pci_driver e1000_driver = {
       
  6719 	.name     = e1000e_driver_name,
       
  6720 	.id_table = e1000_pci_tbl,
       
  6721 	.probe    = e1000_probe,
       
  6722 	.remove   = __devexit_p(e1000_remove),
       
  6723 #ifdef CONFIG_PM
       
  6724 	.driver   = {
       
  6725 		.pm = &e1000_pm_ops,
       
  6726 	},
       
  6727 #endif
       
  6728 	.shutdown = e1000_shutdown,
       
  6729 	.err_handler = &e1000_err_handler
       
  6730 };
       
  6731 
       
  6732 /**
       
  6733  * e1000_init_module - Driver Registration Routine
       
  6734  *
       
  6735  * e1000_init_module is the first routine called when the driver is
       
  6736  * loaded. All it does is register with the PCI subsystem.
       
  6737  **/
       
  6738 static int __init e1000_init_module(void)
       
  6739 {
       
  6740 	int ret;
       
  6741 	pr_info("EtherCAT-capable Intel(R) PRO/1000 Network Driver - %s\n",
       
  6742 		e1000e_driver_version);
       
  6743 	pr_info("Copyright(c) 1999 - 2012 Intel Corporation.\n");
       
  6744 	ret = pci_register_driver(&e1000_driver);
       
  6745 
       
  6746 	return ret;
       
  6747 }
       
  6748 module_init(e1000_init_module);
       
  6749 
       
  6750 /**
       
  6751  * e1000_exit_module - Driver Exit Cleanup Routine
       
  6752  *
       
  6753  * e1000_exit_module is called just before the driver is removed
       
  6754  * from memory.
       
  6755  **/
       
  6756 static void __exit e1000_exit_module(void)
       
  6757 {
       
  6758 	pci_unregister_driver(&e1000_driver);
       
  6759 }
       
  6760 module_exit(e1000_exit_module);
       
  6761 
       
  6762 
       
  6763 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
       
  6764 MODULE_DESCRIPTION("Ethercat-capable Intel(R) PRO/1000 Network Driver");
       
  6765 MODULE_LICENSE("GPL");
       
  6766 MODULE_VERSION(DRV_VERSION);
       
  6767 
       
  6768 /* netdev.c */