Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / net / e1000e / netdev.c
blobfc5c63f4f5788d8de908e1c72d167a0f96bc6938
1 /*******************************************************************************
3 Intel PRO/1000 Linux driver
4 Copyright(c) 1999 - 2007 Intel Corporation.
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.
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.
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.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
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
27 *******************************************************************************/
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>
47 #include "e1000.h"
49 #define DRV_VERSION "0.2.0"
50 char e1000e_driver_name[] = "e1000e";
51 const char e1000e_driver_version[] = DRV_VERSION;
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,
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)
69 return hw->adapter->netdev->name;
71 #endif
73 /**
74 * e1000_desc_unused - calculate if we have unused descriptors
75 **/
76 static int e1000_desc_unused(struct e1000_ring *ring)
78 if (ring->next_to_clean > ring->next_to_use)
79 return ring->next_to_clean - ring->next_to_use - 1;
81 return ring->count + ring->next_to_clean - ring->next_to_use - 1;
84 /**
85 * e1000_receive_skb - helper function to handle rx indications
86 * @adapter: board private structure
87 * @status: descriptor status field as written by hardware
88 * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
89 * @skb: pointer to sk_buff to be indicated to stack
90 **/
91 static void e1000_receive_skb(struct e1000_adapter *adapter,
92 struct net_device *netdev,
93 struct sk_buff *skb,
94 u8 status, __le16 vlan)
96 skb->protocol = eth_type_trans(skb, netdev);
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);
105 netdev->last_rx = jiffies;
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
115 static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
116 u32 csum, struct sk_buff *skb)
118 u16 status = (u16)status_err;
119 u8 errors = (u8)(status_err >> 24);
120 skb->ip_summed = CHECKSUM_NONE;
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;
132 /* TCP/UDP Checksum has not been calculated */
133 if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
134 return;
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.
145 __sum16 sum = (__force __sum16)htons(csum);
146 skb->csum = csum_unfold(~sum);
147 skb->ip_summed = CHECKSUM_COMPLETE;
149 adapter->hw_csum_good++;
153 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
154 * @adapter: address of board private structure
156 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
157 int cleaned_count)
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;
168 i = rx_ring->next_to_use;
169 buffer_info = &rx_ring->buffer_info[i];
171 while (cleaned_count--) {
172 skb = buffer_info->skb;
173 if (skb) {
174 skb_trim(skb, 0);
175 goto map_skb;
178 skb = netdev_alloc_skb(netdev, bufsz);
179 if (!skb) {
180 /* Better luck next round */
181 adapter->alloc_rx_buff_failed++;
182 break;
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
189 skb_reserve(skb, NET_IP_ALIGN);
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;
202 rx_desc = E1000_RX_DESC(*rx_ring, i);
203 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
205 i++;
206 if (i == rx_ring->count)
207 i = 0;
208 buffer_info = &rx_ring->buffer_info[i];
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);
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);
226 * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
227 * @adapter: address of board private structure
229 static void e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
230 int cleaned_count)
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;
241 i = rx_ring->next_to_use;
242 buffer_info = &rx_ring->buffer_info[i];
244 while (cleaned_count--) {
245 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
247 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
248 ps_page = &buffer_info->ps_pages[j];
249 if (j >= adapter->rx_ps_pages) {
250 /* all unused desc entries get hw null ptr */
251 rx_desc->read.buffer_addr[j+1] = ~cpu_to_le64(0);
252 continue;
254 if (!ps_page->page) {
255 ps_page->page = alloc_page(GFP_ATOMIC);
256 if (!ps_page->page) {
257 adapter->alloc_rx_buff_failed++;
258 goto no_buffers;
260 ps_page->dma = pci_map_page(pdev,
261 ps_page->page,
262 0, PAGE_SIZE,
263 PCI_DMA_FROMDEVICE);
264 if (pci_dma_mapping_error(ps_page->dma)) {
265 dev_err(&adapter->pdev->dev,
266 "RX DMA page map failed\n");
267 adapter->rx_dma_failed++;
268 goto no_buffers;
272 * Refresh the desc even if buffer_addrs
273 * didn't change because each write-back
274 * erases this info.
276 rx_desc->read.buffer_addr[j+1] =
277 cpu_to_le64(ps_page->dma);
280 skb = netdev_alloc_skb(netdev,
281 adapter->rx_ps_bsize0 + NET_IP_ALIGN);
283 if (!skb) {
284 adapter->alloc_rx_buff_failed++;
285 break;
288 /* Make buffer alignment 2 beyond a 16 byte boundary
289 * this will result in a 16 byte aligned IP header after
290 * the 14 byte MAC header is removed
292 skb_reserve(skb, NET_IP_ALIGN);
294 buffer_info->skb = skb;
295 buffer_info->dma = pci_map_single(pdev, skb->data,
296 adapter->rx_ps_bsize0,
297 PCI_DMA_FROMDEVICE);
298 if (pci_dma_mapping_error(buffer_info->dma)) {
299 dev_err(&pdev->dev, "RX DMA map failed\n");
300 adapter->rx_dma_failed++;
301 /* cleanup skb */
302 dev_kfree_skb_any(skb);
303 buffer_info->skb = NULL;
304 break;
307 rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
309 i++;
310 if (i == rx_ring->count)
311 i = 0;
312 buffer_info = &rx_ring->buffer_info[i];
315 no_buffers:
316 if (rx_ring->next_to_use != i) {
317 rx_ring->next_to_use = i;
319 if (!(i--))
320 i = (rx_ring->count - 1);
322 /* Force memory writes to complete before letting h/w
323 * know there are new descriptors to fetch. (Only
324 * applicable for weak-ordered memory model archs,
325 * such as IA-64). */
326 wmb();
327 /* Hardware increments by 16 bytes, but packet split
328 * descriptors are 32 bytes...so we increment tail
329 * twice as much.
331 writel(i<<1, adapter->hw.hw_addr + rx_ring->tail);
336 * e1000_clean_rx_irq - Send received data up the network stack; legacy
337 * @adapter: board private structure
339 * the return value indicates whether actual cleaning was done, there
340 * is no guarantee that everything was cleaned
342 static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
343 int *work_done, int work_to_do)
345 struct net_device *netdev = adapter->netdev;
346 struct pci_dev *pdev = adapter->pdev;
347 struct e1000_ring *rx_ring = adapter->rx_ring;
348 struct e1000_rx_desc *rx_desc, *next_rxd;
349 struct e1000_buffer *buffer_info, *next_buffer;
350 u32 length;
351 unsigned int i;
352 int cleaned_count = 0;
353 bool cleaned = 0;
354 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
356 i = rx_ring->next_to_clean;
357 rx_desc = E1000_RX_DESC(*rx_ring, i);
358 buffer_info = &rx_ring->buffer_info[i];
360 while (rx_desc->status & E1000_RXD_STAT_DD) {
361 struct sk_buff *skb;
362 u8 status;
364 if (*work_done >= work_to_do)
365 break;
366 (*work_done)++;
368 status = rx_desc->status;
369 skb = buffer_info->skb;
370 buffer_info->skb = NULL;
372 prefetch(skb->data - NET_IP_ALIGN);
374 i++;
375 if (i == rx_ring->count)
376 i = 0;
377 next_rxd = E1000_RX_DESC(*rx_ring, i);
378 prefetch(next_rxd);
380 next_buffer = &rx_ring->buffer_info[i];
382 cleaned = 1;
383 cleaned_count++;
384 pci_unmap_single(pdev,
385 buffer_info->dma,
386 adapter->rx_buffer_len,
387 PCI_DMA_FROMDEVICE);
388 buffer_info->dma = 0;
390 length = le16_to_cpu(rx_desc->length);
392 /* !EOP means multiple descriptors were used to store a single
393 * packet, also make sure the frame isn't just CRC only */
394 if (!(status & E1000_RXD_STAT_EOP) || (length <= 4)) {
395 /* All receives must fit into a single buffer */
396 ndev_dbg(netdev, "%s: Receive packet consumed "
397 "multiple buffers\n", netdev->name);
398 /* recycle */
399 buffer_info->skb = skb;
400 goto next_desc;
403 if (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK) {
404 /* recycle */
405 buffer_info->skb = skb;
406 goto next_desc;
409 total_rx_bytes += length;
410 total_rx_packets++;
412 /* code added for copybreak, this should improve
413 * performance for small packets with large amounts
414 * of reassembly being done in the stack */
415 if (length < copybreak) {
416 struct sk_buff *new_skb =
417 netdev_alloc_skb(netdev, length + NET_IP_ALIGN);
418 if (new_skb) {
419 skb_reserve(new_skb, NET_IP_ALIGN);
420 memcpy(new_skb->data - NET_IP_ALIGN,
421 skb->data - NET_IP_ALIGN,
422 length + NET_IP_ALIGN);
423 /* save the skb in buffer_info as good */
424 buffer_info->skb = skb;
425 skb = new_skb;
427 /* else just continue with the old one */
429 /* end copybreak code */
430 skb_put(skb, length);
432 /* Receive Checksum Offload */
433 e1000_rx_checksum(adapter,
434 (u32)(status) |
435 ((u32)(rx_desc->errors) << 24),
436 le16_to_cpu(rx_desc->csum), skb);
438 e1000_receive_skb(adapter, netdev, skb,status,rx_desc->special);
440 next_desc:
441 rx_desc->status = 0;
443 /* return some buffers to hardware, one at a time is too slow */
444 if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
445 adapter->alloc_rx_buf(adapter, cleaned_count);
446 cleaned_count = 0;
449 /* use prefetched values */
450 rx_desc = next_rxd;
451 buffer_info = next_buffer;
453 rx_ring->next_to_clean = i;
455 cleaned_count = e1000_desc_unused(rx_ring);
456 if (cleaned_count)
457 adapter->alloc_rx_buf(adapter, cleaned_count);
459 adapter->total_rx_packets += total_rx_packets;
460 adapter->total_rx_bytes += total_rx_bytes;
461 adapter->net_stats.rx_packets += total_rx_packets;
462 adapter->net_stats.rx_bytes += total_rx_bytes;
463 return cleaned;
466 static void e1000_put_txbuf(struct e1000_adapter *adapter,
467 struct e1000_buffer *buffer_info)
469 if (buffer_info->dma) {
470 pci_unmap_page(adapter->pdev, buffer_info->dma,
471 buffer_info->length, PCI_DMA_TODEVICE);
472 buffer_info->dma = 0;
474 if (buffer_info->skb) {
475 dev_kfree_skb_any(buffer_info->skb);
476 buffer_info->skb = NULL;
480 static void e1000_print_tx_hang(struct e1000_adapter *adapter)
482 struct e1000_ring *tx_ring = adapter->tx_ring;
483 unsigned int i = tx_ring->next_to_clean;
484 unsigned int eop = tx_ring->buffer_info[i].next_to_watch;
485 struct e1000_tx_desc *eop_desc = E1000_TX_DESC(*tx_ring, eop);
486 struct net_device *netdev = adapter->netdev;
488 /* detected Tx unit hang */
489 ndev_err(netdev,
490 "Detected Tx Unit Hang:\n"
491 " TDH <%x>\n"
492 " TDT <%x>\n"
493 " next_to_use <%x>\n"
494 " next_to_clean <%x>\n"
495 "buffer_info[next_to_clean]:\n"
496 " time_stamp <%lx>\n"
497 " next_to_watch <%x>\n"
498 " jiffies <%lx>\n"
499 " next_to_watch.status <%x>\n",
500 readl(adapter->hw.hw_addr + tx_ring->head),
501 readl(adapter->hw.hw_addr + tx_ring->tail),
502 tx_ring->next_to_use,
503 tx_ring->next_to_clean,
504 tx_ring->buffer_info[eop].time_stamp,
505 eop,
506 jiffies,
507 eop_desc->upper.fields.status);
511 * e1000_clean_tx_irq - Reclaim resources after transmit completes
512 * @adapter: board private structure
514 * the return value indicates whether actual cleaning was done, there
515 * is no guarantee that everything was cleaned
517 static bool e1000_clean_tx_irq(struct e1000_adapter *adapter)
519 struct net_device *netdev = adapter->netdev;
520 struct e1000_hw *hw = &adapter->hw;
521 struct e1000_ring *tx_ring = adapter->tx_ring;
522 struct e1000_tx_desc *tx_desc, *eop_desc;
523 struct e1000_buffer *buffer_info;
524 unsigned int i, eop;
525 unsigned int count = 0;
526 bool cleaned = 0;
527 unsigned int total_tx_bytes = 0, total_tx_packets = 0;
529 i = tx_ring->next_to_clean;
530 eop = tx_ring->buffer_info[i].next_to_watch;
531 eop_desc = E1000_TX_DESC(*tx_ring, eop);
533 while (eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
534 for (cleaned = 0; !cleaned; ) {
535 tx_desc = E1000_TX_DESC(*tx_ring, i);
536 buffer_info = &tx_ring->buffer_info[i];
537 cleaned = (i == eop);
539 if (cleaned) {
540 struct sk_buff *skb = buffer_info->skb;
541 unsigned int segs, bytecount;
542 segs = skb_shinfo(skb)->gso_segs ?: 1;
543 /* multiply data chunks by size of headers */
544 bytecount = ((segs - 1) * skb_headlen(skb)) +
545 skb->len;
546 total_tx_packets += segs;
547 total_tx_bytes += bytecount;
550 e1000_put_txbuf(adapter, buffer_info);
551 tx_desc->upper.data = 0;
553 i++;
554 if (i == tx_ring->count)
555 i = 0;
558 eop = tx_ring->buffer_info[i].next_to_watch;
559 eop_desc = E1000_TX_DESC(*tx_ring, eop);
560 #define E1000_TX_WEIGHT 64
561 /* weight of a sort for tx, to avoid endless transmit cleanup */
562 if (count++ == E1000_TX_WEIGHT)
563 break;
566 tx_ring->next_to_clean = i;
568 #define TX_WAKE_THRESHOLD 32
569 if (cleaned && netif_carrier_ok(netdev) &&
570 e1000_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD) {
571 /* Make sure that anybody stopping the queue after this
572 * sees the new next_to_clean.
574 smp_mb();
576 if (netif_queue_stopped(netdev) &&
577 !(test_bit(__E1000_DOWN, &adapter->state))) {
578 netif_wake_queue(netdev);
579 ++adapter->restart_queue;
583 if (adapter->detect_tx_hung) {
584 /* Detect a transmit hang in hardware, this serializes the
585 * check with the clearing of time_stamp and movement of i */
586 adapter->detect_tx_hung = 0;
587 if (tx_ring->buffer_info[eop].dma &&
588 time_after(jiffies, tx_ring->buffer_info[eop].time_stamp
589 + (adapter->tx_timeout_factor * HZ))
590 && !(er32(STATUS) &
591 E1000_STATUS_TXOFF)) {
592 e1000_print_tx_hang(adapter);
593 netif_stop_queue(netdev);
596 adapter->total_tx_bytes += total_tx_bytes;
597 adapter->total_tx_packets += total_tx_packets;
598 adapter->net_stats.tx_packets += total_tx_packets;
599 adapter->net_stats.tx_bytes += total_tx_bytes;
600 return cleaned;
604 * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
605 * @adapter: board private structure
607 * the return value indicates whether actual cleaning was done, there
608 * is no guarantee that everything was cleaned
610 static bool e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
611 int *work_done, int work_to_do)
613 union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
614 struct net_device *netdev = adapter->netdev;
615 struct pci_dev *pdev = adapter->pdev;
616 struct e1000_ring *rx_ring = adapter->rx_ring;
617 struct e1000_buffer *buffer_info, *next_buffer;
618 struct e1000_ps_page *ps_page;
619 struct sk_buff *skb;
620 unsigned int i, j;
621 u32 length, staterr;
622 int cleaned_count = 0;
623 bool cleaned = 0;
624 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
626 i = rx_ring->next_to_clean;
627 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
628 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
629 buffer_info = &rx_ring->buffer_info[i];
631 while (staterr & E1000_RXD_STAT_DD) {
632 if (*work_done >= work_to_do)
633 break;
634 (*work_done)++;
635 skb = buffer_info->skb;
637 /* in the packet split case this is header only */
638 prefetch(skb->data - NET_IP_ALIGN);
640 i++;
641 if (i == rx_ring->count)
642 i = 0;
643 next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
644 prefetch(next_rxd);
646 next_buffer = &rx_ring->buffer_info[i];
648 cleaned = 1;
649 cleaned_count++;
650 pci_unmap_single(pdev, buffer_info->dma,
651 adapter->rx_ps_bsize0,
652 PCI_DMA_FROMDEVICE);
653 buffer_info->dma = 0;
655 if (!(staterr & E1000_RXD_STAT_EOP)) {
656 ndev_dbg(netdev, "%s: Packet Split buffers didn't pick "
657 "up the full packet\n", netdev->name);
658 dev_kfree_skb_irq(skb);
659 goto next_desc;
662 if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
663 dev_kfree_skb_irq(skb);
664 goto next_desc;
667 length = le16_to_cpu(rx_desc->wb.middle.length0);
669 if (!length) {
670 ndev_dbg(netdev, "%s: Last part of the packet spanning"
671 " multiple descriptors\n", netdev->name);
672 dev_kfree_skb_irq(skb);
673 goto next_desc;
676 /* Good Receive */
677 skb_put(skb, length);
680 /* this looks ugly, but it seems compiler issues make it
681 more efficient than reusing j */
682 int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
684 /* page alloc/put takes too long and effects small packet
685 * throughput, so unsplit small packets and save the alloc/put*/
686 if (l1 && (l1 <= copybreak) &&
687 ((length + l1) <= adapter->rx_ps_bsize0)) {
688 u8 *vaddr;
690 ps_page = &buffer_info->ps_pages[0];
692 /* there is no documentation about how to call
693 * kmap_atomic, so we can't hold the mapping
694 * very long */
695 pci_dma_sync_single_for_cpu(pdev, ps_page->dma,
696 PAGE_SIZE, PCI_DMA_FROMDEVICE);
697 vaddr = kmap_atomic(ps_page->page, KM_SKB_DATA_SOFTIRQ);
698 memcpy(skb_tail_pointer(skb), vaddr, l1);
699 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
700 pci_dma_sync_single_for_device(pdev, ps_page->dma,
701 PAGE_SIZE, PCI_DMA_FROMDEVICE);
703 skb_put(skb, l1);
704 goto copydone;
705 } /* if */
708 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
709 length = le16_to_cpu(rx_desc->wb.upper.length[j]);
710 if (!length)
711 break;
713 ps_page = &buffer_info->ps_pages[j];
714 pci_unmap_page(pdev, ps_page->dma, PAGE_SIZE,
715 PCI_DMA_FROMDEVICE);
716 ps_page->dma = 0;
717 skb_fill_page_desc(skb, j, ps_page->page, 0, length);
718 ps_page->page = NULL;
719 skb->len += length;
720 skb->data_len += length;
721 skb->truesize += length;
724 copydone:
725 total_rx_bytes += skb->len;
726 total_rx_packets++;
728 e1000_rx_checksum(adapter, staterr, le16_to_cpu(
729 rx_desc->wb.lower.hi_dword.csum_ip.csum), skb);
731 if (rx_desc->wb.upper.header_status &
732 cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP))
733 adapter->rx_hdr_split++;
735 e1000_receive_skb(adapter, netdev, skb,
736 staterr, rx_desc->wb.middle.vlan);
738 next_desc:
739 rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
740 buffer_info->skb = NULL;
742 /* return some buffers to hardware, one at a time is too slow */
743 if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
744 adapter->alloc_rx_buf(adapter, cleaned_count);
745 cleaned_count = 0;
748 /* use prefetched values */
749 rx_desc = next_rxd;
750 buffer_info = next_buffer;
752 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
754 rx_ring->next_to_clean = i;
756 cleaned_count = e1000_desc_unused(rx_ring);
757 if (cleaned_count)
758 adapter->alloc_rx_buf(adapter, cleaned_count);
760 adapter->total_rx_packets += total_rx_packets;
761 adapter->total_rx_bytes += total_rx_bytes;
762 adapter->net_stats.rx_packets += total_rx_packets;
763 adapter->net_stats.rx_bytes += total_rx_bytes;
764 return cleaned;
768 * e1000_clean_rx_ring - Free Rx Buffers per Queue
769 * @adapter: board private structure
771 static void e1000_clean_rx_ring(struct e1000_adapter *adapter)
773 struct e1000_ring *rx_ring = adapter->rx_ring;
774 struct e1000_buffer *buffer_info;
775 struct e1000_ps_page *ps_page;
776 struct pci_dev *pdev = adapter->pdev;
777 unsigned int i, j;
779 /* Free all the Rx ring sk_buffs */
780 for (i = 0; i < rx_ring->count; i++) {
781 buffer_info = &rx_ring->buffer_info[i];
782 if (buffer_info->dma) {
783 if (adapter->clean_rx == e1000_clean_rx_irq)
784 pci_unmap_single(pdev, buffer_info->dma,
785 adapter->rx_buffer_len,
786 PCI_DMA_FROMDEVICE);
787 else if (adapter->clean_rx == e1000_clean_rx_irq_ps)
788 pci_unmap_single(pdev, buffer_info->dma,
789 adapter->rx_ps_bsize0,
790 PCI_DMA_FROMDEVICE);
791 buffer_info->dma = 0;
794 if (buffer_info->skb) {
795 dev_kfree_skb(buffer_info->skb);
796 buffer_info->skb = NULL;
799 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
800 ps_page = &buffer_info->ps_pages[j];
801 if (!ps_page->page)
802 break;
803 pci_unmap_page(pdev, ps_page->dma, PAGE_SIZE,
804 PCI_DMA_FROMDEVICE);
805 ps_page->dma = 0;
806 put_page(ps_page->page);
807 ps_page->page = NULL;
811 /* there also may be some cached data from a chained receive */
812 if (rx_ring->rx_skb_top) {
813 dev_kfree_skb(rx_ring->rx_skb_top);
814 rx_ring->rx_skb_top = NULL;
817 /* Zero out the descriptor ring */
818 memset(rx_ring->desc, 0, rx_ring->size);
820 rx_ring->next_to_clean = 0;
821 rx_ring->next_to_use = 0;
823 writel(0, adapter->hw.hw_addr + rx_ring->head);
824 writel(0, adapter->hw.hw_addr + rx_ring->tail);
828 * e1000_intr_msi - Interrupt Handler
829 * @irq: interrupt number
830 * @data: pointer to a network interface device structure
832 static irqreturn_t e1000_intr_msi(int irq, void *data)
834 struct net_device *netdev = data;
835 struct e1000_adapter *adapter = netdev_priv(netdev);
836 struct e1000_hw *hw = &adapter->hw;
837 u32 icr = er32(ICR);
839 /* read ICR disables interrupts using IAM, so keep up with our
840 * enable/disable accounting */
841 atomic_inc(&adapter->irq_sem);
843 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
844 hw->mac.get_link_status = 1;
845 /* ICH8 workaround-- Call gig speed drop workaround on cable
846 * disconnect (LSC) before accessing any PHY registers */
847 if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
848 (!(er32(STATUS) & E1000_STATUS_LU)))
849 e1000e_gig_downshift_workaround_ich8lan(hw);
851 /* 80003ES2LAN workaround-- For packet buffer work-around on
852 * link down event; disable receives here in the ISR and reset
853 * adapter in watchdog */
854 if (netif_carrier_ok(netdev) &&
855 adapter->flags & FLAG_RX_NEEDS_RESTART) {
856 /* disable receives */
857 u32 rctl = er32(RCTL);
858 ew32(RCTL, rctl & ~E1000_RCTL_EN);
860 /* guard against interrupt when we're going down */
861 if (!test_bit(__E1000_DOWN, &adapter->state))
862 mod_timer(&adapter->watchdog_timer, jiffies + 1);
865 if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
866 adapter->total_tx_bytes = 0;
867 adapter->total_tx_packets = 0;
868 adapter->total_rx_bytes = 0;
869 adapter->total_rx_packets = 0;
870 __netif_rx_schedule(netdev, &adapter->napi);
871 } else {
872 atomic_dec(&adapter->irq_sem);
875 return IRQ_HANDLED;
879 * e1000_intr - Interrupt Handler
880 * @irq: interrupt number
881 * @data: pointer to a network interface device structure
883 static irqreturn_t e1000_intr(int irq, void *data)
885 struct net_device *netdev = data;
886 struct e1000_adapter *adapter = netdev_priv(netdev);
887 struct e1000_hw *hw = &adapter->hw;
889 u32 rctl, icr = er32(ICR);
890 if (!icr)
891 return IRQ_NONE; /* Not our interrupt */
893 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
894 * not set, then the adapter didn't send an interrupt */
895 if (!(icr & E1000_ICR_INT_ASSERTED))
896 return IRQ_NONE;
898 /* Interrupt Auto-Mask...upon reading ICR,
899 * interrupts are masked. No need for the
900 * IMC write, but it does mean we should
901 * account for it ASAP. */
902 atomic_inc(&adapter->irq_sem);
904 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
905 hw->mac.get_link_status = 1;
906 /* ICH8 workaround-- Call gig speed drop workaround on cable
907 * disconnect (LSC) before accessing any PHY registers */
908 if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
909 (!(er32(STATUS) & E1000_STATUS_LU)))
910 e1000e_gig_downshift_workaround_ich8lan(hw);
912 /* 80003ES2LAN workaround--
913 * For packet buffer work-around on link down event;
914 * disable receives here in the ISR and
915 * reset adapter in watchdog
917 if (netif_carrier_ok(netdev) &&
918 (adapter->flags & FLAG_RX_NEEDS_RESTART)) {
919 /* disable receives */
920 rctl = er32(RCTL);
921 ew32(RCTL, rctl & ~E1000_RCTL_EN);
923 /* guard against interrupt when we're going down */
924 if (!test_bit(__E1000_DOWN, &adapter->state))
925 mod_timer(&adapter->watchdog_timer, jiffies + 1);
928 if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
929 adapter->total_tx_bytes = 0;
930 adapter->total_tx_packets = 0;
931 adapter->total_rx_bytes = 0;
932 adapter->total_rx_packets = 0;
933 __netif_rx_schedule(netdev, &adapter->napi);
934 } else {
935 atomic_dec(&adapter->irq_sem);
938 return IRQ_HANDLED;
941 static int e1000_request_irq(struct e1000_adapter *adapter)
943 struct net_device *netdev = adapter->netdev;
944 irq_handler_t handler = e1000_intr;
945 int irq_flags = IRQF_SHARED;
946 int err;
948 if (!pci_enable_msi(adapter->pdev)) {
949 adapter->flags |= FLAG_MSI_ENABLED;
950 handler = e1000_intr_msi;
951 irq_flags = 0;
954 err = request_irq(adapter->pdev->irq, handler, irq_flags, netdev->name,
955 netdev);
956 if (err) {
957 ndev_err(netdev,
958 "Unable to allocate %s interrupt (return: %d)\n",
959 adapter->flags & FLAG_MSI_ENABLED ? "MSI":"INTx",
960 err);
961 if (adapter->flags & FLAG_MSI_ENABLED)
962 pci_disable_msi(adapter->pdev);
965 return err;
968 static void e1000_free_irq(struct e1000_adapter *adapter)
970 struct net_device *netdev = adapter->netdev;
972 free_irq(adapter->pdev->irq, netdev);
973 if (adapter->flags & FLAG_MSI_ENABLED) {
974 pci_disable_msi(adapter->pdev);
975 adapter->flags &= ~FLAG_MSI_ENABLED;
980 * e1000_irq_disable - Mask off interrupt generation on the NIC
982 static void e1000_irq_disable(struct e1000_adapter *adapter)
984 struct e1000_hw *hw = &adapter->hw;
986 atomic_inc(&adapter->irq_sem);
987 ew32(IMC, ~0);
988 e1e_flush();
989 synchronize_irq(adapter->pdev->irq);
993 * e1000_irq_enable - Enable default interrupt generation settings
995 static void e1000_irq_enable(struct e1000_adapter *adapter)
997 struct e1000_hw *hw = &adapter->hw;
999 if (atomic_dec_and_test(&adapter->irq_sem)) {
1000 ew32(IMS, IMS_ENABLE_MASK);
1001 e1e_flush();
1006 * e1000_get_hw_control - get control of the h/w from f/w
1007 * @adapter: address of board private structure
1009 * e1000_get_hw_control sets {CTRL_EXT|SWSM}:DRV_LOAD bit.
1010 * For ASF and Pass Through versions of f/w this means that
1011 * the driver is loaded. For AMT version (only with 82573)
1012 * of the f/w this means that the network i/f is open.
1014 static void e1000_get_hw_control(struct e1000_adapter *adapter)
1016 struct e1000_hw *hw = &adapter->hw;
1017 u32 ctrl_ext;
1018 u32 swsm;
1020 /* Let firmware know the driver has taken over */
1021 if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
1022 swsm = er32(SWSM);
1023 ew32(SWSM, swsm | E1000_SWSM_DRV_LOAD);
1024 } else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
1025 ctrl_ext = er32(CTRL_EXT);
1026 ew32(CTRL_EXT,
1027 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1032 * e1000_release_hw_control - release control of the h/w to f/w
1033 * @adapter: address of board private structure
1035 * e1000_release_hw_control resets {CTRL_EXT|SWSM}:DRV_LOAD bit.
1036 * For ASF and Pass Through versions of f/w this means that the
1037 * driver is no longer loaded. For AMT version (only with 82573) i
1038 * of the f/w this means that the network i/f is closed.
1041 static void e1000_release_hw_control(struct e1000_adapter *adapter)
1043 struct e1000_hw *hw = &adapter->hw;
1044 u32 ctrl_ext;
1045 u32 swsm;
1047 /* Let firmware taken over control of h/w */
1048 if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
1049 swsm = er32(SWSM);
1050 ew32(SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
1051 } else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
1052 ctrl_ext = er32(CTRL_EXT);
1053 ew32(CTRL_EXT,
1054 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1059 * @e1000_alloc_ring - allocate memory for a ring structure
1061 static int e1000_alloc_ring_dma(struct e1000_adapter *adapter,
1062 struct e1000_ring *ring)
1064 struct pci_dev *pdev = adapter->pdev;
1066 ring->desc = dma_alloc_coherent(&pdev->dev, ring->size, &ring->dma,
1067 GFP_KERNEL);
1068 if (!ring->desc)
1069 return -ENOMEM;
1071 return 0;
1075 * e1000e_setup_tx_resources - allocate Tx resources (Descriptors)
1076 * @adapter: board private structure
1078 * Return 0 on success, negative on failure
1080 int e1000e_setup_tx_resources(struct e1000_adapter *adapter)
1082 struct e1000_ring *tx_ring = adapter->tx_ring;
1083 int err = -ENOMEM, size;
1085 size = sizeof(struct e1000_buffer) * tx_ring->count;
1086 tx_ring->buffer_info = vmalloc(size);
1087 if (!tx_ring->buffer_info)
1088 goto err;
1089 memset(tx_ring->buffer_info, 0, size);
1091 /* round up to nearest 4K */
1092 tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
1093 tx_ring->size = ALIGN(tx_ring->size, 4096);
1095 err = e1000_alloc_ring_dma(adapter, tx_ring);
1096 if (err)
1097 goto err;
1099 tx_ring->next_to_use = 0;
1100 tx_ring->next_to_clean = 0;
1101 spin_lock_init(&adapter->tx_queue_lock);
1103 return 0;
1104 err:
1105 vfree(tx_ring->buffer_info);
1106 ndev_err(adapter->netdev,
1107 "Unable to allocate memory for the transmit descriptor ring\n");
1108 return err;
1112 * e1000e_setup_rx_resources - allocate Rx resources (Descriptors)
1113 * @adapter: board private structure
1115 * Returns 0 on success, negative on failure
1117 int e1000e_setup_rx_resources(struct e1000_adapter *adapter)
1119 struct e1000_ring *rx_ring = adapter->rx_ring;
1120 struct e1000_buffer *buffer_info;
1121 int i, size, desc_len, err = -ENOMEM;
1123 size = sizeof(struct e1000_buffer) * rx_ring->count;
1124 rx_ring->buffer_info = vmalloc(size);
1125 if (!rx_ring->buffer_info)
1126 goto err;
1127 memset(rx_ring->buffer_info, 0, size);
1129 for (i = 0; i < rx_ring->count; i++) {
1130 buffer_info = &rx_ring->buffer_info[i];
1131 buffer_info->ps_pages = kcalloc(PS_PAGE_BUFFERS,
1132 sizeof(struct e1000_ps_page),
1133 GFP_KERNEL);
1134 if (!buffer_info->ps_pages)
1135 goto err_pages;
1138 desc_len = sizeof(union e1000_rx_desc_packet_split);
1140 /* Round up to nearest 4K */
1141 rx_ring->size = rx_ring->count * desc_len;
1142 rx_ring->size = ALIGN(rx_ring->size, 4096);
1144 err = e1000_alloc_ring_dma(adapter, rx_ring);
1145 if (err)
1146 goto err_pages;
1148 rx_ring->next_to_clean = 0;
1149 rx_ring->next_to_use = 0;
1150 rx_ring->rx_skb_top = NULL;
1152 return 0;
1154 err_pages:
1155 for (i = 0; i < rx_ring->count; i++) {
1156 buffer_info = &rx_ring->buffer_info[i];
1157 kfree(buffer_info->ps_pages);
1159 err:
1160 vfree(rx_ring->buffer_info);
1161 ndev_err(adapter->netdev,
1162 "Unable to allocate memory for the transmit descriptor ring\n");
1163 return err;
1167 * e1000_clean_tx_ring - Free Tx Buffers
1168 * @adapter: board private structure
1170 static void e1000_clean_tx_ring(struct e1000_adapter *adapter)
1172 struct e1000_ring *tx_ring = adapter->tx_ring;
1173 struct e1000_buffer *buffer_info;
1174 unsigned long size;
1175 unsigned int i;
1177 for (i = 0; i < tx_ring->count; i++) {
1178 buffer_info = &tx_ring->buffer_info[i];
1179 e1000_put_txbuf(adapter, buffer_info);
1182 size = sizeof(struct e1000_buffer) * tx_ring->count;
1183 memset(tx_ring->buffer_info, 0, size);
1185 memset(tx_ring->desc, 0, tx_ring->size);
1187 tx_ring->next_to_use = 0;
1188 tx_ring->next_to_clean = 0;
1190 writel(0, adapter->hw.hw_addr + tx_ring->head);
1191 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1195 * e1000e_free_tx_resources - Free Tx Resources per Queue
1196 * @adapter: board private structure
1198 * Free all transmit software resources
1200 void e1000e_free_tx_resources(struct e1000_adapter *adapter)
1202 struct pci_dev *pdev = adapter->pdev;
1203 struct e1000_ring *tx_ring = adapter->tx_ring;
1205 e1000_clean_tx_ring(adapter);
1207 vfree(tx_ring->buffer_info);
1208 tx_ring->buffer_info = NULL;
1210 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1211 tx_ring->dma);
1212 tx_ring->desc = NULL;
1216 * e1000e_free_rx_resources - Free Rx Resources
1217 * @adapter: board private structure
1219 * Free all receive software resources
1222 void e1000e_free_rx_resources(struct e1000_adapter *adapter)
1224 struct pci_dev *pdev = adapter->pdev;
1225 struct e1000_ring *rx_ring = adapter->rx_ring;
1226 int i;
1228 e1000_clean_rx_ring(adapter);
1230 for (i = 0; i < rx_ring->count; i++) {
1231 kfree(rx_ring->buffer_info[i].ps_pages);
1234 vfree(rx_ring->buffer_info);
1235 rx_ring->buffer_info = NULL;
1237 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
1238 rx_ring->dma);
1239 rx_ring->desc = NULL;
1243 * e1000_update_itr - update the dynamic ITR value based on statistics
1244 * @adapter: pointer to adapter
1245 * @itr_setting: current adapter->itr
1246 * @packets: the number of packets during this measurement interval
1247 * @bytes: the number of bytes during this measurement interval
1249 * Stores a new ITR value based on packets and byte
1250 * counts during the last interrupt. The advantage of per interrupt
1251 * computation is faster updates and more accurate ITR for the current
1252 * traffic pattern. Constants in this function were computed
1253 * based on theoretical maximum wire speed and thresholds were set based
1254 * on testing data as well as attempting to minimize response time
1255 * while increasing bulk throughput.
1256 * this functionality is controlled by the InterruptThrottleRate module
1257 * parameter (see e1000_param.c)
1259 static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
1260 u16 itr_setting, int packets,
1261 int bytes)
1263 unsigned int retval = itr_setting;
1265 if (packets == 0)
1266 goto update_itr_done;
1268 switch (itr_setting) {
1269 case lowest_latency:
1270 /* handle TSO and jumbo frames */
1271 if (bytes/packets > 8000)
1272 retval = bulk_latency;
1273 else if ((packets < 5) && (bytes > 512)) {
1274 retval = low_latency;
1276 break;
1277 case low_latency: /* 50 usec aka 20000 ints/s */
1278 if (bytes > 10000) {
1279 /* this if handles the TSO accounting */
1280 if (bytes/packets > 8000) {
1281 retval = bulk_latency;
1282 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
1283 retval = bulk_latency;
1284 } else if ((packets > 35)) {
1285 retval = lowest_latency;
1287 } else if (bytes/packets > 2000) {
1288 retval = bulk_latency;
1289 } else if (packets <= 2 && bytes < 512) {
1290 retval = lowest_latency;
1292 break;
1293 case bulk_latency: /* 250 usec aka 4000 ints/s */
1294 if (bytes > 25000) {
1295 if (packets > 35) {
1296 retval = low_latency;
1298 } else if (bytes < 6000) {
1299 retval = low_latency;
1301 break;
1304 update_itr_done:
1305 return retval;
1308 static void e1000_set_itr(struct e1000_adapter *adapter)
1310 struct e1000_hw *hw = &adapter->hw;
1311 u16 current_itr;
1312 u32 new_itr = adapter->itr;
1314 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
1315 if (adapter->link_speed != SPEED_1000) {
1316 current_itr = 0;
1317 new_itr = 4000;
1318 goto set_itr_now;
1321 adapter->tx_itr = e1000_update_itr(adapter,
1322 adapter->tx_itr,
1323 adapter->total_tx_packets,
1324 adapter->total_tx_bytes);
1325 /* conservative mode (itr 3) eliminates the lowest_latency setting */
1326 if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
1327 adapter->tx_itr = low_latency;
1329 adapter->rx_itr = e1000_update_itr(adapter,
1330 adapter->rx_itr,
1331 adapter->total_rx_packets,
1332 adapter->total_rx_bytes);
1333 /* conservative mode (itr 3) eliminates the lowest_latency setting */
1334 if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
1335 adapter->rx_itr = low_latency;
1337 current_itr = max(adapter->rx_itr, adapter->tx_itr);
1339 switch (current_itr) {
1340 /* counts and packets in update_itr are dependent on these numbers */
1341 case lowest_latency:
1342 new_itr = 70000;
1343 break;
1344 case low_latency:
1345 new_itr = 20000; /* aka hwitr = ~200 */
1346 break;
1347 case bulk_latency:
1348 new_itr = 4000;
1349 break;
1350 default:
1351 break;
1354 set_itr_now:
1355 if (new_itr != adapter->itr) {
1356 /* this attempts to bias the interrupt rate towards Bulk
1357 * by adding intermediate steps when interrupt rate is
1358 * increasing */
1359 new_itr = new_itr > adapter->itr ?
1360 min(adapter->itr + (new_itr >> 2), new_itr) :
1361 new_itr;
1362 adapter->itr = new_itr;
1363 ew32(ITR, 1000000000 / (new_itr * 256));
1368 * e1000_clean - NAPI Rx polling callback
1369 * @adapter: board private structure
1370 * @budget: amount of packets driver is allowed to process this poll
1372 static int e1000_clean(struct napi_struct *napi, int budget)
1374 struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
1375 struct net_device *poll_dev = adapter->netdev;
1376 int tx_cleaned = 0, work_done = 0;
1378 /* Must NOT use netdev_priv macro here. */
1379 adapter = poll_dev->priv;
1381 /* e1000_clean is called per-cpu. This lock protects
1382 * tx_ring from being cleaned by multiple cpus
1383 * simultaneously. A failure obtaining the lock means
1384 * tx_ring is currently being cleaned anyway. */
1385 if (spin_trylock(&adapter->tx_queue_lock)) {
1386 tx_cleaned = e1000_clean_tx_irq(adapter);
1387 spin_unlock(&adapter->tx_queue_lock);
1390 adapter->clean_rx(adapter, &work_done, budget);
1392 if (tx_cleaned)
1393 work_done = budget;
1395 /* If budget not fully consumed, exit the polling mode */
1396 if (work_done < budget) {
1397 if (adapter->itr_setting & 3)
1398 e1000_set_itr(adapter);
1399 netif_rx_complete(poll_dev, napi);
1400 e1000_irq_enable(adapter);
1403 return work_done;
1406 static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1408 struct e1000_adapter *adapter = netdev_priv(netdev);
1409 struct e1000_hw *hw = &adapter->hw;
1410 u32 vfta, index;
1412 /* don't update vlan cookie if already programmed */
1413 if ((adapter->hw.mng_cookie.status &
1414 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
1415 (vid == adapter->mng_vlan_id))
1416 return;
1417 /* add VID to filter table */
1418 index = (vid >> 5) & 0x7F;
1419 vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
1420 vfta |= (1 << (vid & 0x1F));
1421 e1000e_write_vfta(hw, index, vfta);
1424 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1426 struct e1000_adapter *adapter = netdev_priv(netdev);
1427 struct e1000_hw *hw = &adapter->hw;
1428 u32 vfta, index;
1430 e1000_irq_disable(adapter);
1431 vlan_group_set_device(adapter->vlgrp, vid, NULL);
1432 e1000_irq_enable(adapter);
1434 if ((adapter->hw.mng_cookie.status &
1435 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
1436 (vid == adapter->mng_vlan_id)) {
1437 /* release control to f/w */
1438 e1000_release_hw_control(adapter);
1439 return;
1442 /* remove VID from filter table */
1443 index = (vid >> 5) & 0x7F;
1444 vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
1445 vfta &= ~(1 << (vid & 0x1F));
1446 e1000e_write_vfta(hw, index, vfta);
1449 static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
1451 struct net_device *netdev = adapter->netdev;
1452 u16 vid = adapter->hw.mng_cookie.vlan_id;
1453 u16 old_vid = adapter->mng_vlan_id;
1455 if (!adapter->vlgrp)
1456 return;
1458 if (!vlan_group_get_device(adapter->vlgrp, vid)) {
1459 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
1460 if (adapter->hw.mng_cookie.status &
1461 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1462 e1000_vlan_rx_add_vid(netdev, vid);
1463 adapter->mng_vlan_id = vid;
1466 if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
1467 (vid != old_vid) &&
1468 !vlan_group_get_device(adapter->vlgrp, old_vid))
1469 e1000_vlan_rx_kill_vid(netdev, old_vid);
1470 } else {
1471 adapter->mng_vlan_id = vid;
1476 static void e1000_vlan_rx_register(struct net_device *netdev,
1477 struct vlan_group *grp)
1479 struct e1000_adapter *adapter = netdev_priv(netdev);
1480 struct e1000_hw *hw = &adapter->hw;
1481 u32 ctrl, rctl;
1483 e1000_irq_disable(adapter);
1484 adapter->vlgrp = grp;
1486 if (grp) {
1487 /* enable VLAN tag insert/strip */
1488 ctrl = er32(CTRL);
1489 ctrl |= E1000_CTRL_VME;
1490 ew32(CTRL, ctrl);
1492 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
1493 /* enable VLAN receive filtering */
1494 rctl = er32(RCTL);
1495 rctl |= E1000_RCTL_VFE;
1496 rctl &= ~E1000_RCTL_CFIEN;
1497 ew32(RCTL, rctl);
1498 e1000_update_mng_vlan(adapter);
1500 } else {
1501 /* disable VLAN tag insert/strip */
1502 ctrl = er32(CTRL);
1503 ctrl &= ~E1000_CTRL_VME;
1504 ew32(CTRL, ctrl);
1506 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
1507 /* disable VLAN filtering */
1508 rctl = er32(RCTL);
1509 rctl &= ~E1000_RCTL_VFE;
1510 ew32(RCTL, rctl);
1511 if (adapter->mng_vlan_id !=
1512 (u16)E1000_MNG_VLAN_NONE) {
1513 e1000_vlan_rx_kill_vid(netdev,
1514 adapter->mng_vlan_id);
1515 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
1520 e1000_irq_enable(adapter);
1523 static void e1000_restore_vlan(struct e1000_adapter *adapter)
1525 u16 vid;
1527 e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1529 if (!adapter->vlgrp)
1530 return;
1532 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1533 if (!vlan_group_get_device(adapter->vlgrp, vid))
1534 continue;
1535 e1000_vlan_rx_add_vid(adapter->netdev, vid);
1539 static void e1000_init_manageability(struct e1000_adapter *adapter)
1541 struct e1000_hw *hw = &adapter->hw;
1542 u32 manc, manc2h;
1544 if (!(adapter->flags & FLAG_MNG_PT_ENABLED))
1545 return;
1547 manc = er32(MANC);
1549 /* enable receiving management packets to the host. this will probably
1550 * generate destination unreachable messages from the host OS, but
1551 * the packets will be handled on SMBUS */
1552 manc |= E1000_MANC_EN_MNG2HOST;
1553 manc2h = er32(MANC2H);
1554 #define E1000_MNG2HOST_PORT_623 (1 << 5)
1555 #define E1000_MNG2HOST_PORT_664 (1 << 6)
1556 manc2h |= E1000_MNG2HOST_PORT_623;
1557 manc2h |= E1000_MNG2HOST_PORT_664;
1558 ew32(MANC2H, manc2h);
1559 ew32(MANC, manc);
1563 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1564 * @adapter: board private structure
1566 * Configure the Tx unit of the MAC after a reset.
1568 static void e1000_configure_tx(struct e1000_adapter *adapter)
1570 struct e1000_hw *hw = &adapter->hw;
1571 struct e1000_ring *tx_ring = adapter->tx_ring;
1572 u64 tdba;
1573 u32 tdlen, tctl, tipg, tarc;
1574 u32 ipgr1, ipgr2;
1576 /* Setup the HW Tx Head and Tail descriptor pointers */
1577 tdba = tx_ring->dma;
1578 tdlen = tx_ring->count * sizeof(struct e1000_tx_desc);
1579 ew32(TDBAL, (tdba & DMA_32BIT_MASK));
1580 ew32(TDBAH, (tdba >> 32));
1581 ew32(TDLEN, tdlen);
1582 ew32(TDH, 0);
1583 ew32(TDT, 0);
1584 tx_ring->head = E1000_TDH;
1585 tx_ring->tail = E1000_TDT;
1587 /* Set the default values for the Tx Inter Packet Gap timer */
1588 tipg = DEFAULT_82543_TIPG_IPGT_COPPER; /* 8 */
1589 ipgr1 = DEFAULT_82543_TIPG_IPGR1; /* 8 */
1590 ipgr2 = DEFAULT_82543_TIPG_IPGR2; /* 6 */
1592 if (adapter->flags & FLAG_TIPG_MEDIUM_FOR_80003ESLAN)
1593 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2; /* 7 */
1595 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1596 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
1597 ew32(TIPG, tipg);
1599 /* Set the Tx Interrupt Delay register */
1600 ew32(TIDV, adapter->tx_int_delay);
1601 /* tx irq moderation */
1602 ew32(TADV, adapter->tx_abs_int_delay);
1604 /* Program the Transmit Control Register */
1605 tctl = er32(TCTL);
1606 tctl &= ~E1000_TCTL_CT;
1607 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
1608 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1610 if (adapter->flags & FLAG_TARC_SPEED_MODE_BIT) {
1611 tarc = er32(TARC0);
1612 /* set the speed mode bit, we'll clear it if we're not at
1613 * gigabit link later */
1614 #define SPEED_MODE_BIT (1 << 21)
1615 tarc |= SPEED_MODE_BIT;
1616 ew32(TARC0, tarc);
1619 /* errata: program both queues to unweighted RR */
1620 if (adapter->flags & FLAG_TARC_SET_BIT_ZERO) {
1621 tarc = er32(TARC0);
1622 tarc |= 1;
1623 ew32(TARC0, tarc);
1624 tarc = er32(TARC1);
1625 tarc |= 1;
1626 ew32(TARC1, tarc);
1629 e1000e_config_collision_dist(hw);
1631 /* Setup Transmit Descriptor Settings for eop descriptor */
1632 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
1634 /* only set IDE if we are delaying interrupts using the timers */
1635 if (adapter->tx_int_delay)
1636 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
1638 /* enable Report Status bit */
1639 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1641 ew32(TCTL, tctl);
1643 adapter->tx_queue_len = adapter->netdev->tx_queue_len;
1647 * e1000_setup_rctl - configure the receive control registers
1648 * @adapter: Board private structure
1650 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1651 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
1652 static void e1000_setup_rctl(struct e1000_adapter *adapter)
1654 struct e1000_hw *hw = &adapter->hw;
1655 u32 rctl, rfctl;
1656 u32 psrctl = 0;
1657 u32 pages = 0;
1659 /* Program MC offset vector base */
1660 rctl = er32(RCTL);
1661 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1662 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1663 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1664 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1666 /* Do not Store bad packets */
1667 rctl &= ~E1000_RCTL_SBP;
1669 /* Enable Long Packet receive */
1670 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1671 rctl &= ~E1000_RCTL_LPE;
1672 else
1673 rctl |= E1000_RCTL_LPE;
1675 /* Enable hardware CRC frame stripping */
1676 rctl |= E1000_RCTL_SECRC;
1678 /* Setup buffer sizes */
1679 rctl &= ~E1000_RCTL_SZ_4096;
1680 rctl |= E1000_RCTL_BSEX;
1681 switch (adapter->rx_buffer_len) {
1682 case 256:
1683 rctl |= E1000_RCTL_SZ_256;
1684 rctl &= ~E1000_RCTL_BSEX;
1685 break;
1686 case 512:
1687 rctl |= E1000_RCTL_SZ_512;
1688 rctl &= ~E1000_RCTL_BSEX;
1689 break;
1690 case 1024:
1691 rctl |= E1000_RCTL_SZ_1024;
1692 rctl &= ~E1000_RCTL_BSEX;
1693 break;
1694 case 2048:
1695 default:
1696 rctl |= E1000_RCTL_SZ_2048;
1697 rctl &= ~E1000_RCTL_BSEX;
1698 break;
1699 case 4096:
1700 rctl |= E1000_RCTL_SZ_4096;
1701 break;
1702 case 8192:
1703 rctl |= E1000_RCTL_SZ_8192;
1704 break;
1705 case 16384:
1706 rctl |= E1000_RCTL_SZ_16384;
1707 break;
1711 * 82571 and greater support packet-split where the protocol
1712 * header is placed in skb->data and the packet data is
1713 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
1714 * In the case of a non-split, skb->data is linearly filled,
1715 * followed by the page buffers. Therefore, skb->data is
1716 * sized to hold the largest protocol header.
1718 * allocations using alloc_page take too long for regular MTU
1719 * so only enable packet split for jumbo frames
1721 * Using pages when the page size is greater than 16k wastes
1722 * a lot of memory, since we allocate 3 pages at all times
1723 * per packet.
1725 adapter->rx_ps_pages = 0;
1726 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1727 if ((pages <= 3) && (PAGE_SIZE <= 16384) && (rctl & E1000_RCTL_LPE))
1728 adapter->rx_ps_pages = pages;
1730 if (adapter->rx_ps_pages) {
1731 /* Configure extra packet-split registers */
1732 rfctl = er32(RFCTL);
1733 rfctl |= E1000_RFCTL_EXTEN;
1734 /* disable packet split support for IPv6 extension headers,
1735 * because some malformed IPv6 headers can hang the RX */
1736 rfctl |= (E1000_RFCTL_IPV6_EX_DIS |
1737 E1000_RFCTL_NEW_IPV6_EXT_DIS);
1739 ew32(RFCTL, rfctl);
1741 /* Enable Packet split descriptors */
1742 rctl |= E1000_RCTL_DTYP_PS;
1744 psrctl |= adapter->rx_ps_bsize0 >>
1745 E1000_PSRCTL_BSIZE0_SHIFT;
1747 switch (adapter->rx_ps_pages) {
1748 case 3:
1749 psrctl |= PAGE_SIZE <<
1750 E1000_PSRCTL_BSIZE3_SHIFT;
1751 case 2:
1752 psrctl |= PAGE_SIZE <<
1753 E1000_PSRCTL_BSIZE2_SHIFT;
1754 case 1:
1755 psrctl |= PAGE_SIZE >>
1756 E1000_PSRCTL_BSIZE1_SHIFT;
1757 break;
1760 ew32(PSRCTL, psrctl);
1763 ew32(RCTL, rctl);
1767 * e1000_configure_rx - Configure Receive Unit after Reset
1768 * @adapter: board private structure
1770 * Configure the Rx unit of the MAC after a reset.
1772 static void e1000_configure_rx(struct e1000_adapter *adapter)
1774 struct e1000_hw *hw = &adapter->hw;
1775 struct e1000_ring *rx_ring = adapter->rx_ring;
1776 u64 rdba;
1777 u32 rdlen, rctl, rxcsum, ctrl_ext;
1779 if (adapter->rx_ps_pages) {
1780 /* this is a 32 byte descriptor */
1781 rdlen = rx_ring->count *
1782 sizeof(union e1000_rx_desc_packet_split);
1783 adapter->clean_rx = e1000_clean_rx_irq_ps;
1784 adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
1785 } else {
1786 rdlen = rx_ring->count *
1787 sizeof(struct e1000_rx_desc);
1788 adapter->clean_rx = e1000_clean_rx_irq;
1789 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1792 /* disable receives while setting up the descriptors */
1793 rctl = er32(RCTL);
1794 ew32(RCTL, rctl & ~E1000_RCTL_EN);
1795 e1e_flush();
1796 msleep(10);
1798 /* set the Receive Delay Timer Register */
1799 ew32(RDTR, adapter->rx_int_delay);
1801 /* irq moderation */
1802 ew32(RADV, adapter->rx_abs_int_delay);
1803 if (adapter->itr_setting != 0)
1804 ew32(ITR,
1805 1000000000 / (adapter->itr * 256));
1807 ctrl_ext = er32(CTRL_EXT);
1808 /* Reset delay timers after every interrupt */
1809 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
1810 /* Auto-Mask interrupts upon ICR access */
1811 ctrl_ext |= E1000_CTRL_EXT_IAME;
1812 ew32(IAM, 0xffffffff);
1813 ew32(CTRL_EXT, ctrl_ext);
1814 e1e_flush();
1816 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1817 * the Base and Length of the Rx Descriptor Ring */
1818 rdba = rx_ring->dma;
1819 ew32(RDBAL, (rdba & DMA_32BIT_MASK));
1820 ew32(RDBAH, (rdba >> 32));
1821 ew32(RDLEN, rdlen);
1822 ew32(RDH, 0);
1823 ew32(RDT, 0);
1824 rx_ring->head = E1000_RDH;
1825 rx_ring->tail = E1000_RDT;
1827 /* Enable Receive Checksum Offload for TCP and UDP */
1828 rxcsum = er32(RXCSUM);
1829 if (adapter->flags & FLAG_RX_CSUM_ENABLED) {
1830 rxcsum |= E1000_RXCSUM_TUOFL;
1832 /* IPv4 payload checksum for UDP fragments must be
1833 * used in conjunction with packet-split. */
1834 if (adapter->rx_ps_pages)
1835 rxcsum |= E1000_RXCSUM_IPPCSE;
1836 } else {
1837 rxcsum &= ~E1000_RXCSUM_TUOFL;
1838 /* no need to clear IPPCSE as it defaults to 0 */
1840 ew32(RXCSUM, rxcsum);
1842 /* Enable early receives on supported devices, only takes effect when
1843 * packet size is equal or larger than the specified value (in 8 byte
1844 * units), e.g. using jumbo frames when setting to E1000_ERT_2048 */
1845 if ((adapter->flags & FLAG_HAS_ERT) &&
1846 (adapter->netdev->mtu > ETH_DATA_LEN))
1847 ew32(ERT, E1000_ERT_2048);
1849 /* Enable Receives */
1850 ew32(RCTL, rctl);
1854 * e1000_mc_addr_list_update - Update Multicast addresses
1855 * @hw: pointer to the HW structure
1856 * @mc_addr_list: array of multicast addresses to program
1857 * @mc_addr_count: number of multicast addresses to program
1858 * @rar_used_count: the first RAR register free to program
1859 * @rar_count: total number of supported Receive Address Registers
1861 * Updates the Receive Address Registers and Multicast Table Array.
1862 * The caller must have a packed mc_addr_list of multicast addresses.
1863 * The parameter rar_count will usually be hw->mac.rar_entry_count
1864 * unless there are workarounds that change this. Currently no func pointer
1865 * exists and all implementations are handled in the generic version of this
1866 * function.
1868 static void e1000_mc_addr_list_update(struct e1000_hw *hw, u8 *mc_addr_list,
1869 u32 mc_addr_count, u32 rar_used_count,
1870 u32 rar_count)
1872 hw->mac.ops.mc_addr_list_update(hw, mc_addr_list, mc_addr_count,
1873 rar_used_count, rar_count);
1877 * e1000_set_multi - Multicast and Promiscuous mode set
1878 * @netdev: network interface device structure
1880 * The set_multi entry point is called whenever the multicast address
1881 * list or the network interface flags are updated. This routine is
1882 * responsible for configuring the hardware for proper multicast,
1883 * promiscuous mode, and all-multi behavior.
1885 static void e1000_set_multi(struct net_device *netdev)
1887 struct e1000_adapter *adapter = netdev_priv(netdev);
1888 struct e1000_hw *hw = &adapter->hw;
1889 struct e1000_mac_info *mac = &hw->mac;
1890 struct dev_mc_list *mc_ptr;
1891 u8 *mta_list;
1892 u32 rctl;
1893 int i;
1895 /* Check for Promiscuous and All Multicast modes */
1897 rctl = er32(RCTL);
1899 if (netdev->flags & IFF_PROMISC) {
1900 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1901 } else if (netdev->flags & IFF_ALLMULTI) {
1902 rctl |= E1000_RCTL_MPE;
1903 rctl &= ~E1000_RCTL_UPE;
1904 } else {
1905 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
1908 ew32(RCTL, rctl);
1910 if (netdev->mc_count) {
1911 mta_list = kmalloc(netdev->mc_count * 6, GFP_ATOMIC);
1912 if (!mta_list)
1913 return;
1915 /* prepare a packed array of only addresses. */
1916 mc_ptr = netdev->mc_list;
1918 for (i = 0; i < netdev->mc_count; i++) {
1919 if (!mc_ptr)
1920 break;
1921 memcpy(mta_list + (i*ETH_ALEN), mc_ptr->dmi_addr,
1922 ETH_ALEN);
1923 mc_ptr = mc_ptr->next;
1926 e1000_mc_addr_list_update(hw, mta_list, i, 1,
1927 mac->rar_entry_count);
1928 kfree(mta_list);
1929 } else {
1931 * if we're called from probe, we might not have
1932 * anything to do here, so clear out the list
1934 e1000_mc_addr_list_update(hw, NULL, 0, 1,
1935 mac->rar_entry_count);
1940 * e1000_configure - configure the hardware for RX and TX
1941 * @adapter: private board structure
1943 static void e1000_configure(struct e1000_adapter *adapter)
1945 e1000_set_multi(adapter->netdev);
1947 e1000_restore_vlan(adapter);
1948 e1000_init_manageability(adapter);
1950 e1000_configure_tx(adapter);
1951 e1000_setup_rctl(adapter);
1952 e1000_configure_rx(adapter);
1953 adapter->alloc_rx_buf(adapter,
1954 e1000_desc_unused(adapter->rx_ring));
1958 * e1000e_power_up_phy - restore link in case the phy was powered down
1959 * @adapter: address of board private structure
1961 * The phy may be powered down to save power and turn off link when the
1962 * driver is unloaded and wake on lan is not enabled (among others)
1963 * *** this routine MUST be followed by a call to e1000e_reset ***
1965 void e1000e_power_up_phy(struct e1000_adapter *adapter)
1967 u16 mii_reg = 0;
1969 /* Just clear the power down bit to wake the phy back up */
1970 if (adapter->hw.media_type == e1000_media_type_copper) {
1971 /* according to the manual, the phy will retain its
1972 * settings across a power-down/up cycle */
1973 e1e_rphy(&adapter->hw, PHY_CONTROL, &mii_reg);
1974 mii_reg &= ~MII_CR_POWER_DOWN;
1975 e1e_wphy(&adapter->hw, PHY_CONTROL, mii_reg);
1978 adapter->hw.mac.ops.setup_link(&adapter->hw);
1982 * e1000_power_down_phy - Power down the PHY
1984 * Power down the PHY so no link is implied when interface is down
1985 * The PHY cannot be powered down is management or WoL is active
1987 static void e1000_power_down_phy(struct e1000_adapter *adapter)
1989 struct e1000_hw *hw = &adapter->hw;
1990 u16 mii_reg;
1992 /* WoL is enabled */
1993 if (adapter->wol)
1994 return;
1996 /* non-copper PHY? */
1997 if (adapter->hw.media_type != e1000_media_type_copper)
1998 return;
2000 /* reset is blocked because of a SoL/IDER session */
2001 if (e1000e_check_mng_mode(hw) ||
2002 e1000_check_reset_block(hw))
2003 return;
2005 /* manageability (AMT) is enabled */
2006 if (er32(MANC) & E1000_MANC_SMBUS_EN)
2007 return;
2009 /* power down the PHY */
2010 e1e_rphy(hw, PHY_CONTROL, &mii_reg);
2011 mii_reg |= MII_CR_POWER_DOWN;
2012 e1e_wphy(hw, PHY_CONTROL, mii_reg);
2013 mdelay(1);
2017 * e1000e_reset - bring the hardware into a known good state
2019 * This function boots the hardware and enables some settings that
2020 * require a configuration cycle of the hardware - those cannot be
2021 * set/changed during runtime. After reset the device needs to be
2022 * properly configured for rx, tx etc.
2024 void e1000e_reset(struct e1000_adapter *adapter)
2026 struct e1000_mac_info *mac = &adapter->hw.mac;
2027 struct e1000_hw *hw = &adapter->hw;
2028 u32 tx_space, min_tx_space, min_rx_space;
2029 u32 pba;
2030 u16 hwm;
2032 ew32(PBA, adapter->pba);
2034 if (mac->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN ) {
2035 /* To maintain wire speed transmits, the Tx FIFO should be
2036 * large enough to accommodate two full transmit packets,
2037 * rounded up to the next 1KB and expressed in KB. Likewise,
2038 * the Rx FIFO should be large enough to accommodate at least
2039 * one full receive packet and is similarly rounded up and
2040 * expressed in KB. */
2041 pba = er32(PBA);
2042 /* upper 16 bits has Tx packet buffer allocation size in KB */
2043 tx_space = pba >> 16;
2044 /* lower 16 bits has Rx packet buffer allocation size in KB */
2045 pba &= 0xffff;
2046 /* the tx fifo also stores 16 bytes of information about the tx
2047 * but don't include ethernet FCS because hardware appends it */
2048 min_tx_space = (mac->max_frame_size +
2049 sizeof(struct e1000_tx_desc) -
2050 ETH_FCS_LEN) * 2;
2051 min_tx_space = ALIGN(min_tx_space, 1024);
2052 min_tx_space >>= 10;
2053 /* software strips receive CRC, so leave room for it */
2054 min_rx_space = mac->max_frame_size;
2055 min_rx_space = ALIGN(min_rx_space, 1024);
2056 min_rx_space >>= 10;
2058 /* If current Tx allocation is less than the min Tx FIFO size,
2059 * and the min Tx FIFO size is less than the current Rx FIFO
2060 * allocation, take space away from current Rx allocation */
2061 if ((tx_space < min_tx_space) &&
2062 ((min_tx_space - tx_space) < pba)) {
2063 pba -= min_tx_space - tx_space;
2065 /* if short on rx space, rx wins and must trump tx
2066 * adjustment or use Early Receive if available */
2067 if ((pba < min_rx_space) &&
2068 (!(adapter->flags & FLAG_HAS_ERT)))
2069 /* ERT enabled in e1000_configure_rx */
2070 pba = min_rx_space;
2073 ew32(PBA, pba);
2077 /* flow control settings */
2078 /* The high water mark must be low enough to fit one full frame
2079 * (or the size used for early receive) above it in the Rx FIFO.
2080 * Set it to the lower of:
2081 * - 90% of the Rx FIFO size, and
2082 * - the full Rx FIFO size minus the early receive size (for parts
2083 * with ERT support assuming ERT set to E1000_ERT_2048), or
2084 * - the full Rx FIFO size minus one full frame */
2085 if (adapter->flags & FLAG_HAS_ERT)
2086 hwm = min(((adapter->pba << 10) * 9 / 10),
2087 ((adapter->pba << 10) - (E1000_ERT_2048 << 3)));
2088 else
2089 hwm = min(((adapter->pba << 10) * 9 / 10),
2090 ((adapter->pba << 10) - mac->max_frame_size));
2092 mac->fc_high_water = hwm & 0xFFF8; /* 8-byte granularity */
2093 mac->fc_low_water = mac->fc_high_water - 8;
2095 if (adapter->flags & FLAG_DISABLE_FC_PAUSE_TIME)
2096 mac->fc_pause_time = 0xFFFF;
2097 else
2098 mac->fc_pause_time = E1000_FC_PAUSE_TIME;
2099 mac->fc = mac->original_fc;
2101 /* Allow time for pending master requests to run */
2102 mac->ops.reset_hw(hw);
2103 ew32(WUC, 0);
2105 if (mac->ops.init_hw(hw))
2106 ndev_err(adapter->netdev, "Hardware Error\n");
2108 e1000_update_mng_vlan(adapter);
2110 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
2111 ew32(VET, ETH_P_8021Q);
2113 e1000e_reset_adaptive(hw);
2114 e1000_get_phy_info(hw);
2116 if (!(adapter->flags & FLAG_SMART_POWER_DOWN)) {
2117 u16 phy_data = 0;
2118 /* speed up time to link by disabling smart power down, ignore
2119 * the return value of this function because there is nothing
2120 * different we would do if it failed */
2121 e1e_rphy(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
2122 phy_data &= ~IGP02E1000_PM_SPD;
2123 e1e_wphy(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
2127 int e1000e_up(struct e1000_adapter *adapter)
2129 struct e1000_hw *hw = &adapter->hw;
2131 /* hardware has been reset, we need to reload some things */
2132 e1000_configure(adapter);
2134 clear_bit(__E1000_DOWN, &adapter->state);
2136 napi_enable(&adapter->napi);
2137 e1000_irq_enable(adapter);
2139 /* fire a link change interrupt to start the watchdog */
2140 ew32(ICS, E1000_ICS_LSC);
2141 return 0;
2144 void e1000e_down(struct e1000_adapter *adapter)
2146 struct net_device *netdev = adapter->netdev;
2147 struct e1000_hw *hw = &adapter->hw;
2148 u32 tctl, rctl;
2150 /* signal that we're down so the interrupt handler does not
2151 * reschedule our watchdog timer */
2152 set_bit(__E1000_DOWN, &adapter->state);
2154 /* disable receives in the hardware */
2155 rctl = er32(RCTL);
2156 ew32(RCTL, rctl & ~E1000_RCTL_EN);
2157 /* flush and sleep below */
2159 netif_stop_queue(netdev);
2161 /* disable transmits in the hardware */
2162 tctl = er32(TCTL);
2163 tctl &= ~E1000_TCTL_EN;
2164 ew32(TCTL, tctl);
2165 /* flush both disables and wait for them to finish */
2166 e1e_flush();
2167 msleep(10);
2169 napi_disable(&adapter->napi);
2170 atomic_set(&adapter->irq_sem, 0);
2171 e1000_irq_disable(adapter);
2173 del_timer_sync(&adapter->watchdog_timer);
2174 del_timer_sync(&adapter->phy_info_timer);
2176 netdev->tx_queue_len = adapter->tx_queue_len;
2177 netif_carrier_off(netdev);
2178 adapter->link_speed = 0;
2179 adapter->link_duplex = 0;
2181 e1000e_reset(adapter);
2182 e1000_clean_tx_ring(adapter);
2183 e1000_clean_rx_ring(adapter);
2186 * TODO: for power management, we could drop the link and
2187 * pci_disable_device here.
2191 void e1000e_reinit_locked(struct e1000_adapter *adapter)
2193 might_sleep();
2194 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
2195 msleep(1);
2196 e1000e_down(adapter);
2197 e1000e_up(adapter);
2198 clear_bit(__E1000_RESETTING, &adapter->state);
2202 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
2203 * @adapter: board private structure to initialize
2205 * e1000_sw_init initializes the Adapter private data structure.
2206 * Fields are initialized based on PCI device information and
2207 * OS network device settings (MTU size).
2209 static int __devinit e1000_sw_init(struct e1000_adapter *adapter)
2211 struct e1000_hw *hw = &adapter->hw;
2212 struct net_device *netdev = adapter->netdev;
2214 adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
2215 adapter->rx_ps_bsize0 = 128;
2216 hw->mac.max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
2217 hw->mac.min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2219 adapter->tx_ring = kzalloc(sizeof(struct e1000_ring), GFP_KERNEL);
2220 if (!adapter->tx_ring)
2221 goto err;
2223 adapter->rx_ring = kzalloc(sizeof(struct e1000_ring), GFP_KERNEL);
2224 if (!adapter->rx_ring)
2225 goto err;
2227 spin_lock_init(&adapter->tx_queue_lock);
2229 /* Explicitly disable IRQ since the NIC can be in any state. */
2230 atomic_set(&adapter->irq_sem, 0);
2231 e1000_irq_disable(adapter);
2233 spin_lock_init(&adapter->stats_lock);
2235 set_bit(__E1000_DOWN, &adapter->state);
2236 return 0;
2238 err:
2239 ndev_err(netdev, "Unable to allocate memory for queues\n");
2240 kfree(adapter->rx_ring);
2241 kfree(adapter->tx_ring);
2242 return -ENOMEM;
2246 * e1000_open - Called when a network interface is made active
2247 * @netdev: network interface device structure
2249 * Returns 0 on success, negative value on failure
2251 * The open entry point is called when a network interface is made
2252 * active by the system (IFF_UP). At this point all resources needed
2253 * for transmit and receive operations are allocated, the interrupt
2254 * handler is registered with the OS, the watchdog timer is started,
2255 * and the stack is notified that the interface is ready.
2257 static int e1000_open(struct net_device *netdev)
2259 struct e1000_adapter *adapter = netdev_priv(netdev);
2260 struct e1000_hw *hw = &adapter->hw;
2261 int err;
2263 /* disallow open during test */
2264 if (test_bit(__E1000_TESTING, &adapter->state))
2265 return -EBUSY;
2267 /* allocate transmit descriptors */
2268 err = e1000e_setup_tx_resources(adapter);
2269 if (err)
2270 goto err_setup_tx;
2272 /* allocate receive descriptors */
2273 err = e1000e_setup_rx_resources(adapter);
2274 if (err)
2275 goto err_setup_rx;
2277 e1000e_power_up_phy(adapter);
2279 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
2280 if ((adapter->hw.mng_cookie.status &
2281 E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
2282 e1000_update_mng_vlan(adapter);
2284 /* If AMT is enabled, let the firmware know that the network
2285 * interface is now open */
2286 if ((adapter->flags & FLAG_HAS_AMT) &&
2287 e1000e_check_mng_mode(&adapter->hw))
2288 e1000_get_hw_control(adapter);
2290 /* before we allocate an interrupt, we must be ready to handle it.
2291 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
2292 * as soon as we call pci_request_irq, so we have to setup our
2293 * clean_rx handler before we do so. */
2294 e1000_configure(adapter);
2296 err = e1000_request_irq(adapter);
2297 if (err)
2298 goto err_req_irq;
2300 /* From here on the code is the same as e1000e_up() */
2301 clear_bit(__E1000_DOWN, &adapter->state);
2303 napi_enable(&adapter->napi);
2305 e1000_irq_enable(adapter);
2307 /* fire a link status change interrupt to start the watchdog */
2308 ew32(ICS, E1000_ICS_LSC);
2310 return 0;
2312 err_req_irq:
2313 e1000_release_hw_control(adapter);
2314 e1000_power_down_phy(adapter);
2315 e1000e_free_rx_resources(adapter);
2316 err_setup_rx:
2317 e1000e_free_tx_resources(adapter);
2318 err_setup_tx:
2319 e1000e_reset(adapter);
2321 return err;
2325 * e1000_close - Disables a network interface
2326 * @netdev: network interface device structure
2328 * Returns 0, this is not allowed to fail
2330 * The close entry point is called when an interface is de-activated
2331 * by the OS. The hardware is still under the drivers control, but
2332 * needs to be disabled. A global MAC reset is issued to stop the
2333 * hardware, and all transmit and receive resources are freed.
2335 static int e1000_close(struct net_device *netdev)
2337 struct e1000_adapter *adapter = netdev_priv(netdev);
2339 WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
2340 e1000e_down(adapter);
2341 e1000_power_down_phy(adapter);
2342 e1000_free_irq(adapter);
2344 e1000e_free_tx_resources(adapter);
2345 e1000e_free_rx_resources(adapter);
2347 /* kill manageability vlan ID if supported, but not if a vlan with
2348 * the same ID is registered on the host OS (let 8021q kill it) */
2349 if ((adapter->hw.mng_cookie.status &
2350 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
2351 !(adapter->vlgrp &&
2352 vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id)))
2353 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
2355 /* If AMT is enabled, let the firmware know that the network
2356 * interface is now closed */
2357 if ((adapter->flags & FLAG_HAS_AMT) &&
2358 e1000e_check_mng_mode(&adapter->hw))
2359 e1000_release_hw_control(adapter);
2361 return 0;
2364 * e1000_set_mac - Change the Ethernet Address of the NIC
2365 * @netdev: network interface device structure
2366 * @p: pointer to an address structure
2368 * Returns 0 on success, negative on failure
2370 static int e1000_set_mac(struct net_device *netdev, void *p)
2372 struct e1000_adapter *adapter = netdev_priv(netdev);
2373 struct sockaddr *addr = p;
2375 if (!is_valid_ether_addr(addr->sa_data))
2376 return -EADDRNOTAVAIL;
2378 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2379 memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
2381 e1000e_rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
2383 if (adapter->flags & FLAG_RESET_OVERWRITES_LAA) {
2384 /* activate the work around */
2385 e1000e_set_laa_state_82571(&adapter->hw, 1);
2387 /* Hold a copy of the LAA in RAR[14] This is done so that
2388 * between the time RAR[0] gets clobbered and the time it
2389 * gets fixed (in e1000_watchdog), the actual LAA is in one
2390 * of the RARs and no incoming packets directed to this port
2391 * are dropped. Eventually the LAA will be in RAR[0] and
2392 * RAR[14] */
2393 e1000e_rar_set(&adapter->hw,
2394 adapter->hw.mac.addr,
2395 adapter->hw.mac.rar_entry_count - 1);
2398 return 0;
2401 /* Need to wait a few seconds after link up to get diagnostic information from
2402 * the phy */
2403 static void e1000_update_phy_info(unsigned long data)
2405 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2406 e1000_get_phy_info(&adapter->hw);
2410 * e1000e_update_stats - Update the board statistics counters
2411 * @adapter: board private structure
2413 void e1000e_update_stats(struct e1000_adapter *adapter)
2415 struct e1000_hw *hw = &adapter->hw;
2416 struct pci_dev *pdev = adapter->pdev;
2417 unsigned long irq_flags;
2418 u16 phy_tmp;
2420 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
2423 * Prevent stats update while adapter is being reset, or if the pci
2424 * connection is down.
2426 if (adapter->link_speed == 0)
2427 return;
2428 if (pci_channel_offline(pdev))
2429 return;
2431 spin_lock_irqsave(&adapter->stats_lock, irq_flags);
2433 /* these counters are modified from e1000_adjust_tbi_stats,
2434 * called from the interrupt context, so they must only
2435 * be written while holding adapter->stats_lock
2438 adapter->stats.crcerrs += er32(CRCERRS);
2439 adapter->stats.gprc += er32(GPRC);
2440 adapter->stats.gorcl += er32(GORCL);
2441 adapter->stats.gorch += er32(GORCH);
2442 adapter->stats.bprc += er32(BPRC);
2443 adapter->stats.mprc += er32(MPRC);
2444 adapter->stats.roc += er32(ROC);
2446 if (adapter->flags & FLAG_HAS_STATS_PTC_PRC) {
2447 adapter->stats.prc64 += er32(PRC64);
2448 adapter->stats.prc127 += er32(PRC127);
2449 adapter->stats.prc255 += er32(PRC255);
2450 adapter->stats.prc511 += er32(PRC511);
2451 adapter->stats.prc1023 += er32(PRC1023);
2452 adapter->stats.prc1522 += er32(PRC1522);
2453 adapter->stats.symerrs += er32(SYMERRS);
2454 adapter->stats.sec += er32(SEC);
2457 adapter->stats.mpc += er32(MPC);
2458 adapter->stats.scc += er32(SCC);
2459 adapter->stats.ecol += er32(ECOL);
2460 adapter->stats.mcc += er32(MCC);
2461 adapter->stats.latecol += er32(LATECOL);
2462 adapter->stats.dc += er32(DC);
2463 adapter->stats.rlec += er32(RLEC);
2464 adapter->stats.xonrxc += er32(XONRXC);
2465 adapter->stats.xontxc += er32(XONTXC);
2466 adapter->stats.xoffrxc += er32(XOFFRXC);
2467 adapter->stats.xofftxc += er32(XOFFTXC);
2468 adapter->stats.fcruc += er32(FCRUC);
2469 adapter->stats.gptc += er32(GPTC);
2470 adapter->stats.gotcl += er32(GOTCL);
2471 adapter->stats.gotch += er32(GOTCH);
2472 adapter->stats.rnbc += er32(RNBC);
2473 adapter->stats.ruc += er32(RUC);
2474 adapter->stats.rfc += er32(RFC);
2475 adapter->stats.rjc += er32(RJC);
2476 adapter->stats.torl += er32(TORL);
2477 adapter->stats.torh += er32(TORH);
2478 adapter->stats.totl += er32(TOTL);
2479 adapter->stats.toth += er32(TOTH);
2480 adapter->stats.tpr += er32(TPR);
2482 if (adapter->flags & FLAG_HAS_STATS_PTC_PRC) {
2483 adapter->stats.ptc64 += er32(PTC64);
2484 adapter->stats.ptc127 += er32(PTC127);
2485 adapter->stats.ptc255 += er32(PTC255);
2486 adapter->stats.ptc511 += er32(PTC511);
2487 adapter->stats.ptc1023 += er32(PTC1023);
2488 adapter->stats.ptc1522 += er32(PTC1522);
2491 adapter->stats.mptc += er32(MPTC);
2492 adapter->stats.bptc += er32(BPTC);
2494 /* used for adaptive IFS */
2496 hw->mac.tx_packet_delta = er32(TPT);
2497 adapter->stats.tpt += hw->mac.tx_packet_delta;
2498 hw->mac.collision_delta = er32(COLC);
2499 adapter->stats.colc += hw->mac.collision_delta;
2501 adapter->stats.algnerrc += er32(ALGNERRC);
2502 adapter->stats.rxerrc += er32(RXERRC);
2503 adapter->stats.tncrs += er32(TNCRS);
2504 adapter->stats.cexterr += er32(CEXTERR);
2505 adapter->stats.tsctc += er32(TSCTC);
2506 adapter->stats.tsctfc += er32(TSCTFC);
2508 adapter->stats.iac += er32(IAC);
2510 if (adapter->flags & FLAG_HAS_STATS_ICR_ICT) {
2511 adapter->stats.icrxoc += er32(ICRXOC);
2512 adapter->stats.icrxptc += er32(ICRXPTC);
2513 adapter->stats.icrxatc += er32(ICRXATC);
2514 adapter->stats.ictxptc += er32(ICTXPTC);
2515 adapter->stats.ictxatc += er32(ICTXATC);
2516 adapter->stats.ictxqec += er32(ICTXQEC);
2517 adapter->stats.ictxqmtc += er32(ICTXQMTC);
2518 adapter->stats.icrxdmtc += er32(ICRXDMTC);
2521 /* Fill out the OS statistics structure */
2522 adapter->net_stats.multicast = adapter->stats.mprc;
2523 adapter->net_stats.collisions = adapter->stats.colc;
2525 /* Rx Errors */
2527 /* RLEC on some newer hardware can be incorrect so build
2528 * our own version based on RUC and ROC */
2529 adapter->net_stats.rx_errors = adapter->stats.rxerrc +
2530 adapter->stats.crcerrs + adapter->stats.algnerrc +
2531 adapter->stats.ruc + adapter->stats.roc +
2532 adapter->stats.cexterr;
2533 adapter->net_stats.rx_length_errors = adapter->stats.ruc +
2534 adapter->stats.roc;
2535 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
2536 adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
2537 adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
2539 /* Tx Errors */
2540 adapter->net_stats.tx_errors = adapter->stats.ecol +
2541 adapter->stats.latecol;
2542 adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
2543 adapter->net_stats.tx_window_errors = adapter->stats.latecol;
2544 adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
2546 /* Tx Dropped needs to be maintained elsewhere */
2548 /* Phy Stats */
2549 if (hw->media_type == e1000_media_type_copper) {
2550 if ((adapter->link_speed == SPEED_1000) &&
2551 (!e1e_rphy(hw, PHY_1000T_STATUS, &phy_tmp))) {
2552 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
2553 adapter->phy_stats.idle_errors += phy_tmp;
2557 /* Management Stats */
2558 adapter->stats.mgptc += er32(MGTPTC);
2559 adapter->stats.mgprc += er32(MGTPRC);
2560 adapter->stats.mgpdc += er32(MGTPDC);
2562 spin_unlock_irqrestore(&adapter->stats_lock, irq_flags);
2565 static void e1000_print_link_info(struct e1000_adapter *adapter)
2567 struct net_device *netdev = adapter->netdev;
2568 struct e1000_hw *hw = &adapter->hw;
2569 u32 ctrl = er32(CTRL);
2571 ndev_info(netdev,
2572 "Link is Up %d Mbps %s, Flow Control: %s\n",
2573 adapter->link_speed,
2574 (adapter->link_duplex == FULL_DUPLEX) ?
2575 "Full Duplex" : "Half Duplex",
2576 ((ctrl & E1000_CTRL_TFCE) && (ctrl & E1000_CTRL_RFCE)) ?
2577 "RX/TX" :
2578 ((ctrl & E1000_CTRL_RFCE) ? "RX" :
2579 ((ctrl & E1000_CTRL_TFCE) ? "TX" : "None" )));
2583 * e1000_watchdog - Timer Call-back
2584 * @data: pointer to adapter cast into an unsigned long
2586 static void e1000_watchdog(unsigned long data)
2588 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2590 /* Do the rest outside of interrupt context */
2591 schedule_work(&adapter->watchdog_task);
2593 /* TODO: make this use queue_delayed_work() */
2596 static void e1000_watchdog_task(struct work_struct *work)
2598 struct e1000_adapter *adapter = container_of(work,
2599 struct e1000_adapter, watchdog_task);
2601 struct net_device *netdev = adapter->netdev;
2602 struct e1000_mac_info *mac = &adapter->hw.mac;
2603 struct e1000_ring *tx_ring = adapter->tx_ring;
2604 struct e1000_hw *hw = &adapter->hw;
2605 u32 link, tctl;
2606 s32 ret_val;
2607 int tx_pending = 0;
2609 if ((netif_carrier_ok(netdev)) &&
2610 (er32(STATUS) & E1000_STATUS_LU))
2611 goto link_up;
2613 ret_val = mac->ops.check_for_link(hw);
2614 if ((ret_val == E1000_ERR_PHY) &&
2615 (adapter->hw.phy.type == e1000_phy_igp_3) &&
2616 (er32(CTRL) &
2617 E1000_PHY_CTRL_GBE_DISABLE)) {
2618 /* See e1000_kmrn_lock_loss_workaround_ich8lan() */
2619 ndev_info(netdev,
2620 "Gigabit has been disabled, downgrading speed\n");
2623 if ((e1000e_enable_tx_pkt_filtering(hw)) &&
2624 (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id))
2625 e1000_update_mng_vlan(adapter);
2627 if ((adapter->hw.media_type == e1000_media_type_internal_serdes) &&
2628 !(er32(TXCW) & E1000_TXCW_ANE))
2629 link = adapter->hw.mac.serdes_has_link;
2630 else
2631 link = er32(STATUS) & E1000_STATUS_LU;
2633 if (link) {
2634 if (!netif_carrier_ok(netdev)) {
2635 bool txb2b = 1;
2636 mac->ops.get_link_up_info(&adapter->hw,
2637 &adapter->link_speed,
2638 &adapter->link_duplex);
2639 e1000_print_link_info(adapter);
2640 /* tweak tx_queue_len according to speed/duplex
2641 * and adjust the timeout factor */
2642 netdev->tx_queue_len = adapter->tx_queue_len;
2643 adapter->tx_timeout_factor = 1;
2644 switch (adapter->link_speed) {
2645 case SPEED_10:
2646 txb2b = 0;
2647 netdev->tx_queue_len = 10;
2648 adapter->tx_timeout_factor = 14;
2649 break;
2650 case SPEED_100:
2651 txb2b = 0;
2652 netdev->tx_queue_len = 100;
2653 /* maybe add some timeout factor ? */
2654 break;
2657 /* workaround: re-program speed mode bit after
2658 * link-up event */
2659 if ((adapter->flags & FLAG_TARC_SPEED_MODE_BIT) &&
2660 !txb2b) {
2661 u32 tarc0;
2662 tarc0 = er32(TARC0);
2663 tarc0 &= ~SPEED_MODE_BIT;
2664 ew32(TARC0, tarc0);
2667 /* disable TSO for pcie and 10/100 speeds, to avoid
2668 * some hardware issues */
2669 if (!(adapter->flags & FLAG_TSO_FORCE)) {
2670 switch (adapter->link_speed) {
2671 case SPEED_10:
2672 case SPEED_100:
2673 ndev_info(netdev,
2674 "10/100 speed: disabling TSO\n");
2675 netdev->features &= ~NETIF_F_TSO;
2676 netdev->features &= ~NETIF_F_TSO6;
2677 break;
2678 case SPEED_1000:
2679 netdev->features |= NETIF_F_TSO;
2680 netdev->features |= NETIF_F_TSO6;
2681 break;
2682 default:
2683 /* oops */
2684 break;
2688 /* enable transmits in the hardware, need to do this
2689 * after setting TARC0 */
2690 tctl = er32(TCTL);
2691 tctl |= E1000_TCTL_EN;
2692 ew32(TCTL, tctl);
2694 netif_carrier_on(netdev);
2695 netif_wake_queue(netdev);
2697 if (!test_bit(__E1000_DOWN, &adapter->state))
2698 mod_timer(&adapter->phy_info_timer,
2699 round_jiffies(jiffies + 2 * HZ));
2700 } else {
2701 /* make sure the receive unit is started */
2702 if (adapter->flags & FLAG_RX_NEEDS_RESTART) {
2703 u32 rctl = er32(RCTL);
2704 ew32(RCTL, rctl |
2705 E1000_RCTL_EN);
2708 } else {
2709 if (netif_carrier_ok(netdev)) {
2710 adapter->link_speed = 0;
2711 adapter->link_duplex = 0;
2712 ndev_info(netdev, "Link is Down\n");
2713 netif_carrier_off(netdev);
2714 netif_stop_queue(netdev);
2715 if (!test_bit(__E1000_DOWN, &adapter->state))
2716 mod_timer(&adapter->phy_info_timer,
2717 round_jiffies(jiffies + 2 * HZ));
2719 if (adapter->flags & FLAG_RX_NEEDS_RESTART)
2720 schedule_work(&adapter->reset_task);
2724 link_up:
2725 e1000e_update_stats(adapter);
2727 mac->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2728 adapter->tpt_old = adapter->stats.tpt;
2729 mac->collision_delta = adapter->stats.colc - adapter->colc_old;
2730 adapter->colc_old = adapter->stats.colc;
2732 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2733 adapter->gorcl_old = adapter->stats.gorcl;
2734 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2735 adapter->gotcl_old = adapter->stats.gotcl;
2737 e1000e_update_adaptive(&adapter->hw);
2739 if (!netif_carrier_ok(netdev)) {
2740 tx_pending = (e1000_desc_unused(tx_ring) + 1 <
2741 tx_ring->count);
2742 if (tx_pending) {
2743 /* We've lost link, so the controller stops DMA,
2744 * but we've got queued Tx work that's never going
2745 * to get done, so reset controller to flush Tx.
2746 * (Do the reset outside of interrupt context). */
2747 adapter->tx_timeout_count++;
2748 schedule_work(&adapter->reset_task);
2752 /* Cause software interrupt to ensure rx ring is cleaned */
2753 ew32(ICS, E1000_ICS_RXDMT0);
2755 /* Force detection of hung controller every watchdog period */
2756 adapter->detect_tx_hung = 1;
2758 /* With 82571 controllers, LAA may be overwritten due to controller
2759 * reset from the other port. Set the appropriate LAA in RAR[0] */
2760 if (e1000e_get_laa_state_82571(hw))
2761 e1000e_rar_set(hw, adapter->hw.mac.addr, 0);
2763 /* Reset the timer */
2764 if (!test_bit(__E1000_DOWN, &adapter->state))
2765 mod_timer(&adapter->watchdog_timer,
2766 round_jiffies(jiffies + 2 * HZ));
2769 #define E1000_TX_FLAGS_CSUM 0x00000001
2770 #define E1000_TX_FLAGS_VLAN 0x00000002
2771 #define E1000_TX_FLAGS_TSO 0x00000004
2772 #define E1000_TX_FLAGS_IPV4 0x00000008
2773 #define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
2774 #define E1000_TX_FLAGS_VLAN_SHIFT 16
2776 static int e1000_tso(struct e1000_adapter *adapter,
2777 struct sk_buff *skb)
2779 struct e1000_ring *tx_ring = adapter->tx_ring;
2780 struct e1000_context_desc *context_desc;
2781 struct e1000_buffer *buffer_info;
2782 unsigned int i;
2783 u32 cmd_length = 0;
2784 u16 ipcse = 0, tucse, mss;
2785 u8 ipcss, ipcso, tucss, tucso, hdr_len;
2786 int err;
2788 if (skb_is_gso(skb)) {
2789 if (skb_header_cloned(skb)) {
2790 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2791 if (err)
2792 return err;
2795 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2796 mss = skb_shinfo(skb)->gso_size;
2797 if (skb->protocol == htons(ETH_P_IP)) {
2798 struct iphdr *iph = ip_hdr(skb);
2799 iph->tot_len = 0;
2800 iph->check = 0;
2801 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2802 iph->daddr, 0,
2803 IPPROTO_TCP,
2805 cmd_length = E1000_TXD_CMD_IP;
2806 ipcse = skb_transport_offset(skb) - 1;
2807 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
2808 ipv6_hdr(skb)->payload_len = 0;
2809 tcp_hdr(skb)->check =
2810 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2811 &ipv6_hdr(skb)->daddr,
2812 0, IPPROTO_TCP, 0);
2813 ipcse = 0;
2815 ipcss = skb_network_offset(skb);
2816 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
2817 tucss = skb_transport_offset(skb);
2818 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
2819 tucse = 0;
2821 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
2822 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
2824 i = tx_ring->next_to_use;
2825 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2826 buffer_info = &tx_ring->buffer_info[i];
2828 context_desc->lower_setup.ip_fields.ipcss = ipcss;
2829 context_desc->lower_setup.ip_fields.ipcso = ipcso;
2830 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
2831 context_desc->upper_setup.tcp_fields.tucss = tucss;
2832 context_desc->upper_setup.tcp_fields.tucso = tucso;
2833 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2834 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
2835 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2836 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2838 buffer_info->time_stamp = jiffies;
2839 buffer_info->next_to_watch = i;
2841 i++;
2842 if (i == tx_ring->count)
2843 i = 0;
2844 tx_ring->next_to_use = i;
2846 return 1;
2849 return 0;
2852 static bool e1000_tx_csum(struct e1000_adapter *adapter, struct sk_buff *skb)
2854 struct e1000_ring *tx_ring = adapter->tx_ring;
2855 struct e1000_context_desc *context_desc;
2856 struct e1000_buffer *buffer_info;
2857 unsigned int i;
2858 u8 css;
2860 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2861 css = skb_transport_offset(skb);
2863 i = tx_ring->next_to_use;
2864 buffer_info = &tx_ring->buffer_info[i];
2865 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2867 context_desc->lower_setup.ip_config = 0;
2868 context_desc->upper_setup.tcp_fields.tucss = css;
2869 context_desc->upper_setup.tcp_fields.tucso =
2870 css + skb->csum_offset;
2871 context_desc->upper_setup.tcp_fields.tucse = 0;
2872 context_desc->tcp_seg_setup.data = 0;
2873 context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
2875 buffer_info->time_stamp = jiffies;
2876 buffer_info->next_to_watch = i;
2878 i++;
2879 if (i == tx_ring->count)
2880 i = 0;
2881 tx_ring->next_to_use = i;
2883 return 1;
2886 return 0;
2889 #define E1000_MAX_PER_TXD 8192
2890 #define E1000_MAX_TXD_PWR 12
2892 static int e1000_tx_map(struct e1000_adapter *adapter,
2893 struct sk_buff *skb, unsigned int first,
2894 unsigned int max_per_txd, unsigned int nr_frags,
2895 unsigned int mss)
2897 struct e1000_ring *tx_ring = adapter->tx_ring;
2898 struct e1000_buffer *buffer_info;
2899 unsigned int len = skb->len - skb->data_len;
2900 unsigned int offset = 0, size, count = 0, i;
2901 unsigned int f;
2903 i = tx_ring->next_to_use;
2905 while (len) {
2906 buffer_info = &tx_ring->buffer_info[i];
2907 size = min(len, max_per_txd);
2909 /* Workaround for premature desc write-backs
2910 * in TSO mode. Append 4-byte sentinel desc */
2911 if (mss && !nr_frags && size == len && size > 8)
2912 size -= 4;
2914 buffer_info->length = size;
2915 /* set time_stamp *before* dma to help avoid a possible race */
2916 buffer_info->time_stamp = jiffies;
2917 buffer_info->dma =
2918 pci_map_single(adapter->pdev,
2919 skb->data + offset,
2920 size,
2921 PCI_DMA_TODEVICE);
2922 if (pci_dma_mapping_error(buffer_info->dma)) {
2923 dev_err(&adapter->pdev->dev, "TX DMA map failed\n");
2924 adapter->tx_dma_failed++;
2925 return -1;
2927 buffer_info->next_to_watch = i;
2929 len -= size;
2930 offset += size;
2931 count++;
2932 i++;
2933 if (i == tx_ring->count)
2934 i = 0;
2937 for (f = 0; f < nr_frags; f++) {
2938 struct skb_frag_struct *frag;
2940 frag = &skb_shinfo(skb)->frags[f];
2941 len = frag->size;
2942 offset = frag->page_offset;
2944 while (len) {
2945 buffer_info = &tx_ring->buffer_info[i];
2946 size = min(len, max_per_txd);
2947 /* Workaround for premature desc write-backs
2948 * in TSO mode. Append 4-byte sentinel desc */
2949 if (mss && f == (nr_frags-1) && size == len && size > 8)
2950 size -= 4;
2952 buffer_info->length = size;
2953 buffer_info->time_stamp = jiffies;
2954 buffer_info->dma =
2955 pci_map_page(adapter->pdev,
2956 frag->page,
2957 offset,
2958 size,
2959 PCI_DMA_TODEVICE);
2960 if (pci_dma_mapping_error(buffer_info->dma)) {
2961 dev_err(&adapter->pdev->dev,
2962 "TX DMA page map failed\n");
2963 adapter->tx_dma_failed++;
2964 return -1;
2967 buffer_info->next_to_watch = i;
2969 len -= size;
2970 offset += size;
2971 count++;
2973 i++;
2974 if (i == tx_ring->count)
2975 i = 0;
2979 if (i == 0)
2980 i = tx_ring->count - 1;
2981 else
2982 i--;
2984 tx_ring->buffer_info[i].skb = skb;
2985 tx_ring->buffer_info[first].next_to_watch = i;
2987 return count;
2990 static void e1000_tx_queue(struct e1000_adapter *adapter,
2991 int tx_flags, int count)
2993 struct e1000_ring *tx_ring = adapter->tx_ring;
2994 struct e1000_tx_desc *tx_desc = NULL;
2995 struct e1000_buffer *buffer_info;
2996 u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
2997 unsigned int i;
2999 if (tx_flags & E1000_TX_FLAGS_TSO) {
3000 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
3001 E1000_TXD_CMD_TSE;
3002 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
3004 if (tx_flags & E1000_TX_FLAGS_IPV4)
3005 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
3008 if (tx_flags & E1000_TX_FLAGS_CSUM) {
3009 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
3010 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
3013 if (tx_flags & E1000_TX_FLAGS_VLAN) {
3014 txd_lower |= E1000_TXD_CMD_VLE;
3015 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
3018 i = tx_ring->next_to_use;
3020 while (count--) {
3021 buffer_info = &tx_ring->buffer_info[i];
3022 tx_desc = E1000_TX_DESC(*tx_ring, i);
3023 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
3024 tx_desc->lower.data =
3025 cpu_to_le32(txd_lower | buffer_info->length);
3026 tx_desc->upper.data = cpu_to_le32(txd_upper);
3028 i++;
3029 if (i == tx_ring->count)
3030 i = 0;
3033 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
3035 /* Force memory writes to complete before letting h/w
3036 * know there are new descriptors to fetch. (Only
3037 * applicable for weak-ordered memory model archs,
3038 * such as IA-64). */
3039 wmb();
3041 tx_ring->next_to_use = i;
3042 writel(i, adapter->hw.hw_addr + tx_ring->tail);
3043 /* we need this if more than one processor can write to our tail
3044 * at a time, it synchronizes IO on IA64/Altix systems */
3045 mmiowb();
3048 #define MINIMUM_DHCP_PACKET_SIZE 282
3049 static int e1000_transfer_dhcp_info(struct e1000_adapter *adapter,
3050 struct sk_buff *skb)
3052 struct e1000_hw *hw = &adapter->hw;
3053 u16 length, offset;
3055 if (vlan_tx_tag_present(skb)) {
3056 if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id)
3057 && (adapter->hw.mng_cookie.status &
3058 E1000_MNG_DHCP_COOKIE_STATUS_VLAN)))
3059 return 0;
3062 if (skb->len <= MINIMUM_DHCP_PACKET_SIZE)
3063 return 0;
3065 if (((struct ethhdr *) skb->data)->h_proto != htons(ETH_P_IP))
3066 return 0;
3069 const struct iphdr *ip = (struct iphdr *)((u8 *)skb->data+14);
3070 struct udphdr *udp;
3072 if (ip->protocol != IPPROTO_UDP)
3073 return 0;
3075 udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
3076 if (ntohs(udp->dest) != 67)
3077 return 0;
3079 offset = (u8 *)udp + 8 - skb->data;
3080 length = skb->len - offset;
3081 return e1000e_mng_write_dhcp_info(hw, (u8 *)udp + 8, length);
3084 return 0;
3087 static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
3089 struct e1000_adapter *adapter = netdev_priv(netdev);
3091 netif_stop_queue(netdev);
3092 /* Herbert's original patch had:
3093 * smp_mb__after_netif_stop_queue();
3094 * but since that doesn't exist yet, just open code it. */
3095 smp_mb();
3097 /* We need to check again in a case another CPU has just
3098 * made room available. */
3099 if (e1000_desc_unused(adapter->tx_ring) < size)
3100 return -EBUSY;
3102 /* A reprieve! */
3103 netif_start_queue(netdev);
3104 ++adapter->restart_queue;
3105 return 0;
3108 static int e1000_maybe_stop_tx(struct net_device *netdev, int size)
3110 struct e1000_adapter *adapter = netdev_priv(netdev);
3112 if (e1000_desc_unused(adapter->tx_ring) >= size)
3113 return 0;
3114 return __e1000_maybe_stop_tx(netdev, size);
3117 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
3118 static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3120 struct e1000_adapter *adapter = netdev_priv(netdev);
3121 struct e1000_ring *tx_ring = adapter->tx_ring;
3122 unsigned int first;
3123 unsigned int max_per_txd = E1000_MAX_PER_TXD;
3124 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
3125 unsigned int tx_flags = 0;
3126 unsigned int len = skb->len - skb->data_len;
3127 unsigned long irq_flags;
3128 unsigned int nr_frags;
3129 unsigned int mss;
3130 int count = 0;
3131 int tso;
3132 unsigned int f;
3134 if (test_bit(__E1000_DOWN, &adapter->state)) {
3135 dev_kfree_skb_any(skb);
3136 return NETDEV_TX_OK;
3139 if (skb->len <= 0) {
3140 dev_kfree_skb_any(skb);
3141 return NETDEV_TX_OK;
3144 mss = skb_shinfo(skb)->gso_size;
3145 /* The controller does a simple calculation to
3146 * make sure there is enough room in the FIFO before
3147 * initiating the DMA for each buffer. The calc is:
3148 * 4 = ceil(buffer len/mss). To make sure we don't
3149 * overrun the FIFO, adjust the max buffer len if mss
3150 * drops. */
3151 if (mss) {
3152 u8 hdr_len;
3153 max_per_txd = min(mss << 2, max_per_txd);
3154 max_txd_pwr = fls(max_per_txd) - 1;
3156 /* TSO Workaround for 82571/2/3 Controllers -- if skb->data
3157 * points to just header, pull a few bytes of payload from
3158 * frags into skb->data */
3159 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3160 if (skb->data_len && (hdr_len == len)) {
3161 unsigned int pull_size;
3163 pull_size = min((unsigned int)4, skb->data_len);
3164 if (!__pskb_pull_tail(skb, pull_size)) {
3165 ndev_err(netdev,
3166 "__pskb_pull_tail failed.\n");
3167 dev_kfree_skb_any(skb);
3168 return NETDEV_TX_OK;
3170 len = skb->len - skb->data_len;
3174 /* reserve a descriptor for the offload context */
3175 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
3176 count++;
3177 count++;
3179 count += TXD_USE_COUNT(len, max_txd_pwr);
3181 nr_frags = skb_shinfo(skb)->nr_frags;
3182 for (f = 0; f < nr_frags; f++)
3183 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
3184 max_txd_pwr);
3186 if (adapter->hw.mac.tx_pkt_filtering)
3187 e1000_transfer_dhcp_info(adapter, skb);
3189 if (!spin_trylock_irqsave(&adapter->tx_queue_lock, irq_flags))
3190 /* Collision - tell upper layer to requeue */
3191 return NETDEV_TX_LOCKED;
3193 /* need: count + 2 desc gap to keep tail from touching
3194 * head, otherwise try next time */
3195 if (e1000_maybe_stop_tx(netdev, count + 2)) {
3196 spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
3197 return NETDEV_TX_BUSY;
3200 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
3201 tx_flags |= E1000_TX_FLAGS_VLAN;
3202 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
3205 first = tx_ring->next_to_use;
3207 tso = e1000_tso(adapter, skb);
3208 if (tso < 0) {
3209 dev_kfree_skb_any(skb);
3210 spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
3211 return NETDEV_TX_OK;
3214 if (tso)
3215 tx_flags |= E1000_TX_FLAGS_TSO;
3216 else if (e1000_tx_csum(adapter, skb))
3217 tx_flags |= E1000_TX_FLAGS_CSUM;
3219 /* Old method was to assume IPv4 packet by default if TSO was enabled.
3220 * 82571 hardware supports TSO capabilities for IPv6 as well...
3221 * no longer assume, we must. */
3222 if (skb->protocol == htons(ETH_P_IP))
3223 tx_flags |= E1000_TX_FLAGS_IPV4;
3225 count = e1000_tx_map(adapter, skb, first, max_per_txd, nr_frags, mss);
3226 if (count < 0) {
3227 /* handle pci_map_single() error in e1000_tx_map */
3228 dev_kfree_skb_any(skb);
3229 spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
3230 return NETDEV_TX_OK;
3233 e1000_tx_queue(adapter, tx_flags, count);
3235 netdev->trans_start = jiffies;
3237 /* Make sure there is space in the ring for the next send. */
3238 e1000_maybe_stop_tx(netdev, MAX_SKB_FRAGS + 2);
3240 spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
3241 return NETDEV_TX_OK;
3245 * e1000_tx_timeout - Respond to a Tx Hang
3246 * @netdev: network interface device structure
3248 static void e1000_tx_timeout(struct net_device *netdev)
3250 struct e1000_adapter *adapter = netdev_priv(netdev);
3252 /* Do the reset outside of interrupt context */
3253 adapter->tx_timeout_count++;
3254 schedule_work(&adapter->reset_task);
3257 static void e1000_reset_task(struct work_struct *work)
3259 struct e1000_adapter *adapter;
3260 adapter = container_of(work, struct e1000_adapter, reset_task);
3262 e1000e_reinit_locked(adapter);
3266 * e1000_get_stats - Get System Network Statistics
3267 * @netdev: network interface device structure
3269 * Returns the address of the device statistics structure.
3270 * The statistics are actually updated from the timer callback.
3272 static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
3274 struct e1000_adapter *adapter = netdev_priv(netdev);
3276 /* only return the current stats */
3277 return &adapter->net_stats;
3281 * e1000_change_mtu - Change the Maximum Transfer Unit
3282 * @netdev: network interface device structure
3283 * @new_mtu: new value for maximum frame size
3285 * Returns 0 on success, negative on failure
3287 static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
3289 struct e1000_adapter *adapter = netdev_priv(netdev);
3290 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3292 if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
3293 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
3294 ndev_err(netdev, "Invalid MTU setting\n");
3295 return -EINVAL;
3298 /* Jumbo frame size limits */
3299 if (max_frame > ETH_FRAME_LEN + ETH_FCS_LEN) {
3300 if (!(adapter->flags & FLAG_HAS_JUMBO_FRAMES)) {
3301 ndev_err(netdev, "Jumbo Frames not supported.\n");
3302 return -EINVAL;
3304 if (adapter->hw.phy.type == e1000_phy_ife) {
3305 ndev_err(netdev, "Jumbo Frames not supported.\n");
3306 return -EINVAL;
3310 #define MAX_STD_JUMBO_FRAME_SIZE 9234
3311 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
3312 ndev_err(netdev, "MTU > 9216 not supported.\n");
3313 return -EINVAL;
3316 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
3317 msleep(1);
3318 /* e1000e_down has a dependency on max_frame_size */
3319 adapter->hw.mac.max_frame_size = max_frame;
3320 if (netif_running(netdev))
3321 e1000e_down(adapter);
3323 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
3324 * means we reserve 2 more, this pushes us to allocate from the next
3325 * larger slab size.
3326 * i.e. RXBUFFER_2048 --> size-4096 slab */
3328 if (max_frame <= 256)
3329 adapter->rx_buffer_len = 256;
3330 else if (max_frame <= 512)
3331 adapter->rx_buffer_len = 512;
3332 else if (max_frame <= 1024)
3333 adapter->rx_buffer_len = 1024;
3334 else if (max_frame <= 2048)
3335 adapter->rx_buffer_len = 2048;
3336 else
3337 adapter->rx_buffer_len = 4096;
3339 /* adjust allocation if LPE protects us, and we aren't using SBP */
3340 if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
3341 (max_frame == ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN))
3342 adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN
3343 + ETH_FCS_LEN ;
3345 ndev_info(netdev, "changing MTU from %d to %d\n",
3346 netdev->mtu, new_mtu);
3347 netdev->mtu = new_mtu;
3349 if (netif_running(netdev))
3350 e1000e_up(adapter);
3351 else
3352 e1000e_reset(adapter);
3354 clear_bit(__E1000_RESETTING, &adapter->state);
3356 return 0;
3359 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
3360 int cmd)
3362 struct e1000_adapter *adapter = netdev_priv(netdev);
3363 struct mii_ioctl_data *data = if_mii(ifr);
3364 unsigned long irq_flags;
3366 if (adapter->hw.media_type != e1000_media_type_copper)
3367 return -EOPNOTSUPP;
3369 switch (cmd) {
3370 case SIOCGMIIPHY:
3371 data->phy_id = adapter->hw.phy.addr;
3372 break;
3373 case SIOCGMIIREG:
3374 if (!capable(CAP_NET_ADMIN))
3375 return -EPERM;
3376 spin_lock_irqsave(&adapter->stats_lock, irq_flags);
3377 if (e1e_rphy(&adapter->hw, data->reg_num & 0x1F,
3378 &data->val_out)) {
3379 spin_unlock_irqrestore(&adapter->stats_lock, irq_flags);
3380 return -EIO;
3382 spin_unlock_irqrestore(&adapter->stats_lock, irq_flags);
3383 break;
3384 case SIOCSMIIREG:
3385 default:
3386 return -EOPNOTSUPP;
3388 return 0;
3391 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
3393 switch (cmd) {
3394 case SIOCGMIIPHY:
3395 case SIOCGMIIREG:
3396 case SIOCSMIIREG:
3397 return e1000_mii_ioctl(netdev, ifr, cmd);
3398 default:
3399 return -EOPNOTSUPP;
3403 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
3405 struct net_device *netdev = pci_get_drvdata(pdev);
3406 struct e1000_adapter *adapter = netdev_priv(netdev);
3407 struct e1000_hw *hw = &adapter->hw;
3408 u32 ctrl, ctrl_ext, rctl, status;
3409 u32 wufc = adapter->wol;
3410 int retval = 0;
3412 netif_device_detach(netdev);
3414 if (netif_running(netdev)) {
3415 WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
3416 e1000e_down(adapter);
3417 e1000_free_irq(adapter);
3420 retval = pci_save_state(pdev);
3421 if (retval)
3422 return retval;
3424 status = er32(STATUS);
3425 if (status & E1000_STATUS_LU)
3426 wufc &= ~E1000_WUFC_LNKC;
3428 if (wufc) {
3429 e1000_setup_rctl(adapter);
3430 e1000_set_multi(netdev);
3432 /* turn on all-multi mode if wake on multicast is enabled */
3433 if (wufc & E1000_WUFC_MC) {
3434 rctl = er32(RCTL);
3435 rctl |= E1000_RCTL_MPE;
3436 ew32(RCTL, rctl);
3439 ctrl = er32(CTRL);
3440 /* advertise wake from D3Cold */
3441 #define E1000_CTRL_ADVD3WUC 0x00100000
3442 /* phy power management enable */
3443 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
3444 ctrl |= E1000_CTRL_ADVD3WUC |
3445 E1000_CTRL_EN_PHY_PWR_MGMT;
3446 ew32(CTRL, ctrl);
3448 if (adapter->hw.media_type == e1000_media_type_fiber ||
3449 adapter->hw.media_type == e1000_media_type_internal_serdes) {
3450 /* keep the laser running in D3 */
3451 ctrl_ext = er32(CTRL_EXT);
3452 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
3453 ew32(CTRL_EXT, ctrl_ext);
3456 /* Allow time for pending master requests to run */
3457 e1000e_disable_pcie_master(&adapter->hw);
3459 ew32(WUC, E1000_WUC_PME_EN);
3460 ew32(WUFC, wufc);
3461 pci_enable_wake(pdev, PCI_D3hot, 1);
3462 pci_enable_wake(pdev, PCI_D3cold, 1);
3463 } else {
3464 ew32(WUC, 0);
3465 ew32(WUFC, 0);
3466 pci_enable_wake(pdev, PCI_D3hot, 0);
3467 pci_enable_wake(pdev, PCI_D3cold, 0);
3470 /* make sure adapter isn't asleep if manageability is enabled */
3471 if (adapter->flags & FLAG_MNG_PT_ENABLED) {
3472 pci_enable_wake(pdev, PCI_D3hot, 1);
3473 pci_enable_wake(pdev, PCI_D3cold, 1);
3476 if (adapter->hw.phy.type == e1000_phy_igp_3)
3477 e1000e_igp3_phy_powerdown_workaround_ich8lan(&adapter->hw);
3479 /* Release control of h/w to f/w. If f/w is AMT enabled, this
3480 * would have already happened in close and is redundant. */
3481 e1000_release_hw_control(adapter);
3483 pci_disable_device(pdev);
3485 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3487 return 0;
3490 static void e1000e_disable_l1aspm(struct pci_dev *pdev)
3492 int pos;
3493 u16 val;
3496 * 82573 workaround - disable L1 ASPM on mobile chipsets
3498 * L1 ASPM on various mobile (ich7) chipsets do not behave properly
3499 * resulting in lost data or garbage information on the pci-e link
3500 * level. This could result in (false) bad EEPROM checksum errors,
3501 * long ping times (up to 2s) or even a system freeze/hang.
3503 * Unfortunately this feature saves about 1W power consumption when
3504 * active.
3506 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3507 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &val);
3508 if (val & 0x2) {
3509 dev_warn(&pdev->dev, "Disabling L1 ASPM\n");
3510 val &= ~0x2;
3511 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, val);
3515 #ifdef CONFIG_PM
3516 static int e1000_resume(struct pci_dev *pdev)
3518 struct net_device *netdev = pci_get_drvdata(pdev);
3519 struct e1000_adapter *adapter = netdev_priv(netdev);
3520 struct e1000_hw *hw = &adapter->hw;
3521 u32 err;
3523 pci_set_power_state(pdev, PCI_D0);
3524 pci_restore_state(pdev);
3525 e1000e_disable_l1aspm(pdev);
3526 err = pci_enable_device(pdev);
3527 if (err) {
3528 dev_err(&pdev->dev,
3529 "Cannot enable PCI device from suspend\n");
3530 return err;
3533 pci_set_master(pdev);
3535 pci_enable_wake(pdev, PCI_D3hot, 0);
3536 pci_enable_wake(pdev, PCI_D3cold, 0);
3538 if (netif_running(netdev)) {
3539 err = e1000_request_irq(adapter);
3540 if (err)
3541 return err;
3544 e1000e_power_up_phy(adapter);
3545 e1000e_reset(adapter);
3546 ew32(WUS, ~0);
3548 e1000_init_manageability(adapter);
3550 if (netif_running(netdev))
3551 e1000e_up(adapter);
3553 netif_device_attach(netdev);
3555 /* If the controller has AMT, do not set DRV_LOAD until the interface
3556 * is up. For all other cases, let the f/w know that the h/w is now
3557 * under the control of the driver. */
3558 if (!(adapter->flags & FLAG_HAS_AMT) || !e1000e_check_mng_mode(&adapter->hw))
3559 e1000_get_hw_control(adapter);
3561 return 0;
3563 #endif
3565 static void e1000_shutdown(struct pci_dev *pdev)
3567 e1000_suspend(pdev, PMSG_SUSPEND);
3570 #ifdef CONFIG_NET_POLL_CONTROLLER
3572 * Polling 'interrupt' - used by things like netconsole to send skbs
3573 * without having to re-enable interrupts. It's not called while
3574 * the interrupt routine is executing.
3576 static void e1000_netpoll(struct net_device *netdev)
3578 struct e1000_adapter *adapter = netdev_priv(netdev);
3580 disable_irq(adapter->pdev->irq);
3581 e1000_intr(adapter->pdev->irq, netdev);
3583 e1000_clean_tx_irq(adapter);
3585 enable_irq(adapter->pdev->irq);
3587 #endif
3590 * e1000_io_error_detected - called when PCI error is detected
3591 * @pdev: Pointer to PCI device
3592 * @state: The current pci connection state
3594 * This function is called after a PCI bus error affecting
3595 * this device has been detected.
3597 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
3598 pci_channel_state_t state)
3600 struct net_device *netdev = pci_get_drvdata(pdev);
3601 struct e1000_adapter *adapter = netdev_priv(netdev);
3603 netif_device_detach(netdev);
3605 if (netif_running(netdev))
3606 e1000e_down(adapter);
3607 pci_disable_device(pdev);
3609 /* Request a slot slot reset. */
3610 return PCI_ERS_RESULT_NEED_RESET;
3614 * e1000_io_slot_reset - called after the pci bus has been reset.
3615 * @pdev: Pointer to PCI device
3617 * Restart the card from scratch, as if from a cold-boot. Implementation
3618 * resembles the first-half of the e1000_resume routine.
3620 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
3622 struct net_device *netdev = pci_get_drvdata(pdev);
3623 struct e1000_adapter *adapter = netdev_priv(netdev);
3624 struct e1000_hw *hw = &adapter->hw;
3626 e1000e_disable_l1aspm(pdev);
3627 if (pci_enable_device(pdev)) {
3628 dev_err(&pdev->dev,
3629 "Cannot re-enable PCI device after reset.\n");
3630 return PCI_ERS_RESULT_DISCONNECT;
3632 pci_set_master(pdev);
3634 pci_enable_wake(pdev, PCI_D3hot, 0);
3635 pci_enable_wake(pdev, PCI_D3cold, 0);
3637 e1000e_reset(adapter);
3638 ew32(WUS, ~0);
3640 return PCI_ERS_RESULT_RECOVERED;
3644 * e1000_io_resume - called when traffic can start flowing again.
3645 * @pdev: Pointer to PCI device
3647 * This callback is called when the error recovery driver tells us that
3648 * its OK to resume normal operation. Implementation resembles the
3649 * second-half of the e1000_resume routine.
3651 static void e1000_io_resume(struct pci_dev *pdev)
3653 struct net_device *netdev = pci_get_drvdata(pdev);
3654 struct e1000_adapter *adapter = netdev_priv(netdev);
3656 e1000_init_manageability(adapter);
3658 if (netif_running(netdev)) {
3659 if (e1000e_up(adapter)) {
3660 dev_err(&pdev->dev,
3661 "can't bring device back up after reset\n");
3662 return;
3666 netif_device_attach(netdev);
3668 /* If the controller has AMT, do not set DRV_LOAD until the interface
3669 * is up. For all other cases, let the f/w know that the h/w is now
3670 * under the control of the driver. */
3671 if (!(adapter->flags & FLAG_HAS_AMT) ||
3672 !e1000e_check_mng_mode(&adapter->hw))
3673 e1000_get_hw_control(adapter);
3677 static void e1000_print_device_info(struct e1000_adapter *adapter)
3679 struct e1000_hw *hw = &adapter->hw;
3680 struct net_device *netdev = adapter->netdev;
3681 u32 part_num;
3683 /* print bus type/speed/width info */
3684 ndev_info(netdev, "(PCI Express:2.5GB/s:%s) "
3685 "%02x:%02x:%02x:%02x:%02x:%02x\n",
3686 /* bus width */
3687 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
3688 "Width x1"),
3689 /* MAC address */
3690 netdev->dev_addr[0], netdev->dev_addr[1],
3691 netdev->dev_addr[2], netdev->dev_addr[3],
3692 netdev->dev_addr[4], netdev->dev_addr[5]);
3693 ndev_info(netdev, "Intel(R) PRO/%s Network Connection\n",
3694 (hw->phy.type == e1000_phy_ife)
3695 ? "10/100" : "1000");
3696 e1000e_read_part_num(hw, &part_num);
3697 ndev_info(netdev, "MAC: %d, PHY: %d, PBA No: %06x-%03x\n",
3698 hw->mac.type, hw->phy.type,
3699 (part_num >> 8), (part_num & 0xff));
3703 * e1000_probe - Device Initialization Routine
3704 * @pdev: PCI device information struct
3705 * @ent: entry in e1000_pci_tbl
3707 * Returns 0 on success, negative on failure
3709 * e1000_probe initializes an adapter identified by a pci_dev structure.
3710 * The OS initialization, configuring of the adapter private structure,
3711 * and a hardware reset occur.
3713 static int __devinit e1000_probe(struct pci_dev *pdev,
3714 const struct pci_device_id *ent)
3716 struct net_device *netdev;
3717 struct e1000_adapter *adapter;
3718 struct e1000_hw *hw;
3719 const struct e1000_info *ei = e1000_info_tbl[ent->driver_data];
3720 unsigned long mmio_start, mmio_len;
3721 unsigned long flash_start, flash_len;
3723 static int cards_found;
3724 int i, err, pci_using_dac;
3725 u16 eeprom_data = 0;
3726 u16 eeprom_apme_mask = E1000_EEPROM_APME;
3728 e1000e_disable_l1aspm(pdev);
3729 err = pci_enable_device(pdev);
3730 if (err)
3731 return err;
3733 pci_using_dac = 0;
3734 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
3735 if (!err) {
3736 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
3737 if (!err)
3738 pci_using_dac = 1;
3739 } else {
3740 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3741 if (err) {
3742 err = pci_set_consistent_dma_mask(pdev,
3743 DMA_32BIT_MASK);
3744 if (err) {
3745 dev_err(&pdev->dev, "No usable DMA "
3746 "configuration, aborting\n");
3747 goto err_dma;
3752 err = pci_request_regions(pdev, e1000e_driver_name);
3753 if (err)
3754 goto err_pci_reg;
3756 pci_set_master(pdev);
3758 err = -ENOMEM;
3759 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
3760 if (!netdev)
3761 goto err_alloc_etherdev;
3763 SET_NETDEV_DEV(netdev, &pdev->dev);
3765 pci_set_drvdata(pdev, netdev);
3766 adapter = netdev_priv(netdev);
3767 hw = &adapter->hw;
3768 adapter->netdev = netdev;
3769 adapter->pdev = pdev;
3770 adapter->ei = ei;
3771 adapter->pba = ei->pba;
3772 adapter->flags = ei->flags;
3773 adapter->hw.adapter = adapter;
3774 adapter->hw.mac.type = ei->mac;
3775 adapter->msg_enable = (1 << NETIF_MSG_DRV | NETIF_MSG_PROBE) - 1;
3777 mmio_start = pci_resource_start(pdev, 0);
3778 mmio_len = pci_resource_len(pdev, 0);
3780 err = -EIO;
3781 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
3782 if (!adapter->hw.hw_addr)
3783 goto err_ioremap;
3785 if ((adapter->flags & FLAG_HAS_FLASH) &&
3786 (pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
3787 flash_start = pci_resource_start(pdev, 1);
3788 flash_len = pci_resource_len(pdev, 1);
3789 adapter->hw.flash_address = ioremap(flash_start, flash_len);
3790 if (!adapter->hw.flash_address)
3791 goto err_flashmap;
3794 /* construct the net_device struct */
3795 netdev->open = &e1000_open;
3796 netdev->stop = &e1000_close;
3797 netdev->hard_start_xmit = &e1000_xmit_frame;
3798 netdev->get_stats = &e1000_get_stats;
3799 netdev->set_multicast_list = &e1000_set_multi;
3800 netdev->set_mac_address = &e1000_set_mac;
3801 netdev->change_mtu = &e1000_change_mtu;
3802 netdev->do_ioctl = &e1000_ioctl;
3803 e1000e_set_ethtool_ops(netdev);
3804 netdev->tx_timeout = &e1000_tx_timeout;
3805 netdev->watchdog_timeo = 5 * HZ;
3806 netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
3807 netdev->vlan_rx_register = e1000_vlan_rx_register;
3808 netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid;
3809 netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;
3810 #ifdef CONFIG_NET_POLL_CONTROLLER
3811 netdev->poll_controller = e1000_netpoll;
3812 #endif
3813 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
3815 netdev->mem_start = mmio_start;
3816 netdev->mem_end = mmio_start + mmio_len;
3818 adapter->bd_number = cards_found++;
3820 /* setup adapter struct */
3821 err = e1000_sw_init(adapter);
3822 if (err)
3823 goto err_sw_init;
3825 err = -EIO;
3827 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
3828 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
3829 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
3831 err = ei->get_invariants(adapter);
3832 if (err)
3833 goto err_hw_init;
3835 hw->mac.ops.get_bus_info(&adapter->hw);
3837 adapter->hw.phy.wait_for_link = 0;
3839 /* Copper options */
3840 if (adapter->hw.media_type == e1000_media_type_copper) {
3841 adapter->hw.phy.mdix = AUTO_ALL_MODES;
3842 adapter->hw.phy.disable_polarity_correction = 0;
3843 adapter->hw.phy.ms_type = e1000_ms_hw_default;
3846 if (e1000_check_reset_block(&adapter->hw))
3847 ndev_info(netdev,
3848 "PHY reset is blocked due to SOL/IDER session.\n");
3850 netdev->features = NETIF_F_SG |
3851 NETIF_F_HW_CSUM |
3852 NETIF_F_HW_VLAN_TX |
3853 NETIF_F_HW_VLAN_RX;
3855 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER)
3856 netdev->features |= NETIF_F_HW_VLAN_FILTER;
3858 netdev->features |= NETIF_F_TSO;
3859 netdev->features |= NETIF_F_TSO6;
3861 if (pci_using_dac)
3862 netdev->features |= NETIF_F_HIGHDMA;
3864 /* We should not be using LLTX anymore, but we are still TX faster with
3865 * it. */
3866 netdev->features |= NETIF_F_LLTX;
3868 if (e1000e_enable_mng_pass_thru(&adapter->hw))
3869 adapter->flags |= FLAG_MNG_PT_ENABLED;
3871 /* before reading the NVM, reset the controller to
3872 * put the device in a known good starting state */
3873 adapter->hw.mac.ops.reset_hw(&adapter->hw);
3876 * systems with ASPM and others may see the checksum fail on the first
3877 * attempt. Let's give it a few tries
3879 for (i = 0;; i++) {
3880 if (e1000_validate_nvm_checksum(&adapter->hw) >= 0)
3881 break;
3882 if (i == 2) {
3883 ndev_err(netdev, "The NVM Checksum Is Not Valid\n");
3884 err = -EIO;
3885 goto err_eeprom;
3889 /* copy the MAC address out of the NVM */
3890 if (e1000e_read_mac_addr(&adapter->hw))
3891 ndev_err(netdev, "NVM Read Error while reading MAC address\n");
3893 memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
3894 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3896 if (!is_valid_ether_addr(netdev->perm_addr)) {
3897 ndev_err(netdev, "Invalid MAC Address: "
3898 "%02x:%02x:%02x:%02x:%02x:%02x\n",
3899 netdev->perm_addr[0], netdev->perm_addr[1],
3900 netdev->perm_addr[2], netdev->perm_addr[3],
3901 netdev->perm_addr[4], netdev->perm_addr[5]);
3902 err = -EIO;
3903 goto err_eeprom;
3906 init_timer(&adapter->watchdog_timer);
3907 adapter->watchdog_timer.function = &e1000_watchdog;
3908 adapter->watchdog_timer.data = (unsigned long) adapter;
3910 init_timer(&adapter->phy_info_timer);
3911 adapter->phy_info_timer.function = &e1000_update_phy_info;
3912 adapter->phy_info_timer.data = (unsigned long) adapter;
3914 INIT_WORK(&adapter->reset_task, e1000_reset_task);
3915 INIT_WORK(&adapter->watchdog_task, e1000_watchdog_task);
3917 e1000e_check_options(adapter);
3919 /* Initialize link parameters. User can change them with ethtool */
3920 adapter->hw.mac.autoneg = 1;
3921 adapter->fc_autoneg = 1;
3922 adapter->hw.mac.original_fc = e1000_fc_default;
3923 adapter->hw.mac.fc = e1000_fc_default;
3924 adapter->hw.phy.autoneg_advertised = 0x2f;
3926 /* ring size defaults */
3927 adapter->rx_ring->count = 256;
3928 adapter->tx_ring->count = 256;
3931 * Initial Wake on LAN setting - If APM wake is enabled in
3932 * the EEPROM, enable the ACPI Magic Packet filter
3934 if (adapter->flags & FLAG_APME_IN_WUC) {
3935 /* APME bit in EEPROM is mapped to WUC.APME */
3936 eeprom_data = er32(WUC);
3937 eeprom_apme_mask = E1000_WUC_APME;
3938 } else if (adapter->flags & FLAG_APME_IN_CTRL3) {
3939 if (adapter->flags & FLAG_APME_CHECK_PORT_B &&
3940 (adapter->hw.bus.func == 1))
3941 e1000_read_nvm(&adapter->hw,
3942 NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
3943 else
3944 e1000_read_nvm(&adapter->hw,
3945 NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
3948 /* fetch WoL from EEPROM */
3949 if (eeprom_data & eeprom_apme_mask)
3950 adapter->eeprom_wol |= E1000_WUFC_MAG;
3953 * now that we have the eeprom settings, apply the special cases
3954 * where the eeprom may be wrong or the board simply won't support
3955 * wake on lan on a particular port
3957 if (!(adapter->flags & FLAG_HAS_WOL))
3958 adapter->eeprom_wol = 0;
3960 /* initialize the wol settings based on the eeprom settings */
3961 adapter->wol = adapter->eeprom_wol;
3963 /* reset the hardware with the new settings */
3964 e1000e_reset(adapter);
3966 /* If the controller has AMT, do not set DRV_LOAD until the interface
3967 * is up. For all other cases, let the f/w know that the h/w is now
3968 * under the control of the driver. */
3969 if (!(adapter->flags & FLAG_HAS_AMT) ||
3970 !e1000e_check_mng_mode(&adapter->hw))
3971 e1000_get_hw_control(adapter);
3973 /* tell the stack to leave us alone until e1000_open() is called */
3974 netif_carrier_off(netdev);
3975 netif_stop_queue(netdev);
3977 strcpy(netdev->name, "eth%d");
3978 err = register_netdev(netdev);
3979 if (err)
3980 goto err_register;
3982 e1000_print_device_info(adapter);
3984 return 0;
3986 err_register:
3987 err_hw_init:
3988 e1000_release_hw_control(adapter);
3989 err_eeprom:
3990 if (!e1000_check_reset_block(&adapter->hw))
3991 e1000_phy_hw_reset(&adapter->hw);
3993 if (adapter->hw.flash_address)
3994 iounmap(adapter->hw.flash_address);
3996 err_flashmap:
3997 kfree(adapter->tx_ring);
3998 kfree(adapter->rx_ring);
3999 err_sw_init:
4000 iounmap(adapter->hw.hw_addr);
4001 err_ioremap:
4002 free_netdev(netdev);
4003 err_alloc_etherdev:
4004 pci_release_regions(pdev);
4005 err_pci_reg:
4006 err_dma:
4007 pci_disable_device(pdev);
4008 return err;
4012 * e1000_remove - Device Removal Routine
4013 * @pdev: PCI device information struct
4015 * e1000_remove is called by the PCI subsystem to alert the driver
4016 * that it should release a PCI device. The could be caused by a
4017 * Hot-Plug event, or because the driver is going to be removed from
4018 * memory.
4020 static void __devexit e1000_remove(struct pci_dev *pdev)
4022 struct net_device *netdev = pci_get_drvdata(pdev);
4023 struct e1000_adapter *adapter = netdev_priv(netdev);
4025 /* flush_scheduled work may reschedule our watchdog task, so
4026 * explicitly disable watchdog tasks from being rescheduled */
4027 set_bit(__E1000_DOWN, &adapter->state);
4028 del_timer_sync(&adapter->watchdog_timer);
4029 del_timer_sync(&adapter->phy_info_timer);
4031 flush_scheduled_work();
4033 /* Release control of h/w to f/w. If f/w is AMT enabled, this
4034 * would have already happened in close and is redundant. */
4035 e1000_release_hw_control(adapter);
4037 unregister_netdev(netdev);
4039 if (!e1000_check_reset_block(&adapter->hw))
4040 e1000_phy_hw_reset(&adapter->hw);
4042 kfree(adapter->tx_ring);
4043 kfree(adapter->rx_ring);
4045 iounmap(adapter->hw.hw_addr);
4046 if (adapter->hw.flash_address)
4047 iounmap(adapter->hw.flash_address);
4048 pci_release_regions(pdev);
4050 free_netdev(netdev);
4052 pci_disable_device(pdev);
4055 /* PCI Error Recovery (ERS) */
4056 static struct pci_error_handlers e1000_err_handler = {
4057 .error_detected = e1000_io_error_detected,
4058 .slot_reset = e1000_io_slot_reset,
4059 .resume = e1000_io_resume,
4062 static struct pci_device_id e1000_pci_tbl[] = {
4063 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_COPPER), board_82571 },
4064 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_FIBER), board_82571 },
4065 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER), board_82571 },
4066 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER_LP), board_82571 },
4067 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_FIBER), board_82571 },
4068 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES), board_82571 },
4069 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_DUAL), board_82571 },
4070 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_QUAD), board_82571 },
4071 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571PT_QUAD_COPPER), board_82571 },
4072 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI), board_82572 },
4073 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_COPPER), board_82572 },
4074 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_FIBER), board_82572 },
4075 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_SERDES), board_82572 },
4076 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E), board_82573 },
4077 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E_IAMT), board_82573 },
4078 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573L), board_82573 },
4079 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_DPT),
4080 board_80003es2lan },
4081 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_SPT),
4082 board_80003es2lan },
4083 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_DPT),
4084 board_80003es2lan },
4085 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_SPT),
4086 board_80003es2lan },
4087 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE), board_ich8lan },
4088 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_G), board_ich8lan },
4089 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_GT), board_ich8lan },
4090 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_AMT), board_ich8lan },
4091 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_C), board_ich8lan },
4092 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M), board_ich8lan },
4093 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M_AMT), board_ich8lan },
4094 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE), board_ich9lan },
4095 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_G), board_ich9lan },
4096 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_GT), board_ich9lan },
4097 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_AMT), board_ich9lan },
4098 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_C), board_ich9lan },
4100 { } /* terminate list */
4102 MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
4104 /* PCI Device API Driver */
4105 static struct pci_driver e1000_driver = {
4106 .name = e1000e_driver_name,
4107 .id_table = e1000_pci_tbl,
4108 .probe = e1000_probe,
4109 .remove = __devexit_p(e1000_remove),
4110 #ifdef CONFIG_PM
4111 /* Power Managment Hooks */
4112 .suspend = e1000_suspend,
4113 .resume = e1000_resume,
4114 #endif
4115 .shutdown = e1000_shutdown,
4116 .err_handler = &e1000_err_handler
4120 * e1000_init_module - Driver Registration Routine
4122 * e1000_init_module is the first routine called when the driver is
4123 * loaded. All it does is register with the PCI subsystem.
4125 static int __init e1000_init_module(void)
4127 int ret;
4128 printk(KERN_INFO "%s: Intel(R) PRO/1000 Network Driver - %s\n",
4129 e1000e_driver_name, e1000e_driver_version);
4130 printk(KERN_INFO "%s: Copyright (c) 1999-2007 Intel Corporation.\n",
4131 e1000e_driver_name);
4132 ret = pci_register_driver(&e1000_driver);
4134 return ret;
4136 module_init(e1000_init_module);
4139 * e1000_exit_module - Driver Exit Cleanup Routine
4141 * e1000_exit_module is called just before the driver is removed
4142 * from memory.
4144 static void __exit e1000_exit_module(void)
4146 pci_unregister_driver(&e1000_driver);
4148 module_exit(e1000_exit_module);
4151 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
4152 MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
4153 MODULE_LICENSE("GPL");
4154 MODULE_VERSION(DRV_VERSION);
4156 /* e1000_main.c */