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