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 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
199 i = tx_ring->next_to_clean;
200 eop = tx_ring->tx_buffer_info[i].next_to_watch;
201 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
203 while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
204 (count < tx_ring->count)) {
205 bool cleaned = false;
206 rmb(); /* read buffer_info after eop_desc */
207 /* eop could change between read and DD-check */
208 if (unlikely(eop != tx_ring->tx_buffer_info[i].next_to_watch))
210 for ( ; !cleaned; count++) {
212 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
213 tx_buffer_info = &tx_ring->tx_buffer_info[i];
214 cleaned = (i == eop);
215 skb = tx_buffer_info->skb;
217 if (cleaned && skb) {
218 unsigned int segs, bytecount;
220 /* gso_segs is currently only valid for tcp */
221 segs = skb_shinfo(skb)->gso_segs ?: 1;
222 /* multiply data chunks by size of headers */
223 bytecount = ((segs - 1) * skb_headlen(skb)) +
225 total_packets += segs;
226 total_bytes += bytecount;
229 ixgbevf_unmap_and_free_tx_resource(tx_ring,
232 tx_desc->wb.status = 0;
235 if (i == tx_ring->count)
240 eop = tx_ring->tx_buffer_info[i].next_to_watch;
241 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
244 tx_ring->next_to_clean = i;
246 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
247 if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
248 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
249 /* Make sure that anybody stopping the queue after this
250 * sees the new next_to_clean.
253 if (__netif_subqueue_stopped(tx_ring->netdev,
254 tx_ring->queue_index) &&
255 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
256 netif_wake_subqueue(tx_ring->netdev,
257 tx_ring->queue_index);
258 ++adapter->restart_queue;
262 u64_stats_update_begin(&tx_ring->syncp);
263 tx_ring->total_bytes += total_bytes;
264 tx_ring->total_packets += total_packets;
265 u64_stats_update_end(&tx_ring->syncp);
267 return count < tx_ring->count;
271 * ixgbevf_receive_skb - Send a completed packet up the stack
272 * @q_vector: structure containing interrupt and ring information
273 * @skb: packet to send up
274 * @status: hardware indication of status of receive
275 * @rx_ring: rx descriptor ring (for a specific queue) to setup
276 * @rx_desc: rx descriptor
278 static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
279 struct sk_buff *skb, u8 status,
280 struct ixgbevf_ring *ring,
281 union ixgbe_adv_rx_desc *rx_desc)
283 struct ixgbevf_adapter *adapter = q_vector->adapter;
284 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
285 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
287 if (is_vlan && test_bit(tag & VLAN_VID_MASK, adapter->active_vlans))
288 __vlan_hwaccel_put_tag(skb, tag);
290 napi_gro_receive(&q_vector->napi, skb);
294 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
295 * @adapter: address of board private structure
296 * @status_err: hardware indication of status of receive
297 * @skb: skb currently being received and modified
299 static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
300 struct ixgbevf_ring *ring,
301 u32 status_err, struct sk_buff *skb)
303 skb_checksum_none_assert(skb);
305 /* Rx csum disabled */
306 if (!(ring->netdev->features & NETIF_F_RXCSUM))
309 /* if IP and error */
310 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
311 (status_err & IXGBE_RXDADV_ERR_IPE)) {
312 adapter->hw_csum_rx_error++;
316 if (!(status_err & IXGBE_RXD_STAT_L4CS))
319 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
320 adapter->hw_csum_rx_error++;
324 /* It must be a TCP or UDP packet with a valid checksum */
325 skb->ip_summed = CHECKSUM_UNNECESSARY;
326 adapter->hw_csum_rx_good++;
330 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
331 * @adapter: address of board private structure
333 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
334 struct ixgbevf_ring *rx_ring,
337 struct pci_dev *pdev = adapter->pdev;
338 union ixgbe_adv_rx_desc *rx_desc;
339 struct ixgbevf_rx_buffer *bi;
341 unsigned int i = rx_ring->next_to_use;
343 bi = &rx_ring->rx_buffer_info[i];
345 while (cleaned_count--) {
346 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
349 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
350 rx_ring->rx_buf_len);
352 adapter->alloc_rx_buff_failed++;
358 bi->dma = dma_map_single(&pdev->dev, skb->data,
362 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
365 if (i == rx_ring->count)
367 bi = &rx_ring->rx_buffer_info[i];
371 if (rx_ring->next_to_use != i) {
372 rx_ring->next_to_use = i;
374 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
378 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
381 struct ixgbe_hw *hw = &adapter->hw;
383 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
386 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
387 struct ixgbevf_ring *rx_ring,
390 struct ixgbevf_adapter *adapter = q_vector->adapter;
391 struct pci_dev *pdev = adapter->pdev;
392 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
393 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
397 int cleaned_count = 0;
398 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
400 i = rx_ring->next_to_clean;
401 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
402 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
403 rx_buffer_info = &rx_ring->rx_buffer_info[i];
405 while (staterr & IXGBE_RXD_STAT_DD) {
410 rmb(); /* read descriptor and rx_buffer_info after status DD */
411 len = le16_to_cpu(rx_desc->wb.upper.length);
412 skb = rx_buffer_info->skb;
413 prefetch(skb->data - NET_IP_ALIGN);
414 rx_buffer_info->skb = NULL;
416 if (rx_buffer_info->dma) {
417 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
420 rx_buffer_info->dma = 0;
425 if (i == rx_ring->count)
428 next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
432 next_buffer = &rx_ring->rx_buffer_info[i];
434 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
435 skb->next = next_buffer->skb;
436 skb->next->prev = skb;
437 adapter->non_eop_descs++;
441 /* ERR_MASK will only have valid bits if EOP set */
442 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
443 dev_kfree_skb_irq(skb);
447 ixgbevf_rx_checksum(adapter, rx_ring, staterr, skb);
449 /* probably a little skewed due to removing CRC */
450 total_rx_bytes += skb->len;
454 * Work around issue of some types of VM to VM loop back
455 * packets not getting split correctly
457 if (staterr & IXGBE_RXD_STAT_LB) {
458 u32 header_fixup_len = skb_headlen(skb);
459 if (header_fixup_len < 14)
460 skb_push(skb, header_fixup_len);
462 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
464 ixgbevf_receive_skb(q_vector, skb, staterr, rx_ring, rx_desc);
467 rx_desc->wb.upper.status_error = 0;
469 /* return some buffers to hardware, one at a time is too slow */
470 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
471 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
476 /* use prefetched values */
478 rx_buffer_info = &rx_ring->rx_buffer_info[i];
480 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
483 rx_ring->next_to_clean = i;
484 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
487 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
489 u64_stats_update_begin(&rx_ring->syncp);
490 rx_ring->total_packets += total_rx_packets;
491 rx_ring->total_bytes += total_rx_bytes;
492 u64_stats_update_end(&rx_ring->syncp);
498 * ixgbevf_poll - NAPI polling calback
499 * @napi: napi struct with our devices info in it
500 * @budget: amount of work driver is allowed to do this pass, in packets
502 * This function will clean more than one or more rings associated with a
505 static int ixgbevf_poll(struct napi_struct *napi, int budget)
507 struct ixgbevf_q_vector *q_vector =
508 container_of(napi, struct ixgbevf_q_vector, napi);
509 struct ixgbevf_adapter *adapter = q_vector->adapter;
510 struct ixgbevf_ring *ring;
512 bool clean_complete = true;
514 ixgbevf_for_each_ring(ring, q_vector->tx)
515 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
517 /* attempt to distribute budget to each queue fairly, but don't allow
518 * the budget to go below 1 because we'll exit polling */
519 if (q_vector->rx.count > 1)
520 per_ring_budget = max(budget/q_vector->rx.count, 1);
522 per_ring_budget = budget;
524 ixgbevf_for_each_ring(ring, q_vector->rx)
525 clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
528 /* If all work not completed, return budget and keep polling */
531 /* all work done, exit the polling mode */
533 if (adapter->rx_itr_setting & 1)
534 ixgbevf_set_itr(q_vector);
535 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
536 ixgbevf_irq_enable_queues(adapter,
537 1 << q_vector->v_idx);
543 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
544 * @q_vector: structure containing interrupt and ring information
546 static void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
548 struct ixgbevf_adapter *adapter = q_vector->adapter;
549 struct ixgbe_hw *hw = &adapter->hw;
550 int v_idx = q_vector->v_idx;
551 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
554 * set the WDIS bit to not clear the timer bits and cause an
555 * immediate assertion of the interrupt
557 itr_reg |= IXGBE_EITR_CNT_WDIS;
559 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
563 * ixgbevf_configure_msix - Configure MSI-X hardware
564 * @adapter: board private structure
566 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
569 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
571 struct ixgbevf_q_vector *q_vector;
572 int q_vectors, v_idx;
574 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
575 adapter->eims_enable_mask = 0;
578 * Populate the IVAR table and set the ITR values to the
579 * corresponding register.
581 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
582 struct ixgbevf_ring *ring;
583 q_vector = adapter->q_vector[v_idx];
585 ixgbevf_for_each_ring(ring, q_vector->rx)
586 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
588 ixgbevf_for_each_ring(ring, q_vector->tx)
589 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
591 if (q_vector->tx.ring && !q_vector->rx.ring) {
593 if (adapter->tx_itr_setting == 1)
594 q_vector->itr = IXGBE_10K_ITR;
596 q_vector->itr = adapter->tx_itr_setting;
598 /* rx or rx/tx vector */
599 if (adapter->rx_itr_setting == 1)
600 q_vector->itr = IXGBE_20K_ITR;
602 q_vector->itr = adapter->rx_itr_setting;
605 /* add q_vector eims value to global eims_enable_mask */
606 adapter->eims_enable_mask |= 1 << v_idx;
608 ixgbevf_write_eitr(q_vector);
611 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
612 /* setup eims_other and add value to global eims_enable_mask */
613 adapter->eims_other = 1 << v_idx;
614 adapter->eims_enable_mask |= adapter->eims_other;
621 latency_invalid = 255
625 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
626 * @q_vector: structure containing interrupt and ring information
627 * @ring_container: structure containing ring performance data
629 * Stores a new ITR value based on packets and byte
630 * counts during the last interrupt. The advantage of per interrupt
631 * computation is faster updates and more accurate ITR for the current
632 * traffic pattern. Constants in this function were computed
633 * based on theoretical maximum wire speed and thresholds were set based
634 * on testing data as well as attempting to minimize response time
635 * while increasing bulk throughput.
637 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
638 struct ixgbevf_ring_container *ring_container)
640 int bytes = ring_container->total_bytes;
641 int packets = ring_container->total_packets;
644 u8 itr_setting = ring_container->itr;
649 /* simple throttlerate management
650 * 0-20MB/s lowest (100000 ints/s)
651 * 20-100MB/s low (20000 ints/s)
652 * 100-1249MB/s bulk (8000 ints/s)
654 /* what was last interrupt timeslice? */
655 timepassed_us = q_vector->itr >> 2;
656 bytes_perint = bytes / timepassed_us; /* bytes/usec */
658 switch (itr_setting) {
660 if (bytes_perint > 10)
661 itr_setting = low_latency;
664 if (bytes_perint > 20)
665 itr_setting = bulk_latency;
666 else if (bytes_perint <= 10)
667 itr_setting = lowest_latency;
670 if (bytes_perint <= 20)
671 itr_setting = low_latency;
675 /* clear work counters since we have the values we need */
676 ring_container->total_bytes = 0;
677 ring_container->total_packets = 0;
679 /* write updated itr to ring container */
680 ring_container->itr = itr_setting;
683 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
685 u32 new_itr = q_vector->itr;
688 ixgbevf_update_itr(q_vector, &q_vector->tx);
689 ixgbevf_update_itr(q_vector, &q_vector->rx);
691 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
693 switch (current_itr) {
694 /* counts and packets in update_itr are dependent on these numbers */
696 new_itr = IXGBE_100K_ITR;
699 new_itr = IXGBE_20K_ITR;
703 new_itr = IXGBE_8K_ITR;
707 if (new_itr != q_vector->itr) {
708 /* do an exponential smoothing */
709 new_itr = (10 * new_itr * q_vector->itr) /
710 ((9 * new_itr) + q_vector->itr);
712 /* save the algorithm value here */
713 q_vector->itr = new_itr;
715 ixgbevf_write_eitr(q_vector);
719 static irqreturn_t ixgbevf_msix_mbx(int irq, void *data)
721 struct ixgbevf_adapter *adapter = data;
722 struct ixgbe_hw *hw = &adapter->hw;
724 bool got_ack = false;
726 if (!hw->mbx.ops.check_for_ack(hw))
729 if (!hw->mbx.ops.check_for_msg(hw)) {
730 hw->mbx.ops.read(hw, &msg, 1);
732 if ((msg & IXGBE_MBVFICR_VFREQ_MASK) == IXGBE_PF_CONTROL_MSG)
733 mod_timer(&adapter->watchdog_timer,
734 round_jiffies(jiffies + 1));
736 if (msg & IXGBE_VT_MSGTYPE_NACK)
737 pr_warn("Last Request of type %2.2x to PF Nacked\n",
740 * Restore the PFSTS bit in case someone is polling for a
741 * return message from the PF
743 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFSTS;
747 * checking for the ack clears the PFACK bit. Place
748 * it back in the v2p_mailbox cache so that anyone
749 * polling for an ack will not miss it
752 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFACK;
754 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
761 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
763 * @data: pointer to our q_vector struct for this interrupt vector
765 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
767 struct ixgbevf_q_vector *q_vector = data;
769 /* EIAM disabled interrupts (on this vector) for us */
770 if (q_vector->rx.ring || q_vector->tx.ring)
771 napi_schedule(&q_vector->napi);
776 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
779 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
781 a->rx_ring[r_idx].next = q_vector->rx.ring;
782 q_vector->rx.ring = &a->rx_ring[r_idx];
783 q_vector->rx.count++;
786 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
789 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
791 a->tx_ring[t_idx].next = q_vector->tx.ring;
792 q_vector->tx.ring = &a->tx_ring[t_idx];
793 q_vector->tx.count++;
797 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
798 * @adapter: board private structure to initialize
800 * This function maps descriptor rings to the queue-specific vectors
801 * we were allotted through the MSI-X enabling code. Ideally, we'd have
802 * one vector per ring/queue, but on a constrained vector budget, we
803 * group the rings as "efficiently" as possible. You would add new
804 * mapping configurations in here.
806 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
810 int rxr_idx = 0, txr_idx = 0;
811 int rxr_remaining = adapter->num_rx_queues;
812 int txr_remaining = adapter->num_tx_queues;
817 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
820 * The ideal configuration...
821 * We have enough vectors to map one per queue.
823 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
824 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
825 map_vector_to_rxq(adapter, v_start, rxr_idx);
827 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
828 map_vector_to_txq(adapter, v_start, txr_idx);
833 * If we don't have enough vectors for a 1-to-1
834 * mapping, we'll have to group them so there are
835 * multiple queues per vector.
837 /* Re-adjusting *qpv takes care of the remainder. */
838 for (i = v_start; i < q_vectors; i++) {
839 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
840 for (j = 0; j < rqpv; j++) {
841 map_vector_to_rxq(adapter, i, rxr_idx);
846 for (i = v_start; i < q_vectors; i++) {
847 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
848 for (j = 0; j < tqpv; j++) {
849 map_vector_to_txq(adapter, i, txr_idx);
860 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
861 * @adapter: board private structure
863 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
864 * interrupts from the kernel.
866 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
868 struct net_device *netdev = adapter->netdev;
869 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
873 for (vector = 0; vector < q_vectors; vector++) {
874 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
875 struct msix_entry *entry = &adapter->msix_entries[vector];
877 if (q_vector->tx.ring && q_vector->rx.ring) {
878 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
879 "%s-%s-%d", netdev->name, "TxRx", ri++);
881 } else if (q_vector->rx.ring) {
882 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
883 "%s-%s-%d", netdev->name, "rx", ri++);
884 } else if (q_vector->tx.ring) {
885 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
886 "%s-%s-%d", netdev->name, "tx", ti++);
888 /* skip this unused q_vector */
891 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
892 q_vector->name, q_vector);
895 "request_irq failed for MSIX interrupt "
897 goto free_queue_irqs;
901 err = request_irq(adapter->msix_entries[vector].vector,
902 &ixgbevf_msix_mbx, 0, netdev->name, adapter);
905 "request_irq for msix_mbx failed: %d\n", err);
906 goto free_queue_irqs;
914 free_irq(adapter->msix_entries[vector].vector,
915 adapter->q_vector[vector]);
917 pci_disable_msix(adapter->pdev);
918 kfree(adapter->msix_entries);
919 adapter->msix_entries = NULL;
923 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
925 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
927 for (i = 0; i < q_vectors; i++) {
928 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
929 q_vector->rx.ring = NULL;
930 q_vector->tx.ring = NULL;
931 q_vector->rx.count = 0;
932 q_vector->tx.count = 0;
937 * ixgbevf_request_irq - initialize interrupts
938 * @adapter: board private structure
940 * Attempts to configure interrupts using the best available
941 * capabilities of the hardware and kernel.
943 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
947 err = ixgbevf_request_msix_irqs(adapter);
951 "request_irq failed, Error %d\n", err);
956 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
960 q_vectors = adapter->num_msix_vectors;
963 free_irq(adapter->msix_entries[i].vector, adapter);
966 for (; i >= 0; i--) {
967 /* free only the irqs that were actually requested */
968 if (!adapter->q_vector[i]->rx.ring &&
969 !adapter->q_vector[i]->tx.ring)
972 free_irq(adapter->msix_entries[i].vector,
973 adapter->q_vector[i]);
976 ixgbevf_reset_q_vectors(adapter);
980 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
981 * @adapter: board private structure
983 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
985 struct ixgbe_hw *hw = &adapter->hw;
988 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
989 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
990 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
992 IXGBE_WRITE_FLUSH(hw);
994 for (i = 0; i < adapter->num_msix_vectors; i++)
995 synchronize_irq(adapter->msix_entries[i].vector);
999 * ixgbevf_irq_enable - Enable default interrupt generation settings
1000 * @adapter: board private structure
1002 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1004 struct ixgbe_hw *hw = &adapter->hw;
1006 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1007 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1008 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1012 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1013 * @adapter: board private structure
1015 * Configure the Tx unit of the MAC after a reset.
1017 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1020 struct ixgbe_hw *hw = &adapter->hw;
1021 u32 i, j, tdlen, txctrl;
1023 /* Setup the HW Tx Head and Tail descriptor pointers */
1024 for (i = 0; i < adapter->num_tx_queues; i++) {
1025 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1028 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1029 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1030 (tdba & DMA_BIT_MASK(32)));
1031 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1032 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1033 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1034 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1035 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1036 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1037 /* Disable Tx Head Writeback RO bit, since this hoses
1038 * bookkeeping if things aren't delivered in order.
1040 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1041 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1042 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1046 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1048 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1050 struct ixgbevf_ring *rx_ring;
1051 struct ixgbe_hw *hw = &adapter->hw;
1054 rx_ring = &adapter->rx_ring[index];
1056 srrctl = IXGBE_SRRCTL_DROP_EN;
1058 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1060 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1061 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1063 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1066 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1068 struct ixgbe_hw *hw = &adapter->hw;
1069 struct net_device *netdev = adapter->netdev;
1070 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1074 /* notify the PF of our intent to use this size of frame */
1075 ixgbevf_rlpml_set_vf(hw, max_frame);
1077 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1078 max_frame += VLAN_HLEN;
1081 * Make best use of allocation by using all but 1K of a
1082 * power of 2 allocation that will be used for skb->head.
1084 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1085 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1086 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1087 else if (max_frame <= IXGBEVF_RXBUFFER_3K)
1088 rx_buf_len = IXGBEVF_RXBUFFER_3K;
1089 else if (max_frame <= IXGBEVF_RXBUFFER_7K)
1090 rx_buf_len = IXGBEVF_RXBUFFER_7K;
1091 else if (max_frame <= IXGBEVF_RXBUFFER_15K)
1092 rx_buf_len = IXGBEVF_RXBUFFER_15K;
1094 rx_buf_len = IXGBEVF_MAX_RXBUFFER;
1096 for (i = 0; i < adapter->num_rx_queues; i++)
1097 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1101 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1102 * @adapter: board private structure
1104 * Configure the Rx unit of the MAC after a reset.
1106 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1109 struct ixgbe_hw *hw = &adapter->hw;
1113 /* PSRTYPE must be initialized in 82599 */
1114 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
1116 /* set_rx_buffer_len must be called before ring initialization */
1117 ixgbevf_set_rx_buffer_len(adapter);
1119 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1120 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1121 * the Base and Length of the Rx Descriptor Ring */
1122 for (i = 0; i < adapter->num_rx_queues; i++) {
1123 rdba = adapter->rx_ring[i].dma;
1124 j = adapter->rx_ring[i].reg_idx;
1125 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1126 (rdba & DMA_BIT_MASK(32)));
1127 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1128 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1129 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1130 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1131 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1132 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1134 ixgbevf_configure_srrctl(adapter, j);
1138 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1140 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1141 struct ixgbe_hw *hw = &adapter->hw;
1143 spin_lock(&adapter->mbx_lock);
1145 /* add VID to filter table */
1146 if (hw->mac.ops.set_vfta)
1147 hw->mac.ops.set_vfta(hw, vid, 0, true);
1149 spin_unlock(&adapter->mbx_lock);
1151 set_bit(vid, adapter->active_vlans);
1156 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1158 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1159 struct ixgbe_hw *hw = &adapter->hw;
1161 spin_lock(&adapter->mbx_lock);
1163 /* remove VID from filter table */
1164 if (hw->mac.ops.set_vfta)
1165 hw->mac.ops.set_vfta(hw, vid, 0, false);
1167 spin_unlock(&adapter->mbx_lock);
1169 clear_bit(vid, adapter->active_vlans);
1174 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1178 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1179 ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
1182 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1184 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1185 struct ixgbe_hw *hw = &adapter->hw;
1188 if ((netdev_uc_count(netdev)) > 10) {
1189 pr_err("Too many unicast filters - No Space\n");
1193 if (!netdev_uc_empty(netdev)) {
1194 struct netdev_hw_addr *ha;
1195 netdev_for_each_uc_addr(ha, netdev) {
1196 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1201 * If the list is empty then send message to PF driver to
1202 * clear all macvlans on this VF.
1204 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1211 * ixgbevf_set_rx_mode - Multicast set
1212 * @netdev: network interface device structure
1214 * The set_rx_method entry point is called whenever the multicast address
1215 * list or the network interface flags are updated. This routine is
1216 * responsible for configuring the hardware for proper multicast mode.
1218 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1220 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1221 struct ixgbe_hw *hw = &adapter->hw;
1223 spin_lock(&adapter->mbx_lock);
1225 /* reprogram multicast list */
1226 if (hw->mac.ops.update_mc_addr_list)
1227 hw->mac.ops.update_mc_addr_list(hw, netdev);
1229 ixgbevf_write_uc_addr_list(netdev);
1231 spin_unlock(&adapter->mbx_lock);
1234 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1237 struct ixgbevf_q_vector *q_vector;
1238 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1240 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1241 q_vector = adapter->q_vector[q_idx];
1242 napi_enable(&q_vector->napi);
1246 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1249 struct ixgbevf_q_vector *q_vector;
1250 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1252 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1253 q_vector = adapter->q_vector[q_idx];
1254 napi_disable(&q_vector->napi);
1258 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1260 struct net_device *netdev = adapter->netdev;
1263 ixgbevf_set_rx_mode(netdev);
1265 ixgbevf_restore_vlan(adapter);
1267 ixgbevf_configure_tx(adapter);
1268 ixgbevf_configure_rx(adapter);
1269 for (i = 0; i < adapter->num_rx_queues; i++) {
1270 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1271 ixgbevf_alloc_rx_buffers(adapter, ring,
1272 IXGBE_DESC_UNUSED(ring));
1276 #define IXGBE_MAX_RX_DESC_POLL 10
1277 static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1280 struct ixgbe_hw *hw = &adapter->hw;
1281 int j = adapter->rx_ring[rxr].reg_idx;
1284 for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1285 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1290 if (k >= IXGBE_MAX_RX_DESC_POLL) {
1291 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1292 "not set within the polling period\n", rxr);
1295 ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
1296 (adapter->rx_ring[rxr].count - 1));
1299 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1301 /* Only save pre-reset stats if there are some */
1302 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1303 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1304 adapter->stats.base_vfgprc;
1305 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1306 adapter->stats.base_vfgptc;
1307 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1308 adapter->stats.base_vfgorc;
1309 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1310 adapter->stats.base_vfgotc;
1311 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1312 adapter->stats.base_vfmprc;
1316 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1318 struct ixgbe_hw *hw = &adapter->hw;
1320 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1321 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1322 adapter->stats.last_vfgorc |=
1323 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1324 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1325 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1326 adapter->stats.last_vfgotc |=
1327 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1328 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1330 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1331 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1332 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1333 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1334 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1337 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1339 struct ixgbe_hw *hw = &adapter->hw;
1340 int api[] = { ixgbe_mbox_api_10,
1341 ixgbe_mbox_api_unknown };
1342 int err = 0, idx = 0;
1344 spin_lock(&adapter->mbx_lock);
1346 while (api[idx] != ixgbe_mbox_api_unknown) {
1347 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1353 spin_unlock(&adapter->mbx_lock);
1356 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1358 struct net_device *netdev = adapter->netdev;
1359 struct ixgbe_hw *hw = &adapter->hw;
1361 int num_rx_rings = adapter->num_rx_queues;
1364 for (i = 0; i < adapter->num_tx_queues; i++) {
1365 j = adapter->tx_ring[i].reg_idx;
1366 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1367 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1368 txdctl |= (8 << 16);
1369 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1372 for (i = 0; i < adapter->num_tx_queues; i++) {
1373 j = adapter->tx_ring[i].reg_idx;
1374 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1375 txdctl |= IXGBE_TXDCTL_ENABLE;
1376 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1379 for (i = 0; i < num_rx_rings; i++) {
1380 j = adapter->rx_ring[i].reg_idx;
1381 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1382 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1383 if (hw->mac.type == ixgbe_mac_X540_vf) {
1384 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1385 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1386 IXGBE_RXDCTL_RLPML_EN);
1388 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1389 ixgbevf_rx_desc_queue_enable(adapter, i);
1392 ixgbevf_configure_msix(adapter);
1394 spin_lock(&adapter->mbx_lock);
1396 if (hw->mac.ops.set_rar) {
1397 if (is_valid_ether_addr(hw->mac.addr))
1398 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1400 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1403 spin_unlock(&adapter->mbx_lock);
1405 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1406 ixgbevf_napi_enable_all(adapter);
1408 /* enable transmits */
1409 netif_tx_start_all_queues(netdev);
1411 ixgbevf_save_reset_stats(adapter);
1412 ixgbevf_init_last_counter_stats(adapter);
1414 mod_timer(&adapter->watchdog_timer, jiffies);
1417 void ixgbevf_up(struct ixgbevf_adapter *adapter)
1419 struct ixgbe_hw *hw = &adapter->hw;
1421 ixgbevf_negotiate_api(adapter);
1423 ixgbevf_configure(adapter);
1425 ixgbevf_up_complete(adapter);
1427 /* clear any pending interrupts, may auto mask */
1428 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1430 ixgbevf_irq_enable(adapter);
1434 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1435 * @adapter: board private structure
1436 * @rx_ring: ring to free buffers from
1438 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1439 struct ixgbevf_ring *rx_ring)
1441 struct pci_dev *pdev = adapter->pdev;
1445 if (!rx_ring->rx_buffer_info)
1448 /* Free all the Rx ring sk_buffs */
1449 for (i = 0; i < rx_ring->count; i++) {
1450 struct ixgbevf_rx_buffer *rx_buffer_info;
1452 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1453 if (rx_buffer_info->dma) {
1454 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1455 rx_ring->rx_buf_len,
1457 rx_buffer_info->dma = 0;
1459 if (rx_buffer_info->skb) {
1460 struct sk_buff *skb = rx_buffer_info->skb;
1461 rx_buffer_info->skb = NULL;
1463 struct sk_buff *this = skb;
1465 dev_kfree_skb(this);
1470 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1471 memset(rx_ring->rx_buffer_info, 0, size);
1473 /* Zero out the descriptor ring */
1474 memset(rx_ring->desc, 0, rx_ring->size);
1476 rx_ring->next_to_clean = 0;
1477 rx_ring->next_to_use = 0;
1480 writel(0, adapter->hw.hw_addr + rx_ring->head);
1482 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1486 * ixgbevf_clean_tx_ring - Free Tx Buffers
1487 * @adapter: board private structure
1488 * @tx_ring: ring to be cleaned
1490 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1491 struct ixgbevf_ring *tx_ring)
1493 struct ixgbevf_tx_buffer *tx_buffer_info;
1497 if (!tx_ring->tx_buffer_info)
1500 /* Free all the Tx ring sk_buffs */
1502 for (i = 0; i < tx_ring->count; i++) {
1503 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1504 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
1507 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1508 memset(tx_ring->tx_buffer_info, 0, size);
1510 memset(tx_ring->desc, 0, tx_ring->size);
1512 tx_ring->next_to_use = 0;
1513 tx_ring->next_to_clean = 0;
1516 writel(0, adapter->hw.hw_addr + tx_ring->head);
1518 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1522 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1523 * @adapter: board private structure
1525 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1529 for (i = 0; i < adapter->num_rx_queues; i++)
1530 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1534 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1535 * @adapter: board private structure
1537 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1541 for (i = 0; i < adapter->num_tx_queues; i++)
1542 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1545 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1547 struct net_device *netdev = adapter->netdev;
1548 struct ixgbe_hw *hw = &adapter->hw;
1552 /* signal that we are down to the interrupt handler */
1553 set_bit(__IXGBEVF_DOWN, &adapter->state);
1554 /* disable receives */
1556 netif_tx_disable(netdev);
1560 netif_tx_stop_all_queues(netdev);
1562 ixgbevf_irq_disable(adapter);
1564 ixgbevf_napi_disable_all(adapter);
1566 del_timer_sync(&adapter->watchdog_timer);
1567 /* can't call flush scheduled work here because it can deadlock
1568 * if linkwatch_event tries to acquire the rtnl_lock which we are
1570 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1573 /* disable transmits in the hardware now that interrupts are off */
1574 for (i = 0; i < adapter->num_tx_queues; i++) {
1575 j = adapter->tx_ring[i].reg_idx;
1576 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1577 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1578 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1581 netif_carrier_off(netdev);
1583 if (!pci_channel_offline(adapter->pdev))
1584 ixgbevf_reset(adapter);
1586 ixgbevf_clean_all_tx_rings(adapter);
1587 ixgbevf_clean_all_rx_rings(adapter);
1590 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1592 struct ixgbe_hw *hw = &adapter->hw;
1594 WARN_ON(in_interrupt());
1596 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1600 * Check if PF is up before re-init. If not then skip until
1601 * later when the PF is up and ready to service requests from
1602 * the VF via mailbox. If the VF is up and running then the
1603 * watchdog task will continue to schedule reset tasks until
1604 * the PF is up and running.
1606 if (!hw->mac.ops.reset_hw(hw)) {
1607 ixgbevf_down(adapter);
1608 ixgbevf_up(adapter);
1611 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1614 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1616 struct ixgbe_hw *hw = &adapter->hw;
1617 struct net_device *netdev = adapter->netdev;
1619 spin_lock(&adapter->mbx_lock);
1621 if (hw->mac.ops.reset_hw(hw))
1622 hw_dbg(hw, "PF still resetting\n");
1624 hw->mac.ops.init_hw(hw);
1626 spin_unlock(&adapter->mbx_lock);
1628 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1629 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1631 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1636 static void ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1639 int err, vector_threshold;
1641 /* We'll want at least 2 (vector_threshold):
1642 * 1) TxQ[0] + RxQ[0] handler
1643 * 2) Other (Link Status Change, etc.)
1645 vector_threshold = MIN_MSIX_COUNT;
1647 /* The more we get, the more we will assign to Tx/Rx Cleanup
1648 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1649 * Right now, we simply care about how many we'll get; we'll
1650 * set them up later while requesting irq's.
1652 while (vectors >= vector_threshold) {
1653 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1655 if (!err) /* Success in acquiring all requested vectors. */
1658 vectors = 0; /* Nasty failure, quit now */
1659 else /* err == number of vectors we should try again with */
1663 if (vectors < vector_threshold) {
1664 /* Can't allocate enough MSI-X interrupts? Oh well.
1665 * This just means we'll go with either a single MSI
1666 * vector or fall back to legacy interrupts.
1668 hw_dbg(&adapter->hw,
1669 "Unable to allocate MSI-X interrupts\n");
1670 kfree(adapter->msix_entries);
1671 adapter->msix_entries = NULL;
1674 * Adjust for only the vectors we'll use, which is minimum
1675 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1676 * vectors we were allocated.
1678 adapter->num_msix_vectors = vectors;
1683 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
1684 * @adapter: board private structure to initialize
1686 * This is the top level queue allocation routine. The order here is very
1687 * important, starting with the "most" number of features turned on at once,
1688 * and ending with the smallest set of features. This way large combinations
1689 * can be allocated if they're turned on, and smaller combinations are the
1690 * fallthrough conditions.
1693 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1695 /* Start with base case */
1696 adapter->num_rx_queues = 1;
1697 adapter->num_tx_queues = 1;
1701 * ixgbevf_alloc_queues - Allocate memory for all rings
1702 * @adapter: board private structure to initialize
1704 * We allocate one ring per queue at run-time since we don't know the
1705 * number of queues at compile-time. The polling_netdev array is
1706 * intended for Multiqueue, but should work fine with a single queue.
1708 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1712 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1713 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1714 if (!adapter->tx_ring)
1715 goto err_tx_ring_allocation;
1717 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1718 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1719 if (!adapter->rx_ring)
1720 goto err_rx_ring_allocation;
1722 for (i = 0; i < adapter->num_tx_queues; i++) {
1723 adapter->tx_ring[i].count = adapter->tx_ring_count;
1724 adapter->tx_ring[i].queue_index = i;
1725 adapter->tx_ring[i].reg_idx = i;
1726 adapter->tx_ring[i].dev = &adapter->pdev->dev;
1727 adapter->tx_ring[i].netdev = adapter->netdev;
1730 for (i = 0; i < adapter->num_rx_queues; i++) {
1731 adapter->rx_ring[i].count = adapter->rx_ring_count;
1732 adapter->rx_ring[i].queue_index = i;
1733 adapter->rx_ring[i].reg_idx = i;
1734 adapter->rx_ring[i].dev = &adapter->pdev->dev;
1735 adapter->rx_ring[i].netdev = adapter->netdev;
1740 err_rx_ring_allocation:
1741 kfree(adapter->tx_ring);
1742 err_tx_ring_allocation:
1747 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1748 * @adapter: board private structure to initialize
1750 * Attempt to configure the interrupts using the best available
1751 * capabilities of the hardware and the kernel.
1753 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1756 int vector, v_budget;
1759 * It's easy to be greedy for MSI-X vectors, but it really
1760 * doesn't do us much good if we have a lot more vectors
1761 * than CPU's. So let's be conservative and only ask for
1762 * (roughly) the same number of vectors as there are CPU's.
1763 * The default is to use pairs of vectors.
1765 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
1766 v_budget = min_t(int, v_budget, num_online_cpus());
1767 v_budget += NON_Q_VECTORS;
1769 /* A failure in MSI-X entry allocation isn't fatal, but it does
1770 * mean we disable MSI-X capabilities of the adapter. */
1771 adapter->msix_entries = kcalloc(v_budget,
1772 sizeof(struct msix_entry), GFP_KERNEL);
1773 if (!adapter->msix_entries) {
1778 for (vector = 0; vector < v_budget; vector++)
1779 adapter->msix_entries[vector].entry = vector;
1781 ixgbevf_acquire_msix_vectors(adapter, v_budget);
1788 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1789 * @adapter: board private structure to initialize
1791 * We allocate one q_vector per queue interrupt. If allocation fails we
1794 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1796 int q_idx, num_q_vectors;
1797 struct ixgbevf_q_vector *q_vector;
1799 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1801 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1802 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1805 q_vector->adapter = adapter;
1806 q_vector->v_idx = q_idx;
1807 netif_napi_add(adapter->netdev, &q_vector->napi,
1809 adapter->q_vector[q_idx] = q_vector;
1817 q_vector = adapter->q_vector[q_idx];
1818 netif_napi_del(&q_vector->napi);
1820 adapter->q_vector[q_idx] = NULL;
1826 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1827 * @adapter: board private structure to initialize
1829 * This function frees the memory allocated to the q_vectors. In addition if
1830 * NAPI is enabled it will delete any references to the NAPI struct prior
1831 * to freeing the q_vector.
1833 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1835 int q_idx, num_q_vectors;
1838 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1839 napi_vectors = adapter->num_rx_queues;
1841 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1842 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1844 adapter->q_vector[q_idx] = NULL;
1845 if (q_idx < napi_vectors)
1846 netif_napi_del(&q_vector->napi);
1852 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1853 * @adapter: board private structure
1856 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1858 pci_disable_msix(adapter->pdev);
1859 kfree(adapter->msix_entries);
1860 adapter->msix_entries = NULL;
1864 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1865 * @adapter: board private structure to initialize
1868 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
1872 /* Number of supported queues */
1873 ixgbevf_set_num_queues(adapter);
1875 err = ixgbevf_set_interrupt_capability(adapter);
1877 hw_dbg(&adapter->hw,
1878 "Unable to setup interrupt capabilities\n");
1879 goto err_set_interrupt;
1882 err = ixgbevf_alloc_q_vectors(adapter);
1884 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
1886 goto err_alloc_q_vectors;
1889 err = ixgbevf_alloc_queues(adapter);
1891 pr_err("Unable to allocate memory for queues\n");
1892 goto err_alloc_queues;
1895 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
1896 "Tx Queue count = %u\n",
1897 (adapter->num_rx_queues > 1) ? "Enabled" :
1898 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
1900 set_bit(__IXGBEVF_DOWN, &adapter->state);
1904 ixgbevf_free_q_vectors(adapter);
1905 err_alloc_q_vectors:
1906 ixgbevf_reset_interrupt_capability(adapter);
1912 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
1913 * @adapter: board private structure to clear interrupt scheme on
1915 * We go through and clear interrupt specific resources and reset the structure
1916 * to pre-load conditions
1918 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
1920 adapter->num_tx_queues = 0;
1921 adapter->num_rx_queues = 0;
1923 ixgbevf_free_q_vectors(adapter);
1924 ixgbevf_reset_interrupt_capability(adapter);
1928 * ixgbevf_sw_init - Initialize general software structures
1929 * (struct ixgbevf_adapter)
1930 * @adapter: board private structure to initialize
1932 * ixgbevf_sw_init initializes the Adapter private data structure.
1933 * Fields are initialized based on PCI device information and
1934 * OS network device settings (MTU size).
1936 static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
1938 struct ixgbe_hw *hw = &adapter->hw;
1939 struct pci_dev *pdev = adapter->pdev;
1942 /* PCI config space info */
1944 hw->vendor_id = pdev->vendor;
1945 hw->device_id = pdev->device;
1946 hw->revision_id = pdev->revision;
1947 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1948 hw->subsystem_device_id = pdev->subsystem_device;
1950 hw->mbx.ops.init_params(hw);
1951 hw->mac.max_tx_queues = MAX_TX_QUEUES;
1952 hw->mac.max_rx_queues = MAX_RX_QUEUES;
1953 err = hw->mac.ops.reset_hw(hw);
1955 dev_info(&pdev->dev,
1956 "PF still in reset state, assigning new address\n");
1957 eth_hw_addr_random(adapter->netdev);
1958 memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
1959 adapter->netdev->addr_len);
1961 err = hw->mac.ops.init_hw(hw);
1963 pr_err("init_shared_code failed: %d\n", err);
1966 memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
1967 adapter->netdev->addr_len);
1970 /* lock to protect mailbox accesses */
1971 spin_lock_init(&adapter->mbx_lock);
1973 /* Enable dynamic interrupt throttling rates */
1974 adapter->rx_itr_setting = 1;
1975 adapter->tx_itr_setting = 1;
1977 /* set default ring sizes */
1978 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
1979 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
1981 set_bit(__IXGBEVF_DOWN, &adapter->state);
1988 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
1990 u32 current_counter = IXGBE_READ_REG(hw, reg); \
1991 if (current_counter < last_counter) \
1992 counter += 0x100000000LL; \
1993 last_counter = current_counter; \
1994 counter &= 0xFFFFFFFF00000000LL; \
1995 counter |= current_counter; \
1998 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2000 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2001 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2002 u64 current_counter = (current_counter_msb << 32) | \
2003 current_counter_lsb; \
2004 if (current_counter < last_counter) \
2005 counter += 0x1000000000LL; \
2006 last_counter = current_counter; \
2007 counter &= 0xFFFFFFF000000000LL; \
2008 counter |= current_counter; \
2011 * ixgbevf_update_stats - Update the board statistics counters.
2012 * @adapter: board private structure
2014 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2016 struct ixgbe_hw *hw = &adapter->hw;
2018 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2019 adapter->stats.vfgprc);
2020 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2021 adapter->stats.vfgptc);
2022 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2023 adapter->stats.last_vfgorc,
2024 adapter->stats.vfgorc);
2025 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2026 adapter->stats.last_vfgotc,
2027 adapter->stats.vfgotc);
2028 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2029 adapter->stats.vfmprc);
2033 * ixgbevf_watchdog - Timer Call-back
2034 * @data: pointer to adapter cast into an unsigned long
2036 static void ixgbevf_watchdog(unsigned long data)
2038 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2039 struct ixgbe_hw *hw = &adapter->hw;
2044 * Do the watchdog outside of interrupt context due to the lovely
2045 * delays that some of the newer hardware requires
2048 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2049 goto watchdog_short_circuit;
2051 /* get one bit for every active tx/rx interrupt vector */
2052 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2053 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2054 if (qv->rx.ring || qv->tx.ring)
2058 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
2060 watchdog_short_circuit:
2061 schedule_work(&adapter->watchdog_task);
2065 * ixgbevf_tx_timeout - Respond to a Tx Hang
2066 * @netdev: network interface device structure
2068 static void ixgbevf_tx_timeout(struct net_device *netdev)
2070 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2072 /* Do the reset outside of interrupt context */
2073 schedule_work(&adapter->reset_task);
2076 static void ixgbevf_reset_task(struct work_struct *work)
2078 struct ixgbevf_adapter *adapter;
2079 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2081 /* If we're already down or resetting, just bail */
2082 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2083 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2086 adapter->tx_timeout_count++;
2088 ixgbevf_reinit_locked(adapter);
2092 * ixgbevf_watchdog_task - worker thread to bring link up
2093 * @work: pointer to work_struct containing our data
2095 static void ixgbevf_watchdog_task(struct work_struct *work)
2097 struct ixgbevf_adapter *adapter = container_of(work,
2098 struct ixgbevf_adapter,
2100 struct net_device *netdev = adapter->netdev;
2101 struct ixgbe_hw *hw = &adapter->hw;
2102 u32 link_speed = adapter->link_speed;
2103 bool link_up = adapter->link_up;
2105 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2108 * Always check the link on the watchdog because we have
2111 if (hw->mac.ops.check_link) {
2114 spin_lock(&adapter->mbx_lock);
2116 need_reset = hw->mac.ops.check_link(hw, &link_speed,
2119 spin_unlock(&adapter->mbx_lock);
2122 adapter->link_up = link_up;
2123 adapter->link_speed = link_speed;
2124 netif_carrier_off(netdev);
2125 netif_tx_stop_all_queues(netdev);
2126 schedule_work(&adapter->reset_task);
2130 /* always assume link is up, if no check link
2132 link_speed = IXGBE_LINK_SPEED_10GB_FULL;
2135 adapter->link_up = link_up;
2136 adapter->link_speed = link_speed;
2139 if (!netif_carrier_ok(netdev)) {
2140 hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
2141 (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2143 netif_carrier_on(netdev);
2144 netif_tx_wake_all_queues(netdev);
2147 adapter->link_up = false;
2148 adapter->link_speed = 0;
2149 if (netif_carrier_ok(netdev)) {
2150 hw_dbg(&adapter->hw, "NIC Link is Down\n");
2151 netif_carrier_off(netdev);
2152 netif_tx_stop_all_queues(netdev);
2156 ixgbevf_update_stats(adapter);
2159 /* Reset the timer */
2160 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2161 mod_timer(&adapter->watchdog_timer,
2162 round_jiffies(jiffies + (2 * HZ)));
2164 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2168 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2169 * @adapter: board private structure
2170 * @tx_ring: Tx descriptor ring for a specific queue
2172 * Free all transmit software resources
2174 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2175 struct ixgbevf_ring *tx_ring)
2177 struct pci_dev *pdev = adapter->pdev;
2179 ixgbevf_clean_tx_ring(adapter, tx_ring);
2181 vfree(tx_ring->tx_buffer_info);
2182 tx_ring->tx_buffer_info = NULL;
2184 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2187 tx_ring->desc = NULL;
2191 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2192 * @adapter: board private structure
2194 * Free all transmit software resources
2196 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2200 for (i = 0; i < adapter->num_tx_queues; i++)
2201 if (adapter->tx_ring[i].desc)
2202 ixgbevf_free_tx_resources(adapter,
2203 &adapter->tx_ring[i]);
2208 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2209 * @adapter: board private structure
2210 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2212 * Return 0 on success, negative on failure
2214 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2215 struct ixgbevf_ring *tx_ring)
2217 struct pci_dev *pdev = adapter->pdev;
2220 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2221 tx_ring->tx_buffer_info = vzalloc(size);
2222 if (!tx_ring->tx_buffer_info)
2225 /* round up to nearest 4K */
2226 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2227 tx_ring->size = ALIGN(tx_ring->size, 4096);
2229 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2230 &tx_ring->dma, GFP_KERNEL);
2234 tx_ring->next_to_use = 0;
2235 tx_ring->next_to_clean = 0;
2239 vfree(tx_ring->tx_buffer_info);
2240 tx_ring->tx_buffer_info = NULL;
2241 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2242 "descriptor ring\n");
2247 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2248 * @adapter: board private structure
2250 * If this function returns with an error, then it's possible one or
2251 * more of the rings is populated (while the rest are not). It is the
2252 * callers duty to clean those orphaned rings.
2254 * Return 0 on success, negative on failure
2256 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2260 for (i = 0; i < adapter->num_tx_queues; i++) {
2261 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2264 hw_dbg(&adapter->hw,
2265 "Allocation for Tx Queue %u failed\n", i);
2273 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2274 * @adapter: board private structure
2275 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2277 * Returns 0 on success, negative on failure
2279 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2280 struct ixgbevf_ring *rx_ring)
2282 struct pci_dev *pdev = adapter->pdev;
2285 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2286 rx_ring->rx_buffer_info = vzalloc(size);
2287 if (!rx_ring->rx_buffer_info)
2290 /* Round up to nearest 4K */
2291 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2292 rx_ring->size = ALIGN(rx_ring->size, 4096);
2294 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2295 &rx_ring->dma, GFP_KERNEL);
2297 if (!rx_ring->desc) {
2298 hw_dbg(&adapter->hw,
2299 "Unable to allocate memory for "
2300 "the receive descriptor ring\n");
2301 vfree(rx_ring->rx_buffer_info);
2302 rx_ring->rx_buffer_info = NULL;
2306 rx_ring->next_to_clean = 0;
2307 rx_ring->next_to_use = 0;
2315 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2316 * @adapter: board private structure
2318 * If this function returns with an error, then it's possible one or
2319 * more of the rings is populated (while the rest are not). It is the
2320 * callers duty to clean those orphaned rings.
2322 * Return 0 on success, negative on failure
2324 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2328 for (i = 0; i < adapter->num_rx_queues; i++) {
2329 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2332 hw_dbg(&adapter->hw,
2333 "Allocation for Rx Queue %u failed\n", i);
2340 * ixgbevf_free_rx_resources - Free Rx Resources
2341 * @adapter: board private structure
2342 * @rx_ring: ring to clean the resources from
2344 * Free all receive software resources
2346 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2347 struct ixgbevf_ring *rx_ring)
2349 struct pci_dev *pdev = adapter->pdev;
2351 ixgbevf_clean_rx_ring(adapter, rx_ring);
2353 vfree(rx_ring->rx_buffer_info);
2354 rx_ring->rx_buffer_info = NULL;
2356 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2359 rx_ring->desc = NULL;
2363 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2364 * @adapter: board private structure
2366 * Free all receive software resources
2368 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2372 for (i = 0; i < adapter->num_rx_queues; i++)
2373 if (adapter->rx_ring[i].desc)
2374 ixgbevf_free_rx_resources(adapter,
2375 &adapter->rx_ring[i]);
2379 * ixgbevf_open - Called when a network interface is made active
2380 * @netdev: network interface device structure
2382 * Returns 0 on success, negative value on failure
2384 * The open entry point is called when a network interface is made
2385 * active by the system (IFF_UP). At this point all resources needed
2386 * for transmit and receive operations are allocated, the interrupt
2387 * handler is registered with the OS, the watchdog timer is started,
2388 * and the stack is notified that the interface is ready.
2390 static int ixgbevf_open(struct net_device *netdev)
2392 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2393 struct ixgbe_hw *hw = &adapter->hw;
2396 /* disallow open during test */
2397 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2400 if (hw->adapter_stopped) {
2401 ixgbevf_reset(adapter);
2402 /* if adapter is still stopped then PF isn't up and
2403 * the vf can't start. */
2404 if (hw->adapter_stopped) {
2405 err = IXGBE_ERR_MBX;
2406 pr_err("Unable to start - perhaps the PF Driver isn't "
2408 goto err_setup_reset;
2412 ixgbevf_negotiate_api(adapter);
2414 /* allocate transmit descriptors */
2415 err = ixgbevf_setup_all_tx_resources(adapter);
2419 /* allocate receive descriptors */
2420 err = ixgbevf_setup_all_rx_resources(adapter);
2424 ixgbevf_configure(adapter);
2427 * Map the Tx/Rx rings to the vectors we were allotted.
2428 * if request_irq will be called in this function map_rings
2429 * must be called *before* up_complete
2431 ixgbevf_map_rings_to_vectors(adapter);
2433 ixgbevf_up_complete(adapter);
2435 /* clear any pending interrupts, may auto mask */
2436 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2437 err = ixgbevf_request_irq(adapter);
2441 ixgbevf_irq_enable(adapter);
2446 ixgbevf_down(adapter);
2447 ixgbevf_free_irq(adapter);
2449 ixgbevf_free_all_rx_resources(adapter);
2451 ixgbevf_free_all_tx_resources(adapter);
2452 ixgbevf_reset(adapter);
2460 * ixgbevf_close - Disables a network interface
2461 * @netdev: network interface device structure
2463 * Returns 0, this is not allowed to fail
2465 * The close entry point is called when an interface is de-activated
2466 * by the OS. The hardware is still under the drivers control, but
2467 * needs to be disabled. A global MAC reset is issued to stop the
2468 * hardware, and all transmit and receive resources are freed.
2470 static int ixgbevf_close(struct net_device *netdev)
2472 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2474 ixgbevf_down(adapter);
2475 ixgbevf_free_irq(adapter);
2477 ixgbevf_free_all_tx_resources(adapter);
2478 ixgbevf_free_all_rx_resources(adapter);
2483 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2484 u32 vlan_macip_lens, u32 type_tucmd,
2487 struct ixgbe_adv_tx_context_desc *context_desc;
2488 u16 i = tx_ring->next_to_use;
2490 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2493 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2495 /* set bits to identify this as an advanced context descriptor */
2496 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2498 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2499 context_desc->seqnum_seed = 0;
2500 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2501 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2504 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
2505 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2507 u32 vlan_macip_lens, type_tucmd;
2508 u32 mss_l4len_idx, l4len;
2510 if (!skb_is_gso(skb))
2513 if (skb_header_cloned(skb)) {
2514 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2519 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2520 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2522 if (skb->protocol == htons(ETH_P_IP)) {
2523 struct iphdr *iph = ip_hdr(skb);
2526 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2530 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2531 } else if (skb_is_gso_v6(skb)) {
2532 ipv6_hdr(skb)->payload_len = 0;
2533 tcp_hdr(skb)->check =
2534 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2535 &ipv6_hdr(skb)->daddr,
2539 /* compute header lengths */
2540 l4len = tcp_hdrlen(skb);
2542 *hdr_len = skb_transport_offset(skb) + l4len;
2544 /* mss_l4len_id: use 1 as index for TSO */
2545 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2546 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2547 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2549 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2550 vlan_macip_lens = skb_network_header_len(skb);
2551 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2552 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2554 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2555 type_tucmd, mss_l4len_idx);
2560 static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2561 struct sk_buff *skb, u32 tx_flags)
2566 u32 vlan_macip_lens = 0;
2567 u32 mss_l4len_idx = 0;
2570 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2572 switch (skb->protocol) {
2573 case __constant_htons(ETH_P_IP):
2574 vlan_macip_lens |= skb_network_header_len(skb);
2575 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2576 l4_hdr = ip_hdr(skb)->protocol;
2578 case __constant_htons(ETH_P_IPV6):
2579 vlan_macip_lens |= skb_network_header_len(skb);
2580 l4_hdr = ipv6_hdr(skb)->nexthdr;
2583 if (unlikely(net_ratelimit())) {
2584 dev_warn(tx_ring->dev,
2585 "partial checksum but proto=%x!\n",
2593 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2594 mss_l4len_idx = tcp_hdrlen(skb) <<
2595 IXGBE_ADVTXD_L4LEN_SHIFT;
2598 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2599 mss_l4len_idx = sizeof(struct sctphdr) <<
2600 IXGBE_ADVTXD_L4LEN_SHIFT;
2603 mss_l4len_idx = sizeof(struct udphdr) <<
2604 IXGBE_ADVTXD_L4LEN_SHIFT;
2607 if (unlikely(net_ratelimit())) {
2608 dev_warn(tx_ring->dev,
2609 "partial checksum but l4 proto=%x!\n",
2616 /* vlan_macip_lens: MACLEN, VLAN tag */
2617 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2618 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2620 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2621 type_tucmd, mss_l4len_idx);
2623 return (skb->ip_summed == CHECKSUM_PARTIAL);
2626 static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
2627 struct sk_buff *skb, u32 tx_flags,
2630 struct ixgbevf_tx_buffer *tx_buffer_info;
2632 unsigned int total = skb->len;
2633 unsigned int offset = 0, size;
2635 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2639 i = tx_ring->next_to_use;
2641 len = min(skb_headlen(skb), total);
2643 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2644 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2646 tx_buffer_info->length = size;
2647 tx_buffer_info->mapped_as_page = false;
2648 tx_buffer_info->dma = dma_map_single(tx_ring->dev,
2650 size, DMA_TO_DEVICE);
2651 if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
2653 tx_buffer_info->next_to_watch = i;
2660 if (i == tx_ring->count)
2664 for (f = 0; f < nr_frags; f++) {
2665 const struct skb_frag_struct *frag;
2667 frag = &skb_shinfo(skb)->frags[f];
2668 len = min((unsigned int)skb_frag_size(frag), total);
2672 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2673 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2675 tx_buffer_info->length = size;
2676 tx_buffer_info->dma =
2677 skb_frag_dma_map(tx_ring->dev, frag,
2678 offset, size, DMA_TO_DEVICE);
2679 tx_buffer_info->mapped_as_page = true;
2680 if (dma_mapping_error(tx_ring->dev,
2681 tx_buffer_info->dma))
2683 tx_buffer_info->next_to_watch = i;
2690 if (i == tx_ring->count)
2698 i = tx_ring->count - 1;
2701 tx_ring->tx_buffer_info[i].skb = skb;
2702 tx_ring->tx_buffer_info[first].next_to_watch = i;
2703 tx_ring->tx_buffer_info[first].time_stamp = jiffies;
2708 dev_err(tx_ring->dev, "TX DMA map failed\n");
2710 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2711 tx_buffer_info->dma = 0;
2712 tx_buffer_info->next_to_watch = 0;
2715 /* clear timestamp and dma mappings for remaining portion of packet */
2716 while (count >= 0) {
2720 i += tx_ring->count;
2721 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2722 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
2728 static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
2729 int count, u32 paylen, u8 hdr_len)
2731 union ixgbe_adv_tx_desc *tx_desc = NULL;
2732 struct ixgbevf_tx_buffer *tx_buffer_info;
2733 u32 olinfo_status = 0, cmd_type_len = 0;
2736 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2738 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2740 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2742 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2743 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2745 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2746 olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
2748 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2749 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2751 /* use index 1 context for tso */
2752 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2753 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2754 olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
2759 * Check Context must be set if Tx switch is enabled, which it
2760 * always is for case where virtual functions are running
2762 olinfo_status |= IXGBE_ADVTXD_CC;
2764 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2766 i = tx_ring->next_to_use;
2768 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2769 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2770 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2771 tx_desc->read.cmd_type_len =
2772 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2773 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2775 if (i == tx_ring->count)
2779 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2781 tx_ring->next_to_use = i;
2784 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2786 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
2788 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
2789 /* Herbert's original patch had:
2790 * smp_mb__after_netif_stop_queue();
2791 * but since that doesn't exist yet, just open code it. */
2794 /* We need to check again in a case another CPU has just
2795 * made room available. */
2796 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
2799 /* A reprieve! - use start_queue because it doesn't call schedule */
2800 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2801 ++adapter->restart_queue;
2805 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2807 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
2809 return __ixgbevf_maybe_stop_tx(tx_ring, size);
2812 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2814 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2815 struct ixgbevf_ring *tx_ring;
2817 unsigned int tx_flags = 0;
2820 u16 count = TXD_USE_COUNT(skb_headlen(skb));
2821 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2825 tx_ring = &adapter->tx_ring[r_idx];
2828 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
2829 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
2830 * + 2 desc gap to keep tail from touching head,
2831 * + 1 desc for context descriptor,
2832 * otherwise try next time
2834 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2835 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2836 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
2838 count += skb_shinfo(skb)->nr_frags;
2840 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
2842 return NETDEV_TX_BUSY;
2845 if (vlan_tx_tag_present(skb)) {
2846 tx_flags |= vlan_tx_tag_get(skb);
2847 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
2848 tx_flags |= IXGBE_TX_FLAGS_VLAN;
2851 first = tx_ring->next_to_use;
2853 if (skb->protocol == htons(ETH_P_IP))
2854 tx_flags |= IXGBE_TX_FLAGS_IPV4;
2855 tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
2857 dev_kfree_skb_any(skb);
2858 return NETDEV_TX_OK;
2862 tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
2863 else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
2864 tx_flags |= IXGBE_TX_FLAGS_CSUM;
2866 ixgbevf_tx_queue(tx_ring, tx_flags,
2867 ixgbevf_tx_map(tx_ring, skb, tx_flags, first),
2870 * Force memory writes to complete before letting h/w
2871 * know there are new descriptors to fetch. (Only
2872 * applicable for weak-ordered memory model archs,
2877 writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
2879 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
2881 return NETDEV_TX_OK;
2885 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
2886 * @netdev: network interface device structure
2887 * @p: pointer to an address structure
2889 * Returns 0 on success, negative on failure
2891 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
2893 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2894 struct ixgbe_hw *hw = &adapter->hw;
2895 struct sockaddr *addr = p;
2897 if (!is_valid_ether_addr(addr->sa_data))
2898 return -EADDRNOTAVAIL;
2900 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2901 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
2903 spin_lock(&adapter->mbx_lock);
2905 if (hw->mac.ops.set_rar)
2906 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
2908 spin_unlock(&adapter->mbx_lock);
2914 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
2915 * @netdev: network interface device structure
2916 * @new_mtu: new value for maximum frame size
2918 * Returns 0 on success, negative on failure
2920 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
2922 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2923 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2924 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
2926 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
2927 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
2929 /* MTU < 68 is an error and causes problems on some kernels */
2930 if ((new_mtu < 68) || (max_frame > max_possible_frame))
2933 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
2934 netdev->mtu, new_mtu);
2935 /* must set new MTU before calling down or up */
2936 netdev->mtu = new_mtu;
2938 if (netif_running(netdev))
2939 ixgbevf_reinit_locked(adapter);
2944 static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
2946 struct net_device *netdev = pci_get_drvdata(pdev);
2947 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2952 netif_device_detach(netdev);
2954 if (netif_running(netdev)) {
2956 ixgbevf_down(adapter);
2957 ixgbevf_free_irq(adapter);
2958 ixgbevf_free_all_tx_resources(adapter);
2959 ixgbevf_free_all_rx_resources(adapter);
2963 ixgbevf_clear_interrupt_scheme(adapter);
2966 retval = pci_save_state(pdev);
2971 pci_disable_device(pdev);
2977 static int ixgbevf_resume(struct pci_dev *pdev)
2979 struct ixgbevf_adapter *adapter = pci_get_drvdata(pdev);
2980 struct net_device *netdev = adapter->netdev;
2983 pci_set_power_state(pdev, PCI_D0);
2984 pci_restore_state(pdev);
2986 * pci_restore_state clears dev->state_saved so call
2987 * pci_save_state to restore it.
2989 pci_save_state(pdev);
2991 err = pci_enable_device_mem(pdev);
2993 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
2996 pci_set_master(pdev);
2999 err = ixgbevf_init_interrupt_scheme(adapter);
3002 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3006 ixgbevf_reset(adapter);
3008 if (netif_running(netdev)) {
3009 err = ixgbevf_open(netdev);
3014 netif_device_attach(netdev);
3019 #endif /* CONFIG_PM */
3020 static void ixgbevf_shutdown(struct pci_dev *pdev)
3022 ixgbevf_suspend(pdev, PMSG_SUSPEND);
3025 static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3026 struct rtnl_link_stats64 *stats)
3028 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3031 const struct ixgbevf_ring *ring;
3034 ixgbevf_update_stats(adapter);
3036 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3038 for (i = 0; i < adapter->num_rx_queues; i++) {
3039 ring = &adapter->rx_ring[i];
3041 start = u64_stats_fetch_begin_bh(&ring->syncp);
3042 bytes = ring->total_bytes;
3043 packets = ring->total_packets;
3044 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3045 stats->rx_bytes += bytes;
3046 stats->rx_packets += packets;
3049 for (i = 0; i < adapter->num_tx_queues; i++) {
3050 ring = &adapter->tx_ring[i];
3052 start = u64_stats_fetch_begin_bh(&ring->syncp);
3053 bytes = ring->total_bytes;
3054 packets = ring->total_packets;
3055 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3056 stats->tx_bytes += bytes;
3057 stats->tx_packets += packets;
3063 static const struct net_device_ops ixgbevf_netdev_ops = {
3064 .ndo_open = ixgbevf_open,
3065 .ndo_stop = ixgbevf_close,
3066 .ndo_start_xmit = ixgbevf_xmit_frame,
3067 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
3068 .ndo_get_stats64 = ixgbevf_get_stats,
3069 .ndo_validate_addr = eth_validate_addr,
3070 .ndo_set_mac_address = ixgbevf_set_mac,
3071 .ndo_change_mtu = ixgbevf_change_mtu,
3072 .ndo_tx_timeout = ixgbevf_tx_timeout,
3073 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3074 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
3077 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3079 dev->netdev_ops = &ixgbevf_netdev_ops;
3080 ixgbevf_set_ethtool_ops(dev);
3081 dev->watchdog_timeo = 5 * HZ;
3085 * ixgbevf_probe - Device Initialization Routine
3086 * @pdev: PCI device information struct
3087 * @ent: entry in ixgbevf_pci_tbl
3089 * Returns 0 on success, negative on failure
3091 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3092 * The OS initialization, configuring of the adapter private structure,
3093 * and a hardware reset occur.
3095 static int __devinit ixgbevf_probe(struct pci_dev *pdev,
3096 const struct pci_device_id *ent)
3098 struct net_device *netdev;
3099 struct ixgbevf_adapter *adapter = NULL;
3100 struct ixgbe_hw *hw = NULL;
3101 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3102 static int cards_found;
3103 int err, pci_using_dac;
3105 err = pci_enable_device(pdev);
3109 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3110 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3113 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3115 err = dma_set_coherent_mask(&pdev->dev,
3118 dev_err(&pdev->dev, "No usable DMA "
3119 "configuration, aborting\n");
3126 err = pci_request_regions(pdev, ixgbevf_driver_name);
3128 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3132 pci_set_master(pdev);
3134 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3138 goto err_alloc_etherdev;
3141 SET_NETDEV_DEV(netdev, &pdev->dev);
3143 pci_set_drvdata(pdev, netdev);
3144 adapter = netdev_priv(netdev);
3146 adapter->netdev = netdev;
3147 adapter->pdev = pdev;
3150 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3153 * call save state here in standalone driver because it relies on
3154 * adapter struct to exist, and needs to call netdev_priv
3156 pci_save_state(pdev);
3158 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3159 pci_resource_len(pdev, 0));
3165 ixgbevf_assign_netdev_ops(netdev);
3167 adapter->bd_number = cards_found;
3170 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3171 hw->mac.type = ii->mac;
3173 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3174 sizeof(struct ixgbe_mbx_operations));
3176 /* setup the private structure */
3177 err = ixgbevf_sw_init(adapter);
3181 /* The HW MAC address was set and/or determined in sw_init */
3182 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3184 if (!is_valid_ether_addr(netdev->dev_addr)) {
3185 pr_err("invalid MAC address\n");
3190 netdev->hw_features = NETIF_F_SG |
3197 netdev->features = netdev->hw_features |
3198 NETIF_F_HW_VLAN_TX |
3199 NETIF_F_HW_VLAN_RX |
3200 NETIF_F_HW_VLAN_FILTER;
3202 netdev->vlan_features |= NETIF_F_TSO;
3203 netdev->vlan_features |= NETIF_F_TSO6;
3204 netdev->vlan_features |= NETIF_F_IP_CSUM;
3205 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3206 netdev->vlan_features |= NETIF_F_SG;
3209 netdev->features |= NETIF_F_HIGHDMA;
3211 netdev->priv_flags |= IFF_UNICAST_FLT;
3213 init_timer(&adapter->watchdog_timer);
3214 adapter->watchdog_timer.function = ixgbevf_watchdog;
3215 adapter->watchdog_timer.data = (unsigned long)adapter;
3217 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3218 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3220 err = ixgbevf_init_interrupt_scheme(adapter);
3224 /* pick up the PCI bus settings for reporting later */
3225 if (hw->mac.ops.get_bus_info)
3226 hw->mac.ops.get_bus_info(hw);
3228 strcpy(netdev->name, "eth%d");
3230 err = register_netdev(netdev);
3234 netif_carrier_off(netdev);
3236 ixgbevf_init_last_counter_stats(adapter);
3238 /* print the MAC address */
3239 hw_dbg(hw, "%pM\n", netdev->dev_addr);
3241 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3243 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3248 ixgbevf_clear_interrupt_scheme(adapter);
3250 ixgbevf_reset_interrupt_capability(adapter);
3251 iounmap(hw->hw_addr);
3253 free_netdev(netdev);
3255 pci_release_regions(pdev);
3258 pci_disable_device(pdev);
3263 * ixgbevf_remove - Device Removal Routine
3264 * @pdev: PCI device information struct
3266 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3267 * that it should release a PCI device. The could be caused by a
3268 * Hot-Plug event, or because the driver is going to be removed from
3271 static void __devexit ixgbevf_remove(struct pci_dev *pdev)
3273 struct net_device *netdev = pci_get_drvdata(pdev);
3274 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3276 set_bit(__IXGBEVF_DOWN, &adapter->state);
3278 del_timer_sync(&adapter->watchdog_timer);
3280 cancel_work_sync(&adapter->reset_task);
3281 cancel_work_sync(&adapter->watchdog_task);
3283 if (netdev->reg_state == NETREG_REGISTERED)
3284 unregister_netdev(netdev);
3286 ixgbevf_clear_interrupt_scheme(adapter);
3287 ixgbevf_reset_interrupt_capability(adapter);
3289 iounmap(adapter->hw.hw_addr);
3290 pci_release_regions(pdev);
3292 hw_dbg(&adapter->hw, "Remove complete\n");
3294 kfree(adapter->tx_ring);
3295 kfree(adapter->rx_ring);
3297 free_netdev(netdev);
3299 pci_disable_device(pdev);
3303 * ixgbevf_io_error_detected - called when PCI error is detected
3304 * @pdev: Pointer to PCI device
3305 * @state: The current pci connection state
3307 * This function is called after a PCI bus error affecting
3308 * this device has been detected.
3310 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3311 pci_channel_state_t state)
3313 struct net_device *netdev = pci_get_drvdata(pdev);
3314 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3316 netif_device_detach(netdev);
3318 if (state == pci_channel_io_perm_failure)
3319 return PCI_ERS_RESULT_DISCONNECT;
3321 if (netif_running(netdev))
3322 ixgbevf_down(adapter);
3324 pci_disable_device(pdev);
3326 /* Request a slot slot reset. */
3327 return PCI_ERS_RESULT_NEED_RESET;
3331 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3332 * @pdev: Pointer to PCI device
3334 * Restart the card from scratch, as if from a cold-boot. Implementation
3335 * resembles the first-half of the ixgbevf_resume routine.
3337 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3339 struct net_device *netdev = pci_get_drvdata(pdev);
3340 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3342 if (pci_enable_device_mem(pdev)) {
3344 "Cannot re-enable PCI device after reset.\n");
3345 return PCI_ERS_RESULT_DISCONNECT;
3348 pci_set_master(pdev);
3350 ixgbevf_reset(adapter);
3352 return PCI_ERS_RESULT_RECOVERED;
3356 * ixgbevf_io_resume - called when traffic can start flowing again.
3357 * @pdev: Pointer to PCI device
3359 * This callback is called when the error recovery driver tells us that
3360 * its OK to resume normal operation. Implementation resembles the
3361 * second-half of the ixgbevf_resume routine.
3363 static void ixgbevf_io_resume(struct pci_dev *pdev)
3365 struct net_device *netdev = pci_get_drvdata(pdev);
3366 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3368 if (netif_running(netdev))
3369 ixgbevf_up(adapter);
3371 netif_device_attach(netdev);
3374 /* PCI Error Recovery (ERS) */
3375 static struct pci_error_handlers ixgbevf_err_handler = {
3376 .error_detected = ixgbevf_io_error_detected,
3377 .slot_reset = ixgbevf_io_slot_reset,
3378 .resume = ixgbevf_io_resume,
3381 static struct pci_driver ixgbevf_driver = {
3382 .name = ixgbevf_driver_name,
3383 .id_table = ixgbevf_pci_tbl,
3384 .probe = ixgbevf_probe,
3385 .remove = __devexit_p(ixgbevf_remove),
3387 /* Power Management Hooks */
3388 .suspend = ixgbevf_suspend,
3389 .resume = ixgbevf_resume,
3391 .shutdown = ixgbevf_shutdown,
3392 .err_handler = &ixgbevf_err_handler
3396 * ixgbevf_init_module - Driver Registration Routine
3398 * ixgbevf_init_module is the first routine called when the driver is
3399 * loaded. All it does is register with the PCI subsystem.
3401 static int __init ixgbevf_init_module(void)
3404 pr_info("%s - version %s\n", ixgbevf_driver_string,
3405 ixgbevf_driver_version);
3407 pr_info("%s\n", ixgbevf_copyright);
3409 ret = pci_register_driver(&ixgbevf_driver);
3413 module_init(ixgbevf_init_module);
3416 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3418 * ixgbevf_exit_module is called just before the driver is removed
3421 static void __exit ixgbevf_exit_module(void)
3423 pci_unregister_driver(&ixgbevf_driver);
3428 * ixgbevf_get_hw_dev_name - return device name string
3429 * used by hardware layer to print debugging information
3431 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3433 struct ixgbevf_adapter *adapter = hw->back;
3434 return adapter->netdev->name;
3438 module_exit(ixgbevf_exit_module);
3440 /* ixgbevf_main.c */