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