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