1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
86 	/* required last entry */
87 	{0, }
88 };
89 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
90 
91 #define I40E_MAX_VF_COUNT 128
92 static int debug = -1;
93 module_param(debug, uint, 0);
94 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
95 
96 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
97 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
98 MODULE_LICENSE("GPL v2");
99 
100 static struct workqueue_struct *i40e_wq;
101 
102 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
103 				  struct net_device *netdev, int delta)
104 {
105 	struct netdev_hw_addr *ha;
106 
107 	if (!f || !netdev)
108 		return;
109 
110 	netdev_for_each_mc_addr(ha, netdev) {
111 		if (ether_addr_equal(ha->addr, f->macaddr)) {
112 			ha->refcount += delta;
113 			if (ha->refcount <= 0)
114 				ha->refcount = 1;
115 			break;
116 		}
117 	}
118 }
119 
120 /**
121  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
122  * @hw:   pointer to the HW structure
123  * @mem:  ptr to mem struct to fill out
124  * @size: size of memory requested
125  * @alignment: what to align the allocation to
126  **/
127 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
128 			    u64 size, u32 alignment)
129 {
130 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
131 
132 	mem->size = ALIGN(size, alignment);
133 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
134 				     GFP_KERNEL);
135 	if (!mem->va)
136 		return -ENOMEM;
137 
138 	return 0;
139 }
140 
141 /**
142  * i40e_free_dma_mem_d - OS specific memory free for shared code
143  * @hw:   pointer to the HW structure
144  * @mem:  ptr to mem struct to free
145  **/
146 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
147 {
148 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
149 
150 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
151 	mem->va = NULL;
152 	mem->pa = 0;
153 	mem->size = 0;
154 
155 	return 0;
156 }
157 
158 /**
159  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
160  * @hw:   pointer to the HW structure
161  * @mem:  ptr to mem struct to fill out
162  * @size: size of memory requested
163  **/
164 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
165 			     u32 size)
166 {
167 	mem->size = size;
168 	mem->va = kzalloc(size, GFP_KERNEL);
169 
170 	if (!mem->va)
171 		return -ENOMEM;
172 
173 	return 0;
174 }
175 
176 /**
177  * i40e_free_virt_mem_d - OS specific memory free for shared code
178  * @hw:   pointer to the HW structure
179  * @mem:  ptr to mem struct to free
180  **/
181 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
182 {
183 	/* it's ok to kfree a NULL pointer */
184 	kfree(mem->va);
185 	mem->va = NULL;
186 	mem->size = 0;
187 
188 	return 0;
189 }
190 
191 /**
192  * i40e_get_lump - find a lump of free generic resource
193  * @pf: board private structure
194  * @pile: the pile of resource to search
195  * @needed: the number of items needed
196  * @id: an owner id to stick on the items assigned
197  *
198  * Returns the base item index of the lump, or negative for error
199  **/
200 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
201 			 u16 needed, u16 id)
202 {
203 	int ret = -ENOMEM;
204 	int i, j;
205 
206 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
207 		dev_info(&pf->pdev->dev,
208 			 "param err: pile=%s needed=%d id=0x%04x\n",
209 			 pile ? "<valid>" : "<null>", needed, id);
210 		return -EINVAL;
211 	}
212 
213 	/* Allocate last queue in the pile for FDIR VSI queue
214 	 * so it doesn't fragment the qp_pile
215 	 */
216 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
217 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
218 			dev_err(&pf->pdev->dev,
219 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
220 				pile->num_entries - 1);
221 			return -ENOMEM;
222 		}
223 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
224 		return pile->num_entries - 1;
225 	}
226 
227 	i = 0;
228 	while (i < pile->num_entries) {
229 		/* skip already allocated entries */
230 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
231 			i++;
232 			continue;
233 		}
234 
235 		/* do we have enough in this lump? */
236 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
237 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
238 				break;
239 		}
240 
241 		if (j == needed) {
242 			/* there was enough, so assign it to the requestor */
243 			for (j = 0; j < needed; j++)
244 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
245 			ret = i;
246 			break;
247 		}
248 
249 		/* not enough, so skip over it and continue looking */
250 		i += j;
251 	}
252 
253 	return ret;
254 }
255 
256 /**
257  * i40e_put_lump - return a lump of generic resource
258  * @pile: the pile of resource to search
259  * @index: the base item index
260  * @id: the owner id of the items assigned
261  *
262  * Returns the count of items in the lump
263  **/
264 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
265 {
266 	int valid_id = (id | I40E_PILE_VALID_BIT);
267 	int count = 0;
268 	u16 i;
269 
270 	if (!pile || index >= pile->num_entries)
271 		return -EINVAL;
272 
273 	for (i = index;
274 	     i < pile->num_entries && pile->list[i] == valid_id;
275 	     i++) {
276 		pile->list[i] = 0;
277 		count++;
278 	}
279 
280 
281 	return count;
282 }
283 
284 /**
285  * i40e_find_vsi_from_id - searches for the vsi with the given id
286  * @pf: the pf structure to search for the vsi
287  * @id: id of the vsi it is searching for
288  **/
289 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
290 {
291 	int i;
292 
293 	for (i = 0; i < pf->num_alloc_vsi; i++)
294 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
295 			return pf->vsi[i];
296 
297 	return NULL;
298 }
299 
300 /**
301  * i40e_service_event_schedule - Schedule the service task to wake up
302  * @pf: board private structure
303  *
304  * If not already scheduled, this puts the task into the work queue
305  **/
306 void i40e_service_event_schedule(struct i40e_pf *pf)
307 {
308 	if ((!test_bit(__I40E_DOWN, pf->state) &&
309 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
310 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
311 		queue_work(i40e_wq, &pf->service_task);
312 }
313 
314 /**
315  * i40e_tx_timeout - Respond to a Tx Hang
316  * @netdev: network interface device structure
317  * @txqueue: queue number timing out
318  *
319  * If any port has noticed a Tx timeout, it is likely that the whole
320  * device is munged, not just the one netdev port, so go for the full
321  * reset.
322  **/
323 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
324 {
325 	struct i40e_netdev_priv *np = netdev_priv(netdev);
326 	struct i40e_vsi *vsi = np->vsi;
327 	struct i40e_pf *pf = vsi->back;
328 	struct i40e_ring *tx_ring = NULL;
329 	unsigned int i;
330 	u32 head, val;
331 
332 	pf->tx_timeout_count++;
333 
334 	/* with txqueue index, find the tx_ring struct */
335 	for (i = 0; i < vsi->num_queue_pairs; i++) {
336 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
337 			if (txqueue ==
338 			    vsi->tx_rings[i]->queue_index) {
339 				tx_ring = vsi->tx_rings[i];
340 				break;
341 			}
342 		}
343 	}
344 
345 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
346 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
347 	else if (time_before(jiffies,
348 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
349 		return;   /* don't do any new action before the next timeout */
350 
351 	/* don't kick off another recovery if one is already pending */
352 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
353 		return;
354 
355 	if (tx_ring) {
356 		head = i40e_get_head(tx_ring);
357 		/* Read interrupt register */
358 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
359 			val = rd32(&pf->hw,
360 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
361 						tx_ring->vsi->base_vector - 1));
362 		else
363 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
364 
365 		netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
366 			    vsi->seid, txqueue, tx_ring->next_to_clean,
367 			    head, tx_ring->next_to_use,
368 			    readl(tx_ring->tail), val);
369 	}
370 
371 	pf->tx_timeout_last_recovery = jiffies;
372 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
373 		    pf->tx_timeout_recovery_level, txqueue);
374 
375 	switch (pf->tx_timeout_recovery_level) {
376 	case 1:
377 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
378 		break;
379 	case 2:
380 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
381 		break;
382 	case 3:
383 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
384 		break;
385 	default:
386 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
387 		break;
388 	}
389 
390 	i40e_service_event_schedule(pf);
391 	pf->tx_timeout_recovery_level++;
392 }
393 
394 /**
395  * i40e_get_vsi_stats_struct - Get System Network Statistics
396  * @vsi: the VSI we care about
397  *
398  * Returns the address of the device statistics structure.
399  * The statistics are actually updated from the service task.
400  **/
401 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
402 {
403 	return &vsi->net_stats;
404 }
405 
406 /**
407  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
408  * @ring: Tx ring to get statistics from
409  * @stats: statistics entry to be updated
410  **/
411 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
412 					    struct rtnl_link_stats64 *stats)
413 {
414 	u64 bytes, packets;
415 	unsigned int start;
416 
417 	do {
418 		start = u64_stats_fetch_begin_irq(&ring->syncp);
419 		packets = ring->stats.packets;
420 		bytes   = ring->stats.bytes;
421 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
422 
423 	stats->tx_packets += packets;
424 	stats->tx_bytes   += bytes;
425 }
426 
427 /**
428  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
429  * @netdev: network interface device structure
430  * @stats: data structure to store statistics
431  *
432  * Returns the address of the device statistics structure.
433  * The statistics are actually updated from the service task.
434  **/
435 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
436 				  struct rtnl_link_stats64 *stats)
437 {
438 	struct i40e_netdev_priv *np = netdev_priv(netdev);
439 	struct i40e_vsi *vsi = np->vsi;
440 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
441 	struct i40e_ring *ring;
442 	int i;
443 
444 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
445 		return;
446 
447 	if (!vsi->tx_rings)
448 		return;
449 
450 	rcu_read_lock();
451 	for (i = 0; i < vsi->num_queue_pairs; i++) {
452 		u64 bytes, packets;
453 		unsigned int start;
454 
455 		ring = READ_ONCE(vsi->tx_rings[i]);
456 		if (!ring)
457 			continue;
458 		i40e_get_netdev_stats_struct_tx(ring, stats);
459 
460 		if (i40e_enabled_xdp_vsi(vsi)) {
461 			ring = READ_ONCE(vsi->xdp_rings[i]);
462 			if (!ring)
463 				continue;
464 			i40e_get_netdev_stats_struct_tx(ring, stats);
465 		}
466 
467 		ring = READ_ONCE(vsi->rx_rings[i]);
468 		if (!ring)
469 			continue;
470 		do {
471 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
472 			packets = ring->stats.packets;
473 			bytes   = ring->stats.bytes;
474 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
475 
476 		stats->rx_packets += packets;
477 		stats->rx_bytes   += bytes;
478 
479 	}
480 	rcu_read_unlock();
481 
482 	/* following stats updated by i40e_watchdog_subtask() */
483 	stats->multicast	= vsi_stats->multicast;
484 	stats->tx_errors	= vsi_stats->tx_errors;
485 	stats->tx_dropped	= vsi_stats->tx_dropped;
486 	stats->rx_errors	= vsi_stats->rx_errors;
487 	stats->rx_dropped	= vsi_stats->rx_dropped;
488 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
489 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
490 }
491 
492 /**
493  * i40e_vsi_reset_stats - Resets all stats of the given vsi
494  * @vsi: the VSI to have its stats reset
495  **/
496 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
497 {
498 	struct rtnl_link_stats64 *ns;
499 	int i;
500 
501 	if (!vsi)
502 		return;
503 
504 	ns = i40e_get_vsi_stats_struct(vsi);
505 	memset(ns, 0, sizeof(*ns));
506 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
507 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
508 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
509 	if (vsi->rx_rings && vsi->rx_rings[0]) {
510 		for (i = 0; i < vsi->num_queue_pairs; i++) {
511 			memset(&vsi->rx_rings[i]->stats, 0,
512 			       sizeof(vsi->rx_rings[i]->stats));
513 			memset(&vsi->rx_rings[i]->rx_stats, 0,
514 			       sizeof(vsi->rx_rings[i]->rx_stats));
515 			memset(&vsi->tx_rings[i]->stats, 0,
516 			       sizeof(vsi->tx_rings[i]->stats));
517 			memset(&vsi->tx_rings[i]->tx_stats, 0,
518 			       sizeof(vsi->tx_rings[i]->tx_stats));
519 		}
520 	}
521 	vsi->stat_offsets_loaded = false;
522 }
523 
524 /**
525  * i40e_pf_reset_stats - Reset all of the stats for the given PF
526  * @pf: the PF to be reset
527  **/
528 void i40e_pf_reset_stats(struct i40e_pf *pf)
529 {
530 	int i;
531 
532 	memset(&pf->stats, 0, sizeof(pf->stats));
533 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
534 	pf->stat_offsets_loaded = false;
535 
536 	for (i = 0; i < I40E_MAX_VEB; i++) {
537 		if (pf->veb[i]) {
538 			memset(&pf->veb[i]->stats, 0,
539 			       sizeof(pf->veb[i]->stats));
540 			memset(&pf->veb[i]->stats_offsets, 0,
541 			       sizeof(pf->veb[i]->stats_offsets));
542 			memset(&pf->veb[i]->tc_stats, 0,
543 			       sizeof(pf->veb[i]->tc_stats));
544 			memset(&pf->veb[i]->tc_stats_offsets, 0,
545 			       sizeof(pf->veb[i]->tc_stats_offsets));
546 			pf->veb[i]->stat_offsets_loaded = false;
547 		}
548 	}
549 	pf->hw_csum_rx_error = 0;
550 }
551 
552 /**
553  * i40e_stat_update48 - read and update a 48 bit stat from the chip
554  * @hw: ptr to the hardware info
555  * @hireg: the high 32 bit reg to read
556  * @loreg: the low 32 bit reg to read
557  * @offset_loaded: has the initial offset been loaded yet
558  * @offset: ptr to current offset value
559  * @stat: ptr to the stat
560  *
561  * Since the device stats are not reset at PFReset, they likely will not
562  * be zeroed when the driver starts.  We'll save the first values read
563  * and use them as offsets to be subtracted from the raw values in order
564  * to report stats that count from zero.  In the process, we also manage
565  * the potential roll-over.
566  **/
567 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
568 			       bool offset_loaded, u64 *offset, u64 *stat)
569 {
570 	u64 new_data;
571 
572 	if (hw->device_id == I40E_DEV_ID_QEMU) {
573 		new_data = rd32(hw, loreg);
574 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
575 	} else {
576 		new_data = rd64(hw, loreg);
577 	}
578 	if (!offset_loaded)
579 		*offset = new_data;
580 	if (likely(new_data >= *offset))
581 		*stat = new_data - *offset;
582 	else
583 		*stat = (new_data + BIT_ULL(48)) - *offset;
584 	*stat &= 0xFFFFFFFFFFFFULL;
585 }
586 
587 /**
588  * i40e_stat_update32 - read and update a 32 bit stat from the chip
589  * @hw: ptr to the hardware info
590  * @reg: the hw reg to read
591  * @offset_loaded: has the initial offset been loaded yet
592  * @offset: ptr to current offset value
593  * @stat: ptr to the stat
594  **/
595 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
596 			       bool offset_loaded, u64 *offset, u64 *stat)
597 {
598 	u32 new_data;
599 
600 	new_data = rd32(hw, reg);
601 	if (!offset_loaded)
602 		*offset = new_data;
603 	if (likely(new_data >= *offset))
604 		*stat = (u32)(new_data - *offset);
605 	else
606 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
607 }
608 
609 /**
610  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
611  * @hw: ptr to the hardware info
612  * @reg: the hw reg to read and clear
613  * @stat: ptr to the stat
614  **/
615 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
616 {
617 	u32 new_data = rd32(hw, reg);
618 
619 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
620 	*stat += new_data;
621 }
622 
623 /**
624  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
625  * @vsi: the VSI to be updated
626  **/
627 void i40e_update_eth_stats(struct i40e_vsi *vsi)
628 {
629 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
630 	struct i40e_pf *pf = vsi->back;
631 	struct i40e_hw *hw = &pf->hw;
632 	struct i40e_eth_stats *oes;
633 	struct i40e_eth_stats *es;     /* device's eth stats */
634 
635 	es = &vsi->eth_stats;
636 	oes = &vsi->eth_stats_offsets;
637 
638 	/* Gather up the stats that the hw collects */
639 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
640 			   vsi->stat_offsets_loaded,
641 			   &oes->tx_errors, &es->tx_errors);
642 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
643 			   vsi->stat_offsets_loaded,
644 			   &oes->rx_discards, &es->rx_discards);
645 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
646 			   vsi->stat_offsets_loaded,
647 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
648 
649 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
650 			   I40E_GLV_GORCL(stat_idx),
651 			   vsi->stat_offsets_loaded,
652 			   &oes->rx_bytes, &es->rx_bytes);
653 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
654 			   I40E_GLV_UPRCL(stat_idx),
655 			   vsi->stat_offsets_loaded,
656 			   &oes->rx_unicast, &es->rx_unicast);
657 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
658 			   I40E_GLV_MPRCL(stat_idx),
659 			   vsi->stat_offsets_loaded,
660 			   &oes->rx_multicast, &es->rx_multicast);
661 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
662 			   I40E_GLV_BPRCL(stat_idx),
663 			   vsi->stat_offsets_loaded,
664 			   &oes->rx_broadcast, &es->rx_broadcast);
665 
666 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
667 			   I40E_GLV_GOTCL(stat_idx),
668 			   vsi->stat_offsets_loaded,
669 			   &oes->tx_bytes, &es->tx_bytes);
670 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
671 			   I40E_GLV_UPTCL(stat_idx),
672 			   vsi->stat_offsets_loaded,
673 			   &oes->tx_unicast, &es->tx_unicast);
674 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
675 			   I40E_GLV_MPTCL(stat_idx),
676 			   vsi->stat_offsets_loaded,
677 			   &oes->tx_multicast, &es->tx_multicast);
678 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
679 			   I40E_GLV_BPTCL(stat_idx),
680 			   vsi->stat_offsets_loaded,
681 			   &oes->tx_broadcast, &es->tx_broadcast);
682 	vsi->stat_offsets_loaded = true;
683 }
684 
685 /**
686  * i40e_update_veb_stats - Update Switch component statistics
687  * @veb: the VEB being updated
688  **/
689 void i40e_update_veb_stats(struct i40e_veb *veb)
690 {
691 	struct i40e_pf *pf = veb->pf;
692 	struct i40e_hw *hw = &pf->hw;
693 	struct i40e_eth_stats *oes;
694 	struct i40e_eth_stats *es;     /* device's eth stats */
695 	struct i40e_veb_tc_stats *veb_oes;
696 	struct i40e_veb_tc_stats *veb_es;
697 	int i, idx = 0;
698 
699 	idx = veb->stats_idx;
700 	es = &veb->stats;
701 	oes = &veb->stats_offsets;
702 	veb_es = &veb->tc_stats;
703 	veb_oes = &veb->tc_stats_offsets;
704 
705 	/* Gather up the stats that the hw collects */
706 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
707 			   veb->stat_offsets_loaded,
708 			   &oes->tx_discards, &es->tx_discards);
709 	if (hw->revision_id > 0)
710 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
711 				   veb->stat_offsets_loaded,
712 				   &oes->rx_unknown_protocol,
713 				   &es->rx_unknown_protocol);
714 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
715 			   veb->stat_offsets_loaded,
716 			   &oes->rx_bytes, &es->rx_bytes);
717 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
718 			   veb->stat_offsets_loaded,
719 			   &oes->rx_unicast, &es->rx_unicast);
720 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
721 			   veb->stat_offsets_loaded,
722 			   &oes->rx_multicast, &es->rx_multicast);
723 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
724 			   veb->stat_offsets_loaded,
725 			   &oes->rx_broadcast, &es->rx_broadcast);
726 
727 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
728 			   veb->stat_offsets_loaded,
729 			   &oes->tx_bytes, &es->tx_bytes);
730 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
731 			   veb->stat_offsets_loaded,
732 			   &oes->tx_unicast, &es->tx_unicast);
733 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
734 			   veb->stat_offsets_loaded,
735 			   &oes->tx_multicast, &es->tx_multicast);
736 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
737 			   veb->stat_offsets_loaded,
738 			   &oes->tx_broadcast, &es->tx_broadcast);
739 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
740 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
741 				   I40E_GLVEBTC_RPCL(i, idx),
742 				   veb->stat_offsets_loaded,
743 				   &veb_oes->tc_rx_packets[i],
744 				   &veb_es->tc_rx_packets[i]);
745 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
746 				   I40E_GLVEBTC_RBCL(i, idx),
747 				   veb->stat_offsets_loaded,
748 				   &veb_oes->tc_rx_bytes[i],
749 				   &veb_es->tc_rx_bytes[i]);
750 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
751 				   I40E_GLVEBTC_TPCL(i, idx),
752 				   veb->stat_offsets_loaded,
753 				   &veb_oes->tc_tx_packets[i],
754 				   &veb_es->tc_tx_packets[i]);
755 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
756 				   I40E_GLVEBTC_TBCL(i, idx),
757 				   veb->stat_offsets_loaded,
758 				   &veb_oes->tc_tx_bytes[i],
759 				   &veb_es->tc_tx_bytes[i]);
760 	}
761 	veb->stat_offsets_loaded = true;
762 }
763 
764 /**
765  * i40e_update_vsi_stats - Update the vsi statistics counters.
766  * @vsi: the VSI to be updated
767  *
768  * There are a few instances where we store the same stat in a
769  * couple of different structs.  This is partly because we have
770  * the netdev stats that need to be filled out, which is slightly
771  * different from the "eth_stats" defined by the chip and used in
772  * VF communications.  We sort it out here.
773  **/
774 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
775 {
776 	struct i40e_pf *pf = vsi->back;
777 	struct rtnl_link_stats64 *ons;
778 	struct rtnl_link_stats64 *ns;   /* netdev stats */
779 	struct i40e_eth_stats *oes;
780 	struct i40e_eth_stats *es;     /* device's eth stats */
781 	u64 tx_restart, tx_busy;
782 	struct i40e_ring *p;
783 	u64 rx_page, rx_buf;
784 	u64 bytes, packets;
785 	unsigned int start;
786 	u64 tx_linearize;
787 	u64 tx_force_wb;
788 	u64 rx_p, rx_b;
789 	u64 tx_p, tx_b;
790 	u16 q;
791 
792 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
793 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
794 		return;
795 
796 	ns = i40e_get_vsi_stats_struct(vsi);
797 	ons = &vsi->net_stats_offsets;
798 	es = &vsi->eth_stats;
799 	oes = &vsi->eth_stats_offsets;
800 
801 	/* Gather up the netdev and vsi stats that the driver collects
802 	 * on the fly during packet processing
803 	 */
804 	rx_b = rx_p = 0;
805 	tx_b = tx_p = 0;
806 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
807 	rx_page = 0;
808 	rx_buf = 0;
809 	rcu_read_lock();
810 	for (q = 0; q < vsi->num_queue_pairs; q++) {
811 		/* locate Tx ring */
812 		p = READ_ONCE(vsi->tx_rings[q]);
813 		if (!p)
814 			continue;
815 
816 		do {
817 			start = u64_stats_fetch_begin_irq(&p->syncp);
818 			packets = p->stats.packets;
819 			bytes = p->stats.bytes;
820 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
821 		tx_b += bytes;
822 		tx_p += packets;
823 		tx_restart += p->tx_stats.restart_queue;
824 		tx_busy += p->tx_stats.tx_busy;
825 		tx_linearize += p->tx_stats.tx_linearize;
826 		tx_force_wb += p->tx_stats.tx_force_wb;
827 
828 		/* locate Rx ring */
829 		p = READ_ONCE(vsi->rx_rings[q]);
830 		if (!p)
831 			continue;
832 
833 		do {
834 			start = u64_stats_fetch_begin_irq(&p->syncp);
835 			packets = p->stats.packets;
836 			bytes = p->stats.bytes;
837 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
838 		rx_b += bytes;
839 		rx_p += packets;
840 		rx_buf += p->rx_stats.alloc_buff_failed;
841 		rx_page += p->rx_stats.alloc_page_failed;
842 
843 		if (i40e_enabled_xdp_vsi(vsi)) {
844 			/* locate XDP ring */
845 			p = READ_ONCE(vsi->xdp_rings[q]);
846 			if (!p)
847 				continue;
848 
849 			do {
850 				start = u64_stats_fetch_begin_irq(&p->syncp);
851 				packets = p->stats.packets;
852 				bytes = p->stats.bytes;
853 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
854 			tx_b += bytes;
855 			tx_p += packets;
856 			tx_restart += p->tx_stats.restart_queue;
857 			tx_busy += p->tx_stats.tx_busy;
858 			tx_linearize += p->tx_stats.tx_linearize;
859 			tx_force_wb += p->tx_stats.tx_force_wb;
860 		}
861 	}
862 	rcu_read_unlock();
863 	vsi->tx_restart = tx_restart;
864 	vsi->tx_busy = tx_busy;
865 	vsi->tx_linearize = tx_linearize;
866 	vsi->tx_force_wb = tx_force_wb;
867 	vsi->rx_page_failed = rx_page;
868 	vsi->rx_buf_failed = rx_buf;
869 
870 	ns->rx_packets = rx_p;
871 	ns->rx_bytes = rx_b;
872 	ns->tx_packets = tx_p;
873 	ns->tx_bytes = tx_b;
874 
875 	/* update netdev stats from eth stats */
876 	i40e_update_eth_stats(vsi);
877 	ons->tx_errors = oes->tx_errors;
878 	ns->tx_errors = es->tx_errors;
879 	ons->multicast = oes->rx_multicast;
880 	ns->multicast = es->rx_multicast;
881 	ons->rx_dropped = oes->rx_discards;
882 	ns->rx_dropped = es->rx_discards;
883 	ons->tx_dropped = oes->tx_discards;
884 	ns->tx_dropped = es->tx_discards;
885 
886 	/* pull in a couple PF stats if this is the main vsi */
887 	if (vsi == pf->vsi[pf->lan_vsi]) {
888 		ns->rx_crc_errors = pf->stats.crc_errors;
889 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
890 		ns->rx_length_errors = pf->stats.rx_length_errors;
891 	}
892 }
893 
894 /**
895  * i40e_update_pf_stats - Update the PF statistics counters.
896  * @pf: the PF to be updated
897  **/
898 static void i40e_update_pf_stats(struct i40e_pf *pf)
899 {
900 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
901 	struct i40e_hw_port_stats *nsd = &pf->stats;
902 	struct i40e_hw *hw = &pf->hw;
903 	u32 val;
904 	int i;
905 
906 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
907 			   I40E_GLPRT_GORCL(hw->port),
908 			   pf->stat_offsets_loaded,
909 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
910 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
911 			   I40E_GLPRT_GOTCL(hw->port),
912 			   pf->stat_offsets_loaded,
913 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
914 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
915 			   pf->stat_offsets_loaded,
916 			   &osd->eth.rx_discards,
917 			   &nsd->eth.rx_discards);
918 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
919 			   I40E_GLPRT_UPRCL(hw->port),
920 			   pf->stat_offsets_loaded,
921 			   &osd->eth.rx_unicast,
922 			   &nsd->eth.rx_unicast);
923 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
924 			   I40E_GLPRT_MPRCL(hw->port),
925 			   pf->stat_offsets_loaded,
926 			   &osd->eth.rx_multicast,
927 			   &nsd->eth.rx_multicast);
928 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
929 			   I40E_GLPRT_BPRCL(hw->port),
930 			   pf->stat_offsets_loaded,
931 			   &osd->eth.rx_broadcast,
932 			   &nsd->eth.rx_broadcast);
933 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
934 			   I40E_GLPRT_UPTCL(hw->port),
935 			   pf->stat_offsets_loaded,
936 			   &osd->eth.tx_unicast,
937 			   &nsd->eth.tx_unicast);
938 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
939 			   I40E_GLPRT_MPTCL(hw->port),
940 			   pf->stat_offsets_loaded,
941 			   &osd->eth.tx_multicast,
942 			   &nsd->eth.tx_multicast);
943 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
944 			   I40E_GLPRT_BPTCL(hw->port),
945 			   pf->stat_offsets_loaded,
946 			   &osd->eth.tx_broadcast,
947 			   &nsd->eth.tx_broadcast);
948 
949 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
950 			   pf->stat_offsets_loaded,
951 			   &osd->tx_dropped_link_down,
952 			   &nsd->tx_dropped_link_down);
953 
954 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
955 			   pf->stat_offsets_loaded,
956 			   &osd->crc_errors, &nsd->crc_errors);
957 
958 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
959 			   pf->stat_offsets_loaded,
960 			   &osd->illegal_bytes, &nsd->illegal_bytes);
961 
962 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
963 			   pf->stat_offsets_loaded,
964 			   &osd->mac_local_faults,
965 			   &nsd->mac_local_faults);
966 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
967 			   pf->stat_offsets_loaded,
968 			   &osd->mac_remote_faults,
969 			   &nsd->mac_remote_faults);
970 
971 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
972 			   pf->stat_offsets_loaded,
973 			   &osd->rx_length_errors,
974 			   &nsd->rx_length_errors);
975 
976 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
977 			   pf->stat_offsets_loaded,
978 			   &osd->link_xon_rx, &nsd->link_xon_rx);
979 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
980 			   pf->stat_offsets_loaded,
981 			   &osd->link_xon_tx, &nsd->link_xon_tx);
982 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
983 			   pf->stat_offsets_loaded,
984 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
985 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
986 			   pf->stat_offsets_loaded,
987 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
988 
989 	for (i = 0; i < 8; i++) {
990 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
991 				   pf->stat_offsets_loaded,
992 				   &osd->priority_xoff_rx[i],
993 				   &nsd->priority_xoff_rx[i]);
994 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
995 				   pf->stat_offsets_loaded,
996 				   &osd->priority_xon_rx[i],
997 				   &nsd->priority_xon_rx[i]);
998 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
999 				   pf->stat_offsets_loaded,
1000 				   &osd->priority_xon_tx[i],
1001 				   &nsd->priority_xon_tx[i]);
1002 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1003 				   pf->stat_offsets_loaded,
1004 				   &osd->priority_xoff_tx[i],
1005 				   &nsd->priority_xoff_tx[i]);
1006 		i40e_stat_update32(hw,
1007 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1008 				   pf->stat_offsets_loaded,
1009 				   &osd->priority_xon_2_xoff[i],
1010 				   &nsd->priority_xon_2_xoff[i]);
1011 	}
1012 
1013 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1014 			   I40E_GLPRT_PRC64L(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->rx_size_64, &nsd->rx_size_64);
1017 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1018 			   I40E_GLPRT_PRC127L(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->rx_size_127, &nsd->rx_size_127);
1021 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1022 			   I40E_GLPRT_PRC255L(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->rx_size_255, &nsd->rx_size_255);
1025 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1026 			   I40E_GLPRT_PRC511L(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->rx_size_511, &nsd->rx_size_511);
1029 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1030 			   I40E_GLPRT_PRC1023L(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1033 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1034 			   I40E_GLPRT_PRC1522L(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1037 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1038 			   I40E_GLPRT_PRC9522L(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->rx_size_big, &nsd->rx_size_big);
1041 
1042 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1043 			   I40E_GLPRT_PTC64L(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->tx_size_64, &nsd->tx_size_64);
1046 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1047 			   I40E_GLPRT_PTC127L(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->tx_size_127, &nsd->tx_size_127);
1050 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1051 			   I40E_GLPRT_PTC255L(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->tx_size_255, &nsd->tx_size_255);
1054 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1055 			   I40E_GLPRT_PTC511L(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->tx_size_511, &nsd->tx_size_511);
1058 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1059 			   I40E_GLPRT_PTC1023L(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1062 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1063 			   I40E_GLPRT_PTC1522L(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1066 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1067 			   I40E_GLPRT_PTC9522L(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->tx_size_big, &nsd->tx_size_big);
1070 
1071 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->rx_undersize, &nsd->rx_undersize);
1074 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1075 			   pf->stat_offsets_loaded,
1076 			   &osd->rx_fragments, &nsd->rx_fragments);
1077 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1078 			   pf->stat_offsets_loaded,
1079 			   &osd->rx_oversize, &nsd->rx_oversize);
1080 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->rx_jabber, &nsd->rx_jabber);
1083 
1084 	/* FDIR stats */
1085 	i40e_stat_update_and_clear32(hw,
1086 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1087 			&nsd->fd_atr_match);
1088 	i40e_stat_update_and_clear32(hw,
1089 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1090 			&nsd->fd_sb_match);
1091 	i40e_stat_update_and_clear32(hw,
1092 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1093 			&nsd->fd_atr_tunnel_match);
1094 
1095 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1096 	nsd->tx_lpi_status =
1097 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1098 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1099 	nsd->rx_lpi_status =
1100 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1101 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1102 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1103 			   pf->stat_offsets_loaded,
1104 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1105 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1106 			   pf->stat_offsets_loaded,
1107 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1108 
1109 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1110 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1111 		nsd->fd_sb_status = true;
1112 	else
1113 		nsd->fd_sb_status = false;
1114 
1115 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1116 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1117 		nsd->fd_atr_status = true;
1118 	else
1119 		nsd->fd_atr_status = false;
1120 
1121 	pf->stat_offsets_loaded = true;
1122 }
1123 
1124 /**
1125  * i40e_update_stats - Update the various statistics counters.
1126  * @vsi: the VSI to be updated
1127  *
1128  * Update the various stats for this VSI and its related entities.
1129  **/
1130 void i40e_update_stats(struct i40e_vsi *vsi)
1131 {
1132 	struct i40e_pf *pf = vsi->back;
1133 
1134 	if (vsi == pf->vsi[pf->lan_vsi])
1135 		i40e_update_pf_stats(pf);
1136 
1137 	i40e_update_vsi_stats(vsi);
1138 }
1139 
1140 /**
1141  * i40e_count_filters - counts VSI mac filters
1142  * @vsi: the VSI to be searched
1143  *
1144  * Returns count of mac filters
1145  **/
1146 int i40e_count_filters(struct i40e_vsi *vsi)
1147 {
1148 	struct i40e_mac_filter *f;
1149 	struct hlist_node *h;
1150 	int bkt;
1151 	int cnt = 0;
1152 
1153 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1154 		++cnt;
1155 
1156 	return cnt;
1157 }
1158 
1159 /**
1160  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1161  * @vsi: the VSI to be searched
1162  * @macaddr: the MAC address
1163  * @vlan: the vlan
1164  *
1165  * Returns ptr to the filter object or NULL
1166  **/
1167 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1168 						const u8 *macaddr, s16 vlan)
1169 {
1170 	struct i40e_mac_filter *f;
1171 	u64 key;
1172 
1173 	if (!vsi || !macaddr)
1174 		return NULL;
1175 
1176 	key = i40e_addr_to_hkey(macaddr);
1177 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1178 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1179 		    (vlan == f->vlan))
1180 			return f;
1181 	}
1182 	return NULL;
1183 }
1184 
1185 /**
1186  * i40e_find_mac - Find a mac addr in the macvlan filters list
1187  * @vsi: the VSI to be searched
1188  * @macaddr: the MAC address we are searching for
1189  *
1190  * Returns the first filter with the provided MAC address or NULL if
1191  * MAC address was not found
1192  **/
1193 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1194 {
1195 	struct i40e_mac_filter *f;
1196 	u64 key;
1197 
1198 	if (!vsi || !macaddr)
1199 		return NULL;
1200 
1201 	key = i40e_addr_to_hkey(macaddr);
1202 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1203 		if ((ether_addr_equal(macaddr, f->macaddr)))
1204 			return f;
1205 	}
1206 	return NULL;
1207 }
1208 
1209 /**
1210  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1211  * @vsi: the VSI to be searched
1212  *
1213  * Returns true if VSI is in vlan mode or false otherwise
1214  **/
1215 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1216 {
1217 	/* If we have a PVID, always operate in VLAN mode */
1218 	if (vsi->info.pvid)
1219 		return true;
1220 
1221 	/* We need to operate in VLAN mode whenever we have any filters with
1222 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1223 	 * time, incurring search cost repeatedly. However, we can notice two
1224 	 * things:
1225 	 *
1226 	 * 1) the only place where we can gain a VLAN filter is in
1227 	 *    i40e_add_filter.
1228 	 *
1229 	 * 2) the only place where filters are actually removed is in
1230 	 *    i40e_sync_filters_subtask.
1231 	 *
1232 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1233 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1234 	 * we have to perform the full search after deleting filters in
1235 	 * i40e_sync_filters_subtask, but we already have to search
1236 	 * filters here and can perform the check at the same time. This
1237 	 * results in avoiding embedding a loop for VLAN mode inside another
1238 	 * loop over all the filters, and should maintain correctness as noted
1239 	 * above.
1240 	 */
1241 	return vsi->has_vlan_filter;
1242 }
1243 
1244 /**
1245  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1246  * @vsi: the VSI to configure
1247  * @tmp_add_list: list of filters ready to be added
1248  * @tmp_del_list: list of filters ready to be deleted
1249  * @vlan_filters: the number of active VLAN filters
1250  *
1251  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1252  * behave as expected. If we have any active VLAN filters remaining or about
1253  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1254  * so that they only match against untagged traffic. If we no longer have any
1255  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1256  * so that they match against both tagged and untagged traffic. In this way,
1257  * we ensure that we correctly receive the desired traffic. This ensures that
1258  * when we have an active VLAN we will receive only untagged traffic and
1259  * traffic matching active VLANs. If we have no active VLANs then we will
1260  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1261  *
1262  * Finally, in a similar fashion, this function also corrects filters when
1263  * there is an active PVID assigned to this VSI.
1264  *
1265  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1266  *
1267  * This function is only expected to be called from within
1268  * i40e_sync_vsi_filters.
1269  *
1270  * NOTE: This function expects to be called while under the
1271  * mac_filter_hash_lock
1272  */
1273 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1274 					 struct hlist_head *tmp_add_list,
1275 					 struct hlist_head *tmp_del_list,
1276 					 int vlan_filters)
1277 {
1278 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1279 	struct i40e_mac_filter *f, *add_head;
1280 	struct i40e_new_mac_filter *new;
1281 	struct hlist_node *h;
1282 	int bkt, new_vlan;
1283 
1284 	/* To determine if a particular filter needs to be replaced we
1285 	 * have the three following conditions:
1286 	 *
1287 	 * a) if we have a PVID assigned, then all filters which are
1288 	 *    not marked as VLAN=PVID must be replaced with filters that
1289 	 *    are.
1290 	 * b) otherwise, if we have any active VLANS, all filters
1291 	 *    which are marked as VLAN=-1 must be replaced with
1292 	 *    filters marked as VLAN=0
1293 	 * c) finally, if we do not have any active VLANS, all filters
1294 	 *    which are marked as VLAN=0 must be replaced with filters
1295 	 *    marked as VLAN=-1
1296 	 */
1297 
1298 	/* Update the filters about to be added in place */
1299 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1300 		if (pvid && new->f->vlan != pvid)
1301 			new->f->vlan = pvid;
1302 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1303 			new->f->vlan = 0;
1304 		else if (!vlan_filters && new->f->vlan == 0)
1305 			new->f->vlan = I40E_VLAN_ANY;
1306 	}
1307 
1308 	/* Update the remaining active filters */
1309 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1310 		/* Combine the checks for whether a filter needs to be changed
1311 		 * and then determine the new VLAN inside the if block, in
1312 		 * order to avoid duplicating code for adding the new filter
1313 		 * then deleting the old filter.
1314 		 */
1315 		if ((pvid && f->vlan != pvid) ||
1316 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1317 		    (!vlan_filters && f->vlan == 0)) {
1318 			/* Determine the new vlan we will be adding */
1319 			if (pvid)
1320 				new_vlan = pvid;
1321 			else if (vlan_filters)
1322 				new_vlan = 0;
1323 			else
1324 				new_vlan = I40E_VLAN_ANY;
1325 
1326 			/* Create the new filter */
1327 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1328 			if (!add_head)
1329 				return -ENOMEM;
1330 
1331 			/* Create a temporary i40e_new_mac_filter */
1332 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1333 			if (!new)
1334 				return -ENOMEM;
1335 
1336 			new->f = add_head;
1337 			new->state = add_head->state;
1338 
1339 			/* Add the new filter to the tmp list */
1340 			hlist_add_head(&new->hlist, tmp_add_list);
1341 
1342 			/* Put the original filter into the delete list */
1343 			f->state = I40E_FILTER_REMOVE;
1344 			hash_del(&f->hlist);
1345 			hlist_add_head(&f->hlist, tmp_del_list);
1346 		}
1347 	}
1348 
1349 	vsi->has_vlan_filter = !!vlan_filters;
1350 
1351 	return 0;
1352 }
1353 
1354 /**
1355  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1356  * @vsi: the PF Main VSI - inappropriate for any other VSI
1357  * @macaddr: the MAC address
1358  *
1359  * Remove whatever filter the firmware set up so the driver can manage
1360  * its own filtering intelligently.
1361  **/
1362 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1363 {
1364 	struct i40e_aqc_remove_macvlan_element_data element;
1365 	struct i40e_pf *pf = vsi->back;
1366 
1367 	/* Only appropriate for the PF main VSI */
1368 	if (vsi->type != I40E_VSI_MAIN)
1369 		return;
1370 
1371 	memset(&element, 0, sizeof(element));
1372 	ether_addr_copy(element.mac_addr, macaddr);
1373 	element.vlan_tag = 0;
1374 	/* Ignore error returns, some firmware does it this way... */
1375 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1376 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1377 
1378 	memset(&element, 0, sizeof(element));
1379 	ether_addr_copy(element.mac_addr, macaddr);
1380 	element.vlan_tag = 0;
1381 	/* ...and some firmware does it this way. */
1382 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1383 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1384 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1385 }
1386 
1387 /**
1388  * i40e_add_filter - Add a mac/vlan filter to the VSI
1389  * @vsi: the VSI to be searched
1390  * @macaddr: the MAC address
1391  * @vlan: the vlan
1392  *
1393  * Returns ptr to the filter object or NULL when no memory available.
1394  *
1395  * NOTE: This function is expected to be called with mac_filter_hash_lock
1396  * being held.
1397  **/
1398 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1399 					const u8 *macaddr, s16 vlan)
1400 {
1401 	struct i40e_mac_filter *f;
1402 	u64 key;
1403 
1404 	if (!vsi || !macaddr)
1405 		return NULL;
1406 
1407 	f = i40e_find_filter(vsi, macaddr, vlan);
1408 	if (!f) {
1409 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1410 		if (!f)
1411 			return NULL;
1412 
1413 		/* Update the boolean indicating if we need to function in
1414 		 * VLAN mode.
1415 		 */
1416 		if (vlan >= 0)
1417 			vsi->has_vlan_filter = true;
1418 
1419 		ether_addr_copy(f->macaddr, macaddr);
1420 		f->vlan = vlan;
1421 		f->state = I40E_FILTER_NEW;
1422 		INIT_HLIST_NODE(&f->hlist);
1423 
1424 		key = i40e_addr_to_hkey(macaddr);
1425 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1426 
1427 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1428 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1429 	}
1430 
1431 	/* If we're asked to add a filter that has been marked for removal, it
1432 	 * is safe to simply restore it to active state. __i40e_del_filter
1433 	 * will have simply deleted any filters which were previously marked
1434 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1435 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1436 	 * task, just restore this filter to the ACTIVE state so that the
1437 	 * sync task leaves it in place
1438 	 */
1439 	if (f->state == I40E_FILTER_REMOVE)
1440 		f->state = I40E_FILTER_ACTIVE;
1441 
1442 	return f;
1443 }
1444 
1445 /**
1446  * __i40e_del_filter - Remove a specific filter from the VSI
1447  * @vsi: VSI to remove from
1448  * @f: the filter to remove from the list
1449  *
1450  * This function should be called instead of i40e_del_filter only if you know
1451  * the exact filter you will remove already, such as via i40e_find_filter or
1452  * i40e_find_mac.
1453  *
1454  * NOTE: This function is expected to be called with mac_filter_hash_lock
1455  * being held.
1456  * ANOTHER NOTE: This function MUST be called from within the context of
1457  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1458  * instead of list_for_each_entry().
1459  **/
1460 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1461 {
1462 	if (!f)
1463 		return;
1464 
1465 	/* If the filter was never added to firmware then we can just delete it
1466 	 * directly and we don't want to set the status to remove or else an
1467 	 * admin queue command will unnecessarily fire.
1468 	 */
1469 	if ((f->state == I40E_FILTER_FAILED) ||
1470 	    (f->state == I40E_FILTER_NEW)) {
1471 		hash_del(&f->hlist);
1472 		kfree(f);
1473 	} else {
1474 		f->state = I40E_FILTER_REMOVE;
1475 	}
1476 
1477 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1478 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1479 }
1480 
1481 /**
1482  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1483  * @vsi: the VSI to be searched
1484  * @macaddr: the MAC address
1485  * @vlan: the VLAN
1486  *
1487  * NOTE: This function is expected to be called with mac_filter_hash_lock
1488  * being held.
1489  * ANOTHER NOTE: This function MUST be called from within the context of
1490  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1491  * instead of list_for_each_entry().
1492  **/
1493 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1494 {
1495 	struct i40e_mac_filter *f;
1496 
1497 	if (!vsi || !macaddr)
1498 		return;
1499 
1500 	f = i40e_find_filter(vsi, macaddr, vlan);
1501 	__i40e_del_filter(vsi, f);
1502 }
1503 
1504 /**
1505  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1506  * @vsi: the VSI to be searched
1507  * @macaddr: the mac address to be filtered
1508  *
1509  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1510  * go through all the macvlan filters and add a macvlan filter for each
1511  * unique vlan that already exists. If a PVID has been assigned, instead only
1512  * add the macaddr to that VLAN.
1513  *
1514  * Returns last filter added on success, else NULL
1515  **/
1516 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1517 					    const u8 *macaddr)
1518 {
1519 	struct i40e_mac_filter *f, *add = NULL;
1520 	struct hlist_node *h;
1521 	int bkt;
1522 
1523 	if (vsi->info.pvid)
1524 		return i40e_add_filter(vsi, macaddr,
1525 				       le16_to_cpu(vsi->info.pvid));
1526 
1527 	if (!i40e_is_vsi_in_vlan(vsi))
1528 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1529 
1530 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1531 		if (f->state == I40E_FILTER_REMOVE)
1532 			continue;
1533 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1534 		if (!add)
1535 			return NULL;
1536 	}
1537 
1538 	return add;
1539 }
1540 
1541 /**
1542  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1543  * @vsi: the VSI to be searched
1544  * @macaddr: the mac address to be removed
1545  *
1546  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1547  * associated with.
1548  *
1549  * Returns 0 for success, or error
1550  **/
1551 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1552 {
1553 	struct i40e_mac_filter *f;
1554 	struct hlist_node *h;
1555 	bool found = false;
1556 	int bkt;
1557 
1558 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1559 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1560 		if (ether_addr_equal(macaddr, f->macaddr)) {
1561 			__i40e_del_filter(vsi, f);
1562 			found = true;
1563 		}
1564 	}
1565 
1566 	if (found)
1567 		return 0;
1568 	else
1569 		return -ENOENT;
1570 }
1571 
1572 /**
1573  * i40e_set_mac - NDO callback to set mac address
1574  * @netdev: network interface device structure
1575  * @p: pointer to an address structure
1576  *
1577  * Returns 0 on success, negative on failure
1578  **/
1579 static int i40e_set_mac(struct net_device *netdev, void *p)
1580 {
1581 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1582 	struct i40e_vsi *vsi = np->vsi;
1583 	struct i40e_pf *pf = vsi->back;
1584 	struct i40e_hw *hw = &pf->hw;
1585 	struct sockaddr *addr = p;
1586 
1587 	if (!is_valid_ether_addr(addr->sa_data))
1588 		return -EADDRNOTAVAIL;
1589 
1590 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1591 		netdev_info(netdev, "already using mac address %pM\n",
1592 			    addr->sa_data);
1593 		return 0;
1594 	}
1595 
1596 	if (test_bit(__I40E_DOWN, pf->state) ||
1597 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1598 		return -EADDRNOTAVAIL;
1599 
1600 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1601 		netdev_info(netdev, "returning to hw mac address %pM\n",
1602 			    hw->mac.addr);
1603 	else
1604 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1605 
1606 	/* Copy the address first, so that we avoid a possible race with
1607 	 * .set_rx_mode().
1608 	 * - Remove old address from MAC filter
1609 	 * - Copy new address
1610 	 * - Add new address to MAC filter
1611 	 */
1612 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1613 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1614 	eth_hw_addr_set(netdev, addr->sa_data);
1615 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1616 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1617 
1618 	if (vsi->type == I40E_VSI_MAIN) {
1619 		i40e_status ret;
1620 
1621 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1622 						addr->sa_data, NULL);
1623 		if (ret)
1624 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1625 				    i40e_stat_str(hw, ret),
1626 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1627 	}
1628 
1629 	/* schedule our worker thread which will take care of
1630 	 * applying the new filter changes
1631 	 */
1632 	i40e_service_event_schedule(pf);
1633 	return 0;
1634 }
1635 
1636 /**
1637  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1638  * @vsi: vsi structure
1639  * @seed: RSS hash seed
1640  * @lut: pointer to lookup table of lut_size
1641  * @lut_size: size of the lookup table
1642  **/
1643 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1644 			      u8 *lut, u16 lut_size)
1645 {
1646 	struct i40e_pf *pf = vsi->back;
1647 	struct i40e_hw *hw = &pf->hw;
1648 	int ret = 0;
1649 
1650 	if (seed) {
1651 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1652 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1653 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1654 		if (ret) {
1655 			dev_info(&pf->pdev->dev,
1656 				 "Cannot set RSS key, err %s aq_err %s\n",
1657 				 i40e_stat_str(hw, ret),
1658 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1659 			return ret;
1660 		}
1661 	}
1662 	if (lut) {
1663 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1664 
1665 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1666 		if (ret) {
1667 			dev_info(&pf->pdev->dev,
1668 				 "Cannot set RSS lut, err %s aq_err %s\n",
1669 				 i40e_stat_str(hw, ret),
1670 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1671 			return ret;
1672 		}
1673 	}
1674 	return ret;
1675 }
1676 
1677 /**
1678  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1679  * @vsi: VSI structure
1680  **/
1681 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1682 {
1683 	struct i40e_pf *pf = vsi->back;
1684 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1685 	u8 *lut;
1686 	int ret;
1687 
1688 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1689 		return 0;
1690 	if (!vsi->rss_size)
1691 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1692 				      vsi->num_queue_pairs);
1693 	if (!vsi->rss_size)
1694 		return -EINVAL;
1695 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1696 	if (!lut)
1697 		return -ENOMEM;
1698 
1699 	/* Use the user configured hash keys and lookup table if there is one,
1700 	 * otherwise use default
1701 	 */
1702 	if (vsi->rss_lut_user)
1703 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1704 	else
1705 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1706 	if (vsi->rss_hkey_user)
1707 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1708 	else
1709 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1710 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1711 	kfree(lut);
1712 	return ret;
1713 }
1714 
1715 /**
1716  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1717  * @vsi: the VSI being configured,
1718  * @ctxt: VSI context structure
1719  * @enabled_tc: number of traffic classes to enable
1720  *
1721  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1722  **/
1723 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1724 					   struct i40e_vsi_context *ctxt,
1725 					   u8 enabled_tc)
1726 {
1727 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1728 	int i, override_q, pow, num_qps, ret;
1729 	u8 netdev_tc = 0, offset = 0;
1730 
1731 	if (vsi->type != I40E_VSI_MAIN)
1732 		return -EINVAL;
1733 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1734 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1735 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1736 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1737 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1738 
1739 	/* find the next higher power-of-2 of num queue pairs */
1740 	pow = ilog2(num_qps);
1741 	if (!is_power_of_2(num_qps))
1742 		pow++;
1743 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1744 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1745 
1746 	/* Setup queue offset/count for all TCs for given VSI */
1747 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1748 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1749 		/* See if the given TC is enabled for the given VSI */
1750 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1751 			offset = vsi->mqprio_qopt.qopt.offset[i];
1752 			qcount = vsi->mqprio_qopt.qopt.count[i];
1753 			if (qcount > max_qcount)
1754 				max_qcount = qcount;
1755 			vsi->tc_config.tc_info[i].qoffset = offset;
1756 			vsi->tc_config.tc_info[i].qcount = qcount;
1757 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1758 		} else {
1759 			/* TC is not enabled so set the offset to
1760 			 * default queue and allocate one queue
1761 			 * for the given TC.
1762 			 */
1763 			vsi->tc_config.tc_info[i].qoffset = 0;
1764 			vsi->tc_config.tc_info[i].qcount = 1;
1765 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1766 		}
1767 	}
1768 
1769 	/* Set actual Tx/Rx queue pairs */
1770 	vsi->num_queue_pairs = offset + qcount;
1771 
1772 	/* Setup queue TC[0].qmap for given VSI context */
1773 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1774 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1775 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1776 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1777 
1778 	/* Reconfigure RSS for main VSI with max queue count */
1779 	vsi->rss_size = max_qcount;
1780 	ret = i40e_vsi_config_rss(vsi);
1781 	if (ret) {
1782 		dev_info(&vsi->back->pdev->dev,
1783 			 "Failed to reconfig rss for num_queues (%u)\n",
1784 			 max_qcount);
1785 		return ret;
1786 	}
1787 	vsi->reconfig_rss = true;
1788 	dev_dbg(&vsi->back->pdev->dev,
1789 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1790 
1791 	/* Find queue count available for channel VSIs and starting offset
1792 	 * for channel VSIs
1793 	 */
1794 	override_q = vsi->mqprio_qopt.qopt.count[0];
1795 	if (override_q && override_q < vsi->num_queue_pairs) {
1796 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1797 		vsi->next_base_queue = override_q;
1798 	}
1799 	return 0;
1800 }
1801 
1802 /**
1803  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1804  * @vsi: the VSI being setup
1805  * @ctxt: VSI context structure
1806  * @enabled_tc: Enabled TCs bitmap
1807  * @is_add: True if called before Add VSI
1808  *
1809  * Setup VSI queue mapping for enabled traffic classes.
1810  **/
1811 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1812 				     struct i40e_vsi_context *ctxt,
1813 				     u8 enabled_tc,
1814 				     bool is_add)
1815 {
1816 	struct i40e_pf *pf = vsi->back;
1817 	u16 num_tc_qps = 0;
1818 	u16 sections = 0;
1819 	u8 netdev_tc = 0;
1820 	u16 numtc = 1;
1821 	u16 qcount;
1822 	u8 offset;
1823 	u16 qmap;
1824 	int i;
1825 
1826 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1827 	offset = 0;
1828 	/* zero out queue mapping, it will get updated on the end of the function */
1829 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1830 
1831 	if (vsi->type == I40E_VSI_MAIN) {
1832 		/* This code helps add more queue to the VSI if we have
1833 		 * more cores than RSS can support, the higher cores will
1834 		 * be served by ATR or other filters. Furthermore, the
1835 		 * non-zero req_queue_pairs says that user requested a new
1836 		 * queue count via ethtool's set_channels, so use this
1837 		 * value for queues distribution across traffic classes
1838 		 */
1839 		if (vsi->req_queue_pairs > 0)
1840 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1841 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1842 			vsi->num_queue_pairs = pf->num_lan_msix;
1843 	}
1844 
1845 	/* Number of queues per enabled TC */
1846 	if (vsi->type == I40E_VSI_MAIN ||
1847 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1848 		num_tc_qps = vsi->num_queue_pairs;
1849 	else
1850 		num_tc_qps = vsi->alloc_queue_pairs;
1851 
1852 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1853 		/* Find numtc from enabled TC bitmap */
1854 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1855 			if (enabled_tc & BIT(i)) /* TC is enabled */
1856 				numtc++;
1857 		}
1858 		if (!numtc) {
1859 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1860 			numtc = 1;
1861 		}
1862 		num_tc_qps = num_tc_qps / numtc;
1863 		num_tc_qps = min_t(int, num_tc_qps,
1864 				   i40e_pf_get_max_q_per_tc(pf));
1865 	}
1866 
1867 	vsi->tc_config.numtc = numtc;
1868 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1869 
1870 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1871 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1872 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1873 
1874 	/* Setup queue offset/count for all TCs for given VSI */
1875 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1876 		/* See if the given TC is enabled for the given VSI */
1877 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1878 			/* TC is enabled */
1879 			int pow, num_qps;
1880 
1881 			switch (vsi->type) {
1882 			case I40E_VSI_MAIN:
1883 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1884 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1885 				    vsi->tc_config.enabled_tc != 1) {
1886 					qcount = min_t(int, pf->alloc_rss_size,
1887 						       num_tc_qps);
1888 					break;
1889 				}
1890 				fallthrough;
1891 			case I40E_VSI_FDIR:
1892 			case I40E_VSI_SRIOV:
1893 			case I40E_VSI_VMDQ2:
1894 			default:
1895 				qcount = num_tc_qps;
1896 				WARN_ON(i != 0);
1897 				break;
1898 			}
1899 			vsi->tc_config.tc_info[i].qoffset = offset;
1900 			vsi->tc_config.tc_info[i].qcount = qcount;
1901 
1902 			/* find the next higher power-of-2 of num queue pairs */
1903 			num_qps = qcount;
1904 			pow = 0;
1905 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1906 				pow++;
1907 				num_qps >>= 1;
1908 			}
1909 
1910 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1911 			qmap =
1912 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1913 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1914 
1915 			offset += qcount;
1916 		} else {
1917 			/* TC is not enabled so set the offset to
1918 			 * default queue and allocate one queue
1919 			 * for the given TC.
1920 			 */
1921 			vsi->tc_config.tc_info[i].qoffset = 0;
1922 			vsi->tc_config.tc_info[i].qcount = 1;
1923 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1924 
1925 			qmap = 0;
1926 		}
1927 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1928 	}
1929 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
1930 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
1931 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
1932 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
1933 		vsi->num_queue_pairs = offset;
1934 
1935 	/* Scheduler section valid can only be set for ADD VSI */
1936 	if (is_add) {
1937 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1938 
1939 		ctxt->info.up_enable_bits = enabled_tc;
1940 	}
1941 	if (vsi->type == I40E_VSI_SRIOV) {
1942 		ctxt->info.mapping_flags |=
1943 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1944 		for (i = 0; i < vsi->num_queue_pairs; i++)
1945 			ctxt->info.queue_mapping[i] =
1946 					       cpu_to_le16(vsi->base_queue + i);
1947 	} else {
1948 		ctxt->info.mapping_flags |=
1949 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1950 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1951 	}
1952 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1953 }
1954 
1955 /**
1956  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1957  * @netdev: the netdevice
1958  * @addr: address to add
1959  *
1960  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1961  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1962  */
1963 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1964 {
1965 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1966 	struct i40e_vsi *vsi = np->vsi;
1967 
1968 	if (i40e_add_mac_filter(vsi, addr))
1969 		return 0;
1970 	else
1971 		return -ENOMEM;
1972 }
1973 
1974 /**
1975  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1976  * @netdev: the netdevice
1977  * @addr: address to add
1978  *
1979  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1980  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1981  */
1982 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1983 {
1984 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1985 	struct i40e_vsi *vsi = np->vsi;
1986 
1987 	/* Under some circumstances, we might receive a request to delete
1988 	 * our own device address from our uc list. Because we store the
1989 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1990 	 * such requests and not delete our device address from this list.
1991 	 */
1992 	if (ether_addr_equal(addr, netdev->dev_addr))
1993 		return 0;
1994 
1995 	i40e_del_mac_filter(vsi, addr);
1996 
1997 	return 0;
1998 }
1999 
2000 /**
2001  * i40e_set_rx_mode - NDO callback to set the netdev filters
2002  * @netdev: network interface device structure
2003  **/
2004 static void i40e_set_rx_mode(struct net_device *netdev)
2005 {
2006 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2007 	struct i40e_vsi *vsi = np->vsi;
2008 
2009 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2010 
2011 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2012 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2013 
2014 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2015 
2016 	/* check for other flag changes */
2017 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2018 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2019 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2020 	}
2021 }
2022 
2023 /**
2024  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2025  * @vsi: Pointer to VSI struct
2026  * @from: Pointer to list which contains MAC filter entries - changes to
2027  *        those entries needs to be undone.
2028  *
2029  * MAC filter entries from this list were slated for deletion.
2030  **/
2031 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2032 					 struct hlist_head *from)
2033 {
2034 	struct i40e_mac_filter *f;
2035 	struct hlist_node *h;
2036 
2037 	hlist_for_each_entry_safe(f, h, from, hlist) {
2038 		u64 key = i40e_addr_to_hkey(f->macaddr);
2039 
2040 		/* Move the element back into MAC filter list*/
2041 		hlist_del(&f->hlist);
2042 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2043 	}
2044 }
2045 
2046 /**
2047  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2048  * @vsi: Pointer to vsi struct
2049  * @from: Pointer to list which contains MAC filter entries - changes to
2050  *        those entries needs to be undone.
2051  *
2052  * MAC filter entries from this list were slated for addition.
2053  **/
2054 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2055 					 struct hlist_head *from)
2056 {
2057 	struct i40e_new_mac_filter *new;
2058 	struct hlist_node *h;
2059 
2060 	hlist_for_each_entry_safe(new, h, from, hlist) {
2061 		/* We can simply free the wrapper structure */
2062 		hlist_del(&new->hlist);
2063 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2064 		kfree(new);
2065 	}
2066 }
2067 
2068 /**
2069  * i40e_next_filter - Get the next non-broadcast filter from a list
2070  * @next: pointer to filter in list
2071  *
2072  * Returns the next non-broadcast filter in the list. Required so that we
2073  * ignore broadcast filters within the list, since these are not handled via
2074  * the normal firmware update path.
2075  */
2076 static
2077 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2078 {
2079 	hlist_for_each_entry_continue(next, hlist) {
2080 		if (!is_broadcast_ether_addr(next->f->macaddr))
2081 			return next;
2082 	}
2083 
2084 	return NULL;
2085 }
2086 
2087 /**
2088  * i40e_update_filter_state - Update filter state based on return data
2089  * from firmware
2090  * @count: Number of filters added
2091  * @add_list: return data from fw
2092  * @add_head: pointer to first filter in current batch
2093  *
2094  * MAC filter entries from list were slated to be added to device. Returns
2095  * number of successful filters. Note that 0 does NOT mean success!
2096  **/
2097 static int
2098 i40e_update_filter_state(int count,
2099 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2100 			 struct i40e_new_mac_filter *add_head)
2101 {
2102 	int retval = 0;
2103 	int i;
2104 
2105 	for (i = 0; i < count; i++) {
2106 		/* Always check status of each filter. We don't need to check
2107 		 * the firmware return status because we pre-set the filter
2108 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2109 		 * request to the adminq. Thus, if it no longer matches then
2110 		 * we know the filter is active.
2111 		 */
2112 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2113 			add_head->state = I40E_FILTER_FAILED;
2114 		} else {
2115 			add_head->state = I40E_FILTER_ACTIVE;
2116 			retval++;
2117 		}
2118 
2119 		add_head = i40e_next_filter(add_head);
2120 		if (!add_head)
2121 			break;
2122 	}
2123 
2124 	return retval;
2125 }
2126 
2127 /**
2128  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2129  * @vsi: ptr to the VSI
2130  * @vsi_name: name to display in messages
2131  * @list: the list of filters to send to firmware
2132  * @num_del: the number of filters to delete
2133  * @retval: Set to -EIO on failure to delete
2134  *
2135  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2136  * *retval instead of a return value so that success does not force ret_val to
2137  * be set to 0. This ensures that a sequence of calls to this function
2138  * preserve the previous value of *retval on successful delete.
2139  */
2140 static
2141 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2142 			  struct i40e_aqc_remove_macvlan_element_data *list,
2143 			  int num_del, int *retval)
2144 {
2145 	struct i40e_hw *hw = &vsi->back->hw;
2146 	i40e_status aq_ret;
2147 	int aq_err;
2148 
2149 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2150 	aq_err = hw->aq.asq_last_status;
2151 
2152 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2153 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2154 		*retval = -EIO;
2155 		dev_info(&vsi->back->pdev->dev,
2156 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2157 			 vsi_name, i40e_stat_str(hw, aq_ret),
2158 			 i40e_aq_str(hw, aq_err));
2159 	}
2160 }
2161 
2162 /**
2163  * i40e_aqc_add_filters - Request firmware to add a set of filters
2164  * @vsi: ptr to the VSI
2165  * @vsi_name: name to display in messages
2166  * @list: the list of filters to send to firmware
2167  * @add_head: Position in the add hlist
2168  * @num_add: the number of filters to add
2169  *
2170  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2171  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2172  * space for more filters.
2173  */
2174 static
2175 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2176 			  struct i40e_aqc_add_macvlan_element_data *list,
2177 			  struct i40e_new_mac_filter *add_head,
2178 			  int num_add)
2179 {
2180 	struct i40e_hw *hw = &vsi->back->hw;
2181 	int aq_err, fcnt;
2182 
2183 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2184 	aq_err = hw->aq.asq_last_status;
2185 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2186 
2187 	if (fcnt != num_add) {
2188 		if (vsi->type == I40E_VSI_MAIN) {
2189 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2190 			dev_warn(&vsi->back->pdev->dev,
2191 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2192 				 i40e_aq_str(hw, aq_err), vsi_name);
2193 		} else if (vsi->type == I40E_VSI_SRIOV ||
2194 			   vsi->type == I40E_VSI_VMDQ1 ||
2195 			   vsi->type == I40E_VSI_VMDQ2) {
2196 			dev_warn(&vsi->back->pdev->dev,
2197 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2198 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2199 		} else {
2200 			dev_warn(&vsi->back->pdev->dev,
2201 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2202 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2203 		}
2204 	}
2205 }
2206 
2207 /**
2208  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2209  * @vsi: pointer to the VSI
2210  * @vsi_name: the VSI name
2211  * @f: filter data
2212  *
2213  * This function sets or clears the promiscuous broadcast flags for VLAN
2214  * filters in order to properly receive broadcast frames. Assumes that only
2215  * broadcast filters are passed.
2216  *
2217  * Returns status indicating success or failure;
2218  **/
2219 static i40e_status
2220 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2221 			  struct i40e_mac_filter *f)
2222 {
2223 	bool enable = f->state == I40E_FILTER_NEW;
2224 	struct i40e_hw *hw = &vsi->back->hw;
2225 	i40e_status aq_ret;
2226 
2227 	if (f->vlan == I40E_VLAN_ANY) {
2228 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2229 						   vsi->seid,
2230 						   enable,
2231 						   NULL);
2232 	} else {
2233 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2234 							    vsi->seid,
2235 							    enable,
2236 							    f->vlan,
2237 							    NULL);
2238 	}
2239 
2240 	if (aq_ret) {
2241 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2242 		dev_warn(&vsi->back->pdev->dev,
2243 			 "Error %s, forcing overflow promiscuous on %s\n",
2244 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2245 			 vsi_name);
2246 	}
2247 
2248 	return aq_ret;
2249 }
2250 
2251 /**
2252  * i40e_set_promiscuous - set promiscuous mode
2253  * @pf: board private structure
2254  * @promisc: promisc on or off
2255  *
2256  * There are different ways of setting promiscuous mode on a PF depending on
2257  * what state/environment we're in.  This identifies and sets it appropriately.
2258  * Returns 0 on success.
2259  **/
2260 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2261 {
2262 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2263 	struct i40e_hw *hw = &pf->hw;
2264 	i40e_status aq_ret;
2265 
2266 	if (vsi->type == I40E_VSI_MAIN &&
2267 	    pf->lan_veb != I40E_NO_VEB &&
2268 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2269 		/* set defport ON for Main VSI instead of true promisc
2270 		 * this way we will get all unicast/multicast and VLAN
2271 		 * promisc behavior but will not get VF or VMDq traffic
2272 		 * replicated on the Main VSI.
2273 		 */
2274 		if (promisc)
2275 			aq_ret = i40e_aq_set_default_vsi(hw,
2276 							 vsi->seid,
2277 							 NULL);
2278 		else
2279 			aq_ret = i40e_aq_clear_default_vsi(hw,
2280 							   vsi->seid,
2281 							   NULL);
2282 		if (aq_ret) {
2283 			dev_info(&pf->pdev->dev,
2284 				 "Set default VSI failed, err %s, aq_err %s\n",
2285 				 i40e_stat_str(hw, aq_ret),
2286 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2287 		}
2288 	} else {
2289 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2290 						  hw,
2291 						  vsi->seid,
2292 						  promisc, NULL,
2293 						  true);
2294 		if (aq_ret) {
2295 			dev_info(&pf->pdev->dev,
2296 				 "set unicast promisc failed, err %s, aq_err %s\n",
2297 				 i40e_stat_str(hw, aq_ret),
2298 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2299 		}
2300 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2301 						  hw,
2302 						  vsi->seid,
2303 						  promisc, NULL);
2304 		if (aq_ret) {
2305 			dev_info(&pf->pdev->dev,
2306 				 "set multicast promisc failed, err %s, aq_err %s\n",
2307 				 i40e_stat_str(hw, aq_ret),
2308 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2309 		}
2310 	}
2311 
2312 	if (!aq_ret)
2313 		pf->cur_promisc = promisc;
2314 
2315 	return aq_ret;
2316 }
2317 
2318 /**
2319  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2320  * @vsi: ptr to the VSI
2321  *
2322  * Push any outstanding VSI filter changes through the AdminQ.
2323  *
2324  * Returns 0 or error value
2325  **/
2326 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2327 {
2328 	struct hlist_head tmp_add_list, tmp_del_list;
2329 	struct i40e_mac_filter *f;
2330 	struct i40e_new_mac_filter *new, *add_head = NULL;
2331 	struct i40e_hw *hw = &vsi->back->hw;
2332 	bool old_overflow, new_overflow;
2333 	unsigned int failed_filters = 0;
2334 	unsigned int vlan_filters = 0;
2335 	char vsi_name[16] = "PF";
2336 	int filter_list_len = 0;
2337 	i40e_status aq_ret = 0;
2338 	u32 changed_flags = 0;
2339 	struct hlist_node *h;
2340 	struct i40e_pf *pf;
2341 	int num_add = 0;
2342 	int num_del = 0;
2343 	int retval = 0;
2344 	u16 cmd_flags;
2345 	int list_size;
2346 	int bkt;
2347 
2348 	/* empty array typed pointers, kcalloc later */
2349 	struct i40e_aqc_add_macvlan_element_data *add_list;
2350 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2351 
2352 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2353 		usleep_range(1000, 2000);
2354 	pf = vsi->back;
2355 
2356 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2357 
2358 	if (vsi->netdev) {
2359 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2360 		vsi->current_netdev_flags = vsi->netdev->flags;
2361 	}
2362 
2363 	INIT_HLIST_HEAD(&tmp_add_list);
2364 	INIT_HLIST_HEAD(&tmp_del_list);
2365 
2366 	if (vsi->type == I40E_VSI_SRIOV)
2367 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2368 	else if (vsi->type != I40E_VSI_MAIN)
2369 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2370 
2371 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2372 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2373 
2374 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2375 		/* Create a list of filters to delete. */
2376 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2377 			if (f->state == I40E_FILTER_REMOVE) {
2378 				/* Move the element into temporary del_list */
2379 				hash_del(&f->hlist);
2380 				hlist_add_head(&f->hlist, &tmp_del_list);
2381 
2382 				/* Avoid counting removed filters */
2383 				continue;
2384 			}
2385 			if (f->state == I40E_FILTER_NEW) {
2386 				/* Create a temporary i40e_new_mac_filter */
2387 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2388 				if (!new)
2389 					goto err_no_memory_locked;
2390 
2391 				/* Store pointer to the real filter */
2392 				new->f = f;
2393 				new->state = f->state;
2394 
2395 				/* Add it to the hash list */
2396 				hlist_add_head(&new->hlist, &tmp_add_list);
2397 			}
2398 
2399 			/* Count the number of active (current and new) VLAN
2400 			 * filters we have now. Does not count filters which
2401 			 * are marked for deletion.
2402 			 */
2403 			if (f->vlan > 0)
2404 				vlan_filters++;
2405 		}
2406 
2407 		retval = i40e_correct_mac_vlan_filters(vsi,
2408 						       &tmp_add_list,
2409 						       &tmp_del_list,
2410 						       vlan_filters);
2411 
2412 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2413 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2414 
2415 		if (retval)
2416 			goto err_no_memory_locked;
2417 
2418 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2419 	}
2420 
2421 	/* Now process 'del_list' outside the lock */
2422 	if (!hlist_empty(&tmp_del_list)) {
2423 		filter_list_len = hw->aq.asq_buf_size /
2424 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2425 		list_size = filter_list_len *
2426 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2427 		del_list = kzalloc(list_size, GFP_ATOMIC);
2428 		if (!del_list)
2429 			goto err_no_memory;
2430 
2431 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2432 			cmd_flags = 0;
2433 
2434 			/* handle broadcast filters by updating the broadcast
2435 			 * promiscuous flag and release filter list.
2436 			 */
2437 			if (is_broadcast_ether_addr(f->macaddr)) {
2438 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2439 
2440 				hlist_del(&f->hlist);
2441 				kfree(f);
2442 				continue;
2443 			}
2444 
2445 			/* add to delete list */
2446 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2447 			if (f->vlan == I40E_VLAN_ANY) {
2448 				del_list[num_del].vlan_tag = 0;
2449 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2450 			} else {
2451 				del_list[num_del].vlan_tag =
2452 					cpu_to_le16((u16)(f->vlan));
2453 			}
2454 
2455 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2456 			del_list[num_del].flags = cmd_flags;
2457 			num_del++;
2458 
2459 			/* flush a full buffer */
2460 			if (num_del == filter_list_len) {
2461 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2462 						     num_del, &retval);
2463 				memset(del_list, 0, list_size);
2464 				num_del = 0;
2465 			}
2466 			/* Release memory for MAC filter entries which were
2467 			 * synced up with HW.
2468 			 */
2469 			hlist_del(&f->hlist);
2470 			kfree(f);
2471 		}
2472 
2473 		if (num_del) {
2474 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2475 					     num_del, &retval);
2476 		}
2477 
2478 		kfree(del_list);
2479 		del_list = NULL;
2480 	}
2481 
2482 	if (!hlist_empty(&tmp_add_list)) {
2483 		/* Do all the adds now. */
2484 		filter_list_len = hw->aq.asq_buf_size /
2485 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2486 		list_size = filter_list_len *
2487 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2488 		add_list = kzalloc(list_size, GFP_ATOMIC);
2489 		if (!add_list)
2490 			goto err_no_memory;
2491 
2492 		num_add = 0;
2493 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2494 			/* handle broadcast filters by updating the broadcast
2495 			 * promiscuous flag instead of adding a MAC filter.
2496 			 */
2497 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2498 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2499 							      new->f))
2500 					new->state = I40E_FILTER_FAILED;
2501 				else
2502 					new->state = I40E_FILTER_ACTIVE;
2503 				continue;
2504 			}
2505 
2506 			/* add to add array */
2507 			if (num_add == 0)
2508 				add_head = new;
2509 			cmd_flags = 0;
2510 			ether_addr_copy(add_list[num_add].mac_addr,
2511 					new->f->macaddr);
2512 			if (new->f->vlan == I40E_VLAN_ANY) {
2513 				add_list[num_add].vlan_tag = 0;
2514 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2515 			} else {
2516 				add_list[num_add].vlan_tag =
2517 					cpu_to_le16((u16)(new->f->vlan));
2518 			}
2519 			add_list[num_add].queue_number = 0;
2520 			/* set invalid match method for later detection */
2521 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2522 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2523 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2524 			num_add++;
2525 
2526 			/* flush a full buffer */
2527 			if (num_add == filter_list_len) {
2528 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2529 						     add_head, num_add);
2530 				memset(add_list, 0, list_size);
2531 				num_add = 0;
2532 			}
2533 		}
2534 		if (num_add) {
2535 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2536 					     num_add);
2537 		}
2538 		/* Now move all of the filters from the temp add list back to
2539 		 * the VSI's list.
2540 		 */
2541 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2542 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2543 			/* Only update the state if we're still NEW */
2544 			if (new->f->state == I40E_FILTER_NEW)
2545 				new->f->state = new->state;
2546 			hlist_del(&new->hlist);
2547 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2548 			kfree(new);
2549 		}
2550 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2551 		kfree(add_list);
2552 		add_list = NULL;
2553 	}
2554 
2555 	/* Determine the number of active and failed filters. */
2556 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2557 	vsi->active_filters = 0;
2558 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2559 		if (f->state == I40E_FILTER_ACTIVE)
2560 			vsi->active_filters++;
2561 		else if (f->state == I40E_FILTER_FAILED)
2562 			failed_filters++;
2563 	}
2564 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2565 
2566 	/* Check if we are able to exit overflow promiscuous mode. We can
2567 	 * safely exit if we didn't just enter, we no longer have any failed
2568 	 * filters, and we have reduced filters below the threshold value.
2569 	 */
2570 	if (old_overflow && !failed_filters &&
2571 	    vsi->active_filters < vsi->promisc_threshold) {
2572 		dev_info(&pf->pdev->dev,
2573 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2574 			 vsi_name);
2575 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2576 		vsi->promisc_threshold = 0;
2577 	}
2578 
2579 	/* if the VF is not trusted do not do promisc */
2580 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2581 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2582 		goto out;
2583 	}
2584 
2585 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2586 
2587 	/* If we are entering overflow promiscuous, we need to calculate a new
2588 	 * threshold for when we are safe to exit
2589 	 */
2590 	if (!old_overflow && new_overflow)
2591 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2592 
2593 	/* check for changes in promiscuous modes */
2594 	if (changed_flags & IFF_ALLMULTI) {
2595 		bool cur_multipromisc;
2596 
2597 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2598 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2599 							       vsi->seid,
2600 							       cur_multipromisc,
2601 							       NULL);
2602 		if (aq_ret) {
2603 			retval = i40e_aq_rc_to_posix(aq_ret,
2604 						     hw->aq.asq_last_status);
2605 			dev_info(&pf->pdev->dev,
2606 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2607 				 vsi_name,
2608 				 i40e_stat_str(hw, aq_ret),
2609 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2610 		} else {
2611 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2612 				 cur_multipromisc ? "entering" : "leaving");
2613 		}
2614 	}
2615 
2616 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2617 		bool cur_promisc;
2618 
2619 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2620 			       new_overflow);
2621 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2622 		if (aq_ret) {
2623 			retval = i40e_aq_rc_to_posix(aq_ret,
2624 						     hw->aq.asq_last_status);
2625 			dev_info(&pf->pdev->dev,
2626 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2627 				 cur_promisc ? "on" : "off",
2628 				 vsi_name,
2629 				 i40e_stat_str(hw, aq_ret),
2630 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2631 		}
2632 	}
2633 out:
2634 	/* if something went wrong then set the changed flag so we try again */
2635 	if (retval)
2636 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2637 
2638 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2639 	return retval;
2640 
2641 err_no_memory:
2642 	/* Restore elements on the temporary add and delete lists */
2643 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2644 err_no_memory_locked:
2645 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2646 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2647 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2648 
2649 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2650 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2651 	return -ENOMEM;
2652 }
2653 
2654 /**
2655  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2656  * @pf: board private structure
2657  **/
2658 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2659 {
2660 	int v;
2661 
2662 	if (!pf)
2663 		return;
2664 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2665 		return;
2666 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2667 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2668 		return;
2669 	}
2670 
2671 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2672 		if (pf->vsi[v] &&
2673 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2674 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2675 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2676 
2677 			if (ret) {
2678 				/* come back and try again later */
2679 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2680 					pf->state);
2681 				break;
2682 			}
2683 		}
2684 	}
2685 }
2686 
2687 /**
2688  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2689  * @vsi: the vsi
2690  **/
2691 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2692 {
2693 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2694 		return I40E_RXBUFFER_2048;
2695 	else
2696 		return I40E_RXBUFFER_3072;
2697 }
2698 
2699 /**
2700  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2701  * @netdev: network interface device structure
2702  * @new_mtu: new value for maximum frame size
2703  *
2704  * Returns 0 on success, negative on failure
2705  **/
2706 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2707 {
2708 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2709 	struct i40e_vsi *vsi = np->vsi;
2710 	struct i40e_pf *pf = vsi->back;
2711 
2712 	if (i40e_enabled_xdp_vsi(vsi)) {
2713 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2714 
2715 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2716 			return -EINVAL;
2717 	}
2718 
2719 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2720 		   netdev->mtu, new_mtu);
2721 	netdev->mtu = new_mtu;
2722 	if (netif_running(netdev))
2723 		i40e_vsi_reinit_locked(vsi);
2724 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2725 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2726 	return 0;
2727 }
2728 
2729 /**
2730  * i40e_ioctl - Access the hwtstamp interface
2731  * @netdev: network interface device structure
2732  * @ifr: interface request data
2733  * @cmd: ioctl command
2734  **/
2735 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2736 {
2737 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2738 	struct i40e_pf *pf = np->vsi->back;
2739 
2740 	switch (cmd) {
2741 	case SIOCGHWTSTAMP:
2742 		return i40e_ptp_get_ts_config(pf, ifr);
2743 	case SIOCSHWTSTAMP:
2744 		return i40e_ptp_set_ts_config(pf, ifr);
2745 	default:
2746 		return -EOPNOTSUPP;
2747 	}
2748 }
2749 
2750 /**
2751  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2752  * @vsi: the vsi being adjusted
2753  **/
2754 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2755 {
2756 	struct i40e_vsi_context ctxt;
2757 	i40e_status ret;
2758 
2759 	/* Don't modify stripping options if a port VLAN is active */
2760 	if (vsi->info.pvid)
2761 		return;
2762 
2763 	if ((vsi->info.valid_sections &
2764 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2765 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2766 		return;  /* already enabled */
2767 
2768 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2769 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2770 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2771 
2772 	ctxt.seid = vsi->seid;
2773 	ctxt.info = vsi->info;
2774 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2775 	if (ret) {
2776 		dev_info(&vsi->back->pdev->dev,
2777 			 "update vlan stripping failed, err %s aq_err %s\n",
2778 			 i40e_stat_str(&vsi->back->hw, ret),
2779 			 i40e_aq_str(&vsi->back->hw,
2780 				     vsi->back->hw.aq.asq_last_status));
2781 	}
2782 }
2783 
2784 /**
2785  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2786  * @vsi: the vsi being adjusted
2787  **/
2788 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2789 {
2790 	struct i40e_vsi_context ctxt;
2791 	i40e_status ret;
2792 
2793 	/* Don't modify stripping options if a port VLAN is active */
2794 	if (vsi->info.pvid)
2795 		return;
2796 
2797 	if ((vsi->info.valid_sections &
2798 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2799 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2800 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2801 		return;  /* already disabled */
2802 
2803 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2804 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2805 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2806 
2807 	ctxt.seid = vsi->seid;
2808 	ctxt.info = vsi->info;
2809 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2810 	if (ret) {
2811 		dev_info(&vsi->back->pdev->dev,
2812 			 "update vlan stripping failed, err %s aq_err %s\n",
2813 			 i40e_stat_str(&vsi->back->hw, ret),
2814 			 i40e_aq_str(&vsi->back->hw,
2815 				     vsi->back->hw.aq.asq_last_status));
2816 	}
2817 }
2818 
2819 /**
2820  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2821  * @vsi: the vsi being configured
2822  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2823  *
2824  * This is a helper function for adding a new MAC/VLAN filter with the
2825  * specified VLAN for each existing MAC address already in the hash table.
2826  * This function does *not* perform any accounting to update filters based on
2827  * VLAN mode.
2828  *
2829  * NOTE: this function expects to be called while under the
2830  * mac_filter_hash_lock
2831  **/
2832 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2833 {
2834 	struct i40e_mac_filter *f, *add_f;
2835 	struct hlist_node *h;
2836 	int bkt;
2837 
2838 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2839 		if (f->state == I40E_FILTER_REMOVE)
2840 			continue;
2841 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2842 		if (!add_f) {
2843 			dev_info(&vsi->back->pdev->dev,
2844 				 "Could not add vlan filter %d for %pM\n",
2845 				 vid, f->macaddr);
2846 			return -ENOMEM;
2847 		}
2848 	}
2849 
2850 	return 0;
2851 }
2852 
2853 /**
2854  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2855  * @vsi: the VSI being configured
2856  * @vid: VLAN id to be added
2857  **/
2858 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2859 {
2860 	int err;
2861 
2862 	if (vsi->info.pvid)
2863 		return -EINVAL;
2864 
2865 	/* The network stack will attempt to add VID=0, with the intention to
2866 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2867 	 * these packets by default when configured to receive untagged
2868 	 * packets, so we don't need to add a filter for this case.
2869 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2870 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2871 	 * Thus, we do not want to actually add a filter for VID=0
2872 	 */
2873 	if (!vid)
2874 		return 0;
2875 
2876 	/* Locked once because all functions invoked below iterates list*/
2877 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2878 	err = i40e_add_vlan_all_mac(vsi, vid);
2879 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2880 	if (err)
2881 		return err;
2882 
2883 	/* schedule our worker thread which will take care of
2884 	 * applying the new filter changes
2885 	 */
2886 	i40e_service_event_schedule(vsi->back);
2887 	return 0;
2888 }
2889 
2890 /**
2891  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2892  * @vsi: the vsi being configured
2893  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2894  *
2895  * This function should be used to remove all VLAN filters which match the
2896  * given VID. It does not schedule the service event and does not take the
2897  * mac_filter_hash_lock so it may be combined with other operations under
2898  * a single invocation of the mac_filter_hash_lock.
2899  *
2900  * NOTE: this function expects to be called while under the
2901  * mac_filter_hash_lock
2902  */
2903 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2904 {
2905 	struct i40e_mac_filter *f;
2906 	struct hlist_node *h;
2907 	int bkt;
2908 
2909 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2910 		if (f->vlan == vid)
2911 			__i40e_del_filter(vsi, f);
2912 	}
2913 }
2914 
2915 /**
2916  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2917  * @vsi: the VSI being configured
2918  * @vid: VLAN id to be removed
2919  **/
2920 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2921 {
2922 	if (!vid || vsi->info.pvid)
2923 		return;
2924 
2925 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2926 	i40e_rm_vlan_all_mac(vsi, vid);
2927 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2928 
2929 	/* schedule our worker thread which will take care of
2930 	 * applying the new filter changes
2931 	 */
2932 	i40e_service_event_schedule(vsi->back);
2933 }
2934 
2935 /**
2936  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2937  * @netdev: network interface to be adjusted
2938  * @proto: unused protocol value
2939  * @vid: vlan id to be added
2940  *
2941  * net_device_ops implementation for adding vlan ids
2942  **/
2943 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2944 				__always_unused __be16 proto, u16 vid)
2945 {
2946 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2947 	struct i40e_vsi *vsi = np->vsi;
2948 	int ret = 0;
2949 
2950 	if (vid >= VLAN_N_VID)
2951 		return -EINVAL;
2952 
2953 	ret = i40e_vsi_add_vlan(vsi, vid);
2954 	if (!ret)
2955 		set_bit(vid, vsi->active_vlans);
2956 
2957 	return ret;
2958 }
2959 
2960 /**
2961  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2962  * @netdev: network interface to be adjusted
2963  * @proto: unused protocol value
2964  * @vid: vlan id to be added
2965  **/
2966 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2967 				    __always_unused __be16 proto, u16 vid)
2968 {
2969 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2970 	struct i40e_vsi *vsi = np->vsi;
2971 
2972 	if (vid >= VLAN_N_VID)
2973 		return;
2974 	set_bit(vid, vsi->active_vlans);
2975 }
2976 
2977 /**
2978  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2979  * @netdev: network interface to be adjusted
2980  * @proto: unused protocol value
2981  * @vid: vlan id to be removed
2982  *
2983  * net_device_ops implementation for removing vlan ids
2984  **/
2985 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2986 				 __always_unused __be16 proto, u16 vid)
2987 {
2988 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2989 	struct i40e_vsi *vsi = np->vsi;
2990 
2991 	/* return code is ignored as there is nothing a user
2992 	 * can do about failure to remove and a log message was
2993 	 * already printed from the other function
2994 	 */
2995 	i40e_vsi_kill_vlan(vsi, vid);
2996 
2997 	clear_bit(vid, vsi->active_vlans);
2998 
2999 	return 0;
3000 }
3001 
3002 /**
3003  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3004  * @vsi: the vsi being brought back up
3005  **/
3006 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3007 {
3008 	u16 vid;
3009 
3010 	if (!vsi->netdev)
3011 		return;
3012 
3013 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3014 		i40e_vlan_stripping_enable(vsi);
3015 	else
3016 		i40e_vlan_stripping_disable(vsi);
3017 
3018 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3019 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3020 					vid);
3021 }
3022 
3023 /**
3024  * i40e_vsi_add_pvid - Add pvid for the VSI
3025  * @vsi: the vsi being adjusted
3026  * @vid: the vlan id to set as a PVID
3027  **/
3028 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3029 {
3030 	struct i40e_vsi_context ctxt;
3031 	i40e_status ret;
3032 
3033 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3034 	vsi->info.pvid = cpu_to_le16(vid);
3035 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3036 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3037 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3038 
3039 	ctxt.seid = vsi->seid;
3040 	ctxt.info = vsi->info;
3041 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3042 	if (ret) {
3043 		dev_info(&vsi->back->pdev->dev,
3044 			 "add pvid failed, err %s aq_err %s\n",
3045 			 i40e_stat_str(&vsi->back->hw, ret),
3046 			 i40e_aq_str(&vsi->back->hw,
3047 				     vsi->back->hw.aq.asq_last_status));
3048 		return -ENOENT;
3049 	}
3050 
3051 	return 0;
3052 }
3053 
3054 /**
3055  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3056  * @vsi: the vsi being adjusted
3057  *
3058  * Just use the vlan_rx_register() service to put it back to normal
3059  **/
3060 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3061 {
3062 	vsi->info.pvid = 0;
3063 
3064 	i40e_vlan_stripping_disable(vsi);
3065 }
3066 
3067 /**
3068  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3069  * @vsi: ptr to the VSI
3070  *
3071  * If this function returns with an error, then it's possible one or
3072  * more of the rings is populated (while the rest are not).  It is the
3073  * callers duty to clean those orphaned rings.
3074  *
3075  * Return 0 on success, negative on failure
3076  **/
3077 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3078 {
3079 	int i, err = 0;
3080 
3081 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3082 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3083 
3084 	if (!i40e_enabled_xdp_vsi(vsi))
3085 		return err;
3086 
3087 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3088 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3089 
3090 	return err;
3091 }
3092 
3093 /**
3094  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3095  * @vsi: ptr to the VSI
3096  *
3097  * Free VSI's transmit software resources
3098  **/
3099 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3100 {
3101 	int i;
3102 
3103 	if (vsi->tx_rings) {
3104 		for (i = 0; i < vsi->num_queue_pairs; i++)
3105 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3106 				i40e_free_tx_resources(vsi->tx_rings[i]);
3107 	}
3108 
3109 	if (vsi->xdp_rings) {
3110 		for (i = 0; i < vsi->num_queue_pairs; i++)
3111 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3112 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3113 	}
3114 }
3115 
3116 /**
3117  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3118  * @vsi: ptr to the VSI
3119  *
3120  * If this function returns with an error, then it's possible one or
3121  * more of the rings is populated (while the rest are not).  It is the
3122  * callers duty to clean those orphaned rings.
3123  *
3124  * Return 0 on success, negative on failure
3125  **/
3126 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3127 {
3128 	int i, err = 0;
3129 
3130 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3131 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3132 	return err;
3133 }
3134 
3135 /**
3136  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3137  * @vsi: ptr to the VSI
3138  *
3139  * Free all receive software resources
3140  **/
3141 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3142 {
3143 	int i;
3144 
3145 	if (!vsi->rx_rings)
3146 		return;
3147 
3148 	for (i = 0; i < vsi->num_queue_pairs; i++)
3149 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3150 			i40e_free_rx_resources(vsi->rx_rings[i]);
3151 }
3152 
3153 /**
3154  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3155  * @ring: The Tx ring to configure
3156  *
3157  * This enables/disables XPS for a given Tx descriptor ring
3158  * based on the TCs enabled for the VSI that ring belongs to.
3159  **/
3160 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3161 {
3162 	int cpu;
3163 
3164 	if (!ring->q_vector || !ring->netdev || ring->ch)
3165 		return;
3166 
3167 	/* We only initialize XPS once, so as not to overwrite user settings */
3168 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3169 		return;
3170 
3171 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3172 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3173 			    ring->queue_index);
3174 }
3175 
3176 /**
3177  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3178  * @ring: The Tx or Rx ring
3179  *
3180  * Returns the AF_XDP buffer pool or NULL.
3181  **/
3182 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3183 {
3184 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3185 	int qid = ring->queue_index;
3186 
3187 	if (ring_is_xdp(ring))
3188 		qid -= ring->vsi->alloc_queue_pairs;
3189 
3190 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3191 		return NULL;
3192 
3193 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3194 }
3195 
3196 /**
3197  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3198  * @ring: The Tx ring to configure
3199  *
3200  * Configure the Tx descriptor ring in the HMC context.
3201  **/
3202 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3203 {
3204 	struct i40e_vsi *vsi = ring->vsi;
3205 	u16 pf_q = vsi->base_queue + ring->queue_index;
3206 	struct i40e_hw *hw = &vsi->back->hw;
3207 	struct i40e_hmc_obj_txq tx_ctx;
3208 	i40e_status err = 0;
3209 	u32 qtx_ctl = 0;
3210 
3211 	if (ring_is_xdp(ring))
3212 		ring->xsk_pool = i40e_xsk_pool(ring);
3213 
3214 	/* some ATR related tx ring init */
3215 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3216 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3217 		ring->atr_count = 0;
3218 	} else {
3219 		ring->atr_sample_rate = 0;
3220 	}
3221 
3222 	/* configure XPS */
3223 	i40e_config_xps_tx_ring(ring);
3224 
3225 	/* clear the context structure first */
3226 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3227 
3228 	tx_ctx.new_context = 1;
3229 	tx_ctx.base = (ring->dma / 128);
3230 	tx_ctx.qlen = ring->count;
3231 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3232 					       I40E_FLAG_FD_ATR_ENABLED));
3233 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3234 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3235 	if (vsi->type != I40E_VSI_FDIR)
3236 		tx_ctx.head_wb_ena = 1;
3237 	tx_ctx.head_wb_addr = ring->dma +
3238 			      (ring->count * sizeof(struct i40e_tx_desc));
3239 
3240 	/* As part of VSI creation/update, FW allocates certain
3241 	 * Tx arbitration queue sets for each TC enabled for
3242 	 * the VSI. The FW returns the handles to these queue
3243 	 * sets as part of the response buffer to Add VSI,
3244 	 * Update VSI, etc. AQ commands. It is expected that
3245 	 * these queue set handles be associated with the Tx
3246 	 * queues by the driver as part of the TX queue context
3247 	 * initialization. This has to be done regardless of
3248 	 * DCB as by default everything is mapped to TC0.
3249 	 */
3250 
3251 	if (ring->ch)
3252 		tx_ctx.rdylist =
3253 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3254 
3255 	else
3256 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3257 
3258 	tx_ctx.rdylist_act = 0;
3259 
3260 	/* clear the context in the HMC */
3261 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3262 	if (err) {
3263 		dev_info(&vsi->back->pdev->dev,
3264 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3265 			 ring->queue_index, pf_q, err);
3266 		return -ENOMEM;
3267 	}
3268 
3269 	/* set the context in the HMC */
3270 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3271 	if (err) {
3272 		dev_info(&vsi->back->pdev->dev,
3273 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3274 			 ring->queue_index, pf_q, err);
3275 		return -ENOMEM;
3276 	}
3277 
3278 	/* Now associate this queue with this PCI function */
3279 	if (ring->ch) {
3280 		if (ring->ch->type == I40E_VSI_VMDQ2)
3281 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3282 		else
3283 			return -EINVAL;
3284 
3285 		qtx_ctl |= (ring->ch->vsi_number <<
3286 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3287 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3288 	} else {
3289 		if (vsi->type == I40E_VSI_VMDQ2) {
3290 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3291 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3292 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3293 		} else {
3294 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3295 		}
3296 	}
3297 
3298 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3299 		    I40E_QTX_CTL_PF_INDX_MASK);
3300 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3301 	i40e_flush(hw);
3302 
3303 	/* cache tail off for easier writes later */
3304 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3305 
3306 	return 0;
3307 }
3308 
3309 /**
3310  * i40e_rx_offset - Return expected offset into page to access data
3311  * @rx_ring: Ring we are requesting offset of
3312  *
3313  * Returns the offset value for ring into the data buffer.
3314  */
3315 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3316 {
3317 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3318 }
3319 
3320 /**
3321  * i40e_configure_rx_ring - Configure a receive ring context
3322  * @ring: The Rx ring to configure
3323  *
3324  * Configure the Rx descriptor ring in the HMC context.
3325  **/
3326 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3327 {
3328 	struct i40e_vsi *vsi = ring->vsi;
3329 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3330 	u16 pf_q = vsi->base_queue + ring->queue_index;
3331 	struct i40e_hw *hw = &vsi->back->hw;
3332 	struct i40e_hmc_obj_rxq rx_ctx;
3333 	i40e_status err = 0;
3334 	bool ok;
3335 	int ret;
3336 
3337 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3338 
3339 	/* clear the context structure first */
3340 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3341 
3342 	if (ring->vsi->type == I40E_VSI_MAIN)
3343 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3344 
3345 	kfree(ring->rx_bi);
3346 	ring->xsk_pool = i40e_xsk_pool(ring);
3347 	if (ring->xsk_pool) {
3348 		ret = i40e_alloc_rx_bi_zc(ring);
3349 		if (ret)
3350 			return ret;
3351 		ring->rx_buf_len =
3352 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3353 		/* For AF_XDP ZC, we disallow packets to span on
3354 		 * multiple buffers, thus letting us skip that
3355 		 * handling in the fast-path.
3356 		 */
3357 		chain_len = 1;
3358 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3359 						 MEM_TYPE_XSK_BUFF_POOL,
3360 						 NULL);
3361 		if (ret)
3362 			return ret;
3363 		dev_info(&vsi->back->pdev->dev,
3364 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3365 			 ring->queue_index);
3366 
3367 	} else {
3368 		ret = i40e_alloc_rx_bi(ring);
3369 		if (ret)
3370 			return ret;
3371 		ring->rx_buf_len = vsi->rx_buf_len;
3372 		if (ring->vsi->type == I40E_VSI_MAIN) {
3373 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3374 							 MEM_TYPE_PAGE_SHARED,
3375 							 NULL);
3376 			if (ret)
3377 				return ret;
3378 		}
3379 	}
3380 
3381 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3382 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3383 
3384 	rx_ctx.base = (ring->dma / 128);
3385 	rx_ctx.qlen = ring->count;
3386 
3387 	/* use 16 byte descriptors */
3388 	rx_ctx.dsize = 0;
3389 
3390 	/* descriptor type is always zero
3391 	 * rx_ctx.dtype = 0;
3392 	 */
3393 	rx_ctx.hsplit_0 = 0;
3394 
3395 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3396 	if (hw->revision_id == 0)
3397 		rx_ctx.lrxqthresh = 0;
3398 	else
3399 		rx_ctx.lrxqthresh = 1;
3400 	rx_ctx.crcstrip = 1;
3401 	rx_ctx.l2tsel = 1;
3402 	/* this controls whether VLAN is stripped from inner headers */
3403 	rx_ctx.showiv = 0;
3404 	/* set the prefena field to 1 because the manual says to */
3405 	rx_ctx.prefena = 1;
3406 
3407 	/* clear the context in the HMC */
3408 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3409 	if (err) {
3410 		dev_info(&vsi->back->pdev->dev,
3411 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3412 			 ring->queue_index, pf_q, err);
3413 		return -ENOMEM;
3414 	}
3415 
3416 	/* set the context in the HMC */
3417 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3418 	if (err) {
3419 		dev_info(&vsi->back->pdev->dev,
3420 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3421 			 ring->queue_index, pf_q, err);
3422 		return -ENOMEM;
3423 	}
3424 
3425 	/* configure Rx buffer alignment */
3426 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3427 		clear_ring_build_skb_enabled(ring);
3428 	else
3429 		set_ring_build_skb_enabled(ring);
3430 
3431 	ring->rx_offset = i40e_rx_offset(ring);
3432 
3433 	/* cache tail for quicker writes, and clear the reg before use */
3434 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3435 	writel(0, ring->tail);
3436 
3437 	if (ring->xsk_pool) {
3438 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3439 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3440 	} else {
3441 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3442 	}
3443 	if (!ok) {
3444 		/* Log this in case the user has forgotten to give the kernel
3445 		 * any buffers, even later in the application.
3446 		 */
3447 		dev_info(&vsi->back->pdev->dev,
3448 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3449 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3450 			 ring->queue_index, pf_q);
3451 	}
3452 
3453 	return 0;
3454 }
3455 
3456 /**
3457  * i40e_vsi_configure_tx - Configure the VSI for Tx
3458  * @vsi: VSI structure describing this set of rings and resources
3459  *
3460  * Configure the Tx VSI for operation.
3461  **/
3462 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3463 {
3464 	int err = 0;
3465 	u16 i;
3466 
3467 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3468 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3469 
3470 	if (err || !i40e_enabled_xdp_vsi(vsi))
3471 		return err;
3472 
3473 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3474 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3475 
3476 	return err;
3477 }
3478 
3479 /**
3480  * i40e_vsi_configure_rx - Configure the VSI for Rx
3481  * @vsi: the VSI being configured
3482  *
3483  * Configure the Rx VSI for operation.
3484  **/
3485 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3486 {
3487 	int err = 0;
3488 	u16 i;
3489 
3490 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3491 		vsi->max_frame = I40E_MAX_RXBUFFER;
3492 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3493 #if (PAGE_SIZE < 8192)
3494 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3495 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3496 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3497 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3498 #endif
3499 	} else {
3500 		vsi->max_frame = I40E_MAX_RXBUFFER;
3501 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3502 						       I40E_RXBUFFER_2048;
3503 	}
3504 
3505 	/* set up individual rings */
3506 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3507 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3508 
3509 	return err;
3510 }
3511 
3512 /**
3513  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3514  * @vsi: ptr to the VSI
3515  **/
3516 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3517 {
3518 	struct i40e_ring *tx_ring, *rx_ring;
3519 	u16 qoffset, qcount;
3520 	int i, n;
3521 
3522 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3523 		/* Reset the TC information */
3524 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3525 			rx_ring = vsi->rx_rings[i];
3526 			tx_ring = vsi->tx_rings[i];
3527 			rx_ring->dcb_tc = 0;
3528 			tx_ring->dcb_tc = 0;
3529 		}
3530 		return;
3531 	}
3532 
3533 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3534 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3535 			continue;
3536 
3537 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3538 		qcount = vsi->tc_config.tc_info[n].qcount;
3539 		for (i = qoffset; i < (qoffset + qcount); i++) {
3540 			rx_ring = vsi->rx_rings[i];
3541 			tx_ring = vsi->tx_rings[i];
3542 			rx_ring->dcb_tc = n;
3543 			tx_ring->dcb_tc = n;
3544 		}
3545 	}
3546 }
3547 
3548 /**
3549  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3550  * @vsi: ptr to the VSI
3551  **/
3552 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3553 {
3554 	if (vsi->netdev)
3555 		i40e_set_rx_mode(vsi->netdev);
3556 }
3557 
3558 /**
3559  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3560  * @pf: Pointer to the targeted PF
3561  *
3562  * Set all flow director counters to 0.
3563  */
3564 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3565 {
3566 	pf->fd_tcp4_filter_cnt = 0;
3567 	pf->fd_udp4_filter_cnt = 0;
3568 	pf->fd_sctp4_filter_cnt = 0;
3569 	pf->fd_ip4_filter_cnt = 0;
3570 	pf->fd_tcp6_filter_cnt = 0;
3571 	pf->fd_udp6_filter_cnt = 0;
3572 	pf->fd_sctp6_filter_cnt = 0;
3573 	pf->fd_ip6_filter_cnt = 0;
3574 }
3575 
3576 /**
3577  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3578  * @vsi: Pointer to the targeted VSI
3579  *
3580  * This function replays the hlist on the hw where all the SB Flow Director
3581  * filters were saved.
3582  **/
3583 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3584 {
3585 	struct i40e_fdir_filter *filter;
3586 	struct i40e_pf *pf = vsi->back;
3587 	struct hlist_node *node;
3588 
3589 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3590 		return;
3591 
3592 	/* Reset FDir counters as we're replaying all existing filters */
3593 	i40e_reset_fdir_filter_cnt(pf);
3594 
3595 	hlist_for_each_entry_safe(filter, node,
3596 				  &pf->fdir_filter_list, fdir_node) {
3597 		i40e_add_del_fdir(vsi, filter, true);
3598 	}
3599 }
3600 
3601 /**
3602  * i40e_vsi_configure - Set up the VSI for action
3603  * @vsi: the VSI being configured
3604  **/
3605 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3606 {
3607 	int err;
3608 
3609 	i40e_set_vsi_rx_mode(vsi);
3610 	i40e_restore_vlan(vsi);
3611 	i40e_vsi_config_dcb_rings(vsi);
3612 	err = i40e_vsi_configure_tx(vsi);
3613 	if (!err)
3614 		err = i40e_vsi_configure_rx(vsi);
3615 
3616 	return err;
3617 }
3618 
3619 /**
3620  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3621  * @vsi: the VSI being configured
3622  **/
3623 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3624 {
3625 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3626 	struct i40e_pf *pf = vsi->back;
3627 	struct i40e_hw *hw = &pf->hw;
3628 	u16 vector;
3629 	int i, q;
3630 	u32 qp;
3631 
3632 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3633 	 * and PFINT_LNKLSTn registers, e.g.:
3634 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3635 	 */
3636 	qp = vsi->base_queue;
3637 	vector = vsi->base_vector;
3638 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3639 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3640 
3641 		q_vector->rx.next_update = jiffies + 1;
3642 		q_vector->rx.target_itr =
3643 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3644 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3645 		     q_vector->rx.target_itr >> 1);
3646 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3647 
3648 		q_vector->tx.next_update = jiffies + 1;
3649 		q_vector->tx.target_itr =
3650 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3651 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3652 		     q_vector->tx.target_itr >> 1);
3653 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3654 
3655 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3656 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3657 
3658 		/* Linked list for the queuepairs assigned to this vector */
3659 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3660 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3661 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3662 			u32 val;
3663 
3664 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3665 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3666 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3667 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3668 			      (I40E_QUEUE_TYPE_TX <<
3669 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3670 
3671 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3672 
3673 			if (has_xdp) {
3674 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3675 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3676 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3677 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3678 				      (I40E_QUEUE_TYPE_TX <<
3679 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3680 
3681 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3682 			}
3683 
3684 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3685 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3686 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3687 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3688 			      (I40E_QUEUE_TYPE_RX <<
3689 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3690 
3691 			/* Terminate the linked list */
3692 			if (q == (q_vector->num_ringpairs - 1))
3693 				val |= (I40E_QUEUE_END_OF_LIST <<
3694 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3695 
3696 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3697 			qp++;
3698 		}
3699 	}
3700 
3701 	i40e_flush(hw);
3702 }
3703 
3704 /**
3705  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3706  * @pf: pointer to private device data structure
3707  **/
3708 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3709 {
3710 	struct i40e_hw *hw = &pf->hw;
3711 	u32 val;
3712 
3713 	/* clear things first */
3714 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3715 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3716 
3717 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3718 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3719 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3720 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3721 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3722 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3723 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3724 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3725 
3726 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3727 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3728 
3729 	if (pf->flags & I40E_FLAG_PTP)
3730 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3731 
3732 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3733 
3734 	/* SW_ITR_IDX = 0, but don't change INTENA */
3735 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3736 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3737 
3738 	/* OTHER_ITR_IDX = 0 */
3739 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3740 }
3741 
3742 /**
3743  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3744  * @vsi: the VSI being configured
3745  **/
3746 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3747 {
3748 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3749 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3750 	struct i40e_pf *pf = vsi->back;
3751 	struct i40e_hw *hw = &pf->hw;
3752 	u32 val;
3753 
3754 	/* set the ITR configuration */
3755 	q_vector->rx.next_update = jiffies + 1;
3756 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3757 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3758 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3759 	q_vector->tx.next_update = jiffies + 1;
3760 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3761 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3762 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3763 
3764 	i40e_enable_misc_int_causes(pf);
3765 
3766 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3767 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3768 
3769 	/* Associate the queue pair to the vector and enable the queue int */
3770 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3771 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3772 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3773 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3774 
3775 	wr32(hw, I40E_QINT_RQCTL(0), val);
3776 
3777 	if (i40e_enabled_xdp_vsi(vsi)) {
3778 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3779 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3780 		      (I40E_QUEUE_TYPE_TX
3781 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3782 
3783 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3784 	}
3785 
3786 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3787 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3788 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3789 
3790 	wr32(hw, I40E_QINT_TQCTL(0), val);
3791 	i40e_flush(hw);
3792 }
3793 
3794 /**
3795  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3796  * @pf: board private structure
3797  **/
3798 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3799 {
3800 	struct i40e_hw *hw = &pf->hw;
3801 
3802 	wr32(hw, I40E_PFINT_DYN_CTL0,
3803 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3804 	i40e_flush(hw);
3805 }
3806 
3807 /**
3808  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3809  * @pf: board private structure
3810  **/
3811 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3812 {
3813 	struct i40e_hw *hw = &pf->hw;
3814 	u32 val;
3815 
3816 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3817 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3818 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3819 
3820 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3821 	i40e_flush(hw);
3822 }
3823 
3824 /**
3825  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3826  * @irq: interrupt number
3827  * @data: pointer to a q_vector
3828  **/
3829 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3830 {
3831 	struct i40e_q_vector *q_vector = data;
3832 
3833 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3834 		return IRQ_HANDLED;
3835 
3836 	napi_schedule_irqoff(&q_vector->napi);
3837 
3838 	return IRQ_HANDLED;
3839 }
3840 
3841 /**
3842  * i40e_irq_affinity_notify - Callback for affinity changes
3843  * @notify: context as to what irq was changed
3844  * @mask: the new affinity mask
3845  *
3846  * This is a callback function used by the irq_set_affinity_notifier function
3847  * so that we may register to receive changes to the irq affinity masks.
3848  **/
3849 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3850 				     const cpumask_t *mask)
3851 {
3852 	struct i40e_q_vector *q_vector =
3853 		container_of(notify, struct i40e_q_vector, affinity_notify);
3854 
3855 	cpumask_copy(&q_vector->affinity_mask, mask);
3856 }
3857 
3858 /**
3859  * i40e_irq_affinity_release - Callback for affinity notifier release
3860  * @ref: internal core kernel usage
3861  *
3862  * This is a callback function used by the irq_set_affinity_notifier function
3863  * to inform the current notification subscriber that they will no longer
3864  * receive notifications.
3865  **/
3866 static void i40e_irq_affinity_release(struct kref *ref) {}
3867 
3868 /**
3869  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3870  * @vsi: the VSI being configured
3871  * @basename: name for the vector
3872  *
3873  * Allocates MSI-X vectors and requests interrupts from the kernel.
3874  **/
3875 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3876 {
3877 	int q_vectors = vsi->num_q_vectors;
3878 	struct i40e_pf *pf = vsi->back;
3879 	int base = vsi->base_vector;
3880 	int rx_int_idx = 0;
3881 	int tx_int_idx = 0;
3882 	int vector, err;
3883 	int irq_num;
3884 	int cpu;
3885 
3886 	for (vector = 0; vector < q_vectors; vector++) {
3887 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3888 
3889 		irq_num = pf->msix_entries[base + vector].vector;
3890 
3891 		if (q_vector->tx.ring && q_vector->rx.ring) {
3892 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3893 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3894 			tx_int_idx++;
3895 		} else if (q_vector->rx.ring) {
3896 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3897 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3898 		} else if (q_vector->tx.ring) {
3899 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3900 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3901 		} else {
3902 			/* skip this unused q_vector */
3903 			continue;
3904 		}
3905 		err = request_irq(irq_num,
3906 				  vsi->irq_handler,
3907 				  0,
3908 				  q_vector->name,
3909 				  q_vector);
3910 		if (err) {
3911 			dev_info(&pf->pdev->dev,
3912 				 "MSIX request_irq failed, error: %d\n", err);
3913 			goto free_queue_irqs;
3914 		}
3915 
3916 		/* register for affinity change notifications */
3917 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3918 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3919 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3920 		/* Spread affinity hints out across online CPUs.
3921 		 *
3922 		 * get_cpu_mask returns a static constant mask with
3923 		 * a permanent lifetime so it's ok to pass to
3924 		 * irq_update_affinity_hint without making a copy.
3925 		 */
3926 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3927 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
3928 	}
3929 
3930 	vsi->irqs_ready = true;
3931 	return 0;
3932 
3933 free_queue_irqs:
3934 	while (vector) {
3935 		vector--;
3936 		irq_num = pf->msix_entries[base + vector].vector;
3937 		irq_set_affinity_notifier(irq_num, NULL);
3938 		irq_update_affinity_hint(irq_num, NULL);
3939 		free_irq(irq_num, &vsi->q_vectors[vector]);
3940 	}
3941 	return err;
3942 }
3943 
3944 /**
3945  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3946  * @vsi: the VSI being un-configured
3947  **/
3948 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3949 {
3950 	struct i40e_pf *pf = vsi->back;
3951 	struct i40e_hw *hw = &pf->hw;
3952 	int base = vsi->base_vector;
3953 	int i;
3954 
3955 	/* disable interrupt causation from each queue */
3956 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3957 		u32 val;
3958 
3959 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3960 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3961 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3962 
3963 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3964 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3965 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3966 
3967 		if (!i40e_enabled_xdp_vsi(vsi))
3968 			continue;
3969 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3970 	}
3971 
3972 	/* disable each interrupt */
3973 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3974 		for (i = vsi->base_vector;
3975 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3976 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3977 
3978 		i40e_flush(hw);
3979 		for (i = 0; i < vsi->num_q_vectors; i++)
3980 			synchronize_irq(pf->msix_entries[i + base].vector);
3981 	} else {
3982 		/* Legacy and MSI mode - this stops all interrupt handling */
3983 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3984 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3985 		i40e_flush(hw);
3986 		synchronize_irq(pf->pdev->irq);
3987 	}
3988 }
3989 
3990 /**
3991  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3992  * @vsi: the VSI being configured
3993  **/
3994 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3995 {
3996 	struct i40e_pf *pf = vsi->back;
3997 	int i;
3998 
3999 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4000 		for (i = 0; i < vsi->num_q_vectors; i++)
4001 			i40e_irq_dynamic_enable(vsi, i);
4002 	} else {
4003 		i40e_irq_dynamic_enable_icr0(pf);
4004 	}
4005 
4006 	i40e_flush(&pf->hw);
4007 	return 0;
4008 }
4009 
4010 /**
4011  * i40e_free_misc_vector - Free the vector that handles non-queue events
4012  * @pf: board private structure
4013  **/
4014 static void i40e_free_misc_vector(struct i40e_pf *pf)
4015 {
4016 	/* Disable ICR 0 */
4017 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4018 	i40e_flush(&pf->hw);
4019 
4020 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4021 		synchronize_irq(pf->msix_entries[0].vector);
4022 		free_irq(pf->msix_entries[0].vector, pf);
4023 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4024 	}
4025 }
4026 
4027 /**
4028  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4029  * @irq: interrupt number
4030  * @data: pointer to a q_vector
4031  *
4032  * This is the handler used for all MSI/Legacy interrupts, and deals
4033  * with both queue and non-queue interrupts.  This is also used in
4034  * MSIX mode to handle the non-queue interrupts.
4035  **/
4036 static irqreturn_t i40e_intr(int irq, void *data)
4037 {
4038 	struct i40e_pf *pf = (struct i40e_pf *)data;
4039 	struct i40e_hw *hw = &pf->hw;
4040 	irqreturn_t ret = IRQ_NONE;
4041 	u32 icr0, icr0_remaining;
4042 	u32 val, ena_mask;
4043 
4044 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4045 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4046 
4047 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4048 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4049 		goto enable_intr;
4050 
4051 	/* if interrupt but no bits showing, must be SWINT */
4052 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4053 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4054 		pf->sw_int_count++;
4055 
4056 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4057 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4058 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4059 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4060 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4061 	}
4062 
4063 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4064 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4065 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4066 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4067 
4068 		/* We do not have a way to disarm Queue causes while leaving
4069 		 * interrupt enabled for all other causes, ideally
4070 		 * interrupt should be disabled while we are in NAPI but
4071 		 * this is not a performance path and napi_schedule()
4072 		 * can deal with rescheduling.
4073 		 */
4074 		if (!test_bit(__I40E_DOWN, pf->state))
4075 			napi_schedule_irqoff(&q_vector->napi);
4076 	}
4077 
4078 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4079 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4080 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4081 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4082 	}
4083 
4084 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4085 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4086 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4087 	}
4088 
4089 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4090 		/* disable any further VFLR event notifications */
4091 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4092 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4093 
4094 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4095 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4096 		} else {
4097 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4098 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4099 		}
4100 	}
4101 
4102 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4103 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4104 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4105 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4106 		val = rd32(hw, I40E_GLGEN_RSTAT);
4107 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4108 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4109 		if (val == I40E_RESET_CORER) {
4110 			pf->corer_count++;
4111 		} else if (val == I40E_RESET_GLOBR) {
4112 			pf->globr_count++;
4113 		} else if (val == I40E_RESET_EMPR) {
4114 			pf->empr_count++;
4115 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4116 		}
4117 	}
4118 
4119 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4120 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4121 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4122 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4123 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4124 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4125 	}
4126 
4127 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4128 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4129 
4130 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4131 			schedule_work(&pf->ptp_extts0_work);
4132 
4133 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4134 			i40e_ptp_tx_hwtstamp(pf);
4135 
4136 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4137 	}
4138 
4139 	/* If a critical error is pending we have no choice but to reset the
4140 	 * device.
4141 	 * Report and mask out any remaining unexpected interrupts.
4142 	 */
4143 	icr0_remaining = icr0 & ena_mask;
4144 	if (icr0_remaining) {
4145 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4146 			 icr0_remaining);
4147 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4148 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4149 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4150 			dev_info(&pf->pdev->dev, "device will be reset\n");
4151 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4152 			i40e_service_event_schedule(pf);
4153 		}
4154 		ena_mask &= ~icr0_remaining;
4155 	}
4156 	ret = IRQ_HANDLED;
4157 
4158 enable_intr:
4159 	/* re-enable interrupt causes */
4160 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4161 	if (!test_bit(__I40E_DOWN, pf->state) ||
4162 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4163 		i40e_service_event_schedule(pf);
4164 		i40e_irq_dynamic_enable_icr0(pf);
4165 	}
4166 
4167 	return ret;
4168 }
4169 
4170 /**
4171  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4172  * @tx_ring:  tx ring to clean
4173  * @budget:   how many cleans we're allowed
4174  *
4175  * Returns true if there's any budget left (e.g. the clean is finished)
4176  **/
4177 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4178 {
4179 	struct i40e_vsi *vsi = tx_ring->vsi;
4180 	u16 i = tx_ring->next_to_clean;
4181 	struct i40e_tx_buffer *tx_buf;
4182 	struct i40e_tx_desc *tx_desc;
4183 
4184 	tx_buf = &tx_ring->tx_bi[i];
4185 	tx_desc = I40E_TX_DESC(tx_ring, i);
4186 	i -= tx_ring->count;
4187 
4188 	do {
4189 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4190 
4191 		/* if next_to_watch is not set then there is no work pending */
4192 		if (!eop_desc)
4193 			break;
4194 
4195 		/* prevent any other reads prior to eop_desc */
4196 		smp_rmb();
4197 
4198 		/* if the descriptor isn't done, no work yet to do */
4199 		if (!(eop_desc->cmd_type_offset_bsz &
4200 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4201 			break;
4202 
4203 		/* clear next_to_watch to prevent false hangs */
4204 		tx_buf->next_to_watch = NULL;
4205 
4206 		tx_desc->buffer_addr = 0;
4207 		tx_desc->cmd_type_offset_bsz = 0;
4208 		/* move past filter desc */
4209 		tx_buf++;
4210 		tx_desc++;
4211 		i++;
4212 		if (unlikely(!i)) {
4213 			i -= tx_ring->count;
4214 			tx_buf = tx_ring->tx_bi;
4215 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4216 		}
4217 		/* unmap skb header data */
4218 		dma_unmap_single(tx_ring->dev,
4219 				 dma_unmap_addr(tx_buf, dma),
4220 				 dma_unmap_len(tx_buf, len),
4221 				 DMA_TO_DEVICE);
4222 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4223 			kfree(tx_buf->raw_buf);
4224 
4225 		tx_buf->raw_buf = NULL;
4226 		tx_buf->tx_flags = 0;
4227 		tx_buf->next_to_watch = NULL;
4228 		dma_unmap_len_set(tx_buf, len, 0);
4229 		tx_desc->buffer_addr = 0;
4230 		tx_desc->cmd_type_offset_bsz = 0;
4231 
4232 		/* move us past the eop_desc for start of next FD desc */
4233 		tx_buf++;
4234 		tx_desc++;
4235 		i++;
4236 		if (unlikely(!i)) {
4237 			i -= tx_ring->count;
4238 			tx_buf = tx_ring->tx_bi;
4239 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4240 		}
4241 
4242 		/* update budget accounting */
4243 		budget--;
4244 	} while (likely(budget));
4245 
4246 	i += tx_ring->count;
4247 	tx_ring->next_to_clean = i;
4248 
4249 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4250 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4251 
4252 	return budget > 0;
4253 }
4254 
4255 /**
4256  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4257  * @irq: interrupt number
4258  * @data: pointer to a q_vector
4259  **/
4260 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4261 {
4262 	struct i40e_q_vector *q_vector = data;
4263 	struct i40e_vsi *vsi;
4264 
4265 	if (!q_vector->tx.ring)
4266 		return IRQ_HANDLED;
4267 
4268 	vsi = q_vector->tx.ring->vsi;
4269 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4270 
4271 	return IRQ_HANDLED;
4272 }
4273 
4274 /**
4275  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4276  * @vsi: the VSI being configured
4277  * @v_idx: vector index
4278  * @qp_idx: queue pair index
4279  **/
4280 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4281 {
4282 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4283 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4284 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4285 
4286 	tx_ring->q_vector = q_vector;
4287 	tx_ring->next = q_vector->tx.ring;
4288 	q_vector->tx.ring = tx_ring;
4289 	q_vector->tx.count++;
4290 
4291 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4292 	if (i40e_enabled_xdp_vsi(vsi)) {
4293 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4294 
4295 		xdp_ring->q_vector = q_vector;
4296 		xdp_ring->next = q_vector->tx.ring;
4297 		q_vector->tx.ring = xdp_ring;
4298 		q_vector->tx.count++;
4299 	}
4300 
4301 	rx_ring->q_vector = q_vector;
4302 	rx_ring->next = q_vector->rx.ring;
4303 	q_vector->rx.ring = rx_ring;
4304 	q_vector->rx.count++;
4305 }
4306 
4307 /**
4308  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4309  * @vsi: the VSI being configured
4310  *
4311  * This function maps descriptor rings to the queue-specific vectors
4312  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4313  * one vector per queue pair, but on a constrained vector budget, we
4314  * group the queue pairs as "efficiently" as possible.
4315  **/
4316 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4317 {
4318 	int qp_remaining = vsi->num_queue_pairs;
4319 	int q_vectors = vsi->num_q_vectors;
4320 	int num_ringpairs;
4321 	int v_start = 0;
4322 	int qp_idx = 0;
4323 
4324 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4325 	 * group them so there are multiple queues per vector.
4326 	 * It is also important to go through all the vectors available to be
4327 	 * sure that if we don't use all the vectors, that the remaining vectors
4328 	 * are cleared. This is especially important when decreasing the
4329 	 * number of queues in use.
4330 	 */
4331 	for (; v_start < q_vectors; v_start++) {
4332 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4333 
4334 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4335 
4336 		q_vector->num_ringpairs = num_ringpairs;
4337 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4338 
4339 		q_vector->rx.count = 0;
4340 		q_vector->tx.count = 0;
4341 		q_vector->rx.ring = NULL;
4342 		q_vector->tx.ring = NULL;
4343 
4344 		while (num_ringpairs--) {
4345 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4346 			qp_idx++;
4347 			qp_remaining--;
4348 		}
4349 	}
4350 }
4351 
4352 /**
4353  * i40e_vsi_request_irq - Request IRQ from the OS
4354  * @vsi: the VSI being configured
4355  * @basename: name for the vector
4356  **/
4357 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4358 {
4359 	struct i40e_pf *pf = vsi->back;
4360 	int err;
4361 
4362 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4363 		err = i40e_vsi_request_irq_msix(vsi, basename);
4364 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4365 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4366 				  pf->int_name, pf);
4367 	else
4368 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4369 				  pf->int_name, pf);
4370 
4371 	if (err)
4372 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4373 
4374 	return err;
4375 }
4376 
4377 #ifdef CONFIG_NET_POLL_CONTROLLER
4378 /**
4379  * i40e_netpoll - A Polling 'interrupt' handler
4380  * @netdev: network interface device structure
4381  *
4382  * This is used by netconsole to send skbs without having to re-enable
4383  * interrupts.  It's not called while the normal interrupt routine is executing.
4384  **/
4385 static void i40e_netpoll(struct net_device *netdev)
4386 {
4387 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4388 	struct i40e_vsi *vsi = np->vsi;
4389 	struct i40e_pf *pf = vsi->back;
4390 	int i;
4391 
4392 	/* if interface is down do nothing */
4393 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4394 		return;
4395 
4396 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4397 		for (i = 0; i < vsi->num_q_vectors; i++)
4398 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4399 	} else {
4400 		i40e_intr(pf->pdev->irq, netdev);
4401 	}
4402 }
4403 #endif
4404 
4405 #define I40E_QTX_ENA_WAIT_COUNT 50
4406 
4407 /**
4408  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4409  * @pf: the PF being configured
4410  * @pf_q: the PF queue
4411  * @enable: enable or disable state of the queue
4412  *
4413  * This routine will wait for the given Tx queue of the PF to reach the
4414  * enabled or disabled state.
4415  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4416  * multiple retries; else will return 0 in case of success.
4417  **/
4418 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4419 {
4420 	int i;
4421 	u32 tx_reg;
4422 
4423 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4424 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4425 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4426 			break;
4427 
4428 		usleep_range(10, 20);
4429 	}
4430 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4431 		return -ETIMEDOUT;
4432 
4433 	return 0;
4434 }
4435 
4436 /**
4437  * i40e_control_tx_q - Start or stop a particular Tx queue
4438  * @pf: the PF structure
4439  * @pf_q: the PF queue to configure
4440  * @enable: start or stop the queue
4441  *
4442  * This function enables or disables a single queue. Note that any delay
4443  * required after the operation is expected to be handled by the caller of
4444  * this function.
4445  **/
4446 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4447 {
4448 	struct i40e_hw *hw = &pf->hw;
4449 	u32 tx_reg;
4450 	int i;
4451 
4452 	/* warn the TX unit of coming changes */
4453 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4454 	if (!enable)
4455 		usleep_range(10, 20);
4456 
4457 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4458 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4459 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4460 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4461 			break;
4462 		usleep_range(1000, 2000);
4463 	}
4464 
4465 	/* Skip if the queue is already in the requested state */
4466 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4467 		return;
4468 
4469 	/* turn on/off the queue */
4470 	if (enable) {
4471 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4472 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4473 	} else {
4474 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4475 	}
4476 
4477 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4478 }
4479 
4480 /**
4481  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4482  * @seid: VSI SEID
4483  * @pf: the PF structure
4484  * @pf_q: the PF queue to configure
4485  * @is_xdp: true if the queue is used for XDP
4486  * @enable: start or stop the queue
4487  **/
4488 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4489 			   bool is_xdp, bool enable)
4490 {
4491 	int ret;
4492 
4493 	i40e_control_tx_q(pf, pf_q, enable);
4494 
4495 	/* wait for the change to finish */
4496 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4497 	if (ret) {
4498 		dev_info(&pf->pdev->dev,
4499 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4500 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4501 			 (enable ? "en" : "dis"));
4502 	}
4503 
4504 	return ret;
4505 }
4506 
4507 /**
4508  * i40e_vsi_enable_tx - Start a VSI's rings
4509  * @vsi: the VSI being configured
4510  **/
4511 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4512 {
4513 	struct i40e_pf *pf = vsi->back;
4514 	int i, pf_q, ret = 0;
4515 
4516 	pf_q = vsi->base_queue;
4517 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4518 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4519 					     pf_q,
4520 					     false /*is xdp*/, true);
4521 		if (ret)
4522 			break;
4523 
4524 		if (!i40e_enabled_xdp_vsi(vsi))
4525 			continue;
4526 
4527 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4528 					     pf_q + vsi->alloc_queue_pairs,
4529 					     true /*is xdp*/, true);
4530 		if (ret)
4531 			break;
4532 	}
4533 	return ret;
4534 }
4535 
4536 /**
4537  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4538  * @pf: the PF being configured
4539  * @pf_q: the PF queue
4540  * @enable: enable or disable state of the queue
4541  *
4542  * This routine will wait for the given Rx queue of the PF to reach the
4543  * enabled or disabled state.
4544  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4545  * multiple retries; else will return 0 in case of success.
4546  **/
4547 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4548 {
4549 	int i;
4550 	u32 rx_reg;
4551 
4552 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4553 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4554 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4555 			break;
4556 
4557 		usleep_range(10, 20);
4558 	}
4559 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4560 		return -ETIMEDOUT;
4561 
4562 	return 0;
4563 }
4564 
4565 /**
4566  * i40e_control_rx_q - Start or stop a particular Rx queue
4567  * @pf: the PF structure
4568  * @pf_q: the PF queue to configure
4569  * @enable: start or stop the queue
4570  *
4571  * This function enables or disables a single queue. Note that
4572  * any delay required after the operation is expected to be
4573  * handled by the caller of this function.
4574  **/
4575 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4576 {
4577 	struct i40e_hw *hw = &pf->hw;
4578 	u32 rx_reg;
4579 	int i;
4580 
4581 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4582 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4583 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4584 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4585 			break;
4586 		usleep_range(1000, 2000);
4587 	}
4588 
4589 	/* Skip if the queue is already in the requested state */
4590 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4591 		return;
4592 
4593 	/* turn on/off the queue */
4594 	if (enable)
4595 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4596 	else
4597 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4598 
4599 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4600 }
4601 
4602 /**
4603  * i40e_control_wait_rx_q
4604  * @pf: the PF structure
4605  * @pf_q: queue being configured
4606  * @enable: start or stop the rings
4607  *
4608  * This function enables or disables a single queue along with waiting
4609  * for the change to finish. The caller of this function should handle
4610  * the delays needed in the case of disabling queues.
4611  **/
4612 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4613 {
4614 	int ret = 0;
4615 
4616 	i40e_control_rx_q(pf, pf_q, enable);
4617 
4618 	/* wait for the change to finish */
4619 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4620 	if (ret)
4621 		return ret;
4622 
4623 	return ret;
4624 }
4625 
4626 /**
4627  * i40e_vsi_enable_rx - Start a VSI's rings
4628  * @vsi: the VSI being configured
4629  **/
4630 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4631 {
4632 	struct i40e_pf *pf = vsi->back;
4633 	int i, pf_q, ret = 0;
4634 
4635 	pf_q = vsi->base_queue;
4636 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4637 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4638 		if (ret) {
4639 			dev_info(&pf->pdev->dev,
4640 				 "VSI seid %d Rx ring %d enable timeout\n",
4641 				 vsi->seid, pf_q);
4642 			break;
4643 		}
4644 	}
4645 
4646 	return ret;
4647 }
4648 
4649 /**
4650  * i40e_vsi_start_rings - Start a VSI's rings
4651  * @vsi: the VSI being configured
4652  **/
4653 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4654 {
4655 	int ret = 0;
4656 
4657 	/* do rx first for enable and last for disable */
4658 	ret = i40e_vsi_enable_rx(vsi);
4659 	if (ret)
4660 		return ret;
4661 	ret = i40e_vsi_enable_tx(vsi);
4662 
4663 	return ret;
4664 }
4665 
4666 #define I40E_DISABLE_TX_GAP_MSEC	50
4667 
4668 /**
4669  * i40e_vsi_stop_rings - Stop a VSI's rings
4670  * @vsi: the VSI being configured
4671  **/
4672 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4673 {
4674 	struct i40e_pf *pf = vsi->back;
4675 	int pf_q, err, q_end;
4676 
4677 	/* When port TX is suspended, don't wait */
4678 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4679 		return i40e_vsi_stop_rings_no_wait(vsi);
4680 
4681 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4682 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4683 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4684 
4685 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4686 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4687 		if (err)
4688 			dev_info(&pf->pdev->dev,
4689 				 "VSI seid %d Rx ring %d disable timeout\n",
4690 				 vsi->seid, pf_q);
4691 	}
4692 
4693 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4694 	pf_q = vsi->base_queue;
4695 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4696 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4697 
4698 	i40e_vsi_wait_queues_disabled(vsi);
4699 }
4700 
4701 /**
4702  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4703  * @vsi: the VSI being shutdown
4704  *
4705  * This function stops all the rings for a VSI but does not delay to verify
4706  * that rings have been disabled. It is expected that the caller is shutting
4707  * down multiple VSIs at once and will delay together for all the VSIs after
4708  * initiating the shutdown. This is particularly useful for shutting down lots
4709  * of VFs together. Otherwise, a large delay can be incurred while configuring
4710  * each VSI in serial.
4711  **/
4712 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4713 {
4714 	struct i40e_pf *pf = vsi->back;
4715 	int i, pf_q;
4716 
4717 	pf_q = vsi->base_queue;
4718 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4719 		i40e_control_tx_q(pf, pf_q, false);
4720 		i40e_control_rx_q(pf, pf_q, false);
4721 	}
4722 }
4723 
4724 /**
4725  * i40e_vsi_free_irq - Free the irq association with the OS
4726  * @vsi: the VSI being configured
4727  **/
4728 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4729 {
4730 	struct i40e_pf *pf = vsi->back;
4731 	struct i40e_hw *hw = &pf->hw;
4732 	int base = vsi->base_vector;
4733 	u32 val, qp;
4734 	int i;
4735 
4736 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4737 		if (!vsi->q_vectors)
4738 			return;
4739 
4740 		if (!vsi->irqs_ready)
4741 			return;
4742 
4743 		vsi->irqs_ready = false;
4744 		for (i = 0; i < vsi->num_q_vectors; i++) {
4745 			int irq_num;
4746 			u16 vector;
4747 
4748 			vector = i + base;
4749 			irq_num = pf->msix_entries[vector].vector;
4750 
4751 			/* free only the irqs that were actually requested */
4752 			if (!vsi->q_vectors[i] ||
4753 			    !vsi->q_vectors[i]->num_ringpairs)
4754 				continue;
4755 
4756 			/* clear the affinity notifier in the IRQ descriptor */
4757 			irq_set_affinity_notifier(irq_num, NULL);
4758 			/* remove our suggested affinity mask for this IRQ */
4759 			irq_update_affinity_hint(irq_num, NULL);
4760 			synchronize_irq(irq_num);
4761 			free_irq(irq_num, vsi->q_vectors[i]);
4762 
4763 			/* Tear down the interrupt queue link list
4764 			 *
4765 			 * We know that they come in pairs and always
4766 			 * the Rx first, then the Tx.  To clear the
4767 			 * link list, stick the EOL value into the
4768 			 * next_q field of the registers.
4769 			 */
4770 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4771 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4772 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4773 			val |= I40E_QUEUE_END_OF_LIST
4774 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4775 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4776 
4777 			while (qp != I40E_QUEUE_END_OF_LIST) {
4778 				u32 next;
4779 
4780 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4781 
4782 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4783 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4784 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4785 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4786 
4787 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4788 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4789 
4790 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4791 
4792 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4793 
4794 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4795 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4796 
4797 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4798 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4799 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4800 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4801 
4802 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4803 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4804 
4805 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4806 				qp = next;
4807 			}
4808 		}
4809 	} else {
4810 		free_irq(pf->pdev->irq, pf);
4811 
4812 		val = rd32(hw, I40E_PFINT_LNKLST0);
4813 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4814 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4815 		val |= I40E_QUEUE_END_OF_LIST
4816 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4817 		wr32(hw, I40E_PFINT_LNKLST0, val);
4818 
4819 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4820 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4821 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4822 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4823 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4824 
4825 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4826 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4827 
4828 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4829 
4830 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4831 
4832 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4833 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4834 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4835 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4836 
4837 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4838 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4839 
4840 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4841 	}
4842 }
4843 
4844 /**
4845  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4846  * @vsi: the VSI being configured
4847  * @v_idx: Index of vector to be freed
4848  *
4849  * This function frees the memory allocated to the q_vector.  In addition if
4850  * NAPI is enabled it will delete any references to the NAPI struct prior
4851  * to freeing the q_vector.
4852  **/
4853 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4854 {
4855 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4856 	struct i40e_ring *ring;
4857 
4858 	if (!q_vector)
4859 		return;
4860 
4861 	/* disassociate q_vector from rings */
4862 	i40e_for_each_ring(ring, q_vector->tx)
4863 		ring->q_vector = NULL;
4864 
4865 	i40e_for_each_ring(ring, q_vector->rx)
4866 		ring->q_vector = NULL;
4867 
4868 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4869 	if (vsi->netdev)
4870 		netif_napi_del(&q_vector->napi);
4871 
4872 	vsi->q_vectors[v_idx] = NULL;
4873 
4874 	kfree_rcu(q_vector, rcu);
4875 }
4876 
4877 /**
4878  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4879  * @vsi: the VSI being un-configured
4880  *
4881  * This frees the memory allocated to the q_vectors and
4882  * deletes references to the NAPI struct.
4883  **/
4884 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4885 {
4886 	int v_idx;
4887 
4888 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4889 		i40e_free_q_vector(vsi, v_idx);
4890 }
4891 
4892 /**
4893  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4894  * @pf: board private structure
4895  **/
4896 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4897 {
4898 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4899 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4900 		pci_disable_msix(pf->pdev);
4901 		kfree(pf->msix_entries);
4902 		pf->msix_entries = NULL;
4903 		kfree(pf->irq_pile);
4904 		pf->irq_pile = NULL;
4905 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4906 		pci_disable_msi(pf->pdev);
4907 	}
4908 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4909 }
4910 
4911 /**
4912  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4913  * @pf: board private structure
4914  *
4915  * We go through and clear interrupt specific resources and reset the structure
4916  * to pre-load conditions
4917  **/
4918 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4919 {
4920 	int i;
4921 
4922 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
4923 		i40e_free_misc_vector(pf);
4924 
4925 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4926 		      I40E_IWARP_IRQ_PILE_ID);
4927 
4928 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4929 	for (i = 0; i < pf->num_alloc_vsi; i++)
4930 		if (pf->vsi[i])
4931 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4932 	i40e_reset_interrupt_capability(pf);
4933 }
4934 
4935 /**
4936  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4937  * @vsi: the VSI being configured
4938  **/
4939 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4940 {
4941 	int q_idx;
4942 
4943 	if (!vsi->netdev)
4944 		return;
4945 
4946 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4947 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4948 
4949 		if (q_vector->rx.ring || q_vector->tx.ring)
4950 			napi_enable(&q_vector->napi);
4951 	}
4952 }
4953 
4954 /**
4955  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4956  * @vsi: the VSI being configured
4957  **/
4958 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4959 {
4960 	int q_idx;
4961 
4962 	if (!vsi->netdev)
4963 		return;
4964 
4965 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4966 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4967 
4968 		if (q_vector->rx.ring || q_vector->tx.ring)
4969 			napi_disable(&q_vector->napi);
4970 	}
4971 }
4972 
4973 /**
4974  * i40e_vsi_close - Shut down a VSI
4975  * @vsi: the vsi to be quelled
4976  **/
4977 static void i40e_vsi_close(struct i40e_vsi *vsi)
4978 {
4979 	struct i40e_pf *pf = vsi->back;
4980 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4981 		i40e_down(vsi);
4982 	i40e_vsi_free_irq(vsi);
4983 	i40e_vsi_free_tx_resources(vsi);
4984 	i40e_vsi_free_rx_resources(vsi);
4985 	vsi->current_netdev_flags = 0;
4986 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4987 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4988 		set_bit(__I40E_CLIENT_RESET, pf->state);
4989 }
4990 
4991 /**
4992  * i40e_quiesce_vsi - Pause a given VSI
4993  * @vsi: the VSI being paused
4994  **/
4995 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4996 {
4997 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4998 		return;
4999 
5000 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5001 	if (vsi->netdev && netif_running(vsi->netdev))
5002 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5003 	else
5004 		i40e_vsi_close(vsi);
5005 }
5006 
5007 /**
5008  * i40e_unquiesce_vsi - Resume a given VSI
5009  * @vsi: the VSI being resumed
5010  **/
5011 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5012 {
5013 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5014 		return;
5015 
5016 	if (vsi->netdev && netif_running(vsi->netdev))
5017 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5018 	else
5019 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5020 }
5021 
5022 /**
5023  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5024  * @pf: the PF
5025  **/
5026 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5027 {
5028 	int v;
5029 
5030 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5031 		if (pf->vsi[v])
5032 			i40e_quiesce_vsi(pf->vsi[v]);
5033 	}
5034 }
5035 
5036 /**
5037  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5038  * @pf: the PF
5039  **/
5040 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5041 {
5042 	int v;
5043 
5044 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5045 		if (pf->vsi[v])
5046 			i40e_unquiesce_vsi(pf->vsi[v]);
5047 	}
5048 }
5049 
5050 /**
5051  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5052  * @vsi: the VSI being configured
5053  *
5054  * Wait until all queues on a given VSI have been disabled.
5055  **/
5056 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5057 {
5058 	struct i40e_pf *pf = vsi->back;
5059 	int i, pf_q, ret;
5060 
5061 	pf_q = vsi->base_queue;
5062 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5063 		/* Check and wait for the Tx queue */
5064 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5065 		if (ret) {
5066 			dev_info(&pf->pdev->dev,
5067 				 "VSI seid %d Tx ring %d disable timeout\n",
5068 				 vsi->seid, pf_q);
5069 			return ret;
5070 		}
5071 
5072 		if (!i40e_enabled_xdp_vsi(vsi))
5073 			goto wait_rx;
5074 
5075 		/* Check and wait for the XDP Tx queue */
5076 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5077 				       false);
5078 		if (ret) {
5079 			dev_info(&pf->pdev->dev,
5080 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5081 				 vsi->seid, pf_q);
5082 			return ret;
5083 		}
5084 wait_rx:
5085 		/* Check and wait for the Rx queue */
5086 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5087 		if (ret) {
5088 			dev_info(&pf->pdev->dev,
5089 				 "VSI seid %d Rx ring %d disable timeout\n",
5090 				 vsi->seid, pf_q);
5091 			return ret;
5092 		}
5093 	}
5094 
5095 	return 0;
5096 }
5097 
5098 #ifdef CONFIG_I40E_DCB
5099 /**
5100  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5101  * @pf: the PF
5102  *
5103  * This function waits for the queues to be in disabled state for all the
5104  * VSIs that are managed by this PF.
5105  **/
5106 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5107 {
5108 	int v, ret = 0;
5109 
5110 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5111 		if (pf->vsi[v]) {
5112 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5113 			if (ret)
5114 				break;
5115 		}
5116 	}
5117 
5118 	return ret;
5119 }
5120 
5121 #endif
5122 
5123 /**
5124  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5125  * @pf: pointer to PF
5126  *
5127  * Get TC map for ISCSI PF type that will include iSCSI TC
5128  * and LAN TC.
5129  **/
5130 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5131 {
5132 	struct i40e_dcb_app_priority_table app;
5133 	struct i40e_hw *hw = &pf->hw;
5134 	u8 enabled_tc = 1; /* TC0 is always enabled */
5135 	u8 tc, i;
5136 	/* Get the iSCSI APP TLV */
5137 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5138 
5139 	for (i = 0; i < dcbcfg->numapps; i++) {
5140 		app = dcbcfg->app[i];
5141 		if (app.selector == I40E_APP_SEL_TCPIP &&
5142 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5143 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5144 			enabled_tc |= BIT(tc);
5145 			break;
5146 		}
5147 	}
5148 
5149 	return enabled_tc;
5150 }
5151 
5152 /**
5153  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5154  * @dcbcfg: the corresponding DCBx configuration structure
5155  *
5156  * Return the number of TCs from given DCBx configuration
5157  **/
5158 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5159 {
5160 	int i, tc_unused = 0;
5161 	u8 num_tc = 0;
5162 	u8 ret = 0;
5163 
5164 	/* Scan the ETS Config Priority Table to find
5165 	 * traffic class enabled for a given priority
5166 	 * and create a bitmask of enabled TCs
5167 	 */
5168 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5169 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5170 
5171 	/* Now scan the bitmask to check for
5172 	 * contiguous TCs starting with TC0
5173 	 */
5174 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5175 		if (num_tc & BIT(i)) {
5176 			if (!tc_unused) {
5177 				ret++;
5178 			} else {
5179 				pr_err("Non-contiguous TC - Disabling DCB\n");
5180 				return 1;
5181 			}
5182 		} else {
5183 			tc_unused = 1;
5184 		}
5185 	}
5186 
5187 	/* There is always at least TC0 */
5188 	if (!ret)
5189 		ret = 1;
5190 
5191 	return ret;
5192 }
5193 
5194 /**
5195  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5196  * @dcbcfg: the corresponding DCBx configuration structure
5197  *
5198  * Query the current DCB configuration and return the number of
5199  * traffic classes enabled from the given DCBX config
5200  **/
5201 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5202 {
5203 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5204 	u8 enabled_tc = 1;
5205 	u8 i;
5206 
5207 	for (i = 0; i < num_tc; i++)
5208 		enabled_tc |= BIT(i);
5209 
5210 	return enabled_tc;
5211 }
5212 
5213 /**
5214  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5215  * @pf: PF being queried
5216  *
5217  * Query the current MQPRIO configuration and return the number of
5218  * traffic classes enabled.
5219  **/
5220 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5221 {
5222 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5223 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5224 	u8 enabled_tc = 1, i;
5225 
5226 	for (i = 1; i < num_tc; i++)
5227 		enabled_tc |= BIT(i);
5228 	return enabled_tc;
5229 }
5230 
5231 /**
5232  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5233  * @pf: PF being queried
5234  *
5235  * Return number of traffic classes enabled for the given PF
5236  **/
5237 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5238 {
5239 	struct i40e_hw *hw = &pf->hw;
5240 	u8 i, enabled_tc = 1;
5241 	u8 num_tc = 0;
5242 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5243 
5244 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5245 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5246 
5247 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5248 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5249 		return 1;
5250 
5251 	/* SFP mode will be enabled for all TCs on port */
5252 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5253 		return i40e_dcb_get_num_tc(dcbcfg);
5254 
5255 	/* MFP mode return count of enabled TCs for this PF */
5256 	if (pf->hw.func_caps.iscsi)
5257 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5258 	else
5259 		return 1; /* Only TC0 */
5260 
5261 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5262 		if (enabled_tc & BIT(i))
5263 			num_tc++;
5264 	}
5265 	return num_tc;
5266 }
5267 
5268 /**
5269  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5270  * @pf: PF being queried
5271  *
5272  * Return a bitmap for enabled traffic classes for this PF.
5273  **/
5274 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5275 {
5276 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5277 		return i40e_mqprio_get_enabled_tc(pf);
5278 
5279 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5280 	 * default TC
5281 	 */
5282 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5283 		return I40E_DEFAULT_TRAFFIC_CLASS;
5284 
5285 	/* SFP mode we want PF to be enabled for all TCs */
5286 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5287 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5288 
5289 	/* MFP enabled and iSCSI PF type */
5290 	if (pf->hw.func_caps.iscsi)
5291 		return i40e_get_iscsi_tc_map(pf);
5292 	else
5293 		return I40E_DEFAULT_TRAFFIC_CLASS;
5294 }
5295 
5296 /**
5297  * i40e_vsi_get_bw_info - Query VSI BW Information
5298  * @vsi: the VSI being queried
5299  *
5300  * Returns 0 on success, negative value on failure
5301  **/
5302 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5303 {
5304 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5305 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5306 	struct i40e_pf *pf = vsi->back;
5307 	struct i40e_hw *hw = &pf->hw;
5308 	i40e_status ret;
5309 	u32 tc_bw_max;
5310 	int i;
5311 
5312 	/* Get the VSI level BW configuration */
5313 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5314 	if (ret) {
5315 		dev_info(&pf->pdev->dev,
5316 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5317 			 i40e_stat_str(&pf->hw, ret),
5318 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5319 		return -EINVAL;
5320 	}
5321 
5322 	/* Get the VSI level BW configuration per TC */
5323 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5324 					       NULL);
5325 	if (ret) {
5326 		dev_info(&pf->pdev->dev,
5327 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5328 			 i40e_stat_str(&pf->hw, ret),
5329 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5330 		return -EINVAL;
5331 	}
5332 
5333 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5334 		dev_info(&pf->pdev->dev,
5335 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5336 			 bw_config.tc_valid_bits,
5337 			 bw_ets_config.tc_valid_bits);
5338 		/* Still continuing */
5339 	}
5340 
5341 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5342 	vsi->bw_max_quanta = bw_config.max_bw;
5343 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5344 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5345 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5346 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5347 		vsi->bw_ets_limit_credits[i] =
5348 					le16_to_cpu(bw_ets_config.credits[i]);
5349 		/* 3 bits out of 4 for each TC */
5350 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5351 	}
5352 
5353 	return 0;
5354 }
5355 
5356 /**
5357  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5358  * @vsi: the VSI being configured
5359  * @enabled_tc: TC bitmap
5360  * @bw_share: BW shared credits per TC
5361  *
5362  * Returns 0 on success, negative value on failure
5363  **/
5364 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5365 				       u8 *bw_share)
5366 {
5367 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5368 	struct i40e_pf *pf = vsi->back;
5369 	i40e_status ret;
5370 	int i;
5371 
5372 	/* There is no need to reset BW when mqprio mode is on.  */
5373 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5374 		return 0;
5375 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5376 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5377 		if (ret)
5378 			dev_info(&pf->pdev->dev,
5379 				 "Failed to reset tx rate for vsi->seid %u\n",
5380 				 vsi->seid);
5381 		return ret;
5382 	}
5383 	memset(&bw_data, 0, sizeof(bw_data));
5384 	bw_data.tc_valid_bits = enabled_tc;
5385 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5386 		bw_data.tc_bw_credits[i] = bw_share[i];
5387 
5388 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5389 	if (ret) {
5390 		dev_info(&pf->pdev->dev,
5391 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5392 			 pf->hw.aq.asq_last_status);
5393 		return -EINVAL;
5394 	}
5395 
5396 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5397 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5398 
5399 	return 0;
5400 }
5401 
5402 /**
5403  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5404  * @vsi: the VSI being configured
5405  * @enabled_tc: TC map to be enabled
5406  *
5407  **/
5408 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5409 {
5410 	struct net_device *netdev = vsi->netdev;
5411 	struct i40e_pf *pf = vsi->back;
5412 	struct i40e_hw *hw = &pf->hw;
5413 	u8 netdev_tc = 0;
5414 	int i;
5415 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5416 
5417 	if (!netdev)
5418 		return;
5419 
5420 	if (!enabled_tc) {
5421 		netdev_reset_tc(netdev);
5422 		return;
5423 	}
5424 
5425 	/* Set up actual enabled TCs on the VSI */
5426 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5427 		return;
5428 
5429 	/* set per TC queues for the VSI */
5430 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5431 		/* Only set TC queues for enabled tcs
5432 		 *
5433 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5434 		 * enabled_tc bitmap would be 0x00001001; the driver
5435 		 * will set the numtc for netdev as 2 that will be
5436 		 * referenced by the netdev layer as TC 0 and 1.
5437 		 */
5438 		if (vsi->tc_config.enabled_tc & BIT(i))
5439 			netdev_set_tc_queue(netdev,
5440 					vsi->tc_config.tc_info[i].netdev_tc,
5441 					vsi->tc_config.tc_info[i].qcount,
5442 					vsi->tc_config.tc_info[i].qoffset);
5443 	}
5444 
5445 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5446 		return;
5447 
5448 	/* Assign UP2TC map for the VSI */
5449 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5450 		/* Get the actual TC# for the UP */
5451 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5452 		/* Get the mapped netdev TC# for the UP */
5453 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5454 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5455 	}
5456 }
5457 
5458 /**
5459  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5460  * @vsi: the VSI being configured
5461  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5462  **/
5463 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5464 				      struct i40e_vsi_context *ctxt)
5465 {
5466 	/* copy just the sections touched not the entire info
5467 	 * since not all sections are valid as returned by
5468 	 * update vsi params
5469 	 */
5470 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5471 	memcpy(&vsi->info.queue_mapping,
5472 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5473 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5474 	       sizeof(vsi->info.tc_mapping));
5475 }
5476 
5477 /**
5478  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5479  * @vsi: the VSI being reconfigured
5480  * @vsi_offset: offset from main VF VSI
5481  */
5482 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5483 {
5484 	struct i40e_vsi_context ctxt = {};
5485 	struct i40e_pf *pf;
5486 	struct i40e_hw *hw;
5487 	int ret;
5488 
5489 	if (!vsi)
5490 		return I40E_ERR_PARAM;
5491 	pf = vsi->back;
5492 	hw = &pf->hw;
5493 
5494 	ctxt.seid = vsi->seid;
5495 	ctxt.pf_num = hw->pf_id;
5496 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5497 	ctxt.uplink_seid = vsi->uplink_seid;
5498 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5499 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5500 	ctxt.info = vsi->info;
5501 
5502 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5503 				 false);
5504 	if (vsi->reconfig_rss) {
5505 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5506 				      vsi->num_queue_pairs);
5507 		ret = i40e_vsi_config_rss(vsi);
5508 		if (ret) {
5509 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5510 			return ret;
5511 		}
5512 		vsi->reconfig_rss = false;
5513 	}
5514 
5515 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5516 	if (ret) {
5517 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5518 			 i40e_stat_str(hw, ret),
5519 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5520 		return ret;
5521 	}
5522 	/* update the local VSI info with updated queue map */
5523 	i40e_vsi_update_queue_map(vsi, &ctxt);
5524 	vsi->info.valid_sections = 0;
5525 
5526 	return ret;
5527 }
5528 
5529 /**
5530  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5531  * @vsi: VSI to be configured
5532  * @enabled_tc: TC bitmap
5533  *
5534  * This configures a particular VSI for TCs that are mapped to the
5535  * given TC bitmap. It uses default bandwidth share for TCs across
5536  * VSIs to configure TC for a particular VSI.
5537  *
5538  * NOTE:
5539  * It is expected that the VSI queues have been quisced before calling
5540  * this function.
5541  **/
5542 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5543 {
5544 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5545 	struct i40e_pf *pf = vsi->back;
5546 	struct i40e_hw *hw = &pf->hw;
5547 	struct i40e_vsi_context ctxt;
5548 	int ret = 0;
5549 	int i;
5550 
5551 	/* Check if enabled_tc is same as existing or new TCs */
5552 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5553 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5554 		return ret;
5555 
5556 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5557 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5558 		if (enabled_tc & BIT(i))
5559 			bw_share[i] = 1;
5560 	}
5561 
5562 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5563 	if (ret) {
5564 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5565 
5566 		dev_info(&pf->pdev->dev,
5567 			 "Failed configuring TC map %d for VSI %d\n",
5568 			 enabled_tc, vsi->seid);
5569 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5570 						  &bw_config, NULL);
5571 		if (ret) {
5572 			dev_info(&pf->pdev->dev,
5573 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5574 				 i40e_stat_str(hw, ret),
5575 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5576 			goto out;
5577 		}
5578 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5579 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5580 
5581 			if (!valid_tc)
5582 				valid_tc = bw_config.tc_valid_bits;
5583 			/* Always enable TC0, no matter what */
5584 			valid_tc |= 1;
5585 			dev_info(&pf->pdev->dev,
5586 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5587 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5588 			enabled_tc = valid_tc;
5589 		}
5590 
5591 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5592 		if (ret) {
5593 			dev_err(&pf->pdev->dev,
5594 				"Unable to  configure TC map %d for VSI %d\n",
5595 				enabled_tc, vsi->seid);
5596 			goto out;
5597 		}
5598 	}
5599 
5600 	/* Update Queue Pairs Mapping for currently enabled UPs */
5601 	ctxt.seid = vsi->seid;
5602 	ctxt.pf_num = vsi->back->hw.pf_id;
5603 	ctxt.vf_num = 0;
5604 	ctxt.uplink_seid = vsi->uplink_seid;
5605 	ctxt.info = vsi->info;
5606 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5607 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5608 		if (ret)
5609 			goto out;
5610 	} else {
5611 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5612 	}
5613 
5614 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5615 	 * queues changed.
5616 	 */
5617 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5618 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5619 				      vsi->num_queue_pairs);
5620 		ret = i40e_vsi_config_rss(vsi);
5621 		if (ret) {
5622 			dev_info(&vsi->back->pdev->dev,
5623 				 "Failed to reconfig rss for num_queues\n");
5624 			return ret;
5625 		}
5626 		vsi->reconfig_rss = false;
5627 	}
5628 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5629 		ctxt.info.valid_sections |=
5630 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5631 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5632 	}
5633 
5634 	/* Update the VSI after updating the VSI queue-mapping
5635 	 * information
5636 	 */
5637 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5638 	if (ret) {
5639 		dev_info(&pf->pdev->dev,
5640 			 "Update vsi tc config failed, err %s aq_err %s\n",
5641 			 i40e_stat_str(hw, ret),
5642 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5643 		goto out;
5644 	}
5645 	/* update the local VSI info with updated queue map */
5646 	i40e_vsi_update_queue_map(vsi, &ctxt);
5647 	vsi->info.valid_sections = 0;
5648 
5649 	/* Update current VSI BW information */
5650 	ret = i40e_vsi_get_bw_info(vsi);
5651 	if (ret) {
5652 		dev_info(&pf->pdev->dev,
5653 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5654 			 i40e_stat_str(hw, ret),
5655 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5656 		goto out;
5657 	}
5658 
5659 	/* Update the netdev TC setup */
5660 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5661 out:
5662 	return ret;
5663 }
5664 
5665 /**
5666  * i40e_get_link_speed - Returns link speed for the interface
5667  * @vsi: VSI to be configured
5668  *
5669  **/
5670 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5671 {
5672 	struct i40e_pf *pf = vsi->back;
5673 
5674 	switch (pf->hw.phy.link_info.link_speed) {
5675 	case I40E_LINK_SPEED_40GB:
5676 		return 40000;
5677 	case I40E_LINK_SPEED_25GB:
5678 		return 25000;
5679 	case I40E_LINK_SPEED_20GB:
5680 		return 20000;
5681 	case I40E_LINK_SPEED_10GB:
5682 		return 10000;
5683 	case I40E_LINK_SPEED_1GB:
5684 		return 1000;
5685 	default:
5686 		return -EINVAL;
5687 	}
5688 }
5689 
5690 /**
5691  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5692  * @vsi: VSI to be configured
5693  * @seid: seid of the channel/VSI
5694  * @max_tx_rate: max TX rate to be configured as BW limit
5695  *
5696  * Helper function to set BW limit for a given VSI
5697  **/
5698 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5699 {
5700 	struct i40e_pf *pf = vsi->back;
5701 	u64 credits = 0;
5702 	int speed = 0;
5703 	int ret = 0;
5704 
5705 	speed = i40e_get_link_speed(vsi);
5706 	if (max_tx_rate > speed) {
5707 		dev_err(&pf->pdev->dev,
5708 			"Invalid max tx rate %llu specified for VSI seid %d.",
5709 			max_tx_rate, seid);
5710 		return -EINVAL;
5711 	}
5712 	if (max_tx_rate && max_tx_rate < 50) {
5713 		dev_warn(&pf->pdev->dev,
5714 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5715 		max_tx_rate = 50;
5716 	}
5717 
5718 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5719 	credits = max_tx_rate;
5720 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5721 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5722 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5723 	if (ret)
5724 		dev_err(&pf->pdev->dev,
5725 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5726 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5727 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5728 	return ret;
5729 }
5730 
5731 /**
5732  * i40e_remove_queue_channels - Remove queue channels for the TCs
5733  * @vsi: VSI to be configured
5734  *
5735  * Remove queue channels for the TCs
5736  **/
5737 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5738 {
5739 	enum i40e_admin_queue_err last_aq_status;
5740 	struct i40e_cloud_filter *cfilter;
5741 	struct i40e_channel *ch, *ch_tmp;
5742 	struct i40e_pf *pf = vsi->back;
5743 	struct hlist_node *node;
5744 	int ret, i;
5745 
5746 	/* Reset rss size that was stored when reconfiguring rss for
5747 	 * channel VSIs with non-power-of-2 queue count.
5748 	 */
5749 	vsi->current_rss_size = 0;
5750 
5751 	/* perform cleanup for channels if they exist */
5752 	if (list_empty(&vsi->ch_list))
5753 		return;
5754 
5755 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5756 		struct i40e_vsi *p_vsi;
5757 
5758 		list_del(&ch->list);
5759 		p_vsi = ch->parent_vsi;
5760 		if (!p_vsi || !ch->initialized) {
5761 			kfree(ch);
5762 			continue;
5763 		}
5764 		/* Reset queue contexts */
5765 		for (i = 0; i < ch->num_queue_pairs; i++) {
5766 			struct i40e_ring *tx_ring, *rx_ring;
5767 			u16 pf_q;
5768 
5769 			pf_q = ch->base_queue + i;
5770 			tx_ring = vsi->tx_rings[pf_q];
5771 			tx_ring->ch = NULL;
5772 
5773 			rx_ring = vsi->rx_rings[pf_q];
5774 			rx_ring->ch = NULL;
5775 		}
5776 
5777 		/* Reset BW configured for this VSI via mqprio */
5778 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5779 		if (ret)
5780 			dev_info(&vsi->back->pdev->dev,
5781 				 "Failed to reset tx rate for ch->seid %u\n",
5782 				 ch->seid);
5783 
5784 		/* delete cloud filters associated with this channel */
5785 		hlist_for_each_entry_safe(cfilter, node,
5786 					  &pf->cloud_filter_list, cloud_node) {
5787 			if (cfilter->seid != ch->seid)
5788 				continue;
5789 
5790 			hash_del(&cfilter->cloud_node);
5791 			if (cfilter->dst_port)
5792 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5793 									cfilter,
5794 									false);
5795 			else
5796 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5797 								false);
5798 			last_aq_status = pf->hw.aq.asq_last_status;
5799 			if (ret)
5800 				dev_info(&pf->pdev->dev,
5801 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5802 					 i40e_stat_str(&pf->hw, ret),
5803 					 i40e_aq_str(&pf->hw, last_aq_status));
5804 			kfree(cfilter);
5805 		}
5806 
5807 		/* delete VSI from FW */
5808 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5809 					     NULL);
5810 		if (ret)
5811 			dev_err(&vsi->back->pdev->dev,
5812 				"unable to remove channel (%d) for parent VSI(%d)\n",
5813 				ch->seid, p_vsi->seid);
5814 		kfree(ch);
5815 	}
5816 	INIT_LIST_HEAD(&vsi->ch_list);
5817 }
5818 
5819 /**
5820  * i40e_get_max_queues_for_channel
5821  * @vsi: ptr to VSI to which channels are associated with
5822  *
5823  * Helper function which returns max value among the queue counts set on the
5824  * channels/TCs created.
5825  **/
5826 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5827 {
5828 	struct i40e_channel *ch, *ch_tmp;
5829 	int max = 0;
5830 
5831 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5832 		if (!ch->initialized)
5833 			continue;
5834 		if (ch->num_queue_pairs > max)
5835 			max = ch->num_queue_pairs;
5836 	}
5837 
5838 	return max;
5839 }
5840 
5841 /**
5842  * i40e_validate_num_queues - validate num_queues w.r.t channel
5843  * @pf: ptr to PF device
5844  * @num_queues: number of queues
5845  * @vsi: the parent VSI
5846  * @reconfig_rss: indicates should the RSS be reconfigured or not
5847  *
5848  * This function validates number of queues in the context of new channel
5849  * which is being established and determines if RSS should be reconfigured
5850  * or not for parent VSI.
5851  **/
5852 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5853 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5854 {
5855 	int max_ch_queues;
5856 
5857 	if (!reconfig_rss)
5858 		return -EINVAL;
5859 
5860 	*reconfig_rss = false;
5861 	if (vsi->current_rss_size) {
5862 		if (num_queues > vsi->current_rss_size) {
5863 			dev_dbg(&pf->pdev->dev,
5864 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5865 				num_queues, vsi->current_rss_size);
5866 			return -EINVAL;
5867 		} else if ((num_queues < vsi->current_rss_size) &&
5868 			   (!is_power_of_2(num_queues))) {
5869 			dev_dbg(&pf->pdev->dev,
5870 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5871 				num_queues, vsi->current_rss_size);
5872 			return -EINVAL;
5873 		}
5874 	}
5875 
5876 	if (!is_power_of_2(num_queues)) {
5877 		/* Find the max num_queues configured for channel if channel
5878 		 * exist.
5879 		 * if channel exist, then enforce 'num_queues' to be more than
5880 		 * max ever queues configured for channel.
5881 		 */
5882 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5883 		if (num_queues < max_ch_queues) {
5884 			dev_dbg(&pf->pdev->dev,
5885 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5886 				num_queues, max_ch_queues);
5887 			return -EINVAL;
5888 		}
5889 		*reconfig_rss = true;
5890 	}
5891 
5892 	return 0;
5893 }
5894 
5895 /**
5896  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5897  * @vsi: the VSI being setup
5898  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5899  *
5900  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5901  **/
5902 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5903 {
5904 	struct i40e_pf *pf = vsi->back;
5905 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5906 	struct i40e_hw *hw = &pf->hw;
5907 	int local_rss_size;
5908 	u8 *lut;
5909 	int ret;
5910 
5911 	if (!vsi->rss_size)
5912 		return -EINVAL;
5913 
5914 	if (rss_size > vsi->rss_size)
5915 		return -EINVAL;
5916 
5917 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5918 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5919 	if (!lut)
5920 		return -ENOMEM;
5921 
5922 	/* Ignoring user configured lut if there is one */
5923 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5924 
5925 	/* Use user configured hash key if there is one, otherwise
5926 	 * use default.
5927 	 */
5928 	if (vsi->rss_hkey_user)
5929 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5930 	else
5931 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5932 
5933 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5934 	if (ret) {
5935 		dev_info(&pf->pdev->dev,
5936 			 "Cannot set RSS lut, err %s aq_err %s\n",
5937 			 i40e_stat_str(hw, ret),
5938 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5939 		kfree(lut);
5940 		return ret;
5941 	}
5942 	kfree(lut);
5943 
5944 	/* Do the update w.r.t. storing rss_size */
5945 	if (!vsi->orig_rss_size)
5946 		vsi->orig_rss_size = vsi->rss_size;
5947 	vsi->current_rss_size = local_rss_size;
5948 
5949 	return ret;
5950 }
5951 
5952 /**
5953  * i40e_channel_setup_queue_map - Setup a channel queue map
5954  * @pf: ptr to PF device
5955  * @ctxt: VSI context structure
5956  * @ch: ptr to channel structure
5957  *
5958  * Setup queue map for a specific channel
5959  **/
5960 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5961 					 struct i40e_vsi_context *ctxt,
5962 					 struct i40e_channel *ch)
5963 {
5964 	u16 qcount, qmap, sections = 0;
5965 	u8 offset = 0;
5966 	int pow;
5967 
5968 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5969 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5970 
5971 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5972 	ch->num_queue_pairs = qcount;
5973 
5974 	/* find the next higher power-of-2 of num queue pairs */
5975 	pow = ilog2(qcount);
5976 	if (!is_power_of_2(qcount))
5977 		pow++;
5978 
5979 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5980 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5981 
5982 	/* Setup queue TC[0].qmap for given VSI context */
5983 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5984 
5985 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5986 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5987 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5988 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5989 }
5990 
5991 /**
5992  * i40e_add_channel - add a channel by adding VSI
5993  * @pf: ptr to PF device
5994  * @uplink_seid: underlying HW switching element (VEB) ID
5995  * @ch: ptr to channel structure
5996  *
5997  * Add a channel (VSI) using add_vsi and queue_map
5998  **/
5999 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6000 			    struct i40e_channel *ch)
6001 {
6002 	struct i40e_hw *hw = &pf->hw;
6003 	struct i40e_vsi_context ctxt;
6004 	u8 enabled_tc = 0x1; /* TC0 enabled */
6005 	int ret;
6006 
6007 	if (ch->type != I40E_VSI_VMDQ2) {
6008 		dev_info(&pf->pdev->dev,
6009 			 "add new vsi failed, ch->type %d\n", ch->type);
6010 		return -EINVAL;
6011 	}
6012 
6013 	memset(&ctxt, 0, sizeof(ctxt));
6014 	ctxt.pf_num = hw->pf_id;
6015 	ctxt.vf_num = 0;
6016 	ctxt.uplink_seid = uplink_seid;
6017 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6018 	if (ch->type == I40E_VSI_VMDQ2)
6019 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6020 
6021 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6022 		ctxt.info.valid_sections |=
6023 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6024 		ctxt.info.switch_id =
6025 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6026 	}
6027 
6028 	/* Set queue map for a given VSI context */
6029 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6030 
6031 	/* Now time to create VSI */
6032 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6033 	if (ret) {
6034 		dev_info(&pf->pdev->dev,
6035 			 "add new vsi failed, err %s aq_err %s\n",
6036 			 i40e_stat_str(&pf->hw, ret),
6037 			 i40e_aq_str(&pf->hw,
6038 				     pf->hw.aq.asq_last_status));
6039 		return -ENOENT;
6040 	}
6041 
6042 	/* Success, update channel, set enabled_tc only if the channel
6043 	 * is not a macvlan
6044 	 */
6045 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6046 	ch->seid = ctxt.seid;
6047 	ch->vsi_number = ctxt.vsi_number;
6048 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6049 
6050 	/* copy just the sections touched not the entire info
6051 	 * since not all sections are valid as returned by
6052 	 * update vsi params
6053 	 */
6054 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6055 	memcpy(&ch->info.queue_mapping,
6056 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6057 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6058 	       sizeof(ctxt.info.tc_mapping));
6059 
6060 	return 0;
6061 }
6062 
6063 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6064 				  u8 *bw_share)
6065 {
6066 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6067 	i40e_status ret;
6068 	int i;
6069 
6070 	memset(&bw_data, 0, sizeof(bw_data));
6071 	bw_data.tc_valid_bits = ch->enabled_tc;
6072 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6073 		bw_data.tc_bw_credits[i] = bw_share[i];
6074 
6075 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6076 				       &bw_data, NULL);
6077 	if (ret) {
6078 		dev_info(&vsi->back->pdev->dev,
6079 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6080 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6081 		return -EINVAL;
6082 	}
6083 
6084 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6085 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6086 
6087 	return 0;
6088 }
6089 
6090 /**
6091  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6092  * @pf: ptr to PF device
6093  * @vsi: the VSI being setup
6094  * @ch: ptr to channel structure
6095  *
6096  * Configure TX rings associated with channel (VSI) since queues are being
6097  * from parent VSI.
6098  **/
6099 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6100 				       struct i40e_vsi *vsi,
6101 				       struct i40e_channel *ch)
6102 {
6103 	i40e_status ret;
6104 	int i;
6105 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6106 
6107 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6108 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6109 		if (ch->enabled_tc & BIT(i))
6110 			bw_share[i] = 1;
6111 	}
6112 
6113 	/* configure BW for new VSI */
6114 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6115 	if (ret) {
6116 		dev_info(&vsi->back->pdev->dev,
6117 			 "Failed configuring TC map %d for channel (seid %u)\n",
6118 			 ch->enabled_tc, ch->seid);
6119 		return ret;
6120 	}
6121 
6122 	for (i = 0; i < ch->num_queue_pairs; i++) {
6123 		struct i40e_ring *tx_ring, *rx_ring;
6124 		u16 pf_q;
6125 
6126 		pf_q = ch->base_queue + i;
6127 
6128 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6129 		 * context
6130 		 */
6131 		tx_ring = vsi->tx_rings[pf_q];
6132 		tx_ring->ch = ch;
6133 
6134 		/* Get the RX ring ptr */
6135 		rx_ring = vsi->rx_rings[pf_q];
6136 		rx_ring->ch = ch;
6137 	}
6138 
6139 	return 0;
6140 }
6141 
6142 /**
6143  * i40e_setup_hw_channel - setup new channel
6144  * @pf: ptr to PF device
6145  * @vsi: the VSI being setup
6146  * @ch: ptr to channel structure
6147  * @uplink_seid: underlying HW switching element (VEB) ID
6148  * @type: type of channel to be created (VMDq2/VF)
6149  *
6150  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6151  * and configures TX rings accordingly
6152  **/
6153 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6154 					struct i40e_vsi *vsi,
6155 					struct i40e_channel *ch,
6156 					u16 uplink_seid, u8 type)
6157 {
6158 	int ret;
6159 
6160 	ch->initialized = false;
6161 	ch->base_queue = vsi->next_base_queue;
6162 	ch->type = type;
6163 
6164 	/* Proceed with creation of channel (VMDq2) VSI */
6165 	ret = i40e_add_channel(pf, uplink_seid, ch);
6166 	if (ret) {
6167 		dev_info(&pf->pdev->dev,
6168 			 "failed to add_channel using uplink_seid %u\n",
6169 			 uplink_seid);
6170 		return ret;
6171 	}
6172 
6173 	/* Mark the successful creation of channel */
6174 	ch->initialized = true;
6175 
6176 	/* Reconfigure TX queues using QTX_CTL register */
6177 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6178 	if (ret) {
6179 		dev_info(&pf->pdev->dev,
6180 			 "failed to configure TX rings for channel %u\n",
6181 			 ch->seid);
6182 		return ret;
6183 	}
6184 
6185 	/* update 'next_base_queue' */
6186 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6187 	dev_dbg(&pf->pdev->dev,
6188 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6189 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6190 		ch->num_queue_pairs,
6191 		vsi->next_base_queue);
6192 	return ret;
6193 }
6194 
6195 /**
6196  * i40e_setup_channel - setup new channel using uplink element
6197  * @pf: ptr to PF device
6198  * @vsi: pointer to the VSI to set up the channel within
6199  * @ch: ptr to channel structure
6200  *
6201  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6202  * and uplink switching element (uplink_seid)
6203  **/
6204 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6205 			       struct i40e_channel *ch)
6206 {
6207 	u8 vsi_type;
6208 	u16 seid;
6209 	int ret;
6210 
6211 	if (vsi->type == I40E_VSI_MAIN) {
6212 		vsi_type = I40E_VSI_VMDQ2;
6213 	} else {
6214 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6215 			vsi->type);
6216 		return false;
6217 	}
6218 
6219 	/* underlying switching element */
6220 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6221 
6222 	/* create channel (VSI), configure TX rings */
6223 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6224 	if (ret) {
6225 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6226 		return false;
6227 	}
6228 
6229 	return ch->initialized ? true : false;
6230 }
6231 
6232 /**
6233  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6234  * @vsi: ptr to VSI which has PF backing
6235  *
6236  * Sets up switch mode correctly if it needs to be changed and perform
6237  * what are allowed modes.
6238  **/
6239 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6240 {
6241 	u8 mode;
6242 	struct i40e_pf *pf = vsi->back;
6243 	struct i40e_hw *hw = &pf->hw;
6244 	int ret;
6245 
6246 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6247 	if (ret)
6248 		return -EINVAL;
6249 
6250 	if (hw->dev_caps.switch_mode) {
6251 		/* if switch mode is set, support mode2 (non-tunneled for
6252 		 * cloud filter) for now
6253 		 */
6254 		u32 switch_mode = hw->dev_caps.switch_mode &
6255 				  I40E_SWITCH_MODE_MASK;
6256 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6257 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6258 				return 0;
6259 			dev_err(&pf->pdev->dev,
6260 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6261 				hw->dev_caps.switch_mode);
6262 			return -EINVAL;
6263 		}
6264 	}
6265 
6266 	/* Set Bit 7 to be valid */
6267 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6268 
6269 	/* Set L4type for TCP support */
6270 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6271 
6272 	/* Set cloud filter mode */
6273 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6274 
6275 	/* Prep mode field for set_switch_config */
6276 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6277 					pf->last_sw_conf_valid_flags,
6278 					mode, NULL);
6279 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6280 		dev_err(&pf->pdev->dev,
6281 			"couldn't set switch config bits, err %s aq_err %s\n",
6282 			i40e_stat_str(hw, ret),
6283 			i40e_aq_str(hw,
6284 				    hw->aq.asq_last_status));
6285 
6286 	return ret;
6287 }
6288 
6289 /**
6290  * i40e_create_queue_channel - function to create channel
6291  * @vsi: VSI to be configured
6292  * @ch: ptr to channel (it contains channel specific params)
6293  *
6294  * This function creates channel (VSI) using num_queues specified by user,
6295  * reconfigs RSS if needed.
6296  **/
6297 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6298 			      struct i40e_channel *ch)
6299 {
6300 	struct i40e_pf *pf = vsi->back;
6301 	bool reconfig_rss;
6302 	int err;
6303 
6304 	if (!ch)
6305 		return -EINVAL;
6306 
6307 	if (!ch->num_queue_pairs) {
6308 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6309 			ch->num_queue_pairs);
6310 		return -EINVAL;
6311 	}
6312 
6313 	/* validate user requested num_queues for channel */
6314 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6315 				       &reconfig_rss);
6316 	if (err) {
6317 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6318 			 ch->num_queue_pairs);
6319 		return -EINVAL;
6320 	}
6321 
6322 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6323 	 * VSI to be added switch to VEB mode.
6324 	 */
6325 
6326 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6327 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6328 
6329 		if (vsi->type == I40E_VSI_MAIN) {
6330 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6331 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6332 			else
6333 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6334 		}
6335 		/* now onwards for main VSI, number of queues will be value
6336 		 * of TC0's queue count
6337 		 */
6338 	}
6339 
6340 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6341 	 * it should be more than num_queues
6342 	 */
6343 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6344 		dev_dbg(&pf->pdev->dev,
6345 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6346 			vsi->cnt_q_avail, ch->num_queue_pairs);
6347 		return -EINVAL;
6348 	}
6349 
6350 	/* reconfig_rss only if vsi type is MAIN_VSI */
6351 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6352 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6353 		if (err) {
6354 			dev_info(&pf->pdev->dev,
6355 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6356 				 ch->num_queue_pairs);
6357 			return -EINVAL;
6358 		}
6359 	}
6360 
6361 	if (!i40e_setup_channel(pf, vsi, ch)) {
6362 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6363 		return -EINVAL;
6364 	}
6365 
6366 	dev_info(&pf->pdev->dev,
6367 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6368 		 ch->seid, ch->num_queue_pairs);
6369 
6370 	/* configure VSI for BW limit */
6371 	if (ch->max_tx_rate) {
6372 		u64 credits = ch->max_tx_rate;
6373 
6374 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6375 			return -EINVAL;
6376 
6377 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6378 		dev_dbg(&pf->pdev->dev,
6379 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6380 			ch->max_tx_rate,
6381 			credits,
6382 			ch->seid);
6383 	}
6384 
6385 	/* in case of VF, this will be main SRIOV VSI */
6386 	ch->parent_vsi = vsi;
6387 
6388 	/* and update main_vsi's count for queue_available to use */
6389 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6390 
6391 	return 0;
6392 }
6393 
6394 /**
6395  * i40e_configure_queue_channels - Add queue channel for the given TCs
6396  * @vsi: VSI to be configured
6397  *
6398  * Configures queue channel mapping to the given TCs
6399  **/
6400 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6401 {
6402 	struct i40e_channel *ch;
6403 	u64 max_rate = 0;
6404 	int ret = 0, i;
6405 
6406 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6407 	vsi->tc_seid_map[0] = vsi->seid;
6408 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6409 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6410 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6411 			if (!ch) {
6412 				ret = -ENOMEM;
6413 				goto err_free;
6414 			}
6415 
6416 			INIT_LIST_HEAD(&ch->list);
6417 			ch->num_queue_pairs =
6418 				vsi->tc_config.tc_info[i].qcount;
6419 			ch->base_queue =
6420 				vsi->tc_config.tc_info[i].qoffset;
6421 
6422 			/* Bandwidth limit through tc interface is in bytes/s,
6423 			 * change to Mbit/s
6424 			 */
6425 			max_rate = vsi->mqprio_qopt.max_rate[i];
6426 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6427 			ch->max_tx_rate = max_rate;
6428 
6429 			list_add_tail(&ch->list, &vsi->ch_list);
6430 
6431 			ret = i40e_create_queue_channel(vsi, ch);
6432 			if (ret) {
6433 				dev_err(&vsi->back->pdev->dev,
6434 					"Failed creating queue channel with TC%d: queues %d\n",
6435 					i, ch->num_queue_pairs);
6436 				goto err_free;
6437 			}
6438 			vsi->tc_seid_map[i] = ch->seid;
6439 		}
6440 	}
6441 	return ret;
6442 
6443 err_free:
6444 	i40e_remove_queue_channels(vsi);
6445 	return ret;
6446 }
6447 
6448 /**
6449  * i40e_veb_config_tc - Configure TCs for given VEB
6450  * @veb: given VEB
6451  * @enabled_tc: TC bitmap
6452  *
6453  * Configures given TC bitmap for VEB (switching) element
6454  **/
6455 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6456 {
6457 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6458 	struct i40e_pf *pf = veb->pf;
6459 	int ret = 0;
6460 	int i;
6461 
6462 	/* No TCs or already enabled TCs just return */
6463 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6464 		return ret;
6465 
6466 	bw_data.tc_valid_bits = enabled_tc;
6467 	/* bw_data.absolute_credits is not set (relative) */
6468 
6469 	/* Enable ETS TCs with equal BW Share for now */
6470 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6471 		if (enabled_tc & BIT(i))
6472 			bw_data.tc_bw_share_credits[i] = 1;
6473 	}
6474 
6475 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6476 						   &bw_data, NULL);
6477 	if (ret) {
6478 		dev_info(&pf->pdev->dev,
6479 			 "VEB bw config failed, err %s aq_err %s\n",
6480 			 i40e_stat_str(&pf->hw, ret),
6481 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6482 		goto out;
6483 	}
6484 
6485 	/* Update the BW information */
6486 	ret = i40e_veb_get_bw_info(veb);
6487 	if (ret) {
6488 		dev_info(&pf->pdev->dev,
6489 			 "Failed getting veb bw config, err %s aq_err %s\n",
6490 			 i40e_stat_str(&pf->hw, ret),
6491 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6492 	}
6493 
6494 out:
6495 	return ret;
6496 }
6497 
6498 #ifdef CONFIG_I40E_DCB
6499 /**
6500  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6501  * @pf: PF struct
6502  *
6503  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6504  * the caller would've quiesce all the VSIs before calling
6505  * this function
6506  **/
6507 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6508 {
6509 	u8 tc_map = 0;
6510 	int ret;
6511 	u8 v;
6512 
6513 	/* Enable the TCs available on PF to all VEBs */
6514 	tc_map = i40e_pf_get_tc_map(pf);
6515 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6516 		return;
6517 
6518 	for (v = 0; v < I40E_MAX_VEB; v++) {
6519 		if (!pf->veb[v])
6520 			continue;
6521 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6522 		if (ret) {
6523 			dev_info(&pf->pdev->dev,
6524 				 "Failed configuring TC for VEB seid=%d\n",
6525 				 pf->veb[v]->seid);
6526 			/* Will try to configure as many components */
6527 		}
6528 	}
6529 
6530 	/* Update each VSI */
6531 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6532 		if (!pf->vsi[v])
6533 			continue;
6534 
6535 		/* - Enable all TCs for the LAN VSI
6536 		 * - For all others keep them at TC0 for now
6537 		 */
6538 		if (v == pf->lan_vsi)
6539 			tc_map = i40e_pf_get_tc_map(pf);
6540 		else
6541 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6542 
6543 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6544 		if (ret) {
6545 			dev_info(&pf->pdev->dev,
6546 				 "Failed configuring TC for VSI seid=%d\n",
6547 				 pf->vsi[v]->seid);
6548 			/* Will try to configure as many components */
6549 		} else {
6550 			/* Re-configure VSI vectors based on updated TC map */
6551 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6552 			if (pf->vsi[v]->netdev)
6553 				i40e_dcbnl_set_all(pf->vsi[v]);
6554 		}
6555 	}
6556 }
6557 
6558 /**
6559  * i40e_resume_port_tx - Resume port Tx
6560  * @pf: PF struct
6561  *
6562  * Resume a port's Tx and issue a PF reset in case of failure to
6563  * resume.
6564  **/
6565 static int i40e_resume_port_tx(struct i40e_pf *pf)
6566 {
6567 	struct i40e_hw *hw = &pf->hw;
6568 	int ret;
6569 
6570 	ret = i40e_aq_resume_port_tx(hw, NULL);
6571 	if (ret) {
6572 		dev_info(&pf->pdev->dev,
6573 			 "Resume Port Tx failed, err %s aq_err %s\n",
6574 			  i40e_stat_str(&pf->hw, ret),
6575 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6576 		/* Schedule PF reset to recover */
6577 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6578 		i40e_service_event_schedule(pf);
6579 	}
6580 
6581 	return ret;
6582 }
6583 
6584 /**
6585  * i40e_suspend_port_tx - Suspend port Tx
6586  * @pf: PF struct
6587  *
6588  * Suspend a port's Tx and issue a PF reset in case of failure.
6589  **/
6590 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6591 {
6592 	struct i40e_hw *hw = &pf->hw;
6593 	int ret;
6594 
6595 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6596 	if (ret) {
6597 		dev_info(&pf->pdev->dev,
6598 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6599 			 i40e_stat_str(&pf->hw, ret),
6600 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6601 		/* Schedule PF reset to recover */
6602 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6603 		i40e_service_event_schedule(pf);
6604 	}
6605 
6606 	return ret;
6607 }
6608 
6609 /**
6610  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6611  * @pf: PF being configured
6612  * @new_cfg: New DCBX configuration
6613  *
6614  * Program DCB settings into HW and reconfigure VEB/VSIs on
6615  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6616  **/
6617 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6618 				  struct i40e_dcbx_config *new_cfg)
6619 {
6620 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6621 	int ret;
6622 
6623 	/* Check if need reconfiguration */
6624 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6625 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6626 		return 0;
6627 	}
6628 
6629 	/* Config change disable all VSIs */
6630 	i40e_pf_quiesce_all_vsi(pf);
6631 
6632 	/* Copy the new config to the current config */
6633 	*old_cfg = *new_cfg;
6634 	old_cfg->etsrec = old_cfg->etscfg;
6635 	ret = i40e_set_dcb_config(&pf->hw);
6636 	if (ret) {
6637 		dev_info(&pf->pdev->dev,
6638 			 "Set DCB Config failed, err %s aq_err %s\n",
6639 			 i40e_stat_str(&pf->hw, ret),
6640 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6641 		goto out;
6642 	}
6643 
6644 	/* Changes in configuration update VEB/VSI */
6645 	i40e_dcb_reconfigure(pf);
6646 out:
6647 	/* In case of reset do not try to resume anything */
6648 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6649 		/* Re-start the VSIs if disabled */
6650 		ret = i40e_resume_port_tx(pf);
6651 		/* In case of error no point in resuming VSIs */
6652 		if (ret)
6653 			goto err;
6654 		i40e_pf_unquiesce_all_vsi(pf);
6655 	}
6656 err:
6657 	return ret;
6658 }
6659 
6660 /**
6661  * i40e_hw_dcb_config - Program new DCBX settings into HW
6662  * @pf: PF being configured
6663  * @new_cfg: New DCBX configuration
6664  *
6665  * Program DCB settings into HW and reconfigure VEB/VSIs on
6666  * given PF
6667  **/
6668 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6669 {
6670 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6671 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6672 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6673 	struct i40e_dcbx_config *old_cfg;
6674 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6675 	struct i40e_rx_pb_config pb_cfg;
6676 	struct i40e_hw *hw = &pf->hw;
6677 	u8 num_ports = hw->num_ports;
6678 	bool need_reconfig;
6679 	int ret = -EINVAL;
6680 	u8 lltc_map = 0;
6681 	u8 tc_map = 0;
6682 	u8 new_numtc;
6683 	u8 i;
6684 
6685 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6686 	/* Un-pack information to Program ETS HW via shared API
6687 	 * numtc, tcmap
6688 	 * LLTC map
6689 	 * ETS/NON-ETS arbiter mode
6690 	 * max exponent (credit refills)
6691 	 * Total number of ports
6692 	 * PFC priority bit-map
6693 	 * Priority Table
6694 	 * BW % per TC
6695 	 * Arbiter mode between UPs sharing same TC
6696 	 * TSA table (ETS or non-ETS)
6697 	 * EEE enabled or not
6698 	 * MFS TC table
6699 	 */
6700 
6701 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6702 
6703 	memset(&ets_data, 0, sizeof(ets_data));
6704 	for (i = 0; i < new_numtc; i++) {
6705 		tc_map |= BIT(i);
6706 		switch (new_cfg->etscfg.tsatable[i]) {
6707 		case I40E_IEEE_TSA_ETS:
6708 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6709 			ets_data.tc_bw_share_credits[i] =
6710 					new_cfg->etscfg.tcbwtable[i];
6711 			break;
6712 		case I40E_IEEE_TSA_STRICT:
6713 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6714 			lltc_map |= BIT(i);
6715 			ets_data.tc_bw_share_credits[i] =
6716 					I40E_DCB_STRICT_PRIO_CREDITS;
6717 			break;
6718 		default:
6719 			/* Invalid TSA type */
6720 			need_reconfig = false;
6721 			goto out;
6722 		}
6723 	}
6724 
6725 	old_cfg = &hw->local_dcbx_config;
6726 	/* Check if need reconfiguration */
6727 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6728 
6729 	/* If needed, enable/disable frame tagging, disable all VSIs
6730 	 * and suspend port tx
6731 	 */
6732 	if (need_reconfig) {
6733 		/* Enable DCB tagging only when more than one TC */
6734 		if (new_numtc > 1)
6735 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6736 		else
6737 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6738 
6739 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6740 		/* Reconfiguration needed quiesce all VSIs */
6741 		i40e_pf_quiesce_all_vsi(pf);
6742 		ret = i40e_suspend_port_tx(pf);
6743 		if (ret)
6744 			goto err;
6745 	}
6746 
6747 	/* Configure Port ETS Tx Scheduler */
6748 	ets_data.tc_valid_bits = tc_map;
6749 	ets_data.tc_strict_priority_flags = lltc_map;
6750 	ret = i40e_aq_config_switch_comp_ets
6751 		(hw, pf->mac_seid, &ets_data,
6752 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6753 	if (ret) {
6754 		dev_info(&pf->pdev->dev,
6755 			 "Modify Port ETS failed, err %s aq_err %s\n",
6756 			 i40e_stat_str(&pf->hw, ret),
6757 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6758 		goto out;
6759 	}
6760 
6761 	/* Configure Rx ETS HW */
6762 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6763 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6764 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6765 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6766 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6767 				   lltc_map);
6768 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6769 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6770 				     prio_type);
6771 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6772 			       new_cfg->etscfg.prioritytable);
6773 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6774 
6775 	/* Configure Rx Packet Buffers in HW */
6776 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6777 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6778 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6779 	}
6780 
6781 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6782 					 false, new_cfg->pfc.pfcenable,
6783 					 mfs_tc, &pb_cfg);
6784 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6785 
6786 	/* Update the local Rx Packet buffer config */
6787 	pf->pb_cfg = pb_cfg;
6788 
6789 	/* Inform the FW about changes to DCB configuration */
6790 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6791 	if (ret) {
6792 		dev_info(&pf->pdev->dev,
6793 			 "DCB Updated failed, err %s aq_err %s\n",
6794 			 i40e_stat_str(&pf->hw, ret),
6795 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6796 		goto out;
6797 	}
6798 
6799 	/* Update the port DCBx configuration */
6800 	*old_cfg = *new_cfg;
6801 
6802 	/* Changes in configuration update VEB/VSI */
6803 	i40e_dcb_reconfigure(pf);
6804 out:
6805 	/* Re-start the VSIs if disabled */
6806 	if (need_reconfig) {
6807 		ret = i40e_resume_port_tx(pf);
6808 
6809 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6810 		/* In case of error no point in resuming VSIs */
6811 		if (ret)
6812 			goto err;
6813 
6814 		/* Wait for the PF's queues to be disabled */
6815 		ret = i40e_pf_wait_queues_disabled(pf);
6816 		if (ret) {
6817 			/* Schedule PF reset to recover */
6818 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6819 			i40e_service_event_schedule(pf);
6820 			goto err;
6821 		} else {
6822 			i40e_pf_unquiesce_all_vsi(pf);
6823 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6824 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6825 		}
6826 		/* registers are set, lets apply */
6827 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6828 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6829 	}
6830 
6831 err:
6832 	return ret;
6833 }
6834 
6835 /**
6836  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6837  * @pf: PF being queried
6838  *
6839  * Set default DCB configuration in case DCB is to be done in SW.
6840  **/
6841 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6842 {
6843 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6844 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6845 	struct i40e_hw *hw = &pf->hw;
6846 	int err;
6847 
6848 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6849 		/* Update the local cached instance with TC0 ETS */
6850 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6851 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6852 		pf->tmp_cfg.etscfg.maxtcs = 0;
6853 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6854 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6855 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
6856 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
6857 		/* FW needs one App to configure HW */
6858 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
6859 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
6860 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
6861 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
6862 
6863 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
6864 	}
6865 
6866 	memset(&ets_data, 0, sizeof(ets_data));
6867 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
6868 	ets_data.tc_strict_priority_flags = 0; /* ETS */
6869 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
6870 
6871 	/* Enable ETS on the Physical port */
6872 	err = i40e_aq_config_switch_comp_ets
6873 		(hw, pf->mac_seid, &ets_data,
6874 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
6875 	if (err) {
6876 		dev_info(&pf->pdev->dev,
6877 			 "Enable Port ETS failed, err %s aq_err %s\n",
6878 			 i40e_stat_str(&pf->hw, err),
6879 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6880 		err = -ENOENT;
6881 		goto out;
6882 	}
6883 
6884 	/* Update the local cached instance with TC0 ETS */
6885 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6886 	dcb_cfg->etscfg.cbs = 0;
6887 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
6888 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6889 
6890 out:
6891 	return err;
6892 }
6893 
6894 /**
6895  * i40e_init_pf_dcb - Initialize DCB configuration
6896  * @pf: PF being configured
6897  *
6898  * Query the current DCB configuration and cache it
6899  * in the hardware structure
6900  **/
6901 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6902 {
6903 	struct i40e_hw *hw = &pf->hw;
6904 	int err;
6905 
6906 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6907 	 * Also do not enable DCBx if FW LLDP agent is disabled
6908 	 */
6909 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
6910 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
6911 		err = I40E_NOT_SUPPORTED;
6912 		goto out;
6913 	}
6914 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
6915 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
6916 		err = i40e_dcb_sw_default_config(pf);
6917 		if (err) {
6918 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
6919 			goto out;
6920 		}
6921 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
6922 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
6923 			       DCB_CAP_DCBX_VER_IEEE;
6924 		/* at init capable but disabled */
6925 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
6926 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6927 		goto out;
6928 	}
6929 	err = i40e_init_dcb(hw, true);
6930 	if (!err) {
6931 		/* Device/Function is not DCBX capable */
6932 		if ((!hw->func_caps.dcb) ||
6933 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6934 			dev_info(&pf->pdev->dev,
6935 				 "DCBX offload is not supported or is disabled for this PF.\n");
6936 		} else {
6937 			/* When status is not DISABLED then DCBX in FW */
6938 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6939 				       DCB_CAP_DCBX_VER_IEEE;
6940 
6941 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6942 			/* Enable DCB tagging only when more than one TC
6943 			 * or explicitly disable if only one TC
6944 			 */
6945 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6946 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6947 			else
6948 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6949 			dev_dbg(&pf->pdev->dev,
6950 				"DCBX offload is supported for this PF.\n");
6951 		}
6952 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6953 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6954 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6955 	} else {
6956 		dev_info(&pf->pdev->dev,
6957 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6958 			 i40e_stat_str(&pf->hw, err),
6959 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6960 	}
6961 
6962 out:
6963 	return err;
6964 }
6965 #endif /* CONFIG_I40E_DCB */
6966 
6967 /**
6968  * i40e_print_link_message - print link up or down
6969  * @vsi: the VSI for which link needs a message
6970  * @isup: true of link is up, false otherwise
6971  */
6972 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6973 {
6974 	enum i40e_aq_link_speed new_speed;
6975 	struct i40e_pf *pf = vsi->back;
6976 	char *speed = "Unknown";
6977 	char *fc = "Unknown";
6978 	char *fec = "";
6979 	char *req_fec = "";
6980 	char *an = "";
6981 
6982 	if (isup)
6983 		new_speed = pf->hw.phy.link_info.link_speed;
6984 	else
6985 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6986 
6987 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6988 		return;
6989 	vsi->current_isup = isup;
6990 	vsi->current_speed = new_speed;
6991 	if (!isup) {
6992 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6993 		return;
6994 	}
6995 
6996 	/* Warn user if link speed on NPAR enabled partition is not at
6997 	 * least 10GB
6998 	 */
6999 	if (pf->hw.func_caps.npar_enable &&
7000 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7001 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7002 		netdev_warn(vsi->netdev,
7003 			    "The partition detected link speed that is less than 10Gbps\n");
7004 
7005 	switch (pf->hw.phy.link_info.link_speed) {
7006 	case I40E_LINK_SPEED_40GB:
7007 		speed = "40 G";
7008 		break;
7009 	case I40E_LINK_SPEED_20GB:
7010 		speed = "20 G";
7011 		break;
7012 	case I40E_LINK_SPEED_25GB:
7013 		speed = "25 G";
7014 		break;
7015 	case I40E_LINK_SPEED_10GB:
7016 		speed = "10 G";
7017 		break;
7018 	case I40E_LINK_SPEED_5GB:
7019 		speed = "5 G";
7020 		break;
7021 	case I40E_LINK_SPEED_2_5GB:
7022 		speed = "2.5 G";
7023 		break;
7024 	case I40E_LINK_SPEED_1GB:
7025 		speed = "1000 M";
7026 		break;
7027 	case I40E_LINK_SPEED_100MB:
7028 		speed = "100 M";
7029 		break;
7030 	default:
7031 		break;
7032 	}
7033 
7034 	switch (pf->hw.fc.current_mode) {
7035 	case I40E_FC_FULL:
7036 		fc = "RX/TX";
7037 		break;
7038 	case I40E_FC_TX_PAUSE:
7039 		fc = "TX";
7040 		break;
7041 	case I40E_FC_RX_PAUSE:
7042 		fc = "RX";
7043 		break;
7044 	default:
7045 		fc = "None";
7046 		break;
7047 	}
7048 
7049 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7050 		req_fec = "None";
7051 		fec = "None";
7052 		an = "False";
7053 
7054 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7055 			an = "True";
7056 
7057 		if (pf->hw.phy.link_info.fec_info &
7058 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7059 			fec = "CL74 FC-FEC/BASE-R";
7060 		else if (pf->hw.phy.link_info.fec_info &
7061 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7062 			fec = "CL108 RS-FEC";
7063 
7064 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7065 		 * both RS and FC are requested
7066 		 */
7067 		if (vsi->back->hw.phy.link_info.req_fec_info &
7068 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7069 			if (vsi->back->hw.phy.link_info.req_fec_info &
7070 			    I40E_AQ_REQUEST_FEC_RS)
7071 				req_fec = "CL108 RS-FEC";
7072 			else
7073 				req_fec = "CL74 FC-FEC/BASE-R";
7074 		}
7075 		netdev_info(vsi->netdev,
7076 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7077 			    speed, req_fec, fec, an, fc);
7078 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7079 		req_fec = "None";
7080 		fec = "None";
7081 		an = "False";
7082 
7083 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7084 			an = "True";
7085 
7086 		if (pf->hw.phy.link_info.fec_info &
7087 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7088 			fec = "CL74 FC-FEC/BASE-R";
7089 
7090 		if (pf->hw.phy.link_info.req_fec_info &
7091 		    I40E_AQ_REQUEST_FEC_KR)
7092 			req_fec = "CL74 FC-FEC/BASE-R";
7093 
7094 		netdev_info(vsi->netdev,
7095 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7096 			    speed, req_fec, fec, an, fc);
7097 	} else {
7098 		netdev_info(vsi->netdev,
7099 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7100 			    speed, fc);
7101 	}
7102 
7103 }
7104 
7105 /**
7106  * i40e_up_complete - Finish the last steps of bringing up a connection
7107  * @vsi: the VSI being configured
7108  **/
7109 static int i40e_up_complete(struct i40e_vsi *vsi)
7110 {
7111 	struct i40e_pf *pf = vsi->back;
7112 	int err;
7113 
7114 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7115 		i40e_vsi_configure_msix(vsi);
7116 	else
7117 		i40e_configure_msi_and_legacy(vsi);
7118 
7119 	/* start rings */
7120 	err = i40e_vsi_start_rings(vsi);
7121 	if (err)
7122 		return err;
7123 
7124 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7125 	i40e_napi_enable_all(vsi);
7126 	i40e_vsi_enable_irq(vsi);
7127 
7128 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7129 	    (vsi->netdev)) {
7130 		i40e_print_link_message(vsi, true);
7131 		netif_tx_start_all_queues(vsi->netdev);
7132 		netif_carrier_on(vsi->netdev);
7133 	}
7134 
7135 	/* replay FDIR SB filters */
7136 	if (vsi->type == I40E_VSI_FDIR) {
7137 		/* reset fd counters */
7138 		pf->fd_add_err = 0;
7139 		pf->fd_atr_cnt = 0;
7140 		i40e_fdir_filter_restore(vsi);
7141 	}
7142 
7143 	/* On the next run of the service_task, notify any clients of the new
7144 	 * opened netdev
7145 	 */
7146 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7147 	i40e_service_event_schedule(pf);
7148 
7149 	return 0;
7150 }
7151 
7152 /**
7153  * i40e_vsi_reinit_locked - Reset the VSI
7154  * @vsi: the VSI being configured
7155  *
7156  * Rebuild the ring structs after some configuration
7157  * has changed, e.g. MTU size.
7158  **/
7159 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7160 {
7161 	struct i40e_pf *pf = vsi->back;
7162 
7163 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7164 		usleep_range(1000, 2000);
7165 	i40e_down(vsi);
7166 
7167 	i40e_up(vsi);
7168 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7169 }
7170 
7171 /**
7172  * i40e_force_link_state - Force the link status
7173  * @pf: board private structure
7174  * @is_up: whether the link state should be forced up or down
7175  **/
7176 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7177 {
7178 	struct i40e_aq_get_phy_abilities_resp abilities;
7179 	struct i40e_aq_set_phy_config config = {0};
7180 	bool non_zero_phy_type = is_up;
7181 	struct i40e_hw *hw = &pf->hw;
7182 	i40e_status err;
7183 	u64 mask;
7184 	u8 speed;
7185 
7186 	/* Card might've been put in an unstable state by other drivers
7187 	 * and applications, which causes incorrect speed values being
7188 	 * set on startup. In order to clear speed registers, we call
7189 	 * get_phy_capabilities twice, once to get initial state of
7190 	 * available speeds, and once to get current PHY config.
7191 	 */
7192 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7193 					   NULL);
7194 	if (err) {
7195 		dev_err(&pf->pdev->dev,
7196 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7197 			i40e_stat_str(hw, err),
7198 			i40e_aq_str(hw, hw->aq.asq_last_status));
7199 		return err;
7200 	}
7201 	speed = abilities.link_speed;
7202 
7203 	/* Get the current phy config */
7204 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7205 					   NULL);
7206 	if (err) {
7207 		dev_err(&pf->pdev->dev,
7208 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7209 			i40e_stat_str(hw, err),
7210 			i40e_aq_str(hw, hw->aq.asq_last_status));
7211 		return err;
7212 	}
7213 
7214 	/* If link needs to go up, but was not forced to go down,
7215 	 * and its speed values are OK, no need for a flap
7216 	 * if non_zero_phy_type was set, still need to force up
7217 	 */
7218 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7219 		non_zero_phy_type = true;
7220 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7221 		return I40E_SUCCESS;
7222 
7223 	/* To force link we need to set bits for all supported PHY types,
7224 	 * but there are now more than 32, so we need to split the bitmap
7225 	 * across two fields.
7226 	 */
7227 	mask = I40E_PHY_TYPES_BITMASK;
7228 	config.phy_type =
7229 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7230 	config.phy_type_ext =
7231 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7232 	/* Copy the old settings, except of phy_type */
7233 	config.abilities = abilities.abilities;
7234 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7235 		if (is_up)
7236 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7237 		else
7238 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7239 	}
7240 	if (abilities.link_speed != 0)
7241 		config.link_speed = abilities.link_speed;
7242 	else
7243 		config.link_speed = speed;
7244 	config.eee_capability = abilities.eee_capability;
7245 	config.eeer = abilities.eeer_val;
7246 	config.low_power_ctrl = abilities.d3_lpan;
7247 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7248 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7249 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7250 
7251 	if (err) {
7252 		dev_err(&pf->pdev->dev,
7253 			"set phy config ret =  %s last_status =  %s\n",
7254 			i40e_stat_str(&pf->hw, err),
7255 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7256 		return err;
7257 	}
7258 
7259 	/* Update the link info */
7260 	err = i40e_update_link_info(hw);
7261 	if (err) {
7262 		/* Wait a little bit (on 40G cards it sometimes takes a really
7263 		 * long time for link to come back from the atomic reset)
7264 		 * and try once more
7265 		 */
7266 		msleep(1000);
7267 		i40e_update_link_info(hw);
7268 	}
7269 
7270 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7271 
7272 	return I40E_SUCCESS;
7273 }
7274 
7275 /**
7276  * i40e_up - Bring the connection back up after being down
7277  * @vsi: the VSI being configured
7278  **/
7279 int i40e_up(struct i40e_vsi *vsi)
7280 {
7281 	int err;
7282 
7283 	if (vsi->type == I40E_VSI_MAIN &&
7284 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7285 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7286 		i40e_force_link_state(vsi->back, true);
7287 
7288 	err = i40e_vsi_configure(vsi);
7289 	if (!err)
7290 		err = i40e_up_complete(vsi);
7291 
7292 	return err;
7293 }
7294 
7295 /**
7296  * i40e_down - Shutdown the connection processing
7297  * @vsi: the VSI being stopped
7298  **/
7299 void i40e_down(struct i40e_vsi *vsi)
7300 {
7301 	int i;
7302 
7303 	/* It is assumed that the caller of this function
7304 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7305 	 */
7306 	if (vsi->netdev) {
7307 		netif_carrier_off(vsi->netdev);
7308 		netif_tx_disable(vsi->netdev);
7309 	}
7310 	i40e_vsi_disable_irq(vsi);
7311 	i40e_vsi_stop_rings(vsi);
7312 	if (vsi->type == I40E_VSI_MAIN &&
7313 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7314 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7315 		i40e_force_link_state(vsi->back, false);
7316 	i40e_napi_disable_all(vsi);
7317 
7318 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7319 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7320 		if (i40e_enabled_xdp_vsi(vsi)) {
7321 			/* Make sure that in-progress ndo_xdp_xmit and
7322 			 * ndo_xsk_wakeup calls are completed.
7323 			 */
7324 			synchronize_rcu();
7325 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7326 		}
7327 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7328 	}
7329 
7330 }
7331 
7332 /**
7333  * i40e_validate_mqprio_qopt- validate queue mapping info
7334  * @vsi: the VSI being configured
7335  * @mqprio_qopt: queue parametrs
7336  **/
7337 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7338 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7339 {
7340 	u64 sum_max_rate = 0;
7341 	u64 max_rate = 0;
7342 	int i;
7343 
7344 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7345 	    mqprio_qopt->qopt.num_tc < 1 ||
7346 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7347 		return -EINVAL;
7348 	for (i = 0; ; i++) {
7349 		if (!mqprio_qopt->qopt.count[i])
7350 			return -EINVAL;
7351 		if (mqprio_qopt->min_rate[i]) {
7352 			dev_err(&vsi->back->pdev->dev,
7353 				"Invalid min tx rate (greater than 0) specified\n");
7354 			return -EINVAL;
7355 		}
7356 		max_rate = mqprio_qopt->max_rate[i];
7357 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7358 		sum_max_rate += max_rate;
7359 
7360 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7361 			break;
7362 		if (mqprio_qopt->qopt.offset[i + 1] !=
7363 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7364 			return -EINVAL;
7365 	}
7366 	if (vsi->num_queue_pairs <
7367 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7368 		dev_err(&vsi->back->pdev->dev,
7369 			"Failed to create traffic channel, insufficient number of queues.\n");
7370 		return -EINVAL;
7371 	}
7372 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7373 		dev_err(&vsi->back->pdev->dev,
7374 			"Invalid max tx rate specified\n");
7375 		return -EINVAL;
7376 	}
7377 	return 0;
7378 }
7379 
7380 /**
7381  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7382  * @vsi: the VSI being configured
7383  **/
7384 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7385 {
7386 	u16 qcount;
7387 	int i;
7388 
7389 	/* Only TC0 is enabled */
7390 	vsi->tc_config.numtc = 1;
7391 	vsi->tc_config.enabled_tc = 1;
7392 	qcount = min_t(int, vsi->alloc_queue_pairs,
7393 		       i40e_pf_get_max_q_per_tc(vsi->back));
7394 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7395 		/* For the TC that is not enabled set the offset to default
7396 		 * queue and allocate one queue for the given TC.
7397 		 */
7398 		vsi->tc_config.tc_info[i].qoffset = 0;
7399 		if (i == 0)
7400 			vsi->tc_config.tc_info[i].qcount = qcount;
7401 		else
7402 			vsi->tc_config.tc_info[i].qcount = 1;
7403 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7404 	}
7405 }
7406 
7407 /**
7408  * i40e_del_macvlan_filter
7409  * @hw: pointer to the HW structure
7410  * @seid: seid of the channel VSI
7411  * @macaddr: the mac address to apply as a filter
7412  * @aq_err: store the admin Q error
7413  *
7414  * This function deletes a mac filter on the channel VSI which serves as the
7415  * macvlan. Returns 0 on success.
7416  **/
7417 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7418 					   const u8 *macaddr, int *aq_err)
7419 {
7420 	struct i40e_aqc_remove_macvlan_element_data element;
7421 	i40e_status status;
7422 
7423 	memset(&element, 0, sizeof(element));
7424 	ether_addr_copy(element.mac_addr, macaddr);
7425 	element.vlan_tag = 0;
7426 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7427 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7428 	*aq_err = hw->aq.asq_last_status;
7429 
7430 	return status;
7431 }
7432 
7433 /**
7434  * i40e_add_macvlan_filter
7435  * @hw: pointer to the HW structure
7436  * @seid: seid of the channel VSI
7437  * @macaddr: the mac address to apply as a filter
7438  * @aq_err: store the admin Q error
7439  *
7440  * This function adds a mac filter on the channel VSI which serves as the
7441  * macvlan. Returns 0 on success.
7442  **/
7443 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7444 					   const u8 *macaddr, int *aq_err)
7445 {
7446 	struct i40e_aqc_add_macvlan_element_data element;
7447 	i40e_status status;
7448 	u16 cmd_flags = 0;
7449 
7450 	ether_addr_copy(element.mac_addr, macaddr);
7451 	element.vlan_tag = 0;
7452 	element.queue_number = 0;
7453 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7454 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7455 	element.flags = cpu_to_le16(cmd_flags);
7456 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7457 	*aq_err = hw->aq.asq_last_status;
7458 
7459 	return status;
7460 }
7461 
7462 /**
7463  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7464  * @vsi: the VSI we want to access
7465  * @ch: the channel we want to access
7466  */
7467 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7468 {
7469 	struct i40e_ring *tx_ring, *rx_ring;
7470 	u16 pf_q;
7471 	int i;
7472 
7473 	for (i = 0; i < ch->num_queue_pairs; i++) {
7474 		pf_q = ch->base_queue + i;
7475 		tx_ring = vsi->tx_rings[pf_q];
7476 		tx_ring->ch = NULL;
7477 		rx_ring = vsi->rx_rings[pf_q];
7478 		rx_ring->ch = NULL;
7479 	}
7480 }
7481 
7482 /**
7483  * i40e_free_macvlan_channels
7484  * @vsi: the VSI we want to access
7485  *
7486  * This function frees the Qs of the channel VSI from
7487  * the stack and also deletes the channel VSIs which
7488  * serve as macvlans.
7489  */
7490 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7491 {
7492 	struct i40e_channel *ch, *ch_tmp;
7493 	int ret;
7494 
7495 	if (list_empty(&vsi->macvlan_list))
7496 		return;
7497 
7498 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7499 		struct i40e_vsi *parent_vsi;
7500 
7501 		if (i40e_is_channel_macvlan(ch)) {
7502 			i40e_reset_ch_rings(vsi, ch);
7503 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7504 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7505 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7506 			kfree(ch->fwd);
7507 			ch->fwd = NULL;
7508 		}
7509 
7510 		list_del(&ch->list);
7511 		parent_vsi = ch->parent_vsi;
7512 		if (!parent_vsi || !ch->initialized) {
7513 			kfree(ch);
7514 			continue;
7515 		}
7516 
7517 		/* remove the VSI */
7518 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7519 					     NULL);
7520 		if (ret)
7521 			dev_err(&vsi->back->pdev->dev,
7522 				"unable to remove channel (%d) for parent VSI(%d)\n",
7523 				ch->seid, parent_vsi->seid);
7524 		kfree(ch);
7525 	}
7526 	vsi->macvlan_cnt = 0;
7527 }
7528 
7529 /**
7530  * i40e_fwd_ring_up - bring the macvlan device up
7531  * @vsi: the VSI we want to access
7532  * @vdev: macvlan netdevice
7533  * @fwd: the private fwd structure
7534  */
7535 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7536 			    struct i40e_fwd_adapter *fwd)
7537 {
7538 	int ret = 0, num_tc = 1,  i, aq_err;
7539 	struct i40e_channel *ch, *ch_tmp;
7540 	struct i40e_pf *pf = vsi->back;
7541 	struct i40e_hw *hw = &pf->hw;
7542 
7543 	if (list_empty(&vsi->macvlan_list))
7544 		return -EINVAL;
7545 
7546 	/* Go through the list and find an available channel */
7547 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7548 		if (!i40e_is_channel_macvlan(ch)) {
7549 			ch->fwd = fwd;
7550 			/* record configuration for macvlan interface in vdev */
7551 			for (i = 0; i < num_tc; i++)
7552 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7553 							     i,
7554 							     ch->num_queue_pairs,
7555 							     ch->base_queue);
7556 			for (i = 0; i < ch->num_queue_pairs; i++) {
7557 				struct i40e_ring *tx_ring, *rx_ring;
7558 				u16 pf_q;
7559 
7560 				pf_q = ch->base_queue + i;
7561 
7562 				/* Get to TX ring ptr */
7563 				tx_ring = vsi->tx_rings[pf_q];
7564 				tx_ring->ch = ch;
7565 
7566 				/* Get the RX ring ptr */
7567 				rx_ring = vsi->rx_rings[pf_q];
7568 				rx_ring->ch = ch;
7569 			}
7570 			break;
7571 		}
7572 	}
7573 
7574 	/* Guarantee all rings are updated before we update the
7575 	 * MAC address filter.
7576 	 */
7577 	wmb();
7578 
7579 	/* Add a mac filter */
7580 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7581 	if (ret) {
7582 		/* if we cannot add the MAC rule then disable the offload */
7583 		macvlan_release_l2fw_offload(vdev);
7584 		for (i = 0; i < ch->num_queue_pairs; i++) {
7585 			struct i40e_ring *rx_ring;
7586 			u16 pf_q;
7587 
7588 			pf_q = ch->base_queue + i;
7589 			rx_ring = vsi->rx_rings[pf_q];
7590 			rx_ring->netdev = NULL;
7591 		}
7592 		dev_info(&pf->pdev->dev,
7593 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7594 			  i40e_stat_str(hw, ret),
7595 			  i40e_aq_str(hw, aq_err));
7596 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7597 	}
7598 
7599 	return ret;
7600 }
7601 
7602 /**
7603  * i40e_setup_macvlans - create the channels which will be macvlans
7604  * @vsi: the VSI we want to access
7605  * @macvlan_cnt: no. of macvlans to be setup
7606  * @qcnt: no. of Qs per macvlan
7607  * @vdev: macvlan netdevice
7608  */
7609 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7610 			       struct net_device *vdev)
7611 {
7612 	struct i40e_pf *pf = vsi->back;
7613 	struct i40e_hw *hw = &pf->hw;
7614 	struct i40e_vsi_context ctxt;
7615 	u16 sections, qmap, num_qps;
7616 	struct i40e_channel *ch;
7617 	int i, pow, ret = 0;
7618 	u8 offset = 0;
7619 
7620 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7621 		return -EINVAL;
7622 
7623 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7624 
7625 	/* find the next higher power-of-2 of num queue pairs */
7626 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7627 
7628 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7629 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7630 
7631 	/* Setup context bits for the main VSI */
7632 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7633 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7634 	memset(&ctxt, 0, sizeof(ctxt));
7635 	ctxt.seid = vsi->seid;
7636 	ctxt.pf_num = vsi->back->hw.pf_id;
7637 	ctxt.vf_num = 0;
7638 	ctxt.uplink_seid = vsi->uplink_seid;
7639 	ctxt.info = vsi->info;
7640 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7641 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7642 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7643 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7644 
7645 	/* Reconfigure RSS for main VSI with new max queue count */
7646 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7647 	ret = i40e_vsi_config_rss(vsi);
7648 	if (ret) {
7649 		dev_info(&pf->pdev->dev,
7650 			 "Failed to reconfig RSS for num_queues (%u)\n",
7651 			 vsi->rss_size);
7652 		return ret;
7653 	}
7654 	vsi->reconfig_rss = true;
7655 	dev_dbg(&vsi->back->pdev->dev,
7656 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7657 	vsi->next_base_queue = num_qps;
7658 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7659 
7660 	/* Update the VSI after updating the VSI queue-mapping
7661 	 * information
7662 	 */
7663 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7664 	if (ret) {
7665 		dev_info(&pf->pdev->dev,
7666 			 "Update vsi tc config failed, err %s aq_err %s\n",
7667 			 i40e_stat_str(hw, ret),
7668 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7669 		return ret;
7670 	}
7671 	/* update the local VSI info with updated queue map */
7672 	i40e_vsi_update_queue_map(vsi, &ctxt);
7673 	vsi->info.valid_sections = 0;
7674 
7675 	/* Create channels for macvlans */
7676 	INIT_LIST_HEAD(&vsi->macvlan_list);
7677 	for (i = 0; i < macvlan_cnt; i++) {
7678 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7679 		if (!ch) {
7680 			ret = -ENOMEM;
7681 			goto err_free;
7682 		}
7683 		INIT_LIST_HEAD(&ch->list);
7684 		ch->num_queue_pairs = qcnt;
7685 		if (!i40e_setup_channel(pf, vsi, ch)) {
7686 			ret = -EINVAL;
7687 			kfree(ch);
7688 			goto err_free;
7689 		}
7690 		ch->parent_vsi = vsi;
7691 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7692 		vsi->macvlan_cnt++;
7693 		list_add_tail(&ch->list, &vsi->macvlan_list);
7694 	}
7695 
7696 	return ret;
7697 
7698 err_free:
7699 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7700 	i40e_free_macvlan_channels(vsi);
7701 
7702 	return ret;
7703 }
7704 
7705 /**
7706  * i40e_fwd_add - configure macvlans
7707  * @netdev: net device to configure
7708  * @vdev: macvlan netdevice
7709  **/
7710 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7711 {
7712 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7713 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7714 	struct i40e_vsi *vsi = np->vsi;
7715 	struct i40e_pf *pf = vsi->back;
7716 	struct i40e_fwd_adapter *fwd;
7717 	int avail_macvlan, ret;
7718 
7719 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7720 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7721 		return ERR_PTR(-EINVAL);
7722 	}
7723 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7724 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7725 		return ERR_PTR(-EINVAL);
7726 	}
7727 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7728 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7729 		return ERR_PTR(-EINVAL);
7730 	}
7731 
7732 	/* The macvlan device has to be a single Q device so that the
7733 	 * tc_to_txq field can be reused to pick the tx queue.
7734 	 */
7735 	if (netif_is_multiqueue(vdev))
7736 		return ERR_PTR(-ERANGE);
7737 
7738 	if (!vsi->macvlan_cnt) {
7739 		/* reserve bit 0 for the pf device */
7740 		set_bit(0, vsi->fwd_bitmask);
7741 
7742 		/* Try to reserve as many queues as possible for macvlans. First
7743 		 * reserve 3/4th of max vectors, then half, then quarter and
7744 		 * calculate Qs per macvlan as you go
7745 		 */
7746 		vectors = pf->num_lan_msix;
7747 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7748 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7749 			q_per_macvlan = 4;
7750 			macvlan_cnt = (vectors - 32) / 4;
7751 		} else if (vectors <= 64 && vectors > 32) {
7752 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7753 			q_per_macvlan = 2;
7754 			macvlan_cnt = (vectors - 16) / 2;
7755 		} else if (vectors <= 32 && vectors > 16) {
7756 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7757 			q_per_macvlan = 1;
7758 			macvlan_cnt = vectors - 16;
7759 		} else if (vectors <= 16 && vectors > 8) {
7760 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7761 			q_per_macvlan = 1;
7762 			macvlan_cnt = vectors - 8;
7763 		} else {
7764 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7765 			q_per_macvlan = 1;
7766 			macvlan_cnt = vectors - 1;
7767 		}
7768 
7769 		if (macvlan_cnt == 0)
7770 			return ERR_PTR(-EBUSY);
7771 
7772 		/* Quiesce VSI queues */
7773 		i40e_quiesce_vsi(vsi);
7774 
7775 		/* sets up the macvlans but does not "enable" them */
7776 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7777 					  vdev);
7778 		if (ret)
7779 			return ERR_PTR(ret);
7780 
7781 		/* Unquiesce VSI */
7782 		i40e_unquiesce_vsi(vsi);
7783 	}
7784 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7785 					    vsi->macvlan_cnt);
7786 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7787 		return ERR_PTR(-EBUSY);
7788 
7789 	/* create the fwd struct */
7790 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7791 	if (!fwd)
7792 		return ERR_PTR(-ENOMEM);
7793 
7794 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7795 	fwd->bit_no = avail_macvlan;
7796 	netdev_set_sb_channel(vdev, avail_macvlan);
7797 	fwd->netdev = vdev;
7798 
7799 	if (!netif_running(netdev))
7800 		return fwd;
7801 
7802 	/* Set fwd ring up */
7803 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7804 	if (ret) {
7805 		/* unbind the queues and drop the subordinate channel config */
7806 		netdev_unbind_sb_channel(netdev, vdev);
7807 		netdev_set_sb_channel(vdev, 0);
7808 
7809 		kfree(fwd);
7810 		return ERR_PTR(-EINVAL);
7811 	}
7812 
7813 	return fwd;
7814 }
7815 
7816 /**
7817  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7818  * @vsi: the VSI we want to access
7819  */
7820 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7821 {
7822 	struct i40e_channel *ch, *ch_tmp;
7823 	struct i40e_pf *pf = vsi->back;
7824 	struct i40e_hw *hw = &pf->hw;
7825 	int aq_err, ret = 0;
7826 
7827 	if (list_empty(&vsi->macvlan_list))
7828 		return;
7829 
7830 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7831 		if (i40e_is_channel_macvlan(ch)) {
7832 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7833 						      i40e_channel_mac(ch),
7834 						      &aq_err);
7835 			if (!ret) {
7836 				/* Reset queue contexts */
7837 				i40e_reset_ch_rings(vsi, ch);
7838 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7839 				netdev_unbind_sb_channel(vsi->netdev,
7840 							 ch->fwd->netdev);
7841 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7842 				kfree(ch->fwd);
7843 				ch->fwd = NULL;
7844 			}
7845 		}
7846 	}
7847 }
7848 
7849 /**
7850  * i40e_fwd_del - delete macvlan interfaces
7851  * @netdev: net device to configure
7852  * @vdev: macvlan netdevice
7853  */
7854 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7855 {
7856 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7857 	struct i40e_fwd_adapter *fwd = vdev;
7858 	struct i40e_channel *ch, *ch_tmp;
7859 	struct i40e_vsi *vsi = np->vsi;
7860 	struct i40e_pf *pf = vsi->back;
7861 	struct i40e_hw *hw = &pf->hw;
7862 	int aq_err, ret = 0;
7863 
7864 	/* Find the channel associated with the macvlan and del mac filter */
7865 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7866 		if (i40e_is_channel_macvlan(ch) &&
7867 		    ether_addr_equal(i40e_channel_mac(ch),
7868 				     fwd->netdev->dev_addr)) {
7869 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7870 						      i40e_channel_mac(ch),
7871 						      &aq_err);
7872 			if (!ret) {
7873 				/* Reset queue contexts */
7874 				i40e_reset_ch_rings(vsi, ch);
7875 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7876 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7877 				netdev_set_sb_channel(fwd->netdev, 0);
7878 				kfree(ch->fwd);
7879 				ch->fwd = NULL;
7880 			} else {
7881 				dev_info(&pf->pdev->dev,
7882 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7883 					  i40e_stat_str(hw, ret),
7884 					  i40e_aq_str(hw, aq_err));
7885 			}
7886 			break;
7887 		}
7888 	}
7889 }
7890 
7891 /**
7892  * i40e_setup_tc - configure multiple traffic classes
7893  * @netdev: net device to configure
7894  * @type_data: tc offload data
7895  **/
7896 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7897 {
7898 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7899 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7900 	struct i40e_vsi *vsi = np->vsi;
7901 	struct i40e_pf *pf = vsi->back;
7902 	u8 enabled_tc = 0, num_tc, hw;
7903 	bool need_reset = false;
7904 	int old_queue_pairs;
7905 	int ret = -EINVAL;
7906 	u16 mode;
7907 	int i;
7908 
7909 	old_queue_pairs = vsi->num_queue_pairs;
7910 	num_tc = mqprio_qopt->qopt.num_tc;
7911 	hw = mqprio_qopt->qopt.hw;
7912 	mode = mqprio_qopt->mode;
7913 	if (!hw) {
7914 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7915 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7916 		goto config_tc;
7917 	}
7918 
7919 	/* Check if MFP enabled */
7920 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7921 		netdev_info(netdev,
7922 			    "Configuring TC not supported in MFP mode\n");
7923 		return ret;
7924 	}
7925 	switch (mode) {
7926 	case TC_MQPRIO_MODE_DCB:
7927 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7928 
7929 		/* Check if DCB enabled to continue */
7930 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7931 			netdev_info(netdev,
7932 				    "DCB is not enabled for adapter\n");
7933 			return ret;
7934 		}
7935 
7936 		/* Check whether tc count is within enabled limit */
7937 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7938 			netdev_info(netdev,
7939 				    "TC count greater than enabled on link for adapter\n");
7940 			return ret;
7941 		}
7942 		break;
7943 	case TC_MQPRIO_MODE_CHANNEL:
7944 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7945 			netdev_info(netdev,
7946 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7947 			return ret;
7948 		}
7949 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7950 			return ret;
7951 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7952 		if (ret)
7953 			return ret;
7954 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7955 		       sizeof(*mqprio_qopt));
7956 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7957 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7958 		break;
7959 	default:
7960 		return -EINVAL;
7961 	}
7962 
7963 config_tc:
7964 	/* Generate TC map for number of tc requested */
7965 	for (i = 0; i < num_tc; i++)
7966 		enabled_tc |= BIT(i);
7967 
7968 	/* Requesting same TC configuration as already enabled */
7969 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7970 	    mode != TC_MQPRIO_MODE_CHANNEL)
7971 		return 0;
7972 
7973 	/* Quiesce VSI queues */
7974 	i40e_quiesce_vsi(vsi);
7975 
7976 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7977 		i40e_remove_queue_channels(vsi);
7978 
7979 	/* Configure VSI for enabled TCs */
7980 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7981 	if (ret) {
7982 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7983 			    vsi->seid);
7984 		need_reset = true;
7985 		goto exit;
7986 	} else if (enabled_tc &&
7987 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
7988 		netdev_info(netdev,
7989 			    "Failed to create channel. Override queues (%u) not power of 2\n",
7990 			    vsi->tc_config.tc_info[0].qcount);
7991 		ret = -EINVAL;
7992 		need_reset = true;
7993 		goto exit;
7994 	}
7995 
7996 	dev_info(&vsi->back->pdev->dev,
7997 		 "Setup channel (id:%u) utilizing num_queues %d\n",
7998 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7999 
8000 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
8001 		if (vsi->mqprio_qopt.max_rate[0]) {
8002 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8003 
8004 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8005 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8006 			if (!ret) {
8007 				u64 credits = max_tx_rate;
8008 
8009 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8010 				dev_dbg(&vsi->back->pdev->dev,
8011 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8012 					max_tx_rate,
8013 					credits,
8014 					vsi->seid);
8015 			} else {
8016 				need_reset = true;
8017 				goto exit;
8018 			}
8019 		}
8020 		ret = i40e_configure_queue_channels(vsi);
8021 		if (ret) {
8022 			vsi->num_queue_pairs = old_queue_pairs;
8023 			netdev_info(netdev,
8024 				    "Failed configuring queue channels\n");
8025 			need_reset = true;
8026 			goto exit;
8027 		}
8028 	}
8029 
8030 exit:
8031 	/* Reset the configuration data to defaults, only TC0 is enabled */
8032 	if (need_reset) {
8033 		i40e_vsi_set_default_tc_config(vsi);
8034 		need_reset = false;
8035 	}
8036 
8037 	/* Unquiesce VSI */
8038 	i40e_unquiesce_vsi(vsi);
8039 	return ret;
8040 }
8041 
8042 /**
8043  * i40e_set_cld_element - sets cloud filter element data
8044  * @filter: cloud filter rule
8045  * @cld: ptr to cloud filter element data
8046  *
8047  * This is helper function to copy data into cloud filter element
8048  **/
8049 static inline void
8050 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8051 		     struct i40e_aqc_cloud_filters_element_data *cld)
8052 {
8053 	u32 ipa;
8054 	int i;
8055 
8056 	memset(cld, 0, sizeof(*cld));
8057 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8058 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8059 
8060 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8061 		return;
8062 
8063 	if (filter->n_proto == ETH_P_IPV6) {
8064 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8065 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8066 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8067 
8068 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8069 		}
8070 	} else {
8071 		ipa = be32_to_cpu(filter->dst_ipv4);
8072 
8073 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8074 	}
8075 
8076 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8077 
8078 	/* tenant_id is not supported by FW now, once the support is enabled
8079 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8080 	 */
8081 	if (filter->tenant_id)
8082 		return;
8083 }
8084 
8085 /**
8086  * i40e_add_del_cloud_filter - Add/del cloud filter
8087  * @vsi: pointer to VSI
8088  * @filter: cloud filter rule
8089  * @add: if true, add, if false, delete
8090  *
8091  * Add or delete a cloud filter for a specific flow spec.
8092  * Returns 0 if the filter were successfully added.
8093  **/
8094 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8095 			      struct i40e_cloud_filter *filter, bool add)
8096 {
8097 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8098 	struct i40e_pf *pf = vsi->back;
8099 	int ret;
8100 	static const u16 flag_table[128] = {
8101 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8102 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8103 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8104 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8105 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8106 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8107 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8108 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8109 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8110 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8111 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8112 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8113 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8114 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8115 	};
8116 
8117 	if (filter->flags >= ARRAY_SIZE(flag_table))
8118 		return I40E_ERR_CONFIG;
8119 
8120 	memset(&cld_filter, 0, sizeof(cld_filter));
8121 
8122 	/* copy element needed to add cloud filter from filter */
8123 	i40e_set_cld_element(filter, &cld_filter);
8124 
8125 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8126 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8127 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8128 
8129 	if (filter->n_proto == ETH_P_IPV6)
8130 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8131 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8132 	else
8133 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8134 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8135 
8136 	if (add)
8137 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8138 						&cld_filter, 1);
8139 	else
8140 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8141 						&cld_filter, 1);
8142 	if (ret)
8143 		dev_dbg(&pf->pdev->dev,
8144 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8145 			add ? "add" : "delete", filter->dst_port, ret,
8146 			pf->hw.aq.asq_last_status);
8147 	else
8148 		dev_info(&pf->pdev->dev,
8149 			 "%s cloud filter for VSI: %d\n",
8150 			 add ? "Added" : "Deleted", filter->seid);
8151 	return ret;
8152 }
8153 
8154 /**
8155  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8156  * @vsi: pointer to VSI
8157  * @filter: cloud filter rule
8158  * @add: if true, add, if false, delete
8159  *
8160  * Add or delete a cloud filter for a specific flow spec using big buffer.
8161  * Returns 0 if the filter were successfully added.
8162  **/
8163 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8164 				      struct i40e_cloud_filter *filter,
8165 				      bool add)
8166 {
8167 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8168 	struct i40e_pf *pf = vsi->back;
8169 	int ret;
8170 
8171 	/* Both (src/dst) valid mac_addr are not supported */
8172 	if ((is_valid_ether_addr(filter->dst_mac) &&
8173 	     is_valid_ether_addr(filter->src_mac)) ||
8174 	    (is_multicast_ether_addr(filter->dst_mac) &&
8175 	     is_multicast_ether_addr(filter->src_mac)))
8176 		return -EOPNOTSUPP;
8177 
8178 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8179 	 * ports are not supported via big buffer now.
8180 	 */
8181 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8182 		return -EOPNOTSUPP;
8183 
8184 	/* adding filter using src_port/src_ip is not supported at this stage */
8185 	if (filter->src_port ||
8186 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8187 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8188 		return -EOPNOTSUPP;
8189 
8190 	memset(&cld_filter, 0, sizeof(cld_filter));
8191 
8192 	/* copy element needed to add cloud filter from filter */
8193 	i40e_set_cld_element(filter, &cld_filter.element);
8194 
8195 	if (is_valid_ether_addr(filter->dst_mac) ||
8196 	    is_valid_ether_addr(filter->src_mac) ||
8197 	    is_multicast_ether_addr(filter->dst_mac) ||
8198 	    is_multicast_ether_addr(filter->src_mac)) {
8199 		/* MAC + IP : unsupported mode */
8200 		if (filter->dst_ipv4)
8201 			return -EOPNOTSUPP;
8202 
8203 		/* since we validated that L4 port must be valid before
8204 		 * we get here, start with respective "flags" value
8205 		 * and update if vlan is present or not
8206 		 */
8207 		cld_filter.element.flags =
8208 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8209 
8210 		if (filter->vlan_id) {
8211 			cld_filter.element.flags =
8212 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8213 		}
8214 
8215 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8216 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8217 		cld_filter.element.flags =
8218 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8219 		if (filter->n_proto == ETH_P_IPV6)
8220 			cld_filter.element.flags |=
8221 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8222 		else
8223 			cld_filter.element.flags |=
8224 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8225 	} else {
8226 		dev_err(&pf->pdev->dev,
8227 			"either mac or ip has to be valid for cloud filter\n");
8228 		return -EINVAL;
8229 	}
8230 
8231 	/* Now copy L4 port in Byte 6..7 in general fields */
8232 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8233 						be16_to_cpu(filter->dst_port);
8234 
8235 	if (add) {
8236 		/* Validate current device switch mode, change if necessary */
8237 		ret = i40e_validate_and_set_switch_mode(vsi);
8238 		if (ret) {
8239 			dev_err(&pf->pdev->dev,
8240 				"failed to set switch mode, ret %d\n",
8241 				ret);
8242 			return ret;
8243 		}
8244 
8245 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8246 						   &cld_filter, 1);
8247 	} else {
8248 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8249 						   &cld_filter, 1);
8250 	}
8251 
8252 	if (ret)
8253 		dev_dbg(&pf->pdev->dev,
8254 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8255 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8256 	else
8257 		dev_info(&pf->pdev->dev,
8258 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8259 			 add ? "add" : "delete", filter->seid,
8260 			 ntohs(filter->dst_port));
8261 	return ret;
8262 }
8263 
8264 /**
8265  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8266  * @vsi: Pointer to VSI
8267  * @f: Pointer to struct flow_cls_offload
8268  * @filter: Pointer to cloud filter structure
8269  *
8270  **/
8271 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8272 				 struct flow_cls_offload *f,
8273 				 struct i40e_cloud_filter *filter)
8274 {
8275 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8276 	struct flow_dissector *dissector = rule->match.dissector;
8277 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8278 	struct i40e_pf *pf = vsi->back;
8279 	u8 field_flags = 0;
8280 
8281 	if (dissector->used_keys &
8282 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8283 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8284 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8285 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8286 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8287 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8288 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8289 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8290 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8291 			dissector->used_keys);
8292 		return -EOPNOTSUPP;
8293 	}
8294 
8295 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8296 		struct flow_match_enc_keyid match;
8297 
8298 		flow_rule_match_enc_keyid(rule, &match);
8299 		if (match.mask->keyid != 0)
8300 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8301 
8302 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8303 	}
8304 
8305 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8306 		struct flow_match_basic match;
8307 
8308 		flow_rule_match_basic(rule, &match);
8309 		n_proto_key = ntohs(match.key->n_proto);
8310 		n_proto_mask = ntohs(match.mask->n_proto);
8311 
8312 		if (n_proto_key == ETH_P_ALL) {
8313 			n_proto_key = 0;
8314 			n_proto_mask = 0;
8315 		}
8316 		filter->n_proto = n_proto_key & n_proto_mask;
8317 		filter->ip_proto = match.key->ip_proto;
8318 	}
8319 
8320 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8321 		struct flow_match_eth_addrs match;
8322 
8323 		flow_rule_match_eth_addrs(rule, &match);
8324 
8325 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8326 		if (!is_zero_ether_addr(match.mask->dst)) {
8327 			if (is_broadcast_ether_addr(match.mask->dst)) {
8328 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8329 			} else {
8330 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8331 					match.mask->dst);
8332 				return I40E_ERR_CONFIG;
8333 			}
8334 		}
8335 
8336 		if (!is_zero_ether_addr(match.mask->src)) {
8337 			if (is_broadcast_ether_addr(match.mask->src)) {
8338 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8339 			} else {
8340 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8341 					match.mask->src);
8342 				return I40E_ERR_CONFIG;
8343 			}
8344 		}
8345 		ether_addr_copy(filter->dst_mac, match.key->dst);
8346 		ether_addr_copy(filter->src_mac, match.key->src);
8347 	}
8348 
8349 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8350 		struct flow_match_vlan match;
8351 
8352 		flow_rule_match_vlan(rule, &match);
8353 		if (match.mask->vlan_id) {
8354 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8355 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8356 
8357 			} else {
8358 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8359 					match.mask->vlan_id);
8360 				return I40E_ERR_CONFIG;
8361 			}
8362 		}
8363 
8364 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8365 	}
8366 
8367 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8368 		struct flow_match_control match;
8369 
8370 		flow_rule_match_control(rule, &match);
8371 		addr_type = match.key->addr_type;
8372 	}
8373 
8374 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8375 		struct flow_match_ipv4_addrs match;
8376 
8377 		flow_rule_match_ipv4_addrs(rule, &match);
8378 		if (match.mask->dst) {
8379 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8380 				field_flags |= I40E_CLOUD_FIELD_IIP;
8381 			} else {
8382 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8383 					&match.mask->dst);
8384 				return I40E_ERR_CONFIG;
8385 			}
8386 		}
8387 
8388 		if (match.mask->src) {
8389 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8390 				field_flags |= I40E_CLOUD_FIELD_IIP;
8391 			} else {
8392 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8393 					&match.mask->src);
8394 				return I40E_ERR_CONFIG;
8395 			}
8396 		}
8397 
8398 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8399 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8400 			return I40E_ERR_CONFIG;
8401 		}
8402 		filter->dst_ipv4 = match.key->dst;
8403 		filter->src_ipv4 = match.key->src;
8404 	}
8405 
8406 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8407 		struct flow_match_ipv6_addrs match;
8408 
8409 		flow_rule_match_ipv6_addrs(rule, &match);
8410 
8411 		/* src and dest IPV6 address should not be LOOPBACK
8412 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8413 		 */
8414 		if (ipv6_addr_loopback(&match.key->dst) ||
8415 		    ipv6_addr_loopback(&match.key->src)) {
8416 			dev_err(&pf->pdev->dev,
8417 				"Bad ipv6, addr is LOOPBACK\n");
8418 			return I40E_ERR_CONFIG;
8419 		}
8420 		if (!ipv6_addr_any(&match.mask->dst) ||
8421 		    !ipv6_addr_any(&match.mask->src))
8422 			field_flags |= I40E_CLOUD_FIELD_IIP;
8423 
8424 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8425 		       sizeof(filter->src_ipv6));
8426 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8427 		       sizeof(filter->dst_ipv6));
8428 	}
8429 
8430 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8431 		struct flow_match_ports match;
8432 
8433 		flow_rule_match_ports(rule, &match);
8434 		if (match.mask->src) {
8435 			if (match.mask->src == cpu_to_be16(0xffff)) {
8436 				field_flags |= I40E_CLOUD_FIELD_IIP;
8437 			} else {
8438 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8439 					be16_to_cpu(match.mask->src));
8440 				return I40E_ERR_CONFIG;
8441 			}
8442 		}
8443 
8444 		if (match.mask->dst) {
8445 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8446 				field_flags |= I40E_CLOUD_FIELD_IIP;
8447 			} else {
8448 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8449 					be16_to_cpu(match.mask->dst));
8450 				return I40E_ERR_CONFIG;
8451 			}
8452 		}
8453 
8454 		filter->dst_port = match.key->dst;
8455 		filter->src_port = match.key->src;
8456 
8457 		switch (filter->ip_proto) {
8458 		case IPPROTO_TCP:
8459 		case IPPROTO_UDP:
8460 			break;
8461 		default:
8462 			dev_err(&pf->pdev->dev,
8463 				"Only UDP and TCP transport are supported\n");
8464 			return -EINVAL;
8465 		}
8466 	}
8467 	filter->flags = field_flags;
8468 	return 0;
8469 }
8470 
8471 /**
8472  * i40e_handle_tclass: Forward to a traffic class on the device
8473  * @vsi: Pointer to VSI
8474  * @tc: traffic class index on the device
8475  * @filter: Pointer to cloud filter structure
8476  *
8477  **/
8478 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8479 			      struct i40e_cloud_filter *filter)
8480 {
8481 	struct i40e_channel *ch, *ch_tmp;
8482 
8483 	/* direct to a traffic class on the same device */
8484 	if (tc == 0) {
8485 		filter->seid = vsi->seid;
8486 		return 0;
8487 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8488 		if (!filter->dst_port) {
8489 			dev_err(&vsi->back->pdev->dev,
8490 				"Specify destination port to direct to traffic class that is not default\n");
8491 			return -EINVAL;
8492 		}
8493 		if (list_empty(&vsi->ch_list))
8494 			return -EINVAL;
8495 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8496 					 list) {
8497 			if (ch->seid == vsi->tc_seid_map[tc])
8498 				filter->seid = ch->seid;
8499 		}
8500 		return 0;
8501 	}
8502 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8503 	return -EINVAL;
8504 }
8505 
8506 /**
8507  * i40e_configure_clsflower - Configure tc flower filters
8508  * @vsi: Pointer to VSI
8509  * @cls_flower: Pointer to struct flow_cls_offload
8510  *
8511  **/
8512 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8513 				    struct flow_cls_offload *cls_flower)
8514 {
8515 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8516 	struct i40e_cloud_filter *filter = NULL;
8517 	struct i40e_pf *pf = vsi->back;
8518 	int err = 0;
8519 
8520 	if (tc < 0) {
8521 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8522 		return -EOPNOTSUPP;
8523 	}
8524 
8525 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8526 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8527 		return -EBUSY;
8528 
8529 	if (pf->fdir_pf_active_filters ||
8530 	    (!hlist_empty(&pf->fdir_filter_list))) {
8531 		dev_err(&vsi->back->pdev->dev,
8532 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8533 		return -EINVAL;
8534 	}
8535 
8536 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8537 		dev_err(&vsi->back->pdev->dev,
8538 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8539 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8540 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8541 	}
8542 
8543 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8544 	if (!filter)
8545 		return -ENOMEM;
8546 
8547 	filter->cookie = cls_flower->cookie;
8548 
8549 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8550 	if (err < 0)
8551 		goto err;
8552 
8553 	err = i40e_handle_tclass(vsi, tc, filter);
8554 	if (err < 0)
8555 		goto err;
8556 
8557 	/* Add cloud filter */
8558 	if (filter->dst_port)
8559 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8560 	else
8561 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8562 
8563 	if (err) {
8564 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8565 			err);
8566 		goto err;
8567 	}
8568 
8569 	/* add filter to the ordered list */
8570 	INIT_HLIST_NODE(&filter->cloud_node);
8571 
8572 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8573 
8574 	pf->num_cloud_filters++;
8575 
8576 	return err;
8577 err:
8578 	kfree(filter);
8579 	return err;
8580 }
8581 
8582 /**
8583  * i40e_find_cloud_filter - Find the could filter in the list
8584  * @vsi: Pointer to VSI
8585  * @cookie: filter specific cookie
8586  *
8587  **/
8588 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8589 							unsigned long *cookie)
8590 {
8591 	struct i40e_cloud_filter *filter = NULL;
8592 	struct hlist_node *node2;
8593 
8594 	hlist_for_each_entry_safe(filter, node2,
8595 				  &vsi->back->cloud_filter_list, cloud_node)
8596 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8597 			return filter;
8598 	return NULL;
8599 }
8600 
8601 /**
8602  * i40e_delete_clsflower - Remove tc flower filters
8603  * @vsi: Pointer to VSI
8604  * @cls_flower: Pointer to struct flow_cls_offload
8605  *
8606  **/
8607 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8608 				 struct flow_cls_offload *cls_flower)
8609 {
8610 	struct i40e_cloud_filter *filter = NULL;
8611 	struct i40e_pf *pf = vsi->back;
8612 	int err = 0;
8613 
8614 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8615 
8616 	if (!filter)
8617 		return -EINVAL;
8618 
8619 	hash_del(&filter->cloud_node);
8620 
8621 	if (filter->dst_port)
8622 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8623 	else
8624 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8625 
8626 	kfree(filter);
8627 	if (err) {
8628 		dev_err(&pf->pdev->dev,
8629 			"Failed to delete cloud filter, err %s\n",
8630 			i40e_stat_str(&pf->hw, err));
8631 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8632 	}
8633 
8634 	pf->num_cloud_filters--;
8635 	if (!pf->num_cloud_filters)
8636 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8637 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8638 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8639 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8640 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8641 		}
8642 	return 0;
8643 }
8644 
8645 /**
8646  * i40e_setup_tc_cls_flower - flower classifier offloads
8647  * @np: net device to configure
8648  * @cls_flower: offload data
8649  **/
8650 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8651 				    struct flow_cls_offload *cls_flower)
8652 {
8653 	struct i40e_vsi *vsi = np->vsi;
8654 
8655 	switch (cls_flower->command) {
8656 	case FLOW_CLS_REPLACE:
8657 		return i40e_configure_clsflower(vsi, cls_flower);
8658 	case FLOW_CLS_DESTROY:
8659 		return i40e_delete_clsflower(vsi, cls_flower);
8660 	case FLOW_CLS_STATS:
8661 		return -EOPNOTSUPP;
8662 	default:
8663 		return -EOPNOTSUPP;
8664 	}
8665 }
8666 
8667 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8668 				  void *cb_priv)
8669 {
8670 	struct i40e_netdev_priv *np = cb_priv;
8671 
8672 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8673 		return -EOPNOTSUPP;
8674 
8675 	switch (type) {
8676 	case TC_SETUP_CLSFLOWER:
8677 		return i40e_setup_tc_cls_flower(np, type_data);
8678 
8679 	default:
8680 		return -EOPNOTSUPP;
8681 	}
8682 }
8683 
8684 static LIST_HEAD(i40e_block_cb_list);
8685 
8686 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8687 			   void *type_data)
8688 {
8689 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8690 
8691 	switch (type) {
8692 	case TC_SETUP_QDISC_MQPRIO:
8693 		return i40e_setup_tc(netdev, type_data);
8694 	case TC_SETUP_BLOCK:
8695 		return flow_block_cb_setup_simple(type_data,
8696 						  &i40e_block_cb_list,
8697 						  i40e_setup_tc_block_cb,
8698 						  np, np, true);
8699 	default:
8700 		return -EOPNOTSUPP;
8701 	}
8702 }
8703 
8704 /**
8705  * i40e_open - Called when a network interface is made active
8706  * @netdev: network interface device structure
8707  *
8708  * The open entry point is called when a network interface is made
8709  * active by the system (IFF_UP).  At this point all resources needed
8710  * for transmit and receive operations are allocated, the interrupt
8711  * handler is registered with the OS, the netdev watchdog subtask is
8712  * enabled, and the stack is notified that the interface is ready.
8713  *
8714  * Returns 0 on success, negative value on failure
8715  **/
8716 int i40e_open(struct net_device *netdev)
8717 {
8718 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8719 	struct i40e_vsi *vsi = np->vsi;
8720 	struct i40e_pf *pf = vsi->back;
8721 	int err;
8722 
8723 	/* disallow open during test or if eeprom is broken */
8724 	if (test_bit(__I40E_TESTING, pf->state) ||
8725 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8726 		return -EBUSY;
8727 
8728 	netif_carrier_off(netdev);
8729 
8730 	if (i40e_force_link_state(pf, true))
8731 		return -EAGAIN;
8732 
8733 	err = i40e_vsi_open(vsi);
8734 	if (err)
8735 		return err;
8736 
8737 	/* configure global TSO hardware offload settings */
8738 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8739 						       TCP_FLAG_FIN) >> 16);
8740 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8741 						       TCP_FLAG_FIN |
8742 						       TCP_FLAG_CWR) >> 16);
8743 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8744 	udp_tunnel_get_rx_info(netdev);
8745 
8746 	return 0;
8747 }
8748 
8749 /**
8750  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8751  * @vsi: vsi structure
8752  *
8753  * This updates netdev's number of tx/rx queues
8754  *
8755  * Returns status of setting tx/rx queues
8756  **/
8757 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8758 {
8759 	int ret;
8760 
8761 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8762 					   vsi->num_queue_pairs);
8763 	if (ret)
8764 		return ret;
8765 
8766 	return netif_set_real_num_tx_queues(vsi->netdev,
8767 					    vsi->num_queue_pairs);
8768 }
8769 
8770 /**
8771  * i40e_vsi_open -
8772  * @vsi: the VSI to open
8773  *
8774  * Finish initialization of the VSI.
8775  *
8776  * Returns 0 on success, negative value on failure
8777  *
8778  * Note: expects to be called while under rtnl_lock()
8779  **/
8780 int i40e_vsi_open(struct i40e_vsi *vsi)
8781 {
8782 	struct i40e_pf *pf = vsi->back;
8783 	char int_name[I40E_INT_NAME_STR_LEN];
8784 	int err;
8785 
8786 	/* allocate descriptors */
8787 	err = i40e_vsi_setup_tx_resources(vsi);
8788 	if (err)
8789 		goto err_setup_tx;
8790 	err = i40e_vsi_setup_rx_resources(vsi);
8791 	if (err)
8792 		goto err_setup_rx;
8793 
8794 	err = i40e_vsi_configure(vsi);
8795 	if (err)
8796 		goto err_setup_rx;
8797 
8798 	if (vsi->netdev) {
8799 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8800 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8801 		err = i40e_vsi_request_irq(vsi, int_name);
8802 		if (err)
8803 			goto err_setup_rx;
8804 
8805 		/* Notify the stack of the actual queue counts. */
8806 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8807 		if (err)
8808 			goto err_set_queues;
8809 
8810 	} else if (vsi->type == I40E_VSI_FDIR) {
8811 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8812 			 dev_driver_string(&pf->pdev->dev),
8813 			 dev_name(&pf->pdev->dev));
8814 		err = i40e_vsi_request_irq(vsi, int_name);
8815 		if (err)
8816 			goto err_setup_rx;
8817 
8818 	} else {
8819 		err = -EINVAL;
8820 		goto err_setup_rx;
8821 	}
8822 
8823 	err = i40e_up_complete(vsi);
8824 	if (err)
8825 		goto err_up_complete;
8826 
8827 	return 0;
8828 
8829 err_up_complete:
8830 	i40e_down(vsi);
8831 err_set_queues:
8832 	i40e_vsi_free_irq(vsi);
8833 err_setup_rx:
8834 	i40e_vsi_free_rx_resources(vsi);
8835 err_setup_tx:
8836 	i40e_vsi_free_tx_resources(vsi);
8837 	if (vsi == pf->vsi[pf->lan_vsi])
8838 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8839 
8840 	return err;
8841 }
8842 
8843 /**
8844  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8845  * @pf: Pointer to PF
8846  *
8847  * This function destroys the hlist where all the Flow Director
8848  * filters were saved.
8849  **/
8850 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8851 {
8852 	struct i40e_fdir_filter *filter;
8853 	struct i40e_flex_pit *pit_entry, *tmp;
8854 	struct hlist_node *node2;
8855 
8856 	hlist_for_each_entry_safe(filter, node2,
8857 				  &pf->fdir_filter_list, fdir_node) {
8858 		hlist_del(&filter->fdir_node);
8859 		kfree(filter);
8860 	}
8861 
8862 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8863 		list_del(&pit_entry->list);
8864 		kfree(pit_entry);
8865 	}
8866 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8867 
8868 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8869 		list_del(&pit_entry->list);
8870 		kfree(pit_entry);
8871 	}
8872 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8873 
8874 	pf->fdir_pf_active_filters = 0;
8875 	i40e_reset_fdir_filter_cnt(pf);
8876 
8877 	/* Reprogram the default input set for TCP/IPv4 */
8878 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8879 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8880 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8881 
8882 	/* Reprogram the default input set for TCP/IPv6 */
8883 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
8884 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8885 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8886 
8887 	/* Reprogram the default input set for UDP/IPv4 */
8888 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8889 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8890 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8891 
8892 	/* Reprogram the default input set for UDP/IPv6 */
8893 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
8894 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8895 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8896 
8897 	/* Reprogram the default input set for SCTP/IPv4 */
8898 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8899 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8900 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8901 
8902 	/* Reprogram the default input set for SCTP/IPv6 */
8903 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
8904 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8905 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8906 
8907 	/* Reprogram the default input set for Other/IPv4 */
8908 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8909 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8910 
8911 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8912 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8913 
8914 	/* Reprogram the default input set for Other/IPv6 */
8915 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
8916 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8917 
8918 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
8919 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8920 }
8921 
8922 /**
8923  * i40e_cloud_filter_exit - Cleans up the cloud filters
8924  * @pf: Pointer to PF
8925  *
8926  * This function destroys the hlist where all the cloud filters
8927  * were saved.
8928  **/
8929 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8930 {
8931 	struct i40e_cloud_filter *cfilter;
8932 	struct hlist_node *node;
8933 
8934 	hlist_for_each_entry_safe(cfilter, node,
8935 				  &pf->cloud_filter_list, cloud_node) {
8936 		hlist_del(&cfilter->cloud_node);
8937 		kfree(cfilter);
8938 	}
8939 	pf->num_cloud_filters = 0;
8940 
8941 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8942 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8943 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8944 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8945 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8946 	}
8947 }
8948 
8949 /**
8950  * i40e_close - Disables a network interface
8951  * @netdev: network interface device structure
8952  *
8953  * The close entry point is called when an interface is de-activated
8954  * by the OS.  The hardware is still under the driver's control, but
8955  * this netdev interface is disabled.
8956  *
8957  * Returns 0, this is not allowed to fail
8958  **/
8959 int i40e_close(struct net_device *netdev)
8960 {
8961 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8962 	struct i40e_vsi *vsi = np->vsi;
8963 
8964 	i40e_vsi_close(vsi);
8965 
8966 	return 0;
8967 }
8968 
8969 /**
8970  * i40e_do_reset - Start a PF or Core Reset sequence
8971  * @pf: board private structure
8972  * @reset_flags: which reset is requested
8973  * @lock_acquired: indicates whether or not the lock has been acquired
8974  * before this function was called.
8975  *
8976  * The essential difference in resets is that the PF Reset
8977  * doesn't clear the packet buffers, doesn't reset the PE
8978  * firmware, and doesn't bother the other PFs on the chip.
8979  **/
8980 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8981 {
8982 	u32 val;
8983 
8984 	/* do the biggest reset indicated */
8985 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8986 
8987 		/* Request a Global Reset
8988 		 *
8989 		 * This will start the chip's countdown to the actual full
8990 		 * chip reset event, and a warning interrupt to be sent
8991 		 * to all PFs, including the requestor.  Our handler
8992 		 * for the warning interrupt will deal with the shutdown
8993 		 * and recovery of the switch setup.
8994 		 */
8995 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8996 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8997 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8998 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8999 
9000 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9001 
9002 		/* Request a Core Reset
9003 		 *
9004 		 * Same as Global Reset, except does *not* include the MAC/PHY
9005 		 */
9006 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9007 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9008 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9009 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9010 		i40e_flush(&pf->hw);
9011 
9012 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9013 
9014 		/* Request a PF Reset
9015 		 *
9016 		 * Resets only the PF-specific registers
9017 		 *
9018 		 * This goes directly to the tear-down and rebuild of
9019 		 * the switch, since we need to do all the recovery as
9020 		 * for the Core Reset.
9021 		 */
9022 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9023 		i40e_handle_reset_warning(pf, lock_acquired);
9024 
9025 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9026 		/* Request a PF Reset
9027 		 *
9028 		 * Resets PF and reinitializes PFs VSI.
9029 		 */
9030 		i40e_prep_for_reset(pf);
9031 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9032 		dev_info(&pf->pdev->dev,
9033 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9034 			 "FW LLDP is disabled\n" :
9035 			 "FW LLDP is enabled\n");
9036 
9037 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9038 		int v;
9039 
9040 		/* Find the VSI(s) that requested a re-init */
9041 		dev_info(&pf->pdev->dev,
9042 			 "VSI reinit requested\n");
9043 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9044 			struct i40e_vsi *vsi = pf->vsi[v];
9045 
9046 			if (vsi != NULL &&
9047 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9048 					       vsi->state))
9049 				i40e_vsi_reinit_locked(pf->vsi[v]);
9050 		}
9051 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9052 		int v;
9053 
9054 		/* Find the VSI(s) that needs to be brought down */
9055 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9056 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9057 			struct i40e_vsi *vsi = pf->vsi[v];
9058 
9059 			if (vsi != NULL &&
9060 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9061 					       vsi->state)) {
9062 				set_bit(__I40E_VSI_DOWN, vsi->state);
9063 				i40e_down(vsi);
9064 			}
9065 		}
9066 	} else {
9067 		dev_info(&pf->pdev->dev,
9068 			 "bad reset request 0x%08x\n", reset_flags);
9069 	}
9070 }
9071 
9072 #ifdef CONFIG_I40E_DCB
9073 /**
9074  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9075  * @pf: board private structure
9076  * @old_cfg: current DCB config
9077  * @new_cfg: new DCB config
9078  **/
9079 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9080 			    struct i40e_dcbx_config *old_cfg,
9081 			    struct i40e_dcbx_config *new_cfg)
9082 {
9083 	bool need_reconfig = false;
9084 
9085 	/* Check if ETS configuration has changed */
9086 	if (memcmp(&new_cfg->etscfg,
9087 		   &old_cfg->etscfg,
9088 		   sizeof(new_cfg->etscfg))) {
9089 		/* If Priority Table has changed reconfig is needed */
9090 		if (memcmp(&new_cfg->etscfg.prioritytable,
9091 			   &old_cfg->etscfg.prioritytable,
9092 			   sizeof(new_cfg->etscfg.prioritytable))) {
9093 			need_reconfig = true;
9094 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9095 		}
9096 
9097 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9098 			   &old_cfg->etscfg.tcbwtable,
9099 			   sizeof(new_cfg->etscfg.tcbwtable)))
9100 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9101 
9102 		if (memcmp(&new_cfg->etscfg.tsatable,
9103 			   &old_cfg->etscfg.tsatable,
9104 			   sizeof(new_cfg->etscfg.tsatable)))
9105 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9106 	}
9107 
9108 	/* Check if PFC configuration has changed */
9109 	if (memcmp(&new_cfg->pfc,
9110 		   &old_cfg->pfc,
9111 		   sizeof(new_cfg->pfc))) {
9112 		need_reconfig = true;
9113 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9114 	}
9115 
9116 	/* Check if APP Table has changed */
9117 	if (memcmp(&new_cfg->app,
9118 		   &old_cfg->app,
9119 		   sizeof(new_cfg->app))) {
9120 		need_reconfig = true;
9121 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9122 	}
9123 
9124 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9125 	return need_reconfig;
9126 }
9127 
9128 /**
9129  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9130  * @pf: board private structure
9131  * @e: event info posted on ARQ
9132  **/
9133 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9134 				  struct i40e_arq_event_info *e)
9135 {
9136 	struct i40e_aqc_lldp_get_mib *mib =
9137 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9138 	struct i40e_hw *hw = &pf->hw;
9139 	struct i40e_dcbx_config tmp_dcbx_cfg;
9140 	bool need_reconfig = false;
9141 	int ret = 0;
9142 	u8 type;
9143 
9144 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9145 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9146 	    (hw->phy.link_info.link_speed &
9147 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9148 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9149 		/* let firmware decide if the DCB should be disabled */
9150 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9151 
9152 	/* Not DCB capable or capability disabled */
9153 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9154 		return ret;
9155 
9156 	/* Ignore if event is not for Nearest Bridge */
9157 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9158 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9159 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9160 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9161 		return ret;
9162 
9163 	/* Check MIB Type and return if event for Remote MIB update */
9164 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9165 	dev_dbg(&pf->pdev->dev,
9166 		"LLDP event mib type %s\n", type ? "remote" : "local");
9167 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9168 		/* Update the remote cached instance and return */
9169 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9170 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9171 				&hw->remote_dcbx_config);
9172 		goto exit;
9173 	}
9174 
9175 	/* Store the old configuration */
9176 	tmp_dcbx_cfg = hw->local_dcbx_config;
9177 
9178 	/* Reset the old DCBx configuration data */
9179 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9180 	/* Get updated DCBX data from firmware */
9181 	ret = i40e_get_dcb_config(&pf->hw);
9182 	if (ret) {
9183 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9184 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9185 		    (hw->phy.link_info.link_speed &
9186 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9187 			dev_warn(&pf->pdev->dev,
9188 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9189 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9190 		} else {
9191 			dev_info(&pf->pdev->dev,
9192 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9193 				 i40e_stat_str(&pf->hw, ret),
9194 				 i40e_aq_str(&pf->hw,
9195 					     pf->hw.aq.asq_last_status));
9196 		}
9197 		goto exit;
9198 	}
9199 
9200 	/* No change detected in DCBX configs */
9201 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9202 		    sizeof(tmp_dcbx_cfg))) {
9203 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9204 		goto exit;
9205 	}
9206 
9207 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9208 					       &hw->local_dcbx_config);
9209 
9210 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9211 
9212 	if (!need_reconfig)
9213 		goto exit;
9214 
9215 	/* Enable DCB tagging only when more than one TC */
9216 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9217 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9218 	else
9219 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9220 
9221 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9222 	/* Reconfiguration needed quiesce all VSIs */
9223 	i40e_pf_quiesce_all_vsi(pf);
9224 
9225 	/* Changes in configuration update VEB/VSI */
9226 	i40e_dcb_reconfigure(pf);
9227 
9228 	ret = i40e_resume_port_tx(pf);
9229 
9230 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9231 	/* In case of error no point in resuming VSIs */
9232 	if (ret)
9233 		goto exit;
9234 
9235 	/* Wait for the PF's queues to be disabled */
9236 	ret = i40e_pf_wait_queues_disabled(pf);
9237 	if (ret) {
9238 		/* Schedule PF reset to recover */
9239 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9240 		i40e_service_event_schedule(pf);
9241 	} else {
9242 		i40e_pf_unquiesce_all_vsi(pf);
9243 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9244 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9245 	}
9246 
9247 exit:
9248 	return ret;
9249 }
9250 #endif /* CONFIG_I40E_DCB */
9251 
9252 /**
9253  * i40e_do_reset_safe - Protected reset path for userland calls.
9254  * @pf: board private structure
9255  * @reset_flags: which reset is requested
9256  *
9257  **/
9258 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9259 {
9260 	rtnl_lock();
9261 	i40e_do_reset(pf, reset_flags, true);
9262 	rtnl_unlock();
9263 }
9264 
9265 /**
9266  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9267  * @pf: board private structure
9268  * @e: event info posted on ARQ
9269  *
9270  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9271  * and VF queues
9272  **/
9273 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9274 					   struct i40e_arq_event_info *e)
9275 {
9276 	struct i40e_aqc_lan_overflow *data =
9277 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9278 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9279 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9280 	struct i40e_hw *hw = &pf->hw;
9281 	struct i40e_vf *vf;
9282 	u16 vf_id;
9283 
9284 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9285 		queue, qtx_ctl);
9286 
9287 	/* Queue belongs to VF, find the VF and issue VF reset */
9288 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9289 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9290 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9291 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9292 		vf_id -= hw->func_caps.vf_base_id;
9293 		vf = &pf->vf[vf_id];
9294 		i40e_vc_notify_vf_reset(vf);
9295 		/* Allow VF to process pending reset notification */
9296 		msleep(20);
9297 		i40e_reset_vf(vf, false);
9298 	}
9299 }
9300 
9301 /**
9302  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9303  * @pf: board private structure
9304  **/
9305 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9306 {
9307 	u32 val, fcnt_prog;
9308 
9309 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9310 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9311 	return fcnt_prog;
9312 }
9313 
9314 /**
9315  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9316  * @pf: board private structure
9317  **/
9318 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9319 {
9320 	u32 val, fcnt_prog;
9321 
9322 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9323 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9324 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9325 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9326 	return fcnt_prog;
9327 }
9328 
9329 /**
9330  * i40e_get_global_fd_count - Get total FD filters programmed on device
9331  * @pf: board private structure
9332  **/
9333 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9334 {
9335 	u32 val, fcnt_prog;
9336 
9337 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9338 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9339 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9340 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9341 	return fcnt_prog;
9342 }
9343 
9344 /**
9345  * i40e_reenable_fdir_sb - Restore FDir SB capability
9346  * @pf: board private structure
9347  **/
9348 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9349 {
9350 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9351 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9352 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9353 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9354 }
9355 
9356 /**
9357  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9358  * @pf: board private structure
9359  **/
9360 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9361 {
9362 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9363 		/* ATR uses the same filtering logic as SB rules. It only
9364 		 * functions properly if the input set mask is at the default
9365 		 * settings. It is safe to restore the default input set
9366 		 * because there are no active TCPv4 filter rules.
9367 		 */
9368 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9369 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9370 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9371 
9372 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9373 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9374 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9375 	}
9376 }
9377 
9378 /**
9379  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9380  * @pf: board private structure
9381  * @filter: FDir filter to remove
9382  */
9383 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9384 				       struct i40e_fdir_filter *filter)
9385 {
9386 	/* Update counters */
9387 	pf->fdir_pf_active_filters--;
9388 	pf->fd_inv = 0;
9389 
9390 	switch (filter->flow_type) {
9391 	case TCP_V4_FLOW:
9392 		pf->fd_tcp4_filter_cnt--;
9393 		break;
9394 	case UDP_V4_FLOW:
9395 		pf->fd_udp4_filter_cnt--;
9396 		break;
9397 	case SCTP_V4_FLOW:
9398 		pf->fd_sctp4_filter_cnt--;
9399 		break;
9400 	case TCP_V6_FLOW:
9401 		pf->fd_tcp6_filter_cnt--;
9402 		break;
9403 	case UDP_V6_FLOW:
9404 		pf->fd_udp6_filter_cnt--;
9405 		break;
9406 	case SCTP_V6_FLOW:
9407 		pf->fd_udp6_filter_cnt--;
9408 		break;
9409 	case IP_USER_FLOW:
9410 		switch (filter->ipl4_proto) {
9411 		case IPPROTO_TCP:
9412 			pf->fd_tcp4_filter_cnt--;
9413 			break;
9414 		case IPPROTO_UDP:
9415 			pf->fd_udp4_filter_cnt--;
9416 			break;
9417 		case IPPROTO_SCTP:
9418 			pf->fd_sctp4_filter_cnt--;
9419 			break;
9420 		case IPPROTO_IP:
9421 			pf->fd_ip4_filter_cnt--;
9422 			break;
9423 		}
9424 		break;
9425 	case IPV6_USER_FLOW:
9426 		switch (filter->ipl4_proto) {
9427 		case IPPROTO_TCP:
9428 			pf->fd_tcp6_filter_cnt--;
9429 			break;
9430 		case IPPROTO_UDP:
9431 			pf->fd_udp6_filter_cnt--;
9432 			break;
9433 		case IPPROTO_SCTP:
9434 			pf->fd_sctp6_filter_cnt--;
9435 			break;
9436 		case IPPROTO_IP:
9437 			pf->fd_ip6_filter_cnt--;
9438 			break;
9439 		}
9440 		break;
9441 	}
9442 
9443 	/* Remove the filter from the list and free memory */
9444 	hlist_del(&filter->fdir_node);
9445 	kfree(filter);
9446 }
9447 
9448 /**
9449  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9450  * @pf: board private structure
9451  **/
9452 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9453 {
9454 	struct i40e_fdir_filter *filter;
9455 	u32 fcnt_prog, fcnt_avail;
9456 	struct hlist_node *node;
9457 
9458 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9459 		return;
9460 
9461 	/* Check if we have enough room to re-enable FDir SB capability. */
9462 	fcnt_prog = i40e_get_global_fd_count(pf);
9463 	fcnt_avail = pf->fdir_pf_filter_count;
9464 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9465 	    (pf->fd_add_err == 0) ||
9466 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9467 		i40e_reenable_fdir_sb(pf);
9468 
9469 	/* We should wait for even more space before re-enabling ATR.
9470 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9471 	 * rules active.
9472 	 */
9473 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9474 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9475 		i40e_reenable_fdir_atr(pf);
9476 
9477 	/* if hw had a problem adding a filter, delete it */
9478 	if (pf->fd_inv > 0) {
9479 		hlist_for_each_entry_safe(filter, node,
9480 					  &pf->fdir_filter_list, fdir_node)
9481 			if (filter->fd_id == pf->fd_inv)
9482 				i40e_delete_invalid_filter(pf, filter);
9483 	}
9484 }
9485 
9486 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9487 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9488 /**
9489  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9490  * @pf: board private structure
9491  **/
9492 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9493 {
9494 	unsigned long min_flush_time;
9495 	int flush_wait_retry = 50;
9496 	bool disable_atr = false;
9497 	int fd_room;
9498 	int reg;
9499 
9500 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9501 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9502 		return;
9503 
9504 	/* If the flush is happening too quick and we have mostly SB rules we
9505 	 * should not re-enable ATR for some time.
9506 	 */
9507 	min_flush_time = pf->fd_flush_timestamp +
9508 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9509 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9510 
9511 	if (!(time_after(jiffies, min_flush_time)) &&
9512 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9513 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9514 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9515 		disable_atr = true;
9516 	}
9517 
9518 	pf->fd_flush_timestamp = jiffies;
9519 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9520 	/* flush all filters */
9521 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9522 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9523 	i40e_flush(&pf->hw);
9524 	pf->fd_flush_cnt++;
9525 	pf->fd_add_err = 0;
9526 	do {
9527 		/* Check FD flush status every 5-6msec */
9528 		usleep_range(5000, 6000);
9529 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9530 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9531 			break;
9532 	} while (flush_wait_retry--);
9533 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9534 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9535 	} else {
9536 		/* replay sideband filters */
9537 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9538 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9539 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9540 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9541 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9542 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9543 	}
9544 }
9545 
9546 /**
9547  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9548  * @pf: board private structure
9549  **/
9550 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9551 {
9552 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9553 }
9554 
9555 /**
9556  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9557  * @pf: board private structure
9558  **/
9559 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9560 {
9561 
9562 	/* if interface is down do nothing */
9563 	if (test_bit(__I40E_DOWN, pf->state))
9564 		return;
9565 
9566 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9567 		i40e_fdir_flush_and_replay(pf);
9568 
9569 	i40e_fdir_check_and_reenable(pf);
9570 
9571 }
9572 
9573 /**
9574  * i40e_vsi_link_event - notify VSI of a link event
9575  * @vsi: vsi to be notified
9576  * @link_up: link up or down
9577  **/
9578 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9579 {
9580 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9581 		return;
9582 
9583 	switch (vsi->type) {
9584 	case I40E_VSI_MAIN:
9585 		if (!vsi->netdev || !vsi->netdev_registered)
9586 			break;
9587 
9588 		if (link_up) {
9589 			netif_carrier_on(vsi->netdev);
9590 			netif_tx_wake_all_queues(vsi->netdev);
9591 		} else {
9592 			netif_carrier_off(vsi->netdev);
9593 			netif_tx_stop_all_queues(vsi->netdev);
9594 		}
9595 		break;
9596 
9597 	case I40E_VSI_SRIOV:
9598 	case I40E_VSI_VMDQ2:
9599 	case I40E_VSI_CTRL:
9600 	case I40E_VSI_IWARP:
9601 	case I40E_VSI_MIRROR:
9602 	default:
9603 		/* there is no notification for other VSIs */
9604 		break;
9605 	}
9606 }
9607 
9608 /**
9609  * i40e_veb_link_event - notify elements on the veb of a link event
9610  * @veb: veb to be notified
9611  * @link_up: link up or down
9612  **/
9613 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9614 {
9615 	struct i40e_pf *pf;
9616 	int i;
9617 
9618 	if (!veb || !veb->pf)
9619 		return;
9620 	pf = veb->pf;
9621 
9622 	/* depth first... */
9623 	for (i = 0; i < I40E_MAX_VEB; i++)
9624 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9625 			i40e_veb_link_event(pf->veb[i], link_up);
9626 
9627 	/* ... now the local VSIs */
9628 	for (i = 0; i < pf->num_alloc_vsi; i++)
9629 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9630 			i40e_vsi_link_event(pf->vsi[i], link_up);
9631 }
9632 
9633 /**
9634  * i40e_link_event - Update netif_carrier status
9635  * @pf: board private structure
9636  **/
9637 static void i40e_link_event(struct i40e_pf *pf)
9638 {
9639 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9640 	u8 new_link_speed, old_link_speed;
9641 	i40e_status status;
9642 	bool new_link, old_link;
9643 #ifdef CONFIG_I40E_DCB
9644 	int err;
9645 #endif /* CONFIG_I40E_DCB */
9646 
9647 	/* set this to force the get_link_status call to refresh state */
9648 	pf->hw.phy.get_link_info = true;
9649 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9650 	status = i40e_get_link_status(&pf->hw, &new_link);
9651 
9652 	/* On success, disable temp link polling */
9653 	if (status == I40E_SUCCESS) {
9654 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9655 	} else {
9656 		/* Enable link polling temporarily until i40e_get_link_status
9657 		 * returns I40E_SUCCESS
9658 		 */
9659 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9660 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9661 			status);
9662 		return;
9663 	}
9664 
9665 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9666 	new_link_speed = pf->hw.phy.link_info.link_speed;
9667 
9668 	if (new_link == old_link &&
9669 	    new_link_speed == old_link_speed &&
9670 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9671 	     new_link == netif_carrier_ok(vsi->netdev)))
9672 		return;
9673 
9674 	i40e_print_link_message(vsi, new_link);
9675 
9676 	/* Notify the base of the switch tree connected to
9677 	 * the link.  Floating VEBs are not notified.
9678 	 */
9679 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9680 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9681 	else
9682 		i40e_vsi_link_event(vsi, new_link);
9683 
9684 	if (pf->vf)
9685 		i40e_vc_notify_link_state(pf);
9686 
9687 	if (pf->flags & I40E_FLAG_PTP)
9688 		i40e_ptp_set_increment(pf);
9689 #ifdef CONFIG_I40E_DCB
9690 	if (new_link == old_link)
9691 		return;
9692 	/* Not SW DCB so firmware will take care of default settings */
9693 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9694 		return;
9695 
9696 	/* We cover here only link down, as after link up in case of SW DCB
9697 	 * SW LLDP agent will take care of setting it up
9698 	 */
9699 	if (!new_link) {
9700 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9701 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9702 		err = i40e_dcb_sw_default_config(pf);
9703 		if (err) {
9704 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9705 				       I40E_FLAG_DCB_ENABLED);
9706 		} else {
9707 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9708 				       DCB_CAP_DCBX_VER_IEEE;
9709 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9710 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9711 		}
9712 	}
9713 #endif /* CONFIG_I40E_DCB */
9714 }
9715 
9716 /**
9717  * i40e_watchdog_subtask - periodic checks not using event driven response
9718  * @pf: board private structure
9719  **/
9720 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9721 {
9722 	int i;
9723 
9724 	/* if interface is down do nothing */
9725 	if (test_bit(__I40E_DOWN, pf->state) ||
9726 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9727 		return;
9728 
9729 	/* make sure we don't do these things too often */
9730 	if (time_before(jiffies, (pf->service_timer_previous +
9731 				  pf->service_timer_period)))
9732 		return;
9733 	pf->service_timer_previous = jiffies;
9734 
9735 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9736 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9737 		i40e_link_event(pf);
9738 
9739 	/* Update the stats for active netdevs so the network stack
9740 	 * can look at updated numbers whenever it cares to
9741 	 */
9742 	for (i = 0; i < pf->num_alloc_vsi; i++)
9743 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9744 			i40e_update_stats(pf->vsi[i]);
9745 
9746 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9747 		/* Update the stats for the active switching components */
9748 		for (i = 0; i < I40E_MAX_VEB; i++)
9749 			if (pf->veb[i])
9750 				i40e_update_veb_stats(pf->veb[i]);
9751 	}
9752 
9753 	i40e_ptp_rx_hang(pf);
9754 	i40e_ptp_tx_hang(pf);
9755 }
9756 
9757 /**
9758  * i40e_reset_subtask - Set up for resetting the device and driver
9759  * @pf: board private structure
9760  **/
9761 static void i40e_reset_subtask(struct i40e_pf *pf)
9762 {
9763 	u32 reset_flags = 0;
9764 
9765 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9766 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9767 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9768 	}
9769 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9770 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9771 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9772 	}
9773 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9774 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9775 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9776 	}
9777 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9778 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9779 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9780 	}
9781 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9782 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9783 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9784 	}
9785 
9786 	/* If there's a recovery already waiting, it takes
9787 	 * precedence before starting a new reset sequence.
9788 	 */
9789 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9790 		i40e_prep_for_reset(pf);
9791 		i40e_reset(pf);
9792 		i40e_rebuild(pf, false, false);
9793 	}
9794 
9795 	/* If we're already down or resetting, just bail */
9796 	if (reset_flags &&
9797 	    !test_bit(__I40E_DOWN, pf->state) &&
9798 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9799 		i40e_do_reset(pf, reset_flags, false);
9800 	}
9801 }
9802 
9803 /**
9804  * i40e_handle_link_event - Handle link event
9805  * @pf: board private structure
9806  * @e: event info posted on ARQ
9807  **/
9808 static void i40e_handle_link_event(struct i40e_pf *pf,
9809 				   struct i40e_arq_event_info *e)
9810 {
9811 	struct i40e_aqc_get_link_status *status =
9812 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9813 
9814 	/* Do a new status request to re-enable LSE reporting
9815 	 * and load new status information into the hw struct
9816 	 * This completely ignores any state information
9817 	 * in the ARQ event info, instead choosing to always
9818 	 * issue the AQ update link status command.
9819 	 */
9820 	i40e_link_event(pf);
9821 
9822 	/* Check if module meets thermal requirements */
9823 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9824 		dev_err(&pf->pdev->dev,
9825 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9826 		dev_err(&pf->pdev->dev,
9827 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9828 	} else {
9829 		/* check for unqualified module, if link is down, suppress
9830 		 * the message if link was forced to be down.
9831 		 */
9832 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9833 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9834 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9835 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9836 			dev_err(&pf->pdev->dev,
9837 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9838 			dev_err(&pf->pdev->dev,
9839 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9840 		}
9841 	}
9842 }
9843 
9844 /**
9845  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9846  * @pf: board private structure
9847  **/
9848 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9849 {
9850 	struct i40e_arq_event_info event;
9851 	struct i40e_hw *hw = &pf->hw;
9852 	u16 pending, i = 0;
9853 	i40e_status ret;
9854 	u16 opcode;
9855 	u32 oldval;
9856 	u32 val;
9857 
9858 	/* Do not run clean AQ when PF reset fails */
9859 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9860 		return;
9861 
9862 	/* check for error indications */
9863 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9864 	oldval = val;
9865 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9866 		if (hw->debug_mask & I40E_DEBUG_AQ)
9867 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9868 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9869 	}
9870 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9871 		if (hw->debug_mask & I40E_DEBUG_AQ)
9872 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9873 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9874 		pf->arq_overflows++;
9875 	}
9876 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9877 		if (hw->debug_mask & I40E_DEBUG_AQ)
9878 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9879 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9880 	}
9881 	if (oldval != val)
9882 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9883 
9884 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9885 	oldval = val;
9886 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9887 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9888 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9889 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9890 	}
9891 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9892 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9893 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9894 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9895 	}
9896 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9897 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9898 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9899 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9900 	}
9901 	if (oldval != val)
9902 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9903 
9904 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9905 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9906 	if (!event.msg_buf)
9907 		return;
9908 
9909 	do {
9910 		ret = i40e_clean_arq_element(hw, &event, &pending);
9911 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9912 			break;
9913 		else if (ret) {
9914 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9915 			break;
9916 		}
9917 
9918 		opcode = le16_to_cpu(event.desc.opcode);
9919 		switch (opcode) {
9920 
9921 		case i40e_aqc_opc_get_link_status:
9922 			rtnl_lock();
9923 			i40e_handle_link_event(pf, &event);
9924 			rtnl_unlock();
9925 			break;
9926 		case i40e_aqc_opc_send_msg_to_pf:
9927 			ret = i40e_vc_process_vf_msg(pf,
9928 					le16_to_cpu(event.desc.retval),
9929 					le32_to_cpu(event.desc.cookie_high),
9930 					le32_to_cpu(event.desc.cookie_low),
9931 					event.msg_buf,
9932 					event.msg_len);
9933 			break;
9934 		case i40e_aqc_opc_lldp_update_mib:
9935 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9936 #ifdef CONFIG_I40E_DCB
9937 			rtnl_lock();
9938 			i40e_handle_lldp_event(pf, &event);
9939 			rtnl_unlock();
9940 #endif /* CONFIG_I40E_DCB */
9941 			break;
9942 		case i40e_aqc_opc_event_lan_overflow:
9943 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9944 			i40e_handle_lan_overflow_event(pf, &event);
9945 			break;
9946 		case i40e_aqc_opc_send_msg_to_peer:
9947 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9948 			break;
9949 		case i40e_aqc_opc_nvm_erase:
9950 		case i40e_aqc_opc_nvm_update:
9951 		case i40e_aqc_opc_oem_post_update:
9952 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9953 				   "ARQ NVM operation 0x%04x completed\n",
9954 				   opcode);
9955 			break;
9956 		default:
9957 			dev_info(&pf->pdev->dev,
9958 				 "ARQ: Unknown event 0x%04x ignored\n",
9959 				 opcode);
9960 			break;
9961 		}
9962 	} while (i++ < pf->adminq_work_limit);
9963 
9964 	if (i < pf->adminq_work_limit)
9965 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9966 
9967 	/* re-enable Admin queue interrupt cause */
9968 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9969 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9970 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9971 	i40e_flush(hw);
9972 
9973 	kfree(event.msg_buf);
9974 }
9975 
9976 /**
9977  * i40e_verify_eeprom - make sure eeprom is good to use
9978  * @pf: board private structure
9979  **/
9980 static void i40e_verify_eeprom(struct i40e_pf *pf)
9981 {
9982 	int err;
9983 
9984 	err = i40e_diag_eeprom_test(&pf->hw);
9985 	if (err) {
9986 		/* retry in case of garbage read */
9987 		err = i40e_diag_eeprom_test(&pf->hw);
9988 		if (err) {
9989 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9990 				 err);
9991 			set_bit(__I40E_BAD_EEPROM, pf->state);
9992 		}
9993 	}
9994 
9995 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9996 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9997 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9998 	}
9999 }
10000 
10001 /**
10002  * i40e_enable_pf_switch_lb
10003  * @pf: pointer to the PF structure
10004  *
10005  * enable switch loop back or die - no point in a return value
10006  **/
10007 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10008 {
10009 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10010 	struct i40e_vsi_context ctxt;
10011 	int ret;
10012 
10013 	ctxt.seid = pf->main_vsi_seid;
10014 	ctxt.pf_num = pf->hw.pf_id;
10015 	ctxt.vf_num = 0;
10016 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10017 	if (ret) {
10018 		dev_info(&pf->pdev->dev,
10019 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10020 			 i40e_stat_str(&pf->hw, ret),
10021 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10022 		return;
10023 	}
10024 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10025 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10026 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10027 
10028 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10029 	if (ret) {
10030 		dev_info(&pf->pdev->dev,
10031 			 "update vsi switch failed, err %s aq_err %s\n",
10032 			 i40e_stat_str(&pf->hw, ret),
10033 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10034 	}
10035 }
10036 
10037 /**
10038  * i40e_disable_pf_switch_lb
10039  * @pf: pointer to the PF structure
10040  *
10041  * disable switch loop back or die - no point in a return value
10042  **/
10043 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10044 {
10045 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10046 	struct i40e_vsi_context ctxt;
10047 	int ret;
10048 
10049 	ctxt.seid = pf->main_vsi_seid;
10050 	ctxt.pf_num = pf->hw.pf_id;
10051 	ctxt.vf_num = 0;
10052 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10053 	if (ret) {
10054 		dev_info(&pf->pdev->dev,
10055 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10056 			 i40e_stat_str(&pf->hw, ret),
10057 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10058 		return;
10059 	}
10060 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10061 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10062 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10063 
10064 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10065 	if (ret) {
10066 		dev_info(&pf->pdev->dev,
10067 			 "update vsi switch failed, err %s aq_err %s\n",
10068 			 i40e_stat_str(&pf->hw, ret),
10069 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10070 	}
10071 }
10072 
10073 /**
10074  * i40e_config_bridge_mode - Configure the HW bridge mode
10075  * @veb: pointer to the bridge instance
10076  *
10077  * Configure the loop back mode for the LAN VSI that is downlink to the
10078  * specified HW bridge instance. It is expected this function is called
10079  * when a new HW bridge is instantiated.
10080  **/
10081 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10082 {
10083 	struct i40e_pf *pf = veb->pf;
10084 
10085 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10086 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10087 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10088 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10089 		i40e_disable_pf_switch_lb(pf);
10090 	else
10091 		i40e_enable_pf_switch_lb(pf);
10092 }
10093 
10094 /**
10095  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10096  * @veb: pointer to the VEB instance
10097  *
10098  * This is a recursive function that first builds the attached VSIs then
10099  * recurses in to build the next layer of VEB.  We track the connections
10100  * through our own index numbers because the seid's from the HW could
10101  * change across the reset.
10102  **/
10103 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10104 {
10105 	struct i40e_vsi *ctl_vsi = NULL;
10106 	struct i40e_pf *pf = veb->pf;
10107 	int v, veb_idx;
10108 	int ret;
10109 
10110 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10111 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10112 		if (pf->vsi[v] &&
10113 		    pf->vsi[v]->veb_idx == veb->idx &&
10114 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10115 			ctl_vsi = pf->vsi[v];
10116 			break;
10117 		}
10118 	}
10119 	if (!ctl_vsi) {
10120 		dev_info(&pf->pdev->dev,
10121 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10122 		ret = -ENOENT;
10123 		goto end_reconstitute;
10124 	}
10125 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10126 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10127 	ret = i40e_add_vsi(ctl_vsi);
10128 	if (ret) {
10129 		dev_info(&pf->pdev->dev,
10130 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10131 			 veb->idx, ret);
10132 		goto end_reconstitute;
10133 	}
10134 	i40e_vsi_reset_stats(ctl_vsi);
10135 
10136 	/* create the VEB in the switch and move the VSI onto the VEB */
10137 	ret = i40e_add_veb(veb, ctl_vsi);
10138 	if (ret)
10139 		goto end_reconstitute;
10140 
10141 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10142 		veb->bridge_mode = BRIDGE_MODE_VEB;
10143 	else
10144 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10145 	i40e_config_bridge_mode(veb);
10146 
10147 	/* create the remaining VSIs attached to this VEB */
10148 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10149 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10150 			continue;
10151 
10152 		if (pf->vsi[v]->veb_idx == veb->idx) {
10153 			struct i40e_vsi *vsi = pf->vsi[v];
10154 
10155 			vsi->uplink_seid = veb->seid;
10156 			ret = i40e_add_vsi(vsi);
10157 			if (ret) {
10158 				dev_info(&pf->pdev->dev,
10159 					 "rebuild of vsi_idx %d failed: %d\n",
10160 					 v, ret);
10161 				goto end_reconstitute;
10162 			}
10163 			i40e_vsi_reset_stats(vsi);
10164 		}
10165 	}
10166 
10167 	/* create any VEBs attached to this VEB - RECURSION */
10168 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10169 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10170 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10171 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10172 			if (ret)
10173 				break;
10174 		}
10175 	}
10176 
10177 end_reconstitute:
10178 	return ret;
10179 }
10180 
10181 /**
10182  * i40e_get_capabilities - get info about the HW
10183  * @pf: the PF struct
10184  * @list_type: AQ capability to be queried
10185  **/
10186 static int i40e_get_capabilities(struct i40e_pf *pf,
10187 				 enum i40e_admin_queue_opc list_type)
10188 {
10189 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10190 	u16 data_size;
10191 	int buf_len;
10192 	int err;
10193 
10194 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10195 	do {
10196 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10197 		if (!cap_buf)
10198 			return -ENOMEM;
10199 
10200 		/* this loads the data into the hw struct for us */
10201 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10202 						    &data_size, list_type,
10203 						    NULL);
10204 		/* data loaded, buffer no longer needed */
10205 		kfree(cap_buf);
10206 
10207 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10208 			/* retry with a larger buffer */
10209 			buf_len = data_size;
10210 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10211 			dev_info(&pf->pdev->dev,
10212 				 "capability discovery failed, err %s aq_err %s\n",
10213 				 i40e_stat_str(&pf->hw, err),
10214 				 i40e_aq_str(&pf->hw,
10215 					     pf->hw.aq.asq_last_status));
10216 			return -ENODEV;
10217 		}
10218 	} while (err);
10219 
10220 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10221 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10222 			dev_info(&pf->pdev->dev,
10223 				 "pf=%d, num_vfs=%d, msix_pf=%d, msix_vf=%d, fd_g=%d, fd_b=%d, pf_max_q=%d num_vsi=%d\n",
10224 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10225 				 pf->hw.func_caps.num_msix_vectors,
10226 				 pf->hw.func_caps.num_msix_vectors_vf,
10227 				 pf->hw.func_caps.fd_filters_guaranteed,
10228 				 pf->hw.func_caps.fd_filters_best_effort,
10229 				 pf->hw.func_caps.num_tx_qp,
10230 				 pf->hw.func_caps.num_vsis);
10231 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10232 			dev_info(&pf->pdev->dev,
10233 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10234 				 pf->hw.dev_caps.switch_mode,
10235 				 pf->hw.dev_caps.valid_functions);
10236 			dev_info(&pf->pdev->dev,
10237 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10238 				 pf->hw.dev_caps.sr_iov_1_1,
10239 				 pf->hw.dev_caps.num_vfs);
10240 			dev_info(&pf->pdev->dev,
10241 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10242 				 pf->hw.dev_caps.num_vsis,
10243 				 pf->hw.dev_caps.num_rx_qp,
10244 				 pf->hw.dev_caps.num_tx_qp);
10245 		}
10246 	}
10247 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10248 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10249 		       + pf->hw.func_caps.num_vfs)
10250 		if (pf->hw.revision_id == 0 &&
10251 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10252 			dev_info(&pf->pdev->dev,
10253 				 "got num_vsis %d, setting num_vsis to %d\n",
10254 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10255 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10256 		}
10257 	}
10258 	return 0;
10259 }
10260 
10261 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10262 
10263 /**
10264  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10265  * @pf: board private structure
10266  **/
10267 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10268 {
10269 	struct i40e_vsi *vsi;
10270 
10271 	/* quick workaround for an NVM issue that leaves a critical register
10272 	 * uninitialized
10273 	 */
10274 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10275 		static const u32 hkey[] = {
10276 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10277 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10278 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10279 			0x95b3a76d};
10280 		int i;
10281 
10282 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10283 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10284 	}
10285 
10286 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10287 		return;
10288 
10289 	/* find existing VSI and see if it needs configuring */
10290 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10291 
10292 	/* create a new VSI if none exists */
10293 	if (!vsi) {
10294 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10295 				     pf->vsi[pf->lan_vsi]->seid, 0);
10296 		if (!vsi) {
10297 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10298 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10299 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10300 			return;
10301 		}
10302 	}
10303 
10304 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10305 }
10306 
10307 /**
10308  * i40e_fdir_teardown - release the Flow Director resources
10309  * @pf: board private structure
10310  **/
10311 static void i40e_fdir_teardown(struct i40e_pf *pf)
10312 {
10313 	struct i40e_vsi *vsi;
10314 
10315 	i40e_fdir_filter_exit(pf);
10316 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10317 	if (vsi)
10318 		i40e_vsi_release(vsi);
10319 }
10320 
10321 /**
10322  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10323  * @vsi: PF main vsi
10324  * @seid: seid of main or channel VSIs
10325  *
10326  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10327  * existed before reset
10328  **/
10329 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10330 {
10331 	struct i40e_cloud_filter *cfilter;
10332 	struct i40e_pf *pf = vsi->back;
10333 	struct hlist_node *node;
10334 	i40e_status ret;
10335 
10336 	/* Add cloud filters back if they exist */
10337 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10338 				  cloud_node) {
10339 		if (cfilter->seid != seid)
10340 			continue;
10341 
10342 		if (cfilter->dst_port)
10343 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10344 								true);
10345 		else
10346 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10347 
10348 		if (ret) {
10349 			dev_dbg(&pf->pdev->dev,
10350 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10351 				i40e_stat_str(&pf->hw, ret),
10352 				i40e_aq_str(&pf->hw,
10353 					    pf->hw.aq.asq_last_status));
10354 			return ret;
10355 		}
10356 	}
10357 	return 0;
10358 }
10359 
10360 /**
10361  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10362  * @vsi: PF main vsi
10363  *
10364  * Rebuilds channel VSIs if they existed before reset
10365  **/
10366 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10367 {
10368 	struct i40e_channel *ch, *ch_tmp;
10369 	i40e_status ret;
10370 
10371 	if (list_empty(&vsi->ch_list))
10372 		return 0;
10373 
10374 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10375 		if (!ch->initialized)
10376 			break;
10377 		/* Proceed with creation of channel (VMDq2) VSI */
10378 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10379 		if (ret) {
10380 			dev_info(&vsi->back->pdev->dev,
10381 				 "failed to rebuild channels using uplink_seid %u\n",
10382 				 vsi->uplink_seid);
10383 			return ret;
10384 		}
10385 		/* Reconfigure TX queues using QTX_CTL register */
10386 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10387 		if (ret) {
10388 			dev_info(&vsi->back->pdev->dev,
10389 				 "failed to configure TX rings for channel %u\n",
10390 				 ch->seid);
10391 			return ret;
10392 		}
10393 		/* update 'next_base_queue' */
10394 		vsi->next_base_queue = vsi->next_base_queue +
10395 							ch->num_queue_pairs;
10396 		if (ch->max_tx_rate) {
10397 			u64 credits = ch->max_tx_rate;
10398 
10399 			if (i40e_set_bw_limit(vsi, ch->seid,
10400 					      ch->max_tx_rate))
10401 				return -EINVAL;
10402 
10403 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10404 			dev_dbg(&vsi->back->pdev->dev,
10405 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10406 				ch->max_tx_rate,
10407 				credits,
10408 				ch->seid);
10409 		}
10410 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10411 		if (ret) {
10412 			dev_dbg(&vsi->back->pdev->dev,
10413 				"Failed to rebuild cloud filters for channel VSI %u\n",
10414 				ch->seid);
10415 			return ret;
10416 		}
10417 	}
10418 	return 0;
10419 }
10420 
10421 /**
10422  * i40e_prep_for_reset - prep for the core to reset
10423  * @pf: board private structure
10424  *
10425  * Close up the VFs and other things in prep for PF Reset.
10426   **/
10427 static void i40e_prep_for_reset(struct i40e_pf *pf)
10428 {
10429 	struct i40e_hw *hw = &pf->hw;
10430 	i40e_status ret = 0;
10431 	u32 v;
10432 
10433 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10434 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10435 		return;
10436 	if (i40e_check_asq_alive(&pf->hw))
10437 		i40e_vc_notify_reset(pf);
10438 
10439 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10440 
10441 	/* quiesce the VSIs and their queues that are not already DOWN */
10442 	i40e_pf_quiesce_all_vsi(pf);
10443 
10444 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10445 		if (pf->vsi[v])
10446 			pf->vsi[v]->seid = 0;
10447 	}
10448 
10449 	i40e_shutdown_adminq(&pf->hw);
10450 
10451 	/* call shutdown HMC */
10452 	if (hw->hmc.hmc_obj) {
10453 		ret = i40e_shutdown_lan_hmc(hw);
10454 		if (ret)
10455 			dev_warn(&pf->pdev->dev,
10456 				 "shutdown_lan_hmc failed: %d\n", ret);
10457 	}
10458 
10459 	/* Save the current PTP time so that we can restore the time after the
10460 	 * reset completes.
10461 	 */
10462 	i40e_ptp_save_hw_time(pf);
10463 }
10464 
10465 /**
10466  * i40e_send_version - update firmware with driver version
10467  * @pf: PF struct
10468  */
10469 static void i40e_send_version(struct i40e_pf *pf)
10470 {
10471 	struct i40e_driver_version dv;
10472 
10473 	dv.major_version = 0xff;
10474 	dv.minor_version = 0xff;
10475 	dv.build_version = 0xff;
10476 	dv.subbuild_version = 0;
10477 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10478 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10479 }
10480 
10481 /**
10482  * i40e_get_oem_version - get OEM specific version information
10483  * @hw: pointer to the hardware structure
10484  **/
10485 static void i40e_get_oem_version(struct i40e_hw *hw)
10486 {
10487 	u16 block_offset = 0xffff;
10488 	u16 block_length = 0;
10489 	u16 capabilities = 0;
10490 	u16 gen_snap = 0;
10491 	u16 release = 0;
10492 
10493 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10494 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10495 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10496 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10497 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10498 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10499 #define I40E_NVM_OEM_LENGTH			3
10500 
10501 	/* Check if pointer to OEM version block is valid. */
10502 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10503 	if (block_offset == 0xffff)
10504 		return;
10505 
10506 	/* Check if OEM version block has correct length. */
10507 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10508 			   &block_length);
10509 	if (block_length < I40E_NVM_OEM_LENGTH)
10510 		return;
10511 
10512 	/* Check if OEM version format is as expected. */
10513 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10514 			   &capabilities);
10515 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10516 		return;
10517 
10518 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10519 			   &gen_snap);
10520 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10521 			   &release);
10522 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10523 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10524 }
10525 
10526 /**
10527  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10528  * @pf: board private structure
10529  **/
10530 static int i40e_reset(struct i40e_pf *pf)
10531 {
10532 	struct i40e_hw *hw = &pf->hw;
10533 	i40e_status ret;
10534 
10535 	ret = i40e_pf_reset(hw);
10536 	if (ret) {
10537 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10538 		set_bit(__I40E_RESET_FAILED, pf->state);
10539 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10540 	} else {
10541 		pf->pfr_count++;
10542 	}
10543 	return ret;
10544 }
10545 
10546 /**
10547  * i40e_rebuild - rebuild using a saved config
10548  * @pf: board private structure
10549  * @reinit: if the Main VSI needs to re-initialized.
10550  * @lock_acquired: indicates whether or not the lock has been acquired
10551  * before this function was called.
10552  **/
10553 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10554 {
10555 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10556 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10557 	struct i40e_hw *hw = &pf->hw;
10558 	i40e_status ret;
10559 	u32 val;
10560 	int v;
10561 
10562 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10563 	    i40e_check_recovery_mode(pf)) {
10564 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10565 	}
10566 
10567 	if (test_bit(__I40E_DOWN, pf->state) &&
10568 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10569 	    !old_recovery_mode_bit)
10570 		goto clear_recovery;
10571 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10572 
10573 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10574 	ret = i40e_init_adminq(&pf->hw);
10575 	if (ret) {
10576 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10577 			 i40e_stat_str(&pf->hw, ret),
10578 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10579 		goto clear_recovery;
10580 	}
10581 	i40e_get_oem_version(&pf->hw);
10582 
10583 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10584 		/* The following delay is necessary for firmware update. */
10585 		mdelay(1000);
10586 	}
10587 
10588 	/* re-verify the eeprom if we just had an EMP reset */
10589 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10590 		i40e_verify_eeprom(pf);
10591 
10592 	/* if we are going out of or into recovery mode we have to act
10593 	 * accordingly with regard to resources initialization
10594 	 * and deinitialization
10595 	 */
10596 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10597 	    old_recovery_mode_bit) {
10598 		if (i40e_get_capabilities(pf,
10599 					  i40e_aqc_opc_list_func_capabilities))
10600 			goto end_unlock;
10601 
10602 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10603 			/* we're staying in recovery mode so we'll reinitialize
10604 			 * misc vector here
10605 			 */
10606 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10607 				goto end_unlock;
10608 		} else {
10609 			if (!lock_acquired)
10610 				rtnl_lock();
10611 			/* we're going out of recovery mode so we'll free
10612 			 * the IRQ allocated specifically for recovery mode
10613 			 * and restore the interrupt scheme
10614 			 */
10615 			free_irq(pf->pdev->irq, pf);
10616 			i40e_clear_interrupt_scheme(pf);
10617 			if (i40e_restore_interrupt_scheme(pf))
10618 				goto end_unlock;
10619 		}
10620 
10621 		/* tell the firmware that we're starting */
10622 		i40e_send_version(pf);
10623 
10624 		/* bail out in case recovery mode was detected, as there is
10625 		 * no need for further configuration.
10626 		 */
10627 		goto end_unlock;
10628 	}
10629 
10630 	i40e_clear_pxe_mode(hw);
10631 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10632 	if (ret)
10633 		goto end_core_reset;
10634 
10635 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10636 				hw->func_caps.num_rx_qp, 0, 0);
10637 	if (ret) {
10638 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10639 		goto end_core_reset;
10640 	}
10641 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10642 	if (ret) {
10643 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10644 		goto end_core_reset;
10645 	}
10646 
10647 #ifdef CONFIG_I40E_DCB
10648 	/* Enable FW to write a default DCB config on link-up
10649 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10650 	 * is not supported with new link speed
10651 	 */
10652 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10653 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10654 	} else {
10655 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10656 		    (hw->phy.link_info.link_speed &
10657 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10658 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10659 			dev_warn(&pf->pdev->dev,
10660 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10661 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10662 		} else {
10663 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10664 			ret = i40e_init_pf_dcb(pf);
10665 			if (ret) {
10666 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10667 					 ret);
10668 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10669 				/* Continue without DCB enabled */
10670 			}
10671 		}
10672 	}
10673 
10674 #endif /* CONFIG_I40E_DCB */
10675 	if (!lock_acquired)
10676 		rtnl_lock();
10677 	ret = i40e_setup_pf_switch(pf, reinit, true);
10678 	if (ret)
10679 		goto end_unlock;
10680 
10681 	/* The driver only wants link up/down and module qualification
10682 	 * reports from firmware.  Note the negative logic.
10683 	 */
10684 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10685 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10686 					 I40E_AQ_EVENT_MEDIA_NA |
10687 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10688 	if (ret)
10689 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10690 			 i40e_stat_str(&pf->hw, ret),
10691 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10692 
10693 	/* Rebuild the VSIs and VEBs that existed before reset.
10694 	 * They are still in our local switch element arrays, so only
10695 	 * need to rebuild the switch model in the HW.
10696 	 *
10697 	 * If there were VEBs but the reconstitution failed, we'll try
10698 	 * to recover minimal use by getting the basic PF VSI working.
10699 	 */
10700 	if (vsi->uplink_seid != pf->mac_seid) {
10701 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10702 		/* find the one VEB connected to the MAC, and find orphans */
10703 		for (v = 0; v < I40E_MAX_VEB; v++) {
10704 			if (!pf->veb[v])
10705 				continue;
10706 
10707 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10708 			    pf->veb[v]->uplink_seid == 0) {
10709 				ret = i40e_reconstitute_veb(pf->veb[v]);
10710 
10711 				if (!ret)
10712 					continue;
10713 
10714 				/* If Main VEB failed, we're in deep doodoo,
10715 				 * so give up rebuilding the switch and set up
10716 				 * for minimal rebuild of PF VSI.
10717 				 * If orphan failed, we'll report the error
10718 				 * but try to keep going.
10719 				 */
10720 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10721 					dev_info(&pf->pdev->dev,
10722 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10723 						 ret);
10724 					vsi->uplink_seid = pf->mac_seid;
10725 					break;
10726 				} else if (pf->veb[v]->uplink_seid == 0) {
10727 					dev_info(&pf->pdev->dev,
10728 						 "rebuild of orphan VEB failed: %d\n",
10729 						 ret);
10730 				}
10731 			}
10732 		}
10733 	}
10734 
10735 	if (vsi->uplink_seid == pf->mac_seid) {
10736 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10737 		/* no VEB, so rebuild only the Main VSI */
10738 		ret = i40e_add_vsi(vsi);
10739 		if (ret) {
10740 			dev_info(&pf->pdev->dev,
10741 				 "rebuild of Main VSI failed: %d\n", ret);
10742 			goto end_unlock;
10743 		}
10744 	}
10745 
10746 	if (vsi->mqprio_qopt.max_rate[0]) {
10747 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10748 		u64 credits = 0;
10749 
10750 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10751 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10752 		if (ret)
10753 			goto end_unlock;
10754 
10755 		credits = max_tx_rate;
10756 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10757 		dev_dbg(&vsi->back->pdev->dev,
10758 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10759 			max_tx_rate,
10760 			credits,
10761 			vsi->seid);
10762 	}
10763 
10764 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10765 	if (ret)
10766 		goto end_unlock;
10767 
10768 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10769 	 * for this main VSI if they exist
10770 	 */
10771 	ret = i40e_rebuild_channels(vsi);
10772 	if (ret)
10773 		goto end_unlock;
10774 
10775 	/* Reconfigure hardware for allowing smaller MSS in the case
10776 	 * of TSO, so that we avoid the MDD being fired and causing
10777 	 * a reset in the case of small MSS+TSO.
10778 	 */
10779 #define I40E_REG_MSS          0x000E64DC
10780 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10781 #define I40E_64BYTE_MSS       0x400000
10782 	val = rd32(hw, I40E_REG_MSS);
10783 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10784 		val &= ~I40E_REG_MSS_MIN_MASK;
10785 		val |= I40E_64BYTE_MSS;
10786 		wr32(hw, I40E_REG_MSS, val);
10787 	}
10788 
10789 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10790 		msleep(75);
10791 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10792 		if (ret)
10793 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10794 				 i40e_stat_str(&pf->hw, ret),
10795 				 i40e_aq_str(&pf->hw,
10796 					     pf->hw.aq.asq_last_status));
10797 	}
10798 	/* reinit the misc interrupt */
10799 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10800 		ret = i40e_setup_misc_vector(pf);
10801 
10802 	/* Add a filter to drop all Flow control frames from any VSI from being
10803 	 * transmitted. By doing so we stop a malicious VF from sending out
10804 	 * PAUSE or PFC frames and potentially controlling traffic for other
10805 	 * PF/VF VSIs.
10806 	 * The FW can still send Flow control frames if enabled.
10807 	 */
10808 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10809 						       pf->main_vsi_seid);
10810 
10811 	/* restart the VSIs that were rebuilt and running before the reset */
10812 	i40e_pf_unquiesce_all_vsi(pf);
10813 
10814 	/* Release the RTNL lock before we start resetting VFs */
10815 	if (!lock_acquired)
10816 		rtnl_unlock();
10817 
10818 	/* Restore promiscuous settings */
10819 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10820 	if (ret)
10821 		dev_warn(&pf->pdev->dev,
10822 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10823 			 pf->cur_promisc ? "on" : "off",
10824 			 i40e_stat_str(&pf->hw, ret),
10825 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10826 
10827 	i40e_reset_all_vfs(pf, true);
10828 
10829 	/* tell the firmware that we're starting */
10830 	i40e_send_version(pf);
10831 
10832 	/* We've already released the lock, so don't do it again */
10833 	goto end_core_reset;
10834 
10835 end_unlock:
10836 	if (!lock_acquired)
10837 		rtnl_unlock();
10838 end_core_reset:
10839 	clear_bit(__I40E_RESET_FAILED, pf->state);
10840 clear_recovery:
10841 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10842 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10843 }
10844 
10845 /**
10846  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10847  * @pf: board private structure
10848  * @reinit: if the Main VSI needs to re-initialized.
10849  * @lock_acquired: indicates whether or not the lock has been acquired
10850  * before this function was called.
10851  **/
10852 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10853 				   bool lock_acquired)
10854 {
10855 	int ret;
10856 	/* Now we wait for GRST to settle out.
10857 	 * We don't have to delete the VEBs or VSIs from the hw switch
10858 	 * because the reset will make them disappear.
10859 	 */
10860 	ret = i40e_reset(pf);
10861 	if (!ret)
10862 		i40e_rebuild(pf, reinit, lock_acquired);
10863 }
10864 
10865 /**
10866  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10867  * @pf: board private structure
10868  *
10869  * Close up the VFs and other things in prep for a Core Reset,
10870  * then get ready to rebuild the world.
10871  * @lock_acquired: indicates whether or not the lock has been acquired
10872  * before this function was called.
10873  **/
10874 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10875 {
10876 	i40e_prep_for_reset(pf);
10877 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10878 }
10879 
10880 /**
10881  * i40e_handle_mdd_event
10882  * @pf: pointer to the PF structure
10883  *
10884  * Called from the MDD irq handler to identify possibly malicious vfs
10885  **/
10886 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10887 {
10888 	struct i40e_hw *hw = &pf->hw;
10889 	bool mdd_detected = false;
10890 	struct i40e_vf *vf;
10891 	u32 reg;
10892 	int i;
10893 
10894 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10895 		return;
10896 
10897 	/* find what triggered the MDD event */
10898 	reg = rd32(hw, I40E_GL_MDET_TX);
10899 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10900 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10901 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10902 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10903 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10904 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10905 				I40E_GL_MDET_TX_EVENT_SHIFT;
10906 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10907 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10908 				pf->hw.func_caps.base_queue;
10909 		if (netif_msg_tx_err(pf))
10910 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10911 				 event, queue, pf_num, vf_num);
10912 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10913 		mdd_detected = true;
10914 	}
10915 	reg = rd32(hw, I40E_GL_MDET_RX);
10916 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10917 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10918 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10919 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10920 				I40E_GL_MDET_RX_EVENT_SHIFT;
10921 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10922 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10923 				pf->hw.func_caps.base_queue;
10924 		if (netif_msg_rx_err(pf))
10925 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10926 				 event, queue, func);
10927 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10928 		mdd_detected = true;
10929 	}
10930 
10931 	if (mdd_detected) {
10932 		reg = rd32(hw, I40E_PF_MDET_TX);
10933 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10934 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10935 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10936 		}
10937 		reg = rd32(hw, I40E_PF_MDET_RX);
10938 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10939 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10940 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10941 		}
10942 	}
10943 
10944 	/* see if one of the VFs needs its hand slapped */
10945 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10946 		vf = &(pf->vf[i]);
10947 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10948 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10949 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10950 			vf->num_mdd_events++;
10951 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10952 				 i);
10953 			dev_info(&pf->pdev->dev,
10954 				 "Use PF Control I/F to re-enable the VF\n");
10955 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10956 		}
10957 
10958 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10959 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10960 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10961 			vf->num_mdd_events++;
10962 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10963 				 i);
10964 			dev_info(&pf->pdev->dev,
10965 				 "Use PF Control I/F to re-enable the VF\n");
10966 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10967 		}
10968 	}
10969 
10970 	/* re-enable mdd interrupt cause */
10971 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10972 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10973 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10974 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10975 	i40e_flush(hw);
10976 }
10977 
10978 /**
10979  * i40e_service_task - Run the driver's async subtasks
10980  * @work: pointer to work_struct containing our data
10981  **/
10982 static void i40e_service_task(struct work_struct *work)
10983 {
10984 	struct i40e_pf *pf = container_of(work,
10985 					  struct i40e_pf,
10986 					  service_task);
10987 	unsigned long start_time = jiffies;
10988 
10989 	/* don't bother with service tasks if a reset is in progress */
10990 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10991 	    test_bit(__I40E_SUSPENDED, pf->state))
10992 		return;
10993 
10994 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10995 		return;
10996 
10997 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10998 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10999 		i40e_sync_filters_subtask(pf);
11000 		i40e_reset_subtask(pf);
11001 		i40e_handle_mdd_event(pf);
11002 		i40e_vc_process_vflr_event(pf);
11003 		i40e_watchdog_subtask(pf);
11004 		i40e_fdir_reinit_subtask(pf);
11005 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11006 			/* Client subtask will reopen next time through. */
11007 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11008 							   true);
11009 		} else {
11010 			i40e_client_subtask(pf);
11011 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11012 					       pf->state))
11013 				i40e_notify_client_of_l2_param_changes(
11014 								pf->vsi[pf->lan_vsi]);
11015 		}
11016 		i40e_sync_filters_subtask(pf);
11017 	} else {
11018 		i40e_reset_subtask(pf);
11019 	}
11020 
11021 	i40e_clean_adminq_subtask(pf);
11022 
11023 	/* flush memory to make sure state is correct before next watchdog */
11024 	smp_mb__before_atomic();
11025 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11026 
11027 	/* If the tasks have taken longer than one timer cycle or there
11028 	 * is more work to be done, reschedule the service task now
11029 	 * rather than wait for the timer to tick again.
11030 	 */
11031 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11032 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11033 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11034 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11035 		i40e_service_event_schedule(pf);
11036 }
11037 
11038 /**
11039  * i40e_service_timer - timer callback
11040  * @t: timer list pointer
11041  **/
11042 static void i40e_service_timer(struct timer_list *t)
11043 {
11044 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11045 
11046 	mod_timer(&pf->service_timer,
11047 		  round_jiffies(jiffies + pf->service_timer_period));
11048 	i40e_service_event_schedule(pf);
11049 }
11050 
11051 /**
11052  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11053  * @vsi: the VSI being configured
11054  **/
11055 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11056 {
11057 	struct i40e_pf *pf = vsi->back;
11058 
11059 	switch (vsi->type) {
11060 	case I40E_VSI_MAIN:
11061 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11062 		if (!vsi->num_tx_desc)
11063 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11064 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11065 		if (!vsi->num_rx_desc)
11066 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11067 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11068 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11069 			vsi->num_q_vectors = pf->num_lan_msix;
11070 		else
11071 			vsi->num_q_vectors = 1;
11072 
11073 		break;
11074 
11075 	case I40E_VSI_FDIR:
11076 		vsi->alloc_queue_pairs = 1;
11077 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11078 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11079 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11080 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11081 		vsi->num_q_vectors = pf->num_fdsb_msix;
11082 		break;
11083 
11084 	case I40E_VSI_VMDQ2:
11085 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11086 		if (!vsi->num_tx_desc)
11087 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11088 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11089 		if (!vsi->num_rx_desc)
11090 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11091 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11092 		vsi->num_q_vectors = pf->num_vmdq_msix;
11093 		break;
11094 
11095 	case I40E_VSI_SRIOV:
11096 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11097 		if (!vsi->num_tx_desc)
11098 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11099 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11100 		if (!vsi->num_rx_desc)
11101 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11102 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11103 		break;
11104 
11105 	default:
11106 		WARN_ON(1);
11107 		return -ENODATA;
11108 	}
11109 
11110 	if (is_kdump_kernel()) {
11111 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11112 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11113 	}
11114 
11115 	return 0;
11116 }
11117 
11118 /**
11119  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11120  * @vsi: VSI pointer
11121  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11122  *
11123  * On error: returns error code (negative)
11124  * On success: returns 0
11125  **/
11126 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11127 {
11128 	struct i40e_ring **next_rings;
11129 	int size;
11130 	int ret = 0;
11131 
11132 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11133 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11134 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11135 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11136 	if (!vsi->tx_rings)
11137 		return -ENOMEM;
11138 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11139 	if (i40e_enabled_xdp_vsi(vsi)) {
11140 		vsi->xdp_rings = next_rings;
11141 		next_rings += vsi->alloc_queue_pairs;
11142 	}
11143 	vsi->rx_rings = next_rings;
11144 
11145 	if (alloc_qvectors) {
11146 		/* allocate memory for q_vector pointers */
11147 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11148 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11149 		if (!vsi->q_vectors) {
11150 			ret = -ENOMEM;
11151 			goto err_vectors;
11152 		}
11153 	}
11154 	return ret;
11155 
11156 err_vectors:
11157 	kfree(vsi->tx_rings);
11158 	return ret;
11159 }
11160 
11161 /**
11162  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11163  * @pf: board private structure
11164  * @type: type of VSI
11165  *
11166  * On error: returns error code (negative)
11167  * On success: returns vsi index in PF (positive)
11168  **/
11169 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11170 {
11171 	int ret = -ENODEV;
11172 	struct i40e_vsi *vsi;
11173 	int vsi_idx;
11174 	int i;
11175 
11176 	/* Need to protect the allocation of the VSIs at the PF level */
11177 	mutex_lock(&pf->switch_mutex);
11178 
11179 	/* VSI list may be fragmented if VSI creation/destruction has
11180 	 * been happening.  We can afford to do a quick scan to look
11181 	 * for any free VSIs in the list.
11182 	 *
11183 	 * find next empty vsi slot, looping back around if necessary
11184 	 */
11185 	i = pf->next_vsi;
11186 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11187 		i++;
11188 	if (i >= pf->num_alloc_vsi) {
11189 		i = 0;
11190 		while (i < pf->next_vsi && pf->vsi[i])
11191 			i++;
11192 	}
11193 
11194 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11195 		vsi_idx = i;             /* Found one! */
11196 	} else {
11197 		ret = -ENODEV;
11198 		goto unlock_pf;  /* out of VSI slots! */
11199 	}
11200 	pf->next_vsi = ++i;
11201 
11202 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11203 	if (!vsi) {
11204 		ret = -ENOMEM;
11205 		goto unlock_pf;
11206 	}
11207 	vsi->type = type;
11208 	vsi->back = pf;
11209 	set_bit(__I40E_VSI_DOWN, vsi->state);
11210 	vsi->flags = 0;
11211 	vsi->idx = vsi_idx;
11212 	vsi->int_rate_limit = 0;
11213 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11214 				pf->rss_table_size : 64;
11215 	vsi->netdev_registered = false;
11216 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11217 	hash_init(vsi->mac_filter_hash);
11218 	vsi->irqs_ready = false;
11219 
11220 	if (type == I40E_VSI_MAIN) {
11221 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11222 		if (!vsi->af_xdp_zc_qps)
11223 			goto err_rings;
11224 	}
11225 
11226 	ret = i40e_set_num_rings_in_vsi(vsi);
11227 	if (ret)
11228 		goto err_rings;
11229 
11230 	ret = i40e_vsi_alloc_arrays(vsi, true);
11231 	if (ret)
11232 		goto err_rings;
11233 
11234 	/* Setup default MSIX irq handler for VSI */
11235 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11236 
11237 	/* Initialize VSI lock */
11238 	spin_lock_init(&vsi->mac_filter_hash_lock);
11239 	pf->vsi[vsi_idx] = vsi;
11240 	ret = vsi_idx;
11241 	goto unlock_pf;
11242 
11243 err_rings:
11244 	bitmap_free(vsi->af_xdp_zc_qps);
11245 	pf->next_vsi = i - 1;
11246 	kfree(vsi);
11247 unlock_pf:
11248 	mutex_unlock(&pf->switch_mutex);
11249 	return ret;
11250 }
11251 
11252 /**
11253  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11254  * @vsi: VSI pointer
11255  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11256  *
11257  * On error: returns error code (negative)
11258  * On success: returns 0
11259  **/
11260 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11261 {
11262 	/* free the ring and vector containers */
11263 	if (free_qvectors) {
11264 		kfree(vsi->q_vectors);
11265 		vsi->q_vectors = NULL;
11266 	}
11267 	kfree(vsi->tx_rings);
11268 	vsi->tx_rings = NULL;
11269 	vsi->rx_rings = NULL;
11270 	vsi->xdp_rings = NULL;
11271 }
11272 
11273 /**
11274  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11275  * and lookup table
11276  * @vsi: Pointer to VSI structure
11277  */
11278 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11279 {
11280 	if (!vsi)
11281 		return;
11282 
11283 	kfree(vsi->rss_hkey_user);
11284 	vsi->rss_hkey_user = NULL;
11285 
11286 	kfree(vsi->rss_lut_user);
11287 	vsi->rss_lut_user = NULL;
11288 }
11289 
11290 /**
11291  * i40e_vsi_clear - Deallocate the VSI provided
11292  * @vsi: the VSI being un-configured
11293  **/
11294 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11295 {
11296 	struct i40e_pf *pf;
11297 
11298 	if (!vsi)
11299 		return 0;
11300 
11301 	if (!vsi->back)
11302 		goto free_vsi;
11303 	pf = vsi->back;
11304 
11305 	mutex_lock(&pf->switch_mutex);
11306 	if (!pf->vsi[vsi->idx]) {
11307 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11308 			vsi->idx, vsi->idx, vsi->type);
11309 		goto unlock_vsi;
11310 	}
11311 
11312 	if (pf->vsi[vsi->idx] != vsi) {
11313 		dev_err(&pf->pdev->dev,
11314 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11315 			pf->vsi[vsi->idx]->idx,
11316 			pf->vsi[vsi->idx]->type,
11317 			vsi->idx, vsi->type);
11318 		goto unlock_vsi;
11319 	}
11320 
11321 	/* updates the PF for this cleared vsi */
11322 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11323 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11324 
11325 	bitmap_free(vsi->af_xdp_zc_qps);
11326 	i40e_vsi_free_arrays(vsi, true);
11327 	i40e_clear_rss_config_user(vsi);
11328 
11329 	pf->vsi[vsi->idx] = NULL;
11330 	if (vsi->idx < pf->next_vsi)
11331 		pf->next_vsi = vsi->idx;
11332 
11333 unlock_vsi:
11334 	mutex_unlock(&pf->switch_mutex);
11335 free_vsi:
11336 	kfree(vsi);
11337 
11338 	return 0;
11339 }
11340 
11341 /**
11342  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11343  * @vsi: the VSI being cleaned
11344  **/
11345 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11346 {
11347 	int i;
11348 
11349 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11350 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11351 			kfree_rcu(vsi->tx_rings[i], rcu);
11352 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11353 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11354 			if (vsi->xdp_rings)
11355 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11356 		}
11357 	}
11358 }
11359 
11360 /**
11361  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11362  * @vsi: the VSI being configured
11363  **/
11364 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11365 {
11366 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11367 	struct i40e_pf *pf = vsi->back;
11368 	struct i40e_ring *ring;
11369 
11370 	/* Set basic values in the rings to be used later during open() */
11371 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11372 		/* allocate space for both Tx and Rx in one shot */
11373 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11374 		if (!ring)
11375 			goto err_out;
11376 
11377 		ring->queue_index = i;
11378 		ring->reg_idx = vsi->base_queue + i;
11379 		ring->ring_active = false;
11380 		ring->vsi = vsi;
11381 		ring->netdev = vsi->netdev;
11382 		ring->dev = &pf->pdev->dev;
11383 		ring->count = vsi->num_tx_desc;
11384 		ring->size = 0;
11385 		ring->dcb_tc = 0;
11386 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11387 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11388 		ring->itr_setting = pf->tx_itr_default;
11389 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11390 
11391 		if (!i40e_enabled_xdp_vsi(vsi))
11392 			goto setup_rx;
11393 
11394 		ring->queue_index = vsi->alloc_queue_pairs + i;
11395 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11396 		ring->ring_active = false;
11397 		ring->vsi = vsi;
11398 		ring->netdev = NULL;
11399 		ring->dev = &pf->pdev->dev;
11400 		ring->count = vsi->num_tx_desc;
11401 		ring->size = 0;
11402 		ring->dcb_tc = 0;
11403 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11404 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11405 		set_ring_xdp(ring);
11406 		ring->itr_setting = pf->tx_itr_default;
11407 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11408 
11409 setup_rx:
11410 		ring->queue_index = i;
11411 		ring->reg_idx = vsi->base_queue + i;
11412 		ring->ring_active = false;
11413 		ring->vsi = vsi;
11414 		ring->netdev = vsi->netdev;
11415 		ring->dev = &pf->pdev->dev;
11416 		ring->count = vsi->num_rx_desc;
11417 		ring->size = 0;
11418 		ring->dcb_tc = 0;
11419 		ring->itr_setting = pf->rx_itr_default;
11420 		WRITE_ONCE(vsi->rx_rings[i], ring);
11421 	}
11422 
11423 	return 0;
11424 
11425 err_out:
11426 	i40e_vsi_clear_rings(vsi);
11427 	return -ENOMEM;
11428 }
11429 
11430 /**
11431  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11432  * @pf: board private structure
11433  * @vectors: the number of MSI-X vectors to request
11434  *
11435  * Returns the number of vectors reserved, or error
11436  **/
11437 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11438 {
11439 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11440 					I40E_MIN_MSIX, vectors);
11441 	if (vectors < 0) {
11442 		dev_info(&pf->pdev->dev,
11443 			 "MSI-X vector reservation failed: %d\n", vectors);
11444 		vectors = 0;
11445 	}
11446 
11447 	return vectors;
11448 }
11449 
11450 /**
11451  * i40e_init_msix - Setup the MSIX capability
11452  * @pf: board private structure
11453  *
11454  * Work with the OS to set up the MSIX vectors needed.
11455  *
11456  * Returns the number of vectors reserved or negative on failure
11457  **/
11458 static int i40e_init_msix(struct i40e_pf *pf)
11459 {
11460 	struct i40e_hw *hw = &pf->hw;
11461 	int cpus, extra_vectors;
11462 	int vectors_left;
11463 	int v_budget, i;
11464 	int v_actual;
11465 	int iwarp_requested = 0;
11466 
11467 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11468 		return -ENODEV;
11469 
11470 	/* The number of vectors we'll request will be comprised of:
11471 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11472 	 *   - The number of LAN queue pairs
11473 	 *	- Queues being used for RSS.
11474 	 *		We don't need as many as max_rss_size vectors.
11475 	 *		use rss_size instead in the calculation since that
11476 	 *		is governed by number of cpus in the system.
11477 	 *	- assumes symmetric Tx/Rx pairing
11478 	 *   - The number of VMDq pairs
11479 	 *   - The CPU count within the NUMA node if iWARP is enabled
11480 	 * Once we count this up, try the request.
11481 	 *
11482 	 * If we can't get what we want, we'll simplify to nearly nothing
11483 	 * and try again.  If that still fails, we punt.
11484 	 */
11485 	vectors_left = hw->func_caps.num_msix_vectors;
11486 	v_budget = 0;
11487 
11488 	/* reserve one vector for miscellaneous handler */
11489 	if (vectors_left) {
11490 		v_budget++;
11491 		vectors_left--;
11492 	}
11493 
11494 	/* reserve some vectors for the main PF traffic queues. Initially we
11495 	 * only reserve at most 50% of the available vectors, in the case that
11496 	 * the number of online CPUs is large. This ensures that we can enable
11497 	 * extra features as well. Once we've enabled the other features, we
11498 	 * will use any remaining vectors to reach as close as we can to the
11499 	 * number of online CPUs.
11500 	 */
11501 	cpus = num_online_cpus();
11502 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11503 	vectors_left -= pf->num_lan_msix;
11504 
11505 	/* reserve one vector for sideband flow director */
11506 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11507 		if (vectors_left) {
11508 			pf->num_fdsb_msix = 1;
11509 			v_budget++;
11510 			vectors_left--;
11511 		} else {
11512 			pf->num_fdsb_msix = 0;
11513 		}
11514 	}
11515 
11516 	/* can we reserve enough for iWARP? */
11517 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11518 		iwarp_requested = pf->num_iwarp_msix;
11519 
11520 		if (!vectors_left)
11521 			pf->num_iwarp_msix = 0;
11522 		else if (vectors_left < pf->num_iwarp_msix)
11523 			pf->num_iwarp_msix = 1;
11524 		v_budget += pf->num_iwarp_msix;
11525 		vectors_left -= pf->num_iwarp_msix;
11526 	}
11527 
11528 	/* any vectors left over go for VMDq support */
11529 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11530 		if (!vectors_left) {
11531 			pf->num_vmdq_msix = 0;
11532 			pf->num_vmdq_qps = 0;
11533 		} else {
11534 			int vmdq_vecs_wanted =
11535 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11536 			int vmdq_vecs =
11537 				min_t(int, vectors_left, vmdq_vecs_wanted);
11538 
11539 			/* if we're short on vectors for what's desired, we limit
11540 			 * the queues per vmdq.  If this is still more than are
11541 			 * available, the user will need to change the number of
11542 			 * queues/vectors used by the PF later with the ethtool
11543 			 * channels command
11544 			 */
11545 			if (vectors_left < vmdq_vecs_wanted) {
11546 				pf->num_vmdq_qps = 1;
11547 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11548 				vmdq_vecs = min_t(int,
11549 						  vectors_left,
11550 						  vmdq_vecs_wanted);
11551 			}
11552 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11553 
11554 			v_budget += vmdq_vecs;
11555 			vectors_left -= vmdq_vecs;
11556 		}
11557 	}
11558 
11559 	/* On systems with a large number of SMP cores, we previously limited
11560 	 * the number of vectors for num_lan_msix to be at most 50% of the
11561 	 * available vectors, to allow for other features. Now, we add back
11562 	 * the remaining vectors. However, we ensure that the total
11563 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11564 	 * calculate the number of vectors we can add without going over the
11565 	 * cap of CPUs. For systems with a small number of CPUs this will be
11566 	 * zero.
11567 	 */
11568 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11569 	pf->num_lan_msix += extra_vectors;
11570 	vectors_left -= extra_vectors;
11571 
11572 	WARN(vectors_left < 0,
11573 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11574 
11575 	v_budget += pf->num_lan_msix;
11576 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11577 				   GFP_KERNEL);
11578 	if (!pf->msix_entries)
11579 		return -ENOMEM;
11580 
11581 	for (i = 0; i < v_budget; i++)
11582 		pf->msix_entries[i].entry = i;
11583 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11584 
11585 	if (v_actual < I40E_MIN_MSIX) {
11586 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11587 		kfree(pf->msix_entries);
11588 		pf->msix_entries = NULL;
11589 		pci_disable_msix(pf->pdev);
11590 		return -ENODEV;
11591 
11592 	} else if (v_actual == I40E_MIN_MSIX) {
11593 		/* Adjust for minimal MSIX use */
11594 		pf->num_vmdq_vsis = 0;
11595 		pf->num_vmdq_qps = 0;
11596 		pf->num_lan_qps = 1;
11597 		pf->num_lan_msix = 1;
11598 
11599 	} else if (v_actual != v_budget) {
11600 		/* If we have limited resources, we will start with no vectors
11601 		 * for the special features and then allocate vectors to some
11602 		 * of these features based on the policy and at the end disable
11603 		 * the features that did not get any vectors.
11604 		 */
11605 		int vec;
11606 
11607 		dev_info(&pf->pdev->dev,
11608 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11609 			 v_actual, v_budget);
11610 		/* reserve the misc vector */
11611 		vec = v_actual - 1;
11612 
11613 		/* Scale vector usage down */
11614 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11615 		pf->num_vmdq_vsis = 1;
11616 		pf->num_vmdq_qps = 1;
11617 
11618 		/* partition out the remaining vectors */
11619 		switch (vec) {
11620 		case 2:
11621 			pf->num_lan_msix = 1;
11622 			break;
11623 		case 3:
11624 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11625 				pf->num_lan_msix = 1;
11626 				pf->num_iwarp_msix = 1;
11627 			} else {
11628 				pf->num_lan_msix = 2;
11629 			}
11630 			break;
11631 		default:
11632 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11633 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11634 						 iwarp_requested);
11635 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11636 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11637 			} else {
11638 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11639 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11640 			}
11641 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11642 				pf->num_fdsb_msix = 1;
11643 				vec--;
11644 			}
11645 			pf->num_lan_msix = min_t(int,
11646 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11647 							      pf->num_lan_msix);
11648 			pf->num_lan_qps = pf->num_lan_msix;
11649 			break;
11650 		}
11651 	}
11652 
11653 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11654 	    (pf->num_fdsb_msix == 0)) {
11655 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11656 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11657 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11658 	}
11659 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11660 	    (pf->num_vmdq_msix == 0)) {
11661 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11662 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11663 	}
11664 
11665 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11666 	    (pf->num_iwarp_msix == 0)) {
11667 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11668 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11669 	}
11670 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11671 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11672 		   pf->num_lan_msix,
11673 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11674 		   pf->num_fdsb_msix,
11675 		   pf->num_iwarp_msix);
11676 
11677 	return v_actual;
11678 }
11679 
11680 /**
11681  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11682  * @vsi: the VSI being configured
11683  * @v_idx: index of the vector in the vsi struct
11684  *
11685  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11686  **/
11687 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11688 {
11689 	struct i40e_q_vector *q_vector;
11690 
11691 	/* allocate q_vector */
11692 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11693 	if (!q_vector)
11694 		return -ENOMEM;
11695 
11696 	q_vector->vsi = vsi;
11697 	q_vector->v_idx = v_idx;
11698 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11699 
11700 	if (vsi->netdev)
11701 		netif_napi_add(vsi->netdev, &q_vector->napi,
11702 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11703 
11704 	/* tie q_vector and vsi together */
11705 	vsi->q_vectors[v_idx] = q_vector;
11706 
11707 	return 0;
11708 }
11709 
11710 /**
11711  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11712  * @vsi: the VSI being configured
11713  *
11714  * We allocate one q_vector per queue interrupt.  If allocation fails we
11715  * return -ENOMEM.
11716  **/
11717 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11718 {
11719 	struct i40e_pf *pf = vsi->back;
11720 	int err, v_idx, num_q_vectors;
11721 
11722 	/* if not MSIX, give the one vector only to the LAN VSI */
11723 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11724 		num_q_vectors = vsi->num_q_vectors;
11725 	else if (vsi == pf->vsi[pf->lan_vsi])
11726 		num_q_vectors = 1;
11727 	else
11728 		return -EINVAL;
11729 
11730 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11731 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11732 		if (err)
11733 			goto err_out;
11734 	}
11735 
11736 	return 0;
11737 
11738 err_out:
11739 	while (v_idx--)
11740 		i40e_free_q_vector(vsi, v_idx);
11741 
11742 	return err;
11743 }
11744 
11745 /**
11746  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11747  * @pf: board private structure to initialize
11748  **/
11749 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11750 {
11751 	int vectors = 0;
11752 	ssize_t size;
11753 
11754 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11755 		vectors = i40e_init_msix(pf);
11756 		if (vectors < 0) {
11757 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11758 				       I40E_FLAG_IWARP_ENABLED	|
11759 				       I40E_FLAG_RSS_ENABLED	|
11760 				       I40E_FLAG_DCB_CAPABLE	|
11761 				       I40E_FLAG_DCB_ENABLED	|
11762 				       I40E_FLAG_SRIOV_ENABLED	|
11763 				       I40E_FLAG_FD_SB_ENABLED	|
11764 				       I40E_FLAG_FD_ATR_ENABLED	|
11765 				       I40E_FLAG_VMDQ_ENABLED);
11766 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11767 
11768 			/* rework the queue expectations without MSIX */
11769 			i40e_determine_queue_usage(pf);
11770 		}
11771 	}
11772 
11773 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11774 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11775 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11776 		vectors = pci_enable_msi(pf->pdev);
11777 		if (vectors < 0) {
11778 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11779 				 vectors);
11780 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11781 		}
11782 		vectors = 1;  /* one MSI or Legacy vector */
11783 	}
11784 
11785 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11786 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11787 
11788 	/* set up vector assignment tracking */
11789 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11790 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11791 	if (!pf->irq_pile)
11792 		return -ENOMEM;
11793 
11794 	pf->irq_pile->num_entries = vectors;
11795 
11796 	/* track first vector for misc interrupts, ignore return */
11797 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11798 
11799 	return 0;
11800 }
11801 
11802 /**
11803  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11804  * @pf: private board data structure
11805  *
11806  * Restore the interrupt scheme that was cleared when we suspended the
11807  * device. This should be called during resume to re-allocate the q_vectors
11808  * and reacquire IRQs.
11809  */
11810 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11811 {
11812 	int err, i;
11813 
11814 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11815 	 * scheme. We need to re-enabled them here in order to attempt to
11816 	 * re-acquire the MSI or MSI-X vectors
11817 	 */
11818 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11819 
11820 	err = i40e_init_interrupt_scheme(pf);
11821 	if (err)
11822 		return err;
11823 
11824 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11825 	 * rings together again.
11826 	 */
11827 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11828 		if (pf->vsi[i]) {
11829 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11830 			if (err)
11831 				goto err_unwind;
11832 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11833 		}
11834 	}
11835 
11836 	err = i40e_setup_misc_vector(pf);
11837 	if (err)
11838 		goto err_unwind;
11839 
11840 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11841 		i40e_client_update_msix_info(pf);
11842 
11843 	return 0;
11844 
11845 err_unwind:
11846 	while (i--) {
11847 		if (pf->vsi[i])
11848 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11849 	}
11850 
11851 	return err;
11852 }
11853 
11854 /**
11855  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11856  * non queue events in recovery mode
11857  * @pf: board private structure
11858  *
11859  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11860  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11861  * This is handled differently than in recovery mode since no Tx/Rx resources
11862  * are being allocated.
11863  **/
11864 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11865 {
11866 	int err;
11867 
11868 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11869 		err = i40e_setup_misc_vector(pf);
11870 
11871 		if (err) {
11872 			dev_info(&pf->pdev->dev,
11873 				 "MSI-X misc vector request failed, error %d\n",
11874 				 err);
11875 			return err;
11876 		}
11877 	} else {
11878 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11879 
11880 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11881 				  pf->int_name, pf);
11882 
11883 		if (err) {
11884 			dev_info(&pf->pdev->dev,
11885 				 "MSI/legacy misc vector request failed, error %d\n",
11886 				 err);
11887 			return err;
11888 		}
11889 		i40e_enable_misc_int_causes(pf);
11890 		i40e_irq_dynamic_enable_icr0(pf);
11891 	}
11892 
11893 	return 0;
11894 }
11895 
11896 /**
11897  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11898  * @pf: board private structure
11899  *
11900  * This sets up the handler for MSIX 0, which is used to manage the
11901  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11902  * when in MSI or Legacy interrupt mode.
11903  **/
11904 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11905 {
11906 	struct i40e_hw *hw = &pf->hw;
11907 	int err = 0;
11908 
11909 	/* Only request the IRQ once, the first time through. */
11910 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11911 		err = request_irq(pf->msix_entries[0].vector,
11912 				  i40e_intr, 0, pf->int_name, pf);
11913 		if (err) {
11914 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11915 			dev_info(&pf->pdev->dev,
11916 				 "request_irq for %s failed: %d\n",
11917 				 pf->int_name, err);
11918 			return -EFAULT;
11919 		}
11920 	}
11921 
11922 	i40e_enable_misc_int_causes(pf);
11923 
11924 	/* associate no queues to the misc vector */
11925 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11926 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11927 
11928 	i40e_flush(hw);
11929 
11930 	i40e_irq_dynamic_enable_icr0(pf);
11931 
11932 	return err;
11933 }
11934 
11935 /**
11936  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11937  * @vsi: Pointer to vsi structure
11938  * @seed: Buffter to store the hash keys
11939  * @lut: Buffer to store the lookup table entries
11940  * @lut_size: Size of buffer to store the lookup table entries
11941  *
11942  * Return 0 on success, negative on failure
11943  */
11944 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11945 			   u8 *lut, u16 lut_size)
11946 {
11947 	struct i40e_pf *pf = vsi->back;
11948 	struct i40e_hw *hw = &pf->hw;
11949 	int ret = 0;
11950 
11951 	if (seed) {
11952 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11953 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11954 		if (ret) {
11955 			dev_info(&pf->pdev->dev,
11956 				 "Cannot get RSS key, err %s aq_err %s\n",
11957 				 i40e_stat_str(&pf->hw, ret),
11958 				 i40e_aq_str(&pf->hw,
11959 					     pf->hw.aq.asq_last_status));
11960 			return ret;
11961 		}
11962 	}
11963 
11964 	if (lut) {
11965 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11966 
11967 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11968 		if (ret) {
11969 			dev_info(&pf->pdev->dev,
11970 				 "Cannot get RSS lut, err %s aq_err %s\n",
11971 				 i40e_stat_str(&pf->hw, ret),
11972 				 i40e_aq_str(&pf->hw,
11973 					     pf->hw.aq.asq_last_status));
11974 			return ret;
11975 		}
11976 	}
11977 
11978 	return ret;
11979 }
11980 
11981 /**
11982  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11983  * @vsi: Pointer to vsi structure
11984  * @seed: RSS hash seed
11985  * @lut: Lookup table
11986  * @lut_size: Lookup table size
11987  *
11988  * Returns 0 on success, negative on failure
11989  **/
11990 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11991 			       const u8 *lut, u16 lut_size)
11992 {
11993 	struct i40e_pf *pf = vsi->back;
11994 	struct i40e_hw *hw = &pf->hw;
11995 	u16 vf_id = vsi->vf_id;
11996 	u8 i;
11997 
11998 	/* Fill out hash function seed */
11999 	if (seed) {
12000 		u32 *seed_dw = (u32 *)seed;
12001 
12002 		if (vsi->type == I40E_VSI_MAIN) {
12003 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12004 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12005 		} else if (vsi->type == I40E_VSI_SRIOV) {
12006 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12007 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12008 		} else {
12009 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12010 		}
12011 	}
12012 
12013 	if (lut) {
12014 		u32 *lut_dw = (u32 *)lut;
12015 
12016 		if (vsi->type == I40E_VSI_MAIN) {
12017 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12018 				return -EINVAL;
12019 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12020 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12021 		} else if (vsi->type == I40E_VSI_SRIOV) {
12022 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12023 				return -EINVAL;
12024 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12025 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12026 		} else {
12027 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12028 		}
12029 	}
12030 	i40e_flush(hw);
12031 
12032 	return 0;
12033 }
12034 
12035 /**
12036  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12037  * @vsi: Pointer to VSI structure
12038  * @seed: Buffer to store the keys
12039  * @lut: Buffer to store the lookup table entries
12040  * @lut_size: Size of buffer to store the lookup table entries
12041  *
12042  * Returns 0 on success, negative on failure
12043  */
12044 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12045 			    u8 *lut, u16 lut_size)
12046 {
12047 	struct i40e_pf *pf = vsi->back;
12048 	struct i40e_hw *hw = &pf->hw;
12049 	u16 i;
12050 
12051 	if (seed) {
12052 		u32 *seed_dw = (u32 *)seed;
12053 
12054 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12055 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12056 	}
12057 	if (lut) {
12058 		u32 *lut_dw = (u32 *)lut;
12059 
12060 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12061 			return -EINVAL;
12062 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12063 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12064 	}
12065 
12066 	return 0;
12067 }
12068 
12069 /**
12070  * i40e_config_rss - Configure RSS keys and lut
12071  * @vsi: Pointer to VSI structure
12072  * @seed: RSS hash seed
12073  * @lut: Lookup table
12074  * @lut_size: Lookup table size
12075  *
12076  * Returns 0 on success, negative on failure
12077  */
12078 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12079 {
12080 	struct i40e_pf *pf = vsi->back;
12081 
12082 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12083 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12084 	else
12085 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12086 }
12087 
12088 /**
12089  * i40e_get_rss - Get RSS keys and lut
12090  * @vsi: Pointer to VSI structure
12091  * @seed: Buffer to store the keys
12092  * @lut: Buffer to store the lookup table entries
12093  * @lut_size: Size of buffer to store the lookup table entries
12094  *
12095  * Returns 0 on success, negative on failure
12096  */
12097 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12098 {
12099 	struct i40e_pf *pf = vsi->back;
12100 
12101 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12102 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12103 	else
12104 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12105 }
12106 
12107 /**
12108  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12109  * @pf: Pointer to board private structure
12110  * @lut: Lookup table
12111  * @rss_table_size: Lookup table size
12112  * @rss_size: Range of queue number for hashing
12113  */
12114 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12115 		       u16 rss_table_size, u16 rss_size)
12116 {
12117 	u16 i;
12118 
12119 	for (i = 0; i < rss_table_size; i++)
12120 		lut[i] = i % rss_size;
12121 }
12122 
12123 /**
12124  * i40e_pf_config_rss - Prepare for RSS if used
12125  * @pf: board private structure
12126  **/
12127 static int i40e_pf_config_rss(struct i40e_pf *pf)
12128 {
12129 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12130 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12131 	u8 *lut;
12132 	struct i40e_hw *hw = &pf->hw;
12133 	u32 reg_val;
12134 	u64 hena;
12135 	int ret;
12136 
12137 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12138 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12139 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12140 	hena |= i40e_pf_get_default_rss_hena(pf);
12141 
12142 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12143 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12144 
12145 	/* Determine the RSS table size based on the hardware capabilities */
12146 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12147 	reg_val = (pf->rss_table_size == 512) ?
12148 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12149 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12150 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12151 
12152 	/* Determine the RSS size of the VSI */
12153 	if (!vsi->rss_size) {
12154 		u16 qcount;
12155 		/* If the firmware does something weird during VSI init, we
12156 		 * could end up with zero TCs. Check for that to avoid
12157 		 * divide-by-zero. It probably won't pass traffic, but it also
12158 		 * won't panic.
12159 		 */
12160 		qcount = vsi->num_queue_pairs /
12161 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12162 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12163 	}
12164 	if (!vsi->rss_size)
12165 		return -EINVAL;
12166 
12167 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12168 	if (!lut)
12169 		return -ENOMEM;
12170 
12171 	/* Use user configured lut if there is one, otherwise use default */
12172 	if (vsi->rss_lut_user)
12173 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12174 	else
12175 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12176 
12177 	/* Use user configured hash key if there is one, otherwise
12178 	 * use default.
12179 	 */
12180 	if (vsi->rss_hkey_user)
12181 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12182 	else
12183 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12184 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12185 	kfree(lut);
12186 
12187 	return ret;
12188 }
12189 
12190 /**
12191  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12192  * @pf: board private structure
12193  * @queue_count: the requested queue count for rss.
12194  *
12195  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12196  * count which may be different from the requested queue count.
12197  * Note: expects to be called while under rtnl_lock()
12198  **/
12199 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12200 {
12201 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12202 	int new_rss_size;
12203 
12204 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12205 		return 0;
12206 
12207 	queue_count = min_t(int, queue_count, num_online_cpus());
12208 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12209 
12210 	if (queue_count != vsi->num_queue_pairs) {
12211 		u16 qcount;
12212 
12213 		vsi->req_queue_pairs = queue_count;
12214 		i40e_prep_for_reset(pf);
12215 
12216 		pf->alloc_rss_size = new_rss_size;
12217 
12218 		i40e_reset_and_rebuild(pf, true, true);
12219 
12220 		/* Discard the user configured hash keys and lut, if less
12221 		 * queues are enabled.
12222 		 */
12223 		if (queue_count < vsi->rss_size) {
12224 			i40e_clear_rss_config_user(vsi);
12225 			dev_dbg(&pf->pdev->dev,
12226 				"discard user configured hash keys and lut\n");
12227 		}
12228 
12229 		/* Reset vsi->rss_size, as number of enabled queues changed */
12230 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12231 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12232 
12233 		i40e_pf_config_rss(pf);
12234 	}
12235 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12236 		 vsi->req_queue_pairs, pf->rss_size_max);
12237 	return pf->alloc_rss_size;
12238 }
12239 
12240 /**
12241  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12242  * @pf: board private structure
12243  **/
12244 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12245 {
12246 	i40e_status status;
12247 	bool min_valid, max_valid;
12248 	u32 max_bw, min_bw;
12249 
12250 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12251 					   &min_valid, &max_valid);
12252 
12253 	if (!status) {
12254 		if (min_valid)
12255 			pf->min_bw = min_bw;
12256 		if (max_valid)
12257 			pf->max_bw = max_bw;
12258 	}
12259 
12260 	return status;
12261 }
12262 
12263 /**
12264  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12265  * @pf: board private structure
12266  **/
12267 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12268 {
12269 	struct i40e_aqc_configure_partition_bw_data bw_data;
12270 	i40e_status status;
12271 
12272 	memset(&bw_data, 0, sizeof(bw_data));
12273 
12274 	/* Set the valid bit for this PF */
12275 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12276 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12277 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12278 
12279 	/* Set the new bandwidths */
12280 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12281 
12282 	return status;
12283 }
12284 
12285 /**
12286  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12287  * @pf: board private structure
12288  **/
12289 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12290 {
12291 	/* Commit temporary BW setting to permanent NVM image */
12292 	enum i40e_admin_queue_err last_aq_status;
12293 	i40e_status ret;
12294 	u16 nvm_word;
12295 
12296 	if (pf->hw.partition_id != 1) {
12297 		dev_info(&pf->pdev->dev,
12298 			 "Commit BW only works on partition 1! This is partition %d",
12299 			 pf->hw.partition_id);
12300 		ret = I40E_NOT_SUPPORTED;
12301 		goto bw_commit_out;
12302 	}
12303 
12304 	/* Acquire NVM for read access */
12305 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12306 	last_aq_status = pf->hw.aq.asq_last_status;
12307 	if (ret) {
12308 		dev_info(&pf->pdev->dev,
12309 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12310 			 i40e_stat_str(&pf->hw, ret),
12311 			 i40e_aq_str(&pf->hw, last_aq_status));
12312 		goto bw_commit_out;
12313 	}
12314 
12315 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12316 	ret = i40e_aq_read_nvm(&pf->hw,
12317 			       I40E_SR_NVM_CONTROL_WORD,
12318 			       0x10, sizeof(nvm_word), &nvm_word,
12319 			       false, NULL);
12320 	/* Save off last admin queue command status before releasing
12321 	 * the NVM
12322 	 */
12323 	last_aq_status = pf->hw.aq.asq_last_status;
12324 	i40e_release_nvm(&pf->hw);
12325 	if (ret) {
12326 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12327 			 i40e_stat_str(&pf->hw, ret),
12328 			 i40e_aq_str(&pf->hw, last_aq_status));
12329 		goto bw_commit_out;
12330 	}
12331 
12332 	/* Wait a bit for NVM release to complete */
12333 	msleep(50);
12334 
12335 	/* Acquire NVM for write access */
12336 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12337 	last_aq_status = pf->hw.aq.asq_last_status;
12338 	if (ret) {
12339 		dev_info(&pf->pdev->dev,
12340 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12341 			 i40e_stat_str(&pf->hw, ret),
12342 			 i40e_aq_str(&pf->hw, last_aq_status));
12343 		goto bw_commit_out;
12344 	}
12345 	/* Write it back out unchanged to initiate update NVM,
12346 	 * which will force a write of the shadow (alt) RAM to
12347 	 * the NVM - thus storing the bandwidth values permanently.
12348 	 */
12349 	ret = i40e_aq_update_nvm(&pf->hw,
12350 				 I40E_SR_NVM_CONTROL_WORD,
12351 				 0x10, sizeof(nvm_word),
12352 				 &nvm_word, true, 0, NULL);
12353 	/* Save off last admin queue command status before releasing
12354 	 * the NVM
12355 	 */
12356 	last_aq_status = pf->hw.aq.asq_last_status;
12357 	i40e_release_nvm(&pf->hw);
12358 	if (ret)
12359 		dev_info(&pf->pdev->dev,
12360 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12361 			 i40e_stat_str(&pf->hw, ret),
12362 			 i40e_aq_str(&pf->hw, last_aq_status));
12363 bw_commit_out:
12364 
12365 	return ret;
12366 }
12367 
12368 /**
12369  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12370  * if total port shutdown feature is enabled for this PF
12371  * @pf: board private structure
12372  **/
12373 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12374 {
12375 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12376 #define I40E_FEATURES_ENABLE_PTR		0x2A
12377 #define I40E_CURRENT_SETTING_PTR		0x2B
12378 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12379 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12380 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12381 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12382 	i40e_status read_status = I40E_SUCCESS;
12383 	u16 sr_emp_sr_settings_ptr = 0;
12384 	u16 features_enable = 0;
12385 	u16 link_behavior = 0;
12386 	bool ret = false;
12387 
12388 	read_status = i40e_read_nvm_word(&pf->hw,
12389 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12390 					 &sr_emp_sr_settings_ptr);
12391 	if (read_status)
12392 		goto err_nvm;
12393 	read_status = i40e_read_nvm_word(&pf->hw,
12394 					 sr_emp_sr_settings_ptr +
12395 					 I40E_FEATURES_ENABLE_PTR,
12396 					 &features_enable);
12397 	if (read_status)
12398 		goto err_nvm;
12399 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12400 		read_status = i40e_read_nvm_module_data(&pf->hw,
12401 							I40E_SR_EMP_SR_SETTINGS_PTR,
12402 							I40E_CURRENT_SETTING_PTR,
12403 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12404 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12405 							&link_behavior);
12406 		if (read_status)
12407 			goto err_nvm;
12408 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12409 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12410 	}
12411 	return ret;
12412 
12413 err_nvm:
12414 	dev_warn(&pf->pdev->dev,
12415 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12416 		 i40e_stat_str(&pf->hw, read_status));
12417 	return ret;
12418 }
12419 
12420 /**
12421  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12422  * @pf: board private structure to initialize
12423  *
12424  * i40e_sw_init initializes the Adapter private data structure.
12425  * Fields are initialized based on PCI device information and
12426  * OS network device settings (MTU size).
12427  **/
12428 static int i40e_sw_init(struct i40e_pf *pf)
12429 {
12430 	int err = 0;
12431 	int size;
12432 	u16 pow;
12433 
12434 	/* Set default capability flags */
12435 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12436 		    I40E_FLAG_MSI_ENABLED     |
12437 		    I40E_FLAG_MSIX_ENABLED;
12438 
12439 	/* Set default ITR */
12440 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12441 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12442 
12443 	/* Depending on PF configurations, it is possible that the RSS
12444 	 * maximum might end up larger than the available queues
12445 	 */
12446 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12447 	pf->alloc_rss_size = 1;
12448 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12449 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12450 				 pf->hw.func_caps.num_tx_qp);
12451 
12452 	/* find the next higher power-of-2 of num cpus */
12453 	pow = roundup_pow_of_two(num_online_cpus());
12454 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12455 
12456 	if (pf->hw.func_caps.rss) {
12457 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12458 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12459 					   num_online_cpus());
12460 	}
12461 
12462 	/* MFP mode enabled */
12463 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12464 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12465 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12466 		if (i40e_get_partition_bw_setting(pf)) {
12467 			dev_warn(&pf->pdev->dev,
12468 				 "Could not get partition bw settings\n");
12469 		} else {
12470 			dev_info(&pf->pdev->dev,
12471 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12472 				 pf->min_bw, pf->max_bw);
12473 
12474 			/* nudge the Tx scheduler */
12475 			i40e_set_partition_bw_setting(pf);
12476 		}
12477 	}
12478 
12479 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12480 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12481 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12482 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12483 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12484 		    pf->hw.num_partitions > 1)
12485 			dev_info(&pf->pdev->dev,
12486 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12487 		else
12488 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12489 		pf->fdir_pf_filter_count =
12490 				 pf->hw.func_caps.fd_filters_guaranteed;
12491 		pf->hw.fdir_shared_filter_count =
12492 				 pf->hw.func_caps.fd_filters_best_effort;
12493 	}
12494 
12495 	if (pf->hw.mac.type == I40E_MAC_X722) {
12496 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12497 				    I40E_HW_128_QP_RSS_CAPABLE |
12498 				    I40E_HW_ATR_EVICT_CAPABLE |
12499 				    I40E_HW_WB_ON_ITR_CAPABLE |
12500 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12501 				    I40E_HW_NO_PCI_LINK_CHECK |
12502 				    I40E_HW_USE_SET_LLDP_MIB |
12503 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12504 				    I40E_HW_PTP_L4_CAPABLE |
12505 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12506 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12507 
12508 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12509 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12510 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12511 			dev_warn(&pf->pdev->dev,
12512 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12513 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12514 		}
12515 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12516 		   ((pf->hw.aq.api_maj_ver == 1) &&
12517 		    (pf->hw.aq.api_min_ver > 4))) {
12518 		/* Supported in FW API version higher than 1.4 */
12519 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12520 	}
12521 
12522 	/* Enable HW ATR eviction if possible */
12523 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12524 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12525 
12526 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12527 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12528 	    (pf->hw.aq.fw_maj_ver < 4))) {
12529 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12530 		/* No DCB support  for FW < v4.33 */
12531 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12532 	}
12533 
12534 	/* Disable FW LLDP if FW < v4.3 */
12535 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12536 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12537 	    (pf->hw.aq.fw_maj_ver < 4)))
12538 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12539 
12540 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12541 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12542 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12543 	    (pf->hw.aq.fw_maj_ver >= 5)))
12544 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12545 
12546 	/* Enable PTP L4 if FW > v6.0 */
12547 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12548 	    pf->hw.aq.fw_maj_ver >= 6)
12549 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12550 
12551 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12552 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12553 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12554 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12555 	}
12556 
12557 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12558 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12559 		/* IWARP needs one extra vector for CQP just like MISC.*/
12560 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12561 	}
12562 	/* Stopping FW LLDP engine is supported on XL710 and X722
12563 	 * starting from FW versions determined in i40e_init_adminq.
12564 	 * Stopping the FW LLDP engine is not supported on XL710
12565 	 * if NPAR is functioning so unset this hw flag in this case.
12566 	 */
12567 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12568 	    pf->hw.func_caps.npar_enable &&
12569 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12570 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12571 
12572 #ifdef CONFIG_PCI_IOV
12573 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12574 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12575 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12576 		pf->num_req_vfs = min_t(int,
12577 					pf->hw.func_caps.num_vfs,
12578 					I40E_MAX_VF_COUNT);
12579 	}
12580 #endif /* CONFIG_PCI_IOV */
12581 	pf->eeprom_version = 0xDEAD;
12582 	pf->lan_veb = I40E_NO_VEB;
12583 	pf->lan_vsi = I40E_NO_VSI;
12584 
12585 	/* By default FW has this off for performance reasons */
12586 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12587 
12588 	/* set up queue assignment tracking */
12589 	size = sizeof(struct i40e_lump_tracking)
12590 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12591 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12592 	if (!pf->qp_pile) {
12593 		err = -ENOMEM;
12594 		goto sw_init_done;
12595 	}
12596 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12597 
12598 	pf->tx_timeout_recovery_level = 1;
12599 
12600 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12601 	    i40e_is_total_port_shutdown_enabled(pf)) {
12602 		/* Link down on close must be on when total port shutdown
12603 		 * is enabled for a given port
12604 		 */
12605 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12606 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12607 		dev_info(&pf->pdev->dev,
12608 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12609 	}
12610 	mutex_init(&pf->switch_mutex);
12611 
12612 sw_init_done:
12613 	return err;
12614 }
12615 
12616 /**
12617  * i40e_set_ntuple - set the ntuple feature flag and take action
12618  * @pf: board private structure to initialize
12619  * @features: the feature set that the stack is suggesting
12620  *
12621  * returns a bool to indicate if reset needs to happen
12622  **/
12623 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12624 {
12625 	bool need_reset = false;
12626 
12627 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12628 	 * the state changed, we need to reset.
12629 	 */
12630 	if (features & NETIF_F_NTUPLE) {
12631 		/* Enable filters and mark for reset */
12632 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12633 			need_reset = true;
12634 		/* enable FD_SB only if there is MSI-X vector and no cloud
12635 		 * filters exist
12636 		 */
12637 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12638 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12639 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12640 		}
12641 	} else {
12642 		/* turn off filters, mark for reset and clear SW filter list */
12643 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12644 			need_reset = true;
12645 			i40e_fdir_filter_exit(pf);
12646 		}
12647 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12648 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12649 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12650 
12651 		/* reset fd counters */
12652 		pf->fd_add_err = 0;
12653 		pf->fd_atr_cnt = 0;
12654 		/* if ATR was auto disabled it can be re-enabled. */
12655 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12656 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12657 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12658 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12659 	}
12660 	return need_reset;
12661 }
12662 
12663 /**
12664  * i40e_clear_rss_lut - clear the rx hash lookup table
12665  * @vsi: the VSI being configured
12666  **/
12667 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12668 {
12669 	struct i40e_pf *pf = vsi->back;
12670 	struct i40e_hw *hw = &pf->hw;
12671 	u16 vf_id = vsi->vf_id;
12672 	u8 i;
12673 
12674 	if (vsi->type == I40E_VSI_MAIN) {
12675 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12676 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12677 	} else if (vsi->type == I40E_VSI_SRIOV) {
12678 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12679 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12680 	} else {
12681 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12682 	}
12683 }
12684 
12685 /**
12686  * i40e_set_features - set the netdev feature flags
12687  * @netdev: ptr to the netdev being adjusted
12688  * @features: the feature set that the stack is suggesting
12689  * Note: expects to be called while under rtnl_lock()
12690  **/
12691 static int i40e_set_features(struct net_device *netdev,
12692 			     netdev_features_t features)
12693 {
12694 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12695 	struct i40e_vsi *vsi = np->vsi;
12696 	struct i40e_pf *pf = vsi->back;
12697 	bool need_reset;
12698 
12699 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12700 		i40e_pf_config_rss(pf);
12701 	else if (!(features & NETIF_F_RXHASH) &&
12702 		 netdev->features & NETIF_F_RXHASH)
12703 		i40e_clear_rss_lut(vsi);
12704 
12705 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12706 		i40e_vlan_stripping_enable(vsi);
12707 	else
12708 		i40e_vlan_stripping_disable(vsi);
12709 
12710 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12711 		dev_err(&pf->pdev->dev,
12712 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12713 		return -EINVAL;
12714 	}
12715 
12716 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12717 		i40e_del_all_macvlans(vsi);
12718 
12719 	need_reset = i40e_set_ntuple(pf, features);
12720 
12721 	if (need_reset)
12722 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12723 
12724 	return 0;
12725 }
12726 
12727 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12728 				    unsigned int table, unsigned int idx,
12729 				    struct udp_tunnel_info *ti)
12730 {
12731 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12732 	struct i40e_hw *hw = &np->vsi->back->hw;
12733 	u8 type, filter_index;
12734 	i40e_status ret;
12735 
12736 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12737 						   I40E_AQC_TUNNEL_TYPE_NGE;
12738 
12739 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12740 				     NULL);
12741 	if (ret) {
12742 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12743 			    i40e_stat_str(hw, ret),
12744 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12745 		return -EIO;
12746 	}
12747 
12748 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12749 	return 0;
12750 }
12751 
12752 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12753 				      unsigned int table, unsigned int idx,
12754 				      struct udp_tunnel_info *ti)
12755 {
12756 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12757 	struct i40e_hw *hw = &np->vsi->back->hw;
12758 	i40e_status ret;
12759 
12760 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12761 	if (ret) {
12762 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12763 			    i40e_stat_str(hw, ret),
12764 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12765 		return -EIO;
12766 	}
12767 
12768 	return 0;
12769 }
12770 
12771 static int i40e_get_phys_port_id(struct net_device *netdev,
12772 				 struct netdev_phys_item_id *ppid)
12773 {
12774 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12775 	struct i40e_pf *pf = np->vsi->back;
12776 	struct i40e_hw *hw = &pf->hw;
12777 
12778 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12779 		return -EOPNOTSUPP;
12780 
12781 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12782 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12783 
12784 	return 0;
12785 }
12786 
12787 /**
12788  * i40e_ndo_fdb_add - add an entry to the hardware database
12789  * @ndm: the input from the stack
12790  * @tb: pointer to array of nladdr (unused)
12791  * @dev: the net device pointer
12792  * @addr: the MAC address entry being added
12793  * @vid: VLAN ID
12794  * @flags: instructions from stack about fdb operation
12795  * @extack: netlink extended ack, unused currently
12796  */
12797 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12798 			    struct net_device *dev,
12799 			    const unsigned char *addr, u16 vid,
12800 			    u16 flags,
12801 			    struct netlink_ext_ack *extack)
12802 {
12803 	struct i40e_netdev_priv *np = netdev_priv(dev);
12804 	struct i40e_pf *pf = np->vsi->back;
12805 	int err = 0;
12806 
12807 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12808 		return -EOPNOTSUPP;
12809 
12810 	if (vid) {
12811 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12812 		return -EINVAL;
12813 	}
12814 
12815 	/* Hardware does not support aging addresses so if a
12816 	 * ndm_state is given only allow permanent addresses
12817 	 */
12818 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12819 		netdev_info(dev, "FDB only supports static addresses\n");
12820 		return -EINVAL;
12821 	}
12822 
12823 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12824 		err = dev_uc_add_excl(dev, addr);
12825 	else if (is_multicast_ether_addr(addr))
12826 		err = dev_mc_add_excl(dev, addr);
12827 	else
12828 		err = -EINVAL;
12829 
12830 	/* Only return duplicate errors if NLM_F_EXCL is set */
12831 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12832 		err = 0;
12833 
12834 	return err;
12835 }
12836 
12837 /**
12838  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12839  * @dev: the netdev being configured
12840  * @nlh: RTNL message
12841  * @flags: bridge flags
12842  * @extack: netlink extended ack
12843  *
12844  * Inserts a new hardware bridge if not already created and
12845  * enables the bridging mode requested (VEB or VEPA). If the
12846  * hardware bridge has already been inserted and the request
12847  * is to change the mode then that requires a PF reset to
12848  * allow rebuild of the components with required hardware
12849  * bridge mode enabled.
12850  *
12851  * Note: expects to be called while under rtnl_lock()
12852  **/
12853 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12854 				   struct nlmsghdr *nlh,
12855 				   u16 flags,
12856 				   struct netlink_ext_ack *extack)
12857 {
12858 	struct i40e_netdev_priv *np = netdev_priv(dev);
12859 	struct i40e_vsi *vsi = np->vsi;
12860 	struct i40e_pf *pf = vsi->back;
12861 	struct i40e_veb *veb = NULL;
12862 	struct nlattr *attr, *br_spec;
12863 	int i, rem;
12864 
12865 	/* Only for PF VSI for now */
12866 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12867 		return -EOPNOTSUPP;
12868 
12869 	/* Find the HW bridge for PF VSI */
12870 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12871 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12872 			veb = pf->veb[i];
12873 	}
12874 
12875 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12876 
12877 	nla_for_each_nested(attr, br_spec, rem) {
12878 		__u16 mode;
12879 
12880 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12881 			continue;
12882 
12883 		mode = nla_get_u16(attr);
12884 		if ((mode != BRIDGE_MODE_VEPA) &&
12885 		    (mode != BRIDGE_MODE_VEB))
12886 			return -EINVAL;
12887 
12888 		/* Insert a new HW bridge */
12889 		if (!veb) {
12890 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12891 					     vsi->tc_config.enabled_tc);
12892 			if (veb) {
12893 				veb->bridge_mode = mode;
12894 				i40e_config_bridge_mode(veb);
12895 			} else {
12896 				/* No Bridge HW offload available */
12897 				return -ENOENT;
12898 			}
12899 			break;
12900 		} else if (mode != veb->bridge_mode) {
12901 			/* Existing HW bridge but different mode needs reset */
12902 			veb->bridge_mode = mode;
12903 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12904 			if (mode == BRIDGE_MODE_VEB)
12905 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12906 			else
12907 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12908 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12909 			break;
12910 		}
12911 	}
12912 
12913 	return 0;
12914 }
12915 
12916 /**
12917  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12918  * @skb: skb buff
12919  * @pid: process id
12920  * @seq: RTNL message seq #
12921  * @dev: the netdev being configured
12922  * @filter_mask: unused
12923  * @nlflags: netlink flags passed in
12924  *
12925  * Return the mode in which the hardware bridge is operating in
12926  * i.e VEB or VEPA.
12927  **/
12928 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12929 				   struct net_device *dev,
12930 				   u32 __always_unused filter_mask,
12931 				   int nlflags)
12932 {
12933 	struct i40e_netdev_priv *np = netdev_priv(dev);
12934 	struct i40e_vsi *vsi = np->vsi;
12935 	struct i40e_pf *pf = vsi->back;
12936 	struct i40e_veb *veb = NULL;
12937 	int i;
12938 
12939 	/* Only for PF VSI for now */
12940 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12941 		return -EOPNOTSUPP;
12942 
12943 	/* Find the HW bridge for the PF VSI */
12944 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12945 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12946 			veb = pf->veb[i];
12947 	}
12948 
12949 	if (!veb)
12950 		return 0;
12951 
12952 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12953 				       0, 0, nlflags, filter_mask, NULL);
12954 }
12955 
12956 /**
12957  * i40e_features_check - Validate encapsulated packet conforms to limits
12958  * @skb: skb buff
12959  * @dev: This physical port's netdev
12960  * @features: Offload features that the stack believes apply
12961  **/
12962 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12963 					     struct net_device *dev,
12964 					     netdev_features_t features)
12965 {
12966 	size_t len;
12967 
12968 	/* No point in doing any of this if neither checksum nor GSO are
12969 	 * being requested for this frame.  We can rule out both by just
12970 	 * checking for CHECKSUM_PARTIAL
12971 	 */
12972 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12973 		return features;
12974 
12975 	/* We cannot support GSO if the MSS is going to be less than
12976 	 * 64 bytes.  If it is then we need to drop support for GSO.
12977 	 */
12978 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12979 		features &= ~NETIF_F_GSO_MASK;
12980 
12981 	/* MACLEN can support at most 63 words */
12982 	len = skb_network_header(skb) - skb->data;
12983 	if (len & ~(63 * 2))
12984 		goto out_err;
12985 
12986 	/* IPLEN and EIPLEN can support at most 127 dwords */
12987 	len = skb_transport_header(skb) - skb_network_header(skb);
12988 	if (len & ~(127 * 4))
12989 		goto out_err;
12990 
12991 	if (skb->encapsulation) {
12992 		/* L4TUNLEN can support 127 words */
12993 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12994 		if (len & ~(127 * 2))
12995 			goto out_err;
12996 
12997 		/* IPLEN can support at most 127 dwords */
12998 		len = skb_inner_transport_header(skb) -
12999 		      skb_inner_network_header(skb);
13000 		if (len & ~(127 * 4))
13001 			goto out_err;
13002 	}
13003 
13004 	/* No need to validate L4LEN as TCP is the only protocol with a
13005 	 * a flexible value and we support all possible values supported
13006 	 * by TCP, which is at most 15 dwords
13007 	 */
13008 
13009 	return features;
13010 out_err:
13011 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13012 }
13013 
13014 /**
13015  * i40e_xdp_setup - add/remove an XDP program
13016  * @vsi: VSI to changed
13017  * @prog: XDP program
13018  * @extack: netlink extended ack
13019  **/
13020 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13021 			  struct netlink_ext_ack *extack)
13022 {
13023 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13024 	struct i40e_pf *pf = vsi->back;
13025 	struct bpf_prog *old_prog;
13026 	bool need_reset;
13027 	int i;
13028 
13029 	/* Don't allow frames that span over multiple buffers */
13030 	if (frame_size > vsi->rx_buf_len) {
13031 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13032 		return -EINVAL;
13033 	}
13034 
13035 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13036 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13037 
13038 	if (need_reset)
13039 		i40e_prep_for_reset(pf);
13040 
13041 	old_prog = xchg(&vsi->xdp_prog, prog);
13042 
13043 	if (need_reset) {
13044 		if (!prog)
13045 			/* Wait until ndo_xsk_wakeup completes. */
13046 			synchronize_rcu();
13047 		i40e_reset_and_rebuild(pf, true, true);
13048 	}
13049 
13050 	for (i = 0; i < vsi->num_queue_pairs; i++)
13051 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13052 
13053 	if (old_prog)
13054 		bpf_prog_put(old_prog);
13055 
13056 	/* Kick start the NAPI context if there is an AF_XDP socket open
13057 	 * on that queue id. This so that receiving will start.
13058 	 */
13059 	if (need_reset && prog)
13060 		for (i = 0; i < vsi->num_queue_pairs; i++)
13061 			if (vsi->xdp_rings[i]->xsk_pool)
13062 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13063 						      XDP_WAKEUP_RX);
13064 
13065 	return 0;
13066 }
13067 
13068 /**
13069  * i40e_enter_busy_conf - Enters busy config state
13070  * @vsi: vsi
13071  *
13072  * Returns 0 on success, <0 for failure.
13073  **/
13074 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13075 {
13076 	struct i40e_pf *pf = vsi->back;
13077 	int timeout = 50;
13078 
13079 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13080 		timeout--;
13081 		if (!timeout)
13082 			return -EBUSY;
13083 		usleep_range(1000, 2000);
13084 	}
13085 
13086 	return 0;
13087 }
13088 
13089 /**
13090  * i40e_exit_busy_conf - Exits busy config state
13091  * @vsi: vsi
13092  **/
13093 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13094 {
13095 	struct i40e_pf *pf = vsi->back;
13096 
13097 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13098 }
13099 
13100 /**
13101  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13102  * @vsi: vsi
13103  * @queue_pair: queue pair
13104  **/
13105 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13106 {
13107 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13108 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13109 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13110 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13111 	if (i40e_enabled_xdp_vsi(vsi)) {
13112 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13113 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13114 	}
13115 }
13116 
13117 /**
13118  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13119  * @vsi: vsi
13120  * @queue_pair: queue pair
13121  **/
13122 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13123 {
13124 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13125 	if (i40e_enabled_xdp_vsi(vsi)) {
13126 		/* Make sure that in-progress ndo_xdp_xmit calls are
13127 		 * completed.
13128 		 */
13129 		synchronize_rcu();
13130 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13131 	}
13132 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13133 }
13134 
13135 /**
13136  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13137  * @vsi: vsi
13138  * @queue_pair: queue pair
13139  * @enable: true for enable, false for disable
13140  **/
13141 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13142 					bool enable)
13143 {
13144 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13145 	struct i40e_q_vector *q_vector = rxr->q_vector;
13146 
13147 	if (!vsi->netdev)
13148 		return;
13149 
13150 	/* All rings in a qp belong to the same qvector. */
13151 	if (q_vector->rx.ring || q_vector->tx.ring) {
13152 		if (enable)
13153 			napi_enable(&q_vector->napi);
13154 		else
13155 			napi_disable(&q_vector->napi);
13156 	}
13157 }
13158 
13159 /**
13160  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13161  * @vsi: vsi
13162  * @queue_pair: queue pair
13163  * @enable: true for enable, false for disable
13164  *
13165  * Returns 0 on success, <0 on failure.
13166  **/
13167 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13168 					bool enable)
13169 {
13170 	struct i40e_pf *pf = vsi->back;
13171 	int pf_q, ret = 0;
13172 
13173 	pf_q = vsi->base_queue + queue_pair;
13174 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13175 				     false /*is xdp*/, enable);
13176 	if (ret) {
13177 		dev_info(&pf->pdev->dev,
13178 			 "VSI seid %d Tx ring %d %sable timeout\n",
13179 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13180 		return ret;
13181 	}
13182 
13183 	i40e_control_rx_q(pf, pf_q, enable);
13184 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13185 	if (ret) {
13186 		dev_info(&pf->pdev->dev,
13187 			 "VSI seid %d Rx ring %d %sable timeout\n",
13188 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13189 		return ret;
13190 	}
13191 
13192 	/* Due to HW errata, on Rx disable only, the register can
13193 	 * indicate done before it really is. Needs 50ms to be sure
13194 	 */
13195 	if (!enable)
13196 		mdelay(50);
13197 
13198 	if (!i40e_enabled_xdp_vsi(vsi))
13199 		return ret;
13200 
13201 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13202 				     pf_q + vsi->alloc_queue_pairs,
13203 				     true /*is xdp*/, enable);
13204 	if (ret) {
13205 		dev_info(&pf->pdev->dev,
13206 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13207 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13208 	}
13209 
13210 	return ret;
13211 }
13212 
13213 /**
13214  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13215  * @vsi: vsi
13216  * @queue_pair: queue_pair
13217  **/
13218 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13219 {
13220 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13221 	struct i40e_pf *pf = vsi->back;
13222 	struct i40e_hw *hw = &pf->hw;
13223 
13224 	/* All rings in a qp belong to the same qvector. */
13225 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13226 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13227 	else
13228 		i40e_irq_dynamic_enable_icr0(pf);
13229 
13230 	i40e_flush(hw);
13231 }
13232 
13233 /**
13234  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13235  * @vsi: vsi
13236  * @queue_pair: queue_pair
13237  **/
13238 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13239 {
13240 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13241 	struct i40e_pf *pf = vsi->back;
13242 	struct i40e_hw *hw = &pf->hw;
13243 
13244 	/* For simplicity, instead of removing the qp interrupt causes
13245 	 * from the interrupt linked list, we simply disable the interrupt, and
13246 	 * leave the list intact.
13247 	 *
13248 	 * All rings in a qp belong to the same qvector.
13249 	 */
13250 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13251 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13252 
13253 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13254 		i40e_flush(hw);
13255 		synchronize_irq(pf->msix_entries[intpf].vector);
13256 	} else {
13257 		/* Legacy and MSI mode - this stops all interrupt handling */
13258 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13259 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13260 		i40e_flush(hw);
13261 		synchronize_irq(pf->pdev->irq);
13262 	}
13263 }
13264 
13265 /**
13266  * i40e_queue_pair_disable - Disables a queue pair
13267  * @vsi: vsi
13268  * @queue_pair: queue pair
13269  *
13270  * Returns 0 on success, <0 on failure.
13271  **/
13272 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13273 {
13274 	int err;
13275 
13276 	err = i40e_enter_busy_conf(vsi);
13277 	if (err)
13278 		return err;
13279 
13280 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13281 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13282 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13283 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13284 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13285 
13286 	return err;
13287 }
13288 
13289 /**
13290  * i40e_queue_pair_enable - Enables a queue pair
13291  * @vsi: vsi
13292  * @queue_pair: queue pair
13293  *
13294  * Returns 0 on success, <0 on failure.
13295  **/
13296 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13297 {
13298 	int err;
13299 
13300 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13301 	if (err)
13302 		return err;
13303 
13304 	if (i40e_enabled_xdp_vsi(vsi)) {
13305 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13306 		if (err)
13307 			return err;
13308 	}
13309 
13310 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13311 	if (err)
13312 		return err;
13313 
13314 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13315 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13316 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13317 
13318 	i40e_exit_busy_conf(vsi);
13319 
13320 	return err;
13321 }
13322 
13323 /**
13324  * i40e_xdp - implements ndo_bpf for i40e
13325  * @dev: netdevice
13326  * @xdp: XDP command
13327  **/
13328 static int i40e_xdp(struct net_device *dev,
13329 		    struct netdev_bpf *xdp)
13330 {
13331 	struct i40e_netdev_priv *np = netdev_priv(dev);
13332 	struct i40e_vsi *vsi = np->vsi;
13333 
13334 	if (vsi->type != I40E_VSI_MAIN)
13335 		return -EINVAL;
13336 
13337 	switch (xdp->command) {
13338 	case XDP_SETUP_PROG:
13339 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13340 	case XDP_SETUP_XSK_POOL:
13341 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13342 					   xdp->xsk.queue_id);
13343 	default:
13344 		return -EINVAL;
13345 	}
13346 }
13347 
13348 static const struct net_device_ops i40e_netdev_ops = {
13349 	.ndo_open		= i40e_open,
13350 	.ndo_stop		= i40e_close,
13351 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13352 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13353 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13354 	.ndo_validate_addr	= eth_validate_addr,
13355 	.ndo_set_mac_address	= i40e_set_mac,
13356 	.ndo_change_mtu		= i40e_change_mtu,
13357 	.ndo_eth_ioctl		= i40e_ioctl,
13358 	.ndo_tx_timeout		= i40e_tx_timeout,
13359 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13360 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13361 #ifdef CONFIG_NET_POLL_CONTROLLER
13362 	.ndo_poll_controller	= i40e_netpoll,
13363 #endif
13364 	.ndo_setup_tc		= __i40e_setup_tc,
13365 	.ndo_select_queue	= i40e_lan_select_queue,
13366 	.ndo_set_features	= i40e_set_features,
13367 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13368 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13369 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13370 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13371 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13372 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13373 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13374 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13375 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13376 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13377 	.ndo_features_check	= i40e_features_check,
13378 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13379 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13380 	.ndo_bpf		= i40e_xdp,
13381 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13382 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13383 	.ndo_dfwd_add_station	= i40e_fwd_add,
13384 	.ndo_dfwd_del_station	= i40e_fwd_del,
13385 };
13386 
13387 /**
13388  * i40e_config_netdev - Setup the netdev flags
13389  * @vsi: the VSI being configured
13390  *
13391  * Returns 0 on success, negative value on failure
13392  **/
13393 static int i40e_config_netdev(struct i40e_vsi *vsi)
13394 {
13395 	struct i40e_pf *pf = vsi->back;
13396 	struct i40e_hw *hw = &pf->hw;
13397 	struct i40e_netdev_priv *np;
13398 	struct net_device *netdev;
13399 	u8 broadcast[ETH_ALEN];
13400 	u8 mac_addr[ETH_ALEN];
13401 	int etherdev_size;
13402 	netdev_features_t hw_enc_features;
13403 	netdev_features_t hw_features;
13404 
13405 	etherdev_size = sizeof(struct i40e_netdev_priv);
13406 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13407 	if (!netdev)
13408 		return -ENOMEM;
13409 
13410 	vsi->netdev = netdev;
13411 	np = netdev_priv(netdev);
13412 	np->vsi = vsi;
13413 
13414 	hw_enc_features = NETIF_F_SG			|
13415 			  NETIF_F_IP_CSUM		|
13416 			  NETIF_F_IPV6_CSUM		|
13417 			  NETIF_F_HIGHDMA		|
13418 			  NETIF_F_SOFT_FEATURES		|
13419 			  NETIF_F_TSO			|
13420 			  NETIF_F_TSO_ECN		|
13421 			  NETIF_F_TSO6			|
13422 			  NETIF_F_GSO_GRE		|
13423 			  NETIF_F_GSO_GRE_CSUM		|
13424 			  NETIF_F_GSO_PARTIAL		|
13425 			  NETIF_F_GSO_IPXIP4		|
13426 			  NETIF_F_GSO_IPXIP6		|
13427 			  NETIF_F_GSO_UDP_TUNNEL	|
13428 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13429 			  NETIF_F_GSO_UDP_L4		|
13430 			  NETIF_F_SCTP_CRC		|
13431 			  NETIF_F_RXHASH		|
13432 			  NETIF_F_RXCSUM		|
13433 			  0;
13434 
13435 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13436 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13437 
13438 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13439 
13440 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13441 
13442 	netdev->hw_enc_features |= hw_enc_features;
13443 
13444 	/* record features VLANs can make use of */
13445 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13446 
13447 	/* enable macvlan offloads */
13448 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13449 
13450 	hw_features = hw_enc_features		|
13451 		      NETIF_F_HW_VLAN_CTAG_TX	|
13452 		      NETIF_F_HW_VLAN_CTAG_RX;
13453 
13454 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13455 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13456 
13457 	netdev->hw_features |= hw_features;
13458 
13459 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13460 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13461 
13462 	if (vsi->type == I40E_VSI_MAIN) {
13463 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13464 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13465 		/* The following steps are necessary for two reasons. First,
13466 		 * some older NVM configurations load a default MAC-VLAN
13467 		 * filter that will accept any tagged packet, and we want to
13468 		 * replace this with a normal filter. Additionally, it is
13469 		 * possible our MAC address was provided by the platform using
13470 		 * Open Firmware or similar.
13471 		 *
13472 		 * Thus, we need to remove the default filter and install one
13473 		 * specific to the MAC address.
13474 		 */
13475 		i40e_rm_default_mac_filter(vsi, mac_addr);
13476 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13477 		i40e_add_mac_filter(vsi, mac_addr);
13478 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13479 	} else {
13480 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13481 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13482 		 * the end, which is 4 bytes long, so force truncation of the
13483 		 * original name by IFNAMSIZ - 4
13484 		 */
13485 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13486 			 IFNAMSIZ - 4,
13487 			 pf->vsi[pf->lan_vsi]->netdev->name);
13488 		eth_random_addr(mac_addr);
13489 
13490 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13491 		i40e_add_mac_filter(vsi, mac_addr);
13492 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13493 	}
13494 
13495 	/* Add the broadcast filter so that we initially will receive
13496 	 * broadcast packets. Note that when a new VLAN is first added the
13497 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13498 	 * specific filters as part of transitioning into "vlan" operation.
13499 	 * When more VLANs are added, the driver will copy each existing MAC
13500 	 * filter and add it for the new VLAN.
13501 	 *
13502 	 * Broadcast filters are handled specially by
13503 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13504 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13505 	 * filter. The subtask will update the correct broadcast promiscuous
13506 	 * bits as VLANs become active or inactive.
13507 	 */
13508 	eth_broadcast_addr(broadcast);
13509 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13510 	i40e_add_mac_filter(vsi, broadcast);
13511 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13512 
13513 	eth_hw_addr_set(netdev, mac_addr);
13514 	ether_addr_copy(netdev->perm_addr, mac_addr);
13515 
13516 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13517 	netdev->neigh_priv_len = sizeof(u32) * 4;
13518 
13519 	netdev->priv_flags |= IFF_UNICAST_FLT;
13520 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13521 	/* Setup netdev TC information */
13522 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13523 
13524 	netdev->netdev_ops = &i40e_netdev_ops;
13525 	netdev->watchdog_timeo = 5 * HZ;
13526 	i40e_set_ethtool_ops(netdev);
13527 
13528 	/* MTU range: 68 - 9706 */
13529 	netdev->min_mtu = ETH_MIN_MTU;
13530 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13531 
13532 	return 0;
13533 }
13534 
13535 /**
13536  * i40e_vsi_delete - Delete a VSI from the switch
13537  * @vsi: the VSI being removed
13538  *
13539  * Returns 0 on success, negative value on failure
13540  **/
13541 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13542 {
13543 	/* remove default VSI is not allowed */
13544 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13545 		return;
13546 
13547 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13548 }
13549 
13550 /**
13551  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13552  * @vsi: the VSI being queried
13553  *
13554  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13555  **/
13556 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13557 {
13558 	struct i40e_veb *veb;
13559 	struct i40e_pf *pf = vsi->back;
13560 
13561 	/* Uplink is not a bridge so default to VEB */
13562 	if (vsi->veb_idx >= I40E_MAX_VEB)
13563 		return 1;
13564 
13565 	veb = pf->veb[vsi->veb_idx];
13566 	if (!veb) {
13567 		dev_info(&pf->pdev->dev,
13568 			 "There is no veb associated with the bridge\n");
13569 		return -ENOENT;
13570 	}
13571 
13572 	/* Uplink is a bridge in VEPA mode */
13573 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13574 		return 0;
13575 	} else {
13576 		/* Uplink is a bridge in VEB mode */
13577 		return 1;
13578 	}
13579 
13580 	/* VEPA is now default bridge, so return 0 */
13581 	return 0;
13582 }
13583 
13584 /**
13585  * i40e_add_vsi - Add a VSI to the switch
13586  * @vsi: the VSI being configured
13587  *
13588  * This initializes a VSI context depending on the VSI type to be added and
13589  * passes it down to the add_vsi aq command.
13590  **/
13591 static int i40e_add_vsi(struct i40e_vsi *vsi)
13592 {
13593 	int ret = -ENODEV;
13594 	struct i40e_pf *pf = vsi->back;
13595 	struct i40e_hw *hw = &pf->hw;
13596 	struct i40e_vsi_context ctxt;
13597 	struct i40e_mac_filter *f;
13598 	struct hlist_node *h;
13599 	int bkt;
13600 
13601 	u8 enabled_tc = 0x1; /* TC0 enabled */
13602 	int f_count = 0;
13603 
13604 	memset(&ctxt, 0, sizeof(ctxt));
13605 	switch (vsi->type) {
13606 	case I40E_VSI_MAIN:
13607 		/* The PF's main VSI is already setup as part of the
13608 		 * device initialization, so we'll not bother with
13609 		 * the add_vsi call, but we will retrieve the current
13610 		 * VSI context.
13611 		 */
13612 		ctxt.seid = pf->main_vsi_seid;
13613 		ctxt.pf_num = pf->hw.pf_id;
13614 		ctxt.vf_num = 0;
13615 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13616 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13617 		if (ret) {
13618 			dev_info(&pf->pdev->dev,
13619 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13620 				 i40e_stat_str(&pf->hw, ret),
13621 				 i40e_aq_str(&pf->hw,
13622 					     pf->hw.aq.asq_last_status));
13623 			return -ENOENT;
13624 		}
13625 		vsi->info = ctxt.info;
13626 		vsi->info.valid_sections = 0;
13627 
13628 		vsi->seid = ctxt.seid;
13629 		vsi->id = ctxt.vsi_number;
13630 
13631 		enabled_tc = i40e_pf_get_tc_map(pf);
13632 
13633 		/* Source pruning is enabled by default, so the flag is
13634 		 * negative logic - if it's set, we need to fiddle with
13635 		 * the VSI to disable source pruning.
13636 		 */
13637 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13638 			memset(&ctxt, 0, sizeof(ctxt));
13639 			ctxt.seid = pf->main_vsi_seid;
13640 			ctxt.pf_num = pf->hw.pf_id;
13641 			ctxt.vf_num = 0;
13642 			ctxt.info.valid_sections |=
13643 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13644 			ctxt.info.switch_id =
13645 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13646 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13647 			if (ret) {
13648 				dev_info(&pf->pdev->dev,
13649 					 "update vsi failed, err %s aq_err %s\n",
13650 					 i40e_stat_str(&pf->hw, ret),
13651 					 i40e_aq_str(&pf->hw,
13652 						     pf->hw.aq.asq_last_status));
13653 				ret = -ENOENT;
13654 				goto err;
13655 			}
13656 		}
13657 
13658 		/* MFP mode setup queue map and update VSI */
13659 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13660 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13661 			memset(&ctxt, 0, sizeof(ctxt));
13662 			ctxt.seid = pf->main_vsi_seid;
13663 			ctxt.pf_num = pf->hw.pf_id;
13664 			ctxt.vf_num = 0;
13665 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13666 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13667 			if (ret) {
13668 				dev_info(&pf->pdev->dev,
13669 					 "update vsi failed, err %s aq_err %s\n",
13670 					 i40e_stat_str(&pf->hw, ret),
13671 					 i40e_aq_str(&pf->hw,
13672 						    pf->hw.aq.asq_last_status));
13673 				ret = -ENOENT;
13674 				goto err;
13675 			}
13676 			/* update the local VSI info queue map */
13677 			i40e_vsi_update_queue_map(vsi, &ctxt);
13678 			vsi->info.valid_sections = 0;
13679 		} else {
13680 			/* Default/Main VSI is only enabled for TC0
13681 			 * reconfigure it to enable all TCs that are
13682 			 * available on the port in SFP mode.
13683 			 * For MFP case the iSCSI PF would use this
13684 			 * flow to enable LAN+iSCSI TC.
13685 			 */
13686 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13687 			if (ret) {
13688 				/* Single TC condition is not fatal,
13689 				 * message and continue
13690 				 */
13691 				dev_info(&pf->pdev->dev,
13692 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13693 					 enabled_tc,
13694 					 i40e_stat_str(&pf->hw, ret),
13695 					 i40e_aq_str(&pf->hw,
13696 						    pf->hw.aq.asq_last_status));
13697 			}
13698 		}
13699 		break;
13700 
13701 	case I40E_VSI_FDIR:
13702 		ctxt.pf_num = hw->pf_id;
13703 		ctxt.vf_num = 0;
13704 		ctxt.uplink_seid = vsi->uplink_seid;
13705 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13706 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13707 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13708 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13709 			ctxt.info.valid_sections |=
13710 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13711 			ctxt.info.switch_id =
13712 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13713 		}
13714 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13715 		break;
13716 
13717 	case I40E_VSI_VMDQ2:
13718 		ctxt.pf_num = hw->pf_id;
13719 		ctxt.vf_num = 0;
13720 		ctxt.uplink_seid = vsi->uplink_seid;
13721 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13722 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13723 
13724 		/* This VSI is connected to VEB so the switch_id
13725 		 * should be set to zero by default.
13726 		 */
13727 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13728 			ctxt.info.valid_sections |=
13729 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13730 			ctxt.info.switch_id =
13731 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13732 		}
13733 
13734 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13735 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13736 		break;
13737 
13738 	case I40E_VSI_SRIOV:
13739 		ctxt.pf_num = hw->pf_id;
13740 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13741 		ctxt.uplink_seid = vsi->uplink_seid;
13742 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13743 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13744 
13745 		/* This VSI is connected to VEB so the switch_id
13746 		 * should be set to zero by default.
13747 		 */
13748 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13749 			ctxt.info.valid_sections |=
13750 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13751 			ctxt.info.switch_id =
13752 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13753 		}
13754 
13755 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13756 			ctxt.info.valid_sections |=
13757 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13758 			ctxt.info.queueing_opt_flags |=
13759 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13760 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13761 		}
13762 
13763 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13764 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13765 		if (pf->vf[vsi->vf_id].spoofchk) {
13766 			ctxt.info.valid_sections |=
13767 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13768 			ctxt.info.sec_flags |=
13769 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13770 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13771 		}
13772 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13773 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13774 		break;
13775 
13776 	case I40E_VSI_IWARP:
13777 		/* send down message to iWARP */
13778 		break;
13779 
13780 	default:
13781 		return -ENODEV;
13782 	}
13783 
13784 	if (vsi->type != I40E_VSI_MAIN) {
13785 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13786 		if (ret) {
13787 			dev_info(&vsi->back->pdev->dev,
13788 				 "add vsi failed, err %s aq_err %s\n",
13789 				 i40e_stat_str(&pf->hw, ret),
13790 				 i40e_aq_str(&pf->hw,
13791 					     pf->hw.aq.asq_last_status));
13792 			ret = -ENOENT;
13793 			goto err;
13794 		}
13795 		vsi->info = ctxt.info;
13796 		vsi->info.valid_sections = 0;
13797 		vsi->seid = ctxt.seid;
13798 		vsi->id = ctxt.vsi_number;
13799 	}
13800 
13801 	vsi->active_filters = 0;
13802 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13803 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13804 	/* If macvlan filters already exist, force them to get loaded */
13805 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13806 		f->state = I40E_FILTER_NEW;
13807 		f_count++;
13808 	}
13809 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13810 
13811 	if (f_count) {
13812 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13813 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13814 	}
13815 
13816 	/* Update VSI BW information */
13817 	ret = i40e_vsi_get_bw_info(vsi);
13818 	if (ret) {
13819 		dev_info(&pf->pdev->dev,
13820 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13821 			 i40e_stat_str(&pf->hw, ret),
13822 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13823 		/* VSI is already added so not tearing that up */
13824 		ret = 0;
13825 	}
13826 
13827 err:
13828 	return ret;
13829 }
13830 
13831 /**
13832  * i40e_vsi_release - Delete a VSI and free its resources
13833  * @vsi: the VSI being removed
13834  *
13835  * Returns 0 on success or < 0 on error
13836  **/
13837 int i40e_vsi_release(struct i40e_vsi *vsi)
13838 {
13839 	struct i40e_mac_filter *f;
13840 	struct hlist_node *h;
13841 	struct i40e_veb *veb = NULL;
13842 	struct i40e_pf *pf;
13843 	u16 uplink_seid;
13844 	int i, n, bkt;
13845 
13846 	pf = vsi->back;
13847 
13848 	/* release of a VEB-owner or last VSI is not allowed */
13849 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13850 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13851 			 vsi->seid, vsi->uplink_seid);
13852 		return -ENODEV;
13853 	}
13854 	if (vsi == pf->vsi[pf->lan_vsi] &&
13855 	    !test_bit(__I40E_DOWN, pf->state)) {
13856 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13857 		return -ENODEV;
13858 	}
13859 	set_bit(__I40E_VSI_RELEASING, vsi->state);
13860 	uplink_seid = vsi->uplink_seid;
13861 	if (vsi->type != I40E_VSI_SRIOV) {
13862 		if (vsi->netdev_registered) {
13863 			vsi->netdev_registered = false;
13864 			if (vsi->netdev) {
13865 				/* results in a call to i40e_close() */
13866 				unregister_netdev(vsi->netdev);
13867 			}
13868 		} else {
13869 			i40e_vsi_close(vsi);
13870 		}
13871 		i40e_vsi_disable_irq(vsi);
13872 	}
13873 
13874 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13875 
13876 	/* clear the sync flag on all filters */
13877 	if (vsi->netdev) {
13878 		__dev_uc_unsync(vsi->netdev, NULL);
13879 		__dev_mc_unsync(vsi->netdev, NULL);
13880 	}
13881 
13882 	/* make sure any remaining filters are marked for deletion */
13883 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13884 		__i40e_del_filter(vsi, f);
13885 
13886 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13887 
13888 	i40e_sync_vsi_filters(vsi);
13889 
13890 	i40e_vsi_delete(vsi);
13891 	i40e_vsi_free_q_vectors(vsi);
13892 	if (vsi->netdev) {
13893 		free_netdev(vsi->netdev);
13894 		vsi->netdev = NULL;
13895 	}
13896 	i40e_vsi_clear_rings(vsi);
13897 	i40e_vsi_clear(vsi);
13898 
13899 	/* If this was the last thing on the VEB, except for the
13900 	 * controlling VSI, remove the VEB, which puts the controlling
13901 	 * VSI onto the next level down in the switch.
13902 	 *
13903 	 * Well, okay, there's one more exception here: don't remove
13904 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13905 	 * from up the network stack.
13906 	 */
13907 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13908 		if (pf->vsi[i] &&
13909 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13910 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13911 			n++;      /* count the VSIs */
13912 		}
13913 	}
13914 	for (i = 0; i < I40E_MAX_VEB; i++) {
13915 		if (!pf->veb[i])
13916 			continue;
13917 		if (pf->veb[i]->uplink_seid == uplink_seid)
13918 			n++;     /* count the VEBs */
13919 		if (pf->veb[i]->seid == uplink_seid)
13920 			veb = pf->veb[i];
13921 	}
13922 	if (n == 0 && veb && veb->uplink_seid != 0)
13923 		i40e_veb_release(veb);
13924 
13925 	return 0;
13926 }
13927 
13928 /**
13929  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13930  * @vsi: ptr to the VSI
13931  *
13932  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13933  * corresponding SW VSI structure and initializes num_queue_pairs for the
13934  * newly allocated VSI.
13935  *
13936  * Returns 0 on success or negative on failure
13937  **/
13938 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13939 {
13940 	int ret = -ENOENT;
13941 	struct i40e_pf *pf = vsi->back;
13942 
13943 	if (vsi->q_vectors[0]) {
13944 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13945 			 vsi->seid);
13946 		return -EEXIST;
13947 	}
13948 
13949 	if (vsi->base_vector) {
13950 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13951 			 vsi->seid, vsi->base_vector);
13952 		return -EEXIST;
13953 	}
13954 
13955 	ret = i40e_vsi_alloc_q_vectors(vsi);
13956 	if (ret) {
13957 		dev_info(&pf->pdev->dev,
13958 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13959 			 vsi->num_q_vectors, vsi->seid, ret);
13960 		vsi->num_q_vectors = 0;
13961 		goto vector_setup_out;
13962 	}
13963 
13964 	/* In Legacy mode, we do not have to get any other vector since we
13965 	 * piggyback on the misc/ICR0 for queue interrupts.
13966 	*/
13967 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13968 		return ret;
13969 	if (vsi->num_q_vectors)
13970 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13971 						 vsi->num_q_vectors, vsi->idx);
13972 	if (vsi->base_vector < 0) {
13973 		dev_info(&pf->pdev->dev,
13974 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13975 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13976 		i40e_vsi_free_q_vectors(vsi);
13977 		ret = -ENOENT;
13978 		goto vector_setup_out;
13979 	}
13980 
13981 vector_setup_out:
13982 	return ret;
13983 }
13984 
13985 /**
13986  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13987  * @vsi: pointer to the vsi.
13988  *
13989  * This re-allocates a vsi's queue resources.
13990  *
13991  * Returns pointer to the successfully allocated and configured VSI sw struct
13992  * on success, otherwise returns NULL on failure.
13993  **/
13994 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13995 {
13996 	u16 alloc_queue_pairs;
13997 	struct i40e_pf *pf;
13998 	u8 enabled_tc;
13999 	int ret;
14000 
14001 	if (!vsi)
14002 		return NULL;
14003 
14004 	pf = vsi->back;
14005 
14006 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14007 	i40e_vsi_clear_rings(vsi);
14008 
14009 	i40e_vsi_free_arrays(vsi, false);
14010 	i40e_set_num_rings_in_vsi(vsi);
14011 	ret = i40e_vsi_alloc_arrays(vsi, false);
14012 	if (ret)
14013 		goto err_vsi;
14014 
14015 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14016 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14017 
14018 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14019 	if (ret < 0) {
14020 		dev_info(&pf->pdev->dev,
14021 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14022 			 alloc_queue_pairs, vsi->seid, ret);
14023 		goto err_vsi;
14024 	}
14025 	vsi->base_queue = ret;
14026 
14027 	/* Update the FW view of the VSI. Force a reset of TC and queue
14028 	 * layout configurations.
14029 	 */
14030 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14031 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14032 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14033 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14034 	if (vsi->type == I40E_VSI_MAIN)
14035 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14036 
14037 	/* assign it some queues */
14038 	ret = i40e_alloc_rings(vsi);
14039 	if (ret)
14040 		goto err_rings;
14041 
14042 	/* map all of the rings to the q_vectors */
14043 	i40e_vsi_map_rings_to_vectors(vsi);
14044 	return vsi;
14045 
14046 err_rings:
14047 	i40e_vsi_free_q_vectors(vsi);
14048 	if (vsi->netdev_registered) {
14049 		vsi->netdev_registered = false;
14050 		unregister_netdev(vsi->netdev);
14051 		free_netdev(vsi->netdev);
14052 		vsi->netdev = NULL;
14053 	}
14054 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14055 err_vsi:
14056 	i40e_vsi_clear(vsi);
14057 	return NULL;
14058 }
14059 
14060 /**
14061  * i40e_vsi_setup - Set up a VSI by a given type
14062  * @pf: board private structure
14063  * @type: VSI type
14064  * @uplink_seid: the switch element to link to
14065  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14066  *
14067  * This allocates the sw VSI structure and its queue resources, then add a VSI
14068  * to the identified VEB.
14069  *
14070  * Returns pointer to the successfully allocated and configure VSI sw struct on
14071  * success, otherwise returns NULL on failure.
14072  **/
14073 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14074 				u16 uplink_seid, u32 param1)
14075 {
14076 	struct i40e_vsi *vsi = NULL;
14077 	struct i40e_veb *veb = NULL;
14078 	u16 alloc_queue_pairs;
14079 	int ret, i;
14080 	int v_idx;
14081 
14082 	/* The requested uplink_seid must be either
14083 	 *     - the PF's port seid
14084 	 *              no VEB is needed because this is the PF
14085 	 *              or this is a Flow Director special case VSI
14086 	 *     - seid of an existing VEB
14087 	 *     - seid of a VSI that owns an existing VEB
14088 	 *     - seid of a VSI that doesn't own a VEB
14089 	 *              a new VEB is created and the VSI becomes the owner
14090 	 *     - seid of the PF VSI, which is what creates the first VEB
14091 	 *              this is a special case of the previous
14092 	 *
14093 	 * Find which uplink_seid we were given and create a new VEB if needed
14094 	 */
14095 	for (i = 0; i < I40E_MAX_VEB; i++) {
14096 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14097 			veb = pf->veb[i];
14098 			break;
14099 		}
14100 	}
14101 
14102 	if (!veb && uplink_seid != pf->mac_seid) {
14103 
14104 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14105 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14106 				vsi = pf->vsi[i];
14107 				break;
14108 			}
14109 		}
14110 		if (!vsi) {
14111 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14112 				 uplink_seid);
14113 			return NULL;
14114 		}
14115 
14116 		if (vsi->uplink_seid == pf->mac_seid)
14117 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14118 					     vsi->tc_config.enabled_tc);
14119 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14120 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14121 					     vsi->tc_config.enabled_tc);
14122 		if (veb) {
14123 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14124 				dev_info(&vsi->back->pdev->dev,
14125 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14126 				return NULL;
14127 			}
14128 			/* We come up by default in VEPA mode if SRIOV is not
14129 			 * already enabled, in which case we can't force VEPA
14130 			 * mode.
14131 			 */
14132 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14133 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14134 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14135 			}
14136 			i40e_config_bridge_mode(veb);
14137 		}
14138 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14139 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14140 				veb = pf->veb[i];
14141 		}
14142 		if (!veb) {
14143 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14144 			return NULL;
14145 		}
14146 
14147 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14148 		uplink_seid = veb->seid;
14149 	}
14150 
14151 	/* get vsi sw struct */
14152 	v_idx = i40e_vsi_mem_alloc(pf, type);
14153 	if (v_idx < 0)
14154 		goto err_alloc;
14155 	vsi = pf->vsi[v_idx];
14156 	if (!vsi)
14157 		goto err_alloc;
14158 	vsi->type = type;
14159 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14160 
14161 	if (type == I40E_VSI_MAIN)
14162 		pf->lan_vsi = v_idx;
14163 	else if (type == I40E_VSI_SRIOV)
14164 		vsi->vf_id = param1;
14165 	/* assign it some queues */
14166 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14167 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14168 
14169 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14170 	if (ret < 0) {
14171 		dev_info(&pf->pdev->dev,
14172 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14173 			 alloc_queue_pairs, vsi->seid, ret);
14174 		goto err_vsi;
14175 	}
14176 	vsi->base_queue = ret;
14177 
14178 	/* get a VSI from the hardware */
14179 	vsi->uplink_seid = uplink_seid;
14180 	ret = i40e_add_vsi(vsi);
14181 	if (ret)
14182 		goto err_vsi;
14183 
14184 	switch (vsi->type) {
14185 	/* setup the netdev if needed */
14186 	case I40E_VSI_MAIN:
14187 	case I40E_VSI_VMDQ2:
14188 		ret = i40e_config_netdev(vsi);
14189 		if (ret)
14190 			goto err_netdev;
14191 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14192 		if (ret)
14193 			goto err_netdev;
14194 		ret = register_netdev(vsi->netdev);
14195 		if (ret)
14196 			goto err_netdev;
14197 		vsi->netdev_registered = true;
14198 		netif_carrier_off(vsi->netdev);
14199 #ifdef CONFIG_I40E_DCB
14200 		/* Setup DCB netlink interface */
14201 		i40e_dcbnl_setup(vsi);
14202 #endif /* CONFIG_I40E_DCB */
14203 		fallthrough;
14204 	case I40E_VSI_FDIR:
14205 		/* set up vectors and rings if needed */
14206 		ret = i40e_vsi_setup_vectors(vsi);
14207 		if (ret)
14208 			goto err_msix;
14209 
14210 		ret = i40e_alloc_rings(vsi);
14211 		if (ret)
14212 			goto err_rings;
14213 
14214 		/* map all of the rings to the q_vectors */
14215 		i40e_vsi_map_rings_to_vectors(vsi);
14216 
14217 		i40e_vsi_reset_stats(vsi);
14218 		break;
14219 	default:
14220 		/* no netdev or rings for the other VSI types */
14221 		break;
14222 	}
14223 
14224 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14225 	    (vsi->type == I40E_VSI_VMDQ2)) {
14226 		ret = i40e_vsi_config_rss(vsi);
14227 	}
14228 	return vsi;
14229 
14230 err_rings:
14231 	i40e_vsi_free_q_vectors(vsi);
14232 err_msix:
14233 	if (vsi->netdev_registered) {
14234 		vsi->netdev_registered = false;
14235 		unregister_netdev(vsi->netdev);
14236 		free_netdev(vsi->netdev);
14237 		vsi->netdev = NULL;
14238 	}
14239 err_netdev:
14240 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14241 err_vsi:
14242 	i40e_vsi_clear(vsi);
14243 err_alloc:
14244 	return NULL;
14245 }
14246 
14247 /**
14248  * i40e_veb_get_bw_info - Query VEB BW information
14249  * @veb: the veb to query
14250  *
14251  * Query the Tx scheduler BW configuration data for given VEB
14252  **/
14253 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14254 {
14255 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14256 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14257 	struct i40e_pf *pf = veb->pf;
14258 	struct i40e_hw *hw = &pf->hw;
14259 	u32 tc_bw_max;
14260 	int ret = 0;
14261 	int i;
14262 
14263 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14264 						  &bw_data, NULL);
14265 	if (ret) {
14266 		dev_info(&pf->pdev->dev,
14267 			 "query veb bw config failed, err %s aq_err %s\n",
14268 			 i40e_stat_str(&pf->hw, ret),
14269 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14270 		goto out;
14271 	}
14272 
14273 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14274 						   &ets_data, NULL);
14275 	if (ret) {
14276 		dev_info(&pf->pdev->dev,
14277 			 "query veb bw ets config failed, err %s aq_err %s\n",
14278 			 i40e_stat_str(&pf->hw, ret),
14279 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14280 		goto out;
14281 	}
14282 
14283 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14284 	veb->bw_max_quanta = ets_data.tc_bw_max;
14285 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14286 	veb->enabled_tc = ets_data.tc_valid_bits;
14287 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14288 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14289 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14290 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14291 		veb->bw_tc_limit_credits[i] =
14292 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14293 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14294 	}
14295 
14296 out:
14297 	return ret;
14298 }
14299 
14300 /**
14301  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14302  * @pf: board private structure
14303  *
14304  * On error: returns error code (negative)
14305  * On success: returns vsi index in PF (positive)
14306  **/
14307 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14308 {
14309 	int ret = -ENOENT;
14310 	struct i40e_veb *veb;
14311 	int i;
14312 
14313 	/* Need to protect the allocation of switch elements at the PF level */
14314 	mutex_lock(&pf->switch_mutex);
14315 
14316 	/* VEB list may be fragmented if VEB creation/destruction has
14317 	 * been happening.  We can afford to do a quick scan to look
14318 	 * for any free slots in the list.
14319 	 *
14320 	 * find next empty veb slot, looping back around if necessary
14321 	 */
14322 	i = 0;
14323 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14324 		i++;
14325 	if (i >= I40E_MAX_VEB) {
14326 		ret = -ENOMEM;
14327 		goto err_alloc_veb;  /* out of VEB slots! */
14328 	}
14329 
14330 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14331 	if (!veb) {
14332 		ret = -ENOMEM;
14333 		goto err_alloc_veb;
14334 	}
14335 	veb->pf = pf;
14336 	veb->idx = i;
14337 	veb->enabled_tc = 1;
14338 
14339 	pf->veb[i] = veb;
14340 	ret = i;
14341 err_alloc_veb:
14342 	mutex_unlock(&pf->switch_mutex);
14343 	return ret;
14344 }
14345 
14346 /**
14347  * i40e_switch_branch_release - Delete a branch of the switch tree
14348  * @branch: where to start deleting
14349  *
14350  * This uses recursion to find the tips of the branch to be
14351  * removed, deleting until we get back to and can delete this VEB.
14352  **/
14353 static void i40e_switch_branch_release(struct i40e_veb *branch)
14354 {
14355 	struct i40e_pf *pf = branch->pf;
14356 	u16 branch_seid = branch->seid;
14357 	u16 veb_idx = branch->idx;
14358 	int i;
14359 
14360 	/* release any VEBs on this VEB - RECURSION */
14361 	for (i = 0; i < I40E_MAX_VEB; i++) {
14362 		if (!pf->veb[i])
14363 			continue;
14364 		if (pf->veb[i]->uplink_seid == branch->seid)
14365 			i40e_switch_branch_release(pf->veb[i]);
14366 	}
14367 
14368 	/* Release the VSIs on this VEB, but not the owner VSI.
14369 	 *
14370 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14371 	 *       the VEB itself, so don't use (*branch) after this loop.
14372 	 */
14373 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14374 		if (!pf->vsi[i])
14375 			continue;
14376 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14377 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14378 			i40e_vsi_release(pf->vsi[i]);
14379 		}
14380 	}
14381 
14382 	/* There's one corner case where the VEB might not have been
14383 	 * removed, so double check it here and remove it if needed.
14384 	 * This case happens if the veb was created from the debugfs
14385 	 * commands and no VSIs were added to it.
14386 	 */
14387 	if (pf->veb[veb_idx])
14388 		i40e_veb_release(pf->veb[veb_idx]);
14389 }
14390 
14391 /**
14392  * i40e_veb_clear - remove veb struct
14393  * @veb: the veb to remove
14394  **/
14395 static void i40e_veb_clear(struct i40e_veb *veb)
14396 {
14397 	if (!veb)
14398 		return;
14399 
14400 	if (veb->pf) {
14401 		struct i40e_pf *pf = veb->pf;
14402 
14403 		mutex_lock(&pf->switch_mutex);
14404 		if (pf->veb[veb->idx] == veb)
14405 			pf->veb[veb->idx] = NULL;
14406 		mutex_unlock(&pf->switch_mutex);
14407 	}
14408 
14409 	kfree(veb);
14410 }
14411 
14412 /**
14413  * i40e_veb_release - Delete a VEB and free its resources
14414  * @veb: the VEB being removed
14415  **/
14416 void i40e_veb_release(struct i40e_veb *veb)
14417 {
14418 	struct i40e_vsi *vsi = NULL;
14419 	struct i40e_pf *pf;
14420 	int i, n = 0;
14421 
14422 	pf = veb->pf;
14423 
14424 	/* find the remaining VSI and check for extras */
14425 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14426 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14427 			n++;
14428 			vsi = pf->vsi[i];
14429 		}
14430 	}
14431 	if (n != 1) {
14432 		dev_info(&pf->pdev->dev,
14433 			 "can't remove VEB %d with %d VSIs left\n",
14434 			 veb->seid, n);
14435 		return;
14436 	}
14437 
14438 	/* move the remaining VSI to uplink veb */
14439 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14440 	if (veb->uplink_seid) {
14441 		vsi->uplink_seid = veb->uplink_seid;
14442 		if (veb->uplink_seid == pf->mac_seid)
14443 			vsi->veb_idx = I40E_NO_VEB;
14444 		else
14445 			vsi->veb_idx = veb->veb_idx;
14446 	} else {
14447 		/* floating VEB */
14448 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14449 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14450 	}
14451 
14452 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14453 	i40e_veb_clear(veb);
14454 }
14455 
14456 /**
14457  * i40e_add_veb - create the VEB in the switch
14458  * @veb: the VEB to be instantiated
14459  * @vsi: the controlling VSI
14460  **/
14461 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14462 {
14463 	struct i40e_pf *pf = veb->pf;
14464 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14465 	int ret;
14466 
14467 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14468 			      veb->enabled_tc, false,
14469 			      &veb->seid, enable_stats, NULL);
14470 
14471 	/* get a VEB from the hardware */
14472 	if (ret) {
14473 		dev_info(&pf->pdev->dev,
14474 			 "couldn't add VEB, err %s aq_err %s\n",
14475 			 i40e_stat_str(&pf->hw, ret),
14476 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14477 		return -EPERM;
14478 	}
14479 
14480 	/* get statistics counter */
14481 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14482 					 &veb->stats_idx, NULL, NULL, NULL);
14483 	if (ret) {
14484 		dev_info(&pf->pdev->dev,
14485 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14486 			 i40e_stat_str(&pf->hw, ret),
14487 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14488 		return -EPERM;
14489 	}
14490 	ret = i40e_veb_get_bw_info(veb);
14491 	if (ret) {
14492 		dev_info(&pf->pdev->dev,
14493 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14494 			 i40e_stat_str(&pf->hw, ret),
14495 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14496 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14497 		return -ENOENT;
14498 	}
14499 
14500 	vsi->uplink_seid = veb->seid;
14501 	vsi->veb_idx = veb->idx;
14502 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14503 
14504 	return 0;
14505 }
14506 
14507 /**
14508  * i40e_veb_setup - Set up a VEB
14509  * @pf: board private structure
14510  * @flags: VEB setup flags
14511  * @uplink_seid: the switch element to link to
14512  * @vsi_seid: the initial VSI seid
14513  * @enabled_tc: Enabled TC bit-map
14514  *
14515  * This allocates the sw VEB structure and links it into the switch
14516  * It is possible and legal for this to be a duplicate of an already
14517  * existing VEB.  It is also possible for both uplink and vsi seids
14518  * to be zero, in order to create a floating VEB.
14519  *
14520  * Returns pointer to the successfully allocated VEB sw struct on
14521  * success, otherwise returns NULL on failure.
14522  **/
14523 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14524 				u16 uplink_seid, u16 vsi_seid,
14525 				u8 enabled_tc)
14526 {
14527 	struct i40e_veb *veb, *uplink_veb = NULL;
14528 	int vsi_idx, veb_idx;
14529 	int ret;
14530 
14531 	/* if one seid is 0, the other must be 0 to create a floating relay */
14532 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14533 	    (uplink_seid + vsi_seid != 0)) {
14534 		dev_info(&pf->pdev->dev,
14535 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14536 			 uplink_seid, vsi_seid);
14537 		return NULL;
14538 	}
14539 
14540 	/* make sure there is such a vsi and uplink */
14541 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14542 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14543 			break;
14544 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14545 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14546 			 vsi_seid);
14547 		return NULL;
14548 	}
14549 
14550 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14551 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14552 			if (pf->veb[veb_idx] &&
14553 			    pf->veb[veb_idx]->seid == uplink_seid) {
14554 				uplink_veb = pf->veb[veb_idx];
14555 				break;
14556 			}
14557 		}
14558 		if (!uplink_veb) {
14559 			dev_info(&pf->pdev->dev,
14560 				 "uplink seid %d not found\n", uplink_seid);
14561 			return NULL;
14562 		}
14563 	}
14564 
14565 	/* get veb sw struct */
14566 	veb_idx = i40e_veb_mem_alloc(pf);
14567 	if (veb_idx < 0)
14568 		goto err_alloc;
14569 	veb = pf->veb[veb_idx];
14570 	veb->flags = flags;
14571 	veb->uplink_seid = uplink_seid;
14572 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14573 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14574 
14575 	/* create the VEB in the switch */
14576 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14577 	if (ret)
14578 		goto err_veb;
14579 	if (vsi_idx == pf->lan_vsi)
14580 		pf->lan_veb = veb->idx;
14581 
14582 	return veb;
14583 
14584 err_veb:
14585 	i40e_veb_clear(veb);
14586 err_alloc:
14587 	return NULL;
14588 }
14589 
14590 /**
14591  * i40e_setup_pf_switch_element - set PF vars based on switch type
14592  * @pf: board private structure
14593  * @ele: element we are building info from
14594  * @num_reported: total number of elements
14595  * @printconfig: should we print the contents
14596  *
14597  * helper function to assist in extracting a few useful SEID values.
14598  **/
14599 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14600 				struct i40e_aqc_switch_config_element_resp *ele,
14601 				u16 num_reported, bool printconfig)
14602 {
14603 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14604 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14605 	u8 element_type = ele->element_type;
14606 	u16 seid = le16_to_cpu(ele->seid);
14607 
14608 	if (printconfig)
14609 		dev_info(&pf->pdev->dev,
14610 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14611 			 element_type, seid, uplink_seid, downlink_seid);
14612 
14613 	switch (element_type) {
14614 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14615 		pf->mac_seid = seid;
14616 		break;
14617 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14618 		/* Main VEB? */
14619 		if (uplink_seid != pf->mac_seid)
14620 			break;
14621 		if (pf->lan_veb >= I40E_MAX_VEB) {
14622 			int v;
14623 
14624 			/* find existing or else empty VEB */
14625 			for (v = 0; v < I40E_MAX_VEB; v++) {
14626 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14627 					pf->lan_veb = v;
14628 					break;
14629 				}
14630 			}
14631 			if (pf->lan_veb >= I40E_MAX_VEB) {
14632 				v = i40e_veb_mem_alloc(pf);
14633 				if (v < 0)
14634 					break;
14635 				pf->lan_veb = v;
14636 			}
14637 		}
14638 		if (pf->lan_veb >= I40E_MAX_VEB)
14639 			break;
14640 
14641 		pf->veb[pf->lan_veb]->seid = seid;
14642 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14643 		pf->veb[pf->lan_veb]->pf = pf;
14644 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14645 		break;
14646 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14647 		if (num_reported != 1)
14648 			break;
14649 		/* This is immediately after a reset so we can assume this is
14650 		 * the PF's VSI
14651 		 */
14652 		pf->mac_seid = uplink_seid;
14653 		pf->pf_seid = downlink_seid;
14654 		pf->main_vsi_seid = seid;
14655 		if (printconfig)
14656 			dev_info(&pf->pdev->dev,
14657 				 "pf_seid=%d main_vsi_seid=%d\n",
14658 				 pf->pf_seid, pf->main_vsi_seid);
14659 		break;
14660 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14661 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14662 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14663 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14664 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14665 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14666 		/* ignore these for now */
14667 		break;
14668 	default:
14669 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14670 			 element_type, seid);
14671 		break;
14672 	}
14673 }
14674 
14675 /**
14676  * i40e_fetch_switch_configuration - Get switch config from firmware
14677  * @pf: board private structure
14678  * @printconfig: should we print the contents
14679  *
14680  * Get the current switch configuration from the device and
14681  * extract a few useful SEID values.
14682  **/
14683 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14684 {
14685 	struct i40e_aqc_get_switch_config_resp *sw_config;
14686 	u16 next_seid = 0;
14687 	int ret = 0;
14688 	u8 *aq_buf;
14689 	int i;
14690 
14691 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14692 	if (!aq_buf)
14693 		return -ENOMEM;
14694 
14695 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14696 	do {
14697 		u16 num_reported, num_total;
14698 
14699 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14700 						I40E_AQ_LARGE_BUF,
14701 						&next_seid, NULL);
14702 		if (ret) {
14703 			dev_info(&pf->pdev->dev,
14704 				 "get switch config failed err %s aq_err %s\n",
14705 				 i40e_stat_str(&pf->hw, ret),
14706 				 i40e_aq_str(&pf->hw,
14707 					     pf->hw.aq.asq_last_status));
14708 			kfree(aq_buf);
14709 			return -ENOENT;
14710 		}
14711 
14712 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14713 		num_total = le16_to_cpu(sw_config->header.num_total);
14714 
14715 		if (printconfig)
14716 			dev_info(&pf->pdev->dev,
14717 				 "header: %d reported %d total\n",
14718 				 num_reported, num_total);
14719 
14720 		for (i = 0; i < num_reported; i++) {
14721 			struct i40e_aqc_switch_config_element_resp *ele =
14722 				&sw_config->element[i];
14723 
14724 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14725 						     printconfig);
14726 		}
14727 	} while (next_seid != 0);
14728 
14729 	kfree(aq_buf);
14730 	return ret;
14731 }
14732 
14733 /**
14734  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14735  * @pf: board private structure
14736  * @reinit: if the Main VSI needs to re-initialized.
14737  * @lock_acquired: indicates whether or not the lock has been acquired
14738  *
14739  * Returns 0 on success, negative value on failure
14740  **/
14741 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14742 {
14743 	u16 flags = 0;
14744 	int ret;
14745 
14746 	/* find out what's out there already */
14747 	ret = i40e_fetch_switch_configuration(pf, false);
14748 	if (ret) {
14749 		dev_info(&pf->pdev->dev,
14750 			 "couldn't fetch switch config, err %s aq_err %s\n",
14751 			 i40e_stat_str(&pf->hw, ret),
14752 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14753 		return ret;
14754 	}
14755 	i40e_pf_reset_stats(pf);
14756 
14757 	/* set the switch config bit for the whole device to
14758 	 * support limited promisc or true promisc
14759 	 * when user requests promisc. The default is limited
14760 	 * promisc.
14761 	*/
14762 
14763 	if ((pf->hw.pf_id == 0) &&
14764 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14765 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14766 		pf->last_sw_conf_flags = flags;
14767 	}
14768 
14769 	if (pf->hw.pf_id == 0) {
14770 		u16 valid_flags;
14771 
14772 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14773 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14774 						NULL);
14775 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14776 			dev_info(&pf->pdev->dev,
14777 				 "couldn't set switch config bits, err %s aq_err %s\n",
14778 				 i40e_stat_str(&pf->hw, ret),
14779 				 i40e_aq_str(&pf->hw,
14780 					     pf->hw.aq.asq_last_status));
14781 			/* not a fatal problem, just keep going */
14782 		}
14783 		pf->last_sw_conf_valid_flags = valid_flags;
14784 	}
14785 
14786 	/* first time setup */
14787 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14788 		struct i40e_vsi *vsi = NULL;
14789 		u16 uplink_seid;
14790 
14791 		/* Set up the PF VSI associated with the PF's main VSI
14792 		 * that is already in the HW switch
14793 		 */
14794 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14795 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14796 		else
14797 			uplink_seid = pf->mac_seid;
14798 		if (pf->lan_vsi == I40E_NO_VSI)
14799 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14800 		else if (reinit)
14801 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14802 		if (!vsi) {
14803 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14804 			i40e_cloud_filter_exit(pf);
14805 			i40e_fdir_teardown(pf);
14806 			return -EAGAIN;
14807 		}
14808 	} else {
14809 		/* force a reset of TC and queue layout configurations */
14810 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14811 
14812 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14813 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14814 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14815 	}
14816 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14817 
14818 	i40e_fdir_sb_setup(pf);
14819 
14820 	/* Setup static PF queue filter control settings */
14821 	ret = i40e_setup_pf_filter_control(pf);
14822 	if (ret) {
14823 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14824 			 ret);
14825 		/* Failure here should not stop continuing other steps */
14826 	}
14827 
14828 	/* enable RSS in the HW, even for only one queue, as the stack can use
14829 	 * the hash
14830 	 */
14831 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14832 		i40e_pf_config_rss(pf);
14833 
14834 	/* fill in link information and enable LSE reporting */
14835 	i40e_link_event(pf);
14836 
14837 	/* Initialize user-specific link properties */
14838 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14839 				  I40E_AQ_AN_COMPLETED) ? true : false);
14840 
14841 	i40e_ptp_init(pf);
14842 
14843 	if (!lock_acquired)
14844 		rtnl_lock();
14845 
14846 	/* repopulate tunnel port filters */
14847 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14848 
14849 	if (!lock_acquired)
14850 		rtnl_unlock();
14851 
14852 	return ret;
14853 }
14854 
14855 /**
14856  * i40e_determine_queue_usage - Work out queue distribution
14857  * @pf: board private structure
14858  **/
14859 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14860 {
14861 	int queues_left;
14862 	int q_max;
14863 
14864 	pf->num_lan_qps = 0;
14865 
14866 	/* Find the max queues to be put into basic use.  We'll always be
14867 	 * using TC0, whether or not DCB is running, and TC0 will get the
14868 	 * big RSS set.
14869 	 */
14870 	queues_left = pf->hw.func_caps.num_tx_qp;
14871 
14872 	if ((queues_left == 1) ||
14873 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14874 		/* one qp for PF, no queues for anything else */
14875 		queues_left = 0;
14876 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14877 
14878 		/* make sure all the fancies are disabled */
14879 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14880 			       I40E_FLAG_IWARP_ENABLED	|
14881 			       I40E_FLAG_FD_SB_ENABLED	|
14882 			       I40E_FLAG_FD_ATR_ENABLED	|
14883 			       I40E_FLAG_DCB_CAPABLE	|
14884 			       I40E_FLAG_DCB_ENABLED	|
14885 			       I40E_FLAG_SRIOV_ENABLED	|
14886 			       I40E_FLAG_VMDQ_ENABLED);
14887 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14888 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14889 				  I40E_FLAG_FD_SB_ENABLED |
14890 				  I40E_FLAG_FD_ATR_ENABLED |
14891 				  I40E_FLAG_DCB_CAPABLE))) {
14892 		/* one qp for PF */
14893 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14894 		queues_left -= pf->num_lan_qps;
14895 
14896 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14897 			       I40E_FLAG_IWARP_ENABLED	|
14898 			       I40E_FLAG_FD_SB_ENABLED	|
14899 			       I40E_FLAG_FD_ATR_ENABLED	|
14900 			       I40E_FLAG_DCB_ENABLED	|
14901 			       I40E_FLAG_VMDQ_ENABLED);
14902 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14903 	} else {
14904 		/* Not enough queues for all TCs */
14905 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14906 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14907 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14908 					I40E_FLAG_DCB_ENABLED);
14909 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14910 		}
14911 
14912 		/* limit lan qps to the smaller of qps, cpus or msix */
14913 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14914 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14915 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14916 		pf->num_lan_qps = q_max;
14917 
14918 		queues_left -= pf->num_lan_qps;
14919 	}
14920 
14921 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14922 		if (queues_left > 1) {
14923 			queues_left -= 1; /* save 1 queue for FD */
14924 		} else {
14925 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14926 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14927 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14928 		}
14929 	}
14930 
14931 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14932 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14933 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14934 					(queues_left / pf->num_vf_qps));
14935 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14936 	}
14937 
14938 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14939 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14940 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14941 					  (queues_left / pf->num_vmdq_qps));
14942 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14943 	}
14944 
14945 	pf->queues_left = queues_left;
14946 	dev_dbg(&pf->pdev->dev,
14947 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14948 		pf->hw.func_caps.num_tx_qp,
14949 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14950 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14951 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14952 		queues_left);
14953 }
14954 
14955 /**
14956  * i40e_setup_pf_filter_control - Setup PF static filter control
14957  * @pf: PF to be setup
14958  *
14959  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14960  * settings. If PE/FCoE are enabled then it will also set the per PF
14961  * based filter sizes required for them. It also enables Flow director,
14962  * ethertype and macvlan type filter settings for the pf.
14963  *
14964  * Returns 0 on success, negative on failure
14965  **/
14966 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14967 {
14968 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14969 
14970 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14971 
14972 	/* Flow Director is enabled */
14973 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14974 		settings->enable_fdir = true;
14975 
14976 	/* Ethtype and MACVLAN filters enabled for PF */
14977 	settings->enable_ethtype = true;
14978 	settings->enable_macvlan = true;
14979 
14980 	if (i40e_set_filter_control(&pf->hw, settings))
14981 		return -ENOENT;
14982 
14983 	return 0;
14984 }
14985 
14986 #define INFO_STRING_LEN 255
14987 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
14988 static void i40e_print_features(struct i40e_pf *pf)
14989 {
14990 	struct i40e_hw *hw = &pf->hw;
14991 	char *buf;
14992 	int i;
14993 
14994 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14995 	if (!buf)
14996 		return;
14997 
14998 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14999 #ifdef CONFIG_PCI_IOV
15000 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15001 #endif
15002 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15003 		      pf->hw.func_caps.num_vsis,
15004 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15005 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15006 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15007 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15008 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15009 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15010 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15011 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15012 	}
15013 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15014 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15015 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15016 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15017 	if (pf->flags & I40E_FLAG_PTP)
15018 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15019 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15020 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15021 	else
15022 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15023 
15024 	dev_info(&pf->pdev->dev, "%s\n", buf);
15025 	kfree(buf);
15026 	WARN_ON(i > INFO_STRING_LEN);
15027 }
15028 
15029 /**
15030  * i40e_get_platform_mac_addr - get platform-specific MAC address
15031  * @pdev: PCI device information struct
15032  * @pf: board private structure
15033  *
15034  * Look up the MAC address for the device. First we'll try
15035  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15036  * specific fallback. Otherwise, we'll default to the stored value in
15037  * firmware.
15038  **/
15039 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15040 {
15041 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15042 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15043 }
15044 
15045 /**
15046  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15047  * @fec_cfg: FEC option to set in flags
15048  * @flags: ptr to flags in which we set FEC option
15049  **/
15050 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15051 {
15052 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15053 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15054 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15055 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15056 		*flags |= I40E_FLAG_RS_FEC;
15057 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15058 	}
15059 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15060 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15061 		*flags |= I40E_FLAG_BASE_R_FEC;
15062 		*flags &= ~I40E_FLAG_RS_FEC;
15063 	}
15064 	if (fec_cfg == 0)
15065 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15066 }
15067 
15068 /**
15069  * i40e_check_recovery_mode - check if we are running transition firmware
15070  * @pf: board private structure
15071  *
15072  * Check registers indicating the firmware runs in recovery mode. Sets the
15073  * appropriate driver state.
15074  *
15075  * Returns true if the recovery mode was detected, false otherwise
15076  **/
15077 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15078 {
15079 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15080 
15081 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15082 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15083 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15084 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15085 
15086 		return true;
15087 	}
15088 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15089 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15090 
15091 	return false;
15092 }
15093 
15094 /**
15095  * i40e_pf_loop_reset - perform reset in a loop.
15096  * @pf: board private structure
15097  *
15098  * This function is useful when a NIC is about to enter recovery mode.
15099  * When a NIC's internal data structures are corrupted the NIC's
15100  * firmware is going to enter recovery mode.
15101  * Right after a POR it takes about 7 minutes for firmware to enter
15102  * recovery mode. Until that time a NIC is in some kind of intermediate
15103  * state. After that time period the NIC almost surely enters
15104  * recovery mode. The only way for a driver to detect intermediate
15105  * state is to issue a series of pf-resets and check a return value.
15106  * If a PF reset returns success then the firmware could be in recovery
15107  * mode so the caller of this code needs to check for recovery mode
15108  * if this function returns success. There is a little chance that
15109  * firmware will hang in intermediate state forever.
15110  * Since waiting 7 minutes is quite a lot of time this function waits
15111  * 10 seconds and then gives up by returning an error.
15112  *
15113  * Return 0 on success, negative on failure.
15114  **/
15115 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15116 {
15117 	/* wait max 10 seconds for PF reset to succeed */
15118 	const unsigned long time_end = jiffies + 10 * HZ;
15119 
15120 	struct i40e_hw *hw = &pf->hw;
15121 	i40e_status ret;
15122 
15123 	ret = i40e_pf_reset(hw);
15124 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15125 		usleep_range(10000, 20000);
15126 		ret = i40e_pf_reset(hw);
15127 	}
15128 
15129 	if (ret == I40E_SUCCESS)
15130 		pf->pfr_count++;
15131 	else
15132 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15133 
15134 	return ret;
15135 }
15136 
15137 /**
15138  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15139  * @pf: board private structure
15140  *
15141  * Check FW registers to determine if FW issued unexpected EMP Reset.
15142  * Every time when unexpected EMP Reset occurs the FW increments
15143  * a counter of unexpected EMP Resets. When the counter reaches 10
15144  * the FW should enter the Recovery mode
15145  *
15146  * Returns true if FW issued unexpected EMP Reset
15147  **/
15148 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15149 {
15150 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15151 			   I40E_GL_FWSTS_FWS1B_MASK;
15152 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15153 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15154 }
15155 
15156 /**
15157  * i40e_handle_resets - handle EMP resets and PF resets
15158  * @pf: board private structure
15159  *
15160  * Handle both EMP resets and PF resets and conclude whether there are
15161  * any issues regarding these resets. If there are any issues then
15162  * generate log entry.
15163  *
15164  * Return 0 if NIC is healthy or negative value when there are issues
15165  * with resets
15166  **/
15167 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15168 {
15169 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15170 	const bool is_empr = i40e_check_fw_empr(pf);
15171 
15172 	if (is_empr || pfr != I40E_SUCCESS)
15173 		dev_crit(&pf->pdev->dev, "Entering recovery mode due to repeated FW resets. This may take several minutes. Refer to the Intel(R) Ethernet Adapters and Devices User Guide.\n");
15174 
15175 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15176 }
15177 
15178 /**
15179  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15180  * @pf: board private structure
15181  * @hw: ptr to the hardware info
15182  *
15183  * This function does a minimal setup of all subsystems needed for running
15184  * recovery mode.
15185  *
15186  * Returns 0 on success, negative on failure
15187  **/
15188 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15189 {
15190 	struct i40e_vsi *vsi;
15191 	int err;
15192 	int v_idx;
15193 
15194 	pci_save_state(pf->pdev);
15195 
15196 	/* set up periodic task facility */
15197 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15198 	pf->service_timer_period = HZ;
15199 
15200 	INIT_WORK(&pf->service_task, i40e_service_task);
15201 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15202 
15203 	err = i40e_init_interrupt_scheme(pf);
15204 	if (err)
15205 		goto err_switch_setup;
15206 
15207 	/* The number of VSIs reported by the FW is the minimum guaranteed
15208 	 * to us; HW supports far more and we share the remaining pool with
15209 	 * the other PFs. We allocate space for more than the guarantee with
15210 	 * the understanding that we might not get them all later.
15211 	 */
15212 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15213 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15214 	else
15215 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15216 
15217 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15218 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15219 			  GFP_KERNEL);
15220 	if (!pf->vsi) {
15221 		err = -ENOMEM;
15222 		goto err_switch_setup;
15223 	}
15224 
15225 	/* We allocate one VSI which is needed as absolute minimum
15226 	 * in order to register the netdev
15227 	 */
15228 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15229 	if (v_idx < 0) {
15230 		err = v_idx;
15231 		goto err_switch_setup;
15232 	}
15233 	pf->lan_vsi = v_idx;
15234 	vsi = pf->vsi[v_idx];
15235 	if (!vsi) {
15236 		err = -EFAULT;
15237 		goto err_switch_setup;
15238 	}
15239 	vsi->alloc_queue_pairs = 1;
15240 	err = i40e_config_netdev(vsi);
15241 	if (err)
15242 		goto err_switch_setup;
15243 	err = register_netdev(vsi->netdev);
15244 	if (err)
15245 		goto err_switch_setup;
15246 	vsi->netdev_registered = true;
15247 	i40e_dbg_pf_init(pf);
15248 
15249 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15250 	if (err)
15251 		goto err_switch_setup;
15252 
15253 	/* tell the firmware that we're starting */
15254 	i40e_send_version(pf);
15255 
15256 	/* since everything's happy, start the service_task timer */
15257 	mod_timer(&pf->service_timer,
15258 		  round_jiffies(jiffies + pf->service_timer_period));
15259 
15260 	return 0;
15261 
15262 err_switch_setup:
15263 	i40e_reset_interrupt_capability(pf);
15264 	del_timer_sync(&pf->service_timer);
15265 	i40e_shutdown_adminq(hw);
15266 	iounmap(hw->hw_addr);
15267 	pci_disable_pcie_error_reporting(pf->pdev);
15268 	pci_release_mem_regions(pf->pdev);
15269 	pci_disable_device(pf->pdev);
15270 	kfree(pf);
15271 
15272 	return err;
15273 }
15274 
15275 /**
15276  * i40e_set_subsystem_device_id - set subsystem device id
15277  * @hw: pointer to the hardware info
15278  *
15279  * Set PCI subsystem device id either from a pci_dev structure or
15280  * a specific FW register.
15281  **/
15282 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15283 {
15284 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15285 
15286 	hw->subsystem_device_id = pdev->subsystem_device ?
15287 		pdev->subsystem_device :
15288 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15289 }
15290 
15291 /**
15292  * i40e_probe - Device initialization routine
15293  * @pdev: PCI device information struct
15294  * @ent: entry in i40e_pci_tbl
15295  *
15296  * i40e_probe initializes a PF identified by a pci_dev structure.
15297  * The OS initialization, configuring of the PF private structure,
15298  * and a hardware reset occur.
15299  *
15300  * Returns 0 on success, negative on failure
15301  **/
15302 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15303 {
15304 	struct i40e_aq_get_phy_abilities_resp abilities;
15305 #ifdef CONFIG_I40E_DCB
15306 	enum i40e_get_fw_lldp_status_resp lldp_status;
15307 	i40e_status status;
15308 #endif /* CONFIG_I40E_DCB */
15309 	struct i40e_pf *pf;
15310 	struct i40e_hw *hw;
15311 	static u16 pfs_found;
15312 	u16 wol_nvm_bits;
15313 	u16 link_status;
15314 	int err;
15315 	u32 val;
15316 	u32 i;
15317 
15318 	err = pci_enable_device_mem(pdev);
15319 	if (err)
15320 		return err;
15321 
15322 	/* set up for high or low dma */
15323 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15324 	if (err) {
15325 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
15326 		if (err) {
15327 			dev_err(&pdev->dev,
15328 				"DMA configuration failed: 0x%x\n", err);
15329 			goto err_dma;
15330 		}
15331 	}
15332 
15333 	/* set up pci connections */
15334 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15335 	if (err) {
15336 		dev_info(&pdev->dev,
15337 			 "pci_request_selected_regions failed %d\n", err);
15338 		goto err_pci_reg;
15339 	}
15340 
15341 	pci_enable_pcie_error_reporting(pdev);
15342 	pci_set_master(pdev);
15343 
15344 	/* Now that we have a PCI connection, we need to do the
15345 	 * low level device setup.  This is primarily setting up
15346 	 * the Admin Queue structures and then querying for the
15347 	 * device's current profile information.
15348 	 */
15349 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15350 	if (!pf) {
15351 		err = -ENOMEM;
15352 		goto err_pf_alloc;
15353 	}
15354 	pf->next_vsi = 0;
15355 	pf->pdev = pdev;
15356 	set_bit(__I40E_DOWN, pf->state);
15357 
15358 	hw = &pf->hw;
15359 	hw->back = pf;
15360 
15361 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15362 				I40E_MAX_CSR_SPACE);
15363 	/* We believe that the highest register to read is
15364 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15365 	 * is not less than that before mapping to prevent a
15366 	 * kernel panic.
15367 	 */
15368 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15369 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15370 			pf->ioremap_len);
15371 		err = -ENOMEM;
15372 		goto err_ioremap;
15373 	}
15374 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15375 	if (!hw->hw_addr) {
15376 		err = -EIO;
15377 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15378 			 (unsigned int)pci_resource_start(pdev, 0),
15379 			 pf->ioremap_len, err);
15380 		goto err_ioremap;
15381 	}
15382 	hw->vendor_id = pdev->vendor;
15383 	hw->device_id = pdev->device;
15384 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15385 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15386 	i40e_set_subsystem_device_id(hw);
15387 	hw->bus.device = PCI_SLOT(pdev->devfn);
15388 	hw->bus.func = PCI_FUNC(pdev->devfn);
15389 	hw->bus.bus_id = pdev->bus->number;
15390 	pf->instance = pfs_found;
15391 
15392 	/* Select something other than the 802.1ad ethertype for the
15393 	 * switch to use internally and drop on ingress.
15394 	 */
15395 	hw->switch_tag = 0xffff;
15396 	hw->first_tag = ETH_P_8021AD;
15397 	hw->second_tag = ETH_P_8021Q;
15398 
15399 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15400 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15401 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15402 
15403 	/* set up the locks for the AQ, do this only once in probe
15404 	 * and destroy them only once in remove
15405 	 */
15406 	mutex_init(&hw->aq.asq_mutex);
15407 	mutex_init(&hw->aq.arq_mutex);
15408 
15409 	pf->msg_enable = netif_msg_init(debug,
15410 					NETIF_MSG_DRV |
15411 					NETIF_MSG_PROBE |
15412 					NETIF_MSG_LINK);
15413 	if (debug < -1)
15414 		pf->hw.debug_mask = debug;
15415 
15416 	/* do a special CORER for clearing PXE mode once at init */
15417 	if (hw->revision_id == 0 &&
15418 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15419 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15420 		i40e_flush(hw);
15421 		msleep(200);
15422 		pf->corer_count++;
15423 
15424 		i40e_clear_pxe_mode(hw);
15425 	}
15426 
15427 	/* Reset here to make sure all is clean and to define PF 'n' */
15428 	i40e_clear_hw(hw);
15429 
15430 	err = i40e_set_mac_type(hw);
15431 	if (err) {
15432 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15433 			 err);
15434 		goto err_pf_reset;
15435 	}
15436 
15437 	err = i40e_handle_resets(pf);
15438 	if (err)
15439 		goto err_pf_reset;
15440 
15441 	i40e_check_recovery_mode(pf);
15442 
15443 	if (is_kdump_kernel()) {
15444 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15445 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15446 	} else {
15447 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15448 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15449 	}
15450 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15451 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15452 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15453 
15454 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15455 		 "%s-%s:misc",
15456 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15457 
15458 	err = i40e_init_shared_code(hw);
15459 	if (err) {
15460 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15461 			 err);
15462 		goto err_pf_reset;
15463 	}
15464 
15465 	/* set up a default setting for link flow control */
15466 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15467 
15468 	err = i40e_init_adminq(hw);
15469 	if (err) {
15470 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15471 			dev_info(&pdev->dev,
15472 				 "The driver for the device stopped because the NVM image v%u.%u is newer than expected v%u.%u. You must install the most recent version of the network driver.\n",
15473 				 hw->aq.api_maj_ver,
15474 				 hw->aq.api_min_ver,
15475 				 I40E_FW_API_VERSION_MAJOR,
15476 				 I40E_FW_MINOR_VERSION(hw));
15477 		else
15478 			dev_info(&pdev->dev,
15479 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15480 
15481 		goto err_pf_reset;
15482 	}
15483 	i40e_get_oem_version(hw);
15484 
15485 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15486 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15487 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15488 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15489 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15490 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15491 
15492 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15493 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15494 		dev_dbg(&pdev->dev,
15495 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15496 			 hw->aq.api_maj_ver,
15497 			 hw->aq.api_min_ver,
15498 			 I40E_FW_API_VERSION_MAJOR,
15499 			 I40E_FW_MINOR_VERSION(hw));
15500 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15501 		dev_info(&pdev->dev,
15502 			 "The driver for the device detected an older version of the NVM image v%u.%u than expected v%u.%u. Please update the NVM image.\n",
15503 			 hw->aq.api_maj_ver,
15504 			 hw->aq.api_min_ver,
15505 			 I40E_FW_API_VERSION_MAJOR,
15506 			 I40E_FW_MINOR_VERSION(hw));
15507 
15508 	i40e_verify_eeprom(pf);
15509 
15510 	/* Rev 0 hardware was never productized */
15511 	if (hw->revision_id < 1)
15512 		dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
15513 
15514 	i40e_clear_pxe_mode(hw);
15515 
15516 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15517 	if (err)
15518 		goto err_adminq_setup;
15519 
15520 	err = i40e_sw_init(pf);
15521 	if (err) {
15522 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15523 		goto err_sw_init;
15524 	}
15525 
15526 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15527 		return i40e_init_recovery_mode(pf, hw);
15528 
15529 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15530 				hw->func_caps.num_rx_qp, 0, 0);
15531 	if (err) {
15532 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15533 		goto err_init_lan_hmc;
15534 	}
15535 
15536 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15537 	if (err) {
15538 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15539 		err = -ENOENT;
15540 		goto err_configure_lan_hmc;
15541 	}
15542 
15543 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15544 	 * Ignore error return codes because if it was already disabled via
15545 	 * hardware settings this will fail
15546 	 */
15547 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15548 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15549 		i40e_aq_stop_lldp(hw, true, false, NULL);
15550 	}
15551 
15552 	/* allow a platform config to override the HW addr */
15553 	i40e_get_platform_mac_addr(pdev, pf);
15554 
15555 	if (!is_valid_ether_addr(hw->mac.addr)) {
15556 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15557 		err = -EIO;
15558 		goto err_mac_addr;
15559 	}
15560 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15561 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15562 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15563 	if (is_valid_ether_addr(hw->mac.port_addr))
15564 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15565 
15566 	i40e_ptp_alloc_pins(pf);
15567 	pci_set_drvdata(pdev, pf);
15568 	pci_save_state(pdev);
15569 
15570 #ifdef CONFIG_I40E_DCB
15571 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15572 	(!status &&
15573 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15574 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15575 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15576 	dev_info(&pdev->dev,
15577 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15578 			"FW LLDP is disabled\n" :
15579 			"FW LLDP is enabled\n");
15580 
15581 	/* Enable FW to write default DCB config on link-up */
15582 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15583 
15584 	err = i40e_init_pf_dcb(pf);
15585 	if (err) {
15586 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15587 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15588 		/* Continue without DCB enabled */
15589 	}
15590 #endif /* CONFIG_I40E_DCB */
15591 
15592 	/* set up periodic task facility */
15593 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15594 	pf->service_timer_period = HZ;
15595 
15596 	INIT_WORK(&pf->service_task, i40e_service_task);
15597 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15598 
15599 	/* NVM bit on means WoL disabled for the port */
15600 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15601 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15602 		pf->wol_en = false;
15603 	else
15604 		pf->wol_en = true;
15605 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15606 
15607 	/* set up the main switch operations */
15608 	i40e_determine_queue_usage(pf);
15609 	err = i40e_init_interrupt_scheme(pf);
15610 	if (err)
15611 		goto err_switch_setup;
15612 
15613 	/* Reduce Tx and Rx pairs for kdump
15614 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15615 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15616 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15617 	 */
15618 	if (is_kdump_kernel())
15619 		pf->num_lan_msix = 1;
15620 
15621 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15622 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15623 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15624 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15625 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15626 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15627 						    UDP_TUNNEL_TYPE_GENEVE;
15628 
15629 	/* The number of VSIs reported by the FW is the minimum guaranteed
15630 	 * to us; HW supports far more and we share the remaining pool with
15631 	 * the other PFs. We allocate space for more than the guarantee with
15632 	 * the understanding that we might not get them all later.
15633 	 */
15634 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15635 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15636 	else
15637 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15638 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15639 		dev_warn(&pf->pdev->dev,
15640 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15641 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15642 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15643 	}
15644 
15645 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15646 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15647 			  GFP_KERNEL);
15648 	if (!pf->vsi) {
15649 		err = -ENOMEM;
15650 		goto err_switch_setup;
15651 	}
15652 
15653 #ifdef CONFIG_PCI_IOV
15654 	/* prep for VF support */
15655 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15656 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15657 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15658 		if (pci_num_vf(pdev))
15659 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15660 	}
15661 #endif
15662 	err = i40e_setup_pf_switch(pf, false, false);
15663 	if (err) {
15664 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15665 		goto err_vsis;
15666 	}
15667 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15668 
15669 	/* if FDIR VSI was set up, start it now */
15670 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15671 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15672 			i40e_vsi_open(pf->vsi[i]);
15673 			break;
15674 		}
15675 	}
15676 
15677 	/* The driver only wants link up/down and module qualification
15678 	 * reports from firmware.  Note the negative logic.
15679 	 */
15680 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15681 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15682 					 I40E_AQ_EVENT_MEDIA_NA |
15683 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15684 	if (err)
15685 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15686 			 i40e_stat_str(&pf->hw, err),
15687 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15688 
15689 	/* Reconfigure hardware for allowing smaller MSS in the case
15690 	 * of TSO, so that we avoid the MDD being fired and causing
15691 	 * a reset in the case of small MSS+TSO.
15692 	 */
15693 	val = rd32(hw, I40E_REG_MSS);
15694 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15695 		val &= ~I40E_REG_MSS_MIN_MASK;
15696 		val |= I40E_64BYTE_MSS;
15697 		wr32(hw, I40E_REG_MSS, val);
15698 	}
15699 
15700 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15701 		msleep(75);
15702 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15703 		if (err)
15704 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15705 				 i40e_stat_str(&pf->hw, err),
15706 				 i40e_aq_str(&pf->hw,
15707 					     pf->hw.aq.asq_last_status));
15708 	}
15709 	/* The main driver is (mostly) up and happy. We need to set this state
15710 	 * before setting up the misc vector or we get a race and the vector
15711 	 * ends up disabled forever.
15712 	 */
15713 	clear_bit(__I40E_DOWN, pf->state);
15714 
15715 	/* In case of MSIX we are going to setup the misc vector right here
15716 	 * to handle admin queue events etc. In case of legacy and MSI
15717 	 * the misc functionality and queue processing is combined in
15718 	 * the same vector and that gets setup at open.
15719 	 */
15720 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15721 		err = i40e_setup_misc_vector(pf);
15722 		if (err) {
15723 			dev_info(&pdev->dev,
15724 				 "setup of misc vector failed: %d\n", err);
15725 			i40e_cloud_filter_exit(pf);
15726 			i40e_fdir_teardown(pf);
15727 			goto err_vsis;
15728 		}
15729 	}
15730 
15731 #ifdef CONFIG_PCI_IOV
15732 	/* prep for VF support */
15733 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15734 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15735 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15736 		/* disable link interrupts for VFs */
15737 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15738 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15739 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15740 		i40e_flush(hw);
15741 
15742 		if (pci_num_vf(pdev)) {
15743 			dev_info(&pdev->dev,
15744 				 "Active VFs found, allocating resources.\n");
15745 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15746 			if (err)
15747 				dev_info(&pdev->dev,
15748 					 "Error %d allocating resources for existing VFs\n",
15749 					 err);
15750 		}
15751 	}
15752 #endif /* CONFIG_PCI_IOV */
15753 
15754 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15755 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15756 						      pf->num_iwarp_msix,
15757 						      I40E_IWARP_IRQ_PILE_ID);
15758 		if (pf->iwarp_base_vector < 0) {
15759 			dev_info(&pdev->dev,
15760 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15761 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15762 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15763 		}
15764 	}
15765 
15766 	i40e_dbg_pf_init(pf);
15767 
15768 	/* tell the firmware that we're starting */
15769 	i40e_send_version(pf);
15770 
15771 	/* since everything's happy, start the service_task timer */
15772 	mod_timer(&pf->service_timer,
15773 		  round_jiffies(jiffies + pf->service_timer_period));
15774 
15775 	/* add this PF to client device list and launch a client service task */
15776 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15777 		err = i40e_lan_add_device(pf);
15778 		if (err)
15779 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15780 				 err);
15781 	}
15782 
15783 #define PCI_SPEED_SIZE 8
15784 #define PCI_WIDTH_SIZE 8
15785 	/* Devices on the IOSF bus do not have this information
15786 	 * and will report PCI Gen 1 x 1 by default so don't bother
15787 	 * checking them.
15788 	 */
15789 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15790 		char speed[PCI_SPEED_SIZE] = "Unknown";
15791 		char width[PCI_WIDTH_SIZE] = "Unknown";
15792 
15793 		/* Get the negotiated link width and speed from PCI config
15794 		 * space
15795 		 */
15796 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15797 					  &link_status);
15798 
15799 		i40e_set_pci_config_data(hw, link_status);
15800 
15801 		switch (hw->bus.speed) {
15802 		case i40e_bus_speed_8000:
15803 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15804 		case i40e_bus_speed_5000:
15805 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15806 		case i40e_bus_speed_2500:
15807 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15808 		default:
15809 			break;
15810 		}
15811 		switch (hw->bus.width) {
15812 		case i40e_bus_width_pcie_x8:
15813 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15814 		case i40e_bus_width_pcie_x4:
15815 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15816 		case i40e_bus_width_pcie_x2:
15817 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15818 		case i40e_bus_width_pcie_x1:
15819 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15820 		default:
15821 			break;
15822 		}
15823 
15824 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15825 			 speed, width);
15826 
15827 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15828 		    hw->bus.speed < i40e_bus_speed_8000) {
15829 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15830 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15831 		}
15832 	}
15833 
15834 	/* get the requested speeds from the fw */
15835 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15836 	if (err)
15837 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15838 			i40e_stat_str(&pf->hw, err),
15839 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15840 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15841 
15842 	/* set the FEC config due to the board capabilities */
15843 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15844 
15845 	/* get the supported phy types from the fw */
15846 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15847 	if (err)
15848 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15849 			i40e_stat_str(&pf->hw, err),
15850 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15851 
15852 	/* make sure the MFS hasn't been set lower than the default */
15853 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15854 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15855 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15856 	if (val < MAX_FRAME_SIZE_DEFAULT)
15857 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15858 			 i, val);
15859 
15860 	/* Add a filter to drop all Flow control frames from any VSI from being
15861 	 * transmitted. By doing so we stop a malicious VF from sending out
15862 	 * PAUSE or PFC frames and potentially controlling traffic for other
15863 	 * PF/VF VSIs.
15864 	 * The FW can still send Flow control frames if enabled.
15865 	 */
15866 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15867 						       pf->main_vsi_seid);
15868 
15869 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15870 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15871 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15872 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15873 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15874 	/* print a string summarizing features */
15875 	i40e_print_features(pf);
15876 
15877 	return 0;
15878 
15879 	/* Unwind what we've done if something failed in the setup */
15880 err_vsis:
15881 	set_bit(__I40E_DOWN, pf->state);
15882 	i40e_clear_interrupt_scheme(pf);
15883 	kfree(pf->vsi);
15884 err_switch_setup:
15885 	i40e_reset_interrupt_capability(pf);
15886 	del_timer_sync(&pf->service_timer);
15887 err_mac_addr:
15888 err_configure_lan_hmc:
15889 	(void)i40e_shutdown_lan_hmc(hw);
15890 err_init_lan_hmc:
15891 	kfree(pf->qp_pile);
15892 err_sw_init:
15893 err_adminq_setup:
15894 err_pf_reset:
15895 	iounmap(hw->hw_addr);
15896 err_ioremap:
15897 	kfree(pf);
15898 err_pf_alloc:
15899 	pci_disable_pcie_error_reporting(pdev);
15900 	pci_release_mem_regions(pdev);
15901 err_pci_reg:
15902 err_dma:
15903 	pci_disable_device(pdev);
15904 	return err;
15905 }
15906 
15907 /**
15908  * i40e_remove - Device removal routine
15909  * @pdev: PCI device information struct
15910  *
15911  * i40e_remove is called by the PCI subsystem to alert the driver
15912  * that is should release a PCI device.  This could be caused by a
15913  * Hot-Plug event, or because the driver is going to be removed from
15914  * memory.
15915  **/
15916 static void i40e_remove(struct pci_dev *pdev)
15917 {
15918 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15919 	struct i40e_hw *hw = &pf->hw;
15920 	i40e_status ret_code;
15921 	int i;
15922 
15923 	i40e_dbg_pf_exit(pf);
15924 
15925 	i40e_ptp_stop(pf);
15926 
15927 	/* Disable RSS in hw */
15928 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15929 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15930 
15931 	while (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15932 		usleep_range(1000, 2000);
15933 
15934 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15935 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15936 		i40e_free_vfs(pf);
15937 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15938 	}
15939 	/* no more scheduling of any task */
15940 	set_bit(__I40E_SUSPENDED, pf->state);
15941 	set_bit(__I40E_DOWN, pf->state);
15942 	if (pf->service_timer.function)
15943 		del_timer_sync(&pf->service_timer);
15944 	if (pf->service_task.func)
15945 		cancel_work_sync(&pf->service_task);
15946 
15947 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15948 		struct i40e_vsi *vsi = pf->vsi[0];
15949 
15950 		/* We know that we have allocated only one vsi for this PF,
15951 		 * it was just for registering netdevice, so the interface
15952 		 * could be visible in the 'ifconfig' output
15953 		 */
15954 		unregister_netdev(vsi->netdev);
15955 		free_netdev(vsi->netdev);
15956 
15957 		goto unmap;
15958 	}
15959 
15960 	/* Client close must be called explicitly here because the timer
15961 	 * has been stopped.
15962 	 */
15963 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15964 
15965 	i40e_fdir_teardown(pf);
15966 
15967 	/* If there is a switch structure or any orphans, remove them.
15968 	 * This will leave only the PF's VSI remaining.
15969 	 */
15970 	for (i = 0; i < I40E_MAX_VEB; i++) {
15971 		if (!pf->veb[i])
15972 			continue;
15973 
15974 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15975 		    pf->veb[i]->uplink_seid == 0)
15976 			i40e_switch_branch_release(pf->veb[i]);
15977 	}
15978 
15979 	/* Now we can shutdown the PF's VSI, just before we kill
15980 	 * adminq and hmc.
15981 	 */
15982 	if (pf->vsi[pf->lan_vsi])
15983 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15984 
15985 	i40e_cloud_filter_exit(pf);
15986 
15987 	/* remove attached clients */
15988 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15989 		ret_code = i40e_lan_del_device(pf);
15990 		if (ret_code)
15991 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15992 				 ret_code);
15993 	}
15994 
15995 	/* shutdown and destroy the HMC */
15996 	if (hw->hmc.hmc_obj) {
15997 		ret_code = i40e_shutdown_lan_hmc(hw);
15998 		if (ret_code)
15999 			dev_warn(&pdev->dev,
16000 				 "Failed to destroy the HMC resources: %d\n",
16001 				 ret_code);
16002 	}
16003 
16004 unmap:
16005 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16006 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16007 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16008 		free_irq(pf->pdev->irq, pf);
16009 
16010 	/* shutdown the adminq */
16011 	i40e_shutdown_adminq(hw);
16012 
16013 	/* destroy the locks only once, here */
16014 	mutex_destroy(&hw->aq.arq_mutex);
16015 	mutex_destroy(&hw->aq.asq_mutex);
16016 
16017 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16018 	rtnl_lock();
16019 	i40e_clear_interrupt_scheme(pf);
16020 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16021 		if (pf->vsi[i]) {
16022 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16023 				i40e_vsi_clear_rings(pf->vsi[i]);
16024 			i40e_vsi_clear(pf->vsi[i]);
16025 			pf->vsi[i] = NULL;
16026 		}
16027 	}
16028 	rtnl_unlock();
16029 
16030 	for (i = 0; i < I40E_MAX_VEB; i++) {
16031 		kfree(pf->veb[i]);
16032 		pf->veb[i] = NULL;
16033 	}
16034 
16035 	kfree(pf->qp_pile);
16036 	kfree(pf->vsi);
16037 
16038 	iounmap(hw->hw_addr);
16039 	kfree(pf);
16040 	pci_release_mem_regions(pdev);
16041 
16042 	pci_disable_pcie_error_reporting(pdev);
16043 	pci_disable_device(pdev);
16044 }
16045 
16046 /**
16047  * i40e_pci_error_detected - warning that something funky happened in PCI land
16048  * @pdev: PCI device information struct
16049  * @error: the type of PCI error
16050  *
16051  * Called to warn that something happened and the error handling steps
16052  * are in progress.  Allows the driver to quiesce things, be ready for
16053  * remediation.
16054  **/
16055 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16056 						pci_channel_state_t error)
16057 {
16058 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16059 
16060 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16061 
16062 	if (!pf) {
16063 		dev_info(&pdev->dev,
16064 			 "Cannot recover - error happened during device probe\n");
16065 		return PCI_ERS_RESULT_DISCONNECT;
16066 	}
16067 
16068 	/* shutdown all operations */
16069 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16070 		i40e_prep_for_reset(pf);
16071 
16072 	/* Request a slot reset */
16073 	return PCI_ERS_RESULT_NEED_RESET;
16074 }
16075 
16076 /**
16077  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16078  * @pdev: PCI device information struct
16079  *
16080  * Called to find if the driver can work with the device now that
16081  * the pci slot has been reset.  If a basic connection seems good
16082  * (registers are readable and have sane content) then return a
16083  * happy little PCI_ERS_RESULT_xxx.
16084  **/
16085 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16086 {
16087 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16088 	pci_ers_result_t result;
16089 	u32 reg;
16090 
16091 	dev_dbg(&pdev->dev, "%s\n", __func__);
16092 	if (pci_enable_device_mem(pdev)) {
16093 		dev_info(&pdev->dev,
16094 			 "Cannot re-enable PCI device after reset.\n");
16095 		result = PCI_ERS_RESULT_DISCONNECT;
16096 	} else {
16097 		pci_set_master(pdev);
16098 		pci_restore_state(pdev);
16099 		pci_save_state(pdev);
16100 		pci_wake_from_d3(pdev, false);
16101 
16102 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16103 		if (reg == 0)
16104 			result = PCI_ERS_RESULT_RECOVERED;
16105 		else
16106 			result = PCI_ERS_RESULT_DISCONNECT;
16107 	}
16108 
16109 	return result;
16110 }
16111 
16112 /**
16113  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16114  * @pdev: PCI device information struct
16115  */
16116 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16117 {
16118 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16119 
16120 	i40e_prep_for_reset(pf);
16121 }
16122 
16123 /**
16124  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16125  * @pdev: PCI device information struct
16126  */
16127 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16128 {
16129 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16130 
16131 	i40e_reset_and_rebuild(pf, false, false);
16132 }
16133 
16134 /**
16135  * i40e_pci_error_resume - restart operations after PCI error recovery
16136  * @pdev: PCI device information struct
16137  *
16138  * Called to allow the driver to bring things back up after PCI error
16139  * and/or reset recovery has finished.
16140  **/
16141 static void i40e_pci_error_resume(struct pci_dev *pdev)
16142 {
16143 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16144 
16145 	dev_dbg(&pdev->dev, "%s\n", __func__);
16146 	if (test_bit(__I40E_SUSPENDED, pf->state))
16147 		return;
16148 
16149 	i40e_handle_reset_warning(pf, false);
16150 }
16151 
16152 /**
16153  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16154  * using the mac_address_write admin q function
16155  * @pf: pointer to i40e_pf struct
16156  **/
16157 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16158 {
16159 	struct i40e_hw *hw = &pf->hw;
16160 	i40e_status ret;
16161 	u8 mac_addr[6];
16162 	u16 flags = 0;
16163 
16164 	/* Get current MAC address in case it's an LAA */
16165 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16166 		ether_addr_copy(mac_addr,
16167 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16168 	} else {
16169 		dev_err(&pf->pdev->dev,
16170 			"Failed to retrieve MAC address; using default\n");
16171 		ether_addr_copy(mac_addr, hw->mac.addr);
16172 	}
16173 
16174 	/* The FW expects the mac address write cmd to first be called with
16175 	 * one of these flags before calling it again with the multicast
16176 	 * enable flags.
16177 	 */
16178 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16179 
16180 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16181 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16182 
16183 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16184 	if (ret) {
16185 		dev_err(&pf->pdev->dev,
16186 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16187 		return;
16188 	}
16189 
16190 	flags = I40E_AQC_MC_MAG_EN
16191 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16192 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16193 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16194 	if (ret)
16195 		dev_err(&pf->pdev->dev,
16196 			"Failed to enable Multicast Magic Packet wake up\n");
16197 }
16198 
16199 /**
16200  * i40e_shutdown - PCI callback for shutting down
16201  * @pdev: PCI device information struct
16202  **/
16203 static void i40e_shutdown(struct pci_dev *pdev)
16204 {
16205 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16206 	struct i40e_hw *hw = &pf->hw;
16207 
16208 	set_bit(__I40E_SUSPENDED, pf->state);
16209 	set_bit(__I40E_DOWN, pf->state);
16210 
16211 	del_timer_sync(&pf->service_timer);
16212 	cancel_work_sync(&pf->service_task);
16213 	i40e_cloud_filter_exit(pf);
16214 	i40e_fdir_teardown(pf);
16215 
16216 	/* Client close must be called explicitly here because the timer
16217 	 * has been stopped.
16218 	 */
16219 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16220 
16221 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16222 		i40e_enable_mc_magic_wake(pf);
16223 
16224 	i40e_prep_for_reset(pf);
16225 
16226 	wr32(hw, I40E_PFPM_APM,
16227 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16228 	wr32(hw, I40E_PFPM_WUFC,
16229 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16230 
16231 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16232 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16233 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16234 		free_irq(pf->pdev->irq, pf);
16235 
16236 	/* Since we're going to destroy queues during the
16237 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16238 	 * whole section
16239 	 */
16240 	rtnl_lock();
16241 	i40e_clear_interrupt_scheme(pf);
16242 	rtnl_unlock();
16243 
16244 	if (system_state == SYSTEM_POWER_OFF) {
16245 		pci_wake_from_d3(pdev, pf->wol_en);
16246 		pci_set_power_state(pdev, PCI_D3hot);
16247 	}
16248 }
16249 
16250 /**
16251  * i40e_suspend - PM callback for moving to D3
16252  * @dev: generic device information structure
16253  **/
16254 static int __maybe_unused i40e_suspend(struct device *dev)
16255 {
16256 	struct i40e_pf *pf = dev_get_drvdata(dev);
16257 	struct i40e_hw *hw = &pf->hw;
16258 
16259 	/* If we're already suspended, then there is nothing to do */
16260 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16261 		return 0;
16262 
16263 	set_bit(__I40E_DOWN, pf->state);
16264 
16265 	/* Ensure service task will not be running */
16266 	del_timer_sync(&pf->service_timer);
16267 	cancel_work_sync(&pf->service_task);
16268 
16269 	/* Client close must be called explicitly here because the timer
16270 	 * has been stopped.
16271 	 */
16272 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16273 
16274 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16275 		i40e_enable_mc_magic_wake(pf);
16276 
16277 	/* Since we're going to destroy queues during the
16278 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16279 	 * whole section
16280 	 */
16281 	rtnl_lock();
16282 
16283 	i40e_prep_for_reset(pf);
16284 
16285 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16286 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16287 
16288 	/* Clear the interrupt scheme and release our IRQs so that the system
16289 	 * can safely hibernate even when there are a large number of CPUs.
16290 	 * Otherwise hibernation might fail when mapping all the vectors back
16291 	 * to CPU0.
16292 	 */
16293 	i40e_clear_interrupt_scheme(pf);
16294 
16295 	rtnl_unlock();
16296 
16297 	return 0;
16298 }
16299 
16300 /**
16301  * i40e_resume - PM callback for waking up from D3
16302  * @dev: generic device information structure
16303  **/
16304 static int __maybe_unused i40e_resume(struct device *dev)
16305 {
16306 	struct i40e_pf *pf = dev_get_drvdata(dev);
16307 	int err;
16308 
16309 	/* If we're not suspended, then there is nothing to do */
16310 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16311 		return 0;
16312 
16313 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16314 	 * since we're going to be restoring queues
16315 	 */
16316 	rtnl_lock();
16317 
16318 	/* We cleared the interrupt scheme when we suspended, so we need to
16319 	 * restore it now to resume device functionality.
16320 	 */
16321 	err = i40e_restore_interrupt_scheme(pf);
16322 	if (err) {
16323 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16324 			err);
16325 	}
16326 
16327 	clear_bit(__I40E_DOWN, pf->state);
16328 	i40e_reset_and_rebuild(pf, false, true);
16329 
16330 	rtnl_unlock();
16331 
16332 	/* Clear suspended state last after everything is recovered */
16333 	clear_bit(__I40E_SUSPENDED, pf->state);
16334 
16335 	/* Restart the service task */
16336 	mod_timer(&pf->service_timer,
16337 		  round_jiffies(jiffies + pf->service_timer_period));
16338 
16339 	return 0;
16340 }
16341 
16342 static const struct pci_error_handlers i40e_err_handler = {
16343 	.error_detected = i40e_pci_error_detected,
16344 	.slot_reset = i40e_pci_error_slot_reset,
16345 	.reset_prepare = i40e_pci_error_reset_prepare,
16346 	.reset_done = i40e_pci_error_reset_done,
16347 	.resume = i40e_pci_error_resume,
16348 };
16349 
16350 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16351 
16352 static struct pci_driver i40e_driver = {
16353 	.name     = i40e_driver_name,
16354 	.id_table = i40e_pci_tbl,
16355 	.probe    = i40e_probe,
16356 	.remove   = i40e_remove,
16357 	.driver   = {
16358 		.pm = &i40e_pm_ops,
16359 	},
16360 	.shutdown = i40e_shutdown,
16361 	.err_handler = &i40e_err_handler,
16362 	.sriov_configure = i40e_pci_sriov_configure,
16363 };
16364 
16365 /**
16366  * i40e_init_module - Driver registration routine
16367  *
16368  * i40e_init_module is the first routine called when the driver is
16369  * loaded. All it does is register with the PCI subsystem.
16370  **/
16371 static int __init i40e_init_module(void)
16372 {
16373 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16374 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16375 
16376 	/* There is no need to throttle the number of active tasks because
16377 	 * each device limits its own task using a state bit for scheduling
16378 	 * the service task, and the device tasks do not interfere with each
16379 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16380 	 * since we need to be able to guarantee forward progress even under
16381 	 * memory pressure.
16382 	 */
16383 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16384 	if (!i40e_wq) {
16385 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16386 		return -ENOMEM;
16387 	}
16388 
16389 	i40e_dbg_init();
16390 	return pci_register_driver(&i40e_driver);
16391 }
16392 module_init(i40e_init_module);
16393 
16394 /**
16395  * i40e_exit_module - Driver exit cleanup routine
16396  *
16397  * i40e_exit_module is called just before the driver is removed
16398  * from memory.
16399  **/
16400 static void __exit i40e_exit_module(void)
16401 {
16402 	pci_unregister_driver(&i40e_driver);
16403 	destroy_workqueue(i40e_wq);
16404 	ida_destroy(&i40e_client_ida);
16405 	i40e_dbg_exit();
16406 }
16407 module_exit(i40e_exit_module);
16408