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