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