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