ixgbevf: Add VF DCB + SR-IOV support
[linux-2.6/cjktty.git] / drivers / net / ethernet / intel / ixgbevf / ixgbevf_main.c
blob33444b5b5105cf2398bcf9171c749a6b2919fe5c
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
13 more details.
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".
22 Contact Information:
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>
42 #include <linux/in.h>
43 #include <linux/ip.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>
51 #include <linux/if.h>
52 #include <linux/if_vlan.h>
53 #include <linux/prefetch.h>
55 #include "ixgbevf.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),
81 board_82599_vf},
82 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF),
83 board_X540_vf},
85 /* required last entry */
86 {0, }
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)");
100 /* forward decls */
101 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
102 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
104 static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
105 struct ixgbevf_ring *rx_ring,
106 u32 val)
109 * Force memory writes to complete before letting h/w
110 * know there are new descriptors to fetch. (Only
111 * applicable for weak-ordered memory model archs,
112 * such as IA-64).
114 wmb();
115 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
119 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
120 * @adapter: pointer to adapter struct
121 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
122 * @queue: queue to map the corresponding interrupt to
123 * @msix_vector: the vector to map to the corresponding queue
126 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
127 u8 queue, u8 msix_vector)
129 u32 ivar, index;
130 struct ixgbe_hw *hw = &adapter->hw;
131 if (direction == -1) {
132 /* other causes */
133 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
134 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
135 ivar &= ~0xFF;
136 ivar |= msix_vector;
137 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
138 } else {
139 /* tx or rx causes */
140 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
141 index = ((16 * (queue & 1)) + (8 * direction));
142 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
143 ivar &= ~(0xFF << index);
144 ivar |= (msix_vector << index);
145 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
149 static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
150 struct ixgbevf_tx_buffer
151 *tx_buffer_info)
153 if (tx_buffer_info->dma) {
154 if (tx_buffer_info->mapped_as_page)
155 dma_unmap_page(tx_ring->dev,
156 tx_buffer_info->dma,
157 tx_buffer_info->length,
158 DMA_TO_DEVICE);
159 else
160 dma_unmap_single(tx_ring->dev,
161 tx_buffer_info->dma,
162 tx_buffer_info->length,
163 DMA_TO_DEVICE);
164 tx_buffer_info->dma = 0;
166 if (tx_buffer_info->skb) {
167 dev_kfree_skb_any(tx_buffer_info->skb);
168 tx_buffer_info->skb = NULL;
170 tx_buffer_info->time_stamp = 0;
171 /* tx_buffer_info must be completely set up in the transmit path */
174 #define IXGBE_MAX_TXD_PWR 14
175 #define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
177 /* Tx Descriptors needed, worst case */
178 #define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
179 #define DESC_NEEDED (MAX_SKB_FRAGS + 4)
181 static void ixgbevf_tx_timeout(struct net_device *netdev);
184 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
185 * @q_vector: board private structure
186 * @tx_ring: tx ring to clean
188 static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
189 struct ixgbevf_ring *tx_ring)
191 struct ixgbevf_adapter *adapter = q_vector->adapter;
192 union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
193 struct ixgbevf_tx_buffer *tx_buffer_info;
194 unsigned int i, eop, count = 0;
195 unsigned int total_bytes = 0, total_packets = 0;
197 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
198 return true;
200 i = tx_ring->next_to_clean;
201 eop = tx_ring->tx_buffer_info[i].next_to_watch;
202 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
204 while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
205 (count < tx_ring->count)) {
206 bool cleaned = false;
207 rmb(); /* read buffer_info after eop_desc */
208 /* eop could change between read and DD-check */
209 if (unlikely(eop != tx_ring->tx_buffer_info[i].next_to_watch))
210 goto cont_loop;
211 for ( ; !cleaned; count++) {
212 struct sk_buff *skb;
213 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
214 tx_buffer_info = &tx_ring->tx_buffer_info[i];
215 cleaned = (i == eop);
216 skb = tx_buffer_info->skb;
218 if (cleaned && skb) {
219 unsigned int segs, bytecount;
221 /* gso_segs is currently only valid for tcp */
222 segs = skb_shinfo(skb)->gso_segs ?: 1;
223 /* multiply data chunks by size of headers */
224 bytecount = ((segs - 1) * skb_headlen(skb)) +
225 skb->len;
226 total_packets += segs;
227 total_bytes += bytecount;
230 ixgbevf_unmap_and_free_tx_resource(tx_ring,
231 tx_buffer_info);
233 tx_desc->wb.status = 0;
235 i++;
236 if (i == tx_ring->count)
237 i = 0;
240 cont_loop:
241 eop = tx_ring->tx_buffer_info[i].next_to_watch;
242 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
245 tx_ring->next_to_clean = i;
247 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
248 if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
249 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
250 /* Make sure that anybody stopping the queue after this
251 * sees the new next_to_clean.
253 smp_mb();
254 if (__netif_subqueue_stopped(tx_ring->netdev,
255 tx_ring->queue_index) &&
256 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
257 netif_wake_subqueue(tx_ring->netdev,
258 tx_ring->queue_index);
259 ++adapter->restart_queue;
263 u64_stats_update_begin(&tx_ring->syncp);
264 tx_ring->total_bytes += total_bytes;
265 tx_ring->total_packets += total_packets;
266 u64_stats_update_end(&tx_ring->syncp);
267 q_vector->tx.total_bytes += total_bytes;
268 q_vector->tx.total_packets += total_packets;
270 return count < tx_ring->count;
274 * ixgbevf_receive_skb - Send a completed packet up the stack
275 * @q_vector: structure containing interrupt and ring information
276 * @skb: packet to send up
277 * @status: hardware indication of status of receive
278 * @rx_desc: rx descriptor
280 static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
281 struct sk_buff *skb, u8 status,
282 union ixgbe_adv_rx_desc *rx_desc)
284 struct ixgbevf_adapter *adapter = q_vector->adapter;
285 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
286 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
288 if (is_vlan && test_bit(tag & VLAN_VID_MASK, adapter->active_vlans))
289 __vlan_hwaccel_put_tag(skb, tag);
291 napi_gro_receive(&q_vector->napi, skb);
295 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
296 * @adapter: address of board private structure
297 * @status_err: hardware indication of status of receive
298 * @skb: skb currently being received and modified
300 static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
301 struct ixgbevf_ring *ring,
302 u32 status_err, struct sk_buff *skb)
304 skb_checksum_none_assert(skb);
306 /* Rx csum disabled */
307 if (!(ring->netdev->features & NETIF_F_RXCSUM))
308 return;
310 /* if IP and error */
311 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
312 (status_err & IXGBE_RXDADV_ERR_IPE)) {
313 adapter->hw_csum_rx_error++;
314 return;
317 if (!(status_err & IXGBE_RXD_STAT_L4CS))
318 return;
320 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
321 adapter->hw_csum_rx_error++;
322 return;
325 /* It must be a TCP or UDP packet with a valid checksum */
326 skb->ip_summed = CHECKSUM_UNNECESSARY;
327 adapter->hw_csum_rx_good++;
331 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
332 * @adapter: address of board private structure
334 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
335 struct ixgbevf_ring *rx_ring,
336 int cleaned_count)
338 struct pci_dev *pdev = adapter->pdev;
339 union ixgbe_adv_rx_desc *rx_desc;
340 struct ixgbevf_rx_buffer *bi;
341 struct sk_buff *skb;
342 unsigned int i = rx_ring->next_to_use;
344 bi = &rx_ring->rx_buffer_info[i];
346 while (cleaned_count--) {
347 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
348 skb = bi->skb;
349 if (!skb) {
350 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
351 rx_ring->rx_buf_len);
352 if (!skb) {
353 adapter->alloc_rx_buff_failed++;
354 goto no_buffers;
356 bi->skb = skb;
358 if (!bi->dma) {
359 bi->dma = dma_map_single(&pdev->dev, skb->data,
360 rx_ring->rx_buf_len,
361 DMA_FROM_DEVICE);
363 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
365 i++;
366 if (i == rx_ring->count)
367 i = 0;
368 bi = &rx_ring->rx_buffer_info[i];
371 no_buffers:
372 if (rx_ring->next_to_use != i) {
373 rx_ring->next_to_use = i;
375 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
379 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
380 u32 qmask)
382 struct ixgbe_hw *hw = &adapter->hw;
384 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
387 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
388 struct ixgbevf_ring *rx_ring,
389 int budget)
391 struct ixgbevf_adapter *adapter = q_vector->adapter;
392 struct pci_dev *pdev = adapter->pdev;
393 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
394 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
395 struct sk_buff *skb;
396 unsigned int i;
397 u32 len, staterr;
398 int cleaned_count = 0;
399 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
401 i = rx_ring->next_to_clean;
402 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
403 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
404 rx_buffer_info = &rx_ring->rx_buffer_info[i];
406 while (staterr & IXGBE_RXD_STAT_DD) {
407 if (!budget)
408 break;
409 budget--;
411 rmb(); /* read descriptor and rx_buffer_info after status DD */
412 len = le16_to_cpu(rx_desc->wb.upper.length);
413 skb = rx_buffer_info->skb;
414 prefetch(skb->data - NET_IP_ALIGN);
415 rx_buffer_info->skb = NULL;
417 if (rx_buffer_info->dma) {
418 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
419 rx_ring->rx_buf_len,
420 DMA_FROM_DEVICE);
421 rx_buffer_info->dma = 0;
422 skb_put(skb, len);
425 i++;
426 if (i == rx_ring->count)
427 i = 0;
429 next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
430 prefetch(next_rxd);
431 cleaned_count++;
433 next_buffer = &rx_ring->rx_buffer_info[i];
435 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
436 skb->next = next_buffer->skb;
437 IXGBE_CB(skb->next)->prev = skb;
438 adapter->non_eop_descs++;
439 goto next_desc;
442 /* we should not be chaining buffers, if we did drop the skb */
443 if (IXGBE_CB(skb)->prev) {
444 do {
445 struct sk_buff *this = skb;
446 skb = IXGBE_CB(skb)->prev;
447 dev_kfree_skb(this);
448 } while (skb);
449 goto next_desc;
452 /* ERR_MASK will only have valid bits if EOP set */
453 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
454 dev_kfree_skb_irq(skb);
455 goto next_desc;
458 ixgbevf_rx_checksum(adapter, rx_ring, staterr, skb);
460 /* probably a little skewed due to removing CRC */
461 total_rx_bytes += skb->len;
462 total_rx_packets++;
465 * Work around issue of some types of VM to VM loop back
466 * packets not getting split correctly
468 if (staterr & IXGBE_RXD_STAT_LB) {
469 u32 header_fixup_len = skb_headlen(skb);
470 if (header_fixup_len < 14)
471 skb_push(skb, header_fixup_len);
473 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
475 ixgbevf_receive_skb(q_vector, skb, staterr, rx_desc);
477 next_desc:
478 rx_desc->wb.upper.status_error = 0;
480 /* return some buffers to hardware, one at a time is too slow */
481 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
482 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
483 cleaned_count);
484 cleaned_count = 0;
487 /* use prefetched values */
488 rx_desc = next_rxd;
489 rx_buffer_info = &rx_ring->rx_buffer_info[i];
491 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
494 rx_ring->next_to_clean = i;
495 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
497 if (cleaned_count)
498 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
500 u64_stats_update_begin(&rx_ring->syncp);
501 rx_ring->total_packets += total_rx_packets;
502 rx_ring->total_bytes += total_rx_bytes;
503 u64_stats_update_end(&rx_ring->syncp);
504 q_vector->rx.total_packets += total_rx_packets;
505 q_vector->rx.total_bytes += total_rx_bytes;
507 return !!budget;
511 * ixgbevf_poll - NAPI polling calback
512 * @napi: napi struct with our devices info in it
513 * @budget: amount of work driver is allowed to do this pass, in packets
515 * This function will clean more than one or more rings associated with a
516 * q_vector.
518 static int ixgbevf_poll(struct napi_struct *napi, int budget)
520 struct ixgbevf_q_vector *q_vector =
521 container_of(napi, struct ixgbevf_q_vector, napi);
522 struct ixgbevf_adapter *adapter = q_vector->adapter;
523 struct ixgbevf_ring *ring;
524 int per_ring_budget;
525 bool clean_complete = true;
527 ixgbevf_for_each_ring(ring, q_vector->tx)
528 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
530 /* attempt to distribute budget to each queue fairly, but don't allow
531 * the budget to go below 1 because we'll exit polling */
532 if (q_vector->rx.count > 1)
533 per_ring_budget = max(budget/q_vector->rx.count, 1);
534 else
535 per_ring_budget = budget;
537 ixgbevf_for_each_ring(ring, q_vector->rx)
538 clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
539 per_ring_budget);
541 /* If all work not completed, return budget and keep polling */
542 if (!clean_complete)
543 return budget;
544 /* all work done, exit the polling mode */
545 napi_complete(napi);
546 if (adapter->rx_itr_setting & 1)
547 ixgbevf_set_itr(q_vector);
548 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
549 ixgbevf_irq_enable_queues(adapter,
550 1 << q_vector->v_idx);
552 return 0;
556 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
557 * @q_vector: structure containing interrupt and ring information
559 static void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
561 struct ixgbevf_adapter *adapter = q_vector->adapter;
562 struct ixgbe_hw *hw = &adapter->hw;
563 int v_idx = q_vector->v_idx;
564 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
567 * set the WDIS bit to not clear the timer bits and cause an
568 * immediate assertion of the interrupt
570 itr_reg |= IXGBE_EITR_CNT_WDIS;
572 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
576 * ixgbevf_configure_msix - Configure MSI-X hardware
577 * @adapter: board private structure
579 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
580 * interrupts.
582 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
584 struct ixgbevf_q_vector *q_vector;
585 int q_vectors, v_idx;
587 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
588 adapter->eims_enable_mask = 0;
591 * Populate the IVAR table and set the ITR values to the
592 * corresponding register.
594 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
595 struct ixgbevf_ring *ring;
596 q_vector = adapter->q_vector[v_idx];
598 ixgbevf_for_each_ring(ring, q_vector->rx)
599 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
601 ixgbevf_for_each_ring(ring, q_vector->tx)
602 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
604 if (q_vector->tx.ring && !q_vector->rx.ring) {
605 /* tx only vector */
606 if (adapter->tx_itr_setting == 1)
607 q_vector->itr = IXGBE_10K_ITR;
608 else
609 q_vector->itr = adapter->tx_itr_setting;
610 } else {
611 /* rx or rx/tx vector */
612 if (adapter->rx_itr_setting == 1)
613 q_vector->itr = IXGBE_20K_ITR;
614 else
615 q_vector->itr = adapter->rx_itr_setting;
618 /* add q_vector eims value to global eims_enable_mask */
619 adapter->eims_enable_mask |= 1 << v_idx;
621 ixgbevf_write_eitr(q_vector);
624 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
625 /* setup eims_other and add value to global eims_enable_mask */
626 adapter->eims_other = 1 << v_idx;
627 adapter->eims_enable_mask |= adapter->eims_other;
630 enum latency_range {
631 lowest_latency = 0,
632 low_latency = 1,
633 bulk_latency = 2,
634 latency_invalid = 255
638 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
639 * @q_vector: structure containing interrupt and ring information
640 * @ring_container: structure containing ring performance data
642 * Stores a new ITR value based on packets and byte
643 * counts during the last interrupt. The advantage of per interrupt
644 * computation is faster updates and more accurate ITR for the current
645 * traffic pattern. Constants in this function were computed
646 * based on theoretical maximum wire speed and thresholds were set based
647 * on testing data as well as attempting to minimize response time
648 * while increasing bulk throughput.
650 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
651 struct ixgbevf_ring_container *ring_container)
653 int bytes = ring_container->total_bytes;
654 int packets = ring_container->total_packets;
655 u32 timepassed_us;
656 u64 bytes_perint;
657 u8 itr_setting = ring_container->itr;
659 if (packets == 0)
660 return;
662 /* simple throttlerate management
663 * 0-20MB/s lowest (100000 ints/s)
664 * 20-100MB/s low (20000 ints/s)
665 * 100-1249MB/s bulk (8000 ints/s)
667 /* what was last interrupt timeslice? */
668 timepassed_us = q_vector->itr >> 2;
669 bytes_perint = bytes / timepassed_us; /* bytes/usec */
671 switch (itr_setting) {
672 case lowest_latency:
673 if (bytes_perint > 10)
674 itr_setting = low_latency;
675 break;
676 case low_latency:
677 if (bytes_perint > 20)
678 itr_setting = bulk_latency;
679 else if (bytes_perint <= 10)
680 itr_setting = lowest_latency;
681 break;
682 case bulk_latency:
683 if (bytes_perint <= 20)
684 itr_setting = low_latency;
685 break;
688 /* clear work counters since we have the values we need */
689 ring_container->total_bytes = 0;
690 ring_container->total_packets = 0;
692 /* write updated itr to ring container */
693 ring_container->itr = itr_setting;
696 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
698 u32 new_itr = q_vector->itr;
699 u8 current_itr;
701 ixgbevf_update_itr(q_vector, &q_vector->tx);
702 ixgbevf_update_itr(q_vector, &q_vector->rx);
704 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
706 switch (current_itr) {
707 /* counts and packets in update_itr are dependent on these numbers */
708 case lowest_latency:
709 new_itr = IXGBE_100K_ITR;
710 break;
711 case low_latency:
712 new_itr = IXGBE_20K_ITR;
713 break;
714 case bulk_latency:
715 default:
716 new_itr = IXGBE_8K_ITR;
717 break;
720 if (new_itr != q_vector->itr) {
721 /* do an exponential smoothing */
722 new_itr = (10 * new_itr * q_vector->itr) /
723 ((9 * new_itr) + q_vector->itr);
725 /* save the algorithm value here */
726 q_vector->itr = new_itr;
728 ixgbevf_write_eitr(q_vector);
732 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
734 struct ixgbevf_adapter *adapter = data;
735 struct ixgbe_hw *hw = &adapter->hw;
737 hw->mac.get_link_status = 1;
739 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
740 mod_timer(&adapter->watchdog_timer, jiffies);
742 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
744 return IRQ_HANDLED;
749 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
750 * @irq: unused
751 * @data: pointer to our q_vector struct for this interrupt vector
753 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
755 struct ixgbevf_q_vector *q_vector = data;
757 /* EIAM disabled interrupts (on this vector) for us */
758 if (q_vector->rx.ring || q_vector->tx.ring)
759 napi_schedule(&q_vector->napi);
761 return IRQ_HANDLED;
764 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
765 int r_idx)
767 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
769 a->rx_ring[r_idx].next = q_vector->rx.ring;
770 q_vector->rx.ring = &a->rx_ring[r_idx];
771 q_vector->rx.count++;
774 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
775 int t_idx)
777 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
779 a->tx_ring[t_idx].next = q_vector->tx.ring;
780 q_vector->tx.ring = &a->tx_ring[t_idx];
781 q_vector->tx.count++;
785 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
786 * @adapter: board private structure to initialize
788 * This function maps descriptor rings to the queue-specific vectors
789 * we were allotted through the MSI-X enabling code. Ideally, we'd have
790 * one vector per ring/queue, but on a constrained vector budget, we
791 * group the rings as "efficiently" as possible. You would add new
792 * mapping configurations in here.
794 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
796 int q_vectors;
797 int v_start = 0;
798 int rxr_idx = 0, txr_idx = 0;
799 int rxr_remaining = adapter->num_rx_queues;
800 int txr_remaining = adapter->num_tx_queues;
801 int i, j;
802 int rqpv, tqpv;
803 int err = 0;
805 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
808 * The ideal configuration...
809 * We have enough vectors to map one per queue.
811 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
812 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
813 map_vector_to_rxq(adapter, v_start, rxr_idx);
815 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
816 map_vector_to_txq(adapter, v_start, txr_idx);
817 goto out;
821 * If we don't have enough vectors for a 1-to-1
822 * mapping, we'll have to group them so there are
823 * multiple queues per vector.
825 /* Re-adjusting *qpv takes care of the remainder. */
826 for (i = v_start; i < q_vectors; i++) {
827 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
828 for (j = 0; j < rqpv; j++) {
829 map_vector_to_rxq(adapter, i, rxr_idx);
830 rxr_idx++;
831 rxr_remaining--;
834 for (i = v_start; i < q_vectors; i++) {
835 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
836 for (j = 0; j < tqpv; j++) {
837 map_vector_to_txq(adapter, i, txr_idx);
838 txr_idx++;
839 txr_remaining--;
843 out:
844 return err;
848 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
849 * @adapter: board private structure
851 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
852 * interrupts from the kernel.
854 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
856 struct net_device *netdev = adapter->netdev;
857 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
858 int vector, err;
859 int ri = 0, ti = 0;
861 for (vector = 0; vector < q_vectors; vector++) {
862 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
863 struct msix_entry *entry = &adapter->msix_entries[vector];
865 if (q_vector->tx.ring && q_vector->rx.ring) {
866 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
867 "%s-%s-%d", netdev->name, "TxRx", ri++);
868 ti++;
869 } else if (q_vector->rx.ring) {
870 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
871 "%s-%s-%d", netdev->name, "rx", ri++);
872 } else if (q_vector->tx.ring) {
873 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
874 "%s-%s-%d", netdev->name, "tx", ti++);
875 } else {
876 /* skip this unused q_vector */
877 continue;
879 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
880 q_vector->name, q_vector);
881 if (err) {
882 hw_dbg(&adapter->hw,
883 "request_irq failed for MSIX interrupt "
884 "Error: %d\n", err);
885 goto free_queue_irqs;
889 err = request_irq(adapter->msix_entries[vector].vector,
890 &ixgbevf_msix_other, 0, netdev->name, adapter);
891 if (err) {
892 hw_dbg(&adapter->hw,
893 "request_irq for msix_other failed: %d\n", err);
894 goto free_queue_irqs;
897 return 0;
899 free_queue_irqs:
900 while (vector) {
901 vector--;
902 free_irq(adapter->msix_entries[vector].vector,
903 adapter->q_vector[vector]);
905 pci_disable_msix(adapter->pdev);
906 kfree(adapter->msix_entries);
907 adapter->msix_entries = NULL;
908 return err;
911 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
913 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
915 for (i = 0; i < q_vectors; i++) {
916 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
917 q_vector->rx.ring = NULL;
918 q_vector->tx.ring = NULL;
919 q_vector->rx.count = 0;
920 q_vector->tx.count = 0;
925 * ixgbevf_request_irq - initialize interrupts
926 * @adapter: board private structure
928 * Attempts to configure interrupts using the best available
929 * capabilities of the hardware and kernel.
931 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
933 int err = 0;
935 err = ixgbevf_request_msix_irqs(adapter);
937 if (err)
938 hw_dbg(&adapter->hw,
939 "request_irq failed, Error %d\n", err);
941 return err;
944 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
946 int i, q_vectors;
948 q_vectors = adapter->num_msix_vectors;
949 i = q_vectors - 1;
951 free_irq(adapter->msix_entries[i].vector, adapter);
952 i--;
954 for (; i >= 0; i--) {
955 /* free only the irqs that were actually requested */
956 if (!adapter->q_vector[i]->rx.ring &&
957 !adapter->q_vector[i]->tx.ring)
958 continue;
960 free_irq(adapter->msix_entries[i].vector,
961 adapter->q_vector[i]);
964 ixgbevf_reset_q_vectors(adapter);
968 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
969 * @adapter: board private structure
971 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
973 struct ixgbe_hw *hw = &adapter->hw;
974 int i;
976 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
977 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
978 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
980 IXGBE_WRITE_FLUSH(hw);
982 for (i = 0; i < adapter->num_msix_vectors; i++)
983 synchronize_irq(adapter->msix_entries[i].vector);
987 * ixgbevf_irq_enable - Enable default interrupt generation settings
988 * @adapter: board private structure
990 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
992 struct ixgbe_hw *hw = &adapter->hw;
994 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
995 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
996 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1000 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1001 * @adapter: board private structure
1003 * Configure the Tx unit of the MAC after a reset.
1005 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1007 u64 tdba;
1008 struct ixgbe_hw *hw = &adapter->hw;
1009 u32 i, j, tdlen, txctrl;
1011 /* Setup the HW Tx Head and Tail descriptor pointers */
1012 for (i = 0; i < adapter->num_tx_queues; i++) {
1013 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1014 j = ring->reg_idx;
1015 tdba = ring->dma;
1016 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1017 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1018 (tdba & DMA_BIT_MASK(32)));
1019 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1020 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1021 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1022 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1023 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1024 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1025 /* Disable Tx Head Writeback RO bit, since this hoses
1026 * bookkeeping if things aren't delivered in order.
1028 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1029 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1030 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1034 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1036 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1038 struct ixgbevf_ring *rx_ring;
1039 struct ixgbe_hw *hw = &adapter->hw;
1040 u32 srrctl;
1042 rx_ring = &adapter->rx_ring[index];
1044 srrctl = IXGBE_SRRCTL_DROP_EN;
1046 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1048 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1049 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1051 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1054 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1056 struct ixgbe_hw *hw = &adapter->hw;
1057 struct net_device *netdev = adapter->netdev;
1058 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1059 int i;
1060 u16 rx_buf_len;
1062 /* notify the PF of our intent to use this size of frame */
1063 ixgbevf_rlpml_set_vf(hw, max_frame);
1065 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1066 max_frame += VLAN_HLEN;
1069 * Make best use of allocation by using all but 1K of a
1070 * power of 2 allocation that will be used for skb->head.
1072 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1073 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1074 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1075 else if (max_frame <= IXGBEVF_RXBUFFER_3K)
1076 rx_buf_len = IXGBEVF_RXBUFFER_3K;
1077 else if (max_frame <= IXGBEVF_RXBUFFER_7K)
1078 rx_buf_len = IXGBEVF_RXBUFFER_7K;
1079 else if (max_frame <= IXGBEVF_RXBUFFER_15K)
1080 rx_buf_len = IXGBEVF_RXBUFFER_15K;
1081 else
1082 rx_buf_len = IXGBEVF_MAX_RXBUFFER;
1084 for (i = 0; i < adapter->num_rx_queues; i++)
1085 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1089 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1090 * @adapter: board private structure
1092 * Configure the Rx unit of the MAC after a reset.
1094 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1096 u64 rdba;
1097 struct ixgbe_hw *hw = &adapter->hw;
1098 int i, j;
1099 u32 rdlen;
1101 /* PSRTYPE must be initialized in 82599 */
1102 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
1104 /* set_rx_buffer_len must be called before ring initialization */
1105 ixgbevf_set_rx_buffer_len(adapter);
1107 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1108 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1109 * the Base and Length of the Rx Descriptor Ring */
1110 for (i = 0; i < adapter->num_rx_queues; i++) {
1111 rdba = adapter->rx_ring[i].dma;
1112 j = adapter->rx_ring[i].reg_idx;
1113 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1114 (rdba & DMA_BIT_MASK(32)));
1115 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1116 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1117 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1118 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1119 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1120 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1122 ixgbevf_configure_srrctl(adapter, j);
1126 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1128 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1129 struct ixgbe_hw *hw = &adapter->hw;
1130 int err;
1132 if (!hw->mac.ops.set_vfta)
1133 return -EOPNOTSUPP;
1135 spin_lock(&adapter->mbx_lock);
1137 /* add VID to filter table */
1138 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
1140 spin_unlock(&adapter->mbx_lock);
1142 /* translate error return types so error makes sense */
1143 if (err == IXGBE_ERR_MBX)
1144 return -EIO;
1146 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1147 return -EACCES;
1149 set_bit(vid, adapter->active_vlans);
1151 return err;
1154 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1156 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1157 struct ixgbe_hw *hw = &adapter->hw;
1158 int err = -EOPNOTSUPP;
1160 spin_lock(&adapter->mbx_lock);
1162 /* remove VID from filter table */
1163 if (hw->mac.ops.set_vfta)
1164 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
1166 spin_unlock(&adapter->mbx_lock);
1168 clear_bit(vid, adapter->active_vlans);
1170 return err;
1173 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1175 u16 vid;
1177 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1178 ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
1181 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1183 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1184 struct ixgbe_hw *hw = &adapter->hw;
1185 int count = 0;
1187 if ((netdev_uc_count(netdev)) > 10) {
1188 pr_err("Too many unicast filters - No Space\n");
1189 return -ENOSPC;
1192 if (!netdev_uc_empty(netdev)) {
1193 struct netdev_hw_addr *ha;
1194 netdev_for_each_uc_addr(ha, netdev) {
1195 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1196 udelay(200);
1198 } else {
1200 * If the list is empty then send message to PF driver to
1201 * clear all macvlans on this VF.
1203 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1206 return count;
1210 * ixgbevf_set_rx_mode - Multicast set
1211 * @netdev: network interface device structure
1213 * The set_rx_method entry point is called whenever the multicast address
1214 * list or the network interface flags are updated. This routine is
1215 * responsible for configuring the hardware for proper multicast mode.
1217 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1219 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1220 struct ixgbe_hw *hw = &adapter->hw;
1222 spin_lock(&adapter->mbx_lock);
1224 /* reprogram multicast list */
1225 if (hw->mac.ops.update_mc_addr_list)
1226 hw->mac.ops.update_mc_addr_list(hw, netdev);
1228 ixgbevf_write_uc_addr_list(netdev);
1230 spin_unlock(&adapter->mbx_lock);
1233 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1235 int q_idx;
1236 struct ixgbevf_q_vector *q_vector;
1237 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1239 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1240 q_vector = adapter->q_vector[q_idx];
1241 napi_enable(&q_vector->napi);
1245 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1247 int q_idx;
1248 struct ixgbevf_q_vector *q_vector;
1249 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1251 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1252 q_vector = adapter->q_vector[q_idx];
1253 napi_disable(&q_vector->napi);
1257 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1259 struct net_device *netdev = adapter->netdev;
1260 int i;
1262 ixgbevf_set_rx_mode(netdev);
1264 ixgbevf_restore_vlan(adapter);
1266 ixgbevf_configure_tx(adapter);
1267 ixgbevf_configure_rx(adapter);
1268 for (i = 0; i < adapter->num_rx_queues; i++) {
1269 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1270 ixgbevf_alloc_rx_buffers(adapter, ring,
1271 IXGBE_DESC_UNUSED(ring));
1275 #define IXGBE_MAX_RX_DESC_POLL 10
1276 static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1277 int rxr)
1279 struct ixgbe_hw *hw = &adapter->hw;
1280 int j = adapter->rx_ring[rxr].reg_idx;
1281 int k;
1283 for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1284 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1285 break;
1286 else
1287 msleep(1);
1289 if (k >= IXGBE_MAX_RX_DESC_POLL) {
1290 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1291 "not set within the polling period\n", rxr);
1294 ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
1295 (adapter->rx_ring[rxr].count - 1));
1298 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1300 /* Only save pre-reset stats if there are some */
1301 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1302 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1303 adapter->stats.base_vfgprc;
1304 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1305 adapter->stats.base_vfgptc;
1306 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1307 adapter->stats.base_vfgorc;
1308 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1309 adapter->stats.base_vfgotc;
1310 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1311 adapter->stats.base_vfmprc;
1315 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1317 struct ixgbe_hw *hw = &adapter->hw;
1319 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1320 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1321 adapter->stats.last_vfgorc |=
1322 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1323 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1324 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1325 adapter->stats.last_vfgotc |=
1326 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1327 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1329 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1330 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1331 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1332 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1333 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1336 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1338 struct ixgbe_hw *hw = &adapter->hw;
1339 int api[] = { ixgbe_mbox_api_11,
1340 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]);
1348 if (!err)
1349 break;
1350 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;
1360 int i, j = 0;
1361 int num_rx_rings = adapter->num_rx_queues;
1362 u32 txdctl, rxdctl;
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);
1399 else
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 hw->mac.get_link_status = 1;
1415 mod_timer(&adapter->watchdog_timer, jiffies);
1418 static int ixgbevf_reset_queues(struct ixgbevf_adapter *adapter)
1420 struct ixgbe_hw *hw = &adapter->hw;
1421 struct ixgbevf_ring *rx_ring;
1422 unsigned int def_q = 0;
1423 unsigned int num_tcs = 0;
1424 unsigned int num_rx_queues = 1;
1425 int err, i;
1427 spin_lock(&adapter->mbx_lock);
1429 /* fetch queue configuration from the PF */
1430 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1432 spin_unlock(&adapter->mbx_lock);
1434 if (err)
1435 return err;
1437 if (num_tcs > 1) {
1438 /* update default Tx ring register index */
1439 adapter->tx_ring[0].reg_idx = def_q;
1441 /* we need as many queues as traffic classes */
1442 num_rx_queues = num_tcs;
1445 /* nothing to do if we have the correct number of queues */
1446 if (adapter->num_rx_queues == num_rx_queues)
1447 return 0;
1449 /* allocate new rings */
1450 rx_ring = kcalloc(num_rx_queues,
1451 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1452 if (!rx_ring)
1453 return -ENOMEM;
1455 /* setup ring fields */
1456 for (i = 0; i < num_rx_queues; i++) {
1457 rx_ring[i].count = adapter->rx_ring_count;
1458 rx_ring[i].queue_index = i;
1459 rx_ring[i].reg_idx = i;
1460 rx_ring[i].dev = &adapter->pdev->dev;
1461 rx_ring[i].netdev = adapter->netdev;
1463 /* allocate resources on the ring */
1464 err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
1465 if (err) {
1466 while (i) {
1467 i--;
1468 ixgbevf_free_rx_resources(adapter, &rx_ring[i]);
1470 kfree(rx_ring);
1471 return err;
1475 /* free the existing rings and queues */
1476 ixgbevf_free_all_rx_resources(adapter);
1477 adapter->num_rx_queues = 0;
1478 kfree(adapter->rx_ring);
1480 /* move new rings into position on the adapter struct */
1481 adapter->rx_ring = rx_ring;
1482 adapter->num_rx_queues = num_rx_queues;
1484 /* reset ring to vector mapping */
1485 ixgbevf_reset_q_vectors(adapter);
1486 ixgbevf_map_rings_to_vectors(adapter);
1488 return 0;
1491 void ixgbevf_up(struct ixgbevf_adapter *adapter)
1493 struct ixgbe_hw *hw = &adapter->hw;
1495 ixgbevf_negotiate_api(adapter);
1497 ixgbevf_reset_queues(adapter);
1499 ixgbevf_configure(adapter);
1501 ixgbevf_up_complete(adapter);
1503 /* clear any pending interrupts, may auto mask */
1504 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1506 ixgbevf_irq_enable(adapter);
1510 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1511 * @adapter: board private structure
1512 * @rx_ring: ring to free buffers from
1514 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1515 struct ixgbevf_ring *rx_ring)
1517 struct pci_dev *pdev = adapter->pdev;
1518 unsigned long size;
1519 unsigned int i;
1521 if (!rx_ring->rx_buffer_info)
1522 return;
1524 /* Free all the Rx ring sk_buffs */
1525 for (i = 0; i < rx_ring->count; i++) {
1526 struct ixgbevf_rx_buffer *rx_buffer_info;
1528 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1529 if (rx_buffer_info->dma) {
1530 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1531 rx_ring->rx_buf_len,
1532 DMA_FROM_DEVICE);
1533 rx_buffer_info->dma = 0;
1535 if (rx_buffer_info->skb) {
1536 struct sk_buff *skb = rx_buffer_info->skb;
1537 rx_buffer_info->skb = NULL;
1538 do {
1539 struct sk_buff *this = skb;
1540 skb = IXGBE_CB(skb)->prev;
1541 dev_kfree_skb(this);
1542 } while (skb);
1546 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1547 memset(rx_ring->rx_buffer_info, 0, size);
1549 /* Zero out the descriptor ring */
1550 memset(rx_ring->desc, 0, rx_ring->size);
1552 rx_ring->next_to_clean = 0;
1553 rx_ring->next_to_use = 0;
1555 if (rx_ring->head)
1556 writel(0, adapter->hw.hw_addr + rx_ring->head);
1557 if (rx_ring->tail)
1558 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1562 * ixgbevf_clean_tx_ring - Free Tx Buffers
1563 * @adapter: board private structure
1564 * @tx_ring: ring to be cleaned
1566 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1567 struct ixgbevf_ring *tx_ring)
1569 struct ixgbevf_tx_buffer *tx_buffer_info;
1570 unsigned long size;
1571 unsigned int i;
1573 if (!tx_ring->tx_buffer_info)
1574 return;
1576 /* Free all the Tx ring sk_buffs */
1578 for (i = 0; i < tx_ring->count; i++) {
1579 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1580 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
1583 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1584 memset(tx_ring->tx_buffer_info, 0, size);
1586 memset(tx_ring->desc, 0, tx_ring->size);
1588 tx_ring->next_to_use = 0;
1589 tx_ring->next_to_clean = 0;
1591 if (tx_ring->head)
1592 writel(0, adapter->hw.hw_addr + tx_ring->head);
1593 if (tx_ring->tail)
1594 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1598 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1599 * @adapter: board private structure
1601 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1603 int i;
1605 for (i = 0; i < adapter->num_rx_queues; i++)
1606 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1610 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1611 * @adapter: board private structure
1613 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1615 int i;
1617 for (i = 0; i < adapter->num_tx_queues; i++)
1618 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1621 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1623 struct net_device *netdev = adapter->netdev;
1624 struct ixgbe_hw *hw = &adapter->hw;
1625 u32 txdctl;
1626 int i, j;
1628 /* signal that we are down to the interrupt handler */
1629 set_bit(__IXGBEVF_DOWN, &adapter->state);
1630 /* disable receives */
1632 netif_tx_disable(netdev);
1634 msleep(10);
1636 netif_tx_stop_all_queues(netdev);
1638 ixgbevf_irq_disable(adapter);
1640 ixgbevf_napi_disable_all(adapter);
1642 del_timer_sync(&adapter->watchdog_timer);
1643 /* can't call flush scheduled work here because it can deadlock
1644 * if linkwatch_event tries to acquire the rtnl_lock which we are
1645 * holding */
1646 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1647 msleep(1);
1649 /* disable transmits in the hardware now that interrupts are off */
1650 for (i = 0; i < adapter->num_tx_queues; i++) {
1651 j = adapter->tx_ring[i].reg_idx;
1652 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1653 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1654 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1657 netif_carrier_off(netdev);
1659 if (!pci_channel_offline(adapter->pdev))
1660 ixgbevf_reset(adapter);
1662 ixgbevf_clean_all_tx_rings(adapter);
1663 ixgbevf_clean_all_rx_rings(adapter);
1666 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1668 WARN_ON(in_interrupt());
1670 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1671 msleep(1);
1674 * Check if PF is up before re-init. If not then skip until
1675 * later when the PF is up and ready to service requests from
1676 * the VF via mailbox. If the VF is up and running then the
1677 * watchdog task will continue to schedule reset tasks until
1678 * the PF is up and running.
1680 ixgbevf_down(adapter);
1681 ixgbevf_up(adapter);
1683 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1686 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1688 struct ixgbe_hw *hw = &adapter->hw;
1689 struct net_device *netdev = adapter->netdev;
1691 spin_lock(&adapter->mbx_lock);
1693 if (hw->mac.ops.reset_hw(hw))
1694 hw_dbg(hw, "PF still resetting\n");
1695 else
1696 hw->mac.ops.init_hw(hw);
1698 spin_unlock(&adapter->mbx_lock);
1700 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1701 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1702 netdev->addr_len);
1703 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1704 netdev->addr_len);
1708 static void ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1709 int vectors)
1711 int err, vector_threshold;
1713 /* We'll want at least 2 (vector_threshold):
1714 * 1) TxQ[0] + RxQ[0] handler
1715 * 2) Other (Link Status Change, etc.)
1717 vector_threshold = MIN_MSIX_COUNT;
1719 /* The more we get, the more we will assign to Tx/Rx Cleanup
1720 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1721 * Right now, we simply care about how many we'll get; we'll
1722 * set them up later while requesting irq's.
1724 while (vectors >= vector_threshold) {
1725 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1726 vectors);
1727 if (!err) /* Success in acquiring all requested vectors. */
1728 break;
1729 else if (err < 0)
1730 vectors = 0; /* Nasty failure, quit now */
1731 else /* err == number of vectors we should try again with */
1732 vectors = err;
1735 if (vectors < vector_threshold) {
1736 /* Can't allocate enough MSI-X interrupts? Oh well.
1737 * This just means we'll go with either a single MSI
1738 * vector or fall back to legacy interrupts.
1740 hw_dbg(&adapter->hw,
1741 "Unable to allocate MSI-X interrupts\n");
1742 kfree(adapter->msix_entries);
1743 adapter->msix_entries = NULL;
1744 } else {
1746 * Adjust for only the vectors we'll use, which is minimum
1747 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1748 * vectors we were allocated.
1750 adapter->num_msix_vectors = vectors;
1755 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
1756 * @adapter: board private structure to initialize
1758 * This is the top level queue allocation routine. The order here is very
1759 * important, starting with the "most" number of features turned on at once,
1760 * and ending with the smallest set of features. This way large combinations
1761 * can be allocated if they're turned on, and smaller combinations are the
1762 * fallthrough conditions.
1765 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1767 /* Start with base case */
1768 adapter->num_rx_queues = 1;
1769 adapter->num_tx_queues = 1;
1773 * ixgbevf_alloc_queues - Allocate memory for all rings
1774 * @adapter: board private structure to initialize
1776 * We allocate one ring per queue at run-time since we don't know the
1777 * number of queues at compile-time. The polling_netdev array is
1778 * intended for Multiqueue, but should work fine with a single queue.
1780 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1782 int i;
1784 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1785 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1786 if (!adapter->tx_ring)
1787 goto err_tx_ring_allocation;
1789 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1790 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1791 if (!adapter->rx_ring)
1792 goto err_rx_ring_allocation;
1794 for (i = 0; i < adapter->num_tx_queues; i++) {
1795 adapter->tx_ring[i].count = adapter->tx_ring_count;
1796 adapter->tx_ring[i].queue_index = i;
1797 /* reg_idx may be remapped later by DCB config */
1798 adapter->tx_ring[i].reg_idx = i;
1799 adapter->tx_ring[i].dev = &adapter->pdev->dev;
1800 adapter->tx_ring[i].netdev = adapter->netdev;
1803 for (i = 0; i < adapter->num_rx_queues; i++) {
1804 adapter->rx_ring[i].count = adapter->rx_ring_count;
1805 adapter->rx_ring[i].queue_index = i;
1806 adapter->rx_ring[i].reg_idx = i;
1807 adapter->rx_ring[i].dev = &adapter->pdev->dev;
1808 adapter->rx_ring[i].netdev = adapter->netdev;
1811 return 0;
1813 err_rx_ring_allocation:
1814 kfree(adapter->tx_ring);
1815 err_tx_ring_allocation:
1816 return -ENOMEM;
1820 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1821 * @adapter: board private structure to initialize
1823 * Attempt to configure the interrupts using the best available
1824 * capabilities of the hardware and the kernel.
1826 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1828 struct net_device *netdev = adapter->netdev;
1829 int err = 0;
1830 int vector, v_budget;
1833 * It's easy to be greedy for MSI-X vectors, but it really
1834 * doesn't do us much good if we have a lot more vectors
1835 * than CPU's. So let's be conservative and only ask for
1836 * (roughly) the same number of vectors as there are CPU's.
1837 * The default is to use pairs of vectors.
1839 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
1840 v_budget = min_t(int, v_budget, num_online_cpus());
1841 v_budget += NON_Q_VECTORS;
1843 /* A failure in MSI-X entry allocation isn't fatal, but it does
1844 * mean we disable MSI-X capabilities of the adapter. */
1845 adapter->msix_entries = kcalloc(v_budget,
1846 sizeof(struct msix_entry), GFP_KERNEL);
1847 if (!adapter->msix_entries) {
1848 err = -ENOMEM;
1849 goto out;
1852 for (vector = 0; vector < v_budget; vector++)
1853 adapter->msix_entries[vector].entry = vector;
1855 ixgbevf_acquire_msix_vectors(adapter, v_budget);
1857 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
1858 if (err)
1859 goto out;
1861 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
1863 out:
1864 return err;
1868 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1869 * @adapter: board private structure to initialize
1871 * We allocate one q_vector per queue interrupt. If allocation fails we
1872 * return -ENOMEM.
1874 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1876 int q_idx, num_q_vectors;
1877 struct ixgbevf_q_vector *q_vector;
1879 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1881 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1882 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1883 if (!q_vector)
1884 goto err_out;
1885 q_vector->adapter = adapter;
1886 q_vector->v_idx = q_idx;
1887 netif_napi_add(adapter->netdev, &q_vector->napi,
1888 ixgbevf_poll, 64);
1889 adapter->q_vector[q_idx] = q_vector;
1892 return 0;
1894 err_out:
1895 while (q_idx) {
1896 q_idx--;
1897 q_vector = adapter->q_vector[q_idx];
1898 netif_napi_del(&q_vector->napi);
1899 kfree(q_vector);
1900 adapter->q_vector[q_idx] = NULL;
1902 return -ENOMEM;
1906 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1907 * @adapter: board private structure to initialize
1909 * This function frees the memory allocated to the q_vectors. In addition if
1910 * NAPI is enabled it will delete any references to the NAPI struct prior
1911 * to freeing the q_vector.
1913 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1915 int q_idx, num_q_vectors;
1916 int napi_vectors;
1918 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1919 napi_vectors = adapter->num_rx_queues;
1921 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1922 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1924 adapter->q_vector[q_idx] = NULL;
1925 if (q_idx < napi_vectors)
1926 netif_napi_del(&q_vector->napi);
1927 kfree(q_vector);
1932 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1933 * @adapter: board private structure
1936 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1938 pci_disable_msix(adapter->pdev);
1939 kfree(adapter->msix_entries);
1940 adapter->msix_entries = NULL;
1944 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1945 * @adapter: board private structure to initialize
1948 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
1950 int err;
1952 /* Number of supported queues */
1953 ixgbevf_set_num_queues(adapter);
1955 err = ixgbevf_set_interrupt_capability(adapter);
1956 if (err) {
1957 hw_dbg(&adapter->hw,
1958 "Unable to setup interrupt capabilities\n");
1959 goto err_set_interrupt;
1962 err = ixgbevf_alloc_q_vectors(adapter);
1963 if (err) {
1964 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
1965 "vectors\n");
1966 goto err_alloc_q_vectors;
1969 err = ixgbevf_alloc_queues(adapter);
1970 if (err) {
1971 pr_err("Unable to allocate memory for queues\n");
1972 goto err_alloc_queues;
1975 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
1976 "Tx Queue count = %u\n",
1977 (adapter->num_rx_queues > 1) ? "Enabled" :
1978 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
1980 set_bit(__IXGBEVF_DOWN, &adapter->state);
1982 return 0;
1983 err_alloc_queues:
1984 ixgbevf_free_q_vectors(adapter);
1985 err_alloc_q_vectors:
1986 ixgbevf_reset_interrupt_capability(adapter);
1987 err_set_interrupt:
1988 return err;
1992 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
1993 * @adapter: board private structure to clear interrupt scheme on
1995 * We go through and clear interrupt specific resources and reset the structure
1996 * to pre-load conditions
1998 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2000 adapter->num_tx_queues = 0;
2001 adapter->num_rx_queues = 0;
2003 ixgbevf_free_q_vectors(adapter);
2004 ixgbevf_reset_interrupt_capability(adapter);
2008 * ixgbevf_sw_init - Initialize general software structures
2009 * (struct ixgbevf_adapter)
2010 * @adapter: board private structure to initialize
2012 * ixgbevf_sw_init initializes the Adapter private data structure.
2013 * Fields are initialized based on PCI device information and
2014 * OS network device settings (MTU size).
2016 static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2018 struct ixgbe_hw *hw = &adapter->hw;
2019 struct pci_dev *pdev = adapter->pdev;
2020 int err;
2022 /* PCI config space info */
2024 hw->vendor_id = pdev->vendor;
2025 hw->device_id = pdev->device;
2026 hw->revision_id = pdev->revision;
2027 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2028 hw->subsystem_device_id = pdev->subsystem_device;
2030 hw->mbx.ops.init_params(hw);
2032 /* assume legacy case in which PF would only give VF 2 queues */
2033 hw->mac.max_tx_queues = 2;
2034 hw->mac.max_rx_queues = 2;
2036 err = hw->mac.ops.reset_hw(hw);
2037 if (err) {
2038 dev_info(&pdev->dev,
2039 "PF still in reset state, assigning new address\n");
2040 eth_hw_addr_random(adapter->netdev);
2041 memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
2042 adapter->netdev->addr_len);
2043 } else {
2044 err = hw->mac.ops.init_hw(hw);
2045 if (err) {
2046 pr_err("init_shared_code failed: %d\n", err);
2047 goto out;
2049 memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
2050 adapter->netdev->addr_len);
2053 /* lock to protect mailbox accesses */
2054 spin_lock_init(&adapter->mbx_lock);
2056 /* Enable dynamic interrupt throttling rates */
2057 adapter->rx_itr_setting = 1;
2058 adapter->tx_itr_setting = 1;
2060 /* set default ring sizes */
2061 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2062 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2064 set_bit(__IXGBEVF_DOWN, &adapter->state);
2065 return 0;
2067 out:
2068 return err;
2071 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2073 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2074 if (current_counter < last_counter) \
2075 counter += 0x100000000LL; \
2076 last_counter = current_counter; \
2077 counter &= 0xFFFFFFFF00000000LL; \
2078 counter |= current_counter; \
2081 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2083 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2084 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2085 u64 current_counter = (current_counter_msb << 32) | \
2086 current_counter_lsb; \
2087 if (current_counter < last_counter) \
2088 counter += 0x1000000000LL; \
2089 last_counter = current_counter; \
2090 counter &= 0xFFFFFFF000000000LL; \
2091 counter |= current_counter; \
2094 * ixgbevf_update_stats - Update the board statistics counters.
2095 * @adapter: board private structure
2097 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2099 struct ixgbe_hw *hw = &adapter->hw;
2101 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2102 adapter->stats.vfgprc);
2103 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2104 adapter->stats.vfgptc);
2105 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2106 adapter->stats.last_vfgorc,
2107 adapter->stats.vfgorc);
2108 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2109 adapter->stats.last_vfgotc,
2110 adapter->stats.vfgotc);
2111 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2112 adapter->stats.vfmprc);
2116 * ixgbevf_watchdog - Timer Call-back
2117 * @data: pointer to adapter cast into an unsigned long
2119 static void ixgbevf_watchdog(unsigned long data)
2121 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2122 struct ixgbe_hw *hw = &adapter->hw;
2123 u32 eics = 0;
2124 int i;
2127 * Do the watchdog outside of interrupt context due to the lovely
2128 * delays that some of the newer hardware requires
2131 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2132 goto watchdog_short_circuit;
2134 /* get one bit for every active tx/rx interrupt vector */
2135 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2136 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2137 if (qv->rx.ring || qv->tx.ring)
2138 eics |= 1 << i;
2141 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
2143 watchdog_short_circuit:
2144 schedule_work(&adapter->watchdog_task);
2148 * ixgbevf_tx_timeout - Respond to a Tx Hang
2149 * @netdev: network interface device structure
2151 static void ixgbevf_tx_timeout(struct net_device *netdev)
2153 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2155 /* Do the reset outside of interrupt context */
2156 schedule_work(&adapter->reset_task);
2159 static void ixgbevf_reset_task(struct work_struct *work)
2161 struct ixgbevf_adapter *adapter;
2162 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2164 /* If we're already down or resetting, just bail */
2165 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2166 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2167 return;
2169 adapter->tx_timeout_count++;
2171 ixgbevf_reinit_locked(adapter);
2175 * ixgbevf_watchdog_task - worker thread to bring link up
2176 * @work: pointer to work_struct containing our data
2178 static void ixgbevf_watchdog_task(struct work_struct *work)
2180 struct ixgbevf_adapter *adapter = container_of(work,
2181 struct ixgbevf_adapter,
2182 watchdog_task);
2183 struct net_device *netdev = adapter->netdev;
2184 struct ixgbe_hw *hw = &adapter->hw;
2185 u32 link_speed = adapter->link_speed;
2186 bool link_up = adapter->link_up;
2188 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2191 * Always check the link on the watchdog because we have
2192 * no LSC interrupt
2194 if (hw->mac.ops.check_link) {
2195 s32 need_reset;
2197 spin_lock(&adapter->mbx_lock);
2199 need_reset = hw->mac.ops.check_link(hw, &link_speed,
2200 &link_up, false);
2202 spin_unlock(&adapter->mbx_lock);
2204 if (need_reset) {
2205 adapter->link_up = link_up;
2206 adapter->link_speed = link_speed;
2207 netif_carrier_off(netdev);
2208 netif_tx_stop_all_queues(netdev);
2209 schedule_work(&adapter->reset_task);
2210 goto pf_has_reset;
2212 } else {
2213 /* always assume link is up, if no check link
2214 * function */
2215 link_speed = IXGBE_LINK_SPEED_10GB_FULL;
2216 link_up = true;
2218 adapter->link_up = link_up;
2219 adapter->link_speed = link_speed;
2221 if (link_up) {
2222 if (!netif_carrier_ok(netdev)) {
2223 hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
2224 (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2225 10 : 1);
2226 netif_carrier_on(netdev);
2227 netif_tx_wake_all_queues(netdev);
2229 } else {
2230 adapter->link_up = false;
2231 adapter->link_speed = 0;
2232 if (netif_carrier_ok(netdev)) {
2233 hw_dbg(&adapter->hw, "NIC Link is Down\n");
2234 netif_carrier_off(netdev);
2235 netif_tx_stop_all_queues(netdev);
2239 ixgbevf_update_stats(adapter);
2241 pf_has_reset:
2242 /* Reset the timer */
2243 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2244 mod_timer(&adapter->watchdog_timer,
2245 round_jiffies(jiffies + (2 * HZ)));
2247 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2251 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2252 * @adapter: board private structure
2253 * @tx_ring: Tx descriptor ring for a specific queue
2255 * Free all transmit software resources
2257 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2258 struct ixgbevf_ring *tx_ring)
2260 struct pci_dev *pdev = adapter->pdev;
2262 ixgbevf_clean_tx_ring(adapter, tx_ring);
2264 vfree(tx_ring->tx_buffer_info);
2265 tx_ring->tx_buffer_info = NULL;
2267 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2268 tx_ring->dma);
2270 tx_ring->desc = NULL;
2274 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2275 * @adapter: board private structure
2277 * Free all transmit software resources
2279 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2281 int i;
2283 for (i = 0; i < adapter->num_tx_queues; i++)
2284 if (adapter->tx_ring[i].desc)
2285 ixgbevf_free_tx_resources(adapter,
2286 &adapter->tx_ring[i]);
2291 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2292 * @adapter: board private structure
2293 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2295 * Return 0 on success, negative on failure
2297 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2298 struct ixgbevf_ring *tx_ring)
2300 struct pci_dev *pdev = adapter->pdev;
2301 int size;
2303 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2304 tx_ring->tx_buffer_info = vzalloc(size);
2305 if (!tx_ring->tx_buffer_info)
2306 goto err;
2308 /* round up to nearest 4K */
2309 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2310 tx_ring->size = ALIGN(tx_ring->size, 4096);
2312 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2313 &tx_ring->dma, GFP_KERNEL);
2314 if (!tx_ring->desc)
2315 goto err;
2317 tx_ring->next_to_use = 0;
2318 tx_ring->next_to_clean = 0;
2319 return 0;
2321 err:
2322 vfree(tx_ring->tx_buffer_info);
2323 tx_ring->tx_buffer_info = NULL;
2324 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2325 "descriptor ring\n");
2326 return -ENOMEM;
2330 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2331 * @adapter: board private structure
2333 * If this function returns with an error, then it's possible one or
2334 * more of the rings is populated (while the rest are not). It is the
2335 * callers duty to clean those orphaned rings.
2337 * Return 0 on success, negative on failure
2339 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2341 int i, err = 0;
2343 for (i = 0; i < adapter->num_tx_queues; i++) {
2344 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2345 if (!err)
2346 continue;
2347 hw_dbg(&adapter->hw,
2348 "Allocation for Tx Queue %u failed\n", i);
2349 break;
2352 return err;
2356 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2357 * @adapter: board private structure
2358 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2360 * Returns 0 on success, negative on failure
2362 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2363 struct ixgbevf_ring *rx_ring)
2365 struct pci_dev *pdev = adapter->pdev;
2366 int size;
2368 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2369 rx_ring->rx_buffer_info = vzalloc(size);
2370 if (!rx_ring->rx_buffer_info)
2371 goto alloc_failed;
2373 /* Round up to nearest 4K */
2374 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2375 rx_ring->size = ALIGN(rx_ring->size, 4096);
2377 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2378 &rx_ring->dma, GFP_KERNEL);
2380 if (!rx_ring->desc) {
2381 hw_dbg(&adapter->hw,
2382 "Unable to allocate memory for "
2383 "the receive descriptor ring\n");
2384 vfree(rx_ring->rx_buffer_info);
2385 rx_ring->rx_buffer_info = NULL;
2386 goto alloc_failed;
2389 rx_ring->next_to_clean = 0;
2390 rx_ring->next_to_use = 0;
2392 return 0;
2393 alloc_failed:
2394 return -ENOMEM;
2398 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2399 * @adapter: board private structure
2401 * If this function returns with an error, then it's possible one or
2402 * more of the rings is populated (while the rest are not). It is the
2403 * callers duty to clean those orphaned rings.
2405 * Return 0 on success, negative on failure
2407 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2409 int i, err = 0;
2411 for (i = 0; i < adapter->num_rx_queues; i++) {
2412 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2413 if (!err)
2414 continue;
2415 hw_dbg(&adapter->hw,
2416 "Allocation for Rx Queue %u failed\n", i);
2417 break;
2419 return err;
2423 * ixgbevf_free_rx_resources - Free Rx Resources
2424 * @adapter: board private structure
2425 * @rx_ring: ring to clean the resources from
2427 * Free all receive software resources
2429 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2430 struct ixgbevf_ring *rx_ring)
2432 struct pci_dev *pdev = adapter->pdev;
2434 ixgbevf_clean_rx_ring(adapter, rx_ring);
2436 vfree(rx_ring->rx_buffer_info);
2437 rx_ring->rx_buffer_info = NULL;
2439 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2440 rx_ring->dma);
2442 rx_ring->desc = NULL;
2446 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2447 * @adapter: board private structure
2449 * Free all receive software resources
2451 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2453 int i;
2455 for (i = 0; i < adapter->num_rx_queues; i++)
2456 if (adapter->rx_ring[i].desc)
2457 ixgbevf_free_rx_resources(adapter,
2458 &adapter->rx_ring[i]);
2461 static int ixgbevf_setup_queues(struct ixgbevf_adapter *adapter)
2463 struct ixgbe_hw *hw = &adapter->hw;
2464 struct ixgbevf_ring *rx_ring;
2465 unsigned int def_q = 0;
2466 unsigned int num_tcs = 0;
2467 unsigned int num_rx_queues = 1;
2468 int err, i;
2470 spin_lock(&adapter->mbx_lock);
2472 /* fetch queue configuration from the PF */
2473 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2475 spin_unlock(&adapter->mbx_lock);
2477 if (err)
2478 return err;
2480 if (num_tcs > 1) {
2481 /* update default Tx ring register index */
2482 adapter->tx_ring[0].reg_idx = def_q;
2484 /* we need as many queues as traffic classes */
2485 num_rx_queues = num_tcs;
2488 /* nothing to do if we have the correct number of queues */
2489 if (adapter->num_rx_queues == num_rx_queues)
2490 return 0;
2492 /* allocate new rings */
2493 rx_ring = kcalloc(num_rx_queues,
2494 sizeof(struct ixgbevf_ring), GFP_KERNEL);
2495 if (!rx_ring)
2496 return -ENOMEM;
2498 /* setup ring fields */
2499 for (i = 0; i < num_rx_queues; i++) {
2500 rx_ring[i].count = adapter->rx_ring_count;
2501 rx_ring[i].queue_index = i;
2502 rx_ring[i].reg_idx = i;
2503 rx_ring[i].dev = &adapter->pdev->dev;
2504 rx_ring[i].netdev = adapter->netdev;
2507 /* free the existing ring and queues */
2508 adapter->num_rx_queues = 0;
2509 kfree(adapter->rx_ring);
2511 /* move new rings into position on the adapter struct */
2512 adapter->rx_ring = rx_ring;
2513 adapter->num_rx_queues = num_rx_queues;
2515 return 0;
2519 * ixgbevf_open - Called when a network interface is made active
2520 * @netdev: network interface device structure
2522 * Returns 0 on success, negative value on failure
2524 * The open entry point is called when a network interface is made
2525 * active by the system (IFF_UP). At this point all resources needed
2526 * for transmit and receive operations are allocated, the interrupt
2527 * handler is registered with the OS, the watchdog timer is started,
2528 * and the stack is notified that the interface is ready.
2530 static int ixgbevf_open(struct net_device *netdev)
2532 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2533 struct ixgbe_hw *hw = &adapter->hw;
2534 int err;
2536 /* disallow open during test */
2537 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2538 return -EBUSY;
2540 if (hw->adapter_stopped) {
2541 ixgbevf_reset(adapter);
2542 /* if adapter is still stopped then PF isn't up and
2543 * the vf can't start. */
2544 if (hw->adapter_stopped) {
2545 err = IXGBE_ERR_MBX;
2546 pr_err("Unable to start - perhaps the PF Driver isn't "
2547 "up yet\n");
2548 goto err_setup_reset;
2552 ixgbevf_negotiate_api(adapter);
2554 /* setup queue reg_idx and Rx queue count */
2555 err = ixgbevf_setup_queues(adapter);
2556 if (err)
2557 goto err_setup_queues;
2559 /* allocate transmit descriptors */
2560 err = ixgbevf_setup_all_tx_resources(adapter);
2561 if (err)
2562 goto err_setup_tx;
2564 /* allocate receive descriptors */
2565 err = ixgbevf_setup_all_rx_resources(adapter);
2566 if (err)
2567 goto err_setup_rx;
2569 ixgbevf_configure(adapter);
2572 * Map the Tx/Rx rings to the vectors we were allotted.
2573 * if request_irq will be called in this function map_rings
2574 * must be called *before* up_complete
2576 ixgbevf_map_rings_to_vectors(adapter);
2578 ixgbevf_up_complete(adapter);
2580 /* clear any pending interrupts, may auto mask */
2581 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2582 err = ixgbevf_request_irq(adapter);
2583 if (err)
2584 goto err_req_irq;
2586 ixgbevf_irq_enable(adapter);
2588 return 0;
2590 err_req_irq:
2591 ixgbevf_down(adapter);
2592 ixgbevf_free_irq(adapter);
2593 err_setup_rx:
2594 ixgbevf_free_all_rx_resources(adapter);
2595 err_setup_tx:
2596 ixgbevf_free_all_tx_resources(adapter);
2597 err_setup_queues:
2598 ixgbevf_reset(adapter);
2600 err_setup_reset:
2602 return err;
2606 * ixgbevf_close - Disables a network interface
2607 * @netdev: network interface device structure
2609 * Returns 0, this is not allowed to fail
2611 * The close entry point is called when an interface is de-activated
2612 * by the OS. The hardware is still under the drivers control, but
2613 * needs to be disabled. A global MAC reset is issued to stop the
2614 * hardware, and all transmit and receive resources are freed.
2616 static int ixgbevf_close(struct net_device *netdev)
2618 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2620 ixgbevf_down(adapter);
2621 ixgbevf_free_irq(adapter);
2623 ixgbevf_free_all_tx_resources(adapter);
2624 ixgbevf_free_all_rx_resources(adapter);
2626 return 0;
2629 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2630 u32 vlan_macip_lens, u32 type_tucmd,
2631 u32 mss_l4len_idx)
2633 struct ixgbe_adv_tx_context_desc *context_desc;
2634 u16 i = tx_ring->next_to_use;
2636 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2638 i++;
2639 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2641 /* set bits to identify this as an advanced context descriptor */
2642 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2644 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2645 context_desc->seqnum_seed = 0;
2646 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2647 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2650 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
2651 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2653 u32 vlan_macip_lens, type_tucmd;
2654 u32 mss_l4len_idx, l4len;
2656 if (!skb_is_gso(skb))
2657 return 0;
2659 if (skb_header_cloned(skb)) {
2660 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2661 if (err)
2662 return err;
2665 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2666 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2668 if (skb->protocol == htons(ETH_P_IP)) {
2669 struct iphdr *iph = ip_hdr(skb);
2670 iph->tot_len = 0;
2671 iph->check = 0;
2672 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2673 iph->daddr, 0,
2674 IPPROTO_TCP,
2676 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2677 } else if (skb_is_gso_v6(skb)) {
2678 ipv6_hdr(skb)->payload_len = 0;
2679 tcp_hdr(skb)->check =
2680 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2681 &ipv6_hdr(skb)->daddr,
2682 0, IPPROTO_TCP, 0);
2685 /* compute header lengths */
2686 l4len = tcp_hdrlen(skb);
2687 *hdr_len += l4len;
2688 *hdr_len = skb_transport_offset(skb) + l4len;
2690 /* mss_l4len_id: use 1 as index for TSO */
2691 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2692 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2693 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2695 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2696 vlan_macip_lens = skb_network_header_len(skb);
2697 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2698 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2700 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2701 type_tucmd, mss_l4len_idx);
2703 return 1;
2706 static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2707 struct sk_buff *skb, u32 tx_flags)
2712 u32 vlan_macip_lens = 0;
2713 u32 mss_l4len_idx = 0;
2714 u32 type_tucmd = 0;
2716 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2717 u8 l4_hdr = 0;
2718 switch (skb->protocol) {
2719 case __constant_htons(ETH_P_IP):
2720 vlan_macip_lens |= skb_network_header_len(skb);
2721 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2722 l4_hdr = ip_hdr(skb)->protocol;
2723 break;
2724 case __constant_htons(ETH_P_IPV6):
2725 vlan_macip_lens |= skb_network_header_len(skb);
2726 l4_hdr = ipv6_hdr(skb)->nexthdr;
2727 break;
2728 default:
2729 if (unlikely(net_ratelimit())) {
2730 dev_warn(tx_ring->dev,
2731 "partial checksum but proto=%x!\n",
2732 skb->protocol);
2734 break;
2737 switch (l4_hdr) {
2738 case IPPROTO_TCP:
2739 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2740 mss_l4len_idx = tcp_hdrlen(skb) <<
2741 IXGBE_ADVTXD_L4LEN_SHIFT;
2742 break;
2743 case IPPROTO_SCTP:
2744 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2745 mss_l4len_idx = sizeof(struct sctphdr) <<
2746 IXGBE_ADVTXD_L4LEN_SHIFT;
2747 break;
2748 case IPPROTO_UDP:
2749 mss_l4len_idx = sizeof(struct udphdr) <<
2750 IXGBE_ADVTXD_L4LEN_SHIFT;
2751 break;
2752 default:
2753 if (unlikely(net_ratelimit())) {
2754 dev_warn(tx_ring->dev,
2755 "partial checksum but l4 proto=%x!\n",
2756 l4_hdr);
2758 break;
2762 /* vlan_macip_lens: MACLEN, VLAN tag */
2763 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2764 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2766 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2767 type_tucmd, mss_l4len_idx);
2769 return (skb->ip_summed == CHECKSUM_PARTIAL);
2772 static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
2773 struct sk_buff *skb, u32 tx_flags,
2774 unsigned int first)
2776 struct ixgbevf_tx_buffer *tx_buffer_info;
2777 unsigned int len;
2778 unsigned int total = skb->len;
2779 unsigned int offset = 0, size;
2780 int count = 0;
2781 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2782 unsigned int f;
2783 int i;
2785 i = tx_ring->next_to_use;
2787 len = min(skb_headlen(skb), total);
2788 while (len) {
2789 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2790 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2792 tx_buffer_info->length = size;
2793 tx_buffer_info->mapped_as_page = false;
2794 tx_buffer_info->dma = dma_map_single(tx_ring->dev,
2795 skb->data + offset,
2796 size, DMA_TO_DEVICE);
2797 if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
2798 goto dma_error;
2799 tx_buffer_info->next_to_watch = i;
2801 len -= size;
2802 total -= size;
2803 offset += size;
2804 count++;
2805 i++;
2806 if (i == tx_ring->count)
2807 i = 0;
2810 for (f = 0; f < nr_frags; f++) {
2811 const struct skb_frag_struct *frag;
2813 frag = &skb_shinfo(skb)->frags[f];
2814 len = min((unsigned int)skb_frag_size(frag), total);
2815 offset = 0;
2817 while (len) {
2818 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2819 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2821 tx_buffer_info->length = size;
2822 tx_buffer_info->dma =
2823 skb_frag_dma_map(tx_ring->dev, frag,
2824 offset, size, DMA_TO_DEVICE);
2825 tx_buffer_info->mapped_as_page = true;
2826 if (dma_mapping_error(tx_ring->dev,
2827 tx_buffer_info->dma))
2828 goto dma_error;
2829 tx_buffer_info->next_to_watch = i;
2831 len -= size;
2832 total -= size;
2833 offset += size;
2834 count++;
2835 i++;
2836 if (i == tx_ring->count)
2837 i = 0;
2839 if (total == 0)
2840 break;
2843 if (i == 0)
2844 i = tx_ring->count - 1;
2845 else
2846 i = i - 1;
2847 tx_ring->tx_buffer_info[i].skb = skb;
2848 tx_ring->tx_buffer_info[first].next_to_watch = i;
2849 tx_ring->tx_buffer_info[first].time_stamp = jiffies;
2851 return count;
2853 dma_error:
2854 dev_err(tx_ring->dev, "TX DMA map failed\n");
2856 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2857 tx_buffer_info->dma = 0;
2858 tx_buffer_info->next_to_watch = 0;
2859 count--;
2861 /* clear timestamp and dma mappings for remaining portion of packet */
2862 while (count >= 0) {
2863 count--;
2864 i--;
2865 if (i < 0)
2866 i += tx_ring->count;
2867 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2868 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
2871 return count;
2874 static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
2875 int count, u32 paylen, u8 hdr_len)
2877 union ixgbe_adv_tx_desc *tx_desc = NULL;
2878 struct ixgbevf_tx_buffer *tx_buffer_info;
2879 u32 olinfo_status = 0, cmd_type_len = 0;
2880 unsigned int i;
2882 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2884 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2886 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2888 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2889 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2891 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2892 olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
2894 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2895 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2897 /* use index 1 context for tso */
2898 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2899 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2900 olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
2905 * Check Context must be set if Tx switch is enabled, which it
2906 * always is for case where virtual functions are running
2908 olinfo_status |= IXGBE_ADVTXD_CC;
2910 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2912 i = tx_ring->next_to_use;
2913 while (count--) {
2914 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2915 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2916 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2917 tx_desc->read.cmd_type_len =
2918 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2919 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2920 i++;
2921 if (i == tx_ring->count)
2922 i = 0;
2925 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2927 tx_ring->next_to_use = i;
2930 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2932 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
2934 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
2935 /* Herbert's original patch had:
2936 * smp_mb__after_netif_stop_queue();
2937 * but since that doesn't exist yet, just open code it. */
2938 smp_mb();
2940 /* We need to check again in a case another CPU has just
2941 * made room available. */
2942 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
2943 return -EBUSY;
2945 /* A reprieve! - use start_queue because it doesn't call schedule */
2946 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2947 ++adapter->restart_queue;
2948 return 0;
2951 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2953 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
2954 return 0;
2955 return __ixgbevf_maybe_stop_tx(tx_ring, size);
2958 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2960 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2961 struct ixgbevf_ring *tx_ring;
2962 unsigned int first;
2963 unsigned int tx_flags = 0;
2964 u8 hdr_len = 0;
2965 int r_idx = 0, tso;
2966 u16 count = TXD_USE_COUNT(skb_headlen(skb));
2967 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2968 unsigned short f;
2969 #endif
2971 tx_ring = &adapter->tx_ring[r_idx];
2974 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
2975 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
2976 * + 2 desc gap to keep tail from touching head,
2977 * + 1 desc for context descriptor,
2978 * otherwise try next time
2980 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2981 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2982 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
2983 #else
2984 count += skb_shinfo(skb)->nr_frags;
2985 #endif
2986 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
2987 adapter->tx_busy++;
2988 return NETDEV_TX_BUSY;
2991 if (vlan_tx_tag_present(skb)) {
2992 tx_flags |= vlan_tx_tag_get(skb);
2993 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
2994 tx_flags |= IXGBE_TX_FLAGS_VLAN;
2997 first = tx_ring->next_to_use;
2999 if (skb->protocol == htons(ETH_P_IP))
3000 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3001 tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
3002 if (tso < 0) {
3003 dev_kfree_skb_any(skb);
3004 return NETDEV_TX_OK;
3007 if (tso)
3008 tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
3009 else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
3010 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3012 ixgbevf_tx_queue(tx_ring, tx_flags,
3013 ixgbevf_tx_map(tx_ring, skb, tx_flags, first),
3014 skb->len, hdr_len);
3016 * Force memory writes to complete before letting h/w
3017 * know there are new descriptors to fetch. (Only
3018 * applicable for weak-ordered memory model archs,
3019 * such as IA-64).
3021 wmb();
3023 writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
3025 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
3027 return NETDEV_TX_OK;
3031 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3032 * @netdev: network interface device structure
3033 * @p: pointer to an address structure
3035 * Returns 0 on success, negative on failure
3037 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3039 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3040 struct ixgbe_hw *hw = &adapter->hw;
3041 struct sockaddr *addr = p;
3043 if (!is_valid_ether_addr(addr->sa_data))
3044 return -EADDRNOTAVAIL;
3046 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3047 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3049 spin_lock(&adapter->mbx_lock);
3051 if (hw->mac.ops.set_rar)
3052 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
3054 spin_unlock(&adapter->mbx_lock);
3056 return 0;
3060 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3061 * @netdev: network interface device structure
3062 * @new_mtu: new value for maximum frame size
3064 * Returns 0 on success, negative on failure
3066 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3068 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3069 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3070 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
3072 switch (adapter->hw.api_version) {
3073 case ixgbe_mbox_api_11:
3074 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3075 break;
3076 default:
3077 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3078 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3079 break;
3082 /* MTU < 68 is an error and causes problems on some kernels */
3083 if ((new_mtu < 68) || (max_frame > max_possible_frame))
3084 return -EINVAL;
3086 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3087 netdev->mtu, new_mtu);
3088 /* must set new MTU before calling down or up */
3089 netdev->mtu = new_mtu;
3091 if (netif_running(netdev))
3092 ixgbevf_reinit_locked(adapter);
3094 return 0;
3097 static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
3099 struct net_device *netdev = pci_get_drvdata(pdev);
3100 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3101 #ifdef CONFIG_PM
3102 int retval = 0;
3103 #endif
3105 netif_device_detach(netdev);
3107 if (netif_running(netdev)) {
3108 rtnl_lock();
3109 ixgbevf_down(adapter);
3110 ixgbevf_free_irq(adapter);
3111 ixgbevf_free_all_tx_resources(adapter);
3112 ixgbevf_free_all_rx_resources(adapter);
3113 rtnl_unlock();
3116 ixgbevf_clear_interrupt_scheme(adapter);
3118 #ifdef CONFIG_PM
3119 retval = pci_save_state(pdev);
3120 if (retval)
3121 return retval;
3123 #endif
3124 pci_disable_device(pdev);
3126 return 0;
3129 #ifdef CONFIG_PM
3130 static int ixgbevf_resume(struct pci_dev *pdev)
3132 struct ixgbevf_adapter *adapter = pci_get_drvdata(pdev);
3133 struct net_device *netdev = adapter->netdev;
3134 u32 err;
3136 pci_set_power_state(pdev, PCI_D0);
3137 pci_restore_state(pdev);
3139 * pci_restore_state clears dev->state_saved so call
3140 * pci_save_state to restore it.
3142 pci_save_state(pdev);
3144 err = pci_enable_device_mem(pdev);
3145 if (err) {
3146 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3147 return err;
3149 pci_set_master(pdev);
3151 rtnl_lock();
3152 err = ixgbevf_init_interrupt_scheme(adapter);
3153 rtnl_unlock();
3154 if (err) {
3155 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3156 return err;
3159 ixgbevf_reset(adapter);
3161 if (netif_running(netdev)) {
3162 err = ixgbevf_open(netdev);
3163 if (err)
3164 return err;
3167 netif_device_attach(netdev);
3169 return err;
3172 #endif /* CONFIG_PM */
3173 static void ixgbevf_shutdown(struct pci_dev *pdev)
3175 ixgbevf_suspend(pdev, PMSG_SUSPEND);
3178 static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3179 struct rtnl_link_stats64 *stats)
3181 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3182 unsigned int start;
3183 u64 bytes, packets;
3184 const struct ixgbevf_ring *ring;
3185 int i;
3187 ixgbevf_update_stats(adapter);
3189 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3191 for (i = 0; i < adapter->num_rx_queues; i++) {
3192 ring = &adapter->rx_ring[i];
3193 do {
3194 start = u64_stats_fetch_begin_bh(&ring->syncp);
3195 bytes = ring->total_bytes;
3196 packets = ring->total_packets;
3197 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3198 stats->rx_bytes += bytes;
3199 stats->rx_packets += packets;
3202 for (i = 0; i < adapter->num_tx_queues; i++) {
3203 ring = &adapter->tx_ring[i];
3204 do {
3205 start = u64_stats_fetch_begin_bh(&ring->syncp);
3206 bytes = ring->total_bytes;
3207 packets = ring->total_packets;
3208 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3209 stats->tx_bytes += bytes;
3210 stats->tx_packets += packets;
3213 return stats;
3216 static const struct net_device_ops ixgbevf_netdev_ops = {
3217 .ndo_open = ixgbevf_open,
3218 .ndo_stop = ixgbevf_close,
3219 .ndo_start_xmit = ixgbevf_xmit_frame,
3220 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
3221 .ndo_get_stats64 = ixgbevf_get_stats,
3222 .ndo_validate_addr = eth_validate_addr,
3223 .ndo_set_mac_address = ixgbevf_set_mac,
3224 .ndo_change_mtu = ixgbevf_change_mtu,
3225 .ndo_tx_timeout = ixgbevf_tx_timeout,
3226 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3227 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
3230 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3232 dev->netdev_ops = &ixgbevf_netdev_ops;
3233 ixgbevf_set_ethtool_ops(dev);
3234 dev->watchdog_timeo = 5 * HZ;
3238 * ixgbevf_probe - Device Initialization Routine
3239 * @pdev: PCI device information struct
3240 * @ent: entry in ixgbevf_pci_tbl
3242 * Returns 0 on success, negative on failure
3244 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3245 * The OS initialization, configuring of the adapter private structure,
3246 * and a hardware reset occur.
3248 static int __devinit ixgbevf_probe(struct pci_dev *pdev,
3249 const struct pci_device_id *ent)
3251 struct net_device *netdev;
3252 struct ixgbevf_adapter *adapter = NULL;
3253 struct ixgbe_hw *hw = NULL;
3254 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3255 static int cards_found;
3256 int err, pci_using_dac;
3258 err = pci_enable_device(pdev);
3259 if (err)
3260 return err;
3262 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3263 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3264 pci_using_dac = 1;
3265 } else {
3266 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3267 if (err) {
3268 err = dma_set_coherent_mask(&pdev->dev,
3269 DMA_BIT_MASK(32));
3270 if (err) {
3271 dev_err(&pdev->dev, "No usable DMA "
3272 "configuration, aborting\n");
3273 goto err_dma;
3276 pci_using_dac = 0;
3279 err = pci_request_regions(pdev, ixgbevf_driver_name);
3280 if (err) {
3281 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3282 goto err_pci_reg;
3285 pci_set_master(pdev);
3287 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3288 MAX_TX_QUEUES);
3289 if (!netdev) {
3290 err = -ENOMEM;
3291 goto err_alloc_etherdev;
3294 SET_NETDEV_DEV(netdev, &pdev->dev);
3296 pci_set_drvdata(pdev, netdev);
3297 adapter = netdev_priv(netdev);
3299 adapter->netdev = netdev;
3300 adapter->pdev = pdev;
3301 hw = &adapter->hw;
3302 hw->back = adapter;
3303 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3306 * call save state here in standalone driver because it relies on
3307 * adapter struct to exist, and needs to call netdev_priv
3309 pci_save_state(pdev);
3311 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3312 pci_resource_len(pdev, 0));
3313 if (!hw->hw_addr) {
3314 err = -EIO;
3315 goto err_ioremap;
3318 ixgbevf_assign_netdev_ops(netdev);
3320 adapter->bd_number = cards_found;
3322 /* Setup hw api */
3323 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3324 hw->mac.type = ii->mac;
3326 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3327 sizeof(struct ixgbe_mbx_operations));
3329 /* setup the private structure */
3330 err = ixgbevf_sw_init(adapter);
3331 if (err)
3332 goto err_sw_init;
3334 /* The HW MAC address was set and/or determined in sw_init */
3335 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3337 if (!is_valid_ether_addr(netdev->dev_addr)) {
3338 pr_err("invalid MAC address\n");
3339 err = -EIO;
3340 goto err_sw_init;
3343 netdev->hw_features = NETIF_F_SG |
3344 NETIF_F_IP_CSUM |
3345 NETIF_F_IPV6_CSUM |
3346 NETIF_F_TSO |
3347 NETIF_F_TSO6 |
3348 NETIF_F_RXCSUM;
3350 netdev->features = netdev->hw_features |
3351 NETIF_F_HW_VLAN_TX |
3352 NETIF_F_HW_VLAN_RX |
3353 NETIF_F_HW_VLAN_FILTER;
3355 netdev->vlan_features |= NETIF_F_TSO;
3356 netdev->vlan_features |= NETIF_F_TSO6;
3357 netdev->vlan_features |= NETIF_F_IP_CSUM;
3358 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3359 netdev->vlan_features |= NETIF_F_SG;
3361 if (pci_using_dac)
3362 netdev->features |= NETIF_F_HIGHDMA;
3364 netdev->priv_flags |= IFF_UNICAST_FLT;
3366 init_timer(&adapter->watchdog_timer);
3367 adapter->watchdog_timer.function = ixgbevf_watchdog;
3368 adapter->watchdog_timer.data = (unsigned long)adapter;
3370 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3371 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3373 err = ixgbevf_init_interrupt_scheme(adapter);
3374 if (err)
3375 goto err_sw_init;
3377 /* pick up the PCI bus settings for reporting later */
3378 if (hw->mac.ops.get_bus_info)
3379 hw->mac.ops.get_bus_info(hw);
3381 strcpy(netdev->name, "eth%d");
3383 err = register_netdev(netdev);
3384 if (err)
3385 goto err_register;
3387 netif_carrier_off(netdev);
3389 ixgbevf_init_last_counter_stats(adapter);
3391 /* print the MAC address */
3392 hw_dbg(hw, "%pM\n", netdev->dev_addr);
3394 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3396 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3397 cards_found++;
3398 return 0;
3400 err_register:
3401 ixgbevf_clear_interrupt_scheme(adapter);
3402 err_sw_init:
3403 ixgbevf_reset_interrupt_capability(adapter);
3404 iounmap(hw->hw_addr);
3405 err_ioremap:
3406 free_netdev(netdev);
3407 err_alloc_etherdev:
3408 pci_release_regions(pdev);
3409 err_pci_reg:
3410 err_dma:
3411 pci_disable_device(pdev);
3412 return err;
3416 * ixgbevf_remove - Device Removal Routine
3417 * @pdev: PCI device information struct
3419 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3420 * that it should release a PCI device. The could be caused by a
3421 * Hot-Plug event, or because the driver is going to be removed from
3422 * memory.
3424 static void __devexit ixgbevf_remove(struct pci_dev *pdev)
3426 struct net_device *netdev = pci_get_drvdata(pdev);
3427 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3429 set_bit(__IXGBEVF_DOWN, &adapter->state);
3431 del_timer_sync(&adapter->watchdog_timer);
3433 cancel_work_sync(&adapter->reset_task);
3434 cancel_work_sync(&adapter->watchdog_task);
3436 if (netdev->reg_state == NETREG_REGISTERED)
3437 unregister_netdev(netdev);
3439 ixgbevf_clear_interrupt_scheme(adapter);
3440 ixgbevf_reset_interrupt_capability(adapter);
3442 iounmap(adapter->hw.hw_addr);
3443 pci_release_regions(pdev);
3445 hw_dbg(&adapter->hw, "Remove complete\n");
3447 kfree(adapter->tx_ring);
3448 kfree(adapter->rx_ring);
3450 free_netdev(netdev);
3452 pci_disable_device(pdev);
3456 * ixgbevf_io_error_detected - called when PCI error is detected
3457 * @pdev: Pointer to PCI device
3458 * @state: The current pci connection state
3460 * This function is called after a PCI bus error affecting
3461 * this device has been detected.
3463 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3464 pci_channel_state_t state)
3466 struct net_device *netdev = pci_get_drvdata(pdev);
3467 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3469 netif_device_detach(netdev);
3471 if (state == pci_channel_io_perm_failure)
3472 return PCI_ERS_RESULT_DISCONNECT;
3474 if (netif_running(netdev))
3475 ixgbevf_down(adapter);
3477 pci_disable_device(pdev);
3479 /* Request a slot slot reset. */
3480 return PCI_ERS_RESULT_NEED_RESET;
3484 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3485 * @pdev: Pointer to PCI device
3487 * Restart the card from scratch, as if from a cold-boot. Implementation
3488 * resembles the first-half of the ixgbevf_resume routine.
3490 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3492 struct net_device *netdev = pci_get_drvdata(pdev);
3493 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3495 if (pci_enable_device_mem(pdev)) {
3496 dev_err(&pdev->dev,
3497 "Cannot re-enable PCI device after reset.\n");
3498 return PCI_ERS_RESULT_DISCONNECT;
3501 pci_set_master(pdev);
3503 ixgbevf_reset(adapter);
3505 return PCI_ERS_RESULT_RECOVERED;
3509 * ixgbevf_io_resume - called when traffic can start flowing again.
3510 * @pdev: Pointer to PCI device
3512 * This callback is called when the error recovery driver tells us that
3513 * its OK to resume normal operation. Implementation resembles the
3514 * second-half of the ixgbevf_resume routine.
3516 static void ixgbevf_io_resume(struct pci_dev *pdev)
3518 struct net_device *netdev = pci_get_drvdata(pdev);
3519 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3521 if (netif_running(netdev))
3522 ixgbevf_up(adapter);
3524 netif_device_attach(netdev);
3527 /* PCI Error Recovery (ERS) */
3528 static const struct pci_error_handlers ixgbevf_err_handler = {
3529 .error_detected = ixgbevf_io_error_detected,
3530 .slot_reset = ixgbevf_io_slot_reset,
3531 .resume = ixgbevf_io_resume,
3534 static struct pci_driver ixgbevf_driver = {
3535 .name = ixgbevf_driver_name,
3536 .id_table = ixgbevf_pci_tbl,
3537 .probe = ixgbevf_probe,
3538 .remove = __devexit_p(ixgbevf_remove),
3539 #ifdef CONFIG_PM
3540 /* Power Management Hooks */
3541 .suspend = ixgbevf_suspend,
3542 .resume = ixgbevf_resume,
3543 #endif
3544 .shutdown = ixgbevf_shutdown,
3545 .err_handler = &ixgbevf_err_handler
3549 * ixgbevf_init_module - Driver Registration Routine
3551 * ixgbevf_init_module is the first routine called when the driver is
3552 * loaded. All it does is register with the PCI subsystem.
3554 static int __init ixgbevf_init_module(void)
3556 int ret;
3557 pr_info("%s - version %s\n", ixgbevf_driver_string,
3558 ixgbevf_driver_version);
3560 pr_info("%s\n", ixgbevf_copyright);
3562 ret = pci_register_driver(&ixgbevf_driver);
3563 return ret;
3566 module_init(ixgbevf_init_module);
3569 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3571 * ixgbevf_exit_module is called just before the driver is removed
3572 * from memory.
3574 static void __exit ixgbevf_exit_module(void)
3576 pci_unregister_driver(&ixgbevf_driver);
3579 #ifdef DEBUG
3581 * ixgbevf_get_hw_dev_name - return device name string
3582 * used by hardware layer to print debugging information
3584 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3586 struct ixgbevf_adapter *adapter = hw->back;
3587 return adapter->netdev->name;
3590 #endif
3591 module_exit(ixgbevf_exit_module);
3593 /* ixgbevf_main.c */