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