1 /*******************************************************************************
3 Intel 82599 Virtual Function driver
4 Copyright(c) 1999 - 2012 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 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *******************************************************************************/
29 /******************************************************************************
30 Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
31 ******************************************************************************/
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/types.h>
36 #include <linux/bitops.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/netdevice.h>
40 #include <linux/vmalloc.h>
41 #include <linux/string.h>
44 #include <linux/tcp.h>
45 #include <linux/sctp.h>
46 #include <linux/ipv6.h>
47 #include <linux/slab.h>
48 #include <net/checksum.h>
49 #include <net/ip6_checksum.h>
50 #include <linux/ethtool.h>
52 #include <linux/if_vlan.h>
53 #include <linux/prefetch.h>
57 const char ixgbevf_driver_name[] = "ixgbevf";
58 static const char ixgbevf_driver_string[] =
59 "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
61 #define DRV_VERSION "2.7.12-k"
62 const char ixgbevf_driver_version[] = DRV_VERSION;
63 static char ixgbevf_copyright[] =
64 "Copyright (c) 2009 - 2012 Intel Corporation.";
66 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
67 [board_82599_vf] = &ixgbevf_82599_vf_info,
68 [board_X540_vf] = &ixgbevf_X540_vf_info,
71 /* ixgbevf_pci_tbl - PCI Device ID Table
73 * Wildcard entries (PCI_ANY_ID) should come last
74 * Last entry must be all 0s
76 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
77 * Class, Class Mask, private data (not used) }
79 static struct pci_device_id ixgbevf_pci_tbl[] = {
80 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF),
82 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF),
85 /* required last entry */
88 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
90 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
91 MODULE_DESCRIPTION("Intel(R) 82599 Virtual Function Driver");
92 MODULE_LICENSE("GPL");
93 MODULE_VERSION(DRV_VERSION);
95 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
96 static int debug = -1;
97 module_param(debug, int, 0);
98 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
101 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
102 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
104 static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
105 struct ixgbevf_ring *rx_ring,
109 * Force memory writes to complete before letting h/w
110 * know there are new descriptors to fetch. (Only
111 * applicable for weak-ordered memory model archs,
115 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
119 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
120 * @adapter: pointer to adapter struct
121 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
122 * @queue: queue to map the corresponding interrupt to
123 * @msix_vector: the vector to map to the corresponding queue
125 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
126 u8 queue, u8 msix_vector)
129 struct ixgbe_hw *hw = &adapter->hw;
130 if (direction == -1) {
132 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
133 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
136 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
138 /* tx or rx causes */
139 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
140 index = ((16 * (queue & 1)) + (8 * direction));
141 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
142 ivar &= ~(0xFF << index);
143 ivar |= (msix_vector << index);
144 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
148 static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
149 struct ixgbevf_tx_buffer
152 if (tx_buffer_info->dma) {
153 if (tx_buffer_info->mapped_as_page)
154 dma_unmap_page(tx_ring->dev,
156 tx_buffer_info->length,
159 dma_unmap_single(tx_ring->dev,
161 tx_buffer_info->length,
163 tx_buffer_info->dma = 0;
165 if (tx_buffer_info->skb) {
166 dev_kfree_skb_any(tx_buffer_info->skb);
167 tx_buffer_info->skb = NULL;
169 tx_buffer_info->time_stamp = 0;
170 /* tx_buffer_info must be completely set up in the transmit path */
173 #define IXGBE_MAX_TXD_PWR 14
174 #define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
176 /* Tx Descriptors needed, worst case */
177 #define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
178 #define DESC_NEEDED (MAX_SKB_FRAGS + 4)
180 static void ixgbevf_tx_timeout(struct net_device *netdev);
183 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
184 * @q_vector: board private structure
185 * @tx_ring: tx ring to clean
187 static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
188 struct ixgbevf_ring *tx_ring)
190 struct ixgbevf_adapter *adapter = q_vector->adapter;
191 union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
192 struct ixgbevf_tx_buffer *tx_buffer_info;
193 unsigned int i, eop, count = 0;
194 unsigned int total_bytes = 0, total_packets = 0;
196 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
199 i = tx_ring->next_to_clean;
200 eop = tx_ring->tx_buffer_info[i].next_to_watch;
201 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
203 while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
204 (count < tx_ring->count)) {
205 bool cleaned = false;
206 rmb(); /* read buffer_info after eop_desc */
207 /* eop could change between read and DD-check */
208 if (unlikely(eop != tx_ring->tx_buffer_info[i].next_to_watch))
210 for ( ; !cleaned; count++) {
212 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
213 tx_buffer_info = &tx_ring->tx_buffer_info[i];
214 cleaned = (i == eop);
215 skb = tx_buffer_info->skb;
217 if (cleaned && skb) {
218 unsigned int segs, bytecount;
220 /* gso_segs is currently only valid for tcp */
221 segs = skb_shinfo(skb)->gso_segs ?: 1;
222 /* multiply data chunks by size of headers */
223 bytecount = ((segs - 1) * skb_headlen(skb)) +
225 total_packets += segs;
226 total_bytes += bytecount;
229 ixgbevf_unmap_and_free_tx_resource(tx_ring,
232 tx_desc->wb.status = 0;
235 if (i == tx_ring->count)
240 eop = tx_ring->tx_buffer_info[i].next_to_watch;
241 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
244 tx_ring->next_to_clean = i;
246 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
247 if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
248 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
249 /* Make sure that anybody stopping the queue after this
250 * sees the new next_to_clean.
253 if (__netif_subqueue_stopped(tx_ring->netdev,
254 tx_ring->queue_index) &&
255 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
256 netif_wake_subqueue(tx_ring->netdev,
257 tx_ring->queue_index);
258 ++adapter->restart_queue;
262 u64_stats_update_begin(&tx_ring->syncp);
263 tx_ring->total_bytes += total_bytes;
264 tx_ring->total_packets += total_packets;
265 u64_stats_update_end(&tx_ring->syncp);
266 q_vector->tx.total_bytes += total_bytes;
267 q_vector->tx.total_packets += total_packets;
269 return count < tx_ring->count;
273 * ixgbevf_receive_skb - Send a completed packet up the stack
274 * @q_vector: structure containing interrupt and ring information
275 * @skb: packet to send up
276 * @status: hardware indication of status of receive
277 * @rx_desc: rx descriptor
279 static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
280 struct sk_buff *skb, u8 status,
281 union ixgbe_adv_rx_desc *rx_desc)
283 struct ixgbevf_adapter *adapter = q_vector->adapter;
284 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
285 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
287 if (is_vlan && test_bit(tag & VLAN_VID_MASK, adapter->active_vlans))
288 __vlan_hwaccel_put_tag(skb, tag);
290 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
291 napi_gro_receive(&q_vector->napi, skb);
297 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
298 * @ring: pointer to Rx descriptor ring structure
299 * @status_err: hardware indication of status of receive
300 * @skb: skb currently being received and modified
302 static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
303 u32 status_err, struct sk_buff *skb)
305 skb_checksum_none_assert(skb);
307 /* Rx csum disabled */
308 if (!(ring->netdev->features & NETIF_F_RXCSUM))
311 /* if IP and error */
312 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
313 (status_err & IXGBE_RXDADV_ERR_IPE)) {
314 ring->hw_csum_rx_error++;
318 if (!(status_err & IXGBE_RXD_STAT_L4CS))
321 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
322 ring->hw_csum_rx_error++;
326 /* It must be a TCP or UDP packet with a valid checksum */
327 skb->ip_summed = CHECKSUM_UNNECESSARY;
328 ring->hw_csum_rx_good++;
332 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
333 * @adapter: address of board private structure
335 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
336 struct ixgbevf_ring *rx_ring,
339 struct pci_dev *pdev = adapter->pdev;
340 union ixgbe_adv_rx_desc *rx_desc;
341 struct ixgbevf_rx_buffer *bi;
342 unsigned int i = rx_ring->next_to_use;
344 bi = &rx_ring->rx_buffer_info[i];
346 while (cleaned_count--) {
347 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
352 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
353 rx_ring->rx_buf_len);
355 adapter->alloc_rx_buff_failed++;
360 bi->dma = dma_map_single(&pdev->dev, skb->data,
363 if (dma_mapping_error(&pdev->dev, bi->dma)) {
366 dev_err(&pdev->dev, "RX DMA map failed\n");
370 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
373 if (i == rx_ring->count)
375 bi = &rx_ring->rx_buffer_info[i];
379 if (rx_ring->next_to_use != i) {
380 rx_ring->next_to_use = i;
381 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
385 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
388 struct ixgbe_hw *hw = &adapter->hw;
390 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
393 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
394 struct ixgbevf_ring *rx_ring,
397 struct ixgbevf_adapter *adapter = q_vector->adapter;
398 struct pci_dev *pdev = adapter->pdev;
399 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
400 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
404 int cleaned_count = 0;
405 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
407 i = rx_ring->next_to_clean;
408 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
409 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
410 rx_buffer_info = &rx_ring->rx_buffer_info[i];
412 while (staterr & IXGBE_RXD_STAT_DD) {
417 rmb(); /* read descriptor and rx_buffer_info after status DD */
418 len = le16_to_cpu(rx_desc->wb.upper.length);
419 skb = rx_buffer_info->skb;
420 prefetch(skb->data - NET_IP_ALIGN);
421 rx_buffer_info->skb = NULL;
423 if (rx_buffer_info->dma) {
424 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
427 rx_buffer_info->dma = 0;
432 if (i == rx_ring->count)
435 next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
439 next_buffer = &rx_ring->rx_buffer_info[i];
441 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
442 skb->next = next_buffer->skb;
443 IXGBE_CB(skb->next)->prev = skb;
444 adapter->non_eop_descs++;
448 /* we should not be chaining buffers, if we did drop the skb */
449 if (IXGBE_CB(skb)->prev) {
451 struct sk_buff *this = skb;
452 skb = IXGBE_CB(skb)->prev;
458 /* ERR_MASK will only have valid bits if EOP set */
459 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
460 dev_kfree_skb_irq(skb);
464 ixgbevf_rx_checksum(rx_ring, staterr, skb);
466 /* probably a little skewed due to removing CRC */
467 total_rx_bytes += skb->len;
471 * Work around issue of some types of VM to VM loop back
472 * packets not getting split correctly
474 if (staterr & IXGBE_RXD_STAT_LB) {
475 u32 header_fixup_len = skb_headlen(skb);
476 if (header_fixup_len < 14)
477 skb_push(skb, header_fixup_len);
479 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
481 /* Workaround hardware that can't do proper VEPA multicast
484 if ((skb->pkt_type & (PACKET_BROADCAST | PACKET_MULTICAST)) &&
485 !(compare_ether_addr(adapter->netdev->dev_addr,
486 eth_hdr(skb)->h_source))) {
487 dev_kfree_skb_irq(skb);
491 ixgbevf_receive_skb(q_vector, skb, staterr, rx_desc);
494 rx_desc->wb.upper.status_error = 0;
496 /* return some buffers to hardware, one at a time is too slow */
497 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
498 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
503 /* use prefetched values */
505 rx_buffer_info = &rx_ring->rx_buffer_info[i];
507 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
510 rx_ring->next_to_clean = i;
511 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
514 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
516 u64_stats_update_begin(&rx_ring->syncp);
517 rx_ring->total_packets += total_rx_packets;
518 rx_ring->total_bytes += total_rx_bytes;
519 u64_stats_update_end(&rx_ring->syncp);
520 q_vector->rx.total_packets += total_rx_packets;
521 q_vector->rx.total_bytes += total_rx_bytes;
527 * ixgbevf_poll - NAPI polling calback
528 * @napi: napi struct with our devices info in it
529 * @budget: amount of work driver is allowed to do this pass, in packets
531 * This function will clean more than one or more rings associated with a
534 static int ixgbevf_poll(struct napi_struct *napi, int budget)
536 struct ixgbevf_q_vector *q_vector =
537 container_of(napi, struct ixgbevf_q_vector, napi);
538 struct ixgbevf_adapter *adapter = q_vector->adapter;
539 struct ixgbevf_ring *ring;
541 bool clean_complete = true;
543 ixgbevf_for_each_ring(ring, q_vector->tx)
544 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
546 /* attempt to distribute budget to each queue fairly, but don't allow
547 * the budget to go below 1 because we'll exit polling */
548 if (q_vector->rx.count > 1)
549 per_ring_budget = max(budget/q_vector->rx.count, 1);
551 per_ring_budget = budget;
553 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
554 ixgbevf_for_each_ring(ring, q_vector->rx)
555 clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
557 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
559 /* If all work not completed, return budget and keep polling */
562 /* all work done, exit the polling mode */
564 if (adapter->rx_itr_setting & 1)
565 ixgbevf_set_itr(q_vector);
566 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
567 ixgbevf_irq_enable_queues(adapter,
568 1 << q_vector->v_idx);
574 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
575 * @q_vector: structure containing interrupt and ring information
577 static void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
579 struct ixgbevf_adapter *adapter = q_vector->adapter;
580 struct ixgbe_hw *hw = &adapter->hw;
581 int v_idx = q_vector->v_idx;
582 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
585 * set the WDIS bit to not clear the timer bits and cause an
586 * immediate assertion of the interrupt
588 itr_reg |= IXGBE_EITR_CNT_WDIS;
590 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
594 * ixgbevf_configure_msix - Configure MSI-X hardware
595 * @adapter: board private structure
597 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
600 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
602 struct ixgbevf_q_vector *q_vector;
603 int q_vectors, v_idx;
605 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
606 adapter->eims_enable_mask = 0;
609 * Populate the IVAR table and set the ITR values to the
610 * corresponding register.
612 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
613 struct ixgbevf_ring *ring;
614 q_vector = adapter->q_vector[v_idx];
616 ixgbevf_for_each_ring(ring, q_vector->rx)
617 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
619 ixgbevf_for_each_ring(ring, q_vector->tx)
620 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
622 if (q_vector->tx.ring && !q_vector->rx.ring) {
624 if (adapter->tx_itr_setting == 1)
625 q_vector->itr = IXGBE_10K_ITR;
627 q_vector->itr = adapter->tx_itr_setting;
629 /* rx or rx/tx vector */
630 if (adapter->rx_itr_setting == 1)
631 q_vector->itr = IXGBE_20K_ITR;
633 q_vector->itr = adapter->rx_itr_setting;
636 /* add q_vector eims value to global eims_enable_mask */
637 adapter->eims_enable_mask |= 1 << v_idx;
639 ixgbevf_write_eitr(q_vector);
642 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
643 /* setup eims_other and add value to global eims_enable_mask */
644 adapter->eims_other = 1 << v_idx;
645 adapter->eims_enable_mask |= adapter->eims_other;
652 latency_invalid = 255
656 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
657 * @q_vector: structure containing interrupt and ring information
658 * @ring_container: structure containing ring performance data
660 * Stores a new ITR value based on packets and byte
661 * counts during the last interrupt. The advantage of per interrupt
662 * computation is faster updates and more accurate ITR for the current
663 * traffic pattern. Constants in this function were computed
664 * based on theoretical maximum wire speed and thresholds were set based
665 * on testing data as well as attempting to minimize response time
666 * while increasing bulk throughput.
668 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
669 struct ixgbevf_ring_container *ring_container)
671 int bytes = ring_container->total_bytes;
672 int packets = ring_container->total_packets;
675 u8 itr_setting = ring_container->itr;
680 /* simple throttlerate management
681 * 0-20MB/s lowest (100000 ints/s)
682 * 20-100MB/s low (20000 ints/s)
683 * 100-1249MB/s bulk (8000 ints/s)
685 /* what was last interrupt timeslice? */
686 timepassed_us = q_vector->itr >> 2;
687 bytes_perint = bytes / timepassed_us; /* bytes/usec */
689 switch (itr_setting) {
691 if (bytes_perint > 10)
692 itr_setting = low_latency;
695 if (bytes_perint > 20)
696 itr_setting = bulk_latency;
697 else if (bytes_perint <= 10)
698 itr_setting = lowest_latency;
701 if (bytes_perint <= 20)
702 itr_setting = low_latency;
706 /* clear work counters since we have the values we need */
707 ring_container->total_bytes = 0;
708 ring_container->total_packets = 0;
710 /* write updated itr to ring container */
711 ring_container->itr = itr_setting;
714 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
716 u32 new_itr = q_vector->itr;
719 ixgbevf_update_itr(q_vector, &q_vector->tx);
720 ixgbevf_update_itr(q_vector, &q_vector->rx);
722 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
724 switch (current_itr) {
725 /* counts and packets in update_itr are dependent on these numbers */
727 new_itr = IXGBE_100K_ITR;
730 new_itr = IXGBE_20K_ITR;
734 new_itr = IXGBE_8K_ITR;
738 if (new_itr != q_vector->itr) {
739 /* do an exponential smoothing */
740 new_itr = (10 * new_itr * q_vector->itr) /
741 ((9 * new_itr) + q_vector->itr);
743 /* save the algorithm value here */
744 q_vector->itr = new_itr;
746 ixgbevf_write_eitr(q_vector);
750 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
752 struct ixgbevf_adapter *adapter = data;
753 struct ixgbe_hw *hw = &adapter->hw;
755 hw->mac.get_link_status = 1;
757 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
758 mod_timer(&adapter->watchdog_timer, jiffies);
760 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
766 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
768 * @data: pointer to our q_vector struct for this interrupt vector
770 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
772 struct ixgbevf_q_vector *q_vector = data;
774 /* EIAM disabled interrupts (on this vector) for us */
775 if (q_vector->rx.ring || q_vector->tx.ring)
776 napi_schedule(&q_vector->napi);
781 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
784 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
786 a->rx_ring[r_idx].next = q_vector->rx.ring;
787 q_vector->rx.ring = &a->rx_ring[r_idx];
788 q_vector->rx.count++;
791 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
794 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
796 a->tx_ring[t_idx].next = q_vector->tx.ring;
797 q_vector->tx.ring = &a->tx_ring[t_idx];
798 q_vector->tx.count++;
802 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
803 * @adapter: board private structure to initialize
805 * This function maps descriptor rings to the queue-specific vectors
806 * we were allotted through the MSI-X enabling code. Ideally, we'd have
807 * one vector per ring/queue, but on a constrained vector budget, we
808 * group the rings as "efficiently" as possible. You would add new
809 * mapping configurations in here.
811 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
815 int rxr_idx = 0, txr_idx = 0;
816 int rxr_remaining = adapter->num_rx_queues;
817 int txr_remaining = adapter->num_tx_queues;
822 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
825 * The ideal configuration...
826 * We have enough vectors to map one per queue.
828 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
829 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
830 map_vector_to_rxq(adapter, v_start, rxr_idx);
832 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
833 map_vector_to_txq(adapter, v_start, txr_idx);
838 * If we don't have enough vectors for a 1-to-1
839 * mapping, we'll have to group them so there are
840 * multiple queues per vector.
842 /* Re-adjusting *qpv takes care of the remainder. */
843 for (i = v_start; i < q_vectors; i++) {
844 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
845 for (j = 0; j < rqpv; j++) {
846 map_vector_to_rxq(adapter, i, rxr_idx);
851 for (i = v_start; i < q_vectors; i++) {
852 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
853 for (j = 0; j < tqpv; j++) {
854 map_vector_to_txq(adapter, i, txr_idx);
865 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
866 * @adapter: board private structure
868 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
869 * interrupts from the kernel.
871 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
873 struct net_device *netdev = adapter->netdev;
874 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
878 for (vector = 0; vector < q_vectors; vector++) {
879 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
880 struct msix_entry *entry = &adapter->msix_entries[vector];
882 if (q_vector->tx.ring && q_vector->rx.ring) {
883 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
884 "%s-%s-%d", netdev->name, "TxRx", ri++);
886 } else if (q_vector->rx.ring) {
887 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
888 "%s-%s-%d", netdev->name, "rx", ri++);
889 } else if (q_vector->tx.ring) {
890 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
891 "%s-%s-%d", netdev->name, "tx", ti++);
893 /* skip this unused q_vector */
896 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
897 q_vector->name, q_vector);
900 "request_irq failed for MSIX interrupt "
902 goto free_queue_irqs;
906 err = request_irq(adapter->msix_entries[vector].vector,
907 &ixgbevf_msix_other, 0, netdev->name, adapter);
910 "request_irq for msix_other failed: %d\n", err);
911 goto free_queue_irqs;
919 free_irq(adapter->msix_entries[vector].vector,
920 adapter->q_vector[vector]);
922 pci_disable_msix(adapter->pdev);
923 kfree(adapter->msix_entries);
924 adapter->msix_entries = NULL;
928 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
930 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
932 for (i = 0; i < q_vectors; i++) {
933 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
934 q_vector->rx.ring = NULL;
935 q_vector->tx.ring = NULL;
936 q_vector->rx.count = 0;
937 q_vector->tx.count = 0;
942 * ixgbevf_request_irq - initialize interrupts
943 * @adapter: board private structure
945 * Attempts to configure interrupts using the best available
946 * capabilities of the hardware and kernel.
948 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
952 err = ixgbevf_request_msix_irqs(adapter);
956 "request_irq failed, Error %d\n", err);
961 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
965 q_vectors = adapter->num_msix_vectors;
968 free_irq(adapter->msix_entries[i].vector, adapter);
971 for (; i >= 0; i--) {
972 /* free only the irqs that were actually requested */
973 if (!adapter->q_vector[i]->rx.ring &&
974 !adapter->q_vector[i]->tx.ring)
977 free_irq(adapter->msix_entries[i].vector,
978 adapter->q_vector[i]);
981 ixgbevf_reset_q_vectors(adapter);
985 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
986 * @adapter: board private structure
988 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
990 struct ixgbe_hw *hw = &adapter->hw;
993 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
994 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
995 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
997 IXGBE_WRITE_FLUSH(hw);
999 for (i = 0; i < adapter->num_msix_vectors; i++)
1000 synchronize_irq(adapter->msix_entries[i].vector);
1004 * ixgbevf_irq_enable - Enable default interrupt generation settings
1005 * @adapter: board private structure
1007 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1009 struct ixgbe_hw *hw = &adapter->hw;
1011 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1012 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1013 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1017 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1018 * @adapter: board private structure
1020 * Configure the Tx unit of the MAC after a reset.
1022 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1025 struct ixgbe_hw *hw = &adapter->hw;
1026 u32 i, j, tdlen, txctrl;
1028 /* Setup the HW Tx Head and Tail descriptor pointers */
1029 for (i = 0; i < adapter->num_tx_queues; i++) {
1030 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1033 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1034 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1035 (tdba & DMA_BIT_MASK(32)));
1036 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1037 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1038 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1039 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1040 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1041 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1042 /* Disable Tx Head Writeback RO bit, since this hoses
1043 * bookkeeping if things aren't delivered in order.
1045 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1046 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1047 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1051 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1053 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1055 struct ixgbevf_ring *rx_ring;
1056 struct ixgbe_hw *hw = &adapter->hw;
1059 rx_ring = &adapter->rx_ring[index];
1061 srrctl = IXGBE_SRRCTL_DROP_EN;
1063 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1065 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1066 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1068 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1071 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1073 struct ixgbe_hw *hw = &adapter->hw;
1074 struct net_device *netdev = adapter->netdev;
1075 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1079 /* notify the PF of our intent to use this size of frame */
1080 ixgbevf_rlpml_set_vf(hw, max_frame);
1082 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1083 max_frame += VLAN_HLEN;
1086 * Allocate buffer sizes that fit well into 32K and
1087 * take into account max frame size of 9.5K
1089 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1090 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1091 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1092 else if (max_frame <= IXGBEVF_RXBUFFER_2K)
1093 rx_buf_len = IXGBEVF_RXBUFFER_2K;
1094 else if (max_frame <= IXGBEVF_RXBUFFER_4K)
1095 rx_buf_len = IXGBEVF_RXBUFFER_4K;
1096 else if (max_frame <= IXGBEVF_RXBUFFER_8K)
1097 rx_buf_len = IXGBEVF_RXBUFFER_8K;
1099 rx_buf_len = IXGBEVF_RXBUFFER_10K;
1101 for (i = 0; i < adapter->num_rx_queues; i++)
1102 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1106 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1107 * @adapter: board private structure
1109 * Configure the Rx unit of the MAC after a reset.
1111 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1114 struct ixgbe_hw *hw = &adapter->hw;
1118 /* PSRTYPE must be initialized in 82599 */
1119 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
1121 /* set_rx_buffer_len must be called before ring initialization */
1122 ixgbevf_set_rx_buffer_len(adapter);
1124 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1125 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1126 * the Base and Length of the Rx Descriptor Ring */
1127 for (i = 0; i < adapter->num_rx_queues; i++) {
1128 rdba = adapter->rx_ring[i].dma;
1129 j = adapter->rx_ring[i].reg_idx;
1130 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1131 (rdba & DMA_BIT_MASK(32)));
1132 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1133 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1134 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1135 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1136 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1137 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1139 ixgbevf_configure_srrctl(adapter, j);
1143 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1145 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1146 struct ixgbe_hw *hw = &adapter->hw;
1149 spin_lock_bh(&adapter->mbx_lock);
1151 /* add VID to filter table */
1152 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
1154 spin_unlock_bh(&adapter->mbx_lock);
1156 /* translate error return types so error makes sense */
1157 if (err == IXGBE_ERR_MBX)
1160 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1163 set_bit(vid, adapter->active_vlans);
1168 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1170 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1171 struct ixgbe_hw *hw = &adapter->hw;
1172 int err = -EOPNOTSUPP;
1174 spin_lock_bh(&adapter->mbx_lock);
1176 /* remove VID from filter table */
1177 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
1179 spin_unlock_bh(&adapter->mbx_lock);
1181 clear_bit(vid, adapter->active_vlans);
1186 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1190 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1191 ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
1194 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1196 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1197 struct ixgbe_hw *hw = &adapter->hw;
1200 if ((netdev_uc_count(netdev)) > 10) {
1201 pr_err("Too many unicast filters - No Space\n");
1205 if (!netdev_uc_empty(netdev)) {
1206 struct netdev_hw_addr *ha;
1207 netdev_for_each_uc_addr(ha, netdev) {
1208 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1213 * If the list is empty then send message to PF driver to
1214 * clear all macvlans on this VF.
1216 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1223 * ixgbevf_set_rx_mode - Multicast and unicast set
1224 * @netdev: network interface device structure
1226 * The set_rx_method entry point is called whenever the multicast address
1227 * list, unicast address list or the network interface flags are updated.
1228 * This routine is responsible for configuring the hardware for proper
1229 * multicast mode and configuring requested unicast filters.
1231 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1233 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1234 struct ixgbe_hw *hw = &adapter->hw;
1236 spin_lock_bh(&adapter->mbx_lock);
1238 /* reprogram multicast list */
1239 hw->mac.ops.update_mc_addr_list(hw, netdev);
1241 ixgbevf_write_uc_addr_list(netdev);
1243 spin_unlock_bh(&adapter->mbx_lock);
1246 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1249 struct ixgbevf_q_vector *q_vector;
1250 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1252 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1253 q_vector = adapter->q_vector[q_idx];
1254 napi_enable(&q_vector->napi);
1258 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1261 struct ixgbevf_q_vector *q_vector;
1262 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1264 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1265 q_vector = adapter->q_vector[q_idx];
1266 napi_disable(&q_vector->napi);
1270 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1272 struct net_device *netdev = adapter->netdev;
1275 ixgbevf_set_rx_mode(netdev);
1277 ixgbevf_restore_vlan(adapter);
1279 ixgbevf_configure_tx(adapter);
1280 ixgbevf_configure_rx(adapter);
1281 for (i = 0; i < adapter->num_rx_queues; i++) {
1282 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1283 ixgbevf_alloc_rx_buffers(adapter, ring,
1284 IXGBE_DESC_UNUSED(ring));
1288 #define IXGBE_MAX_RX_DESC_POLL 10
1289 static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1292 struct ixgbe_hw *hw = &adapter->hw;
1293 int j = adapter->rx_ring[rxr].reg_idx;
1296 for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1297 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1302 if (k >= IXGBE_MAX_RX_DESC_POLL) {
1303 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1304 "not set within the polling period\n", rxr);
1307 ixgbevf_release_rx_desc(hw, &adapter->rx_ring[rxr],
1308 adapter->rx_ring[rxr].count - 1);
1311 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1313 /* Only save pre-reset stats if there are some */
1314 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1315 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1316 adapter->stats.base_vfgprc;
1317 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1318 adapter->stats.base_vfgptc;
1319 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1320 adapter->stats.base_vfgorc;
1321 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1322 adapter->stats.base_vfgotc;
1323 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1324 adapter->stats.base_vfmprc;
1328 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1330 struct ixgbe_hw *hw = &adapter->hw;
1332 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1333 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1334 adapter->stats.last_vfgorc |=
1335 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1336 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1337 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1338 adapter->stats.last_vfgotc |=
1339 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1340 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1342 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1343 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1344 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1345 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1346 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1349 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1351 struct ixgbe_hw *hw = &adapter->hw;
1352 int api[] = { ixgbe_mbox_api_11,
1354 ixgbe_mbox_api_unknown };
1355 int err = 0, idx = 0;
1357 spin_lock_bh(&adapter->mbx_lock);
1359 while (api[idx] != ixgbe_mbox_api_unknown) {
1360 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1366 spin_unlock_bh(&adapter->mbx_lock);
1369 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1371 struct net_device *netdev = adapter->netdev;
1372 struct ixgbe_hw *hw = &adapter->hw;
1374 int num_rx_rings = adapter->num_rx_queues;
1377 for (i = 0; i < adapter->num_tx_queues; i++) {
1378 j = adapter->tx_ring[i].reg_idx;
1379 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1380 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1381 txdctl |= (8 << 16);
1382 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1385 for (i = 0; i < adapter->num_tx_queues; i++) {
1386 j = adapter->tx_ring[i].reg_idx;
1387 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1388 txdctl |= IXGBE_TXDCTL_ENABLE;
1389 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1392 for (i = 0; i < num_rx_rings; i++) {
1393 j = adapter->rx_ring[i].reg_idx;
1394 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1395 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1396 if (hw->mac.type == ixgbe_mac_X540_vf) {
1397 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1398 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1399 IXGBE_RXDCTL_RLPML_EN);
1401 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1402 ixgbevf_rx_desc_queue_enable(adapter, i);
1405 ixgbevf_configure_msix(adapter);
1407 spin_lock_bh(&adapter->mbx_lock);
1409 if (is_valid_ether_addr(hw->mac.addr))
1410 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1412 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1414 spin_unlock_bh(&adapter->mbx_lock);
1416 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1417 ixgbevf_napi_enable_all(adapter);
1419 /* enable transmits */
1420 netif_tx_start_all_queues(netdev);
1422 ixgbevf_save_reset_stats(adapter);
1423 ixgbevf_init_last_counter_stats(adapter);
1425 hw->mac.get_link_status = 1;
1426 mod_timer(&adapter->watchdog_timer, jiffies);
1429 static int ixgbevf_reset_queues(struct ixgbevf_adapter *adapter)
1431 struct ixgbe_hw *hw = &adapter->hw;
1432 struct ixgbevf_ring *rx_ring;
1433 unsigned int def_q = 0;
1434 unsigned int num_tcs = 0;
1435 unsigned int num_rx_queues = 1;
1438 spin_lock_bh(&adapter->mbx_lock);
1440 /* fetch queue configuration from the PF */
1441 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1443 spin_unlock_bh(&adapter->mbx_lock);
1449 /* update default Tx ring register index */
1450 adapter->tx_ring[0].reg_idx = def_q;
1452 /* we need as many queues as traffic classes */
1453 num_rx_queues = num_tcs;
1456 /* nothing to do if we have the correct number of queues */
1457 if (adapter->num_rx_queues == num_rx_queues)
1460 /* allocate new rings */
1461 rx_ring = kcalloc(num_rx_queues,
1462 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1466 /* setup ring fields */
1467 for (i = 0; i < num_rx_queues; i++) {
1468 rx_ring[i].count = adapter->rx_ring_count;
1469 rx_ring[i].queue_index = i;
1470 rx_ring[i].reg_idx = i;
1471 rx_ring[i].dev = &adapter->pdev->dev;
1472 rx_ring[i].netdev = adapter->netdev;
1474 /* allocate resources on the ring */
1475 err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
1479 ixgbevf_free_rx_resources(adapter, &rx_ring[i]);
1486 /* free the existing rings and queues */
1487 ixgbevf_free_all_rx_resources(adapter);
1488 adapter->num_rx_queues = 0;
1489 kfree(adapter->rx_ring);
1491 /* move new rings into position on the adapter struct */
1492 adapter->rx_ring = rx_ring;
1493 adapter->num_rx_queues = num_rx_queues;
1495 /* reset ring to vector mapping */
1496 ixgbevf_reset_q_vectors(adapter);
1497 ixgbevf_map_rings_to_vectors(adapter);
1502 void ixgbevf_up(struct ixgbevf_adapter *adapter)
1504 struct ixgbe_hw *hw = &adapter->hw;
1506 ixgbevf_negotiate_api(adapter);
1508 ixgbevf_reset_queues(adapter);
1510 ixgbevf_configure(adapter);
1512 ixgbevf_up_complete(adapter);
1514 /* clear any pending interrupts, may auto mask */
1515 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1517 ixgbevf_irq_enable(adapter);
1521 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1522 * @adapter: board private structure
1523 * @rx_ring: ring to free buffers from
1525 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1526 struct ixgbevf_ring *rx_ring)
1528 struct pci_dev *pdev = adapter->pdev;
1532 if (!rx_ring->rx_buffer_info)
1535 /* Free all the Rx ring sk_buffs */
1536 for (i = 0; i < rx_ring->count; i++) {
1537 struct ixgbevf_rx_buffer *rx_buffer_info;
1539 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1540 if (rx_buffer_info->dma) {
1541 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1542 rx_ring->rx_buf_len,
1544 rx_buffer_info->dma = 0;
1546 if (rx_buffer_info->skb) {
1547 struct sk_buff *skb = rx_buffer_info->skb;
1548 rx_buffer_info->skb = NULL;
1550 struct sk_buff *this = skb;
1551 skb = IXGBE_CB(skb)->prev;
1552 dev_kfree_skb(this);
1557 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1558 memset(rx_ring->rx_buffer_info, 0, size);
1560 /* Zero out the descriptor ring */
1561 memset(rx_ring->desc, 0, rx_ring->size);
1563 rx_ring->next_to_clean = 0;
1564 rx_ring->next_to_use = 0;
1567 writel(0, adapter->hw.hw_addr + rx_ring->head);
1569 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1573 * ixgbevf_clean_tx_ring - Free Tx Buffers
1574 * @adapter: board private structure
1575 * @tx_ring: ring to be cleaned
1577 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1578 struct ixgbevf_ring *tx_ring)
1580 struct ixgbevf_tx_buffer *tx_buffer_info;
1584 if (!tx_ring->tx_buffer_info)
1587 /* Free all the Tx ring sk_buffs */
1588 for (i = 0; i < tx_ring->count; i++) {
1589 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1590 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
1593 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1594 memset(tx_ring->tx_buffer_info, 0, size);
1596 memset(tx_ring->desc, 0, tx_ring->size);
1598 tx_ring->next_to_use = 0;
1599 tx_ring->next_to_clean = 0;
1602 writel(0, adapter->hw.hw_addr + tx_ring->head);
1604 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1608 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1609 * @adapter: board private structure
1611 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1615 for (i = 0; i < adapter->num_rx_queues; i++)
1616 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1620 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1621 * @adapter: board private structure
1623 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1627 for (i = 0; i < adapter->num_tx_queues; i++)
1628 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1631 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1633 struct net_device *netdev = adapter->netdev;
1634 struct ixgbe_hw *hw = &adapter->hw;
1638 /* signal that we are down to the interrupt handler */
1639 set_bit(__IXGBEVF_DOWN, &adapter->state);
1640 /* disable receives */
1642 netif_tx_disable(netdev);
1646 netif_tx_stop_all_queues(netdev);
1648 ixgbevf_irq_disable(adapter);
1650 ixgbevf_napi_disable_all(adapter);
1652 del_timer_sync(&adapter->watchdog_timer);
1653 /* can't call flush scheduled work here because it can deadlock
1654 * if linkwatch_event tries to acquire the rtnl_lock which we are
1656 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1659 /* disable transmits in the hardware now that interrupts are off */
1660 for (i = 0; i < adapter->num_tx_queues; i++) {
1661 j = adapter->tx_ring[i].reg_idx;
1662 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1663 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1664 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1667 netif_carrier_off(netdev);
1669 if (!pci_channel_offline(adapter->pdev))
1670 ixgbevf_reset(adapter);
1672 ixgbevf_clean_all_tx_rings(adapter);
1673 ixgbevf_clean_all_rx_rings(adapter);
1676 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1678 WARN_ON(in_interrupt());
1680 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1683 ixgbevf_down(adapter);
1684 ixgbevf_up(adapter);
1686 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1689 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1691 struct ixgbe_hw *hw = &adapter->hw;
1692 struct net_device *netdev = adapter->netdev;
1694 if (hw->mac.ops.reset_hw(hw))
1695 hw_dbg(hw, "PF still resetting\n");
1697 hw->mac.ops.init_hw(hw);
1699 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1700 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1702 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1707 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1711 int vector_threshold;
1713 /* We'll want at least 2 (vector_threshold):
1714 * 1) TxQ[0] + RxQ[0] handler
1715 * 2) Other (Link Status Change, etc.)
1717 vector_threshold = MIN_MSIX_COUNT;
1719 /* The more we get, the more we will assign to Tx/Rx Cleanup
1720 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1721 * Right now, we simply care about how many we'll get; we'll
1722 * set them up later while requesting irq's.
1724 while (vectors >= vector_threshold) {
1725 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1727 if (!err || err < 0) /* Success or a nasty failure. */
1729 else /* err == number of vectors we should try again with */
1733 if (vectors < vector_threshold)
1737 dev_err(&adapter->pdev->dev,
1738 "Unable to allocate MSI-X interrupts\n");
1739 kfree(adapter->msix_entries);
1740 adapter->msix_entries = NULL;
1743 * Adjust for only the vectors we'll use, which is minimum
1744 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1745 * vectors we were allocated.
1747 adapter->num_msix_vectors = vectors;
1754 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
1755 * @adapter: board private structure to initialize
1757 * This is the top level queue allocation routine. The order here is very
1758 * important, starting with the "most" number of features turned on at once,
1759 * and ending with the smallest set of features. This way large combinations
1760 * can be allocated if they're turned on, and smaller combinations are the
1761 * fallthrough conditions.
1764 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1766 /* Start with base case */
1767 adapter->num_rx_queues = 1;
1768 adapter->num_tx_queues = 1;
1772 * ixgbevf_alloc_queues - Allocate memory for all rings
1773 * @adapter: board private structure to initialize
1775 * We allocate one ring per queue at run-time since we don't know the
1776 * number of queues at compile-time. The polling_netdev array is
1777 * intended for Multiqueue, but should work fine with a single queue.
1779 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1783 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1784 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1785 if (!adapter->tx_ring)
1786 goto err_tx_ring_allocation;
1788 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1789 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1790 if (!adapter->rx_ring)
1791 goto err_rx_ring_allocation;
1793 for (i = 0; i < adapter->num_tx_queues; i++) {
1794 adapter->tx_ring[i].count = adapter->tx_ring_count;
1795 adapter->tx_ring[i].queue_index = i;
1796 /* reg_idx may be remapped later by DCB config */
1797 adapter->tx_ring[i].reg_idx = i;
1798 adapter->tx_ring[i].dev = &adapter->pdev->dev;
1799 adapter->tx_ring[i].netdev = adapter->netdev;
1802 for (i = 0; i < adapter->num_rx_queues; i++) {
1803 adapter->rx_ring[i].count = adapter->rx_ring_count;
1804 adapter->rx_ring[i].queue_index = i;
1805 adapter->rx_ring[i].reg_idx = i;
1806 adapter->rx_ring[i].dev = &adapter->pdev->dev;
1807 adapter->rx_ring[i].netdev = adapter->netdev;
1812 err_rx_ring_allocation:
1813 kfree(adapter->tx_ring);
1814 err_tx_ring_allocation:
1819 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1820 * @adapter: board private structure to initialize
1822 * Attempt to configure the interrupts using the best available
1823 * capabilities of the hardware and the kernel.
1825 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1827 struct net_device *netdev = adapter->netdev;
1829 int vector, v_budget;
1832 * It's easy to be greedy for MSI-X vectors, but it really
1833 * doesn't do us much good if we have a lot more vectors
1834 * than CPU's. So let's be conservative and only ask for
1835 * (roughly) the same number of vectors as there are CPU's.
1836 * The default is to use pairs of vectors.
1838 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
1839 v_budget = min_t(int, v_budget, num_online_cpus());
1840 v_budget += NON_Q_VECTORS;
1842 /* A failure in MSI-X entry allocation isn't fatal, but it does
1843 * mean we disable MSI-X capabilities of the adapter. */
1844 adapter->msix_entries = kcalloc(v_budget,
1845 sizeof(struct msix_entry), GFP_KERNEL);
1846 if (!adapter->msix_entries) {
1851 for (vector = 0; vector < v_budget; vector++)
1852 adapter->msix_entries[vector].entry = vector;
1854 err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
1858 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
1862 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
1869 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1870 * @adapter: board private structure to initialize
1872 * We allocate one q_vector per queue interrupt. If allocation fails we
1875 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1877 int q_idx, num_q_vectors;
1878 struct ixgbevf_q_vector *q_vector;
1880 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1882 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1883 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1886 q_vector->adapter = adapter;
1887 q_vector->v_idx = q_idx;
1888 netif_napi_add(adapter->netdev, &q_vector->napi,
1890 adapter->q_vector[q_idx] = q_vector;
1898 q_vector = adapter->q_vector[q_idx];
1899 netif_napi_del(&q_vector->napi);
1901 adapter->q_vector[q_idx] = NULL;
1907 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1908 * @adapter: board private structure to initialize
1910 * This function frees the memory allocated to the q_vectors. In addition if
1911 * NAPI is enabled it will delete any references to the NAPI struct prior
1912 * to freeing the q_vector.
1914 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1916 int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1918 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1919 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1921 adapter->q_vector[q_idx] = NULL;
1922 netif_napi_del(&q_vector->napi);
1928 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1929 * @adapter: board private structure
1932 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1934 pci_disable_msix(adapter->pdev);
1935 kfree(adapter->msix_entries);
1936 adapter->msix_entries = NULL;
1940 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1941 * @adapter: board private structure to initialize
1944 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
1948 /* Number of supported queues */
1949 ixgbevf_set_num_queues(adapter);
1951 err = ixgbevf_set_interrupt_capability(adapter);
1953 hw_dbg(&adapter->hw,
1954 "Unable to setup interrupt capabilities\n");
1955 goto err_set_interrupt;
1958 err = ixgbevf_alloc_q_vectors(adapter);
1960 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
1962 goto err_alloc_q_vectors;
1965 err = ixgbevf_alloc_queues(adapter);
1967 pr_err("Unable to allocate memory for queues\n");
1968 goto err_alloc_queues;
1971 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
1972 "Tx Queue count = %u\n",
1973 (adapter->num_rx_queues > 1) ? "Enabled" :
1974 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
1976 set_bit(__IXGBEVF_DOWN, &adapter->state);
1980 ixgbevf_free_q_vectors(adapter);
1981 err_alloc_q_vectors:
1982 ixgbevf_reset_interrupt_capability(adapter);
1988 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
1989 * @adapter: board private structure to clear interrupt scheme on
1991 * We go through and clear interrupt specific resources and reset the structure
1992 * to pre-load conditions
1994 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
1996 adapter->num_tx_queues = 0;
1997 adapter->num_rx_queues = 0;
1999 ixgbevf_free_q_vectors(adapter);
2000 ixgbevf_reset_interrupt_capability(adapter);
2004 * ixgbevf_sw_init - Initialize general software structures
2005 * (struct ixgbevf_adapter)
2006 * @adapter: board private structure to initialize
2008 * ixgbevf_sw_init initializes the Adapter private data structure.
2009 * Fields are initialized based on PCI device information and
2010 * OS network device settings (MTU size).
2012 static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2014 struct ixgbe_hw *hw = &adapter->hw;
2015 struct pci_dev *pdev = adapter->pdev;
2018 /* PCI config space info */
2020 hw->vendor_id = pdev->vendor;
2021 hw->device_id = pdev->device;
2022 hw->revision_id = pdev->revision;
2023 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2024 hw->subsystem_device_id = pdev->subsystem_device;
2026 hw->mbx.ops.init_params(hw);
2028 /* assume legacy case in which PF would only give VF 2 queues */
2029 hw->mac.max_tx_queues = 2;
2030 hw->mac.max_rx_queues = 2;
2032 err = hw->mac.ops.reset_hw(hw);
2034 dev_info(&pdev->dev,
2035 "PF still in reset state, assigning new address\n");
2036 eth_hw_addr_random(adapter->netdev);
2037 memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
2038 adapter->netdev->addr_len);
2040 err = hw->mac.ops.init_hw(hw);
2042 pr_err("init_shared_code failed: %d\n", err);
2045 memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
2046 adapter->netdev->addr_len);
2049 /* lock to protect mailbox accesses */
2050 spin_lock_init(&adapter->mbx_lock);
2052 /* Enable dynamic interrupt throttling rates */
2053 adapter->rx_itr_setting = 1;
2054 adapter->tx_itr_setting = 1;
2056 /* set default ring sizes */
2057 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2058 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2060 set_bit(__IXGBEVF_DOWN, &adapter->state);
2067 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2069 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2070 if (current_counter < last_counter) \
2071 counter += 0x100000000LL; \
2072 last_counter = current_counter; \
2073 counter &= 0xFFFFFFFF00000000LL; \
2074 counter |= current_counter; \
2077 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2079 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2080 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2081 u64 current_counter = (current_counter_msb << 32) | \
2082 current_counter_lsb; \
2083 if (current_counter < last_counter) \
2084 counter += 0x1000000000LL; \
2085 last_counter = current_counter; \
2086 counter &= 0xFFFFFFF000000000LL; \
2087 counter |= current_counter; \
2090 * ixgbevf_update_stats - Update the board statistics counters.
2091 * @adapter: board private structure
2093 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2095 struct ixgbe_hw *hw = &adapter->hw;
2098 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2099 adapter->stats.vfgprc);
2100 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2101 adapter->stats.vfgptc);
2102 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2103 adapter->stats.last_vfgorc,
2104 adapter->stats.vfgorc);
2105 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2106 adapter->stats.last_vfgotc,
2107 adapter->stats.vfgotc);
2108 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2109 adapter->stats.vfmprc);
2111 for (i = 0; i < adapter->num_rx_queues; i++) {
2112 adapter->hw_csum_rx_error +=
2113 adapter->rx_ring[i].hw_csum_rx_error;
2114 adapter->hw_csum_rx_good +=
2115 adapter->rx_ring[i].hw_csum_rx_good;
2116 adapter->rx_ring[i].hw_csum_rx_error = 0;
2117 adapter->rx_ring[i].hw_csum_rx_good = 0;
2122 * ixgbevf_watchdog - Timer Call-back
2123 * @data: pointer to adapter cast into an unsigned long
2125 static void ixgbevf_watchdog(unsigned long data)
2127 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2128 struct ixgbe_hw *hw = &adapter->hw;
2133 * Do the watchdog outside of interrupt context due to the lovely
2134 * delays that some of the newer hardware requires
2137 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2138 goto watchdog_short_circuit;
2140 /* get one bit for every active tx/rx interrupt vector */
2141 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2142 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2143 if (qv->rx.ring || qv->tx.ring)
2147 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
2149 watchdog_short_circuit:
2150 schedule_work(&adapter->watchdog_task);
2154 * ixgbevf_tx_timeout - Respond to a Tx Hang
2155 * @netdev: network interface device structure
2157 static void ixgbevf_tx_timeout(struct net_device *netdev)
2159 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2161 /* Do the reset outside of interrupt context */
2162 schedule_work(&adapter->reset_task);
2165 static void ixgbevf_reset_task(struct work_struct *work)
2167 struct ixgbevf_adapter *adapter;
2168 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2170 /* If we're already down or resetting, just bail */
2171 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2172 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2175 adapter->tx_timeout_count++;
2177 ixgbevf_reinit_locked(adapter);
2181 * ixgbevf_watchdog_task - worker thread to bring link up
2182 * @work: pointer to work_struct containing our data
2184 static void ixgbevf_watchdog_task(struct work_struct *work)
2186 struct ixgbevf_adapter *adapter = container_of(work,
2187 struct ixgbevf_adapter,
2189 struct net_device *netdev = adapter->netdev;
2190 struct ixgbe_hw *hw = &adapter->hw;
2191 u32 link_speed = adapter->link_speed;
2192 bool link_up = adapter->link_up;
2195 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2198 * Always check the link on the watchdog because we have
2201 spin_lock_bh(&adapter->mbx_lock);
2203 need_reset = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
2205 spin_unlock_bh(&adapter->mbx_lock);
2208 adapter->link_up = link_up;
2209 adapter->link_speed = link_speed;
2210 netif_carrier_off(netdev);
2211 netif_tx_stop_all_queues(netdev);
2212 schedule_work(&adapter->reset_task);
2215 adapter->link_up = link_up;
2216 adapter->link_speed = link_speed;
2219 if (!netif_carrier_ok(netdev)) {
2220 hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
2221 (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2223 netif_carrier_on(netdev);
2224 netif_tx_wake_all_queues(netdev);
2227 adapter->link_up = false;
2228 adapter->link_speed = 0;
2229 if (netif_carrier_ok(netdev)) {
2230 hw_dbg(&adapter->hw, "NIC Link is Down\n");
2231 netif_carrier_off(netdev);
2232 netif_tx_stop_all_queues(netdev);
2236 ixgbevf_update_stats(adapter);
2239 /* Reset the timer */
2240 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2241 mod_timer(&adapter->watchdog_timer,
2242 round_jiffies(jiffies + (2 * HZ)));
2244 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2248 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2249 * @adapter: board private structure
2250 * @tx_ring: Tx descriptor ring for a specific queue
2252 * Free all transmit software resources
2254 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2255 struct ixgbevf_ring *tx_ring)
2257 struct pci_dev *pdev = adapter->pdev;
2259 ixgbevf_clean_tx_ring(adapter, tx_ring);
2261 vfree(tx_ring->tx_buffer_info);
2262 tx_ring->tx_buffer_info = NULL;
2264 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2267 tx_ring->desc = NULL;
2271 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2272 * @adapter: board private structure
2274 * Free all transmit software resources
2276 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2280 for (i = 0; i < adapter->num_tx_queues; i++)
2281 if (adapter->tx_ring[i].desc)
2282 ixgbevf_free_tx_resources(adapter,
2283 &adapter->tx_ring[i]);
2288 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2289 * @adapter: board private structure
2290 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2292 * Return 0 on success, negative on failure
2294 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2295 struct ixgbevf_ring *tx_ring)
2297 struct pci_dev *pdev = adapter->pdev;
2300 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2301 tx_ring->tx_buffer_info = vzalloc(size);
2302 if (!tx_ring->tx_buffer_info)
2305 /* round up to nearest 4K */
2306 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2307 tx_ring->size = ALIGN(tx_ring->size, 4096);
2309 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2310 &tx_ring->dma, GFP_KERNEL);
2314 tx_ring->next_to_use = 0;
2315 tx_ring->next_to_clean = 0;
2319 vfree(tx_ring->tx_buffer_info);
2320 tx_ring->tx_buffer_info = NULL;
2321 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2322 "descriptor ring\n");
2327 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2328 * @adapter: board private structure
2330 * If this function returns with an error, then it's possible one or
2331 * more of the rings is populated (while the rest are not). It is the
2332 * callers duty to clean those orphaned rings.
2334 * Return 0 on success, negative on failure
2336 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2340 for (i = 0; i < adapter->num_tx_queues; i++) {
2341 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2344 hw_dbg(&adapter->hw,
2345 "Allocation for Tx Queue %u failed\n", i);
2353 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2354 * @adapter: board private structure
2355 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2357 * Returns 0 on success, negative on failure
2359 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2360 struct ixgbevf_ring *rx_ring)
2362 struct pci_dev *pdev = adapter->pdev;
2365 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2366 rx_ring->rx_buffer_info = vzalloc(size);
2367 if (!rx_ring->rx_buffer_info)
2370 /* Round up to nearest 4K */
2371 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2372 rx_ring->size = ALIGN(rx_ring->size, 4096);
2374 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2375 &rx_ring->dma, GFP_KERNEL);
2377 if (!rx_ring->desc) {
2378 hw_dbg(&adapter->hw,
2379 "Unable to allocate memory for "
2380 "the receive descriptor ring\n");
2381 vfree(rx_ring->rx_buffer_info);
2382 rx_ring->rx_buffer_info = NULL;
2386 rx_ring->next_to_clean = 0;
2387 rx_ring->next_to_use = 0;
2395 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2396 * @adapter: board private structure
2398 * If this function returns with an error, then it's possible one or
2399 * more of the rings is populated (while the rest are not). It is the
2400 * callers duty to clean those orphaned rings.
2402 * Return 0 on success, negative on failure
2404 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2408 for (i = 0; i < adapter->num_rx_queues; i++) {
2409 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2412 hw_dbg(&adapter->hw,
2413 "Allocation for Rx Queue %u failed\n", i);
2420 * ixgbevf_free_rx_resources - Free Rx Resources
2421 * @adapter: board private structure
2422 * @rx_ring: ring to clean the resources from
2424 * Free all receive software resources
2426 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2427 struct ixgbevf_ring *rx_ring)
2429 struct pci_dev *pdev = adapter->pdev;
2431 ixgbevf_clean_rx_ring(adapter, rx_ring);
2433 vfree(rx_ring->rx_buffer_info);
2434 rx_ring->rx_buffer_info = NULL;
2436 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2439 rx_ring->desc = NULL;
2443 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2444 * @adapter: board private structure
2446 * Free all receive software resources
2448 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2452 for (i = 0; i < adapter->num_rx_queues; i++)
2453 if (adapter->rx_ring[i].desc)
2454 ixgbevf_free_rx_resources(adapter,
2455 &adapter->rx_ring[i]);
2458 static int ixgbevf_setup_queues(struct ixgbevf_adapter *adapter)
2460 struct ixgbe_hw *hw = &adapter->hw;
2461 struct ixgbevf_ring *rx_ring;
2462 unsigned int def_q = 0;
2463 unsigned int num_tcs = 0;
2464 unsigned int num_rx_queues = 1;
2467 spin_lock_bh(&adapter->mbx_lock);
2469 /* fetch queue configuration from the PF */
2470 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2472 spin_unlock_bh(&adapter->mbx_lock);
2478 /* update default Tx ring register index */
2479 adapter->tx_ring[0].reg_idx = def_q;
2481 /* we need as many queues as traffic classes */
2482 num_rx_queues = num_tcs;
2485 /* nothing to do if we have the correct number of queues */
2486 if (adapter->num_rx_queues == num_rx_queues)
2489 /* allocate new rings */
2490 rx_ring = kcalloc(num_rx_queues,
2491 sizeof(struct ixgbevf_ring), GFP_KERNEL);
2495 /* setup ring fields */
2496 for (i = 0; i < num_rx_queues; i++) {
2497 rx_ring[i].count = adapter->rx_ring_count;
2498 rx_ring[i].queue_index = i;
2499 rx_ring[i].reg_idx = i;
2500 rx_ring[i].dev = &adapter->pdev->dev;
2501 rx_ring[i].netdev = adapter->netdev;
2504 /* free the existing ring and queues */
2505 adapter->num_rx_queues = 0;
2506 kfree(adapter->rx_ring);
2508 /* move new rings into position on the adapter struct */
2509 adapter->rx_ring = rx_ring;
2510 adapter->num_rx_queues = num_rx_queues;
2516 * ixgbevf_open - Called when a network interface is made active
2517 * @netdev: network interface device structure
2519 * Returns 0 on success, negative value on failure
2521 * The open entry point is called when a network interface is made
2522 * active by the system (IFF_UP). At this point all resources needed
2523 * for transmit and receive operations are allocated, the interrupt
2524 * handler is registered with the OS, the watchdog timer is started,
2525 * and the stack is notified that the interface is ready.
2527 static int ixgbevf_open(struct net_device *netdev)
2529 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2530 struct ixgbe_hw *hw = &adapter->hw;
2533 /* disallow open during test */
2534 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2537 if (hw->adapter_stopped) {
2538 ixgbevf_reset(adapter);
2539 /* if adapter is still stopped then PF isn't up and
2540 * the vf can't start. */
2541 if (hw->adapter_stopped) {
2542 err = IXGBE_ERR_MBX;
2543 pr_err("Unable to start - perhaps the PF Driver isn't "
2545 goto err_setup_reset;
2549 ixgbevf_negotiate_api(adapter);
2551 /* setup queue reg_idx and Rx queue count */
2552 err = ixgbevf_setup_queues(adapter);
2554 goto err_setup_queues;
2556 /* allocate transmit descriptors */
2557 err = ixgbevf_setup_all_tx_resources(adapter);
2561 /* allocate receive descriptors */
2562 err = ixgbevf_setup_all_rx_resources(adapter);
2566 ixgbevf_configure(adapter);
2569 * Map the Tx/Rx rings to the vectors we were allotted.
2570 * if request_irq will be called in this function map_rings
2571 * must be called *before* up_complete
2573 ixgbevf_map_rings_to_vectors(adapter);
2575 ixgbevf_up_complete(adapter);
2577 /* clear any pending interrupts, may auto mask */
2578 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2579 err = ixgbevf_request_irq(adapter);
2583 ixgbevf_irq_enable(adapter);
2588 ixgbevf_down(adapter);
2589 ixgbevf_free_irq(adapter);
2591 ixgbevf_free_all_rx_resources(adapter);
2593 ixgbevf_free_all_tx_resources(adapter);
2595 ixgbevf_reset(adapter);
2603 * ixgbevf_close - Disables a network interface
2604 * @netdev: network interface device structure
2606 * Returns 0, this is not allowed to fail
2608 * The close entry point is called when an interface is de-activated
2609 * by the OS. The hardware is still under the drivers control, but
2610 * needs to be disabled. A global MAC reset is issued to stop the
2611 * hardware, and all transmit and receive resources are freed.
2613 static int ixgbevf_close(struct net_device *netdev)
2615 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2617 ixgbevf_down(adapter);
2618 ixgbevf_free_irq(adapter);
2620 ixgbevf_free_all_tx_resources(adapter);
2621 ixgbevf_free_all_rx_resources(adapter);
2626 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2627 u32 vlan_macip_lens, u32 type_tucmd,
2630 struct ixgbe_adv_tx_context_desc *context_desc;
2631 u16 i = tx_ring->next_to_use;
2633 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2636 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2638 /* set bits to identify this as an advanced context descriptor */
2639 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2641 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2642 context_desc->seqnum_seed = 0;
2643 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2644 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2647 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
2648 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2650 u32 vlan_macip_lens, type_tucmd;
2651 u32 mss_l4len_idx, l4len;
2653 if (!skb_is_gso(skb))
2656 if (skb_header_cloned(skb)) {
2657 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2662 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2663 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2665 if (skb->protocol == htons(ETH_P_IP)) {
2666 struct iphdr *iph = ip_hdr(skb);
2669 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2673 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2674 } else if (skb_is_gso_v6(skb)) {
2675 ipv6_hdr(skb)->payload_len = 0;
2676 tcp_hdr(skb)->check =
2677 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2678 &ipv6_hdr(skb)->daddr,
2682 /* compute header lengths */
2683 l4len = tcp_hdrlen(skb);
2685 *hdr_len = skb_transport_offset(skb) + l4len;
2687 /* mss_l4len_id: use 1 as index for TSO */
2688 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2689 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2690 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2692 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2693 vlan_macip_lens = skb_network_header_len(skb);
2694 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2695 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2697 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2698 type_tucmd, mss_l4len_idx);
2703 static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2704 struct sk_buff *skb, u32 tx_flags)
2706 u32 vlan_macip_lens = 0;
2707 u32 mss_l4len_idx = 0;
2710 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2712 switch (skb->protocol) {
2713 case __constant_htons(ETH_P_IP):
2714 vlan_macip_lens |= skb_network_header_len(skb);
2715 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2716 l4_hdr = ip_hdr(skb)->protocol;
2718 case __constant_htons(ETH_P_IPV6):
2719 vlan_macip_lens |= skb_network_header_len(skb);
2720 l4_hdr = ipv6_hdr(skb)->nexthdr;
2723 if (unlikely(net_ratelimit())) {
2724 dev_warn(tx_ring->dev,
2725 "partial checksum but proto=%x!\n",
2733 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2734 mss_l4len_idx = tcp_hdrlen(skb) <<
2735 IXGBE_ADVTXD_L4LEN_SHIFT;
2738 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2739 mss_l4len_idx = sizeof(struct sctphdr) <<
2740 IXGBE_ADVTXD_L4LEN_SHIFT;
2743 mss_l4len_idx = sizeof(struct udphdr) <<
2744 IXGBE_ADVTXD_L4LEN_SHIFT;
2747 if (unlikely(net_ratelimit())) {
2748 dev_warn(tx_ring->dev,
2749 "partial checksum but l4 proto=%x!\n",
2756 /* vlan_macip_lens: MACLEN, VLAN tag */
2757 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2758 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2760 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2761 type_tucmd, mss_l4len_idx);
2763 return (skb->ip_summed == CHECKSUM_PARTIAL);
2766 static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
2767 struct sk_buff *skb, u32 tx_flags,
2770 struct ixgbevf_tx_buffer *tx_buffer_info;
2772 unsigned int total = skb->len;
2773 unsigned int offset = 0, size;
2775 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2779 i = tx_ring->next_to_use;
2781 len = min(skb_headlen(skb), total);
2783 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2784 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2786 tx_buffer_info->length = size;
2787 tx_buffer_info->mapped_as_page = false;
2788 tx_buffer_info->dma = dma_map_single(tx_ring->dev,
2790 size, DMA_TO_DEVICE);
2791 if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
2793 tx_buffer_info->next_to_watch = i;
2800 if (i == tx_ring->count)
2804 for (f = 0; f < nr_frags; f++) {
2805 const struct skb_frag_struct *frag;
2807 frag = &skb_shinfo(skb)->frags[f];
2808 len = min((unsigned int)skb_frag_size(frag), total);
2812 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2813 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2815 tx_buffer_info->length = size;
2816 tx_buffer_info->dma =
2817 skb_frag_dma_map(tx_ring->dev, frag,
2818 offset, size, DMA_TO_DEVICE);
2819 if (dma_mapping_error(tx_ring->dev,
2820 tx_buffer_info->dma))
2822 tx_buffer_info->mapped_as_page = true;
2823 tx_buffer_info->next_to_watch = i;
2830 if (i == tx_ring->count)
2838 i = tx_ring->count - 1;
2841 tx_ring->tx_buffer_info[i].skb = skb;
2842 tx_ring->tx_buffer_info[first].next_to_watch = i;
2843 tx_ring->tx_buffer_info[first].time_stamp = jiffies;
2848 dev_err(tx_ring->dev, "TX DMA map failed\n");
2850 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2851 tx_buffer_info->dma = 0;
2852 tx_buffer_info->next_to_watch = 0;
2855 /* clear timestamp and dma mappings for remaining portion of packet */
2856 while (count >= 0) {
2860 i += tx_ring->count;
2861 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2862 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
2868 static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
2869 int count, u32 paylen, u8 hdr_len)
2871 union ixgbe_adv_tx_desc *tx_desc = NULL;
2872 struct ixgbevf_tx_buffer *tx_buffer_info;
2873 u32 olinfo_status = 0, cmd_type_len = 0;
2876 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2878 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2880 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2882 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2883 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2885 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2886 olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
2888 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2889 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2891 /* use index 1 context for tso */
2892 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2893 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2894 olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
2898 * Check Context must be set if Tx switch is enabled, which it
2899 * always is for case where virtual functions are running
2901 olinfo_status |= IXGBE_ADVTXD_CC;
2903 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2905 i = tx_ring->next_to_use;
2907 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2908 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2909 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2910 tx_desc->read.cmd_type_len =
2911 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2912 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2914 if (i == tx_ring->count)
2918 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2920 tx_ring->next_to_use = i;
2923 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2925 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
2927 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
2928 /* Herbert's original patch had:
2929 * smp_mb__after_netif_stop_queue();
2930 * but since that doesn't exist yet, just open code it. */
2933 /* We need to check again in a case another CPU has just
2934 * made room available. */
2935 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
2938 /* A reprieve! - use start_queue because it doesn't call schedule */
2939 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2940 ++adapter->restart_queue;
2944 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2946 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
2948 return __ixgbevf_maybe_stop_tx(tx_ring, size);
2951 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2953 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2954 struct ixgbevf_ring *tx_ring;
2956 unsigned int tx_flags = 0;
2959 u16 count = TXD_USE_COUNT(skb_headlen(skb));
2960 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2963 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
2964 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
2966 return NETDEV_TX_OK;
2969 tx_ring = &adapter->tx_ring[r_idx];
2972 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
2973 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
2974 * + 2 desc gap to keep tail from touching head,
2975 * + 1 desc for context descriptor,
2976 * otherwise try next time
2978 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2979 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2980 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
2982 count += skb_shinfo(skb)->nr_frags;
2984 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
2986 return NETDEV_TX_BUSY;
2989 if (vlan_tx_tag_present(skb)) {
2990 tx_flags |= vlan_tx_tag_get(skb);
2991 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
2992 tx_flags |= IXGBE_TX_FLAGS_VLAN;
2995 first = tx_ring->next_to_use;
2997 if (skb->protocol == htons(ETH_P_IP))
2998 tx_flags |= IXGBE_TX_FLAGS_IPV4;
2999 tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
3001 dev_kfree_skb_any(skb);
3002 return NETDEV_TX_OK;
3006 tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
3007 else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
3008 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3010 ixgbevf_tx_queue(tx_ring, tx_flags,
3011 ixgbevf_tx_map(tx_ring, skb, tx_flags, first),
3014 * Force memory writes to complete before letting h/w
3015 * know there are new descriptors to fetch. (Only
3016 * applicable for weak-ordered memory model archs,
3021 writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
3023 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
3025 return NETDEV_TX_OK;
3029 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3030 * @netdev: network interface device structure
3031 * @p: pointer to an address structure
3033 * Returns 0 on success, negative on failure
3035 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3037 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3038 struct ixgbe_hw *hw = &adapter->hw;
3039 struct sockaddr *addr = p;
3041 if (!is_valid_ether_addr(addr->sa_data))
3042 return -EADDRNOTAVAIL;
3044 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3045 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3047 spin_lock_bh(&adapter->mbx_lock);
3049 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
3051 spin_unlock_bh(&adapter->mbx_lock);
3057 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3058 * @netdev: network interface device structure
3059 * @new_mtu: new value for maximum frame size
3061 * Returns 0 on success, negative on failure
3063 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3065 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3066 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3067 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
3069 switch (adapter->hw.api_version) {
3070 case ixgbe_mbox_api_11:
3071 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3074 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3075 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3079 /* MTU < 68 is an error and causes problems on some kernels */
3080 if ((new_mtu < 68) || (max_frame > max_possible_frame))
3083 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3084 netdev->mtu, new_mtu);
3085 /* must set new MTU before calling down or up */
3086 netdev->mtu = new_mtu;
3088 if (netif_running(netdev))
3089 ixgbevf_reinit_locked(adapter);
3094 static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
3096 struct net_device *netdev = pci_get_drvdata(pdev);
3097 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3102 netif_device_detach(netdev);
3104 if (netif_running(netdev)) {
3106 ixgbevf_down(adapter);
3107 ixgbevf_free_irq(adapter);
3108 ixgbevf_free_all_tx_resources(adapter);
3109 ixgbevf_free_all_rx_resources(adapter);
3113 ixgbevf_clear_interrupt_scheme(adapter);
3116 retval = pci_save_state(pdev);
3121 pci_disable_device(pdev);
3127 static int ixgbevf_resume(struct pci_dev *pdev)
3129 struct ixgbevf_adapter *adapter = pci_get_drvdata(pdev);
3130 struct net_device *netdev = adapter->netdev;
3133 pci_set_power_state(pdev, PCI_D0);
3134 pci_restore_state(pdev);
3136 * pci_restore_state clears dev->state_saved so call
3137 * pci_save_state to restore it.
3139 pci_save_state(pdev);
3141 err = pci_enable_device_mem(pdev);
3143 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3146 pci_set_master(pdev);
3149 err = ixgbevf_init_interrupt_scheme(adapter);
3152 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3156 ixgbevf_reset(adapter);
3158 if (netif_running(netdev)) {
3159 err = ixgbevf_open(netdev);
3164 netif_device_attach(netdev);
3169 #endif /* CONFIG_PM */
3170 static void ixgbevf_shutdown(struct pci_dev *pdev)
3172 ixgbevf_suspend(pdev, PMSG_SUSPEND);
3175 static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3176 struct rtnl_link_stats64 *stats)
3178 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3181 const struct ixgbevf_ring *ring;
3184 ixgbevf_update_stats(adapter);
3186 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3188 for (i = 0; i < adapter->num_rx_queues; i++) {
3189 ring = &adapter->rx_ring[i];
3191 start = u64_stats_fetch_begin_bh(&ring->syncp);
3192 bytes = ring->total_bytes;
3193 packets = ring->total_packets;
3194 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3195 stats->rx_bytes += bytes;
3196 stats->rx_packets += packets;
3199 for (i = 0; i < adapter->num_tx_queues; i++) {
3200 ring = &adapter->tx_ring[i];
3202 start = u64_stats_fetch_begin_bh(&ring->syncp);
3203 bytes = ring->total_bytes;
3204 packets = ring->total_packets;
3205 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3206 stats->tx_bytes += bytes;
3207 stats->tx_packets += packets;
3213 static const struct net_device_ops ixgbevf_netdev_ops = {
3214 .ndo_open = ixgbevf_open,
3215 .ndo_stop = ixgbevf_close,
3216 .ndo_start_xmit = ixgbevf_xmit_frame,
3217 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
3218 .ndo_get_stats64 = ixgbevf_get_stats,
3219 .ndo_validate_addr = eth_validate_addr,
3220 .ndo_set_mac_address = ixgbevf_set_mac,
3221 .ndo_change_mtu = ixgbevf_change_mtu,
3222 .ndo_tx_timeout = ixgbevf_tx_timeout,
3223 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3224 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
3227 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3229 dev->netdev_ops = &ixgbevf_netdev_ops;
3230 ixgbevf_set_ethtool_ops(dev);
3231 dev->watchdog_timeo = 5 * HZ;
3235 * ixgbevf_probe - Device Initialization Routine
3236 * @pdev: PCI device information struct
3237 * @ent: entry in ixgbevf_pci_tbl
3239 * Returns 0 on success, negative on failure
3241 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3242 * The OS initialization, configuring of the adapter private structure,
3243 * and a hardware reset occur.
3245 static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3247 struct net_device *netdev;
3248 struct ixgbevf_adapter *adapter = NULL;
3249 struct ixgbe_hw *hw = NULL;
3250 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3251 static int cards_found;
3252 int err, pci_using_dac;
3254 err = pci_enable_device(pdev);
3258 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3259 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3262 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3264 err = dma_set_coherent_mask(&pdev->dev,
3267 dev_err(&pdev->dev, "No usable DMA "
3268 "configuration, aborting\n");
3275 err = pci_request_regions(pdev, ixgbevf_driver_name);
3277 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3281 pci_set_master(pdev);
3283 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3287 goto err_alloc_etherdev;
3290 SET_NETDEV_DEV(netdev, &pdev->dev);
3292 pci_set_drvdata(pdev, netdev);
3293 adapter = netdev_priv(netdev);
3295 adapter->netdev = netdev;
3296 adapter->pdev = pdev;
3299 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3302 * call save state here in standalone driver because it relies on
3303 * adapter struct to exist, and needs to call netdev_priv
3305 pci_save_state(pdev);
3307 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3308 pci_resource_len(pdev, 0));
3314 ixgbevf_assign_netdev_ops(netdev);
3316 adapter->bd_number = cards_found;
3319 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3320 hw->mac.type = ii->mac;
3322 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3323 sizeof(struct ixgbe_mbx_operations));
3325 /* setup the private structure */
3326 err = ixgbevf_sw_init(adapter);
3330 /* The HW MAC address was set and/or determined in sw_init */
3331 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3333 if (!is_valid_ether_addr(netdev->dev_addr)) {
3334 pr_err("invalid MAC address\n");
3339 netdev->hw_features = NETIF_F_SG |
3346 netdev->features = netdev->hw_features |
3347 NETIF_F_HW_VLAN_TX |
3348 NETIF_F_HW_VLAN_RX |
3349 NETIF_F_HW_VLAN_FILTER;
3351 netdev->vlan_features |= NETIF_F_TSO;
3352 netdev->vlan_features |= NETIF_F_TSO6;
3353 netdev->vlan_features |= NETIF_F_IP_CSUM;
3354 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3355 netdev->vlan_features |= NETIF_F_SG;
3358 netdev->features |= NETIF_F_HIGHDMA;
3360 netdev->priv_flags |= IFF_UNICAST_FLT;
3362 init_timer(&adapter->watchdog_timer);
3363 adapter->watchdog_timer.function = ixgbevf_watchdog;
3364 adapter->watchdog_timer.data = (unsigned long)adapter;
3366 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3367 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3369 err = ixgbevf_init_interrupt_scheme(adapter);
3373 strcpy(netdev->name, "eth%d");
3375 err = register_netdev(netdev);
3379 netif_carrier_off(netdev);
3381 ixgbevf_init_last_counter_stats(adapter);
3383 /* print the MAC address */
3384 hw_dbg(hw, "%pM\n", netdev->dev_addr);
3386 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3388 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3393 ixgbevf_clear_interrupt_scheme(adapter);
3395 ixgbevf_reset_interrupt_capability(adapter);
3396 iounmap(hw->hw_addr);
3398 free_netdev(netdev);
3400 pci_release_regions(pdev);
3403 pci_disable_device(pdev);
3408 * ixgbevf_remove - Device Removal Routine
3409 * @pdev: PCI device information struct
3411 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3412 * that it should release a PCI device. The could be caused by a
3413 * Hot-Plug event, or because the driver is going to be removed from
3416 static void ixgbevf_remove(struct pci_dev *pdev)
3418 struct net_device *netdev = pci_get_drvdata(pdev);
3419 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3421 set_bit(__IXGBEVF_DOWN, &adapter->state);
3423 del_timer_sync(&adapter->watchdog_timer);
3425 cancel_work_sync(&adapter->reset_task);
3426 cancel_work_sync(&adapter->watchdog_task);
3428 if (netdev->reg_state == NETREG_REGISTERED)
3429 unregister_netdev(netdev);
3431 ixgbevf_clear_interrupt_scheme(adapter);
3432 ixgbevf_reset_interrupt_capability(adapter);
3434 iounmap(adapter->hw.hw_addr);
3435 pci_release_regions(pdev);
3437 hw_dbg(&adapter->hw, "Remove complete\n");
3439 kfree(adapter->tx_ring);
3440 kfree(adapter->rx_ring);
3442 free_netdev(netdev);
3444 pci_disable_device(pdev);
3448 * ixgbevf_io_error_detected - called when PCI error is detected
3449 * @pdev: Pointer to PCI device
3450 * @state: The current pci connection state
3452 * This function is called after a PCI bus error affecting
3453 * this device has been detected.
3455 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3456 pci_channel_state_t state)
3458 struct net_device *netdev = pci_get_drvdata(pdev);
3459 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3461 netif_device_detach(netdev);
3463 if (state == pci_channel_io_perm_failure)
3464 return PCI_ERS_RESULT_DISCONNECT;
3466 if (netif_running(netdev))
3467 ixgbevf_down(adapter);
3469 pci_disable_device(pdev);
3471 /* Request a slot slot reset. */
3472 return PCI_ERS_RESULT_NEED_RESET;
3476 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3477 * @pdev: Pointer to PCI device
3479 * Restart the card from scratch, as if from a cold-boot. Implementation
3480 * resembles the first-half of the ixgbevf_resume routine.
3482 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3484 struct net_device *netdev = pci_get_drvdata(pdev);
3485 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3487 if (pci_enable_device_mem(pdev)) {
3489 "Cannot re-enable PCI device after reset.\n");
3490 return PCI_ERS_RESULT_DISCONNECT;
3493 pci_set_master(pdev);
3495 ixgbevf_reset(adapter);
3497 return PCI_ERS_RESULT_RECOVERED;
3501 * ixgbevf_io_resume - called when traffic can start flowing again.
3502 * @pdev: Pointer to PCI device
3504 * This callback is called when the error recovery driver tells us that
3505 * its OK to resume normal operation. Implementation resembles the
3506 * second-half of the ixgbevf_resume routine.
3508 static void ixgbevf_io_resume(struct pci_dev *pdev)
3510 struct net_device *netdev = pci_get_drvdata(pdev);
3511 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3513 if (netif_running(netdev))
3514 ixgbevf_up(adapter);
3516 netif_device_attach(netdev);
3519 /* PCI Error Recovery (ERS) */
3520 static const struct pci_error_handlers ixgbevf_err_handler = {
3521 .error_detected = ixgbevf_io_error_detected,
3522 .slot_reset = ixgbevf_io_slot_reset,
3523 .resume = ixgbevf_io_resume,
3526 static struct pci_driver ixgbevf_driver = {
3527 .name = ixgbevf_driver_name,
3528 .id_table = ixgbevf_pci_tbl,
3529 .probe = ixgbevf_probe,
3530 .remove = ixgbevf_remove,
3532 /* Power Management Hooks */
3533 .suspend = ixgbevf_suspend,
3534 .resume = ixgbevf_resume,
3536 .shutdown = ixgbevf_shutdown,
3537 .err_handler = &ixgbevf_err_handler
3541 * ixgbevf_init_module - Driver Registration Routine
3543 * ixgbevf_init_module is the first routine called when the driver is
3544 * loaded. All it does is register with the PCI subsystem.
3546 static int __init ixgbevf_init_module(void)
3549 pr_info("%s - version %s\n", ixgbevf_driver_string,
3550 ixgbevf_driver_version);
3552 pr_info("%s\n", ixgbevf_copyright);
3554 ret = pci_register_driver(&ixgbevf_driver);
3558 module_init(ixgbevf_init_module);
3561 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3563 * ixgbevf_exit_module is called just before the driver is removed
3566 static void __exit ixgbevf_exit_module(void)
3568 pci_unregister_driver(&ixgbevf_driver);
3573 * ixgbevf_get_hw_dev_name - return device name string
3574 * used by hardware layer to print debugging information
3576 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3578 struct ixgbevf_adapter *adapter = hw->back;
3579 return adapter->netdev->name;
3583 module_exit(ixgbevf_exit_module);
3585 /* ixgbevf_main.c */