ixgbevf: Reduce size of maximum rx buffer
[linux-2.6/libata-dev.git] / drivers / net / ethernet / intel / ixgbevf / ixgbevf_main.c
blob9d88153023633c5426b9b1841b269d7738f98c34
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.7.12-k"
62 const char ixgbevf_driver_version[] = DRV_VERSION;
63 static char ixgbevf_copyright[] =
64 "Copyright (c) 2009 - 2012 Intel Corporation.";
66 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
67 [board_82599_vf] = &ixgbevf_82599_vf_info,
68 [board_X540_vf] = &ixgbevf_X540_vf_info,
71 /* ixgbevf_pci_tbl - PCI Device ID Table
73 * Wildcard entries (PCI_ANY_ID) should come last
74 * Last entry must be all 0s
76 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
77 * Class, Class Mask, private data (not used) }
79 static struct pci_device_id ixgbevf_pci_tbl[] = {
80 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF),
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 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
292 napi_gro_receive(&q_vector->napi, skb);
293 else
294 netif_rx(skb);
298 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
299 * @adapter: address of board private structure
300 * @status_err: hardware indication of status of receive
301 * @skb: skb currently being received and modified
303 static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
304 struct ixgbevf_ring *ring,
305 u32 status_err, struct sk_buff *skb)
307 skb_checksum_none_assert(skb);
309 /* Rx csum disabled */
310 if (!(ring->netdev->features & NETIF_F_RXCSUM))
311 return;
313 /* if IP and error */
314 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
315 (status_err & IXGBE_RXDADV_ERR_IPE)) {
316 adapter->hw_csum_rx_error++;
317 return;
320 if (!(status_err & IXGBE_RXD_STAT_L4CS))
321 return;
323 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
324 adapter->hw_csum_rx_error++;
325 return;
328 /* It must be a TCP or UDP packet with a valid checksum */
329 skb->ip_summed = CHECKSUM_UNNECESSARY;
330 adapter->hw_csum_rx_good++;
334 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
335 * @adapter: address of board private structure
337 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
338 struct ixgbevf_ring *rx_ring,
339 int cleaned_count)
341 struct pci_dev *pdev = adapter->pdev;
342 union ixgbe_adv_rx_desc *rx_desc;
343 struct ixgbevf_rx_buffer *bi;
344 struct sk_buff *skb;
345 unsigned int i = rx_ring->next_to_use;
347 bi = &rx_ring->rx_buffer_info[i];
349 while (cleaned_count--) {
350 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
351 skb = bi->skb;
352 if (!skb) {
353 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
354 rx_ring->rx_buf_len);
355 if (!skb) {
356 adapter->alloc_rx_buff_failed++;
357 goto no_buffers;
359 bi->skb = skb;
361 if (!bi->dma) {
362 bi->dma = dma_map_single(&pdev->dev, skb->data,
363 rx_ring->rx_buf_len,
364 DMA_FROM_DEVICE);
365 if (dma_mapping_error(&pdev->dev, bi->dma)) {
366 dev_kfree_skb(skb);
367 bi->skb = NULL;
368 dev_err(&pdev->dev, "RX DMA map failed\n");
369 break;
372 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
374 i++;
375 if (i == rx_ring->count)
376 i = 0;
377 bi = &rx_ring->rx_buffer_info[i];
380 no_buffers:
381 if (rx_ring->next_to_use != i) {
382 rx_ring->next_to_use = i;
384 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
388 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
389 u32 qmask)
391 struct ixgbe_hw *hw = &adapter->hw;
393 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
396 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
397 struct ixgbevf_ring *rx_ring,
398 int budget)
400 struct ixgbevf_adapter *adapter = q_vector->adapter;
401 struct pci_dev *pdev = adapter->pdev;
402 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
403 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
404 struct sk_buff *skb;
405 unsigned int i;
406 u32 len, staterr;
407 int cleaned_count = 0;
408 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
410 i = rx_ring->next_to_clean;
411 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
412 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
413 rx_buffer_info = &rx_ring->rx_buffer_info[i];
415 while (staterr & IXGBE_RXD_STAT_DD) {
416 if (!budget)
417 break;
418 budget--;
420 rmb(); /* read descriptor and rx_buffer_info after status DD */
421 len = le16_to_cpu(rx_desc->wb.upper.length);
422 skb = rx_buffer_info->skb;
423 prefetch(skb->data - NET_IP_ALIGN);
424 rx_buffer_info->skb = NULL;
426 if (rx_buffer_info->dma) {
427 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
428 rx_ring->rx_buf_len,
429 DMA_FROM_DEVICE);
430 rx_buffer_info->dma = 0;
431 skb_put(skb, len);
434 i++;
435 if (i == rx_ring->count)
436 i = 0;
438 next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
439 prefetch(next_rxd);
440 cleaned_count++;
442 next_buffer = &rx_ring->rx_buffer_info[i];
444 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
445 skb->next = next_buffer->skb;
446 IXGBE_CB(skb->next)->prev = skb;
447 adapter->non_eop_descs++;
448 goto next_desc;
451 /* we should not be chaining buffers, if we did drop the skb */
452 if (IXGBE_CB(skb)->prev) {
453 do {
454 struct sk_buff *this = skb;
455 skb = IXGBE_CB(skb)->prev;
456 dev_kfree_skb(this);
457 } while (skb);
458 goto next_desc;
461 /* ERR_MASK will only have valid bits if EOP set */
462 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
463 dev_kfree_skb_irq(skb);
464 goto next_desc;
467 ixgbevf_rx_checksum(adapter, rx_ring, staterr, skb);
469 /* probably a little skewed due to removing CRC */
470 total_rx_bytes += skb->len;
471 total_rx_packets++;
474 * Work around issue of some types of VM to VM loop back
475 * packets not getting split correctly
477 if (staterr & IXGBE_RXD_STAT_LB) {
478 u32 header_fixup_len = skb_headlen(skb);
479 if (header_fixup_len < 14)
480 skb_push(skb, header_fixup_len);
482 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
484 /* Workaround hardware that can't do proper VEPA multicast
485 * source pruning.
487 if ((skb->pkt_type & (PACKET_BROADCAST | PACKET_MULTICAST)) &&
488 !(compare_ether_addr(adapter->netdev->dev_addr,
489 eth_hdr(skb)->h_source))) {
490 dev_kfree_skb_irq(skb);
491 goto next_desc;
494 ixgbevf_receive_skb(q_vector, skb, staterr, rx_desc);
496 next_desc:
497 rx_desc->wb.upper.status_error = 0;
499 /* return some buffers to hardware, one at a time is too slow */
500 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
501 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
502 cleaned_count);
503 cleaned_count = 0;
506 /* use prefetched values */
507 rx_desc = next_rxd;
508 rx_buffer_info = &rx_ring->rx_buffer_info[i];
510 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
513 rx_ring->next_to_clean = i;
514 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
516 if (cleaned_count)
517 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
519 u64_stats_update_begin(&rx_ring->syncp);
520 rx_ring->total_packets += total_rx_packets;
521 rx_ring->total_bytes += total_rx_bytes;
522 u64_stats_update_end(&rx_ring->syncp);
523 q_vector->rx.total_packets += total_rx_packets;
524 q_vector->rx.total_bytes += total_rx_bytes;
526 return !!budget;
530 * ixgbevf_poll - NAPI polling calback
531 * @napi: napi struct with our devices info in it
532 * @budget: amount of work driver is allowed to do this pass, in packets
534 * This function will clean more than one or more rings associated with a
535 * q_vector.
537 static int ixgbevf_poll(struct napi_struct *napi, int budget)
539 struct ixgbevf_q_vector *q_vector =
540 container_of(napi, struct ixgbevf_q_vector, napi);
541 struct ixgbevf_adapter *adapter = q_vector->adapter;
542 struct ixgbevf_ring *ring;
543 int per_ring_budget;
544 bool clean_complete = true;
546 ixgbevf_for_each_ring(ring, q_vector->tx)
547 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
549 /* attempt to distribute budget to each queue fairly, but don't allow
550 * the budget to go below 1 because we'll exit polling */
551 if (q_vector->rx.count > 1)
552 per_ring_budget = max(budget/q_vector->rx.count, 1);
553 else
554 per_ring_budget = budget;
556 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
557 ixgbevf_for_each_ring(ring, q_vector->rx)
558 clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
559 per_ring_budget);
560 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
562 /* If all work not completed, return budget and keep polling */
563 if (!clean_complete)
564 return budget;
565 /* all work done, exit the polling mode */
566 napi_complete(napi);
567 if (adapter->rx_itr_setting & 1)
568 ixgbevf_set_itr(q_vector);
569 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
570 ixgbevf_irq_enable_queues(adapter,
571 1 << q_vector->v_idx);
573 return 0;
577 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
578 * @q_vector: structure containing interrupt and ring information
580 static void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
582 struct ixgbevf_adapter *adapter = q_vector->adapter;
583 struct ixgbe_hw *hw = &adapter->hw;
584 int v_idx = q_vector->v_idx;
585 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
588 * set the WDIS bit to not clear the timer bits and cause an
589 * immediate assertion of the interrupt
591 itr_reg |= IXGBE_EITR_CNT_WDIS;
593 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
597 * ixgbevf_configure_msix - Configure MSI-X hardware
598 * @adapter: board private structure
600 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
601 * interrupts.
603 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
605 struct ixgbevf_q_vector *q_vector;
606 int q_vectors, v_idx;
608 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
609 adapter->eims_enable_mask = 0;
612 * Populate the IVAR table and set the ITR values to the
613 * corresponding register.
615 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
616 struct ixgbevf_ring *ring;
617 q_vector = adapter->q_vector[v_idx];
619 ixgbevf_for_each_ring(ring, q_vector->rx)
620 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
622 ixgbevf_for_each_ring(ring, q_vector->tx)
623 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
625 if (q_vector->tx.ring && !q_vector->rx.ring) {
626 /* tx only vector */
627 if (adapter->tx_itr_setting == 1)
628 q_vector->itr = IXGBE_10K_ITR;
629 else
630 q_vector->itr = adapter->tx_itr_setting;
631 } else {
632 /* rx or rx/tx vector */
633 if (adapter->rx_itr_setting == 1)
634 q_vector->itr = IXGBE_20K_ITR;
635 else
636 q_vector->itr = adapter->rx_itr_setting;
639 /* add q_vector eims value to global eims_enable_mask */
640 adapter->eims_enable_mask |= 1 << v_idx;
642 ixgbevf_write_eitr(q_vector);
645 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
646 /* setup eims_other and add value to global eims_enable_mask */
647 adapter->eims_other = 1 << v_idx;
648 adapter->eims_enable_mask |= adapter->eims_other;
651 enum latency_range {
652 lowest_latency = 0,
653 low_latency = 1,
654 bulk_latency = 2,
655 latency_invalid = 255
659 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
660 * @q_vector: structure containing interrupt and ring information
661 * @ring_container: structure containing ring performance data
663 * Stores a new ITR value based on packets and byte
664 * counts during the last interrupt. The advantage of per interrupt
665 * computation is faster updates and more accurate ITR for the current
666 * traffic pattern. Constants in this function were computed
667 * based on theoretical maximum wire speed and thresholds were set based
668 * on testing data as well as attempting to minimize response time
669 * while increasing bulk throughput.
671 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
672 struct ixgbevf_ring_container *ring_container)
674 int bytes = ring_container->total_bytes;
675 int packets = ring_container->total_packets;
676 u32 timepassed_us;
677 u64 bytes_perint;
678 u8 itr_setting = ring_container->itr;
680 if (packets == 0)
681 return;
683 /* simple throttlerate management
684 * 0-20MB/s lowest (100000 ints/s)
685 * 20-100MB/s low (20000 ints/s)
686 * 100-1249MB/s bulk (8000 ints/s)
688 /* what was last interrupt timeslice? */
689 timepassed_us = q_vector->itr >> 2;
690 bytes_perint = bytes / timepassed_us; /* bytes/usec */
692 switch (itr_setting) {
693 case lowest_latency:
694 if (bytes_perint > 10)
695 itr_setting = low_latency;
696 break;
697 case low_latency:
698 if (bytes_perint > 20)
699 itr_setting = bulk_latency;
700 else if (bytes_perint <= 10)
701 itr_setting = lowest_latency;
702 break;
703 case bulk_latency:
704 if (bytes_perint <= 20)
705 itr_setting = low_latency;
706 break;
709 /* clear work counters since we have the values we need */
710 ring_container->total_bytes = 0;
711 ring_container->total_packets = 0;
713 /* write updated itr to ring container */
714 ring_container->itr = itr_setting;
717 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
719 u32 new_itr = q_vector->itr;
720 u8 current_itr;
722 ixgbevf_update_itr(q_vector, &q_vector->tx);
723 ixgbevf_update_itr(q_vector, &q_vector->rx);
725 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
727 switch (current_itr) {
728 /* counts and packets in update_itr are dependent on these numbers */
729 case lowest_latency:
730 new_itr = IXGBE_100K_ITR;
731 break;
732 case low_latency:
733 new_itr = IXGBE_20K_ITR;
734 break;
735 case bulk_latency:
736 default:
737 new_itr = IXGBE_8K_ITR;
738 break;
741 if (new_itr != q_vector->itr) {
742 /* do an exponential smoothing */
743 new_itr = (10 * new_itr * q_vector->itr) /
744 ((9 * new_itr) + q_vector->itr);
746 /* save the algorithm value here */
747 q_vector->itr = new_itr;
749 ixgbevf_write_eitr(q_vector);
753 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
755 struct ixgbevf_adapter *adapter = data;
756 struct ixgbe_hw *hw = &adapter->hw;
758 hw->mac.get_link_status = 1;
760 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
761 mod_timer(&adapter->watchdog_timer, jiffies);
763 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
765 return IRQ_HANDLED;
770 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
771 * @irq: unused
772 * @data: pointer to our q_vector struct for this interrupt vector
774 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
776 struct ixgbevf_q_vector *q_vector = data;
778 /* EIAM disabled interrupts (on this vector) for us */
779 if (q_vector->rx.ring || q_vector->tx.ring)
780 napi_schedule(&q_vector->napi);
782 return IRQ_HANDLED;
785 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
786 int r_idx)
788 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
790 a->rx_ring[r_idx].next = q_vector->rx.ring;
791 q_vector->rx.ring = &a->rx_ring[r_idx];
792 q_vector->rx.count++;
795 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
796 int t_idx)
798 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
800 a->tx_ring[t_idx].next = q_vector->tx.ring;
801 q_vector->tx.ring = &a->tx_ring[t_idx];
802 q_vector->tx.count++;
806 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
807 * @adapter: board private structure to initialize
809 * This function maps descriptor rings to the queue-specific vectors
810 * we were allotted through the MSI-X enabling code. Ideally, we'd have
811 * one vector per ring/queue, but on a constrained vector budget, we
812 * group the rings as "efficiently" as possible. You would add new
813 * mapping configurations in here.
815 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
817 int q_vectors;
818 int v_start = 0;
819 int rxr_idx = 0, txr_idx = 0;
820 int rxr_remaining = adapter->num_rx_queues;
821 int txr_remaining = adapter->num_tx_queues;
822 int i, j;
823 int rqpv, tqpv;
824 int err = 0;
826 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
829 * The ideal configuration...
830 * We have enough vectors to map one per queue.
832 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
833 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
834 map_vector_to_rxq(adapter, v_start, rxr_idx);
836 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
837 map_vector_to_txq(adapter, v_start, txr_idx);
838 goto out;
842 * If we don't have enough vectors for a 1-to-1
843 * mapping, we'll have to group them so there are
844 * multiple queues per vector.
846 /* Re-adjusting *qpv takes care of the remainder. */
847 for (i = v_start; i < q_vectors; i++) {
848 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
849 for (j = 0; j < rqpv; j++) {
850 map_vector_to_rxq(adapter, i, rxr_idx);
851 rxr_idx++;
852 rxr_remaining--;
855 for (i = v_start; i < q_vectors; i++) {
856 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
857 for (j = 0; j < tqpv; j++) {
858 map_vector_to_txq(adapter, i, txr_idx);
859 txr_idx++;
860 txr_remaining--;
864 out:
865 return err;
869 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
870 * @adapter: board private structure
872 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
873 * interrupts from the kernel.
875 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
877 struct net_device *netdev = adapter->netdev;
878 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
879 int vector, err;
880 int ri = 0, ti = 0;
882 for (vector = 0; vector < q_vectors; vector++) {
883 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
884 struct msix_entry *entry = &adapter->msix_entries[vector];
886 if (q_vector->tx.ring && q_vector->rx.ring) {
887 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
888 "%s-%s-%d", netdev->name, "TxRx", ri++);
889 ti++;
890 } else if (q_vector->rx.ring) {
891 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
892 "%s-%s-%d", netdev->name, "rx", ri++);
893 } else if (q_vector->tx.ring) {
894 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
895 "%s-%s-%d", netdev->name, "tx", ti++);
896 } else {
897 /* skip this unused q_vector */
898 continue;
900 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
901 q_vector->name, q_vector);
902 if (err) {
903 hw_dbg(&adapter->hw,
904 "request_irq failed for MSIX interrupt "
905 "Error: %d\n", err);
906 goto free_queue_irqs;
910 err = request_irq(adapter->msix_entries[vector].vector,
911 &ixgbevf_msix_other, 0, netdev->name, adapter);
912 if (err) {
913 hw_dbg(&adapter->hw,
914 "request_irq for msix_other failed: %d\n", err);
915 goto free_queue_irqs;
918 return 0;
920 free_queue_irqs:
921 while (vector) {
922 vector--;
923 free_irq(adapter->msix_entries[vector].vector,
924 adapter->q_vector[vector]);
926 pci_disable_msix(adapter->pdev);
927 kfree(adapter->msix_entries);
928 adapter->msix_entries = NULL;
929 return err;
932 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
934 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
936 for (i = 0; i < q_vectors; i++) {
937 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
938 q_vector->rx.ring = NULL;
939 q_vector->tx.ring = NULL;
940 q_vector->rx.count = 0;
941 q_vector->tx.count = 0;
946 * ixgbevf_request_irq - initialize interrupts
947 * @adapter: board private structure
949 * Attempts to configure interrupts using the best available
950 * capabilities of the hardware and kernel.
952 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
954 int err = 0;
956 err = ixgbevf_request_msix_irqs(adapter);
958 if (err)
959 hw_dbg(&adapter->hw,
960 "request_irq failed, Error %d\n", err);
962 return err;
965 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
967 int i, q_vectors;
969 q_vectors = adapter->num_msix_vectors;
970 i = q_vectors - 1;
972 free_irq(adapter->msix_entries[i].vector, adapter);
973 i--;
975 for (; i >= 0; i--) {
976 /* free only the irqs that were actually requested */
977 if (!adapter->q_vector[i]->rx.ring &&
978 !adapter->q_vector[i]->tx.ring)
979 continue;
981 free_irq(adapter->msix_entries[i].vector,
982 adapter->q_vector[i]);
985 ixgbevf_reset_q_vectors(adapter);
989 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
990 * @adapter: board private structure
992 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
994 struct ixgbe_hw *hw = &adapter->hw;
995 int i;
997 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
998 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
999 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
1001 IXGBE_WRITE_FLUSH(hw);
1003 for (i = 0; i < adapter->num_msix_vectors; i++)
1004 synchronize_irq(adapter->msix_entries[i].vector);
1008 * ixgbevf_irq_enable - Enable default interrupt generation settings
1009 * @adapter: board private structure
1011 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1013 struct ixgbe_hw *hw = &adapter->hw;
1015 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1016 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1017 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1021 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1022 * @adapter: board private structure
1024 * Configure the Tx unit of the MAC after a reset.
1026 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1028 u64 tdba;
1029 struct ixgbe_hw *hw = &adapter->hw;
1030 u32 i, j, tdlen, txctrl;
1032 /* Setup the HW Tx Head and Tail descriptor pointers */
1033 for (i = 0; i < adapter->num_tx_queues; i++) {
1034 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1035 j = ring->reg_idx;
1036 tdba = ring->dma;
1037 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1038 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1039 (tdba & DMA_BIT_MASK(32)));
1040 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1041 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1042 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1043 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1044 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1045 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1046 /* Disable Tx Head Writeback RO bit, since this hoses
1047 * bookkeeping if things aren't delivered in order.
1049 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1050 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1051 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1055 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1057 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1059 struct ixgbevf_ring *rx_ring;
1060 struct ixgbe_hw *hw = &adapter->hw;
1061 u32 srrctl;
1063 rx_ring = &adapter->rx_ring[index];
1065 srrctl = IXGBE_SRRCTL_DROP_EN;
1067 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1069 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1070 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1072 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1075 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1077 struct ixgbe_hw *hw = &adapter->hw;
1078 struct net_device *netdev = adapter->netdev;
1079 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1080 int i;
1081 u16 rx_buf_len;
1083 /* notify the PF of our intent to use this size of frame */
1084 ixgbevf_rlpml_set_vf(hw, max_frame);
1086 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1087 max_frame += VLAN_HLEN;
1090 * Allocate buffer sizes that fit well into 32K and
1091 * take into account max frame size of 9.5K
1093 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1094 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1095 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1096 else if (max_frame <= IXGBEVF_RXBUFFER_2K)
1097 rx_buf_len = IXGBEVF_RXBUFFER_2K;
1098 else if (max_frame <= IXGBEVF_RXBUFFER_4K)
1099 rx_buf_len = IXGBEVF_RXBUFFER_4K;
1100 else if (max_frame <= IXGBEVF_RXBUFFER_8K)
1101 rx_buf_len = IXGBEVF_RXBUFFER_8K;
1102 else
1103 rx_buf_len = IXGBEVF_RXBUFFER_10K;
1105 for (i = 0; i < adapter->num_rx_queues; i++)
1106 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1110 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1111 * @adapter: board private structure
1113 * Configure the Rx unit of the MAC after a reset.
1115 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1117 u64 rdba;
1118 struct ixgbe_hw *hw = &adapter->hw;
1119 int i, j;
1120 u32 rdlen;
1122 /* PSRTYPE must be initialized in 82599 */
1123 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
1125 /* set_rx_buffer_len must be called before ring initialization */
1126 ixgbevf_set_rx_buffer_len(adapter);
1128 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1129 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1130 * the Base and Length of the Rx Descriptor Ring */
1131 for (i = 0; i < adapter->num_rx_queues; i++) {
1132 rdba = adapter->rx_ring[i].dma;
1133 j = adapter->rx_ring[i].reg_idx;
1134 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1135 (rdba & DMA_BIT_MASK(32)));
1136 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1137 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1138 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1139 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1140 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1141 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1143 ixgbevf_configure_srrctl(adapter, j);
1147 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1149 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1150 struct ixgbe_hw *hw = &adapter->hw;
1151 int err;
1153 if (!hw->mac.ops.set_vfta)
1154 return -EOPNOTSUPP;
1156 spin_lock_bh(&adapter->mbx_lock);
1158 /* add VID to filter table */
1159 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
1161 spin_unlock_bh(&adapter->mbx_lock);
1163 /* translate error return types so error makes sense */
1164 if (err == IXGBE_ERR_MBX)
1165 return -EIO;
1167 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1168 return -EACCES;
1170 set_bit(vid, adapter->active_vlans);
1172 return err;
1175 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1177 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1178 struct ixgbe_hw *hw = &adapter->hw;
1179 int err = -EOPNOTSUPP;
1181 spin_lock_bh(&adapter->mbx_lock);
1183 /* remove VID from filter table */
1184 if (hw->mac.ops.set_vfta)
1185 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
1187 spin_unlock_bh(&adapter->mbx_lock);
1189 clear_bit(vid, adapter->active_vlans);
1191 return err;
1194 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1196 u16 vid;
1198 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1199 ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
1202 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1204 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1205 struct ixgbe_hw *hw = &adapter->hw;
1206 int count = 0;
1208 if ((netdev_uc_count(netdev)) > 10) {
1209 pr_err("Too many unicast filters - No Space\n");
1210 return -ENOSPC;
1213 if (!netdev_uc_empty(netdev)) {
1214 struct netdev_hw_addr *ha;
1215 netdev_for_each_uc_addr(ha, netdev) {
1216 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1217 udelay(200);
1219 } else {
1221 * If the list is empty then send message to PF driver to
1222 * clear all macvlans on this VF.
1224 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1227 return count;
1231 * ixgbevf_set_rx_mode - Multicast set
1232 * @netdev: network interface device structure
1234 * The set_rx_method entry point is called whenever the multicast address
1235 * list or the network interface flags are updated. This routine is
1236 * responsible for configuring the hardware for proper multicast mode.
1238 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1240 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1241 struct ixgbe_hw *hw = &adapter->hw;
1243 spin_lock_bh(&adapter->mbx_lock);
1245 /* reprogram multicast list */
1246 if (hw->mac.ops.update_mc_addr_list)
1247 hw->mac.ops.update_mc_addr_list(hw, netdev);
1249 ixgbevf_write_uc_addr_list(netdev);
1251 spin_unlock_bh(&adapter->mbx_lock);
1254 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1256 int q_idx;
1257 struct ixgbevf_q_vector *q_vector;
1258 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1260 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1261 q_vector = adapter->q_vector[q_idx];
1262 napi_enable(&q_vector->napi);
1266 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1268 int q_idx;
1269 struct ixgbevf_q_vector *q_vector;
1270 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1272 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1273 q_vector = adapter->q_vector[q_idx];
1274 napi_disable(&q_vector->napi);
1278 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1280 struct net_device *netdev = adapter->netdev;
1281 int i;
1283 ixgbevf_set_rx_mode(netdev);
1285 ixgbevf_restore_vlan(adapter);
1287 ixgbevf_configure_tx(adapter);
1288 ixgbevf_configure_rx(adapter);
1289 for (i = 0; i < adapter->num_rx_queues; i++) {
1290 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1291 ixgbevf_alloc_rx_buffers(adapter, ring,
1292 IXGBE_DESC_UNUSED(ring));
1296 #define IXGBE_MAX_RX_DESC_POLL 10
1297 static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1298 int rxr)
1300 struct ixgbe_hw *hw = &adapter->hw;
1301 int j = adapter->rx_ring[rxr].reg_idx;
1302 int k;
1304 for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1305 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1306 break;
1307 else
1308 msleep(1);
1310 if (k >= IXGBE_MAX_RX_DESC_POLL) {
1311 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1312 "not set within the polling period\n", rxr);
1315 ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
1316 (adapter->rx_ring[rxr].count - 1));
1319 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1321 /* Only save pre-reset stats if there are some */
1322 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1323 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1324 adapter->stats.base_vfgprc;
1325 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1326 adapter->stats.base_vfgptc;
1327 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1328 adapter->stats.base_vfgorc;
1329 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1330 adapter->stats.base_vfgotc;
1331 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1332 adapter->stats.base_vfmprc;
1336 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1338 struct ixgbe_hw *hw = &adapter->hw;
1340 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1341 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1342 adapter->stats.last_vfgorc |=
1343 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1344 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1345 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1346 adapter->stats.last_vfgotc |=
1347 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1348 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1350 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1351 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1352 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1353 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1354 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1357 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1359 struct ixgbe_hw *hw = &adapter->hw;
1360 int api[] = { ixgbe_mbox_api_11,
1361 ixgbe_mbox_api_10,
1362 ixgbe_mbox_api_unknown };
1363 int err = 0, idx = 0;
1365 spin_lock_bh(&adapter->mbx_lock);
1367 while (api[idx] != ixgbe_mbox_api_unknown) {
1368 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1369 if (!err)
1370 break;
1371 idx++;
1374 spin_unlock_bh(&adapter->mbx_lock);
1377 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1379 struct net_device *netdev = adapter->netdev;
1380 struct ixgbe_hw *hw = &adapter->hw;
1381 int i, j = 0;
1382 int num_rx_rings = adapter->num_rx_queues;
1383 u32 txdctl, rxdctl;
1385 for (i = 0; i < adapter->num_tx_queues; i++) {
1386 j = adapter->tx_ring[i].reg_idx;
1387 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1388 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1389 txdctl |= (8 << 16);
1390 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1393 for (i = 0; i < adapter->num_tx_queues; i++) {
1394 j = adapter->tx_ring[i].reg_idx;
1395 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1396 txdctl |= IXGBE_TXDCTL_ENABLE;
1397 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1400 for (i = 0; i < num_rx_rings; i++) {
1401 j = adapter->rx_ring[i].reg_idx;
1402 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1403 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1404 if (hw->mac.type == ixgbe_mac_X540_vf) {
1405 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1406 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1407 IXGBE_RXDCTL_RLPML_EN);
1409 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1410 ixgbevf_rx_desc_queue_enable(adapter, i);
1413 ixgbevf_configure_msix(adapter);
1415 spin_lock_bh(&adapter->mbx_lock);
1417 if (hw->mac.ops.set_rar) {
1418 if (is_valid_ether_addr(hw->mac.addr))
1419 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1420 else
1421 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1424 spin_unlock_bh(&adapter->mbx_lock);
1426 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1427 ixgbevf_napi_enable_all(adapter);
1429 /* enable transmits */
1430 netif_tx_start_all_queues(netdev);
1432 ixgbevf_save_reset_stats(adapter);
1433 ixgbevf_init_last_counter_stats(adapter);
1435 hw->mac.get_link_status = 1;
1436 mod_timer(&adapter->watchdog_timer, jiffies);
1439 static int ixgbevf_reset_queues(struct ixgbevf_adapter *adapter)
1441 struct ixgbe_hw *hw = &adapter->hw;
1442 struct ixgbevf_ring *rx_ring;
1443 unsigned int def_q = 0;
1444 unsigned int num_tcs = 0;
1445 unsigned int num_rx_queues = 1;
1446 int err, i;
1448 spin_lock_bh(&adapter->mbx_lock);
1450 /* fetch queue configuration from the PF */
1451 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1453 spin_unlock_bh(&adapter->mbx_lock);
1455 if (err)
1456 return err;
1458 if (num_tcs > 1) {
1459 /* update default Tx ring register index */
1460 adapter->tx_ring[0].reg_idx = def_q;
1462 /* we need as many queues as traffic classes */
1463 num_rx_queues = num_tcs;
1466 /* nothing to do if we have the correct number of queues */
1467 if (adapter->num_rx_queues == num_rx_queues)
1468 return 0;
1470 /* allocate new rings */
1471 rx_ring = kcalloc(num_rx_queues,
1472 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1473 if (!rx_ring)
1474 return -ENOMEM;
1476 /* setup ring fields */
1477 for (i = 0; i < num_rx_queues; i++) {
1478 rx_ring[i].count = adapter->rx_ring_count;
1479 rx_ring[i].queue_index = i;
1480 rx_ring[i].reg_idx = i;
1481 rx_ring[i].dev = &adapter->pdev->dev;
1482 rx_ring[i].netdev = adapter->netdev;
1484 /* allocate resources on the ring */
1485 err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
1486 if (err) {
1487 while (i) {
1488 i--;
1489 ixgbevf_free_rx_resources(adapter, &rx_ring[i]);
1491 kfree(rx_ring);
1492 return err;
1496 /* free the existing rings and queues */
1497 ixgbevf_free_all_rx_resources(adapter);
1498 adapter->num_rx_queues = 0;
1499 kfree(adapter->rx_ring);
1501 /* move new rings into position on the adapter struct */
1502 adapter->rx_ring = rx_ring;
1503 adapter->num_rx_queues = num_rx_queues;
1505 /* reset ring to vector mapping */
1506 ixgbevf_reset_q_vectors(adapter);
1507 ixgbevf_map_rings_to_vectors(adapter);
1509 return 0;
1512 void ixgbevf_up(struct ixgbevf_adapter *adapter)
1514 struct ixgbe_hw *hw = &adapter->hw;
1516 ixgbevf_negotiate_api(adapter);
1518 ixgbevf_reset_queues(adapter);
1520 ixgbevf_configure(adapter);
1522 ixgbevf_up_complete(adapter);
1524 /* clear any pending interrupts, may auto mask */
1525 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1527 ixgbevf_irq_enable(adapter);
1531 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1532 * @adapter: board private structure
1533 * @rx_ring: ring to free buffers from
1535 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1536 struct ixgbevf_ring *rx_ring)
1538 struct pci_dev *pdev = adapter->pdev;
1539 unsigned long size;
1540 unsigned int i;
1542 if (!rx_ring->rx_buffer_info)
1543 return;
1545 /* Free all the Rx ring sk_buffs */
1546 for (i = 0; i < rx_ring->count; i++) {
1547 struct ixgbevf_rx_buffer *rx_buffer_info;
1549 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1550 if (rx_buffer_info->dma) {
1551 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1552 rx_ring->rx_buf_len,
1553 DMA_FROM_DEVICE);
1554 rx_buffer_info->dma = 0;
1556 if (rx_buffer_info->skb) {
1557 struct sk_buff *skb = rx_buffer_info->skb;
1558 rx_buffer_info->skb = NULL;
1559 do {
1560 struct sk_buff *this = skb;
1561 skb = IXGBE_CB(skb)->prev;
1562 dev_kfree_skb(this);
1563 } while (skb);
1567 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1568 memset(rx_ring->rx_buffer_info, 0, size);
1570 /* Zero out the descriptor ring */
1571 memset(rx_ring->desc, 0, rx_ring->size);
1573 rx_ring->next_to_clean = 0;
1574 rx_ring->next_to_use = 0;
1576 if (rx_ring->head)
1577 writel(0, adapter->hw.hw_addr + rx_ring->head);
1578 if (rx_ring->tail)
1579 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1583 * ixgbevf_clean_tx_ring - Free Tx Buffers
1584 * @adapter: board private structure
1585 * @tx_ring: ring to be cleaned
1587 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1588 struct ixgbevf_ring *tx_ring)
1590 struct ixgbevf_tx_buffer *tx_buffer_info;
1591 unsigned long size;
1592 unsigned int i;
1594 if (!tx_ring->tx_buffer_info)
1595 return;
1597 /* Free all the Tx ring sk_buffs */
1599 for (i = 0; i < tx_ring->count; i++) {
1600 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1601 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
1604 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1605 memset(tx_ring->tx_buffer_info, 0, size);
1607 memset(tx_ring->desc, 0, tx_ring->size);
1609 tx_ring->next_to_use = 0;
1610 tx_ring->next_to_clean = 0;
1612 if (tx_ring->head)
1613 writel(0, adapter->hw.hw_addr + tx_ring->head);
1614 if (tx_ring->tail)
1615 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1619 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1620 * @adapter: board private structure
1622 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1624 int i;
1626 for (i = 0; i < adapter->num_rx_queues; i++)
1627 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1631 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1632 * @adapter: board private structure
1634 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1636 int i;
1638 for (i = 0; i < adapter->num_tx_queues; i++)
1639 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1642 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1644 struct net_device *netdev = adapter->netdev;
1645 struct ixgbe_hw *hw = &adapter->hw;
1646 u32 txdctl;
1647 int i, j;
1649 /* signal that we are down to the interrupt handler */
1650 set_bit(__IXGBEVF_DOWN, &adapter->state);
1651 /* disable receives */
1653 netif_tx_disable(netdev);
1655 msleep(10);
1657 netif_tx_stop_all_queues(netdev);
1659 ixgbevf_irq_disable(adapter);
1661 ixgbevf_napi_disable_all(adapter);
1663 del_timer_sync(&adapter->watchdog_timer);
1664 /* can't call flush scheduled work here because it can deadlock
1665 * if linkwatch_event tries to acquire the rtnl_lock which we are
1666 * holding */
1667 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1668 msleep(1);
1670 /* disable transmits in the hardware now that interrupts are off */
1671 for (i = 0; i < adapter->num_tx_queues; i++) {
1672 j = adapter->tx_ring[i].reg_idx;
1673 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1674 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1675 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1678 netif_carrier_off(netdev);
1680 if (!pci_channel_offline(adapter->pdev))
1681 ixgbevf_reset(adapter);
1683 ixgbevf_clean_all_tx_rings(adapter);
1684 ixgbevf_clean_all_rx_rings(adapter);
1687 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1689 WARN_ON(in_interrupt());
1691 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1692 msleep(1);
1695 * Check if PF is up before re-init. If not then skip until
1696 * later when the PF is up and ready to service requests from
1697 * the VF via mailbox. If the VF is up and running then the
1698 * watchdog task will continue to schedule reset tasks until
1699 * the PF is up and running.
1701 ixgbevf_down(adapter);
1702 ixgbevf_up(adapter);
1704 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1707 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1709 struct ixgbe_hw *hw = &adapter->hw;
1710 struct net_device *netdev = adapter->netdev;
1712 spin_lock_bh(&adapter->mbx_lock);
1714 if (hw->mac.ops.reset_hw(hw))
1715 hw_dbg(hw, "PF still resetting\n");
1716 else
1717 hw->mac.ops.init_hw(hw);
1719 spin_unlock_bh(&adapter->mbx_lock);
1721 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1722 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1723 netdev->addr_len);
1724 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1725 netdev->addr_len);
1729 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1730 int vectors)
1732 int err = 0;
1733 int vector_threshold;
1735 /* We'll want at least 2 (vector_threshold):
1736 * 1) TxQ[0] + RxQ[0] handler
1737 * 2) Other (Link Status Change, etc.)
1739 vector_threshold = MIN_MSIX_COUNT;
1741 /* The more we get, the more we will assign to Tx/Rx Cleanup
1742 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1743 * Right now, we simply care about how many we'll get; we'll
1744 * set them up later while requesting irq's.
1746 while (vectors >= vector_threshold) {
1747 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1748 vectors);
1749 if (!err || err < 0) /* Success or a nasty failure. */
1750 break;
1751 else /* err == number of vectors we should try again with */
1752 vectors = err;
1755 if (vectors < vector_threshold)
1756 err = -ENOMEM;
1758 if (err) {
1759 dev_err(&adapter->pdev->dev,
1760 "Unable to allocate MSI-X interrupts\n");
1761 kfree(adapter->msix_entries);
1762 adapter->msix_entries = NULL;
1763 } else {
1765 * Adjust for only the vectors we'll use, which is minimum
1766 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1767 * vectors we were allocated.
1769 adapter->num_msix_vectors = vectors;
1771 return err;
1775 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
1776 * @adapter: board private structure to initialize
1778 * This is the top level queue allocation routine. The order here is very
1779 * important, starting with the "most" number of features turned on at once,
1780 * and ending with the smallest set of features. This way large combinations
1781 * can be allocated if they're turned on, and smaller combinations are the
1782 * fallthrough conditions.
1785 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1787 /* Start with base case */
1788 adapter->num_rx_queues = 1;
1789 adapter->num_tx_queues = 1;
1793 * ixgbevf_alloc_queues - Allocate memory for all rings
1794 * @adapter: board private structure to initialize
1796 * We allocate one ring per queue at run-time since we don't know the
1797 * number of queues at compile-time. The polling_netdev array is
1798 * intended for Multiqueue, but should work fine with a single queue.
1800 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1802 int i;
1804 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1805 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1806 if (!adapter->tx_ring)
1807 goto err_tx_ring_allocation;
1809 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1810 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1811 if (!adapter->rx_ring)
1812 goto err_rx_ring_allocation;
1814 for (i = 0; i < adapter->num_tx_queues; i++) {
1815 adapter->tx_ring[i].count = adapter->tx_ring_count;
1816 adapter->tx_ring[i].queue_index = i;
1817 /* reg_idx may be remapped later by DCB config */
1818 adapter->tx_ring[i].reg_idx = i;
1819 adapter->tx_ring[i].dev = &adapter->pdev->dev;
1820 adapter->tx_ring[i].netdev = adapter->netdev;
1823 for (i = 0; i < adapter->num_rx_queues; i++) {
1824 adapter->rx_ring[i].count = adapter->rx_ring_count;
1825 adapter->rx_ring[i].queue_index = i;
1826 adapter->rx_ring[i].reg_idx = i;
1827 adapter->rx_ring[i].dev = &adapter->pdev->dev;
1828 adapter->rx_ring[i].netdev = adapter->netdev;
1831 return 0;
1833 err_rx_ring_allocation:
1834 kfree(adapter->tx_ring);
1835 err_tx_ring_allocation:
1836 return -ENOMEM;
1840 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1841 * @adapter: board private structure to initialize
1843 * Attempt to configure the interrupts using the best available
1844 * capabilities of the hardware and the kernel.
1846 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1848 struct net_device *netdev = adapter->netdev;
1849 int err = 0;
1850 int vector, v_budget;
1853 * It's easy to be greedy for MSI-X vectors, but it really
1854 * doesn't do us much good if we have a lot more vectors
1855 * than CPU's. So let's be conservative and only ask for
1856 * (roughly) the same number of vectors as there are CPU's.
1857 * The default is to use pairs of vectors.
1859 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
1860 v_budget = min_t(int, v_budget, num_online_cpus());
1861 v_budget += NON_Q_VECTORS;
1863 /* A failure in MSI-X entry allocation isn't fatal, but it does
1864 * mean we disable MSI-X capabilities of the adapter. */
1865 adapter->msix_entries = kcalloc(v_budget,
1866 sizeof(struct msix_entry), GFP_KERNEL);
1867 if (!adapter->msix_entries) {
1868 err = -ENOMEM;
1869 goto out;
1872 for (vector = 0; vector < v_budget; vector++)
1873 adapter->msix_entries[vector].entry = vector;
1875 err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
1876 if (err)
1877 goto out;
1879 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
1880 if (err)
1881 goto out;
1883 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
1885 out:
1886 return err;
1890 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1891 * @adapter: board private structure to initialize
1893 * We allocate one q_vector per queue interrupt. If allocation fails we
1894 * return -ENOMEM.
1896 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1898 int q_idx, num_q_vectors;
1899 struct ixgbevf_q_vector *q_vector;
1901 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1903 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1904 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1905 if (!q_vector)
1906 goto err_out;
1907 q_vector->adapter = adapter;
1908 q_vector->v_idx = q_idx;
1909 netif_napi_add(adapter->netdev, &q_vector->napi,
1910 ixgbevf_poll, 64);
1911 adapter->q_vector[q_idx] = q_vector;
1914 return 0;
1916 err_out:
1917 while (q_idx) {
1918 q_idx--;
1919 q_vector = adapter->q_vector[q_idx];
1920 netif_napi_del(&q_vector->napi);
1921 kfree(q_vector);
1922 adapter->q_vector[q_idx] = NULL;
1924 return -ENOMEM;
1928 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1929 * @adapter: board private structure to initialize
1931 * This function frees the memory allocated to the q_vectors. In addition if
1932 * NAPI is enabled it will delete any references to the NAPI struct prior
1933 * to freeing the q_vector.
1935 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1937 int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1939 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1940 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1942 adapter->q_vector[q_idx] = NULL;
1943 netif_napi_del(&q_vector->napi);
1944 kfree(q_vector);
1949 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1950 * @adapter: board private structure
1953 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1955 pci_disable_msix(adapter->pdev);
1956 kfree(adapter->msix_entries);
1957 adapter->msix_entries = NULL;
1961 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1962 * @adapter: board private structure to initialize
1965 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
1967 int err;
1969 /* Number of supported queues */
1970 ixgbevf_set_num_queues(adapter);
1972 err = ixgbevf_set_interrupt_capability(adapter);
1973 if (err) {
1974 hw_dbg(&adapter->hw,
1975 "Unable to setup interrupt capabilities\n");
1976 goto err_set_interrupt;
1979 err = ixgbevf_alloc_q_vectors(adapter);
1980 if (err) {
1981 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
1982 "vectors\n");
1983 goto err_alloc_q_vectors;
1986 err = ixgbevf_alloc_queues(adapter);
1987 if (err) {
1988 pr_err("Unable to allocate memory for queues\n");
1989 goto err_alloc_queues;
1992 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
1993 "Tx Queue count = %u\n",
1994 (adapter->num_rx_queues > 1) ? "Enabled" :
1995 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
1997 set_bit(__IXGBEVF_DOWN, &adapter->state);
1999 return 0;
2000 err_alloc_queues:
2001 ixgbevf_free_q_vectors(adapter);
2002 err_alloc_q_vectors:
2003 ixgbevf_reset_interrupt_capability(adapter);
2004 err_set_interrupt:
2005 return err;
2009 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2010 * @adapter: board private structure to clear interrupt scheme on
2012 * We go through and clear interrupt specific resources and reset the structure
2013 * to pre-load conditions
2015 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2017 adapter->num_tx_queues = 0;
2018 adapter->num_rx_queues = 0;
2020 ixgbevf_free_q_vectors(adapter);
2021 ixgbevf_reset_interrupt_capability(adapter);
2025 * ixgbevf_sw_init - Initialize general software structures
2026 * (struct ixgbevf_adapter)
2027 * @adapter: board private structure to initialize
2029 * ixgbevf_sw_init initializes the Adapter private data structure.
2030 * Fields are initialized based on PCI device information and
2031 * OS network device settings (MTU size).
2033 static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2035 struct ixgbe_hw *hw = &adapter->hw;
2036 struct pci_dev *pdev = adapter->pdev;
2037 int err;
2039 /* PCI config space info */
2041 hw->vendor_id = pdev->vendor;
2042 hw->device_id = pdev->device;
2043 hw->revision_id = pdev->revision;
2044 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2045 hw->subsystem_device_id = pdev->subsystem_device;
2047 hw->mbx.ops.init_params(hw);
2049 /* assume legacy case in which PF would only give VF 2 queues */
2050 hw->mac.max_tx_queues = 2;
2051 hw->mac.max_rx_queues = 2;
2053 err = hw->mac.ops.reset_hw(hw);
2054 if (err) {
2055 dev_info(&pdev->dev,
2056 "PF still in reset state, assigning new address\n");
2057 eth_hw_addr_random(adapter->netdev);
2058 memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
2059 adapter->netdev->addr_len);
2060 } else {
2061 err = hw->mac.ops.init_hw(hw);
2062 if (err) {
2063 pr_err("init_shared_code failed: %d\n", err);
2064 goto out;
2066 memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
2067 adapter->netdev->addr_len);
2070 /* lock to protect mailbox accesses */
2071 spin_lock_init(&adapter->mbx_lock);
2073 /* Enable dynamic interrupt throttling rates */
2074 adapter->rx_itr_setting = 1;
2075 adapter->tx_itr_setting = 1;
2077 /* set default ring sizes */
2078 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2079 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2081 set_bit(__IXGBEVF_DOWN, &adapter->state);
2082 return 0;
2084 out:
2085 return err;
2088 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2090 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2091 if (current_counter < last_counter) \
2092 counter += 0x100000000LL; \
2093 last_counter = current_counter; \
2094 counter &= 0xFFFFFFFF00000000LL; \
2095 counter |= current_counter; \
2098 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2100 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2101 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2102 u64 current_counter = (current_counter_msb << 32) | \
2103 current_counter_lsb; \
2104 if (current_counter < last_counter) \
2105 counter += 0x1000000000LL; \
2106 last_counter = current_counter; \
2107 counter &= 0xFFFFFFF000000000LL; \
2108 counter |= current_counter; \
2111 * ixgbevf_update_stats - Update the board statistics counters.
2112 * @adapter: board private structure
2114 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2116 struct ixgbe_hw *hw = &adapter->hw;
2118 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2119 adapter->stats.vfgprc);
2120 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2121 adapter->stats.vfgptc);
2122 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2123 adapter->stats.last_vfgorc,
2124 adapter->stats.vfgorc);
2125 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2126 adapter->stats.last_vfgotc,
2127 adapter->stats.vfgotc);
2128 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2129 adapter->stats.vfmprc);
2133 * ixgbevf_watchdog - Timer Call-back
2134 * @data: pointer to adapter cast into an unsigned long
2136 static void ixgbevf_watchdog(unsigned long data)
2138 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2139 struct ixgbe_hw *hw = &adapter->hw;
2140 u32 eics = 0;
2141 int i;
2144 * Do the watchdog outside of interrupt context due to the lovely
2145 * delays that some of the newer hardware requires
2148 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2149 goto watchdog_short_circuit;
2151 /* get one bit for every active tx/rx interrupt vector */
2152 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2153 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2154 if (qv->rx.ring || qv->tx.ring)
2155 eics |= 1 << i;
2158 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
2160 watchdog_short_circuit:
2161 schedule_work(&adapter->watchdog_task);
2165 * ixgbevf_tx_timeout - Respond to a Tx Hang
2166 * @netdev: network interface device structure
2168 static void ixgbevf_tx_timeout(struct net_device *netdev)
2170 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2172 /* Do the reset outside of interrupt context */
2173 schedule_work(&adapter->reset_task);
2176 static void ixgbevf_reset_task(struct work_struct *work)
2178 struct ixgbevf_adapter *adapter;
2179 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2181 /* If we're already down or resetting, just bail */
2182 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2183 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2184 return;
2186 adapter->tx_timeout_count++;
2188 ixgbevf_reinit_locked(adapter);
2192 * ixgbevf_watchdog_task - worker thread to bring link up
2193 * @work: pointer to work_struct containing our data
2195 static void ixgbevf_watchdog_task(struct work_struct *work)
2197 struct ixgbevf_adapter *adapter = container_of(work,
2198 struct ixgbevf_adapter,
2199 watchdog_task);
2200 struct net_device *netdev = adapter->netdev;
2201 struct ixgbe_hw *hw = &adapter->hw;
2202 u32 link_speed = adapter->link_speed;
2203 bool link_up = adapter->link_up;
2205 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2208 * Always check the link on the watchdog because we have
2209 * no LSC interrupt
2211 if (hw->mac.ops.check_link) {
2212 s32 need_reset;
2214 spin_lock_bh(&adapter->mbx_lock);
2216 need_reset = hw->mac.ops.check_link(hw, &link_speed,
2217 &link_up, false);
2219 spin_unlock_bh(&adapter->mbx_lock);
2221 if (need_reset) {
2222 adapter->link_up = link_up;
2223 adapter->link_speed = link_speed;
2224 netif_carrier_off(netdev);
2225 netif_tx_stop_all_queues(netdev);
2226 schedule_work(&adapter->reset_task);
2227 goto pf_has_reset;
2229 } else {
2230 /* always assume link is up, if no check link
2231 * function */
2232 link_speed = IXGBE_LINK_SPEED_10GB_FULL;
2233 link_up = true;
2235 adapter->link_up = link_up;
2236 adapter->link_speed = link_speed;
2238 if (link_up) {
2239 if (!netif_carrier_ok(netdev)) {
2240 hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
2241 (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2242 10 : 1);
2243 netif_carrier_on(netdev);
2244 netif_tx_wake_all_queues(netdev);
2246 } else {
2247 adapter->link_up = false;
2248 adapter->link_speed = 0;
2249 if (netif_carrier_ok(netdev)) {
2250 hw_dbg(&adapter->hw, "NIC Link is Down\n");
2251 netif_carrier_off(netdev);
2252 netif_tx_stop_all_queues(netdev);
2256 ixgbevf_update_stats(adapter);
2258 pf_has_reset:
2259 /* Reset the timer */
2260 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2261 mod_timer(&adapter->watchdog_timer,
2262 round_jiffies(jiffies + (2 * HZ)));
2264 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2268 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2269 * @adapter: board private structure
2270 * @tx_ring: Tx descriptor ring for a specific queue
2272 * Free all transmit software resources
2274 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2275 struct ixgbevf_ring *tx_ring)
2277 struct pci_dev *pdev = adapter->pdev;
2279 ixgbevf_clean_tx_ring(adapter, tx_ring);
2281 vfree(tx_ring->tx_buffer_info);
2282 tx_ring->tx_buffer_info = NULL;
2284 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2285 tx_ring->dma);
2287 tx_ring->desc = NULL;
2291 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2292 * @adapter: board private structure
2294 * Free all transmit software resources
2296 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2298 int i;
2300 for (i = 0; i < adapter->num_tx_queues; i++)
2301 if (adapter->tx_ring[i].desc)
2302 ixgbevf_free_tx_resources(adapter,
2303 &adapter->tx_ring[i]);
2308 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2309 * @adapter: board private structure
2310 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2312 * Return 0 on success, negative on failure
2314 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2315 struct ixgbevf_ring *tx_ring)
2317 struct pci_dev *pdev = adapter->pdev;
2318 int size;
2320 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2321 tx_ring->tx_buffer_info = vzalloc(size);
2322 if (!tx_ring->tx_buffer_info)
2323 goto err;
2325 /* round up to nearest 4K */
2326 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2327 tx_ring->size = ALIGN(tx_ring->size, 4096);
2329 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2330 &tx_ring->dma, GFP_KERNEL);
2331 if (!tx_ring->desc)
2332 goto err;
2334 tx_ring->next_to_use = 0;
2335 tx_ring->next_to_clean = 0;
2336 return 0;
2338 err:
2339 vfree(tx_ring->tx_buffer_info);
2340 tx_ring->tx_buffer_info = NULL;
2341 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2342 "descriptor ring\n");
2343 return -ENOMEM;
2347 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2348 * @adapter: board private structure
2350 * If this function returns with an error, then it's possible one or
2351 * more of the rings is populated (while the rest are not). It is the
2352 * callers duty to clean those orphaned rings.
2354 * Return 0 on success, negative on failure
2356 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2358 int i, err = 0;
2360 for (i = 0; i < adapter->num_tx_queues; i++) {
2361 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2362 if (!err)
2363 continue;
2364 hw_dbg(&adapter->hw,
2365 "Allocation for Tx Queue %u failed\n", i);
2366 break;
2369 return err;
2373 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2374 * @adapter: board private structure
2375 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2377 * Returns 0 on success, negative on failure
2379 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2380 struct ixgbevf_ring *rx_ring)
2382 struct pci_dev *pdev = adapter->pdev;
2383 int size;
2385 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2386 rx_ring->rx_buffer_info = vzalloc(size);
2387 if (!rx_ring->rx_buffer_info)
2388 goto alloc_failed;
2390 /* Round up to nearest 4K */
2391 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2392 rx_ring->size = ALIGN(rx_ring->size, 4096);
2394 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2395 &rx_ring->dma, GFP_KERNEL);
2397 if (!rx_ring->desc) {
2398 hw_dbg(&adapter->hw,
2399 "Unable to allocate memory for "
2400 "the receive descriptor ring\n");
2401 vfree(rx_ring->rx_buffer_info);
2402 rx_ring->rx_buffer_info = NULL;
2403 goto alloc_failed;
2406 rx_ring->next_to_clean = 0;
2407 rx_ring->next_to_use = 0;
2409 return 0;
2410 alloc_failed:
2411 return -ENOMEM;
2415 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2416 * @adapter: board private structure
2418 * If this function returns with an error, then it's possible one or
2419 * more of the rings is populated (while the rest are not). It is the
2420 * callers duty to clean those orphaned rings.
2422 * Return 0 on success, negative on failure
2424 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2426 int i, err = 0;
2428 for (i = 0; i < adapter->num_rx_queues; i++) {
2429 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2430 if (!err)
2431 continue;
2432 hw_dbg(&adapter->hw,
2433 "Allocation for Rx Queue %u failed\n", i);
2434 break;
2436 return err;
2440 * ixgbevf_free_rx_resources - Free Rx Resources
2441 * @adapter: board private structure
2442 * @rx_ring: ring to clean the resources from
2444 * Free all receive software resources
2446 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2447 struct ixgbevf_ring *rx_ring)
2449 struct pci_dev *pdev = adapter->pdev;
2451 ixgbevf_clean_rx_ring(adapter, rx_ring);
2453 vfree(rx_ring->rx_buffer_info);
2454 rx_ring->rx_buffer_info = NULL;
2456 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2457 rx_ring->dma);
2459 rx_ring->desc = NULL;
2463 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2464 * @adapter: board private structure
2466 * Free all receive software resources
2468 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2470 int i;
2472 for (i = 0; i < adapter->num_rx_queues; i++)
2473 if (adapter->rx_ring[i].desc)
2474 ixgbevf_free_rx_resources(adapter,
2475 &adapter->rx_ring[i]);
2478 static int ixgbevf_setup_queues(struct ixgbevf_adapter *adapter)
2480 struct ixgbe_hw *hw = &adapter->hw;
2481 struct ixgbevf_ring *rx_ring;
2482 unsigned int def_q = 0;
2483 unsigned int num_tcs = 0;
2484 unsigned int num_rx_queues = 1;
2485 int err, i;
2487 spin_lock_bh(&adapter->mbx_lock);
2489 /* fetch queue configuration from the PF */
2490 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2492 spin_unlock_bh(&adapter->mbx_lock);
2494 if (err)
2495 return err;
2497 if (num_tcs > 1) {
2498 /* update default Tx ring register index */
2499 adapter->tx_ring[0].reg_idx = def_q;
2501 /* we need as many queues as traffic classes */
2502 num_rx_queues = num_tcs;
2505 /* nothing to do if we have the correct number of queues */
2506 if (adapter->num_rx_queues == num_rx_queues)
2507 return 0;
2509 /* allocate new rings */
2510 rx_ring = kcalloc(num_rx_queues,
2511 sizeof(struct ixgbevf_ring), GFP_KERNEL);
2512 if (!rx_ring)
2513 return -ENOMEM;
2515 /* setup ring fields */
2516 for (i = 0; i < num_rx_queues; i++) {
2517 rx_ring[i].count = adapter->rx_ring_count;
2518 rx_ring[i].queue_index = i;
2519 rx_ring[i].reg_idx = i;
2520 rx_ring[i].dev = &adapter->pdev->dev;
2521 rx_ring[i].netdev = adapter->netdev;
2524 /* free the existing ring and queues */
2525 adapter->num_rx_queues = 0;
2526 kfree(adapter->rx_ring);
2528 /* move new rings into position on the adapter struct */
2529 adapter->rx_ring = rx_ring;
2530 adapter->num_rx_queues = num_rx_queues;
2532 return 0;
2536 * ixgbevf_open - Called when a network interface is made active
2537 * @netdev: network interface device structure
2539 * Returns 0 on success, negative value on failure
2541 * The open entry point is called when a network interface is made
2542 * active by the system (IFF_UP). At this point all resources needed
2543 * for transmit and receive operations are allocated, the interrupt
2544 * handler is registered with the OS, the watchdog timer is started,
2545 * and the stack is notified that the interface is ready.
2547 static int ixgbevf_open(struct net_device *netdev)
2549 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2550 struct ixgbe_hw *hw = &adapter->hw;
2551 int err;
2553 /* disallow open during test */
2554 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2555 return -EBUSY;
2557 if (hw->adapter_stopped) {
2558 ixgbevf_reset(adapter);
2559 /* if adapter is still stopped then PF isn't up and
2560 * the vf can't start. */
2561 if (hw->adapter_stopped) {
2562 err = IXGBE_ERR_MBX;
2563 pr_err("Unable to start - perhaps the PF Driver isn't "
2564 "up yet\n");
2565 goto err_setup_reset;
2569 ixgbevf_negotiate_api(adapter);
2571 /* setup queue reg_idx and Rx queue count */
2572 err = ixgbevf_setup_queues(adapter);
2573 if (err)
2574 goto err_setup_queues;
2576 /* allocate transmit descriptors */
2577 err = ixgbevf_setup_all_tx_resources(adapter);
2578 if (err)
2579 goto err_setup_tx;
2581 /* allocate receive descriptors */
2582 err = ixgbevf_setup_all_rx_resources(adapter);
2583 if (err)
2584 goto err_setup_rx;
2586 ixgbevf_configure(adapter);
2589 * Map the Tx/Rx rings to the vectors we were allotted.
2590 * if request_irq will be called in this function map_rings
2591 * must be called *before* up_complete
2593 ixgbevf_map_rings_to_vectors(adapter);
2595 ixgbevf_up_complete(adapter);
2597 /* clear any pending interrupts, may auto mask */
2598 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2599 err = ixgbevf_request_irq(adapter);
2600 if (err)
2601 goto err_req_irq;
2603 ixgbevf_irq_enable(adapter);
2605 return 0;
2607 err_req_irq:
2608 ixgbevf_down(adapter);
2609 ixgbevf_free_irq(adapter);
2610 err_setup_rx:
2611 ixgbevf_free_all_rx_resources(adapter);
2612 err_setup_tx:
2613 ixgbevf_free_all_tx_resources(adapter);
2614 err_setup_queues:
2615 ixgbevf_reset(adapter);
2617 err_setup_reset:
2619 return err;
2623 * ixgbevf_close - Disables a network interface
2624 * @netdev: network interface device structure
2626 * Returns 0, this is not allowed to fail
2628 * The close entry point is called when an interface is de-activated
2629 * by the OS. The hardware is still under the drivers control, but
2630 * needs to be disabled. A global MAC reset is issued to stop the
2631 * hardware, and all transmit and receive resources are freed.
2633 static int ixgbevf_close(struct net_device *netdev)
2635 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2637 ixgbevf_down(adapter);
2638 ixgbevf_free_irq(adapter);
2640 ixgbevf_free_all_tx_resources(adapter);
2641 ixgbevf_free_all_rx_resources(adapter);
2643 return 0;
2646 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2647 u32 vlan_macip_lens, u32 type_tucmd,
2648 u32 mss_l4len_idx)
2650 struct ixgbe_adv_tx_context_desc *context_desc;
2651 u16 i = tx_ring->next_to_use;
2653 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2655 i++;
2656 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2658 /* set bits to identify this as an advanced context descriptor */
2659 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2661 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2662 context_desc->seqnum_seed = 0;
2663 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2664 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2667 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
2668 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2670 u32 vlan_macip_lens, type_tucmd;
2671 u32 mss_l4len_idx, l4len;
2673 if (!skb_is_gso(skb))
2674 return 0;
2676 if (skb_header_cloned(skb)) {
2677 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2678 if (err)
2679 return err;
2682 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2683 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2685 if (skb->protocol == htons(ETH_P_IP)) {
2686 struct iphdr *iph = ip_hdr(skb);
2687 iph->tot_len = 0;
2688 iph->check = 0;
2689 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2690 iph->daddr, 0,
2691 IPPROTO_TCP,
2693 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2694 } else if (skb_is_gso_v6(skb)) {
2695 ipv6_hdr(skb)->payload_len = 0;
2696 tcp_hdr(skb)->check =
2697 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2698 &ipv6_hdr(skb)->daddr,
2699 0, IPPROTO_TCP, 0);
2702 /* compute header lengths */
2703 l4len = tcp_hdrlen(skb);
2704 *hdr_len += l4len;
2705 *hdr_len = skb_transport_offset(skb) + l4len;
2707 /* mss_l4len_id: use 1 as index for TSO */
2708 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2709 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2710 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2712 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2713 vlan_macip_lens = skb_network_header_len(skb);
2714 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2715 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2717 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2718 type_tucmd, mss_l4len_idx);
2720 return 1;
2723 static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2724 struct sk_buff *skb, u32 tx_flags)
2729 u32 vlan_macip_lens = 0;
2730 u32 mss_l4len_idx = 0;
2731 u32 type_tucmd = 0;
2733 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2734 u8 l4_hdr = 0;
2735 switch (skb->protocol) {
2736 case __constant_htons(ETH_P_IP):
2737 vlan_macip_lens |= skb_network_header_len(skb);
2738 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2739 l4_hdr = ip_hdr(skb)->protocol;
2740 break;
2741 case __constant_htons(ETH_P_IPV6):
2742 vlan_macip_lens |= skb_network_header_len(skb);
2743 l4_hdr = ipv6_hdr(skb)->nexthdr;
2744 break;
2745 default:
2746 if (unlikely(net_ratelimit())) {
2747 dev_warn(tx_ring->dev,
2748 "partial checksum but proto=%x!\n",
2749 skb->protocol);
2751 break;
2754 switch (l4_hdr) {
2755 case IPPROTO_TCP:
2756 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2757 mss_l4len_idx = tcp_hdrlen(skb) <<
2758 IXGBE_ADVTXD_L4LEN_SHIFT;
2759 break;
2760 case IPPROTO_SCTP:
2761 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2762 mss_l4len_idx = sizeof(struct sctphdr) <<
2763 IXGBE_ADVTXD_L4LEN_SHIFT;
2764 break;
2765 case IPPROTO_UDP:
2766 mss_l4len_idx = sizeof(struct udphdr) <<
2767 IXGBE_ADVTXD_L4LEN_SHIFT;
2768 break;
2769 default:
2770 if (unlikely(net_ratelimit())) {
2771 dev_warn(tx_ring->dev,
2772 "partial checksum but l4 proto=%x!\n",
2773 l4_hdr);
2775 break;
2779 /* vlan_macip_lens: MACLEN, VLAN tag */
2780 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2781 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2783 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2784 type_tucmd, mss_l4len_idx);
2786 return (skb->ip_summed == CHECKSUM_PARTIAL);
2789 static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
2790 struct sk_buff *skb, u32 tx_flags,
2791 unsigned int first)
2793 struct ixgbevf_tx_buffer *tx_buffer_info;
2794 unsigned int len;
2795 unsigned int total = skb->len;
2796 unsigned int offset = 0, size;
2797 int count = 0;
2798 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2799 unsigned int f;
2800 int i;
2802 i = tx_ring->next_to_use;
2804 len = min(skb_headlen(skb), total);
2805 while (len) {
2806 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2807 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2809 tx_buffer_info->length = size;
2810 tx_buffer_info->mapped_as_page = false;
2811 tx_buffer_info->dma = dma_map_single(tx_ring->dev,
2812 skb->data + offset,
2813 size, DMA_TO_DEVICE);
2814 if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
2815 goto dma_error;
2816 tx_buffer_info->next_to_watch = i;
2818 len -= size;
2819 total -= size;
2820 offset += size;
2821 count++;
2822 i++;
2823 if (i == tx_ring->count)
2824 i = 0;
2827 for (f = 0; f < nr_frags; f++) {
2828 const struct skb_frag_struct *frag;
2830 frag = &skb_shinfo(skb)->frags[f];
2831 len = min((unsigned int)skb_frag_size(frag), total);
2832 offset = 0;
2834 while (len) {
2835 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2836 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2838 tx_buffer_info->length = size;
2839 tx_buffer_info->dma =
2840 skb_frag_dma_map(tx_ring->dev, frag,
2841 offset, size, DMA_TO_DEVICE);
2842 if (dma_mapping_error(tx_ring->dev,
2843 tx_buffer_info->dma))
2844 goto dma_error;
2845 tx_buffer_info->mapped_as_page = true;
2846 tx_buffer_info->next_to_watch = i;
2848 len -= size;
2849 total -= size;
2850 offset += size;
2851 count++;
2852 i++;
2853 if (i == tx_ring->count)
2854 i = 0;
2856 if (total == 0)
2857 break;
2860 if (i == 0)
2861 i = tx_ring->count - 1;
2862 else
2863 i = i - 1;
2864 tx_ring->tx_buffer_info[i].skb = skb;
2865 tx_ring->tx_buffer_info[first].next_to_watch = i;
2866 tx_ring->tx_buffer_info[first].time_stamp = jiffies;
2868 return count;
2870 dma_error:
2871 dev_err(tx_ring->dev, "TX DMA map failed\n");
2873 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2874 tx_buffer_info->dma = 0;
2875 tx_buffer_info->next_to_watch = 0;
2876 count--;
2878 /* clear timestamp and dma mappings for remaining portion of packet */
2879 while (count >= 0) {
2880 count--;
2881 i--;
2882 if (i < 0)
2883 i += tx_ring->count;
2884 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2885 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
2888 return count;
2891 static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
2892 int count, u32 paylen, u8 hdr_len)
2894 union ixgbe_adv_tx_desc *tx_desc = NULL;
2895 struct ixgbevf_tx_buffer *tx_buffer_info;
2896 u32 olinfo_status = 0, cmd_type_len = 0;
2897 unsigned int i;
2899 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2901 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2903 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2905 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2906 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2908 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2909 olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
2911 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2912 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2914 /* use index 1 context for tso */
2915 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2916 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2917 olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
2922 * Check Context must be set if Tx switch is enabled, which it
2923 * always is for case where virtual functions are running
2925 olinfo_status |= IXGBE_ADVTXD_CC;
2927 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2929 i = tx_ring->next_to_use;
2930 while (count--) {
2931 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2932 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2933 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2934 tx_desc->read.cmd_type_len =
2935 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2936 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2937 i++;
2938 if (i == tx_ring->count)
2939 i = 0;
2942 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2944 tx_ring->next_to_use = i;
2947 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2949 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
2951 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
2952 /* Herbert's original patch had:
2953 * smp_mb__after_netif_stop_queue();
2954 * but since that doesn't exist yet, just open code it. */
2955 smp_mb();
2957 /* We need to check again in a case another CPU has just
2958 * made room available. */
2959 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
2960 return -EBUSY;
2962 /* A reprieve! - use start_queue because it doesn't call schedule */
2963 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2964 ++adapter->restart_queue;
2965 return 0;
2968 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
2970 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
2971 return 0;
2972 return __ixgbevf_maybe_stop_tx(tx_ring, size);
2975 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2977 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2978 struct ixgbevf_ring *tx_ring;
2979 unsigned int first;
2980 unsigned int tx_flags = 0;
2981 u8 hdr_len = 0;
2982 int r_idx = 0, tso;
2983 u16 count = TXD_USE_COUNT(skb_headlen(skb));
2984 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2985 unsigned short f;
2986 #endif
2987 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
2988 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
2989 dev_kfree_skb(skb);
2990 return NETDEV_TX_OK;
2993 tx_ring = &adapter->tx_ring[r_idx];
2996 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
2997 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
2998 * + 2 desc gap to keep tail from touching head,
2999 * + 1 desc for context descriptor,
3000 * otherwise try next time
3002 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3003 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3004 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3005 #else
3006 count += skb_shinfo(skb)->nr_frags;
3007 #endif
3008 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
3009 adapter->tx_busy++;
3010 return NETDEV_TX_BUSY;
3013 if (vlan_tx_tag_present(skb)) {
3014 tx_flags |= vlan_tx_tag_get(skb);
3015 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3016 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3019 first = tx_ring->next_to_use;
3021 if (skb->protocol == htons(ETH_P_IP))
3022 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3023 tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
3024 if (tso < 0) {
3025 dev_kfree_skb_any(skb);
3026 return NETDEV_TX_OK;
3029 if (tso)
3030 tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
3031 else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
3032 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3034 ixgbevf_tx_queue(tx_ring, tx_flags,
3035 ixgbevf_tx_map(tx_ring, skb, tx_flags, first),
3036 skb->len, hdr_len);
3038 * Force memory writes to complete before letting h/w
3039 * know there are new descriptors to fetch. (Only
3040 * applicable for weak-ordered memory model archs,
3041 * such as IA-64).
3043 wmb();
3045 writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
3047 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
3049 return NETDEV_TX_OK;
3053 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3054 * @netdev: network interface device structure
3055 * @p: pointer to an address structure
3057 * Returns 0 on success, negative on failure
3059 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3061 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3062 struct ixgbe_hw *hw = &adapter->hw;
3063 struct sockaddr *addr = p;
3065 if (!is_valid_ether_addr(addr->sa_data))
3066 return -EADDRNOTAVAIL;
3068 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3069 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3071 spin_lock_bh(&adapter->mbx_lock);
3073 if (hw->mac.ops.set_rar)
3074 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
3076 spin_unlock_bh(&adapter->mbx_lock);
3078 return 0;
3082 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3083 * @netdev: network interface device structure
3084 * @new_mtu: new value for maximum frame size
3086 * Returns 0 on success, negative on failure
3088 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3090 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3091 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3092 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
3094 switch (adapter->hw.api_version) {
3095 case ixgbe_mbox_api_11:
3096 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3097 break;
3098 default:
3099 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3100 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3101 break;
3104 /* MTU < 68 is an error and causes problems on some kernels */
3105 if ((new_mtu < 68) || (max_frame > max_possible_frame))
3106 return -EINVAL;
3108 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3109 netdev->mtu, new_mtu);
3110 /* must set new MTU before calling down or up */
3111 netdev->mtu = new_mtu;
3113 if (netif_running(netdev))
3114 ixgbevf_reinit_locked(adapter);
3116 return 0;
3119 static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
3121 struct net_device *netdev = pci_get_drvdata(pdev);
3122 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3123 #ifdef CONFIG_PM
3124 int retval = 0;
3125 #endif
3127 netif_device_detach(netdev);
3129 if (netif_running(netdev)) {
3130 rtnl_lock();
3131 ixgbevf_down(adapter);
3132 ixgbevf_free_irq(adapter);
3133 ixgbevf_free_all_tx_resources(adapter);
3134 ixgbevf_free_all_rx_resources(adapter);
3135 rtnl_unlock();
3138 ixgbevf_clear_interrupt_scheme(adapter);
3140 #ifdef CONFIG_PM
3141 retval = pci_save_state(pdev);
3142 if (retval)
3143 return retval;
3145 #endif
3146 pci_disable_device(pdev);
3148 return 0;
3151 #ifdef CONFIG_PM
3152 static int ixgbevf_resume(struct pci_dev *pdev)
3154 struct ixgbevf_adapter *adapter = pci_get_drvdata(pdev);
3155 struct net_device *netdev = adapter->netdev;
3156 u32 err;
3158 pci_set_power_state(pdev, PCI_D0);
3159 pci_restore_state(pdev);
3161 * pci_restore_state clears dev->state_saved so call
3162 * pci_save_state to restore it.
3164 pci_save_state(pdev);
3166 err = pci_enable_device_mem(pdev);
3167 if (err) {
3168 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3169 return err;
3171 pci_set_master(pdev);
3173 rtnl_lock();
3174 err = ixgbevf_init_interrupt_scheme(adapter);
3175 rtnl_unlock();
3176 if (err) {
3177 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3178 return err;
3181 ixgbevf_reset(adapter);
3183 if (netif_running(netdev)) {
3184 err = ixgbevf_open(netdev);
3185 if (err)
3186 return err;
3189 netif_device_attach(netdev);
3191 return err;
3194 #endif /* CONFIG_PM */
3195 static void ixgbevf_shutdown(struct pci_dev *pdev)
3197 ixgbevf_suspend(pdev, PMSG_SUSPEND);
3200 static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3201 struct rtnl_link_stats64 *stats)
3203 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3204 unsigned int start;
3205 u64 bytes, packets;
3206 const struct ixgbevf_ring *ring;
3207 int i;
3209 ixgbevf_update_stats(adapter);
3211 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3213 for (i = 0; i < adapter->num_rx_queues; i++) {
3214 ring = &adapter->rx_ring[i];
3215 do {
3216 start = u64_stats_fetch_begin_bh(&ring->syncp);
3217 bytes = ring->total_bytes;
3218 packets = ring->total_packets;
3219 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3220 stats->rx_bytes += bytes;
3221 stats->rx_packets += packets;
3224 for (i = 0; i < adapter->num_tx_queues; i++) {
3225 ring = &adapter->tx_ring[i];
3226 do {
3227 start = u64_stats_fetch_begin_bh(&ring->syncp);
3228 bytes = ring->total_bytes;
3229 packets = ring->total_packets;
3230 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3231 stats->tx_bytes += bytes;
3232 stats->tx_packets += packets;
3235 return stats;
3238 static const struct net_device_ops ixgbevf_netdev_ops = {
3239 .ndo_open = ixgbevf_open,
3240 .ndo_stop = ixgbevf_close,
3241 .ndo_start_xmit = ixgbevf_xmit_frame,
3242 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
3243 .ndo_get_stats64 = ixgbevf_get_stats,
3244 .ndo_validate_addr = eth_validate_addr,
3245 .ndo_set_mac_address = ixgbevf_set_mac,
3246 .ndo_change_mtu = ixgbevf_change_mtu,
3247 .ndo_tx_timeout = ixgbevf_tx_timeout,
3248 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3249 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
3252 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3254 dev->netdev_ops = &ixgbevf_netdev_ops;
3255 ixgbevf_set_ethtool_ops(dev);
3256 dev->watchdog_timeo = 5 * HZ;
3260 * ixgbevf_probe - Device Initialization Routine
3261 * @pdev: PCI device information struct
3262 * @ent: entry in ixgbevf_pci_tbl
3264 * Returns 0 on success, negative on failure
3266 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3267 * The OS initialization, configuring of the adapter private structure,
3268 * and a hardware reset occur.
3270 static int __devinit ixgbevf_probe(struct pci_dev *pdev,
3271 const struct pci_device_id *ent)
3273 struct net_device *netdev;
3274 struct ixgbevf_adapter *adapter = NULL;
3275 struct ixgbe_hw *hw = NULL;
3276 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3277 static int cards_found;
3278 int err, pci_using_dac;
3280 err = pci_enable_device(pdev);
3281 if (err)
3282 return err;
3284 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3285 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3286 pci_using_dac = 1;
3287 } else {
3288 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3289 if (err) {
3290 err = dma_set_coherent_mask(&pdev->dev,
3291 DMA_BIT_MASK(32));
3292 if (err) {
3293 dev_err(&pdev->dev, "No usable DMA "
3294 "configuration, aborting\n");
3295 goto err_dma;
3298 pci_using_dac = 0;
3301 err = pci_request_regions(pdev, ixgbevf_driver_name);
3302 if (err) {
3303 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3304 goto err_pci_reg;
3307 pci_set_master(pdev);
3309 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3310 MAX_TX_QUEUES);
3311 if (!netdev) {
3312 err = -ENOMEM;
3313 goto err_alloc_etherdev;
3316 SET_NETDEV_DEV(netdev, &pdev->dev);
3318 pci_set_drvdata(pdev, netdev);
3319 adapter = netdev_priv(netdev);
3321 adapter->netdev = netdev;
3322 adapter->pdev = pdev;
3323 hw = &adapter->hw;
3324 hw->back = adapter;
3325 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3328 * call save state here in standalone driver because it relies on
3329 * adapter struct to exist, and needs to call netdev_priv
3331 pci_save_state(pdev);
3333 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3334 pci_resource_len(pdev, 0));
3335 if (!hw->hw_addr) {
3336 err = -EIO;
3337 goto err_ioremap;
3340 ixgbevf_assign_netdev_ops(netdev);
3342 adapter->bd_number = cards_found;
3344 /* Setup hw api */
3345 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3346 hw->mac.type = ii->mac;
3348 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3349 sizeof(struct ixgbe_mbx_operations));
3351 /* setup the private structure */
3352 err = ixgbevf_sw_init(adapter);
3353 if (err)
3354 goto err_sw_init;
3356 /* The HW MAC address was set and/or determined in sw_init */
3357 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3359 if (!is_valid_ether_addr(netdev->dev_addr)) {
3360 pr_err("invalid MAC address\n");
3361 err = -EIO;
3362 goto err_sw_init;
3365 netdev->hw_features = NETIF_F_SG |
3366 NETIF_F_IP_CSUM |
3367 NETIF_F_IPV6_CSUM |
3368 NETIF_F_TSO |
3369 NETIF_F_TSO6 |
3370 NETIF_F_RXCSUM;
3372 netdev->features = netdev->hw_features |
3373 NETIF_F_HW_VLAN_TX |
3374 NETIF_F_HW_VLAN_RX |
3375 NETIF_F_HW_VLAN_FILTER;
3377 netdev->vlan_features |= NETIF_F_TSO;
3378 netdev->vlan_features |= NETIF_F_TSO6;
3379 netdev->vlan_features |= NETIF_F_IP_CSUM;
3380 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3381 netdev->vlan_features |= NETIF_F_SG;
3383 if (pci_using_dac)
3384 netdev->features |= NETIF_F_HIGHDMA;
3386 netdev->priv_flags |= IFF_UNICAST_FLT;
3388 init_timer(&adapter->watchdog_timer);
3389 adapter->watchdog_timer.function = ixgbevf_watchdog;
3390 adapter->watchdog_timer.data = (unsigned long)adapter;
3392 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3393 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3395 err = ixgbevf_init_interrupt_scheme(adapter);
3396 if (err)
3397 goto err_sw_init;
3399 /* pick up the PCI bus settings for reporting later */
3400 if (hw->mac.ops.get_bus_info)
3401 hw->mac.ops.get_bus_info(hw);
3403 strcpy(netdev->name, "eth%d");
3405 err = register_netdev(netdev);
3406 if (err)
3407 goto err_register;
3409 netif_carrier_off(netdev);
3411 ixgbevf_init_last_counter_stats(adapter);
3413 /* print the MAC address */
3414 hw_dbg(hw, "%pM\n", netdev->dev_addr);
3416 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3418 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3419 cards_found++;
3420 return 0;
3422 err_register:
3423 ixgbevf_clear_interrupt_scheme(adapter);
3424 err_sw_init:
3425 ixgbevf_reset_interrupt_capability(adapter);
3426 iounmap(hw->hw_addr);
3427 err_ioremap:
3428 free_netdev(netdev);
3429 err_alloc_etherdev:
3430 pci_release_regions(pdev);
3431 err_pci_reg:
3432 err_dma:
3433 pci_disable_device(pdev);
3434 return err;
3438 * ixgbevf_remove - Device Removal Routine
3439 * @pdev: PCI device information struct
3441 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3442 * that it should release a PCI device. The could be caused by a
3443 * Hot-Plug event, or because the driver is going to be removed from
3444 * memory.
3446 static void __devexit ixgbevf_remove(struct pci_dev *pdev)
3448 struct net_device *netdev = pci_get_drvdata(pdev);
3449 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3451 set_bit(__IXGBEVF_DOWN, &adapter->state);
3453 del_timer_sync(&adapter->watchdog_timer);
3455 cancel_work_sync(&adapter->reset_task);
3456 cancel_work_sync(&adapter->watchdog_task);
3458 if (netdev->reg_state == NETREG_REGISTERED)
3459 unregister_netdev(netdev);
3461 ixgbevf_clear_interrupt_scheme(adapter);
3462 ixgbevf_reset_interrupt_capability(adapter);
3464 iounmap(adapter->hw.hw_addr);
3465 pci_release_regions(pdev);
3467 hw_dbg(&adapter->hw, "Remove complete\n");
3469 kfree(adapter->tx_ring);
3470 kfree(adapter->rx_ring);
3472 free_netdev(netdev);
3474 pci_disable_device(pdev);
3478 * ixgbevf_io_error_detected - called when PCI error is detected
3479 * @pdev: Pointer to PCI device
3480 * @state: The current pci connection state
3482 * This function is called after a PCI bus error affecting
3483 * this device has been detected.
3485 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3486 pci_channel_state_t state)
3488 struct net_device *netdev = pci_get_drvdata(pdev);
3489 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3491 netif_device_detach(netdev);
3493 if (state == pci_channel_io_perm_failure)
3494 return PCI_ERS_RESULT_DISCONNECT;
3496 if (netif_running(netdev))
3497 ixgbevf_down(adapter);
3499 pci_disable_device(pdev);
3501 /* Request a slot slot reset. */
3502 return PCI_ERS_RESULT_NEED_RESET;
3506 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3507 * @pdev: Pointer to PCI device
3509 * Restart the card from scratch, as if from a cold-boot. Implementation
3510 * resembles the first-half of the ixgbevf_resume routine.
3512 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3514 struct net_device *netdev = pci_get_drvdata(pdev);
3515 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3517 if (pci_enable_device_mem(pdev)) {
3518 dev_err(&pdev->dev,
3519 "Cannot re-enable PCI device after reset.\n");
3520 return PCI_ERS_RESULT_DISCONNECT;
3523 pci_set_master(pdev);
3525 ixgbevf_reset(adapter);
3527 return PCI_ERS_RESULT_RECOVERED;
3531 * ixgbevf_io_resume - called when traffic can start flowing again.
3532 * @pdev: Pointer to PCI device
3534 * This callback is called when the error recovery driver tells us that
3535 * its OK to resume normal operation. Implementation resembles the
3536 * second-half of the ixgbevf_resume routine.
3538 static void ixgbevf_io_resume(struct pci_dev *pdev)
3540 struct net_device *netdev = pci_get_drvdata(pdev);
3541 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3543 if (netif_running(netdev))
3544 ixgbevf_up(adapter);
3546 netif_device_attach(netdev);
3549 /* PCI Error Recovery (ERS) */
3550 static const struct pci_error_handlers ixgbevf_err_handler = {
3551 .error_detected = ixgbevf_io_error_detected,
3552 .slot_reset = ixgbevf_io_slot_reset,
3553 .resume = ixgbevf_io_resume,
3556 static struct pci_driver ixgbevf_driver = {
3557 .name = ixgbevf_driver_name,
3558 .id_table = ixgbevf_pci_tbl,
3559 .probe = ixgbevf_probe,
3560 .remove = __devexit_p(ixgbevf_remove),
3561 #ifdef CONFIG_PM
3562 /* Power Management Hooks */
3563 .suspend = ixgbevf_suspend,
3564 .resume = ixgbevf_resume,
3565 #endif
3566 .shutdown = ixgbevf_shutdown,
3567 .err_handler = &ixgbevf_err_handler
3571 * ixgbevf_init_module - Driver Registration Routine
3573 * ixgbevf_init_module is the first routine called when the driver is
3574 * loaded. All it does is register with the PCI subsystem.
3576 static int __init ixgbevf_init_module(void)
3578 int ret;
3579 pr_info("%s - version %s\n", ixgbevf_driver_string,
3580 ixgbevf_driver_version);
3582 pr_info("%s\n", ixgbevf_copyright);
3584 ret = pci_register_driver(&ixgbevf_driver);
3585 return ret;
3588 module_init(ixgbevf_init_module);
3591 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3593 * ixgbevf_exit_module is called just before the driver is removed
3594 * from memory.
3596 static void __exit ixgbevf_exit_module(void)
3598 pci_unregister_driver(&ixgbevf_driver);
3601 #ifdef DEBUG
3603 * ixgbevf_get_hw_dev_name - return device name string
3604 * used by hardware layer to print debugging information
3606 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3608 struct ixgbevf_adapter *adapter = hw->back;
3609 return adapter->netdev->name;
3612 #endif
3613 module_exit(ixgbevf_exit_module);
3615 /* ixgbevf_main.c */