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