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