1 /*******************************************************************************
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2007 Intel Corporation.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
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".
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/types.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/netdevice.h>
33 #include <linux/vmalloc.h>
34 #include <linux/string.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/ethtool.h>
42 #include <linux/if_vlan.h>
45 #include "ixgbe_common.h"
47 char ixgbe_driver_name[] = "ixgbe";
48 static const char ixgbe_driver_string[] =
49 "Intel(R) 10 Gigabit PCI Express Network Driver";
51 #define DRV_VERSION "1.3.18-k4"
52 const char ixgbe_driver_version[] = DRV_VERSION;
53 static const char ixgbe_copyright[] =
54 "Copyright (c) 1999-2007 Intel Corporation.";
56 static const struct ixgbe_info *ixgbe_info_tbl[] = {
57 [board_82598] = &ixgbe_82598_info,
60 /* ixgbe_pci_tbl - PCI Device ID Table
62 * Wildcard entries (PCI_ANY_ID) should come last
63 * Last entry must be all 0s
65 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
66 * Class, Class Mask, private data (not used) }
68 static struct pci_device_id ixgbe_pci_tbl[] = {
69 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_DUAL_PORT),
71 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_SINGLE_PORT),
73 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_CX4),
75 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_CX4_DUAL_PORT),
78 /* required last entry */
81 MODULE_DEVICE_TABLE(pci, ixgbe_pci_tbl);
83 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
84 static int ixgbe_notify_dca(struct notifier_block *, unsigned long event,
86 static struct notifier_block dca_notifier = {
87 .notifier_call = ixgbe_notify_dca,
93 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
94 MODULE_DESCRIPTION("Intel(R) 10 Gigabit PCI Express Network Driver");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(DRV_VERSION);
98 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
100 static void ixgbe_release_hw_control(struct ixgbe_adapter *adapter)
104 /* Let firmware take over control of h/w */
105 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
106 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
107 ctrl_ext & ~IXGBE_CTRL_EXT_DRV_LOAD);
110 static void ixgbe_get_hw_control(struct ixgbe_adapter *adapter)
114 /* Let firmware know the driver has taken over */
115 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
116 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
117 ctrl_ext | IXGBE_CTRL_EXT_DRV_LOAD);
122 * ixgbe_get_hw_dev_name - return device name string
123 * used by hardware layer to print debugging information
125 char *ixgbe_get_hw_dev_name(struct ixgbe_hw *hw)
127 struct ixgbe_adapter *adapter = hw->back;
128 struct net_device *netdev = adapter->netdev;
133 static void ixgbe_set_ivar(struct ixgbe_adapter *adapter, u16 int_alloc_entry,
138 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
139 index = (int_alloc_entry >> 2) & 0x1F;
140 ivar = IXGBE_READ_REG(&adapter->hw, IXGBE_IVAR(index));
141 ivar &= ~(0xFF << (8 * (int_alloc_entry & 0x3)));
142 ivar |= (msix_vector << (8 * (int_alloc_entry & 0x3)));
143 IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
146 static void ixgbe_unmap_and_free_tx_resource(struct ixgbe_adapter *adapter,
147 struct ixgbe_tx_buffer
150 if (tx_buffer_info->dma) {
151 pci_unmap_page(adapter->pdev, tx_buffer_info->dma,
152 tx_buffer_info->length, PCI_DMA_TODEVICE);
153 tx_buffer_info->dma = 0;
155 if (tx_buffer_info->skb) {
156 dev_kfree_skb_any(tx_buffer_info->skb);
157 tx_buffer_info->skb = NULL;
159 /* tx_buffer_info must be completely set up in the transmit path */
162 static inline bool ixgbe_check_tx_hang(struct ixgbe_adapter *adapter,
163 struct ixgbe_ring *tx_ring,
166 struct ixgbe_hw *hw = &adapter->hw;
169 /* Detect a transmit hang in hardware, this serializes the
170 * check with the clearing of time_stamp and movement of eop */
171 head = IXGBE_READ_REG(hw, tx_ring->head);
172 tail = IXGBE_READ_REG(hw, tx_ring->tail);
173 adapter->detect_tx_hung = false;
174 if ((head != tail) &&
175 tx_ring->tx_buffer_info[eop].time_stamp &&
176 time_after(jiffies, tx_ring->tx_buffer_info[eop].time_stamp + HZ) &&
177 !(IXGBE_READ_REG(&adapter->hw, IXGBE_TFCS) & IXGBE_TFCS_TXOFF)) {
178 /* detected Tx unit hang */
179 union ixgbe_adv_tx_desc *tx_desc;
180 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
181 DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
183 " TDH, TDT <%x>, <%x>\n"
184 " next_to_use <%x>\n"
185 " next_to_clean <%x>\n"
186 "tx_buffer_info[next_to_clean]\n"
187 " time_stamp <%lx>\n"
189 tx_ring->queue_index,
191 tx_ring->next_to_use, eop,
192 tx_ring->tx_buffer_info[eop].time_stamp, jiffies);
199 #define IXGBE_MAX_TXD_PWR 14
200 #define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
202 /* Tx Descriptors needed, worst case */
203 #define TXD_USE_COUNT(S) (((S) >> IXGBE_MAX_TXD_PWR) + \
204 (((S) & (IXGBE_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
205 #define DESC_NEEDED (TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD) /* skb->data */ + \
206 MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1) /* for context */
208 #define GET_TX_HEAD_FROM_RING(ring) (\
210 ((union ixgbe_adv_tx_desc *)(ring)->desc + (ring)->count))
211 static void ixgbe_tx_timeout(struct net_device *netdev);
214 * ixgbe_clean_tx_irq - Reclaim resources after transmit completes
215 * @adapter: board private structure
216 * @tx_ring: tx ring to clean
218 static bool ixgbe_clean_tx_irq(struct ixgbe_adapter *adapter,
219 struct ixgbe_ring *tx_ring)
221 union ixgbe_adv_tx_desc *tx_desc;
222 struct ixgbe_tx_buffer *tx_buffer_info;
223 struct net_device *netdev = adapter->netdev;
227 unsigned int count = 0;
228 unsigned int total_bytes = 0, total_packets = 0;
231 head = GET_TX_HEAD_FROM_RING(tx_ring);
232 head = le32_to_cpu(head);
233 i = tx_ring->next_to_clean;
236 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
237 tx_buffer_info = &tx_ring->tx_buffer_info[i];
238 skb = tx_buffer_info->skb;
241 unsigned int segs, bytecount;
243 /* gso_segs is currently only valid for tcp */
244 segs = skb_shinfo(skb)->gso_segs ?: 1;
245 /* multiply data chunks by size of headers */
246 bytecount = ((segs - 1) * skb_headlen(skb)) +
248 total_packets += segs;
249 total_bytes += bytecount;
252 ixgbe_unmap_and_free_tx_resource(adapter,
256 if (i == tx_ring->count)
260 if (count == tx_ring->count)
265 head = GET_TX_HEAD_FROM_RING(tx_ring);
266 head = le32_to_cpu(head);
272 tx_ring->next_to_clean = i;
274 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
275 if (unlikely(count && netif_carrier_ok(netdev) &&
276 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
277 /* Make sure that anybody stopping the queue after this
278 * sees the new next_to_clean.
281 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
282 !test_bit(__IXGBE_DOWN, &adapter->state)) {
283 netif_wake_subqueue(netdev, tx_ring->queue_index);
284 ++adapter->restart_queue;
288 if (adapter->detect_tx_hung) {
289 if (ixgbe_check_tx_hang(adapter, tx_ring, i)) {
290 /* schedule immediate reset if we believe we hung */
292 "tx hang %d detected, resetting adapter\n",
293 adapter->tx_timeout_count + 1);
294 ixgbe_tx_timeout(adapter->netdev);
298 /* re-arm the interrupt */
299 if ((total_packets >= tx_ring->work_limit) ||
300 (count == tx_ring->count))
301 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, tx_ring->v_idx);
303 tx_ring->total_bytes += total_bytes;
304 tx_ring->total_packets += total_packets;
305 tx_ring->stats.bytes += total_bytes;
306 tx_ring->stats.packets += total_packets;
307 adapter->net_stats.tx_bytes += total_bytes;
308 adapter->net_stats.tx_packets += total_packets;
309 return (total_packets ? true : false);
312 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
313 static void ixgbe_update_rx_dca(struct ixgbe_adapter *adapter,
314 struct ixgbe_ring *rx_ring)
318 int q = rx_ring - adapter->rx_ring;
320 if (rx_ring->cpu != cpu) {
321 rxctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_DCA_RXCTRL(q));
322 rxctrl &= ~IXGBE_DCA_RXCTRL_CPUID_MASK;
323 rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
324 rxctrl |= IXGBE_DCA_RXCTRL_DESC_DCA_EN;
325 rxctrl |= IXGBE_DCA_RXCTRL_HEAD_DCA_EN;
326 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_RXCTRL(q), rxctrl);
332 static void ixgbe_update_tx_dca(struct ixgbe_adapter *adapter,
333 struct ixgbe_ring *tx_ring)
337 int q = tx_ring - adapter->tx_ring;
339 if (tx_ring->cpu != cpu) {
340 txctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_DCA_TXCTRL(q));
341 txctrl &= ~IXGBE_DCA_TXCTRL_CPUID_MASK;
342 txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
343 txctrl |= IXGBE_DCA_TXCTRL_DESC_DCA_EN;
344 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_TXCTRL(q), txctrl);
350 static void ixgbe_setup_dca(struct ixgbe_adapter *adapter)
354 if (!(adapter->flags & IXGBE_FLAG_DCA_ENABLED))
357 for (i = 0; i < adapter->num_tx_queues; i++) {
358 adapter->tx_ring[i].cpu = -1;
359 ixgbe_update_tx_dca(adapter, &adapter->tx_ring[i]);
361 for (i = 0; i < adapter->num_rx_queues; i++) {
362 adapter->rx_ring[i].cpu = -1;
363 ixgbe_update_rx_dca(adapter, &adapter->rx_ring[i]);
367 static int __ixgbe_notify_dca(struct device *dev, void *data)
369 struct net_device *netdev = dev_get_drvdata(dev);
370 struct ixgbe_adapter *adapter = netdev_priv(netdev);
371 unsigned long event = *(unsigned long *)data;
374 case DCA_PROVIDER_ADD:
375 /* if we're already enabled, don't do it again */
376 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
378 /* Always use CB2 mode, difference is masked
379 * in the CB driver. */
380 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 2);
381 if (dca_add_requester(dev) == 0) {
382 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
383 ixgbe_setup_dca(adapter);
386 /* Fall Through since DCA is disabled. */
387 case DCA_PROVIDER_REMOVE:
388 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
389 dca_remove_requester(dev);
390 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
391 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
399 #endif /* CONFIG_DCA or CONFIG_DCA_MODULE */
401 * ixgbe_receive_skb - Send a completed packet up the stack
402 * @adapter: board private structure
403 * @skb: packet to send up
404 * @status: hardware indication of status of receive
405 * @rx_ring: rx descriptor ring (for a specific queue) to setup
406 * @rx_desc: rx descriptor
408 static void ixgbe_receive_skb(struct ixgbe_adapter *adapter,
409 struct sk_buff *skb, u8 status,
410 struct ixgbe_ring *ring,
411 union ixgbe_adv_rx_desc *rx_desc)
413 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
414 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
416 if (adapter->netdev->features & NETIF_F_LRO &&
417 skb->ip_summed == CHECKSUM_UNNECESSARY) {
418 if (adapter->vlgrp && is_vlan)
419 lro_vlan_hwaccel_receive_skb(&ring->lro_mgr, skb,
423 lro_receive_skb(&ring->lro_mgr, skb, rx_desc);
424 ring->lro_used = true;
426 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL)) {
427 if (adapter->vlgrp && is_vlan)
428 vlan_hwaccel_receive_skb(skb, adapter->vlgrp, tag);
430 netif_receive_skb(skb);
432 if (adapter->vlgrp && is_vlan)
433 vlan_hwaccel_rx(skb, adapter->vlgrp, tag);
441 * ixgbe_rx_checksum - indicate in skb if hw indicated a good cksum
442 * @adapter: address of board private structure
443 * @status_err: hardware indication of status of receive
444 * @skb: skb currently being received and modified
446 static inline void ixgbe_rx_checksum(struct ixgbe_adapter *adapter,
447 u32 status_err, struct sk_buff *skb)
449 skb->ip_summed = CHECKSUM_NONE;
451 /* Rx csum disabled */
452 if (!(adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED))
455 /* if IP and error */
456 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
457 (status_err & IXGBE_RXDADV_ERR_IPE)) {
458 adapter->hw_csum_rx_error++;
462 if (!(status_err & IXGBE_RXD_STAT_L4CS))
465 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
466 adapter->hw_csum_rx_error++;
470 /* It must be a TCP or UDP packet with a valid checksum */
471 skb->ip_summed = CHECKSUM_UNNECESSARY;
472 adapter->hw_csum_rx_good++;
476 * ixgbe_alloc_rx_buffers - Replace used receive buffers; packet split
477 * @adapter: address of board private structure
479 static void ixgbe_alloc_rx_buffers(struct ixgbe_adapter *adapter,
480 struct ixgbe_ring *rx_ring,
483 struct net_device *netdev = adapter->netdev;
484 struct pci_dev *pdev = adapter->pdev;
485 union ixgbe_adv_rx_desc *rx_desc;
486 struct ixgbe_rx_buffer *bi;
488 unsigned int bufsz = rx_ring->rx_buf_len + NET_IP_ALIGN;
490 i = rx_ring->next_to_use;
491 bi = &rx_ring->rx_buffer_info[i];
493 while (cleaned_count--) {
494 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
497 (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED)) {
498 bi->page = alloc_page(GFP_ATOMIC);
500 adapter->alloc_rx_page_failed++;
503 bi->page_dma = pci_map_page(pdev, bi->page, 0,
509 struct sk_buff *skb = netdev_alloc_skb(netdev, bufsz);
512 adapter->alloc_rx_buff_failed++;
517 * Make buffer alignment 2 beyond a 16 byte boundary
518 * this will result in a 16 byte aligned IP header after
519 * the 14 byte MAC header is removed
521 skb_reserve(skb, NET_IP_ALIGN);
524 bi->dma = pci_map_single(pdev, skb->data, bufsz,
527 /* Refresh the desc even if buffer_addrs didn't change because
528 * each write-back erases this info. */
529 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
530 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
531 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
533 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
537 if (i == rx_ring->count)
539 bi = &rx_ring->rx_buffer_info[i];
543 if (rx_ring->next_to_use != i) {
544 rx_ring->next_to_use = i;
546 i = (rx_ring->count - 1);
549 * Force memory writes to complete before letting h/w
550 * know there are new descriptors to fetch. (Only
551 * applicable for weak-ordered memory model archs,
555 writel(i, adapter->hw.hw_addr + rx_ring->tail);
559 static inline u16 ixgbe_get_hdr_info(union ixgbe_adv_rx_desc *rx_desc)
561 return rx_desc->wb.lower.lo_dword.hs_rss.hdr_info;
564 static inline u16 ixgbe_get_pkt_info(union ixgbe_adv_rx_desc *rx_desc)
566 return rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
569 static bool ixgbe_clean_rx_irq(struct ixgbe_adapter *adapter,
570 struct ixgbe_ring *rx_ring,
571 int *work_done, int work_to_do)
573 struct net_device *netdev = adapter->netdev;
574 struct pci_dev *pdev = adapter->pdev;
575 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
576 struct ixgbe_rx_buffer *rx_buffer_info, *next_buffer;
581 bool cleaned = false;
582 int cleaned_count = 0;
583 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
585 i = rx_ring->next_to_clean;
586 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
587 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
588 rx_buffer_info = &rx_ring->rx_buffer_info[i];
590 while (staterr & IXGBE_RXD_STAT_DD) {
592 if (*work_done >= work_to_do)
596 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
597 hdr_info = le16_to_cpu(ixgbe_get_hdr_info(rx_desc));
598 len = (hdr_info & IXGBE_RXDADV_HDRBUFLEN_MASK) >>
599 IXGBE_RXDADV_HDRBUFLEN_SHIFT;
600 if (hdr_info & IXGBE_RXDADV_SPH)
601 adapter->rx_hdr_split++;
602 if (len > IXGBE_RX_HDR_SIZE)
603 len = IXGBE_RX_HDR_SIZE;
604 upper_len = le16_to_cpu(rx_desc->wb.upper.length);
606 len = le16_to_cpu(rx_desc->wb.upper.length);
610 skb = rx_buffer_info->skb;
611 prefetch(skb->data - NET_IP_ALIGN);
612 rx_buffer_info->skb = NULL;
614 if (len && !skb_shinfo(skb)->nr_frags) {
615 pci_unmap_single(pdev, rx_buffer_info->dma,
616 rx_ring->rx_buf_len + NET_IP_ALIGN,
622 pci_unmap_page(pdev, rx_buffer_info->page_dma,
623 PAGE_SIZE, PCI_DMA_FROMDEVICE);
624 rx_buffer_info->page_dma = 0;
625 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
626 rx_buffer_info->page, 0, upper_len);
627 rx_buffer_info->page = NULL;
629 skb->len += upper_len;
630 skb->data_len += upper_len;
631 skb->truesize += upper_len;
635 if (i == rx_ring->count)
637 next_buffer = &rx_ring->rx_buffer_info[i];
639 next_rxd = IXGBE_RX_DESC_ADV(*rx_ring, i);
643 if (staterr & IXGBE_RXD_STAT_EOP) {
644 rx_ring->stats.packets++;
645 rx_ring->stats.bytes += skb->len;
647 rx_buffer_info->skb = next_buffer->skb;
648 rx_buffer_info->dma = next_buffer->dma;
649 next_buffer->skb = skb;
650 adapter->non_eop_descs++;
654 if (staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK) {
655 dev_kfree_skb_irq(skb);
659 ixgbe_rx_checksum(adapter, staterr, skb);
661 /* probably a little skewed due to removing CRC */
662 total_rx_bytes += skb->len;
665 skb->protocol = eth_type_trans(skb, netdev);
666 ixgbe_receive_skb(adapter, skb, staterr, rx_ring, rx_desc);
667 netdev->last_rx = jiffies;
670 rx_desc->wb.upper.status_error = 0;
672 /* return some buffers to hardware, one at a time is too slow */
673 if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
674 ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
678 /* use prefetched values */
680 rx_buffer_info = next_buffer;
682 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
685 if (rx_ring->lro_used) {
686 lro_flush_all(&rx_ring->lro_mgr);
687 rx_ring->lro_used = false;
690 rx_ring->next_to_clean = i;
691 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
694 ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
696 rx_ring->total_packets += total_rx_packets;
697 rx_ring->total_bytes += total_rx_bytes;
698 adapter->net_stats.rx_bytes += total_rx_bytes;
699 adapter->net_stats.rx_packets += total_rx_packets;
704 static int ixgbe_clean_rxonly(struct napi_struct *, int);
706 * ixgbe_configure_msix - Configure MSI-X hardware
707 * @adapter: board private structure
709 * ixgbe_configure_msix sets up the hardware to properly generate MSI-X
712 static void ixgbe_configure_msix(struct ixgbe_adapter *adapter)
714 struct ixgbe_q_vector *q_vector;
715 int i, j, q_vectors, v_idx, r_idx;
718 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
720 /* Populate the IVAR table and set the ITR values to the
721 * corresponding register.
723 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
724 q_vector = &adapter->q_vector[v_idx];
725 /* XXX for_each_bit(...) */
726 r_idx = find_first_bit(q_vector->rxr_idx,
727 adapter->num_rx_queues);
729 for (i = 0; i < q_vector->rxr_count; i++) {
730 j = adapter->rx_ring[r_idx].reg_idx;
731 ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(j), v_idx);
732 r_idx = find_next_bit(q_vector->rxr_idx,
733 adapter->num_rx_queues,
736 r_idx = find_first_bit(q_vector->txr_idx,
737 adapter->num_tx_queues);
739 for (i = 0; i < q_vector->txr_count; i++) {
740 j = adapter->tx_ring[r_idx].reg_idx;
741 ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(j), v_idx);
742 r_idx = find_next_bit(q_vector->txr_idx,
743 adapter->num_tx_queues,
747 /* if this is a tx only vector use half the irq (tx) rate */
748 if (q_vector->txr_count && !q_vector->rxr_count)
749 q_vector->eitr = adapter->tx_eitr;
751 /* rx only or mixed */
752 q_vector->eitr = adapter->rx_eitr;
754 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx),
755 EITR_INTS_PER_SEC_TO_REG(q_vector->eitr));
758 ixgbe_set_ivar(adapter, IXGBE_IVAR_OTHER_CAUSES_INDEX, v_idx);
759 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx), 1950);
761 /* set up to autoclear timer, and the vectors */
762 mask = IXGBE_EIMS_ENABLE_MASK;
763 mask &= ~(IXGBE_EIMS_OTHER | IXGBE_EIMS_LSC);
764 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, mask);
771 latency_invalid = 255
775 * ixgbe_update_itr - update the dynamic ITR value based on statistics
776 * @adapter: pointer to adapter
777 * @eitr: eitr setting (ints per sec) to give last timeslice
778 * @itr_setting: current throttle rate in ints/second
779 * @packets: the number of packets during this measurement interval
780 * @bytes: the number of bytes during this measurement interval
782 * Stores a new ITR value based on packets and byte
783 * counts during the last interrupt. The advantage of per interrupt
784 * computation is faster updates and more accurate ITR for the current
785 * traffic pattern. Constants in this function were computed
786 * based on theoretical maximum wire speed and thresholds were set based
787 * on testing data as well as attempting to minimize response time
788 * while increasing bulk throughput.
789 * this functionality is controlled by the InterruptThrottleRate module
790 * parameter (see ixgbe_param.c)
792 static u8 ixgbe_update_itr(struct ixgbe_adapter *adapter,
793 u32 eitr, u8 itr_setting,
794 int packets, int bytes)
796 unsigned int retval = itr_setting;
801 goto update_itr_done;
804 /* simple throttlerate management
805 * 0-20MB/s lowest (100000 ints/s)
806 * 20-100MB/s low (20000 ints/s)
807 * 100-1249MB/s bulk (8000 ints/s)
809 /* what was last interrupt timeslice? */
810 timepassed_us = 1000000/eitr;
811 bytes_perint = bytes / timepassed_us; /* bytes/usec */
813 switch (itr_setting) {
815 if (bytes_perint > adapter->eitr_low)
816 retval = low_latency;
819 if (bytes_perint > adapter->eitr_high)
820 retval = bulk_latency;
821 else if (bytes_perint <= adapter->eitr_low)
822 retval = lowest_latency;
825 if (bytes_perint <= adapter->eitr_high)
826 retval = low_latency;
834 static void ixgbe_set_itr_msix(struct ixgbe_q_vector *q_vector)
836 struct ixgbe_adapter *adapter = q_vector->adapter;
837 struct ixgbe_hw *hw = &adapter->hw;
839 u8 current_itr, ret_itr;
840 int i, r_idx, v_idx = ((void *)q_vector - (void *)(adapter->q_vector)) /
841 sizeof(struct ixgbe_q_vector);
842 struct ixgbe_ring *rx_ring, *tx_ring;
844 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
845 for (i = 0; i < q_vector->txr_count; i++) {
846 tx_ring = &(adapter->tx_ring[r_idx]);
847 ret_itr = ixgbe_update_itr(adapter, q_vector->eitr,
849 tx_ring->total_packets,
850 tx_ring->total_bytes);
851 /* if the result for this queue would decrease interrupt
852 * rate for this vector then use that result */
853 q_vector->tx_eitr = ((q_vector->tx_eitr > ret_itr) ?
854 q_vector->tx_eitr - 1 : ret_itr);
855 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
859 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
860 for (i = 0; i < q_vector->rxr_count; i++) {
861 rx_ring = &(adapter->rx_ring[r_idx]);
862 ret_itr = ixgbe_update_itr(adapter, q_vector->eitr,
864 rx_ring->total_packets,
865 rx_ring->total_bytes);
866 /* if the result for this queue would decrease interrupt
867 * rate for this vector then use that result */
868 q_vector->rx_eitr = ((q_vector->rx_eitr > ret_itr) ?
869 q_vector->rx_eitr - 1 : ret_itr);
870 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
874 current_itr = max(q_vector->rx_eitr, q_vector->tx_eitr);
876 switch (current_itr) {
877 /* counts and packets in update_itr are dependent on these numbers */
882 new_itr = 20000; /* aka hwitr = ~200 */
890 if (new_itr != q_vector->eitr) {
892 /* do an exponential smoothing */
893 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
894 q_vector->eitr = new_itr;
895 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
896 /* must write high and low 16 bits to reset counter */
897 DPRINTK(TX_ERR, DEBUG, "writing eitr(%d): %08X\n", v_idx,
899 IXGBE_WRITE_REG(hw, IXGBE_EITR(v_idx), itr_reg | (itr_reg)<<16);
906 static void ixgbe_check_lsc(struct ixgbe_adapter *adapter)
908 struct ixgbe_hw *hw = &adapter->hw;
911 adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
912 adapter->link_check_timeout = jiffies;
913 if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
914 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
915 schedule_work(&adapter->watchdog_task);
919 static irqreturn_t ixgbe_msix_lsc(int irq, void *data)
921 struct net_device *netdev = data;
922 struct ixgbe_adapter *adapter = netdev_priv(netdev);
923 struct ixgbe_hw *hw = &adapter->hw;
924 u32 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
926 if (eicr & IXGBE_EICR_LSC)
927 ixgbe_check_lsc(adapter);
929 if (!test_bit(__IXGBE_DOWN, &adapter->state))
930 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
935 static irqreturn_t ixgbe_msix_clean_tx(int irq, void *data)
937 struct ixgbe_q_vector *q_vector = data;
938 struct ixgbe_adapter *adapter = q_vector->adapter;
939 struct ixgbe_ring *tx_ring;
942 if (!q_vector->txr_count)
945 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
946 for (i = 0; i < q_vector->txr_count; i++) {
947 tx_ring = &(adapter->tx_ring[r_idx]);
948 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
949 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
950 ixgbe_update_tx_dca(adapter, tx_ring);
952 tx_ring->total_bytes = 0;
953 tx_ring->total_packets = 0;
954 ixgbe_clean_tx_irq(adapter, tx_ring);
955 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
963 * ixgbe_msix_clean_rx - single unshared vector rx clean (all queues)
965 * @data: pointer to our q_vector struct for this interrupt vector
967 static irqreturn_t ixgbe_msix_clean_rx(int irq, void *data)
969 struct ixgbe_q_vector *q_vector = data;
970 struct ixgbe_adapter *adapter = q_vector->adapter;
971 struct ixgbe_ring *rx_ring;
974 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
975 if (!q_vector->rxr_count)
978 rx_ring = &(adapter->rx_ring[r_idx]);
979 /* disable interrupts on this vector only */
980 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, rx_ring->v_idx);
981 rx_ring->total_bytes = 0;
982 rx_ring->total_packets = 0;
983 netif_rx_schedule(adapter->netdev, &q_vector->napi);
988 static irqreturn_t ixgbe_msix_clean_many(int irq, void *data)
990 ixgbe_msix_clean_rx(irq, data);
991 ixgbe_msix_clean_tx(irq, data);
997 * ixgbe_clean_rxonly - msix (aka one shot) rx clean routine
998 * @napi: napi struct with our devices info in it
999 * @budget: amount of work driver is allowed to do this pass, in packets
1002 static int ixgbe_clean_rxonly(struct napi_struct *napi, int budget)
1004 struct ixgbe_q_vector *q_vector =
1005 container_of(napi, struct ixgbe_q_vector, napi);
1006 struct ixgbe_adapter *adapter = q_vector->adapter;
1007 struct ixgbe_ring *rx_ring;
1011 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1012 rx_ring = &(adapter->rx_ring[r_idx]);
1013 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
1014 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1015 ixgbe_update_rx_dca(adapter, rx_ring);
1018 ixgbe_clean_rx_irq(adapter, rx_ring, &work_done, budget);
1020 /* If all Rx work done, exit the polling mode */
1021 if (work_done < budget) {
1022 netif_rx_complete(adapter->netdev, napi);
1023 if (adapter->rx_eitr < IXGBE_MIN_ITR_USECS)
1024 ixgbe_set_itr_msix(q_vector);
1025 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1026 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, rx_ring->v_idx);
1032 static inline void map_vector_to_rxq(struct ixgbe_adapter *a, int v_idx,
1035 a->q_vector[v_idx].adapter = a;
1036 set_bit(r_idx, a->q_vector[v_idx].rxr_idx);
1037 a->q_vector[v_idx].rxr_count++;
1038 a->rx_ring[r_idx].v_idx = 1 << v_idx;
1041 static inline void map_vector_to_txq(struct ixgbe_adapter *a, int v_idx,
1044 a->q_vector[v_idx].adapter = a;
1045 set_bit(r_idx, a->q_vector[v_idx].txr_idx);
1046 a->q_vector[v_idx].txr_count++;
1047 a->tx_ring[r_idx].v_idx = 1 << v_idx;
1051 * ixgbe_map_rings_to_vectors - Maps descriptor rings to vectors
1052 * @adapter: board private structure to initialize
1053 * @vectors: allotted vector count for descriptor rings
1055 * This function maps descriptor rings to the queue-specific vectors
1056 * we were allotted through the MSI-X enabling code. Ideally, we'd have
1057 * one vector per ring/queue, but on a constrained vector budget, we
1058 * group the rings as "efficiently" as possible. You would add new
1059 * mapping configurations in here.
1061 static int ixgbe_map_rings_to_vectors(struct ixgbe_adapter *adapter,
1065 int rxr_idx = 0, txr_idx = 0;
1066 int rxr_remaining = adapter->num_rx_queues;
1067 int txr_remaining = adapter->num_tx_queues;
1072 /* No mapping required if MSI-X is disabled. */
1073 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1077 * The ideal configuration...
1078 * We have enough vectors to map one per queue.
1080 if (vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
1081 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
1082 map_vector_to_rxq(adapter, v_start, rxr_idx);
1084 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
1085 map_vector_to_txq(adapter, v_start, txr_idx);
1091 * If we don't have enough vectors for a 1-to-1
1092 * mapping, we'll have to group them so there are
1093 * multiple queues per vector.
1095 /* Re-adjusting *qpv takes care of the remainder. */
1096 for (i = v_start; i < vectors; i++) {
1097 rqpv = DIV_ROUND_UP(rxr_remaining, vectors - i);
1098 for (j = 0; j < rqpv; j++) {
1099 map_vector_to_rxq(adapter, i, rxr_idx);
1104 for (i = v_start; i < vectors; i++) {
1105 tqpv = DIV_ROUND_UP(txr_remaining, vectors - i);
1106 for (j = 0; j < tqpv; j++) {
1107 map_vector_to_txq(adapter, i, txr_idx);
1118 * ixgbe_request_msix_irqs - Initialize MSI-X interrupts
1119 * @adapter: board private structure
1121 * ixgbe_request_msix_irqs allocates MSI-X vectors and requests
1122 * interrupts from the kernel.
1124 static int ixgbe_request_msix_irqs(struct ixgbe_adapter *adapter)
1126 struct net_device *netdev = adapter->netdev;
1127 irqreturn_t (*handler)(int, void *);
1128 int i, vector, q_vectors, err;
1130 /* Decrement for Other and TCP Timer vectors */
1131 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1133 /* Map the Tx/Rx rings to the vectors we were allotted. */
1134 err = ixgbe_map_rings_to_vectors(adapter, q_vectors);
1138 #define SET_HANDLER(_v) ((!(_v)->rxr_count) ? &ixgbe_msix_clean_tx : \
1139 (!(_v)->txr_count) ? &ixgbe_msix_clean_rx : \
1140 &ixgbe_msix_clean_many)
1141 for (vector = 0; vector < q_vectors; vector++) {
1142 handler = SET_HANDLER(&adapter->q_vector[vector]);
1143 sprintf(adapter->name[vector], "%s:v%d-%s",
1144 netdev->name, vector,
1145 (handler == &ixgbe_msix_clean_rx) ? "Rx" :
1146 ((handler == &ixgbe_msix_clean_tx) ? "Tx" : "TxRx"));
1147 err = request_irq(adapter->msix_entries[vector].vector,
1148 handler, 0, adapter->name[vector],
1149 &(adapter->q_vector[vector]));
1152 "request_irq failed for MSIX interrupt "
1153 "Error: %d\n", err);
1154 goto free_queue_irqs;
1158 sprintf(adapter->name[vector], "%s:lsc", netdev->name);
1159 err = request_irq(adapter->msix_entries[vector].vector,
1160 &ixgbe_msix_lsc, 0, adapter->name[vector], netdev);
1163 "request_irq for msix_lsc failed: %d\n", err);
1164 goto free_queue_irqs;
1170 for (i = vector - 1; i >= 0; i--)
1171 free_irq(adapter->msix_entries[--vector].vector,
1172 &(adapter->q_vector[i]));
1173 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
1174 pci_disable_msix(adapter->pdev);
1175 kfree(adapter->msix_entries);
1176 adapter->msix_entries = NULL;
1181 static void ixgbe_set_itr(struct ixgbe_adapter *adapter)
1183 struct ixgbe_hw *hw = &adapter->hw;
1184 struct ixgbe_q_vector *q_vector = adapter->q_vector;
1186 u32 new_itr = q_vector->eitr;
1187 struct ixgbe_ring *rx_ring = &adapter->rx_ring[0];
1188 struct ixgbe_ring *tx_ring = &adapter->tx_ring[0];
1190 q_vector->tx_eitr = ixgbe_update_itr(adapter, new_itr,
1192 tx_ring->total_packets,
1193 tx_ring->total_bytes);
1194 q_vector->rx_eitr = ixgbe_update_itr(adapter, new_itr,
1196 rx_ring->total_packets,
1197 rx_ring->total_bytes);
1199 current_itr = max(q_vector->rx_eitr, q_vector->tx_eitr);
1201 switch (current_itr) {
1202 /* counts and packets in update_itr are dependent on these numbers */
1203 case lowest_latency:
1207 new_itr = 20000; /* aka hwitr = ~200 */
1216 if (new_itr != q_vector->eitr) {
1218 /* do an exponential smoothing */
1219 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
1220 q_vector->eitr = new_itr;
1221 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
1222 /* must write high and low 16 bits to reset counter */
1223 IXGBE_WRITE_REG(hw, IXGBE_EITR(0), itr_reg | (itr_reg)<<16);
1229 static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter);
1232 * ixgbe_intr - legacy mode Interrupt Handler
1233 * @irq: interrupt number
1234 * @data: pointer to a network interface device structure
1235 * @pt_regs: CPU registers structure
1237 static irqreturn_t ixgbe_intr(int irq, void *data)
1239 struct net_device *netdev = data;
1240 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1241 struct ixgbe_hw *hw = &adapter->hw;
1245 /* for NAPI, using EIAM to auto-mask tx/rx interrupt bits on read
1246 * therefore no explict interrupt disable is necessary */
1247 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
1249 /* shared interrupt alert!
1250 * make sure interrupts are enabled because the read will
1251 * have disabled interrupts due to EIAM */
1252 ixgbe_irq_enable(adapter);
1253 return IRQ_NONE; /* Not our interrupt */
1256 if (eicr & IXGBE_EICR_LSC)
1257 ixgbe_check_lsc(adapter);
1259 if (netif_rx_schedule_prep(netdev, &adapter->q_vector[0].napi)) {
1260 adapter->tx_ring[0].total_packets = 0;
1261 adapter->tx_ring[0].total_bytes = 0;
1262 adapter->rx_ring[0].total_packets = 0;
1263 adapter->rx_ring[0].total_bytes = 0;
1264 /* would disable interrupts here but EIAM disabled it */
1265 __netif_rx_schedule(netdev, &adapter->q_vector[0].napi);
1271 static inline void ixgbe_reset_q_vectors(struct ixgbe_adapter *adapter)
1273 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1275 for (i = 0; i < q_vectors; i++) {
1276 struct ixgbe_q_vector *q_vector = &adapter->q_vector[i];
1277 bitmap_zero(q_vector->rxr_idx, MAX_RX_QUEUES);
1278 bitmap_zero(q_vector->txr_idx, MAX_TX_QUEUES);
1279 q_vector->rxr_count = 0;
1280 q_vector->txr_count = 0;
1285 * ixgbe_request_irq - initialize interrupts
1286 * @adapter: board private structure
1288 * Attempts to configure interrupts using the best available
1289 * capabilities of the hardware and kernel.
1291 static int ixgbe_request_irq(struct ixgbe_adapter *adapter)
1293 struct net_device *netdev = adapter->netdev;
1296 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1297 err = ixgbe_request_msix_irqs(adapter);
1298 } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
1299 err = request_irq(adapter->pdev->irq, &ixgbe_intr, 0,
1300 netdev->name, netdev);
1302 err = request_irq(adapter->pdev->irq, &ixgbe_intr, IRQF_SHARED,
1303 netdev->name, netdev);
1307 DPRINTK(PROBE, ERR, "request_irq failed, Error %d\n", err);
1312 static void ixgbe_free_irq(struct ixgbe_adapter *adapter)
1314 struct net_device *netdev = adapter->netdev;
1316 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1319 q_vectors = adapter->num_msix_vectors;
1322 free_irq(adapter->msix_entries[i].vector, netdev);
1325 for (; i >= 0; i--) {
1326 free_irq(adapter->msix_entries[i].vector,
1327 &(adapter->q_vector[i]));
1330 ixgbe_reset_q_vectors(adapter);
1332 free_irq(adapter->pdev->irq, netdev);
1337 * ixgbe_irq_disable - Mask off interrupt generation on the NIC
1338 * @adapter: board private structure
1340 static inline void ixgbe_irq_disable(struct ixgbe_adapter *adapter)
1342 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
1343 IXGBE_WRITE_FLUSH(&adapter->hw);
1344 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1346 for (i = 0; i < adapter->num_msix_vectors; i++)
1347 synchronize_irq(adapter->msix_entries[i].vector);
1349 synchronize_irq(adapter->pdev->irq);
1354 * ixgbe_irq_enable - Enable default interrupt generation settings
1355 * @adapter: board private structure
1357 static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter)
1360 mask = IXGBE_EIMS_ENABLE_MASK;
1361 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, mask);
1362 IXGBE_WRITE_FLUSH(&adapter->hw);
1366 * ixgbe_configure_msi_and_legacy - Initialize PIN (INTA...) and MSI interrupts
1369 static void ixgbe_configure_msi_and_legacy(struct ixgbe_adapter *adapter)
1371 struct ixgbe_hw *hw = &adapter->hw;
1373 IXGBE_WRITE_REG(hw, IXGBE_EITR(0),
1374 EITR_INTS_PER_SEC_TO_REG(adapter->rx_eitr));
1376 ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(0), 0);
1377 ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(0), 0);
1379 map_vector_to_rxq(adapter, 0, 0);
1380 map_vector_to_txq(adapter, 0, 0);
1382 DPRINTK(HW, INFO, "Legacy interrupt IVAR setup done\n");
1386 * ixgbe_configure_tx - Configure 8259x Transmit Unit after Reset
1387 * @adapter: board private structure
1389 * Configure the Tx unit of the MAC after a reset.
1391 static void ixgbe_configure_tx(struct ixgbe_adapter *adapter)
1394 struct ixgbe_hw *hw = &adapter->hw;
1395 u32 i, j, tdlen, txctrl;
1397 /* Setup the HW Tx Head and Tail descriptor pointers */
1398 for (i = 0; i < adapter->num_tx_queues; i++) {
1399 struct ixgbe_ring *ring = &adapter->tx_ring[i];
1402 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1403 IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
1404 (tdba & DMA_32BIT_MASK));
1405 IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
1407 (ring->count * sizeof(union ixgbe_adv_tx_desc));
1408 tdwba |= IXGBE_TDWBAL_HEAD_WB_ENABLE;
1409 IXGBE_WRITE_REG(hw, IXGBE_TDWBAL(j), tdwba & DMA_32BIT_MASK);
1410 IXGBE_WRITE_REG(hw, IXGBE_TDWBAH(j), (tdwba >> 32));
1411 IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j), tdlen);
1412 IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
1413 IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
1414 adapter->tx_ring[i].head = IXGBE_TDH(j);
1415 adapter->tx_ring[i].tail = IXGBE_TDT(j);
1416 /* Disable Tx Head Writeback RO bit, since this hoses
1417 * bookkeeping if things aren't delivered in order.
1419 txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
1420 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1421 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
1425 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1427 static void ixgbe_configure_srrctl(struct ixgbe_adapter *adapter, int index)
1429 struct ixgbe_ring *rx_ring;
1434 /* program one srrctl register per VMDq index */
1435 if (adapter->flags & IXGBE_FLAG_VMDQ_ENABLED) {
1437 mask = (unsigned long) adapter->ring_feature[RING_F_RSS].mask;
1438 len = sizeof(adapter->ring_feature[RING_F_VMDQ].mask) * 8;
1439 shift = find_first_bit(&mask, len);
1440 queue0 = index & mask;
1441 index = (index & mask) >> shift;
1442 /* program one srrctl per RSS queue since RDRXCTL.MVMEN is enabled */
1444 mask = (unsigned long) adapter->ring_feature[RING_F_RSS].mask;
1445 queue0 = index & mask;
1446 index = index & mask;
1449 rx_ring = &adapter->rx_ring[queue0];
1451 srrctl = IXGBE_READ_REG(&adapter->hw, IXGBE_SRRCTL(index));
1453 srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
1454 srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
1456 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1457 srrctl |= IXGBE_RXBUFFER_2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1458 srrctl |= IXGBE_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
1459 srrctl |= ((IXGBE_RX_HDR_SIZE <<
1460 IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
1461 IXGBE_SRRCTL_BSIZEHDR_MASK);
1463 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1465 if (rx_ring->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
1466 srrctl |= IXGBE_RXBUFFER_2048 >>
1467 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1469 srrctl |= rx_ring->rx_buf_len >>
1470 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1472 IXGBE_WRITE_REG(&adapter->hw, IXGBE_SRRCTL(index), srrctl);
1476 * ixgbe_get_skb_hdr - helper function for LRO header processing
1477 * @skb: pointer to sk_buff to be added to LRO packet
1478 * @iphdr: pointer to tcp header structure
1479 * @tcph: pointer to tcp header structure
1480 * @hdr_flags: pointer to header flags
1481 * @priv: private data
1483 static int ixgbe_get_skb_hdr(struct sk_buff *skb, void **iphdr, void **tcph,
1484 u64 *hdr_flags, void *priv)
1486 union ixgbe_adv_rx_desc *rx_desc = priv;
1488 /* Verify that this is a valid IPv4 TCP packet */
1489 if (!((ixgbe_get_pkt_info(rx_desc) & IXGBE_RXDADV_PKTTYPE_IPV4) &&
1490 (ixgbe_get_pkt_info(rx_desc) & IXGBE_RXDADV_PKTTYPE_TCP)))
1493 /* Set network headers */
1494 skb_reset_network_header(skb);
1495 skb_set_transport_header(skb, ip_hdrlen(skb));
1496 *iphdr = ip_hdr(skb);
1497 *tcph = tcp_hdr(skb);
1498 *hdr_flags = LRO_IPV4 | LRO_TCP;
1502 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1503 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
1506 * ixgbe_configure_rx - Configure 8259x Receive Unit after Reset
1507 * @adapter: board private structure
1509 * Configure the Rx unit of the MAC after a reset.
1511 static void ixgbe_configure_rx(struct ixgbe_adapter *adapter)
1514 struct ixgbe_hw *hw = &adapter->hw;
1515 struct net_device *netdev = adapter->netdev;
1516 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1518 u32 rdlen, rxctrl, rxcsum;
1519 static const u32 seed[10] = { 0xE291D73D, 0x1805EC6C, 0x2A94B30D,
1520 0xA54F2BEC, 0xEA49AF7C, 0xE214AD3D, 0xB855AABE,
1521 0x6A3E67EA, 0x14364D17, 0x3BED200D};
1528 /* Decide whether to use packet split mode or not */
1529 if (netdev->mtu > ETH_DATA_LEN)
1530 adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
1532 adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
1534 /* Set the RX buffer length according to the mode */
1535 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1536 rx_buf_len = IXGBE_RX_HDR_SIZE;
1538 if (netdev->mtu <= ETH_DATA_LEN)
1539 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1541 rx_buf_len = ALIGN(max_frame, 1024);
1544 fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
1545 fctrl |= IXGBE_FCTRL_BAM;
1546 fctrl |= IXGBE_FCTRL_DPF; /* discard pause frames when FC enabled */
1547 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
1549 hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
1550 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1551 hlreg0 &= ~IXGBE_HLREG0_JUMBOEN;
1553 hlreg0 |= IXGBE_HLREG0_JUMBOEN;
1554 IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
1556 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1558 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1559 /* disable receives while setting up the descriptors */
1560 rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1561 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
1563 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1564 * the Base and Length of the Rx Descriptor Ring */
1565 for (i = 0; i < adapter->num_rx_queues; i++) {
1566 rdba = adapter->rx_ring[i].dma;
1567 j = adapter->rx_ring[i].reg_idx;
1568 IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j), (rdba & DMA_32BIT_MASK));
1569 IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
1570 IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j), rdlen);
1571 IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
1572 IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
1573 adapter->rx_ring[i].head = IXGBE_RDH(j);
1574 adapter->rx_ring[i].tail = IXGBE_RDT(j);
1575 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1576 /* Intitial LRO Settings */
1577 adapter->rx_ring[i].lro_mgr.max_aggr = IXGBE_MAX_LRO_AGGREGATE;
1578 adapter->rx_ring[i].lro_mgr.max_desc = IXGBE_MAX_LRO_DESCRIPTORS;
1579 adapter->rx_ring[i].lro_mgr.get_skb_header = ixgbe_get_skb_hdr;
1580 adapter->rx_ring[i].lro_mgr.features = LRO_F_EXTRACT_VLAN_ID;
1581 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
1582 adapter->rx_ring[i].lro_mgr.features |= LRO_F_NAPI;
1583 adapter->rx_ring[i].lro_mgr.dev = adapter->netdev;
1584 adapter->rx_ring[i].lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1585 adapter->rx_ring[i].lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1587 ixgbe_configure_srrctl(adapter, j);
1591 * For VMDq support of different descriptor types or
1592 * buffer sizes through the use of multiple SRRCTL
1593 * registers, RDRXCTL.MVMEN must be set to 1
1595 * also, the manual doesn't mention it clearly but DCA hints
1596 * will only use queue 0's tags unless this bit is set. Side
1597 * effects of setting this bit are only that SRRCTL must be
1598 * fully programmed [0..15]
1600 rdrxctl = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
1601 rdrxctl |= IXGBE_RDRXCTL_MVMEN;
1602 IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rdrxctl);
1605 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
1606 /* Fill out redirection table */
1607 for (i = 0, j = 0; i < 128; i++, j++) {
1608 if (j == adapter->ring_feature[RING_F_RSS].indices)
1610 /* reta = 4-byte sliding window of
1611 * 0x00..(indices-1)(indices-1)00..etc. */
1612 reta = (reta << 8) | (j * 0x11);
1614 IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
1617 /* Fill out hash function seeds */
1618 for (i = 0; i < 10; i++)
1619 IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), seed[i]);
1621 mrqc = IXGBE_MRQC_RSSEN
1622 /* Perform hash on these packet types */
1623 | IXGBE_MRQC_RSS_FIELD_IPV4
1624 | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
1625 | IXGBE_MRQC_RSS_FIELD_IPV4_UDP
1626 | IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP
1627 | IXGBE_MRQC_RSS_FIELD_IPV6_EX
1628 | IXGBE_MRQC_RSS_FIELD_IPV6
1629 | IXGBE_MRQC_RSS_FIELD_IPV6_TCP
1630 | IXGBE_MRQC_RSS_FIELD_IPV6_UDP
1631 | IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
1632 IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
1635 rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
1637 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED ||
1638 adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED) {
1639 /* Disable indicating checksum in descriptor, enables
1641 rxcsum |= IXGBE_RXCSUM_PCSD;
1643 if (!(rxcsum & IXGBE_RXCSUM_PCSD)) {
1644 /* Enable IPv4 payload checksum for UDP fragments
1645 * if PCSD is not set */
1646 rxcsum |= IXGBE_RXCSUM_IPPCSE;
1649 IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
1652 static void ixgbe_vlan_rx_register(struct net_device *netdev,
1653 struct vlan_group *grp)
1655 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1658 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1659 ixgbe_irq_disable(adapter);
1660 adapter->vlgrp = grp;
1663 /* enable VLAN tag insert/strip */
1664 ctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_VLNCTRL);
1665 ctrl |= IXGBE_VLNCTRL_VME;
1666 ctrl &= ~IXGBE_VLNCTRL_CFIEN;
1667 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VLNCTRL, ctrl);
1670 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1671 ixgbe_irq_enable(adapter);
1674 static void ixgbe_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1676 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1678 /* add VID to filter table */
1679 ixgbe_set_vfta(&adapter->hw, vid, 0, true);
1682 static void ixgbe_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1684 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1686 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1687 ixgbe_irq_disable(adapter);
1689 vlan_group_set_device(adapter->vlgrp, vid, NULL);
1691 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1692 ixgbe_irq_enable(adapter);
1694 /* remove VID from filter table */
1695 ixgbe_set_vfta(&adapter->hw, vid, 0, false);
1698 static void ixgbe_restore_vlan(struct ixgbe_adapter *adapter)
1700 ixgbe_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1702 if (adapter->vlgrp) {
1704 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1705 if (!vlan_group_get_device(adapter->vlgrp, vid))
1707 ixgbe_vlan_rx_add_vid(adapter->netdev, vid);
1712 static u8 *ixgbe_addr_list_itr(struct ixgbe_hw *hw, u8 **mc_addr_ptr, u32 *vmdq)
1714 struct dev_mc_list *mc_ptr;
1715 u8 *addr = *mc_addr_ptr;
1718 mc_ptr = container_of(addr, struct dev_mc_list, dmi_addr[0]);
1720 *mc_addr_ptr = mc_ptr->next->dmi_addr;
1722 *mc_addr_ptr = NULL;
1728 * ixgbe_set_rx_mode - Unicast, Multicast and Promiscuous mode set
1729 * @netdev: network interface device structure
1731 * The set_rx_method entry point is called whenever the unicast/multicast
1732 * address list or the network interface flags are updated. This routine is
1733 * responsible for configuring the hardware for proper unicast, multicast and
1736 static void ixgbe_set_rx_mode(struct net_device *netdev)
1738 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1739 struct ixgbe_hw *hw = &adapter->hw;
1741 u8 *addr_list = NULL;
1744 /* Check for Promiscuous and All Multicast modes */
1746 fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1747 vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1749 if (netdev->flags & IFF_PROMISC) {
1750 hw->addr_ctrl.user_set_promisc = 1;
1751 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1752 vlnctrl &= ~IXGBE_VLNCTRL_VFE;
1754 if (netdev->flags & IFF_ALLMULTI) {
1755 fctrl |= IXGBE_FCTRL_MPE;
1756 fctrl &= ~IXGBE_FCTRL_UPE;
1758 fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1760 vlnctrl |= IXGBE_VLNCTRL_VFE;
1761 hw->addr_ctrl.user_set_promisc = 0;
1764 IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1765 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
1767 /* reprogram secondary unicast list */
1768 addr_count = netdev->uc_count;
1770 addr_list = netdev->uc_list->dmi_addr;
1771 ixgbe_update_uc_addr_list(hw, addr_list, addr_count,
1772 ixgbe_addr_list_itr);
1774 /* reprogram multicast list */
1775 addr_count = netdev->mc_count;
1777 addr_list = netdev->mc_list->dmi_addr;
1778 ixgbe_update_mc_addr_list(hw, addr_list, addr_count,
1779 ixgbe_addr_list_itr);
1782 static void ixgbe_napi_enable_all(struct ixgbe_adapter *adapter)
1785 struct ixgbe_q_vector *q_vector;
1786 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1788 /* legacy and MSI only use one vector */
1789 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1792 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1793 q_vector = &adapter->q_vector[q_idx];
1794 if (!q_vector->rxr_count)
1796 napi_enable(&q_vector->napi);
1800 static void ixgbe_napi_disable_all(struct ixgbe_adapter *adapter)
1803 struct ixgbe_q_vector *q_vector;
1804 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1806 /* legacy and MSI only use one vector */
1807 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1810 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1811 q_vector = &adapter->q_vector[q_idx];
1812 if (!q_vector->rxr_count)
1814 napi_disable(&q_vector->napi);
1818 static void ixgbe_configure(struct ixgbe_adapter *adapter)
1820 struct net_device *netdev = adapter->netdev;
1823 ixgbe_set_rx_mode(netdev);
1825 ixgbe_restore_vlan(adapter);
1827 ixgbe_configure_tx(adapter);
1828 ixgbe_configure_rx(adapter);
1829 for (i = 0; i < adapter->num_rx_queues; i++)
1830 ixgbe_alloc_rx_buffers(adapter, &adapter->rx_ring[i],
1831 (adapter->rx_ring[i].count - 1));
1834 static int ixgbe_up_complete(struct ixgbe_adapter *adapter)
1836 struct net_device *netdev = adapter->netdev;
1837 struct ixgbe_hw *hw = &adapter->hw;
1839 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1840 u32 txdctl, rxdctl, mhadd;
1843 ixgbe_get_hw_control(adapter);
1845 if ((adapter->flags & IXGBE_FLAG_MSIX_ENABLED) ||
1846 (adapter->flags & IXGBE_FLAG_MSI_ENABLED)) {
1847 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1848 gpie = (IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_EIAME |
1849 IXGBE_GPIE_PBA_SUPPORT | IXGBE_GPIE_OCD);
1854 /* XXX: to interrupt immediately for EICS writes, enable this */
1855 /* gpie |= IXGBE_GPIE_EIMEN; */
1856 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
1859 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
1860 /* legacy interrupts, use EIAM to auto-mask when reading EICR,
1861 * specifically only auto mask tx and rx interrupts */
1862 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
1865 mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
1866 if (max_frame != (mhadd >> IXGBE_MHADD_MFS_SHIFT)) {
1867 mhadd &= ~IXGBE_MHADD_MFS_MASK;
1868 mhadd |= max_frame << IXGBE_MHADD_MFS_SHIFT;
1870 IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
1873 for (i = 0; i < adapter->num_tx_queues; i++) {
1874 j = adapter->tx_ring[i].reg_idx;
1875 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(j));
1876 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1877 txdctl |= (8 << 16);
1878 txdctl |= IXGBE_TXDCTL_ENABLE;
1879 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(j), txdctl);
1882 for (i = 0; i < adapter->num_rx_queues; i++) {
1883 j = adapter->rx_ring[i].reg_idx;
1884 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
1885 /* enable PTHRESH=32 descriptors (half the internal cache)
1886 * and HTHRESH=0 descriptors (to minimize latency on fetch),
1887 * this also removes a pesky rx_no_buffer_count increment */
1889 rxdctl |= IXGBE_RXDCTL_ENABLE;
1890 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), rxdctl);
1892 /* enable all receives */
1893 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1894 rxdctl |= (IXGBE_RXCTRL_DMBYPS | IXGBE_RXCTRL_RXEN);
1895 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxdctl);
1897 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
1898 ixgbe_configure_msix(adapter);
1900 ixgbe_configure_msi_and_legacy(adapter);
1902 clear_bit(__IXGBE_DOWN, &adapter->state);
1903 ixgbe_napi_enable_all(adapter);
1905 /* clear any pending interrupts, may auto mask */
1906 IXGBE_READ_REG(hw, IXGBE_EICR);
1908 ixgbe_irq_enable(adapter);
1910 /* bring the link up in the watchdog, this could race with our first
1911 * link up interrupt but shouldn't be a problem */
1912 adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
1913 adapter->link_check_timeout = jiffies;
1914 mod_timer(&adapter->watchdog_timer, jiffies);
1918 void ixgbe_reinit_locked(struct ixgbe_adapter *adapter)
1920 WARN_ON(in_interrupt());
1921 while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
1923 ixgbe_down(adapter);
1925 clear_bit(__IXGBE_RESETTING, &adapter->state);
1928 int ixgbe_up(struct ixgbe_adapter *adapter)
1930 /* hardware has been reset, we need to reload some things */
1931 ixgbe_configure(adapter);
1933 return ixgbe_up_complete(adapter);
1936 void ixgbe_reset(struct ixgbe_adapter *adapter)
1938 if (ixgbe_init_hw(&adapter->hw))
1939 DPRINTK(PROBE, ERR, "Hardware Error\n");
1941 /* reprogram the RAR[0] in case user changed it. */
1942 ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
1947 static int ixgbe_resume(struct pci_dev *pdev)
1949 struct net_device *netdev = pci_get_drvdata(pdev);
1950 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1953 pci_set_power_state(pdev, PCI_D0);
1954 pci_restore_state(pdev);
1955 err = pci_enable_device(pdev);
1957 printk(KERN_ERR "ixgbe: Cannot enable PCI device from " \
1961 pci_set_master(pdev);
1963 pci_enable_wake(pdev, PCI_D3hot, 0);
1964 pci_enable_wake(pdev, PCI_D3cold, 0);
1966 if (netif_running(netdev)) {
1967 err = ixgbe_request_irq(adapter);
1972 ixgbe_reset(adapter);
1974 if (netif_running(netdev))
1977 netif_device_attach(netdev);
1984 * ixgbe_clean_rx_ring - Free Rx Buffers per Queue
1985 * @adapter: board private structure
1986 * @rx_ring: ring to free buffers from
1988 static void ixgbe_clean_rx_ring(struct ixgbe_adapter *adapter,
1989 struct ixgbe_ring *rx_ring)
1991 struct pci_dev *pdev = adapter->pdev;
1995 /* Free all the Rx ring sk_buffs */
1997 for (i = 0; i < rx_ring->count; i++) {
1998 struct ixgbe_rx_buffer *rx_buffer_info;
2000 rx_buffer_info = &rx_ring->rx_buffer_info[i];
2001 if (rx_buffer_info->dma) {
2002 pci_unmap_single(pdev, rx_buffer_info->dma,
2003 rx_ring->rx_buf_len,
2004 PCI_DMA_FROMDEVICE);
2005 rx_buffer_info->dma = 0;
2007 if (rx_buffer_info->skb) {
2008 dev_kfree_skb(rx_buffer_info->skb);
2009 rx_buffer_info->skb = NULL;
2011 if (!rx_buffer_info->page)
2013 pci_unmap_page(pdev, rx_buffer_info->page_dma, PAGE_SIZE,
2014 PCI_DMA_FROMDEVICE);
2015 rx_buffer_info->page_dma = 0;
2017 put_page(rx_buffer_info->page);
2018 rx_buffer_info->page = NULL;
2021 size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
2022 memset(rx_ring->rx_buffer_info, 0, size);
2024 /* Zero out the descriptor ring */
2025 memset(rx_ring->desc, 0, rx_ring->size);
2027 rx_ring->next_to_clean = 0;
2028 rx_ring->next_to_use = 0;
2030 writel(0, adapter->hw.hw_addr + rx_ring->head);
2031 writel(0, adapter->hw.hw_addr + rx_ring->tail);
2035 * ixgbe_clean_tx_ring - Free Tx Buffers
2036 * @adapter: board private structure
2037 * @tx_ring: ring to be cleaned
2039 static void ixgbe_clean_tx_ring(struct ixgbe_adapter *adapter,
2040 struct ixgbe_ring *tx_ring)
2042 struct ixgbe_tx_buffer *tx_buffer_info;
2046 /* Free all the Tx ring sk_buffs */
2048 for (i = 0; i < tx_ring->count; i++) {
2049 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2050 ixgbe_unmap_and_free_tx_resource(adapter, tx_buffer_info);
2053 size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
2054 memset(tx_ring->tx_buffer_info, 0, size);
2056 /* Zero out the descriptor ring */
2057 memset(tx_ring->desc, 0, tx_ring->size);
2059 tx_ring->next_to_use = 0;
2060 tx_ring->next_to_clean = 0;
2062 writel(0, adapter->hw.hw_addr + tx_ring->head);
2063 writel(0, adapter->hw.hw_addr + tx_ring->tail);
2067 * ixgbe_clean_all_rx_rings - Free Rx Buffers for all queues
2068 * @adapter: board private structure
2070 static void ixgbe_clean_all_rx_rings(struct ixgbe_adapter *adapter)
2074 for (i = 0; i < adapter->num_rx_queues; i++)
2075 ixgbe_clean_rx_ring(adapter, &adapter->rx_ring[i]);
2079 * ixgbe_clean_all_tx_rings - Free Tx Buffers for all queues
2080 * @adapter: board private structure
2082 static void ixgbe_clean_all_tx_rings(struct ixgbe_adapter *adapter)
2086 for (i = 0; i < adapter->num_tx_queues; i++)
2087 ixgbe_clean_tx_ring(adapter, &adapter->tx_ring[i]);
2090 void ixgbe_down(struct ixgbe_adapter *adapter)
2092 struct net_device *netdev = adapter->netdev;
2095 /* signal that we are down to the interrupt handler */
2096 set_bit(__IXGBE_DOWN, &adapter->state);
2098 /* disable receives */
2099 rxctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_RXCTRL);
2100 IXGBE_WRITE_REG(&adapter->hw, IXGBE_RXCTRL,
2101 rxctrl & ~IXGBE_RXCTRL_RXEN);
2103 netif_tx_disable(netdev);
2105 /* disable transmits in the hardware */
2107 /* flush both disables */
2108 IXGBE_WRITE_FLUSH(&adapter->hw);
2111 ixgbe_irq_disable(adapter);
2113 ixgbe_napi_disable_all(adapter);
2114 del_timer_sync(&adapter->watchdog_timer);
2115 cancel_work_sync(&adapter->watchdog_task);
2117 netif_carrier_off(netdev);
2118 netif_tx_stop_all_queues(netdev);
2120 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
2121 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
2122 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
2123 dca_remove_requester(&adapter->pdev->dev);
2127 if (!pci_channel_offline(adapter->pdev))
2128 ixgbe_reset(adapter);
2129 ixgbe_clean_all_tx_rings(adapter);
2130 ixgbe_clean_all_rx_rings(adapter);
2132 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
2133 /* since we reset the hardware DCA settings were cleared */
2134 if (dca_add_requester(&adapter->pdev->dev) == 0) {
2135 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
2136 /* always use CB2 mode, difference is masked
2137 * in the CB driver */
2138 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 2);
2139 ixgbe_setup_dca(adapter);
2144 static int ixgbe_suspend(struct pci_dev *pdev, pm_message_t state)
2146 struct net_device *netdev = pci_get_drvdata(pdev);
2147 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2152 netif_device_detach(netdev);
2154 if (netif_running(netdev)) {
2155 ixgbe_down(adapter);
2156 ixgbe_free_irq(adapter);
2160 retval = pci_save_state(pdev);
2165 pci_enable_wake(pdev, PCI_D3hot, 0);
2166 pci_enable_wake(pdev, PCI_D3cold, 0);
2168 ixgbe_release_hw_control(adapter);
2170 pci_disable_device(pdev);
2172 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2177 static void ixgbe_shutdown(struct pci_dev *pdev)
2179 ixgbe_suspend(pdev, PMSG_SUSPEND);
2183 * ixgbe_poll - NAPI Rx polling callback
2184 * @napi: structure for representing this polling device
2185 * @budget: how many packets driver is allowed to clean
2187 * This function is used for legacy and MSI, NAPI mode
2189 static int ixgbe_poll(struct napi_struct *napi, int budget)
2191 struct ixgbe_q_vector *q_vector = container_of(napi,
2192 struct ixgbe_q_vector, napi);
2193 struct ixgbe_adapter *adapter = q_vector->adapter;
2194 int tx_cleaned = 0, work_done = 0;
2196 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
2197 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
2198 ixgbe_update_tx_dca(adapter, adapter->tx_ring);
2199 ixgbe_update_rx_dca(adapter, adapter->rx_ring);
2203 tx_cleaned = ixgbe_clean_tx_irq(adapter, adapter->tx_ring);
2204 ixgbe_clean_rx_irq(adapter, adapter->rx_ring, &work_done, budget);
2209 /* If budget not fully consumed, exit the polling mode */
2210 if (work_done < budget) {
2211 netif_rx_complete(adapter->netdev, napi);
2212 if (adapter->rx_eitr < IXGBE_MIN_ITR_USECS)
2213 ixgbe_set_itr(adapter);
2214 if (!test_bit(__IXGBE_DOWN, &adapter->state))
2215 ixgbe_irq_enable(adapter);
2222 * ixgbe_tx_timeout - Respond to a Tx Hang
2223 * @netdev: network interface device structure
2225 static void ixgbe_tx_timeout(struct net_device *netdev)
2227 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2229 /* Do the reset outside of interrupt context */
2230 schedule_work(&adapter->reset_task);
2233 static void ixgbe_reset_task(struct work_struct *work)
2235 struct ixgbe_adapter *adapter;
2236 adapter = container_of(work, struct ixgbe_adapter, reset_task);
2238 adapter->tx_timeout_count++;
2240 ixgbe_reinit_locked(adapter);
2243 static void ixgbe_acquire_msix_vectors(struct ixgbe_adapter *adapter,
2246 int err, vector_threshold;
2248 /* We'll want at least 3 (vector_threshold):
2251 * 3) Other (Link Status Change, etc.)
2252 * 4) TCP Timer (optional)
2254 vector_threshold = MIN_MSIX_COUNT;
2256 /* The more we get, the more we will assign to Tx/Rx Cleanup
2257 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2258 * Right now, we simply care about how many we'll get; we'll
2259 * set them up later while requesting irq's.
2261 while (vectors >= vector_threshold) {
2262 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
2264 if (!err) /* Success in acquiring all requested vectors. */
2267 vectors = 0; /* Nasty failure, quit now */
2268 else /* err == number of vectors we should try again with */
2272 if (vectors < vector_threshold) {
2273 /* Can't allocate enough MSI-X interrupts? Oh well.
2274 * This just means we'll go with either a single MSI
2275 * vector or fall back to legacy interrupts.
2277 DPRINTK(HW, DEBUG, "Unable to allocate MSI-X interrupts\n");
2278 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2279 kfree(adapter->msix_entries);
2280 adapter->msix_entries = NULL;
2281 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
2282 adapter->num_tx_queues = 1;
2283 adapter->num_rx_queues = 1;
2285 adapter->flags |= IXGBE_FLAG_MSIX_ENABLED; /* Woot! */
2286 adapter->num_msix_vectors = vectors;
2290 static void __devinit ixgbe_set_num_queues(struct ixgbe_adapter *adapter)
2293 int feature_mask = 0, rss_i, rss_m;
2295 /* Number of supported queues */
2296 switch (adapter->hw.mac.type) {
2297 case ixgbe_mac_82598EB:
2298 rss_i = adapter->ring_feature[RING_F_RSS].indices;
2300 feature_mask |= IXGBE_FLAG_RSS_ENABLED;
2302 switch (adapter->flags & feature_mask) {
2303 case (IXGBE_FLAG_RSS_ENABLED):
2317 adapter->ring_feature[RING_F_RSS].indices = rss_i;
2318 adapter->ring_feature[RING_F_RSS].mask = rss_m;
2326 adapter->num_rx_queues = nrq;
2327 adapter->num_tx_queues = ntq;
2331 * ixgbe_cache_ring_register - Descriptor ring to register mapping
2332 * @adapter: board private structure to initialize
2334 * Once we know the feature-set enabled for the device, we'll cache
2335 * the register offset the descriptor ring is assigned to.
2337 static void __devinit ixgbe_cache_ring_register(struct ixgbe_adapter *adapter)
2339 /* TODO: Remove all uses of the indices in the cases where multiple
2340 * features are OR'd together, if the feature set makes sense.
2342 int feature_mask = 0, rss_i;
2343 int i, txr_idx, rxr_idx;
2345 /* Number of supported queues */
2346 switch (adapter->hw.mac.type) {
2347 case ixgbe_mac_82598EB:
2348 rss_i = adapter->ring_feature[RING_F_RSS].indices;
2351 feature_mask |= IXGBE_FLAG_RSS_ENABLED;
2352 switch (adapter->flags & feature_mask) {
2353 case (IXGBE_FLAG_RSS_ENABLED):
2354 for (i = 0; i < adapter->num_rx_queues; i++)
2355 adapter->rx_ring[i].reg_idx = i;
2356 for (i = 0; i < adapter->num_tx_queues; i++)
2357 adapter->tx_ring[i].reg_idx = i;
2370 * ixgbe_alloc_queues - Allocate memory for all rings
2371 * @adapter: board private structure to initialize
2373 * We allocate one ring per queue at run-time since we don't know the
2374 * number of queues at compile-time. The polling_netdev array is
2375 * intended for Multiqueue, but should work fine with a single queue.
2377 static int __devinit ixgbe_alloc_queues(struct ixgbe_adapter *adapter)
2381 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
2382 sizeof(struct ixgbe_ring), GFP_KERNEL);
2383 if (!adapter->tx_ring)
2384 goto err_tx_ring_allocation;
2386 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
2387 sizeof(struct ixgbe_ring), GFP_KERNEL);
2388 if (!adapter->rx_ring)
2389 goto err_rx_ring_allocation;
2391 for (i = 0; i < adapter->num_tx_queues; i++) {
2392 adapter->tx_ring[i].count = IXGBE_DEFAULT_TXD;
2393 adapter->tx_ring[i].queue_index = i;
2395 for (i = 0; i < adapter->num_rx_queues; i++) {
2396 adapter->rx_ring[i].count = IXGBE_DEFAULT_RXD;
2397 adapter->rx_ring[i].queue_index = i;
2400 ixgbe_cache_ring_register(adapter);
2404 err_rx_ring_allocation:
2405 kfree(adapter->tx_ring);
2406 err_tx_ring_allocation:
2411 * ixgbe_set_interrupt_capability - set MSI-X or MSI if supported
2412 * @adapter: board private structure to initialize
2414 * Attempt to configure the interrupts using the best available
2415 * capabilities of the hardware and the kernel.
2417 static int __devinit ixgbe_set_interrupt_capability(struct ixgbe_adapter
2421 int vector, v_budget;
2424 * Set the default interrupt throttle rate.
2426 adapter->rx_eitr = (1000000 / IXGBE_DEFAULT_ITR_RX_USECS);
2427 adapter->tx_eitr = (1000000 / IXGBE_DEFAULT_ITR_TX_USECS);
2430 * It's easy to be greedy for MSI-X vectors, but it really
2431 * doesn't do us much good if we have a lot more vectors
2432 * than CPU's. So let's be conservative and only ask for
2433 * (roughly) twice the number of vectors as there are CPU's.
2435 v_budget = min(adapter->num_rx_queues + adapter->num_tx_queues,
2436 (int)(num_online_cpus() * 2)) + NON_Q_VECTORS;
2439 * At the same time, hardware can only support a maximum of
2440 * MAX_MSIX_COUNT vectors. With features such as RSS and VMDq,
2441 * we can easily reach upwards of 64 Rx descriptor queues and
2442 * 32 Tx queues. Thus, we cap it off in those rare cases where
2443 * the cpu count also exceeds our vector limit.
2445 v_budget = min(v_budget, MAX_MSIX_COUNT);
2447 /* A failure in MSI-X entry allocation isn't fatal, but it does
2448 * mean we disable MSI-X capabilities of the adapter. */
2449 adapter->msix_entries = kcalloc(v_budget,
2450 sizeof(struct msix_entry), GFP_KERNEL);
2451 if (!adapter->msix_entries) {
2452 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
2453 ixgbe_set_num_queues(adapter);
2454 kfree(adapter->tx_ring);
2455 kfree(adapter->rx_ring);
2456 err = ixgbe_alloc_queues(adapter);
2458 DPRINTK(PROBE, ERR, "Unable to allocate memory "
2466 for (vector = 0; vector < v_budget; vector++)
2467 adapter->msix_entries[vector].entry = vector;
2469 ixgbe_acquire_msix_vectors(adapter, v_budget);
2471 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
2475 err = pci_enable_msi(adapter->pdev);
2477 adapter->flags |= IXGBE_FLAG_MSI_ENABLED;
2479 DPRINTK(HW, DEBUG, "Unable to allocate MSI interrupt, "
2480 "falling back to legacy. Error: %d\n", err);
2486 /* Notify the stack of the (possibly) reduced Tx Queue count. */
2487 adapter->netdev->real_num_tx_queues = adapter->num_tx_queues;
2492 static void ixgbe_reset_interrupt_capability(struct ixgbe_adapter *adapter)
2494 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2495 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2496 pci_disable_msix(adapter->pdev);
2497 kfree(adapter->msix_entries);
2498 adapter->msix_entries = NULL;
2499 } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
2500 adapter->flags &= ~IXGBE_FLAG_MSI_ENABLED;
2501 pci_disable_msi(adapter->pdev);
2507 * ixgbe_init_interrupt_scheme - Determine proper interrupt scheme
2508 * @adapter: board private structure to initialize
2510 * We determine which interrupt scheme to use based on...
2511 * - Kernel support (MSI, MSI-X)
2512 * - which can be user-defined (via MODULE_PARAM)
2513 * - Hardware queue count (num_*_queues)
2514 * - defined by miscellaneous hardware support/features (RSS, etc.)
2516 static int __devinit ixgbe_init_interrupt_scheme(struct ixgbe_adapter *adapter)
2520 /* Number of supported queues */
2521 ixgbe_set_num_queues(adapter);
2523 err = ixgbe_alloc_queues(adapter);
2525 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
2526 goto err_alloc_queues;
2529 err = ixgbe_set_interrupt_capability(adapter);
2531 DPRINTK(PROBE, ERR, "Unable to setup interrupt capabilities\n");
2532 goto err_set_interrupt;
2535 DPRINTK(DRV, INFO, "Multiqueue %s: Rx Queue count = %u, "
2536 "Tx Queue count = %u\n",
2537 (adapter->num_rx_queues > 1) ? "Enabled" :
2538 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2540 set_bit(__IXGBE_DOWN, &adapter->state);
2545 kfree(adapter->tx_ring);
2546 kfree(adapter->rx_ring);
2552 * ixgbe_sw_init - Initialize general software structures (struct ixgbe_adapter)
2553 * @adapter: board private structure to initialize
2555 * ixgbe_sw_init initializes the Adapter private data structure.
2556 * Fields are initialized based on PCI device information and
2557 * OS network device settings (MTU size).
2559 static int __devinit ixgbe_sw_init(struct ixgbe_adapter *adapter)
2561 struct ixgbe_hw *hw = &adapter->hw;
2562 struct pci_dev *pdev = adapter->pdev;
2565 /* Set capability flags */
2566 rss = min(IXGBE_MAX_RSS_INDICES, (int)num_online_cpus());
2567 adapter->ring_feature[RING_F_RSS].indices = rss;
2568 adapter->flags |= IXGBE_FLAG_RSS_ENABLED;
2570 /* Enable Dynamic interrupt throttling by default */
2571 adapter->rx_eitr = 1;
2572 adapter->tx_eitr = 1;
2574 /* default flow control settings */
2575 hw->fc.original_type = ixgbe_fc_none;
2576 hw->fc.type = ixgbe_fc_none;
2577 hw->fc.high_water = IXGBE_DEFAULT_FCRTH;
2578 hw->fc.low_water = IXGBE_DEFAULT_FCRTL;
2579 hw->fc.pause_time = IXGBE_DEFAULT_FCPAUSE;
2580 hw->fc.send_xon = true;
2582 /* select 10G link by default */
2583 hw->mac.link_mode_select = IXGBE_AUTOC_LMS_10G_LINK_NO_AN;
2584 if (hw->mac.ops.reset(hw)) {
2585 dev_err(&pdev->dev, "HW Init failed\n");
2588 if (hw->mac.ops.setup_link_speed(hw, IXGBE_LINK_SPEED_10GB_FULL, true,
2590 dev_err(&pdev->dev, "Link Speed setup failed\n");
2594 /* initialize eeprom parameters */
2595 if (ixgbe_init_eeprom(hw)) {
2596 dev_err(&pdev->dev, "EEPROM initialization failed\n");
2600 /* enable rx csum by default */
2601 adapter->flags |= IXGBE_FLAG_RX_CSUM_ENABLED;
2603 set_bit(__IXGBE_DOWN, &adapter->state);
2609 * ixgbe_setup_tx_resources - allocate Tx resources (Descriptors)
2610 * @adapter: board private structure
2611 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2613 * Return 0 on success, negative on failure
2615 int ixgbe_setup_tx_resources(struct ixgbe_adapter *adapter,
2616 struct ixgbe_ring *tx_ring)
2618 struct pci_dev *pdev = adapter->pdev;
2621 size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
2622 tx_ring->tx_buffer_info = vmalloc(size);
2623 if (!tx_ring->tx_buffer_info)
2625 memset(tx_ring->tx_buffer_info, 0, size);
2627 /* round up to nearest 4K */
2628 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc) +
2630 tx_ring->size = ALIGN(tx_ring->size, 4096);
2632 tx_ring->desc = pci_alloc_consistent(pdev, tx_ring->size,
2637 tx_ring->next_to_use = 0;
2638 tx_ring->next_to_clean = 0;
2639 tx_ring->work_limit = tx_ring->count;
2643 vfree(tx_ring->tx_buffer_info);
2644 tx_ring->tx_buffer_info = NULL;
2645 DPRINTK(PROBE, ERR, "Unable to allocate memory for the transmit "
2646 "descriptor ring\n");
2651 * ixgbe_setup_rx_resources - allocate Rx resources (Descriptors)
2652 * @adapter: board private structure
2653 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2655 * Returns 0 on success, negative on failure
2657 int ixgbe_setup_rx_resources(struct ixgbe_adapter *adapter,
2658 struct ixgbe_ring *rx_ring)
2660 struct pci_dev *pdev = adapter->pdev;
2663 size = sizeof(struct net_lro_desc) * IXGBE_MAX_LRO_DESCRIPTORS;
2664 rx_ring->lro_mgr.lro_arr = vmalloc(size);
2665 if (!rx_ring->lro_mgr.lro_arr)
2667 memset(rx_ring->lro_mgr.lro_arr, 0, size);
2669 size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
2670 rx_ring->rx_buffer_info = vmalloc(size);
2671 if (!rx_ring->rx_buffer_info) {
2673 "vmalloc allocation failed for the rx desc ring\n");
2676 memset(rx_ring->rx_buffer_info, 0, size);
2678 /* Round up to nearest 4K */
2679 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2680 rx_ring->size = ALIGN(rx_ring->size, 4096);
2682 rx_ring->desc = pci_alloc_consistent(pdev, rx_ring->size, &rx_ring->dma);
2684 if (!rx_ring->desc) {
2686 "Memory allocation failed for the rx desc ring\n");
2687 vfree(rx_ring->rx_buffer_info);
2691 rx_ring->next_to_clean = 0;
2692 rx_ring->next_to_use = 0;
2697 vfree(rx_ring->lro_mgr.lro_arr);
2698 rx_ring->lro_mgr.lro_arr = NULL;
2703 * ixgbe_free_tx_resources - Free Tx Resources per Queue
2704 * @adapter: board private structure
2705 * @tx_ring: Tx descriptor ring for a specific queue
2707 * Free all transmit software resources
2709 static void ixgbe_free_tx_resources(struct ixgbe_adapter *adapter,
2710 struct ixgbe_ring *tx_ring)
2712 struct pci_dev *pdev = adapter->pdev;
2714 ixgbe_clean_tx_ring(adapter, tx_ring);
2716 vfree(tx_ring->tx_buffer_info);
2717 tx_ring->tx_buffer_info = NULL;
2719 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
2721 tx_ring->desc = NULL;
2725 * ixgbe_free_all_tx_resources - Free Tx Resources for All Queues
2726 * @adapter: board private structure
2728 * Free all transmit software resources
2730 static void ixgbe_free_all_tx_resources(struct ixgbe_adapter *adapter)
2734 for (i = 0; i < adapter->num_tx_queues; i++)
2735 ixgbe_free_tx_resources(adapter, &adapter->tx_ring[i]);
2739 * ixgbe_free_rx_resources - Free Rx Resources
2740 * @adapter: board private structure
2741 * @rx_ring: ring to clean the resources from
2743 * Free all receive software resources
2745 static void ixgbe_free_rx_resources(struct ixgbe_adapter *adapter,
2746 struct ixgbe_ring *rx_ring)
2748 struct pci_dev *pdev = adapter->pdev;
2750 vfree(rx_ring->lro_mgr.lro_arr);
2751 rx_ring->lro_mgr.lro_arr = NULL;
2753 ixgbe_clean_rx_ring(adapter, rx_ring);
2755 vfree(rx_ring->rx_buffer_info);
2756 rx_ring->rx_buffer_info = NULL;
2758 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2760 rx_ring->desc = NULL;
2764 * ixgbe_free_all_rx_resources - Free Rx Resources for All Queues
2765 * @adapter: board private structure
2767 * Free all receive software resources
2769 static void ixgbe_free_all_rx_resources(struct ixgbe_adapter *adapter)
2773 for (i = 0; i < adapter->num_rx_queues; i++)
2774 ixgbe_free_rx_resources(adapter, &adapter->rx_ring[i]);
2778 * ixgbe_setup_all_tx_resources - allocate all queues Tx resources
2779 * @adapter: board private structure
2781 * If this function returns with an error, then it's possible one or
2782 * more of the rings is populated (while the rest are not). It is the
2783 * callers duty to clean those orphaned rings.
2785 * Return 0 on success, negative on failure
2787 static int ixgbe_setup_all_tx_resources(struct ixgbe_adapter *adapter)
2791 for (i = 0; i < adapter->num_tx_queues; i++) {
2792 err = ixgbe_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2795 "Allocation for Tx Queue %u failed\n", i);
2804 * ixgbe_setup_all_rx_resources - allocate all queues Rx resources
2805 * @adapter: board private structure
2807 * If this function returns with an error, then it's possible one or
2808 * more of the rings is populated (while the rest are not). It is the
2809 * callers duty to clean those orphaned rings.
2811 * Return 0 on success, negative on failure
2814 static int ixgbe_setup_all_rx_resources(struct ixgbe_adapter *adapter)
2818 for (i = 0; i < adapter->num_rx_queues; i++) {
2819 err = ixgbe_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2822 "Allocation for Rx Queue %u failed\n", i);
2831 * ixgbe_change_mtu - Change the Maximum Transfer Unit
2832 * @netdev: network interface device structure
2833 * @new_mtu: new value for maximum frame size
2835 * Returns 0 on success, negative on failure
2837 static int ixgbe_change_mtu(struct net_device *netdev, int new_mtu)
2839 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2840 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2842 if ((max_frame < (ETH_ZLEN + ETH_FCS_LEN)) ||
2843 (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE))
2846 DPRINTK(PROBE, INFO, "changing MTU from %d to %d\n",
2847 netdev->mtu, new_mtu);
2848 /* must set new MTU before calling down or up */
2849 netdev->mtu = new_mtu;
2851 if (netif_running(netdev))
2852 ixgbe_reinit_locked(adapter);
2858 * ixgbe_open - Called when a network interface is made active
2859 * @netdev: network interface device structure
2861 * Returns 0 on success, negative value on failure
2863 * The open entry point is called when a network interface is made
2864 * active by the system (IFF_UP). At this point all resources needed
2865 * for transmit and receive operations are allocated, the interrupt
2866 * handler is registered with the OS, the watchdog timer is started,
2867 * and the stack is notified that the interface is ready.
2869 static int ixgbe_open(struct net_device *netdev)
2871 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2874 /* disallow open during test */
2875 if (test_bit(__IXGBE_TESTING, &adapter->state))
2878 /* allocate transmit descriptors */
2879 err = ixgbe_setup_all_tx_resources(adapter);
2883 /* allocate receive descriptors */
2884 err = ixgbe_setup_all_rx_resources(adapter);
2888 ixgbe_configure(adapter);
2890 err = ixgbe_request_irq(adapter);
2894 err = ixgbe_up_complete(adapter);
2898 netif_tx_start_all_queues(netdev);
2903 ixgbe_release_hw_control(adapter);
2904 ixgbe_free_irq(adapter);
2906 ixgbe_free_all_rx_resources(adapter);
2908 ixgbe_free_all_tx_resources(adapter);
2910 ixgbe_reset(adapter);
2916 * ixgbe_close - Disables a network interface
2917 * @netdev: network interface device structure
2919 * Returns 0, this is not allowed to fail
2921 * The close entry point is called when an interface is de-activated
2922 * by the OS. The hardware is still under the drivers control, but
2923 * needs to be disabled. A global MAC reset is issued to stop the
2924 * hardware, and all transmit and receive resources are freed.
2926 static int ixgbe_close(struct net_device *netdev)
2928 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2930 ixgbe_down(adapter);
2931 ixgbe_free_irq(adapter);
2933 ixgbe_free_all_tx_resources(adapter);
2934 ixgbe_free_all_rx_resources(adapter);
2936 ixgbe_release_hw_control(adapter);
2942 * ixgbe_update_stats - Update the board statistics counters.
2943 * @adapter: board private structure
2945 void ixgbe_update_stats(struct ixgbe_adapter *adapter)
2947 struct ixgbe_hw *hw = &adapter->hw;
2949 u32 i, missed_rx = 0, mpc, bprc, lxon, lxoff, xon_off_tot;
2951 adapter->stats.crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
2952 for (i = 0; i < 8; i++) {
2953 /* for packet buffers not used, the register should read 0 */
2954 mpc = IXGBE_READ_REG(hw, IXGBE_MPC(i));
2956 adapter->stats.mpc[i] += mpc;
2957 total_mpc += adapter->stats.mpc[i];
2958 adapter->stats.rnbc[i] += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
2960 adapter->stats.gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
2961 /* work around hardware counting issue */
2962 adapter->stats.gprc -= missed_rx;
2964 /* 82598 hardware only has a 32 bit counter in the high register */
2965 adapter->stats.gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
2966 adapter->stats.gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
2967 adapter->stats.tor += IXGBE_READ_REG(hw, IXGBE_TORH);
2968 bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
2969 adapter->stats.bprc += bprc;
2970 adapter->stats.mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
2971 adapter->stats.mprc -= bprc;
2972 adapter->stats.roc += IXGBE_READ_REG(hw, IXGBE_ROC);
2973 adapter->stats.prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
2974 adapter->stats.prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
2975 adapter->stats.prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
2976 adapter->stats.prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
2977 adapter->stats.prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
2978 adapter->stats.prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
2979 adapter->stats.rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
2980 adapter->stats.lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
2981 adapter->stats.lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
2982 lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
2983 adapter->stats.lxontxc += lxon;
2984 lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
2985 adapter->stats.lxofftxc += lxoff;
2986 adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
2987 adapter->stats.gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
2988 adapter->stats.mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
2990 * 82598 errata - tx of flow control packets is included in tx counters
2992 xon_off_tot = lxon + lxoff;
2993 adapter->stats.gptc -= xon_off_tot;
2994 adapter->stats.mptc -= xon_off_tot;
2995 adapter->stats.gotc -= (xon_off_tot * (ETH_ZLEN + ETH_FCS_LEN));
2996 adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
2997 adapter->stats.rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
2998 adapter->stats.rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
2999 adapter->stats.tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
3000 adapter->stats.ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
3001 adapter->stats.ptc64 -= xon_off_tot;
3002 adapter->stats.ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
3003 adapter->stats.ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
3004 adapter->stats.ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
3005 adapter->stats.ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
3006 adapter->stats.ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
3007 adapter->stats.bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
3009 /* Fill out the OS statistics structure */
3010 adapter->net_stats.multicast = adapter->stats.mprc;
3013 adapter->net_stats.rx_errors = adapter->stats.crcerrs +
3014 adapter->stats.rlec;
3015 adapter->net_stats.rx_dropped = 0;
3016 adapter->net_stats.rx_length_errors = adapter->stats.rlec;
3017 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3018 adapter->net_stats.rx_missed_errors = total_mpc;
3022 * ixgbe_watchdog - Timer Call-back
3023 * @data: pointer to adapter cast into an unsigned long
3025 static void ixgbe_watchdog(unsigned long data)
3027 struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
3028 struct ixgbe_hw *hw = &adapter->hw;
3030 /* Do the watchdog outside of interrupt context due to the lovely
3031 * delays that some of the newer hardware requires */
3032 if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
3033 /* Cause software interrupt to ensure rx rings are cleaned */
3034 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3036 (1 << (adapter->num_msix_vectors - NON_Q_VECTORS)) - 1;
3037 IXGBE_WRITE_REG(hw, IXGBE_EICS, eics);
3039 /* For legacy and MSI interrupts don't set any bits that
3040 * are enabled for EIAM, because this operation would
3041 * set *both* EIMS and EICS for any bit in EIAM */
3042 IXGBE_WRITE_REG(hw, IXGBE_EICS,
3043 (IXGBE_EICS_TCP_TIMER | IXGBE_EICS_OTHER));
3045 /* Reset the timer */
3046 mod_timer(&adapter->watchdog_timer,
3047 round_jiffies(jiffies + 2 * HZ));
3050 schedule_work(&adapter->watchdog_task);
3054 * ixgbe_watchdog_task - worker thread to bring link up
3055 * @work: pointer to work_struct containing our data
3057 static void ixgbe_watchdog_task(struct work_struct *work)
3059 struct ixgbe_adapter *adapter = container_of(work,
3060 struct ixgbe_adapter,
3062 struct net_device *netdev = adapter->netdev;
3063 struct ixgbe_hw *hw = &adapter->hw;
3064 u32 link_speed = adapter->link_speed;
3065 bool link_up = adapter->link_up;
3067 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
3069 if (adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE) {
3070 hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
3072 time_after(jiffies, (adapter->link_check_timeout +
3073 IXGBE_TRY_LINK_TIMEOUT))) {
3074 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMC_LSC);
3075 adapter->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
3077 adapter->link_up = link_up;
3078 adapter->link_speed = link_speed;
3082 if (!netif_carrier_ok(netdev)) {
3083 u32 frctl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3084 u32 rmcs = IXGBE_READ_REG(hw, IXGBE_RMCS);
3085 #define FLOW_RX (frctl & IXGBE_FCTRL_RFCE)
3086 #define FLOW_TX (rmcs & IXGBE_RMCS_TFCE_802_3X)
3087 DPRINTK(LINK, INFO, "NIC Link is Up %s, "
3088 "Flow Control: %s\n",
3089 (link_speed == IXGBE_LINK_SPEED_10GB_FULL ?
3091 (link_speed == IXGBE_LINK_SPEED_1GB_FULL ?
3092 "1 Gbps" : "unknown speed")),
3093 ((FLOW_RX && FLOW_TX) ? "RX/TX" :
3095 (FLOW_TX ? "TX" : "None"))));
3097 netif_carrier_on(netdev);
3098 netif_tx_wake_all_queues(netdev);
3100 /* Force detection of hung controller */
3101 adapter->detect_tx_hung = true;
3104 adapter->link_up = false;
3105 adapter->link_speed = 0;
3106 if (netif_carrier_ok(netdev)) {
3107 DPRINTK(LINK, INFO, "NIC Link is Down\n");
3108 netif_carrier_off(netdev);
3109 netif_tx_stop_all_queues(netdev);
3113 ixgbe_update_stats(adapter);
3114 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
3117 static int ixgbe_tso(struct ixgbe_adapter *adapter,
3118 struct ixgbe_ring *tx_ring, struct sk_buff *skb,
3119 u32 tx_flags, u8 *hdr_len)
3121 struct ixgbe_adv_tx_context_desc *context_desc;
3124 struct ixgbe_tx_buffer *tx_buffer_info;
3125 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
3126 u32 mss_l4len_idx = 0, l4len;
3128 if (skb_is_gso(skb)) {
3129 if (skb_header_cloned(skb)) {
3130 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3134 l4len = tcp_hdrlen(skb);
3137 if (skb->protocol == htons(ETH_P_IP)) {
3138 struct iphdr *iph = ip_hdr(skb);
3141 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
3145 adapter->hw_tso_ctxt++;
3146 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
3147 ipv6_hdr(skb)->payload_len = 0;
3148 tcp_hdr(skb)->check =
3149 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3150 &ipv6_hdr(skb)->daddr,
3152 adapter->hw_tso6_ctxt++;
3155 i = tx_ring->next_to_use;
3157 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3158 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
3160 /* VLAN MACLEN IPLEN */
3161 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3163 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
3164 vlan_macip_lens |= ((skb_network_offset(skb)) <<
3165 IXGBE_ADVTXD_MACLEN_SHIFT);
3166 *hdr_len += skb_network_offset(skb);
3168 (skb_transport_header(skb) - skb_network_header(skb));
3170 (skb_transport_header(skb) - skb_network_header(skb));
3171 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3172 context_desc->seqnum_seed = 0;
3174 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3175 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
3176 IXGBE_ADVTXD_DTYP_CTXT);
3178 if (skb->protocol == htons(ETH_P_IP))
3179 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
3180 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
3181 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
3185 (skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT);
3186 mss_l4len_idx |= (l4len << IXGBE_ADVTXD_L4LEN_SHIFT);
3187 /* use index 1 for TSO */
3188 mss_l4len_idx |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
3189 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3191 tx_buffer_info->time_stamp = jiffies;
3192 tx_buffer_info->next_to_watch = i;
3195 if (i == tx_ring->count)
3197 tx_ring->next_to_use = i;
3204 static bool ixgbe_tx_csum(struct ixgbe_adapter *adapter,
3205 struct ixgbe_ring *tx_ring,
3206 struct sk_buff *skb, u32 tx_flags)
3208 struct ixgbe_adv_tx_context_desc *context_desc;
3210 struct ixgbe_tx_buffer *tx_buffer_info;
3211 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
3213 if (skb->ip_summed == CHECKSUM_PARTIAL ||
3214 (tx_flags & IXGBE_TX_FLAGS_VLAN)) {
3215 i = tx_ring->next_to_use;
3216 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3217 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
3219 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3221 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
3222 vlan_macip_lens |= (skb_network_offset(skb) <<
3223 IXGBE_ADVTXD_MACLEN_SHIFT);
3224 if (skb->ip_summed == CHECKSUM_PARTIAL)
3225 vlan_macip_lens |= (skb_transport_header(skb) -
3226 skb_network_header(skb));
3228 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3229 context_desc->seqnum_seed = 0;
3231 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
3232 IXGBE_ADVTXD_DTYP_CTXT);
3234 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3235 switch (skb->protocol) {
3236 case __constant_htons(ETH_P_IP):
3237 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
3238 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
3240 IXGBE_ADVTXD_TUCMD_L4T_TCP;
3243 case __constant_htons(ETH_P_IPV6):
3244 /* XXX what about other V6 headers?? */
3245 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
3247 IXGBE_ADVTXD_TUCMD_L4T_TCP;
3251 if (unlikely(net_ratelimit())) {
3252 DPRINTK(PROBE, WARNING,
3253 "partial checksum but proto=%x!\n",
3260 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
3261 /* use index zero for tx checksum offload */
3262 context_desc->mss_l4len_idx = 0;
3264 tx_buffer_info->time_stamp = jiffies;
3265 tx_buffer_info->next_to_watch = i;
3266 adapter->hw_csum_tx_good++;
3268 if (i == tx_ring->count)
3270 tx_ring->next_to_use = i;
3277 static int ixgbe_tx_map(struct ixgbe_adapter *adapter,
3278 struct ixgbe_ring *tx_ring,
3279 struct sk_buff *skb, unsigned int first)
3281 struct ixgbe_tx_buffer *tx_buffer_info;
3282 unsigned int len = skb->len;
3283 unsigned int offset = 0, size, count = 0, i;
3284 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
3287 len -= skb->data_len;
3289 i = tx_ring->next_to_use;
3292 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3293 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
3295 tx_buffer_info->length = size;
3296 tx_buffer_info->dma = pci_map_single(adapter->pdev,
3298 size, PCI_DMA_TODEVICE);
3299 tx_buffer_info->time_stamp = jiffies;
3300 tx_buffer_info->next_to_watch = i;
3306 if (i == tx_ring->count)
3310 for (f = 0; f < nr_frags; f++) {
3311 struct skb_frag_struct *frag;
3313 frag = &skb_shinfo(skb)->frags[f];
3315 offset = frag->page_offset;
3318 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3319 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
3321 tx_buffer_info->length = size;
3322 tx_buffer_info->dma = pci_map_page(adapter->pdev,
3325 size, PCI_DMA_TODEVICE);
3326 tx_buffer_info->time_stamp = jiffies;
3327 tx_buffer_info->next_to_watch = i;
3333 if (i == tx_ring->count)
3338 i = tx_ring->count - 1;
3341 tx_ring->tx_buffer_info[i].skb = skb;
3342 tx_ring->tx_buffer_info[first].next_to_watch = i;
3347 static void ixgbe_tx_queue(struct ixgbe_adapter *adapter,
3348 struct ixgbe_ring *tx_ring,
3349 int tx_flags, int count, u32 paylen, u8 hdr_len)
3351 union ixgbe_adv_tx_desc *tx_desc = NULL;
3352 struct ixgbe_tx_buffer *tx_buffer_info;
3353 u32 olinfo_status = 0, cmd_type_len = 0;
3355 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
3357 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
3359 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
3361 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3362 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
3364 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
3365 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
3367 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
3368 IXGBE_ADVTXD_POPTS_SHIFT;
3370 /* use index 1 context for tso */
3371 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
3372 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3373 olinfo_status |= IXGBE_TXD_POPTS_IXSM <<
3374 IXGBE_ADVTXD_POPTS_SHIFT;
3376 } else if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3377 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
3378 IXGBE_ADVTXD_POPTS_SHIFT;
3380 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
3382 i = tx_ring->next_to_use;
3384 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3385 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
3386 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
3387 tx_desc->read.cmd_type_len =
3388 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
3389 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
3392 if (i == tx_ring->count)
3396 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
3399 * Force memory writes to complete before letting h/w
3400 * know there are new descriptors to fetch. (Only
3401 * applicable for weak-ordered memory model archs,
3406 tx_ring->next_to_use = i;
3407 writel(i, adapter->hw.hw_addr + tx_ring->tail);
3410 static int __ixgbe_maybe_stop_tx(struct net_device *netdev,
3411 struct ixgbe_ring *tx_ring, int size)
3413 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3415 netif_stop_subqueue(netdev, tx_ring->queue_index);
3416 /* Herbert's original patch had:
3417 * smp_mb__after_netif_stop_queue();
3418 * but since that doesn't exist yet, just open code it. */
3421 /* We need to check again in a case another CPU has just
3422 * made room available. */
3423 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
3426 /* A reprieve! - use start_queue because it doesn't call schedule */
3427 netif_start_subqueue(netdev, tx_ring->queue_index);
3428 ++adapter->restart_queue;
3432 static int ixgbe_maybe_stop_tx(struct net_device *netdev,
3433 struct ixgbe_ring *tx_ring, int size)
3435 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
3437 return __ixgbe_maybe_stop_tx(netdev, tx_ring, size);
3441 static int ixgbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3443 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3444 struct ixgbe_ring *tx_ring;
3445 unsigned int len = skb->len;
3447 unsigned int tx_flags = 0;
3450 unsigned int mss = 0;
3453 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
3454 len -= skb->data_len;
3455 r_idx = (adapter->num_tx_queues - 1) & skb->queue_mapping;
3456 tx_ring = &adapter->tx_ring[r_idx];
3459 if (skb->len <= 0) {
3461 return NETDEV_TX_OK;
3463 mss = skb_shinfo(skb)->gso_size;
3467 else if (skb->ip_summed == CHECKSUM_PARTIAL)
3470 count += TXD_USE_COUNT(len);
3471 for (f = 0; f < nr_frags; f++)
3472 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3474 if (ixgbe_maybe_stop_tx(netdev, tx_ring, count)) {
3476 return NETDEV_TX_BUSY;
3478 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
3479 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3480 tx_flags |= (vlan_tx_tag_get(skb) << IXGBE_TX_FLAGS_VLAN_SHIFT);
3483 if (skb->protocol == htons(ETH_P_IP))
3484 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3485 first = tx_ring->next_to_use;
3486 tso = ixgbe_tso(adapter, tx_ring, skb, tx_flags, &hdr_len);
3488 dev_kfree_skb_any(skb);
3489 return NETDEV_TX_OK;
3493 tx_flags |= IXGBE_TX_FLAGS_TSO;
3494 else if (ixgbe_tx_csum(adapter, tx_ring, skb, tx_flags) &&
3495 (skb->ip_summed == CHECKSUM_PARTIAL))
3496 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3498 ixgbe_tx_queue(adapter, tx_ring, tx_flags,
3499 ixgbe_tx_map(adapter, tx_ring, skb, first),
3502 netdev->trans_start = jiffies;
3504 ixgbe_maybe_stop_tx(netdev, tx_ring, DESC_NEEDED);
3506 return NETDEV_TX_OK;
3510 * ixgbe_get_stats - Get System Network Statistics
3511 * @netdev: network interface device structure
3513 * Returns the address of the device statistics structure.
3514 * The statistics are actually updated from the timer callback.
3516 static struct net_device_stats *ixgbe_get_stats(struct net_device *netdev)
3518 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3520 /* only return the current stats */
3521 return &adapter->net_stats;
3525 * ixgbe_set_mac - Change the Ethernet Address of the NIC
3526 * @netdev: network interface device structure
3527 * @p: pointer to an address structure
3529 * Returns 0 on success, negative on failure
3531 static int ixgbe_set_mac(struct net_device *netdev, void *p)
3533 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3534 struct sockaddr *addr = p;
3536 if (!is_valid_ether_addr(addr->sa_data))
3537 return -EADDRNOTAVAIL;
3539 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3540 memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
3542 ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
3547 #ifdef CONFIG_NET_POLL_CONTROLLER
3549 * Polling 'interrupt' - used by things like netconsole to send skbs
3550 * without having to re-enable interrupts. It's not called while
3551 * the interrupt routine is executing.
3553 static void ixgbe_netpoll(struct net_device *netdev)
3555 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3557 disable_irq(adapter->pdev->irq);
3558 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
3559 ixgbe_intr(adapter->pdev->irq, netdev);
3560 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
3561 enable_irq(adapter->pdev->irq);
3566 * ixgbe_napi_add_all - prep napi structs for use
3567 * @adapter: private struct
3568 * helper function to napi_add each possible q_vector->napi
3570 static void ixgbe_napi_add_all(struct ixgbe_adapter *adapter)
3572 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3573 int (*poll)(struct napi_struct *, int);
3575 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3576 poll = &ixgbe_clean_rxonly;
3579 /* only one q_vector for legacy modes */
3583 for (i = 0; i < q_vectors; i++) {
3584 struct ixgbe_q_vector *q_vector = &adapter->q_vector[i];
3585 netif_napi_add(adapter->netdev, &q_vector->napi,
3591 * ixgbe_probe - Device Initialization Routine
3592 * @pdev: PCI device information struct
3593 * @ent: entry in ixgbe_pci_tbl
3595 * Returns 0 on success, negative on failure
3597 * ixgbe_probe initializes an adapter identified by a pci_dev structure.
3598 * The OS initialization, configuring of the adapter private structure,
3599 * and a hardware reset occur.
3601 static int __devinit ixgbe_probe(struct pci_dev *pdev,
3602 const struct pci_device_id *ent)
3604 struct net_device *netdev;
3605 struct ixgbe_adapter *adapter = NULL;
3606 struct ixgbe_hw *hw;
3607 const struct ixgbe_info *ii = ixgbe_info_tbl[ent->driver_data];
3608 unsigned long mmio_start, mmio_len;
3609 static int cards_found;
3610 int i, err, pci_using_dac;
3611 u16 link_status, link_speed, link_width;
3614 err = pci_enable_device(pdev);
3618 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK) &&
3619 !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK)) {
3622 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3624 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3626 dev_err(&pdev->dev, "No usable DMA "
3627 "configuration, aborting\n");
3634 err = pci_request_regions(pdev, ixgbe_driver_name);
3636 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3640 pci_set_master(pdev);
3641 pci_save_state(pdev);
3643 netdev = alloc_etherdev_mq(sizeof(struct ixgbe_adapter), MAX_TX_QUEUES);
3646 goto err_alloc_etherdev;
3649 SET_NETDEV_DEV(netdev, &pdev->dev);
3651 pci_set_drvdata(pdev, netdev);
3652 adapter = netdev_priv(netdev);
3654 adapter->netdev = netdev;
3655 adapter->pdev = pdev;
3658 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3660 mmio_start = pci_resource_start(pdev, 0);
3661 mmio_len = pci_resource_len(pdev, 0);
3663 hw->hw_addr = ioremap(mmio_start, mmio_len);
3669 for (i = 1; i <= 5; i++) {
3670 if (pci_resource_len(pdev, i) == 0)
3674 netdev->open = &ixgbe_open;
3675 netdev->stop = &ixgbe_close;
3676 netdev->hard_start_xmit = &ixgbe_xmit_frame;
3677 netdev->get_stats = &ixgbe_get_stats;
3678 netdev->set_rx_mode = &ixgbe_set_rx_mode;
3679 netdev->set_multicast_list = &ixgbe_set_rx_mode;
3680 netdev->set_mac_address = &ixgbe_set_mac;
3681 netdev->change_mtu = &ixgbe_change_mtu;
3682 ixgbe_set_ethtool_ops(netdev);
3683 netdev->tx_timeout = &ixgbe_tx_timeout;
3684 netdev->watchdog_timeo = 5 * HZ;
3685 netdev->vlan_rx_register = ixgbe_vlan_rx_register;
3686 netdev->vlan_rx_add_vid = ixgbe_vlan_rx_add_vid;
3687 netdev->vlan_rx_kill_vid = ixgbe_vlan_rx_kill_vid;
3688 #ifdef CONFIG_NET_POLL_CONTROLLER
3689 netdev->poll_controller = ixgbe_netpoll;
3691 strcpy(netdev->name, pci_name(pdev));
3693 netdev->mem_start = mmio_start;
3694 netdev->mem_end = mmio_start + mmio_len;
3696 adapter->bd_number = cards_found;
3698 /* PCI config space info */
3699 hw->vendor_id = pdev->vendor;
3700 hw->device_id = pdev->device;
3701 hw->revision_id = pdev->revision;
3702 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3703 hw->subsystem_device_id = pdev->subsystem_device;
3706 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3707 hw->mac.type = ii->mac;
3709 err = ii->get_invariants(hw);
3713 /* setup the private structure */
3714 err = ixgbe_sw_init(adapter);
3718 netdev->features = NETIF_F_SG |
3720 NETIF_F_HW_VLAN_TX |
3721 NETIF_F_HW_VLAN_RX |
3722 NETIF_F_HW_VLAN_FILTER;
3724 netdev->features |= NETIF_F_IPV6_CSUM;
3725 netdev->features |= NETIF_F_TSO;
3726 netdev->features |= NETIF_F_TSO6;
3727 netdev->features |= NETIF_F_LRO;
3729 netdev->vlan_features |= NETIF_F_TSO;
3730 netdev->vlan_features |= NETIF_F_TSO6;
3731 netdev->vlan_features |= NETIF_F_IP_CSUM;
3732 netdev->vlan_features |= NETIF_F_SG;
3735 netdev->features |= NETIF_F_HIGHDMA;
3737 /* make sure the EEPROM is good */
3738 if (ixgbe_validate_eeprom_checksum(hw, NULL) < 0) {
3739 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
3744 memcpy(netdev->dev_addr, hw->mac.perm_addr, netdev->addr_len);
3745 memcpy(netdev->perm_addr, hw->mac.perm_addr, netdev->addr_len);
3747 if (ixgbe_validate_mac_addr(netdev->dev_addr)) {
3752 init_timer(&adapter->watchdog_timer);
3753 adapter->watchdog_timer.function = &ixgbe_watchdog;
3754 adapter->watchdog_timer.data = (unsigned long)adapter;
3756 INIT_WORK(&adapter->reset_task, ixgbe_reset_task);
3757 INIT_WORK(&adapter->watchdog_task, ixgbe_watchdog_task);
3759 err = ixgbe_init_interrupt_scheme(adapter);
3763 /* print bus type/speed/width info */
3764 pci_read_config_word(pdev, IXGBE_PCI_LINK_STATUS, &link_status);
3765 link_speed = link_status & IXGBE_PCI_LINK_SPEED;
3766 link_width = link_status & IXGBE_PCI_LINK_WIDTH;
3767 dev_info(&pdev->dev, "(PCI Express:%s:%s) "
3768 "%02x:%02x:%02x:%02x:%02x:%02x\n",
3769 ((link_speed == IXGBE_PCI_LINK_SPEED_5000) ? "5.0Gb/s" :
3770 (link_speed == IXGBE_PCI_LINK_SPEED_2500) ? "2.5Gb/s" :
3772 ((link_width == IXGBE_PCI_LINK_WIDTH_8) ? "Width x8" :
3773 (link_width == IXGBE_PCI_LINK_WIDTH_4) ? "Width x4" :
3774 (link_width == IXGBE_PCI_LINK_WIDTH_2) ? "Width x2" :
3775 (link_width == IXGBE_PCI_LINK_WIDTH_1) ? "Width x1" :
3777 netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2],
3778 netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);
3779 ixgbe_read_part_num(hw, &part_num);
3780 dev_info(&pdev->dev, "MAC: %d, PHY: %d, PBA No: %06x-%03x\n",
3781 hw->mac.type, hw->phy.type,
3782 (part_num >> 8), (part_num & 0xff));
3784 if (link_width <= IXGBE_PCI_LINK_WIDTH_4) {
3785 dev_warn(&pdev->dev, "PCI-Express bandwidth available for "
3786 "this card is not sufficient for optimal "
3788 dev_warn(&pdev->dev, "For optimal performance a x8 "
3789 "PCI-Express slot is required.\n");
3792 /* reset the hardware with the new settings */
3795 netif_carrier_off(netdev);
3796 netif_tx_stop_all_queues(netdev);
3798 ixgbe_napi_add_all(adapter);
3800 strcpy(netdev->name, "eth%d");
3801 err = register_netdev(netdev);
3805 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
3806 if (dca_add_requester(&pdev->dev) == 0) {
3807 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
3808 /* always use CB2 mode, difference is masked
3809 * in the CB driver */
3810 IXGBE_WRITE_REG(hw, IXGBE_DCA_CTRL, 2);
3811 ixgbe_setup_dca(adapter);
3815 dev_info(&pdev->dev, "Intel(R) 10 Gigabit Network Connection\n");
3820 ixgbe_release_hw_control(adapter);
3823 ixgbe_reset_interrupt_capability(adapter);
3825 iounmap(hw->hw_addr);
3827 free_netdev(netdev);
3829 pci_release_regions(pdev);
3832 pci_disable_device(pdev);
3837 * ixgbe_remove - Device Removal Routine
3838 * @pdev: PCI device information struct
3840 * ixgbe_remove is called by the PCI subsystem to alert the driver
3841 * that it should release a PCI device. The could be caused by a
3842 * Hot-Plug event, or because the driver is going to be removed from
3845 static void __devexit ixgbe_remove(struct pci_dev *pdev)
3847 struct net_device *netdev = pci_get_drvdata(pdev);
3848 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3850 set_bit(__IXGBE_DOWN, &adapter->state);
3851 del_timer_sync(&adapter->watchdog_timer);
3853 flush_scheduled_work();
3855 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
3856 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
3857 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
3858 dca_remove_requester(&pdev->dev);
3859 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
3863 unregister_netdev(netdev);
3865 ixgbe_reset_interrupt_capability(adapter);
3867 ixgbe_release_hw_control(adapter);
3869 iounmap(adapter->hw.hw_addr);
3870 pci_release_regions(pdev);
3872 DPRINTK(PROBE, INFO, "complete\n");
3873 kfree(adapter->tx_ring);
3874 kfree(adapter->rx_ring);
3876 free_netdev(netdev);
3878 pci_disable_device(pdev);
3882 * ixgbe_io_error_detected - called when PCI error is detected
3883 * @pdev: Pointer to PCI device
3884 * @state: The current pci connection state
3886 * This function is called after a PCI bus error affecting
3887 * this device has been detected.
3889 static pci_ers_result_t ixgbe_io_error_detected(struct pci_dev *pdev,
3890 pci_channel_state_t state)
3892 struct net_device *netdev = pci_get_drvdata(pdev);
3893 struct ixgbe_adapter *adapter = netdev->priv;
3895 netif_device_detach(netdev);
3897 if (netif_running(netdev))
3898 ixgbe_down(adapter);
3899 pci_disable_device(pdev);
3901 /* Request a slot slot reset. */
3902 return PCI_ERS_RESULT_NEED_RESET;
3906 * ixgbe_io_slot_reset - called after the pci bus has been reset.
3907 * @pdev: Pointer to PCI device
3909 * Restart the card from scratch, as if from a cold-boot.
3911 static pci_ers_result_t ixgbe_io_slot_reset(struct pci_dev *pdev)
3913 struct net_device *netdev = pci_get_drvdata(pdev);
3914 struct ixgbe_adapter *adapter = netdev->priv;
3916 if (pci_enable_device(pdev)) {
3918 "Cannot re-enable PCI device after reset.\n");
3919 return PCI_ERS_RESULT_DISCONNECT;
3921 pci_set_master(pdev);
3922 pci_restore_state(pdev);
3924 pci_enable_wake(pdev, PCI_D3hot, 0);
3925 pci_enable_wake(pdev, PCI_D3cold, 0);
3927 ixgbe_reset(adapter);
3929 return PCI_ERS_RESULT_RECOVERED;
3933 * ixgbe_io_resume - called when traffic can start flowing again.
3934 * @pdev: Pointer to PCI device
3936 * This callback is called when the error recovery driver tells us that
3937 * its OK to resume normal operation.
3939 static void ixgbe_io_resume(struct pci_dev *pdev)
3941 struct net_device *netdev = pci_get_drvdata(pdev);
3942 struct ixgbe_adapter *adapter = netdev->priv;
3944 if (netif_running(netdev)) {
3945 if (ixgbe_up(adapter)) {
3946 DPRINTK(PROBE, INFO, "ixgbe_up failed after reset\n");
3951 netif_device_attach(netdev);
3955 static struct pci_error_handlers ixgbe_err_handler = {
3956 .error_detected = ixgbe_io_error_detected,
3957 .slot_reset = ixgbe_io_slot_reset,
3958 .resume = ixgbe_io_resume,
3961 static struct pci_driver ixgbe_driver = {
3962 .name = ixgbe_driver_name,
3963 .id_table = ixgbe_pci_tbl,
3964 .probe = ixgbe_probe,
3965 .remove = __devexit_p(ixgbe_remove),
3967 .suspend = ixgbe_suspend,
3968 .resume = ixgbe_resume,
3970 .shutdown = ixgbe_shutdown,
3971 .err_handler = &ixgbe_err_handler
3975 * ixgbe_init_module - Driver Registration Routine
3977 * ixgbe_init_module is the first routine called when the driver is
3978 * loaded. All it does is register with the PCI subsystem.
3980 static int __init ixgbe_init_module(void)
3983 printk(KERN_INFO "%s: %s - version %s\n", ixgbe_driver_name,
3984 ixgbe_driver_string, ixgbe_driver_version);
3986 printk(KERN_INFO "%s: %s\n", ixgbe_driver_name, ixgbe_copyright);
3988 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
3989 dca_register_notify(&dca_notifier);
3992 ret = pci_register_driver(&ixgbe_driver);
3995 module_init(ixgbe_init_module);
3998 * ixgbe_exit_module - Driver Exit Cleanup Routine
4000 * ixgbe_exit_module is called just before the driver is removed
4003 static void __exit ixgbe_exit_module(void)
4005 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
4006 dca_unregister_notify(&dca_notifier);
4008 pci_unregister_driver(&ixgbe_driver);
4011 #if defined(CONFIG_DCA) || defined(CONFIG_DCA_MODULE)
4012 static int ixgbe_notify_dca(struct notifier_block *nb, unsigned long event,
4017 ret_val = driver_for_each_device(&ixgbe_driver.driver, NULL, &event,
4018 __ixgbe_notify_dca);
4020 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
4022 #endif /* CONFIG_DCA or CONFIG_DCA_MODULE */
4024 module_exit(ixgbe_exit_module);