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.6.0-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);
103 static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
104 struct ixgbevf_ring *rx_ring,
108 * Force memory writes to complete before letting h/w
109 * know there are new descriptors to fetch. (Only
110 * applicable for weak-ordered memory model archs,
114 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
118 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
119 * @adapter: pointer to adapter struct
120 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
121 * @queue: queue to map the corresponding interrupt to
122 * @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 i = tx_ring->next_to_clean;
197 eop = tx_ring->tx_buffer_info[i].next_to_watch;
198 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
200 while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
201 (count < tx_ring->count)) {
202 bool cleaned = false;
203 rmb(); /* read buffer_info after eop_desc */
204 /* eop could change between read and DD-check */
205 if (unlikely(eop != tx_ring->tx_buffer_info[i].next_to_watch))
207 for ( ; !cleaned; count++) {
209 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
210 tx_buffer_info = &tx_ring->tx_buffer_info[i];
211 cleaned = (i == eop);
212 skb = tx_buffer_info->skb;
214 if (cleaned && skb) {
215 unsigned int segs, bytecount;
217 /* gso_segs is currently only valid for tcp */
218 segs = skb_shinfo(skb)->gso_segs ?: 1;
219 /* multiply data chunks by size of headers */
220 bytecount = ((segs - 1) * skb_headlen(skb)) +
222 total_packets += segs;
223 total_bytes += bytecount;
226 ixgbevf_unmap_and_free_tx_resource(tx_ring,
229 tx_desc->wb.status = 0;
232 if (i == tx_ring->count)
237 eop = tx_ring->tx_buffer_info[i].next_to_watch;
238 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
241 tx_ring->next_to_clean = i;
243 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
244 if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
245 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
246 /* Make sure that anybody stopping the queue after this
247 * sees the new next_to_clean.
250 if (__netif_subqueue_stopped(tx_ring->netdev,
251 tx_ring->queue_index) &&
252 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
253 netif_wake_subqueue(tx_ring->netdev,
254 tx_ring->queue_index);
255 ++adapter->restart_queue;
259 u64_stats_update_begin(&tx_ring->syncp);
260 tx_ring->total_bytes += total_bytes;
261 tx_ring->total_packets += total_packets;
262 u64_stats_update_end(&tx_ring->syncp);
264 return count < tx_ring->count;
268 * ixgbevf_receive_skb - Send a completed packet up the stack
269 * @q_vector: structure containing interrupt and ring information
270 * @skb: packet to send up
271 * @status: hardware indication of status of receive
272 * @rx_ring: rx descriptor ring (for a specific queue) to setup
273 * @rx_desc: rx descriptor
275 static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
276 struct sk_buff *skb, u8 status,
277 struct ixgbevf_ring *ring,
278 union ixgbe_adv_rx_desc *rx_desc)
280 struct ixgbevf_adapter *adapter = q_vector->adapter;
281 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
282 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
284 if (is_vlan && test_bit(tag & VLAN_VID_MASK, adapter->active_vlans))
285 __vlan_hwaccel_put_tag(skb, tag);
287 napi_gro_receive(&q_vector->napi, skb);
291 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
292 * @adapter: address of board private structure
293 * @status_err: hardware indication of status of receive
294 * @skb: skb currently being received and modified
296 static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
297 struct ixgbevf_ring *ring,
298 u32 status_err, struct sk_buff *skb)
300 skb_checksum_none_assert(skb);
302 /* Rx csum disabled */
303 if (!(ring->netdev->features & NETIF_F_RXCSUM))
306 /* if IP and error */
307 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
308 (status_err & IXGBE_RXDADV_ERR_IPE)) {
309 adapter->hw_csum_rx_error++;
313 if (!(status_err & IXGBE_RXD_STAT_L4CS))
316 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
317 adapter->hw_csum_rx_error++;
321 /* It must be a TCP or UDP packet with a valid checksum */
322 skb->ip_summed = CHECKSUM_UNNECESSARY;
323 adapter->hw_csum_rx_good++;
327 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
328 * @adapter: address of board private structure
330 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
331 struct ixgbevf_ring *rx_ring,
334 struct pci_dev *pdev = adapter->pdev;
335 union ixgbe_adv_rx_desc *rx_desc;
336 struct ixgbevf_rx_buffer *bi;
338 unsigned int i = rx_ring->next_to_use;
340 bi = &rx_ring->rx_buffer_info[i];
342 while (cleaned_count--) {
343 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
346 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
347 rx_ring->rx_buf_len);
349 adapter->alloc_rx_buff_failed++;
356 bi->dma = dma_map_single(&pdev->dev, skb->data,
360 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
363 if (i == rx_ring->count)
365 bi = &rx_ring->rx_buffer_info[i];
369 if (rx_ring->next_to_use != i) {
370 rx_ring->next_to_use = i;
372 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
376 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
379 struct ixgbe_hw *hw = &adapter->hw;
381 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
384 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
385 struct ixgbevf_ring *rx_ring,
388 struct ixgbevf_adapter *adapter = q_vector->adapter;
389 struct pci_dev *pdev = adapter->pdev;
390 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
391 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
395 int cleaned_count = 0;
396 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
398 i = rx_ring->next_to_clean;
399 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
400 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
401 rx_buffer_info = &rx_ring->rx_buffer_info[i];
403 while (staterr & IXGBE_RXD_STAT_DD) {
408 rmb(); /* read descriptor and rx_buffer_info after status DD */
409 len = le16_to_cpu(rx_desc->wb.upper.length);
410 skb = rx_buffer_info->skb;
411 prefetch(skb->data - NET_IP_ALIGN);
412 rx_buffer_info->skb = NULL;
414 if (rx_buffer_info->dma) {
415 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
418 rx_buffer_info->dma = 0;
423 if (i == rx_ring->count)
426 next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
430 next_buffer = &rx_ring->rx_buffer_info[i];
432 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
433 skb->next = next_buffer->skb;
434 skb->next->prev = skb;
435 adapter->non_eop_descs++;
439 /* ERR_MASK will only have valid bits if EOP set */
440 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
441 dev_kfree_skb_irq(skb);
445 ixgbevf_rx_checksum(adapter, rx_ring, staterr, skb);
447 /* probably a little skewed due to removing CRC */
448 total_rx_bytes += skb->len;
452 * Work around issue of some types of VM to VM loop back
453 * packets not getting split correctly
455 if (staterr & IXGBE_RXD_STAT_LB) {
456 u32 header_fixup_len = skb_headlen(skb);
457 if (header_fixup_len < 14)
458 skb_push(skb, header_fixup_len);
460 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
462 ixgbevf_receive_skb(q_vector, skb, staterr, rx_ring, rx_desc);
465 rx_desc->wb.upper.status_error = 0;
467 /* return some buffers to hardware, one at a time is too slow */
468 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
469 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
474 /* use prefetched values */
476 rx_buffer_info = &rx_ring->rx_buffer_info[i];
478 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
481 rx_ring->next_to_clean = i;
482 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
485 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
487 u64_stats_update_begin(&rx_ring->syncp);
488 rx_ring->total_packets += total_rx_packets;
489 rx_ring->total_bytes += total_rx_bytes;
490 u64_stats_update_end(&rx_ring->syncp);
496 * ixgbevf_poll - NAPI polling calback
497 * @napi: napi struct with our devices info in it
498 * @budget: amount of work driver is allowed to do this pass, in packets
500 * This function will clean more than one or more rings associated with a
503 static int ixgbevf_poll(struct napi_struct *napi, int budget)
505 struct ixgbevf_q_vector *q_vector =
506 container_of(napi, struct ixgbevf_q_vector, napi);
507 struct ixgbevf_adapter *adapter = q_vector->adapter;
508 struct ixgbevf_ring *ring;
510 bool clean_complete = true;
512 ixgbevf_for_each_ring(ring, q_vector->tx)
513 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
515 /* attempt to distribute budget to each queue fairly, but don't allow
516 * the budget to go below 1 because we'll exit polling */
517 if (q_vector->rx.count > 1)
518 per_ring_budget = max(budget/q_vector->rx.count, 1);
520 per_ring_budget = budget;
522 ixgbevf_for_each_ring(ring, q_vector->rx)
523 clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
526 /* If all work not completed, return budget and keep polling */
529 /* all work done, exit the polling mode */
531 if (adapter->rx_itr_setting & 1)
532 ixgbevf_set_itr(q_vector);
533 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
534 ixgbevf_irq_enable_queues(adapter,
535 1 << q_vector->v_idx);
542 * ixgbevf_configure_msix - Configure MSI-X hardware
543 * @adapter: board private structure
545 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
548 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
550 struct ixgbevf_q_vector *q_vector;
551 int q_vectors, v_idx;
553 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
554 adapter->eims_enable_mask = 0;
557 * Populate the IVAR table and set the ITR values to the
558 * corresponding register.
560 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
561 struct ixgbevf_ring *ring;
562 q_vector = adapter->q_vector[v_idx];
564 ixgbevf_for_each_ring(ring, q_vector->rx)
565 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
567 ixgbevf_for_each_ring(ring, q_vector->tx)
568 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
570 if (q_vector->tx.ring && !q_vector->rx.ring) {
572 if (adapter->tx_itr_setting == 1)
573 q_vector->itr = IXGBE_10K_ITR;
575 q_vector->itr = adapter->tx_itr_setting;
577 /* rx or rx/tx vector */
578 if (adapter->rx_itr_setting == 1)
579 q_vector->itr = IXGBE_20K_ITR;
581 q_vector->itr = adapter->rx_itr_setting;
584 /* add q_vector eims value to global eims_enable_mask */
585 adapter->eims_enable_mask |= 1 << v_idx;
587 ixgbevf_write_eitr(q_vector);
590 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
591 /* setup eims_other and add value to global eims_enable_mask */
592 adapter->eims_other = 1 << v_idx;
593 adapter->eims_enable_mask |= adapter->eims_other;
600 latency_invalid = 255
604 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
605 * @q_vector: structure containing interrupt and ring information
606 * @ring_container: structure containing ring performance data
608 * Stores a new ITR value based on packets and byte
609 * counts during the last interrupt. The advantage of per interrupt
610 * computation is faster updates and more accurate ITR for the current
611 * traffic pattern. Constants in this function were computed
612 * based on theoretical maximum wire speed and thresholds were set based
613 * on testing data as well as attempting to minimize response time
614 * while increasing bulk throughput.
616 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
617 struct ixgbevf_ring_container *ring_container)
619 int bytes = ring_container->total_bytes;
620 int packets = ring_container->total_packets;
623 u8 itr_setting = ring_container->itr;
628 /* simple throttlerate management
629 * 0-20MB/s lowest (100000 ints/s)
630 * 20-100MB/s low (20000 ints/s)
631 * 100-1249MB/s bulk (8000 ints/s)
633 /* what was last interrupt timeslice? */
634 timepassed_us = q_vector->itr >> 2;
635 bytes_perint = bytes / timepassed_us; /* bytes/usec */
637 switch (itr_setting) {
639 if (bytes_perint > 10)
640 itr_setting = low_latency;
643 if (bytes_perint > 20)
644 itr_setting = bulk_latency;
645 else if (bytes_perint <= 10)
646 itr_setting = lowest_latency;
649 if (bytes_perint <= 20)
650 itr_setting = low_latency;
654 /* clear work counters since we have the values we need */
655 ring_container->total_bytes = 0;
656 ring_container->total_packets = 0;
658 /* write updated itr to ring container */
659 ring_container->itr = itr_setting;
663 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
664 * @q_vector: structure containing interrupt and ring information
666 * This function is made to be called by ethtool and by the driver
667 * when it needs to update VTEITR registers at runtime. Hardware
668 * specific quirks/differences are taken care of here.
670 void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
672 struct ixgbevf_adapter *adapter = q_vector->adapter;
673 struct ixgbe_hw *hw = &adapter->hw;
674 int v_idx = q_vector->v_idx;
675 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
678 * set the WDIS bit to not clear the timer bits and cause an
679 * immediate assertion of the interrupt
681 itr_reg |= IXGBE_EITR_CNT_WDIS;
683 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
686 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
688 u32 new_itr = q_vector->itr;
691 ixgbevf_update_itr(q_vector, &q_vector->tx);
692 ixgbevf_update_itr(q_vector, &q_vector->rx);
694 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
696 switch (current_itr) {
697 /* counts and packets in update_itr are dependent on these numbers */
699 new_itr = IXGBE_100K_ITR;
702 new_itr = IXGBE_20K_ITR;
706 new_itr = IXGBE_8K_ITR;
710 if (new_itr != q_vector->itr) {
711 /* do an exponential smoothing */
712 new_itr = (10 * new_itr * q_vector->itr) /
713 ((9 * new_itr) + q_vector->itr);
715 /* save the algorithm value here */
716 q_vector->itr = new_itr;
718 ixgbevf_write_eitr(q_vector);
722 static irqreturn_t ixgbevf_msix_mbx(int irq, void *data)
724 struct ixgbevf_adapter *adapter = data;
725 struct ixgbe_hw *hw = &adapter->hw;
727 bool got_ack = false;
729 if (!hw->mbx.ops.check_for_ack(hw))
732 if (!hw->mbx.ops.check_for_msg(hw)) {
733 hw->mbx.ops.read(hw, &msg, 1);
735 if ((msg & IXGBE_MBVFICR_VFREQ_MASK) == IXGBE_PF_CONTROL_MSG)
736 mod_timer(&adapter->watchdog_timer,
737 round_jiffies(jiffies + 1));
739 if (msg & IXGBE_VT_MSGTYPE_NACK)
740 pr_warn("Last Request of type %2.2x to PF Nacked\n",
743 * Restore the PFSTS bit in case someone is polling for a
744 * return message from the PF
746 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFSTS;
750 * checking for the ack clears the PFACK bit. Place
751 * it back in the v2p_mailbox cache so that anyone
752 * polling for an ack will not miss it
755 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFACK;
757 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
764 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
766 * @data: pointer to our q_vector struct for this interrupt vector
768 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
770 struct ixgbevf_q_vector *q_vector = data;
772 /* EIAM disabled interrupts (on this vector) for us */
773 if (q_vector->rx.ring || q_vector->tx.ring)
774 napi_schedule(&q_vector->napi);
779 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
782 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
784 a->rx_ring[r_idx].next = q_vector->rx.ring;
785 q_vector->rx.ring = &a->rx_ring[r_idx];
786 q_vector->rx.count++;
789 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
792 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
794 a->tx_ring[t_idx].next = q_vector->tx.ring;
795 q_vector->tx.ring = &a->tx_ring[t_idx];
796 q_vector->tx.count++;
800 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
801 * @adapter: board private structure to initialize
803 * This function maps descriptor rings to the queue-specific vectors
804 * we were allotted through the MSI-X enabling code. Ideally, we'd have
805 * one vector per ring/queue, but on a constrained vector budget, we
806 * group the rings as "efficiently" as possible. You would add new
807 * mapping configurations in here.
809 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
813 int rxr_idx = 0, txr_idx = 0;
814 int rxr_remaining = adapter->num_rx_queues;
815 int txr_remaining = adapter->num_tx_queues;
820 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
823 * The ideal configuration...
824 * We have enough vectors to map one per queue.
826 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
827 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
828 map_vector_to_rxq(adapter, v_start, rxr_idx);
830 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
831 map_vector_to_txq(adapter, v_start, txr_idx);
836 * If we don't have enough vectors for a 1-to-1
837 * mapping, we'll have to group them so there are
838 * multiple queues per vector.
840 /* Re-adjusting *qpv takes care of the remainder. */
841 for (i = v_start; i < q_vectors; i++) {
842 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
843 for (j = 0; j < rqpv; j++) {
844 map_vector_to_rxq(adapter, i, rxr_idx);
849 for (i = v_start; i < q_vectors; i++) {
850 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
851 for (j = 0; j < tqpv; j++) {
852 map_vector_to_txq(adapter, i, txr_idx);
863 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
864 * @adapter: board private structure
866 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
867 * interrupts from the kernel.
869 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
871 struct net_device *netdev = adapter->netdev;
872 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
876 for (vector = 0; vector < q_vectors; vector++) {
877 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
878 struct msix_entry *entry = &adapter->msix_entries[vector];
880 if (q_vector->tx.ring && q_vector->rx.ring) {
881 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
882 "%s-%s-%d", netdev->name, "TxRx", ri++);
884 } else if (q_vector->rx.ring) {
885 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
886 "%s-%s-%d", netdev->name, "rx", ri++);
887 } else if (q_vector->tx.ring) {
888 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
889 "%s-%s-%d", netdev->name, "tx", ti++);
891 /* skip this unused q_vector */
894 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
895 q_vector->name, q_vector);
898 "request_irq failed for MSIX interrupt "
900 goto free_queue_irqs;
904 err = request_irq(adapter->msix_entries[vector].vector,
905 &ixgbevf_msix_mbx, 0, netdev->name, adapter);
908 "request_irq for msix_mbx failed: %d\n", err);
909 goto free_queue_irqs;
917 free_irq(adapter->msix_entries[vector].vector,
918 adapter->q_vector[vector]);
920 pci_disable_msix(adapter->pdev);
921 kfree(adapter->msix_entries);
922 adapter->msix_entries = NULL;
926 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
928 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
930 for (i = 0; i < q_vectors; i++) {
931 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
932 q_vector->rx.ring = NULL;
933 q_vector->tx.ring = NULL;
934 q_vector->rx.count = 0;
935 q_vector->tx.count = 0;
940 * ixgbevf_request_irq - initialize interrupts
941 * @adapter: board private structure
943 * Attempts to configure interrupts using the best available
944 * capabilities of the hardware and kernel.
946 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
950 err = ixgbevf_request_msix_irqs(adapter);
954 "request_irq failed, Error %d\n", err);
959 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
963 q_vectors = adapter->num_msix_vectors;
966 free_irq(adapter->msix_entries[i].vector, adapter);
969 for (; i >= 0; i--) {
970 /* free only the irqs that were actually requested */
971 if (!adapter->q_vector[i]->rx.ring &&
972 !adapter->q_vector[i]->tx.ring)
975 free_irq(adapter->msix_entries[i].vector,
976 adapter->q_vector[i]);
979 ixgbevf_reset_q_vectors(adapter);
983 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
984 * @adapter: board private structure
986 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
988 struct ixgbe_hw *hw = &adapter->hw;
991 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
992 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
993 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
995 IXGBE_WRITE_FLUSH(hw);
997 for (i = 0; i < adapter->num_msix_vectors; i++)
998 synchronize_irq(adapter->msix_entries[i].vector);
1002 * ixgbevf_irq_enable - Enable default interrupt generation settings
1003 * @adapter: board private structure
1005 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1007 struct ixgbe_hw *hw = &adapter->hw;
1009 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1010 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1011 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1015 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1016 * @adapter: board private structure
1018 * Configure the Tx unit of the MAC after a reset.
1020 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1023 struct ixgbe_hw *hw = &adapter->hw;
1024 u32 i, j, tdlen, txctrl;
1026 /* Setup the HW Tx Head and Tail descriptor pointers */
1027 for (i = 0; i < adapter->num_tx_queues; i++) {
1028 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1031 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1032 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1033 (tdba & DMA_BIT_MASK(32)));
1034 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1035 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1036 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1037 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1038 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1039 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1040 /* Disable Tx Head Writeback RO bit, since this hoses
1041 * bookkeeping if things aren't delivered in order.
1043 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1044 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1045 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1049 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1051 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1053 struct ixgbevf_ring *rx_ring;
1054 struct ixgbe_hw *hw = &adapter->hw;
1057 rx_ring = &adapter->rx_ring[index];
1059 srrctl = IXGBE_SRRCTL_DROP_EN;
1061 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1063 if (rx_ring->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
1064 srrctl |= IXGBEVF_RXBUFFER_2048 >>
1065 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1067 srrctl |= rx_ring->rx_buf_len >>
1068 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1069 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1073 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1074 * @adapter: board private structure
1076 * Configure the Rx unit of the MAC after a reset.
1078 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1081 struct ixgbe_hw *hw = &adapter->hw;
1082 struct net_device *netdev = adapter->netdev;
1083 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1088 /* PSRTYPE must be initialized in 82599 */
1089 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
1090 if (netdev->mtu <= ETH_DATA_LEN)
1091 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1093 rx_buf_len = ALIGN(max_frame, 1024);
1095 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1096 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1097 * the Base and Length of the Rx Descriptor Ring */
1098 for (i = 0; i < adapter->num_rx_queues; i++) {
1099 rdba = adapter->rx_ring[i].dma;
1100 j = adapter->rx_ring[i].reg_idx;
1101 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1102 (rdba & DMA_BIT_MASK(32)));
1103 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1104 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1105 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1106 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1107 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1108 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1109 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1111 ixgbevf_configure_srrctl(adapter, j);
1115 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1117 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1118 struct ixgbe_hw *hw = &adapter->hw;
1120 /* add VID to filter table */
1121 if (hw->mac.ops.set_vfta)
1122 hw->mac.ops.set_vfta(hw, vid, 0, true);
1123 set_bit(vid, adapter->active_vlans);
1128 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1130 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1131 struct ixgbe_hw *hw = &adapter->hw;
1133 /* remove VID from filter table */
1134 if (hw->mac.ops.set_vfta)
1135 hw->mac.ops.set_vfta(hw, vid, 0, false);
1136 clear_bit(vid, adapter->active_vlans);
1141 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1145 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1146 ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
1149 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1151 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1152 struct ixgbe_hw *hw = &adapter->hw;
1155 if ((netdev_uc_count(netdev)) > 10) {
1156 pr_err("Too many unicast filters - No Space\n");
1160 if (!netdev_uc_empty(netdev)) {
1161 struct netdev_hw_addr *ha;
1162 netdev_for_each_uc_addr(ha, netdev) {
1163 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1168 * If the list is empty then send message to PF driver to
1169 * clear all macvlans on this VF.
1171 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1178 * ixgbevf_set_rx_mode - Multicast set
1179 * @netdev: network interface device structure
1181 * The set_rx_method entry point is called whenever the multicast address
1182 * list or the network interface flags are updated. This routine is
1183 * responsible for configuring the hardware for proper multicast mode.
1185 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1187 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1188 struct ixgbe_hw *hw = &adapter->hw;
1190 /* reprogram multicast list */
1191 if (hw->mac.ops.update_mc_addr_list)
1192 hw->mac.ops.update_mc_addr_list(hw, netdev);
1194 ixgbevf_write_uc_addr_list(netdev);
1197 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1200 struct ixgbevf_q_vector *q_vector;
1201 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1203 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1204 q_vector = adapter->q_vector[q_idx];
1205 napi_enable(&q_vector->napi);
1209 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1212 struct ixgbevf_q_vector *q_vector;
1213 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1215 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1216 q_vector = adapter->q_vector[q_idx];
1217 napi_disable(&q_vector->napi);
1221 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1223 struct net_device *netdev = adapter->netdev;
1226 ixgbevf_set_rx_mode(netdev);
1228 ixgbevf_restore_vlan(adapter);
1230 ixgbevf_configure_tx(adapter);
1231 ixgbevf_configure_rx(adapter);
1232 for (i = 0; i < adapter->num_rx_queues; i++) {
1233 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1234 ixgbevf_alloc_rx_buffers(adapter, ring,
1235 IXGBE_DESC_UNUSED(ring));
1239 #define IXGBE_MAX_RX_DESC_POLL 10
1240 static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1243 struct ixgbe_hw *hw = &adapter->hw;
1244 int j = adapter->rx_ring[rxr].reg_idx;
1247 for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1248 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1253 if (k >= IXGBE_MAX_RX_DESC_POLL) {
1254 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1255 "not set within the polling period\n", rxr);
1258 ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
1259 (adapter->rx_ring[rxr].count - 1));
1262 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1264 /* Only save pre-reset stats if there are some */
1265 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1266 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1267 adapter->stats.base_vfgprc;
1268 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1269 adapter->stats.base_vfgptc;
1270 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1271 adapter->stats.base_vfgorc;
1272 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1273 adapter->stats.base_vfgotc;
1274 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1275 adapter->stats.base_vfmprc;
1279 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1281 struct ixgbe_hw *hw = &adapter->hw;
1283 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1284 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1285 adapter->stats.last_vfgorc |=
1286 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1287 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1288 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1289 adapter->stats.last_vfgotc |=
1290 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1291 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1293 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1294 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1295 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1296 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1297 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1300 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1302 struct net_device *netdev = adapter->netdev;
1303 struct ixgbe_hw *hw = &adapter->hw;
1305 int num_rx_rings = adapter->num_rx_queues;
1309 for (i = 0; i < adapter->num_tx_queues; i++) {
1310 j = adapter->tx_ring[i].reg_idx;
1311 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1312 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1313 txdctl |= (8 << 16);
1314 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1317 for (i = 0; i < adapter->num_tx_queues; i++) {
1318 j = adapter->tx_ring[i].reg_idx;
1319 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1320 txdctl |= IXGBE_TXDCTL_ENABLE;
1321 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1324 for (i = 0; i < num_rx_rings; i++) {
1325 j = adapter->rx_ring[i].reg_idx;
1326 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1327 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1328 if (hw->mac.type == ixgbe_mac_X540_vf) {
1329 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1330 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1331 IXGBE_RXDCTL_RLPML_EN);
1333 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1334 ixgbevf_rx_desc_queue_enable(adapter, i);
1337 ixgbevf_configure_msix(adapter);
1339 if (hw->mac.ops.set_rar) {
1340 if (is_valid_ether_addr(hw->mac.addr))
1341 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1343 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1346 msg[0] = IXGBE_VF_SET_LPE;
1347 msg[1] = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1348 hw->mbx.ops.write_posted(hw, msg, 2);
1350 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1351 ixgbevf_napi_enable_all(adapter);
1353 /* enable transmits */
1354 netif_tx_start_all_queues(netdev);
1356 ixgbevf_save_reset_stats(adapter);
1357 ixgbevf_init_last_counter_stats(adapter);
1359 mod_timer(&adapter->watchdog_timer, jiffies);
1362 void ixgbevf_up(struct ixgbevf_adapter *adapter)
1364 struct ixgbe_hw *hw = &adapter->hw;
1366 ixgbevf_configure(adapter);
1368 ixgbevf_up_complete(adapter);
1370 /* clear any pending interrupts, may auto mask */
1371 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1373 ixgbevf_irq_enable(adapter);
1377 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1378 * @adapter: board private structure
1379 * @rx_ring: ring to free buffers from
1381 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1382 struct ixgbevf_ring *rx_ring)
1384 struct pci_dev *pdev = adapter->pdev;
1388 if (!rx_ring->rx_buffer_info)
1391 /* Free all the Rx ring sk_buffs */
1392 for (i = 0; i < rx_ring->count; i++) {
1393 struct ixgbevf_rx_buffer *rx_buffer_info;
1395 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1396 if (rx_buffer_info->dma) {
1397 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1398 rx_ring->rx_buf_len,
1400 rx_buffer_info->dma = 0;
1402 if (rx_buffer_info->skb) {
1403 struct sk_buff *skb = rx_buffer_info->skb;
1404 rx_buffer_info->skb = NULL;
1406 struct sk_buff *this = skb;
1408 dev_kfree_skb(this);
1413 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1414 memset(rx_ring->rx_buffer_info, 0, size);
1416 /* Zero out the descriptor ring */
1417 memset(rx_ring->desc, 0, rx_ring->size);
1419 rx_ring->next_to_clean = 0;
1420 rx_ring->next_to_use = 0;
1423 writel(0, adapter->hw.hw_addr + rx_ring->head);
1425 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1429 * ixgbevf_clean_tx_ring - Free Tx Buffers
1430 * @adapter: board private structure
1431 * @tx_ring: ring to be cleaned
1433 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1434 struct ixgbevf_ring *tx_ring)
1436 struct ixgbevf_tx_buffer *tx_buffer_info;
1440 if (!tx_ring->tx_buffer_info)
1443 /* Free all the Tx ring sk_buffs */
1445 for (i = 0; i < tx_ring->count; i++) {
1446 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1447 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
1450 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1451 memset(tx_ring->tx_buffer_info, 0, size);
1453 memset(tx_ring->desc, 0, tx_ring->size);
1455 tx_ring->next_to_use = 0;
1456 tx_ring->next_to_clean = 0;
1459 writel(0, adapter->hw.hw_addr + tx_ring->head);
1461 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1465 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1466 * @adapter: board private structure
1468 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1472 for (i = 0; i < adapter->num_rx_queues; i++)
1473 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1477 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1478 * @adapter: board private structure
1480 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1484 for (i = 0; i < adapter->num_tx_queues; i++)
1485 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1488 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1490 struct net_device *netdev = adapter->netdev;
1491 struct ixgbe_hw *hw = &adapter->hw;
1495 /* signal that we are down to the interrupt handler */
1496 set_bit(__IXGBEVF_DOWN, &adapter->state);
1497 /* disable receives */
1499 netif_tx_disable(netdev);
1503 netif_tx_stop_all_queues(netdev);
1505 ixgbevf_irq_disable(adapter);
1507 ixgbevf_napi_disable_all(adapter);
1509 del_timer_sync(&adapter->watchdog_timer);
1510 /* can't call flush scheduled work here because it can deadlock
1511 * if linkwatch_event tries to acquire the rtnl_lock which we are
1513 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1516 /* disable transmits in the hardware now that interrupts are off */
1517 for (i = 0; i < adapter->num_tx_queues; i++) {
1518 j = adapter->tx_ring[i].reg_idx;
1519 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1520 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1521 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1524 netif_carrier_off(netdev);
1526 if (!pci_channel_offline(adapter->pdev))
1527 ixgbevf_reset(adapter);
1529 ixgbevf_clean_all_tx_rings(adapter);
1530 ixgbevf_clean_all_rx_rings(adapter);
1533 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1535 struct ixgbe_hw *hw = &adapter->hw;
1537 WARN_ON(in_interrupt());
1539 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1543 * Check if PF is up before re-init. If not then skip until
1544 * later when the PF is up and ready to service requests from
1545 * the VF via mailbox. If the VF is up and running then the
1546 * watchdog task will continue to schedule reset tasks until
1547 * the PF is up and running.
1549 if (!hw->mac.ops.reset_hw(hw)) {
1550 ixgbevf_down(adapter);
1551 ixgbevf_up(adapter);
1554 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1557 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1559 struct ixgbe_hw *hw = &adapter->hw;
1560 struct net_device *netdev = adapter->netdev;
1562 if (hw->mac.ops.reset_hw(hw))
1563 hw_dbg(hw, "PF still resetting\n");
1565 hw->mac.ops.init_hw(hw);
1567 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1568 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1570 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1575 static void ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1578 int err, vector_threshold;
1580 /* We'll want at least 2 (vector_threshold):
1581 * 1) TxQ[0] + RxQ[0] handler
1582 * 2) Other (Link Status Change, etc.)
1584 vector_threshold = MIN_MSIX_COUNT;
1586 /* The more we get, the more we will assign to Tx/Rx Cleanup
1587 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1588 * Right now, we simply care about how many we'll get; we'll
1589 * set them up later while requesting irq's.
1591 while (vectors >= vector_threshold) {
1592 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1594 if (!err) /* Success in acquiring all requested vectors. */
1597 vectors = 0; /* Nasty failure, quit now */
1598 else /* err == number of vectors we should try again with */
1602 if (vectors < vector_threshold) {
1603 /* Can't allocate enough MSI-X interrupts? Oh well.
1604 * This just means we'll go with either a single MSI
1605 * vector or fall back to legacy interrupts.
1607 hw_dbg(&adapter->hw,
1608 "Unable to allocate MSI-X interrupts\n");
1609 kfree(adapter->msix_entries);
1610 adapter->msix_entries = NULL;
1613 * Adjust for only the vectors we'll use, which is minimum
1614 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1615 * vectors we were allocated.
1617 adapter->num_msix_vectors = vectors;
1622 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
1623 * @adapter: board private structure to initialize
1625 * This is the top level queue allocation routine. The order here is very
1626 * important, starting with the "most" number of features turned on at once,
1627 * and ending with the smallest set of features. This way large combinations
1628 * can be allocated if they're turned on, and smaller combinations are the
1629 * fallthrough conditions.
1632 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1634 /* Start with base case */
1635 adapter->num_rx_queues = 1;
1636 adapter->num_tx_queues = 1;
1640 * ixgbevf_alloc_queues - Allocate memory for all rings
1641 * @adapter: board private structure to initialize
1643 * We allocate one ring per queue at run-time since we don't know the
1644 * number of queues at compile-time. The polling_netdev array is
1645 * intended for Multiqueue, but should work fine with a single queue.
1647 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1651 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1652 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1653 if (!adapter->tx_ring)
1654 goto err_tx_ring_allocation;
1656 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1657 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1658 if (!adapter->rx_ring)
1659 goto err_rx_ring_allocation;
1661 for (i = 0; i < adapter->num_tx_queues; i++) {
1662 adapter->tx_ring[i].count = adapter->tx_ring_count;
1663 adapter->tx_ring[i].queue_index = i;
1664 adapter->tx_ring[i].reg_idx = i;
1665 adapter->tx_ring[i].dev = &adapter->pdev->dev;
1666 adapter->tx_ring[i].netdev = adapter->netdev;
1669 for (i = 0; i < adapter->num_rx_queues; i++) {
1670 adapter->rx_ring[i].count = adapter->rx_ring_count;
1671 adapter->rx_ring[i].queue_index = i;
1672 adapter->rx_ring[i].reg_idx = i;
1673 adapter->rx_ring[i].dev = &adapter->pdev->dev;
1674 adapter->rx_ring[i].netdev = adapter->netdev;
1679 err_rx_ring_allocation:
1680 kfree(adapter->tx_ring);
1681 err_tx_ring_allocation:
1686 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1687 * @adapter: board private structure to initialize
1689 * Attempt to configure the interrupts using the best available
1690 * capabilities of the hardware and the kernel.
1692 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1695 int vector, v_budget;
1698 * It's easy to be greedy for MSI-X vectors, but it really
1699 * doesn't do us much good if we have a lot more vectors
1700 * than CPU's. So let's be conservative and only ask for
1701 * (roughly) the same number of vectors as there are CPU's.
1702 * The default is to use pairs of vectors.
1704 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
1705 v_budget = min_t(int, v_budget, num_online_cpus());
1706 v_budget += NON_Q_VECTORS;
1708 /* A failure in MSI-X entry allocation isn't fatal, but it does
1709 * mean we disable MSI-X capabilities of the adapter. */
1710 adapter->msix_entries = kcalloc(v_budget,
1711 sizeof(struct msix_entry), GFP_KERNEL);
1712 if (!adapter->msix_entries) {
1717 for (vector = 0; vector < v_budget; vector++)
1718 adapter->msix_entries[vector].entry = vector;
1720 ixgbevf_acquire_msix_vectors(adapter, v_budget);
1727 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1728 * @adapter: board private structure to initialize
1730 * We allocate one q_vector per queue interrupt. If allocation fails we
1733 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1735 int q_idx, num_q_vectors;
1736 struct ixgbevf_q_vector *q_vector;
1738 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1740 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1741 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1744 q_vector->adapter = adapter;
1745 q_vector->v_idx = q_idx;
1746 netif_napi_add(adapter->netdev, &q_vector->napi,
1748 adapter->q_vector[q_idx] = q_vector;
1756 q_vector = adapter->q_vector[q_idx];
1757 netif_napi_del(&q_vector->napi);
1759 adapter->q_vector[q_idx] = NULL;
1765 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1766 * @adapter: board private structure to initialize
1768 * This function frees the memory allocated to the q_vectors. In addition if
1769 * NAPI is enabled it will delete any references to the NAPI struct prior
1770 * to freeing the q_vector.
1772 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1774 int q_idx, num_q_vectors;
1777 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1778 napi_vectors = adapter->num_rx_queues;
1780 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1781 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1783 adapter->q_vector[q_idx] = NULL;
1784 if (q_idx < napi_vectors)
1785 netif_napi_del(&q_vector->napi);
1791 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1792 * @adapter: board private structure
1795 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1797 pci_disable_msix(adapter->pdev);
1798 kfree(adapter->msix_entries);
1799 adapter->msix_entries = NULL;
1803 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1804 * @adapter: board private structure to initialize
1807 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
1811 /* Number of supported queues */
1812 ixgbevf_set_num_queues(adapter);
1814 err = ixgbevf_set_interrupt_capability(adapter);
1816 hw_dbg(&adapter->hw,
1817 "Unable to setup interrupt capabilities\n");
1818 goto err_set_interrupt;
1821 err = ixgbevf_alloc_q_vectors(adapter);
1823 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
1825 goto err_alloc_q_vectors;
1828 err = ixgbevf_alloc_queues(adapter);
1830 pr_err("Unable to allocate memory for queues\n");
1831 goto err_alloc_queues;
1834 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
1835 "Tx Queue count = %u\n",
1836 (adapter->num_rx_queues > 1) ? "Enabled" :
1837 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
1839 set_bit(__IXGBEVF_DOWN, &adapter->state);
1843 ixgbevf_free_q_vectors(adapter);
1844 err_alloc_q_vectors:
1845 ixgbevf_reset_interrupt_capability(adapter);
1851 * ixgbevf_sw_init - Initialize general software structures
1852 * (struct ixgbevf_adapter)
1853 * @adapter: board private structure to initialize
1855 * ixgbevf_sw_init initializes the Adapter private data structure.
1856 * Fields are initialized based on PCI device information and
1857 * OS network device settings (MTU size).
1859 static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
1861 struct ixgbe_hw *hw = &adapter->hw;
1862 struct pci_dev *pdev = adapter->pdev;
1865 /* PCI config space info */
1867 hw->vendor_id = pdev->vendor;
1868 hw->device_id = pdev->device;
1869 hw->revision_id = pdev->revision;
1870 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1871 hw->subsystem_device_id = pdev->subsystem_device;
1873 hw->mbx.ops.init_params(hw);
1874 hw->mac.max_tx_queues = MAX_TX_QUEUES;
1875 hw->mac.max_rx_queues = MAX_RX_QUEUES;
1876 err = hw->mac.ops.reset_hw(hw);
1878 dev_info(&pdev->dev,
1879 "PF still in reset state, assigning new address\n");
1880 eth_hw_addr_random(adapter->netdev);
1881 memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
1882 adapter->netdev->addr_len);
1884 err = hw->mac.ops.init_hw(hw);
1886 pr_err("init_shared_code failed: %d\n", err);
1889 memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
1890 adapter->netdev->addr_len);
1893 /* Enable dynamic interrupt throttling rates */
1894 adapter->rx_itr_setting = 1;
1895 adapter->tx_itr_setting = 1;
1897 /* set default ring sizes */
1898 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
1899 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
1901 set_bit(__IXGBEVF_DOWN, &adapter->state);
1908 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
1910 u32 current_counter = IXGBE_READ_REG(hw, reg); \
1911 if (current_counter < last_counter) \
1912 counter += 0x100000000LL; \
1913 last_counter = current_counter; \
1914 counter &= 0xFFFFFFFF00000000LL; \
1915 counter |= current_counter; \
1918 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
1920 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
1921 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
1922 u64 current_counter = (current_counter_msb << 32) | \
1923 current_counter_lsb; \
1924 if (current_counter < last_counter) \
1925 counter += 0x1000000000LL; \
1926 last_counter = current_counter; \
1927 counter &= 0xFFFFFFF000000000LL; \
1928 counter |= current_counter; \
1931 * ixgbevf_update_stats - Update the board statistics counters.
1932 * @adapter: board private structure
1934 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
1936 struct ixgbe_hw *hw = &adapter->hw;
1938 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
1939 adapter->stats.vfgprc);
1940 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
1941 adapter->stats.vfgptc);
1942 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
1943 adapter->stats.last_vfgorc,
1944 adapter->stats.vfgorc);
1945 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
1946 adapter->stats.last_vfgotc,
1947 adapter->stats.vfgotc);
1948 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
1949 adapter->stats.vfmprc);
1953 * ixgbevf_watchdog - Timer Call-back
1954 * @data: pointer to adapter cast into an unsigned long
1956 static void ixgbevf_watchdog(unsigned long data)
1958 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
1959 struct ixgbe_hw *hw = &adapter->hw;
1964 * Do the watchdog outside of interrupt context due to the lovely
1965 * delays that some of the newer hardware requires
1968 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
1969 goto watchdog_short_circuit;
1971 /* get one bit for every active tx/rx interrupt vector */
1972 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
1973 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
1974 if (qv->rx.ring || qv->tx.ring)
1978 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
1980 watchdog_short_circuit:
1981 schedule_work(&adapter->watchdog_task);
1985 * ixgbevf_tx_timeout - Respond to a Tx Hang
1986 * @netdev: network interface device structure
1988 static void ixgbevf_tx_timeout(struct net_device *netdev)
1990 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1992 /* Do the reset outside of interrupt context */
1993 schedule_work(&adapter->reset_task);
1996 static void ixgbevf_reset_task(struct work_struct *work)
1998 struct ixgbevf_adapter *adapter;
1999 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2001 /* If we're already down or resetting, just bail */
2002 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2003 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2006 adapter->tx_timeout_count++;
2008 ixgbevf_reinit_locked(adapter);
2012 * ixgbevf_watchdog_task - worker thread to bring link up
2013 * @work: pointer to work_struct containing our data
2015 static void ixgbevf_watchdog_task(struct work_struct *work)
2017 struct ixgbevf_adapter *adapter = container_of(work,
2018 struct ixgbevf_adapter,
2020 struct net_device *netdev = adapter->netdev;
2021 struct ixgbe_hw *hw = &adapter->hw;
2022 u32 link_speed = adapter->link_speed;
2023 bool link_up = adapter->link_up;
2025 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2028 * Always check the link on the watchdog because we have
2031 if (hw->mac.ops.check_link) {
2032 if ((hw->mac.ops.check_link(hw, &link_speed,
2033 &link_up, false)) != 0) {
2034 adapter->link_up = link_up;
2035 adapter->link_speed = link_speed;
2036 netif_carrier_off(netdev);
2037 netif_tx_stop_all_queues(netdev);
2038 schedule_work(&adapter->reset_task);
2042 /* always assume link is up, if no check link
2044 link_speed = IXGBE_LINK_SPEED_10GB_FULL;
2047 adapter->link_up = link_up;
2048 adapter->link_speed = link_speed;
2051 if (!netif_carrier_ok(netdev)) {
2052 hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
2053 (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2055 netif_carrier_on(netdev);
2056 netif_tx_wake_all_queues(netdev);
2059 adapter->link_up = false;
2060 adapter->link_speed = 0;
2061 if (netif_carrier_ok(netdev)) {
2062 hw_dbg(&adapter->hw, "NIC Link is Down\n");
2063 netif_carrier_off(netdev);
2064 netif_tx_stop_all_queues(netdev);
2068 ixgbevf_update_stats(adapter);
2071 /* Reset the timer */
2072 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2073 mod_timer(&adapter->watchdog_timer,
2074 round_jiffies(jiffies + (2 * HZ)));
2076 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2080 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2081 * @adapter: board private structure
2082 * @tx_ring: Tx descriptor ring for a specific queue
2084 * Free all transmit software resources
2086 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2087 struct ixgbevf_ring *tx_ring)
2089 struct pci_dev *pdev = adapter->pdev;
2091 ixgbevf_clean_tx_ring(adapter, tx_ring);
2093 vfree(tx_ring->tx_buffer_info);
2094 tx_ring->tx_buffer_info = NULL;
2096 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2099 tx_ring->desc = NULL;
2103 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2104 * @adapter: board private structure
2106 * Free all transmit software resources
2108 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2112 for (i = 0; i < adapter->num_tx_queues; i++)
2113 if (adapter->tx_ring[i].desc)
2114 ixgbevf_free_tx_resources(adapter,
2115 &adapter->tx_ring[i]);
2120 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2121 * @adapter: board private structure
2122 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2124 * Return 0 on success, negative on failure
2126 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2127 struct ixgbevf_ring *tx_ring)
2129 struct pci_dev *pdev = adapter->pdev;
2132 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2133 tx_ring->tx_buffer_info = vzalloc(size);
2134 if (!tx_ring->tx_buffer_info)
2137 /* round up to nearest 4K */
2138 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2139 tx_ring->size = ALIGN(tx_ring->size, 4096);
2141 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2142 &tx_ring->dma, GFP_KERNEL);
2146 tx_ring->next_to_use = 0;
2147 tx_ring->next_to_clean = 0;
2151 vfree(tx_ring->tx_buffer_info);
2152 tx_ring->tx_buffer_info = NULL;
2153 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2154 "descriptor ring\n");
2159 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2160 * @adapter: board private structure
2162 * If this function returns with an error, then it's possible one or
2163 * more of the rings is populated (while the rest are not). It is the
2164 * callers duty to clean those orphaned rings.
2166 * Return 0 on success, negative on failure
2168 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2172 for (i = 0; i < adapter->num_tx_queues; i++) {
2173 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2176 hw_dbg(&adapter->hw,
2177 "Allocation for Tx Queue %u failed\n", i);
2185 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2186 * @adapter: board private structure
2187 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2189 * Returns 0 on success, negative on failure
2191 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2192 struct ixgbevf_ring *rx_ring)
2194 struct pci_dev *pdev = adapter->pdev;
2197 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2198 rx_ring->rx_buffer_info = vzalloc(size);
2199 if (!rx_ring->rx_buffer_info)
2202 /* Round up to nearest 4K */
2203 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2204 rx_ring->size = ALIGN(rx_ring->size, 4096);
2206 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2207 &rx_ring->dma, GFP_KERNEL);
2209 if (!rx_ring->desc) {
2210 hw_dbg(&adapter->hw,
2211 "Unable to allocate memory for "
2212 "the receive descriptor ring\n");
2213 vfree(rx_ring->rx_buffer_info);
2214 rx_ring->rx_buffer_info = NULL;
2218 rx_ring->next_to_clean = 0;
2219 rx_ring->next_to_use = 0;
2227 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2228 * @adapter: board private structure
2230 * If this function returns with an error, then it's possible one or
2231 * more of the rings is populated (while the rest are not). It is the
2232 * callers duty to clean those orphaned rings.
2234 * Return 0 on success, negative on failure
2236 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2240 for (i = 0; i < adapter->num_rx_queues; i++) {
2241 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2244 hw_dbg(&adapter->hw,
2245 "Allocation for Rx Queue %u failed\n", i);
2252 * ixgbevf_free_rx_resources - Free Rx Resources
2253 * @adapter: board private structure
2254 * @rx_ring: ring to clean the resources from
2256 * Free all receive software resources
2258 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2259 struct ixgbevf_ring *rx_ring)
2261 struct pci_dev *pdev = adapter->pdev;
2263 ixgbevf_clean_rx_ring(adapter, rx_ring);
2265 vfree(rx_ring->rx_buffer_info);
2266 rx_ring->rx_buffer_info = NULL;
2268 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2271 rx_ring->desc = NULL;
2275 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2276 * @adapter: board private structure
2278 * Free all receive software resources
2280 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2284 for (i = 0; i < adapter->num_rx_queues; i++)
2285 if (adapter->rx_ring[i].desc)
2286 ixgbevf_free_rx_resources(adapter,
2287 &adapter->rx_ring[i]);
2291 * ixgbevf_open - Called when a network interface is made active
2292 * @netdev: network interface device structure
2294 * Returns 0 on success, negative value on failure
2296 * The open entry point is called when a network interface is made
2297 * active by the system (IFF_UP). At this point all resources needed
2298 * for transmit and receive operations are allocated, the interrupt
2299 * handler is registered with the OS, the watchdog timer is started,
2300 * and the stack is notified that the interface is ready.
2302 static int ixgbevf_open(struct net_device *netdev)
2304 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2305 struct ixgbe_hw *hw = &adapter->hw;
2308 /* disallow open during test */
2309 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2312 if (hw->adapter_stopped) {
2313 ixgbevf_reset(adapter);
2314 /* if adapter is still stopped then PF isn't up and
2315 * the vf can't start. */
2316 if (hw->adapter_stopped) {
2317 err = IXGBE_ERR_MBX;
2318 pr_err("Unable to start - perhaps the PF Driver isn't "
2320 goto err_setup_reset;
2324 /* allocate transmit descriptors */
2325 err = ixgbevf_setup_all_tx_resources(adapter);
2329 /* allocate receive descriptors */
2330 err = ixgbevf_setup_all_rx_resources(adapter);
2334 ixgbevf_configure(adapter);
2337 * Map the Tx/Rx rings to the vectors we were allotted.
2338 * if request_irq will be called in this function map_rings
2339 * must be called *before* up_complete
2341 ixgbevf_map_rings_to_vectors(adapter);
2343 ixgbevf_up_complete(adapter);
2345 /* clear any pending interrupts, may auto mask */
2346 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2347 err = ixgbevf_request_irq(adapter);
2351 ixgbevf_irq_enable(adapter);
2356 ixgbevf_down(adapter);
2357 ixgbevf_free_irq(adapter);
2359 ixgbevf_free_all_rx_resources(adapter);
2361 ixgbevf_free_all_tx_resources(adapter);
2362 ixgbevf_reset(adapter);
2370 * ixgbevf_close - Disables a network interface
2371 * @netdev: network interface device structure
2373 * Returns 0, this is not allowed to fail
2375 * The close entry point is called when an interface is de-activated
2376 * by the OS. The hardware is still under the drivers control, but
2377 * needs to be disabled. A global MAC reset is issued to stop the
2378 * hardware, and all transmit and receive resources are freed.
2380 static int ixgbevf_close(struct net_device *netdev)
2382 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2384 ixgbevf_down(adapter);
2385 ixgbevf_free_irq(adapter);
2387 ixgbevf_free_all_tx_resources(adapter);
2388 ixgbevf_free_all_rx_resources(adapter);
2393 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2394 u32 vlan_macip_lens, u32 type_tucmd,
2397 struct ixgbe_adv_tx_context_desc *context_desc;
2398 u16 i = tx_ring->next_to_use;
2400 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2403 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2405 /* set bits to identify this as an advanced context descriptor */
2406 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2408 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2409 context_desc->seqnum_seed = 0;
2410 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2411 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2414 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
2415 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2417 u32 vlan_macip_lens, type_tucmd;
2418 u32 mss_l4len_idx, l4len;
2420 if (!skb_is_gso(skb))
2423 if (skb_header_cloned(skb)) {
2424 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2429 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2430 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2432 if (skb->protocol == htons(ETH_P_IP)) {
2433 struct iphdr *iph = ip_hdr(skb);
2436 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2440 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2441 } else if (skb_is_gso_v6(skb)) {
2442 ipv6_hdr(skb)->payload_len = 0;
2443 tcp_hdr(skb)->check =
2444 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2445 &ipv6_hdr(skb)->daddr,
2449 /* compute header lengths */
2450 l4len = tcp_hdrlen(skb);
2452 *hdr_len = skb_transport_offset(skb) + l4len;
2454 /* mss_l4len_id: use 1 as index for TSO */
2455 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2456 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2457 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2459 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2460 vlan_macip_lens = skb_network_header_len(skb);
2461 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2462 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2464 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2465 type_tucmd, mss_l4len_idx);
2470 static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2471 struct sk_buff *skb, u32 tx_flags)
2476 u32 vlan_macip_lens = 0;
2477 u32 mss_l4len_idx = 0;
2480 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2482 switch (skb->protocol) {
2483 case __constant_htons(ETH_P_IP):
2484 vlan_macip_lens |= skb_network_header_len(skb);
2485 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2486 l4_hdr = ip_hdr(skb)->protocol;
2488 case __constant_htons(ETH_P_IPV6):
2489 vlan_macip_lens |= skb_network_header_len(skb);
2490 l4_hdr = ipv6_hdr(skb)->nexthdr;
2493 if (unlikely(net_ratelimit())) {
2494 dev_warn(tx_ring->dev,
2495 "partial checksum but proto=%x!\n",
2503 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2504 mss_l4len_idx = tcp_hdrlen(skb) <<
2505 IXGBE_ADVTXD_L4LEN_SHIFT;
2508 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2509 mss_l4len_idx = sizeof(struct sctphdr) <<
2510 IXGBE_ADVTXD_L4LEN_SHIFT;
2513 mss_l4len_idx = sizeof(struct udphdr) <<
2514 IXGBE_ADVTXD_L4LEN_SHIFT;
2517 if (unlikely(net_ratelimit())) {
2518 dev_warn(tx_ring->dev,
2519 "partial checksum but l4 proto=%x!\n",
2526 /* vlan_macip_lens: MACLEN, VLAN tag */
2527 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2528 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2530 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2531 type_tucmd, mss_l4len_idx);
2533 return (skb->ip_summed == CHECKSUM_PARTIAL);
2536 static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
2537 struct sk_buff *skb, u32 tx_flags,
2540 struct ixgbevf_tx_buffer *tx_buffer_info;
2542 unsigned int total = skb->len;
2543 unsigned int offset = 0, size;
2545 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2549 i = tx_ring->next_to_use;
2551 len = min(skb_headlen(skb), total);
2553 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2554 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2556 tx_buffer_info->length = size;
2557 tx_buffer_info->mapped_as_page = false;
2558 tx_buffer_info->dma = dma_map_single(tx_ring->dev,
2560 size, DMA_TO_DEVICE);
2561 if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
2563 tx_buffer_info->next_to_watch = i;
2570 if (i == tx_ring->count)
2574 for (f = 0; f < nr_frags; f++) {
2575 const struct skb_frag_struct *frag;
2577 frag = &skb_shinfo(skb)->frags[f];
2578 len = min((unsigned int)skb_frag_size(frag), total);
2582 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2583 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2585 tx_buffer_info->length = size;
2586 tx_buffer_info->dma =
2587 skb_frag_dma_map(tx_ring->dev, frag,
2588 offset, size, DMA_TO_DEVICE);
2589 tx_buffer_info->mapped_as_page = true;
2590 if (dma_mapping_error(tx_ring->dev,
2591 tx_buffer_info->dma))
2593 tx_buffer_info->next_to_watch = i;
2600 if (i == tx_ring->count)
2608 i = tx_ring->count - 1;
2611 tx_ring->tx_buffer_info[i].skb = skb;
2612 tx_ring->tx_buffer_info[first].next_to_watch = i;
2613 tx_ring->tx_buffer_info[first].time_stamp = jiffies;
2618 dev_err(tx_ring->dev, "TX DMA map failed\n");
2620 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2621 tx_buffer_info->dma = 0;
2622 tx_buffer_info->next_to_watch = 0;
2625 /* clear timestamp and dma mappings for remaining portion of packet */
2626 while (count >= 0) {
2630 i += tx_ring->count;
2631 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2632 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
2638 static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
2639 int count, u32 paylen, u8 hdr_len)
2641 union ixgbe_adv_tx_desc *tx_desc = NULL;
2642 struct ixgbevf_tx_buffer *tx_buffer_info;
2643 u32 olinfo_status = 0, cmd_type_len = 0;
2646 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2648 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2650 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2652 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2653 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2655 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2656 olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
2658 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2659 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2661 /* use index 1 context for tso */
2662 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2663 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2664 olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
2669 * Check Context must be set if Tx switch is enabled, which it
2670 * always is for case where virtual functions are running
2672 olinfo_status |= IXGBE_ADVTXD_CC;
2674 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2676 i = tx_ring->next_to_use;
2678 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2679 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2680 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2681 tx_desc->read.cmd_type_len =
2682 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2683 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2685 if (i == tx_ring->count)
2689 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2691 tx_ring->next_to_use = i;
2694 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2696 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
2698 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
2699 /* Herbert's original patch had:
2700 * smp_mb__after_netif_stop_queue();
2701 * but since that doesn't exist yet, just open code it. */
2704 /* We need to check again in a case another CPU has just
2705 * made room available. */
2706 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
2709 /* A reprieve! - use start_queue because it doesn't call schedule */
2710 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2711 ++adapter->restart_queue;
2715 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2717 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
2719 return __ixgbevf_maybe_stop_tx(tx_ring, size);
2722 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2724 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2725 struct ixgbevf_ring *tx_ring;
2727 unsigned int tx_flags = 0;
2730 u16 count = TXD_USE_COUNT(skb_headlen(skb));
2731 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2735 tx_ring = &adapter->tx_ring[r_idx];
2738 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
2739 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
2740 * + 2 desc gap to keep tail from touching head,
2741 * + 1 desc for context descriptor,
2742 * otherwise try next time
2744 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2745 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2746 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
2748 count += skb_shinfo(skb)->nr_frags;
2750 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
2752 return NETDEV_TX_BUSY;
2755 if (vlan_tx_tag_present(skb)) {
2756 tx_flags |= vlan_tx_tag_get(skb);
2757 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
2758 tx_flags |= IXGBE_TX_FLAGS_VLAN;
2761 first = tx_ring->next_to_use;
2763 if (skb->protocol == htons(ETH_P_IP))
2764 tx_flags |= IXGBE_TX_FLAGS_IPV4;
2765 tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
2767 dev_kfree_skb_any(skb);
2768 return NETDEV_TX_OK;
2772 tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
2773 else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
2774 tx_flags |= IXGBE_TX_FLAGS_CSUM;
2776 ixgbevf_tx_queue(tx_ring, tx_flags,
2777 ixgbevf_tx_map(tx_ring, skb, tx_flags, first),
2780 * Force memory writes to complete before letting h/w
2781 * know there are new descriptors to fetch. (Only
2782 * applicable for weak-ordered memory model archs,
2787 writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
2789 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
2791 return NETDEV_TX_OK;
2795 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
2796 * @netdev: network interface device structure
2797 * @p: pointer to an address structure
2799 * Returns 0 on success, negative on failure
2801 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
2803 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2804 struct ixgbe_hw *hw = &adapter->hw;
2805 struct sockaddr *addr = p;
2807 if (!is_valid_ether_addr(addr->sa_data))
2808 return -EADDRNOTAVAIL;
2810 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2811 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
2813 if (hw->mac.ops.set_rar)
2814 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
2820 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
2821 * @netdev: network interface device structure
2822 * @new_mtu: new value for maximum frame size
2824 * Returns 0 on success, negative on failure
2826 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
2828 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2829 struct ixgbe_hw *hw = &adapter->hw;
2830 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2831 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
2834 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
2835 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
2837 /* MTU < 68 is an error and causes problems on some kernels */
2838 if ((new_mtu < 68) || (max_frame > max_possible_frame))
2841 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
2842 netdev->mtu, new_mtu);
2843 /* must set new MTU before calling down or up */
2844 netdev->mtu = new_mtu;
2846 if (!netif_running(netdev)) {
2847 msg[0] = IXGBE_VF_SET_LPE;
2849 hw->mbx.ops.write_posted(hw, msg, 2);
2852 if (netif_running(netdev))
2853 ixgbevf_reinit_locked(adapter);
2858 static void ixgbevf_shutdown(struct pci_dev *pdev)
2860 struct net_device *netdev = pci_get_drvdata(pdev);
2861 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2863 netif_device_detach(netdev);
2865 if (netif_running(netdev)) {
2866 ixgbevf_down(adapter);
2867 ixgbevf_free_irq(adapter);
2868 ixgbevf_free_all_tx_resources(adapter);
2869 ixgbevf_free_all_rx_resources(adapter);
2872 pci_save_state(pdev);
2874 pci_disable_device(pdev);
2877 static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
2878 struct rtnl_link_stats64 *stats)
2880 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2883 const struct ixgbevf_ring *ring;
2886 ixgbevf_update_stats(adapter);
2888 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
2890 for (i = 0; i < adapter->num_rx_queues; i++) {
2891 ring = &adapter->rx_ring[i];
2893 start = u64_stats_fetch_begin_bh(&ring->syncp);
2894 bytes = ring->total_bytes;
2895 packets = ring->total_packets;
2896 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
2897 stats->rx_bytes += bytes;
2898 stats->rx_packets += packets;
2901 for (i = 0; i < adapter->num_tx_queues; i++) {
2902 ring = &adapter->tx_ring[i];
2904 start = u64_stats_fetch_begin_bh(&ring->syncp);
2905 bytes = ring->total_bytes;
2906 packets = ring->total_packets;
2907 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
2908 stats->tx_bytes += bytes;
2909 stats->tx_packets += packets;
2915 static const struct net_device_ops ixgbe_netdev_ops = {
2916 .ndo_open = ixgbevf_open,
2917 .ndo_stop = ixgbevf_close,
2918 .ndo_start_xmit = ixgbevf_xmit_frame,
2919 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
2920 .ndo_get_stats64 = ixgbevf_get_stats,
2921 .ndo_validate_addr = eth_validate_addr,
2922 .ndo_set_mac_address = ixgbevf_set_mac,
2923 .ndo_change_mtu = ixgbevf_change_mtu,
2924 .ndo_tx_timeout = ixgbevf_tx_timeout,
2925 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
2926 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
2929 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
2931 dev->netdev_ops = &ixgbe_netdev_ops;
2932 ixgbevf_set_ethtool_ops(dev);
2933 dev->watchdog_timeo = 5 * HZ;
2937 * ixgbevf_probe - Device Initialization Routine
2938 * @pdev: PCI device information struct
2939 * @ent: entry in ixgbevf_pci_tbl
2941 * Returns 0 on success, negative on failure
2943 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
2944 * The OS initialization, configuring of the adapter private structure,
2945 * and a hardware reset occur.
2947 static int __devinit ixgbevf_probe(struct pci_dev *pdev,
2948 const struct pci_device_id *ent)
2950 struct net_device *netdev;
2951 struct ixgbevf_adapter *adapter = NULL;
2952 struct ixgbe_hw *hw = NULL;
2953 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
2954 static int cards_found;
2955 int err, pci_using_dac;
2957 err = pci_enable_device(pdev);
2961 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
2962 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
2965 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
2967 err = dma_set_coherent_mask(&pdev->dev,
2970 dev_err(&pdev->dev, "No usable DMA "
2971 "configuration, aborting\n");
2978 err = pci_request_regions(pdev, ixgbevf_driver_name);
2980 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
2984 pci_set_master(pdev);
2986 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
2990 goto err_alloc_etherdev;
2993 SET_NETDEV_DEV(netdev, &pdev->dev);
2995 pci_set_drvdata(pdev, netdev);
2996 adapter = netdev_priv(netdev);
2998 adapter->netdev = netdev;
2999 adapter->pdev = pdev;
3002 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3005 * call save state here in standalone driver because it relies on
3006 * adapter struct to exist, and needs to call netdev_priv
3008 pci_save_state(pdev);
3010 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3011 pci_resource_len(pdev, 0));
3017 ixgbevf_assign_netdev_ops(netdev);
3019 adapter->bd_number = cards_found;
3022 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3023 hw->mac.type = ii->mac;
3025 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3026 sizeof(struct ixgbe_mbx_operations));
3028 /* setup the private structure */
3029 err = ixgbevf_sw_init(adapter);
3033 /* The HW MAC address was set and/or determined in sw_init */
3034 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3036 if (!is_valid_ether_addr(netdev->dev_addr)) {
3037 pr_err("invalid MAC address\n");
3042 netdev->hw_features = NETIF_F_SG |
3049 netdev->features = netdev->hw_features |
3050 NETIF_F_HW_VLAN_TX |
3051 NETIF_F_HW_VLAN_RX |
3052 NETIF_F_HW_VLAN_FILTER;
3054 netdev->vlan_features |= NETIF_F_TSO;
3055 netdev->vlan_features |= NETIF_F_TSO6;
3056 netdev->vlan_features |= NETIF_F_IP_CSUM;
3057 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3058 netdev->vlan_features |= NETIF_F_SG;
3061 netdev->features |= NETIF_F_HIGHDMA;
3063 netdev->priv_flags |= IFF_UNICAST_FLT;
3065 init_timer(&adapter->watchdog_timer);
3066 adapter->watchdog_timer.function = ixgbevf_watchdog;
3067 adapter->watchdog_timer.data = (unsigned long)adapter;
3069 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3070 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3072 err = ixgbevf_init_interrupt_scheme(adapter);
3076 /* pick up the PCI bus settings for reporting later */
3077 if (hw->mac.ops.get_bus_info)
3078 hw->mac.ops.get_bus_info(hw);
3080 strcpy(netdev->name, "eth%d");
3082 err = register_netdev(netdev);
3086 netif_carrier_off(netdev);
3088 ixgbevf_init_last_counter_stats(adapter);
3090 /* print the MAC address */
3091 hw_dbg(hw, "%pM\n", netdev->dev_addr);
3093 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3095 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3101 ixgbevf_reset_interrupt_capability(adapter);
3102 iounmap(hw->hw_addr);
3104 free_netdev(netdev);
3106 pci_release_regions(pdev);
3109 pci_disable_device(pdev);
3114 * ixgbevf_remove - Device Removal Routine
3115 * @pdev: PCI device information struct
3117 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3118 * that it should release a PCI device. The could be caused by a
3119 * Hot-Plug event, or because the driver is going to be removed from
3122 static void __devexit ixgbevf_remove(struct pci_dev *pdev)
3124 struct net_device *netdev = pci_get_drvdata(pdev);
3125 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3127 set_bit(__IXGBEVF_DOWN, &adapter->state);
3129 del_timer_sync(&adapter->watchdog_timer);
3131 cancel_work_sync(&adapter->reset_task);
3132 cancel_work_sync(&adapter->watchdog_task);
3134 if (netdev->reg_state == NETREG_REGISTERED)
3135 unregister_netdev(netdev);
3137 ixgbevf_reset_interrupt_capability(adapter);
3139 iounmap(adapter->hw.hw_addr);
3140 pci_release_regions(pdev);
3142 hw_dbg(&adapter->hw, "Remove complete\n");
3144 kfree(adapter->tx_ring);
3145 kfree(adapter->rx_ring);
3147 free_netdev(netdev);
3149 pci_disable_device(pdev);
3152 static struct pci_driver ixgbevf_driver = {
3153 .name = ixgbevf_driver_name,
3154 .id_table = ixgbevf_pci_tbl,
3155 .probe = ixgbevf_probe,
3156 .remove = __devexit_p(ixgbevf_remove),
3157 .shutdown = ixgbevf_shutdown,
3161 * ixgbevf_init_module - Driver Registration Routine
3163 * ixgbevf_init_module is the first routine called when the driver is
3164 * loaded. All it does is register with the PCI subsystem.
3166 static int __init ixgbevf_init_module(void)
3169 pr_info("%s - version %s\n", ixgbevf_driver_string,
3170 ixgbevf_driver_version);
3172 pr_info("%s\n", ixgbevf_copyright);
3174 ret = pci_register_driver(&ixgbevf_driver);
3178 module_init(ixgbevf_init_module);
3181 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3183 * ixgbevf_exit_module is called just before the driver is removed
3186 static void __exit ixgbevf_exit_module(void)
3188 pci_unregister_driver(&ixgbevf_driver);
3193 * ixgbevf_get_hw_dev_name - return device name string
3194 * used by hardware layer to print debugging information
3196 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3198 struct ixgbevf_adapter *adapter = hw->back;
3199 return adapter->netdev->name;
3203 module_exit(ixgbevf_exit_module);
3205 /* ixgbevf_main.c */