]> Pileus Git - ~andy/linux/blob - drivers/net/ethernet/intel/ixgbe/ixgbe_main.c
ixgbe: allow RAR table to be updated in promisc mode
[~andy/linux] / drivers / net / ethernet / intel / ixgbe / ixgbe_main.c
1 /*******************************************************************************
2
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2012 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/interrupt.h>
36 #include <linux/ip.h>
37 #include <linux/tcp.h>
38 #include <linux/sctp.h>
39 #include <linux/pkt_sched.h>
40 #include <linux/ipv6.h>
41 #include <linux/slab.h>
42 #include <net/checksum.h>
43 #include <net/ip6_checksum.h>
44 #include <linux/ethtool.h>
45 #include <linux/if.h>
46 #include <linux/if_vlan.h>
47 #include <linux/prefetch.h>
48 #include <scsi/fc/fc_fcoe.h>
49
50 #include "ixgbe.h"
51 #include "ixgbe_common.h"
52 #include "ixgbe_dcb_82599.h"
53 #include "ixgbe_sriov.h"
54
55 char ixgbe_driver_name[] = "ixgbe";
56 static const char ixgbe_driver_string[] =
57                               "Intel(R) 10 Gigabit PCI Express Network Driver";
58 #ifdef IXGBE_FCOE
59 char ixgbe_default_device_descr[] =
60                               "Intel(R) 10 Gigabit Network Connection";
61 #else
62 static char ixgbe_default_device_descr[] =
63                               "Intel(R) 10 Gigabit Network Connection";
64 #endif
65 #define MAJ 3
66 #define MIN 8
67 #define BUILD 21
68 #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
69         __stringify(BUILD) "-k"
70 const char ixgbe_driver_version[] = DRV_VERSION;
71 static const char ixgbe_copyright[] =
72                                 "Copyright (c) 1999-2012 Intel Corporation.";
73
74 static const struct ixgbe_info *ixgbe_info_tbl[] = {
75         [board_82598] = &ixgbe_82598_info,
76         [board_82599] = &ixgbe_82599_info,
77         [board_X540] = &ixgbe_X540_info,
78 };
79
80 /* ixgbe_pci_tbl - PCI Device ID Table
81  *
82  * Wildcard entries (PCI_ANY_ID) should come last
83  * Last entry must be all 0s
84  *
85  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
86  *   Class, Class Mask, private data (not used) }
87  */
88 static DEFINE_PCI_DEVICE_TABLE(ixgbe_pci_tbl) = {
89         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598), board_82598 },
90         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_DUAL_PORT), board_82598 },
91         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_SINGLE_PORT), board_82598 },
92         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT), board_82598 },
93         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT2), board_82598 },
94         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_CX4), board_82598 },
95         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_CX4_DUAL_PORT), board_82598 },
96         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_DA_DUAL_PORT), board_82598 },
97         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM), board_82598 },
98         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_XF_LR), board_82598 },
99         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_SFP_LOM), board_82598 },
100         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_BX), board_82598 },
101         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KX4), board_82599 },
102         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_XAUI_LOM), board_82599 },
103         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KR), board_82599 },
104         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP), board_82599 },
105         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_EM), board_82599 },
106         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KX4_MEZZ), board_82599 },
107         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_CX4), board_82599 },
108         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_BACKPLANE_FCOE), board_82599 },
109         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_FCOE), board_82599 },
110         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_T3_LOM), board_82599 },
111         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_COMBO_BACKPLANE), board_82599 },
112         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540T), board_X540 },
113         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_SF2), board_82599 },
114         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_LS), board_82599 },
115         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599EN_SFP), board_82599 },
116         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_SF_QP), board_82599 },
117         /* required last entry */
118         {0, }
119 };
120 MODULE_DEVICE_TABLE(pci, ixgbe_pci_tbl);
121
122 #ifdef CONFIG_IXGBE_DCA
123 static int ixgbe_notify_dca(struct notifier_block *, unsigned long event,
124                             void *p);
125 static struct notifier_block dca_notifier = {
126         .notifier_call = ixgbe_notify_dca,
127         .next          = NULL,
128         .priority      = 0
129 };
130 #endif
131
132 #ifdef CONFIG_PCI_IOV
133 static unsigned int max_vfs;
134 module_param(max_vfs, uint, 0);
135 MODULE_PARM_DESC(max_vfs,
136                  "Maximum number of virtual functions to allocate per physical function");
137 #endif /* CONFIG_PCI_IOV */
138
139 static unsigned int allow_unsupported_sfp;
140 module_param(allow_unsupported_sfp, uint, 0);
141 MODULE_PARM_DESC(allow_unsupported_sfp,
142                  "Allow unsupported and untested SFP+ modules on 82599-based adapters");
143
144 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
145 static int debug = -1;
146 module_param(debug, int, 0);
147 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
148
149 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
150 MODULE_DESCRIPTION("Intel(R) 10 Gigabit PCI Express Network Driver");
151 MODULE_LICENSE("GPL");
152 MODULE_VERSION(DRV_VERSION);
153
154 static void ixgbe_service_event_schedule(struct ixgbe_adapter *adapter)
155 {
156         if (!test_bit(__IXGBE_DOWN, &adapter->state) &&
157             !test_and_set_bit(__IXGBE_SERVICE_SCHED, &adapter->state))
158                 schedule_work(&adapter->service_task);
159 }
160
161 static void ixgbe_service_event_complete(struct ixgbe_adapter *adapter)
162 {
163         BUG_ON(!test_bit(__IXGBE_SERVICE_SCHED, &adapter->state));
164
165         /* flush memory to make sure state is correct before next watchdog */
166         smp_mb__before_clear_bit();
167         clear_bit(__IXGBE_SERVICE_SCHED, &adapter->state);
168 }
169
170 struct ixgbe_reg_info {
171         u32 ofs;
172         char *name;
173 };
174
175 static const struct ixgbe_reg_info ixgbe_reg_info_tbl[] = {
176
177         /* General Registers */
178         {IXGBE_CTRL, "CTRL"},
179         {IXGBE_STATUS, "STATUS"},
180         {IXGBE_CTRL_EXT, "CTRL_EXT"},
181
182         /* Interrupt Registers */
183         {IXGBE_EICR, "EICR"},
184
185         /* RX Registers */
186         {IXGBE_SRRCTL(0), "SRRCTL"},
187         {IXGBE_DCA_RXCTRL(0), "DRXCTL"},
188         {IXGBE_RDLEN(0), "RDLEN"},
189         {IXGBE_RDH(0), "RDH"},
190         {IXGBE_RDT(0), "RDT"},
191         {IXGBE_RXDCTL(0), "RXDCTL"},
192         {IXGBE_RDBAL(0), "RDBAL"},
193         {IXGBE_RDBAH(0), "RDBAH"},
194
195         /* TX Registers */
196         {IXGBE_TDBAL(0), "TDBAL"},
197         {IXGBE_TDBAH(0), "TDBAH"},
198         {IXGBE_TDLEN(0), "TDLEN"},
199         {IXGBE_TDH(0), "TDH"},
200         {IXGBE_TDT(0), "TDT"},
201         {IXGBE_TXDCTL(0), "TXDCTL"},
202
203         /* List Terminator */
204         {}
205 };
206
207
208 /*
209  * ixgbe_regdump - register printout routine
210  */
211 static void ixgbe_regdump(struct ixgbe_hw *hw, struct ixgbe_reg_info *reginfo)
212 {
213         int i = 0, j = 0;
214         char rname[16];
215         u32 regs[64];
216
217         switch (reginfo->ofs) {
218         case IXGBE_SRRCTL(0):
219                 for (i = 0; i < 64; i++)
220                         regs[i] = IXGBE_READ_REG(hw, IXGBE_SRRCTL(i));
221                 break;
222         case IXGBE_DCA_RXCTRL(0):
223                 for (i = 0; i < 64; i++)
224                         regs[i] = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(i));
225                 break;
226         case IXGBE_RDLEN(0):
227                 for (i = 0; i < 64; i++)
228                         regs[i] = IXGBE_READ_REG(hw, IXGBE_RDLEN(i));
229                 break;
230         case IXGBE_RDH(0):
231                 for (i = 0; i < 64; i++)
232                         regs[i] = IXGBE_READ_REG(hw, IXGBE_RDH(i));
233                 break;
234         case IXGBE_RDT(0):
235                 for (i = 0; i < 64; i++)
236                         regs[i] = IXGBE_READ_REG(hw, IXGBE_RDT(i));
237                 break;
238         case IXGBE_RXDCTL(0):
239                 for (i = 0; i < 64; i++)
240                         regs[i] = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
241                 break;
242         case IXGBE_RDBAL(0):
243                 for (i = 0; i < 64; i++)
244                         regs[i] = IXGBE_READ_REG(hw, IXGBE_RDBAL(i));
245                 break;
246         case IXGBE_RDBAH(0):
247                 for (i = 0; i < 64; i++)
248                         regs[i] = IXGBE_READ_REG(hw, IXGBE_RDBAH(i));
249                 break;
250         case IXGBE_TDBAL(0):
251                 for (i = 0; i < 64; i++)
252                         regs[i] = IXGBE_READ_REG(hw, IXGBE_TDBAL(i));
253                 break;
254         case IXGBE_TDBAH(0):
255                 for (i = 0; i < 64; i++)
256                         regs[i] = IXGBE_READ_REG(hw, IXGBE_TDBAH(i));
257                 break;
258         case IXGBE_TDLEN(0):
259                 for (i = 0; i < 64; i++)
260                         regs[i] = IXGBE_READ_REG(hw, IXGBE_TDLEN(i));
261                 break;
262         case IXGBE_TDH(0):
263                 for (i = 0; i < 64; i++)
264                         regs[i] = IXGBE_READ_REG(hw, IXGBE_TDH(i));
265                 break;
266         case IXGBE_TDT(0):
267                 for (i = 0; i < 64; i++)
268                         regs[i] = IXGBE_READ_REG(hw, IXGBE_TDT(i));
269                 break;
270         case IXGBE_TXDCTL(0):
271                 for (i = 0; i < 64; i++)
272                         regs[i] = IXGBE_READ_REG(hw, IXGBE_TXDCTL(i));
273                 break;
274         default:
275                 pr_info("%-15s %08x\n", reginfo->name,
276                         IXGBE_READ_REG(hw, reginfo->ofs));
277                 return;
278         }
279
280         for (i = 0; i < 8; i++) {
281                 snprintf(rname, 16, "%s[%d-%d]", reginfo->name, i*8, i*8+7);
282                 pr_err("%-15s", rname);
283                 for (j = 0; j < 8; j++)
284                         pr_cont(" %08x", regs[i*8+j]);
285                 pr_cont("\n");
286         }
287
288 }
289
290 /*
291  * ixgbe_dump - Print registers, tx-rings and rx-rings
292  */
293 static void ixgbe_dump(struct ixgbe_adapter *adapter)
294 {
295         struct net_device *netdev = adapter->netdev;
296         struct ixgbe_hw *hw = &adapter->hw;
297         struct ixgbe_reg_info *reginfo;
298         int n = 0;
299         struct ixgbe_ring *tx_ring;
300         struct ixgbe_tx_buffer *tx_buffer;
301         union ixgbe_adv_tx_desc *tx_desc;
302         struct my_u0 { u64 a; u64 b; } *u0;
303         struct ixgbe_ring *rx_ring;
304         union ixgbe_adv_rx_desc *rx_desc;
305         struct ixgbe_rx_buffer *rx_buffer_info;
306         u32 staterr;
307         int i = 0;
308
309         if (!netif_msg_hw(adapter))
310                 return;
311
312         /* Print netdevice Info */
313         if (netdev) {
314                 dev_info(&adapter->pdev->dev, "Net device Info\n");
315                 pr_info("Device Name     state            "
316                         "trans_start      last_rx\n");
317                 pr_info("%-15s %016lX %016lX %016lX\n",
318                         netdev->name,
319                         netdev->state,
320                         netdev->trans_start,
321                         netdev->last_rx);
322         }
323
324         /* Print Registers */
325         dev_info(&adapter->pdev->dev, "Register Dump\n");
326         pr_info(" Register Name   Value\n");
327         for (reginfo = (struct ixgbe_reg_info *)ixgbe_reg_info_tbl;
328              reginfo->name; reginfo++) {
329                 ixgbe_regdump(hw, reginfo);
330         }
331
332         /* Print TX Ring Summary */
333         if (!netdev || !netif_running(netdev))
334                 goto exit;
335
336         dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
337         pr_info("Queue [NTU] [NTC] [bi(ntc)->dma  ] leng ntw timestamp\n");
338         for (n = 0; n < adapter->num_tx_queues; n++) {
339                 tx_ring = adapter->tx_ring[n];
340                 tx_buffer = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
341                 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
342                            n, tx_ring->next_to_use, tx_ring->next_to_clean,
343                            (u64)dma_unmap_addr(tx_buffer, dma),
344                            dma_unmap_len(tx_buffer, len),
345                            tx_buffer->next_to_watch,
346                            (u64)tx_buffer->time_stamp);
347         }
348
349         /* Print TX Rings */
350         if (!netif_msg_tx_done(adapter))
351                 goto rx_ring_summary;
352
353         dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
354
355         /* Transmit Descriptor Formats
356          *
357          * Advanced Transmit Descriptor
358          *   +--------------------------------------------------------------+
359          * 0 |         Buffer Address [63:0]                                |
360          *   +--------------------------------------------------------------+
361          * 8 |  PAYLEN  | PORTS  | IDX | STA | DCMD  |DTYP |  RSV |  DTALEN |
362          *   +--------------------------------------------------------------+
363          *   63       46 45    40 39 36 35 32 31   24 23 20 19              0
364          */
365
366         for (n = 0; n < adapter->num_tx_queues; n++) {
367                 tx_ring = adapter->tx_ring[n];
368                 pr_info("------------------------------------\n");
369                 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
370                 pr_info("------------------------------------\n");
371                 pr_info("T [desc]     [address 63:0  ] "
372                         "[PlPOIdStDDt Ln] [bi->dma       ] "
373                         "leng  ntw timestamp        bi->skb\n");
374
375                 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
376                         tx_desc = IXGBE_TX_DESC(tx_ring, i);
377                         tx_buffer = &tx_ring->tx_buffer_info[i];
378                         u0 = (struct my_u0 *)tx_desc;
379                         pr_info("T [0x%03X]    %016llX %016llX %016llX"
380                                 " %04X  %p %016llX %p", i,
381                                 le64_to_cpu(u0->a),
382                                 le64_to_cpu(u0->b),
383                                 (u64)dma_unmap_addr(tx_buffer, dma),
384                                 dma_unmap_len(tx_buffer, len),
385                                 tx_buffer->next_to_watch,
386                                 (u64)tx_buffer->time_stamp,
387                                 tx_buffer->skb);
388                         if (i == tx_ring->next_to_use &&
389                                 i == tx_ring->next_to_clean)
390                                 pr_cont(" NTC/U\n");
391                         else if (i == tx_ring->next_to_use)
392                                 pr_cont(" NTU\n");
393                         else if (i == tx_ring->next_to_clean)
394                                 pr_cont(" NTC\n");
395                         else
396                                 pr_cont("\n");
397
398                         if (netif_msg_pktdata(adapter) &&
399                             dma_unmap_len(tx_buffer, len) != 0)
400                                 print_hex_dump(KERN_INFO, "",
401                                         DUMP_PREFIX_ADDRESS, 16, 1,
402                                         phys_to_virt(dma_unmap_addr(tx_buffer,
403                                                                     dma)),
404                                         dma_unmap_len(tx_buffer, len),
405                                         true);
406                 }
407         }
408
409         /* Print RX Rings Summary */
410 rx_ring_summary:
411         dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
412         pr_info("Queue [NTU] [NTC]\n");
413         for (n = 0; n < adapter->num_rx_queues; n++) {
414                 rx_ring = adapter->rx_ring[n];
415                 pr_info("%5d %5X %5X\n",
416                         n, rx_ring->next_to_use, rx_ring->next_to_clean);
417         }
418
419         /* Print RX Rings */
420         if (!netif_msg_rx_status(adapter))
421                 goto exit;
422
423         dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
424
425         /* Advanced Receive Descriptor (Read) Format
426          *    63                                           1        0
427          *    +-----------------------------------------------------+
428          *  0 |       Packet Buffer Address [63:1]           |A0/NSE|
429          *    +----------------------------------------------+------+
430          *  8 |       Header Buffer Address [63:1]           |  DD  |
431          *    +-----------------------------------------------------+
432          *
433          *
434          * Advanced Receive Descriptor (Write-Back) Format
435          *
436          *   63       48 47    32 31  30      21 20 16 15   4 3     0
437          *   +------------------------------------------------------+
438          * 0 | Packet     IP     |SPH| HDR_LEN   | RSV|Packet|  RSS |
439          *   | Checksum   Ident  |   |           |    | Type | Type |
440          *   +------------------------------------------------------+
441          * 8 | VLAN Tag | Length | Extended Error | Extended Status |
442          *   +------------------------------------------------------+
443          *   63       48 47    32 31            20 19               0
444          */
445         for (n = 0; n < adapter->num_rx_queues; n++) {
446                 rx_ring = adapter->rx_ring[n];
447                 pr_info("------------------------------------\n");
448                 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
449                 pr_info("------------------------------------\n");
450                 pr_info("R  [desc]      [ PktBuf     A0] "
451                         "[  HeadBuf   DD] [bi->dma       ] [bi->skb] "
452                         "<-- Adv Rx Read format\n");
453                 pr_info("RWB[desc]      [PcsmIpSHl PtRs] "
454                         "[vl er S cks ln] ---------------- [bi->skb] "
455                         "<-- Adv Rx Write-Back format\n");
456
457                 for (i = 0; i < rx_ring->count; i++) {
458                         rx_buffer_info = &rx_ring->rx_buffer_info[i];
459                         rx_desc = IXGBE_RX_DESC(rx_ring, i);
460                         u0 = (struct my_u0 *)rx_desc;
461                         staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
462                         if (staterr & IXGBE_RXD_STAT_DD) {
463                                 /* Descriptor Done */
464                                 pr_info("RWB[0x%03X]     %016llX "
465                                         "%016llX ---------------- %p", i,
466                                         le64_to_cpu(u0->a),
467                                         le64_to_cpu(u0->b),
468                                         rx_buffer_info->skb);
469                         } else {
470                                 pr_info("R  [0x%03X]     %016llX "
471                                         "%016llX %016llX %p", i,
472                                         le64_to_cpu(u0->a),
473                                         le64_to_cpu(u0->b),
474                                         (u64)rx_buffer_info->dma,
475                                         rx_buffer_info->skb);
476
477                                 if (netif_msg_pktdata(adapter)) {
478                                         print_hex_dump(KERN_INFO, "",
479                                            DUMP_PREFIX_ADDRESS, 16, 1,
480                                            phys_to_virt(rx_buffer_info->dma),
481                                            ixgbe_rx_bufsz(rx_ring), true);
482                                 }
483                         }
484
485                         if (i == rx_ring->next_to_use)
486                                 pr_cont(" NTU\n");
487                         else if (i == rx_ring->next_to_clean)
488                                 pr_cont(" NTC\n");
489                         else
490                                 pr_cont("\n");
491
492                 }
493         }
494
495 exit:
496         return;
497 }
498
499 static void ixgbe_release_hw_control(struct ixgbe_adapter *adapter)
500 {
501         u32 ctrl_ext;
502
503         /* Let firmware take over control of h/w */
504         ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
505         IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
506                         ctrl_ext & ~IXGBE_CTRL_EXT_DRV_LOAD);
507 }
508
509 static void ixgbe_get_hw_control(struct ixgbe_adapter *adapter)
510 {
511         u32 ctrl_ext;
512
513         /* Let firmware know the driver has taken over */
514         ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
515         IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
516                         ctrl_ext | IXGBE_CTRL_EXT_DRV_LOAD);
517 }
518
519 /*
520  * ixgbe_set_ivar - set the IVAR registers, mapping interrupt causes to vectors
521  * @adapter: pointer to adapter struct
522  * @direction: 0 for Rx, 1 for Tx, -1 for other causes
523  * @queue: queue to map the corresponding interrupt to
524  * @msix_vector: the vector to map to the corresponding queue
525  *
526  */
527 static void ixgbe_set_ivar(struct ixgbe_adapter *adapter, s8 direction,
528                            u8 queue, u8 msix_vector)
529 {
530         u32 ivar, index;
531         struct ixgbe_hw *hw = &adapter->hw;
532         switch (hw->mac.type) {
533         case ixgbe_mac_82598EB:
534                 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
535                 if (direction == -1)
536                         direction = 0;
537                 index = (((direction * 64) + queue) >> 2) & 0x1F;
538                 ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
539                 ivar &= ~(0xFF << (8 * (queue & 0x3)));
540                 ivar |= (msix_vector << (8 * (queue & 0x3)));
541                 IXGBE_WRITE_REG(hw, IXGBE_IVAR(index), ivar);
542                 break;
543         case ixgbe_mac_82599EB:
544         case ixgbe_mac_X540:
545                 if (direction == -1) {
546                         /* other causes */
547                         msix_vector |= IXGBE_IVAR_ALLOC_VAL;
548                         index = ((queue & 1) * 8);
549                         ivar = IXGBE_READ_REG(&adapter->hw, IXGBE_IVAR_MISC);
550                         ivar &= ~(0xFF << index);
551                         ivar |= (msix_vector << index);
552                         IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR_MISC, ivar);
553                         break;
554                 } else {
555                         /* tx or rx causes */
556                         msix_vector |= IXGBE_IVAR_ALLOC_VAL;
557                         index = ((16 * (queue & 1)) + (8 * direction));
558                         ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(queue >> 1));
559                         ivar &= ~(0xFF << index);
560                         ivar |= (msix_vector << index);
561                         IXGBE_WRITE_REG(hw, IXGBE_IVAR(queue >> 1), ivar);
562                         break;
563                 }
564         default:
565                 break;
566         }
567 }
568
569 static inline void ixgbe_irq_rearm_queues(struct ixgbe_adapter *adapter,
570                                           u64 qmask)
571 {
572         u32 mask;
573
574         switch (adapter->hw.mac.type) {
575         case ixgbe_mac_82598EB:
576                 mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
577                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, mask);
578                 break;
579         case ixgbe_mac_82599EB:
580         case ixgbe_mac_X540:
581                 mask = (qmask & 0xFFFFFFFF);
582                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(0), mask);
583                 mask = (qmask >> 32);
584                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(1), mask);
585                 break;
586         default:
587                 break;
588         }
589 }
590
591 void ixgbe_unmap_and_free_tx_resource(struct ixgbe_ring *ring,
592                                       struct ixgbe_tx_buffer *tx_buffer)
593 {
594         if (tx_buffer->skb) {
595                 dev_kfree_skb_any(tx_buffer->skb);
596                 if (dma_unmap_len(tx_buffer, len))
597                         dma_unmap_single(ring->dev,
598                                          dma_unmap_addr(tx_buffer, dma),
599                                          dma_unmap_len(tx_buffer, len),
600                                          DMA_TO_DEVICE);
601         } else if (dma_unmap_len(tx_buffer, len)) {
602                 dma_unmap_page(ring->dev,
603                                dma_unmap_addr(tx_buffer, dma),
604                                dma_unmap_len(tx_buffer, len),
605                                DMA_TO_DEVICE);
606         }
607         tx_buffer->next_to_watch = NULL;
608         tx_buffer->skb = NULL;
609         dma_unmap_len_set(tx_buffer, len, 0);
610         /* tx_buffer must be completely set up in the transmit path */
611 }
612
613 static void ixgbe_update_xoff_received(struct ixgbe_adapter *adapter)
614 {
615         struct ixgbe_hw *hw = &adapter->hw;
616         struct ixgbe_hw_stats *hwstats = &adapter->stats;
617         u32 data = 0;
618         u32 xoff[8] = {0};
619         int i;
620
621         if ((hw->fc.current_mode == ixgbe_fc_full) ||
622             (hw->fc.current_mode == ixgbe_fc_rx_pause)) {
623                 switch (hw->mac.type) {
624                 case ixgbe_mac_82598EB:
625                         data = IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
626                         break;
627                 default:
628                         data = IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
629                 }
630                 hwstats->lxoffrxc += data;
631
632                 /* refill credits (no tx hang) if we received xoff */
633                 if (!data)
634                         return;
635
636                 for (i = 0; i < adapter->num_tx_queues; i++)
637                         clear_bit(__IXGBE_HANG_CHECK_ARMED,
638                                   &adapter->tx_ring[i]->state);
639                 return;
640         } else if (!(adapter->dcb_cfg.pfc_mode_enable))
641                 return;
642
643         /* update stats for each tc, only valid with PFC enabled */
644         for (i = 0; i < MAX_TX_PACKET_BUFFERS; i++) {
645                 switch (hw->mac.type) {
646                 case ixgbe_mac_82598EB:
647                         xoff[i] = IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
648                         break;
649                 default:
650                         xoff[i] = IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
651                 }
652                 hwstats->pxoffrxc[i] += xoff[i];
653         }
654
655         /* disarm tx queues that have received xoff frames */
656         for (i = 0; i < adapter->num_tx_queues; i++) {
657                 struct ixgbe_ring *tx_ring = adapter->tx_ring[i];
658                 u8 tc = tx_ring->dcb_tc;
659
660                 if (xoff[tc])
661                         clear_bit(__IXGBE_HANG_CHECK_ARMED, &tx_ring->state);
662         }
663 }
664
665 static u64 ixgbe_get_tx_completed(struct ixgbe_ring *ring)
666 {
667         return ring->stats.packets;
668 }
669
670 static u64 ixgbe_get_tx_pending(struct ixgbe_ring *ring)
671 {
672         struct ixgbe_adapter *adapter = netdev_priv(ring->netdev);
673         struct ixgbe_hw *hw = &adapter->hw;
674
675         u32 head = IXGBE_READ_REG(hw, IXGBE_TDH(ring->reg_idx));
676         u32 tail = IXGBE_READ_REG(hw, IXGBE_TDT(ring->reg_idx));
677
678         if (head != tail)
679                 return (head < tail) ?
680                         tail - head : (tail + ring->count - head);
681
682         return 0;
683 }
684
685 static inline bool ixgbe_check_tx_hang(struct ixgbe_ring *tx_ring)
686 {
687         u32 tx_done = ixgbe_get_tx_completed(tx_ring);
688         u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
689         u32 tx_pending = ixgbe_get_tx_pending(tx_ring);
690         bool ret = false;
691
692         clear_check_for_tx_hang(tx_ring);
693
694         /*
695          * Check for a hung queue, but be thorough. This verifies
696          * that a transmit has been completed since the previous
697          * check AND there is at least one packet pending. The
698          * ARMED bit is set to indicate a potential hang. The
699          * bit is cleared if a pause frame is received to remove
700          * false hang detection due to PFC or 802.3x frames. By
701          * requiring this to fail twice we avoid races with
702          * pfc clearing the ARMED bit and conditions where we
703          * run the check_tx_hang logic with a transmit completion
704          * pending but without time to complete it yet.
705          */
706         if ((tx_done_old == tx_done) && tx_pending) {
707                 /* make sure it is true for two checks in a row */
708                 ret = test_and_set_bit(__IXGBE_HANG_CHECK_ARMED,
709                                        &tx_ring->state);
710         } else {
711                 /* update completed stats and continue */
712                 tx_ring->tx_stats.tx_done_old = tx_done;
713                 /* reset the countdown */
714                 clear_bit(__IXGBE_HANG_CHECK_ARMED, &tx_ring->state);
715         }
716
717         return ret;
718 }
719
720 /**
721  * ixgbe_tx_timeout_reset - initiate reset due to Tx timeout
722  * @adapter: driver private struct
723  **/
724 static void ixgbe_tx_timeout_reset(struct ixgbe_adapter *adapter)
725 {
726
727         /* Do the reset outside of interrupt context */
728         if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
729                 adapter->flags2 |= IXGBE_FLAG2_RESET_REQUESTED;
730                 ixgbe_service_event_schedule(adapter);
731         }
732 }
733
734 /**
735  * ixgbe_clean_tx_irq - Reclaim resources after transmit completes
736  * @q_vector: structure containing interrupt and ring information
737  * @tx_ring: tx ring to clean
738  **/
739 static bool ixgbe_clean_tx_irq(struct ixgbe_q_vector *q_vector,
740                                struct ixgbe_ring *tx_ring)
741 {
742         struct ixgbe_adapter *adapter = q_vector->adapter;
743         struct ixgbe_tx_buffer *tx_buffer;
744         union ixgbe_adv_tx_desc *tx_desc;
745         unsigned int total_bytes = 0, total_packets = 0;
746         unsigned int budget = q_vector->tx.work_limit;
747         unsigned int i = tx_ring->next_to_clean;
748
749         if (test_bit(__IXGBE_DOWN, &adapter->state))
750                 return true;
751
752         tx_buffer = &tx_ring->tx_buffer_info[i];
753         tx_desc = IXGBE_TX_DESC(tx_ring, i);
754         i -= tx_ring->count;
755
756         do {
757                 union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
758
759                 /* if next_to_watch is not set then there is no work pending */
760                 if (!eop_desc)
761                         break;
762
763                 /* prevent any other reads prior to eop_desc */
764                 rmb();
765
766                 /* if DD is not set pending work has not been completed */
767                 if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
768                         break;
769
770                 /* clear next_to_watch to prevent false hangs */
771                 tx_buffer->next_to_watch = NULL;
772
773                 /* update the statistics for this packet */
774                 total_bytes += tx_buffer->bytecount;
775                 total_packets += tx_buffer->gso_segs;
776
777                 /* free the skb */
778                 dev_kfree_skb_any(tx_buffer->skb);
779
780                 /* unmap skb header data */
781                 dma_unmap_single(tx_ring->dev,
782                                  dma_unmap_addr(tx_buffer, dma),
783                                  dma_unmap_len(tx_buffer, len),
784                                  DMA_TO_DEVICE);
785
786                 /* clear tx_buffer data */
787                 tx_buffer->skb = NULL;
788                 dma_unmap_len_set(tx_buffer, len, 0);
789
790                 /* unmap remaining buffers */
791                 while (tx_desc != eop_desc) {
792                         tx_buffer++;
793                         tx_desc++;
794                         i++;
795                         if (unlikely(!i)) {
796                                 i -= tx_ring->count;
797                                 tx_buffer = tx_ring->tx_buffer_info;
798                                 tx_desc = IXGBE_TX_DESC(tx_ring, 0);
799                         }
800
801                         /* unmap any remaining paged data */
802                         if (dma_unmap_len(tx_buffer, len)) {
803                                 dma_unmap_page(tx_ring->dev,
804                                                dma_unmap_addr(tx_buffer, dma),
805                                                dma_unmap_len(tx_buffer, len),
806                                                DMA_TO_DEVICE);
807                                 dma_unmap_len_set(tx_buffer, len, 0);
808                         }
809                 }
810
811                 /* move us one more past the eop_desc for start of next pkt */
812                 tx_buffer++;
813                 tx_desc++;
814                 i++;
815                 if (unlikely(!i)) {
816                         i -= tx_ring->count;
817                         tx_buffer = tx_ring->tx_buffer_info;
818                         tx_desc = IXGBE_TX_DESC(tx_ring, 0);
819                 }
820
821                 /* issue prefetch for next Tx descriptor */
822                 prefetch(tx_desc);
823
824                 /* update budget accounting */
825                 budget--;
826         } while (likely(budget));
827
828         i += tx_ring->count;
829         tx_ring->next_to_clean = i;
830         u64_stats_update_begin(&tx_ring->syncp);
831         tx_ring->stats.bytes += total_bytes;
832         tx_ring->stats.packets += total_packets;
833         u64_stats_update_end(&tx_ring->syncp);
834         q_vector->tx.total_bytes += total_bytes;
835         q_vector->tx.total_packets += total_packets;
836
837         if (check_for_tx_hang(tx_ring) && ixgbe_check_tx_hang(tx_ring)) {
838                 /* schedule immediate reset if we believe we hung */
839                 struct ixgbe_hw *hw = &adapter->hw;
840                 e_err(drv, "Detected Tx Unit Hang\n"
841                         "  Tx Queue             <%d>\n"
842                         "  TDH, TDT             <%x>, <%x>\n"
843                         "  next_to_use          <%x>\n"
844                         "  next_to_clean        <%x>\n"
845                         "tx_buffer_info[next_to_clean]\n"
846                         "  time_stamp           <%lx>\n"
847                         "  jiffies              <%lx>\n",
848                         tx_ring->queue_index,
849                         IXGBE_READ_REG(hw, IXGBE_TDH(tx_ring->reg_idx)),
850                         IXGBE_READ_REG(hw, IXGBE_TDT(tx_ring->reg_idx)),
851                         tx_ring->next_to_use, i,
852                         tx_ring->tx_buffer_info[i].time_stamp, jiffies);
853
854                 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
855
856                 e_info(probe,
857                        "tx hang %d detected on queue %d, resetting adapter\n",
858                         adapter->tx_timeout_count + 1, tx_ring->queue_index);
859
860                 /* schedule immediate reset if we believe we hung */
861                 ixgbe_tx_timeout_reset(adapter);
862
863                 /* the adapter is about to reset, no point in enabling stuff */
864                 return true;
865         }
866
867         netdev_tx_completed_queue(txring_txq(tx_ring),
868                                   total_packets, total_bytes);
869
870 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
871         if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
872                      (ixgbe_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
873                 /* Make sure that anybody stopping the queue after this
874                  * sees the new next_to_clean.
875                  */
876                 smp_mb();
877                 if (__netif_subqueue_stopped(tx_ring->netdev,
878                                              tx_ring->queue_index)
879                     && !test_bit(__IXGBE_DOWN, &adapter->state)) {
880                         netif_wake_subqueue(tx_ring->netdev,
881                                             tx_ring->queue_index);
882                         ++tx_ring->tx_stats.restart_queue;
883                 }
884         }
885
886         return !!budget;
887 }
888
889 #ifdef CONFIG_IXGBE_DCA
890 static void ixgbe_update_tx_dca(struct ixgbe_adapter *adapter,
891                                 struct ixgbe_ring *tx_ring,
892                                 int cpu)
893 {
894         struct ixgbe_hw *hw = &adapter->hw;
895         u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
896         u16 reg_offset;
897
898         switch (hw->mac.type) {
899         case ixgbe_mac_82598EB:
900                 reg_offset = IXGBE_DCA_TXCTRL(tx_ring->reg_idx);
901                 break;
902         case ixgbe_mac_82599EB:
903         case ixgbe_mac_X540:
904                 reg_offset = IXGBE_DCA_TXCTRL_82599(tx_ring->reg_idx);
905                 txctrl <<= IXGBE_DCA_TXCTRL_CPUID_SHIFT_82599;
906                 break;
907         default:
908                 /* for unknown hardware do not write register */
909                 return;
910         }
911
912         /*
913          * We can enable relaxed ordering for reads, but not writes when
914          * DCA is enabled.  This is due to a known issue in some chipsets
915          * which will cause the DCA tag to be cleared.
916          */
917         txctrl |= IXGBE_DCA_TXCTRL_DESC_RRO_EN |
918                   IXGBE_DCA_TXCTRL_DATA_RRO_EN |
919                   IXGBE_DCA_TXCTRL_DESC_DCA_EN;
920
921         IXGBE_WRITE_REG(hw, reg_offset, txctrl);
922 }
923
924 static void ixgbe_update_rx_dca(struct ixgbe_adapter *adapter,
925                                 struct ixgbe_ring *rx_ring,
926                                 int cpu)
927 {
928         struct ixgbe_hw *hw = &adapter->hw;
929         u32 rxctrl = dca3_get_tag(rx_ring->dev, cpu);
930         u8 reg_idx = rx_ring->reg_idx;
931
932
933         switch (hw->mac.type) {
934         case ixgbe_mac_82599EB:
935         case ixgbe_mac_X540:
936                 rxctrl <<= IXGBE_DCA_RXCTRL_CPUID_SHIFT_82599;
937                 break;
938         default:
939                 break;
940         }
941
942         /*
943          * We can enable relaxed ordering for reads, but not writes when
944          * DCA is enabled.  This is due to a known issue in some chipsets
945          * which will cause the DCA tag to be cleared.
946          */
947         rxctrl |= IXGBE_DCA_RXCTRL_DESC_RRO_EN |
948                   IXGBE_DCA_RXCTRL_DATA_DCA_EN |
949                   IXGBE_DCA_RXCTRL_DESC_DCA_EN;
950
951         IXGBE_WRITE_REG(hw, IXGBE_DCA_RXCTRL(reg_idx), rxctrl);
952 }
953
954 static void ixgbe_update_dca(struct ixgbe_q_vector *q_vector)
955 {
956         struct ixgbe_adapter *adapter = q_vector->adapter;
957         struct ixgbe_ring *ring;
958         int cpu = get_cpu();
959
960         if (q_vector->cpu == cpu)
961                 goto out_no_update;
962
963         ixgbe_for_each_ring(ring, q_vector->tx)
964                 ixgbe_update_tx_dca(adapter, ring, cpu);
965
966         ixgbe_for_each_ring(ring, q_vector->rx)
967                 ixgbe_update_rx_dca(adapter, ring, cpu);
968
969         q_vector->cpu = cpu;
970 out_no_update:
971         put_cpu();
972 }
973
974 static void ixgbe_setup_dca(struct ixgbe_adapter *adapter)
975 {
976         int num_q_vectors;
977         int i;
978
979         if (!(adapter->flags & IXGBE_FLAG_DCA_ENABLED))
980                 return;
981
982         /* always use CB2 mode, difference is masked in the CB driver */
983         IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 2);
984
985         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
986                 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
987         else
988                 num_q_vectors = 1;
989
990         for (i = 0; i < num_q_vectors; i++) {
991                 adapter->q_vector[i]->cpu = -1;
992                 ixgbe_update_dca(adapter->q_vector[i]);
993         }
994 }
995
996 static int __ixgbe_notify_dca(struct device *dev, void *data)
997 {
998         struct ixgbe_adapter *adapter = dev_get_drvdata(dev);
999         unsigned long event = *(unsigned long *)data;
1000
1001         if (!(adapter->flags & IXGBE_FLAG_DCA_CAPABLE))
1002                 return 0;
1003
1004         switch (event) {
1005         case DCA_PROVIDER_ADD:
1006                 /* if we're already enabled, don't do it again */
1007                 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1008                         break;
1009                 if (dca_add_requester(dev) == 0) {
1010                         adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
1011                         ixgbe_setup_dca(adapter);
1012                         break;
1013                 }
1014                 /* Fall Through since DCA is disabled. */
1015         case DCA_PROVIDER_REMOVE:
1016                 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
1017                         dca_remove_requester(dev);
1018                         adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
1019                         IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
1020                 }
1021                 break;
1022         }
1023
1024         return 0;
1025 }
1026
1027 #endif /* CONFIG_IXGBE_DCA */
1028 static inline void ixgbe_rx_hash(struct ixgbe_ring *ring,
1029                                  union ixgbe_adv_rx_desc *rx_desc,
1030                                  struct sk_buff *skb)
1031 {
1032         if (ring->netdev->features & NETIF_F_RXHASH)
1033                 skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
1034 }
1035
1036 #ifdef IXGBE_FCOE
1037 /**
1038  * ixgbe_rx_is_fcoe - check the rx desc for incoming pkt type
1039  * @adapter: address of board private structure
1040  * @rx_desc: advanced rx descriptor
1041  *
1042  * Returns : true if it is FCoE pkt
1043  */
1044 static inline bool ixgbe_rx_is_fcoe(struct ixgbe_adapter *adapter,
1045                                     union ixgbe_adv_rx_desc *rx_desc)
1046 {
1047         __le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
1048
1049         return (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) &&
1050                ((pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_ETQF_MASK)) ==
1051                 (cpu_to_le16(IXGBE_ETQF_FILTER_FCOE <<
1052                              IXGBE_RXDADV_PKTTYPE_ETQF_SHIFT)));
1053 }
1054
1055 #endif /* IXGBE_FCOE */
1056 /**
1057  * ixgbe_rx_checksum - indicate in skb if hw indicated a good cksum
1058  * @ring: structure containing ring specific data
1059  * @rx_desc: current Rx descriptor being processed
1060  * @skb: skb currently being received and modified
1061  **/
1062 static inline void ixgbe_rx_checksum(struct ixgbe_ring *ring,
1063                                      union ixgbe_adv_rx_desc *rx_desc,
1064                                      struct sk_buff *skb)
1065 {
1066         skb_checksum_none_assert(skb);
1067
1068         /* Rx csum disabled */
1069         if (!(ring->netdev->features & NETIF_F_RXCSUM))
1070                 return;
1071
1072         /* if IP and error */
1073         if (ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_IPCS) &&
1074             ixgbe_test_staterr(rx_desc, IXGBE_RXDADV_ERR_IPE)) {
1075                 ring->rx_stats.csum_err++;
1076                 return;
1077         }
1078
1079         if (!ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
1080                 return;
1081
1082         if (ixgbe_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
1083                 __le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
1084
1085                 /*
1086                  * 82599 errata, UDP frames with a 0 checksum can be marked as
1087                  * checksum errors.
1088                  */
1089                 if ((pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_UDP)) &&
1090                     test_bit(__IXGBE_RX_CSUM_UDP_ZERO_ERR, &ring->state))
1091                         return;
1092
1093                 ring->rx_stats.csum_err++;
1094                 return;
1095         }
1096
1097         /* It must be a TCP or UDP packet with a valid checksum */
1098         skb->ip_summed = CHECKSUM_UNNECESSARY;
1099 }
1100
1101 static inline void ixgbe_release_rx_desc(struct ixgbe_ring *rx_ring, u32 val)
1102 {
1103         rx_ring->next_to_use = val;
1104
1105         /* update next to alloc since we have filled the ring */
1106         rx_ring->next_to_alloc = val;
1107         /*
1108          * Force memory writes to complete before letting h/w
1109          * know there are new descriptors to fetch.  (Only
1110          * applicable for weak-ordered memory model archs,
1111          * such as IA-64).
1112          */
1113         wmb();
1114         writel(val, rx_ring->tail);
1115 }
1116
1117 static bool ixgbe_alloc_mapped_page(struct ixgbe_ring *rx_ring,
1118                                     struct ixgbe_rx_buffer *bi)
1119 {
1120         struct page *page = bi->page;
1121         dma_addr_t dma = bi->dma;
1122
1123         /* since we are recycling buffers we should seldom need to alloc */
1124         if (likely(dma))
1125                 return true;
1126
1127         /* alloc new page for storage */
1128         if (likely(!page)) {
1129                 page = alloc_pages(GFP_ATOMIC | __GFP_COLD,
1130                                    ixgbe_rx_pg_order(rx_ring));
1131                 if (unlikely(!page)) {
1132                         rx_ring->rx_stats.alloc_rx_page_failed++;
1133                         return false;
1134                 }
1135                 bi->page = page;
1136         }
1137
1138         /* map page for use */
1139         dma = dma_map_page(rx_ring->dev, page, 0,
1140                            ixgbe_rx_pg_size(rx_ring), DMA_FROM_DEVICE);
1141
1142         /*
1143          * if mapping failed free memory back to system since
1144          * there isn't much point in holding memory we can't use
1145          */
1146         if (dma_mapping_error(rx_ring->dev, dma)) {
1147                 put_page(page);
1148                 bi->page = NULL;
1149
1150                 rx_ring->rx_stats.alloc_rx_page_failed++;
1151                 return false;
1152         }
1153
1154         bi->dma = dma;
1155         bi->page_offset ^= ixgbe_rx_bufsz(rx_ring);
1156
1157         return true;
1158 }
1159
1160 /**
1161  * ixgbe_alloc_rx_buffers - Replace used receive buffers
1162  * @rx_ring: ring to place buffers on
1163  * @cleaned_count: number of buffers to replace
1164  **/
1165 void ixgbe_alloc_rx_buffers(struct ixgbe_ring *rx_ring, u16 cleaned_count)
1166 {
1167         union ixgbe_adv_rx_desc *rx_desc;
1168         struct ixgbe_rx_buffer *bi;
1169         u16 i = rx_ring->next_to_use;
1170
1171         /* nothing to do */
1172         if (!cleaned_count)
1173                 return;
1174
1175         rx_desc = IXGBE_RX_DESC(rx_ring, i);
1176         bi = &rx_ring->rx_buffer_info[i];
1177         i -= rx_ring->count;
1178
1179         do {
1180                 if (!ixgbe_alloc_mapped_page(rx_ring, bi))
1181                         break;
1182
1183                 /*
1184                  * Refresh the desc even if buffer_addrs didn't change
1185                  * because each write-back erases this info.
1186                  */
1187                 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
1188
1189                 rx_desc++;
1190                 bi++;
1191                 i++;
1192                 if (unlikely(!i)) {
1193                         rx_desc = IXGBE_RX_DESC(rx_ring, 0);
1194                         bi = rx_ring->rx_buffer_info;
1195                         i -= rx_ring->count;
1196                 }
1197
1198                 /* clear the hdr_addr for the next_to_use descriptor */
1199                 rx_desc->read.hdr_addr = 0;
1200
1201                 cleaned_count--;
1202         } while (cleaned_count);
1203
1204         i += rx_ring->count;
1205
1206         if (rx_ring->next_to_use != i)
1207                 ixgbe_release_rx_desc(rx_ring, i);
1208 }
1209
1210 /**
1211  * ixgbe_get_headlen - determine size of header for RSC/LRO/GRO/FCOE
1212  * @data: pointer to the start of the headers
1213  * @max_len: total length of section to find headers in
1214  *
1215  * This function is meant to determine the length of headers that will
1216  * be recognized by hardware for LRO, GRO, and RSC offloads.  The main
1217  * motivation of doing this is to only perform one pull for IPv4 TCP
1218  * packets so that we can do basic things like calculating the gso_size
1219  * based on the average data per packet.
1220  **/
1221 static unsigned int ixgbe_get_headlen(unsigned char *data,
1222                                       unsigned int max_len)
1223 {
1224         union {
1225                 unsigned char *network;
1226                 /* l2 headers */
1227                 struct ethhdr *eth;
1228                 struct vlan_hdr *vlan;
1229                 /* l3 headers */
1230                 struct iphdr *ipv4;
1231         } hdr;
1232         __be16 protocol;
1233         u8 nexthdr = 0; /* default to not TCP */
1234         u8 hlen;
1235
1236         /* this should never happen, but better safe than sorry */
1237         if (max_len < ETH_HLEN)
1238                 return max_len;
1239
1240         /* initialize network frame pointer */
1241         hdr.network = data;
1242
1243         /* set first protocol and move network header forward */
1244         protocol = hdr.eth->h_proto;
1245         hdr.network += ETH_HLEN;
1246
1247         /* handle any vlan tag if present */
1248         if (protocol == __constant_htons(ETH_P_8021Q)) {
1249                 if ((hdr.network - data) > (max_len - VLAN_HLEN))
1250                         return max_len;
1251
1252                 protocol = hdr.vlan->h_vlan_encapsulated_proto;
1253                 hdr.network += VLAN_HLEN;
1254         }
1255
1256         /* handle L3 protocols */
1257         if (protocol == __constant_htons(ETH_P_IP)) {
1258                 if ((hdr.network - data) > (max_len - sizeof(struct iphdr)))
1259                         return max_len;
1260
1261                 /* access ihl as a u8 to avoid unaligned access on ia64 */
1262                 hlen = (hdr.network[0] & 0x0F) << 2;
1263
1264                 /* verify hlen meets minimum size requirements */
1265                 if (hlen < sizeof(struct iphdr))
1266                         return hdr.network - data;
1267
1268                 /* record next protocol */
1269                 nexthdr = hdr.ipv4->protocol;
1270                 hdr.network += hlen;
1271 #ifdef IXGBE_FCOE
1272         } else if (protocol == __constant_htons(ETH_P_FCOE)) {
1273                 if ((hdr.network - data) > (max_len - FCOE_HEADER_LEN))
1274                         return max_len;
1275                 hdr.network += FCOE_HEADER_LEN;
1276 #endif
1277         } else {
1278                 return hdr.network - data;
1279         }
1280
1281         /* finally sort out TCP */
1282         if (nexthdr == IPPROTO_TCP) {
1283                 if ((hdr.network - data) > (max_len - sizeof(struct tcphdr)))
1284                         return max_len;
1285
1286                 /* access doff as a u8 to avoid unaligned access on ia64 */
1287                 hlen = (hdr.network[12] & 0xF0) >> 2;
1288
1289                 /* verify hlen meets minimum size requirements */
1290                 if (hlen < sizeof(struct tcphdr))
1291                         return hdr.network - data;
1292
1293                 hdr.network += hlen;
1294         }
1295
1296         /*
1297          * If everything has gone correctly hdr.network should be the
1298          * data section of the packet and will be the end of the header.
1299          * If not then it probably represents the end of the last recognized
1300          * header.
1301          */
1302         if ((hdr.network - data) < max_len)
1303                 return hdr.network - data;
1304         else
1305                 return max_len;
1306 }
1307
1308 static void ixgbe_get_rsc_cnt(struct ixgbe_ring *rx_ring,
1309                               union ixgbe_adv_rx_desc *rx_desc,
1310                               struct sk_buff *skb)
1311 {
1312         __le32 rsc_enabled;
1313         u32 rsc_cnt;
1314
1315         if (!ring_is_rsc_enabled(rx_ring))
1316                 return;
1317
1318         rsc_enabled = rx_desc->wb.lower.lo_dword.data &
1319                       cpu_to_le32(IXGBE_RXDADV_RSCCNT_MASK);
1320
1321         /* If this is an RSC frame rsc_cnt should be non-zero */
1322         if (!rsc_enabled)
1323                 return;
1324
1325         rsc_cnt = le32_to_cpu(rsc_enabled);
1326         rsc_cnt >>= IXGBE_RXDADV_RSCCNT_SHIFT;
1327
1328         IXGBE_CB(skb)->append_cnt += rsc_cnt - 1;
1329 }
1330
1331 static void ixgbe_set_rsc_gso_size(struct ixgbe_ring *ring,
1332                                    struct sk_buff *skb)
1333 {
1334         u16 hdr_len = skb_headlen(skb);
1335
1336         /* set gso_size to avoid messing up TCP MSS */
1337         skb_shinfo(skb)->gso_size = DIV_ROUND_UP((skb->len - hdr_len),
1338                                                  IXGBE_CB(skb)->append_cnt);
1339 }
1340
1341 static void ixgbe_update_rsc_stats(struct ixgbe_ring *rx_ring,
1342                                    struct sk_buff *skb)
1343 {
1344         /* if append_cnt is 0 then frame is not RSC */
1345         if (!IXGBE_CB(skb)->append_cnt)
1346                 return;
1347
1348         rx_ring->rx_stats.rsc_count += IXGBE_CB(skb)->append_cnt;
1349         rx_ring->rx_stats.rsc_flush++;
1350
1351         ixgbe_set_rsc_gso_size(rx_ring, skb);
1352
1353         /* gso_size is computed using append_cnt so always clear it last */
1354         IXGBE_CB(skb)->append_cnt = 0;
1355 }
1356
1357 /**
1358  * ixgbe_process_skb_fields - Populate skb header fields from Rx descriptor
1359  * @rx_ring: rx descriptor ring packet is being transacted on
1360  * @rx_desc: pointer to the EOP Rx descriptor
1361  * @skb: pointer to current skb being populated
1362  *
1363  * This function checks the ring, descriptor, and packet information in
1364  * order to populate the hash, checksum, VLAN, timestamp, protocol, and
1365  * other fields within the skb.
1366  **/
1367 static void ixgbe_process_skb_fields(struct ixgbe_ring *rx_ring,
1368                                      union ixgbe_adv_rx_desc *rx_desc,
1369                                      struct sk_buff *skb)
1370 {
1371         ixgbe_update_rsc_stats(rx_ring, skb);
1372
1373         ixgbe_rx_hash(rx_ring, rx_desc, skb);
1374
1375         ixgbe_rx_checksum(rx_ring, rx_desc, skb);
1376
1377         if (ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_VP)) {
1378                 u16 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
1379                 __vlan_hwaccel_put_tag(skb, vid);
1380         }
1381
1382         skb_record_rx_queue(skb, rx_ring->queue_index);
1383
1384         skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1385 }
1386
1387 static void ixgbe_rx_skb(struct ixgbe_q_vector *q_vector,
1388                          struct sk_buff *skb)
1389 {
1390         struct ixgbe_adapter *adapter = q_vector->adapter;
1391
1392         if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
1393                 napi_gro_receive(&q_vector->napi, skb);
1394         else
1395                 netif_rx(skb);
1396 }
1397
1398 /**
1399  * ixgbe_is_non_eop - process handling of non-EOP buffers
1400  * @rx_ring: Rx ring being processed
1401  * @rx_desc: Rx descriptor for current buffer
1402  * @skb: Current socket buffer containing buffer in progress
1403  *
1404  * This function updates next to clean.  If the buffer is an EOP buffer
1405  * this function exits returning false, otherwise it will place the
1406  * sk_buff in the next buffer to be chained and return true indicating
1407  * that this is in fact a non-EOP buffer.
1408  **/
1409 static bool ixgbe_is_non_eop(struct ixgbe_ring *rx_ring,
1410                              union ixgbe_adv_rx_desc *rx_desc,
1411                              struct sk_buff *skb)
1412 {
1413         u32 ntc = rx_ring->next_to_clean + 1;
1414
1415         /* fetch, update, and store next to clean */
1416         ntc = (ntc < rx_ring->count) ? ntc : 0;
1417         rx_ring->next_to_clean = ntc;
1418
1419         prefetch(IXGBE_RX_DESC(rx_ring, ntc));
1420
1421         if (likely(ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
1422                 return false;
1423
1424         /* append_cnt indicates packet is RSC, if so fetch nextp */
1425         if (IXGBE_CB(skb)->append_cnt) {
1426                 ntc = le32_to_cpu(rx_desc->wb.upper.status_error);
1427                 ntc &= IXGBE_RXDADV_NEXTP_MASK;
1428                 ntc >>= IXGBE_RXDADV_NEXTP_SHIFT;
1429         }
1430
1431         /* place skb in next buffer to be received */
1432         rx_ring->rx_buffer_info[ntc].skb = skb;
1433         rx_ring->rx_stats.non_eop_descs++;
1434
1435         return true;
1436 }
1437
1438 /**
1439  * ixgbe_cleanup_headers - Correct corrupted or empty headers
1440  * @rx_ring: rx descriptor ring packet is being transacted on
1441  * @rx_desc: pointer to the EOP Rx descriptor
1442  * @skb: pointer to current skb being fixed
1443  *
1444  * Check for corrupted packet headers caused by senders on the local L2
1445  * embedded NIC switch not setting up their Tx Descriptors right.  These
1446  * should be very rare.
1447  *
1448  * Also address the case where we are pulling data in on pages only
1449  * and as such no data is present in the skb header.
1450  *
1451  * In addition if skb is not at least 60 bytes we need to pad it so that
1452  * it is large enough to qualify as a valid Ethernet frame.
1453  *
1454  * Returns true if an error was encountered and skb was freed.
1455  **/
1456 static bool ixgbe_cleanup_headers(struct ixgbe_ring *rx_ring,
1457                                   union ixgbe_adv_rx_desc *rx_desc,
1458                                   struct sk_buff *skb)
1459 {
1460         struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
1461         struct net_device *netdev = rx_ring->netdev;
1462         unsigned char *va;
1463         unsigned int pull_len;
1464
1465         /* if the page was released unmap it, else just sync our portion */
1466         if (unlikely(IXGBE_CB(skb)->page_released)) {
1467                 dma_unmap_page(rx_ring->dev, IXGBE_CB(skb)->dma,
1468                                ixgbe_rx_pg_size(rx_ring), DMA_FROM_DEVICE);
1469                 IXGBE_CB(skb)->page_released = false;
1470         } else {
1471                 dma_sync_single_range_for_cpu(rx_ring->dev,
1472                                               IXGBE_CB(skb)->dma,
1473                                               frag->page_offset,
1474                                               ixgbe_rx_bufsz(rx_ring),
1475                                               DMA_FROM_DEVICE);
1476         }
1477         IXGBE_CB(skb)->dma = 0;
1478
1479         /* verify that the packet does not have any known errors */
1480         if (unlikely(ixgbe_test_staterr(rx_desc,
1481                                         IXGBE_RXDADV_ERR_FRAME_ERR_MASK) &&
1482             !(netdev->features & NETIF_F_RXALL))) {
1483                 dev_kfree_skb_any(skb);
1484                 return true;
1485         }
1486
1487         /*
1488          * it is valid to use page_address instead of kmap since we are
1489          * working with pages allocated out of the lomem pool per
1490          * alloc_page(GFP_ATOMIC)
1491          */
1492         va = skb_frag_address(frag);
1493
1494         /*
1495          * we need the header to contain the greater of either ETH_HLEN or
1496          * 60 bytes if the skb->len is less than 60 for skb_pad.
1497          */
1498         pull_len = skb_frag_size(frag);
1499         if (pull_len > 256)
1500                 pull_len = ixgbe_get_headlen(va, pull_len);
1501
1502         /* align pull length to size of long to optimize memcpy performance */
1503         skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
1504
1505         /* update all of the pointers */
1506         skb_frag_size_sub(frag, pull_len);
1507         frag->page_offset += pull_len;
1508         skb->data_len -= pull_len;
1509         skb->tail += pull_len;
1510
1511         /*
1512          * if we sucked the frag empty then we should free it,
1513          * if there are other frags here something is screwed up in hardware
1514          */
1515         if (skb_frag_size(frag) == 0) {
1516                 BUG_ON(skb_shinfo(skb)->nr_frags != 1);
1517                 skb_shinfo(skb)->nr_frags = 0;
1518                 __skb_frag_unref(frag);
1519                 skb->truesize -= ixgbe_rx_bufsz(rx_ring);
1520         }
1521
1522         /* if skb_pad returns an error the skb was freed */
1523         if (unlikely(skb->len < 60)) {
1524                 int pad_len = 60 - skb->len;
1525
1526                 if (skb_pad(skb, pad_len))
1527                         return true;
1528                 __skb_put(skb, pad_len);
1529         }
1530
1531         return false;
1532 }
1533
1534 /**
1535  * ixgbe_can_reuse_page - determine if we can reuse a page
1536  * @rx_buffer: pointer to rx_buffer containing the page we want to reuse
1537  *
1538  * Returns true if page can be reused in another Rx buffer
1539  **/
1540 static inline bool ixgbe_can_reuse_page(struct ixgbe_rx_buffer *rx_buffer)
1541 {
1542         struct page *page = rx_buffer->page;
1543
1544         /* if we are only owner of page and it is local we can reuse it */
1545         return likely(page_count(page) == 1) &&
1546                likely(page_to_nid(page) == numa_node_id());
1547 }
1548
1549 /**
1550  * ixgbe_reuse_rx_page - page flip buffer and store it back on the ring
1551  * @rx_ring: rx descriptor ring to store buffers on
1552  * @old_buff: donor buffer to have page reused
1553  *
1554  * Syncronizes page for reuse by the adapter
1555  **/
1556 static void ixgbe_reuse_rx_page(struct ixgbe_ring *rx_ring,
1557                                 struct ixgbe_rx_buffer *old_buff)
1558 {
1559         struct ixgbe_rx_buffer *new_buff;
1560         u16 nta = rx_ring->next_to_alloc;
1561         u16 bufsz = ixgbe_rx_bufsz(rx_ring);
1562
1563         new_buff = &rx_ring->rx_buffer_info[nta];
1564
1565         /* update, and store next to alloc */
1566         nta++;
1567         rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1568
1569         /* transfer page from old buffer to new buffer */
1570         new_buff->page = old_buff->page;
1571         new_buff->dma = old_buff->dma;
1572
1573         /* flip page offset to other buffer and store to new_buff */
1574         new_buff->page_offset = old_buff->page_offset ^ bufsz;
1575
1576         /* sync the buffer for use by the device */
1577         dma_sync_single_range_for_device(rx_ring->dev, new_buff->dma,
1578                                          new_buff->page_offset, bufsz,
1579                                          DMA_FROM_DEVICE);
1580
1581         /* bump ref count on page before it is given to the stack */
1582         get_page(new_buff->page);
1583 }
1584
1585 /**
1586  * ixgbe_add_rx_frag - Add contents of Rx buffer to sk_buff
1587  * @rx_ring: rx descriptor ring to transact packets on
1588  * @rx_buffer: buffer containing page to add
1589  * @rx_desc: descriptor containing length of buffer written by hardware
1590  * @skb: sk_buff to place the data into
1591  *
1592  * This function is based on skb_add_rx_frag.  I would have used that
1593  * function however it doesn't handle the truesize case correctly since we
1594  * are allocating more memory than might be used for a single receive.
1595  **/
1596 static void ixgbe_add_rx_frag(struct ixgbe_ring *rx_ring,
1597                               struct ixgbe_rx_buffer *rx_buffer,
1598                               struct sk_buff *skb, int size)
1599 {
1600         skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
1601                            rx_buffer->page, rx_buffer->page_offset,
1602                            size);
1603         skb->len += size;
1604         skb->data_len += size;
1605         skb->truesize += ixgbe_rx_bufsz(rx_ring);
1606 }
1607
1608 /**
1609  * ixgbe_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
1610  * @q_vector: structure containing interrupt and ring information
1611  * @rx_ring: rx descriptor ring to transact packets on
1612  * @budget: Total limit on number of packets to process
1613  *
1614  * This function provides a "bounce buffer" approach to Rx interrupt
1615  * processing.  The advantage to this is that on systems that have
1616  * expensive overhead for IOMMU access this provides a means of avoiding
1617  * it by maintaining the mapping of the page to the syste.
1618  *
1619  * Returns true if all work is completed without reaching budget
1620  **/
1621 static bool ixgbe_clean_rx_irq(struct ixgbe_q_vector *q_vector,
1622                                struct ixgbe_ring *rx_ring,
1623                                int budget)
1624 {
1625         unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1626 #ifdef IXGBE_FCOE
1627         struct ixgbe_adapter *adapter = q_vector->adapter;
1628         int ddp_bytes = 0;
1629 #endif /* IXGBE_FCOE */
1630         u16 cleaned_count = ixgbe_desc_unused(rx_ring);
1631
1632         do {
1633                 struct ixgbe_rx_buffer *rx_buffer;
1634                 union ixgbe_adv_rx_desc *rx_desc;
1635                 struct sk_buff *skb;
1636                 struct page *page;
1637                 u16 ntc;
1638
1639                 /* return some buffers to hardware, one at a time is too slow */
1640                 if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
1641                         ixgbe_alloc_rx_buffers(rx_ring, cleaned_count);
1642                         cleaned_count = 0;
1643                 }
1644
1645                 ntc = rx_ring->next_to_clean;
1646                 rx_desc = IXGBE_RX_DESC(rx_ring, ntc);
1647                 rx_buffer = &rx_ring->rx_buffer_info[ntc];
1648
1649                 if (!ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_DD))
1650                         break;
1651
1652                 /*
1653                  * This memory barrier is needed to keep us from reading
1654                  * any other fields out of the rx_desc until we know the
1655                  * RXD_STAT_DD bit is set
1656                  */
1657                 rmb();
1658
1659                 page = rx_buffer->page;
1660                 prefetchw(page);
1661
1662                 skb = rx_buffer->skb;
1663
1664                 if (likely(!skb)) {
1665                         void *page_addr = page_address(page) +
1666                                           rx_buffer->page_offset;
1667
1668                         /* prefetch first cache line of first page */
1669                         prefetch(page_addr);
1670 #if L1_CACHE_BYTES < 128
1671                         prefetch(page_addr + L1_CACHE_BYTES);
1672 #endif
1673
1674                         /* allocate a skb to store the frags */
1675                         skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
1676                                                         IXGBE_RX_HDR_SIZE);
1677                         if (unlikely(!skb)) {
1678                                 rx_ring->rx_stats.alloc_rx_buff_failed++;
1679                                 break;
1680                         }
1681
1682                         /*
1683                          * we will be copying header into skb->data in
1684                          * pskb_may_pull so it is in our interest to prefetch
1685                          * it now to avoid a possible cache miss
1686                          */
1687                         prefetchw(skb->data);
1688
1689                         /*
1690                          * Delay unmapping of the first packet. It carries the
1691                          * header information, HW may still access the header
1692                          * after the writeback.  Only unmap it when EOP is
1693                          * reached
1694                          */
1695                         IXGBE_CB(skb)->dma = rx_buffer->dma;
1696                 } else {
1697                         /* we are reusing so sync this buffer for CPU use */
1698                         dma_sync_single_range_for_cpu(rx_ring->dev,
1699                                                       rx_buffer->dma,
1700                                                       rx_buffer->page_offset,
1701                                                       ixgbe_rx_bufsz(rx_ring),
1702                                                       DMA_FROM_DEVICE);
1703                 }
1704
1705                 /* pull page into skb */
1706                 ixgbe_add_rx_frag(rx_ring, rx_buffer, skb,
1707                                   le16_to_cpu(rx_desc->wb.upper.length));
1708
1709                 if (ixgbe_can_reuse_page(rx_buffer)) {
1710                         /* hand second half of page back to the ring */
1711                         ixgbe_reuse_rx_page(rx_ring, rx_buffer);
1712                 } else if (IXGBE_CB(skb)->dma == rx_buffer->dma) {
1713                         /* the page has been released from the ring */
1714                         IXGBE_CB(skb)->page_released = true;
1715                 } else {
1716                         /* we are not reusing the buffer so unmap it */
1717                         dma_unmap_page(rx_ring->dev, rx_buffer->dma,
1718                                        ixgbe_rx_pg_size(rx_ring),
1719                                        DMA_FROM_DEVICE);
1720                 }
1721
1722                 /* clear contents of buffer_info */
1723                 rx_buffer->skb = NULL;
1724                 rx_buffer->dma = 0;
1725                 rx_buffer->page = NULL;
1726
1727                 ixgbe_get_rsc_cnt(rx_ring, rx_desc, skb);
1728
1729                 cleaned_count++;
1730
1731                 /* place incomplete frames back on ring for completion */
1732                 if (ixgbe_is_non_eop(rx_ring, rx_desc, skb))
1733                         continue;
1734
1735                 /* verify the packet layout is correct */
1736                 if (ixgbe_cleanup_headers(rx_ring, rx_desc, skb))
1737                         continue;
1738
1739                 /* probably a little skewed due to removing CRC */
1740                 total_rx_bytes += skb->len;
1741                 total_rx_packets++;
1742
1743                 /* populate checksum, timestamp, VLAN, and protocol */
1744                 ixgbe_process_skb_fields(rx_ring, rx_desc, skb);
1745
1746 #ifdef IXGBE_FCOE
1747                 /* if ddp, not passing to ULD unless for FCP_RSP or error */
1748                 if (ixgbe_rx_is_fcoe(adapter, rx_desc)) {
1749                         ddp_bytes = ixgbe_fcoe_ddp(adapter, rx_desc, skb);
1750                         if (!ddp_bytes) {
1751                                 dev_kfree_skb_any(skb);
1752                                 continue;
1753                         }
1754                 }
1755
1756 #endif /* IXGBE_FCOE */
1757                 ixgbe_rx_skb(q_vector, skb);
1758
1759                 /* update budget accounting */
1760                 budget--;
1761         } while (likely(budget));
1762
1763 #ifdef IXGBE_FCOE
1764         /* include DDPed FCoE data */
1765         if (ddp_bytes > 0) {
1766                 unsigned int mss;
1767
1768                 mss = rx_ring->netdev->mtu - sizeof(struct fcoe_hdr) -
1769                         sizeof(struct fc_frame_header) -
1770                         sizeof(struct fcoe_crc_eof);
1771                 if (mss > 512)
1772                         mss &= ~511;
1773                 total_rx_bytes += ddp_bytes;
1774                 total_rx_packets += DIV_ROUND_UP(ddp_bytes, mss);
1775         }
1776
1777 #endif /* IXGBE_FCOE */
1778         u64_stats_update_begin(&rx_ring->syncp);
1779         rx_ring->stats.packets += total_rx_packets;
1780         rx_ring->stats.bytes += total_rx_bytes;
1781         u64_stats_update_end(&rx_ring->syncp);
1782         q_vector->rx.total_packets += total_rx_packets;
1783         q_vector->rx.total_bytes += total_rx_bytes;
1784
1785         if (cleaned_count)
1786                 ixgbe_alloc_rx_buffers(rx_ring, cleaned_count);
1787
1788         return !!budget;
1789 }
1790
1791 /**
1792  * ixgbe_configure_msix - Configure MSI-X hardware
1793  * @adapter: board private structure
1794  *
1795  * ixgbe_configure_msix sets up the hardware to properly generate MSI-X
1796  * interrupts.
1797  **/
1798 static void ixgbe_configure_msix(struct ixgbe_adapter *adapter)
1799 {
1800         struct ixgbe_q_vector *q_vector;
1801         int q_vectors, v_idx;
1802         u32 mask;
1803
1804         q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1805
1806         /* Populate MSIX to EITR Select */
1807         if (adapter->num_vfs > 32) {
1808                 u32 eitrsel = (1 << (adapter->num_vfs - 32)) - 1;
1809                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITRSEL, eitrsel);
1810         }
1811
1812         /*
1813          * Populate the IVAR table and set the ITR values to the
1814          * corresponding register.
1815          */
1816         for (v_idx = 0; v_idx < q_vectors; v_idx++) {
1817                 struct ixgbe_ring *ring;
1818                 q_vector = adapter->q_vector[v_idx];
1819
1820                 ixgbe_for_each_ring(ring, q_vector->rx)
1821                         ixgbe_set_ivar(adapter, 0, ring->reg_idx, v_idx);
1822
1823                 ixgbe_for_each_ring(ring, q_vector->tx)
1824                         ixgbe_set_ivar(adapter, 1, ring->reg_idx, v_idx);
1825
1826                 if (q_vector->tx.ring && !q_vector->rx.ring) {
1827                         /* tx only vector */
1828                         if (adapter->tx_itr_setting == 1)
1829                                 q_vector->itr = IXGBE_10K_ITR;
1830                         else
1831                                 q_vector->itr = adapter->tx_itr_setting;
1832                 } else {
1833                         /* rx or rx/tx vector */
1834                         if (adapter->rx_itr_setting == 1)
1835                                 q_vector->itr = IXGBE_20K_ITR;
1836                         else
1837                                 q_vector->itr = adapter->rx_itr_setting;
1838                 }
1839
1840                 ixgbe_write_eitr(q_vector);
1841         }
1842
1843         switch (adapter->hw.mac.type) {
1844         case ixgbe_mac_82598EB:
1845                 ixgbe_set_ivar(adapter, -1, IXGBE_IVAR_OTHER_CAUSES_INDEX,
1846                                v_idx);
1847                 break;
1848         case ixgbe_mac_82599EB:
1849         case ixgbe_mac_X540:
1850                 ixgbe_set_ivar(adapter, -1, 1, v_idx);
1851                 break;
1852         default:
1853                 break;
1854         }
1855         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx), 1950);
1856
1857         /* set up to autoclear timer, and the vectors */
1858         mask = IXGBE_EIMS_ENABLE_MASK;
1859         mask &= ~(IXGBE_EIMS_OTHER |
1860                   IXGBE_EIMS_MAILBOX |
1861                   IXGBE_EIMS_LSC);
1862
1863         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, mask);
1864 }
1865
1866 enum latency_range {
1867         lowest_latency = 0,
1868         low_latency = 1,
1869         bulk_latency = 2,
1870         latency_invalid = 255
1871 };
1872
1873 /**
1874  * ixgbe_update_itr - update the dynamic ITR value based on statistics
1875  * @q_vector: structure containing interrupt and ring information
1876  * @ring_container: structure containing ring performance data
1877  *
1878  *      Stores a new ITR value based on packets and byte
1879  *      counts during the last interrupt.  The advantage of per interrupt
1880  *      computation is faster updates and more accurate ITR for the current
1881  *      traffic pattern.  Constants in this function were computed
1882  *      based on theoretical maximum wire speed and thresholds were set based
1883  *      on testing data as well as attempting to minimize response time
1884  *      while increasing bulk throughput.
1885  *      this functionality is controlled by the InterruptThrottleRate module
1886  *      parameter (see ixgbe_param.c)
1887  **/
1888 static void ixgbe_update_itr(struct ixgbe_q_vector *q_vector,
1889                              struct ixgbe_ring_container *ring_container)
1890 {
1891         int bytes = ring_container->total_bytes;
1892         int packets = ring_container->total_packets;
1893         u32 timepassed_us;
1894         u64 bytes_perint;
1895         u8 itr_setting = ring_container->itr;
1896
1897         if (packets == 0)
1898                 return;
1899
1900         /* simple throttlerate management
1901          *   0-10MB/s   lowest (100000 ints/s)
1902          *  10-20MB/s   low    (20000 ints/s)
1903          *  20-1249MB/s bulk   (8000 ints/s)
1904          */
1905         /* what was last interrupt timeslice? */
1906         timepassed_us = q_vector->itr >> 2;
1907         bytes_perint = bytes / timepassed_us; /* bytes/usec */
1908
1909         switch (itr_setting) {
1910         case lowest_latency:
1911                 if (bytes_perint > 10)
1912                         itr_setting = low_latency;
1913                 break;
1914         case low_latency:
1915                 if (bytes_perint > 20)
1916                         itr_setting = bulk_latency;
1917                 else if (bytes_perint <= 10)
1918                         itr_setting = lowest_latency;
1919                 break;
1920         case bulk_latency:
1921                 if (bytes_perint <= 20)
1922                         itr_setting = low_latency;
1923                 break;
1924         }
1925
1926         /* clear work counters since we have the values we need */
1927         ring_container->total_bytes = 0;
1928         ring_container->total_packets = 0;
1929
1930         /* write updated itr to ring container */
1931         ring_container->itr = itr_setting;
1932 }
1933
1934 /**
1935  * ixgbe_write_eitr - write EITR register in hardware specific way
1936  * @q_vector: structure containing interrupt and ring information
1937  *
1938  * This function is made to be called by ethtool and by the driver
1939  * when it needs to update EITR registers at runtime.  Hardware
1940  * specific quirks/differences are taken care of here.
1941  */
1942 void ixgbe_write_eitr(struct ixgbe_q_vector *q_vector)
1943 {
1944         struct ixgbe_adapter *adapter = q_vector->adapter;
1945         struct ixgbe_hw *hw = &adapter->hw;
1946         int v_idx = q_vector->v_idx;
1947         u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
1948
1949         switch (adapter->hw.mac.type) {
1950         case ixgbe_mac_82598EB:
1951                 /* must write high and low 16 bits to reset counter */
1952                 itr_reg |= (itr_reg << 16);
1953                 break;
1954         case ixgbe_mac_82599EB:
1955         case ixgbe_mac_X540:
1956                 /*
1957                  * set the WDIS bit to not clear the timer bits and cause an
1958                  * immediate assertion of the interrupt
1959                  */
1960                 itr_reg |= IXGBE_EITR_CNT_WDIS;
1961                 break;
1962         default:
1963                 break;
1964         }
1965         IXGBE_WRITE_REG(hw, IXGBE_EITR(v_idx), itr_reg);
1966 }
1967
1968 static void ixgbe_set_itr(struct ixgbe_q_vector *q_vector)
1969 {
1970         u32 new_itr = q_vector->itr;
1971         u8 current_itr;
1972
1973         ixgbe_update_itr(q_vector, &q_vector->tx);
1974         ixgbe_update_itr(q_vector, &q_vector->rx);
1975
1976         current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
1977
1978         switch (current_itr) {
1979         /* counts and packets in update_itr are dependent on these numbers */
1980         case lowest_latency:
1981                 new_itr = IXGBE_100K_ITR;
1982                 break;
1983         case low_latency:
1984                 new_itr = IXGBE_20K_ITR;
1985                 break;
1986         case bulk_latency:
1987                 new_itr = IXGBE_8K_ITR;
1988                 break;
1989         default:
1990                 break;
1991         }
1992
1993         if (new_itr != q_vector->itr) {
1994                 /* do an exponential smoothing */
1995                 new_itr = (10 * new_itr * q_vector->itr) /
1996                           ((9 * new_itr) + q_vector->itr);
1997
1998                 /* save the algorithm value here */
1999                 q_vector->itr = new_itr;
2000
2001                 ixgbe_write_eitr(q_vector);
2002         }
2003 }
2004
2005 /**
2006  * ixgbe_check_overtemp_subtask - check for over temperature
2007  * @adapter: pointer to adapter
2008  **/
2009 static void ixgbe_check_overtemp_subtask(struct ixgbe_adapter *adapter)
2010 {
2011         struct ixgbe_hw *hw = &adapter->hw;
2012         u32 eicr = adapter->interrupt_event;
2013
2014         if (test_bit(__IXGBE_DOWN, &adapter->state))
2015                 return;
2016
2017         if (!(adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE) &&
2018             !(adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_EVENT))
2019                 return;
2020
2021         adapter->flags2 &= ~IXGBE_FLAG2_TEMP_SENSOR_EVENT;
2022
2023         switch (hw->device_id) {
2024         case IXGBE_DEV_ID_82599_T3_LOM:
2025                 /*
2026                  * Since the warning interrupt is for both ports
2027                  * we don't have to check if:
2028                  *  - This interrupt wasn't for our port.
2029                  *  - We may have missed the interrupt so always have to
2030                  *    check if we  got a LSC
2031                  */
2032                 if (!(eicr & IXGBE_EICR_GPI_SDP0) &&
2033                     !(eicr & IXGBE_EICR_LSC))
2034                         return;
2035
2036                 if (!(eicr & IXGBE_EICR_LSC) && hw->mac.ops.check_link) {
2037                         u32 autoneg;
2038                         bool link_up = false;
2039
2040                         hw->mac.ops.check_link(hw, &autoneg, &link_up, false);
2041
2042                         if (link_up)
2043                                 return;
2044                 }
2045
2046                 /* Check if this is not due to overtemp */
2047                 if (hw->phy.ops.check_overtemp(hw) != IXGBE_ERR_OVERTEMP)
2048                         return;
2049
2050                 break;
2051         default:
2052                 if (!(eicr & IXGBE_EICR_GPI_SDP0))
2053                         return;
2054                 break;
2055         }
2056         e_crit(drv,
2057                "Network adapter has been stopped because it has over heated. "
2058                "Restart the computer. If the problem persists, "
2059                "power off the system and replace the adapter\n");
2060
2061         adapter->interrupt_event = 0;
2062 }
2063
2064 static void ixgbe_check_fan_failure(struct ixgbe_adapter *adapter, u32 eicr)
2065 {
2066         struct ixgbe_hw *hw = &adapter->hw;
2067
2068         if ((adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) &&
2069             (eicr & IXGBE_EICR_GPI_SDP1)) {
2070                 e_crit(probe, "Fan has stopped, replace the adapter\n");
2071                 /* write to clear the interrupt */
2072                 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
2073         }
2074 }
2075
2076 static void ixgbe_check_overtemp_event(struct ixgbe_adapter *adapter, u32 eicr)
2077 {
2078         if (!(adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE))
2079                 return;
2080
2081         switch (adapter->hw.mac.type) {
2082         case ixgbe_mac_82599EB:
2083                 /*
2084                  * Need to check link state so complete overtemp check
2085                  * on service task
2086                  */
2087                 if (((eicr & IXGBE_EICR_GPI_SDP0) || (eicr & IXGBE_EICR_LSC)) &&
2088                     (!test_bit(__IXGBE_DOWN, &adapter->state))) {
2089                         adapter->interrupt_event = eicr;
2090                         adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_EVENT;
2091                         ixgbe_service_event_schedule(adapter);
2092                         return;
2093                 }
2094                 return;
2095         case ixgbe_mac_X540:
2096                 if (!(eicr & IXGBE_EICR_TS))
2097                         return;
2098                 break;
2099         default:
2100                 return;
2101         }
2102
2103         e_crit(drv,
2104                "Network adapter has been stopped because it has over heated. "
2105                "Restart the computer. If the problem persists, "
2106                "power off the system and replace the adapter\n");
2107 }
2108
2109 static void ixgbe_check_sfp_event(struct ixgbe_adapter *adapter, u32 eicr)
2110 {
2111         struct ixgbe_hw *hw = &adapter->hw;
2112
2113         if (eicr & IXGBE_EICR_GPI_SDP2) {
2114                 /* Clear the interrupt */
2115                 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP2);
2116                 if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
2117                         adapter->flags2 |= IXGBE_FLAG2_SFP_NEEDS_RESET;
2118                         ixgbe_service_event_schedule(adapter);
2119                 }
2120         }
2121
2122         if (eicr & IXGBE_EICR_GPI_SDP1) {
2123                 /* Clear the interrupt */
2124                 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
2125                 if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
2126                         adapter->flags |= IXGBE_FLAG_NEED_LINK_CONFIG;
2127                         ixgbe_service_event_schedule(adapter);
2128                 }
2129         }
2130 }
2131
2132 static void ixgbe_check_lsc(struct ixgbe_adapter *adapter)
2133 {
2134         struct ixgbe_hw *hw = &adapter->hw;
2135
2136         adapter->lsc_int++;
2137         adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
2138         adapter->link_check_timeout = jiffies;
2139         if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
2140                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
2141                 IXGBE_WRITE_FLUSH(hw);
2142                 ixgbe_service_event_schedule(adapter);
2143         }
2144 }
2145
2146 static inline void ixgbe_irq_enable_queues(struct ixgbe_adapter *adapter,
2147                                            u64 qmask)
2148 {
2149         u32 mask;
2150         struct ixgbe_hw *hw = &adapter->hw;
2151
2152         switch (hw->mac.type) {
2153         case ixgbe_mac_82598EB:
2154                 mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
2155                 IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
2156                 break;
2157         case ixgbe_mac_82599EB:
2158         case ixgbe_mac_X540:
2159                 mask = (qmask & 0xFFFFFFFF);
2160                 if (mask)
2161                         IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
2162                 mask = (qmask >> 32);
2163                 if (mask)
2164                         IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
2165                 break;
2166         default:
2167                 break;
2168         }
2169         /* skip the flush */
2170 }
2171
2172 static inline void ixgbe_irq_disable_queues(struct ixgbe_adapter *adapter,
2173                                             u64 qmask)
2174 {
2175         u32 mask;
2176         struct ixgbe_hw *hw = &adapter->hw;
2177
2178         switch (hw->mac.type) {
2179         case ixgbe_mac_82598EB:
2180                 mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
2181                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
2182                 break;
2183         case ixgbe_mac_82599EB:
2184         case ixgbe_mac_X540:
2185                 mask = (qmask & 0xFFFFFFFF);
2186                 if (mask)
2187                         IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
2188                 mask = (qmask >> 32);
2189                 if (mask)
2190                         IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
2191                 break;
2192         default:
2193                 break;
2194         }
2195         /* skip the flush */
2196 }
2197
2198 /**
2199  * ixgbe_irq_enable - Enable default interrupt generation settings
2200  * @adapter: board private structure
2201  **/
2202 static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter, bool queues,
2203                                     bool flush)
2204 {
2205         u32 mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
2206
2207         /* don't reenable LSC while waiting for link */
2208         if (adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE)
2209                 mask &= ~IXGBE_EIMS_LSC;
2210
2211         if (adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE)
2212                 switch (adapter->hw.mac.type) {
2213                 case ixgbe_mac_82599EB:
2214                         mask |= IXGBE_EIMS_GPI_SDP0;
2215                         break;
2216                 case ixgbe_mac_X540:
2217                         mask |= IXGBE_EIMS_TS;
2218                         break;
2219                 default:
2220                         break;
2221                 }
2222         if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE)
2223                 mask |= IXGBE_EIMS_GPI_SDP1;
2224         switch (adapter->hw.mac.type) {
2225         case ixgbe_mac_82599EB:
2226                 mask |= IXGBE_EIMS_GPI_SDP1;
2227                 mask |= IXGBE_EIMS_GPI_SDP2;
2228         case ixgbe_mac_X540:
2229                 mask |= IXGBE_EIMS_ECC;
2230                 mask |= IXGBE_EIMS_MAILBOX;
2231                 break;
2232         default:
2233                 break;
2234         }
2235         if ((adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) &&
2236             !(adapter->flags2 & IXGBE_FLAG2_FDIR_REQUIRES_REINIT))
2237                 mask |= IXGBE_EIMS_FLOW_DIR;
2238
2239         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, mask);
2240         if (queues)
2241                 ixgbe_irq_enable_queues(adapter, ~0);
2242         if (flush)
2243                 IXGBE_WRITE_FLUSH(&adapter->hw);
2244 }
2245
2246 static irqreturn_t ixgbe_msix_other(int irq, void *data)
2247 {
2248         struct ixgbe_adapter *adapter = data;
2249         struct ixgbe_hw *hw = &adapter->hw;
2250         u32 eicr;
2251
2252         /*
2253          * Workaround for Silicon errata.  Use clear-by-write instead
2254          * of clear-by-read.  Reading with EICS will return the
2255          * interrupt causes without clearing, which later be done
2256          * with the write to EICR.
2257          */
2258         eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
2259         IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
2260
2261         if (eicr & IXGBE_EICR_LSC)
2262                 ixgbe_check_lsc(adapter);
2263
2264         if (eicr & IXGBE_EICR_MAILBOX)
2265                 ixgbe_msg_task(adapter);
2266
2267         switch (hw->mac.type) {
2268         case ixgbe_mac_82599EB:
2269         case ixgbe_mac_X540:
2270                 if (eicr & IXGBE_EICR_ECC)
2271                         e_info(link, "Received unrecoverable ECC Err, please "
2272                                "reboot\n");
2273                 /* Handle Flow Director Full threshold interrupt */
2274                 if (eicr & IXGBE_EICR_FLOW_DIR) {
2275                         int reinit_count = 0;
2276                         int i;
2277                         for (i = 0; i < adapter->num_tx_queues; i++) {
2278                                 struct ixgbe_ring *ring = adapter->tx_ring[i];
2279                                 if (test_and_clear_bit(__IXGBE_TX_FDIR_INIT_DONE,
2280                                                        &ring->state))
2281                                         reinit_count++;
2282                         }
2283                         if (reinit_count) {
2284                                 /* no more flow director interrupts until after init */
2285                                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_FLOW_DIR);
2286                                 adapter->flags2 |= IXGBE_FLAG2_FDIR_REQUIRES_REINIT;
2287                                 ixgbe_service_event_schedule(adapter);
2288                         }
2289                 }
2290                 ixgbe_check_sfp_event(adapter, eicr);
2291                 ixgbe_check_overtemp_event(adapter, eicr);
2292                 break;
2293         default:
2294                 break;
2295         }
2296
2297         ixgbe_check_fan_failure(adapter, eicr);
2298
2299         /* re-enable the original interrupt state, no lsc, no queues */
2300         if (!test_bit(__IXGBE_DOWN, &adapter->state))
2301                 ixgbe_irq_enable(adapter, false, false);
2302
2303         return IRQ_HANDLED;
2304 }
2305
2306 static irqreturn_t ixgbe_msix_clean_rings(int irq, void *data)
2307 {
2308         struct ixgbe_q_vector *q_vector = data;
2309
2310         /* EIAM disabled interrupts (on this vector) for us */
2311
2312         if (q_vector->rx.ring || q_vector->tx.ring)
2313                 napi_schedule(&q_vector->napi);
2314
2315         return IRQ_HANDLED;
2316 }
2317
2318 /**
2319  * ixgbe_poll - NAPI Rx polling callback
2320  * @napi: structure for representing this polling device
2321  * @budget: how many packets driver is allowed to clean
2322  *
2323  * This function is used for legacy and MSI, NAPI mode
2324  **/
2325 int ixgbe_poll(struct napi_struct *napi, int budget)
2326 {
2327         struct ixgbe_q_vector *q_vector =
2328                                 container_of(napi, struct ixgbe_q_vector, napi);
2329         struct ixgbe_adapter *adapter = q_vector->adapter;
2330         struct ixgbe_ring *ring;
2331         int per_ring_budget;
2332         bool clean_complete = true;
2333
2334 #ifdef CONFIG_IXGBE_DCA
2335         if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
2336                 ixgbe_update_dca(q_vector);
2337 #endif
2338
2339         ixgbe_for_each_ring(ring, q_vector->tx)
2340                 clean_complete &= !!ixgbe_clean_tx_irq(q_vector, ring);
2341
2342         /* attempt to distribute budget to each queue fairly, but don't allow
2343          * the budget to go below 1 because we'll exit polling */
2344         if (q_vector->rx.count > 1)
2345                 per_ring_budget = max(budget/q_vector->rx.count, 1);
2346         else
2347                 per_ring_budget = budget;
2348
2349         ixgbe_for_each_ring(ring, q_vector->rx)
2350                 clean_complete &= ixgbe_clean_rx_irq(q_vector, ring,
2351                                                      per_ring_budget);
2352
2353         /* If all work not completed, return budget and keep polling */
2354         if (!clean_complete)
2355                 return budget;
2356
2357         /* all work done, exit the polling mode */
2358         napi_complete(napi);
2359         if (adapter->rx_itr_setting & 1)
2360                 ixgbe_set_itr(q_vector);
2361         if (!test_bit(__IXGBE_DOWN, &adapter->state))
2362                 ixgbe_irq_enable_queues(adapter, ((u64)1 << q_vector->v_idx));
2363
2364         return 0;
2365 }
2366
2367 /**
2368  * ixgbe_request_msix_irqs - Initialize MSI-X interrupts
2369  * @adapter: board private structure
2370  *
2371  * ixgbe_request_msix_irqs allocates MSI-X vectors and requests
2372  * interrupts from the kernel.
2373  **/
2374 static int ixgbe_request_msix_irqs(struct ixgbe_adapter *adapter)
2375 {
2376         struct net_device *netdev = adapter->netdev;
2377         int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2378         int vector, err;
2379         int ri = 0, ti = 0;
2380
2381         for (vector = 0; vector < q_vectors; vector++) {
2382                 struct ixgbe_q_vector *q_vector = adapter->q_vector[vector];
2383                 struct msix_entry *entry = &adapter->msix_entries[vector];
2384
2385                 if (q_vector->tx.ring && q_vector->rx.ring) {
2386                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2387                                  "%s-%s-%d", netdev->name, "TxRx", ri++);
2388                         ti++;
2389                 } else if (q_vector->rx.ring) {
2390                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2391                                  "%s-%s-%d", netdev->name, "rx", ri++);
2392                 } else if (q_vector->tx.ring) {
2393                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2394                                  "%s-%s-%d", netdev->name, "tx", ti++);
2395                 } else {
2396                         /* skip this unused q_vector */
2397                         continue;
2398                 }
2399                 err = request_irq(entry->vector, &ixgbe_msix_clean_rings, 0,
2400                                   q_vector->name, q_vector);
2401                 if (err) {
2402                         e_err(probe, "request_irq failed for MSIX interrupt "
2403                               "Error: %d\n", err);
2404                         goto free_queue_irqs;
2405                 }
2406                 /* If Flow Director is enabled, set interrupt affinity */
2407                 if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
2408                         /* assign the mask for this irq */
2409                         irq_set_affinity_hint(entry->vector,
2410                                               &q_vector->affinity_mask);
2411                 }
2412         }
2413
2414         err = request_irq(adapter->msix_entries[vector].vector,
2415                           ixgbe_msix_other, 0, netdev->name, adapter);
2416         if (err) {
2417                 e_err(probe, "request_irq for msix_other failed: %d\n", err);
2418                 goto free_queue_irqs;
2419         }
2420
2421         return 0;
2422
2423 free_queue_irqs:
2424         while (vector) {
2425                 vector--;
2426                 irq_set_affinity_hint(adapter->msix_entries[vector].vector,
2427                                       NULL);
2428                 free_irq(adapter->msix_entries[vector].vector,
2429                          adapter->q_vector[vector]);
2430         }
2431         adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2432         pci_disable_msix(adapter->pdev);
2433         kfree(adapter->msix_entries);
2434         adapter->msix_entries = NULL;
2435         return err;
2436 }
2437
2438 /**
2439  * ixgbe_intr - legacy mode Interrupt Handler
2440  * @irq: interrupt number
2441  * @data: pointer to a network interface device structure
2442  **/
2443 static irqreturn_t ixgbe_intr(int irq, void *data)
2444 {
2445         struct ixgbe_adapter *adapter = data;
2446         struct ixgbe_hw *hw = &adapter->hw;
2447         struct ixgbe_q_vector *q_vector = adapter->q_vector[0];
2448         u32 eicr;
2449
2450         /*
2451          * Workaround for silicon errata #26 on 82598.  Mask the interrupt
2452          * before the read of EICR.
2453          */
2454         IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
2455
2456         /* for NAPI, using EIAM to auto-mask tx/rx interrupt bits on read
2457          * therefore no explicit interrupt disable is necessary */
2458         eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
2459         if (!eicr) {
2460                 /*
2461                  * shared interrupt alert!
2462                  * make sure interrupts are enabled because the read will
2463                  * have disabled interrupts due to EIAM
2464                  * finish the workaround of silicon errata on 82598.  Unmask
2465                  * the interrupt that we masked before the EICR read.
2466                  */
2467                 if (!test_bit(__IXGBE_DOWN, &adapter->state))
2468                         ixgbe_irq_enable(adapter, true, true);
2469                 return IRQ_NONE;        /* Not our interrupt */
2470         }
2471
2472         if (eicr & IXGBE_EICR_LSC)
2473                 ixgbe_check_lsc(adapter);
2474
2475         switch (hw->mac.type) {
2476         case ixgbe_mac_82599EB:
2477                 ixgbe_check_sfp_event(adapter, eicr);
2478                 /* Fall through */
2479         case ixgbe_mac_X540:
2480                 if (eicr & IXGBE_EICR_ECC)
2481                         e_info(link, "Received unrecoverable ECC err, please "
2482                                      "reboot\n");
2483                 ixgbe_check_overtemp_event(adapter, eicr);
2484                 break;
2485         default:
2486                 break;
2487         }
2488
2489         ixgbe_check_fan_failure(adapter, eicr);
2490
2491         /* would disable interrupts here but EIAM disabled it */
2492         napi_schedule(&q_vector->napi);
2493
2494         /*
2495          * re-enable link(maybe) and non-queue interrupts, no flush.
2496          * ixgbe_poll will re-enable the queue interrupts
2497          */
2498         if (!test_bit(__IXGBE_DOWN, &adapter->state))
2499                 ixgbe_irq_enable(adapter, false, false);
2500
2501         return IRQ_HANDLED;
2502 }
2503
2504 /**
2505  * ixgbe_request_irq - initialize interrupts
2506  * @adapter: board private structure
2507  *
2508  * Attempts to configure interrupts using the best available
2509  * capabilities of the hardware and kernel.
2510  **/
2511 static int ixgbe_request_irq(struct ixgbe_adapter *adapter)
2512 {
2513         struct net_device *netdev = adapter->netdev;
2514         int err;
2515
2516         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
2517                 err = ixgbe_request_msix_irqs(adapter);
2518         else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED)
2519                 err = request_irq(adapter->pdev->irq, ixgbe_intr, 0,
2520                                   netdev->name, adapter);
2521         else
2522                 err = request_irq(adapter->pdev->irq, ixgbe_intr, IRQF_SHARED,
2523                                   netdev->name, adapter);
2524
2525         if (err)
2526                 e_err(probe, "request_irq failed, Error %d\n", err);
2527
2528         return err;
2529 }
2530
2531 static void ixgbe_free_irq(struct ixgbe_adapter *adapter)
2532 {
2533         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2534                 int i, q_vectors;
2535
2536                 q_vectors = adapter->num_msix_vectors;
2537                 i = q_vectors - 1;
2538                 free_irq(adapter->msix_entries[i].vector, adapter);
2539                 i--;
2540
2541                 for (; i >= 0; i--) {
2542                         /* free only the irqs that were actually requested */
2543                         if (!adapter->q_vector[i]->rx.ring &&
2544                             !adapter->q_vector[i]->tx.ring)
2545                                 continue;
2546
2547                         /* clear the affinity_mask in the IRQ descriptor */
2548                         irq_set_affinity_hint(adapter->msix_entries[i].vector,
2549                                               NULL);
2550
2551                         free_irq(adapter->msix_entries[i].vector,
2552                                  adapter->q_vector[i]);
2553                 }
2554         } else {
2555                 free_irq(adapter->pdev->irq, adapter);
2556         }
2557 }
2558
2559 /**
2560  * ixgbe_irq_disable - Mask off interrupt generation on the NIC
2561  * @adapter: board private structure
2562  **/
2563 static inline void ixgbe_irq_disable(struct ixgbe_adapter *adapter)
2564 {
2565         switch (adapter->hw.mac.type) {
2566         case ixgbe_mac_82598EB:
2567                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
2568                 break;
2569         case ixgbe_mac_82599EB:
2570         case ixgbe_mac_X540:
2571                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, 0xFFFF0000);
2572                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(0), ~0);
2573                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(1), ~0);
2574                 break;
2575         default:
2576                 break;
2577         }
2578         IXGBE_WRITE_FLUSH(&adapter->hw);
2579         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2580                 int i;
2581                 for (i = 0; i < adapter->num_msix_vectors; i++)
2582                         synchronize_irq(adapter->msix_entries[i].vector);
2583         } else {
2584                 synchronize_irq(adapter->pdev->irq);
2585         }
2586 }
2587
2588 /**
2589  * ixgbe_configure_msi_and_legacy - Initialize PIN (INTA...) and MSI interrupts
2590  *
2591  **/
2592 static void ixgbe_configure_msi_and_legacy(struct ixgbe_adapter *adapter)
2593 {
2594         struct ixgbe_q_vector *q_vector = adapter->q_vector[0];
2595
2596         /* rx/tx vector */
2597         if (adapter->rx_itr_setting == 1)
2598                 q_vector->itr = IXGBE_20K_ITR;
2599         else
2600                 q_vector->itr = adapter->rx_itr_setting;
2601
2602         ixgbe_write_eitr(q_vector);
2603
2604         ixgbe_set_ivar(adapter, 0, 0, 0);
2605         ixgbe_set_ivar(adapter, 1, 0, 0);
2606
2607         e_info(hw, "Legacy interrupt IVAR setup done\n");
2608 }
2609
2610 /**
2611  * ixgbe_configure_tx_ring - Configure 8259x Tx ring after Reset
2612  * @adapter: board private structure
2613  * @ring: structure containing ring specific data
2614  *
2615  * Configure the Tx descriptor ring after a reset.
2616  **/
2617 void ixgbe_configure_tx_ring(struct ixgbe_adapter *adapter,
2618                              struct ixgbe_ring *ring)
2619 {
2620         struct ixgbe_hw *hw = &adapter->hw;
2621         u64 tdba = ring->dma;
2622         int wait_loop = 10;
2623         u32 txdctl = IXGBE_TXDCTL_ENABLE;
2624         u8 reg_idx = ring->reg_idx;
2625
2626         /* disable queue to avoid issues while updating state */
2627         IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), 0);
2628         IXGBE_WRITE_FLUSH(hw);
2629
2630         IXGBE_WRITE_REG(hw, IXGBE_TDBAL(reg_idx),
2631                         (tdba & DMA_BIT_MASK(32)));
2632         IXGBE_WRITE_REG(hw, IXGBE_TDBAH(reg_idx), (tdba >> 32));
2633         IXGBE_WRITE_REG(hw, IXGBE_TDLEN(reg_idx),
2634                         ring->count * sizeof(union ixgbe_adv_tx_desc));
2635         IXGBE_WRITE_REG(hw, IXGBE_TDH(reg_idx), 0);
2636         IXGBE_WRITE_REG(hw, IXGBE_TDT(reg_idx), 0);
2637         ring->tail = hw->hw_addr + IXGBE_TDT(reg_idx);
2638
2639         /*
2640          * set WTHRESH to encourage burst writeback, it should not be set
2641          * higher than 1 when ITR is 0 as it could cause false TX hangs
2642          *
2643          * In order to avoid issues WTHRESH + PTHRESH should always be equal
2644          * to or less than the number of on chip descriptors, which is
2645          * currently 40.
2646          */
2647         if (!ring->q_vector || (ring->q_vector->itr < 8))
2648                 txdctl |= (1 << 16);    /* WTHRESH = 1 */
2649         else
2650                 txdctl |= (8 << 16);    /* WTHRESH = 8 */
2651
2652         /*
2653          * Setting PTHRESH to 32 both improves performance
2654          * and avoids a TX hang with DFP enabled
2655          */
2656         txdctl |= (1 << 8) |    /* HTHRESH = 1 */
2657                    32;          /* PTHRESH = 32 */
2658
2659         /* reinitialize flowdirector state */
2660         if ((adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) &&
2661             adapter->atr_sample_rate) {
2662                 ring->atr_sample_rate = adapter->atr_sample_rate;
2663                 ring->atr_count = 0;
2664                 set_bit(__IXGBE_TX_FDIR_INIT_DONE, &ring->state);
2665         } else {
2666                 ring->atr_sample_rate = 0;
2667         }
2668
2669         clear_bit(__IXGBE_HANG_CHECK_ARMED, &ring->state);
2670
2671         /* enable queue */
2672         IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), txdctl);
2673
2674         netdev_tx_reset_queue(txring_txq(ring));
2675
2676         /* TXDCTL.EN will return 0 on 82598 if link is down, so skip it */
2677         if (hw->mac.type == ixgbe_mac_82598EB &&
2678             !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
2679                 return;
2680
2681         /* poll to verify queue is enabled */
2682         do {
2683                 usleep_range(1000, 2000);
2684                 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(reg_idx));
2685         } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
2686         if (!wait_loop)
2687                 e_err(drv, "Could not enable Tx Queue %d\n", reg_idx);
2688 }
2689
2690 static void ixgbe_setup_mtqc(struct ixgbe_adapter *adapter)
2691 {
2692         struct ixgbe_hw *hw = &adapter->hw;
2693         u32 rttdcs;
2694         u32 reg;
2695         u8 tcs = netdev_get_num_tc(adapter->netdev);
2696
2697         if (hw->mac.type == ixgbe_mac_82598EB)
2698                 return;
2699
2700         /* disable the arbiter while setting MTQC */
2701         rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
2702         rttdcs |= IXGBE_RTTDCS_ARBDIS;
2703         IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
2704
2705         /* set transmit pool layout */
2706         switch (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
2707         case (IXGBE_FLAG_SRIOV_ENABLED):
2708                 IXGBE_WRITE_REG(hw, IXGBE_MTQC,
2709                                 (IXGBE_MTQC_VT_ENA | IXGBE_MTQC_64VF));
2710                 break;
2711         default:
2712                 if (!tcs)
2713                         reg = IXGBE_MTQC_64Q_1PB;
2714                 else if (tcs <= 4)
2715                         reg = IXGBE_MTQC_RT_ENA | IXGBE_MTQC_4TC_4TQ;
2716                 else
2717                         reg = IXGBE_MTQC_RT_ENA | IXGBE_MTQC_8TC_8TQ;
2718
2719                 IXGBE_WRITE_REG(hw, IXGBE_MTQC, reg);
2720
2721                 /* Enable Security TX Buffer IFG for multiple pb */
2722                 if (tcs) {
2723                         reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
2724                         reg |= IXGBE_SECTX_DCB;
2725                         IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
2726                 }
2727                 break;
2728         }
2729
2730         /* re-enable the arbiter */
2731         rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
2732         IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
2733 }
2734
2735 /**
2736  * ixgbe_configure_tx - Configure 8259x Transmit Unit after Reset
2737  * @adapter: board private structure
2738  *
2739  * Configure the Tx unit of the MAC after a reset.
2740  **/
2741 static void ixgbe_configure_tx(struct ixgbe_adapter *adapter)
2742 {
2743         struct ixgbe_hw *hw = &adapter->hw;
2744         u32 dmatxctl;
2745         u32 i;
2746
2747         ixgbe_setup_mtqc(adapter);
2748
2749         if (hw->mac.type != ixgbe_mac_82598EB) {
2750                 /* DMATXCTL.EN must be before Tx queues are enabled */
2751                 dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
2752                 dmatxctl |= IXGBE_DMATXCTL_TE;
2753                 IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
2754         }
2755
2756         /* Setup the HW Tx Head and Tail descriptor pointers */
2757         for (i = 0; i < adapter->num_tx_queues; i++)
2758                 ixgbe_configure_tx_ring(adapter, adapter->tx_ring[i]);
2759 }
2760
2761 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
2762
2763 static void ixgbe_configure_srrctl(struct ixgbe_adapter *adapter,
2764                                    struct ixgbe_ring *rx_ring)
2765 {
2766         u32 srrctl;
2767         u8 reg_idx = rx_ring->reg_idx;
2768
2769         switch (adapter->hw.mac.type) {
2770         case ixgbe_mac_82598EB: {
2771                 struct ixgbe_ring_feature *feature = adapter->ring_feature;
2772                 const int mask = feature[RING_F_RSS].mask;
2773                 reg_idx = reg_idx & mask;
2774         }
2775                 break;
2776         case ixgbe_mac_82599EB:
2777         case ixgbe_mac_X540:
2778         default:
2779                 break;
2780         }
2781
2782         srrctl = IXGBE_READ_REG(&adapter->hw, IXGBE_SRRCTL(reg_idx));
2783
2784         srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
2785         srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
2786         if (adapter->num_vfs)
2787                 srrctl |= IXGBE_SRRCTL_DROP_EN;
2788
2789         srrctl |= (IXGBE_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
2790                   IXGBE_SRRCTL_BSIZEHDR_MASK;
2791
2792 #if PAGE_SIZE > IXGBE_MAX_RXBUFFER
2793         srrctl |= IXGBE_MAX_RXBUFFER >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
2794 #else
2795         srrctl |= ixgbe_rx_bufsz(rx_ring) >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
2796 #endif
2797         srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
2798
2799         IXGBE_WRITE_REG(&adapter->hw, IXGBE_SRRCTL(reg_idx), srrctl);
2800 }
2801
2802 static void ixgbe_setup_mrqc(struct ixgbe_adapter *adapter)
2803 {
2804         struct ixgbe_hw *hw = &adapter->hw;
2805         static const u32 seed[10] = { 0xE291D73D, 0x1805EC6C, 0x2A94B30D,
2806                           0xA54F2BEC, 0xEA49AF7C, 0xE214AD3D, 0xB855AABE,
2807                           0x6A3E67EA, 0x14364D17, 0x3BED200D};
2808         u32 mrqc = 0, reta = 0;
2809         u32 rxcsum;
2810         int i, j;
2811         u8 tcs = netdev_get_num_tc(adapter->netdev);
2812         int maxq = adapter->ring_feature[RING_F_RSS].indices;
2813
2814         if (tcs)
2815                 maxq = min(maxq, adapter->num_tx_queues / tcs);
2816
2817         /* Fill out hash function seeds */
2818         for (i = 0; i < 10; i++)
2819                 IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), seed[i]);
2820
2821         /* Fill out redirection table */
2822         for (i = 0, j = 0; i < 128; i++, j++) {
2823                 if (j == maxq)
2824                         j = 0;
2825                 /* reta = 4-byte sliding window of
2826                  * 0x00..(indices-1)(indices-1)00..etc. */
2827                 reta = (reta << 8) | (j * 0x11);
2828                 if ((i & 3) == 3)
2829                         IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
2830         }
2831
2832         /* Disable indicating checksum in descriptor, enables RSS hash */
2833         rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
2834         rxcsum |= IXGBE_RXCSUM_PCSD;
2835         IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
2836
2837         if (adapter->hw.mac.type == ixgbe_mac_82598EB &&
2838             (adapter->flags & IXGBE_FLAG_RSS_ENABLED)) {
2839                 mrqc = IXGBE_MRQC_RSSEN;
2840         } else {
2841                 int mask = adapter->flags & (IXGBE_FLAG_RSS_ENABLED
2842                                              | IXGBE_FLAG_SRIOV_ENABLED);
2843
2844                 switch (mask) {
2845                 case (IXGBE_FLAG_RSS_ENABLED):
2846                         if (!tcs)
2847                                 mrqc = IXGBE_MRQC_RSSEN;
2848                         else if (tcs <= 4)
2849                                 mrqc = IXGBE_MRQC_RTRSS4TCEN;
2850                         else
2851                                 mrqc = IXGBE_MRQC_RTRSS8TCEN;
2852                         break;
2853                 case (IXGBE_FLAG_SRIOV_ENABLED):
2854                         mrqc = IXGBE_MRQC_VMDQEN;
2855                         break;
2856                 default:
2857                         break;
2858                 }
2859         }
2860
2861         /* Perform hash on these packet types */
2862         mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4
2863               | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
2864               | IXGBE_MRQC_RSS_FIELD_IPV6
2865               | IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
2866
2867         if (adapter->flags2 & IXGBE_FLAG2_RSS_FIELD_IPV4_UDP)
2868                 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
2869         if (adapter->flags2 & IXGBE_FLAG2_RSS_FIELD_IPV6_UDP)
2870                 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
2871
2872         IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
2873 }
2874
2875 /**
2876  * ixgbe_configure_rscctl - enable RSC for the indicated ring
2877  * @adapter:    address of board private structure
2878  * @index:      index of ring to set
2879  **/
2880 static void ixgbe_configure_rscctl(struct ixgbe_adapter *adapter,
2881                                    struct ixgbe_ring *ring)
2882 {
2883         struct ixgbe_hw *hw = &adapter->hw;
2884         u32 rscctrl;
2885         u8 reg_idx = ring->reg_idx;
2886
2887         if (!ring_is_rsc_enabled(ring))
2888                 return;
2889
2890         rscctrl = IXGBE_READ_REG(hw, IXGBE_RSCCTL(reg_idx));
2891         rscctrl |= IXGBE_RSCCTL_RSCEN;
2892         /*
2893          * we must limit the number of descriptors so that the
2894          * total size of max desc * buf_len is not greater
2895          * than 65536
2896          */
2897 #if (PAGE_SIZE <= 8192)
2898         rscctrl |= IXGBE_RSCCTL_MAXDESC_16;
2899 #elif (PAGE_SIZE <= 16384)
2900         rscctrl |= IXGBE_RSCCTL_MAXDESC_8;
2901 #else
2902         rscctrl |= IXGBE_RSCCTL_MAXDESC_4;
2903 #endif
2904         IXGBE_WRITE_REG(hw, IXGBE_RSCCTL(reg_idx), rscctrl);
2905 }
2906
2907 /**
2908  *  ixgbe_set_uta - Set unicast filter table address
2909  *  @adapter: board private structure
2910  *
2911  *  The unicast table address is a register array of 32-bit registers.
2912  *  The table is meant to be used in a way similar to how the MTA is used
2913  *  however due to certain limitations in the hardware it is necessary to
2914  *  set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
2915  *  enable bit to allow vlan tag stripping when promiscuous mode is enabled
2916  **/
2917 static void ixgbe_set_uta(struct ixgbe_adapter *adapter)
2918 {
2919         struct ixgbe_hw *hw = &adapter->hw;
2920         int i;
2921
2922         /* The UTA table only exists on 82599 hardware and newer */
2923         if (hw->mac.type < ixgbe_mac_82599EB)
2924                 return;
2925
2926         /* we only need to do this if VMDq is enabled */
2927         if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
2928                 return;
2929
2930         for (i = 0; i < 128; i++)
2931                 IXGBE_WRITE_REG(hw, IXGBE_UTA(i), ~0);
2932 }
2933
2934 #define IXGBE_MAX_RX_DESC_POLL 10
2935 static void ixgbe_rx_desc_queue_enable(struct ixgbe_adapter *adapter,
2936                                        struct ixgbe_ring *ring)
2937 {
2938         struct ixgbe_hw *hw = &adapter->hw;
2939         int wait_loop = IXGBE_MAX_RX_DESC_POLL;
2940         u32 rxdctl;
2941         u8 reg_idx = ring->reg_idx;
2942
2943         /* RXDCTL.EN will return 0 on 82598 if link is down, so skip it */
2944         if (hw->mac.type == ixgbe_mac_82598EB &&
2945             !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
2946                 return;
2947
2948         do {
2949                 usleep_range(1000, 2000);
2950                 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
2951         } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
2952
2953         if (!wait_loop) {
2954                 e_err(drv, "RXDCTL.ENABLE on Rx queue %d not set within "
2955                       "the polling period\n", reg_idx);
2956         }
2957 }
2958
2959 void ixgbe_disable_rx_queue(struct ixgbe_adapter *adapter,
2960                             struct ixgbe_ring *ring)
2961 {
2962         struct ixgbe_hw *hw = &adapter->hw;
2963         int wait_loop = IXGBE_MAX_RX_DESC_POLL;
2964         u32 rxdctl;
2965         u8 reg_idx = ring->reg_idx;
2966
2967         rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
2968         rxdctl &= ~IXGBE_RXDCTL_ENABLE;
2969
2970         /* write value back with RXDCTL.ENABLE bit cleared */
2971         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(reg_idx), rxdctl);
2972
2973         if (hw->mac.type == ixgbe_mac_82598EB &&
2974             !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
2975                 return;
2976
2977         /* the hardware may take up to 100us to really disable the rx queue */
2978         do {
2979                 udelay(10);
2980                 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
2981         } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
2982
2983         if (!wait_loop) {
2984                 e_err(drv, "RXDCTL.ENABLE on Rx queue %d not cleared within "
2985                       "the polling period\n", reg_idx);
2986         }
2987 }
2988
2989 void ixgbe_configure_rx_ring(struct ixgbe_adapter *adapter,
2990                              struct ixgbe_ring *ring)
2991 {
2992         struct ixgbe_hw *hw = &adapter->hw;
2993         u64 rdba = ring->dma;
2994         u32 rxdctl;
2995         u8 reg_idx = ring->reg_idx;
2996
2997         /* disable queue to avoid issues while updating state */
2998         rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
2999         ixgbe_disable_rx_queue(adapter, ring);
3000
3001         IXGBE_WRITE_REG(hw, IXGBE_RDBAL(reg_idx), (rdba & DMA_BIT_MASK(32)));
3002         IXGBE_WRITE_REG(hw, IXGBE_RDBAH(reg_idx), (rdba >> 32));
3003         IXGBE_WRITE_REG(hw, IXGBE_RDLEN(reg_idx),
3004                         ring->count * sizeof(union ixgbe_adv_rx_desc));
3005         IXGBE_WRITE_REG(hw, IXGBE_RDH(reg_idx), 0);
3006         IXGBE_WRITE_REG(hw, IXGBE_RDT(reg_idx), 0);
3007         ring->tail = hw->hw_addr + IXGBE_RDT(reg_idx);
3008
3009         ixgbe_configure_srrctl(adapter, ring);
3010         ixgbe_configure_rscctl(adapter, ring);
3011
3012         /* If operating in IOV mode set RLPML for X540 */
3013         if ((adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) &&
3014             hw->mac.type == ixgbe_mac_X540) {
3015                 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
3016                 rxdctl |= ((ring->netdev->mtu + ETH_HLEN +
3017                             ETH_FCS_LEN + VLAN_HLEN) | IXGBE_RXDCTL_RLPML_EN);
3018         }
3019
3020         if (hw->mac.type == ixgbe_mac_82598EB) {
3021                 /*
3022                  * enable cache line friendly hardware writes:
3023                  * PTHRESH=32 descriptors (half the internal cache),
3024                  * this also removes ugly rx_no_buffer_count increment
3025                  * HTHRESH=4 descriptors (to minimize latency on fetch)
3026                  * WTHRESH=8 burst writeback up to two cache lines
3027                  */
3028                 rxdctl &= ~0x3FFFFF;
3029                 rxdctl |=  0x080420;
3030         }
3031
3032         /* enable receive descriptor ring */
3033         rxdctl |= IXGBE_RXDCTL_ENABLE;
3034         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(reg_idx), rxdctl);
3035
3036         ixgbe_rx_desc_queue_enable(adapter, ring);
3037         ixgbe_alloc_rx_buffers(ring, ixgbe_desc_unused(ring));
3038 }
3039
3040 static void ixgbe_setup_psrtype(struct ixgbe_adapter *adapter)
3041 {
3042         struct ixgbe_hw *hw = &adapter->hw;
3043         int p;
3044
3045         /* PSRTYPE must be initialized in non 82598 adapters */
3046         u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
3047                       IXGBE_PSRTYPE_UDPHDR |
3048                       IXGBE_PSRTYPE_IPV4HDR |
3049                       IXGBE_PSRTYPE_L2HDR |
3050                       IXGBE_PSRTYPE_IPV6HDR;
3051
3052         if (hw->mac.type == ixgbe_mac_82598EB)
3053                 return;
3054
3055         if (adapter->flags & IXGBE_FLAG_RSS_ENABLED)
3056                 psrtype |= (adapter->num_rx_queues_per_pool << 29);
3057
3058         for (p = 0; p < adapter->num_rx_pools; p++)
3059                 IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(adapter->num_vfs + p),
3060                                 psrtype);
3061 }
3062
3063 static void ixgbe_configure_virtualization(struct ixgbe_adapter *adapter)
3064 {
3065         struct ixgbe_hw *hw = &adapter->hw;
3066         u32 gcr_ext;
3067         u32 vt_reg_bits;
3068         u32 reg_offset, vf_shift;
3069         u32 vmdctl;
3070         int i;
3071
3072         if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
3073                 return;
3074
3075         vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
3076         vt_reg_bits = IXGBE_VMD_CTL_VMDQ_EN | IXGBE_VT_CTL_REPLEN;
3077         vt_reg_bits |= (adapter->num_vfs << IXGBE_VT_CTL_POOL_SHIFT);
3078         IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl | vt_reg_bits);
3079
3080         vf_shift = adapter->num_vfs % 32;
3081         reg_offset = (adapter->num_vfs >= 32) ? 1 : 0;
3082
3083         /* Enable only the PF's pool for Tx/Rx */
3084         IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), (1 << vf_shift));
3085         IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset ^ 1), 0);
3086         IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), (1 << vf_shift));
3087         IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset ^ 1), 0);
3088         IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
3089
3090         /* Map PF MAC address in RAR Entry 0 to first pool following VFs */
3091         hw->mac.ops.set_vmdq(hw, 0, adapter->num_vfs);
3092
3093         /*
3094          * Set up VF register offsets for selected VT Mode,
3095          * i.e. 32 or 64 VFs for SR-IOV
3096          */
3097         gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
3098         gcr_ext |= IXGBE_GCR_EXT_MSIX_EN;
3099         gcr_ext |= IXGBE_GCR_EXT_VT_MODE_64;
3100         IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
3101
3102         /* enable Tx loopback for VF/PF communication */
3103         IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
3104         /* Enable MAC Anti-Spoofing */
3105         hw->mac.ops.set_mac_anti_spoofing(hw,
3106                                            (adapter->num_vfs != 0),
3107                                           adapter->num_vfs);
3108         /* For VFs that have spoof checking turned off */
3109         for (i = 0; i < adapter->num_vfs; i++) {
3110                 if (!adapter->vfinfo[i].spoofchk_enabled)
3111                         ixgbe_ndo_set_vf_spoofchk(adapter->netdev, i, false);
3112         }
3113 }
3114
3115 static void ixgbe_set_rx_buffer_len(struct ixgbe_adapter *adapter)
3116 {
3117         struct ixgbe_hw *hw = &adapter->hw;
3118         struct net_device *netdev = adapter->netdev;
3119         int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
3120         struct ixgbe_ring *rx_ring;
3121         int i;
3122         u32 mhadd, hlreg0;
3123
3124 #ifdef IXGBE_FCOE
3125         /* adjust max frame to be able to do baby jumbo for FCoE */
3126         if ((adapter->flags & IXGBE_FLAG_FCOE_ENABLED) &&
3127             (max_frame < IXGBE_FCOE_JUMBO_FRAME_SIZE))
3128                 max_frame = IXGBE_FCOE_JUMBO_FRAME_SIZE;
3129
3130 #endif /* IXGBE_FCOE */
3131         mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
3132         if (max_frame != (mhadd >> IXGBE_MHADD_MFS_SHIFT)) {
3133                 mhadd &= ~IXGBE_MHADD_MFS_MASK;
3134                 mhadd |= max_frame << IXGBE_MHADD_MFS_SHIFT;
3135
3136                 IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
3137         }
3138
3139         /* MHADD will allow an extra 4 bytes past for vlan tagged frames */
3140         max_frame += VLAN_HLEN;
3141
3142         hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
3143         /* set jumbo enable since MHADD.MFS is keeping size locked at max_frame */
3144         hlreg0 |= IXGBE_HLREG0_JUMBOEN;
3145         IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
3146
3147         /*
3148          * Setup the HW Rx Head and Tail Descriptor Pointers and
3149          * the Base and Length of the Rx Descriptor Ring
3150          */
3151         for (i = 0; i < adapter->num_rx_queues; i++) {
3152                 rx_ring = adapter->rx_ring[i];
3153                 if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)
3154                         set_ring_rsc_enabled(rx_ring);
3155                 else
3156                         clear_ring_rsc_enabled(rx_ring);
3157 #ifdef IXGBE_FCOE
3158                 if (netdev->features & NETIF_F_FCOE_MTU) {
3159                         struct ixgbe_ring_feature *f;
3160                         f = &adapter->ring_feature[RING_F_FCOE];
3161                         if ((i >= f->mask) && (i < f->mask + f->indices))
3162                                 set_bit(__IXGBE_RX_FCOE_BUFSZ, &rx_ring->state);
3163                 }
3164 #endif /* IXGBE_FCOE */
3165         }
3166 }
3167
3168 static void ixgbe_setup_rdrxctl(struct ixgbe_adapter *adapter)
3169 {
3170         struct ixgbe_hw *hw = &adapter->hw;
3171         u32 rdrxctl = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
3172
3173         switch (hw->mac.type) {
3174         case ixgbe_mac_82598EB:
3175                 /*
3176                  * For VMDq support of different descriptor types or
3177                  * buffer sizes through the use of multiple SRRCTL
3178                  * registers, RDRXCTL.MVMEN must be set to 1
3179                  *
3180                  * also, the manual doesn't mention it clearly but DCA hints
3181                  * will only use queue 0's tags unless this bit is set.  Side
3182                  * effects of setting this bit are only that SRRCTL must be
3183                  * fully programmed [0..15]
3184                  */
3185                 rdrxctl |= IXGBE_RDRXCTL_MVMEN;
3186                 break;
3187         case ixgbe_mac_82599EB:
3188         case ixgbe_mac_X540:
3189                 /* Disable RSC for ACK packets */
3190                 IXGBE_WRITE_REG(hw, IXGBE_RSCDBU,
3191                    (IXGBE_RSCDBU_RSCACKDIS | IXGBE_READ_REG(hw, IXGBE_RSCDBU)));
3192                 rdrxctl &= ~IXGBE_RDRXCTL_RSCFRSTSIZE;
3193                 /* hardware requires some bits to be set by default */
3194                 rdrxctl |= (IXGBE_RDRXCTL_RSCACKC | IXGBE_RDRXCTL_FCOE_WRFIX);
3195                 rdrxctl |= IXGBE_RDRXCTL_CRCSTRIP;
3196                 break;
3197         default:
3198                 /* We should do nothing since we don't know this hardware */
3199                 return;
3200         }
3201
3202         IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rdrxctl);
3203 }
3204
3205 /**
3206  * ixgbe_configure_rx - Configure 8259x Receive Unit after Reset
3207  * @adapter: board private structure
3208  *
3209  * Configure the Rx unit of the MAC after a reset.
3210  **/
3211 static void ixgbe_configure_rx(struct ixgbe_adapter *adapter)
3212 {
3213         struct ixgbe_hw *hw = &adapter->hw;
3214         int i;
3215         u32 rxctrl;
3216
3217         /* disable receives while setting up the descriptors */
3218         rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
3219         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
3220
3221         ixgbe_setup_psrtype(adapter);
3222         ixgbe_setup_rdrxctl(adapter);
3223
3224         /* Program registers for the distribution of queues */
3225         ixgbe_setup_mrqc(adapter);
3226
3227         ixgbe_set_uta(adapter);
3228
3229         /* set_rx_buffer_len must be called before ring initialization */
3230         ixgbe_set_rx_buffer_len(adapter);
3231
3232         /*
3233          * Setup the HW Rx Head and Tail Descriptor Pointers and
3234          * the Base and Length of the Rx Descriptor Ring
3235          */
3236         for (i = 0; i < adapter->num_rx_queues; i++)
3237                 ixgbe_configure_rx_ring(adapter, adapter->rx_ring[i]);
3238
3239         /* disable drop enable for 82598 parts */
3240         if (hw->mac.type == ixgbe_mac_82598EB)
3241                 rxctrl |= IXGBE_RXCTRL_DMBYPS;
3242
3243         /* enable all receives */
3244         rxctrl |= IXGBE_RXCTRL_RXEN;
3245         hw->mac.ops.enable_rx_dma(hw, rxctrl);
3246 }
3247
3248 static int ixgbe_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
3249 {
3250         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3251         struct ixgbe_hw *hw = &adapter->hw;
3252         int pool_ndx = adapter->num_vfs;
3253
3254         /* add VID to filter table */
3255         hw->mac.ops.set_vfta(&adapter->hw, vid, pool_ndx, true);
3256         set_bit(vid, adapter->active_vlans);
3257
3258         return 0;
3259 }
3260
3261 static int ixgbe_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
3262 {
3263         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3264         struct ixgbe_hw *hw = &adapter->hw;
3265         int pool_ndx = adapter->num_vfs;
3266
3267         /* remove VID from filter table */
3268         hw->mac.ops.set_vfta(&adapter->hw, vid, pool_ndx, false);
3269         clear_bit(vid, adapter->active_vlans);
3270
3271         return 0;
3272 }
3273
3274 /**
3275  * ixgbe_vlan_filter_disable - helper to disable hw vlan filtering
3276  * @adapter: driver data
3277  */
3278 static void ixgbe_vlan_filter_disable(struct ixgbe_adapter *adapter)
3279 {
3280         struct ixgbe_hw *hw = &adapter->hw;
3281         u32 vlnctrl;
3282
3283         vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3284         vlnctrl &= ~(IXGBE_VLNCTRL_VFE | IXGBE_VLNCTRL_CFIEN);
3285         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3286 }
3287
3288 /**
3289  * ixgbe_vlan_filter_enable - helper to enable hw vlan filtering
3290  * @adapter: driver data
3291  */
3292 static void ixgbe_vlan_filter_enable(struct ixgbe_adapter *adapter)
3293 {
3294         struct ixgbe_hw *hw = &adapter->hw;
3295         u32 vlnctrl;
3296
3297         vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3298         vlnctrl |= IXGBE_VLNCTRL_VFE;
3299         vlnctrl &= ~IXGBE_VLNCTRL_CFIEN;
3300         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3301 }
3302
3303 /**
3304  * ixgbe_vlan_strip_disable - helper to disable hw vlan stripping
3305  * @adapter: driver data
3306  */
3307 static void ixgbe_vlan_strip_disable(struct ixgbe_adapter *adapter)
3308 {
3309         struct ixgbe_hw *hw = &adapter->hw;
3310         u32 vlnctrl;
3311         int i, j;
3312
3313         switch (hw->mac.type) {
3314         case ixgbe_mac_82598EB:
3315                 vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3316                 vlnctrl &= ~IXGBE_VLNCTRL_VME;
3317                 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3318                 break;
3319         case ixgbe_mac_82599EB:
3320         case ixgbe_mac_X540:
3321                 for (i = 0; i < adapter->num_rx_queues; i++) {
3322                         j = adapter->rx_ring[i]->reg_idx;
3323                         vlnctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
3324                         vlnctrl &= ~IXGBE_RXDCTL_VME;
3325                         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), vlnctrl);
3326                 }
3327                 break;
3328         default:
3329                 break;
3330         }
3331 }
3332
3333 /**
3334  * ixgbe_vlan_strip_enable - helper to enable hw vlan stripping
3335  * @adapter: driver data
3336  */
3337 static void ixgbe_vlan_strip_enable(struct ixgbe_adapter *adapter)
3338 {
3339         struct ixgbe_hw *hw = &adapter->hw;
3340         u32 vlnctrl;
3341         int i, j;
3342
3343         switch (hw->mac.type) {
3344         case ixgbe_mac_82598EB:
3345                 vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3346                 vlnctrl |= IXGBE_VLNCTRL_VME;
3347                 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3348                 break;
3349         case ixgbe_mac_82599EB:
3350         case ixgbe_mac_X540:
3351                 for (i = 0; i < adapter->num_rx_queues; i++) {
3352                         j = adapter->rx_ring[i]->reg_idx;
3353                         vlnctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
3354                         vlnctrl |= IXGBE_RXDCTL_VME;
3355                         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), vlnctrl);
3356                 }
3357                 break;
3358         default:
3359                 break;
3360         }
3361 }
3362
3363 static void ixgbe_restore_vlan(struct ixgbe_adapter *adapter)
3364 {
3365         u16 vid;
3366
3367         ixgbe_vlan_rx_add_vid(adapter->netdev, 0);
3368
3369         for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
3370                 ixgbe_vlan_rx_add_vid(adapter->netdev, vid);
3371 }
3372
3373 /**
3374  * ixgbe_write_uc_addr_list - write unicast addresses to RAR table
3375  * @netdev: network interface device structure
3376  *
3377  * Writes unicast address list to the RAR table.
3378  * Returns: -ENOMEM on failure/insufficient address space
3379  *                0 on no addresses written
3380  *                X on writing X addresses to the RAR table
3381  **/
3382 static int ixgbe_write_uc_addr_list(struct net_device *netdev)
3383 {
3384         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3385         struct ixgbe_hw *hw = &adapter->hw;
3386         unsigned int vfn = adapter->num_vfs;
3387         unsigned int rar_entries = IXGBE_MAX_PF_MACVLANS;
3388         int count = 0;
3389
3390         /* return ENOMEM indicating insufficient memory for addresses */
3391         if (netdev_uc_count(netdev) > rar_entries)
3392                 return -ENOMEM;
3393
3394         if (!netdev_uc_empty(netdev) && rar_entries) {
3395                 struct netdev_hw_addr *ha;
3396                 /* return error if we do not support writing to RAR table */
3397                 if (!hw->mac.ops.set_rar)
3398                         return -ENOMEM;
3399
3400                 netdev_for_each_uc_addr(ha, netdev) {
3401                         if (!rar_entries)
3402                                 break;
3403                         hw->mac.ops.set_rar(hw, rar_entries--, ha->addr,
3404                                             vfn, IXGBE_RAH_AV);
3405                         count++;
3406                 }
3407         }
3408         /* write the addresses in reverse order to avoid write combining */
3409         for (; rar_entries > 0 ; rar_entries--)
3410                 hw->mac.ops.clear_rar(hw, rar_entries);
3411
3412         return count;
3413 }
3414
3415 /**
3416  * ixgbe_set_rx_mode - Unicast, Multicast and Promiscuous mode set
3417  * @netdev: network interface device structure
3418  *
3419  * The set_rx_method entry point is called whenever the unicast/multicast
3420  * address list or the network interface flags are updated.  This routine is
3421  * responsible for configuring the hardware for proper unicast, multicast and
3422  * promiscuous mode.
3423  **/
3424 void ixgbe_set_rx_mode(struct net_device *netdev)
3425 {
3426         struct ixgbe_adapter *adapter = netdev_priv(netdev);
3427         struct ixgbe_hw *hw = &adapter->hw;
3428         u32 fctrl, vmolr = IXGBE_VMOLR_BAM | IXGBE_VMOLR_AUPE;
3429         int count;
3430
3431         /* Check for Promiscuous and All Multicast modes */
3432
3433         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3434
3435         /* set all bits that we expect to always be set */
3436         fctrl &= ~IXGBE_FCTRL_SBP; /* disable store-bad-packets */
3437         fctrl |= IXGBE_FCTRL_BAM;
3438         fctrl |= IXGBE_FCTRL_DPF; /* discard pause frames when FC enabled */
3439         fctrl |= IXGBE_FCTRL_PMCF;
3440
3441         /* clear the bits we are changing the status of */
3442         fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
3443
3444         if (netdev->flags & IFF_PROMISC) {
3445                 hw->addr_ctrl.user_set_promisc = true;
3446                 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
3447                 vmolr |= (IXGBE_VMOLR_ROPE | IXGBE_VMOLR_MPE);
3448                 /* don't hardware filter vlans in promisc mode */
3449                 ixgbe_vlan_filter_disable(adapter);
3450         } else {
3451                 if (netdev->flags & IFF_ALLMULTI) {
3452                         fctrl |= IXGBE_FCTRL_MPE;
3453                         vmolr |= IXGBE_VMOLR_MPE;
3454                 } else {
3455                         /*
3456                          * Write addresses to the MTA, if the attempt fails
3457                          * then we should just turn on promiscuous mode so
3458                          * that we can at least receive multicast traffic
3459                          */
3460                         hw->mac.ops.update_mc_addr_list(hw, netdev);
3461                         vmolr |= IXGBE_VMOLR_ROMPE;
3462                 }
3463                 ixgbe_vlan_filter_enable(adapter);
3464                 hw->addr_ctrl.user_set_promisc = false;
3465         }
3466
3467         /*
3468          * Write addresses to available RAR registers, if there is not
3469          * sufficient space to store all the addresses then enable
3470          * unicast promiscuous mode
3471          */
3472         count = ixgbe_write_uc_addr_list(netdev);
3473         if (count < 0) {
3474                 fctrl |= IXGBE_FCTRL_UPE;
3475                 vmolr |= IXGBE_VMOLR_ROPE;
3476         }
3477
3478         if (adapter->num_vfs) {
3479                 ixgbe_restore_vf_multicasts(adapter);
3480                 vmolr |= IXGBE_READ_REG(hw, IXGBE_VMOLR(adapter->num_vfs)) &
3481                          ~(IXGBE_VMOLR_MPE | IXGBE_VMOLR_ROMPE |
3482                            IXGBE_VMOLR_ROPE);
3483                 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(adapter->num_vfs), vmolr);
3484         }
3485
3486         /* This is useful for sniffing bad packets. */
3487         if (adapter->netdev->features & NETIF_F_RXALL) {
3488                 /* UPE and MPE will be handled by normal PROMISC logic
3489                  * in e1000e_set_rx_mode */
3490                 fctrl |= (IXGBE_FCTRL_SBP | /* Receive bad packets */
3491                           IXGBE_FCTRL_BAM | /* RX All Bcast Pkts */
3492                           IXGBE_FCTRL_PMCF); /* RX All MAC Ctrl Pkts */
3493
3494                 fctrl &= ~(IXGBE_FCTRL_DPF);
3495                 /* NOTE:  VLAN filtering is disabled by setting PROMISC */
3496         }
3497
3498         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
3499
3500         if (netdev->features & NETIF_F_HW_VLAN_RX)
3501                 ixgbe_vlan_strip_enable(adapter);
3502         else
3503                 ixgbe_vlan_strip_disable(adapter);
3504 }
3505
3506 static void ixgbe_napi_enable_all(struct ixgbe_adapter *adapter)
3507 {
3508         int q_idx;
3509         struct ixgbe_q_vector *q_vector;
3510         int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3511
3512         /* legacy and MSI only use one vector */
3513         if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
3514                 q_vectors = 1;
3515
3516         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
3517                 q_vector = adapter->q_vector[q_idx];
3518                 napi_enable(&q_vector->napi);
3519         }
3520 }
3521
3522 static void ixgbe_napi_disable_all(struct ixgbe_adapter *adapter)
3523 {
3524         int q_idx;
3525         struct ixgbe_q_vector *q_vector;
3526         int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3527
3528         /* legacy and MSI only use one vector */
3529         if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
3530                 q_vectors = 1;
3531
3532         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
3533                 q_vector = adapter->q_vector[q_idx];
3534                 napi_disable(&q_vector->napi);
3535         }
3536 }
3537
3538 #ifdef CONFIG_IXGBE_DCB
3539 /*
3540  * ixgbe_configure_dcb - Configure DCB hardware
3541  * @adapter: ixgbe adapter struct
3542  *
3543  * This is called by the driver on open to configure the DCB hardware.
3544  * This is also called by the gennetlink interface when reconfiguring
3545  * the DCB state.
3546  */
3547 static void ixgbe_configure_dcb(struct ixgbe_adapter *adapter)
3548 {
3549         struct ixgbe_hw *hw = &adapter->hw;
3550         int max_frame = adapter->netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
3551
3552         if (!(adapter->flags & IXGBE_FLAG_DCB_ENABLED)) {
3553                 if (hw->mac.type == ixgbe_mac_82598EB)
3554                         netif_set_gso_max_size(adapter->netdev, 65536);
3555                 return;
3556         }
3557
3558         if (hw->mac.type == ixgbe_mac_82598EB)
3559                 netif_set_gso_max_size(adapter->netdev, 32768);
3560
3561
3562         /* Enable VLAN tag insert/strip */
3563         adapter->netdev->features |= NETIF_F_HW_VLAN_RX;
3564
3565         hw->mac.ops.set_vfta(&adapter->hw, 0, 0, true);
3566
3567 #ifdef IXGBE_FCOE
3568         if (adapter->netdev->features & NETIF_F_FCOE_MTU)
3569                 max_frame = max(max_frame, IXGBE_FCOE_JUMBO_FRAME_SIZE);
3570 #endif
3571
3572         /* reconfigure the hardware */
3573         if (adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE) {
3574                 ixgbe_dcb_calculate_tc_credits(hw, &adapter->dcb_cfg, max_frame,
3575                                                 DCB_TX_CONFIG);
3576                 ixgbe_dcb_calculate_tc_credits(hw, &adapter->dcb_cfg, max_frame,
3577                                                 DCB_RX_CONFIG);
3578                 ixgbe_dcb_hw_config(hw, &adapter->dcb_cfg);
3579         } else if (adapter->ixgbe_ieee_ets && adapter->ixgbe_ieee_pfc) {
3580                 ixgbe_dcb_hw_ets(&adapter->hw,
3581                                  adapter->ixgbe_ieee_ets,
3582                                  max_frame);
3583                 ixgbe_dcb_hw_pfc_config(&adapter->hw,
3584                                         adapter->ixgbe_ieee_pfc->pfc_en,
3585                                         adapter->ixgbe_ieee_ets->prio_tc);
3586         }
3587
3588         /* Enable RSS Hash per TC */
3589         if (hw->mac.type != ixgbe_mac_82598EB) {
3590                 int i;
3591                 u32 reg = 0;
3592
3593                 for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
3594                         u8 msb = 0;
3595                         u8 cnt = adapter->netdev->tc_to_txq[i].count;
3596
3597                         while (cnt >>= 1)
3598                                 msb++;
3599
3600                         reg |= msb << IXGBE_RQTC_SHIFT_TC(i);
3601                 }
3602                 IXGBE_WRITE_REG(hw, IXGBE_RQTC, reg);
3603         }
3604 }
3605 #endif
3606
3607 /* Additional bittime to account for IXGBE framing */
3608 #define IXGBE_ETH_FRAMING 20
3609
3610 /*
3611  * ixgbe_hpbthresh - calculate high water mark for flow control
3612  *
3613  * @adapter: board private structure to calculate for
3614  * @pb - packet buffer to calculate
3615  */
3616 static int ixgbe_hpbthresh(struct ixgbe_adapter *adapter, int pb)
3617 {
3618         struct ixgbe_hw *hw = &adapter->hw;
3619         struct net_device *dev = adapter->netdev;
3620         int link, tc, kb, marker;
3621         u32 dv_id, rx_pba;
3622
3623         /* Calculate max LAN frame size */
3624         tc = link = dev->mtu + ETH_HLEN + ETH_FCS_LEN + IXGBE_ETH_FRAMING;
3625
3626 #ifdef IXGBE_FCOE
3627         /* FCoE traffic class uses FCOE jumbo frames */
3628         if (dev->features & NETIF_F_FCOE_MTU) {
3629                 int fcoe_pb = 0;
3630
3631 #ifdef CONFIG_IXGBE_DCB
3632                 fcoe_pb = netdev_get_prio_tc_map(dev, adapter->fcoe.up);
3633
3634 #endif
3635                 if (fcoe_pb == pb && tc < IXGBE_FCOE_JUMBO_FRAME_SIZE)
3636                         tc = IXGBE_FCOE_JUMBO_FRAME_SIZE;
3637         }
3638 #endif
3639
3640         /* Calculate delay value for device */
3641         switch (hw->mac.type) {
3642         case ixgbe_mac_X540:
3643                 dv_id = IXGBE_DV_X540(link, tc);
3644                 break;
3645         default:
3646                 dv_id = IXGBE_DV(link, tc);
3647                 break;
3648         }
3649
3650         /* Loopback switch introduces additional latency */
3651         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
3652                 dv_id += IXGBE_B2BT(tc);
3653
3654         /* Delay value is calculated in bit times convert to KB */
3655         kb = IXGBE_BT2KB(dv_id);
3656         rx_pba = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(pb)) >> 10;
3657
3658         marker = rx_pba - kb;
3659
3660         /* It is possible that the packet buffer is not large enough
3661          * to provide required headroom. In this case throw an error
3662          * to user and a do the best we can.
3663          */
3664         if (marker < 0) {
3665                 e_warn(drv, "Packet Buffer(%i) can not provide enough"
3666                             "headroom to support flow control."
3667                             "Decrease MTU or number of traffic classes\n", pb);
3668                 marker = tc + 1;
3669         }
3670
3671         return marker;
3672 }
3673
3674 /*
3675  * ixgbe_lpbthresh - calculate low water mark for for flow control
3676  *
3677  * @adapter: board private structure to calculate for
3678  * @pb - packet buffer to calculate
3679  */
3680 static int ixgbe_lpbthresh(struct ixgbe_adapter *adapter)
3681 {
3682         struct ixgbe_hw *hw = &adapter->hw;
3683         struct net_device *dev = adapter->netdev;
3684         int tc;
3685         u32 dv_id;
3686
3687         /* Calculate max LAN frame size */
3688         tc = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
3689
3690         /* Calculate delay value for device */
3691         switch (hw->mac.type) {
3692         case ixgbe_mac_X540:
3693                 dv_id = IXGBE_LOW_DV_X540(tc);
3694                 break;
3695         default:
3696                 dv_id = IXGBE_LOW_DV(tc);
3697                 break;
3698         }
3699
3700         /* Delay value is calculated in bit times convert to KB */
3701         return IXGBE_BT2KB(dv_id);
3702 }
3703
3704 /*
3705  * ixgbe_pbthresh_setup - calculate and setup high low water marks
3706  */
3707 static void ixgbe_pbthresh_setup(struct ixgbe_adapter *adapter)
3708 {
3709         struct ixgbe_hw *hw = &adapter->hw;
3710         int num_tc = netdev_get_num_tc(adapter->netdev);
3711         int i;
3712
3713         if (!num_tc)
3714                 num_tc = 1;
3715
3716         hw->fc.low_water = ixgbe_lpbthresh(adapter);
3717
3718         for (i = 0; i < num_tc; i++) {
3719                 hw->fc.high_water[i] = ixgbe_hpbthresh(adapter, i);
3720
3721                 /* Low water marks must not be larger than high water marks */
3722                 if (hw->fc.low_water > hw->fc.high_water[i])
3723                         hw->fc.low_water = 0;
3724         }
3725 }
3726
3727 static void ixgbe_configure_pb(struct ixgbe_adapter *adapter)
3728 {
3729         struct ixgbe_hw *hw = &adapter->hw;
3730         int hdrm;
3731         u8 tc = netdev_get_num_tc(adapter->netdev);
3732
3733         if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE ||
3734             adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)
3735                 hdrm = 32 << adapter->fdir_pballoc;
3736         else
3737                 hdrm = 0;
3738
3739         hw->mac.ops.set_rxpba(hw, tc, hdrm, PBA_STRATEGY_EQUAL);
3740         ixgbe_pbthresh_setup(adapter);
3741 }
3742
3743 static void ixgbe_fdir_filter_restore(struct ixgbe_adapter *adapter)
3744 {
3745         struct ixgbe_hw *hw = &adapter->hw;
3746         struct hlist_node *node, *node2;
3747         struct ixgbe_fdir_filter *filter;
3748
3749         spin_lock(&adapter->fdir_perfect_lock);
3750
3751         if (!hlist_empty(&adapter->fdir_filter_list))
3752                 ixgbe_fdir_set_input_mask_82599(hw, &adapter->fdir_mask);
3753
3754         hlist_for_each_entry_safe(filter, node, node2,
3755                                   &adapter->fdir_filter_list, fdir_node) {
3756                 ixgbe_fdir_write_perfect_filter_82599(hw,
3757                                 &filter->filter,
3758                                 filter->sw_idx,
3759                                 (filter->action == IXGBE_FDIR_DROP_QUEUE) ?
3760                                 IXGBE_FDIR_DROP_QUEUE :
3761                                 adapter->rx_ring[filter->action]->reg_idx);
3762         }
3763
3764         spin_unlock(&adapter->fdir_perfect_lock);
3765 }
3766
3767 static void ixgbe_configure(struct ixgbe_adapter *adapter)
3768 {
3769         struct ixgbe_hw *hw = &adapter->hw;
3770
3771         ixgbe_configure_pb(adapter);
3772 #ifdef CONFIG_IXGBE_DCB
3773         ixgbe_configure_dcb(adapter);
3774 #endif
3775
3776         ixgbe_set_rx_mode(adapter->netdev);
3777         ixgbe_restore_vlan(adapter);
3778
3779 #ifdef IXGBE_FCOE
3780         if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
3781                 ixgbe_configure_fcoe(adapter);
3782
3783 #endif /* IXGBE_FCOE */
3784
3785         switch (hw->mac.type) {
3786         case ixgbe_mac_82599EB:
3787         case ixgbe_mac_X540:
3788                 hw->mac.ops.disable_rx_buff(hw);
3789                 break;
3790         default:
3791                 break;
3792         }
3793
3794         if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
3795                 ixgbe_init_fdir_signature_82599(&adapter->hw,
3796                                                 adapter->fdir_pballoc);
3797         } else if (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE) {
3798                 ixgbe_init_fdir_perfect_82599(&adapter->hw,
3799                                               adapter->fdir_pballoc);
3800                 ixgbe_fdir_filter_restore(adapter);
3801         }
3802
3803         switch (hw->mac.type) {
3804         case ixgbe_mac_82599EB:
3805         case ixgbe_mac_X540:
3806                 hw->mac.ops.enable_rx_buff(hw);
3807                 break;
3808         default:
3809                 break;
3810         }
3811
3812         ixgbe_configure_virtualization(adapter);
3813
3814         ixgbe_configure_tx(adapter);
3815         ixgbe_configure_rx(adapter);
3816 }
3817
3818 static inline bool ixgbe_is_sfp(struct ixgbe_hw *hw)
3819 {
3820         switch (hw->phy.type) {
3821         case ixgbe_phy_sfp_avago:
3822         case ixgbe_phy_sfp_ftl:
3823         case ixgbe_phy_sfp_intel:
3824         case ixgbe_phy_sfp_unknown:
3825         case ixgbe_phy_sfp_passive_tyco:
3826         case ixgbe_phy_sfp_passive_unknown:
3827         case ixgbe_phy_sfp_active_unknown:
3828         case ixgbe_phy_sfp_ftl_active:
3829                 return true;
3830         case ixgbe_phy_nl:
3831                 if (hw->mac.type == ixgbe_mac_82598EB)
3832                         return true;
3833         default:
3834                 return false;
3835         }
3836 }
3837
3838 /**
3839  * ixgbe_sfp_link_config - set up SFP+ link
3840  * @adapter: pointer to private adapter struct
3841  **/
3842 static void ixgbe_sfp_link_config(struct ixgbe_adapter *adapter)
3843 {
3844         /*
3845          * We are assuming the worst case scenario here, and that
3846          * is that an SFP was inserted/removed after the reset
3847          * but before SFP detection was enabled.  As such the best
3848          * solution is to just start searching as soon as we start
3849          */
3850         if (adapter->hw.mac.type == ixgbe_mac_82598EB)
3851                 adapter->flags2 |= IXGBE_FLAG2_SEARCH_FOR_SFP;
3852
3853         adapter->flags2 |= IXGBE_FLAG2_SFP_NEEDS_RESET;
3854 }
3855
3856 /**
3857  * ixgbe_non_sfp_link_config - set up non-SFP+ link
3858  * @hw: pointer to private hardware struct
3859  *
3860  * Returns 0 on success, negative on failure
3861  **/
3862 static int ixgbe_non_sfp_link_config(struct ixgbe_hw *hw)
3863 {
3864         u32 autoneg;
3865         bool negotiation, link_up = false;
3866         u32 ret = IXGBE_ERR_LINK_SETUP;
3867
3868         if (hw->mac.ops.check_link)
3869                 ret = hw->mac.ops.check_link(hw, &autoneg, &link_up, false);
3870
3871         if (ret)
3872                 goto link_cfg_out;
3873
3874         autoneg = hw->phy.autoneg_advertised;
3875         if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
3876                 ret = hw->mac.ops.get_link_capabilities(hw, &autoneg,
3877                                                         &negotiation);
3878         if (ret)
3879                 goto link_cfg_out;
3880
3881         if (hw->mac.ops.setup_link)
3882                 ret = hw->mac.ops.setup_link(hw, autoneg, negotiation, link_up);
3883 link_cfg_out:
3884         return ret;
3885 }
3886
3887 static void ixgbe_setup_gpie(struct ixgbe_adapter *adapter)
3888 {
3889         struct ixgbe_hw *hw = &adapter->hw;
3890         u32 gpie = 0;
3891
3892         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3893                 gpie = IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_PBA_SUPPORT |
3894                        IXGBE_GPIE_OCD;
3895                 gpie |= IXGBE_GPIE_EIAME;
3896                 /*
3897                  * use EIAM to auto-mask when MSI-X interrupt is asserted
3898                  * this saves a register write for every interrupt
3899                  */
3900                 switch (hw->mac.type) {
3901                 case ixgbe_mac_82598EB:
3902                         IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
3903                         break;
3904                 case ixgbe_mac_82599EB:
3905                 case ixgbe_mac_X540:
3906                 default:
3907                         IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
3908                         IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
3909                         break;
3910                 }
3911         } else {
3912                 /* legacy interrupts, use EIAM to auto-mask when reading EICR,
3913                  * specifically only auto mask tx and rx interrupts */
3914                 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
3915         }
3916
3917         /* XXX: to interrupt immediately for EICS writes, enable this */
3918         /* gpie |= IXGBE_GPIE_EIMEN; */
3919
3920         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
3921                 gpie &= ~IXGBE_GPIE_VTMODE_MASK;
3922                 gpie |= IXGBE_GPIE_VTMODE_64;
3923         }
3924
3925         /* Enable Thermal over heat sensor interrupt */
3926         if (adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE) {
3927                 switch (adapter->hw.mac.type) {
3928                 case ixgbe_mac_82599EB:
3929                         gpie |= IXGBE_SDP0_GPIEN;
3930                         break;
3931                 case ixgbe_mac_X540:
3932                         gpie |= IXGBE_EIMS_TS;
3933                         break;
3934                 default:
3935                         break;
3936                 }
3937         }
3938
3939         /* Enable fan failure interrupt */
3940         if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE)
3941                 gpie |= IXGBE_SDP1_GPIEN;
3942
3943         if (hw->mac.type == ixgbe_mac_82599EB) {
3944                 gpie |= IXGBE_SDP1_GPIEN;
3945                 gpie |= IXGBE_SDP2_GPIEN;
3946         }
3947
3948         IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
3949 }
3950
3951 static void ixgbe_up_complete(struct ixgbe_adapter *adapter)
3952 {
3953         struct ixgbe_hw *hw = &adapter->hw;
3954         int err;
3955         u32 ctrl_ext;
3956
3957         ixgbe_get_hw_control(adapter);
3958         ixgbe_setup_gpie(adapter);
3959
3960         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
3961                 ixgbe_configure_msix(adapter);
3962         else
3963                 ixgbe_configure_msi_and_legacy(adapter);
3964
3965         /* enable the optics for both mult-speed fiber and 82599 SFP+ fiber */
3966         if (hw->mac.ops.enable_tx_laser &&
3967             ((hw->phy.multispeed_fiber) ||
3968              ((hw->mac.ops.get_media_type(hw) == ixgbe_media_type_fiber) &&
3969               (hw->mac.type == ixgbe_mac_82599EB))))
3970                 hw->mac.ops.enable_tx_laser(hw);
3971
3972         clear_bit(__IXGBE_DOWN, &adapter->state);
3973         ixgbe_napi_enable_all(adapter);
3974
3975         if (ixgbe_is_sfp(hw)) {
3976                 ixgbe_sfp_link_config(adapter);
3977         } else {
3978                 err = ixgbe_non_sfp_link_config(hw);
3979                 if (err)
3980                         e_err(probe, "link_config FAILED %d\n", err);
3981         }
3982
3983         /* clear any pending interrupts, may auto mask */
3984         IXGBE_READ_REG(hw, IXGBE_EICR);
3985         ixgbe_irq_enable(adapter, true, true);
3986
3987         /*
3988          * If this adapter has a fan, check to see if we had a failure
3989          * before we enabled the interrupt.
3990          */
3991         if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) {
3992                 u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
3993                 if (esdp & IXGBE_ESDP_SDP1)
3994                         e_crit(drv, "Fan has stopped, replace the adapter\n");
3995         }
3996
3997         /* enable transmits */
3998         netif_tx_start_all_queues(adapter->netdev);
3999
4000         /* bring the link up in the watchdog, this could race with our first
4001          * link up interrupt but shouldn't be a problem */
4002         adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
4003         adapter->link_check_timeout = jiffies;
4004         mod_timer(&adapter->service_timer, jiffies);
4005
4006         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
4007         ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
4008         ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
4009         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
4010 }
4011
4012 void ixgbe_reinit_locked(struct ixgbe_adapter *adapter)
4013 {
4014         WARN_ON(in_interrupt());
4015         /* put off any impending NetWatchDogTimeout */
4016         adapter->netdev->trans_start = jiffies;
4017
4018         while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
4019                 usleep_range(1000, 2000);
4020         ixgbe_down(adapter);
4021         /*
4022          * If SR-IOV enabled then wait a bit before bringing the adapter
4023          * back up to give the VFs time to respond to the reset.  The
4024          * two second wait is based upon the watchdog timer cycle in
4025          * the VF driver.
4026          */
4027         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
4028                 msleep(2000);
4029         ixgbe_up(adapter);
4030         clear_bit(__IXGBE_RESETTING, &adapter->state);
4031 }
4032
4033 void ixgbe_up(struct ixgbe_adapter *adapter)
4034 {
4035         /* hardware has been reset, we need to reload some things */
4036         ixgbe_configure(adapter);
4037
4038         ixgbe_up_complete(adapter);
4039 }
4040
4041 void ixgbe_reset(struct ixgbe_adapter *adapter)
4042 {
4043         struct ixgbe_hw *hw = &adapter->hw;
4044         int err;
4045
4046         /* lock SFP init bit to prevent race conditions with the watchdog */
4047         while (test_and_set_bit(__IXGBE_IN_SFP_INIT, &adapter->state))
4048                 usleep_range(1000, 2000);
4049
4050         /* clear all SFP and link config related flags while holding SFP_INIT */
4051         adapter->flags2 &= ~(IXGBE_FLAG2_SEARCH_FOR_SFP |
4052                              IXGBE_FLAG2_SFP_NEEDS_RESET);
4053         adapter->flags &= ~IXGBE_FLAG_NEED_LINK_CONFIG;
4054
4055         err = hw->mac.ops.init_hw(hw);
4056         switch (err) {
4057         case 0:
4058         case IXGBE_ERR_SFP_NOT_PRESENT:
4059         case IXGBE_ERR_SFP_NOT_SUPPORTED:
4060                 break;
4061         case IXGBE_ERR_MASTER_REQUESTS_PENDING:
4062                 e_dev_err("master disable timed out\n");
4063                 break;
4064         case IXGBE_ERR_EEPROM_VERSION:
4065                 /* We are running on a pre-production device, log a warning */
4066                 e_dev_warn("This device is a pre-production adapter/LOM. "
4067                            "Please be aware there may be issues associated with "
4068                            "your hardware.  If you are experiencing problems "
4069                            "please contact your Intel or hardware "
4070                            "representative who provided you with this "
4071                            "hardware.\n");
4072                 break;
4073         default:
4074                 e_dev_err("Hardware Error: %d\n", err);
4075         }
4076
4077         clear_bit(__IXGBE_IN_SFP_INIT, &adapter->state);
4078
4079         /* reprogram the RAR[0] in case user changed it. */
4080         hw->mac.ops.set_rar(hw, 0, hw->mac.addr, adapter->num_vfs,
4081                             IXGBE_RAH_AV);
4082 }
4083
4084 /**
4085  * ixgbe_init_rx_page_offset - initialize page offset values for Rx buffers
4086  * @rx_ring: ring to setup
4087  *
4088  * On many IA platforms the L1 cache has a critical stride of 4K, this
4089  * results in each receive buffer starting in the same cache set.  To help
4090  * reduce the pressure on this cache set we can interleave the offsets so
4091  * that only every other buffer will be in the same cache set.
4092  **/
4093 static void ixgbe_init_rx_page_offset(struct ixgbe_ring *rx_ring)
4094 {
4095         struct ixgbe_rx_buffer *rx_buffer = rx_ring->rx_buffer_info;
4096         u16 i;
4097
4098         for (i = 0; i < rx_ring->count; i += 2) {
4099                 rx_buffer[0].page_offset = 0;
4100                 rx_buffer[1].page_offset = ixgbe_rx_bufsz(rx_ring);
4101                 rx_buffer = &rx_buffer[2];
4102         }
4103 }
4104
4105 /**
4106  * ixgbe_clean_rx_ring - Free Rx Buffers per Queue
4107  * @rx_ring: ring to free buffers from
4108  **/
4109 static void ixgbe_clean_rx_ring(struct ixgbe_ring *rx_ring)
4110 {
4111         struct device *dev = rx_ring->dev;
4112         unsigned long size;
4113         u16 i;
4114
4115         /* ring already cleared, nothing to do */
4116         if (!rx_ring->rx_buffer_info)
4117                 return;
4118
4119         /* Free all the Rx ring sk_buffs */
4120         for (i = 0; i < rx_ring->count; i++) {
4121                 struct ixgbe_rx_buffer *rx_buffer;
4122
4123                 rx_buffer = &rx_ring->rx_buffer_info[i];
4124                 if (rx_buffer->skb) {
4125                         struct sk_buff *skb = rx_buffer->skb;
4126                         if (IXGBE_CB(skb)->page_released) {
4127                                 dma_unmap_page(dev,
4128                                                IXGBE_CB(skb)->dma,
4129                                                ixgbe_rx_bufsz(rx_ring),
4130                                                DMA_FROM_DEVICE);
4131                                 IXGBE_CB(skb)->page_released = false;
4132                         }
4133                         dev_kfree_skb(skb);
4134                 }
4135                 rx_buffer->skb = NULL;
4136                 if (rx_buffer->dma)
4137                         dma_unmap_page(dev, rx_buffer->dma,
4138                                        ixgbe_rx_pg_size(rx_ring),
4139                                        DMA_FROM_DEVICE);
4140                 rx_buffer->dma = 0;
4141                 if (rx_buffer->page)
4142                         put_page(rx_buffer->page);
4143                 rx_buffer->page = NULL;
4144         }
4145
4146         size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
4147         memset(rx_ring->rx_buffer_info, 0, size);
4148
4149         ixgbe_init_rx_page_offset(rx_ring);
4150
4151         /* Zero out the descriptor ring */
4152         memset(rx_ring->desc, 0, rx_ring->size);
4153
4154         rx_ring->next_to_alloc = 0;
4155         rx_ring->next_to_clean = 0;
4156         rx_ring->next_to_use = 0;
4157 }
4158
4159 /**
4160  * ixgbe_clean_tx_ring - Free Tx Buffers
4161  * @tx_ring: ring to be cleaned
4162  **/
4163 static void ixgbe_clean_tx_ring(struct ixgbe_ring *tx_ring)
4164 {
4165         struct ixgbe_tx_buffer *tx_buffer_info;
4166         unsigned long size;
4167         u16 i;
4168
4169         /* ring already cleared, nothing to do */
4170         if (!tx_ring->tx_buffer_info)
4171                 return;
4172
4173         /* Free all the Tx ring sk_buffs */
4174         for (i = 0; i < tx_ring->count; i++) {
4175                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
4176                 ixgbe_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
4177         }
4178
4179         size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
4180         memset(tx_ring->tx_buffer_info, 0, size);
4181
4182         /* Zero out the descriptor ring */
4183         memset(tx_ring->desc, 0, tx_ring->size);
4184
4185         tx_ring->next_to_use = 0;
4186         tx_ring->next_to_clean = 0;
4187 }
4188
4189 /**
4190  * ixgbe_clean_all_rx_rings - Free Rx Buffers for all queues
4191  * @adapter: board private structure
4192  **/
4193 static void ixgbe_clean_all_rx_rings(struct ixgbe_adapter *adapter)
4194 {
4195         int i;
4196
4197         for (i = 0; i < adapter->num_rx_queues; i++)
4198                 ixgbe_clean_rx_ring(adapter->rx_ring[i]);
4199 }
4200
4201 /**
4202  * ixgbe_clean_all_tx_rings - Free Tx Buffers for all queues
4203  * @adapter: board private structure
4204  **/
4205 static void ixgbe_clean_all_tx_rings(struct ixgbe_adapter *adapter)
4206 {
4207         int i;
4208
4209         for (i = 0; i < adapter->num_tx_queues; i++)
4210                 ixgbe_clean_tx_ring(adapter->tx_ring[i]);
4211 }
4212
4213 static void ixgbe_fdir_filter_exit(struct ixgbe_adapter *adapter)
4214 {
4215         struct hlist_node *node, *node2;
4216         struct ixgbe_fdir_filter *filter;
4217
4218         spin_lock(&adapter->fdir_perfect_lock);
4219
4220         hlist_for_each_entry_safe(filter, node, node2,
4221                                   &adapter->fdir_filter_list, fdir_node) {
4222                 hlist_del(&filter->fdir_node);
4223                 kfree(filter);
4224         }
4225         adapter->fdir_filter_count = 0;
4226
4227         spin_unlock(&adapter->fdir_perfect_lock);
4228 }
4229
4230 void ixgbe_down(struct ixgbe_adapter *adapter)
4231 {
4232         struct net_device *netdev = adapter->netdev;
4233         struct ixgbe_hw *hw = &adapter->hw;
4234         u32 rxctrl;
4235         int i;
4236
4237         /* signal that we are down to the interrupt handler */
4238         set_bit(__IXGBE_DOWN, &adapter->state);
4239
4240         /* disable receives */
4241         rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
4242         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
4243
4244         /* disable all enabled rx queues */
4245         for (i = 0; i < adapter->num_rx_queues; i++)
4246                 /* this call also flushes the previous write */
4247                 ixgbe_disable_rx_queue(adapter, adapter->rx_ring[i]);
4248
4249         usleep_range(10000, 20000);
4250
4251         netif_tx_stop_all_queues(netdev);
4252
4253         /* call carrier off first to avoid false dev_watchdog timeouts */
4254         netif_carrier_off(netdev);
4255         netif_tx_disable(netdev);
4256
4257         ixgbe_irq_disable(adapter);
4258
4259         ixgbe_napi_disable_all(adapter);
4260
4261         adapter->flags2 &= ~(IXGBE_FLAG2_FDIR_REQUIRES_REINIT |
4262                              IXGBE_FLAG2_RESET_REQUESTED);
4263         adapter->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
4264
4265         del_timer_sync(&adapter->service_timer);
4266
4267         if (adapter->num_vfs) {
4268                 /* Clear EITR Select mapping */
4269                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITRSEL, 0);
4270
4271                 /* Mark all the VFs as inactive */
4272                 for (i = 0 ; i < adapter->num_vfs; i++)
4273                         adapter->vfinfo[i].clear_to_send = false;
4274
4275                 /* ping all the active vfs to let them know we are going down */
4276                 ixgbe_ping_all_vfs(adapter);
4277
4278                 /* Disable all VFTE/VFRE TX/RX */
4279                 ixgbe_disable_tx_rx(adapter);
4280         }
4281
4282         /* disable transmits in the hardware now that interrupts are off */
4283         for (i = 0; i < adapter->num_tx_queues; i++) {
4284                 u8 reg_idx = adapter->tx_ring[i]->reg_idx;
4285                 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
4286         }
4287
4288         /* Disable the Tx DMA engine on 82599 and X540 */
4289         switch (hw->mac.type) {
4290         case ixgbe_mac_82599EB:
4291         case ixgbe_mac_X540:
4292                 IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL,
4293                                 (IXGBE_READ_REG(hw, IXGBE_DMATXCTL) &
4294                                  ~IXGBE_DMATXCTL_TE));
4295                 break;
4296         default:
4297                 break;
4298         }
4299
4300         if (!pci_channel_offline(adapter->pdev))
4301                 ixgbe_reset(adapter);
4302
4303         /* power down the optics for multispeed fiber and 82599 SFP+ fiber */
4304         if (hw->mac.ops.disable_tx_laser &&
4305             ((hw->phy.multispeed_fiber) ||
4306              ((hw->mac.ops.get_media_type(hw) == ixgbe_media_type_fiber) &&
4307               (hw->mac.type == ixgbe_mac_82599EB))))
4308                 hw->mac.ops.disable_tx_laser(hw);
4309
4310         ixgbe_clean_all_tx_rings(adapter);
4311         ixgbe_clean_all_rx_rings(adapter);
4312
4313 #ifdef CONFIG_IXGBE_DCA
4314         /* since we reset the hardware DCA settings were cleared */
4315         ixgbe_setup_dca(adapter);
4316 #endif
4317 }
4318
4319 /**
4320  * ixgbe_tx_timeout - Respond to a Tx Hang
4321  * @netdev: network interface device structure
4322  **/
4323 static void ixgbe_tx_timeout(struct net_device *netdev)
4324 {
4325         struct ixgbe_adapter *adapter = netdev_priv(netdev);
4326
4327         /* Do the reset outside of interrupt context */
4328         ixgbe_tx_timeout_reset(adapter);
4329 }
4330
4331 /**
4332  * ixgbe_sw_init - Initialize general software structures (struct ixgbe_adapter)
4333  * @adapter: board private structure to initialize
4334  *
4335  * ixgbe_sw_init initializes the Adapter private data structure.
4336  * Fields are initialized based on PCI device information and
4337  * OS network device settings (MTU size).
4338  **/
4339 static int __devinit ixgbe_sw_init(struct ixgbe_adapter *adapter)
4340 {
4341         struct ixgbe_hw *hw = &adapter->hw;
4342         struct pci_dev *pdev = adapter->pdev;
4343         unsigned int rss;
4344 #ifdef CONFIG_IXGBE_DCB
4345         int j;
4346         struct tc_configuration *tc;
4347 #endif
4348
4349         /* PCI config space info */
4350
4351         hw->vendor_id = pdev->vendor;
4352         hw->device_id = pdev->device;
4353         hw->revision_id = pdev->revision;
4354         hw->subsystem_vendor_id = pdev->subsystem_vendor;
4355         hw->subsystem_device_id = pdev->subsystem_device;
4356
4357         /* Set capability flags */
4358         rss = min_t(int, IXGBE_MAX_RSS_INDICES, num_online_cpus());
4359         adapter->ring_feature[RING_F_RSS].indices = rss;
4360         adapter->flags |= IXGBE_FLAG_RSS_ENABLED;
4361         switch (hw->mac.type) {
4362         case ixgbe_mac_82598EB:
4363                 if (hw->device_id == IXGBE_DEV_ID_82598AT)
4364                         adapter->flags |= IXGBE_FLAG_FAN_FAIL_CAPABLE;
4365                 adapter->max_msix_q_vectors = MAX_MSIX_Q_VECTORS_82598;
4366                 break;
4367         case ixgbe_mac_X540:
4368                 adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_CAPABLE;
4369         case ixgbe_mac_82599EB:
4370                 adapter->max_msix_q_vectors = MAX_MSIX_Q_VECTORS_82599;
4371                 adapter->flags2 |= IXGBE_FLAG2_RSC_CAPABLE;
4372                 adapter->flags2 |= IXGBE_FLAG2_RSC_ENABLED;
4373                 if (hw->device_id == IXGBE_DEV_ID_82599_T3_LOM)
4374                         adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_CAPABLE;
4375                 /* Flow Director hash filters enabled */
4376                 adapter->flags |= IXGBE_FLAG_FDIR_HASH_CAPABLE;
4377                 adapter->atr_sample_rate = 20;
4378                 adapter->ring_feature[RING_F_FDIR].indices =
4379                                                          IXGBE_MAX_FDIR_INDICES;
4380                 adapter->fdir_pballoc = IXGBE_FDIR_PBALLOC_64K;
4381 #ifdef IXGBE_FCOE
4382                 adapter->flags |= IXGBE_FLAG_FCOE_CAPABLE;
4383                 adapter->flags &= ~IXGBE_FLAG_FCOE_ENABLED;
4384                 adapter->ring_feature[RING_F_FCOE].indices = 0;
4385 #ifdef CONFIG_IXGBE_DCB
4386                 /* Default traffic class to use for FCoE */
4387                 adapter->fcoe.up = IXGBE_FCOE_DEFTC;
4388 #endif
4389 #endif /* IXGBE_FCOE */
4390                 break;
4391         default:
4392                 break;
4393         }
4394
4395         /* n-tuple support exists, always init our spinlock */
4396         spin_lock_init(&adapter->fdir_perfect_lock);
4397
4398 #ifdef CONFIG_IXGBE_DCB
4399         switch (hw->mac.type) {
4400         case ixgbe_mac_X540:
4401                 adapter->dcb_cfg.num_tcs.pg_tcs = X540_TRAFFIC_CLASS;
4402                 adapter->dcb_cfg.num_tcs.pfc_tcs = X540_TRAFFIC_CLASS;
4403                 break;
4404         default:
4405                 adapter->dcb_cfg.num_tcs.pg_tcs = MAX_TRAFFIC_CLASS;
4406                 adapter->dcb_cfg.num_tcs.pfc_tcs = MAX_TRAFFIC_CLASS;
4407                 break;
4408         }
4409
4410         /* Configure DCB traffic classes */
4411         for (j = 0; j < MAX_TRAFFIC_CLASS; j++) {
4412                 tc = &adapter->dcb_cfg.tc_config[j];
4413                 tc->path[DCB_TX_CONFIG].bwg_id = 0;
4414                 tc->path[DCB_TX_CONFIG].bwg_percent = 12 + (j & 1);
4415                 tc->path[DCB_RX_CONFIG].bwg_id = 0;
4416                 tc->path[DCB_RX_CONFIG].bwg_percent = 12 + (j & 1);
4417                 tc->dcb_pfc = pfc_disabled;
4418         }
4419
4420         /* Initialize default user to priority mapping, UPx->TC0 */
4421         tc = &adapter->dcb_cfg.tc_config[0];
4422         tc->path[DCB_TX_CONFIG].up_to_tc_bitmap = 0xFF;
4423         tc->path[DCB_RX_CONFIG].up_to_tc_bitmap = 0xFF;
4424
4425         adapter->dcb_cfg.bw_percentage[DCB_TX_CONFIG][0] = 100;
4426         adapter->dcb_cfg.bw_percentage[DCB_RX_CONFIG][0] = 100;
4427         adapter->dcb_cfg.pfc_mode_enable = false;
4428         adapter->dcb_set_bitmap = 0x00;
4429         adapter->dcbx_cap = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_CEE;
4430         ixgbe_copy_dcb_cfg(&adapter->dcb_cfg, &adapter->temp_dcb_cfg,
4431                            MAX_TRAFFIC_CLASS);
4432
4433 #endif
4434
4435         /* default flow control settings */
4436         hw->fc.requested_mode = ixgbe_fc_full;
4437         hw->fc.current_mode = ixgbe_fc_full;    /* init for ethtool output */
4438 #ifdef CONFIG_DCB
4439         adapter->last_lfc_mode = hw->fc.current_mode;
4440 #endif
4441         ixgbe_pbthresh_setup(adapter);
4442         hw->fc.pause_time = IXGBE_DEFAULT_FCPAUSE;
4443         hw->fc.send_xon = true;
4444         hw->fc.disable_fc_autoneg = false;
4445
4446         /* enable itr by default in dynamic mode */
4447         adapter->rx_itr_setting = 1;
4448         adapter->tx_itr_setting = 1;
4449
4450         /* set default ring sizes */
4451         adapter->tx_ring_count = IXGBE_DEFAULT_TXD;
4452         adapter->rx_ring_count = IXGBE_DEFAULT_RXD;
4453
4454         /* set default work limits */
4455         adapter->tx_work_limit = IXGBE_DEFAULT_TX_WORK;
4456
4457         /* initialize eeprom parameters */
4458         if (ixgbe_init_eeprom_params_generic(hw)) {
4459                 e_dev_err("EEPROM initialization failed\n");
4460                 return -EIO;
4461         }
4462
4463         set_bit(__IXGBE_DOWN, &adapter->state);
4464
4465         return 0;
4466 }
4467
4468 /**
4469  * ixgbe_setup_tx_resources - allocate Tx resources (Descriptors)
4470  * @tx_ring:    tx descriptor ring (for a specific queue) to setup
4471  *
4472  * Return 0 on success, negative on failure
4473  **/
4474 int ixgbe_setup_tx_resources(struct ixgbe_ring *tx_ring)
4475 {
4476         struct device *dev = tx_ring->dev;
4477         int orig_node = dev_to_node(dev);
4478         int numa_node = -1;
4479         int size;
4480
4481         size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
4482
4483         if (tx_ring->q_vector)
4484                 numa_node = tx_ring->q_vector->numa_node;
4485
4486         tx_ring->tx_buffer_info = vzalloc_node(size, numa_node);
4487         if (!tx_ring->tx_buffer_info)
4488                 tx_ring->tx_buffer_info = vzalloc(size);
4489         if (!tx_ring->tx_buffer_info)
4490                 goto err;
4491
4492         /* round up to nearest 4K */
4493         tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
4494         tx_ring->size = ALIGN(tx_ring->size, 4096);
4495
4496         set_dev_node(dev, numa_node);
4497         tx_ring->desc = dma_alloc_coherent(dev,
4498                                            tx_ring->size,
4499                                            &tx_ring->dma,
4500                                            GFP_KERNEL);
4501         set_dev_node(dev, orig_node);
4502         if (!tx_ring->desc)
4503                 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
4504                                                    &tx_ring->dma, GFP_KERNEL);
4505         if (!tx_ring->desc)
4506                 goto err;
4507
4508         tx_ring->next_to_use = 0;
4509         tx_ring->next_to_clean = 0;
4510         return 0;
4511
4512 err:
4513         vfree(tx_ring->tx_buffer_info);
4514         tx_ring->tx_buffer_info = NULL;
4515         dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
4516         return -ENOMEM;
4517 }
4518
4519 /**
4520  * ixgbe_setup_all_tx_resources - allocate all queues Tx resources
4521  * @adapter: board private structure
4522  *
4523  * If this function returns with an error, then it's possible one or
4524  * more of the rings is populated (while the rest are not).  It is the
4525  * callers duty to clean those orphaned rings.
4526  *
4527  * Return 0 on success, negative on failure
4528  **/
4529 static int ixgbe_setup_all_tx_resources(struct ixgbe_adapter *adapter)
4530 {
4531         int i, err = 0;
4532
4533         for (i = 0; i < adapter->num_tx_queues; i++) {
4534                 err = ixgbe_setup_tx_resources(adapter->tx_ring[i]);
4535                 if (!err)
4536                         continue;
4537                 e_err(probe, "Allocation for Tx Queue %u failed\n", i);
4538                 break;
4539         }
4540
4541         return err;
4542 }
4543
4544 /**
4545  * ixgbe_setup_rx_resources - allocate Rx resources (Descriptors)
4546  * @rx_ring:    rx descriptor ring (for a specific queue) to setup
4547  *
4548  * Returns 0 on success, negative on failure
4549  **/
4550 int ixgbe_setup_rx_resources(struct ixgbe_ring *rx_ring)
4551 {
4552         struct device *dev = rx_ring->dev;
4553         int orig_node = dev_to_node(dev);
4554         int numa_node = -1;
4555         int size;
4556
4557         size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
4558
4559         if (rx_ring->q_vector)
4560                 numa_node = rx_ring->q_vector->numa_node;
4561
4562         rx_ring->rx_buffer_info = vzalloc_node(size, numa_node);
4563         if (!rx_ring->rx_buffer_info)
4564                 rx_ring->rx_buffer_info = vzalloc(size);
4565         if (!rx_ring->rx_buffer_info)
4566                 goto err;
4567
4568         /* Round up to nearest 4K */
4569         rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
4570         rx_ring->size = ALIGN(rx_ring->size, 4096);
4571
4572         set_dev_node(dev, numa_node);
4573         rx_ring->desc = dma_alloc_coherent(dev,
4574                                            rx_ring->size,
4575                                            &rx_ring->dma,
4576                                            GFP_KERNEL);
4577         set_dev_node(dev, orig_node);
4578         if (!rx_ring->desc)
4579                 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
4580                                                    &rx_ring->dma, GFP_KERNEL);
4581         if (!rx_ring->desc)
4582                 goto err;
4583
4584         rx_ring->next_to_clean = 0;
4585         rx_ring->next_to_use = 0;
4586
4587         ixgbe_init_rx_page_offset(rx_ring);
4588
4589         return 0;
4590 err:
4591         vfree(rx_ring->rx_buffer_info);
4592         rx_ring->rx_buffer_info = NULL;
4593         dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
4594         return -ENOMEM;
4595 }
4596
4597 /**
4598  * ixgbe_setup_all_rx_resources - allocate all queues Rx resources
4599  * @adapter: board private structure
4600  *
4601  * If this function returns with an error, then it's possible one or
4602  * more of the rings is populated (while the rest are not).  It is the
4603  * callers duty to clean those orphaned rings.
4604  *
4605  * Return 0 on success, negative on failure
4606  **/
4607 static int ixgbe_setup_all_rx_resources(struct ixgbe_adapter *adapter)
4608 {
4609         int i, err = 0;
4610
4611         for (i = 0; i < adapter->num_rx_queues; i++) {
4612                 err = ixgbe_setup_rx_resources(adapter->rx_ring[i]);
4613                 if (!err)
4614                         continue;
4615                 e_err(probe, "Allocation for Rx Queue %u failed\n", i);
4616                 break;
4617         }
4618
4619         return err;
4620 }
4621
4622 /**
4623  * ixgbe_free_tx_resources - Free Tx Resources per Queue
4624  * @tx_ring: Tx descriptor ring for a specific queue
4625  *
4626  * Free all transmit software resources
4627  **/
4628 void ixgbe_free_tx_resources(struct ixgbe_ring *tx_ring)
4629 {
4630         ixgbe_clean_tx_ring(tx_ring);
4631
4632         vfree(tx_ring->tx_buffer_info);
4633         tx_ring->tx_buffer_info = NULL;
4634
4635         /* if not set, then don't free */
4636         if (!tx_ring->desc)
4637                 return;
4638
4639         dma_free_coherent(tx_ring->dev, tx_ring->size,
4640                           tx_ring->desc, tx_ring->dma);
4641
4642         tx_ring->desc = NULL;
4643 }
4644
4645 /**
4646  * ixgbe_free_all_tx_resources - Free Tx Resources for All Queues
4647  * @adapter: board private structure
4648  *
4649  * Free all transmit software resources
4650  **/
4651 static void ixgbe_free_all_tx_resources(struct ixgbe_adapter *adapter)
4652 {
4653         int i;
4654
4655         for (i = 0; i < adapter->num_tx_queues; i++)
4656                 if (adapter->tx_ring[i]->desc)
4657                         ixgbe_free_tx_resources(adapter->tx_ring[i]);
4658 }
4659
4660 /**
4661  * ixgbe_free_rx_resources - Free Rx Resources
4662  * @rx_ring: ring to clean the resources from
4663  *
4664  * Free all receive software resources
4665  **/
4666 void ixgbe_free_rx_resources(struct ixgbe_ring *rx_ring)
4667 {
4668         ixgbe_clean_rx_ring(rx_ring);
4669
4670         vfree(rx_ring->rx_buffer_info);
4671         rx_ring->rx_buffer_info = NULL;
4672
4673         /* if not set, then don't free */
4674         if (!rx_ring->desc)
4675                 return;
4676
4677         dma_free_coherent(rx_ring->dev, rx_ring->size,
4678                           rx_ring->desc, rx_ring->dma);
4679
4680         rx_ring->desc = NULL;
4681 }
4682
4683 /**
4684  * ixgbe_free_all_rx_resources - Free Rx Resources for All Queues
4685  * @adapter: board private structure
4686  *
4687  * Free all receive software resources
4688  **/
4689 static void ixgbe_free_all_rx_resources(struct ixgbe_adapter *adapter)
4690 {
4691         int i;
4692
4693         for (i = 0; i < adapter->num_rx_queues; i++)
4694                 if (adapter->rx_ring[i]->desc)
4695                         ixgbe_free_rx_resources(adapter->rx_ring[i]);
4696 }
4697
4698 /**
4699  * ixgbe_change_mtu - Change the Maximum Transfer Unit
4700  * @netdev: network interface device structure
4701  * @new_mtu: new value for maximum frame size
4702  *
4703  * Returns 0 on success, negative on failure
4704  **/
4705 static int ixgbe_change_mtu(struct net_device *netdev, int new_mtu)
4706 {
4707         struct ixgbe_adapter *adapter = netdev_priv(netdev);
4708         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
4709
4710         /* MTU < 68 is an error and causes problems on some kernels */
4711         if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE))
4712                 return -EINVAL;
4713
4714         /*
4715          * For 82599EB we cannot allow PF to change MTU greater than 1500
4716          * in SR-IOV mode as it may cause buffer overruns in guest VFs that
4717          * don't allocate and chain buffers correctly.
4718          */
4719         if ((adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) &&
4720             (adapter->hw.mac.type == ixgbe_mac_82599EB) &&
4721             (max_frame > MAXIMUM_ETHERNET_VLAN_SIZE))
4722                         return -EINVAL;
4723
4724         e_info(probe, "changing MTU from %d to %d\n", netdev->mtu, new_mtu);
4725
4726         /* must set new MTU before calling down or up */
4727         netdev->mtu = new_mtu;
4728
4729         if (netif_running(netdev))
4730                 ixgbe_reinit_locked(adapter);
4731
4732         return 0;
4733 }
4734
4735 /**
4736  * ixgbe_open - Called when a network interface is made active
4737  * @netdev: network interface device structure
4738  *
4739  * Returns 0 on success, negative value on failure
4740  *
4741  * The open entry point is called when a network interface is made
4742  * active by the system (IFF_UP).  At this point all resources needed
4743  * for transmit and receive operations are allocated, the interrupt
4744  * handler is registered with the OS, the watchdog timer is started,
4745  * and the stack is notified that the interface is ready.
4746  **/
4747 static int ixgbe_open(struct net_device *netdev)
4748 {
4749         struct ixgbe_adapter *adapter = netdev_priv(netdev);
4750         int err;
4751
4752         /* disallow open during test */
4753         if (test_bit(__IXGBE_TESTING, &adapter->state))
4754                 return -EBUSY;
4755
4756         netif_carrier_off(netdev);
4757
4758         /* allocate transmit descriptors */
4759         err = ixgbe_setup_all_tx_resources(adapter);
4760         if (err)
4761                 goto err_setup_tx;
4762
4763         /* allocate receive descriptors */
4764         err = ixgbe_setup_all_rx_resources(adapter);
4765         if (err)
4766                 goto err_setup_rx;
4767
4768         ixgbe_configure(adapter);
4769
4770         err = ixgbe_request_irq(adapter);
4771         if (err)
4772                 goto err_req_irq;
4773
4774         ixgbe_up_complete(adapter);
4775
4776         return 0;
4777
4778 err_req_irq:
4779 err_setup_rx:
4780         ixgbe_free_all_rx_resources(adapter);
4781 err_setup_tx:
4782         ixgbe_free_all_tx_resources(adapter);
4783         ixgbe_reset(adapter);
4784
4785         return err;
4786 }
4787
4788 /**
4789  * ixgbe_close - Disables a network interface
4790  * @netdev: network interface device structure
4791  *
4792  * Returns 0, this is not allowed to fail
4793  *
4794  * The close entry point is called when an interface is de-activated
4795  * by the OS.  The hardware is still under the drivers control, but
4796  * needs to be disabled.  A global MAC reset is issued to stop the
4797  * hardware, and all transmit and receive resources are freed.
4798  **/
4799 static int ixgbe_close(struct net_device *netdev)
4800 {
4801         struct ixgbe_adapter *adapter = netdev_priv(netdev);
4802
4803         ixgbe_down(adapter);
4804         ixgbe_free_irq(adapter);
4805
4806         ixgbe_fdir_filter_exit(adapter);
4807
4808         ixgbe_free_all_tx_resources(adapter);
4809         ixgbe_free_all_rx_resources(adapter);
4810
4811         ixgbe_release_hw_control(adapter);
4812
4813         return 0;
4814 }
4815
4816 #ifdef CONFIG_PM
4817 static int ixgbe_resume(struct pci_dev *pdev)
4818 {
4819         struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
4820         struct net_device *netdev = adapter->netdev;
4821         u32 err;
4822
4823         pci_set_power_state(pdev, PCI_D0);
4824         pci_restore_state(pdev);
4825         /*
4826          * pci_restore_state clears dev->state_saved so call
4827          * pci_save_state to restore it.
4828          */
4829         pci_save_state(pdev);
4830
4831         err = pci_enable_device_mem(pdev);
4832         if (err) {
4833                 e_dev_err("Cannot enable PCI device from suspend\n");
4834                 return err;
4835         }
4836         pci_set_master(pdev);
4837
4838         pci_wake_from_d3(pdev, false);
4839
4840         err = ixgbe_init_interrupt_scheme(adapter);
4841         if (err) {
4842                 e_dev_err("Cannot initialize interrupts for device\n");
4843                 return err;
4844         }
4845
4846         ixgbe_reset(adapter);
4847
4848         IXGBE_WRITE_REG(&adapter->hw, IXGBE_WUS, ~0);
4849
4850         if (netif_running(netdev)) {
4851                 err = ixgbe_open(netdev);
4852                 if (err)
4853                         return err;
4854         }
4855
4856         netif_device_attach(netdev);
4857
4858         return 0;
4859 }
4860 #endif /* CONFIG_PM */
4861
4862 static int __ixgbe_shutdown(struct pci_dev *pdev, bool *enable_wake)
4863 {
4864         struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
4865         struct net_device *netdev = adapter->netdev;
4866         struct ixgbe_hw *hw = &adapter->hw;
4867         u32 ctrl, fctrl;
4868         u32 wufc = adapter->wol;
4869 #ifdef CONFIG_PM
4870         int retval = 0;
4871 #endif
4872
4873         netif_device_detach(netdev);
4874
4875         if (netif_running(netdev)) {
4876                 ixgbe_down(adapter);
4877                 ixgbe_free_irq(adapter);
4878                 ixgbe_free_all_tx_resources(adapter);
4879                 ixgbe_free_all_rx_resources(adapter);
4880         }
4881
4882         ixgbe_clear_interrupt_scheme(adapter);
4883 #ifdef CONFIG_DCB
4884         kfree(adapter->ixgbe_ieee_pfc);
4885         kfree(adapter->ixgbe_ieee_ets);
4886 #endif
4887
4888 #ifdef CONFIG_PM
4889         retval = pci_save_state(pdev);
4890         if (retval)
4891                 return retval;
4892
4893 #endif
4894         if (wufc) {
4895                 ixgbe_set_rx_mode(netdev);
4896
4897                 /* turn on all-multi mode if wake on multicast is enabled */
4898                 if (wufc & IXGBE_WUFC_MC) {
4899                         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
4900                         fctrl |= IXGBE_FCTRL_MPE;
4901                         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
4902                 }
4903
4904                 ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
4905                 ctrl |= IXGBE_CTRL_GIO_DIS;
4906                 IXGBE_WRITE_REG(hw, IXGBE_CTRL, ctrl);
4907
4908                 IXGBE_WRITE_REG(hw, IXGBE_WUFC, wufc);
4909         } else {
4910                 IXGBE_WRITE_REG(hw, IXGBE_WUC, 0);
4911                 IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
4912         }
4913
4914         switch (hw->mac.type) {
4915         case ixgbe_mac_82598EB:
4916                 pci_wake_from_d3(pdev, false);
4917                 break;
4918         case ixgbe_mac_82599EB:
4919         case ixgbe_mac_X540:
4920                 pci_wake_from_d3(pdev, !!wufc);
4921                 break;
4922         default:
4923                 break;
4924         }
4925
4926         *enable_wake = !!wufc;
4927
4928         ixgbe_release_hw_control(adapter);
4929
4930         pci_disable_device(pdev);
4931
4932         return 0;
4933 }
4934
4935 #ifdef CONFIG_PM
4936 static int ixgbe_suspend(struct pci_dev *pdev, pm_message_t state)
4937 {
4938         int retval;
4939         bool wake;
4940
4941         retval = __ixgbe_shutdown(pdev, &wake);
4942         if (retval)
4943                 return retval;
4944
4945         if (wake) {
4946                 pci_prepare_to_sleep(pdev);
4947         } else {
4948                 pci_wake_from_d3(pdev, false);
4949                 pci_set_power_state(pdev, PCI_D3hot);
4950         }
4951
4952         return 0;
4953 }
4954 #endif /* CONFIG_PM */
4955
4956 static void ixgbe_shutdown(struct pci_dev *pdev)
4957 {
4958         bool wake;
4959
4960         __ixgbe_shutdown(pdev, &wake);
4961
4962         if (system_state == SYSTEM_POWER_OFF) {
4963                 pci_wake_from_d3(pdev, wake);
4964                 pci_set_power_state(pdev, PCI_D3hot);
4965         }
4966 }
4967
4968 /**
4969  * ixgbe_update_stats - Update the board statistics counters.
4970  * @adapter: board private structure
4971  **/
4972 void ixgbe_update_stats(struct ixgbe_adapter *adapter)
4973 {
4974         struct net_device *netdev = adapter->netdev;
4975         struct ixgbe_hw *hw = &adapter->hw;
4976         struct ixgbe_hw_stats *hwstats = &adapter->stats;
4977         u64 total_mpc = 0;
4978         u32 i, missed_rx = 0, mpc, bprc, lxon, lxoff, xon_off_tot;
4979         u64 non_eop_descs = 0, restart_queue = 0, tx_busy = 0;
4980         u64 alloc_rx_page_failed = 0, alloc_rx_buff_failed = 0;
4981         u64 bytes = 0, packets = 0, hw_csum_rx_error = 0;
4982 #ifdef IXGBE_FCOE
4983         struct ixgbe_fcoe *fcoe = &adapter->fcoe;
4984         unsigned int cpu;
4985         u64 fcoe_noddp_counts_sum = 0, fcoe_noddp_ext_buff_counts_sum = 0;
4986 #endif /* IXGBE_FCOE */
4987
4988         if (test_bit(__IXGBE_DOWN, &adapter->state) ||
4989             test_bit(__IXGBE_RESETTING, &adapter->state))
4990                 return;
4991
4992         if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED) {
4993                 u64 rsc_count = 0;
4994                 u64 rsc_flush = 0;
4995                 for (i = 0; i < 16; i++)
4996                         adapter->hw_rx_no_dma_resources +=
4997                                 IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
4998                 for (i = 0; i < adapter->num_rx_queues; i++) {
4999                         rsc_count += adapter->rx_ring[i]->rx_stats.rsc_count;
5000                         rsc_flush += adapter->rx_ring[i]->rx_stats.rsc_flush;
5001                 }
5002                 adapter->rsc_total_count = rsc_count;
5003                 adapter->rsc_total_flush = rsc_flush;
5004         }
5005
5006         for (i = 0; i < adapter->num_rx_queues; i++) {
5007                 struct ixgbe_ring *rx_ring = adapter->rx_ring[i];
5008                 non_eop_descs += rx_ring->rx_stats.non_eop_descs;
5009                 alloc_rx_page_failed += rx_ring->rx_stats.alloc_rx_page_failed;
5010                 alloc_rx_buff_failed += rx_ring->rx_stats.alloc_rx_buff_failed;
5011                 hw_csum_rx_error += rx_ring->rx_stats.csum_err;
5012                 bytes += rx_ring->stats.bytes;
5013                 packets += rx_ring->stats.packets;
5014         }
5015         adapter->non_eop_descs = non_eop_descs;
5016         adapter->alloc_rx_page_failed = alloc_rx_page_failed;
5017         adapter->alloc_rx_buff_failed = alloc_rx_buff_failed;
5018         adapter->hw_csum_rx_error = hw_csum_rx_error;
5019         netdev->stats.rx_bytes = bytes;
5020         netdev->stats.rx_packets = packets;
5021
5022         bytes = 0;
5023         packets = 0;
5024         /* gather some stats to the adapter struct that are per queue */
5025         for (i = 0; i < adapter->num_tx_queues; i++) {
5026                 struct ixgbe_ring *tx_ring = adapter->tx_ring[i];
5027                 restart_queue += tx_ring->tx_stats.restart_queue;
5028                 tx_busy += tx_ring->tx_stats.tx_busy;
5029                 bytes += tx_ring->stats.bytes;
5030                 packets += tx_ring->stats.packets;
5031         }
5032         adapter->restart_queue = restart_queue;
5033         adapter->tx_busy = tx_busy;
5034         netdev->stats.tx_bytes = bytes;
5035         netdev->stats.tx_packets = packets;
5036
5037         hwstats->crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
5038
5039         /* 8 register reads */
5040         for (i = 0; i < 8; i++) {
5041                 /* for packet buffers not used, the register should read 0 */
5042                 mpc = IXGBE_READ_REG(hw, IXGBE_MPC(i));
5043                 missed_rx += mpc;
5044                 hwstats->mpc[i] += mpc;
5045                 total_mpc += hwstats->mpc[i];
5046                 hwstats->pxontxc[i] += IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
5047                 hwstats->pxofftxc[i] += IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
5048                 switch (hw->mac.type) {
5049                 case ixgbe_mac_82598EB:
5050                         hwstats->rnbc[i] += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
5051                         hwstats->qbtc[i] += IXGBE_READ_REG(hw, IXGBE_QBTC(i));
5052                         hwstats->qbrc[i] += IXGBE_READ_REG(hw, IXGBE_QBRC(i));
5053                         hwstats->pxonrxc[i] +=
5054                                 IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
5055                         break;
5056                 case ixgbe_mac_82599EB:
5057                 case ixgbe_mac_X540:
5058                         hwstats->pxonrxc[i] +=
5059                                 IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
5060                         break;
5061                 default:
5062                         break;
5063                 }
5064         }
5065
5066         /*16 register reads */
5067         for (i = 0; i < 16; i++) {
5068                 hwstats->qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
5069                 hwstats->qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
5070                 if ((hw->mac.type == ixgbe_mac_82599EB) ||
5071                     (hw->mac.type == ixgbe_mac_X540)) {
5072                         hwstats->qbtc[i] += IXGBE_READ_REG(hw, IXGBE_QBTC_L(i));
5073                         IXGBE_READ_REG(hw, IXGBE_QBTC_H(i)); /* to clear */
5074                         hwstats->qbrc[i] += IXGBE_READ_REG(hw, IXGBE_QBRC_L(i));
5075                         IXGBE_READ_REG(hw, IXGBE_QBRC_H(i)); /* to clear */
5076                 }
5077         }
5078
5079         hwstats->gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
5080         /* work around hardware counting issue */
5081         hwstats->gprc -= missed_rx;
5082
5083         ixgbe_update_xoff_received(adapter);
5084
5085         /* 82598 hardware only has a 32 bit counter in the high register */
5086         switch (hw->mac.type) {
5087         case ixgbe_mac_82598EB:
5088                 hwstats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
5089                 hwstats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
5090                 hwstats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
5091                 hwstats->tor += IXGBE_READ_REG(hw, IXGBE_TORH);
5092                 break;
5093         case ixgbe_mac_X540:
5094                 /* OS2BMC stats are X540 only*/
5095                 hwstats->o2bgptc += IXGBE_READ_REG(hw, IXGBE_O2BGPTC);
5096                 hwstats->o2bspc += IXGBE_READ_REG(hw, IXGBE_O2BSPC);
5097                 hwstats->b2ospc += IXGBE_READ_REG(hw, IXGBE_B2OSPC);
5098                 hwstats->b2ogprc += IXGBE_READ_REG(hw, IXGBE_B2OGPRC);
5099         case ixgbe_mac_82599EB:
5100                 hwstats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCL);
5101                 IXGBE_READ_REG(hw, IXGBE_GORCH); /* to clear */
5102                 hwstats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCL);
5103                 IXGBE_READ_REG(hw, IXGBE_GOTCH); /* to clear */
5104                 hwstats->tor += IXGBE_READ_REG(hw, IXGBE_TORL);
5105                 IXGBE_READ_REG(hw, IXGBE_TORH); /* to clear */
5106                 hwstats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
5107                 hwstats->fdirmatch += IXGBE_READ_REG(hw, IXGBE_FDIRMATCH);
5108                 hwstats->fdirmiss += IXGBE_READ_REG(hw, IXGBE_FDIRMISS);
5109 #ifdef IXGBE_FCOE
5110                 hwstats->fccrc += IXGBE_READ_REG(hw, IXGBE_FCCRC);
5111                 hwstats->fcoerpdc += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
5112                 hwstats->fcoeprc += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
5113                 hwstats->fcoeptc += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
5114                 hwstats->fcoedwrc += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
5115                 hwstats->fcoedwtc += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
5116                 /* Add up per cpu counters for total ddp aloc fail */
5117                 if (fcoe->pcpu_noddp && fcoe->pcpu_noddp_ext_buff) {
5118                         for_each_possible_cpu(cpu) {
5119                                 fcoe_noddp_counts_sum +=
5120                                         *per_cpu_ptr(fcoe->pcpu_noddp, cpu);
5121                                 fcoe_noddp_ext_buff_counts_sum +=
5122                                         *per_cpu_ptr(fcoe->
5123                                                 pcpu_noddp_ext_buff, cpu);
5124                         }
5125                 }
5126                 hwstats->fcoe_noddp = fcoe_noddp_counts_sum;
5127                 hwstats->fcoe_noddp_ext_buff = fcoe_noddp_ext_buff_counts_sum;
5128 #endif /* IXGBE_FCOE */
5129                 break;
5130         default:
5131                 break;
5132         }
5133         bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
5134         hwstats->bprc += bprc;
5135         hwstats->mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
5136         if (hw->mac.type == ixgbe_mac_82598EB)
5137                 hwstats->mprc -= bprc;
5138         hwstats->roc += IXGBE_READ_REG(hw, IXGBE_ROC);
5139         hwstats->prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
5140         hwstats->prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
5141         hwstats->prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
5142         hwstats->prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
5143         hwstats->prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
5144         hwstats->prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
5145         hwstats->rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
5146         lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
5147         hwstats->lxontxc += lxon;
5148         lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
5149         hwstats->lxofftxc += lxoff;
5150         hwstats->gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
5151         hwstats->mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
5152         /*
5153          * 82598 errata - tx of flow control packets is included in tx counters
5154          */
5155         xon_off_tot = lxon + lxoff;
5156         hwstats->gptc -= xon_off_tot;
5157         hwstats->mptc -= xon_off_tot;
5158         hwstats->gotc -= (xon_off_tot * (ETH_ZLEN + ETH_FCS_LEN));
5159         hwstats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
5160         hwstats->rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
5161         hwstats->rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
5162         hwstats->tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
5163         hwstats->ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
5164         hwstats->ptc64 -= xon_off_tot;
5165         hwstats->ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
5166         hwstats->ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
5167         hwstats->ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
5168         hwstats->ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
5169         hwstats->ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
5170         hwstats->bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
5171
5172         /* Fill out the OS statistics structure */
5173         netdev->stats.multicast = hwstats->mprc;
5174
5175         /* Rx Errors */
5176         netdev->stats.rx_errors = hwstats->crcerrs + hwstats->rlec;
5177         netdev->stats.rx_dropped = 0;
5178         netdev->stats.rx_length_errors = hwstats->rlec;
5179         netdev->stats.rx_crc_errors = hwstats->crcerrs;
5180         netdev->stats.rx_missed_errors = total_mpc;
5181 }
5182
5183 /**
5184  * ixgbe_fdir_reinit_subtask - worker thread to reinit FDIR filter table
5185  * @adapter - pointer to the device adapter structure
5186  **/
5187 static void ixgbe_fdir_reinit_subtask(struct ixgbe_adapter *adapter)
5188 {
5189         struct ixgbe_hw *hw = &adapter->hw;
5190         int i;
5191
5192         if (!(adapter->flags2 & IXGBE_FLAG2_FDIR_REQUIRES_REINIT))
5193                 return;
5194
5195         adapter->flags2 &= ~IXGBE_FLAG2_FDIR_REQUIRES_REINIT;
5196
5197         /* if interface is down do nothing */
5198         if (test_bit(__IXGBE_DOWN, &adapter->state))
5199                 return;
5200
5201         /* do nothing if we are not using signature filters */
5202         if (!(adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE))
5203                 return;
5204
5205         adapter->fdir_overflow++;
5206
5207         if (ixgbe_reinit_fdir_tables_82599(hw) == 0) {
5208                 for (i = 0; i < adapter->num_tx_queues; i++)
5209                         set_bit(__IXGBE_TX_FDIR_INIT_DONE,
5210                                 &(adapter->tx_ring[i]->state));
5211                 /* re-enable flow director interrupts */
5212                 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_FLOW_DIR);
5213         } else {
5214                 e_err(probe, "failed to finish FDIR re-initialization, "
5215                       "ignored adding FDIR ATR filters\n");
5216         }
5217 }
5218
5219 /**
5220  * ixgbe_check_hang_subtask - check for hung queues and dropped interrupts
5221  * @adapter - pointer to the device adapter structure
5222  *
5223  * This function serves two purposes.  First it strobes the interrupt lines
5224  * in order to make certain interrupts are occurring.  Secondly it sets the
5225  * bits needed to check for TX hangs.  As a result we should immediately
5226  * determine if a hang has occurred.
5227  */
5228 static void ixgbe_check_hang_subtask(struct ixgbe_adapter *adapter)
5229 {
5230         struct ixgbe_hw *hw = &adapter->hw;
5231         u64 eics = 0;
5232         int i;
5233
5234         /* If we're down or resetting, just bail */
5235         if (test_bit(__IXGBE_DOWN, &adapter->state) ||
5236             test_bit(__IXGBE_RESETTING, &adapter->state))
5237                 return;
5238
5239         /* Force detection of hung controller */
5240         if (netif_carrier_ok(adapter->netdev)) {
5241                 for (i = 0; i < adapter->num_tx_queues; i++)
5242                         set_check_for_tx_hang(adapter->tx_ring[i]);
5243         }
5244
5245         if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
5246                 /*
5247                  * for legacy and MSI interrupts don't set any bits
5248                  * that are enabled for EIAM, because this operation
5249                  * would set *both* EIMS and EICS for any bit in EIAM
5250                  */
5251                 IXGBE_WRITE_REG(hw, IXGBE_EICS,
5252                         (IXGBE_EICS_TCP_TIMER | IXGBE_EICS_OTHER));
5253         } else {
5254                 /* get one bit for every active tx/rx interrupt vector */
5255                 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
5256                         struct ixgbe_q_vector *qv = adapter->q_vector[i];
5257                         if (qv->rx.ring || qv->tx.ring)
5258                                 eics |= ((u64)1 << i);
5259                 }
5260         }
5261
5262         /* Cause software interrupt to ensure rings are cleaned */
5263         ixgbe_irq_rearm_queues(adapter, eics);
5264
5265 }
5266
5267 /**
5268  * ixgbe_watchdog_update_link - update the link status
5269  * @adapter - pointer to the device adapter structure
5270  * @link_speed - pointer to a u32 to store the link_speed
5271  **/
5272 static void ixgbe_watchdog_update_link(struct ixgbe_adapter *adapter)
5273 {
5274         struct ixgbe_hw *hw = &adapter->hw;
5275         u32 link_speed = adapter->link_speed;
5276         bool link_up = adapter->link_up;
5277         int i;
5278
5279         if (!(adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE))
5280                 return;
5281
5282         if (hw->mac.ops.check_link) {
5283                 hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
5284         } else {
5285                 /* always assume link is up, if no check link function */
5286                 link_speed = IXGBE_LINK_SPEED_10GB_FULL;
5287                 link_up = true;
5288         }
5289         if (link_up) {
5290                 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
5291                         for (i = 0; i < MAX_TRAFFIC_CLASS; i++)
5292                                 hw->mac.ops.fc_enable(hw, i);
5293                 } else {
5294                         hw->mac.ops.fc_enable(hw, 0);
5295                 }
5296         }
5297
5298         if (link_up ||
5299             time_after(jiffies, (adapter->link_check_timeout +
5300                                  IXGBE_TRY_LINK_TIMEOUT))) {
5301                 adapter->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
5302                 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMC_LSC);
5303                 IXGBE_WRITE_FLUSH(hw);
5304         }
5305
5306         adapter->link_up = link_up;
5307         adapter->link_speed = link_speed;
5308 }
5309
5310 /**
5311  * ixgbe_watchdog_link_is_up - update netif_carrier status and
5312  *                             print link up message
5313  * @adapter - pointer to the device adapter structure
5314  **/
5315 static void ixgbe_watchdog_link_is_up(struct ixgbe_adapter *adapter)
5316 {
5317         struct net_device *netdev = adapter->netdev;
5318         struct ixgbe_hw *hw = &adapter->hw;
5319         u32 link_speed = adapter->link_speed;
5320         bool flow_rx, flow_tx;
5321
5322         /* only continue if link was previously down */
5323         if (netif_carrier_ok(netdev))
5324                 return;
5325
5326         adapter->flags2 &= ~IXGBE_FLAG2_SEARCH_FOR_SFP;
5327
5328         switch (hw->mac.type) {
5329         case ixgbe_mac_82598EB: {
5330                 u32 frctl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
5331                 u32 rmcs = IXGBE_READ_REG(hw, IXGBE_RMCS);
5332                 flow_rx = !!(frctl & IXGBE_FCTRL_RFCE);
5333                 flow_tx = !!(rmcs & IXGBE_RMCS_TFCE_802_3X);
5334         }
5335                 break;
5336         case ixgbe_mac_X540:
5337         case ixgbe_mac_82599EB: {
5338                 u32 mflcn = IXGBE_READ_REG(hw, IXGBE_MFLCN);
5339                 u32 fccfg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
5340                 flow_rx = !!(mflcn & IXGBE_MFLCN_RFCE);
5341                 flow_tx = !!(fccfg & IXGBE_FCCFG_TFCE_802_3X);
5342         }
5343                 break;
5344         default:
5345                 flow_tx = false;
5346                 flow_rx = false;
5347                 break;
5348         }
5349         e_info(drv, "NIC Link is Up %s, Flow Control: %s\n",
5350                (link_speed == IXGBE_LINK_SPEED_10GB_FULL ?
5351                "10 Gbps" :
5352                (link_speed == IXGBE_LINK_SPEED_1GB_FULL ?
5353                "1 Gbps" :
5354                (link_speed == IXGBE_LINK_SPEED_100_FULL ?
5355                "100 Mbps" :
5356                "unknown speed"))),
5357                ((flow_rx && flow_tx) ? "RX/TX" :
5358                (flow_rx ? "RX" :
5359                (flow_tx ? "TX" : "None"))));
5360
5361         netif_carrier_on(netdev);
5362         ixgbe_check_vf_rate_limit(adapter);
5363 }
5364
5365 /**
5366  * ixgbe_watchdog_link_is_down - update netif_carrier status and
5367  *                               print link down message
5368  * @adapter - pointer to the adapter structure
5369  **/
5370 static void ixgbe_watchdog_link_is_down(struct ixgbe_adapter *adapter)
5371 {
5372         struct net_device *netdev = adapter->netdev;
5373         struct ixgbe_hw *hw = &adapter->hw;
5374
5375         adapter->link_up = false;
5376         adapter->link_speed = 0;
5377
5378         /* only continue if link was up previously */
5379         if (!netif_carrier_ok(netdev))
5380                 return;
5381
5382         /* poll for SFP+ cable when link is down */
5383         if (ixgbe_is_sfp(hw) && hw->mac.type == ixgbe_mac_82598EB)
5384                 adapter->flags2 |= IXGBE_FLAG2_SEARCH_FOR_SFP;
5385
5386         e_info(drv, "NIC Link is Down\n");
5387         netif_carrier_off(netdev);
5388 }
5389
5390 /**
5391  * ixgbe_watchdog_flush_tx - flush queues on link down
5392  * @adapter - pointer to the device adapter structure
5393  **/
5394 static void ixgbe_watchdog_flush_tx(struct ixgbe_adapter *adapter)
5395 {
5396         int i;
5397         int some_tx_pending = 0;
5398
5399         if (!netif_carrier_ok(adapter->netdev)) {
5400                 for (i = 0; i < adapter->num_tx_queues; i++) {
5401                         struct ixgbe_ring *tx_ring = adapter->tx_ring[i];
5402                         if (tx_ring->next_to_use != tx_ring->next_to_clean) {
5403                                 some_tx_pending = 1;
5404                                 break;
5405                         }
5406                 }
5407
5408                 if (some_tx_pending) {
5409                         /* We've lost link, so the controller stops DMA,
5410                          * but we've got queued Tx work that's never going
5411                          * to get done, so reset controller to flush Tx.
5412                          * (Do the reset outside of interrupt context).
5413                          */
5414                         adapter->flags2 |= IXGBE_FLAG2_RESET_REQUESTED;
5415                 }
5416         }
5417 }
5418
5419 static void ixgbe_spoof_check(struct ixgbe_adapter *adapter)
5420 {
5421         u32 ssvpc;
5422
5423         /* Do not perform spoof check for 82598 */
5424         if (adapter->hw.mac.type == ixgbe_mac_82598EB)
5425                 return;
5426
5427         ssvpc = IXGBE_READ_REG(&adapter->hw, IXGBE_SSVPC);
5428
5429         /*
5430          * ssvpc register is cleared on read, if zero then no
5431          * spoofed packets in the last interval.
5432          */
5433         if (!ssvpc)
5434                 return;
5435
5436         e_warn(drv, "%d Spoofed packets detected\n", ssvpc);
5437 }
5438
5439 /**
5440  * ixgbe_watchdog_subtask - check and bring link up
5441  * @adapter - pointer to the device adapter structure
5442  **/
5443 static void ixgbe_watchdog_subtask(struct ixgbe_adapter *adapter)
5444 {
5445         /* if interface is down do nothing */
5446         if (test_bit(__IXGBE_DOWN, &adapter->state) ||
5447             test_bit(__IXGBE_RESETTING, &adapter->state))
5448                 return;
5449
5450         ixgbe_watchdog_update_link(adapter);
5451
5452         if (adapter->link_up)
5453                 ixgbe_watchdog_link_is_up(adapter);
5454         else
5455                 ixgbe_watchdog_link_is_down(adapter);
5456
5457         ixgbe_spoof_check(adapter);
5458         ixgbe_update_stats(adapter);
5459
5460         ixgbe_watchdog_flush_tx(adapter);
5461 }
5462
5463 /**
5464  * ixgbe_sfp_detection_subtask - poll for SFP+ cable
5465  * @adapter - the ixgbe adapter structure
5466  **/
5467 static void ixgbe_sfp_detection_subtask(struct ixgbe_adapter *adapter)
5468 {
5469         struct ixgbe_hw *hw = &adapter->hw;
5470         s32 err;
5471
5472         /* not searching for SFP so there is nothing to do here */
5473         if (!(adapter->flags2 & IXGBE_FLAG2_SEARCH_FOR_SFP) &&
5474             !(adapter->flags2 & IXGBE_FLAG2_SFP_NEEDS_RESET))
5475                 return;
5476
5477         /* someone else is in init, wait until next service event */
5478         if (test_and_set_bit(__IXGBE_IN_SFP_INIT, &adapter->state))
5479                 return;
5480
5481         err = hw->phy.ops.identify_sfp(hw);
5482         if (err == IXGBE_ERR_SFP_NOT_SUPPORTED)
5483                 goto sfp_out;
5484
5485         if (err == IXGBE_ERR_SFP_NOT_PRESENT) {
5486                 /* If no cable is present, then we need to reset
5487                  * the next time we find a good cable. */
5488                 adapter->flags2 |= IXGBE_FLAG2_SFP_NEEDS_RESET;
5489         }
5490
5491         /* exit on error */
5492         if (err)
5493                 goto sfp_out;
5494
5495         /* exit if reset not needed */
5496         if (!(adapter->flags2 & IXGBE_FLAG2_SFP_NEEDS_RESET))
5497                 goto sfp_out;
5498
5499         adapter->flags2 &= ~IXGBE_FLAG2_SFP_NEEDS_RESET;
5500
5501         /*
5502          * A module may be identified correctly, but the EEPROM may not have
5503          * support for that module.  setup_sfp() will fail in that case, so
5504          * we should not allow that module to load.
5505          */
5506         if (hw->mac.type == ixgbe_mac_82598EB)
5507                 err = hw->phy.ops.reset(hw);
5508         else
5509                 err = hw->mac.ops.setup_sfp(hw);
5510
5511         if (err == IXGBE_ERR_SFP_NOT_SUPPORTED)
5512                 goto sfp_out;
5513
5514         adapter->flags |= IXGBE_FLAG_NEED_LINK_CONFIG;
5515         e_info(probe, "detected SFP+: %d\n", hw->phy.sfp_type);
5516
5517 sfp_out:
5518         clear_bit(__IXGBE_IN_SFP_INIT, &adapter->state);
5519
5520         if ((err == IXGBE_ERR_SFP_NOT_SUPPORTED) &&
5521             (adapter->netdev->reg_state == NETREG_REGISTERED)) {
5522                 e_dev_err("failed to initialize because an unsupported "
5523                           "SFP+ module type was detected.\n");
5524                 e_dev_err("Reload the driver after installing a "
5525                           "supported module.\n");
5526                 unregister_netdev(adapter->netdev);
5527         }
5528 }
5529
5530 /**
5531  * ixgbe_sfp_link_config_subtask - set up link SFP after module install
5532  * @adapter - the ixgbe adapter structure
5533  **/
5534 static void ixgbe_sfp_link_config_subtask(struct ixgbe_adapter *adapter)
5535 {
5536         struct ixgbe_hw *hw = &adapter->hw;
5537         u32 autoneg;
5538         bool negotiation;
5539
5540         if (!(adapter->flags & IXGBE_FLAG_NEED_LINK_CONFIG))
5541                 return;
5542
5543         /* someone else is in init, wait until next service event */
5544         if (test_and_set_bit(__IXGBE_IN_SFP_INIT, &adapter->state))
5545                 return;
5546
5547         adapter->flags &= ~IXGBE_FLAG_NEED_LINK_CONFIG;
5548
5549         autoneg = hw->phy.autoneg_advertised;
5550         if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
5551                 hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiation);
5552         if (hw->mac.ops.setup_link)
5553                 hw->mac.ops.setup_link(hw, autoneg, negotiation, true);
5554
5555         adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
5556         adapter->link_check_timeout = jiffies;
5557         clear_bit(__IXGBE_IN_SFP_INIT, &adapter->state);
5558 }
5559
5560 #ifdef CONFIG_PCI_IOV
5561 static void ixgbe_check_for_bad_vf(struct ixgbe_adapter *adapter)
5562 {
5563         int vf;
5564         struct ixgbe_hw *hw = &adapter->hw;
5565         struct net_device *netdev = adapter->netdev;
5566         u32 gpc;
5567         u32 ciaa, ciad;
5568
5569         gpc = IXGBE_READ_REG(hw, IXGBE_TXDGPC);
5570         if (gpc) /* If incrementing then no need for the check below */
5571                 return;
5572         /*
5573          * Check to see if a bad DMA write target from an errant or
5574          * malicious VF has caused a PCIe error.  If so then we can
5575          * issue a VFLR to the offending VF(s) and then resume without
5576          * requesting a full slot reset.
5577          */
5578
5579         for (vf = 0; vf < adapter->num_vfs; vf++) {
5580                 ciaa = (vf << 16) | 0x80000000;
5581                 /* 32 bit read so align, we really want status at offset 6 */
5582                 ciaa |= PCI_COMMAND;
5583                 IXGBE_WRITE_REG(hw, IXGBE_CIAA_82599, ciaa);
5584                 ciad = IXGBE_READ_REG(hw, IXGBE_CIAD_82599);
5585                 ciaa &= 0x7FFFFFFF;
5586                 /* disable debug mode asap after reading data */
5587                 IXGBE_WRITE_REG(hw, IXGBE_CIAA_82599, ciaa);
5588                 /* Get the upper 16 bits which will be the PCI status reg */
5589                 ciad >>= 16;
5590                 if (ciad & PCI_STATUS_REC_MASTER_ABORT) {
5591                         netdev_err(netdev, "VF %d Hung DMA\n", vf);
5592                         /* Issue VFLR */
5593                         ciaa = (vf << 16) | 0x80000000;
5594                         ciaa |= 0xA8;
5595                         IXGBE_WRITE_REG(hw, IXGBE_CIAA_82599, ciaa);
5596                         ciad = 0x00008000;  /* VFLR */
5597                         IXGBE_WRITE_REG(hw, IXGBE_CIAD_82599, ciad);
5598                         ciaa &= 0x7FFFFFFF;
5599                         IXGBE_WRITE_REG(hw, IXGBE_CIAA_82599, ciaa);
5600                 }
5601         }
5602 }
5603
5604 #endif
5605 /**
5606  * ixgbe_service_timer - Timer Call-back
5607  * @data: pointer to adapter cast into an unsigned long
5608  **/
5609 static void ixgbe_service_timer(unsigned long data)
5610 {
5611         struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
5612         unsigned long next_event_offset;
5613         bool ready = true;
5614
5615         /* poll faster when waiting for link */
5616         if (adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE)
5617                 next_event_offset = HZ / 10;
5618         else
5619                 next_event_offset = HZ * 2;
5620
5621 #ifdef CONFIG_PCI_IOV
5622         /*
5623          * don't bother with SR-IOV VF DMA hang check if there are
5624          * no VFs or the link is down
5625          */
5626         if (!adapter->num_vfs ||
5627             (adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE))
5628                 goto normal_timer_service;
5629
5630         /* If we have VFs allocated then we must check for DMA hangs */
5631         ixgbe_check_for_bad_vf(adapter);
5632         next_event_offset = HZ / 50;
5633         adapter->timer_event_accumulator++;
5634
5635         if (adapter->timer_event_accumulator >= 100)
5636                 adapter->timer_event_accumulator = 0;
5637         else
5638                 ready = false;
5639
5640 normal_timer_service:
5641 #endif
5642         /* Reset the timer */
5643         mod_timer(&adapter->service_timer, next_event_offset + jiffies);
5644
5645         if (ready)
5646                 ixgbe_service_event_schedule(adapter);
5647 }
5648
5649 static void ixgbe_reset_subtask(struct ixgbe_adapter *adapter)
5650 {
5651         if (!(adapter->flags2 & IXGBE_FLAG2_RESET_REQUESTED))
5652                 return;
5653
5654         adapter->flags2 &= ~IXGBE_FLAG2_RESET_REQUESTED;
5655
5656         /* If we're already down or resetting, just bail */
5657         if (test_bit(__IXGBE_DOWN, &adapter->state) ||
5658             test_bit(__IXGBE_RESETTING, &adapter->state))
5659                 return;
5660
5661         ixgbe_dump(adapter);
5662         netdev_err(adapter->netdev, "Reset adapter\n");
5663         adapter->tx_timeout_count++;
5664
5665         ixgbe_reinit_locked(adapter);
5666 }
5667
5668 /**
5669  * ixgbe_service_task - manages and runs subtasks
5670  * @work: pointer to work_struct containing our data
5671  **/
5672 static void ixgbe_service_task(struct work_struct *work)
5673 {
5674         struct ixgbe_adapter *adapter = container_of(work,
5675                                                      struct ixgbe_adapter,
5676                                                      service_task);
5677
5678         ixgbe_reset_subtask(adapter);
5679         ixgbe_sfp_detection_subtask(adapter);
5680         ixgbe_sfp_link_config_subtask(adapter);
5681         ixgbe_check_overtemp_subtask(adapter);
5682         ixgbe_watchdog_subtask(adapter);
5683         ixgbe_fdir_reinit_subtask(adapter);
5684         ixgbe_check_hang_subtask(adapter);
5685
5686         ixgbe_service_event_complete(adapter);
5687 }
5688
5689 static int ixgbe_tso(struct ixgbe_ring *tx_ring,
5690                      struct ixgbe_tx_buffer *first,
5691                      u8 *hdr_len)
5692 {
5693         struct sk_buff *skb = first->skb;
5694         u32 vlan_macip_lens, type_tucmd;
5695         u32 mss_l4len_idx, l4len;
5696
5697         if (!skb_is_gso(skb))
5698                 return 0;
5699
5700         if (skb_header_cloned(skb)) {
5701                 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5702                 if (err)
5703                         return err;
5704         }
5705
5706         /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
5707         type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
5708
5709         if (first->protocol == __constant_htons(ETH_P_IP)) {
5710                 struct iphdr *iph = ip_hdr(skb);
5711                 iph->tot_len = 0;
5712                 iph->check = 0;
5713                 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5714                                                          iph->daddr, 0,
5715                                                          IPPROTO_TCP,
5716                                                          0);
5717                 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
5718                 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
5719                                    IXGBE_TX_FLAGS_CSUM |
5720                                    IXGBE_TX_FLAGS_IPV4;
5721         } else if (skb_is_gso_v6(skb)) {
5722                 ipv6_hdr(skb)->payload_len = 0;
5723                 tcp_hdr(skb)->check =
5724                     ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5725                                      &ipv6_hdr(skb)->daddr,
5726                                      0, IPPROTO_TCP, 0);
5727                 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
5728                                    IXGBE_TX_FLAGS_CSUM;
5729         }
5730
5731         /* compute header lengths */
5732         l4len = tcp_hdrlen(skb);
5733         *hdr_len = skb_transport_offset(skb) + l4len;
5734
5735         /* update gso size and bytecount with header size */
5736         first->gso_segs = skb_shinfo(skb)->gso_segs;
5737         first->bytecount += (first->gso_segs - 1) * *hdr_len;
5738
5739         /* mss_l4len_id: use 1 as index for TSO */
5740         mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
5741         mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
5742         mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
5743
5744         /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
5745         vlan_macip_lens = skb_network_header_len(skb);
5746         vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
5747         vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
5748
5749         ixgbe_tx_ctxtdesc(tx_ring, vlan_macip_lens, 0, type_tucmd,
5750                           mss_l4len_idx);
5751
5752         return 1;
5753 }
5754
5755 static void ixgbe_tx_csum(struct ixgbe_ring *tx_ring,
5756                           struct ixgbe_tx_buffer *first)
5757 {
5758         struct sk_buff *skb = first->skb;
5759         u32 vlan_macip_lens = 0;
5760         u32 mss_l4len_idx = 0;
5761         u32 type_tucmd = 0;
5762
5763         if (skb->ip_summed != CHECKSUM_PARTIAL) {
5764                 if (!(first->tx_flags & IXGBE_TX_FLAGS_HW_VLAN) &&
5765                     !(first->tx_flags & IXGBE_TX_FLAGS_TXSW))
5766                         return;
5767         } else {
5768                 u8 l4_hdr = 0;
5769                 switch (first->protocol) {
5770                 case __constant_htons(ETH_P_IP):
5771                         vlan_macip_lens |= skb_network_header_len(skb);
5772                         type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
5773                         l4_hdr = ip_hdr(skb)->protocol;
5774                         break;
5775                 case __constant_htons(ETH_P_IPV6):
5776                         vlan_macip_lens |= skb_network_header_len(skb);
5777                         l4_hdr = ipv6_hdr(skb)->nexthdr;
5778                         break;
5779                 default:
5780                         if (unlikely(net_ratelimit())) {
5781                                 dev_warn(tx_ring->dev,
5782                                  "partial checksum but proto=%x!\n",
5783                                  first->protocol);
5784                         }
5785                         break;
5786                 }
5787
5788                 switch (l4_hdr) {
5789                 case IPPROTO_TCP:
5790                         type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
5791                         mss_l4len_idx = tcp_hdrlen(skb) <<
5792                                         IXGBE_ADVTXD_L4LEN_SHIFT;
5793                         break;
5794                 case IPPROTO_SCTP:
5795                         type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
5796                         mss_l4len_idx = sizeof(struct sctphdr) <<
5797                                         IXGBE_ADVTXD_L4LEN_SHIFT;
5798                         break;
5799                 case IPPROTO_UDP:
5800                         mss_l4len_idx = sizeof(struct udphdr) <<
5801                                         IXGBE_ADVTXD_L4LEN_SHIFT;
5802                         break;
5803                 default:
5804                         if (unlikely(net_ratelimit())) {
5805                                 dev_warn(tx_ring->dev,
5806                                  "partial checksum but l4 proto=%x!\n",
5807                                  l4_hdr);
5808                         }
5809                         break;
5810                 }
5811
5812                 /* update TX checksum flag */
5813                 first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
5814         }
5815
5816         /* vlan_macip_lens: MACLEN, VLAN tag */
5817         vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
5818         vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
5819
5820         ixgbe_tx_ctxtdesc(tx_ring, vlan_macip_lens, 0,
5821                           type_tucmd, mss_l4len_idx);
5822 }
5823
5824 static __le32 ixgbe_tx_cmd_type(u32 tx_flags)
5825 {
5826         /* set type for advanced descriptor with frame checksum insertion */
5827         __le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
5828                                       IXGBE_ADVTXD_DCMD_IFCS |
5829                                       IXGBE_ADVTXD_DCMD_DEXT);
5830
5831         /* set HW vlan bit if vlan is present */
5832         if (tx_flags & IXGBE_TX_FLAGS_HW_VLAN)
5833                 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
5834
5835         /* set segmentation enable bits for TSO/FSO */
5836 #ifdef IXGBE_FCOE
5837         if (tx_flags & (IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_FSO))
5838 #else
5839         if (tx_flags & IXGBE_TX_FLAGS_TSO)
5840 #endif
5841                 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
5842
5843         return cmd_type;
5844 }
5845
5846 static void ixgbe_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
5847                                    u32 tx_flags, unsigned int paylen)
5848 {
5849         __le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
5850
5851         /* enable L4 checksum for TSO and TX checksum offload */
5852         if (tx_flags & IXGBE_TX_FLAGS_CSUM)
5853                 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
5854
5855         /* enble IPv4 checksum for TSO */
5856         if (tx_flags & IXGBE_TX_FLAGS_IPV4)
5857                 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
5858
5859         /* use index 1 context for TSO/FSO/FCOE */
5860 #ifdef IXGBE_FCOE
5861         if (tx_flags & (IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_FCOE))
5862 #else
5863         if (tx_flags & IXGBE_TX_FLAGS_TSO)
5864 #endif
5865                 olinfo_status |= cpu_to_le32(1 << IXGBE_ADVTXD_IDX_SHIFT);
5866
5867         /*
5868          * Check Context must be set if Tx switch is enabled, which it
5869          * always is for case where virtual functions are running
5870          */
5871 #ifdef IXGBE_FCOE
5872         if (tx_flags & (IXGBE_TX_FLAGS_TXSW | IXGBE_TX_FLAGS_FCOE))
5873 #else
5874         if (tx_flags & IXGBE_TX_FLAGS_TXSW)
5875 #endif
5876                 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
5877
5878         tx_desc->read.olinfo_status = olinfo_status;
5879 }
5880
5881 #define IXGBE_TXD_CMD (IXGBE_TXD_CMD_EOP | \
5882                        IXGBE_TXD_CMD_RS)
5883
5884 static void ixgbe_tx_map(struct ixgbe_ring *tx_ring,
5885                          struct ixgbe_tx_buffer *first,
5886                          const u8 hdr_len)
5887 {
5888         dma_addr_t dma;
5889         struct sk_buff *skb = first->skb;
5890         struct ixgbe_tx_buffer *tx_buffer;
5891         union ixgbe_adv_tx_desc *tx_desc;
5892         struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
5893         unsigned int data_len = skb->data_len;
5894         unsigned int size = skb_headlen(skb);
5895         unsigned int paylen = skb->len - hdr_len;
5896         u32 tx_flags = first->tx_flags;
5897         __le32 cmd_type;
5898         u16 i = tx_ring->next_to_use;
5899
5900         tx_desc = IXGBE_TX_DESC(tx_ring, i);
5901
5902         ixgbe_tx_olinfo_status(tx_desc, tx_flags, paylen);
5903         cmd_type = ixgbe_tx_cmd_type(tx_flags);
5904
5905 #ifdef IXGBE_FCOE
5906         if (tx_flags & IXGBE_TX_FLAGS_FCOE) {
5907                 if (data_len < sizeof(struct fcoe_crc_eof)) {
5908                         size -= sizeof(struct fcoe_crc_eof) - data_len;
5909                         data_len = 0;
5910                 } else {
5911                         data_len -= sizeof(struct fcoe_crc_eof);
5912                 }
5913         }
5914
5915 #endif
5916         dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
5917         if (dma_mapping_error(tx_ring->dev, dma))
5918                 goto dma_error;
5919
5920         /* record length, and DMA address */
5921         dma_unmap_len_set(first, len, size);
5922         dma_unmap_addr_set(first, dma, dma);
5923
5924         tx_desc->read.buffer_addr = cpu_to_le64(dma);
5925
5926         for (;;) {
5927                 while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
5928                         tx_desc->read.cmd_type_len =
5929                                 cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
5930
5931                         i++;
5932                         tx_desc++;
5933                         if (i == tx_ring->count) {
5934                                 tx_desc = IXGBE_TX_DESC(tx_ring, 0);
5935                                 i = 0;
5936                         }
5937
5938                         dma += IXGBE_MAX_DATA_PER_TXD;
5939                         size -= IXGBE_MAX_DATA_PER_TXD;
5940
5941                         tx_desc->read.buffer_addr = cpu_to_le64(dma);
5942                         tx_desc->read.olinfo_status = 0;
5943                 }
5944
5945                 if (likely(!data_len))
5946                         break;
5947
5948                 if (unlikely(skb->no_fcs))
5949                         cmd_type &= ~(cpu_to_le32(IXGBE_ADVTXD_DCMD_IFCS));
5950                 tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
5951
5952                 i++;
5953                 tx_desc++;
5954                 if (i == tx_ring->count) {
5955                         tx_desc = IXGBE_TX_DESC(tx_ring, 0);
5956                         i = 0;
5957                 }
5958
5959 #ifdef IXGBE_FCOE
5960                 size = min_t(unsigned int, data_len, skb_frag_size(frag));
5961 #else
5962                 size = skb_frag_size(frag);
5963 #endif
5964                 data_len -= size;
5965
5966                 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
5967                                        DMA_TO_DEVICE);
5968                 if (dma_mapping_error(tx_ring->dev, dma))
5969                         goto dma_error;
5970
5971                 tx_buffer = &tx_ring->tx_buffer_info[i];
5972                 dma_unmap_len_set(tx_buffer, len, size);
5973                 dma_unmap_addr_set(tx_buffer, dma, dma);
5974
5975                 tx_desc->read.buffer_addr = cpu_to_le64(dma);
5976                 tx_desc->read.olinfo_status = 0;
5977
5978                 frag++;
5979         }
5980
5981         /* write last descriptor with RS and EOP bits */
5982         cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
5983         tx_desc->read.cmd_type_len = cmd_type;
5984
5985         netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
5986
5987         /* set the timestamp */
5988         first->time_stamp = jiffies;
5989
5990         /*
5991          * Force memory writes to complete before letting h/w know there
5992          * are new descriptors to fetch.  (Only applicable for weak-ordered
5993          * memory model archs, such as IA-64).
5994          *
5995          * We also need this memory barrier to make certain all of the
5996          * status bits have been updated before next_to_watch is written.
5997          */
5998         wmb();
5999
6000         /* set next_to_watch value indicating a packet is present */
6001         first->next_to_watch = tx_desc;
6002
6003         i++;
6004         if (i == tx_ring->count)
6005                 i = 0;
6006
6007         tx_ring->next_to_use = i;
6008
6009         /* notify HW of packet */
6010         writel(i, tx_ring->tail);
6011
6012         return;
6013 dma_error:
6014         dev_err(tx_ring->dev, "TX DMA map failed\n");
6015
6016         /* clear dma mappings for failed tx_buffer_info map */
6017         for (;;) {
6018                 tx_buffer = &tx_ring->tx_buffer_info[i];
6019                 ixgbe_unmap_and_free_tx_resource(tx_ring, tx_buffer);
6020                 if (tx_buffer == first)
6021                         break;
6022                 if (i == 0)
6023                         i = tx_ring->count;
6024                 i--;
6025         }
6026
6027         tx_ring->next_to_use = i;
6028 }
6029
6030 static void ixgbe_atr(struct ixgbe_ring *ring,
6031                       struct ixgbe_tx_buffer *first)
6032 {
6033         struct ixgbe_q_vector *q_vector = ring->q_vector;
6034         union ixgbe_atr_hash_dword input = { .dword = 0 };
6035         union ixgbe_atr_hash_dword common = { .dword = 0 };
6036         union {
6037                 unsigned char *network;
6038                 struct iphdr *ipv4;
6039                 struct ipv6hdr *ipv6;
6040         } hdr;
6041         struct tcphdr *th;
6042         __be16 vlan_id;
6043
6044         /* if ring doesn't have a interrupt vector, cannot perform ATR */
6045         if (!q_vector)
6046                 return;
6047
6048         /* do nothing if sampling is disabled */
6049         if (!ring->atr_sample_rate)
6050                 return;
6051
6052         ring->atr_count++;
6053
6054         /* snag network header to get L4 type and address */
6055         hdr.network = skb_network_header(first->skb);
6056
6057         /* Currently only IPv4/IPv6 with TCP is supported */
6058         if ((first->protocol != __constant_htons(ETH_P_IPV6) ||
6059              hdr.ipv6->nexthdr != IPPROTO_TCP) &&
6060             (first->protocol != __constant_htons(ETH_P_IP) ||
6061              hdr.ipv4->protocol != IPPROTO_TCP))
6062                 return;
6063
6064         th = tcp_hdr(first->skb);
6065
6066         /* skip this packet since it is invalid or the socket is closing */
6067         if (!th || th->fin)
6068                 return;
6069
6070         /* sample on all syn packets or once every atr sample count */
6071         if (!th->syn && (ring->atr_count < ring->atr_sample_rate))
6072                 return;
6073
6074         /* reset sample count */
6075         ring->atr_count = 0;
6076
6077         vlan_id = htons(first->tx_flags >> IXGBE_TX_FLAGS_VLAN_SHIFT);
6078
6079         /*
6080          * src and dst are inverted, think how the receiver sees them
6081          *
6082          * The input is broken into two sections, a non-compressed section
6083          * containing vm_pool, vlan_id, and flow_type.  The rest of the data
6084          * is XORed together and stored in the compressed dword.
6085          */
6086         input.formatted.vlan_id = vlan_id;
6087
6088         /*
6089          * since src port and flex bytes occupy the same word XOR them together
6090          * and write the value to source port portion of compressed dword
6091          */
6092         if (first->tx_flags & (IXGBE_TX_FLAGS_SW_VLAN | IXGBE_TX_FLAGS_HW_VLAN))
6093                 common.port.src ^= th->dest ^ __constant_htons(ETH_P_8021Q);
6094         else
6095                 common.port.src ^= th->dest ^ first->protocol;
6096         common.port.dst ^= th->source;
6097
6098         if (first->protocol == __constant_htons(ETH_P_IP)) {
6099                 input.formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV4;
6100                 common.ip ^= hdr.ipv4->saddr ^ hdr.ipv4->daddr;
6101         } else {
6102                 input.formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV6;
6103                 common.ip ^= hdr.ipv6->saddr.s6_addr32[0] ^
6104                              hdr.ipv6->saddr.s6_addr32[1] ^
6105                              hdr.ipv6->saddr.s6_addr32[2] ^
6106                              hdr.ipv6->saddr.s6_addr32[3] ^
6107                              hdr.ipv6->daddr.s6_addr32[0] ^
6108                              hdr.ipv6->daddr.s6_addr32[1] ^
6109                              hdr.ipv6->daddr.s6_addr32[2] ^
6110                              hdr.ipv6->daddr.s6_addr32[3];
6111         }
6112
6113         /* This assumes the Rx queue and Tx queue are bound to the same CPU */
6114         ixgbe_fdir_add_signature_filter_82599(&q_vector->adapter->hw,
6115                                               input, common, ring->queue_index);
6116 }
6117
6118 static int __ixgbe_maybe_stop_tx(struct ixgbe_ring *tx_ring, u16 size)
6119 {
6120         netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
6121         /* Herbert's original patch had:
6122          *  smp_mb__after_netif_stop_queue();
6123          * but since that doesn't exist yet, just open code it. */
6124         smp_mb();
6125
6126         /* We need to check again in a case another CPU has just
6127          * made room available. */
6128         if (likely(ixgbe_desc_unused(tx_ring) < size))
6129                 return -EBUSY;
6130
6131         /* A reprieve! - use start_queue because it doesn't call schedule */
6132         netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
6133         ++tx_ring->tx_stats.restart_queue;
6134         return 0;
6135 }
6136
6137 static inline int ixgbe_maybe_stop_tx(struct ixgbe_ring *tx_ring, u16 size)
6138 {
6139         if (likely(ixgbe_desc_unused(tx_ring) >= size))
6140                 return 0;
6141         return __ixgbe_maybe_stop_tx(tx_ring, size);
6142 }
6143
6144 static u16 ixgbe_select_queue(struct net_device *dev, struct sk_buff *skb)
6145 {
6146         struct ixgbe_adapter *adapter = netdev_priv(dev);
6147         int txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) :
6148                                                smp_processor_id();
6149 #ifdef IXGBE_FCOE
6150         __be16 protocol = vlan_get_protocol(skb);
6151
6152         if (((protocol == htons(ETH_P_FCOE)) ||
6153             (protocol == htons(ETH_P_FIP))) &&
6154             (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)) {
6155                 txq &= (adapter->ring_feature[RING_F_FCOE].indices - 1);
6156                 txq += adapter->ring_feature[RING_F_FCOE].mask;
6157                 return txq;
6158         }
6159 #endif
6160
6161         if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
6162                 while (unlikely(txq >= dev->real_num_tx_queues))
6163                         txq -= dev->real_num_tx_queues;
6164                 return txq;
6165         }
6166
6167         return skb_tx_hash(dev, skb);
6168 }
6169
6170 netdev_tx_t ixgbe_xmit_frame_ring(struct sk_buff *skb,
6171                           struct ixgbe_adapter *adapter,
6172                           struct ixgbe_ring *tx_ring)
6173 {
6174         struct ixgbe_tx_buffer *first;
6175         int tso;
6176         u32 tx_flags = 0;
6177 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
6178         unsigned short f;
6179 #endif
6180         u16 count = TXD_USE_COUNT(skb_headlen(skb));
6181         __be16 protocol = skb->protocol;
6182         u8 hdr_len = 0;
6183
6184         /*
6185          * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
6186          *       + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
6187          *       + 2 desc gap to keep tail from touching head,
6188          *       + 1 desc for context descriptor,
6189          * otherwise try next time
6190          */
6191 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
6192         for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
6193                 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
6194 #else
6195         count += skb_shinfo(skb)->nr_frags;
6196 #endif
6197         if (ixgbe_maybe_stop_tx(tx_ring, count + 3)) {
6198                 tx_ring->tx_stats.tx_busy++;
6199                 return NETDEV_TX_BUSY;
6200         }
6201
6202         /* record the location of the first descriptor for this packet */
6203         first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
6204         first->skb = skb;
6205         first->bytecount = skb->len;
6206         first->gso_segs = 1;
6207
6208         /* if we have a HW VLAN tag being added default to the HW one */
6209         if (vlan_tx_tag_present(skb)) {
6210                 tx_flags |= vlan_tx_tag_get(skb) << IXGBE_TX_FLAGS_VLAN_SHIFT;
6211                 tx_flags |= IXGBE_TX_FLAGS_HW_VLAN;
6212         /* else if it is a SW VLAN check the next protocol and store the tag */
6213         } else if (protocol == __constant_htons(ETH_P_8021Q)) {
6214                 struct vlan_hdr *vhdr, _vhdr;
6215                 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
6216                 if (!vhdr)
6217                         goto out_drop;
6218
6219                 protocol = vhdr->h_vlan_encapsulated_proto;
6220                 tx_flags |= ntohs(vhdr->h_vlan_TCI) <<
6221                                   IXGBE_TX_FLAGS_VLAN_SHIFT;
6222                 tx_flags |= IXGBE_TX_FLAGS_SW_VLAN;
6223         }
6224
6225 #ifdef CONFIG_PCI_IOV
6226         /*
6227          * Use the l2switch_enable flag - would be false if the DMA
6228          * Tx switch had been disabled.
6229          */
6230         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
6231                 tx_flags |= IXGBE_TX_FLAGS_TXSW;
6232
6233 #endif
6234         /* DCB maps skb priorities 0-7 onto 3 bit PCP of VLAN tag. */
6235         if ((adapter->flags & IXGBE_FLAG_DCB_ENABLED) &&
6236             ((tx_flags & (IXGBE_TX_FLAGS_HW_VLAN | IXGBE_TX_FLAGS_SW_VLAN)) ||
6237              (skb->priority != TC_PRIO_CONTROL))) {
6238                 tx_flags &= ~IXGBE_TX_FLAGS_VLAN_PRIO_MASK;
6239                 tx_flags |= (skb->priority & 0x7) <<
6240                                         IXGBE_TX_FLAGS_VLAN_PRIO_SHIFT;
6241                 if (tx_flags & IXGBE_TX_FLAGS_SW_VLAN) {
6242                         struct vlan_ethhdr *vhdr;
6243                         if (skb_header_cloned(skb) &&
6244                             pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6245                                 goto out_drop;
6246                         vhdr = (struct vlan_ethhdr *)skb->data;
6247                         vhdr->h_vlan_TCI = htons(tx_flags >>
6248                                                  IXGBE_TX_FLAGS_VLAN_SHIFT);
6249                 } else {
6250                         tx_flags |= IXGBE_TX_FLAGS_HW_VLAN;
6251                 }
6252         }
6253
6254         /* record initial flags and protocol */
6255         first->tx_flags = tx_flags;
6256         first->protocol = protocol;
6257
6258 #ifdef IXGBE_FCOE
6259         /* setup tx offload for FCoE */
6260         if ((protocol == __constant_htons(ETH_P_FCOE)) &&
6261             (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)) {
6262                 tso = ixgbe_fso(tx_ring, first, &hdr_len);
6263                 if (tso < 0)
6264                         goto out_drop;
6265
6266                 goto xmit_fcoe;
6267         }
6268
6269 #endif /* IXGBE_FCOE */
6270         tso = ixgbe_tso(tx_ring, first, &hdr_len);
6271         if (tso < 0)
6272                 goto out_drop;
6273         else if (!tso)
6274                 ixgbe_tx_csum(tx_ring, first);
6275
6276         /* add the ATR filter if ATR is on */
6277         if (test_bit(__IXGBE_TX_FDIR_INIT_DONE, &tx_ring->state))
6278                 ixgbe_atr(tx_ring, first);
6279
6280 #ifdef IXGBE_FCOE
6281 xmit_fcoe:
6282 #endif /* IXGBE_FCOE */
6283         ixgbe_tx_map(tx_ring, first, hdr_len);
6284
6285         ixgbe_maybe_stop_tx(tx_ring, DESC_NEEDED);
6286
6287         return NETDEV_TX_OK;
6288
6289 out_drop:
6290         dev_kfree_skb_any(first->skb);
6291         first->skb = NULL;
6292
6293         return NETDEV_TX_OK;
6294 }
6295
6296 static netdev_tx_t ixgbe_xmit_frame(struct sk_buff *skb,
6297                                     struct net_device *netdev)
6298 {
6299         struct ixgbe_adapter *adapter = netdev_priv(netdev);
6300         struct ixgbe_ring *tx_ring;
6301
6302         if (skb->len <= 0) {
6303                 dev_kfree_skb_any(skb);
6304                 return NETDEV_TX_OK;
6305         }
6306
6307         /*
6308          * The minimum packet size for olinfo paylen is 17 so pad the skb
6309          * in order to meet this minimum size requirement.
6310          */
6311         if (skb->len < 17) {
6312                 if (skb_padto(skb, 17))
6313                         return NETDEV_TX_OK;
6314                 skb->len = 17;
6315         }
6316
6317         tx_ring = adapter->tx_ring[skb->queue_mapping];
6318         return ixgbe_xmit_frame_ring(skb, adapter, tx_ring);
6319 }
6320
6321 /**
6322  * ixgbe_set_mac - Change the Ethernet Address of the NIC
6323  * @netdev: network interface device structure
6324  * @p: pointer to an address structure
6325  *
6326  * Returns 0 on success, negative on failure
6327  **/
6328 static int ixgbe_set_mac(struct net_device *netdev, void *p)
6329 {
6330         struct ixgbe_adapter *adapter = netdev_priv(netdev);
6331         struct ixgbe_hw *hw = &adapter->hw;
6332         struct sockaddr *addr = p;
6333
6334         if (!is_valid_ether_addr(addr->sa_data))
6335                 return -EADDRNOTAVAIL;
6336
6337         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
6338         memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
6339
6340         hw->mac.ops.set_rar(hw, 0, hw->mac.addr, adapter->num_vfs,
6341                             IXGBE_RAH_AV);
6342
6343         return 0;
6344 }
6345
6346 static int
6347 ixgbe_mdio_read(struct net_device *netdev, int prtad, int devad, u16 addr)
6348 {
6349         struct ixgbe_adapter *adapter = netdev_priv(netdev);
6350         struct ixgbe_hw *hw = &adapter->hw;
6351         u16 value;
6352         int rc;
6353
6354         if (prtad != hw->phy.mdio.prtad)
6355                 return -EINVAL;
6356         rc = hw->phy.ops.read_reg(hw, addr, devad, &value);
6357         if (!rc)
6358                 rc = value;
6359         return rc;
6360 }
6361
6362 static int ixgbe_mdio_write(struct net_device *netdev, int prtad, int devad,
6363                             u16 addr, u16 value)
6364 {
6365         struct ixgbe_adapter *adapter = netdev_priv(netdev);
6366         struct ixgbe_hw *hw = &adapter->hw;
6367
6368         if (prtad != hw->phy.mdio.prtad)
6369                 return -EINVAL;
6370         return hw->phy.ops.write_reg(hw, addr, devad, value);
6371 }
6372
6373 static int ixgbe_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
6374 {
6375         struct ixgbe_adapter *adapter = netdev_priv(netdev);
6376
6377         return mdio_mii_ioctl(&adapter->hw.phy.mdio, if_mii(req), cmd);
6378 }
6379
6380 /**
6381  * ixgbe_add_sanmac_netdev - Add the SAN MAC address to the corresponding
6382  * netdev->dev_addrs
6383  * @netdev: network interface device structure
6384  *
6385  * Returns non-zero on failure
6386  **/
6387 static int ixgbe_add_sanmac_netdev(struct net_device *dev)
6388 {
6389         int err = 0;
6390         struct ixgbe_adapter *adapter = netdev_priv(dev);
6391         struct ixgbe_mac_info *mac = &adapter->hw.mac;
6392
6393         if (is_valid_ether_addr(mac->san_addr)) {
6394                 rtnl_lock();
6395                 err = dev_addr_add(dev, mac->san_addr, NETDEV_HW_ADDR_T_SAN);
6396                 rtnl_unlock();
6397         }
6398         return err;
6399 }
6400
6401 /**
6402  * ixgbe_del_sanmac_netdev - Removes the SAN MAC address to the corresponding
6403  * netdev->dev_addrs
6404  * @netdev: network interface device structure
6405  *
6406  * Returns non-zero on failure
6407  **/
6408 static int ixgbe_del_sanmac_netdev(struct net_device *dev)
6409 {
6410         int err = 0;
6411         struct ixgbe_adapter *adapter = netdev_priv(dev);
6412         struct ixgbe_mac_info *mac = &adapter->hw.mac;
6413
6414         if (is_valid_ether_addr(mac->san_addr)) {
6415                 rtnl_lock();
6416                 err = dev_addr_del(dev, mac->san_addr, NETDEV_HW_ADDR_T_SAN);
6417                 rtnl_unlock();
6418         }
6419         return err;
6420 }
6421
6422 #ifdef CONFIG_NET_POLL_CONTROLLER
6423 /*
6424  * Polling 'interrupt' - used by things like netconsole to send skbs
6425  * without having to re-enable interrupts. It's not called while
6426  * the interrupt routine is executing.
6427  */
6428 static void ixgbe_netpoll(struct net_device *netdev)
6429 {
6430         struct ixgbe_adapter *adapter = netdev_priv(netdev);
6431         int i;
6432
6433         /* if interface is down do nothing */
6434         if (test_bit(__IXGBE_DOWN, &adapter->state))
6435                 return;
6436
6437         adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
6438         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
6439                 int num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
6440                 for (i = 0; i < num_q_vectors; i++) {
6441                         struct ixgbe_q_vector *q_vector = adapter->q_vector[i];
6442                         ixgbe_msix_clean_rings(0, q_vector);
6443                 }
6444         } else {
6445                 ixgbe_intr(adapter->pdev->irq, netdev);
6446         }
6447         adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
6448 }
6449
6450 #endif
6451 static struct rtnl_link_stats64 *ixgbe_get_stats64(struct net_device *netdev,
6452                                                    struct rtnl_link_stats64 *stats)
6453 {
6454         struct ixgbe_adapter *adapter = netdev_priv(netdev);
6455         int i;
6456
6457         rcu_read_lock();
6458         for (i = 0; i < adapter->num_rx_queues; i++) {
6459                 struct ixgbe_ring *ring = ACCESS_ONCE(adapter->rx_ring[i]);
6460                 u64 bytes, packets;
6461                 unsigned int start;
6462
6463                 if (ring) {
6464                         do {
6465                                 start = u64_stats_fetch_begin_bh(&ring->syncp);
6466                                 packets = ring->stats.packets;
6467                                 bytes   = ring->stats.bytes;
6468                         } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
6469                         stats->rx_packets += packets;
6470                         stats->rx_bytes   += bytes;
6471                 }
6472         }
6473
6474         for (i = 0; i < adapter->num_tx_queues; i++) {
6475                 struct ixgbe_ring *ring = ACCESS_ONCE(adapter->tx_ring[i]);
6476                 u64 bytes, packets;
6477                 unsigned int start;
6478
6479                 if (ring) {
6480                         do {
6481                                 start = u64_stats_fetch_begin_bh(&ring->syncp);
6482                                 packets = ring->stats.packets;
6483                                 bytes   = ring->stats.bytes;
6484                         } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
6485                         stats->tx_packets += packets;
6486                         stats->tx_bytes   += bytes;
6487                 }
6488         }
6489         rcu_read_unlock();
6490         /* following stats updated by ixgbe_watchdog_task() */
6491         stats->multicast        = netdev->stats.multicast;
6492         stats->rx_errors        = netdev->stats.rx_errors;
6493         stats->rx_length_errors = netdev->stats.rx_length_errors;
6494         stats->rx_crc_errors    = netdev->stats.rx_crc_errors;
6495         stats->rx_missed_errors = netdev->stats.rx_missed_errors;
6496         return stats;
6497 }
6498
6499 #ifdef CONFIG_IXGBE_DCB
6500 /* ixgbe_validate_rtr - verify 802.1Qp to Rx packet buffer mapping is valid.
6501  * #adapter: pointer to ixgbe_adapter
6502  * @tc: number of traffic classes currently enabled
6503  *
6504  * Configure a valid 802.1Qp to Rx packet buffer mapping ie confirm
6505  * 802.1Q priority maps to a packet buffer that exists.
6506  */
6507 static void ixgbe_validate_rtr(struct ixgbe_adapter *adapter, u8 tc)
6508 {
6509         struct ixgbe_hw *hw = &adapter->hw;
6510         u32 reg, rsave;
6511         int i;
6512
6513         /* 82598 have a static priority to TC mapping that can not
6514          * be changed so no validation is needed.
6515          */
6516         if (hw->mac.type == ixgbe_mac_82598EB)
6517                 return;
6518
6519         reg = IXGBE_READ_REG(hw, IXGBE_RTRUP2TC);
6520         rsave = reg;
6521
6522         for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
6523                 u8 up2tc = reg >> (i * IXGBE_RTRUP2TC_UP_SHIFT);
6524
6525                 /* If up2tc is out of bounds default to zero */
6526                 if (up2tc > tc)
6527                         reg &= ~(0x7 << IXGBE_RTRUP2TC_UP_SHIFT);
6528         }
6529
6530         if (reg != rsave)
6531                 IXGBE_WRITE_REG(hw, IXGBE_RTRUP2TC, reg);
6532
6533         return;
6534 }
6535
6536 /* ixgbe_setup_tc - routine to configure net_device for multiple traffic
6537  * classes.
6538  *
6539  * @netdev: net device to configure
6540  * @tc: number of traffic classes to enable
6541  */
6542 int ixgbe_setup_tc(struct net_device *dev, u8 tc)
6543 {
6544         struct ixgbe_adapter *adapter = netdev_priv(dev);
6545         struct ixgbe_hw *hw = &adapter->hw;
6546
6547         /* Multiple traffic classes requires multiple queues */
6548         if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
6549                 e_err(drv, "Enable failed, needs MSI-X\n");
6550                 return -EINVAL;
6551         }
6552
6553         /* Hardware supports up to 8 traffic classes */
6554         if (tc > adapter->dcb_cfg.num_tcs.pg_tcs ||
6555             (hw->mac.type == ixgbe_mac_82598EB &&
6556              tc < MAX_TRAFFIC_CLASS))
6557                 return -EINVAL;
6558
6559         /* Hardware has to reinitialize queues and interrupts to
6560          * match packet buffer alignment. Unfortunately, the
6561          * hardware is not flexible enough to do this dynamically.
6562          */
6563         if (netif_running(dev))
6564                 ixgbe_close(dev);
6565         ixgbe_clear_interrupt_scheme(adapter);
6566
6567         if (tc) {
6568                 netdev_set_num_tc(dev, tc);
6569                 adapter->last_lfc_mode = adapter->hw.fc.current_mode;
6570                 adapter->flags |= IXGBE_FLAG_DCB_ENABLED;
6571                 adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
6572
6573                 if (adapter->hw.mac.type == ixgbe_mac_82598EB)
6574                         adapter->hw.fc.requested_mode = ixgbe_fc_none;
6575         } else {
6576                 netdev_reset_tc(dev);
6577                 adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
6578
6579                 adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
6580                 adapter->flags |= IXGBE_FLAG_FDIR_HASH_CAPABLE;
6581
6582                 adapter->temp_dcb_cfg.pfc_mode_enable = false;
6583                 adapter->dcb_cfg.pfc_mode_enable = false;
6584         }
6585
6586         ixgbe_init_interrupt_scheme(adapter);
6587         ixgbe_validate_rtr(adapter, tc);
6588         if (netif_running(dev))
6589                 ixgbe_open(dev);
6590
6591         return 0;
6592 }
6593
6594 #endif /* CONFIG_IXGBE_DCB */
6595 void ixgbe_do_reset(struct net_device *netdev)
6596 {
6597         struct ixgbe_adapter *adapter = netdev_priv(netdev);
6598
6599         if (netif_running(netdev))
6600                 ixgbe_reinit_locked(adapter);
6601         else
6602                 ixgbe_reset(adapter);
6603 }
6604
6605 static netdev_features_t ixgbe_fix_features(struct net_device *netdev,
6606                                             netdev_features_t features)
6607 {
6608         struct ixgbe_adapter *adapter = netdev_priv(netdev);
6609
6610 #ifdef CONFIG_DCB
6611         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED)
6612                 features &= ~NETIF_F_HW_VLAN_RX;
6613 #endif
6614
6615         /* return error if RXHASH is being enabled when RSS is not supported */
6616         if (!(adapter->flags & IXGBE_FLAG_RSS_ENABLED))
6617                 features &= ~NETIF_F_RXHASH;
6618
6619         /* If Rx checksum is disabled, then RSC/LRO should also be disabled */
6620         if (!(features & NETIF_F_RXCSUM))
6621                 features &= ~NETIF_F_LRO;
6622
6623         /* Turn off LRO if not RSC capable */
6624         if (!(adapter->flags2 & IXGBE_FLAG2_RSC_CAPABLE))
6625                 features &= ~NETIF_F_LRO;
6626         
6627
6628         return features;
6629 }
6630
6631 static int ixgbe_set_features(struct net_device *netdev,
6632                               netdev_features_t features)
6633 {
6634         struct ixgbe_adapter *adapter = netdev_priv(netdev);
6635         netdev_features_t changed = netdev->features ^ features;
6636         bool need_reset = false;
6637
6638         /* Make sure RSC matches LRO, reset if change */
6639         if (!(features & NETIF_F_LRO)) {
6640                 if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)
6641                         need_reset = true;
6642                 adapter->flags2 &= ~IXGBE_FLAG2_RSC_ENABLED;
6643         } else if ((adapter->flags2 & IXGBE_FLAG2_RSC_CAPABLE) &&
6644                    !(adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)) {
6645                 if (adapter->rx_itr_setting == 1 ||
6646                     adapter->rx_itr_setting > IXGBE_MIN_RSC_ITR) {
6647                         adapter->flags2 |= IXGBE_FLAG2_RSC_ENABLED;
6648                         need_reset = true;
6649                 } else if ((changed ^ features) & NETIF_F_LRO) {
6650                         e_info(probe, "rx-usecs set too low, "
6651                                "disabling RSC\n");
6652                 }
6653         }
6654
6655         /*
6656          * Check if Flow Director n-tuple support was enabled or disabled.  If
6657          * the state changed, we need to reset.
6658          */
6659         if (!(features & NETIF_F_NTUPLE)) {
6660                 if (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE) {
6661                         /* turn off Flow Director, set ATR and reset */
6662                         if ((adapter->flags & IXGBE_FLAG_RSS_ENABLED) &&
6663                             !(adapter->flags & IXGBE_FLAG_DCB_ENABLED))
6664                                 adapter->flags |= IXGBE_FLAG_FDIR_HASH_CAPABLE;
6665                         need_reset = true;
6666                 }
6667                 adapter->flags &= ~IXGBE_FLAG_FDIR_PERFECT_CAPABLE;
6668         } else if (!(adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)) {
6669                 /* turn off ATR, enable perfect filters and reset */
6670                 adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
6671                 adapter->flags |= IXGBE_FLAG_FDIR_PERFECT_CAPABLE;
6672                 need_reset = true;
6673         }
6674
6675         if (changed & NETIF_F_RXALL)
6676                 need_reset = true;
6677
6678         netdev->features = features;
6679         if (need_reset)
6680                 ixgbe_do_reset(netdev);
6681
6682         return 0;
6683 }
6684
6685 static int ixgbe_ndo_fdb_add(struct ndmsg *ndm,
6686                              struct net_device *dev,
6687                              unsigned char *addr,
6688                              u16 flags)
6689 {
6690         struct ixgbe_adapter *adapter = netdev_priv(dev);
6691         int err = -EOPNOTSUPP;
6692
6693         if (ndm->ndm_state & NUD_PERMANENT) {
6694                 pr_info("%s: FDB only supports static addresses\n",
6695                         ixgbe_driver_name);
6696                 return -EINVAL;
6697         }
6698
6699         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
6700                 if (is_unicast_ether_addr(addr))
6701                         err = dev_uc_add_excl(dev, addr);
6702                 else if (is_multicast_ether_addr(addr))
6703                         err = dev_mc_add_excl(dev, addr);
6704                 else
6705                         err = -EINVAL;
6706         }
6707
6708         /* Only return duplicate errors if NLM_F_EXCL is set */
6709         if (err == -EEXIST && !(flags & NLM_F_EXCL))
6710                 err = 0;
6711
6712         return err;
6713 }
6714
6715 static int ixgbe_ndo_fdb_del(struct ndmsg *ndm,
6716                              struct net_device *dev,
6717                              unsigned char *addr)
6718 {
6719         struct ixgbe_adapter *adapter = netdev_priv(dev);
6720         int err = -EOPNOTSUPP;
6721
6722         if (ndm->ndm_state & NUD_PERMANENT) {
6723                 pr_info("%s: FDB only supports static addresses\n",
6724                         ixgbe_driver_name);
6725                 return -EINVAL;
6726         }
6727
6728         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
6729                 if (is_unicast_ether_addr(addr))
6730                         err = dev_uc_del(dev, addr);
6731                 else if (is_multicast_ether_addr(addr))
6732                         err = dev_mc_del(dev, addr);
6733                 else
6734                         err = -EINVAL;
6735         }
6736
6737         return err;
6738 }
6739
6740 static int ixgbe_ndo_fdb_dump(struct sk_buff *skb,
6741                               struct netlink_callback *cb,
6742                               struct net_device *dev,
6743                               int idx)
6744 {
6745         struct ixgbe_adapter *adapter = netdev_priv(dev);
6746
6747         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
6748                 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
6749
6750         return idx;
6751 }
6752
6753 static const struct net_device_ops ixgbe_netdev_ops = {
6754         .ndo_open               = ixgbe_open,
6755         .ndo_stop               = ixgbe_close,
6756         .ndo_start_xmit         = ixgbe_xmit_frame,
6757         .ndo_select_queue       = ixgbe_select_queue,
6758         .ndo_set_rx_mode        = ixgbe_set_rx_mode,
6759         .ndo_validate_addr      = eth_validate_addr,
6760         .ndo_set_mac_address    = ixgbe_set_mac,
6761         .ndo_change_mtu         = ixgbe_change_mtu,
6762         .ndo_tx_timeout         = ixgbe_tx_timeout,
6763         .ndo_vlan_rx_add_vid    = ixgbe_vlan_rx_add_vid,
6764         .ndo_vlan_rx_kill_vid   = ixgbe_vlan_rx_kill_vid,
6765         .ndo_do_ioctl           = ixgbe_ioctl,
6766         .ndo_set_vf_mac         = ixgbe_ndo_set_vf_mac,
6767         .ndo_set_vf_vlan        = ixgbe_ndo_set_vf_vlan,
6768         .ndo_set_vf_tx_rate     = ixgbe_ndo_set_vf_bw,
6769         .ndo_set_vf_spoofchk    = ixgbe_ndo_set_vf_spoofchk,
6770         .ndo_get_vf_config      = ixgbe_ndo_get_vf_config,
6771         .ndo_get_stats64        = ixgbe_get_stats64,
6772 #ifdef CONFIG_IXGBE_DCB
6773         .ndo_setup_tc           = ixgbe_setup_tc,
6774 #endif
6775 #ifdef CONFIG_NET_POLL_CONTROLLER
6776         .ndo_poll_controller    = ixgbe_netpoll,
6777 #endif
6778 #ifdef IXGBE_FCOE
6779         .ndo_fcoe_ddp_setup = ixgbe_fcoe_ddp_get,
6780         .ndo_fcoe_ddp_target = ixgbe_fcoe_ddp_target,
6781         .ndo_fcoe_ddp_done = ixgbe_fcoe_ddp_put,
6782         .ndo_fcoe_enable = ixgbe_fcoe_enable,
6783         .ndo_fcoe_disable = ixgbe_fcoe_disable,
6784         .ndo_fcoe_get_wwn = ixgbe_fcoe_get_wwn,
6785         .ndo_fcoe_get_hbainfo = ixgbe_fcoe_get_hbainfo,
6786 #endif /* IXGBE_FCOE */
6787         .ndo_set_features = ixgbe_set_features,
6788         .ndo_fix_features = ixgbe_fix_features,
6789         .ndo_fdb_add            = ixgbe_ndo_fdb_add,
6790         .ndo_fdb_del            = ixgbe_ndo_fdb_del,
6791         .ndo_fdb_dump           = ixgbe_ndo_fdb_dump,
6792 };
6793
6794 static void __devinit ixgbe_probe_vf(struct ixgbe_adapter *adapter,
6795                                      const struct ixgbe_info *ii)
6796 {
6797 #ifdef CONFIG_PCI_IOV
6798         struct ixgbe_hw *hw = &adapter->hw;
6799
6800         if (hw->mac.type == ixgbe_mac_82598EB)
6801                 return;
6802
6803         /* The 82599 supports up to 64 VFs per physical function
6804          * but this implementation limits allocation to 63 so that
6805          * basic networking resources are still available to the
6806          * physical function
6807          */
6808         adapter->num_vfs = (max_vfs > 63) ? 63 : max_vfs;
6809         ixgbe_enable_sriov(adapter, ii);
6810 #endif /* CONFIG_PCI_IOV */
6811 }
6812
6813 /**
6814  * ixgbe_probe - Device Initialization Routine
6815  * @pdev: PCI device information struct
6816  * @ent: entry in ixgbe_pci_tbl
6817  *
6818  * Returns 0 on success, negative on failure
6819  *
6820  * ixgbe_probe initializes an adapter identified by a pci_dev structure.
6821  * The OS initialization, configuring of the adapter private structure,
6822  * and a hardware reset occur.
6823  **/
6824 static int __devinit ixgbe_probe(struct pci_dev *pdev,
6825                                  const struct pci_device_id *ent)
6826 {
6827         struct net_device *netdev;
6828         struct ixgbe_adapter *adapter = NULL;
6829         struct ixgbe_hw *hw;
6830         const struct ixgbe_info *ii = ixgbe_info_tbl[ent->driver_data];
6831         static int cards_found;
6832         int i, err, pci_using_dac;
6833         u8 part_str[IXGBE_PBANUM_LENGTH];
6834         unsigned int indices = num_possible_cpus();
6835 #ifdef IXGBE_FCOE
6836         u16 device_caps;
6837 #endif
6838         u32 eec;
6839         u16 wol_cap;
6840
6841         /* Catch broken hardware that put the wrong VF device ID in
6842          * the PCIe SR-IOV capability.
6843          */
6844         if (pdev->is_virtfn) {
6845                 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
6846                      pci_name(pdev), pdev->vendor, pdev->device);
6847                 return -EINVAL;
6848         }
6849
6850         err = pci_enable_device_mem(pdev);
6851         if (err)
6852                 return err;
6853
6854         if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
6855             !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
6856                 pci_using_dac = 1;
6857         } else {
6858                 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
6859                 if (err) {
6860                         err = dma_set_coherent_mask(&pdev->dev,
6861                                                     DMA_BIT_MASK(32));
6862                         if (err) {
6863                                 dev_err(&pdev->dev,
6864                                         "No usable DMA configuration, aborting\n");
6865                                 goto err_dma;
6866                         }
6867                 }
6868                 pci_using_dac = 0;
6869         }
6870
6871         err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
6872                                            IORESOURCE_MEM), ixgbe_driver_name);
6873         if (err) {
6874                 dev_err(&pdev->dev,
6875                         "pci_request_selected_regions failed 0x%x\n", err);
6876                 goto err_pci_reg;
6877         }
6878
6879         pci_enable_pcie_error_reporting(pdev);
6880
6881         pci_set_master(pdev);
6882         pci_save_state(pdev);
6883
6884 #ifdef CONFIG_IXGBE_DCB
6885         indices *= MAX_TRAFFIC_CLASS;
6886 #endif
6887
6888         if (ii->mac == ixgbe_mac_82598EB)
6889                 indices = min_t(unsigned int, indices, IXGBE_MAX_RSS_INDICES);
6890         else
6891                 indices = min_t(unsigned int, indices, IXGBE_MAX_FDIR_INDICES);
6892
6893 #ifdef IXGBE_FCOE
6894         indices += min_t(unsigned int, num_possible_cpus(),
6895                          IXGBE_MAX_FCOE_INDICES);
6896 #endif
6897         netdev = alloc_etherdev_mq(sizeof(struct ixgbe_adapter), indices);
6898         if (!netdev) {
6899                 err = -ENOMEM;
6900                 goto err_alloc_etherdev;
6901         }
6902
6903         SET_NETDEV_DEV(netdev, &pdev->dev);
6904
6905         adapter = netdev_priv(netdev);
6906         pci_set_drvdata(pdev, adapter);
6907
6908         adapter->netdev = netdev;
6909         adapter->pdev = pdev;
6910         hw = &adapter->hw;
6911         hw->back = adapter;
6912         adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
6913
6914         hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
6915                               pci_resource_len(pdev, 0));
6916         if (!hw->hw_addr) {
6917                 err = -EIO;
6918                 goto err_ioremap;
6919         }
6920
6921         for (i = 1; i <= 5; i++) {
6922                 if (pci_resource_len(pdev, i) == 0)
6923                         continue;
6924         }
6925
6926         netdev->netdev_ops = &ixgbe_netdev_ops;
6927         ixgbe_set_ethtool_ops(netdev);
6928         netdev->watchdog_timeo = 5 * HZ;
6929         strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
6930
6931         adapter->bd_number = cards_found;
6932
6933         /* Setup hw api */
6934         memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
6935         hw->mac.type  = ii->mac;
6936
6937         /* EEPROM */
6938         memcpy(&hw->eeprom.ops, ii->eeprom_ops, sizeof(hw->eeprom.ops));
6939         eec = IXGBE_READ_REG(hw, IXGBE_EEC);
6940         /* If EEPROM is valid (bit 8 = 1), use default otherwise use bit bang */
6941         if (!(eec & (1 << 8)))
6942                 hw->eeprom.ops.read = &ixgbe_read_eeprom_bit_bang_generic;
6943
6944         /* PHY */
6945         memcpy(&hw->phy.ops, ii->phy_ops, sizeof(hw->phy.ops));
6946         hw->phy.sfp_type = ixgbe_sfp_type_unknown;
6947         /* ixgbe_identify_phy_generic will set prtad and mmds properly */
6948         hw->phy.mdio.prtad = MDIO_PRTAD_NONE;
6949         hw->phy.mdio.mmds = 0;
6950         hw->phy.mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
6951         hw->phy.mdio.dev = netdev;
6952         hw->phy.mdio.mdio_read = ixgbe_mdio_read;
6953         hw->phy.mdio.mdio_write = ixgbe_mdio_write;
6954
6955         ii->get_invariants(hw);
6956
6957         /* setup the private structure */
6958         err = ixgbe_sw_init(adapter);
6959         if (err)
6960                 goto err_sw_init;
6961
6962         /* Make it possible the adapter to be woken up via WOL */
6963         switch (adapter->hw.mac.type) {
6964         case ixgbe_mac_82599EB:
6965         case ixgbe_mac_X540:
6966                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_WUS, ~0);
6967                 break;
6968         default:
6969                 break;
6970         }
6971
6972         /*
6973          * If there is a fan on this device and it has failed log the
6974          * failure.
6975          */
6976         if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) {
6977                 u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
6978                 if (esdp & IXGBE_ESDP_SDP1)
6979                         e_crit(probe, "Fan has stopped, replace the adapter\n");
6980         }
6981
6982         if (allow_unsupported_sfp)
6983                 hw->allow_unsupported_sfp = allow_unsupported_sfp;
6984
6985         /* reset_hw fills in the perm_addr as well */
6986         hw->phy.reset_if_overtemp = true;
6987         err = hw->mac.ops.reset_hw(hw);
6988         hw->phy.reset_if_overtemp = false;
6989         if (err == IXGBE_ERR_SFP_NOT_PRESENT &&
6990             hw->mac.type == ixgbe_mac_82598EB) {
6991                 err = 0;
6992         } else if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
6993                 e_dev_err("failed to load because an unsupported SFP+ "
6994                           "module type was detected.\n");
6995                 e_dev_err("Reload the driver after installing a supported "
6996                           "module.\n");
6997                 goto err_sw_init;
6998         } else if (err) {
6999                 e_dev_err("HW Init failed: %d\n", err);
7000                 goto err_sw_init;
7001         }
7002
7003         ixgbe_probe_vf(adapter, ii);
7004
7005         netdev->features = NETIF_F_SG |
7006                            NETIF_F_IP_CSUM |
7007                            NETIF_F_IPV6_CSUM |
7008                            NETIF_F_HW_VLAN_TX |
7009                            NETIF_F_HW_VLAN_RX |
7010                            NETIF_F_HW_VLAN_FILTER |
7011                            NETIF_F_TSO |
7012                            NETIF_F_TSO6 |
7013                            NETIF_F_RXHASH |
7014                            NETIF_F_RXCSUM;
7015
7016         netdev->hw_features = netdev->features;
7017
7018         switch (adapter->hw.mac.type) {
7019         case ixgbe_mac_82599EB:
7020         case ixgbe_mac_X540:
7021                 netdev->features |= NETIF_F_SCTP_CSUM;
7022                 netdev->hw_features |= NETIF_F_SCTP_CSUM |
7023                                        NETIF_F_NTUPLE;
7024                 break;
7025         default:
7026                 break;
7027         }
7028
7029         netdev->hw_features |= NETIF_F_RXALL;
7030
7031         netdev->vlan_features |= NETIF_F_TSO;
7032         netdev->vlan_features |= NETIF_F_TSO6;
7033         netdev->vlan_features |= NETIF_F_IP_CSUM;
7034         netdev->vlan_features |= NETIF_F_IPV6_CSUM;
7035         netdev->vlan_features |= NETIF_F_SG;
7036
7037         netdev->priv_flags |= IFF_UNICAST_FLT;
7038         netdev->priv_flags |= IFF_SUPP_NOFCS;
7039
7040         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
7041                 adapter->flags &= ~(IXGBE_FLAG_RSS_ENABLED |
7042                                     IXGBE_FLAG_DCB_ENABLED);
7043
7044 #ifdef CONFIG_IXGBE_DCB
7045         netdev->dcbnl_ops = &dcbnl_ops;
7046 #endif
7047
7048 #ifdef IXGBE_FCOE
7049         if (adapter->flags & IXGBE_FLAG_FCOE_CAPABLE) {
7050                 if (hw->mac.ops.get_device_caps) {
7051                         hw->mac.ops.get_device_caps(hw, &device_caps);
7052                         if (device_caps & IXGBE_DEVICE_CAPS_FCOE_OFFLOADS)
7053                                 adapter->flags &= ~IXGBE_FLAG_FCOE_CAPABLE;
7054                 }
7055         }
7056         if (adapter->flags & IXGBE_FLAG_FCOE_CAPABLE) {
7057                 netdev->vlan_features |= NETIF_F_FCOE_CRC;
7058                 netdev->vlan_features |= NETIF_F_FSO;
7059                 netdev->vlan_features |= NETIF_F_FCOE_MTU;
7060         }
7061 #endif /* IXGBE_FCOE */
7062         if (pci_using_dac) {
7063                 netdev->features |= NETIF_F_HIGHDMA;
7064                 netdev->vlan_features |= NETIF_F_HIGHDMA;
7065         }
7066
7067         if (adapter->flags2 & IXGBE_FLAG2_RSC_CAPABLE)
7068                 netdev->hw_features |= NETIF_F_LRO;
7069         if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)
7070                 netdev->features |= NETIF_F_LRO;
7071
7072         /* make sure the EEPROM is good */
7073         if (hw->eeprom.ops.validate_checksum(hw, NULL) < 0) {
7074                 e_dev_err("The EEPROM Checksum Is Not Valid\n");
7075                 err = -EIO;
7076                 goto err_sw_init;
7077         }
7078
7079         memcpy(netdev->dev_addr, hw->mac.perm_addr, netdev->addr_len);
7080         memcpy(netdev->perm_addr, hw->mac.perm_addr, netdev->addr_len);
7081
7082         if (ixgbe_validate_mac_addr(netdev->perm_addr)) {
7083                 e_dev_err("invalid MAC address\n");
7084                 err = -EIO;
7085                 goto err_sw_init;
7086         }
7087
7088         setup_timer(&adapter->service_timer, &ixgbe_service_timer,
7089                     (unsigned long) adapter);
7090
7091         INIT_WORK(&adapter->service_task, ixgbe_service_task);
7092         clear_bit(__IXGBE_SERVICE_SCHED, &adapter->state);
7093
7094         err = ixgbe_init_interrupt_scheme(adapter);
7095         if (err)
7096                 goto err_sw_init;
7097
7098         if (!(adapter->flags & IXGBE_FLAG_RSS_ENABLED)) {
7099                 netdev->hw_features &= ~NETIF_F_RXHASH;
7100                 netdev->features &= ~NETIF_F_RXHASH;
7101         }
7102
7103         /* WOL not supported for all but the following */
7104         adapter->wol = 0;
7105         switch (pdev->device) {
7106         case IXGBE_DEV_ID_82599_SFP:
7107                 /* Only these subdevice supports WOL */
7108                 switch (pdev->subsystem_device) {
7109                 case IXGBE_SUBDEV_ID_82599_560FLR:
7110                         /* only support first port */
7111                         if (hw->bus.func != 0)
7112                                 break;
7113                 case IXGBE_SUBDEV_ID_82599_SFP:
7114                         adapter->wol = IXGBE_WUFC_MAG;
7115                         break;
7116                 }
7117                 break;
7118         case IXGBE_DEV_ID_82599_COMBO_BACKPLANE:
7119                 /* All except this subdevice support WOL */
7120                 if (pdev->subsystem_device != IXGBE_SUBDEV_ID_82599_KX4_KR_MEZZ)
7121                         adapter->wol = IXGBE_WUFC_MAG;
7122                 break;
7123         case IXGBE_DEV_ID_82599_KX4:
7124                 adapter->wol = IXGBE_WUFC_MAG;
7125                 break;
7126         case IXGBE_DEV_ID_X540T:
7127                 /* Check eeprom to see if it is enabled */
7128                 hw->eeprom.ops.read(hw, 0x2c, &adapter->eeprom_cap);
7129                 wol_cap = adapter->eeprom_cap & IXGBE_DEVICE_CAPS_WOL_MASK;
7130
7131                 if ((wol_cap == IXGBE_DEVICE_CAPS_WOL_PORT0_1) ||
7132                     ((wol_cap == IXGBE_DEVICE_CAPS_WOL_PORT0) &&
7133                      (hw->bus.func == 0)))
7134                         adapter->wol = IXGBE_WUFC_MAG;
7135                 break;
7136         }
7137         device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
7138
7139         /* save off EEPROM version number */
7140         hw->eeprom.ops.read(hw, 0x2e, &adapter->eeprom_verh);
7141         hw->eeprom.ops.read(hw, 0x2d, &adapter->eeprom_verl);
7142
7143         /* pick up the PCI bus settings for reporting later */
7144         hw->mac.ops.get_bus_info(hw);
7145
7146         /* print bus type/speed/width info */
7147         e_dev_info("(PCI Express:%s:%s) %pM\n",
7148                    (hw->bus.speed == ixgbe_bus_speed_5000 ? "5.0GT/s" :
7149                     hw->bus.speed == ixgbe_bus_speed_2500 ? "2.5GT/s" :
7150                     "Unknown"),
7151                    (hw->bus.width == ixgbe_bus_width_pcie_x8 ? "Width x8" :
7152                     hw->bus.width == ixgbe_bus_width_pcie_x4 ? "Width x4" :
7153                     hw->bus.width == ixgbe_bus_width_pcie_x1 ? "Width x1" :
7154                     "Unknown"),
7155                    netdev->dev_addr);
7156
7157         err = ixgbe_read_pba_string_generic(hw, part_str, IXGBE_PBANUM_LENGTH);
7158         if (err)
7159                 strncpy(part_str, "Unknown", IXGBE_PBANUM_LENGTH);
7160         if (ixgbe_is_sfp(hw) && hw->phy.sfp_type != ixgbe_sfp_type_not_present)
7161                 e_dev_info("MAC: %d, PHY: %d, SFP+: %d, PBA No: %s\n",
7162                            hw->mac.type, hw->phy.type, hw->phy.sfp_type,
7163                            part_str);
7164         else
7165                 e_dev_info("MAC: %d, PHY: %d, PBA No: %s\n",
7166                            hw->mac.type, hw->phy.type, part_str);
7167
7168         if (hw->bus.width <= ixgbe_bus_width_pcie_x4) {
7169                 e_dev_warn("PCI-Express bandwidth available for this card is "
7170                            "not sufficient for optimal performance.\n");
7171                 e_dev_warn("For optimal performance a x8 PCI-Express slot "
7172                            "is required.\n");
7173         }
7174
7175         /* reset the hardware with the new settings */
7176         err = hw->mac.ops.start_hw(hw);
7177         if (err == IXGBE_ERR_EEPROM_VERSION) {
7178                 /* We are running on a pre-production device, log a warning */
7179                 e_dev_warn("This device is a pre-production adapter/LOM. "
7180                            "Please be aware there may be issues associated "
7181                            "with your hardware.  If you are experiencing "
7182                            "problems please contact your Intel or hardware "
7183                            "representative who provided you with this "
7184                            "hardware.\n");
7185         }
7186         strcpy(netdev->name, "eth%d");
7187         err = register_netdev(netdev);
7188         if (err)
7189                 goto err_register;
7190
7191         /* power down the optics for multispeed fiber and 82599 SFP+ fiber */
7192         if (hw->mac.ops.disable_tx_laser &&
7193             ((hw->phy.multispeed_fiber) ||
7194              ((hw->mac.ops.get_media_type(hw) == ixgbe_media_type_fiber) &&
7195               (hw->mac.type == ixgbe_mac_82599EB))))
7196                 hw->mac.ops.disable_tx_laser(hw);
7197
7198         /* carrier off reporting is important to ethtool even BEFORE open */
7199         netif_carrier_off(netdev);
7200
7201 #ifdef CONFIG_IXGBE_DCA
7202         if (dca_add_requester(&pdev->dev) == 0) {
7203                 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
7204                 ixgbe_setup_dca(adapter);
7205         }
7206 #endif
7207         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
7208                 e_info(probe, "IOV is enabled with %d VFs\n", adapter->num_vfs);
7209                 for (i = 0; i < adapter->num_vfs; i++)
7210                         ixgbe_vf_configuration(pdev, (i | 0x10000000));
7211         }
7212
7213         /* firmware requires driver version to be 0xFFFFFFFF
7214          * since os does not support feature
7215          */
7216         if (hw->mac.ops.set_fw_drv_ver)
7217                 hw->mac.ops.set_fw_drv_ver(hw, 0xFF, 0xFF, 0xFF,
7218                                            0xFF);
7219
7220         /* add san mac addr to netdev */
7221         ixgbe_add_sanmac_netdev(netdev);
7222
7223         e_dev_info("%s\n", ixgbe_default_device_descr);
7224         cards_found++;
7225         return 0;
7226
7227 err_register:
7228         ixgbe_release_hw_control(adapter);
7229         ixgbe_clear_interrupt_scheme(adapter);
7230 err_sw_init:
7231         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
7232                 ixgbe_disable_sriov(adapter);
7233         adapter->flags2 &= ~IXGBE_FLAG2_SEARCH_FOR_SFP;
7234         iounmap(hw->hw_addr);
7235 err_ioremap:
7236         free_netdev(netdev);
7237 err_alloc_etherdev:
7238         pci_release_selected_regions(pdev,
7239                                      pci_select_bars(pdev, IORESOURCE_MEM));
7240 err_pci_reg:
7241 err_dma:
7242         pci_disable_device(pdev);
7243         return err;
7244 }
7245
7246 /**
7247  * ixgbe_remove - Device Removal Routine
7248  * @pdev: PCI device information struct
7249  *
7250  * ixgbe_remove is called by the PCI subsystem to alert the driver
7251  * that it should release a PCI device.  The could be caused by a
7252  * Hot-Plug event, or because the driver is going to be removed from
7253  * memory.
7254  **/
7255 static void __devexit ixgbe_remove(struct pci_dev *pdev)
7256 {
7257         struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
7258         struct net_device *netdev = adapter->netdev;
7259
7260         set_bit(__IXGBE_DOWN, &adapter->state);
7261         cancel_work_sync(&adapter->service_task);
7262
7263 #ifdef CONFIG_IXGBE_DCA
7264         if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
7265                 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
7266                 dca_remove_requester(&pdev->dev);
7267                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
7268         }
7269
7270 #endif
7271 #ifdef IXGBE_FCOE
7272         if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
7273                 ixgbe_cleanup_fcoe(adapter);
7274
7275 #endif /* IXGBE_FCOE */
7276
7277         /* remove the added san mac */
7278         ixgbe_del_sanmac_netdev(netdev);
7279
7280         if (netdev->reg_state == NETREG_REGISTERED)
7281                 unregister_netdev(netdev);
7282
7283         if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
7284                 if (!(ixgbe_check_vf_assignment(adapter)))
7285                         ixgbe_disable_sriov(adapter);
7286                 else
7287                         e_dev_warn("Unloading driver while VFs are assigned "
7288                                    "- VFs will not be deallocated\n");
7289         }
7290
7291         ixgbe_clear_interrupt_scheme(adapter);
7292
7293         ixgbe_release_hw_control(adapter);
7294
7295         iounmap(adapter->hw.hw_addr);
7296         pci_release_selected_regions(pdev, pci_select_bars(pdev,
7297                                      IORESOURCE_MEM));
7298
7299         e_dev_info("complete\n");
7300
7301         free_netdev(netdev);
7302
7303         pci_disable_pcie_error_reporting(pdev);
7304
7305         pci_disable_device(pdev);
7306 }
7307
7308 /**
7309  * ixgbe_io_error_detected - called when PCI error is detected
7310  * @pdev: Pointer to PCI device
7311  * @state: The current pci connection state
7312  *
7313  * This function is called after a PCI bus error affecting
7314  * this device has been detected.
7315  */
7316 static pci_ers_result_t ixgbe_io_error_detected(struct pci_dev *pdev,
7317                                                 pci_channel_state_t state)
7318 {
7319         struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
7320         struct net_device *netdev = adapter->netdev;
7321
7322 #ifdef CONFIG_PCI_IOV
7323         struct pci_dev *bdev, *vfdev;
7324         u32 dw0, dw1, dw2, dw3;
7325         int vf, pos;
7326         u16 req_id, pf_func;
7327
7328         if (adapter->hw.mac.type == ixgbe_mac_82598EB ||
7329             adapter->num_vfs == 0)
7330                 goto skip_bad_vf_detection;
7331
7332         bdev = pdev->bus->self;
7333         while (bdev && (bdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT))
7334                 bdev = bdev->bus->self;
7335
7336         if (!bdev)
7337                 goto skip_bad_vf_detection;
7338
7339         pos = pci_find_ext_capability(bdev, PCI_EXT_CAP_ID_ERR);
7340         if (!pos)
7341                 goto skip_bad_vf_detection;
7342
7343         pci_read_config_dword(bdev, pos + PCI_ERR_HEADER_LOG, &dw0);
7344         pci_read_config_dword(bdev, pos + PCI_ERR_HEADER_LOG + 4, &dw1);
7345         pci_read_config_dword(bdev, pos + PCI_ERR_HEADER_LOG + 8, &dw2);
7346         pci_read_config_dword(bdev, pos + PCI_ERR_HEADER_LOG + 12, &dw3);
7347
7348         req_id = dw1 >> 16;
7349         /* On the 82599 if bit 7 of the requestor ID is set then it's a VF */
7350         if (!(req_id & 0x0080))
7351                 goto skip_bad_vf_detection;
7352
7353         pf_func = req_id & 0x01;
7354         if ((pf_func & 1) == (pdev->devfn & 1)) {
7355                 unsigned int device_id;
7356
7357                 vf = (req_id & 0x7F) >> 1;
7358                 e_dev_err("VF %d has caused a PCIe error\n", vf);
7359                 e_dev_err("TLP: dw0: %8.8x\tdw1: %8.8x\tdw2: "
7360                                 "%8.8x\tdw3: %8.8x\n",
7361                 dw0, dw1, dw2, dw3);
7362                 switch (adapter->hw.mac.type) {
7363                 case ixgbe_mac_82599EB:
7364                         device_id = IXGBE_82599_VF_DEVICE_ID;
7365                         break;
7366                 case ixgbe_mac_X540:
7367                         device_id = IXGBE_X540_VF_DEVICE_ID;
7368                         break;
7369                 default:
7370                         device_id = 0;
7371                         break;
7372                 }
7373
7374                 /* Find the pci device of the offending VF */
7375                 vfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID, device_id, NULL);
7376                 while (vfdev) {
7377                         if (vfdev->devfn == (req_id & 0xFF))
7378                                 break;
7379                         vfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
7380                                                device_id, vfdev);
7381                 }
7382                 /*
7383                  * There's a slim chance the VF could have been hot plugged,
7384                  * so if it is no longer present we don't need to issue the
7385                  * VFLR.  Just clean up the AER in that case.
7386                  */
7387                 if (vfdev) {
7388                         e_dev_err("Issuing VFLR to VF %d\n", vf);
7389                         pci_write_config_dword(vfdev, 0xA8, 0x00008000);
7390                 }
7391
7392                 pci_cleanup_aer_uncorrect_error_status(pdev);
7393         }
7394
7395         /*
7396          * Even though the error may have occurred on the other port
7397          * we still need to increment the vf error reference count for
7398          * both ports because the I/O resume function will be called
7399          * for both of them.
7400          */
7401         adapter->vferr_refcount++;
7402
7403         return PCI_ERS_RESULT_RECOVERED;
7404
7405 skip_bad_vf_detection:
7406 #endif /* CONFIG_PCI_IOV */
7407         netif_device_detach(netdev);
7408
7409         if (state == pci_channel_io_perm_failure)
7410                 return PCI_ERS_RESULT_DISCONNECT;
7411
7412         if (netif_running(netdev))
7413                 ixgbe_down(adapter);
7414         pci_disable_device(pdev);
7415
7416         /* Request a slot reset. */
7417         return PCI_ERS_RESULT_NEED_RESET;
7418 }
7419
7420 /**
7421  * ixgbe_io_slot_reset - called after the pci bus has been reset.
7422  * @pdev: Pointer to PCI device
7423  *
7424  * Restart the card from scratch, as if from a cold-boot.
7425  */
7426 static pci_ers_result_t ixgbe_io_slot_reset(struct pci_dev *pdev)
7427 {
7428         struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
7429         pci_ers_result_t result;
7430         int err;
7431
7432         if (pci_enable_device_mem(pdev)) {
7433                 e_err(probe, "Cannot re-enable PCI device after reset.\n");
7434                 result = PCI_ERS_RESULT_DISCONNECT;
7435         } else {
7436                 pci_set_master(pdev);
7437                 pci_restore_state(pdev);
7438                 pci_save_state(pdev);
7439
7440                 pci_wake_from_d3(pdev, false);
7441
7442                 ixgbe_reset(adapter);
7443                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_WUS, ~0);
7444                 result = PCI_ERS_RESULT_RECOVERED;
7445         }
7446
7447         err = pci_cleanup_aer_uncorrect_error_status(pdev);
7448         if (err) {
7449                 e_dev_err("pci_cleanup_aer_uncorrect_error_status "
7450                           "failed 0x%0x\n", err);
7451                 /* non-fatal, continue */
7452         }
7453
7454         return result;
7455 }
7456
7457 /**
7458  * ixgbe_io_resume - called when traffic can start flowing again.
7459  * @pdev: Pointer to PCI device
7460  *
7461  * This callback is called when the error recovery driver tells us that
7462  * its OK to resume normal operation.
7463  */
7464 static void ixgbe_io_resume(struct pci_dev *pdev)
7465 {
7466         struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
7467         struct net_device *netdev = adapter->netdev;
7468
7469 #ifdef CONFIG_PCI_IOV
7470         if (adapter->vferr_refcount) {
7471                 e_info(drv, "Resuming after VF err\n");
7472                 adapter->vferr_refcount--;
7473                 return;
7474         }
7475
7476 #endif
7477         if (netif_running(netdev))
7478                 ixgbe_up(adapter);
7479
7480         netif_device_attach(netdev);
7481 }
7482
7483 static struct pci_error_handlers ixgbe_err_handler = {
7484         .error_detected = ixgbe_io_error_detected,
7485         .slot_reset = ixgbe_io_slot_reset,
7486         .resume = ixgbe_io_resume,
7487 };
7488
7489 static struct pci_driver ixgbe_driver = {
7490         .name     = ixgbe_driver_name,
7491         .id_table = ixgbe_pci_tbl,
7492         .probe    = ixgbe_probe,
7493         .remove   = __devexit_p(ixgbe_remove),
7494 #ifdef CONFIG_PM
7495         .suspend  = ixgbe_suspend,
7496         .resume   = ixgbe_resume,
7497 #endif
7498         .shutdown = ixgbe_shutdown,
7499         .err_handler = &ixgbe_err_handler
7500 };
7501
7502 /**
7503  * ixgbe_init_module - Driver Registration Routine
7504  *
7505  * ixgbe_init_module is the first routine called when the driver is
7506  * loaded. All it does is register with the PCI subsystem.
7507  **/
7508 static int __init ixgbe_init_module(void)
7509 {
7510         int ret;
7511         pr_info("%s - version %s\n", ixgbe_driver_string, ixgbe_driver_version);
7512         pr_info("%s\n", ixgbe_copyright);
7513
7514 #ifdef CONFIG_IXGBE_DCA
7515         dca_register_notify(&dca_notifier);
7516 #endif
7517
7518         ret = pci_register_driver(&ixgbe_driver);
7519         return ret;
7520 }
7521
7522 module_init(ixgbe_init_module);
7523
7524 /**
7525  * ixgbe_exit_module - Driver Exit Cleanup Routine
7526  *
7527  * ixgbe_exit_module is called just before the driver is removed
7528  * from memory.
7529  **/
7530 static void __exit ixgbe_exit_module(void)
7531 {
7532 #ifdef CONFIG_IXGBE_DCA
7533         dca_unregister_notify(&dca_notifier);
7534 #endif
7535         pci_unregister_driver(&ixgbe_driver);
7536         rcu_barrier(); /* Wait for completion of call_rcu()'s */
7537 }
7538
7539 #ifdef CONFIG_IXGBE_DCA
7540 static int ixgbe_notify_dca(struct notifier_block *nb, unsigned long event,
7541                             void *p)
7542 {
7543         int ret_val;
7544
7545         ret_val = driver_for_each_device(&ixgbe_driver.driver, NULL, &event,
7546                                          __ixgbe_notify_dca);
7547
7548         return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
7549 }
7550
7551 #endif /* CONFIG_IXGBE_DCA */
7552
7553 module_exit(ixgbe_exit_module);
7554
7555 /* ixgbe_main.c */