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.11.3-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 DEFINE_PCI_DEVICE_TABLE(ixgbevf_pci_tbl) = {
80 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF), board_82599_vf },
81 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF), board_X540_vf },
82 /* required last entry */
85 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
87 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
88 MODULE_DESCRIPTION("Intel(R) 82599 Virtual Function Driver");
89 MODULE_LICENSE("GPL");
90 MODULE_VERSION(DRV_VERSION);
92 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
93 static int debug = -1;
94 module_param(debug, int, 0);
95 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
98 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
99 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
101 static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
102 struct ixgbevf_ring *rx_ring,
106 * Force memory writes to complete before letting h/w
107 * know there are new descriptors to fetch. (Only
108 * applicable for weak-ordered memory model archs,
112 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
116 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
117 * @adapter: pointer to adapter struct
118 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
119 * @queue: queue to map the corresponding interrupt to
120 * @msix_vector: the vector to map to the corresponding queue
122 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
123 u8 queue, u8 msix_vector)
126 struct ixgbe_hw *hw = &adapter->hw;
127 if (direction == -1) {
129 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
130 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
133 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
135 /* tx or rx causes */
136 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
137 index = ((16 * (queue & 1)) + (8 * direction));
138 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
139 ivar &= ~(0xFF << index);
140 ivar |= (msix_vector << index);
141 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
145 static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
146 struct ixgbevf_tx_buffer
149 if (tx_buffer_info->dma) {
150 if (tx_buffer_info->mapped_as_page)
151 dma_unmap_page(tx_ring->dev,
153 tx_buffer_info->length,
156 dma_unmap_single(tx_ring->dev,
158 tx_buffer_info->length,
160 tx_buffer_info->dma = 0;
162 if (tx_buffer_info->skb) {
163 dev_kfree_skb_any(tx_buffer_info->skb);
164 tx_buffer_info->skb = NULL;
166 tx_buffer_info->time_stamp = 0;
167 /* tx_buffer_info must be completely set up in the transmit path */
170 #define IXGBE_MAX_TXD_PWR 14
171 #define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
173 /* Tx Descriptors needed, worst case */
174 #define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
175 #define DESC_NEEDED (MAX_SKB_FRAGS + 4)
177 static void ixgbevf_tx_timeout(struct net_device *netdev);
180 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
181 * @q_vector: board private structure
182 * @tx_ring: tx ring to clean
184 static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
185 struct ixgbevf_ring *tx_ring)
187 struct ixgbevf_adapter *adapter = q_vector->adapter;
188 union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
189 struct ixgbevf_tx_buffer *tx_buffer_info;
190 unsigned int i, count = 0;
191 unsigned int total_bytes = 0, total_packets = 0;
193 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
196 i = tx_ring->next_to_clean;
197 tx_buffer_info = &tx_ring->tx_buffer_info[i];
198 eop_desc = tx_buffer_info->next_to_watch;
201 bool cleaned = false;
203 /* if next_to_watch is not set then there is no work pending */
207 /* prevent any other reads prior to eop_desc */
208 read_barrier_depends();
210 /* if DD is not set pending work has not been completed */
211 if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
214 /* clear next_to_watch to prevent false hangs */
215 tx_buffer_info->next_to_watch = NULL;
217 for ( ; !cleaned; count++) {
219 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
220 cleaned = (tx_desc == eop_desc);
221 skb = tx_buffer_info->skb;
223 if (cleaned && skb) {
224 unsigned int segs, bytecount;
226 /* gso_segs is currently only valid for tcp */
227 segs = skb_shinfo(skb)->gso_segs ?: 1;
228 /* multiply data chunks by size of headers */
229 bytecount = ((segs - 1) * skb_headlen(skb)) +
231 total_packets += segs;
232 total_bytes += bytecount;
235 ixgbevf_unmap_and_free_tx_resource(tx_ring,
238 tx_desc->wb.status = 0;
241 if (i == tx_ring->count)
244 tx_buffer_info = &tx_ring->tx_buffer_info[i];
247 eop_desc = tx_buffer_info->next_to_watch;
248 } while (count < tx_ring->count);
250 tx_ring->next_to_clean = i;
252 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
253 if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
254 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
255 /* Make sure that anybody stopping the queue after this
256 * sees the new next_to_clean.
259 if (__netif_subqueue_stopped(tx_ring->netdev,
260 tx_ring->queue_index) &&
261 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
262 netif_wake_subqueue(tx_ring->netdev,
263 tx_ring->queue_index);
264 ++adapter->restart_queue;
268 u64_stats_update_begin(&tx_ring->syncp);
269 tx_ring->total_bytes += total_bytes;
270 tx_ring->total_packets += total_packets;
271 u64_stats_update_end(&tx_ring->syncp);
272 q_vector->tx.total_bytes += total_bytes;
273 q_vector->tx.total_packets += total_packets;
275 return count < tx_ring->count;
279 * ixgbevf_receive_skb - Send a completed packet up the stack
280 * @q_vector: structure containing interrupt and ring information
281 * @skb: packet to send up
282 * @status: hardware indication of status of receive
283 * @rx_desc: rx descriptor
285 static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
286 struct sk_buff *skb, u8 status,
287 union ixgbe_adv_rx_desc *rx_desc)
289 struct ixgbevf_adapter *adapter = q_vector->adapter;
290 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
291 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
293 if (is_vlan && test_bit(tag & VLAN_VID_MASK, adapter->active_vlans))
294 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tag);
296 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
297 napi_gro_receive(&q_vector->napi, skb);
303 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
304 * @ring: pointer to Rx descriptor ring structure
305 * @status_err: hardware indication of status of receive
306 * @skb: skb currently being received and modified
308 static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
309 u32 status_err, struct sk_buff *skb)
311 skb_checksum_none_assert(skb);
313 /* Rx csum disabled */
314 if (!(ring->netdev->features & NETIF_F_RXCSUM))
317 /* if IP and error */
318 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
319 (status_err & IXGBE_RXDADV_ERR_IPE)) {
320 ring->hw_csum_rx_error++;
324 if (!(status_err & IXGBE_RXD_STAT_L4CS))
327 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
328 ring->hw_csum_rx_error++;
332 /* It must be a TCP or UDP packet with a valid checksum */
333 skb->ip_summed = CHECKSUM_UNNECESSARY;
334 ring->hw_csum_rx_good++;
338 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
339 * @adapter: address of board private structure
341 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
342 struct ixgbevf_ring *rx_ring,
345 struct pci_dev *pdev = adapter->pdev;
346 union ixgbe_adv_rx_desc *rx_desc;
347 struct ixgbevf_rx_buffer *bi;
348 unsigned int i = rx_ring->next_to_use;
350 bi = &rx_ring->rx_buffer_info[i];
352 while (cleaned_count--) {
353 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
358 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
359 rx_ring->rx_buf_len);
361 adapter->alloc_rx_buff_failed++;
366 bi->dma = dma_map_single(&pdev->dev, skb->data,
369 if (dma_mapping_error(&pdev->dev, bi->dma)) {
372 dev_err(&pdev->dev, "RX DMA map failed\n");
376 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
379 if (i == rx_ring->count)
381 bi = &rx_ring->rx_buffer_info[i];
385 if (rx_ring->next_to_use != i) {
386 rx_ring->next_to_use = i;
387 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
391 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
394 struct ixgbe_hw *hw = &adapter->hw;
396 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
399 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
400 struct ixgbevf_ring *rx_ring,
403 struct ixgbevf_adapter *adapter = q_vector->adapter;
404 struct pci_dev *pdev = adapter->pdev;
405 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
406 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
410 int cleaned_count = 0;
411 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
413 i = rx_ring->next_to_clean;
414 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
415 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
416 rx_buffer_info = &rx_ring->rx_buffer_info[i];
418 while (staterr & IXGBE_RXD_STAT_DD) {
423 rmb(); /* read descriptor and rx_buffer_info after status DD */
424 len = le16_to_cpu(rx_desc->wb.upper.length);
425 skb = rx_buffer_info->skb;
426 prefetch(skb->data - NET_IP_ALIGN);
427 rx_buffer_info->skb = NULL;
429 if (rx_buffer_info->dma) {
430 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
433 rx_buffer_info->dma = 0;
438 if (i == rx_ring->count)
441 next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
445 next_buffer = &rx_ring->rx_buffer_info[i];
447 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
448 skb->next = next_buffer->skb;
449 IXGBE_CB(skb->next)->prev = skb;
450 adapter->non_eop_descs++;
454 /* we should not be chaining buffers, if we did drop the skb */
455 if (IXGBE_CB(skb)->prev) {
457 struct sk_buff *this = skb;
458 skb = IXGBE_CB(skb)->prev;
464 /* ERR_MASK will only have valid bits if EOP set */
465 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
466 dev_kfree_skb_irq(skb);
470 ixgbevf_rx_checksum(rx_ring, staterr, skb);
472 /* probably a little skewed due to removing CRC */
473 total_rx_bytes += skb->len;
477 * Work around issue of some types of VM to VM loop back
478 * packets not getting split correctly
480 if (staterr & IXGBE_RXD_STAT_LB) {
481 u32 header_fixup_len = skb_headlen(skb);
482 if (header_fixup_len < 14)
483 skb_push(skb, header_fixup_len);
485 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
487 /* Workaround hardware that can't do proper VEPA multicast
490 if ((skb->pkt_type & (PACKET_BROADCAST | PACKET_MULTICAST)) &&
491 ether_addr_equal(adapter->netdev->dev_addr,
492 eth_hdr(skb)->h_source)) {
493 dev_kfree_skb_irq(skb);
497 ixgbevf_receive_skb(q_vector, skb, staterr, rx_desc);
500 rx_desc->wb.upper.status_error = 0;
502 /* return some buffers to hardware, one at a time is too slow */
503 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
504 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
509 /* use prefetched values */
511 rx_buffer_info = &rx_ring->rx_buffer_info[i];
513 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
516 rx_ring->next_to_clean = i;
517 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
520 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
522 u64_stats_update_begin(&rx_ring->syncp);
523 rx_ring->total_packets += total_rx_packets;
524 rx_ring->total_bytes += total_rx_bytes;
525 u64_stats_update_end(&rx_ring->syncp);
526 q_vector->rx.total_packets += total_rx_packets;
527 q_vector->rx.total_bytes += total_rx_bytes;
533 * ixgbevf_poll - NAPI polling calback
534 * @napi: napi struct with our devices info in it
535 * @budget: amount of work driver is allowed to do this pass, in packets
537 * This function will clean more than one or more rings associated with a
540 static int ixgbevf_poll(struct napi_struct *napi, int budget)
542 struct ixgbevf_q_vector *q_vector =
543 container_of(napi, struct ixgbevf_q_vector, napi);
544 struct ixgbevf_adapter *adapter = q_vector->adapter;
545 struct ixgbevf_ring *ring;
547 bool clean_complete = true;
549 ixgbevf_for_each_ring(ring, q_vector->tx)
550 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
552 /* attempt to distribute budget to each queue fairly, but don't allow
553 * the budget to go below 1 because we'll exit polling */
554 if (q_vector->rx.count > 1)
555 per_ring_budget = max(budget/q_vector->rx.count, 1);
557 per_ring_budget = budget;
559 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
560 ixgbevf_for_each_ring(ring, q_vector->rx)
561 clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
563 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
565 /* If all work not completed, return budget and keep polling */
568 /* all work done, exit the polling mode */
570 if (adapter->rx_itr_setting & 1)
571 ixgbevf_set_itr(q_vector);
572 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
573 ixgbevf_irq_enable_queues(adapter,
574 1 << q_vector->v_idx);
580 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
581 * @q_vector: structure containing interrupt and ring information
583 void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
585 struct ixgbevf_adapter *adapter = q_vector->adapter;
586 struct ixgbe_hw *hw = &adapter->hw;
587 int v_idx = q_vector->v_idx;
588 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
591 * set the WDIS bit to not clear the timer bits and cause an
592 * immediate assertion of the interrupt
594 itr_reg |= IXGBE_EITR_CNT_WDIS;
596 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
600 * ixgbevf_configure_msix - Configure MSI-X hardware
601 * @adapter: board private structure
603 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
606 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
608 struct ixgbevf_q_vector *q_vector;
609 int q_vectors, v_idx;
611 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
612 adapter->eims_enable_mask = 0;
615 * Populate the IVAR table and set the ITR values to the
616 * corresponding register.
618 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
619 struct ixgbevf_ring *ring;
620 q_vector = adapter->q_vector[v_idx];
622 ixgbevf_for_each_ring(ring, q_vector->rx)
623 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
625 ixgbevf_for_each_ring(ring, q_vector->tx)
626 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
628 if (q_vector->tx.ring && !q_vector->rx.ring) {
630 if (adapter->tx_itr_setting == 1)
631 q_vector->itr = IXGBE_10K_ITR;
633 q_vector->itr = adapter->tx_itr_setting;
635 /* rx or rx/tx vector */
636 if (adapter->rx_itr_setting == 1)
637 q_vector->itr = IXGBE_20K_ITR;
639 q_vector->itr = adapter->rx_itr_setting;
642 /* add q_vector eims value to global eims_enable_mask */
643 adapter->eims_enable_mask |= 1 << v_idx;
645 ixgbevf_write_eitr(q_vector);
648 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
649 /* setup eims_other and add value to global eims_enable_mask */
650 adapter->eims_other = 1 << v_idx;
651 adapter->eims_enable_mask |= adapter->eims_other;
658 latency_invalid = 255
662 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
663 * @q_vector: structure containing interrupt and ring information
664 * @ring_container: structure containing ring performance data
666 * Stores a new ITR value based on packets and byte
667 * counts during the last interrupt. The advantage of per interrupt
668 * computation is faster updates and more accurate ITR for the current
669 * traffic pattern. Constants in this function were computed
670 * based on theoretical maximum wire speed and thresholds were set based
671 * on testing data as well as attempting to minimize response time
672 * while increasing bulk throughput.
674 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
675 struct ixgbevf_ring_container *ring_container)
677 int bytes = ring_container->total_bytes;
678 int packets = ring_container->total_packets;
681 u8 itr_setting = ring_container->itr;
686 /* simple throttlerate management
687 * 0-20MB/s lowest (100000 ints/s)
688 * 20-100MB/s low (20000 ints/s)
689 * 100-1249MB/s bulk (8000 ints/s)
691 /* what was last interrupt timeslice? */
692 timepassed_us = q_vector->itr >> 2;
693 bytes_perint = bytes / timepassed_us; /* bytes/usec */
695 switch (itr_setting) {
697 if (bytes_perint > 10)
698 itr_setting = low_latency;
701 if (bytes_perint > 20)
702 itr_setting = bulk_latency;
703 else if (bytes_perint <= 10)
704 itr_setting = lowest_latency;
707 if (bytes_perint <= 20)
708 itr_setting = low_latency;
712 /* clear work counters since we have the values we need */
713 ring_container->total_bytes = 0;
714 ring_container->total_packets = 0;
716 /* write updated itr to ring container */
717 ring_container->itr = itr_setting;
720 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
722 u32 new_itr = q_vector->itr;
725 ixgbevf_update_itr(q_vector, &q_vector->tx);
726 ixgbevf_update_itr(q_vector, &q_vector->rx);
728 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
730 switch (current_itr) {
731 /* counts and packets in update_itr are dependent on these numbers */
733 new_itr = IXGBE_100K_ITR;
736 new_itr = IXGBE_20K_ITR;
740 new_itr = IXGBE_8K_ITR;
744 if (new_itr != q_vector->itr) {
745 /* do an exponential smoothing */
746 new_itr = (10 * new_itr * q_vector->itr) /
747 ((9 * new_itr) + q_vector->itr);
749 /* save the algorithm value here */
750 q_vector->itr = new_itr;
752 ixgbevf_write_eitr(q_vector);
756 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
758 struct ixgbevf_adapter *adapter = data;
759 struct ixgbe_hw *hw = &adapter->hw;
761 hw->mac.get_link_status = 1;
763 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
764 mod_timer(&adapter->watchdog_timer, jiffies);
766 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
772 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
774 * @data: pointer to our q_vector struct for this interrupt vector
776 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
778 struct ixgbevf_q_vector *q_vector = data;
780 /* EIAM disabled interrupts (on this vector) for us */
781 if (q_vector->rx.ring || q_vector->tx.ring)
782 napi_schedule(&q_vector->napi);
787 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
790 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
792 a->rx_ring[r_idx].next = q_vector->rx.ring;
793 q_vector->rx.ring = &a->rx_ring[r_idx];
794 q_vector->rx.count++;
797 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
800 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
802 a->tx_ring[t_idx].next = q_vector->tx.ring;
803 q_vector->tx.ring = &a->tx_ring[t_idx];
804 q_vector->tx.count++;
808 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
809 * @adapter: board private structure to initialize
811 * This function maps descriptor rings to the queue-specific vectors
812 * we were allotted through the MSI-X enabling code. Ideally, we'd have
813 * one vector per ring/queue, but on a constrained vector budget, we
814 * group the rings as "efficiently" as possible. You would add new
815 * mapping configurations in here.
817 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
821 int rxr_idx = 0, txr_idx = 0;
822 int rxr_remaining = adapter->num_rx_queues;
823 int txr_remaining = adapter->num_tx_queues;
828 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
831 * The ideal configuration...
832 * We have enough vectors to map one per queue.
834 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
835 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
836 map_vector_to_rxq(adapter, v_start, rxr_idx);
838 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
839 map_vector_to_txq(adapter, v_start, txr_idx);
844 * If we don't have enough vectors for a 1-to-1
845 * mapping, we'll have to group them so there are
846 * multiple queues per vector.
848 /* Re-adjusting *qpv takes care of the remainder. */
849 for (i = v_start; i < q_vectors; i++) {
850 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
851 for (j = 0; j < rqpv; j++) {
852 map_vector_to_rxq(adapter, i, rxr_idx);
857 for (i = v_start; i < q_vectors; i++) {
858 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
859 for (j = 0; j < tqpv; j++) {
860 map_vector_to_txq(adapter, i, txr_idx);
871 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
872 * @adapter: board private structure
874 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
875 * interrupts from the kernel.
877 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
879 struct net_device *netdev = adapter->netdev;
880 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
884 for (vector = 0; vector < q_vectors; vector++) {
885 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
886 struct msix_entry *entry = &adapter->msix_entries[vector];
888 if (q_vector->tx.ring && q_vector->rx.ring) {
889 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
890 "%s-%s-%d", netdev->name, "TxRx", ri++);
892 } else if (q_vector->rx.ring) {
893 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
894 "%s-%s-%d", netdev->name, "rx", ri++);
895 } else if (q_vector->tx.ring) {
896 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
897 "%s-%s-%d", netdev->name, "tx", ti++);
899 /* skip this unused q_vector */
902 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
903 q_vector->name, q_vector);
906 "request_irq failed for MSIX interrupt "
908 goto free_queue_irqs;
912 err = request_irq(adapter->msix_entries[vector].vector,
913 &ixgbevf_msix_other, 0, netdev->name, adapter);
916 "request_irq for msix_other failed: %d\n", err);
917 goto free_queue_irqs;
925 free_irq(adapter->msix_entries[vector].vector,
926 adapter->q_vector[vector]);
928 /* This failure is non-recoverable - it indicates the system is
929 * out of MSIX vector resources and the VF driver cannot run
930 * without them. Set the number of msix vectors to zero
931 * indicating that not enough can be allocated. The error
932 * will be returned to the user indicating device open failed.
933 * Any further attempts to force the driver to open will also
934 * fail. The only way to recover is to unload the driver and
935 * reload it again. If the system has recovered some MSIX
936 * vectors then it may succeed.
938 adapter->num_msix_vectors = 0;
942 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
944 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
946 for (i = 0; i < q_vectors; i++) {
947 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
948 q_vector->rx.ring = NULL;
949 q_vector->tx.ring = NULL;
950 q_vector->rx.count = 0;
951 q_vector->tx.count = 0;
956 * ixgbevf_request_irq - initialize interrupts
957 * @adapter: board private structure
959 * Attempts to configure interrupts using the best available
960 * capabilities of the hardware and kernel.
962 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
966 err = ixgbevf_request_msix_irqs(adapter);
970 "request_irq failed, Error %d\n", err);
975 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
979 q_vectors = adapter->num_msix_vectors;
982 free_irq(adapter->msix_entries[i].vector, adapter);
985 for (; i >= 0; i--) {
986 /* free only the irqs that were actually requested */
987 if (!adapter->q_vector[i]->rx.ring &&
988 !adapter->q_vector[i]->tx.ring)
991 free_irq(adapter->msix_entries[i].vector,
992 adapter->q_vector[i]);
995 ixgbevf_reset_q_vectors(adapter);
999 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1000 * @adapter: board private structure
1002 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1004 struct ixgbe_hw *hw = &adapter->hw;
1007 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
1008 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1009 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
1011 IXGBE_WRITE_FLUSH(hw);
1013 for (i = 0; i < adapter->num_msix_vectors; i++)
1014 synchronize_irq(adapter->msix_entries[i].vector);
1018 * ixgbevf_irq_enable - Enable default interrupt generation settings
1019 * @adapter: board private structure
1021 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1023 struct ixgbe_hw *hw = &adapter->hw;
1025 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1026 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1027 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1031 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1032 * @adapter: board private structure
1034 * Configure the Tx unit of the MAC after a reset.
1036 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1039 struct ixgbe_hw *hw = &adapter->hw;
1040 u32 i, j, tdlen, txctrl;
1042 /* Setup the HW Tx Head and Tail descriptor pointers */
1043 for (i = 0; i < adapter->num_tx_queues; i++) {
1044 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1047 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1048 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1049 (tdba & DMA_BIT_MASK(32)));
1050 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1051 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1052 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1053 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1054 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1055 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1056 /* Disable Tx Head Writeback RO bit, since this hoses
1057 * bookkeeping if things aren't delivered in order.
1059 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1060 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1061 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1065 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1067 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1069 struct ixgbevf_ring *rx_ring;
1070 struct ixgbe_hw *hw = &adapter->hw;
1073 rx_ring = &adapter->rx_ring[index];
1075 srrctl = IXGBE_SRRCTL_DROP_EN;
1077 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1079 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1080 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1082 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1085 static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1087 struct ixgbe_hw *hw = &adapter->hw;
1089 /* PSRTYPE must be initialized in 82599 */
1090 u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1091 IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1092 IXGBE_PSRTYPE_L2HDR;
1094 if (adapter->num_rx_queues > 1)
1097 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1100 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1102 struct ixgbe_hw *hw = &adapter->hw;
1103 struct net_device *netdev = adapter->netdev;
1104 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1108 /* notify the PF of our intent to use this size of frame */
1109 ixgbevf_rlpml_set_vf(hw, max_frame);
1111 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1112 max_frame += VLAN_HLEN;
1115 * Allocate buffer sizes that fit well into 32K and
1116 * take into account max frame size of 9.5K
1118 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1119 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1120 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1121 else if (max_frame <= IXGBEVF_RXBUFFER_2K)
1122 rx_buf_len = IXGBEVF_RXBUFFER_2K;
1123 else if (max_frame <= IXGBEVF_RXBUFFER_4K)
1124 rx_buf_len = IXGBEVF_RXBUFFER_4K;
1125 else if (max_frame <= IXGBEVF_RXBUFFER_8K)
1126 rx_buf_len = IXGBEVF_RXBUFFER_8K;
1128 rx_buf_len = IXGBEVF_RXBUFFER_10K;
1130 for (i = 0; i < adapter->num_rx_queues; i++)
1131 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1135 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1136 * @adapter: board private structure
1138 * Configure the Rx unit of the MAC after a reset.
1140 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1143 struct ixgbe_hw *hw = &adapter->hw;
1147 ixgbevf_setup_psrtype(adapter);
1149 /* set_rx_buffer_len must be called before ring initialization */
1150 ixgbevf_set_rx_buffer_len(adapter);
1152 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1153 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1154 * the Base and Length of the Rx Descriptor Ring */
1155 for (i = 0; i < adapter->num_rx_queues; i++) {
1156 rdba = adapter->rx_ring[i].dma;
1157 j = adapter->rx_ring[i].reg_idx;
1158 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1159 (rdba & DMA_BIT_MASK(32)));
1160 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1161 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1162 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1163 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1164 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1165 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1167 ixgbevf_configure_srrctl(adapter, j);
1171 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
1172 __be16 proto, u16 vid)
1174 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1175 struct ixgbe_hw *hw = &adapter->hw;
1178 spin_lock_bh(&adapter->mbx_lock);
1180 /* add VID to filter table */
1181 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
1183 spin_unlock_bh(&adapter->mbx_lock);
1185 /* translate error return types so error makes sense */
1186 if (err == IXGBE_ERR_MBX)
1189 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1192 set_bit(vid, adapter->active_vlans);
1197 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
1198 __be16 proto, u16 vid)
1200 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1201 struct ixgbe_hw *hw = &adapter->hw;
1202 int err = -EOPNOTSUPP;
1204 spin_lock_bh(&adapter->mbx_lock);
1206 /* remove VID from filter table */
1207 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
1209 spin_unlock_bh(&adapter->mbx_lock);
1211 clear_bit(vid, adapter->active_vlans);
1216 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1220 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1221 ixgbevf_vlan_rx_add_vid(adapter->netdev,
1222 htons(ETH_P_8021Q), vid);
1225 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1227 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1228 struct ixgbe_hw *hw = &adapter->hw;
1231 if ((netdev_uc_count(netdev)) > 10) {
1232 pr_err("Too many unicast filters - No Space\n");
1236 if (!netdev_uc_empty(netdev)) {
1237 struct netdev_hw_addr *ha;
1238 netdev_for_each_uc_addr(ha, netdev) {
1239 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1244 * If the list is empty then send message to PF driver to
1245 * clear all macvlans on this VF.
1247 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1254 * ixgbevf_set_rx_mode - Multicast and unicast set
1255 * @netdev: network interface device structure
1257 * The set_rx_method entry point is called whenever the multicast address
1258 * list, unicast address list or the network interface flags are updated.
1259 * This routine is responsible for configuring the hardware for proper
1260 * multicast mode and configuring requested unicast filters.
1262 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1264 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1265 struct ixgbe_hw *hw = &adapter->hw;
1267 spin_lock_bh(&adapter->mbx_lock);
1269 /* reprogram multicast list */
1270 hw->mac.ops.update_mc_addr_list(hw, netdev);
1272 ixgbevf_write_uc_addr_list(netdev);
1274 spin_unlock_bh(&adapter->mbx_lock);
1277 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1280 struct ixgbevf_q_vector *q_vector;
1281 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1283 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1284 q_vector = adapter->q_vector[q_idx];
1285 napi_enable(&q_vector->napi);
1289 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1292 struct ixgbevf_q_vector *q_vector;
1293 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1295 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1296 q_vector = adapter->q_vector[q_idx];
1297 napi_disable(&q_vector->napi);
1301 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1303 struct net_device *netdev = adapter->netdev;
1306 ixgbevf_set_rx_mode(netdev);
1308 ixgbevf_restore_vlan(adapter);
1310 ixgbevf_configure_tx(adapter);
1311 ixgbevf_configure_rx(adapter);
1312 for (i = 0; i < adapter->num_rx_queues; i++) {
1313 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1314 ixgbevf_alloc_rx_buffers(adapter, ring,
1315 IXGBE_DESC_UNUSED(ring));
1319 #define IXGBEVF_MAX_RX_DESC_POLL 10
1320 static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1323 struct ixgbe_hw *hw = &adapter->hw;
1324 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1326 int j = adapter->rx_ring[rxr].reg_idx;
1329 usleep_range(1000, 2000);
1330 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1331 } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1334 hw_dbg(hw, "RXDCTL.ENABLE queue %d not set while polling\n",
1337 ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
1338 (adapter->rx_ring[rxr].count - 1));
1341 static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1342 struct ixgbevf_ring *ring)
1344 struct ixgbe_hw *hw = &adapter->hw;
1345 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1347 u8 reg_idx = ring->reg_idx;
1349 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1350 rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1352 /* write value back with RXDCTL.ENABLE bit cleared */
1353 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1355 /* the hardware may take up to 100us to really disable the rx queue */
1358 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1359 } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1362 hw_dbg(hw, "RXDCTL.ENABLE queue %d not cleared while polling\n",
1366 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1368 /* Only save pre-reset stats if there are some */
1369 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1370 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1371 adapter->stats.base_vfgprc;
1372 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1373 adapter->stats.base_vfgptc;
1374 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1375 adapter->stats.base_vfgorc;
1376 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1377 adapter->stats.base_vfgotc;
1378 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1379 adapter->stats.base_vfmprc;
1383 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1385 struct ixgbe_hw *hw = &adapter->hw;
1387 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1388 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1389 adapter->stats.last_vfgorc |=
1390 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1391 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1392 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1393 adapter->stats.last_vfgotc |=
1394 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1395 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1397 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1398 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1399 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1400 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1401 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1404 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1406 struct ixgbe_hw *hw = &adapter->hw;
1407 int api[] = { ixgbe_mbox_api_11,
1409 ixgbe_mbox_api_unknown };
1410 int err = 0, idx = 0;
1412 spin_lock_bh(&adapter->mbx_lock);
1414 while (api[idx] != ixgbe_mbox_api_unknown) {
1415 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1421 spin_unlock_bh(&adapter->mbx_lock);
1424 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1426 struct net_device *netdev = adapter->netdev;
1427 struct ixgbe_hw *hw = &adapter->hw;
1429 int num_rx_rings = adapter->num_rx_queues;
1432 for (i = 0; i < adapter->num_tx_queues; i++) {
1433 j = adapter->tx_ring[i].reg_idx;
1434 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1435 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1436 txdctl |= (8 << 16);
1437 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1440 for (i = 0; i < adapter->num_tx_queues; i++) {
1441 j = adapter->tx_ring[i].reg_idx;
1442 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1443 txdctl |= IXGBE_TXDCTL_ENABLE;
1444 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1447 for (i = 0; i < num_rx_rings; i++) {
1448 j = adapter->rx_ring[i].reg_idx;
1449 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1450 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1451 if (hw->mac.type == ixgbe_mac_X540_vf) {
1452 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1453 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1454 IXGBE_RXDCTL_RLPML_EN);
1456 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1457 ixgbevf_rx_desc_queue_enable(adapter, i);
1460 ixgbevf_configure_msix(adapter);
1462 spin_lock_bh(&adapter->mbx_lock);
1464 if (is_valid_ether_addr(hw->mac.addr))
1465 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1467 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1469 spin_unlock_bh(&adapter->mbx_lock);
1471 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1472 ixgbevf_napi_enable_all(adapter);
1474 /* enable transmits */
1475 netif_tx_start_all_queues(netdev);
1477 ixgbevf_save_reset_stats(adapter);
1478 ixgbevf_init_last_counter_stats(adapter);
1480 hw->mac.get_link_status = 1;
1481 mod_timer(&adapter->watchdog_timer, jiffies);
1484 static int ixgbevf_reset_queues(struct ixgbevf_adapter *adapter)
1486 struct ixgbe_hw *hw = &adapter->hw;
1487 struct ixgbevf_ring *rx_ring;
1488 unsigned int def_q = 0;
1489 unsigned int num_tcs = 0;
1490 unsigned int num_rx_queues = 1;
1493 spin_lock_bh(&adapter->mbx_lock);
1495 /* fetch queue configuration from the PF */
1496 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1498 spin_unlock_bh(&adapter->mbx_lock);
1504 /* update default Tx ring register index */
1505 adapter->tx_ring[0].reg_idx = def_q;
1507 /* we need as many queues as traffic classes */
1508 num_rx_queues = num_tcs;
1511 /* nothing to do if we have the correct number of queues */
1512 if (adapter->num_rx_queues == num_rx_queues)
1515 /* allocate new rings */
1516 rx_ring = kcalloc(num_rx_queues,
1517 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1521 /* setup ring fields */
1522 for (i = 0; i < num_rx_queues; i++) {
1523 rx_ring[i].count = adapter->rx_ring_count;
1524 rx_ring[i].queue_index = i;
1525 rx_ring[i].reg_idx = i;
1526 rx_ring[i].dev = &adapter->pdev->dev;
1527 rx_ring[i].netdev = adapter->netdev;
1529 /* allocate resources on the ring */
1530 err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
1534 ixgbevf_free_rx_resources(adapter, &rx_ring[i]);
1541 /* free the existing rings and queues */
1542 ixgbevf_free_all_rx_resources(adapter);
1543 adapter->num_rx_queues = 0;
1544 kfree(adapter->rx_ring);
1546 /* move new rings into position on the adapter struct */
1547 adapter->rx_ring = rx_ring;
1548 adapter->num_rx_queues = num_rx_queues;
1550 /* reset ring to vector mapping */
1551 ixgbevf_reset_q_vectors(adapter);
1552 ixgbevf_map_rings_to_vectors(adapter);
1557 void ixgbevf_up(struct ixgbevf_adapter *adapter)
1559 struct ixgbe_hw *hw = &adapter->hw;
1561 ixgbevf_reset_queues(adapter);
1563 ixgbevf_configure(adapter);
1565 ixgbevf_up_complete(adapter);
1567 /* clear any pending interrupts, may auto mask */
1568 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1570 ixgbevf_irq_enable(adapter);
1574 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1575 * @adapter: board private structure
1576 * @rx_ring: ring to free buffers from
1578 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1579 struct ixgbevf_ring *rx_ring)
1581 struct pci_dev *pdev = adapter->pdev;
1585 if (!rx_ring->rx_buffer_info)
1588 /* Free all the Rx ring sk_buffs */
1589 for (i = 0; i < rx_ring->count; i++) {
1590 struct ixgbevf_rx_buffer *rx_buffer_info;
1592 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1593 if (rx_buffer_info->dma) {
1594 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1595 rx_ring->rx_buf_len,
1597 rx_buffer_info->dma = 0;
1599 if (rx_buffer_info->skb) {
1600 struct sk_buff *skb = rx_buffer_info->skb;
1601 rx_buffer_info->skb = NULL;
1603 struct sk_buff *this = skb;
1604 skb = IXGBE_CB(skb)->prev;
1605 dev_kfree_skb(this);
1610 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1611 memset(rx_ring->rx_buffer_info, 0, size);
1613 /* Zero out the descriptor ring */
1614 memset(rx_ring->desc, 0, rx_ring->size);
1616 rx_ring->next_to_clean = 0;
1617 rx_ring->next_to_use = 0;
1620 writel(0, adapter->hw.hw_addr + rx_ring->head);
1622 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1626 * ixgbevf_clean_tx_ring - Free Tx Buffers
1627 * @adapter: board private structure
1628 * @tx_ring: ring to be cleaned
1630 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1631 struct ixgbevf_ring *tx_ring)
1633 struct ixgbevf_tx_buffer *tx_buffer_info;
1637 if (!tx_ring->tx_buffer_info)
1640 /* Free all the Tx ring sk_buffs */
1641 for (i = 0; i < tx_ring->count; i++) {
1642 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1643 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
1646 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1647 memset(tx_ring->tx_buffer_info, 0, size);
1649 memset(tx_ring->desc, 0, tx_ring->size);
1651 tx_ring->next_to_use = 0;
1652 tx_ring->next_to_clean = 0;
1655 writel(0, adapter->hw.hw_addr + tx_ring->head);
1657 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1661 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1662 * @adapter: board private structure
1664 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1668 for (i = 0; i < adapter->num_rx_queues; i++)
1669 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1673 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1674 * @adapter: board private structure
1676 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1680 for (i = 0; i < adapter->num_tx_queues; i++)
1681 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1684 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1686 struct net_device *netdev = adapter->netdev;
1687 struct ixgbe_hw *hw = &adapter->hw;
1691 /* signal that we are down to the interrupt handler */
1692 set_bit(__IXGBEVF_DOWN, &adapter->state);
1694 /* disable all enabled rx queues */
1695 for (i = 0; i < adapter->num_rx_queues; i++)
1696 ixgbevf_disable_rx_queue(adapter, &adapter->rx_ring[i]);
1698 netif_tx_disable(netdev);
1702 netif_tx_stop_all_queues(netdev);
1704 ixgbevf_irq_disable(adapter);
1706 ixgbevf_napi_disable_all(adapter);
1708 del_timer_sync(&adapter->watchdog_timer);
1709 /* can't call flush scheduled work here because it can deadlock
1710 * if linkwatch_event tries to acquire the rtnl_lock which we are
1712 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1715 /* disable transmits in the hardware now that interrupts are off */
1716 for (i = 0; i < adapter->num_tx_queues; i++) {
1717 j = adapter->tx_ring[i].reg_idx;
1718 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1719 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1720 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1723 netif_carrier_off(netdev);
1725 if (!pci_channel_offline(adapter->pdev))
1726 ixgbevf_reset(adapter);
1728 ixgbevf_clean_all_tx_rings(adapter);
1729 ixgbevf_clean_all_rx_rings(adapter);
1732 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1734 WARN_ON(in_interrupt());
1736 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1739 ixgbevf_down(adapter);
1740 ixgbevf_up(adapter);
1742 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1745 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1747 struct ixgbe_hw *hw = &adapter->hw;
1748 struct net_device *netdev = adapter->netdev;
1750 if (hw->mac.ops.reset_hw(hw)) {
1751 hw_dbg(hw, "PF still resetting\n");
1753 hw->mac.ops.init_hw(hw);
1754 ixgbevf_negotiate_api(adapter);
1757 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1758 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1760 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1765 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1769 int vector_threshold;
1771 /* We'll want at least 2 (vector_threshold):
1772 * 1) TxQ[0] + RxQ[0] handler
1773 * 2) Other (Link Status Change, etc.)
1775 vector_threshold = MIN_MSIX_COUNT;
1777 /* The more we get, the more we will assign to Tx/Rx Cleanup
1778 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1779 * Right now, we simply care about how many we'll get; we'll
1780 * set them up later while requesting irq's.
1782 while (vectors >= vector_threshold) {
1783 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1785 if (!err || err < 0) /* Success or a nasty failure. */
1787 else /* err == number of vectors we should try again with */
1791 if (vectors < vector_threshold)
1795 dev_err(&adapter->pdev->dev,
1796 "Unable to allocate MSI-X interrupts\n");
1797 kfree(adapter->msix_entries);
1798 adapter->msix_entries = NULL;
1801 * Adjust for only the vectors we'll use, which is minimum
1802 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1803 * vectors we were allocated.
1805 adapter->num_msix_vectors = vectors;
1812 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
1813 * @adapter: board private structure to initialize
1815 * This is the top level queue allocation routine. The order here is very
1816 * important, starting with the "most" number of features turned on at once,
1817 * and ending with the smallest set of features. This way large combinations
1818 * can be allocated if they're turned on, and smaller combinations are the
1819 * fallthrough conditions.
1822 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1824 /* Start with base case */
1825 adapter->num_rx_queues = 1;
1826 adapter->num_tx_queues = 1;
1830 * ixgbevf_alloc_queues - Allocate memory for all rings
1831 * @adapter: board private structure to initialize
1833 * We allocate one ring per queue at run-time since we don't know the
1834 * number of queues at compile-time. The polling_netdev array is
1835 * intended for Multiqueue, but should work fine with a single queue.
1837 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1841 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1842 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1843 if (!adapter->tx_ring)
1844 goto err_tx_ring_allocation;
1846 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1847 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1848 if (!adapter->rx_ring)
1849 goto err_rx_ring_allocation;
1851 for (i = 0; i < adapter->num_tx_queues; i++) {
1852 adapter->tx_ring[i].count = adapter->tx_ring_count;
1853 adapter->tx_ring[i].queue_index = i;
1854 /* reg_idx may be remapped later by DCB config */
1855 adapter->tx_ring[i].reg_idx = i;
1856 adapter->tx_ring[i].dev = &adapter->pdev->dev;
1857 adapter->tx_ring[i].netdev = adapter->netdev;
1860 for (i = 0; i < adapter->num_rx_queues; i++) {
1861 adapter->rx_ring[i].count = adapter->rx_ring_count;
1862 adapter->rx_ring[i].queue_index = i;
1863 adapter->rx_ring[i].reg_idx = i;
1864 adapter->rx_ring[i].dev = &adapter->pdev->dev;
1865 adapter->rx_ring[i].netdev = adapter->netdev;
1870 err_rx_ring_allocation:
1871 kfree(adapter->tx_ring);
1872 err_tx_ring_allocation:
1877 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1878 * @adapter: board private structure to initialize
1880 * Attempt to configure the interrupts using the best available
1881 * capabilities of the hardware and the kernel.
1883 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1885 struct net_device *netdev = adapter->netdev;
1887 int vector, v_budget;
1890 * It's easy to be greedy for MSI-X vectors, but it really
1891 * doesn't do us much good if we have a lot more vectors
1892 * than CPU's. So let's be conservative and only ask for
1893 * (roughly) the same number of vectors as there are CPU's.
1894 * The default is to use pairs of vectors.
1896 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
1897 v_budget = min_t(int, v_budget, num_online_cpus());
1898 v_budget += NON_Q_VECTORS;
1900 /* A failure in MSI-X entry allocation isn't fatal, but it does
1901 * mean we disable MSI-X capabilities of the adapter. */
1902 adapter->msix_entries = kcalloc(v_budget,
1903 sizeof(struct msix_entry), GFP_KERNEL);
1904 if (!adapter->msix_entries) {
1909 for (vector = 0; vector < v_budget; vector++)
1910 adapter->msix_entries[vector].entry = vector;
1912 err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
1916 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
1920 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
1927 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1928 * @adapter: board private structure to initialize
1930 * We allocate one q_vector per queue interrupt. If allocation fails we
1933 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1935 int q_idx, num_q_vectors;
1936 struct ixgbevf_q_vector *q_vector;
1938 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1940 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1941 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1944 q_vector->adapter = adapter;
1945 q_vector->v_idx = q_idx;
1946 netif_napi_add(adapter->netdev, &q_vector->napi,
1948 adapter->q_vector[q_idx] = q_vector;
1956 q_vector = adapter->q_vector[q_idx];
1957 netif_napi_del(&q_vector->napi);
1959 adapter->q_vector[q_idx] = NULL;
1965 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1966 * @adapter: board private structure to initialize
1968 * This function frees the memory allocated to the q_vectors. In addition if
1969 * NAPI is enabled it will delete any references to the NAPI struct prior
1970 * to freeing the q_vector.
1972 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1974 int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1976 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1977 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1979 adapter->q_vector[q_idx] = NULL;
1980 netif_napi_del(&q_vector->napi);
1986 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1987 * @adapter: board private structure
1990 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1992 pci_disable_msix(adapter->pdev);
1993 kfree(adapter->msix_entries);
1994 adapter->msix_entries = NULL;
1998 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1999 * @adapter: board private structure to initialize
2002 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2006 /* Number of supported queues */
2007 ixgbevf_set_num_queues(adapter);
2009 err = ixgbevf_set_interrupt_capability(adapter);
2011 hw_dbg(&adapter->hw,
2012 "Unable to setup interrupt capabilities\n");
2013 goto err_set_interrupt;
2016 err = ixgbevf_alloc_q_vectors(adapter);
2018 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
2020 goto err_alloc_q_vectors;
2023 err = ixgbevf_alloc_queues(adapter);
2025 pr_err("Unable to allocate memory for queues\n");
2026 goto err_alloc_queues;
2029 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
2030 "Tx Queue count = %u\n",
2031 (adapter->num_rx_queues > 1) ? "Enabled" :
2032 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2034 set_bit(__IXGBEVF_DOWN, &adapter->state);
2038 ixgbevf_free_q_vectors(adapter);
2039 err_alloc_q_vectors:
2040 ixgbevf_reset_interrupt_capability(adapter);
2046 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2047 * @adapter: board private structure to clear interrupt scheme on
2049 * We go through and clear interrupt specific resources and reset the structure
2050 * to pre-load conditions
2052 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2054 adapter->num_tx_queues = 0;
2055 adapter->num_rx_queues = 0;
2057 ixgbevf_free_q_vectors(adapter);
2058 ixgbevf_reset_interrupt_capability(adapter);
2062 * ixgbevf_sw_init - Initialize general software structures
2063 * (struct ixgbevf_adapter)
2064 * @adapter: board private structure to initialize
2066 * ixgbevf_sw_init initializes the Adapter private data structure.
2067 * Fields are initialized based on PCI device information and
2068 * OS network device settings (MTU size).
2070 static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2072 struct ixgbe_hw *hw = &adapter->hw;
2073 struct pci_dev *pdev = adapter->pdev;
2074 struct net_device *netdev = adapter->netdev;
2077 /* PCI config space info */
2079 hw->vendor_id = pdev->vendor;
2080 hw->device_id = pdev->device;
2081 hw->revision_id = pdev->revision;
2082 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2083 hw->subsystem_device_id = pdev->subsystem_device;
2085 hw->mbx.ops.init_params(hw);
2087 /* assume legacy case in which PF would only give VF 2 queues */
2088 hw->mac.max_tx_queues = 2;
2089 hw->mac.max_rx_queues = 2;
2091 /* lock to protect mailbox accesses */
2092 spin_lock_init(&adapter->mbx_lock);
2094 err = hw->mac.ops.reset_hw(hw);
2096 dev_info(&pdev->dev,
2097 "PF still in reset state. Is the PF interface up?\n");
2099 err = hw->mac.ops.init_hw(hw);
2101 pr_err("init_shared_code failed: %d\n", err);
2104 ixgbevf_negotiate_api(adapter);
2105 err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
2107 dev_info(&pdev->dev, "Error reading MAC address\n");
2108 else if (is_zero_ether_addr(adapter->hw.mac.addr))
2109 dev_info(&pdev->dev,
2110 "MAC address not assigned by administrator.\n");
2111 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2114 if (!is_valid_ether_addr(netdev->dev_addr)) {
2115 dev_info(&pdev->dev, "Assigning random MAC address\n");
2116 eth_hw_addr_random(netdev);
2117 memcpy(hw->mac.addr, netdev->dev_addr, netdev->addr_len);
2120 /* Enable dynamic interrupt throttling rates */
2121 adapter->rx_itr_setting = 1;
2122 adapter->tx_itr_setting = 1;
2124 /* set default ring sizes */
2125 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2126 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2128 set_bit(__IXGBEVF_DOWN, &adapter->state);
2135 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2137 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2138 if (current_counter < last_counter) \
2139 counter += 0x100000000LL; \
2140 last_counter = current_counter; \
2141 counter &= 0xFFFFFFFF00000000LL; \
2142 counter |= current_counter; \
2145 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2147 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2148 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2149 u64 current_counter = (current_counter_msb << 32) | \
2150 current_counter_lsb; \
2151 if (current_counter < last_counter) \
2152 counter += 0x1000000000LL; \
2153 last_counter = current_counter; \
2154 counter &= 0xFFFFFFF000000000LL; \
2155 counter |= current_counter; \
2158 * ixgbevf_update_stats - Update the board statistics counters.
2159 * @adapter: board private structure
2161 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2163 struct ixgbe_hw *hw = &adapter->hw;
2166 if (!adapter->link_up)
2169 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2170 adapter->stats.vfgprc);
2171 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2172 adapter->stats.vfgptc);
2173 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2174 adapter->stats.last_vfgorc,
2175 adapter->stats.vfgorc);
2176 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2177 adapter->stats.last_vfgotc,
2178 adapter->stats.vfgotc);
2179 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2180 adapter->stats.vfmprc);
2182 for (i = 0; i < adapter->num_rx_queues; i++) {
2183 adapter->hw_csum_rx_error +=
2184 adapter->rx_ring[i].hw_csum_rx_error;
2185 adapter->hw_csum_rx_good +=
2186 adapter->rx_ring[i].hw_csum_rx_good;
2187 adapter->rx_ring[i].hw_csum_rx_error = 0;
2188 adapter->rx_ring[i].hw_csum_rx_good = 0;
2193 * ixgbevf_watchdog - Timer Call-back
2194 * @data: pointer to adapter cast into an unsigned long
2196 static void ixgbevf_watchdog(unsigned long data)
2198 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2199 struct ixgbe_hw *hw = &adapter->hw;
2204 * Do the watchdog outside of interrupt context due to the lovely
2205 * delays that some of the newer hardware requires
2208 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2209 goto watchdog_short_circuit;
2211 /* get one bit for every active tx/rx interrupt vector */
2212 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2213 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2214 if (qv->rx.ring || qv->tx.ring)
2218 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
2220 watchdog_short_circuit:
2221 schedule_work(&adapter->watchdog_task);
2225 * ixgbevf_tx_timeout - Respond to a Tx Hang
2226 * @netdev: network interface device structure
2228 static void ixgbevf_tx_timeout(struct net_device *netdev)
2230 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2232 /* Do the reset outside of interrupt context */
2233 schedule_work(&adapter->reset_task);
2236 static void ixgbevf_reset_task(struct work_struct *work)
2238 struct ixgbevf_adapter *adapter;
2239 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2241 /* If we're already down or resetting, just bail */
2242 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2243 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2246 adapter->tx_timeout_count++;
2248 ixgbevf_reinit_locked(adapter);
2252 * ixgbevf_watchdog_task - worker thread to bring link up
2253 * @work: pointer to work_struct containing our data
2255 static void ixgbevf_watchdog_task(struct work_struct *work)
2257 struct ixgbevf_adapter *adapter = container_of(work,
2258 struct ixgbevf_adapter,
2260 struct net_device *netdev = adapter->netdev;
2261 struct ixgbe_hw *hw = &adapter->hw;
2262 u32 link_speed = adapter->link_speed;
2263 bool link_up = adapter->link_up;
2266 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2269 * Always check the link on the watchdog because we have
2272 spin_lock_bh(&adapter->mbx_lock);
2274 need_reset = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
2276 spin_unlock_bh(&adapter->mbx_lock);
2279 adapter->link_up = link_up;
2280 adapter->link_speed = link_speed;
2281 netif_carrier_off(netdev);
2282 netif_tx_stop_all_queues(netdev);
2283 schedule_work(&adapter->reset_task);
2286 adapter->link_up = link_up;
2287 adapter->link_speed = link_speed;
2290 if (!netif_carrier_ok(netdev)) {
2291 char *link_speed_string;
2292 switch (link_speed) {
2293 case IXGBE_LINK_SPEED_10GB_FULL:
2294 link_speed_string = "10 Gbps";
2296 case IXGBE_LINK_SPEED_1GB_FULL:
2297 link_speed_string = "1 Gbps";
2299 case IXGBE_LINK_SPEED_100_FULL:
2300 link_speed_string = "100 Mbps";
2303 link_speed_string = "unknown speed";
2306 dev_info(&adapter->pdev->dev,
2307 "NIC Link is Up, %s\n", link_speed_string);
2308 netif_carrier_on(netdev);
2309 netif_tx_wake_all_queues(netdev);
2312 adapter->link_up = false;
2313 adapter->link_speed = 0;
2314 if (netif_carrier_ok(netdev)) {
2315 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
2316 netif_carrier_off(netdev);
2317 netif_tx_stop_all_queues(netdev);
2321 ixgbevf_update_stats(adapter);
2324 /* Reset the timer */
2325 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2326 mod_timer(&adapter->watchdog_timer,
2327 round_jiffies(jiffies + (2 * HZ)));
2329 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2333 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2334 * @adapter: board private structure
2335 * @tx_ring: Tx descriptor ring for a specific queue
2337 * Free all transmit software resources
2339 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2340 struct ixgbevf_ring *tx_ring)
2342 struct pci_dev *pdev = adapter->pdev;
2344 ixgbevf_clean_tx_ring(adapter, tx_ring);
2346 vfree(tx_ring->tx_buffer_info);
2347 tx_ring->tx_buffer_info = NULL;
2349 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2352 tx_ring->desc = NULL;
2356 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2357 * @adapter: board private structure
2359 * Free all transmit software resources
2361 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2365 for (i = 0; i < adapter->num_tx_queues; i++)
2366 if (adapter->tx_ring[i].desc)
2367 ixgbevf_free_tx_resources(adapter,
2368 &adapter->tx_ring[i]);
2373 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2374 * @adapter: board private structure
2375 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2377 * Return 0 on success, negative on failure
2379 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2380 struct ixgbevf_ring *tx_ring)
2382 struct pci_dev *pdev = adapter->pdev;
2385 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2386 tx_ring->tx_buffer_info = vzalloc(size);
2387 if (!tx_ring->tx_buffer_info)
2390 /* round up to nearest 4K */
2391 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2392 tx_ring->size = ALIGN(tx_ring->size, 4096);
2394 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2395 &tx_ring->dma, GFP_KERNEL);
2399 tx_ring->next_to_use = 0;
2400 tx_ring->next_to_clean = 0;
2404 vfree(tx_ring->tx_buffer_info);
2405 tx_ring->tx_buffer_info = NULL;
2406 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2407 "descriptor ring\n");
2412 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2413 * @adapter: board private structure
2415 * If this function returns with an error, then it's possible one or
2416 * more of the rings is populated (while the rest are not). It is the
2417 * callers duty to clean those orphaned rings.
2419 * Return 0 on success, negative on failure
2421 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2425 for (i = 0; i < adapter->num_tx_queues; i++) {
2426 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2429 hw_dbg(&adapter->hw,
2430 "Allocation for Tx Queue %u failed\n", i);
2438 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2439 * @adapter: board private structure
2440 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2442 * Returns 0 on success, negative on failure
2444 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2445 struct ixgbevf_ring *rx_ring)
2447 struct pci_dev *pdev = adapter->pdev;
2450 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2451 rx_ring->rx_buffer_info = vzalloc(size);
2452 if (!rx_ring->rx_buffer_info)
2455 /* Round up to nearest 4K */
2456 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2457 rx_ring->size = ALIGN(rx_ring->size, 4096);
2459 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2460 &rx_ring->dma, GFP_KERNEL);
2462 if (!rx_ring->desc) {
2463 vfree(rx_ring->rx_buffer_info);
2464 rx_ring->rx_buffer_info = NULL;
2468 rx_ring->next_to_clean = 0;
2469 rx_ring->next_to_use = 0;
2477 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2478 * @adapter: board private structure
2480 * If this function returns with an error, then it's possible one or
2481 * more of the rings is populated (while the rest are not). It is the
2482 * callers duty to clean those orphaned rings.
2484 * Return 0 on success, negative on failure
2486 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2490 for (i = 0; i < adapter->num_rx_queues; i++) {
2491 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2494 hw_dbg(&adapter->hw,
2495 "Allocation for Rx Queue %u failed\n", i);
2502 * ixgbevf_free_rx_resources - Free Rx Resources
2503 * @adapter: board private structure
2504 * @rx_ring: ring to clean the resources from
2506 * Free all receive software resources
2508 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2509 struct ixgbevf_ring *rx_ring)
2511 struct pci_dev *pdev = adapter->pdev;
2513 ixgbevf_clean_rx_ring(adapter, rx_ring);
2515 vfree(rx_ring->rx_buffer_info);
2516 rx_ring->rx_buffer_info = NULL;
2518 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2521 rx_ring->desc = NULL;
2525 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2526 * @adapter: board private structure
2528 * Free all receive software resources
2530 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2534 for (i = 0; i < adapter->num_rx_queues; i++)
2535 if (adapter->rx_ring[i].desc)
2536 ixgbevf_free_rx_resources(adapter,
2537 &adapter->rx_ring[i]);
2540 static int ixgbevf_setup_queues(struct ixgbevf_adapter *adapter)
2542 struct ixgbe_hw *hw = &adapter->hw;
2543 struct ixgbevf_ring *rx_ring;
2544 unsigned int def_q = 0;
2545 unsigned int num_tcs = 0;
2546 unsigned int num_rx_queues = 1;
2549 spin_lock_bh(&adapter->mbx_lock);
2551 /* fetch queue configuration from the PF */
2552 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2554 spin_unlock_bh(&adapter->mbx_lock);
2560 /* update default Tx ring register index */
2561 adapter->tx_ring[0].reg_idx = def_q;
2563 /* we need as many queues as traffic classes */
2564 num_rx_queues = num_tcs;
2567 /* nothing to do if we have the correct number of queues */
2568 if (adapter->num_rx_queues == num_rx_queues)
2571 /* allocate new rings */
2572 rx_ring = kcalloc(num_rx_queues,
2573 sizeof(struct ixgbevf_ring), GFP_KERNEL);
2577 /* setup ring fields */
2578 for (i = 0; i < num_rx_queues; i++) {
2579 rx_ring[i].count = adapter->rx_ring_count;
2580 rx_ring[i].queue_index = i;
2581 rx_ring[i].reg_idx = i;
2582 rx_ring[i].dev = &adapter->pdev->dev;
2583 rx_ring[i].netdev = adapter->netdev;
2586 /* free the existing ring and queues */
2587 adapter->num_rx_queues = 0;
2588 kfree(adapter->rx_ring);
2590 /* move new rings into position on the adapter struct */
2591 adapter->rx_ring = rx_ring;
2592 adapter->num_rx_queues = num_rx_queues;
2598 * ixgbevf_open - Called when a network interface is made active
2599 * @netdev: network interface device structure
2601 * Returns 0 on success, negative value on failure
2603 * The open entry point is called when a network interface is made
2604 * active by the system (IFF_UP). At this point all resources needed
2605 * for transmit and receive operations are allocated, the interrupt
2606 * handler is registered with the OS, the watchdog timer is started,
2607 * and the stack is notified that the interface is ready.
2609 static int ixgbevf_open(struct net_device *netdev)
2611 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2612 struct ixgbe_hw *hw = &adapter->hw;
2615 /* A previous failure to open the device because of a lack of
2616 * available MSIX vector resources may have reset the number
2617 * of msix vectors variable to zero. The only way to recover
2618 * is to unload/reload the driver and hope that the system has
2619 * been able to recover some MSIX vector resources.
2621 if (!adapter->num_msix_vectors)
2624 /* disallow open during test */
2625 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2628 if (hw->adapter_stopped) {
2629 ixgbevf_reset(adapter);
2630 /* if adapter is still stopped then PF isn't up and
2631 * the vf can't start. */
2632 if (hw->adapter_stopped) {
2633 err = IXGBE_ERR_MBX;
2634 pr_err("Unable to start - perhaps the PF Driver isn't "
2636 goto err_setup_reset;
2640 /* setup queue reg_idx and Rx queue count */
2641 err = ixgbevf_setup_queues(adapter);
2643 goto err_setup_queues;
2645 /* allocate transmit descriptors */
2646 err = ixgbevf_setup_all_tx_resources(adapter);
2650 /* allocate receive descriptors */
2651 err = ixgbevf_setup_all_rx_resources(adapter);
2655 ixgbevf_configure(adapter);
2658 * Map the Tx/Rx rings to the vectors we were allotted.
2659 * if request_irq will be called in this function map_rings
2660 * must be called *before* up_complete
2662 ixgbevf_map_rings_to_vectors(adapter);
2664 ixgbevf_up_complete(adapter);
2666 /* clear any pending interrupts, may auto mask */
2667 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2668 err = ixgbevf_request_irq(adapter);
2672 ixgbevf_irq_enable(adapter);
2677 ixgbevf_down(adapter);
2679 ixgbevf_free_all_rx_resources(adapter);
2681 ixgbevf_free_all_tx_resources(adapter);
2683 ixgbevf_reset(adapter);
2691 * ixgbevf_close - Disables a network interface
2692 * @netdev: network interface device structure
2694 * Returns 0, this is not allowed to fail
2696 * The close entry point is called when an interface is de-activated
2697 * by the OS. The hardware is still under the drivers control, but
2698 * needs to be disabled. A global MAC reset is issued to stop the
2699 * hardware, and all transmit and receive resources are freed.
2701 static int ixgbevf_close(struct net_device *netdev)
2703 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2705 ixgbevf_down(adapter);
2706 ixgbevf_free_irq(adapter);
2708 ixgbevf_free_all_tx_resources(adapter);
2709 ixgbevf_free_all_rx_resources(adapter);
2714 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2715 u32 vlan_macip_lens, u32 type_tucmd,
2718 struct ixgbe_adv_tx_context_desc *context_desc;
2719 u16 i = tx_ring->next_to_use;
2721 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2724 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2726 /* set bits to identify this as an advanced context descriptor */
2727 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2729 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2730 context_desc->seqnum_seed = 0;
2731 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2732 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2735 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
2736 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2738 u32 vlan_macip_lens, type_tucmd;
2739 u32 mss_l4len_idx, l4len;
2741 if (!skb_is_gso(skb))
2744 if (skb_header_cloned(skb)) {
2745 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2750 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2751 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2753 if (skb->protocol == htons(ETH_P_IP)) {
2754 struct iphdr *iph = ip_hdr(skb);
2757 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2761 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2762 } else if (skb_is_gso_v6(skb)) {
2763 ipv6_hdr(skb)->payload_len = 0;
2764 tcp_hdr(skb)->check =
2765 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2766 &ipv6_hdr(skb)->daddr,
2770 /* compute header lengths */
2771 l4len = tcp_hdrlen(skb);
2773 *hdr_len = skb_transport_offset(skb) + l4len;
2775 /* mss_l4len_id: use 1 as index for TSO */
2776 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2777 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2778 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2780 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2781 vlan_macip_lens = skb_network_header_len(skb);
2782 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2783 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2785 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2786 type_tucmd, mss_l4len_idx);
2791 static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2792 struct sk_buff *skb, u32 tx_flags)
2794 u32 vlan_macip_lens = 0;
2795 u32 mss_l4len_idx = 0;
2798 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2800 switch (skb->protocol) {
2801 case __constant_htons(ETH_P_IP):
2802 vlan_macip_lens |= skb_network_header_len(skb);
2803 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2804 l4_hdr = ip_hdr(skb)->protocol;
2806 case __constant_htons(ETH_P_IPV6):
2807 vlan_macip_lens |= skb_network_header_len(skb);
2808 l4_hdr = ipv6_hdr(skb)->nexthdr;
2811 if (unlikely(net_ratelimit())) {
2812 dev_warn(tx_ring->dev,
2813 "partial checksum but proto=%x!\n",
2821 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2822 mss_l4len_idx = tcp_hdrlen(skb) <<
2823 IXGBE_ADVTXD_L4LEN_SHIFT;
2826 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2827 mss_l4len_idx = sizeof(struct sctphdr) <<
2828 IXGBE_ADVTXD_L4LEN_SHIFT;
2831 mss_l4len_idx = sizeof(struct udphdr) <<
2832 IXGBE_ADVTXD_L4LEN_SHIFT;
2835 if (unlikely(net_ratelimit())) {
2836 dev_warn(tx_ring->dev,
2837 "partial checksum but l4 proto=%x!\n",
2844 /* vlan_macip_lens: MACLEN, VLAN tag */
2845 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2846 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2848 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2849 type_tucmd, mss_l4len_idx);
2851 return (skb->ip_summed == CHECKSUM_PARTIAL);
2854 static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
2855 struct sk_buff *skb, u32 tx_flags)
2857 struct ixgbevf_tx_buffer *tx_buffer_info;
2859 unsigned int total = skb->len;
2860 unsigned int offset = 0, size;
2862 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2866 i = tx_ring->next_to_use;
2868 len = min(skb_headlen(skb), 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->mapped_as_page = false;
2875 tx_buffer_info->dma = dma_map_single(tx_ring->dev,
2877 size, DMA_TO_DEVICE);
2878 if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
2886 if (i == tx_ring->count)
2890 for (f = 0; f < nr_frags; f++) {
2891 const struct skb_frag_struct *frag;
2893 frag = &skb_shinfo(skb)->frags[f];
2894 len = min((unsigned int)skb_frag_size(frag), total);
2898 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2899 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2901 tx_buffer_info->length = size;
2902 tx_buffer_info->dma =
2903 skb_frag_dma_map(tx_ring->dev, frag,
2904 offset, size, DMA_TO_DEVICE);
2905 if (dma_mapping_error(tx_ring->dev,
2906 tx_buffer_info->dma))
2908 tx_buffer_info->mapped_as_page = true;
2915 if (i == tx_ring->count)
2923 i = tx_ring->count - 1;
2926 tx_ring->tx_buffer_info[i].skb = skb;
2931 dev_err(tx_ring->dev, "TX DMA map failed\n");
2933 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2934 tx_buffer_info->dma = 0;
2937 /* clear timestamp and dma mappings for remaining portion of packet */
2938 while (count >= 0) {
2942 i += tx_ring->count;
2943 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2944 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
2950 static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
2951 int count, unsigned int first, u32 paylen,
2954 union ixgbe_adv_tx_desc *tx_desc = NULL;
2955 struct ixgbevf_tx_buffer *tx_buffer_info;
2956 u32 olinfo_status = 0, cmd_type_len = 0;
2959 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2961 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2963 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2965 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2966 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2968 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2969 olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
2971 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2972 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2974 /* use index 1 context for tso */
2975 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2976 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2977 olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
2981 * Check Context must be set if Tx switch is enabled, which it
2982 * always is for case where virtual functions are running
2984 olinfo_status |= IXGBE_ADVTXD_CC;
2986 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2988 i = tx_ring->next_to_use;
2990 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2991 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2992 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2993 tx_desc->read.cmd_type_len =
2994 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2995 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2997 if (i == tx_ring->count)
3001 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
3003 tx_ring->tx_buffer_info[first].time_stamp = jiffies;
3005 /* Force memory writes to complete before letting h/w
3006 * know there are new descriptors to fetch. (Only
3007 * applicable for weak-ordered memory model archs,
3012 tx_ring->tx_buffer_info[first].next_to_watch = tx_desc;
3013 tx_ring->next_to_use = i;
3016 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
3018 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
3020 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
3021 /* Herbert's original patch had:
3022 * smp_mb__after_netif_stop_queue();
3023 * but since that doesn't exist yet, just open code it. */
3026 /* We need to check again in a case another CPU has just
3027 * made room available. */
3028 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
3031 /* A reprieve! - use start_queue because it doesn't call schedule */
3032 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
3033 ++adapter->restart_queue;
3037 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
3039 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
3041 return __ixgbevf_maybe_stop_tx(tx_ring, size);
3044 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3046 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3047 struct ixgbevf_ring *tx_ring;
3049 unsigned int tx_flags = 0;
3052 u16 count = TXD_USE_COUNT(skb_headlen(skb));
3053 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3056 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
3057 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
3059 return NETDEV_TX_OK;
3062 tx_ring = &adapter->tx_ring[r_idx];
3065 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
3066 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
3067 * + 2 desc gap to keep tail from touching head,
3068 * + 1 desc for context descriptor,
3069 * otherwise try next time
3071 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3072 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3073 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3075 count += skb_shinfo(skb)->nr_frags;
3077 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
3079 return NETDEV_TX_BUSY;
3082 if (vlan_tx_tag_present(skb)) {
3083 tx_flags |= vlan_tx_tag_get(skb);
3084 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3085 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3088 first = tx_ring->next_to_use;
3090 if (skb->protocol == htons(ETH_P_IP))
3091 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3092 tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
3094 dev_kfree_skb_any(skb);
3095 return NETDEV_TX_OK;
3099 tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
3100 else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
3101 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3103 ixgbevf_tx_queue(tx_ring, tx_flags,
3104 ixgbevf_tx_map(tx_ring, skb, tx_flags),
3105 first, skb->len, hdr_len);
3107 writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
3109 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
3111 return NETDEV_TX_OK;
3115 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3116 * @netdev: network interface device structure
3117 * @p: pointer to an address structure
3119 * Returns 0 on success, negative on failure
3121 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3123 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3124 struct ixgbe_hw *hw = &adapter->hw;
3125 struct sockaddr *addr = p;
3127 if (!is_valid_ether_addr(addr->sa_data))
3128 return -EADDRNOTAVAIL;
3130 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3131 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3133 spin_lock_bh(&adapter->mbx_lock);
3135 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
3137 spin_unlock_bh(&adapter->mbx_lock);
3143 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3144 * @netdev: network interface device structure
3145 * @new_mtu: new value for maximum frame size
3147 * Returns 0 on success, negative on failure
3149 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3151 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3152 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3153 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
3155 switch (adapter->hw.api_version) {
3156 case ixgbe_mbox_api_11:
3157 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3160 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3161 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3165 /* MTU < 68 is an error and causes problems on some kernels */
3166 if ((new_mtu < 68) || (max_frame > max_possible_frame))
3169 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3170 netdev->mtu, new_mtu);
3171 /* must set new MTU before calling down or up */
3172 netdev->mtu = new_mtu;
3174 if (netif_running(netdev))
3175 ixgbevf_reinit_locked(adapter);
3180 static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
3182 struct net_device *netdev = pci_get_drvdata(pdev);
3183 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3188 netif_device_detach(netdev);
3190 if (netif_running(netdev)) {
3192 ixgbevf_down(adapter);
3193 ixgbevf_free_irq(adapter);
3194 ixgbevf_free_all_tx_resources(adapter);
3195 ixgbevf_free_all_rx_resources(adapter);
3199 ixgbevf_clear_interrupt_scheme(adapter);
3202 retval = pci_save_state(pdev);
3207 pci_disable_device(pdev);
3213 static int ixgbevf_resume(struct pci_dev *pdev)
3215 struct ixgbevf_adapter *adapter = pci_get_drvdata(pdev);
3216 struct net_device *netdev = adapter->netdev;
3219 pci_set_power_state(pdev, PCI_D0);
3220 pci_restore_state(pdev);
3222 * pci_restore_state clears dev->state_saved so call
3223 * pci_save_state to restore it.
3225 pci_save_state(pdev);
3227 err = pci_enable_device_mem(pdev);
3229 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3232 pci_set_master(pdev);
3234 ixgbevf_reset(adapter);
3237 err = ixgbevf_init_interrupt_scheme(adapter);
3240 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3244 if (netif_running(netdev)) {
3245 err = ixgbevf_open(netdev);
3250 netif_device_attach(netdev);
3255 #endif /* CONFIG_PM */
3256 static void ixgbevf_shutdown(struct pci_dev *pdev)
3258 ixgbevf_suspend(pdev, PMSG_SUSPEND);
3261 static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3262 struct rtnl_link_stats64 *stats)
3264 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3267 const struct ixgbevf_ring *ring;
3270 ixgbevf_update_stats(adapter);
3272 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3274 for (i = 0; i < adapter->num_rx_queues; i++) {
3275 ring = &adapter->rx_ring[i];
3277 start = u64_stats_fetch_begin_bh(&ring->syncp);
3278 bytes = ring->total_bytes;
3279 packets = ring->total_packets;
3280 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3281 stats->rx_bytes += bytes;
3282 stats->rx_packets += packets;
3285 for (i = 0; i < adapter->num_tx_queues; i++) {
3286 ring = &adapter->tx_ring[i];
3288 start = u64_stats_fetch_begin_bh(&ring->syncp);
3289 bytes = ring->total_bytes;
3290 packets = ring->total_packets;
3291 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3292 stats->tx_bytes += bytes;
3293 stats->tx_packets += packets;
3299 static const struct net_device_ops ixgbevf_netdev_ops = {
3300 .ndo_open = ixgbevf_open,
3301 .ndo_stop = ixgbevf_close,
3302 .ndo_start_xmit = ixgbevf_xmit_frame,
3303 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
3304 .ndo_get_stats64 = ixgbevf_get_stats,
3305 .ndo_validate_addr = eth_validate_addr,
3306 .ndo_set_mac_address = ixgbevf_set_mac,
3307 .ndo_change_mtu = ixgbevf_change_mtu,
3308 .ndo_tx_timeout = ixgbevf_tx_timeout,
3309 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3310 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
3313 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3315 dev->netdev_ops = &ixgbevf_netdev_ops;
3316 ixgbevf_set_ethtool_ops(dev);
3317 dev->watchdog_timeo = 5 * HZ;
3321 * ixgbevf_probe - Device Initialization Routine
3322 * @pdev: PCI device information struct
3323 * @ent: entry in ixgbevf_pci_tbl
3325 * Returns 0 on success, negative on failure
3327 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3328 * The OS initialization, configuring of the adapter private structure,
3329 * and a hardware reset occur.
3331 static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3333 struct net_device *netdev;
3334 struct ixgbevf_adapter *adapter = NULL;
3335 struct ixgbe_hw *hw = NULL;
3336 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3337 static int cards_found;
3338 int err, pci_using_dac;
3340 err = pci_enable_device(pdev);
3344 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3345 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3348 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3350 err = dma_set_coherent_mask(&pdev->dev,
3353 dev_err(&pdev->dev, "No usable DMA "
3354 "configuration, aborting\n");
3361 err = pci_request_regions(pdev, ixgbevf_driver_name);
3363 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3367 pci_set_master(pdev);
3369 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3373 goto err_alloc_etherdev;
3376 SET_NETDEV_DEV(netdev, &pdev->dev);
3378 pci_set_drvdata(pdev, netdev);
3379 adapter = netdev_priv(netdev);
3381 adapter->netdev = netdev;
3382 adapter->pdev = pdev;
3385 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3388 * call save state here in standalone driver because it relies on
3389 * adapter struct to exist, and needs to call netdev_priv
3391 pci_save_state(pdev);
3393 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3394 pci_resource_len(pdev, 0));
3400 ixgbevf_assign_netdev_ops(netdev);
3402 adapter->bd_number = cards_found;
3405 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3406 hw->mac.type = ii->mac;
3408 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3409 sizeof(struct ixgbe_mbx_operations));
3411 /* setup the private structure */
3412 err = ixgbevf_sw_init(adapter);
3416 /* The HW MAC address was set and/or determined in sw_init */
3417 if (!is_valid_ether_addr(netdev->dev_addr)) {
3418 pr_err("invalid MAC address\n");
3423 netdev->hw_features = NETIF_F_SG |
3430 netdev->features = netdev->hw_features |
3431 NETIF_F_HW_VLAN_CTAG_TX |
3432 NETIF_F_HW_VLAN_CTAG_RX |
3433 NETIF_F_HW_VLAN_CTAG_FILTER;
3435 netdev->vlan_features |= NETIF_F_TSO;
3436 netdev->vlan_features |= NETIF_F_TSO6;
3437 netdev->vlan_features |= NETIF_F_IP_CSUM;
3438 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3439 netdev->vlan_features |= NETIF_F_SG;
3442 netdev->features |= NETIF_F_HIGHDMA;
3444 netdev->priv_flags |= IFF_UNICAST_FLT;
3446 init_timer(&adapter->watchdog_timer);
3447 adapter->watchdog_timer.function = ixgbevf_watchdog;
3448 adapter->watchdog_timer.data = (unsigned long)adapter;
3450 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3451 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3453 err = ixgbevf_init_interrupt_scheme(adapter);
3457 strcpy(netdev->name, "eth%d");
3459 err = register_netdev(netdev);
3463 netif_carrier_off(netdev);
3465 ixgbevf_init_last_counter_stats(adapter);
3467 /* print the MAC address */
3468 hw_dbg(hw, "%pM\n", netdev->dev_addr);
3470 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3472 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3477 ixgbevf_clear_interrupt_scheme(adapter);
3479 ixgbevf_reset_interrupt_capability(adapter);
3480 iounmap(hw->hw_addr);
3482 free_netdev(netdev);
3484 pci_release_regions(pdev);
3487 pci_disable_device(pdev);
3492 * ixgbevf_remove - Device Removal Routine
3493 * @pdev: PCI device information struct
3495 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3496 * that it should release a PCI device. The could be caused by a
3497 * Hot-Plug event, or because the driver is going to be removed from
3500 static void ixgbevf_remove(struct pci_dev *pdev)
3502 struct net_device *netdev = pci_get_drvdata(pdev);
3503 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3505 set_bit(__IXGBEVF_DOWN, &adapter->state);
3507 del_timer_sync(&adapter->watchdog_timer);
3509 cancel_work_sync(&adapter->reset_task);
3510 cancel_work_sync(&adapter->watchdog_task);
3512 if (netdev->reg_state == NETREG_REGISTERED)
3513 unregister_netdev(netdev);
3515 ixgbevf_clear_interrupt_scheme(adapter);
3516 ixgbevf_reset_interrupt_capability(adapter);
3518 iounmap(adapter->hw.hw_addr);
3519 pci_release_regions(pdev);
3521 hw_dbg(&adapter->hw, "Remove complete\n");
3523 kfree(adapter->tx_ring);
3524 kfree(adapter->rx_ring);
3526 free_netdev(netdev);
3528 pci_disable_device(pdev);
3532 * ixgbevf_io_error_detected - called when PCI error is detected
3533 * @pdev: Pointer to PCI device
3534 * @state: The current pci connection state
3536 * This function is called after a PCI bus error affecting
3537 * this device has been detected.
3539 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3540 pci_channel_state_t state)
3542 struct net_device *netdev = pci_get_drvdata(pdev);
3543 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3545 netif_device_detach(netdev);
3547 if (state == pci_channel_io_perm_failure)
3548 return PCI_ERS_RESULT_DISCONNECT;
3550 if (netif_running(netdev))
3551 ixgbevf_down(adapter);
3553 pci_disable_device(pdev);
3555 /* Request a slot slot reset. */
3556 return PCI_ERS_RESULT_NEED_RESET;
3560 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3561 * @pdev: Pointer to PCI device
3563 * Restart the card from scratch, as if from a cold-boot. Implementation
3564 * resembles the first-half of the ixgbevf_resume routine.
3566 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3568 struct net_device *netdev = pci_get_drvdata(pdev);
3569 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3571 if (pci_enable_device_mem(pdev)) {
3573 "Cannot re-enable PCI device after reset.\n");
3574 return PCI_ERS_RESULT_DISCONNECT;
3577 pci_set_master(pdev);
3579 ixgbevf_reset(adapter);
3581 return PCI_ERS_RESULT_RECOVERED;
3585 * ixgbevf_io_resume - called when traffic can start flowing again.
3586 * @pdev: Pointer to PCI device
3588 * This callback is called when the error recovery driver tells us that
3589 * its OK to resume normal operation. Implementation resembles the
3590 * second-half of the ixgbevf_resume routine.
3592 static void ixgbevf_io_resume(struct pci_dev *pdev)
3594 struct net_device *netdev = pci_get_drvdata(pdev);
3595 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3597 if (netif_running(netdev))
3598 ixgbevf_up(adapter);
3600 netif_device_attach(netdev);
3603 /* PCI Error Recovery (ERS) */
3604 static const struct pci_error_handlers ixgbevf_err_handler = {
3605 .error_detected = ixgbevf_io_error_detected,
3606 .slot_reset = ixgbevf_io_slot_reset,
3607 .resume = ixgbevf_io_resume,
3610 static struct pci_driver ixgbevf_driver = {
3611 .name = ixgbevf_driver_name,
3612 .id_table = ixgbevf_pci_tbl,
3613 .probe = ixgbevf_probe,
3614 .remove = ixgbevf_remove,
3616 /* Power Management Hooks */
3617 .suspend = ixgbevf_suspend,
3618 .resume = ixgbevf_resume,
3620 .shutdown = ixgbevf_shutdown,
3621 .err_handler = &ixgbevf_err_handler
3625 * ixgbevf_init_module - Driver Registration Routine
3627 * ixgbevf_init_module is the first routine called when the driver is
3628 * loaded. All it does is register with the PCI subsystem.
3630 static int __init ixgbevf_init_module(void)
3633 pr_info("%s - version %s\n", ixgbevf_driver_string,
3634 ixgbevf_driver_version);
3636 pr_info("%s\n", ixgbevf_copyright);
3638 ret = pci_register_driver(&ixgbevf_driver);
3642 module_init(ixgbevf_init_module);
3645 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3647 * ixgbevf_exit_module is called just before the driver is removed
3650 static void __exit ixgbevf_exit_module(void)
3652 pci_unregister_driver(&ixgbevf_driver);
3657 * ixgbevf_get_hw_dev_name - return device name string
3658 * used by hardware layer to print debugging information
3660 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3662 struct ixgbevf_adapter *adapter = hw->back;
3663 return adapter->netdev->name;
3667 module_exit(ixgbevf_exit_module);
3669 /* ixgbevf_main.c */