1 /*******************************************************************************
2 
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2016 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   Linux NICS <linux.nics@intel.com>
24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 
27 *******************************************************************************/
28 
29 #include <linux/types.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/netdevice.h>
33 #include <linux/vmalloc.h>
34 #include <linux/string.h>
35 #include <linux/in.h>
36 #include <linux/interrupt.h>
37 #include <linux/ip.h>
38 #include <linux/tcp.h>
39 #include <linux/sctp.h>
40 #include <linux/pkt_sched.h>
41 #include <linux/ipv6.h>
42 #include <linux/slab.h>
43 #include <net/checksum.h>
44 #include <net/ip6_checksum.h>
45 #include <linux/etherdevice.h>
46 #include <linux/ethtool.h>
47 #include <linux/if.h>
48 #include <linux/if_vlan.h>
49 #include <linux/if_macvlan.h>
50 #include <linux/if_bridge.h>
51 #include <linux/prefetch.h>
52 #include <linux/bpf.h>
53 #include <linux/bpf_trace.h>
54 #include <linux/atomic.h>
55 #include <scsi/fc/fc_fcoe.h>
56 #include <net/udp_tunnel.h>
57 #include <net/pkt_cls.h>
58 #include <net/tc_act/tc_gact.h>
59 #include <net/tc_act/tc_mirred.h>
60 #include <net/vxlan.h>
61 
62 #include "ixgbe.h"
63 #include "ixgbe_common.h"
64 #include "ixgbe_dcb_82599.h"
65 #include "ixgbe_sriov.h"
66 #include "ixgbe_model.h"
67 
68 char ixgbe_driver_name[] = "ixgbe";
69 static const char ixgbe_driver_string[] =
70 			      "Intel(R) 10 Gigabit PCI Express Network Driver";
71 #ifdef IXGBE_FCOE
72 char ixgbe_default_device_descr[] =
73 			      "Intel(R) 10 Gigabit Network Connection";
74 #else
75 static char ixgbe_default_device_descr[] =
76 			      "Intel(R) 10 Gigabit Network Connection";
77 #endif
78 #define DRV_VERSION "5.0.0-k"
79 const char ixgbe_driver_version[] = DRV_VERSION;
80 static const char ixgbe_copyright[] =
81 				"Copyright (c) 1999-2016 Intel Corporation.";
82 
83 static const char ixgbe_overheat_msg[] = "Network adapter has been stopped because it has over heated. Restart the computer. If the problem persists, power off the system and replace the adapter";
84 
85 static const struct ixgbe_info *ixgbe_info_tbl[] = {
86 	[board_82598]		= &ixgbe_82598_info,
87 	[board_82599]		= &ixgbe_82599_info,
88 	[board_X540]		= &ixgbe_X540_info,
89 	[board_X550]		= &ixgbe_X550_info,
90 	[board_X550EM_x]	= &ixgbe_X550EM_x_info,
91 	[board_x550em_x_fw]	= &ixgbe_x550em_x_fw_info,
92 	[board_x550em_a]	= &ixgbe_x550em_a_info,
93 	[board_x550em_a_fw]	= &ixgbe_x550em_a_fw_info,
94 };
95 
96 /* ixgbe_pci_tbl - PCI Device ID Table
97  *
98  * Wildcard entries (PCI_ANY_ID) should come last
99  * Last entry must be all 0s
100  *
101  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
102  *   Class, Class Mask, private data (not used) }
103  */
104 static const struct pci_device_id ixgbe_pci_tbl[] = {
105 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598), board_82598 },
106 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_DUAL_PORT), board_82598 },
107 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_SINGLE_PORT), board_82598 },
108 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT), board_82598 },
109 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT2), board_82598 },
110 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_CX4), board_82598 },
111 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_CX4_DUAL_PORT), board_82598 },
112 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_DA_DUAL_PORT), board_82598 },
113 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM), board_82598 },
114 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_XF_LR), board_82598 },
115 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_SFP_LOM), board_82598 },
116 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_BX), board_82598 },
117 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KX4), board_82599 },
118 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_XAUI_LOM), board_82599 },
119 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KR), board_82599 },
120 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP), board_82599 },
121 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_EM), board_82599 },
122 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KX4_MEZZ), board_82599 },
123 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_CX4), board_82599 },
124 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_BACKPLANE_FCOE), board_82599 },
125 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_FCOE), board_82599 },
126 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_T3_LOM), board_82599 },
127 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_COMBO_BACKPLANE), board_82599 },
128 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540T), board_X540 },
129 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_SF2), board_82599 },
130 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_LS), board_82599 },
131 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_QSFP_SF_QP), board_82599 },
132 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599EN_SFP), board_82599 },
133 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_SF_QP), board_82599 },
134 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540T1), board_X540 },
135 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550T), board_X550},
136 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550T1), board_X550},
137 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_KX4), board_X550EM_x},
138 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_XFI), board_X550EM_x},
139 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_KR), board_X550EM_x},
140 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_10G_T), board_X550EM_x},
141 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_SFP), board_X550EM_x},
142 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_1G_T), board_x550em_x_fw},
143 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_KR), board_x550em_a },
144 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_KR_L), board_x550em_a },
145 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_SFP_N), board_x550em_a },
146 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_SGMII), board_x550em_a },
147 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_SGMII_L), board_x550em_a },
148 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_10G_T), board_x550em_a},
149 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_SFP), board_x550em_a },
150 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_1G_T), board_x550em_a_fw },
151 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_1G_T_L), board_x550em_a_fw },
152 	/* required last entry */
153 	{0, }
154 };
155 MODULE_DEVICE_TABLE(pci, ixgbe_pci_tbl);
156 
157 #ifdef CONFIG_IXGBE_DCA
158 static int ixgbe_notify_dca(struct notifier_block *, unsigned long event,
159 			    void *p);
160 static struct notifier_block dca_notifier = {
161 	.notifier_call = ixgbe_notify_dca,
162 	.next          = NULL,
163 	.priority      = 0
164 };
165 #endif
166 
167 #ifdef CONFIG_PCI_IOV
168 static unsigned int max_vfs;
169 module_param(max_vfs, uint, 0);
170 MODULE_PARM_DESC(max_vfs,
171 		 "Maximum number of virtual functions to allocate per physical function - default is zero and maximum value is 63. (Deprecated)");
172 #endif /* CONFIG_PCI_IOV */
173 
174 static unsigned int allow_unsupported_sfp;
175 module_param(allow_unsupported_sfp, uint, 0);
176 MODULE_PARM_DESC(allow_unsupported_sfp,
177 		 "Allow unsupported and untested SFP+ modules on 82599-based adapters");
178 
179 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
180 static int debug = -1;
181 module_param(debug, int, 0);
182 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
183 
184 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
185 MODULE_DESCRIPTION("Intel(R) 10 Gigabit PCI Express Network Driver");
186 MODULE_LICENSE("GPL");
187 MODULE_VERSION(DRV_VERSION);
188 
189 static struct workqueue_struct *ixgbe_wq;
190 
191 static bool ixgbe_check_cfg_remove(struct ixgbe_hw *hw, struct pci_dev *pdev);
192 static void ixgbe_watchdog_link_is_down(struct ixgbe_adapter *);
193 
194 static int ixgbe_read_pci_cfg_word_parent(struct ixgbe_adapter *adapter,
195 					  u32 reg, u16 *value)
196 {
197 	struct pci_dev *parent_dev;
198 	struct pci_bus *parent_bus;
199 
200 	parent_bus = adapter->pdev->bus->parent;
201 	if (!parent_bus)
202 		return -1;
203 
204 	parent_dev = parent_bus->self;
205 	if (!parent_dev)
206 		return -1;
207 
208 	if (!pci_is_pcie(parent_dev))
209 		return -1;
210 
211 	pcie_capability_read_word(parent_dev, reg, value);
212 	if (*value == IXGBE_FAILED_READ_CFG_WORD &&
213 	    ixgbe_check_cfg_remove(&adapter->hw, parent_dev))
214 		return -1;
215 	return 0;
216 }
217 
218 static s32 ixgbe_get_parent_bus_info(struct ixgbe_adapter *adapter)
219 {
220 	struct ixgbe_hw *hw = &adapter->hw;
221 	u16 link_status = 0;
222 	int err;
223 
224 	hw->bus.type = ixgbe_bus_type_pci_express;
225 
226 	/* Get the negotiated link width and speed from PCI config space of the
227 	 * parent, as this device is behind a switch
228 	 */
229 	err = ixgbe_read_pci_cfg_word_parent(adapter, 18, &link_status);
230 
231 	/* assume caller will handle error case */
232 	if (err)
233 		return err;
234 
235 	hw->bus.width = ixgbe_convert_bus_width(link_status);
236 	hw->bus.speed = ixgbe_convert_bus_speed(link_status);
237 
238 	return 0;
239 }
240 
241 /**
242  * ixgbe_check_from_parent - Determine whether PCIe info should come from parent
243  * @hw: hw specific details
244  *
245  * This function is used by probe to determine whether a device's PCI-Express
246  * bandwidth details should be gathered from the parent bus instead of from the
247  * device. Used to ensure that various locations all have the correct device ID
248  * checks.
249  */
250 static inline bool ixgbe_pcie_from_parent(struct ixgbe_hw *hw)
251 {
252 	switch (hw->device_id) {
253 	case IXGBE_DEV_ID_82599_SFP_SF_QP:
254 	case IXGBE_DEV_ID_82599_QSFP_SF_QP:
255 		return true;
256 	default:
257 		return false;
258 	}
259 }
260 
261 static void ixgbe_check_minimum_link(struct ixgbe_adapter *adapter,
262 				     int expected_gts)
263 {
264 	struct ixgbe_hw *hw = &adapter->hw;
265 	int max_gts = 0;
266 	enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
267 	enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
268 	struct pci_dev *pdev;
269 
270 	/* Some devices are not connected over PCIe and thus do not negotiate
271 	 * speed. These devices do not have valid bus info, and thus any report
272 	 * we generate may not be correct.
273 	 */
274 	if (hw->bus.type == ixgbe_bus_type_internal)
275 		return;
276 
277 	/* determine whether to use the parent device */
278 	if (ixgbe_pcie_from_parent(&adapter->hw))
279 		pdev = adapter->pdev->bus->parent->self;
280 	else
281 		pdev = adapter->pdev;
282 
283 	if (pcie_get_minimum_link(pdev, &speed, &width) ||
284 	    speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
285 		e_dev_warn("Unable to determine PCI Express bandwidth.\n");
286 		return;
287 	}
288 
289 	switch (speed) {
290 	case PCIE_SPEED_2_5GT:
291 		/* 8b/10b encoding reduces max throughput by 20% */
292 		max_gts = 2 * width;
293 		break;
294 	case PCIE_SPEED_5_0GT:
295 		/* 8b/10b encoding reduces max throughput by 20% */
296 		max_gts = 4 * width;
297 		break;
298 	case PCIE_SPEED_8_0GT:
299 		/* 128b/130b encoding reduces throughput by less than 2% */
300 		max_gts = 8 * width;
301 		break;
302 	default:
303 		e_dev_warn("Unable to determine PCI Express bandwidth.\n");
304 		return;
305 	}
306 
307 	e_dev_info("PCI Express bandwidth of %dGT/s available\n",
308 		   max_gts);
309 	e_dev_info("(Speed:%s, Width: x%d, Encoding Loss:%s)\n",
310 		   (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
311 		    speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
312 		    speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
313 		    "Unknown"),
314 		   width,
315 		   (speed == PCIE_SPEED_2_5GT ? "20%" :
316 		    speed == PCIE_SPEED_5_0GT ? "20%" :
317 		    speed == PCIE_SPEED_8_0GT ? "<2%" :
318 		    "Unknown"));
319 
320 	if (max_gts < expected_gts) {
321 		e_dev_warn("This is not sufficient for optimal performance of this card.\n");
322 		e_dev_warn("For optimal performance, at least %dGT/s of bandwidth is required.\n",
323 			expected_gts);
324 		e_dev_warn("A slot with more lanes and/or higher speed is suggested.\n");
325 	}
326 }
327 
328 static void ixgbe_service_event_schedule(struct ixgbe_adapter *adapter)
329 {
330 	if (!test_bit(__IXGBE_DOWN, &adapter->state) &&
331 	    !test_bit(__IXGBE_REMOVING, &adapter->state) &&
332 	    !test_and_set_bit(__IXGBE_SERVICE_SCHED, &adapter->state))
333 		queue_work(ixgbe_wq, &adapter->service_task);
334 }
335 
336 static void ixgbe_remove_adapter(struct ixgbe_hw *hw)
337 {
338 	struct ixgbe_adapter *adapter = hw->back;
339 
340 	if (!hw->hw_addr)
341 		return;
342 	hw->hw_addr = NULL;
343 	e_dev_err("Adapter removed\n");
344 	if (test_bit(__IXGBE_SERVICE_INITED, &adapter->state))
345 		ixgbe_service_event_schedule(adapter);
346 }
347 
348 static void ixgbe_check_remove(struct ixgbe_hw *hw, u32 reg)
349 {
350 	u32 value;
351 
352 	/* The following check not only optimizes a bit by not
353 	 * performing a read on the status register when the
354 	 * register just read was a status register read that
355 	 * returned IXGBE_FAILED_READ_REG. It also blocks any
356 	 * potential recursion.
357 	 */
358 	if (reg == IXGBE_STATUS) {
359 		ixgbe_remove_adapter(hw);
360 		return;
361 	}
362 	value = ixgbe_read_reg(hw, IXGBE_STATUS);
363 	if (value == IXGBE_FAILED_READ_REG)
364 		ixgbe_remove_adapter(hw);
365 }
366 
367 /**
368  * ixgbe_read_reg - Read from device register
369  * @hw: hw specific details
370  * @reg: offset of register to read
371  *
372  * Returns : value read or IXGBE_FAILED_READ_REG if removed
373  *
374  * This function is used to read device registers. It checks for device
375  * removal by confirming any read that returns all ones by checking the
376  * status register value for all ones. This function avoids reading from
377  * the hardware if a removal was previously detected in which case it
378  * returns IXGBE_FAILED_READ_REG (all ones).
379  */
380 u32 ixgbe_read_reg(struct ixgbe_hw *hw, u32 reg)
381 {
382 	u8 __iomem *reg_addr = ACCESS_ONCE(hw->hw_addr);
383 	u32 value;
384 
385 	if (ixgbe_removed(reg_addr))
386 		return IXGBE_FAILED_READ_REG;
387 	if (unlikely(hw->phy.nw_mng_if_sel &
388 		     IXGBE_NW_MNG_IF_SEL_ENABLE_10_100M)) {
389 		struct ixgbe_adapter *adapter;
390 		int i;
391 
392 		for (i = 0; i < 200; ++i) {
393 			value = readl(reg_addr + IXGBE_MAC_SGMII_BUSY);
394 			if (likely(!value))
395 				goto writes_completed;
396 			if (value == IXGBE_FAILED_READ_REG) {
397 				ixgbe_remove_adapter(hw);
398 				return IXGBE_FAILED_READ_REG;
399 			}
400 			udelay(5);
401 		}
402 
403 		adapter = hw->back;
404 		e_warn(hw, "register writes incomplete %08x\n", value);
405 	}
406 
407 writes_completed:
408 	value = readl(reg_addr + reg);
409 	if (unlikely(value == IXGBE_FAILED_READ_REG))
410 		ixgbe_check_remove(hw, reg);
411 	return value;
412 }
413 
414 static bool ixgbe_check_cfg_remove(struct ixgbe_hw *hw, struct pci_dev *pdev)
415 {
416 	u16 value;
417 
418 	pci_read_config_word(pdev, PCI_VENDOR_ID, &value);
419 	if (value == IXGBE_FAILED_READ_CFG_WORD) {
420 		ixgbe_remove_adapter(hw);
421 		return true;
422 	}
423 	return false;
424 }
425 
426 u16 ixgbe_read_pci_cfg_word(struct ixgbe_hw *hw, u32 reg)
427 {
428 	struct ixgbe_adapter *adapter = hw->back;
429 	u16 value;
430 
431 	if (ixgbe_removed(hw->hw_addr))
432 		return IXGBE_FAILED_READ_CFG_WORD;
433 	pci_read_config_word(adapter->pdev, reg, &value);
434 	if (value == IXGBE_FAILED_READ_CFG_WORD &&
435 	    ixgbe_check_cfg_remove(hw, adapter->pdev))
436 		return IXGBE_FAILED_READ_CFG_WORD;
437 	return value;
438 }
439 
440 #ifdef CONFIG_PCI_IOV
441 static u32 ixgbe_read_pci_cfg_dword(struct ixgbe_hw *hw, u32 reg)
442 {
443 	struct ixgbe_adapter *adapter = hw->back;
444 	u32 value;
445 
446 	if (ixgbe_removed(hw->hw_addr))
447 		return IXGBE_FAILED_READ_CFG_DWORD;
448 	pci_read_config_dword(adapter->pdev, reg, &value);
449 	if (value == IXGBE_FAILED_READ_CFG_DWORD &&
450 	    ixgbe_check_cfg_remove(hw, adapter->pdev))
451 		return IXGBE_FAILED_READ_CFG_DWORD;
452 	return value;
453 }
454 #endif /* CONFIG_PCI_IOV */
455 
456 void ixgbe_write_pci_cfg_word(struct ixgbe_hw *hw, u32 reg, u16 value)
457 {
458 	struct ixgbe_adapter *adapter = hw->back;
459 
460 	if (ixgbe_removed(hw->hw_addr))
461 		return;
462 	pci_write_config_word(adapter->pdev, reg, value);
463 }
464 
465 static void ixgbe_service_event_complete(struct ixgbe_adapter *adapter)
466 {
467 	BUG_ON(!test_bit(__IXGBE_SERVICE_SCHED, &adapter->state));
468 
469 	/* flush memory to make sure state is correct before next watchdog */
470 	smp_mb__before_atomic();
471 	clear_bit(__IXGBE_SERVICE_SCHED, &adapter->state);
472 }
473 
474 struct ixgbe_reg_info {
475 	u32 ofs;
476 	char *name;
477 };
478 
479 static const struct ixgbe_reg_info ixgbe_reg_info_tbl[] = {
480 
481 	/* General Registers */
482 	{IXGBE_CTRL, "CTRL"},
483 	{IXGBE_STATUS, "STATUS"},
484 	{IXGBE_CTRL_EXT, "CTRL_EXT"},
485 
486 	/* Interrupt Registers */
487 	{IXGBE_EICR, "EICR"},
488 
489 	/* RX Registers */
490 	{IXGBE_SRRCTL(0), "SRRCTL"},
491 	{IXGBE_DCA_RXCTRL(0), "DRXCTL"},
492 	{IXGBE_RDLEN(0), "RDLEN"},
493 	{IXGBE_RDH(0), "RDH"},
494 	{IXGBE_RDT(0), "RDT"},
495 	{IXGBE_RXDCTL(0), "RXDCTL"},
496 	{IXGBE_RDBAL(0), "RDBAL"},
497 	{IXGBE_RDBAH(0), "RDBAH"},
498 
499 	/* TX Registers */
500 	{IXGBE_TDBAL(0), "TDBAL"},
501 	{IXGBE_TDBAH(0), "TDBAH"},
502 	{IXGBE_TDLEN(0), "TDLEN"},
503 	{IXGBE_TDH(0), "TDH"},
504 	{IXGBE_TDT(0), "TDT"},
505 	{IXGBE_TXDCTL(0), "TXDCTL"},
506 
507 	/* List Terminator */
508 	{ .name = NULL }
509 };
510 
511 
512 /*
513  * ixgbe_regdump - register printout routine
514  */
515 static void ixgbe_regdump(struct ixgbe_hw *hw, struct ixgbe_reg_info *reginfo)
516 {
517 	int i;
518 	char rname[16];
519 	u32 regs[64];
520 
521 	switch (reginfo->ofs) {
522 	case IXGBE_SRRCTL(0):
523 		for (i = 0; i < 64; i++)
524 			regs[i] = IXGBE_READ_REG(hw, IXGBE_SRRCTL(i));
525 		break;
526 	case IXGBE_DCA_RXCTRL(0):
527 		for (i = 0; i < 64; i++)
528 			regs[i] = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(i));
529 		break;
530 	case IXGBE_RDLEN(0):
531 		for (i = 0; i < 64; i++)
532 			regs[i] = IXGBE_READ_REG(hw, IXGBE_RDLEN(i));
533 		break;
534 	case IXGBE_RDH(0):
535 		for (i = 0; i < 64; i++)
536 			regs[i] = IXGBE_READ_REG(hw, IXGBE_RDH(i));
537 		break;
538 	case IXGBE_RDT(0):
539 		for (i = 0; i < 64; i++)
540 			regs[i] = IXGBE_READ_REG(hw, IXGBE_RDT(i));
541 		break;
542 	case IXGBE_RXDCTL(0):
543 		for (i = 0; i < 64; i++)
544 			regs[i] = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
545 		break;
546 	case IXGBE_RDBAL(0):
547 		for (i = 0; i < 64; i++)
548 			regs[i] = IXGBE_READ_REG(hw, IXGBE_RDBAL(i));
549 		break;
550 	case IXGBE_RDBAH(0):
551 		for (i = 0; i < 64; i++)
552 			regs[i] = IXGBE_READ_REG(hw, IXGBE_RDBAH(i));
553 		break;
554 	case IXGBE_TDBAL(0):
555 		for (i = 0; i < 64; i++)
556 			regs[i] = IXGBE_READ_REG(hw, IXGBE_TDBAL(i));
557 		break;
558 	case IXGBE_TDBAH(0):
559 		for (i = 0; i < 64; i++)
560 			regs[i] = IXGBE_READ_REG(hw, IXGBE_TDBAH(i));
561 		break;
562 	case IXGBE_TDLEN(0):
563 		for (i = 0; i < 64; i++)
564 			regs[i] = IXGBE_READ_REG(hw, IXGBE_TDLEN(i));
565 		break;
566 	case IXGBE_TDH(0):
567 		for (i = 0; i < 64; i++)
568 			regs[i] = IXGBE_READ_REG(hw, IXGBE_TDH(i));
569 		break;
570 	case IXGBE_TDT(0):
571 		for (i = 0; i < 64; i++)
572 			regs[i] = IXGBE_READ_REG(hw, IXGBE_TDT(i));
573 		break;
574 	case IXGBE_TXDCTL(0):
575 		for (i = 0; i < 64; i++)
576 			regs[i] = IXGBE_READ_REG(hw, IXGBE_TXDCTL(i));
577 		break;
578 	default:
579 		pr_info("%-15s %08x\n",
580 			reginfo->name, IXGBE_READ_REG(hw, reginfo->ofs));
581 		return;
582 	}
583 
584 	i = 0;
585 	while (i < 64) {
586 		int j;
587 		char buf[9 * 8 + 1];
588 		char *p = buf;
589 
590 		snprintf(rname, 16, "%s[%d-%d]", reginfo->name, i, i + 7);
591 		for (j = 0; j < 8; j++)
592 			p += sprintf(p, " %08x", regs[i++]);
593 		pr_err("%-15s%s\n", rname, buf);
594 	}
595 
596 }
597 
598 static void ixgbe_print_buffer(struct ixgbe_ring *ring, int n)
599 {
600 	struct ixgbe_tx_buffer *tx_buffer;
601 
602 	tx_buffer = &ring->tx_buffer_info[ring->next_to_clean];
603 	pr_info(" %5d %5X %5X %016llX %08X %p %016llX\n",
604 		n, ring->next_to_use, ring->next_to_clean,
605 		(u64)dma_unmap_addr(tx_buffer, dma),
606 		dma_unmap_len(tx_buffer, len),
607 		tx_buffer->next_to_watch,
608 		(u64)tx_buffer->time_stamp);
609 }
610 
611 /*
612  * ixgbe_dump - Print registers, tx-rings and rx-rings
613  */
614 static void ixgbe_dump(struct ixgbe_adapter *adapter)
615 {
616 	struct net_device *netdev = adapter->netdev;
617 	struct ixgbe_hw *hw = &adapter->hw;
618 	struct ixgbe_reg_info *reginfo;
619 	int n = 0;
620 	struct ixgbe_ring *ring;
621 	struct ixgbe_tx_buffer *tx_buffer;
622 	union ixgbe_adv_tx_desc *tx_desc;
623 	struct my_u0 { u64 a; u64 b; } *u0;
624 	struct ixgbe_ring *rx_ring;
625 	union ixgbe_adv_rx_desc *rx_desc;
626 	struct ixgbe_rx_buffer *rx_buffer_info;
627 	int i = 0;
628 
629 	if (!netif_msg_hw(adapter))
630 		return;
631 
632 	/* Print netdevice Info */
633 	if (netdev) {
634 		dev_info(&adapter->pdev->dev, "Net device Info\n");
635 		pr_info("Device Name     state            "
636 			"trans_start\n");
637 		pr_info("%-15s %016lX %016lX\n",
638 			netdev->name,
639 			netdev->state,
640 			dev_trans_start(netdev));
641 	}
642 
643 	/* Print Registers */
644 	dev_info(&adapter->pdev->dev, "Register Dump\n");
645 	pr_info(" Register Name   Value\n");
646 	for (reginfo = (struct ixgbe_reg_info *)ixgbe_reg_info_tbl;
647 	     reginfo->name; reginfo++) {
648 		ixgbe_regdump(hw, reginfo);
649 	}
650 
651 	/* Print TX Ring Summary */
652 	if (!netdev || !netif_running(netdev))
653 		return;
654 
655 	dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
656 	pr_info(" %s     %s              %s        %s\n",
657 		"Queue [NTU] [NTC] [bi(ntc)->dma  ]",
658 		"leng", "ntw", "timestamp");
659 	for (n = 0; n < adapter->num_tx_queues; n++) {
660 		ring = adapter->tx_ring[n];
661 		ixgbe_print_buffer(ring, n);
662 	}
663 
664 	for (n = 0; n < adapter->num_xdp_queues; n++) {
665 		ring = adapter->xdp_ring[n];
666 		ixgbe_print_buffer(ring, n);
667 	}
668 
669 	/* Print TX Rings */
670 	if (!netif_msg_tx_done(adapter))
671 		goto rx_ring_summary;
672 
673 	dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
674 
675 	/* Transmit Descriptor Formats
676 	 *
677 	 * 82598 Advanced Transmit Descriptor
678 	 *   +--------------------------------------------------------------+
679 	 * 0 |         Buffer Address [63:0]                                |
680 	 *   +--------------------------------------------------------------+
681 	 * 8 |  PAYLEN  | POPTS  | IDX | STA | DCMD  |DTYP |  RSV |  DTALEN |
682 	 *   +--------------------------------------------------------------+
683 	 *   63       46 45    40 39 36 35 32 31   24 23 20 19              0
684 	 *
685 	 * 82598 Advanced Transmit Descriptor (Write-Back Format)
686 	 *   +--------------------------------------------------------------+
687 	 * 0 |                          RSV [63:0]                          |
688 	 *   +--------------------------------------------------------------+
689 	 * 8 |            RSV           |  STA  |          NXTSEQ           |
690 	 *   +--------------------------------------------------------------+
691 	 *   63                       36 35   32 31                         0
692 	 *
693 	 * 82599+ Advanced Transmit Descriptor
694 	 *   +--------------------------------------------------------------+
695 	 * 0 |         Buffer Address [63:0]                                |
696 	 *   +--------------------------------------------------------------+
697 	 * 8 |PAYLEN  |POPTS|CC|IDX  |STA  |DCMD  |DTYP |MAC  |RSV  |DTALEN |
698 	 *   +--------------------------------------------------------------+
699 	 *   63     46 45 40 39 38 36 35 32 31  24 23 20 19 18 17 16 15     0
700 	 *
701 	 * 82599+ Advanced Transmit Descriptor (Write-Back Format)
702 	 *   +--------------------------------------------------------------+
703 	 * 0 |                          RSV [63:0]                          |
704 	 *   +--------------------------------------------------------------+
705 	 * 8 |            RSV           |  STA  |           RSV             |
706 	 *   +--------------------------------------------------------------+
707 	 *   63                       36 35   32 31                         0
708 	 */
709 
710 	for (n = 0; n < adapter->num_tx_queues; n++) {
711 		ring = adapter->tx_ring[n];
712 		pr_info("------------------------------------\n");
713 		pr_info("TX QUEUE INDEX = %d\n", ring->queue_index);
714 		pr_info("------------------------------------\n");
715 		pr_info("%s%s    %s              %s        %s          %s\n",
716 			"T [desc]     [address 63:0  ] ",
717 			"[PlPOIdStDDt Ln] [bi->dma       ] ",
718 			"leng", "ntw", "timestamp", "bi->skb");
719 
720 		for (i = 0; ring->desc && (i < ring->count); i++) {
721 			tx_desc = IXGBE_TX_DESC(ring, i);
722 			tx_buffer = &ring->tx_buffer_info[i];
723 			u0 = (struct my_u0 *)tx_desc;
724 			if (dma_unmap_len(tx_buffer, len) > 0) {
725 				const char *ring_desc;
726 
727 				if (i == ring->next_to_use &&
728 				    i == ring->next_to_clean)
729 					ring_desc = " NTC/U";
730 				else if (i == ring->next_to_use)
731 					ring_desc = " NTU";
732 				else if (i == ring->next_to_clean)
733 					ring_desc = " NTC";
734 				else
735 					ring_desc = "";
736 				pr_info("T [0x%03X]    %016llX %016llX %016llX %08X %p %016llX %p%s",
737 					i,
738 					le64_to_cpu(u0->a),
739 					le64_to_cpu(u0->b),
740 					(u64)dma_unmap_addr(tx_buffer, dma),
741 					dma_unmap_len(tx_buffer, len),
742 					tx_buffer->next_to_watch,
743 					(u64)tx_buffer->time_stamp,
744 					tx_buffer->skb,
745 					ring_desc);
746 
747 				if (netif_msg_pktdata(adapter) &&
748 				    tx_buffer->skb)
749 					print_hex_dump(KERN_INFO, "",
750 						DUMP_PREFIX_ADDRESS, 16, 1,
751 						tx_buffer->skb->data,
752 						dma_unmap_len(tx_buffer, len),
753 						true);
754 			}
755 		}
756 	}
757 
758 	/* Print RX Rings Summary */
759 rx_ring_summary:
760 	dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
761 	pr_info("Queue [NTU] [NTC]\n");
762 	for (n = 0; n < adapter->num_rx_queues; n++) {
763 		rx_ring = adapter->rx_ring[n];
764 		pr_info("%5d %5X %5X\n",
765 			n, rx_ring->next_to_use, rx_ring->next_to_clean);
766 	}
767 
768 	/* Print RX Rings */
769 	if (!netif_msg_rx_status(adapter))
770 		return;
771 
772 	dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
773 
774 	/* Receive Descriptor Formats
775 	 *
776 	 * 82598 Advanced Receive Descriptor (Read) Format
777 	 *    63                                           1        0
778 	 *    +-----------------------------------------------------+
779 	 *  0 |       Packet Buffer Address [63:1]           |A0/NSE|
780 	 *    +----------------------------------------------+------+
781 	 *  8 |       Header Buffer Address [63:1]           |  DD  |
782 	 *    +-----------------------------------------------------+
783 	 *
784 	 *
785 	 * 82598 Advanced Receive Descriptor (Write-Back) Format
786 	 *
787 	 *   63       48 47    32 31  30      21 20 16 15   4 3     0
788 	 *   +------------------------------------------------------+
789 	 * 0 |       RSS Hash /  |SPH| HDR_LEN  | RSV |Packet|  RSS |
790 	 *   | Packet   | IP     |   |          |     | Type | Type |
791 	 *   | Checksum | Ident  |   |          |     |      |      |
792 	 *   +------------------------------------------------------+
793 	 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
794 	 *   +------------------------------------------------------+
795 	 *   63       48 47    32 31            20 19               0
796 	 *
797 	 * 82599+ Advanced Receive Descriptor (Read) Format
798 	 *    63                                           1        0
799 	 *    +-----------------------------------------------------+
800 	 *  0 |       Packet Buffer Address [63:1]           |A0/NSE|
801 	 *    +----------------------------------------------+------+
802 	 *  8 |       Header Buffer Address [63:1]           |  DD  |
803 	 *    +-----------------------------------------------------+
804 	 *
805 	 *
806 	 * 82599+ Advanced Receive Descriptor (Write-Back) Format
807 	 *
808 	 *   63       48 47    32 31  30      21 20 17 16   4 3     0
809 	 *   +------------------------------------------------------+
810 	 * 0 |RSS / Frag Checksum|SPH| HDR_LEN  |RSC- |Packet|  RSS |
811 	 *   |/ RTT / PCoE_PARAM |   |          | CNT | Type | Type |
812 	 *   |/ Flow Dir Flt ID  |   |          |     |      |      |
813 	 *   +------------------------------------------------------+
814 	 * 8 | VLAN Tag | Length |Extended Error| Xtnd Status/NEXTP |
815 	 *   +------------------------------------------------------+
816 	 *   63       48 47    32 31          20 19                 0
817 	 */
818 
819 	for (n = 0; n < adapter->num_rx_queues; n++) {
820 		rx_ring = adapter->rx_ring[n];
821 		pr_info("------------------------------------\n");
822 		pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
823 		pr_info("------------------------------------\n");
824 		pr_info("%s%s%s\n",
825 			"R  [desc]      [ PktBuf     A0] ",
826 			"[  HeadBuf   DD] [bi->dma       ] [bi->skb       ] ",
827 			"<-- Adv Rx Read format");
828 		pr_info("%s%s%s\n",
829 			"RWB[desc]      [PcsmIpSHl PtRs] ",
830 			"[vl er S cks ln] ---------------- [bi->skb       ] ",
831 			"<-- Adv Rx Write-Back format");
832 
833 		for (i = 0; i < rx_ring->count; i++) {
834 			const char *ring_desc;
835 
836 			if (i == rx_ring->next_to_use)
837 				ring_desc = " NTU";
838 			else if (i == rx_ring->next_to_clean)
839 				ring_desc = " NTC";
840 			else
841 				ring_desc = "";
842 
843 			rx_buffer_info = &rx_ring->rx_buffer_info[i];
844 			rx_desc = IXGBE_RX_DESC(rx_ring, i);
845 			u0 = (struct my_u0 *)rx_desc;
846 			if (rx_desc->wb.upper.length) {
847 				/* Descriptor Done */
848 				pr_info("RWB[0x%03X]     %016llX %016llX ---------------- %p%s\n",
849 					i,
850 					le64_to_cpu(u0->a),
851 					le64_to_cpu(u0->b),
852 					rx_buffer_info->skb,
853 					ring_desc);
854 			} else {
855 				pr_info("R  [0x%03X]     %016llX %016llX %016llX %p%s\n",
856 					i,
857 					le64_to_cpu(u0->a),
858 					le64_to_cpu(u0->b),
859 					(u64)rx_buffer_info->dma,
860 					rx_buffer_info->skb,
861 					ring_desc);
862 
863 				if (netif_msg_pktdata(adapter) &&
864 				    rx_buffer_info->dma) {
865 					print_hex_dump(KERN_INFO, "",
866 					   DUMP_PREFIX_ADDRESS, 16, 1,
867 					   page_address(rx_buffer_info->page) +
868 						    rx_buffer_info->page_offset,
869 					   ixgbe_rx_bufsz(rx_ring), true);
870 				}
871 			}
872 		}
873 	}
874 }
875 
876 static void ixgbe_release_hw_control(struct ixgbe_adapter *adapter)
877 {
878 	u32 ctrl_ext;
879 
880 	/* Let firmware take over control of h/w */
881 	ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
882 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
883 			ctrl_ext & ~IXGBE_CTRL_EXT_DRV_LOAD);
884 }
885 
886 static void ixgbe_get_hw_control(struct ixgbe_adapter *adapter)
887 {
888 	u32 ctrl_ext;
889 
890 	/* Let firmware know the driver has taken over */
891 	ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
892 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
893 			ctrl_ext | IXGBE_CTRL_EXT_DRV_LOAD);
894 }
895 
896 /**
897  * ixgbe_set_ivar - set the IVAR registers, mapping interrupt causes to vectors
898  * @adapter: pointer to adapter struct
899  * @direction: 0 for Rx, 1 for Tx, -1 for other causes
900  * @queue: queue to map the corresponding interrupt to
901  * @msix_vector: the vector to map to the corresponding queue
902  *
903  */
904 static void ixgbe_set_ivar(struct ixgbe_adapter *adapter, s8 direction,
905 			   u8 queue, u8 msix_vector)
906 {
907 	u32 ivar, index;
908 	struct ixgbe_hw *hw = &adapter->hw;
909 	switch (hw->mac.type) {
910 	case ixgbe_mac_82598EB:
911 		msix_vector |= IXGBE_IVAR_ALLOC_VAL;
912 		if (direction == -1)
913 			direction = 0;
914 		index = (((direction * 64) + queue) >> 2) & 0x1F;
915 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
916 		ivar &= ~(0xFF << (8 * (queue & 0x3)));
917 		ivar |= (msix_vector << (8 * (queue & 0x3)));
918 		IXGBE_WRITE_REG(hw, IXGBE_IVAR(index), ivar);
919 		break;
920 	case ixgbe_mac_82599EB:
921 	case ixgbe_mac_X540:
922 	case ixgbe_mac_X550:
923 	case ixgbe_mac_X550EM_x:
924 	case ixgbe_mac_x550em_a:
925 		if (direction == -1) {
926 			/* other causes */
927 			msix_vector |= IXGBE_IVAR_ALLOC_VAL;
928 			index = ((queue & 1) * 8);
929 			ivar = IXGBE_READ_REG(&adapter->hw, IXGBE_IVAR_MISC);
930 			ivar &= ~(0xFF << index);
931 			ivar |= (msix_vector << index);
932 			IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR_MISC, ivar);
933 			break;
934 		} else {
935 			/* tx or rx causes */
936 			msix_vector |= IXGBE_IVAR_ALLOC_VAL;
937 			index = ((16 * (queue & 1)) + (8 * direction));
938 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(queue >> 1));
939 			ivar &= ~(0xFF << index);
940 			ivar |= (msix_vector << index);
941 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(queue >> 1), ivar);
942 			break;
943 		}
944 	default:
945 		break;
946 	}
947 }
948 
949 static inline void ixgbe_irq_rearm_queues(struct ixgbe_adapter *adapter,
950 					  u64 qmask)
951 {
952 	u32 mask;
953 
954 	switch (adapter->hw.mac.type) {
955 	case ixgbe_mac_82598EB:
956 		mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
957 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, mask);
958 		break;
959 	case ixgbe_mac_82599EB:
960 	case ixgbe_mac_X540:
961 	case ixgbe_mac_X550:
962 	case ixgbe_mac_X550EM_x:
963 	case ixgbe_mac_x550em_a:
964 		mask = (qmask & 0xFFFFFFFF);
965 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(0), mask);
966 		mask = (qmask >> 32);
967 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(1), mask);
968 		break;
969 	default:
970 		break;
971 	}
972 }
973 
974 static void ixgbe_update_xoff_rx_lfc(struct ixgbe_adapter *adapter)
975 {
976 	struct ixgbe_hw *hw = &adapter->hw;
977 	struct ixgbe_hw_stats *hwstats = &adapter->stats;
978 	int i;
979 	u32 data;
980 
981 	if ((hw->fc.current_mode != ixgbe_fc_full) &&
982 	    (hw->fc.current_mode != ixgbe_fc_rx_pause))
983 		return;
984 
985 	switch (hw->mac.type) {
986 	case ixgbe_mac_82598EB:
987 		data = IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
988 		break;
989 	default:
990 		data = IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
991 	}
992 	hwstats->lxoffrxc += data;
993 
994 	/* refill credits (no tx hang) if we received xoff */
995 	if (!data)
996 		return;
997 
998 	for (i = 0; i < adapter->num_tx_queues; i++)
999 		clear_bit(__IXGBE_HANG_CHECK_ARMED,
1000 			  &adapter->tx_ring[i]->state);
1001 
1002 	for (i = 0; i < adapter->num_xdp_queues; i++)
1003 		clear_bit(__IXGBE_HANG_CHECK_ARMED,
1004 			  &adapter->xdp_ring[i]->state);
1005 }
1006 
1007 static void ixgbe_update_xoff_received(struct ixgbe_adapter *adapter)
1008 {
1009 	struct ixgbe_hw *hw = &adapter->hw;
1010 	struct ixgbe_hw_stats *hwstats = &adapter->stats;
1011 	u32 xoff[8] = {0};
1012 	u8 tc;
1013 	int i;
1014 	bool pfc_en = adapter->dcb_cfg.pfc_mode_enable;
1015 
1016 	if (adapter->ixgbe_ieee_pfc)
1017 		pfc_en |= !!(adapter->ixgbe_ieee_pfc->pfc_en);
1018 
1019 	if (!(adapter->flags & IXGBE_FLAG_DCB_ENABLED) || !pfc_en) {
1020 		ixgbe_update_xoff_rx_lfc(adapter);
1021 		return;
1022 	}
1023 
1024 	/* update stats for each tc, only valid with PFC enabled */
1025 	for (i = 0; i < MAX_TX_PACKET_BUFFERS; i++) {
1026 		u32 pxoffrxc;
1027 
1028 		switch (hw->mac.type) {
1029 		case ixgbe_mac_82598EB:
1030 			pxoffrxc = IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
1031 			break;
1032 		default:
1033 			pxoffrxc = IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
1034 		}
1035 		hwstats->pxoffrxc[i] += pxoffrxc;
1036 		/* Get the TC for given UP */
1037 		tc = netdev_get_prio_tc_map(adapter->netdev, i);
1038 		xoff[tc] += pxoffrxc;
1039 	}
1040 
1041 	/* disarm tx queues that have received xoff frames */
1042 	for (i = 0; i < adapter->num_tx_queues; i++) {
1043 		struct ixgbe_ring *tx_ring = adapter->tx_ring[i];
1044 
1045 		tc = tx_ring->dcb_tc;
1046 		if (xoff[tc])
1047 			clear_bit(__IXGBE_HANG_CHECK_ARMED, &tx_ring->state);
1048 	}
1049 
1050 	for (i = 0; i < adapter->num_xdp_queues; i++) {
1051 		struct ixgbe_ring *xdp_ring = adapter->xdp_ring[i];
1052 
1053 		tc = xdp_ring->dcb_tc;
1054 		if (xoff[tc])
1055 			clear_bit(__IXGBE_HANG_CHECK_ARMED, &xdp_ring->state);
1056 	}
1057 }
1058 
1059 static u64 ixgbe_get_tx_completed(struct ixgbe_ring *ring)
1060 {
1061 	return ring->stats.packets;
1062 }
1063 
1064 static u64 ixgbe_get_tx_pending(struct ixgbe_ring *ring)
1065 {
1066 	struct ixgbe_adapter *adapter;
1067 	struct ixgbe_hw *hw;
1068 	u32 head, tail;
1069 
1070 	if (ring->l2_accel_priv)
1071 		adapter = ring->l2_accel_priv->real_adapter;
1072 	else
1073 		adapter = netdev_priv(ring->netdev);
1074 
1075 	hw = &adapter->hw;
1076 	head = IXGBE_READ_REG(hw, IXGBE_TDH(ring->reg_idx));
1077 	tail = IXGBE_READ_REG(hw, IXGBE_TDT(ring->reg_idx));
1078 
1079 	if (head != tail)
1080 		return (head < tail) ?
1081 			tail - head : (tail + ring->count - head);
1082 
1083 	return 0;
1084 }
1085 
1086 static inline bool ixgbe_check_tx_hang(struct ixgbe_ring *tx_ring)
1087 {
1088 	u32 tx_done = ixgbe_get_tx_completed(tx_ring);
1089 	u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
1090 	u32 tx_pending = ixgbe_get_tx_pending(tx_ring);
1091 
1092 	clear_check_for_tx_hang(tx_ring);
1093 
1094 	/*
1095 	 * Check for a hung queue, but be thorough. This verifies
1096 	 * that a transmit has been completed since the previous
1097 	 * check AND there is at least one packet pending. The
1098 	 * ARMED bit is set to indicate a potential hang. The
1099 	 * bit is cleared if a pause frame is received to remove
1100 	 * false hang detection due to PFC or 802.3x frames. By
1101 	 * requiring this to fail twice we avoid races with
1102 	 * pfc clearing the ARMED bit and conditions where we
1103 	 * run the check_tx_hang logic with a transmit completion
1104 	 * pending but without time to complete it yet.
1105 	 */
1106 	if (tx_done_old == tx_done && tx_pending)
1107 		/* make sure it is true for two checks in a row */
1108 		return test_and_set_bit(__IXGBE_HANG_CHECK_ARMED,
1109 					&tx_ring->state);
1110 	/* update completed stats and continue */
1111 	tx_ring->tx_stats.tx_done_old = tx_done;
1112 	/* reset the countdown */
1113 	clear_bit(__IXGBE_HANG_CHECK_ARMED, &tx_ring->state);
1114 
1115 	return false;
1116 }
1117 
1118 /**
1119  * ixgbe_tx_timeout_reset - initiate reset due to Tx timeout
1120  * @adapter: driver private struct
1121  **/
1122 static void ixgbe_tx_timeout_reset(struct ixgbe_adapter *adapter)
1123 {
1124 
1125 	/* Do the reset outside of interrupt context */
1126 	if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
1127 		set_bit(__IXGBE_RESET_REQUESTED, &adapter->state);
1128 		e_warn(drv, "initiating reset due to tx timeout\n");
1129 		ixgbe_service_event_schedule(adapter);
1130 	}
1131 }
1132 
1133 /**
1134  * ixgbe_tx_maxrate - callback to set the maximum per-queue bitrate
1135  **/
1136 static int ixgbe_tx_maxrate(struct net_device *netdev,
1137 			    int queue_index, u32 maxrate)
1138 {
1139 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
1140 	struct ixgbe_hw *hw = &adapter->hw;
1141 	u32 bcnrc_val = ixgbe_link_mbps(adapter);
1142 
1143 	if (!maxrate)
1144 		return 0;
1145 
1146 	/* Calculate the rate factor values to set */
1147 	bcnrc_val <<= IXGBE_RTTBCNRC_RF_INT_SHIFT;
1148 	bcnrc_val /= maxrate;
1149 
1150 	/* clear everything but the rate factor */
1151 	bcnrc_val &= IXGBE_RTTBCNRC_RF_INT_MASK |
1152 	IXGBE_RTTBCNRC_RF_DEC_MASK;
1153 
1154 	/* enable the rate scheduler */
1155 	bcnrc_val |= IXGBE_RTTBCNRC_RS_ENA;
1156 
1157 	IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, queue_index);
1158 	IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
1159 
1160 	return 0;
1161 }
1162 
1163 /**
1164  * ixgbe_clean_tx_irq - Reclaim resources after transmit completes
1165  * @q_vector: structure containing interrupt and ring information
1166  * @tx_ring: tx ring to clean
1167  * @napi_budget: Used to determine if we are in netpoll
1168  **/
1169 static bool ixgbe_clean_tx_irq(struct ixgbe_q_vector *q_vector,
1170 			       struct ixgbe_ring *tx_ring, int napi_budget)
1171 {
1172 	struct ixgbe_adapter *adapter = q_vector->adapter;
1173 	struct ixgbe_tx_buffer *tx_buffer;
1174 	union ixgbe_adv_tx_desc *tx_desc;
1175 	unsigned int total_bytes = 0, total_packets = 0;
1176 	unsigned int budget = q_vector->tx.work_limit;
1177 	unsigned int i = tx_ring->next_to_clean;
1178 
1179 	if (test_bit(__IXGBE_DOWN, &adapter->state))
1180 		return true;
1181 
1182 	tx_buffer = &tx_ring->tx_buffer_info[i];
1183 	tx_desc = IXGBE_TX_DESC(tx_ring, i);
1184 	i -= tx_ring->count;
1185 
1186 	do {
1187 		union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
1188 
1189 		/* if next_to_watch is not set then there is no work pending */
1190 		if (!eop_desc)
1191 			break;
1192 
1193 		/* prevent any other reads prior to eop_desc */
1194 		read_barrier_depends();
1195 
1196 		/* if DD is not set pending work has not been completed */
1197 		if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
1198 			break;
1199 
1200 		/* clear next_to_watch to prevent false hangs */
1201 		tx_buffer->next_to_watch = NULL;
1202 
1203 		/* update the statistics for this packet */
1204 		total_bytes += tx_buffer->bytecount;
1205 		total_packets += tx_buffer->gso_segs;
1206 
1207 		/* free the skb */
1208 		if (ring_is_xdp(tx_ring))
1209 			page_frag_free(tx_buffer->data);
1210 		else
1211 			napi_consume_skb(tx_buffer->skb, napi_budget);
1212 
1213 		/* unmap skb header data */
1214 		dma_unmap_single(tx_ring->dev,
1215 				 dma_unmap_addr(tx_buffer, dma),
1216 				 dma_unmap_len(tx_buffer, len),
1217 				 DMA_TO_DEVICE);
1218 
1219 		/* clear tx_buffer data */
1220 		dma_unmap_len_set(tx_buffer, len, 0);
1221 
1222 		/* unmap remaining buffers */
1223 		while (tx_desc != eop_desc) {
1224 			tx_buffer++;
1225 			tx_desc++;
1226 			i++;
1227 			if (unlikely(!i)) {
1228 				i -= tx_ring->count;
1229 				tx_buffer = tx_ring->tx_buffer_info;
1230 				tx_desc = IXGBE_TX_DESC(tx_ring, 0);
1231 			}
1232 
1233 			/* unmap any remaining paged data */
1234 			if (dma_unmap_len(tx_buffer, len)) {
1235 				dma_unmap_page(tx_ring->dev,
1236 					       dma_unmap_addr(tx_buffer, dma),
1237 					       dma_unmap_len(tx_buffer, len),
1238 					       DMA_TO_DEVICE);
1239 				dma_unmap_len_set(tx_buffer, len, 0);
1240 			}
1241 		}
1242 
1243 		/* move us one more past the eop_desc for start of next pkt */
1244 		tx_buffer++;
1245 		tx_desc++;
1246 		i++;
1247 		if (unlikely(!i)) {
1248 			i -= tx_ring->count;
1249 			tx_buffer = tx_ring->tx_buffer_info;
1250 			tx_desc = IXGBE_TX_DESC(tx_ring, 0);
1251 		}
1252 
1253 		/* issue prefetch for next Tx descriptor */
1254 		prefetch(tx_desc);
1255 
1256 		/* update budget accounting */
1257 		budget--;
1258 	} while (likely(budget));
1259 
1260 	i += tx_ring->count;
1261 	tx_ring->next_to_clean = i;
1262 	u64_stats_update_begin(&tx_ring->syncp);
1263 	tx_ring->stats.bytes += total_bytes;
1264 	tx_ring->stats.packets += total_packets;
1265 	u64_stats_update_end(&tx_ring->syncp);
1266 	q_vector->tx.total_bytes += total_bytes;
1267 	q_vector->tx.total_packets += total_packets;
1268 
1269 	if (check_for_tx_hang(tx_ring) && ixgbe_check_tx_hang(tx_ring)) {
1270 		/* schedule immediate reset if we believe we hung */
1271 		struct ixgbe_hw *hw = &adapter->hw;
1272 		e_err(drv, "Detected Tx Unit Hang %s\n"
1273 			"  Tx Queue             <%d>\n"
1274 			"  TDH, TDT             <%x>, <%x>\n"
1275 			"  next_to_use          <%x>\n"
1276 			"  next_to_clean        <%x>\n"
1277 			"tx_buffer_info[next_to_clean]\n"
1278 			"  time_stamp           <%lx>\n"
1279 			"  jiffies              <%lx>\n",
1280 			ring_is_xdp(tx_ring) ? "(XDP)" : "",
1281 			tx_ring->queue_index,
1282 			IXGBE_READ_REG(hw, IXGBE_TDH(tx_ring->reg_idx)),
1283 			IXGBE_READ_REG(hw, IXGBE_TDT(tx_ring->reg_idx)),
1284 			tx_ring->next_to_use, i,
1285 			tx_ring->tx_buffer_info[i].time_stamp, jiffies);
1286 
1287 		if (!ring_is_xdp(tx_ring))
1288 			netif_stop_subqueue(tx_ring->netdev,
1289 					    tx_ring->queue_index);
1290 
1291 		e_info(probe,
1292 		       "tx hang %d detected on queue %d, resetting adapter\n",
1293 			adapter->tx_timeout_count + 1, tx_ring->queue_index);
1294 
1295 		/* schedule immediate reset if we believe we hung */
1296 		ixgbe_tx_timeout_reset(adapter);
1297 
1298 		/* the adapter is about to reset, no point in enabling stuff */
1299 		return true;
1300 	}
1301 
1302 	if (ring_is_xdp(tx_ring))
1303 		return !!budget;
1304 
1305 	netdev_tx_completed_queue(txring_txq(tx_ring),
1306 				  total_packets, total_bytes);
1307 
1308 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
1309 	if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
1310 		     (ixgbe_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
1311 		/* Make sure that anybody stopping the queue after this
1312 		 * sees the new next_to_clean.
1313 		 */
1314 		smp_mb();
1315 		if (__netif_subqueue_stopped(tx_ring->netdev,
1316 					     tx_ring->queue_index)
1317 		    && !test_bit(__IXGBE_DOWN, &adapter->state)) {
1318 			netif_wake_subqueue(tx_ring->netdev,
1319 					    tx_ring->queue_index);
1320 			++tx_ring->tx_stats.restart_queue;
1321 		}
1322 	}
1323 
1324 	return !!budget;
1325 }
1326 
1327 #ifdef CONFIG_IXGBE_DCA
1328 static void ixgbe_update_tx_dca(struct ixgbe_adapter *adapter,
1329 				struct ixgbe_ring *tx_ring,
1330 				int cpu)
1331 {
1332 	struct ixgbe_hw *hw = &adapter->hw;
1333 	u32 txctrl = 0;
1334 	u16 reg_offset;
1335 
1336 	if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1337 		txctrl = dca3_get_tag(tx_ring->dev, cpu);
1338 
1339 	switch (hw->mac.type) {
1340 	case ixgbe_mac_82598EB:
1341 		reg_offset = IXGBE_DCA_TXCTRL(tx_ring->reg_idx);
1342 		break;
1343 	case ixgbe_mac_82599EB:
1344 	case ixgbe_mac_X540:
1345 		reg_offset = IXGBE_DCA_TXCTRL_82599(tx_ring->reg_idx);
1346 		txctrl <<= IXGBE_DCA_TXCTRL_CPUID_SHIFT_82599;
1347 		break;
1348 	default:
1349 		/* for unknown hardware do not write register */
1350 		return;
1351 	}
1352 
1353 	/*
1354 	 * We can enable relaxed ordering for reads, but not writes when
1355 	 * DCA is enabled.  This is due to a known issue in some chipsets
1356 	 * which will cause the DCA tag to be cleared.
1357 	 */
1358 	txctrl |= IXGBE_DCA_TXCTRL_DESC_RRO_EN |
1359 		  IXGBE_DCA_TXCTRL_DATA_RRO_EN |
1360 		  IXGBE_DCA_TXCTRL_DESC_DCA_EN;
1361 
1362 	IXGBE_WRITE_REG(hw, reg_offset, txctrl);
1363 }
1364 
1365 static void ixgbe_update_rx_dca(struct ixgbe_adapter *adapter,
1366 				struct ixgbe_ring *rx_ring,
1367 				int cpu)
1368 {
1369 	struct ixgbe_hw *hw = &adapter->hw;
1370 	u32 rxctrl = 0;
1371 	u8 reg_idx = rx_ring->reg_idx;
1372 
1373 	if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1374 		rxctrl = dca3_get_tag(rx_ring->dev, cpu);
1375 
1376 	switch (hw->mac.type) {
1377 	case ixgbe_mac_82599EB:
1378 	case ixgbe_mac_X540:
1379 		rxctrl <<= IXGBE_DCA_RXCTRL_CPUID_SHIFT_82599;
1380 		break;
1381 	default:
1382 		break;
1383 	}
1384 
1385 	/*
1386 	 * We can enable relaxed ordering for reads, but not writes when
1387 	 * DCA is enabled.  This is due to a known issue in some chipsets
1388 	 * which will cause the DCA tag to be cleared.
1389 	 */
1390 	rxctrl |= IXGBE_DCA_RXCTRL_DESC_RRO_EN |
1391 		  IXGBE_DCA_RXCTRL_DATA_DCA_EN |
1392 		  IXGBE_DCA_RXCTRL_DESC_DCA_EN;
1393 
1394 	IXGBE_WRITE_REG(hw, IXGBE_DCA_RXCTRL(reg_idx), rxctrl);
1395 }
1396 
1397 static void ixgbe_update_dca(struct ixgbe_q_vector *q_vector)
1398 {
1399 	struct ixgbe_adapter *adapter = q_vector->adapter;
1400 	struct ixgbe_ring *ring;
1401 	int cpu = get_cpu();
1402 
1403 	if (q_vector->cpu == cpu)
1404 		goto out_no_update;
1405 
1406 	ixgbe_for_each_ring(ring, q_vector->tx)
1407 		ixgbe_update_tx_dca(adapter, ring, cpu);
1408 
1409 	ixgbe_for_each_ring(ring, q_vector->rx)
1410 		ixgbe_update_rx_dca(adapter, ring, cpu);
1411 
1412 	q_vector->cpu = cpu;
1413 out_no_update:
1414 	put_cpu();
1415 }
1416 
1417 static void ixgbe_setup_dca(struct ixgbe_adapter *adapter)
1418 {
1419 	int i;
1420 
1421 	/* always use CB2 mode, difference is masked in the CB driver */
1422 	if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1423 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL,
1424 				IXGBE_DCA_CTRL_DCA_MODE_CB2);
1425 	else
1426 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL,
1427 				IXGBE_DCA_CTRL_DCA_DISABLE);
1428 
1429 	for (i = 0; i < adapter->num_q_vectors; i++) {
1430 		adapter->q_vector[i]->cpu = -1;
1431 		ixgbe_update_dca(adapter->q_vector[i]);
1432 	}
1433 }
1434 
1435 static int __ixgbe_notify_dca(struct device *dev, void *data)
1436 {
1437 	struct ixgbe_adapter *adapter = dev_get_drvdata(dev);
1438 	unsigned long event = *(unsigned long *)data;
1439 
1440 	if (!(adapter->flags & IXGBE_FLAG_DCA_CAPABLE))
1441 		return 0;
1442 
1443 	switch (event) {
1444 	case DCA_PROVIDER_ADD:
1445 		/* if we're already enabled, don't do it again */
1446 		if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1447 			break;
1448 		if (dca_add_requester(dev) == 0) {
1449 			adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
1450 			IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL,
1451 					IXGBE_DCA_CTRL_DCA_MODE_CB2);
1452 			break;
1453 		}
1454 		/* Fall Through since DCA is disabled. */
1455 	case DCA_PROVIDER_REMOVE:
1456 		if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
1457 			dca_remove_requester(dev);
1458 			adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
1459 			IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL,
1460 					IXGBE_DCA_CTRL_DCA_DISABLE);
1461 		}
1462 		break;
1463 	}
1464 
1465 	return 0;
1466 }
1467 
1468 #endif /* CONFIG_IXGBE_DCA */
1469 
1470 #define IXGBE_RSS_L4_TYPES_MASK \
1471 	((1ul << IXGBE_RXDADV_RSSTYPE_IPV4_TCP) | \
1472 	 (1ul << IXGBE_RXDADV_RSSTYPE_IPV4_UDP) | \
1473 	 (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_TCP) | \
1474 	 (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_UDP))
1475 
1476 static inline void ixgbe_rx_hash(struct ixgbe_ring *ring,
1477 				 union ixgbe_adv_rx_desc *rx_desc,
1478 				 struct sk_buff *skb)
1479 {
1480 	u16 rss_type;
1481 
1482 	if (!(ring->netdev->features & NETIF_F_RXHASH))
1483 		return;
1484 
1485 	rss_type = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info) &
1486 		   IXGBE_RXDADV_RSSTYPE_MASK;
1487 
1488 	if (!rss_type)
1489 		return;
1490 
1491 	skb_set_hash(skb, le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
1492 		     (IXGBE_RSS_L4_TYPES_MASK & (1ul << rss_type)) ?
1493 		     PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
1494 }
1495 
1496 #ifdef IXGBE_FCOE
1497 /**
1498  * ixgbe_rx_is_fcoe - check the rx desc for incoming pkt type
1499  * @ring: structure containing ring specific data
1500  * @rx_desc: advanced rx descriptor
1501  *
1502  * Returns : true if it is FCoE pkt
1503  */
1504 static inline bool ixgbe_rx_is_fcoe(struct ixgbe_ring *ring,
1505 				    union ixgbe_adv_rx_desc *rx_desc)
1506 {
1507 	__le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
1508 
1509 	return test_bit(__IXGBE_RX_FCOE, &ring->state) &&
1510 	       ((pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_ETQF_MASK)) ==
1511 		(cpu_to_le16(IXGBE_ETQF_FILTER_FCOE <<
1512 			     IXGBE_RXDADV_PKTTYPE_ETQF_SHIFT)));
1513 }
1514 
1515 #endif /* IXGBE_FCOE */
1516 /**
1517  * ixgbe_rx_checksum - indicate in skb if hw indicated a good cksum
1518  * @ring: structure containing ring specific data
1519  * @rx_desc: current Rx descriptor being processed
1520  * @skb: skb currently being received and modified
1521  **/
1522 static inline void ixgbe_rx_checksum(struct ixgbe_ring *ring,
1523 				     union ixgbe_adv_rx_desc *rx_desc,
1524 				     struct sk_buff *skb)
1525 {
1526 	__le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
1527 	bool encap_pkt = false;
1528 
1529 	skb_checksum_none_assert(skb);
1530 
1531 	/* Rx csum disabled */
1532 	if (!(ring->netdev->features & NETIF_F_RXCSUM))
1533 		return;
1534 
1535 	/* check for VXLAN and Geneve packets */
1536 	if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_VXLAN)) {
1537 		encap_pkt = true;
1538 		skb->encapsulation = 1;
1539 	}
1540 
1541 	/* if IP and error */
1542 	if (ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_IPCS) &&
1543 	    ixgbe_test_staterr(rx_desc, IXGBE_RXDADV_ERR_IPE)) {
1544 		ring->rx_stats.csum_err++;
1545 		return;
1546 	}
1547 
1548 	if (!ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
1549 		return;
1550 
1551 	if (ixgbe_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
1552 		/*
1553 		 * 82599 errata, UDP frames with a 0 checksum can be marked as
1554 		 * checksum errors.
1555 		 */
1556 		if ((pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_UDP)) &&
1557 		    test_bit(__IXGBE_RX_CSUM_UDP_ZERO_ERR, &ring->state))
1558 			return;
1559 
1560 		ring->rx_stats.csum_err++;
1561 		return;
1562 	}
1563 
1564 	/* It must be a TCP or UDP packet with a valid checksum */
1565 	skb->ip_summed = CHECKSUM_UNNECESSARY;
1566 	if (encap_pkt) {
1567 		if (!ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_OUTERIPCS))
1568 			return;
1569 
1570 		if (ixgbe_test_staterr(rx_desc, IXGBE_RXDADV_ERR_OUTERIPER)) {
1571 			skb->ip_summed = CHECKSUM_NONE;
1572 			return;
1573 		}
1574 		/* If we checked the outer header let the stack know */
1575 		skb->csum_level = 1;
1576 	}
1577 }
1578 
1579 static inline unsigned int ixgbe_rx_offset(struct ixgbe_ring *rx_ring)
1580 {
1581 	return ring_uses_build_skb(rx_ring) ? IXGBE_SKB_PAD : 0;
1582 }
1583 
1584 static bool ixgbe_alloc_mapped_page(struct ixgbe_ring *rx_ring,
1585 				    struct ixgbe_rx_buffer *bi)
1586 {
1587 	struct page *page = bi->page;
1588 	dma_addr_t dma;
1589 
1590 	/* since we are recycling buffers we should seldom need to alloc */
1591 	if (likely(page))
1592 		return true;
1593 
1594 	/* alloc new page for storage */
1595 	page = dev_alloc_pages(ixgbe_rx_pg_order(rx_ring));
1596 	if (unlikely(!page)) {
1597 		rx_ring->rx_stats.alloc_rx_page_failed++;
1598 		return false;
1599 	}
1600 
1601 	/* map page for use */
1602 	dma = dma_map_page_attrs(rx_ring->dev, page, 0,
1603 				 ixgbe_rx_pg_size(rx_ring),
1604 				 DMA_FROM_DEVICE,
1605 				 IXGBE_RX_DMA_ATTR);
1606 
1607 	/*
1608 	 * if mapping failed free memory back to system since
1609 	 * there isn't much point in holding memory we can't use
1610 	 */
1611 	if (dma_mapping_error(rx_ring->dev, dma)) {
1612 		__free_pages(page, ixgbe_rx_pg_order(rx_ring));
1613 
1614 		rx_ring->rx_stats.alloc_rx_page_failed++;
1615 		return false;
1616 	}
1617 
1618 	bi->dma = dma;
1619 	bi->page = page;
1620 	bi->page_offset = ixgbe_rx_offset(rx_ring);
1621 	bi->pagecnt_bias = 1;
1622 
1623 	return true;
1624 }
1625 
1626 /**
1627  * ixgbe_alloc_rx_buffers - Replace used receive buffers
1628  * @rx_ring: ring to place buffers on
1629  * @cleaned_count: number of buffers to replace
1630  **/
1631 void ixgbe_alloc_rx_buffers(struct ixgbe_ring *rx_ring, u16 cleaned_count)
1632 {
1633 	union ixgbe_adv_rx_desc *rx_desc;
1634 	struct ixgbe_rx_buffer *bi;
1635 	u16 i = rx_ring->next_to_use;
1636 	u16 bufsz;
1637 
1638 	/* nothing to do */
1639 	if (!cleaned_count)
1640 		return;
1641 
1642 	rx_desc = IXGBE_RX_DESC(rx_ring, i);
1643 	bi = &rx_ring->rx_buffer_info[i];
1644 	i -= rx_ring->count;
1645 
1646 	bufsz = ixgbe_rx_bufsz(rx_ring);
1647 
1648 	do {
1649 		if (!ixgbe_alloc_mapped_page(rx_ring, bi))
1650 			break;
1651 
1652 		/* sync the buffer for use by the device */
1653 		dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
1654 						 bi->page_offset, bufsz,
1655 						 DMA_FROM_DEVICE);
1656 
1657 		/*
1658 		 * Refresh the desc even if buffer_addrs didn't change
1659 		 * because each write-back erases this info.
1660 		 */
1661 		rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
1662 
1663 		rx_desc++;
1664 		bi++;
1665 		i++;
1666 		if (unlikely(!i)) {
1667 			rx_desc = IXGBE_RX_DESC(rx_ring, 0);
1668 			bi = rx_ring->rx_buffer_info;
1669 			i -= rx_ring->count;
1670 		}
1671 
1672 		/* clear the length for the next_to_use descriptor */
1673 		rx_desc->wb.upper.length = 0;
1674 
1675 		cleaned_count--;
1676 	} while (cleaned_count);
1677 
1678 	i += rx_ring->count;
1679 
1680 	if (rx_ring->next_to_use != i) {
1681 		rx_ring->next_to_use = i;
1682 
1683 		/* update next to alloc since we have filled the ring */
1684 		rx_ring->next_to_alloc = i;
1685 
1686 		/* Force memory writes to complete before letting h/w
1687 		 * know there are new descriptors to fetch.  (Only
1688 		 * applicable for weak-ordered memory model archs,
1689 		 * such as IA-64).
1690 		 */
1691 		wmb();
1692 		writel(i, rx_ring->tail);
1693 	}
1694 }
1695 
1696 static void ixgbe_set_rsc_gso_size(struct ixgbe_ring *ring,
1697 				   struct sk_buff *skb)
1698 {
1699 	u16 hdr_len = skb_headlen(skb);
1700 
1701 	/* set gso_size to avoid messing up TCP MSS */
1702 	skb_shinfo(skb)->gso_size = DIV_ROUND_UP((skb->len - hdr_len),
1703 						 IXGBE_CB(skb)->append_cnt);
1704 	skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1705 }
1706 
1707 static void ixgbe_update_rsc_stats(struct ixgbe_ring *rx_ring,
1708 				   struct sk_buff *skb)
1709 {
1710 	/* if append_cnt is 0 then frame is not RSC */
1711 	if (!IXGBE_CB(skb)->append_cnt)
1712 		return;
1713 
1714 	rx_ring->rx_stats.rsc_count += IXGBE_CB(skb)->append_cnt;
1715 	rx_ring->rx_stats.rsc_flush++;
1716 
1717 	ixgbe_set_rsc_gso_size(rx_ring, skb);
1718 
1719 	/* gso_size is computed using append_cnt so always clear it last */
1720 	IXGBE_CB(skb)->append_cnt = 0;
1721 }
1722 
1723 /**
1724  * ixgbe_process_skb_fields - Populate skb header fields from Rx descriptor
1725  * @rx_ring: rx descriptor ring packet is being transacted on
1726  * @rx_desc: pointer to the EOP Rx descriptor
1727  * @skb: pointer to current skb being populated
1728  *
1729  * This function checks the ring, descriptor, and packet information in
1730  * order to populate the hash, checksum, VLAN, timestamp, protocol, and
1731  * other fields within the skb.
1732  **/
1733 static void ixgbe_process_skb_fields(struct ixgbe_ring *rx_ring,
1734 				     union ixgbe_adv_rx_desc *rx_desc,
1735 				     struct sk_buff *skb)
1736 {
1737 	struct net_device *dev = rx_ring->netdev;
1738 	u32 flags = rx_ring->q_vector->adapter->flags;
1739 
1740 	ixgbe_update_rsc_stats(rx_ring, skb);
1741 
1742 	ixgbe_rx_hash(rx_ring, rx_desc, skb);
1743 
1744 	ixgbe_rx_checksum(rx_ring, rx_desc, skb);
1745 
1746 	if (unlikely(flags & IXGBE_FLAG_RX_HWTSTAMP_ENABLED))
1747 		ixgbe_ptp_rx_hwtstamp(rx_ring, rx_desc, skb);
1748 
1749 	if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1750 	    ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_VP)) {
1751 		u16 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
1752 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1753 	}
1754 
1755 	skb_record_rx_queue(skb, rx_ring->queue_index);
1756 
1757 	skb->protocol = eth_type_trans(skb, dev);
1758 }
1759 
1760 static void ixgbe_rx_skb(struct ixgbe_q_vector *q_vector,
1761 			 struct sk_buff *skb)
1762 {
1763 	napi_gro_receive(&q_vector->napi, skb);
1764 }
1765 
1766 /**
1767  * ixgbe_is_non_eop - process handling of non-EOP buffers
1768  * @rx_ring: Rx ring being processed
1769  * @rx_desc: Rx descriptor for current buffer
1770  * @skb: Current socket buffer containing buffer in progress
1771  *
1772  * This function updates next to clean.  If the buffer is an EOP buffer
1773  * this function exits returning false, otherwise it will place the
1774  * sk_buff in the next buffer to be chained and return true indicating
1775  * that this is in fact a non-EOP buffer.
1776  **/
1777 static bool ixgbe_is_non_eop(struct ixgbe_ring *rx_ring,
1778 			     union ixgbe_adv_rx_desc *rx_desc,
1779 			     struct sk_buff *skb)
1780 {
1781 	u32 ntc = rx_ring->next_to_clean + 1;
1782 
1783 	/* fetch, update, and store next to clean */
1784 	ntc = (ntc < rx_ring->count) ? ntc : 0;
1785 	rx_ring->next_to_clean = ntc;
1786 
1787 	prefetch(IXGBE_RX_DESC(rx_ring, ntc));
1788 
1789 	/* update RSC append count if present */
1790 	if (ring_is_rsc_enabled(rx_ring)) {
1791 		__le32 rsc_enabled = rx_desc->wb.lower.lo_dword.data &
1792 				     cpu_to_le32(IXGBE_RXDADV_RSCCNT_MASK);
1793 
1794 		if (unlikely(rsc_enabled)) {
1795 			u32 rsc_cnt = le32_to_cpu(rsc_enabled);
1796 
1797 			rsc_cnt >>= IXGBE_RXDADV_RSCCNT_SHIFT;
1798 			IXGBE_CB(skb)->append_cnt += rsc_cnt - 1;
1799 
1800 			/* update ntc based on RSC value */
1801 			ntc = le32_to_cpu(rx_desc->wb.upper.status_error);
1802 			ntc &= IXGBE_RXDADV_NEXTP_MASK;
1803 			ntc >>= IXGBE_RXDADV_NEXTP_SHIFT;
1804 		}
1805 	}
1806 
1807 	/* if we are the last buffer then there is nothing else to do */
1808 	if (likely(ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
1809 		return false;
1810 
1811 	/* place skb in next buffer to be received */
1812 	rx_ring->rx_buffer_info[ntc].skb = skb;
1813 	rx_ring->rx_stats.non_eop_descs++;
1814 
1815 	return true;
1816 }
1817 
1818 /**
1819  * ixgbe_pull_tail - ixgbe specific version of skb_pull_tail
1820  * @rx_ring: rx descriptor ring packet is being transacted on
1821  * @skb: pointer to current skb being adjusted
1822  *
1823  * This function is an ixgbe specific version of __pskb_pull_tail.  The
1824  * main difference between this version and the original function is that
1825  * this function can make several assumptions about the state of things
1826  * that allow for significant optimizations versus the standard function.
1827  * As a result we can do things like drop a frag and maintain an accurate
1828  * truesize for the skb.
1829  */
1830 static void ixgbe_pull_tail(struct ixgbe_ring *rx_ring,
1831 			    struct sk_buff *skb)
1832 {
1833 	struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
1834 	unsigned char *va;
1835 	unsigned int pull_len;
1836 
1837 	/*
1838 	 * it is valid to use page_address instead of kmap since we are
1839 	 * working with pages allocated out of the lomem pool per
1840 	 * alloc_page(GFP_ATOMIC)
1841 	 */
1842 	va = skb_frag_address(frag);
1843 
1844 	/*
1845 	 * we need the header to contain the greater of either ETH_HLEN or
1846 	 * 60 bytes if the skb->len is less than 60 for skb_pad.
1847 	 */
1848 	pull_len = eth_get_headlen(va, IXGBE_RX_HDR_SIZE);
1849 
1850 	/* align pull length to size of long to optimize memcpy performance */
1851 	skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
1852 
1853 	/* update all of the pointers */
1854 	skb_frag_size_sub(frag, pull_len);
1855 	frag->page_offset += pull_len;
1856 	skb->data_len -= pull_len;
1857 	skb->tail += pull_len;
1858 }
1859 
1860 /**
1861  * ixgbe_dma_sync_frag - perform DMA sync for first frag of SKB
1862  * @rx_ring: rx descriptor ring packet is being transacted on
1863  * @skb: pointer to current skb being updated
1864  *
1865  * This function provides a basic DMA sync up for the first fragment of an
1866  * skb.  The reason for doing this is that the first fragment cannot be
1867  * unmapped until we have reached the end of packet descriptor for a buffer
1868  * chain.
1869  */
1870 static void ixgbe_dma_sync_frag(struct ixgbe_ring *rx_ring,
1871 				struct sk_buff *skb)
1872 {
1873 	/* if the page was released unmap it, else just sync our portion */
1874 	if (unlikely(IXGBE_CB(skb)->page_released)) {
1875 		dma_unmap_page_attrs(rx_ring->dev, IXGBE_CB(skb)->dma,
1876 				     ixgbe_rx_pg_size(rx_ring),
1877 				     DMA_FROM_DEVICE,
1878 				     IXGBE_RX_DMA_ATTR);
1879 	} else {
1880 		struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
1881 
1882 		dma_sync_single_range_for_cpu(rx_ring->dev,
1883 					      IXGBE_CB(skb)->dma,
1884 					      frag->page_offset,
1885 					      skb_frag_size(frag),
1886 					      DMA_FROM_DEVICE);
1887 	}
1888 }
1889 
1890 /**
1891  * ixgbe_cleanup_headers - Correct corrupted or empty headers
1892  * @rx_ring: rx descriptor ring packet is being transacted on
1893  * @rx_desc: pointer to the EOP Rx descriptor
1894  * @skb: pointer to current skb being fixed
1895  *
1896  * Check if the skb is valid in the XDP case it will be an error pointer.
1897  * Return true in this case to abort processing and advance to next
1898  * descriptor.
1899  *
1900  * Check for corrupted packet headers caused by senders on the local L2
1901  * embedded NIC switch not setting up their Tx Descriptors right.  These
1902  * should be very rare.
1903  *
1904  * Also address the case where we are pulling data in on pages only
1905  * and as such no data is present in the skb header.
1906  *
1907  * In addition if skb is not at least 60 bytes we need to pad it so that
1908  * it is large enough to qualify as a valid Ethernet frame.
1909  *
1910  * Returns true if an error was encountered and skb was freed.
1911  **/
1912 static bool ixgbe_cleanup_headers(struct ixgbe_ring *rx_ring,
1913 				  union ixgbe_adv_rx_desc *rx_desc,
1914 				  struct sk_buff *skb)
1915 {
1916 	struct net_device *netdev = rx_ring->netdev;
1917 
1918 	/* XDP packets use error pointer so abort at this point */
1919 	if (IS_ERR(skb))
1920 		return true;
1921 
1922 	/* verify that the packet does not have any known errors */
1923 	if (unlikely(ixgbe_test_staterr(rx_desc,
1924 					IXGBE_RXDADV_ERR_FRAME_ERR_MASK) &&
1925 	    !(netdev->features & NETIF_F_RXALL))) {
1926 		dev_kfree_skb_any(skb);
1927 		return true;
1928 	}
1929 
1930 	/* place header in linear portion of buffer */
1931 	if (!skb_headlen(skb))
1932 		ixgbe_pull_tail(rx_ring, skb);
1933 
1934 #ifdef IXGBE_FCOE
1935 	/* do not attempt to pad FCoE Frames as this will disrupt DDP */
1936 	if (ixgbe_rx_is_fcoe(rx_ring, rx_desc))
1937 		return false;
1938 
1939 #endif
1940 	/* if eth_skb_pad returns an error the skb was freed */
1941 	if (eth_skb_pad(skb))
1942 		return true;
1943 
1944 	return false;
1945 }
1946 
1947 /**
1948  * ixgbe_reuse_rx_page - page flip buffer and store it back on the ring
1949  * @rx_ring: rx descriptor ring to store buffers on
1950  * @old_buff: donor buffer to have page reused
1951  *
1952  * Synchronizes page for reuse by the adapter
1953  **/
1954 static void ixgbe_reuse_rx_page(struct ixgbe_ring *rx_ring,
1955 				struct ixgbe_rx_buffer *old_buff)
1956 {
1957 	struct ixgbe_rx_buffer *new_buff;
1958 	u16 nta = rx_ring->next_to_alloc;
1959 
1960 	new_buff = &rx_ring->rx_buffer_info[nta];
1961 
1962 	/* update, and store next to alloc */
1963 	nta++;
1964 	rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1965 
1966 	/* Transfer page from old buffer to new buffer.
1967 	 * Move each member individually to avoid possible store
1968 	 * forwarding stalls and unnecessary copy of skb.
1969 	 */
1970 	new_buff->dma		= old_buff->dma;
1971 	new_buff->page		= old_buff->page;
1972 	new_buff->page_offset	= old_buff->page_offset;
1973 	new_buff->pagecnt_bias	= old_buff->pagecnt_bias;
1974 }
1975 
1976 static inline bool ixgbe_page_is_reserved(struct page *page)
1977 {
1978 	return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
1979 }
1980 
1981 static bool ixgbe_can_reuse_rx_page(struct ixgbe_rx_buffer *rx_buffer)
1982 {
1983 	unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1984 	struct page *page = rx_buffer->page;
1985 
1986 	/* avoid re-using remote pages */
1987 	if (unlikely(ixgbe_page_is_reserved(page)))
1988 		return false;
1989 
1990 #if (PAGE_SIZE < 8192)
1991 	/* if we are only owner of page we can reuse it */
1992 	if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
1993 		return false;
1994 #else
1995 	/* The last offset is a bit aggressive in that we assume the
1996 	 * worst case of FCoE being enabled and using a 3K buffer.
1997 	 * However this should have minimal impact as the 1K extra is
1998 	 * still less than one buffer in size.
1999 	 */
2000 #define IXGBE_LAST_OFFSET \
2001 	(SKB_WITH_OVERHEAD(PAGE_SIZE) - IXGBE_RXBUFFER_3K)
2002 	if (rx_buffer->page_offset > IXGBE_LAST_OFFSET)
2003 		return false;
2004 #endif
2005 
2006 	/* If we have drained the page fragment pool we need to update
2007 	 * the pagecnt_bias and page count so that we fully restock the
2008 	 * number of references the driver holds.
2009 	 */
2010 	if (unlikely(!pagecnt_bias)) {
2011 		page_ref_add(page, USHRT_MAX);
2012 		rx_buffer->pagecnt_bias = USHRT_MAX;
2013 	}
2014 
2015 	return true;
2016 }
2017 
2018 /**
2019  * ixgbe_add_rx_frag - Add contents of Rx buffer to sk_buff
2020  * @rx_ring: rx descriptor ring to transact packets on
2021  * @rx_buffer: buffer containing page to add
2022  * @rx_desc: descriptor containing length of buffer written by hardware
2023  * @skb: sk_buff to place the data into
2024  *
2025  * This function will add the data contained in rx_buffer->page to the skb.
2026  * This is done either through a direct copy if the data in the buffer is
2027  * less than the skb header size, otherwise it will just attach the page as
2028  * a frag to the skb.
2029  *
2030  * The function will then update the page offset if necessary and return
2031  * true if the buffer can be reused by the adapter.
2032  **/
2033 static void ixgbe_add_rx_frag(struct ixgbe_ring *rx_ring,
2034 			      struct ixgbe_rx_buffer *rx_buffer,
2035 			      struct sk_buff *skb,
2036 			      unsigned int size)
2037 {
2038 #if (PAGE_SIZE < 8192)
2039 	unsigned int truesize = ixgbe_rx_pg_size(rx_ring) / 2;
2040 #else
2041 	unsigned int truesize = ring_uses_build_skb(rx_ring) ?
2042 				SKB_DATA_ALIGN(IXGBE_SKB_PAD + size) :
2043 				SKB_DATA_ALIGN(size);
2044 #endif
2045 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
2046 			rx_buffer->page_offset, size, truesize);
2047 #if (PAGE_SIZE < 8192)
2048 	rx_buffer->page_offset ^= truesize;
2049 #else
2050 	rx_buffer->page_offset += truesize;
2051 #endif
2052 }
2053 
2054 static struct ixgbe_rx_buffer *ixgbe_get_rx_buffer(struct ixgbe_ring *rx_ring,
2055 						   union ixgbe_adv_rx_desc *rx_desc,
2056 						   struct sk_buff **skb,
2057 						   const unsigned int size)
2058 {
2059 	struct ixgbe_rx_buffer *rx_buffer;
2060 
2061 	rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
2062 	prefetchw(rx_buffer->page);
2063 	*skb = rx_buffer->skb;
2064 
2065 	/* Delay unmapping of the first packet. It carries the header
2066 	 * information, HW may still access the header after the writeback.
2067 	 * Only unmap it when EOP is reached
2068 	 */
2069 	if (!ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)) {
2070 		if (!*skb)
2071 			goto skip_sync;
2072 	} else {
2073 		if (*skb)
2074 			ixgbe_dma_sync_frag(rx_ring, *skb);
2075 	}
2076 
2077 	/* we are reusing so sync this buffer for CPU use */
2078 	dma_sync_single_range_for_cpu(rx_ring->dev,
2079 				      rx_buffer->dma,
2080 				      rx_buffer->page_offset,
2081 				      size,
2082 				      DMA_FROM_DEVICE);
2083 skip_sync:
2084 	rx_buffer->pagecnt_bias--;
2085 
2086 	return rx_buffer;
2087 }
2088 
2089 static void ixgbe_put_rx_buffer(struct ixgbe_ring *rx_ring,
2090 				struct ixgbe_rx_buffer *rx_buffer,
2091 				struct sk_buff *skb)
2092 {
2093 	if (ixgbe_can_reuse_rx_page(rx_buffer)) {
2094 		/* hand second half of page back to the ring */
2095 		ixgbe_reuse_rx_page(rx_ring, rx_buffer);
2096 	} else {
2097 		if (!IS_ERR(skb) && IXGBE_CB(skb)->dma == rx_buffer->dma) {
2098 			/* the page has been released from the ring */
2099 			IXGBE_CB(skb)->page_released = true;
2100 		} else {
2101 			/* we are not reusing the buffer so unmap it */
2102 			dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
2103 					     ixgbe_rx_pg_size(rx_ring),
2104 					     DMA_FROM_DEVICE,
2105 					     IXGBE_RX_DMA_ATTR);
2106 		}
2107 		__page_frag_cache_drain(rx_buffer->page,
2108 					rx_buffer->pagecnt_bias);
2109 	}
2110 
2111 	/* clear contents of rx_buffer */
2112 	rx_buffer->page = NULL;
2113 	rx_buffer->skb = NULL;
2114 }
2115 
2116 static struct sk_buff *ixgbe_construct_skb(struct ixgbe_ring *rx_ring,
2117 					   struct ixgbe_rx_buffer *rx_buffer,
2118 					   struct xdp_buff *xdp,
2119 					   union ixgbe_adv_rx_desc *rx_desc)
2120 {
2121 	unsigned int size = xdp->data_end - xdp->data;
2122 #if (PAGE_SIZE < 8192)
2123 	unsigned int truesize = ixgbe_rx_pg_size(rx_ring) / 2;
2124 #else
2125 	unsigned int truesize = SKB_DATA_ALIGN(xdp->data_end -
2126 					       xdp->data_hard_start);
2127 #endif
2128 	struct sk_buff *skb;
2129 
2130 	/* prefetch first cache line of first page */
2131 	prefetch(xdp->data);
2132 #if L1_CACHE_BYTES < 128
2133 	prefetch(xdp->data + L1_CACHE_BYTES);
2134 #endif
2135 
2136 	/* allocate a skb to store the frags */
2137 	skb = napi_alloc_skb(&rx_ring->q_vector->napi, IXGBE_RX_HDR_SIZE);
2138 	if (unlikely(!skb))
2139 		return NULL;
2140 
2141 	if (size > IXGBE_RX_HDR_SIZE) {
2142 		if (!ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP))
2143 			IXGBE_CB(skb)->dma = rx_buffer->dma;
2144 
2145 		skb_add_rx_frag(skb, 0, rx_buffer->page,
2146 				xdp->data - page_address(rx_buffer->page),
2147 				size, truesize);
2148 #if (PAGE_SIZE < 8192)
2149 		rx_buffer->page_offset ^= truesize;
2150 #else
2151 		rx_buffer->page_offset += truesize;
2152 #endif
2153 	} else {
2154 		memcpy(__skb_put(skb, size),
2155 		       xdp->data, ALIGN(size, sizeof(long)));
2156 		rx_buffer->pagecnt_bias++;
2157 	}
2158 
2159 	return skb;
2160 }
2161 
2162 static struct sk_buff *ixgbe_build_skb(struct ixgbe_ring *rx_ring,
2163 				       struct ixgbe_rx_buffer *rx_buffer,
2164 				       struct xdp_buff *xdp,
2165 				       union ixgbe_adv_rx_desc *rx_desc)
2166 {
2167 #if (PAGE_SIZE < 8192)
2168 	unsigned int truesize = ixgbe_rx_pg_size(rx_ring) / 2;
2169 #else
2170 	unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
2171 				SKB_DATA_ALIGN(xdp->data_end -
2172 					       xdp->data_hard_start);
2173 #endif
2174 	struct sk_buff *skb;
2175 
2176 	/* prefetch first cache line of first page */
2177 	prefetch(xdp->data);
2178 #if L1_CACHE_BYTES < 128
2179 	prefetch(xdp->data + L1_CACHE_BYTES);
2180 #endif
2181 
2182 	/* build an skb to around the page buffer */
2183 	skb = build_skb(xdp->data_hard_start, truesize);
2184 	if (unlikely(!skb))
2185 		return NULL;
2186 
2187 	/* update pointers within the skb to store the data */
2188 	skb_reserve(skb, xdp->data - xdp->data_hard_start);
2189 	__skb_put(skb, xdp->data_end - xdp->data);
2190 
2191 	/* record DMA address if this is the start of a chain of buffers */
2192 	if (!ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP))
2193 		IXGBE_CB(skb)->dma = rx_buffer->dma;
2194 
2195 	/* update buffer offset */
2196 #if (PAGE_SIZE < 8192)
2197 	rx_buffer->page_offset ^= truesize;
2198 #else
2199 	rx_buffer->page_offset += truesize;
2200 #endif
2201 
2202 	return skb;
2203 }
2204 
2205 #define IXGBE_XDP_PASS 0
2206 #define IXGBE_XDP_CONSUMED 1
2207 #define IXGBE_XDP_TX 2
2208 
2209 static int ixgbe_xmit_xdp_ring(struct ixgbe_adapter *adapter,
2210 			       struct xdp_buff *xdp);
2211 
2212 static struct sk_buff *ixgbe_run_xdp(struct ixgbe_adapter *adapter,
2213 				     struct ixgbe_ring *rx_ring,
2214 				     struct xdp_buff *xdp)
2215 {
2216 	int result = IXGBE_XDP_PASS;
2217 	struct bpf_prog *xdp_prog;
2218 	u32 act;
2219 
2220 	rcu_read_lock();
2221 	xdp_prog = READ_ONCE(rx_ring->xdp_prog);
2222 
2223 	if (!xdp_prog)
2224 		goto xdp_out;
2225 
2226 	act = bpf_prog_run_xdp(xdp_prog, xdp);
2227 	switch (act) {
2228 	case XDP_PASS:
2229 		break;
2230 	case XDP_TX:
2231 		result = ixgbe_xmit_xdp_ring(adapter, xdp);
2232 		break;
2233 	default:
2234 		bpf_warn_invalid_xdp_action(act);
2235 	case XDP_ABORTED:
2236 		trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
2237 		/* fallthrough -- handle aborts by dropping packet */
2238 	case XDP_DROP:
2239 		result = IXGBE_XDP_CONSUMED;
2240 		break;
2241 	}
2242 xdp_out:
2243 	rcu_read_unlock();
2244 	return ERR_PTR(-result);
2245 }
2246 
2247 static void ixgbe_rx_buffer_flip(struct ixgbe_ring *rx_ring,
2248 				 struct ixgbe_rx_buffer *rx_buffer,
2249 				 unsigned int size)
2250 {
2251 #if (PAGE_SIZE < 8192)
2252 	unsigned int truesize = ixgbe_rx_pg_size(rx_ring) / 2;
2253 
2254 	rx_buffer->page_offset ^= truesize;
2255 #else
2256 	unsigned int truesize = ring_uses_build_skb(rx_ring) ?
2257 				SKB_DATA_ALIGN(IXGBE_SKB_PAD + size) :
2258 				SKB_DATA_ALIGN(size);
2259 
2260 	rx_buffer->page_offset += truesize;
2261 #endif
2262 }
2263 
2264 /**
2265  * ixgbe_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
2266  * @q_vector: structure containing interrupt and ring information
2267  * @rx_ring: rx descriptor ring to transact packets on
2268  * @budget: Total limit on number of packets to process
2269  *
2270  * This function provides a "bounce buffer" approach to Rx interrupt
2271  * processing.  The advantage to this is that on systems that have
2272  * expensive overhead for IOMMU access this provides a means of avoiding
2273  * it by maintaining the mapping of the page to the syste.
2274  *
2275  * Returns amount of work completed
2276  **/
2277 static int ixgbe_clean_rx_irq(struct ixgbe_q_vector *q_vector,
2278 			       struct ixgbe_ring *rx_ring,
2279 			       const int budget)
2280 {
2281 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
2282 	struct ixgbe_adapter *adapter = q_vector->adapter;
2283 #ifdef IXGBE_FCOE
2284 	int ddp_bytes;
2285 	unsigned int mss = 0;
2286 #endif /* IXGBE_FCOE */
2287 	u16 cleaned_count = ixgbe_desc_unused(rx_ring);
2288 	bool xdp_xmit = false;
2289 
2290 	while (likely(total_rx_packets < budget)) {
2291 		union ixgbe_adv_rx_desc *rx_desc;
2292 		struct ixgbe_rx_buffer *rx_buffer;
2293 		struct sk_buff *skb;
2294 		struct xdp_buff xdp;
2295 		unsigned int size;
2296 
2297 		/* return some buffers to hardware, one at a time is too slow */
2298 		if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
2299 			ixgbe_alloc_rx_buffers(rx_ring, cleaned_count);
2300 			cleaned_count = 0;
2301 		}
2302 
2303 		rx_desc = IXGBE_RX_DESC(rx_ring, rx_ring->next_to_clean);
2304 		size = le16_to_cpu(rx_desc->wb.upper.length);
2305 		if (!size)
2306 			break;
2307 
2308 		/* This memory barrier is needed to keep us from reading
2309 		 * any other fields out of the rx_desc until we know the
2310 		 * descriptor has been written back
2311 		 */
2312 		dma_rmb();
2313 
2314 		rx_buffer = ixgbe_get_rx_buffer(rx_ring, rx_desc, &skb, size);
2315 
2316 		/* retrieve a buffer from the ring */
2317 		if (!skb) {
2318 			xdp.data = page_address(rx_buffer->page) +
2319 				   rx_buffer->page_offset;
2320 			xdp.data_hard_start = xdp.data -
2321 					      ixgbe_rx_offset(rx_ring);
2322 			xdp.data_end = xdp.data + size;
2323 
2324 			skb = ixgbe_run_xdp(adapter, rx_ring, &xdp);
2325 		}
2326 
2327 		if (IS_ERR(skb)) {
2328 			if (PTR_ERR(skb) == -IXGBE_XDP_TX) {
2329 				xdp_xmit = true;
2330 				ixgbe_rx_buffer_flip(rx_ring, rx_buffer, size);
2331 			} else {
2332 				rx_buffer->pagecnt_bias++;
2333 			}
2334 			total_rx_packets++;
2335 			total_rx_bytes += size;
2336 		} else if (skb) {
2337 			ixgbe_add_rx_frag(rx_ring, rx_buffer, skb, size);
2338 		} else if (ring_uses_build_skb(rx_ring)) {
2339 			skb = ixgbe_build_skb(rx_ring, rx_buffer,
2340 					      &xdp, rx_desc);
2341 		} else {
2342 			skb = ixgbe_construct_skb(rx_ring, rx_buffer,
2343 						  &xdp, rx_desc);
2344 		}
2345 
2346 		/* exit if we failed to retrieve a buffer */
2347 		if (!skb) {
2348 			rx_ring->rx_stats.alloc_rx_buff_failed++;
2349 			rx_buffer->pagecnt_bias++;
2350 			break;
2351 		}
2352 
2353 		ixgbe_put_rx_buffer(rx_ring, rx_buffer, skb);
2354 		cleaned_count++;
2355 
2356 		/* place incomplete frames back on ring for completion */
2357 		if (ixgbe_is_non_eop(rx_ring, rx_desc, skb))
2358 			continue;
2359 
2360 		/* verify the packet layout is correct */
2361 		if (ixgbe_cleanup_headers(rx_ring, rx_desc, skb))
2362 			continue;
2363 
2364 		/* probably a little skewed due to removing CRC */
2365 		total_rx_bytes += skb->len;
2366 
2367 		/* populate checksum, timestamp, VLAN, and protocol */
2368 		ixgbe_process_skb_fields(rx_ring, rx_desc, skb);
2369 
2370 #ifdef IXGBE_FCOE
2371 		/* if ddp, not passing to ULD unless for FCP_RSP or error */
2372 		if (ixgbe_rx_is_fcoe(rx_ring, rx_desc)) {
2373 			ddp_bytes = ixgbe_fcoe_ddp(adapter, rx_desc, skb);
2374 			/* include DDPed FCoE data */
2375 			if (ddp_bytes > 0) {
2376 				if (!mss) {
2377 					mss = rx_ring->netdev->mtu -
2378 						sizeof(struct fcoe_hdr) -
2379 						sizeof(struct fc_frame_header) -
2380 						sizeof(struct fcoe_crc_eof);
2381 					if (mss > 512)
2382 						mss &= ~511;
2383 				}
2384 				total_rx_bytes += ddp_bytes;
2385 				total_rx_packets += DIV_ROUND_UP(ddp_bytes,
2386 								 mss);
2387 			}
2388 			if (!ddp_bytes) {
2389 				dev_kfree_skb_any(skb);
2390 				continue;
2391 			}
2392 		}
2393 
2394 #endif /* IXGBE_FCOE */
2395 		ixgbe_rx_skb(q_vector, skb);
2396 
2397 		/* update budget accounting */
2398 		total_rx_packets++;
2399 	}
2400 
2401 	if (xdp_xmit) {
2402 		struct ixgbe_ring *ring = adapter->xdp_ring[smp_processor_id()];
2403 
2404 		/* Force memory writes to complete before letting h/w
2405 		 * know there are new descriptors to fetch.
2406 		 */
2407 		wmb();
2408 		writel(ring->next_to_use, ring->tail);
2409 	}
2410 
2411 	u64_stats_update_begin(&rx_ring->syncp);
2412 	rx_ring->stats.packets += total_rx_packets;
2413 	rx_ring->stats.bytes += total_rx_bytes;
2414 	u64_stats_update_end(&rx_ring->syncp);
2415 	q_vector->rx.total_packets += total_rx_packets;
2416 	q_vector->rx.total_bytes += total_rx_bytes;
2417 
2418 	return total_rx_packets;
2419 }
2420 
2421 /**
2422  * ixgbe_configure_msix - Configure MSI-X hardware
2423  * @adapter: board private structure
2424  *
2425  * ixgbe_configure_msix sets up the hardware to properly generate MSI-X
2426  * interrupts.
2427  **/
2428 static void ixgbe_configure_msix(struct ixgbe_adapter *adapter)
2429 {
2430 	struct ixgbe_q_vector *q_vector;
2431 	int v_idx;
2432 	u32 mask;
2433 
2434 	/* Populate MSIX to EITR Select */
2435 	if (adapter->num_vfs > 32) {
2436 		u32 eitrsel = BIT(adapter->num_vfs - 32) - 1;
2437 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITRSEL, eitrsel);
2438 	}
2439 
2440 	/*
2441 	 * Populate the IVAR table and set the ITR values to the
2442 	 * corresponding register.
2443 	 */
2444 	for (v_idx = 0; v_idx < adapter->num_q_vectors; v_idx++) {
2445 		struct ixgbe_ring *ring;
2446 		q_vector = adapter->q_vector[v_idx];
2447 
2448 		ixgbe_for_each_ring(ring, q_vector->rx)
2449 			ixgbe_set_ivar(adapter, 0, ring->reg_idx, v_idx);
2450 
2451 		ixgbe_for_each_ring(ring, q_vector->tx)
2452 			ixgbe_set_ivar(adapter, 1, ring->reg_idx, v_idx);
2453 
2454 		ixgbe_write_eitr(q_vector);
2455 	}
2456 
2457 	switch (adapter->hw.mac.type) {
2458 	case ixgbe_mac_82598EB:
2459 		ixgbe_set_ivar(adapter, -1, IXGBE_IVAR_OTHER_CAUSES_INDEX,
2460 			       v_idx);
2461 		break;
2462 	case ixgbe_mac_82599EB:
2463 	case ixgbe_mac_X540:
2464 	case ixgbe_mac_X550:
2465 	case ixgbe_mac_X550EM_x:
2466 	case ixgbe_mac_x550em_a:
2467 		ixgbe_set_ivar(adapter, -1, 1, v_idx);
2468 		break;
2469 	default:
2470 		break;
2471 	}
2472 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx), 1950);
2473 
2474 	/* set up to autoclear timer, and the vectors */
2475 	mask = IXGBE_EIMS_ENABLE_MASK;
2476 	mask &= ~(IXGBE_EIMS_OTHER |
2477 		  IXGBE_EIMS_MAILBOX |
2478 		  IXGBE_EIMS_LSC);
2479 
2480 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, mask);
2481 }
2482 
2483 enum latency_range {
2484 	lowest_latency = 0,
2485 	low_latency = 1,
2486 	bulk_latency = 2,
2487 	latency_invalid = 255
2488 };
2489 
2490 /**
2491  * ixgbe_update_itr - update the dynamic ITR value based on statistics
2492  * @q_vector: structure containing interrupt and ring information
2493  * @ring_container: structure containing ring performance data
2494  *
2495  *      Stores a new ITR value based on packets and byte
2496  *      counts during the last interrupt.  The advantage of per interrupt
2497  *      computation is faster updates and more accurate ITR for the current
2498  *      traffic pattern.  Constants in this function were computed
2499  *      based on theoretical maximum wire speed and thresholds were set based
2500  *      on testing data as well as attempting to minimize response time
2501  *      while increasing bulk throughput.
2502  *      this functionality is controlled by the InterruptThrottleRate module
2503  *      parameter (see ixgbe_param.c)
2504  **/
2505 static void ixgbe_update_itr(struct ixgbe_q_vector *q_vector,
2506 			     struct ixgbe_ring_container *ring_container)
2507 {
2508 	int bytes = ring_container->total_bytes;
2509 	int packets = ring_container->total_packets;
2510 	u32 timepassed_us;
2511 	u64 bytes_perint;
2512 	u8 itr_setting = ring_container->itr;
2513 
2514 	if (packets == 0)
2515 		return;
2516 
2517 	/* simple throttlerate management
2518 	 *   0-10MB/s   lowest (100000 ints/s)
2519 	 *  10-20MB/s   low    (20000 ints/s)
2520 	 *  20-1249MB/s bulk   (12000 ints/s)
2521 	 */
2522 	/* what was last interrupt timeslice? */
2523 	timepassed_us = q_vector->itr >> 2;
2524 	if (timepassed_us == 0)
2525 		return;
2526 
2527 	bytes_perint = bytes / timepassed_us; /* bytes/usec */
2528 
2529 	switch (itr_setting) {
2530 	case lowest_latency:
2531 		if (bytes_perint > 10)
2532 			itr_setting = low_latency;
2533 		break;
2534 	case low_latency:
2535 		if (bytes_perint > 20)
2536 			itr_setting = bulk_latency;
2537 		else if (bytes_perint <= 10)
2538 			itr_setting = lowest_latency;
2539 		break;
2540 	case bulk_latency:
2541 		if (bytes_perint <= 20)
2542 			itr_setting = low_latency;
2543 		break;
2544 	}
2545 
2546 	/* clear work counters since we have the values we need */
2547 	ring_container->total_bytes = 0;
2548 	ring_container->total_packets = 0;
2549 
2550 	/* write updated itr to ring container */
2551 	ring_container->itr = itr_setting;
2552 }
2553 
2554 /**
2555  * ixgbe_write_eitr - write EITR register in hardware specific way
2556  * @q_vector: structure containing interrupt and ring information
2557  *
2558  * This function is made to be called by ethtool and by the driver
2559  * when it needs to update EITR registers at runtime.  Hardware
2560  * specific quirks/differences are taken care of here.
2561  */
2562 void ixgbe_write_eitr(struct ixgbe_q_vector *q_vector)
2563 {
2564 	struct ixgbe_adapter *adapter = q_vector->adapter;
2565 	struct ixgbe_hw *hw = &adapter->hw;
2566 	int v_idx = q_vector->v_idx;
2567 	u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
2568 
2569 	switch (adapter->hw.mac.type) {
2570 	case ixgbe_mac_82598EB:
2571 		/* must write high and low 16 bits to reset counter */
2572 		itr_reg |= (itr_reg << 16);
2573 		break;
2574 	case ixgbe_mac_82599EB:
2575 	case ixgbe_mac_X540:
2576 	case ixgbe_mac_X550:
2577 	case ixgbe_mac_X550EM_x:
2578 	case ixgbe_mac_x550em_a:
2579 		/*
2580 		 * set the WDIS bit to not clear the timer bits and cause an
2581 		 * immediate assertion of the interrupt
2582 		 */
2583 		itr_reg |= IXGBE_EITR_CNT_WDIS;
2584 		break;
2585 	default:
2586 		break;
2587 	}
2588 	IXGBE_WRITE_REG(hw, IXGBE_EITR(v_idx), itr_reg);
2589 }
2590 
2591 static void ixgbe_set_itr(struct ixgbe_q_vector *q_vector)
2592 {
2593 	u32 new_itr = q_vector->itr;
2594 	u8 current_itr;
2595 
2596 	ixgbe_update_itr(q_vector, &q_vector->tx);
2597 	ixgbe_update_itr(q_vector, &q_vector->rx);
2598 
2599 	current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
2600 
2601 	switch (current_itr) {
2602 	/* counts and packets in update_itr are dependent on these numbers */
2603 	case lowest_latency:
2604 		new_itr = IXGBE_100K_ITR;
2605 		break;
2606 	case low_latency:
2607 		new_itr = IXGBE_20K_ITR;
2608 		break;
2609 	case bulk_latency:
2610 		new_itr = IXGBE_12K_ITR;
2611 		break;
2612 	default:
2613 		break;
2614 	}
2615 
2616 	if (new_itr != q_vector->itr) {
2617 		/* do an exponential smoothing */
2618 		new_itr = (10 * new_itr * q_vector->itr) /
2619 			  ((9 * new_itr) + q_vector->itr);
2620 
2621 		/* save the algorithm value here */
2622 		q_vector->itr = new_itr;
2623 
2624 		ixgbe_write_eitr(q_vector);
2625 	}
2626 }
2627 
2628 /**
2629  * ixgbe_check_overtemp_subtask - check for over temperature
2630  * @adapter: pointer to adapter
2631  **/
2632 static void ixgbe_check_overtemp_subtask(struct ixgbe_adapter *adapter)
2633 {
2634 	struct ixgbe_hw *hw = &adapter->hw;
2635 	u32 eicr = adapter->interrupt_event;
2636 	s32 rc;
2637 
2638 	if (test_bit(__IXGBE_DOWN, &adapter->state))
2639 		return;
2640 
2641 	if (!(adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE) &&
2642 	    !(adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_EVENT))
2643 		return;
2644 
2645 	adapter->flags2 &= ~IXGBE_FLAG2_TEMP_SENSOR_EVENT;
2646 
2647 	switch (hw->device_id) {
2648 	case IXGBE_DEV_ID_82599_T3_LOM:
2649 		/*
2650 		 * Since the warning interrupt is for both ports
2651 		 * we don't have to check if:
2652 		 *  - This interrupt wasn't for our port.
2653 		 *  - We may have missed the interrupt so always have to
2654 		 *    check if we  got a LSC
2655 		 */
2656 		if (!(eicr & IXGBE_EICR_GPI_SDP0_8259X) &&
2657 		    !(eicr & IXGBE_EICR_LSC))
2658 			return;
2659 
2660 		if (!(eicr & IXGBE_EICR_LSC) && hw->mac.ops.check_link) {
2661 			u32 speed;
2662 			bool link_up = false;
2663 
2664 			hw->mac.ops.check_link(hw, &speed, &link_up, false);
2665 
2666 			if (link_up)
2667 				return;
2668 		}
2669 
2670 		/* Check if this is not due to overtemp */
2671 		if (hw->phy.ops.check_overtemp(hw) != IXGBE_ERR_OVERTEMP)
2672 			return;
2673 
2674 		break;
2675 	case IXGBE_DEV_ID_X550EM_A_1G_T:
2676 	case IXGBE_DEV_ID_X550EM_A_1G_T_L:
2677 		rc = hw->phy.ops.check_overtemp(hw);
2678 		if (rc != IXGBE_ERR_OVERTEMP)
2679 			return;
2680 		break;
2681 	default:
2682 		if (adapter->hw.mac.type >= ixgbe_mac_X540)
2683 			return;
2684 		if (!(eicr & IXGBE_EICR_GPI_SDP0(hw)))
2685 			return;
2686 		break;
2687 	}
2688 	e_crit(drv, "%s\n", ixgbe_overheat_msg);
2689 
2690 	adapter->interrupt_event = 0;
2691 }
2692 
2693 static void ixgbe_check_fan_failure(struct ixgbe_adapter *adapter, u32 eicr)
2694 {
2695 	struct ixgbe_hw *hw = &adapter->hw;
2696 
2697 	if ((adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) &&
2698 	    (eicr & IXGBE_EICR_GPI_SDP1(hw))) {
2699 		e_crit(probe, "Fan has stopped, replace the adapter\n");
2700 		/* write to clear the interrupt */
2701 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1(hw));
2702 	}
2703 }
2704 
2705 static void ixgbe_check_overtemp_event(struct ixgbe_adapter *adapter, u32 eicr)
2706 {
2707 	struct ixgbe_hw *hw = &adapter->hw;
2708 
2709 	if (!(adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE))
2710 		return;
2711 
2712 	switch (adapter->hw.mac.type) {
2713 	case ixgbe_mac_82599EB:
2714 		/*
2715 		 * Need to check link state so complete overtemp check
2716 		 * on service task
2717 		 */
2718 		if (((eicr & IXGBE_EICR_GPI_SDP0(hw)) ||
2719 		     (eicr & IXGBE_EICR_LSC)) &&
2720 		    (!test_bit(__IXGBE_DOWN, &adapter->state))) {
2721 			adapter->interrupt_event = eicr;
2722 			adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_EVENT;
2723 			ixgbe_service_event_schedule(adapter);
2724 			return;
2725 		}
2726 		return;
2727 	case ixgbe_mac_x550em_a:
2728 		if (eicr & IXGBE_EICR_GPI_SDP0_X550EM_a) {
2729 			adapter->interrupt_event = eicr;
2730 			adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_EVENT;
2731 			ixgbe_service_event_schedule(adapter);
2732 			IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC,
2733 					IXGBE_EICR_GPI_SDP0_X550EM_a);
2734 			IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICR,
2735 					IXGBE_EICR_GPI_SDP0_X550EM_a);
2736 		}
2737 		return;
2738 	case ixgbe_mac_X550:
2739 	case ixgbe_mac_X540:
2740 		if (!(eicr & IXGBE_EICR_TS))
2741 			return;
2742 		break;
2743 	default:
2744 		return;
2745 	}
2746 
2747 	e_crit(drv, "%s\n", ixgbe_overheat_msg);
2748 }
2749 
2750 static inline bool ixgbe_is_sfp(struct ixgbe_hw *hw)
2751 {
2752 	switch (hw->mac.type) {
2753 	case ixgbe_mac_82598EB:
2754 		if (hw->phy.type == ixgbe_phy_nl)
2755 			return true;
2756 		return false;
2757 	case ixgbe_mac_82599EB:
2758 	case ixgbe_mac_X550EM_x:
2759 	case ixgbe_mac_x550em_a:
2760 		switch (hw->mac.ops.get_media_type(hw)) {
2761 		case ixgbe_media_type_fiber:
2762 		case ixgbe_media_type_fiber_qsfp:
2763 			return true;
2764 		default:
2765 			return false;
2766 		}
2767 	default:
2768 		return false;
2769 	}
2770 }
2771 
2772 static void ixgbe_check_sfp_event(struct ixgbe_adapter *adapter, u32 eicr)
2773 {
2774 	struct ixgbe_hw *hw = &adapter->hw;
2775 	u32 eicr_mask = IXGBE_EICR_GPI_SDP2(hw);
2776 
2777 	if (!ixgbe_is_sfp(hw))
2778 		return;
2779 
2780 	/* Later MAC's use different SDP */
2781 	if (hw->mac.type >= ixgbe_mac_X540)
2782 		eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
2783 
2784 	if (eicr & eicr_mask) {
2785 		/* Clear the interrupt */
2786 		IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
2787 		if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
2788 			adapter->flags2 |= IXGBE_FLAG2_SFP_NEEDS_RESET;
2789 			adapter->sfp_poll_time = 0;
2790 			ixgbe_service_event_schedule(adapter);
2791 		}
2792 	}
2793 
2794 	if (adapter->hw.mac.type == ixgbe_mac_82599EB &&
2795 	    (eicr & IXGBE_EICR_GPI_SDP1(hw))) {
2796 		/* Clear the interrupt */
2797 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1(hw));
2798 		if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
2799 			adapter->flags |= IXGBE_FLAG_NEED_LINK_CONFIG;
2800 			ixgbe_service_event_schedule(adapter);
2801 		}
2802 	}
2803 }
2804 
2805 static void ixgbe_check_lsc(struct ixgbe_adapter *adapter)
2806 {
2807 	struct ixgbe_hw *hw = &adapter->hw;
2808 
2809 	adapter->lsc_int++;
2810 	adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
2811 	adapter->link_check_timeout = jiffies;
2812 	if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
2813 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
2814 		IXGBE_WRITE_FLUSH(hw);
2815 		ixgbe_service_event_schedule(adapter);
2816 	}
2817 }
2818 
2819 static inline void ixgbe_irq_enable_queues(struct ixgbe_adapter *adapter,
2820 					   u64 qmask)
2821 {
2822 	u32 mask;
2823 	struct ixgbe_hw *hw = &adapter->hw;
2824 
2825 	switch (hw->mac.type) {
2826 	case ixgbe_mac_82598EB:
2827 		mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
2828 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
2829 		break;
2830 	case ixgbe_mac_82599EB:
2831 	case ixgbe_mac_X540:
2832 	case ixgbe_mac_X550:
2833 	case ixgbe_mac_X550EM_x:
2834 	case ixgbe_mac_x550em_a:
2835 		mask = (qmask & 0xFFFFFFFF);
2836 		if (mask)
2837 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
2838 		mask = (qmask >> 32);
2839 		if (mask)
2840 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
2841 		break;
2842 	default:
2843 		break;
2844 	}
2845 	/* skip the flush */
2846 }
2847 
2848 static inline void ixgbe_irq_disable_queues(struct ixgbe_adapter *adapter,
2849 					    u64 qmask)
2850 {
2851 	u32 mask;
2852 	struct ixgbe_hw *hw = &adapter->hw;
2853 
2854 	switch (hw->mac.type) {
2855 	case ixgbe_mac_82598EB:
2856 		mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
2857 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
2858 		break;
2859 	case ixgbe_mac_82599EB:
2860 	case ixgbe_mac_X540:
2861 	case ixgbe_mac_X550:
2862 	case ixgbe_mac_X550EM_x:
2863 	case ixgbe_mac_x550em_a:
2864 		mask = (qmask & 0xFFFFFFFF);
2865 		if (mask)
2866 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
2867 		mask = (qmask >> 32);
2868 		if (mask)
2869 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
2870 		break;
2871 	default:
2872 		break;
2873 	}
2874 	/* skip the flush */
2875 }
2876 
2877 /**
2878  * ixgbe_irq_enable - Enable default interrupt generation settings
2879  * @adapter: board private structure
2880  **/
2881 static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter, bool queues,
2882 				    bool flush)
2883 {
2884 	struct ixgbe_hw *hw = &adapter->hw;
2885 	u32 mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
2886 
2887 	/* don't reenable LSC while waiting for link */
2888 	if (adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE)
2889 		mask &= ~IXGBE_EIMS_LSC;
2890 
2891 	if (adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE)
2892 		switch (adapter->hw.mac.type) {
2893 		case ixgbe_mac_82599EB:
2894 			mask |= IXGBE_EIMS_GPI_SDP0(hw);
2895 			break;
2896 		case ixgbe_mac_X540:
2897 		case ixgbe_mac_X550:
2898 		case ixgbe_mac_X550EM_x:
2899 		case ixgbe_mac_x550em_a:
2900 			mask |= IXGBE_EIMS_TS;
2901 			break;
2902 		default:
2903 			break;
2904 		}
2905 	if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE)
2906 		mask |= IXGBE_EIMS_GPI_SDP1(hw);
2907 	switch (adapter->hw.mac.type) {
2908 	case ixgbe_mac_82599EB:
2909 		mask |= IXGBE_EIMS_GPI_SDP1(hw);
2910 		mask |= IXGBE_EIMS_GPI_SDP2(hw);
2911 		/* fall through */
2912 	case ixgbe_mac_X540:
2913 	case ixgbe_mac_X550:
2914 	case ixgbe_mac_X550EM_x:
2915 	case ixgbe_mac_x550em_a:
2916 		if (adapter->hw.device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
2917 		    adapter->hw.device_id == IXGBE_DEV_ID_X550EM_A_SFP ||
2918 		    adapter->hw.device_id == IXGBE_DEV_ID_X550EM_A_SFP_N)
2919 			mask |= IXGBE_EIMS_GPI_SDP0(&adapter->hw);
2920 		if (adapter->hw.phy.type == ixgbe_phy_x550em_ext_t)
2921 			mask |= IXGBE_EICR_GPI_SDP0_X540;
2922 		mask |= IXGBE_EIMS_ECC;
2923 		mask |= IXGBE_EIMS_MAILBOX;
2924 		break;
2925 	default:
2926 		break;
2927 	}
2928 
2929 	if ((adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) &&
2930 	    !(adapter->flags2 & IXGBE_FLAG2_FDIR_REQUIRES_REINIT))
2931 		mask |= IXGBE_EIMS_FLOW_DIR;
2932 
2933 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, mask);
2934 	if (queues)
2935 		ixgbe_irq_enable_queues(adapter, ~0);
2936 	if (flush)
2937 		IXGBE_WRITE_FLUSH(&adapter->hw);
2938 }
2939 
2940 static irqreturn_t ixgbe_msix_other(int irq, void *data)
2941 {
2942 	struct ixgbe_adapter *adapter = data;
2943 	struct ixgbe_hw *hw = &adapter->hw;
2944 	u32 eicr;
2945 
2946 	/*
2947 	 * Workaround for Silicon errata.  Use clear-by-write instead
2948 	 * of clear-by-read.  Reading with EICS will return the
2949 	 * interrupt causes without clearing, which later be done
2950 	 * with the write to EICR.
2951 	 */
2952 	eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
2953 
2954 	/* The lower 16bits of the EICR register are for the queue interrupts
2955 	 * which should be masked here in order to not accidentally clear them if
2956 	 * the bits are high when ixgbe_msix_other is called. There is a race
2957 	 * condition otherwise which results in possible performance loss
2958 	 * especially if the ixgbe_msix_other interrupt is triggering
2959 	 * consistently (as it would when PPS is turned on for the X540 device)
2960 	 */
2961 	eicr &= 0xFFFF0000;
2962 
2963 	IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
2964 
2965 	if (eicr & IXGBE_EICR_LSC)
2966 		ixgbe_check_lsc(adapter);
2967 
2968 	if (eicr & IXGBE_EICR_MAILBOX)
2969 		ixgbe_msg_task(adapter);
2970 
2971 	switch (hw->mac.type) {
2972 	case ixgbe_mac_82599EB:
2973 	case ixgbe_mac_X540:
2974 	case ixgbe_mac_X550:
2975 	case ixgbe_mac_X550EM_x:
2976 	case ixgbe_mac_x550em_a:
2977 		if (hw->phy.type == ixgbe_phy_x550em_ext_t &&
2978 		    (eicr & IXGBE_EICR_GPI_SDP0_X540)) {
2979 			adapter->flags2 |= IXGBE_FLAG2_PHY_INTERRUPT;
2980 			ixgbe_service_event_schedule(adapter);
2981 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
2982 					IXGBE_EICR_GPI_SDP0_X540);
2983 		}
2984 		if (eicr & IXGBE_EICR_ECC) {
2985 			e_info(link, "Received ECC Err, initiating reset\n");
2986 			set_bit(__IXGBE_RESET_REQUESTED, &adapter->state);
2987 			ixgbe_service_event_schedule(adapter);
2988 			IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
2989 		}
2990 		/* Handle Flow Director Full threshold interrupt */
2991 		if (eicr & IXGBE_EICR_FLOW_DIR) {
2992 			int reinit_count = 0;
2993 			int i;
2994 			for (i = 0; i < adapter->num_tx_queues; i++) {
2995 				struct ixgbe_ring *ring = adapter->tx_ring[i];
2996 				if (test_and_clear_bit(__IXGBE_TX_FDIR_INIT_DONE,
2997 						       &ring->state))
2998 					reinit_count++;
2999 			}
3000 			if (reinit_count) {
3001 				/* no more flow director interrupts until after init */
3002 				IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_FLOW_DIR);
3003 				adapter->flags2 |= IXGBE_FLAG2_FDIR_REQUIRES_REINIT;
3004 				ixgbe_service_event_schedule(adapter);
3005 			}
3006 		}
3007 		ixgbe_check_sfp_event(adapter, eicr);
3008 		ixgbe_check_overtemp_event(adapter, eicr);
3009 		break;
3010 	default:
3011 		break;
3012 	}
3013 
3014 	ixgbe_check_fan_failure(adapter, eicr);
3015 
3016 	if (unlikely(eicr & IXGBE_EICR_TIMESYNC))
3017 		ixgbe_ptp_check_pps_event(adapter);
3018 
3019 	/* re-enable the original interrupt state, no lsc, no queues */
3020 	if (!test_bit(__IXGBE_DOWN, &adapter->state))
3021 		ixgbe_irq_enable(adapter, false, false);
3022 
3023 	return IRQ_HANDLED;
3024 }
3025 
3026 static irqreturn_t ixgbe_msix_clean_rings(int irq, void *data)
3027 {
3028 	struct ixgbe_q_vector *q_vector = data;
3029 
3030 	/* EIAM disabled interrupts (on this vector) for us */
3031 
3032 	if (q_vector->rx.ring || q_vector->tx.ring)
3033 		napi_schedule_irqoff(&q_vector->napi);
3034 
3035 	return IRQ_HANDLED;
3036 }
3037 
3038 /**
3039  * ixgbe_poll - NAPI Rx polling callback
3040  * @napi: structure for representing this polling device
3041  * @budget: how many packets driver is allowed to clean
3042  *
3043  * This function is used for legacy and MSI, NAPI mode
3044  **/
3045 int ixgbe_poll(struct napi_struct *napi, int budget)
3046 {
3047 	struct ixgbe_q_vector *q_vector =
3048 				container_of(napi, struct ixgbe_q_vector, napi);
3049 	struct ixgbe_adapter *adapter = q_vector->adapter;
3050 	struct ixgbe_ring *ring;
3051 	int per_ring_budget, work_done = 0;
3052 	bool clean_complete = true;
3053 
3054 #ifdef CONFIG_IXGBE_DCA
3055 	if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
3056 		ixgbe_update_dca(q_vector);
3057 #endif
3058 
3059 	ixgbe_for_each_ring(ring, q_vector->tx) {
3060 		if (!ixgbe_clean_tx_irq(q_vector, ring, budget))
3061 			clean_complete = false;
3062 	}
3063 
3064 	/* Exit if we are called by netpoll */
3065 	if (budget <= 0)
3066 		return budget;
3067 
3068 	/* attempt to distribute budget to each queue fairly, but don't allow
3069 	 * the budget to go below 1 because we'll exit polling */
3070 	if (q_vector->rx.count > 1)
3071 		per_ring_budget = max(budget/q_vector->rx.count, 1);
3072 	else
3073 		per_ring_budget = budget;
3074 
3075 	ixgbe_for_each_ring(ring, q_vector->rx) {
3076 		int cleaned = ixgbe_clean_rx_irq(q_vector, ring,
3077 						 per_ring_budget);
3078 
3079 		work_done += cleaned;
3080 		if (cleaned >= per_ring_budget)
3081 			clean_complete = false;
3082 	}
3083 
3084 	/* If all work not completed, return budget and keep polling */
3085 	if (!clean_complete)
3086 		return budget;
3087 
3088 	/* all work done, exit the polling mode */
3089 	napi_complete_done(napi, work_done);
3090 	if (adapter->rx_itr_setting & 1)
3091 		ixgbe_set_itr(q_vector);
3092 	if (!test_bit(__IXGBE_DOWN, &adapter->state))
3093 		ixgbe_irq_enable_queues(adapter, BIT_ULL(q_vector->v_idx));
3094 
3095 	return min(work_done, budget - 1);
3096 }
3097 
3098 /**
3099  * ixgbe_request_msix_irqs - Initialize MSI-X interrupts
3100  * @adapter: board private structure
3101  *
3102  * ixgbe_request_msix_irqs allocates MSI-X vectors and requests
3103  * interrupts from the kernel.
3104  **/
3105 static int ixgbe_request_msix_irqs(struct ixgbe_adapter *adapter)
3106 {
3107 	struct net_device *netdev = adapter->netdev;
3108 	int vector, err;
3109 	int ri = 0, ti = 0;
3110 
3111 	for (vector = 0; vector < adapter->num_q_vectors; vector++) {
3112 		struct ixgbe_q_vector *q_vector = adapter->q_vector[vector];
3113 		struct msix_entry *entry = &adapter->msix_entries[vector];
3114 
3115 		if (q_vector->tx.ring && q_vector->rx.ring) {
3116 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3117 				 "%s-%s-%d", netdev->name, "TxRx", ri++);
3118 			ti++;
3119 		} else if (q_vector->rx.ring) {
3120 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3121 				 "%s-%s-%d", netdev->name, "rx", ri++);
3122 		} else if (q_vector->tx.ring) {
3123 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3124 				 "%s-%s-%d", netdev->name, "tx", ti++);
3125 		} else {
3126 			/* skip this unused q_vector */
3127 			continue;
3128 		}
3129 		err = request_irq(entry->vector, &ixgbe_msix_clean_rings, 0,
3130 				  q_vector->name, q_vector);
3131 		if (err) {
3132 			e_err(probe, "request_irq failed for MSIX interrupt "
3133 			      "Error: %d\n", err);
3134 			goto free_queue_irqs;
3135 		}
3136 		/* If Flow Director is enabled, set interrupt affinity */
3137 		if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
3138 			/* assign the mask for this irq */
3139 			irq_set_affinity_hint(entry->vector,
3140 					      &q_vector->affinity_mask);
3141 		}
3142 	}
3143 
3144 	err = request_irq(adapter->msix_entries[vector].vector,
3145 			  ixgbe_msix_other, 0, netdev->name, adapter);
3146 	if (err) {
3147 		e_err(probe, "request_irq for msix_other failed: %d\n", err);
3148 		goto free_queue_irqs;
3149 	}
3150 
3151 	return 0;
3152 
3153 free_queue_irqs:
3154 	while (vector) {
3155 		vector--;
3156 		irq_set_affinity_hint(adapter->msix_entries[vector].vector,
3157 				      NULL);
3158 		free_irq(adapter->msix_entries[vector].vector,
3159 			 adapter->q_vector[vector]);
3160 	}
3161 	adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
3162 	pci_disable_msix(adapter->pdev);
3163 	kfree(adapter->msix_entries);
3164 	adapter->msix_entries = NULL;
3165 	return err;
3166 }
3167 
3168 /**
3169  * ixgbe_intr - legacy mode Interrupt Handler
3170  * @irq: interrupt number
3171  * @data: pointer to a network interface device structure
3172  **/
3173 static irqreturn_t ixgbe_intr(int irq, void *data)
3174 {
3175 	struct ixgbe_adapter *adapter = data;
3176 	struct ixgbe_hw *hw = &adapter->hw;
3177 	struct ixgbe_q_vector *q_vector = adapter->q_vector[0];
3178 	u32 eicr;
3179 
3180 	/*
3181 	 * Workaround for silicon errata #26 on 82598.  Mask the interrupt
3182 	 * before the read of EICR.
3183 	 */
3184 	IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
3185 
3186 	/* for NAPI, using EIAM to auto-mask tx/rx interrupt bits on read
3187 	 * therefore no explicit interrupt disable is necessary */
3188 	eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
3189 	if (!eicr) {
3190 		/*
3191 		 * shared interrupt alert!
3192 		 * make sure interrupts are enabled because the read will
3193 		 * have disabled interrupts due to EIAM
3194 		 * finish the workaround of silicon errata on 82598.  Unmask
3195 		 * the interrupt that we masked before the EICR read.
3196 		 */
3197 		if (!test_bit(__IXGBE_DOWN, &adapter->state))
3198 			ixgbe_irq_enable(adapter, true, true);
3199 		return IRQ_NONE;	/* Not our interrupt */
3200 	}
3201 
3202 	if (eicr & IXGBE_EICR_LSC)
3203 		ixgbe_check_lsc(adapter);
3204 
3205 	switch (hw->mac.type) {
3206 	case ixgbe_mac_82599EB:
3207 		ixgbe_check_sfp_event(adapter, eicr);
3208 		/* Fall through */
3209 	case ixgbe_mac_X540:
3210 	case ixgbe_mac_X550:
3211 	case ixgbe_mac_X550EM_x:
3212 	case ixgbe_mac_x550em_a:
3213 		if (eicr & IXGBE_EICR_ECC) {
3214 			e_info(link, "Received ECC Err, initiating reset\n");
3215 			set_bit(__IXGBE_RESET_REQUESTED, &adapter->state);
3216 			ixgbe_service_event_schedule(adapter);
3217 			IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
3218 		}
3219 		ixgbe_check_overtemp_event(adapter, eicr);
3220 		break;
3221 	default:
3222 		break;
3223 	}
3224 
3225 	ixgbe_check_fan_failure(adapter, eicr);
3226 	if (unlikely(eicr & IXGBE_EICR_TIMESYNC))
3227 		ixgbe_ptp_check_pps_event(adapter);
3228 
3229 	/* would disable interrupts here but EIAM disabled it */
3230 	napi_schedule_irqoff(&q_vector->napi);
3231 
3232 	/*
3233 	 * re-enable link(maybe) and non-queue interrupts, no flush.
3234 	 * ixgbe_poll will re-enable the queue interrupts
3235 	 */
3236 	if (!test_bit(__IXGBE_DOWN, &adapter->state))
3237 		ixgbe_irq_enable(adapter, false, false);
3238 
3239 	return IRQ_HANDLED;
3240 }
3241 
3242 /**
3243  * ixgbe_request_irq - initialize interrupts
3244  * @adapter: board private structure
3245  *
3246  * Attempts to configure interrupts using the best available
3247  * capabilities of the hardware and kernel.
3248  **/
3249 static int ixgbe_request_irq(struct ixgbe_adapter *adapter)
3250 {
3251 	struct net_device *netdev = adapter->netdev;
3252 	int err;
3253 
3254 	if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
3255 		err = ixgbe_request_msix_irqs(adapter);
3256 	else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED)
3257 		err = request_irq(adapter->pdev->irq, ixgbe_intr, 0,
3258 				  netdev->name, adapter);
3259 	else
3260 		err = request_irq(adapter->pdev->irq, ixgbe_intr, IRQF_SHARED,
3261 				  netdev->name, adapter);
3262 
3263 	if (err)
3264 		e_err(probe, "request_irq failed, Error %d\n", err);
3265 
3266 	return err;
3267 }
3268 
3269 static void ixgbe_free_irq(struct ixgbe_adapter *adapter)
3270 {
3271 	int vector;
3272 
3273 	if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
3274 		free_irq(adapter->pdev->irq, adapter);
3275 		return;
3276 	}
3277 
3278 	if (!adapter->msix_entries)
3279 		return;
3280 
3281 	for (vector = 0; vector < adapter->num_q_vectors; vector++) {
3282 		struct ixgbe_q_vector *q_vector = adapter->q_vector[vector];
3283 		struct msix_entry *entry = &adapter->msix_entries[vector];
3284 
3285 		/* free only the irqs that were actually requested */
3286 		if (!q_vector->rx.ring && !q_vector->tx.ring)
3287 			continue;
3288 
3289 		/* clear the affinity_mask in the IRQ descriptor */
3290 		irq_set_affinity_hint(entry->vector, NULL);
3291 
3292 		free_irq(entry->vector, q_vector);
3293 	}
3294 
3295 	free_irq(adapter->msix_entries[vector].vector, adapter);
3296 }
3297 
3298 /**
3299  * ixgbe_irq_disable - Mask off interrupt generation on the NIC
3300  * @adapter: board private structure
3301  **/
3302 static inline void ixgbe_irq_disable(struct ixgbe_adapter *adapter)
3303 {
3304 	switch (adapter->hw.mac.type) {
3305 	case ixgbe_mac_82598EB:
3306 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
3307 		break;
3308 	case ixgbe_mac_82599EB:
3309 	case ixgbe_mac_X540:
3310 	case ixgbe_mac_X550:
3311 	case ixgbe_mac_X550EM_x:
3312 	case ixgbe_mac_x550em_a:
3313 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, 0xFFFF0000);
3314 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(0), ~0);
3315 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(1), ~0);
3316 		break;
3317 	default:
3318 		break;
3319 	}
3320 	IXGBE_WRITE_FLUSH(&adapter->hw);
3321 	if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3322 		int vector;
3323 
3324 		for (vector = 0; vector < adapter->num_q_vectors; vector++)
3325 			synchronize_irq(adapter->msix_entries[vector].vector);
3326 
3327 		synchronize_irq(adapter->msix_entries[vector++].vector);
3328 	} else {
3329 		synchronize_irq(adapter->pdev->irq);
3330 	}
3331 }
3332 
3333 /**
3334  * ixgbe_configure_msi_and_legacy - Initialize PIN (INTA...) and MSI interrupts
3335  *
3336  **/
3337 static void ixgbe_configure_msi_and_legacy(struct ixgbe_adapter *adapter)
3338 {
3339 	struct ixgbe_q_vector *q_vector = adapter->q_vector[0];
3340 
3341 	ixgbe_write_eitr(q_vector);
3342 
3343 	ixgbe_set_ivar(adapter, 0, 0, 0);
3344 	ixgbe_set_ivar(adapter, 1, 0, 0);
3345 
3346 	e_info(hw, "Legacy interrupt IVAR setup done\n");
3347 }
3348 
3349 /**
3350  * ixgbe_configure_tx_ring - Configure 8259x Tx ring after Reset
3351  * @adapter: board private structure
3352  * @ring: structure containing ring specific data
3353  *
3354  * Configure the Tx descriptor ring after a reset.
3355  **/
3356 void ixgbe_configure_tx_ring(struct ixgbe_adapter *adapter,
3357 			     struct ixgbe_ring *ring)
3358 {
3359 	struct ixgbe_hw *hw = &adapter->hw;
3360 	u64 tdba = ring->dma;
3361 	int wait_loop = 10;
3362 	u32 txdctl = IXGBE_TXDCTL_ENABLE;
3363 	u8 reg_idx = ring->reg_idx;
3364 
3365 	/* disable queue to avoid issues while updating state */
3366 	IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), 0);
3367 	IXGBE_WRITE_FLUSH(hw);
3368 
3369 	IXGBE_WRITE_REG(hw, IXGBE_TDBAL(reg_idx),
3370 			(tdba & DMA_BIT_MASK(32)));
3371 	IXGBE_WRITE_REG(hw, IXGBE_TDBAH(reg_idx), (tdba >> 32));
3372 	IXGBE_WRITE_REG(hw, IXGBE_TDLEN(reg_idx),
3373 			ring->count * sizeof(union ixgbe_adv_tx_desc));
3374 	IXGBE_WRITE_REG(hw, IXGBE_TDH(reg_idx), 0);
3375 	IXGBE_WRITE_REG(hw, IXGBE_TDT(reg_idx), 0);
3376 	ring->tail = adapter->io_addr + IXGBE_TDT(reg_idx);
3377 
3378 	/*
3379 	 * set WTHRESH to encourage burst writeback, it should not be set
3380 	 * higher than 1 when:
3381 	 * - ITR is 0 as it could cause false TX hangs
3382 	 * - ITR is set to > 100k int/sec and BQL is enabled
3383 	 *
3384 	 * In order to avoid issues WTHRESH + PTHRESH should always be equal
3385 	 * to or less than the number of on chip descriptors, which is
3386 	 * currently 40.
3387 	 */
3388 	if (!ring->q_vector || (ring->q_vector->itr < IXGBE_100K_ITR))
3389 		txdctl |= 1u << 16;	/* WTHRESH = 1 */
3390 	else
3391 		txdctl |= 8u << 16;	/* WTHRESH = 8 */
3392 
3393 	/*
3394 	 * Setting PTHRESH to 32 both improves performance
3395 	 * and avoids a TX hang with DFP enabled
3396 	 */
3397 	txdctl |= (1u << 8) |	/* HTHRESH = 1 */
3398 		   32;		/* PTHRESH = 32 */
3399 
3400 	/* reinitialize flowdirector state */
3401 	if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
3402 		ring->atr_sample_rate = adapter->atr_sample_rate;
3403 		ring->atr_count = 0;
3404 		set_bit(__IXGBE_TX_FDIR_INIT_DONE, &ring->state);
3405 	} else {
3406 		ring->atr_sample_rate = 0;
3407 	}
3408 
3409 	/* initialize XPS */
3410 	if (!test_and_set_bit(__IXGBE_TX_XPS_INIT_DONE, &ring->state)) {
3411 		struct ixgbe_q_vector *q_vector = ring->q_vector;
3412 
3413 		if (q_vector)
3414 			netif_set_xps_queue(ring->netdev,
3415 					    &q_vector->affinity_mask,
3416 					    ring->queue_index);
3417 	}
3418 
3419 	clear_bit(__IXGBE_HANG_CHECK_ARMED, &ring->state);
3420 
3421 	/* reinitialize tx_buffer_info */
3422 	memset(ring->tx_buffer_info, 0,
3423 	       sizeof(struct ixgbe_tx_buffer) * ring->count);
3424 
3425 	/* enable queue */
3426 	IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), txdctl);
3427 
3428 	/* TXDCTL.EN will return 0 on 82598 if link is down, so skip it */
3429 	if (hw->mac.type == ixgbe_mac_82598EB &&
3430 	    !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
3431 		return;
3432 
3433 	/* poll to verify queue is enabled */
3434 	do {
3435 		usleep_range(1000, 2000);
3436 		txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(reg_idx));
3437 	} while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
3438 	if (!wait_loop)
3439 		hw_dbg(hw, "Could not enable Tx Queue %d\n", reg_idx);
3440 }
3441 
3442 static void ixgbe_setup_mtqc(struct ixgbe_adapter *adapter)
3443 {
3444 	struct ixgbe_hw *hw = &adapter->hw;
3445 	u32 rttdcs, mtqc;
3446 	u8 tcs = netdev_get_num_tc(adapter->netdev);
3447 
3448 	if (hw->mac.type == ixgbe_mac_82598EB)
3449 		return;
3450 
3451 	/* disable the arbiter while setting MTQC */
3452 	rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
3453 	rttdcs |= IXGBE_RTTDCS_ARBDIS;
3454 	IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
3455 
3456 	/* set transmit pool layout */
3457 	if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
3458 		mtqc = IXGBE_MTQC_VT_ENA;
3459 		if (tcs > 4)
3460 			mtqc |= IXGBE_MTQC_RT_ENA | IXGBE_MTQC_8TC_8TQ;
3461 		else if (tcs > 1)
3462 			mtqc |= IXGBE_MTQC_RT_ENA | IXGBE_MTQC_4TC_4TQ;
3463 		else if (adapter->ring_feature[RING_F_VMDQ].mask ==
3464 			 IXGBE_82599_VMDQ_4Q_MASK)
3465 			mtqc |= IXGBE_MTQC_32VF;
3466 		else
3467 			mtqc |= IXGBE_MTQC_64VF;
3468 	} else {
3469 		if (tcs > 4)
3470 			mtqc = IXGBE_MTQC_RT_ENA | IXGBE_MTQC_8TC_8TQ;
3471 		else if (tcs > 1)
3472 			mtqc = IXGBE_MTQC_RT_ENA | IXGBE_MTQC_4TC_4TQ;
3473 		else
3474 			mtqc = IXGBE_MTQC_64Q_1PB;
3475 	}
3476 
3477 	IXGBE_WRITE_REG(hw, IXGBE_MTQC, mtqc);
3478 
3479 	/* Enable Security TX Buffer IFG for multiple pb */
3480 	if (tcs) {
3481 		u32 sectx = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
3482 		sectx |= IXGBE_SECTX_DCB;
3483 		IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, sectx);
3484 	}
3485 
3486 	/* re-enable the arbiter */
3487 	rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
3488 	IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
3489 }
3490 
3491 /**
3492  * ixgbe_configure_tx - Configure 8259x Transmit Unit after Reset
3493  * @adapter: board private structure
3494  *
3495  * Configure the Tx unit of the MAC after a reset.
3496  **/
3497 static void ixgbe_configure_tx(struct ixgbe_adapter *adapter)
3498 {
3499 	struct ixgbe_hw *hw = &adapter->hw;
3500 	u32 dmatxctl;
3501 	u32 i;
3502 
3503 	ixgbe_setup_mtqc(adapter);
3504 
3505 	if (hw->mac.type != ixgbe_mac_82598EB) {
3506 		/* DMATXCTL.EN must be before Tx queues are enabled */
3507 		dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
3508 		dmatxctl |= IXGBE_DMATXCTL_TE;
3509 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
3510 	}
3511 
3512 	/* Setup the HW Tx Head and Tail descriptor pointers */
3513 	for (i = 0; i < adapter->num_tx_queues; i++)
3514 		ixgbe_configure_tx_ring(adapter, adapter->tx_ring[i]);
3515 	for (i = 0; i < adapter->num_xdp_queues; i++)
3516 		ixgbe_configure_tx_ring(adapter, adapter->xdp_ring[i]);
3517 }
3518 
3519 static void ixgbe_enable_rx_drop(struct ixgbe_adapter *adapter,
3520 				 struct ixgbe_ring *ring)
3521 {
3522 	struct ixgbe_hw *hw = &adapter->hw;
3523 	u8 reg_idx = ring->reg_idx;
3524 	u32 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(reg_idx));
3525 
3526 	srrctl |= IXGBE_SRRCTL_DROP_EN;
3527 
3528 	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(reg_idx), srrctl);
3529 }
3530 
3531 static void ixgbe_disable_rx_drop(struct ixgbe_adapter *adapter,
3532 				  struct ixgbe_ring *ring)
3533 {
3534 	struct ixgbe_hw *hw = &adapter->hw;
3535 	u8 reg_idx = ring->reg_idx;
3536 	u32 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(reg_idx));
3537 
3538 	srrctl &= ~IXGBE_SRRCTL_DROP_EN;
3539 
3540 	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(reg_idx), srrctl);
3541 }
3542 
3543 #ifdef CONFIG_IXGBE_DCB
3544 void ixgbe_set_rx_drop_en(struct ixgbe_adapter *adapter)
3545 #else
3546 static void ixgbe_set_rx_drop_en(struct ixgbe_adapter *adapter)
3547 #endif
3548 {
3549 	int i;
3550 	bool pfc_en = adapter->dcb_cfg.pfc_mode_enable;
3551 
3552 	if (adapter->ixgbe_ieee_pfc)
3553 		pfc_en |= !!(adapter->ixgbe_ieee_pfc->pfc_en);
3554 
3555 	/*
3556 	 * We should set the drop enable bit if:
3557 	 *  SR-IOV is enabled
3558 	 *   or
3559 	 *  Number of Rx queues > 1 and flow control is disabled
3560 	 *
3561 	 *  This allows us to avoid head of line blocking for security
3562 	 *  and performance reasons.
3563 	 */
3564 	if (adapter->num_vfs || (adapter->num_rx_queues > 1 &&
3565 	    !(adapter->hw.fc.current_mode & ixgbe_fc_tx_pause) && !pfc_en)) {
3566 		for (i = 0; i < adapter->num_rx_queues; i++)
3567 			ixgbe_enable_rx_drop(adapter, adapter->rx_ring[i]);
3568 	} else {
3569 		for (i = 0; i < adapter->num_rx_queues; i++)
3570 			ixgbe_disable_rx_drop(adapter, adapter->rx_ring[i]);
3571 	}
3572 }
3573 
3574 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
3575 
3576 static void ixgbe_configure_srrctl(struct ixgbe_adapter *adapter,
3577 				   struct ixgbe_ring *rx_ring)
3578 {
3579 	struct ixgbe_hw *hw = &adapter->hw;
3580 	u32 srrctl;
3581 	u8 reg_idx = rx_ring->reg_idx;
3582 
3583 	if (hw->mac.type == ixgbe_mac_82598EB) {
3584 		u16 mask = adapter->ring_feature[RING_F_RSS].mask;
3585 
3586 		/*
3587 		 * if VMDq is not active we must program one srrctl register
3588 		 * per RSS queue since we have enabled RDRXCTL.MVMEN
3589 		 */
3590 		reg_idx &= mask;
3591 	}
3592 
3593 	/* configure header buffer length, needed for RSC */
3594 	srrctl = IXGBE_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT;
3595 
3596 	/* configure the packet buffer length */
3597 	if (test_bit(__IXGBE_RX_3K_BUFFER, &rx_ring->state))
3598 		srrctl |= IXGBE_RXBUFFER_3K >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
3599 	else
3600 		srrctl |= IXGBE_RXBUFFER_2K >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
3601 
3602 	/* configure descriptor type */
3603 	srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
3604 
3605 	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(reg_idx), srrctl);
3606 }
3607 
3608 /**
3609  * ixgbe_rss_indir_tbl_entries - Return RSS indirection table entries
3610  * @adapter: device handle
3611  *
3612  *  - 82598/82599/X540:     128
3613  *  - X550(non-SRIOV mode): 512
3614  *  - X550(SRIOV mode):     64
3615  */
3616 u32 ixgbe_rss_indir_tbl_entries(struct ixgbe_adapter *adapter)
3617 {
3618 	if (adapter->hw.mac.type < ixgbe_mac_X550)
3619 		return 128;
3620 	else if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
3621 		return 64;
3622 	else
3623 		return 512;
3624 }
3625 
3626 /**
3627  * ixgbe_store_key - Write the RSS key to HW
3628  * @adapter: device handle
3629  *
3630  * Write the RSS key stored in adapter.rss_key to HW.
3631  */
3632 void ixgbe_store_key(struct ixgbe_adapter *adapter)
3633 {
3634 	struct ixgbe_hw *hw = &adapter->hw;
3635 	int i;
3636 
3637 	for (i = 0; i < 10; i++)
3638 		IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), adapter->rss_key[i]);
3639 }
3640 
3641 /**
3642  * ixgbe_init_rss_key - Initialize adapter RSS key
3643  * @adapter: device handle
3644  *
3645  * Allocates and initializes the RSS key if it is not allocated.
3646  **/
3647 static inline int ixgbe_init_rss_key(struct ixgbe_adapter *adapter)
3648 {
3649 	u32 *rss_key;
3650 
3651 	if (!adapter->rss_key) {
3652 		rss_key = kzalloc(IXGBE_RSS_KEY_SIZE, GFP_KERNEL);
3653 		if (unlikely(!rss_key))
3654 			return -ENOMEM;
3655 
3656 		netdev_rss_key_fill(rss_key, IXGBE_RSS_KEY_SIZE);
3657 		adapter->rss_key = rss_key;
3658 	}
3659 
3660 	return 0;
3661 }
3662 
3663 /**
3664  * ixgbe_store_reta - Write the RETA table to HW
3665  * @adapter: device handle
3666  *
3667  * Write the RSS redirection table stored in adapter.rss_indir_tbl[] to HW.
3668  */
3669 void ixgbe_store_reta(struct ixgbe_adapter *adapter)
3670 {
3671 	u32 i, reta_entries = ixgbe_rss_indir_tbl_entries(adapter);
3672 	struct ixgbe_hw *hw = &adapter->hw;
3673 	u32 reta = 0;
3674 	u32 indices_multi;
3675 	u8 *indir_tbl = adapter->rss_indir_tbl;
3676 
3677 	/* Fill out the redirection table as follows:
3678 	 *  - 82598:      8 bit wide entries containing pair of 4 bit RSS
3679 	 *    indices.
3680 	 *  - 82599/X540: 8 bit wide entries containing 4 bit RSS index
3681 	 *  - X550:       8 bit wide entries containing 6 bit RSS index
3682 	 */
3683 	if (adapter->hw.mac.type == ixgbe_mac_82598EB)
3684 		indices_multi = 0x11;
3685 	else
3686 		indices_multi = 0x1;
3687 
3688 	/* Write redirection table to HW */
3689 	for (i = 0; i < reta_entries; i++) {
3690 		reta |= indices_multi * indir_tbl[i] << (i & 0x3) * 8;
3691 		if ((i & 3) == 3) {
3692 			if (i < 128)
3693 				IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
3694 			else
3695 				IXGBE_WRITE_REG(hw, IXGBE_ERETA((i >> 2) - 32),
3696 						reta);
3697 			reta = 0;
3698 		}
3699 	}
3700 }
3701 
3702 /**
3703  * ixgbe_store_vfreta - Write the RETA table to HW (x550 devices in SRIOV mode)
3704  * @adapter: device handle
3705  *
3706  * Write the RSS redirection table stored in adapter.rss_indir_tbl[] to HW.
3707  */
3708 static void ixgbe_store_vfreta(struct ixgbe_adapter *adapter)
3709 {
3710 	u32 i, reta_entries = ixgbe_rss_indir_tbl_entries(adapter);
3711 	struct ixgbe_hw *hw = &adapter->hw;
3712 	u32 vfreta = 0;
3713 	unsigned int pf_pool = adapter->num_vfs;
3714 
3715 	/* Write redirection table to HW */
3716 	for (i = 0; i < reta_entries; i++) {
3717 		vfreta |= (u32)adapter->rss_indir_tbl[i] << (i & 0x3) * 8;
3718 		if ((i & 3) == 3) {
3719 			IXGBE_WRITE_REG(hw, IXGBE_PFVFRETA(i >> 2, pf_pool),
3720 					vfreta);
3721 			vfreta = 0;
3722 		}
3723 	}
3724 }
3725 
3726 static void ixgbe_setup_reta(struct ixgbe_adapter *adapter)
3727 {
3728 	u32 i, j;
3729 	u32 reta_entries = ixgbe_rss_indir_tbl_entries(adapter);
3730 	u16 rss_i = adapter->ring_feature[RING_F_RSS].indices;
3731 
3732 	/* Program table for at least 4 queues w/ SR-IOV so that VFs can
3733 	 * make full use of any rings they may have.  We will use the
3734 	 * PSRTYPE register to control how many rings we use within the PF.
3735 	 */
3736 	if ((adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) && (rss_i < 4))
3737 		rss_i = 4;
3738 
3739 	/* Fill out hash function seeds */
3740 	ixgbe_store_key(adapter);
3741 
3742 	/* Fill out redirection table */
3743 	memset(adapter->rss_indir_tbl, 0, sizeof(adapter->rss_indir_tbl));
3744 
3745 	for (i = 0, j = 0; i < reta_entries; i++, j++) {
3746 		if (j == rss_i)
3747 			j = 0;
3748 
3749 		adapter->rss_indir_tbl[i] = j;
3750 	}
3751 
3752 	ixgbe_store_reta(adapter);
3753 }
3754 
3755 static void ixgbe_setup_vfreta(struct ixgbe_adapter *adapter)
3756 {
3757 	struct ixgbe_hw *hw = &adapter->hw;
3758 	u16 rss_i = adapter->ring_feature[RING_F_RSS].indices;
3759 	unsigned int pf_pool = adapter->num_vfs;
3760 	int i, j;
3761 
3762 	/* Fill out hash function seeds */
3763 	for (i = 0; i < 10; i++)
3764 		IXGBE_WRITE_REG(hw, IXGBE_PFVFRSSRK(i, pf_pool),
3765 				*(adapter->rss_key + i));
3766 
3767 	/* Fill out the redirection table */
3768 	for (i = 0, j = 0; i < 64; i++, j++) {
3769 		if (j == rss_i)
3770 			j = 0;
3771 
3772 		adapter->rss_indir_tbl[i] = j;
3773 	}
3774 
3775 	ixgbe_store_vfreta(adapter);
3776 }
3777 
3778 static void ixgbe_setup_mrqc(struct ixgbe_adapter *adapter)
3779 {
3780 	struct ixgbe_hw *hw = &adapter->hw;
3781 	u32 mrqc = 0, rss_field = 0, vfmrqc = 0;
3782 	u32 rxcsum;
3783 
3784 	/* Disable indicating checksum in descriptor, enables RSS hash */
3785 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
3786 	rxcsum |= IXGBE_RXCSUM_PCSD;
3787 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
3788 
3789 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
3790 		if (adapter->ring_feature[RING_F_RSS].mask)
3791 			mrqc = IXGBE_MRQC_RSSEN;
3792 	} else {
3793 		u8 tcs = netdev_get_num_tc(adapter->netdev);
3794 
3795 		if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
3796 			if (tcs > 4)
3797 				mrqc = IXGBE_MRQC_VMDQRT8TCEN;	/* 8 TCs */
3798 			else if (tcs > 1)
3799 				mrqc = IXGBE_MRQC_VMDQRT4TCEN;	/* 4 TCs */
3800 			else if (adapter->ring_feature[RING_F_VMDQ].mask ==
3801 				 IXGBE_82599_VMDQ_4Q_MASK)
3802 				mrqc = IXGBE_MRQC_VMDQRSS32EN;
3803 			else
3804 				mrqc = IXGBE_MRQC_VMDQRSS64EN;
3805 		} else {
3806 			if (tcs > 4)
3807 				mrqc = IXGBE_MRQC_RTRSS8TCEN;
3808 			else if (tcs > 1)
3809 				mrqc = IXGBE_MRQC_RTRSS4TCEN;
3810 			else
3811 				mrqc = IXGBE_MRQC_RSSEN;
3812 		}
3813 	}
3814 
3815 	/* Perform hash on these packet types */
3816 	rss_field |= IXGBE_MRQC_RSS_FIELD_IPV4 |
3817 		     IXGBE_MRQC_RSS_FIELD_IPV4_TCP |
3818 		     IXGBE_MRQC_RSS_FIELD_IPV6 |
3819 		     IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
3820 
3821 	if (adapter->flags2 & IXGBE_FLAG2_RSS_FIELD_IPV4_UDP)
3822 		rss_field |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
3823 	if (adapter->flags2 & IXGBE_FLAG2_RSS_FIELD_IPV6_UDP)
3824 		rss_field |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
3825 
3826 	if ((hw->mac.type >= ixgbe_mac_X550) &&
3827 	    (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)) {
3828 		unsigned int pf_pool = adapter->num_vfs;
3829 
3830 		/* Enable VF RSS mode */
3831 		mrqc |= IXGBE_MRQC_MULTIPLE_RSS;
3832 		IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
3833 
3834 		/* Setup RSS through the VF registers */
3835 		ixgbe_setup_vfreta(adapter);
3836 		vfmrqc = IXGBE_MRQC_RSSEN;
3837 		vfmrqc |= rss_field;
3838 		IXGBE_WRITE_REG(hw, IXGBE_PFVFMRQC(pf_pool), vfmrqc);
3839 	} else {
3840 		ixgbe_setup_reta(adapter);
3841 		mrqc |= rss_field;
3842 		IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
3843 	}
3844 }
3845 
3846 /**
3847  * ixgbe_configure_rscctl - enable RSC for the indicated ring
3848  * @adapter:    address of board private structure
3849  * @index:      index of ring to set
3850  **/
3851 static void ixgbe_configure_rscctl(struct ixgbe_adapter *adapter,
3852 				   struct ixgbe_ring *ring)
3853 {
3854 	struct ixgbe_hw *hw = &adapter->hw;
3855 	u32 rscctrl;
3856 	u8 reg_idx = ring->reg_idx;
3857 
3858 	if (!ring_is_rsc_enabled(ring))
3859 		return;
3860 
3861 	rscctrl = IXGBE_READ_REG(hw, IXGBE_RSCCTL(reg_idx));
3862 	rscctrl |= IXGBE_RSCCTL_RSCEN;
3863 	/*
3864 	 * we must limit the number of descriptors so that the
3865 	 * total size of max desc * buf_len is not greater
3866 	 * than 65536
3867 	 */
3868 	rscctrl |= IXGBE_RSCCTL_MAXDESC_16;
3869 	IXGBE_WRITE_REG(hw, IXGBE_RSCCTL(reg_idx), rscctrl);
3870 }
3871 
3872 #define IXGBE_MAX_RX_DESC_POLL 10
3873 static void ixgbe_rx_desc_queue_enable(struct ixgbe_adapter *adapter,
3874 				       struct ixgbe_ring *ring)
3875 {
3876 	struct ixgbe_hw *hw = &adapter->hw;
3877 	int wait_loop = IXGBE_MAX_RX_DESC_POLL;
3878 	u32 rxdctl;
3879 	u8 reg_idx = ring->reg_idx;
3880 
3881 	if (ixgbe_removed(hw->hw_addr))
3882 		return;
3883 	/* RXDCTL.EN will return 0 on 82598 if link is down, so skip it */
3884 	if (hw->mac.type == ixgbe_mac_82598EB &&
3885 	    !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
3886 		return;
3887 
3888 	do {
3889 		usleep_range(1000, 2000);
3890 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
3891 	} while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
3892 
3893 	if (!wait_loop) {
3894 		e_err(drv, "RXDCTL.ENABLE on Rx queue %d not set within "
3895 		      "the polling period\n", reg_idx);
3896 	}
3897 }
3898 
3899 void ixgbe_disable_rx_queue(struct ixgbe_adapter *adapter,
3900 			    struct ixgbe_ring *ring)
3901 {
3902 	struct ixgbe_hw *hw = &adapter->hw;
3903 	int wait_loop = IXGBE_MAX_RX_DESC_POLL;
3904 	u32 rxdctl;
3905 	u8 reg_idx = ring->reg_idx;
3906 
3907 	if (ixgbe_removed(hw->hw_addr))
3908 		return;
3909 	rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
3910 	rxdctl &= ~IXGBE_RXDCTL_ENABLE;
3911 
3912 	/* write value back with RXDCTL.ENABLE bit cleared */
3913 	IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(reg_idx), rxdctl);
3914 
3915 	if (hw->mac.type == ixgbe_mac_82598EB &&
3916 	    !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
3917 		return;
3918 
3919 	/* the hardware may take up to 100us to really disable the rx queue */
3920 	do {
3921 		udelay(10);
3922 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
3923 	} while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
3924 
3925 	if (!wait_loop) {
3926 		e_err(drv, "RXDCTL.ENABLE on Rx queue %d not cleared within "
3927 		      "the polling period\n", reg_idx);
3928 	}
3929 }
3930 
3931 void ixgbe_configure_rx_ring(struct ixgbe_adapter *adapter,
3932 			     struct ixgbe_ring *ring)
3933 {
3934 	struct ixgbe_hw *hw = &adapter->hw;
3935 	union ixgbe_adv_rx_desc *rx_desc;
3936 	u64 rdba = ring->dma;
3937 	u32 rxdctl;
3938 	u8 reg_idx = ring->reg_idx;
3939 
3940 	/* disable queue to avoid issues while updating state */
3941 	rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
3942 	ixgbe_disable_rx_queue(adapter, ring);
3943 
3944 	IXGBE_WRITE_REG(hw, IXGBE_RDBAL(reg_idx), (rdba & DMA_BIT_MASK(32)));
3945 	IXGBE_WRITE_REG(hw, IXGBE_RDBAH(reg_idx), (rdba >> 32));
3946 	IXGBE_WRITE_REG(hw, IXGBE_RDLEN(reg_idx),
3947 			ring->count * sizeof(union ixgbe_adv_rx_desc));
3948 	/* Force flushing of IXGBE_RDLEN to prevent MDD */
3949 	IXGBE_WRITE_FLUSH(hw);
3950 
3951 	IXGBE_WRITE_REG(hw, IXGBE_RDH(reg_idx), 0);
3952 	IXGBE_WRITE_REG(hw, IXGBE_RDT(reg_idx), 0);
3953 	ring->tail = adapter->io_addr + IXGBE_RDT(reg_idx);
3954 
3955 	ixgbe_configure_srrctl(adapter, ring);
3956 	ixgbe_configure_rscctl(adapter, ring);
3957 
3958 	if (hw->mac.type == ixgbe_mac_82598EB) {
3959 		/*
3960 		 * enable cache line friendly hardware writes:
3961 		 * PTHRESH=32 descriptors (half the internal cache),
3962 		 * this also removes ugly rx_no_buffer_count increment
3963 		 * HTHRESH=4 descriptors (to minimize latency on fetch)
3964 		 * WTHRESH=8 burst writeback up to two cache lines
3965 		 */
3966 		rxdctl &= ~0x3FFFFF;
3967 		rxdctl |=  0x080420;
3968 #if (PAGE_SIZE < 8192)
3969 	} else {
3970 		rxdctl &= ~(IXGBE_RXDCTL_RLPMLMASK |
3971 			    IXGBE_RXDCTL_RLPML_EN);
3972 
3973 		/* Limit the maximum frame size so we don't overrun the skb */
3974 		if (ring_uses_build_skb(ring) &&
3975 		    !test_bit(__IXGBE_RX_3K_BUFFER, &ring->state))
3976 			rxdctl |= IXGBE_MAX_2K_FRAME_BUILD_SKB |
3977 				  IXGBE_RXDCTL_RLPML_EN;
3978 #endif
3979 	}
3980 
3981 	/* initialize rx_buffer_info */
3982 	memset(ring->rx_buffer_info, 0,
3983 	       sizeof(struct ixgbe_rx_buffer) * ring->count);
3984 
3985 	/* initialize Rx descriptor 0 */
3986 	rx_desc = IXGBE_RX_DESC(ring, 0);
3987 	rx_desc->wb.upper.length = 0;
3988 
3989 	/* enable receive descriptor ring */
3990 	rxdctl |= IXGBE_RXDCTL_ENABLE;
3991 	IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(reg_idx), rxdctl);
3992 
3993 	ixgbe_rx_desc_queue_enable(adapter, ring);
3994 	ixgbe_alloc_rx_buffers(ring, ixgbe_desc_unused(ring));
3995 }
3996 
3997 static void ixgbe_setup_psrtype(struct ixgbe_adapter *adapter)
3998 {
3999 	struct ixgbe_hw *hw = &adapter->hw;
4000 	int rss_i = adapter->ring_feature[RING_F_RSS].indices;
4001 	u16 pool;
4002 
4003 	/* PSRTYPE must be initialized in non 82598 adapters */
4004 	u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
4005 		      IXGBE_PSRTYPE_UDPHDR |
4006 		      IXGBE_PSRTYPE_IPV4HDR |
4007 		      IXGBE_PSRTYPE_L2HDR |
4008 		      IXGBE_PSRTYPE_IPV6HDR;
4009 
4010 	if (hw->mac.type == ixgbe_mac_82598EB)
4011 		return;
4012 
4013 	if (rss_i > 3)
4014 		psrtype |= 2u << 29;
4015 	else if (rss_i > 1)
4016 		psrtype |= 1u << 29;
4017 
4018 	for_each_set_bit(pool, &adapter->fwd_bitmask, 32)
4019 		IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(VMDQ_P(pool)), psrtype);
4020 }
4021 
4022 static void ixgbe_configure_virtualization(struct ixgbe_adapter *adapter)
4023 {
4024 	struct ixgbe_hw *hw = &adapter->hw;
4025 	u32 reg_offset, vf_shift;
4026 	u32 gcr_ext, vmdctl;
4027 	int i;
4028 
4029 	if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
4030 		return;
4031 
4032 	vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
4033 	vmdctl |= IXGBE_VMD_CTL_VMDQ_EN;
4034 	vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
4035 	vmdctl |= VMDQ_P(0) << IXGBE_VT_CTL_POOL_SHIFT;
4036 	vmdctl |= IXGBE_VT_CTL_REPLEN;
4037 	IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
4038 
4039 	vf_shift = VMDQ_P(0) % 32;
4040 	reg_offset = (VMDQ_P(0) >= 32) ? 1 : 0;
4041 
4042 	/* Enable only the PF's pool for Tx/Rx */
4043 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), GENMASK(31, vf_shift));
4044 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset ^ 1), reg_offset - 1);
4045 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), GENMASK(31, vf_shift));
4046 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset ^ 1), reg_offset - 1);
4047 	if (adapter->bridge_mode == BRIDGE_MODE_VEB)
4048 		IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
4049 
4050 	/* Map PF MAC address in RAR Entry 0 to first pool following VFs */
4051 	hw->mac.ops.set_vmdq(hw, 0, VMDQ_P(0));
4052 
4053 	/* clear VLAN promisc flag so VFTA will be updated if necessary */
4054 	adapter->flags2 &= ~IXGBE_FLAG2_VLAN_PROMISC;
4055 
4056 	/*
4057 	 * Set up VF register offsets for selected VT Mode,
4058 	 * i.e. 32 or 64 VFs for SR-IOV
4059 	 */
4060 	switch (adapter->ring_feature[RING_F_VMDQ].mask) {
4061 	case IXGBE_82599_VMDQ_8Q_MASK:
4062 		gcr_ext = IXGBE_GCR_EXT_VT_MODE_16;
4063 		break;
4064 	case IXGBE_82599_VMDQ_4Q_MASK:
4065 		gcr_ext = IXGBE_GCR_EXT_VT_MODE_32;
4066 		break;
4067 	default:
4068 		gcr_ext = IXGBE_GCR_EXT_VT_MODE_64;
4069 		break;
4070 	}
4071 
4072 	IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
4073 
4074 	for (i = 0; i < adapter->num_vfs; i++) {
4075 		/* configure spoof checking */
4076 		ixgbe_ndo_set_vf_spoofchk(adapter->netdev, i,
4077 					  adapter->vfinfo[i].spoofchk_enabled);
4078 
4079 		/* Enable/Disable RSS query feature  */
4080 		ixgbe_ndo_set_vf_rss_query_en(adapter->netdev, i,
4081 					  adapter->vfinfo[i].rss_query_enabled);
4082 	}
4083 }
4084 
4085 static void ixgbe_set_rx_buffer_len(struct ixgbe_adapter *adapter)
4086 {
4087 	struct ixgbe_hw *hw = &adapter->hw;
4088 	struct net_device *netdev = adapter->netdev;
4089 	int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
4090 	struct ixgbe_ring *rx_ring;
4091 	int i;
4092 	u32 mhadd, hlreg0;
4093 
4094 #ifdef IXGBE_FCOE
4095 	/* adjust max frame to be able to do baby jumbo for FCoE */
4096 	if ((adapter->flags & IXGBE_FLAG_FCOE_ENABLED) &&
4097 	    (max_frame < IXGBE_FCOE_JUMBO_FRAME_SIZE))
4098 		max_frame = IXGBE_FCOE_JUMBO_FRAME_SIZE;
4099 
4100 #endif /* IXGBE_FCOE */
4101 
4102 	/* adjust max frame to be at least the size of a standard frame */
4103 	if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
4104 		max_frame = (ETH_FRAME_LEN + ETH_FCS_LEN);
4105 
4106 	mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
4107 	if (max_frame != (mhadd >> IXGBE_MHADD_MFS_SHIFT)) {
4108 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
4109 		mhadd |= max_frame << IXGBE_MHADD_MFS_SHIFT;
4110 
4111 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
4112 	}
4113 
4114 	hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
4115 	/* set jumbo enable since MHADD.MFS is keeping size locked at max_frame */
4116 	hlreg0 |= IXGBE_HLREG0_JUMBOEN;
4117 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
4118 
4119 	/*
4120 	 * Setup the HW Rx Head and Tail Descriptor Pointers and
4121 	 * the Base and Length of the Rx Descriptor Ring
4122 	 */
4123 	for (i = 0; i < adapter->num_rx_queues; i++) {
4124 		rx_ring = adapter->rx_ring[i];
4125 
4126 		clear_ring_rsc_enabled(rx_ring);
4127 		clear_bit(__IXGBE_RX_3K_BUFFER, &rx_ring->state);
4128 		clear_bit(__IXGBE_RX_BUILD_SKB_ENABLED, &rx_ring->state);
4129 
4130 		if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)
4131 			set_ring_rsc_enabled(rx_ring);
4132 
4133 		if (test_bit(__IXGBE_RX_FCOE, &rx_ring->state))
4134 			set_bit(__IXGBE_RX_3K_BUFFER, &rx_ring->state);
4135 
4136 		clear_bit(__IXGBE_RX_BUILD_SKB_ENABLED, &rx_ring->state);
4137 		if (adapter->flags2 & IXGBE_FLAG2_RX_LEGACY)
4138 			continue;
4139 
4140 		set_bit(__IXGBE_RX_BUILD_SKB_ENABLED, &rx_ring->state);
4141 
4142 #if (PAGE_SIZE < 8192)
4143 		if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)
4144 			set_bit(__IXGBE_RX_3K_BUFFER, &rx_ring->state);
4145 
4146 		if (IXGBE_2K_TOO_SMALL_WITH_PADDING ||
4147 		    (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)))
4148 			set_bit(__IXGBE_RX_3K_BUFFER, &rx_ring->state);
4149 #endif
4150 	}
4151 }
4152 
4153 static void ixgbe_setup_rdrxctl(struct ixgbe_adapter *adapter)
4154 {
4155 	struct ixgbe_hw *hw = &adapter->hw;
4156 	u32 rdrxctl = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
4157 
4158 	switch (hw->mac.type) {
4159 	case ixgbe_mac_82598EB:
4160 		/*
4161 		 * For VMDq support of different descriptor types or
4162 		 * buffer sizes through the use of multiple SRRCTL
4163 		 * registers, RDRXCTL.MVMEN must be set to 1
4164 		 *
4165 		 * also, the manual doesn't mention it clearly but DCA hints
4166 		 * will only use queue 0's tags unless this bit is set.  Side
4167 		 * effects of setting this bit are only that SRRCTL must be
4168 		 * fully programmed [0..15]
4169 		 */
4170 		rdrxctl |= IXGBE_RDRXCTL_MVMEN;
4171 		break;
4172 	case ixgbe_mac_X550:
4173 	case ixgbe_mac_X550EM_x:
4174 	case ixgbe_mac_x550em_a:
4175 		if (adapter->num_vfs)
4176 			rdrxctl |= IXGBE_RDRXCTL_PSP;
4177 		/* fall through for older HW */
4178 	case ixgbe_mac_82599EB:
4179 	case ixgbe_mac_X540:
4180 		/* Disable RSC for ACK packets */
4181 		IXGBE_WRITE_REG(hw, IXGBE_RSCDBU,
4182 		   (IXGBE_RSCDBU_RSCACKDIS | IXGBE_READ_REG(hw, IXGBE_RSCDBU)));
4183 		rdrxctl &= ~IXGBE_RDRXCTL_RSCFRSTSIZE;
4184 		/* hardware requires some bits to be set by default */
4185 		rdrxctl |= (IXGBE_RDRXCTL_RSCACKC | IXGBE_RDRXCTL_FCOE_WRFIX);
4186 		rdrxctl |= IXGBE_RDRXCTL_CRCSTRIP;
4187 		break;
4188 	default:
4189 		/* We should do nothing since we don't know this hardware */
4190 		return;
4191 	}
4192 
4193 	IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rdrxctl);
4194 }
4195 
4196 /**
4197  * ixgbe_configure_rx - Configure 8259x Receive Unit after Reset
4198  * @adapter: board private structure
4199  *
4200  * Configure the Rx unit of the MAC after a reset.
4201  **/
4202 static void ixgbe_configure_rx(struct ixgbe_adapter *adapter)
4203 {
4204 	struct ixgbe_hw *hw = &adapter->hw;
4205 	int i;
4206 	u32 rxctrl, rfctl;
4207 
4208 	/* disable receives while setting up the descriptors */
4209 	hw->mac.ops.disable_rx(hw);
4210 
4211 	ixgbe_setup_psrtype(adapter);
4212 	ixgbe_setup_rdrxctl(adapter);
4213 
4214 	/* RSC Setup */
4215 	rfctl = IXGBE_READ_REG(hw, IXGBE_RFCTL);
4216 	rfctl &= ~IXGBE_RFCTL_RSC_DIS;
4217 	if (!(adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED))
4218 		rfctl |= IXGBE_RFCTL_RSC_DIS;
4219 
4220 	/* disable NFS filtering */
4221 	rfctl |= (IXGBE_RFCTL_NFSW_DIS | IXGBE_RFCTL_NFSR_DIS);
4222 	IXGBE_WRITE_REG(hw, IXGBE_RFCTL, rfctl);
4223 
4224 	/* Program registers for the distribution of queues */
4225 	ixgbe_setup_mrqc(adapter);
4226 
4227 	/* set_rx_buffer_len must be called before ring initialization */
4228 	ixgbe_set_rx_buffer_len(adapter);
4229 
4230 	/*
4231 	 * Setup the HW Rx Head and Tail Descriptor Pointers and
4232 	 * the Base and Length of the Rx Descriptor Ring
4233 	 */
4234 	for (i = 0; i < adapter->num_rx_queues; i++)
4235 		ixgbe_configure_rx_ring(adapter, adapter->rx_ring[i]);
4236 
4237 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
4238 	/* disable drop enable for 82598 parts */
4239 	if (hw->mac.type == ixgbe_mac_82598EB)
4240 		rxctrl |= IXGBE_RXCTRL_DMBYPS;
4241 
4242 	/* enable all receives */
4243 	rxctrl |= IXGBE_RXCTRL_RXEN;
4244 	hw->mac.ops.enable_rx_dma(hw, rxctrl);
4245 }
4246 
4247 static int ixgbe_vlan_rx_add_vid(struct net_device *netdev,
4248 				 __be16 proto, u16 vid)
4249 {
4250 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
4251 	struct ixgbe_hw *hw = &adapter->hw;
4252 
4253 	/* add VID to filter table */
4254 	if (!vid || !(adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC))
4255 		hw->mac.ops.set_vfta(&adapter->hw, vid, VMDQ_P(0), true, !!vid);
4256 
4257 	set_bit(vid, adapter->active_vlans);
4258 
4259 	return 0;
4260 }
4261 
4262 static int ixgbe_find_vlvf_entry(struct ixgbe_hw *hw, u32 vlan)
4263 {
4264 	u32 vlvf;
4265 	int idx;
4266 
4267 	/* short cut the special case */
4268 	if (vlan == 0)
4269 		return 0;
4270 
4271 	/* Search for the vlan id in the VLVF entries */
4272 	for (idx = IXGBE_VLVF_ENTRIES; --idx;) {
4273 		vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(idx));
4274 		if ((vlvf & VLAN_VID_MASK) == vlan)
4275 			break;
4276 	}
4277 
4278 	return idx;
4279 }
4280 
4281 void ixgbe_update_pf_promisc_vlvf(struct ixgbe_adapter *adapter, u32 vid)
4282 {
4283 	struct ixgbe_hw *hw = &adapter->hw;
4284 	u32 bits, word;
4285 	int idx;
4286 
4287 	idx = ixgbe_find_vlvf_entry(hw, vid);
4288 	if (!idx)
4289 		return;
4290 
4291 	/* See if any other pools are set for this VLAN filter
4292 	 * entry other than the PF.
4293 	 */
4294 	word = idx * 2 + (VMDQ_P(0) / 32);
4295 	bits = ~BIT(VMDQ_P(0) % 32);
4296 	bits &= IXGBE_READ_REG(hw, IXGBE_VLVFB(word));
4297 
4298 	/* Disable the filter so this falls into the default pool. */
4299 	if (!bits && !IXGBE_READ_REG(hw, IXGBE_VLVFB(word ^ 1))) {
4300 		if (!(adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC))
4301 			IXGBE_WRITE_REG(hw, IXGBE_VLVFB(word), 0);
4302 		IXGBE_WRITE_REG(hw, IXGBE_VLVF(idx), 0);
4303 	}
4304 }
4305 
4306 static int ixgbe_vlan_rx_kill_vid(struct net_device *netdev,
4307 				  __be16 proto, u16 vid)
4308 {
4309 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
4310 	struct ixgbe_hw *hw = &adapter->hw;
4311 
4312 	/* remove VID from filter table */
4313 	if (vid && !(adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC))
4314 		hw->mac.ops.set_vfta(hw, vid, VMDQ_P(0), false, true);
4315 
4316 	clear_bit(vid, adapter->active_vlans);
4317 
4318 	return 0;
4319 }
4320 
4321 /**
4322  * ixgbe_vlan_strip_disable - helper to disable hw vlan stripping
4323  * @adapter: driver data
4324  */
4325 static void ixgbe_vlan_strip_disable(struct ixgbe_adapter *adapter)
4326 {
4327 	struct ixgbe_hw *hw = &adapter->hw;
4328 	u32 vlnctrl;
4329 	int i, j;
4330 
4331 	switch (hw->mac.type) {
4332 	case ixgbe_mac_82598EB:
4333 		vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
4334 		vlnctrl &= ~IXGBE_VLNCTRL_VME;
4335 		IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
4336 		break;
4337 	case ixgbe_mac_82599EB:
4338 	case ixgbe_mac_X540:
4339 	case ixgbe_mac_X550:
4340 	case ixgbe_mac_X550EM_x:
4341 	case ixgbe_mac_x550em_a:
4342 		for (i = 0; i < adapter->num_rx_queues; i++) {
4343 			struct ixgbe_ring *ring = adapter->rx_ring[i];
4344 
4345 			if (ring->l2_accel_priv)
4346 				continue;
4347 			j = ring->reg_idx;
4348 			vlnctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
4349 			vlnctrl &= ~IXGBE_RXDCTL_VME;
4350 			IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), vlnctrl);
4351 		}
4352 		break;
4353 	default:
4354 		break;
4355 	}
4356 }
4357 
4358 /**
4359  * ixgbe_vlan_strip_enable - helper to enable hw vlan stripping
4360  * @adapter: driver data
4361  */
4362 static void ixgbe_vlan_strip_enable(struct ixgbe_adapter *adapter)
4363 {
4364 	struct ixgbe_hw *hw = &adapter->hw;
4365 	u32 vlnctrl;
4366 	int i, j;
4367 
4368 	switch (hw->mac.type) {
4369 	case ixgbe_mac_82598EB:
4370 		vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
4371 		vlnctrl |= IXGBE_VLNCTRL_VME;
4372 		IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
4373 		break;
4374 	case ixgbe_mac_82599EB:
4375 	case ixgbe_mac_X540:
4376 	case ixgbe_mac_X550:
4377 	case ixgbe_mac_X550EM_x:
4378 	case ixgbe_mac_x550em_a:
4379 		for (i = 0; i < adapter->num_rx_queues; i++) {
4380 			struct ixgbe_ring *ring = adapter->rx_ring[i];
4381 
4382 			if (ring->l2_accel_priv)
4383 				continue;
4384 			j = ring->reg_idx;
4385 			vlnctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
4386 			vlnctrl |= IXGBE_RXDCTL_VME;
4387 			IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), vlnctrl);
4388 		}
4389 		break;
4390 	default:
4391 		break;
4392 	}
4393 }
4394 
4395 static void ixgbe_vlan_promisc_enable(struct ixgbe_adapter *adapter)
4396 {
4397 	struct ixgbe_hw *hw = &adapter->hw;
4398 	u32 vlnctrl, i;
4399 
4400 	vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
4401 
4402 	if (adapter->flags & IXGBE_FLAG_VMDQ_ENABLED) {
4403 	/* For VMDq and SR-IOV we must leave VLAN filtering enabled */
4404 		vlnctrl |= IXGBE_VLNCTRL_VFE;
4405 		IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
4406 	} else {
4407 		vlnctrl &= ~IXGBE_VLNCTRL_VFE;
4408 		IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
4409 		return;
4410 	}
4411 
4412 	/* Nothing to do for 82598 */
4413 	if (hw->mac.type == ixgbe_mac_82598EB)
4414 		return;
4415 
4416 	/* We are already in VLAN promisc, nothing to do */
4417 	if (adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC)
4418 		return;
4419 
4420 	/* Set flag so we don't redo unnecessary work */
4421 	adapter->flags2 |= IXGBE_FLAG2_VLAN_PROMISC;
4422 
4423 	/* Add PF to all active pools */
4424 	for (i = IXGBE_VLVF_ENTRIES; --i;) {
4425 		u32 reg_offset = IXGBE_VLVFB(i * 2 + VMDQ_P(0) / 32);
4426 		u32 vlvfb = IXGBE_READ_REG(hw, reg_offset);
4427 
4428 		vlvfb |= BIT(VMDQ_P(0) % 32);
4429 		IXGBE_WRITE_REG(hw, reg_offset, vlvfb);
4430 	}
4431 
4432 	/* Set all bits in the VLAN filter table array */
4433 	for (i = hw->mac.vft_size; i--;)
4434 		IXGBE_WRITE_REG(hw, IXGBE_VFTA(i), ~0U);
4435 }
4436 
4437 #define VFTA_BLOCK_SIZE 8
4438 static void ixgbe_scrub_vfta(struct ixgbe_adapter *adapter, u32 vfta_offset)
4439 {
4440 	struct ixgbe_hw *hw = &adapter->hw;
4441 	u32 vfta[VFTA_BLOCK_SIZE] = { 0 };
4442 	u32 vid_start = vfta_offset * 32;
4443 	u32 vid_end = vid_start + (VFTA_BLOCK_SIZE * 32);
4444 	u32 i, vid, word, bits;
4445 
4446 	for (i = IXGBE_VLVF_ENTRIES; --i;) {
4447 		u32 vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(i));
4448 
4449 		/* pull VLAN ID from VLVF */
4450 		vid = vlvf & VLAN_VID_MASK;
4451 
4452 		/* only concern outselves with a certain range */
4453 		if (vid < vid_start || vid >= vid_end)
4454 			continue;
4455 
4456 		if (vlvf) {
4457 			/* record VLAN ID in VFTA */
4458 			vfta[(vid - vid_start) / 32] |= BIT(vid % 32);
4459 
4460 			/* if PF is part of this then continue */
4461 			if (test_bit(vid, adapter->active_vlans))
4462 				continue;
4463 		}
4464 
4465 		/* remove PF from the pool */
4466 		word = i * 2 + VMDQ_P(0) / 32;
4467 		bits = ~BIT(VMDQ_P(0) % 32);
4468 		bits &= IXGBE_READ_REG(hw, IXGBE_VLVFB(word));
4469 		IXGBE_WRITE_REG(hw, IXGBE_VLVFB(word), bits);
4470 	}
4471 
4472 	/* extract values from active_vlans and write back to VFTA */
4473 	for (i = VFTA_BLOCK_SIZE; i--;) {
4474 		vid = (vfta_offset + i) * 32;
4475 		word = vid / BITS_PER_LONG;
4476 		bits = vid % BITS_PER_LONG;
4477 
4478 		vfta[i] |= adapter->active_vlans[word] >> bits;
4479 
4480 		IXGBE_WRITE_REG(hw, IXGBE_VFTA(vfta_offset + i), vfta[i]);
4481 	}
4482 }
4483 
4484 static void ixgbe_vlan_promisc_disable(struct ixgbe_adapter *adapter)
4485 {
4486 	struct ixgbe_hw *hw = &adapter->hw;
4487 	u32 vlnctrl, i;
4488 
4489 	/* Set VLAN filtering to enabled */
4490 	vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
4491 	vlnctrl |= IXGBE_VLNCTRL_VFE;
4492 	IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
4493 
4494 	if (!(adapter->flags & IXGBE_FLAG_VMDQ_ENABLED) ||
4495 	    hw->mac.type == ixgbe_mac_82598EB)
4496 		return;
4497 
4498 	/* We are not in VLAN promisc, nothing to do */
4499 	if (!(adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC))
4500 		return;
4501 
4502 	/* Set flag so we don't redo unnecessary work */
4503 	adapter->flags2 &= ~IXGBE_FLAG2_VLAN_PROMISC;
4504 
4505 	for (i = 0; i < hw->mac.vft_size; i += VFTA_BLOCK_SIZE)
4506 		ixgbe_scrub_vfta(adapter, i);
4507 }
4508 
4509 static void ixgbe_restore_vlan(struct ixgbe_adapter *adapter)
4510 {
4511 	u16 vid = 1;
4512 
4513 	ixgbe_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), 0);
4514 
4515 	for_each_set_bit_from(vid, adapter->active_vlans, VLAN_N_VID)
4516 		ixgbe_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
4517 }
4518 
4519 /**
4520  * ixgbe_write_mc_addr_list - write multicast addresses to MTA
4521  * @netdev: network interface device structure
4522  *
4523  * Writes multicast address list to the MTA hash table.
4524  * Returns: -ENOMEM on failure
4525  *                0 on no addresses written
4526  *                X on writing X addresses to MTA
4527  **/
4528 static int ixgbe_write_mc_addr_list(struct net_device *netdev)
4529 {
4530 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
4531 	struct ixgbe_hw *hw = &adapter->hw;
4532 
4533 	if (!netif_running(netdev))
4534 		return 0;
4535 
4536 	if (hw->mac.ops.update_mc_addr_list)
4537 		hw->mac.ops.update_mc_addr_list(hw, netdev);
4538 	else
4539 		return -ENOMEM;
4540 
4541 #ifdef CONFIG_PCI_IOV
4542 	ixgbe_restore_vf_multicasts(adapter);
4543 #endif
4544 
4545 	return netdev_mc_count(netdev);
4546 }
4547 
4548 #ifdef CONFIG_PCI_IOV
4549 void ixgbe_full_sync_mac_table(struct ixgbe_adapter *adapter)
4550 {
4551 	struct ixgbe_mac_addr *mac_table = &adapter->mac_table[0];
4552 	struct ixgbe_hw *hw = &adapter->hw;
4553 	int i;
4554 
4555 	for (i = 0; i < hw->mac.num_rar_entries; i++, mac_table++) {
4556 		mac_table->state &= ~IXGBE_MAC_STATE_MODIFIED;
4557 
4558 		if (mac_table->state & IXGBE_MAC_STATE_IN_USE)
4559 			hw->mac.ops.set_rar(hw, i,
4560 					    mac_table->addr,
4561 					    mac_table->pool,
4562 					    IXGBE_RAH_AV);
4563 		else
4564 			hw->mac.ops.clear_rar(hw, i);
4565 	}
4566 }
4567 
4568 #endif
4569 static void ixgbe_sync_mac_table(struct ixgbe_adapter *adapter)
4570 {
4571 	struct ixgbe_mac_addr *mac_table = &adapter->mac_table[0];
4572 	struct ixgbe_hw *hw = &adapter->hw;
4573 	int i;
4574 
4575 	for (i = 0; i < hw->mac.num_rar_entries; i++, mac_table++) {
4576 		if (!(mac_table->state & IXGBE_MAC_STATE_MODIFIED))
4577 			continue;
4578 
4579 		mac_table->state &= ~IXGBE_MAC_STATE_MODIFIED;
4580 
4581 		if (mac_table->state & IXGBE_MAC_STATE_IN_USE)
4582 			hw->mac.ops.set_rar(hw, i,
4583 					    mac_table->addr,
4584 					    mac_table->pool,
4585 					    IXGBE_RAH_AV);
4586 		else
4587 			hw->mac.ops.clear_rar(hw, i);
4588 	}
4589 }
4590 
4591 static void ixgbe_flush_sw_mac_table(struct ixgbe_adapter *adapter)
4592 {
4593 	struct ixgbe_mac_addr *mac_table = &adapter->mac_table[0];
4594 	struct ixgbe_hw *hw = &adapter->hw;
4595 	int i;
4596 
4597 	for (i = 0; i < hw->mac.num_rar_entries; i++, mac_table++) {
4598 		mac_table->state |= IXGBE_MAC_STATE_MODIFIED;
4599 		mac_table->state &= ~IXGBE_MAC_STATE_IN_USE;
4600 	}
4601 
4602 	ixgbe_sync_mac_table(adapter);
4603 }
4604 
4605 static int ixgbe_available_rars(struct ixgbe_adapter *adapter, u16 pool)
4606 {
4607 	struct ixgbe_mac_addr *mac_table = &adapter->mac_table[0];
4608 	struct ixgbe_hw *hw = &adapter->hw;
4609 	int i, count = 0;
4610 
4611 	for (i = 0; i < hw->mac.num_rar_entries; i++, mac_table++) {
4612 		/* do not count default RAR as available */
4613 		if (mac_table->state & IXGBE_MAC_STATE_DEFAULT)
4614 			continue;
4615 
4616 		/* only count unused and addresses that belong to us */
4617 		if (mac_table->state & IXGBE_MAC_STATE_IN_USE) {
4618 			if (mac_table->pool != pool)
4619 				continue;
4620 		}
4621 
4622 		count++;
4623 	}
4624 
4625 	return count;
4626 }
4627 
4628 /* this function destroys the first RAR entry */
4629 static void ixgbe_mac_set_default_filter(struct ixgbe_adapter *adapter)
4630 {
4631 	struct ixgbe_mac_addr *mac_table = &adapter->mac_table[0];
4632 	struct ixgbe_hw *hw = &adapter->hw;
4633 
4634 	memcpy(&mac_table->addr, hw->mac.addr, ETH_ALEN);
4635 	mac_table->pool = VMDQ_P(0);
4636 
4637 	mac_table->state = IXGBE_MAC_STATE_DEFAULT | IXGBE_MAC_STATE_IN_USE;
4638 
4639 	hw->mac.ops.set_rar(hw, 0, mac_table->addr, mac_table->pool,
4640 			    IXGBE_RAH_AV);
4641 }
4642 
4643 int ixgbe_add_mac_filter(struct ixgbe_adapter *adapter,
4644 			 const u8 *addr, u16 pool)
4645 {
4646 	struct ixgbe_mac_addr *mac_table = &adapter->mac_table[0];
4647 	struct ixgbe_hw *hw = &adapter->hw;
4648 	int i;
4649 
4650 	if (is_zero_ether_addr(addr))
4651 		return -EINVAL;
4652 
4653 	for (i = 0; i < hw->mac.num_rar_entries; i++, mac_table++) {
4654 		if (mac_table->state & IXGBE_MAC_STATE_IN_USE)
4655 			continue;
4656 
4657 		ether_addr_copy(mac_table->addr, addr);
4658 		mac_table->pool = pool;
4659 
4660 		mac_table->state |= IXGBE_MAC_STATE_MODIFIED |
4661 				    IXGBE_MAC_STATE_IN_USE;
4662 
4663 		ixgbe_sync_mac_table(adapter);
4664 
4665 		return i;
4666 	}
4667 
4668 	return -ENOMEM;
4669 }
4670 
4671 int ixgbe_del_mac_filter(struct ixgbe_adapter *adapter,
4672 			 const u8 *addr, u16 pool)
4673 {
4674 	struct ixgbe_mac_addr *mac_table = &adapter->mac_table[0];
4675 	struct ixgbe_hw *hw = &adapter->hw;
4676 	int i;
4677 
4678 	if (is_zero_ether_addr(addr))
4679 		return -EINVAL;
4680 
4681 	/* search table for addr, if found clear IN_USE flag and sync */
4682 	for (i = 0; i < hw->mac.num_rar_entries; i++, mac_table++) {
4683 		/* we can only delete an entry if it is in use */
4684 		if (!(mac_table->state & IXGBE_MAC_STATE_IN_USE))
4685 			continue;
4686 		/* we only care about entries that belong to the given pool */
4687 		if (mac_table->pool != pool)
4688 			continue;
4689 		/* we only care about a specific MAC address */
4690 		if (!ether_addr_equal(addr, mac_table->addr))
4691 			continue;
4692 
4693 		mac_table->state |= IXGBE_MAC_STATE_MODIFIED;
4694 		mac_table->state &= ~IXGBE_MAC_STATE_IN_USE;
4695 
4696 		ixgbe_sync_mac_table(adapter);
4697 
4698 		return 0;
4699 	}
4700 
4701 	return -ENOMEM;
4702 }
4703 /**
4704  * ixgbe_write_uc_addr_list - write unicast addresses to RAR table
4705  * @netdev: network interface device structure
4706  *
4707  * Writes unicast address list to the RAR table.
4708  * Returns: -ENOMEM on failure/insufficient address space
4709  *                0 on no addresses written
4710  *                X on writing X addresses to the RAR table
4711  **/
4712 static int ixgbe_write_uc_addr_list(struct net_device *netdev, int vfn)
4713 {
4714 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
4715 	int count = 0;
4716 
4717 	/* return ENOMEM indicating insufficient memory for addresses */
4718 	if (netdev_uc_count(netdev) > ixgbe_available_rars(adapter, vfn))
4719 		return -ENOMEM;
4720 
4721 	if (!netdev_uc_empty(netdev)) {
4722 		struct netdev_hw_addr *ha;
4723 		netdev_for_each_uc_addr(ha, netdev) {
4724 			ixgbe_del_mac_filter(adapter, ha->addr, vfn);
4725 			ixgbe_add_mac_filter(adapter, ha->addr, vfn);
4726 			count++;
4727 		}
4728 	}
4729 	return count;
4730 }
4731 
4732 static int ixgbe_uc_sync(struct net_device *netdev, const unsigned char *addr)
4733 {
4734 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
4735 	int ret;
4736 
4737 	ret = ixgbe_add_mac_filter(adapter, addr, VMDQ_P(0));
4738 
4739 	return min_t(int, ret, 0);
4740 }
4741 
4742 static int ixgbe_uc_unsync(struct net_device *netdev, const unsigned char *addr)
4743 {
4744 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
4745 
4746 	ixgbe_del_mac_filter(adapter, addr, VMDQ_P(0));
4747 
4748 	return 0;
4749 }
4750 
4751 /**
4752  * ixgbe_set_rx_mode - Unicast, Multicast and Promiscuous mode set
4753  * @netdev: network interface device structure
4754  *
4755  * The set_rx_method entry point is called whenever the unicast/multicast
4756  * address list or the network interface flags are updated.  This routine is
4757  * responsible for configuring the hardware for proper unicast, multicast and
4758  * promiscuous mode.
4759  **/
4760 void ixgbe_set_rx_mode(struct net_device *netdev)
4761 {
4762 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
4763 	struct ixgbe_hw *hw = &adapter->hw;
4764 	u32 fctrl, vmolr = IXGBE_VMOLR_BAM | IXGBE_VMOLR_AUPE;
4765 	netdev_features_t features = netdev->features;
4766 	int count;
4767 
4768 	/* Check for Promiscuous and All Multicast modes */
4769 	fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
4770 
4771 	/* set all bits that we expect to always be set */
4772 	fctrl &= ~IXGBE_FCTRL_SBP; /* disable store-bad-packets */
4773 	fctrl |= IXGBE_FCTRL_BAM;
4774 	fctrl |= IXGBE_FCTRL_DPF; /* discard pause frames when FC enabled */
4775 	fctrl |= IXGBE_FCTRL_PMCF;
4776 
4777 	/* clear the bits we are changing the status of */
4778 	fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
4779 	if (netdev->flags & IFF_PROMISC) {
4780 		hw->addr_ctrl.user_set_promisc = true;
4781 		fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
4782 		vmolr |= IXGBE_VMOLR_MPE;
4783 		features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
4784 	} else {
4785 		if (netdev->flags & IFF_ALLMULTI) {
4786 			fctrl |= IXGBE_FCTRL_MPE;
4787 			vmolr |= IXGBE_VMOLR_MPE;
4788 		}
4789 		hw->addr_ctrl.user_set_promisc = false;
4790 	}
4791 
4792 	/*
4793 	 * Write addresses to available RAR registers, if there is not
4794 	 * sufficient space to store all the addresses then enable
4795 	 * unicast promiscuous mode
4796 	 */
4797 	if (__dev_uc_sync(netdev, ixgbe_uc_sync, ixgbe_uc_unsync)) {
4798 		fctrl |= IXGBE_FCTRL_UPE;
4799 		vmolr |= IXGBE_VMOLR_ROPE;
4800 	}
4801 
4802 	/* Write addresses to the MTA, if the attempt fails
4803 	 * then we should just turn on promiscuous mode so
4804 	 * that we can at least receive multicast traffic
4805 	 */
4806 	count = ixgbe_write_mc_addr_list(netdev);
4807 	if (count < 0) {
4808 		fctrl |= IXGBE_FCTRL_MPE;
4809 		vmolr |= IXGBE_VMOLR_MPE;
4810 	} else if (count) {
4811 		vmolr |= IXGBE_VMOLR_ROMPE;
4812 	}
4813 
4814 	if (hw->mac.type != ixgbe_mac_82598EB) {
4815 		vmolr |= IXGBE_READ_REG(hw, IXGBE_VMOLR(VMDQ_P(0))) &
4816 			 ~(IXGBE_VMOLR_MPE | IXGBE_VMOLR_ROMPE |
4817 			   IXGBE_VMOLR_ROPE);
4818 		IXGBE_WRITE_REG(hw, IXGBE_VMOLR(VMDQ_P(0)), vmolr);
4819 	}
4820 
4821 	/* This is useful for sniffing bad packets. */
4822 	if (features & NETIF_F_RXALL) {
4823 		/* UPE and MPE will be handled by normal PROMISC logic
4824 		 * in e1000e_set_rx_mode */
4825 		fctrl |= (IXGBE_FCTRL_SBP | /* Receive bad packets */
4826 			  IXGBE_FCTRL_BAM | /* RX All Bcast Pkts */
4827 			  IXGBE_FCTRL_PMCF); /* RX All MAC Ctrl Pkts */
4828 
4829 		fctrl &= ~(IXGBE_FCTRL_DPF);
4830 		/* NOTE:  VLAN filtering is disabled by setting PROMISC */
4831 	}
4832 
4833 	IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
4834 
4835 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
4836 		ixgbe_vlan_strip_enable(adapter);
4837 	else
4838 		ixgbe_vlan_strip_disable(adapter);
4839 
4840 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
4841 		ixgbe_vlan_promisc_disable(adapter);
4842 	else
4843 		ixgbe_vlan_promisc_enable(adapter);
4844 }
4845 
4846 static void ixgbe_napi_enable_all(struct ixgbe_adapter *adapter)
4847 {
4848 	int q_idx;
4849 
4850 	for (q_idx = 0; q_idx < adapter->num_q_vectors; q_idx++)
4851 		napi_enable(&adapter->q_vector[q_idx]->napi);
4852 }
4853 
4854 static void ixgbe_napi_disable_all(struct ixgbe_adapter *adapter)
4855 {
4856 	int q_idx;
4857 
4858 	for (q_idx = 0; q_idx < adapter->num_q_vectors; q_idx++)
4859 		napi_disable(&adapter->q_vector[q_idx]->napi);
4860 }
4861 
4862 static void ixgbe_clear_udp_tunnel_port(struct ixgbe_adapter *adapter, u32 mask)
4863 {
4864 	struct ixgbe_hw *hw = &adapter->hw;
4865 	u32 vxlanctrl;
4866 
4867 	if (!(adapter->flags & (IXGBE_FLAG_VXLAN_OFFLOAD_CAPABLE |
4868 				IXGBE_FLAG_GENEVE_OFFLOAD_CAPABLE)))
4869 		return;
4870 
4871 	vxlanctrl = IXGBE_READ_REG(hw, IXGBE_VXLANCTRL) && ~mask;
4872 	IXGBE_WRITE_REG(hw, IXGBE_VXLANCTRL, vxlanctrl);
4873 
4874 	if (mask & IXGBE_VXLANCTRL_VXLAN_UDPPORT_MASK)
4875 		adapter->vxlan_port = 0;
4876 
4877 	if (mask & IXGBE_VXLANCTRL_GENEVE_UDPPORT_MASK)
4878 		adapter->geneve_port = 0;
4879 }
4880 
4881 #ifdef CONFIG_IXGBE_DCB
4882 /**
4883  * ixgbe_configure_dcb - Configure DCB hardware
4884  * @adapter: ixgbe adapter struct
4885  *
4886  * This is called by the driver on open to configure the DCB hardware.
4887  * This is also called by the gennetlink interface when reconfiguring
4888  * the DCB state.
4889  */
4890 static void ixgbe_configure_dcb(struct ixgbe_adapter *adapter)
4891 {
4892 	struct ixgbe_hw *hw = &adapter->hw;
4893 	int max_frame = adapter->netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
4894 
4895 	if (!(adapter->flags & IXGBE_FLAG_DCB_ENABLED)) {
4896 		if (hw->mac.type == ixgbe_mac_82598EB)
4897 			netif_set_gso_max_size(adapter->netdev, 65536);
4898 		return;
4899 	}
4900 
4901 	if (hw->mac.type == ixgbe_mac_82598EB)
4902 		netif_set_gso_max_size(adapter->netdev, 32768);
4903 
4904 #ifdef IXGBE_FCOE
4905 	if (adapter->netdev->features & NETIF_F_FCOE_MTU)
4906 		max_frame = max(max_frame, IXGBE_FCOE_JUMBO_FRAME_SIZE);
4907 #endif
4908 
4909 	/* reconfigure the hardware */
4910 	if (adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE) {
4911 		ixgbe_dcb_calculate_tc_credits(hw, &adapter->dcb_cfg, max_frame,
4912 						DCB_TX_CONFIG);
4913 		ixgbe_dcb_calculate_tc_credits(hw, &adapter->dcb_cfg, max_frame,
4914 						DCB_RX_CONFIG);
4915 		ixgbe_dcb_hw_config(hw, &adapter->dcb_cfg);
4916 	} else if (adapter->ixgbe_ieee_ets && adapter->ixgbe_ieee_pfc) {
4917 		ixgbe_dcb_hw_ets(&adapter->hw,
4918 				 adapter->ixgbe_ieee_ets,
4919 				 max_frame);
4920 		ixgbe_dcb_hw_pfc_config(&adapter->hw,
4921 					adapter->ixgbe_ieee_pfc->pfc_en,
4922 					adapter->ixgbe_ieee_ets->prio_tc);
4923 	}
4924 
4925 	/* Enable RSS Hash per TC */
4926 	if (hw->mac.type != ixgbe_mac_82598EB) {
4927 		u32 msb = 0;
4928 		u16 rss_i = adapter->ring_feature[RING_F_RSS].indices - 1;
4929 
4930 		while (rss_i) {
4931 			msb++;
4932 			rss_i >>= 1;
4933 		}
4934 
4935 		/* write msb to all 8 TCs in one write */
4936 		IXGBE_WRITE_REG(hw, IXGBE_RQTC, msb * 0x11111111);
4937 	}
4938 }
4939 #endif
4940 
4941 /* Additional bittime to account for IXGBE framing */
4942 #define IXGBE_ETH_FRAMING 20
4943 
4944 /**
4945  * ixgbe_hpbthresh - calculate high water mark for flow control
4946  *
4947  * @adapter: board private structure to calculate for
4948  * @pb: packet buffer to calculate
4949  */
4950 static int ixgbe_hpbthresh(struct ixgbe_adapter *adapter, int pb)
4951 {
4952 	struct ixgbe_hw *hw = &adapter->hw;
4953 	struct net_device *dev = adapter->netdev;
4954 	int link, tc, kb, marker;
4955 	u32 dv_id, rx_pba;
4956 
4957 	/* Calculate max LAN frame size */
4958 	tc = link = dev->mtu + ETH_HLEN + ETH_FCS_LEN + IXGBE_ETH_FRAMING;
4959 
4960 #ifdef IXGBE_FCOE
4961 	/* FCoE traffic class uses FCOE jumbo frames */
4962 	if ((dev->features & NETIF_F_FCOE_MTU) &&
4963 	    (tc < IXGBE_FCOE_JUMBO_FRAME_SIZE) &&
4964 	    (pb == ixgbe_fcoe_get_tc(adapter)))
4965 		tc = IXGBE_FCOE_JUMBO_FRAME_SIZE;
4966 #endif
4967 
4968 	/* Calculate delay value for device */
4969 	switch (hw->mac.type) {
4970 	case ixgbe_mac_X540:
4971 	case ixgbe_mac_X550:
4972 	case ixgbe_mac_X550EM_x:
4973 	case ixgbe_mac_x550em_a:
4974 		dv_id = IXGBE_DV_X540(link, tc);
4975 		break;
4976 	default:
4977 		dv_id = IXGBE_DV(link, tc);
4978 		break;
4979 	}
4980 
4981 	/* Loopback switch introduces additional latency */
4982 	if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
4983 		dv_id += IXGBE_B2BT(tc);
4984 
4985 	/* Delay value is calculated in bit times convert to KB */
4986 	kb = IXGBE_BT2KB(dv_id);
4987 	rx_pba = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(pb)) >> 10;
4988 
4989 	marker = rx_pba - kb;
4990 
4991 	/* It is possible that the packet buffer is not large enough
4992 	 * to provide required headroom. In this case throw an error
4993 	 * to user and a do the best we can.
4994 	 */
4995 	if (marker < 0) {
4996 		e_warn(drv, "Packet Buffer(%i) can not provide enough"
4997 			    "headroom to support flow control."
4998 			    "Decrease MTU or number of traffic classes\n", pb);
4999 		marker = tc + 1;
5000 	}
5001 
5002 	return marker;
5003 }
5004 
5005 /**
5006  * ixgbe_lpbthresh - calculate low water mark for for flow control
5007  *
5008  * @adapter: board private structure to calculate for
5009  * @pb: packet buffer to calculate
5010  */
5011 static int ixgbe_lpbthresh(struct ixgbe_adapter *adapter, int pb)
5012 {
5013 	struct ixgbe_hw *hw = &adapter->hw;
5014 	struct net_device *dev = adapter->netdev;
5015 	int tc;
5016 	u32 dv_id;
5017 
5018 	/* Calculate max LAN frame size */
5019 	tc = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
5020 
5021 #ifdef IXGBE_FCOE
5022 	/* FCoE traffic class uses FCOE jumbo frames */
5023 	if ((dev->features & NETIF_F_FCOE_MTU) &&
5024 	    (tc < IXGBE_FCOE_JUMBO_FRAME_SIZE) &&
5025 	    (pb == netdev_get_prio_tc_map(dev, adapter->fcoe.up)))
5026 		tc = IXGBE_FCOE_JUMBO_FRAME_SIZE;
5027 #endif
5028 
5029 	/* Calculate delay value for device */
5030 	switch (hw->mac.type) {
5031 	case ixgbe_mac_X540:
5032 	case ixgbe_mac_X550:
5033 	case ixgbe_mac_X550EM_x:
5034 	case ixgbe_mac_x550em_a:
5035 		dv_id = IXGBE_LOW_DV_X540(tc);
5036 		break;
5037 	default:
5038 		dv_id = IXGBE_LOW_DV(tc);
5039 		break;
5040 	}
5041 
5042 	/* Delay value is calculated in bit times convert to KB */
5043 	return IXGBE_BT2KB(dv_id);
5044 }
5045 
5046 /*
5047  * ixgbe_pbthresh_setup - calculate and setup high low water marks
5048  */
5049 static void ixgbe_pbthresh_setup(struct ixgbe_adapter *adapter)
5050 {
5051 	struct ixgbe_hw *hw = &adapter->hw;
5052 	int num_tc = netdev_get_num_tc(adapter->netdev);
5053 	int i;
5054 
5055 	if (!num_tc)
5056 		num_tc = 1;
5057 
5058 	for (i = 0; i < num_tc; i++) {
5059 		hw->fc.high_water[i] = ixgbe_hpbthresh(adapter, i);
5060 		hw->fc.low_water[i] = ixgbe_lpbthresh(adapter, i);
5061 
5062 		/* Low water marks must not be larger than high water marks */
5063 		if (hw->fc.low_water[i] > hw->fc.high_water[i])
5064 			hw->fc.low_water[i] = 0;
5065 	}
5066 
5067 	for (; i < MAX_TRAFFIC_CLASS; i++)
5068 		hw->fc.high_water[i] = 0;
5069 }
5070 
5071 static void ixgbe_configure_pb(struct ixgbe_adapter *adapter)
5072 {
5073 	struct ixgbe_hw *hw = &adapter->hw;
5074 	int hdrm;
5075 	u8 tc = netdev_get_num_tc(adapter->netdev);
5076 
5077 	if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE ||
5078 	    adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)
5079 		hdrm = 32 << adapter->fdir_pballoc;
5080 	else
5081 		hdrm = 0;
5082 
5083 	hw->mac.ops.set_rxpba(hw, tc, hdrm, PBA_STRATEGY_EQUAL);
5084 	ixgbe_pbthresh_setup(adapter);
5085 }
5086 
5087 static void ixgbe_fdir_filter_restore(struct ixgbe_adapter *adapter)
5088 {
5089 	struct ixgbe_hw *hw = &adapter->hw;
5090 	struct hlist_node *node2;
5091 	struct ixgbe_fdir_filter *filter;
5092 
5093 	spin_lock(&adapter->fdir_perfect_lock);
5094 
5095 	if (!hlist_empty(&adapter->fdir_filter_list))
5096 		ixgbe_fdir_set_input_mask_82599(hw, &adapter->fdir_mask);
5097 
5098 	hlist_for_each_entry_safe(filter, node2,
5099 				  &adapter->fdir_filter_list, fdir_node) {
5100 		ixgbe_fdir_write_perfect_filter_82599(hw,
5101 				&filter->filter,
5102 				filter->sw_idx,
5103 				(filter->action == IXGBE_FDIR_DROP_QUEUE) ?
5104 				IXGBE_FDIR_DROP_QUEUE :
5105 				adapter->rx_ring[filter->action]->reg_idx);
5106 	}
5107 
5108 	spin_unlock(&adapter->fdir_perfect_lock);
5109 }
5110 
5111 static void ixgbe_macvlan_set_rx_mode(struct net_device *dev, unsigned int pool,
5112 				      struct ixgbe_adapter *adapter)
5113 {
5114 	struct ixgbe_hw *hw = &adapter->hw;
5115 	u32 vmolr;
5116 
5117 	/* No unicast promiscuous support for VMDQ devices. */
5118 	vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(pool));
5119 	vmolr |= (IXGBE_VMOLR_ROMPE | IXGBE_VMOLR_BAM | IXGBE_VMOLR_AUPE);
5120 
5121 	/* clear the affected bit */
5122 	vmolr &= ~IXGBE_VMOLR_MPE;
5123 
5124 	if (dev->flags & IFF_ALLMULTI) {
5125 		vmolr |= IXGBE_VMOLR_MPE;
5126 	} else {
5127 		vmolr |= IXGBE_VMOLR_ROMPE;
5128 		hw->mac.ops.update_mc_addr_list(hw, dev);
5129 	}
5130 	ixgbe_write_uc_addr_list(adapter->netdev, pool);
5131 	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(pool), vmolr);
5132 }
5133 
5134 static void ixgbe_fwd_psrtype(struct ixgbe_fwd_adapter *vadapter)
5135 {
5136 	struct ixgbe_adapter *adapter = vadapter->real_adapter;
5137 	int rss_i = adapter->num_rx_queues_per_pool;
5138 	struct ixgbe_hw *hw = &adapter->hw;
5139 	u16 pool = vadapter->pool;
5140 	u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
5141 		      IXGBE_PSRTYPE_UDPHDR |
5142 		      IXGBE_PSRTYPE_IPV4HDR |
5143 		      IXGBE_PSRTYPE_L2HDR |
5144 		      IXGBE_PSRTYPE_IPV6HDR;
5145 
5146 	if (hw->mac.type == ixgbe_mac_82598EB)
5147 		return;
5148 
5149 	if (rss_i > 3)
5150 		psrtype |= 2u << 29;
5151 	else if (rss_i > 1)
5152 		psrtype |= 1u << 29;
5153 
5154 	IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(VMDQ_P(pool)), psrtype);
5155 }
5156 
5157 /**
5158  * ixgbe_clean_rx_ring - Free Rx Buffers per Queue
5159  * @rx_ring: ring to free buffers from
5160  **/
5161 static void ixgbe_clean_rx_ring(struct ixgbe_ring *rx_ring)
5162 {
5163 	u16 i = rx_ring->next_to_clean;
5164 	struct ixgbe_rx_buffer *rx_buffer = &rx_ring->rx_buffer_info[i];
5165 
5166 	/* Free all the Rx ring sk_buffs */
5167 	while (i != rx_ring->next_to_alloc) {
5168 		if (rx_buffer->skb) {
5169 			struct sk_buff *skb = rx_buffer->skb;
5170 			if (IXGBE_CB(skb)->page_released)
5171 				dma_unmap_page_attrs(rx_ring->dev,
5172 						     IXGBE_CB(skb)->dma,
5173 						     ixgbe_rx_pg_size(rx_ring),
5174 						     DMA_FROM_DEVICE,
5175 						     IXGBE_RX_DMA_ATTR);
5176 			dev_kfree_skb(skb);
5177 		}
5178 
5179 		/* Invalidate cache lines that may have been written to by
5180 		 * device so that we avoid corrupting memory.
5181 		 */
5182 		dma_sync_single_range_for_cpu(rx_ring->dev,
5183 					      rx_buffer->dma,
5184 					      rx_buffer->page_offset,
5185 					      ixgbe_rx_bufsz(rx_ring),
5186 					      DMA_FROM_DEVICE);
5187 
5188 		/* free resources associated with mapping */
5189 		dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
5190 				     ixgbe_rx_pg_size(rx_ring),
5191 				     DMA_FROM_DEVICE,
5192 				     IXGBE_RX_DMA_ATTR);
5193 		__page_frag_cache_drain(rx_buffer->page,
5194 					rx_buffer->pagecnt_bias);
5195 
5196 		i++;
5197 		rx_buffer++;
5198 		if (i == rx_ring->count) {
5199 			i = 0;
5200 			rx_buffer = rx_ring->rx_buffer_info;
5201 		}
5202 	}
5203 
5204 	rx_ring->next_to_alloc = 0;
5205 	rx_ring->next_to_clean = 0;
5206 	rx_ring->next_to_use = 0;
5207 }
5208 
5209 static void ixgbe_disable_fwd_ring(struct ixgbe_fwd_adapter *vadapter,
5210 				   struct ixgbe_ring *rx_ring)
5211 {
5212 	struct ixgbe_adapter *adapter = vadapter->real_adapter;
5213 	int index = rx_ring->queue_index + vadapter->rx_base_queue;
5214 
5215 	/* shutdown specific queue receive and wait for dma to settle */
5216 	ixgbe_disable_rx_queue(adapter, rx_ring);
5217 	usleep_range(10000, 20000);
5218 	ixgbe_irq_disable_queues(adapter, BIT_ULL(index));
5219 	ixgbe_clean_rx_ring(rx_ring);
5220 	rx_ring->l2_accel_priv = NULL;
5221 }
5222 
5223 static int ixgbe_fwd_ring_down(struct net_device *vdev,
5224 			       struct ixgbe_fwd_adapter *accel)
5225 {
5226 	struct ixgbe_adapter *adapter = accel->real_adapter;
5227 	unsigned int rxbase = accel->rx_base_queue;
5228 	unsigned int txbase = accel->tx_base_queue;
5229 	int i;
5230 
5231 	netif_tx_stop_all_queues(vdev);
5232 
5233 	for (i = 0; i < adapter->num_rx_queues_per_pool; i++) {
5234 		ixgbe_disable_fwd_ring(accel, adapter->rx_ring[rxbase + i]);
5235 		adapter->rx_ring[rxbase + i]->netdev = adapter->netdev;
5236 	}
5237 
5238 	for (i = 0; i < adapter->num_rx_queues_per_pool; i++) {
5239 		adapter->tx_ring[txbase + i]->l2_accel_priv = NULL;
5240 		adapter->tx_ring[txbase + i]->netdev = adapter->netdev;
5241 	}
5242 
5243 
5244 	return 0;
5245 }
5246 
5247 static int ixgbe_fwd_ring_up(struct net_device *vdev,
5248 			     struct ixgbe_fwd_adapter *accel)
5249 {
5250 	struct ixgbe_adapter *adapter = accel->real_adapter;
5251 	unsigned int rxbase, txbase, queues;
5252 	int i, baseq, err = 0;
5253 
5254 	if (!test_bit(accel->pool, &adapter->fwd_bitmask))
5255 		return 0;
5256 
5257 	baseq = accel->pool * adapter->num_rx_queues_per_pool;
5258 	netdev_dbg(vdev, "pool %i:%i queues %i:%i VSI bitmask %lx\n",
5259 		   accel->pool, adapter->num_rx_pools,
5260 		   baseq, baseq + adapter->num_rx_queues_per_pool,
5261 		   adapter->fwd_bitmask);
5262 
5263 	accel->netdev = vdev;
5264 	accel->rx_base_queue = rxbase = baseq;
5265 	accel->tx_base_queue = txbase = baseq;
5266 
5267 	for (i = 0; i < adapter->num_rx_queues_per_pool; i++)
5268 		ixgbe_disable_fwd_ring(accel, adapter->rx_ring[rxbase + i]);
5269 
5270 	for (i = 0; i < adapter->num_rx_queues_per_pool; i++) {
5271 		adapter->rx_ring[rxbase + i]->netdev = vdev;
5272 		adapter->rx_ring[rxbase + i]->l2_accel_priv = accel;
5273 		ixgbe_configure_rx_ring(adapter, adapter->rx_ring[rxbase + i]);
5274 	}
5275 
5276 	for (i = 0; i < adapter->num_rx_queues_per_pool; i++) {
5277 		adapter->tx_ring[txbase + i]->netdev = vdev;
5278 		adapter->tx_ring[txbase + i]->l2_accel_priv = accel;
5279 	}
5280 
5281 	queues = min_t(unsigned int,
5282 		       adapter->num_rx_queues_per_pool, vdev->num_tx_queues);
5283 	err = netif_set_real_num_tx_queues(vdev, queues);
5284 	if (err)
5285 		goto fwd_queue_err;
5286 
5287 	err = netif_set_real_num_rx_queues(vdev, queues);
5288 	if (err)
5289 		goto fwd_queue_err;
5290 
5291 	if (is_valid_ether_addr(vdev->dev_addr))
5292 		ixgbe_add_mac_filter(adapter, vdev->dev_addr, accel->pool);
5293 
5294 	ixgbe_fwd_psrtype(accel);
5295 	ixgbe_macvlan_set_rx_mode(vdev, accel->pool, adapter);
5296 	return err;
5297 fwd_queue_err:
5298 	ixgbe_fwd_ring_down(vdev, accel);
5299 	return err;
5300 }
5301 
5302 static int ixgbe_upper_dev_walk(struct net_device *upper, void *data)
5303 {
5304 	if (netif_is_macvlan(upper)) {
5305 		struct macvlan_dev *dfwd = netdev_priv(upper);
5306 		struct ixgbe_fwd_adapter *vadapter = dfwd->fwd_priv;
5307 
5308 		if (dfwd->fwd_priv)
5309 			ixgbe_fwd_ring_up(upper, vadapter);
5310 	}
5311 
5312 	return 0;
5313 }
5314 
5315 static void ixgbe_configure_dfwd(struct ixgbe_adapter *adapter)
5316 {
5317 	netdev_walk_all_upper_dev_rcu(adapter->netdev,
5318 				      ixgbe_upper_dev_walk, NULL);
5319 }
5320 
5321 static void ixgbe_configure(struct ixgbe_adapter *adapter)
5322 {
5323 	struct ixgbe_hw *hw = &adapter->hw;
5324 
5325 	ixgbe_configure_pb(adapter);
5326 #ifdef CONFIG_IXGBE_DCB
5327 	ixgbe_configure_dcb(adapter);
5328 #endif
5329 	/*
5330 	 * We must restore virtualization before VLANs or else
5331 	 * the VLVF registers will not be populated
5332 	 */
5333 	ixgbe_configure_virtualization(adapter);
5334 
5335 	ixgbe_set_rx_mode(adapter->netdev);
5336 	ixgbe_restore_vlan(adapter);
5337 
5338 	switch (hw->mac.type) {
5339 	case ixgbe_mac_82599EB:
5340 	case ixgbe_mac_X540:
5341 		hw->mac.ops.disable_rx_buff(hw);
5342 		break;
5343 	default:
5344 		break;
5345 	}
5346 
5347 	if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
5348 		ixgbe_init_fdir_signature_82599(&adapter->hw,
5349 						adapter->fdir_pballoc);
5350 	} else if (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE) {
5351 		ixgbe_init_fdir_perfect_82599(&adapter->hw,
5352 					      adapter->fdir_pballoc);
5353 		ixgbe_fdir_filter_restore(adapter);
5354 	}
5355 
5356 	switch (hw->mac.type) {
5357 	case ixgbe_mac_82599EB:
5358 	case ixgbe_mac_X540:
5359 		hw->mac.ops.enable_rx_buff(hw);
5360 		break;
5361 	default:
5362 		break;
5363 	}
5364 
5365 #ifdef CONFIG_IXGBE_DCA
5366 	/* configure DCA */
5367 	if (adapter->flags & IXGBE_FLAG_DCA_CAPABLE)
5368 		ixgbe_setup_dca(adapter);
5369 #endif /* CONFIG_IXGBE_DCA */
5370 
5371 #ifdef IXGBE_FCOE
5372 	/* configure FCoE L2 filters, redirection table, and Rx control */
5373 	ixgbe_configure_fcoe(adapter);
5374 
5375 #endif /* IXGBE_FCOE */
5376 	ixgbe_configure_tx(adapter);
5377 	ixgbe_configure_rx(adapter);
5378 	ixgbe_configure_dfwd(adapter);
5379 }
5380 
5381 /**
5382  * ixgbe_sfp_link_config - set up SFP+ link
5383  * @adapter: pointer to private adapter struct
5384  **/
5385 static void ixgbe_sfp_link_config(struct ixgbe_adapter *adapter)
5386 {
5387 	/*
5388 	 * We are assuming the worst case scenario here, and that
5389 	 * is that an SFP was inserted/removed after the reset
5390 	 * but before SFP detection was enabled.  As such the best
5391 	 * solution is to just start searching as soon as we start
5392 	 */
5393 	if (adapter->hw.mac.type == ixgbe_mac_82598EB)
5394 		adapter->flags2 |= IXGBE_FLAG2_SEARCH_FOR_SFP;
5395 
5396 	adapter->flags2 |= IXGBE_FLAG2_SFP_NEEDS_RESET;
5397 	adapter->sfp_poll_time = 0;
5398 }
5399 
5400 /**
5401  * ixgbe_non_sfp_link_config - set up non-SFP+ link
5402  * @hw: pointer to private hardware struct
5403  *
5404  * Returns 0 on success, negative on failure
5405  **/
5406 static int ixgbe_non_sfp_link_config(struct ixgbe_hw *hw)
5407 {
5408 	u32 speed;
5409 	bool autoneg, link_up = false;
5410 	int ret = IXGBE_ERR_LINK_SETUP;
5411 
5412 	if (hw->mac.ops.check_link)
5413 		ret = hw->mac.ops.check_link(hw, &speed, &link_up, false);
5414 
5415 	if (ret)
5416 		return ret;
5417 
5418 	speed = hw->phy.autoneg_advertised;
5419 	if ((!speed) && (hw->mac.ops.get_link_capabilities))
5420 		ret = hw->mac.ops.get_link_capabilities(hw, &speed,
5421 							&autoneg);
5422 	if (ret)
5423 		return ret;
5424 
5425 	if (hw->mac.ops.setup_link)
5426 		ret = hw->mac.ops.setup_link(hw, speed, link_up);
5427 
5428 	return ret;
5429 }
5430 
5431 static void ixgbe_setup_gpie(struct ixgbe_adapter *adapter)
5432 {
5433 	struct ixgbe_hw *hw = &adapter->hw;
5434 	u32 gpie = 0;
5435 
5436 	if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
5437 		gpie = IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_PBA_SUPPORT |
5438 		       IXGBE_GPIE_OCD;
5439 		gpie |= IXGBE_GPIE_EIAME;
5440 		/*
5441 		 * use EIAM to auto-mask when MSI-X interrupt is asserted
5442 		 * this saves a register write for every interrupt
5443 		 */
5444 		switch (hw->mac.type) {
5445 		case ixgbe_mac_82598EB:
5446 			IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
5447 			break;
5448 		case ixgbe_mac_82599EB:
5449 		case ixgbe_mac_X540:
5450 		case ixgbe_mac_X550:
5451 		case ixgbe_mac_X550EM_x:
5452 		case ixgbe_mac_x550em_a:
5453 		default:
5454 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
5455 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
5456 			break;
5457 		}
5458 	} else {
5459 		/* legacy interrupts, use EIAM to auto-mask when reading EICR,
5460 		 * specifically only auto mask tx and rx interrupts */
5461 		IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
5462 	}
5463 
5464 	/* XXX: to interrupt immediately for EICS writes, enable this */
5465 	/* gpie |= IXGBE_GPIE_EIMEN; */
5466 
5467 	if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
5468 		gpie &= ~IXGBE_GPIE_VTMODE_MASK;
5469 
5470 		switch (adapter->ring_feature[RING_F_VMDQ].mask) {
5471 		case IXGBE_82599_VMDQ_8Q_MASK:
5472 			gpie |= IXGBE_GPIE_VTMODE_16;
5473 			break;
5474 		case IXGBE_82599_VMDQ_4Q_MASK:
5475 			gpie |= IXGBE_GPIE_VTMODE_32;
5476 			break;
5477 		default:
5478 			gpie |= IXGBE_GPIE_VTMODE_64;
5479 			break;
5480 		}
5481 	}
5482 
5483 	/* Enable Thermal over heat sensor interrupt */
5484 	if (adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE) {
5485 		switch (adapter->hw.mac.type) {
5486 		case ixgbe_mac_82599EB:
5487 			gpie |= IXGBE_SDP0_GPIEN_8259X;
5488 			break;
5489 		default:
5490 			break;
5491 		}
5492 	}
5493 
5494 	/* Enable fan failure interrupt */
5495 	if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE)
5496 		gpie |= IXGBE_SDP1_GPIEN(hw);
5497 
5498 	switch (hw->mac.type) {
5499 	case ixgbe_mac_82599EB:
5500 		gpie |= IXGBE_SDP1_GPIEN_8259X | IXGBE_SDP2_GPIEN_8259X;
5501 		break;
5502 	case ixgbe_mac_X550EM_x:
5503 	case ixgbe_mac_x550em_a:
5504 		gpie |= IXGBE_SDP0_GPIEN_X540;
5505 		break;
5506 	default:
5507 		break;
5508 	}
5509 
5510 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
5511 }
5512 
5513 static void ixgbe_up_complete(struct ixgbe_adapter *adapter)
5514 {
5515 	struct ixgbe_hw *hw = &adapter->hw;
5516 	int err;
5517 	u32 ctrl_ext;
5518 
5519 	ixgbe_get_hw_control(adapter);
5520 	ixgbe_setup_gpie(adapter);
5521 
5522 	if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
5523 		ixgbe_configure_msix(adapter);
5524 	else
5525 		ixgbe_configure_msi_and_legacy(adapter);
5526 
5527 	/* enable the optics for 82599 SFP+ fiber */
5528 	if (hw->mac.ops.enable_tx_laser)
5529 		hw->mac.ops.enable_tx_laser(hw);
5530 
5531 	if (hw->phy.ops.set_phy_power)
5532 		hw->phy.ops.set_phy_power(hw, true);
5533 
5534 	smp_mb__before_atomic();
5535 	clear_bit(__IXGBE_DOWN, &adapter->state);
5536 	ixgbe_napi_enable_all(adapter);
5537 
5538 	if (ixgbe_is_sfp(hw)) {
5539 		ixgbe_sfp_link_config(adapter);
5540 	} else {
5541 		err = ixgbe_non_sfp_link_config(hw);
5542 		if (err)
5543 			e_err(probe, "link_config FAILED %d\n", err);
5544 	}
5545 
5546 	/* clear any pending interrupts, may auto mask */
5547 	IXGBE_READ_REG(hw, IXGBE_EICR);
5548 	ixgbe_irq_enable(adapter, true, true);
5549 
5550 	/*
5551 	 * If this adapter has a fan, check to see if we had a failure
5552 	 * before we enabled the interrupt.
5553 	 */
5554 	if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) {
5555 		u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
5556 		if (esdp & IXGBE_ESDP_SDP1)
5557 			e_crit(drv, "Fan has stopped, replace the adapter\n");
5558 	}
5559 
5560 	/* bring the link up in the watchdog, this could race with our first
5561 	 * link up interrupt but shouldn't be a problem */
5562 	adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
5563 	adapter->link_check_timeout = jiffies;
5564 	mod_timer(&adapter->service_timer, jiffies);
5565 
5566 	/* Set PF Reset Done bit so PF/VF Mail Ops can work */
5567 	ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
5568 	ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
5569 	IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
5570 }
5571 
5572 void ixgbe_reinit_locked(struct ixgbe_adapter *adapter)
5573 {
5574 	WARN_ON(in_interrupt());
5575 	/* put off any impending NetWatchDogTimeout */
5576 	netif_trans_update(adapter->netdev);
5577 
5578 	while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
5579 		usleep_range(1000, 2000);
5580 	if (adapter->hw.phy.type == ixgbe_phy_fw)
5581 		ixgbe_watchdog_link_is_down(adapter);
5582 	ixgbe_down(adapter);
5583 	/*
5584 	 * If SR-IOV enabled then wait a bit before bringing the adapter
5585 	 * back up to give the VFs time to respond to the reset.  The
5586 	 * two second wait is based upon the watchdog timer cycle in
5587 	 * the VF driver.
5588 	 */
5589 	if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
5590 		msleep(2000);
5591 	ixgbe_up(adapter);
5592 	clear_bit(__IXGBE_RESETTING, &adapter->state);
5593 }
5594 
5595 void ixgbe_up(struct ixgbe_adapter *adapter)
5596 {
5597 	/* hardware has been reset, we need to reload some things */
5598 	ixgbe_configure(adapter);
5599 
5600 	ixgbe_up_complete(adapter);
5601 }
5602 
5603 void ixgbe_reset(struct ixgbe_adapter *adapter)
5604 {
5605 	struct ixgbe_hw *hw = &adapter->hw;
5606 	struct net_device *netdev = adapter->netdev;
5607 	int err;
5608 
5609 	if (ixgbe_removed(hw->hw_addr))
5610 		return;
5611 	/* lock SFP init bit to prevent race conditions with the watchdog */
5612 	while (test_and_set_bit(__IXGBE_IN_SFP_INIT, &adapter->state))
5613 		usleep_range(1000, 2000);
5614 
5615 	/* clear all SFP and link config related flags while holding SFP_INIT */
5616 	adapter->flags2 &= ~(IXGBE_FLAG2_SEARCH_FOR_SFP |
5617 			     IXGBE_FLAG2_SFP_NEEDS_RESET);
5618 	adapter->flags &= ~IXGBE_FLAG_NEED_LINK_CONFIG;
5619 
5620 	err = hw->mac.ops.init_hw(hw);
5621 	switch (err) {
5622 	case 0:
5623 	case IXGBE_ERR_SFP_NOT_PRESENT:
5624 	case IXGBE_ERR_SFP_NOT_SUPPORTED:
5625 		break;
5626 	case IXGBE_ERR_MASTER_REQUESTS_PENDING:
5627 		e_dev_err("master disable timed out\n");
5628 		break;
5629 	case IXGBE_ERR_EEPROM_VERSION:
5630 		/* We are running on a pre-production device, log a warning */
5631 		e_dev_warn("This device is a pre-production adapter/LOM. "
5632 			   "Please be aware there may be issues associated with "
5633 			   "your hardware.  If you are experiencing problems "
5634 			   "please contact your Intel or hardware "
5635 			   "representative who provided you with this "
5636 			   "hardware.\n");
5637 		break;
5638 	default:
5639 		e_dev_err("Hardware Error: %d\n", err);
5640 	}
5641 
5642 	clear_bit(__IXGBE_IN_SFP_INIT, &adapter->state);
5643 
5644 	/* flush entries out of MAC table */
5645 	ixgbe_flush_sw_mac_table(adapter);
5646 	__dev_uc_unsync(netdev, NULL);
5647 
5648 	/* do not flush user set addresses */
5649 	ixgbe_mac_set_default_filter(adapter);
5650 
5651 	/* update SAN MAC vmdq pool selection */
5652 	if (hw->mac.san_mac_rar_index)
5653 		hw->mac.ops.set_vmdq_san_mac(hw, VMDQ_P(0));
5654 
5655 	if (test_bit(__IXGBE_PTP_RUNNING, &adapter->state))
5656 		ixgbe_ptp_reset(adapter);
5657 
5658 	if (hw->phy.ops.set_phy_power) {
5659 		if (!netif_running(adapter->netdev) && !adapter->wol)
5660 			hw->phy.ops.set_phy_power(hw, false);
5661 		else
5662 			hw->phy.ops.set_phy_power(hw, true);
5663 	}
5664 }
5665 
5666 /**
5667  * ixgbe_clean_tx_ring - Free Tx Buffers
5668  * @tx_ring: ring to be cleaned
5669  **/
5670 static void ixgbe_clean_tx_ring(struct ixgbe_ring *tx_ring)
5671 {
5672 	u16 i = tx_ring->next_to_clean;
5673 	struct ixgbe_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
5674 
5675 	while (i != tx_ring->next_to_use) {
5676 		union ixgbe_adv_tx_desc *eop_desc, *tx_desc;
5677 
5678 		/* Free all the Tx ring sk_buffs */
5679 		if (ring_is_xdp(tx_ring))
5680 			page_frag_free(tx_buffer->data);
5681 		else
5682 			dev_kfree_skb_any(tx_buffer->skb);
5683 
5684 		/* unmap skb header data */
5685 		dma_unmap_single(tx_ring->dev,
5686 				 dma_unmap_addr(tx_buffer, dma),
5687 				 dma_unmap_len(tx_buffer, len),
5688 				 DMA_TO_DEVICE);
5689 
5690 		/* check for eop_desc to determine the end of the packet */
5691 		eop_desc = tx_buffer->next_to_watch;
5692 		tx_desc = IXGBE_TX_DESC(tx_ring, i);
5693 
5694 		/* unmap remaining buffers */
5695 		while (tx_desc != eop_desc) {
5696 			tx_buffer++;
5697 			tx_desc++;
5698 			i++;
5699 			if (unlikely(i == tx_ring->count)) {
5700 				i = 0;
5701 				tx_buffer = tx_ring->tx_buffer_info;
5702 				tx_desc = IXGBE_TX_DESC(tx_ring, 0);
5703 			}
5704 
5705 			/* unmap any remaining paged data */
5706 			if (dma_unmap_len(tx_buffer, len))
5707 				dma_unmap_page(tx_ring->dev,
5708 					       dma_unmap_addr(tx_buffer, dma),
5709 					       dma_unmap_len(tx_buffer, len),
5710 					       DMA_TO_DEVICE);
5711 		}
5712 
5713 		/* move us one more past the eop_desc for start of next pkt */
5714 		tx_buffer++;
5715 		i++;
5716 		if (unlikely(i == tx_ring->count)) {
5717 			i = 0;
5718 			tx_buffer = tx_ring->tx_buffer_info;
5719 		}
5720 	}
5721 
5722 	/* reset BQL for queue */
5723 	if (!ring_is_xdp(tx_ring))
5724 		netdev_tx_reset_queue(txring_txq(tx_ring));
5725 
5726 	/* reset next_to_use and next_to_clean */
5727 	tx_ring->next_to_use = 0;
5728 	tx_ring->next_to_clean = 0;
5729 }
5730 
5731 /**
5732  * ixgbe_clean_all_rx_rings - Free Rx Buffers for all queues
5733  * @adapter: board private structure
5734  **/
5735 static void ixgbe_clean_all_rx_rings(struct ixgbe_adapter *adapter)
5736 {
5737 	int i;
5738 
5739 	for (i = 0; i < adapter->num_rx_queues; i++)
5740 		ixgbe_clean_rx_ring(adapter->rx_ring[i]);
5741 }
5742 
5743 /**
5744  * ixgbe_clean_all_tx_rings - Free Tx Buffers for all queues
5745  * @adapter: board private structure
5746  **/
5747 static void ixgbe_clean_all_tx_rings(struct ixgbe_adapter *adapter)
5748 {
5749 	int i;
5750 
5751 	for (i = 0; i < adapter->num_tx_queues; i++)
5752 		ixgbe_clean_tx_ring(adapter->tx_ring[i]);
5753 	for (i = 0; i < adapter->num_xdp_queues; i++)
5754 		ixgbe_clean_tx_ring(adapter->xdp_ring[i]);
5755 }
5756 
5757 static void ixgbe_fdir_filter_exit(struct ixgbe_adapter *adapter)
5758 {
5759 	struct hlist_node *node2;
5760 	struct ixgbe_fdir_filter *filter;
5761 
5762 	spin_lock(&adapter->fdir_perfect_lock);
5763 
5764 	hlist_for_each_entry_safe(filter, node2,
5765 				  &adapter->fdir_filter_list, fdir_node) {
5766 		hlist_del(&filter->fdir_node);
5767 		kfree(filter);
5768 	}
5769 	adapter->fdir_filter_count = 0;
5770 
5771 	spin_unlock(&adapter->fdir_perfect_lock);
5772 }
5773 
5774 static int ixgbe_disable_macvlan(struct net_device *upper, void *data)
5775 {
5776 	if (netif_is_macvlan(upper)) {
5777 		struct macvlan_dev *vlan = netdev_priv(upper);
5778 
5779 		if (vlan->fwd_priv) {
5780 			netif_tx_stop_all_queues(upper);
5781 			netif_carrier_off(upper);
5782 			netif_tx_disable(upper);
5783 		}
5784 	}
5785 
5786 	return 0;
5787 }
5788 
5789 void ixgbe_down(struct ixgbe_adapter *adapter)
5790 {
5791 	struct net_device *netdev = adapter->netdev;
5792 	struct ixgbe_hw *hw = &adapter->hw;
5793 	int i;
5794 
5795 	/* signal that we are down to the interrupt handler */
5796 	if (test_and_set_bit(__IXGBE_DOWN, &adapter->state))
5797 		return; /* do nothing if already down */
5798 
5799 	/* disable receives */
5800 	hw->mac.ops.disable_rx(hw);
5801 
5802 	/* disable all enabled rx queues */
5803 	for (i = 0; i < adapter->num_rx_queues; i++)
5804 		/* this call also flushes the previous write */
5805 		ixgbe_disable_rx_queue(adapter, adapter->rx_ring[i]);
5806 
5807 	usleep_range(10000, 20000);
5808 
5809 	netif_tx_stop_all_queues(netdev);
5810 
5811 	/* call carrier off first to avoid false dev_watchdog timeouts */
5812 	netif_carrier_off(netdev);
5813 	netif_tx_disable(netdev);
5814 
5815 	/* disable any upper devices */
5816 	netdev_walk_all_upper_dev_rcu(adapter->netdev,
5817 				      ixgbe_disable_macvlan, NULL);
5818 
5819 	ixgbe_irq_disable(adapter);
5820 
5821 	ixgbe_napi_disable_all(adapter);
5822 
5823 	clear_bit(__IXGBE_RESET_REQUESTED, &adapter->state);
5824 	adapter->flags2 &= ~IXGBE_FLAG2_FDIR_REQUIRES_REINIT;
5825 	adapter->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
5826 
5827 	del_timer_sync(&adapter->service_timer);
5828 
5829 	if (adapter->num_vfs) {
5830 		/* Clear EITR Select mapping */
5831 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITRSEL, 0);
5832 
5833 		/* Mark all the VFs as inactive */
5834 		for (i = 0 ; i < adapter->num_vfs; i++)
5835 			adapter->vfinfo[i].clear_to_send = false;
5836 
5837 		/* ping all the active vfs to let them know we are going down */
5838 		ixgbe_ping_all_vfs(adapter);
5839 
5840 		/* Disable all VFTE/VFRE TX/RX */
5841 		ixgbe_disable_tx_rx(adapter);
5842 	}
5843 
5844 	/* disable transmits in the hardware now that interrupts are off */
5845 	for (i = 0; i < adapter->num_tx_queues; i++) {
5846 		u8 reg_idx = adapter->tx_ring[i]->reg_idx;
5847 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
5848 	}
5849 	for (i = 0; i < adapter->num_xdp_queues; i++) {
5850 		u8 reg_idx = adapter->xdp_ring[i]->reg_idx;
5851 
5852 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
5853 	}
5854 
5855 	/* Disable the Tx DMA engine on 82599 and later MAC */
5856 	switch (hw->mac.type) {
5857 	case ixgbe_mac_82599EB:
5858 	case ixgbe_mac_X540:
5859 	case ixgbe_mac_X550:
5860 	case ixgbe_mac_X550EM_x:
5861 	case ixgbe_mac_x550em_a:
5862 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL,
5863 				(IXGBE_READ_REG(hw, IXGBE_DMATXCTL) &
5864 				 ~IXGBE_DMATXCTL_TE));
5865 		break;
5866 	default:
5867 		break;
5868 	}
5869 
5870 	if (!pci_channel_offline(adapter->pdev))
5871 		ixgbe_reset(adapter);
5872 
5873 	/* power down the optics for 82599 SFP+ fiber */
5874 	if (hw->mac.ops.disable_tx_laser)
5875 		hw->mac.ops.disable_tx_laser(hw);
5876 
5877 	ixgbe_clean_all_tx_rings(adapter);
5878 	ixgbe_clean_all_rx_rings(adapter);
5879 }
5880 
5881 /**
5882  * ixgbe_eee_capable - helper function to determine EEE support on X550
5883  * @adapter: board private structure
5884  */
5885 static void ixgbe_set_eee_capable(struct ixgbe_adapter *adapter)
5886 {
5887 	struct ixgbe_hw *hw = &adapter->hw;
5888 
5889 	switch (hw->device_id) {
5890 	case IXGBE_DEV_ID_X550EM_A_1G_T:
5891 	case IXGBE_DEV_ID_X550EM_A_1G_T_L:
5892 		if (!hw->phy.eee_speeds_supported)
5893 			break;
5894 		adapter->flags2 |= IXGBE_FLAG2_EEE_CAPABLE;
5895 		if (!hw->phy.eee_speeds_advertised)
5896 			break;
5897 		adapter->flags2 |= IXGBE_FLAG2_EEE_ENABLED;
5898 		break;
5899 	default:
5900 		adapter->flags2 &= ~IXGBE_FLAG2_EEE_CAPABLE;
5901 		adapter->flags2 &= ~IXGBE_FLAG2_EEE_ENABLED;
5902 		break;
5903 	}
5904 }
5905 
5906 /**
5907  * ixgbe_tx_timeout - Respond to a Tx Hang
5908  * @netdev: network interface device structure
5909  **/
5910 static void ixgbe_tx_timeout(struct net_device *netdev)
5911 {
5912 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
5913 
5914 	/* Do the reset outside of interrupt context */
5915 	ixgbe_tx_timeout_reset(adapter);
5916 }
5917 
5918 #ifdef CONFIG_IXGBE_DCB
5919 static void ixgbe_init_dcb(struct ixgbe_adapter *adapter)
5920 {
5921 	struct ixgbe_hw *hw = &adapter->hw;
5922 	struct tc_configuration *tc;
5923 	int j;
5924 
5925 	switch (hw->mac.type) {
5926 	case ixgbe_mac_82598EB:
5927 	case ixgbe_mac_82599EB:
5928 		adapter->dcb_cfg.num_tcs.pg_tcs = MAX_TRAFFIC_CLASS;
5929 		adapter->dcb_cfg.num_tcs.pfc_tcs = MAX_TRAFFIC_CLASS;
5930 		break;
5931 	case ixgbe_mac_X540:
5932 	case ixgbe_mac_X550:
5933 		adapter->dcb_cfg.num_tcs.pg_tcs = X540_TRAFFIC_CLASS;
5934 		adapter->dcb_cfg.num_tcs.pfc_tcs = X540_TRAFFIC_CLASS;
5935 		break;
5936 	case ixgbe_mac_X550EM_x:
5937 	case ixgbe_mac_x550em_a:
5938 	default:
5939 		adapter->dcb_cfg.num_tcs.pg_tcs = DEF_TRAFFIC_CLASS;
5940 		adapter->dcb_cfg.num_tcs.pfc_tcs = DEF_TRAFFIC_CLASS;
5941 		break;
5942 	}
5943 
5944 	/* Configure DCB traffic classes */
5945 	for (j = 0; j < MAX_TRAFFIC_CLASS; j++) {
5946 		tc = &adapter->dcb_cfg.tc_config[j];
5947 		tc->path[DCB_TX_CONFIG].bwg_id = 0;
5948 		tc->path[DCB_TX_CONFIG].bwg_percent = 12 + (j & 1);
5949 		tc->path[DCB_RX_CONFIG].bwg_id = 0;
5950 		tc->path[DCB_RX_CONFIG].bwg_percent = 12 + (j & 1);
5951 		tc->dcb_pfc = pfc_disabled;
5952 	}
5953 
5954 	/* Initialize default user to priority mapping, UPx->TC0 */
5955 	tc = &adapter->dcb_cfg.tc_config[0];
5956 	tc->path[DCB_TX_CONFIG].up_to_tc_bitmap = 0xFF;
5957 	tc->path[DCB_RX_CONFIG].up_to_tc_bitmap = 0xFF;
5958 
5959 	adapter->dcb_cfg.bw_percentage[DCB_TX_CONFIG][0] = 100;
5960 	adapter->dcb_cfg.bw_percentage[DCB_RX_CONFIG][0] = 100;
5961 	adapter->dcb_cfg.pfc_mode_enable = false;
5962 	adapter->dcb_set_bitmap = 0x00;
5963 	if (adapter->flags & IXGBE_FLAG_DCB_CAPABLE)
5964 		adapter->dcbx_cap = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_CEE;
5965 	memcpy(&adapter->temp_dcb_cfg, &adapter->dcb_cfg,
5966 	       sizeof(adapter->temp_dcb_cfg));
5967 }
5968 #endif
5969 
5970 /**
5971  * ixgbe_sw_init - Initialize general software structures (struct ixgbe_adapter)
5972  * @adapter: board private structure to initialize
5973  *
5974  * ixgbe_sw_init initializes the Adapter private data structure.
5975  * Fields are initialized based on PCI device information and
5976  * OS network device settings (MTU size).
5977  **/
5978 static int ixgbe_sw_init(struct ixgbe_adapter *adapter,
5979 			 const struct ixgbe_info *ii)
5980 {
5981 	struct ixgbe_hw *hw = &adapter->hw;
5982 	struct pci_dev *pdev = adapter->pdev;
5983 	unsigned int rss, fdir;
5984 	u32 fwsm;
5985 	int i;
5986 
5987 	/* PCI config space info */
5988 
5989 	hw->vendor_id = pdev->vendor;
5990 	hw->device_id = pdev->device;
5991 	hw->revision_id = pdev->revision;
5992 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
5993 	hw->subsystem_device_id = pdev->subsystem_device;
5994 
5995 	/* get_invariants needs the device IDs */
5996 	ii->get_invariants(hw);
5997 
5998 	/* Set common capability flags and settings */
5999 	rss = min_t(int, ixgbe_max_rss_indices(adapter), num_online_cpus());
6000 	adapter->ring_feature[RING_F_RSS].limit = rss;
6001 	adapter->flags2 |= IXGBE_FLAG2_RSC_CAPABLE;
6002 	adapter->max_q_vectors = MAX_Q_VECTORS_82599;
6003 	adapter->atr_sample_rate = 20;
6004 	fdir = min_t(int, IXGBE_MAX_FDIR_INDICES, num_online_cpus());
6005 	adapter->ring_feature[RING_F_FDIR].limit = fdir;
6006 	adapter->fdir_pballoc = IXGBE_FDIR_PBALLOC_64K;
6007 #ifdef CONFIG_IXGBE_DCA
6008 	adapter->flags |= IXGBE_FLAG_DCA_CAPABLE;
6009 #endif
6010 #ifdef CONFIG_IXGBE_DCB
6011 	adapter->flags |= IXGBE_FLAG_DCB_CAPABLE;
6012 	adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
6013 #endif
6014 #ifdef IXGBE_FCOE
6015 	adapter->flags |= IXGBE_FLAG_FCOE_CAPABLE;
6016 	adapter->flags &= ~IXGBE_FLAG_FCOE_ENABLED;
6017 #ifdef CONFIG_IXGBE_DCB
6018 	/* Default traffic class to use for FCoE */
6019 	adapter->fcoe.up = IXGBE_FCOE_DEFTC;
6020 #endif /* CONFIG_IXGBE_DCB */
6021 #endif /* IXGBE_FCOE */
6022 
6023 	/* initialize static ixgbe jump table entries */
6024 	adapter->jump_tables[0] = kzalloc(sizeof(*adapter->jump_tables[0]),
6025 					  GFP_KERNEL);
6026 	if (!adapter->jump_tables[0])
6027 		return -ENOMEM;
6028 	adapter->jump_tables[0]->mat = ixgbe_ipv4_fields;
6029 
6030 	for (i = 1; i < IXGBE_MAX_LINK_HANDLE; i++)
6031 		adapter->jump_tables[i] = NULL;
6032 
6033 	adapter->mac_table = kzalloc(sizeof(struct ixgbe_mac_addr) *
6034 				     hw->mac.num_rar_entries,
6035 				     GFP_ATOMIC);
6036 	if (!adapter->mac_table)
6037 		return -ENOMEM;
6038 
6039 	if (ixgbe_init_rss_key(adapter))
6040 		return -ENOMEM;
6041 
6042 	/* Set MAC specific capability flags and exceptions */
6043 	switch (hw->mac.type) {
6044 	case ixgbe_mac_82598EB:
6045 		adapter->flags2 &= ~IXGBE_FLAG2_RSC_CAPABLE;
6046 
6047 		if (hw->device_id == IXGBE_DEV_ID_82598AT)
6048 			adapter->flags |= IXGBE_FLAG_FAN_FAIL_CAPABLE;
6049 
6050 		adapter->max_q_vectors = MAX_Q_VECTORS_82598;
6051 		adapter->ring_feature[RING_F_FDIR].limit = 0;
6052 		adapter->atr_sample_rate = 0;
6053 		adapter->fdir_pballoc = 0;
6054 #ifdef IXGBE_FCOE
6055 		adapter->flags &= ~IXGBE_FLAG_FCOE_CAPABLE;
6056 		adapter->flags &= ~IXGBE_FLAG_FCOE_ENABLED;
6057 #ifdef CONFIG_IXGBE_DCB
6058 		adapter->fcoe.up = 0;
6059 #endif /* IXGBE_DCB */
6060 #endif /* IXGBE_FCOE */
6061 		break;
6062 	case ixgbe_mac_82599EB:
6063 		if (hw->device_id == IXGBE_DEV_ID_82599_T3_LOM)
6064 			adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_CAPABLE;
6065 		break;
6066 	case ixgbe_mac_X540:
6067 		fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM(hw));
6068 		if (fwsm & IXGBE_FWSM_TS_ENABLED)
6069 			adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_CAPABLE;
6070 		break;
6071 	case ixgbe_mac_x550em_a:
6072 		adapter->flags |= IXGBE_FLAG_GENEVE_OFFLOAD_CAPABLE;
6073 		switch (hw->device_id) {
6074 		case IXGBE_DEV_ID_X550EM_A_1G_T:
6075 		case IXGBE_DEV_ID_X550EM_A_1G_T_L:
6076 			adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_CAPABLE;
6077 			break;
6078 		default:
6079 			break;
6080 		}
6081 	/* fall through */
6082 	case ixgbe_mac_X550EM_x:
6083 #ifdef CONFIG_IXGBE_DCB
6084 		adapter->flags &= ~IXGBE_FLAG_DCB_CAPABLE;
6085 #endif
6086 #ifdef IXGBE_FCOE
6087 		adapter->flags &= ~IXGBE_FLAG_FCOE_CAPABLE;
6088 #ifdef CONFIG_IXGBE_DCB
6089 		adapter->fcoe.up = 0;
6090 #endif /* IXGBE_DCB */
6091 #endif /* IXGBE_FCOE */
6092 	/* Fall Through */
6093 	case ixgbe_mac_X550:
6094 		if (hw->mac.type == ixgbe_mac_X550)
6095 			adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_CAPABLE;
6096 #ifdef CONFIG_IXGBE_DCA
6097 		adapter->flags &= ~IXGBE_FLAG_DCA_CAPABLE;
6098 #endif
6099 		adapter->flags |= IXGBE_FLAG_VXLAN_OFFLOAD_CAPABLE;
6100 		break;
6101 	default:
6102 		break;
6103 	}
6104 
6105 #ifdef IXGBE_FCOE
6106 	/* FCoE support exists, always init the FCoE lock */
6107 	spin_lock_init(&adapter->fcoe.lock);
6108 
6109 #endif
6110 	/* n-tuple support exists, always init our spinlock */
6111 	spin_lock_init(&adapter->fdir_perfect_lock);
6112 
6113 #ifdef CONFIG_IXGBE_DCB
6114 	ixgbe_init_dcb(adapter);
6115 #endif
6116 
6117 	/* default flow control settings */
6118 	hw->fc.requested_mode = ixgbe_fc_full;
6119 	hw->fc.current_mode = ixgbe_fc_full;	/* init for ethtool output */
6120 	ixgbe_pbthresh_setup(adapter);
6121 	hw->fc.pause_time = IXGBE_DEFAULT_FCPAUSE;
6122 	hw->fc.send_xon = true;
6123 	hw->fc.disable_fc_autoneg = ixgbe_device_supports_autoneg_fc(hw);
6124 
6125 #ifdef CONFIG_PCI_IOV
6126 	if (max_vfs > 0)
6127 		e_dev_warn("Enabling SR-IOV VFs using the max_vfs module parameter is deprecated - please use the pci sysfs interface instead.\n");
6128 
6129 	/* assign number of SR-IOV VFs */
6130 	if (hw->mac.type != ixgbe_mac_82598EB) {
6131 		if (max_vfs > IXGBE_MAX_VFS_DRV_LIMIT) {
6132 			max_vfs = 0;
6133 			e_dev_warn("max_vfs parameter out of range. Not assigning any SR-IOV VFs\n");
6134 		}
6135 	}
6136 #endif /* CONFIG_PCI_IOV */
6137 
6138 	/* enable itr by default in dynamic mode */
6139 	adapter->rx_itr_setting = 1;
6140 	adapter->tx_itr_setting = 1;
6141 
6142 	/* set default ring sizes */
6143 	adapter->tx_ring_count = IXGBE_DEFAULT_TXD;
6144 	adapter->rx_ring_count = IXGBE_DEFAULT_RXD;
6145 
6146 	/* set default work limits */
6147 	adapter->tx_work_limit = IXGBE_DEFAULT_TX_WORK;
6148 
6149 	/* initialize eeprom parameters */
6150 	if (ixgbe_init_eeprom_params_generic(hw)) {
6151 		e_dev_err("EEPROM initialization failed\n");
6152 		return -EIO;
6153 	}
6154 
6155 	/* PF holds first pool slot */
6156 	set_bit(0, &adapter->fwd_bitmask);
6157 	set_bit(__IXGBE_DOWN, &adapter->state);
6158 
6159 	return 0;
6160 }
6161 
6162 /**
6163  * ixgbe_setup_tx_resources - allocate Tx resources (Descriptors)
6164  * @tx_ring:    tx descriptor ring (for a specific queue) to setup
6165  *
6166  * Return 0 on success, negative on failure
6167  **/
6168 int ixgbe_setup_tx_resources(struct ixgbe_ring *tx_ring)
6169 {
6170 	struct device *dev = tx_ring->dev;
6171 	int orig_node = dev_to_node(dev);
6172 	int ring_node = -1;
6173 	int size;
6174 
6175 	size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
6176 
6177 	if (tx_ring->q_vector)
6178 		ring_node = tx_ring->q_vector->numa_node;
6179 
6180 	tx_ring->tx_buffer_info = vmalloc_node(size, ring_node);
6181 	if (!tx_ring->tx_buffer_info)
6182 		tx_ring->tx_buffer_info = vmalloc(size);
6183 	if (!tx_ring->tx_buffer_info)
6184 		goto err;
6185 
6186 	u64_stats_init(&tx_ring->syncp);
6187 
6188 	/* round up to nearest 4K */
6189 	tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
6190 	tx_ring->size = ALIGN(tx_ring->size, 4096);
6191 
6192 	set_dev_node(dev, ring_node);
6193 	tx_ring->desc = dma_alloc_coherent(dev,
6194 					   tx_ring->size,
6195 					   &tx_ring->dma,
6196 					   GFP_KERNEL);
6197 	set_dev_node(dev, orig_node);
6198 	if (!tx_ring->desc)
6199 		tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
6200 						   &tx_ring->dma, GFP_KERNEL);
6201 	if (!tx_ring->desc)
6202 		goto err;
6203 
6204 	tx_ring->next_to_use = 0;
6205 	tx_ring->next_to_clean = 0;
6206 	return 0;
6207 
6208 err:
6209 	vfree(tx_ring->tx_buffer_info);
6210 	tx_ring->tx_buffer_info = NULL;
6211 	dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
6212 	return -ENOMEM;
6213 }
6214 
6215 /**
6216  * ixgbe_setup_all_tx_resources - allocate all queues Tx resources
6217  * @adapter: board private structure
6218  *
6219  * If this function returns with an error, then it's possible one or
6220  * more of the rings is populated (while the rest are not).  It is the
6221  * callers duty to clean those orphaned rings.
6222  *
6223  * Return 0 on success, negative on failure
6224  **/
6225 static int ixgbe_setup_all_tx_resources(struct ixgbe_adapter *adapter)
6226 {
6227 	int i, j = 0, err = 0;
6228 
6229 	for (i = 0; i < adapter->num_tx_queues; i++) {
6230 		err = ixgbe_setup_tx_resources(adapter->tx_ring[i]);
6231 		if (!err)
6232 			continue;
6233 
6234 		e_err(probe, "Allocation for Tx Queue %u failed\n", i);
6235 		goto err_setup_tx;
6236 	}
6237 	for (j = 0; j < adapter->num_xdp_queues; j++) {
6238 		err = ixgbe_setup_tx_resources(adapter->xdp_ring[j]);
6239 		if (!err)
6240 			continue;
6241 
6242 		e_err(probe, "Allocation for Tx Queue %u failed\n", j);
6243 		goto err_setup_tx;
6244 	}
6245 
6246 	return 0;
6247 err_setup_tx:
6248 	/* rewind the index freeing the rings as we go */
6249 	while (j--)
6250 		ixgbe_free_tx_resources(adapter->xdp_ring[j]);
6251 	while (i--)
6252 		ixgbe_free_tx_resources(adapter->tx_ring[i]);
6253 	return err;
6254 }
6255 
6256 /**
6257  * ixgbe_setup_rx_resources - allocate Rx resources (Descriptors)
6258  * @rx_ring:    rx descriptor ring (for a specific queue) to setup
6259  *
6260  * Returns 0 on success, negative on failure
6261  **/
6262 int ixgbe_setup_rx_resources(struct ixgbe_adapter *adapter,
6263 			     struct ixgbe_ring *rx_ring)
6264 {
6265 	struct device *dev = rx_ring->dev;
6266 	int orig_node = dev_to_node(dev);
6267 	int ring_node = -1;
6268 	int size;
6269 
6270 	size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
6271 
6272 	if (rx_ring->q_vector)
6273 		ring_node = rx_ring->q_vector->numa_node;
6274 
6275 	rx_ring->rx_buffer_info = vmalloc_node(size, ring_node);
6276 	if (!rx_ring->rx_buffer_info)
6277 		rx_ring->rx_buffer_info = vmalloc(size);
6278 	if (!rx_ring->rx_buffer_info)
6279 		goto err;
6280 
6281 	u64_stats_init(&rx_ring->syncp);
6282 
6283 	/* Round up to nearest 4K */
6284 	rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
6285 	rx_ring->size = ALIGN(rx_ring->size, 4096);
6286 
6287 	set_dev_node(dev, ring_node);
6288 	rx_ring->desc = dma_alloc_coherent(dev,
6289 					   rx_ring->size,
6290 					   &rx_ring->dma,
6291 					   GFP_KERNEL);
6292 	set_dev_node(dev, orig_node);
6293 	if (!rx_ring->desc)
6294 		rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
6295 						   &rx_ring->dma, GFP_KERNEL);
6296 	if (!rx_ring->desc)
6297 		goto err;
6298 
6299 	rx_ring->next_to_clean = 0;
6300 	rx_ring->next_to_use = 0;
6301 
6302 	rx_ring->xdp_prog = adapter->xdp_prog;
6303 
6304 	return 0;
6305 err:
6306 	vfree(rx_ring->rx_buffer_info);
6307 	rx_ring->rx_buffer_info = NULL;
6308 	dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
6309 	return -ENOMEM;
6310 }
6311 
6312 /**
6313  * ixgbe_setup_all_rx_resources - allocate all queues Rx resources
6314  * @adapter: board private structure
6315  *
6316  * If this function returns with an error, then it's possible one or
6317  * more of the rings is populated (while the rest are not).  It is the
6318  * callers duty to clean those orphaned rings.
6319  *
6320  * Return 0 on success, negative on failure
6321  **/
6322 static int ixgbe_setup_all_rx_resources(struct ixgbe_adapter *adapter)
6323 {
6324 	int i, err = 0;
6325 
6326 	for (i = 0; i < adapter->num_rx_queues; i++) {
6327 		err = ixgbe_setup_rx_resources(adapter, adapter->rx_ring[i]);
6328 		if (!err)
6329 			continue;
6330 
6331 		e_err(probe, "Allocation for Rx Queue %u failed\n", i);
6332 		goto err_setup_rx;
6333 	}
6334 
6335 #ifdef IXGBE_FCOE
6336 	err = ixgbe_setup_fcoe_ddp_resources(adapter);
6337 	if (!err)
6338 #endif
6339 		return 0;
6340 err_setup_rx:
6341 	/* rewind the index freeing the rings as we go */
6342 	while (i--)
6343 		ixgbe_free_rx_resources(adapter->rx_ring[i]);
6344 	return err;
6345 }
6346 
6347 /**
6348  * ixgbe_free_tx_resources - Free Tx Resources per Queue
6349  * @tx_ring: Tx descriptor ring for a specific queue
6350  *
6351  * Free all transmit software resources
6352  **/
6353 void ixgbe_free_tx_resources(struct ixgbe_ring *tx_ring)
6354 {
6355 	ixgbe_clean_tx_ring(tx_ring);
6356 
6357 	vfree(tx_ring->tx_buffer_info);
6358 	tx_ring->tx_buffer_info = NULL;
6359 
6360 	/* if not set, then don't free */
6361 	if (!tx_ring->desc)
6362 		return;
6363 
6364 	dma_free_coherent(tx_ring->dev, tx_ring->size,
6365 			  tx_ring->desc, tx_ring->dma);
6366 
6367 	tx_ring->desc = NULL;
6368 }
6369 
6370 /**
6371  * ixgbe_free_all_tx_resources - Free Tx Resources for All Queues
6372  * @adapter: board private structure
6373  *
6374  * Free all transmit software resources
6375  **/
6376 static void ixgbe_free_all_tx_resources(struct ixgbe_adapter *adapter)
6377 {
6378 	int i;
6379 
6380 	for (i = 0; i < adapter->num_tx_queues; i++)
6381 		if (adapter->tx_ring[i]->desc)
6382 			ixgbe_free_tx_resources(adapter->tx_ring[i]);
6383 	for (i = 0; i < adapter->num_xdp_queues; i++)
6384 		if (adapter->xdp_ring[i]->desc)
6385 			ixgbe_free_tx_resources(adapter->xdp_ring[i]);
6386 }
6387 
6388 /**
6389  * ixgbe_free_rx_resources - Free Rx Resources
6390  * @rx_ring: ring to clean the resources from
6391  *
6392  * Free all receive software resources
6393  **/
6394 void ixgbe_free_rx_resources(struct ixgbe_ring *rx_ring)
6395 {
6396 	ixgbe_clean_rx_ring(rx_ring);
6397 
6398 	rx_ring->xdp_prog = NULL;
6399 	vfree(rx_ring->rx_buffer_info);
6400 	rx_ring->rx_buffer_info = NULL;
6401 
6402 	/* if not set, then don't free */
6403 	if (!rx_ring->desc)
6404 		return;
6405 
6406 	dma_free_coherent(rx_ring->dev, rx_ring->size,
6407 			  rx_ring->desc, rx_ring->dma);
6408 
6409 	rx_ring->desc = NULL;
6410 }
6411 
6412 /**
6413  * ixgbe_free_all_rx_resources - Free Rx Resources for All Queues
6414  * @adapter: board private structure
6415  *
6416  * Free all receive software resources
6417  **/
6418 static void ixgbe_free_all_rx_resources(struct ixgbe_adapter *adapter)
6419 {
6420 	int i;
6421 
6422 #ifdef IXGBE_FCOE
6423 	ixgbe_free_fcoe_ddp_resources(adapter);
6424 
6425 #endif
6426 	for (i = 0; i < adapter->num_rx_queues; i++)
6427 		if (adapter->rx_ring[i]->desc)
6428 			ixgbe_free_rx_resources(adapter->rx_ring[i]);
6429 }
6430 
6431 /**
6432  * ixgbe_change_mtu - Change the Maximum Transfer Unit
6433  * @netdev: network interface device structure
6434  * @new_mtu: new value for maximum frame size
6435  *
6436  * Returns 0 on success, negative on failure
6437  **/
6438 static int ixgbe_change_mtu(struct net_device *netdev, int new_mtu)
6439 {
6440 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
6441 
6442 	/*
6443 	 * For 82599EB we cannot allow legacy VFs to enable their receive
6444 	 * paths when MTU greater than 1500 is configured.  So display a
6445 	 * warning that legacy VFs will be disabled.
6446 	 */
6447 	if ((adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) &&
6448 	    (adapter->hw.mac.type == ixgbe_mac_82599EB) &&
6449 	    (new_mtu > ETH_DATA_LEN))
6450 		e_warn(probe, "Setting MTU > 1500 will disable legacy VFs\n");
6451 
6452 	e_info(probe, "changing MTU from %d to %d\n", netdev->mtu, new_mtu);
6453 
6454 	/* must set new MTU before calling down or up */
6455 	netdev->mtu = new_mtu;
6456 
6457 	if (netif_running(netdev))
6458 		ixgbe_reinit_locked(adapter);
6459 
6460 	return 0;
6461 }
6462 
6463 /**
6464  * ixgbe_open - Called when a network interface is made active
6465  * @netdev: network interface device structure
6466  *
6467  * Returns 0 on success, negative value on failure
6468  *
6469  * The open entry point is called when a network interface is made
6470  * active by the system (IFF_UP).  At this point all resources needed
6471  * for transmit and receive operations are allocated, the interrupt
6472  * handler is registered with the OS, the watchdog timer is started,
6473  * and the stack is notified that the interface is ready.
6474  **/
6475 int ixgbe_open(struct net_device *netdev)
6476 {
6477 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
6478 	struct ixgbe_hw *hw = &adapter->hw;
6479 	int err, queues;
6480 
6481 	/* disallow open during test */
6482 	if (test_bit(__IXGBE_TESTING, &adapter->state))
6483 		return -EBUSY;
6484 
6485 	netif_carrier_off(netdev);
6486 
6487 	/* allocate transmit descriptors */
6488 	err = ixgbe_setup_all_tx_resources(adapter);
6489 	if (err)
6490 		goto err_setup_tx;
6491 
6492 	/* allocate receive descriptors */
6493 	err = ixgbe_setup_all_rx_resources(adapter);
6494 	if (err)
6495 		goto err_setup_rx;
6496 
6497 	ixgbe_configure(adapter);
6498 
6499 	err = ixgbe_request_irq(adapter);
6500 	if (err)
6501 		goto err_req_irq;
6502 
6503 	/* Notify the stack of the actual queue counts. */
6504 	if (adapter->num_rx_pools > 1)
6505 		queues = adapter->num_rx_queues_per_pool;
6506 	else
6507 		queues = adapter->num_tx_queues;
6508 
6509 	err = netif_set_real_num_tx_queues(netdev, queues);
6510 	if (err)
6511 		goto err_set_queues;
6512 
6513 	if (adapter->num_rx_pools > 1 &&
6514 	    adapter->num_rx_queues > IXGBE_MAX_L2A_QUEUES)
6515 		queues = IXGBE_MAX_L2A_QUEUES;
6516 	else
6517 		queues = adapter->num_rx_queues;
6518 	err = netif_set_real_num_rx_queues(netdev, queues);
6519 	if (err)
6520 		goto err_set_queues;
6521 
6522 	ixgbe_ptp_init(adapter);
6523 
6524 	ixgbe_up_complete(adapter);
6525 
6526 	ixgbe_clear_udp_tunnel_port(adapter, IXGBE_VXLANCTRL_ALL_UDPPORT_MASK);
6527 	udp_tunnel_get_rx_info(netdev);
6528 
6529 	return 0;
6530 
6531 err_set_queues:
6532 	ixgbe_free_irq(adapter);
6533 err_req_irq:
6534 	ixgbe_free_all_rx_resources(adapter);
6535 	if (hw->phy.ops.set_phy_power && !adapter->wol)
6536 		hw->phy.ops.set_phy_power(&adapter->hw, false);
6537 err_setup_rx:
6538 	ixgbe_free_all_tx_resources(adapter);
6539 err_setup_tx:
6540 	ixgbe_reset(adapter);
6541 
6542 	return err;
6543 }
6544 
6545 static void ixgbe_close_suspend(struct ixgbe_adapter *adapter)
6546 {
6547 	ixgbe_ptp_suspend(adapter);
6548 
6549 	if (adapter->hw.phy.ops.enter_lplu) {
6550 		adapter->hw.phy.reset_disable = true;
6551 		ixgbe_down(adapter);
6552 		adapter->hw.phy.ops.enter_lplu(&adapter->hw);
6553 		adapter->hw.phy.reset_disable = false;
6554 	} else {
6555 		ixgbe_down(adapter);
6556 	}
6557 
6558 	ixgbe_free_irq(adapter);
6559 
6560 	ixgbe_free_all_tx_resources(adapter);
6561 	ixgbe_free_all_rx_resources(adapter);
6562 }
6563 
6564 /**
6565  * ixgbe_close - Disables a network interface
6566  * @netdev: network interface device structure
6567  *
6568  * Returns 0, this is not allowed to fail
6569  *
6570  * The close entry point is called when an interface is de-activated
6571  * by the OS.  The hardware is still under the drivers control, but
6572  * needs to be disabled.  A global MAC reset is issued to stop the
6573  * hardware, and all transmit and receive resources are freed.
6574  **/
6575 int ixgbe_close(struct net_device *netdev)
6576 {
6577 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
6578 
6579 	ixgbe_ptp_stop(adapter);
6580 
6581 	if (netif_device_present(netdev))
6582 		ixgbe_close_suspend(adapter);
6583 
6584 	ixgbe_fdir_filter_exit(adapter);
6585 
6586 	ixgbe_release_hw_control(adapter);
6587 
6588 	return 0;
6589 }
6590 
6591 #ifdef CONFIG_PM
6592 static int ixgbe_resume(struct pci_dev *pdev)
6593 {
6594 	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
6595 	struct net_device *netdev = adapter->netdev;
6596 	u32 err;
6597 
6598 	adapter->hw.hw_addr = adapter->io_addr;
6599 	pci_set_power_state(pdev, PCI_D0);
6600 	pci_restore_state(pdev);
6601 	/*
6602 	 * pci_restore_state clears dev->state_saved so call
6603 	 * pci_save_state to restore it.
6604 	 */
6605 	pci_save_state(pdev);
6606 
6607 	err = pci_enable_device_mem(pdev);
6608 	if (err) {
6609 		e_dev_err("Cannot enable PCI device from suspend\n");
6610 		return err;
6611 	}
6612 	smp_mb__before_atomic();
6613 	clear_bit(__IXGBE_DISABLED, &adapter->state);
6614 	pci_set_master(pdev);
6615 
6616 	pci_wake_from_d3(pdev, false);
6617 
6618 	ixgbe_reset(adapter);
6619 
6620 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_WUS, ~0);
6621 
6622 	rtnl_lock();
6623 	err = ixgbe_init_interrupt_scheme(adapter);
6624 	if (!err && netif_running(netdev))
6625 		err = ixgbe_open(netdev);
6626 
6627 
6628 	if (!err)
6629 		netif_device_attach(netdev);
6630 	rtnl_unlock();
6631 
6632 	return err;
6633 }
6634 #endif /* CONFIG_PM */
6635 
6636 static int __ixgbe_shutdown(struct pci_dev *pdev, bool *enable_wake)
6637 {
6638 	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
6639 	struct net_device *netdev = adapter->netdev;
6640 	struct ixgbe_hw *hw = &adapter->hw;
6641 	u32 ctrl, fctrl;
6642 	u32 wufc = adapter->wol;
6643 #ifdef CONFIG_PM
6644 	int retval = 0;
6645 #endif
6646 
6647 	rtnl_lock();
6648 	netif_device_detach(netdev);
6649 
6650 	if (netif_running(netdev))
6651 		ixgbe_close_suspend(adapter);
6652 
6653 	ixgbe_clear_interrupt_scheme(adapter);
6654 	rtnl_unlock();
6655 
6656 #ifdef CONFIG_PM
6657 	retval = pci_save_state(pdev);
6658 	if (retval)
6659 		return retval;
6660 
6661 #endif
6662 	if (hw->mac.ops.stop_link_on_d3)
6663 		hw->mac.ops.stop_link_on_d3(hw);
6664 
6665 	if (wufc) {
6666 		ixgbe_set_rx_mode(netdev);
6667 
6668 		/* enable the optics for 82599 SFP+ fiber as we can WoL */
6669 		if (hw->mac.ops.enable_tx_laser)
6670 			hw->mac.ops.enable_tx_laser(hw);
6671 
6672 		/* turn on all-multi mode if wake on multicast is enabled */
6673 		if (wufc & IXGBE_WUFC_MC) {
6674 			fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
6675 			fctrl |= IXGBE_FCTRL_MPE;
6676 			IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
6677 		}
6678 
6679 		ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
6680 		ctrl |= IXGBE_CTRL_GIO_DIS;
6681 		IXGBE_WRITE_REG(hw, IXGBE_CTRL, ctrl);
6682 
6683 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, wufc);
6684 	} else {
6685 		IXGBE_WRITE_REG(hw, IXGBE_WUC, 0);
6686 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
6687 	}
6688 
6689 	switch (hw->mac.type) {
6690 	case ixgbe_mac_82598EB:
6691 		pci_wake_from_d3(pdev, false);
6692 		break;
6693 	case ixgbe_mac_82599EB:
6694 	case ixgbe_mac_X540:
6695 	case ixgbe_mac_X550:
6696 	case ixgbe_mac_X550EM_x:
6697 	case ixgbe_mac_x550em_a:
6698 		pci_wake_from_d3(pdev, !!wufc);
6699 		break;
6700 	default:
6701 		break;
6702 	}
6703 
6704 	*enable_wake = !!wufc;
6705 	if (hw->phy.ops.set_phy_power && !*enable_wake)
6706 		hw->phy.ops.set_phy_power(hw, false);
6707 
6708 	ixgbe_release_hw_control(adapter);
6709 
6710 	if (!test_and_set_bit(__IXGBE_DISABLED, &adapter->state))
6711 		pci_disable_device(pdev);
6712 
6713 	return 0;
6714 }
6715 
6716 #ifdef CONFIG_PM
6717 static int ixgbe_suspend(struct pci_dev *pdev, pm_message_t state)
6718 {
6719 	int retval;
6720 	bool wake;
6721 
6722 	retval = __ixgbe_shutdown(pdev, &wake);
6723 	if (retval)
6724 		return retval;
6725 
6726 	if (wake) {
6727 		pci_prepare_to_sleep(pdev);
6728 	} else {
6729 		pci_wake_from_d3(pdev, false);
6730 		pci_set_power_state(pdev, PCI_D3hot);
6731 	}
6732 
6733 	return 0;
6734 }
6735 #endif /* CONFIG_PM */
6736 
6737 static void ixgbe_shutdown(struct pci_dev *pdev)
6738 {
6739 	bool wake;
6740 
6741 	__ixgbe_shutdown(pdev, &wake);
6742 
6743 	if (system_state == SYSTEM_POWER_OFF) {
6744 		pci_wake_from_d3(pdev, wake);
6745 		pci_set_power_state(pdev, PCI_D3hot);
6746 	}
6747 }
6748 
6749 /**
6750  * ixgbe_update_stats - Update the board statistics counters.
6751  * @adapter: board private structure
6752  **/
6753 void ixgbe_update_stats(struct ixgbe_adapter *adapter)
6754 {
6755 	struct net_device *netdev = adapter->netdev;
6756 	struct ixgbe_hw *hw = &adapter->hw;
6757 	struct ixgbe_hw_stats *hwstats = &adapter->stats;
6758 	u64 total_mpc = 0;
6759 	u32 i, missed_rx = 0, mpc, bprc, lxon, lxoff, xon_off_tot;
6760 	u64 non_eop_descs = 0, restart_queue = 0, tx_busy = 0;
6761 	u64 alloc_rx_page_failed = 0, alloc_rx_buff_failed = 0;
6762 	u64 bytes = 0, packets = 0, hw_csum_rx_error = 0;
6763 
6764 	if (test_bit(__IXGBE_DOWN, &adapter->state) ||
6765 	    test_bit(__IXGBE_RESETTING, &adapter->state))
6766 		return;
6767 
6768 	if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED) {
6769 		u64 rsc_count = 0;
6770 		u64 rsc_flush = 0;
6771 		for (i = 0; i < adapter->num_rx_queues; i++) {
6772 			rsc_count += adapter->rx_ring[i]->rx_stats.rsc_count;
6773 			rsc_flush += adapter->rx_ring[i]->rx_stats.rsc_flush;
6774 		}
6775 		adapter->rsc_total_count = rsc_count;
6776 		adapter->rsc_total_flush = rsc_flush;
6777 	}
6778 
6779 	for (i = 0; i < adapter->num_rx_queues; i++) {
6780 		struct ixgbe_ring *rx_ring = adapter->rx_ring[i];
6781 		non_eop_descs += rx_ring->rx_stats.non_eop_descs;
6782 		alloc_rx_page_failed += rx_ring->rx_stats.alloc_rx_page_failed;
6783 		alloc_rx_buff_failed += rx_ring->rx_stats.alloc_rx_buff_failed;
6784 		hw_csum_rx_error += rx_ring->rx_stats.csum_err;
6785 		bytes += rx_ring->stats.bytes;
6786 		packets += rx_ring->stats.packets;
6787 	}
6788 	adapter->non_eop_descs = non_eop_descs;
6789 	adapter->alloc_rx_page_failed = alloc_rx_page_failed;
6790 	adapter->alloc_rx_buff_failed = alloc_rx_buff_failed;
6791 	adapter->hw_csum_rx_error = hw_csum_rx_error;
6792 	netdev->stats.rx_bytes = bytes;
6793 	netdev->stats.rx_packets = packets;
6794 
6795 	bytes = 0;
6796 	packets = 0;
6797 	/* gather some stats to the adapter struct that are per queue */
6798 	for (i = 0; i < adapter->num_tx_queues; i++) {
6799 		struct ixgbe_ring *tx_ring = adapter->tx_ring[i];
6800 		restart_queue += tx_ring->tx_stats.restart_queue;
6801 		tx_busy += tx_ring->tx_stats.tx_busy;
6802 		bytes += tx_ring->stats.bytes;
6803 		packets += tx_ring->stats.packets;
6804 	}
6805 	for (i = 0; i < adapter->num_xdp_queues; i++) {
6806 		struct ixgbe_ring *xdp_ring = adapter->xdp_ring[i];
6807 
6808 		restart_queue += xdp_ring->tx_stats.restart_queue;
6809 		tx_busy += xdp_ring->tx_stats.tx_busy;
6810 		bytes += xdp_ring->stats.bytes;
6811 		packets += xdp_ring->stats.packets;
6812 	}
6813 	adapter->restart_queue = restart_queue;
6814 	adapter->tx_busy = tx_busy;
6815 	netdev->stats.tx_bytes = bytes;
6816 	netdev->stats.tx_packets = packets;
6817 
6818 	hwstats->crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
6819 
6820 	/* 8 register reads */
6821 	for (i = 0; i < 8; i++) {
6822 		/* for packet buffers not used, the register should read 0 */
6823 		mpc = IXGBE_READ_REG(hw, IXGBE_MPC(i));
6824 		missed_rx += mpc;
6825 		hwstats->mpc[i] += mpc;
6826 		total_mpc += hwstats->mpc[i];
6827 		hwstats->pxontxc[i] += IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
6828 		hwstats->pxofftxc[i] += IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
6829 		switch (hw->mac.type) {
6830 		case ixgbe_mac_82598EB:
6831 			hwstats->rnbc[i] += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
6832 			hwstats->qbtc[i] += IXGBE_READ_REG(hw, IXGBE_QBTC(i));
6833 			hwstats->qbrc[i] += IXGBE_READ_REG(hw, IXGBE_QBRC(i));
6834 			hwstats->pxonrxc[i] +=
6835 				IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
6836 			break;
6837 		case ixgbe_mac_82599EB:
6838 		case ixgbe_mac_X540:
6839 		case ixgbe_mac_X550:
6840 		case ixgbe_mac_X550EM_x:
6841 		case ixgbe_mac_x550em_a:
6842 			hwstats->pxonrxc[i] +=
6843 				IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
6844 			break;
6845 		default:
6846 			break;
6847 		}
6848 	}
6849 
6850 	/*16 register reads */
6851 	for (i = 0; i < 16; i++) {
6852 		hwstats->qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
6853 		hwstats->qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
6854 		if ((hw->mac.type == ixgbe_mac_82599EB) ||
6855 		    (hw->mac.type == ixgbe_mac_X540) ||
6856 		    (hw->mac.type == ixgbe_mac_X550) ||
6857 		    (hw->mac.type == ixgbe_mac_X550EM_x) ||
6858 		    (hw->mac.type == ixgbe_mac_x550em_a)) {
6859 			hwstats->qbtc[i] += IXGBE_READ_REG(hw, IXGBE_QBTC_L(i));
6860 			IXGBE_READ_REG(hw, IXGBE_QBTC_H(i)); /* to clear */
6861 			hwstats->qbrc[i] += IXGBE_READ_REG(hw, IXGBE_QBRC_L(i));
6862 			IXGBE_READ_REG(hw, IXGBE_QBRC_H(i)); /* to clear */
6863 		}
6864 	}
6865 
6866 	hwstats->gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
6867 	/* work around hardware counting issue */
6868 	hwstats->gprc -= missed_rx;
6869 
6870 	ixgbe_update_xoff_received(adapter);
6871 
6872 	/* 82598 hardware only has a 32 bit counter in the high register */
6873 	switch (hw->mac.type) {
6874 	case ixgbe_mac_82598EB:
6875 		hwstats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
6876 		hwstats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
6877 		hwstats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
6878 		hwstats->tor += IXGBE_READ_REG(hw, IXGBE_TORH);
6879 		break;
6880 	case ixgbe_mac_X540:
6881 	case ixgbe_mac_X550:
6882 	case ixgbe_mac_X550EM_x:
6883 	case ixgbe_mac_x550em_a:
6884 		/* OS2BMC stats are X540 and later */
6885 		hwstats->o2bgptc += IXGBE_READ_REG(hw, IXGBE_O2BGPTC);
6886 		hwstats->o2bspc += IXGBE_READ_REG(hw, IXGBE_O2BSPC);
6887 		hwstats->b2ospc += IXGBE_READ_REG(hw, IXGBE_B2OSPC);
6888 		hwstats->b2ogprc += IXGBE_READ_REG(hw, IXGBE_B2OGPRC);
6889 	case ixgbe_mac_82599EB:
6890 		for (i = 0; i < 16; i++)
6891 			adapter->hw_rx_no_dma_resources +=
6892 					     IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
6893 		hwstats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCL);
6894 		IXGBE_READ_REG(hw, IXGBE_GORCH); /* to clear */
6895 		hwstats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCL);
6896 		IXGBE_READ_REG(hw, IXGBE_GOTCH); /* to clear */
6897 		hwstats->tor += IXGBE_READ_REG(hw, IXGBE_TORL);
6898 		IXGBE_READ_REG(hw, IXGBE_TORH); /* to clear */
6899 		hwstats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
6900 		hwstats->fdirmatch += IXGBE_READ_REG(hw, IXGBE_FDIRMATCH);
6901 		hwstats->fdirmiss += IXGBE_READ_REG(hw, IXGBE_FDIRMISS);
6902 #ifdef IXGBE_FCOE
6903 		hwstats->fccrc += IXGBE_READ_REG(hw, IXGBE_FCCRC);
6904 		hwstats->fcoerpdc += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
6905 		hwstats->fcoeprc += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
6906 		hwstats->fcoeptc += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
6907 		hwstats->fcoedwrc += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
6908 		hwstats->fcoedwtc += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
6909 		/* Add up per cpu counters for total ddp aloc fail */
6910 		if (adapter->fcoe.ddp_pool) {
6911 			struct ixgbe_fcoe *fcoe = &adapter->fcoe;
6912 			struct ixgbe_fcoe_ddp_pool *ddp_pool;
6913 			unsigned int cpu;
6914 			u64 noddp = 0, noddp_ext_buff = 0;
6915 			for_each_possible_cpu(cpu) {
6916 				ddp_pool = per_cpu_ptr(fcoe->ddp_pool, cpu);
6917 				noddp += ddp_pool->noddp;
6918 				noddp_ext_buff += ddp_pool->noddp_ext_buff;
6919 			}
6920 			hwstats->fcoe_noddp = noddp;
6921 			hwstats->fcoe_noddp_ext_buff = noddp_ext_buff;
6922 		}
6923 #endif /* IXGBE_FCOE */
6924 		break;
6925 	default:
6926 		break;
6927 	}
6928 	bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
6929 	hwstats->bprc += bprc;
6930 	hwstats->mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
6931 	if (hw->mac.type == ixgbe_mac_82598EB)
6932 		hwstats->mprc -= bprc;
6933 	hwstats->roc += IXGBE_READ_REG(hw, IXGBE_ROC);
6934 	hwstats->prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
6935 	hwstats->prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
6936 	hwstats->prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
6937 	hwstats->prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
6938 	hwstats->prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
6939 	hwstats->prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
6940 	hwstats->rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
6941 	lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
6942 	hwstats->lxontxc += lxon;
6943 	lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
6944 	hwstats->lxofftxc += lxoff;
6945 	hwstats->gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
6946 	hwstats->mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
6947 	/*
6948 	 * 82598 errata - tx of flow control packets is included in tx counters
6949 	 */
6950 	xon_off_tot = lxon + lxoff;
6951 	hwstats->gptc -= xon_off_tot;
6952 	hwstats->mptc -= xon_off_tot;
6953 	hwstats->gotc -= (xon_off_tot * (ETH_ZLEN + ETH_FCS_LEN));
6954 	hwstats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
6955 	hwstats->rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
6956 	hwstats->rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
6957 	hwstats->tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
6958 	hwstats->ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
6959 	hwstats->ptc64 -= xon_off_tot;
6960 	hwstats->ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
6961 	hwstats->ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
6962 	hwstats->ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
6963 	hwstats->ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
6964 	hwstats->ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
6965 	hwstats->bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
6966 
6967 	/* Fill out the OS statistics structure */
6968 	netdev->stats.multicast = hwstats->mprc;
6969 
6970 	/* Rx Errors */
6971 	netdev->stats.rx_errors = hwstats->crcerrs + hwstats->rlec;
6972 	netdev->stats.rx_dropped = 0;
6973 	netdev->stats.rx_length_errors = hwstats->rlec;
6974 	netdev->stats.rx_crc_errors = hwstats->crcerrs;
6975 	netdev->stats.rx_missed_errors = total_mpc;
6976 }
6977 
6978 /**
6979  * ixgbe_fdir_reinit_subtask - worker thread to reinit FDIR filter table
6980  * @adapter: pointer to the device adapter structure
6981  **/
6982 static void ixgbe_fdir_reinit_subtask(struct ixgbe_adapter *adapter)
6983 {
6984 	struct ixgbe_hw *hw = &adapter->hw;
6985 	int i;
6986 
6987 	if (!(adapter->flags2 & IXGBE_FLAG2_FDIR_REQUIRES_REINIT))
6988 		return;
6989 
6990 	adapter->flags2 &= ~IXGBE_FLAG2_FDIR_REQUIRES_REINIT;
6991 
6992 	/* if interface is down do nothing */
6993 	if (test_bit(__IXGBE_DOWN, &adapter->state))
6994 		return;
6995 
6996 	/* do nothing if we are not using signature filters */
6997 	if (!(adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE))
6998 		return;
6999 
7000 	adapter->fdir_overflow++;
7001 
7002 	if (ixgbe_reinit_fdir_tables_82599(hw) == 0) {
7003 		for (i = 0; i < adapter->num_tx_queues; i++)
7004 			set_bit(__IXGBE_TX_FDIR_INIT_DONE,
7005 				&(adapter->tx_ring[i]->state));
7006 		for (i = 0; i < adapter->num_xdp_queues; i++)
7007 			set_bit(__IXGBE_TX_FDIR_INIT_DONE,
7008 				&adapter->xdp_ring[i]->state);
7009 		/* re-enable flow director interrupts */
7010 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_FLOW_DIR);
7011 	} else {
7012 		e_err(probe, "failed to finish FDIR re-initialization, "
7013 		      "ignored adding FDIR ATR filters\n");
7014 	}
7015 }
7016 
7017 /**
7018  * ixgbe_check_hang_subtask - check for hung queues and dropped interrupts
7019  * @adapter: pointer to the device adapter structure
7020  *
7021  * This function serves two purposes.  First it strobes the interrupt lines
7022  * in order to make certain interrupts are occurring.  Secondly it sets the
7023  * bits needed to check for TX hangs.  As a result we should immediately
7024  * determine if a hang has occurred.
7025  */
7026 static void ixgbe_check_hang_subtask(struct ixgbe_adapter *adapter)
7027 {
7028 	struct ixgbe_hw *hw = &adapter->hw;
7029 	u64 eics = 0;
7030 	int i;
7031 
7032 	/* If we're down, removing or resetting, just bail */
7033 	if (test_bit(__IXGBE_DOWN, &adapter->state) ||
7034 	    test_bit(__IXGBE_REMOVING, &adapter->state) ||
7035 	    test_bit(__IXGBE_RESETTING, &adapter->state))
7036 		return;
7037 
7038 	/* Force detection of hung controller */
7039 	if (netif_carrier_ok(adapter->netdev)) {
7040 		for (i = 0; i < adapter->num_tx_queues; i++)
7041 			set_check_for_tx_hang(adapter->tx_ring[i]);
7042 		for (i = 0; i < adapter->num_xdp_queues; i++)
7043 			set_check_for_tx_hang(adapter->xdp_ring[i]);
7044 	}
7045 
7046 	if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
7047 		/*
7048 		 * for legacy and MSI interrupts don't set any bits
7049 		 * that are enabled for EIAM, because this operation
7050 		 * would set *both* EIMS and EICS for any bit in EIAM
7051 		 */
7052 		IXGBE_WRITE_REG(hw, IXGBE_EICS,
7053 			(IXGBE_EICS_TCP_TIMER | IXGBE_EICS_OTHER));
7054 	} else {
7055 		/* get one bit for every active tx/rx interrupt vector */
7056 		for (i = 0; i < adapter->num_q_vectors; i++) {
7057 			struct ixgbe_q_vector *qv = adapter->q_vector[i];
7058 			if (qv->rx.ring || qv->tx.ring)
7059 				eics |= BIT_ULL(i);
7060 		}
7061 	}
7062 
7063 	/* Cause software interrupt to ensure rings are cleaned */
7064 	ixgbe_irq_rearm_queues(adapter, eics);
7065 }
7066 
7067 /**
7068  * ixgbe_watchdog_update_link - update the link status
7069  * @adapter: pointer to the device adapter structure
7070  * @link_speed: pointer to a u32 to store the link_speed
7071  **/
7072 static void ixgbe_watchdog_update_link(struct ixgbe_adapter *adapter)
7073 {
7074 	struct ixgbe_hw *hw = &adapter->hw;
7075 	u32 link_speed = adapter->link_speed;
7076 	bool link_up = adapter->link_up;
7077 	bool pfc_en = adapter->dcb_cfg.pfc_mode_enable;
7078 
7079 	if (!(adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE))
7080 		return;
7081 
7082 	if (hw->mac.ops.check_link) {
7083 		hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
7084 	} else {
7085 		/* always assume link is up, if no check link function */
7086 		link_speed = IXGBE_LINK_SPEED_10GB_FULL;
7087 		link_up = true;
7088 	}
7089 
7090 	if (adapter->ixgbe_ieee_pfc)
7091 		pfc_en |= !!(adapter->ixgbe_ieee_pfc->pfc_en);
7092 
7093 	if (link_up && !((adapter->flags & IXGBE_FLAG_DCB_ENABLED) && pfc_en)) {
7094 		hw->mac.ops.fc_enable(hw);
7095 		ixgbe_set_rx_drop_en(adapter);
7096 	}
7097 
7098 	if (link_up ||
7099 	    time_after(jiffies, (adapter->link_check_timeout +
7100 				 IXGBE_TRY_LINK_TIMEOUT))) {
7101 		adapter->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
7102 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMC_LSC);
7103 		IXGBE_WRITE_FLUSH(hw);
7104 	}
7105 
7106 	adapter->link_up = link_up;
7107 	adapter->link_speed = link_speed;
7108 }
7109 
7110 static void ixgbe_update_default_up(struct ixgbe_adapter *adapter)
7111 {
7112 #ifdef CONFIG_IXGBE_DCB
7113 	struct net_device *netdev = adapter->netdev;
7114 	struct dcb_app app = {
7115 			      .selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
7116 			      .protocol = 0,
7117 			     };
7118 	u8 up = 0;
7119 
7120 	if (adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE)
7121 		up = dcb_ieee_getapp_mask(netdev, &app);
7122 
7123 	adapter->default_up = (up > 1) ? (ffs(up) - 1) : 0;
7124 #endif
7125 }
7126 
7127 static int ixgbe_enable_macvlan(struct net_device *upper, void *data)
7128 {
7129 	if (netif_is_macvlan(upper)) {
7130 		struct macvlan_dev *vlan = netdev_priv(upper);
7131 
7132 		if (vlan->fwd_priv)
7133 			netif_tx_wake_all_queues(upper);
7134 	}
7135 
7136 	return 0;
7137 }
7138 
7139 /**
7140  * ixgbe_watchdog_link_is_up - update netif_carrier status and
7141  *                             print link up message
7142  * @adapter: pointer to the device adapter structure
7143  **/
7144 static void ixgbe_watchdog_link_is_up(struct ixgbe_adapter *adapter)
7145 {
7146 	struct net_device *netdev = adapter->netdev;
7147 	struct ixgbe_hw *hw = &adapter->hw;
7148 	u32 link_speed = adapter->link_speed;
7149 	const char *speed_str;
7150 	bool flow_rx, flow_tx;
7151 
7152 	/* only continue if link was previously down */
7153 	if (netif_carrier_ok(netdev))
7154 		return;
7155 
7156 	adapter->flags2 &= ~IXGBE_FLAG2_SEARCH_FOR_SFP;
7157 
7158 	switch (hw->mac.type) {
7159 	case ixgbe_mac_82598EB: {
7160 		u32 frctl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
7161 		u32 rmcs = IXGBE_READ_REG(hw, IXGBE_RMCS);
7162 		flow_rx = !!(frctl & IXGBE_FCTRL_RFCE);
7163 		flow_tx = !!(rmcs & IXGBE_RMCS_TFCE_802_3X);
7164 	}
7165 		break;
7166 	case ixgbe_mac_X540:
7167 	case ixgbe_mac_X550:
7168 	case ixgbe_mac_X550EM_x:
7169 	case ixgbe_mac_x550em_a:
7170 	case ixgbe_mac_82599EB: {
7171 		u32 mflcn = IXGBE_READ_REG(hw, IXGBE_MFLCN);
7172 		u32 fccfg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
7173 		flow_rx = !!(mflcn & IXGBE_MFLCN_RFCE);
7174 		flow_tx = !!(fccfg & IXGBE_FCCFG_TFCE_802_3X);
7175 	}
7176 		break;
7177 	default:
7178 		flow_tx = false;
7179 		flow_rx = false;
7180 		break;
7181 	}
7182 
7183 	adapter->last_rx_ptp_check = jiffies;
7184 
7185 	if (test_bit(__IXGBE_PTP_RUNNING, &adapter->state))
7186 		ixgbe_ptp_start_cyclecounter(adapter);
7187 
7188 	switch (link_speed) {
7189 	case IXGBE_LINK_SPEED_10GB_FULL:
7190 		speed_str = "10 Gbps";
7191 		break;
7192 	case IXGBE_LINK_SPEED_2_5GB_FULL:
7193 		speed_str = "2.5 Gbps";
7194 		break;
7195 	case IXGBE_LINK_SPEED_1GB_FULL:
7196 		speed_str = "1 Gbps";
7197 		break;
7198 	case IXGBE_LINK_SPEED_100_FULL:
7199 		speed_str = "100 Mbps";
7200 		break;
7201 	case IXGBE_LINK_SPEED_10_FULL:
7202 		speed_str = "10 Mbps";
7203 		break;
7204 	default:
7205 		speed_str = "unknown speed";
7206 		break;
7207 	}
7208 	e_info(drv, "NIC Link is Up %s, Flow Control: %s\n", speed_str,
7209 	       ((flow_rx && flow_tx) ? "RX/TX" :
7210 	       (flow_rx ? "RX" :
7211 	       (flow_tx ? "TX" : "None"))));
7212 
7213 	netif_carrier_on(netdev);
7214 	ixgbe_check_vf_rate_limit(adapter);
7215 
7216 	/* enable transmits */
7217 	netif_tx_wake_all_queues(adapter->netdev);
7218 
7219 	/* enable any upper devices */
7220 	rtnl_lock();
7221 	netdev_walk_all_upper_dev_rcu(adapter->netdev,
7222 				      ixgbe_enable_macvlan, NULL);
7223 	rtnl_unlock();
7224 
7225 	/* update the default user priority for VFs */
7226 	ixgbe_update_default_up(adapter);
7227 
7228 	/* ping all the active vfs to let them know link has changed */
7229 	ixgbe_ping_all_vfs(adapter);
7230 }
7231 
7232 /**
7233  * ixgbe_watchdog_link_is_down - update netif_carrier status and
7234  *                               print link down message
7235  * @adapter: pointer to the adapter structure
7236  **/
7237 static void ixgbe_watchdog_link_is_down(struct ixgbe_adapter *adapter)
7238 {
7239 	struct net_device *netdev = adapter->netdev;
7240 	struct ixgbe_hw *hw = &adapter->hw;
7241 
7242 	adapter->link_up = false;
7243 	adapter->link_speed = 0;
7244 
7245 	/* only continue if link was up previously */
7246 	if (!netif_carrier_ok(netdev))
7247 		return;
7248 
7249 	/* poll for SFP+ cable when link is down */
7250 	if (ixgbe_is_sfp(hw) && hw->mac.type == ixgbe_mac_82598EB)
7251 		adapter->flags2 |= IXGBE_FLAG2_SEARCH_FOR_SFP;
7252 
7253 	if (test_bit(__IXGBE_PTP_RUNNING, &adapter->state))
7254 		ixgbe_ptp_start_cyclecounter(adapter);
7255 
7256 	e_info(drv, "NIC Link is Down\n");
7257 	netif_carrier_off(netdev);
7258 
7259 	/* ping all the active vfs to let them know link has changed */
7260 	ixgbe_ping_all_vfs(adapter);
7261 }
7262 
7263 static bool ixgbe_ring_tx_pending(struct ixgbe_adapter *adapter)
7264 {
7265 	int i;
7266 
7267 	for (i = 0; i < adapter->num_tx_queues; i++) {
7268 		struct ixgbe_ring *tx_ring = adapter->tx_ring[i];
7269 
7270 		if (tx_ring->next_to_use != tx_ring->next_to_clean)
7271 			return true;
7272 	}
7273 
7274 	for (i = 0; i < adapter->num_xdp_queues; i++) {
7275 		struct ixgbe_ring *ring = adapter->xdp_ring[i];
7276 
7277 		if (ring->next_to_use != ring->next_to_clean)
7278 			return true;
7279 	}
7280 
7281 	return false;
7282 }
7283 
7284 static bool ixgbe_vf_tx_pending(struct ixgbe_adapter *adapter)
7285 {
7286 	struct ixgbe_hw *hw = &adapter->hw;
7287 	struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
7288 	u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
7289 
7290 	int i, j;
7291 
7292 	if (!adapter->num_vfs)
7293 		return false;
7294 
7295 	/* resetting the PF is only needed for MAC before X550 */
7296 	if (hw->mac.type >= ixgbe_mac_X550)
7297 		return false;
7298 
7299 	for (i = 0; i < adapter->num_vfs; i++) {
7300 		for (j = 0; j < q_per_pool; j++) {
7301 			u32 h, t;
7302 
7303 			h = IXGBE_READ_REG(hw, IXGBE_PVFTDHN(q_per_pool, i, j));
7304 			t = IXGBE_READ_REG(hw, IXGBE_PVFTDTN(q_per_pool, i, j));
7305 
7306 			if (h != t)
7307 				return true;
7308 		}
7309 	}
7310 
7311 	return false;
7312 }
7313 
7314 /**
7315  * ixgbe_watchdog_flush_tx - flush queues on link down
7316  * @adapter: pointer to the device adapter structure
7317  **/
7318 static void ixgbe_watchdog_flush_tx(struct ixgbe_adapter *adapter)
7319 {
7320 	if (!netif_carrier_ok(adapter->netdev)) {
7321 		if (ixgbe_ring_tx_pending(adapter) ||
7322 		    ixgbe_vf_tx_pending(adapter)) {
7323 			/* We've lost link, so the controller stops DMA,
7324 			 * but we've got queued Tx work that's never going
7325 			 * to get done, so reset controller to flush Tx.
7326 			 * (Do the reset outside of interrupt context).
7327 			 */
7328 			e_warn(drv, "initiating reset to clear Tx work after link loss\n");
7329 			set_bit(__IXGBE_RESET_REQUESTED, &adapter->state);
7330 		}
7331 	}
7332 }
7333 
7334 #ifdef CONFIG_PCI_IOV
7335 static void ixgbe_check_for_bad_vf(struct ixgbe_adapter *adapter)
7336 {
7337 	struct ixgbe_hw *hw = &adapter->hw;
7338 	struct pci_dev *pdev = adapter->pdev;
7339 	unsigned int vf;
7340 	u32 gpc;
7341 
7342 	if (!(netif_carrier_ok(adapter->netdev)))
7343 		return;
7344 
7345 	gpc = IXGBE_READ_REG(hw, IXGBE_TXDGPC);
7346 	if (gpc) /* If incrementing then no need for the check below */
7347 		return;
7348 	/* Check to see if a bad DMA write target from an errant or
7349 	 * malicious VF has caused a PCIe error.  If so then we can
7350 	 * issue a VFLR to the offending VF(s) and then resume without
7351 	 * requesting a full slot reset.
7352 	 */
7353 
7354 	if (!pdev)
7355 		return;
7356 
7357 	/* check status reg for all VFs owned by this PF */
7358 	for (vf = 0; vf < adapter->num_vfs; ++vf) {
7359 		struct pci_dev *vfdev = adapter->vfinfo[vf].vfdev;
7360 		u16 status_reg;
7361 
7362 		if (!vfdev)
7363 			continue;
7364 		pci_read_config_word(vfdev, PCI_STATUS, &status_reg);
7365 		if (status_reg != IXGBE_FAILED_READ_CFG_WORD &&
7366 		    status_reg & PCI_STATUS_REC_MASTER_ABORT)
7367 			pcie_flr(vfdev);
7368 	}
7369 }
7370 
7371 static void ixgbe_spoof_check(struct ixgbe_adapter *adapter)
7372 {
7373 	u32 ssvpc;
7374 
7375 	/* Do not perform spoof check for 82598 or if not in IOV mode */
7376 	if (adapter->hw.mac.type == ixgbe_mac_82598EB ||
7377 	    adapter->num_vfs == 0)
7378 		return;
7379 
7380 	ssvpc = IXGBE_READ_REG(&adapter->hw, IXGBE_SSVPC);
7381 
7382 	/*
7383 	 * ssvpc register is cleared on read, if zero then no
7384 	 * spoofed packets in the last interval.
7385 	 */
7386 	if (!ssvpc)
7387 		return;
7388 
7389 	e_warn(drv, "%u Spoofed packets detected\n", ssvpc);
7390 }
7391 #else
7392 static void ixgbe_spoof_check(struct ixgbe_adapter __always_unused *adapter)
7393 {
7394 }
7395 
7396 static void
7397 ixgbe_check_for_bad_vf(struct ixgbe_adapter __always_unused *adapter)
7398 {
7399 }
7400 #endif /* CONFIG_PCI_IOV */
7401 
7402 
7403 /**
7404  * ixgbe_watchdog_subtask - check and bring link up
7405  * @adapter: pointer to the device adapter structure
7406  **/
7407 static void ixgbe_watchdog_subtask(struct ixgbe_adapter *adapter)
7408 {
7409 	/* if interface is down, removing or resetting, do nothing */
7410 	if (test_bit(__IXGBE_DOWN, &adapter->state) ||
7411 	    test_bit(__IXGBE_REMOVING, &adapter->state) ||
7412 	    test_bit(__IXGBE_RESETTING, &adapter->state))
7413 		return;
7414 
7415 	ixgbe_watchdog_update_link(adapter);
7416 
7417 	if (adapter->link_up)
7418 		ixgbe_watchdog_link_is_up(adapter);
7419 	else
7420 		ixgbe_watchdog_link_is_down(adapter);
7421 
7422 	ixgbe_check_for_bad_vf(adapter);
7423 	ixgbe_spoof_check(adapter);
7424 	ixgbe_update_stats(adapter);
7425 
7426 	ixgbe_watchdog_flush_tx(adapter);
7427 }
7428 
7429 /**
7430  * ixgbe_sfp_detection_subtask - poll for SFP+ cable
7431  * @adapter: the ixgbe adapter structure
7432  **/
7433 static void ixgbe_sfp_detection_subtask(struct ixgbe_adapter *adapter)
7434 {
7435 	struct ixgbe_hw *hw = &adapter->hw;
7436 	s32 err;
7437 
7438 	/* not searching for SFP so there is nothing to do here */
7439 	if (!(adapter->flags2 & IXGBE_FLAG2_SEARCH_FOR_SFP) &&
7440 	    !(adapter->flags2 & IXGBE_FLAG2_SFP_NEEDS_RESET))
7441 		return;
7442 
7443 	if (adapter->sfp_poll_time &&
7444 	    time_after(adapter->sfp_poll_time, jiffies))
7445 		return; /* If not yet time to poll for SFP */
7446 
7447 	/* someone else is in init, wait until next service event */
7448 	if (test_and_set_bit(__IXGBE_IN_SFP_INIT, &adapter->state))
7449 		return;
7450 
7451 	adapter->sfp_poll_time = jiffies + IXGBE_SFP_POLL_JIFFIES - 1;
7452 
7453 	err = hw->phy.ops.identify_sfp(hw);
7454 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED)
7455 		goto sfp_out;
7456 
7457 	if (err == IXGBE_ERR_SFP_NOT_PRESENT) {
7458 		/* If no cable is present, then we need to reset
7459 		 * the next time we find a good cable. */
7460 		adapter->flags2 |= IXGBE_FLAG2_SFP_NEEDS_RESET;
7461 	}
7462 
7463 	/* exit on error */
7464 	if (err)
7465 		goto sfp_out;
7466 
7467 	/* exit if reset not needed */
7468 	if (!(adapter->flags2 & IXGBE_FLAG2_SFP_NEEDS_RESET))
7469 		goto sfp_out;
7470 
7471 	adapter->flags2 &= ~IXGBE_FLAG2_SFP_NEEDS_RESET;
7472 
7473 	/*
7474 	 * A module may be identified correctly, but the EEPROM may not have
7475 	 * support for that module.  setup_sfp() will fail in that case, so
7476 	 * we should not allow that module to load.
7477 	 */
7478 	if (hw->mac.type == ixgbe_mac_82598EB)
7479 		err = hw->phy.ops.reset(hw);
7480 	else
7481 		err = hw->mac.ops.setup_sfp(hw);
7482 
7483 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED)
7484 		goto sfp_out;
7485 
7486 	adapter->flags |= IXGBE_FLAG_NEED_LINK_CONFIG;
7487 	e_info(probe, "detected SFP+: %d\n", hw->phy.sfp_type);
7488 
7489 sfp_out:
7490 	clear_bit(__IXGBE_IN_SFP_INIT, &adapter->state);
7491 
7492 	if ((err == IXGBE_ERR_SFP_NOT_SUPPORTED) &&
7493 	    (adapter->netdev->reg_state == NETREG_REGISTERED)) {
7494 		e_dev_err("failed to initialize because an unsupported "
7495 			  "SFP+ module type was detected.\n");
7496 		e_dev_err("Reload the driver after installing a "
7497 			  "supported module.\n");
7498 		unregister_netdev(adapter->netdev);
7499 	}
7500 }
7501 
7502 /**
7503  * ixgbe_sfp_link_config_subtask - set up link SFP after module install
7504  * @adapter: the ixgbe adapter structure
7505  **/
7506 static void ixgbe_sfp_link_config_subtask(struct ixgbe_adapter *adapter)
7507 {
7508 	struct ixgbe_hw *hw = &adapter->hw;
7509 	u32 speed;
7510 	bool autoneg = false;
7511 
7512 	if (!(adapter->flags & IXGBE_FLAG_NEED_LINK_CONFIG))
7513 		return;
7514 
7515 	/* someone else is in init, wait until next service event */
7516 	if (test_and_set_bit(__IXGBE_IN_SFP_INIT, &adapter->state))
7517 		return;
7518 
7519 	adapter->flags &= ~IXGBE_FLAG_NEED_LINK_CONFIG;
7520 
7521 	speed = hw->phy.autoneg_advertised;
7522 	if ((!speed) && (hw->mac.ops.get_link_capabilities)) {
7523 		hw->mac.ops.get_link_capabilities(hw, &speed, &autoneg);
7524 
7525 		/* setup the highest link when no autoneg */
7526 		if (!autoneg) {
7527 			if (speed & IXGBE_LINK_SPEED_10GB_FULL)
7528 				speed = IXGBE_LINK_SPEED_10GB_FULL;
7529 		}
7530 	}
7531 
7532 	if (hw->mac.ops.setup_link)
7533 		hw->mac.ops.setup_link(hw, speed, true);
7534 
7535 	adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
7536 	adapter->link_check_timeout = jiffies;
7537 	clear_bit(__IXGBE_IN_SFP_INIT, &adapter->state);
7538 }
7539 
7540 /**
7541  * ixgbe_service_timer - Timer Call-back
7542  * @data: pointer to adapter cast into an unsigned long
7543  **/
7544 static void ixgbe_service_timer(unsigned long data)
7545 {
7546 	struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
7547 	unsigned long next_event_offset;
7548 
7549 	/* poll faster when waiting for link */
7550 	if (adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE)
7551 		next_event_offset = HZ / 10;
7552 	else
7553 		next_event_offset = HZ * 2;
7554 
7555 	/* Reset the timer */
7556 	mod_timer(&adapter->service_timer, next_event_offset + jiffies);
7557 
7558 	ixgbe_service_event_schedule(adapter);
7559 }
7560 
7561 static void ixgbe_phy_interrupt_subtask(struct ixgbe_adapter *adapter)
7562 {
7563 	struct ixgbe_hw *hw = &adapter->hw;
7564 	u32 status;
7565 
7566 	if (!(adapter->flags2 & IXGBE_FLAG2_PHY_INTERRUPT))
7567 		return;
7568 
7569 	adapter->flags2 &= ~IXGBE_FLAG2_PHY_INTERRUPT;
7570 
7571 	if (!hw->phy.ops.handle_lasi)
7572 		return;
7573 
7574 	status = hw->phy.ops.handle_lasi(&adapter->hw);
7575 	if (status != IXGBE_ERR_OVERTEMP)
7576 		return;
7577 
7578 	e_crit(drv, "%s\n", ixgbe_overheat_msg);
7579 }
7580 
7581 static void ixgbe_reset_subtask(struct ixgbe_adapter *adapter)
7582 {
7583 	if (!test_and_clear_bit(__IXGBE_RESET_REQUESTED, &adapter->state))
7584 		return;
7585 
7586 	/* If we're already down, removing or resetting, just bail */
7587 	if (test_bit(__IXGBE_DOWN, &adapter->state) ||
7588 	    test_bit(__IXGBE_REMOVING, &adapter->state) ||
7589 	    test_bit(__IXGBE_RESETTING, &adapter->state))
7590 		return;
7591 
7592 	ixgbe_dump(adapter);
7593 	netdev_err(adapter->netdev, "Reset adapter\n");
7594 	adapter->tx_timeout_count++;
7595 
7596 	rtnl_lock();
7597 	ixgbe_reinit_locked(adapter);
7598 	rtnl_unlock();
7599 }
7600 
7601 /**
7602  * ixgbe_service_task - manages and runs subtasks
7603  * @work: pointer to work_struct containing our data
7604  **/
7605 static void ixgbe_service_task(struct work_struct *work)
7606 {
7607 	struct ixgbe_adapter *adapter = container_of(work,
7608 						     struct ixgbe_adapter,
7609 						     service_task);
7610 	if (ixgbe_removed(adapter->hw.hw_addr)) {
7611 		if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
7612 			rtnl_lock();
7613 			ixgbe_down(adapter);
7614 			rtnl_unlock();
7615 		}
7616 		ixgbe_service_event_complete(adapter);
7617 		return;
7618 	}
7619 	if (adapter->flags2 & IXGBE_FLAG2_UDP_TUN_REREG_NEEDED) {
7620 		rtnl_lock();
7621 		adapter->flags2 &= ~IXGBE_FLAG2_UDP_TUN_REREG_NEEDED;
7622 		udp_tunnel_get_rx_info(adapter->netdev);
7623 		rtnl_unlock();
7624 	}
7625 	ixgbe_reset_subtask(adapter);
7626 	ixgbe_phy_interrupt_subtask(adapter);
7627 	ixgbe_sfp_detection_subtask(adapter);
7628 	ixgbe_sfp_link_config_subtask(adapter);
7629 	ixgbe_check_overtemp_subtask(adapter);
7630 	ixgbe_watchdog_subtask(adapter);
7631 	ixgbe_fdir_reinit_subtask(adapter);
7632 	ixgbe_check_hang_subtask(adapter);
7633 
7634 	if (test_bit(__IXGBE_PTP_RUNNING, &adapter->state)) {
7635 		ixgbe_ptp_overflow_check(adapter);
7636 		ixgbe_ptp_rx_hang(adapter);
7637 	}
7638 
7639 	ixgbe_service_event_complete(adapter);
7640 }
7641 
7642 static int ixgbe_tso(struct ixgbe_ring *tx_ring,
7643 		     struct ixgbe_tx_buffer *first,
7644 		     u8 *hdr_len)
7645 {
7646 	u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
7647 	struct sk_buff *skb = first->skb;
7648 	union {
7649 		struct iphdr *v4;
7650 		struct ipv6hdr *v6;
7651 		unsigned char *hdr;
7652 	} ip;
7653 	union {
7654 		struct tcphdr *tcp;
7655 		unsigned char *hdr;
7656 	} l4;
7657 	u32 paylen, l4_offset;
7658 	int err;
7659 
7660 	if (skb->ip_summed != CHECKSUM_PARTIAL)
7661 		return 0;
7662 
7663 	if (!skb_is_gso(skb))
7664 		return 0;
7665 
7666 	err = skb_cow_head(skb, 0);
7667 	if (err < 0)
7668 		return err;
7669 
7670 	ip.hdr = skb_network_header(skb);
7671 	l4.hdr = skb_checksum_start(skb);
7672 
7673 	/* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
7674 	type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
7675 
7676 	/* initialize outer IP header fields */
7677 	if (ip.v4->version == 4) {
7678 		unsigned char *csum_start = skb_checksum_start(skb);
7679 		unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
7680 
7681 		/* IP header will have to cancel out any data that
7682 		 * is not a part of the outer IP header
7683 		 */
7684 		ip.v4->check = csum_fold(csum_partial(trans_start,
7685 						      csum_start - trans_start,
7686 						      0));
7687 		type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
7688 
7689 		ip.v4->tot_len = 0;
7690 		first->tx_flags |= IXGBE_TX_FLAGS_TSO |
7691 				   IXGBE_TX_FLAGS_CSUM |
7692 				   IXGBE_TX_FLAGS_IPV4;
7693 	} else {
7694 		ip.v6->payload_len = 0;
7695 		first->tx_flags |= IXGBE_TX_FLAGS_TSO |
7696 				   IXGBE_TX_FLAGS_CSUM;
7697 	}
7698 
7699 	/* determine offset of inner transport header */
7700 	l4_offset = l4.hdr - skb->data;
7701 
7702 	/* compute length of segmentation header */
7703 	*hdr_len = (l4.tcp->doff * 4) + l4_offset;
7704 
7705 	/* remove payload length from inner checksum */
7706 	paylen = skb->len - l4_offset;
7707 	csum_replace_by_diff(&l4.tcp->check, htonl(paylen));
7708 
7709 	/* update gso size and bytecount with header size */
7710 	first->gso_segs = skb_shinfo(skb)->gso_segs;
7711 	first->bytecount += (first->gso_segs - 1) * *hdr_len;
7712 
7713 	/* mss_l4len_id: use 0 as index for TSO */
7714 	mss_l4len_idx = (*hdr_len - l4_offset) << IXGBE_ADVTXD_L4LEN_SHIFT;
7715 	mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
7716 
7717 	/* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
7718 	vlan_macip_lens = l4.hdr - ip.hdr;
7719 	vlan_macip_lens |= (ip.hdr - skb->data) << IXGBE_ADVTXD_MACLEN_SHIFT;
7720 	vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
7721 
7722 	ixgbe_tx_ctxtdesc(tx_ring, vlan_macip_lens, 0, type_tucmd,
7723 			  mss_l4len_idx);
7724 
7725 	return 1;
7726 }
7727 
7728 static inline bool ixgbe_ipv6_csum_is_sctp(struct sk_buff *skb)
7729 {
7730 	unsigned int offset = 0;
7731 
7732 	ipv6_find_hdr(skb, &offset, IPPROTO_SCTP, NULL, NULL);
7733 
7734 	return offset == skb_checksum_start_offset(skb);
7735 }
7736 
7737 static void ixgbe_tx_csum(struct ixgbe_ring *tx_ring,
7738 			  struct ixgbe_tx_buffer *first)
7739 {
7740 	struct sk_buff *skb = first->skb;
7741 	u32 vlan_macip_lens = 0;
7742 	u32 type_tucmd = 0;
7743 
7744 	if (skb->ip_summed != CHECKSUM_PARTIAL) {
7745 csum_failed:
7746 		if (!(first->tx_flags & (IXGBE_TX_FLAGS_HW_VLAN |
7747 					 IXGBE_TX_FLAGS_CC)))
7748 			return;
7749 		goto no_csum;
7750 	}
7751 
7752 	switch (skb->csum_offset) {
7753 	case offsetof(struct tcphdr, check):
7754 		type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
7755 		/* fall through */
7756 	case offsetof(struct udphdr, check):
7757 		break;
7758 	case offsetof(struct sctphdr, checksum):
7759 		/* validate that this is actually an SCTP request */
7760 		if (((first->protocol == htons(ETH_P_IP)) &&
7761 		     (ip_hdr(skb)->protocol == IPPROTO_SCTP)) ||
7762 		    ((first->protocol == htons(ETH_P_IPV6)) &&
7763 		     ixgbe_ipv6_csum_is_sctp(skb))) {
7764 			type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_SCTP;
7765 			break;
7766 		}
7767 		/* fall through */
7768 	default:
7769 		skb_checksum_help(skb);
7770 		goto csum_failed;
7771 	}
7772 
7773 	/* update TX checksum flag */
7774 	first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
7775 	vlan_macip_lens = skb_checksum_start_offset(skb) -
7776 			  skb_network_offset(skb);
7777 no_csum:
7778 	/* vlan_macip_lens: MACLEN, VLAN tag */
7779 	vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
7780 	vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
7781 
7782 	ixgbe_tx_ctxtdesc(tx_ring, vlan_macip_lens, 0, type_tucmd, 0);
7783 }
7784 
7785 #define IXGBE_SET_FLAG(_input, _flag, _result) \
7786 	((_flag <= _result) ? \
7787 	 ((u32)(_input & _flag) * (_result / _flag)) : \
7788 	 ((u32)(_input & _flag) / (_flag / _result)))
7789 
7790 static u32 ixgbe_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
7791 {
7792 	/* set type for advanced descriptor with frame checksum insertion */
7793 	u32 cmd_type = IXGBE_ADVTXD_DTYP_DATA |
7794 		       IXGBE_ADVTXD_DCMD_DEXT |
7795 		       IXGBE_ADVTXD_DCMD_IFCS;
7796 
7797 	/* set HW vlan bit if vlan is present */
7798 	cmd_type |= IXGBE_SET_FLAG(tx_flags, IXGBE_TX_FLAGS_HW_VLAN,
7799 				   IXGBE_ADVTXD_DCMD_VLE);
7800 
7801 	/* set segmentation enable bits for TSO/FSO */
7802 	cmd_type |= IXGBE_SET_FLAG(tx_flags, IXGBE_TX_FLAGS_TSO,
7803 				   IXGBE_ADVTXD_DCMD_TSE);
7804 
7805 	/* set timestamp bit if present */
7806 	cmd_type |= IXGBE_SET_FLAG(tx_flags, IXGBE_TX_FLAGS_TSTAMP,
7807 				   IXGBE_ADVTXD_MAC_TSTAMP);
7808 
7809 	/* insert frame checksum */
7810 	cmd_type ^= IXGBE_SET_FLAG(skb->no_fcs, 1, IXGBE_ADVTXD_DCMD_IFCS);
7811 
7812 	return cmd_type;
7813 }
7814 
7815 static void ixgbe_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
7816 				   u32 tx_flags, unsigned int paylen)
7817 {
7818 	u32 olinfo_status = paylen << IXGBE_ADVTXD_PAYLEN_SHIFT;
7819 
7820 	/* enable L4 checksum for TSO and TX checksum offload */
7821 	olinfo_status |= IXGBE_SET_FLAG(tx_flags,
7822 					IXGBE_TX_FLAGS_CSUM,
7823 					IXGBE_ADVTXD_POPTS_TXSM);
7824 
7825 	/* enble IPv4 checksum for TSO */
7826 	olinfo_status |= IXGBE_SET_FLAG(tx_flags,
7827 					IXGBE_TX_FLAGS_IPV4,
7828 					IXGBE_ADVTXD_POPTS_IXSM);
7829 
7830 	/*
7831 	 * Check Context must be set if Tx switch is enabled, which it
7832 	 * always is for case where virtual functions are running
7833 	 */
7834 	olinfo_status |= IXGBE_SET_FLAG(tx_flags,
7835 					IXGBE_TX_FLAGS_CC,
7836 					IXGBE_ADVTXD_CC);
7837 
7838 	tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
7839 }
7840 
7841 static int __ixgbe_maybe_stop_tx(struct ixgbe_ring *tx_ring, u16 size)
7842 {
7843 	netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
7844 
7845 	/* Herbert's original patch had:
7846 	 *  smp_mb__after_netif_stop_queue();
7847 	 * but since that doesn't exist yet, just open code it.
7848 	 */
7849 	smp_mb();
7850 
7851 	/* We need to check again in a case another CPU has just
7852 	 * made room available.
7853 	 */
7854 	if (likely(ixgbe_desc_unused(tx_ring) < size))
7855 		return -EBUSY;
7856 
7857 	/* A reprieve! - use start_queue because it doesn't call schedule */
7858 	netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
7859 	++tx_ring->tx_stats.restart_queue;
7860 	return 0;
7861 }
7862 
7863 static inline int ixgbe_maybe_stop_tx(struct ixgbe_ring *tx_ring, u16 size)
7864 {
7865 	if (likely(ixgbe_desc_unused(tx_ring) >= size))
7866 		return 0;
7867 
7868 	return __ixgbe_maybe_stop_tx(tx_ring, size);
7869 }
7870 
7871 #define IXGBE_TXD_CMD (IXGBE_TXD_CMD_EOP | \
7872 		       IXGBE_TXD_CMD_RS)
7873 
7874 static void ixgbe_tx_map(struct ixgbe_ring *tx_ring,
7875 			 struct ixgbe_tx_buffer *first,
7876 			 const u8 hdr_len)
7877 {
7878 	struct sk_buff *skb = first->skb;
7879 	struct ixgbe_tx_buffer *tx_buffer;
7880 	union ixgbe_adv_tx_desc *tx_desc;
7881 	struct skb_frag_struct *frag;
7882 	dma_addr_t dma;
7883 	unsigned int data_len, size;
7884 	u32 tx_flags = first->tx_flags;
7885 	u32 cmd_type = ixgbe_tx_cmd_type(skb, tx_flags);
7886 	u16 i = tx_ring->next_to_use;
7887 
7888 	tx_desc = IXGBE_TX_DESC(tx_ring, i);
7889 
7890 	ixgbe_tx_olinfo_status(tx_desc, tx_flags, skb->len - hdr_len);
7891 
7892 	size = skb_headlen(skb);
7893 	data_len = skb->data_len;
7894 
7895 #ifdef IXGBE_FCOE
7896 	if (tx_flags & IXGBE_TX_FLAGS_FCOE) {
7897 		if (data_len < sizeof(struct fcoe_crc_eof)) {
7898 			size -= sizeof(struct fcoe_crc_eof) - data_len;
7899 			data_len = 0;
7900 		} else {
7901 			data_len -= sizeof(struct fcoe_crc_eof);
7902 		}
7903 	}
7904 
7905 #endif
7906 	dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
7907 
7908 	tx_buffer = first;
7909 
7910 	for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
7911 		if (dma_mapping_error(tx_ring->dev, dma))
7912 			goto dma_error;
7913 
7914 		/* record length, and DMA address */
7915 		dma_unmap_len_set(tx_buffer, len, size);
7916 		dma_unmap_addr_set(tx_buffer, dma, dma);
7917 
7918 		tx_desc->read.buffer_addr = cpu_to_le64(dma);
7919 
7920 		while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
7921 			tx_desc->read.cmd_type_len =
7922 				cpu_to_le32(cmd_type ^ IXGBE_MAX_DATA_PER_TXD);
7923 
7924 			i++;
7925 			tx_desc++;
7926 			if (i == tx_ring->count) {
7927 				tx_desc = IXGBE_TX_DESC(tx_ring, 0);
7928 				i = 0;
7929 			}
7930 			tx_desc->read.olinfo_status = 0;
7931 
7932 			dma += IXGBE_MAX_DATA_PER_TXD;
7933 			size -= IXGBE_MAX_DATA_PER_TXD;
7934 
7935 			tx_desc->read.buffer_addr = cpu_to_le64(dma);
7936 		}
7937 
7938 		if (likely(!data_len))
7939 			break;
7940 
7941 		tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
7942 
7943 		i++;
7944 		tx_desc++;
7945 		if (i == tx_ring->count) {
7946 			tx_desc = IXGBE_TX_DESC(tx_ring, 0);
7947 			i = 0;
7948 		}
7949 		tx_desc->read.olinfo_status = 0;
7950 
7951 #ifdef IXGBE_FCOE
7952 		size = min_t(unsigned int, data_len, skb_frag_size(frag));
7953 #else
7954 		size = skb_frag_size(frag);
7955 #endif
7956 		data_len -= size;
7957 
7958 		dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
7959 				       DMA_TO_DEVICE);
7960 
7961 		tx_buffer = &tx_ring->tx_buffer_info[i];
7962 	}
7963 
7964 	/* write last descriptor with RS and EOP bits */
7965 	cmd_type |= size | IXGBE_TXD_CMD;
7966 	tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
7967 
7968 	netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
7969 
7970 	/* set the timestamp */
7971 	first->time_stamp = jiffies;
7972 
7973 	/*
7974 	 * Force memory writes to complete before letting h/w know there
7975 	 * are new descriptors to fetch.  (Only applicable for weak-ordered
7976 	 * memory model archs, such as IA-64).
7977 	 *
7978 	 * We also need this memory barrier to make certain all of the
7979 	 * status bits have been updated before next_to_watch is written.
7980 	 */
7981 	wmb();
7982 
7983 	/* set next_to_watch value indicating a packet is present */
7984 	first->next_to_watch = tx_desc;
7985 
7986 	i++;
7987 	if (i == tx_ring->count)
7988 		i = 0;
7989 
7990 	tx_ring->next_to_use = i;
7991 
7992 	ixgbe_maybe_stop_tx(tx_ring, DESC_NEEDED);
7993 
7994 	if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
7995 		writel(i, tx_ring->tail);
7996 
7997 		/* we need this if more than one processor can write to our tail
7998 		 * at a time, it synchronizes IO on IA64/Altix systems
7999 		 */
8000 		mmiowb();
8001 	}
8002 
8003 	return;
8004 dma_error:
8005 	dev_err(tx_ring->dev, "TX DMA map failed\n");
8006 	tx_buffer = &tx_ring->tx_buffer_info[i];
8007 
8008 	/* clear dma mappings for failed tx_buffer_info map */
8009 	while (tx_buffer != first) {
8010 		if (dma_unmap_len(tx_buffer, len))
8011 			dma_unmap_page(tx_ring->dev,
8012 				       dma_unmap_addr(tx_buffer, dma),
8013 				       dma_unmap_len(tx_buffer, len),
8014 				       DMA_TO_DEVICE);
8015 		dma_unmap_len_set(tx_buffer, len, 0);
8016 
8017 		if (i--)
8018 			i += tx_ring->count;
8019 		tx_buffer = &tx_ring->tx_buffer_info[i];
8020 	}
8021 
8022 	if (dma_unmap_len(tx_buffer, len))
8023 		dma_unmap_single(tx_ring->dev,
8024 				 dma_unmap_addr(tx_buffer, dma),
8025 				 dma_unmap_len(tx_buffer, len),
8026 				 DMA_TO_DEVICE);
8027 	dma_unmap_len_set(tx_buffer, len, 0);
8028 
8029 	dev_kfree_skb_any(first->skb);
8030 	first->skb = NULL;
8031 
8032 	tx_ring->next_to_use = i;
8033 }
8034 
8035 static void ixgbe_atr(struct ixgbe_ring *ring,
8036 		      struct ixgbe_tx_buffer *first)
8037 {
8038 	struct ixgbe_q_vector *q_vector = ring->q_vector;
8039 	union ixgbe_atr_hash_dword input = { .dword = 0 };
8040 	union ixgbe_atr_hash_dword common = { .dword = 0 };
8041 	union {
8042 		unsigned char *network;
8043 		struct iphdr *ipv4;
8044 		struct ipv6hdr *ipv6;
8045 	} hdr;
8046 	struct tcphdr *th;
8047 	unsigned int hlen;
8048 	struct sk_buff *skb;
8049 	__be16 vlan_id;
8050 	int l4_proto;
8051 
8052 	/* if ring doesn't have a interrupt vector, cannot perform ATR */
8053 	if (!q_vector)
8054 		return;
8055 
8056 	/* do nothing if sampling is disabled */
8057 	if (!ring->atr_sample_rate)
8058 		return;
8059 
8060 	ring->atr_count++;
8061 
8062 	/* currently only IPv4/IPv6 with TCP is supported */
8063 	if ((first->protocol != htons(ETH_P_IP)) &&
8064 	    (first->protocol != htons(ETH_P_IPV6)))
8065 		return;
8066 
8067 	/* snag network header to get L4 type and address */
8068 	skb = first->skb;
8069 	hdr.network = skb_network_header(skb);
8070 	if (unlikely(hdr.network <= skb->data))
8071 		return;
8072 	if (skb->encapsulation &&
8073 	    first->protocol == htons(ETH_P_IP) &&
8074 	    hdr.ipv4->protocol == IPPROTO_UDP) {
8075 		struct ixgbe_adapter *adapter = q_vector->adapter;
8076 
8077 		if (unlikely(skb_tail_pointer(skb) < hdr.network +
8078 			     VXLAN_HEADROOM))
8079 			return;
8080 
8081 		/* verify the port is recognized as VXLAN */
8082 		if (adapter->vxlan_port &&
8083 		    udp_hdr(skb)->dest == adapter->vxlan_port)
8084 			hdr.network = skb_inner_network_header(skb);
8085 
8086 		if (adapter->geneve_port &&
8087 		    udp_hdr(skb)->dest == adapter->geneve_port)
8088 			hdr.network = skb_inner_network_header(skb);
8089 	}
8090 
8091 	/* Make sure we have at least [minimum IPv4 header + TCP]
8092 	 * or [IPv6 header] bytes
8093 	 */
8094 	if (unlikely(skb_tail_pointer(skb) < hdr.network + 40))
8095 		return;
8096 
8097 	/* Currently only IPv4/IPv6 with TCP is supported */
8098 	switch (hdr.ipv4->version) {
8099 	case IPVERSION:
8100 		/* access ihl as u8 to avoid unaligned access on ia64 */
8101 		hlen = (hdr.network[0] & 0x0F) << 2;
8102 		l4_proto = hdr.ipv4->protocol;
8103 		break;
8104 	case 6:
8105 		hlen = hdr.network - skb->data;
8106 		l4_proto = ipv6_find_hdr(skb, &hlen, IPPROTO_TCP, NULL, NULL);
8107 		hlen -= hdr.network - skb->data;
8108 		break;
8109 	default:
8110 		return;
8111 	}
8112 
8113 	if (l4_proto != IPPROTO_TCP)
8114 		return;
8115 
8116 	if (unlikely(skb_tail_pointer(skb) < hdr.network +
8117 		     hlen + sizeof(struct tcphdr)))
8118 		return;
8119 
8120 	th = (struct tcphdr *)(hdr.network + hlen);
8121 
8122 	/* skip this packet since the socket is closing */
8123 	if (th->fin)
8124 		return;
8125 
8126 	/* sample on all syn packets or once every atr sample count */
8127 	if (!th->syn && (ring->atr_count < ring->atr_sample_rate))
8128 		return;
8129 
8130 	/* reset sample count */
8131 	ring->atr_count = 0;
8132 
8133 	vlan_id = htons(first->tx_flags >> IXGBE_TX_FLAGS_VLAN_SHIFT);
8134 
8135 	/*
8136 	 * src and dst are inverted, think how the receiver sees them
8137 	 *
8138 	 * The input is broken into two sections, a non-compressed section
8139 	 * containing vm_pool, vlan_id, and flow_type.  The rest of the data
8140 	 * is XORed together and stored in the compressed dword.
8141 	 */
8142 	input.formatted.vlan_id = vlan_id;
8143 
8144 	/*
8145 	 * since src port and flex bytes occupy the same word XOR them together
8146 	 * and write the value to source port portion of compressed dword
8147 	 */
8148 	if (first->tx_flags & (IXGBE_TX_FLAGS_SW_VLAN | IXGBE_TX_FLAGS_HW_VLAN))
8149 		common.port.src ^= th->dest ^ htons(ETH_P_8021Q);
8150 	else
8151 		common.port.src ^= th->dest ^ first->protocol;
8152 	common.port.dst ^= th->source;
8153 
8154 	switch (hdr.ipv4->version) {
8155 	case IPVERSION:
8156 		input.formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV4;
8157 		common.ip ^= hdr.ipv4->saddr ^ hdr.ipv4->daddr;
8158 		break;
8159 	case 6:
8160 		input.formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV6;
8161 		common.ip ^= hdr.ipv6->saddr.s6_addr32[0] ^
8162 			     hdr.ipv6->saddr.s6_addr32[1] ^
8163 			     hdr.ipv6->saddr.s6_addr32[2] ^
8164 			     hdr.ipv6->saddr.s6_addr32[3] ^
8165 			     hdr.ipv6->daddr.s6_addr32[0] ^
8166 			     hdr.ipv6->daddr.s6_addr32[1] ^
8167 			     hdr.ipv6->daddr.s6_addr32[2] ^
8168 			     hdr.ipv6->daddr.s6_addr32[3];
8169 		break;
8170 	default:
8171 		break;
8172 	}
8173 
8174 	if (hdr.network != skb_network_header(skb))
8175 		input.formatted.flow_type |= IXGBE_ATR_L4TYPE_TUNNEL_MASK;
8176 
8177 	/* This assumes the Rx queue and Tx queue are bound to the same CPU */
8178 	ixgbe_fdir_add_signature_filter_82599(&q_vector->adapter->hw,
8179 					      input, common, ring->queue_index);
8180 }
8181 
8182 static u16 ixgbe_select_queue(struct net_device *dev, struct sk_buff *skb,
8183 			      void *accel_priv, select_queue_fallback_t fallback)
8184 {
8185 	struct ixgbe_fwd_adapter *fwd_adapter = accel_priv;
8186 #ifdef IXGBE_FCOE
8187 	struct ixgbe_adapter *adapter;
8188 	struct ixgbe_ring_feature *f;
8189 	int txq;
8190 #endif
8191 
8192 	if (fwd_adapter)
8193 		return skb->queue_mapping + fwd_adapter->tx_base_queue;
8194 
8195 #ifdef IXGBE_FCOE
8196 
8197 	/*
8198 	 * only execute the code below if protocol is FCoE
8199 	 * or FIP and we have FCoE enabled on the adapter
8200 	 */
8201 	switch (vlan_get_protocol(skb)) {
8202 	case htons(ETH_P_FCOE):
8203 	case htons(ETH_P_FIP):
8204 		adapter = netdev_priv(dev);
8205 
8206 		if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
8207 			break;
8208 	default:
8209 		return fallback(dev, skb);
8210 	}
8211 
8212 	f = &adapter->ring_feature[RING_F_FCOE];
8213 
8214 	txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) :
8215 					   smp_processor_id();
8216 
8217 	while (txq >= f->indices)
8218 		txq -= f->indices;
8219 
8220 	return txq + f->offset;
8221 #else
8222 	return fallback(dev, skb);
8223 #endif
8224 }
8225 
8226 static int ixgbe_xmit_xdp_ring(struct ixgbe_adapter *adapter,
8227 			       struct xdp_buff *xdp)
8228 {
8229 	struct ixgbe_ring *ring = adapter->xdp_ring[smp_processor_id()];
8230 	struct ixgbe_tx_buffer *tx_buffer;
8231 	union ixgbe_adv_tx_desc *tx_desc;
8232 	u32 len, cmd_type;
8233 	dma_addr_t dma;
8234 	u16 i;
8235 
8236 	len = xdp->data_end - xdp->data;
8237 
8238 	if (unlikely(!ixgbe_desc_unused(ring)))
8239 		return IXGBE_XDP_CONSUMED;
8240 
8241 	dma = dma_map_single(ring->dev, xdp->data, len, DMA_TO_DEVICE);
8242 	if (dma_mapping_error(ring->dev, dma))
8243 		return IXGBE_XDP_CONSUMED;
8244 
8245 	/* record the location of the first descriptor for this packet */
8246 	tx_buffer = &ring->tx_buffer_info[ring->next_to_use];
8247 	tx_buffer->bytecount = len;
8248 	tx_buffer->gso_segs = 1;
8249 	tx_buffer->protocol = 0;
8250 
8251 	i = ring->next_to_use;
8252 	tx_desc = IXGBE_TX_DESC(ring, i);
8253 
8254 	dma_unmap_len_set(tx_buffer, len, len);
8255 	dma_unmap_addr_set(tx_buffer, dma, dma);
8256 	tx_buffer->data = xdp->data;
8257 	tx_desc->read.buffer_addr = cpu_to_le64(dma);
8258 
8259 	/* put descriptor type bits */
8260 	cmd_type = IXGBE_ADVTXD_DTYP_DATA |
8261 		   IXGBE_ADVTXD_DCMD_DEXT |
8262 		   IXGBE_ADVTXD_DCMD_IFCS;
8263 	cmd_type |= len | IXGBE_TXD_CMD;
8264 	tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
8265 	tx_desc->read.olinfo_status =
8266 		cpu_to_le32(len << IXGBE_ADVTXD_PAYLEN_SHIFT);
8267 
8268 	/* Avoid any potential race with xdp_xmit and cleanup */
8269 	smp_wmb();
8270 
8271 	/* set next_to_watch value indicating a packet is present */
8272 	i++;
8273 	if (i == ring->count)
8274 		i = 0;
8275 
8276 	tx_buffer->next_to_watch = tx_desc;
8277 	ring->next_to_use = i;
8278 
8279 	return IXGBE_XDP_TX;
8280 }
8281 
8282 netdev_tx_t ixgbe_xmit_frame_ring(struct sk_buff *skb,
8283 			  struct ixgbe_adapter *adapter,
8284 			  struct ixgbe_ring *tx_ring)
8285 {
8286 	struct ixgbe_tx_buffer *first;
8287 	int tso;
8288 	u32 tx_flags = 0;
8289 	unsigned short f;
8290 	u16 count = TXD_USE_COUNT(skb_headlen(skb));
8291 	__be16 protocol = skb->protocol;
8292 	u8 hdr_len = 0;
8293 
8294 	/*
8295 	 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
8296 	 *       + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
8297 	 *       + 2 desc gap to keep tail from touching head,
8298 	 *       + 1 desc for context descriptor,
8299 	 * otherwise try next time
8300 	 */
8301 	for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
8302 		count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
8303 
8304 	if (ixgbe_maybe_stop_tx(tx_ring, count + 3)) {
8305 		tx_ring->tx_stats.tx_busy++;
8306 		return NETDEV_TX_BUSY;
8307 	}
8308 
8309 	/* record the location of the first descriptor for this packet */
8310 	first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
8311 	first->skb = skb;
8312 	first->bytecount = skb->len;
8313 	first->gso_segs = 1;
8314 
8315 	/* if we have a HW VLAN tag being added default to the HW one */
8316 	if (skb_vlan_tag_present(skb)) {
8317 		tx_flags |= skb_vlan_tag_get(skb) << IXGBE_TX_FLAGS_VLAN_SHIFT;
8318 		tx_flags |= IXGBE_TX_FLAGS_HW_VLAN;
8319 	/* else if it is a SW VLAN check the next protocol and store the tag */
8320 	} else if (protocol == htons(ETH_P_8021Q)) {
8321 		struct vlan_hdr *vhdr, _vhdr;
8322 		vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
8323 		if (!vhdr)
8324 			goto out_drop;
8325 
8326 		tx_flags |= ntohs(vhdr->h_vlan_TCI) <<
8327 				  IXGBE_TX_FLAGS_VLAN_SHIFT;
8328 		tx_flags |= IXGBE_TX_FLAGS_SW_VLAN;
8329 	}
8330 	protocol = vlan_get_protocol(skb);
8331 
8332 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
8333 	    adapter->ptp_clock &&
8334 	    !test_and_set_bit_lock(__IXGBE_PTP_TX_IN_PROGRESS,
8335 				   &adapter->state)) {
8336 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
8337 		tx_flags |= IXGBE_TX_FLAGS_TSTAMP;
8338 
8339 		/* schedule check for Tx timestamp */
8340 		adapter->ptp_tx_skb = skb_get(skb);
8341 		adapter->ptp_tx_start = jiffies;
8342 		schedule_work(&adapter->ptp_tx_work);
8343 	}
8344 
8345 	skb_tx_timestamp(skb);
8346 
8347 #ifdef CONFIG_PCI_IOV
8348 	/*
8349 	 * Use the l2switch_enable flag - would be false if the DMA
8350 	 * Tx switch had been disabled.
8351 	 */
8352 	if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
8353 		tx_flags |= IXGBE_TX_FLAGS_CC;
8354 
8355 #endif
8356 	/* DCB maps skb priorities 0-7 onto 3 bit PCP of VLAN tag. */
8357 	if ((adapter->flags & IXGBE_FLAG_DCB_ENABLED) &&
8358 	    ((tx_flags & (IXGBE_TX_FLAGS_HW_VLAN | IXGBE_TX_FLAGS_SW_VLAN)) ||
8359 	     (skb->priority != TC_PRIO_CONTROL))) {
8360 		tx_flags &= ~IXGBE_TX_FLAGS_VLAN_PRIO_MASK;
8361 		tx_flags |= (skb->priority & 0x7) <<
8362 					IXGBE_TX_FLAGS_VLAN_PRIO_SHIFT;
8363 		if (tx_flags & IXGBE_TX_FLAGS_SW_VLAN) {
8364 			struct vlan_ethhdr *vhdr;
8365 
8366 			if (skb_cow_head(skb, 0))
8367 				goto out_drop;
8368 			vhdr = (struct vlan_ethhdr *)skb->data;
8369 			vhdr->h_vlan_TCI = htons(tx_flags >>
8370 						 IXGBE_TX_FLAGS_VLAN_SHIFT);
8371 		} else {
8372 			tx_flags |= IXGBE_TX_FLAGS_HW_VLAN;
8373 		}
8374 	}
8375 
8376 	/* record initial flags and protocol */
8377 	first->tx_flags = tx_flags;
8378 	first->protocol = protocol;
8379 
8380 #ifdef IXGBE_FCOE
8381 	/* setup tx offload for FCoE */
8382 	if ((protocol == htons(ETH_P_FCOE)) &&
8383 	    (tx_ring->netdev->features & (NETIF_F_FSO | NETIF_F_FCOE_CRC))) {
8384 		tso = ixgbe_fso(tx_ring, first, &hdr_len);
8385 		if (tso < 0)
8386 			goto out_drop;
8387 
8388 		goto xmit_fcoe;
8389 	}
8390 
8391 #endif /* IXGBE_FCOE */
8392 	tso = ixgbe_tso(tx_ring, first, &hdr_len);
8393 	if (tso < 0)
8394 		goto out_drop;
8395 	else if (!tso)
8396 		ixgbe_tx_csum(tx_ring, first);
8397 
8398 	/* add the ATR filter if ATR is on */
8399 	if (test_bit(__IXGBE_TX_FDIR_INIT_DONE, &tx_ring->state))
8400 		ixgbe_atr(tx_ring, first);
8401 
8402 #ifdef IXGBE_FCOE
8403 xmit_fcoe:
8404 #endif /* IXGBE_FCOE */
8405 	ixgbe_tx_map(tx_ring, first, hdr_len);
8406 
8407 	return NETDEV_TX_OK;
8408 
8409 out_drop:
8410 	dev_kfree_skb_any(first->skb);
8411 	first->skb = NULL;
8412 
8413 	return NETDEV_TX_OK;
8414 }
8415 
8416 static netdev_tx_t __ixgbe_xmit_frame(struct sk_buff *skb,
8417 				      struct net_device *netdev,
8418 				      struct ixgbe_ring *ring)
8419 {
8420 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
8421 	struct ixgbe_ring *tx_ring;
8422 
8423 	/*
8424 	 * The minimum packet size for olinfo paylen is 17 so pad the skb
8425 	 * in order to meet this minimum size requirement.
8426 	 */
8427 	if (skb_put_padto(skb, 17))
8428 		return NETDEV_TX_OK;
8429 
8430 	tx_ring = ring ? ring : adapter->tx_ring[skb->queue_mapping];
8431 
8432 	return ixgbe_xmit_frame_ring(skb, adapter, tx_ring);
8433 }
8434 
8435 static netdev_tx_t ixgbe_xmit_frame(struct sk_buff *skb,
8436 				    struct net_device *netdev)
8437 {
8438 	return __ixgbe_xmit_frame(skb, netdev, NULL);
8439 }
8440 
8441 /**
8442  * ixgbe_set_mac - Change the Ethernet Address of the NIC
8443  * @netdev: network interface device structure
8444  * @p: pointer to an address structure
8445  *
8446  * Returns 0 on success, negative on failure
8447  **/
8448 static int ixgbe_set_mac(struct net_device *netdev, void *p)
8449 {
8450 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
8451 	struct ixgbe_hw *hw = &adapter->hw;
8452 	struct sockaddr *addr = p;
8453 
8454 	if (!is_valid_ether_addr(addr->sa_data))
8455 		return -EADDRNOTAVAIL;
8456 
8457 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
8458 	memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
8459 
8460 	ixgbe_mac_set_default_filter(adapter);
8461 
8462 	return 0;
8463 }
8464 
8465 static int
8466 ixgbe_mdio_read(struct net_device *netdev, int prtad, int devad, u16 addr)
8467 {
8468 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
8469 	struct ixgbe_hw *hw = &adapter->hw;
8470 	u16 value;
8471 	int rc;
8472 
8473 	if (prtad != hw->phy.mdio.prtad)
8474 		return -EINVAL;
8475 	rc = hw->phy.ops.read_reg(hw, addr, devad, &value);
8476 	if (!rc)
8477 		rc = value;
8478 	return rc;
8479 }
8480 
8481 static int ixgbe_mdio_write(struct net_device *netdev, int prtad, int devad,
8482 			    u16 addr, u16 value)
8483 {
8484 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
8485 	struct ixgbe_hw *hw = &adapter->hw;
8486 
8487 	if (prtad != hw->phy.mdio.prtad)
8488 		return -EINVAL;
8489 	return hw->phy.ops.write_reg(hw, addr, devad, value);
8490 }
8491 
8492 static int ixgbe_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
8493 {
8494 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
8495 
8496 	switch (cmd) {
8497 	case SIOCSHWTSTAMP:
8498 		return ixgbe_ptp_set_ts_config(adapter, req);
8499 	case SIOCGHWTSTAMP:
8500 		return ixgbe_ptp_get_ts_config(adapter, req);
8501 	default:
8502 		return mdio_mii_ioctl(&adapter->hw.phy.mdio, if_mii(req), cmd);
8503 	}
8504 }
8505 
8506 /**
8507  * ixgbe_add_sanmac_netdev - Add the SAN MAC address to the corresponding
8508  * netdev->dev_addrs
8509  * @netdev: network interface device structure
8510  *
8511  * Returns non-zero on failure
8512  **/
8513 static int ixgbe_add_sanmac_netdev(struct net_device *dev)
8514 {
8515 	int err = 0;
8516 	struct ixgbe_adapter *adapter = netdev_priv(dev);
8517 	struct ixgbe_hw *hw = &adapter->hw;
8518 
8519 	if (is_valid_ether_addr(hw->mac.san_addr)) {
8520 		rtnl_lock();
8521 		err = dev_addr_add(dev, hw->mac.san_addr, NETDEV_HW_ADDR_T_SAN);
8522 		rtnl_unlock();
8523 
8524 		/* update SAN MAC vmdq pool selection */
8525 		hw->mac.ops.set_vmdq_san_mac(hw, VMDQ_P(0));
8526 	}
8527 	return err;
8528 }
8529 
8530 /**
8531  * ixgbe_del_sanmac_netdev - Removes the SAN MAC address to the corresponding
8532  * netdev->dev_addrs
8533  * @netdev: network interface device structure
8534  *
8535  * Returns non-zero on failure
8536  **/
8537 static int ixgbe_del_sanmac_netdev(struct net_device *dev)
8538 {
8539 	int err = 0;
8540 	struct ixgbe_adapter *adapter = netdev_priv(dev);
8541 	struct ixgbe_mac_info *mac = &adapter->hw.mac;
8542 
8543 	if (is_valid_ether_addr(mac->san_addr)) {
8544 		rtnl_lock();
8545 		err = dev_addr_del(dev, mac->san_addr, NETDEV_HW_ADDR_T_SAN);
8546 		rtnl_unlock();
8547 	}
8548 	return err;
8549 }
8550 
8551 #ifdef CONFIG_NET_POLL_CONTROLLER
8552 /*
8553  * Polling 'interrupt' - used by things like netconsole to send skbs
8554  * without having to re-enable interrupts. It's not called while
8555  * the interrupt routine is executing.
8556  */
8557 static void ixgbe_netpoll(struct net_device *netdev)
8558 {
8559 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
8560 	int i;
8561 
8562 	/* if interface is down do nothing */
8563 	if (test_bit(__IXGBE_DOWN, &adapter->state))
8564 		return;
8565 
8566 	/* loop through and schedule all active queues */
8567 	for (i = 0; i < adapter->num_q_vectors; i++)
8568 		ixgbe_msix_clean_rings(0, adapter->q_vector[i]);
8569 }
8570 
8571 #endif
8572 
8573 static void ixgbe_get_ring_stats64(struct rtnl_link_stats64 *stats,
8574 				   struct ixgbe_ring *ring)
8575 {
8576 	u64 bytes, packets;
8577 	unsigned int start;
8578 
8579 	if (ring) {
8580 		do {
8581 			start = u64_stats_fetch_begin_irq(&ring->syncp);
8582 			packets = ring->stats.packets;
8583 			bytes   = ring->stats.bytes;
8584 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
8585 		stats->tx_packets += packets;
8586 		stats->tx_bytes   += bytes;
8587 	}
8588 }
8589 
8590 static void ixgbe_get_stats64(struct net_device *netdev,
8591 			      struct rtnl_link_stats64 *stats)
8592 {
8593 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
8594 	int i;
8595 
8596 	rcu_read_lock();
8597 	for (i = 0; i < adapter->num_rx_queues; i++) {
8598 		struct ixgbe_ring *ring = ACCESS_ONCE(adapter->rx_ring[i]);
8599 		u64 bytes, packets;
8600 		unsigned int start;
8601 
8602 		if (ring) {
8603 			do {
8604 				start = u64_stats_fetch_begin_irq(&ring->syncp);
8605 				packets = ring->stats.packets;
8606 				bytes   = ring->stats.bytes;
8607 			} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
8608 			stats->rx_packets += packets;
8609 			stats->rx_bytes   += bytes;
8610 		}
8611 	}
8612 
8613 	for (i = 0; i < adapter->num_tx_queues; i++) {
8614 		struct ixgbe_ring *ring = ACCESS_ONCE(adapter->tx_ring[i]);
8615 
8616 		ixgbe_get_ring_stats64(stats, ring);
8617 	}
8618 	for (i = 0; i < adapter->num_xdp_queues; i++) {
8619 		struct ixgbe_ring *ring = ACCESS_ONCE(adapter->xdp_ring[i]);
8620 
8621 		ixgbe_get_ring_stats64(stats, ring);
8622 	}
8623 	rcu_read_unlock();
8624 
8625 	/* following stats updated by ixgbe_watchdog_task() */
8626 	stats->multicast	= netdev->stats.multicast;
8627 	stats->rx_errors	= netdev->stats.rx_errors;
8628 	stats->rx_length_errors	= netdev->stats.rx_length_errors;
8629 	stats->rx_crc_errors	= netdev->stats.rx_crc_errors;
8630 	stats->rx_missed_errors	= netdev->stats.rx_missed_errors;
8631 }
8632 
8633 #ifdef CONFIG_IXGBE_DCB
8634 /**
8635  * ixgbe_validate_rtr - verify 802.1Qp to Rx packet buffer mapping is valid.
8636  * @adapter: pointer to ixgbe_adapter
8637  * @tc: number of traffic classes currently enabled
8638  *
8639  * Configure a valid 802.1Qp to Rx packet buffer mapping ie confirm
8640  * 802.1Q priority maps to a packet buffer that exists.
8641  */
8642 static void ixgbe_validate_rtr(struct ixgbe_adapter *adapter, u8 tc)
8643 {
8644 	struct ixgbe_hw *hw = &adapter->hw;
8645 	u32 reg, rsave;
8646 	int i;
8647 
8648 	/* 82598 have a static priority to TC mapping that can not
8649 	 * be changed so no validation is needed.
8650 	 */
8651 	if (hw->mac.type == ixgbe_mac_82598EB)
8652 		return;
8653 
8654 	reg = IXGBE_READ_REG(hw, IXGBE_RTRUP2TC);
8655 	rsave = reg;
8656 
8657 	for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
8658 		u8 up2tc = reg >> (i * IXGBE_RTRUP2TC_UP_SHIFT);
8659 
8660 		/* If up2tc is out of bounds default to zero */
8661 		if (up2tc > tc)
8662 			reg &= ~(0x7 << IXGBE_RTRUP2TC_UP_SHIFT);
8663 	}
8664 
8665 	if (reg != rsave)
8666 		IXGBE_WRITE_REG(hw, IXGBE_RTRUP2TC, reg);
8667 
8668 	return;
8669 }
8670 
8671 /**
8672  * ixgbe_set_prio_tc_map - Configure netdev prio tc map
8673  * @adapter: Pointer to adapter struct
8674  *
8675  * Populate the netdev user priority to tc map
8676  */
8677 static void ixgbe_set_prio_tc_map(struct ixgbe_adapter *adapter)
8678 {
8679 	struct net_device *dev = adapter->netdev;
8680 	struct ixgbe_dcb_config *dcb_cfg = &adapter->dcb_cfg;
8681 	struct ieee_ets *ets = adapter->ixgbe_ieee_ets;
8682 	u8 prio;
8683 
8684 	for (prio = 0; prio < MAX_USER_PRIORITY; prio++) {
8685 		u8 tc = 0;
8686 
8687 		if (adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE)
8688 			tc = ixgbe_dcb_get_tc_from_up(dcb_cfg, 0, prio);
8689 		else if (ets)
8690 			tc = ets->prio_tc[prio];
8691 
8692 		netdev_set_prio_tc_map(dev, prio, tc);
8693 	}
8694 }
8695 
8696 #endif /* CONFIG_IXGBE_DCB */
8697 /**
8698  * ixgbe_setup_tc - configure net_device for multiple traffic classes
8699  *
8700  * @netdev: net device to configure
8701  * @tc: number of traffic classes to enable
8702  */
8703 int ixgbe_setup_tc(struct net_device *dev, u8 tc)
8704 {
8705 	struct ixgbe_adapter *adapter = netdev_priv(dev);
8706 	struct ixgbe_hw *hw = &adapter->hw;
8707 	bool pools;
8708 
8709 	/* Hardware supports up to 8 traffic classes */
8710 	if (tc > adapter->dcb_cfg.num_tcs.pg_tcs)
8711 		return -EINVAL;
8712 
8713 	if (hw->mac.type == ixgbe_mac_82598EB && tc && tc < MAX_TRAFFIC_CLASS)
8714 		return -EINVAL;
8715 
8716 	pools = (find_first_zero_bit(&adapter->fwd_bitmask, 32) > 1);
8717 	if (tc && pools && adapter->num_rx_pools > IXGBE_MAX_DCBMACVLANS)
8718 		return -EBUSY;
8719 
8720 	/* Hardware has to reinitialize queues and interrupts to
8721 	 * match packet buffer alignment. Unfortunately, the
8722 	 * hardware is not flexible enough to do this dynamically.
8723 	 */
8724 	if (netif_running(dev))
8725 		ixgbe_close(dev);
8726 	else
8727 		ixgbe_reset(adapter);
8728 
8729 	ixgbe_clear_interrupt_scheme(adapter);
8730 
8731 #ifdef CONFIG_IXGBE_DCB
8732 	if (tc) {
8733 		netdev_set_num_tc(dev, tc);
8734 		ixgbe_set_prio_tc_map(adapter);
8735 
8736 		adapter->flags |= IXGBE_FLAG_DCB_ENABLED;
8737 
8738 		if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
8739 			adapter->last_lfc_mode = adapter->hw.fc.requested_mode;
8740 			adapter->hw.fc.requested_mode = ixgbe_fc_none;
8741 		}
8742 	} else {
8743 		netdev_reset_tc(dev);
8744 
8745 		if (adapter->hw.mac.type == ixgbe_mac_82598EB)
8746 			adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
8747 
8748 		adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
8749 
8750 		adapter->temp_dcb_cfg.pfc_mode_enable = false;
8751 		adapter->dcb_cfg.pfc_mode_enable = false;
8752 	}
8753 
8754 	ixgbe_validate_rtr(adapter, tc);
8755 
8756 #endif /* CONFIG_IXGBE_DCB */
8757 	ixgbe_init_interrupt_scheme(adapter);
8758 
8759 	if (netif_running(dev))
8760 		return ixgbe_open(dev);
8761 
8762 	return 0;
8763 }
8764 
8765 static int ixgbe_delete_clsu32(struct ixgbe_adapter *adapter,
8766 			       struct tc_cls_u32_offload *cls)
8767 {
8768 	u32 hdl = cls->knode.handle;
8769 	u32 uhtid = TC_U32_USERHTID(cls->knode.handle);
8770 	u32 loc = cls->knode.handle & 0xfffff;
8771 	int err = 0, i, j;
8772 	struct ixgbe_jump_table *jump = NULL;
8773 
8774 	if (loc > IXGBE_MAX_HW_ENTRIES)
8775 		return -EINVAL;
8776 
8777 	if ((uhtid != 0x800) && (uhtid >= IXGBE_MAX_LINK_HANDLE))
8778 		return -EINVAL;
8779 
8780 	/* Clear this filter in the link data it is associated with */
8781 	if (uhtid != 0x800) {
8782 		jump = adapter->jump_tables[uhtid];
8783 		if (!jump)
8784 			return -EINVAL;
8785 		if (!test_bit(loc - 1, jump->child_loc_map))
8786 			return -EINVAL;
8787 		clear_bit(loc - 1, jump->child_loc_map);
8788 	}
8789 
8790 	/* Check if the filter being deleted is a link */
8791 	for (i = 1; i < IXGBE_MAX_LINK_HANDLE; i++) {
8792 		jump = adapter->jump_tables[i];
8793 		if (jump && jump->link_hdl == hdl) {
8794 			/* Delete filters in the hardware in the child hash
8795 			 * table associated with this link
8796 			 */
8797 			for (j = 0; j < IXGBE_MAX_HW_ENTRIES; j++) {
8798 				if (!test_bit(j, jump->child_loc_map))
8799 					continue;
8800 				spin_lock(&adapter->fdir_perfect_lock);
8801 				err = ixgbe_update_ethtool_fdir_entry(adapter,
8802 								      NULL,
8803 								      j + 1);
8804 				spin_unlock(&adapter->fdir_perfect_lock);
8805 				clear_bit(j, jump->child_loc_map);
8806 			}
8807 			/* Remove resources for this link */
8808 			kfree(jump->input);
8809 			kfree(jump->mask);
8810 			kfree(jump);
8811 			adapter->jump_tables[i] = NULL;
8812 			return err;
8813 		}
8814 	}
8815 
8816 	spin_lock(&adapter->fdir_perfect_lock);
8817 	err = ixgbe_update_ethtool_fdir_entry(adapter, NULL, loc);
8818 	spin_unlock(&adapter->fdir_perfect_lock);
8819 	return err;
8820 }
8821 
8822 static int ixgbe_configure_clsu32_add_hnode(struct ixgbe_adapter *adapter,
8823 					    __be16 protocol,
8824 					    struct tc_cls_u32_offload *cls)
8825 {
8826 	u32 uhtid = TC_U32_USERHTID(cls->hnode.handle);
8827 
8828 	if (uhtid >= IXGBE_MAX_LINK_HANDLE)
8829 		return -EINVAL;
8830 
8831 	/* This ixgbe devices do not support hash tables at the moment
8832 	 * so abort when given hash tables.
8833 	 */
8834 	if (cls->hnode.divisor > 0)
8835 		return -EINVAL;
8836 
8837 	set_bit(uhtid - 1, &adapter->tables);
8838 	return 0;
8839 }
8840 
8841 static int ixgbe_configure_clsu32_del_hnode(struct ixgbe_adapter *adapter,
8842 					    struct tc_cls_u32_offload *cls)
8843 {
8844 	u32 uhtid = TC_U32_USERHTID(cls->hnode.handle);
8845 
8846 	if (uhtid >= IXGBE_MAX_LINK_HANDLE)
8847 		return -EINVAL;
8848 
8849 	clear_bit(uhtid - 1, &adapter->tables);
8850 	return 0;
8851 }
8852 
8853 #ifdef CONFIG_NET_CLS_ACT
8854 struct upper_walk_data {
8855 	struct ixgbe_adapter *adapter;
8856 	u64 action;
8857 	int ifindex;
8858 	u8 queue;
8859 };
8860 
8861 static int get_macvlan_queue(struct net_device *upper, void *_data)
8862 {
8863 	if (netif_is_macvlan(upper)) {
8864 		struct macvlan_dev *dfwd = netdev_priv(upper);
8865 		struct ixgbe_fwd_adapter *vadapter = dfwd->fwd_priv;
8866 		struct upper_walk_data *data = _data;
8867 		struct ixgbe_adapter *adapter = data->adapter;
8868 		int ifindex = data->ifindex;
8869 
8870 		if (vadapter && vadapter->netdev->ifindex == ifindex) {
8871 			data->queue = adapter->rx_ring[vadapter->rx_base_queue]->reg_idx;
8872 			data->action = data->queue;
8873 			return 1;
8874 		}
8875 	}
8876 
8877 	return 0;
8878 }
8879 
8880 static int handle_redirect_action(struct ixgbe_adapter *adapter, int ifindex,
8881 				  u8 *queue, u64 *action)
8882 {
8883 	unsigned int num_vfs = adapter->num_vfs, vf;
8884 	struct upper_walk_data data;
8885 	struct net_device *upper;
8886 
8887 	/* redirect to a SRIOV VF */
8888 	for (vf = 0; vf < num_vfs; ++vf) {
8889 		upper = pci_get_drvdata(adapter->vfinfo[vf].vfdev);
8890 		if (upper->ifindex == ifindex) {
8891 			if (adapter->num_rx_pools > 1)
8892 				*queue = vf * 2;
8893 			else
8894 				*queue = vf * adapter->num_rx_queues_per_pool;
8895 
8896 			*action = vf + 1;
8897 			*action <<= ETHTOOL_RX_FLOW_SPEC_RING_VF_OFF;
8898 			return 0;
8899 		}
8900 	}
8901 
8902 	/* redirect to a offloaded macvlan netdev */
8903 	data.adapter = adapter;
8904 	data.ifindex = ifindex;
8905 	data.action = 0;
8906 	data.queue = 0;
8907 	if (netdev_walk_all_upper_dev_rcu(adapter->netdev,
8908 					  get_macvlan_queue, &data)) {
8909 		*action = data.action;
8910 		*queue = data.queue;
8911 
8912 		return 0;
8913 	}
8914 
8915 	return -EINVAL;
8916 }
8917 
8918 static int parse_tc_actions(struct ixgbe_adapter *adapter,
8919 			    struct tcf_exts *exts, u64 *action, u8 *queue)
8920 {
8921 	const struct tc_action *a;
8922 	LIST_HEAD(actions);
8923 	int err;
8924 
8925 	if (tc_no_actions(exts))
8926 		return -EINVAL;
8927 
8928 	tcf_exts_to_list(exts, &actions);
8929 	list_for_each_entry(a, &actions, list) {
8930 
8931 		/* Drop action */
8932 		if (is_tcf_gact_shot(a)) {
8933 			*action = IXGBE_FDIR_DROP_QUEUE;
8934 			*queue = IXGBE_FDIR_DROP_QUEUE;
8935 			return 0;
8936 		}
8937 
8938 		/* Redirect to a VF or a offloaded macvlan */
8939 		if (is_tcf_mirred_egress_redirect(a)) {
8940 			int ifindex = tcf_mirred_ifindex(a);
8941 
8942 			err = handle_redirect_action(adapter, ifindex, queue,
8943 						     action);
8944 			if (err == 0)
8945 				return err;
8946 		}
8947 	}
8948 
8949 	return -EINVAL;
8950 }
8951 #else
8952 static int parse_tc_actions(struct ixgbe_adapter *adapter,
8953 			    struct tcf_exts *exts, u64 *action, u8 *queue)
8954 {
8955 	return -EINVAL;
8956 }
8957 #endif /* CONFIG_NET_CLS_ACT */
8958 
8959 static int ixgbe_clsu32_build_input(struct ixgbe_fdir_filter *input,
8960 				    union ixgbe_atr_input *mask,
8961 				    struct tc_cls_u32_offload *cls,
8962 				    struct ixgbe_mat_field *field_ptr,
8963 				    struct ixgbe_nexthdr *nexthdr)
8964 {
8965 	int i, j, off;
8966 	__be32 val, m;
8967 	bool found_entry = false, found_jump_field = false;
8968 
8969 	for (i = 0; i < cls->knode.sel->nkeys; i++) {
8970 		off = cls->knode.sel->keys[i].off;
8971 		val = cls->knode.sel->keys[i].val;
8972 		m = cls->knode.sel->keys[i].mask;
8973 
8974 		for (j = 0; field_ptr[j].val; j++) {
8975 			if (field_ptr[j].off == off) {
8976 				field_ptr[j].val(input, mask, val, m);
8977 				input->filter.formatted.flow_type |=
8978 					field_ptr[j].type;
8979 				found_entry = true;
8980 				break;
8981 			}
8982 		}
8983 		if (nexthdr) {
8984 			if (nexthdr->off == cls->knode.sel->keys[i].off &&
8985 			    nexthdr->val == cls->knode.sel->keys[i].val &&
8986 			    nexthdr->mask == cls->knode.sel->keys[i].mask)
8987 				found_jump_field = true;
8988 			else
8989 				continue;
8990 		}
8991 	}
8992 
8993 	if (nexthdr && !found_jump_field)
8994 		return -EINVAL;
8995 
8996 	if (!found_entry)
8997 		return 0;
8998 
8999 	mask->formatted.flow_type = IXGBE_ATR_L4TYPE_IPV6_MASK |
9000 				    IXGBE_ATR_L4TYPE_MASK;
9001 
9002 	if (input->filter.formatted.flow_type == IXGBE_ATR_FLOW_TYPE_IPV4)
9003 		mask->formatted.flow_type &= IXGBE_ATR_L4TYPE_IPV6_MASK;
9004 
9005 	return 0;
9006 }
9007 
9008 static int ixgbe_configure_clsu32(struct ixgbe_adapter *adapter,
9009 				  __be16 protocol,
9010 				  struct tc_cls_u32_offload *cls)
9011 {
9012 	u32 loc = cls->knode.handle & 0xfffff;
9013 	struct ixgbe_hw *hw = &adapter->hw;
9014 	struct ixgbe_mat_field *field_ptr;
9015 	struct ixgbe_fdir_filter *input = NULL;
9016 	union ixgbe_atr_input *mask = NULL;
9017 	struct ixgbe_jump_table *jump = NULL;
9018 	int i, err = -EINVAL;
9019 	u8 queue;
9020 	u32 uhtid, link_uhtid;
9021 
9022 	uhtid = TC_U32_USERHTID(cls->knode.handle);
9023 	link_uhtid = TC_U32_USERHTID(cls->knode.link_handle);
9024 
9025 	/* At the moment cls_u32 jumps to network layer and skips past
9026 	 * L2 headers. The canonical method to match L2 frames is to use
9027 	 * negative values. However this is error prone at best but really
9028 	 * just broken because there is no way to "know" what sort of hdr
9029 	 * is in front of the network layer. Fix cls_u32 to support L2
9030 	 * headers when needed.
9031 	 */
9032 	if (protocol != htons(ETH_P_IP))
9033 		return err;
9034 
9035 	if (loc >= ((1024 << adapter->fdir_pballoc) - 2)) {
9036 		e_err(drv, "Location out of range\n");
9037 		return err;
9038 	}
9039 
9040 	/* cls u32 is a graph starting at root node 0x800. The driver tracks
9041 	 * links and also the fields used to advance the parser across each
9042 	 * link (e.g. nexthdr/eat parameters from 'tc'). This way we can map
9043 	 * the u32 graph onto the hardware parse graph denoted in ixgbe_model.h
9044 	 * To add support for new nodes update ixgbe_model.h parse structures
9045 	 * this function _should_ be generic try not to hardcode values here.
9046 	 */
9047 	if (uhtid == 0x800) {
9048 		field_ptr = (adapter->jump_tables[0])->mat;
9049 	} else {
9050 		if (uhtid >= IXGBE_MAX_LINK_HANDLE)
9051 			return err;
9052 		if (!adapter->jump_tables[uhtid])
9053 			return err;
9054 		field_ptr = (adapter->jump_tables[uhtid])->mat;
9055 	}
9056 
9057 	if (!field_ptr)
9058 		return err;
9059 
9060 	/* At this point we know the field_ptr is valid and need to either
9061 	 * build cls_u32 link or attach filter. Because adding a link to
9062 	 * a handle that does not exist is invalid and the same for adding
9063 	 * rules to handles that don't exist.
9064 	 */
9065 
9066 	if (link_uhtid) {
9067 		struct ixgbe_nexthdr *nexthdr = ixgbe_ipv4_jumps;
9068 
9069 		if (link_uhtid >= IXGBE_MAX_LINK_HANDLE)
9070 			return err;
9071 
9072 		if (!test_bit(link_uhtid - 1, &adapter->tables))
9073 			return err;
9074 
9075 		/* Multiple filters as links to the same hash table are not
9076 		 * supported. To add a new filter with the same next header
9077 		 * but different match/jump conditions, create a new hash table
9078 		 * and link to it.
9079 		 */
9080 		if (adapter->jump_tables[link_uhtid] &&
9081 		    (adapter->jump_tables[link_uhtid])->link_hdl) {
9082 			e_err(drv, "Link filter exists for link: %x\n",
9083 			      link_uhtid);
9084 			return err;
9085 		}
9086 
9087 		for (i = 0; nexthdr[i].jump; i++) {
9088 			if (nexthdr[i].o != cls->knode.sel->offoff ||
9089 			    nexthdr[i].s != cls->knode.sel->offshift ||
9090 			    nexthdr[i].m != cls->knode.sel->offmask)
9091 				return err;
9092 
9093 			jump = kzalloc(sizeof(*jump), GFP_KERNEL);
9094 			if (!jump)
9095 				return -ENOMEM;
9096 			input = kzalloc(sizeof(*input), GFP_KERNEL);
9097 			if (!input) {
9098 				err = -ENOMEM;
9099 				goto free_jump;
9100 			}
9101 			mask = kzalloc(sizeof(*mask), GFP_KERNEL);
9102 			if (!mask) {
9103 				err = -ENOMEM;
9104 				goto free_input;
9105 			}
9106 			jump->input = input;
9107 			jump->mask = mask;
9108 			jump->link_hdl = cls->knode.handle;
9109 
9110 			err = ixgbe_clsu32_build_input(input, mask, cls,
9111 						       field_ptr, &nexthdr[i]);
9112 			if (!err) {
9113 				jump->mat = nexthdr[i].jump;
9114 				adapter->jump_tables[link_uhtid] = jump;
9115 				break;
9116 			}
9117 		}
9118 		return 0;
9119 	}
9120 
9121 	input = kzalloc(sizeof(*input), GFP_KERNEL);
9122 	if (!input)
9123 		return -ENOMEM;
9124 	mask = kzalloc(sizeof(*mask), GFP_KERNEL);
9125 	if (!mask) {
9126 		err = -ENOMEM;
9127 		goto free_input;
9128 	}
9129 
9130 	if ((uhtid != 0x800) && (adapter->jump_tables[uhtid])) {
9131 		if ((adapter->jump_tables[uhtid])->input)
9132 			memcpy(input, (adapter->jump_tables[uhtid])->input,
9133 			       sizeof(*input));
9134 		if ((adapter->jump_tables[uhtid])->mask)
9135 			memcpy(mask, (adapter->jump_tables[uhtid])->mask,
9136 			       sizeof(*mask));
9137 
9138 		/* Lookup in all child hash tables if this location is already
9139 		 * filled with a filter
9140 		 */
9141 		for (i = 1; i < IXGBE_MAX_LINK_HANDLE; i++) {
9142 			struct ixgbe_jump_table *link = adapter->jump_tables[i];
9143 
9144 			if (link && (test_bit(loc - 1, link->child_loc_map))) {
9145 				e_err(drv, "Filter exists in location: %x\n",
9146 				      loc);
9147 				err = -EINVAL;
9148 				goto err_out;
9149 			}
9150 		}
9151 	}
9152 	err = ixgbe_clsu32_build_input(input, mask, cls, field_ptr, NULL);
9153 	if (err)
9154 		goto err_out;
9155 
9156 	err = parse_tc_actions(adapter, cls->knode.exts, &input->action,
9157 			       &queue);
9158 	if (err < 0)
9159 		goto err_out;
9160 
9161 	input->sw_idx = loc;
9162 
9163 	spin_lock(&adapter->fdir_perfect_lock);
9164 
9165 	if (hlist_empty(&adapter->fdir_filter_list)) {
9166 		memcpy(&adapter->fdir_mask, mask, sizeof(*mask));
9167 		err = ixgbe_fdir_set_input_mask_82599(hw, mask);
9168 		if (err)
9169 			goto err_out_w_lock;
9170 	} else if (memcmp(&adapter->fdir_mask, mask, sizeof(*mask))) {
9171 		err = -EINVAL;
9172 		goto err_out_w_lock;
9173 	}
9174 
9175 	ixgbe_atr_compute_perfect_hash_82599(&input->filter, mask);
9176 	err = ixgbe_fdir_write_perfect_filter_82599(hw, &input->filter,
9177 						    input->sw_idx, queue);
9178 	if (!err)
9179 		ixgbe_update_ethtool_fdir_entry(adapter, input, input->sw_idx);
9180 	spin_unlock(&adapter->fdir_perfect_lock);
9181 
9182 	if ((uhtid != 0x800) && (adapter->jump_tables[uhtid]))
9183 		set_bit(loc - 1, (adapter->jump_tables[uhtid])->child_loc_map);
9184 
9185 	kfree(mask);
9186 	return err;
9187 err_out_w_lock:
9188 	spin_unlock(&adapter->fdir_perfect_lock);
9189 err_out:
9190 	kfree(mask);
9191 free_input:
9192 	kfree(input);
9193 free_jump:
9194 	kfree(jump);
9195 	return err;
9196 }
9197 
9198 static int __ixgbe_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
9199 			    struct tc_to_netdev *tc)
9200 {
9201 	struct ixgbe_adapter *adapter = netdev_priv(dev);
9202 
9203 	if (TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS) &&
9204 	    tc->type == TC_SETUP_CLSU32) {
9205 		switch (tc->cls_u32->command) {
9206 		case TC_CLSU32_NEW_KNODE:
9207 		case TC_CLSU32_REPLACE_KNODE:
9208 			return ixgbe_configure_clsu32(adapter,
9209 						      proto, tc->cls_u32);
9210 		case TC_CLSU32_DELETE_KNODE:
9211 			return ixgbe_delete_clsu32(adapter, tc->cls_u32);
9212 		case TC_CLSU32_NEW_HNODE:
9213 		case TC_CLSU32_REPLACE_HNODE:
9214 			return ixgbe_configure_clsu32_add_hnode(adapter, proto,
9215 								tc->cls_u32);
9216 		case TC_CLSU32_DELETE_HNODE:
9217 			return ixgbe_configure_clsu32_del_hnode(adapter,
9218 								tc->cls_u32);
9219 		default:
9220 			return -EINVAL;
9221 		}
9222 	}
9223 
9224 	if (tc->type != TC_SETUP_MQPRIO)
9225 		return -EINVAL;
9226 
9227 	tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
9228 
9229 	return ixgbe_setup_tc(dev, tc->mqprio->num_tc);
9230 }
9231 
9232 #ifdef CONFIG_PCI_IOV
9233 void ixgbe_sriov_reinit(struct ixgbe_adapter *adapter)
9234 {
9235 	struct net_device *netdev = adapter->netdev;
9236 
9237 	rtnl_lock();
9238 	ixgbe_setup_tc(netdev, netdev_get_num_tc(netdev));
9239 	rtnl_unlock();
9240 }
9241 
9242 #endif
9243 void ixgbe_do_reset(struct net_device *netdev)
9244 {
9245 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
9246 
9247 	if (netif_running(netdev))
9248 		ixgbe_reinit_locked(adapter);
9249 	else
9250 		ixgbe_reset(adapter);
9251 }
9252 
9253 static netdev_features_t ixgbe_fix_features(struct net_device *netdev,
9254 					    netdev_features_t features)
9255 {
9256 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
9257 
9258 	/* If Rx checksum is disabled, then RSC/LRO should also be disabled */
9259 	if (!(features & NETIF_F_RXCSUM))
9260 		features &= ~NETIF_F_LRO;
9261 
9262 	/* Turn off LRO if not RSC capable */
9263 	if (!(adapter->flags2 & IXGBE_FLAG2_RSC_CAPABLE))
9264 		features &= ~NETIF_F_LRO;
9265 
9266 	return features;
9267 }
9268 
9269 static int ixgbe_set_features(struct net_device *netdev,
9270 			      netdev_features_t features)
9271 {
9272 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
9273 	netdev_features_t changed = netdev->features ^ features;
9274 	bool need_reset = false;
9275 
9276 	/* Make sure RSC matches LRO, reset if change */
9277 	if (!(features & NETIF_F_LRO)) {
9278 		if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)
9279 			need_reset = true;
9280 		adapter->flags2 &= ~IXGBE_FLAG2_RSC_ENABLED;
9281 	} else if ((adapter->flags2 & IXGBE_FLAG2_RSC_CAPABLE) &&
9282 		   !(adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)) {
9283 		if (adapter->rx_itr_setting == 1 ||
9284 		    adapter->rx_itr_setting > IXGBE_MIN_RSC_ITR) {
9285 			adapter->flags2 |= IXGBE_FLAG2_RSC_ENABLED;
9286 			need_reset = true;
9287 		} else if ((changed ^ features) & NETIF_F_LRO) {
9288 			e_info(probe, "rx-usecs set too low, "
9289 			       "disabling RSC\n");
9290 		}
9291 	}
9292 
9293 	/*
9294 	 * Check if Flow Director n-tuple support or hw_tc support was
9295 	 * enabled or disabled.  If the state changed, we need to reset.
9296 	 */
9297 	if ((features & NETIF_F_NTUPLE) || (features & NETIF_F_HW_TC)) {
9298 		/* turn off ATR, enable perfect filters and reset */
9299 		if (!(adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE))
9300 			need_reset = true;
9301 
9302 		adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
9303 		adapter->flags |= IXGBE_FLAG_FDIR_PERFECT_CAPABLE;
9304 	} else {
9305 		/* turn off perfect filters, enable ATR and reset */
9306 		if (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)
9307 			need_reset = true;
9308 
9309 		adapter->flags &= ~IXGBE_FLAG_FDIR_PERFECT_CAPABLE;
9310 
9311 		/* We cannot enable ATR if SR-IOV is enabled */
9312 		if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED ||
9313 		    /* We cannot enable ATR if we have 2 or more tcs */
9314 		    (netdev_get_num_tc(netdev) > 1) ||
9315 		    /* We cannot enable ATR if RSS is disabled */
9316 		    (adapter->ring_feature[RING_F_RSS].limit <= 1) ||
9317 		    /* A sample rate of 0 indicates ATR disabled */
9318 		    (!adapter->atr_sample_rate))
9319 			; /* do nothing not supported */
9320 		else /* otherwise supported and set the flag */
9321 			adapter->flags |= IXGBE_FLAG_FDIR_HASH_CAPABLE;
9322 	}
9323 
9324 	if (changed & NETIF_F_RXALL)
9325 		need_reset = true;
9326 
9327 	netdev->features = features;
9328 
9329 	if ((adapter->flags & IXGBE_FLAG_VXLAN_OFFLOAD_CAPABLE)) {
9330 		if (features & NETIF_F_RXCSUM) {
9331 			adapter->flags2 |= IXGBE_FLAG2_UDP_TUN_REREG_NEEDED;
9332 		} else {
9333 			u32 port_mask = IXGBE_VXLANCTRL_VXLAN_UDPPORT_MASK;
9334 
9335 			ixgbe_clear_udp_tunnel_port(adapter, port_mask);
9336 		}
9337 	}
9338 
9339 	if ((adapter->flags & IXGBE_FLAG_GENEVE_OFFLOAD_CAPABLE)) {
9340 		if (features & NETIF_F_RXCSUM) {
9341 			adapter->flags2 |= IXGBE_FLAG2_UDP_TUN_REREG_NEEDED;
9342 		} else {
9343 			u32 port_mask = IXGBE_VXLANCTRL_GENEVE_UDPPORT_MASK;
9344 
9345 			ixgbe_clear_udp_tunnel_port(adapter, port_mask);
9346 		}
9347 	}
9348 
9349 	if (need_reset)
9350 		ixgbe_do_reset(netdev);
9351 	else if (changed & (NETIF_F_HW_VLAN_CTAG_RX |
9352 			    NETIF_F_HW_VLAN_CTAG_FILTER))
9353 		ixgbe_set_rx_mode(netdev);
9354 
9355 	return 0;
9356 }
9357 
9358 /**
9359  * ixgbe_add_udp_tunnel_port - Get notifications about adding UDP tunnel ports
9360  * @dev: The port's netdev
9361  * @ti: Tunnel endpoint information
9362  **/
9363 static void ixgbe_add_udp_tunnel_port(struct net_device *dev,
9364 				      struct udp_tunnel_info *ti)
9365 {
9366 	struct ixgbe_adapter *adapter = netdev_priv(dev);
9367 	struct ixgbe_hw *hw = &adapter->hw;
9368 	__be16 port = ti->port;
9369 	u32 port_shift = 0;
9370 	u32 reg;
9371 
9372 	if (ti->sa_family != AF_INET)
9373 		return;
9374 
9375 	switch (ti->type) {
9376 	case UDP_TUNNEL_TYPE_VXLAN:
9377 		if (!(adapter->flags & IXGBE_FLAG_VXLAN_OFFLOAD_CAPABLE))
9378 			return;
9379 
9380 		if (adapter->vxlan_port == port)
9381 			return;
9382 
9383 		if (adapter->vxlan_port) {
9384 			netdev_info(dev,
9385 				    "VXLAN port %d set, not adding port %d\n",
9386 				    ntohs(adapter->vxlan_port),
9387 				    ntohs(port));
9388 			return;
9389 		}
9390 
9391 		adapter->vxlan_port = port;
9392 		break;
9393 	case UDP_TUNNEL_TYPE_GENEVE:
9394 		if (!(adapter->flags & IXGBE_FLAG_GENEVE_OFFLOAD_CAPABLE))
9395 			return;
9396 
9397 		if (adapter->geneve_port == port)
9398 			return;
9399 
9400 		if (adapter->geneve_port) {
9401 			netdev_info(dev,
9402 				    "GENEVE port %d set, not adding port %d\n",
9403 				    ntohs(adapter->geneve_port),
9404 				    ntohs(port));
9405 			return;
9406 		}
9407 
9408 		port_shift = IXGBE_VXLANCTRL_GENEVE_UDPPORT_SHIFT;
9409 		adapter->geneve_port = port;
9410 		break;
9411 	default:
9412 		return;
9413 	}
9414 
9415 	reg = IXGBE_READ_REG(hw, IXGBE_VXLANCTRL) | ntohs(port) << port_shift;
9416 	IXGBE_WRITE_REG(hw, IXGBE_VXLANCTRL, reg);
9417 }
9418 
9419 /**
9420  * ixgbe_del_udp_tunnel_port - Get notifications about removing UDP tunnel ports
9421  * @dev: The port's netdev
9422  * @ti: Tunnel endpoint information
9423  **/
9424 static void ixgbe_del_udp_tunnel_port(struct net_device *dev,
9425 				      struct udp_tunnel_info *ti)
9426 {
9427 	struct ixgbe_adapter *adapter = netdev_priv(dev);
9428 	u32 port_mask;
9429 
9430 	if (ti->type != UDP_TUNNEL_TYPE_VXLAN &&
9431 	    ti->type != UDP_TUNNEL_TYPE_GENEVE)
9432 		return;
9433 
9434 	if (ti->sa_family != AF_INET)
9435 		return;
9436 
9437 	switch (ti->type) {
9438 	case UDP_TUNNEL_TYPE_VXLAN:
9439 		if (!(adapter->flags & IXGBE_FLAG_VXLAN_OFFLOAD_CAPABLE))
9440 			return;
9441 
9442 		if (adapter->vxlan_port != ti->port) {
9443 			netdev_info(dev, "VXLAN port %d not found\n",
9444 				    ntohs(ti->port));
9445 			return;
9446 		}
9447 
9448 		port_mask = IXGBE_VXLANCTRL_VXLAN_UDPPORT_MASK;
9449 		break;
9450 	case UDP_TUNNEL_TYPE_GENEVE:
9451 		if (!(adapter->flags & IXGBE_FLAG_GENEVE_OFFLOAD_CAPABLE))
9452 			return;
9453 
9454 		if (adapter->geneve_port != ti->port) {
9455 			netdev_info(dev, "GENEVE port %d not found\n",
9456 				    ntohs(ti->port));
9457 			return;
9458 		}
9459 
9460 		port_mask = IXGBE_VXLANCTRL_GENEVE_UDPPORT_MASK;
9461 		break;
9462 	default:
9463 		return;
9464 	}
9465 
9466 	ixgbe_clear_udp_tunnel_port(adapter, port_mask);
9467 	adapter->flags2 |= IXGBE_FLAG2_UDP_TUN_REREG_NEEDED;
9468 }
9469 
9470 static int ixgbe_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9471 			     struct net_device *dev,
9472 			     const unsigned char *addr, u16 vid,
9473 			     u16 flags)
9474 {
9475 	/* guarantee we can provide a unique filter for the unicast address */
9476 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) {
9477 		struct ixgbe_adapter *adapter = netdev_priv(dev);
9478 		u16 pool = VMDQ_P(0);
9479 
9480 		if (netdev_uc_count(dev) >= ixgbe_available_rars(adapter, pool))
9481 			return -ENOMEM;
9482 	}
9483 
9484 	return ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, flags);
9485 }
9486 
9487 /**
9488  * ixgbe_configure_bridge_mode - set various bridge modes
9489  * @adapter - the private structure
9490  * @mode - requested bridge mode
9491  *
9492  * Configure some settings require for various bridge modes.
9493  **/
9494 static int ixgbe_configure_bridge_mode(struct ixgbe_adapter *adapter,
9495 				       __u16 mode)
9496 {
9497 	struct ixgbe_hw *hw = &adapter->hw;
9498 	unsigned int p, num_pools;
9499 	u32 vmdctl;
9500 
9501 	switch (mode) {
9502 	case BRIDGE_MODE_VEPA:
9503 		/* disable Tx loopback, rely on switch hairpin mode */
9504 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_PFDTXGSWC, 0);
9505 
9506 		/* must enable Rx switching replication to allow multicast
9507 		 * packet reception on all VFs, and to enable source address
9508 		 * pruning.
9509 		 */
9510 		vmdctl = IXGBE_READ_REG(hw, IXGBE_VMD_CTL);
9511 		vmdctl |= IXGBE_VT_CTL_REPLEN;
9512 		IXGBE_WRITE_REG(hw, IXGBE_VMD_CTL, vmdctl);
9513 
9514 		/* enable Rx source address pruning. Note, this requires
9515 		 * replication to be enabled or else it does nothing.
9516 		 */
9517 		num_pools = adapter->num_vfs + adapter->num_rx_pools;
9518 		for (p = 0; p < num_pools; p++) {
9519 			if (hw->mac.ops.set_source_address_pruning)
9520 				hw->mac.ops.set_source_address_pruning(hw,
9521 								       true,
9522 								       p);
9523 		}
9524 		break;
9525 	case BRIDGE_MODE_VEB:
9526 		/* enable Tx loopback for internal VF/PF communication */
9527 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_PFDTXGSWC,
9528 				IXGBE_PFDTXGSWC_VT_LBEN);
9529 
9530 		/* disable Rx switching replication unless we have SR-IOV
9531 		 * virtual functions
9532 		 */
9533 		vmdctl = IXGBE_READ_REG(hw, IXGBE_VMD_CTL);
9534 		if (!adapter->num_vfs)
9535 			vmdctl &= ~IXGBE_VT_CTL_REPLEN;
9536 		IXGBE_WRITE_REG(hw, IXGBE_VMD_CTL, vmdctl);
9537 
9538 		/* disable Rx source address pruning, since we don't expect to
9539 		 * be receiving external loopback of our transmitted frames.
9540 		 */
9541 		num_pools = adapter->num_vfs + adapter->num_rx_pools;
9542 		for (p = 0; p < num_pools; p++) {
9543 			if (hw->mac.ops.set_source_address_pruning)
9544 				hw->mac.ops.set_source_address_pruning(hw,
9545 								       false,
9546 								       p);
9547 		}
9548 		break;
9549 	default:
9550 		return -EINVAL;
9551 	}
9552 
9553 	adapter->bridge_mode = mode;
9554 
9555 	e_info(drv, "enabling bridge mode: %s\n",
9556 	       mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
9557 
9558 	return 0;
9559 }
9560 
9561 static int ixgbe_ndo_bridge_setlink(struct net_device *dev,
9562 				    struct nlmsghdr *nlh, u16 flags)
9563 {
9564 	struct ixgbe_adapter *adapter = netdev_priv(dev);
9565 	struct nlattr *attr, *br_spec;
9566 	int rem;
9567 
9568 	if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
9569 		return -EOPNOTSUPP;
9570 
9571 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9572 	if (!br_spec)
9573 		return -EINVAL;
9574 
9575 	nla_for_each_nested(attr, br_spec, rem) {
9576 		int status;
9577 		__u16 mode;
9578 
9579 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
9580 			continue;
9581 
9582 		if (nla_len(attr) < sizeof(mode))
9583 			return -EINVAL;
9584 
9585 		mode = nla_get_u16(attr);
9586 		status = ixgbe_configure_bridge_mode(adapter, mode);
9587 		if (status)
9588 			return status;
9589 
9590 		break;
9591 	}
9592 
9593 	return 0;
9594 }
9595 
9596 static int ixgbe_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9597 				    struct net_device *dev,
9598 				    u32 filter_mask, int nlflags)
9599 {
9600 	struct ixgbe_adapter *adapter = netdev_priv(dev);
9601 
9602 	if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
9603 		return 0;
9604 
9605 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
9606 				       adapter->bridge_mode, 0, 0, nlflags,
9607 				       filter_mask, NULL);
9608 }
9609 
9610 static void *ixgbe_fwd_add(struct net_device *pdev, struct net_device *vdev)
9611 {
9612 	struct ixgbe_fwd_adapter *fwd_adapter = NULL;
9613 	struct ixgbe_adapter *adapter = netdev_priv(pdev);
9614 	int used_pools = adapter->num_vfs + adapter->num_rx_pools;
9615 	unsigned int limit;
9616 	int pool, err;
9617 
9618 	/* Hardware has a limited number of available pools. Each VF, and the
9619 	 * PF require a pool. Check to ensure we don't attempt to use more
9620 	 * then the available number of pools.
9621 	 */
9622 	if (used_pools >= IXGBE_MAX_VF_FUNCTIONS)
9623 		return ERR_PTR(-EINVAL);
9624 
9625 #ifdef CONFIG_RPS
9626 	if (vdev->num_rx_queues != vdev->num_tx_queues) {
9627 		netdev_info(pdev, "%s: Only supports a single queue count for TX and RX\n",
9628 			    vdev->name);
9629 		return ERR_PTR(-EINVAL);
9630 	}
9631 #endif
9632 	/* Check for hardware restriction on number of rx/tx queues */
9633 	if (vdev->num_tx_queues > IXGBE_MAX_L2A_QUEUES ||
9634 	    vdev->num_tx_queues == IXGBE_BAD_L2A_QUEUE) {
9635 		netdev_info(pdev,
9636 			    "%s: Supports RX/TX Queue counts 1,2, and 4\n",
9637 			    pdev->name);
9638 		return ERR_PTR(-EINVAL);
9639 	}
9640 
9641 	if (((adapter->flags & IXGBE_FLAG_DCB_ENABLED) &&
9642 	      adapter->num_rx_pools > IXGBE_MAX_DCBMACVLANS - 1) ||
9643 	    (adapter->num_rx_pools > IXGBE_MAX_MACVLANS))
9644 		return ERR_PTR(-EBUSY);
9645 
9646 	fwd_adapter = kzalloc(sizeof(*fwd_adapter), GFP_KERNEL);
9647 	if (!fwd_adapter)
9648 		return ERR_PTR(-ENOMEM);
9649 
9650 	pool = find_first_zero_bit(&adapter->fwd_bitmask, 32);
9651 	adapter->num_rx_pools++;
9652 	set_bit(pool, &adapter->fwd_bitmask);
9653 	limit = find_last_bit(&adapter->fwd_bitmask, 32);
9654 
9655 	/* Enable VMDq flag so device will be set in VM mode */
9656 	adapter->flags |= IXGBE_FLAG_VMDQ_ENABLED | IXGBE_FLAG_SRIOV_ENABLED;
9657 	adapter->ring_feature[RING_F_VMDQ].limit = limit + 1;
9658 	adapter->ring_feature[RING_F_RSS].limit = vdev->num_tx_queues;
9659 
9660 	/* Force reinit of ring allocation with VMDQ enabled */
9661 	err = ixgbe_setup_tc(pdev, netdev_get_num_tc(pdev));
9662 	if (err)
9663 		goto fwd_add_err;
9664 	fwd_adapter->pool = pool;
9665 	fwd_adapter->real_adapter = adapter;
9666 
9667 	if (netif_running(pdev)) {
9668 		err = ixgbe_fwd_ring_up(vdev, fwd_adapter);
9669 		if (err)
9670 			goto fwd_add_err;
9671 		netif_tx_start_all_queues(vdev);
9672 	}
9673 
9674 	return fwd_adapter;
9675 fwd_add_err:
9676 	/* unwind counter and free adapter struct */
9677 	netdev_info(pdev,
9678 		    "%s: dfwd hardware acceleration failed\n", vdev->name);
9679 	clear_bit(pool, &adapter->fwd_bitmask);
9680 	adapter->num_rx_pools--;
9681 	kfree(fwd_adapter);
9682 	return ERR_PTR(err);
9683 }
9684 
9685 static void ixgbe_fwd_del(struct net_device *pdev, void *priv)
9686 {
9687 	struct ixgbe_fwd_adapter *fwd_adapter = priv;
9688 	struct ixgbe_adapter *adapter = fwd_adapter->real_adapter;
9689 	unsigned int limit;
9690 
9691 	clear_bit(fwd_adapter->pool, &adapter->fwd_bitmask);
9692 	adapter->num_rx_pools--;
9693 
9694 	limit = find_last_bit(&adapter->fwd_bitmask, 32);
9695 	adapter->ring_feature[RING_F_VMDQ].limit = limit + 1;
9696 	ixgbe_fwd_ring_down(fwd_adapter->netdev, fwd_adapter);
9697 	ixgbe_setup_tc(pdev, netdev_get_num_tc(pdev));
9698 	netdev_dbg(pdev, "pool %i:%i queues %i:%i VSI bitmask %lx\n",
9699 		   fwd_adapter->pool, adapter->num_rx_pools,
9700 		   fwd_adapter->rx_base_queue,
9701 		   fwd_adapter->rx_base_queue + adapter->num_rx_queues_per_pool,
9702 		   adapter->fwd_bitmask);
9703 	kfree(fwd_adapter);
9704 }
9705 
9706 #define IXGBE_MAX_MAC_HDR_LEN		127
9707 #define IXGBE_MAX_NETWORK_HDR_LEN	511
9708 
9709 static netdev_features_t
9710 ixgbe_features_check(struct sk_buff *skb, struct net_device *dev,
9711 		     netdev_features_t features)
9712 {
9713 	unsigned int network_hdr_len, mac_hdr_len;
9714 
9715 	/* Make certain the headers can be described by a context descriptor */
9716 	mac_hdr_len = skb_network_header(skb) - skb->data;
9717 	if (unlikely(mac_hdr_len > IXGBE_MAX_MAC_HDR_LEN))
9718 		return features & ~(NETIF_F_HW_CSUM |
9719 				    NETIF_F_SCTP_CRC |
9720 				    NETIF_F_HW_VLAN_CTAG_TX |
9721 				    NETIF_F_TSO |
9722 				    NETIF_F_TSO6);
9723 
9724 	network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
9725 	if (unlikely(network_hdr_len >  IXGBE_MAX_NETWORK_HDR_LEN))
9726 		return features & ~(NETIF_F_HW_CSUM |
9727 				    NETIF_F_SCTP_CRC |
9728 				    NETIF_F_TSO |
9729 				    NETIF_F_TSO6);
9730 
9731 	/* We can only support IPV4 TSO in tunnels if we can mangle the
9732 	 * inner IP ID field, so strip TSO if MANGLEID is not supported.
9733 	 */
9734 	if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
9735 		features &= ~NETIF_F_TSO;
9736 
9737 	return features;
9738 }
9739 
9740 static int ixgbe_xdp_setup(struct net_device *dev, struct bpf_prog *prog)
9741 {
9742 	int i, frame_size = dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
9743 	struct ixgbe_adapter *adapter = netdev_priv(dev);
9744 	struct bpf_prog *old_prog;
9745 
9746 	if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
9747 		return -EINVAL;
9748 
9749 	if (adapter->flags & IXGBE_FLAG_DCB_ENABLED)
9750 		return -EINVAL;
9751 
9752 	/* verify ixgbe ring attributes are sufficient for XDP */
9753 	for (i = 0; i < adapter->num_rx_queues; i++) {
9754 		struct ixgbe_ring *ring = adapter->rx_ring[i];
9755 
9756 		if (ring_is_rsc_enabled(ring))
9757 			return -EINVAL;
9758 
9759 		if (frame_size > ixgbe_rx_bufsz(ring))
9760 			return -EINVAL;
9761 	}
9762 
9763 	if (nr_cpu_ids > MAX_XDP_QUEUES)
9764 		return -ENOMEM;
9765 
9766 	old_prog = xchg(&adapter->xdp_prog, prog);
9767 
9768 	/* If transitioning XDP modes reconfigure rings */
9769 	if (!!prog != !!old_prog) {
9770 		int err = ixgbe_setup_tc(dev, netdev_get_num_tc(dev));
9771 
9772 		if (err) {
9773 			rcu_assign_pointer(adapter->xdp_prog, old_prog);
9774 			return -EINVAL;
9775 		}
9776 	} else {
9777 		for (i = 0; i < adapter->num_rx_queues; i++)
9778 			xchg(&adapter->rx_ring[i]->xdp_prog, adapter->xdp_prog);
9779 	}
9780 
9781 	if (old_prog)
9782 		bpf_prog_put(old_prog);
9783 
9784 	return 0;
9785 }
9786 
9787 static int ixgbe_xdp(struct net_device *dev, struct netdev_xdp *xdp)
9788 {
9789 	struct ixgbe_adapter *adapter = netdev_priv(dev);
9790 
9791 	switch (xdp->command) {
9792 	case XDP_SETUP_PROG:
9793 		return ixgbe_xdp_setup(dev, xdp->prog);
9794 	case XDP_QUERY_PROG:
9795 		xdp->prog_attached = !!(adapter->xdp_prog);
9796 		return 0;
9797 	default:
9798 		return -EINVAL;
9799 	}
9800 }
9801 
9802 static const struct net_device_ops ixgbe_netdev_ops = {
9803 	.ndo_open		= ixgbe_open,
9804 	.ndo_stop		= ixgbe_close,
9805 	.ndo_start_xmit		= ixgbe_xmit_frame,
9806 	.ndo_select_queue	= ixgbe_select_queue,
9807 	.ndo_set_rx_mode	= ixgbe_set_rx_mode,
9808 	.ndo_validate_addr	= eth_validate_addr,
9809 	.ndo_set_mac_address	= ixgbe_set_mac,
9810 	.ndo_change_mtu		= ixgbe_change_mtu,
9811 	.ndo_tx_timeout		= ixgbe_tx_timeout,
9812 	.ndo_set_tx_maxrate	= ixgbe_tx_maxrate,
9813 	.ndo_vlan_rx_add_vid	= ixgbe_vlan_rx_add_vid,
9814 	.ndo_vlan_rx_kill_vid	= ixgbe_vlan_rx_kill_vid,
9815 	.ndo_do_ioctl		= ixgbe_ioctl,
9816 	.ndo_set_vf_mac		= ixgbe_ndo_set_vf_mac,
9817 	.ndo_set_vf_vlan	= ixgbe_ndo_set_vf_vlan,
9818 	.ndo_set_vf_rate	= ixgbe_ndo_set_vf_bw,
9819 	.ndo_set_vf_spoofchk	= ixgbe_ndo_set_vf_spoofchk,
9820 	.ndo_set_vf_rss_query_en = ixgbe_ndo_set_vf_rss_query_en,
9821 	.ndo_set_vf_trust	= ixgbe_ndo_set_vf_trust,
9822 	.ndo_get_vf_config	= ixgbe_ndo_get_vf_config,
9823 	.ndo_get_stats64	= ixgbe_get_stats64,
9824 	.ndo_setup_tc		= __ixgbe_setup_tc,
9825 #ifdef CONFIG_NET_POLL_CONTROLLER
9826 	.ndo_poll_controller	= ixgbe_netpoll,
9827 #endif
9828 #ifdef IXGBE_FCOE
9829 	.ndo_fcoe_ddp_setup = ixgbe_fcoe_ddp_get,
9830 	.ndo_fcoe_ddp_target = ixgbe_fcoe_ddp_target,
9831 	.ndo_fcoe_ddp_done = ixgbe_fcoe_ddp_put,
9832 	.ndo_fcoe_enable = ixgbe_fcoe_enable,
9833 	.ndo_fcoe_disable = ixgbe_fcoe_disable,
9834 	.ndo_fcoe_get_wwn = ixgbe_fcoe_get_wwn,
9835 	.ndo_fcoe_get_hbainfo = ixgbe_fcoe_get_hbainfo,
9836 #endif /* IXGBE_FCOE */
9837 	.ndo_set_features = ixgbe_set_features,
9838 	.ndo_fix_features = ixgbe_fix_features,
9839 	.ndo_fdb_add		= ixgbe_ndo_fdb_add,
9840 	.ndo_bridge_setlink	= ixgbe_ndo_bridge_setlink,
9841 	.ndo_bridge_getlink	= ixgbe_ndo_bridge_getlink,
9842 	.ndo_dfwd_add_station	= ixgbe_fwd_add,
9843 	.ndo_dfwd_del_station	= ixgbe_fwd_del,
9844 	.ndo_udp_tunnel_add	= ixgbe_add_udp_tunnel_port,
9845 	.ndo_udp_tunnel_del	= ixgbe_del_udp_tunnel_port,
9846 	.ndo_features_check	= ixgbe_features_check,
9847 	.ndo_xdp		= ixgbe_xdp,
9848 };
9849 
9850 /**
9851  * ixgbe_enumerate_functions - Get the number of ports this device has
9852  * @adapter: adapter structure
9853  *
9854  * This function enumerates the phsyical functions co-located on a single slot,
9855  * in order to determine how many ports a device has. This is most useful in
9856  * determining the required GT/s of PCIe bandwidth necessary for optimal
9857  * performance.
9858  **/
9859 static inline int ixgbe_enumerate_functions(struct ixgbe_adapter *adapter)
9860 {
9861 	struct pci_dev *entry, *pdev = adapter->pdev;
9862 	int physfns = 0;
9863 
9864 	/* Some cards can not use the generic count PCIe functions method,
9865 	 * because they are behind a parent switch, so we hardcode these with
9866 	 * the correct number of functions.
9867 	 */
9868 	if (ixgbe_pcie_from_parent(&adapter->hw))
9869 		physfns = 4;
9870 
9871 	list_for_each_entry(entry, &adapter->pdev->bus->devices, bus_list) {
9872 		/* don't count virtual functions */
9873 		if (entry->is_virtfn)
9874 			continue;
9875 
9876 		/* When the devices on the bus don't all match our device ID,
9877 		 * we can't reliably determine the correct number of
9878 		 * functions. This can occur if a function has been direct
9879 		 * attached to a virtual machine using VT-d, for example. In
9880 		 * this case, simply return -1 to indicate this.
9881 		 */
9882 		if ((entry->vendor != pdev->vendor) ||
9883 		    (entry->device != pdev->device))
9884 			return -1;
9885 
9886 		physfns++;
9887 	}
9888 
9889 	return physfns;
9890 }
9891 
9892 /**
9893  * ixgbe_wol_supported - Check whether device supports WoL
9894  * @adapter: the adapter private structure
9895  * @device_id: the device ID
9896  * @subdev_id: the subsystem device ID
9897  *
9898  * This function is used by probe and ethtool to determine
9899  * which devices have WoL support
9900  *
9901  **/
9902 bool ixgbe_wol_supported(struct ixgbe_adapter *adapter, u16 device_id,
9903 			 u16 subdevice_id)
9904 {
9905 	struct ixgbe_hw *hw = &adapter->hw;
9906 	u16 wol_cap = adapter->eeprom_cap & IXGBE_DEVICE_CAPS_WOL_MASK;
9907 
9908 	/* WOL not supported on 82598 */
9909 	if (hw->mac.type == ixgbe_mac_82598EB)
9910 		return false;
9911 
9912 	/* check eeprom to see if WOL is enabled for X540 and newer */
9913 	if (hw->mac.type >= ixgbe_mac_X540) {
9914 		if ((wol_cap == IXGBE_DEVICE_CAPS_WOL_PORT0_1) ||
9915 		    ((wol_cap == IXGBE_DEVICE_CAPS_WOL_PORT0) &&
9916 		     (hw->bus.func == 0)))
9917 			return true;
9918 	}
9919 
9920 	/* WOL is determined based on device IDs for 82599 MACs */
9921 	switch (device_id) {
9922 	case IXGBE_DEV_ID_82599_SFP:
9923 		/* Only these subdevices could supports WOL */
9924 		switch (subdevice_id) {
9925 		case IXGBE_SUBDEV_ID_82599_560FLR:
9926 		case IXGBE_SUBDEV_ID_82599_LOM_SNAP6:
9927 		case IXGBE_SUBDEV_ID_82599_SFP_WOL0:
9928 		case IXGBE_SUBDEV_ID_82599_SFP_2OCP:
9929 			/* only support first port */
9930 			if (hw->bus.func != 0)
9931 				break;
9932 		case IXGBE_SUBDEV_ID_82599_SP_560FLR:
9933 		case IXGBE_SUBDEV_ID_82599_SFP:
9934 		case IXGBE_SUBDEV_ID_82599_RNDC:
9935 		case IXGBE_SUBDEV_ID_82599_ECNA_DP:
9936 		case IXGBE_SUBDEV_ID_82599_SFP_1OCP:
9937 		case IXGBE_SUBDEV_ID_82599_SFP_LOM_OEM1:
9938 		case IXGBE_SUBDEV_ID_82599_SFP_LOM_OEM2:
9939 			return true;
9940 		}
9941 		break;
9942 	case IXGBE_DEV_ID_82599EN_SFP:
9943 		/* Only these subdevices support WOL */
9944 		switch (subdevice_id) {
9945 		case IXGBE_SUBDEV_ID_82599EN_SFP_OCP1:
9946 			return true;
9947 		}
9948 		break;
9949 	case IXGBE_DEV_ID_82599_COMBO_BACKPLANE:
9950 		/* All except this subdevice support WOL */
9951 		if (subdevice_id != IXGBE_SUBDEV_ID_82599_KX4_KR_MEZZ)
9952 			return true;
9953 		break;
9954 	case IXGBE_DEV_ID_82599_KX4:
9955 		return  true;
9956 	default:
9957 		break;
9958 	}
9959 
9960 	return false;
9961 }
9962 
9963 /**
9964  * ixgbe_probe - Device Initialization Routine
9965  * @pdev: PCI device information struct
9966  * @ent: entry in ixgbe_pci_tbl
9967  *
9968  * Returns 0 on success, negative on failure
9969  *
9970  * ixgbe_probe initializes an adapter identified by a pci_dev structure.
9971  * The OS initialization, configuring of the adapter private structure,
9972  * and a hardware reset occur.
9973  **/
9974 static int ixgbe_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
9975 {
9976 	struct net_device *netdev;
9977 	struct ixgbe_adapter *adapter = NULL;
9978 	struct ixgbe_hw *hw;
9979 	const struct ixgbe_info *ii = ixgbe_info_tbl[ent->driver_data];
9980 	int i, err, pci_using_dac, expected_gts;
9981 	unsigned int indices = MAX_TX_QUEUES;
9982 	u8 part_str[IXGBE_PBANUM_LENGTH];
9983 	bool disable_dev = false;
9984 #ifdef IXGBE_FCOE
9985 	u16 device_caps;
9986 #endif
9987 	u32 eec;
9988 
9989 	/* Catch broken hardware that put the wrong VF device ID in
9990 	 * the PCIe SR-IOV capability.
9991 	 */
9992 	if (pdev->is_virtfn) {
9993 		WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
9994 		     pci_name(pdev), pdev->vendor, pdev->device);
9995 		return -EINVAL;
9996 	}
9997 
9998 	err = pci_enable_device_mem(pdev);
9999 	if (err)
10000 		return err;
10001 
10002 	if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
10003 		pci_using_dac = 1;
10004 	} else {
10005 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10006 		if (err) {
10007 			dev_err(&pdev->dev,
10008 				"No usable DMA configuration, aborting\n");
10009 			goto err_dma;
10010 		}
10011 		pci_using_dac = 0;
10012 	}
10013 
10014 	err = pci_request_mem_regions(pdev, ixgbe_driver_name);
10015 	if (err) {
10016 		dev_err(&pdev->dev,
10017 			"pci_request_selected_regions failed 0x%x\n", err);
10018 		goto err_pci_reg;
10019 	}
10020 
10021 	pci_enable_pcie_error_reporting(pdev);
10022 
10023 	pci_set_master(pdev);
10024 	pci_save_state(pdev);
10025 
10026 	if (ii->mac == ixgbe_mac_82598EB) {
10027 #ifdef CONFIG_IXGBE_DCB
10028 		/* 8 TC w/ 4 queues per TC */
10029 		indices = 4 * MAX_TRAFFIC_CLASS;
10030 #else
10031 		indices = IXGBE_MAX_RSS_INDICES;
10032 #endif
10033 	}
10034 
10035 	netdev = alloc_etherdev_mq(sizeof(struct ixgbe_adapter), indices);
10036 	if (!netdev) {
10037 		err = -ENOMEM;
10038 		goto err_alloc_etherdev;
10039 	}
10040 
10041 	SET_NETDEV_DEV(netdev, &pdev->dev);
10042 
10043 	adapter = netdev_priv(netdev);
10044 
10045 	adapter->netdev = netdev;
10046 	adapter->pdev = pdev;
10047 	hw = &adapter->hw;
10048 	hw->back = adapter;
10049 	adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
10050 
10051 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
10052 			      pci_resource_len(pdev, 0));
10053 	adapter->io_addr = hw->hw_addr;
10054 	if (!hw->hw_addr) {
10055 		err = -EIO;
10056 		goto err_ioremap;
10057 	}
10058 
10059 	netdev->netdev_ops = &ixgbe_netdev_ops;
10060 	ixgbe_set_ethtool_ops(netdev);
10061 	netdev->watchdog_timeo = 5 * HZ;
10062 	strlcpy(netdev->name, pci_name(pdev), sizeof(netdev->name));
10063 
10064 	/* Setup hw api */
10065 	hw->mac.ops   = *ii->mac_ops;
10066 	hw->mac.type  = ii->mac;
10067 	hw->mvals     = ii->mvals;
10068 	if (ii->link_ops)
10069 		hw->link.ops  = *ii->link_ops;
10070 
10071 	/* EEPROM */
10072 	hw->eeprom.ops = *ii->eeprom_ops;
10073 	eec = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
10074 	if (ixgbe_removed(hw->hw_addr)) {
10075 		err = -EIO;
10076 		goto err_ioremap;
10077 	}
10078 	/* If EEPROM is valid (bit 8 = 1), use default otherwise use bit bang */
10079 	if (!(eec & BIT(8)))
10080 		hw->eeprom.ops.read = &ixgbe_read_eeprom_bit_bang_generic;
10081 
10082 	/* PHY */
10083 	hw->phy.ops = *ii->phy_ops;
10084 	hw->phy.sfp_type = ixgbe_sfp_type_unknown;
10085 	/* ixgbe_identify_phy_generic will set prtad and mmds properly */
10086 	hw->phy.mdio.prtad = MDIO_PRTAD_NONE;
10087 	hw->phy.mdio.mmds = 0;
10088 	hw->phy.mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
10089 	hw->phy.mdio.dev = netdev;
10090 	hw->phy.mdio.mdio_read = ixgbe_mdio_read;
10091 	hw->phy.mdio.mdio_write = ixgbe_mdio_write;
10092 
10093 	/* setup the private structure */
10094 	err = ixgbe_sw_init(adapter, ii);
10095 	if (err)
10096 		goto err_sw_init;
10097 
10098 	/* Make sure the SWFW semaphore is in a valid state */
10099 	if (hw->mac.ops.init_swfw_sync)
10100 		hw->mac.ops.init_swfw_sync(hw);
10101 
10102 	/* Make it possible the adapter to be woken up via WOL */
10103 	switch (adapter->hw.mac.type) {
10104 	case ixgbe_mac_82599EB:
10105 	case ixgbe_mac_X540:
10106 	case ixgbe_mac_X550:
10107 	case ixgbe_mac_X550EM_x:
10108 	case ixgbe_mac_x550em_a:
10109 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_WUS, ~0);
10110 		break;
10111 	default:
10112 		break;
10113 	}
10114 
10115 	/*
10116 	 * If there is a fan on this device and it has failed log the
10117 	 * failure.
10118 	 */
10119 	if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) {
10120 		u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
10121 		if (esdp & IXGBE_ESDP_SDP1)
10122 			e_crit(probe, "Fan has stopped, replace the adapter\n");
10123 	}
10124 
10125 	if (allow_unsupported_sfp)
10126 		hw->allow_unsupported_sfp = allow_unsupported_sfp;
10127 
10128 	/* reset_hw fills in the perm_addr as well */
10129 	hw->phy.reset_if_overtemp = true;
10130 	err = hw->mac.ops.reset_hw(hw);
10131 	hw->phy.reset_if_overtemp = false;
10132 	ixgbe_set_eee_capable(adapter);
10133 	if (err == IXGBE_ERR_SFP_NOT_PRESENT) {
10134 		err = 0;
10135 	} else if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
10136 		e_dev_err("failed to load because an unsupported SFP+ or QSFP module type was detected.\n");
10137 		e_dev_err("Reload the driver after installing a supported module.\n");
10138 		goto err_sw_init;
10139 	} else if (err) {
10140 		e_dev_err("HW Init failed: %d\n", err);
10141 		goto err_sw_init;
10142 	}
10143 
10144 #ifdef CONFIG_PCI_IOV
10145 	/* SR-IOV not supported on the 82598 */
10146 	if (adapter->hw.mac.type == ixgbe_mac_82598EB)
10147 		goto skip_sriov;
10148 	/* Mailbox */
10149 	ixgbe_init_mbx_params_pf(hw);
10150 	hw->mbx.ops = ii->mbx_ops;
10151 	pci_sriov_set_totalvfs(pdev, IXGBE_MAX_VFS_DRV_LIMIT);
10152 	ixgbe_enable_sriov(adapter, max_vfs);
10153 skip_sriov:
10154 
10155 #endif
10156 	netdev->features = NETIF_F_SG |
10157 			   NETIF_F_TSO |
10158 			   NETIF_F_TSO6 |
10159 			   NETIF_F_RXHASH |
10160 			   NETIF_F_RXCSUM |
10161 			   NETIF_F_HW_CSUM;
10162 
10163 #define IXGBE_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
10164 				    NETIF_F_GSO_GRE_CSUM | \
10165 				    NETIF_F_GSO_IPXIP4 | \
10166 				    NETIF_F_GSO_IPXIP6 | \
10167 				    NETIF_F_GSO_UDP_TUNNEL | \
10168 				    NETIF_F_GSO_UDP_TUNNEL_CSUM)
10169 
10170 	netdev->gso_partial_features = IXGBE_GSO_PARTIAL_FEATURES;
10171 	netdev->features |= NETIF_F_GSO_PARTIAL |
10172 			    IXGBE_GSO_PARTIAL_FEATURES;
10173 
10174 	if (hw->mac.type >= ixgbe_mac_82599EB)
10175 		netdev->features |= NETIF_F_SCTP_CRC;
10176 
10177 	/* copy netdev features into list of user selectable features */
10178 	netdev->hw_features |= netdev->features |
10179 			       NETIF_F_HW_VLAN_CTAG_FILTER |
10180 			       NETIF_F_HW_VLAN_CTAG_RX |
10181 			       NETIF_F_HW_VLAN_CTAG_TX |
10182 			       NETIF_F_RXALL |
10183 			       NETIF_F_HW_L2FW_DOFFLOAD;
10184 
10185 	if (hw->mac.type >= ixgbe_mac_82599EB)
10186 		netdev->hw_features |= NETIF_F_NTUPLE |
10187 				       NETIF_F_HW_TC;
10188 
10189 	if (pci_using_dac)
10190 		netdev->features |= NETIF_F_HIGHDMA;
10191 
10192 	netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
10193 	netdev->hw_enc_features |= netdev->vlan_features;
10194 	netdev->mpls_features |= NETIF_F_HW_CSUM;
10195 
10196 	/* set this bit last since it cannot be part of vlan_features */
10197 	netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
10198 			    NETIF_F_HW_VLAN_CTAG_RX |
10199 			    NETIF_F_HW_VLAN_CTAG_TX;
10200 
10201 	netdev->priv_flags |= IFF_UNICAST_FLT;
10202 	netdev->priv_flags |= IFF_SUPP_NOFCS;
10203 
10204 	/* MTU range: 68 - 9710 */
10205 	netdev->min_mtu = ETH_MIN_MTU;
10206 	netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN);
10207 
10208 #ifdef CONFIG_IXGBE_DCB
10209 	if (adapter->flags & IXGBE_FLAG_DCB_CAPABLE)
10210 		netdev->dcbnl_ops = &ixgbe_dcbnl_ops;
10211 #endif
10212 
10213 #ifdef IXGBE_FCOE
10214 	if (adapter->flags & IXGBE_FLAG_FCOE_CAPABLE) {
10215 		unsigned int fcoe_l;
10216 
10217 		if (hw->mac.ops.get_device_caps) {
10218 			hw->mac.ops.get_device_caps(hw, &device_caps);
10219 			if (device_caps & IXGBE_DEVICE_CAPS_FCOE_OFFLOADS)
10220 				adapter->flags &= ~IXGBE_FLAG_FCOE_CAPABLE;
10221 		}
10222 
10223 
10224 		fcoe_l = min_t(int, IXGBE_FCRETA_SIZE, num_online_cpus());
10225 		adapter->ring_feature[RING_F_FCOE].limit = fcoe_l;
10226 
10227 		netdev->features |= NETIF_F_FSO |
10228 				    NETIF_F_FCOE_CRC;
10229 
10230 		netdev->vlan_features |= NETIF_F_FSO |
10231 					 NETIF_F_FCOE_CRC |
10232 					 NETIF_F_FCOE_MTU;
10233 	}
10234 #endif /* IXGBE_FCOE */
10235 
10236 	if (adapter->flags2 & IXGBE_FLAG2_RSC_CAPABLE)
10237 		netdev->hw_features |= NETIF_F_LRO;
10238 	if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)
10239 		netdev->features |= NETIF_F_LRO;
10240 
10241 	/* make sure the EEPROM is good */
10242 	if (hw->eeprom.ops.validate_checksum(hw, NULL) < 0) {
10243 		e_dev_err("The EEPROM Checksum Is Not Valid\n");
10244 		err = -EIO;
10245 		goto err_sw_init;
10246 	}
10247 
10248 	eth_platform_get_mac_address(&adapter->pdev->dev,
10249 				     adapter->hw.mac.perm_addr);
10250 
10251 	memcpy(netdev->dev_addr, hw->mac.perm_addr, netdev->addr_len);
10252 
10253 	if (!is_valid_ether_addr(netdev->dev_addr)) {
10254 		e_dev_err("invalid MAC address\n");
10255 		err = -EIO;
10256 		goto err_sw_init;
10257 	}
10258 
10259 	/* Set hw->mac.addr to permanent MAC address */
10260 	ether_addr_copy(hw->mac.addr, hw->mac.perm_addr);
10261 	ixgbe_mac_set_default_filter(adapter);
10262 
10263 	setup_timer(&adapter->service_timer, &ixgbe_service_timer,
10264 		    (unsigned long) adapter);
10265 
10266 	if (ixgbe_removed(hw->hw_addr)) {
10267 		err = -EIO;
10268 		goto err_sw_init;
10269 	}
10270 	INIT_WORK(&adapter->service_task, ixgbe_service_task);
10271 	set_bit(__IXGBE_SERVICE_INITED, &adapter->state);
10272 	clear_bit(__IXGBE_SERVICE_SCHED, &adapter->state);
10273 
10274 	err = ixgbe_init_interrupt_scheme(adapter);
10275 	if (err)
10276 		goto err_sw_init;
10277 
10278 	for (i = 0; i < adapter->num_xdp_queues; i++)
10279 		u64_stats_init(&adapter->xdp_ring[i]->syncp);
10280 
10281 	/* WOL not supported for all devices */
10282 	adapter->wol = 0;
10283 	hw->eeprom.ops.read(hw, 0x2c, &adapter->eeprom_cap);
10284 	hw->wol_enabled = ixgbe_wol_supported(adapter, pdev->device,
10285 						pdev->subsystem_device);
10286 	if (hw->wol_enabled)
10287 		adapter->wol = IXGBE_WUFC_MAG;
10288 
10289 	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
10290 
10291 	/* save off EEPROM version number */
10292 	hw->eeprom.ops.read(hw, 0x2e, &adapter->eeprom_verh);
10293 	hw->eeprom.ops.read(hw, 0x2d, &adapter->eeprom_verl);
10294 
10295 	/* pick up the PCI bus settings for reporting later */
10296 	if (ixgbe_pcie_from_parent(hw))
10297 		ixgbe_get_parent_bus_info(adapter);
10298 	else
10299 		 hw->mac.ops.get_bus_info(hw);
10300 
10301 	/* calculate the expected PCIe bandwidth required for optimal
10302 	 * performance. Note that some older parts will never have enough
10303 	 * bandwidth due to being older generation PCIe parts. We clamp these
10304 	 * parts to ensure no warning is displayed if it can't be fixed.
10305 	 */
10306 	switch (hw->mac.type) {
10307 	case ixgbe_mac_82598EB:
10308 		expected_gts = min(ixgbe_enumerate_functions(adapter) * 10, 16);
10309 		break;
10310 	default:
10311 		expected_gts = ixgbe_enumerate_functions(adapter) * 10;
10312 		break;
10313 	}
10314 
10315 	/* don't check link if we failed to enumerate functions */
10316 	if (expected_gts > 0)
10317 		ixgbe_check_minimum_link(adapter, expected_gts);
10318 
10319 	err = ixgbe_read_pba_string_generic(hw, part_str, sizeof(part_str));
10320 	if (err)
10321 		strlcpy(part_str, "Unknown", sizeof(part_str));
10322 	if (ixgbe_is_sfp(hw) && hw->phy.sfp_type != ixgbe_sfp_type_not_present)
10323 		e_dev_info("MAC: %d, PHY: %d, SFP+: %d, PBA No: %s\n",
10324 			   hw->mac.type, hw->phy.type, hw->phy.sfp_type,
10325 			   part_str);
10326 	else
10327 		e_dev_info("MAC: %d, PHY: %d, PBA No: %s\n",
10328 			   hw->mac.type, hw->phy.type, part_str);
10329 
10330 	e_dev_info("%pM\n", netdev->dev_addr);
10331 
10332 	/* reset the hardware with the new settings */
10333 	err = hw->mac.ops.start_hw(hw);
10334 	if (err == IXGBE_ERR_EEPROM_VERSION) {
10335 		/* We are running on a pre-production device, log a warning */
10336 		e_dev_warn("This device is a pre-production adapter/LOM. "
10337 			   "Please be aware there may be issues associated "
10338 			   "with your hardware.  If you are experiencing "
10339 			   "problems please contact your Intel or hardware "
10340 			   "representative who provided you with this "
10341 			   "hardware.\n");
10342 	}
10343 	strcpy(netdev->name, "eth%d");
10344 	err = register_netdev(netdev);
10345 	if (err)
10346 		goto err_register;
10347 
10348 	pci_set_drvdata(pdev, adapter);
10349 
10350 	/* power down the optics for 82599 SFP+ fiber */
10351 	if (hw->mac.ops.disable_tx_laser)
10352 		hw->mac.ops.disable_tx_laser(hw);
10353 
10354 	/* carrier off reporting is important to ethtool even BEFORE open */
10355 	netif_carrier_off(netdev);
10356 
10357 #ifdef CONFIG_IXGBE_DCA
10358 	if (dca_add_requester(&pdev->dev) == 0) {
10359 		adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
10360 		ixgbe_setup_dca(adapter);
10361 	}
10362 #endif
10363 	if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
10364 		e_info(probe, "IOV is enabled with %d VFs\n", adapter->num_vfs);
10365 		for (i = 0; i < adapter->num_vfs; i++)
10366 			ixgbe_vf_configuration(pdev, (i | 0x10000000));
10367 	}
10368 
10369 	/* firmware requires driver version to be 0xFFFFFFFF
10370 	 * since os does not support feature
10371 	 */
10372 	if (hw->mac.ops.set_fw_drv_ver)
10373 		hw->mac.ops.set_fw_drv_ver(hw, 0xFF, 0xFF, 0xFF, 0xFF,
10374 					   sizeof(ixgbe_driver_version) - 1,
10375 					   ixgbe_driver_version);
10376 
10377 	/* add san mac addr to netdev */
10378 	ixgbe_add_sanmac_netdev(netdev);
10379 
10380 	e_dev_info("%s\n", ixgbe_default_device_descr);
10381 
10382 #ifdef CONFIG_IXGBE_HWMON
10383 	if (ixgbe_sysfs_init(adapter))
10384 		e_err(probe, "failed to allocate sysfs resources\n");
10385 #endif /* CONFIG_IXGBE_HWMON */
10386 
10387 	ixgbe_dbg_adapter_init(adapter);
10388 
10389 	/* setup link for SFP devices with MNG FW, else wait for IXGBE_UP */
10390 	if (ixgbe_mng_enabled(hw) && ixgbe_is_sfp(hw) && hw->mac.ops.setup_link)
10391 		hw->mac.ops.setup_link(hw,
10392 			IXGBE_LINK_SPEED_10GB_FULL | IXGBE_LINK_SPEED_1GB_FULL,
10393 			true);
10394 
10395 	return 0;
10396 
10397 err_register:
10398 	ixgbe_release_hw_control(adapter);
10399 	ixgbe_clear_interrupt_scheme(adapter);
10400 err_sw_init:
10401 	ixgbe_disable_sriov(adapter);
10402 	adapter->flags2 &= ~IXGBE_FLAG2_SEARCH_FOR_SFP;
10403 	iounmap(adapter->io_addr);
10404 	kfree(adapter->jump_tables[0]);
10405 	kfree(adapter->mac_table);
10406 	kfree(adapter->rss_key);
10407 err_ioremap:
10408 	disable_dev = !test_and_set_bit(__IXGBE_DISABLED, &adapter->state);
10409 	free_netdev(netdev);
10410 err_alloc_etherdev:
10411 	pci_release_mem_regions(pdev);
10412 err_pci_reg:
10413 err_dma:
10414 	if (!adapter || disable_dev)
10415 		pci_disable_device(pdev);
10416 	return err;
10417 }
10418 
10419 /**
10420  * ixgbe_remove - Device Removal Routine
10421  * @pdev: PCI device information struct
10422  *
10423  * ixgbe_remove is called by the PCI subsystem to alert the driver
10424  * that it should release a PCI device.  The could be caused by a
10425  * Hot-Plug event, or because the driver is going to be removed from
10426  * memory.
10427  **/
10428 static void ixgbe_remove(struct pci_dev *pdev)
10429 {
10430 	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
10431 	struct net_device *netdev;
10432 	bool disable_dev;
10433 	int i;
10434 
10435 	/* if !adapter then we already cleaned up in probe */
10436 	if (!adapter)
10437 		return;
10438 
10439 	netdev  = adapter->netdev;
10440 	ixgbe_dbg_adapter_exit(adapter);
10441 
10442 	set_bit(__IXGBE_REMOVING, &adapter->state);
10443 	cancel_work_sync(&adapter->service_task);
10444 
10445 
10446 #ifdef CONFIG_IXGBE_DCA
10447 	if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
10448 		adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
10449 		dca_remove_requester(&pdev->dev);
10450 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL,
10451 				IXGBE_DCA_CTRL_DCA_DISABLE);
10452 	}
10453 
10454 #endif
10455 #ifdef CONFIG_IXGBE_HWMON
10456 	ixgbe_sysfs_exit(adapter);
10457 #endif /* CONFIG_IXGBE_HWMON */
10458 
10459 	/* remove the added san mac */
10460 	ixgbe_del_sanmac_netdev(netdev);
10461 
10462 #ifdef CONFIG_PCI_IOV
10463 	ixgbe_disable_sriov(adapter);
10464 #endif
10465 	if (netdev->reg_state == NETREG_REGISTERED)
10466 		unregister_netdev(netdev);
10467 
10468 	ixgbe_clear_interrupt_scheme(adapter);
10469 
10470 	ixgbe_release_hw_control(adapter);
10471 
10472 #ifdef CONFIG_DCB
10473 	kfree(adapter->ixgbe_ieee_pfc);
10474 	kfree(adapter->ixgbe_ieee_ets);
10475 
10476 #endif
10477 	iounmap(adapter->io_addr);
10478 	pci_release_mem_regions(pdev);
10479 
10480 	e_dev_info("complete\n");
10481 
10482 	for (i = 0; i < IXGBE_MAX_LINK_HANDLE; i++) {
10483 		if (adapter->jump_tables[i]) {
10484 			kfree(adapter->jump_tables[i]->input);
10485 			kfree(adapter->jump_tables[i]->mask);
10486 		}
10487 		kfree(adapter->jump_tables[i]);
10488 	}
10489 
10490 	kfree(adapter->mac_table);
10491 	kfree(adapter->rss_key);
10492 	disable_dev = !test_and_set_bit(__IXGBE_DISABLED, &adapter->state);
10493 	free_netdev(netdev);
10494 
10495 	pci_disable_pcie_error_reporting(pdev);
10496 
10497 	if (disable_dev)
10498 		pci_disable_device(pdev);
10499 }
10500 
10501 /**
10502  * ixgbe_io_error_detected - called when PCI error is detected
10503  * @pdev: Pointer to PCI device
10504  * @state: The current pci connection state
10505  *
10506  * This function is called after a PCI bus error affecting
10507  * this device has been detected.
10508  */
10509 static pci_ers_result_t ixgbe_io_error_detected(struct pci_dev *pdev,
10510 						pci_channel_state_t state)
10511 {
10512 	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
10513 	struct net_device *netdev = adapter->netdev;
10514 
10515 #ifdef CONFIG_PCI_IOV
10516 	struct ixgbe_hw *hw = &adapter->hw;
10517 	struct pci_dev *bdev, *vfdev;
10518 	u32 dw0, dw1, dw2, dw3;
10519 	int vf, pos;
10520 	u16 req_id, pf_func;
10521 
10522 	if (adapter->hw.mac.type == ixgbe_mac_82598EB ||
10523 	    adapter->num_vfs == 0)
10524 		goto skip_bad_vf_detection;
10525 
10526 	bdev = pdev->bus->self;
10527 	while (bdev && (pci_pcie_type(bdev) != PCI_EXP_TYPE_ROOT_PORT))
10528 		bdev = bdev->bus->self;
10529 
10530 	if (!bdev)
10531 		goto skip_bad_vf_detection;
10532 
10533 	pos = pci_find_ext_capability(bdev, PCI_EXT_CAP_ID_ERR);
10534 	if (!pos)
10535 		goto skip_bad_vf_detection;
10536 
10537 	dw0 = ixgbe_read_pci_cfg_dword(hw, pos + PCI_ERR_HEADER_LOG);
10538 	dw1 = ixgbe_read_pci_cfg_dword(hw, pos + PCI_ERR_HEADER_LOG + 4);
10539 	dw2 = ixgbe_read_pci_cfg_dword(hw, pos + PCI_ERR_HEADER_LOG + 8);
10540 	dw3 = ixgbe_read_pci_cfg_dword(hw, pos + PCI_ERR_HEADER_LOG + 12);
10541 	if (ixgbe_removed(hw->hw_addr))
10542 		goto skip_bad_vf_detection;
10543 
10544 	req_id = dw1 >> 16;
10545 	/* On the 82599 if bit 7 of the requestor ID is set then it's a VF */
10546 	if (!(req_id & 0x0080))
10547 		goto skip_bad_vf_detection;
10548 
10549 	pf_func = req_id & 0x01;
10550 	if ((pf_func & 1) == (pdev->devfn & 1)) {
10551 		unsigned int device_id;
10552 
10553 		vf = (req_id & 0x7F) >> 1;
10554 		e_dev_err("VF %d has caused a PCIe error\n", vf);
10555 		e_dev_err("TLP: dw0: %8.8x\tdw1: %8.8x\tdw2: "
10556 				"%8.8x\tdw3: %8.8x\n",
10557 		dw0, dw1, dw2, dw3);
10558 		switch (adapter->hw.mac.type) {
10559 		case ixgbe_mac_82599EB:
10560 			device_id = IXGBE_82599_VF_DEVICE_ID;
10561 			break;
10562 		case ixgbe_mac_X540:
10563 			device_id = IXGBE_X540_VF_DEVICE_ID;
10564 			break;
10565 		case ixgbe_mac_X550:
10566 			device_id = IXGBE_DEV_ID_X550_VF;
10567 			break;
10568 		case ixgbe_mac_X550EM_x:
10569 			device_id = IXGBE_DEV_ID_X550EM_X_VF;
10570 			break;
10571 		case ixgbe_mac_x550em_a:
10572 			device_id = IXGBE_DEV_ID_X550EM_A_VF;
10573 			break;
10574 		default:
10575 			device_id = 0;
10576 			break;
10577 		}
10578 
10579 		/* Find the pci device of the offending VF */
10580 		vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, device_id, NULL);
10581 		while (vfdev) {
10582 			if (vfdev->devfn == (req_id & 0xFF))
10583 				break;
10584 			vfdev = pci_get_device(PCI_VENDOR_ID_INTEL,
10585 					       device_id, vfdev);
10586 		}
10587 		/*
10588 		 * There's a slim chance the VF could have been hot plugged,
10589 		 * so if it is no longer present we don't need to issue the
10590 		 * VFLR.  Just clean up the AER in that case.
10591 		 */
10592 		if (vfdev) {
10593 			pcie_flr(vfdev);
10594 			/* Free device reference count */
10595 			pci_dev_put(vfdev);
10596 		}
10597 
10598 		pci_cleanup_aer_uncorrect_error_status(pdev);
10599 	}
10600 
10601 	/*
10602 	 * Even though the error may have occurred on the other port
10603 	 * we still need to increment the vf error reference count for
10604 	 * both ports because the I/O resume function will be called
10605 	 * for both of them.
10606 	 */
10607 	adapter->vferr_refcount++;
10608 
10609 	return PCI_ERS_RESULT_RECOVERED;
10610 
10611 skip_bad_vf_detection:
10612 #endif /* CONFIG_PCI_IOV */
10613 	if (!test_bit(__IXGBE_SERVICE_INITED, &adapter->state))
10614 		return PCI_ERS_RESULT_DISCONNECT;
10615 
10616 	rtnl_lock();
10617 	netif_device_detach(netdev);
10618 
10619 	if (state == pci_channel_io_perm_failure) {
10620 		rtnl_unlock();
10621 		return PCI_ERS_RESULT_DISCONNECT;
10622 	}
10623 
10624 	if (netif_running(netdev))
10625 		ixgbe_close_suspend(adapter);
10626 
10627 	if (!test_and_set_bit(__IXGBE_DISABLED, &adapter->state))
10628 		pci_disable_device(pdev);
10629 	rtnl_unlock();
10630 
10631 	/* Request a slot reset. */
10632 	return PCI_ERS_RESULT_NEED_RESET;
10633 }
10634 
10635 /**
10636  * ixgbe_io_slot_reset - called after the pci bus has been reset.
10637  * @pdev: Pointer to PCI device
10638  *
10639  * Restart the card from scratch, as if from a cold-boot.
10640  */
10641 static pci_ers_result_t ixgbe_io_slot_reset(struct pci_dev *pdev)
10642 {
10643 	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
10644 	pci_ers_result_t result;
10645 	int err;
10646 
10647 	if (pci_enable_device_mem(pdev)) {
10648 		e_err(probe, "Cannot re-enable PCI device after reset.\n");
10649 		result = PCI_ERS_RESULT_DISCONNECT;
10650 	} else {
10651 		smp_mb__before_atomic();
10652 		clear_bit(__IXGBE_DISABLED, &adapter->state);
10653 		adapter->hw.hw_addr = adapter->io_addr;
10654 		pci_set_master(pdev);
10655 		pci_restore_state(pdev);
10656 		pci_save_state(pdev);
10657 
10658 		pci_wake_from_d3(pdev, false);
10659 
10660 		ixgbe_reset(adapter);
10661 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_WUS, ~0);
10662 		result = PCI_ERS_RESULT_RECOVERED;
10663 	}
10664 
10665 	err = pci_cleanup_aer_uncorrect_error_status(pdev);
10666 	if (err) {
10667 		e_dev_err("pci_cleanup_aer_uncorrect_error_status "
10668 			  "failed 0x%0x\n", err);
10669 		/* non-fatal, continue */
10670 	}
10671 
10672 	return result;
10673 }
10674 
10675 /**
10676  * ixgbe_io_resume - called when traffic can start flowing again.
10677  * @pdev: Pointer to PCI device
10678  *
10679  * This callback is called when the error recovery driver tells us that
10680  * its OK to resume normal operation.
10681  */
10682 static void ixgbe_io_resume(struct pci_dev *pdev)
10683 {
10684 	struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
10685 	struct net_device *netdev = adapter->netdev;
10686 
10687 #ifdef CONFIG_PCI_IOV
10688 	if (adapter->vferr_refcount) {
10689 		e_info(drv, "Resuming after VF err\n");
10690 		adapter->vferr_refcount--;
10691 		return;
10692 	}
10693 
10694 #endif
10695 	rtnl_lock();
10696 	if (netif_running(netdev))
10697 		ixgbe_open(netdev);
10698 
10699 	netif_device_attach(netdev);
10700 	rtnl_unlock();
10701 }
10702 
10703 static const struct pci_error_handlers ixgbe_err_handler = {
10704 	.error_detected = ixgbe_io_error_detected,
10705 	.slot_reset = ixgbe_io_slot_reset,
10706 	.resume = ixgbe_io_resume,
10707 };
10708 
10709 static struct pci_driver ixgbe_driver = {
10710 	.name     = ixgbe_driver_name,
10711 	.id_table = ixgbe_pci_tbl,
10712 	.probe    = ixgbe_probe,
10713 	.remove   = ixgbe_remove,
10714 #ifdef CONFIG_PM
10715 	.suspend  = ixgbe_suspend,
10716 	.resume   = ixgbe_resume,
10717 #endif
10718 	.shutdown = ixgbe_shutdown,
10719 	.sriov_configure = ixgbe_pci_sriov_configure,
10720 	.err_handler = &ixgbe_err_handler
10721 };
10722 
10723 /**
10724  * ixgbe_init_module - Driver Registration Routine
10725  *
10726  * ixgbe_init_module is the first routine called when the driver is
10727  * loaded. All it does is register with the PCI subsystem.
10728  **/
10729 static int __init ixgbe_init_module(void)
10730 {
10731 	int ret;
10732 	pr_info("%s - version %s\n", ixgbe_driver_string, ixgbe_driver_version);
10733 	pr_info("%s\n", ixgbe_copyright);
10734 
10735 	ixgbe_wq = create_singlethread_workqueue(ixgbe_driver_name);
10736 	if (!ixgbe_wq) {
10737 		pr_err("%s: Failed to create workqueue\n", ixgbe_driver_name);
10738 		return -ENOMEM;
10739 	}
10740 
10741 	ixgbe_dbg_init();
10742 
10743 	ret = pci_register_driver(&ixgbe_driver);
10744 	if (ret) {
10745 		destroy_workqueue(ixgbe_wq);
10746 		ixgbe_dbg_exit();
10747 		return ret;
10748 	}
10749 
10750 #ifdef CONFIG_IXGBE_DCA
10751 	dca_register_notify(&dca_notifier);
10752 #endif
10753 
10754 	return 0;
10755 }
10756 
10757 module_init(ixgbe_init_module);
10758 
10759 /**
10760  * ixgbe_exit_module - Driver Exit Cleanup Routine
10761  *
10762  * ixgbe_exit_module is called just before the driver is removed
10763  * from memory.
10764  **/
10765 static void __exit ixgbe_exit_module(void)
10766 {
10767 #ifdef CONFIG_IXGBE_DCA
10768 	dca_unregister_notify(&dca_notifier);
10769 #endif
10770 	pci_unregister_driver(&ixgbe_driver);
10771 
10772 	ixgbe_dbg_exit();
10773 	if (ixgbe_wq) {
10774 		destroy_workqueue(ixgbe_wq);
10775 		ixgbe_wq = NULL;
10776 	}
10777 }
10778 
10779 #ifdef CONFIG_IXGBE_DCA
10780 static int ixgbe_notify_dca(struct notifier_block *nb, unsigned long event,
10781 			    void *p)
10782 {
10783 	int ret_val;
10784 
10785 	ret_val = driver_for_each_device(&ixgbe_driver.driver, NULL, &event,
10786 					 __ixgbe_notify_dca);
10787 
10788 	return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
10789 }
10790 
10791 #endif /* CONFIG_IXGBE_DCA */
10792 
10793 module_exit(ixgbe_exit_module);
10794 
10795 /* ixgbe_main.c */
10796