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