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