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 pci_dev *pdev = adapter->pdev;
754 struct ixgbe_hw *hw = &adapter->hw;
756 bool got_ack = false;
758 hw->mac.get_link_status = 1;
759 if (!hw->mbx.ops.check_for_ack(hw))
762 if (!hw->mbx.ops.check_for_msg(hw)) {
763 hw->mbx.ops.read(hw, &msg, 1);
765 if ((msg & IXGBE_MBVFICR_VFREQ_MASK) == IXGBE_PF_CONTROL_MSG) {
766 mod_timer(&adapter->watchdog_timer,
767 round_jiffies(jiffies + 1));
768 adapter->link_up = false;
771 if (msg & IXGBE_VT_MSGTYPE_NACK)
773 "Last Request of type %2.2x to PF Nacked\n",
775 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFSTS;
778 /* checking for the ack clears the PFACK bit. Place
779 * it back in the v2p_mailbox cache so that anyone
780 * polling for an ack will not miss it
783 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFACK;
785 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
791 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
793 * @data: pointer to our q_vector struct for this interrupt vector
795 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
797 struct ixgbevf_q_vector *q_vector = data;
799 /* EIAM disabled interrupts (on this vector) for us */
800 if (q_vector->rx.ring || q_vector->tx.ring)
801 napi_schedule(&q_vector->napi);
806 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
809 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
811 a->rx_ring[r_idx].next = q_vector->rx.ring;
812 q_vector->rx.ring = &a->rx_ring[r_idx];
813 q_vector->rx.count++;
816 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
819 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
821 a->tx_ring[t_idx].next = q_vector->tx.ring;
822 q_vector->tx.ring = &a->tx_ring[t_idx];
823 q_vector->tx.count++;
827 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
828 * @adapter: board private structure to initialize
830 * This function maps descriptor rings to the queue-specific vectors
831 * we were allotted through the MSI-X enabling code. Ideally, we'd have
832 * one vector per ring/queue, but on a constrained vector budget, we
833 * group the rings as "efficiently" as possible. You would add new
834 * mapping configurations in here.
836 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
840 int rxr_idx = 0, txr_idx = 0;
841 int rxr_remaining = adapter->num_rx_queues;
842 int txr_remaining = adapter->num_tx_queues;
847 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
850 * The ideal configuration...
851 * We have enough vectors to map one per queue.
853 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
854 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
855 map_vector_to_rxq(adapter, v_start, rxr_idx);
857 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
858 map_vector_to_txq(adapter, v_start, txr_idx);
863 * If we don't have enough vectors for a 1-to-1
864 * mapping, we'll have to group them so there are
865 * multiple queues per vector.
867 /* Re-adjusting *qpv takes care of the remainder. */
868 for (i = v_start; i < q_vectors; i++) {
869 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
870 for (j = 0; j < rqpv; j++) {
871 map_vector_to_rxq(adapter, i, rxr_idx);
876 for (i = v_start; i < q_vectors; i++) {
877 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
878 for (j = 0; j < tqpv; j++) {
879 map_vector_to_txq(adapter, i, txr_idx);
890 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
891 * @adapter: board private structure
893 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
894 * interrupts from the kernel.
896 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
898 struct net_device *netdev = adapter->netdev;
899 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
903 for (vector = 0; vector < q_vectors; vector++) {
904 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
905 struct msix_entry *entry = &adapter->msix_entries[vector];
907 if (q_vector->tx.ring && q_vector->rx.ring) {
908 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
909 "%s-%s-%d", netdev->name, "TxRx", ri++);
911 } else if (q_vector->rx.ring) {
912 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
913 "%s-%s-%d", netdev->name, "rx", ri++);
914 } else if (q_vector->tx.ring) {
915 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
916 "%s-%s-%d", netdev->name, "tx", ti++);
918 /* skip this unused q_vector */
921 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
922 q_vector->name, q_vector);
925 "request_irq failed for MSIX interrupt "
927 goto free_queue_irqs;
931 err = request_irq(adapter->msix_entries[vector].vector,
932 &ixgbevf_msix_other, 0, netdev->name, adapter);
935 "request_irq for msix_other failed: %d\n", err);
936 goto free_queue_irqs;
944 free_irq(adapter->msix_entries[vector].vector,
945 adapter->q_vector[vector]);
947 /* This failure is non-recoverable - it indicates the system is
948 * out of MSIX vector resources and the VF driver cannot run
949 * without them. Set the number of msix vectors to zero
950 * indicating that not enough can be allocated. The error
951 * will be returned to the user indicating device open failed.
952 * Any further attempts to force the driver to open will also
953 * fail. The only way to recover is to unload the driver and
954 * reload it again. If the system has recovered some MSIX
955 * vectors then it may succeed.
957 adapter->num_msix_vectors = 0;
961 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
963 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
965 for (i = 0; i < q_vectors; i++) {
966 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
967 q_vector->rx.ring = NULL;
968 q_vector->tx.ring = NULL;
969 q_vector->rx.count = 0;
970 q_vector->tx.count = 0;
975 * ixgbevf_request_irq - initialize interrupts
976 * @adapter: board private structure
978 * Attempts to configure interrupts using the best available
979 * capabilities of the hardware and kernel.
981 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
985 err = ixgbevf_request_msix_irqs(adapter);
989 "request_irq failed, Error %d\n", err);
994 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
998 q_vectors = adapter->num_msix_vectors;
1001 free_irq(adapter->msix_entries[i].vector, adapter);
1004 for (; i >= 0; i--) {
1005 /* free only the irqs that were actually requested */
1006 if (!adapter->q_vector[i]->rx.ring &&
1007 !adapter->q_vector[i]->tx.ring)
1010 free_irq(adapter->msix_entries[i].vector,
1011 adapter->q_vector[i]);
1014 ixgbevf_reset_q_vectors(adapter);
1018 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1019 * @adapter: board private structure
1021 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1023 struct ixgbe_hw *hw = &adapter->hw;
1026 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
1027 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1028 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
1030 IXGBE_WRITE_FLUSH(hw);
1032 for (i = 0; i < adapter->num_msix_vectors; i++)
1033 synchronize_irq(adapter->msix_entries[i].vector);
1037 * ixgbevf_irq_enable - Enable default interrupt generation settings
1038 * @adapter: board private structure
1040 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1042 struct ixgbe_hw *hw = &adapter->hw;
1044 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1045 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1046 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1050 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1051 * @adapter: board private structure
1053 * Configure the Tx unit of the MAC after a reset.
1055 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1058 struct ixgbe_hw *hw = &adapter->hw;
1059 u32 i, j, tdlen, txctrl;
1061 /* Setup the HW Tx Head and Tail descriptor pointers */
1062 for (i = 0; i < adapter->num_tx_queues; i++) {
1063 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1066 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1067 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1068 (tdba & DMA_BIT_MASK(32)));
1069 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1070 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1071 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1072 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1073 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1074 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1075 /* Disable Tx Head Writeback RO bit, since this hoses
1076 * bookkeeping if things aren't delivered in order.
1078 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1079 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1080 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1084 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1086 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1088 struct ixgbevf_ring *rx_ring;
1089 struct ixgbe_hw *hw = &adapter->hw;
1092 rx_ring = &adapter->rx_ring[index];
1094 srrctl = IXGBE_SRRCTL_DROP_EN;
1096 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1098 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1099 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1101 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1104 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1106 struct ixgbe_hw *hw = &adapter->hw;
1107 struct net_device *netdev = adapter->netdev;
1108 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1112 /* notify the PF of our intent to use this size of frame */
1113 ixgbevf_rlpml_set_vf(hw, max_frame);
1115 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1116 max_frame += VLAN_HLEN;
1119 * Allocate buffer sizes that fit well into 32K and
1120 * take into account max frame size of 9.5K
1122 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1123 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1124 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1125 else if (max_frame <= IXGBEVF_RXBUFFER_2K)
1126 rx_buf_len = IXGBEVF_RXBUFFER_2K;
1127 else if (max_frame <= IXGBEVF_RXBUFFER_4K)
1128 rx_buf_len = IXGBEVF_RXBUFFER_4K;
1129 else if (max_frame <= IXGBEVF_RXBUFFER_8K)
1130 rx_buf_len = IXGBEVF_RXBUFFER_8K;
1132 rx_buf_len = IXGBEVF_RXBUFFER_10K;
1134 for (i = 0; i < adapter->num_rx_queues; i++)
1135 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1139 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1140 * @adapter: board private structure
1142 * Configure the Rx unit of the MAC after a reset.
1144 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1147 struct ixgbe_hw *hw = &adapter->hw;
1151 /* PSRTYPE must be initialized in 82599 */
1152 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
1154 /* set_rx_buffer_len must be called before ring initialization */
1155 ixgbevf_set_rx_buffer_len(adapter);
1157 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1158 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1159 * the Base and Length of the Rx Descriptor Ring */
1160 for (i = 0; i < adapter->num_rx_queues; i++) {
1161 rdba = adapter->rx_ring[i].dma;
1162 j = adapter->rx_ring[i].reg_idx;
1163 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1164 (rdba & DMA_BIT_MASK(32)));
1165 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1166 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1167 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1168 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1169 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1170 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1172 ixgbevf_configure_srrctl(adapter, j);
1176 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1178 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1179 struct ixgbe_hw *hw = &adapter->hw;
1182 spin_lock_bh(&adapter->mbx_lock);
1184 /* add VID to filter table */
1185 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
1187 spin_unlock_bh(&adapter->mbx_lock);
1189 /* translate error return types so error makes sense */
1190 if (err == IXGBE_ERR_MBX)
1193 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1196 set_bit(vid, adapter->active_vlans);
1201 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1203 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1204 struct ixgbe_hw *hw = &adapter->hw;
1205 int err = -EOPNOTSUPP;
1207 spin_lock_bh(&adapter->mbx_lock);
1209 /* remove VID from filter table */
1210 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
1212 spin_unlock_bh(&adapter->mbx_lock);
1214 clear_bit(vid, adapter->active_vlans);
1219 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1223 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1224 ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
1227 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1229 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1230 struct ixgbe_hw *hw = &adapter->hw;
1233 if ((netdev_uc_count(netdev)) > 10) {
1234 pr_err("Too many unicast filters - No Space\n");
1238 if (!netdev_uc_empty(netdev)) {
1239 struct netdev_hw_addr *ha;
1240 netdev_for_each_uc_addr(ha, netdev) {
1241 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1246 * If the list is empty then send message to PF driver to
1247 * clear all macvlans on this VF.
1249 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1256 * ixgbevf_set_rx_mode - Multicast and unicast set
1257 * @netdev: network interface device structure
1259 * The set_rx_method entry point is called whenever the multicast address
1260 * list, unicast address list or the network interface flags are updated.
1261 * This routine is responsible for configuring the hardware for proper
1262 * multicast mode and configuring requested unicast filters.
1264 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1266 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1267 struct ixgbe_hw *hw = &adapter->hw;
1269 spin_lock_bh(&adapter->mbx_lock);
1271 /* reprogram multicast list */
1272 hw->mac.ops.update_mc_addr_list(hw, netdev);
1274 ixgbevf_write_uc_addr_list(netdev);
1276 spin_unlock_bh(&adapter->mbx_lock);
1279 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1282 struct ixgbevf_q_vector *q_vector;
1283 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1285 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1286 q_vector = adapter->q_vector[q_idx];
1287 napi_enable(&q_vector->napi);
1291 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1294 struct ixgbevf_q_vector *q_vector;
1295 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1297 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1298 q_vector = adapter->q_vector[q_idx];
1299 napi_disable(&q_vector->napi);
1303 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1305 struct net_device *netdev = adapter->netdev;
1308 ixgbevf_set_rx_mode(netdev);
1310 ixgbevf_restore_vlan(adapter);
1312 ixgbevf_configure_tx(adapter);
1313 ixgbevf_configure_rx(adapter);
1314 for (i = 0; i < adapter->num_rx_queues; i++) {
1315 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1316 ixgbevf_alloc_rx_buffers(adapter, ring,
1317 IXGBE_DESC_UNUSED(ring));
1321 #define IXGBE_MAX_RX_DESC_POLL 10
1322 static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1325 struct ixgbe_hw *hw = &adapter->hw;
1326 int j = adapter->rx_ring[rxr].reg_idx;
1329 for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1330 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1335 if (k >= IXGBE_MAX_RX_DESC_POLL) {
1336 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1337 "not set within the polling period\n", rxr);
1340 ixgbevf_release_rx_desc(hw, &adapter->rx_ring[rxr],
1341 adapter->rx_ring[rxr].count - 1);
1344 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1346 /* Only save pre-reset stats if there are some */
1347 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1348 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1349 adapter->stats.base_vfgprc;
1350 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1351 adapter->stats.base_vfgptc;
1352 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1353 adapter->stats.base_vfgorc;
1354 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1355 adapter->stats.base_vfgotc;
1356 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1357 adapter->stats.base_vfmprc;
1361 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1363 struct ixgbe_hw *hw = &adapter->hw;
1365 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1366 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1367 adapter->stats.last_vfgorc |=
1368 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1369 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1370 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1371 adapter->stats.last_vfgotc |=
1372 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1373 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1375 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1376 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1377 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1378 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1379 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1382 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1384 struct ixgbe_hw *hw = &adapter->hw;
1385 int api[] = { ixgbe_mbox_api_11,
1387 ixgbe_mbox_api_unknown };
1388 int err = 0, idx = 0;
1390 spin_lock_bh(&adapter->mbx_lock);
1392 while (api[idx] != ixgbe_mbox_api_unknown) {
1393 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1399 spin_unlock_bh(&adapter->mbx_lock);
1402 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1404 struct net_device *netdev = adapter->netdev;
1405 struct ixgbe_hw *hw = &adapter->hw;
1407 int num_rx_rings = adapter->num_rx_queues;
1410 for (i = 0; i < adapter->num_tx_queues; i++) {
1411 j = adapter->tx_ring[i].reg_idx;
1412 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1413 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1414 txdctl |= (8 << 16);
1415 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1418 for (i = 0; i < adapter->num_tx_queues; i++) {
1419 j = adapter->tx_ring[i].reg_idx;
1420 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1421 txdctl |= IXGBE_TXDCTL_ENABLE;
1422 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1425 for (i = 0; i < num_rx_rings; i++) {
1426 j = adapter->rx_ring[i].reg_idx;
1427 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1428 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1429 if (hw->mac.type == ixgbe_mac_X540_vf) {
1430 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1431 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1432 IXGBE_RXDCTL_RLPML_EN);
1434 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1435 ixgbevf_rx_desc_queue_enable(adapter, i);
1438 ixgbevf_configure_msix(adapter);
1440 spin_lock_bh(&adapter->mbx_lock);
1442 if (is_valid_ether_addr(hw->mac.addr))
1443 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1445 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1447 spin_unlock_bh(&adapter->mbx_lock);
1449 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1450 ixgbevf_napi_enable_all(adapter);
1452 /* enable transmits */
1453 netif_tx_start_all_queues(netdev);
1455 ixgbevf_save_reset_stats(adapter);
1456 ixgbevf_init_last_counter_stats(adapter);
1458 hw->mac.get_link_status = 1;
1459 mod_timer(&adapter->watchdog_timer, jiffies);
1462 static int ixgbevf_reset_queues(struct ixgbevf_adapter *adapter)
1464 struct ixgbe_hw *hw = &adapter->hw;
1465 struct ixgbevf_ring *rx_ring;
1466 unsigned int def_q = 0;
1467 unsigned int num_tcs = 0;
1468 unsigned int num_rx_queues = 1;
1471 spin_lock_bh(&adapter->mbx_lock);
1473 /* fetch queue configuration from the PF */
1474 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1476 spin_unlock_bh(&adapter->mbx_lock);
1482 /* update default Tx ring register index */
1483 adapter->tx_ring[0].reg_idx = def_q;
1485 /* we need as many queues as traffic classes */
1486 num_rx_queues = num_tcs;
1489 /* nothing to do if we have the correct number of queues */
1490 if (adapter->num_rx_queues == num_rx_queues)
1493 /* allocate new rings */
1494 rx_ring = kcalloc(num_rx_queues,
1495 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1499 /* setup ring fields */
1500 for (i = 0; i < num_rx_queues; i++) {
1501 rx_ring[i].count = adapter->rx_ring_count;
1502 rx_ring[i].queue_index = i;
1503 rx_ring[i].reg_idx = i;
1504 rx_ring[i].dev = &adapter->pdev->dev;
1505 rx_ring[i].netdev = adapter->netdev;
1507 /* allocate resources on the ring */
1508 err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
1512 ixgbevf_free_rx_resources(adapter, &rx_ring[i]);
1519 /* free the existing rings and queues */
1520 ixgbevf_free_all_rx_resources(adapter);
1521 adapter->num_rx_queues = 0;
1522 kfree(adapter->rx_ring);
1524 /* move new rings into position on the adapter struct */
1525 adapter->rx_ring = rx_ring;
1526 adapter->num_rx_queues = num_rx_queues;
1528 /* reset ring to vector mapping */
1529 ixgbevf_reset_q_vectors(adapter);
1530 ixgbevf_map_rings_to_vectors(adapter);
1535 void ixgbevf_up(struct ixgbevf_adapter *adapter)
1537 struct ixgbe_hw *hw = &adapter->hw;
1539 ixgbevf_negotiate_api(adapter);
1541 ixgbevf_reset_queues(adapter);
1543 ixgbevf_configure(adapter);
1545 ixgbevf_up_complete(adapter);
1547 /* clear any pending interrupts, may auto mask */
1548 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1550 ixgbevf_irq_enable(adapter);
1554 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1555 * @adapter: board private structure
1556 * @rx_ring: ring to free buffers from
1558 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1559 struct ixgbevf_ring *rx_ring)
1561 struct pci_dev *pdev = adapter->pdev;
1565 if (!rx_ring->rx_buffer_info)
1568 /* Free all the Rx ring sk_buffs */
1569 for (i = 0; i < rx_ring->count; i++) {
1570 struct ixgbevf_rx_buffer *rx_buffer_info;
1572 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1573 if (rx_buffer_info->dma) {
1574 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1575 rx_ring->rx_buf_len,
1577 rx_buffer_info->dma = 0;
1579 if (rx_buffer_info->skb) {
1580 struct sk_buff *skb = rx_buffer_info->skb;
1581 rx_buffer_info->skb = NULL;
1583 struct sk_buff *this = skb;
1584 skb = IXGBE_CB(skb)->prev;
1585 dev_kfree_skb(this);
1590 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1591 memset(rx_ring->rx_buffer_info, 0, size);
1593 /* Zero out the descriptor ring */
1594 memset(rx_ring->desc, 0, rx_ring->size);
1596 rx_ring->next_to_clean = 0;
1597 rx_ring->next_to_use = 0;
1600 writel(0, adapter->hw.hw_addr + rx_ring->head);
1602 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1606 * ixgbevf_clean_tx_ring - Free Tx Buffers
1607 * @adapter: board private structure
1608 * @tx_ring: ring to be cleaned
1610 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1611 struct ixgbevf_ring *tx_ring)
1613 struct ixgbevf_tx_buffer *tx_buffer_info;
1617 if (!tx_ring->tx_buffer_info)
1620 /* Free all the Tx ring sk_buffs */
1621 for (i = 0; i < tx_ring->count; i++) {
1622 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1623 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
1626 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1627 memset(tx_ring->tx_buffer_info, 0, size);
1629 memset(tx_ring->desc, 0, tx_ring->size);
1631 tx_ring->next_to_use = 0;
1632 tx_ring->next_to_clean = 0;
1635 writel(0, adapter->hw.hw_addr + tx_ring->head);
1637 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1641 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1642 * @adapter: board private structure
1644 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1648 for (i = 0; i < adapter->num_rx_queues; i++)
1649 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1653 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1654 * @adapter: board private structure
1656 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1660 for (i = 0; i < adapter->num_tx_queues; i++)
1661 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1664 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1666 struct net_device *netdev = adapter->netdev;
1667 struct ixgbe_hw *hw = &adapter->hw;
1671 /* signal that we are down to the interrupt handler */
1672 set_bit(__IXGBEVF_DOWN, &adapter->state);
1673 /* disable receives */
1675 netif_tx_disable(netdev);
1679 netif_tx_stop_all_queues(netdev);
1681 ixgbevf_irq_disable(adapter);
1683 ixgbevf_napi_disable_all(adapter);
1685 del_timer_sync(&adapter->watchdog_timer);
1686 /* can't call flush scheduled work here because it can deadlock
1687 * if linkwatch_event tries to acquire the rtnl_lock which we are
1689 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1692 /* disable transmits in the hardware now that interrupts are off */
1693 for (i = 0; i < adapter->num_tx_queues; i++) {
1694 j = adapter->tx_ring[i].reg_idx;
1695 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1696 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1697 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1700 netif_carrier_off(netdev);
1702 if (!pci_channel_offline(adapter->pdev))
1703 ixgbevf_reset(adapter);
1705 ixgbevf_clean_all_tx_rings(adapter);
1706 ixgbevf_clean_all_rx_rings(adapter);
1709 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1711 WARN_ON(in_interrupt());
1713 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1716 ixgbevf_down(adapter);
1717 ixgbevf_up(adapter);
1719 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1722 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1724 struct ixgbe_hw *hw = &adapter->hw;
1725 struct net_device *netdev = adapter->netdev;
1727 if (hw->mac.ops.reset_hw(hw))
1728 hw_dbg(hw, "PF still resetting\n");
1730 hw->mac.ops.init_hw(hw);
1732 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1733 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1735 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1740 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1744 int vector_threshold;
1746 /* We'll want at least 2 (vector_threshold):
1747 * 1) TxQ[0] + RxQ[0] handler
1748 * 2) Other (Link Status Change, etc.)
1750 vector_threshold = MIN_MSIX_COUNT;
1752 /* The more we get, the more we will assign to Tx/Rx Cleanup
1753 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1754 * Right now, we simply care about how many we'll get; we'll
1755 * set them up later while requesting irq's.
1757 while (vectors >= vector_threshold) {
1758 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1760 if (!err || err < 0) /* Success or a nasty failure. */
1762 else /* err == number of vectors we should try again with */
1766 if (vectors < vector_threshold)
1770 dev_err(&adapter->pdev->dev,
1771 "Unable to allocate MSI-X interrupts\n");
1772 kfree(adapter->msix_entries);
1773 adapter->msix_entries = NULL;
1776 * Adjust for only the vectors we'll use, which is minimum
1777 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1778 * vectors we were allocated.
1780 adapter->num_msix_vectors = vectors;
1787 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
1788 * @adapter: board private structure to initialize
1790 * This is the top level queue allocation routine. The order here is very
1791 * important, starting with the "most" number of features turned on at once,
1792 * and ending with the smallest set of features. This way large combinations
1793 * can be allocated if they're turned on, and smaller combinations are the
1794 * fallthrough conditions.
1797 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1799 /* Start with base case */
1800 adapter->num_rx_queues = 1;
1801 adapter->num_tx_queues = 1;
1805 * ixgbevf_alloc_queues - Allocate memory for all rings
1806 * @adapter: board private structure to initialize
1808 * We allocate one ring per queue at run-time since we don't know the
1809 * number of queues at compile-time. The polling_netdev array is
1810 * intended for Multiqueue, but should work fine with a single queue.
1812 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1816 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1817 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1818 if (!adapter->tx_ring)
1819 goto err_tx_ring_allocation;
1821 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1822 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1823 if (!adapter->rx_ring)
1824 goto err_rx_ring_allocation;
1826 for (i = 0; i < adapter->num_tx_queues; i++) {
1827 adapter->tx_ring[i].count = adapter->tx_ring_count;
1828 adapter->tx_ring[i].queue_index = i;
1829 /* reg_idx may be remapped later by DCB config */
1830 adapter->tx_ring[i].reg_idx = i;
1831 adapter->tx_ring[i].dev = &adapter->pdev->dev;
1832 adapter->tx_ring[i].netdev = adapter->netdev;
1835 for (i = 0; i < adapter->num_rx_queues; i++) {
1836 adapter->rx_ring[i].count = adapter->rx_ring_count;
1837 adapter->rx_ring[i].queue_index = i;
1838 adapter->rx_ring[i].reg_idx = i;
1839 adapter->rx_ring[i].dev = &adapter->pdev->dev;
1840 adapter->rx_ring[i].netdev = adapter->netdev;
1845 err_rx_ring_allocation:
1846 kfree(adapter->tx_ring);
1847 err_tx_ring_allocation:
1852 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1853 * @adapter: board private structure to initialize
1855 * Attempt to configure the interrupts using the best available
1856 * capabilities of the hardware and the kernel.
1858 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1860 struct net_device *netdev = adapter->netdev;
1862 int vector, v_budget;
1865 * It's easy to be greedy for MSI-X vectors, but it really
1866 * doesn't do us much good if we have a lot more vectors
1867 * than CPU's. So let's be conservative and only ask for
1868 * (roughly) the same number of vectors as there are CPU's.
1869 * The default is to use pairs of vectors.
1871 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
1872 v_budget = min_t(int, v_budget, num_online_cpus());
1873 v_budget += NON_Q_VECTORS;
1875 /* A failure in MSI-X entry allocation isn't fatal, but it does
1876 * mean we disable MSI-X capabilities of the adapter. */
1877 adapter->msix_entries = kcalloc(v_budget,
1878 sizeof(struct msix_entry), GFP_KERNEL);
1879 if (!adapter->msix_entries) {
1884 for (vector = 0; vector < v_budget; vector++)
1885 adapter->msix_entries[vector].entry = vector;
1887 err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
1891 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
1895 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
1902 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1903 * @adapter: board private structure to initialize
1905 * We allocate one q_vector per queue interrupt. If allocation fails we
1908 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1910 int q_idx, num_q_vectors;
1911 struct ixgbevf_q_vector *q_vector;
1913 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1915 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1916 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1919 q_vector->adapter = adapter;
1920 q_vector->v_idx = q_idx;
1921 netif_napi_add(adapter->netdev, &q_vector->napi,
1923 adapter->q_vector[q_idx] = q_vector;
1931 q_vector = adapter->q_vector[q_idx];
1932 netif_napi_del(&q_vector->napi);
1934 adapter->q_vector[q_idx] = NULL;
1940 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1941 * @adapter: board private structure to initialize
1943 * This function frees the memory allocated to the q_vectors. In addition if
1944 * NAPI is enabled it will delete any references to the NAPI struct prior
1945 * to freeing the q_vector.
1947 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1949 int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1951 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1952 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1954 adapter->q_vector[q_idx] = NULL;
1955 netif_napi_del(&q_vector->napi);
1961 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1962 * @adapter: board private structure
1965 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1967 pci_disable_msix(adapter->pdev);
1968 kfree(adapter->msix_entries);
1969 adapter->msix_entries = NULL;
1973 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1974 * @adapter: board private structure to initialize
1977 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
1981 /* Number of supported queues */
1982 ixgbevf_set_num_queues(adapter);
1984 err = ixgbevf_set_interrupt_capability(adapter);
1986 hw_dbg(&adapter->hw,
1987 "Unable to setup interrupt capabilities\n");
1988 goto err_set_interrupt;
1991 err = ixgbevf_alloc_q_vectors(adapter);
1993 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
1995 goto err_alloc_q_vectors;
1998 err = ixgbevf_alloc_queues(adapter);
2000 pr_err("Unable to allocate memory for queues\n");
2001 goto err_alloc_queues;
2004 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
2005 "Tx Queue count = %u\n",
2006 (adapter->num_rx_queues > 1) ? "Enabled" :
2007 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2009 set_bit(__IXGBEVF_DOWN, &adapter->state);
2013 ixgbevf_free_q_vectors(adapter);
2014 err_alloc_q_vectors:
2015 ixgbevf_reset_interrupt_capability(adapter);
2021 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2022 * @adapter: board private structure to clear interrupt scheme on
2024 * We go through and clear interrupt specific resources and reset the structure
2025 * to pre-load conditions
2027 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2029 adapter->num_tx_queues = 0;
2030 adapter->num_rx_queues = 0;
2032 ixgbevf_free_q_vectors(adapter);
2033 ixgbevf_reset_interrupt_capability(adapter);
2037 * ixgbevf_sw_init - Initialize general software structures
2038 * (struct ixgbevf_adapter)
2039 * @adapter: board private structure to initialize
2041 * ixgbevf_sw_init initializes the Adapter private data structure.
2042 * Fields are initialized based on PCI device information and
2043 * OS network device settings (MTU size).
2045 static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2047 struct ixgbe_hw *hw = &adapter->hw;
2048 struct pci_dev *pdev = adapter->pdev;
2051 /* PCI config space info */
2053 hw->vendor_id = pdev->vendor;
2054 hw->device_id = pdev->device;
2055 hw->revision_id = pdev->revision;
2056 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2057 hw->subsystem_device_id = pdev->subsystem_device;
2059 hw->mbx.ops.init_params(hw);
2061 /* assume legacy case in which PF would only give VF 2 queues */
2062 hw->mac.max_tx_queues = 2;
2063 hw->mac.max_rx_queues = 2;
2065 err = hw->mac.ops.reset_hw(hw);
2067 dev_info(&pdev->dev,
2068 "PF still in reset state, assigning new address\n");
2069 eth_hw_addr_random(adapter->netdev);
2070 memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
2071 adapter->netdev->addr_len);
2073 err = hw->mac.ops.init_hw(hw);
2075 pr_err("init_shared_code failed: %d\n", err);
2078 memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
2079 adapter->netdev->addr_len);
2082 /* lock to protect mailbox accesses */
2083 spin_lock_init(&adapter->mbx_lock);
2085 /* Enable dynamic interrupt throttling rates */
2086 adapter->rx_itr_setting = 1;
2087 adapter->tx_itr_setting = 1;
2089 /* set default ring sizes */
2090 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2091 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2093 set_bit(__IXGBEVF_DOWN, &adapter->state);
2100 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2102 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2103 if (current_counter < last_counter) \
2104 counter += 0x100000000LL; \
2105 last_counter = current_counter; \
2106 counter &= 0xFFFFFFFF00000000LL; \
2107 counter |= current_counter; \
2110 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2112 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2113 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2114 u64 current_counter = (current_counter_msb << 32) | \
2115 current_counter_lsb; \
2116 if (current_counter < last_counter) \
2117 counter += 0x1000000000LL; \
2118 last_counter = current_counter; \
2119 counter &= 0xFFFFFFF000000000LL; \
2120 counter |= current_counter; \
2123 * ixgbevf_update_stats - Update the board statistics counters.
2124 * @adapter: board private structure
2126 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2128 struct ixgbe_hw *hw = &adapter->hw;
2131 if (!adapter->link_up)
2134 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2135 adapter->stats.vfgprc);
2136 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2137 adapter->stats.vfgptc);
2138 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2139 adapter->stats.last_vfgorc,
2140 adapter->stats.vfgorc);
2141 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2142 adapter->stats.last_vfgotc,
2143 adapter->stats.vfgotc);
2144 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2145 adapter->stats.vfmprc);
2147 for (i = 0; i < adapter->num_rx_queues; i++) {
2148 adapter->hw_csum_rx_error +=
2149 adapter->rx_ring[i].hw_csum_rx_error;
2150 adapter->hw_csum_rx_good +=
2151 adapter->rx_ring[i].hw_csum_rx_good;
2152 adapter->rx_ring[i].hw_csum_rx_error = 0;
2153 adapter->rx_ring[i].hw_csum_rx_good = 0;
2158 * ixgbevf_watchdog - Timer Call-back
2159 * @data: pointer to adapter cast into an unsigned long
2161 static void ixgbevf_watchdog(unsigned long data)
2163 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2164 struct ixgbe_hw *hw = &adapter->hw;
2169 * Do the watchdog outside of interrupt context due to the lovely
2170 * delays that some of the newer hardware requires
2173 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2174 goto watchdog_short_circuit;
2176 /* get one bit for every active tx/rx interrupt vector */
2177 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2178 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2179 if (qv->rx.ring || qv->tx.ring)
2183 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
2185 watchdog_short_circuit:
2186 schedule_work(&adapter->watchdog_task);
2190 * ixgbevf_tx_timeout - Respond to a Tx Hang
2191 * @netdev: network interface device structure
2193 static void ixgbevf_tx_timeout(struct net_device *netdev)
2195 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2197 /* Do the reset outside of interrupt context */
2198 schedule_work(&adapter->reset_task);
2201 static void ixgbevf_reset_task(struct work_struct *work)
2203 struct ixgbevf_adapter *adapter;
2204 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2206 /* If we're already down or resetting, just bail */
2207 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2208 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2211 adapter->tx_timeout_count++;
2213 ixgbevf_reinit_locked(adapter);
2217 * ixgbevf_watchdog_task - worker thread to bring link up
2218 * @work: pointer to work_struct containing our data
2220 static void ixgbevf_watchdog_task(struct work_struct *work)
2222 struct ixgbevf_adapter *adapter = container_of(work,
2223 struct ixgbevf_adapter,
2225 struct net_device *netdev = adapter->netdev;
2226 struct ixgbe_hw *hw = &adapter->hw;
2227 u32 link_speed = adapter->link_speed;
2228 bool link_up = adapter->link_up;
2231 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2234 * Always check the link on the watchdog because we have
2237 spin_lock_bh(&adapter->mbx_lock);
2239 need_reset = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
2241 spin_unlock_bh(&adapter->mbx_lock);
2244 adapter->link_up = link_up;
2245 adapter->link_speed = link_speed;
2246 netif_carrier_off(netdev);
2247 netif_tx_stop_all_queues(netdev);
2248 schedule_work(&adapter->reset_task);
2251 adapter->link_up = link_up;
2252 adapter->link_speed = link_speed;
2255 if (!netif_carrier_ok(netdev)) {
2256 char *link_speed_string;
2257 switch (link_speed) {
2258 case IXGBE_LINK_SPEED_10GB_FULL:
2259 link_speed_string = "10 Gbps";
2261 case IXGBE_LINK_SPEED_1GB_FULL:
2262 link_speed_string = "1 Gbps";
2264 case IXGBE_LINK_SPEED_100_FULL:
2265 link_speed_string = "100 Mbps";
2268 link_speed_string = "unknown speed";
2271 dev_info(&adapter->pdev->dev,
2272 "NIC Link is Up, %s\n", link_speed_string);
2273 netif_carrier_on(netdev);
2274 netif_tx_wake_all_queues(netdev);
2277 adapter->link_up = false;
2278 adapter->link_speed = 0;
2279 if (netif_carrier_ok(netdev)) {
2280 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
2281 netif_carrier_off(netdev);
2282 netif_tx_stop_all_queues(netdev);
2286 ixgbevf_update_stats(adapter);
2289 /* Reset the timer */
2290 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2291 mod_timer(&adapter->watchdog_timer,
2292 round_jiffies(jiffies + (2 * HZ)));
2294 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2298 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2299 * @adapter: board private structure
2300 * @tx_ring: Tx descriptor ring for a specific queue
2302 * Free all transmit software resources
2304 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2305 struct ixgbevf_ring *tx_ring)
2307 struct pci_dev *pdev = adapter->pdev;
2309 ixgbevf_clean_tx_ring(adapter, tx_ring);
2311 vfree(tx_ring->tx_buffer_info);
2312 tx_ring->tx_buffer_info = NULL;
2314 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2317 tx_ring->desc = NULL;
2321 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2322 * @adapter: board private structure
2324 * Free all transmit software resources
2326 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2330 for (i = 0; i < adapter->num_tx_queues; i++)
2331 if (adapter->tx_ring[i].desc)
2332 ixgbevf_free_tx_resources(adapter,
2333 &adapter->tx_ring[i]);
2338 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2339 * @adapter: board private structure
2340 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2342 * Return 0 on success, negative on failure
2344 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2345 struct ixgbevf_ring *tx_ring)
2347 struct pci_dev *pdev = adapter->pdev;
2350 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2351 tx_ring->tx_buffer_info = vzalloc(size);
2352 if (!tx_ring->tx_buffer_info)
2355 /* round up to nearest 4K */
2356 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2357 tx_ring->size = ALIGN(tx_ring->size, 4096);
2359 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2360 &tx_ring->dma, GFP_KERNEL);
2364 tx_ring->next_to_use = 0;
2365 tx_ring->next_to_clean = 0;
2369 vfree(tx_ring->tx_buffer_info);
2370 tx_ring->tx_buffer_info = NULL;
2371 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2372 "descriptor ring\n");
2377 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2378 * @adapter: board private structure
2380 * If this function returns with an error, then it's possible one or
2381 * more of the rings is populated (while the rest are not). It is the
2382 * callers duty to clean those orphaned rings.
2384 * Return 0 on success, negative on failure
2386 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2390 for (i = 0; i < adapter->num_tx_queues; i++) {
2391 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2394 hw_dbg(&adapter->hw,
2395 "Allocation for Tx Queue %u failed\n", i);
2403 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2404 * @adapter: board private structure
2405 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2407 * Returns 0 on success, negative on failure
2409 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2410 struct ixgbevf_ring *rx_ring)
2412 struct pci_dev *pdev = adapter->pdev;
2415 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2416 rx_ring->rx_buffer_info = vzalloc(size);
2417 if (!rx_ring->rx_buffer_info)
2420 /* Round up to nearest 4K */
2421 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2422 rx_ring->size = ALIGN(rx_ring->size, 4096);
2424 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2425 &rx_ring->dma, GFP_KERNEL);
2427 if (!rx_ring->desc) {
2428 hw_dbg(&adapter->hw,
2429 "Unable to allocate memory for "
2430 "the receive descriptor ring\n");
2431 vfree(rx_ring->rx_buffer_info);
2432 rx_ring->rx_buffer_info = NULL;
2436 rx_ring->next_to_clean = 0;
2437 rx_ring->next_to_use = 0;
2445 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2446 * @adapter: board private structure
2448 * If this function returns with an error, then it's possible one or
2449 * more of the rings is populated (while the rest are not). It is the
2450 * callers duty to clean those orphaned rings.
2452 * Return 0 on success, negative on failure
2454 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2458 for (i = 0; i < adapter->num_rx_queues; i++) {
2459 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2462 hw_dbg(&adapter->hw,
2463 "Allocation for Rx Queue %u failed\n", i);
2470 * ixgbevf_free_rx_resources - Free Rx Resources
2471 * @adapter: board private structure
2472 * @rx_ring: ring to clean the resources from
2474 * Free all receive software resources
2476 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2477 struct ixgbevf_ring *rx_ring)
2479 struct pci_dev *pdev = adapter->pdev;
2481 ixgbevf_clean_rx_ring(adapter, rx_ring);
2483 vfree(rx_ring->rx_buffer_info);
2484 rx_ring->rx_buffer_info = NULL;
2486 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2489 rx_ring->desc = NULL;
2493 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2494 * @adapter: board private structure
2496 * Free all receive software resources
2498 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2502 for (i = 0; i < adapter->num_rx_queues; i++)
2503 if (adapter->rx_ring[i].desc)
2504 ixgbevf_free_rx_resources(adapter,
2505 &adapter->rx_ring[i]);
2508 static int ixgbevf_setup_queues(struct ixgbevf_adapter *adapter)
2510 struct ixgbe_hw *hw = &adapter->hw;
2511 struct ixgbevf_ring *rx_ring;
2512 unsigned int def_q = 0;
2513 unsigned int num_tcs = 0;
2514 unsigned int num_rx_queues = 1;
2517 spin_lock_bh(&adapter->mbx_lock);
2519 /* fetch queue configuration from the PF */
2520 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2522 spin_unlock_bh(&adapter->mbx_lock);
2528 /* update default Tx ring register index */
2529 adapter->tx_ring[0].reg_idx = def_q;
2531 /* we need as many queues as traffic classes */
2532 num_rx_queues = num_tcs;
2535 /* nothing to do if we have the correct number of queues */
2536 if (adapter->num_rx_queues == num_rx_queues)
2539 /* allocate new rings */
2540 rx_ring = kcalloc(num_rx_queues,
2541 sizeof(struct ixgbevf_ring), GFP_KERNEL);
2545 /* setup ring fields */
2546 for (i = 0; i < num_rx_queues; i++) {
2547 rx_ring[i].count = adapter->rx_ring_count;
2548 rx_ring[i].queue_index = i;
2549 rx_ring[i].reg_idx = i;
2550 rx_ring[i].dev = &adapter->pdev->dev;
2551 rx_ring[i].netdev = adapter->netdev;
2554 /* free the existing ring and queues */
2555 adapter->num_rx_queues = 0;
2556 kfree(adapter->rx_ring);
2558 /* move new rings into position on the adapter struct */
2559 adapter->rx_ring = rx_ring;
2560 adapter->num_rx_queues = num_rx_queues;
2566 * ixgbevf_open - Called when a network interface is made active
2567 * @netdev: network interface device structure
2569 * Returns 0 on success, negative value on failure
2571 * The open entry point is called when a network interface is made
2572 * active by the system (IFF_UP). At this point all resources needed
2573 * for transmit and receive operations are allocated, the interrupt
2574 * handler is registered with the OS, the watchdog timer is started,
2575 * and the stack is notified that the interface is ready.
2577 static int ixgbevf_open(struct net_device *netdev)
2579 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2580 struct ixgbe_hw *hw = &adapter->hw;
2583 /* A previous failure to open the device because of a lack of
2584 * available MSIX vector resources may have reset the number
2585 * of msix vectors variable to zero. The only way to recover
2586 * is to unload/reload the driver and hope that the system has
2587 * been able to recover some MSIX vector resources.
2589 if (!adapter->num_msix_vectors)
2592 /* disallow open during test */
2593 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2596 if (hw->adapter_stopped) {
2597 ixgbevf_reset(adapter);
2598 /* if adapter is still stopped then PF isn't up and
2599 * the vf can't start. */
2600 if (hw->adapter_stopped) {
2601 err = IXGBE_ERR_MBX;
2602 pr_err("Unable to start - perhaps the PF Driver isn't "
2604 goto err_setup_reset;
2608 ixgbevf_negotiate_api(adapter);
2610 /* setup queue reg_idx and Rx queue count */
2611 err = ixgbevf_setup_queues(adapter);
2613 goto err_setup_queues;
2615 /* allocate transmit descriptors */
2616 err = ixgbevf_setup_all_tx_resources(adapter);
2620 /* allocate receive descriptors */
2621 err = ixgbevf_setup_all_rx_resources(adapter);
2625 ixgbevf_configure(adapter);
2628 * Map the Tx/Rx rings to the vectors we were allotted.
2629 * if request_irq will be called in this function map_rings
2630 * must be called *before* up_complete
2632 ixgbevf_map_rings_to_vectors(adapter);
2634 ixgbevf_up_complete(adapter);
2636 /* clear any pending interrupts, may auto mask */
2637 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2638 err = ixgbevf_request_irq(adapter);
2642 ixgbevf_irq_enable(adapter);
2647 ixgbevf_down(adapter);
2649 ixgbevf_free_all_rx_resources(adapter);
2651 ixgbevf_free_all_tx_resources(adapter);
2653 ixgbevf_reset(adapter);
2661 * ixgbevf_close - Disables a network interface
2662 * @netdev: network interface device structure
2664 * Returns 0, this is not allowed to fail
2666 * The close entry point is called when an interface is de-activated
2667 * by the OS. The hardware is still under the drivers control, but
2668 * needs to be disabled. A global MAC reset is issued to stop the
2669 * hardware, and all transmit and receive resources are freed.
2671 static int ixgbevf_close(struct net_device *netdev)
2673 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2675 ixgbevf_down(adapter);
2676 ixgbevf_free_irq(adapter);
2678 ixgbevf_free_all_tx_resources(adapter);
2679 ixgbevf_free_all_rx_resources(adapter);
2684 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2685 u32 vlan_macip_lens, u32 type_tucmd,
2688 struct ixgbe_adv_tx_context_desc *context_desc;
2689 u16 i = tx_ring->next_to_use;
2691 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2694 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2696 /* set bits to identify this as an advanced context descriptor */
2697 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2699 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2700 context_desc->seqnum_seed = 0;
2701 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2702 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2705 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
2706 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2708 u32 vlan_macip_lens, type_tucmd;
2709 u32 mss_l4len_idx, l4len;
2711 if (!skb_is_gso(skb))
2714 if (skb_header_cloned(skb)) {
2715 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2720 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2721 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2723 if (skb->protocol == htons(ETH_P_IP)) {
2724 struct iphdr *iph = ip_hdr(skb);
2727 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2731 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2732 } else if (skb_is_gso_v6(skb)) {
2733 ipv6_hdr(skb)->payload_len = 0;
2734 tcp_hdr(skb)->check =
2735 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2736 &ipv6_hdr(skb)->daddr,
2740 /* compute header lengths */
2741 l4len = tcp_hdrlen(skb);
2743 *hdr_len = skb_transport_offset(skb) + l4len;
2745 /* mss_l4len_id: use 1 as index for TSO */
2746 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2747 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2748 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2750 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2751 vlan_macip_lens = skb_network_header_len(skb);
2752 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2753 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2755 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2756 type_tucmd, mss_l4len_idx);
2761 static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2762 struct sk_buff *skb, u32 tx_flags)
2764 u32 vlan_macip_lens = 0;
2765 u32 mss_l4len_idx = 0;
2768 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2770 switch (skb->protocol) {
2771 case __constant_htons(ETH_P_IP):
2772 vlan_macip_lens |= skb_network_header_len(skb);
2773 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2774 l4_hdr = ip_hdr(skb)->protocol;
2776 case __constant_htons(ETH_P_IPV6):
2777 vlan_macip_lens |= skb_network_header_len(skb);
2778 l4_hdr = ipv6_hdr(skb)->nexthdr;
2781 if (unlikely(net_ratelimit())) {
2782 dev_warn(tx_ring->dev,
2783 "partial checksum but proto=%x!\n",
2791 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2792 mss_l4len_idx = tcp_hdrlen(skb) <<
2793 IXGBE_ADVTXD_L4LEN_SHIFT;
2796 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2797 mss_l4len_idx = sizeof(struct sctphdr) <<
2798 IXGBE_ADVTXD_L4LEN_SHIFT;
2801 mss_l4len_idx = sizeof(struct udphdr) <<
2802 IXGBE_ADVTXD_L4LEN_SHIFT;
2805 if (unlikely(net_ratelimit())) {
2806 dev_warn(tx_ring->dev,
2807 "partial checksum but l4 proto=%x!\n",
2814 /* vlan_macip_lens: MACLEN, VLAN tag */
2815 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2816 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2818 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2819 type_tucmd, mss_l4len_idx);
2821 return (skb->ip_summed == CHECKSUM_PARTIAL);
2824 static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
2825 struct sk_buff *skb, u32 tx_flags,
2828 struct ixgbevf_tx_buffer *tx_buffer_info;
2830 unsigned int total = skb->len;
2831 unsigned int offset = 0, size;
2833 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2837 i = tx_ring->next_to_use;
2839 len = min(skb_headlen(skb), total);
2841 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2842 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2844 tx_buffer_info->length = size;
2845 tx_buffer_info->mapped_as_page = false;
2846 tx_buffer_info->dma = dma_map_single(tx_ring->dev,
2848 size, DMA_TO_DEVICE);
2849 if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
2851 tx_buffer_info->next_to_watch = i;
2858 if (i == tx_ring->count)
2862 for (f = 0; f < nr_frags; f++) {
2863 const struct skb_frag_struct *frag;
2865 frag = &skb_shinfo(skb)->frags[f];
2866 len = min((unsigned int)skb_frag_size(frag), total);
2870 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2871 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2873 tx_buffer_info->length = size;
2874 tx_buffer_info->dma =
2875 skb_frag_dma_map(tx_ring->dev, frag,
2876 offset, size, DMA_TO_DEVICE);
2877 if (dma_mapping_error(tx_ring->dev,
2878 tx_buffer_info->dma))
2880 tx_buffer_info->mapped_as_page = true;
2881 tx_buffer_info->next_to_watch = i;
2888 if (i == tx_ring->count)
2896 i = tx_ring->count - 1;
2899 tx_ring->tx_buffer_info[i].skb = skb;
2900 tx_ring->tx_buffer_info[first].next_to_watch = i;
2901 tx_ring->tx_buffer_info[first].time_stamp = jiffies;
2906 dev_err(tx_ring->dev, "TX DMA map failed\n");
2908 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2909 tx_buffer_info->dma = 0;
2910 tx_buffer_info->next_to_watch = 0;
2913 /* clear timestamp and dma mappings for remaining portion of packet */
2914 while (count >= 0) {
2918 i += tx_ring->count;
2919 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2920 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
2926 static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
2927 int count, u32 paylen, u8 hdr_len)
2929 union ixgbe_adv_tx_desc *tx_desc = NULL;
2930 struct ixgbevf_tx_buffer *tx_buffer_info;
2931 u32 olinfo_status = 0, cmd_type_len = 0;
2934 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2936 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2938 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2940 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2941 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2943 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2944 olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
2946 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2947 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2949 /* use index 1 context for tso */
2950 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2951 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2952 olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
2956 * Check Context must be set if Tx switch is enabled, which it
2957 * always is for case where virtual functions are running
2959 olinfo_status |= IXGBE_ADVTXD_CC;
2961 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2963 i = tx_ring->next_to_use;
2965 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2966 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2967 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2968 tx_desc->read.cmd_type_len =
2969 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2970 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2972 if (i == tx_ring->count)
2976 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2978 tx_ring->next_to_use = i;
2981 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2983 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
2985 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
2986 /* Herbert's original patch had:
2987 * smp_mb__after_netif_stop_queue();
2988 * but since that doesn't exist yet, just open code it. */
2991 /* We need to check again in a case another CPU has just
2992 * made room available. */
2993 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
2996 /* A reprieve! - use start_queue because it doesn't call schedule */
2997 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2998 ++adapter->restart_queue;
3002 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
3004 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
3006 return __ixgbevf_maybe_stop_tx(tx_ring, size);
3009 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3011 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3012 struct ixgbevf_ring *tx_ring;
3014 unsigned int tx_flags = 0;
3017 u16 count = TXD_USE_COUNT(skb_headlen(skb));
3018 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3021 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
3022 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
3024 return NETDEV_TX_OK;
3027 tx_ring = &adapter->tx_ring[r_idx];
3030 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
3031 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
3032 * + 2 desc gap to keep tail from touching head,
3033 * + 1 desc for context descriptor,
3034 * otherwise try next time
3036 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3037 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3038 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3040 count += skb_shinfo(skb)->nr_frags;
3042 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
3044 return NETDEV_TX_BUSY;
3047 if (vlan_tx_tag_present(skb)) {
3048 tx_flags |= vlan_tx_tag_get(skb);
3049 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3050 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3053 first = tx_ring->next_to_use;
3055 if (skb->protocol == htons(ETH_P_IP))
3056 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3057 tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
3059 dev_kfree_skb_any(skb);
3060 return NETDEV_TX_OK;
3064 tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
3065 else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
3066 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3068 ixgbevf_tx_queue(tx_ring, tx_flags,
3069 ixgbevf_tx_map(tx_ring, skb, tx_flags, first),
3072 * Force memory writes to complete before letting h/w
3073 * know there are new descriptors to fetch. (Only
3074 * applicable for weak-ordered memory model archs,
3079 writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
3081 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
3083 return NETDEV_TX_OK;
3087 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3088 * @netdev: network interface device structure
3089 * @p: pointer to an address structure
3091 * Returns 0 on success, negative on failure
3093 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3095 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3096 struct ixgbe_hw *hw = &adapter->hw;
3097 struct sockaddr *addr = p;
3099 if (!is_valid_ether_addr(addr->sa_data))
3100 return -EADDRNOTAVAIL;
3102 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3103 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3105 spin_lock_bh(&adapter->mbx_lock);
3107 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
3109 spin_unlock_bh(&adapter->mbx_lock);
3115 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3116 * @netdev: network interface device structure
3117 * @new_mtu: new value for maximum frame size
3119 * Returns 0 on success, negative on failure
3121 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3123 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3124 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3125 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
3127 switch (adapter->hw.api_version) {
3128 case ixgbe_mbox_api_11:
3129 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3132 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3133 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3137 /* MTU < 68 is an error and causes problems on some kernels */
3138 if ((new_mtu < 68) || (max_frame > max_possible_frame))
3141 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3142 netdev->mtu, new_mtu);
3143 /* must set new MTU before calling down or up */
3144 netdev->mtu = new_mtu;
3146 if (netif_running(netdev))
3147 ixgbevf_reinit_locked(adapter);
3152 static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
3154 struct net_device *netdev = pci_get_drvdata(pdev);
3155 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3160 netif_device_detach(netdev);
3162 if (netif_running(netdev)) {
3164 ixgbevf_down(adapter);
3165 ixgbevf_free_irq(adapter);
3166 ixgbevf_free_all_tx_resources(adapter);
3167 ixgbevf_free_all_rx_resources(adapter);
3171 ixgbevf_clear_interrupt_scheme(adapter);
3174 retval = pci_save_state(pdev);
3179 pci_disable_device(pdev);
3185 static int ixgbevf_resume(struct pci_dev *pdev)
3187 struct ixgbevf_adapter *adapter = pci_get_drvdata(pdev);
3188 struct net_device *netdev = adapter->netdev;
3191 pci_set_power_state(pdev, PCI_D0);
3192 pci_restore_state(pdev);
3194 * pci_restore_state clears dev->state_saved so call
3195 * pci_save_state to restore it.
3197 pci_save_state(pdev);
3199 err = pci_enable_device_mem(pdev);
3201 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3204 pci_set_master(pdev);
3207 err = ixgbevf_init_interrupt_scheme(adapter);
3210 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3214 ixgbevf_reset(adapter);
3216 if (netif_running(netdev)) {
3217 err = ixgbevf_open(netdev);
3222 netif_device_attach(netdev);
3227 #endif /* CONFIG_PM */
3228 static void ixgbevf_shutdown(struct pci_dev *pdev)
3230 ixgbevf_suspend(pdev, PMSG_SUSPEND);
3233 static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3234 struct rtnl_link_stats64 *stats)
3236 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3239 const struct ixgbevf_ring *ring;
3242 ixgbevf_update_stats(adapter);
3244 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3246 for (i = 0; i < adapter->num_rx_queues; i++) {
3247 ring = &adapter->rx_ring[i];
3249 start = u64_stats_fetch_begin_bh(&ring->syncp);
3250 bytes = ring->total_bytes;
3251 packets = ring->total_packets;
3252 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3253 stats->rx_bytes += bytes;
3254 stats->rx_packets += packets;
3257 for (i = 0; i < adapter->num_tx_queues; i++) {
3258 ring = &adapter->tx_ring[i];
3260 start = u64_stats_fetch_begin_bh(&ring->syncp);
3261 bytes = ring->total_bytes;
3262 packets = ring->total_packets;
3263 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3264 stats->tx_bytes += bytes;
3265 stats->tx_packets += packets;
3271 static const struct net_device_ops ixgbevf_netdev_ops = {
3272 .ndo_open = ixgbevf_open,
3273 .ndo_stop = ixgbevf_close,
3274 .ndo_start_xmit = ixgbevf_xmit_frame,
3275 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
3276 .ndo_get_stats64 = ixgbevf_get_stats,
3277 .ndo_validate_addr = eth_validate_addr,
3278 .ndo_set_mac_address = ixgbevf_set_mac,
3279 .ndo_change_mtu = ixgbevf_change_mtu,
3280 .ndo_tx_timeout = ixgbevf_tx_timeout,
3281 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3282 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
3285 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3287 dev->netdev_ops = &ixgbevf_netdev_ops;
3288 ixgbevf_set_ethtool_ops(dev);
3289 dev->watchdog_timeo = 5 * HZ;
3293 * ixgbevf_probe - Device Initialization Routine
3294 * @pdev: PCI device information struct
3295 * @ent: entry in ixgbevf_pci_tbl
3297 * Returns 0 on success, negative on failure
3299 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3300 * The OS initialization, configuring of the adapter private structure,
3301 * and a hardware reset occur.
3303 static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3305 struct net_device *netdev;
3306 struct ixgbevf_adapter *adapter = NULL;
3307 struct ixgbe_hw *hw = NULL;
3308 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3309 static int cards_found;
3310 int err, pci_using_dac;
3312 err = pci_enable_device(pdev);
3316 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3317 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3320 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3322 err = dma_set_coherent_mask(&pdev->dev,
3325 dev_err(&pdev->dev, "No usable DMA "
3326 "configuration, aborting\n");
3333 err = pci_request_regions(pdev, ixgbevf_driver_name);
3335 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3339 pci_set_master(pdev);
3341 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3345 goto err_alloc_etherdev;
3348 SET_NETDEV_DEV(netdev, &pdev->dev);
3350 pci_set_drvdata(pdev, netdev);
3351 adapter = netdev_priv(netdev);
3353 adapter->netdev = netdev;
3354 adapter->pdev = pdev;
3357 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3360 * call save state here in standalone driver because it relies on
3361 * adapter struct to exist, and needs to call netdev_priv
3363 pci_save_state(pdev);
3365 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3366 pci_resource_len(pdev, 0));
3372 ixgbevf_assign_netdev_ops(netdev);
3374 adapter->bd_number = cards_found;
3377 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3378 hw->mac.type = ii->mac;
3380 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3381 sizeof(struct ixgbe_mbx_operations));
3383 /* setup the private structure */
3384 err = ixgbevf_sw_init(adapter);
3388 /* The HW MAC address was set and/or determined in sw_init */
3389 if (!is_valid_ether_addr(netdev->dev_addr)) {
3390 pr_err("invalid MAC address\n");
3395 netdev->hw_features = NETIF_F_SG |
3402 netdev->features = netdev->hw_features |
3403 NETIF_F_HW_VLAN_TX |
3404 NETIF_F_HW_VLAN_RX |
3405 NETIF_F_HW_VLAN_FILTER;
3407 netdev->vlan_features |= NETIF_F_TSO;
3408 netdev->vlan_features |= NETIF_F_TSO6;
3409 netdev->vlan_features |= NETIF_F_IP_CSUM;
3410 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3411 netdev->vlan_features |= NETIF_F_SG;
3414 netdev->features |= NETIF_F_HIGHDMA;
3416 netdev->priv_flags |= IFF_UNICAST_FLT;
3418 init_timer(&adapter->watchdog_timer);
3419 adapter->watchdog_timer.function = ixgbevf_watchdog;
3420 adapter->watchdog_timer.data = (unsigned long)adapter;
3422 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3423 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3425 err = ixgbevf_init_interrupt_scheme(adapter);
3429 strcpy(netdev->name, "eth%d");
3431 err = register_netdev(netdev);
3435 netif_carrier_off(netdev);
3437 ixgbevf_init_last_counter_stats(adapter);
3439 /* print the MAC address */
3440 hw_dbg(hw, "%pM\n", netdev->dev_addr);
3442 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3444 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3449 ixgbevf_clear_interrupt_scheme(adapter);
3451 ixgbevf_reset_interrupt_capability(adapter);
3452 iounmap(hw->hw_addr);
3454 free_netdev(netdev);
3456 pci_release_regions(pdev);
3459 pci_disable_device(pdev);
3464 * ixgbevf_remove - Device Removal Routine
3465 * @pdev: PCI device information struct
3467 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3468 * that it should release a PCI device. The could be caused by a
3469 * Hot-Plug event, or because the driver is going to be removed from
3472 static void ixgbevf_remove(struct pci_dev *pdev)
3474 struct net_device *netdev = pci_get_drvdata(pdev);
3475 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3477 set_bit(__IXGBEVF_DOWN, &adapter->state);
3479 del_timer_sync(&adapter->watchdog_timer);
3481 cancel_work_sync(&adapter->reset_task);
3482 cancel_work_sync(&adapter->watchdog_task);
3484 if (netdev->reg_state == NETREG_REGISTERED)
3485 unregister_netdev(netdev);
3487 ixgbevf_clear_interrupt_scheme(adapter);
3488 ixgbevf_reset_interrupt_capability(adapter);
3490 iounmap(adapter->hw.hw_addr);
3491 pci_release_regions(pdev);
3493 hw_dbg(&adapter->hw, "Remove complete\n");
3495 kfree(adapter->tx_ring);
3496 kfree(adapter->rx_ring);
3498 free_netdev(netdev);
3500 pci_disable_device(pdev);
3504 * ixgbevf_io_error_detected - called when PCI error is detected
3505 * @pdev: Pointer to PCI device
3506 * @state: The current pci connection state
3508 * This function is called after a PCI bus error affecting
3509 * this device has been detected.
3511 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3512 pci_channel_state_t state)
3514 struct net_device *netdev = pci_get_drvdata(pdev);
3515 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3517 netif_device_detach(netdev);
3519 if (state == pci_channel_io_perm_failure)
3520 return PCI_ERS_RESULT_DISCONNECT;
3522 if (netif_running(netdev))
3523 ixgbevf_down(adapter);
3525 pci_disable_device(pdev);
3527 /* Request a slot slot reset. */
3528 return PCI_ERS_RESULT_NEED_RESET;
3532 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3533 * @pdev: Pointer to PCI device
3535 * Restart the card from scratch, as if from a cold-boot. Implementation
3536 * resembles the first-half of the ixgbevf_resume routine.
3538 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3540 struct net_device *netdev = pci_get_drvdata(pdev);
3541 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3543 if (pci_enable_device_mem(pdev)) {
3545 "Cannot re-enable PCI device after reset.\n");
3546 return PCI_ERS_RESULT_DISCONNECT;
3549 pci_set_master(pdev);
3551 ixgbevf_reset(adapter);
3553 return PCI_ERS_RESULT_RECOVERED;
3557 * ixgbevf_io_resume - called when traffic can start flowing again.
3558 * @pdev: Pointer to PCI device
3560 * This callback is called when the error recovery driver tells us that
3561 * its OK to resume normal operation. Implementation resembles the
3562 * second-half of the ixgbevf_resume routine.
3564 static void ixgbevf_io_resume(struct pci_dev *pdev)
3566 struct net_device *netdev = pci_get_drvdata(pdev);
3567 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3569 if (netif_running(netdev))
3570 ixgbevf_up(adapter);
3572 netif_device_attach(netdev);
3575 /* PCI Error Recovery (ERS) */
3576 static const struct pci_error_handlers ixgbevf_err_handler = {
3577 .error_detected = ixgbevf_io_error_detected,
3578 .slot_reset = ixgbevf_io_slot_reset,
3579 .resume = ixgbevf_io_resume,
3582 static struct pci_driver ixgbevf_driver = {
3583 .name = ixgbevf_driver_name,
3584 .id_table = ixgbevf_pci_tbl,
3585 .probe = ixgbevf_probe,
3586 .remove = ixgbevf_remove,
3588 /* Power Management Hooks */
3589 .suspend = ixgbevf_suspend,
3590 .resume = ixgbevf_resume,
3592 .shutdown = ixgbevf_shutdown,
3593 .err_handler = &ixgbevf_err_handler
3597 * ixgbevf_init_module - Driver Registration Routine
3599 * ixgbevf_init_module is the first routine called when the driver is
3600 * loaded. All it does is register with the PCI subsystem.
3602 static int __init ixgbevf_init_module(void)
3605 pr_info("%s - version %s\n", ixgbevf_driver_string,
3606 ixgbevf_driver_version);
3608 pr_info("%s\n", ixgbevf_copyright);
3610 ret = pci_register_driver(&ixgbevf_driver);
3614 module_init(ixgbevf_init_module);
3617 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3619 * ixgbevf_exit_module is called just before the driver is removed
3622 static void __exit ixgbevf_exit_module(void)
3624 pci_unregister_driver(&ixgbevf_driver);
3629 * ixgbevf_get_hw_dev_name - return device name string
3630 * used by hardware layer to print debugging information
3632 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3634 struct ixgbevf_adapter *adapter = hw->back;
3635 return adapter->netdev->name;
3639 module_exit(ixgbevf_exit_module);
3641 /* ixgbevf_main.c */