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