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