1 /*******************************************************************************
3 Intel 82599 Virtual Function driver
4 Copyright(c) 1999 - 2012 Intel Corporation.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *******************************************************************************/
29 /******************************************************************************
30 Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
31 ******************************************************************************/
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/types.h>
36 #include <linux/bitops.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/netdevice.h>
40 #include <linux/vmalloc.h>
41 #include <linux/string.h>
44 #include <linux/tcp.h>
45 #include <linux/sctp.h>
46 #include <linux/ipv6.h>
47 #include <linux/slab.h>
48 #include <net/checksum.h>
49 #include <net/ip6_checksum.h>
50 #include <linux/ethtool.h>
52 #include <linux/if_vlan.h>
53 #include <linux/prefetch.h>
57 const char ixgbevf_driver_name[] = "ixgbevf";
58 static const char ixgbevf_driver_string[] =
59 "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
61 #define DRV_VERSION "2.7.12-k"
62 const char ixgbevf_driver_version[] = DRV_VERSION;
63 static char ixgbevf_copyright[] =
64 "Copyright (c) 2009 - 2012 Intel Corporation.";
66 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
67 [board_82599_vf] = &ixgbevf_82599_vf_info,
68 [board_X540_vf] = &ixgbevf_X540_vf_info,
71 /* ixgbevf_pci_tbl - PCI Device ID Table
73 * Wildcard entries (PCI_ANY_ID) should come last
74 * Last entry must be all 0s
76 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
77 * Class, Class Mask, private data (not used) }
79 static struct pci_device_id ixgbevf_pci_tbl[] = {
80 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF),
82 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF),
85 /* required last entry */
88 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
90 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
91 MODULE_DESCRIPTION("Intel(R) 82599 Virtual Function Driver");
92 MODULE_LICENSE("GPL");
93 MODULE_VERSION(DRV_VERSION);
95 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
96 static int debug = -1;
97 module_param(debug, int, 0);
98 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
101 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
102 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
104 static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
105 struct ixgbevf_ring *rx_ring,
109 * Force memory writes to complete before letting h/w
110 * know there are new descriptors to fetch. (Only
111 * applicable for weak-ordered memory model archs,
115 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
119 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
120 * @adapter: pointer to adapter struct
121 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
122 * @queue: queue to map the corresponding interrupt to
123 * @msix_vector: the vector to map to the corresponding queue
125 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
126 u8 queue, u8 msix_vector)
129 struct ixgbe_hw *hw = &adapter->hw;
130 if (direction == -1) {
132 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
133 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
136 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
138 /* tx or rx causes */
139 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
140 index = ((16 * (queue & 1)) + (8 * direction));
141 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
142 ivar &= ~(0xFF << index);
143 ivar |= (msix_vector << index);
144 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
148 static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
149 struct ixgbevf_tx_buffer
152 if (tx_buffer_info->dma) {
153 if (tx_buffer_info->mapped_as_page)
154 dma_unmap_page(tx_ring->dev,
156 tx_buffer_info->length,
159 dma_unmap_single(tx_ring->dev,
161 tx_buffer_info->length,
163 tx_buffer_info->dma = 0;
165 if (tx_buffer_info->skb) {
166 dev_kfree_skb_any(tx_buffer_info->skb);
167 tx_buffer_info->skb = NULL;
169 tx_buffer_info->time_stamp = 0;
170 /* tx_buffer_info must be completely set up in the transmit path */
173 #define IXGBE_MAX_TXD_PWR 14
174 #define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
176 /* Tx Descriptors needed, worst case */
177 #define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
178 #define DESC_NEEDED (MAX_SKB_FRAGS + 4)
180 static void ixgbevf_tx_timeout(struct net_device *netdev);
183 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
184 * @q_vector: board private structure
185 * @tx_ring: tx ring to clean
187 static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
188 struct ixgbevf_ring *tx_ring)
190 struct ixgbevf_adapter *adapter = q_vector->adapter;
191 union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
192 struct ixgbevf_tx_buffer *tx_buffer_info;
193 unsigned int i, eop, count = 0;
194 unsigned int total_bytes = 0, total_packets = 0;
196 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
199 i = tx_ring->next_to_clean;
200 eop = tx_ring->tx_buffer_info[i].next_to_watch;
201 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
203 while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
204 (count < tx_ring->count)) {
205 bool cleaned = false;
206 rmb(); /* read buffer_info after eop_desc */
207 /* eop could change between read and DD-check */
208 if (unlikely(eop != tx_ring->tx_buffer_info[i].next_to_watch))
210 for ( ; !cleaned; count++) {
212 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
213 tx_buffer_info = &tx_ring->tx_buffer_info[i];
214 cleaned = (i == eop);
215 skb = tx_buffer_info->skb;
217 if (cleaned && skb) {
218 unsigned int segs, bytecount;
220 /* gso_segs is currently only valid for tcp */
221 segs = skb_shinfo(skb)->gso_segs ?: 1;
222 /* multiply data chunks by size of headers */
223 bytecount = ((segs - 1) * skb_headlen(skb)) +
225 total_packets += segs;
226 total_bytes += bytecount;
229 ixgbevf_unmap_and_free_tx_resource(tx_ring,
232 tx_desc->wb.status = 0;
235 if (i == tx_ring->count)
240 eop = tx_ring->tx_buffer_info[i].next_to_watch;
241 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
244 tx_ring->next_to_clean = i;
246 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
247 if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
248 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
249 /* Make sure that anybody stopping the queue after this
250 * sees the new next_to_clean.
253 if (__netif_subqueue_stopped(tx_ring->netdev,
254 tx_ring->queue_index) &&
255 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
256 netif_wake_subqueue(tx_ring->netdev,
257 tx_ring->queue_index);
258 ++adapter->restart_queue;
262 u64_stats_update_begin(&tx_ring->syncp);
263 tx_ring->total_bytes += total_bytes;
264 tx_ring->total_packets += total_packets;
265 u64_stats_update_end(&tx_ring->syncp);
266 q_vector->tx.total_bytes += total_bytes;
267 q_vector->tx.total_packets += total_packets;
269 return count < tx_ring->count;
273 * ixgbevf_receive_skb - Send a completed packet up the stack
274 * @q_vector: structure containing interrupt and ring information
275 * @skb: packet to send up
276 * @status: hardware indication of status of receive
277 * @rx_desc: rx descriptor
279 static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
280 struct sk_buff *skb, u8 status,
281 union ixgbe_adv_rx_desc *rx_desc)
283 struct ixgbevf_adapter *adapter = q_vector->adapter;
284 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
285 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
287 if (is_vlan && test_bit(tag & VLAN_VID_MASK, adapter->active_vlans))
288 __vlan_hwaccel_put_tag(skb, tag);
290 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
291 napi_gro_receive(&q_vector->napi, skb);
297 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
298 * @adapter: address of board private structure
299 * @status_err: hardware indication of status of receive
300 * @skb: skb currently being received and modified
302 static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
303 struct ixgbevf_ring *ring,
304 u32 status_err, struct sk_buff *skb)
306 skb_checksum_none_assert(skb);
308 /* Rx csum disabled */
309 if (!(ring->netdev->features & NETIF_F_RXCSUM))
312 /* if IP and error */
313 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
314 (status_err & IXGBE_RXDADV_ERR_IPE)) {
315 adapter->hw_csum_rx_error++;
319 if (!(status_err & IXGBE_RXD_STAT_L4CS))
322 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
323 adapter->hw_csum_rx_error++;
327 /* It must be a TCP or UDP packet with a valid checksum */
328 skb->ip_summed = CHECKSUM_UNNECESSARY;
329 adapter->hw_csum_rx_good++;
333 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
334 * @adapter: address of board private structure
336 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
337 struct ixgbevf_ring *rx_ring,
340 struct pci_dev *pdev = adapter->pdev;
341 union ixgbe_adv_rx_desc *rx_desc;
342 struct ixgbevf_rx_buffer *bi;
343 unsigned int i = rx_ring->next_to_use;
345 bi = &rx_ring->rx_buffer_info[i];
347 while (cleaned_count--) {
348 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
353 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
354 rx_ring->rx_buf_len);
356 adapter->alloc_rx_buff_failed++;
361 bi->dma = dma_map_single(&pdev->dev, skb->data,
364 if (dma_mapping_error(&pdev->dev, bi->dma)) {
367 dev_err(&pdev->dev, "RX DMA map failed\n");
371 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
374 if (i == rx_ring->count)
376 bi = &rx_ring->rx_buffer_info[i];
380 if (rx_ring->next_to_use != i) {
381 rx_ring->next_to_use = i;
382 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
386 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
389 struct ixgbe_hw *hw = &adapter->hw;
391 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
394 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
395 struct ixgbevf_ring *rx_ring,
398 struct ixgbevf_adapter *adapter = q_vector->adapter;
399 struct pci_dev *pdev = adapter->pdev;
400 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
401 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
405 int cleaned_count = 0;
406 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
408 i = rx_ring->next_to_clean;
409 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
410 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
411 rx_buffer_info = &rx_ring->rx_buffer_info[i];
413 while (staterr & IXGBE_RXD_STAT_DD) {
418 rmb(); /* read descriptor and rx_buffer_info after status DD */
419 len = le16_to_cpu(rx_desc->wb.upper.length);
420 skb = rx_buffer_info->skb;
421 prefetch(skb->data - NET_IP_ALIGN);
422 rx_buffer_info->skb = NULL;
424 if (rx_buffer_info->dma) {
425 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
428 rx_buffer_info->dma = 0;
433 if (i == rx_ring->count)
436 next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
440 next_buffer = &rx_ring->rx_buffer_info[i];
442 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
443 skb->next = next_buffer->skb;
444 IXGBE_CB(skb->next)->prev = skb;
445 adapter->non_eop_descs++;
449 /* we should not be chaining buffers, if we did drop the skb */
450 if (IXGBE_CB(skb)->prev) {
452 struct sk_buff *this = skb;
453 skb = IXGBE_CB(skb)->prev;
459 /* ERR_MASK will only have valid bits if EOP set */
460 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
461 dev_kfree_skb_irq(skb);
465 ixgbevf_rx_checksum(adapter, rx_ring, staterr, skb);
467 /* probably a little skewed due to removing CRC */
468 total_rx_bytes += skb->len;
472 * Work around issue of some types of VM to VM loop back
473 * packets not getting split correctly
475 if (staterr & IXGBE_RXD_STAT_LB) {
476 u32 header_fixup_len = skb_headlen(skb);
477 if (header_fixup_len < 14)
478 skb_push(skb, header_fixup_len);
480 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
482 /* Workaround hardware that can't do proper VEPA multicast
485 if ((skb->pkt_type & (PACKET_BROADCAST | PACKET_MULTICAST)) &&
486 !(compare_ether_addr(adapter->netdev->dev_addr,
487 eth_hdr(skb)->h_source))) {
488 dev_kfree_skb_irq(skb);
492 ixgbevf_receive_skb(q_vector, skb, staterr, rx_desc);
495 rx_desc->wb.upper.status_error = 0;
497 /* return some buffers to hardware, one at a time is too slow */
498 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
499 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
504 /* use prefetched values */
506 rx_buffer_info = &rx_ring->rx_buffer_info[i];
508 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
511 rx_ring->next_to_clean = i;
512 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
515 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
517 u64_stats_update_begin(&rx_ring->syncp);
518 rx_ring->total_packets += total_rx_packets;
519 rx_ring->total_bytes += total_rx_bytes;
520 u64_stats_update_end(&rx_ring->syncp);
521 q_vector->rx.total_packets += total_rx_packets;
522 q_vector->rx.total_bytes += total_rx_bytes;
528 * ixgbevf_poll - NAPI polling calback
529 * @napi: napi struct with our devices info in it
530 * @budget: amount of work driver is allowed to do this pass, in packets
532 * This function will clean more than one or more rings associated with a
535 static int ixgbevf_poll(struct napi_struct *napi, int budget)
537 struct ixgbevf_q_vector *q_vector =
538 container_of(napi, struct ixgbevf_q_vector, napi);
539 struct ixgbevf_adapter *adapter = q_vector->adapter;
540 struct ixgbevf_ring *ring;
542 bool clean_complete = true;
544 ixgbevf_for_each_ring(ring, q_vector->tx)
545 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
547 /* attempt to distribute budget to each queue fairly, but don't allow
548 * the budget to go below 1 because we'll exit polling */
549 if (q_vector->rx.count > 1)
550 per_ring_budget = max(budget/q_vector->rx.count, 1);
552 per_ring_budget = budget;
554 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
555 ixgbevf_for_each_ring(ring, q_vector->rx)
556 clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
558 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
560 /* If all work not completed, return budget and keep polling */
563 /* all work done, exit the polling mode */
565 if (adapter->rx_itr_setting & 1)
566 ixgbevf_set_itr(q_vector);
567 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
568 ixgbevf_irq_enable_queues(adapter,
569 1 << q_vector->v_idx);
575 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
576 * @q_vector: structure containing interrupt and ring information
578 static void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
580 struct ixgbevf_adapter *adapter = q_vector->adapter;
581 struct ixgbe_hw *hw = &adapter->hw;
582 int v_idx = q_vector->v_idx;
583 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
586 * set the WDIS bit to not clear the timer bits and cause an
587 * immediate assertion of the interrupt
589 itr_reg |= IXGBE_EITR_CNT_WDIS;
591 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
595 * ixgbevf_configure_msix - Configure MSI-X hardware
596 * @adapter: board private structure
598 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
601 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
603 struct ixgbevf_q_vector *q_vector;
604 int q_vectors, v_idx;
606 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
607 adapter->eims_enable_mask = 0;
610 * Populate the IVAR table and set the ITR values to the
611 * corresponding register.
613 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
614 struct ixgbevf_ring *ring;
615 q_vector = adapter->q_vector[v_idx];
617 ixgbevf_for_each_ring(ring, q_vector->rx)
618 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
620 ixgbevf_for_each_ring(ring, q_vector->tx)
621 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
623 if (q_vector->tx.ring && !q_vector->rx.ring) {
625 if (adapter->tx_itr_setting == 1)
626 q_vector->itr = IXGBE_10K_ITR;
628 q_vector->itr = adapter->tx_itr_setting;
630 /* rx or rx/tx vector */
631 if (adapter->rx_itr_setting == 1)
632 q_vector->itr = IXGBE_20K_ITR;
634 q_vector->itr = adapter->rx_itr_setting;
637 /* add q_vector eims value to global eims_enable_mask */
638 adapter->eims_enable_mask |= 1 << v_idx;
640 ixgbevf_write_eitr(q_vector);
643 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
644 /* setup eims_other and add value to global eims_enable_mask */
645 adapter->eims_other = 1 << v_idx;
646 adapter->eims_enable_mask |= adapter->eims_other;
653 latency_invalid = 255
657 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
658 * @q_vector: structure containing interrupt and ring information
659 * @ring_container: structure containing ring performance data
661 * Stores a new ITR value based on packets and byte
662 * counts during the last interrupt. The advantage of per interrupt
663 * computation is faster updates and more accurate ITR for the current
664 * traffic pattern. Constants in this function were computed
665 * based on theoretical maximum wire speed and thresholds were set based
666 * on testing data as well as attempting to minimize response time
667 * while increasing bulk throughput.
669 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
670 struct ixgbevf_ring_container *ring_container)
672 int bytes = ring_container->total_bytes;
673 int packets = ring_container->total_packets;
676 u8 itr_setting = ring_container->itr;
681 /* simple throttlerate management
682 * 0-20MB/s lowest (100000 ints/s)
683 * 20-100MB/s low (20000 ints/s)
684 * 100-1249MB/s bulk (8000 ints/s)
686 /* what was last interrupt timeslice? */
687 timepassed_us = q_vector->itr >> 2;
688 bytes_perint = bytes / timepassed_us; /* bytes/usec */
690 switch (itr_setting) {
692 if (bytes_perint > 10)
693 itr_setting = low_latency;
696 if (bytes_perint > 20)
697 itr_setting = bulk_latency;
698 else if (bytes_perint <= 10)
699 itr_setting = lowest_latency;
702 if (bytes_perint <= 20)
703 itr_setting = low_latency;
707 /* clear work counters since we have the values we need */
708 ring_container->total_bytes = 0;
709 ring_container->total_packets = 0;
711 /* write updated itr to ring container */
712 ring_container->itr = itr_setting;
715 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
717 u32 new_itr = q_vector->itr;
720 ixgbevf_update_itr(q_vector, &q_vector->tx);
721 ixgbevf_update_itr(q_vector, &q_vector->rx);
723 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
725 switch (current_itr) {
726 /* counts and packets in update_itr are dependent on these numbers */
728 new_itr = IXGBE_100K_ITR;
731 new_itr = IXGBE_20K_ITR;
735 new_itr = IXGBE_8K_ITR;
739 if (new_itr != q_vector->itr) {
740 /* do an exponential smoothing */
741 new_itr = (10 * new_itr * q_vector->itr) /
742 ((9 * new_itr) + q_vector->itr);
744 /* save the algorithm value here */
745 q_vector->itr = new_itr;
747 ixgbevf_write_eitr(q_vector);
751 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
753 struct ixgbevf_adapter *adapter = data;
754 struct ixgbe_hw *hw = &adapter->hw;
756 hw->mac.get_link_status = 1;
758 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
759 mod_timer(&adapter->watchdog_timer, jiffies);
761 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
767 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
769 * @data: pointer to our q_vector struct for this interrupt vector
771 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
773 struct ixgbevf_q_vector *q_vector = data;
775 /* EIAM disabled interrupts (on this vector) for us */
776 if (q_vector->rx.ring || q_vector->tx.ring)
777 napi_schedule(&q_vector->napi);
782 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
785 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
787 a->rx_ring[r_idx].next = q_vector->rx.ring;
788 q_vector->rx.ring = &a->rx_ring[r_idx];
789 q_vector->rx.count++;
792 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
795 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
797 a->tx_ring[t_idx].next = q_vector->tx.ring;
798 q_vector->tx.ring = &a->tx_ring[t_idx];
799 q_vector->tx.count++;
803 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
804 * @adapter: board private structure to initialize
806 * This function maps descriptor rings to the queue-specific vectors
807 * we were allotted through the MSI-X enabling code. Ideally, we'd have
808 * one vector per ring/queue, but on a constrained vector budget, we
809 * group the rings as "efficiently" as possible. You would add new
810 * mapping configurations in here.
812 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
816 int rxr_idx = 0, txr_idx = 0;
817 int rxr_remaining = adapter->num_rx_queues;
818 int txr_remaining = adapter->num_tx_queues;
823 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
826 * The ideal configuration...
827 * We have enough vectors to map one per queue.
829 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
830 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
831 map_vector_to_rxq(adapter, v_start, rxr_idx);
833 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
834 map_vector_to_txq(adapter, v_start, txr_idx);
839 * If we don't have enough vectors for a 1-to-1
840 * mapping, we'll have to group them so there are
841 * multiple queues per vector.
843 /* Re-adjusting *qpv takes care of the remainder. */
844 for (i = v_start; i < q_vectors; i++) {
845 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
846 for (j = 0; j < rqpv; j++) {
847 map_vector_to_rxq(adapter, i, rxr_idx);
852 for (i = v_start; i < q_vectors; i++) {
853 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
854 for (j = 0; j < tqpv; j++) {
855 map_vector_to_txq(adapter, i, txr_idx);
866 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
867 * @adapter: board private structure
869 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
870 * interrupts from the kernel.
872 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
874 struct net_device *netdev = adapter->netdev;
875 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
879 for (vector = 0; vector < q_vectors; vector++) {
880 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
881 struct msix_entry *entry = &adapter->msix_entries[vector];
883 if (q_vector->tx.ring && q_vector->rx.ring) {
884 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
885 "%s-%s-%d", netdev->name, "TxRx", ri++);
887 } else if (q_vector->rx.ring) {
888 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
889 "%s-%s-%d", netdev->name, "rx", ri++);
890 } else if (q_vector->tx.ring) {
891 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
892 "%s-%s-%d", netdev->name, "tx", ti++);
894 /* skip this unused q_vector */
897 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
898 q_vector->name, q_vector);
901 "request_irq failed for MSIX interrupt "
903 goto free_queue_irqs;
907 err = request_irq(adapter->msix_entries[vector].vector,
908 &ixgbevf_msix_other, 0, netdev->name, adapter);
911 "request_irq for msix_other failed: %d\n", err);
912 goto free_queue_irqs;
920 free_irq(adapter->msix_entries[vector].vector,
921 adapter->q_vector[vector]);
923 pci_disable_msix(adapter->pdev);
924 kfree(adapter->msix_entries);
925 adapter->msix_entries = NULL;
929 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
931 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
933 for (i = 0; i < q_vectors; i++) {
934 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
935 q_vector->rx.ring = NULL;
936 q_vector->tx.ring = NULL;
937 q_vector->rx.count = 0;
938 q_vector->tx.count = 0;
943 * ixgbevf_request_irq - initialize interrupts
944 * @adapter: board private structure
946 * Attempts to configure interrupts using the best available
947 * capabilities of the hardware and kernel.
949 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
953 err = ixgbevf_request_msix_irqs(adapter);
957 "request_irq failed, Error %d\n", err);
962 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
966 q_vectors = adapter->num_msix_vectors;
969 free_irq(adapter->msix_entries[i].vector, adapter);
972 for (; i >= 0; i--) {
973 /* free only the irqs that were actually requested */
974 if (!adapter->q_vector[i]->rx.ring &&
975 !adapter->q_vector[i]->tx.ring)
978 free_irq(adapter->msix_entries[i].vector,
979 adapter->q_vector[i]);
982 ixgbevf_reset_q_vectors(adapter);
986 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
987 * @adapter: board private structure
989 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
991 struct ixgbe_hw *hw = &adapter->hw;
994 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
995 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
996 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
998 IXGBE_WRITE_FLUSH(hw);
1000 for (i = 0; i < adapter->num_msix_vectors; i++)
1001 synchronize_irq(adapter->msix_entries[i].vector);
1005 * ixgbevf_irq_enable - Enable default interrupt generation settings
1006 * @adapter: board private structure
1008 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1010 struct ixgbe_hw *hw = &adapter->hw;
1012 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1013 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1014 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1018 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1019 * @adapter: board private structure
1021 * Configure the Tx unit of the MAC after a reset.
1023 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1026 struct ixgbe_hw *hw = &adapter->hw;
1027 u32 i, j, tdlen, txctrl;
1029 /* Setup the HW Tx Head and Tail descriptor pointers */
1030 for (i = 0; i < adapter->num_tx_queues; i++) {
1031 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1034 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1035 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1036 (tdba & DMA_BIT_MASK(32)));
1037 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1038 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1039 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1040 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1041 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1042 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1043 /* Disable Tx Head Writeback RO bit, since this hoses
1044 * bookkeeping if things aren't delivered in order.
1046 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1047 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1048 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1052 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1054 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1056 struct ixgbevf_ring *rx_ring;
1057 struct ixgbe_hw *hw = &adapter->hw;
1060 rx_ring = &adapter->rx_ring[index];
1062 srrctl = IXGBE_SRRCTL_DROP_EN;
1064 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1066 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1067 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1069 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1072 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1074 struct ixgbe_hw *hw = &adapter->hw;
1075 struct net_device *netdev = adapter->netdev;
1076 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1080 /* notify the PF of our intent to use this size of frame */
1081 ixgbevf_rlpml_set_vf(hw, max_frame);
1083 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1084 max_frame += VLAN_HLEN;
1087 * Allocate buffer sizes that fit well into 32K and
1088 * take into account max frame size of 9.5K
1090 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1091 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1092 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1093 else if (max_frame <= IXGBEVF_RXBUFFER_2K)
1094 rx_buf_len = IXGBEVF_RXBUFFER_2K;
1095 else if (max_frame <= IXGBEVF_RXBUFFER_4K)
1096 rx_buf_len = IXGBEVF_RXBUFFER_4K;
1097 else if (max_frame <= IXGBEVF_RXBUFFER_8K)
1098 rx_buf_len = IXGBEVF_RXBUFFER_8K;
1100 rx_buf_len = IXGBEVF_RXBUFFER_10K;
1102 for (i = 0; i < adapter->num_rx_queues; i++)
1103 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1107 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1108 * @adapter: board private structure
1110 * Configure the Rx unit of the MAC after a reset.
1112 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1115 struct ixgbe_hw *hw = &adapter->hw;
1119 /* PSRTYPE must be initialized in 82599 */
1120 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
1122 /* set_rx_buffer_len must be called before ring initialization */
1123 ixgbevf_set_rx_buffer_len(adapter);
1125 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1126 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1127 * the Base and Length of the Rx Descriptor Ring */
1128 for (i = 0; i < adapter->num_rx_queues; i++) {
1129 rdba = adapter->rx_ring[i].dma;
1130 j = adapter->rx_ring[i].reg_idx;
1131 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1132 (rdba & DMA_BIT_MASK(32)));
1133 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1134 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1135 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1136 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1137 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1138 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1140 ixgbevf_configure_srrctl(adapter, j);
1144 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1146 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1147 struct ixgbe_hw *hw = &adapter->hw;
1150 spin_lock_bh(&adapter->mbx_lock);
1152 /* add VID to filter table */
1153 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
1155 spin_unlock_bh(&adapter->mbx_lock);
1157 /* translate error return types so error makes sense */
1158 if (err == IXGBE_ERR_MBX)
1161 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1164 set_bit(vid, adapter->active_vlans);
1169 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1171 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1172 struct ixgbe_hw *hw = &adapter->hw;
1173 int err = -EOPNOTSUPP;
1175 spin_lock_bh(&adapter->mbx_lock);
1177 /* remove VID from filter table */
1178 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
1180 spin_unlock_bh(&adapter->mbx_lock);
1182 clear_bit(vid, adapter->active_vlans);
1187 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1191 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1192 ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
1195 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1197 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1198 struct ixgbe_hw *hw = &adapter->hw;
1201 if ((netdev_uc_count(netdev)) > 10) {
1202 pr_err("Too many unicast filters - No Space\n");
1206 if (!netdev_uc_empty(netdev)) {
1207 struct netdev_hw_addr *ha;
1208 netdev_for_each_uc_addr(ha, netdev) {
1209 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1214 * If the list is empty then send message to PF driver to
1215 * clear all macvlans on this VF.
1217 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1224 * ixgbevf_set_rx_mode - Multicast and unicast set
1225 * @netdev: network interface device structure
1227 * The set_rx_method entry point is called whenever the multicast address
1228 * list, unicast address list or the network interface flags are updated.
1229 * This routine is responsible for configuring the hardware for proper
1230 * multicast mode and configuring requested unicast filters.
1232 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1234 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1235 struct ixgbe_hw *hw = &adapter->hw;
1237 spin_lock_bh(&adapter->mbx_lock);
1239 /* reprogram multicast list */
1240 hw->mac.ops.update_mc_addr_list(hw, netdev);
1242 ixgbevf_write_uc_addr_list(netdev);
1244 spin_unlock_bh(&adapter->mbx_lock);
1247 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1250 struct ixgbevf_q_vector *q_vector;
1251 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1253 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1254 q_vector = adapter->q_vector[q_idx];
1255 napi_enable(&q_vector->napi);
1259 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1262 struct ixgbevf_q_vector *q_vector;
1263 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1265 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1266 q_vector = adapter->q_vector[q_idx];
1267 napi_disable(&q_vector->napi);
1271 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1273 struct net_device *netdev = adapter->netdev;
1276 ixgbevf_set_rx_mode(netdev);
1278 ixgbevf_restore_vlan(adapter);
1280 ixgbevf_configure_tx(adapter);
1281 ixgbevf_configure_rx(adapter);
1282 for (i = 0; i < adapter->num_rx_queues; i++) {
1283 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1284 ixgbevf_alloc_rx_buffers(adapter, ring,
1285 IXGBE_DESC_UNUSED(ring));
1289 #define IXGBE_MAX_RX_DESC_POLL 10
1290 static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1293 struct ixgbe_hw *hw = &adapter->hw;
1294 int j = adapter->rx_ring[rxr].reg_idx;
1297 for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1298 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1303 if (k >= IXGBE_MAX_RX_DESC_POLL) {
1304 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1305 "not set within the polling period\n", rxr);
1308 ixgbevf_release_rx_desc(hw, &adapter->rx_ring[rxr],
1309 adapter->rx_ring[rxr].count - 1);
1312 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1314 /* Only save pre-reset stats if there are some */
1315 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1316 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1317 adapter->stats.base_vfgprc;
1318 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1319 adapter->stats.base_vfgptc;
1320 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1321 adapter->stats.base_vfgorc;
1322 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1323 adapter->stats.base_vfgotc;
1324 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1325 adapter->stats.base_vfmprc;
1329 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1331 struct ixgbe_hw *hw = &adapter->hw;
1333 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1334 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1335 adapter->stats.last_vfgorc |=
1336 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1337 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1338 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1339 adapter->stats.last_vfgotc |=
1340 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1341 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1343 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1344 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1345 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1346 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1347 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1350 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1352 struct ixgbe_hw *hw = &adapter->hw;
1353 int api[] = { ixgbe_mbox_api_11,
1355 ixgbe_mbox_api_unknown };
1356 int err = 0, idx = 0;
1358 spin_lock_bh(&adapter->mbx_lock);
1360 while (api[idx] != ixgbe_mbox_api_unknown) {
1361 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1367 spin_unlock_bh(&adapter->mbx_lock);
1370 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1372 struct net_device *netdev = adapter->netdev;
1373 struct ixgbe_hw *hw = &adapter->hw;
1375 int num_rx_rings = adapter->num_rx_queues;
1378 for (i = 0; i < adapter->num_tx_queues; i++) {
1379 j = adapter->tx_ring[i].reg_idx;
1380 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1381 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1382 txdctl |= (8 << 16);
1383 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1386 for (i = 0; i < adapter->num_tx_queues; i++) {
1387 j = adapter->tx_ring[i].reg_idx;
1388 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1389 txdctl |= IXGBE_TXDCTL_ENABLE;
1390 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1393 for (i = 0; i < num_rx_rings; i++) {
1394 j = adapter->rx_ring[i].reg_idx;
1395 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1396 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1397 if (hw->mac.type == ixgbe_mac_X540_vf) {
1398 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1399 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1400 IXGBE_RXDCTL_RLPML_EN);
1402 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1403 ixgbevf_rx_desc_queue_enable(adapter, i);
1406 ixgbevf_configure_msix(adapter);
1408 spin_lock_bh(&adapter->mbx_lock);
1410 if (is_valid_ether_addr(hw->mac.addr))
1411 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1413 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1415 spin_unlock_bh(&adapter->mbx_lock);
1417 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1418 ixgbevf_napi_enable_all(adapter);
1420 /* enable transmits */
1421 netif_tx_start_all_queues(netdev);
1423 ixgbevf_save_reset_stats(adapter);
1424 ixgbevf_init_last_counter_stats(adapter);
1426 hw->mac.get_link_status = 1;
1427 mod_timer(&adapter->watchdog_timer, jiffies);
1430 static int ixgbevf_reset_queues(struct ixgbevf_adapter *adapter)
1432 struct ixgbe_hw *hw = &adapter->hw;
1433 struct ixgbevf_ring *rx_ring;
1434 unsigned int def_q = 0;
1435 unsigned int num_tcs = 0;
1436 unsigned int num_rx_queues = 1;
1439 spin_lock_bh(&adapter->mbx_lock);
1441 /* fetch queue configuration from the PF */
1442 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1444 spin_unlock_bh(&adapter->mbx_lock);
1450 /* update default Tx ring register index */
1451 adapter->tx_ring[0].reg_idx = def_q;
1453 /* we need as many queues as traffic classes */
1454 num_rx_queues = num_tcs;
1457 /* nothing to do if we have the correct number of queues */
1458 if (adapter->num_rx_queues == num_rx_queues)
1461 /* allocate new rings */
1462 rx_ring = kcalloc(num_rx_queues,
1463 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1467 /* setup ring fields */
1468 for (i = 0; i < num_rx_queues; i++) {
1469 rx_ring[i].count = adapter->rx_ring_count;
1470 rx_ring[i].queue_index = i;
1471 rx_ring[i].reg_idx = i;
1472 rx_ring[i].dev = &adapter->pdev->dev;
1473 rx_ring[i].netdev = adapter->netdev;
1475 /* allocate resources on the ring */
1476 err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
1480 ixgbevf_free_rx_resources(adapter, &rx_ring[i]);
1487 /* free the existing rings and queues */
1488 ixgbevf_free_all_rx_resources(adapter);
1489 adapter->num_rx_queues = 0;
1490 kfree(adapter->rx_ring);
1492 /* move new rings into position on the adapter struct */
1493 adapter->rx_ring = rx_ring;
1494 adapter->num_rx_queues = num_rx_queues;
1496 /* reset ring to vector mapping */
1497 ixgbevf_reset_q_vectors(adapter);
1498 ixgbevf_map_rings_to_vectors(adapter);
1503 void ixgbevf_up(struct ixgbevf_adapter *adapter)
1505 struct ixgbe_hw *hw = &adapter->hw;
1507 ixgbevf_negotiate_api(adapter);
1509 ixgbevf_reset_queues(adapter);
1511 ixgbevf_configure(adapter);
1513 ixgbevf_up_complete(adapter);
1515 /* clear any pending interrupts, may auto mask */
1516 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1518 ixgbevf_irq_enable(adapter);
1522 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1523 * @adapter: board private structure
1524 * @rx_ring: ring to free buffers from
1526 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1527 struct ixgbevf_ring *rx_ring)
1529 struct pci_dev *pdev = adapter->pdev;
1533 if (!rx_ring->rx_buffer_info)
1536 /* Free all the Rx ring sk_buffs */
1537 for (i = 0; i < rx_ring->count; i++) {
1538 struct ixgbevf_rx_buffer *rx_buffer_info;
1540 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1541 if (rx_buffer_info->dma) {
1542 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1543 rx_ring->rx_buf_len,
1545 rx_buffer_info->dma = 0;
1547 if (rx_buffer_info->skb) {
1548 struct sk_buff *skb = rx_buffer_info->skb;
1549 rx_buffer_info->skb = NULL;
1551 struct sk_buff *this = skb;
1552 skb = IXGBE_CB(skb)->prev;
1553 dev_kfree_skb(this);
1558 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1559 memset(rx_ring->rx_buffer_info, 0, size);
1561 /* Zero out the descriptor ring */
1562 memset(rx_ring->desc, 0, rx_ring->size);
1564 rx_ring->next_to_clean = 0;
1565 rx_ring->next_to_use = 0;
1568 writel(0, adapter->hw.hw_addr + rx_ring->head);
1570 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1574 * ixgbevf_clean_tx_ring - Free Tx Buffers
1575 * @adapter: board private structure
1576 * @tx_ring: ring to be cleaned
1578 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1579 struct ixgbevf_ring *tx_ring)
1581 struct ixgbevf_tx_buffer *tx_buffer_info;
1585 if (!tx_ring->tx_buffer_info)
1588 /* Free all the Tx ring sk_buffs */
1589 for (i = 0; i < tx_ring->count; i++) {
1590 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1591 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
1594 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1595 memset(tx_ring->tx_buffer_info, 0, size);
1597 memset(tx_ring->desc, 0, tx_ring->size);
1599 tx_ring->next_to_use = 0;
1600 tx_ring->next_to_clean = 0;
1603 writel(0, adapter->hw.hw_addr + tx_ring->head);
1605 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1609 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1610 * @adapter: board private structure
1612 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1616 for (i = 0; i < adapter->num_rx_queues; i++)
1617 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1621 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1622 * @adapter: board private structure
1624 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1628 for (i = 0; i < adapter->num_tx_queues; i++)
1629 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1632 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1634 struct net_device *netdev = adapter->netdev;
1635 struct ixgbe_hw *hw = &adapter->hw;
1639 /* signal that we are down to the interrupt handler */
1640 set_bit(__IXGBEVF_DOWN, &adapter->state);
1641 /* disable receives */
1643 netif_tx_disable(netdev);
1647 netif_tx_stop_all_queues(netdev);
1649 ixgbevf_irq_disable(adapter);
1651 ixgbevf_napi_disable_all(adapter);
1653 del_timer_sync(&adapter->watchdog_timer);
1654 /* can't call flush scheduled work here because it can deadlock
1655 * if linkwatch_event tries to acquire the rtnl_lock which we are
1657 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1660 /* disable transmits in the hardware now that interrupts are off */
1661 for (i = 0; i < adapter->num_tx_queues; i++) {
1662 j = adapter->tx_ring[i].reg_idx;
1663 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1664 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1665 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1668 netif_carrier_off(netdev);
1670 if (!pci_channel_offline(adapter->pdev))
1671 ixgbevf_reset(adapter);
1673 ixgbevf_clean_all_tx_rings(adapter);
1674 ixgbevf_clean_all_rx_rings(adapter);
1677 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1679 WARN_ON(in_interrupt());
1681 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1684 ixgbevf_down(adapter);
1685 ixgbevf_up(adapter);
1687 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1690 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1692 struct ixgbe_hw *hw = &adapter->hw;
1693 struct net_device *netdev = adapter->netdev;
1695 if (hw->mac.ops.reset_hw(hw))
1696 hw_dbg(hw, "PF still resetting\n");
1698 hw->mac.ops.init_hw(hw);
1700 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1701 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1703 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1708 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1712 int vector_threshold;
1714 /* We'll want at least 2 (vector_threshold):
1715 * 1) TxQ[0] + RxQ[0] handler
1716 * 2) Other (Link Status Change, etc.)
1718 vector_threshold = MIN_MSIX_COUNT;
1720 /* The more we get, the more we will assign to Tx/Rx Cleanup
1721 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1722 * Right now, we simply care about how many we'll get; we'll
1723 * set them up later while requesting irq's.
1725 while (vectors >= vector_threshold) {
1726 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1728 if (!err || err < 0) /* Success or a nasty failure. */
1730 else /* err == number of vectors we should try again with */
1734 if (vectors < vector_threshold)
1738 dev_err(&adapter->pdev->dev,
1739 "Unable to allocate MSI-X interrupts\n");
1740 kfree(adapter->msix_entries);
1741 adapter->msix_entries = NULL;
1744 * Adjust for only the vectors we'll use, which is minimum
1745 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1746 * vectors we were allocated.
1748 adapter->num_msix_vectors = vectors;
1755 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
1756 * @adapter: board private structure to initialize
1758 * This is the top level queue allocation routine. The order here is very
1759 * important, starting with the "most" number of features turned on at once,
1760 * and ending with the smallest set of features. This way large combinations
1761 * can be allocated if they're turned on, and smaller combinations are the
1762 * fallthrough conditions.
1765 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1767 /* Start with base case */
1768 adapter->num_rx_queues = 1;
1769 adapter->num_tx_queues = 1;
1773 * ixgbevf_alloc_queues - Allocate memory for all rings
1774 * @adapter: board private structure to initialize
1776 * We allocate one ring per queue at run-time since we don't know the
1777 * number of queues at compile-time. The polling_netdev array is
1778 * intended for Multiqueue, but should work fine with a single queue.
1780 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1784 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1785 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1786 if (!adapter->tx_ring)
1787 goto err_tx_ring_allocation;
1789 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1790 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1791 if (!adapter->rx_ring)
1792 goto err_rx_ring_allocation;
1794 for (i = 0; i < adapter->num_tx_queues; i++) {
1795 adapter->tx_ring[i].count = adapter->tx_ring_count;
1796 adapter->tx_ring[i].queue_index = i;
1797 /* reg_idx may be remapped later by DCB config */
1798 adapter->tx_ring[i].reg_idx = i;
1799 adapter->tx_ring[i].dev = &adapter->pdev->dev;
1800 adapter->tx_ring[i].netdev = adapter->netdev;
1803 for (i = 0; i < adapter->num_rx_queues; i++) {
1804 adapter->rx_ring[i].count = adapter->rx_ring_count;
1805 adapter->rx_ring[i].queue_index = i;
1806 adapter->rx_ring[i].reg_idx = i;
1807 adapter->rx_ring[i].dev = &adapter->pdev->dev;
1808 adapter->rx_ring[i].netdev = adapter->netdev;
1813 err_rx_ring_allocation:
1814 kfree(adapter->tx_ring);
1815 err_tx_ring_allocation:
1820 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1821 * @adapter: board private structure to initialize
1823 * Attempt to configure the interrupts using the best available
1824 * capabilities of the hardware and the kernel.
1826 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1828 struct net_device *netdev = adapter->netdev;
1830 int vector, v_budget;
1833 * It's easy to be greedy for MSI-X vectors, but it really
1834 * doesn't do us much good if we have a lot more vectors
1835 * than CPU's. So let's be conservative and only ask for
1836 * (roughly) the same number of vectors as there are CPU's.
1837 * The default is to use pairs of vectors.
1839 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
1840 v_budget = min_t(int, v_budget, num_online_cpus());
1841 v_budget += NON_Q_VECTORS;
1843 /* A failure in MSI-X entry allocation isn't fatal, but it does
1844 * mean we disable MSI-X capabilities of the adapter. */
1845 adapter->msix_entries = kcalloc(v_budget,
1846 sizeof(struct msix_entry), GFP_KERNEL);
1847 if (!adapter->msix_entries) {
1852 for (vector = 0; vector < v_budget; vector++)
1853 adapter->msix_entries[vector].entry = vector;
1855 err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
1859 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
1863 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
1870 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1871 * @adapter: board private structure to initialize
1873 * We allocate one q_vector per queue interrupt. If allocation fails we
1876 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1878 int q_idx, num_q_vectors;
1879 struct ixgbevf_q_vector *q_vector;
1881 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1883 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1884 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1887 q_vector->adapter = adapter;
1888 q_vector->v_idx = q_idx;
1889 netif_napi_add(adapter->netdev, &q_vector->napi,
1891 adapter->q_vector[q_idx] = q_vector;
1899 q_vector = adapter->q_vector[q_idx];
1900 netif_napi_del(&q_vector->napi);
1902 adapter->q_vector[q_idx] = NULL;
1908 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1909 * @adapter: board private structure to initialize
1911 * This function frees the memory allocated to the q_vectors. In addition if
1912 * NAPI is enabled it will delete any references to the NAPI struct prior
1913 * to freeing the q_vector.
1915 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1917 int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1919 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1920 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1922 adapter->q_vector[q_idx] = NULL;
1923 netif_napi_del(&q_vector->napi);
1929 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1930 * @adapter: board private structure
1933 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1935 pci_disable_msix(adapter->pdev);
1936 kfree(adapter->msix_entries);
1937 adapter->msix_entries = NULL;
1941 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1942 * @adapter: board private structure to initialize
1945 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
1949 /* Number of supported queues */
1950 ixgbevf_set_num_queues(adapter);
1952 err = ixgbevf_set_interrupt_capability(adapter);
1954 hw_dbg(&adapter->hw,
1955 "Unable to setup interrupt capabilities\n");
1956 goto err_set_interrupt;
1959 err = ixgbevf_alloc_q_vectors(adapter);
1961 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
1963 goto err_alloc_q_vectors;
1966 err = ixgbevf_alloc_queues(adapter);
1968 pr_err("Unable to allocate memory for queues\n");
1969 goto err_alloc_queues;
1972 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
1973 "Tx Queue count = %u\n",
1974 (adapter->num_rx_queues > 1) ? "Enabled" :
1975 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
1977 set_bit(__IXGBEVF_DOWN, &adapter->state);
1981 ixgbevf_free_q_vectors(adapter);
1982 err_alloc_q_vectors:
1983 ixgbevf_reset_interrupt_capability(adapter);
1989 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
1990 * @adapter: board private structure to clear interrupt scheme on
1992 * We go through and clear interrupt specific resources and reset the structure
1993 * to pre-load conditions
1995 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
1997 adapter->num_tx_queues = 0;
1998 adapter->num_rx_queues = 0;
2000 ixgbevf_free_q_vectors(adapter);
2001 ixgbevf_reset_interrupt_capability(adapter);
2005 * ixgbevf_sw_init - Initialize general software structures
2006 * (struct ixgbevf_adapter)
2007 * @adapter: board private structure to initialize
2009 * ixgbevf_sw_init initializes the Adapter private data structure.
2010 * Fields are initialized based on PCI device information and
2011 * OS network device settings (MTU size).
2013 static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2015 struct ixgbe_hw *hw = &adapter->hw;
2016 struct pci_dev *pdev = adapter->pdev;
2019 /* PCI config space info */
2021 hw->vendor_id = pdev->vendor;
2022 hw->device_id = pdev->device;
2023 hw->revision_id = pdev->revision;
2024 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2025 hw->subsystem_device_id = pdev->subsystem_device;
2027 hw->mbx.ops.init_params(hw);
2029 /* assume legacy case in which PF would only give VF 2 queues */
2030 hw->mac.max_tx_queues = 2;
2031 hw->mac.max_rx_queues = 2;
2033 err = hw->mac.ops.reset_hw(hw);
2035 dev_info(&pdev->dev,
2036 "PF still in reset state, assigning new address\n");
2037 eth_hw_addr_random(adapter->netdev);
2038 memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
2039 adapter->netdev->addr_len);
2041 err = hw->mac.ops.init_hw(hw);
2043 pr_err("init_shared_code failed: %d\n", err);
2046 memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
2047 adapter->netdev->addr_len);
2050 /* lock to protect mailbox accesses */
2051 spin_lock_init(&adapter->mbx_lock);
2053 /* Enable dynamic interrupt throttling rates */
2054 adapter->rx_itr_setting = 1;
2055 adapter->tx_itr_setting = 1;
2057 /* set default ring sizes */
2058 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2059 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2061 set_bit(__IXGBEVF_DOWN, &adapter->state);
2068 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2070 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2071 if (current_counter < last_counter) \
2072 counter += 0x100000000LL; \
2073 last_counter = current_counter; \
2074 counter &= 0xFFFFFFFF00000000LL; \
2075 counter |= current_counter; \
2078 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2080 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2081 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2082 u64 current_counter = (current_counter_msb << 32) | \
2083 current_counter_lsb; \
2084 if (current_counter < last_counter) \
2085 counter += 0x1000000000LL; \
2086 last_counter = current_counter; \
2087 counter &= 0xFFFFFFF000000000LL; \
2088 counter |= current_counter; \
2091 * ixgbevf_update_stats - Update the board statistics counters.
2092 * @adapter: board private structure
2094 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2096 struct ixgbe_hw *hw = &adapter->hw;
2098 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2099 adapter->stats.vfgprc);
2100 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2101 adapter->stats.vfgptc);
2102 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2103 adapter->stats.last_vfgorc,
2104 adapter->stats.vfgorc);
2105 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2106 adapter->stats.last_vfgotc,
2107 adapter->stats.vfgotc);
2108 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2109 adapter->stats.vfmprc);
2113 * ixgbevf_watchdog - Timer Call-back
2114 * @data: pointer to adapter cast into an unsigned long
2116 static void ixgbevf_watchdog(unsigned long data)
2118 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2119 struct ixgbe_hw *hw = &adapter->hw;
2124 * Do the watchdog outside of interrupt context due to the lovely
2125 * delays that some of the newer hardware requires
2128 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2129 goto watchdog_short_circuit;
2131 /* get one bit for every active tx/rx interrupt vector */
2132 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2133 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2134 if (qv->rx.ring || qv->tx.ring)
2138 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
2140 watchdog_short_circuit:
2141 schedule_work(&adapter->watchdog_task);
2145 * ixgbevf_tx_timeout - Respond to a Tx Hang
2146 * @netdev: network interface device structure
2148 static void ixgbevf_tx_timeout(struct net_device *netdev)
2150 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2152 /* Do the reset outside of interrupt context */
2153 schedule_work(&adapter->reset_task);
2156 static void ixgbevf_reset_task(struct work_struct *work)
2158 struct ixgbevf_adapter *adapter;
2159 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2161 /* If we're already down or resetting, just bail */
2162 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2163 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2166 adapter->tx_timeout_count++;
2168 ixgbevf_reinit_locked(adapter);
2172 * ixgbevf_watchdog_task - worker thread to bring link up
2173 * @work: pointer to work_struct containing our data
2175 static void ixgbevf_watchdog_task(struct work_struct *work)
2177 struct ixgbevf_adapter *adapter = container_of(work,
2178 struct ixgbevf_adapter,
2180 struct net_device *netdev = adapter->netdev;
2181 struct ixgbe_hw *hw = &adapter->hw;
2182 u32 link_speed = adapter->link_speed;
2183 bool link_up = adapter->link_up;
2186 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2189 * Always check the link on the watchdog because we have
2192 spin_lock_bh(&adapter->mbx_lock);
2194 need_reset = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
2196 spin_unlock_bh(&adapter->mbx_lock);
2199 adapter->link_up = link_up;
2200 adapter->link_speed = link_speed;
2201 netif_carrier_off(netdev);
2202 netif_tx_stop_all_queues(netdev);
2203 schedule_work(&adapter->reset_task);
2206 adapter->link_up = link_up;
2207 adapter->link_speed = link_speed;
2210 if (!netif_carrier_ok(netdev)) {
2211 hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
2212 (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2214 netif_carrier_on(netdev);
2215 netif_tx_wake_all_queues(netdev);
2218 adapter->link_up = false;
2219 adapter->link_speed = 0;
2220 if (netif_carrier_ok(netdev)) {
2221 hw_dbg(&adapter->hw, "NIC Link is Down\n");
2222 netif_carrier_off(netdev);
2223 netif_tx_stop_all_queues(netdev);
2227 ixgbevf_update_stats(adapter);
2230 /* Reset the timer */
2231 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2232 mod_timer(&adapter->watchdog_timer,
2233 round_jiffies(jiffies + (2 * HZ)));
2235 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2239 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2240 * @adapter: board private structure
2241 * @tx_ring: Tx descriptor ring for a specific queue
2243 * Free all transmit software resources
2245 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2246 struct ixgbevf_ring *tx_ring)
2248 struct pci_dev *pdev = adapter->pdev;
2250 ixgbevf_clean_tx_ring(adapter, tx_ring);
2252 vfree(tx_ring->tx_buffer_info);
2253 tx_ring->tx_buffer_info = NULL;
2255 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2258 tx_ring->desc = NULL;
2262 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2263 * @adapter: board private structure
2265 * Free all transmit software resources
2267 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2271 for (i = 0; i < adapter->num_tx_queues; i++)
2272 if (adapter->tx_ring[i].desc)
2273 ixgbevf_free_tx_resources(adapter,
2274 &adapter->tx_ring[i]);
2279 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2280 * @adapter: board private structure
2281 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2283 * Return 0 on success, negative on failure
2285 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2286 struct ixgbevf_ring *tx_ring)
2288 struct pci_dev *pdev = adapter->pdev;
2291 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2292 tx_ring->tx_buffer_info = vzalloc(size);
2293 if (!tx_ring->tx_buffer_info)
2296 /* round up to nearest 4K */
2297 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2298 tx_ring->size = ALIGN(tx_ring->size, 4096);
2300 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2301 &tx_ring->dma, GFP_KERNEL);
2305 tx_ring->next_to_use = 0;
2306 tx_ring->next_to_clean = 0;
2310 vfree(tx_ring->tx_buffer_info);
2311 tx_ring->tx_buffer_info = NULL;
2312 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2313 "descriptor ring\n");
2318 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2319 * @adapter: board private structure
2321 * If this function returns with an error, then it's possible one or
2322 * more of the rings is populated (while the rest are not). It is the
2323 * callers duty to clean those orphaned rings.
2325 * Return 0 on success, negative on failure
2327 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2331 for (i = 0; i < adapter->num_tx_queues; i++) {
2332 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2335 hw_dbg(&adapter->hw,
2336 "Allocation for Tx Queue %u failed\n", i);
2344 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2345 * @adapter: board private structure
2346 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2348 * Returns 0 on success, negative on failure
2350 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2351 struct ixgbevf_ring *rx_ring)
2353 struct pci_dev *pdev = adapter->pdev;
2356 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2357 rx_ring->rx_buffer_info = vzalloc(size);
2358 if (!rx_ring->rx_buffer_info)
2361 /* Round up to nearest 4K */
2362 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2363 rx_ring->size = ALIGN(rx_ring->size, 4096);
2365 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2366 &rx_ring->dma, GFP_KERNEL);
2368 if (!rx_ring->desc) {
2369 hw_dbg(&adapter->hw,
2370 "Unable to allocate memory for "
2371 "the receive descriptor ring\n");
2372 vfree(rx_ring->rx_buffer_info);
2373 rx_ring->rx_buffer_info = NULL;
2377 rx_ring->next_to_clean = 0;
2378 rx_ring->next_to_use = 0;
2386 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2387 * @adapter: board private structure
2389 * If this function returns with an error, then it's possible one or
2390 * more of the rings is populated (while the rest are not). It is the
2391 * callers duty to clean those orphaned rings.
2393 * Return 0 on success, negative on failure
2395 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2399 for (i = 0; i < adapter->num_rx_queues; i++) {
2400 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2403 hw_dbg(&adapter->hw,
2404 "Allocation for Rx Queue %u failed\n", i);
2411 * ixgbevf_free_rx_resources - Free Rx Resources
2412 * @adapter: board private structure
2413 * @rx_ring: ring to clean the resources from
2415 * Free all receive software resources
2417 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2418 struct ixgbevf_ring *rx_ring)
2420 struct pci_dev *pdev = adapter->pdev;
2422 ixgbevf_clean_rx_ring(adapter, rx_ring);
2424 vfree(rx_ring->rx_buffer_info);
2425 rx_ring->rx_buffer_info = NULL;
2427 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2430 rx_ring->desc = NULL;
2434 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2435 * @adapter: board private structure
2437 * Free all receive software resources
2439 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2443 for (i = 0; i < adapter->num_rx_queues; i++)
2444 if (adapter->rx_ring[i].desc)
2445 ixgbevf_free_rx_resources(adapter,
2446 &adapter->rx_ring[i]);
2449 static int ixgbevf_setup_queues(struct ixgbevf_adapter *adapter)
2451 struct ixgbe_hw *hw = &adapter->hw;
2452 struct ixgbevf_ring *rx_ring;
2453 unsigned int def_q = 0;
2454 unsigned int num_tcs = 0;
2455 unsigned int num_rx_queues = 1;
2458 spin_lock_bh(&adapter->mbx_lock);
2460 /* fetch queue configuration from the PF */
2461 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2463 spin_unlock_bh(&adapter->mbx_lock);
2469 /* update default Tx ring register index */
2470 adapter->tx_ring[0].reg_idx = def_q;
2472 /* we need as many queues as traffic classes */
2473 num_rx_queues = num_tcs;
2476 /* nothing to do if we have the correct number of queues */
2477 if (adapter->num_rx_queues == num_rx_queues)
2480 /* allocate new rings */
2481 rx_ring = kcalloc(num_rx_queues,
2482 sizeof(struct ixgbevf_ring), GFP_KERNEL);
2486 /* setup ring fields */
2487 for (i = 0; i < num_rx_queues; i++) {
2488 rx_ring[i].count = adapter->rx_ring_count;
2489 rx_ring[i].queue_index = i;
2490 rx_ring[i].reg_idx = i;
2491 rx_ring[i].dev = &adapter->pdev->dev;
2492 rx_ring[i].netdev = adapter->netdev;
2495 /* free the existing ring and queues */
2496 adapter->num_rx_queues = 0;
2497 kfree(adapter->rx_ring);
2499 /* move new rings into position on the adapter struct */
2500 adapter->rx_ring = rx_ring;
2501 adapter->num_rx_queues = num_rx_queues;
2507 * ixgbevf_open - Called when a network interface is made active
2508 * @netdev: network interface device structure
2510 * Returns 0 on success, negative value on failure
2512 * The open entry point is called when a network interface is made
2513 * active by the system (IFF_UP). At this point all resources needed
2514 * for transmit and receive operations are allocated, the interrupt
2515 * handler is registered with the OS, the watchdog timer is started,
2516 * and the stack is notified that the interface is ready.
2518 static int ixgbevf_open(struct net_device *netdev)
2520 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2521 struct ixgbe_hw *hw = &adapter->hw;
2524 /* disallow open during test */
2525 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2528 if (hw->adapter_stopped) {
2529 ixgbevf_reset(adapter);
2530 /* if adapter is still stopped then PF isn't up and
2531 * the vf can't start. */
2532 if (hw->adapter_stopped) {
2533 err = IXGBE_ERR_MBX;
2534 pr_err("Unable to start - perhaps the PF Driver isn't "
2536 goto err_setup_reset;
2540 ixgbevf_negotiate_api(adapter);
2542 /* setup queue reg_idx and Rx queue count */
2543 err = ixgbevf_setup_queues(adapter);
2545 goto err_setup_queues;
2547 /* allocate transmit descriptors */
2548 err = ixgbevf_setup_all_tx_resources(adapter);
2552 /* allocate receive descriptors */
2553 err = ixgbevf_setup_all_rx_resources(adapter);
2557 ixgbevf_configure(adapter);
2560 * Map the Tx/Rx rings to the vectors we were allotted.
2561 * if request_irq will be called in this function map_rings
2562 * must be called *before* up_complete
2564 ixgbevf_map_rings_to_vectors(adapter);
2566 ixgbevf_up_complete(adapter);
2568 /* clear any pending interrupts, may auto mask */
2569 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2570 err = ixgbevf_request_irq(adapter);
2574 ixgbevf_irq_enable(adapter);
2579 ixgbevf_down(adapter);
2580 ixgbevf_free_irq(adapter);
2582 ixgbevf_free_all_rx_resources(adapter);
2584 ixgbevf_free_all_tx_resources(adapter);
2586 ixgbevf_reset(adapter);
2594 * ixgbevf_close - Disables a network interface
2595 * @netdev: network interface device structure
2597 * Returns 0, this is not allowed to fail
2599 * The close entry point is called when an interface is de-activated
2600 * by the OS. The hardware is still under the drivers control, but
2601 * needs to be disabled. A global MAC reset is issued to stop the
2602 * hardware, and all transmit and receive resources are freed.
2604 static int ixgbevf_close(struct net_device *netdev)
2606 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2608 ixgbevf_down(adapter);
2609 ixgbevf_free_irq(adapter);
2611 ixgbevf_free_all_tx_resources(adapter);
2612 ixgbevf_free_all_rx_resources(adapter);
2617 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2618 u32 vlan_macip_lens, u32 type_tucmd,
2621 struct ixgbe_adv_tx_context_desc *context_desc;
2622 u16 i = tx_ring->next_to_use;
2624 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2627 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2629 /* set bits to identify this as an advanced context descriptor */
2630 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2632 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2633 context_desc->seqnum_seed = 0;
2634 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2635 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2638 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
2639 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2641 u32 vlan_macip_lens, type_tucmd;
2642 u32 mss_l4len_idx, l4len;
2644 if (!skb_is_gso(skb))
2647 if (skb_header_cloned(skb)) {
2648 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2653 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2654 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2656 if (skb->protocol == htons(ETH_P_IP)) {
2657 struct iphdr *iph = ip_hdr(skb);
2660 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2664 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2665 } else if (skb_is_gso_v6(skb)) {
2666 ipv6_hdr(skb)->payload_len = 0;
2667 tcp_hdr(skb)->check =
2668 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2669 &ipv6_hdr(skb)->daddr,
2673 /* compute header lengths */
2674 l4len = tcp_hdrlen(skb);
2676 *hdr_len = skb_transport_offset(skb) + l4len;
2678 /* mss_l4len_id: use 1 as index for TSO */
2679 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2680 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2681 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2683 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2684 vlan_macip_lens = skb_network_header_len(skb);
2685 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2686 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2688 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2689 type_tucmd, mss_l4len_idx);
2694 static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2695 struct sk_buff *skb, u32 tx_flags)
2697 u32 vlan_macip_lens = 0;
2698 u32 mss_l4len_idx = 0;
2701 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2703 switch (skb->protocol) {
2704 case __constant_htons(ETH_P_IP):
2705 vlan_macip_lens |= skb_network_header_len(skb);
2706 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2707 l4_hdr = ip_hdr(skb)->protocol;
2709 case __constant_htons(ETH_P_IPV6):
2710 vlan_macip_lens |= skb_network_header_len(skb);
2711 l4_hdr = ipv6_hdr(skb)->nexthdr;
2714 if (unlikely(net_ratelimit())) {
2715 dev_warn(tx_ring->dev,
2716 "partial checksum but proto=%x!\n",
2724 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2725 mss_l4len_idx = tcp_hdrlen(skb) <<
2726 IXGBE_ADVTXD_L4LEN_SHIFT;
2729 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2730 mss_l4len_idx = sizeof(struct sctphdr) <<
2731 IXGBE_ADVTXD_L4LEN_SHIFT;
2734 mss_l4len_idx = sizeof(struct udphdr) <<
2735 IXGBE_ADVTXD_L4LEN_SHIFT;
2738 if (unlikely(net_ratelimit())) {
2739 dev_warn(tx_ring->dev,
2740 "partial checksum but l4 proto=%x!\n",
2747 /* vlan_macip_lens: MACLEN, VLAN tag */
2748 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2749 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2751 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2752 type_tucmd, mss_l4len_idx);
2754 return (skb->ip_summed == CHECKSUM_PARTIAL);
2757 static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
2758 struct sk_buff *skb, u32 tx_flags,
2761 struct ixgbevf_tx_buffer *tx_buffer_info;
2763 unsigned int total = skb->len;
2764 unsigned int offset = 0, size;
2766 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2770 i = tx_ring->next_to_use;
2772 len = min(skb_headlen(skb), total);
2774 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2775 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2777 tx_buffer_info->length = size;
2778 tx_buffer_info->mapped_as_page = false;
2779 tx_buffer_info->dma = dma_map_single(tx_ring->dev,
2781 size, DMA_TO_DEVICE);
2782 if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
2784 tx_buffer_info->next_to_watch = i;
2791 if (i == tx_ring->count)
2795 for (f = 0; f < nr_frags; f++) {
2796 const struct skb_frag_struct *frag;
2798 frag = &skb_shinfo(skb)->frags[f];
2799 len = min((unsigned int)skb_frag_size(frag), total);
2803 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2804 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2806 tx_buffer_info->length = size;
2807 tx_buffer_info->dma =
2808 skb_frag_dma_map(tx_ring->dev, frag,
2809 offset, size, DMA_TO_DEVICE);
2810 if (dma_mapping_error(tx_ring->dev,
2811 tx_buffer_info->dma))
2813 tx_buffer_info->mapped_as_page = true;
2814 tx_buffer_info->next_to_watch = i;
2821 if (i == tx_ring->count)
2829 i = tx_ring->count - 1;
2832 tx_ring->tx_buffer_info[i].skb = skb;
2833 tx_ring->tx_buffer_info[first].next_to_watch = i;
2834 tx_ring->tx_buffer_info[first].time_stamp = jiffies;
2839 dev_err(tx_ring->dev, "TX DMA map failed\n");
2841 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2842 tx_buffer_info->dma = 0;
2843 tx_buffer_info->next_to_watch = 0;
2846 /* clear timestamp and dma mappings for remaining portion of packet */
2847 while (count >= 0) {
2851 i += tx_ring->count;
2852 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2853 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
2859 static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
2860 int count, u32 paylen, u8 hdr_len)
2862 union ixgbe_adv_tx_desc *tx_desc = NULL;
2863 struct ixgbevf_tx_buffer *tx_buffer_info;
2864 u32 olinfo_status = 0, cmd_type_len = 0;
2867 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2869 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2871 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2873 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2874 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2876 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2877 olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
2879 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2880 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2882 /* use index 1 context for tso */
2883 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2884 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2885 olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
2889 * Check Context must be set if Tx switch is enabled, which it
2890 * always is for case where virtual functions are running
2892 olinfo_status |= IXGBE_ADVTXD_CC;
2894 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2896 i = tx_ring->next_to_use;
2898 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2899 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2900 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2901 tx_desc->read.cmd_type_len =
2902 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2903 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2905 if (i == tx_ring->count)
2909 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2911 tx_ring->next_to_use = i;
2914 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2916 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
2918 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
2919 /* Herbert's original patch had:
2920 * smp_mb__after_netif_stop_queue();
2921 * but since that doesn't exist yet, just open code it. */
2924 /* We need to check again in a case another CPU has just
2925 * made room available. */
2926 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
2929 /* A reprieve! - use start_queue because it doesn't call schedule */
2930 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2931 ++adapter->restart_queue;
2935 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2937 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
2939 return __ixgbevf_maybe_stop_tx(tx_ring, size);
2942 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2944 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2945 struct ixgbevf_ring *tx_ring;
2947 unsigned int tx_flags = 0;
2950 u16 count = TXD_USE_COUNT(skb_headlen(skb));
2951 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2954 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
2955 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
2957 return NETDEV_TX_OK;
2960 tx_ring = &adapter->tx_ring[r_idx];
2963 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
2964 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
2965 * + 2 desc gap to keep tail from touching head,
2966 * + 1 desc for context descriptor,
2967 * otherwise try next time
2969 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2970 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2971 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
2973 count += skb_shinfo(skb)->nr_frags;
2975 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
2977 return NETDEV_TX_BUSY;
2980 if (vlan_tx_tag_present(skb)) {
2981 tx_flags |= vlan_tx_tag_get(skb);
2982 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
2983 tx_flags |= IXGBE_TX_FLAGS_VLAN;
2986 first = tx_ring->next_to_use;
2988 if (skb->protocol == htons(ETH_P_IP))
2989 tx_flags |= IXGBE_TX_FLAGS_IPV4;
2990 tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
2992 dev_kfree_skb_any(skb);
2993 return NETDEV_TX_OK;
2997 tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
2998 else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
2999 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3001 ixgbevf_tx_queue(tx_ring, tx_flags,
3002 ixgbevf_tx_map(tx_ring, skb, tx_flags, first),
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 writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
3014 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
3016 return NETDEV_TX_OK;
3020 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3021 * @netdev: network interface device structure
3022 * @p: pointer to an address structure
3024 * Returns 0 on success, negative on failure
3026 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3028 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3029 struct ixgbe_hw *hw = &adapter->hw;
3030 struct sockaddr *addr = p;
3032 if (!is_valid_ether_addr(addr->sa_data))
3033 return -EADDRNOTAVAIL;
3035 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3036 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3038 spin_lock_bh(&adapter->mbx_lock);
3040 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
3042 spin_unlock_bh(&adapter->mbx_lock);
3048 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3049 * @netdev: network interface device structure
3050 * @new_mtu: new value for maximum frame size
3052 * Returns 0 on success, negative on failure
3054 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3056 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3057 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3058 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
3060 switch (adapter->hw.api_version) {
3061 case ixgbe_mbox_api_11:
3062 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3065 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3066 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3070 /* MTU < 68 is an error and causes problems on some kernels */
3071 if ((new_mtu < 68) || (max_frame > max_possible_frame))
3074 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3075 netdev->mtu, new_mtu);
3076 /* must set new MTU before calling down or up */
3077 netdev->mtu = new_mtu;
3079 if (netif_running(netdev))
3080 ixgbevf_reinit_locked(adapter);
3085 static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
3087 struct net_device *netdev = pci_get_drvdata(pdev);
3088 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3093 netif_device_detach(netdev);
3095 if (netif_running(netdev)) {
3097 ixgbevf_down(adapter);
3098 ixgbevf_free_irq(adapter);
3099 ixgbevf_free_all_tx_resources(adapter);
3100 ixgbevf_free_all_rx_resources(adapter);
3104 ixgbevf_clear_interrupt_scheme(adapter);
3107 retval = pci_save_state(pdev);
3112 pci_disable_device(pdev);
3118 static int ixgbevf_resume(struct pci_dev *pdev)
3120 struct ixgbevf_adapter *adapter = pci_get_drvdata(pdev);
3121 struct net_device *netdev = adapter->netdev;
3124 pci_set_power_state(pdev, PCI_D0);
3125 pci_restore_state(pdev);
3127 * pci_restore_state clears dev->state_saved so call
3128 * pci_save_state to restore it.
3130 pci_save_state(pdev);
3132 err = pci_enable_device_mem(pdev);
3134 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3137 pci_set_master(pdev);
3140 err = ixgbevf_init_interrupt_scheme(adapter);
3143 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3147 ixgbevf_reset(adapter);
3149 if (netif_running(netdev)) {
3150 err = ixgbevf_open(netdev);
3155 netif_device_attach(netdev);
3160 #endif /* CONFIG_PM */
3161 static void ixgbevf_shutdown(struct pci_dev *pdev)
3163 ixgbevf_suspend(pdev, PMSG_SUSPEND);
3166 static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3167 struct rtnl_link_stats64 *stats)
3169 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3172 const struct ixgbevf_ring *ring;
3175 ixgbevf_update_stats(adapter);
3177 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3179 for (i = 0; i < adapter->num_rx_queues; i++) {
3180 ring = &adapter->rx_ring[i];
3182 start = u64_stats_fetch_begin_bh(&ring->syncp);
3183 bytes = ring->total_bytes;
3184 packets = ring->total_packets;
3185 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3186 stats->rx_bytes += bytes;
3187 stats->rx_packets += packets;
3190 for (i = 0; i < adapter->num_tx_queues; i++) {
3191 ring = &adapter->tx_ring[i];
3193 start = u64_stats_fetch_begin_bh(&ring->syncp);
3194 bytes = ring->total_bytes;
3195 packets = ring->total_packets;
3196 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3197 stats->tx_bytes += bytes;
3198 stats->tx_packets += packets;
3204 static const struct net_device_ops ixgbevf_netdev_ops = {
3205 .ndo_open = ixgbevf_open,
3206 .ndo_stop = ixgbevf_close,
3207 .ndo_start_xmit = ixgbevf_xmit_frame,
3208 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
3209 .ndo_get_stats64 = ixgbevf_get_stats,
3210 .ndo_validate_addr = eth_validate_addr,
3211 .ndo_set_mac_address = ixgbevf_set_mac,
3212 .ndo_change_mtu = ixgbevf_change_mtu,
3213 .ndo_tx_timeout = ixgbevf_tx_timeout,
3214 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3215 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
3218 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3220 dev->netdev_ops = &ixgbevf_netdev_ops;
3221 ixgbevf_set_ethtool_ops(dev);
3222 dev->watchdog_timeo = 5 * HZ;
3226 * ixgbevf_probe - Device Initialization Routine
3227 * @pdev: PCI device information struct
3228 * @ent: entry in ixgbevf_pci_tbl
3230 * Returns 0 on success, negative on failure
3232 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3233 * The OS initialization, configuring of the adapter private structure,
3234 * and a hardware reset occur.
3236 static int __devinit ixgbevf_probe(struct pci_dev *pdev,
3237 const struct pci_device_id *ent)
3239 struct net_device *netdev;
3240 struct ixgbevf_adapter *adapter = NULL;
3241 struct ixgbe_hw *hw = NULL;
3242 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3243 static int cards_found;
3244 int err, pci_using_dac;
3246 err = pci_enable_device(pdev);
3250 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3251 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3254 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3256 err = dma_set_coherent_mask(&pdev->dev,
3259 dev_err(&pdev->dev, "No usable DMA "
3260 "configuration, aborting\n");
3267 err = pci_request_regions(pdev, ixgbevf_driver_name);
3269 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3273 pci_set_master(pdev);
3275 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3279 goto err_alloc_etherdev;
3282 SET_NETDEV_DEV(netdev, &pdev->dev);
3284 pci_set_drvdata(pdev, netdev);
3285 adapter = netdev_priv(netdev);
3287 adapter->netdev = netdev;
3288 adapter->pdev = pdev;
3291 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3294 * call save state here in standalone driver because it relies on
3295 * adapter struct to exist, and needs to call netdev_priv
3297 pci_save_state(pdev);
3299 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3300 pci_resource_len(pdev, 0));
3306 ixgbevf_assign_netdev_ops(netdev);
3308 adapter->bd_number = cards_found;
3311 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3312 hw->mac.type = ii->mac;
3314 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3315 sizeof(struct ixgbe_mbx_operations));
3317 /* setup the private structure */
3318 err = ixgbevf_sw_init(adapter);
3322 /* The HW MAC address was set and/or determined in sw_init */
3323 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3325 if (!is_valid_ether_addr(netdev->dev_addr)) {
3326 pr_err("invalid MAC address\n");
3331 netdev->hw_features = NETIF_F_SG |
3338 netdev->features = netdev->hw_features |
3339 NETIF_F_HW_VLAN_TX |
3340 NETIF_F_HW_VLAN_RX |
3341 NETIF_F_HW_VLAN_FILTER;
3343 netdev->vlan_features |= NETIF_F_TSO;
3344 netdev->vlan_features |= NETIF_F_TSO6;
3345 netdev->vlan_features |= NETIF_F_IP_CSUM;
3346 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3347 netdev->vlan_features |= NETIF_F_SG;
3350 netdev->features |= NETIF_F_HIGHDMA;
3352 netdev->priv_flags |= IFF_UNICAST_FLT;
3354 init_timer(&adapter->watchdog_timer);
3355 adapter->watchdog_timer.function = ixgbevf_watchdog;
3356 adapter->watchdog_timer.data = (unsigned long)adapter;
3358 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3359 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3361 err = ixgbevf_init_interrupt_scheme(adapter);
3365 strcpy(netdev->name, "eth%d");
3367 err = register_netdev(netdev);
3371 netif_carrier_off(netdev);
3373 ixgbevf_init_last_counter_stats(adapter);
3375 /* print the MAC address */
3376 hw_dbg(hw, "%pM\n", netdev->dev_addr);
3378 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3380 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3385 ixgbevf_clear_interrupt_scheme(adapter);
3387 ixgbevf_reset_interrupt_capability(adapter);
3388 iounmap(hw->hw_addr);
3390 free_netdev(netdev);
3392 pci_release_regions(pdev);
3395 pci_disable_device(pdev);
3400 * ixgbevf_remove - Device Removal Routine
3401 * @pdev: PCI device information struct
3403 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3404 * that it should release a PCI device. The could be caused by a
3405 * Hot-Plug event, or because the driver is going to be removed from
3408 static void __devexit ixgbevf_remove(struct pci_dev *pdev)
3410 struct net_device *netdev = pci_get_drvdata(pdev);
3411 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3413 set_bit(__IXGBEVF_DOWN, &adapter->state);
3415 del_timer_sync(&adapter->watchdog_timer);
3417 cancel_work_sync(&adapter->reset_task);
3418 cancel_work_sync(&adapter->watchdog_task);
3420 if (netdev->reg_state == NETREG_REGISTERED)
3421 unregister_netdev(netdev);
3423 ixgbevf_clear_interrupt_scheme(adapter);
3424 ixgbevf_reset_interrupt_capability(adapter);
3426 iounmap(adapter->hw.hw_addr);
3427 pci_release_regions(pdev);
3429 hw_dbg(&adapter->hw, "Remove complete\n");
3431 kfree(adapter->tx_ring);
3432 kfree(adapter->rx_ring);
3434 free_netdev(netdev);
3436 pci_disable_device(pdev);
3440 * ixgbevf_io_error_detected - called when PCI error is detected
3441 * @pdev: Pointer to PCI device
3442 * @state: The current pci connection state
3444 * This function is called after a PCI bus error affecting
3445 * this device has been detected.
3447 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3448 pci_channel_state_t state)
3450 struct net_device *netdev = pci_get_drvdata(pdev);
3451 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3453 netif_device_detach(netdev);
3455 if (state == pci_channel_io_perm_failure)
3456 return PCI_ERS_RESULT_DISCONNECT;
3458 if (netif_running(netdev))
3459 ixgbevf_down(adapter);
3461 pci_disable_device(pdev);
3463 /* Request a slot slot reset. */
3464 return PCI_ERS_RESULT_NEED_RESET;
3468 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3469 * @pdev: Pointer to PCI device
3471 * Restart the card from scratch, as if from a cold-boot. Implementation
3472 * resembles the first-half of the ixgbevf_resume routine.
3474 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3476 struct net_device *netdev = pci_get_drvdata(pdev);
3477 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3479 if (pci_enable_device_mem(pdev)) {
3481 "Cannot re-enable PCI device after reset.\n");
3482 return PCI_ERS_RESULT_DISCONNECT;
3485 pci_set_master(pdev);
3487 ixgbevf_reset(adapter);
3489 return PCI_ERS_RESULT_RECOVERED;
3493 * ixgbevf_io_resume - called when traffic can start flowing again.
3494 * @pdev: Pointer to PCI device
3496 * This callback is called when the error recovery driver tells us that
3497 * its OK to resume normal operation. Implementation resembles the
3498 * second-half of the ixgbevf_resume routine.
3500 static void ixgbevf_io_resume(struct pci_dev *pdev)
3502 struct net_device *netdev = pci_get_drvdata(pdev);
3503 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3505 if (netif_running(netdev))
3506 ixgbevf_up(adapter);
3508 netif_device_attach(netdev);
3511 /* PCI Error Recovery (ERS) */
3512 static const struct pci_error_handlers ixgbevf_err_handler = {
3513 .error_detected = ixgbevf_io_error_detected,
3514 .slot_reset = ixgbevf_io_slot_reset,
3515 .resume = ixgbevf_io_resume,
3518 static struct pci_driver ixgbevf_driver = {
3519 .name = ixgbevf_driver_name,
3520 .id_table = ixgbevf_pci_tbl,
3521 .probe = ixgbevf_probe,
3522 .remove = __devexit_p(ixgbevf_remove),
3524 /* Power Management Hooks */
3525 .suspend = ixgbevf_suspend,
3526 .resume = ixgbevf_resume,
3528 .shutdown = ixgbevf_shutdown,
3529 .err_handler = &ixgbevf_err_handler
3533 * ixgbevf_init_module - Driver Registration Routine
3535 * ixgbevf_init_module is the first routine called when the driver is
3536 * loaded. All it does is register with the PCI subsystem.
3538 static int __init ixgbevf_init_module(void)
3541 pr_info("%s - version %s\n", ixgbevf_driver_string,
3542 ixgbevf_driver_version);
3544 pr_info("%s\n", ixgbevf_copyright);
3546 ret = pci_register_driver(&ixgbevf_driver);
3550 module_init(ixgbevf_init_module);
3553 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3555 * ixgbevf_exit_module is called just before the driver is removed
3558 static void __exit ixgbevf_exit_module(void)
3560 pci_unregister_driver(&ixgbevf_driver);
3565 * ixgbevf_get_hw_dev_name - return device name string
3566 * used by hardware layer to print debugging information
3568 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3570 struct ixgbevf_adapter *adapter = hw->back;
3571 return adapter->netdev->name;
3575 module_exit(ixgbevf_exit_module);
3577 /* ixgbevf_main.c */