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