Staging: hv: mousevsc: Cleanup and properly implement reportdesc_callback()
[zen-stable.git] / drivers / net / ixgbevf / ixgbevf_main.c
blob3b880a27f8d118eb6b08fcd5e23678bdf2c27653
1 /*******************************************************************************
3 Intel 82599 Virtual Function driver
4 Copyright(c) 1999 - 2010 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 ******************************************************************************/
32 #include <linux/types.h>
33 #include <linux/bitops.h>
34 #include <linux/module.h>
35 #include <linux/pci.h>
36 #include <linux/netdevice.h>
37 #include <linux/vmalloc.h>
38 #include <linux/string.h>
39 #include <linux/in.h>
40 #include <linux/ip.h>
41 #include <linux/tcp.h>
42 #include <linux/ipv6.h>
43 #include <linux/slab.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <linux/ethtool.h>
47 #include <linux/if_vlan.h>
48 #include <linux/prefetch.h>
50 #include "ixgbevf.h"
52 char ixgbevf_driver_name[] = "ixgbevf";
53 static const char ixgbevf_driver_string[] =
54 "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
56 #define DRV_VERSION "2.1.0-k"
57 const char ixgbevf_driver_version[] = DRV_VERSION;
58 static char ixgbevf_copyright[] =
59 "Copyright (c) 2009 - 2010 Intel Corporation.";
61 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
62 [board_82599_vf] = &ixgbevf_82599_vf_info,
63 [board_X540_vf] = &ixgbevf_X540_vf_info,
66 /* ixgbevf_pci_tbl - PCI Device ID Table
68 * Wildcard entries (PCI_ANY_ID) should come last
69 * Last entry must be all 0s
71 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
72 * Class, Class Mask, private data (not used) }
74 static struct pci_device_id ixgbevf_pci_tbl[] = {
75 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF),
76 board_82599_vf},
77 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF),
78 board_X540_vf},
80 /* required last entry */
81 {0, }
83 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
85 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
86 MODULE_DESCRIPTION("Intel(R) 82599 Virtual Function Driver");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION);
90 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
92 /* forward decls */
93 static void ixgbevf_set_itr_msix(struct ixgbevf_q_vector *q_vector);
94 static void ixgbevf_write_eitr(struct ixgbevf_adapter *adapter, int v_idx,
95 u32 itr_reg);
97 static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
98 struct ixgbevf_ring *rx_ring,
99 u32 val)
102 * Force memory writes to complete before letting h/w
103 * know there are new descriptors to fetch. (Only
104 * applicable for weak-ordered memory model archs,
105 * such as IA-64).
107 wmb();
108 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
112 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
113 * @adapter: pointer to adapter struct
114 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
115 * @queue: queue to map the corresponding interrupt to
116 * @msix_vector: the vector to map to the corresponding queue
119 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
120 u8 queue, u8 msix_vector)
122 u32 ivar, index;
123 struct ixgbe_hw *hw = &adapter->hw;
124 if (direction == -1) {
125 /* other causes */
126 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
127 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
128 ivar &= ~0xFF;
129 ivar |= msix_vector;
130 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
131 } else {
132 /* tx or rx causes */
133 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
134 index = ((16 * (queue & 1)) + (8 * direction));
135 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
136 ivar &= ~(0xFF << index);
137 ivar |= (msix_vector << index);
138 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
142 static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_adapter *adapter,
143 struct ixgbevf_tx_buffer
144 *tx_buffer_info)
146 if (tx_buffer_info->dma) {
147 if (tx_buffer_info->mapped_as_page)
148 dma_unmap_page(&adapter->pdev->dev,
149 tx_buffer_info->dma,
150 tx_buffer_info->length,
151 DMA_TO_DEVICE);
152 else
153 dma_unmap_single(&adapter->pdev->dev,
154 tx_buffer_info->dma,
155 tx_buffer_info->length,
156 DMA_TO_DEVICE);
157 tx_buffer_info->dma = 0;
159 if (tx_buffer_info->skb) {
160 dev_kfree_skb_any(tx_buffer_info->skb);
161 tx_buffer_info->skb = NULL;
163 tx_buffer_info->time_stamp = 0;
164 /* tx_buffer_info must be completely set up in the transmit path */
167 #define IXGBE_MAX_TXD_PWR 14
168 #define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
170 /* Tx Descriptors needed, worst case */
171 #define TXD_USE_COUNT(S) (((S) >> IXGBE_MAX_TXD_PWR) + \
172 (((S) & (IXGBE_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
173 #ifdef MAX_SKB_FRAGS
174 #define DESC_NEEDED (TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD) /* skb->data */ + \
175 MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1) /* for context */
176 #else
177 #define DESC_NEEDED TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD)
178 #endif
180 static void ixgbevf_tx_timeout(struct net_device *netdev);
183 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
184 * @adapter: board private structure
185 * @tx_ring: tx ring to clean
187 static bool ixgbevf_clean_tx_irq(struct ixgbevf_adapter *adapter,
188 struct ixgbevf_ring *tx_ring)
190 struct net_device *netdev = adapter->netdev;
191 struct ixgbe_hw *hw = &adapter->hw;
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 i = tx_ring->next_to_clean;
198 eop = tx_ring->tx_buffer_info[i].next_to_watch;
199 eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
201 while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
202 (count < tx_ring->work_limit)) {
203 bool cleaned = false;
204 rmb(); /* read buffer_info after eop_desc */
205 for ( ; !cleaned; count++) {
206 struct sk_buff *skb;
207 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
208 tx_buffer_info = &tx_ring->tx_buffer_info[i];
209 cleaned = (i == eop);
210 skb = tx_buffer_info->skb;
212 if (cleaned && skb) {
213 unsigned int segs, bytecount;
215 /* gso_segs is currently only valid for tcp */
216 segs = skb_shinfo(skb)->gso_segs ?: 1;
217 /* multiply data chunks by size of headers */
218 bytecount = ((segs - 1) * skb_headlen(skb)) +
219 skb->len;
220 total_packets += segs;
221 total_bytes += bytecount;
224 ixgbevf_unmap_and_free_tx_resource(adapter,
225 tx_buffer_info);
227 tx_desc->wb.status = 0;
229 i++;
230 if (i == tx_ring->count)
231 i = 0;
234 eop = tx_ring->tx_buffer_info[i].next_to_watch;
235 eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
238 tx_ring->next_to_clean = i;
240 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
241 if (unlikely(count && netif_carrier_ok(netdev) &&
242 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
243 /* Make sure that anybody stopping the queue after this
244 * sees the new next_to_clean.
246 smp_mb();
247 #ifdef HAVE_TX_MQ
248 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
249 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
250 netif_wake_subqueue(netdev, tx_ring->queue_index);
251 ++adapter->restart_queue;
253 #else
254 if (netif_queue_stopped(netdev) &&
255 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
256 netif_wake_queue(netdev);
257 ++adapter->restart_queue;
259 #endif
262 /* re-arm the interrupt */
263 if ((count >= tx_ring->work_limit) &&
264 (!test_bit(__IXGBEVF_DOWN, &adapter->state))) {
265 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, tx_ring->v_idx);
268 tx_ring->total_bytes += total_bytes;
269 tx_ring->total_packets += total_packets;
271 netdev->stats.tx_bytes += total_bytes;
272 netdev->stats.tx_packets += total_packets;
274 return count < tx_ring->work_limit;
278 * ixgbevf_receive_skb - Send a completed packet up the stack
279 * @q_vector: structure containing interrupt and ring information
280 * @skb: packet to send up
281 * @status: hardware indication of status of receive
282 * @rx_ring: rx descriptor ring (for a specific queue) to setup
283 * @rx_desc: rx descriptor
285 static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
286 struct sk_buff *skb, u8 status,
287 struct ixgbevf_ring *ring,
288 union ixgbe_adv_rx_desc *rx_desc)
290 struct ixgbevf_adapter *adapter = q_vector->adapter;
291 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
293 if (is_vlan) {
294 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
296 __vlan_hwaccel_put_tag(skb, tag);
299 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
300 napi_gro_receive(&q_vector->napi, skb);
301 else
302 netif_rx(skb);
306 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
307 * @adapter: address of board private structure
308 * @status_err: hardware indication of status of receive
309 * @skb: skb currently being received and modified
311 static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
312 u32 status_err, struct sk_buff *skb)
314 skb_checksum_none_assert(skb);
316 /* Rx csum disabled */
317 if (!(adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED))
318 return;
320 /* if IP and error */
321 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
322 (status_err & IXGBE_RXDADV_ERR_IPE)) {
323 adapter->hw_csum_rx_error++;
324 return;
327 if (!(status_err & IXGBE_RXD_STAT_L4CS))
328 return;
330 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
331 adapter->hw_csum_rx_error++;
332 return;
335 /* It must be a TCP or UDP packet with a valid checksum */
336 skb->ip_summed = CHECKSUM_UNNECESSARY;
337 adapter->hw_csum_rx_good++;
341 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
342 * @adapter: address of board private structure
344 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
345 struct ixgbevf_ring *rx_ring,
346 int cleaned_count)
348 struct pci_dev *pdev = adapter->pdev;
349 union ixgbe_adv_rx_desc *rx_desc;
350 struct ixgbevf_rx_buffer *bi;
351 struct sk_buff *skb;
352 unsigned int i;
353 unsigned int bufsz = rx_ring->rx_buf_len + NET_IP_ALIGN;
355 i = rx_ring->next_to_use;
356 bi = &rx_ring->rx_buffer_info[i];
358 while (cleaned_count--) {
359 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
361 if (!bi->page_dma &&
362 (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED)) {
363 if (!bi->page) {
364 bi->page = netdev_alloc_page(adapter->netdev);
365 if (!bi->page) {
366 adapter->alloc_rx_page_failed++;
367 goto no_buffers;
369 bi->page_offset = 0;
370 } else {
371 /* use a half page if we're re-using */
372 bi->page_offset ^= (PAGE_SIZE / 2);
375 bi->page_dma = dma_map_page(&pdev->dev, bi->page,
376 bi->page_offset,
377 (PAGE_SIZE / 2),
378 DMA_FROM_DEVICE);
381 skb = bi->skb;
382 if (!skb) {
383 skb = netdev_alloc_skb(adapter->netdev,
384 bufsz);
386 if (!skb) {
387 adapter->alloc_rx_buff_failed++;
388 goto no_buffers;
392 * Make buffer alignment 2 beyond a 16 byte boundary
393 * this will result in a 16 byte aligned IP header after
394 * the 14 byte MAC header is removed
396 skb_reserve(skb, NET_IP_ALIGN);
398 bi->skb = skb;
400 if (!bi->dma) {
401 bi->dma = dma_map_single(&pdev->dev, skb->data,
402 rx_ring->rx_buf_len,
403 DMA_FROM_DEVICE);
405 /* Refresh the desc even if buffer_addrs didn't change because
406 * each write-back erases this info. */
407 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
408 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
409 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
410 } else {
411 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
414 i++;
415 if (i == rx_ring->count)
416 i = 0;
417 bi = &rx_ring->rx_buffer_info[i];
420 no_buffers:
421 if (rx_ring->next_to_use != i) {
422 rx_ring->next_to_use = i;
423 if (i-- == 0)
424 i = (rx_ring->count - 1);
426 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
430 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
431 u64 qmask)
433 u32 mask;
434 struct ixgbe_hw *hw = &adapter->hw;
436 mask = (qmask & 0xFFFFFFFF);
437 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
440 static inline u16 ixgbevf_get_hdr_info(union ixgbe_adv_rx_desc *rx_desc)
442 return rx_desc->wb.lower.lo_dword.hs_rss.hdr_info;
445 static inline u16 ixgbevf_get_pkt_info(union ixgbe_adv_rx_desc *rx_desc)
447 return rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
450 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
451 struct ixgbevf_ring *rx_ring,
452 int *work_done, int work_to_do)
454 struct ixgbevf_adapter *adapter = q_vector->adapter;
455 struct pci_dev *pdev = adapter->pdev;
456 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
457 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
458 struct sk_buff *skb;
459 unsigned int i;
460 u32 len, staterr;
461 u16 hdr_info;
462 bool cleaned = false;
463 int cleaned_count = 0;
464 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
466 i = rx_ring->next_to_clean;
467 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
468 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
469 rx_buffer_info = &rx_ring->rx_buffer_info[i];
471 while (staterr & IXGBE_RXD_STAT_DD) {
472 u32 upper_len = 0;
473 if (*work_done >= work_to_do)
474 break;
475 (*work_done)++;
477 rmb(); /* read descriptor and rx_buffer_info after status DD */
478 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
479 hdr_info = le16_to_cpu(ixgbevf_get_hdr_info(rx_desc));
480 len = (hdr_info & IXGBE_RXDADV_HDRBUFLEN_MASK) >>
481 IXGBE_RXDADV_HDRBUFLEN_SHIFT;
482 if (hdr_info & IXGBE_RXDADV_SPH)
483 adapter->rx_hdr_split++;
484 if (len > IXGBEVF_RX_HDR_SIZE)
485 len = IXGBEVF_RX_HDR_SIZE;
486 upper_len = le16_to_cpu(rx_desc->wb.upper.length);
487 } else {
488 len = le16_to_cpu(rx_desc->wb.upper.length);
490 cleaned = true;
491 skb = rx_buffer_info->skb;
492 prefetch(skb->data - NET_IP_ALIGN);
493 rx_buffer_info->skb = NULL;
495 if (rx_buffer_info->dma) {
496 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
497 rx_ring->rx_buf_len,
498 DMA_FROM_DEVICE);
499 rx_buffer_info->dma = 0;
500 skb_put(skb, len);
503 if (upper_len) {
504 dma_unmap_page(&pdev->dev, rx_buffer_info->page_dma,
505 PAGE_SIZE / 2, DMA_FROM_DEVICE);
506 rx_buffer_info->page_dma = 0;
507 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
508 rx_buffer_info->page,
509 rx_buffer_info->page_offset,
510 upper_len);
512 if ((rx_ring->rx_buf_len > (PAGE_SIZE / 2)) ||
513 (page_count(rx_buffer_info->page) != 1))
514 rx_buffer_info->page = NULL;
515 else
516 get_page(rx_buffer_info->page);
518 skb->len += upper_len;
519 skb->data_len += upper_len;
520 skb->truesize += upper_len;
523 i++;
524 if (i == rx_ring->count)
525 i = 0;
527 next_rxd = IXGBE_RX_DESC_ADV(*rx_ring, i);
528 prefetch(next_rxd);
529 cleaned_count++;
531 next_buffer = &rx_ring->rx_buffer_info[i];
533 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
534 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
535 rx_buffer_info->skb = next_buffer->skb;
536 rx_buffer_info->dma = next_buffer->dma;
537 next_buffer->skb = skb;
538 next_buffer->dma = 0;
539 } else {
540 skb->next = next_buffer->skb;
541 skb->next->prev = skb;
543 adapter->non_eop_descs++;
544 goto next_desc;
547 /* ERR_MASK will only have valid bits if EOP set */
548 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
549 dev_kfree_skb_irq(skb);
550 goto next_desc;
553 ixgbevf_rx_checksum(adapter, staterr, skb);
555 /* probably a little skewed due to removing CRC */
556 total_rx_bytes += skb->len;
557 total_rx_packets++;
560 * Work around issue of some types of VM to VM loop back
561 * packets not getting split correctly
563 if (staterr & IXGBE_RXD_STAT_LB) {
564 u32 header_fixup_len = skb_headlen(skb);
565 if (header_fixup_len < 14)
566 skb_push(skb, header_fixup_len);
568 skb->protocol = eth_type_trans(skb, adapter->netdev);
570 ixgbevf_receive_skb(q_vector, skb, staterr, rx_ring, rx_desc);
572 next_desc:
573 rx_desc->wb.upper.status_error = 0;
575 /* return some buffers to hardware, one at a time is too slow */
576 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
577 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
578 cleaned_count);
579 cleaned_count = 0;
582 /* use prefetched values */
583 rx_desc = next_rxd;
584 rx_buffer_info = &rx_ring->rx_buffer_info[i];
586 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
589 rx_ring->next_to_clean = i;
590 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
592 if (cleaned_count)
593 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
595 rx_ring->total_packets += total_rx_packets;
596 rx_ring->total_bytes += total_rx_bytes;
597 adapter->netdev->stats.rx_bytes += total_rx_bytes;
598 adapter->netdev->stats.rx_packets += total_rx_packets;
600 return cleaned;
604 * ixgbevf_clean_rxonly - msix (aka one shot) rx clean routine
605 * @napi: napi struct with our devices info in it
606 * @budget: amount of work driver is allowed to do this pass, in packets
608 * This function is optimized for cleaning one queue only on a single
609 * q_vector!!!
611 static int ixgbevf_clean_rxonly(struct napi_struct *napi, int budget)
613 struct ixgbevf_q_vector *q_vector =
614 container_of(napi, struct ixgbevf_q_vector, napi);
615 struct ixgbevf_adapter *adapter = q_vector->adapter;
616 struct ixgbevf_ring *rx_ring = NULL;
617 int work_done = 0;
618 long r_idx;
620 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
621 rx_ring = &(adapter->rx_ring[r_idx]);
623 ixgbevf_clean_rx_irq(q_vector, rx_ring, &work_done, budget);
625 /* If all Rx work done, exit the polling mode */
626 if (work_done < budget) {
627 napi_complete(napi);
628 if (adapter->itr_setting & 1)
629 ixgbevf_set_itr_msix(q_vector);
630 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
631 ixgbevf_irq_enable_queues(adapter, rx_ring->v_idx);
634 return work_done;
638 * ixgbevf_clean_rxonly_many - msix (aka one shot) rx clean routine
639 * @napi: napi struct with our devices info in it
640 * @budget: amount of work driver is allowed to do this pass, in packets
642 * This function will clean more than one rx queue associated with a
643 * q_vector.
645 static int ixgbevf_clean_rxonly_many(struct napi_struct *napi, int budget)
647 struct ixgbevf_q_vector *q_vector =
648 container_of(napi, struct ixgbevf_q_vector, napi);
649 struct ixgbevf_adapter *adapter = q_vector->adapter;
650 struct ixgbevf_ring *rx_ring = NULL;
651 int work_done = 0, i;
652 long r_idx;
653 u64 enable_mask = 0;
655 /* attempt to distribute budget to each queue fairly, but don't allow
656 * the budget to go below 1 because we'll exit polling */
657 budget /= (q_vector->rxr_count ?: 1);
658 budget = max(budget, 1);
659 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
660 for (i = 0; i < q_vector->rxr_count; i++) {
661 rx_ring = &(adapter->rx_ring[r_idx]);
662 ixgbevf_clean_rx_irq(q_vector, rx_ring, &work_done, budget);
663 enable_mask |= rx_ring->v_idx;
664 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
665 r_idx + 1);
668 #ifndef HAVE_NETDEV_NAPI_LIST
669 if (!netif_running(adapter->netdev))
670 work_done = 0;
672 #endif
673 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
674 rx_ring = &(adapter->rx_ring[r_idx]);
676 /* If all Rx work done, exit the polling mode */
677 if (work_done < budget) {
678 napi_complete(napi);
679 if (adapter->itr_setting & 1)
680 ixgbevf_set_itr_msix(q_vector);
681 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
682 ixgbevf_irq_enable_queues(adapter, enable_mask);
685 return work_done;
690 * ixgbevf_configure_msix - Configure MSI-X hardware
691 * @adapter: board private structure
693 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
694 * interrupts.
696 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
698 struct ixgbevf_q_vector *q_vector;
699 struct ixgbe_hw *hw = &adapter->hw;
700 int i, j, q_vectors, v_idx, r_idx;
701 u32 mask;
703 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
706 * Populate the IVAR table and set the ITR values to the
707 * corresponding register.
709 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
710 q_vector = adapter->q_vector[v_idx];
711 /* XXX for_each_set_bit(...) */
712 r_idx = find_first_bit(q_vector->rxr_idx,
713 adapter->num_rx_queues);
715 for (i = 0; i < q_vector->rxr_count; i++) {
716 j = adapter->rx_ring[r_idx].reg_idx;
717 ixgbevf_set_ivar(adapter, 0, j, v_idx);
718 r_idx = find_next_bit(q_vector->rxr_idx,
719 adapter->num_rx_queues,
720 r_idx + 1);
722 r_idx = find_first_bit(q_vector->txr_idx,
723 adapter->num_tx_queues);
725 for (i = 0; i < q_vector->txr_count; i++) {
726 j = adapter->tx_ring[r_idx].reg_idx;
727 ixgbevf_set_ivar(adapter, 1, j, v_idx);
728 r_idx = find_next_bit(q_vector->txr_idx,
729 adapter->num_tx_queues,
730 r_idx + 1);
733 /* if this is a tx only vector halve the interrupt rate */
734 if (q_vector->txr_count && !q_vector->rxr_count)
735 q_vector->eitr = (adapter->eitr_param >> 1);
736 else if (q_vector->rxr_count)
737 /* rx only */
738 q_vector->eitr = adapter->eitr_param;
740 ixgbevf_write_eitr(adapter, v_idx, q_vector->eitr);
743 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
745 /* set up to autoclear timer, and the vectors */
746 mask = IXGBE_EIMS_ENABLE_MASK;
747 mask &= ~IXGBE_EIMS_OTHER;
748 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, mask);
751 enum latency_range {
752 lowest_latency = 0,
753 low_latency = 1,
754 bulk_latency = 2,
755 latency_invalid = 255
759 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
760 * @adapter: pointer to adapter
761 * @eitr: eitr setting (ints per sec) to give last timeslice
762 * @itr_setting: current throttle rate in ints/second
763 * @packets: the number of packets during this measurement interval
764 * @bytes: the number of bytes during this measurement interval
766 * Stores a new ITR value based on packets and byte
767 * counts during the last interrupt. The advantage of per interrupt
768 * computation is faster updates and more accurate ITR for the current
769 * traffic pattern. Constants in this function were computed
770 * based on theoretical maximum wire speed and thresholds were set based
771 * on testing data as well as attempting to minimize response time
772 * while increasing bulk throughput.
774 static u8 ixgbevf_update_itr(struct ixgbevf_adapter *adapter,
775 u32 eitr, u8 itr_setting,
776 int packets, int bytes)
778 unsigned int retval = itr_setting;
779 u32 timepassed_us;
780 u64 bytes_perint;
782 if (packets == 0)
783 goto update_itr_done;
786 /* simple throttlerate management
787 * 0-20MB/s lowest (100000 ints/s)
788 * 20-100MB/s low (20000 ints/s)
789 * 100-1249MB/s bulk (8000 ints/s)
791 /* what was last interrupt timeslice? */
792 timepassed_us = 1000000/eitr;
793 bytes_perint = bytes / timepassed_us; /* bytes/usec */
795 switch (itr_setting) {
796 case lowest_latency:
797 if (bytes_perint > adapter->eitr_low)
798 retval = low_latency;
799 break;
800 case low_latency:
801 if (bytes_perint > adapter->eitr_high)
802 retval = bulk_latency;
803 else if (bytes_perint <= adapter->eitr_low)
804 retval = lowest_latency;
805 break;
806 case bulk_latency:
807 if (bytes_perint <= adapter->eitr_high)
808 retval = low_latency;
809 break;
812 update_itr_done:
813 return retval;
817 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
818 * @adapter: pointer to adapter struct
819 * @v_idx: vector index into q_vector array
820 * @itr_reg: new value to be written in *register* format, not ints/s
822 * This function is made to be called by ethtool and by the driver
823 * when it needs to update VTEITR registers at runtime. Hardware
824 * specific quirks/differences are taken care of here.
826 static void ixgbevf_write_eitr(struct ixgbevf_adapter *adapter, int v_idx,
827 u32 itr_reg)
829 struct ixgbe_hw *hw = &adapter->hw;
831 itr_reg = EITR_INTS_PER_SEC_TO_REG(itr_reg);
834 * set the WDIS bit to not clear the timer bits and cause an
835 * immediate assertion of the interrupt
837 itr_reg |= IXGBE_EITR_CNT_WDIS;
839 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
842 static void ixgbevf_set_itr_msix(struct ixgbevf_q_vector *q_vector)
844 struct ixgbevf_adapter *adapter = q_vector->adapter;
845 u32 new_itr;
846 u8 current_itr, ret_itr;
847 int i, r_idx, v_idx = q_vector->v_idx;
848 struct ixgbevf_ring *rx_ring, *tx_ring;
850 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
851 for (i = 0; i < q_vector->txr_count; i++) {
852 tx_ring = &(adapter->tx_ring[r_idx]);
853 ret_itr = ixgbevf_update_itr(adapter, q_vector->eitr,
854 q_vector->tx_itr,
855 tx_ring->total_packets,
856 tx_ring->total_bytes);
857 /* if the result for this queue would decrease interrupt
858 * rate for this vector then use that result */
859 q_vector->tx_itr = ((q_vector->tx_itr > ret_itr) ?
860 q_vector->tx_itr - 1 : ret_itr);
861 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
862 r_idx + 1);
865 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
866 for (i = 0; i < q_vector->rxr_count; i++) {
867 rx_ring = &(adapter->rx_ring[r_idx]);
868 ret_itr = ixgbevf_update_itr(adapter, q_vector->eitr,
869 q_vector->rx_itr,
870 rx_ring->total_packets,
871 rx_ring->total_bytes);
872 /* if the result for this queue would decrease interrupt
873 * rate for this vector then use that result */
874 q_vector->rx_itr = ((q_vector->rx_itr > ret_itr) ?
875 q_vector->rx_itr - 1 : ret_itr);
876 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
877 r_idx + 1);
880 current_itr = max(q_vector->rx_itr, q_vector->tx_itr);
882 switch (current_itr) {
883 /* counts and packets in update_itr are dependent on these numbers */
884 case lowest_latency:
885 new_itr = 100000;
886 break;
887 case low_latency:
888 new_itr = 20000; /* aka hwitr = ~200 */
889 break;
890 case bulk_latency:
891 default:
892 new_itr = 8000;
893 break;
896 if (new_itr != q_vector->eitr) {
897 u32 itr_reg;
899 /* save the algorithm value here, not the smoothed one */
900 q_vector->eitr = new_itr;
901 /* do an exponential smoothing */
902 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
903 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
904 ixgbevf_write_eitr(adapter, v_idx, itr_reg);
908 static irqreturn_t ixgbevf_msix_mbx(int irq, void *data)
910 struct net_device *netdev = data;
911 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
912 struct ixgbe_hw *hw = &adapter->hw;
913 u32 eicr;
914 u32 msg;
916 eicr = IXGBE_READ_REG(hw, IXGBE_VTEICS);
917 IXGBE_WRITE_REG(hw, IXGBE_VTEICR, eicr);
919 if (!hw->mbx.ops.check_for_ack(hw)) {
921 * checking for the ack clears the PFACK bit. Place
922 * it back in the v2p_mailbox cache so that anyone
923 * polling for an ack will not miss it. Also
924 * avoid the read below because the code to read
925 * the mailbox will also clear the ack bit. This was
926 * causing lost acks. Just cache the bit and exit
927 * the IRQ handler.
929 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFACK;
930 goto out;
933 /* Not an ack interrupt, go ahead and read the message */
934 hw->mbx.ops.read(hw, &msg, 1);
936 if ((msg & IXGBE_MBVFICR_VFREQ_MASK) == IXGBE_PF_CONTROL_MSG)
937 mod_timer(&adapter->watchdog_timer,
938 round_jiffies(jiffies + 1));
940 out:
941 return IRQ_HANDLED;
944 static irqreturn_t ixgbevf_msix_clean_tx(int irq, void *data)
946 struct ixgbevf_q_vector *q_vector = data;
947 struct ixgbevf_adapter *adapter = q_vector->adapter;
948 struct ixgbevf_ring *tx_ring;
949 int i, r_idx;
951 if (!q_vector->txr_count)
952 return IRQ_HANDLED;
954 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
955 for (i = 0; i < q_vector->txr_count; i++) {
956 tx_ring = &(adapter->tx_ring[r_idx]);
957 tx_ring->total_bytes = 0;
958 tx_ring->total_packets = 0;
959 ixgbevf_clean_tx_irq(adapter, tx_ring);
960 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
961 r_idx + 1);
964 if (adapter->itr_setting & 1)
965 ixgbevf_set_itr_msix(q_vector);
967 return IRQ_HANDLED;
971 * ixgbevf_msix_clean_rx - single unshared vector rx clean (all queues)
972 * @irq: unused
973 * @data: pointer to our q_vector struct for this interrupt vector
975 static irqreturn_t ixgbevf_msix_clean_rx(int irq, void *data)
977 struct ixgbevf_q_vector *q_vector = data;
978 struct ixgbevf_adapter *adapter = q_vector->adapter;
979 struct ixgbe_hw *hw = &adapter->hw;
980 struct ixgbevf_ring *rx_ring;
981 int r_idx;
982 int i;
984 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
985 for (i = 0; i < q_vector->rxr_count; i++) {
986 rx_ring = &(adapter->rx_ring[r_idx]);
987 rx_ring->total_bytes = 0;
988 rx_ring->total_packets = 0;
989 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
990 r_idx + 1);
993 if (!q_vector->rxr_count)
994 return IRQ_HANDLED;
996 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
997 rx_ring = &(adapter->rx_ring[r_idx]);
998 /* disable interrupts on this vector only */
999 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, rx_ring->v_idx);
1000 napi_schedule(&q_vector->napi);
1003 return IRQ_HANDLED;
1006 static irqreturn_t ixgbevf_msix_clean_many(int irq, void *data)
1008 ixgbevf_msix_clean_rx(irq, data);
1009 ixgbevf_msix_clean_tx(irq, data);
1011 return IRQ_HANDLED;
1014 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
1015 int r_idx)
1017 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
1019 set_bit(r_idx, q_vector->rxr_idx);
1020 q_vector->rxr_count++;
1021 a->rx_ring[r_idx].v_idx = 1 << v_idx;
1024 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
1025 int t_idx)
1027 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
1029 set_bit(t_idx, q_vector->txr_idx);
1030 q_vector->txr_count++;
1031 a->tx_ring[t_idx].v_idx = 1 << v_idx;
1035 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
1036 * @adapter: board private structure to initialize
1038 * This function maps descriptor rings to the queue-specific vectors
1039 * we were allotted through the MSI-X enabling code. Ideally, we'd have
1040 * one vector per ring/queue, but on a constrained vector budget, we
1041 * group the rings as "efficiently" as possible. You would add new
1042 * mapping configurations in here.
1044 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
1046 int q_vectors;
1047 int v_start = 0;
1048 int rxr_idx = 0, txr_idx = 0;
1049 int rxr_remaining = adapter->num_rx_queues;
1050 int txr_remaining = adapter->num_tx_queues;
1051 int i, j;
1052 int rqpv, tqpv;
1053 int err = 0;
1055 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1058 * The ideal configuration...
1059 * We have enough vectors to map one per queue.
1061 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
1062 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
1063 map_vector_to_rxq(adapter, v_start, rxr_idx);
1065 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
1066 map_vector_to_txq(adapter, v_start, txr_idx);
1067 goto out;
1071 * If we don't have enough vectors for a 1-to-1
1072 * mapping, we'll have to group them so there are
1073 * multiple queues per vector.
1075 /* Re-adjusting *qpv takes care of the remainder. */
1076 for (i = v_start; i < q_vectors; i++) {
1077 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
1078 for (j = 0; j < rqpv; j++) {
1079 map_vector_to_rxq(adapter, i, rxr_idx);
1080 rxr_idx++;
1081 rxr_remaining--;
1084 for (i = v_start; i < q_vectors; i++) {
1085 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
1086 for (j = 0; j < tqpv; j++) {
1087 map_vector_to_txq(adapter, i, txr_idx);
1088 txr_idx++;
1089 txr_remaining--;
1093 out:
1094 return err;
1098 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1099 * @adapter: board private structure
1101 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1102 * interrupts from the kernel.
1104 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1106 struct net_device *netdev = adapter->netdev;
1107 irqreturn_t (*handler)(int, void *);
1108 int i, vector, q_vectors, err;
1109 int ri = 0, ti = 0;
1111 /* Decrement for Other and TCP Timer vectors */
1112 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1114 #define SET_HANDLER(_v) (((_v)->rxr_count && (_v)->txr_count) \
1115 ? &ixgbevf_msix_clean_many : \
1116 (_v)->rxr_count ? &ixgbevf_msix_clean_rx : \
1117 (_v)->txr_count ? &ixgbevf_msix_clean_tx : \
1118 NULL)
1119 for (vector = 0; vector < q_vectors; vector++) {
1120 handler = SET_HANDLER(adapter->q_vector[vector]);
1122 if (handler == &ixgbevf_msix_clean_rx) {
1123 sprintf(adapter->name[vector], "%s-%s-%d",
1124 netdev->name, "rx", ri++);
1125 } else if (handler == &ixgbevf_msix_clean_tx) {
1126 sprintf(adapter->name[vector], "%s-%s-%d",
1127 netdev->name, "tx", ti++);
1128 } else if (handler == &ixgbevf_msix_clean_many) {
1129 sprintf(adapter->name[vector], "%s-%s-%d",
1130 netdev->name, "TxRx", vector);
1131 } else {
1132 /* skip this unused q_vector */
1133 continue;
1135 err = request_irq(adapter->msix_entries[vector].vector,
1136 handler, 0, adapter->name[vector],
1137 adapter->q_vector[vector]);
1138 if (err) {
1139 hw_dbg(&adapter->hw,
1140 "request_irq failed for MSIX interrupt "
1141 "Error: %d\n", err);
1142 goto free_queue_irqs;
1146 sprintf(adapter->name[vector], "%s:mbx", netdev->name);
1147 err = request_irq(adapter->msix_entries[vector].vector,
1148 &ixgbevf_msix_mbx, 0, adapter->name[vector], netdev);
1149 if (err) {
1150 hw_dbg(&adapter->hw,
1151 "request_irq for msix_mbx failed: %d\n", err);
1152 goto free_queue_irqs;
1155 return 0;
1157 free_queue_irqs:
1158 for (i = vector - 1; i >= 0; i--)
1159 free_irq(adapter->msix_entries[--vector].vector,
1160 &(adapter->q_vector[i]));
1161 pci_disable_msix(adapter->pdev);
1162 kfree(adapter->msix_entries);
1163 adapter->msix_entries = NULL;
1164 return err;
1167 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
1169 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1171 for (i = 0; i < q_vectors; i++) {
1172 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
1173 bitmap_zero(q_vector->rxr_idx, MAX_RX_QUEUES);
1174 bitmap_zero(q_vector->txr_idx, MAX_TX_QUEUES);
1175 q_vector->rxr_count = 0;
1176 q_vector->txr_count = 0;
1177 q_vector->eitr = adapter->eitr_param;
1182 * ixgbevf_request_irq - initialize interrupts
1183 * @adapter: board private structure
1185 * Attempts to configure interrupts using the best available
1186 * capabilities of the hardware and kernel.
1188 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1190 int err = 0;
1192 err = ixgbevf_request_msix_irqs(adapter);
1194 if (err)
1195 hw_dbg(&adapter->hw,
1196 "request_irq failed, Error %d\n", err);
1198 return err;
1201 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1203 struct net_device *netdev = adapter->netdev;
1204 int i, q_vectors;
1206 q_vectors = adapter->num_msix_vectors;
1208 i = q_vectors - 1;
1210 free_irq(adapter->msix_entries[i].vector, netdev);
1211 i--;
1213 for (; i >= 0; i--) {
1214 free_irq(adapter->msix_entries[i].vector,
1215 adapter->q_vector[i]);
1218 ixgbevf_reset_q_vectors(adapter);
1222 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1223 * @adapter: board private structure
1225 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1227 int i;
1228 struct ixgbe_hw *hw = &adapter->hw;
1230 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1232 IXGBE_WRITE_FLUSH(hw);
1234 for (i = 0; i < adapter->num_msix_vectors; i++)
1235 synchronize_irq(adapter->msix_entries[i].vector);
1239 * ixgbevf_irq_enable - Enable default interrupt generation settings
1240 * @adapter: board private structure
1242 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter,
1243 bool queues, bool flush)
1245 struct ixgbe_hw *hw = &adapter->hw;
1246 u32 mask;
1247 u64 qmask;
1249 mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
1250 qmask = ~0;
1252 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
1254 if (queues)
1255 ixgbevf_irq_enable_queues(adapter, qmask);
1257 if (flush)
1258 IXGBE_WRITE_FLUSH(hw);
1262 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1263 * @adapter: board private structure
1265 * Configure the Tx unit of the MAC after a reset.
1267 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1269 u64 tdba;
1270 struct ixgbe_hw *hw = &adapter->hw;
1271 u32 i, j, tdlen, txctrl;
1273 /* Setup the HW Tx Head and Tail descriptor pointers */
1274 for (i = 0; i < adapter->num_tx_queues; i++) {
1275 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1276 j = ring->reg_idx;
1277 tdba = ring->dma;
1278 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1279 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1280 (tdba & DMA_BIT_MASK(32)));
1281 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1282 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1283 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1284 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1285 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1286 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1287 /* Disable Tx Head Writeback RO bit, since this hoses
1288 * bookkeeping if things aren't delivered in order.
1290 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1291 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1292 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1296 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1298 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1300 struct ixgbevf_ring *rx_ring;
1301 struct ixgbe_hw *hw = &adapter->hw;
1302 u32 srrctl;
1304 rx_ring = &adapter->rx_ring[index];
1306 srrctl = IXGBE_SRRCTL_DROP_EN;
1308 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1309 u16 bufsz = IXGBEVF_RXBUFFER_2048;
1310 /* grow the amount we can receive on large page machines */
1311 if (bufsz < (PAGE_SIZE / 2))
1312 bufsz = (PAGE_SIZE / 2);
1313 /* cap the bufsz at our largest descriptor size */
1314 bufsz = min((u16)IXGBEVF_MAX_RXBUFFER, bufsz);
1316 srrctl |= bufsz >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1317 srrctl |= IXGBE_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
1318 srrctl |= ((IXGBEVF_RX_HDR_SIZE <<
1319 IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
1320 IXGBE_SRRCTL_BSIZEHDR_MASK);
1321 } else {
1322 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1324 if (rx_ring->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
1325 srrctl |= IXGBEVF_RXBUFFER_2048 >>
1326 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1327 else
1328 srrctl |= rx_ring->rx_buf_len >>
1329 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1331 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1335 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1336 * @adapter: board private structure
1338 * Configure the Rx unit of the MAC after a reset.
1340 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1342 u64 rdba;
1343 struct ixgbe_hw *hw = &adapter->hw;
1344 struct net_device *netdev = adapter->netdev;
1345 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1346 int i, j;
1347 u32 rdlen;
1348 int rx_buf_len;
1350 /* Decide whether to use packet split mode or not */
1351 if (netdev->mtu > ETH_DATA_LEN) {
1352 if (adapter->flags & IXGBE_FLAG_RX_PS_CAPABLE)
1353 adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
1354 else
1355 adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
1356 } else {
1357 if (adapter->flags & IXGBE_FLAG_RX_1BUF_CAPABLE)
1358 adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
1359 else
1360 adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
1363 /* Set the RX buffer length according to the mode */
1364 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1365 /* PSRTYPE must be initialized in 82599 */
1366 u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
1367 IXGBE_PSRTYPE_UDPHDR |
1368 IXGBE_PSRTYPE_IPV4HDR |
1369 IXGBE_PSRTYPE_IPV6HDR |
1370 IXGBE_PSRTYPE_L2HDR;
1371 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1372 rx_buf_len = IXGBEVF_RX_HDR_SIZE;
1373 } else {
1374 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
1375 if (netdev->mtu <= ETH_DATA_LEN)
1376 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1377 else
1378 rx_buf_len = ALIGN(max_frame, 1024);
1381 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1382 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1383 * the Base and Length of the Rx Descriptor Ring */
1384 for (i = 0; i < adapter->num_rx_queues; i++) {
1385 rdba = adapter->rx_ring[i].dma;
1386 j = adapter->rx_ring[i].reg_idx;
1387 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1388 (rdba & DMA_BIT_MASK(32)));
1389 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1390 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1391 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1392 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1393 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1394 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1395 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1397 ixgbevf_configure_srrctl(adapter, j);
1401 static void ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1403 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1404 struct ixgbe_hw *hw = &adapter->hw;
1406 /* add VID to filter table */
1407 if (hw->mac.ops.set_vfta)
1408 hw->mac.ops.set_vfta(hw, vid, 0, true);
1409 set_bit(vid, adapter->active_vlans);
1412 static void ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1414 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1415 struct ixgbe_hw *hw = &adapter->hw;
1417 /* remove VID from filter table */
1418 if (hw->mac.ops.set_vfta)
1419 hw->mac.ops.set_vfta(hw, vid, 0, false);
1420 clear_bit(vid, adapter->active_vlans);
1423 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1425 u16 vid;
1427 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1428 ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
1431 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1433 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1434 struct ixgbe_hw *hw = &adapter->hw;
1435 int count = 0;
1437 if ((netdev_uc_count(netdev)) > 10) {
1438 printk(KERN_ERR "Too many unicast filters - No Space\n");
1439 return -ENOSPC;
1442 if (!netdev_uc_empty(netdev)) {
1443 struct netdev_hw_addr *ha;
1444 netdev_for_each_uc_addr(ha, netdev) {
1445 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1446 udelay(200);
1448 } else {
1450 * If the list is empty then send message to PF driver to
1451 * clear all macvlans on this VF.
1453 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1456 return count;
1460 * ixgbevf_set_rx_mode - Multicast set
1461 * @netdev: network interface device structure
1463 * The set_rx_method entry point is called whenever the multicast address
1464 * list or the network interface flags are updated. This routine is
1465 * responsible for configuring the hardware for proper multicast mode.
1467 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1469 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1470 struct ixgbe_hw *hw = &adapter->hw;
1472 /* reprogram multicast list */
1473 if (hw->mac.ops.update_mc_addr_list)
1474 hw->mac.ops.update_mc_addr_list(hw, netdev);
1476 ixgbevf_write_uc_addr_list(netdev);
1479 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1481 int q_idx;
1482 struct ixgbevf_q_vector *q_vector;
1483 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1485 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1486 struct napi_struct *napi;
1487 q_vector = adapter->q_vector[q_idx];
1488 if (!q_vector->rxr_count)
1489 continue;
1490 napi = &q_vector->napi;
1491 if (q_vector->rxr_count > 1)
1492 napi->poll = &ixgbevf_clean_rxonly_many;
1494 napi_enable(napi);
1498 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1500 int q_idx;
1501 struct ixgbevf_q_vector *q_vector;
1502 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1504 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1505 q_vector = adapter->q_vector[q_idx];
1506 if (!q_vector->rxr_count)
1507 continue;
1508 napi_disable(&q_vector->napi);
1512 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1514 struct net_device *netdev = adapter->netdev;
1515 int i;
1517 ixgbevf_set_rx_mode(netdev);
1519 ixgbevf_restore_vlan(adapter);
1521 ixgbevf_configure_tx(adapter);
1522 ixgbevf_configure_rx(adapter);
1523 for (i = 0; i < adapter->num_rx_queues; i++) {
1524 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1525 ixgbevf_alloc_rx_buffers(adapter, ring, ring->count);
1526 ring->next_to_use = ring->count - 1;
1527 writel(ring->next_to_use, adapter->hw.hw_addr + ring->tail);
1531 #define IXGBE_MAX_RX_DESC_POLL 10
1532 static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1533 int rxr)
1535 struct ixgbe_hw *hw = &adapter->hw;
1536 int j = adapter->rx_ring[rxr].reg_idx;
1537 int k;
1539 for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1540 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1541 break;
1542 else
1543 msleep(1);
1545 if (k >= IXGBE_MAX_RX_DESC_POLL) {
1546 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1547 "not set within the polling period\n", rxr);
1550 ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
1551 (adapter->rx_ring[rxr].count - 1));
1554 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1556 /* Only save pre-reset stats if there are some */
1557 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1558 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1559 adapter->stats.base_vfgprc;
1560 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1561 adapter->stats.base_vfgptc;
1562 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1563 adapter->stats.base_vfgorc;
1564 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1565 adapter->stats.base_vfgotc;
1566 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1567 adapter->stats.base_vfmprc;
1571 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1573 struct ixgbe_hw *hw = &adapter->hw;
1575 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1576 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1577 adapter->stats.last_vfgorc |=
1578 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1579 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1580 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1581 adapter->stats.last_vfgotc |=
1582 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1583 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1585 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1586 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1587 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1588 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1589 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1592 static int ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1594 struct net_device *netdev = adapter->netdev;
1595 struct ixgbe_hw *hw = &adapter->hw;
1596 int i, j = 0;
1597 int num_rx_rings = adapter->num_rx_queues;
1598 u32 txdctl, rxdctl;
1600 for (i = 0; i < adapter->num_tx_queues; i++) {
1601 j = adapter->tx_ring[i].reg_idx;
1602 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1603 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1604 txdctl |= (8 << 16);
1605 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1608 for (i = 0; i < adapter->num_tx_queues; i++) {
1609 j = adapter->tx_ring[i].reg_idx;
1610 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1611 txdctl |= IXGBE_TXDCTL_ENABLE;
1612 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1615 for (i = 0; i < num_rx_rings; i++) {
1616 j = adapter->rx_ring[i].reg_idx;
1617 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1618 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1619 if (hw->mac.type == ixgbe_mac_X540_vf) {
1620 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1621 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1622 IXGBE_RXDCTL_RLPML_EN);
1624 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1625 ixgbevf_rx_desc_queue_enable(adapter, i);
1628 ixgbevf_configure_msix(adapter);
1630 if (hw->mac.ops.set_rar) {
1631 if (is_valid_ether_addr(hw->mac.addr))
1632 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1633 else
1634 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1637 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1638 ixgbevf_napi_enable_all(adapter);
1640 /* enable transmits */
1641 netif_tx_start_all_queues(netdev);
1643 ixgbevf_save_reset_stats(adapter);
1644 ixgbevf_init_last_counter_stats(adapter);
1646 /* bring the link up in the watchdog, this could race with our first
1647 * link up interrupt but shouldn't be a problem */
1648 adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
1649 adapter->link_check_timeout = jiffies;
1650 mod_timer(&adapter->watchdog_timer, jiffies);
1651 return 0;
1654 int ixgbevf_up(struct ixgbevf_adapter *adapter)
1656 int err;
1657 struct ixgbe_hw *hw = &adapter->hw;
1659 ixgbevf_configure(adapter);
1661 err = ixgbevf_up_complete(adapter);
1663 /* clear any pending interrupts, may auto mask */
1664 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1666 ixgbevf_irq_enable(adapter, true, true);
1668 return err;
1672 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1673 * @adapter: board private structure
1674 * @rx_ring: ring to free buffers from
1676 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1677 struct ixgbevf_ring *rx_ring)
1679 struct pci_dev *pdev = adapter->pdev;
1680 unsigned long size;
1681 unsigned int i;
1683 if (!rx_ring->rx_buffer_info)
1684 return;
1686 /* Free all the Rx ring sk_buffs */
1687 for (i = 0; i < rx_ring->count; i++) {
1688 struct ixgbevf_rx_buffer *rx_buffer_info;
1690 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1691 if (rx_buffer_info->dma) {
1692 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1693 rx_ring->rx_buf_len,
1694 DMA_FROM_DEVICE);
1695 rx_buffer_info->dma = 0;
1697 if (rx_buffer_info->skb) {
1698 struct sk_buff *skb = rx_buffer_info->skb;
1699 rx_buffer_info->skb = NULL;
1700 do {
1701 struct sk_buff *this = skb;
1702 skb = skb->prev;
1703 dev_kfree_skb(this);
1704 } while (skb);
1706 if (!rx_buffer_info->page)
1707 continue;
1708 dma_unmap_page(&pdev->dev, rx_buffer_info->page_dma,
1709 PAGE_SIZE / 2, DMA_FROM_DEVICE);
1710 rx_buffer_info->page_dma = 0;
1711 put_page(rx_buffer_info->page);
1712 rx_buffer_info->page = NULL;
1713 rx_buffer_info->page_offset = 0;
1716 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1717 memset(rx_ring->rx_buffer_info, 0, size);
1719 /* Zero out the descriptor ring */
1720 memset(rx_ring->desc, 0, rx_ring->size);
1722 rx_ring->next_to_clean = 0;
1723 rx_ring->next_to_use = 0;
1725 if (rx_ring->head)
1726 writel(0, adapter->hw.hw_addr + rx_ring->head);
1727 if (rx_ring->tail)
1728 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1732 * ixgbevf_clean_tx_ring - Free Tx Buffers
1733 * @adapter: board private structure
1734 * @tx_ring: ring to be cleaned
1736 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1737 struct ixgbevf_ring *tx_ring)
1739 struct ixgbevf_tx_buffer *tx_buffer_info;
1740 unsigned long size;
1741 unsigned int i;
1743 if (!tx_ring->tx_buffer_info)
1744 return;
1746 /* Free all the Tx ring sk_buffs */
1748 for (i = 0; i < tx_ring->count; i++) {
1749 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1750 ixgbevf_unmap_and_free_tx_resource(adapter, tx_buffer_info);
1753 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1754 memset(tx_ring->tx_buffer_info, 0, size);
1756 memset(tx_ring->desc, 0, tx_ring->size);
1758 tx_ring->next_to_use = 0;
1759 tx_ring->next_to_clean = 0;
1761 if (tx_ring->head)
1762 writel(0, adapter->hw.hw_addr + tx_ring->head);
1763 if (tx_ring->tail)
1764 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1768 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1769 * @adapter: board private structure
1771 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1773 int i;
1775 for (i = 0; i < adapter->num_rx_queues; i++)
1776 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1780 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1781 * @adapter: board private structure
1783 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1785 int i;
1787 for (i = 0; i < adapter->num_tx_queues; i++)
1788 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1791 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1793 struct net_device *netdev = adapter->netdev;
1794 struct ixgbe_hw *hw = &adapter->hw;
1795 u32 txdctl;
1796 int i, j;
1798 /* signal that we are down to the interrupt handler */
1799 set_bit(__IXGBEVF_DOWN, &adapter->state);
1800 /* disable receives */
1802 netif_tx_disable(netdev);
1804 msleep(10);
1806 netif_tx_stop_all_queues(netdev);
1808 ixgbevf_irq_disable(adapter);
1810 ixgbevf_napi_disable_all(adapter);
1812 del_timer_sync(&adapter->watchdog_timer);
1813 /* can't call flush scheduled work here because it can deadlock
1814 * if linkwatch_event tries to acquire the rtnl_lock which we are
1815 * holding */
1816 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1817 msleep(1);
1819 /* disable transmits in the hardware now that interrupts are off */
1820 for (i = 0; i < adapter->num_tx_queues; i++) {
1821 j = adapter->tx_ring[i].reg_idx;
1822 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1823 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1824 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1827 netif_carrier_off(netdev);
1829 if (!pci_channel_offline(adapter->pdev))
1830 ixgbevf_reset(adapter);
1832 ixgbevf_clean_all_tx_rings(adapter);
1833 ixgbevf_clean_all_rx_rings(adapter);
1836 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1838 struct ixgbe_hw *hw = &adapter->hw;
1840 WARN_ON(in_interrupt());
1842 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1843 msleep(1);
1846 * Check if PF is up before re-init. If not then skip until
1847 * later when the PF is up and ready to service requests from
1848 * the VF via mailbox. If the VF is up and running then the
1849 * watchdog task will continue to schedule reset tasks until
1850 * the PF is up and running.
1852 if (!hw->mac.ops.reset_hw(hw)) {
1853 ixgbevf_down(adapter);
1854 ixgbevf_up(adapter);
1857 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1860 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1862 struct ixgbe_hw *hw = &adapter->hw;
1863 struct net_device *netdev = adapter->netdev;
1865 if (hw->mac.ops.reset_hw(hw))
1866 hw_dbg(hw, "PF still resetting\n");
1867 else
1868 hw->mac.ops.init_hw(hw);
1870 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1871 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1872 netdev->addr_len);
1873 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1874 netdev->addr_len);
1878 static void ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1879 int vectors)
1881 int err, vector_threshold;
1883 /* We'll want at least 3 (vector_threshold):
1884 * 1) TxQ[0] Cleanup
1885 * 2) RxQ[0] Cleanup
1886 * 3) Other (Link Status Change, etc.)
1888 vector_threshold = MIN_MSIX_COUNT;
1890 /* The more we get, the more we will assign to Tx/Rx Cleanup
1891 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1892 * Right now, we simply care about how many we'll get; we'll
1893 * set them up later while requesting irq's.
1895 while (vectors >= vector_threshold) {
1896 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1897 vectors);
1898 if (!err) /* Success in acquiring all requested vectors. */
1899 break;
1900 else if (err < 0)
1901 vectors = 0; /* Nasty failure, quit now */
1902 else /* err == number of vectors we should try again with */
1903 vectors = err;
1906 if (vectors < vector_threshold) {
1907 /* Can't allocate enough MSI-X interrupts? Oh well.
1908 * This just means we'll go with either a single MSI
1909 * vector or fall back to legacy interrupts.
1911 hw_dbg(&adapter->hw,
1912 "Unable to allocate MSI-X interrupts\n");
1913 kfree(adapter->msix_entries);
1914 adapter->msix_entries = NULL;
1915 } else {
1917 * Adjust for only the vectors we'll use, which is minimum
1918 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1919 * vectors we were allocated.
1921 adapter->num_msix_vectors = vectors;
1926 * ixgbevf_set_num_queues: Allocate queues for device, feature dependent
1927 * @adapter: board private structure to initialize
1929 * This is the top level queue allocation routine. The order here is very
1930 * important, starting with the "most" number of features turned on at once,
1931 * and ending with the smallest set of features. This way large combinations
1932 * can be allocated if they're turned on, and smaller combinations are the
1933 * fallthrough conditions.
1936 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1938 /* Start with base case */
1939 adapter->num_rx_queues = 1;
1940 adapter->num_tx_queues = 1;
1941 adapter->num_rx_pools = adapter->num_rx_queues;
1942 adapter->num_rx_queues_per_pool = 1;
1946 * ixgbevf_alloc_queues - Allocate memory for all rings
1947 * @adapter: board private structure to initialize
1949 * We allocate one ring per queue at run-time since we don't know the
1950 * number of queues at compile-time. The polling_netdev array is
1951 * intended for Multiqueue, but should work fine with a single queue.
1953 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1955 int i;
1957 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1958 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1959 if (!adapter->tx_ring)
1960 goto err_tx_ring_allocation;
1962 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1963 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1964 if (!adapter->rx_ring)
1965 goto err_rx_ring_allocation;
1967 for (i = 0; i < adapter->num_tx_queues; i++) {
1968 adapter->tx_ring[i].count = adapter->tx_ring_count;
1969 adapter->tx_ring[i].queue_index = i;
1970 adapter->tx_ring[i].reg_idx = i;
1973 for (i = 0; i < adapter->num_rx_queues; i++) {
1974 adapter->rx_ring[i].count = adapter->rx_ring_count;
1975 adapter->rx_ring[i].queue_index = i;
1976 adapter->rx_ring[i].reg_idx = i;
1979 return 0;
1981 err_rx_ring_allocation:
1982 kfree(adapter->tx_ring);
1983 err_tx_ring_allocation:
1984 return -ENOMEM;
1988 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1989 * @adapter: board private structure to initialize
1991 * Attempt to configure the interrupts using the best available
1992 * capabilities of the hardware and the kernel.
1994 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1996 int err = 0;
1997 int vector, v_budget;
2000 * It's easy to be greedy for MSI-X vectors, but it really
2001 * doesn't do us much good if we have a lot more vectors
2002 * than CPU's. So let's be conservative and only ask for
2003 * (roughly) twice the number of vectors as there are CPU's.
2005 v_budget = min(adapter->num_rx_queues + adapter->num_tx_queues,
2006 (int)(num_online_cpus() * 2)) + NON_Q_VECTORS;
2008 /* A failure in MSI-X entry allocation isn't fatal, but it does
2009 * mean we disable MSI-X capabilities of the adapter. */
2010 adapter->msix_entries = kcalloc(v_budget,
2011 sizeof(struct msix_entry), GFP_KERNEL);
2012 if (!adapter->msix_entries) {
2013 err = -ENOMEM;
2014 goto out;
2017 for (vector = 0; vector < v_budget; vector++)
2018 adapter->msix_entries[vector].entry = vector;
2020 ixgbevf_acquire_msix_vectors(adapter, v_budget);
2022 out:
2023 return err;
2027 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2028 * @adapter: board private structure to initialize
2030 * We allocate one q_vector per queue interrupt. If allocation fails we
2031 * return -ENOMEM.
2033 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2035 int q_idx, num_q_vectors;
2036 struct ixgbevf_q_vector *q_vector;
2037 int napi_vectors;
2038 int (*poll)(struct napi_struct *, int);
2040 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2041 napi_vectors = adapter->num_rx_queues;
2042 poll = &ixgbevf_clean_rxonly;
2044 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2045 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
2046 if (!q_vector)
2047 goto err_out;
2048 q_vector->adapter = adapter;
2049 q_vector->v_idx = q_idx;
2050 q_vector->eitr = adapter->eitr_param;
2051 if (q_idx < napi_vectors)
2052 netif_napi_add(adapter->netdev, &q_vector->napi,
2053 (*poll), 64);
2054 adapter->q_vector[q_idx] = q_vector;
2057 return 0;
2059 err_out:
2060 while (q_idx) {
2061 q_idx--;
2062 q_vector = adapter->q_vector[q_idx];
2063 netif_napi_del(&q_vector->napi);
2064 kfree(q_vector);
2065 adapter->q_vector[q_idx] = NULL;
2067 return -ENOMEM;
2071 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2072 * @adapter: board private structure to initialize
2074 * This function frees the memory allocated to the q_vectors. In addition if
2075 * NAPI is enabled it will delete any references to the NAPI struct prior
2076 * to freeing the q_vector.
2078 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2080 int q_idx, num_q_vectors;
2081 int napi_vectors;
2083 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2084 napi_vectors = adapter->num_rx_queues;
2086 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2087 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
2089 adapter->q_vector[q_idx] = NULL;
2090 if (q_idx < napi_vectors)
2091 netif_napi_del(&q_vector->napi);
2092 kfree(q_vector);
2097 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2098 * @adapter: board private structure
2101 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2103 pci_disable_msix(adapter->pdev);
2104 kfree(adapter->msix_entries);
2105 adapter->msix_entries = NULL;
2109 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
2110 * @adapter: board private structure to initialize
2113 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2115 int err;
2117 /* Number of supported queues */
2118 ixgbevf_set_num_queues(adapter);
2120 err = ixgbevf_set_interrupt_capability(adapter);
2121 if (err) {
2122 hw_dbg(&adapter->hw,
2123 "Unable to setup interrupt capabilities\n");
2124 goto err_set_interrupt;
2127 err = ixgbevf_alloc_q_vectors(adapter);
2128 if (err) {
2129 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
2130 "vectors\n");
2131 goto err_alloc_q_vectors;
2134 err = ixgbevf_alloc_queues(adapter);
2135 if (err) {
2136 printk(KERN_ERR "Unable to allocate memory for queues\n");
2137 goto err_alloc_queues;
2140 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
2141 "Tx Queue count = %u\n",
2142 (adapter->num_rx_queues > 1) ? "Enabled" :
2143 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2145 set_bit(__IXGBEVF_DOWN, &adapter->state);
2147 return 0;
2148 err_alloc_queues:
2149 ixgbevf_free_q_vectors(adapter);
2150 err_alloc_q_vectors:
2151 ixgbevf_reset_interrupt_capability(adapter);
2152 err_set_interrupt:
2153 return err;
2157 * ixgbevf_sw_init - Initialize general software structures
2158 * (struct ixgbevf_adapter)
2159 * @adapter: board private structure to initialize
2161 * ixgbevf_sw_init initializes the Adapter private data structure.
2162 * Fields are initialized based on PCI device information and
2163 * OS network device settings (MTU size).
2165 static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2167 struct ixgbe_hw *hw = &adapter->hw;
2168 struct pci_dev *pdev = adapter->pdev;
2169 int err;
2171 /* PCI config space info */
2173 hw->vendor_id = pdev->vendor;
2174 hw->device_id = pdev->device;
2175 hw->revision_id = pdev->revision;
2176 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2177 hw->subsystem_device_id = pdev->subsystem_device;
2179 hw->mbx.ops.init_params(hw);
2180 hw->mac.max_tx_queues = MAX_TX_QUEUES;
2181 hw->mac.max_rx_queues = MAX_RX_QUEUES;
2182 err = hw->mac.ops.reset_hw(hw);
2183 if (err) {
2184 dev_info(&pdev->dev,
2185 "PF still in reset state, assigning new address\n");
2186 dev_hw_addr_random(adapter->netdev, hw->mac.addr);
2187 } else {
2188 err = hw->mac.ops.init_hw(hw);
2189 if (err) {
2190 printk(KERN_ERR "init_shared_code failed: %d\n", err);
2191 goto out;
2195 /* Enable dynamic interrupt throttling rates */
2196 adapter->eitr_param = 20000;
2197 adapter->itr_setting = 1;
2199 /* set defaults for eitr in MegaBytes */
2200 adapter->eitr_low = 10;
2201 adapter->eitr_high = 20;
2203 /* set default ring sizes */
2204 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2205 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2207 /* enable rx csum by default */
2208 adapter->flags |= IXGBE_FLAG_RX_CSUM_ENABLED;
2210 set_bit(__IXGBEVF_DOWN, &adapter->state);
2212 out:
2213 return err;
2216 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2218 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2219 if (current_counter < last_counter) \
2220 counter += 0x100000000LL; \
2221 last_counter = current_counter; \
2222 counter &= 0xFFFFFFFF00000000LL; \
2223 counter |= current_counter; \
2226 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2228 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2229 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2230 u64 current_counter = (current_counter_msb << 32) | \
2231 current_counter_lsb; \
2232 if (current_counter < last_counter) \
2233 counter += 0x1000000000LL; \
2234 last_counter = current_counter; \
2235 counter &= 0xFFFFFFF000000000LL; \
2236 counter |= current_counter; \
2239 * ixgbevf_update_stats - Update the board statistics counters.
2240 * @adapter: board private structure
2242 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2244 struct ixgbe_hw *hw = &adapter->hw;
2246 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2247 adapter->stats.vfgprc);
2248 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2249 adapter->stats.vfgptc);
2250 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2251 adapter->stats.last_vfgorc,
2252 adapter->stats.vfgorc);
2253 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2254 adapter->stats.last_vfgotc,
2255 adapter->stats.vfgotc);
2256 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2257 adapter->stats.vfmprc);
2259 /* Fill out the OS statistics structure */
2260 adapter->netdev->stats.multicast = adapter->stats.vfmprc -
2261 adapter->stats.base_vfmprc;
2265 * ixgbevf_watchdog - Timer Call-back
2266 * @data: pointer to adapter cast into an unsigned long
2268 static void ixgbevf_watchdog(unsigned long data)
2270 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2271 struct ixgbe_hw *hw = &adapter->hw;
2272 u64 eics = 0;
2273 int i;
2276 * Do the watchdog outside of interrupt context due to the lovely
2277 * delays that some of the newer hardware requires
2280 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2281 goto watchdog_short_circuit;
2283 /* get one bit for every active tx/rx interrupt vector */
2284 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2285 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2286 if (qv->rxr_count || qv->txr_count)
2287 eics |= (1 << i);
2290 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, (u32)eics);
2292 watchdog_short_circuit:
2293 schedule_work(&adapter->watchdog_task);
2297 * ixgbevf_tx_timeout - Respond to a Tx Hang
2298 * @netdev: network interface device structure
2300 static void ixgbevf_tx_timeout(struct net_device *netdev)
2302 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2304 /* Do the reset outside of interrupt context */
2305 schedule_work(&adapter->reset_task);
2308 static void ixgbevf_reset_task(struct work_struct *work)
2310 struct ixgbevf_adapter *adapter;
2311 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2313 /* If we're already down or resetting, just bail */
2314 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2315 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2316 return;
2318 adapter->tx_timeout_count++;
2320 ixgbevf_reinit_locked(adapter);
2324 * ixgbevf_watchdog_task - worker thread to bring link up
2325 * @work: pointer to work_struct containing our data
2327 static void ixgbevf_watchdog_task(struct work_struct *work)
2329 struct ixgbevf_adapter *adapter = container_of(work,
2330 struct ixgbevf_adapter,
2331 watchdog_task);
2332 struct net_device *netdev = adapter->netdev;
2333 struct ixgbe_hw *hw = &adapter->hw;
2334 u32 link_speed = adapter->link_speed;
2335 bool link_up = adapter->link_up;
2337 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2340 * Always check the link on the watchdog because we have
2341 * no LSC interrupt
2343 if (hw->mac.ops.check_link) {
2344 if ((hw->mac.ops.check_link(hw, &link_speed,
2345 &link_up, false)) != 0) {
2346 adapter->link_up = link_up;
2347 adapter->link_speed = link_speed;
2348 netif_carrier_off(netdev);
2349 netif_tx_stop_all_queues(netdev);
2350 schedule_work(&adapter->reset_task);
2351 goto pf_has_reset;
2353 } else {
2354 /* always assume link is up, if no check link
2355 * function */
2356 link_speed = IXGBE_LINK_SPEED_10GB_FULL;
2357 link_up = true;
2359 adapter->link_up = link_up;
2360 adapter->link_speed = link_speed;
2362 if (link_up) {
2363 if (!netif_carrier_ok(netdev)) {
2364 hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
2365 (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2366 10 : 1);
2367 netif_carrier_on(netdev);
2368 netif_tx_wake_all_queues(netdev);
2370 } else {
2371 adapter->link_up = false;
2372 adapter->link_speed = 0;
2373 if (netif_carrier_ok(netdev)) {
2374 hw_dbg(&adapter->hw, "NIC Link is Down\n");
2375 netif_carrier_off(netdev);
2376 netif_tx_stop_all_queues(netdev);
2380 ixgbevf_update_stats(adapter);
2382 pf_has_reset:
2383 /* Reset the timer */
2384 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2385 mod_timer(&adapter->watchdog_timer,
2386 round_jiffies(jiffies + (2 * HZ)));
2388 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2392 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2393 * @adapter: board private structure
2394 * @tx_ring: Tx descriptor ring for a specific queue
2396 * Free all transmit software resources
2398 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2399 struct ixgbevf_ring *tx_ring)
2401 struct pci_dev *pdev = adapter->pdev;
2403 ixgbevf_clean_tx_ring(adapter, tx_ring);
2405 vfree(tx_ring->tx_buffer_info);
2406 tx_ring->tx_buffer_info = NULL;
2408 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2409 tx_ring->dma);
2411 tx_ring->desc = NULL;
2415 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2416 * @adapter: board private structure
2418 * Free all transmit software resources
2420 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2422 int i;
2424 for (i = 0; i < adapter->num_tx_queues; i++)
2425 if (adapter->tx_ring[i].desc)
2426 ixgbevf_free_tx_resources(adapter,
2427 &adapter->tx_ring[i]);
2432 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2433 * @adapter: board private structure
2434 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2436 * Return 0 on success, negative on failure
2438 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2439 struct ixgbevf_ring *tx_ring)
2441 struct pci_dev *pdev = adapter->pdev;
2442 int size;
2444 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2445 tx_ring->tx_buffer_info = vzalloc(size);
2446 if (!tx_ring->tx_buffer_info)
2447 goto err;
2449 /* round up to nearest 4K */
2450 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2451 tx_ring->size = ALIGN(tx_ring->size, 4096);
2453 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2454 &tx_ring->dma, GFP_KERNEL);
2455 if (!tx_ring->desc)
2456 goto err;
2458 tx_ring->next_to_use = 0;
2459 tx_ring->next_to_clean = 0;
2460 tx_ring->work_limit = tx_ring->count;
2461 return 0;
2463 err:
2464 vfree(tx_ring->tx_buffer_info);
2465 tx_ring->tx_buffer_info = NULL;
2466 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2467 "descriptor ring\n");
2468 return -ENOMEM;
2472 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2473 * @adapter: board private structure
2475 * If this function returns with an error, then it's possible one or
2476 * more of the rings is populated (while the rest are not). It is the
2477 * callers duty to clean those orphaned rings.
2479 * Return 0 on success, negative on failure
2481 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2483 int i, err = 0;
2485 for (i = 0; i < adapter->num_tx_queues; i++) {
2486 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2487 if (!err)
2488 continue;
2489 hw_dbg(&adapter->hw,
2490 "Allocation for Tx Queue %u failed\n", i);
2491 break;
2494 return err;
2498 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2499 * @adapter: board private structure
2500 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2502 * Returns 0 on success, negative on failure
2504 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2505 struct ixgbevf_ring *rx_ring)
2507 struct pci_dev *pdev = adapter->pdev;
2508 int size;
2510 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2511 rx_ring->rx_buffer_info = vzalloc(size);
2512 if (!rx_ring->rx_buffer_info) {
2513 hw_dbg(&adapter->hw,
2514 "Unable to vmalloc buffer memory for "
2515 "the receive descriptor ring\n");
2516 goto alloc_failed;
2519 /* Round up to nearest 4K */
2520 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2521 rx_ring->size = ALIGN(rx_ring->size, 4096);
2523 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2524 &rx_ring->dma, GFP_KERNEL);
2526 if (!rx_ring->desc) {
2527 hw_dbg(&adapter->hw,
2528 "Unable to allocate memory for "
2529 "the receive descriptor ring\n");
2530 vfree(rx_ring->rx_buffer_info);
2531 rx_ring->rx_buffer_info = NULL;
2532 goto alloc_failed;
2535 rx_ring->next_to_clean = 0;
2536 rx_ring->next_to_use = 0;
2538 return 0;
2539 alloc_failed:
2540 return -ENOMEM;
2544 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2545 * @adapter: board private structure
2547 * If this function returns with an error, then it's possible one or
2548 * more of the rings is populated (while the rest are not). It is the
2549 * callers duty to clean those orphaned rings.
2551 * Return 0 on success, negative on failure
2553 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2555 int i, err = 0;
2557 for (i = 0; i < adapter->num_rx_queues; i++) {
2558 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2559 if (!err)
2560 continue;
2561 hw_dbg(&adapter->hw,
2562 "Allocation for Rx Queue %u failed\n", i);
2563 break;
2565 return err;
2569 * ixgbevf_free_rx_resources - Free Rx Resources
2570 * @adapter: board private structure
2571 * @rx_ring: ring to clean the resources from
2573 * Free all receive software resources
2575 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2576 struct ixgbevf_ring *rx_ring)
2578 struct pci_dev *pdev = adapter->pdev;
2580 ixgbevf_clean_rx_ring(adapter, rx_ring);
2582 vfree(rx_ring->rx_buffer_info);
2583 rx_ring->rx_buffer_info = NULL;
2585 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2586 rx_ring->dma);
2588 rx_ring->desc = NULL;
2592 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2593 * @adapter: board private structure
2595 * Free all receive software resources
2597 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2599 int i;
2601 for (i = 0; i < adapter->num_rx_queues; i++)
2602 if (adapter->rx_ring[i].desc)
2603 ixgbevf_free_rx_resources(adapter,
2604 &adapter->rx_ring[i]);
2608 * ixgbevf_open - Called when a network interface is made active
2609 * @netdev: network interface device structure
2611 * Returns 0 on success, negative value on failure
2613 * The open entry point is called when a network interface is made
2614 * active by the system (IFF_UP). At this point all resources needed
2615 * for transmit and receive operations are allocated, the interrupt
2616 * handler is registered with the OS, the watchdog timer is started,
2617 * and the stack is notified that the interface is ready.
2619 static int ixgbevf_open(struct net_device *netdev)
2621 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2622 struct ixgbe_hw *hw = &adapter->hw;
2623 int err;
2625 /* disallow open during test */
2626 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2627 return -EBUSY;
2629 if (hw->adapter_stopped) {
2630 ixgbevf_reset(adapter);
2631 /* if adapter is still stopped then PF isn't up and
2632 * the vf can't start. */
2633 if (hw->adapter_stopped) {
2634 err = IXGBE_ERR_MBX;
2635 printk(KERN_ERR "Unable to start - perhaps the PF"
2636 " Driver isn't up yet\n");
2637 goto err_setup_reset;
2641 /* allocate transmit descriptors */
2642 err = ixgbevf_setup_all_tx_resources(adapter);
2643 if (err)
2644 goto err_setup_tx;
2646 /* allocate receive descriptors */
2647 err = ixgbevf_setup_all_rx_resources(adapter);
2648 if (err)
2649 goto err_setup_rx;
2651 ixgbevf_configure(adapter);
2654 * Map the Tx/Rx rings to the vectors we were allotted.
2655 * if request_irq will be called in this function map_rings
2656 * must be called *before* up_complete
2658 ixgbevf_map_rings_to_vectors(adapter);
2660 err = ixgbevf_up_complete(adapter);
2661 if (err)
2662 goto err_up;
2664 /* clear any pending interrupts, may auto mask */
2665 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2666 err = ixgbevf_request_irq(adapter);
2667 if (err)
2668 goto err_req_irq;
2670 ixgbevf_irq_enable(adapter, true, true);
2672 return 0;
2674 err_req_irq:
2675 ixgbevf_down(adapter);
2676 err_up:
2677 ixgbevf_free_irq(adapter);
2678 err_setup_rx:
2679 ixgbevf_free_all_rx_resources(adapter);
2680 err_setup_tx:
2681 ixgbevf_free_all_tx_resources(adapter);
2682 ixgbevf_reset(adapter);
2684 err_setup_reset:
2686 return err;
2690 * ixgbevf_close - Disables a network interface
2691 * @netdev: network interface device structure
2693 * Returns 0, this is not allowed to fail
2695 * The close entry point is called when an interface is de-activated
2696 * by the OS. The hardware is still under the drivers control, but
2697 * needs to be disabled. A global MAC reset is issued to stop the
2698 * hardware, and all transmit and receive resources are freed.
2700 static int ixgbevf_close(struct net_device *netdev)
2702 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2704 ixgbevf_down(adapter);
2705 ixgbevf_free_irq(adapter);
2707 ixgbevf_free_all_tx_resources(adapter);
2708 ixgbevf_free_all_rx_resources(adapter);
2710 return 0;
2713 static int ixgbevf_tso(struct ixgbevf_adapter *adapter,
2714 struct ixgbevf_ring *tx_ring,
2715 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2717 struct ixgbe_adv_tx_context_desc *context_desc;
2718 unsigned int i;
2719 int err;
2720 struct ixgbevf_tx_buffer *tx_buffer_info;
2721 u32 vlan_macip_lens = 0, type_tucmd_mlhl;
2722 u32 mss_l4len_idx, l4len;
2724 if (skb_is_gso(skb)) {
2725 if (skb_header_cloned(skb)) {
2726 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2727 if (err)
2728 return err;
2730 l4len = tcp_hdrlen(skb);
2731 *hdr_len += l4len;
2733 if (skb->protocol == htons(ETH_P_IP)) {
2734 struct iphdr *iph = ip_hdr(skb);
2735 iph->tot_len = 0;
2736 iph->check = 0;
2737 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2738 iph->daddr, 0,
2739 IPPROTO_TCP,
2741 adapter->hw_tso_ctxt++;
2742 } else if (skb_is_gso_v6(skb)) {
2743 ipv6_hdr(skb)->payload_len = 0;
2744 tcp_hdr(skb)->check =
2745 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2746 &ipv6_hdr(skb)->daddr,
2747 0, IPPROTO_TCP, 0);
2748 adapter->hw_tso6_ctxt++;
2751 i = tx_ring->next_to_use;
2753 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2754 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2756 /* VLAN MACLEN IPLEN */
2757 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2758 vlan_macip_lens |=
2759 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
2760 vlan_macip_lens |= ((skb_network_offset(skb)) <<
2761 IXGBE_ADVTXD_MACLEN_SHIFT);
2762 *hdr_len += skb_network_offset(skb);
2763 vlan_macip_lens |=
2764 (skb_transport_header(skb) - skb_network_header(skb));
2765 *hdr_len +=
2766 (skb_transport_header(skb) - skb_network_header(skb));
2767 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2768 context_desc->seqnum_seed = 0;
2770 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2771 type_tucmd_mlhl = (IXGBE_TXD_CMD_DEXT |
2772 IXGBE_ADVTXD_DTYP_CTXT);
2774 if (skb->protocol == htons(ETH_P_IP))
2775 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
2776 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2777 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2779 /* MSS L4LEN IDX */
2780 mss_l4len_idx =
2781 (skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT);
2782 mss_l4len_idx |= (l4len << IXGBE_ADVTXD_L4LEN_SHIFT);
2783 /* use index 1 for TSO */
2784 mss_l4len_idx |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2785 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2787 tx_buffer_info->time_stamp = jiffies;
2788 tx_buffer_info->next_to_watch = i;
2790 i++;
2791 if (i == tx_ring->count)
2792 i = 0;
2793 tx_ring->next_to_use = i;
2795 return true;
2798 return false;
2801 static bool ixgbevf_tx_csum(struct ixgbevf_adapter *adapter,
2802 struct ixgbevf_ring *tx_ring,
2803 struct sk_buff *skb, u32 tx_flags)
2805 struct ixgbe_adv_tx_context_desc *context_desc;
2806 unsigned int i;
2807 struct ixgbevf_tx_buffer *tx_buffer_info;
2808 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
2810 if (skb->ip_summed == CHECKSUM_PARTIAL ||
2811 (tx_flags & IXGBE_TX_FLAGS_VLAN)) {
2812 i = tx_ring->next_to_use;
2813 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2814 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2816 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2817 vlan_macip_lens |= (tx_flags &
2818 IXGBE_TX_FLAGS_VLAN_MASK);
2819 vlan_macip_lens |= (skb_network_offset(skb) <<
2820 IXGBE_ADVTXD_MACLEN_SHIFT);
2821 if (skb->ip_summed == CHECKSUM_PARTIAL)
2822 vlan_macip_lens |= (skb_transport_header(skb) -
2823 skb_network_header(skb));
2825 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2826 context_desc->seqnum_seed = 0;
2828 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
2829 IXGBE_ADVTXD_DTYP_CTXT);
2831 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2832 switch (skb->protocol) {
2833 case __constant_htons(ETH_P_IP):
2834 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
2835 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2836 type_tucmd_mlhl |=
2837 IXGBE_ADVTXD_TUCMD_L4T_TCP;
2838 break;
2839 case __constant_htons(ETH_P_IPV6):
2840 /* XXX what about other V6 headers?? */
2841 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2842 type_tucmd_mlhl |=
2843 IXGBE_ADVTXD_TUCMD_L4T_TCP;
2844 break;
2845 default:
2846 if (unlikely(net_ratelimit())) {
2847 printk(KERN_WARNING
2848 "partial checksum but "
2849 "proto=%x!\n",
2850 skb->protocol);
2852 break;
2856 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2857 /* use index zero for tx checksum offload */
2858 context_desc->mss_l4len_idx = 0;
2860 tx_buffer_info->time_stamp = jiffies;
2861 tx_buffer_info->next_to_watch = i;
2863 adapter->hw_csum_tx_good++;
2864 i++;
2865 if (i == tx_ring->count)
2866 i = 0;
2867 tx_ring->next_to_use = i;
2869 return true;
2872 return false;
2875 static int ixgbevf_tx_map(struct ixgbevf_adapter *adapter,
2876 struct ixgbevf_ring *tx_ring,
2877 struct sk_buff *skb, u32 tx_flags,
2878 unsigned int first)
2880 struct pci_dev *pdev = adapter->pdev;
2881 struct ixgbevf_tx_buffer *tx_buffer_info;
2882 unsigned int len;
2883 unsigned int total = skb->len;
2884 unsigned int offset = 0, size;
2885 int count = 0;
2886 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2887 unsigned int f;
2888 int i;
2890 i = tx_ring->next_to_use;
2892 len = min(skb_headlen(skb), total);
2893 while (len) {
2894 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2895 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2897 tx_buffer_info->length = size;
2898 tx_buffer_info->mapped_as_page = false;
2899 tx_buffer_info->dma = dma_map_single(&adapter->pdev->dev,
2900 skb->data + offset,
2901 size, DMA_TO_DEVICE);
2902 if (dma_mapping_error(&pdev->dev, tx_buffer_info->dma))
2903 goto dma_error;
2904 tx_buffer_info->time_stamp = jiffies;
2905 tx_buffer_info->next_to_watch = i;
2907 len -= size;
2908 total -= size;
2909 offset += size;
2910 count++;
2911 i++;
2912 if (i == tx_ring->count)
2913 i = 0;
2916 for (f = 0; f < nr_frags; f++) {
2917 struct skb_frag_struct *frag;
2919 frag = &skb_shinfo(skb)->frags[f];
2920 len = min((unsigned int)frag->size, total);
2921 offset = frag->page_offset;
2923 while (len) {
2924 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2925 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2927 tx_buffer_info->length = size;
2928 tx_buffer_info->dma = dma_map_page(&adapter->pdev->dev,
2929 frag->page,
2930 offset,
2931 size,
2932 DMA_TO_DEVICE);
2933 tx_buffer_info->mapped_as_page = true;
2934 if (dma_mapping_error(&pdev->dev, tx_buffer_info->dma))
2935 goto dma_error;
2936 tx_buffer_info->time_stamp = jiffies;
2937 tx_buffer_info->next_to_watch = i;
2939 len -= size;
2940 total -= size;
2941 offset += size;
2942 count++;
2943 i++;
2944 if (i == tx_ring->count)
2945 i = 0;
2947 if (total == 0)
2948 break;
2951 if (i == 0)
2952 i = tx_ring->count - 1;
2953 else
2954 i = i - 1;
2955 tx_ring->tx_buffer_info[i].skb = skb;
2956 tx_ring->tx_buffer_info[first].next_to_watch = i;
2958 return count;
2960 dma_error:
2961 dev_err(&pdev->dev, "TX DMA map failed\n");
2963 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2964 tx_buffer_info->dma = 0;
2965 tx_buffer_info->time_stamp = 0;
2966 tx_buffer_info->next_to_watch = 0;
2967 count--;
2969 /* clear timestamp and dma mappings for remaining portion of packet */
2970 while (count >= 0) {
2971 count--;
2972 i--;
2973 if (i < 0)
2974 i += tx_ring->count;
2975 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2976 ixgbevf_unmap_and_free_tx_resource(adapter, tx_buffer_info);
2979 return count;
2982 static void ixgbevf_tx_queue(struct ixgbevf_adapter *adapter,
2983 struct ixgbevf_ring *tx_ring, int tx_flags,
2984 int count, u32 paylen, u8 hdr_len)
2986 union ixgbe_adv_tx_desc *tx_desc = NULL;
2987 struct ixgbevf_tx_buffer *tx_buffer_info;
2988 u32 olinfo_status = 0, cmd_type_len = 0;
2989 unsigned int i;
2991 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2993 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2995 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2997 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2998 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
3000 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
3001 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
3003 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
3004 IXGBE_ADVTXD_POPTS_SHIFT;
3006 /* use index 1 context for tso */
3007 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
3008 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3009 olinfo_status |= IXGBE_TXD_POPTS_IXSM <<
3010 IXGBE_ADVTXD_POPTS_SHIFT;
3012 } else if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3013 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
3014 IXGBE_ADVTXD_POPTS_SHIFT;
3016 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
3018 i = tx_ring->next_to_use;
3019 while (count--) {
3020 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3021 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
3022 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
3023 tx_desc->read.cmd_type_len =
3024 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
3025 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
3026 i++;
3027 if (i == tx_ring->count)
3028 i = 0;
3031 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
3034 * Force memory writes to complete before letting h/w
3035 * know there are new descriptors to fetch. (Only
3036 * applicable for weak-ordered memory model archs,
3037 * such as IA-64).
3039 wmb();
3041 tx_ring->next_to_use = i;
3042 writel(i, adapter->hw.hw_addr + tx_ring->tail);
3045 static int __ixgbevf_maybe_stop_tx(struct net_device *netdev,
3046 struct ixgbevf_ring *tx_ring, int size)
3048 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3050 netif_stop_subqueue(netdev, tx_ring->queue_index);
3051 /* Herbert's original patch had:
3052 * smp_mb__after_netif_stop_queue();
3053 * but since that doesn't exist yet, just open code it. */
3054 smp_mb();
3056 /* We need to check again in a case another CPU has just
3057 * made room available. */
3058 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
3059 return -EBUSY;
3061 /* A reprieve! - use start_queue because it doesn't call schedule */
3062 netif_start_subqueue(netdev, tx_ring->queue_index);
3063 ++adapter->restart_queue;
3064 return 0;
3067 static int ixgbevf_maybe_stop_tx(struct net_device *netdev,
3068 struct ixgbevf_ring *tx_ring, int size)
3070 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
3071 return 0;
3072 return __ixgbevf_maybe_stop_tx(netdev, tx_ring, size);
3075 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3077 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3078 struct ixgbevf_ring *tx_ring;
3079 unsigned int first;
3080 unsigned int tx_flags = 0;
3081 u8 hdr_len = 0;
3082 int r_idx = 0, tso;
3083 int count = 0;
3085 unsigned int f;
3087 tx_ring = &adapter->tx_ring[r_idx];
3089 if (vlan_tx_tag_present(skb)) {
3090 tx_flags |= vlan_tx_tag_get(skb);
3091 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3092 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3095 /* four things can cause us to need a context descriptor */
3096 if (skb_is_gso(skb) ||
3097 (skb->ip_summed == CHECKSUM_PARTIAL) ||
3098 (tx_flags & IXGBE_TX_FLAGS_VLAN))
3099 count++;
3101 count += TXD_USE_COUNT(skb_headlen(skb));
3102 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3103 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3105 if (ixgbevf_maybe_stop_tx(netdev, tx_ring, count)) {
3106 adapter->tx_busy++;
3107 return NETDEV_TX_BUSY;
3110 first = tx_ring->next_to_use;
3112 if (skb->protocol == htons(ETH_P_IP))
3113 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3114 tso = ixgbevf_tso(adapter, tx_ring, skb, tx_flags, &hdr_len);
3115 if (tso < 0) {
3116 dev_kfree_skb_any(skb);
3117 return NETDEV_TX_OK;
3120 if (tso)
3121 tx_flags |= IXGBE_TX_FLAGS_TSO;
3122 else if (ixgbevf_tx_csum(adapter, tx_ring, skb, tx_flags) &&
3123 (skb->ip_summed == CHECKSUM_PARTIAL))
3124 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3126 ixgbevf_tx_queue(adapter, tx_ring, tx_flags,
3127 ixgbevf_tx_map(adapter, tx_ring, skb, tx_flags, first),
3128 skb->len, hdr_len);
3130 ixgbevf_maybe_stop_tx(netdev, tx_ring, DESC_NEEDED);
3132 return NETDEV_TX_OK;
3136 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3137 * @netdev: network interface device structure
3138 * @p: pointer to an address structure
3140 * Returns 0 on success, negative on failure
3142 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3144 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3145 struct ixgbe_hw *hw = &adapter->hw;
3146 struct sockaddr *addr = p;
3148 if (!is_valid_ether_addr(addr->sa_data))
3149 return -EADDRNOTAVAIL;
3151 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3152 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3154 if (hw->mac.ops.set_rar)
3155 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
3157 return 0;
3161 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3162 * @netdev: network interface device structure
3163 * @new_mtu: new value for maximum frame size
3165 * Returns 0 on success, negative on failure
3167 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3169 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3170 struct ixgbe_hw *hw = &adapter->hw;
3171 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3172 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
3173 u32 msg[2];
3175 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3176 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3178 /* MTU < 68 is an error and causes problems on some kernels */
3179 if ((new_mtu < 68) || (max_frame > max_possible_frame))
3180 return -EINVAL;
3182 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3183 netdev->mtu, new_mtu);
3184 /* must set new MTU before calling down or up */
3185 netdev->mtu = new_mtu;
3187 msg[0] = IXGBE_VF_SET_LPE;
3188 msg[1] = max_frame;
3189 hw->mbx.ops.write_posted(hw, msg, 2);
3191 if (netif_running(netdev))
3192 ixgbevf_reinit_locked(adapter);
3194 return 0;
3197 static void ixgbevf_shutdown(struct pci_dev *pdev)
3199 struct net_device *netdev = pci_get_drvdata(pdev);
3200 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3202 netif_device_detach(netdev);
3204 if (netif_running(netdev)) {
3205 ixgbevf_down(adapter);
3206 ixgbevf_free_irq(adapter);
3207 ixgbevf_free_all_tx_resources(adapter);
3208 ixgbevf_free_all_rx_resources(adapter);
3211 #ifdef CONFIG_PM
3212 pci_save_state(pdev);
3213 #endif
3215 pci_disable_device(pdev);
3218 static const struct net_device_ops ixgbe_netdev_ops = {
3219 .ndo_open = ixgbevf_open,
3220 .ndo_stop = ixgbevf_close,
3221 .ndo_start_xmit = ixgbevf_xmit_frame,
3222 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
3223 .ndo_set_multicast_list = ixgbevf_set_rx_mode,
3224 .ndo_validate_addr = eth_validate_addr,
3225 .ndo_set_mac_address = ixgbevf_set_mac,
3226 .ndo_change_mtu = ixgbevf_change_mtu,
3227 .ndo_tx_timeout = ixgbevf_tx_timeout,
3228 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3229 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
3232 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3234 dev->netdev_ops = &ixgbe_netdev_ops;
3235 ixgbevf_set_ethtool_ops(dev);
3236 dev->watchdog_timeo = 5 * HZ;
3240 * ixgbevf_probe - Device Initialization Routine
3241 * @pdev: PCI device information struct
3242 * @ent: entry in ixgbevf_pci_tbl
3244 * Returns 0 on success, negative on failure
3246 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3247 * The OS initialization, configuring of the adapter private structure,
3248 * and a hardware reset occur.
3250 static int __devinit ixgbevf_probe(struct pci_dev *pdev,
3251 const struct pci_device_id *ent)
3253 struct net_device *netdev;
3254 struct ixgbevf_adapter *adapter = NULL;
3255 struct ixgbe_hw *hw = NULL;
3256 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3257 static int cards_found;
3258 int err, pci_using_dac;
3260 err = pci_enable_device(pdev);
3261 if (err)
3262 return err;
3264 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3265 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3266 pci_using_dac = 1;
3267 } else {
3268 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3269 if (err) {
3270 err = dma_set_coherent_mask(&pdev->dev,
3271 DMA_BIT_MASK(32));
3272 if (err) {
3273 dev_err(&pdev->dev, "No usable DMA "
3274 "configuration, aborting\n");
3275 goto err_dma;
3278 pci_using_dac = 0;
3281 err = pci_request_regions(pdev, ixgbevf_driver_name);
3282 if (err) {
3283 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3284 goto err_pci_reg;
3287 pci_set_master(pdev);
3289 #ifdef HAVE_TX_MQ
3290 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3291 MAX_TX_QUEUES);
3292 #else
3293 netdev = alloc_etherdev(sizeof(struct ixgbevf_adapter));
3294 #endif
3295 if (!netdev) {
3296 err = -ENOMEM;
3297 goto err_alloc_etherdev;
3300 SET_NETDEV_DEV(netdev, &pdev->dev);
3302 pci_set_drvdata(pdev, netdev);
3303 adapter = netdev_priv(netdev);
3305 adapter->netdev = netdev;
3306 adapter->pdev = pdev;
3307 hw = &adapter->hw;
3308 hw->back = adapter;
3309 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3312 * call save state here in standalone driver because it relies on
3313 * adapter struct to exist, and needs to call netdev_priv
3315 pci_save_state(pdev);
3317 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3318 pci_resource_len(pdev, 0));
3319 if (!hw->hw_addr) {
3320 err = -EIO;
3321 goto err_ioremap;
3324 ixgbevf_assign_netdev_ops(netdev);
3326 adapter->bd_number = cards_found;
3328 /* Setup hw api */
3329 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3330 hw->mac.type = ii->mac;
3332 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3333 sizeof(struct ixgbe_mbx_operations));
3335 adapter->flags &= ~IXGBE_FLAG_RX_PS_CAPABLE;
3336 adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
3337 adapter->flags |= IXGBE_FLAG_RX_1BUF_CAPABLE;
3339 /* setup the private structure */
3340 err = ixgbevf_sw_init(adapter);
3342 netdev->features = NETIF_F_SG |
3343 NETIF_F_IP_CSUM |
3344 NETIF_F_HW_VLAN_TX |
3345 NETIF_F_HW_VLAN_RX |
3346 NETIF_F_HW_VLAN_FILTER;
3348 netdev->features |= NETIF_F_IPV6_CSUM;
3349 netdev->features |= NETIF_F_TSO;
3350 netdev->features |= NETIF_F_TSO6;
3351 netdev->features |= NETIF_F_GRO;
3352 netdev->vlan_features |= NETIF_F_TSO;
3353 netdev->vlan_features |= NETIF_F_TSO6;
3354 netdev->vlan_features |= NETIF_F_IP_CSUM;
3355 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3356 netdev->vlan_features |= NETIF_F_SG;
3358 if (pci_using_dac)
3359 netdev->features |= NETIF_F_HIGHDMA;
3361 /* The HW MAC address was set and/or determined in sw_init */
3362 memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
3363 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3365 if (!is_valid_ether_addr(netdev->dev_addr)) {
3366 printk(KERN_ERR "invalid MAC address\n");
3367 err = -EIO;
3368 goto err_sw_init;
3371 init_timer(&adapter->watchdog_timer);
3372 adapter->watchdog_timer.function = ixgbevf_watchdog;
3373 adapter->watchdog_timer.data = (unsigned long)adapter;
3375 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3376 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3378 err = ixgbevf_init_interrupt_scheme(adapter);
3379 if (err)
3380 goto err_sw_init;
3382 /* pick up the PCI bus settings for reporting later */
3383 if (hw->mac.ops.get_bus_info)
3384 hw->mac.ops.get_bus_info(hw);
3386 strcpy(netdev->name, "eth%d");
3388 err = register_netdev(netdev);
3389 if (err)
3390 goto err_register;
3392 adapter->netdev_registered = true;
3394 netif_carrier_off(netdev);
3396 ixgbevf_init_last_counter_stats(adapter);
3398 /* print the MAC address */
3399 hw_dbg(hw, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
3400 netdev->dev_addr[0],
3401 netdev->dev_addr[1],
3402 netdev->dev_addr[2],
3403 netdev->dev_addr[3],
3404 netdev->dev_addr[4],
3405 netdev->dev_addr[5]);
3407 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3409 hw_dbg(hw, "LRO is disabled\n");
3411 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3412 cards_found++;
3413 return 0;
3415 err_register:
3416 err_sw_init:
3417 ixgbevf_reset_interrupt_capability(adapter);
3418 iounmap(hw->hw_addr);
3419 err_ioremap:
3420 free_netdev(netdev);
3421 err_alloc_etherdev:
3422 pci_release_regions(pdev);
3423 err_pci_reg:
3424 err_dma:
3425 pci_disable_device(pdev);
3426 return err;
3430 * ixgbevf_remove - Device Removal Routine
3431 * @pdev: PCI device information struct
3433 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3434 * that it should release a PCI device. The could be caused by a
3435 * Hot-Plug event, or because the driver is going to be removed from
3436 * memory.
3438 static void __devexit ixgbevf_remove(struct pci_dev *pdev)
3440 struct net_device *netdev = pci_get_drvdata(pdev);
3441 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3443 set_bit(__IXGBEVF_DOWN, &adapter->state);
3445 del_timer_sync(&adapter->watchdog_timer);
3447 cancel_work_sync(&adapter->reset_task);
3448 cancel_work_sync(&adapter->watchdog_task);
3450 if (adapter->netdev_registered) {
3451 unregister_netdev(netdev);
3452 adapter->netdev_registered = false;
3455 ixgbevf_reset_interrupt_capability(adapter);
3457 iounmap(adapter->hw.hw_addr);
3458 pci_release_regions(pdev);
3460 hw_dbg(&adapter->hw, "Remove complete\n");
3462 kfree(adapter->tx_ring);
3463 kfree(adapter->rx_ring);
3465 free_netdev(netdev);
3467 pci_disable_device(pdev);
3470 static struct pci_driver ixgbevf_driver = {
3471 .name = ixgbevf_driver_name,
3472 .id_table = ixgbevf_pci_tbl,
3473 .probe = ixgbevf_probe,
3474 .remove = __devexit_p(ixgbevf_remove),
3475 .shutdown = ixgbevf_shutdown,
3479 * ixgbevf_init_module - Driver Registration Routine
3481 * ixgbevf_init_module is the first routine called when the driver is
3482 * loaded. All it does is register with the PCI subsystem.
3484 static int __init ixgbevf_init_module(void)
3486 int ret;
3487 printk(KERN_INFO "ixgbevf: %s - version %s\n", ixgbevf_driver_string,
3488 ixgbevf_driver_version);
3490 printk(KERN_INFO "%s\n", ixgbevf_copyright);
3492 ret = pci_register_driver(&ixgbevf_driver);
3493 return ret;
3496 module_init(ixgbevf_init_module);
3499 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3501 * ixgbevf_exit_module is called just before the driver is removed
3502 * from memory.
3504 static void __exit ixgbevf_exit_module(void)
3506 pci_unregister_driver(&ixgbevf_driver);
3509 #ifdef DEBUG
3511 * ixgbevf_get_hw_dev_name - return device name string
3512 * used by hardware layer to print debugging information
3514 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3516 struct ixgbevf_adapter *adapter = hw->back;
3517 return adapter->netdev->name;
3520 #endif
3521 module_exit(ixgbevf_exit_module);
3523 /* ixgbevf_main.c */