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