added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / net / e1000e / netdev.c
blob7cbe39c4577be8a07e352282ed250625d984a70c
1 /*******************************************************************************
3 Intel PRO/1000 Linux driver
4 Copyright(c) 1999 - 2008 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>
46 #include <linux/pm_qos_params.h>
48 #include "e1000.h"
50 #define DRV_VERSION "0.3.3.3-k6"
51 char e1000e_driver_name[] = "e1000e";
52 const char e1000e_driver_version[] = DRV_VERSION;
54 static const struct e1000_info *e1000_info_tbl[] = {
55 [board_82571] = &e1000_82571_info,
56 [board_82572] = &e1000_82572_info,
57 [board_82573] = &e1000_82573_info,
58 [board_82574] = &e1000_82574_info,
59 [board_80003es2lan] = &e1000_es2_info,
60 [board_ich8lan] = &e1000_ich8_info,
61 [board_ich9lan] = &e1000_ich9_info,
62 [board_ich10lan] = &e1000_ich10_info,
65 #ifdef DEBUG
66 /**
67 * e1000_get_hw_dev_name - return device name string
68 * used by hardware layer to print debugging information
69 **/
70 char *e1000e_get_hw_dev_name(struct e1000_hw *hw)
72 return hw->adapter->netdev->name;
74 #endif
76 /**
77 * e1000_desc_unused - calculate if we have unused descriptors
78 **/
79 static int e1000_desc_unused(struct e1000_ring *ring)
81 if (ring->next_to_clean > ring->next_to_use)
82 return ring->next_to_clean - ring->next_to_use - 1;
84 return ring->count + ring->next_to_clean - ring->next_to_use - 1;
87 /**
88 * e1000_receive_skb - helper function to handle Rx indications
89 * @adapter: board private structure
90 * @status: descriptor status field as written by hardware
91 * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
92 * @skb: pointer to sk_buff to be indicated to stack
93 **/
94 static void e1000_receive_skb(struct e1000_adapter *adapter,
95 struct net_device *netdev,
96 struct sk_buff *skb,
97 u8 status, __le16 vlan)
99 skb->protocol = eth_type_trans(skb, netdev);
101 if (adapter->vlgrp && (status & E1000_RXD_STAT_VP))
102 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
103 le16_to_cpu(vlan));
104 else
105 napi_gro_receive(&adapter->napi, skb);
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 {
142 * IP fragment with UDP payload
143 * Hardware complements the payload checksum, so we undo it
144 * and then put the value in host order for further stack use.
146 __sum16 sum = (__force __sum16)htons(csum);
147 skb->csum = csum_unfold(~sum);
148 skb->ip_summed = CHECKSUM_COMPLETE;
150 adapter->hw_csum_good++;
154 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
155 * @adapter: address of board private structure
157 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
158 int cleaned_count)
160 struct net_device *netdev = adapter->netdev;
161 struct pci_dev *pdev = adapter->pdev;
162 struct e1000_ring *rx_ring = adapter->rx_ring;
163 struct e1000_rx_desc *rx_desc;
164 struct e1000_buffer *buffer_info;
165 struct sk_buff *skb;
166 unsigned int i;
167 unsigned int bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
169 i = rx_ring->next_to_use;
170 buffer_info = &rx_ring->buffer_info[i];
172 while (cleaned_count--) {
173 skb = buffer_info->skb;
174 if (skb) {
175 skb_trim(skb, 0);
176 goto map_skb;
179 skb = netdev_alloc_skb(netdev, bufsz);
180 if (!skb) {
181 /* Better luck next round */
182 adapter->alloc_rx_buff_failed++;
183 break;
187 * Make buffer alignment 2 beyond a 16 byte boundary
188 * this will result in a 16 byte aligned IP header after
189 * the 14 byte MAC header is removed
191 skb_reserve(skb, NET_IP_ALIGN);
193 buffer_info->skb = skb;
194 map_skb:
195 buffer_info->dma = pci_map_single(pdev, skb->data,
196 adapter->rx_buffer_len,
197 PCI_DMA_FROMDEVICE);
198 if (pci_dma_mapping_error(pdev, buffer_info->dma)) {
199 dev_err(&pdev->dev, "RX DMA map failed\n");
200 adapter->rx_dma_failed++;
201 break;
204 rx_desc = E1000_RX_DESC(*rx_ring, i);
205 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
207 i++;
208 if (i == rx_ring->count)
209 i = 0;
210 buffer_info = &rx_ring->buffer_info[i];
213 if (rx_ring->next_to_use != i) {
214 rx_ring->next_to_use = i;
215 if (i-- == 0)
216 i = (rx_ring->count - 1);
219 * Force memory writes to complete before letting h/w
220 * know there are new descriptors to fetch. (Only
221 * applicable for weak-ordered memory model archs,
222 * such as IA-64).
224 wmb();
225 writel(i, adapter->hw.hw_addr + rx_ring->tail);
230 * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
231 * @adapter: address of board private structure
233 static void e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
234 int cleaned_count)
236 struct net_device *netdev = adapter->netdev;
237 struct pci_dev *pdev = adapter->pdev;
238 union e1000_rx_desc_packet_split *rx_desc;
239 struct e1000_ring *rx_ring = adapter->rx_ring;
240 struct e1000_buffer *buffer_info;
241 struct e1000_ps_page *ps_page;
242 struct sk_buff *skb;
243 unsigned int i, j;
245 i = rx_ring->next_to_use;
246 buffer_info = &rx_ring->buffer_info[i];
248 while (cleaned_count--) {
249 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
251 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
252 ps_page = &buffer_info->ps_pages[j];
253 if (j >= adapter->rx_ps_pages) {
254 /* all unused desc entries get hw null ptr */
255 rx_desc->read.buffer_addr[j+1] = ~cpu_to_le64(0);
256 continue;
258 if (!ps_page->page) {
259 ps_page->page = alloc_page(GFP_ATOMIC);
260 if (!ps_page->page) {
261 adapter->alloc_rx_buff_failed++;
262 goto no_buffers;
264 ps_page->dma = pci_map_page(pdev,
265 ps_page->page,
266 0, PAGE_SIZE,
267 PCI_DMA_FROMDEVICE);
268 if (pci_dma_mapping_error(pdev, ps_page->dma)) {
269 dev_err(&adapter->pdev->dev,
270 "RX DMA page map failed\n");
271 adapter->rx_dma_failed++;
272 goto no_buffers;
276 * Refresh the desc even if buffer_addrs
277 * didn't change because each write-back
278 * erases this info.
280 rx_desc->read.buffer_addr[j+1] =
281 cpu_to_le64(ps_page->dma);
284 skb = netdev_alloc_skb(netdev,
285 adapter->rx_ps_bsize0 + NET_IP_ALIGN);
287 if (!skb) {
288 adapter->alloc_rx_buff_failed++;
289 break;
293 * Make buffer alignment 2 beyond a 16 byte boundary
294 * this will result in a 16 byte aligned IP header after
295 * the 14 byte MAC header is removed
297 skb_reserve(skb, NET_IP_ALIGN);
299 buffer_info->skb = skb;
300 buffer_info->dma = pci_map_single(pdev, skb->data,
301 adapter->rx_ps_bsize0,
302 PCI_DMA_FROMDEVICE);
303 if (pci_dma_mapping_error(pdev, buffer_info->dma)) {
304 dev_err(&pdev->dev, "RX DMA map failed\n");
305 adapter->rx_dma_failed++;
306 /* cleanup skb */
307 dev_kfree_skb_any(skb);
308 buffer_info->skb = NULL;
309 break;
312 rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
314 i++;
315 if (i == rx_ring->count)
316 i = 0;
317 buffer_info = &rx_ring->buffer_info[i];
320 no_buffers:
321 if (rx_ring->next_to_use != i) {
322 rx_ring->next_to_use = i;
324 if (!(i--))
325 i = (rx_ring->count - 1);
328 * Force memory writes to complete before letting h/w
329 * know there are new descriptors to fetch. (Only
330 * applicable for weak-ordered memory model archs,
331 * such as IA-64).
333 wmb();
335 * Hardware increments by 16 bytes, but packet split
336 * descriptors are 32 bytes...so we increment tail
337 * twice as much.
339 writel(i<<1, adapter->hw.hw_addr + rx_ring->tail);
344 * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
345 * @adapter: address of board private structure
346 * @cleaned_count: number of buffers to allocate this pass
349 static void e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
350 int cleaned_count)
352 struct net_device *netdev = adapter->netdev;
353 struct pci_dev *pdev = adapter->pdev;
354 struct e1000_rx_desc *rx_desc;
355 struct e1000_ring *rx_ring = adapter->rx_ring;
356 struct e1000_buffer *buffer_info;
357 struct sk_buff *skb;
358 unsigned int i;
359 unsigned int bufsz = 256 -
360 16 /* for skb_reserve */ -
361 NET_IP_ALIGN;
363 i = rx_ring->next_to_use;
364 buffer_info = &rx_ring->buffer_info[i];
366 while (cleaned_count--) {
367 skb = buffer_info->skb;
368 if (skb) {
369 skb_trim(skb, 0);
370 goto check_page;
373 skb = netdev_alloc_skb(netdev, bufsz);
374 if (unlikely(!skb)) {
375 /* Better luck next round */
376 adapter->alloc_rx_buff_failed++;
377 break;
380 /* Make buffer alignment 2 beyond a 16 byte boundary
381 * this will result in a 16 byte aligned IP header after
382 * the 14 byte MAC header is removed
384 skb_reserve(skb, NET_IP_ALIGN);
386 buffer_info->skb = skb;
387 check_page:
388 /* allocate a new page if necessary */
389 if (!buffer_info->page) {
390 buffer_info->page = alloc_page(GFP_ATOMIC);
391 if (unlikely(!buffer_info->page)) {
392 adapter->alloc_rx_buff_failed++;
393 break;
397 if (!buffer_info->dma)
398 buffer_info->dma = pci_map_page(pdev,
399 buffer_info->page, 0,
400 PAGE_SIZE,
401 PCI_DMA_FROMDEVICE);
403 rx_desc = E1000_RX_DESC(*rx_ring, i);
404 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
406 if (unlikely(++i == rx_ring->count))
407 i = 0;
408 buffer_info = &rx_ring->buffer_info[i];
411 if (likely(rx_ring->next_to_use != i)) {
412 rx_ring->next_to_use = i;
413 if (unlikely(i-- == 0))
414 i = (rx_ring->count - 1);
416 /* Force memory writes to complete before letting h/w
417 * know there are new descriptors to fetch. (Only
418 * applicable for weak-ordered memory model archs,
419 * such as IA-64). */
420 wmb();
421 writel(i, adapter->hw.hw_addr + rx_ring->tail);
426 * e1000_clean_rx_irq - Send received data up the network stack; legacy
427 * @adapter: board private structure
429 * the return value indicates whether actual cleaning was done, there
430 * is no guarantee that everything was cleaned
432 static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
433 int *work_done, int work_to_do)
435 struct net_device *netdev = adapter->netdev;
436 struct pci_dev *pdev = adapter->pdev;
437 struct e1000_ring *rx_ring = adapter->rx_ring;
438 struct e1000_rx_desc *rx_desc, *next_rxd;
439 struct e1000_buffer *buffer_info, *next_buffer;
440 u32 length;
441 unsigned int i;
442 int cleaned_count = 0;
443 bool cleaned = 0;
444 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
446 i = rx_ring->next_to_clean;
447 rx_desc = E1000_RX_DESC(*rx_ring, i);
448 buffer_info = &rx_ring->buffer_info[i];
450 while (rx_desc->status & E1000_RXD_STAT_DD) {
451 struct sk_buff *skb;
452 u8 status;
454 if (*work_done >= work_to_do)
455 break;
456 (*work_done)++;
458 status = rx_desc->status;
459 skb = buffer_info->skb;
460 buffer_info->skb = NULL;
462 prefetch(skb->data - NET_IP_ALIGN);
464 i++;
465 if (i == rx_ring->count)
466 i = 0;
467 next_rxd = E1000_RX_DESC(*rx_ring, i);
468 prefetch(next_rxd);
470 next_buffer = &rx_ring->buffer_info[i];
472 cleaned = 1;
473 cleaned_count++;
474 pci_unmap_single(pdev,
475 buffer_info->dma,
476 adapter->rx_buffer_len,
477 PCI_DMA_FROMDEVICE);
478 buffer_info->dma = 0;
480 length = le16_to_cpu(rx_desc->length);
482 /* !EOP means multiple descriptors were used to store a single
483 * packet, also make sure the frame isn't just CRC only */
484 if (!(status & E1000_RXD_STAT_EOP) || (length <= 4)) {
485 /* All receives must fit into a single buffer */
486 e_dbg("%s: Receive packet consumed multiple buffers\n",
487 netdev->name);
488 /* recycle */
489 buffer_info->skb = skb;
490 goto next_desc;
493 if (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK) {
494 /* recycle */
495 buffer_info->skb = skb;
496 goto next_desc;
499 /* adjust length to remove Ethernet CRC */
500 if (!(adapter->flags2 & FLAG2_CRC_STRIPPING))
501 length -= 4;
503 total_rx_bytes += length;
504 total_rx_packets++;
507 * code added for copybreak, this should improve
508 * performance for small packets with large amounts
509 * of reassembly being done in the stack
511 if (length < copybreak) {
512 struct sk_buff *new_skb =
513 netdev_alloc_skb(netdev, length + NET_IP_ALIGN);
514 if (new_skb) {
515 skb_reserve(new_skb, NET_IP_ALIGN);
516 skb_copy_to_linear_data_offset(new_skb,
517 -NET_IP_ALIGN,
518 (skb->data -
519 NET_IP_ALIGN),
520 (length +
521 NET_IP_ALIGN));
522 /* save the skb in buffer_info as good */
523 buffer_info->skb = skb;
524 skb = new_skb;
526 /* else just continue with the old one */
528 /* end copybreak code */
529 skb_put(skb, length);
531 /* Receive Checksum Offload */
532 e1000_rx_checksum(adapter,
533 (u32)(status) |
534 ((u32)(rx_desc->errors) << 24),
535 le16_to_cpu(rx_desc->csum), skb);
537 e1000_receive_skb(adapter, netdev, skb,status,rx_desc->special);
539 next_desc:
540 rx_desc->status = 0;
542 /* return some buffers to hardware, one at a time is too slow */
543 if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
544 adapter->alloc_rx_buf(adapter, cleaned_count);
545 cleaned_count = 0;
548 /* use prefetched values */
549 rx_desc = next_rxd;
550 buffer_info = next_buffer;
552 rx_ring->next_to_clean = i;
554 cleaned_count = e1000_desc_unused(rx_ring);
555 if (cleaned_count)
556 adapter->alloc_rx_buf(adapter, cleaned_count);
558 adapter->total_rx_bytes += total_rx_bytes;
559 adapter->total_rx_packets += total_rx_packets;
560 adapter->net_stats.rx_bytes += total_rx_bytes;
561 adapter->net_stats.rx_packets += total_rx_packets;
562 return cleaned;
565 static void e1000_put_txbuf(struct e1000_adapter *adapter,
566 struct e1000_buffer *buffer_info)
568 buffer_info->dma = 0;
569 if (buffer_info->skb) {
570 skb_dma_unmap(&adapter->pdev->dev, buffer_info->skb,
571 DMA_TO_DEVICE);
572 dev_kfree_skb_any(buffer_info->skb);
573 buffer_info->skb = NULL;
577 static void e1000_print_tx_hang(struct e1000_adapter *adapter)
579 struct e1000_ring *tx_ring = adapter->tx_ring;
580 unsigned int i = tx_ring->next_to_clean;
581 unsigned int eop = tx_ring->buffer_info[i].next_to_watch;
582 struct e1000_tx_desc *eop_desc = E1000_TX_DESC(*tx_ring, eop);
584 /* detected Tx unit hang */
585 e_err("Detected Tx Unit Hang:\n"
586 " TDH <%x>\n"
587 " TDT <%x>\n"
588 " next_to_use <%x>\n"
589 " next_to_clean <%x>\n"
590 "buffer_info[next_to_clean]:\n"
591 " time_stamp <%lx>\n"
592 " next_to_watch <%x>\n"
593 " jiffies <%lx>\n"
594 " next_to_watch.status <%x>\n",
595 readl(adapter->hw.hw_addr + tx_ring->head),
596 readl(adapter->hw.hw_addr + tx_ring->tail),
597 tx_ring->next_to_use,
598 tx_ring->next_to_clean,
599 tx_ring->buffer_info[eop].time_stamp,
600 eop,
601 jiffies,
602 eop_desc->upper.fields.status);
606 * e1000_clean_tx_irq - Reclaim resources after transmit completes
607 * @adapter: board private structure
609 * the return value indicates whether actual cleaning was done, there
610 * is no guarantee that everything was cleaned
612 static bool e1000_clean_tx_irq(struct e1000_adapter *adapter)
614 struct net_device *netdev = adapter->netdev;
615 struct e1000_hw *hw = &adapter->hw;
616 struct e1000_ring *tx_ring = adapter->tx_ring;
617 struct e1000_tx_desc *tx_desc, *eop_desc;
618 struct e1000_buffer *buffer_info;
619 unsigned int i, eop;
620 unsigned int count = 0;
621 bool cleaned = 0;
622 unsigned int total_tx_bytes = 0, total_tx_packets = 0;
624 i = tx_ring->next_to_clean;
625 eop = tx_ring->buffer_info[i].next_to_watch;
626 eop_desc = E1000_TX_DESC(*tx_ring, eop);
628 while (eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
629 for (cleaned = 0; !cleaned; ) {
630 tx_desc = E1000_TX_DESC(*tx_ring, i);
631 buffer_info = &tx_ring->buffer_info[i];
632 cleaned = (i == eop);
634 if (cleaned) {
635 struct sk_buff *skb = buffer_info->skb;
636 unsigned int segs, bytecount;
637 segs = skb_shinfo(skb)->gso_segs ?: 1;
638 /* multiply data chunks by size of headers */
639 bytecount = ((segs - 1) * skb_headlen(skb)) +
640 skb->len;
641 total_tx_packets += segs;
642 total_tx_bytes += bytecount;
645 e1000_put_txbuf(adapter, buffer_info);
646 tx_desc->upper.data = 0;
648 i++;
649 if (i == tx_ring->count)
650 i = 0;
653 eop = tx_ring->buffer_info[i].next_to_watch;
654 eop_desc = E1000_TX_DESC(*tx_ring, eop);
655 #define E1000_TX_WEIGHT 64
656 /* weight of a sort for tx, to avoid endless transmit cleanup */
657 if (count++ == E1000_TX_WEIGHT)
658 break;
661 tx_ring->next_to_clean = i;
663 #define TX_WAKE_THRESHOLD 32
664 if (cleaned && netif_carrier_ok(netdev) &&
665 e1000_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD) {
666 /* Make sure that anybody stopping the queue after this
667 * sees the new next_to_clean.
669 smp_mb();
671 if (netif_queue_stopped(netdev) &&
672 !(test_bit(__E1000_DOWN, &adapter->state))) {
673 netif_wake_queue(netdev);
674 ++adapter->restart_queue;
678 if (adapter->detect_tx_hung) {
680 * Detect a transmit hang in hardware, this serializes the
681 * check with the clearing of time_stamp and movement of i
683 adapter->detect_tx_hung = 0;
685 * read barrier to make sure that the ->dma member and time
686 * stamp are updated fully
688 smp_rmb();
689 if (tx_ring->buffer_info[eop].dma &&
690 time_after(jiffies, tx_ring->buffer_info[eop].time_stamp
691 + (adapter->tx_timeout_factor * HZ))
692 && !(er32(STATUS) & E1000_STATUS_TXOFF)) {
693 e1000_print_tx_hang(adapter);
694 netif_stop_queue(netdev);
697 adapter->total_tx_bytes += total_tx_bytes;
698 adapter->total_tx_packets += total_tx_packets;
699 adapter->net_stats.tx_bytes += total_tx_bytes;
700 adapter->net_stats.tx_packets += total_tx_packets;
701 return cleaned;
705 * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
706 * @adapter: board private structure
708 * the return value indicates whether actual cleaning was done, there
709 * is no guarantee that everything was cleaned
711 static bool e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
712 int *work_done, int work_to_do)
714 union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
715 struct net_device *netdev = adapter->netdev;
716 struct pci_dev *pdev = adapter->pdev;
717 struct e1000_ring *rx_ring = adapter->rx_ring;
718 struct e1000_buffer *buffer_info, *next_buffer;
719 struct e1000_ps_page *ps_page;
720 struct sk_buff *skb;
721 unsigned int i, j;
722 u32 length, staterr;
723 int cleaned_count = 0;
724 bool cleaned = 0;
725 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
727 i = rx_ring->next_to_clean;
728 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
729 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
730 buffer_info = &rx_ring->buffer_info[i];
732 while (staterr & E1000_RXD_STAT_DD) {
733 if (*work_done >= work_to_do)
734 break;
735 (*work_done)++;
736 skb = buffer_info->skb;
738 /* in the packet split case this is header only */
739 prefetch(skb->data - NET_IP_ALIGN);
741 i++;
742 if (i == rx_ring->count)
743 i = 0;
744 next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
745 prefetch(next_rxd);
747 next_buffer = &rx_ring->buffer_info[i];
749 cleaned = 1;
750 cleaned_count++;
751 pci_unmap_single(pdev, buffer_info->dma,
752 adapter->rx_ps_bsize0,
753 PCI_DMA_FROMDEVICE);
754 buffer_info->dma = 0;
756 if (!(staterr & E1000_RXD_STAT_EOP)) {
757 e_dbg("%s: Packet Split buffers didn't pick up the "
758 "full packet\n", netdev->name);
759 dev_kfree_skb_irq(skb);
760 goto next_desc;
763 if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
764 dev_kfree_skb_irq(skb);
765 goto next_desc;
768 length = le16_to_cpu(rx_desc->wb.middle.length0);
770 if (!length) {
771 e_dbg("%s: Last part of the packet spanning multiple "
772 "descriptors\n", netdev->name);
773 dev_kfree_skb_irq(skb);
774 goto next_desc;
777 /* Good Receive */
778 skb_put(skb, length);
782 * this looks ugly, but it seems compiler issues make it
783 * more efficient than reusing j
785 int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
788 * page alloc/put takes too long and effects small packet
789 * throughput, so unsplit small packets and save the alloc/put
790 * only valid in softirq (napi) context to call kmap_*
792 if (l1 && (l1 <= copybreak) &&
793 ((length + l1) <= adapter->rx_ps_bsize0)) {
794 u8 *vaddr;
796 ps_page = &buffer_info->ps_pages[0];
799 * there is no documentation about how to call
800 * kmap_atomic, so we can't hold the mapping
801 * very long
803 pci_dma_sync_single_for_cpu(pdev, ps_page->dma,
804 PAGE_SIZE, PCI_DMA_FROMDEVICE);
805 vaddr = kmap_atomic(ps_page->page, KM_SKB_DATA_SOFTIRQ);
806 memcpy(skb_tail_pointer(skb), vaddr, l1);
807 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
808 pci_dma_sync_single_for_device(pdev, ps_page->dma,
809 PAGE_SIZE, PCI_DMA_FROMDEVICE);
811 /* remove the CRC */
812 if (!(adapter->flags2 & FLAG2_CRC_STRIPPING))
813 l1 -= 4;
815 skb_put(skb, l1);
816 goto copydone;
817 } /* if */
820 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
821 length = le16_to_cpu(rx_desc->wb.upper.length[j]);
822 if (!length)
823 break;
825 ps_page = &buffer_info->ps_pages[j];
826 pci_unmap_page(pdev, ps_page->dma, PAGE_SIZE,
827 PCI_DMA_FROMDEVICE);
828 ps_page->dma = 0;
829 skb_fill_page_desc(skb, j, ps_page->page, 0, length);
830 ps_page->page = NULL;
831 skb->len += length;
832 skb->data_len += length;
833 skb->truesize += length;
836 /* strip the ethernet crc, problem is we're using pages now so
837 * this whole operation can get a little cpu intensive
839 if (!(adapter->flags2 & FLAG2_CRC_STRIPPING))
840 pskb_trim(skb, skb->len - 4);
842 copydone:
843 total_rx_bytes += skb->len;
844 total_rx_packets++;
846 e1000_rx_checksum(adapter, staterr, le16_to_cpu(
847 rx_desc->wb.lower.hi_dword.csum_ip.csum), skb);
849 if (rx_desc->wb.upper.header_status &
850 cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP))
851 adapter->rx_hdr_split++;
853 e1000_receive_skb(adapter, netdev, skb,
854 staterr, rx_desc->wb.middle.vlan);
856 next_desc:
857 rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
858 buffer_info->skb = NULL;
860 /* return some buffers to hardware, one at a time is too slow */
861 if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
862 adapter->alloc_rx_buf(adapter, cleaned_count);
863 cleaned_count = 0;
866 /* use prefetched values */
867 rx_desc = next_rxd;
868 buffer_info = next_buffer;
870 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
872 rx_ring->next_to_clean = i;
874 cleaned_count = e1000_desc_unused(rx_ring);
875 if (cleaned_count)
876 adapter->alloc_rx_buf(adapter, cleaned_count);
878 adapter->total_rx_bytes += total_rx_bytes;
879 adapter->total_rx_packets += total_rx_packets;
880 adapter->net_stats.rx_bytes += total_rx_bytes;
881 adapter->net_stats.rx_packets += total_rx_packets;
882 return cleaned;
886 * e1000_consume_page - helper function
888 static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
889 u16 length)
891 bi->page = NULL;
892 skb->len += length;
893 skb->data_len += length;
894 skb->truesize += length;
898 * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
899 * @adapter: board private structure
901 * the return value indicates whether actual cleaning was done, there
902 * is no guarantee that everything was cleaned
905 static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
906 int *work_done, int work_to_do)
908 struct net_device *netdev = adapter->netdev;
909 struct pci_dev *pdev = adapter->pdev;
910 struct e1000_ring *rx_ring = adapter->rx_ring;
911 struct e1000_rx_desc *rx_desc, *next_rxd;
912 struct e1000_buffer *buffer_info, *next_buffer;
913 u32 length;
914 unsigned int i;
915 int cleaned_count = 0;
916 bool cleaned = false;
917 unsigned int total_rx_bytes=0, total_rx_packets=0;
919 i = rx_ring->next_to_clean;
920 rx_desc = E1000_RX_DESC(*rx_ring, i);
921 buffer_info = &rx_ring->buffer_info[i];
923 while (rx_desc->status & E1000_RXD_STAT_DD) {
924 struct sk_buff *skb;
925 u8 status;
927 if (*work_done >= work_to_do)
928 break;
929 (*work_done)++;
931 status = rx_desc->status;
932 skb = buffer_info->skb;
933 buffer_info->skb = NULL;
935 ++i;
936 if (i == rx_ring->count)
937 i = 0;
938 next_rxd = E1000_RX_DESC(*rx_ring, i);
939 prefetch(next_rxd);
941 next_buffer = &rx_ring->buffer_info[i];
943 cleaned = true;
944 cleaned_count++;
945 pci_unmap_page(pdev, buffer_info->dma, PAGE_SIZE,
946 PCI_DMA_FROMDEVICE);
947 buffer_info->dma = 0;
949 length = le16_to_cpu(rx_desc->length);
951 /* errors is only valid for DD + EOP descriptors */
952 if (unlikely((status & E1000_RXD_STAT_EOP) &&
953 (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
954 /* recycle both page and skb */
955 buffer_info->skb = skb;
956 /* an error means any chain goes out the window
957 * too */
958 if (rx_ring->rx_skb_top)
959 dev_kfree_skb(rx_ring->rx_skb_top);
960 rx_ring->rx_skb_top = NULL;
961 goto next_desc;
964 #define rxtop rx_ring->rx_skb_top
965 if (!(status & E1000_RXD_STAT_EOP)) {
966 /* this descriptor is only the beginning (or middle) */
967 if (!rxtop) {
968 /* this is the beginning of a chain */
969 rxtop = skb;
970 skb_fill_page_desc(rxtop, 0, buffer_info->page,
971 0, length);
972 } else {
973 /* this is the middle of a chain */
974 skb_fill_page_desc(rxtop,
975 skb_shinfo(rxtop)->nr_frags,
976 buffer_info->page, 0, length);
977 /* re-use the skb, only consumed the page */
978 buffer_info->skb = skb;
980 e1000_consume_page(buffer_info, rxtop, length);
981 goto next_desc;
982 } else {
983 if (rxtop) {
984 /* end of the chain */
985 skb_fill_page_desc(rxtop,
986 skb_shinfo(rxtop)->nr_frags,
987 buffer_info->page, 0, length);
988 /* re-use the current skb, we only consumed the
989 * page */
990 buffer_info->skb = skb;
991 skb = rxtop;
992 rxtop = NULL;
993 e1000_consume_page(buffer_info, skb, length);
994 } else {
995 /* no chain, got EOP, this buf is the packet
996 * copybreak to save the put_page/alloc_page */
997 if (length <= copybreak &&
998 skb_tailroom(skb) >= length) {
999 u8 *vaddr;
1000 vaddr = kmap_atomic(buffer_info->page,
1001 KM_SKB_DATA_SOFTIRQ);
1002 memcpy(skb_tail_pointer(skb), vaddr,
1003 length);
1004 kunmap_atomic(vaddr,
1005 KM_SKB_DATA_SOFTIRQ);
1006 /* re-use the page, so don't erase
1007 * buffer_info->page */
1008 skb_put(skb, length);
1009 } else {
1010 skb_fill_page_desc(skb, 0,
1011 buffer_info->page, 0,
1012 length);
1013 e1000_consume_page(buffer_info, skb,
1014 length);
1019 /* Receive Checksum Offload XXX recompute due to CRC strip? */
1020 e1000_rx_checksum(adapter,
1021 (u32)(status) |
1022 ((u32)(rx_desc->errors) << 24),
1023 le16_to_cpu(rx_desc->csum), skb);
1025 /* probably a little skewed due to removing CRC */
1026 total_rx_bytes += skb->len;
1027 total_rx_packets++;
1029 /* eth type trans needs skb->data to point to something */
1030 if (!pskb_may_pull(skb, ETH_HLEN)) {
1031 e_err("pskb_may_pull failed.\n");
1032 dev_kfree_skb(skb);
1033 goto next_desc;
1036 e1000_receive_skb(adapter, netdev, skb, status,
1037 rx_desc->special);
1039 next_desc:
1040 rx_desc->status = 0;
1042 /* return some buffers to hardware, one at a time is too slow */
1043 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
1044 adapter->alloc_rx_buf(adapter, cleaned_count);
1045 cleaned_count = 0;
1048 /* use prefetched values */
1049 rx_desc = next_rxd;
1050 buffer_info = next_buffer;
1052 rx_ring->next_to_clean = i;
1054 cleaned_count = e1000_desc_unused(rx_ring);
1055 if (cleaned_count)
1056 adapter->alloc_rx_buf(adapter, cleaned_count);
1058 adapter->total_rx_bytes += total_rx_bytes;
1059 adapter->total_rx_packets += total_rx_packets;
1060 adapter->net_stats.rx_bytes += total_rx_bytes;
1061 adapter->net_stats.rx_packets += total_rx_packets;
1062 return cleaned;
1066 * e1000_clean_rx_ring - Free Rx Buffers per Queue
1067 * @adapter: board private structure
1069 static void e1000_clean_rx_ring(struct e1000_adapter *adapter)
1071 struct e1000_ring *rx_ring = adapter->rx_ring;
1072 struct e1000_buffer *buffer_info;
1073 struct e1000_ps_page *ps_page;
1074 struct pci_dev *pdev = adapter->pdev;
1075 unsigned int i, j;
1077 /* Free all the Rx ring sk_buffs */
1078 for (i = 0; i < rx_ring->count; i++) {
1079 buffer_info = &rx_ring->buffer_info[i];
1080 if (buffer_info->dma) {
1081 if (adapter->clean_rx == e1000_clean_rx_irq)
1082 pci_unmap_single(pdev, buffer_info->dma,
1083 adapter->rx_buffer_len,
1084 PCI_DMA_FROMDEVICE);
1085 else if (adapter->clean_rx == e1000_clean_jumbo_rx_irq)
1086 pci_unmap_page(pdev, buffer_info->dma,
1087 PAGE_SIZE,
1088 PCI_DMA_FROMDEVICE);
1089 else if (adapter->clean_rx == e1000_clean_rx_irq_ps)
1090 pci_unmap_single(pdev, buffer_info->dma,
1091 adapter->rx_ps_bsize0,
1092 PCI_DMA_FROMDEVICE);
1093 buffer_info->dma = 0;
1096 if (buffer_info->page) {
1097 put_page(buffer_info->page);
1098 buffer_info->page = NULL;
1101 if (buffer_info->skb) {
1102 dev_kfree_skb(buffer_info->skb);
1103 buffer_info->skb = NULL;
1106 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
1107 ps_page = &buffer_info->ps_pages[j];
1108 if (!ps_page->page)
1109 break;
1110 pci_unmap_page(pdev, ps_page->dma, PAGE_SIZE,
1111 PCI_DMA_FROMDEVICE);
1112 ps_page->dma = 0;
1113 put_page(ps_page->page);
1114 ps_page->page = NULL;
1118 /* there also may be some cached data from a chained receive */
1119 if (rx_ring->rx_skb_top) {
1120 dev_kfree_skb(rx_ring->rx_skb_top);
1121 rx_ring->rx_skb_top = NULL;
1124 /* Zero out the descriptor ring */
1125 memset(rx_ring->desc, 0, rx_ring->size);
1127 rx_ring->next_to_clean = 0;
1128 rx_ring->next_to_use = 0;
1130 writel(0, adapter->hw.hw_addr + rx_ring->head);
1131 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1134 static void e1000e_downshift_workaround(struct work_struct *work)
1136 struct e1000_adapter *adapter = container_of(work,
1137 struct e1000_adapter, downshift_task);
1139 e1000e_gig_downshift_workaround_ich8lan(&adapter->hw);
1143 * e1000_intr_msi - Interrupt Handler
1144 * @irq: interrupt number
1145 * @data: pointer to a network interface device structure
1147 static irqreturn_t e1000_intr_msi(int irq, void *data)
1149 struct net_device *netdev = data;
1150 struct e1000_adapter *adapter = netdev_priv(netdev);
1151 struct e1000_hw *hw = &adapter->hw;
1152 u32 icr = er32(ICR);
1155 * read ICR disables interrupts using IAM
1158 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
1159 hw->mac.get_link_status = 1;
1161 * ICH8 workaround-- Call gig speed drop workaround on cable
1162 * disconnect (LSC) before accessing any PHY registers
1164 if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
1165 (!(er32(STATUS) & E1000_STATUS_LU)))
1166 schedule_work(&adapter->downshift_task);
1169 * 80003ES2LAN workaround-- For packet buffer work-around on
1170 * link down event; disable receives here in the ISR and reset
1171 * adapter in watchdog
1173 if (netif_carrier_ok(netdev) &&
1174 adapter->flags & FLAG_RX_NEEDS_RESTART) {
1175 /* disable receives */
1176 u32 rctl = er32(RCTL);
1177 ew32(RCTL, rctl & ~E1000_RCTL_EN);
1178 adapter->flags |= FLAG_RX_RESTART_NOW;
1180 /* guard against interrupt when we're going down */
1181 if (!test_bit(__E1000_DOWN, &adapter->state))
1182 mod_timer(&adapter->watchdog_timer, jiffies + 1);
1185 if (netif_rx_schedule_prep(&adapter->napi)) {
1186 adapter->total_tx_bytes = 0;
1187 adapter->total_tx_packets = 0;
1188 adapter->total_rx_bytes = 0;
1189 adapter->total_rx_packets = 0;
1190 __netif_rx_schedule(&adapter->napi);
1193 return IRQ_HANDLED;
1197 * e1000_intr - Interrupt Handler
1198 * @irq: interrupt number
1199 * @data: pointer to a network interface device structure
1201 static irqreturn_t e1000_intr(int irq, void *data)
1203 struct net_device *netdev = data;
1204 struct e1000_adapter *adapter = netdev_priv(netdev);
1205 struct e1000_hw *hw = &adapter->hw;
1206 u32 rctl, icr = er32(ICR);
1208 if (!icr)
1209 return IRQ_NONE; /* Not our interrupt */
1212 * IMS will not auto-mask if INT_ASSERTED is not set, and if it is
1213 * not set, then the adapter didn't send an interrupt
1215 if (!(icr & E1000_ICR_INT_ASSERTED))
1216 return IRQ_NONE;
1219 * Interrupt Auto-Mask...upon reading ICR,
1220 * interrupts are masked. No need for the
1221 * IMC write
1224 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
1225 hw->mac.get_link_status = 1;
1227 * ICH8 workaround-- Call gig speed drop workaround on cable
1228 * disconnect (LSC) before accessing any PHY registers
1230 if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
1231 (!(er32(STATUS) & E1000_STATUS_LU)))
1232 schedule_work(&adapter->downshift_task);
1235 * 80003ES2LAN workaround--
1236 * For packet buffer work-around on link down event;
1237 * disable receives here in the ISR and
1238 * reset adapter in watchdog
1240 if (netif_carrier_ok(netdev) &&
1241 (adapter->flags & FLAG_RX_NEEDS_RESTART)) {
1242 /* disable receives */
1243 rctl = er32(RCTL);
1244 ew32(RCTL, rctl & ~E1000_RCTL_EN);
1245 adapter->flags |= FLAG_RX_RESTART_NOW;
1247 /* guard against interrupt when we're going down */
1248 if (!test_bit(__E1000_DOWN, &adapter->state))
1249 mod_timer(&adapter->watchdog_timer, jiffies + 1);
1252 if (netif_rx_schedule_prep(&adapter->napi)) {
1253 adapter->total_tx_bytes = 0;
1254 adapter->total_tx_packets = 0;
1255 adapter->total_rx_bytes = 0;
1256 adapter->total_rx_packets = 0;
1257 __netif_rx_schedule(&adapter->napi);
1260 return IRQ_HANDLED;
1263 static irqreturn_t e1000_msix_other(int irq, void *data)
1265 struct net_device *netdev = data;
1266 struct e1000_adapter *adapter = netdev_priv(netdev);
1267 struct e1000_hw *hw = &adapter->hw;
1268 u32 icr = er32(ICR);
1270 if (!(icr & E1000_ICR_INT_ASSERTED)) {
1271 ew32(IMS, E1000_IMS_OTHER);
1272 return IRQ_NONE;
1275 if (icr & adapter->eiac_mask)
1276 ew32(ICS, (icr & adapter->eiac_mask));
1278 if (icr & E1000_ICR_OTHER) {
1279 if (!(icr & E1000_ICR_LSC))
1280 goto no_link_interrupt;
1281 hw->mac.get_link_status = 1;
1282 /* guard against interrupt when we're going down */
1283 if (!test_bit(__E1000_DOWN, &adapter->state))
1284 mod_timer(&adapter->watchdog_timer, jiffies + 1);
1287 no_link_interrupt:
1288 ew32(IMS, E1000_IMS_LSC | E1000_IMS_OTHER);
1290 return IRQ_HANDLED;
1294 static irqreturn_t e1000_intr_msix_tx(int irq, void *data)
1296 struct net_device *netdev = data;
1297 struct e1000_adapter *adapter = netdev_priv(netdev);
1298 struct e1000_hw *hw = &adapter->hw;
1299 struct e1000_ring *tx_ring = adapter->tx_ring;
1302 adapter->total_tx_bytes = 0;
1303 adapter->total_tx_packets = 0;
1305 if (!e1000_clean_tx_irq(adapter))
1306 /* Ring was not completely cleaned, so fire another interrupt */
1307 ew32(ICS, tx_ring->ims_val);
1309 return IRQ_HANDLED;
1312 static irqreturn_t e1000_intr_msix_rx(int irq, void *data)
1314 struct net_device *netdev = data;
1315 struct e1000_adapter *adapter = netdev_priv(netdev);
1317 /* Write the ITR value calculated at the end of the
1318 * previous interrupt.
1320 if (adapter->rx_ring->set_itr) {
1321 writel(1000000000 / (adapter->rx_ring->itr_val * 256),
1322 adapter->hw.hw_addr + adapter->rx_ring->itr_register);
1323 adapter->rx_ring->set_itr = 0;
1326 if (netif_rx_schedule_prep(&adapter->napi)) {
1327 adapter->total_rx_bytes = 0;
1328 adapter->total_rx_packets = 0;
1329 __netif_rx_schedule(&adapter->napi);
1331 return IRQ_HANDLED;
1335 * e1000_configure_msix - Configure MSI-X hardware
1337 * e1000_configure_msix sets up the hardware to properly
1338 * generate MSI-X interrupts.
1340 static void e1000_configure_msix(struct e1000_adapter *adapter)
1342 struct e1000_hw *hw = &adapter->hw;
1343 struct e1000_ring *rx_ring = adapter->rx_ring;
1344 struct e1000_ring *tx_ring = adapter->tx_ring;
1345 int vector = 0;
1346 u32 ctrl_ext, ivar = 0;
1348 adapter->eiac_mask = 0;
1350 /* Workaround issue with spurious interrupts on 82574 in MSI-X mode */
1351 if (hw->mac.type == e1000_82574) {
1352 u32 rfctl = er32(RFCTL);
1353 rfctl |= E1000_RFCTL_ACK_DIS;
1354 ew32(RFCTL, rfctl);
1357 #define E1000_IVAR_INT_ALLOC_VALID 0x8
1358 /* Configure Rx vector */
1359 rx_ring->ims_val = E1000_IMS_RXQ0;
1360 adapter->eiac_mask |= rx_ring->ims_val;
1361 if (rx_ring->itr_val)
1362 writel(1000000000 / (rx_ring->itr_val * 256),
1363 hw->hw_addr + rx_ring->itr_register);
1364 else
1365 writel(1, hw->hw_addr + rx_ring->itr_register);
1366 ivar = E1000_IVAR_INT_ALLOC_VALID | vector;
1368 /* Configure Tx vector */
1369 tx_ring->ims_val = E1000_IMS_TXQ0;
1370 vector++;
1371 if (tx_ring->itr_val)
1372 writel(1000000000 / (tx_ring->itr_val * 256),
1373 hw->hw_addr + tx_ring->itr_register);
1374 else
1375 writel(1, hw->hw_addr + tx_ring->itr_register);
1376 adapter->eiac_mask |= tx_ring->ims_val;
1377 ivar |= ((E1000_IVAR_INT_ALLOC_VALID | vector) << 8);
1379 /* set vector for Other Causes, e.g. link changes */
1380 vector++;
1381 ivar |= ((E1000_IVAR_INT_ALLOC_VALID | vector) << 16);
1382 if (rx_ring->itr_val)
1383 writel(1000000000 / (rx_ring->itr_val * 256),
1384 hw->hw_addr + E1000_EITR_82574(vector));
1385 else
1386 writel(1, hw->hw_addr + E1000_EITR_82574(vector));
1388 /* Cause Tx interrupts on every write back */
1389 ivar |= (1 << 31);
1391 ew32(IVAR, ivar);
1393 /* enable MSI-X PBA support */
1394 ctrl_ext = er32(CTRL_EXT);
1395 ctrl_ext |= E1000_CTRL_EXT_PBA_CLR;
1397 /* Auto-Mask Other interrupts upon ICR read */
1398 #define E1000_EIAC_MASK_82574 0x01F00000
1399 ew32(IAM, ~E1000_EIAC_MASK_82574 | E1000_IMS_OTHER);
1400 ctrl_ext |= E1000_CTRL_EXT_EIAME;
1401 ew32(CTRL_EXT, ctrl_ext);
1402 e1e_flush();
1405 void e1000e_reset_interrupt_capability(struct e1000_adapter *adapter)
1407 if (adapter->msix_entries) {
1408 pci_disable_msix(adapter->pdev);
1409 kfree(adapter->msix_entries);
1410 adapter->msix_entries = NULL;
1411 } else if (adapter->flags & FLAG_MSI_ENABLED) {
1412 pci_disable_msi(adapter->pdev);
1413 adapter->flags &= ~FLAG_MSI_ENABLED;
1416 return;
1420 * e1000e_set_interrupt_capability - set MSI or MSI-X if supported
1422 * Attempt to configure interrupts using the best available
1423 * capabilities of the hardware and kernel.
1425 void e1000e_set_interrupt_capability(struct e1000_adapter *adapter)
1427 int err;
1428 int numvecs, i;
1431 switch (adapter->int_mode) {
1432 case E1000E_INT_MODE_MSIX:
1433 if (adapter->flags & FLAG_HAS_MSIX) {
1434 numvecs = 3; /* RxQ0, TxQ0 and other */
1435 adapter->msix_entries = kcalloc(numvecs,
1436 sizeof(struct msix_entry),
1437 GFP_KERNEL);
1438 if (adapter->msix_entries) {
1439 for (i = 0; i < numvecs; i++)
1440 adapter->msix_entries[i].entry = i;
1442 err = pci_enable_msix(adapter->pdev,
1443 adapter->msix_entries,
1444 numvecs);
1445 if (err == 0)
1446 return;
1448 /* MSI-X failed, so fall through and try MSI */
1449 e_err("Failed to initialize MSI-X interrupts. "
1450 "Falling back to MSI interrupts.\n");
1451 e1000e_reset_interrupt_capability(adapter);
1453 adapter->int_mode = E1000E_INT_MODE_MSI;
1454 /* Fall through */
1455 case E1000E_INT_MODE_MSI:
1456 if (!pci_enable_msi(adapter->pdev)) {
1457 adapter->flags |= FLAG_MSI_ENABLED;
1458 } else {
1459 adapter->int_mode = E1000E_INT_MODE_LEGACY;
1460 e_err("Failed to initialize MSI interrupts. Falling "
1461 "back to legacy interrupts.\n");
1463 /* Fall through */
1464 case E1000E_INT_MODE_LEGACY:
1465 /* Don't do anything; this is the system default */
1466 break;
1469 return;
1473 * e1000_request_msix - Initialize MSI-X interrupts
1475 * e1000_request_msix allocates MSI-X vectors and requests interrupts from the
1476 * kernel.
1478 static int e1000_request_msix(struct e1000_adapter *adapter)
1480 struct net_device *netdev = adapter->netdev;
1481 int err = 0, vector = 0;
1483 if (strlen(netdev->name) < (IFNAMSIZ - 5))
1484 sprintf(adapter->rx_ring->name, "%s-rx-0", netdev->name);
1485 else
1486 memcpy(adapter->rx_ring->name, netdev->name, IFNAMSIZ);
1487 err = request_irq(adapter->msix_entries[vector].vector,
1488 &e1000_intr_msix_rx, 0, adapter->rx_ring->name,
1489 netdev);
1490 if (err)
1491 goto out;
1492 adapter->rx_ring->itr_register = E1000_EITR_82574(vector);
1493 adapter->rx_ring->itr_val = adapter->itr;
1494 vector++;
1496 if (strlen(netdev->name) < (IFNAMSIZ - 5))
1497 sprintf(adapter->tx_ring->name, "%s-tx-0", netdev->name);
1498 else
1499 memcpy(adapter->tx_ring->name, netdev->name, IFNAMSIZ);
1500 err = request_irq(adapter->msix_entries[vector].vector,
1501 &e1000_intr_msix_tx, 0, adapter->tx_ring->name,
1502 netdev);
1503 if (err)
1504 goto out;
1505 adapter->tx_ring->itr_register = E1000_EITR_82574(vector);
1506 adapter->tx_ring->itr_val = adapter->itr;
1507 vector++;
1509 err = request_irq(adapter->msix_entries[vector].vector,
1510 &e1000_msix_other, 0, netdev->name, netdev);
1511 if (err)
1512 goto out;
1514 e1000_configure_msix(adapter);
1515 return 0;
1516 out:
1517 return err;
1521 * e1000_request_irq - initialize interrupts
1523 * Attempts to configure interrupts using the best available
1524 * capabilities of the hardware and kernel.
1526 static int e1000_request_irq(struct e1000_adapter *adapter)
1528 struct net_device *netdev = adapter->netdev;
1529 int err;
1531 if (adapter->msix_entries) {
1532 err = e1000_request_msix(adapter);
1533 if (!err)
1534 return err;
1535 /* fall back to MSI */
1536 e1000e_reset_interrupt_capability(adapter);
1537 adapter->int_mode = E1000E_INT_MODE_MSI;
1538 e1000e_set_interrupt_capability(adapter);
1540 if (adapter->flags & FLAG_MSI_ENABLED) {
1541 err = request_irq(adapter->pdev->irq, &e1000_intr_msi, 0,
1542 netdev->name, netdev);
1543 if (!err)
1544 return err;
1546 /* fall back to legacy interrupt */
1547 e1000e_reset_interrupt_capability(adapter);
1548 adapter->int_mode = E1000E_INT_MODE_LEGACY;
1551 err = request_irq(adapter->pdev->irq, &e1000_intr, IRQF_SHARED,
1552 netdev->name, netdev);
1553 if (err)
1554 e_err("Unable to allocate interrupt, Error: %d\n", err);
1556 return err;
1559 static void e1000_free_irq(struct e1000_adapter *adapter)
1561 struct net_device *netdev = adapter->netdev;
1563 if (adapter->msix_entries) {
1564 int vector = 0;
1566 free_irq(adapter->msix_entries[vector].vector, netdev);
1567 vector++;
1569 free_irq(adapter->msix_entries[vector].vector, netdev);
1570 vector++;
1572 /* Other Causes interrupt vector */
1573 free_irq(adapter->msix_entries[vector].vector, netdev);
1574 return;
1577 free_irq(adapter->pdev->irq, netdev);
1581 * e1000_irq_disable - Mask off interrupt generation on the NIC
1583 static void e1000_irq_disable(struct e1000_adapter *adapter)
1585 struct e1000_hw *hw = &adapter->hw;
1587 ew32(IMC, ~0);
1588 if (adapter->msix_entries)
1589 ew32(EIAC_82574, 0);
1590 e1e_flush();
1591 synchronize_irq(adapter->pdev->irq);
1595 * e1000_irq_enable - Enable default interrupt generation settings
1597 static void e1000_irq_enable(struct e1000_adapter *adapter)
1599 struct e1000_hw *hw = &adapter->hw;
1601 if (adapter->msix_entries) {
1602 ew32(EIAC_82574, adapter->eiac_mask & E1000_EIAC_MASK_82574);
1603 ew32(IMS, adapter->eiac_mask | E1000_IMS_OTHER | E1000_IMS_LSC);
1604 } else {
1605 ew32(IMS, IMS_ENABLE_MASK);
1607 e1e_flush();
1611 * e1000_get_hw_control - get control of the h/w from f/w
1612 * @adapter: address of board private structure
1614 * e1000_get_hw_control sets {CTRL_EXT|SWSM}:DRV_LOAD bit.
1615 * For ASF and Pass Through versions of f/w this means that
1616 * the driver is loaded. For AMT version (only with 82573)
1617 * of the f/w this means that the network i/f is open.
1619 static void e1000_get_hw_control(struct e1000_adapter *adapter)
1621 struct e1000_hw *hw = &adapter->hw;
1622 u32 ctrl_ext;
1623 u32 swsm;
1625 /* Let firmware know the driver has taken over */
1626 if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
1627 swsm = er32(SWSM);
1628 ew32(SWSM, swsm | E1000_SWSM_DRV_LOAD);
1629 } else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
1630 ctrl_ext = er32(CTRL_EXT);
1631 ew32(CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1636 * e1000_release_hw_control - release control of the h/w to f/w
1637 * @adapter: address of board private structure
1639 * e1000_release_hw_control resets {CTRL_EXT|SWSM}:DRV_LOAD bit.
1640 * For ASF and Pass Through versions of f/w this means that the
1641 * driver is no longer loaded. For AMT version (only with 82573) i
1642 * of the f/w this means that the network i/f is closed.
1645 static void e1000_release_hw_control(struct e1000_adapter *adapter)
1647 struct e1000_hw *hw = &adapter->hw;
1648 u32 ctrl_ext;
1649 u32 swsm;
1651 /* Let firmware taken over control of h/w */
1652 if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
1653 swsm = er32(SWSM);
1654 ew32(SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
1655 } else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
1656 ctrl_ext = er32(CTRL_EXT);
1657 ew32(CTRL_EXT, ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1662 * @e1000_alloc_ring - allocate memory for a ring structure
1664 static int e1000_alloc_ring_dma(struct e1000_adapter *adapter,
1665 struct e1000_ring *ring)
1667 struct pci_dev *pdev = adapter->pdev;
1669 ring->desc = dma_alloc_coherent(&pdev->dev, ring->size, &ring->dma,
1670 GFP_KERNEL);
1671 if (!ring->desc)
1672 return -ENOMEM;
1674 return 0;
1678 * e1000e_setup_tx_resources - allocate Tx resources (Descriptors)
1679 * @adapter: board private structure
1681 * Return 0 on success, negative on failure
1683 int e1000e_setup_tx_resources(struct e1000_adapter *adapter)
1685 struct e1000_ring *tx_ring = adapter->tx_ring;
1686 int err = -ENOMEM, size;
1688 size = sizeof(struct e1000_buffer) * tx_ring->count;
1689 tx_ring->buffer_info = vmalloc(size);
1690 if (!tx_ring->buffer_info)
1691 goto err;
1692 memset(tx_ring->buffer_info, 0, size);
1694 /* round up to nearest 4K */
1695 tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
1696 tx_ring->size = ALIGN(tx_ring->size, 4096);
1698 err = e1000_alloc_ring_dma(adapter, tx_ring);
1699 if (err)
1700 goto err;
1702 tx_ring->next_to_use = 0;
1703 tx_ring->next_to_clean = 0;
1704 spin_lock_init(&adapter->tx_queue_lock);
1706 return 0;
1707 err:
1708 vfree(tx_ring->buffer_info);
1709 e_err("Unable to allocate memory for the transmit descriptor ring\n");
1710 return err;
1714 * e1000e_setup_rx_resources - allocate Rx resources (Descriptors)
1715 * @adapter: board private structure
1717 * Returns 0 on success, negative on failure
1719 int e1000e_setup_rx_resources(struct e1000_adapter *adapter)
1721 struct e1000_ring *rx_ring = adapter->rx_ring;
1722 struct e1000_buffer *buffer_info;
1723 int i, size, desc_len, err = -ENOMEM;
1725 size = sizeof(struct e1000_buffer) * rx_ring->count;
1726 rx_ring->buffer_info = vmalloc(size);
1727 if (!rx_ring->buffer_info)
1728 goto err;
1729 memset(rx_ring->buffer_info, 0, size);
1731 for (i = 0; i < rx_ring->count; i++) {
1732 buffer_info = &rx_ring->buffer_info[i];
1733 buffer_info->ps_pages = kcalloc(PS_PAGE_BUFFERS,
1734 sizeof(struct e1000_ps_page),
1735 GFP_KERNEL);
1736 if (!buffer_info->ps_pages)
1737 goto err_pages;
1740 desc_len = sizeof(union e1000_rx_desc_packet_split);
1742 /* Round up to nearest 4K */
1743 rx_ring->size = rx_ring->count * desc_len;
1744 rx_ring->size = ALIGN(rx_ring->size, 4096);
1746 err = e1000_alloc_ring_dma(adapter, rx_ring);
1747 if (err)
1748 goto err_pages;
1750 rx_ring->next_to_clean = 0;
1751 rx_ring->next_to_use = 0;
1752 rx_ring->rx_skb_top = NULL;
1754 return 0;
1756 err_pages:
1757 for (i = 0; i < rx_ring->count; i++) {
1758 buffer_info = &rx_ring->buffer_info[i];
1759 kfree(buffer_info->ps_pages);
1761 err:
1762 vfree(rx_ring->buffer_info);
1763 e_err("Unable to allocate memory for the transmit descriptor ring\n");
1764 return err;
1768 * e1000_clean_tx_ring - Free Tx Buffers
1769 * @adapter: board private structure
1771 static void e1000_clean_tx_ring(struct e1000_adapter *adapter)
1773 struct e1000_ring *tx_ring = adapter->tx_ring;
1774 struct e1000_buffer *buffer_info;
1775 unsigned long size;
1776 unsigned int i;
1778 for (i = 0; i < tx_ring->count; i++) {
1779 buffer_info = &tx_ring->buffer_info[i];
1780 e1000_put_txbuf(adapter, buffer_info);
1783 size = sizeof(struct e1000_buffer) * tx_ring->count;
1784 memset(tx_ring->buffer_info, 0, size);
1786 memset(tx_ring->desc, 0, tx_ring->size);
1788 tx_ring->next_to_use = 0;
1789 tx_ring->next_to_clean = 0;
1791 writel(0, adapter->hw.hw_addr + tx_ring->head);
1792 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1796 * e1000e_free_tx_resources - Free Tx Resources per Queue
1797 * @adapter: board private structure
1799 * Free all transmit software resources
1801 void e1000e_free_tx_resources(struct e1000_adapter *adapter)
1803 struct pci_dev *pdev = adapter->pdev;
1804 struct e1000_ring *tx_ring = adapter->tx_ring;
1806 e1000_clean_tx_ring(adapter);
1808 vfree(tx_ring->buffer_info);
1809 tx_ring->buffer_info = NULL;
1811 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
1812 tx_ring->dma);
1813 tx_ring->desc = NULL;
1817 * e1000e_free_rx_resources - Free Rx Resources
1818 * @adapter: board private structure
1820 * Free all receive software resources
1823 void e1000e_free_rx_resources(struct e1000_adapter *adapter)
1825 struct pci_dev *pdev = adapter->pdev;
1826 struct e1000_ring *rx_ring = adapter->rx_ring;
1827 int i;
1829 e1000_clean_rx_ring(adapter);
1831 for (i = 0; i < rx_ring->count; i++) {
1832 kfree(rx_ring->buffer_info[i].ps_pages);
1835 vfree(rx_ring->buffer_info);
1836 rx_ring->buffer_info = NULL;
1838 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
1839 rx_ring->dma);
1840 rx_ring->desc = NULL;
1844 * e1000_update_itr - update the dynamic ITR value based on statistics
1845 * @adapter: pointer to adapter
1846 * @itr_setting: current adapter->itr
1847 * @packets: the number of packets during this measurement interval
1848 * @bytes: the number of bytes during this measurement interval
1850 * Stores a new ITR value based on packets and byte
1851 * counts during the last interrupt. The advantage of per interrupt
1852 * computation is faster updates and more accurate ITR for the current
1853 * traffic pattern. Constants in this function were computed
1854 * based on theoretical maximum wire speed and thresholds were set based
1855 * on testing data as well as attempting to minimize response time
1856 * while increasing bulk throughput. This functionality is controlled
1857 * by the InterruptThrottleRate module parameter.
1859 static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
1860 u16 itr_setting, int packets,
1861 int bytes)
1863 unsigned int retval = itr_setting;
1865 if (packets == 0)
1866 goto update_itr_done;
1868 switch (itr_setting) {
1869 case lowest_latency:
1870 /* handle TSO and jumbo frames */
1871 if (bytes/packets > 8000)
1872 retval = bulk_latency;
1873 else if ((packets < 5) && (bytes > 512)) {
1874 retval = low_latency;
1876 break;
1877 case low_latency: /* 50 usec aka 20000 ints/s */
1878 if (bytes > 10000) {
1879 /* this if handles the TSO accounting */
1880 if (bytes/packets > 8000) {
1881 retval = bulk_latency;
1882 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
1883 retval = bulk_latency;
1884 } else if ((packets > 35)) {
1885 retval = lowest_latency;
1887 } else if (bytes/packets > 2000) {
1888 retval = bulk_latency;
1889 } else if (packets <= 2 && bytes < 512) {
1890 retval = lowest_latency;
1892 break;
1893 case bulk_latency: /* 250 usec aka 4000 ints/s */
1894 if (bytes > 25000) {
1895 if (packets > 35) {
1896 retval = low_latency;
1898 } else if (bytes < 6000) {
1899 retval = low_latency;
1901 break;
1904 update_itr_done:
1905 return retval;
1908 static void e1000_set_itr(struct e1000_adapter *adapter)
1910 struct e1000_hw *hw = &adapter->hw;
1911 u16 current_itr;
1912 u32 new_itr = adapter->itr;
1914 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
1915 if (adapter->link_speed != SPEED_1000) {
1916 current_itr = 0;
1917 new_itr = 4000;
1918 goto set_itr_now;
1921 adapter->tx_itr = e1000_update_itr(adapter,
1922 adapter->tx_itr,
1923 adapter->total_tx_packets,
1924 adapter->total_tx_bytes);
1925 /* conservative mode (itr 3) eliminates the lowest_latency setting */
1926 if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
1927 adapter->tx_itr = low_latency;
1929 adapter->rx_itr = e1000_update_itr(adapter,
1930 adapter->rx_itr,
1931 adapter->total_rx_packets,
1932 adapter->total_rx_bytes);
1933 /* conservative mode (itr 3) eliminates the lowest_latency setting */
1934 if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
1935 adapter->rx_itr = low_latency;
1937 current_itr = max(adapter->rx_itr, adapter->tx_itr);
1939 switch (current_itr) {
1940 /* counts and packets in update_itr are dependent on these numbers */
1941 case lowest_latency:
1942 new_itr = 70000;
1943 break;
1944 case low_latency:
1945 new_itr = 20000; /* aka hwitr = ~200 */
1946 break;
1947 case bulk_latency:
1948 new_itr = 4000;
1949 break;
1950 default:
1951 break;
1954 set_itr_now:
1955 if (new_itr != adapter->itr) {
1957 * this attempts to bias the interrupt rate towards Bulk
1958 * by adding intermediate steps when interrupt rate is
1959 * increasing
1961 new_itr = new_itr > adapter->itr ?
1962 min(adapter->itr + (new_itr >> 2), new_itr) :
1963 new_itr;
1964 adapter->itr = new_itr;
1965 adapter->rx_ring->itr_val = new_itr;
1966 if (adapter->msix_entries)
1967 adapter->rx_ring->set_itr = 1;
1968 else
1969 ew32(ITR, 1000000000 / (new_itr * 256));
1974 * e1000_alloc_queues - Allocate memory for all rings
1975 * @adapter: board private structure to initialize
1977 static int __devinit e1000_alloc_queues(struct e1000_adapter *adapter)
1979 adapter->tx_ring = kzalloc(sizeof(struct e1000_ring), GFP_KERNEL);
1980 if (!adapter->tx_ring)
1981 goto err;
1983 adapter->rx_ring = kzalloc(sizeof(struct e1000_ring), GFP_KERNEL);
1984 if (!adapter->rx_ring)
1985 goto err;
1987 return 0;
1988 err:
1989 e_err("Unable to allocate memory for queues\n");
1990 kfree(adapter->rx_ring);
1991 kfree(adapter->tx_ring);
1992 return -ENOMEM;
1996 * e1000_clean - NAPI Rx polling callback
1997 * @napi: struct associated with this polling callback
1998 * @budget: amount of packets driver is allowed to process this poll
2000 static int e1000_clean(struct napi_struct *napi, int budget)
2002 struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
2003 struct e1000_hw *hw = &adapter->hw;
2004 struct net_device *poll_dev = adapter->netdev;
2005 int tx_cleaned = 0, work_done = 0;
2007 adapter = netdev_priv(poll_dev);
2009 if (adapter->msix_entries &&
2010 !(adapter->rx_ring->ims_val & adapter->tx_ring->ims_val))
2011 goto clean_rx;
2014 * e1000_clean is called per-cpu. This lock protects
2015 * tx_ring from being cleaned by multiple cpus
2016 * simultaneously. A failure obtaining the lock means
2017 * tx_ring is currently being cleaned anyway.
2019 if (spin_trylock(&adapter->tx_queue_lock)) {
2020 tx_cleaned = e1000_clean_tx_irq(adapter);
2021 spin_unlock(&adapter->tx_queue_lock);
2024 clean_rx:
2025 adapter->clean_rx(adapter, &work_done, budget);
2027 if (tx_cleaned)
2028 work_done = budget;
2030 /* If budget not fully consumed, exit the polling mode */
2031 if (work_done < budget) {
2032 if (adapter->itr_setting & 3)
2033 e1000_set_itr(adapter);
2034 netif_rx_complete(napi);
2035 if (adapter->msix_entries)
2036 ew32(IMS, adapter->rx_ring->ims_val);
2037 else
2038 e1000_irq_enable(adapter);
2041 return work_done;
2044 static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
2046 struct e1000_adapter *adapter = netdev_priv(netdev);
2047 struct e1000_hw *hw = &adapter->hw;
2048 u32 vfta, index;
2050 /* don't update vlan cookie if already programmed */
2051 if ((adapter->hw.mng_cookie.status &
2052 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
2053 (vid == adapter->mng_vlan_id))
2054 return;
2055 /* add VID to filter table */
2056 index = (vid >> 5) & 0x7F;
2057 vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
2058 vfta |= (1 << (vid & 0x1F));
2059 e1000e_write_vfta(hw, index, vfta);
2062 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
2064 struct e1000_adapter *adapter = netdev_priv(netdev);
2065 struct e1000_hw *hw = &adapter->hw;
2066 u32 vfta, index;
2068 if (!test_bit(__E1000_DOWN, &adapter->state))
2069 e1000_irq_disable(adapter);
2070 vlan_group_set_device(adapter->vlgrp, vid, NULL);
2072 if (!test_bit(__E1000_DOWN, &adapter->state))
2073 e1000_irq_enable(adapter);
2075 if ((adapter->hw.mng_cookie.status &
2076 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
2077 (vid == adapter->mng_vlan_id)) {
2078 /* release control to f/w */
2079 e1000_release_hw_control(adapter);
2080 return;
2083 /* remove VID from filter table */
2084 index = (vid >> 5) & 0x7F;
2085 vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
2086 vfta &= ~(1 << (vid & 0x1F));
2087 e1000e_write_vfta(hw, index, vfta);
2090 static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
2092 struct net_device *netdev = adapter->netdev;
2093 u16 vid = adapter->hw.mng_cookie.vlan_id;
2094 u16 old_vid = adapter->mng_vlan_id;
2096 if (!adapter->vlgrp)
2097 return;
2099 if (!vlan_group_get_device(adapter->vlgrp, vid)) {
2100 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
2101 if (adapter->hw.mng_cookie.status &
2102 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
2103 e1000_vlan_rx_add_vid(netdev, vid);
2104 adapter->mng_vlan_id = vid;
2107 if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
2108 (vid != old_vid) &&
2109 !vlan_group_get_device(adapter->vlgrp, old_vid))
2110 e1000_vlan_rx_kill_vid(netdev, old_vid);
2111 } else {
2112 adapter->mng_vlan_id = vid;
2117 static void e1000_vlan_rx_register(struct net_device *netdev,
2118 struct vlan_group *grp)
2120 struct e1000_adapter *adapter = netdev_priv(netdev);
2121 struct e1000_hw *hw = &adapter->hw;
2122 u32 ctrl, rctl;
2124 if (!test_bit(__E1000_DOWN, &adapter->state))
2125 e1000_irq_disable(adapter);
2126 adapter->vlgrp = grp;
2128 if (grp) {
2129 /* enable VLAN tag insert/strip */
2130 ctrl = er32(CTRL);
2131 ctrl |= E1000_CTRL_VME;
2132 ew32(CTRL, ctrl);
2134 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2135 /* enable VLAN receive filtering */
2136 rctl = er32(RCTL);
2137 rctl &= ~E1000_RCTL_CFIEN;
2138 ew32(RCTL, rctl);
2139 e1000_update_mng_vlan(adapter);
2141 } else {
2142 /* disable VLAN tag insert/strip */
2143 ctrl = er32(CTRL);
2144 ctrl &= ~E1000_CTRL_VME;
2145 ew32(CTRL, ctrl);
2147 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2148 if (adapter->mng_vlan_id !=
2149 (u16)E1000_MNG_VLAN_NONE) {
2150 e1000_vlan_rx_kill_vid(netdev,
2151 adapter->mng_vlan_id);
2152 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
2157 if (!test_bit(__E1000_DOWN, &adapter->state))
2158 e1000_irq_enable(adapter);
2161 static void e1000_restore_vlan(struct e1000_adapter *adapter)
2163 u16 vid;
2165 e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
2167 if (!adapter->vlgrp)
2168 return;
2170 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
2171 if (!vlan_group_get_device(adapter->vlgrp, vid))
2172 continue;
2173 e1000_vlan_rx_add_vid(adapter->netdev, vid);
2177 static void e1000_init_manageability(struct e1000_adapter *adapter)
2179 struct e1000_hw *hw = &adapter->hw;
2180 u32 manc, manc2h;
2182 if (!(adapter->flags & FLAG_MNG_PT_ENABLED))
2183 return;
2185 manc = er32(MANC);
2188 * enable receiving management packets to the host. this will probably
2189 * generate destination unreachable messages from the host OS, but
2190 * the packets will be handled on SMBUS
2192 manc |= E1000_MANC_EN_MNG2HOST;
2193 manc2h = er32(MANC2H);
2194 #define E1000_MNG2HOST_PORT_623 (1 << 5)
2195 #define E1000_MNG2HOST_PORT_664 (1 << 6)
2196 manc2h |= E1000_MNG2HOST_PORT_623;
2197 manc2h |= E1000_MNG2HOST_PORT_664;
2198 ew32(MANC2H, manc2h);
2199 ew32(MANC, manc);
2203 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
2204 * @adapter: board private structure
2206 * Configure the Tx unit of the MAC after a reset.
2208 static void e1000_configure_tx(struct e1000_adapter *adapter)
2210 struct e1000_hw *hw = &adapter->hw;
2211 struct e1000_ring *tx_ring = adapter->tx_ring;
2212 u64 tdba;
2213 u32 tdlen, tctl, tipg, tarc;
2214 u32 ipgr1, ipgr2;
2216 /* Setup the HW Tx Head and Tail descriptor pointers */
2217 tdba = tx_ring->dma;
2218 tdlen = tx_ring->count * sizeof(struct e1000_tx_desc);
2219 ew32(TDBAL, (tdba & DMA_32BIT_MASK));
2220 ew32(TDBAH, (tdba >> 32));
2221 ew32(TDLEN, tdlen);
2222 ew32(TDH, 0);
2223 ew32(TDT, 0);
2224 tx_ring->head = E1000_TDH;
2225 tx_ring->tail = E1000_TDT;
2227 /* Set the default values for the Tx Inter Packet Gap timer */
2228 tipg = DEFAULT_82543_TIPG_IPGT_COPPER; /* 8 */
2229 ipgr1 = DEFAULT_82543_TIPG_IPGR1; /* 8 */
2230 ipgr2 = DEFAULT_82543_TIPG_IPGR2; /* 6 */
2232 if (adapter->flags & FLAG_TIPG_MEDIUM_FOR_80003ESLAN)
2233 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2; /* 7 */
2235 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
2236 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
2237 ew32(TIPG, tipg);
2239 /* Set the Tx Interrupt Delay register */
2240 ew32(TIDV, adapter->tx_int_delay);
2241 /* Tx irq moderation */
2242 ew32(TADV, adapter->tx_abs_int_delay);
2244 /* Program the Transmit Control Register */
2245 tctl = er32(TCTL);
2246 tctl &= ~E1000_TCTL_CT;
2247 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2248 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2250 if (adapter->flags & FLAG_TARC_SPEED_MODE_BIT) {
2251 tarc = er32(TARC(0));
2253 * set the speed mode bit, we'll clear it if we're not at
2254 * gigabit link later
2256 #define SPEED_MODE_BIT (1 << 21)
2257 tarc |= SPEED_MODE_BIT;
2258 ew32(TARC(0), tarc);
2261 /* errata: program both queues to unweighted RR */
2262 if (adapter->flags & FLAG_TARC_SET_BIT_ZERO) {
2263 tarc = er32(TARC(0));
2264 tarc |= 1;
2265 ew32(TARC(0), tarc);
2266 tarc = er32(TARC(1));
2267 tarc |= 1;
2268 ew32(TARC(1), tarc);
2271 e1000e_config_collision_dist(hw);
2273 /* Setup Transmit Descriptor Settings for eop descriptor */
2274 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
2276 /* only set IDE if we are delaying interrupts using the timers */
2277 if (adapter->tx_int_delay)
2278 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
2280 /* enable Report Status bit */
2281 adapter->txd_cmd |= E1000_TXD_CMD_RS;
2283 ew32(TCTL, tctl);
2285 adapter->tx_queue_len = adapter->netdev->tx_queue_len;
2289 * e1000_setup_rctl - configure the receive control registers
2290 * @adapter: Board private structure
2292 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
2293 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
2294 static void e1000_setup_rctl(struct e1000_adapter *adapter)
2296 struct e1000_hw *hw = &adapter->hw;
2297 u32 rctl, rfctl;
2298 u32 psrctl = 0;
2299 u32 pages = 0;
2301 /* Program MC offset vector base */
2302 rctl = er32(RCTL);
2303 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
2304 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
2305 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
2306 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
2308 /* Do not Store bad packets */
2309 rctl &= ~E1000_RCTL_SBP;
2311 /* Enable Long Packet receive */
2312 if (adapter->netdev->mtu <= ETH_DATA_LEN)
2313 rctl &= ~E1000_RCTL_LPE;
2314 else
2315 rctl |= E1000_RCTL_LPE;
2317 /* Some systems expect that the CRC is included in SMBUS traffic. The
2318 * hardware strips the CRC before sending to both SMBUS (BMC) and to
2319 * host memory when this is enabled
2321 if (adapter->flags2 & FLAG2_CRC_STRIPPING)
2322 rctl |= E1000_RCTL_SECRC;
2324 /* Setup buffer sizes */
2325 rctl &= ~E1000_RCTL_SZ_4096;
2326 rctl |= E1000_RCTL_BSEX;
2327 switch (adapter->rx_buffer_len) {
2328 case 256:
2329 rctl |= E1000_RCTL_SZ_256;
2330 rctl &= ~E1000_RCTL_BSEX;
2331 break;
2332 case 512:
2333 rctl |= E1000_RCTL_SZ_512;
2334 rctl &= ~E1000_RCTL_BSEX;
2335 break;
2336 case 1024:
2337 rctl |= E1000_RCTL_SZ_1024;
2338 rctl &= ~E1000_RCTL_BSEX;
2339 break;
2340 case 2048:
2341 default:
2342 rctl |= E1000_RCTL_SZ_2048;
2343 rctl &= ~E1000_RCTL_BSEX;
2344 break;
2345 case 4096:
2346 rctl |= E1000_RCTL_SZ_4096;
2347 break;
2348 case 8192:
2349 rctl |= E1000_RCTL_SZ_8192;
2350 break;
2351 case 16384:
2352 rctl |= E1000_RCTL_SZ_16384;
2353 break;
2357 * 82571 and greater support packet-split where the protocol
2358 * header is placed in skb->data and the packet data is
2359 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
2360 * In the case of a non-split, skb->data is linearly filled,
2361 * followed by the page buffers. Therefore, skb->data is
2362 * sized to hold the largest protocol header.
2364 * allocations using alloc_page take too long for regular MTU
2365 * so only enable packet split for jumbo frames
2367 * Using pages when the page size is greater than 16k wastes
2368 * a lot of memory, since we allocate 3 pages at all times
2369 * per packet.
2371 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
2372 if (!(adapter->flags & FLAG_IS_ICH) && (pages <= 3) &&
2373 (PAGE_SIZE <= 16384) && (rctl & E1000_RCTL_LPE))
2374 adapter->rx_ps_pages = pages;
2375 else
2376 adapter->rx_ps_pages = 0;
2378 if (adapter->rx_ps_pages) {
2379 /* Configure extra packet-split registers */
2380 rfctl = er32(RFCTL);
2381 rfctl |= E1000_RFCTL_EXTEN;
2383 * disable packet split support for IPv6 extension headers,
2384 * because some malformed IPv6 headers can hang the Rx
2386 rfctl |= (E1000_RFCTL_IPV6_EX_DIS |
2387 E1000_RFCTL_NEW_IPV6_EXT_DIS);
2389 ew32(RFCTL, rfctl);
2391 /* Enable Packet split descriptors */
2392 rctl |= E1000_RCTL_DTYP_PS;
2394 psrctl |= adapter->rx_ps_bsize0 >>
2395 E1000_PSRCTL_BSIZE0_SHIFT;
2397 switch (adapter->rx_ps_pages) {
2398 case 3:
2399 psrctl |= PAGE_SIZE <<
2400 E1000_PSRCTL_BSIZE3_SHIFT;
2401 case 2:
2402 psrctl |= PAGE_SIZE <<
2403 E1000_PSRCTL_BSIZE2_SHIFT;
2404 case 1:
2405 psrctl |= PAGE_SIZE >>
2406 E1000_PSRCTL_BSIZE1_SHIFT;
2407 break;
2410 ew32(PSRCTL, psrctl);
2413 ew32(RCTL, rctl);
2414 /* just started the receive unit, no need to restart */
2415 adapter->flags &= ~FLAG_RX_RESTART_NOW;
2419 * e1000_configure_rx - Configure Receive Unit after Reset
2420 * @adapter: board private structure
2422 * Configure the Rx unit of the MAC after a reset.
2424 static void e1000_configure_rx(struct e1000_adapter *adapter)
2426 struct e1000_hw *hw = &adapter->hw;
2427 struct e1000_ring *rx_ring = adapter->rx_ring;
2428 u64 rdba;
2429 u32 rdlen, rctl, rxcsum, ctrl_ext;
2431 if (adapter->rx_ps_pages) {
2432 /* this is a 32 byte descriptor */
2433 rdlen = rx_ring->count *
2434 sizeof(union e1000_rx_desc_packet_split);
2435 adapter->clean_rx = e1000_clean_rx_irq_ps;
2436 adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
2437 } else if (adapter->netdev->mtu > ETH_FRAME_LEN + ETH_FCS_LEN) {
2438 rdlen = rx_ring->count * sizeof(struct e1000_rx_desc);
2439 adapter->clean_rx = e1000_clean_jumbo_rx_irq;
2440 adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
2441 } else {
2442 rdlen = rx_ring->count * sizeof(struct e1000_rx_desc);
2443 adapter->clean_rx = e1000_clean_rx_irq;
2444 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
2447 /* disable receives while setting up the descriptors */
2448 rctl = er32(RCTL);
2449 ew32(RCTL, rctl & ~E1000_RCTL_EN);
2450 e1e_flush();
2451 msleep(10);
2453 /* set the Receive Delay Timer Register */
2454 ew32(RDTR, adapter->rx_int_delay);
2456 /* irq moderation */
2457 ew32(RADV, adapter->rx_abs_int_delay);
2458 if (adapter->itr_setting != 0)
2459 ew32(ITR, 1000000000 / (adapter->itr * 256));
2461 ctrl_ext = er32(CTRL_EXT);
2462 /* Reset delay timers after every interrupt */
2463 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
2464 /* Auto-Mask interrupts upon ICR access */
2465 ctrl_ext |= E1000_CTRL_EXT_IAME;
2466 ew32(IAM, 0xffffffff);
2467 ew32(CTRL_EXT, ctrl_ext);
2468 e1e_flush();
2471 * Setup the HW Rx Head and Tail Descriptor Pointers and
2472 * the Base and Length of the Rx Descriptor Ring
2474 rdba = rx_ring->dma;
2475 ew32(RDBAL, (rdba & DMA_32BIT_MASK));
2476 ew32(RDBAH, (rdba >> 32));
2477 ew32(RDLEN, rdlen);
2478 ew32(RDH, 0);
2479 ew32(RDT, 0);
2480 rx_ring->head = E1000_RDH;
2481 rx_ring->tail = E1000_RDT;
2483 /* Enable Receive Checksum Offload for TCP and UDP */
2484 rxcsum = er32(RXCSUM);
2485 if (adapter->flags & FLAG_RX_CSUM_ENABLED) {
2486 rxcsum |= E1000_RXCSUM_TUOFL;
2489 * IPv4 payload checksum for UDP fragments must be
2490 * used in conjunction with packet-split.
2492 if (adapter->rx_ps_pages)
2493 rxcsum |= E1000_RXCSUM_IPPCSE;
2494 } else {
2495 rxcsum &= ~E1000_RXCSUM_TUOFL;
2496 /* no need to clear IPPCSE as it defaults to 0 */
2498 ew32(RXCSUM, rxcsum);
2501 * Enable early receives on supported devices, only takes effect when
2502 * packet size is equal or larger than the specified value (in 8 byte
2503 * units), e.g. using jumbo frames when setting to E1000_ERT_2048
2505 if ((adapter->flags & FLAG_HAS_ERT) &&
2506 (adapter->netdev->mtu > ETH_DATA_LEN)) {
2507 u32 rxdctl = er32(RXDCTL(0));
2508 ew32(RXDCTL(0), rxdctl | 0x3);
2509 ew32(ERT, E1000_ERT_2048 | (1 << 13));
2511 * With jumbo frames and early-receive enabled, excessive
2512 * C4->C2 latencies result in dropped transactions.
2514 pm_qos_update_requirement(PM_QOS_CPU_DMA_LATENCY,
2515 e1000e_driver_name, 55);
2516 } else {
2517 pm_qos_update_requirement(PM_QOS_CPU_DMA_LATENCY,
2518 e1000e_driver_name,
2519 PM_QOS_DEFAULT_VALUE);
2522 /* Enable Receives */
2523 ew32(RCTL, rctl);
2527 * e1000_update_mc_addr_list - Update Multicast addresses
2528 * @hw: pointer to the HW structure
2529 * @mc_addr_list: array of multicast addresses to program
2530 * @mc_addr_count: number of multicast addresses to program
2531 * @rar_used_count: the first RAR register free to program
2532 * @rar_count: total number of supported Receive Address Registers
2534 * Updates the Receive Address Registers and Multicast Table Array.
2535 * The caller must have a packed mc_addr_list of multicast addresses.
2536 * The parameter rar_count will usually be hw->mac.rar_entry_count
2537 * unless there are workarounds that change this. Currently no func pointer
2538 * exists and all implementations are handled in the generic version of this
2539 * function.
2541 static void e1000_update_mc_addr_list(struct e1000_hw *hw, u8 *mc_addr_list,
2542 u32 mc_addr_count, u32 rar_used_count,
2543 u32 rar_count)
2545 hw->mac.ops.update_mc_addr_list(hw, mc_addr_list, mc_addr_count,
2546 rar_used_count, rar_count);
2550 * e1000_set_multi - Multicast and Promiscuous mode set
2551 * @netdev: network interface device structure
2553 * The set_multi entry point is called whenever the multicast address
2554 * list or the network interface flags are updated. This routine is
2555 * responsible for configuring the hardware for proper multicast,
2556 * promiscuous mode, and all-multi behavior.
2558 static void e1000_set_multi(struct net_device *netdev)
2560 struct e1000_adapter *adapter = netdev_priv(netdev);
2561 struct e1000_hw *hw = &adapter->hw;
2562 struct e1000_mac_info *mac = &hw->mac;
2563 struct dev_mc_list *mc_ptr;
2564 u8 *mta_list;
2565 u32 rctl;
2566 int i;
2568 /* Check for Promiscuous and All Multicast modes */
2570 rctl = er32(RCTL);
2572 if (netdev->flags & IFF_PROMISC) {
2573 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2574 rctl &= ~E1000_RCTL_VFE;
2575 } else {
2576 if (netdev->flags & IFF_ALLMULTI) {
2577 rctl |= E1000_RCTL_MPE;
2578 rctl &= ~E1000_RCTL_UPE;
2579 } else {
2580 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
2582 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER)
2583 rctl |= E1000_RCTL_VFE;
2586 ew32(RCTL, rctl);
2588 if (netdev->mc_count) {
2589 mta_list = kmalloc(netdev->mc_count * 6, GFP_ATOMIC);
2590 if (!mta_list)
2591 return;
2593 /* prepare a packed array of only addresses. */
2594 mc_ptr = netdev->mc_list;
2596 for (i = 0; i < netdev->mc_count; i++) {
2597 if (!mc_ptr)
2598 break;
2599 memcpy(mta_list + (i*ETH_ALEN), mc_ptr->dmi_addr,
2600 ETH_ALEN);
2601 mc_ptr = mc_ptr->next;
2604 e1000_update_mc_addr_list(hw, mta_list, i, 1,
2605 mac->rar_entry_count);
2606 kfree(mta_list);
2607 } else {
2609 * if we're called from probe, we might not have
2610 * anything to do here, so clear out the list
2612 e1000_update_mc_addr_list(hw, NULL, 0, 1, mac->rar_entry_count);
2617 * e1000_configure - configure the hardware for Rx and Tx
2618 * @adapter: private board structure
2620 static void e1000_configure(struct e1000_adapter *adapter)
2622 e1000_set_multi(adapter->netdev);
2624 e1000_restore_vlan(adapter);
2625 e1000_init_manageability(adapter);
2627 e1000_configure_tx(adapter);
2628 e1000_setup_rctl(adapter);
2629 e1000_configure_rx(adapter);
2630 adapter->alloc_rx_buf(adapter, e1000_desc_unused(adapter->rx_ring));
2634 * e1000e_power_up_phy - restore link in case the phy was powered down
2635 * @adapter: address of board private structure
2637 * The phy may be powered down to save power and turn off link when the
2638 * driver is unloaded and wake on lan is not enabled (among others)
2639 * *** this routine MUST be followed by a call to e1000e_reset ***
2641 void e1000e_power_up_phy(struct e1000_adapter *adapter)
2643 u16 mii_reg = 0;
2645 /* Just clear the power down bit to wake the phy back up */
2646 if (adapter->hw.phy.media_type == e1000_media_type_copper) {
2648 * According to the manual, the phy will retain its
2649 * settings across a power-down/up cycle
2651 e1e_rphy(&adapter->hw, PHY_CONTROL, &mii_reg);
2652 mii_reg &= ~MII_CR_POWER_DOWN;
2653 e1e_wphy(&adapter->hw, PHY_CONTROL, mii_reg);
2656 adapter->hw.mac.ops.setup_link(&adapter->hw);
2660 * e1000_power_down_phy - Power down the PHY
2662 * Power down the PHY so no link is implied when interface is down
2663 * The PHY cannot be powered down is management or WoL is active
2665 static void e1000_power_down_phy(struct e1000_adapter *adapter)
2667 struct e1000_hw *hw = &adapter->hw;
2668 u16 mii_reg;
2670 /* WoL is enabled */
2671 if (adapter->wol)
2672 return;
2674 /* non-copper PHY? */
2675 if (adapter->hw.phy.media_type != e1000_media_type_copper)
2676 return;
2678 /* reset is blocked because of a SoL/IDER session */
2679 if (e1000e_check_mng_mode(hw) || e1000_check_reset_block(hw))
2680 return;
2682 /* manageability (AMT) is enabled */
2683 if (er32(MANC) & E1000_MANC_SMBUS_EN)
2684 return;
2686 /* power down the PHY */
2687 e1e_rphy(hw, PHY_CONTROL, &mii_reg);
2688 mii_reg |= MII_CR_POWER_DOWN;
2689 e1e_wphy(hw, PHY_CONTROL, mii_reg);
2690 mdelay(1);
2694 * e1000e_reset - bring the hardware into a known good state
2696 * This function boots the hardware and enables some settings that
2697 * require a configuration cycle of the hardware - those cannot be
2698 * set/changed during runtime. After reset the device needs to be
2699 * properly configured for Rx, Tx etc.
2701 void e1000e_reset(struct e1000_adapter *adapter)
2703 struct e1000_mac_info *mac = &adapter->hw.mac;
2704 struct e1000_fc_info *fc = &adapter->hw.fc;
2705 struct e1000_hw *hw = &adapter->hw;
2706 u32 tx_space, min_tx_space, min_rx_space;
2707 u32 pba = adapter->pba;
2708 u16 hwm;
2710 /* reset Packet Buffer Allocation to default */
2711 ew32(PBA, pba);
2713 if (adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) {
2715 * To maintain wire speed transmits, the Tx FIFO should be
2716 * large enough to accommodate two full transmit packets,
2717 * rounded up to the next 1KB and expressed in KB. Likewise,
2718 * the Rx FIFO should be large enough to accommodate at least
2719 * one full receive packet and is similarly rounded up and
2720 * expressed in KB.
2722 pba = er32(PBA);
2723 /* upper 16 bits has Tx packet buffer allocation size in KB */
2724 tx_space = pba >> 16;
2725 /* lower 16 bits has Rx packet buffer allocation size in KB */
2726 pba &= 0xffff;
2728 * the Tx fifo also stores 16 bytes of information about the tx
2729 * but don't include ethernet FCS because hardware appends it
2731 min_tx_space = (adapter->max_frame_size +
2732 sizeof(struct e1000_tx_desc) -
2733 ETH_FCS_LEN) * 2;
2734 min_tx_space = ALIGN(min_tx_space, 1024);
2735 min_tx_space >>= 10;
2736 /* software strips receive CRC, so leave room for it */
2737 min_rx_space = adapter->max_frame_size;
2738 min_rx_space = ALIGN(min_rx_space, 1024);
2739 min_rx_space >>= 10;
2742 * If current Tx allocation is less than the min Tx FIFO size,
2743 * and the min Tx FIFO size is less than the current Rx FIFO
2744 * allocation, take space away from current Rx allocation
2746 if ((tx_space < min_tx_space) &&
2747 ((min_tx_space - tx_space) < pba)) {
2748 pba -= min_tx_space - tx_space;
2751 * if short on Rx space, Rx wins and must trump tx
2752 * adjustment or use Early Receive if available
2754 if ((pba < min_rx_space) &&
2755 (!(adapter->flags & FLAG_HAS_ERT)))
2756 /* ERT enabled in e1000_configure_rx */
2757 pba = min_rx_space;
2760 ew32(PBA, pba);
2765 * flow control settings
2767 * The high water mark must be low enough to fit one full frame
2768 * (or the size used for early receive) above it in the Rx FIFO.
2769 * Set it to the lower of:
2770 * - 90% of the Rx FIFO size, and
2771 * - the full Rx FIFO size minus the early receive size (for parts
2772 * with ERT support assuming ERT set to E1000_ERT_2048), or
2773 * - the full Rx FIFO size minus one full frame
2775 if (adapter->flags & FLAG_HAS_ERT)
2776 hwm = min(((pba << 10) * 9 / 10),
2777 ((pba << 10) - (E1000_ERT_2048 << 3)));
2778 else
2779 hwm = min(((pba << 10) * 9 / 10),
2780 ((pba << 10) - adapter->max_frame_size));
2782 fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
2783 fc->low_water = fc->high_water - 8;
2785 if (adapter->flags & FLAG_DISABLE_FC_PAUSE_TIME)
2786 fc->pause_time = 0xFFFF;
2787 else
2788 fc->pause_time = E1000_FC_PAUSE_TIME;
2789 fc->send_xon = 1;
2790 fc->current_mode = fc->requested_mode;
2792 /* Allow time for pending master requests to run */
2793 mac->ops.reset_hw(hw);
2796 * For parts with AMT enabled, let the firmware know
2797 * that the network interface is in control
2799 if (adapter->flags & FLAG_HAS_AMT)
2800 e1000_get_hw_control(adapter);
2802 ew32(WUC, 0);
2804 if (mac->ops.init_hw(hw))
2805 e_err("Hardware Error\n");
2807 e1000_update_mng_vlan(adapter);
2809 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
2810 ew32(VET, ETH_P_8021Q);
2812 e1000e_reset_adaptive(hw);
2813 e1000_get_phy_info(hw);
2815 if (!(adapter->flags & FLAG_SMART_POWER_DOWN)) {
2816 u16 phy_data = 0;
2818 * speed up time to link by disabling smart power down, ignore
2819 * the return value of this function because there is nothing
2820 * different we would do if it failed
2822 e1e_rphy(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
2823 phy_data &= ~IGP02E1000_PM_SPD;
2824 e1e_wphy(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
2828 int e1000e_up(struct e1000_adapter *adapter)
2830 struct e1000_hw *hw = &adapter->hw;
2832 /* hardware has been reset, we need to reload some things */
2833 e1000_configure(adapter);
2835 clear_bit(__E1000_DOWN, &adapter->state);
2837 napi_enable(&adapter->napi);
2838 if (adapter->msix_entries)
2839 e1000_configure_msix(adapter);
2840 e1000_irq_enable(adapter);
2842 /* fire a link change interrupt to start the watchdog */
2843 ew32(ICS, E1000_ICS_LSC);
2844 return 0;
2847 void e1000e_down(struct e1000_adapter *adapter)
2849 struct net_device *netdev = adapter->netdev;
2850 struct e1000_hw *hw = &adapter->hw;
2851 u32 tctl, rctl;
2854 * signal that we're down so the interrupt handler does not
2855 * reschedule our watchdog timer
2857 set_bit(__E1000_DOWN, &adapter->state);
2859 /* disable receives in the hardware */
2860 rctl = er32(RCTL);
2861 ew32(RCTL, rctl & ~E1000_RCTL_EN);
2862 /* flush and sleep below */
2864 netif_tx_stop_all_queues(netdev);
2866 /* disable transmits in the hardware */
2867 tctl = er32(TCTL);
2868 tctl &= ~E1000_TCTL_EN;
2869 ew32(TCTL, tctl);
2870 /* flush both disables and wait for them to finish */
2871 e1e_flush();
2872 msleep(10);
2874 napi_disable(&adapter->napi);
2875 e1000_irq_disable(adapter);
2877 del_timer_sync(&adapter->watchdog_timer);
2878 del_timer_sync(&adapter->phy_info_timer);
2880 netdev->tx_queue_len = adapter->tx_queue_len;
2881 netif_carrier_off(netdev);
2882 adapter->link_speed = 0;
2883 adapter->link_duplex = 0;
2885 if (!pci_channel_offline(adapter->pdev))
2886 e1000e_reset(adapter);
2887 e1000_clean_tx_ring(adapter);
2888 e1000_clean_rx_ring(adapter);
2891 * TODO: for power management, we could drop the link and
2892 * pci_disable_device here.
2896 void e1000e_reinit_locked(struct e1000_adapter *adapter)
2898 might_sleep();
2899 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
2900 msleep(1);
2901 e1000e_down(adapter);
2902 e1000e_up(adapter);
2903 clear_bit(__E1000_RESETTING, &adapter->state);
2907 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
2908 * @adapter: board private structure to initialize
2910 * e1000_sw_init initializes the Adapter private data structure.
2911 * Fields are initialized based on PCI device information and
2912 * OS network device settings (MTU size).
2914 static int __devinit e1000_sw_init(struct e1000_adapter *adapter)
2916 struct net_device *netdev = adapter->netdev;
2918 adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
2919 adapter->rx_ps_bsize0 = 128;
2920 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
2921 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2923 e1000e_set_interrupt_capability(adapter);
2925 if (e1000_alloc_queues(adapter))
2926 return -ENOMEM;
2928 spin_lock_init(&adapter->tx_queue_lock);
2930 /* Explicitly disable IRQ since the NIC can be in any state. */
2931 e1000_irq_disable(adapter);
2933 set_bit(__E1000_DOWN, &adapter->state);
2934 return 0;
2938 * e1000_intr_msi_test - Interrupt Handler
2939 * @irq: interrupt number
2940 * @data: pointer to a network interface device structure
2942 static irqreturn_t e1000_intr_msi_test(int irq, void *data)
2944 struct net_device *netdev = data;
2945 struct e1000_adapter *adapter = netdev_priv(netdev);
2946 struct e1000_hw *hw = &adapter->hw;
2947 u32 icr = er32(ICR);
2949 e_dbg("%s: icr is %08X\n", netdev->name, icr);
2950 if (icr & E1000_ICR_RXSEQ) {
2951 adapter->flags &= ~FLAG_MSI_TEST_FAILED;
2952 wmb();
2955 return IRQ_HANDLED;
2959 * e1000_test_msi_interrupt - Returns 0 for successful test
2960 * @adapter: board private struct
2962 * code flow taken from tg3.c
2964 static int e1000_test_msi_interrupt(struct e1000_adapter *adapter)
2966 struct net_device *netdev = adapter->netdev;
2967 struct e1000_hw *hw = &adapter->hw;
2968 int err;
2970 /* poll_enable hasn't been called yet, so don't need disable */
2971 /* clear any pending events */
2972 er32(ICR);
2974 /* free the real vector and request a test handler */
2975 e1000_free_irq(adapter);
2976 e1000e_reset_interrupt_capability(adapter);
2978 /* Assume that the test fails, if it succeeds then the test
2979 * MSI irq handler will unset this flag */
2980 adapter->flags |= FLAG_MSI_TEST_FAILED;
2982 err = pci_enable_msi(adapter->pdev);
2983 if (err)
2984 goto msi_test_failed;
2986 err = request_irq(adapter->pdev->irq, &e1000_intr_msi_test, 0,
2987 netdev->name, netdev);
2988 if (err) {
2989 pci_disable_msi(adapter->pdev);
2990 goto msi_test_failed;
2993 wmb();
2995 e1000_irq_enable(adapter);
2997 /* fire an unusual interrupt on the test handler */
2998 ew32(ICS, E1000_ICS_RXSEQ);
2999 e1e_flush();
3000 msleep(50);
3002 e1000_irq_disable(adapter);
3004 rmb();
3006 if (adapter->flags & FLAG_MSI_TEST_FAILED) {
3007 adapter->int_mode = E1000E_INT_MODE_LEGACY;
3008 err = -EIO;
3009 e_info("MSI interrupt test failed!\n");
3012 free_irq(adapter->pdev->irq, netdev);
3013 pci_disable_msi(adapter->pdev);
3015 if (err == -EIO)
3016 goto msi_test_failed;
3018 /* okay so the test worked, restore settings */
3019 e_dbg("%s: MSI interrupt test succeeded!\n", netdev->name);
3020 msi_test_failed:
3021 e1000e_set_interrupt_capability(adapter);
3022 e1000_request_irq(adapter);
3023 return err;
3027 * e1000_test_msi - Returns 0 if MSI test succeeds or INTx mode is restored
3028 * @adapter: board private struct
3030 * code flow taken from tg3.c, called with e1000 interrupts disabled.
3032 static int e1000_test_msi(struct e1000_adapter *adapter)
3034 int err;
3035 u16 pci_cmd;
3037 if (!(adapter->flags & FLAG_MSI_ENABLED))
3038 return 0;
3040 /* disable SERR in case the MSI write causes a master abort */
3041 pci_read_config_word(adapter->pdev, PCI_COMMAND, &pci_cmd);
3042 pci_write_config_word(adapter->pdev, PCI_COMMAND,
3043 pci_cmd & ~PCI_COMMAND_SERR);
3045 err = e1000_test_msi_interrupt(adapter);
3047 /* restore previous setting of command word */
3048 pci_write_config_word(adapter->pdev, PCI_COMMAND, pci_cmd);
3050 /* success ! */
3051 if (!err)
3052 return 0;
3054 /* EIO means MSI test failed */
3055 if (err != -EIO)
3056 return err;
3058 /* back to INTx mode */
3059 e_warn("MSI interrupt test failed, using legacy interrupt.\n");
3061 e1000_free_irq(adapter);
3063 err = e1000_request_irq(adapter);
3065 return err;
3069 * e1000_open - Called when a network interface is made active
3070 * @netdev: network interface device structure
3072 * Returns 0 on success, negative value on failure
3074 * The open entry point is called when a network interface is made
3075 * active by the system (IFF_UP). At this point all resources needed
3076 * for transmit and receive operations are allocated, the interrupt
3077 * handler is registered with the OS, the watchdog timer is started,
3078 * and the stack is notified that the interface is ready.
3080 static int e1000_open(struct net_device *netdev)
3082 struct e1000_adapter *adapter = netdev_priv(netdev);
3083 struct e1000_hw *hw = &adapter->hw;
3084 int err;
3086 /* disallow open during test */
3087 if (test_bit(__E1000_TESTING, &adapter->state))
3088 return -EBUSY;
3090 /* allocate transmit descriptors */
3091 err = e1000e_setup_tx_resources(adapter);
3092 if (err)
3093 goto err_setup_tx;
3095 /* allocate receive descriptors */
3096 err = e1000e_setup_rx_resources(adapter);
3097 if (err)
3098 goto err_setup_rx;
3100 e1000e_power_up_phy(adapter);
3102 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
3103 if ((adapter->hw.mng_cookie.status &
3104 E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
3105 e1000_update_mng_vlan(adapter);
3108 * If AMT is enabled, let the firmware know that the network
3109 * interface is now open
3111 if (adapter->flags & FLAG_HAS_AMT)
3112 e1000_get_hw_control(adapter);
3115 * before we allocate an interrupt, we must be ready to handle it.
3116 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
3117 * as soon as we call pci_request_irq, so we have to setup our
3118 * clean_rx handler before we do so.
3120 e1000_configure(adapter);
3122 err = e1000_request_irq(adapter);
3123 if (err)
3124 goto err_req_irq;
3127 * Work around PCIe errata with MSI interrupts causing some chipsets to
3128 * ignore e1000e MSI messages, which means we need to test our MSI
3129 * interrupt now
3131 if (adapter->int_mode != E1000E_INT_MODE_LEGACY) {
3132 err = e1000_test_msi(adapter);
3133 if (err) {
3134 e_err("Interrupt allocation failed\n");
3135 goto err_req_irq;
3139 /* From here on the code is the same as e1000e_up() */
3140 clear_bit(__E1000_DOWN, &adapter->state);
3142 napi_enable(&adapter->napi);
3144 e1000_irq_enable(adapter);
3146 netif_tx_start_all_queues(netdev);
3148 /* fire a link status change interrupt to start the watchdog */
3149 ew32(ICS, E1000_ICS_LSC);
3151 return 0;
3153 err_req_irq:
3154 e1000_release_hw_control(adapter);
3155 e1000_power_down_phy(adapter);
3156 e1000e_free_rx_resources(adapter);
3157 err_setup_rx:
3158 e1000e_free_tx_resources(adapter);
3159 err_setup_tx:
3160 e1000e_reset(adapter);
3162 return err;
3166 * e1000_close - Disables a network interface
3167 * @netdev: network interface device structure
3169 * Returns 0, this is not allowed to fail
3171 * The close entry point is called when an interface is de-activated
3172 * by the OS. The hardware is still under the drivers control, but
3173 * needs to be disabled. A global MAC reset is issued to stop the
3174 * hardware, and all transmit and receive resources are freed.
3176 static int e1000_close(struct net_device *netdev)
3178 struct e1000_adapter *adapter = netdev_priv(netdev);
3180 WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
3181 e1000e_down(adapter);
3182 e1000_power_down_phy(adapter);
3183 e1000_free_irq(adapter);
3185 e1000e_free_tx_resources(adapter);
3186 e1000e_free_rx_resources(adapter);
3189 * kill manageability vlan ID if supported, but not if a vlan with
3190 * the same ID is registered on the host OS (let 8021q kill it)
3192 if ((adapter->hw.mng_cookie.status &
3193 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
3194 !(adapter->vlgrp &&
3195 vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id)))
3196 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
3199 * If AMT is enabled, let the firmware know that the network
3200 * interface is now closed
3202 if (adapter->flags & FLAG_HAS_AMT)
3203 e1000_release_hw_control(adapter);
3205 return 0;
3208 * e1000_set_mac - Change the Ethernet Address of the NIC
3209 * @netdev: network interface device structure
3210 * @p: pointer to an address structure
3212 * Returns 0 on success, negative on failure
3214 static int e1000_set_mac(struct net_device *netdev, void *p)
3216 struct e1000_adapter *adapter = netdev_priv(netdev);
3217 struct sockaddr *addr = p;
3219 if (!is_valid_ether_addr(addr->sa_data))
3220 return -EADDRNOTAVAIL;
3222 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3223 memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
3225 e1000e_rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
3227 if (adapter->flags & FLAG_RESET_OVERWRITES_LAA) {
3228 /* activate the work around */
3229 e1000e_set_laa_state_82571(&adapter->hw, 1);
3232 * Hold a copy of the LAA in RAR[14] This is done so that
3233 * between the time RAR[0] gets clobbered and the time it
3234 * gets fixed (in e1000_watchdog), the actual LAA is in one
3235 * of the RARs and no incoming packets directed to this port
3236 * are dropped. Eventually the LAA will be in RAR[0] and
3237 * RAR[14]
3239 e1000e_rar_set(&adapter->hw,
3240 adapter->hw.mac.addr,
3241 adapter->hw.mac.rar_entry_count - 1);
3244 return 0;
3248 * e1000e_update_phy_task - work thread to update phy
3249 * @work: pointer to our work struct
3251 * this worker thread exists because we must acquire a
3252 * semaphore to read the phy, which we could msleep while
3253 * waiting for it, and we can't msleep in a timer.
3255 static void e1000e_update_phy_task(struct work_struct *work)
3257 struct e1000_adapter *adapter = container_of(work,
3258 struct e1000_adapter, update_phy_task);
3259 e1000_get_phy_info(&adapter->hw);
3263 * Need to wait a few seconds after link up to get diagnostic information from
3264 * the phy
3266 static void e1000_update_phy_info(unsigned long data)
3268 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
3269 schedule_work(&adapter->update_phy_task);
3273 * e1000e_update_stats - Update the board statistics counters
3274 * @adapter: board private structure
3276 void e1000e_update_stats(struct e1000_adapter *adapter)
3278 struct e1000_hw *hw = &adapter->hw;
3279 struct pci_dev *pdev = adapter->pdev;
3282 * Prevent stats update while adapter is being reset, or if the pci
3283 * connection is down.
3285 if (adapter->link_speed == 0)
3286 return;
3287 if (pci_channel_offline(pdev))
3288 return;
3290 adapter->stats.crcerrs += er32(CRCERRS);
3291 adapter->stats.gprc += er32(GPRC);
3292 adapter->stats.gorc += er32(GORCL);
3293 er32(GORCH); /* Clear gorc */
3294 adapter->stats.bprc += er32(BPRC);
3295 adapter->stats.mprc += er32(MPRC);
3296 adapter->stats.roc += er32(ROC);
3298 adapter->stats.mpc += er32(MPC);
3299 adapter->stats.scc += er32(SCC);
3300 adapter->stats.ecol += er32(ECOL);
3301 adapter->stats.mcc += er32(MCC);
3302 adapter->stats.latecol += er32(LATECOL);
3303 adapter->stats.dc += er32(DC);
3304 adapter->stats.xonrxc += er32(XONRXC);
3305 adapter->stats.xontxc += er32(XONTXC);
3306 adapter->stats.xoffrxc += er32(XOFFRXC);
3307 adapter->stats.xofftxc += er32(XOFFTXC);
3308 adapter->stats.gptc += er32(GPTC);
3309 adapter->stats.gotc += er32(GOTCL);
3310 er32(GOTCH); /* Clear gotc */
3311 adapter->stats.rnbc += er32(RNBC);
3312 adapter->stats.ruc += er32(RUC);
3314 adapter->stats.mptc += er32(MPTC);
3315 adapter->stats.bptc += er32(BPTC);
3317 /* used for adaptive IFS */
3319 hw->mac.tx_packet_delta = er32(TPT);
3320 adapter->stats.tpt += hw->mac.tx_packet_delta;
3321 hw->mac.collision_delta = er32(COLC);
3322 adapter->stats.colc += hw->mac.collision_delta;
3324 adapter->stats.algnerrc += er32(ALGNERRC);
3325 adapter->stats.rxerrc += er32(RXERRC);
3326 if (hw->mac.type != e1000_82574)
3327 adapter->stats.tncrs += er32(TNCRS);
3328 adapter->stats.cexterr += er32(CEXTERR);
3329 adapter->stats.tsctc += er32(TSCTC);
3330 adapter->stats.tsctfc += er32(TSCTFC);
3332 /* Fill out the OS statistics structure */
3333 adapter->net_stats.multicast = adapter->stats.mprc;
3334 adapter->net_stats.collisions = adapter->stats.colc;
3336 /* Rx Errors */
3339 * RLEC on some newer hardware can be incorrect so build
3340 * our own version based on RUC and ROC
3342 adapter->net_stats.rx_errors = adapter->stats.rxerrc +
3343 adapter->stats.crcerrs + adapter->stats.algnerrc +
3344 adapter->stats.ruc + adapter->stats.roc +
3345 adapter->stats.cexterr;
3346 adapter->net_stats.rx_length_errors = adapter->stats.ruc +
3347 adapter->stats.roc;
3348 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3349 adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
3350 adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
3352 /* Tx Errors */
3353 adapter->net_stats.tx_errors = adapter->stats.ecol +
3354 adapter->stats.latecol;
3355 adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
3356 adapter->net_stats.tx_window_errors = adapter->stats.latecol;
3357 adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
3359 /* Tx Dropped needs to be maintained elsewhere */
3361 /* Management Stats */
3362 adapter->stats.mgptc += er32(MGTPTC);
3363 adapter->stats.mgprc += er32(MGTPRC);
3364 adapter->stats.mgpdc += er32(MGTPDC);
3368 * e1000_phy_read_status - Update the PHY register status snapshot
3369 * @adapter: board private structure
3371 static void e1000_phy_read_status(struct e1000_adapter *adapter)
3373 struct e1000_hw *hw = &adapter->hw;
3374 struct e1000_phy_regs *phy = &adapter->phy_regs;
3375 int ret_val;
3377 if ((er32(STATUS) & E1000_STATUS_LU) &&
3378 (adapter->hw.phy.media_type == e1000_media_type_copper)) {
3379 ret_val = e1e_rphy(hw, PHY_CONTROL, &phy->bmcr);
3380 ret_val |= e1e_rphy(hw, PHY_STATUS, &phy->bmsr);
3381 ret_val |= e1e_rphy(hw, PHY_AUTONEG_ADV, &phy->advertise);
3382 ret_val |= e1e_rphy(hw, PHY_LP_ABILITY, &phy->lpa);
3383 ret_val |= e1e_rphy(hw, PHY_AUTONEG_EXP, &phy->expansion);
3384 ret_val |= e1e_rphy(hw, PHY_1000T_CTRL, &phy->ctrl1000);
3385 ret_val |= e1e_rphy(hw, PHY_1000T_STATUS, &phy->stat1000);
3386 ret_val |= e1e_rphy(hw, PHY_EXT_STATUS, &phy->estatus);
3387 if (ret_val)
3388 e_warn("Error reading PHY register\n");
3389 } else {
3391 * Do not read PHY registers if link is not up
3392 * Set values to typical power-on defaults
3394 phy->bmcr = (BMCR_SPEED1000 | BMCR_ANENABLE | BMCR_FULLDPLX);
3395 phy->bmsr = (BMSR_100FULL | BMSR_100HALF | BMSR_10FULL |
3396 BMSR_10HALF | BMSR_ESTATEN | BMSR_ANEGCAPABLE |
3397 BMSR_ERCAP);
3398 phy->advertise = (ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP |
3399 ADVERTISE_ALL | ADVERTISE_CSMA);
3400 phy->lpa = 0;
3401 phy->expansion = EXPANSION_ENABLENPAGE;
3402 phy->ctrl1000 = ADVERTISE_1000FULL;
3403 phy->stat1000 = 0;
3404 phy->estatus = (ESTATUS_1000_TFULL | ESTATUS_1000_THALF);
3408 static void e1000_print_link_info(struct e1000_adapter *adapter)
3410 struct e1000_hw *hw = &adapter->hw;
3411 u32 ctrl = er32(CTRL);
3413 /* Link status message must follow this format for user tools */
3414 printk(KERN_INFO "e1000e: %s NIC Link is Up %d Mbps %s, "
3415 "Flow Control: %s\n",
3416 adapter->netdev->name,
3417 adapter->link_speed,
3418 (adapter->link_duplex == FULL_DUPLEX) ?
3419 "Full Duplex" : "Half Duplex",
3420 ((ctrl & E1000_CTRL_TFCE) && (ctrl & E1000_CTRL_RFCE)) ?
3421 "RX/TX" :
3422 ((ctrl & E1000_CTRL_RFCE) ? "RX" :
3423 ((ctrl & E1000_CTRL_TFCE) ? "TX" : "None" )));
3426 bool e1000_has_link(struct e1000_adapter *adapter)
3428 struct e1000_hw *hw = &adapter->hw;
3429 bool link_active = 0;
3430 s32 ret_val = 0;
3433 * get_link_status is set on LSC (link status) interrupt or
3434 * Rx sequence error interrupt. get_link_status will stay
3435 * false until the check_for_link establishes link
3436 * for copper adapters ONLY
3438 switch (hw->phy.media_type) {
3439 case e1000_media_type_copper:
3440 if (hw->mac.get_link_status) {
3441 ret_val = hw->mac.ops.check_for_link(hw);
3442 link_active = !hw->mac.get_link_status;
3443 } else {
3444 link_active = 1;
3446 break;
3447 case e1000_media_type_fiber:
3448 ret_val = hw->mac.ops.check_for_link(hw);
3449 link_active = !!(er32(STATUS) & E1000_STATUS_LU);
3450 break;
3451 case e1000_media_type_internal_serdes:
3452 ret_val = hw->mac.ops.check_for_link(hw);
3453 link_active = adapter->hw.mac.serdes_has_link;
3454 break;
3455 default:
3456 case e1000_media_type_unknown:
3457 break;
3460 if ((ret_val == E1000_ERR_PHY) && (hw->phy.type == e1000_phy_igp_3) &&
3461 (er32(CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
3462 /* See e1000_kmrn_lock_loss_workaround_ich8lan() */
3463 e_info("Gigabit has been disabled, downgrading speed\n");
3466 return link_active;
3469 static void e1000e_enable_receives(struct e1000_adapter *adapter)
3471 /* make sure the receive unit is started */
3472 if ((adapter->flags & FLAG_RX_NEEDS_RESTART) &&
3473 (adapter->flags & FLAG_RX_RESTART_NOW)) {
3474 struct e1000_hw *hw = &adapter->hw;
3475 u32 rctl = er32(RCTL);
3476 ew32(RCTL, rctl | E1000_RCTL_EN);
3477 adapter->flags &= ~FLAG_RX_RESTART_NOW;
3482 * e1000_watchdog - Timer Call-back
3483 * @data: pointer to adapter cast into an unsigned long
3485 static void e1000_watchdog(unsigned long data)
3487 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
3489 /* Do the rest outside of interrupt context */
3490 schedule_work(&adapter->watchdog_task);
3492 /* TODO: make this use queue_delayed_work() */
3495 static void e1000_watchdog_task(struct work_struct *work)
3497 struct e1000_adapter *adapter = container_of(work,
3498 struct e1000_adapter, watchdog_task);
3499 struct net_device *netdev = adapter->netdev;
3500 struct e1000_mac_info *mac = &adapter->hw.mac;
3501 struct e1000_phy_info *phy = &adapter->hw.phy;
3502 struct e1000_ring *tx_ring = adapter->tx_ring;
3503 struct e1000_hw *hw = &adapter->hw;
3504 u32 link, tctl;
3505 int tx_pending = 0;
3507 link = e1000_has_link(adapter);
3508 if ((netif_carrier_ok(netdev)) && link) {
3509 e1000e_enable_receives(adapter);
3510 goto link_up;
3513 if ((e1000e_enable_tx_pkt_filtering(hw)) &&
3514 (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id))
3515 e1000_update_mng_vlan(adapter);
3517 if (link) {
3518 if (!netif_carrier_ok(netdev)) {
3519 bool txb2b = 1;
3520 /* update snapshot of PHY registers on LSC */
3521 e1000_phy_read_status(adapter);
3522 mac->ops.get_link_up_info(&adapter->hw,
3523 &adapter->link_speed,
3524 &adapter->link_duplex);
3525 e1000_print_link_info(adapter);
3527 * On supported PHYs, check for duplex mismatch only
3528 * if link has autonegotiated at 10/100 half
3530 if ((hw->phy.type == e1000_phy_igp_3 ||
3531 hw->phy.type == e1000_phy_bm) &&
3532 (hw->mac.autoneg == true) &&
3533 (adapter->link_speed == SPEED_10 ||
3534 adapter->link_speed == SPEED_100) &&
3535 (adapter->link_duplex == HALF_DUPLEX)) {
3536 u16 autoneg_exp;
3538 e1e_rphy(hw, PHY_AUTONEG_EXP, &autoneg_exp);
3540 if (!(autoneg_exp & NWAY_ER_LP_NWAY_CAPS))
3541 e_info("Autonegotiated half duplex but"
3542 " link partner cannot autoneg. "
3543 " Try forcing full duplex if "
3544 "link gets many collisions.\n");
3548 * tweak tx_queue_len according to speed/duplex
3549 * and adjust the timeout factor
3551 netdev->tx_queue_len = adapter->tx_queue_len;
3552 adapter->tx_timeout_factor = 1;
3553 switch (adapter->link_speed) {
3554 case SPEED_10:
3555 txb2b = 0;
3556 netdev->tx_queue_len = 10;
3557 adapter->tx_timeout_factor = 16;
3558 break;
3559 case SPEED_100:
3560 txb2b = 0;
3561 netdev->tx_queue_len = 100;
3562 /* maybe add some timeout factor ? */
3563 break;
3567 * workaround: re-program speed mode bit after
3568 * link-up event
3570 if ((adapter->flags & FLAG_TARC_SPEED_MODE_BIT) &&
3571 !txb2b) {
3572 u32 tarc0;
3573 tarc0 = er32(TARC(0));
3574 tarc0 &= ~SPEED_MODE_BIT;
3575 ew32(TARC(0), tarc0);
3579 * disable TSO for pcie and 10/100 speeds, to avoid
3580 * some hardware issues
3582 if (!(adapter->flags & FLAG_TSO_FORCE)) {
3583 switch (adapter->link_speed) {
3584 case SPEED_10:
3585 case SPEED_100:
3586 e_info("10/100 speed: disabling TSO\n");
3587 netdev->features &= ~NETIF_F_TSO;
3588 netdev->features &= ~NETIF_F_TSO6;
3589 break;
3590 case SPEED_1000:
3591 netdev->features |= NETIF_F_TSO;
3592 netdev->features |= NETIF_F_TSO6;
3593 break;
3594 default:
3595 /* oops */
3596 break;
3601 * enable transmits in the hardware, need to do this
3602 * after setting TARC(0)
3604 tctl = er32(TCTL);
3605 tctl |= E1000_TCTL_EN;
3606 ew32(TCTL, tctl);
3609 * Perform any post-link-up configuration before
3610 * reporting link up.
3612 if (phy->ops.cfg_on_link_up)
3613 phy->ops.cfg_on_link_up(hw);
3615 netif_carrier_on(netdev);
3616 netif_tx_wake_all_queues(netdev);
3618 if (!test_bit(__E1000_DOWN, &adapter->state))
3619 mod_timer(&adapter->phy_info_timer,
3620 round_jiffies(jiffies + 2 * HZ));
3622 } else {
3623 if (netif_carrier_ok(netdev)) {
3624 adapter->link_speed = 0;
3625 adapter->link_duplex = 0;
3626 /* Link status message must follow this format */
3627 printk(KERN_INFO "e1000e: %s NIC Link is Down\n",
3628 adapter->netdev->name);
3629 netif_carrier_off(netdev);
3630 netif_tx_stop_all_queues(netdev);
3631 if (!test_bit(__E1000_DOWN, &adapter->state))
3632 mod_timer(&adapter->phy_info_timer,
3633 round_jiffies(jiffies + 2 * HZ));
3635 if (adapter->flags & FLAG_RX_NEEDS_RESTART)
3636 schedule_work(&adapter->reset_task);
3640 link_up:
3641 e1000e_update_stats(adapter);
3643 mac->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
3644 adapter->tpt_old = adapter->stats.tpt;
3645 mac->collision_delta = adapter->stats.colc - adapter->colc_old;
3646 adapter->colc_old = adapter->stats.colc;
3648 adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
3649 adapter->gorc_old = adapter->stats.gorc;
3650 adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
3651 adapter->gotc_old = adapter->stats.gotc;
3653 e1000e_update_adaptive(&adapter->hw);
3655 if (!netif_carrier_ok(netdev)) {
3656 tx_pending = (e1000_desc_unused(tx_ring) + 1 <
3657 tx_ring->count);
3658 if (tx_pending) {
3660 * We've lost link, so the controller stops DMA,
3661 * but we've got queued Tx work that's never going
3662 * to get done, so reset controller to flush Tx.
3663 * (Do the reset outside of interrupt context).
3665 adapter->tx_timeout_count++;
3666 schedule_work(&adapter->reset_task);
3670 /* Cause software interrupt to ensure Rx ring is cleaned */
3671 if (adapter->msix_entries)
3672 ew32(ICS, adapter->rx_ring->ims_val);
3673 else
3674 ew32(ICS, E1000_ICS_RXDMT0);
3676 /* Force detection of hung controller every watchdog period */
3677 adapter->detect_tx_hung = 1;
3680 * With 82571 controllers, LAA may be overwritten due to controller
3681 * reset from the other port. Set the appropriate LAA in RAR[0]
3683 if (e1000e_get_laa_state_82571(hw))
3684 e1000e_rar_set(hw, adapter->hw.mac.addr, 0);
3686 /* Reset the timer */
3687 if (!test_bit(__E1000_DOWN, &adapter->state))
3688 mod_timer(&adapter->watchdog_timer,
3689 round_jiffies(jiffies + 2 * HZ));
3692 #define E1000_TX_FLAGS_CSUM 0x00000001
3693 #define E1000_TX_FLAGS_VLAN 0x00000002
3694 #define E1000_TX_FLAGS_TSO 0x00000004
3695 #define E1000_TX_FLAGS_IPV4 0x00000008
3696 #define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
3697 #define E1000_TX_FLAGS_VLAN_SHIFT 16
3699 static int e1000_tso(struct e1000_adapter *adapter,
3700 struct sk_buff *skb)
3702 struct e1000_ring *tx_ring = adapter->tx_ring;
3703 struct e1000_context_desc *context_desc;
3704 struct e1000_buffer *buffer_info;
3705 unsigned int i;
3706 u32 cmd_length = 0;
3707 u16 ipcse = 0, tucse, mss;
3708 u8 ipcss, ipcso, tucss, tucso, hdr_len;
3709 int err;
3711 if (skb_is_gso(skb)) {
3712 if (skb_header_cloned(skb)) {
3713 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3714 if (err)
3715 return err;
3718 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3719 mss = skb_shinfo(skb)->gso_size;
3720 if (skb->protocol == htons(ETH_P_IP)) {
3721 struct iphdr *iph = ip_hdr(skb);
3722 iph->tot_len = 0;
3723 iph->check = 0;
3724 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
3725 iph->daddr, 0,
3726 IPPROTO_TCP,
3728 cmd_length = E1000_TXD_CMD_IP;
3729 ipcse = skb_transport_offset(skb) - 1;
3730 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
3731 ipv6_hdr(skb)->payload_len = 0;
3732 tcp_hdr(skb)->check =
3733 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3734 &ipv6_hdr(skb)->daddr,
3735 0, IPPROTO_TCP, 0);
3736 ipcse = 0;
3738 ipcss = skb_network_offset(skb);
3739 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
3740 tucss = skb_transport_offset(skb);
3741 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
3742 tucse = 0;
3744 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
3745 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
3747 i = tx_ring->next_to_use;
3748 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
3749 buffer_info = &tx_ring->buffer_info[i];
3751 context_desc->lower_setup.ip_fields.ipcss = ipcss;
3752 context_desc->lower_setup.ip_fields.ipcso = ipcso;
3753 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
3754 context_desc->upper_setup.tcp_fields.tucss = tucss;
3755 context_desc->upper_setup.tcp_fields.tucso = tucso;
3756 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
3757 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
3758 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
3759 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
3761 buffer_info->time_stamp = jiffies;
3762 buffer_info->next_to_watch = i;
3764 i++;
3765 if (i == tx_ring->count)
3766 i = 0;
3767 tx_ring->next_to_use = i;
3769 return 1;
3772 return 0;
3775 static bool e1000_tx_csum(struct e1000_adapter *adapter, struct sk_buff *skb)
3777 struct e1000_ring *tx_ring = adapter->tx_ring;
3778 struct e1000_context_desc *context_desc;
3779 struct e1000_buffer *buffer_info;
3780 unsigned int i;
3781 u8 css;
3782 u32 cmd_len = E1000_TXD_CMD_DEXT;
3784 if (skb->ip_summed != CHECKSUM_PARTIAL)
3785 return 0;
3787 switch (skb->protocol) {
3788 case __constant_htons(ETH_P_IP):
3789 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
3790 cmd_len |= E1000_TXD_CMD_TCP;
3791 break;
3792 case __constant_htons(ETH_P_IPV6):
3793 /* XXX not handling all IPV6 headers */
3794 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
3795 cmd_len |= E1000_TXD_CMD_TCP;
3796 break;
3797 default:
3798 if (unlikely(net_ratelimit()))
3799 e_warn("checksum_partial proto=%x!\n", skb->protocol);
3800 break;
3803 css = skb_transport_offset(skb);
3805 i = tx_ring->next_to_use;
3806 buffer_info = &tx_ring->buffer_info[i];
3807 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
3809 context_desc->lower_setup.ip_config = 0;
3810 context_desc->upper_setup.tcp_fields.tucss = css;
3811 context_desc->upper_setup.tcp_fields.tucso =
3812 css + skb->csum_offset;
3813 context_desc->upper_setup.tcp_fields.tucse = 0;
3814 context_desc->tcp_seg_setup.data = 0;
3815 context_desc->cmd_and_length = cpu_to_le32(cmd_len);
3817 buffer_info->time_stamp = jiffies;
3818 buffer_info->next_to_watch = i;
3820 i++;
3821 if (i == tx_ring->count)
3822 i = 0;
3823 tx_ring->next_to_use = i;
3825 return 1;
3828 #define E1000_MAX_PER_TXD 8192
3829 #define E1000_MAX_TXD_PWR 12
3831 static int e1000_tx_map(struct e1000_adapter *adapter,
3832 struct sk_buff *skb, unsigned int first,
3833 unsigned int max_per_txd, unsigned int nr_frags,
3834 unsigned int mss)
3836 struct e1000_ring *tx_ring = adapter->tx_ring;
3837 unsigned int len = skb_headlen(skb);
3838 unsigned int offset, size, count = 0, i;
3839 unsigned int f;
3840 dma_addr_t map;
3842 i = tx_ring->next_to_use;
3844 if (skb_dma_map(&adapter->pdev->dev, skb, DMA_TO_DEVICE)) {
3845 dev_err(&adapter->pdev->dev, "TX DMA map failed\n");
3846 adapter->tx_dma_failed++;
3847 dev_kfree_skb(skb);
3848 return -2;
3851 map = skb_shinfo(skb)->dma_maps[0];
3852 offset = 0;
3854 while (len) {
3855 struct e1000_buffer *buffer_info = &tx_ring->buffer_info[i];
3856 size = min(len, max_per_txd);
3858 /* Workaround for premature desc write-backs
3859 * in TSO mode. Append 4-byte sentinel desc */
3860 if (mss && !nr_frags && size == len && size > 8)
3861 size -= 4;
3863 buffer_info->length = size;
3864 /* set time_stamp *before* dma to help avoid a possible race */
3865 buffer_info->time_stamp = jiffies;
3866 buffer_info->dma = map + offset;
3867 buffer_info->next_to_watch = i;
3869 len -= size;
3870 offset += size;
3871 count++;
3872 i++;
3873 if (i == tx_ring->count)
3874 i = 0;
3877 for (f = 0; f < nr_frags; f++) {
3878 struct skb_frag_struct *frag;
3880 frag = &skb_shinfo(skb)->frags[f];
3881 len = frag->size;
3882 map = skb_shinfo(skb)->dma_maps[f + 1];
3883 offset = 0;
3885 while (len) {
3886 struct e1000_buffer *buffer_info;
3887 buffer_info = &tx_ring->buffer_info[i];
3888 size = min(len, max_per_txd);
3889 /* Workaround for premature desc write-backs
3890 * in TSO mode. Append 4-byte sentinel desc */
3891 if (mss && f == (nr_frags-1) && size == len && size > 8)
3892 size -= 4;
3894 buffer_info->length = size;
3895 buffer_info->time_stamp = jiffies;
3896 buffer_info->dma = map + offset;
3897 buffer_info->next_to_watch = i;
3899 len -= size;
3900 offset += size;
3901 count++;
3903 i++;
3904 if (i == tx_ring->count)
3905 i = 0;
3909 if (i == 0)
3910 i = tx_ring->count - 1;
3911 else
3912 i--;
3914 tx_ring->buffer_info[i].skb = skb;
3915 tx_ring->buffer_info[first].next_to_watch = i;
3916 smp_wmb();
3918 return count;
3921 static void e1000_tx_queue(struct e1000_adapter *adapter,
3922 int tx_flags, int count)
3924 struct e1000_ring *tx_ring = adapter->tx_ring;
3925 struct e1000_tx_desc *tx_desc = NULL;
3926 struct e1000_buffer *buffer_info;
3927 u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
3928 unsigned int i;
3930 if (tx_flags & E1000_TX_FLAGS_TSO) {
3931 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
3932 E1000_TXD_CMD_TSE;
3933 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
3935 if (tx_flags & E1000_TX_FLAGS_IPV4)
3936 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
3939 if (tx_flags & E1000_TX_FLAGS_CSUM) {
3940 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
3941 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
3944 if (tx_flags & E1000_TX_FLAGS_VLAN) {
3945 txd_lower |= E1000_TXD_CMD_VLE;
3946 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
3949 i = tx_ring->next_to_use;
3951 while (count--) {
3952 buffer_info = &tx_ring->buffer_info[i];
3953 tx_desc = E1000_TX_DESC(*tx_ring, i);
3954 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
3955 tx_desc->lower.data =
3956 cpu_to_le32(txd_lower | buffer_info->length);
3957 tx_desc->upper.data = cpu_to_le32(txd_upper);
3959 i++;
3960 if (i == tx_ring->count)
3961 i = 0;
3964 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
3967 * Force memory writes to complete before letting h/w
3968 * know there are new descriptors to fetch. (Only
3969 * applicable for weak-ordered memory model archs,
3970 * such as IA-64).
3972 wmb();
3974 tx_ring->next_to_use = i;
3975 writel(i, adapter->hw.hw_addr + tx_ring->tail);
3977 * we need this if more than one processor can write to our tail
3978 * at a time, it synchronizes IO on IA64/Altix systems
3980 mmiowb();
3983 #define MINIMUM_DHCP_PACKET_SIZE 282
3984 static int e1000_transfer_dhcp_info(struct e1000_adapter *adapter,
3985 struct sk_buff *skb)
3987 struct e1000_hw *hw = &adapter->hw;
3988 u16 length, offset;
3990 if (vlan_tx_tag_present(skb)) {
3991 if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id)
3992 && (adapter->hw.mng_cookie.status &
3993 E1000_MNG_DHCP_COOKIE_STATUS_VLAN)))
3994 return 0;
3997 if (skb->len <= MINIMUM_DHCP_PACKET_SIZE)
3998 return 0;
4000 if (((struct ethhdr *) skb->data)->h_proto != htons(ETH_P_IP))
4001 return 0;
4004 const struct iphdr *ip = (struct iphdr *)((u8 *)skb->data+14);
4005 struct udphdr *udp;
4007 if (ip->protocol != IPPROTO_UDP)
4008 return 0;
4010 udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
4011 if (ntohs(udp->dest) != 67)
4012 return 0;
4014 offset = (u8 *)udp + 8 - skb->data;
4015 length = skb->len - offset;
4016 return e1000e_mng_write_dhcp_info(hw, (u8 *)udp + 8, length);
4019 return 0;
4022 static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
4024 struct e1000_adapter *adapter = netdev_priv(netdev);
4026 netif_stop_queue(netdev);
4028 * Herbert's original patch had:
4029 * smp_mb__after_netif_stop_queue();
4030 * but since that doesn't exist yet, just open code it.
4032 smp_mb();
4035 * We need to check again in a case another CPU has just
4036 * made room available.
4038 if (e1000_desc_unused(adapter->tx_ring) < size)
4039 return -EBUSY;
4041 /* A reprieve! */
4042 netif_start_queue(netdev);
4043 ++adapter->restart_queue;
4044 return 0;
4047 static int e1000_maybe_stop_tx(struct net_device *netdev, int size)
4049 struct e1000_adapter *adapter = netdev_priv(netdev);
4051 if (e1000_desc_unused(adapter->tx_ring) >= size)
4052 return 0;
4053 return __e1000_maybe_stop_tx(netdev, size);
4056 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
4057 static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
4059 struct e1000_adapter *adapter = netdev_priv(netdev);
4060 struct e1000_ring *tx_ring = adapter->tx_ring;
4061 unsigned int first;
4062 unsigned int max_per_txd = E1000_MAX_PER_TXD;
4063 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
4064 unsigned int tx_flags = 0;
4065 unsigned int len = skb->len - skb->data_len;
4066 unsigned long irq_flags;
4067 unsigned int nr_frags;
4068 unsigned int mss;
4069 int count = 0;
4070 int tso;
4071 unsigned int f;
4073 if (test_bit(__E1000_DOWN, &adapter->state)) {
4074 dev_kfree_skb_any(skb);
4075 return NETDEV_TX_OK;
4078 if (skb->len <= 0) {
4079 dev_kfree_skb_any(skb);
4080 return NETDEV_TX_OK;
4083 mss = skb_shinfo(skb)->gso_size;
4085 * The controller does a simple calculation to
4086 * make sure there is enough room in the FIFO before
4087 * initiating the DMA for each buffer. The calc is:
4088 * 4 = ceil(buffer len/mss). To make sure we don't
4089 * overrun the FIFO, adjust the max buffer len if mss
4090 * drops.
4092 if (mss) {
4093 u8 hdr_len;
4094 max_per_txd = min(mss << 2, max_per_txd);
4095 max_txd_pwr = fls(max_per_txd) - 1;
4098 * TSO Workaround for 82571/2/3 Controllers -- if skb->data
4099 * points to just header, pull a few bytes of payload from
4100 * frags into skb->data
4102 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
4104 * we do this workaround for ES2LAN, but it is un-necessary,
4105 * avoiding it could save a lot of cycles
4107 if (skb->data_len && (hdr_len == len)) {
4108 unsigned int pull_size;
4110 pull_size = min((unsigned int)4, skb->data_len);
4111 if (!__pskb_pull_tail(skb, pull_size)) {
4112 e_err("__pskb_pull_tail failed.\n");
4113 dev_kfree_skb_any(skb);
4114 return NETDEV_TX_OK;
4116 len = skb->len - skb->data_len;
4120 /* reserve a descriptor for the offload context */
4121 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
4122 count++;
4123 count++;
4125 count += TXD_USE_COUNT(len, max_txd_pwr);
4127 nr_frags = skb_shinfo(skb)->nr_frags;
4128 for (f = 0; f < nr_frags; f++)
4129 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
4130 max_txd_pwr);
4132 if (adapter->hw.mac.tx_pkt_filtering)
4133 e1000_transfer_dhcp_info(adapter, skb);
4135 spin_lock_irqsave(&adapter->tx_queue_lock, irq_flags);
4138 * need: count + 2 desc gap to keep tail from touching
4139 * head, otherwise try next time
4141 if (e1000_maybe_stop_tx(netdev, count + 2)) {
4142 spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
4143 return NETDEV_TX_BUSY;
4146 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
4147 tx_flags |= E1000_TX_FLAGS_VLAN;
4148 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
4151 first = tx_ring->next_to_use;
4153 tso = e1000_tso(adapter, skb);
4154 if (tso < 0) {
4155 dev_kfree_skb_any(skb);
4156 spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
4157 return NETDEV_TX_OK;
4160 if (tso)
4161 tx_flags |= E1000_TX_FLAGS_TSO;
4162 else if (e1000_tx_csum(adapter, skb))
4163 tx_flags |= E1000_TX_FLAGS_CSUM;
4166 * Old method was to assume IPv4 packet by default if TSO was enabled.
4167 * 82571 hardware supports TSO capabilities for IPv6 as well...
4168 * no longer assume, we must.
4170 if (skb->protocol == htons(ETH_P_IP))
4171 tx_flags |= E1000_TX_FLAGS_IPV4;
4173 count = e1000_tx_map(adapter, skb, first, max_per_txd, nr_frags, mss);
4174 if (count < 0) {
4175 /* handle pci_map_single() error in e1000_tx_map */
4176 dev_kfree_skb_any(skb);
4177 spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
4178 return NETDEV_TX_OK;
4181 e1000_tx_queue(adapter, tx_flags, count);
4183 netdev->trans_start = jiffies;
4185 /* Make sure there is space in the ring for the next send. */
4186 e1000_maybe_stop_tx(netdev, MAX_SKB_FRAGS + 2);
4188 spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
4189 return NETDEV_TX_OK;
4193 * e1000_tx_timeout - Respond to a Tx Hang
4194 * @netdev: network interface device structure
4196 static void e1000_tx_timeout(struct net_device *netdev)
4198 struct e1000_adapter *adapter = netdev_priv(netdev);
4200 /* Do the reset outside of interrupt context */
4201 adapter->tx_timeout_count++;
4202 schedule_work(&adapter->reset_task);
4205 static void e1000_reset_task(struct work_struct *work)
4207 struct e1000_adapter *adapter;
4208 adapter = container_of(work, struct e1000_adapter, reset_task);
4210 e1000e_reinit_locked(adapter);
4214 * e1000_get_stats - Get System Network Statistics
4215 * @netdev: network interface device structure
4217 * Returns the address of the device statistics structure.
4218 * The statistics are actually updated from the timer callback.
4220 static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
4222 struct e1000_adapter *adapter = netdev_priv(netdev);
4224 /* only return the current stats */
4225 return &adapter->net_stats;
4229 * e1000_change_mtu - Change the Maximum Transfer Unit
4230 * @netdev: network interface device structure
4231 * @new_mtu: new value for maximum frame size
4233 * Returns 0 on success, negative on failure
4235 static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
4237 struct e1000_adapter *adapter = netdev_priv(netdev);
4238 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
4240 if ((new_mtu < ETH_ZLEN + ETH_FCS_LEN + VLAN_HLEN) ||
4241 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
4242 e_err("Invalid MTU setting\n");
4243 return -EINVAL;
4246 /* Jumbo frame size limits */
4247 if (max_frame > ETH_FRAME_LEN + ETH_FCS_LEN) {
4248 if (!(adapter->flags & FLAG_HAS_JUMBO_FRAMES)) {
4249 e_err("Jumbo Frames not supported.\n");
4250 return -EINVAL;
4252 if (adapter->hw.phy.type == e1000_phy_ife) {
4253 e_err("Jumbo Frames not supported.\n");
4254 return -EINVAL;
4258 #define MAX_STD_JUMBO_FRAME_SIZE 9234
4259 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
4260 e_err("MTU > 9216 not supported.\n");
4261 return -EINVAL;
4264 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
4265 msleep(1);
4266 /* e1000e_down has a dependency on max_frame_size */
4267 adapter->max_frame_size = max_frame;
4268 if (netif_running(netdev))
4269 e1000e_down(adapter);
4272 * NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
4273 * means we reserve 2 more, this pushes us to allocate from the next
4274 * larger slab size.
4275 * i.e. RXBUFFER_2048 --> size-4096 slab
4276 * However with the new *_jumbo_rx* routines, jumbo receives will use
4277 * fragmented skbs
4280 if (max_frame <= 256)
4281 adapter->rx_buffer_len = 256;
4282 else if (max_frame <= 512)
4283 adapter->rx_buffer_len = 512;
4284 else if (max_frame <= 1024)
4285 adapter->rx_buffer_len = 1024;
4286 else if (max_frame <= 2048)
4287 adapter->rx_buffer_len = 2048;
4288 else
4289 adapter->rx_buffer_len = 4096;
4291 /* adjust allocation if LPE protects us, and we aren't using SBP */
4292 if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
4293 (max_frame == ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN))
4294 adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN
4295 + ETH_FCS_LEN;
4297 e_info("changing MTU from %d to %d\n", netdev->mtu, new_mtu);
4298 netdev->mtu = new_mtu;
4300 if (netif_running(netdev))
4301 e1000e_up(adapter);
4302 else
4303 e1000e_reset(adapter);
4305 clear_bit(__E1000_RESETTING, &adapter->state);
4307 return 0;
4310 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
4311 int cmd)
4313 struct e1000_adapter *adapter = netdev_priv(netdev);
4314 struct mii_ioctl_data *data = if_mii(ifr);
4316 if (adapter->hw.phy.media_type != e1000_media_type_copper)
4317 return -EOPNOTSUPP;
4319 switch (cmd) {
4320 case SIOCGMIIPHY:
4321 data->phy_id = adapter->hw.phy.addr;
4322 break;
4323 case SIOCGMIIREG:
4324 if (!capable(CAP_NET_ADMIN))
4325 return -EPERM;
4326 switch (data->reg_num & 0x1F) {
4327 case MII_BMCR:
4328 data->val_out = adapter->phy_regs.bmcr;
4329 break;
4330 case MII_BMSR:
4331 data->val_out = adapter->phy_regs.bmsr;
4332 break;
4333 case MII_PHYSID1:
4334 data->val_out = (adapter->hw.phy.id >> 16);
4335 break;
4336 case MII_PHYSID2:
4337 data->val_out = (adapter->hw.phy.id & 0xFFFF);
4338 break;
4339 case MII_ADVERTISE:
4340 data->val_out = adapter->phy_regs.advertise;
4341 break;
4342 case MII_LPA:
4343 data->val_out = adapter->phy_regs.lpa;
4344 break;
4345 case MII_EXPANSION:
4346 data->val_out = adapter->phy_regs.expansion;
4347 break;
4348 case MII_CTRL1000:
4349 data->val_out = adapter->phy_regs.ctrl1000;
4350 break;
4351 case MII_STAT1000:
4352 data->val_out = adapter->phy_regs.stat1000;
4353 break;
4354 case MII_ESTATUS:
4355 data->val_out = adapter->phy_regs.estatus;
4356 break;
4357 default:
4358 return -EIO;
4360 break;
4361 case SIOCSMIIREG:
4362 default:
4363 return -EOPNOTSUPP;
4365 return 0;
4368 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4370 switch (cmd) {
4371 case SIOCGMIIPHY:
4372 case SIOCGMIIREG:
4373 case SIOCSMIIREG:
4374 return e1000_mii_ioctl(netdev, ifr, cmd);
4375 default:
4376 return -EOPNOTSUPP;
4380 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
4382 struct net_device *netdev = pci_get_drvdata(pdev);
4383 struct e1000_adapter *adapter = netdev_priv(netdev);
4384 struct e1000_hw *hw = &adapter->hw;
4385 u32 ctrl, ctrl_ext, rctl, status;
4386 u32 wufc = adapter->wol;
4387 int retval = 0;
4389 netif_device_detach(netdev);
4391 if (netif_running(netdev)) {
4392 WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
4393 e1000e_down(adapter);
4394 e1000_free_irq(adapter);
4396 e1000e_reset_interrupt_capability(adapter);
4398 retval = pci_save_state(pdev);
4399 if (retval)
4400 return retval;
4402 status = er32(STATUS);
4403 if (status & E1000_STATUS_LU)
4404 wufc &= ~E1000_WUFC_LNKC;
4406 if (wufc) {
4407 e1000_setup_rctl(adapter);
4408 e1000_set_multi(netdev);
4410 /* turn on all-multi mode if wake on multicast is enabled */
4411 if (wufc & E1000_WUFC_MC) {
4412 rctl = er32(RCTL);
4413 rctl |= E1000_RCTL_MPE;
4414 ew32(RCTL, rctl);
4417 ctrl = er32(CTRL);
4418 /* advertise wake from D3Cold */
4419 #define E1000_CTRL_ADVD3WUC 0x00100000
4420 /* phy power management enable */
4421 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4422 ctrl |= E1000_CTRL_ADVD3WUC |
4423 E1000_CTRL_EN_PHY_PWR_MGMT;
4424 ew32(CTRL, ctrl);
4426 if (adapter->hw.phy.media_type == e1000_media_type_fiber ||
4427 adapter->hw.phy.media_type ==
4428 e1000_media_type_internal_serdes) {
4429 /* keep the laser running in D3 */
4430 ctrl_ext = er32(CTRL_EXT);
4431 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
4432 ew32(CTRL_EXT, ctrl_ext);
4435 if (adapter->flags & FLAG_IS_ICH)
4436 e1000e_disable_gig_wol_ich8lan(&adapter->hw);
4438 /* Allow time for pending master requests to run */
4439 e1000e_disable_pcie_master(&adapter->hw);
4441 ew32(WUC, E1000_WUC_PME_EN);
4442 ew32(WUFC, wufc);
4443 pci_enable_wake(pdev, PCI_D3hot, 1);
4444 pci_enable_wake(pdev, PCI_D3cold, 1);
4445 } else {
4446 ew32(WUC, 0);
4447 ew32(WUFC, 0);
4448 pci_enable_wake(pdev, PCI_D3hot, 0);
4449 pci_enable_wake(pdev, PCI_D3cold, 0);
4452 /* make sure adapter isn't asleep if manageability is enabled */
4453 if (adapter->flags & FLAG_MNG_PT_ENABLED) {
4454 pci_enable_wake(pdev, PCI_D3hot, 1);
4455 pci_enable_wake(pdev, PCI_D3cold, 1);
4458 if (adapter->hw.phy.type == e1000_phy_igp_3)
4459 e1000e_igp3_phy_powerdown_workaround_ich8lan(&adapter->hw);
4462 * Release control of h/w to f/w. If f/w is AMT enabled, this
4463 * would have already happened in close and is redundant.
4465 e1000_release_hw_control(adapter);
4467 pci_disable_device(pdev);
4470 * The pci-e switch on some quad port adapters will report a
4471 * correctable error when the MAC transitions from D0 to D3. To
4472 * prevent this we need to mask off the correctable errors on the
4473 * downstream port of the pci-e switch.
4475 if (adapter->flags & FLAG_IS_QUAD_PORT) {
4476 struct pci_dev *us_dev = pdev->bus->self;
4477 int pos = pci_find_capability(us_dev, PCI_CAP_ID_EXP);
4478 u16 devctl;
4480 pci_read_config_word(us_dev, pos + PCI_EXP_DEVCTL, &devctl);
4481 pci_write_config_word(us_dev, pos + PCI_EXP_DEVCTL,
4482 (devctl & ~PCI_EXP_DEVCTL_CERE));
4484 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4486 pci_write_config_word(us_dev, pos + PCI_EXP_DEVCTL, devctl);
4487 } else {
4488 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4491 return 0;
4494 static void e1000e_disable_l1aspm(struct pci_dev *pdev)
4496 int pos;
4497 u16 val;
4500 * 82573 workaround - disable L1 ASPM on mobile chipsets
4502 * L1 ASPM on various mobile (ich7) chipsets do not behave properly
4503 * resulting in lost data or garbage information on the pci-e link
4504 * level. This could result in (false) bad EEPROM checksum errors,
4505 * long ping times (up to 2s) or even a system freeze/hang.
4507 * Unfortunately this feature saves about 1W power consumption when
4508 * active.
4510 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4511 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &val);
4512 if (val & 0x2) {
4513 dev_warn(&pdev->dev, "Disabling L1 ASPM\n");
4514 val &= ~0x2;
4515 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, val);
4519 #ifdef CONFIG_PM
4520 static int e1000_resume(struct pci_dev *pdev)
4522 struct net_device *netdev = pci_get_drvdata(pdev);
4523 struct e1000_adapter *adapter = netdev_priv(netdev);
4524 struct e1000_hw *hw = &adapter->hw;
4525 u32 err;
4527 pci_set_power_state(pdev, PCI_D0);
4528 pci_restore_state(pdev);
4529 e1000e_disable_l1aspm(pdev);
4531 err = pci_enable_device_mem(pdev);
4532 if (err) {
4533 dev_err(&pdev->dev,
4534 "Cannot enable PCI device from suspend\n");
4535 return err;
4538 pci_set_master(pdev);
4540 pci_enable_wake(pdev, PCI_D3hot, 0);
4541 pci_enable_wake(pdev, PCI_D3cold, 0);
4543 e1000e_set_interrupt_capability(adapter);
4544 if (netif_running(netdev)) {
4545 err = e1000_request_irq(adapter);
4546 if (err)
4547 return err;
4550 e1000e_power_up_phy(adapter);
4551 e1000e_reset(adapter);
4552 ew32(WUS, ~0);
4554 e1000_init_manageability(adapter);
4556 if (netif_running(netdev))
4557 e1000e_up(adapter);
4559 netif_device_attach(netdev);
4562 * If the controller has AMT, do not set DRV_LOAD until the interface
4563 * is up. For all other cases, let the f/w know that the h/w is now
4564 * under the control of the driver.
4566 if (!(adapter->flags & FLAG_HAS_AMT))
4567 e1000_get_hw_control(adapter);
4569 return 0;
4571 #endif
4573 static void e1000_shutdown(struct pci_dev *pdev)
4575 e1000_suspend(pdev, PMSG_SUSPEND);
4578 #ifdef CONFIG_NET_POLL_CONTROLLER
4580 * Polling 'interrupt' - used by things like netconsole to send skbs
4581 * without having to re-enable interrupts. It's not called while
4582 * the interrupt routine is executing.
4584 static void e1000_netpoll(struct net_device *netdev)
4586 struct e1000_adapter *adapter = netdev_priv(netdev);
4588 disable_irq(adapter->pdev->irq);
4589 e1000_intr(adapter->pdev->irq, netdev);
4591 enable_irq(adapter->pdev->irq);
4593 #endif
4596 * e1000_io_error_detected - called when PCI error is detected
4597 * @pdev: Pointer to PCI device
4598 * @state: The current pci connection state
4600 * This function is called after a PCI bus error affecting
4601 * this device has been detected.
4603 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
4604 pci_channel_state_t state)
4606 struct net_device *netdev = pci_get_drvdata(pdev);
4607 struct e1000_adapter *adapter = netdev_priv(netdev);
4609 netif_device_detach(netdev);
4611 if (netif_running(netdev))
4612 e1000e_down(adapter);
4613 pci_disable_device(pdev);
4615 /* Request a slot slot reset. */
4616 return PCI_ERS_RESULT_NEED_RESET;
4620 * e1000_io_slot_reset - called after the pci bus has been reset.
4621 * @pdev: Pointer to PCI device
4623 * Restart the card from scratch, as if from a cold-boot. Implementation
4624 * resembles the first-half of the e1000_resume routine.
4626 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
4628 struct net_device *netdev = pci_get_drvdata(pdev);
4629 struct e1000_adapter *adapter = netdev_priv(netdev);
4630 struct e1000_hw *hw = &adapter->hw;
4631 int err;
4633 e1000e_disable_l1aspm(pdev);
4634 err = pci_enable_device_mem(pdev);
4635 if (err) {
4636 dev_err(&pdev->dev,
4637 "Cannot re-enable PCI device after reset.\n");
4638 return PCI_ERS_RESULT_DISCONNECT;
4640 pci_set_master(pdev);
4641 pci_restore_state(pdev);
4643 pci_enable_wake(pdev, PCI_D3hot, 0);
4644 pci_enable_wake(pdev, PCI_D3cold, 0);
4646 e1000e_reset(adapter);
4647 ew32(WUS, ~0);
4649 return PCI_ERS_RESULT_RECOVERED;
4653 * e1000_io_resume - called when traffic can start flowing again.
4654 * @pdev: Pointer to PCI device
4656 * This callback is called when the error recovery driver tells us that
4657 * its OK to resume normal operation. Implementation resembles the
4658 * second-half of the e1000_resume routine.
4660 static void e1000_io_resume(struct pci_dev *pdev)
4662 struct net_device *netdev = pci_get_drvdata(pdev);
4663 struct e1000_adapter *adapter = netdev_priv(netdev);
4665 e1000_init_manageability(adapter);
4667 if (netif_running(netdev)) {
4668 if (e1000e_up(adapter)) {
4669 dev_err(&pdev->dev,
4670 "can't bring device back up after reset\n");
4671 return;
4675 netif_device_attach(netdev);
4678 * If the controller has AMT, do not set DRV_LOAD until the interface
4679 * is up. For all other cases, let the f/w know that the h/w is now
4680 * under the control of the driver.
4682 if (!(adapter->flags & FLAG_HAS_AMT))
4683 e1000_get_hw_control(adapter);
4687 static void e1000_print_device_info(struct e1000_adapter *adapter)
4689 struct e1000_hw *hw = &adapter->hw;
4690 struct net_device *netdev = adapter->netdev;
4691 u32 pba_num;
4693 /* print bus type/speed/width info */
4694 e_info("(PCI Express:2.5GB/s:%s) %pM\n",
4695 /* bus width */
4696 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
4697 "Width x1"),
4698 /* MAC address */
4699 netdev->dev_addr);
4700 e_info("Intel(R) PRO/%s Network Connection\n",
4701 (hw->phy.type == e1000_phy_ife) ? "10/100" : "1000");
4702 e1000e_read_pba_num(hw, &pba_num);
4703 e_info("MAC: %d, PHY: %d, PBA No: %06x-%03x\n",
4704 hw->mac.type, hw->phy.type, (pba_num >> 8), (pba_num & 0xff));
4707 static void e1000_eeprom_checks(struct e1000_adapter *adapter)
4709 struct e1000_hw *hw = &adapter->hw;
4710 int ret_val;
4711 u16 buf = 0;
4713 if (hw->mac.type != e1000_82573)
4714 return;
4716 ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &buf);
4717 if (!ret_val && (!(le16_to_cpu(buf) & (1 << 0)))) {
4718 /* Deep Smart Power Down (DSPD) */
4719 dev_warn(&adapter->pdev->dev,
4720 "Warning: detected DSPD enabled in EEPROM\n");
4723 ret_val = e1000_read_nvm(hw, NVM_INIT_3GIO_3, 1, &buf);
4724 if (!ret_val && (le16_to_cpu(buf) & (3 << 2))) {
4725 /* ASPM enable */
4726 dev_warn(&adapter->pdev->dev,
4727 "Warning: detected ASPM enabled in EEPROM\n");
4731 static const struct net_device_ops e1000e_netdev_ops = {
4732 .ndo_open = e1000_open,
4733 .ndo_stop = e1000_close,
4734 .ndo_start_xmit = e1000_xmit_frame,
4735 .ndo_get_stats = e1000_get_stats,
4736 .ndo_set_multicast_list = e1000_set_multi,
4737 .ndo_set_mac_address = e1000_set_mac,
4738 .ndo_change_mtu = e1000_change_mtu,
4739 .ndo_do_ioctl = e1000_ioctl,
4740 .ndo_tx_timeout = e1000_tx_timeout,
4741 .ndo_validate_addr = eth_validate_addr,
4743 .ndo_vlan_rx_register = e1000_vlan_rx_register,
4744 .ndo_vlan_rx_add_vid = e1000_vlan_rx_add_vid,
4745 .ndo_vlan_rx_kill_vid = e1000_vlan_rx_kill_vid,
4746 #ifdef CONFIG_NET_POLL_CONTROLLER
4747 .ndo_poll_controller = e1000_netpoll,
4748 #endif
4752 * e1000_probe - Device Initialization Routine
4753 * @pdev: PCI device information struct
4754 * @ent: entry in e1000_pci_tbl
4756 * Returns 0 on success, negative on failure
4758 * e1000_probe initializes an adapter identified by a pci_dev structure.
4759 * The OS initialization, configuring of the adapter private structure,
4760 * and a hardware reset occur.
4762 static int __devinit e1000_probe(struct pci_dev *pdev,
4763 const struct pci_device_id *ent)
4765 struct net_device *netdev;
4766 struct e1000_adapter *adapter;
4767 struct e1000_hw *hw;
4768 const struct e1000_info *ei = e1000_info_tbl[ent->driver_data];
4769 resource_size_t mmio_start, mmio_len;
4770 resource_size_t flash_start, flash_len;
4772 static int cards_found;
4773 int i, err, pci_using_dac;
4774 u16 eeprom_data = 0;
4775 u16 eeprom_apme_mask = E1000_EEPROM_APME;
4777 e1000e_disable_l1aspm(pdev);
4779 err = pci_enable_device_mem(pdev);
4780 if (err)
4781 return err;
4783 pci_using_dac = 0;
4784 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
4785 if (!err) {
4786 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
4787 if (!err)
4788 pci_using_dac = 1;
4789 } else {
4790 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
4791 if (err) {
4792 err = pci_set_consistent_dma_mask(pdev,
4793 DMA_32BIT_MASK);
4794 if (err) {
4795 dev_err(&pdev->dev, "No usable DMA "
4796 "configuration, aborting\n");
4797 goto err_dma;
4802 err = pci_request_selected_regions_exclusive(pdev,
4803 pci_select_bars(pdev, IORESOURCE_MEM),
4804 e1000e_driver_name);
4805 if (err)
4806 goto err_pci_reg;
4808 pci_set_master(pdev);
4809 /* PCI config space info */
4810 err = pci_save_state(pdev);
4811 if (err)
4812 goto err_alloc_etherdev;
4814 err = -ENOMEM;
4815 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
4816 if (!netdev)
4817 goto err_alloc_etherdev;
4819 SET_NETDEV_DEV(netdev, &pdev->dev);
4821 pci_set_drvdata(pdev, netdev);
4822 adapter = netdev_priv(netdev);
4823 hw = &adapter->hw;
4824 adapter->netdev = netdev;
4825 adapter->pdev = pdev;
4826 adapter->ei = ei;
4827 adapter->pba = ei->pba;
4828 adapter->flags = ei->flags;
4829 adapter->flags2 = ei->flags2;
4830 adapter->hw.adapter = adapter;
4831 adapter->hw.mac.type = ei->mac;
4832 adapter->msg_enable = (1 << NETIF_MSG_DRV | NETIF_MSG_PROBE) - 1;
4834 mmio_start = pci_resource_start(pdev, 0);
4835 mmio_len = pci_resource_len(pdev, 0);
4837 err = -EIO;
4838 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
4839 if (!adapter->hw.hw_addr)
4840 goto err_ioremap;
4842 if ((adapter->flags & FLAG_HAS_FLASH) &&
4843 (pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
4844 flash_start = pci_resource_start(pdev, 1);
4845 flash_len = pci_resource_len(pdev, 1);
4846 adapter->hw.flash_address = ioremap(flash_start, flash_len);
4847 if (!adapter->hw.flash_address)
4848 goto err_flashmap;
4851 /* construct the net_device struct */
4852 netdev->netdev_ops = &e1000e_netdev_ops;
4853 e1000e_set_ethtool_ops(netdev);
4854 netdev->watchdog_timeo = 5 * HZ;
4855 netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
4856 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
4858 netdev->mem_start = mmio_start;
4859 netdev->mem_end = mmio_start + mmio_len;
4861 adapter->bd_number = cards_found++;
4863 e1000e_check_options(adapter);
4865 /* setup adapter struct */
4866 err = e1000_sw_init(adapter);
4867 if (err)
4868 goto err_sw_init;
4870 err = -EIO;
4872 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
4873 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
4874 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
4876 err = ei->get_variants(adapter);
4877 if (err)
4878 goto err_hw_init;
4880 if ((adapter->flags & FLAG_IS_ICH) &&
4881 (adapter->flags & FLAG_READ_ONLY_NVM))
4882 e1000e_write_protect_nvm_ich8lan(&adapter->hw);
4884 hw->mac.ops.get_bus_info(&adapter->hw);
4886 adapter->hw.phy.autoneg_wait_to_complete = 0;
4888 /* Copper options */
4889 if (adapter->hw.phy.media_type == e1000_media_type_copper) {
4890 adapter->hw.phy.mdix = AUTO_ALL_MODES;
4891 adapter->hw.phy.disable_polarity_correction = 0;
4892 adapter->hw.phy.ms_type = e1000_ms_hw_default;
4895 if (e1000_check_reset_block(&adapter->hw))
4896 e_info("PHY reset is blocked due to SOL/IDER session.\n");
4898 netdev->features = NETIF_F_SG |
4899 NETIF_F_HW_CSUM |
4900 NETIF_F_HW_VLAN_TX |
4901 NETIF_F_HW_VLAN_RX;
4903 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER)
4904 netdev->features |= NETIF_F_HW_VLAN_FILTER;
4906 netdev->features |= NETIF_F_TSO;
4907 netdev->features |= NETIF_F_TSO6;
4909 netdev->vlan_features |= NETIF_F_TSO;
4910 netdev->vlan_features |= NETIF_F_TSO6;
4911 netdev->vlan_features |= NETIF_F_HW_CSUM;
4912 netdev->vlan_features |= NETIF_F_SG;
4914 if (pci_using_dac)
4915 netdev->features |= NETIF_F_HIGHDMA;
4918 * We should not be using LLTX anymore, but we are still Tx faster with
4919 * it.
4921 netdev->features |= NETIF_F_LLTX;
4923 if (e1000e_enable_mng_pass_thru(&adapter->hw))
4924 adapter->flags |= FLAG_MNG_PT_ENABLED;
4927 * before reading the NVM, reset the controller to
4928 * put the device in a known good starting state
4930 adapter->hw.mac.ops.reset_hw(&adapter->hw);
4933 * systems with ASPM and others may see the checksum fail on the first
4934 * attempt. Let's give it a few tries
4936 for (i = 0;; i++) {
4937 if (e1000_validate_nvm_checksum(&adapter->hw) >= 0)
4938 break;
4939 if (i == 2) {
4940 e_err("The NVM Checksum Is Not Valid\n");
4941 err = -EIO;
4942 goto err_eeprom;
4946 e1000_eeprom_checks(adapter);
4948 /* copy the MAC address out of the NVM */
4949 if (e1000e_read_mac_addr(&adapter->hw))
4950 e_err("NVM Read Error while reading MAC address\n");
4952 memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
4953 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
4955 if (!is_valid_ether_addr(netdev->perm_addr)) {
4956 e_err("Invalid MAC Address: %pM\n", netdev->perm_addr);
4957 err = -EIO;
4958 goto err_eeprom;
4961 init_timer(&adapter->watchdog_timer);
4962 adapter->watchdog_timer.function = &e1000_watchdog;
4963 adapter->watchdog_timer.data = (unsigned long) adapter;
4965 init_timer(&adapter->phy_info_timer);
4966 adapter->phy_info_timer.function = &e1000_update_phy_info;
4967 adapter->phy_info_timer.data = (unsigned long) adapter;
4969 INIT_WORK(&adapter->reset_task, e1000_reset_task);
4970 INIT_WORK(&adapter->watchdog_task, e1000_watchdog_task);
4971 INIT_WORK(&adapter->downshift_task, e1000e_downshift_workaround);
4972 INIT_WORK(&adapter->update_phy_task, e1000e_update_phy_task);
4974 /* Initialize link parameters. User can change them with ethtool */
4975 adapter->hw.mac.autoneg = 1;
4976 adapter->fc_autoneg = 1;
4977 adapter->hw.fc.requested_mode = e1000_fc_default;
4978 adapter->hw.fc.current_mode = e1000_fc_default;
4979 adapter->hw.phy.autoneg_advertised = 0x2f;
4981 /* ring size defaults */
4982 adapter->rx_ring->count = 256;
4983 adapter->tx_ring->count = 256;
4986 * Initial Wake on LAN setting - If APM wake is enabled in
4987 * the EEPROM, enable the ACPI Magic Packet filter
4989 if (adapter->flags & FLAG_APME_IN_WUC) {
4990 /* APME bit in EEPROM is mapped to WUC.APME */
4991 eeprom_data = er32(WUC);
4992 eeprom_apme_mask = E1000_WUC_APME;
4993 } else if (adapter->flags & FLAG_APME_IN_CTRL3) {
4994 if (adapter->flags & FLAG_APME_CHECK_PORT_B &&
4995 (adapter->hw.bus.func == 1))
4996 e1000_read_nvm(&adapter->hw,
4997 NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
4998 else
4999 e1000_read_nvm(&adapter->hw,
5000 NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
5003 /* fetch WoL from EEPROM */
5004 if (eeprom_data & eeprom_apme_mask)
5005 adapter->eeprom_wol |= E1000_WUFC_MAG;
5008 * now that we have the eeprom settings, apply the special cases
5009 * where the eeprom may be wrong or the board simply won't support
5010 * wake on lan on a particular port
5012 if (!(adapter->flags & FLAG_HAS_WOL))
5013 adapter->eeprom_wol = 0;
5015 /* initialize the wol settings based on the eeprom settings */
5016 adapter->wol = adapter->eeprom_wol;
5017 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
5019 /* save off EEPROM version number */
5020 e1000_read_nvm(&adapter->hw, 5, 1, &adapter->eeprom_vers);
5022 /* reset the hardware with the new settings */
5023 e1000e_reset(adapter);
5026 * If the controller has AMT, do not set DRV_LOAD until the interface
5027 * is up. For all other cases, let the f/w know that the h/w is now
5028 * under the control of the driver.
5030 if (!(adapter->flags & FLAG_HAS_AMT))
5031 e1000_get_hw_control(adapter);
5033 /* tell the stack to leave us alone until e1000_open() is called */
5034 netif_carrier_off(netdev);
5035 netif_tx_stop_all_queues(netdev);
5037 strcpy(netdev->name, "eth%d");
5038 err = register_netdev(netdev);
5039 if (err)
5040 goto err_register;
5042 e1000_print_device_info(adapter);
5044 return 0;
5046 err_register:
5047 if (!(adapter->flags & FLAG_HAS_AMT))
5048 e1000_release_hw_control(adapter);
5049 err_eeprom:
5050 if (!e1000_check_reset_block(&adapter->hw))
5051 e1000_phy_hw_reset(&adapter->hw);
5052 err_hw_init:
5054 kfree(adapter->tx_ring);
5055 kfree(adapter->rx_ring);
5056 err_sw_init:
5057 if (adapter->hw.flash_address)
5058 iounmap(adapter->hw.flash_address);
5059 e1000e_reset_interrupt_capability(adapter);
5060 err_flashmap:
5061 iounmap(adapter->hw.hw_addr);
5062 err_ioremap:
5063 free_netdev(netdev);
5064 err_alloc_etherdev:
5065 pci_release_selected_regions(pdev,
5066 pci_select_bars(pdev, IORESOURCE_MEM));
5067 err_pci_reg:
5068 err_dma:
5069 pci_disable_device(pdev);
5070 return err;
5074 * e1000_remove - Device Removal Routine
5075 * @pdev: PCI device information struct
5077 * e1000_remove is called by the PCI subsystem to alert the driver
5078 * that it should release a PCI device. The could be caused by a
5079 * Hot-Plug event, or because the driver is going to be removed from
5080 * memory.
5082 static void __devexit e1000_remove(struct pci_dev *pdev)
5084 struct net_device *netdev = pci_get_drvdata(pdev);
5085 struct e1000_adapter *adapter = netdev_priv(netdev);
5088 * flush_scheduled work may reschedule our watchdog task, so
5089 * explicitly disable watchdog tasks from being rescheduled
5091 set_bit(__E1000_DOWN, &adapter->state);
5092 del_timer_sync(&adapter->watchdog_timer);
5093 del_timer_sync(&adapter->phy_info_timer);
5095 flush_scheduled_work();
5098 * Release control of h/w to f/w. If f/w is AMT enabled, this
5099 * would have already happened in close and is redundant.
5101 e1000_release_hw_control(adapter);
5103 unregister_netdev(netdev);
5105 if (!e1000_check_reset_block(&adapter->hw))
5106 e1000_phy_hw_reset(&adapter->hw);
5108 e1000e_reset_interrupt_capability(adapter);
5109 kfree(adapter->tx_ring);
5110 kfree(adapter->rx_ring);
5112 iounmap(adapter->hw.hw_addr);
5113 if (adapter->hw.flash_address)
5114 iounmap(adapter->hw.flash_address);
5115 pci_release_selected_regions(pdev,
5116 pci_select_bars(pdev, IORESOURCE_MEM));
5118 free_netdev(netdev);
5120 pci_disable_device(pdev);
5123 /* PCI Error Recovery (ERS) */
5124 static struct pci_error_handlers e1000_err_handler = {
5125 .error_detected = e1000_io_error_detected,
5126 .slot_reset = e1000_io_slot_reset,
5127 .resume = e1000_io_resume,
5130 static struct pci_device_id e1000_pci_tbl[] = {
5131 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_COPPER), board_82571 },
5132 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_FIBER), board_82571 },
5133 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER), board_82571 },
5134 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER_LP), board_82571 },
5135 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_FIBER), board_82571 },
5136 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES), board_82571 },
5137 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_DUAL), board_82571 },
5138 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_QUAD), board_82571 },
5139 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571PT_QUAD_COPPER), board_82571 },
5141 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI), board_82572 },
5142 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_COPPER), board_82572 },
5143 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_FIBER), board_82572 },
5144 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_SERDES), board_82572 },
5146 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E), board_82573 },
5147 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E_IAMT), board_82573 },
5148 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573L), board_82573 },
5150 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82574L), board_82574 },
5152 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_DPT),
5153 board_80003es2lan },
5154 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_SPT),
5155 board_80003es2lan },
5156 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_DPT),
5157 board_80003es2lan },
5158 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_SPT),
5159 board_80003es2lan },
5161 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE), board_ich8lan },
5162 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_G), board_ich8lan },
5163 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_GT), board_ich8lan },
5164 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_AMT), board_ich8lan },
5165 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_C), board_ich8lan },
5166 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M), board_ich8lan },
5167 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M_AMT), board_ich8lan },
5169 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE), board_ich9lan },
5170 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_G), board_ich9lan },
5171 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_GT), board_ich9lan },
5172 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_AMT), board_ich9lan },
5173 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_C), board_ich9lan },
5174 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_BM), board_ich9lan },
5175 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M), board_ich9lan },
5176 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_AMT), board_ich9lan },
5177 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_V), board_ich9lan },
5179 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LM), board_ich9lan },
5180 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LF), board_ich9lan },
5181 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_V), board_ich9lan },
5183 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_LM), board_ich10lan },
5184 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_LF), board_ich10lan },
5186 { } /* terminate list */
5188 MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
5190 /* PCI Device API Driver */
5191 static struct pci_driver e1000_driver = {
5192 .name = e1000e_driver_name,
5193 .id_table = e1000_pci_tbl,
5194 .probe = e1000_probe,
5195 .remove = __devexit_p(e1000_remove),
5196 #ifdef CONFIG_PM
5197 /* Power Management Hooks */
5198 .suspend = e1000_suspend,
5199 .resume = e1000_resume,
5200 #endif
5201 .shutdown = e1000_shutdown,
5202 .err_handler = &e1000_err_handler
5206 * e1000_init_module - Driver Registration Routine
5208 * e1000_init_module is the first routine called when the driver is
5209 * loaded. All it does is register with the PCI subsystem.
5211 static int __init e1000_init_module(void)
5213 int ret;
5214 printk(KERN_INFO "%s: Intel(R) PRO/1000 Network Driver - %s\n",
5215 e1000e_driver_name, e1000e_driver_version);
5216 printk(KERN_INFO "%s: Copyright (c) 1999-2008 Intel Corporation.\n",
5217 e1000e_driver_name);
5218 ret = pci_register_driver(&e1000_driver);
5219 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY, e1000e_driver_name,
5220 PM_QOS_DEFAULT_VALUE);
5222 return ret;
5224 module_init(e1000_init_module);
5227 * e1000_exit_module - Driver Exit Cleanup Routine
5229 * e1000_exit_module is called just before the driver is removed
5230 * from memory.
5232 static void __exit e1000_exit_module(void)
5234 pci_unregister_driver(&e1000_driver);
5235 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY, e1000e_driver_name);
5237 module_exit(e1000_exit_module);
5240 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
5241 MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
5242 MODULE_LICENSE("GPL");
5243 MODULE_VERSION(DRV_VERSION);
5245 /* e1000_main.c */