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/ipv6.h>
46 #include <linux/slab.h>
47 #include <net/checksum.h>
48 #include <net/ip6_checksum.h>
49 #include <linux/ethtool.h>
51 #include <linux/if_vlan.h>
52 #include <linux/prefetch.h>
56 const char ixgbevf_driver_name[] = "ixgbevf";
57 static const char ixgbevf_driver_string[] =
58 "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
60 #define DRV_VERSION "2.6.0-k"
61 const char ixgbevf_driver_version[] = DRV_VERSION;
62 static char ixgbevf_copyright[] =
63 "Copyright (c) 2009 - 2012 Intel Corporation.";
65 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
66 [board_82599_vf] = &ixgbevf_82599_vf_info,
67 [board_X540_vf] = &ixgbevf_X540_vf_info,
70 /* ixgbevf_pci_tbl - PCI Device ID Table
72 * Wildcard entries (PCI_ANY_ID) should come last
73 * Last entry must be all 0s
75 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
76 * Class, Class Mask, private data (not used) }
78 static struct pci_device_id ixgbevf_pci_tbl[] = {
79 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF),
81 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF),
84 /* required last entry */
87 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
89 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
90 MODULE_DESCRIPTION("Intel(R) 82599 Virtual Function Driver");
91 MODULE_LICENSE("GPL");
92 MODULE_VERSION(DRV_VERSION);
94 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
95 static int debug = -1;
96 module_param(debug, int, 0);
97 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
100 static void ixgbevf_set_itr_msix(struct ixgbevf_q_vector *q_vector);
101 static void ixgbevf_write_eitr(struct ixgbevf_adapter *adapter, int v_idx,
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
126 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
127 u8 queue, u8 msix_vector)
130 struct ixgbe_hw *hw = &adapter->hw;
131 if (direction == -1) {
133 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
134 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
137 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
139 /* tx or rx causes */
140 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
141 index = ((16 * (queue & 1)) + (8 * direction));
142 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
143 ivar &= ~(0xFF << index);
144 ivar |= (msix_vector << index);
145 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
149 static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_adapter *adapter,
150 struct ixgbevf_tx_buffer
153 if (tx_buffer_info->dma) {
154 if (tx_buffer_info->mapped_as_page)
155 dma_unmap_page(&adapter->pdev->dev,
157 tx_buffer_info->length,
160 dma_unmap_single(&adapter->pdev->dev,
162 tx_buffer_info->length,
164 tx_buffer_info->dma = 0;
166 if (tx_buffer_info->skb) {
167 dev_kfree_skb_any(tx_buffer_info->skb);
168 tx_buffer_info->skb = NULL;
170 tx_buffer_info->time_stamp = 0;
171 /* tx_buffer_info must be completely set up in the transmit path */
174 #define IXGBE_MAX_TXD_PWR 14
175 #define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
177 /* Tx Descriptors needed, worst case */
178 #define TXD_USE_COUNT(S) (((S) >> IXGBE_MAX_TXD_PWR) + \
179 (((S) & (IXGBE_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
180 #define DESC_NEEDED (TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD) /* skb->data */ + \
181 MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1) /* for context */
183 static void ixgbevf_tx_timeout(struct net_device *netdev);
186 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
187 * @adapter: board private structure
188 * @tx_ring: tx ring to clean
190 static bool ixgbevf_clean_tx_irq(struct ixgbevf_adapter *adapter,
191 struct ixgbevf_ring *tx_ring)
193 struct net_device *netdev = adapter->netdev;
194 struct ixgbe_hw *hw = &adapter->hw;
195 union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
196 struct ixgbevf_tx_buffer *tx_buffer_info;
197 unsigned int i, eop, count = 0;
198 unsigned int total_bytes = 0, total_packets = 0;
200 i = tx_ring->next_to_clean;
201 eop = tx_ring->tx_buffer_info[i].next_to_watch;
202 eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
204 while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
205 (count < tx_ring->work_limit)) {
206 bool cleaned = false;
207 rmb(); /* read buffer_info after eop_desc */
208 /* eop could change between read and DD-check */
209 if (unlikely(eop != tx_ring->tx_buffer_info[i].next_to_watch))
211 for ( ; !cleaned; count++) {
213 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
214 tx_buffer_info = &tx_ring->tx_buffer_info[i];
215 cleaned = (i == eop);
216 skb = tx_buffer_info->skb;
218 if (cleaned && skb) {
219 unsigned int segs, bytecount;
221 /* gso_segs is currently only valid for tcp */
222 segs = skb_shinfo(skb)->gso_segs ?: 1;
223 /* multiply data chunks by size of headers */
224 bytecount = ((segs - 1) * skb_headlen(skb)) +
226 total_packets += segs;
227 total_bytes += bytecount;
230 ixgbevf_unmap_and_free_tx_resource(adapter,
233 tx_desc->wb.status = 0;
236 if (i == tx_ring->count)
241 eop = tx_ring->tx_buffer_info[i].next_to_watch;
242 eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
245 tx_ring->next_to_clean = i;
247 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
248 if (unlikely(count && netif_carrier_ok(netdev) &&
249 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
250 /* Make sure that anybody stopping the queue after this
251 * sees the new next_to_clean.
254 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
255 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
256 netif_wake_subqueue(netdev, tx_ring->queue_index);
257 ++adapter->restart_queue;
261 /* re-arm the interrupt */
262 if ((count >= tx_ring->work_limit) &&
263 (!test_bit(__IXGBEVF_DOWN, &adapter->state))) {
264 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, tx_ring->v_idx);
267 u64_stats_update_begin(&tx_ring->syncp);
268 tx_ring->total_bytes += total_bytes;
269 tx_ring->total_packets += total_packets;
270 u64_stats_update_end(&tx_ring->syncp);
272 return count < tx_ring->work_limit;
276 * ixgbevf_receive_skb - Send a completed packet up the stack
277 * @q_vector: structure containing interrupt and ring information
278 * @skb: packet to send up
279 * @status: hardware indication of status of receive
280 * @rx_ring: rx descriptor ring (for a specific queue) to setup
281 * @rx_desc: rx descriptor
283 static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
284 struct sk_buff *skb, u8 status,
285 struct ixgbevf_ring *ring,
286 union ixgbe_adv_rx_desc *rx_desc)
288 struct ixgbevf_adapter *adapter = q_vector->adapter;
289 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
290 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
292 if (is_vlan && test_bit(tag, adapter->active_vlans))
293 __vlan_hwaccel_put_tag(skb, tag);
295 napi_gro_receive(&q_vector->napi, skb);
299 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
300 * @adapter: address of board private structure
301 * @status_err: hardware indication of status of receive
302 * @skb: skb currently being received and modified
304 static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
305 u32 status_err, struct sk_buff *skb)
307 skb_checksum_none_assert(skb);
309 /* Rx csum disabled */
310 if (!(adapter->netdev->features & NETIF_F_RXCSUM))
313 /* if IP and error */
314 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
315 (status_err & IXGBE_RXDADV_ERR_IPE)) {
316 adapter->hw_csum_rx_error++;
320 if (!(status_err & IXGBE_RXD_STAT_L4CS))
323 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
324 adapter->hw_csum_rx_error++;
328 /* It must be a TCP or UDP packet with a valid checksum */
329 skb->ip_summed = CHECKSUM_UNNECESSARY;
330 adapter->hw_csum_rx_good++;
334 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
335 * @adapter: address of board private structure
337 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
338 struct ixgbevf_ring *rx_ring,
341 struct pci_dev *pdev = adapter->pdev;
342 union ixgbe_adv_rx_desc *rx_desc;
343 struct ixgbevf_rx_buffer *bi;
346 unsigned int bufsz = rx_ring->rx_buf_len + NET_IP_ALIGN;
348 i = rx_ring->next_to_use;
349 bi = &rx_ring->rx_buffer_info[i];
351 while (cleaned_count--) {
352 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
355 skb = netdev_alloc_skb(adapter->netdev,
359 adapter->alloc_rx_buff_failed++;
364 * Make buffer alignment 2 beyond a 16 byte boundary
365 * this will result in a 16 byte aligned IP header after
366 * the 14 byte MAC header is removed
368 skb_reserve(skb, NET_IP_ALIGN);
373 bi->dma = dma_map_single(&pdev->dev, skb->data,
377 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
380 if (i == rx_ring->count)
382 bi = &rx_ring->rx_buffer_info[i];
386 if (rx_ring->next_to_use != i) {
387 rx_ring->next_to_use = i;
389 i = (rx_ring->count - 1);
391 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
395 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
399 struct ixgbe_hw *hw = &adapter->hw;
401 mask = (qmask & 0xFFFFFFFF);
402 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
405 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
406 struct ixgbevf_ring *rx_ring,
407 int *work_done, int work_to_do)
409 struct ixgbevf_adapter *adapter = q_vector->adapter;
410 struct pci_dev *pdev = adapter->pdev;
411 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
412 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
416 bool cleaned = false;
417 int cleaned_count = 0;
418 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
420 i = rx_ring->next_to_clean;
421 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
422 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
423 rx_buffer_info = &rx_ring->rx_buffer_info[i];
425 while (staterr & IXGBE_RXD_STAT_DD) {
426 if (*work_done >= work_to_do)
430 rmb(); /* read descriptor and rx_buffer_info after status DD */
431 len = le16_to_cpu(rx_desc->wb.upper.length);
433 skb = rx_buffer_info->skb;
434 prefetch(skb->data - NET_IP_ALIGN);
435 rx_buffer_info->skb = NULL;
437 if (rx_buffer_info->dma) {
438 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
441 rx_buffer_info->dma = 0;
446 if (i == rx_ring->count)
449 next_rxd = IXGBE_RX_DESC_ADV(*rx_ring, i);
453 next_buffer = &rx_ring->rx_buffer_info[i];
455 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
456 skb->next = next_buffer->skb;
457 skb->next->prev = skb;
458 adapter->non_eop_descs++;
462 /* ERR_MASK will only have valid bits if EOP set */
463 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
464 dev_kfree_skb_irq(skb);
468 ixgbevf_rx_checksum(adapter, staterr, skb);
470 /* probably a little skewed due to removing CRC */
471 total_rx_bytes += skb->len;
475 * Work around issue of some types of VM to VM loop back
476 * packets not getting split correctly
478 if (staterr & IXGBE_RXD_STAT_LB) {
479 u32 header_fixup_len = skb_headlen(skb);
480 if (header_fixup_len < 14)
481 skb_push(skb, header_fixup_len);
483 skb->protocol = eth_type_trans(skb, adapter->netdev);
485 ixgbevf_receive_skb(q_vector, skb, staterr, rx_ring, rx_desc);
488 rx_desc->wb.upper.status_error = 0;
490 /* return some buffers to hardware, one at a time is too slow */
491 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
492 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
497 /* use prefetched values */
499 rx_buffer_info = &rx_ring->rx_buffer_info[i];
501 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
504 rx_ring->next_to_clean = i;
505 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
508 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
510 u64_stats_update_begin(&rx_ring->syncp);
511 rx_ring->total_packets += total_rx_packets;
512 rx_ring->total_bytes += total_rx_bytes;
513 u64_stats_update_end(&rx_ring->syncp);
519 * ixgbevf_clean_rxonly - msix (aka one shot) rx clean routine
520 * @napi: napi struct with our devices info in it
521 * @budget: amount of work driver is allowed to do this pass, in packets
523 * This function is optimized for cleaning one queue only on a single
526 static int ixgbevf_clean_rxonly(struct napi_struct *napi, int budget)
528 struct ixgbevf_q_vector *q_vector =
529 container_of(napi, struct ixgbevf_q_vector, napi);
530 struct ixgbevf_adapter *adapter = q_vector->adapter;
531 struct ixgbevf_ring *rx_ring = NULL;
535 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
536 rx_ring = &(adapter->rx_ring[r_idx]);
538 ixgbevf_clean_rx_irq(q_vector, rx_ring, &work_done, budget);
540 /* If all Rx work done, exit the polling mode */
541 if (work_done < budget) {
543 if (adapter->itr_setting & 1)
544 ixgbevf_set_itr_msix(q_vector);
545 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
546 ixgbevf_irq_enable_queues(adapter, rx_ring->v_idx);
553 * ixgbevf_clean_rxonly_many - msix (aka one shot) rx clean routine
554 * @napi: napi struct with our devices info in it
555 * @budget: amount of work driver is allowed to do this pass, in packets
557 * This function will clean more than one rx queue associated with a
560 static int ixgbevf_clean_rxonly_many(struct napi_struct *napi, int budget)
562 struct ixgbevf_q_vector *q_vector =
563 container_of(napi, struct ixgbevf_q_vector, napi);
564 struct ixgbevf_adapter *adapter = q_vector->adapter;
565 struct ixgbevf_ring *rx_ring = NULL;
566 int work_done = 0, i;
570 /* attempt to distribute budget to each queue fairly, but don't allow
571 * the budget to go below 1 because we'll exit polling */
572 budget /= (q_vector->rxr_count ?: 1);
573 budget = max(budget, 1);
574 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
575 for (i = 0; i < q_vector->rxr_count; i++) {
576 rx_ring = &(adapter->rx_ring[r_idx]);
577 ixgbevf_clean_rx_irq(q_vector, rx_ring, &work_done, budget);
578 enable_mask |= rx_ring->v_idx;
579 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
583 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
584 rx_ring = &(adapter->rx_ring[r_idx]);
586 /* If all Rx work done, exit the polling mode */
587 if (work_done < budget) {
589 if (adapter->itr_setting & 1)
590 ixgbevf_set_itr_msix(q_vector);
591 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
592 ixgbevf_irq_enable_queues(adapter, enable_mask);
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 struct ixgbe_hw *hw = &adapter->hw;
610 int i, j, q_vectors, v_idx, r_idx;
613 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
616 * Populate the IVAR table and set the ITR values to the
617 * corresponding register.
619 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
620 q_vector = adapter->q_vector[v_idx];
621 /* XXX for_each_set_bit(...) */
622 r_idx = find_first_bit(q_vector->rxr_idx,
623 adapter->num_rx_queues);
625 for (i = 0; i < q_vector->rxr_count; i++) {
626 j = adapter->rx_ring[r_idx].reg_idx;
627 ixgbevf_set_ivar(adapter, 0, j, v_idx);
628 r_idx = find_next_bit(q_vector->rxr_idx,
629 adapter->num_rx_queues,
632 r_idx = find_first_bit(q_vector->txr_idx,
633 adapter->num_tx_queues);
635 for (i = 0; i < q_vector->txr_count; i++) {
636 j = adapter->tx_ring[r_idx].reg_idx;
637 ixgbevf_set_ivar(adapter, 1, j, v_idx);
638 r_idx = find_next_bit(q_vector->txr_idx,
639 adapter->num_tx_queues,
643 /* if this is a tx only vector halve the interrupt rate */
644 if (q_vector->txr_count && !q_vector->rxr_count)
645 q_vector->eitr = (adapter->eitr_param >> 1);
646 else if (q_vector->rxr_count)
648 q_vector->eitr = adapter->eitr_param;
650 ixgbevf_write_eitr(adapter, v_idx, q_vector->eitr);
653 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
655 /* set up to autoclear timer, and the vectors */
656 mask = IXGBE_EIMS_ENABLE_MASK;
657 mask &= ~IXGBE_EIMS_OTHER;
658 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, mask);
665 latency_invalid = 255
669 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
670 * @adapter: pointer to adapter
671 * @eitr: eitr setting (ints per sec) to give last timeslice
672 * @itr_setting: current throttle rate in ints/second
673 * @packets: the number of packets during this measurement interval
674 * @bytes: the number of bytes during this measurement interval
676 * Stores a new ITR value based on packets and byte
677 * counts during the last interrupt. The advantage of per interrupt
678 * computation is faster updates and more accurate ITR for the current
679 * traffic pattern. Constants in this function were computed
680 * based on theoretical maximum wire speed and thresholds were set based
681 * on testing data as well as attempting to minimize response time
682 * while increasing bulk throughput.
684 static u8 ixgbevf_update_itr(struct ixgbevf_adapter *adapter,
685 u32 eitr, u8 itr_setting,
686 int packets, int bytes)
688 unsigned int retval = itr_setting;
693 goto update_itr_done;
696 /* simple throttlerate management
697 * 0-20MB/s lowest (100000 ints/s)
698 * 20-100MB/s low (20000 ints/s)
699 * 100-1249MB/s bulk (8000 ints/s)
701 /* what was last interrupt timeslice? */
702 timepassed_us = 1000000/eitr;
703 bytes_perint = bytes / timepassed_us; /* bytes/usec */
705 switch (itr_setting) {
707 if (bytes_perint > 10)
708 retval = low_latency;
711 if (bytes_perint > 20)
712 retval = bulk_latency;
713 else if (bytes_perint <= 10)
714 retval = lowest_latency;
717 if (bytes_perint <= 20)
718 retval = low_latency;
727 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
728 * @adapter: pointer to adapter struct
729 * @v_idx: vector index into q_vector array
730 * @itr_reg: new value to be written in *register* format, not ints/s
732 * This function is made to be called by ethtool and by the driver
733 * when it needs to update VTEITR registers at runtime. Hardware
734 * specific quirks/differences are taken care of here.
736 static void ixgbevf_write_eitr(struct ixgbevf_adapter *adapter, int v_idx,
739 struct ixgbe_hw *hw = &adapter->hw;
741 itr_reg = EITR_INTS_PER_SEC_TO_REG(itr_reg);
744 * set the WDIS bit to not clear the timer bits and cause an
745 * immediate assertion of the interrupt
747 itr_reg |= IXGBE_EITR_CNT_WDIS;
749 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
752 static void ixgbevf_set_itr_msix(struct ixgbevf_q_vector *q_vector)
754 struct ixgbevf_adapter *adapter = q_vector->adapter;
756 u8 current_itr, ret_itr;
757 int i, r_idx, v_idx = q_vector->v_idx;
758 struct ixgbevf_ring *rx_ring, *tx_ring;
760 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
761 for (i = 0; i < q_vector->txr_count; i++) {
762 tx_ring = &(adapter->tx_ring[r_idx]);
763 ret_itr = ixgbevf_update_itr(adapter, q_vector->eitr,
765 tx_ring->total_packets,
766 tx_ring->total_bytes);
767 /* if the result for this queue would decrease interrupt
768 * rate for this vector then use that result */
769 q_vector->tx_itr = ((q_vector->tx_itr > ret_itr) ?
770 q_vector->tx_itr - 1 : ret_itr);
771 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
775 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
776 for (i = 0; i < q_vector->rxr_count; i++) {
777 rx_ring = &(adapter->rx_ring[r_idx]);
778 ret_itr = ixgbevf_update_itr(adapter, q_vector->eitr,
780 rx_ring->total_packets,
781 rx_ring->total_bytes);
782 /* if the result for this queue would decrease interrupt
783 * rate for this vector then use that result */
784 q_vector->rx_itr = ((q_vector->rx_itr > ret_itr) ?
785 q_vector->rx_itr - 1 : ret_itr);
786 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
790 current_itr = max(q_vector->rx_itr, q_vector->tx_itr);
792 switch (current_itr) {
793 /* counts and packets in update_itr are dependent on these numbers */
798 new_itr = 20000; /* aka hwitr = ~200 */
806 if (new_itr != q_vector->eitr) {
809 /* save the algorithm value here, not the smoothed one */
810 q_vector->eitr = new_itr;
811 /* do an exponential smoothing */
812 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
813 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
814 ixgbevf_write_eitr(adapter, v_idx, itr_reg);
818 static irqreturn_t ixgbevf_msix_mbx(int irq, void *data)
820 struct net_device *netdev = data;
821 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
822 struct ixgbe_hw *hw = &adapter->hw;
825 bool got_ack = false;
827 eicr = IXGBE_READ_REG(hw, IXGBE_VTEICS);
828 IXGBE_WRITE_REG(hw, IXGBE_VTEICR, eicr);
830 if (!hw->mbx.ops.check_for_ack(hw))
833 if (!hw->mbx.ops.check_for_msg(hw)) {
834 hw->mbx.ops.read(hw, &msg, 1);
836 if ((msg & IXGBE_MBVFICR_VFREQ_MASK) == IXGBE_PF_CONTROL_MSG)
837 mod_timer(&adapter->watchdog_timer,
838 round_jiffies(jiffies + 1));
840 if (msg & IXGBE_VT_MSGTYPE_NACK)
841 pr_warn("Last Request of type %2.2x to PF Nacked\n",
844 * Restore the PFSTS bit in case someone is polling for a
845 * return message from the PF
847 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFSTS;
851 * checking for the ack clears the PFACK bit. Place
852 * it back in the v2p_mailbox cache so that anyone
853 * polling for an ack will not miss it
856 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFACK;
861 static irqreturn_t ixgbevf_msix_clean_tx(int irq, void *data)
863 struct ixgbevf_q_vector *q_vector = data;
864 struct ixgbevf_adapter *adapter = q_vector->adapter;
865 struct ixgbevf_ring *tx_ring;
868 if (!q_vector->txr_count)
871 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
872 for (i = 0; i < q_vector->txr_count; i++) {
873 tx_ring = &(adapter->tx_ring[r_idx]);
874 tx_ring->total_bytes = 0;
875 tx_ring->total_packets = 0;
876 ixgbevf_clean_tx_irq(adapter, tx_ring);
877 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
881 if (adapter->itr_setting & 1)
882 ixgbevf_set_itr_msix(q_vector);
888 * ixgbevf_msix_clean_rx - single unshared vector rx clean (all queues)
890 * @data: pointer to our q_vector struct for this interrupt vector
892 static irqreturn_t ixgbevf_msix_clean_rx(int irq, void *data)
894 struct ixgbevf_q_vector *q_vector = data;
895 struct ixgbevf_adapter *adapter = q_vector->adapter;
896 struct ixgbe_hw *hw = &adapter->hw;
897 struct ixgbevf_ring *rx_ring;
901 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
902 for (i = 0; i < q_vector->rxr_count; i++) {
903 rx_ring = &(adapter->rx_ring[r_idx]);
904 rx_ring->total_bytes = 0;
905 rx_ring->total_packets = 0;
906 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
910 if (!q_vector->rxr_count)
913 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
914 rx_ring = &(adapter->rx_ring[r_idx]);
915 /* disable interrupts on this vector only */
916 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, rx_ring->v_idx);
917 napi_schedule(&q_vector->napi);
923 static irqreturn_t ixgbevf_msix_clean_many(int irq, void *data)
925 ixgbevf_msix_clean_rx(irq, data);
926 ixgbevf_msix_clean_tx(irq, data);
931 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
934 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
936 set_bit(r_idx, q_vector->rxr_idx);
937 q_vector->rxr_count++;
938 a->rx_ring[r_idx].v_idx = 1 << v_idx;
941 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
944 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
946 set_bit(t_idx, q_vector->txr_idx);
947 q_vector->txr_count++;
948 a->tx_ring[t_idx].v_idx = 1 << v_idx;
952 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
953 * @adapter: board private structure to initialize
955 * This function maps descriptor rings to the queue-specific vectors
956 * we were allotted through the MSI-X enabling code. Ideally, we'd have
957 * one vector per ring/queue, but on a constrained vector budget, we
958 * group the rings as "efficiently" as possible. You would add new
959 * mapping configurations in here.
961 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
965 int rxr_idx = 0, txr_idx = 0;
966 int rxr_remaining = adapter->num_rx_queues;
967 int txr_remaining = adapter->num_tx_queues;
972 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
975 * The ideal configuration...
976 * We have enough vectors to map one per queue.
978 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
979 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
980 map_vector_to_rxq(adapter, v_start, rxr_idx);
982 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
983 map_vector_to_txq(adapter, v_start, txr_idx);
988 * If we don't have enough vectors for a 1-to-1
989 * mapping, we'll have to group them so there are
990 * multiple queues per vector.
992 /* Re-adjusting *qpv takes care of the remainder. */
993 for (i = v_start; i < q_vectors; i++) {
994 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
995 for (j = 0; j < rqpv; j++) {
996 map_vector_to_rxq(adapter, i, rxr_idx);
1001 for (i = v_start; i < q_vectors; i++) {
1002 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
1003 for (j = 0; j < tqpv; j++) {
1004 map_vector_to_txq(adapter, i, txr_idx);
1015 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1016 * @adapter: board private structure
1018 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1019 * interrupts from the kernel.
1021 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1023 struct net_device *netdev = adapter->netdev;
1024 irqreturn_t (*handler)(int, void *);
1025 int i, vector, q_vectors, err;
1028 /* Decrement for Other and TCP Timer vectors */
1029 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1031 #define SET_HANDLER(_v) (((_v)->rxr_count && (_v)->txr_count) \
1032 ? &ixgbevf_msix_clean_many : \
1033 (_v)->rxr_count ? &ixgbevf_msix_clean_rx : \
1034 (_v)->txr_count ? &ixgbevf_msix_clean_tx : \
1036 for (vector = 0; vector < q_vectors; vector++) {
1037 handler = SET_HANDLER(adapter->q_vector[vector]);
1039 if (handler == &ixgbevf_msix_clean_rx) {
1040 sprintf(adapter->name[vector], "%s-%s-%d",
1041 netdev->name, "rx", ri++);
1042 } else if (handler == &ixgbevf_msix_clean_tx) {
1043 sprintf(adapter->name[vector], "%s-%s-%d",
1044 netdev->name, "tx", ti++);
1045 } else if (handler == &ixgbevf_msix_clean_many) {
1046 sprintf(adapter->name[vector], "%s-%s-%d",
1047 netdev->name, "TxRx", vector);
1049 /* skip this unused q_vector */
1052 err = request_irq(adapter->msix_entries[vector].vector,
1053 handler, 0, adapter->name[vector],
1054 adapter->q_vector[vector]);
1056 hw_dbg(&adapter->hw,
1057 "request_irq failed for MSIX interrupt "
1058 "Error: %d\n", err);
1059 goto free_queue_irqs;
1063 sprintf(adapter->name[vector], "%s:mbx", netdev->name);
1064 err = request_irq(adapter->msix_entries[vector].vector,
1065 &ixgbevf_msix_mbx, 0, adapter->name[vector], netdev);
1067 hw_dbg(&adapter->hw,
1068 "request_irq for msix_mbx failed: %d\n", err);
1069 goto free_queue_irqs;
1075 for (i = vector - 1; i >= 0; i--)
1076 free_irq(adapter->msix_entries[--vector].vector,
1077 &(adapter->q_vector[i]));
1078 pci_disable_msix(adapter->pdev);
1079 kfree(adapter->msix_entries);
1080 adapter->msix_entries = NULL;
1084 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
1086 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1088 for (i = 0; i < q_vectors; i++) {
1089 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
1090 bitmap_zero(q_vector->rxr_idx, MAX_RX_QUEUES);
1091 bitmap_zero(q_vector->txr_idx, MAX_TX_QUEUES);
1092 q_vector->rxr_count = 0;
1093 q_vector->txr_count = 0;
1094 q_vector->eitr = adapter->eitr_param;
1099 * ixgbevf_request_irq - initialize interrupts
1100 * @adapter: board private structure
1102 * Attempts to configure interrupts using the best available
1103 * capabilities of the hardware and kernel.
1105 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1109 err = ixgbevf_request_msix_irqs(adapter);
1112 hw_dbg(&adapter->hw,
1113 "request_irq failed, Error %d\n", err);
1118 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1120 struct net_device *netdev = adapter->netdev;
1123 q_vectors = adapter->num_msix_vectors;
1127 free_irq(adapter->msix_entries[i].vector, netdev);
1130 for (; i >= 0; i--) {
1131 free_irq(adapter->msix_entries[i].vector,
1132 adapter->q_vector[i]);
1135 ixgbevf_reset_q_vectors(adapter);
1139 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1140 * @adapter: board private structure
1142 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1145 struct ixgbe_hw *hw = &adapter->hw;
1147 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1149 IXGBE_WRITE_FLUSH(hw);
1151 for (i = 0; i < adapter->num_msix_vectors; i++)
1152 synchronize_irq(adapter->msix_entries[i].vector);
1156 * ixgbevf_irq_enable - Enable default interrupt generation settings
1157 * @adapter: board private structure
1159 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter,
1160 bool queues, bool flush)
1162 struct ixgbe_hw *hw = &adapter->hw;
1166 mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
1169 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
1172 ixgbevf_irq_enable_queues(adapter, qmask);
1175 IXGBE_WRITE_FLUSH(hw);
1179 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1180 * @adapter: board private structure
1182 * Configure the Tx unit of the MAC after a reset.
1184 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1187 struct ixgbe_hw *hw = &adapter->hw;
1188 u32 i, j, tdlen, txctrl;
1190 /* Setup the HW Tx Head and Tail descriptor pointers */
1191 for (i = 0; i < adapter->num_tx_queues; i++) {
1192 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1195 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1196 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1197 (tdba & DMA_BIT_MASK(32)));
1198 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1199 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1200 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1201 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1202 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1203 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1204 /* Disable Tx Head Writeback RO bit, since this hoses
1205 * bookkeeping if things aren't delivered in order.
1207 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1208 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1209 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1213 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1215 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1217 struct ixgbevf_ring *rx_ring;
1218 struct ixgbe_hw *hw = &adapter->hw;
1221 rx_ring = &adapter->rx_ring[index];
1223 srrctl = IXGBE_SRRCTL_DROP_EN;
1225 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1227 if (rx_ring->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
1228 srrctl |= IXGBEVF_RXBUFFER_2048 >>
1229 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1231 srrctl |= rx_ring->rx_buf_len >>
1232 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1233 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1237 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1238 * @adapter: board private structure
1240 * Configure the Rx unit of the MAC after a reset.
1242 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1245 struct ixgbe_hw *hw = &adapter->hw;
1246 struct net_device *netdev = adapter->netdev;
1247 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1252 /* PSRTYPE must be initialized in 82599 */
1253 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
1254 if (netdev->mtu <= ETH_DATA_LEN)
1255 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1257 rx_buf_len = ALIGN(max_frame, 1024);
1259 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1260 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1261 * the Base and Length of the Rx Descriptor Ring */
1262 for (i = 0; i < adapter->num_rx_queues; i++) {
1263 rdba = adapter->rx_ring[i].dma;
1264 j = adapter->rx_ring[i].reg_idx;
1265 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1266 (rdba & DMA_BIT_MASK(32)));
1267 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1268 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1269 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1270 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1271 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1272 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1273 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1275 ixgbevf_configure_srrctl(adapter, j);
1279 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1281 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1282 struct ixgbe_hw *hw = &adapter->hw;
1284 /* add VID to filter table */
1285 if (hw->mac.ops.set_vfta)
1286 hw->mac.ops.set_vfta(hw, vid, 0, true);
1287 set_bit(vid, adapter->active_vlans);
1292 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1294 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1295 struct ixgbe_hw *hw = &adapter->hw;
1297 /* remove VID from filter table */
1298 if (hw->mac.ops.set_vfta)
1299 hw->mac.ops.set_vfta(hw, vid, 0, false);
1300 clear_bit(vid, adapter->active_vlans);
1305 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1309 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1310 ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
1313 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1315 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1316 struct ixgbe_hw *hw = &adapter->hw;
1319 if ((netdev_uc_count(netdev)) > 10) {
1320 pr_err("Too many unicast filters - No Space\n");
1324 if (!netdev_uc_empty(netdev)) {
1325 struct netdev_hw_addr *ha;
1326 netdev_for_each_uc_addr(ha, netdev) {
1327 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1332 * If the list is empty then send message to PF driver to
1333 * clear all macvlans on this VF.
1335 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1342 * ixgbevf_set_rx_mode - Multicast set
1343 * @netdev: network interface device structure
1345 * The set_rx_method entry point is called whenever the multicast address
1346 * list or the network interface flags are updated. This routine is
1347 * responsible for configuring the hardware for proper multicast mode.
1349 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1351 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1352 struct ixgbe_hw *hw = &adapter->hw;
1354 /* reprogram multicast list */
1355 if (hw->mac.ops.update_mc_addr_list)
1356 hw->mac.ops.update_mc_addr_list(hw, netdev);
1358 ixgbevf_write_uc_addr_list(netdev);
1361 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1364 struct ixgbevf_q_vector *q_vector;
1365 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1367 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1368 struct napi_struct *napi;
1369 q_vector = adapter->q_vector[q_idx];
1370 if (!q_vector->rxr_count)
1372 napi = &q_vector->napi;
1373 if (q_vector->rxr_count > 1)
1374 napi->poll = &ixgbevf_clean_rxonly_many;
1380 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1383 struct ixgbevf_q_vector *q_vector;
1384 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1386 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1387 q_vector = adapter->q_vector[q_idx];
1388 if (!q_vector->rxr_count)
1390 napi_disable(&q_vector->napi);
1394 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1396 struct net_device *netdev = adapter->netdev;
1399 ixgbevf_set_rx_mode(netdev);
1401 ixgbevf_restore_vlan(adapter);
1403 ixgbevf_configure_tx(adapter);
1404 ixgbevf_configure_rx(adapter);
1405 for (i = 0; i < adapter->num_rx_queues; i++) {
1406 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1407 ixgbevf_alloc_rx_buffers(adapter, ring, ring->count);
1408 ring->next_to_use = ring->count - 1;
1409 writel(ring->next_to_use, adapter->hw.hw_addr + ring->tail);
1413 #define IXGBE_MAX_RX_DESC_POLL 10
1414 static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1417 struct ixgbe_hw *hw = &adapter->hw;
1418 int j = adapter->rx_ring[rxr].reg_idx;
1421 for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1422 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1427 if (k >= IXGBE_MAX_RX_DESC_POLL) {
1428 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1429 "not set within the polling period\n", rxr);
1432 ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
1433 (adapter->rx_ring[rxr].count - 1));
1436 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1438 /* Only save pre-reset stats if there are some */
1439 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1440 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1441 adapter->stats.base_vfgprc;
1442 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1443 adapter->stats.base_vfgptc;
1444 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1445 adapter->stats.base_vfgorc;
1446 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1447 adapter->stats.base_vfgotc;
1448 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1449 adapter->stats.base_vfmprc;
1453 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1455 struct ixgbe_hw *hw = &adapter->hw;
1457 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1458 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1459 adapter->stats.last_vfgorc |=
1460 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1461 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1462 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1463 adapter->stats.last_vfgotc |=
1464 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1465 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1467 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1468 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1469 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1470 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1471 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1474 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1476 struct net_device *netdev = adapter->netdev;
1477 struct ixgbe_hw *hw = &adapter->hw;
1479 int num_rx_rings = adapter->num_rx_queues;
1483 for (i = 0; i < adapter->num_tx_queues; i++) {
1484 j = adapter->tx_ring[i].reg_idx;
1485 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1486 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1487 txdctl |= (8 << 16);
1488 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1491 for (i = 0; i < adapter->num_tx_queues; i++) {
1492 j = adapter->tx_ring[i].reg_idx;
1493 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1494 txdctl |= IXGBE_TXDCTL_ENABLE;
1495 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1498 for (i = 0; i < num_rx_rings; i++) {
1499 j = adapter->rx_ring[i].reg_idx;
1500 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1501 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1502 if (hw->mac.type == ixgbe_mac_X540_vf) {
1503 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1504 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1505 IXGBE_RXDCTL_RLPML_EN);
1507 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1508 ixgbevf_rx_desc_queue_enable(adapter, i);
1511 ixgbevf_configure_msix(adapter);
1513 if (hw->mac.ops.set_rar) {
1514 if (is_valid_ether_addr(hw->mac.addr))
1515 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1517 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1520 msg[0] = IXGBE_VF_SET_LPE;
1521 msg[1] = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1522 hw->mbx.ops.write_posted(hw, msg, 2);
1524 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1525 ixgbevf_napi_enable_all(adapter);
1527 /* enable transmits */
1528 netif_tx_start_all_queues(netdev);
1530 ixgbevf_save_reset_stats(adapter);
1531 ixgbevf_init_last_counter_stats(adapter);
1533 mod_timer(&adapter->watchdog_timer, jiffies);
1536 void ixgbevf_up(struct ixgbevf_adapter *adapter)
1538 struct ixgbe_hw *hw = &adapter->hw;
1540 ixgbevf_configure(adapter);
1542 ixgbevf_up_complete(adapter);
1544 /* clear any pending interrupts, may auto mask */
1545 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1547 ixgbevf_irq_enable(adapter, true, true);
1551 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1552 * @adapter: board private structure
1553 * @rx_ring: ring to free buffers from
1555 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1556 struct ixgbevf_ring *rx_ring)
1558 struct pci_dev *pdev = adapter->pdev;
1562 if (!rx_ring->rx_buffer_info)
1565 /* Free all the Rx ring sk_buffs */
1566 for (i = 0; i < rx_ring->count; i++) {
1567 struct ixgbevf_rx_buffer *rx_buffer_info;
1569 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1570 if (rx_buffer_info->dma) {
1571 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1572 rx_ring->rx_buf_len,
1574 rx_buffer_info->dma = 0;
1576 if (rx_buffer_info->skb) {
1577 struct sk_buff *skb = rx_buffer_info->skb;
1578 rx_buffer_info->skb = NULL;
1580 struct sk_buff *this = skb;
1582 dev_kfree_skb(this);
1587 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1588 memset(rx_ring->rx_buffer_info, 0, size);
1590 /* Zero out the descriptor ring */
1591 memset(rx_ring->desc, 0, rx_ring->size);
1593 rx_ring->next_to_clean = 0;
1594 rx_ring->next_to_use = 0;
1597 writel(0, adapter->hw.hw_addr + rx_ring->head);
1599 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1603 * ixgbevf_clean_tx_ring - Free Tx Buffers
1604 * @adapter: board private structure
1605 * @tx_ring: ring to be cleaned
1607 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1608 struct ixgbevf_ring *tx_ring)
1610 struct ixgbevf_tx_buffer *tx_buffer_info;
1614 if (!tx_ring->tx_buffer_info)
1617 /* Free all the Tx ring sk_buffs */
1619 for (i = 0; i < tx_ring->count; i++) {
1620 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1621 ixgbevf_unmap_and_free_tx_resource(adapter, tx_buffer_info);
1624 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1625 memset(tx_ring->tx_buffer_info, 0, size);
1627 memset(tx_ring->desc, 0, tx_ring->size);
1629 tx_ring->next_to_use = 0;
1630 tx_ring->next_to_clean = 0;
1633 writel(0, adapter->hw.hw_addr + tx_ring->head);
1635 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1639 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1640 * @adapter: board private structure
1642 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1646 for (i = 0; i < adapter->num_rx_queues; i++)
1647 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1651 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1652 * @adapter: board private structure
1654 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1658 for (i = 0; i < adapter->num_tx_queues; i++)
1659 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1662 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1664 struct net_device *netdev = adapter->netdev;
1665 struct ixgbe_hw *hw = &adapter->hw;
1669 /* signal that we are down to the interrupt handler */
1670 set_bit(__IXGBEVF_DOWN, &adapter->state);
1671 /* disable receives */
1673 netif_tx_disable(netdev);
1677 netif_tx_stop_all_queues(netdev);
1679 ixgbevf_irq_disable(adapter);
1681 ixgbevf_napi_disable_all(adapter);
1683 del_timer_sync(&adapter->watchdog_timer);
1684 /* can't call flush scheduled work here because it can deadlock
1685 * if linkwatch_event tries to acquire the rtnl_lock which we are
1687 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1690 /* disable transmits in the hardware now that interrupts are off */
1691 for (i = 0; i < adapter->num_tx_queues; i++) {
1692 j = adapter->tx_ring[i].reg_idx;
1693 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1694 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1695 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1698 netif_carrier_off(netdev);
1700 if (!pci_channel_offline(adapter->pdev))
1701 ixgbevf_reset(adapter);
1703 ixgbevf_clean_all_tx_rings(adapter);
1704 ixgbevf_clean_all_rx_rings(adapter);
1707 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1709 struct ixgbe_hw *hw = &adapter->hw;
1711 WARN_ON(in_interrupt());
1713 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1717 * Check if PF is up before re-init. If not then skip until
1718 * later when the PF is up and ready to service requests from
1719 * the VF via mailbox. If the VF is up and running then the
1720 * watchdog task will continue to schedule reset tasks until
1721 * the PF is up and running.
1723 if (!hw->mac.ops.reset_hw(hw)) {
1724 ixgbevf_down(adapter);
1725 ixgbevf_up(adapter);
1728 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1731 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1733 struct ixgbe_hw *hw = &adapter->hw;
1734 struct net_device *netdev = adapter->netdev;
1736 if (hw->mac.ops.reset_hw(hw))
1737 hw_dbg(hw, "PF still resetting\n");
1739 hw->mac.ops.init_hw(hw);
1741 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1742 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1744 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1749 static void ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1752 int err, vector_threshold;
1754 /* We'll want at least 3 (vector_threshold):
1757 * 3) Other (Link Status Change, etc.)
1759 vector_threshold = MIN_MSIX_COUNT;
1761 /* The more we get, the more we will assign to Tx/Rx Cleanup
1762 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1763 * Right now, we simply care about how many we'll get; we'll
1764 * set them up later while requesting irq's.
1766 while (vectors >= vector_threshold) {
1767 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1769 if (!err) /* Success in acquiring all requested vectors. */
1772 vectors = 0; /* Nasty failure, quit now */
1773 else /* err == number of vectors we should try again with */
1777 if (vectors < vector_threshold) {
1778 /* Can't allocate enough MSI-X interrupts? Oh well.
1779 * This just means we'll go with either a single MSI
1780 * vector or fall back to legacy interrupts.
1782 hw_dbg(&adapter->hw,
1783 "Unable to allocate MSI-X interrupts\n");
1784 kfree(adapter->msix_entries);
1785 adapter->msix_entries = NULL;
1788 * Adjust for only the vectors we'll use, which is minimum
1789 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1790 * vectors we were allocated.
1792 adapter->num_msix_vectors = vectors;
1797 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
1798 * @adapter: board private structure to initialize
1800 * This is the top level queue allocation routine. The order here is very
1801 * important, starting with the "most" number of features turned on at once,
1802 * and ending with the smallest set of features. This way large combinations
1803 * can be allocated if they're turned on, and smaller combinations are the
1804 * fallthrough conditions.
1807 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1809 /* Start with base case */
1810 adapter->num_rx_queues = 1;
1811 adapter->num_tx_queues = 1;
1815 * ixgbevf_alloc_queues - Allocate memory for all rings
1816 * @adapter: board private structure to initialize
1818 * We allocate one ring per queue at run-time since we don't know the
1819 * number of queues at compile-time. The polling_netdev array is
1820 * intended for Multiqueue, but should work fine with a single queue.
1822 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1826 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1827 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1828 if (!adapter->tx_ring)
1829 goto err_tx_ring_allocation;
1831 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1832 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1833 if (!adapter->rx_ring)
1834 goto err_rx_ring_allocation;
1836 for (i = 0; i < adapter->num_tx_queues; i++) {
1837 adapter->tx_ring[i].count = adapter->tx_ring_count;
1838 adapter->tx_ring[i].queue_index = i;
1839 adapter->tx_ring[i].reg_idx = i;
1842 for (i = 0; i < adapter->num_rx_queues; i++) {
1843 adapter->rx_ring[i].count = adapter->rx_ring_count;
1844 adapter->rx_ring[i].queue_index = i;
1845 adapter->rx_ring[i].reg_idx = i;
1850 err_rx_ring_allocation:
1851 kfree(adapter->tx_ring);
1852 err_tx_ring_allocation:
1857 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1858 * @adapter: board private structure to initialize
1860 * Attempt to configure the interrupts using the best available
1861 * capabilities of the hardware and the kernel.
1863 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1866 int vector, v_budget;
1869 * It's easy to be greedy for MSI-X vectors, but it really
1870 * doesn't do us much good if we have a lot more vectors
1871 * than CPU's. So let's be conservative and only ask for
1872 * (roughly) twice the number of vectors as there are CPU's.
1874 v_budget = min(adapter->num_rx_queues + adapter->num_tx_queues,
1875 (int)(num_online_cpus() * 2)) + NON_Q_VECTORS;
1877 /* A failure in MSI-X entry allocation isn't fatal, but it does
1878 * mean we disable MSI-X capabilities of the adapter. */
1879 adapter->msix_entries = kcalloc(v_budget,
1880 sizeof(struct msix_entry), GFP_KERNEL);
1881 if (!adapter->msix_entries) {
1886 for (vector = 0; vector < v_budget; vector++)
1887 adapter->msix_entries[vector].entry = vector;
1889 ixgbevf_acquire_msix_vectors(adapter, v_budget);
1896 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1897 * @adapter: board private structure to initialize
1899 * We allocate one q_vector per queue interrupt. If allocation fails we
1902 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1904 int q_idx, num_q_vectors;
1905 struct ixgbevf_q_vector *q_vector;
1907 int (*poll)(struct napi_struct *, int);
1909 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1910 napi_vectors = adapter->num_rx_queues;
1911 poll = &ixgbevf_clean_rxonly;
1913 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1914 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1917 q_vector->adapter = adapter;
1918 q_vector->v_idx = q_idx;
1919 q_vector->eitr = adapter->eitr_param;
1920 if (q_idx < napi_vectors)
1921 netif_napi_add(adapter->netdev, &q_vector->napi,
1923 adapter->q_vector[q_idx] = q_vector;
1931 q_vector = adapter->q_vector[q_idx];
1932 netif_napi_del(&q_vector->napi);
1934 adapter->q_vector[q_idx] = NULL;
1940 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1941 * @adapter: board private structure to initialize
1943 * This function frees the memory allocated to the q_vectors. In addition if
1944 * NAPI is enabled it will delete any references to the NAPI struct prior
1945 * to freeing the q_vector.
1947 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1949 int q_idx, num_q_vectors;
1952 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1953 napi_vectors = adapter->num_rx_queues;
1955 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1956 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1958 adapter->q_vector[q_idx] = NULL;
1959 if (q_idx < napi_vectors)
1960 netif_napi_del(&q_vector->napi);
1966 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1967 * @adapter: board private structure
1970 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1972 pci_disable_msix(adapter->pdev);
1973 kfree(adapter->msix_entries);
1974 adapter->msix_entries = NULL;
1978 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1979 * @adapter: board private structure to initialize
1982 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
1986 /* Number of supported queues */
1987 ixgbevf_set_num_queues(adapter);
1989 err = ixgbevf_set_interrupt_capability(adapter);
1991 hw_dbg(&adapter->hw,
1992 "Unable to setup interrupt capabilities\n");
1993 goto err_set_interrupt;
1996 err = ixgbevf_alloc_q_vectors(adapter);
1998 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
2000 goto err_alloc_q_vectors;
2003 err = ixgbevf_alloc_queues(adapter);
2005 pr_err("Unable to allocate memory for queues\n");
2006 goto err_alloc_queues;
2009 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
2010 "Tx Queue count = %u\n",
2011 (adapter->num_rx_queues > 1) ? "Enabled" :
2012 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2014 set_bit(__IXGBEVF_DOWN, &adapter->state);
2018 ixgbevf_free_q_vectors(adapter);
2019 err_alloc_q_vectors:
2020 ixgbevf_reset_interrupt_capability(adapter);
2026 * ixgbevf_sw_init - Initialize general software structures
2027 * (struct ixgbevf_adapter)
2028 * @adapter: board private structure to initialize
2030 * ixgbevf_sw_init initializes the Adapter private data structure.
2031 * Fields are initialized based on PCI device information and
2032 * OS network device settings (MTU size).
2034 static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2036 struct ixgbe_hw *hw = &adapter->hw;
2037 struct pci_dev *pdev = adapter->pdev;
2040 /* PCI config space info */
2042 hw->vendor_id = pdev->vendor;
2043 hw->device_id = pdev->device;
2044 hw->revision_id = pdev->revision;
2045 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2046 hw->subsystem_device_id = pdev->subsystem_device;
2048 hw->mbx.ops.init_params(hw);
2049 hw->mac.max_tx_queues = MAX_TX_QUEUES;
2050 hw->mac.max_rx_queues = MAX_RX_QUEUES;
2051 err = hw->mac.ops.reset_hw(hw);
2053 dev_info(&pdev->dev,
2054 "PF still in reset state, assigning new address\n");
2055 eth_hw_addr_random(adapter->netdev);
2056 memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
2057 adapter->netdev->addr_len);
2059 err = hw->mac.ops.init_hw(hw);
2061 pr_err("init_shared_code failed: %d\n", err);
2064 memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
2065 adapter->netdev->addr_len);
2068 /* Enable dynamic interrupt throttling rates */
2069 adapter->eitr_param = 20000;
2070 adapter->itr_setting = 1;
2072 /* set default ring sizes */
2073 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2074 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2076 set_bit(__IXGBEVF_DOWN, &adapter->state);
2083 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2085 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2086 if (current_counter < last_counter) \
2087 counter += 0x100000000LL; \
2088 last_counter = current_counter; \
2089 counter &= 0xFFFFFFFF00000000LL; \
2090 counter |= current_counter; \
2093 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2095 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2096 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2097 u64 current_counter = (current_counter_msb << 32) | \
2098 current_counter_lsb; \
2099 if (current_counter < last_counter) \
2100 counter += 0x1000000000LL; \
2101 last_counter = current_counter; \
2102 counter &= 0xFFFFFFF000000000LL; \
2103 counter |= current_counter; \
2106 * ixgbevf_update_stats - Update the board statistics counters.
2107 * @adapter: board private structure
2109 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2111 struct ixgbe_hw *hw = &adapter->hw;
2113 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2114 adapter->stats.vfgprc);
2115 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2116 adapter->stats.vfgptc);
2117 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2118 adapter->stats.last_vfgorc,
2119 adapter->stats.vfgorc);
2120 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2121 adapter->stats.last_vfgotc,
2122 adapter->stats.vfgotc);
2123 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2124 adapter->stats.vfmprc);
2128 * ixgbevf_watchdog - Timer Call-back
2129 * @data: pointer to adapter cast into an unsigned long
2131 static void ixgbevf_watchdog(unsigned long data)
2133 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2134 struct ixgbe_hw *hw = &adapter->hw;
2139 * Do the watchdog outside of interrupt context due to the lovely
2140 * delays that some of the newer hardware requires
2143 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2144 goto watchdog_short_circuit;
2146 /* get one bit for every active tx/rx interrupt vector */
2147 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2148 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2149 if (qv->rxr_count || qv->txr_count)
2153 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, (u32)eics);
2155 watchdog_short_circuit:
2156 schedule_work(&adapter->watchdog_task);
2160 * ixgbevf_tx_timeout - Respond to a Tx Hang
2161 * @netdev: network interface device structure
2163 static void ixgbevf_tx_timeout(struct net_device *netdev)
2165 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2167 /* Do the reset outside of interrupt context */
2168 schedule_work(&adapter->reset_task);
2171 static void ixgbevf_reset_task(struct work_struct *work)
2173 struct ixgbevf_adapter *adapter;
2174 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2176 /* If we're already down or resetting, just bail */
2177 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2178 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2181 adapter->tx_timeout_count++;
2183 ixgbevf_reinit_locked(adapter);
2187 * ixgbevf_watchdog_task - worker thread to bring link up
2188 * @work: pointer to work_struct containing our data
2190 static void ixgbevf_watchdog_task(struct work_struct *work)
2192 struct ixgbevf_adapter *adapter = container_of(work,
2193 struct ixgbevf_adapter,
2195 struct net_device *netdev = adapter->netdev;
2196 struct ixgbe_hw *hw = &adapter->hw;
2197 u32 link_speed = adapter->link_speed;
2198 bool link_up = adapter->link_up;
2200 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2203 * Always check the link on the watchdog because we have
2206 if (hw->mac.ops.check_link) {
2207 if ((hw->mac.ops.check_link(hw, &link_speed,
2208 &link_up, false)) != 0) {
2209 adapter->link_up = link_up;
2210 adapter->link_speed = link_speed;
2211 netif_carrier_off(netdev);
2212 netif_tx_stop_all_queues(netdev);
2213 schedule_work(&adapter->reset_task);
2217 /* always assume link is up, if no check link
2219 link_speed = IXGBE_LINK_SPEED_10GB_FULL;
2222 adapter->link_up = link_up;
2223 adapter->link_speed = link_speed;
2226 if (!netif_carrier_ok(netdev)) {
2227 hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
2228 (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2230 netif_carrier_on(netdev);
2231 netif_tx_wake_all_queues(netdev);
2234 adapter->link_up = false;
2235 adapter->link_speed = 0;
2236 if (netif_carrier_ok(netdev)) {
2237 hw_dbg(&adapter->hw, "NIC Link is Down\n");
2238 netif_carrier_off(netdev);
2239 netif_tx_stop_all_queues(netdev);
2243 ixgbevf_update_stats(adapter);
2246 /* Reset the timer */
2247 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2248 mod_timer(&adapter->watchdog_timer,
2249 round_jiffies(jiffies + (2 * HZ)));
2251 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2255 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2256 * @adapter: board private structure
2257 * @tx_ring: Tx descriptor ring for a specific queue
2259 * Free all transmit software resources
2261 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2262 struct ixgbevf_ring *tx_ring)
2264 struct pci_dev *pdev = adapter->pdev;
2266 ixgbevf_clean_tx_ring(adapter, tx_ring);
2268 vfree(tx_ring->tx_buffer_info);
2269 tx_ring->tx_buffer_info = NULL;
2271 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2274 tx_ring->desc = NULL;
2278 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2279 * @adapter: board private structure
2281 * Free all transmit software resources
2283 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2287 for (i = 0; i < adapter->num_tx_queues; i++)
2288 if (adapter->tx_ring[i].desc)
2289 ixgbevf_free_tx_resources(adapter,
2290 &adapter->tx_ring[i]);
2295 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2296 * @adapter: board private structure
2297 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2299 * Return 0 on success, negative on failure
2301 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2302 struct ixgbevf_ring *tx_ring)
2304 struct pci_dev *pdev = adapter->pdev;
2307 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2308 tx_ring->tx_buffer_info = vzalloc(size);
2309 if (!tx_ring->tx_buffer_info)
2312 /* round up to nearest 4K */
2313 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2314 tx_ring->size = ALIGN(tx_ring->size, 4096);
2316 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2317 &tx_ring->dma, GFP_KERNEL);
2321 tx_ring->next_to_use = 0;
2322 tx_ring->next_to_clean = 0;
2323 tx_ring->work_limit = tx_ring->count;
2327 vfree(tx_ring->tx_buffer_info);
2328 tx_ring->tx_buffer_info = NULL;
2329 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2330 "descriptor ring\n");
2335 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2336 * @adapter: board private structure
2338 * If this function returns with an error, then it's possible one or
2339 * more of the rings is populated (while the rest are not). It is the
2340 * callers duty to clean those orphaned rings.
2342 * Return 0 on success, negative on failure
2344 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2348 for (i = 0; i < adapter->num_tx_queues; i++) {
2349 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2352 hw_dbg(&adapter->hw,
2353 "Allocation for Tx Queue %u failed\n", i);
2361 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2362 * @adapter: board private structure
2363 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2365 * Returns 0 on success, negative on failure
2367 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2368 struct ixgbevf_ring *rx_ring)
2370 struct pci_dev *pdev = adapter->pdev;
2373 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2374 rx_ring->rx_buffer_info = vzalloc(size);
2375 if (!rx_ring->rx_buffer_info)
2378 /* Round up to nearest 4K */
2379 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2380 rx_ring->size = ALIGN(rx_ring->size, 4096);
2382 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2383 &rx_ring->dma, GFP_KERNEL);
2385 if (!rx_ring->desc) {
2386 hw_dbg(&adapter->hw,
2387 "Unable to allocate memory for "
2388 "the receive descriptor ring\n");
2389 vfree(rx_ring->rx_buffer_info);
2390 rx_ring->rx_buffer_info = NULL;
2394 rx_ring->next_to_clean = 0;
2395 rx_ring->next_to_use = 0;
2403 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2404 * @adapter: board private structure
2406 * If this function returns with an error, then it's possible one or
2407 * more of the rings is populated (while the rest are not). It is the
2408 * callers duty to clean those orphaned rings.
2410 * Return 0 on success, negative on failure
2412 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2416 for (i = 0; i < adapter->num_rx_queues; i++) {
2417 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2420 hw_dbg(&adapter->hw,
2421 "Allocation for Rx Queue %u failed\n", i);
2428 * ixgbevf_free_rx_resources - Free Rx Resources
2429 * @adapter: board private structure
2430 * @rx_ring: ring to clean the resources from
2432 * Free all receive software resources
2434 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2435 struct ixgbevf_ring *rx_ring)
2437 struct pci_dev *pdev = adapter->pdev;
2439 ixgbevf_clean_rx_ring(adapter, rx_ring);
2441 vfree(rx_ring->rx_buffer_info);
2442 rx_ring->rx_buffer_info = NULL;
2444 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2447 rx_ring->desc = NULL;
2451 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2452 * @adapter: board private structure
2454 * Free all receive software resources
2456 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2460 for (i = 0; i < adapter->num_rx_queues; i++)
2461 if (adapter->rx_ring[i].desc)
2462 ixgbevf_free_rx_resources(adapter,
2463 &adapter->rx_ring[i]);
2467 * ixgbevf_open - Called when a network interface is made active
2468 * @netdev: network interface device structure
2470 * Returns 0 on success, negative value on failure
2472 * The open entry point is called when a network interface is made
2473 * active by the system (IFF_UP). At this point all resources needed
2474 * for transmit and receive operations are allocated, the interrupt
2475 * handler is registered with the OS, the watchdog timer is started,
2476 * and the stack is notified that the interface is ready.
2478 static int ixgbevf_open(struct net_device *netdev)
2480 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2481 struct ixgbe_hw *hw = &adapter->hw;
2484 /* disallow open during test */
2485 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2488 if (hw->adapter_stopped) {
2489 ixgbevf_reset(adapter);
2490 /* if adapter is still stopped then PF isn't up and
2491 * the vf can't start. */
2492 if (hw->adapter_stopped) {
2493 err = IXGBE_ERR_MBX;
2494 pr_err("Unable to start - perhaps the PF Driver isn't "
2496 goto err_setup_reset;
2500 /* allocate transmit descriptors */
2501 err = ixgbevf_setup_all_tx_resources(adapter);
2505 /* allocate receive descriptors */
2506 err = ixgbevf_setup_all_rx_resources(adapter);
2510 ixgbevf_configure(adapter);
2513 * Map the Tx/Rx rings to the vectors we were allotted.
2514 * if request_irq will be called in this function map_rings
2515 * must be called *before* up_complete
2517 ixgbevf_map_rings_to_vectors(adapter);
2519 ixgbevf_up_complete(adapter);
2521 /* clear any pending interrupts, may auto mask */
2522 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2523 err = ixgbevf_request_irq(adapter);
2527 ixgbevf_irq_enable(adapter, true, true);
2532 ixgbevf_down(adapter);
2533 ixgbevf_free_irq(adapter);
2535 ixgbevf_free_all_rx_resources(adapter);
2537 ixgbevf_free_all_tx_resources(adapter);
2538 ixgbevf_reset(adapter);
2546 * ixgbevf_close - Disables a network interface
2547 * @netdev: network interface device structure
2549 * Returns 0, this is not allowed to fail
2551 * The close entry point is called when an interface is de-activated
2552 * by the OS. The hardware is still under the drivers control, but
2553 * needs to be disabled. A global MAC reset is issued to stop the
2554 * hardware, and all transmit and receive resources are freed.
2556 static int ixgbevf_close(struct net_device *netdev)
2558 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2560 ixgbevf_down(adapter);
2561 ixgbevf_free_irq(adapter);
2563 ixgbevf_free_all_tx_resources(adapter);
2564 ixgbevf_free_all_rx_resources(adapter);
2569 static int ixgbevf_tso(struct ixgbevf_adapter *adapter,
2570 struct ixgbevf_ring *tx_ring,
2571 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2573 struct ixgbe_adv_tx_context_desc *context_desc;
2576 struct ixgbevf_tx_buffer *tx_buffer_info;
2577 u32 vlan_macip_lens = 0, type_tucmd_mlhl;
2578 u32 mss_l4len_idx, l4len;
2580 if (skb_is_gso(skb)) {
2581 if (skb_header_cloned(skb)) {
2582 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2586 l4len = tcp_hdrlen(skb);
2589 if (skb->protocol == htons(ETH_P_IP)) {
2590 struct iphdr *iph = ip_hdr(skb);
2593 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2597 adapter->hw_tso_ctxt++;
2598 } else if (skb_is_gso_v6(skb)) {
2599 ipv6_hdr(skb)->payload_len = 0;
2600 tcp_hdr(skb)->check =
2601 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2602 &ipv6_hdr(skb)->daddr,
2604 adapter->hw_tso6_ctxt++;
2607 i = tx_ring->next_to_use;
2609 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2610 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2612 /* VLAN MACLEN IPLEN */
2613 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2615 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
2616 vlan_macip_lens |= ((skb_network_offset(skb)) <<
2617 IXGBE_ADVTXD_MACLEN_SHIFT);
2618 *hdr_len += skb_network_offset(skb);
2620 (skb_transport_header(skb) - skb_network_header(skb));
2622 (skb_transport_header(skb) - skb_network_header(skb));
2623 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2624 context_desc->seqnum_seed = 0;
2626 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2627 type_tucmd_mlhl = (IXGBE_TXD_CMD_DEXT |
2628 IXGBE_ADVTXD_DTYP_CTXT);
2630 if (skb->protocol == htons(ETH_P_IP))
2631 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
2632 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2633 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2637 (skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT);
2638 mss_l4len_idx |= (l4len << IXGBE_ADVTXD_L4LEN_SHIFT);
2639 /* use index 1 for TSO */
2640 mss_l4len_idx |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2641 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2643 tx_buffer_info->time_stamp = jiffies;
2644 tx_buffer_info->next_to_watch = i;
2647 if (i == tx_ring->count)
2649 tx_ring->next_to_use = i;
2657 static bool ixgbevf_tx_csum(struct ixgbevf_adapter *adapter,
2658 struct ixgbevf_ring *tx_ring,
2659 struct sk_buff *skb, u32 tx_flags)
2661 struct ixgbe_adv_tx_context_desc *context_desc;
2663 struct ixgbevf_tx_buffer *tx_buffer_info;
2664 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
2666 if (skb->ip_summed == CHECKSUM_PARTIAL ||
2667 (tx_flags & IXGBE_TX_FLAGS_VLAN)) {
2668 i = tx_ring->next_to_use;
2669 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2670 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2672 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2673 vlan_macip_lens |= (tx_flags &
2674 IXGBE_TX_FLAGS_VLAN_MASK);
2675 vlan_macip_lens |= (skb_network_offset(skb) <<
2676 IXGBE_ADVTXD_MACLEN_SHIFT);
2677 if (skb->ip_summed == CHECKSUM_PARTIAL)
2678 vlan_macip_lens |= (skb_transport_header(skb) -
2679 skb_network_header(skb));
2681 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2682 context_desc->seqnum_seed = 0;
2684 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
2685 IXGBE_ADVTXD_DTYP_CTXT);
2687 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2688 switch (skb->protocol) {
2689 case __constant_htons(ETH_P_IP):
2690 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
2691 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2693 IXGBE_ADVTXD_TUCMD_L4T_TCP;
2695 case __constant_htons(ETH_P_IPV6):
2696 /* XXX what about other V6 headers?? */
2697 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2699 IXGBE_ADVTXD_TUCMD_L4T_TCP;
2702 if (unlikely(net_ratelimit())) {
2703 pr_warn("partial checksum but "
2704 "proto=%x!\n", skb->protocol);
2710 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2711 /* use index zero for tx checksum offload */
2712 context_desc->mss_l4len_idx = 0;
2714 tx_buffer_info->time_stamp = jiffies;
2715 tx_buffer_info->next_to_watch = i;
2717 adapter->hw_csum_tx_good++;
2719 if (i == tx_ring->count)
2721 tx_ring->next_to_use = i;
2729 static int ixgbevf_tx_map(struct ixgbevf_adapter *adapter,
2730 struct ixgbevf_ring *tx_ring,
2731 struct sk_buff *skb, u32 tx_flags,
2734 struct pci_dev *pdev = adapter->pdev;
2735 struct ixgbevf_tx_buffer *tx_buffer_info;
2737 unsigned int total = skb->len;
2738 unsigned int offset = 0, size;
2740 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2744 i = tx_ring->next_to_use;
2746 len = min(skb_headlen(skb), total);
2748 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2749 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2751 tx_buffer_info->length = size;
2752 tx_buffer_info->mapped_as_page = false;
2753 tx_buffer_info->dma = dma_map_single(&adapter->pdev->dev,
2755 size, DMA_TO_DEVICE);
2756 if (dma_mapping_error(&pdev->dev, tx_buffer_info->dma))
2758 tx_buffer_info->time_stamp = jiffies;
2759 tx_buffer_info->next_to_watch = i;
2766 if (i == tx_ring->count)
2770 for (f = 0; f < nr_frags; f++) {
2771 const struct skb_frag_struct *frag;
2773 frag = &skb_shinfo(skb)->frags[f];
2774 len = min((unsigned int)skb_frag_size(frag), total);
2778 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2779 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2781 tx_buffer_info->length = size;
2782 tx_buffer_info->dma =
2783 skb_frag_dma_map(&adapter->pdev->dev, frag,
2784 offset, size, DMA_TO_DEVICE);
2785 tx_buffer_info->mapped_as_page = true;
2786 if (dma_mapping_error(&pdev->dev, tx_buffer_info->dma))
2788 tx_buffer_info->time_stamp = jiffies;
2789 tx_buffer_info->next_to_watch = i;
2796 if (i == tx_ring->count)
2804 i = tx_ring->count - 1;
2807 tx_ring->tx_buffer_info[i].skb = skb;
2808 tx_ring->tx_buffer_info[first].next_to_watch = i;
2813 dev_err(&pdev->dev, "TX DMA map failed\n");
2815 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2816 tx_buffer_info->dma = 0;
2817 tx_buffer_info->time_stamp = 0;
2818 tx_buffer_info->next_to_watch = 0;
2821 /* clear timestamp and dma mappings for remaining portion of packet */
2822 while (count >= 0) {
2826 i += tx_ring->count;
2827 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2828 ixgbevf_unmap_and_free_tx_resource(adapter, tx_buffer_info);
2834 static void ixgbevf_tx_queue(struct ixgbevf_adapter *adapter,
2835 struct ixgbevf_ring *tx_ring, int tx_flags,
2836 int count, u32 paylen, u8 hdr_len)
2838 union ixgbe_adv_tx_desc *tx_desc = NULL;
2839 struct ixgbevf_tx_buffer *tx_buffer_info;
2840 u32 olinfo_status = 0, cmd_type_len = 0;
2843 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2845 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2847 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2849 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2850 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2852 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2853 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2855 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
2856 IXGBE_ADVTXD_POPTS_SHIFT;
2858 /* use index 1 context for tso */
2859 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2860 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2861 olinfo_status |= IXGBE_TXD_POPTS_IXSM <<
2862 IXGBE_ADVTXD_POPTS_SHIFT;
2864 } else if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2865 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
2866 IXGBE_ADVTXD_POPTS_SHIFT;
2868 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2870 i = tx_ring->next_to_use;
2872 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2873 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
2874 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2875 tx_desc->read.cmd_type_len =
2876 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2877 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2879 if (i == tx_ring->count)
2883 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2886 * Force memory writes to complete before letting h/w
2887 * know there are new descriptors to fetch. (Only
2888 * applicable for weak-ordered memory model archs,
2893 tx_ring->next_to_use = i;
2894 writel(i, adapter->hw.hw_addr + tx_ring->tail);
2897 static int __ixgbevf_maybe_stop_tx(struct net_device *netdev,
2898 struct ixgbevf_ring *tx_ring, int size)
2900 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2902 netif_stop_subqueue(netdev, tx_ring->queue_index);
2903 /* Herbert's original patch had:
2904 * smp_mb__after_netif_stop_queue();
2905 * but since that doesn't exist yet, just open code it. */
2908 /* We need to check again in a case another CPU has just
2909 * made room available. */
2910 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
2913 /* A reprieve! - use start_queue because it doesn't call schedule */
2914 netif_start_subqueue(netdev, tx_ring->queue_index);
2915 ++adapter->restart_queue;
2919 static int ixgbevf_maybe_stop_tx(struct net_device *netdev,
2920 struct ixgbevf_ring *tx_ring, int size)
2922 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
2924 return __ixgbevf_maybe_stop_tx(netdev, tx_ring, size);
2927 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2929 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2930 struct ixgbevf_ring *tx_ring;
2932 unsigned int tx_flags = 0;
2939 tx_ring = &adapter->tx_ring[r_idx];
2941 if (vlan_tx_tag_present(skb)) {
2942 tx_flags |= vlan_tx_tag_get(skb);
2943 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
2944 tx_flags |= IXGBE_TX_FLAGS_VLAN;
2947 /* four things can cause us to need a context descriptor */
2948 if (skb_is_gso(skb) ||
2949 (skb->ip_summed == CHECKSUM_PARTIAL) ||
2950 (tx_flags & IXGBE_TX_FLAGS_VLAN))
2953 count += TXD_USE_COUNT(skb_headlen(skb));
2954 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2955 count += TXD_USE_COUNT(skb_frag_size(&skb_shinfo(skb)->frags[f]));
2957 if (ixgbevf_maybe_stop_tx(netdev, tx_ring, count)) {
2959 return NETDEV_TX_BUSY;
2962 first = tx_ring->next_to_use;
2964 if (skb->protocol == htons(ETH_P_IP))
2965 tx_flags |= IXGBE_TX_FLAGS_IPV4;
2966 tso = ixgbevf_tso(adapter, tx_ring, skb, tx_flags, &hdr_len);
2968 dev_kfree_skb_any(skb);
2969 return NETDEV_TX_OK;
2973 tx_flags |= IXGBE_TX_FLAGS_TSO;
2974 else if (ixgbevf_tx_csum(adapter, tx_ring, skb, tx_flags) &&
2975 (skb->ip_summed == CHECKSUM_PARTIAL))
2976 tx_flags |= IXGBE_TX_FLAGS_CSUM;
2978 ixgbevf_tx_queue(adapter, tx_ring, tx_flags,
2979 ixgbevf_tx_map(adapter, tx_ring, skb, tx_flags, first),
2982 ixgbevf_maybe_stop_tx(netdev, tx_ring, DESC_NEEDED);
2984 return NETDEV_TX_OK;
2988 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
2989 * @netdev: network interface device structure
2990 * @p: pointer to an address structure
2992 * Returns 0 on success, negative on failure
2994 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
2996 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2997 struct ixgbe_hw *hw = &adapter->hw;
2998 struct sockaddr *addr = p;
3000 if (!is_valid_ether_addr(addr->sa_data))
3001 return -EADDRNOTAVAIL;
3003 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3004 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3006 if (hw->mac.ops.set_rar)
3007 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
3013 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3014 * @netdev: network interface device structure
3015 * @new_mtu: new value for maximum frame size
3017 * Returns 0 on success, negative on failure
3019 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3021 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3022 struct ixgbe_hw *hw = &adapter->hw;
3023 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3024 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
3027 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3028 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3030 /* MTU < 68 is an error and causes problems on some kernels */
3031 if ((new_mtu < 68) || (max_frame > max_possible_frame))
3034 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3035 netdev->mtu, new_mtu);
3036 /* must set new MTU before calling down or up */
3037 netdev->mtu = new_mtu;
3039 if (!netif_running(netdev)) {
3040 msg[0] = IXGBE_VF_SET_LPE;
3042 hw->mbx.ops.write_posted(hw, msg, 2);
3045 if (netif_running(netdev))
3046 ixgbevf_reinit_locked(adapter);
3051 static void ixgbevf_shutdown(struct pci_dev *pdev)
3053 struct net_device *netdev = pci_get_drvdata(pdev);
3054 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3056 netif_device_detach(netdev);
3058 if (netif_running(netdev)) {
3059 ixgbevf_down(adapter);
3060 ixgbevf_free_irq(adapter);
3061 ixgbevf_free_all_tx_resources(adapter);
3062 ixgbevf_free_all_rx_resources(adapter);
3065 pci_save_state(pdev);
3067 pci_disable_device(pdev);
3070 static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3071 struct rtnl_link_stats64 *stats)
3073 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3076 const struct ixgbevf_ring *ring;
3079 ixgbevf_update_stats(adapter);
3081 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3083 for (i = 0; i < adapter->num_rx_queues; i++) {
3084 ring = &adapter->rx_ring[i];
3086 start = u64_stats_fetch_begin_bh(&ring->syncp);
3087 bytes = ring->total_bytes;
3088 packets = ring->total_packets;
3089 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3090 stats->rx_bytes += bytes;
3091 stats->rx_packets += packets;
3094 for (i = 0; i < adapter->num_tx_queues; i++) {
3095 ring = &adapter->tx_ring[i];
3097 start = u64_stats_fetch_begin_bh(&ring->syncp);
3098 bytes = ring->total_bytes;
3099 packets = ring->total_packets;
3100 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3101 stats->tx_bytes += bytes;
3102 stats->tx_packets += packets;
3108 static const struct net_device_ops ixgbe_netdev_ops = {
3109 .ndo_open = ixgbevf_open,
3110 .ndo_stop = ixgbevf_close,
3111 .ndo_start_xmit = ixgbevf_xmit_frame,
3112 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
3113 .ndo_get_stats64 = ixgbevf_get_stats,
3114 .ndo_validate_addr = eth_validate_addr,
3115 .ndo_set_mac_address = ixgbevf_set_mac,
3116 .ndo_change_mtu = ixgbevf_change_mtu,
3117 .ndo_tx_timeout = ixgbevf_tx_timeout,
3118 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3119 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
3122 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3124 dev->netdev_ops = &ixgbe_netdev_ops;
3125 ixgbevf_set_ethtool_ops(dev);
3126 dev->watchdog_timeo = 5 * HZ;
3130 * ixgbevf_probe - Device Initialization Routine
3131 * @pdev: PCI device information struct
3132 * @ent: entry in ixgbevf_pci_tbl
3134 * Returns 0 on success, negative on failure
3136 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3137 * The OS initialization, configuring of the adapter private structure,
3138 * and a hardware reset occur.
3140 static int __devinit ixgbevf_probe(struct pci_dev *pdev,
3141 const struct pci_device_id *ent)
3143 struct net_device *netdev;
3144 struct ixgbevf_adapter *adapter = NULL;
3145 struct ixgbe_hw *hw = NULL;
3146 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3147 static int cards_found;
3148 int err, pci_using_dac;
3150 err = pci_enable_device(pdev);
3154 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3155 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3158 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3160 err = dma_set_coherent_mask(&pdev->dev,
3163 dev_err(&pdev->dev, "No usable DMA "
3164 "configuration, aborting\n");
3171 err = pci_request_regions(pdev, ixgbevf_driver_name);
3173 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3177 pci_set_master(pdev);
3179 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3183 goto err_alloc_etherdev;
3186 SET_NETDEV_DEV(netdev, &pdev->dev);
3188 pci_set_drvdata(pdev, netdev);
3189 adapter = netdev_priv(netdev);
3191 adapter->netdev = netdev;
3192 adapter->pdev = pdev;
3195 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3198 * call save state here in standalone driver because it relies on
3199 * adapter struct to exist, and needs to call netdev_priv
3201 pci_save_state(pdev);
3203 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3204 pci_resource_len(pdev, 0));
3210 ixgbevf_assign_netdev_ops(netdev);
3212 adapter->bd_number = cards_found;
3215 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3216 hw->mac.type = ii->mac;
3218 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3219 sizeof(struct ixgbe_mbx_operations));
3221 /* setup the private structure */
3222 err = ixgbevf_sw_init(adapter);
3226 /* The HW MAC address was set and/or determined in sw_init */
3227 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3229 if (!is_valid_ether_addr(netdev->dev_addr)) {
3230 pr_err("invalid MAC address\n");
3235 netdev->hw_features = NETIF_F_SG |
3242 netdev->features = netdev->hw_features |
3243 NETIF_F_HW_VLAN_TX |
3244 NETIF_F_HW_VLAN_RX |
3245 NETIF_F_HW_VLAN_FILTER;
3247 netdev->vlan_features |= NETIF_F_TSO;
3248 netdev->vlan_features |= NETIF_F_TSO6;
3249 netdev->vlan_features |= NETIF_F_IP_CSUM;
3250 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3251 netdev->vlan_features |= NETIF_F_SG;
3254 netdev->features |= NETIF_F_HIGHDMA;
3256 netdev->priv_flags |= IFF_UNICAST_FLT;
3258 init_timer(&adapter->watchdog_timer);
3259 adapter->watchdog_timer.function = ixgbevf_watchdog;
3260 adapter->watchdog_timer.data = (unsigned long)adapter;
3262 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3263 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3265 err = ixgbevf_init_interrupt_scheme(adapter);
3269 /* pick up the PCI bus settings for reporting later */
3270 if (hw->mac.ops.get_bus_info)
3271 hw->mac.ops.get_bus_info(hw);
3273 strcpy(netdev->name, "eth%d");
3275 err = register_netdev(netdev);
3279 netif_carrier_off(netdev);
3281 ixgbevf_init_last_counter_stats(adapter);
3283 /* print the MAC address */
3284 hw_dbg(hw, "%pM\n", netdev->dev_addr);
3286 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3288 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3294 ixgbevf_reset_interrupt_capability(adapter);
3295 iounmap(hw->hw_addr);
3297 free_netdev(netdev);
3299 pci_release_regions(pdev);
3302 pci_disable_device(pdev);
3307 * ixgbevf_remove - Device Removal Routine
3308 * @pdev: PCI device information struct
3310 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3311 * that it should release a PCI device. The could be caused by a
3312 * Hot-Plug event, or because the driver is going to be removed from
3315 static void __devexit ixgbevf_remove(struct pci_dev *pdev)
3317 struct net_device *netdev = pci_get_drvdata(pdev);
3318 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3320 set_bit(__IXGBEVF_DOWN, &adapter->state);
3322 del_timer_sync(&adapter->watchdog_timer);
3324 cancel_work_sync(&adapter->reset_task);
3325 cancel_work_sync(&adapter->watchdog_task);
3327 if (netdev->reg_state == NETREG_REGISTERED)
3328 unregister_netdev(netdev);
3330 ixgbevf_reset_interrupt_capability(adapter);
3332 iounmap(adapter->hw.hw_addr);
3333 pci_release_regions(pdev);
3335 hw_dbg(&adapter->hw, "Remove complete\n");
3337 kfree(adapter->tx_ring);
3338 kfree(adapter->rx_ring);
3340 free_netdev(netdev);
3342 pci_disable_device(pdev);
3345 static struct pci_driver ixgbevf_driver = {
3346 .name = ixgbevf_driver_name,
3347 .id_table = ixgbevf_pci_tbl,
3348 .probe = ixgbevf_probe,
3349 .remove = __devexit_p(ixgbevf_remove),
3350 .shutdown = ixgbevf_shutdown,
3354 * ixgbevf_init_module - Driver Registration Routine
3356 * ixgbevf_init_module is the first routine called when the driver is
3357 * loaded. All it does is register with the PCI subsystem.
3359 static int __init ixgbevf_init_module(void)
3362 pr_info("%s - version %s\n", ixgbevf_driver_string,
3363 ixgbevf_driver_version);
3365 pr_info("%s\n", ixgbevf_copyright);
3367 ret = pci_register_driver(&ixgbevf_driver);
3371 module_init(ixgbevf_init_module);
3374 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3376 * ixgbevf_exit_module is called just before the driver is removed
3379 static void __exit ixgbevf_exit_module(void)
3381 pci_unregister_driver(&ixgbevf_driver);
3386 * ixgbevf_get_hw_dev_name - return device name string
3387 * used by hardware layer to print debugging information
3389 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3391 struct ixgbevf_adapter *adapter = hw->back;
3392 return adapter->netdev->name;
3396 module_exit(ixgbevf_exit_module);
3398 /* ixgbevf_main.c */