d0c3538271e3baada86cfd38521833b028abe7d4
[linux-2.6.git] / drivers / net / ixgbevf / ixgbevf_main.c
1 /*******************************************************************************
2
3   Intel 82599 Virtual Function driver
4   Copyright(c) 1999 - 2009 Intel Corporation.
5
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.
9
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.
14
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.
18
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21
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
25
26 *******************************************************************************/
27
28
29 /******************************************************************************
30  Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
31 ******************************************************************************/
32 #include <linux/types.h>
33 #include <linux/module.h>
34 #include <linux/pci.h>
35 #include <linux/netdevice.h>
36 #include <linux/vmalloc.h>
37 #include <linux/string.h>
38 #include <linux/in.h>
39 #include <linux/ip.h>
40 #include <linux/tcp.h>
41 #include <linux/ipv6.h>
42 #include <linux/slab.h>
43 #include <net/checksum.h>
44 #include <net/ip6_checksum.h>
45 #include <linux/ethtool.h>
46 #include <linux/if_vlan.h>
47
48 #include "ixgbevf.h"
49
50 char ixgbevf_driver_name[] = "ixgbevf";
51 static const char ixgbevf_driver_string[] =
52         "Intel(R) 82599 Virtual Function";
53
54 #define DRV_VERSION "1.0.0-k0"
55 const char ixgbevf_driver_version[] = DRV_VERSION;
56 static char ixgbevf_copyright[] = "Copyright (c) 2009 Intel Corporation.";
57
58 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
59         [board_82599_vf] = &ixgbevf_vf_info,
60 };
61
62 /* ixgbevf_pci_tbl - PCI Device ID Table
63  *
64  * Wildcard entries (PCI_ANY_ID) should come last
65  * Last entry must be all 0s
66  *
67  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
68  *   Class, Class Mask, private data (not used) }
69  */
70 static struct pci_device_id ixgbevf_pci_tbl[] = {
71         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF),
72         board_82599_vf},
73
74         /* required last entry */
75         {0, }
76 };
77 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
78
79 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
80 MODULE_DESCRIPTION("Intel(R) 82599 Virtual Function Driver");
81 MODULE_LICENSE("GPL");
82 MODULE_VERSION(DRV_VERSION);
83
84 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
85
86 /* forward decls */
87 static void ixgbevf_set_itr_msix(struct ixgbevf_q_vector *q_vector);
88 static void ixgbevf_write_eitr(struct ixgbevf_adapter *adapter, int v_idx,
89                                u32 itr_reg);
90
91 static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
92                                            struct ixgbevf_ring *rx_ring,
93                                            u32 val)
94 {
95         /*
96          * Force memory writes to complete before letting h/w
97          * know there are new descriptors to fetch.  (Only
98          * applicable for weak-ordered memory model archs,
99          * such as IA-64).
100          */
101         wmb();
102         IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
103 }
104
105 /*
106  * ixgbe_set_ivar - set the IVAR registers, mapping interrupt causes to vectors
107  * @adapter: pointer to adapter struct
108  * @direction: 0 for Rx, 1 for Tx, -1 for other causes
109  * @queue: queue to map the corresponding interrupt to
110  * @msix_vector: the vector to map to the corresponding queue
111  *
112  */
113 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
114                              u8 queue, u8 msix_vector)
115 {
116         u32 ivar, index;
117         struct ixgbe_hw *hw = &adapter->hw;
118         if (direction == -1) {
119                 /* other causes */
120                 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
121                 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
122                 ivar &= ~0xFF;
123                 ivar |= msix_vector;
124                 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
125         } else {
126                 /* tx or rx causes */
127                 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
128                 index = ((16 * (queue & 1)) + (8 * direction));
129                 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
130                 ivar &= ~(0xFF << index);
131                 ivar |= (msix_vector << index);
132                 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
133         }
134 }
135
136 static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_adapter *adapter,
137                                                struct ixgbevf_tx_buffer
138                                                *tx_buffer_info)
139 {
140         if (tx_buffer_info->dma) {
141                 if (tx_buffer_info->mapped_as_page)
142                         pci_unmap_page(adapter->pdev,
143                                        tx_buffer_info->dma,
144                                        tx_buffer_info->length,
145                                        PCI_DMA_TODEVICE);
146                 else
147                         pci_unmap_single(adapter->pdev,
148                                          tx_buffer_info->dma,
149                                          tx_buffer_info->length,
150                                          PCI_DMA_TODEVICE);
151                 tx_buffer_info->dma = 0;
152         }
153         if (tx_buffer_info->skb) {
154                 dev_kfree_skb_any(tx_buffer_info->skb);
155                 tx_buffer_info->skb = NULL;
156         }
157         tx_buffer_info->time_stamp = 0;
158         /* tx_buffer_info must be completely set up in the transmit path */
159 }
160
161 static inline bool ixgbevf_check_tx_hang(struct ixgbevf_adapter *adapter,
162                                          struct ixgbevf_ring *tx_ring,
163                                          unsigned int eop)
164 {
165         struct ixgbe_hw *hw = &adapter->hw;
166         u32 head, tail;
167
168         /* Detect a transmit hang in hardware, this serializes the
169          * check with the clearing of time_stamp and movement of eop */
170         head = readl(hw->hw_addr + tx_ring->head);
171         tail = readl(hw->hw_addr + tx_ring->tail);
172         adapter->detect_tx_hung = false;
173         if ((head != tail) &&
174             tx_ring->tx_buffer_info[eop].time_stamp &&
175             time_after(jiffies, tx_ring->tx_buffer_info[eop].time_stamp + HZ)) {
176                 /* detected Tx unit hang */
177                 union ixgbe_adv_tx_desc *tx_desc;
178                 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
179                 printk(KERN_ERR "Detected Tx Unit Hang\n"
180                        "  Tx Queue             <%d>\n"
181                        "  TDH, TDT             <%x>, <%x>\n"
182                        "  next_to_use          <%x>\n"
183                        "  next_to_clean        <%x>\n"
184                        "tx_buffer_info[next_to_clean]\n"
185                        "  time_stamp           <%lx>\n"
186                        "  jiffies              <%lx>\n",
187                        tx_ring->queue_index,
188                        head, tail,
189                        tx_ring->next_to_use, eop,
190                        tx_ring->tx_buffer_info[eop].time_stamp, jiffies);
191                 return true;
192         }
193
194         return false;
195 }
196
197 #define IXGBE_MAX_TXD_PWR       14
198 #define IXGBE_MAX_DATA_PER_TXD  (1 << IXGBE_MAX_TXD_PWR)
199
200 /* Tx Descriptors needed, worst case */
201 #define TXD_USE_COUNT(S) (((S) >> IXGBE_MAX_TXD_PWR) + \
202                          (((S) & (IXGBE_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
203 #ifdef MAX_SKB_FRAGS
204 #define DESC_NEEDED (TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD) /* skb->data */ + \
205         MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1)      /* for context */
206 #else
207 #define DESC_NEEDED TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD)
208 #endif
209
210 static void ixgbevf_tx_timeout(struct net_device *netdev);
211
212 /**
213  * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
214  * @adapter: board private structure
215  * @tx_ring: tx ring to clean
216  **/
217 static bool ixgbevf_clean_tx_irq(struct ixgbevf_adapter *adapter,
218                                  struct ixgbevf_ring *tx_ring)
219 {
220         struct net_device *netdev = adapter->netdev;
221         struct ixgbe_hw *hw = &adapter->hw;
222         union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
223         struct ixgbevf_tx_buffer *tx_buffer_info;
224         unsigned int i, eop, count = 0;
225         unsigned int total_bytes = 0, total_packets = 0;
226
227         i = tx_ring->next_to_clean;
228         eop = tx_ring->tx_buffer_info[i].next_to_watch;
229         eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
230
231         while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
232                (count < tx_ring->work_limit)) {
233                 bool cleaned = false;
234                 for ( ; !cleaned; count++) {
235                         struct sk_buff *skb;
236                         tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
237                         tx_buffer_info = &tx_ring->tx_buffer_info[i];
238                         cleaned = (i == eop);
239                         skb = tx_buffer_info->skb;
240
241                         if (cleaned && skb) {
242                                 unsigned int segs, bytecount;
243
244                                 /* gso_segs is currently only valid for tcp */
245                                 segs = skb_shinfo(skb)->gso_segs ?: 1;
246                                 /* multiply data chunks by size of headers */
247                                 bytecount = ((segs - 1) * skb_headlen(skb)) +
248                                             skb->len;
249                                 total_packets += segs;
250                                 total_bytes += bytecount;
251                         }
252
253                         ixgbevf_unmap_and_free_tx_resource(adapter,
254                                                            tx_buffer_info);
255
256                         tx_desc->wb.status = 0;
257
258                         i++;
259                         if (i == tx_ring->count)
260                                 i = 0;
261                 }
262
263                 eop = tx_ring->tx_buffer_info[i].next_to_watch;
264                 eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
265         }
266
267         tx_ring->next_to_clean = i;
268
269 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
270         if (unlikely(count && netif_carrier_ok(netdev) &&
271                      (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
272                 /* Make sure that anybody stopping the queue after this
273                  * sees the new next_to_clean.
274                  */
275                 smp_mb();
276 #ifdef HAVE_TX_MQ
277                 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
278                     !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
279                         netif_wake_subqueue(netdev, tx_ring->queue_index);
280                         ++adapter->restart_queue;
281                 }
282 #else
283                 if (netif_queue_stopped(netdev) &&
284                     !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
285                         netif_wake_queue(netdev);
286                         ++adapter->restart_queue;
287                 }
288 #endif
289         }
290
291         if (adapter->detect_tx_hung) {
292                 if (ixgbevf_check_tx_hang(adapter, tx_ring, i)) {
293                         /* schedule immediate reset if we believe we hung */
294                         printk(KERN_INFO
295                                "tx hang %d detected, resetting adapter\n",
296                                adapter->tx_timeout_count + 1);
297                         ixgbevf_tx_timeout(adapter->netdev);
298                 }
299         }
300
301         /* re-arm the interrupt */
302         if ((count >= tx_ring->work_limit) &&
303             (!test_bit(__IXGBEVF_DOWN, &adapter->state))) {
304                 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, tx_ring->v_idx);
305         }
306
307         tx_ring->total_bytes += total_bytes;
308         tx_ring->total_packets += total_packets;
309
310         adapter->net_stats.tx_bytes += total_bytes;
311         adapter->net_stats.tx_packets += total_packets;
312
313         return (count < tx_ring->work_limit);
314 }
315
316 /**
317  * ixgbevf_receive_skb - Send a completed packet up the stack
318  * @q_vector: structure containing interrupt and ring information
319  * @skb: packet to send up
320  * @status: hardware indication of status of receive
321  * @rx_ring: rx descriptor ring (for a specific queue) to setup
322  * @rx_desc: rx descriptor
323  **/
324 static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
325                                 struct sk_buff *skb, u8 status,
326                                 struct ixgbevf_ring *ring,
327                                 union ixgbe_adv_rx_desc *rx_desc)
328 {
329         struct ixgbevf_adapter *adapter = q_vector->adapter;
330         bool is_vlan = (status & IXGBE_RXD_STAT_VP);
331         u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
332         int ret;
333
334         if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL)) {
335                 if (adapter->vlgrp && is_vlan)
336                         vlan_gro_receive(&q_vector->napi,
337                                          adapter->vlgrp,
338                                          tag, skb);
339                 else
340                         napi_gro_receive(&q_vector->napi, skb);
341         } else {
342                 if (adapter->vlgrp && is_vlan)
343                         ret = vlan_hwaccel_rx(skb, adapter->vlgrp, tag);
344                 else
345                         ret = netif_rx(skb);
346         }
347 }
348
349 /**
350  * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
351  * @adapter: address of board private structure
352  * @status_err: hardware indication of status of receive
353  * @skb: skb currently being received and modified
354  **/
355 static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
356                                        u32 status_err, struct sk_buff *skb)
357 {
358         skb->ip_summed = CHECKSUM_NONE;
359
360         /* Rx csum disabled */
361         if (!(adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED))
362                 return;
363
364         /* if IP and error */
365         if ((status_err & IXGBE_RXD_STAT_IPCS) &&
366             (status_err & IXGBE_RXDADV_ERR_IPE)) {
367                 adapter->hw_csum_rx_error++;
368                 return;
369         }
370
371         if (!(status_err & IXGBE_RXD_STAT_L4CS))
372                 return;
373
374         if (status_err & IXGBE_RXDADV_ERR_TCPE) {
375                 adapter->hw_csum_rx_error++;
376                 return;
377         }
378
379         /* It must be a TCP or UDP packet with a valid checksum */
380         skb->ip_summed = CHECKSUM_UNNECESSARY;
381         adapter->hw_csum_rx_good++;
382 }
383
384 /**
385  * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
386  * @adapter: address of board private structure
387  **/
388 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
389                                      struct ixgbevf_ring *rx_ring,
390                                      int cleaned_count)
391 {
392         struct pci_dev *pdev = adapter->pdev;
393         union ixgbe_adv_rx_desc *rx_desc;
394         struct ixgbevf_rx_buffer *bi;
395         struct sk_buff *skb;
396         unsigned int i;
397         unsigned int bufsz = rx_ring->rx_buf_len + NET_IP_ALIGN;
398
399         i = rx_ring->next_to_use;
400         bi = &rx_ring->rx_buffer_info[i];
401
402         while (cleaned_count--) {
403                 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
404
405                 if (!bi->page_dma &&
406                     (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED)) {
407                         if (!bi->page) {
408                                 bi->page = netdev_alloc_page(adapter->netdev);
409                                 if (!bi->page) {
410                                         adapter->alloc_rx_page_failed++;
411                                         goto no_buffers;
412                                 }
413                                 bi->page_offset = 0;
414                         } else {
415                                 /* use a half page if we're re-using */
416                                 bi->page_offset ^= (PAGE_SIZE / 2);
417                         }
418
419                         bi->page_dma = pci_map_page(pdev, bi->page,
420                                                     bi->page_offset,
421                                                     (PAGE_SIZE / 2),
422                                                     PCI_DMA_FROMDEVICE);
423                 }
424
425                 skb = bi->skb;
426                 if (!skb) {
427                         skb = netdev_alloc_skb(adapter->netdev,
428                                                                bufsz);
429
430                         if (!skb) {
431                                 adapter->alloc_rx_buff_failed++;
432                                 goto no_buffers;
433                         }
434
435                         /*
436                          * Make buffer alignment 2 beyond a 16 byte boundary
437                          * this will result in a 16 byte aligned IP header after
438                          * the 14 byte MAC header is removed
439                          */
440                         skb_reserve(skb, NET_IP_ALIGN);
441
442                         bi->skb = skb;
443                 }
444                 if (!bi->dma) {
445                         bi->dma = pci_map_single(pdev, skb->data,
446                                                  rx_ring->rx_buf_len,
447                                                  PCI_DMA_FROMDEVICE);
448                 }
449                 /* Refresh the desc even if buffer_addrs didn't change because
450                  * each write-back erases this info. */
451                 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
452                         rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
453                         rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
454                 } else {
455                         rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
456                 }
457
458                 i++;
459                 if (i == rx_ring->count)
460                         i = 0;
461                 bi = &rx_ring->rx_buffer_info[i];
462         }
463
464 no_buffers:
465         if (rx_ring->next_to_use != i) {
466                 rx_ring->next_to_use = i;
467                 if (i-- == 0)
468                         i = (rx_ring->count - 1);
469
470                 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
471         }
472 }
473
474 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
475                                              u64 qmask)
476 {
477         u32 mask;
478         struct ixgbe_hw *hw = &adapter->hw;
479
480         mask = (qmask & 0xFFFFFFFF);
481         IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
482 }
483
484 static inline u16 ixgbevf_get_hdr_info(union ixgbe_adv_rx_desc *rx_desc)
485 {
486         return rx_desc->wb.lower.lo_dword.hs_rss.hdr_info;
487 }
488
489 static inline u16 ixgbevf_get_pkt_info(union ixgbe_adv_rx_desc *rx_desc)
490 {
491         return rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
492 }
493
494 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
495                                  struct ixgbevf_ring *rx_ring,
496                                  int *work_done, int work_to_do)
497 {
498         struct ixgbevf_adapter *adapter = q_vector->adapter;
499         struct pci_dev *pdev = adapter->pdev;
500         union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
501         struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
502         struct sk_buff *skb;
503         unsigned int i;
504         u32 len, staterr;
505         u16 hdr_info;
506         bool cleaned = false;
507         int cleaned_count = 0;
508         unsigned int total_rx_bytes = 0, total_rx_packets = 0;
509
510         i = rx_ring->next_to_clean;
511         rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
512         staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
513         rx_buffer_info = &rx_ring->rx_buffer_info[i];
514
515         while (staterr & IXGBE_RXD_STAT_DD) {
516                 u32 upper_len = 0;
517                 if (*work_done >= work_to_do)
518                         break;
519                 (*work_done)++;
520
521                 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
522                         hdr_info = le16_to_cpu(ixgbevf_get_hdr_info(rx_desc));
523                         len = (hdr_info & IXGBE_RXDADV_HDRBUFLEN_MASK) >>
524                                IXGBE_RXDADV_HDRBUFLEN_SHIFT;
525                         if (hdr_info & IXGBE_RXDADV_SPH)
526                                 adapter->rx_hdr_split++;
527                         if (len > IXGBEVF_RX_HDR_SIZE)
528                                 len = IXGBEVF_RX_HDR_SIZE;
529                         upper_len = le16_to_cpu(rx_desc->wb.upper.length);
530                 } else {
531                         len = le16_to_cpu(rx_desc->wb.upper.length);
532                 }
533                 cleaned = true;
534                 skb = rx_buffer_info->skb;
535                 prefetch(skb->data - NET_IP_ALIGN);
536                 rx_buffer_info->skb = NULL;
537
538                 if (rx_buffer_info->dma) {
539                         pci_unmap_single(pdev, rx_buffer_info->dma,
540                                          rx_ring->rx_buf_len,
541                                          PCI_DMA_FROMDEVICE);
542                         rx_buffer_info->dma = 0;
543                         skb_put(skb, len);
544                 }
545
546                 if (upper_len) {
547                         pci_unmap_page(pdev, rx_buffer_info->page_dma,
548                                        PAGE_SIZE / 2, PCI_DMA_FROMDEVICE);
549                         rx_buffer_info->page_dma = 0;
550                         skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
551                                            rx_buffer_info->page,
552                                            rx_buffer_info->page_offset,
553                                            upper_len);
554
555                         if ((rx_ring->rx_buf_len > (PAGE_SIZE / 2)) ||
556                             (page_count(rx_buffer_info->page) != 1))
557                                 rx_buffer_info->page = NULL;
558                         else
559                                 get_page(rx_buffer_info->page);
560
561                         skb->len += upper_len;
562                         skb->data_len += upper_len;
563                         skb->truesize += upper_len;
564                 }
565
566                 i++;
567                 if (i == rx_ring->count)
568                         i = 0;
569
570                 next_rxd = IXGBE_RX_DESC_ADV(*rx_ring, i);
571                 prefetch(next_rxd);
572                 cleaned_count++;
573
574                 next_buffer = &rx_ring->rx_buffer_info[i];
575
576                 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
577                         if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
578                                 rx_buffer_info->skb = next_buffer->skb;
579                                 rx_buffer_info->dma = next_buffer->dma;
580                                 next_buffer->skb = skb;
581                                 next_buffer->dma = 0;
582                         } else {
583                                 skb->next = next_buffer->skb;
584                                 skb->next->prev = skb;
585                         }
586                         adapter->non_eop_descs++;
587                         goto next_desc;
588                 }
589
590                 /* ERR_MASK will only have valid bits if EOP set */
591                 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
592                         dev_kfree_skb_irq(skb);
593                         goto next_desc;
594                 }
595
596                 ixgbevf_rx_checksum(adapter, staterr, skb);
597
598                 /* probably a little skewed due to removing CRC */
599                 total_rx_bytes += skb->len;
600                 total_rx_packets++;
601
602                 /*
603                  * Work around issue of some types of VM to VM loop back
604                  * packets not getting split correctly
605                  */
606                 if (staterr & IXGBE_RXD_STAT_LB) {
607                         u32 header_fixup_len = skb->len - skb->data_len;
608                         if (header_fixup_len < 14)
609                                 skb_push(skb, header_fixup_len);
610                 }
611                 skb->protocol = eth_type_trans(skb, adapter->netdev);
612
613                 ixgbevf_receive_skb(q_vector, skb, staterr, rx_ring, rx_desc);
614                 adapter->netdev->last_rx = jiffies;
615
616 next_desc:
617                 rx_desc->wb.upper.status_error = 0;
618
619                 /* return some buffers to hardware, one at a time is too slow */
620                 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
621                         ixgbevf_alloc_rx_buffers(adapter, rx_ring,
622                                                  cleaned_count);
623                         cleaned_count = 0;
624                 }
625
626                 /* use prefetched values */
627                 rx_desc = next_rxd;
628                 rx_buffer_info = &rx_ring->rx_buffer_info[i];
629
630                 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
631         }
632
633         rx_ring->next_to_clean = i;
634         cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
635
636         if (cleaned_count)
637                 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
638
639         rx_ring->total_packets += total_rx_packets;
640         rx_ring->total_bytes += total_rx_bytes;
641         adapter->net_stats.rx_bytes += total_rx_bytes;
642         adapter->net_stats.rx_packets += total_rx_packets;
643
644         return cleaned;
645 }
646
647 /**
648  * ixgbevf_clean_rxonly - msix (aka one shot) rx clean routine
649  * @napi: napi struct with our devices info in it
650  * @budget: amount of work driver is allowed to do this pass, in packets
651  *
652  * This function is optimized for cleaning one queue only on a single
653  * q_vector!!!
654  **/
655 static int ixgbevf_clean_rxonly(struct napi_struct *napi, int budget)
656 {
657         struct ixgbevf_q_vector *q_vector =
658                 container_of(napi, struct ixgbevf_q_vector, napi);
659         struct ixgbevf_adapter *adapter = q_vector->adapter;
660         struct ixgbevf_ring *rx_ring = NULL;
661         int work_done = 0;
662         long r_idx;
663
664         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
665         rx_ring = &(adapter->rx_ring[r_idx]);
666
667         ixgbevf_clean_rx_irq(q_vector, rx_ring, &work_done, budget);
668
669         /* If all Rx work done, exit the polling mode */
670         if (work_done < budget) {
671                 napi_complete(napi);
672                 if (adapter->itr_setting & 1)
673                         ixgbevf_set_itr_msix(q_vector);
674                 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
675                         ixgbevf_irq_enable_queues(adapter, rx_ring->v_idx);
676         }
677
678         return work_done;
679 }
680
681 /**
682  * ixgbevf_clean_rxonly_many - msix (aka one shot) rx clean routine
683  * @napi: napi struct with our devices info in it
684  * @budget: amount of work driver is allowed to do this pass, in packets
685  *
686  * This function will clean more than one rx queue associated with a
687  * q_vector.
688  **/
689 static int ixgbevf_clean_rxonly_many(struct napi_struct *napi, int budget)
690 {
691         struct ixgbevf_q_vector *q_vector =
692                 container_of(napi, struct ixgbevf_q_vector, napi);
693         struct ixgbevf_adapter *adapter = q_vector->adapter;
694         struct ixgbevf_ring *rx_ring = NULL;
695         int work_done = 0, i;
696         long r_idx;
697         u64 enable_mask = 0;
698
699         /* attempt to distribute budget to each queue fairly, but don't allow
700          * the budget to go below 1 because we'll exit polling */
701         budget /= (q_vector->rxr_count ?: 1);
702         budget = max(budget, 1);
703         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
704         for (i = 0; i < q_vector->rxr_count; i++) {
705                 rx_ring = &(adapter->rx_ring[r_idx]);
706                 ixgbevf_clean_rx_irq(q_vector, rx_ring, &work_done, budget);
707                 enable_mask |= rx_ring->v_idx;
708                 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
709                                       r_idx + 1);
710         }
711
712 #ifndef HAVE_NETDEV_NAPI_LIST
713         if (!netif_running(adapter->netdev))
714                 work_done = 0;
715
716 #endif
717         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
718         rx_ring = &(adapter->rx_ring[r_idx]);
719
720         /* If all Rx work done, exit the polling mode */
721         if (work_done < budget) {
722                 napi_complete(napi);
723                 if (adapter->itr_setting & 1)
724                         ixgbevf_set_itr_msix(q_vector);
725                 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
726                         ixgbevf_irq_enable_queues(adapter, enable_mask);
727         }
728
729         return work_done;
730 }
731
732
733 /**
734  * ixgbevf_configure_msix - Configure MSI-X hardware
735  * @adapter: board private structure
736  *
737  * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
738  * interrupts.
739  **/
740 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
741 {
742         struct ixgbevf_q_vector *q_vector;
743         struct ixgbe_hw *hw = &adapter->hw;
744         int i, j, q_vectors, v_idx, r_idx;
745         u32 mask;
746
747         q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
748
749         /*
750          * Populate the IVAR table and set the ITR values to the
751          * corresponding register.
752          */
753         for (v_idx = 0; v_idx < q_vectors; v_idx++) {
754                 q_vector = adapter->q_vector[v_idx];
755                 /* XXX for_each_set_bit(...) */
756                 r_idx = find_first_bit(q_vector->rxr_idx,
757                                        adapter->num_rx_queues);
758
759                 for (i = 0; i < q_vector->rxr_count; i++) {
760                         j = adapter->rx_ring[r_idx].reg_idx;
761                         ixgbevf_set_ivar(adapter, 0, j, v_idx);
762                         r_idx = find_next_bit(q_vector->rxr_idx,
763                                               adapter->num_rx_queues,
764                                               r_idx + 1);
765                 }
766                 r_idx = find_first_bit(q_vector->txr_idx,
767                                        adapter->num_tx_queues);
768
769                 for (i = 0; i < q_vector->txr_count; i++) {
770                         j = adapter->tx_ring[r_idx].reg_idx;
771                         ixgbevf_set_ivar(adapter, 1, j, v_idx);
772                         r_idx = find_next_bit(q_vector->txr_idx,
773                                               adapter->num_tx_queues,
774                                               r_idx + 1);
775                 }
776
777                 /* if this is a tx only vector halve the interrupt rate */
778                 if (q_vector->txr_count && !q_vector->rxr_count)
779                         q_vector->eitr = (adapter->eitr_param >> 1);
780                 else if (q_vector->rxr_count)
781                         /* rx only */
782                         q_vector->eitr = adapter->eitr_param;
783
784                 ixgbevf_write_eitr(adapter, v_idx, q_vector->eitr);
785         }
786
787         ixgbevf_set_ivar(adapter, -1, 1, v_idx);
788
789         /* set up to autoclear timer, and the vectors */
790         mask = IXGBE_EIMS_ENABLE_MASK;
791         mask &= ~IXGBE_EIMS_OTHER;
792         IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, mask);
793 }
794
795 enum latency_range {
796         lowest_latency = 0,
797         low_latency = 1,
798         bulk_latency = 2,
799         latency_invalid = 255
800 };
801
802 /**
803  * ixgbevf_update_itr - update the dynamic ITR value based on statistics
804  * @adapter: pointer to adapter
805  * @eitr: eitr setting (ints per sec) to give last timeslice
806  * @itr_setting: current throttle rate in ints/second
807  * @packets: the number of packets during this measurement interval
808  * @bytes: the number of bytes during this measurement interval
809  *
810  *      Stores a new ITR value based on packets and byte
811  *      counts during the last interrupt.  The advantage of per interrupt
812  *      computation is faster updates and more accurate ITR for the current
813  *      traffic pattern.  Constants in this function were computed
814  *      based on theoretical maximum wire speed and thresholds were set based
815  *      on testing data as well as attempting to minimize response time
816  *      while increasing bulk throughput.
817  **/
818 static u8 ixgbevf_update_itr(struct ixgbevf_adapter *adapter,
819                              u32 eitr, u8 itr_setting,
820                              int packets, int bytes)
821 {
822         unsigned int retval = itr_setting;
823         u32 timepassed_us;
824         u64 bytes_perint;
825
826         if (packets == 0)
827                 goto update_itr_done;
828
829
830         /* simple throttlerate management
831          *    0-20MB/s lowest (100000 ints/s)
832          *   20-100MB/s low   (20000 ints/s)
833          *  100-1249MB/s bulk (8000 ints/s)
834          */
835         /* what was last interrupt timeslice? */
836         timepassed_us = 1000000/eitr;
837         bytes_perint = bytes / timepassed_us; /* bytes/usec */
838
839         switch (itr_setting) {
840         case lowest_latency:
841                 if (bytes_perint > adapter->eitr_low)
842                         retval = low_latency;
843                 break;
844         case low_latency:
845                 if (bytes_perint > adapter->eitr_high)
846                         retval = bulk_latency;
847                 else if (bytes_perint <= adapter->eitr_low)
848                         retval = lowest_latency;
849                 break;
850         case bulk_latency:
851                 if (bytes_perint <= adapter->eitr_high)
852                         retval = low_latency;
853                 break;
854         }
855
856 update_itr_done:
857         return retval;
858 }
859
860 /**
861  * ixgbevf_write_eitr - write VTEITR register in hardware specific way
862  * @adapter: pointer to adapter struct
863  * @v_idx: vector index into q_vector array
864  * @itr_reg: new value to be written in *register* format, not ints/s
865  *
866  * This function is made to be called by ethtool and by the driver
867  * when it needs to update VTEITR registers at runtime.  Hardware
868  * specific quirks/differences are taken care of here.
869  */
870 static void ixgbevf_write_eitr(struct ixgbevf_adapter *adapter, int v_idx,
871                                u32 itr_reg)
872 {
873         struct ixgbe_hw *hw = &adapter->hw;
874
875         itr_reg = EITR_INTS_PER_SEC_TO_REG(itr_reg);
876
877         /*
878          * set the WDIS bit to not clear the timer bits and cause an
879          * immediate assertion of the interrupt
880          */
881         itr_reg |= IXGBE_EITR_CNT_WDIS;
882
883         IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
884 }
885
886 static void ixgbevf_set_itr_msix(struct ixgbevf_q_vector *q_vector)
887 {
888         struct ixgbevf_adapter *adapter = q_vector->adapter;
889         u32 new_itr;
890         u8 current_itr, ret_itr;
891         int i, r_idx, v_idx = q_vector->v_idx;
892         struct ixgbevf_ring *rx_ring, *tx_ring;
893
894         r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
895         for (i = 0; i < q_vector->txr_count; i++) {
896                 tx_ring = &(adapter->tx_ring[r_idx]);
897                 ret_itr = ixgbevf_update_itr(adapter, q_vector->eitr,
898                                              q_vector->tx_itr,
899                                              tx_ring->total_packets,
900                                              tx_ring->total_bytes);
901                 /* if the result for this queue would decrease interrupt
902                  * rate for this vector then use that result */
903                 q_vector->tx_itr = ((q_vector->tx_itr > ret_itr) ?
904                                     q_vector->tx_itr - 1 : ret_itr);
905                 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
906                                       r_idx + 1);
907         }
908
909         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
910         for (i = 0; i < q_vector->rxr_count; i++) {
911                 rx_ring = &(adapter->rx_ring[r_idx]);
912                 ret_itr = ixgbevf_update_itr(adapter, q_vector->eitr,
913                                              q_vector->rx_itr,
914                                              rx_ring->total_packets,
915                                              rx_ring->total_bytes);
916                 /* if the result for this queue would decrease interrupt
917                  * rate for this vector then use that result */
918                 q_vector->rx_itr = ((q_vector->rx_itr > ret_itr) ?
919                                     q_vector->rx_itr - 1 : ret_itr);
920                 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
921                                       r_idx + 1);
922         }
923
924         current_itr = max(q_vector->rx_itr, q_vector->tx_itr);
925
926         switch (current_itr) {
927         /* counts and packets in update_itr are dependent on these numbers */
928         case lowest_latency:
929                 new_itr = 100000;
930                 break;
931         case low_latency:
932                 new_itr = 20000; /* aka hwitr = ~200 */
933                 break;
934         case bulk_latency:
935         default:
936                 new_itr = 8000;
937                 break;
938         }
939
940         if (new_itr != q_vector->eitr) {
941                 u32 itr_reg;
942
943                 /* save the algorithm value here, not the smoothed one */
944                 q_vector->eitr = new_itr;
945                 /* do an exponential smoothing */
946                 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
947                 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
948                 ixgbevf_write_eitr(adapter, v_idx, itr_reg);
949         }
950
951         return;
952 }
953
954 static irqreturn_t ixgbevf_msix_mbx(int irq, void *data)
955 {
956         struct net_device *netdev = data;
957         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
958         struct ixgbe_hw *hw = &adapter->hw;
959         u32 eicr;
960         u32 msg;
961
962         eicr = IXGBE_READ_REG(hw, IXGBE_VTEICS);
963         IXGBE_WRITE_REG(hw, IXGBE_VTEICR, eicr);
964
965         hw->mbx.ops.read(hw, &msg, 1);
966
967         if ((msg & IXGBE_MBVFICR_VFREQ_MASK) == IXGBE_PF_CONTROL_MSG)
968                 mod_timer(&adapter->watchdog_timer,
969                           round_jiffies(jiffies + 1));
970
971         return IRQ_HANDLED;
972 }
973
974 static irqreturn_t ixgbevf_msix_clean_tx(int irq, void *data)
975 {
976         struct ixgbevf_q_vector *q_vector = data;
977         struct ixgbevf_adapter  *adapter = q_vector->adapter;
978         struct ixgbevf_ring     *tx_ring;
979         int i, r_idx;
980
981         if (!q_vector->txr_count)
982                 return IRQ_HANDLED;
983
984         r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
985         for (i = 0; i < q_vector->txr_count; i++) {
986                 tx_ring = &(adapter->tx_ring[r_idx]);
987                 tx_ring->total_bytes = 0;
988                 tx_ring->total_packets = 0;
989                 ixgbevf_clean_tx_irq(adapter, tx_ring);
990                 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
991                                       r_idx + 1);
992         }
993
994         if (adapter->itr_setting & 1)
995                 ixgbevf_set_itr_msix(q_vector);
996
997         return IRQ_HANDLED;
998 }
999
1000 /**
1001  * ixgbe_msix_clean_rx - single unshared vector rx clean (all queues)
1002  * @irq: unused
1003  * @data: pointer to our q_vector struct for this interrupt vector
1004  **/
1005 static irqreturn_t ixgbevf_msix_clean_rx(int irq, void *data)
1006 {
1007         struct ixgbevf_q_vector *q_vector = data;
1008         struct ixgbevf_adapter  *adapter = q_vector->adapter;
1009         struct ixgbe_hw *hw = &adapter->hw;
1010         struct ixgbevf_ring  *rx_ring;
1011         int r_idx;
1012         int i;
1013
1014         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1015         for (i = 0; i < q_vector->rxr_count; i++) {
1016                 rx_ring = &(adapter->rx_ring[r_idx]);
1017                 rx_ring->total_bytes = 0;
1018                 rx_ring->total_packets = 0;
1019                 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
1020                                       r_idx + 1);
1021         }
1022
1023         if (!q_vector->rxr_count)
1024                 return IRQ_HANDLED;
1025
1026         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1027         rx_ring = &(adapter->rx_ring[r_idx]);
1028         /* disable interrupts on this vector only */
1029         IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, rx_ring->v_idx);
1030         napi_schedule(&q_vector->napi);
1031
1032
1033         return IRQ_HANDLED;
1034 }
1035
1036 static irqreturn_t ixgbevf_msix_clean_many(int irq, void *data)
1037 {
1038         ixgbevf_msix_clean_rx(irq, data);
1039         ixgbevf_msix_clean_tx(irq, data);
1040
1041         return IRQ_HANDLED;
1042 }
1043
1044 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
1045                                      int r_idx)
1046 {
1047         struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
1048
1049         set_bit(r_idx, q_vector->rxr_idx);
1050         q_vector->rxr_count++;
1051         a->rx_ring[r_idx].v_idx = 1 << v_idx;
1052 }
1053
1054 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
1055                                      int t_idx)
1056 {
1057         struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
1058
1059         set_bit(t_idx, q_vector->txr_idx);
1060         q_vector->txr_count++;
1061         a->tx_ring[t_idx].v_idx = 1 << v_idx;
1062 }
1063
1064 /**
1065  * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
1066  * @adapter: board private structure to initialize
1067  *
1068  * This function maps descriptor rings to the queue-specific vectors
1069  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
1070  * one vector per ring/queue, but on a constrained vector budget, we
1071  * group the rings as "efficiently" as possible.  You would add new
1072  * mapping configurations in here.
1073  **/
1074 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
1075 {
1076         int q_vectors;
1077         int v_start = 0;
1078         int rxr_idx = 0, txr_idx = 0;
1079         int rxr_remaining = adapter->num_rx_queues;
1080         int txr_remaining = adapter->num_tx_queues;
1081         int i, j;
1082         int rqpv, tqpv;
1083         int err = 0;
1084
1085         q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1086
1087         /*
1088          * The ideal configuration...
1089          * We have enough vectors to map one per queue.
1090          */
1091         if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
1092                 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
1093                         map_vector_to_rxq(adapter, v_start, rxr_idx);
1094
1095                 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
1096                         map_vector_to_txq(adapter, v_start, txr_idx);
1097                 goto out;
1098         }
1099
1100         /*
1101          * If we don't have enough vectors for a 1-to-1
1102          * mapping, we'll have to group them so there are
1103          * multiple queues per vector.
1104          */
1105         /* Re-adjusting *qpv takes care of the remainder. */
1106         for (i = v_start; i < q_vectors; i++) {
1107                 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
1108                 for (j = 0; j < rqpv; j++) {
1109                         map_vector_to_rxq(adapter, i, rxr_idx);
1110                         rxr_idx++;
1111                         rxr_remaining--;
1112                 }
1113         }
1114         for (i = v_start; i < q_vectors; i++) {
1115                 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
1116                 for (j = 0; j < tqpv; j++) {
1117                         map_vector_to_txq(adapter, i, txr_idx);
1118                         txr_idx++;
1119                         txr_remaining--;
1120                 }
1121         }
1122
1123 out:
1124         return err;
1125 }
1126
1127 /**
1128  * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1129  * @adapter: board private structure
1130  *
1131  * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1132  * interrupts from the kernel.
1133  **/
1134 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1135 {
1136         struct net_device *netdev = adapter->netdev;
1137         irqreturn_t (*handler)(int, void *);
1138         int i, vector, q_vectors, err;
1139         int ri = 0, ti = 0;
1140
1141         /* Decrement for Other and TCP Timer vectors */
1142         q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1143
1144 #define SET_HANDLER(_v) (((_v)->rxr_count && (_v)->txr_count)          \
1145                                           ? &ixgbevf_msix_clean_many : \
1146                           (_v)->rxr_count ? &ixgbevf_msix_clean_rx   : \
1147                           (_v)->txr_count ? &ixgbevf_msix_clean_tx   : \
1148                           NULL)
1149         for (vector = 0; vector < q_vectors; vector++) {
1150                 handler = SET_HANDLER(adapter->q_vector[vector]);
1151
1152                 if (handler == &ixgbevf_msix_clean_rx) {
1153                         sprintf(adapter->name[vector], "%s-%s-%d",
1154                                 netdev->name, "rx", ri++);
1155                 } else if (handler == &ixgbevf_msix_clean_tx) {
1156                         sprintf(adapter->name[vector], "%s-%s-%d",
1157                                 netdev->name, "tx", ti++);
1158                 } else if (handler == &ixgbevf_msix_clean_many) {
1159                         sprintf(adapter->name[vector], "%s-%s-%d",
1160                                 netdev->name, "TxRx", vector);
1161                 } else {
1162                         /* skip this unused q_vector */
1163                         continue;
1164                 }
1165                 err = request_irq(adapter->msix_entries[vector].vector,
1166                                   handler, 0, adapter->name[vector],
1167                                   adapter->q_vector[vector]);
1168                 if (err) {
1169                         hw_dbg(&adapter->hw,
1170                                "request_irq failed for MSIX interrupt "
1171                                "Error: %d\n", err);
1172                         goto free_queue_irqs;
1173                 }
1174         }
1175
1176         sprintf(adapter->name[vector], "%s:mbx", netdev->name);
1177         err = request_irq(adapter->msix_entries[vector].vector,
1178                           &ixgbevf_msix_mbx, 0, adapter->name[vector], netdev);
1179         if (err) {
1180                 hw_dbg(&adapter->hw,
1181                        "request_irq for msix_mbx failed: %d\n", err);
1182                 goto free_queue_irqs;
1183         }
1184
1185         return 0;
1186
1187 free_queue_irqs:
1188         for (i = vector - 1; i >= 0; i--)
1189                 free_irq(adapter->msix_entries[--vector].vector,
1190                          &(adapter->q_vector[i]));
1191         pci_disable_msix(adapter->pdev);
1192         kfree(adapter->msix_entries);
1193         adapter->msix_entries = NULL;
1194         return err;
1195 }
1196
1197 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
1198 {
1199         int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1200
1201         for (i = 0; i < q_vectors; i++) {
1202                 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
1203                 bitmap_zero(q_vector->rxr_idx, MAX_RX_QUEUES);
1204                 bitmap_zero(q_vector->txr_idx, MAX_TX_QUEUES);
1205                 q_vector->rxr_count = 0;
1206                 q_vector->txr_count = 0;
1207                 q_vector->eitr = adapter->eitr_param;
1208         }
1209 }
1210
1211 /**
1212  * ixgbevf_request_irq - initialize interrupts
1213  * @adapter: board private structure
1214  *
1215  * Attempts to configure interrupts using the best available
1216  * capabilities of the hardware and kernel.
1217  **/
1218 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1219 {
1220         int err = 0;
1221
1222         err = ixgbevf_request_msix_irqs(adapter);
1223
1224         if (err)
1225                 hw_dbg(&adapter->hw,
1226                        "request_irq failed, Error %d\n", err);
1227
1228         return err;
1229 }
1230
1231 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1232 {
1233         struct net_device *netdev = adapter->netdev;
1234         int i, q_vectors;
1235
1236         q_vectors = adapter->num_msix_vectors;
1237
1238         i = q_vectors - 1;
1239
1240         free_irq(adapter->msix_entries[i].vector, netdev);
1241         i--;
1242
1243         for (; i >= 0; i--) {
1244                 free_irq(adapter->msix_entries[i].vector,
1245                          adapter->q_vector[i]);
1246         }
1247
1248         ixgbevf_reset_q_vectors(adapter);
1249 }
1250
1251 /**
1252  * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1253  * @adapter: board private structure
1254  **/
1255 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1256 {
1257         int i;
1258         struct ixgbe_hw *hw = &adapter->hw;
1259
1260         IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1261
1262         IXGBE_WRITE_FLUSH(hw);
1263
1264         for (i = 0; i < adapter->num_msix_vectors; i++)
1265                 synchronize_irq(adapter->msix_entries[i].vector);
1266 }
1267
1268 /**
1269  * ixgbevf_irq_enable - Enable default interrupt generation settings
1270  * @adapter: board private structure
1271  **/
1272 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter,
1273                                       bool queues, bool flush)
1274 {
1275         struct ixgbe_hw *hw = &adapter->hw;
1276         u32 mask;
1277         u64 qmask;
1278
1279         mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
1280         qmask = ~0;
1281
1282         IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
1283
1284         if (queues)
1285                 ixgbevf_irq_enable_queues(adapter, qmask);
1286
1287         if (flush)
1288                 IXGBE_WRITE_FLUSH(hw);
1289 }
1290
1291 /**
1292  * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1293  * @adapter: board private structure
1294  *
1295  * Configure the Tx unit of the MAC after a reset.
1296  **/
1297 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1298 {
1299         u64 tdba;
1300         struct ixgbe_hw *hw = &adapter->hw;
1301         u32 i, j, tdlen, txctrl;
1302
1303         /* Setup the HW Tx Head and Tail descriptor pointers */
1304         for (i = 0; i < adapter->num_tx_queues; i++) {
1305                 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1306                 j = ring->reg_idx;
1307                 tdba = ring->dma;
1308                 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1309                 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1310                                 (tdba & DMA_BIT_MASK(32)));
1311                 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1312                 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1313                 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1314                 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1315                 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1316                 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1317                 /* Disable Tx Head Writeback RO bit, since this hoses
1318                  * bookkeeping if things aren't delivered in order.
1319                  */
1320                 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1321                 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1322                 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1323         }
1324 }
1325
1326 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1327
1328 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1329 {
1330         struct ixgbevf_ring *rx_ring;
1331         struct ixgbe_hw *hw = &adapter->hw;
1332         u32 srrctl;
1333
1334         rx_ring = &adapter->rx_ring[index];
1335
1336         srrctl = IXGBE_SRRCTL_DROP_EN;
1337
1338         if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1339                 u16 bufsz = IXGBEVF_RXBUFFER_2048;
1340                 /* grow the amount we can receive on large page machines */
1341                 if (bufsz < (PAGE_SIZE / 2))
1342                         bufsz = (PAGE_SIZE / 2);
1343                 /* cap the bufsz at our largest descriptor size */
1344                 bufsz = min((u16)IXGBEVF_MAX_RXBUFFER, bufsz);
1345
1346                 srrctl |= bufsz >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1347                 srrctl |= IXGBE_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
1348                 srrctl |= ((IXGBEVF_RX_HDR_SIZE <<
1349                            IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
1350                            IXGBE_SRRCTL_BSIZEHDR_MASK);
1351         } else {
1352                 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1353
1354                 if (rx_ring->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
1355                         srrctl |= IXGBEVF_RXBUFFER_2048 >>
1356                                 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1357                 else
1358                         srrctl |= rx_ring->rx_buf_len >>
1359                                 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1360         }
1361         IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1362 }
1363
1364 /**
1365  * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1366  * @adapter: board private structure
1367  *
1368  * Configure the Rx unit of the MAC after a reset.
1369  **/
1370 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1371 {
1372         u64 rdba;
1373         struct ixgbe_hw *hw = &adapter->hw;
1374         struct net_device *netdev = adapter->netdev;
1375         int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1376         int i, j;
1377         u32 rdlen;
1378         int rx_buf_len;
1379
1380         /* Decide whether to use packet split mode or not */
1381         if (netdev->mtu > ETH_DATA_LEN) {
1382                 if (adapter->flags & IXGBE_FLAG_RX_PS_CAPABLE)
1383                         adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
1384                 else
1385                         adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
1386         } else {
1387                 if (adapter->flags & IXGBE_FLAG_RX_1BUF_CAPABLE)
1388                         adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
1389                 else
1390                         adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
1391         }
1392
1393         /* Set the RX buffer length according to the mode */
1394         if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1395                 /* PSRTYPE must be initialized in 82599 */
1396                 u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
1397                         IXGBE_PSRTYPE_UDPHDR |
1398                         IXGBE_PSRTYPE_IPV4HDR |
1399                         IXGBE_PSRTYPE_IPV6HDR |
1400                         IXGBE_PSRTYPE_L2HDR;
1401                 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1402                 rx_buf_len = IXGBEVF_RX_HDR_SIZE;
1403         } else {
1404                 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
1405                 if (netdev->mtu <= ETH_DATA_LEN)
1406                         rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1407                 else
1408                         rx_buf_len = ALIGN(max_frame, 1024);
1409         }
1410
1411         rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1412         /* Setup the HW Rx Head and Tail Descriptor Pointers and
1413          * the Base and Length of the Rx Descriptor Ring */
1414         for (i = 0; i < adapter->num_rx_queues; i++) {
1415                 rdba = adapter->rx_ring[i].dma;
1416                 j = adapter->rx_ring[i].reg_idx;
1417                 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1418                                 (rdba & DMA_BIT_MASK(32)));
1419                 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1420                 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1421                 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1422                 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1423                 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1424                 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1425                 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1426
1427                 ixgbevf_configure_srrctl(adapter, j);
1428         }
1429 }
1430
1431 static void ixgbevf_vlan_rx_register(struct net_device *netdev,
1432                                      struct vlan_group *grp)
1433 {
1434         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1435         struct ixgbe_hw *hw = &adapter->hw;
1436         int i, j;
1437         u32 ctrl;
1438
1439         adapter->vlgrp = grp;
1440
1441         for (i = 0; i < adapter->num_rx_queues; i++) {
1442                 j = adapter->rx_ring[i].reg_idx;
1443                 ctrl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1444                 ctrl |= IXGBE_RXDCTL_VME;
1445                 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), ctrl);
1446         }
1447 }
1448
1449 static void ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1450 {
1451         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1452         struct ixgbe_hw *hw = &adapter->hw;
1453         struct net_device *v_netdev;
1454
1455         /* add VID to filter table */
1456         if (hw->mac.ops.set_vfta)
1457                 hw->mac.ops.set_vfta(hw, vid, 0, true);
1458         /*
1459          * Copy feature flags from netdev to the vlan netdev for this vid.
1460          * This allows things like TSO to bubble down to our vlan device.
1461          */
1462         v_netdev = vlan_group_get_device(adapter->vlgrp, vid);
1463         v_netdev->features |= adapter->netdev->features;
1464         vlan_group_set_device(adapter->vlgrp, vid, v_netdev);
1465 }
1466
1467 static void ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1468 {
1469         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1470         struct ixgbe_hw *hw = &adapter->hw;
1471
1472         if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
1473                 ixgbevf_irq_disable(adapter);
1474
1475         vlan_group_set_device(adapter->vlgrp, vid, NULL);
1476
1477         if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
1478                 ixgbevf_irq_enable(adapter, true, true);
1479
1480         /* remove VID from filter table */
1481         if (hw->mac.ops.set_vfta)
1482                 hw->mac.ops.set_vfta(hw, vid, 0, false);
1483 }
1484
1485 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1486 {
1487         ixgbevf_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1488
1489         if (adapter->vlgrp) {
1490                 u16 vid;
1491                 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1492                         if (!vlan_group_get_device(adapter->vlgrp, vid))
1493                                 continue;
1494                         ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
1495                 }
1496         }
1497 }
1498
1499 /**
1500  * ixgbevf_set_rx_mode - Multicast set
1501  * @netdev: network interface device structure
1502  *
1503  * The set_rx_method entry point is called whenever the multicast address
1504  * list or the network interface flags are updated.  This routine is
1505  * responsible for configuring the hardware for proper multicast mode.
1506  **/
1507 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1508 {
1509         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1510         struct ixgbe_hw *hw = &adapter->hw;
1511
1512         /* reprogram multicast list */
1513         if (hw->mac.ops.update_mc_addr_list)
1514                 hw->mac.ops.update_mc_addr_list(hw, netdev);
1515 }
1516
1517 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1518 {
1519         int q_idx;
1520         struct ixgbevf_q_vector *q_vector;
1521         int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1522
1523         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1524                 struct napi_struct *napi;
1525                 q_vector = adapter->q_vector[q_idx];
1526                 if (!q_vector->rxr_count)
1527                         continue;
1528                 napi = &q_vector->napi;
1529                 if (q_vector->rxr_count > 1)
1530                         napi->poll = &ixgbevf_clean_rxonly_many;
1531
1532                 napi_enable(napi);
1533         }
1534 }
1535
1536 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1537 {
1538         int q_idx;
1539         struct ixgbevf_q_vector *q_vector;
1540         int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1541
1542         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1543                 q_vector = adapter->q_vector[q_idx];
1544                 if (!q_vector->rxr_count)
1545                         continue;
1546                 napi_disable(&q_vector->napi);
1547         }
1548 }
1549
1550 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1551 {
1552         struct net_device *netdev = adapter->netdev;
1553         int i;
1554
1555         ixgbevf_set_rx_mode(netdev);
1556
1557         ixgbevf_restore_vlan(adapter);
1558
1559         ixgbevf_configure_tx(adapter);
1560         ixgbevf_configure_rx(adapter);
1561         for (i = 0; i < adapter->num_rx_queues; i++) {
1562                 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1563                 ixgbevf_alloc_rx_buffers(adapter, ring, ring->count);
1564                 ring->next_to_use = ring->count - 1;
1565                 writel(ring->next_to_use, adapter->hw.hw_addr + ring->tail);
1566         }
1567 }
1568
1569 #define IXGBE_MAX_RX_DESC_POLL 10
1570 static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1571                                                 int rxr)
1572 {
1573         struct ixgbe_hw *hw = &adapter->hw;
1574         int j = adapter->rx_ring[rxr].reg_idx;
1575         int k;
1576
1577         for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1578                 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1579                         break;
1580                 else
1581                         msleep(1);
1582         }
1583         if (k >= IXGBE_MAX_RX_DESC_POLL) {
1584                 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1585                        "not set within the polling period\n", rxr);
1586         }
1587
1588         ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
1589                                 (adapter->rx_ring[rxr].count - 1));
1590 }
1591
1592 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1593 {
1594         /* Only save pre-reset stats if there are some */
1595         if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1596                 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1597                         adapter->stats.base_vfgprc;
1598                 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1599                         adapter->stats.base_vfgptc;
1600                 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1601                         adapter->stats.base_vfgorc;
1602                 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1603                         adapter->stats.base_vfgotc;
1604                 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1605                         adapter->stats.base_vfmprc;
1606         }
1607 }
1608
1609 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1610 {
1611         struct ixgbe_hw *hw = &adapter->hw;
1612
1613         adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1614         adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1615         adapter->stats.last_vfgorc |=
1616                 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1617         adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1618         adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1619         adapter->stats.last_vfgotc |=
1620                 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1621         adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1622
1623         adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1624         adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1625         adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1626         adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1627         adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1628 }
1629
1630 static int ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1631 {
1632         struct net_device *netdev = adapter->netdev;
1633         struct ixgbe_hw *hw = &adapter->hw;
1634         int i, j = 0;
1635         int num_rx_rings = adapter->num_rx_queues;
1636         u32 txdctl, rxdctl;
1637
1638         for (i = 0; i < adapter->num_tx_queues; i++) {
1639                 j = adapter->tx_ring[i].reg_idx;
1640                 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1641                 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1642                 txdctl |= (8 << 16);
1643                 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1644         }
1645
1646         for (i = 0; i < adapter->num_tx_queues; i++) {
1647                 j = adapter->tx_ring[i].reg_idx;
1648                 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1649                 txdctl |= IXGBE_TXDCTL_ENABLE;
1650                 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1651         }
1652
1653         for (i = 0; i < num_rx_rings; i++) {
1654                 j = adapter->rx_ring[i].reg_idx;
1655                 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1656                 rxdctl |= IXGBE_RXDCTL_ENABLE;
1657                 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1658                 ixgbevf_rx_desc_queue_enable(adapter, i);
1659         }
1660
1661         ixgbevf_configure_msix(adapter);
1662
1663         if (hw->mac.ops.set_rar) {
1664                 if (is_valid_ether_addr(hw->mac.addr))
1665                         hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1666                 else
1667                         hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1668         }
1669
1670         clear_bit(__IXGBEVF_DOWN, &adapter->state);
1671         ixgbevf_napi_enable_all(adapter);
1672
1673         /* enable transmits */
1674         netif_tx_start_all_queues(netdev);
1675
1676         ixgbevf_save_reset_stats(adapter);
1677         ixgbevf_init_last_counter_stats(adapter);
1678
1679         /* bring the link up in the watchdog, this could race with our first
1680          * link up interrupt but shouldn't be a problem */
1681         adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
1682         adapter->link_check_timeout = jiffies;
1683         mod_timer(&adapter->watchdog_timer, jiffies);
1684         return 0;
1685 }
1686
1687 int ixgbevf_up(struct ixgbevf_adapter *adapter)
1688 {
1689         int err;
1690         struct ixgbe_hw *hw = &adapter->hw;
1691
1692         ixgbevf_configure(adapter);
1693
1694         err = ixgbevf_up_complete(adapter);
1695
1696         /* clear any pending interrupts, may auto mask */
1697         IXGBE_READ_REG(hw, IXGBE_VTEICR);
1698
1699         ixgbevf_irq_enable(adapter, true, true);
1700
1701         return err;
1702 }
1703
1704 /**
1705  * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1706  * @adapter: board private structure
1707  * @rx_ring: ring to free buffers from
1708  **/
1709 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1710                                   struct ixgbevf_ring *rx_ring)
1711 {
1712         struct pci_dev *pdev = adapter->pdev;
1713         unsigned long size;
1714         unsigned int i;
1715
1716         if (!rx_ring->rx_buffer_info)
1717                 return;
1718
1719         /* Free all the Rx ring sk_buffs */
1720         for (i = 0; i < rx_ring->count; i++) {
1721                 struct ixgbevf_rx_buffer *rx_buffer_info;
1722
1723                 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1724                 if (rx_buffer_info->dma) {
1725                         pci_unmap_single(pdev, rx_buffer_info->dma,
1726                                          rx_ring->rx_buf_len,
1727                                          PCI_DMA_FROMDEVICE);
1728                         rx_buffer_info->dma = 0;
1729                 }
1730                 if (rx_buffer_info->skb) {
1731                         struct sk_buff *skb = rx_buffer_info->skb;
1732                         rx_buffer_info->skb = NULL;
1733                         do {
1734                                 struct sk_buff *this = skb;
1735                                 skb = skb->prev;
1736                                 dev_kfree_skb(this);
1737                         } while (skb);
1738                 }
1739                 if (!rx_buffer_info->page)
1740                         continue;
1741                 pci_unmap_page(pdev, rx_buffer_info->page_dma, PAGE_SIZE / 2,
1742                                PCI_DMA_FROMDEVICE);
1743                 rx_buffer_info->page_dma = 0;
1744                 put_page(rx_buffer_info->page);
1745                 rx_buffer_info->page = NULL;
1746                 rx_buffer_info->page_offset = 0;
1747         }
1748
1749         size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1750         memset(rx_ring->rx_buffer_info, 0, size);
1751
1752         /* Zero out the descriptor ring */
1753         memset(rx_ring->desc, 0, rx_ring->size);
1754
1755         rx_ring->next_to_clean = 0;
1756         rx_ring->next_to_use = 0;
1757
1758         if (rx_ring->head)
1759                 writel(0, adapter->hw.hw_addr + rx_ring->head);
1760         if (rx_ring->tail)
1761                 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1762 }
1763
1764 /**
1765  * ixgbevf_clean_tx_ring - Free Tx Buffers
1766  * @adapter: board private structure
1767  * @tx_ring: ring to be cleaned
1768  **/
1769 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1770                                   struct ixgbevf_ring *tx_ring)
1771 {
1772         struct ixgbevf_tx_buffer *tx_buffer_info;
1773         unsigned long size;
1774         unsigned int i;
1775
1776         if (!tx_ring->tx_buffer_info)
1777                 return;
1778
1779         /* Free all the Tx ring sk_buffs */
1780
1781         for (i = 0; i < tx_ring->count; i++) {
1782                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1783                 ixgbevf_unmap_and_free_tx_resource(adapter, tx_buffer_info);
1784         }
1785
1786         size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1787         memset(tx_ring->tx_buffer_info, 0, size);
1788
1789         memset(tx_ring->desc, 0, tx_ring->size);
1790
1791         tx_ring->next_to_use = 0;
1792         tx_ring->next_to_clean = 0;
1793
1794         if (tx_ring->head)
1795                 writel(0, adapter->hw.hw_addr + tx_ring->head);
1796         if (tx_ring->tail)
1797                 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1798 }
1799
1800 /**
1801  * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1802  * @adapter: board private structure
1803  **/
1804 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1805 {
1806         int i;
1807
1808         for (i = 0; i < adapter->num_rx_queues; i++)
1809                 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1810 }
1811
1812 /**
1813  * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1814  * @adapter: board private structure
1815  **/
1816 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1817 {
1818         int i;
1819
1820         for (i = 0; i < adapter->num_tx_queues; i++)
1821                 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1822 }
1823
1824 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1825 {
1826         struct net_device *netdev = adapter->netdev;
1827         struct ixgbe_hw *hw = &adapter->hw;
1828         u32 txdctl;
1829         int i, j;
1830
1831         /* signal that we are down to the interrupt handler */
1832         set_bit(__IXGBEVF_DOWN, &adapter->state);
1833         /* disable receives */
1834
1835         netif_tx_disable(netdev);
1836
1837         msleep(10);
1838
1839         netif_tx_stop_all_queues(netdev);
1840
1841         ixgbevf_irq_disable(adapter);
1842
1843         ixgbevf_napi_disable_all(adapter);
1844
1845         del_timer_sync(&adapter->watchdog_timer);
1846         /* can't call flush scheduled work here because it can deadlock
1847          * if linkwatch_event tries to acquire the rtnl_lock which we are
1848          * holding */
1849         while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1850                 msleep(1);
1851
1852         /* disable transmits in the hardware now that interrupts are off */
1853         for (i = 0; i < adapter->num_tx_queues; i++) {
1854                 j = adapter->tx_ring[i].reg_idx;
1855                 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1856                 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1857                                 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1858         }
1859
1860         netif_carrier_off(netdev);
1861
1862         if (!pci_channel_offline(adapter->pdev))
1863                 ixgbevf_reset(adapter);
1864
1865         ixgbevf_clean_all_tx_rings(adapter);
1866         ixgbevf_clean_all_rx_rings(adapter);
1867 }
1868
1869 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1870 {
1871         struct ixgbe_hw *hw = &adapter->hw;
1872
1873         WARN_ON(in_interrupt());
1874
1875         while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1876                 msleep(1);
1877
1878         /*
1879          * Check if PF is up before re-init.  If not then skip until
1880          * later when the PF is up and ready to service requests from
1881          * the VF via mailbox.  If the VF is up and running then the
1882          * watchdog task will continue to schedule reset tasks until
1883          * the PF is up and running.
1884          */
1885         if (!hw->mac.ops.reset_hw(hw)) {
1886                 ixgbevf_down(adapter);
1887                 ixgbevf_up(adapter);
1888         }
1889
1890         clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1891 }
1892
1893 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1894 {
1895         struct ixgbe_hw *hw = &adapter->hw;
1896         struct net_device *netdev = adapter->netdev;
1897
1898         if (hw->mac.ops.reset_hw(hw))
1899                 hw_dbg(hw, "PF still resetting\n");
1900         else
1901                 hw->mac.ops.init_hw(hw);
1902
1903         if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1904                 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1905                        netdev->addr_len);
1906                 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1907                        netdev->addr_len);
1908         }
1909 }
1910
1911 static void ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1912                                          int vectors)
1913 {
1914         int err, vector_threshold;
1915
1916         /* We'll want at least 3 (vector_threshold):
1917          * 1) TxQ[0] Cleanup
1918          * 2) RxQ[0] Cleanup
1919          * 3) Other (Link Status Change, etc.)
1920          */
1921         vector_threshold = MIN_MSIX_COUNT;
1922
1923         /* The more we get, the more we will assign to Tx/Rx Cleanup
1924          * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1925          * Right now, we simply care about how many we'll get; we'll
1926          * set them up later while requesting irq's.
1927          */
1928         while (vectors >= vector_threshold) {
1929                 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1930                                       vectors);
1931                 if (!err) /* Success in acquiring all requested vectors. */
1932                         break;
1933                 else if (err < 0)
1934                         vectors = 0; /* Nasty failure, quit now */
1935                 else /* err == number of vectors we should try again with */
1936                         vectors = err;
1937         }
1938
1939         if (vectors < vector_threshold) {
1940                 /* Can't allocate enough MSI-X interrupts?  Oh well.
1941                  * This just means we'll go with either a single MSI
1942                  * vector or fall back to legacy interrupts.
1943                  */
1944                 hw_dbg(&adapter->hw,
1945                        "Unable to allocate MSI-X interrupts\n");
1946                 kfree(adapter->msix_entries);
1947                 adapter->msix_entries = NULL;
1948         } else {
1949                 /*
1950                  * Adjust for only the vectors we'll use, which is minimum
1951                  * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1952                  * vectors we were allocated.
1953                  */
1954                 adapter->num_msix_vectors = vectors;
1955         }
1956 }
1957
1958 /*
1959  * ixgbe_set_num_queues: Allocate queues for device, feature dependant
1960  * @adapter: board private structure to initialize
1961  *
1962  * This is the top level queue allocation routine.  The order here is very
1963  * important, starting with the "most" number of features turned on at once,
1964  * and ending with the smallest set of features.  This way large combinations
1965  * can be allocated if they're turned on, and smaller combinations are the
1966  * fallthrough conditions.
1967  *
1968  **/
1969 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1970 {
1971         /* Start with base case */
1972         adapter->num_rx_queues = 1;
1973         adapter->num_tx_queues = 1;
1974         adapter->num_rx_pools = adapter->num_rx_queues;
1975         adapter->num_rx_queues_per_pool = 1;
1976 }
1977
1978 /**
1979  * ixgbevf_alloc_queues - Allocate memory for all rings
1980  * @adapter: board private structure to initialize
1981  *
1982  * We allocate one ring per queue at run-time since we don't know the
1983  * number of queues at compile-time.  The polling_netdev array is
1984  * intended for Multiqueue, but should work fine with a single queue.
1985  **/
1986 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1987 {
1988         int i;
1989
1990         adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1991                                    sizeof(struct ixgbevf_ring), GFP_KERNEL);
1992         if (!adapter->tx_ring)
1993                 goto err_tx_ring_allocation;
1994
1995         adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1996                                    sizeof(struct ixgbevf_ring), GFP_KERNEL);
1997         if (!adapter->rx_ring)
1998                 goto err_rx_ring_allocation;
1999
2000         for (i = 0; i < adapter->num_tx_queues; i++) {
2001                 adapter->tx_ring[i].count = adapter->tx_ring_count;
2002                 adapter->tx_ring[i].queue_index = i;
2003                 adapter->tx_ring[i].reg_idx = i;
2004         }
2005
2006         for (i = 0; i < adapter->num_rx_queues; i++) {
2007                 adapter->rx_ring[i].count = adapter->rx_ring_count;
2008                 adapter->rx_ring[i].queue_index = i;
2009                 adapter->rx_ring[i].reg_idx = i;
2010         }
2011
2012         return 0;
2013
2014 err_rx_ring_allocation:
2015         kfree(adapter->tx_ring);
2016 err_tx_ring_allocation:
2017         return -ENOMEM;
2018 }
2019
2020 /**
2021  * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
2022  * @adapter: board private structure to initialize
2023  *
2024  * Attempt to configure the interrupts using the best available
2025  * capabilities of the hardware and the kernel.
2026  **/
2027 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
2028 {
2029         int err = 0;
2030         int vector, v_budget;
2031
2032         /*
2033          * It's easy to be greedy for MSI-X vectors, but it really
2034          * doesn't do us much good if we have a lot more vectors
2035          * than CPU's.  So let's be conservative and only ask for
2036          * (roughly) twice the number of vectors as there are CPU's.
2037          */
2038         v_budget = min(adapter->num_rx_queues + adapter->num_tx_queues,
2039                        (int)(num_online_cpus() * 2)) + NON_Q_VECTORS;
2040
2041         /* A failure in MSI-X entry allocation isn't fatal, but it does
2042          * mean we disable MSI-X capabilities of the adapter. */
2043         adapter->msix_entries = kcalloc(v_budget,
2044                                         sizeof(struct msix_entry), GFP_KERNEL);
2045         if (!adapter->msix_entries) {
2046                 err = -ENOMEM;
2047                 goto out;
2048         }
2049
2050         for (vector = 0; vector < v_budget; vector++)
2051                 adapter->msix_entries[vector].entry = vector;
2052
2053         ixgbevf_acquire_msix_vectors(adapter, v_budget);
2054
2055 out:
2056         return err;
2057 }
2058
2059 /**
2060  * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2061  * @adapter: board private structure to initialize
2062  *
2063  * We allocate one q_vector per queue interrupt.  If allocation fails we
2064  * return -ENOMEM.
2065  **/
2066 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2067 {
2068         int q_idx, num_q_vectors;
2069         struct ixgbevf_q_vector *q_vector;
2070         int napi_vectors;
2071         int (*poll)(struct napi_struct *, int);
2072
2073         num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2074         napi_vectors = adapter->num_rx_queues;
2075         poll = &ixgbevf_clean_rxonly;
2076
2077         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2078                 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
2079                 if (!q_vector)
2080                         goto err_out;
2081                 q_vector->adapter = adapter;
2082                 q_vector->v_idx = q_idx;
2083                 q_vector->eitr = adapter->eitr_param;
2084                 if (q_idx < napi_vectors)
2085                         netif_napi_add(adapter->netdev, &q_vector->napi,
2086                                        (*poll), 64);
2087                 adapter->q_vector[q_idx] = q_vector;
2088         }
2089
2090         return 0;
2091
2092 err_out:
2093         while (q_idx) {
2094                 q_idx--;
2095                 q_vector = adapter->q_vector[q_idx];
2096                 netif_napi_del(&q_vector->napi);
2097                 kfree(q_vector);
2098                 adapter->q_vector[q_idx] = NULL;
2099         }
2100         return -ENOMEM;
2101 }
2102
2103 /**
2104  * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2105  * @adapter: board private structure to initialize
2106  *
2107  * This function frees the memory allocated to the q_vectors.  In addition if
2108  * NAPI is enabled it will delete any references to the NAPI struct prior
2109  * to freeing the q_vector.
2110  **/
2111 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2112 {
2113         int q_idx, num_q_vectors;
2114         int napi_vectors;
2115
2116         num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2117         napi_vectors = adapter->num_rx_queues;
2118
2119         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2120                 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
2121
2122                 adapter->q_vector[q_idx] = NULL;
2123                 if (q_idx < napi_vectors)
2124                         netif_napi_del(&q_vector->napi);
2125                 kfree(q_vector);
2126         }
2127 }
2128
2129 /**
2130  * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2131  * @adapter: board private structure
2132  *
2133  **/
2134 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2135 {
2136         pci_disable_msix(adapter->pdev);
2137         kfree(adapter->msix_entries);
2138         adapter->msix_entries = NULL;
2139
2140         return;
2141 }
2142
2143 /**
2144  * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
2145  * @adapter: board private structure to initialize
2146  *
2147  **/
2148 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2149 {
2150         int err;
2151
2152         /* Number of supported queues */
2153         ixgbevf_set_num_queues(adapter);
2154
2155         err = ixgbevf_set_interrupt_capability(adapter);
2156         if (err) {
2157                 hw_dbg(&adapter->hw,
2158                        "Unable to setup interrupt capabilities\n");
2159                 goto err_set_interrupt;
2160         }
2161
2162         err = ixgbevf_alloc_q_vectors(adapter);
2163         if (err) {
2164                 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
2165                        "vectors\n");
2166                 goto err_alloc_q_vectors;
2167         }
2168
2169         err = ixgbevf_alloc_queues(adapter);
2170         if (err) {
2171                 printk(KERN_ERR "Unable to allocate memory for queues\n");
2172                 goto err_alloc_queues;
2173         }
2174
2175         hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
2176                "Tx Queue count = %u\n",
2177                (adapter->num_rx_queues > 1) ? "Enabled" :
2178                "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2179
2180         set_bit(__IXGBEVF_DOWN, &adapter->state);
2181
2182         return 0;
2183 err_alloc_queues:
2184         ixgbevf_free_q_vectors(adapter);
2185 err_alloc_q_vectors:
2186         ixgbevf_reset_interrupt_capability(adapter);
2187 err_set_interrupt:
2188         return err;
2189 }
2190
2191 /**
2192  * ixgbevf_sw_init - Initialize general software structures
2193  * (struct ixgbevf_adapter)
2194  * @adapter: board private structure to initialize
2195  *
2196  * ixgbevf_sw_init initializes the Adapter private data structure.
2197  * Fields are initialized based on PCI device information and
2198  * OS network device settings (MTU size).
2199  **/
2200 static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2201 {
2202         struct ixgbe_hw *hw = &adapter->hw;
2203         struct pci_dev *pdev = adapter->pdev;
2204         int err;
2205
2206         /* PCI config space info */
2207
2208         hw->vendor_id = pdev->vendor;
2209         hw->device_id = pdev->device;
2210         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2211         hw->subsystem_vendor_id = pdev->subsystem_vendor;
2212         hw->subsystem_device_id = pdev->subsystem_device;
2213
2214         hw->mbx.ops.init_params(hw);
2215         hw->mac.max_tx_queues = MAX_TX_QUEUES;
2216         hw->mac.max_rx_queues = MAX_RX_QUEUES;
2217         err = hw->mac.ops.reset_hw(hw);
2218         if (err) {
2219                 dev_info(&pdev->dev,
2220                          "PF still in reset state, assigning new address\n");
2221                 random_ether_addr(hw->mac.addr);
2222         } else {
2223                 err = hw->mac.ops.init_hw(hw);
2224                 if (err) {
2225                         printk(KERN_ERR "init_shared_code failed: %d\n", err);
2226                         goto out;
2227                 }
2228         }
2229
2230         /* Enable dynamic interrupt throttling rates */
2231         adapter->eitr_param = 20000;
2232         adapter->itr_setting = 1;
2233
2234         /* set defaults for eitr in MegaBytes */
2235         adapter->eitr_low = 10;
2236         adapter->eitr_high = 20;
2237
2238         /* set default ring sizes */
2239         adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2240         adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2241
2242         /* enable rx csum by default */
2243         adapter->flags |= IXGBE_FLAG_RX_CSUM_ENABLED;
2244
2245         set_bit(__IXGBEVF_DOWN, &adapter->state);
2246
2247 out:
2248         return err;
2249 }
2250
2251 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter)     \
2252         {                                                       \
2253                 u32 current_counter = IXGBE_READ_REG(hw, reg);  \
2254                 if (current_counter < last_counter)             \
2255                         counter += 0x100000000LL;               \
2256                 last_counter = current_counter;                 \
2257                 counter &= 0xFFFFFFFF00000000LL;                \
2258                 counter |= current_counter;                     \
2259         }
2260
2261 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2262         {                                                                \
2263                 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb);   \
2264                 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb);   \
2265                 u64 current_counter = (current_counter_msb << 32) |      \
2266                         current_counter_lsb;                             \
2267                 if (current_counter < last_counter)                      \
2268                         counter += 0x1000000000LL;                       \
2269                 last_counter = current_counter;                          \
2270                 counter &= 0xFFFFFFF000000000LL;                         \
2271                 counter |= current_counter;                              \
2272         }
2273 /**
2274  * ixgbevf_update_stats - Update the board statistics counters.
2275  * @adapter: board private structure
2276  **/
2277 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2278 {
2279         struct ixgbe_hw *hw = &adapter->hw;
2280
2281         UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2282                                 adapter->stats.vfgprc);
2283         UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2284                                 adapter->stats.vfgptc);
2285         UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2286                                 adapter->stats.last_vfgorc,
2287                                 adapter->stats.vfgorc);
2288         UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2289                                 adapter->stats.last_vfgotc,
2290                                 adapter->stats.vfgotc);
2291         UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2292                                 adapter->stats.vfmprc);
2293
2294         /* Fill out the OS statistics structure */
2295         adapter->net_stats.multicast = adapter->stats.vfmprc -
2296                 adapter->stats.base_vfmprc;
2297 }
2298
2299 /**
2300  * ixgbevf_watchdog - Timer Call-back
2301  * @data: pointer to adapter cast into an unsigned long
2302  **/
2303 static void ixgbevf_watchdog(unsigned long data)
2304 {
2305         struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2306         struct ixgbe_hw *hw = &adapter->hw;
2307         u64 eics = 0;
2308         int i;
2309
2310         /*
2311          * Do the watchdog outside of interrupt context due to the lovely
2312          * delays that some of the newer hardware requires
2313          */
2314
2315         if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2316                 goto watchdog_short_circuit;
2317
2318         /* get one bit for every active tx/rx interrupt vector */
2319         for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2320                 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2321                 if (qv->rxr_count || qv->txr_count)
2322                         eics |= (1 << i);
2323         }
2324
2325         IXGBE_WRITE_REG(hw, IXGBE_VTEICS, (u32)eics);
2326
2327 watchdog_short_circuit:
2328         schedule_work(&adapter->watchdog_task);
2329 }
2330
2331 /**
2332  * ixgbevf_tx_timeout - Respond to a Tx Hang
2333  * @netdev: network interface device structure
2334  **/
2335 static void ixgbevf_tx_timeout(struct net_device *netdev)
2336 {
2337         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2338
2339         /* Do the reset outside of interrupt context */
2340         schedule_work(&adapter->reset_task);
2341 }
2342
2343 static void ixgbevf_reset_task(struct work_struct *work)
2344 {
2345         struct ixgbevf_adapter *adapter;
2346         adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2347
2348         /* If we're already down or resetting, just bail */
2349         if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2350             test_bit(__IXGBEVF_RESETTING, &adapter->state))
2351                 return;
2352
2353         adapter->tx_timeout_count++;
2354
2355         ixgbevf_reinit_locked(adapter);
2356 }
2357
2358 /**
2359  * ixgbevf_watchdog_task - worker thread to bring link up
2360  * @work: pointer to work_struct containing our data
2361  **/
2362 static void ixgbevf_watchdog_task(struct work_struct *work)
2363 {
2364         struct ixgbevf_adapter *adapter = container_of(work,
2365                                                        struct ixgbevf_adapter,
2366                                                        watchdog_task);
2367         struct net_device *netdev = adapter->netdev;
2368         struct ixgbe_hw *hw = &adapter->hw;
2369         u32 link_speed = adapter->link_speed;
2370         bool link_up = adapter->link_up;
2371
2372         adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2373
2374         /*
2375          * Always check the link on the watchdog because we have
2376          * no LSC interrupt
2377          */
2378         if (hw->mac.ops.check_link) {
2379                 if ((hw->mac.ops.check_link(hw, &link_speed,
2380                                             &link_up, false)) != 0) {
2381                         adapter->link_up = link_up;
2382                         adapter->link_speed = link_speed;
2383                         netif_carrier_off(netdev);
2384                         netif_tx_stop_all_queues(netdev);
2385                         schedule_work(&adapter->reset_task);
2386                         goto pf_has_reset;
2387                 }
2388         } else {
2389                 /* always assume link is up, if no check link
2390                  * function */
2391                 link_speed = IXGBE_LINK_SPEED_10GB_FULL;
2392                 link_up = true;
2393         }
2394         adapter->link_up = link_up;
2395         adapter->link_speed = link_speed;
2396
2397         if (link_up) {
2398                 if (!netif_carrier_ok(netdev)) {
2399                         hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
2400                                (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2401                                10 : 1);
2402                         netif_carrier_on(netdev);
2403                         netif_tx_wake_all_queues(netdev);
2404                 } else {
2405                         /* Force detection of hung controller */
2406                         adapter->detect_tx_hung = true;
2407                 }
2408         } else {
2409                 adapter->link_up = false;
2410                 adapter->link_speed = 0;
2411                 if (netif_carrier_ok(netdev)) {
2412                         hw_dbg(&adapter->hw, "NIC Link is Down\n");
2413                         netif_carrier_off(netdev);
2414                         netif_tx_stop_all_queues(netdev);
2415                 }
2416         }
2417
2418         ixgbevf_update_stats(adapter);
2419
2420 pf_has_reset:
2421         /* Force detection of hung controller every watchdog period */
2422         adapter->detect_tx_hung = true;
2423
2424         /* Reset the timer */
2425         if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2426                 mod_timer(&adapter->watchdog_timer,
2427                           round_jiffies(jiffies + (2 * HZ)));
2428
2429         adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2430 }
2431
2432 /**
2433  * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2434  * @adapter: board private structure
2435  * @tx_ring: Tx descriptor ring for a specific queue
2436  *
2437  * Free all transmit software resources
2438  **/
2439 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2440                                struct ixgbevf_ring *tx_ring)
2441 {
2442         struct pci_dev *pdev = adapter->pdev;
2443
2444         ixgbevf_clean_tx_ring(adapter, tx_ring);
2445
2446         vfree(tx_ring->tx_buffer_info);
2447         tx_ring->tx_buffer_info = NULL;
2448
2449         pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
2450
2451         tx_ring->desc = NULL;
2452 }
2453
2454 /**
2455  * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2456  * @adapter: board private structure
2457  *
2458  * Free all transmit software resources
2459  **/
2460 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2461 {
2462         int i;
2463
2464         for (i = 0; i < adapter->num_tx_queues; i++)
2465                 if (adapter->tx_ring[i].desc)
2466                         ixgbevf_free_tx_resources(adapter,
2467                                                   &adapter->tx_ring[i]);
2468
2469 }
2470
2471 /**
2472  * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2473  * @adapter: board private structure
2474  * @tx_ring:    tx descriptor ring (for a specific queue) to setup
2475  *
2476  * Return 0 on success, negative on failure
2477  **/
2478 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2479                                struct ixgbevf_ring *tx_ring)
2480 {
2481         struct pci_dev *pdev = adapter->pdev;
2482         int size;
2483
2484         size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2485         tx_ring->tx_buffer_info = vmalloc(size);
2486         if (!tx_ring->tx_buffer_info)
2487                 goto err;
2488         memset(tx_ring->tx_buffer_info, 0, size);
2489
2490         /* round up to nearest 4K */
2491         tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2492         tx_ring->size = ALIGN(tx_ring->size, 4096);
2493
2494         tx_ring->desc = pci_alloc_consistent(pdev, tx_ring->size,
2495                                              &tx_ring->dma);
2496         if (!tx_ring->desc)
2497                 goto err;
2498
2499         tx_ring->next_to_use = 0;
2500         tx_ring->next_to_clean = 0;
2501         tx_ring->work_limit = tx_ring->count;
2502         return 0;
2503
2504 err:
2505         vfree(tx_ring->tx_buffer_info);
2506         tx_ring->tx_buffer_info = NULL;
2507         hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2508                "descriptor ring\n");
2509         return -ENOMEM;
2510 }
2511
2512 /**
2513  * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2514  * @adapter: board private structure
2515  *
2516  * If this function returns with an error, then it's possible one or
2517  * more of the rings is populated (while the rest are not).  It is the
2518  * callers duty to clean those orphaned rings.
2519  *
2520  * Return 0 on success, negative on failure
2521  **/
2522 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2523 {
2524         int i, err = 0;
2525
2526         for (i = 0; i < adapter->num_tx_queues; i++) {
2527                 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2528                 if (!err)
2529                         continue;
2530                 hw_dbg(&adapter->hw,
2531                        "Allocation for Tx Queue %u failed\n", i);
2532                 break;
2533         }
2534
2535         return err;
2536 }
2537
2538 /**
2539  * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2540  * @adapter: board private structure
2541  * @rx_ring:    rx descriptor ring (for a specific queue) to setup
2542  *
2543  * Returns 0 on success, negative on failure
2544  **/
2545 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2546                                struct ixgbevf_ring *rx_ring)
2547 {
2548         struct pci_dev *pdev = adapter->pdev;
2549         int size;
2550
2551         size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2552         rx_ring->rx_buffer_info = vmalloc(size);
2553         if (!rx_ring->rx_buffer_info) {
2554                 hw_dbg(&adapter->hw,
2555                        "Unable to vmalloc buffer memory for "
2556                        "the receive descriptor ring\n");
2557                 goto alloc_failed;
2558         }
2559         memset(rx_ring->rx_buffer_info, 0, size);
2560
2561         /* Round up to nearest 4K */
2562         rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2563         rx_ring->size = ALIGN(rx_ring->size, 4096);
2564
2565         rx_ring->desc = pci_alloc_consistent(pdev, rx_ring->size,
2566                                              &rx_ring->dma);
2567
2568         if (!rx_ring->desc) {
2569                 hw_dbg(&adapter->hw,
2570                        "Unable to allocate memory for "
2571                        "the receive descriptor ring\n");
2572                 vfree(rx_ring->rx_buffer_info);
2573                 rx_ring->rx_buffer_info = NULL;
2574                 goto alloc_failed;
2575         }
2576
2577         rx_ring->next_to_clean = 0;
2578         rx_ring->next_to_use = 0;
2579
2580         return 0;
2581 alloc_failed:
2582         return -ENOMEM;
2583 }
2584
2585 /**
2586  * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2587  * @adapter: board private structure
2588  *
2589  * If this function returns with an error, then it's possible one or
2590  * more of the rings is populated (while the rest are not).  It is the
2591  * callers duty to clean those orphaned rings.
2592  *
2593  * Return 0 on success, negative on failure
2594  **/
2595 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2596 {
2597         int i, err = 0;
2598
2599         for (i = 0; i < adapter->num_rx_queues; i++) {
2600                 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2601                 if (!err)
2602                         continue;
2603                 hw_dbg(&adapter->hw,
2604                        "Allocation for Rx Queue %u failed\n", i);
2605                 break;
2606         }
2607         return err;
2608 }
2609
2610 /**
2611  * ixgbevf_free_rx_resources - Free Rx Resources
2612  * @adapter: board private structure
2613  * @rx_ring: ring to clean the resources from
2614  *
2615  * Free all receive software resources
2616  **/
2617 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2618                                struct ixgbevf_ring *rx_ring)
2619 {
2620         struct pci_dev *pdev = adapter->pdev;
2621
2622         ixgbevf_clean_rx_ring(adapter, rx_ring);
2623
2624         vfree(rx_ring->rx_buffer_info);
2625         rx_ring->rx_buffer_info = NULL;
2626
2627         pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2628
2629         rx_ring->desc = NULL;
2630 }
2631
2632 /**
2633  * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2634  * @adapter: board private structure
2635  *
2636  * Free all receive software resources
2637  **/
2638 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2639 {
2640         int i;
2641
2642         for (i = 0; i < adapter->num_rx_queues; i++)
2643                 if (adapter->rx_ring[i].desc)
2644                         ixgbevf_free_rx_resources(adapter,
2645                                                   &adapter->rx_ring[i]);
2646 }
2647
2648 /**
2649  * ixgbevf_open - Called when a network interface is made active
2650  * @netdev: network interface device structure
2651  *
2652  * Returns 0 on success, negative value on failure
2653  *
2654  * The open entry point is called when a network interface is made
2655  * active by the system (IFF_UP).  At this point all resources needed
2656  * for transmit and receive operations are allocated, the interrupt
2657  * handler is registered with the OS, the watchdog timer is started,
2658  * and the stack is notified that the interface is ready.
2659  **/
2660 static int ixgbevf_open(struct net_device *netdev)
2661 {
2662         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2663         struct ixgbe_hw *hw = &adapter->hw;
2664         int err;
2665
2666         /* disallow open during test */
2667         if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2668                 return -EBUSY;
2669
2670         if (hw->adapter_stopped) {
2671                 ixgbevf_reset(adapter);
2672                 /* if adapter is still stopped then PF isn't up and
2673                  * the vf can't start. */
2674                 if (hw->adapter_stopped) {
2675                         err = IXGBE_ERR_MBX;
2676                         printk(KERN_ERR "Unable to start - perhaps the PF"
2677                                " Driver isn't up yet\n");
2678                         goto err_setup_reset;
2679                 }
2680         }
2681
2682         /* allocate transmit descriptors */
2683         err = ixgbevf_setup_all_tx_resources(adapter);
2684         if (err)
2685                 goto err_setup_tx;
2686
2687         /* allocate receive descriptors */
2688         err = ixgbevf_setup_all_rx_resources(adapter);
2689         if (err)
2690                 goto err_setup_rx;
2691
2692         ixgbevf_configure(adapter);
2693
2694         /*
2695          * Map the Tx/Rx rings to the vectors we were allotted.
2696          * if request_irq will be called in this function map_rings
2697          * must be called *before* up_complete
2698          */
2699         ixgbevf_map_rings_to_vectors(adapter);
2700
2701         err = ixgbevf_up_complete(adapter);
2702         if (err)
2703                 goto err_up;
2704
2705         /* clear any pending interrupts, may auto mask */
2706         IXGBE_READ_REG(hw, IXGBE_VTEICR);
2707         err = ixgbevf_request_irq(adapter);
2708         if (err)
2709                 goto err_req_irq;
2710
2711         ixgbevf_irq_enable(adapter, true, true);
2712
2713         return 0;
2714
2715 err_req_irq:
2716         ixgbevf_down(adapter);
2717 err_up:
2718         ixgbevf_free_irq(adapter);
2719 err_setup_rx:
2720         ixgbevf_free_all_rx_resources(adapter);
2721 err_setup_tx:
2722         ixgbevf_free_all_tx_resources(adapter);
2723         ixgbevf_reset(adapter);
2724
2725 err_setup_reset:
2726
2727         return err;
2728 }
2729
2730 /**
2731  * ixgbevf_close - Disables a network interface
2732  * @netdev: network interface device structure
2733  *
2734  * Returns 0, this is not allowed to fail
2735  *
2736  * The close entry point is called when an interface is de-activated
2737  * by the OS.  The hardware is still under the drivers control, but
2738  * needs to be disabled.  A global MAC reset is issued to stop the
2739  * hardware, and all transmit and receive resources are freed.
2740  **/
2741 static int ixgbevf_close(struct net_device *netdev)
2742 {
2743         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2744
2745         ixgbevf_down(adapter);
2746         ixgbevf_free_irq(adapter);
2747
2748         ixgbevf_free_all_tx_resources(adapter);
2749         ixgbevf_free_all_rx_resources(adapter);
2750
2751         return 0;
2752 }
2753
2754 static int ixgbevf_tso(struct ixgbevf_adapter *adapter,
2755                        struct ixgbevf_ring *tx_ring,
2756                        struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2757 {
2758         struct ixgbe_adv_tx_context_desc *context_desc;
2759         unsigned int i;
2760         int err;
2761         struct ixgbevf_tx_buffer *tx_buffer_info;
2762         u32 vlan_macip_lens = 0, type_tucmd_mlhl;
2763         u32 mss_l4len_idx, l4len;
2764
2765         if (skb_is_gso(skb)) {
2766                 if (skb_header_cloned(skb)) {
2767                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2768                         if (err)
2769                                 return err;
2770                 }
2771                 l4len = tcp_hdrlen(skb);
2772                 *hdr_len += l4len;
2773
2774                 if (skb->protocol == htons(ETH_P_IP)) {
2775                         struct iphdr *iph = ip_hdr(skb);
2776                         iph->tot_len = 0;
2777                         iph->check = 0;
2778                         tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2779                                                                  iph->daddr, 0,
2780                                                                  IPPROTO_TCP,
2781                                                                  0);
2782                         adapter->hw_tso_ctxt++;
2783                 } else if (skb_is_gso_v6(skb)) {
2784                         ipv6_hdr(skb)->payload_len = 0;
2785                         tcp_hdr(skb)->check =
2786                             ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2787                                              &ipv6_hdr(skb)->daddr,
2788                                              0, IPPROTO_TCP, 0);
2789                         adapter->hw_tso6_ctxt++;
2790                 }
2791
2792                 i = tx_ring->next_to_use;
2793
2794                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2795                 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2796
2797                 /* VLAN MACLEN IPLEN */
2798                 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2799                         vlan_macip_lens |=
2800                                 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
2801                 vlan_macip_lens |= ((skb_network_offset(skb)) <<
2802                                     IXGBE_ADVTXD_MACLEN_SHIFT);
2803                 *hdr_len += skb_network_offset(skb);
2804                 vlan_macip_lens |=
2805                         (skb_transport_header(skb) - skb_network_header(skb));
2806                 *hdr_len +=
2807                         (skb_transport_header(skb) - skb_network_header(skb));
2808                 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2809                 context_desc->seqnum_seed = 0;
2810
2811                 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2812                 type_tucmd_mlhl = (IXGBE_TXD_CMD_DEXT |
2813                                     IXGBE_ADVTXD_DTYP_CTXT);
2814
2815                 if (skb->protocol == htons(ETH_P_IP))
2816                         type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
2817                 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2818                 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2819
2820                 /* MSS L4LEN IDX */
2821                 mss_l4len_idx =
2822                         (skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT);
2823                 mss_l4len_idx |= (l4len << IXGBE_ADVTXD_L4LEN_SHIFT);
2824                 /* use index 1 for TSO */
2825                 mss_l4len_idx |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2826                 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2827
2828                 tx_buffer_info->time_stamp = jiffies;
2829                 tx_buffer_info->next_to_watch = i;
2830
2831                 i++;
2832                 if (i == tx_ring->count)
2833                         i = 0;
2834                 tx_ring->next_to_use = i;
2835
2836                 return true;
2837         }
2838
2839         return false;
2840 }
2841
2842 static bool ixgbevf_tx_csum(struct ixgbevf_adapter *adapter,
2843                             struct ixgbevf_ring *tx_ring,
2844                             struct sk_buff *skb, u32 tx_flags)
2845 {
2846         struct ixgbe_adv_tx_context_desc *context_desc;
2847         unsigned int i;
2848         struct ixgbevf_tx_buffer *tx_buffer_info;
2849         u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
2850
2851         if (skb->ip_summed == CHECKSUM_PARTIAL ||
2852             (tx_flags & IXGBE_TX_FLAGS_VLAN)) {
2853                 i = tx_ring->next_to_use;
2854                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2855                 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2856
2857                 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2858                         vlan_macip_lens |= (tx_flags &
2859                                             IXGBE_TX_FLAGS_VLAN_MASK);
2860                 vlan_macip_lens |= (skb_network_offset(skb) <<
2861                                     IXGBE_ADVTXD_MACLEN_SHIFT);
2862                 if (skb->ip_summed == CHECKSUM_PARTIAL)
2863                         vlan_macip_lens |= (skb_transport_header(skb) -
2864                                             skb_network_header(skb));
2865
2866                 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2867                 context_desc->seqnum_seed = 0;
2868
2869                 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
2870                                     IXGBE_ADVTXD_DTYP_CTXT);
2871
2872                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2873                         switch (skb->protocol) {
2874                         case __constant_htons(ETH_P_IP):
2875                                 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
2876                                 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2877                                         type_tucmd_mlhl |=
2878                                             IXGBE_ADVTXD_TUCMD_L4T_TCP;
2879                                 break;
2880                         case __constant_htons(ETH_P_IPV6):
2881                                 /* XXX what about other V6 headers?? */
2882                                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2883                                         type_tucmd_mlhl |=
2884                                                 IXGBE_ADVTXD_TUCMD_L4T_TCP;
2885                                 break;
2886                         default:
2887                                 if (unlikely(net_ratelimit())) {
2888                                         printk(KERN_WARNING
2889                                                "partial checksum but "
2890                                                "proto=%x!\n",
2891                                                skb->protocol);
2892                                 }
2893                                 break;
2894                         }
2895                 }
2896
2897                 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2898                 /* use index zero for tx checksum offload */
2899                 context_desc->mss_l4len_idx = 0;
2900
2901                 tx_buffer_info->time_stamp = jiffies;
2902                 tx_buffer_info->next_to_watch = i;
2903
2904                 adapter->hw_csum_tx_good++;
2905                 i++;
2906                 if (i == tx_ring->count)
2907                         i = 0;
2908                 tx_ring->next_to_use = i;
2909
2910                 return true;
2911         }
2912
2913         return false;
2914 }
2915
2916 static int ixgbevf_tx_map(struct ixgbevf_adapter *adapter,
2917                           struct ixgbevf_ring *tx_ring,
2918                           struct sk_buff *skb, u32 tx_flags,
2919                           unsigned int first)
2920 {
2921         struct pci_dev *pdev = adapter->pdev;
2922         struct ixgbevf_tx_buffer *tx_buffer_info;
2923         unsigned int len;
2924         unsigned int total = skb->len;
2925         unsigned int offset = 0, size, count = 0;
2926         unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2927         unsigned int f;
2928         int i;
2929
2930         i = tx_ring->next_to_use;
2931
2932         len = min(skb_headlen(skb), total);
2933         while (len) {
2934                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2935                 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2936
2937                 tx_buffer_info->length = size;
2938                 tx_buffer_info->mapped_as_page = false;
2939                 tx_buffer_info->dma = pci_map_single(adapter->pdev,
2940                                                      skb->data + offset,
2941                                                      size, PCI_DMA_TODEVICE);
2942                 if (pci_dma_mapping_error(pdev, tx_buffer_info->dma))
2943                         goto dma_error;
2944                 tx_buffer_info->time_stamp = jiffies;
2945                 tx_buffer_info->next_to_watch = i;
2946
2947                 len -= size;
2948                 total -= size;
2949                 offset += size;
2950                 count++;
2951                 i++;
2952                 if (i == tx_ring->count)
2953                         i = 0;
2954         }
2955
2956         for (f = 0; f < nr_frags; f++) {
2957                 struct skb_frag_struct *frag;
2958
2959                 frag = &skb_shinfo(skb)->frags[f];
2960                 len = min((unsigned int)frag->size, total);
2961                 offset = frag->page_offset;
2962
2963                 while (len) {
2964                         tx_buffer_info = &tx_ring->tx_buffer_info[i];
2965                         size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2966
2967                         tx_buffer_info->length = size;
2968                         tx_buffer_info->dma = pci_map_page(adapter->pdev,
2969                                                            frag->page,
2970                                                            offset,
2971                                                            size,
2972                                                            PCI_DMA_TODEVICE);
2973                         tx_buffer_info->mapped_as_page = true;
2974                         if (pci_dma_mapping_error(pdev, tx_buffer_info->dma))
2975                                 goto dma_error;
2976                         tx_buffer_info->time_stamp = jiffies;
2977                         tx_buffer_info->next_to_watch = i;
2978
2979                         len -= size;
2980                         total -= size;
2981                         offset += size;
2982                         count++;
2983                         i++;
2984                         if (i == tx_ring->count)
2985                                 i = 0;
2986                 }
2987                 if (total == 0)
2988                         break;
2989         }
2990
2991         if (i == 0)
2992                 i = tx_ring->count - 1;
2993         else
2994                 i = i - 1;
2995         tx_ring->tx_buffer_info[i].skb = skb;
2996         tx_ring->tx_buffer_info[first].next_to_watch = i;
2997
2998         return count;
2999
3000 dma_error:
3001         dev_err(&pdev->dev, "TX DMA map failed\n");
3002
3003         /* clear timestamp and dma mappings for failed tx_buffer_info map */
3004         tx_buffer_info->dma = 0;
3005         tx_buffer_info->time_stamp = 0;
3006         tx_buffer_info->next_to_watch = 0;
3007         count--;
3008
3009         /* clear timestamp and dma mappings for remaining portion of packet */
3010         while (count >= 0) {
3011                 count--;
3012                 i--;
3013                 if (i < 0)
3014                         i += tx_ring->count;
3015                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3016                 ixgbevf_unmap_and_free_tx_resource(adapter, tx_buffer_info);
3017         }
3018
3019         return count;
3020 }
3021
3022 static void ixgbevf_tx_queue(struct ixgbevf_adapter *adapter,
3023                              struct ixgbevf_ring *tx_ring, int tx_flags,
3024                              int count, u32 paylen, u8 hdr_len)
3025 {
3026         union ixgbe_adv_tx_desc *tx_desc = NULL;
3027         struct ixgbevf_tx_buffer *tx_buffer_info;
3028         u32 olinfo_status = 0, cmd_type_len = 0;
3029         unsigned int i;
3030
3031         u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
3032
3033         cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
3034
3035         cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
3036
3037         if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3038                 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
3039
3040         if (tx_flags & IXGBE_TX_FLAGS_TSO) {
3041                 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
3042
3043                 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
3044                         IXGBE_ADVTXD_POPTS_SHIFT;
3045
3046                 /* use index 1 context for tso */
3047                 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
3048                 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3049                         olinfo_status |= IXGBE_TXD_POPTS_IXSM <<
3050                                 IXGBE_ADVTXD_POPTS_SHIFT;
3051
3052         } else if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3053                 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
3054                         IXGBE_ADVTXD_POPTS_SHIFT;
3055
3056         olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
3057
3058         i = tx_ring->next_to_use;
3059         while (count--) {
3060                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3061                 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
3062                 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
3063                 tx_desc->read.cmd_type_len =
3064                         cpu_to_le32(cmd_type_len | tx_buffer_info->length);
3065                 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
3066                 i++;
3067                 if (i == tx_ring->count)
3068                         i = 0;
3069         }
3070
3071         tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
3072
3073         /*
3074          * Force memory writes to complete before letting h/w
3075          * know there are new descriptors to fetch.  (Only
3076          * applicable for weak-ordered memory model archs,
3077          * such as IA-64).
3078          */
3079         wmb();
3080
3081         tx_ring->next_to_use = i;
3082         writel(i, adapter->hw.hw_addr + tx_ring->tail);
3083 }
3084
3085 static int __ixgbevf_maybe_stop_tx(struct net_device *netdev,
3086                                    struct ixgbevf_ring *tx_ring, int size)
3087 {
3088         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3089
3090         netif_stop_subqueue(netdev, tx_ring->queue_index);
3091         /* Herbert's original patch had:
3092          *  smp_mb__after_netif_stop_queue();
3093          * but since that doesn't exist yet, just open code it. */
3094         smp_mb();
3095
3096         /* We need to check again in a case another CPU has just
3097          * made room available. */
3098         if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
3099                 return -EBUSY;
3100
3101         /* A reprieve! - use start_queue because it doesn't call schedule */
3102         netif_start_subqueue(netdev, tx_ring->queue_index);
3103         ++adapter->restart_queue;
3104         return 0;
3105 }
3106
3107 static int ixgbevf_maybe_stop_tx(struct net_device *netdev,
3108                                  struct ixgbevf_ring *tx_ring, int size)
3109 {
3110         if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
3111                 return 0;
3112         return __ixgbevf_maybe_stop_tx(netdev, tx_ring, size);
3113 }
3114
3115 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3116 {
3117         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3118         struct ixgbevf_ring *tx_ring;
3119         unsigned int first;
3120         unsigned int tx_flags = 0;
3121         u8 hdr_len = 0;
3122         int r_idx = 0, tso;
3123         int count = 0;
3124
3125         unsigned int f;
3126
3127         tx_ring = &adapter->tx_ring[r_idx];
3128
3129         if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
3130                 tx_flags |= vlan_tx_tag_get(skb);
3131                 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3132                 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3133         }
3134
3135         /* four things can cause us to need a context descriptor */
3136         if (skb_is_gso(skb) ||
3137             (skb->ip_summed == CHECKSUM_PARTIAL) ||
3138             (tx_flags & IXGBE_TX_FLAGS_VLAN))
3139                 count++;
3140
3141         count += TXD_USE_COUNT(skb_headlen(skb));
3142         for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3143                 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3144
3145         if (ixgbevf_maybe_stop_tx(netdev, tx_ring, count)) {
3146                 adapter->tx_busy++;
3147                 return NETDEV_TX_BUSY;
3148         }
3149
3150         first = tx_ring->next_to_use;
3151
3152         if (skb->protocol == htons(ETH_P_IP))
3153                 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3154         tso = ixgbevf_tso(adapter, tx_ring, skb, tx_flags, &hdr_len);
3155         if (tso < 0) {
3156                 dev_kfree_skb_any(skb);
3157                 return NETDEV_TX_OK;
3158         }
3159
3160         if (tso)
3161                 tx_flags |= IXGBE_TX_FLAGS_TSO;
3162         else if (ixgbevf_tx_csum(adapter, tx_ring, skb, tx_flags) &&
3163                  (skb->ip_summed == CHECKSUM_PARTIAL))
3164                 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3165
3166         ixgbevf_tx_queue(adapter, tx_ring, tx_flags,
3167                          ixgbevf_tx_map(adapter, tx_ring, skb, tx_flags, first),
3168                          skb->len, hdr_len);
3169
3170         netdev->trans_start = jiffies;
3171
3172         ixgbevf_maybe_stop_tx(netdev, tx_ring, DESC_NEEDED);
3173
3174         return NETDEV_TX_OK;
3175 }
3176
3177 /**
3178  * ixgbevf_get_stats - Get System Network Statistics
3179  * @netdev: network interface device structure
3180  *
3181  * Returns the address of the device statistics structure.
3182  * The statistics are actually updated from the timer callback.
3183  **/
3184 static struct net_device_stats *ixgbevf_get_stats(struct net_device *netdev)
3185 {
3186         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3187
3188         /* only return the current stats */
3189         return &adapter->net_stats;
3190 }
3191
3192 /**
3193  * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3194  * @netdev: network interface device structure
3195  * @p: pointer to an address structure
3196  *
3197  * Returns 0 on success, negative on failure
3198  **/
3199 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3200 {
3201         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3202         struct ixgbe_hw *hw = &adapter->hw;
3203         struct sockaddr *addr = p;
3204
3205         if (!is_valid_ether_addr(addr->sa_data))
3206                 return -EADDRNOTAVAIL;
3207
3208         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3209         memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3210
3211         if (hw->mac.ops.set_rar)
3212                 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
3213
3214         return 0;
3215 }
3216
3217 /**
3218  * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3219  * @netdev: network interface device structure
3220  * @new_mtu: new value for maximum frame size
3221  *
3222  * Returns 0 on success, negative on failure
3223  **/
3224 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3225 {
3226         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3227         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3228
3229         /* MTU < 68 is an error and causes problems on some kernels */
3230         if ((new_mtu < 68) || (max_frame > MAXIMUM_ETHERNET_VLAN_SIZE))
3231                 return -EINVAL;
3232
3233         hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3234                netdev->mtu, new_mtu);
3235         /* must set new MTU before calling down or up */
3236         netdev->mtu = new_mtu;
3237
3238         if (netif_running(netdev))
3239                 ixgbevf_reinit_locked(adapter);
3240
3241         return 0;
3242 }
3243
3244 static void ixgbevf_shutdown(struct pci_dev *pdev)
3245 {
3246         struct net_device *netdev = pci_get_drvdata(pdev);
3247         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3248
3249         netif_device_detach(netdev);
3250
3251         if (netif_running(netdev)) {
3252                 ixgbevf_down(adapter);
3253                 ixgbevf_free_irq(adapter);
3254                 ixgbevf_free_all_tx_resources(adapter);
3255                 ixgbevf_free_all_rx_resources(adapter);
3256         }
3257
3258 #ifdef CONFIG_PM
3259         pci_save_state(pdev);
3260 #endif
3261
3262         pci_disable_device(pdev);
3263 }
3264
3265 static const struct net_device_ops ixgbe_netdev_ops = {
3266         .ndo_open               = &ixgbevf_open,
3267         .ndo_stop               = &ixgbevf_close,
3268         .ndo_start_xmit         = &ixgbevf_xmit_frame,
3269         .ndo_get_stats          = &ixgbevf_get_stats,
3270         .ndo_set_rx_mode        = &ixgbevf_set_rx_mode,
3271         .ndo_set_multicast_list = &ixgbevf_set_rx_mode,
3272         .ndo_validate_addr      = eth_validate_addr,
3273         .ndo_set_mac_address    = &ixgbevf_set_mac,
3274         .ndo_change_mtu         = &ixgbevf_change_mtu,
3275         .ndo_tx_timeout         = &ixgbevf_tx_timeout,
3276         .ndo_vlan_rx_register   = &ixgbevf_vlan_rx_register,
3277         .ndo_vlan_rx_add_vid    = &ixgbevf_vlan_rx_add_vid,
3278         .ndo_vlan_rx_kill_vid   = &ixgbevf_vlan_rx_kill_vid,
3279 };
3280
3281 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3282 {
3283         struct ixgbevf_adapter *adapter;
3284         adapter = netdev_priv(dev);
3285         dev->netdev_ops = &ixgbe_netdev_ops;
3286         ixgbevf_set_ethtool_ops(dev);
3287         dev->watchdog_timeo = 5 * HZ;
3288 }
3289
3290 /**
3291  * ixgbevf_probe - Device Initialization Routine
3292  * @pdev: PCI device information struct
3293  * @ent: entry in ixgbevf_pci_tbl
3294  *
3295  * Returns 0 on success, negative on failure
3296  *
3297  * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3298  * The OS initialization, configuring of the adapter private structure,
3299  * and a hardware reset occur.
3300  **/
3301 static int __devinit ixgbevf_probe(struct pci_dev *pdev,
3302                                    const struct pci_device_id *ent)
3303 {
3304         struct net_device *netdev;
3305         struct ixgbevf_adapter *adapter = NULL;
3306         struct ixgbe_hw *hw = NULL;
3307         const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3308         static int cards_found;
3309         int err, pci_using_dac;
3310
3311         err = pci_enable_device(pdev);
3312         if (err)
3313                 return err;
3314
3315         if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
3316             !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
3317                 pci_using_dac = 1;
3318         } else {
3319                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3320                 if (err) {
3321                         err = pci_set_consistent_dma_mask(pdev,
3322                                                           DMA_BIT_MASK(32));
3323                         if (err) {
3324                                 dev_err(&pdev->dev, "No usable DMA "
3325                                         "configuration, aborting\n");
3326                                 goto err_dma;
3327                         }
3328                 }
3329                 pci_using_dac = 0;
3330         }
3331
3332         err = pci_request_regions(pdev, ixgbevf_driver_name);
3333         if (err) {
3334                 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3335                 goto err_pci_reg;
3336         }
3337
3338         pci_set_master(pdev);
3339
3340 #ifdef HAVE_TX_MQ
3341         netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3342                                    MAX_TX_QUEUES);
3343 #else
3344         netdev = alloc_etherdev(sizeof(struct ixgbevf_adapter));
3345 #endif
3346         if (!netdev) {
3347                 err = -ENOMEM;
3348                 goto err_alloc_etherdev;
3349         }
3350
3351         SET_NETDEV_DEV(netdev, &pdev->dev);
3352
3353         pci_set_drvdata(pdev, netdev);
3354         adapter = netdev_priv(netdev);
3355
3356         adapter->netdev = netdev;
3357         adapter->pdev = pdev;
3358         hw = &adapter->hw;
3359         hw->back = adapter;
3360         adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3361
3362         /*
3363          * call save state here in standalone driver because it relies on
3364          * adapter struct to exist, and needs to call netdev_priv
3365          */
3366         pci_save_state(pdev);
3367
3368         hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3369                               pci_resource_len(pdev, 0));
3370         if (!hw->hw_addr) {
3371                 err = -EIO;
3372                 goto err_ioremap;
3373         }
3374
3375         ixgbevf_assign_netdev_ops(netdev);
3376
3377         adapter->bd_number = cards_found;
3378
3379         /* Setup hw api */
3380         memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3381         hw->mac.type  = ii->mac;
3382
3383         memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3384                sizeof(struct ixgbe_mac_operations));
3385
3386         adapter->flags &= ~IXGBE_FLAG_RX_PS_CAPABLE;
3387         adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
3388         adapter->flags |= IXGBE_FLAG_RX_1BUF_CAPABLE;
3389
3390         /* setup the private structure */
3391         err = ixgbevf_sw_init(adapter);
3392
3393 #ifdef MAX_SKB_FRAGS
3394         netdev->features = NETIF_F_SG |
3395                            NETIF_F_IP_CSUM |
3396                            NETIF_F_HW_VLAN_TX |
3397                            NETIF_F_HW_VLAN_RX |
3398                            NETIF_F_HW_VLAN_FILTER;
3399
3400         netdev->features |= NETIF_F_IPV6_CSUM;
3401         netdev->features |= NETIF_F_TSO;
3402         netdev->features |= NETIF_F_TSO6;
3403         netdev->vlan_features |= NETIF_F_TSO;
3404         netdev->vlan_features |= NETIF_F_TSO6;
3405         netdev->vlan_features |= NETIF_F_IP_CSUM;
3406         netdev->vlan_features |= NETIF_F_SG;
3407
3408         if (pci_using_dac)
3409                 netdev->features |= NETIF_F_HIGHDMA;
3410
3411 #endif /* MAX_SKB_FRAGS */
3412
3413         /* The HW MAC address was set and/or determined in sw_init */
3414         memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
3415         memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3416
3417         if (!is_valid_ether_addr(netdev->dev_addr)) {
3418                 printk(KERN_ERR "invalid MAC address\n");
3419                 err = -EIO;
3420                 goto err_sw_init;
3421         }
3422
3423         init_timer(&adapter->watchdog_timer);
3424         adapter->watchdog_timer.function = &ixgbevf_watchdog;
3425         adapter->watchdog_timer.data = (unsigned long)adapter;
3426
3427         INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3428         INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3429
3430         err = ixgbevf_init_interrupt_scheme(adapter);
3431         if (err)
3432                 goto err_sw_init;
3433
3434         /* pick up the PCI bus settings for reporting later */
3435         if (hw->mac.ops.get_bus_info)
3436                 hw->mac.ops.get_bus_info(hw);
3437
3438
3439         netif_carrier_off(netdev);
3440         netif_tx_stop_all_queues(netdev);
3441
3442         strcpy(netdev->name, "eth%d");
3443
3444         err = register_netdev(netdev);
3445         if (err)
3446                 goto err_register;
3447
3448         adapter->netdev_registered = true;
3449
3450         ixgbevf_init_last_counter_stats(adapter);
3451
3452         /* print the MAC address */
3453         hw_dbg(hw, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
3454                netdev->dev_addr[0],
3455                netdev->dev_addr[1],
3456                netdev->dev_addr[2],
3457                netdev->dev_addr[3],
3458                netdev->dev_addr[4],
3459                netdev->dev_addr[5]);
3460
3461         hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3462
3463         hw_dbg(hw, "LRO is disabled\n");
3464
3465         hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3466         cards_found++;
3467         return 0;
3468
3469 err_register:
3470 err_sw_init:
3471         ixgbevf_reset_interrupt_capability(adapter);
3472         iounmap(hw->hw_addr);
3473 err_ioremap:
3474         free_netdev(netdev);
3475 err_alloc_etherdev:
3476         pci_release_regions(pdev);
3477 err_pci_reg:
3478 err_dma:
3479         pci_disable_device(pdev);
3480         return err;
3481 }
3482
3483 /**
3484  * ixgbevf_remove - Device Removal Routine
3485  * @pdev: PCI device information struct
3486  *
3487  * ixgbevf_remove is called by the PCI subsystem to alert the driver
3488  * that it should release a PCI device.  The could be caused by a
3489  * Hot-Plug event, or because the driver is going to be removed from
3490  * memory.
3491  **/
3492 static void __devexit ixgbevf_remove(struct pci_dev *pdev)
3493 {
3494         struct net_device *netdev = pci_get_drvdata(pdev);
3495         struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3496
3497         set_bit(__IXGBEVF_DOWN, &adapter->state);
3498
3499         del_timer_sync(&adapter->watchdog_timer);
3500
3501         cancel_work_sync(&adapter->watchdog_task);
3502
3503         flush_scheduled_work();
3504
3505         if (adapter->netdev_registered) {
3506                 unregister_netdev(netdev);
3507                 adapter->netdev_registered = false;
3508         }
3509
3510         ixgbevf_reset_interrupt_capability(adapter);
3511
3512         iounmap(adapter->hw.hw_addr);
3513         pci_release_regions(pdev);
3514
3515         hw_dbg(&adapter->hw, "Remove complete\n");
3516
3517         kfree(adapter->tx_ring);
3518         kfree(adapter->rx_ring);
3519
3520         free_netdev(netdev);
3521
3522         pci_disable_device(pdev);
3523 }
3524
3525 static struct pci_driver ixgbevf_driver = {
3526         .name     = ixgbevf_driver_name,
3527         .id_table = ixgbevf_pci_tbl,
3528         .probe    = ixgbevf_probe,
3529         .remove   = __devexit_p(ixgbevf_remove),
3530         .shutdown = ixgbevf_shutdown,
3531 };
3532
3533 /**
3534  * ixgbe_init_module - Driver Registration Routine
3535  *
3536  * ixgbe_init_module is the first routine called when the driver is
3537  * loaded. All it does is register with the PCI subsystem.
3538  **/
3539 static int __init ixgbevf_init_module(void)
3540 {
3541         int ret;
3542         printk(KERN_INFO "ixgbevf: %s - version %s\n", ixgbevf_driver_string,
3543                ixgbevf_driver_version);
3544
3545         printk(KERN_INFO "%s\n", ixgbevf_copyright);
3546
3547         ret = pci_register_driver(&ixgbevf_driver);
3548         return ret;
3549 }
3550
3551 module_init(ixgbevf_init_module);
3552
3553 /**
3554  * ixgbe_exit_module - Driver Exit Cleanup Routine
3555  *
3556  * ixgbe_exit_module is called just before the driver is removed
3557  * from memory.
3558  **/
3559 static void __exit ixgbevf_exit_module(void)
3560 {
3561         pci_unregister_driver(&ixgbevf_driver);
3562 }
3563
3564 #ifdef DEBUG
3565 /**
3566  * ixgbe_get_hw_dev_name - return device name string
3567  * used by hardware layer to print debugging information
3568  **/
3569 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3570 {
3571         struct ixgbevf_adapter *adapter = hw->back;
3572         return adapter->netdev->name;
3573 }
3574
3575 #endif
3576 module_exit(ixgbevf_exit_module);
3577
3578 /* ixgbevf_main.c */