Merge branch 'topic/ca0106' into for-linus
[linux-2.6] / drivers / net / ixgbe / ixgbe_main.c
1 /*******************************************************************************
2
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2008 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 #include <linux/types.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/netdevice.h>
32 #include <linux/vmalloc.h>
33 #include <linux/string.h>
34 #include <linux/in.h>
35 #include <linux/ip.h>
36 #include <linux/tcp.h>
37 #include <linux/ipv6.h>
38 #include <net/checksum.h>
39 #include <net/ip6_checksum.h>
40 #include <linux/ethtool.h>
41 #include <linux/if_vlan.h>
42
43 #include "ixgbe.h"
44 #include "ixgbe_common.h"
45
46 char ixgbe_driver_name[] = "ixgbe";
47 static const char ixgbe_driver_string[] =
48                               "Intel(R) 10 Gigabit PCI Express Network Driver";
49
50 #define DRV_VERSION "1.3.30-k2"
51 const char ixgbe_driver_version[] = DRV_VERSION;
52 static char ixgbe_copyright[] = "Copyright (c) 1999-2007 Intel Corporation.";
53
54 static const struct ixgbe_info *ixgbe_info_tbl[] = {
55         [board_82598] = &ixgbe_82598_info,
56 };
57
58 /* ixgbe_pci_tbl - PCI Device ID Table
59  *
60  * Wildcard entries (PCI_ANY_ID) should come last
61  * Last entry must be all 0s
62  *
63  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64  *   Class, Class Mask, private data (not used) }
65  */
66 static struct pci_device_id ixgbe_pci_tbl[] = {
67         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_DUAL_PORT),
68          board_82598 },
69         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_SINGLE_PORT),
70          board_82598 },
71         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT),
72          board_82598 },
73         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_CX4),
74          board_82598 },
75         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_CX4_DUAL_PORT),
76          board_82598 },
77         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_DA_DUAL_PORT),
78          board_82598 },
79         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM),
80          board_82598 },
81         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_XF_LR),
82          board_82598 },
83         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_SFP_LOM),
84          board_82598 },
85
86         /* required last entry */
87         {0, }
88 };
89 MODULE_DEVICE_TABLE(pci, ixgbe_pci_tbl);
90
91 #ifdef CONFIG_IXGBE_DCA
92 static int ixgbe_notify_dca(struct notifier_block *, unsigned long event,
93                             void *p);
94 static struct notifier_block dca_notifier = {
95         .notifier_call = ixgbe_notify_dca,
96         .next          = NULL,
97         .priority      = 0
98 };
99 #endif
100
101 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
102 MODULE_DESCRIPTION("Intel(R) 10 Gigabit PCI Express Network Driver");
103 MODULE_LICENSE("GPL");
104 MODULE_VERSION(DRV_VERSION);
105
106 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
107
108 static void ixgbe_release_hw_control(struct ixgbe_adapter *adapter)
109 {
110         u32 ctrl_ext;
111
112         /* Let firmware take over control of h/w */
113         ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
114         IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
115                         ctrl_ext & ~IXGBE_CTRL_EXT_DRV_LOAD);
116 }
117
118 static void ixgbe_get_hw_control(struct ixgbe_adapter *adapter)
119 {
120         u32 ctrl_ext;
121
122         /* Let firmware know the driver has taken over */
123         ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
124         IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
125                         ctrl_ext | IXGBE_CTRL_EXT_DRV_LOAD);
126 }
127
128 static void ixgbe_set_ivar(struct ixgbe_adapter *adapter, u16 int_alloc_entry,
129                            u8 msix_vector)
130 {
131         u32 ivar, index;
132
133         msix_vector |= IXGBE_IVAR_ALLOC_VAL;
134         index = (int_alloc_entry >> 2) & 0x1F;
135         ivar = IXGBE_READ_REG(&adapter->hw, IXGBE_IVAR(index));
136         ivar &= ~(0xFF << (8 * (int_alloc_entry & 0x3)));
137         ivar |= (msix_vector << (8 * (int_alloc_entry & 0x3)));
138         IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
139 }
140
141 static void ixgbe_unmap_and_free_tx_resource(struct ixgbe_adapter *adapter,
142                                              struct ixgbe_tx_buffer
143                                              *tx_buffer_info)
144 {
145         if (tx_buffer_info->dma) {
146                 pci_unmap_page(adapter->pdev, tx_buffer_info->dma,
147                                tx_buffer_info->length, PCI_DMA_TODEVICE);
148                 tx_buffer_info->dma = 0;
149         }
150         if (tx_buffer_info->skb) {
151                 dev_kfree_skb_any(tx_buffer_info->skb);
152                 tx_buffer_info->skb = NULL;
153         }
154         /* tx_buffer_info must be completely set up in the transmit path */
155 }
156
157 static inline bool ixgbe_check_tx_hang(struct ixgbe_adapter *adapter,
158                                        struct ixgbe_ring *tx_ring,
159                                        unsigned int eop)
160 {
161         struct ixgbe_hw *hw = &adapter->hw;
162         u32 head, tail;
163
164         /* Detect a transmit hang in hardware, this serializes the
165          * check with the clearing of time_stamp and movement of eop */
166         head = IXGBE_READ_REG(hw, tx_ring->head);
167         tail = IXGBE_READ_REG(hw, tx_ring->tail);
168         adapter->detect_tx_hung = false;
169         if ((head != tail) &&
170             tx_ring->tx_buffer_info[eop].time_stamp &&
171             time_after(jiffies, tx_ring->tx_buffer_info[eop].time_stamp + HZ) &&
172             !(IXGBE_READ_REG(&adapter->hw, IXGBE_TFCS) & IXGBE_TFCS_TXOFF)) {
173                 /* detected Tx unit hang */
174                 union ixgbe_adv_tx_desc *tx_desc;
175                 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
176                 DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
177                         "  Tx Queue             <%d>\n"
178                         "  TDH, TDT             <%x>, <%x>\n"
179                         "  next_to_use          <%x>\n"
180                         "  next_to_clean        <%x>\n"
181                         "tx_buffer_info[next_to_clean]\n"
182                         "  time_stamp           <%lx>\n"
183                         "  jiffies              <%lx>\n",
184                         tx_ring->queue_index,
185                         head, tail,
186                         tx_ring->next_to_use, eop,
187                         tx_ring->tx_buffer_info[eop].time_stamp, jiffies);
188                 return true;
189         }
190
191         return false;
192 }
193
194 #define IXGBE_MAX_TXD_PWR       14
195 #define IXGBE_MAX_DATA_PER_TXD  (1 << IXGBE_MAX_TXD_PWR)
196
197 /* Tx Descriptors needed, worst case */
198 #define TXD_USE_COUNT(S) (((S) >> IXGBE_MAX_TXD_PWR) + \
199                          (((S) & (IXGBE_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
200 #define DESC_NEEDED (TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD) /* skb->data */ + \
201         MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1) /* for context */
202
203 #define GET_TX_HEAD_FROM_RING(ring) (\
204         *(volatile u32 *) \
205         ((union ixgbe_adv_tx_desc *)(ring)->desc + (ring)->count))
206 static void ixgbe_tx_timeout(struct net_device *netdev);
207
208 /**
209  * ixgbe_clean_tx_irq - Reclaim resources after transmit completes
210  * @adapter: board private structure
211  * @tx_ring: tx ring to clean
212  **/
213 static bool ixgbe_clean_tx_irq(struct ixgbe_adapter *adapter,
214                                struct ixgbe_ring *tx_ring)
215 {
216         union ixgbe_adv_tx_desc *tx_desc;
217         struct ixgbe_tx_buffer *tx_buffer_info;
218         struct net_device *netdev = adapter->netdev;
219         struct sk_buff *skb;
220         unsigned int i;
221         u32 head, oldhead;
222         unsigned int count = 0;
223         unsigned int total_bytes = 0, total_packets = 0;
224
225         rmb();
226         head = GET_TX_HEAD_FROM_RING(tx_ring);
227         head = le32_to_cpu(head);
228         i = tx_ring->next_to_clean;
229         while (1) {
230                 while (i != head) {
231                         tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
232                         tx_buffer_info = &tx_ring->tx_buffer_info[i];
233                         skb = tx_buffer_info->skb;
234
235                         if (skb) {
236                                 unsigned int segs, bytecount;
237
238                                 /* gso_segs is currently only valid for tcp */
239                                 segs = skb_shinfo(skb)->gso_segs ?: 1;
240                                 /* multiply data chunks by size of headers */
241                                 bytecount = ((segs - 1) * skb_headlen(skb)) +
242                                             skb->len;
243                                 total_packets += segs;
244                                 total_bytes += bytecount;
245                         }
246
247                         ixgbe_unmap_and_free_tx_resource(adapter,
248                                                          tx_buffer_info);
249
250                         i++;
251                         if (i == tx_ring->count)
252                                 i = 0;
253
254                         count++;
255                         if (count == tx_ring->count)
256                                 goto done_cleaning;
257                 }
258                 oldhead = head;
259                 rmb();
260                 head = GET_TX_HEAD_FROM_RING(tx_ring);
261                 head = le32_to_cpu(head);
262                 if (head == oldhead)
263                         goto done_cleaning;
264         } /* while (1) */
265
266 done_cleaning:
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                 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
277                     !test_bit(__IXGBE_DOWN, &adapter->state)) {
278                         netif_wake_subqueue(netdev, tx_ring->queue_index);
279                         ++adapter->restart_queue;
280                 }
281         }
282
283         if (adapter->detect_tx_hung) {
284                 if (ixgbe_check_tx_hang(adapter, tx_ring, i)) {
285                         /* schedule immediate reset if we believe we hung */
286                         DPRINTK(PROBE, INFO,
287                                 "tx hang %d detected, resetting adapter\n",
288                                 adapter->tx_timeout_count + 1);
289                         ixgbe_tx_timeout(adapter->netdev);
290                 }
291         }
292
293         /* re-arm the interrupt */
294         if ((total_packets >= tx_ring->work_limit) ||
295             (count == tx_ring->count))
296                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, tx_ring->v_idx);
297
298         tx_ring->total_bytes += total_bytes;
299         tx_ring->total_packets += total_packets;
300         tx_ring->stats.bytes += total_bytes;
301         tx_ring->stats.packets += total_packets;
302         adapter->net_stats.tx_bytes += total_bytes;
303         adapter->net_stats.tx_packets += total_packets;
304         return (total_packets ? true : false);
305 }
306
307 #ifdef CONFIG_IXGBE_DCA
308 static void ixgbe_update_rx_dca(struct ixgbe_adapter *adapter,
309                                 struct ixgbe_ring *rx_ring)
310 {
311         u32 rxctrl;
312         int cpu = get_cpu();
313         int q = rx_ring - adapter->rx_ring;
314
315         if (rx_ring->cpu != cpu) {
316                 rxctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_DCA_RXCTRL(q));
317                 rxctrl &= ~IXGBE_DCA_RXCTRL_CPUID_MASK;
318                 rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
319                 rxctrl |= IXGBE_DCA_RXCTRL_DESC_DCA_EN;
320                 rxctrl |= IXGBE_DCA_RXCTRL_HEAD_DCA_EN;
321                 rxctrl &= ~(IXGBE_DCA_RXCTRL_DESC_RRO_EN);
322                 rxctrl &= ~(IXGBE_DCA_RXCTRL_DESC_WRO_EN |
323                             IXGBE_DCA_RXCTRL_DESC_HSRO_EN);
324                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_RXCTRL(q), rxctrl);
325                 rx_ring->cpu = cpu;
326         }
327         put_cpu();
328 }
329
330 static void ixgbe_update_tx_dca(struct ixgbe_adapter *adapter,
331                                 struct ixgbe_ring *tx_ring)
332 {
333         u32 txctrl;
334         int cpu = get_cpu();
335         int q = tx_ring - adapter->tx_ring;
336
337         if (tx_ring->cpu != cpu) {
338                 txctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_DCA_TXCTRL(q));
339                 txctrl &= ~IXGBE_DCA_TXCTRL_CPUID_MASK;
340                 txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
341                 txctrl |= IXGBE_DCA_TXCTRL_DESC_DCA_EN;
342                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_TXCTRL(q), txctrl);
343                 tx_ring->cpu = cpu;
344         }
345         put_cpu();
346 }
347
348 static void ixgbe_setup_dca(struct ixgbe_adapter *adapter)
349 {
350         int i;
351
352         if (!(adapter->flags & IXGBE_FLAG_DCA_ENABLED))
353                 return;
354
355         for (i = 0; i < adapter->num_tx_queues; i++) {
356                 adapter->tx_ring[i].cpu = -1;
357                 ixgbe_update_tx_dca(adapter, &adapter->tx_ring[i]);
358         }
359         for (i = 0; i < adapter->num_rx_queues; i++) {
360                 adapter->rx_ring[i].cpu = -1;
361                 ixgbe_update_rx_dca(adapter, &adapter->rx_ring[i]);
362         }
363 }
364
365 static int __ixgbe_notify_dca(struct device *dev, void *data)
366 {
367         struct net_device *netdev = dev_get_drvdata(dev);
368         struct ixgbe_adapter *adapter = netdev_priv(netdev);
369         unsigned long event = *(unsigned long *)data;
370
371         switch (event) {
372         case DCA_PROVIDER_ADD:
373                 /* if we're already enabled, don't do it again */
374                 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
375                         break;
376                 /* Always use CB2 mode, difference is masked
377                  * in the CB driver. */
378                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 2);
379                 if (dca_add_requester(dev) == 0) {
380                         adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
381                         ixgbe_setup_dca(adapter);
382                         break;
383                 }
384                 /* Fall Through since DCA is disabled. */
385         case DCA_PROVIDER_REMOVE:
386                 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
387                         dca_remove_requester(dev);
388                         adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
389                         IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
390                 }
391                 break;
392         }
393
394         return 0;
395 }
396
397 #endif /* CONFIG_IXGBE_DCA */
398 /**
399  * ixgbe_receive_skb - Send a completed packet up the stack
400  * @adapter: board private structure
401  * @skb: packet to send up
402  * @status: hardware indication of status of receive
403  * @rx_ring: rx descriptor ring (for a specific queue) to setup
404  * @rx_desc: rx descriptor
405  **/
406 static void ixgbe_receive_skb(struct ixgbe_adapter *adapter,
407                               struct sk_buff *skb, u8 status,
408                               struct ixgbe_ring *ring,
409                               union ixgbe_adv_rx_desc *rx_desc)
410 {
411         bool is_vlan = (status & IXGBE_RXD_STAT_VP);
412         u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
413
414         if (adapter->netdev->features & NETIF_F_LRO &&
415             skb->ip_summed == CHECKSUM_UNNECESSARY) {
416                 if (adapter->vlgrp && is_vlan && (tag != 0))
417                         lro_vlan_hwaccel_receive_skb(&ring->lro_mgr, skb,
418                                                      adapter->vlgrp, tag,
419                                                      rx_desc);
420                 else
421                         lro_receive_skb(&ring->lro_mgr, skb, rx_desc);
422                 ring->lro_used = true;
423         } else {
424                 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL)) {
425                         if (adapter->vlgrp && is_vlan && (tag != 0))
426                                 vlan_hwaccel_receive_skb(skb, adapter->vlgrp, tag);
427                         else
428                                 netif_receive_skb(skb);
429                 } else {
430                         if (adapter->vlgrp && is_vlan && (tag != 0))
431                                 vlan_hwaccel_rx(skb, adapter->vlgrp, tag);
432                         else
433                                 netif_rx(skb);
434                 }
435         }
436 }
437
438 /**
439  * ixgbe_rx_checksum - indicate in skb if hw indicated a good cksum
440  * @adapter: address of board private structure
441  * @status_err: hardware indication of status of receive
442  * @skb: skb currently being received and modified
443  **/
444 static inline void ixgbe_rx_checksum(struct ixgbe_adapter *adapter,
445                                      u32 status_err, struct sk_buff *skb)
446 {
447         skb->ip_summed = CHECKSUM_NONE;
448
449         /* Rx csum disabled */
450         if (!(adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED))
451                 return;
452
453         /* if IP and error */
454         if ((status_err & IXGBE_RXD_STAT_IPCS) &&
455             (status_err & IXGBE_RXDADV_ERR_IPE)) {
456                 adapter->hw_csum_rx_error++;
457                 return;
458         }
459
460         if (!(status_err & IXGBE_RXD_STAT_L4CS))
461                 return;
462
463         if (status_err & IXGBE_RXDADV_ERR_TCPE) {
464                 adapter->hw_csum_rx_error++;
465                 return;
466         }
467
468         /* It must be a TCP or UDP packet with a valid checksum */
469         skb->ip_summed = CHECKSUM_UNNECESSARY;
470         adapter->hw_csum_rx_good++;
471 }
472
473 /**
474  * ixgbe_alloc_rx_buffers - Replace used receive buffers; packet split
475  * @adapter: address of board private structure
476  **/
477 static void ixgbe_alloc_rx_buffers(struct ixgbe_adapter *adapter,
478                                    struct ixgbe_ring *rx_ring,
479                                    int cleaned_count)
480 {
481         struct pci_dev *pdev = adapter->pdev;
482         union ixgbe_adv_rx_desc *rx_desc;
483         struct ixgbe_rx_buffer *bi;
484         unsigned int i;
485
486         i = rx_ring->next_to_use;
487         bi = &rx_ring->rx_buffer_info[i];
488
489         while (cleaned_count--) {
490                 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
491
492                 if (!bi->page_dma &&
493                     (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED)) {
494                         if (!bi->page) {
495                                 bi->page = alloc_page(GFP_ATOMIC);
496                                 if (!bi->page) {
497                                         adapter->alloc_rx_page_failed++;
498                                         goto no_buffers;
499                                 }
500                                 bi->page_offset = 0;
501                         } else {
502                                 /* use a half page if we're re-using */
503                                 bi->page_offset ^= (PAGE_SIZE / 2);
504                         }
505
506                         bi->page_dma = pci_map_page(pdev, bi->page,
507                                                     bi->page_offset,
508                                                     (PAGE_SIZE / 2),
509                                                     PCI_DMA_FROMDEVICE);
510                 }
511
512                 if (!bi->skb) {
513                         struct sk_buff *skb;
514                         skb = netdev_alloc_skb(adapter->netdev,
515                                                (rx_ring->rx_buf_len +
516                                                 NET_IP_ALIGN));
517
518                         if (!skb) {
519                                 adapter->alloc_rx_buff_failed++;
520                                 goto no_buffers;
521                         }
522
523                         /*
524                          * Make buffer alignment 2 beyond a 16 byte boundary
525                          * this will result in a 16 byte aligned IP header after
526                          * the 14 byte MAC header is removed
527                          */
528                         skb_reserve(skb, NET_IP_ALIGN);
529
530                         bi->skb = skb;
531                         bi->dma = pci_map_single(pdev, skb->data,
532                                                  rx_ring->rx_buf_len,
533                                                  PCI_DMA_FROMDEVICE);
534                 }
535                 /* Refresh the desc even if buffer_addrs didn't change because
536                  * each write-back erases this info. */
537                 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
538                         rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
539                         rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
540                 } else {
541                         rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
542                 }
543
544                 i++;
545                 if (i == rx_ring->count)
546                         i = 0;
547                 bi = &rx_ring->rx_buffer_info[i];
548         }
549
550 no_buffers:
551         if (rx_ring->next_to_use != i) {
552                 rx_ring->next_to_use = i;
553                 if (i-- == 0)
554                         i = (rx_ring->count - 1);
555
556                 /*
557                  * Force memory writes to complete before letting h/w
558                  * know there are new descriptors to fetch.  (Only
559                  * applicable for weak-ordered memory model archs,
560                  * such as IA-64).
561                  */
562                 wmb();
563                 writel(i, adapter->hw.hw_addr + rx_ring->tail);
564         }
565 }
566
567 static inline u16 ixgbe_get_hdr_info(union ixgbe_adv_rx_desc *rx_desc)
568 {
569         return rx_desc->wb.lower.lo_dword.hs_rss.hdr_info;
570 }
571
572 static inline u16 ixgbe_get_pkt_info(union ixgbe_adv_rx_desc *rx_desc)
573 {
574         return rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
575 }
576
577 static bool ixgbe_clean_rx_irq(struct ixgbe_adapter *adapter,
578                                struct ixgbe_ring *rx_ring,
579                                int *work_done, int work_to_do)
580 {
581         struct pci_dev *pdev = adapter->pdev;
582         union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
583         struct ixgbe_rx_buffer *rx_buffer_info, *next_buffer;
584         struct sk_buff *skb;
585         unsigned int i;
586         u32 len, staterr;
587         u16 hdr_info;
588         bool cleaned = false;
589         int cleaned_count = 0;
590         unsigned int total_rx_bytes = 0, total_rx_packets = 0;
591
592         i = rx_ring->next_to_clean;
593         rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
594         staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
595         rx_buffer_info = &rx_ring->rx_buffer_info[i];
596
597         while (staterr & IXGBE_RXD_STAT_DD) {
598                 u32 upper_len = 0;
599                 if (*work_done >= work_to_do)
600                         break;
601                 (*work_done)++;
602
603                 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
604                         hdr_info = le16_to_cpu(ixgbe_get_hdr_info(rx_desc));
605                         len = (hdr_info & IXGBE_RXDADV_HDRBUFLEN_MASK) >>
606                                IXGBE_RXDADV_HDRBUFLEN_SHIFT;
607                         if (hdr_info & IXGBE_RXDADV_SPH)
608                                 adapter->rx_hdr_split++;
609                         if (len > IXGBE_RX_HDR_SIZE)
610                                 len = IXGBE_RX_HDR_SIZE;
611                         upper_len = le16_to_cpu(rx_desc->wb.upper.length);
612                 } else {
613                         len = le16_to_cpu(rx_desc->wb.upper.length);
614                 }
615
616                 cleaned = true;
617                 skb = rx_buffer_info->skb;
618                 prefetch(skb->data - NET_IP_ALIGN);
619                 rx_buffer_info->skb = NULL;
620
621                 if (len && !skb_shinfo(skb)->nr_frags) {
622                         pci_unmap_single(pdev, rx_buffer_info->dma,
623                                          rx_ring->rx_buf_len,
624                                          PCI_DMA_FROMDEVICE);
625                         skb_put(skb, len);
626                 }
627
628                 if (upper_len) {
629                         pci_unmap_page(pdev, rx_buffer_info->page_dma,
630                                        PAGE_SIZE / 2, PCI_DMA_FROMDEVICE);
631                         rx_buffer_info->page_dma = 0;
632                         skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
633                                            rx_buffer_info->page,
634                                            rx_buffer_info->page_offset,
635                                            upper_len);
636
637                         if ((rx_ring->rx_buf_len > (PAGE_SIZE / 2)) ||
638                             (page_count(rx_buffer_info->page) != 1))
639                                 rx_buffer_info->page = NULL;
640                         else
641                                 get_page(rx_buffer_info->page);
642
643                         skb->len += upper_len;
644                         skb->data_len += upper_len;
645                         skb->truesize += upper_len;
646                 }
647
648                 i++;
649                 if (i == rx_ring->count)
650                         i = 0;
651                 next_buffer = &rx_ring->rx_buffer_info[i];
652
653                 next_rxd = IXGBE_RX_DESC_ADV(*rx_ring, i);
654                 prefetch(next_rxd);
655
656                 cleaned_count++;
657                 if (staterr & IXGBE_RXD_STAT_EOP) {
658                         rx_ring->stats.packets++;
659                         rx_ring->stats.bytes += skb->len;
660                 } else {
661                         rx_buffer_info->skb = next_buffer->skb;
662                         rx_buffer_info->dma = next_buffer->dma;
663                         next_buffer->skb = skb;
664                         next_buffer->dma = 0;
665                         adapter->non_eop_descs++;
666                         goto next_desc;
667                 }
668
669                 if (staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK) {
670                         dev_kfree_skb_irq(skb);
671                         goto next_desc;
672                 }
673
674                 ixgbe_rx_checksum(adapter, staterr, skb);
675
676                 /* probably a little skewed due to removing CRC */
677                 total_rx_bytes += skb->len;
678                 total_rx_packets++;
679
680                 skb->protocol = eth_type_trans(skb, adapter->netdev);
681                 ixgbe_receive_skb(adapter, skb, staterr, rx_ring, rx_desc);
682
683 next_desc:
684                 rx_desc->wb.upper.status_error = 0;
685
686                 /* return some buffers to hardware, one at a time is too slow */
687                 if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
688                         ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
689                         cleaned_count = 0;
690                 }
691
692                 /* use prefetched values */
693                 rx_desc = next_rxd;
694                 rx_buffer_info = next_buffer;
695
696                 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
697         }
698
699         if (rx_ring->lro_used) {
700                 lro_flush_all(&rx_ring->lro_mgr);
701                 rx_ring->lro_used = false;
702         }
703
704         rx_ring->next_to_clean = i;
705         cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
706
707         if (cleaned_count)
708                 ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
709
710         rx_ring->total_packets += total_rx_packets;
711         rx_ring->total_bytes += total_rx_bytes;
712         adapter->net_stats.rx_bytes += total_rx_bytes;
713         adapter->net_stats.rx_packets += total_rx_packets;
714
715         return cleaned;
716 }
717
718 static int ixgbe_clean_rxonly(struct napi_struct *, int);
719 /**
720  * ixgbe_configure_msix - Configure MSI-X hardware
721  * @adapter: board private structure
722  *
723  * ixgbe_configure_msix sets up the hardware to properly generate MSI-X
724  * interrupts.
725  **/
726 static void ixgbe_configure_msix(struct ixgbe_adapter *adapter)
727 {
728         struct ixgbe_q_vector *q_vector;
729         int i, j, q_vectors, v_idx, r_idx;
730         u32 mask;
731
732         q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
733
734         /* Populate the IVAR table and set the ITR values to the
735          * corresponding register.
736          */
737         for (v_idx = 0; v_idx < q_vectors; v_idx++) {
738                 q_vector = &adapter->q_vector[v_idx];
739                 /* XXX for_each_bit(...) */
740                 r_idx = find_first_bit(q_vector->rxr_idx,
741                                        adapter->num_rx_queues);
742
743                 for (i = 0; i < q_vector->rxr_count; i++) {
744                         j = adapter->rx_ring[r_idx].reg_idx;
745                         ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(j), v_idx);
746                         r_idx = find_next_bit(q_vector->rxr_idx,
747                                               adapter->num_rx_queues,
748                                               r_idx + 1);
749                 }
750                 r_idx = find_first_bit(q_vector->txr_idx,
751                                        adapter->num_tx_queues);
752
753                 for (i = 0; i < q_vector->txr_count; i++) {
754                         j = adapter->tx_ring[r_idx].reg_idx;
755                         ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(j), v_idx);
756                         r_idx = find_next_bit(q_vector->txr_idx,
757                                               adapter->num_tx_queues,
758                                               r_idx + 1);
759                 }
760
761                 /* if this is a tx only vector halve the interrupt rate */
762                 if (q_vector->txr_count && !q_vector->rxr_count)
763                         q_vector->eitr = (adapter->eitr_param >> 1);
764                 else
765                         /* rx only */
766                         q_vector->eitr = adapter->eitr_param;
767
768                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx),
769                                 EITR_INTS_PER_SEC_TO_REG(q_vector->eitr));
770         }
771
772         ixgbe_set_ivar(adapter, IXGBE_IVAR_OTHER_CAUSES_INDEX, v_idx);
773         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx), 1950);
774
775         /* set up to autoclear timer, and the vectors */
776         mask = IXGBE_EIMS_ENABLE_MASK;
777         mask &= ~(IXGBE_EIMS_OTHER | IXGBE_EIMS_LSC);
778         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, mask);
779 }
780
781 enum latency_range {
782         lowest_latency = 0,
783         low_latency = 1,
784         bulk_latency = 2,
785         latency_invalid = 255
786 };
787
788 /**
789  * ixgbe_update_itr - update the dynamic ITR value based on statistics
790  * @adapter: pointer to adapter
791  * @eitr: eitr setting (ints per sec) to give last timeslice
792  * @itr_setting: current throttle rate in ints/second
793  * @packets: the number of packets during this measurement interval
794  * @bytes: the number of bytes during this measurement interval
795  *
796  *      Stores a new ITR value based on packets and byte
797  *      counts during the last interrupt.  The advantage of per interrupt
798  *      computation is faster updates and more accurate ITR for the current
799  *      traffic pattern.  Constants in this function were computed
800  *      based on theoretical maximum wire speed and thresholds were set based
801  *      on testing data as well as attempting to minimize response time
802  *      while increasing bulk throughput.
803  *      this functionality is controlled by the InterruptThrottleRate module
804  *      parameter (see ixgbe_param.c)
805  **/
806 static u8 ixgbe_update_itr(struct ixgbe_adapter *adapter,
807                            u32 eitr, u8 itr_setting,
808                            int packets, int bytes)
809 {
810         unsigned int retval = itr_setting;
811         u32 timepassed_us;
812         u64 bytes_perint;
813
814         if (packets == 0)
815                 goto update_itr_done;
816
817
818         /* simple throttlerate management
819          *    0-20MB/s lowest (100000 ints/s)
820          *   20-100MB/s low   (20000 ints/s)
821          *  100-1249MB/s bulk (8000 ints/s)
822          */
823         /* what was last interrupt timeslice? */
824         timepassed_us = 1000000/eitr;
825         bytes_perint = bytes / timepassed_us; /* bytes/usec */
826
827         switch (itr_setting) {
828         case lowest_latency:
829                 if (bytes_perint > adapter->eitr_low)
830                         retval = low_latency;
831                 break;
832         case low_latency:
833                 if (bytes_perint > adapter->eitr_high)
834                         retval = bulk_latency;
835                 else if (bytes_perint <= adapter->eitr_low)
836                         retval = lowest_latency;
837                 break;
838         case bulk_latency:
839                 if (bytes_perint <= adapter->eitr_high)
840                         retval = low_latency;
841                 break;
842         }
843
844 update_itr_done:
845         return retval;
846 }
847
848 static void ixgbe_set_itr_msix(struct ixgbe_q_vector *q_vector)
849 {
850         struct ixgbe_adapter *adapter = q_vector->adapter;
851         struct ixgbe_hw *hw = &adapter->hw;
852         u32 new_itr;
853         u8 current_itr, ret_itr;
854         int i, r_idx, v_idx = ((void *)q_vector - (void *)(adapter->q_vector)) /
855                                sizeof(struct ixgbe_q_vector);
856         struct ixgbe_ring *rx_ring, *tx_ring;
857
858         r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
859         for (i = 0; i < q_vector->txr_count; i++) {
860                 tx_ring = &(adapter->tx_ring[r_idx]);
861                 ret_itr = ixgbe_update_itr(adapter, q_vector->eitr,
862                                            q_vector->tx_itr,
863                                            tx_ring->total_packets,
864                                            tx_ring->total_bytes);
865                 /* if the result for this queue would decrease interrupt
866                  * rate for this vector then use that result */
867                 q_vector->tx_itr = ((q_vector->tx_itr > ret_itr) ?
868                                     q_vector->tx_itr - 1 : ret_itr);
869                 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
870                                       r_idx + 1);
871         }
872
873         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
874         for (i = 0; i < q_vector->rxr_count; i++) {
875                 rx_ring = &(adapter->rx_ring[r_idx]);
876                 ret_itr = ixgbe_update_itr(adapter, q_vector->eitr,
877                                            q_vector->rx_itr,
878                                            rx_ring->total_packets,
879                                            rx_ring->total_bytes);
880                 /* if the result for this queue would decrease interrupt
881                  * rate for this vector then use that result */
882                 q_vector->rx_itr = ((q_vector->rx_itr > ret_itr) ?
883                                     q_vector->rx_itr - 1 : ret_itr);
884                 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
885                                       r_idx + 1);
886         }
887
888         current_itr = max(q_vector->rx_itr, q_vector->tx_itr);
889
890         switch (current_itr) {
891         /* counts and packets in update_itr are dependent on these numbers */
892         case lowest_latency:
893                 new_itr = 100000;
894                 break;
895         case low_latency:
896                 new_itr = 20000; /* aka hwitr = ~200 */
897                 break;
898         case bulk_latency:
899         default:
900                 new_itr = 8000;
901                 break;
902         }
903
904         if (new_itr != q_vector->eitr) {
905                 u32 itr_reg;
906                 /* do an exponential smoothing */
907                 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
908                 q_vector->eitr = new_itr;
909                 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
910                 /* must write high and low 16 bits to reset counter */
911                 DPRINTK(TX_ERR, DEBUG, "writing eitr(%d): %08X\n", v_idx,
912                         itr_reg);
913                 IXGBE_WRITE_REG(hw, IXGBE_EITR(v_idx), itr_reg | (itr_reg)<<16);
914         }
915
916         return;
917 }
918
919 static void ixgbe_check_fan_failure(struct ixgbe_adapter *adapter, u32 eicr)
920 {
921         struct ixgbe_hw *hw = &adapter->hw;
922
923         if ((adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) &&
924             (eicr & IXGBE_EICR_GPI_SDP1)) {
925                 DPRINTK(PROBE, CRIT, "Fan has stopped, replace the adapter\n");
926                 /* write to clear the interrupt */
927                 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
928         }
929 }
930
931 static void ixgbe_check_lsc(struct ixgbe_adapter *adapter)
932 {
933         struct ixgbe_hw *hw = &adapter->hw;
934
935         adapter->lsc_int++;
936         adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
937         adapter->link_check_timeout = jiffies;
938         if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
939                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
940                 schedule_work(&adapter->watchdog_task);
941         }
942 }
943
944 static irqreturn_t ixgbe_msix_lsc(int irq, void *data)
945 {
946         struct net_device *netdev = data;
947         struct ixgbe_adapter *adapter = netdev_priv(netdev);
948         struct ixgbe_hw *hw = &adapter->hw;
949         u32 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
950
951         if (eicr & IXGBE_EICR_LSC)
952                 ixgbe_check_lsc(adapter);
953
954         ixgbe_check_fan_failure(adapter, eicr);
955
956         if (!test_bit(__IXGBE_DOWN, &adapter->state))
957                 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
958
959         return IRQ_HANDLED;
960 }
961
962 static irqreturn_t ixgbe_msix_clean_tx(int irq, void *data)
963 {
964         struct ixgbe_q_vector *q_vector = data;
965         struct ixgbe_adapter  *adapter = q_vector->adapter;
966         struct ixgbe_ring     *tx_ring;
967         int i, r_idx;
968
969         if (!q_vector->txr_count)
970                 return IRQ_HANDLED;
971
972         r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
973         for (i = 0; i < q_vector->txr_count; i++) {
974                 tx_ring = &(adapter->tx_ring[r_idx]);
975 #ifdef CONFIG_IXGBE_DCA
976                 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
977                         ixgbe_update_tx_dca(adapter, tx_ring);
978 #endif
979                 tx_ring->total_bytes = 0;
980                 tx_ring->total_packets = 0;
981                 ixgbe_clean_tx_irq(adapter, tx_ring);
982                 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
983                                       r_idx + 1);
984         }
985
986         return IRQ_HANDLED;
987 }
988
989 /**
990  * ixgbe_msix_clean_rx - single unshared vector rx clean (all queues)
991  * @irq: unused
992  * @data: pointer to our q_vector struct for this interrupt vector
993  **/
994 static irqreturn_t ixgbe_msix_clean_rx(int irq, void *data)
995 {
996         struct ixgbe_q_vector *q_vector = data;
997         struct ixgbe_adapter  *adapter = q_vector->adapter;
998         struct ixgbe_ring  *rx_ring;
999         int r_idx;
1000         int i;
1001
1002         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1003         for (i = 0;  i < q_vector->rxr_count; i++) {
1004                 rx_ring = &(adapter->rx_ring[r_idx]);
1005                 rx_ring->total_bytes = 0;
1006                 rx_ring->total_packets = 0;
1007                 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
1008                                       r_idx + 1);
1009         }
1010
1011         if (!q_vector->rxr_count)
1012                 return IRQ_HANDLED;
1013
1014         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1015         rx_ring = &(adapter->rx_ring[r_idx]);
1016         /* disable interrupts on this vector only */
1017         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, rx_ring->v_idx);
1018         netif_rx_schedule(&q_vector->napi);
1019
1020         return IRQ_HANDLED;
1021 }
1022
1023 static irqreturn_t ixgbe_msix_clean_many(int irq, void *data)
1024 {
1025         ixgbe_msix_clean_rx(irq, data);
1026         ixgbe_msix_clean_tx(irq, data);
1027
1028         return IRQ_HANDLED;
1029 }
1030
1031 /**
1032  * ixgbe_clean_rxonly - msix (aka one shot) rx clean routine
1033  * @napi: napi struct with our devices info in it
1034  * @budget: amount of work driver is allowed to do this pass, in packets
1035  *
1036  * This function is optimized for cleaning one queue only on a single
1037  * q_vector!!!
1038  **/
1039 static int ixgbe_clean_rxonly(struct napi_struct *napi, int budget)
1040 {
1041         struct ixgbe_q_vector *q_vector =
1042                                container_of(napi, struct ixgbe_q_vector, napi);
1043         struct ixgbe_adapter *adapter = q_vector->adapter;
1044         struct ixgbe_ring *rx_ring = NULL;
1045         int work_done = 0;
1046         long r_idx;
1047
1048         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1049         rx_ring = &(adapter->rx_ring[r_idx]);
1050 #ifdef CONFIG_IXGBE_DCA
1051         if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1052                 ixgbe_update_rx_dca(adapter, rx_ring);
1053 #endif
1054
1055         ixgbe_clean_rx_irq(adapter, rx_ring, &work_done, budget);
1056
1057         /* If all Rx work done, exit the polling mode */
1058         if (work_done < budget) {
1059                 netif_rx_complete(napi);
1060                 if (adapter->itr_setting & 3)
1061                         ixgbe_set_itr_msix(q_vector);
1062                 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1063                         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, rx_ring->v_idx);
1064         }
1065
1066         return work_done;
1067 }
1068
1069 /**
1070  * ixgbe_clean_rxonly_many - msix (aka one shot) rx clean routine
1071  * @napi: napi struct with our devices info in it
1072  * @budget: amount of work driver is allowed to do this pass, in packets
1073  *
1074  * This function will clean more than one rx queue associated with a
1075  * q_vector.
1076  **/
1077 static int ixgbe_clean_rxonly_many(struct napi_struct *napi, int budget)
1078 {
1079         struct ixgbe_q_vector *q_vector =
1080                                container_of(napi, struct ixgbe_q_vector, napi);
1081         struct ixgbe_adapter *adapter = q_vector->adapter;
1082         struct ixgbe_ring *rx_ring = NULL;
1083         int work_done = 0, i;
1084         long r_idx;
1085         u16 enable_mask = 0;
1086
1087         /* attempt to distribute budget to each queue fairly, but don't allow
1088          * the budget to go below 1 because we'll exit polling */
1089         budget /= (q_vector->rxr_count ?: 1);
1090         budget = max(budget, 1);
1091         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1092         for (i = 0; i < q_vector->rxr_count; i++) {
1093                 rx_ring = &(adapter->rx_ring[r_idx]);
1094 #ifdef CONFIG_IXGBE_DCA
1095                 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1096                         ixgbe_update_rx_dca(adapter, rx_ring);
1097 #endif
1098                 ixgbe_clean_rx_irq(adapter, rx_ring, &work_done, budget);
1099                 enable_mask |= rx_ring->v_idx;
1100                 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
1101                                       r_idx + 1);
1102         }
1103
1104         r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1105         rx_ring = &(adapter->rx_ring[r_idx]);
1106         /* If all Rx work done, exit the polling mode */
1107         if (work_done < budget) {
1108                 netif_rx_complete(napi);
1109                 if (adapter->itr_setting & 3)
1110                         ixgbe_set_itr_msix(q_vector);
1111                 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1112                         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, enable_mask);
1113                 return 0;
1114         }
1115
1116         return work_done;
1117 }
1118 static inline void map_vector_to_rxq(struct ixgbe_adapter *a, int v_idx,
1119                                      int r_idx)
1120 {
1121         a->q_vector[v_idx].adapter = a;
1122         set_bit(r_idx, a->q_vector[v_idx].rxr_idx);
1123         a->q_vector[v_idx].rxr_count++;
1124         a->rx_ring[r_idx].v_idx = 1 << v_idx;
1125 }
1126
1127 static inline void map_vector_to_txq(struct ixgbe_adapter *a, int v_idx,
1128                                      int r_idx)
1129 {
1130         a->q_vector[v_idx].adapter = a;
1131         set_bit(r_idx, a->q_vector[v_idx].txr_idx);
1132         a->q_vector[v_idx].txr_count++;
1133         a->tx_ring[r_idx].v_idx = 1 << v_idx;
1134 }
1135
1136 /**
1137  * ixgbe_map_rings_to_vectors - Maps descriptor rings to vectors
1138  * @adapter: board private structure to initialize
1139  * @vectors: allotted vector count for descriptor rings
1140  *
1141  * This function maps descriptor rings to the queue-specific vectors
1142  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
1143  * one vector per ring/queue, but on a constrained vector budget, we
1144  * group the rings as "efficiently" as possible.  You would add new
1145  * mapping configurations in here.
1146  **/
1147 static int ixgbe_map_rings_to_vectors(struct ixgbe_adapter *adapter,
1148                                       int vectors)
1149 {
1150         int v_start = 0;
1151         int rxr_idx = 0, txr_idx = 0;
1152         int rxr_remaining = adapter->num_rx_queues;
1153         int txr_remaining = adapter->num_tx_queues;
1154         int i, j;
1155         int rqpv, tqpv;
1156         int err = 0;
1157
1158         /* No mapping required if MSI-X is disabled. */
1159         if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1160                 goto out;
1161
1162         /*
1163          * The ideal configuration...
1164          * We have enough vectors to map one per queue.
1165          */
1166         if (vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
1167                 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
1168                         map_vector_to_rxq(adapter, v_start, rxr_idx);
1169
1170                 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
1171                         map_vector_to_txq(adapter, v_start, txr_idx);
1172
1173                 goto out;
1174         }
1175
1176         /*
1177          * If we don't have enough vectors for a 1-to-1
1178          * mapping, we'll have to group them so there are
1179          * multiple queues per vector.
1180          */
1181         /* Re-adjusting *qpv takes care of the remainder. */
1182         for (i = v_start; i < vectors; i++) {
1183                 rqpv = DIV_ROUND_UP(rxr_remaining, vectors - i);
1184                 for (j = 0; j < rqpv; j++) {
1185                         map_vector_to_rxq(adapter, i, rxr_idx);
1186                         rxr_idx++;
1187                         rxr_remaining--;
1188                 }
1189         }
1190         for (i = v_start; i < vectors; i++) {
1191                 tqpv = DIV_ROUND_UP(txr_remaining, vectors - i);
1192                 for (j = 0; j < tqpv; j++) {
1193                         map_vector_to_txq(adapter, i, txr_idx);
1194                         txr_idx++;
1195                         txr_remaining--;
1196                 }
1197         }
1198
1199 out:
1200         return err;
1201 }
1202
1203 /**
1204  * ixgbe_request_msix_irqs - Initialize MSI-X interrupts
1205  * @adapter: board private structure
1206  *
1207  * ixgbe_request_msix_irqs allocates MSI-X vectors and requests
1208  * interrupts from the kernel.
1209  **/
1210 static int ixgbe_request_msix_irqs(struct ixgbe_adapter *adapter)
1211 {
1212         struct net_device *netdev = adapter->netdev;
1213         irqreturn_t (*handler)(int, void *);
1214         int i, vector, q_vectors, err;
1215         int ri=0, ti=0;
1216
1217         /* Decrement for Other and TCP Timer vectors */
1218         q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1219
1220         /* Map the Tx/Rx rings to the vectors we were allotted. */
1221         err = ixgbe_map_rings_to_vectors(adapter, q_vectors);
1222         if (err)
1223                 goto out;
1224
1225 #define SET_HANDLER(_v) ((!(_v)->rxr_count) ? &ixgbe_msix_clean_tx : \
1226                          (!(_v)->txr_count) ? &ixgbe_msix_clean_rx : \
1227                          &ixgbe_msix_clean_many)
1228         for (vector = 0; vector < q_vectors; vector++) {
1229                 handler = SET_HANDLER(&adapter->q_vector[vector]);
1230
1231                 if(handler == &ixgbe_msix_clean_rx) {
1232                         sprintf(adapter->name[vector], "%s-%s-%d",
1233                                 netdev->name, "rx", ri++);
1234                 }
1235                 else if(handler == &ixgbe_msix_clean_tx) {
1236                         sprintf(adapter->name[vector], "%s-%s-%d",
1237                                 netdev->name, "tx", ti++);
1238                 }
1239                 else
1240                         sprintf(adapter->name[vector], "%s-%s-%d",
1241                                 netdev->name, "TxRx", vector);
1242
1243                 err = request_irq(adapter->msix_entries[vector].vector,
1244                                   handler, 0, adapter->name[vector],
1245                                   &(adapter->q_vector[vector]));
1246                 if (err) {
1247                         DPRINTK(PROBE, ERR,
1248                                 "request_irq failed for MSIX interrupt "
1249                                 "Error: %d\n", err);
1250                         goto free_queue_irqs;
1251                 }
1252         }
1253
1254         sprintf(adapter->name[vector], "%s:lsc", netdev->name);
1255         err = request_irq(adapter->msix_entries[vector].vector,
1256                           &ixgbe_msix_lsc, 0, adapter->name[vector], netdev);
1257         if (err) {
1258                 DPRINTK(PROBE, ERR,
1259                         "request_irq for msix_lsc failed: %d\n", err);
1260                 goto free_queue_irqs;
1261         }
1262
1263         return 0;
1264
1265 free_queue_irqs:
1266         for (i = vector - 1; i >= 0; i--)
1267                 free_irq(adapter->msix_entries[--vector].vector,
1268                          &(adapter->q_vector[i]));
1269         adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
1270         pci_disable_msix(adapter->pdev);
1271         kfree(adapter->msix_entries);
1272         adapter->msix_entries = NULL;
1273 out:
1274         return err;
1275 }
1276
1277 static void ixgbe_set_itr(struct ixgbe_adapter *adapter)
1278 {
1279         struct ixgbe_hw *hw = &adapter->hw;
1280         struct ixgbe_q_vector *q_vector = adapter->q_vector;
1281         u8 current_itr;
1282         u32 new_itr = q_vector->eitr;
1283         struct ixgbe_ring *rx_ring = &adapter->rx_ring[0];
1284         struct ixgbe_ring *tx_ring = &adapter->tx_ring[0];
1285
1286         q_vector->tx_itr = ixgbe_update_itr(adapter, new_itr,
1287                                             q_vector->tx_itr,
1288                                             tx_ring->total_packets,
1289                                             tx_ring->total_bytes);
1290         q_vector->rx_itr = ixgbe_update_itr(adapter, new_itr,
1291                                             q_vector->rx_itr,
1292                                             rx_ring->total_packets,
1293                                             rx_ring->total_bytes);
1294
1295         current_itr = max(q_vector->rx_itr, q_vector->tx_itr);
1296
1297         switch (current_itr) {
1298         /* counts and packets in update_itr are dependent on these numbers */
1299         case lowest_latency:
1300                 new_itr = 100000;
1301                 break;
1302         case low_latency:
1303                 new_itr = 20000; /* aka hwitr = ~200 */
1304                 break;
1305         case bulk_latency:
1306                 new_itr = 8000;
1307                 break;
1308         default:
1309                 break;
1310         }
1311
1312         if (new_itr != q_vector->eitr) {
1313                 u32 itr_reg;
1314                 /* do an exponential smoothing */
1315                 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
1316                 q_vector->eitr = new_itr;
1317                 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
1318                 /* must write high and low 16 bits to reset counter */
1319                 IXGBE_WRITE_REG(hw, IXGBE_EITR(0), itr_reg | (itr_reg)<<16);
1320         }
1321
1322         return;
1323 }
1324
1325 /**
1326  * ixgbe_irq_disable - Mask off interrupt generation on the NIC
1327  * @adapter: board private structure
1328  **/
1329 static inline void ixgbe_irq_disable(struct ixgbe_adapter *adapter)
1330 {
1331         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
1332         IXGBE_WRITE_FLUSH(&adapter->hw);
1333         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1334                 int i;
1335                 for (i = 0; i < adapter->num_msix_vectors; i++)
1336                         synchronize_irq(adapter->msix_entries[i].vector);
1337         } else {
1338                 synchronize_irq(adapter->pdev->irq);
1339         }
1340 }
1341
1342 /**
1343  * ixgbe_irq_enable - Enable default interrupt generation settings
1344  * @adapter: board private structure
1345  **/
1346 static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter)
1347 {
1348         u32 mask;
1349         mask = IXGBE_EIMS_ENABLE_MASK;
1350         if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE)
1351                 mask |= IXGBE_EIMS_GPI_SDP1;
1352         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, mask);
1353         IXGBE_WRITE_FLUSH(&adapter->hw);
1354 }
1355
1356 /**
1357  * ixgbe_intr - legacy mode Interrupt Handler
1358  * @irq: interrupt number
1359  * @data: pointer to a network interface device structure
1360  **/
1361 static irqreturn_t ixgbe_intr(int irq, void *data)
1362 {
1363         struct net_device *netdev = data;
1364         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1365         struct ixgbe_hw *hw = &adapter->hw;
1366         u32 eicr;
1367
1368         /* for NAPI, using EIAM to auto-mask tx/rx interrupt bits on read
1369          * therefore no explict interrupt disable is necessary */
1370         eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
1371         if (!eicr) {
1372                 /* shared interrupt alert!
1373                  * make sure interrupts are enabled because the read will
1374                  * have disabled interrupts due to EIAM */
1375                 ixgbe_irq_enable(adapter);
1376                 return IRQ_NONE;        /* Not our interrupt */
1377         }
1378
1379         if (eicr & IXGBE_EICR_LSC)
1380                 ixgbe_check_lsc(adapter);
1381
1382         ixgbe_check_fan_failure(adapter, eicr);
1383
1384         if (netif_rx_schedule_prep(&adapter->q_vector[0].napi)) {
1385                 adapter->tx_ring[0].total_packets = 0;
1386                 adapter->tx_ring[0].total_bytes = 0;
1387                 adapter->rx_ring[0].total_packets = 0;
1388                 adapter->rx_ring[0].total_bytes = 0;
1389                 /* would disable interrupts here but EIAM disabled it */
1390                 __netif_rx_schedule(&adapter->q_vector[0].napi);
1391         }
1392
1393         return IRQ_HANDLED;
1394 }
1395
1396 static inline void ixgbe_reset_q_vectors(struct ixgbe_adapter *adapter)
1397 {
1398         int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1399
1400         for (i = 0; i < q_vectors; i++) {
1401                 struct ixgbe_q_vector *q_vector = &adapter->q_vector[i];
1402                 bitmap_zero(q_vector->rxr_idx, MAX_RX_QUEUES);
1403                 bitmap_zero(q_vector->txr_idx, MAX_TX_QUEUES);
1404                 q_vector->rxr_count = 0;
1405                 q_vector->txr_count = 0;
1406         }
1407 }
1408
1409 /**
1410  * ixgbe_request_irq - initialize interrupts
1411  * @adapter: board private structure
1412  *
1413  * Attempts to configure interrupts using the best available
1414  * capabilities of the hardware and kernel.
1415  **/
1416 static int ixgbe_request_irq(struct ixgbe_adapter *adapter)
1417 {
1418         struct net_device *netdev = adapter->netdev;
1419         int err;
1420
1421         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1422                 err = ixgbe_request_msix_irqs(adapter);
1423         } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
1424                 err = request_irq(adapter->pdev->irq, &ixgbe_intr, 0,
1425                                   netdev->name, netdev);
1426         } else {
1427                 err = request_irq(adapter->pdev->irq, &ixgbe_intr, IRQF_SHARED,
1428                                   netdev->name, netdev);
1429         }
1430
1431         if (err)
1432                 DPRINTK(PROBE, ERR, "request_irq failed, Error %d\n", err);
1433
1434         return err;
1435 }
1436
1437 static void ixgbe_free_irq(struct ixgbe_adapter *adapter)
1438 {
1439         struct net_device *netdev = adapter->netdev;
1440
1441         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1442                 int i, q_vectors;
1443
1444                 q_vectors = adapter->num_msix_vectors;
1445
1446                 i = q_vectors - 1;
1447                 free_irq(adapter->msix_entries[i].vector, netdev);
1448
1449                 i--;
1450                 for (; i >= 0; i--) {
1451                         free_irq(adapter->msix_entries[i].vector,
1452                                  &(adapter->q_vector[i]));
1453                 }
1454
1455                 ixgbe_reset_q_vectors(adapter);
1456         } else {
1457                 free_irq(adapter->pdev->irq, netdev);
1458         }
1459 }
1460
1461 /**
1462  * ixgbe_configure_msi_and_legacy - Initialize PIN (INTA...) and MSI interrupts
1463  *
1464  **/
1465 static void ixgbe_configure_msi_and_legacy(struct ixgbe_adapter *adapter)
1466 {
1467         struct ixgbe_hw *hw = &adapter->hw;
1468
1469         IXGBE_WRITE_REG(hw, IXGBE_EITR(0),
1470                         EITR_INTS_PER_SEC_TO_REG(adapter->eitr_param));
1471
1472         ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(0), 0);
1473         ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(0), 0);
1474
1475         map_vector_to_rxq(adapter, 0, 0);
1476         map_vector_to_txq(adapter, 0, 0);
1477
1478         DPRINTK(HW, INFO, "Legacy interrupt IVAR setup done\n");
1479 }
1480
1481 /**
1482  * ixgbe_configure_tx - Configure 8259x Transmit Unit after Reset
1483  * @adapter: board private structure
1484  *
1485  * Configure the Tx unit of the MAC after a reset.
1486  **/
1487 static void ixgbe_configure_tx(struct ixgbe_adapter *adapter)
1488 {
1489         u64 tdba, tdwba;
1490         struct ixgbe_hw *hw = &adapter->hw;
1491         u32 i, j, tdlen, txctrl;
1492
1493         /* Setup the HW Tx Head and Tail descriptor pointers */
1494         for (i = 0; i < adapter->num_tx_queues; i++) {
1495                 struct ixgbe_ring *ring = &adapter->tx_ring[i];
1496                 j = ring->reg_idx;
1497                 tdba = ring->dma;
1498                 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1499                 IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
1500                                 (tdba & DMA_32BIT_MASK));
1501                 IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
1502                 tdwba = ring->dma +
1503                         (ring->count * sizeof(union ixgbe_adv_tx_desc));
1504                 tdwba |= IXGBE_TDWBAL_HEAD_WB_ENABLE;
1505                 IXGBE_WRITE_REG(hw, IXGBE_TDWBAL(j), tdwba & DMA_32BIT_MASK);
1506                 IXGBE_WRITE_REG(hw, IXGBE_TDWBAH(j), (tdwba >> 32));
1507                 IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j), tdlen);
1508                 IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
1509                 IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
1510                 adapter->tx_ring[i].head = IXGBE_TDH(j);
1511                 adapter->tx_ring[i].tail = IXGBE_TDT(j);
1512                 /* Disable Tx Head Writeback RO bit, since this hoses
1513                  * bookkeeping if things aren't delivered in order.
1514                  */
1515                 txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
1516                 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1517                 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
1518         }
1519 }
1520
1521 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1522
1523 static void ixgbe_configure_srrctl(struct ixgbe_adapter *adapter, int index)
1524 {
1525         struct ixgbe_ring *rx_ring;
1526         u32 srrctl;
1527         int queue0;
1528         unsigned long mask;
1529
1530         /* program one srrctl register per VMDq index */
1531         if (adapter->flags & IXGBE_FLAG_VMDQ_ENABLED) {
1532                 long shift, len;
1533                 mask = (unsigned long) adapter->ring_feature[RING_F_RSS].mask;
1534                 len = sizeof(adapter->ring_feature[RING_F_VMDQ].mask) * 8;
1535                 shift = find_first_bit(&mask, len);
1536                 queue0 = index & mask;
1537                 index = (index & mask) >> shift;
1538         /* program one srrctl per RSS queue since RDRXCTL.MVMEN is enabled */
1539         } else {
1540                 mask = (unsigned long) adapter->ring_feature[RING_F_RSS].mask;
1541                 queue0 = index & mask;
1542                 index = index & mask;
1543         }
1544
1545         rx_ring = &adapter->rx_ring[queue0];
1546
1547         srrctl = IXGBE_READ_REG(&adapter->hw, IXGBE_SRRCTL(index));
1548
1549         srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
1550         srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
1551
1552         if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1553                 srrctl |= IXGBE_RXBUFFER_2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1554                 srrctl |= IXGBE_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
1555                 srrctl |= ((IXGBE_RX_HDR_SIZE <<
1556                             IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
1557                            IXGBE_SRRCTL_BSIZEHDR_MASK);
1558         } else {
1559                 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1560
1561                 if (rx_ring->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
1562                         srrctl |= IXGBE_RXBUFFER_2048 >>
1563                                   IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1564                 else
1565                         srrctl |= rx_ring->rx_buf_len >>
1566                                   IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1567         }
1568         IXGBE_WRITE_REG(&adapter->hw, IXGBE_SRRCTL(index), srrctl);
1569 }
1570
1571 /**
1572  * ixgbe_get_skb_hdr - helper function for LRO header processing
1573  * @skb: pointer to sk_buff to be added to LRO packet
1574  * @iphdr: pointer to ip header structure
1575  * @tcph: pointer to tcp header structure
1576  * @hdr_flags: pointer to header flags
1577  * @priv: private data
1578  **/
1579 static int ixgbe_get_skb_hdr(struct sk_buff *skb, void **iphdr, void **tcph,
1580                              u64 *hdr_flags, void *priv)
1581 {
1582         union ixgbe_adv_rx_desc *rx_desc = priv;
1583
1584         /* Verify that this is a valid IPv4 TCP packet */
1585         if (!((ixgbe_get_pkt_info(rx_desc) & IXGBE_RXDADV_PKTTYPE_IPV4) &&
1586              (ixgbe_get_pkt_info(rx_desc) & IXGBE_RXDADV_PKTTYPE_TCP)))
1587                 return -1;
1588
1589         /* Set network headers */
1590         skb_reset_network_header(skb);
1591         skb_set_transport_header(skb, ip_hdrlen(skb));
1592         *iphdr = ip_hdr(skb);
1593         *tcph = tcp_hdr(skb);
1594         *hdr_flags = LRO_IPV4 | LRO_TCP;
1595         return 0;
1596 }
1597
1598 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1599                            (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
1600
1601 /**
1602  * ixgbe_configure_rx - Configure 8259x Receive Unit after Reset
1603  * @adapter: board private structure
1604  *
1605  * Configure the Rx unit of the MAC after a reset.
1606  **/
1607 static void ixgbe_configure_rx(struct ixgbe_adapter *adapter)
1608 {
1609         u64 rdba;
1610         struct ixgbe_hw *hw = &adapter->hw;
1611         struct net_device *netdev = adapter->netdev;
1612         int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1613         int i, j;
1614         u32 rdlen, rxctrl, rxcsum;
1615         static const u32 seed[10] = { 0xE291D73D, 0x1805EC6C, 0x2A94B30D,
1616                           0xA54F2BEC, 0xEA49AF7C, 0xE214AD3D, 0xB855AABE,
1617                           0x6A3E67EA, 0x14364D17, 0x3BED200D};
1618         u32 fctrl, hlreg0;
1619         u32 pages;
1620         u32 reta = 0, mrqc;
1621         u32 rdrxctl;
1622         int rx_buf_len;
1623
1624         /* Decide whether to use packet split mode or not */
1625         adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
1626
1627         /* Set the RX buffer length according to the mode */
1628         if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1629                 rx_buf_len = IXGBE_RX_HDR_SIZE;
1630         } else {
1631                 if (netdev->mtu <= ETH_DATA_LEN)
1632                         rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1633                 else
1634                         rx_buf_len = ALIGN(max_frame, 1024);
1635         }
1636
1637         fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
1638         fctrl |= IXGBE_FCTRL_BAM;
1639         fctrl |= IXGBE_FCTRL_DPF; /* discard pause frames when FC enabled */
1640         IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
1641
1642         hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
1643         if (adapter->netdev->mtu <= ETH_DATA_LEN)
1644                 hlreg0 &= ~IXGBE_HLREG0_JUMBOEN;
1645         else
1646                 hlreg0 |= IXGBE_HLREG0_JUMBOEN;
1647         IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
1648
1649         pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1650
1651         rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1652         /* disable receives while setting up the descriptors */
1653         rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1654         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
1655
1656         /* Setup the HW Rx Head and Tail Descriptor Pointers and
1657          * the Base and Length of the Rx Descriptor Ring */
1658         for (i = 0; i < adapter->num_rx_queues; i++) {
1659                 rdba = adapter->rx_ring[i].dma;
1660                 j = adapter->rx_ring[i].reg_idx;
1661                 IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j), (rdba & DMA_32BIT_MASK));
1662                 IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
1663                 IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j), rdlen);
1664                 IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
1665                 IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
1666                 adapter->rx_ring[i].head = IXGBE_RDH(j);
1667                 adapter->rx_ring[i].tail = IXGBE_RDT(j);
1668                 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1669                 /* Intitial LRO Settings */
1670                 adapter->rx_ring[i].lro_mgr.max_aggr = IXGBE_MAX_LRO_AGGREGATE;
1671                 adapter->rx_ring[i].lro_mgr.max_desc = IXGBE_MAX_LRO_DESCRIPTORS;
1672                 adapter->rx_ring[i].lro_mgr.get_skb_header = ixgbe_get_skb_hdr;
1673                 adapter->rx_ring[i].lro_mgr.features = LRO_F_EXTRACT_VLAN_ID;
1674                 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
1675                         adapter->rx_ring[i].lro_mgr.features |= LRO_F_NAPI;
1676                 adapter->rx_ring[i].lro_mgr.dev = adapter->netdev;
1677                 adapter->rx_ring[i].lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1678                 adapter->rx_ring[i].lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1679
1680                 ixgbe_configure_srrctl(adapter, j);
1681         }
1682
1683         /*
1684          * For VMDq support of different descriptor types or
1685          * buffer sizes through the use of multiple SRRCTL
1686          * registers, RDRXCTL.MVMEN must be set to 1
1687          *
1688          * also, the manual doesn't mention it clearly but DCA hints
1689          * will only use queue 0's tags unless this bit is set.  Side
1690          * effects of setting this bit are only that SRRCTL must be
1691          * fully programmed [0..15]
1692          */
1693         if (adapter->flags &
1694             (IXGBE_FLAG_RSS_ENABLED | IXGBE_FLAG_VMDQ_ENABLED)) {
1695                 rdrxctl = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
1696                 rdrxctl |= IXGBE_RDRXCTL_MVMEN;
1697                 IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rdrxctl);
1698         }
1699
1700         if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
1701                 /* Fill out redirection table */
1702                 for (i = 0, j = 0; i < 128; i++, j++) {
1703                         if (j == adapter->ring_feature[RING_F_RSS].indices)
1704                                 j = 0;
1705                         /* reta = 4-byte sliding window of
1706                          * 0x00..(indices-1)(indices-1)00..etc. */
1707                         reta = (reta << 8) | (j * 0x11);
1708                         if ((i & 3) == 3)
1709                                 IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
1710                 }
1711
1712                 /* Fill out hash function seeds */
1713                 for (i = 0; i < 10; i++)
1714                         IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), seed[i]);
1715
1716                 mrqc = IXGBE_MRQC_RSSEN
1717                     /* Perform hash on these packet types */
1718                        | IXGBE_MRQC_RSS_FIELD_IPV4
1719                        | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
1720                        | IXGBE_MRQC_RSS_FIELD_IPV4_UDP
1721                        | IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP
1722                        | IXGBE_MRQC_RSS_FIELD_IPV6_EX
1723                        | IXGBE_MRQC_RSS_FIELD_IPV6
1724                        | IXGBE_MRQC_RSS_FIELD_IPV6_TCP
1725                        | IXGBE_MRQC_RSS_FIELD_IPV6_UDP
1726                        | IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
1727                 IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
1728         }
1729
1730         rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
1731
1732         if (adapter->flags & IXGBE_FLAG_RSS_ENABLED ||
1733             adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED) {
1734                 /* Disable indicating checksum in descriptor, enables
1735                  * RSS hash */
1736                 rxcsum |= IXGBE_RXCSUM_PCSD;
1737         }
1738         if (!(rxcsum & IXGBE_RXCSUM_PCSD)) {
1739                 /* Enable IPv4 payload checksum for UDP fragments
1740                  * if PCSD is not set */
1741                 rxcsum |= IXGBE_RXCSUM_IPPCSE;
1742         }
1743
1744         IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
1745 }
1746
1747 static void ixgbe_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1748 {
1749         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1750         struct ixgbe_hw *hw = &adapter->hw;
1751
1752         /* add VID to filter table */
1753         hw->mac.ops.set_vfta(&adapter->hw, vid, 0, true);
1754 }
1755
1756 static void ixgbe_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1757 {
1758         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1759         struct ixgbe_hw *hw = &adapter->hw;
1760
1761         if (!test_bit(__IXGBE_DOWN, &adapter->state))
1762                 ixgbe_irq_disable(adapter);
1763
1764         vlan_group_set_device(adapter->vlgrp, vid, NULL);
1765
1766         if (!test_bit(__IXGBE_DOWN, &adapter->state))
1767                 ixgbe_irq_enable(adapter);
1768
1769         /* remove VID from filter table */
1770         hw->mac.ops.set_vfta(&adapter->hw, vid, 0, false);
1771 }
1772
1773 static void ixgbe_vlan_rx_register(struct net_device *netdev,
1774                                    struct vlan_group *grp)
1775 {
1776         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1777         u32 ctrl;
1778
1779         if (!test_bit(__IXGBE_DOWN, &adapter->state))
1780                 ixgbe_irq_disable(adapter);
1781         adapter->vlgrp = grp;
1782
1783         /*
1784          * For a DCB driver, always enable VLAN tag stripping so we can
1785          * still receive traffic from a DCB-enabled host even if we're
1786          * not in DCB mode.
1787          */
1788         ctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_VLNCTRL);
1789         ctrl |= IXGBE_VLNCTRL_VME;
1790         ctrl &= ~IXGBE_VLNCTRL_CFIEN;
1791         IXGBE_WRITE_REG(&adapter->hw, IXGBE_VLNCTRL, ctrl);
1792         ixgbe_vlan_rx_add_vid(netdev, 0);
1793
1794         if (grp) {
1795                 /* enable VLAN tag insert/strip */
1796                 ctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_VLNCTRL);
1797                 ctrl |= IXGBE_VLNCTRL_VME;
1798                 ctrl &= ~IXGBE_VLNCTRL_CFIEN;
1799                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VLNCTRL, ctrl);
1800         }
1801
1802         if (!test_bit(__IXGBE_DOWN, &adapter->state))
1803                 ixgbe_irq_enable(adapter);
1804 }
1805
1806 static void ixgbe_restore_vlan(struct ixgbe_adapter *adapter)
1807 {
1808         ixgbe_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1809
1810         if (adapter->vlgrp) {
1811                 u16 vid;
1812                 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1813                         if (!vlan_group_get_device(adapter->vlgrp, vid))
1814                                 continue;
1815                         ixgbe_vlan_rx_add_vid(adapter->netdev, vid);
1816                 }
1817         }
1818 }
1819
1820 static u8 *ixgbe_addr_list_itr(struct ixgbe_hw *hw, u8 **mc_addr_ptr, u32 *vmdq)
1821 {
1822         struct dev_mc_list *mc_ptr;
1823         u8 *addr = *mc_addr_ptr;
1824         *vmdq = 0;
1825
1826         mc_ptr = container_of(addr, struct dev_mc_list, dmi_addr[0]);
1827         if (mc_ptr->next)
1828                 *mc_addr_ptr = mc_ptr->next->dmi_addr;
1829         else
1830                 *mc_addr_ptr = NULL;
1831
1832         return addr;
1833 }
1834
1835 /**
1836  * ixgbe_set_rx_mode - Unicast, Multicast and Promiscuous mode set
1837  * @netdev: network interface device structure
1838  *
1839  * The set_rx_method entry point is called whenever the unicast/multicast
1840  * address list or the network interface flags are updated.  This routine is
1841  * responsible for configuring the hardware for proper unicast, multicast and
1842  * promiscuous mode.
1843  **/
1844 static void ixgbe_set_rx_mode(struct net_device *netdev)
1845 {
1846         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1847         struct ixgbe_hw *hw = &adapter->hw;
1848         u32 fctrl, vlnctrl;
1849         u8 *addr_list = NULL;
1850         int addr_count = 0;
1851
1852         /* Check for Promiscuous and All Multicast modes */
1853
1854         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1855         vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1856
1857         if (netdev->flags & IFF_PROMISC) {
1858                 hw->addr_ctrl.user_set_promisc = 1;
1859                 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1860                 vlnctrl &= ~IXGBE_VLNCTRL_VFE;
1861         } else {
1862                 if (netdev->flags & IFF_ALLMULTI) {
1863                         fctrl |= IXGBE_FCTRL_MPE;
1864                         fctrl &= ~IXGBE_FCTRL_UPE;
1865                 } else {
1866                         fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1867                 }
1868                 vlnctrl |= IXGBE_VLNCTRL_VFE;
1869                 hw->addr_ctrl.user_set_promisc = 0;
1870         }
1871
1872         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1873         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
1874
1875         /* reprogram secondary unicast list */
1876         addr_count = netdev->uc_count;
1877         if (addr_count)
1878                 addr_list = netdev->uc_list->dmi_addr;
1879         hw->mac.ops.update_uc_addr_list(hw, addr_list, addr_count,
1880                                           ixgbe_addr_list_itr);
1881
1882         /* reprogram multicast list */
1883         addr_count = netdev->mc_count;
1884         if (addr_count)
1885                 addr_list = netdev->mc_list->dmi_addr;
1886         hw->mac.ops.update_mc_addr_list(hw, addr_list, addr_count,
1887                                         ixgbe_addr_list_itr);
1888 }
1889
1890 static void ixgbe_napi_enable_all(struct ixgbe_adapter *adapter)
1891 {
1892         int q_idx;
1893         struct ixgbe_q_vector *q_vector;
1894         int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1895
1896         /* legacy and MSI only use one vector */
1897         if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1898                 q_vectors = 1;
1899
1900         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1901                 struct napi_struct *napi;
1902                 q_vector = &adapter->q_vector[q_idx];
1903                 if (!q_vector->rxr_count)
1904                         continue;
1905                 napi = &q_vector->napi;
1906                 if ((adapter->flags & IXGBE_FLAG_MSIX_ENABLED) &&
1907                     (q_vector->rxr_count > 1))
1908                         napi->poll = &ixgbe_clean_rxonly_many;
1909
1910                 napi_enable(napi);
1911         }
1912 }
1913
1914 static void ixgbe_napi_disable_all(struct ixgbe_adapter *adapter)
1915 {
1916         int q_idx;
1917         struct ixgbe_q_vector *q_vector;
1918         int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1919
1920         /* legacy and MSI only use one vector */
1921         if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1922                 q_vectors = 1;
1923
1924         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1925                 q_vector = &adapter->q_vector[q_idx];
1926                 if (!q_vector->rxr_count)
1927                         continue;
1928                 napi_disable(&q_vector->napi);
1929         }
1930 }
1931
1932 #ifdef CONFIG_IXGBE_DCB
1933 /*
1934  * ixgbe_configure_dcb - Configure DCB hardware
1935  * @adapter: ixgbe adapter struct
1936  *
1937  * This is called by the driver on open to configure the DCB hardware.
1938  * This is also called by the gennetlink interface when reconfiguring
1939  * the DCB state.
1940  */
1941 static void ixgbe_configure_dcb(struct ixgbe_adapter *adapter)
1942 {
1943         struct ixgbe_hw *hw = &adapter->hw;
1944         u32 txdctl, vlnctrl;
1945         int i, j;
1946
1947         ixgbe_dcb_check_config(&adapter->dcb_cfg);
1948         ixgbe_dcb_calculate_tc_credits(&adapter->dcb_cfg, DCB_TX_CONFIG);
1949         ixgbe_dcb_calculate_tc_credits(&adapter->dcb_cfg, DCB_RX_CONFIG);
1950
1951         /* reconfigure the hardware */
1952         ixgbe_dcb_hw_config(&adapter->hw, &adapter->dcb_cfg);
1953
1954         for (i = 0; i < adapter->num_tx_queues; i++) {
1955                 j = adapter->tx_ring[i].reg_idx;
1956                 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(j));
1957                 /* PThresh workaround for Tx hang with DFP enabled. */
1958                 txdctl |= 32;
1959                 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(j), txdctl);
1960         }
1961         /* Enable VLAN tag insert/strip */
1962         vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1963         vlnctrl |= IXGBE_VLNCTRL_VME | IXGBE_VLNCTRL_VFE;
1964         vlnctrl &= ~IXGBE_VLNCTRL_CFIEN;
1965         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
1966         hw->mac.ops.set_vfta(&adapter->hw, 0, 0, true);
1967 }
1968
1969 #endif
1970 static void ixgbe_configure(struct ixgbe_adapter *adapter)
1971 {
1972         struct net_device *netdev = adapter->netdev;
1973         int i;
1974
1975         ixgbe_set_rx_mode(netdev);
1976
1977         ixgbe_restore_vlan(adapter);
1978 #ifdef CONFIG_IXGBE_DCB
1979         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
1980                 netif_set_gso_max_size(netdev, 32768);
1981                 ixgbe_configure_dcb(adapter);
1982         } else {
1983                 netif_set_gso_max_size(netdev, 65536);
1984         }
1985 #else
1986         netif_set_gso_max_size(netdev, 65536);
1987 #endif
1988
1989         ixgbe_configure_tx(adapter);
1990         ixgbe_configure_rx(adapter);
1991         for (i = 0; i < adapter->num_rx_queues; i++)
1992                 ixgbe_alloc_rx_buffers(adapter, &adapter->rx_ring[i],
1993                                        (adapter->rx_ring[i].count - 1));
1994 }
1995
1996 static int ixgbe_up_complete(struct ixgbe_adapter *adapter)
1997 {
1998         struct net_device *netdev = adapter->netdev;
1999         struct ixgbe_hw *hw = &adapter->hw;
2000         int i, j = 0;
2001         int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
2002         u32 txdctl, rxdctl, mhadd;
2003         u32 gpie;
2004
2005         ixgbe_get_hw_control(adapter);
2006
2007         if ((adapter->flags & IXGBE_FLAG_MSIX_ENABLED) ||
2008             (adapter->flags & IXGBE_FLAG_MSI_ENABLED)) {
2009                 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2010                         gpie = (IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_EIAME |
2011                                 IXGBE_GPIE_PBA_SUPPORT | IXGBE_GPIE_OCD);
2012                 } else {
2013                         /* MSI only */
2014                         gpie = 0;
2015                 }
2016                 /* XXX: to interrupt immediately for EICS writes, enable this */
2017                 /* gpie |= IXGBE_GPIE_EIMEN; */
2018                 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
2019         }
2020
2021         if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
2022                 /* legacy interrupts, use EIAM to auto-mask when reading EICR,
2023                  * specifically only auto mask tx and rx interrupts */
2024                 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
2025         }
2026
2027         /* Enable fan failure interrupt if media type is copper */
2028         if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) {
2029                 gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
2030                 gpie |= IXGBE_SDP1_GPIEN;
2031                 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
2032         }
2033
2034         mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
2035         if (max_frame != (mhadd >> IXGBE_MHADD_MFS_SHIFT)) {
2036                 mhadd &= ~IXGBE_MHADD_MFS_MASK;
2037                 mhadd |= max_frame << IXGBE_MHADD_MFS_SHIFT;
2038
2039                 IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
2040         }
2041
2042         for (i = 0; i < adapter->num_tx_queues; i++) {
2043                 j = adapter->tx_ring[i].reg_idx;
2044                 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(j));
2045                 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
2046                 txdctl |= (8 << 16);
2047                 txdctl |= IXGBE_TXDCTL_ENABLE;
2048                 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(j), txdctl);
2049         }
2050
2051         for (i = 0; i < adapter->num_rx_queues; i++) {
2052                 j = adapter->rx_ring[i].reg_idx;
2053                 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
2054                 /* enable PTHRESH=32 descriptors (half the internal cache)
2055                  * and HTHRESH=0 descriptors (to minimize latency on fetch),
2056                  * this also removes a pesky rx_no_buffer_count increment */
2057                 rxdctl |= 0x0020;
2058                 rxdctl |= IXGBE_RXDCTL_ENABLE;
2059                 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), rxdctl);
2060         }
2061         /* enable all receives */
2062         rxdctl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
2063         rxdctl |= (IXGBE_RXCTRL_DMBYPS | IXGBE_RXCTRL_RXEN);
2064         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxdctl);
2065
2066         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
2067                 ixgbe_configure_msix(adapter);
2068         else
2069                 ixgbe_configure_msi_and_legacy(adapter);
2070
2071         ixgbe_napi_add_all(adapter);
2072
2073         clear_bit(__IXGBE_DOWN, &adapter->state);
2074         ixgbe_napi_enable_all(adapter);
2075
2076         /* clear any pending interrupts, may auto mask */
2077         IXGBE_READ_REG(hw, IXGBE_EICR);
2078
2079         ixgbe_irq_enable(adapter);
2080
2081         /* enable transmits */
2082         netif_tx_start_all_queues(netdev);
2083
2084         /* bring the link up in the watchdog, this could race with our first
2085          * link up interrupt but shouldn't be a problem */
2086         adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
2087         adapter->link_check_timeout = jiffies;
2088         mod_timer(&adapter->watchdog_timer, jiffies);
2089         return 0;
2090 }
2091
2092 void ixgbe_reinit_locked(struct ixgbe_adapter *adapter)
2093 {
2094         WARN_ON(in_interrupt());
2095         while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
2096                 msleep(1);
2097         ixgbe_down(adapter);
2098         ixgbe_up(adapter);
2099         clear_bit(__IXGBE_RESETTING, &adapter->state);
2100 }
2101
2102 int ixgbe_up(struct ixgbe_adapter *adapter)
2103 {
2104         /* hardware has been reset, we need to reload some things */
2105         ixgbe_configure(adapter);
2106
2107         return ixgbe_up_complete(adapter);
2108 }
2109
2110 void ixgbe_reset(struct ixgbe_adapter *adapter)
2111 {
2112         struct ixgbe_hw *hw = &adapter->hw;
2113         if (hw->mac.ops.init_hw(hw))
2114                 dev_err(&adapter->pdev->dev, "Hardware Error\n");
2115
2116         /* reprogram the RAR[0] in case user changed it. */
2117         hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
2118
2119 }
2120
2121 /**
2122  * ixgbe_clean_rx_ring - Free Rx Buffers per Queue
2123  * @adapter: board private structure
2124  * @rx_ring: ring to free buffers from
2125  **/
2126 static void ixgbe_clean_rx_ring(struct ixgbe_adapter *adapter,
2127                                 struct ixgbe_ring *rx_ring)
2128 {
2129         struct pci_dev *pdev = adapter->pdev;
2130         unsigned long size;
2131         unsigned int i;
2132
2133         /* Free all the Rx ring sk_buffs */
2134
2135         for (i = 0; i < rx_ring->count; i++) {
2136                 struct ixgbe_rx_buffer *rx_buffer_info;
2137
2138                 rx_buffer_info = &rx_ring->rx_buffer_info[i];
2139                 if (rx_buffer_info->dma) {
2140                         pci_unmap_single(pdev, rx_buffer_info->dma,
2141                                          rx_ring->rx_buf_len,
2142                                          PCI_DMA_FROMDEVICE);
2143                         rx_buffer_info->dma = 0;
2144                 }
2145                 if (rx_buffer_info->skb) {
2146                         dev_kfree_skb(rx_buffer_info->skb);
2147                         rx_buffer_info->skb = NULL;
2148                 }
2149                 if (!rx_buffer_info->page)
2150                         continue;
2151                 pci_unmap_page(pdev, rx_buffer_info->page_dma, PAGE_SIZE / 2,
2152                                PCI_DMA_FROMDEVICE);
2153                 rx_buffer_info->page_dma = 0;
2154                 put_page(rx_buffer_info->page);
2155                 rx_buffer_info->page = NULL;
2156                 rx_buffer_info->page_offset = 0;
2157         }
2158
2159         size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
2160         memset(rx_ring->rx_buffer_info, 0, size);
2161
2162         /* Zero out the descriptor ring */
2163         memset(rx_ring->desc, 0, rx_ring->size);
2164
2165         rx_ring->next_to_clean = 0;
2166         rx_ring->next_to_use = 0;
2167
2168         writel(0, adapter->hw.hw_addr + rx_ring->head);
2169         writel(0, adapter->hw.hw_addr + rx_ring->tail);
2170 }
2171
2172 /**
2173  * ixgbe_clean_tx_ring - Free Tx Buffers
2174  * @adapter: board private structure
2175  * @tx_ring: ring to be cleaned
2176  **/
2177 static void ixgbe_clean_tx_ring(struct ixgbe_adapter *adapter,
2178                                 struct ixgbe_ring *tx_ring)
2179 {
2180         struct ixgbe_tx_buffer *tx_buffer_info;
2181         unsigned long size;
2182         unsigned int i;
2183
2184         /* Free all the Tx ring sk_buffs */
2185
2186         for (i = 0; i < tx_ring->count; i++) {
2187                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2188                 ixgbe_unmap_and_free_tx_resource(adapter, tx_buffer_info);
2189         }
2190
2191         size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
2192         memset(tx_ring->tx_buffer_info, 0, size);
2193
2194         /* Zero out the descriptor ring */
2195         memset(tx_ring->desc, 0, tx_ring->size);
2196
2197         tx_ring->next_to_use = 0;
2198         tx_ring->next_to_clean = 0;
2199
2200         writel(0, adapter->hw.hw_addr + tx_ring->head);
2201         writel(0, adapter->hw.hw_addr + tx_ring->tail);
2202 }
2203
2204 /**
2205  * ixgbe_clean_all_rx_rings - Free Rx Buffers for all queues
2206  * @adapter: board private structure
2207  **/
2208 static void ixgbe_clean_all_rx_rings(struct ixgbe_adapter *adapter)
2209 {
2210         int i;
2211
2212         for (i = 0; i < adapter->num_rx_queues; i++)
2213                 ixgbe_clean_rx_ring(adapter, &adapter->rx_ring[i]);
2214 }
2215
2216 /**
2217  * ixgbe_clean_all_tx_rings - Free Tx Buffers for all queues
2218  * @adapter: board private structure
2219  **/
2220 static void ixgbe_clean_all_tx_rings(struct ixgbe_adapter *adapter)
2221 {
2222         int i;
2223
2224         for (i = 0; i < adapter->num_tx_queues; i++)
2225                 ixgbe_clean_tx_ring(adapter, &adapter->tx_ring[i]);
2226 }
2227
2228 void ixgbe_down(struct ixgbe_adapter *adapter)
2229 {
2230         struct net_device *netdev = adapter->netdev;
2231         struct ixgbe_hw *hw = &adapter->hw;
2232         u32 rxctrl;
2233         u32 txdctl;
2234         int i, j;
2235
2236         /* signal that we are down to the interrupt handler */
2237         set_bit(__IXGBE_DOWN, &adapter->state);
2238
2239         /* disable receives */
2240         rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
2241         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
2242
2243         netif_tx_disable(netdev);
2244
2245         IXGBE_WRITE_FLUSH(hw);
2246         msleep(10);
2247
2248         netif_tx_stop_all_queues(netdev);
2249
2250         ixgbe_irq_disable(adapter);
2251
2252         ixgbe_napi_disable_all(adapter);
2253
2254         del_timer_sync(&adapter->watchdog_timer);
2255         cancel_work_sync(&adapter->watchdog_task);
2256
2257         /* disable transmits in the hardware now that interrupts are off */
2258         for (i = 0; i < adapter->num_tx_queues; i++) {
2259                 j = adapter->tx_ring[i].reg_idx;
2260                 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(j));
2261                 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(j),
2262                                 (txdctl & ~IXGBE_TXDCTL_ENABLE));
2263         }
2264
2265         netif_carrier_off(netdev);
2266
2267 #ifdef CONFIG_IXGBE_DCA
2268         if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
2269                 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
2270                 dca_remove_requester(&adapter->pdev->dev);
2271         }
2272
2273 #endif
2274         if (!pci_channel_offline(adapter->pdev))
2275                 ixgbe_reset(adapter);
2276         ixgbe_clean_all_tx_rings(adapter);
2277         ixgbe_clean_all_rx_rings(adapter);
2278
2279 #ifdef CONFIG_IXGBE_DCA
2280         /* since we reset the hardware DCA settings were cleared */
2281         if (dca_add_requester(&adapter->pdev->dev) == 0) {
2282                 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
2283                 /* always use CB2 mode, difference is masked
2284                  * in the CB driver */
2285                 IXGBE_WRITE_REG(hw, IXGBE_DCA_CTRL, 2);
2286                 ixgbe_setup_dca(adapter);
2287         }
2288 #endif
2289 }
2290
2291 /**
2292  * ixgbe_poll - NAPI Rx polling callback
2293  * @napi: structure for representing this polling device
2294  * @budget: how many packets driver is allowed to clean
2295  *
2296  * This function is used for legacy and MSI, NAPI mode
2297  **/
2298 static int ixgbe_poll(struct napi_struct *napi, int budget)
2299 {
2300         struct ixgbe_q_vector *q_vector = container_of(napi,
2301                                                   struct ixgbe_q_vector, napi);
2302         struct ixgbe_adapter *adapter = q_vector->adapter;
2303         int tx_cleaned, work_done = 0;
2304
2305 #ifdef CONFIG_IXGBE_DCA
2306         if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
2307                 ixgbe_update_tx_dca(adapter, adapter->tx_ring);
2308                 ixgbe_update_rx_dca(adapter, adapter->rx_ring);
2309         }
2310 #endif
2311
2312         tx_cleaned = ixgbe_clean_tx_irq(adapter, adapter->tx_ring);
2313         ixgbe_clean_rx_irq(adapter, adapter->rx_ring, &work_done, budget);
2314
2315         if (tx_cleaned)
2316                 work_done = budget;
2317
2318         /* If budget not fully consumed, exit the polling mode */
2319         if (work_done < budget) {
2320                 netif_rx_complete(napi);
2321                 if (adapter->itr_setting & 3)
2322                         ixgbe_set_itr(adapter);
2323                 if (!test_bit(__IXGBE_DOWN, &adapter->state))
2324                         ixgbe_irq_enable(adapter);
2325         }
2326         return work_done;
2327 }
2328
2329 /**
2330  * ixgbe_tx_timeout - Respond to a Tx Hang
2331  * @netdev: network interface device structure
2332  **/
2333 static void ixgbe_tx_timeout(struct net_device *netdev)
2334 {
2335         struct ixgbe_adapter *adapter = netdev_priv(netdev);
2336
2337         /* Do the reset outside of interrupt context */
2338         schedule_work(&adapter->reset_task);
2339 }
2340
2341 static void ixgbe_reset_task(struct work_struct *work)
2342 {
2343         struct ixgbe_adapter *adapter;
2344         adapter = container_of(work, struct ixgbe_adapter, reset_task);
2345
2346         /* If we're already down or resetting, just bail */
2347         if (test_bit(__IXGBE_DOWN, &adapter->state) ||
2348             test_bit(__IXGBE_RESETTING, &adapter->state))
2349                 return;
2350
2351         adapter->tx_timeout_count++;
2352
2353         ixgbe_reinit_locked(adapter);
2354 }
2355
2356 static void ixgbe_set_num_queues(struct ixgbe_adapter *adapter)
2357 {
2358         int nrq = 1, ntq = 1;
2359         int feature_mask = 0, rss_i, rss_m;
2360         int dcb_i, dcb_m;
2361
2362         /* Number of supported queues */
2363         switch (adapter->hw.mac.type) {
2364         case ixgbe_mac_82598EB:
2365                 dcb_i = adapter->ring_feature[RING_F_DCB].indices;
2366                 dcb_m = 0;
2367                 rss_i = adapter->ring_feature[RING_F_RSS].indices;
2368                 rss_m = 0;
2369                 feature_mask |= IXGBE_FLAG_RSS_ENABLED;
2370                 feature_mask |= IXGBE_FLAG_DCB_ENABLED;
2371
2372                 switch (adapter->flags & feature_mask) {
2373                 case (IXGBE_FLAG_RSS_ENABLED | IXGBE_FLAG_DCB_ENABLED):
2374                         dcb_m = 0x7 << 3;
2375                         rss_i = min(8, rss_i);
2376                         rss_m = 0x7;
2377                         nrq = dcb_i * rss_i;
2378                         ntq = min(MAX_TX_QUEUES, dcb_i * rss_i);
2379                         break;
2380                 case (IXGBE_FLAG_DCB_ENABLED):
2381                         dcb_m = 0x7 << 3;
2382                         nrq = dcb_i;
2383                         ntq = dcb_i;
2384                         break;
2385                 case (IXGBE_FLAG_RSS_ENABLED):
2386                         rss_m = 0xF;
2387                         nrq = rss_i;
2388                         ntq = rss_i;
2389                         break;
2390                 case 0:
2391                 default:
2392                         dcb_i = 0;
2393                         dcb_m = 0;
2394                         rss_i = 0;
2395                         rss_m = 0;
2396                         nrq = 1;
2397                         ntq = 1;
2398                         break;
2399                 }
2400
2401                 /* Sanity check, we should never have zero queues */
2402                 nrq = (nrq ?:1);
2403                 ntq = (ntq ?:1);
2404
2405                 adapter->ring_feature[RING_F_DCB].indices = dcb_i;
2406                 adapter->ring_feature[RING_F_DCB].mask = dcb_m;
2407                 adapter->ring_feature[RING_F_RSS].indices = rss_i;
2408                 adapter->ring_feature[RING_F_RSS].mask = rss_m;
2409                 break;
2410         default:
2411                 nrq = 1;
2412                 ntq = 1;
2413                 break;
2414         }
2415
2416         adapter->num_rx_queues = nrq;
2417         adapter->num_tx_queues = ntq;
2418 }
2419
2420 static void ixgbe_acquire_msix_vectors(struct ixgbe_adapter *adapter,
2421                                        int vectors)
2422 {
2423         int err, vector_threshold;
2424
2425         /* We'll want at least 3 (vector_threshold):
2426          * 1) TxQ[0] Cleanup
2427          * 2) RxQ[0] Cleanup
2428          * 3) Other (Link Status Change, etc.)
2429          * 4) TCP Timer (optional)
2430          */
2431         vector_threshold = MIN_MSIX_COUNT;
2432
2433         /* The more we get, the more we will assign to Tx/Rx Cleanup
2434          * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2435          * Right now, we simply care about how many we'll get; we'll
2436          * set them up later while requesting irq's.
2437          */
2438         while (vectors >= vector_threshold) {
2439                 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
2440                                       vectors);
2441                 if (!err) /* Success in acquiring all requested vectors. */
2442                         break;
2443                 else if (err < 0)
2444                         vectors = 0; /* Nasty failure, quit now */
2445                 else /* err == number of vectors we should try again with */
2446                         vectors = err;
2447         }
2448
2449         if (vectors < vector_threshold) {
2450                 /* Can't allocate enough MSI-X interrupts?  Oh well.
2451                  * This just means we'll go with either a single MSI
2452                  * vector or fall back to legacy interrupts.
2453                  */
2454                 DPRINTK(HW, DEBUG, "Unable to allocate MSI-X interrupts\n");
2455                 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2456                 kfree(adapter->msix_entries);
2457                 adapter->msix_entries = NULL;
2458                 adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
2459                 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
2460                 ixgbe_set_num_queues(adapter);
2461         } else {
2462                 adapter->flags |= IXGBE_FLAG_MSIX_ENABLED; /* Woot! */
2463                 adapter->num_msix_vectors = vectors;
2464         }
2465 }
2466
2467 /**
2468  * ixgbe_cache_ring_register - Descriptor ring to register mapping
2469  * @adapter: board private structure to initialize
2470  *
2471  * Once we know the feature-set enabled for the device, we'll cache
2472  * the register offset the descriptor ring is assigned to.
2473  **/
2474 static void ixgbe_cache_ring_register(struct ixgbe_adapter *adapter)
2475 {
2476         int feature_mask = 0, rss_i;
2477         int i, txr_idx, rxr_idx;
2478         int dcb_i;
2479
2480         /* Number of supported queues */
2481         switch (adapter->hw.mac.type) {
2482         case ixgbe_mac_82598EB:
2483                 dcb_i = adapter->ring_feature[RING_F_DCB].indices;
2484                 rss_i = adapter->ring_feature[RING_F_RSS].indices;
2485                 txr_idx = 0;
2486                 rxr_idx = 0;
2487                 feature_mask |= IXGBE_FLAG_DCB_ENABLED;
2488                 feature_mask |= IXGBE_FLAG_RSS_ENABLED;
2489                 switch (adapter->flags & feature_mask) {
2490                 case (IXGBE_FLAG_RSS_ENABLED | IXGBE_FLAG_DCB_ENABLED):
2491                         for (i = 0; i < dcb_i; i++) {
2492                                 int j;
2493                                 /* Rx first */
2494                                 for (j = 0; j < adapter->num_rx_queues; j++) {
2495                                         adapter->rx_ring[rxr_idx].reg_idx =
2496                                                 i << 3 | j;
2497                                         rxr_idx++;
2498                                 }
2499                                 /* Tx now */
2500                                 for (j = 0; j < adapter->num_tx_queues; j++) {
2501                                         adapter->tx_ring[txr_idx].reg_idx =
2502                                                 i << 2 | (j >> 1);
2503                                         if (j & 1)
2504                                                 txr_idx++;
2505                                 }
2506                         }
2507                 case (IXGBE_FLAG_DCB_ENABLED):
2508                         /* the number of queues is assumed to be symmetric */
2509                         for (i = 0; i < dcb_i; i++) {
2510                                 adapter->rx_ring[i].reg_idx = i << 3;
2511                                 adapter->tx_ring[i].reg_idx = i << 2;
2512                         }
2513                         break;
2514                 case (IXGBE_FLAG_RSS_ENABLED):
2515                         for (i = 0; i < adapter->num_rx_queues; i++)
2516                                 adapter->rx_ring[i].reg_idx = i;
2517                         for (i = 0; i < adapter->num_tx_queues; i++)
2518                                 adapter->tx_ring[i].reg_idx = i;
2519                         break;
2520                 case 0:
2521                 default:
2522                         break;
2523                 }
2524                 break;
2525         default:
2526                 break;
2527         }
2528 }
2529
2530 /**
2531  * ixgbe_alloc_queues - Allocate memory for all rings
2532  * @adapter: board private structure to initialize
2533  *
2534  * We allocate one ring per queue at run-time since we don't know the
2535  * number of queues at compile-time.
2536  **/
2537 static int ixgbe_alloc_queues(struct ixgbe_adapter *adapter)
2538 {
2539         int i;
2540
2541         adapter->tx_ring = kcalloc(adapter->num_tx_queues,
2542                                    sizeof(struct ixgbe_ring), GFP_KERNEL);
2543         if (!adapter->tx_ring)
2544                 goto err_tx_ring_allocation;
2545
2546         adapter->rx_ring = kcalloc(adapter->num_rx_queues,
2547                                    sizeof(struct ixgbe_ring), GFP_KERNEL);
2548         if (!adapter->rx_ring)
2549                 goto err_rx_ring_allocation;
2550
2551         for (i = 0; i < adapter->num_tx_queues; i++) {
2552                 adapter->tx_ring[i].count = adapter->tx_ring_count;
2553                 adapter->tx_ring[i].queue_index = i;
2554         }
2555
2556         for (i = 0; i < adapter->num_rx_queues; i++) {
2557                 adapter->rx_ring[i].count = adapter->rx_ring_count;
2558                 adapter->rx_ring[i].queue_index = i;
2559         }
2560
2561         ixgbe_cache_ring_register(adapter);
2562
2563         return 0;
2564
2565 err_rx_ring_allocation:
2566         kfree(adapter->tx_ring);
2567 err_tx_ring_allocation:
2568         return -ENOMEM;
2569 }
2570
2571 /**
2572  * ixgbe_set_interrupt_capability - set MSI-X or MSI if supported
2573  * @adapter: board private structure to initialize
2574  *
2575  * Attempt to configure the interrupts using the best available
2576  * capabilities of the hardware and the kernel.
2577  **/
2578 static int ixgbe_set_interrupt_capability(struct ixgbe_adapter *adapter)
2579 {
2580         int err = 0;
2581         int vector, v_budget;
2582
2583         /*
2584          * It's easy to be greedy for MSI-X vectors, but it really
2585          * doesn't do us much good if we have a lot more vectors
2586          * than CPU's.  So let's be conservative and only ask for
2587          * (roughly) twice the number of vectors as there are CPU's.
2588          */
2589         v_budget = min(adapter->num_rx_queues + adapter->num_tx_queues,
2590                        (int)(num_online_cpus() * 2)) + NON_Q_VECTORS;
2591
2592         /*
2593          * At the same time, hardware can only support a maximum of
2594          * MAX_MSIX_COUNT vectors.  With features such as RSS and VMDq,
2595          * we can easily reach upwards of 64 Rx descriptor queues and
2596          * 32 Tx queues.  Thus, we cap it off in those rare cases where
2597          * the cpu count also exceeds our vector limit.
2598          */
2599         v_budget = min(v_budget, MAX_MSIX_COUNT);
2600
2601         /* A failure in MSI-X entry allocation isn't fatal, but it does
2602          * mean we disable MSI-X capabilities of the adapter. */
2603         adapter->msix_entries = kcalloc(v_budget,
2604                                         sizeof(struct msix_entry), GFP_KERNEL);
2605         if (!adapter->msix_entries) {
2606                 adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
2607                 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
2608                 ixgbe_set_num_queues(adapter);
2609                 kfree(adapter->tx_ring);
2610                 kfree(adapter->rx_ring);
2611                 err = ixgbe_alloc_queues(adapter);
2612                 if (err) {
2613                         DPRINTK(PROBE, ERR, "Unable to allocate memory "
2614                                 "for queues\n");
2615                         goto out;
2616                 }
2617
2618                 goto try_msi;
2619         }
2620
2621         for (vector = 0; vector < v_budget; vector++)
2622                 adapter->msix_entries[vector].entry = vector;
2623
2624         ixgbe_acquire_msix_vectors(adapter, v_budget);
2625
2626         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
2627                 goto out;
2628
2629 try_msi:
2630         err = pci_enable_msi(adapter->pdev);
2631         if (!err) {
2632                 adapter->flags |= IXGBE_FLAG_MSI_ENABLED;
2633         } else {
2634                 DPRINTK(HW, DEBUG, "Unable to allocate MSI interrupt, "
2635                         "falling back to legacy.  Error: %d\n", err);
2636                 /* reset err */
2637                 err = 0;
2638         }
2639
2640 out:
2641         /* Notify the stack of the (possibly) reduced Tx Queue count. */
2642         adapter->netdev->real_num_tx_queues = adapter->num_tx_queues;
2643
2644         return err;
2645 }
2646
2647 void ixgbe_reset_interrupt_capability(struct ixgbe_adapter *adapter)
2648 {
2649         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2650                 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2651                 pci_disable_msix(adapter->pdev);
2652                 kfree(adapter->msix_entries);
2653                 adapter->msix_entries = NULL;
2654         } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
2655                 adapter->flags &= ~IXGBE_FLAG_MSI_ENABLED;
2656                 pci_disable_msi(adapter->pdev);
2657         }
2658         return;
2659 }
2660
2661 /**
2662  * ixgbe_init_interrupt_scheme - Determine proper interrupt scheme
2663  * @adapter: board private structure to initialize
2664  *
2665  * We determine which interrupt scheme to use based on...
2666  * - Kernel support (MSI, MSI-X)
2667  *   - which can be user-defined (via MODULE_PARAM)
2668  * - Hardware queue count (num_*_queues)
2669  *   - defined by miscellaneous hardware support/features (RSS, etc.)
2670  **/
2671 int ixgbe_init_interrupt_scheme(struct ixgbe_adapter *adapter)
2672 {
2673         int err;
2674
2675         /* Number of supported queues */
2676         ixgbe_set_num_queues(adapter);
2677
2678         err = ixgbe_alloc_queues(adapter);
2679         if (err) {
2680                 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
2681                 goto err_alloc_queues;
2682         }
2683
2684         err = ixgbe_set_interrupt_capability(adapter);
2685         if (err) {
2686                 DPRINTK(PROBE, ERR, "Unable to setup interrupt capabilities\n");
2687                 goto err_set_interrupt;
2688         }
2689
2690         DPRINTK(DRV, INFO, "Multiqueue %s: Rx Queue count = %u, "
2691                 "Tx Queue count = %u\n",
2692                 (adapter->num_rx_queues > 1) ? "Enabled" :
2693                 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2694
2695         set_bit(__IXGBE_DOWN, &adapter->state);
2696
2697         return 0;
2698
2699 err_set_interrupt:
2700         kfree(adapter->tx_ring);
2701         kfree(adapter->rx_ring);
2702 err_alloc_queues:
2703         return err;
2704 }
2705
2706 /**
2707  * ixgbe_sfp_timer - worker thread to find a missing module
2708  * @data: pointer to our adapter struct
2709  **/
2710 static void ixgbe_sfp_timer(unsigned long data)
2711 {
2712         struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
2713
2714         /* Do the sfp_timer outside of interrupt context due to the
2715          * delays that sfp+ detection requires
2716          */
2717         schedule_work(&adapter->sfp_task);
2718 }
2719
2720 /**
2721  * ixgbe_sfp_task - worker thread to find a missing module
2722  * @work: pointer to work_struct containing our data
2723  **/
2724 static void ixgbe_sfp_task(struct work_struct *work)
2725 {
2726         struct ixgbe_adapter *adapter = container_of(work,
2727                                                      struct ixgbe_adapter,
2728                                                      sfp_task);
2729         struct ixgbe_hw *hw = &adapter->hw;
2730
2731         if ((hw->phy.type == ixgbe_phy_nl) &&
2732             (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
2733                 s32 ret = hw->phy.ops.identify_sfp(hw);
2734                 if (ret)
2735                         goto reschedule;
2736                 ret = hw->phy.ops.reset(hw);
2737                 if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
2738                         DPRINTK(PROBE, ERR, "failed to initialize because an "
2739                                 "unsupported SFP+ module type was detected.\n"
2740                                 "Reload the driver after installing a "
2741                                 "supported module.\n");
2742                         unregister_netdev(adapter->netdev);
2743                 } else {
2744                         DPRINTK(PROBE, INFO, "detected SFP+: %d\n",
2745                                 hw->phy.sfp_type);
2746                 }
2747                 /* don't need this routine any more */
2748                 clear_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
2749         }
2750         return;
2751 reschedule:
2752         if (test_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state))
2753                 mod_timer(&adapter->sfp_timer,
2754                           round_jiffies(jiffies + (2 * HZ)));
2755 }
2756
2757 /**
2758  * ixgbe_sw_init - Initialize general software structures (struct ixgbe_adapter)
2759  * @adapter: board private structure to initialize
2760  *
2761  * ixgbe_sw_init initializes the Adapter private data structure.
2762  * Fields are initialized based on PCI device information and
2763  * OS network device settings (MTU size).
2764  **/
2765 static int __devinit ixgbe_sw_init(struct ixgbe_adapter *adapter)
2766 {
2767         struct ixgbe_hw *hw = &adapter->hw;
2768         struct pci_dev *pdev = adapter->pdev;
2769         unsigned int rss;
2770 #ifdef CONFIG_IXGBE_DCB
2771         int j;
2772         struct tc_configuration *tc;
2773 #endif
2774
2775         /* PCI config space info */
2776
2777         hw->vendor_id = pdev->vendor;
2778         hw->device_id = pdev->device;
2779         hw->revision_id = pdev->revision;
2780         hw->subsystem_vendor_id = pdev->subsystem_vendor;
2781         hw->subsystem_device_id = pdev->subsystem_device;
2782
2783         /* Set capability flags */
2784         rss = min(IXGBE_MAX_RSS_INDICES, (int)num_online_cpus());
2785         adapter->ring_feature[RING_F_RSS].indices = rss;
2786         adapter->flags |= IXGBE_FLAG_RSS_ENABLED;
2787         adapter->ring_feature[RING_F_DCB].indices = IXGBE_MAX_DCB_INDICES;
2788
2789 #ifdef CONFIG_IXGBE_DCB
2790         /* Configure DCB traffic classes */
2791         for (j = 0; j < MAX_TRAFFIC_CLASS; j++) {
2792                 tc = &adapter->dcb_cfg.tc_config[j];
2793                 tc->path[DCB_TX_CONFIG].bwg_id = 0;
2794                 tc->path[DCB_TX_CONFIG].bwg_percent = 12 + (j & 1);
2795                 tc->path[DCB_RX_CONFIG].bwg_id = 0;
2796                 tc->path[DCB_RX_CONFIG].bwg_percent = 12 + (j & 1);
2797                 tc->dcb_pfc = pfc_disabled;
2798         }
2799         adapter->dcb_cfg.bw_percentage[DCB_TX_CONFIG][0] = 100;
2800         adapter->dcb_cfg.bw_percentage[DCB_RX_CONFIG][0] = 100;
2801         adapter->dcb_cfg.rx_pba_cfg = pba_equal;
2802         adapter->dcb_cfg.round_robin_enable = false;
2803         adapter->dcb_set_bitmap = 0x00;
2804         ixgbe_copy_dcb_cfg(&adapter->dcb_cfg, &adapter->temp_dcb_cfg,
2805                            adapter->ring_feature[RING_F_DCB].indices);
2806
2807 #endif
2808         if (hw->mac.ops.get_media_type &&
2809             (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper))
2810                 adapter->flags |= IXGBE_FLAG_FAN_FAIL_CAPABLE;
2811
2812         /* default flow control settings */
2813         hw->fc.original_type = ixgbe_fc_none;
2814         hw->fc.type = ixgbe_fc_none;
2815         hw->fc.high_water = IXGBE_DEFAULT_FCRTH;
2816         hw->fc.low_water = IXGBE_DEFAULT_FCRTL;
2817         hw->fc.pause_time = IXGBE_DEFAULT_FCPAUSE;
2818         hw->fc.send_xon = true;
2819
2820         /* select 10G link by default */
2821         hw->mac.link_mode_select = IXGBE_AUTOC_LMS_10G_LINK_NO_AN;
2822
2823         /* enable itr by default in dynamic mode */
2824         adapter->itr_setting = 1;
2825         adapter->eitr_param = 20000;
2826
2827         /* set defaults for eitr in MegaBytes */
2828         adapter->eitr_low = 10;
2829         adapter->eitr_high = 20;
2830
2831         /* set default ring sizes */
2832         adapter->tx_ring_count = IXGBE_DEFAULT_TXD;
2833         adapter->rx_ring_count = IXGBE_DEFAULT_RXD;
2834
2835         /* initialize eeprom parameters */
2836         if (ixgbe_init_eeprom_params_generic(hw)) {
2837                 dev_err(&pdev->dev, "EEPROM initialization failed\n");
2838                 return -EIO;
2839         }
2840
2841         /* enable rx csum by default */
2842         adapter->flags |= IXGBE_FLAG_RX_CSUM_ENABLED;
2843
2844         set_bit(__IXGBE_DOWN, &adapter->state);
2845
2846         return 0;
2847 }
2848
2849 /**
2850  * ixgbe_setup_tx_resources - allocate Tx resources (Descriptors)
2851  * @adapter: board private structure
2852  * @tx_ring:    tx descriptor ring (for a specific queue) to setup
2853  *
2854  * Return 0 on success, negative on failure
2855  **/
2856 int ixgbe_setup_tx_resources(struct ixgbe_adapter *adapter,
2857                              struct ixgbe_ring *tx_ring)
2858 {
2859         struct pci_dev *pdev = adapter->pdev;
2860         int size;
2861
2862         size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
2863         tx_ring->tx_buffer_info = vmalloc(size);
2864         if (!tx_ring->tx_buffer_info)
2865                 goto err;
2866         memset(tx_ring->tx_buffer_info, 0, size);
2867
2868         /* round up to nearest 4K */
2869         tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc) +
2870                         sizeof(u32);
2871         tx_ring->size = ALIGN(tx_ring->size, 4096);
2872
2873         tx_ring->desc = pci_alloc_consistent(pdev, tx_ring->size,
2874                                              &tx_ring->dma);
2875         if (!tx_ring->desc)
2876                 goto err;
2877
2878         tx_ring->next_to_use = 0;
2879         tx_ring->next_to_clean = 0;
2880         tx_ring->work_limit = tx_ring->count;
2881         return 0;
2882
2883 err:
2884         vfree(tx_ring->tx_buffer_info);
2885         tx_ring->tx_buffer_info = NULL;
2886         DPRINTK(PROBE, ERR, "Unable to allocate memory for the transmit "
2887                             "descriptor ring\n");
2888         return -ENOMEM;
2889 }
2890
2891 /**
2892  * ixgbe_setup_all_tx_resources - allocate all queues Tx resources
2893  * @adapter: board private structure
2894  *
2895  * If this function returns with an error, then it's possible one or
2896  * more of the rings is populated (while the rest are not).  It is the
2897  * callers duty to clean those orphaned rings.
2898  *
2899  * Return 0 on success, negative on failure
2900  **/
2901 static int ixgbe_setup_all_tx_resources(struct ixgbe_adapter *adapter)
2902 {
2903         int i, err = 0;
2904
2905         for (i = 0; i < adapter->num_tx_queues; i++) {
2906                 err = ixgbe_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2907                 if (!err)
2908                         continue;
2909                 DPRINTK(PROBE, ERR, "Allocation for Tx Queue %u failed\n", i);
2910                 break;
2911         }
2912
2913         return err;
2914 }
2915
2916 /**
2917  * ixgbe_setup_rx_resources - allocate Rx resources (Descriptors)
2918  * @adapter: board private structure
2919  * @rx_ring:    rx descriptor ring (for a specific queue) to setup
2920  *
2921  * Returns 0 on success, negative on failure
2922  **/
2923 int ixgbe_setup_rx_resources(struct ixgbe_adapter *adapter,
2924                              struct ixgbe_ring *rx_ring)
2925 {
2926         struct pci_dev *pdev = adapter->pdev;
2927         int size;
2928
2929         size = sizeof(struct net_lro_desc) * IXGBE_MAX_LRO_DESCRIPTORS;
2930         rx_ring->lro_mgr.lro_arr = vmalloc(size);
2931         if (!rx_ring->lro_mgr.lro_arr)
2932                 return -ENOMEM;
2933         memset(rx_ring->lro_mgr.lro_arr, 0, size);
2934
2935         size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
2936         rx_ring->rx_buffer_info = vmalloc(size);
2937         if (!rx_ring->rx_buffer_info) {
2938                 DPRINTK(PROBE, ERR,
2939                         "vmalloc allocation failed for the rx desc ring\n");
2940                 goto alloc_failed;
2941         }
2942         memset(rx_ring->rx_buffer_info, 0, size);
2943
2944         /* Round up to nearest 4K */
2945         rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2946         rx_ring->size = ALIGN(rx_ring->size, 4096);
2947
2948         rx_ring->desc = pci_alloc_consistent(pdev, rx_ring->size, &rx_ring->dma);
2949
2950         if (!rx_ring->desc) {
2951                 DPRINTK(PROBE, ERR,
2952                         "Memory allocation failed for the rx desc ring\n");
2953                 vfree(rx_ring->rx_buffer_info);
2954                 goto alloc_failed;
2955         }
2956
2957         rx_ring->next_to_clean = 0;
2958         rx_ring->next_to_use = 0;
2959
2960         return 0;
2961
2962 alloc_failed:
2963         vfree(rx_ring->lro_mgr.lro_arr);
2964         rx_ring->lro_mgr.lro_arr = NULL;
2965         return -ENOMEM;
2966 }
2967
2968 /**
2969  * ixgbe_setup_all_rx_resources - allocate all queues Rx resources
2970  * @adapter: board private structure
2971  *
2972  * If this function returns with an error, then it's possible one or
2973  * more of the rings is populated (while the rest are not).  It is the
2974  * callers duty to clean those orphaned rings.
2975  *
2976  * Return 0 on success, negative on failure
2977  **/
2978
2979 static int ixgbe_setup_all_rx_resources(struct ixgbe_adapter *adapter)
2980 {
2981         int i, err = 0;
2982
2983         for (i = 0; i < adapter->num_rx_queues; i++) {
2984                 err = ixgbe_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2985                 if (!err)
2986                         continue;
2987                 DPRINTK(PROBE, ERR, "Allocation for Rx Queue %u failed\n", i);
2988                 break;
2989         }
2990
2991         return err;
2992 }
2993
2994 /**
2995  * ixgbe_free_tx_resources - Free Tx Resources per Queue
2996  * @adapter: board private structure
2997  * @tx_ring: Tx descriptor ring for a specific queue
2998  *
2999  * Free all transmit software resources
3000  **/
3001 void ixgbe_free_tx_resources(struct ixgbe_adapter *adapter,
3002                              struct ixgbe_ring *tx_ring)
3003 {
3004         struct pci_dev *pdev = adapter->pdev;
3005
3006         ixgbe_clean_tx_ring(adapter, tx_ring);
3007
3008         vfree(tx_ring->tx_buffer_info);
3009         tx_ring->tx_buffer_info = NULL;
3010
3011         pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
3012
3013         tx_ring->desc = NULL;
3014 }
3015
3016 /**
3017  * ixgbe_free_all_tx_resources - Free Tx Resources for All Queues
3018  * @adapter: board private structure
3019  *
3020  * Free all transmit software resources
3021  **/
3022 static void ixgbe_free_all_tx_resources(struct ixgbe_adapter *adapter)
3023 {
3024         int i;
3025
3026         for (i = 0; i < adapter->num_tx_queues; i++)
3027                 ixgbe_free_tx_resources(adapter, &adapter->tx_ring[i]);
3028 }
3029
3030 /**
3031  * ixgbe_free_rx_resources - Free Rx Resources
3032  * @adapter: board private structure
3033  * @rx_ring: ring to clean the resources from
3034  *
3035  * Free all receive software resources
3036  **/
3037 void ixgbe_free_rx_resources(struct ixgbe_adapter *adapter,
3038                              struct ixgbe_ring *rx_ring)
3039 {
3040         struct pci_dev *pdev = adapter->pdev;
3041
3042         vfree(rx_ring->lro_mgr.lro_arr);
3043         rx_ring->lro_mgr.lro_arr = NULL;
3044
3045         ixgbe_clean_rx_ring(adapter, rx_ring);
3046
3047         vfree(rx_ring->rx_buffer_info);
3048         rx_ring->rx_buffer_info = NULL;
3049
3050         pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
3051
3052         rx_ring->desc = NULL;
3053 }
3054
3055 /**
3056  * ixgbe_free_all_rx_resources - Free Rx Resources for All Queues
3057  * @adapter: board private structure
3058  *
3059  * Free all receive software resources
3060  **/
3061 static void ixgbe_free_all_rx_resources(struct ixgbe_adapter *adapter)
3062 {
3063         int i;
3064
3065         for (i = 0; i < adapter->num_rx_queues; i++)
3066                 ixgbe_free_rx_resources(adapter, &adapter->rx_ring[i]);
3067 }
3068
3069 /**
3070  * ixgbe_change_mtu - Change the Maximum Transfer Unit
3071  * @netdev: network interface device structure
3072  * @new_mtu: new value for maximum frame size
3073  *
3074  * Returns 0 on success, negative on failure
3075  **/
3076 static int ixgbe_change_mtu(struct net_device *netdev, int new_mtu)
3077 {
3078         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3079         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3080
3081         /* MTU < 68 is an error and causes problems on some kernels */
3082         if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE))
3083                 return -EINVAL;
3084
3085         DPRINTK(PROBE, INFO, "changing MTU from %d to %d\n",
3086                 netdev->mtu, new_mtu);
3087         /* must set new MTU before calling down or up */
3088         netdev->mtu = new_mtu;
3089
3090         if (netif_running(netdev))
3091                 ixgbe_reinit_locked(adapter);
3092
3093         return 0;
3094 }
3095
3096 /**
3097  * ixgbe_open - Called when a network interface is made active
3098  * @netdev: network interface device structure
3099  *
3100  * Returns 0 on success, negative value on failure
3101  *
3102  * The open entry point is called when a network interface is made
3103  * active by the system (IFF_UP).  At this point all resources needed
3104  * for transmit and receive operations are allocated, the interrupt
3105  * handler is registered with the OS, the watchdog timer is started,
3106  * and the stack is notified that the interface is ready.
3107  **/
3108 static int ixgbe_open(struct net_device *netdev)
3109 {
3110         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3111         int err;
3112
3113         /* disallow open during test */
3114         if (test_bit(__IXGBE_TESTING, &adapter->state))
3115                 return -EBUSY;
3116
3117         /* allocate transmit descriptors */
3118         err = ixgbe_setup_all_tx_resources(adapter);
3119         if (err)
3120                 goto err_setup_tx;
3121
3122         /* allocate receive descriptors */
3123         err = ixgbe_setup_all_rx_resources(adapter);
3124         if (err)
3125                 goto err_setup_rx;
3126
3127         ixgbe_configure(adapter);
3128
3129         err = ixgbe_request_irq(adapter);
3130         if (err)
3131                 goto err_req_irq;
3132
3133         err = ixgbe_up_complete(adapter);
3134         if (err)
3135                 goto err_up;
3136
3137         netif_tx_start_all_queues(netdev);
3138
3139         return 0;
3140
3141 err_up:
3142         ixgbe_release_hw_control(adapter);
3143         ixgbe_free_irq(adapter);
3144 err_req_irq:
3145         ixgbe_free_all_rx_resources(adapter);
3146 err_setup_rx:
3147         ixgbe_free_all_tx_resources(adapter);
3148 err_setup_tx:
3149         ixgbe_reset(adapter);
3150
3151         return err;
3152 }
3153
3154 /**
3155  * ixgbe_close - Disables a network interface
3156  * @netdev: network interface device structure
3157  *
3158  * Returns 0, this is not allowed to fail
3159  *
3160  * The close entry point is called when an interface is de-activated
3161  * by the OS.  The hardware is still under the drivers control, but
3162  * needs to be disabled.  A global MAC reset is issued to stop the
3163  * hardware, and all transmit and receive resources are freed.
3164  **/
3165 static int ixgbe_close(struct net_device *netdev)
3166 {
3167         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3168
3169         ixgbe_down(adapter);
3170         ixgbe_free_irq(adapter);
3171
3172         ixgbe_free_all_tx_resources(adapter);
3173         ixgbe_free_all_rx_resources(adapter);
3174
3175         ixgbe_release_hw_control(adapter);
3176
3177         return 0;
3178 }
3179
3180 /**
3181  * ixgbe_napi_add_all - prep napi structs for use
3182  * @adapter: private struct
3183  * helper function to napi_add each possible q_vector->napi
3184  */
3185 void ixgbe_napi_add_all(struct ixgbe_adapter *adapter)
3186 {
3187         int q_idx, q_vectors;
3188         struct net_device *netdev = adapter->netdev;
3189         int (*poll)(struct napi_struct *, int);
3190
3191         /* check if we already have our netdev->napi_list populated */
3192         if (&netdev->napi_list != netdev->napi_list.next)
3193                 return;
3194
3195         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3196                 poll = &ixgbe_clean_rxonly;
3197                 /* Only enable as many vectors as we have rx queues. */
3198                 q_vectors = adapter->num_rx_queues;
3199         } else {
3200                 poll = &ixgbe_poll;
3201                 /* only one q_vector for legacy modes */
3202                 q_vectors = 1;
3203         }
3204
3205         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
3206                 struct ixgbe_q_vector *q_vector = &adapter->q_vector[q_idx];
3207                 netif_napi_add(adapter->netdev, &q_vector->napi, (*poll), 64);
3208         }
3209 }
3210
3211 void ixgbe_napi_del_all(struct ixgbe_adapter *adapter)
3212 {
3213         int q_idx;
3214         int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3215
3216         /* legacy and MSI only use one vector */
3217         if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
3218                 q_vectors = 1;
3219
3220         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
3221                 struct ixgbe_q_vector *q_vector = &adapter->q_vector[q_idx];
3222                 if (!q_vector->rxr_count)
3223                         continue;
3224                 netif_napi_del(&q_vector->napi);
3225         }
3226 }
3227
3228 #ifdef CONFIG_PM
3229 static int ixgbe_resume(struct pci_dev *pdev)
3230 {
3231         struct net_device *netdev = pci_get_drvdata(pdev);
3232         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3233         u32 err;
3234
3235         pci_set_power_state(pdev, PCI_D0);
3236         pci_restore_state(pdev);
3237         err = pci_enable_device(pdev);
3238         if (err) {
3239                 printk(KERN_ERR "ixgbe: Cannot enable PCI device from "
3240                                 "suspend\n");
3241                 return err;
3242         }
3243         pci_set_master(pdev);
3244
3245         pci_enable_wake(pdev, PCI_D3hot, 0);
3246         pci_enable_wake(pdev, PCI_D3cold, 0);
3247
3248         err = ixgbe_init_interrupt_scheme(adapter);
3249         if (err) {
3250                 printk(KERN_ERR "ixgbe: Cannot initialize interrupts for "
3251                                 "device\n");
3252                 return err;
3253         }
3254
3255         ixgbe_napi_add_all(adapter);
3256         ixgbe_reset(adapter);
3257
3258         if (netif_running(netdev)) {
3259                 err = ixgbe_open(adapter->netdev);
3260                 if (err)
3261                         return err;
3262         }
3263
3264         netif_device_attach(netdev);
3265
3266         return 0;
3267 }
3268
3269 #endif /* CONFIG_PM */
3270 static int ixgbe_suspend(struct pci_dev *pdev, pm_message_t state)
3271 {
3272         struct net_device *netdev = pci_get_drvdata(pdev);
3273         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3274 #ifdef CONFIG_PM
3275         int retval = 0;
3276 #endif
3277
3278         netif_device_detach(netdev);
3279
3280         if (netif_running(netdev)) {
3281                 ixgbe_down(adapter);
3282                 ixgbe_free_irq(adapter);
3283                 ixgbe_free_all_tx_resources(adapter);
3284                 ixgbe_free_all_rx_resources(adapter);
3285         }
3286         ixgbe_reset_interrupt_capability(adapter);
3287         ixgbe_napi_del_all(adapter);
3288         INIT_LIST_HEAD(&netdev->napi_list);
3289         kfree(adapter->tx_ring);
3290         kfree(adapter->rx_ring);
3291
3292 #ifdef CONFIG_PM
3293         retval = pci_save_state(pdev);
3294         if (retval)
3295                 return retval;
3296 #endif
3297
3298         pci_enable_wake(pdev, PCI_D3hot, 0);
3299         pci_enable_wake(pdev, PCI_D3cold, 0);
3300
3301         ixgbe_release_hw_control(adapter);
3302
3303         pci_disable_device(pdev);
3304
3305         pci_set_power_state(pdev, pci_choose_state(pdev, state));
3306
3307         return 0;
3308 }
3309
3310 static void ixgbe_shutdown(struct pci_dev *pdev)
3311 {
3312         ixgbe_suspend(pdev, PMSG_SUSPEND);
3313 }
3314
3315 /**
3316  * ixgbe_update_stats - Update the board statistics counters.
3317  * @adapter: board private structure
3318  **/
3319 void ixgbe_update_stats(struct ixgbe_adapter *adapter)
3320 {
3321         struct ixgbe_hw *hw = &adapter->hw;
3322         u64 total_mpc = 0;
3323         u32 i, missed_rx = 0, mpc, bprc, lxon, lxoff, xon_off_tot;
3324
3325         adapter->stats.crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
3326         for (i = 0; i < 8; i++) {
3327                 /* for packet buffers not used, the register should read 0 */
3328                 mpc = IXGBE_READ_REG(hw, IXGBE_MPC(i));
3329                 missed_rx += mpc;
3330                 adapter->stats.mpc[i] += mpc;
3331                 total_mpc += adapter->stats.mpc[i];
3332                 adapter->stats.rnbc[i] += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
3333                 adapter->stats.qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
3334                 adapter->stats.qbtc[i] += IXGBE_READ_REG(hw, IXGBE_QBTC(i));
3335                 adapter->stats.qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
3336                 adapter->stats.qbrc[i] += IXGBE_READ_REG(hw, IXGBE_QBRC(i));
3337                 adapter->stats.pxonrxc[i] += IXGBE_READ_REG(hw,
3338                                                             IXGBE_PXONRXC(i));
3339                 adapter->stats.pxontxc[i] += IXGBE_READ_REG(hw,
3340                                                             IXGBE_PXONTXC(i));
3341                 adapter->stats.pxoffrxc[i] += IXGBE_READ_REG(hw,
3342                                                             IXGBE_PXOFFRXC(i));
3343                 adapter->stats.pxofftxc[i] += IXGBE_READ_REG(hw,
3344                                                             IXGBE_PXOFFTXC(i));
3345         }
3346         adapter->stats.gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
3347         /* work around hardware counting issue */
3348         adapter->stats.gprc -= missed_rx;
3349
3350         /* 82598 hardware only has a 32 bit counter in the high register */
3351         adapter->stats.gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
3352         adapter->stats.gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
3353         adapter->stats.tor += IXGBE_READ_REG(hw, IXGBE_TORH);
3354         bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
3355         adapter->stats.bprc += bprc;
3356         adapter->stats.mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
3357         adapter->stats.mprc -= bprc;
3358         adapter->stats.roc += IXGBE_READ_REG(hw, IXGBE_ROC);
3359         adapter->stats.prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
3360         adapter->stats.prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
3361         adapter->stats.prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
3362         adapter->stats.prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
3363         adapter->stats.prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
3364         adapter->stats.prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
3365         adapter->stats.rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
3366         adapter->stats.lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
3367         adapter->stats.lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
3368         lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
3369         adapter->stats.lxontxc += lxon;
3370         lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
3371         adapter->stats.lxofftxc += lxoff;
3372         adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
3373         adapter->stats.gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
3374         adapter->stats.mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
3375         /*
3376          * 82598 errata - tx of flow control packets is included in tx counters
3377          */
3378         xon_off_tot = lxon + lxoff;
3379         adapter->stats.gptc -= xon_off_tot;
3380         adapter->stats.mptc -= xon_off_tot;
3381         adapter->stats.gotc -= (xon_off_tot * (ETH_ZLEN + ETH_FCS_LEN));
3382         adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
3383         adapter->stats.rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
3384         adapter->stats.rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
3385         adapter->stats.tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
3386         adapter->stats.ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
3387         adapter->stats.ptc64 -= xon_off_tot;
3388         adapter->stats.ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
3389         adapter->stats.ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
3390         adapter->stats.ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
3391         adapter->stats.ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
3392         adapter->stats.ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
3393         adapter->stats.bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
3394
3395         /* Fill out the OS statistics structure */
3396         adapter->net_stats.multicast = adapter->stats.mprc;
3397
3398         /* Rx Errors */
3399         adapter->net_stats.rx_errors = adapter->stats.crcerrs +
3400                                        adapter->stats.rlec;
3401         adapter->net_stats.rx_dropped = 0;
3402         adapter->net_stats.rx_length_errors = adapter->stats.rlec;
3403         adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3404         adapter->net_stats.rx_missed_errors = total_mpc;
3405 }
3406
3407 /**
3408  * ixgbe_watchdog - Timer Call-back
3409  * @data: pointer to adapter cast into an unsigned long
3410  **/
3411 static void ixgbe_watchdog(unsigned long data)
3412 {
3413         struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
3414         struct ixgbe_hw *hw = &adapter->hw;
3415
3416         /* Do the watchdog outside of interrupt context due to the lovely
3417          * delays that some of the newer hardware requires */
3418         if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
3419                 /* Cause software interrupt to ensure rx rings are cleaned */
3420                 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3421                         u32 eics =
3422                          (1 << (adapter->num_msix_vectors - NON_Q_VECTORS)) - 1;
3423                         IXGBE_WRITE_REG(hw, IXGBE_EICS, eics);
3424                 } else {
3425                         /* For legacy and MSI interrupts don't set any bits that
3426                          * are enabled for EIAM, because this operation would
3427                          * set *both* EIMS and EICS for any bit in EIAM */
3428                         IXGBE_WRITE_REG(hw, IXGBE_EICS,
3429                                     (IXGBE_EICS_TCP_TIMER | IXGBE_EICS_OTHER));
3430                 }
3431                 /* Reset the timer */
3432                 mod_timer(&adapter->watchdog_timer,
3433                           round_jiffies(jiffies + 2 * HZ));
3434         }
3435
3436         schedule_work(&adapter->watchdog_task);
3437 }
3438
3439 /**
3440  * ixgbe_watchdog_task - worker thread to bring link up
3441  * @work: pointer to work_struct containing our data
3442  **/
3443 static void ixgbe_watchdog_task(struct work_struct *work)
3444 {
3445         struct ixgbe_adapter *adapter = container_of(work,
3446                                                      struct ixgbe_adapter,
3447                                                      watchdog_task);
3448         struct net_device *netdev = adapter->netdev;
3449         struct ixgbe_hw *hw = &adapter->hw;
3450         u32 link_speed = adapter->link_speed;
3451         bool link_up = adapter->link_up;
3452
3453         adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
3454
3455         if (adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE) {
3456                 hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
3457                 if (link_up ||
3458                     time_after(jiffies, (adapter->link_check_timeout +
3459                                          IXGBE_TRY_LINK_TIMEOUT))) {
3460                         IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMC_LSC);
3461                         adapter->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
3462                 }
3463                 adapter->link_up = link_up;
3464                 adapter->link_speed = link_speed;
3465         }
3466
3467         if (link_up) {
3468                 if (!netif_carrier_ok(netdev)) {
3469                         u32 frctl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3470                         u32 rmcs = IXGBE_READ_REG(hw, IXGBE_RMCS);
3471 #define FLOW_RX (frctl & IXGBE_FCTRL_RFCE)
3472 #define FLOW_TX (rmcs & IXGBE_RMCS_TFCE_802_3X)
3473                         printk(KERN_INFO "ixgbe: %s NIC Link is Up %s, "
3474                                "Flow Control: %s\n",
3475                                netdev->name,
3476                                (link_speed == IXGBE_LINK_SPEED_10GB_FULL ?
3477                                 "10 Gbps" :
3478                                 (link_speed == IXGBE_LINK_SPEED_1GB_FULL ?
3479                                  "1 Gbps" : "unknown speed")),
3480                                ((FLOW_RX && FLOW_TX) ? "RX/TX" :
3481                                 (FLOW_RX ? "RX" :
3482                                 (FLOW_TX ? "TX" : "None"))));
3483
3484                         netif_carrier_on(netdev);
3485                 } else {
3486                         /* Force detection of hung controller */
3487                         adapter->detect_tx_hung = true;
3488                 }
3489         } else {
3490                 adapter->link_up = false;
3491                 adapter->link_speed = 0;
3492                 if (netif_carrier_ok(netdev)) {
3493                         printk(KERN_INFO "ixgbe: %s NIC Link is Down\n",
3494                                netdev->name);
3495                         netif_carrier_off(netdev);
3496                 }
3497         }
3498
3499         ixgbe_update_stats(adapter);
3500         adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
3501 }
3502
3503 static int ixgbe_tso(struct ixgbe_adapter *adapter,
3504                      struct ixgbe_ring *tx_ring, struct sk_buff *skb,
3505                      u32 tx_flags, u8 *hdr_len)
3506 {
3507         struct ixgbe_adv_tx_context_desc *context_desc;
3508         unsigned int i;
3509         int err;
3510         struct ixgbe_tx_buffer *tx_buffer_info;
3511         u32 vlan_macip_lens = 0, type_tucmd_mlhl;
3512         u32 mss_l4len_idx, l4len;
3513
3514         if (skb_is_gso(skb)) {
3515                 if (skb_header_cloned(skb)) {
3516                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3517                         if (err)
3518                                 return err;
3519                 }
3520                 l4len = tcp_hdrlen(skb);
3521                 *hdr_len += l4len;
3522
3523                 if (skb->protocol == htons(ETH_P_IP)) {
3524                         struct iphdr *iph = ip_hdr(skb);
3525                         iph->tot_len = 0;
3526                         iph->check = 0;
3527                         tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
3528                                                                  iph->daddr, 0,
3529                                                                  IPPROTO_TCP,
3530                                                                  0);
3531                         adapter->hw_tso_ctxt++;
3532                 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
3533                         ipv6_hdr(skb)->payload_len = 0;
3534                         tcp_hdr(skb)->check =
3535                             ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3536                                              &ipv6_hdr(skb)->daddr,
3537                                              0, IPPROTO_TCP, 0);
3538                         adapter->hw_tso6_ctxt++;
3539                 }
3540
3541                 i = tx_ring->next_to_use;
3542
3543                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3544                 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
3545
3546                 /* VLAN MACLEN IPLEN */
3547                 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3548                         vlan_macip_lens |=
3549                             (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
3550                 vlan_macip_lens |= ((skb_network_offset(skb)) <<
3551                                     IXGBE_ADVTXD_MACLEN_SHIFT);
3552                 *hdr_len += skb_network_offset(skb);
3553                 vlan_macip_lens |=
3554                     (skb_transport_header(skb) - skb_network_header(skb));
3555                 *hdr_len +=
3556                     (skb_transport_header(skb) - skb_network_header(skb));
3557                 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3558                 context_desc->seqnum_seed = 0;
3559
3560                 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3561                 type_tucmd_mlhl = (IXGBE_TXD_CMD_DEXT |
3562                                    IXGBE_ADVTXD_DTYP_CTXT);
3563
3564                 if (skb->protocol == htons(ETH_P_IP))
3565                         type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
3566                 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
3567                 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
3568
3569                 /* MSS L4LEN IDX */
3570                 mss_l4len_idx =
3571                     (skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT);
3572                 mss_l4len_idx |= (l4len << IXGBE_ADVTXD_L4LEN_SHIFT);
3573                 /* use index 1 for TSO */
3574                 mss_l4len_idx |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
3575                 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3576
3577                 tx_buffer_info->time_stamp = jiffies;
3578                 tx_buffer_info->next_to_watch = i;
3579
3580                 i++;
3581                 if (i == tx_ring->count)
3582                         i = 0;
3583                 tx_ring->next_to_use = i;
3584
3585                 return true;
3586         }
3587         return false;
3588 }
3589
3590 static bool ixgbe_tx_csum(struct ixgbe_adapter *adapter,
3591                           struct ixgbe_ring *tx_ring,
3592                           struct sk_buff *skb, u32 tx_flags)
3593 {
3594         struct ixgbe_adv_tx_context_desc *context_desc;
3595         unsigned int i;
3596         struct ixgbe_tx_buffer *tx_buffer_info;
3597         u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
3598
3599         if (skb->ip_summed == CHECKSUM_PARTIAL ||
3600             (tx_flags & IXGBE_TX_FLAGS_VLAN)) {
3601                 i = tx_ring->next_to_use;
3602                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3603                 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
3604
3605                 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3606                         vlan_macip_lens |=
3607                             (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
3608                 vlan_macip_lens |= (skb_network_offset(skb) <<
3609                                     IXGBE_ADVTXD_MACLEN_SHIFT);
3610                 if (skb->ip_summed == CHECKSUM_PARTIAL)
3611                         vlan_macip_lens |= (skb_transport_header(skb) -
3612                                             skb_network_header(skb));
3613
3614                 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3615                 context_desc->seqnum_seed = 0;
3616
3617                 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
3618                                     IXGBE_ADVTXD_DTYP_CTXT);
3619
3620                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3621                         switch (skb->protocol) {
3622                         case __constant_htons(ETH_P_IP):
3623                                 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
3624                                 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
3625                                         type_tucmd_mlhl |=
3626                                                 IXGBE_ADVTXD_TUCMD_L4T_TCP;
3627                                 break;
3628                         case __constant_htons(ETH_P_IPV6):
3629                                 /* XXX what about other V6 headers?? */
3630                                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
3631                                         type_tucmd_mlhl |=
3632                                                 IXGBE_ADVTXD_TUCMD_L4T_TCP;
3633                                 break;
3634                         default:
3635                                 if (unlikely(net_ratelimit())) {
3636                                         DPRINTK(PROBE, WARNING,
3637                                          "partial checksum but proto=%x!\n",
3638                                          skb->protocol);
3639                                 }
3640                                 break;
3641                         }
3642                 }
3643
3644                 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
3645                 /* use index zero for tx checksum offload */
3646                 context_desc->mss_l4len_idx = 0;
3647
3648                 tx_buffer_info->time_stamp = jiffies;
3649                 tx_buffer_info->next_to_watch = i;
3650
3651                 adapter->hw_csum_tx_good++;
3652                 i++;
3653                 if (i == tx_ring->count)
3654                         i = 0;
3655                 tx_ring->next_to_use = i;
3656
3657                 return true;
3658         }
3659
3660         return false;
3661 }
3662
3663 static int ixgbe_tx_map(struct ixgbe_adapter *adapter,
3664                         struct ixgbe_ring *tx_ring,
3665                         struct sk_buff *skb, unsigned int first)
3666 {
3667         struct ixgbe_tx_buffer *tx_buffer_info;
3668         unsigned int len = skb->len;
3669         unsigned int offset = 0, size, count = 0, i;
3670         unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
3671         unsigned int f;
3672
3673         len -= skb->data_len;
3674
3675         i = tx_ring->next_to_use;
3676
3677         while (len) {
3678                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3679                 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
3680
3681                 tx_buffer_info->length = size;
3682                 tx_buffer_info->dma = pci_map_single(adapter->pdev,
3683                                                      skb->data + offset,
3684                                                      size, PCI_DMA_TODEVICE);
3685                 tx_buffer_info->time_stamp = jiffies;
3686                 tx_buffer_info->next_to_watch = i;
3687
3688                 len -= size;
3689                 offset += size;
3690                 count++;
3691                 i++;
3692                 if (i == tx_ring->count)
3693                         i = 0;
3694         }
3695
3696         for (f = 0; f < nr_frags; f++) {
3697                 struct skb_frag_struct *frag;
3698
3699                 frag = &skb_shinfo(skb)->frags[f];
3700                 len = frag->size;
3701                 offset = frag->page_offset;
3702
3703                 while (len) {
3704                         tx_buffer_info = &tx_ring->tx_buffer_info[i];
3705                         size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
3706
3707                         tx_buffer_info->length = size;
3708                         tx_buffer_info->dma = pci_map_page(adapter->pdev,
3709                                                            frag->page,
3710                                                            offset,
3711                                                            size,
3712                                                            PCI_DMA_TODEVICE);
3713                         tx_buffer_info->time_stamp = jiffies;
3714                         tx_buffer_info->next_to_watch = i;
3715
3716                         len -= size;
3717                         offset += size;
3718                         count++;
3719                         i++;
3720                         if (i == tx_ring->count)
3721                                 i = 0;
3722                 }
3723         }
3724         if (i == 0)
3725                 i = tx_ring->count - 1;
3726         else
3727                 i = i - 1;
3728         tx_ring->tx_buffer_info[i].skb = skb;
3729         tx_ring->tx_buffer_info[first].next_to_watch = i;
3730
3731         return count;
3732 }
3733
3734 static void ixgbe_tx_queue(struct ixgbe_adapter *adapter,
3735                            struct ixgbe_ring *tx_ring,
3736                            int tx_flags, int count, u32 paylen, u8 hdr_len)
3737 {
3738         union ixgbe_adv_tx_desc *tx_desc = NULL;
3739         struct ixgbe_tx_buffer *tx_buffer_info;
3740         u32 olinfo_status = 0, cmd_type_len = 0;
3741         unsigned int i;
3742         u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
3743
3744         cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
3745
3746         cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
3747
3748         if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3749                 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
3750
3751         if (tx_flags & IXGBE_TX_FLAGS_TSO) {
3752                 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
3753
3754                 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
3755                                  IXGBE_ADVTXD_POPTS_SHIFT;
3756
3757                 /* use index 1 context for tso */
3758                 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
3759                 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3760                         olinfo_status |= IXGBE_TXD_POPTS_IXSM <<
3761                                          IXGBE_ADVTXD_POPTS_SHIFT;
3762
3763         } else if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3764                 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
3765                                  IXGBE_ADVTXD_POPTS_SHIFT;
3766
3767         olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
3768
3769         i = tx_ring->next_to_use;
3770         while (count--) {
3771                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3772                 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
3773                 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
3774                 tx_desc->read.cmd_type_len =
3775                         cpu_to_le32(cmd_type_len | tx_buffer_info->length);
3776                 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
3777                 i++;
3778                 if (i == tx_ring->count)
3779                         i = 0;
3780         }
3781
3782         tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
3783
3784         /*
3785          * Force memory writes to complete before letting h/w
3786          * know there are new descriptors to fetch.  (Only
3787          * applicable for weak-ordered memory model archs,
3788          * such as IA-64).
3789          */
3790         wmb();
3791
3792         tx_ring->next_to_use = i;
3793         writel(i, adapter->hw.hw_addr + tx_ring->tail);
3794 }
3795
3796 static int __ixgbe_maybe_stop_tx(struct net_device *netdev,
3797                                  struct ixgbe_ring *tx_ring, int size)
3798 {
3799         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3800
3801         netif_stop_subqueue(netdev, tx_ring->queue_index);
3802         /* Herbert's original patch had:
3803          *  smp_mb__after_netif_stop_queue();
3804          * but since that doesn't exist yet, just open code it. */
3805         smp_mb();
3806
3807         /* We need to check again in a case another CPU has just
3808          * made room available. */
3809         if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
3810                 return -EBUSY;
3811
3812         /* A reprieve! - use start_queue because it doesn't call schedule */
3813         netif_start_subqueue(netdev, tx_ring->queue_index);
3814         ++adapter->restart_queue;
3815         return 0;
3816 }
3817
3818 static int ixgbe_maybe_stop_tx(struct net_device *netdev,
3819                               struct ixgbe_ring *tx_ring, int size)
3820 {
3821         if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
3822                 return 0;
3823         return __ixgbe_maybe_stop_tx(netdev, tx_ring, size);
3824 }
3825
3826 static int ixgbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3827 {
3828         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3829         struct ixgbe_ring *tx_ring;
3830         unsigned int first;
3831         unsigned int tx_flags = 0;
3832         u8 hdr_len = 0;
3833         int r_idx = 0, tso;
3834         int count = 0;
3835         unsigned int f;
3836
3837         r_idx = (adapter->num_tx_queues - 1) & skb->queue_mapping;
3838         tx_ring = &adapter->tx_ring[r_idx];
3839
3840         if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
3841                 tx_flags |= vlan_tx_tag_get(skb);
3842                 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
3843                         tx_flags &= ~IXGBE_TX_FLAGS_VLAN_PRIO_MASK;
3844                         tx_flags |= (skb->queue_mapping << 13);
3845                 }
3846                 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3847                 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3848         } else if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
3849                 tx_flags |= (skb->queue_mapping << 13);
3850                 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3851                 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3852         }
3853         /* three things can cause us to need a context descriptor */
3854         if (skb_is_gso(skb) ||
3855             (skb->ip_summed == CHECKSUM_PARTIAL) ||
3856             (tx_flags & IXGBE_TX_FLAGS_VLAN))
3857                 count++;
3858
3859         count += TXD_USE_COUNT(skb_headlen(skb));
3860         for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3861                 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3862
3863         if (ixgbe_maybe_stop_tx(netdev, tx_ring, count)) {
3864                 adapter->tx_busy++;
3865                 return NETDEV_TX_BUSY;
3866         }
3867
3868         if (skb->protocol == htons(ETH_P_IP))
3869                 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3870         first = tx_ring->next_to_use;
3871         tso = ixgbe_tso(adapter, tx_ring, skb, tx_flags, &hdr_len);
3872         if (tso < 0) {
3873                 dev_kfree_skb_any(skb);
3874                 return NETDEV_TX_OK;
3875         }
3876
3877         if (tso)
3878                 tx_flags |= IXGBE_TX_FLAGS_TSO;
3879         else if (ixgbe_tx_csum(adapter, tx_ring, skb, tx_flags) &&
3880                  (skb->ip_summed == CHECKSUM_PARTIAL))
3881                 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3882
3883         ixgbe_tx_queue(adapter, tx_ring, tx_flags,
3884                        ixgbe_tx_map(adapter, tx_ring, skb, first),
3885                        skb->len, hdr_len);
3886
3887         netdev->trans_start = jiffies;
3888
3889         ixgbe_maybe_stop_tx(netdev, tx_ring, DESC_NEEDED);
3890
3891         return NETDEV_TX_OK;
3892 }
3893
3894 /**
3895  * ixgbe_get_stats - Get System Network Statistics
3896  * @netdev: network interface device structure
3897  *
3898  * Returns the address of the device statistics structure.
3899  * The statistics are actually updated from the timer callback.
3900  **/
3901 static struct net_device_stats *ixgbe_get_stats(struct net_device *netdev)
3902 {
3903         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3904
3905         /* only return the current stats */
3906         return &adapter->net_stats;
3907 }
3908
3909 /**
3910  * ixgbe_set_mac - Change the Ethernet Address of the NIC
3911  * @netdev: network interface device structure
3912  * @p: pointer to an address structure
3913  *
3914  * Returns 0 on success, negative on failure
3915  **/
3916 static int ixgbe_set_mac(struct net_device *netdev, void *p)
3917 {
3918         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3919         struct ixgbe_hw *hw = &adapter->hw;
3920         struct sockaddr *addr = p;
3921
3922         if (!is_valid_ether_addr(addr->sa_data))
3923                 return -EADDRNOTAVAIL;
3924
3925         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3926         memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3927
3928         hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
3929
3930         return 0;
3931 }
3932
3933 #ifdef CONFIG_NET_POLL_CONTROLLER
3934 /*
3935  * Polling 'interrupt' - used by things like netconsole to send skbs
3936  * without having to re-enable interrupts. It's not called while
3937  * the interrupt routine is executing.
3938  */
3939 static void ixgbe_netpoll(struct net_device *netdev)
3940 {
3941         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3942
3943         disable_irq(adapter->pdev->irq);
3944         adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
3945         ixgbe_intr(adapter->pdev->irq, netdev);
3946         adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
3947         enable_irq(adapter->pdev->irq);
3948 }
3949 #endif
3950
3951 /**
3952  * ixgbe_link_config - set up initial link with default speed and duplex
3953  * @hw: pointer to private hardware struct
3954  *
3955  * Returns 0 on success, negative on failure
3956  **/
3957 static int ixgbe_link_config(struct ixgbe_hw *hw)
3958 {
3959         u32 autoneg = IXGBE_LINK_SPEED_10GB_FULL;
3960
3961         /* must always autoneg for both 1G and 10G link */
3962         hw->mac.autoneg = true;
3963
3964         if ((hw->mac.type == ixgbe_mac_82598EB) &&
3965             (hw->phy.media_type == ixgbe_media_type_copper))
3966                 autoneg = IXGBE_LINK_SPEED_82598_AUTONEG;
3967
3968         return hw->mac.ops.setup_link_speed(hw, autoneg, true, true);
3969 }
3970
3971 static const struct net_device_ops ixgbe_netdev_ops = {
3972         .ndo_open               = ixgbe_open,
3973         .ndo_stop               = ixgbe_close,
3974         .ndo_start_xmit         = ixgbe_xmit_frame,
3975         .ndo_get_stats          = ixgbe_get_stats,
3976         .ndo_set_rx_mode        = ixgbe_set_rx_mode,
3977         .ndo_set_multicast_list = ixgbe_set_rx_mode,
3978         .ndo_validate_addr      = eth_validate_addr,
3979         .ndo_set_mac_address    = ixgbe_set_mac,
3980         .ndo_change_mtu         = ixgbe_change_mtu,
3981         .ndo_tx_timeout         = ixgbe_tx_timeout,
3982         .ndo_vlan_rx_register   = ixgbe_vlan_rx_register,
3983         .ndo_vlan_rx_add_vid    = ixgbe_vlan_rx_add_vid,
3984         .ndo_vlan_rx_kill_vid   = ixgbe_vlan_rx_kill_vid,
3985 #ifdef CONFIG_NET_POLL_CONTROLLER
3986         .ndo_poll_controller    = ixgbe_netpoll,
3987 #endif
3988 };
3989
3990 /**
3991  * ixgbe_probe - Device Initialization Routine
3992  * @pdev: PCI device information struct
3993  * @ent: entry in ixgbe_pci_tbl
3994  *
3995  * Returns 0 on success, negative on failure
3996  *
3997  * ixgbe_probe initializes an adapter identified by a pci_dev structure.
3998  * The OS initialization, configuring of the adapter private structure,
3999  * and a hardware reset occur.
4000  **/
4001 static int __devinit ixgbe_probe(struct pci_dev *pdev,
4002                                  const struct pci_device_id *ent)
4003 {
4004         struct net_device *netdev;
4005         struct ixgbe_adapter *adapter = NULL;
4006         struct ixgbe_hw *hw;
4007         const struct ixgbe_info *ii = ixgbe_info_tbl[ent->driver_data];
4008         static int cards_found;
4009         int i, err, pci_using_dac;
4010         u16 link_status, link_speed, link_width;
4011         u32 part_num, eec;
4012
4013         err = pci_enable_device(pdev);
4014         if (err)
4015                 return err;
4016
4017         if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK) &&
4018             !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK)) {
4019                 pci_using_dac = 1;
4020         } else {
4021                 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
4022                 if (err) {
4023                         err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
4024                         if (err) {
4025                                 dev_err(&pdev->dev, "No usable DMA "
4026                                         "configuration, aborting\n");
4027                                 goto err_dma;
4028                         }
4029                 }
4030                 pci_using_dac = 0;
4031         }
4032
4033         err = pci_request_regions(pdev, ixgbe_driver_name);
4034         if (err) {
4035                 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
4036                 goto err_pci_reg;
4037         }
4038
4039         err = pci_enable_pcie_error_reporting(pdev);
4040         if (err) {
4041                 dev_err(&pdev->dev, "pci_enable_pcie_error_reporting failed "
4042                                     "0x%x\n", err);
4043                 /* non-fatal, continue */
4044         }
4045
4046         pci_set_master(pdev);
4047         pci_save_state(pdev);
4048
4049         netdev = alloc_etherdev_mq(sizeof(struct ixgbe_adapter), MAX_TX_QUEUES);
4050         if (!netdev) {
4051                 err = -ENOMEM;
4052                 goto err_alloc_etherdev;
4053         }
4054
4055         SET_NETDEV_DEV(netdev, &pdev->dev);
4056
4057         pci_set_drvdata(pdev, netdev);
4058         adapter = netdev_priv(netdev);
4059
4060         adapter->netdev = netdev;
4061         adapter->pdev = pdev;
4062         hw = &adapter->hw;
4063         hw->back = adapter;
4064         adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
4065
4066         hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
4067                               pci_resource_len(pdev, 0));
4068         if (!hw->hw_addr) {
4069                 err = -EIO;
4070                 goto err_ioremap;
4071         }
4072
4073         for (i = 1; i <= 5; i++) {
4074                 if (pci_resource_len(pdev, i) == 0)
4075                         continue;
4076         }
4077
4078         netdev->netdev_ops = &ixgbe_netdev_ops;
4079         ixgbe_set_ethtool_ops(netdev);
4080         netdev->watchdog_timeo = 5 * HZ;
4081         strcpy(netdev->name, pci_name(pdev));
4082
4083         adapter->bd_number = cards_found;
4084
4085         /* Setup hw api */
4086         memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
4087         hw->mac.type  = ii->mac;
4088
4089         /* EEPROM */
4090         memcpy(&hw->eeprom.ops, ii->eeprom_ops, sizeof(hw->eeprom.ops));
4091         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
4092         /* If EEPROM is valid (bit 8 = 1), use default otherwise use bit bang */
4093         if (!(eec & (1 << 8)))
4094                 hw->eeprom.ops.read = &ixgbe_read_eeprom_bit_bang_generic;
4095
4096         /* PHY */
4097         memcpy(&hw->phy.ops, ii->phy_ops, sizeof(hw->phy.ops));
4098         hw->phy.sfp_type = ixgbe_sfp_type_unknown;
4099
4100         /* set up this timer and work struct before calling get_invariants
4101          * which might start the timer
4102          */
4103         init_timer(&adapter->sfp_timer);
4104         adapter->sfp_timer.function = &ixgbe_sfp_timer;
4105         adapter->sfp_timer.data = (unsigned long) adapter;
4106
4107         INIT_WORK(&adapter->sfp_task, ixgbe_sfp_task);
4108
4109         err = ii->get_invariants(hw);
4110         if (err == IXGBE_ERR_SFP_NOT_PRESENT) {
4111                 /* start a kernel thread to watch for a module to arrive */
4112                 set_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
4113                 mod_timer(&adapter->sfp_timer,
4114                           round_jiffies(jiffies + (2 * HZ)));
4115                 err = 0;
4116         } else if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
4117                 DPRINTK(PROBE, ERR, "failed to load because an "
4118                         "unsupported SFP+ module type was detected.\n");
4119                 goto err_hw_init;
4120         } else if (err) {
4121                 goto err_hw_init;
4122         }
4123
4124         /* setup the private structure */
4125         err = ixgbe_sw_init(adapter);
4126         if (err)
4127                 goto err_sw_init;
4128
4129         /* reset_hw fills in the perm_addr as well */
4130         err = hw->mac.ops.reset_hw(hw);
4131         if (err) {
4132                 dev_err(&adapter->pdev->dev, "HW Init failed: %d\n", err);
4133                 goto err_sw_init;
4134         }
4135
4136         netdev->features = NETIF_F_SG |
4137                            NETIF_F_IP_CSUM |
4138                            NETIF_F_HW_VLAN_TX |
4139                            NETIF_F_HW_VLAN_RX |
4140                            NETIF_F_HW_VLAN_FILTER;
4141
4142         netdev->features |= NETIF_F_IPV6_CSUM;
4143         netdev->features |= NETIF_F_TSO;
4144         netdev->features |= NETIF_F_TSO6;
4145         netdev->features |= NETIF_F_LRO;
4146
4147         netdev->vlan_features |= NETIF_F_TSO;
4148         netdev->vlan_features |= NETIF_F_TSO6;
4149         netdev->vlan_features |= NETIF_F_IP_CSUM;
4150         netdev->vlan_features |= NETIF_F_SG;
4151
4152         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED)
4153                 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
4154
4155 #ifdef CONFIG_IXGBE_DCB
4156         netdev->dcbnl_ops = &dcbnl_ops;
4157 #endif
4158
4159         if (pci_using_dac)
4160                 netdev->features |= NETIF_F_HIGHDMA;
4161
4162         /* make sure the EEPROM is good */
4163         if (hw->eeprom.ops.validate_checksum(hw, NULL) < 0) {
4164                 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
4165                 err = -EIO;
4166                 goto err_eeprom;
4167         }
4168
4169         memcpy(netdev->dev_addr, hw->mac.perm_addr, netdev->addr_len);
4170         memcpy(netdev->perm_addr, hw->mac.perm_addr, netdev->addr_len);
4171
4172         if (ixgbe_validate_mac_addr(netdev->perm_addr)) {
4173                 dev_err(&pdev->dev, "invalid MAC address\n");
4174                 err = -EIO;
4175                 goto err_eeprom;
4176         }
4177
4178         init_timer(&adapter->watchdog_timer);
4179         adapter->watchdog_timer.function = &ixgbe_watchdog;
4180         adapter->watchdog_timer.data = (unsigned long)adapter;
4181
4182         INIT_WORK(&adapter->reset_task, ixgbe_reset_task);
4183         INIT_WORK(&adapter->watchdog_task, ixgbe_watchdog_task);
4184
4185         err = ixgbe_init_interrupt_scheme(adapter);
4186         if (err)
4187                 goto err_sw_init;
4188
4189         /* print bus type/speed/width info */
4190         pci_read_config_word(pdev, IXGBE_PCI_LINK_STATUS, &link_status);
4191         link_speed = link_status & IXGBE_PCI_LINK_SPEED;
4192         link_width = link_status & IXGBE_PCI_LINK_WIDTH;
4193         dev_info(&pdev->dev, "(PCI Express:%s:%s) %pM\n",
4194                 ((link_speed == IXGBE_PCI_LINK_SPEED_5000) ? "5.0Gb/s" :
4195                  (link_speed == IXGBE_PCI_LINK_SPEED_2500) ? "2.5Gb/s" :
4196                  "Unknown"),
4197                 ((link_width == IXGBE_PCI_LINK_WIDTH_8) ? "Width x8" :
4198                  (link_width == IXGBE_PCI_LINK_WIDTH_4) ? "Width x4" :
4199                  (link_width == IXGBE_PCI_LINK_WIDTH_2) ? "Width x2" :
4200                  (link_width == IXGBE_PCI_LINK_WIDTH_1) ? "Width x1" :
4201                  "Unknown"),
4202                 netdev->dev_addr);
4203         ixgbe_read_pba_num_generic(hw, &part_num);
4204         dev_info(&pdev->dev, "MAC: %d, PHY: %d, PBA No: %06x-%03x\n",
4205                  hw->mac.type, hw->phy.type,
4206                  (part_num >> 8), (part_num & 0xff));
4207
4208         if (link_width <= IXGBE_PCI_LINK_WIDTH_4) {
4209                 dev_warn(&pdev->dev, "PCI-Express bandwidth available for "
4210                          "this card is not sufficient for optimal "
4211                          "performance.\n");
4212                 dev_warn(&pdev->dev, "For optimal performance a x8 "
4213                          "PCI-Express slot is required.\n");
4214         }
4215
4216         /* reset the hardware with the new settings */
4217         hw->mac.ops.start_hw(hw);
4218
4219         /* link_config depends on start_hw being called at least once */
4220         err = ixgbe_link_config(hw);
4221         if (err) {
4222                 dev_err(&pdev->dev, "setup_link_speed FAILED %d\n", err);
4223                 goto err_register;
4224         }
4225
4226         netif_carrier_off(netdev);
4227
4228         strcpy(netdev->name, "eth%d");
4229         err = register_netdev(netdev);
4230         if (err)
4231                 goto err_register;
4232
4233 #ifdef CONFIG_IXGBE_DCA
4234         if (dca_add_requester(&pdev->dev) == 0) {
4235                 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
4236                 /* always use CB2 mode, difference is masked
4237                  * in the CB driver */
4238                 IXGBE_WRITE_REG(hw, IXGBE_DCA_CTRL, 2);
4239                 ixgbe_setup_dca(adapter);
4240         }
4241 #endif
4242
4243         dev_info(&pdev->dev, "Intel(R) 10 Gigabit Network Connection\n");
4244         cards_found++;
4245         return 0;
4246
4247 err_register:
4248         ixgbe_release_hw_control(adapter);
4249 err_hw_init:
4250 err_sw_init:
4251         ixgbe_reset_interrupt_capability(adapter);
4252 err_eeprom:
4253         clear_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
4254         del_timer_sync(&adapter->sfp_timer);
4255         cancel_work_sync(&adapter->sfp_task);
4256         iounmap(hw->hw_addr);
4257 err_ioremap:
4258         free_netdev(netdev);
4259 err_alloc_etherdev:
4260         pci_release_regions(pdev);
4261 err_pci_reg:
4262 err_dma:
4263         pci_disable_device(pdev);
4264         return err;
4265 }
4266
4267 /**
4268  * ixgbe_remove - Device Removal Routine
4269  * @pdev: PCI device information struct
4270  *
4271  * ixgbe_remove is called by the PCI subsystem to alert the driver
4272  * that it should release a PCI device.  The could be caused by a
4273  * Hot-Plug event, or because the driver is going to be removed from
4274  * memory.
4275  **/
4276 static void __devexit ixgbe_remove(struct pci_dev *pdev)
4277 {
4278         struct net_device *netdev = pci_get_drvdata(pdev);
4279         struct ixgbe_adapter *adapter = netdev_priv(netdev);
4280         int err;
4281
4282         set_bit(__IXGBE_DOWN, &adapter->state);
4283         /* clear the module not found bit to make sure the worker won't
4284          * reschedule
4285          */
4286         clear_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
4287         del_timer_sync(&adapter->watchdog_timer);
4288
4289         del_timer_sync(&adapter->sfp_timer);
4290         cancel_work_sync(&adapter->watchdog_task);
4291         cancel_work_sync(&adapter->sfp_task);
4292         flush_scheduled_work();
4293
4294 #ifdef CONFIG_IXGBE_DCA
4295         if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
4296                 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
4297                 dca_remove_requester(&pdev->dev);
4298                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
4299         }
4300
4301 #endif
4302         if (netdev->reg_state == NETREG_REGISTERED)
4303                 unregister_netdev(netdev);
4304
4305         ixgbe_reset_interrupt_capability(adapter);
4306
4307         ixgbe_release_hw_control(adapter);
4308
4309         iounmap(adapter->hw.hw_addr);
4310         pci_release_regions(pdev);
4311
4312         DPRINTK(PROBE, INFO, "complete\n");
4313         kfree(adapter->tx_ring);
4314         kfree(adapter->rx_ring);
4315
4316         free_netdev(netdev);
4317
4318         err = pci_disable_pcie_error_reporting(pdev);
4319         if (err)
4320                 dev_err(&pdev->dev,
4321                         "pci_disable_pcie_error_reporting failed 0x%x\n", err);
4322
4323         pci_disable_device(pdev);
4324 }
4325
4326 /**
4327  * ixgbe_io_error_detected - called when PCI error is detected
4328  * @pdev: Pointer to PCI device
4329  * @state: The current pci connection state
4330  *
4331  * This function is called after a PCI bus error affecting
4332  * this device has been detected.
4333  */
4334 static pci_ers_result_t ixgbe_io_error_detected(struct pci_dev *pdev,
4335                                                 pci_channel_state_t state)
4336 {
4337         struct net_device *netdev = pci_get_drvdata(pdev);
4338         struct ixgbe_adapter *adapter = netdev_priv(netdev);
4339
4340         netif_device_detach(netdev);
4341
4342         if (netif_running(netdev))
4343                 ixgbe_down(adapter);
4344         pci_disable_device(pdev);
4345
4346         /* Request a slot reset. */
4347         return PCI_ERS_RESULT_NEED_RESET;
4348 }
4349
4350 /**
4351  * ixgbe_io_slot_reset - called after the pci bus has been reset.
4352  * @pdev: Pointer to PCI device
4353  *
4354  * Restart the card from scratch, as if from a cold-boot.
4355  */
4356 static pci_ers_result_t ixgbe_io_slot_reset(struct pci_dev *pdev)
4357 {
4358         struct net_device *netdev = pci_get_drvdata(pdev);
4359         struct ixgbe_adapter *adapter = netdev_priv(netdev);
4360         pci_ers_result_t result;
4361         int err;
4362
4363         if (pci_enable_device(pdev)) {
4364                 DPRINTK(PROBE, ERR,
4365                         "Cannot re-enable PCI device after reset.\n");
4366                 result = PCI_ERS_RESULT_DISCONNECT;
4367         } else {
4368                 pci_set_master(pdev);
4369                 pci_restore_state(pdev);
4370
4371                 pci_enable_wake(pdev, PCI_D3hot, 0);
4372                 pci_enable_wake(pdev, PCI_D3cold, 0);
4373
4374                 ixgbe_reset(adapter);
4375
4376                 result = PCI_ERS_RESULT_RECOVERED;
4377         }
4378
4379         err = pci_cleanup_aer_uncorrect_error_status(pdev);
4380         if (err) {
4381                 dev_err(&pdev->dev,
4382                   "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n", err);
4383                 /* non-fatal, continue */
4384         }
4385
4386         return result;
4387 }
4388
4389 /**
4390  * ixgbe_io_resume - called when traffic can start flowing again.
4391  * @pdev: Pointer to PCI device
4392  *
4393  * This callback is called when the error recovery driver tells us that
4394  * its OK to resume normal operation.
4395  */
4396 static void ixgbe_io_resume(struct pci_dev *pdev)
4397 {
4398         struct net_device *netdev = pci_get_drvdata(pdev);
4399         struct ixgbe_adapter *adapter = netdev_priv(netdev);
4400
4401         if (netif_running(netdev)) {
4402                 if (ixgbe_up(adapter)) {
4403                         DPRINTK(PROBE, INFO, "ixgbe_up failed after reset\n");
4404                         return;
4405                 }
4406         }
4407
4408         netif_device_attach(netdev);
4409 }
4410
4411 static struct pci_error_handlers ixgbe_err_handler = {
4412         .error_detected = ixgbe_io_error_detected,
4413         .slot_reset = ixgbe_io_slot_reset,
4414         .resume = ixgbe_io_resume,
4415 };
4416
4417 static struct pci_driver ixgbe_driver = {
4418         .name     = ixgbe_driver_name,
4419         .id_table = ixgbe_pci_tbl,
4420         .probe    = ixgbe_probe,
4421         .remove   = __devexit_p(ixgbe_remove),
4422 #ifdef CONFIG_PM
4423         .suspend  = ixgbe_suspend,
4424         .resume   = ixgbe_resume,
4425 #endif
4426         .shutdown = ixgbe_shutdown,
4427         .err_handler = &ixgbe_err_handler
4428 };
4429
4430 /**
4431  * ixgbe_init_module - Driver Registration Routine
4432  *
4433  * ixgbe_init_module is the first routine called when the driver is
4434  * loaded. All it does is register with the PCI subsystem.
4435  **/
4436 static int __init ixgbe_init_module(void)
4437 {
4438         int ret;
4439         printk(KERN_INFO "%s: %s - version %s\n", ixgbe_driver_name,
4440                ixgbe_driver_string, ixgbe_driver_version);
4441
4442         printk(KERN_INFO "%s: %s\n", ixgbe_driver_name, ixgbe_copyright);
4443
4444 #ifdef CONFIG_IXGBE_DCA
4445         dca_register_notify(&dca_notifier);
4446 #endif
4447
4448         ret = pci_register_driver(&ixgbe_driver);
4449         return ret;
4450 }
4451
4452 module_init(ixgbe_init_module);
4453
4454 /**
4455  * ixgbe_exit_module - Driver Exit Cleanup Routine
4456  *
4457  * ixgbe_exit_module is called just before the driver is removed
4458  * from memory.
4459  **/
4460 static void __exit ixgbe_exit_module(void)
4461 {
4462 #ifdef CONFIG_IXGBE_DCA
4463         dca_unregister_notify(&dca_notifier);
4464 #endif
4465         pci_unregister_driver(&ixgbe_driver);
4466 }
4467
4468 #ifdef CONFIG_IXGBE_DCA
4469 static int ixgbe_notify_dca(struct notifier_block *nb, unsigned long event,
4470                             void *p)
4471 {
4472         int ret_val;
4473
4474         ret_val = driver_for_each_device(&ixgbe_driver.driver, NULL, &event,
4475                                          __ixgbe_notify_dca);
4476
4477         return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
4478 }
4479 #endif /* CONFIG_IXGBE_DCA */
4480
4481 module_exit(ixgbe_exit_module);
4482
4483 /* ixgbe_main.c */