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_SFP_X722_A), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
87 	/* required last entry */
88 	{0, }
89 };
90 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
91 
92 #define I40E_MAX_VF_COUNT 128
93 static int debug = -1;
94 module_param(debug, uint, 0);
95 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
96 
97 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
98 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
99 MODULE_LICENSE("GPL v2");
100 
101 static struct workqueue_struct *i40e_wq;
102 
103 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
104 				  struct net_device *netdev, int delta)
105 {
106 	struct netdev_hw_addr *ha;
107 
108 	if (!f || !netdev)
109 		return;
110 
111 	netdev_for_each_mc_addr(ha, netdev) {
112 		if (ether_addr_equal(ha->addr, f->macaddr)) {
113 			ha->refcount += delta;
114 			if (ha->refcount <= 0)
115 				ha->refcount = 1;
116 			break;
117 		}
118 	}
119 }
120 
121 /**
122  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
123  * @hw:   pointer to the HW structure
124  * @mem:  ptr to mem struct to fill out
125  * @size: size of memory requested
126  * @alignment: what to align the allocation to
127  **/
128 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
129 			    u64 size, u32 alignment)
130 {
131 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
132 
133 	mem->size = ALIGN(size, alignment);
134 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
135 				     GFP_KERNEL);
136 	if (!mem->va)
137 		return -ENOMEM;
138 
139 	return 0;
140 }
141 
142 /**
143  * i40e_free_dma_mem_d - OS specific memory free for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to free
146  **/
147 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
148 {
149 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
150 
151 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
152 	mem->va = NULL;
153 	mem->pa = 0;
154 	mem->size = 0;
155 
156 	return 0;
157 }
158 
159 /**
160  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
161  * @hw:   pointer to the HW structure
162  * @mem:  ptr to mem struct to fill out
163  * @size: size of memory requested
164  **/
165 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
166 			     u32 size)
167 {
168 	mem->size = size;
169 	mem->va = kzalloc(size, GFP_KERNEL);
170 
171 	if (!mem->va)
172 		return -ENOMEM;
173 
174 	return 0;
175 }
176 
177 /**
178  * i40e_free_virt_mem_d - OS specific memory free for shared code
179  * @hw:   pointer to the HW structure
180  * @mem:  ptr to mem struct to free
181  **/
182 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
183 {
184 	/* it's ok to kfree a NULL pointer */
185 	kfree(mem->va);
186 	mem->va = NULL;
187 	mem->size = 0;
188 
189 	return 0;
190 }
191 
192 /**
193  * i40e_get_lump - find a lump of free generic resource
194  * @pf: board private structure
195  * @pile: the pile of resource to search
196  * @needed: the number of items needed
197  * @id: an owner id to stick on the items assigned
198  *
199  * Returns the base item index of the lump, or negative for error
200  **/
201 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
202 			 u16 needed, u16 id)
203 {
204 	int ret = -ENOMEM;
205 	int i, j;
206 
207 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
208 		dev_info(&pf->pdev->dev,
209 			 "param err: pile=%s needed=%d id=0x%04x\n",
210 			 pile ? "<valid>" : "<null>", needed, id);
211 		return -EINVAL;
212 	}
213 
214 	/* Allocate last queue in the pile for FDIR VSI queue
215 	 * so it doesn't fragment the qp_pile
216 	 */
217 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
218 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
219 			dev_err(&pf->pdev->dev,
220 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
221 				pile->num_entries - 1);
222 			return -ENOMEM;
223 		}
224 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
225 		return pile->num_entries - 1;
226 	}
227 
228 	i = 0;
229 	while (i < pile->num_entries) {
230 		/* skip already allocated entries */
231 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
232 			i++;
233 			continue;
234 		}
235 
236 		/* do we have enough in this lump? */
237 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
238 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
239 				break;
240 		}
241 
242 		if (j == needed) {
243 			/* there was enough, so assign it to the requestor */
244 			for (j = 0; j < needed; j++)
245 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
246 			ret = i;
247 			break;
248 		}
249 
250 		/* not enough, so skip over it and continue looking */
251 		i += j;
252 	}
253 
254 	return ret;
255 }
256 
257 /**
258  * i40e_put_lump - return a lump of generic resource
259  * @pile: the pile of resource to search
260  * @index: the base item index
261  * @id: the owner id of the items assigned
262  *
263  * Returns the count of items in the lump
264  **/
265 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
266 {
267 	int valid_id = (id | I40E_PILE_VALID_BIT);
268 	int count = 0;
269 	u16 i;
270 
271 	if (!pile || index >= pile->num_entries)
272 		return -EINVAL;
273 
274 	for (i = index;
275 	     i < pile->num_entries && pile->list[i] == valid_id;
276 	     i++) {
277 		pile->list[i] = 0;
278 		count++;
279 	}
280 
281 
282 	return count;
283 }
284 
285 /**
286  * i40e_find_vsi_from_id - searches for the vsi with the given id
287  * @pf: the pf structure to search for the vsi
288  * @id: id of the vsi it is searching for
289  **/
290 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
291 {
292 	int i;
293 
294 	for (i = 0; i < pf->num_alloc_vsi; i++)
295 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
296 			return pf->vsi[i];
297 
298 	return NULL;
299 }
300 
301 /**
302  * i40e_service_event_schedule - Schedule the service task to wake up
303  * @pf: board private structure
304  *
305  * If not already scheduled, this puts the task into the work queue
306  **/
307 void i40e_service_event_schedule(struct i40e_pf *pf)
308 {
309 	if ((!test_bit(__I40E_DOWN, pf->state) &&
310 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
311 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
312 		queue_work(i40e_wq, &pf->service_task);
313 }
314 
315 /**
316  * i40e_tx_timeout - Respond to a Tx Hang
317  * @netdev: network interface device structure
318  * @txqueue: queue number timing out
319  *
320  * If any port has noticed a Tx timeout, it is likely that the whole
321  * device is munged, not just the one netdev port, so go for the full
322  * reset.
323  **/
324 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
325 {
326 	struct i40e_netdev_priv *np = netdev_priv(netdev);
327 	struct i40e_vsi *vsi = np->vsi;
328 	struct i40e_pf *pf = vsi->back;
329 	struct i40e_ring *tx_ring = NULL;
330 	unsigned int i;
331 	u32 head, val;
332 
333 	pf->tx_timeout_count++;
334 
335 	/* with txqueue index, find the tx_ring struct */
336 	for (i = 0; i < vsi->num_queue_pairs; i++) {
337 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
338 			if (txqueue ==
339 			    vsi->tx_rings[i]->queue_index) {
340 				tx_ring = vsi->tx_rings[i];
341 				break;
342 			}
343 		}
344 	}
345 
346 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
347 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
348 	else if (time_before(jiffies,
349 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
350 		return;   /* don't do any new action before the next timeout */
351 
352 	/* don't kick off another recovery if one is already pending */
353 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
354 		return;
355 
356 	if (tx_ring) {
357 		head = i40e_get_head(tx_ring);
358 		/* Read interrupt register */
359 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
360 			val = rd32(&pf->hw,
361 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
362 						tx_ring->vsi->base_vector - 1));
363 		else
364 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
365 
366 		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",
367 			    vsi->seid, txqueue, tx_ring->next_to_clean,
368 			    head, tx_ring->next_to_use,
369 			    readl(tx_ring->tail), val);
370 	}
371 
372 	pf->tx_timeout_last_recovery = jiffies;
373 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
374 		    pf->tx_timeout_recovery_level, txqueue);
375 
376 	switch (pf->tx_timeout_recovery_level) {
377 	case 1:
378 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
379 		break;
380 	case 2:
381 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
382 		break;
383 	case 3:
384 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
385 		break;
386 	default:
387 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
388 		break;
389 	}
390 
391 	i40e_service_event_schedule(pf);
392 	pf->tx_timeout_recovery_level++;
393 }
394 
395 /**
396  * i40e_get_vsi_stats_struct - Get System Network Statistics
397  * @vsi: the VSI we care about
398  *
399  * Returns the address of the device statistics structure.
400  * The statistics are actually updated from the service task.
401  **/
402 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
403 {
404 	return &vsi->net_stats;
405 }
406 
407 /**
408  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
409  * @ring: Tx ring to get statistics from
410  * @stats: statistics entry to be updated
411  **/
412 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
413 					    struct rtnl_link_stats64 *stats)
414 {
415 	u64 bytes, packets;
416 	unsigned int start;
417 
418 	do {
419 		start = u64_stats_fetch_begin_irq(&ring->syncp);
420 		packets = ring->stats.packets;
421 		bytes   = ring->stats.bytes;
422 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
423 
424 	stats->tx_packets += packets;
425 	stats->tx_bytes   += bytes;
426 }
427 
428 /**
429  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
430  * @netdev: network interface device structure
431  * @stats: data structure to store statistics
432  *
433  * Returns the address of the device statistics structure.
434  * The statistics are actually updated from the service task.
435  **/
436 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
437 				  struct rtnl_link_stats64 *stats)
438 {
439 	struct i40e_netdev_priv *np = netdev_priv(netdev);
440 	struct i40e_vsi *vsi = np->vsi;
441 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
442 	struct i40e_ring *ring;
443 	int i;
444 
445 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
446 		return;
447 
448 	if (!vsi->tx_rings)
449 		return;
450 
451 	rcu_read_lock();
452 	for (i = 0; i < vsi->num_queue_pairs; i++) {
453 		u64 bytes, packets;
454 		unsigned int start;
455 
456 		ring = READ_ONCE(vsi->tx_rings[i]);
457 		if (!ring)
458 			continue;
459 		i40e_get_netdev_stats_struct_tx(ring, stats);
460 
461 		if (i40e_enabled_xdp_vsi(vsi)) {
462 			ring = READ_ONCE(vsi->xdp_rings[i]);
463 			if (!ring)
464 				continue;
465 			i40e_get_netdev_stats_struct_tx(ring, stats);
466 		}
467 
468 		ring = READ_ONCE(vsi->rx_rings[i]);
469 		if (!ring)
470 			continue;
471 		do {
472 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
473 			packets = ring->stats.packets;
474 			bytes   = ring->stats.bytes;
475 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
476 
477 		stats->rx_packets += packets;
478 		stats->rx_bytes   += bytes;
479 
480 	}
481 	rcu_read_unlock();
482 
483 	/* following stats updated by i40e_watchdog_subtask() */
484 	stats->multicast	= vsi_stats->multicast;
485 	stats->tx_errors	= vsi_stats->tx_errors;
486 	stats->tx_dropped	= vsi_stats->tx_dropped;
487 	stats->rx_errors	= vsi_stats->rx_errors;
488 	stats->rx_dropped	= vsi_stats->rx_dropped;
489 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
490 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
491 }
492 
493 /**
494  * i40e_vsi_reset_stats - Resets all stats of the given vsi
495  * @vsi: the VSI to have its stats reset
496  **/
497 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
498 {
499 	struct rtnl_link_stats64 *ns;
500 	int i;
501 
502 	if (!vsi)
503 		return;
504 
505 	ns = i40e_get_vsi_stats_struct(vsi);
506 	memset(ns, 0, sizeof(*ns));
507 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
508 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
509 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
510 	if (vsi->rx_rings && vsi->rx_rings[0]) {
511 		for (i = 0; i < vsi->num_queue_pairs; i++) {
512 			memset(&vsi->rx_rings[i]->stats, 0,
513 			       sizeof(vsi->rx_rings[i]->stats));
514 			memset(&vsi->rx_rings[i]->rx_stats, 0,
515 			       sizeof(vsi->rx_rings[i]->rx_stats));
516 			memset(&vsi->tx_rings[i]->stats, 0,
517 			       sizeof(vsi->tx_rings[i]->stats));
518 			memset(&vsi->tx_rings[i]->tx_stats, 0,
519 			       sizeof(vsi->tx_rings[i]->tx_stats));
520 		}
521 	}
522 	vsi->stat_offsets_loaded = false;
523 }
524 
525 /**
526  * i40e_pf_reset_stats - Reset all of the stats for the given PF
527  * @pf: the PF to be reset
528  **/
529 void i40e_pf_reset_stats(struct i40e_pf *pf)
530 {
531 	int i;
532 
533 	memset(&pf->stats, 0, sizeof(pf->stats));
534 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
535 	pf->stat_offsets_loaded = false;
536 
537 	for (i = 0; i < I40E_MAX_VEB; i++) {
538 		if (pf->veb[i]) {
539 			memset(&pf->veb[i]->stats, 0,
540 			       sizeof(pf->veb[i]->stats));
541 			memset(&pf->veb[i]->stats_offsets, 0,
542 			       sizeof(pf->veb[i]->stats_offsets));
543 			memset(&pf->veb[i]->tc_stats, 0,
544 			       sizeof(pf->veb[i]->tc_stats));
545 			memset(&pf->veb[i]->tc_stats_offsets, 0,
546 			       sizeof(pf->veb[i]->tc_stats_offsets));
547 			pf->veb[i]->stat_offsets_loaded = false;
548 		}
549 	}
550 	pf->hw_csum_rx_error = 0;
551 }
552 
553 /**
554  * i40e_stat_update48 - read and update a 48 bit stat from the chip
555  * @hw: ptr to the hardware info
556  * @hireg: the high 32 bit reg to read
557  * @loreg: the low 32 bit reg to read
558  * @offset_loaded: has the initial offset been loaded yet
559  * @offset: ptr to current offset value
560  * @stat: ptr to the stat
561  *
562  * Since the device stats are not reset at PFReset, they likely will not
563  * be zeroed when the driver starts.  We'll save the first values read
564  * and use them as offsets to be subtracted from the raw values in order
565  * to report stats that count from zero.  In the process, we also manage
566  * the potential roll-over.
567  **/
568 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
569 			       bool offset_loaded, u64 *offset, u64 *stat)
570 {
571 	u64 new_data;
572 
573 	if (hw->device_id == I40E_DEV_ID_QEMU) {
574 		new_data = rd32(hw, loreg);
575 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
576 	} else {
577 		new_data = rd64(hw, loreg);
578 	}
579 	if (!offset_loaded)
580 		*offset = new_data;
581 	if (likely(new_data >= *offset))
582 		*stat = new_data - *offset;
583 	else
584 		*stat = (new_data + BIT_ULL(48)) - *offset;
585 	*stat &= 0xFFFFFFFFFFFFULL;
586 }
587 
588 /**
589  * i40e_stat_update32 - read and update a 32 bit stat from the chip
590  * @hw: ptr to the hardware info
591  * @reg: the hw reg to read
592  * @offset_loaded: has the initial offset been loaded yet
593  * @offset: ptr to current offset value
594  * @stat: ptr to the stat
595  **/
596 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
597 			       bool offset_loaded, u64 *offset, u64 *stat)
598 {
599 	u32 new_data;
600 
601 	new_data = rd32(hw, reg);
602 	if (!offset_loaded)
603 		*offset = new_data;
604 	if (likely(new_data >= *offset))
605 		*stat = (u32)(new_data - *offset);
606 	else
607 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
608 }
609 
610 /**
611  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
612  * @hw: ptr to the hardware info
613  * @reg: the hw reg to read and clear
614  * @stat: ptr to the stat
615  **/
616 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
617 {
618 	u32 new_data = rd32(hw, reg);
619 
620 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
621 	*stat += new_data;
622 }
623 
624 /**
625  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
626  * @vsi: the VSI to be updated
627  **/
628 void i40e_update_eth_stats(struct i40e_vsi *vsi)
629 {
630 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
631 	struct i40e_pf *pf = vsi->back;
632 	struct i40e_hw *hw = &pf->hw;
633 	struct i40e_eth_stats *oes;
634 	struct i40e_eth_stats *es;     /* device's eth stats */
635 
636 	es = &vsi->eth_stats;
637 	oes = &vsi->eth_stats_offsets;
638 
639 	/* Gather up the stats that the hw collects */
640 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
641 			   vsi->stat_offsets_loaded,
642 			   &oes->tx_errors, &es->tx_errors);
643 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
644 			   vsi->stat_offsets_loaded,
645 			   &oes->rx_discards, &es->rx_discards);
646 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
647 			   vsi->stat_offsets_loaded,
648 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
649 
650 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
651 			   I40E_GLV_GORCL(stat_idx),
652 			   vsi->stat_offsets_loaded,
653 			   &oes->rx_bytes, &es->rx_bytes);
654 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
655 			   I40E_GLV_UPRCL(stat_idx),
656 			   vsi->stat_offsets_loaded,
657 			   &oes->rx_unicast, &es->rx_unicast);
658 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
659 			   I40E_GLV_MPRCL(stat_idx),
660 			   vsi->stat_offsets_loaded,
661 			   &oes->rx_multicast, &es->rx_multicast);
662 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
663 			   I40E_GLV_BPRCL(stat_idx),
664 			   vsi->stat_offsets_loaded,
665 			   &oes->rx_broadcast, &es->rx_broadcast);
666 
667 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
668 			   I40E_GLV_GOTCL(stat_idx),
669 			   vsi->stat_offsets_loaded,
670 			   &oes->tx_bytes, &es->tx_bytes);
671 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
672 			   I40E_GLV_UPTCL(stat_idx),
673 			   vsi->stat_offsets_loaded,
674 			   &oes->tx_unicast, &es->tx_unicast);
675 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
676 			   I40E_GLV_MPTCL(stat_idx),
677 			   vsi->stat_offsets_loaded,
678 			   &oes->tx_multicast, &es->tx_multicast);
679 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
680 			   I40E_GLV_BPTCL(stat_idx),
681 			   vsi->stat_offsets_loaded,
682 			   &oes->tx_broadcast, &es->tx_broadcast);
683 	vsi->stat_offsets_loaded = true;
684 }
685 
686 /**
687  * i40e_update_veb_stats - Update Switch component statistics
688  * @veb: the VEB being updated
689  **/
690 void i40e_update_veb_stats(struct i40e_veb *veb)
691 {
692 	struct i40e_pf *pf = veb->pf;
693 	struct i40e_hw *hw = &pf->hw;
694 	struct i40e_eth_stats *oes;
695 	struct i40e_eth_stats *es;     /* device's eth stats */
696 	struct i40e_veb_tc_stats *veb_oes;
697 	struct i40e_veb_tc_stats *veb_es;
698 	int i, idx = 0;
699 
700 	idx = veb->stats_idx;
701 	es = &veb->stats;
702 	oes = &veb->stats_offsets;
703 	veb_es = &veb->tc_stats;
704 	veb_oes = &veb->tc_stats_offsets;
705 
706 	/* Gather up the stats that the hw collects */
707 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
708 			   veb->stat_offsets_loaded,
709 			   &oes->tx_discards, &es->tx_discards);
710 	if (hw->revision_id > 0)
711 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
712 				   veb->stat_offsets_loaded,
713 				   &oes->rx_unknown_protocol,
714 				   &es->rx_unknown_protocol);
715 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
716 			   veb->stat_offsets_loaded,
717 			   &oes->rx_bytes, &es->rx_bytes);
718 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
719 			   veb->stat_offsets_loaded,
720 			   &oes->rx_unicast, &es->rx_unicast);
721 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
722 			   veb->stat_offsets_loaded,
723 			   &oes->rx_multicast, &es->rx_multicast);
724 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
725 			   veb->stat_offsets_loaded,
726 			   &oes->rx_broadcast, &es->rx_broadcast);
727 
728 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
729 			   veb->stat_offsets_loaded,
730 			   &oes->tx_bytes, &es->tx_bytes);
731 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
732 			   veb->stat_offsets_loaded,
733 			   &oes->tx_unicast, &es->tx_unicast);
734 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
735 			   veb->stat_offsets_loaded,
736 			   &oes->tx_multicast, &es->tx_multicast);
737 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
738 			   veb->stat_offsets_loaded,
739 			   &oes->tx_broadcast, &es->tx_broadcast);
740 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
741 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
742 				   I40E_GLVEBTC_RPCL(i, idx),
743 				   veb->stat_offsets_loaded,
744 				   &veb_oes->tc_rx_packets[i],
745 				   &veb_es->tc_rx_packets[i]);
746 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
747 				   I40E_GLVEBTC_RBCL(i, idx),
748 				   veb->stat_offsets_loaded,
749 				   &veb_oes->tc_rx_bytes[i],
750 				   &veb_es->tc_rx_bytes[i]);
751 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
752 				   I40E_GLVEBTC_TPCL(i, idx),
753 				   veb->stat_offsets_loaded,
754 				   &veb_oes->tc_tx_packets[i],
755 				   &veb_es->tc_tx_packets[i]);
756 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
757 				   I40E_GLVEBTC_TBCL(i, idx),
758 				   veb->stat_offsets_loaded,
759 				   &veb_oes->tc_tx_bytes[i],
760 				   &veb_es->tc_tx_bytes[i]);
761 	}
762 	veb->stat_offsets_loaded = true;
763 }
764 
765 /**
766  * i40e_update_vsi_stats - Update the vsi statistics counters.
767  * @vsi: the VSI to be updated
768  *
769  * There are a few instances where we store the same stat in a
770  * couple of different structs.  This is partly because we have
771  * the netdev stats that need to be filled out, which is slightly
772  * different from the "eth_stats" defined by the chip and used in
773  * VF communications.  We sort it out here.
774  **/
775 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
776 {
777 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
778 	struct i40e_pf *pf = vsi->back;
779 	struct rtnl_link_stats64 *ons;
780 	struct rtnl_link_stats64 *ns;   /* netdev stats */
781 	struct i40e_eth_stats *oes;
782 	struct i40e_eth_stats *es;     /* device's eth stats */
783 	u64 tx_restart, tx_busy;
784 	struct i40e_ring *p;
785 	u64 bytes, packets;
786 	unsigned int start;
787 	u64 tx_linearize;
788 	u64 tx_force_wb;
789 	u64 tx_stopped;
790 	u64 rx_p, rx_b;
791 	u64 tx_p, tx_b;
792 	u16 q;
793 
794 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
795 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
796 		return;
797 
798 	ns = i40e_get_vsi_stats_struct(vsi);
799 	ons = &vsi->net_stats_offsets;
800 	es = &vsi->eth_stats;
801 	oes = &vsi->eth_stats_offsets;
802 
803 	/* Gather up the netdev and vsi stats that the driver collects
804 	 * on the fly during packet processing
805 	 */
806 	rx_b = rx_p = 0;
807 	tx_b = tx_p = 0;
808 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
809 	tx_stopped = 0;
810 	rx_page = 0;
811 	rx_buf = 0;
812 	rx_reuse = 0;
813 	rx_alloc = 0;
814 	rx_waive = 0;
815 	rx_busy = 0;
816 	rcu_read_lock();
817 	for (q = 0; q < vsi->num_queue_pairs; q++) {
818 		/* locate Tx ring */
819 		p = READ_ONCE(vsi->tx_rings[q]);
820 		if (!p)
821 			continue;
822 
823 		do {
824 			start = u64_stats_fetch_begin_irq(&p->syncp);
825 			packets = p->stats.packets;
826 			bytes = p->stats.bytes;
827 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
828 		tx_b += bytes;
829 		tx_p += packets;
830 		tx_restart += p->tx_stats.restart_queue;
831 		tx_busy += p->tx_stats.tx_busy;
832 		tx_linearize += p->tx_stats.tx_linearize;
833 		tx_force_wb += p->tx_stats.tx_force_wb;
834 		tx_stopped += p->tx_stats.tx_stopped;
835 
836 		/* locate Rx ring */
837 		p = READ_ONCE(vsi->rx_rings[q]);
838 		if (!p)
839 			continue;
840 
841 		do {
842 			start = u64_stats_fetch_begin_irq(&p->syncp);
843 			packets = p->stats.packets;
844 			bytes = p->stats.bytes;
845 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
846 		rx_b += bytes;
847 		rx_p += packets;
848 		rx_buf += p->rx_stats.alloc_buff_failed;
849 		rx_page += p->rx_stats.alloc_page_failed;
850 		rx_reuse += p->rx_stats.page_reuse_count;
851 		rx_alloc += p->rx_stats.page_alloc_count;
852 		rx_waive += p->rx_stats.page_waive_count;
853 		rx_busy += p->rx_stats.page_busy_count;
854 
855 		if (i40e_enabled_xdp_vsi(vsi)) {
856 			/* locate XDP ring */
857 			p = READ_ONCE(vsi->xdp_rings[q]);
858 			if (!p)
859 				continue;
860 
861 			do {
862 				start = u64_stats_fetch_begin_irq(&p->syncp);
863 				packets = p->stats.packets;
864 				bytes = p->stats.bytes;
865 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
866 			tx_b += bytes;
867 			tx_p += packets;
868 			tx_restart += p->tx_stats.restart_queue;
869 			tx_busy += p->tx_stats.tx_busy;
870 			tx_linearize += p->tx_stats.tx_linearize;
871 			tx_force_wb += p->tx_stats.tx_force_wb;
872 		}
873 	}
874 	rcu_read_unlock();
875 	vsi->tx_restart = tx_restart;
876 	vsi->tx_busy = tx_busy;
877 	vsi->tx_linearize = tx_linearize;
878 	vsi->tx_force_wb = tx_force_wb;
879 	vsi->tx_stopped = tx_stopped;
880 	vsi->rx_page_failed = rx_page;
881 	vsi->rx_buf_failed = rx_buf;
882 	vsi->rx_page_reuse = rx_reuse;
883 	vsi->rx_page_alloc = rx_alloc;
884 	vsi->rx_page_waive = rx_waive;
885 	vsi->rx_page_busy = rx_busy;
886 
887 	ns->rx_packets = rx_p;
888 	ns->rx_bytes = rx_b;
889 	ns->tx_packets = tx_p;
890 	ns->tx_bytes = tx_b;
891 
892 	/* update netdev stats from eth stats */
893 	i40e_update_eth_stats(vsi);
894 	ons->tx_errors = oes->tx_errors;
895 	ns->tx_errors = es->tx_errors;
896 	ons->multicast = oes->rx_multicast;
897 	ns->multicast = es->rx_multicast;
898 	ons->rx_dropped = oes->rx_discards;
899 	ns->rx_dropped = es->rx_discards;
900 	ons->tx_dropped = oes->tx_discards;
901 	ns->tx_dropped = es->tx_discards;
902 
903 	/* pull in a couple PF stats if this is the main vsi */
904 	if (vsi == pf->vsi[pf->lan_vsi]) {
905 		ns->rx_crc_errors = pf->stats.crc_errors;
906 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
907 		ns->rx_length_errors = pf->stats.rx_length_errors;
908 	}
909 }
910 
911 /**
912  * i40e_update_pf_stats - Update the PF statistics counters.
913  * @pf: the PF to be updated
914  **/
915 static void i40e_update_pf_stats(struct i40e_pf *pf)
916 {
917 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
918 	struct i40e_hw_port_stats *nsd = &pf->stats;
919 	struct i40e_hw *hw = &pf->hw;
920 	u32 val;
921 	int i;
922 
923 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
924 			   I40E_GLPRT_GORCL(hw->port),
925 			   pf->stat_offsets_loaded,
926 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
927 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
928 			   I40E_GLPRT_GOTCL(hw->port),
929 			   pf->stat_offsets_loaded,
930 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
931 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
932 			   pf->stat_offsets_loaded,
933 			   &osd->eth.rx_discards,
934 			   &nsd->eth.rx_discards);
935 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
936 			   I40E_GLPRT_UPRCL(hw->port),
937 			   pf->stat_offsets_loaded,
938 			   &osd->eth.rx_unicast,
939 			   &nsd->eth.rx_unicast);
940 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
941 			   I40E_GLPRT_MPRCL(hw->port),
942 			   pf->stat_offsets_loaded,
943 			   &osd->eth.rx_multicast,
944 			   &nsd->eth.rx_multicast);
945 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
946 			   I40E_GLPRT_BPRCL(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->eth.rx_broadcast,
949 			   &nsd->eth.rx_broadcast);
950 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
951 			   I40E_GLPRT_UPTCL(hw->port),
952 			   pf->stat_offsets_loaded,
953 			   &osd->eth.tx_unicast,
954 			   &nsd->eth.tx_unicast);
955 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
956 			   I40E_GLPRT_MPTCL(hw->port),
957 			   pf->stat_offsets_loaded,
958 			   &osd->eth.tx_multicast,
959 			   &nsd->eth.tx_multicast);
960 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
961 			   I40E_GLPRT_BPTCL(hw->port),
962 			   pf->stat_offsets_loaded,
963 			   &osd->eth.tx_broadcast,
964 			   &nsd->eth.tx_broadcast);
965 
966 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
967 			   pf->stat_offsets_loaded,
968 			   &osd->tx_dropped_link_down,
969 			   &nsd->tx_dropped_link_down);
970 
971 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
972 			   pf->stat_offsets_loaded,
973 			   &osd->crc_errors, &nsd->crc_errors);
974 
975 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
976 			   pf->stat_offsets_loaded,
977 			   &osd->illegal_bytes, &nsd->illegal_bytes);
978 
979 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
980 			   pf->stat_offsets_loaded,
981 			   &osd->mac_local_faults,
982 			   &nsd->mac_local_faults);
983 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
984 			   pf->stat_offsets_loaded,
985 			   &osd->mac_remote_faults,
986 			   &nsd->mac_remote_faults);
987 
988 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
989 			   pf->stat_offsets_loaded,
990 			   &osd->rx_length_errors,
991 			   &nsd->rx_length_errors);
992 
993 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
994 			   pf->stat_offsets_loaded,
995 			   &osd->link_xon_rx, &nsd->link_xon_rx);
996 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
997 			   pf->stat_offsets_loaded,
998 			   &osd->link_xon_tx, &nsd->link_xon_tx);
999 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1000 			   pf->stat_offsets_loaded,
1001 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1002 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1003 			   pf->stat_offsets_loaded,
1004 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1005 
1006 	for (i = 0; i < 8; i++) {
1007 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1008 				   pf->stat_offsets_loaded,
1009 				   &osd->priority_xoff_rx[i],
1010 				   &nsd->priority_xoff_rx[i]);
1011 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1012 				   pf->stat_offsets_loaded,
1013 				   &osd->priority_xon_rx[i],
1014 				   &nsd->priority_xon_rx[i]);
1015 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1016 				   pf->stat_offsets_loaded,
1017 				   &osd->priority_xon_tx[i],
1018 				   &nsd->priority_xon_tx[i]);
1019 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1020 				   pf->stat_offsets_loaded,
1021 				   &osd->priority_xoff_tx[i],
1022 				   &nsd->priority_xoff_tx[i]);
1023 		i40e_stat_update32(hw,
1024 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1025 				   pf->stat_offsets_loaded,
1026 				   &osd->priority_xon_2_xoff[i],
1027 				   &nsd->priority_xon_2_xoff[i]);
1028 	}
1029 
1030 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1031 			   I40E_GLPRT_PRC64L(hw->port),
1032 			   pf->stat_offsets_loaded,
1033 			   &osd->rx_size_64, &nsd->rx_size_64);
1034 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1035 			   I40E_GLPRT_PRC127L(hw->port),
1036 			   pf->stat_offsets_loaded,
1037 			   &osd->rx_size_127, &nsd->rx_size_127);
1038 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1039 			   I40E_GLPRT_PRC255L(hw->port),
1040 			   pf->stat_offsets_loaded,
1041 			   &osd->rx_size_255, &nsd->rx_size_255);
1042 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1043 			   I40E_GLPRT_PRC511L(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->rx_size_511, &nsd->rx_size_511);
1046 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1047 			   I40E_GLPRT_PRC1023L(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1050 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1051 			   I40E_GLPRT_PRC1522L(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1054 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1055 			   I40E_GLPRT_PRC9522L(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->rx_size_big, &nsd->rx_size_big);
1058 
1059 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1060 			   I40E_GLPRT_PTC64L(hw->port),
1061 			   pf->stat_offsets_loaded,
1062 			   &osd->tx_size_64, &nsd->tx_size_64);
1063 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1064 			   I40E_GLPRT_PTC127L(hw->port),
1065 			   pf->stat_offsets_loaded,
1066 			   &osd->tx_size_127, &nsd->tx_size_127);
1067 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1068 			   I40E_GLPRT_PTC255L(hw->port),
1069 			   pf->stat_offsets_loaded,
1070 			   &osd->tx_size_255, &nsd->tx_size_255);
1071 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1072 			   I40E_GLPRT_PTC511L(hw->port),
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->tx_size_511, &nsd->tx_size_511);
1075 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1076 			   I40E_GLPRT_PTC1023L(hw->port),
1077 			   pf->stat_offsets_loaded,
1078 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1079 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1080 			   I40E_GLPRT_PTC1522L(hw->port),
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1083 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1084 			   I40E_GLPRT_PTC9522L(hw->port),
1085 			   pf->stat_offsets_loaded,
1086 			   &osd->tx_size_big, &nsd->tx_size_big);
1087 
1088 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->rx_undersize, &nsd->rx_undersize);
1091 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1092 			   pf->stat_offsets_loaded,
1093 			   &osd->rx_fragments, &nsd->rx_fragments);
1094 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1095 			   pf->stat_offsets_loaded,
1096 			   &osd->rx_oversize, &nsd->rx_oversize);
1097 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1098 			   pf->stat_offsets_loaded,
1099 			   &osd->rx_jabber, &nsd->rx_jabber);
1100 
1101 	/* FDIR stats */
1102 	i40e_stat_update_and_clear32(hw,
1103 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1104 			&nsd->fd_atr_match);
1105 	i40e_stat_update_and_clear32(hw,
1106 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1107 			&nsd->fd_sb_match);
1108 	i40e_stat_update_and_clear32(hw,
1109 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1110 			&nsd->fd_atr_tunnel_match);
1111 
1112 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1113 	nsd->tx_lpi_status =
1114 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1115 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1116 	nsd->rx_lpi_status =
1117 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1118 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1119 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1120 			   pf->stat_offsets_loaded,
1121 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1122 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1123 			   pf->stat_offsets_loaded,
1124 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1125 
1126 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1127 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1128 		nsd->fd_sb_status = true;
1129 	else
1130 		nsd->fd_sb_status = false;
1131 
1132 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1133 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1134 		nsd->fd_atr_status = true;
1135 	else
1136 		nsd->fd_atr_status = false;
1137 
1138 	pf->stat_offsets_loaded = true;
1139 }
1140 
1141 /**
1142  * i40e_update_stats - Update the various statistics counters.
1143  * @vsi: the VSI to be updated
1144  *
1145  * Update the various stats for this VSI and its related entities.
1146  **/
1147 void i40e_update_stats(struct i40e_vsi *vsi)
1148 {
1149 	struct i40e_pf *pf = vsi->back;
1150 
1151 	if (vsi == pf->vsi[pf->lan_vsi])
1152 		i40e_update_pf_stats(pf);
1153 
1154 	i40e_update_vsi_stats(vsi);
1155 }
1156 
1157 /**
1158  * i40e_count_filters - counts VSI mac filters
1159  * @vsi: the VSI to be searched
1160  *
1161  * Returns count of mac filters
1162  **/
1163 int i40e_count_filters(struct i40e_vsi *vsi)
1164 {
1165 	struct i40e_mac_filter *f;
1166 	struct hlist_node *h;
1167 	int bkt;
1168 	int cnt = 0;
1169 
1170 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1171 		++cnt;
1172 
1173 	return cnt;
1174 }
1175 
1176 /**
1177  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1178  * @vsi: the VSI to be searched
1179  * @macaddr: the MAC address
1180  * @vlan: the vlan
1181  *
1182  * Returns ptr to the filter object or NULL
1183  **/
1184 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1185 						const u8 *macaddr, s16 vlan)
1186 {
1187 	struct i40e_mac_filter *f;
1188 	u64 key;
1189 
1190 	if (!vsi || !macaddr)
1191 		return NULL;
1192 
1193 	key = i40e_addr_to_hkey(macaddr);
1194 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1195 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1196 		    (vlan == f->vlan))
1197 			return f;
1198 	}
1199 	return NULL;
1200 }
1201 
1202 /**
1203  * i40e_find_mac - Find a mac addr in the macvlan filters list
1204  * @vsi: the VSI to be searched
1205  * @macaddr: the MAC address we are searching for
1206  *
1207  * Returns the first filter with the provided MAC address or NULL if
1208  * MAC address was not found
1209  **/
1210 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1211 {
1212 	struct i40e_mac_filter *f;
1213 	u64 key;
1214 
1215 	if (!vsi || !macaddr)
1216 		return NULL;
1217 
1218 	key = i40e_addr_to_hkey(macaddr);
1219 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1220 		if ((ether_addr_equal(macaddr, f->macaddr)))
1221 			return f;
1222 	}
1223 	return NULL;
1224 }
1225 
1226 /**
1227  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1228  * @vsi: the VSI to be searched
1229  *
1230  * Returns true if VSI is in vlan mode or false otherwise
1231  **/
1232 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1233 {
1234 	/* If we have a PVID, always operate in VLAN mode */
1235 	if (vsi->info.pvid)
1236 		return true;
1237 
1238 	/* We need to operate in VLAN mode whenever we have any filters with
1239 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1240 	 * time, incurring search cost repeatedly. However, we can notice two
1241 	 * things:
1242 	 *
1243 	 * 1) the only place where we can gain a VLAN filter is in
1244 	 *    i40e_add_filter.
1245 	 *
1246 	 * 2) the only place where filters are actually removed is in
1247 	 *    i40e_sync_filters_subtask.
1248 	 *
1249 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1250 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1251 	 * we have to perform the full search after deleting filters in
1252 	 * i40e_sync_filters_subtask, but we already have to search
1253 	 * filters here and can perform the check at the same time. This
1254 	 * results in avoiding embedding a loop for VLAN mode inside another
1255 	 * loop over all the filters, and should maintain correctness as noted
1256 	 * above.
1257 	 */
1258 	return vsi->has_vlan_filter;
1259 }
1260 
1261 /**
1262  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1263  * @vsi: the VSI to configure
1264  * @tmp_add_list: list of filters ready to be added
1265  * @tmp_del_list: list of filters ready to be deleted
1266  * @vlan_filters: the number of active VLAN filters
1267  *
1268  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1269  * behave as expected. If we have any active VLAN filters remaining or about
1270  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1271  * so that they only match against untagged traffic. If we no longer have any
1272  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1273  * so that they match against both tagged and untagged traffic. In this way,
1274  * we ensure that we correctly receive the desired traffic. This ensures that
1275  * when we have an active VLAN we will receive only untagged traffic and
1276  * traffic matching active VLANs. If we have no active VLANs then we will
1277  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1278  *
1279  * Finally, in a similar fashion, this function also corrects filters when
1280  * there is an active PVID assigned to this VSI.
1281  *
1282  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1283  *
1284  * This function is only expected to be called from within
1285  * i40e_sync_vsi_filters.
1286  *
1287  * NOTE: This function expects to be called while under the
1288  * mac_filter_hash_lock
1289  */
1290 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1291 					 struct hlist_head *tmp_add_list,
1292 					 struct hlist_head *tmp_del_list,
1293 					 int vlan_filters)
1294 {
1295 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1296 	struct i40e_mac_filter *f, *add_head;
1297 	struct i40e_new_mac_filter *new;
1298 	struct hlist_node *h;
1299 	int bkt, new_vlan;
1300 
1301 	/* To determine if a particular filter needs to be replaced we
1302 	 * have the three following conditions:
1303 	 *
1304 	 * a) if we have a PVID assigned, then all filters which are
1305 	 *    not marked as VLAN=PVID must be replaced with filters that
1306 	 *    are.
1307 	 * b) otherwise, if we have any active VLANS, all filters
1308 	 *    which are marked as VLAN=-1 must be replaced with
1309 	 *    filters marked as VLAN=0
1310 	 * c) finally, if we do not have any active VLANS, all filters
1311 	 *    which are marked as VLAN=0 must be replaced with filters
1312 	 *    marked as VLAN=-1
1313 	 */
1314 
1315 	/* Update the filters about to be added in place */
1316 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1317 		if (pvid && new->f->vlan != pvid)
1318 			new->f->vlan = pvid;
1319 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1320 			new->f->vlan = 0;
1321 		else if (!vlan_filters && new->f->vlan == 0)
1322 			new->f->vlan = I40E_VLAN_ANY;
1323 	}
1324 
1325 	/* Update the remaining active filters */
1326 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1327 		/* Combine the checks for whether a filter needs to be changed
1328 		 * and then determine the new VLAN inside the if block, in
1329 		 * order to avoid duplicating code for adding the new filter
1330 		 * then deleting the old filter.
1331 		 */
1332 		if ((pvid && f->vlan != pvid) ||
1333 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1334 		    (!vlan_filters && f->vlan == 0)) {
1335 			/* Determine the new vlan we will be adding */
1336 			if (pvid)
1337 				new_vlan = pvid;
1338 			else if (vlan_filters)
1339 				new_vlan = 0;
1340 			else
1341 				new_vlan = I40E_VLAN_ANY;
1342 
1343 			/* Create the new filter */
1344 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1345 			if (!add_head)
1346 				return -ENOMEM;
1347 
1348 			/* Create a temporary i40e_new_mac_filter */
1349 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1350 			if (!new)
1351 				return -ENOMEM;
1352 
1353 			new->f = add_head;
1354 			new->state = add_head->state;
1355 
1356 			/* Add the new filter to the tmp list */
1357 			hlist_add_head(&new->hlist, tmp_add_list);
1358 
1359 			/* Put the original filter into the delete list */
1360 			f->state = I40E_FILTER_REMOVE;
1361 			hash_del(&f->hlist);
1362 			hlist_add_head(&f->hlist, tmp_del_list);
1363 		}
1364 	}
1365 
1366 	vsi->has_vlan_filter = !!vlan_filters;
1367 
1368 	return 0;
1369 }
1370 
1371 /**
1372  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1373  * @vsi: the vsi to configure
1374  * @new_mac: new mac filter to be added
1375  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1376  * @vlan_filters: the number of active VLAN filters
1377  * @trusted: flag if the VF is trusted
1378  *
1379  * Get new VLAN id based on current VLAN filters, trust, PVID
1380  * and vf-vlan-prune-disable flag.
1381  *
1382  * Returns the value of the new vlan filter or
1383  * the old value if no new filter is needed.
1384  */
1385 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1386 				struct i40e_new_mac_filter *new_mac,
1387 				struct i40e_mac_filter *f,
1388 				int vlan_filters,
1389 				bool trusted)
1390 {
1391 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1392 	struct i40e_pf *pf = vsi->back;
1393 	bool is_any;
1394 
1395 	if (new_mac)
1396 		f = new_mac->f;
1397 
1398 	if (pvid && f->vlan != pvid)
1399 		return pvid;
1400 
1401 	is_any = (trusted ||
1402 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1403 
1404 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1405 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1406 	    (is_any && !vlan_filters && f->vlan == 0)) {
1407 		if (is_any)
1408 			return I40E_VLAN_ANY;
1409 		else
1410 			return 0;
1411 	}
1412 
1413 	return f->vlan;
1414 }
1415 
1416 /**
1417  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1418  * @vsi: the vsi to configure
1419  * @tmp_add_list: list of filters ready to be added
1420  * @tmp_del_list: list of filters ready to be deleted
1421  * @vlan_filters: the number of active VLAN filters
1422  * @trusted: flag if the VF is trusted
1423  *
1424  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1425  * and vf-vlan-prune-disable flag.
1426  *
1427  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1428  *
1429  * This function is only expected to be called from within
1430  * i40e_sync_vsi_filters.
1431  *
1432  * NOTE: This function expects to be called while under the
1433  * mac_filter_hash_lock
1434  */
1435 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1436 					    struct hlist_head *tmp_add_list,
1437 					    struct hlist_head *tmp_del_list,
1438 					    int vlan_filters,
1439 					    bool trusted)
1440 {
1441 	struct i40e_mac_filter *f, *add_head;
1442 	struct i40e_new_mac_filter *new_mac;
1443 	struct hlist_node *h;
1444 	int bkt, new_vlan;
1445 
1446 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1447 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1448 							vlan_filters, trusted);
1449 	}
1450 
1451 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1452 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1453 						trusted);
1454 		if (new_vlan != f->vlan) {
1455 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1456 			if (!add_head)
1457 				return -ENOMEM;
1458 			/* Create a temporary i40e_new_mac_filter */
1459 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1460 			if (!new_mac)
1461 				return -ENOMEM;
1462 			new_mac->f = add_head;
1463 			new_mac->state = add_head->state;
1464 
1465 			/* Add the new filter to the tmp list */
1466 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1467 
1468 			/* Put the original filter into the delete list */
1469 			f->state = I40E_FILTER_REMOVE;
1470 			hash_del(&f->hlist);
1471 			hlist_add_head(&f->hlist, tmp_del_list);
1472 		}
1473 	}
1474 
1475 	vsi->has_vlan_filter = !!vlan_filters;
1476 	return 0;
1477 }
1478 
1479 /**
1480  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1481  * @vsi: the PF Main VSI - inappropriate for any other VSI
1482  * @macaddr: the MAC address
1483  *
1484  * Remove whatever filter the firmware set up so the driver can manage
1485  * its own filtering intelligently.
1486  **/
1487 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1488 {
1489 	struct i40e_aqc_remove_macvlan_element_data element;
1490 	struct i40e_pf *pf = vsi->back;
1491 
1492 	/* Only appropriate for the PF main VSI */
1493 	if (vsi->type != I40E_VSI_MAIN)
1494 		return;
1495 
1496 	memset(&element, 0, sizeof(element));
1497 	ether_addr_copy(element.mac_addr, macaddr);
1498 	element.vlan_tag = 0;
1499 	/* Ignore error returns, some firmware does it this way... */
1500 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1501 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1502 
1503 	memset(&element, 0, sizeof(element));
1504 	ether_addr_copy(element.mac_addr, macaddr);
1505 	element.vlan_tag = 0;
1506 	/* ...and some firmware does it this way. */
1507 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1508 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1509 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1510 }
1511 
1512 /**
1513  * i40e_add_filter - Add a mac/vlan filter to the VSI
1514  * @vsi: the VSI to be searched
1515  * @macaddr: the MAC address
1516  * @vlan: the vlan
1517  *
1518  * Returns ptr to the filter object or NULL when no memory available.
1519  *
1520  * NOTE: This function is expected to be called with mac_filter_hash_lock
1521  * being held.
1522  **/
1523 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1524 					const u8 *macaddr, s16 vlan)
1525 {
1526 	struct i40e_mac_filter *f;
1527 	u64 key;
1528 
1529 	if (!vsi || !macaddr)
1530 		return NULL;
1531 
1532 	f = i40e_find_filter(vsi, macaddr, vlan);
1533 	if (!f) {
1534 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1535 		if (!f)
1536 			return NULL;
1537 
1538 		/* Update the boolean indicating if we need to function in
1539 		 * VLAN mode.
1540 		 */
1541 		if (vlan >= 0)
1542 			vsi->has_vlan_filter = true;
1543 
1544 		ether_addr_copy(f->macaddr, macaddr);
1545 		f->vlan = vlan;
1546 		f->state = I40E_FILTER_NEW;
1547 		INIT_HLIST_NODE(&f->hlist);
1548 
1549 		key = i40e_addr_to_hkey(macaddr);
1550 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1551 
1552 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1553 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1554 	}
1555 
1556 	/* If we're asked to add a filter that has been marked for removal, it
1557 	 * is safe to simply restore it to active state. __i40e_del_filter
1558 	 * will have simply deleted any filters which were previously marked
1559 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1560 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1561 	 * task, just restore this filter to the ACTIVE state so that the
1562 	 * sync task leaves it in place
1563 	 */
1564 	if (f->state == I40E_FILTER_REMOVE)
1565 		f->state = I40E_FILTER_ACTIVE;
1566 
1567 	return f;
1568 }
1569 
1570 /**
1571  * __i40e_del_filter - Remove a specific filter from the VSI
1572  * @vsi: VSI to remove from
1573  * @f: the filter to remove from the list
1574  *
1575  * This function should be called instead of i40e_del_filter only if you know
1576  * the exact filter you will remove already, such as via i40e_find_filter or
1577  * i40e_find_mac.
1578  *
1579  * NOTE: This function is expected to be called with mac_filter_hash_lock
1580  * being held.
1581  * ANOTHER NOTE: This function MUST be called from within the context of
1582  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1583  * instead of list_for_each_entry().
1584  **/
1585 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1586 {
1587 	if (!f)
1588 		return;
1589 
1590 	/* If the filter was never added to firmware then we can just delete it
1591 	 * directly and we don't want to set the status to remove or else an
1592 	 * admin queue command will unnecessarily fire.
1593 	 */
1594 	if ((f->state == I40E_FILTER_FAILED) ||
1595 	    (f->state == I40E_FILTER_NEW)) {
1596 		hash_del(&f->hlist);
1597 		kfree(f);
1598 	} else {
1599 		f->state = I40E_FILTER_REMOVE;
1600 	}
1601 
1602 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1603 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1604 }
1605 
1606 /**
1607  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1608  * @vsi: the VSI to be searched
1609  * @macaddr: the MAC address
1610  * @vlan: the VLAN
1611  *
1612  * NOTE: This function is expected to be called with mac_filter_hash_lock
1613  * being held.
1614  * ANOTHER NOTE: This function MUST be called from within the context of
1615  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1616  * instead of list_for_each_entry().
1617  **/
1618 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1619 {
1620 	struct i40e_mac_filter *f;
1621 
1622 	if (!vsi || !macaddr)
1623 		return;
1624 
1625 	f = i40e_find_filter(vsi, macaddr, vlan);
1626 	__i40e_del_filter(vsi, f);
1627 }
1628 
1629 /**
1630  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1631  * @vsi: the VSI to be searched
1632  * @macaddr: the mac address to be filtered
1633  *
1634  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1635  * go through all the macvlan filters and add a macvlan filter for each
1636  * unique vlan that already exists. If a PVID has been assigned, instead only
1637  * add the macaddr to that VLAN.
1638  *
1639  * Returns last filter added on success, else NULL
1640  **/
1641 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1642 					    const u8 *macaddr)
1643 {
1644 	struct i40e_mac_filter *f, *add = NULL;
1645 	struct hlist_node *h;
1646 	int bkt;
1647 
1648 	if (vsi->info.pvid)
1649 		return i40e_add_filter(vsi, macaddr,
1650 				       le16_to_cpu(vsi->info.pvid));
1651 
1652 	if (!i40e_is_vsi_in_vlan(vsi))
1653 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1654 
1655 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1656 		if (f->state == I40E_FILTER_REMOVE)
1657 			continue;
1658 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1659 		if (!add)
1660 			return NULL;
1661 	}
1662 
1663 	return add;
1664 }
1665 
1666 /**
1667  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1668  * @vsi: the VSI to be searched
1669  * @macaddr: the mac address to be removed
1670  *
1671  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1672  * associated with.
1673  *
1674  * Returns 0 for success, or error
1675  **/
1676 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1677 {
1678 	struct i40e_mac_filter *f;
1679 	struct hlist_node *h;
1680 	bool found = false;
1681 	int bkt;
1682 
1683 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1684 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1685 		if (ether_addr_equal(macaddr, f->macaddr)) {
1686 			__i40e_del_filter(vsi, f);
1687 			found = true;
1688 		}
1689 	}
1690 
1691 	if (found)
1692 		return 0;
1693 	else
1694 		return -ENOENT;
1695 }
1696 
1697 /**
1698  * i40e_set_mac - NDO callback to set mac address
1699  * @netdev: network interface device structure
1700  * @p: pointer to an address structure
1701  *
1702  * Returns 0 on success, negative on failure
1703  **/
1704 static int i40e_set_mac(struct net_device *netdev, void *p)
1705 {
1706 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1707 	struct i40e_vsi *vsi = np->vsi;
1708 	struct i40e_pf *pf = vsi->back;
1709 	struct i40e_hw *hw = &pf->hw;
1710 	struct sockaddr *addr = p;
1711 
1712 	if (!is_valid_ether_addr(addr->sa_data))
1713 		return -EADDRNOTAVAIL;
1714 
1715 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1716 		netdev_info(netdev, "already using mac address %pM\n",
1717 			    addr->sa_data);
1718 		return 0;
1719 	}
1720 
1721 	if (test_bit(__I40E_DOWN, pf->state) ||
1722 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1723 		return -EADDRNOTAVAIL;
1724 
1725 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1726 		netdev_info(netdev, "returning to hw mac address %pM\n",
1727 			    hw->mac.addr);
1728 	else
1729 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1730 
1731 	/* Copy the address first, so that we avoid a possible race with
1732 	 * .set_rx_mode().
1733 	 * - Remove old address from MAC filter
1734 	 * - Copy new address
1735 	 * - Add new address to MAC filter
1736 	 */
1737 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1738 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1739 	eth_hw_addr_set(netdev, addr->sa_data);
1740 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1741 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1742 
1743 	if (vsi->type == I40E_VSI_MAIN) {
1744 		i40e_status ret;
1745 
1746 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1747 						addr->sa_data, NULL);
1748 		if (ret)
1749 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1750 				    i40e_stat_str(hw, ret),
1751 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1752 	}
1753 
1754 	/* schedule our worker thread which will take care of
1755 	 * applying the new filter changes
1756 	 */
1757 	i40e_service_event_schedule(pf);
1758 	return 0;
1759 }
1760 
1761 /**
1762  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1763  * @vsi: vsi structure
1764  * @seed: RSS hash seed
1765  * @lut: pointer to lookup table of lut_size
1766  * @lut_size: size of the lookup table
1767  **/
1768 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1769 			      u8 *lut, u16 lut_size)
1770 {
1771 	struct i40e_pf *pf = vsi->back;
1772 	struct i40e_hw *hw = &pf->hw;
1773 	int ret = 0;
1774 
1775 	if (seed) {
1776 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1777 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1778 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1779 		if (ret) {
1780 			dev_info(&pf->pdev->dev,
1781 				 "Cannot set RSS key, err %s aq_err %s\n",
1782 				 i40e_stat_str(hw, ret),
1783 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1784 			return ret;
1785 		}
1786 	}
1787 	if (lut) {
1788 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1789 
1790 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1791 		if (ret) {
1792 			dev_info(&pf->pdev->dev,
1793 				 "Cannot set RSS lut, err %s aq_err %s\n",
1794 				 i40e_stat_str(hw, ret),
1795 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1796 			return ret;
1797 		}
1798 	}
1799 	return ret;
1800 }
1801 
1802 /**
1803  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1804  * @vsi: VSI structure
1805  **/
1806 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1807 {
1808 	struct i40e_pf *pf = vsi->back;
1809 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1810 	u8 *lut;
1811 	int ret;
1812 
1813 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1814 		return 0;
1815 	if (!vsi->rss_size)
1816 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1817 				      vsi->num_queue_pairs);
1818 	if (!vsi->rss_size)
1819 		return -EINVAL;
1820 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1821 	if (!lut)
1822 		return -ENOMEM;
1823 
1824 	/* Use the user configured hash keys and lookup table if there is one,
1825 	 * otherwise use default
1826 	 */
1827 	if (vsi->rss_lut_user)
1828 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1829 	else
1830 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1831 	if (vsi->rss_hkey_user)
1832 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1833 	else
1834 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1835 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1836 	kfree(lut);
1837 	return ret;
1838 }
1839 
1840 /**
1841  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1842  * @vsi: the VSI being configured,
1843  * @ctxt: VSI context structure
1844  * @enabled_tc: number of traffic classes to enable
1845  *
1846  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1847  **/
1848 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1849 					   struct i40e_vsi_context *ctxt,
1850 					   u8 enabled_tc)
1851 {
1852 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1853 	int i, override_q, pow, num_qps, ret;
1854 	u8 netdev_tc = 0, offset = 0;
1855 
1856 	if (vsi->type != I40E_VSI_MAIN)
1857 		return -EINVAL;
1858 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1859 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1860 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1861 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1862 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1863 
1864 	/* find the next higher power-of-2 of num queue pairs */
1865 	pow = ilog2(num_qps);
1866 	if (!is_power_of_2(num_qps))
1867 		pow++;
1868 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1869 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1870 
1871 	/* Setup queue offset/count for all TCs for given VSI */
1872 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1873 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1874 		/* See if the given TC is enabled for the given VSI */
1875 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1876 			offset = vsi->mqprio_qopt.qopt.offset[i];
1877 			qcount = vsi->mqprio_qopt.qopt.count[i];
1878 			if (qcount > max_qcount)
1879 				max_qcount = qcount;
1880 			vsi->tc_config.tc_info[i].qoffset = offset;
1881 			vsi->tc_config.tc_info[i].qcount = qcount;
1882 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1883 		} else {
1884 			/* TC is not enabled so set the offset to
1885 			 * default queue and allocate one queue
1886 			 * for the given TC.
1887 			 */
1888 			vsi->tc_config.tc_info[i].qoffset = 0;
1889 			vsi->tc_config.tc_info[i].qcount = 1;
1890 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1891 		}
1892 	}
1893 
1894 	/* Set actual Tx/Rx queue pairs */
1895 	vsi->num_queue_pairs = offset + qcount;
1896 
1897 	/* Setup queue TC[0].qmap for given VSI context */
1898 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1899 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1900 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1901 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1902 
1903 	/* Reconfigure RSS for main VSI with max queue count */
1904 	vsi->rss_size = max_qcount;
1905 	ret = i40e_vsi_config_rss(vsi);
1906 	if (ret) {
1907 		dev_info(&vsi->back->pdev->dev,
1908 			 "Failed to reconfig rss for num_queues (%u)\n",
1909 			 max_qcount);
1910 		return ret;
1911 	}
1912 	vsi->reconfig_rss = true;
1913 	dev_dbg(&vsi->back->pdev->dev,
1914 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1915 
1916 	/* Find queue count available for channel VSIs and starting offset
1917 	 * for channel VSIs
1918 	 */
1919 	override_q = vsi->mqprio_qopt.qopt.count[0];
1920 	if (override_q && override_q < vsi->num_queue_pairs) {
1921 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1922 		vsi->next_base_queue = override_q;
1923 	}
1924 	return 0;
1925 }
1926 
1927 /**
1928  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1929  * @vsi: the VSI being setup
1930  * @ctxt: VSI context structure
1931  * @enabled_tc: Enabled TCs bitmap
1932  * @is_add: True if called before Add VSI
1933  *
1934  * Setup VSI queue mapping for enabled traffic classes.
1935  **/
1936 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1937 				     struct i40e_vsi_context *ctxt,
1938 				     u8 enabled_tc,
1939 				     bool is_add)
1940 {
1941 	struct i40e_pf *pf = vsi->back;
1942 	u16 num_tc_qps = 0;
1943 	u16 sections = 0;
1944 	u8 netdev_tc = 0;
1945 	u16 numtc = 1;
1946 	u16 qcount;
1947 	u8 offset;
1948 	u16 qmap;
1949 	int i;
1950 
1951 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1952 	offset = 0;
1953 	/* zero out queue mapping, it will get updated on the end of the function */
1954 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1955 
1956 	if (vsi->type == I40E_VSI_MAIN) {
1957 		/* This code helps add more queue to the VSI if we have
1958 		 * more cores than RSS can support, the higher cores will
1959 		 * be served by ATR or other filters. Furthermore, the
1960 		 * non-zero req_queue_pairs says that user requested a new
1961 		 * queue count via ethtool's set_channels, so use this
1962 		 * value for queues distribution across traffic classes
1963 		 */
1964 		if (vsi->req_queue_pairs > 0)
1965 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1966 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1967 			vsi->num_queue_pairs = pf->num_lan_msix;
1968 	}
1969 
1970 	/* Number of queues per enabled TC */
1971 	if (vsi->type == I40E_VSI_MAIN ||
1972 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1973 		num_tc_qps = vsi->num_queue_pairs;
1974 	else
1975 		num_tc_qps = vsi->alloc_queue_pairs;
1976 
1977 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1978 		/* Find numtc from enabled TC bitmap */
1979 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1980 			if (enabled_tc & BIT(i)) /* TC is enabled */
1981 				numtc++;
1982 		}
1983 		if (!numtc) {
1984 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1985 			numtc = 1;
1986 		}
1987 		num_tc_qps = num_tc_qps / numtc;
1988 		num_tc_qps = min_t(int, num_tc_qps,
1989 				   i40e_pf_get_max_q_per_tc(pf));
1990 	}
1991 
1992 	vsi->tc_config.numtc = numtc;
1993 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1994 
1995 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1996 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1997 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1998 
1999 	/* Setup queue offset/count for all TCs for given VSI */
2000 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2001 		/* See if the given TC is enabled for the given VSI */
2002 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2003 			/* TC is enabled */
2004 			int pow, num_qps;
2005 
2006 			switch (vsi->type) {
2007 			case I40E_VSI_MAIN:
2008 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2009 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2010 				    vsi->tc_config.enabled_tc != 1) {
2011 					qcount = min_t(int, pf->alloc_rss_size,
2012 						       num_tc_qps);
2013 					break;
2014 				}
2015 				fallthrough;
2016 			case I40E_VSI_FDIR:
2017 			case I40E_VSI_SRIOV:
2018 			case I40E_VSI_VMDQ2:
2019 			default:
2020 				qcount = num_tc_qps;
2021 				WARN_ON(i != 0);
2022 				break;
2023 			}
2024 			vsi->tc_config.tc_info[i].qoffset = offset;
2025 			vsi->tc_config.tc_info[i].qcount = qcount;
2026 
2027 			/* find the next higher power-of-2 of num queue pairs */
2028 			num_qps = qcount;
2029 			pow = 0;
2030 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2031 				pow++;
2032 				num_qps >>= 1;
2033 			}
2034 
2035 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2036 			qmap =
2037 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2038 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2039 
2040 			offset += qcount;
2041 		} else {
2042 			/* TC is not enabled so set the offset to
2043 			 * default queue and allocate one queue
2044 			 * for the given TC.
2045 			 */
2046 			vsi->tc_config.tc_info[i].qoffset = 0;
2047 			vsi->tc_config.tc_info[i].qcount = 1;
2048 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2049 
2050 			qmap = 0;
2051 		}
2052 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2053 	}
2054 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2055 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2056 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2057 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2058 		vsi->num_queue_pairs = offset;
2059 
2060 	/* Scheduler section valid can only be set for ADD VSI */
2061 	if (is_add) {
2062 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2063 
2064 		ctxt->info.up_enable_bits = enabled_tc;
2065 	}
2066 	if (vsi->type == I40E_VSI_SRIOV) {
2067 		ctxt->info.mapping_flags |=
2068 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2069 		for (i = 0; i < vsi->num_queue_pairs; i++)
2070 			ctxt->info.queue_mapping[i] =
2071 					       cpu_to_le16(vsi->base_queue + i);
2072 	} else {
2073 		ctxt->info.mapping_flags |=
2074 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2075 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2076 	}
2077 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2078 }
2079 
2080 /**
2081  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2082  * @netdev: the netdevice
2083  * @addr: address to add
2084  *
2085  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2086  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2087  */
2088 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2089 {
2090 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2091 	struct i40e_vsi *vsi = np->vsi;
2092 
2093 	if (i40e_add_mac_filter(vsi, addr))
2094 		return 0;
2095 	else
2096 		return -ENOMEM;
2097 }
2098 
2099 /**
2100  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2101  * @netdev: the netdevice
2102  * @addr: address to add
2103  *
2104  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2105  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2106  */
2107 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2108 {
2109 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2110 	struct i40e_vsi *vsi = np->vsi;
2111 
2112 	/* Under some circumstances, we might receive a request to delete
2113 	 * our own device address from our uc list. Because we store the
2114 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2115 	 * such requests and not delete our device address from this list.
2116 	 */
2117 	if (ether_addr_equal(addr, netdev->dev_addr))
2118 		return 0;
2119 
2120 	i40e_del_mac_filter(vsi, addr);
2121 
2122 	return 0;
2123 }
2124 
2125 /**
2126  * i40e_set_rx_mode - NDO callback to set the netdev filters
2127  * @netdev: network interface device structure
2128  **/
2129 static void i40e_set_rx_mode(struct net_device *netdev)
2130 {
2131 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2132 	struct i40e_vsi *vsi = np->vsi;
2133 
2134 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2135 
2136 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2137 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2138 
2139 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2140 
2141 	/* check for other flag changes */
2142 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2143 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2144 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2145 	}
2146 }
2147 
2148 /**
2149  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2150  * @vsi: Pointer to VSI struct
2151  * @from: Pointer to list which contains MAC filter entries - changes to
2152  *        those entries needs to be undone.
2153  *
2154  * MAC filter entries from this list were slated for deletion.
2155  **/
2156 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2157 					 struct hlist_head *from)
2158 {
2159 	struct i40e_mac_filter *f;
2160 	struct hlist_node *h;
2161 
2162 	hlist_for_each_entry_safe(f, h, from, hlist) {
2163 		u64 key = i40e_addr_to_hkey(f->macaddr);
2164 
2165 		/* Move the element back into MAC filter list*/
2166 		hlist_del(&f->hlist);
2167 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2168 	}
2169 }
2170 
2171 /**
2172  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2173  * @vsi: Pointer to vsi struct
2174  * @from: Pointer to list which contains MAC filter entries - changes to
2175  *        those entries needs to be undone.
2176  *
2177  * MAC filter entries from this list were slated for addition.
2178  **/
2179 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2180 					 struct hlist_head *from)
2181 {
2182 	struct i40e_new_mac_filter *new;
2183 	struct hlist_node *h;
2184 
2185 	hlist_for_each_entry_safe(new, h, from, hlist) {
2186 		/* We can simply free the wrapper structure */
2187 		hlist_del(&new->hlist);
2188 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2189 		kfree(new);
2190 	}
2191 }
2192 
2193 /**
2194  * i40e_next_filter - Get the next non-broadcast filter from a list
2195  * @next: pointer to filter in list
2196  *
2197  * Returns the next non-broadcast filter in the list. Required so that we
2198  * ignore broadcast filters within the list, since these are not handled via
2199  * the normal firmware update path.
2200  */
2201 static
2202 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2203 {
2204 	hlist_for_each_entry_continue(next, hlist) {
2205 		if (!is_broadcast_ether_addr(next->f->macaddr))
2206 			return next;
2207 	}
2208 
2209 	return NULL;
2210 }
2211 
2212 /**
2213  * i40e_update_filter_state - Update filter state based on return data
2214  * from firmware
2215  * @count: Number of filters added
2216  * @add_list: return data from fw
2217  * @add_head: pointer to first filter in current batch
2218  *
2219  * MAC filter entries from list were slated to be added to device. Returns
2220  * number of successful filters. Note that 0 does NOT mean success!
2221  **/
2222 static int
2223 i40e_update_filter_state(int count,
2224 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2225 			 struct i40e_new_mac_filter *add_head)
2226 {
2227 	int retval = 0;
2228 	int i;
2229 
2230 	for (i = 0; i < count; i++) {
2231 		/* Always check status of each filter. We don't need to check
2232 		 * the firmware return status because we pre-set the filter
2233 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2234 		 * request to the adminq. Thus, if it no longer matches then
2235 		 * we know the filter is active.
2236 		 */
2237 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2238 			add_head->state = I40E_FILTER_FAILED;
2239 		} else {
2240 			add_head->state = I40E_FILTER_ACTIVE;
2241 			retval++;
2242 		}
2243 
2244 		add_head = i40e_next_filter(add_head);
2245 		if (!add_head)
2246 			break;
2247 	}
2248 
2249 	return retval;
2250 }
2251 
2252 /**
2253  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2254  * @vsi: ptr to the VSI
2255  * @vsi_name: name to display in messages
2256  * @list: the list of filters to send to firmware
2257  * @num_del: the number of filters to delete
2258  * @retval: Set to -EIO on failure to delete
2259  *
2260  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2261  * *retval instead of a return value so that success does not force ret_val to
2262  * be set to 0. This ensures that a sequence of calls to this function
2263  * preserve the previous value of *retval on successful delete.
2264  */
2265 static
2266 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2267 			  struct i40e_aqc_remove_macvlan_element_data *list,
2268 			  int num_del, int *retval)
2269 {
2270 	struct i40e_hw *hw = &vsi->back->hw;
2271 	enum i40e_admin_queue_err aq_status;
2272 	i40e_status aq_ret;
2273 
2274 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2275 					   &aq_status);
2276 
2277 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2278 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2279 		*retval = -EIO;
2280 		dev_info(&vsi->back->pdev->dev,
2281 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2282 			 vsi_name, i40e_stat_str(hw, aq_ret),
2283 			 i40e_aq_str(hw, aq_status));
2284 	}
2285 }
2286 
2287 /**
2288  * i40e_aqc_add_filters - Request firmware to add a set of filters
2289  * @vsi: ptr to the VSI
2290  * @vsi_name: name to display in messages
2291  * @list: the list of filters to send to firmware
2292  * @add_head: Position in the add hlist
2293  * @num_add: the number of filters to add
2294  *
2295  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2296  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2297  * space for more filters.
2298  */
2299 static
2300 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2301 			  struct i40e_aqc_add_macvlan_element_data *list,
2302 			  struct i40e_new_mac_filter *add_head,
2303 			  int num_add)
2304 {
2305 	struct i40e_hw *hw = &vsi->back->hw;
2306 	enum i40e_admin_queue_err aq_status;
2307 	int fcnt;
2308 
2309 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2310 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2311 
2312 	if (fcnt != num_add) {
2313 		if (vsi->type == I40E_VSI_MAIN) {
2314 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2315 			dev_warn(&vsi->back->pdev->dev,
2316 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2317 				 i40e_aq_str(hw, aq_status), vsi_name);
2318 		} else if (vsi->type == I40E_VSI_SRIOV ||
2319 			   vsi->type == I40E_VSI_VMDQ1 ||
2320 			   vsi->type == I40E_VSI_VMDQ2) {
2321 			dev_warn(&vsi->back->pdev->dev,
2322 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2323 				 i40e_aq_str(hw, aq_status), vsi_name,
2324 					     vsi_name);
2325 		} else {
2326 			dev_warn(&vsi->back->pdev->dev,
2327 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2328 				 i40e_aq_str(hw, aq_status), vsi_name,
2329 					     vsi->type);
2330 		}
2331 	}
2332 }
2333 
2334 /**
2335  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2336  * @vsi: pointer to the VSI
2337  * @vsi_name: the VSI name
2338  * @f: filter data
2339  *
2340  * This function sets or clears the promiscuous broadcast flags for VLAN
2341  * filters in order to properly receive broadcast frames. Assumes that only
2342  * broadcast filters are passed.
2343  *
2344  * Returns status indicating success or failure;
2345  **/
2346 static i40e_status
2347 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2348 			  struct i40e_mac_filter *f)
2349 {
2350 	bool enable = f->state == I40E_FILTER_NEW;
2351 	struct i40e_hw *hw = &vsi->back->hw;
2352 	i40e_status aq_ret;
2353 
2354 	if (f->vlan == I40E_VLAN_ANY) {
2355 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2356 						   vsi->seid,
2357 						   enable,
2358 						   NULL);
2359 	} else {
2360 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2361 							    vsi->seid,
2362 							    enable,
2363 							    f->vlan,
2364 							    NULL);
2365 	}
2366 
2367 	if (aq_ret) {
2368 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2369 		dev_warn(&vsi->back->pdev->dev,
2370 			 "Error %s, forcing overflow promiscuous on %s\n",
2371 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2372 			 vsi_name);
2373 	}
2374 
2375 	return aq_ret;
2376 }
2377 
2378 /**
2379  * i40e_set_promiscuous - set promiscuous mode
2380  * @pf: board private structure
2381  * @promisc: promisc on or off
2382  *
2383  * There are different ways of setting promiscuous mode on a PF depending on
2384  * what state/environment we're in.  This identifies and sets it appropriately.
2385  * Returns 0 on success.
2386  **/
2387 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2388 {
2389 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2390 	struct i40e_hw *hw = &pf->hw;
2391 	i40e_status aq_ret;
2392 
2393 	if (vsi->type == I40E_VSI_MAIN &&
2394 	    pf->lan_veb != I40E_NO_VEB &&
2395 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2396 		/* set defport ON for Main VSI instead of true promisc
2397 		 * this way we will get all unicast/multicast and VLAN
2398 		 * promisc behavior but will not get VF or VMDq traffic
2399 		 * replicated on the Main VSI.
2400 		 */
2401 		if (promisc)
2402 			aq_ret = i40e_aq_set_default_vsi(hw,
2403 							 vsi->seid,
2404 							 NULL);
2405 		else
2406 			aq_ret = i40e_aq_clear_default_vsi(hw,
2407 							   vsi->seid,
2408 							   NULL);
2409 		if (aq_ret) {
2410 			dev_info(&pf->pdev->dev,
2411 				 "Set default VSI failed, err %s, aq_err %s\n",
2412 				 i40e_stat_str(hw, aq_ret),
2413 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2414 		}
2415 	} else {
2416 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2417 						  hw,
2418 						  vsi->seid,
2419 						  promisc, NULL,
2420 						  true);
2421 		if (aq_ret) {
2422 			dev_info(&pf->pdev->dev,
2423 				 "set unicast promisc failed, err %s, aq_err %s\n",
2424 				 i40e_stat_str(hw, aq_ret),
2425 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2426 		}
2427 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2428 						  hw,
2429 						  vsi->seid,
2430 						  promisc, NULL);
2431 		if (aq_ret) {
2432 			dev_info(&pf->pdev->dev,
2433 				 "set multicast promisc failed, err %s, aq_err %s\n",
2434 				 i40e_stat_str(hw, aq_ret),
2435 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2436 		}
2437 	}
2438 
2439 	if (!aq_ret)
2440 		pf->cur_promisc = promisc;
2441 
2442 	return aq_ret;
2443 }
2444 
2445 /**
2446  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2447  * @vsi: ptr to the VSI
2448  *
2449  * Push any outstanding VSI filter changes through the AdminQ.
2450  *
2451  * Returns 0 or error value
2452  **/
2453 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2454 {
2455 	struct hlist_head tmp_add_list, tmp_del_list;
2456 	struct i40e_mac_filter *f;
2457 	struct i40e_new_mac_filter *new, *add_head = NULL;
2458 	struct i40e_hw *hw = &vsi->back->hw;
2459 	bool old_overflow, new_overflow;
2460 	unsigned int failed_filters = 0;
2461 	unsigned int vlan_filters = 0;
2462 	char vsi_name[16] = "PF";
2463 	int filter_list_len = 0;
2464 	i40e_status aq_ret = 0;
2465 	u32 changed_flags = 0;
2466 	struct hlist_node *h;
2467 	struct i40e_pf *pf;
2468 	int num_add = 0;
2469 	int num_del = 0;
2470 	int retval = 0;
2471 	u16 cmd_flags;
2472 	int list_size;
2473 	int bkt;
2474 
2475 	/* empty array typed pointers, kcalloc later */
2476 	struct i40e_aqc_add_macvlan_element_data *add_list;
2477 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2478 
2479 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2480 		usleep_range(1000, 2000);
2481 	pf = vsi->back;
2482 
2483 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2484 
2485 	if (vsi->netdev) {
2486 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2487 		vsi->current_netdev_flags = vsi->netdev->flags;
2488 	}
2489 
2490 	INIT_HLIST_HEAD(&tmp_add_list);
2491 	INIT_HLIST_HEAD(&tmp_del_list);
2492 
2493 	if (vsi->type == I40E_VSI_SRIOV)
2494 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2495 	else if (vsi->type != I40E_VSI_MAIN)
2496 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2497 
2498 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2499 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2500 
2501 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2502 		/* Create a list of filters to delete. */
2503 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2504 			if (f->state == I40E_FILTER_REMOVE) {
2505 				/* Move the element into temporary del_list */
2506 				hash_del(&f->hlist);
2507 				hlist_add_head(&f->hlist, &tmp_del_list);
2508 
2509 				/* Avoid counting removed filters */
2510 				continue;
2511 			}
2512 			if (f->state == I40E_FILTER_NEW) {
2513 				/* Create a temporary i40e_new_mac_filter */
2514 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2515 				if (!new)
2516 					goto err_no_memory_locked;
2517 
2518 				/* Store pointer to the real filter */
2519 				new->f = f;
2520 				new->state = f->state;
2521 
2522 				/* Add it to the hash list */
2523 				hlist_add_head(&new->hlist, &tmp_add_list);
2524 			}
2525 
2526 			/* Count the number of active (current and new) VLAN
2527 			 * filters we have now. Does not count filters which
2528 			 * are marked for deletion.
2529 			 */
2530 			if (f->vlan > 0)
2531 				vlan_filters++;
2532 		}
2533 
2534 		if (vsi->type != I40E_VSI_SRIOV)
2535 			retval = i40e_correct_mac_vlan_filters
2536 				(vsi, &tmp_add_list, &tmp_del_list,
2537 				 vlan_filters);
2538 		else
2539 			retval = i40e_correct_vf_mac_vlan_filters
2540 				(vsi, &tmp_add_list, &tmp_del_list,
2541 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2542 
2543 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2544 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2545 
2546 		if (retval)
2547 			goto err_no_memory_locked;
2548 
2549 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2550 	}
2551 
2552 	/* Now process 'del_list' outside the lock */
2553 	if (!hlist_empty(&tmp_del_list)) {
2554 		filter_list_len = hw->aq.asq_buf_size /
2555 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2556 		list_size = filter_list_len *
2557 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2558 		del_list = kzalloc(list_size, GFP_ATOMIC);
2559 		if (!del_list)
2560 			goto err_no_memory;
2561 
2562 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2563 			cmd_flags = 0;
2564 
2565 			/* handle broadcast filters by updating the broadcast
2566 			 * promiscuous flag and release filter list.
2567 			 */
2568 			if (is_broadcast_ether_addr(f->macaddr)) {
2569 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2570 
2571 				hlist_del(&f->hlist);
2572 				kfree(f);
2573 				continue;
2574 			}
2575 
2576 			/* add to delete list */
2577 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2578 			if (f->vlan == I40E_VLAN_ANY) {
2579 				del_list[num_del].vlan_tag = 0;
2580 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2581 			} else {
2582 				del_list[num_del].vlan_tag =
2583 					cpu_to_le16((u16)(f->vlan));
2584 			}
2585 
2586 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2587 			del_list[num_del].flags = cmd_flags;
2588 			num_del++;
2589 
2590 			/* flush a full buffer */
2591 			if (num_del == filter_list_len) {
2592 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2593 						     num_del, &retval);
2594 				memset(del_list, 0, list_size);
2595 				num_del = 0;
2596 			}
2597 			/* Release memory for MAC filter entries which were
2598 			 * synced up with HW.
2599 			 */
2600 			hlist_del(&f->hlist);
2601 			kfree(f);
2602 		}
2603 
2604 		if (num_del) {
2605 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2606 					     num_del, &retval);
2607 		}
2608 
2609 		kfree(del_list);
2610 		del_list = NULL;
2611 	}
2612 
2613 	if (!hlist_empty(&tmp_add_list)) {
2614 		/* Do all the adds now. */
2615 		filter_list_len = hw->aq.asq_buf_size /
2616 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2617 		list_size = filter_list_len *
2618 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2619 		add_list = kzalloc(list_size, GFP_ATOMIC);
2620 		if (!add_list)
2621 			goto err_no_memory;
2622 
2623 		num_add = 0;
2624 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2625 			/* handle broadcast filters by updating the broadcast
2626 			 * promiscuous flag instead of adding a MAC filter.
2627 			 */
2628 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2629 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2630 							      new->f))
2631 					new->state = I40E_FILTER_FAILED;
2632 				else
2633 					new->state = I40E_FILTER_ACTIVE;
2634 				continue;
2635 			}
2636 
2637 			/* add to add array */
2638 			if (num_add == 0)
2639 				add_head = new;
2640 			cmd_flags = 0;
2641 			ether_addr_copy(add_list[num_add].mac_addr,
2642 					new->f->macaddr);
2643 			if (new->f->vlan == I40E_VLAN_ANY) {
2644 				add_list[num_add].vlan_tag = 0;
2645 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2646 			} else {
2647 				add_list[num_add].vlan_tag =
2648 					cpu_to_le16((u16)(new->f->vlan));
2649 			}
2650 			add_list[num_add].queue_number = 0;
2651 			/* set invalid match method for later detection */
2652 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2653 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2654 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2655 			num_add++;
2656 
2657 			/* flush a full buffer */
2658 			if (num_add == filter_list_len) {
2659 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2660 						     add_head, num_add);
2661 				memset(add_list, 0, list_size);
2662 				num_add = 0;
2663 			}
2664 		}
2665 		if (num_add) {
2666 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2667 					     num_add);
2668 		}
2669 		/* Now move all of the filters from the temp add list back to
2670 		 * the VSI's list.
2671 		 */
2672 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2673 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2674 			/* Only update the state if we're still NEW */
2675 			if (new->f->state == I40E_FILTER_NEW)
2676 				new->f->state = new->state;
2677 			hlist_del(&new->hlist);
2678 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2679 			kfree(new);
2680 		}
2681 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2682 		kfree(add_list);
2683 		add_list = NULL;
2684 	}
2685 
2686 	/* Determine the number of active and failed filters. */
2687 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2688 	vsi->active_filters = 0;
2689 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2690 		if (f->state == I40E_FILTER_ACTIVE)
2691 			vsi->active_filters++;
2692 		else if (f->state == I40E_FILTER_FAILED)
2693 			failed_filters++;
2694 	}
2695 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2696 
2697 	/* Check if we are able to exit overflow promiscuous mode. We can
2698 	 * safely exit if we didn't just enter, we no longer have any failed
2699 	 * filters, and we have reduced filters below the threshold value.
2700 	 */
2701 	if (old_overflow && !failed_filters &&
2702 	    vsi->active_filters < vsi->promisc_threshold) {
2703 		dev_info(&pf->pdev->dev,
2704 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2705 			 vsi_name);
2706 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2707 		vsi->promisc_threshold = 0;
2708 	}
2709 
2710 	/* if the VF is not trusted do not do promisc */
2711 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2712 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2713 		goto out;
2714 	}
2715 
2716 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2717 
2718 	/* If we are entering overflow promiscuous, we need to calculate a new
2719 	 * threshold for when we are safe to exit
2720 	 */
2721 	if (!old_overflow && new_overflow)
2722 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2723 
2724 	/* check for changes in promiscuous modes */
2725 	if (changed_flags & IFF_ALLMULTI) {
2726 		bool cur_multipromisc;
2727 
2728 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2729 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2730 							       vsi->seid,
2731 							       cur_multipromisc,
2732 							       NULL);
2733 		if (aq_ret) {
2734 			retval = i40e_aq_rc_to_posix(aq_ret,
2735 						     hw->aq.asq_last_status);
2736 			dev_info(&pf->pdev->dev,
2737 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2738 				 vsi_name,
2739 				 i40e_stat_str(hw, aq_ret),
2740 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2741 		} else {
2742 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2743 				 cur_multipromisc ? "entering" : "leaving");
2744 		}
2745 	}
2746 
2747 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2748 		bool cur_promisc;
2749 
2750 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2751 			       new_overflow);
2752 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2753 		if (aq_ret) {
2754 			retval = i40e_aq_rc_to_posix(aq_ret,
2755 						     hw->aq.asq_last_status);
2756 			dev_info(&pf->pdev->dev,
2757 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2758 				 cur_promisc ? "on" : "off",
2759 				 vsi_name,
2760 				 i40e_stat_str(hw, aq_ret),
2761 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2762 		}
2763 	}
2764 out:
2765 	/* if something went wrong then set the changed flag so we try again */
2766 	if (retval)
2767 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2768 
2769 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2770 	return retval;
2771 
2772 err_no_memory:
2773 	/* Restore elements on the temporary add and delete lists */
2774 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2775 err_no_memory_locked:
2776 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2777 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2778 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2779 
2780 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2781 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2782 	return -ENOMEM;
2783 }
2784 
2785 /**
2786  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2787  * @pf: board private structure
2788  **/
2789 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2790 {
2791 	int v;
2792 
2793 	if (!pf)
2794 		return;
2795 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2796 		return;
2797 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2798 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2799 		return;
2800 	}
2801 
2802 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2803 		if (pf->vsi[v] &&
2804 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2805 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2806 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2807 
2808 			if (ret) {
2809 				/* come back and try again later */
2810 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2811 					pf->state);
2812 				break;
2813 			}
2814 		}
2815 	}
2816 }
2817 
2818 /**
2819  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2820  * @vsi: the vsi
2821  **/
2822 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2823 {
2824 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2825 		return I40E_RXBUFFER_2048;
2826 	else
2827 		return I40E_RXBUFFER_3072;
2828 }
2829 
2830 /**
2831  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2832  * @netdev: network interface device structure
2833  * @new_mtu: new value for maximum frame size
2834  *
2835  * Returns 0 on success, negative on failure
2836  **/
2837 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2838 {
2839 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2840 	struct i40e_vsi *vsi = np->vsi;
2841 	struct i40e_pf *pf = vsi->back;
2842 
2843 	if (i40e_enabled_xdp_vsi(vsi)) {
2844 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2845 
2846 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2847 			return -EINVAL;
2848 	}
2849 
2850 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2851 		   netdev->mtu, new_mtu);
2852 	netdev->mtu = new_mtu;
2853 	if (netif_running(netdev))
2854 		i40e_vsi_reinit_locked(vsi);
2855 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2856 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2857 	return 0;
2858 }
2859 
2860 /**
2861  * i40e_ioctl - Access the hwtstamp interface
2862  * @netdev: network interface device structure
2863  * @ifr: interface request data
2864  * @cmd: ioctl command
2865  **/
2866 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2867 {
2868 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2869 	struct i40e_pf *pf = np->vsi->back;
2870 
2871 	switch (cmd) {
2872 	case SIOCGHWTSTAMP:
2873 		return i40e_ptp_get_ts_config(pf, ifr);
2874 	case SIOCSHWTSTAMP:
2875 		return i40e_ptp_set_ts_config(pf, ifr);
2876 	default:
2877 		return -EOPNOTSUPP;
2878 	}
2879 }
2880 
2881 /**
2882  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2883  * @vsi: the vsi being adjusted
2884  **/
2885 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2886 {
2887 	struct i40e_vsi_context ctxt;
2888 	i40e_status ret;
2889 
2890 	/* Don't modify stripping options if a port VLAN is active */
2891 	if (vsi->info.pvid)
2892 		return;
2893 
2894 	if ((vsi->info.valid_sections &
2895 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2896 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2897 		return;  /* already enabled */
2898 
2899 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2900 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2901 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2902 
2903 	ctxt.seid = vsi->seid;
2904 	ctxt.info = vsi->info;
2905 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2906 	if (ret) {
2907 		dev_info(&vsi->back->pdev->dev,
2908 			 "update vlan stripping failed, err %s aq_err %s\n",
2909 			 i40e_stat_str(&vsi->back->hw, ret),
2910 			 i40e_aq_str(&vsi->back->hw,
2911 				     vsi->back->hw.aq.asq_last_status));
2912 	}
2913 }
2914 
2915 /**
2916  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2917  * @vsi: the vsi being adjusted
2918  **/
2919 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2920 {
2921 	struct i40e_vsi_context ctxt;
2922 	i40e_status ret;
2923 
2924 	/* Don't modify stripping options if a port VLAN is active */
2925 	if (vsi->info.pvid)
2926 		return;
2927 
2928 	if ((vsi->info.valid_sections &
2929 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2930 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2931 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2932 		return;  /* already disabled */
2933 
2934 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2935 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2936 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2937 
2938 	ctxt.seid = vsi->seid;
2939 	ctxt.info = vsi->info;
2940 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2941 	if (ret) {
2942 		dev_info(&vsi->back->pdev->dev,
2943 			 "update vlan stripping failed, err %s aq_err %s\n",
2944 			 i40e_stat_str(&vsi->back->hw, ret),
2945 			 i40e_aq_str(&vsi->back->hw,
2946 				     vsi->back->hw.aq.asq_last_status));
2947 	}
2948 }
2949 
2950 /**
2951  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2952  * @vsi: the vsi being configured
2953  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2954  *
2955  * This is a helper function for adding a new MAC/VLAN filter with the
2956  * specified VLAN for each existing MAC address already in the hash table.
2957  * This function does *not* perform any accounting to update filters based on
2958  * VLAN mode.
2959  *
2960  * NOTE: this function expects to be called while under the
2961  * mac_filter_hash_lock
2962  **/
2963 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2964 {
2965 	struct i40e_mac_filter *f, *add_f;
2966 	struct hlist_node *h;
2967 	int bkt;
2968 
2969 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2970 		/* If we're asked to add a filter that has been marked for
2971 		 * removal, it is safe to simply restore it to active state.
2972 		 * __i40e_del_filter will have simply deleted any filters which
2973 		 * were previously marked NEW or FAILED, so if it is currently
2974 		 * marked REMOVE it must have previously been ACTIVE. Since we
2975 		 * haven't yet run the sync filters task, just restore this
2976 		 * filter to the ACTIVE state so that the sync task leaves it
2977 		 * in place.
2978 		 */
2979 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
2980 			f->state = I40E_FILTER_ACTIVE;
2981 			continue;
2982 		} else if (f->state == I40E_FILTER_REMOVE) {
2983 			continue;
2984 		}
2985 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2986 		if (!add_f) {
2987 			dev_info(&vsi->back->pdev->dev,
2988 				 "Could not add vlan filter %d for %pM\n",
2989 				 vid, f->macaddr);
2990 			return -ENOMEM;
2991 		}
2992 	}
2993 
2994 	return 0;
2995 }
2996 
2997 /**
2998  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2999  * @vsi: the VSI being configured
3000  * @vid: VLAN id to be added
3001  **/
3002 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3003 {
3004 	int err;
3005 
3006 	if (vsi->info.pvid)
3007 		return -EINVAL;
3008 
3009 	/* The network stack will attempt to add VID=0, with the intention to
3010 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3011 	 * these packets by default when configured to receive untagged
3012 	 * packets, so we don't need to add a filter for this case.
3013 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3014 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3015 	 * Thus, we do not want to actually add a filter for VID=0
3016 	 */
3017 	if (!vid)
3018 		return 0;
3019 
3020 	/* Locked once because all functions invoked below iterates list*/
3021 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3022 	err = i40e_add_vlan_all_mac(vsi, vid);
3023 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3024 	if (err)
3025 		return err;
3026 
3027 	/* schedule our worker thread which will take care of
3028 	 * applying the new filter changes
3029 	 */
3030 	i40e_service_event_schedule(vsi->back);
3031 	return 0;
3032 }
3033 
3034 /**
3035  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3036  * @vsi: the vsi being configured
3037  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3038  *
3039  * This function should be used to remove all VLAN filters which match the
3040  * given VID. It does not schedule the service event and does not take the
3041  * mac_filter_hash_lock so it may be combined with other operations under
3042  * a single invocation of the mac_filter_hash_lock.
3043  *
3044  * NOTE: this function expects to be called while under the
3045  * mac_filter_hash_lock
3046  */
3047 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3048 {
3049 	struct i40e_mac_filter *f;
3050 	struct hlist_node *h;
3051 	int bkt;
3052 
3053 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3054 		if (f->vlan == vid)
3055 			__i40e_del_filter(vsi, f);
3056 	}
3057 }
3058 
3059 /**
3060  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3061  * @vsi: the VSI being configured
3062  * @vid: VLAN id to be removed
3063  **/
3064 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3065 {
3066 	if (!vid || vsi->info.pvid)
3067 		return;
3068 
3069 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3070 	i40e_rm_vlan_all_mac(vsi, vid);
3071 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3072 
3073 	/* schedule our worker thread which will take care of
3074 	 * applying the new filter changes
3075 	 */
3076 	i40e_service_event_schedule(vsi->back);
3077 }
3078 
3079 /**
3080  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3081  * @netdev: network interface to be adjusted
3082  * @proto: unused protocol value
3083  * @vid: vlan id to be added
3084  *
3085  * net_device_ops implementation for adding vlan ids
3086  **/
3087 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3088 				__always_unused __be16 proto, u16 vid)
3089 {
3090 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3091 	struct i40e_vsi *vsi = np->vsi;
3092 	int ret = 0;
3093 
3094 	if (vid >= VLAN_N_VID)
3095 		return -EINVAL;
3096 
3097 	ret = i40e_vsi_add_vlan(vsi, vid);
3098 	if (!ret)
3099 		set_bit(vid, vsi->active_vlans);
3100 
3101 	return ret;
3102 }
3103 
3104 /**
3105  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3106  * @netdev: network interface to be adjusted
3107  * @proto: unused protocol value
3108  * @vid: vlan id to be added
3109  **/
3110 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3111 				    __always_unused __be16 proto, u16 vid)
3112 {
3113 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3114 	struct i40e_vsi *vsi = np->vsi;
3115 
3116 	if (vid >= VLAN_N_VID)
3117 		return;
3118 	set_bit(vid, vsi->active_vlans);
3119 }
3120 
3121 /**
3122  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3123  * @netdev: network interface to be adjusted
3124  * @proto: unused protocol value
3125  * @vid: vlan id to be removed
3126  *
3127  * net_device_ops implementation for removing vlan ids
3128  **/
3129 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3130 				 __always_unused __be16 proto, u16 vid)
3131 {
3132 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3133 	struct i40e_vsi *vsi = np->vsi;
3134 
3135 	/* return code is ignored as there is nothing a user
3136 	 * can do about failure to remove and a log message was
3137 	 * already printed from the other function
3138 	 */
3139 	i40e_vsi_kill_vlan(vsi, vid);
3140 
3141 	clear_bit(vid, vsi->active_vlans);
3142 
3143 	return 0;
3144 }
3145 
3146 /**
3147  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3148  * @vsi: the vsi being brought back up
3149  **/
3150 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3151 {
3152 	u16 vid;
3153 
3154 	if (!vsi->netdev)
3155 		return;
3156 
3157 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3158 		i40e_vlan_stripping_enable(vsi);
3159 	else
3160 		i40e_vlan_stripping_disable(vsi);
3161 
3162 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3163 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3164 					vid);
3165 }
3166 
3167 /**
3168  * i40e_vsi_add_pvid - Add pvid for the VSI
3169  * @vsi: the vsi being adjusted
3170  * @vid: the vlan id to set as a PVID
3171  **/
3172 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3173 {
3174 	struct i40e_vsi_context ctxt;
3175 	i40e_status ret;
3176 
3177 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3178 	vsi->info.pvid = cpu_to_le16(vid);
3179 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3180 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3181 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3182 
3183 	ctxt.seid = vsi->seid;
3184 	ctxt.info = vsi->info;
3185 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3186 	if (ret) {
3187 		dev_info(&vsi->back->pdev->dev,
3188 			 "add pvid failed, err %s aq_err %s\n",
3189 			 i40e_stat_str(&vsi->back->hw, ret),
3190 			 i40e_aq_str(&vsi->back->hw,
3191 				     vsi->back->hw.aq.asq_last_status));
3192 		return -ENOENT;
3193 	}
3194 
3195 	return 0;
3196 }
3197 
3198 /**
3199  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3200  * @vsi: the vsi being adjusted
3201  *
3202  * Just use the vlan_rx_register() service to put it back to normal
3203  **/
3204 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3205 {
3206 	vsi->info.pvid = 0;
3207 
3208 	i40e_vlan_stripping_disable(vsi);
3209 }
3210 
3211 /**
3212  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3213  * @vsi: ptr to the VSI
3214  *
3215  * If this function returns with an error, then it's possible one or
3216  * more of the rings is populated (while the rest are not).  It is the
3217  * callers duty to clean those orphaned rings.
3218  *
3219  * Return 0 on success, negative on failure
3220  **/
3221 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3222 {
3223 	int i, err = 0;
3224 
3225 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3226 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3227 
3228 	if (!i40e_enabled_xdp_vsi(vsi))
3229 		return err;
3230 
3231 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3232 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3233 
3234 	return err;
3235 }
3236 
3237 /**
3238  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3239  * @vsi: ptr to the VSI
3240  *
3241  * Free VSI's transmit software resources
3242  **/
3243 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3244 {
3245 	int i;
3246 
3247 	if (vsi->tx_rings) {
3248 		for (i = 0; i < vsi->num_queue_pairs; i++)
3249 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3250 				i40e_free_tx_resources(vsi->tx_rings[i]);
3251 	}
3252 
3253 	if (vsi->xdp_rings) {
3254 		for (i = 0; i < vsi->num_queue_pairs; i++)
3255 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3256 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3257 	}
3258 }
3259 
3260 /**
3261  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3262  * @vsi: ptr to the VSI
3263  *
3264  * If this function returns with an error, then it's possible one or
3265  * more of the rings is populated (while the rest are not).  It is the
3266  * callers duty to clean those orphaned rings.
3267  *
3268  * Return 0 on success, negative on failure
3269  **/
3270 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3271 {
3272 	int i, err = 0;
3273 
3274 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3275 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3276 	return err;
3277 }
3278 
3279 /**
3280  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3281  * @vsi: ptr to the VSI
3282  *
3283  * Free all receive software resources
3284  **/
3285 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3286 {
3287 	int i;
3288 
3289 	if (!vsi->rx_rings)
3290 		return;
3291 
3292 	for (i = 0; i < vsi->num_queue_pairs; i++)
3293 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3294 			i40e_free_rx_resources(vsi->rx_rings[i]);
3295 }
3296 
3297 /**
3298  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3299  * @ring: The Tx ring to configure
3300  *
3301  * This enables/disables XPS for a given Tx descriptor ring
3302  * based on the TCs enabled for the VSI that ring belongs to.
3303  **/
3304 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3305 {
3306 	int cpu;
3307 
3308 	if (!ring->q_vector || !ring->netdev || ring->ch)
3309 		return;
3310 
3311 	/* We only initialize XPS once, so as not to overwrite user settings */
3312 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3313 		return;
3314 
3315 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3316 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3317 			    ring->queue_index);
3318 }
3319 
3320 /**
3321  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3322  * @ring: The Tx or Rx ring
3323  *
3324  * Returns the AF_XDP buffer pool or NULL.
3325  **/
3326 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3327 {
3328 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3329 	int qid = ring->queue_index;
3330 
3331 	if (ring_is_xdp(ring))
3332 		qid -= ring->vsi->alloc_queue_pairs;
3333 
3334 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3335 		return NULL;
3336 
3337 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3338 }
3339 
3340 /**
3341  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3342  * @ring: The Tx ring to configure
3343  *
3344  * Configure the Tx descriptor ring in the HMC context.
3345  **/
3346 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3347 {
3348 	struct i40e_vsi *vsi = ring->vsi;
3349 	u16 pf_q = vsi->base_queue + ring->queue_index;
3350 	struct i40e_hw *hw = &vsi->back->hw;
3351 	struct i40e_hmc_obj_txq tx_ctx;
3352 	i40e_status err = 0;
3353 	u32 qtx_ctl = 0;
3354 
3355 	if (ring_is_xdp(ring))
3356 		ring->xsk_pool = i40e_xsk_pool(ring);
3357 
3358 	/* some ATR related tx ring init */
3359 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3360 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3361 		ring->atr_count = 0;
3362 	} else {
3363 		ring->atr_sample_rate = 0;
3364 	}
3365 
3366 	/* configure XPS */
3367 	i40e_config_xps_tx_ring(ring);
3368 
3369 	/* clear the context structure first */
3370 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3371 
3372 	tx_ctx.new_context = 1;
3373 	tx_ctx.base = (ring->dma / 128);
3374 	tx_ctx.qlen = ring->count;
3375 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3376 					       I40E_FLAG_FD_ATR_ENABLED));
3377 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3378 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3379 	if (vsi->type != I40E_VSI_FDIR)
3380 		tx_ctx.head_wb_ena = 1;
3381 	tx_ctx.head_wb_addr = ring->dma +
3382 			      (ring->count * sizeof(struct i40e_tx_desc));
3383 
3384 	/* As part of VSI creation/update, FW allocates certain
3385 	 * Tx arbitration queue sets for each TC enabled for
3386 	 * the VSI. The FW returns the handles to these queue
3387 	 * sets as part of the response buffer to Add VSI,
3388 	 * Update VSI, etc. AQ commands. It is expected that
3389 	 * these queue set handles be associated with the Tx
3390 	 * queues by the driver as part of the TX queue context
3391 	 * initialization. This has to be done regardless of
3392 	 * DCB as by default everything is mapped to TC0.
3393 	 */
3394 
3395 	if (ring->ch)
3396 		tx_ctx.rdylist =
3397 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3398 
3399 	else
3400 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3401 
3402 	tx_ctx.rdylist_act = 0;
3403 
3404 	/* clear the context in the HMC */
3405 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3406 	if (err) {
3407 		dev_info(&vsi->back->pdev->dev,
3408 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3409 			 ring->queue_index, pf_q, err);
3410 		return -ENOMEM;
3411 	}
3412 
3413 	/* set the context in the HMC */
3414 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3415 	if (err) {
3416 		dev_info(&vsi->back->pdev->dev,
3417 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3418 			 ring->queue_index, pf_q, err);
3419 		return -ENOMEM;
3420 	}
3421 
3422 	/* Now associate this queue with this PCI function */
3423 	if (ring->ch) {
3424 		if (ring->ch->type == I40E_VSI_VMDQ2)
3425 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3426 		else
3427 			return -EINVAL;
3428 
3429 		qtx_ctl |= (ring->ch->vsi_number <<
3430 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3431 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3432 	} else {
3433 		if (vsi->type == I40E_VSI_VMDQ2) {
3434 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3435 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3436 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3437 		} else {
3438 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3439 		}
3440 	}
3441 
3442 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3443 		    I40E_QTX_CTL_PF_INDX_MASK);
3444 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3445 	i40e_flush(hw);
3446 
3447 	/* cache tail off for easier writes later */
3448 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3449 
3450 	return 0;
3451 }
3452 
3453 /**
3454  * i40e_rx_offset - Return expected offset into page to access data
3455  * @rx_ring: Ring we are requesting offset of
3456  *
3457  * Returns the offset value for ring into the data buffer.
3458  */
3459 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3460 {
3461 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3462 }
3463 
3464 /**
3465  * i40e_configure_rx_ring - Configure a receive ring context
3466  * @ring: The Rx ring to configure
3467  *
3468  * Configure the Rx descriptor ring in the HMC context.
3469  **/
3470 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3471 {
3472 	struct i40e_vsi *vsi = ring->vsi;
3473 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3474 	u16 pf_q = vsi->base_queue + ring->queue_index;
3475 	struct i40e_hw *hw = &vsi->back->hw;
3476 	struct i40e_hmc_obj_rxq rx_ctx;
3477 	i40e_status err = 0;
3478 	bool ok;
3479 	int ret;
3480 
3481 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3482 
3483 	/* clear the context structure first */
3484 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3485 
3486 	if (ring->vsi->type == I40E_VSI_MAIN)
3487 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3488 
3489 	kfree(ring->rx_bi);
3490 	ring->xsk_pool = i40e_xsk_pool(ring);
3491 	if (ring->xsk_pool) {
3492 		ret = i40e_alloc_rx_bi_zc(ring);
3493 		if (ret)
3494 			return ret;
3495 		ring->rx_buf_len =
3496 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3497 		/* For AF_XDP ZC, we disallow packets to span on
3498 		 * multiple buffers, thus letting us skip that
3499 		 * handling in the fast-path.
3500 		 */
3501 		chain_len = 1;
3502 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3503 						 MEM_TYPE_XSK_BUFF_POOL,
3504 						 NULL);
3505 		if (ret)
3506 			return ret;
3507 		dev_info(&vsi->back->pdev->dev,
3508 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3509 			 ring->queue_index);
3510 
3511 	} else {
3512 		ret = i40e_alloc_rx_bi(ring);
3513 		if (ret)
3514 			return ret;
3515 		ring->rx_buf_len = vsi->rx_buf_len;
3516 		if (ring->vsi->type == I40E_VSI_MAIN) {
3517 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3518 							 MEM_TYPE_PAGE_SHARED,
3519 							 NULL);
3520 			if (ret)
3521 				return ret;
3522 		}
3523 	}
3524 
3525 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3526 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3527 
3528 	rx_ctx.base = (ring->dma / 128);
3529 	rx_ctx.qlen = ring->count;
3530 
3531 	/* use 16 byte descriptors */
3532 	rx_ctx.dsize = 0;
3533 
3534 	/* descriptor type is always zero
3535 	 * rx_ctx.dtype = 0;
3536 	 */
3537 	rx_ctx.hsplit_0 = 0;
3538 
3539 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3540 	if (hw->revision_id == 0)
3541 		rx_ctx.lrxqthresh = 0;
3542 	else
3543 		rx_ctx.lrxqthresh = 1;
3544 	rx_ctx.crcstrip = 1;
3545 	rx_ctx.l2tsel = 1;
3546 	/* this controls whether VLAN is stripped from inner headers */
3547 	rx_ctx.showiv = 0;
3548 	/* set the prefena field to 1 because the manual says to */
3549 	rx_ctx.prefena = 1;
3550 
3551 	/* clear the context in the HMC */
3552 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3553 	if (err) {
3554 		dev_info(&vsi->back->pdev->dev,
3555 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3556 			 ring->queue_index, pf_q, err);
3557 		return -ENOMEM;
3558 	}
3559 
3560 	/* set the context in the HMC */
3561 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3562 	if (err) {
3563 		dev_info(&vsi->back->pdev->dev,
3564 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3565 			 ring->queue_index, pf_q, err);
3566 		return -ENOMEM;
3567 	}
3568 
3569 	/* configure Rx buffer alignment */
3570 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3571 		clear_ring_build_skb_enabled(ring);
3572 	else
3573 		set_ring_build_skb_enabled(ring);
3574 
3575 	ring->rx_offset = i40e_rx_offset(ring);
3576 
3577 	/* cache tail for quicker writes, and clear the reg before use */
3578 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3579 	writel(0, ring->tail);
3580 
3581 	if (ring->xsk_pool) {
3582 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3583 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3584 	} else {
3585 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3586 	}
3587 	if (!ok) {
3588 		/* Log this in case the user has forgotten to give the kernel
3589 		 * any buffers, even later in the application.
3590 		 */
3591 		dev_info(&vsi->back->pdev->dev,
3592 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3593 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3594 			 ring->queue_index, pf_q);
3595 	}
3596 
3597 	return 0;
3598 }
3599 
3600 /**
3601  * i40e_vsi_configure_tx - Configure the VSI for Tx
3602  * @vsi: VSI structure describing this set of rings and resources
3603  *
3604  * Configure the Tx VSI for operation.
3605  **/
3606 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3607 {
3608 	int err = 0;
3609 	u16 i;
3610 
3611 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3612 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3613 
3614 	if (err || !i40e_enabled_xdp_vsi(vsi))
3615 		return err;
3616 
3617 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3618 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3619 
3620 	return err;
3621 }
3622 
3623 /**
3624  * i40e_vsi_configure_rx - Configure the VSI for Rx
3625  * @vsi: the VSI being configured
3626  *
3627  * Configure the Rx VSI for operation.
3628  **/
3629 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3630 {
3631 	int err = 0;
3632 	u16 i;
3633 
3634 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3635 		vsi->max_frame = I40E_MAX_RXBUFFER;
3636 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3637 #if (PAGE_SIZE < 8192)
3638 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3639 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3640 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3641 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3642 #endif
3643 	} else {
3644 		vsi->max_frame = I40E_MAX_RXBUFFER;
3645 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3646 						       I40E_RXBUFFER_2048;
3647 	}
3648 
3649 	/* set up individual rings */
3650 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3651 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3652 
3653 	return err;
3654 }
3655 
3656 /**
3657  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3658  * @vsi: ptr to the VSI
3659  **/
3660 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3661 {
3662 	struct i40e_ring *tx_ring, *rx_ring;
3663 	u16 qoffset, qcount;
3664 	int i, n;
3665 
3666 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3667 		/* Reset the TC information */
3668 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3669 			rx_ring = vsi->rx_rings[i];
3670 			tx_ring = vsi->tx_rings[i];
3671 			rx_ring->dcb_tc = 0;
3672 			tx_ring->dcb_tc = 0;
3673 		}
3674 		return;
3675 	}
3676 
3677 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3678 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3679 			continue;
3680 
3681 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3682 		qcount = vsi->tc_config.tc_info[n].qcount;
3683 		for (i = qoffset; i < (qoffset + qcount); i++) {
3684 			rx_ring = vsi->rx_rings[i];
3685 			tx_ring = vsi->tx_rings[i];
3686 			rx_ring->dcb_tc = n;
3687 			tx_ring->dcb_tc = n;
3688 		}
3689 	}
3690 }
3691 
3692 /**
3693  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3694  * @vsi: ptr to the VSI
3695  **/
3696 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3697 {
3698 	if (vsi->netdev)
3699 		i40e_set_rx_mode(vsi->netdev);
3700 }
3701 
3702 /**
3703  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3704  * @pf: Pointer to the targeted PF
3705  *
3706  * Set all flow director counters to 0.
3707  */
3708 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3709 {
3710 	pf->fd_tcp4_filter_cnt = 0;
3711 	pf->fd_udp4_filter_cnt = 0;
3712 	pf->fd_sctp4_filter_cnt = 0;
3713 	pf->fd_ip4_filter_cnt = 0;
3714 	pf->fd_tcp6_filter_cnt = 0;
3715 	pf->fd_udp6_filter_cnt = 0;
3716 	pf->fd_sctp6_filter_cnt = 0;
3717 	pf->fd_ip6_filter_cnt = 0;
3718 }
3719 
3720 /**
3721  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3722  * @vsi: Pointer to the targeted VSI
3723  *
3724  * This function replays the hlist on the hw where all the SB Flow Director
3725  * filters were saved.
3726  **/
3727 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3728 {
3729 	struct i40e_fdir_filter *filter;
3730 	struct i40e_pf *pf = vsi->back;
3731 	struct hlist_node *node;
3732 
3733 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3734 		return;
3735 
3736 	/* Reset FDir counters as we're replaying all existing filters */
3737 	i40e_reset_fdir_filter_cnt(pf);
3738 
3739 	hlist_for_each_entry_safe(filter, node,
3740 				  &pf->fdir_filter_list, fdir_node) {
3741 		i40e_add_del_fdir(vsi, filter, true);
3742 	}
3743 }
3744 
3745 /**
3746  * i40e_vsi_configure - Set up the VSI for action
3747  * @vsi: the VSI being configured
3748  **/
3749 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3750 {
3751 	int err;
3752 
3753 	i40e_set_vsi_rx_mode(vsi);
3754 	i40e_restore_vlan(vsi);
3755 	i40e_vsi_config_dcb_rings(vsi);
3756 	err = i40e_vsi_configure_tx(vsi);
3757 	if (!err)
3758 		err = i40e_vsi_configure_rx(vsi);
3759 
3760 	return err;
3761 }
3762 
3763 /**
3764  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3765  * @vsi: the VSI being configured
3766  **/
3767 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3768 {
3769 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3770 	struct i40e_pf *pf = vsi->back;
3771 	struct i40e_hw *hw = &pf->hw;
3772 	u16 vector;
3773 	int i, q;
3774 	u32 qp;
3775 
3776 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3777 	 * and PFINT_LNKLSTn registers, e.g.:
3778 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3779 	 */
3780 	qp = vsi->base_queue;
3781 	vector = vsi->base_vector;
3782 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3783 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3784 
3785 		q_vector->rx.next_update = jiffies + 1;
3786 		q_vector->rx.target_itr =
3787 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3788 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3789 		     q_vector->rx.target_itr >> 1);
3790 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3791 
3792 		q_vector->tx.next_update = jiffies + 1;
3793 		q_vector->tx.target_itr =
3794 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3795 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3796 		     q_vector->tx.target_itr >> 1);
3797 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3798 
3799 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3800 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3801 
3802 		/* Linked list for the queuepairs assigned to this vector */
3803 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3804 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3805 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3806 			u32 val;
3807 
3808 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3809 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3810 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3811 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3812 			      (I40E_QUEUE_TYPE_TX <<
3813 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3814 
3815 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3816 
3817 			if (has_xdp) {
3818 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3819 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3820 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3821 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3822 				      (I40E_QUEUE_TYPE_TX <<
3823 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3824 
3825 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3826 			}
3827 
3828 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3829 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3830 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3831 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3832 			      (I40E_QUEUE_TYPE_RX <<
3833 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3834 
3835 			/* Terminate the linked list */
3836 			if (q == (q_vector->num_ringpairs - 1))
3837 				val |= (I40E_QUEUE_END_OF_LIST <<
3838 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3839 
3840 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3841 			qp++;
3842 		}
3843 	}
3844 
3845 	i40e_flush(hw);
3846 }
3847 
3848 /**
3849  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3850  * @pf: pointer to private device data structure
3851  **/
3852 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3853 {
3854 	struct i40e_hw *hw = &pf->hw;
3855 	u32 val;
3856 
3857 	/* clear things first */
3858 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3859 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3860 
3861 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3862 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3863 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3864 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3865 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3866 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3867 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3868 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3869 
3870 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3871 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3872 
3873 	if (pf->flags & I40E_FLAG_PTP)
3874 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3875 
3876 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3877 
3878 	/* SW_ITR_IDX = 0, but don't change INTENA */
3879 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3880 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3881 
3882 	/* OTHER_ITR_IDX = 0 */
3883 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3884 }
3885 
3886 /**
3887  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3888  * @vsi: the VSI being configured
3889  **/
3890 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3891 {
3892 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3893 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3894 	struct i40e_pf *pf = vsi->back;
3895 	struct i40e_hw *hw = &pf->hw;
3896 	u32 val;
3897 
3898 	/* set the ITR configuration */
3899 	q_vector->rx.next_update = jiffies + 1;
3900 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3901 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3902 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3903 	q_vector->tx.next_update = jiffies + 1;
3904 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3905 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3906 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3907 
3908 	i40e_enable_misc_int_causes(pf);
3909 
3910 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3911 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3912 
3913 	/* Associate the queue pair to the vector and enable the queue int */
3914 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3915 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3916 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3917 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3918 
3919 	wr32(hw, I40E_QINT_RQCTL(0), val);
3920 
3921 	if (i40e_enabled_xdp_vsi(vsi)) {
3922 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3923 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3924 		      (I40E_QUEUE_TYPE_TX
3925 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3926 
3927 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3928 	}
3929 
3930 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3931 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3932 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3933 
3934 	wr32(hw, I40E_QINT_TQCTL(0), val);
3935 	i40e_flush(hw);
3936 }
3937 
3938 /**
3939  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3940  * @pf: board private structure
3941  **/
3942 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3943 {
3944 	struct i40e_hw *hw = &pf->hw;
3945 
3946 	wr32(hw, I40E_PFINT_DYN_CTL0,
3947 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3948 	i40e_flush(hw);
3949 }
3950 
3951 /**
3952  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3953  * @pf: board private structure
3954  **/
3955 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3956 {
3957 	struct i40e_hw *hw = &pf->hw;
3958 	u32 val;
3959 
3960 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3961 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3962 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3963 
3964 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3965 	i40e_flush(hw);
3966 }
3967 
3968 /**
3969  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3970  * @irq: interrupt number
3971  * @data: pointer to a q_vector
3972  **/
3973 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3974 {
3975 	struct i40e_q_vector *q_vector = data;
3976 
3977 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3978 		return IRQ_HANDLED;
3979 
3980 	napi_schedule_irqoff(&q_vector->napi);
3981 
3982 	return IRQ_HANDLED;
3983 }
3984 
3985 /**
3986  * i40e_irq_affinity_notify - Callback for affinity changes
3987  * @notify: context as to what irq was changed
3988  * @mask: the new affinity mask
3989  *
3990  * This is a callback function used by the irq_set_affinity_notifier function
3991  * so that we may register to receive changes to the irq affinity masks.
3992  **/
3993 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3994 				     const cpumask_t *mask)
3995 {
3996 	struct i40e_q_vector *q_vector =
3997 		container_of(notify, struct i40e_q_vector, affinity_notify);
3998 
3999 	cpumask_copy(&q_vector->affinity_mask, mask);
4000 }
4001 
4002 /**
4003  * i40e_irq_affinity_release - Callback for affinity notifier release
4004  * @ref: internal core kernel usage
4005  *
4006  * This is a callback function used by the irq_set_affinity_notifier function
4007  * to inform the current notification subscriber that they will no longer
4008  * receive notifications.
4009  **/
4010 static void i40e_irq_affinity_release(struct kref *ref) {}
4011 
4012 /**
4013  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4014  * @vsi: the VSI being configured
4015  * @basename: name for the vector
4016  *
4017  * Allocates MSI-X vectors and requests interrupts from the kernel.
4018  **/
4019 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4020 {
4021 	int q_vectors = vsi->num_q_vectors;
4022 	struct i40e_pf *pf = vsi->back;
4023 	int base = vsi->base_vector;
4024 	int rx_int_idx = 0;
4025 	int tx_int_idx = 0;
4026 	int vector, err;
4027 	int irq_num;
4028 	int cpu;
4029 
4030 	for (vector = 0; vector < q_vectors; vector++) {
4031 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4032 
4033 		irq_num = pf->msix_entries[base + vector].vector;
4034 
4035 		if (q_vector->tx.ring && q_vector->rx.ring) {
4036 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4037 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4038 			tx_int_idx++;
4039 		} else if (q_vector->rx.ring) {
4040 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4041 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4042 		} else if (q_vector->tx.ring) {
4043 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4044 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4045 		} else {
4046 			/* skip this unused q_vector */
4047 			continue;
4048 		}
4049 		err = request_irq(irq_num,
4050 				  vsi->irq_handler,
4051 				  0,
4052 				  q_vector->name,
4053 				  q_vector);
4054 		if (err) {
4055 			dev_info(&pf->pdev->dev,
4056 				 "MSIX request_irq failed, error: %d\n", err);
4057 			goto free_queue_irqs;
4058 		}
4059 
4060 		/* register for affinity change notifications */
4061 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4062 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4063 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4064 		/* Spread affinity hints out across online CPUs.
4065 		 *
4066 		 * get_cpu_mask returns a static constant mask with
4067 		 * a permanent lifetime so it's ok to pass to
4068 		 * irq_update_affinity_hint without making a copy.
4069 		 */
4070 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4071 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4072 	}
4073 
4074 	vsi->irqs_ready = true;
4075 	return 0;
4076 
4077 free_queue_irqs:
4078 	while (vector) {
4079 		vector--;
4080 		irq_num = pf->msix_entries[base + vector].vector;
4081 		irq_set_affinity_notifier(irq_num, NULL);
4082 		irq_update_affinity_hint(irq_num, NULL);
4083 		free_irq(irq_num, &vsi->q_vectors[vector]);
4084 	}
4085 	return err;
4086 }
4087 
4088 /**
4089  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4090  * @vsi: the VSI being un-configured
4091  **/
4092 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4093 {
4094 	struct i40e_pf *pf = vsi->back;
4095 	struct i40e_hw *hw = &pf->hw;
4096 	int base = vsi->base_vector;
4097 	int i;
4098 
4099 	/* disable interrupt causation from each queue */
4100 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4101 		u32 val;
4102 
4103 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4104 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4105 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4106 
4107 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4108 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4109 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4110 
4111 		if (!i40e_enabled_xdp_vsi(vsi))
4112 			continue;
4113 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4114 	}
4115 
4116 	/* disable each interrupt */
4117 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4118 		for (i = vsi->base_vector;
4119 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4120 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4121 
4122 		i40e_flush(hw);
4123 		for (i = 0; i < vsi->num_q_vectors; i++)
4124 			synchronize_irq(pf->msix_entries[i + base].vector);
4125 	} else {
4126 		/* Legacy and MSI mode - this stops all interrupt handling */
4127 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4128 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4129 		i40e_flush(hw);
4130 		synchronize_irq(pf->pdev->irq);
4131 	}
4132 }
4133 
4134 /**
4135  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4136  * @vsi: the VSI being configured
4137  **/
4138 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4139 {
4140 	struct i40e_pf *pf = vsi->back;
4141 	int i;
4142 
4143 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4144 		for (i = 0; i < vsi->num_q_vectors; i++)
4145 			i40e_irq_dynamic_enable(vsi, i);
4146 	} else {
4147 		i40e_irq_dynamic_enable_icr0(pf);
4148 	}
4149 
4150 	i40e_flush(&pf->hw);
4151 	return 0;
4152 }
4153 
4154 /**
4155  * i40e_free_misc_vector - Free the vector that handles non-queue events
4156  * @pf: board private structure
4157  **/
4158 static void i40e_free_misc_vector(struct i40e_pf *pf)
4159 {
4160 	/* Disable ICR 0 */
4161 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4162 	i40e_flush(&pf->hw);
4163 
4164 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4165 		synchronize_irq(pf->msix_entries[0].vector);
4166 		free_irq(pf->msix_entries[0].vector, pf);
4167 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4168 	}
4169 }
4170 
4171 /**
4172  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4173  * @irq: interrupt number
4174  * @data: pointer to a q_vector
4175  *
4176  * This is the handler used for all MSI/Legacy interrupts, and deals
4177  * with both queue and non-queue interrupts.  This is also used in
4178  * MSIX mode to handle the non-queue interrupts.
4179  **/
4180 static irqreturn_t i40e_intr(int irq, void *data)
4181 {
4182 	struct i40e_pf *pf = (struct i40e_pf *)data;
4183 	struct i40e_hw *hw = &pf->hw;
4184 	irqreturn_t ret = IRQ_NONE;
4185 	u32 icr0, icr0_remaining;
4186 	u32 val, ena_mask;
4187 
4188 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4189 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4190 
4191 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4192 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4193 		goto enable_intr;
4194 
4195 	/* if interrupt but no bits showing, must be SWINT */
4196 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4197 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4198 		pf->sw_int_count++;
4199 
4200 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4201 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4202 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4203 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4204 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4205 	}
4206 
4207 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4208 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4209 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4210 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4211 
4212 		/* We do not have a way to disarm Queue causes while leaving
4213 		 * interrupt enabled for all other causes, ideally
4214 		 * interrupt should be disabled while we are in NAPI but
4215 		 * this is not a performance path and napi_schedule()
4216 		 * can deal with rescheduling.
4217 		 */
4218 		if (!test_bit(__I40E_DOWN, pf->state))
4219 			napi_schedule_irqoff(&q_vector->napi);
4220 	}
4221 
4222 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4223 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4224 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4225 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4226 	}
4227 
4228 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4229 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4230 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4231 	}
4232 
4233 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4234 		/* disable any further VFLR event notifications */
4235 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4236 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4237 
4238 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4239 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4240 		} else {
4241 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4242 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4243 		}
4244 	}
4245 
4246 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4247 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4248 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4249 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4250 		val = rd32(hw, I40E_GLGEN_RSTAT);
4251 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4252 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4253 		if (val == I40E_RESET_CORER) {
4254 			pf->corer_count++;
4255 		} else if (val == I40E_RESET_GLOBR) {
4256 			pf->globr_count++;
4257 		} else if (val == I40E_RESET_EMPR) {
4258 			pf->empr_count++;
4259 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4260 		}
4261 	}
4262 
4263 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4264 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4265 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4266 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4267 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4268 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4269 	}
4270 
4271 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4272 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4273 
4274 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4275 			schedule_work(&pf->ptp_extts0_work);
4276 
4277 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4278 			i40e_ptp_tx_hwtstamp(pf);
4279 
4280 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4281 	}
4282 
4283 	/* If a critical error is pending we have no choice but to reset the
4284 	 * device.
4285 	 * Report and mask out any remaining unexpected interrupts.
4286 	 */
4287 	icr0_remaining = icr0 & ena_mask;
4288 	if (icr0_remaining) {
4289 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4290 			 icr0_remaining);
4291 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4292 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4293 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4294 			dev_info(&pf->pdev->dev, "device will be reset\n");
4295 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4296 			i40e_service_event_schedule(pf);
4297 		}
4298 		ena_mask &= ~icr0_remaining;
4299 	}
4300 	ret = IRQ_HANDLED;
4301 
4302 enable_intr:
4303 	/* re-enable interrupt causes */
4304 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4305 	if (!test_bit(__I40E_DOWN, pf->state) ||
4306 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4307 		i40e_service_event_schedule(pf);
4308 		i40e_irq_dynamic_enable_icr0(pf);
4309 	}
4310 
4311 	return ret;
4312 }
4313 
4314 /**
4315  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4316  * @tx_ring:  tx ring to clean
4317  * @budget:   how many cleans we're allowed
4318  *
4319  * Returns true if there's any budget left (e.g. the clean is finished)
4320  **/
4321 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4322 {
4323 	struct i40e_vsi *vsi = tx_ring->vsi;
4324 	u16 i = tx_ring->next_to_clean;
4325 	struct i40e_tx_buffer *tx_buf;
4326 	struct i40e_tx_desc *tx_desc;
4327 
4328 	tx_buf = &tx_ring->tx_bi[i];
4329 	tx_desc = I40E_TX_DESC(tx_ring, i);
4330 	i -= tx_ring->count;
4331 
4332 	do {
4333 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4334 
4335 		/* if next_to_watch is not set then there is no work pending */
4336 		if (!eop_desc)
4337 			break;
4338 
4339 		/* prevent any other reads prior to eop_desc */
4340 		smp_rmb();
4341 
4342 		/* if the descriptor isn't done, no work yet to do */
4343 		if (!(eop_desc->cmd_type_offset_bsz &
4344 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4345 			break;
4346 
4347 		/* clear next_to_watch to prevent false hangs */
4348 		tx_buf->next_to_watch = NULL;
4349 
4350 		tx_desc->buffer_addr = 0;
4351 		tx_desc->cmd_type_offset_bsz = 0;
4352 		/* move past filter desc */
4353 		tx_buf++;
4354 		tx_desc++;
4355 		i++;
4356 		if (unlikely(!i)) {
4357 			i -= tx_ring->count;
4358 			tx_buf = tx_ring->tx_bi;
4359 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4360 		}
4361 		/* unmap skb header data */
4362 		dma_unmap_single(tx_ring->dev,
4363 				 dma_unmap_addr(tx_buf, dma),
4364 				 dma_unmap_len(tx_buf, len),
4365 				 DMA_TO_DEVICE);
4366 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4367 			kfree(tx_buf->raw_buf);
4368 
4369 		tx_buf->raw_buf = NULL;
4370 		tx_buf->tx_flags = 0;
4371 		tx_buf->next_to_watch = NULL;
4372 		dma_unmap_len_set(tx_buf, len, 0);
4373 		tx_desc->buffer_addr = 0;
4374 		tx_desc->cmd_type_offset_bsz = 0;
4375 
4376 		/* move us past the eop_desc for start of next FD desc */
4377 		tx_buf++;
4378 		tx_desc++;
4379 		i++;
4380 		if (unlikely(!i)) {
4381 			i -= tx_ring->count;
4382 			tx_buf = tx_ring->tx_bi;
4383 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4384 		}
4385 
4386 		/* update budget accounting */
4387 		budget--;
4388 	} while (likely(budget));
4389 
4390 	i += tx_ring->count;
4391 	tx_ring->next_to_clean = i;
4392 
4393 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4394 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4395 
4396 	return budget > 0;
4397 }
4398 
4399 /**
4400  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4401  * @irq: interrupt number
4402  * @data: pointer to a q_vector
4403  **/
4404 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4405 {
4406 	struct i40e_q_vector *q_vector = data;
4407 	struct i40e_vsi *vsi;
4408 
4409 	if (!q_vector->tx.ring)
4410 		return IRQ_HANDLED;
4411 
4412 	vsi = q_vector->tx.ring->vsi;
4413 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4414 
4415 	return IRQ_HANDLED;
4416 }
4417 
4418 /**
4419  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4420  * @vsi: the VSI being configured
4421  * @v_idx: vector index
4422  * @qp_idx: queue pair index
4423  **/
4424 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4425 {
4426 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4427 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4428 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4429 
4430 	tx_ring->q_vector = q_vector;
4431 	tx_ring->next = q_vector->tx.ring;
4432 	q_vector->tx.ring = tx_ring;
4433 	q_vector->tx.count++;
4434 
4435 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4436 	if (i40e_enabled_xdp_vsi(vsi)) {
4437 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4438 
4439 		xdp_ring->q_vector = q_vector;
4440 		xdp_ring->next = q_vector->tx.ring;
4441 		q_vector->tx.ring = xdp_ring;
4442 		q_vector->tx.count++;
4443 	}
4444 
4445 	rx_ring->q_vector = q_vector;
4446 	rx_ring->next = q_vector->rx.ring;
4447 	q_vector->rx.ring = rx_ring;
4448 	q_vector->rx.count++;
4449 }
4450 
4451 /**
4452  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4453  * @vsi: the VSI being configured
4454  *
4455  * This function maps descriptor rings to the queue-specific vectors
4456  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4457  * one vector per queue pair, but on a constrained vector budget, we
4458  * group the queue pairs as "efficiently" as possible.
4459  **/
4460 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4461 {
4462 	int qp_remaining = vsi->num_queue_pairs;
4463 	int q_vectors = vsi->num_q_vectors;
4464 	int num_ringpairs;
4465 	int v_start = 0;
4466 	int qp_idx = 0;
4467 
4468 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4469 	 * group them so there are multiple queues per vector.
4470 	 * It is also important to go through all the vectors available to be
4471 	 * sure that if we don't use all the vectors, that the remaining vectors
4472 	 * are cleared. This is especially important when decreasing the
4473 	 * number of queues in use.
4474 	 */
4475 	for (; v_start < q_vectors; v_start++) {
4476 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4477 
4478 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4479 
4480 		q_vector->num_ringpairs = num_ringpairs;
4481 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4482 
4483 		q_vector->rx.count = 0;
4484 		q_vector->tx.count = 0;
4485 		q_vector->rx.ring = NULL;
4486 		q_vector->tx.ring = NULL;
4487 
4488 		while (num_ringpairs--) {
4489 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4490 			qp_idx++;
4491 			qp_remaining--;
4492 		}
4493 	}
4494 }
4495 
4496 /**
4497  * i40e_vsi_request_irq - Request IRQ from the OS
4498  * @vsi: the VSI being configured
4499  * @basename: name for the vector
4500  **/
4501 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4502 {
4503 	struct i40e_pf *pf = vsi->back;
4504 	int err;
4505 
4506 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4507 		err = i40e_vsi_request_irq_msix(vsi, basename);
4508 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4509 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4510 				  pf->int_name, pf);
4511 	else
4512 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4513 				  pf->int_name, pf);
4514 
4515 	if (err)
4516 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4517 
4518 	return err;
4519 }
4520 
4521 #ifdef CONFIG_NET_POLL_CONTROLLER
4522 /**
4523  * i40e_netpoll - A Polling 'interrupt' handler
4524  * @netdev: network interface device structure
4525  *
4526  * This is used by netconsole to send skbs without having to re-enable
4527  * interrupts.  It's not called while the normal interrupt routine is executing.
4528  **/
4529 static void i40e_netpoll(struct net_device *netdev)
4530 {
4531 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4532 	struct i40e_vsi *vsi = np->vsi;
4533 	struct i40e_pf *pf = vsi->back;
4534 	int i;
4535 
4536 	/* if interface is down do nothing */
4537 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4538 		return;
4539 
4540 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4541 		for (i = 0; i < vsi->num_q_vectors; i++)
4542 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4543 	} else {
4544 		i40e_intr(pf->pdev->irq, netdev);
4545 	}
4546 }
4547 #endif
4548 
4549 #define I40E_QTX_ENA_WAIT_COUNT 50
4550 
4551 /**
4552  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4553  * @pf: the PF being configured
4554  * @pf_q: the PF queue
4555  * @enable: enable or disable state of the queue
4556  *
4557  * This routine will wait for the given Tx queue of the PF to reach the
4558  * enabled or disabled state.
4559  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4560  * multiple retries; else will return 0 in case of success.
4561  **/
4562 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4563 {
4564 	int i;
4565 	u32 tx_reg;
4566 
4567 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4568 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4569 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4570 			break;
4571 
4572 		usleep_range(10, 20);
4573 	}
4574 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4575 		return -ETIMEDOUT;
4576 
4577 	return 0;
4578 }
4579 
4580 /**
4581  * i40e_control_tx_q - Start or stop a particular Tx queue
4582  * @pf: the PF structure
4583  * @pf_q: the PF queue to configure
4584  * @enable: start or stop the queue
4585  *
4586  * This function enables or disables a single queue. Note that any delay
4587  * required after the operation is expected to be handled by the caller of
4588  * this function.
4589  **/
4590 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4591 {
4592 	struct i40e_hw *hw = &pf->hw;
4593 	u32 tx_reg;
4594 	int i;
4595 
4596 	/* warn the TX unit of coming changes */
4597 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4598 	if (!enable)
4599 		usleep_range(10, 20);
4600 
4601 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4602 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4603 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4604 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4605 			break;
4606 		usleep_range(1000, 2000);
4607 	}
4608 
4609 	/* Skip if the queue is already in the requested state */
4610 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4611 		return;
4612 
4613 	/* turn on/off the queue */
4614 	if (enable) {
4615 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4616 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4617 	} else {
4618 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4619 	}
4620 
4621 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4622 }
4623 
4624 /**
4625  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4626  * @seid: VSI SEID
4627  * @pf: the PF structure
4628  * @pf_q: the PF queue to configure
4629  * @is_xdp: true if the queue is used for XDP
4630  * @enable: start or stop the queue
4631  **/
4632 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4633 			   bool is_xdp, bool enable)
4634 {
4635 	int ret;
4636 
4637 	i40e_control_tx_q(pf, pf_q, enable);
4638 
4639 	/* wait for the change to finish */
4640 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4641 	if (ret) {
4642 		dev_info(&pf->pdev->dev,
4643 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4644 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4645 			 (enable ? "en" : "dis"));
4646 	}
4647 
4648 	return ret;
4649 }
4650 
4651 /**
4652  * i40e_vsi_enable_tx - Start a VSI's rings
4653  * @vsi: the VSI being configured
4654  **/
4655 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4656 {
4657 	struct i40e_pf *pf = vsi->back;
4658 	int i, pf_q, ret = 0;
4659 
4660 	pf_q = vsi->base_queue;
4661 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4662 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4663 					     pf_q,
4664 					     false /*is xdp*/, true);
4665 		if (ret)
4666 			break;
4667 
4668 		if (!i40e_enabled_xdp_vsi(vsi))
4669 			continue;
4670 
4671 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4672 					     pf_q + vsi->alloc_queue_pairs,
4673 					     true /*is xdp*/, true);
4674 		if (ret)
4675 			break;
4676 	}
4677 	return ret;
4678 }
4679 
4680 /**
4681  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4682  * @pf: the PF being configured
4683  * @pf_q: the PF queue
4684  * @enable: enable or disable state of the queue
4685  *
4686  * This routine will wait for the given Rx queue of the PF to reach the
4687  * enabled or disabled state.
4688  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4689  * multiple retries; else will return 0 in case of success.
4690  **/
4691 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4692 {
4693 	int i;
4694 	u32 rx_reg;
4695 
4696 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4697 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4698 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4699 			break;
4700 
4701 		usleep_range(10, 20);
4702 	}
4703 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4704 		return -ETIMEDOUT;
4705 
4706 	return 0;
4707 }
4708 
4709 /**
4710  * i40e_control_rx_q - Start or stop a particular Rx queue
4711  * @pf: the PF structure
4712  * @pf_q: the PF queue to configure
4713  * @enable: start or stop the queue
4714  *
4715  * This function enables or disables a single queue. Note that
4716  * any delay required after the operation is expected to be
4717  * handled by the caller of this function.
4718  **/
4719 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4720 {
4721 	struct i40e_hw *hw = &pf->hw;
4722 	u32 rx_reg;
4723 	int i;
4724 
4725 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4726 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4727 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4728 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4729 			break;
4730 		usleep_range(1000, 2000);
4731 	}
4732 
4733 	/* Skip if the queue is already in the requested state */
4734 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4735 		return;
4736 
4737 	/* turn on/off the queue */
4738 	if (enable)
4739 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4740 	else
4741 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4742 
4743 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4744 }
4745 
4746 /**
4747  * i40e_control_wait_rx_q
4748  * @pf: the PF structure
4749  * @pf_q: queue being configured
4750  * @enable: start or stop the rings
4751  *
4752  * This function enables or disables a single queue along with waiting
4753  * for the change to finish. The caller of this function should handle
4754  * the delays needed in the case of disabling queues.
4755  **/
4756 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4757 {
4758 	int ret = 0;
4759 
4760 	i40e_control_rx_q(pf, pf_q, enable);
4761 
4762 	/* wait for the change to finish */
4763 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4764 	if (ret)
4765 		return ret;
4766 
4767 	return ret;
4768 }
4769 
4770 /**
4771  * i40e_vsi_enable_rx - Start a VSI's rings
4772  * @vsi: the VSI being configured
4773  **/
4774 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4775 {
4776 	struct i40e_pf *pf = vsi->back;
4777 	int i, pf_q, ret = 0;
4778 
4779 	pf_q = vsi->base_queue;
4780 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4781 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4782 		if (ret) {
4783 			dev_info(&pf->pdev->dev,
4784 				 "VSI seid %d Rx ring %d enable timeout\n",
4785 				 vsi->seid, pf_q);
4786 			break;
4787 		}
4788 	}
4789 
4790 	return ret;
4791 }
4792 
4793 /**
4794  * i40e_vsi_start_rings - Start a VSI's rings
4795  * @vsi: the VSI being configured
4796  **/
4797 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4798 {
4799 	int ret = 0;
4800 
4801 	/* do rx first for enable and last for disable */
4802 	ret = i40e_vsi_enable_rx(vsi);
4803 	if (ret)
4804 		return ret;
4805 	ret = i40e_vsi_enable_tx(vsi);
4806 
4807 	return ret;
4808 }
4809 
4810 #define I40E_DISABLE_TX_GAP_MSEC	50
4811 
4812 /**
4813  * i40e_vsi_stop_rings - Stop a VSI's rings
4814  * @vsi: the VSI being configured
4815  **/
4816 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4817 {
4818 	struct i40e_pf *pf = vsi->back;
4819 	int pf_q, err, q_end;
4820 
4821 	/* When port TX is suspended, don't wait */
4822 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4823 		return i40e_vsi_stop_rings_no_wait(vsi);
4824 
4825 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4826 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4827 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4828 
4829 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4830 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4831 		if (err)
4832 			dev_info(&pf->pdev->dev,
4833 				 "VSI seid %d Rx ring %d disable timeout\n",
4834 				 vsi->seid, pf_q);
4835 	}
4836 
4837 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4838 	pf_q = vsi->base_queue;
4839 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4840 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4841 
4842 	i40e_vsi_wait_queues_disabled(vsi);
4843 }
4844 
4845 /**
4846  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4847  * @vsi: the VSI being shutdown
4848  *
4849  * This function stops all the rings for a VSI but does not delay to verify
4850  * that rings have been disabled. It is expected that the caller is shutting
4851  * down multiple VSIs at once and will delay together for all the VSIs after
4852  * initiating the shutdown. This is particularly useful for shutting down lots
4853  * of VFs together. Otherwise, a large delay can be incurred while configuring
4854  * each VSI in serial.
4855  **/
4856 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4857 {
4858 	struct i40e_pf *pf = vsi->back;
4859 	int i, pf_q;
4860 
4861 	pf_q = vsi->base_queue;
4862 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4863 		i40e_control_tx_q(pf, pf_q, false);
4864 		i40e_control_rx_q(pf, pf_q, false);
4865 	}
4866 }
4867 
4868 /**
4869  * i40e_vsi_free_irq - Free the irq association with the OS
4870  * @vsi: the VSI being configured
4871  **/
4872 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4873 {
4874 	struct i40e_pf *pf = vsi->back;
4875 	struct i40e_hw *hw = &pf->hw;
4876 	int base = vsi->base_vector;
4877 	u32 val, qp;
4878 	int i;
4879 
4880 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4881 		if (!vsi->q_vectors)
4882 			return;
4883 
4884 		if (!vsi->irqs_ready)
4885 			return;
4886 
4887 		vsi->irqs_ready = false;
4888 		for (i = 0; i < vsi->num_q_vectors; i++) {
4889 			int irq_num;
4890 			u16 vector;
4891 
4892 			vector = i + base;
4893 			irq_num = pf->msix_entries[vector].vector;
4894 
4895 			/* free only the irqs that were actually requested */
4896 			if (!vsi->q_vectors[i] ||
4897 			    !vsi->q_vectors[i]->num_ringpairs)
4898 				continue;
4899 
4900 			/* clear the affinity notifier in the IRQ descriptor */
4901 			irq_set_affinity_notifier(irq_num, NULL);
4902 			/* remove our suggested affinity mask for this IRQ */
4903 			irq_update_affinity_hint(irq_num, NULL);
4904 			synchronize_irq(irq_num);
4905 			free_irq(irq_num, vsi->q_vectors[i]);
4906 
4907 			/* Tear down the interrupt queue link list
4908 			 *
4909 			 * We know that they come in pairs and always
4910 			 * the Rx first, then the Tx.  To clear the
4911 			 * link list, stick the EOL value into the
4912 			 * next_q field of the registers.
4913 			 */
4914 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4915 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4916 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4917 			val |= I40E_QUEUE_END_OF_LIST
4918 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4919 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4920 
4921 			while (qp != I40E_QUEUE_END_OF_LIST) {
4922 				u32 next;
4923 
4924 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4925 
4926 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4927 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4928 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4929 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4930 
4931 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4932 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4933 
4934 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4935 
4936 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4937 
4938 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4939 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4940 
4941 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4942 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4943 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4944 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4945 
4946 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4947 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4948 
4949 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4950 				qp = next;
4951 			}
4952 		}
4953 	} else {
4954 		free_irq(pf->pdev->irq, pf);
4955 
4956 		val = rd32(hw, I40E_PFINT_LNKLST0);
4957 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4958 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4959 		val |= I40E_QUEUE_END_OF_LIST
4960 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4961 		wr32(hw, I40E_PFINT_LNKLST0, val);
4962 
4963 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4964 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4965 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4966 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4967 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4968 
4969 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4970 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4971 
4972 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4973 
4974 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4975 
4976 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4977 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4978 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4979 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4980 
4981 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4982 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4983 
4984 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4985 	}
4986 }
4987 
4988 /**
4989  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4990  * @vsi: the VSI being configured
4991  * @v_idx: Index of vector to be freed
4992  *
4993  * This function frees the memory allocated to the q_vector.  In addition if
4994  * NAPI is enabled it will delete any references to the NAPI struct prior
4995  * to freeing the q_vector.
4996  **/
4997 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4998 {
4999 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5000 	struct i40e_ring *ring;
5001 
5002 	if (!q_vector)
5003 		return;
5004 
5005 	/* disassociate q_vector from rings */
5006 	i40e_for_each_ring(ring, q_vector->tx)
5007 		ring->q_vector = NULL;
5008 
5009 	i40e_for_each_ring(ring, q_vector->rx)
5010 		ring->q_vector = NULL;
5011 
5012 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5013 	if (vsi->netdev)
5014 		netif_napi_del(&q_vector->napi);
5015 
5016 	vsi->q_vectors[v_idx] = NULL;
5017 
5018 	kfree_rcu(q_vector, rcu);
5019 }
5020 
5021 /**
5022  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5023  * @vsi: the VSI being un-configured
5024  *
5025  * This frees the memory allocated to the q_vectors and
5026  * deletes references to the NAPI struct.
5027  **/
5028 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5029 {
5030 	int v_idx;
5031 
5032 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5033 		i40e_free_q_vector(vsi, v_idx);
5034 }
5035 
5036 /**
5037  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5038  * @pf: board private structure
5039  **/
5040 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5041 {
5042 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5043 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5044 		pci_disable_msix(pf->pdev);
5045 		kfree(pf->msix_entries);
5046 		pf->msix_entries = NULL;
5047 		kfree(pf->irq_pile);
5048 		pf->irq_pile = NULL;
5049 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5050 		pci_disable_msi(pf->pdev);
5051 	}
5052 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5053 }
5054 
5055 /**
5056  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5057  * @pf: board private structure
5058  *
5059  * We go through and clear interrupt specific resources and reset the structure
5060  * to pre-load conditions
5061  **/
5062 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5063 {
5064 	int i;
5065 
5066 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5067 		i40e_free_misc_vector(pf);
5068 
5069 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5070 		      I40E_IWARP_IRQ_PILE_ID);
5071 
5072 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5073 	for (i = 0; i < pf->num_alloc_vsi; i++)
5074 		if (pf->vsi[i])
5075 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5076 	i40e_reset_interrupt_capability(pf);
5077 }
5078 
5079 /**
5080  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5081  * @vsi: the VSI being configured
5082  **/
5083 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5084 {
5085 	int q_idx;
5086 
5087 	if (!vsi->netdev)
5088 		return;
5089 
5090 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5091 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5092 
5093 		if (q_vector->rx.ring || q_vector->tx.ring)
5094 			napi_enable(&q_vector->napi);
5095 	}
5096 }
5097 
5098 /**
5099  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5100  * @vsi: the VSI being configured
5101  **/
5102 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5103 {
5104 	int q_idx;
5105 
5106 	if (!vsi->netdev)
5107 		return;
5108 
5109 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5110 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5111 
5112 		if (q_vector->rx.ring || q_vector->tx.ring)
5113 			napi_disable(&q_vector->napi);
5114 	}
5115 }
5116 
5117 /**
5118  * i40e_vsi_close - Shut down a VSI
5119  * @vsi: the vsi to be quelled
5120  **/
5121 static void i40e_vsi_close(struct i40e_vsi *vsi)
5122 {
5123 	struct i40e_pf *pf = vsi->back;
5124 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5125 		i40e_down(vsi);
5126 	i40e_vsi_free_irq(vsi);
5127 	i40e_vsi_free_tx_resources(vsi);
5128 	i40e_vsi_free_rx_resources(vsi);
5129 	vsi->current_netdev_flags = 0;
5130 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5131 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5132 		set_bit(__I40E_CLIENT_RESET, pf->state);
5133 }
5134 
5135 /**
5136  * i40e_quiesce_vsi - Pause a given VSI
5137  * @vsi: the VSI being paused
5138  **/
5139 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5140 {
5141 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5142 		return;
5143 
5144 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5145 	if (vsi->netdev && netif_running(vsi->netdev))
5146 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5147 	else
5148 		i40e_vsi_close(vsi);
5149 }
5150 
5151 /**
5152  * i40e_unquiesce_vsi - Resume a given VSI
5153  * @vsi: the VSI being resumed
5154  **/
5155 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5156 {
5157 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5158 		return;
5159 
5160 	if (vsi->netdev && netif_running(vsi->netdev))
5161 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5162 	else
5163 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5164 }
5165 
5166 /**
5167  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5168  * @pf: the PF
5169  **/
5170 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5171 {
5172 	int v;
5173 
5174 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5175 		if (pf->vsi[v])
5176 			i40e_quiesce_vsi(pf->vsi[v]);
5177 	}
5178 }
5179 
5180 /**
5181  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5182  * @pf: the PF
5183  **/
5184 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5185 {
5186 	int v;
5187 
5188 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5189 		if (pf->vsi[v])
5190 			i40e_unquiesce_vsi(pf->vsi[v]);
5191 	}
5192 }
5193 
5194 /**
5195  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5196  * @vsi: the VSI being configured
5197  *
5198  * Wait until all queues on a given VSI have been disabled.
5199  **/
5200 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5201 {
5202 	struct i40e_pf *pf = vsi->back;
5203 	int i, pf_q, ret;
5204 
5205 	pf_q = vsi->base_queue;
5206 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5207 		/* Check and wait for the Tx queue */
5208 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5209 		if (ret) {
5210 			dev_info(&pf->pdev->dev,
5211 				 "VSI seid %d Tx ring %d disable timeout\n",
5212 				 vsi->seid, pf_q);
5213 			return ret;
5214 		}
5215 
5216 		if (!i40e_enabled_xdp_vsi(vsi))
5217 			goto wait_rx;
5218 
5219 		/* Check and wait for the XDP Tx queue */
5220 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5221 				       false);
5222 		if (ret) {
5223 			dev_info(&pf->pdev->dev,
5224 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5225 				 vsi->seid, pf_q);
5226 			return ret;
5227 		}
5228 wait_rx:
5229 		/* Check and wait for the Rx queue */
5230 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5231 		if (ret) {
5232 			dev_info(&pf->pdev->dev,
5233 				 "VSI seid %d Rx ring %d disable timeout\n",
5234 				 vsi->seid, pf_q);
5235 			return ret;
5236 		}
5237 	}
5238 
5239 	return 0;
5240 }
5241 
5242 #ifdef CONFIG_I40E_DCB
5243 /**
5244  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5245  * @pf: the PF
5246  *
5247  * This function waits for the queues to be in disabled state for all the
5248  * VSIs that are managed by this PF.
5249  **/
5250 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5251 {
5252 	int v, ret = 0;
5253 
5254 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5255 		if (pf->vsi[v]) {
5256 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5257 			if (ret)
5258 				break;
5259 		}
5260 	}
5261 
5262 	return ret;
5263 }
5264 
5265 #endif
5266 
5267 /**
5268  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5269  * @pf: pointer to PF
5270  *
5271  * Get TC map for ISCSI PF type that will include iSCSI TC
5272  * and LAN TC.
5273  **/
5274 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5275 {
5276 	struct i40e_dcb_app_priority_table app;
5277 	struct i40e_hw *hw = &pf->hw;
5278 	u8 enabled_tc = 1; /* TC0 is always enabled */
5279 	u8 tc, i;
5280 	/* Get the iSCSI APP TLV */
5281 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5282 
5283 	for (i = 0; i < dcbcfg->numapps; i++) {
5284 		app = dcbcfg->app[i];
5285 		if (app.selector == I40E_APP_SEL_TCPIP &&
5286 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5287 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5288 			enabled_tc |= BIT(tc);
5289 			break;
5290 		}
5291 	}
5292 
5293 	return enabled_tc;
5294 }
5295 
5296 /**
5297  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5298  * @dcbcfg: the corresponding DCBx configuration structure
5299  *
5300  * Return the number of TCs from given DCBx configuration
5301  **/
5302 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5303 {
5304 	int i, tc_unused = 0;
5305 	u8 num_tc = 0;
5306 	u8 ret = 0;
5307 
5308 	/* Scan the ETS Config Priority Table to find
5309 	 * traffic class enabled for a given priority
5310 	 * and create a bitmask of enabled TCs
5311 	 */
5312 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5313 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5314 
5315 	/* Now scan the bitmask to check for
5316 	 * contiguous TCs starting with TC0
5317 	 */
5318 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5319 		if (num_tc & BIT(i)) {
5320 			if (!tc_unused) {
5321 				ret++;
5322 			} else {
5323 				pr_err("Non-contiguous TC - Disabling DCB\n");
5324 				return 1;
5325 			}
5326 		} else {
5327 			tc_unused = 1;
5328 		}
5329 	}
5330 
5331 	/* There is always at least TC0 */
5332 	if (!ret)
5333 		ret = 1;
5334 
5335 	return ret;
5336 }
5337 
5338 /**
5339  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5340  * @dcbcfg: the corresponding DCBx configuration structure
5341  *
5342  * Query the current DCB configuration and return the number of
5343  * traffic classes enabled from the given DCBX config
5344  **/
5345 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5346 {
5347 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5348 	u8 enabled_tc = 1;
5349 	u8 i;
5350 
5351 	for (i = 0; i < num_tc; i++)
5352 		enabled_tc |= BIT(i);
5353 
5354 	return enabled_tc;
5355 }
5356 
5357 /**
5358  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5359  * @pf: PF being queried
5360  *
5361  * Query the current MQPRIO configuration and return the number of
5362  * traffic classes enabled.
5363  **/
5364 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5365 {
5366 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5367 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5368 	u8 enabled_tc = 1, i;
5369 
5370 	for (i = 1; i < num_tc; i++)
5371 		enabled_tc |= BIT(i);
5372 	return enabled_tc;
5373 }
5374 
5375 /**
5376  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5377  * @pf: PF being queried
5378  *
5379  * Return number of traffic classes enabled for the given PF
5380  **/
5381 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5382 {
5383 	struct i40e_hw *hw = &pf->hw;
5384 	u8 i, enabled_tc = 1;
5385 	u8 num_tc = 0;
5386 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5387 
5388 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5389 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5390 
5391 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5392 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5393 		return 1;
5394 
5395 	/* SFP mode will be enabled for all TCs on port */
5396 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5397 		return i40e_dcb_get_num_tc(dcbcfg);
5398 
5399 	/* MFP mode return count of enabled TCs for this PF */
5400 	if (pf->hw.func_caps.iscsi)
5401 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5402 	else
5403 		return 1; /* Only TC0 */
5404 
5405 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5406 		if (enabled_tc & BIT(i))
5407 			num_tc++;
5408 	}
5409 	return num_tc;
5410 }
5411 
5412 /**
5413  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5414  * @pf: PF being queried
5415  *
5416  * Return a bitmap for enabled traffic classes for this PF.
5417  **/
5418 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5419 {
5420 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5421 		return i40e_mqprio_get_enabled_tc(pf);
5422 
5423 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5424 	 * default TC
5425 	 */
5426 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5427 		return I40E_DEFAULT_TRAFFIC_CLASS;
5428 
5429 	/* SFP mode we want PF to be enabled for all TCs */
5430 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5431 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5432 
5433 	/* MFP enabled and iSCSI PF type */
5434 	if (pf->hw.func_caps.iscsi)
5435 		return i40e_get_iscsi_tc_map(pf);
5436 	else
5437 		return I40E_DEFAULT_TRAFFIC_CLASS;
5438 }
5439 
5440 /**
5441  * i40e_vsi_get_bw_info - Query VSI BW Information
5442  * @vsi: the VSI being queried
5443  *
5444  * Returns 0 on success, negative value on failure
5445  **/
5446 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5447 {
5448 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5449 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5450 	struct i40e_pf *pf = vsi->back;
5451 	struct i40e_hw *hw = &pf->hw;
5452 	i40e_status ret;
5453 	u32 tc_bw_max;
5454 	int i;
5455 
5456 	/* Get the VSI level BW configuration */
5457 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5458 	if (ret) {
5459 		dev_info(&pf->pdev->dev,
5460 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5461 			 i40e_stat_str(&pf->hw, ret),
5462 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5463 		return -EINVAL;
5464 	}
5465 
5466 	/* Get the VSI level BW configuration per TC */
5467 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5468 					       NULL);
5469 	if (ret) {
5470 		dev_info(&pf->pdev->dev,
5471 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5472 			 i40e_stat_str(&pf->hw, ret),
5473 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5474 		return -EINVAL;
5475 	}
5476 
5477 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5478 		dev_info(&pf->pdev->dev,
5479 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5480 			 bw_config.tc_valid_bits,
5481 			 bw_ets_config.tc_valid_bits);
5482 		/* Still continuing */
5483 	}
5484 
5485 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5486 	vsi->bw_max_quanta = bw_config.max_bw;
5487 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5488 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5489 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5490 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5491 		vsi->bw_ets_limit_credits[i] =
5492 					le16_to_cpu(bw_ets_config.credits[i]);
5493 		/* 3 bits out of 4 for each TC */
5494 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5495 	}
5496 
5497 	return 0;
5498 }
5499 
5500 /**
5501  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5502  * @vsi: the VSI being configured
5503  * @enabled_tc: TC bitmap
5504  * @bw_share: BW shared credits per TC
5505  *
5506  * Returns 0 on success, negative value on failure
5507  **/
5508 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5509 				       u8 *bw_share)
5510 {
5511 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5512 	struct i40e_pf *pf = vsi->back;
5513 	i40e_status ret;
5514 	int i;
5515 
5516 	/* There is no need to reset BW when mqprio mode is on.  */
5517 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5518 		return 0;
5519 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5520 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5521 		if (ret)
5522 			dev_info(&pf->pdev->dev,
5523 				 "Failed to reset tx rate for vsi->seid %u\n",
5524 				 vsi->seid);
5525 		return ret;
5526 	}
5527 	memset(&bw_data, 0, sizeof(bw_data));
5528 	bw_data.tc_valid_bits = enabled_tc;
5529 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5530 		bw_data.tc_bw_credits[i] = bw_share[i];
5531 
5532 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5533 	if (ret) {
5534 		dev_info(&pf->pdev->dev,
5535 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5536 			 pf->hw.aq.asq_last_status);
5537 		return -EINVAL;
5538 	}
5539 
5540 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5541 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5542 
5543 	return 0;
5544 }
5545 
5546 /**
5547  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5548  * @vsi: the VSI being configured
5549  * @enabled_tc: TC map to be enabled
5550  *
5551  **/
5552 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5553 {
5554 	struct net_device *netdev = vsi->netdev;
5555 	struct i40e_pf *pf = vsi->back;
5556 	struct i40e_hw *hw = &pf->hw;
5557 	u8 netdev_tc = 0;
5558 	int i;
5559 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5560 
5561 	if (!netdev)
5562 		return;
5563 
5564 	if (!enabled_tc) {
5565 		netdev_reset_tc(netdev);
5566 		return;
5567 	}
5568 
5569 	/* Set up actual enabled TCs on the VSI */
5570 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5571 		return;
5572 
5573 	/* set per TC queues for the VSI */
5574 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5575 		/* Only set TC queues for enabled tcs
5576 		 *
5577 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5578 		 * enabled_tc bitmap would be 0x00001001; the driver
5579 		 * will set the numtc for netdev as 2 that will be
5580 		 * referenced by the netdev layer as TC 0 and 1.
5581 		 */
5582 		if (vsi->tc_config.enabled_tc & BIT(i))
5583 			netdev_set_tc_queue(netdev,
5584 					vsi->tc_config.tc_info[i].netdev_tc,
5585 					vsi->tc_config.tc_info[i].qcount,
5586 					vsi->tc_config.tc_info[i].qoffset);
5587 	}
5588 
5589 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5590 		return;
5591 
5592 	/* Assign UP2TC map for the VSI */
5593 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5594 		/* Get the actual TC# for the UP */
5595 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5596 		/* Get the mapped netdev TC# for the UP */
5597 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5598 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5599 	}
5600 }
5601 
5602 /**
5603  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5604  * @vsi: the VSI being configured
5605  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5606  **/
5607 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5608 				      struct i40e_vsi_context *ctxt)
5609 {
5610 	/* copy just the sections touched not the entire info
5611 	 * since not all sections are valid as returned by
5612 	 * update vsi params
5613 	 */
5614 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5615 	memcpy(&vsi->info.queue_mapping,
5616 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5617 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5618 	       sizeof(vsi->info.tc_mapping));
5619 }
5620 
5621 /**
5622  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5623  * @vsi: the VSI being reconfigured
5624  * @vsi_offset: offset from main VF VSI
5625  */
5626 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5627 {
5628 	struct i40e_vsi_context ctxt = {};
5629 	struct i40e_pf *pf;
5630 	struct i40e_hw *hw;
5631 	int ret;
5632 
5633 	if (!vsi)
5634 		return I40E_ERR_PARAM;
5635 	pf = vsi->back;
5636 	hw = &pf->hw;
5637 
5638 	ctxt.seid = vsi->seid;
5639 	ctxt.pf_num = hw->pf_id;
5640 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5641 	ctxt.uplink_seid = vsi->uplink_seid;
5642 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5643 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5644 	ctxt.info = vsi->info;
5645 
5646 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5647 				 false);
5648 	if (vsi->reconfig_rss) {
5649 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5650 				      vsi->num_queue_pairs);
5651 		ret = i40e_vsi_config_rss(vsi);
5652 		if (ret) {
5653 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5654 			return ret;
5655 		}
5656 		vsi->reconfig_rss = false;
5657 	}
5658 
5659 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5660 	if (ret) {
5661 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5662 			 i40e_stat_str(hw, ret),
5663 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5664 		return ret;
5665 	}
5666 	/* update the local VSI info with updated queue map */
5667 	i40e_vsi_update_queue_map(vsi, &ctxt);
5668 	vsi->info.valid_sections = 0;
5669 
5670 	return ret;
5671 }
5672 
5673 /**
5674  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5675  * @vsi: VSI to be configured
5676  * @enabled_tc: TC bitmap
5677  *
5678  * This configures a particular VSI for TCs that are mapped to the
5679  * given TC bitmap. It uses default bandwidth share for TCs across
5680  * VSIs to configure TC for a particular VSI.
5681  *
5682  * NOTE:
5683  * It is expected that the VSI queues have been quisced before calling
5684  * this function.
5685  **/
5686 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5687 {
5688 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5689 	struct i40e_pf *pf = vsi->back;
5690 	struct i40e_hw *hw = &pf->hw;
5691 	struct i40e_vsi_context ctxt;
5692 	int ret = 0;
5693 	int i;
5694 
5695 	/* Check if enabled_tc is same as existing or new TCs */
5696 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5697 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5698 		return ret;
5699 
5700 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5701 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5702 		if (enabled_tc & BIT(i))
5703 			bw_share[i] = 1;
5704 	}
5705 
5706 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5707 	if (ret) {
5708 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5709 
5710 		dev_info(&pf->pdev->dev,
5711 			 "Failed configuring TC map %d for VSI %d\n",
5712 			 enabled_tc, vsi->seid);
5713 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5714 						  &bw_config, NULL);
5715 		if (ret) {
5716 			dev_info(&pf->pdev->dev,
5717 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5718 				 i40e_stat_str(hw, ret),
5719 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5720 			goto out;
5721 		}
5722 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5723 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5724 
5725 			if (!valid_tc)
5726 				valid_tc = bw_config.tc_valid_bits;
5727 			/* Always enable TC0, no matter what */
5728 			valid_tc |= 1;
5729 			dev_info(&pf->pdev->dev,
5730 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5731 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5732 			enabled_tc = valid_tc;
5733 		}
5734 
5735 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5736 		if (ret) {
5737 			dev_err(&pf->pdev->dev,
5738 				"Unable to  configure TC map %d for VSI %d\n",
5739 				enabled_tc, vsi->seid);
5740 			goto out;
5741 		}
5742 	}
5743 
5744 	/* Update Queue Pairs Mapping for currently enabled UPs */
5745 	ctxt.seid = vsi->seid;
5746 	ctxt.pf_num = vsi->back->hw.pf_id;
5747 	ctxt.vf_num = 0;
5748 	ctxt.uplink_seid = vsi->uplink_seid;
5749 	ctxt.info = vsi->info;
5750 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5751 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5752 		if (ret)
5753 			goto out;
5754 	} else {
5755 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5756 	}
5757 
5758 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5759 	 * queues changed.
5760 	 */
5761 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5762 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5763 				      vsi->num_queue_pairs);
5764 		ret = i40e_vsi_config_rss(vsi);
5765 		if (ret) {
5766 			dev_info(&vsi->back->pdev->dev,
5767 				 "Failed to reconfig rss for num_queues\n");
5768 			return ret;
5769 		}
5770 		vsi->reconfig_rss = false;
5771 	}
5772 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5773 		ctxt.info.valid_sections |=
5774 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5775 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5776 	}
5777 
5778 	/* Update the VSI after updating the VSI queue-mapping
5779 	 * information
5780 	 */
5781 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5782 	if (ret) {
5783 		dev_info(&pf->pdev->dev,
5784 			 "Update vsi tc config failed, err %s aq_err %s\n",
5785 			 i40e_stat_str(hw, ret),
5786 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5787 		goto out;
5788 	}
5789 	/* update the local VSI info with updated queue map */
5790 	i40e_vsi_update_queue_map(vsi, &ctxt);
5791 	vsi->info.valid_sections = 0;
5792 
5793 	/* Update current VSI BW information */
5794 	ret = i40e_vsi_get_bw_info(vsi);
5795 	if (ret) {
5796 		dev_info(&pf->pdev->dev,
5797 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5798 			 i40e_stat_str(hw, ret),
5799 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5800 		goto out;
5801 	}
5802 
5803 	/* Update the netdev TC setup */
5804 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5805 out:
5806 	return ret;
5807 }
5808 
5809 /**
5810  * i40e_get_link_speed - Returns link speed for the interface
5811  * @vsi: VSI to be configured
5812  *
5813  **/
5814 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5815 {
5816 	struct i40e_pf *pf = vsi->back;
5817 
5818 	switch (pf->hw.phy.link_info.link_speed) {
5819 	case I40E_LINK_SPEED_40GB:
5820 		return 40000;
5821 	case I40E_LINK_SPEED_25GB:
5822 		return 25000;
5823 	case I40E_LINK_SPEED_20GB:
5824 		return 20000;
5825 	case I40E_LINK_SPEED_10GB:
5826 		return 10000;
5827 	case I40E_LINK_SPEED_1GB:
5828 		return 1000;
5829 	default:
5830 		return -EINVAL;
5831 	}
5832 }
5833 
5834 /**
5835  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5836  * @vsi: VSI to be configured
5837  * @seid: seid of the channel/VSI
5838  * @max_tx_rate: max TX rate to be configured as BW limit
5839  *
5840  * Helper function to set BW limit for a given VSI
5841  **/
5842 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5843 {
5844 	struct i40e_pf *pf = vsi->back;
5845 	u64 credits = 0;
5846 	int speed = 0;
5847 	int ret = 0;
5848 
5849 	speed = i40e_get_link_speed(vsi);
5850 	if (max_tx_rate > speed) {
5851 		dev_err(&pf->pdev->dev,
5852 			"Invalid max tx rate %llu specified for VSI seid %d.",
5853 			max_tx_rate, seid);
5854 		return -EINVAL;
5855 	}
5856 	if (max_tx_rate && max_tx_rate < 50) {
5857 		dev_warn(&pf->pdev->dev,
5858 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5859 		max_tx_rate = 50;
5860 	}
5861 
5862 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5863 	credits = max_tx_rate;
5864 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5865 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5866 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5867 	if (ret)
5868 		dev_err(&pf->pdev->dev,
5869 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5870 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5871 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5872 	return ret;
5873 }
5874 
5875 /**
5876  * i40e_remove_queue_channels - Remove queue channels for the TCs
5877  * @vsi: VSI to be configured
5878  *
5879  * Remove queue channels for the TCs
5880  **/
5881 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5882 {
5883 	enum i40e_admin_queue_err last_aq_status;
5884 	struct i40e_cloud_filter *cfilter;
5885 	struct i40e_channel *ch, *ch_tmp;
5886 	struct i40e_pf *pf = vsi->back;
5887 	struct hlist_node *node;
5888 	int ret, i;
5889 
5890 	/* Reset rss size that was stored when reconfiguring rss for
5891 	 * channel VSIs with non-power-of-2 queue count.
5892 	 */
5893 	vsi->current_rss_size = 0;
5894 
5895 	/* perform cleanup for channels if they exist */
5896 	if (list_empty(&vsi->ch_list))
5897 		return;
5898 
5899 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5900 		struct i40e_vsi *p_vsi;
5901 
5902 		list_del(&ch->list);
5903 		p_vsi = ch->parent_vsi;
5904 		if (!p_vsi || !ch->initialized) {
5905 			kfree(ch);
5906 			continue;
5907 		}
5908 		/* Reset queue contexts */
5909 		for (i = 0; i < ch->num_queue_pairs; i++) {
5910 			struct i40e_ring *tx_ring, *rx_ring;
5911 			u16 pf_q;
5912 
5913 			pf_q = ch->base_queue + i;
5914 			tx_ring = vsi->tx_rings[pf_q];
5915 			tx_ring->ch = NULL;
5916 
5917 			rx_ring = vsi->rx_rings[pf_q];
5918 			rx_ring->ch = NULL;
5919 		}
5920 
5921 		/* Reset BW configured for this VSI via mqprio */
5922 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5923 		if (ret)
5924 			dev_info(&vsi->back->pdev->dev,
5925 				 "Failed to reset tx rate for ch->seid %u\n",
5926 				 ch->seid);
5927 
5928 		/* delete cloud filters associated with this channel */
5929 		hlist_for_each_entry_safe(cfilter, node,
5930 					  &pf->cloud_filter_list, cloud_node) {
5931 			if (cfilter->seid != ch->seid)
5932 				continue;
5933 
5934 			hash_del(&cfilter->cloud_node);
5935 			if (cfilter->dst_port)
5936 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5937 									cfilter,
5938 									false);
5939 			else
5940 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5941 								false);
5942 			last_aq_status = pf->hw.aq.asq_last_status;
5943 			if (ret)
5944 				dev_info(&pf->pdev->dev,
5945 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5946 					 i40e_stat_str(&pf->hw, ret),
5947 					 i40e_aq_str(&pf->hw, last_aq_status));
5948 			kfree(cfilter);
5949 		}
5950 
5951 		/* delete VSI from FW */
5952 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5953 					     NULL);
5954 		if (ret)
5955 			dev_err(&vsi->back->pdev->dev,
5956 				"unable to remove channel (%d) for parent VSI(%d)\n",
5957 				ch->seid, p_vsi->seid);
5958 		kfree(ch);
5959 	}
5960 	INIT_LIST_HEAD(&vsi->ch_list);
5961 }
5962 
5963 /**
5964  * i40e_get_max_queues_for_channel
5965  * @vsi: ptr to VSI to which channels are associated with
5966  *
5967  * Helper function which returns max value among the queue counts set on the
5968  * channels/TCs created.
5969  **/
5970 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5971 {
5972 	struct i40e_channel *ch, *ch_tmp;
5973 	int max = 0;
5974 
5975 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5976 		if (!ch->initialized)
5977 			continue;
5978 		if (ch->num_queue_pairs > max)
5979 			max = ch->num_queue_pairs;
5980 	}
5981 
5982 	return max;
5983 }
5984 
5985 /**
5986  * i40e_validate_num_queues - validate num_queues w.r.t channel
5987  * @pf: ptr to PF device
5988  * @num_queues: number of queues
5989  * @vsi: the parent VSI
5990  * @reconfig_rss: indicates should the RSS be reconfigured or not
5991  *
5992  * This function validates number of queues in the context of new channel
5993  * which is being established and determines if RSS should be reconfigured
5994  * or not for parent VSI.
5995  **/
5996 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5997 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5998 {
5999 	int max_ch_queues;
6000 
6001 	if (!reconfig_rss)
6002 		return -EINVAL;
6003 
6004 	*reconfig_rss = false;
6005 	if (vsi->current_rss_size) {
6006 		if (num_queues > vsi->current_rss_size) {
6007 			dev_dbg(&pf->pdev->dev,
6008 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6009 				num_queues, vsi->current_rss_size);
6010 			return -EINVAL;
6011 		} else if ((num_queues < vsi->current_rss_size) &&
6012 			   (!is_power_of_2(num_queues))) {
6013 			dev_dbg(&pf->pdev->dev,
6014 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6015 				num_queues, vsi->current_rss_size);
6016 			return -EINVAL;
6017 		}
6018 	}
6019 
6020 	if (!is_power_of_2(num_queues)) {
6021 		/* Find the max num_queues configured for channel if channel
6022 		 * exist.
6023 		 * if channel exist, then enforce 'num_queues' to be more than
6024 		 * max ever queues configured for channel.
6025 		 */
6026 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6027 		if (num_queues < max_ch_queues) {
6028 			dev_dbg(&pf->pdev->dev,
6029 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6030 				num_queues, max_ch_queues);
6031 			return -EINVAL;
6032 		}
6033 		*reconfig_rss = true;
6034 	}
6035 
6036 	return 0;
6037 }
6038 
6039 /**
6040  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6041  * @vsi: the VSI being setup
6042  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6043  *
6044  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6045  **/
6046 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6047 {
6048 	struct i40e_pf *pf = vsi->back;
6049 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6050 	struct i40e_hw *hw = &pf->hw;
6051 	int local_rss_size;
6052 	u8 *lut;
6053 	int ret;
6054 
6055 	if (!vsi->rss_size)
6056 		return -EINVAL;
6057 
6058 	if (rss_size > vsi->rss_size)
6059 		return -EINVAL;
6060 
6061 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6062 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6063 	if (!lut)
6064 		return -ENOMEM;
6065 
6066 	/* Ignoring user configured lut if there is one */
6067 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6068 
6069 	/* Use user configured hash key if there is one, otherwise
6070 	 * use default.
6071 	 */
6072 	if (vsi->rss_hkey_user)
6073 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6074 	else
6075 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6076 
6077 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6078 	if (ret) {
6079 		dev_info(&pf->pdev->dev,
6080 			 "Cannot set RSS lut, err %s aq_err %s\n",
6081 			 i40e_stat_str(hw, ret),
6082 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6083 		kfree(lut);
6084 		return ret;
6085 	}
6086 	kfree(lut);
6087 
6088 	/* Do the update w.r.t. storing rss_size */
6089 	if (!vsi->orig_rss_size)
6090 		vsi->orig_rss_size = vsi->rss_size;
6091 	vsi->current_rss_size = local_rss_size;
6092 
6093 	return ret;
6094 }
6095 
6096 /**
6097  * i40e_channel_setup_queue_map - Setup a channel queue map
6098  * @pf: ptr to PF device
6099  * @ctxt: VSI context structure
6100  * @ch: ptr to channel structure
6101  *
6102  * Setup queue map for a specific channel
6103  **/
6104 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6105 					 struct i40e_vsi_context *ctxt,
6106 					 struct i40e_channel *ch)
6107 {
6108 	u16 qcount, qmap, sections = 0;
6109 	u8 offset = 0;
6110 	int pow;
6111 
6112 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6113 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6114 
6115 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6116 	ch->num_queue_pairs = qcount;
6117 
6118 	/* find the next higher power-of-2 of num queue pairs */
6119 	pow = ilog2(qcount);
6120 	if (!is_power_of_2(qcount))
6121 		pow++;
6122 
6123 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6124 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6125 
6126 	/* Setup queue TC[0].qmap for given VSI context */
6127 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6128 
6129 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6130 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6131 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6132 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6133 }
6134 
6135 /**
6136  * i40e_add_channel - add a channel by adding VSI
6137  * @pf: ptr to PF device
6138  * @uplink_seid: underlying HW switching element (VEB) ID
6139  * @ch: ptr to channel structure
6140  *
6141  * Add a channel (VSI) using add_vsi and queue_map
6142  **/
6143 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6144 			    struct i40e_channel *ch)
6145 {
6146 	struct i40e_hw *hw = &pf->hw;
6147 	struct i40e_vsi_context ctxt;
6148 	u8 enabled_tc = 0x1; /* TC0 enabled */
6149 	int ret;
6150 
6151 	if (ch->type != I40E_VSI_VMDQ2) {
6152 		dev_info(&pf->pdev->dev,
6153 			 "add new vsi failed, ch->type %d\n", ch->type);
6154 		return -EINVAL;
6155 	}
6156 
6157 	memset(&ctxt, 0, sizeof(ctxt));
6158 	ctxt.pf_num = hw->pf_id;
6159 	ctxt.vf_num = 0;
6160 	ctxt.uplink_seid = uplink_seid;
6161 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6162 	if (ch->type == I40E_VSI_VMDQ2)
6163 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6164 
6165 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6166 		ctxt.info.valid_sections |=
6167 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6168 		ctxt.info.switch_id =
6169 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6170 	}
6171 
6172 	/* Set queue map for a given VSI context */
6173 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6174 
6175 	/* Now time to create VSI */
6176 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6177 	if (ret) {
6178 		dev_info(&pf->pdev->dev,
6179 			 "add new vsi failed, err %s aq_err %s\n",
6180 			 i40e_stat_str(&pf->hw, ret),
6181 			 i40e_aq_str(&pf->hw,
6182 				     pf->hw.aq.asq_last_status));
6183 		return -ENOENT;
6184 	}
6185 
6186 	/* Success, update channel, set enabled_tc only if the channel
6187 	 * is not a macvlan
6188 	 */
6189 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6190 	ch->seid = ctxt.seid;
6191 	ch->vsi_number = ctxt.vsi_number;
6192 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6193 
6194 	/* copy just the sections touched not the entire info
6195 	 * since not all sections are valid as returned by
6196 	 * update vsi params
6197 	 */
6198 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6199 	memcpy(&ch->info.queue_mapping,
6200 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6201 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6202 	       sizeof(ctxt.info.tc_mapping));
6203 
6204 	return 0;
6205 }
6206 
6207 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6208 				  u8 *bw_share)
6209 {
6210 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6211 	i40e_status ret;
6212 	int i;
6213 
6214 	memset(&bw_data, 0, sizeof(bw_data));
6215 	bw_data.tc_valid_bits = ch->enabled_tc;
6216 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6217 		bw_data.tc_bw_credits[i] = bw_share[i];
6218 
6219 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6220 				       &bw_data, NULL);
6221 	if (ret) {
6222 		dev_info(&vsi->back->pdev->dev,
6223 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6224 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6225 		return -EINVAL;
6226 	}
6227 
6228 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6229 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6230 
6231 	return 0;
6232 }
6233 
6234 /**
6235  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6236  * @pf: ptr to PF device
6237  * @vsi: the VSI being setup
6238  * @ch: ptr to channel structure
6239  *
6240  * Configure TX rings associated with channel (VSI) since queues are being
6241  * from parent VSI.
6242  **/
6243 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6244 				       struct i40e_vsi *vsi,
6245 				       struct i40e_channel *ch)
6246 {
6247 	i40e_status ret;
6248 	int i;
6249 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6250 
6251 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6252 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6253 		if (ch->enabled_tc & BIT(i))
6254 			bw_share[i] = 1;
6255 	}
6256 
6257 	/* configure BW for new VSI */
6258 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6259 	if (ret) {
6260 		dev_info(&vsi->back->pdev->dev,
6261 			 "Failed configuring TC map %d for channel (seid %u)\n",
6262 			 ch->enabled_tc, ch->seid);
6263 		return ret;
6264 	}
6265 
6266 	for (i = 0; i < ch->num_queue_pairs; i++) {
6267 		struct i40e_ring *tx_ring, *rx_ring;
6268 		u16 pf_q;
6269 
6270 		pf_q = ch->base_queue + i;
6271 
6272 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6273 		 * context
6274 		 */
6275 		tx_ring = vsi->tx_rings[pf_q];
6276 		tx_ring->ch = ch;
6277 
6278 		/* Get the RX ring ptr */
6279 		rx_ring = vsi->rx_rings[pf_q];
6280 		rx_ring->ch = ch;
6281 	}
6282 
6283 	return 0;
6284 }
6285 
6286 /**
6287  * i40e_setup_hw_channel - setup new channel
6288  * @pf: ptr to PF device
6289  * @vsi: the VSI being setup
6290  * @ch: ptr to channel structure
6291  * @uplink_seid: underlying HW switching element (VEB) ID
6292  * @type: type of channel to be created (VMDq2/VF)
6293  *
6294  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6295  * and configures TX rings accordingly
6296  **/
6297 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6298 					struct i40e_vsi *vsi,
6299 					struct i40e_channel *ch,
6300 					u16 uplink_seid, u8 type)
6301 {
6302 	int ret;
6303 
6304 	ch->initialized = false;
6305 	ch->base_queue = vsi->next_base_queue;
6306 	ch->type = type;
6307 
6308 	/* Proceed with creation of channel (VMDq2) VSI */
6309 	ret = i40e_add_channel(pf, uplink_seid, ch);
6310 	if (ret) {
6311 		dev_info(&pf->pdev->dev,
6312 			 "failed to add_channel using uplink_seid %u\n",
6313 			 uplink_seid);
6314 		return ret;
6315 	}
6316 
6317 	/* Mark the successful creation of channel */
6318 	ch->initialized = true;
6319 
6320 	/* Reconfigure TX queues using QTX_CTL register */
6321 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6322 	if (ret) {
6323 		dev_info(&pf->pdev->dev,
6324 			 "failed to configure TX rings for channel %u\n",
6325 			 ch->seid);
6326 		return ret;
6327 	}
6328 
6329 	/* update 'next_base_queue' */
6330 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6331 	dev_dbg(&pf->pdev->dev,
6332 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6333 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6334 		ch->num_queue_pairs,
6335 		vsi->next_base_queue);
6336 	return ret;
6337 }
6338 
6339 /**
6340  * i40e_setup_channel - setup new channel using uplink element
6341  * @pf: ptr to PF device
6342  * @vsi: pointer to the VSI to set up the channel within
6343  * @ch: ptr to channel structure
6344  *
6345  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6346  * and uplink switching element (uplink_seid)
6347  **/
6348 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6349 			       struct i40e_channel *ch)
6350 {
6351 	u8 vsi_type;
6352 	u16 seid;
6353 	int ret;
6354 
6355 	if (vsi->type == I40E_VSI_MAIN) {
6356 		vsi_type = I40E_VSI_VMDQ2;
6357 	} else {
6358 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6359 			vsi->type);
6360 		return false;
6361 	}
6362 
6363 	/* underlying switching element */
6364 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6365 
6366 	/* create channel (VSI), configure TX rings */
6367 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6368 	if (ret) {
6369 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6370 		return false;
6371 	}
6372 
6373 	return ch->initialized ? true : false;
6374 }
6375 
6376 /**
6377  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6378  * @vsi: ptr to VSI which has PF backing
6379  *
6380  * Sets up switch mode correctly if it needs to be changed and perform
6381  * what are allowed modes.
6382  **/
6383 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6384 {
6385 	u8 mode;
6386 	struct i40e_pf *pf = vsi->back;
6387 	struct i40e_hw *hw = &pf->hw;
6388 	int ret;
6389 
6390 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6391 	if (ret)
6392 		return -EINVAL;
6393 
6394 	if (hw->dev_caps.switch_mode) {
6395 		/* if switch mode is set, support mode2 (non-tunneled for
6396 		 * cloud filter) for now
6397 		 */
6398 		u32 switch_mode = hw->dev_caps.switch_mode &
6399 				  I40E_SWITCH_MODE_MASK;
6400 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6401 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6402 				return 0;
6403 			dev_err(&pf->pdev->dev,
6404 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6405 				hw->dev_caps.switch_mode);
6406 			return -EINVAL;
6407 		}
6408 	}
6409 
6410 	/* Set Bit 7 to be valid */
6411 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6412 
6413 	/* Set L4type for TCP support */
6414 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6415 
6416 	/* Set cloud filter mode */
6417 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6418 
6419 	/* Prep mode field for set_switch_config */
6420 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6421 					pf->last_sw_conf_valid_flags,
6422 					mode, NULL);
6423 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6424 		dev_err(&pf->pdev->dev,
6425 			"couldn't set switch config bits, err %s aq_err %s\n",
6426 			i40e_stat_str(hw, ret),
6427 			i40e_aq_str(hw,
6428 				    hw->aq.asq_last_status));
6429 
6430 	return ret;
6431 }
6432 
6433 /**
6434  * i40e_create_queue_channel - function to create channel
6435  * @vsi: VSI to be configured
6436  * @ch: ptr to channel (it contains channel specific params)
6437  *
6438  * This function creates channel (VSI) using num_queues specified by user,
6439  * reconfigs RSS if needed.
6440  **/
6441 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6442 			      struct i40e_channel *ch)
6443 {
6444 	struct i40e_pf *pf = vsi->back;
6445 	bool reconfig_rss;
6446 	int err;
6447 
6448 	if (!ch)
6449 		return -EINVAL;
6450 
6451 	if (!ch->num_queue_pairs) {
6452 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6453 			ch->num_queue_pairs);
6454 		return -EINVAL;
6455 	}
6456 
6457 	/* validate user requested num_queues for channel */
6458 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6459 				       &reconfig_rss);
6460 	if (err) {
6461 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6462 			 ch->num_queue_pairs);
6463 		return -EINVAL;
6464 	}
6465 
6466 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6467 	 * VSI to be added switch to VEB mode.
6468 	 */
6469 
6470 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6471 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6472 
6473 		if (vsi->type == I40E_VSI_MAIN) {
6474 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6475 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6476 			else
6477 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6478 		}
6479 		/* now onwards for main VSI, number of queues will be value
6480 		 * of TC0's queue count
6481 		 */
6482 	}
6483 
6484 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6485 	 * it should be more than num_queues
6486 	 */
6487 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6488 		dev_dbg(&pf->pdev->dev,
6489 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6490 			vsi->cnt_q_avail, ch->num_queue_pairs);
6491 		return -EINVAL;
6492 	}
6493 
6494 	/* reconfig_rss only if vsi type is MAIN_VSI */
6495 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6496 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6497 		if (err) {
6498 			dev_info(&pf->pdev->dev,
6499 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6500 				 ch->num_queue_pairs);
6501 			return -EINVAL;
6502 		}
6503 	}
6504 
6505 	if (!i40e_setup_channel(pf, vsi, ch)) {
6506 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6507 		return -EINVAL;
6508 	}
6509 
6510 	dev_info(&pf->pdev->dev,
6511 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6512 		 ch->seid, ch->num_queue_pairs);
6513 
6514 	/* configure VSI for BW limit */
6515 	if (ch->max_tx_rate) {
6516 		u64 credits = ch->max_tx_rate;
6517 
6518 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6519 			return -EINVAL;
6520 
6521 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6522 		dev_dbg(&pf->pdev->dev,
6523 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6524 			ch->max_tx_rate,
6525 			credits,
6526 			ch->seid);
6527 	}
6528 
6529 	/* in case of VF, this will be main SRIOV VSI */
6530 	ch->parent_vsi = vsi;
6531 
6532 	/* and update main_vsi's count for queue_available to use */
6533 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6534 
6535 	return 0;
6536 }
6537 
6538 /**
6539  * i40e_configure_queue_channels - Add queue channel for the given TCs
6540  * @vsi: VSI to be configured
6541  *
6542  * Configures queue channel mapping to the given TCs
6543  **/
6544 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6545 {
6546 	struct i40e_channel *ch;
6547 	u64 max_rate = 0;
6548 	int ret = 0, i;
6549 
6550 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6551 	vsi->tc_seid_map[0] = vsi->seid;
6552 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6553 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6554 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6555 			if (!ch) {
6556 				ret = -ENOMEM;
6557 				goto err_free;
6558 			}
6559 
6560 			INIT_LIST_HEAD(&ch->list);
6561 			ch->num_queue_pairs =
6562 				vsi->tc_config.tc_info[i].qcount;
6563 			ch->base_queue =
6564 				vsi->tc_config.tc_info[i].qoffset;
6565 
6566 			/* Bandwidth limit through tc interface is in bytes/s,
6567 			 * change to Mbit/s
6568 			 */
6569 			max_rate = vsi->mqprio_qopt.max_rate[i];
6570 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6571 			ch->max_tx_rate = max_rate;
6572 
6573 			list_add_tail(&ch->list, &vsi->ch_list);
6574 
6575 			ret = i40e_create_queue_channel(vsi, ch);
6576 			if (ret) {
6577 				dev_err(&vsi->back->pdev->dev,
6578 					"Failed creating queue channel with TC%d: queues %d\n",
6579 					i, ch->num_queue_pairs);
6580 				goto err_free;
6581 			}
6582 			vsi->tc_seid_map[i] = ch->seid;
6583 		}
6584 	}
6585 	return ret;
6586 
6587 err_free:
6588 	i40e_remove_queue_channels(vsi);
6589 	return ret;
6590 }
6591 
6592 /**
6593  * i40e_veb_config_tc - Configure TCs for given VEB
6594  * @veb: given VEB
6595  * @enabled_tc: TC bitmap
6596  *
6597  * Configures given TC bitmap for VEB (switching) element
6598  **/
6599 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6600 {
6601 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6602 	struct i40e_pf *pf = veb->pf;
6603 	int ret = 0;
6604 	int i;
6605 
6606 	/* No TCs or already enabled TCs just return */
6607 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6608 		return ret;
6609 
6610 	bw_data.tc_valid_bits = enabled_tc;
6611 	/* bw_data.absolute_credits is not set (relative) */
6612 
6613 	/* Enable ETS TCs with equal BW Share for now */
6614 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6615 		if (enabled_tc & BIT(i))
6616 			bw_data.tc_bw_share_credits[i] = 1;
6617 	}
6618 
6619 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6620 						   &bw_data, NULL);
6621 	if (ret) {
6622 		dev_info(&pf->pdev->dev,
6623 			 "VEB bw config failed, err %s aq_err %s\n",
6624 			 i40e_stat_str(&pf->hw, ret),
6625 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6626 		goto out;
6627 	}
6628 
6629 	/* Update the BW information */
6630 	ret = i40e_veb_get_bw_info(veb);
6631 	if (ret) {
6632 		dev_info(&pf->pdev->dev,
6633 			 "Failed getting veb bw config, err %s aq_err %s\n",
6634 			 i40e_stat_str(&pf->hw, ret),
6635 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6636 	}
6637 
6638 out:
6639 	return ret;
6640 }
6641 
6642 #ifdef CONFIG_I40E_DCB
6643 /**
6644  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6645  * @pf: PF struct
6646  *
6647  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6648  * the caller would've quiesce all the VSIs before calling
6649  * this function
6650  **/
6651 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6652 {
6653 	u8 tc_map = 0;
6654 	int ret;
6655 	u8 v;
6656 
6657 	/* Enable the TCs available on PF to all VEBs */
6658 	tc_map = i40e_pf_get_tc_map(pf);
6659 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6660 		return;
6661 
6662 	for (v = 0; v < I40E_MAX_VEB; v++) {
6663 		if (!pf->veb[v])
6664 			continue;
6665 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6666 		if (ret) {
6667 			dev_info(&pf->pdev->dev,
6668 				 "Failed configuring TC for VEB seid=%d\n",
6669 				 pf->veb[v]->seid);
6670 			/* Will try to configure as many components */
6671 		}
6672 	}
6673 
6674 	/* Update each VSI */
6675 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6676 		if (!pf->vsi[v])
6677 			continue;
6678 
6679 		/* - Enable all TCs for the LAN VSI
6680 		 * - For all others keep them at TC0 for now
6681 		 */
6682 		if (v == pf->lan_vsi)
6683 			tc_map = i40e_pf_get_tc_map(pf);
6684 		else
6685 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6686 
6687 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6688 		if (ret) {
6689 			dev_info(&pf->pdev->dev,
6690 				 "Failed configuring TC for VSI seid=%d\n",
6691 				 pf->vsi[v]->seid);
6692 			/* Will try to configure as many components */
6693 		} else {
6694 			/* Re-configure VSI vectors based on updated TC map */
6695 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6696 			if (pf->vsi[v]->netdev)
6697 				i40e_dcbnl_set_all(pf->vsi[v]);
6698 		}
6699 	}
6700 }
6701 
6702 /**
6703  * i40e_resume_port_tx - Resume port Tx
6704  * @pf: PF struct
6705  *
6706  * Resume a port's Tx and issue a PF reset in case of failure to
6707  * resume.
6708  **/
6709 static int i40e_resume_port_tx(struct i40e_pf *pf)
6710 {
6711 	struct i40e_hw *hw = &pf->hw;
6712 	int ret;
6713 
6714 	ret = i40e_aq_resume_port_tx(hw, NULL);
6715 	if (ret) {
6716 		dev_info(&pf->pdev->dev,
6717 			 "Resume Port Tx failed, err %s aq_err %s\n",
6718 			  i40e_stat_str(&pf->hw, ret),
6719 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6720 		/* Schedule PF reset to recover */
6721 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6722 		i40e_service_event_schedule(pf);
6723 	}
6724 
6725 	return ret;
6726 }
6727 
6728 /**
6729  * i40e_suspend_port_tx - Suspend port Tx
6730  * @pf: PF struct
6731  *
6732  * Suspend a port's Tx and issue a PF reset in case of failure.
6733  **/
6734 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6735 {
6736 	struct i40e_hw *hw = &pf->hw;
6737 	int ret;
6738 
6739 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6740 	if (ret) {
6741 		dev_info(&pf->pdev->dev,
6742 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6743 			 i40e_stat_str(&pf->hw, ret),
6744 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6745 		/* Schedule PF reset to recover */
6746 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6747 		i40e_service_event_schedule(pf);
6748 	}
6749 
6750 	return ret;
6751 }
6752 
6753 /**
6754  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6755  * @pf: PF being configured
6756  * @new_cfg: New DCBX configuration
6757  *
6758  * Program DCB settings into HW and reconfigure VEB/VSIs on
6759  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6760  **/
6761 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6762 				  struct i40e_dcbx_config *new_cfg)
6763 {
6764 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6765 	int ret;
6766 
6767 	/* Check if need reconfiguration */
6768 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6769 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6770 		return 0;
6771 	}
6772 
6773 	/* Config change disable all VSIs */
6774 	i40e_pf_quiesce_all_vsi(pf);
6775 
6776 	/* Copy the new config to the current config */
6777 	*old_cfg = *new_cfg;
6778 	old_cfg->etsrec = old_cfg->etscfg;
6779 	ret = i40e_set_dcb_config(&pf->hw);
6780 	if (ret) {
6781 		dev_info(&pf->pdev->dev,
6782 			 "Set DCB Config failed, err %s aq_err %s\n",
6783 			 i40e_stat_str(&pf->hw, ret),
6784 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6785 		goto out;
6786 	}
6787 
6788 	/* Changes in configuration update VEB/VSI */
6789 	i40e_dcb_reconfigure(pf);
6790 out:
6791 	/* In case of reset do not try to resume anything */
6792 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6793 		/* Re-start the VSIs if disabled */
6794 		ret = i40e_resume_port_tx(pf);
6795 		/* In case of error no point in resuming VSIs */
6796 		if (ret)
6797 			goto err;
6798 		i40e_pf_unquiesce_all_vsi(pf);
6799 	}
6800 err:
6801 	return ret;
6802 }
6803 
6804 /**
6805  * i40e_hw_dcb_config - Program new DCBX settings into HW
6806  * @pf: PF being configured
6807  * @new_cfg: New DCBX configuration
6808  *
6809  * Program DCB settings into HW and reconfigure VEB/VSIs on
6810  * given PF
6811  **/
6812 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6813 {
6814 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6815 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6816 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6817 	struct i40e_dcbx_config *old_cfg;
6818 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6819 	struct i40e_rx_pb_config pb_cfg;
6820 	struct i40e_hw *hw = &pf->hw;
6821 	u8 num_ports = hw->num_ports;
6822 	bool need_reconfig;
6823 	int ret = -EINVAL;
6824 	u8 lltc_map = 0;
6825 	u8 tc_map = 0;
6826 	u8 new_numtc;
6827 	u8 i;
6828 
6829 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6830 	/* Un-pack information to Program ETS HW via shared API
6831 	 * numtc, tcmap
6832 	 * LLTC map
6833 	 * ETS/NON-ETS arbiter mode
6834 	 * max exponent (credit refills)
6835 	 * Total number of ports
6836 	 * PFC priority bit-map
6837 	 * Priority Table
6838 	 * BW % per TC
6839 	 * Arbiter mode between UPs sharing same TC
6840 	 * TSA table (ETS or non-ETS)
6841 	 * EEE enabled or not
6842 	 * MFS TC table
6843 	 */
6844 
6845 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6846 
6847 	memset(&ets_data, 0, sizeof(ets_data));
6848 	for (i = 0; i < new_numtc; i++) {
6849 		tc_map |= BIT(i);
6850 		switch (new_cfg->etscfg.tsatable[i]) {
6851 		case I40E_IEEE_TSA_ETS:
6852 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6853 			ets_data.tc_bw_share_credits[i] =
6854 					new_cfg->etscfg.tcbwtable[i];
6855 			break;
6856 		case I40E_IEEE_TSA_STRICT:
6857 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6858 			lltc_map |= BIT(i);
6859 			ets_data.tc_bw_share_credits[i] =
6860 					I40E_DCB_STRICT_PRIO_CREDITS;
6861 			break;
6862 		default:
6863 			/* Invalid TSA type */
6864 			need_reconfig = false;
6865 			goto out;
6866 		}
6867 	}
6868 
6869 	old_cfg = &hw->local_dcbx_config;
6870 	/* Check if need reconfiguration */
6871 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6872 
6873 	/* If needed, enable/disable frame tagging, disable all VSIs
6874 	 * and suspend port tx
6875 	 */
6876 	if (need_reconfig) {
6877 		/* Enable DCB tagging only when more than one TC */
6878 		if (new_numtc > 1)
6879 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6880 		else
6881 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6882 
6883 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6884 		/* Reconfiguration needed quiesce all VSIs */
6885 		i40e_pf_quiesce_all_vsi(pf);
6886 		ret = i40e_suspend_port_tx(pf);
6887 		if (ret)
6888 			goto err;
6889 	}
6890 
6891 	/* Configure Port ETS Tx Scheduler */
6892 	ets_data.tc_valid_bits = tc_map;
6893 	ets_data.tc_strict_priority_flags = lltc_map;
6894 	ret = i40e_aq_config_switch_comp_ets
6895 		(hw, pf->mac_seid, &ets_data,
6896 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6897 	if (ret) {
6898 		dev_info(&pf->pdev->dev,
6899 			 "Modify Port ETS failed, err %s aq_err %s\n",
6900 			 i40e_stat_str(&pf->hw, ret),
6901 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6902 		goto out;
6903 	}
6904 
6905 	/* Configure Rx ETS HW */
6906 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6907 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6908 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6909 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6910 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6911 				   lltc_map);
6912 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6913 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6914 				     prio_type);
6915 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6916 			       new_cfg->etscfg.prioritytable);
6917 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6918 
6919 	/* Configure Rx Packet Buffers in HW */
6920 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6921 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6922 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6923 	}
6924 
6925 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6926 					 false, new_cfg->pfc.pfcenable,
6927 					 mfs_tc, &pb_cfg);
6928 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6929 
6930 	/* Update the local Rx Packet buffer config */
6931 	pf->pb_cfg = pb_cfg;
6932 
6933 	/* Inform the FW about changes to DCB configuration */
6934 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6935 	if (ret) {
6936 		dev_info(&pf->pdev->dev,
6937 			 "DCB Updated failed, err %s aq_err %s\n",
6938 			 i40e_stat_str(&pf->hw, ret),
6939 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6940 		goto out;
6941 	}
6942 
6943 	/* Update the port DCBx configuration */
6944 	*old_cfg = *new_cfg;
6945 
6946 	/* Changes in configuration update VEB/VSI */
6947 	i40e_dcb_reconfigure(pf);
6948 out:
6949 	/* Re-start the VSIs if disabled */
6950 	if (need_reconfig) {
6951 		ret = i40e_resume_port_tx(pf);
6952 
6953 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6954 		/* In case of error no point in resuming VSIs */
6955 		if (ret)
6956 			goto err;
6957 
6958 		/* Wait for the PF's queues to be disabled */
6959 		ret = i40e_pf_wait_queues_disabled(pf);
6960 		if (ret) {
6961 			/* Schedule PF reset to recover */
6962 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6963 			i40e_service_event_schedule(pf);
6964 			goto err;
6965 		} else {
6966 			i40e_pf_unquiesce_all_vsi(pf);
6967 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6968 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6969 		}
6970 		/* registers are set, lets apply */
6971 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6972 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6973 	}
6974 
6975 err:
6976 	return ret;
6977 }
6978 
6979 /**
6980  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6981  * @pf: PF being queried
6982  *
6983  * Set default DCB configuration in case DCB is to be done in SW.
6984  **/
6985 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6986 {
6987 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6988 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6989 	struct i40e_hw *hw = &pf->hw;
6990 	int err;
6991 
6992 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6993 		/* Update the local cached instance with TC0 ETS */
6994 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6995 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6996 		pf->tmp_cfg.etscfg.maxtcs = 0;
6997 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6998 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6999 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7000 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7001 		/* FW needs one App to configure HW */
7002 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7003 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7004 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7005 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7006 
7007 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7008 	}
7009 
7010 	memset(&ets_data, 0, sizeof(ets_data));
7011 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7012 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7013 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7014 
7015 	/* Enable ETS on the Physical port */
7016 	err = i40e_aq_config_switch_comp_ets
7017 		(hw, pf->mac_seid, &ets_data,
7018 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7019 	if (err) {
7020 		dev_info(&pf->pdev->dev,
7021 			 "Enable Port ETS failed, err %s aq_err %s\n",
7022 			 i40e_stat_str(&pf->hw, err),
7023 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7024 		err = -ENOENT;
7025 		goto out;
7026 	}
7027 
7028 	/* Update the local cached instance with TC0 ETS */
7029 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7030 	dcb_cfg->etscfg.cbs = 0;
7031 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7032 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7033 
7034 out:
7035 	return err;
7036 }
7037 
7038 /**
7039  * i40e_init_pf_dcb - Initialize DCB configuration
7040  * @pf: PF being configured
7041  *
7042  * Query the current DCB configuration and cache it
7043  * in the hardware structure
7044  **/
7045 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7046 {
7047 	struct i40e_hw *hw = &pf->hw;
7048 	int err;
7049 
7050 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7051 	 * Also do not enable DCBx if FW LLDP agent is disabled
7052 	 */
7053 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7054 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7055 		err = I40E_NOT_SUPPORTED;
7056 		goto out;
7057 	}
7058 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7059 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7060 		err = i40e_dcb_sw_default_config(pf);
7061 		if (err) {
7062 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7063 			goto out;
7064 		}
7065 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7066 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7067 			       DCB_CAP_DCBX_VER_IEEE;
7068 		/* at init capable but disabled */
7069 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7070 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7071 		goto out;
7072 	}
7073 	err = i40e_init_dcb(hw, true);
7074 	if (!err) {
7075 		/* Device/Function is not DCBX capable */
7076 		if ((!hw->func_caps.dcb) ||
7077 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7078 			dev_info(&pf->pdev->dev,
7079 				 "DCBX offload is not supported or is disabled for this PF.\n");
7080 		} else {
7081 			/* When status is not DISABLED then DCBX in FW */
7082 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7083 				       DCB_CAP_DCBX_VER_IEEE;
7084 
7085 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7086 			/* Enable DCB tagging only when more than one TC
7087 			 * or explicitly disable if only one TC
7088 			 */
7089 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7090 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7091 			else
7092 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7093 			dev_dbg(&pf->pdev->dev,
7094 				"DCBX offload is supported for this PF.\n");
7095 		}
7096 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7097 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7098 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7099 	} else {
7100 		dev_info(&pf->pdev->dev,
7101 			 "Query for DCB configuration failed, err %s aq_err %s\n",
7102 			 i40e_stat_str(&pf->hw, err),
7103 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7104 	}
7105 
7106 out:
7107 	return err;
7108 }
7109 #endif /* CONFIG_I40E_DCB */
7110 
7111 /**
7112  * i40e_print_link_message - print link up or down
7113  * @vsi: the VSI for which link needs a message
7114  * @isup: true of link is up, false otherwise
7115  */
7116 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7117 {
7118 	enum i40e_aq_link_speed new_speed;
7119 	struct i40e_pf *pf = vsi->back;
7120 	char *speed = "Unknown";
7121 	char *fc = "Unknown";
7122 	char *fec = "";
7123 	char *req_fec = "";
7124 	char *an = "";
7125 
7126 	if (isup)
7127 		new_speed = pf->hw.phy.link_info.link_speed;
7128 	else
7129 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7130 
7131 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7132 		return;
7133 	vsi->current_isup = isup;
7134 	vsi->current_speed = new_speed;
7135 	if (!isup) {
7136 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7137 		return;
7138 	}
7139 
7140 	/* Warn user if link speed on NPAR enabled partition is not at
7141 	 * least 10GB
7142 	 */
7143 	if (pf->hw.func_caps.npar_enable &&
7144 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7145 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7146 		netdev_warn(vsi->netdev,
7147 			    "The partition detected link speed that is less than 10Gbps\n");
7148 
7149 	switch (pf->hw.phy.link_info.link_speed) {
7150 	case I40E_LINK_SPEED_40GB:
7151 		speed = "40 G";
7152 		break;
7153 	case I40E_LINK_SPEED_20GB:
7154 		speed = "20 G";
7155 		break;
7156 	case I40E_LINK_SPEED_25GB:
7157 		speed = "25 G";
7158 		break;
7159 	case I40E_LINK_SPEED_10GB:
7160 		speed = "10 G";
7161 		break;
7162 	case I40E_LINK_SPEED_5GB:
7163 		speed = "5 G";
7164 		break;
7165 	case I40E_LINK_SPEED_2_5GB:
7166 		speed = "2.5 G";
7167 		break;
7168 	case I40E_LINK_SPEED_1GB:
7169 		speed = "1000 M";
7170 		break;
7171 	case I40E_LINK_SPEED_100MB:
7172 		speed = "100 M";
7173 		break;
7174 	default:
7175 		break;
7176 	}
7177 
7178 	switch (pf->hw.fc.current_mode) {
7179 	case I40E_FC_FULL:
7180 		fc = "RX/TX";
7181 		break;
7182 	case I40E_FC_TX_PAUSE:
7183 		fc = "TX";
7184 		break;
7185 	case I40E_FC_RX_PAUSE:
7186 		fc = "RX";
7187 		break;
7188 	default:
7189 		fc = "None";
7190 		break;
7191 	}
7192 
7193 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7194 		req_fec = "None";
7195 		fec = "None";
7196 		an = "False";
7197 
7198 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7199 			an = "True";
7200 
7201 		if (pf->hw.phy.link_info.fec_info &
7202 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7203 			fec = "CL74 FC-FEC/BASE-R";
7204 		else if (pf->hw.phy.link_info.fec_info &
7205 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7206 			fec = "CL108 RS-FEC";
7207 
7208 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7209 		 * both RS and FC are requested
7210 		 */
7211 		if (vsi->back->hw.phy.link_info.req_fec_info &
7212 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7213 			if (vsi->back->hw.phy.link_info.req_fec_info &
7214 			    I40E_AQ_REQUEST_FEC_RS)
7215 				req_fec = "CL108 RS-FEC";
7216 			else
7217 				req_fec = "CL74 FC-FEC/BASE-R";
7218 		}
7219 		netdev_info(vsi->netdev,
7220 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7221 			    speed, req_fec, fec, an, fc);
7222 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7223 		req_fec = "None";
7224 		fec = "None";
7225 		an = "False";
7226 
7227 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7228 			an = "True";
7229 
7230 		if (pf->hw.phy.link_info.fec_info &
7231 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7232 			fec = "CL74 FC-FEC/BASE-R";
7233 
7234 		if (pf->hw.phy.link_info.req_fec_info &
7235 		    I40E_AQ_REQUEST_FEC_KR)
7236 			req_fec = "CL74 FC-FEC/BASE-R";
7237 
7238 		netdev_info(vsi->netdev,
7239 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7240 			    speed, req_fec, fec, an, fc);
7241 	} else {
7242 		netdev_info(vsi->netdev,
7243 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7244 			    speed, fc);
7245 	}
7246 
7247 }
7248 
7249 /**
7250  * i40e_up_complete - Finish the last steps of bringing up a connection
7251  * @vsi: the VSI being configured
7252  **/
7253 static int i40e_up_complete(struct i40e_vsi *vsi)
7254 {
7255 	struct i40e_pf *pf = vsi->back;
7256 	int err;
7257 
7258 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7259 		i40e_vsi_configure_msix(vsi);
7260 	else
7261 		i40e_configure_msi_and_legacy(vsi);
7262 
7263 	/* start rings */
7264 	err = i40e_vsi_start_rings(vsi);
7265 	if (err)
7266 		return err;
7267 
7268 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7269 	i40e_napi_enable_all(vsi);
7270 	i40e_vsi_enable_irq(vsi);
7271 
7272 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7273 	    (vsi->netdev)) {
7274 		i40e_print_link_message(vsi, true);
7275 		netif_tx_start_all_queues(vsi->netdev);
7276 		netif_carrier_on(vsi->netdev);
7277 	}
7278 
7279 	/* replay FDIR SB filters */
7280 	if (vsi->type == I40E_VSI_FDIR) {
7281 		/* reset fd counters */
7282 		pf->fd_add_err = 0;
7283 		pf->fd_atr_cnt = 0;
7284 		i40e_fdir_filter_restore(vsi);
7285 	}
7286 
7287 	/* On the next run of the service_task, notify any clients of the new
7288 	 * opened netdev
7289 	 */
7290 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7291 	i40e_service_event_schedule(pf);
7292 
7293 	return 0;
7294 }
7295 
7296 /**
7297  * i40e_vsi_reinit_locked - Reset the VSI
7298  * @vsi: the VSI being configured
7299  *
7300  * Rebuild the ring structs after some configuration
7301  * has changed, e.g. MTU size.
7302  **/
7303 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7304 {
7305 	struct i40e_pf *pf = vsi->back;
7306 
7307 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7308 		usleep_range(1000, 2000);
7309 	i40e_down(vsi);
7310 
7311 	i40e_up(vsi);
7312 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7313 }
7314 
7315 /**
7316  * i40e_force_link_state - Force the link status
7317  * @pf: board private structure
7318  * @is_up: whether the link state should be forced up or down
7319  **/
7320 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7321 {
7322 	struct i40e_aq_get_phy_abilities_resp abilities;
7323 	struct i40e_aq_set_phy_config config = {0};
7324 	bool non_zero_phy_type = is_up;
7325 	struct i40e_hw *hw = &pf->hw;
7326 	i40e_status err;
7327 	u64 mask;
7328 	u8 speed;
7329 
7330 	/* Card might've been put in an unstable state by other drivers
7331 	 * and applications, which causes incorrect speed values being
7332 	 * set on startup. In order to clear speed registers, we call
7333 	 * get_phy_capabilities twice, once to get initial state of
7334 	 * available speeds, and once to get current PHY config.
7335 	 */
7336 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7337 					   NULL);
7338 	if (err) {
7339 		dev_err(&pf->pdev->dev,
7340 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7341 			i40e_stat_str(hw, err),
7342 			i40e_aq_str(hw, hw->aq.asq_last_status));
7343 		return err;
7344 	}
7345 	speed = abilities.link_speed;
7346 
7347 	/* Get the current phy config */
7348 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7349 					   NULL);
7350 	if (err) {
7351 		dev_err(&pf->pdev->dev,
7352 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7353 			i40e_stat_str(hw, err),
7354 			i40e_aq_str(hw, hw->aq.asq_last_status));
7355 		return err;
7356 	}
7357 
7358 	/* If link needs to go up, but was not forced to go down,
7359 	 * and its speed values are OK, no need for a flap
7360 	 * if non_zero_phy_type was set, still need to force up
7361 	 */
7362 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7363 		non_zero_phy_type = true;
7364 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7365 		return I40E_SUCCESS;
7366 
7367 	/* To force link we need to set bits for all supported PHY types,
7368 	 * but there are now more than 32, so we need to split the bitmap
7369 	 * across two fields.
7370 	 */
7371 	mask = I40E_PHY_TYPES_BITMASK;
7372 	config.phy_type =
7373 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7374 	config.phy_type_ext =
7375 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7376 	/* Copy the old settings, except of phy_type */
7377 	config.abilities = abilities.abilities;
7378 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7379 		if (is_up)
7380 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7381 		else
7382 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7383 	}
7384 	if (abilities.link_speed != 0)
7385 		config.link_speed = abilities.link_speed;
7386 	else
7387 		config.link_speed = speed;
7388 	config.eee_capability = abilities.eee_capability;
7389 	config.eeer = abilities.eeer_val;
7390 	config.low_power_ctrl = abilities.d3_lpan;
7391 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7392 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7393 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7394 
7395 	if (err) {
7396 		dev_err(&pf->pdev->dev,
7397 			"set phy config ret =  %s last_status =  %s\n",
7398 			i40e_stat_str(&pf->hw, err),
7399 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7400 		return err;
7401 	}
7402 
7403 	/* Update the link info */
7404 	err = i40e_update_link_info(hw);
7405 	if (err) {
7406 		/* Wait a little bit (on 40G cards it sometimes takes a really
7407 		 * long time for link to come back from the atomic reset)
7408 		 * and try once more
7409 		 */
7410 		msleep(1000);
7411 		i40e_update_link_info(hw);
7412 	}
7413 
7414 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7415 
7416 	return I40E_SUCCESS;
7417 }
7418 
7419 /**
7420  * i40e_up - Bring the connection back up after being down
7421  * @vsi: the VSI being configured
7422  **/
7423 int i40e_up(struct i40e_vsi *vsi)
7424 {
7425 	int err;
7426 
7427 	if (vsi->type == I40E_VSI_MAIN &&
7428 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7429 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7430 		i40e_force_link_state(vsi->back, true);
7431 
7432 	err = i40e_vsi_configure(vsi);
7433 	if (!err)
7434 		err = i40e_up_complete(vsi);
7435 
7436 	return err;
7437 }
7438 
7439 /**
7440  * i40e_down - Shutdown the connection processing
7441  * @vsi: the VSI being stopped
7442  **/
7443 void i40e_down(struct i40e_vsi *vsi)
7444 {
7445 	int i;
7446 
7447 	/* It is assumed that the caller of this function
7448 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7449 	 */
7450 	if (vsi->netdev) {
7451 		netif_carrier_off(vsi->netdev);
7452 		netif_tx_disable(vsi->netdev);
7453 	}
7454 	i40e_vsi_disable_irq(vsi);
7455 	i40e_vsi_stop_rings(vsi);
7456 	if (vsi->type == I40E_VSI_MAIN &&
7457 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7458 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7459 		i40e_force_link_state(vsi->back, false);
7460 	i40e_napi_disable_all(vsi);
7461 
7462 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7463 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7464 		if (i40e_enabled_xdp_vsi(vsi)) {
7465 			/* Make sure that in-progress ndo_xdp_xmit and
7466 			 * ndo_xsk_wakeup calls are completed.
7467 			 */
7468 			synchronize_rcu();
7469 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7470 		}
7471 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7472 	}
7473 
7474 }
7475 
7476 /**
7477  * i40e_validate_mqprio_qopt- validate queue mapping info
7478  * @vsi: the VSI being configured
7479  * @mqprio_qopt: queue parametrs
7480  **/
7481 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7482 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7483 {
7484 	u64 sum_max_rate = 0;
7485 	u64 max_rate = 0;
7486 	int i;
7487 
7488 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7489 	    mqprio_qopt->qopt.num_tc < 1 ||
7490 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7491 		return -EINVAL;
7492 	for (i = 0; ; i++) {
7493 		if (!mqprio_qopt->qopt.count[i])
7494 			return -EINVAL;
7495 		if (mqprio_qopt->min_rate[i]) {
7496 			dev_err(&vsi->back->pdev->dev,
7497 				"Invalid min tx rate (greater than 0) specified\n");
7498 			return -EINVAL;
7499 		}
7500 		max_rate = mqprio_qopt->max_rate[i];
7501 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7502 		sum_max_rate += max_rate;
7503 
7504 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7505 			break;
7506 		if (mqprio_qopt->qopt.offset[i + 1] !=
7507 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7508 			return -EINVAL;
7509 	}
7510 	if (vsi->num_queue_pairs <
7511 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7512 		dev_err(&vsi->back->pdev->dev,
7513 			"Failed to create traffic channel, insufficient number of queues.\n");
7514 		return -EINVAL;
7515 	}
7516 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7517 		dev_err(&vsi->back->pdev->dev,
7518 			"Invalid max tx rate specified\n");
7519 		return -EINVAL;
7520 	}
7521 	return 0;
7522 }
7523 
7524 /**
7525  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7526  * @vsi: the VSI being configured
7527  **/
7528 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7529 {
7530 	u16 qcount;
7531 	int i;
7532 
7533 	/* Only TC0 is enabled */
7534 	vsi->tc_config.numtc = 1;
7535 	vsi->tc_config.enabled_tc = 1;
7536 	qcount = min_t(int, vsi->alloc_queue_pairs,
7537 		       i40e_pf_get_max_q_per_tc(vsi->back));
7538 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7539 		/* For the TC that is not enabled set the offset to default
7540 		 * queue and allocate one queue for the given TC.
7541 		 */
7542 		vsi->tc_config.tc_info[i].qoffset = 0;
7543 		if (i == 0)
7544 			vsi->tc_config.tc_info[i].qcount = qcount;
7545 		else
7546 			vsi->tc_config.tc_info[i].qcount = 1;
7547 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7548 	}
7549 }
7550 
7551 /**
7552  * i40e_del_macvlan_filter
7553  * @hw: pointer to the HW structure
7554  * @seid: seid of the channel VSI
7555  * @macaddr: the mac address to apply as a filter
7556  * @aq_err: store the admin Q error
7557  *
7558  * This function deletes a mac filter on the channel VSI which serves as the
7559  * macvlan. Returns 0 on success.
7560  **/
7561 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7562 					   const u8 *macaddr, int *aq_err)
7563 {
7564 	struct i40e_aqc_remove_macvlan_element_data element;
7565 	i40e_status status;
7566 
7567 	memset(&element, 0, sizeof(element));
7568 	ether_addr_copy(element.mac_addr, macaddr);
7569 	element.vlan_tag = 0;
7570 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7571 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7572 	*aq_err = hw->aq.asq_last_status;
7573 
7574 	return status;
7575 }
7576 
7577 /**
7578  * i40e_add_macvlan_filter
7579  * @hw: pointer to the HW structure
7580  * @seid: seid of the channel VSI
7581  * @macaddr: the mac address to apply as a filter
7582  * @aq_err: store the admin Q error
7583  *
7584  * This function adds a mac filter on the channel VSI which serves as the
7585  * macvlan. Returns 0 on success.
7586  **/
7587 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7588 					   const u8 *macaddr, int *aq_err)
7589 {
7590 	struct i40e_aqc_add_macvlan_element_data element;
7591 	i40e_status status;
7592 	u16 cmd_flags = 0;
7593 
7594 	ether_addr_copy(element.mac_addr, macaddr);
7595 	element.vlan_tag = 0;
7596 	element.queue_number = 0;
7597 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7598 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7599 	element.flags = cpu_to_le16(cmd_flags);
7600 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7601 	*aq_err = hw->aq.asq_last_status;
7602 
7603 	return status;
7604 }
7605 
7606 /**
7607  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7608  * @vsi: the VSI we want to access
7609  * @ch: the channel we want to access
7610  */
7611 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7612 {
7613 	struct i40e_ring *tx_ring, *rx_ring;
7614 	u16 pf_q;
7615 	int i;
7616 
7617 	for (i = 0; i < ch->num_queue_pairs; i++) {
7618 		pf_q = ch->base_queue + i;
7619 		tx_ring = vsi->tx_rings[pf_q];
7620 		tx_ring->ch = NULL;
7621 		rx_ring = vsi->rx_rings[pf_q];
7622 		rx_ring->ch = NULL;
7623 	}
7624 }
7625 
7626 /**
7627  * i40e_free_macvlan_channels
7628  * @vsi: the VSI we want to access
7629  *
7630  * This function frees the Qs of the channel VSI from
7631  * the stack and also deletes the channel VSIs which
7632  * serve as macvlans.
7633  */
7634 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7635 {
7636 	struct i40e_channel *ch, *ch_tmp;
7637 	int ret;
7638 
7639 	if (list_empty(&vsi->macvlan_list))
7640 		return;
7641 
7642 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7643 		struct i40e_vsi *parent_vsi;
7644 
7645 		if (i40e_is_channel_macvlan(ch)) {
7646 			i40e_reset_ch_rings(vsi, ch);
7647 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7648 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7649 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7650 			kfree(ch->fwd);
7651 			ch->fwd = NULL;
7652 		}
7653 
7654 		list_del(&ch->list);
7655 		parent_vsi = ch->parent_vsi;
7656 		if (!parent_vsi || !ch->initialized) {
7657 			kfree(ch);
7658 			continue;
7659 		}
7660 
7661 		/* remove the VSI */
7662 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7663 					     NULL);
7664 		if (ret)
7665 			dev_err(&vsi->back->pdev->dev,
7666 				"unable to remove channel (%d) for parent VSI(%d)\n",
7667 				ch->seid, parent_vsi->seid);
7668 		kfree(ch);
7669 	}
7670 	vsi->macvlan_cnt = 0;
7671 }
7672 
7673 /**
7674  * i40e_fwd_ring_up - bring the macvlan device up
7675  * @vsi: the VSI we want to access
7676  * @vdev: macvlan netdevice
7677  * @fwd: the private fwd structure
7678  */
7679 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7680 			    struct i40e_fwd_adapter *fwd)
7681 {
7682 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7683 	int ret = 0, num_tc = 1,  i, aq_err;
7684 	struct i40e_pf *pf = vsi->back;
7685 	struct i40e_hw *hw = &pf->hw;
7686 
7687 	/* Go through the list and find an available channel */
7688 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7689 		if (!i40e_is_channel_macvlan(iter)) {
7690 			iter->fwd = fwd;
7691 			/* record configuration for macvlan interface in vdev */
7692 			for (i = 0; i < num_tc; i++)
7693 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7694 							     i,
7695 							     iter->num_queue_pairs,
7696 							     iter->base_queue);
7697 			for (i = 0; i < iter->num_queue_pairs; i++) {
7698 				struct i40e_ring *tx_ring, *rx_ring;
7699 				u16 pf_q;
7700 
7701 				pf_q = iter->base_queue + i;
7702 
7703 				/* Get to TX ring ptr */
7704 				tx_ring = vsi->tx_rings[pf_q];
7705 				tx_ring->ch = iter;
7706 
7707 				/* Get the RX ring ptr */
7708 				rx_ring = vsi->rx_rings[pf_q];
7709 				rx_ring->ch = iter;
7710 			}
7711 			ch = iter;
7712 			break;
7713 		}
7714 	}
7715 
7716 	if (!ch)
7717 		return -EINVAL;
7718 
7719 	/* Guarantee all rings are updated before we update the
7720 	 * MAC address filter.
7721 	 */
7722 	wmb();
7723 
7724 	/* Add a mac filter */
7725 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7726 	if (ret) {
7727 		/* if we cannot add the MAC rule then disable the offload */
7728 		macvlan_release_l2fw_offload(vdev);
7729 		for (i = 0; i < ch->num_queue_pairs; i++) {
7730 			struct i40e_ring *rx_ring;
7731 			u16 pf_q;
7732 
7733 			pf_q = ch->base_queue + i;
7734 			rx_ring = vsi->rx_rings[pf_q];
7735 			rx_ring->netdev = NULL;
7736 		}
7737 		dev_info(&pf->pdev->dev,
7738 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7739 			  i40e_stat_str(hw, ret),
7740 			  i40e_aq_str(hw, aq_err));
7741 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7742 	}
7743 
7744 	return ret;
7745 }
7746 
7747 /**
7748  * i40e_setup_macvlans - create the channels which will be macvlans
7749  * @vsi: the VSI we want to access
7750  * @macvlan_cnt: no. of macvlans to be setup
7751  * @qcnt: no. of Qs per macvlan
7752  * @vdev: macvlan netdevice
7753  */
7754 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7755 			       struct net_device *vdev)
7756 {
7757 	struct i40e_pf *pf = vsi->back;
7758 	struct i40e_hw *hw = &pf->hw;
7759 	struct i40e_vsi_context ctxt;
7760 	u16 sections, qmap, num_qps;
7761 	struct i40e_channel *ch;
7762 	int i, pow, ret = 0;
7763 	u8 offset = 0;
7764 
7765 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7766 		return -EINVAL;
7767 
7768 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7769 
7770 	/* find the next higher power-of-2 of num queue pairs */
7771 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7772 
7773 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7774 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7775 
7776 	/* Setup context bits for the main VSI */
7777 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7778 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7779 	memset(&ctxt, 0, sizeof(ctxt));
7780 	ctxt.seid = vsi->seid;
7781 	ctxt.pf_num = vsi->back->hw.pf_id;
7782 	ctxt.vf_num = 0;
7783 	ctxt.uplink_seid = vsi->uplink_seid;
7784 	ctxt.info = vsi->info;
7785 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7786 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7787 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7788 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7789 
7790 	/* Reconfigure RSS for main VSI with new max queue count */
7791 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7792 	ret = i40e_vsi_config_rss(vsi);
7793 	if (ret) {
7794 		dev_info(&pf->pdev->dev,
7795 			 "Failed to reconfig RSS for num_queues (%u)\n",
7796 			 vsi->rss_size);
7797 		return ret;
7798 	}
7799 	vsi->reconfig_rss = true;
7800 	dev_dbg(&vsi->back->pdev->dev,
7801 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7802 	vsi->next_base_queue = num_qps;
7803 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7804 
7805 	/* Update the VSI after updating the VSI queue-mapping
7806 	 * information
7807 	 */
7808 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7809 	if (ret) {
7810 		dev_info(&pf->pdev->dev,
7811 			 "Update vsi tc config failed, err %s aq_err %s\n",
7812 			 i40e_stat_str(hw, ret),
7813 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7814 		return ret;
7815 	}
7816 	/* update the local VSI info with updated queue map */
7817 	i40e_vsi_update_queue_map(vsi, &ctxt);
7818 	vsi->info.valid_sections = 0;
7819 
7820 	/* Create channels for macvlans */
7821 	INIT_LIST_HEAD(&vsi->macvlan_list);
7822 	for (i = 0; i < macvlan_cnt; i++) {
7823 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7824 		if (!ch) {
7825 			ret = -ENOMEM;
7826 			goto err_free;
7827 		}
7828 		INIT_LIST_HEAD(&ch->list);
7829 		ch->num_queue_pairs = qcnt;
7830 		if (!i40e_setup_channel(pf, vsi, ch)) {
7831 			ret = -EINVAL;
7832 			kfree(ch);
7833 			goto err_free;
7834 		}
7835 		ch->parent_vsi = vsi;
7836 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7837 		vsi->macvlan_cnt++;
7838 		list_add_tail(&ch->list, &vsi->macvlan_list);
7839 	}
7840 
7841 	return ret;
7842 
7843 err_free:
7844 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7845 	i40e_free_macvlan_channels(vsi);
7846 
7847 	return ret;
7848 }
7849 
7850 /**
7851  * i40e_fwd_add - configure macvlans
7852  * @netdev: net device to configure
7853  * @vdev: macvlan netdevice
7854  **/
7855 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7856 {
7857 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7858 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7859 	struct i40e_vsi *vsi = np->vsi;
7860 	struct i40e_pf *pf = vsi->back;
7861 	struct i40e_fwd_adapter *fwd;
7862 	int avail_macvlan, ret;
7863 
7864 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7865 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7866 		return ERR_PTR(-EINVAL);
7867 	}
7868 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7869 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7870 		return ERR_PTR(-EINVAL);
7871 	}
7872 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7873 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7874 		return ERR_PTR(-EINVAL);
7875 	}
7876 
7877 	/* The macvlan device has to be a single Q device so that the
7878 	 * tc_to_txq field can be reused to pick the tx queue.
7879 	 */
7880 	if (netif_is_multiqueue(vdev))
7881 		return ERR_PTR(-ERANGE);
7882 
7883 	if (!vsi->macvlan_cnt) {
7884 		/* reserve bit 0 for the pf device */
7885 		set_bit(0, vsi->fwd_bitmask);
7886 
7887 		/* Try to reserve as many queues as possible for macvlans. First
7888 		 * reserve 3/4th of max vectors, then half, then quarter and
7889 		 * calculate Qs per macvlan as you go
7890 		 */
7891 		vectors = pf->num_lan_msix;
7892 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7893 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7894 			q_per_macvlan = 4;
7895 			macvlan_cnt = (vectors - 32) / 4;
7896 		} else if (vectors <= 64 && vectors > 32) {
7897 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7898 			q_per_macvlan = 2;
7899 			macvlan_cnt = (vectors - 16) / 2;
7900 		} else if (vectors <= 32 && vectors > 16) {
7901 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7902 			q_per_macvlan = 1;
7903 			macvlan_cnt = vectors - 16;
7904 		} else if (vectors <= 16 && vectors > 8) {
7905 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7906 			q_per_macvlan = 1;
7907 			macvlan_cnt = vectors - 8;
7908 		} else {
7909 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7910 			q_per_macvlan = 1;
7911 			macvlan_cnt = vectors - 1;
7912 		}
7913 
7914 		if (macvlan_cnt == 0)
7915 			return ERR_PTR(-EBUSY);
7916 
7917 		/* Quiesce VSI queues */
7918 		i40e_quiesce_vsi(vsi);
7919 
7920 		/* sets up the macvlans but does not "enable" them */
7921 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7922 					  vdev);
7923 		if (ret)
7924 			return ERR_PTR(ret);
7925 
7926 		/* Unquiesce VSI */
7927 		i40e_unquiesce_vsi(vsi);
7928 	}
7929 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7930 					    vsi->macvlan_cnt);
7931 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7932 		return ERR_PTR(-EBUSY);
7933 
7934 	/* create the fwd struct */
7935 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7936 	if (!fwd)
7937 		return ERR_PTR(-ENOMEM);
7938 
7939 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7940 	fwd->bit_no = avail_macvlan;
7941 	netdev_set_sb_channel(vdev, avail_macvlan);
7942 	fwd->netdev = vdev;
7943 
7944 	if (!netif_running(netdev))
7945 		return fwd;
7946 
7947 	/* Set fwd ring up */
7948 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7949 	if (ret) {
7950 		/* unbind the queues and drop the subordinate channel config */
7951 		netdev_unbind_sb_channel(netdev, vdev);
7952 		netdev_set_sb_channel(vdev, 0);
7953 
7954 		kfree(fwd);
7955 		return ERR_PTR(-EINVAL);
7956 	}
7957 
7958 	return fwd;
7959 }
7960 
7961 /**
7962  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7963  * @vsi: the VSI we want to access
7964  */
7965 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7966 {
7967 	struct i40e_channel *ch, *ch_tmp;
7968 	struct i40e_pf *pf = vsi->back;
7969 	struct i40e_hw *hw = &pf->hw;
7970 	int aq_err, ret = 0;
7971 
7972 	if (list_empty(&vsi->macvlan_list))
7973 		return;
7974 
7975 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7976 		if (i40e_is_channel_macvlan(ch)) {
7977 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7978 						      i40e_channel_mac(ch),
7979 						      &aq_err);
7980 			if (!ret) {
7981 				/* Reset queue contexts */
7982 				i40e_reset_ch_rings(vsi, ch);
7983 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7984 				netdev_unbind_sb_channel(vsi->netdev,
7985 							 ch->fwd->netdev);
7986 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7987 				kfree(ch->fwd);
7988 				ch->fwd = NULL;
7989 			}
7990 		}
7991 	}
7992 }
7993 
7994 /**
7995  * i40e_fwd_del - delete macvlan interfaces
7996  * @netdev: net device to configure
7997  * @vdev: macvlan netdevice
7998  */
7999 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8000 {
8001 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8002 	struct i40e_fwd_adapter *fwd = vdev;
8003 	struct i40e_channel *ch, *ch_tmp;
8004 	struct i40e_vsi *vsi = np->vsi;
8005 	struct i40e_pf *pf = vsi->back;
8006 	struct i40e_hw *hw = &pf->hw;
8007 	int aq_err, ret = 0;
8008 
8009 	/* Find the channel associated with the macvlan and del mac filter */
8010 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8011 		if (i40e_is_channel_macvlan(ch) &&
8012 		    ether_addr_equal(i40e_channel_mac(ch),
8013 				     fwd->netdev->dev_addr)) {
8014 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8015 						      i40e_channel_mac(ch),
8016 						      &aq_err);
8017 			if (!ret) {
8018 				/* Reset queue contexts */
8019 				i40e_reset_ch_rings(vsi, ch);
8020 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8021 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8022 				netdev_set_sb_channel(fwd->netdev, 0);
8023 				kfree(ch->fwd);
8024 				ch->fwd = NULL;
8025 			} else {
8026 				dev_info(&pf->pdev->dev,
8027 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8028 					  i40e_stat_str(hw, ret),
8029 					  i40e_aq_str(hw, aq_err));
8030 			}
8031 			break;
8032 		}
8033 	}
8034 }
8035 
8036 /**
8037  * i40e_setup_tc - configure multiple traffic classes
8038  * @netdev: net device to configure
8039  * @type_data: tc offload data
8040  **/
8041 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8042 {
8043 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8044 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8045 	struct i40e_vsi *vsi = np->vsi;
8046 	struct i40e_pf *pf = vsi->back;
8047 	u8 enabled_tc = 0, num_tc, hw;
8048 	bool need_reset = false;
8049 	int old_queue_pairs;
8050 	int ret = -EINVAL;
8051 	u16 mode;
8052 	int i;
8053 
8054 	old_queue_pairs = vsi->num_queue_pairs;
8055 	num_tc = mqprio_qopt->qopt.num_tc;
8056 	hw = mqprio_qopt->qopt.hw;
8057 	mode = mqprio_qopt->mode;
8058 	if (!hw) {
8059 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8060 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8061 		goto config_tc;
8062 	}
8063 
8064 	/* Check if MFP enabled */
8065 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8066 		netdev_info(netdev,
8067 			    "Configuring TC not supported in MFP mode\n");
8068 		return ret;
8069 	}
8070 	switch (mode) {
8071 	case TC_MQPRIO_MODE_DCB:
8072 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8073 
8074 		/* Check if DCB enabled to continue */
8075 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8076 			netdev_info(netdev,
8077 				    "DCB is not enabled for adapter\n");
8078 			return ret;
8079 		}
8080 
8081 		/* Check whether tc count is within enabled limit */
8082 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8083 			netdev_info(netdev,
8084 				    "TC count greater than enabled on link for adapter\n");
8085 			return ret;
8086 		}
8087 		break;
8088 	case TC_MQPRIO_MODE_CHANNEL:
8089 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8090 			netdev_info(netdev,
8091 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8092 			return ret;
8093 		}
8094 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8095 			return ret;
8096 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8097 		if (ret)
8098 			return ret;
8099 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8100 		       sizeof(*mqprio_qopt));
8101 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8102 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8103 		break;
8104 	default:
8105 		return -EINVAL;
8106 	}
8107 
8108 config_tc:
8109 	/* Generate TC map for number of tc requested */
8110 	for (i = 0; i < num_tc; i++)
8111 		enabled_tc |= BIT(i);
8112 
8113 	/* Requesting same TC configuration as already enabled */
8114 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8115 	    mode != TC_MQPRIO_MODE_CHANNEL)
8116 		return 0;
8117 
8118 	/* Quiesce VSI queues */
8119 	i40e_quiesce_vsi(vsi);
8120 
8121 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
8122 		i40e_remove_queue_channels(vsi);
8123 
8124 	/* Configure VSI for enabled TCs */
8125 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8126 	if (ret) {
8127 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8128 			    vsi->seid);
8129 		need_reset = true;
8130 		goto exit;
8131 	} else if (enabled_tc &&
8132 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8133 		netdev_info(netdev,
8134 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8135 			    vsi->tc_config.tc_info[0].qcount);
8136 		ret = -EINVAL;
8137 		need_reset = true;
8138 		goto exit;
8139 	}
8140 
8141 	dev_info(&vsi->back->pdev->dev,
8142 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8143 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8144 
8145 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
8146 		if (vsi->mqprio_qopt.max_rate[0]) {
8147 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8148 
8149 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8150 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8151 			if (!ret) {
8152 				u64 credits = max_tx_rate;
8153 
8154 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8155 				dev_dbg(&vsi->back->pdev->dev,
8156 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8157 					max_tx_rate,
8158 					credits,
8159 					vsi->seid);
8160 			} else {
8161 				need_reset = true;
8162 				goto exit;
8163 			}
8164 		}
8165 		ret = i40e_configure_queue_channels(vsi);
8166 		if (ret) {
8167 			vsi->num_queue_pairs = old_queue_pairs;
8168 			netdev_info(netdev,
8169 				    "Failed configuring queue channels\n");
8170 			need_reset = true;
8171 			goto exit;
8172 		}
8173 	}
8174 
8175 exit:
8176 	/* Reset the configuration data to defaults, only TC0 is enabled */
8177 	if (need_reset) {
8178 		i40e_vsi_set_default_tc_config(vsi);
8179 		need_reset = false;
8180 	}
8181 
8182 	/* Unquiesce VSI */
8183 	i40e_unquiesce_vsi(vsi);
8184 	return ret;
8185 }
8186 
8187 /**
8188  * i40e_set_cld_element - sets cloud filter element data
8189  * @filter: cloud filter rule
8190  * @cld: ptr to cloud filter element data
8191  *
8192  * This is helper function to copy data into cloud filter element
8193  **/
8194 static inline void
8195 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8196 		     struct i40e_aqc_cloud_filters_element_data *cld)
8197 {
8198 	u32 ipa;
8199 	int i;
8200 
8201 	memset(cld, 0, sizeof(*cld));
8202 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8203 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8204 
8205 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8206 		return;
8207 
8208 	if (filter->n_proto == ETH_P_IPV6) {
8209 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8210 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8211 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8212 
8213 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8214 		}
8215 	} else {
8216 		ipa = be32_to_cpu(filter->dst_ipv4);
8217 
8218 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8219 	}
8220 
8221 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8222 
8223 	/* tenant_id is not supported by FW now, once the support is enabled
8224 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8225 	 */
8226 	if (filter->tenant_id)
8227 		return;
8228 }
8229 
8230 /**
8231  * i40e_add_del_cloud_filter - Add/del cloud filter
8232  * @vsi: pointer to VSI
8233  * @filter: cloud filter rule
8234  * @add: if true, add, if false, delete
8235  *
8236  * Add or delete a cloud filter for a specific flow spec.
8237  * Returns 0 if the filter were successfully added.
8238  **/
8239 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8240 			      struct i40e_cloud_filter *filter, bool add)
8241 {
8242 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8243 	struct i40e_pf *pf = vsi->back;
8244 	int ret;
8245 	static const u16 flag_table[128] = {
8246 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8247 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8248 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8249 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8250 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8251 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8252 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8253 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8254 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8255 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8256 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8257 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8258 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8259 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8260 	};
8261 
8262 	if (filter->flags >= ARRAY_SIZE(flag_table))
8263 		return I40E_ERR_CONFIG;
8264 
8265 	memset(&cld_filter, 0, sizeof(cld_filter));
8266 
8267 	/* copy element needed to add cloud filter from filter */
8268 	i40e_set_cld_element(filter, &cld_filter);
8269 
8270 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8271 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8272 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8273 
8274 	if (filter->n_proto == ETH_P_IPV6)
8275 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8276 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8277 	else
8278 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8279 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8280 
8281 	if (add)
8282 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8283 						&cld_filter, 1);
8284 	else
8285 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8286 						&cld_filter, 1);
8287 	if (ret)
8288 		dev_dbg(&pf->pdev->dev,
8289 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8290 			add ? "add" : "delete", filter->dst_port, ret,
8291 			pf->hw.aq.asq_last_status);
8292 	else
8293 		dev_info(&pf->pdev->dev,
8294 			 "%s cloud filter for VSI: %d\n",
8295 			 add ? "Added" : "Deleted", filter->seid);
8296 	return ret;
8297 }
8298 
8299 /**
8300  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8301  * @vsi: pointer to VSI
8302  * @filter: cloud filter rule
8303  * @add: if true, add, if false, delete
8304  *
8305  * Add or delete a cloud filter for a specific flow spec using big buffer.
8306  * Returns 0 if the filter were successfully added.
8307  **/
8308 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8309 				      struct i40e_cloud_filter *filter,
8310 				      bool add)
8311 {
8312 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8313 	struct i40e_pf *pf = vsi->back;
8314 	int ret;
8315 
8316 	/* Both (src/dst) valid mac_addr are not supported */
8317 	if ((is_valid_ether_addr(filter->dst_mac) &&
8318 	     is_valid_ether_addr(filter->src_mac)) ||
8319 	    (is_multicast_ether_addr(filter->dst_mac) &&
8320 	     is_multicast_ether_addr(filter->src_mac)))
8321 		return -EOPNOTSUPP;
8322 
8323 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8324 	 * ports are not supported via big buffer now.
8325 	 */
8326 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8327 		return -EOPNOTSUPP;
8328 
8329 	/* adding filter using src_port/src_ip is not supported at this stage */
8330 	if (filter->src_port ||
8331 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8332 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8333 		return -EOPNOTSUPP;
8334 
8335 	memset(&cld_filter, 0, sizeof(cld_filter));
8336 
8337 	/* copy element needed to add cloud filter from filter */
8338 	i40e_set_cld_element(filter, &cld_filter.element);
8339 
8340 	if (is_valid_ether_addr(filter->dst_mac) ||
8341 	    is_valid_ether_addr(filter->src_mac) ||
8342 	    is_multicast_ether_addr(filter->dst_mac) ||
8343 	    is_multicast_ether_addr(filter->src_mac)) {
8344 		/* MAC + IP : unsupported mode */
8345 		if (filter->dst_ipv4)
8346 			return -EOPNOTSUPP;
8347 
8348 		/* since we validated that L4 port must be valid before
8349 		 * we get here, start with respective "flags" value
8350 		 * and update if vlan is present or not
8351 		 */
8352 		cld_filter.element.flags =
8353 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8354 
8355 		if (filter->vlan_id) {
8356 			cld_filter.element.flags =
8357 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8358 		}
8359 
8360 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8361 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8362 		cld_filter.element.flags =
8363 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8364 		if (filter->n_proto == ETH_P_IPV6)
8365 			cld_filter.element.flags |=
8366 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8367 		else
8368 			cld_filter.element.flags |=
8369 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8370 	} else {
8371 		dev_err(&pf->pdev->dev,
8372 			"either mac or ip has to be valid for cloud filter\n");
8373 		return -EINVAL;
8374 	}
8375 
8376 	/* Now copy L4 port in Byte 6..7 in general fields */
8377 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8378 						be16_to_cpu(filter->dst_port);
8379 
8380 	if (add) {
8381 		/* Validate current device switch mode, change if necessary */
8382 		ret = i40e_validate_and_set_switch_mode(vsi);
8383 		if (ret) {
8384 			dev_err(&pf->pdev->dev,
8385 				"failed to set switch mode, ret %d\n",
8386 				ret);
8387 			return ret;
8388 		}
8389 
8390 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8391 						   &cld_filter, 1);
8392 	} else {
8393 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8394 						   &cld_filter, 1);
8395 	}
8396 
8397 	if (ret)
8398 		dev_dbg(&pf->pdev->dev,
8399 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8400 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8401 	else
8402 		dev_info(&pf->pdev->dev,
8403 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8404 			 add ? "add" : "delete", filter->seid,
8405 			 ntohs(filter->dst_port));
8406 	return ret;
8407 }
8408 
8409 /**
8410  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8411  * @vsi: Pointer to VSI
8412  * @f: Pointer to struct flow_cls_offload
8413  * @filter: Pointer to cloud filter structure
8414  *
8415  **/
8416 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8417 				 struct flow_cls_offload *f,
8418 				 struct i40e_cloud_filter *filter)
8419 {
8420 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8421 	struct flow_dissector *dissector = rule->match.dissector;
8422 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8423 	struct i40e_pf *pf = vsi->back;
8424 	u8 field_flags = 0;
8425 
8426 	if (dissector->used_keys &
8427 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8428 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8429 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8430 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8431 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8432 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8433 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8434 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8435 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8436 			dissector->used_keys);
8437 		return -EOPNOTSUPP;
8438 	}
8439 
8440 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8441 		struct flow_match_enc_keyid match;
8442 
8443 		flow_rule_match_enc_keyid(rule, &match);
8444 		if (match.mask->keyid != 0)
8445 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8446 
8447 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8448 	}
8449 
8450 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8451 		struct flow_match_basic match;
8452 
8453 		flow_rule_match_basic(rule, &match);
8454 		n_proto_key = ntohs(match.key->n_proto);
8455 		n_proto_mask = ntohs(match.mask->n_proto);
8456 
8457 		if (n_proto_key == ETH_P_ALL) {
8458 			n_proto_key = 0;
8459 			n_proto_mask = 0;
8460 		}
8461 		filter->n_proto = n_proto_key & n_proto_mask;
8462 		filter->ip_proto = match.key->ip_proto;
8463 	}
8464 
8465 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8466 		struct flow_match_eth_addrs match;
8467 
8468 		flow_rule_match_eth_addrs(rule, &match);
8469 
8470 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8471 		if (!is_zero_ether_addr(match.mask->dst)) {
8472 			if (is_broadcast_ether_addr(match.mask->dst)) {
8473 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8474 			} else {
8475 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8476 					match.mask->dst);
8477 				return I40E_ERR_CONFIG;
8478 			}
8479 		}
8480 
8481 		if (!is_zero_ether_addr(match.mask->src)) {
8482 			if (is_broadcast_ether_addr(match.mask->src)) {
8483 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8484 			} else {
8485 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8486 					match.mask->src);
8487 				return I40E_ERR_CONFIG;
8488 			}
8489 		}
8490 		ether_addr_copy(filter->dst_mac, match.key->dst);
8491 		ether_addr_copy(filter->src_mac, match.key->src);
8492 	}
8493 
8494 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8495 		struct flow_match_vlan match;
8496 
8497 		flow_rule_match_vlan(rule, &match);
8498 		if (match.mask->vlan_id) {
8499 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8500 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8501 
8502 			} else {
8503 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8504 					match.mask->vlan_id);
8505 				return I40E_ERR_CONFIG;
8506 			}
8507 		}
8508 
8509 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8510 	}
8511 
8512 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8513 		struct flow_match_control match;
8514 
8515 		flow_rule_match_control(rule, &match);
8516 		addr_type = match.key->addr_type;
8517 	}
8518 
8519 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8520 		struct flow_match_ipv4_addrs match;
8521 
8522 		flow_rule_match_ipv4_addrs(rule, &match);
8523 		if (match.mask->dst) {
8524 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8525 				field_flags |= I40E_CLOUD_FIELD_IIP;
8526 			} else {
8527 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8528 					&match.mask->dst);
8529 				return I40E_ERR_CONFIG;
8530 			}
8531 		}
8532 
8533 		if (match.mask->src) {
8534 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8535 				field_flags |= I40E_CLOUD_FIELD_IIP;
8536 			} else {
8537 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8538 					&match.mask->src);
8539 				return I40E_ERR_CONFIG;
8540 			}
8541 		}
8542 
8543 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8544 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8545 			return I40E_ERR_CONFIG;
8546 		}
8547 		filter->dst_ipv4 = match.key->dst;
8548 		filter->src_ipv4 = match.key->src;
8549 	}
8550 
8551 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8552 		struct flow_match_ipv6_addrs match;
8553 
8554 		flow_rule_match_ipv6_addrs(rule, &match);
8555 
8556 		/* src and dest IPV6 address should not be LOOPBACK
8557 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8558 		 */
8559 		if (ipv6_addr_loopback(&match.key->dst) ||
8560 		    ipv6_addr_loopback(&match.key->src)) {
8561 			dev_err(&pf->pdev->dev,
8562 				"Bad ipv6, addr is LOOPBACK\n");
8563 			return I40E_ERR_CONFIG;
8564 		}
8565 		if (!ipv6_addr_any(&match.mask->dst) ||
8566 		    !ipv6_addr_any(&match.mask->src))
8567 			field_flags |= I40E_CLOUD_FIELD_IIP;
8568 
8569 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8570 		       sizeof(filter->src_ipv6));
8571 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8572 		       sizeof(filter->dst_ipv6));
8573 	}
8574 
8575 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8576 		struct flow_match_ports match;
8577 
8578 		flow_rule_match_ports(rule, &match);
8579 		if (match.mask->src) {
8580 			if (match.mask->src == cpu_to_be16(0xffff)) {
8581 				field_flags |= I40E_CLOUD_FIELD_IIP;
8582 			} else {
8583 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8584 					be16_to_cpu(match.mask->src));
8585 				return I40E_ERR_CONFIG;
8586 			}
8587 		}
8588 
8589 		if (match.mask->dst) {
8590 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8591 				field_flags |= I40E_CLOUD_FIELD_IIP;
8592 			} else {
8593 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8594 					be16_to_cpu(match.mask->dst));
8595 				return I40E_ERR_CONFIG;
8596 			}
8597 		}
8598 
8599 		filter->dst_port = match.key->dst;
8600 		filter->src_port = match.key->src;
8601 
8602 		switch (filter->ip_proto) {
8603 		case IPPROTO_TCP:
8604 		case IPPROTO_UDP:
8605 			break;
8606 		default:
8607 			dev_err(&pf->pdev->dev,
8608 				"Only UDP and TCP transport are supported\n");
8609 			return -EINVAL;
8610 		}
8611 	}
8612 	filter->flags = field_flags;
8613 	return 0;
8614 }
8615 
8616 /**
8617  * i40e_handle_tclass: Forward to a traffic class on the device
8618  * @vsi: Pointer to VSI
8619  * @tc: traffic class index on the device
8620  * @filter: Pointer to cloud filter structure
8621  *
8622  **/
8623 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8624 			      struct i40e_cloud_filter *filter)
8625 {
8626 	struct i40e_channel *ch, *ch_tmp;
8627 
8628 	/* direct to a traffic class on the same device */
8629 	if (tc == 0) {
8630 		filter->seid = vsi->seid;
8631 		return 0;
8632 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8633 		if (!filter->dst_port) {
8634 			dev_err(&vsi->back->pdev->dev,
8635 				"Specify destination port to direct to traffic class that is not default\n");
8636 			return -EINVAL;
8637 		}
8638 		if (list_empty(&vsi->ch_list))
8639 			return -EINVAL;
8640 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8641 					 list) {
8642 			if (ch->seid == vsi->tc_seid_map[tc])
8643 				filter->seid = ch->seid;
8644 		}
8645 		return 0;
8646 	}
8647 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8648 	return -EINVAL;
8649 }
8650 
8651 /**
8652  * i40e_configure_clsflower - Configure tc flower filters
8653  * @vsi: Pointer to VSI
8654  * @cls_flower: Pointer to struct flow_cls_offload
8655  *
8656  **/
8657 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8658 				    struct flow_cls_offload *cls_flower)
8659 {
8660 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8661 	struct i40e_cloud_filter *filter = NULL;
8662 	struct i40e_pf *pf = vsi->back;
8663 	int err = 0;
8664 
8665 	if (tc < 0) {
8666 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8667 		return -EOPNOTSUPP;
8668 	}
8669 
8670 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8671 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8672 		return -EBUSY;
8673 
8674 	if (pf->fdir_pf_active_filters ||
8675 	    (!hlist_empty(&pf->fdir_filter_list))) {
8676 		dev_err(&vsi->back->pdev->dev,
8677 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8678 		return -EINVAL;
8679 	}
8680 
8681 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8682 		dev_err(&vsi->back->pdev->dev,
8683 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8684 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8685 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8686 	}
8687 
8688 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8689 	if (!filter)
8690 		return -ENOMEM;
8691 
8692 	filter->cookie = cls_flower->cookie;
8693 
8694 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8695 	if (err < 0)
8696 		goto err;
8697 
8698 	err = i40e_handle_tclass(vsi, tc, filter);
8699 	if (err < 0)
8700 		goto err;
8701 
8702 	/* Add cloud filter */
8703 	if (filter->dst_port)
8704 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8705 	else
8706 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8707 
8708 	if (err) {
8709 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8710 			err);
8711 		goto err;
8712 	}
8713 
8714 	/* add filter to the ordered list */
8715 	INIT_HLIST_NODE(&filter->cloud_node);
8716 
8717 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8718 
8719 	pf->num_cloud_filters++;
8720 
8721 	return err;
8722 err:
8723 	kfree(filter);
8724 	return err;
8725 }
8726 
8727 /**
8728  * i40e_find_cloud_filter - Find the could filter in the list
8729  * @vsi: Pointer to VSI
8730  * @cookie: filter specific cookie
8731  *
8732  **/
8733 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8734 							unsigned long *cookie)
8735 {
8736 	struct i40e_cloud_filter *filter = NULL;
8737 	struct hlist_node *node2;
8738 
8739 	hlist_for_each_entry_safe(filter, node2,
8740 				  &vsi->back->cloud_filter_list, cloud_node)
8741 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8742 			return filter;
8743 	return NULL;
8744 }
8745 
8746 /**
8747  * i40e_delete_clsflower - Remove tc flower filters
8748  * @vsi: Pointer to VSI
8749  * @cls_flower: Pointer to struct flow_cls_offload
8750  *
8751  **/
8752 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8753 				 struct flow_cls_offload *cls_flower)
8754 {
8755 	struct i40e_cloud_filter *filter = NULL;
8756 	struct i40e_pf *pf = vsi->back;
8757 	int err = 0;
8758 
8759 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8760 
8761 	if (!filter)
8762 		return -EINVAL;
8763 
8764 	hash_del(&filter->cloud_node);
8765 
8766 	if (filter->dst_port)
8767 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8768 	else
8769 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8770 
8771 	kfree(filter);
8772 	if (err) {
8773 		dev_err(&pf->pdev->dev,
8774 			"Failed to delete cloud filter, err %s\n",
8775 			i40e_stat_str(&pf->hw, err));
8776 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8777 	}
8778 
8779 	pf->num_cloud_filters--;
8780 	if (!pf->num_cloud_filters)
8781 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8782 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8783 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8784 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8785 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8786 		}
8787 	return 0;
8788 }
8789 
8790 /**
8791  * i40e_setup_tc_cls_flower - flower classifier offloads
8792  * @np: net device to configure
8793  * @cls_flower: offload data
8794  **/
8795 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8796 				    struct flow_cls_offload *cls_flower)
8797 {
8798 	struct i40e_vsi *vsi = np->vsi;
8799 
8800 	switch (cls_flower->command) {
8801 	case FLOW_CLS_REPLACE:
8802 		return i40e_configure_clsflower(vsi, cls_flower);
8803 	case FLOW_CLS_DESTROY:
8804 		return i40e_delete_clsflower(vsi, cls_flower);
8805 	case FLOW_CLS_STATS:
8806 		return -EOPNOTSUPP;
8807 	default:
8808 		return -EOPNOTSUPP;
8809 	}
8810 }
8811 
8812 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8813 				  void *cb_priv)
8814 {
8815 	struct i40e_netdev_priv *np = cb_priv;
8816 
8817 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8818 		return -EOPNOTSUPP;
8819 
8820 	switch (type) {
8821 	case TC_SETUP_CLSFLOWER:
8822 		return i40e_setup_tc_cls_flower(np, type_data);
8823 
8824 	default:
8825 		return -EOPNOTSUPP;
8826 	}
8827 }
8828 
8829 static LIST_HEAD(i40e_block_cb_list);
8830 
8831 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8832 			   void *type_data)
8833 {
8834 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8835 
8836 	switch (type) {
8837 	case TC_SETUP_QDISC_MQPRIO:
8838 		return i40e_setup_tc(netdev, type_data);
8839 	case TC_SETUP_BLOCK:
8840 		return flow_block_cb_setup_simple(type_data,
8841 						  &i40e_block_cb_list,
8842 						  i40e_setup_tc_block_cb,
8843 						  np, np, true);
8844 	default:
8845 		return -EOPNOTSUPP;
8846 	}
8847 }
8848 
8849 /**
8850  * i40e_open - Called when a network interface is made active
8851  * @netdev: network interface device structure
8852  *
8853  * The open entry point is called when a network interface is made
8854  * active by the system (IFF_UP).  At this point all resources needed
8855  * for transmit and receive operations are allocated, the interrupt
8856  * handler is registered with the OS, the netdev watchdog subtask is
8857  * enabled, and the stack is notified that the interface is ready.
8858  *
8859  * Returns 0 on success, negative value on failure
8860  **/
8861 int i40e_open(struct net_device *netdev)
8862 {
8863 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8864 	struct i40e_vsi *vsi = np->vsi;
8865 	struct i40e_pf *pf = vsi->back;
8866 	int err;
8867 
8868 	/* disallow open during test or if eeprom is broken */
8869 	if (test_bit(__I40E_TESTING, pf->state) ||
8870 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8871 		return -EBUSY;
8872 
8873 	netif_carrier_off(netdev);
8874 
8875 	if (i40e_force_link_state(pf, true))
8876 		return -EAGAIN;
8877 
8878 	err = i40e_vsi_open(vsi);
8879 	if (err)
8880 		return err;
8881 
8882 	/* configure global TSO hardware offload settings */
8883 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8884 						       TCP_FLAG_FIN) >> 16);
8885 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8886 						       TCP_FLAG_FIN |
8887 						       TCP_FLAG_CWR) >> 16);
8888 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8889 	udp_tunnel_get_rx_info(netdev);
8890 
8891 	return 0;
8892 }
8893 
8894 /**
8895  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8896  * @vsi: vsi structure
8897  *
8898  * This updates netdev's number of tx/rx queues
8899  *
8900  * Returns status of setting tx/rx queues
8901  **/
8902 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8903 {
8904 	int ret;
8905 
8906 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8907 					   vsi->num_queue_pairs);
8908 	if (ret)
8909 		return ret;
8910 
8911 	return netif_set_real_num_tx_queues(vsi->netdev,
8912 					    vsi->num_queue_pairs);
8913 }
8914 
8915 /**
8916  * i40e_vsi_open -
8917  * @vsi: the VSI to open
8918  *
8919  * Finish initialization of the VSI.
8920  *
8921  * Returns 0 on success, negative value on failure
8922  *
8923  * Note: expects to be called while under rtnl_lock()
8924  **/
8925 int i40e_vsi_open(struct i40e_vsi *vsi)
8926 {
8927 	struct i40e_pf *pf = vsi->back;
8928 	char int_name[I40E_INT_NAME_STR_LEN];
8929 	int err;
8930 
8931 	/* allocate descriptors */
8932 	err = i40e_vsi_setup_tx_resources(vsi);
8933 	if (err)
8934 		goto err_setup_tx;
8935 	err = i40e_vsi_setup_rx_resources(vsi);
8936 	if (err)
8937 		goto err_setup_rx;
8938 
8939 	err = i40e_vsi_configure(vsi);
8940 	if (err)
8941 		goto err_setup_rx;
8942 
8943 	if (vsi->netdev) {
8944 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8945 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8946 		err = i40e_vsi_request_irq(vsi, int_name);
8947 		if (err)
8948 			goto err_setup_rx;
8949 
8950 		/* Notify the stack of the actual queue counts. */
8951 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8952 		if (err)
8953 			goto err_set_queues;
8954 
8955 	} else if (vsi->type == I40E_VSI_FDIR) {
8956 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8957 			 dev_driver_string(&pf->pdev->dev),
8958 			 dev_name(&pf->pdev->dev));
8959 		err = i40e_vsi_request_irq(vsi, int_name);
8960 		if (err)
8961 			goto err_setup_rx;
8962 
8963 	} else {
8964 		err = -EINVAL;
8965 		goto err_setup_rx;
8966 	}
8967 
8968 	err = i40e_up_complete(vsi);
8969 	if (err)
8970 		goto err_up_complete;
8971 
8972 	return 0;
8973 
8974 err_up_complete:
8975 	i40e_down(vsi);
8976 err_set_queues:
8977 	i40e_vsi_free_irq(vsi);
8978 err_setup_rx:
8979 	i40e_vsi_free_rx_resources(vsi);
8980 err_setup_tx:
8981 	i40e_vsi_free_tx_resources(vsi);
8982 	if (vsi == pf->vsi[pf->lan_vsi])
8983 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8984 
8985 	return err;
8986 }
8987 
8988 /**
8989  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8990  * @pf: Pointer to PF
8991  *
8992  * This function destroys the hlist where all the Flow Director
8993  * filters were saved.
8994  **/
8995 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8996 {
8997 	struct i40e_fdir_filter *filter;
8998 	struct i40e_flex_pit *pit_entry, *tmp;
8999 	struct hlist_node *node2;
9000 
9001 	hlist_for_each_entry_safe(filter, node2,
9002 				  &pf->fdir_filter_list, fdir_node) {
9003 		hlist_del(&filter->fdir_node);
9004 		kfree(filter);
9005 	}
9006 
9007 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9008 		list_del(&pit_entry->list);
9009 		kfree(pit_entry);
9010 	}
9011 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9012 
9013 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9014 		list_del(&pit_entry->list);
9015 		kfree(pit_entry);
9016 	}
9017 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9018 
9019 	pf->fdir_pf_active_filters = 0;
9020 	i40e_reset_fdir_filter_cnt(pf);
9021 
9022 	/* Reprogram the default input set for TCP/IPv4 */
9023 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9024 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9025 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9026 
9027 	/* Reprogram the default input set for TCP/IPv6 */
9028 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9029 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9030 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9031 
9032 	/* Reprogram the default input set for UDP/IPv4 */
9033 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9034 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9035 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9036 
9037 	/* Reprogram the default input set for UDP/IPv6 */
9038 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9039 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9040 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9041 
9042 	/* Reprogram the default input set for SCTP/IPv4 */
9043 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9044 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9045 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9046 
9047 	/* Reprogram the default input set for SCTP/IPv6 */
9048 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9049 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9050 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9051 
9052 	/* Reprogram the default input set for Other/IPv4 */
9053 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9054 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9055 
9056 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9057 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9058 
9059 	/* Reprogram the default input set for Other/IPv6 */
9060 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9061 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9062 
9063 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9064 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9065 }
9066 
9067 /**
9068  * i40e_cloud_filter_exit - Cleans up the cloud filters
9069  * @pf: Pointer to PF
9070  *
9071  * This function destroys the hlist where all the cloud filters
9072  * were saved.
9073  **/
9074 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9075 {
9076 	struct i40e_cloud_filter *cfilter;
9077 	struct hlist_node *node;
9078 
9079 	hlist_for_each_entry_safe(cfilter, node,
9080 				  &pf->cloud_filter_list, cloud_node) {
9081 		hlist_del(&cfilter->cloud_node);
9082 		kfree(cfilter);
9083 	}
9084 	pf->num_cloud_filters = 0;
9085 
9086 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9087 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9088 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9089 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9090 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9091 	}
9092 }
9093 
9094 /**
9095  * i40e_close - Disables a network interface
9096  * @netdev: network interface device structure
9097  *
9098  * The close entry point is called when an interface is de-activated
9099  * by the OS.  The hardware is still under the driver's control, but
9100  * this netdev interface is disabled.
9101  *
9102  * Returns 0, this is not allowed to fail
9103  **/
9104 int i40e_close(struct net_device *netdev)
9105 {
9106 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9107 	struct i40e_vsi *vsi = np->vsi;
9108 
9109 	i40e_vsi_close(vsi);
9110 
9111 	return 0;
9112 }
9113 
9114 /**
9115  * i40e_do_reset - Start a PF or Core Reset sequence
9116  * @pf: board private structure
9117  * @reset_flags: which reset is requested
9118  * @lock_acquired: indicates whether or not the lock has been acquired
9119  * before this function was called.
9120  *
9121  * The essential difference in resets is that the PF Reset
9122  * doesn't clear the packet buffers, doesn't reset the PE
9123  * firmware, and doesn't bother the other PFs on the chip.
9124  **/
9125 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9126 {
9127 	u32 val;
9128 
9129 	/* do the biggest reset indicated */
9130 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9131 
9132 		/* Request a Global Reset
9133 		 *
9134 		 * This will start the chip's countdown to the actual full
9135 		 * chip reset event, and a warning interrupt to be sent
9136 		 * to all PFs, including the requestor.  Our handler
9137 		 * for the warning interrupt will deal with the shutdown
9138 		 * and recovery of the switch setup.
9139 		 */
9140 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9141 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9142 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9143 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9144 
9145 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9146 
9147 		/* Request a Core Reset
9148 		 *
9149 		 * Same as Global Reset, except does *not* include the MAC/PHY
9150 		 */
9151 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9152 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9153 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9154 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9155 		i40e_flush(&pf->hw);
9156 
9157 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9158 
9159 		/* Request a PF Reset
9160 		 *
9161 		 * Resets only the PF-specific registers
9162 		 *
9163 		 * This goes directly to the tear-down and rebuild of
9164 		 * the switch, since we need to do all the recovery as
9165 		 * for the Core Reset.
9166 		 */
9167 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9168 		i40e_handle_reset_warning(pf, lock_acquired);
9169 
9170 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9171 		/* Request a PF Reset
9172 		 *
9173 		 * Resets PF and reinitializes PFs VSI.
9174 		 */
9175 		i40e_prep_for_reset(pf);
9176 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9177 		dev_info(&pf->pdev->dev,
9178 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9179 			 "FW LLDP is disabled\n" :
9180 			 "FW LLDP is enabled\n");
9181 
9182 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9183 		int v;
9184 
9185 		/* Find the VSI(s) that requested a re-init */
9186 		dev_info(&pf->pdev->dev,
9187 			 "VSI reinit requested\n");
9188 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9189 			struct i40e_vsi *vsi = pf->vsi[v];
9190 
9191 			if (vsi != NULL &&
9192 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9193 					       vsi->state))
9194 				i40e_vsi_reinit_locked(pf->vsi[v]);
9195 		}
9196 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9197 		int v;
9198 
9199 		/* Find the VSI(s) that needs to be brought down */
9200 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9201 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9202 			struct i40e_vsi *vsi = pf->vsi[v];
9203 
9204 			if (vsi != NULL &&
9205 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9206 					       vsi->state)) {
9207 				set_bit(__I40E_VSI_DOWN, vsi->state);
9208 				i40e_down(vsi);
9209 			}
9210 		}
9211 	} else {
9212 		dev_info(&pf->pdev->dev,
9213 			 "bad reset request 0x%08x\n", reset_flags);
9214 	}
9215 }
9216 
9217 #ifdef CONFIG_I40E_DCB
9218 /**
9219  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9220  * @pf: board private structure
9221  * @old_cfg: current DCB config
9222  * @new_cfg: new DCB config
9223  **/
9224 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9225 			    struct i40e_dcbx_config *old_cfg,
9226 			    struct i40e_dcbx_config *new_cfg)
9227 {
9228 	bool need_reconfig = false;
9229 
9230 	/* Check if ETS configuration has changed */
9231 	if (memcmp(&new_cfg->etscfg,
9232 		   &old_cfg->etscfg,
9233 		   sizeof(new_cfg->etscfg))) {
9234 		/* If Priority Table has changed reconfig is needed */
9235 		if (memcmp(&new_cfg->etscfg.prioritytable,
9236 			   &old_cfg->etscfg.prioritytable,
9237 			   sizeof(new_cfg->etscfg.prioritytable))) {
9238 			need_reconfig = true;
9239 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9240 		}
9241 
9242 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9243 			   &old_cfg->etscfg.tcbwtable,
9244 			   sizeof(new_cfg->etscfg.tcbwtable)))
9245 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9246 
9247 		if (memcmp(&new_cfg->etscfg.tsatable,
9248 			   &old_cfg->etscfg.tsatable,
9249 			   sizeof(new_cfg->etscfg.tsatable)))
9250 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9251 	}
9252 
9253 	/* Check if PFC configuration has changed */
9254 	if (memcmp(&new_cfg->pfc,
9255 		   &old_cfg->pfc,
9256 		   sizeof(new_cfg->pfc))) {
9257 		need_reconfig = true;
9258 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9259 	}
9260 
9261 	/* Check if APP Table has changed */
9262 	if (memcmp(&new_cfg->app,
9263 		   &old_cfg->app,
9264 		   sizeof(new_cfg->app))) {
9265 		need_reconfig = true;
9266 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9267 	}
9268 
9269 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9270 	return need_reconfig;
9271 }
9272 
9273 /**
9274  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9275  * @pf: board private structure
9276  * @e: event info posted on ARQ
9277  **/
9278 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9279 				  struct i40e_arq_event_info *e)
9280 {
9281 	struct i40e_aqc_lldp_get_mib *mib =
9282 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9283 	struct i40e_hw *hw = &pf->hw;
9284 	struct i40e_dcbx_config tmp_dcbx_cfg;
9285 	bool need_reconfig = false;
9286 	int ret = 0;
9287 	u8 type;
9288 
9289 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9290 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9291 	    (hw->phy.link_info.link_speed &
9292 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9293 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9294 		/* let firmware decide if the DCB should be disabled */
9295 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9296 
9297 	/* Not DCB capable or capability disabled */
9298 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9299 		return ret;
9300 
9301 	/* Ignore if event is not for Nearest Bridge */
9302 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9303 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9304 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9305 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9306 		return ret;
9307 
9308 	/* Check MIB Type and return if event for Remote MIB update */
9309 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9310 	dev_dbg(&pf->pdev->dev,
9311 		"LLDP event mib type %s\n", type ? "remote" : "local");
9312 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9313 		/* Update the remote cached instance and return */
9314 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9315 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9316 				&hw->remote_dcbx_config);
9317 		goto exit;
9318 	}
9319 
9320 	/* Store the old configuration */
9321 	tmp_dcbx_cfg = hw->local_dcbx_config;
9322 
9323 	/* Reset the old DCBx configuration data */
9324 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9325 	/* Get updated DCBX data from firmware */
9326 	ret = i40e_get_dcb_config(&pf->hw);
9327 	if (ret) {
9328 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9329 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9330 		    (hw->phy.link_info.link_speed &
9331 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9332 			dev_warn(&pf->pdev->dev,
9333 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9334 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9335 		} else {
9336 			dev_info(&pf->pdev->dev,
9337 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9338 				 i40e_stat_str(&pf->hw, ret),
9339 				 i40e_aq_str(&pf->hw,
9340 					     pf->hw.aq.asq_last_status));
9341 		}
9342 		goto exit;
9343 	}
9344 
9345 	/* No change detected in DCBX configs */
9346 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9347 		    sizeof(tmp_dcbx_cfg))) {
9348 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9349 		goto exit;
9350 	}
9351 
9352 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9353 					       &hw->local_dcbx_config);
9354 
9355 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9356 
9357 	if (!need_reconfig)
9358 		goto exit;
9359 
9360 	/* Enable DCB tagging only when more than one TC */
9361 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9362 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9363 	else
9364 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9365 
9366 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9367 	/* Reconfiguration needed quiesce all VSIs */
9368 	i40e_pf_quiesce_all_vsi(pf);
9369 
9370 	/* Changes in configuration update VEB/VSI */
9371 	i40e_dcb_reconfigure(pf);
9372 
9373 	ret = i40e_resume_port_tx(pf);
9374 
9375 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9376 	/* In case of error no point in resuming VSIs */
9377 	if (ret)
9378 		goto exit;
9379 
9380 	/* Wait for the PF's queues to be disabled */
9381 	ret = i40e_pf_wait_queues_disabled(pf);
9382 	if (ret) {
9383 		/* Schedule PF reset to recover */
9384 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9385 		i40e_service_event_schedule(pf);
9386 	} else {
9387 		i40e_pf_unquiesce_all_vsi(pf);
9388 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9389 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9390 	}
9391 
9392 exit:
9393 	return ret;
9394 }
9395 #endif /* CONFIG_I40E_DCB */
9396 
9397 /**
9398  * i40e_do_reset_safe - Protected reset path for userland calls.
9399  * @pf: board private structure
9400  * @reset_flags: which reset is requested
9401  *
9402  **/
9403 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9404 {
9405 	rtnl_lock();
9406 	i40e_do_reset(pf, reset_flags, true);
9407 	rtnl_unlock();
9408 }
9409 
9410 /**
9411  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9412  * @pf: board private structure
9413  * @e: event info posted on ARQ
9414  *
9415  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9416  * and VF queues
9417  **/
9418 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9419 					   struct i40e_arq_event_info *e)
9420 {
9421 	struct i40e_aqc_lan_overflow *data =
9422 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9423 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9424 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9425 	struct i40e_hw *hw = &pf->hw;
9426 	struct i40e_vf *vf;
9427 	u16 vf_id;
9428 
9429 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9430 		queue, qtx_ctl);
9431 
9432 	/* Queue belongs to VF, find the VF and issue VF reset */
9433 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9434 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9435 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9436 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9437 		vf_id -= hw->func_caps.vf_base_id;
9438 		vf = &pf->vf[vf_id];
9439 		i40e_vc_notify_vf_reset(vf);
9440 		/* Allow VF to process pending reset notification */
9441 		msleep(20);
9442 		i40e_reset_vf(vf, false);
9443 	}
9444 }
9445 
9446 /**
9447  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9448  * @pf: board private structure
9449  **/
9450 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9451 {
9452 	u32 val, fcnt_prog;
9453 
9454 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9455 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9456 	return fcnt_prog;
9457 }
9458 
9459 /**
9460  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9461  * @pf: board private structure
9462  **/
9463 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9464 {
9465 	u32 val, fcnt_prog;
9466 
9467 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9468 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9469 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9470 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9471 	return fcnt_prog;
9472 }
9473 
9474 /**
9475  * i40e_get_global_fd_count - Get total FD filters programmed on device
9476  * @pf: board private structure
9477  **/
9478 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9479 {
9480 	u32 val, fcnt_prog;
9481 
9482 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9483 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9484 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9485 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9486 	return fcnt_prog;
9487 }
9488 
9489 /**
9490  * i40e_reenable_fdir_sb - Restore FDir SB capability
9491  * @pf: board private structure
9492  **/
9493 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9494 {
9495 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9496 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9497 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9498 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9499 }
9500 
9501 /**
9502  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9503  * @pf: board private structure
9504  **/
9505 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9506 {
9507 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9508 		/* ATR uses the same filtering logic as SB rules. It only
9509 		 * functions properly if the input set mask is at the default
9510 		 * settings. It is safe to restore the default input set
9511 		 * because there are no active TCPv4 filter rules.
9512 		 */
9513 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9514 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9515 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9516 
9517 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9518 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9519 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9520 	}
9521 }
9522 
9523 /**
9524  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9525  * @pf: board private structure
9526  * @filter: FDir filter to remove
9527  */
9528 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9529 				       struct i40e_fdir_filter *filter)
9530 {
9531 	/* Update counters */
9532 	pf->fdir_pf_active_filters--;
9533 	pf->fd_inv = 0;
9534 
9535 	switch (filter->flow_type) {
9536 	case TCP_V4_FLOW:
9537 		pf->fd_tcp4_filter_cnt--;
9538 		break;
9539 	case UDP_V4_FLOW:
9540 		pf->fd_udp4_filter_cnt--;
9541 		break;
9542 	case SCTP_V4_FLOW:
9543 		pf->fd_sctp4_filter_cnt--;
9544 		break;
9545 	case TCP_V6_FLOW:
9546 		pf->fd_tcp6_filter_cnt--;
9547 		break;
9548 	case UDP_V6_FLOW:
9549 		pf->fd_udp6_filter_cnt--;
9550 		break;
9551 	case SCTP_V6_FLOW:
9552 		pf->fd_udp6_filter_cnt--;
9553 		break;
9554 	case IP_USER_FLOW:
9555 		switch (filter->ipl4_proto) {
9556 		case IPPROTO_TCP:
9557 			pf->fd_tcp4_filter_cnt--;
9558 			break;
9559 		case IPPROTO_UDP:
9560 			pf->fd_udp4_filter_cnt--;
9561 			break;
9562 		case IPPROTO_SCTP:
9563 			pf->fd_sctp4_filter_cnt--;
9564 			break;
9565 		case IPPROTO_IP:
9566 			pf->fd_ip4_filter_cnt--;
9567 			break;
9568 		}
9569 		break;
9570 	case IPV6_USER_FLOW:
9571 		switch (filter->ipl4_proto) {
9572 		case IPPROTO_TCP:
9573 			pf->fd_tcp6_filter_cnt--;
9574 			break;
9575 		case IPPROTO_UDP:
9576 			pf->fd_udp6_filter_cnt--;
9577 			break;
9578 		case IPPROTO_SCTP:
9579 			pf->fd_sctp6_filter_cnt--;
9580 			break;
9581 		case IPPROTO_IP:
9582 			pf->fd_ip6_filter_cnt--;
9583 			break;
9584 		}
9585 		break;
9586 	}
9587 
9588 	/* Remove the filter from the list and free memory */
9589 	hlist_del(&filter->fdir_node);
9590 	kfree(filter);
9591 }
9592 
9593 /**
9594  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9595  * @pf: board private structure
9596  **/
9597 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9598 {
9599 	struct i40e_fdir_filter *filter;
9600 	u32 fcnt_prog, fcnt_avail;
9601 	struct hlist_node *node;
9602 
9603 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9604 		return;
9605 
9606 	/* Check if we have enough room to re-enable FDir SB capability. */
9607 	fcnt_prog = i40e_get_global_fd_count(pf);
9608 	fcnt_avail = pf->fdir_pf_filter_count;
9609 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9610 	    (pf->fd_add_err == 0) ||
9611 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9612 		i40e_reenable_fdir_sb(pf);
9613 
9614 	/* We should wait for even more space before re-enabling ATR.
9615 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9616 	 * rules active.
9617 	 */
9618 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9619 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9620 		i40e_reenable_fdir_atr(pf);
9621 
9622 	/* if hw had a problem adding a filter, delete it */
9623 	if (pf->fd_inv > 0) {
9624 		hlist_for_each_entry_safe(filter, node,
9625 					  &pf->fdir_filter_list, fdir_node)
9626 			if (filter->fd_id == pf->fd_inv)
9627 				i40e_delete_invalid_filter(pf, filter);
9628 	}
9629 }
9630 
9631 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9632 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9633 /**
9634  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9635  * @pf: board private structure
9636  **/
9637 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9638 {
9639 	unsigned long min_flush_time;
9640 	int flush_wait_retry = 50;
9641 	bool disable_atr = false;
9642 	int fd_room;
9643 	int reg;
9644 
9645 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9646 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9647 		return;
9648 
9649 	/* If the flush is happening too quick and we have mostly SB rules we
9650 	 * should not re-enable ATR for some time.
9651 	 */
9652 	min_flush_time = pf->fd_flush_timestamp +
9653 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9654 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9655 
9656 	if (!(time_after(jiffies, min_flush_time)) &&
9657 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9658 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9659 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9660 		disable_atr = true;
9661 	}
9662 
9663 	pf->fd_flush_timestamp = jiffies;
9664 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9665 	/* flush all filters */
9666 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9667 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9668 	i40e_flush(&pf->hw);
9669 	pf->fd_flush_cnt++;
9670 	pf->fd_add_err = 0;
9671 	do {
9672 		/* Check FD flush status every 5-6msec */
9673 		usleep_range(5000, 6000);
9674 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9675 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9676 			break;
9677 	} while (flush_wait_retry--);
9678 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9679 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9680 	} else {
9681 		/* replay sideband filters */
9682 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9683 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9684 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9685 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9686 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9687 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9688 	}
9689 }
9690 
9691 /**
9692  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9693  * @pf: board private structure
9694  **/
9695 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9696 {
9697 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9698 }
9699 
9700 /**
9701  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9702  * @pf: board private structure
9703  **/
9704 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9705 {
9706 
9707 	/* if interface is down do nothing */
9708 	if (test_bit(__I40E_DOWN, pf->state))
9709 		return;
9710 
9711 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9712 		i40e_fdir_flush_and_replay(pf);
9713 
9714 	i40e_fdir_check_and_reenable(pf);
9715 
9716 }
9717 
9718 /**
9719  * i40e_vsi_link_event - notify VSI of a link event
9720  * @vsi: vsi to be notified
9721  * @link_up: link up or down
9722  **/
9723 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9724 {
9725 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9726 		return;
9727 
9728 	switch (vsi->type) {
9729 	case I40E_VSI_MAIN:
9730 		if (!vsi->netdev || !vsi->netdev_registered)
9731 			break;
9732 
9733 		if (link_up) {
9734 			netif_carrier_on(vsi->netdev);
9735 			netif_tx_wake_all_queues(vsi->netdev);
9736 		} else {
9737 			netif_carrier_off(vsi->netdev);
9738 			netif_tx_stop_all_queues(vsi->netdev);
9739 		}
9740 		break;
9741 
9742 	case I40E_VSI_SRIOV:
9743 	case I40E_VSI_VMDQ2:
9744 	case I40E_VSI_CTRL:
9745 	case I40E_VSI_IWARP:
9746 	case I40E_VSI_MIRROR:
9747 	default:
9748 		/* there is no notification for other VSIs */
9749 		break;
9750 	}
9751 }
9752 
9753 /**
9754  * i40e_veb_link_event - notify elements on the veb of a link event
9755  * @veb: veb to be notified
9756  * @link_up: link up or down
9757  **/
9758 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9759 {
9760 	struct i40e_pf *pf;
9761 	int i;
9762 
9763 	if (!veb || !veb->pf)
9764 		return;
9765 	pf = veb->pf;
9766 
9767 	/* depth first... */
9768 	for (i = 0; i < I40E_MAX_VEB; i++)
9769 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9770 			i40e_veb_link_event(pf->veb[i], link_up);
9771 
9772 	/* ... now the local VSIs */
9773 	for (i = 0; i < pf->num_alloc_vsi; i++)
9774 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9775 			i40e_vsi_link_event(pf->vsi[i], link_up);
9776 }
9777 
9778 /**
9779  * i40e_link_event - Update netif_carrier status
9780  * @pf: board private structure
9781  **/
9782 static void i40e_link_event(struct i40e_pf *pf)
9783 {
9784 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9785 	u8 new_link_speed, old_link_speed;
9786 	i40e_status status;
9787 	bool new_link, old_link;
9788 #ifdef CONFIG_I40E_DCB
9789 	int err;
9790 #endif /* CONFIG_I40E_DCB */
9791 
9792 	/* set this to force the get_link_status call to refresh state */
9793 	pf->hw.phy.get_link_info = true;
9794 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9795 	status = i40e_get_link_status(&pf->hw, &new_link);
9796 
9797 	/* On success, disable temp link polling */
9798 	if (status == I40E_SUCCESS) {
9799 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9800 	} else {
9801 		/* Enable link polling temporarily until i40e_get_link_status
9802 		 * returns I40E_SUCCESS
9803 		 */
9804 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9805 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9806 			status);
9807 		return;
9808 	}
9809 
9810 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9811 	new_link_speed = pf->hw.phy.link_info.link_speed;
9812 
9813 	if (new_link == old_link &&
9814 	    new_link_speed == old_link_speed &&
9815 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9816 	     new_link == netif_carrier_ok(vsi->netdev)))
9817 		return;
9818 
9819 	i40e_print_link_message(vsi, new_link);
9820 
9821 	/* Notify the base of the switch tree connected to
9822 	 * the link.  Floating VEBs are not notified.
9823 	 */
9824 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9825 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9826 	else
9827 		i40e_vsi_link_event(vsi, new_link);
9828 
9829 	if (pf->vf)
9830 		i40e_vc_notify_link_state(pf);
9831 
9832 	if (pf->flags & I40E_FLAG_PTP)
9833 		i40e_ptp_set_increment(pf);
9834 #ifdef CONFIG_I40E_DCB
9835 	if (new_link == old_link)
9836 		return;
9837 	/* Not SW DCB so firmware will take care of default settings */
9838 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9839 		return;
9840 
9841 	/* We cover here only link down, as after link up in case of SW DCB
9842 	 * SW LLDP agent will take care of setting it up
9843 	 */
9844 	if (!new_link) {
9845 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9846 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9847 		err = i40e_dcb_sw_default_config(pf);
9848 		if (err) {
9849 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9850 				       I40E_FLAG_DCB_ENABLED);
9851 		} else {
9852 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9853 				       DCB_CAP_DCBX_VER_IEEE;
9854 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9855 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9856 		}
9857 	}
9858 #endif /* CONFIG_I40E_DCB */
9859 }
9860 
9861 /**
9862  * i40e_watchdog_subtask - periodic checks not using event driven response
9863  * @pf: board private structure
9864  **/
9865 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9866 {
9867 	int i;
9868 
9869 	/* if interface is down do nothing */
9870 	if (test_bit(__I40E_DOWN, pf->state) ||
9871 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9872 		return;
9873 
9874 	/* make sure we don't do these things too often */
9875 	if (time_before(jiffies, (pf->service_timer_previous +
9876 				  pf->service_timer_period)))
9877 		return;
9878 	pf->service_timer_previous = jiffies;
9879 
9880 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9881 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9882 		i40e_link_event(pf);
9883 
9884 	/* Update the stats for active netdevs so the network stack
9885 	 * can look at updated numbers whenever it cares to
9886 	 */
9887 	for (i = 0; i < pf->num_alloc_vsi; i++)
9888 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9889 			i40e_update_stats(pf->vsi[i]);
9890 
9891 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9892 		/* Update the stats for the active switching components */
9893 		for (i = 0; i < I40E_MAX_VEB; i++)
9894 			if (pf->veb[i])
9895 				i40e_update_veb_stats(pf->veb[i]);
9896 	}
9897 
9898 	i40e_ptp_rx_hang(pf);
9899 	i40e_ptp_tx_hang(pf);
9900 }
9901 
9902 /**
9903  * i40e_reset_subtask - Set up for resetting the device and driver
9904  * @pf: board private structure
9905  **/
9906 static void i40e_reset_subtask(struct i40e_pf *pf)
9907 {
9908 	u32 reset_flags = 0;
9909 
9910 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9911 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9912 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9913 	}
9914 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9915 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9916 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9917 	}
9918 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9919 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9920 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9921 	}
9922 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9923 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9924 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9925 	}
9926 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9927 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9928 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9929 	}
9930 
9931 	/* If there's a recovery already waiting, it takes
9932 	 * precedence before starting a new reset sequence.
9933 	 */
9934 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9935 		i40e_prep_for_reset(pf);
9936 		i40e_reset(pf);
9937 		i40e_rebuild(pf, false, false);
9938 	}
9939 
9940 	/* If we're already down or resetting, just bail */
9941 	if (reset_flags &&
9942 	    !test_bit(__I40E_DOWN, pf->state) &&
9943 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9944 		i40e_do_reset(pf, reset_flags, false);
9945 	}
9946 }
9947 
9948 /**
9949  * i40e_handle_link_event - Handle link event
9950  * @pf: board private structure
9951  * @e: event info posted on ARQ
9952  **/
9953 static void i40e_handle_link_event(struct i40e_pf *pf,
9954 				   struct i40e_arq_event_info *e)
9955 {
9956 	struct i40e_aqc_get_link_status *status =
9957 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9958 
9959 	/* Do a new status request to re-enable LSE reporting
9960 	 * and load new status information into the hw struct
9961 	 * This completely ignores any state information
9962 	 * in the ARQ event info, instead choosing to always
9963 	 * issue the AQ update link status command.
9964 	 */
9965 	i40e_link_event(pf);
9966 
9967 	/* Check if module meets thermal requirements */
9968 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9969 		dev_err(&pf->pdev->dev,
9970 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9971 		dev_err(&pf->pdev->dev,
9972 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9973 	} else {
9974 		/* check for unqualified module, if link is down, suppress
9975 		 * the message if link was forced to be down.
9976 		 */
9977 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9978 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9979 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9980 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9981 			dev_err(&pf->pdev->dev,
9982 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9983 			dev_err(&pf->pdev->dev,
9984 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9985 		}
9986 	}
9987 }
9988 
9989 /**
9990  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9991  * @pf: board private structure
9992  **/
9993 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9994 {
9995 	struct i40e_arq_event_info event;
9996 	struct i40e_hw *hw = &pf->hw;
9997 	u16 pending, i = 0;
9998 	i40e_status ret;
9999 	u16 opcode;
10000 	u32 oldval;
10001 	u32 val;
10002 
10003 	/* Do not run clean AQ when PF reset fails */
10004 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10005 		return;
10006 
10007 	/* check for error indications */
10008 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10009 	oldval = val;
10010 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10011 		if (hw->debug_mask & I40E_DEBUG_AQ)
10012 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10013 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10014 	}
10015 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10016 		if (hw->debug_mask & I40E_DEBUG_AQ)
10017 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10018 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10019 		pf->arq_overflows++;
10020 	}
10021 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10022 		if (hw->debug_mask & I40E_DEBUG_AQ)
10023 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10024 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10025 	}
10026 	if (oldval != val)
10027 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10028 
10029 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10030 	oldval = val;
10031 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10032 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10033 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10034 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10035 	}
10036 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10037 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10038 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10039 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10040 	}
10041 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10042 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10043 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10044 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10045 	}
10046 	if (oldval != val)
10047 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10048 
10049 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10050 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10051 	if (!event.msg_buf)
10052 		return;
10053 
10054 	do {
10055 		ret = i40e_clean_arq_element(hw, &event, &pending);
10056 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10057 			break;
10058 		else if (ret) {
10059 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10060 			break;
10061 		}
10062 
10063 		opcode = le16_to_cpu(event.desc.opcode);
10064 		switch (opcode) {
10065 
10066 		case i40e_aqc_opc_get_link_status:
10067 			rtnl_lock();
10068 			i40e_handle_link_event(pf, &event);
10069 			rtnl_unlock();
10070 			break;
10071 		case i40e_aqc_opc_send_msg_to_pf:
10072 			ret = i40e_vc_process_vf_msg(pf,
10073 					le16_to_cpu(event.desc.retval),
10074 					le32_to_cpu(event.desc.cookie_high),
10075 					le32_to_cpu(event.desc.cookie_low),
10076 					event.msg_buf,
10077 					event.msg_len);
10078 			break;
10079 		case i40e_aqc_opc_lldp_update_mib:
10080 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10081 #ifdef CONFIG_I40E_DCB
10082 			rtnl_lock();
10083 			i40e_handle_lldp_event(pf, &event);
10084 			rtnl_unlock();
10085 #endif /* CONFIG_I40E_DCB */
10086 			break;
10087 		case i40e_aqc_opc_event_lan_overflow:
10088 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10089 			i40e_handle_lan_overflow_event(pf, &event);
10090 			break;
10091 		case i40e_aqc_opc_send_msg_to_peer:
10092 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10093 			break;
10094 		case i40e_aqc_opc_nvm_erase:
10095 		case i40e_aqc_opc_nvm_update:
10096 		case i40e_aqc_opc_oem_post_update:
10097 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10098 				   "ARQ NVM operation 0x%04x completed\n",
10099 				   opcode);
10100 			break;
10101 		default:
10102 			dev_info(&pf->pdev->dev,
10103 				 "ARQ: Unknown event 0x%04x ignored\n",
10104 				 opcode);
10105 			break;
10106 		}
10107 	} while (i++ < pf->adminq_work_limit);
10108 
10109 	if (i < pf->adminq_work_limit)
10110 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10111 
10112 	/* re-enable Admin queue interrupt cause */
10113 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10114 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10115 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10116 	i40e_flush(hw);
10117 
10118 	kfree(event.msg_buf);
10119 }
10120 
10121 /**
10122  * i40e_verify_eeprom - make sure eeprom is good to use
10123  * @pf: board private structure
10124  **/
10125 static void i40e_verify_eeprom(struct i40e_pf *pf)
10126 {
10127 	int err;
10128 
10129 	err = i40e_diag_eeprom_test(&pf->hw);
10130 	if (err) {
10131 		/* retry in case of garbage read */
10132 		err = i40e_diag_eeprom_test(&pf->hw);
10133 		if (err) {
10134 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10135 				 err);
10136 			set_bit(__I40E_BAD_EEPROM, pf->state);
10137 		}
10138 	}
10139 
10140 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10141 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10142 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10143 	}
10144 }
10145 
10146 /**
10147  * i40e_enable_pf_switch_lb
10148  * @pf: pointer to the PF structure
10149  *
10150  * enable switch loop back or die - no point in a return value
10151  **/
10152 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10153 {
10154 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10155 	struct i40e_vsi_context ctxt;
10156 	int ret;
10157 
10158 	ctxt.seid = pf->main_vsi_seid;
10159 	ctxt.pf_num = pf->hw.pf_id;
10160 	ctxt.vf_num = 0;
10161 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10162 	if (ret) {
10163 		dev_info(&pf->pdev->dev,
10164 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10165 			 i40e_stat_str(&pf->hw, ret),
10166 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10167 		return;
10168 	}
10169 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10170 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10171 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10172 
10173 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10174 	if (ret) {
10175 		dev_info(&pf->pdev->dev,
10176 			 "update vsi switch failed, err %s aq_err %s\n",
10177 			 i40e_stat_str(&pf->hw, ret),
10178 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10179 	}
10180 }
10181 
10182 /**
10183  * i40e_disable_pf_switch_lb
10184  * @pf: pointer to the PF structure
10185  *
10186  * disable switch loop back or die - no point in a return value
10187  **/
10188 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10189 {
10190 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10191 	struct i40e_vsi_context ctxt;
10192 	int ret;
10193 
10194 	ctxt.seid = pf->main_vsi_seid;
10195 	ctxt.pf_num = pf->hw.pf_id;
10196 	ctxt.vf_num = 0;
10197 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10198 	if (ret) {
10199 		dev_info(&pf->pdev->dev,
10200 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10201 			 i40e_stat_str(&pf->hw, ret),
10202 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10203 		return;
10204 	}
10205 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10206 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10207 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10208 
10209 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10210 	if (ret) {
10211 		dev_info(&pf->pdev->dev,
10212 			 "update vsi switch failed, err %s aq_err %s\n",
10213 			 i40e_stat_str(&pf->hw, ret),
10214 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10215 	}
10216 }
10217 
10218 /**
10219  * i40e_config_bridge_mode - Configure the HW bridge mode
10220  * @veb: pointer to the bridge instance
10221  *
10222  * Configure the loop back mode for the LAN VSI that is downlink to the
10223  * specified HW bridge instance. It is expected this function is called
10224  * when a new HW bridge is instantiated.
10225  **/
10226 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10227 {
10228 	struct i40e_pf *pf = veb->pf;
10229 
10230 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10231 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10232 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10233 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10234 		i40e_disable_pf_switch_lb(pf);
10235 	else
10236 		i40e_enable_pf_switch_lb(pf);
10237 }
10238 
10239 /**
10240  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10241  * @veb: pointer to the VEB instance
10242  *
10243  * This is a recursive function that first builds the attached VSIs then
10244  * recurses in to build the next layer of VEB.  We track the connections
10245  * through our own index numbers because the seid's from the HW could
10246  * change across the reset.
10247  **/
10248 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10249 {
10250 	struct i40e_vsi *ctl_vsi = NULL;
10251 	struct i40e_pf *pf = veb->pf;
10252 	int v, veb_idx;
10253 	int ret;
10254 
10255 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10256 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10257 		if (pf->vsi[v] &&
10258 		    pf->vsi[v]->veb_idx == veb->idx &&
10259 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10260 			ctl_vsi = pf->vsi[v];
10261 			break;
10262 		}
10263 	}
10264 	if (!ctl_vsi) {
10265 		dev_info(&pf->pdev->dev,
10266 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10267 		ret = -ENOENT;
10268 		goto end_reconstitute;
10269 	}
10270 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10271 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10272 	ret = i40e_add_vsi(ctl_vsi);
10273 	if (ret) {
10274 		dev_info(&pf->pdev->dev,
10275 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10276 			 veb->idx, ret);
10277 		goto end_reconstitute;
10278 	}
10279 	i40e_vsi_reset_stats(ctl_vsi);
10280 
10281 	/* create the VEB in the switch and move the VSI onto the VEB */
10282 	ret = i40e_add_veb(veb, ctl_vsi);
10283 	if (ret)
10284 		goto end_reconstitute;
10285 
10286 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10287 		veb->bridge_mode = BRIDGE_MODE_VEB;
10288 	else
10289 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10290 	i40e_config_bridge_mode(veb);
10291 
10292 	/* create the remaining VSIs attached to this VEB */
10293 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10294 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10295 			continue;
10296 
10297 		if (pf->vsi[v]->veb_idx == veb->idx) {
10298 			struct i40e_vsi *vsi = pf->vsi[v];
10299 
10300 			vsi->uplink_seid = veb->seid;
10301 			ret = i40e_add_vsi(vsi);
10302 			if (ret) {
10303 				dev_info(&pf->pdev->dev,
10304 					 "rebuild of vsi_idx %d failed: %d\n",
10305 					 v, ret);
10306 				goto end_reconstitute;
10307 			}
10308 			i40e_vsi_reset_stats(vsi);
10309 		}
10310 	}
10311 
10312 	/* create any VEBs attached to this VEB - RECURSION */
10313 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10314 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10315 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10316 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10317 			if (ret)
10318 				break;
10319 		}
10320 	}
10321 
10322 end_reconstitute:
10323 	return ret;
10324 }
10325 
10326 /**
10327  * i40e_get_capabilities - get info about the HW
10328  * @pf: the PF struct
10329  * @list_type: AQ capability to be queried
10330  **/
10331 static int i40e_get_capabilities(struct i40e_pf *pf,
10332 				 enum i40e_admin_queue_opc list_type)
10333 {
10334 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10335 	u16 data_size;
10336 	int buf_len;
10337 	int err;
10338 
10339 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10340 	do {
10341 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10342 		if (!cap_buf)
10343 			return -ENOMEM;
10344 
10345 		/* this loads the data into the hw struct for us */
10346 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10347 						    &data_size, list_type,
10348 						    NULL);
10349 		/* data loaded, buffer no longer needed */
10350 		kfree(cap_buf);
10351 
10352 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10353 			/* retry with a larger buffer */
10354 			buf_len = data_size;
10355 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10356 			dev_info(&pf->pdev->dev,
10357 				 "capability discovery failed, err %s aq_err %s\n",
10358 				 i40e_stat_str(&pf->hw, err),
10359 				 i40e_aq_str(&pf->hw,
10360 					     pf->hw.aq.asq_last_status));
10361 			return -ENODEV;
10362 		}
10363 	} while (err);
10364 
10365 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10366 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10367 			dev_info(&pf->pdev->dev,
10368 				 "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",
10369 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10370 				 pf->hw.func_caps.num_msix_vectors,
10371 				 pf->hw.func_caps.num_msix_vectors_vf,
10372 				 pf->hw.func_caps.fd_filters_guaranteed,
10373 				 pf->hw.func_caps.fd_filters_best_effort,
10374 				 pf->hw.func_caps.num_tx_qp,
10375 				 pf->hw.func_caps.num_vsis);
10376 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10377 			dev_info(&pf->pdev->dev,
10378 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10379 				 pf->hw.dev_caps.switch_mode,
10380 				 pf->hw.dev_caps.valid_functions);
10381 			dev_info(&pf->pdev->dev,
10382 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10383 				 pf->hw.dev_caps.sr_iov_1_1,
10384 				 pf->hw.dev_caps.num_vfs);
10385 			dev_info(&pf->pdev->dev,
10386 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10387 				 pf->hw.dev_caps.num_vsis,
10388 				 pf->hw.dev_caps.num_rx_qp,
10389 				 pf->hw.dev_caps.num_tx_qp);
10390 		}
10391 	}
10392 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10393 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10394 		       + pf->hw.func_caps.num_vfs)
10395 		if (pf->hw.revision_id == 0 &&
10396 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10397 			dev_info(&pf->pdev->dev,
10398 				 "got num_vsis %d, setting num_vsis to %d\n",
10399 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10400 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10401 		}
10402 	}
10403 	return 0;
10404 }
10405 
10406 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10407 
10408 /**
10409  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10410  * @pf: board private structure
10411  **/
10412 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10413 {
10414 	struct i40e_vsi *vsi;
10415 
10416 	/* quick workaround for an NVM issue that leaves a critical register
10417 	 * uninitialized
10418 	 */
10419 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10420 		static const u32 hkey[] = {
10421 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10422 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10423 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10424 			0x95b3a76d};
10425 		int i;
10426 
10427 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10428 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10429 	}
10430 
10431 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10432 		return;
10433 
10434 	/* find existing VSI and see if it needs configuring */
10435 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10436 
10437 	/* create a new VSI if none exists */
10438 	if (!vsi) {
10439 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10440 				     pf->vsi[pf->lan_vsi]->seid, 0);
10441 		if (!vsi) {
10442 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10443 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10444 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10445 			return;
10446 		}
10447 	}
10448 
10449 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10450 }
10451 
10452 /**
10453  * i40e_fdir_teardown - release the Flow Director resources
10454  * @pf: board private structure
10455  **/
10456 static void i40e_fdir_teardown(struct i40e_pf *pf)
10457 {
10458 	struct i40e_vsi *vsi;
10459 
10460 	i40e_fdir_filter_exit(pf);
10461 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10462 	if (vsi)
10463 		i40e_vsi_release(vsi);
10464 }
10465 
10466 /**
10467  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10468  * @vsi: PF main vsi
10469  * @seid: seid of main or channel VSIs
10470  *
10471  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10472  * existed before reset
10473  **/
10474 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10475 {
10476 	struct i40e_cloud_filter *cfilter;
10477 	struct i40e_pf *pf = vsi->back;
10478 	struct hlist_node *node;
10479 	i40e_status ret;
10480 
10481 	/* Add cloud filters back if they exist */
10482 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10483 				  cloud_node) {
10484 		if (cfilter->seid != seid)
10485 			continue;
10486 
10487 		if (cfilter->dst_port)
10488 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10489 								true);
10490 		else
10491 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10492 
10493 		if (ret) {
10494 			dev_dbg(&pf->pdev->dev,
10495 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10496 				i40e_stat_str(&pf->hw, ret),
10497 				i40e_aq_str(&pf->hw,
10498 					    pf->hw.aq.asq_last_status));
10499 			return ret;
10500 		}
10501 	}
10502 	return 0;
10503 }
10504 
10505 /**
10506  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10507  * @vsi: PF main vsi
10508  *
10509  * Rebuilds channel VSIs if they existed before reset
10510  **/
10511 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10512 {
10513 	struct i40e_channel *ch, *ch_tmp;
10514 	i40e_status ret;
10515 
10516 	if (list_empty(&vsi->ch_list))
10517 		return 0;
10518 
10519 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10520 		if (!ch->initialized)
10521 			break;
10522 		/* Proceed with creation of channel (VMDq2) VSI */
10523 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10524 		if (ret) {
10525 			dev_info(&vsi->back->pdev->dev,
10526 				 "failed to rebuild channels using uplink_seid %u\n",
10527 				 vsi->uplink_seid);
10528 			return ret;
10529 		}
10530 		/* Reconfigure TX queues using QTX_CTL register */
10531 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10532 		if (ret) {
10533 			dev_info(&vsi->back->pdev->dev,
10534 				 "failed to configure TX rings for channel %u\n",
10535 				 ch->seid);
10536 			return ret;
10537 		}
10538 		/* update 'next_base_queue' */
10539 		vsi->next_base_queue = vsi->next_base_queue +
10540 							ch->num_queue_pairs;
10541 		if (ch->max_tx_rate) {
10542 			u64 credits = ch->max_tx_rate;
10543 
10544 			if (i40e_set_bw_limit(vsi, ch->seid,
10545 					      ch->max_tx_rate))
10546 				return -EINVAL;
10547 
10548 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10549 			dev_dbg(&vsi->back->pdev->dev,
10550 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10551 				ch->max_tx_rate,
10552 				credits,
10553 				ch->seid);
10554 		}
10555 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10556 		if (ret) {
10557 			dev_dbg(&vsi->back->pdev->dev,
10558 				"Failed to rebuild cloud filters for channel VSI %u\n",
10559 				ch->seid);
10560 			return ret;
10561 		}
10562 	}
10563 	return 0;
10564 }
10565 
10566 /**
10567  * i40e_prep_for_reset - prep for the core to reset
10568  * @pf: board private structure
10569  *
10570  * Close up the VFs and other things in prep for PF Reset.
10571   **/
10572 static void i40e_prep_for_reset(struct i40e_pf *pf)
10573 {
10574 	struct i40e_hw *hw = &pf->hw;
10575 	i40e_status ret = 0;
10576 	u32 v;
10577 
10578 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10579 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10580 		return;
10581 	if (i40e_check_asq_alive(&pf->hw))
10582 		i40e_vc_notify_reset(pf);
10583 
10584 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10585 
10586 	/* quiesce the VSIs and their queues that are not already DOWN */
10587 	i40e_pf_quiesce_all_vsi(pf);
10588 
10589 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10590 		if (pf->vsi[v])
10591 			pf->vsi[v]->seid = 0;
10592 	}
10593 
10594 	i40e_shutdown_adminq(&pf->hw);
10595 
10596 	/* call shutdown HMC */
10597 	if (hw->hmc.hmc_obj) {
10598 		ret = i40e_shutdown_lan_hmc(hw);
10599 		if (ret)
10600 			dev_warn(&pf->pdev->dev,
10601 				 "shutdown_lan_hmc failed: %d\n", ret);
10602 	}
10603 
10604 	/* Save the current PTP time so that we can restore the time after the
10605 	 * reset completes.
10606 	 */
10607 	i40e_ptp_save_hw_time(pf);
10608 }
10609 
10610 /**
10611  * i40e_send_version - update firmware with driver version
10612  * @pf: PF struct
10613  */
10614 static void i40e_send_version(struct i40e_pf *pf)
10615 {
10616 	struct i40e_driver_version dv;
10617 
10618 	dv.major_version = 0xff;
10619 	dv.minor_version = 0xff;
10620 	dv.build_version = 0xff;
10621 	dv.subbuild_version = 0;
10622 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10623 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10624 }
10625 
10626 /**
10627  * i40e_get_oem_version - get OEM specific version information
10628  * @hw: pointer to the hardware structure
10629  **/
10630 static void i40e_get_oem_version(struct i40e_hw *hw)
10631 {
10632 	u16 block_offset = 0xffff;
10633 	u16 block_length = 0;
10634 	u16 capabilities = 0;
10635 	u16 gen_snap = 0;
10636 	u16 release = 0;
10637 
10638 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10639 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10640 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10641 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10642 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10643 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10644 #define I40E_NVM_OEM_LENGTH			3
10645 
10646 	/* Check if pointer to OEM version block is valid. */
10647 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10648 	if (block_offset == 0xffff)
10649 		return;
10650 
10651 	/* Check if OEM version block has correct length. */
10652 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10653 			   &block_length);
10654 	if (block_length < I40E_NVM_OEM_LENGTH)
10655 		return;
10656 
10657 	/* Check if OEM version format is as expected. */
10658 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10659 			   &capabilities);
10660 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10661 		return;
10662 
10663 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10664 			   &gen_snap);
10665 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10666 			   &release);
10667 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10668 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10669 }
10670 
10671 /**
10672  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10673  * @pf: board private structure
10674  **/
10675 static int i40e_reset(struct i40e_pf *pf)
10676 {
10677 	struct i40e_hw *hw = &pf->hw;
10678 	i40e_status ret;
10679 
10680 	ret = i40e_pf_reset(hw);
10681 	if (ret) {
10682 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10683 		set_bit(__I40E_RESET_FAILED, pf->state);
10684 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10685 	} else {
10686 		pf->pfr_count++;
10687 	}
10688 	return ret;
10689 }
10690 
10691 /**
10692  * i40e_rebuild - rebuild using a saved config
10693  * @pf: board private structure
10694  * @reinit: if the Main VSI needs to re-initialized.
10695  * @lock_acquired: indicates whether or not the lock has been acquired
10696  * before this function was called.
10697  **/
10698 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10699 {
10700 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10701 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10702 	struct i40e_hw *hw = &pf->hw;
10703 	i40e_status ret;
10704 	u32 val;
10705 	int v;
10706 
10707 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10708 	    i40e_check_recovery_mode(pf)) {
10709 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10710 	}
10711 
10712 	if (test_bit(__I40E_DOWN, pf->state) &&
10713 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10714 	    !old_recovery_mode_bit)
10715 		goto clear_recovery;
10716 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10717 
10718 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10719 	ret = i40e_init_adminq(&pf->hw);
10720 	if (ret) {
10721 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10722 			 i40e_stat_str(&pf->hw, ret),
10723 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10724 		goto clear_recovery;
10725 	}
10726 	i40e_get_oem_version(&pf->hw);
10727 
10728 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10729 		/* The following delay is necessary for firmware update. */
10730 		mdelay(1000);
10731 	}
10732 
10733 	/* re-verify the eeprom if we just had an EMP reset */
10734 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10735 		i40e_verify_eeprom(pf);
10736 
10737 	/* if we are going out of or into recovery mode we have to act
10738 	 * accordingly with regard to resources initialization
10739 	 * and deinitialization
10740 	 */
10741 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10742 	    old_recovery_mode_bit) {
10743 		if (i40e_get_capabilities(pf,
10744 					  i40e_aqc_opc_list_func_capabilities))
10745 			goto end_unlock;
10746 
10747 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10748 			/* we're staying in recovery mode so we'll reinitialize
10749 			 * misc vector here
10750 			 */
10751 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10752 				goto end_unlock;
10753 		} else {
10754 			if (!lock_acquired)
10755 				rtnl_lock();
10756 			/* we're going out of recovery mode so we'll free
10757 			 * the IRQ allocated specifically for recovery mode
10758 			 * and restore the interrupt scheme
10759 			 */
10760 			free_irq(pf->pdev->irq, pf);
10761 			i40e_clear_interrupt_scheme(pf);
10762 			if (i40e_restore_interrupt_scheme(pf))
10763 				goto end_unlock;
10764 		}
10765 
10766 		/* tell the firmware that we're starting */
10767 		i40e_send_version(pf);
10768 
10769 		/* bail out in case recovery mode was detected, as there is
10770 		 * no need for further configuration.
10771 		 */
10772 		goto end_unlock;
10773 	}
10774 
10775 	i40e_clear_pxe_mode(hw);
10776 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10777 	if (ret)
10778 		goto end_core_reset;
10779 
10780 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10781 				hw->func_caps.num_rx_qp, 0, 0);
10782 	if (ret) {
10783 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10784 		goto end_core_reset;
10785 	}
10786 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10787 	if (ret) {
10788 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10789 		goto end_core_reset;
10790 	}
10791 
10792 #ifdef CONFIG_I40E_DCB
10793 	/* Enable FW to write a default DCB config on link-up
10794 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10795 	 * is not supported with new link speed
10796 	 */
10797 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10798 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10799 	} else {
10800 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10801 		    (hw->phy.link_info.link_speed &
10802 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10803 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10804 			dev_warn(&pf->pdev->dev,
10805 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10806 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10807 		} else {
10808 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10809 			ret = i40e_init_pf_dcb(pf);
10810 			if (ret) {
10811 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10812 					 ret);
10813 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10814 				/* Continue without DCB enabled */
10815 			}
10816 		}
10817 	}
10818 
10819 #endif /* CONFIG_I40E_DCB */
10820 	if (!lock_acquired)
10821 		rtnl_lock();
10822 	ret = i40e_setup_pf_switch(pf, reinit, true);
10823 	if (ret)
10824 		goto end_unlock;
10825 
10826 	/* The driver only wants link up/down and module qualification
10827 	 * reports from firmware.  Note the negative logic.
10828 	 */
10829 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10830 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10831 					 I40E_AQ_EVENT_MEDIA_NA |
10832 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10833 	if (ret)
10834 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10835 			 i40e_stat_str(&pf->hw, ret),
10836 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10837 
10838 	/* Rebuild the VSIs and VEBs that existed before reset.
10839 	 * They are still in our local switch element arrays, so only
10840 	 * need to rebuild the switch model in the HW.
10841 	 *
10842 	 * If there were VEBs but the reconstitution failed, we'll try
10843 	 * to recover minimal use by getting the basic PF VSI working.
10844 	 */
10845 	if (vsi->uplink_seid != pf->mac_seid) {
10846 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10847 		/* find the one VEB connected to the MAC, and find orphans */
10848 		for (v = 0; v < I40E_MAX_VEB; v++) {
10849 			if (!pf->veb[v])
10850 				continue;
10851 
10852 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10853 			    pf->veb[v]->uplink_seid == 0) {
10854 				ret = i40e_reconstitute_veb(pf->veb[v]);
10855 
10856 				if (!ret)
10857 					continue;
10858 
10859 				/* If Main VEB failed, we're in deep doodoo,
10860 				 * so give up rebuilding the switch and set up
10861 				 * for minimal rebuild of PF VSI.
10862 				 * If orphan failed, we'll report the error
10863 				 * but try to keep going.
10864 				 */
10865 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10866 					dev_info(&pf->pdev->dev,
10867 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10868 						 ret);
10869 					vsi->uplink_seid = pf->mac_seid;
10870 					break;
10871 				} else if (pf->veb[v]->uplink_seid == 0) {
10872 					dev_info(&pf->pdev->dev,
10873 						 "rebuild of orphan VEB failed: %d\n",
10874 						 ret);
10875 				}
10876 			}
10877 		}
10878 	}
10879 
10880 	if (vsi->uplink_seid == pf->mac_seid) {
10881 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10882 		/* no VEB, so rebuild only the Main VSI */
10883 		ret = i40e_add_vsi(vsi);
10884 		if (ret) {
10885 			dev_info(&pf->pdev->dev,
10886 				 "rebuild of Main VSI failed: %d\n", ret);
10887 			goto end_unlock;
10888 		}
10889 	}
10890 
10891 	if (vsi->mqprio_qopt.max_rate[0]) {
10892 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10893 		u64 credits = 0;
10894 
10895 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10896 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10897 		if (ret)
10898 			goto end_unlock;
10899 
10900 		credits = max_tx_rate;
10901 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10902 		dev_dbg(&vsi->back->pdev->dev,
10903 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10904 			max_tx_rate,
10905 			credits,
10906 			vsi->seid);
10907 	}
10908 
10909 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10910 	if (ret)
10911 		goto end_unlock;
10912 
10913 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10914 	 * for this main VSI if they exist
10915 	 */
10916 	ret = i40e_rebuild_channels(vsi);
10917 	if (ret)
10918 		goto end_unlock;
10919 
10920 	/* Reconfigure hardware for allowing smaller MSS in the case
10921 	 * of TSO, so that we avoid the MDD being fired and causing
10922 	 * a reset in the case of small MSS+TSO.
10923 	 */
10924 #define I40E_REG_MSS          0x000E64DC
10925 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10926 #define I40E_64BYTE_MSS       0x400000
10927 	val = rd32(hw, I40E_REG_MSS);
10928 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10929 		val &= ~I40E_REG_MSS_MIN_MASK;
10930 		val |= I40E_64BYTE_MSS;
10931 		wr32(hw, I40E_REG_MSS, val);
10932 	}
10933 
10934 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10935 		msleep(75);
10936 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10937 		if (ret)
10938 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10939 				 i40e_stat_str(&pf->hw, ret),
10940 				 i40e_aq_str(&pf->hw,
10941 					     pf->hw.aq.asq_last_status));
10942 	}
10943 	/* reinit the misc interrupt */
10944 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10945 		ret = i40e_setup_misc_vector(pf);
10946 
10947 	/* Add a filter to drop all Flow control frames from any VSI from being
10948 	 * transmitted. By doing so we stop a malicious VF from sending out
10949 	 * PAUSE or PFC frames and potentially controlling traffic for other
10950 	 * PF/VF VSIs.
10951 	 * The FW can still send Flow control frames if enabled.
10952 	 */
10953 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10954 						       pf->main_vsi_seid);
10955 
10956 	/* restart the VSIs that were rebuilt and running before the reset */
10957 	i40e_pf_unquiesce_all_vsi(pf);
10958 
10959 	/* Release the RTNL lock before we start resetting VFs */
10960 	if (!lock_acquired)
10961 		rtnl_unlock();
10962 
10963 	/* Restore promiscuous settings */
10964 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10965 	if (ret)
10966 		dev_warn(&pf->pdev->dev,
10967 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10968 			 pf->cur_promisc ? "on" : "off",
10969 			 i40e_stat_str(&pf->hw, ret),
10970 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10971 
10972 	i40e_reset_all_vfs(pf, true);
10973 
10974 	/* tell the firmware that we're starting */
10975 	i40e_send_version(pf);
10976 
10977 	/* We've already released the lock, so don't do it again */
10978 	goto end_core_reset;
10979 
10980 end_unlock:
10981 	if (!lock_acquired)
10982 		rtnl_unlock();
10983 end_core_reset:
10984 	clear_bit(__I40E_RESET_FAILED, pf->state);
10985 clear_recovery:
10986 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10987 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10988 }
10989 
10990 /**
10991  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10992  * @pf: board private structure
10993  * @reinit: if the Main VSI needs to re-initialized.
10994  * @lock_acquired: indicates whether or not the lock has been acquired
10995  * before this function was called.
10996  **/
10997 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10998 				   bool lock_acquired)
10999 {
11000 	int ret;
11001 
11002 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11003 		return;
11004 	/* Now we wait for GRST to settle out.
11005 	 * We don't have to delete the VEBs or VSIs from the hw switch
11006 	 * because the reset will make them disappear.
11007 	 */
11008 	ret = i40e_reset(pf);
11009 	if (!ret)
11010 		i40e_rebuild(pf, reinit, lock_acquired);
11011 }
11012 
11013 /**
11014  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11015  * @pf: board private structure
11016  *
11017  * Close up the VFs and other things in prep for a Core Reset,
11018  * then get ready to rebuild the world.
11019  * @lock_acquired: indicates whether or not the lock has been acquired
11020  * before this function was called.
11021  **/
11022 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11023 {
11024 	i40e_prep_for_reset(pf);
11025 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11026 }
11027 
11028 /**
11029  * i40e_handle_mdd_event
11030  * @pf: pointer to the PF structure
11031  *
11032  * Called from the MDD irq handler to identify possibly malicious vfs
11033  **/
11034 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11035 {
11036 	struct i40e_hw *hw = &pf->hw;
11037 	bool mdd_detected = false;
11038 	struct i40e_vf *vf;
11039 	u32 reg;
11040 	int i;
11041 
11042 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11043 		return;
11044 
11045 	/* find what triggered the MDD event */
11046 	reg = rd32(hw, I40E_GL_MDET_TX);
11047 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11048 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11049 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11050 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11051 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11052 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11053 				I40E_GL_MDET_TX_EVENT_SHIFT;
11054 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11055 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11056 				pf->hw.func_caps.base_queue;
11057 		if (netif_msg_tx_err(pf))
11058 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11059 				 event, queue, pf_num, vf_num);
11060 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11061 		mdd_detected = true;
11062 	}
11063 	reg = rd32(hw, I40E_GL_MDET_RX);
11064 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11065 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11066 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11067 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11068 				I40E_GL_MDET_RX_EVENT_SHIFT;
11069 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11070 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11071 				pf->hw.func_caps.base_queue;
11072 		if (netif_msg_rx_err(pf))
11073 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11074 				 event, queue, func);
11075 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11076 		mdd_detected = true;
11077 	}
11078 
11079 	if (mdd_detected) {
11080 		reg = rd32(hw, I40E_PF_MDET_TX);
11081 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11082 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11083 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11084 		}
11085 		reg = rd32(hw, I40E_PF_MDET_RX);
11086 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11087 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11088 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11089 		}
11090 	}
11091 
11092 	/* see if one of the VFs needs its hand slapped */
11093 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11094 		vf = &(pf->vf[i]);
11095 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11096 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11097 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11098 			vf->num_mdd_events++;
11099 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11100 				 i);
11101 			dev_info(&pf->pdev->dev,
11102 				 "Use PF Control I/F to re-enable the VF\n");
11103 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11104 		}
11105 
11106 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11107 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11108 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11109 			vf->num_mdd_events++;
11110 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11111 				 i);
11112 			dev_info(&pf->pdev->dev,
11113 				 "Use PF Control I/F to re-enable the VF\n");
11114 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11115 		}
11116 	}
11117 
11118 	/* re-enable mdd interrupt cause */
11119 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11120 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11121 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11122 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11123 	i40e_flush(hw);
11124 }
11125 
11126 /**
11127  * i40e_service_task - Run the driver's async subtasks
11128  * @work: pointer to work_struct containing our data
11129  **/
11130 static void i40e_service_task(struct work_struct *work)
11131 {
11132 	struct i40e_pf *pf = container_of(work,
11133 					  struct i40e_pf,
11134 					  service_task);
11135 	unsigned long start_time = jiffies;
11136 
11137 	/* don't bother with service tasks if a reset is in progress */
11138 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11139 	    test_bit(__I40E_SUSPENDED, pf->state))
11140 		return;
11141 
11142 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11143 		return;
11144 
11145 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11146 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11147 		i40e_sync_filters_subtask(pf);
11148 		i40e_reset_subtask(pf);
11149 		i40e_handle_mdd_event(pf);
11150 		i40e_vc_process_vflr_event(pf);
11151 		i40e_watchdog_subtask(pf);
11152 		i40e_fdir_reinit_subtask(pf);
11153 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11154 			/* Client subtask will reopen next time through. */
11155 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11156 							   true);
11157 		} else {
11158 			i40e_client_subtask(pf);
11159 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11160 					       pf->state))
11161 				i40e_notify_client_of_l2_param_changes(
11162 								pf->vsi[pf->lan_vsi]);
11163 		}
11164 		i40e_sync_filters_subtask(pf);
11165 	} else {
11166 		i40e_reset_subtask(pf);
11167 	}
11168 
11169 	i40e_clean_adminq_subtask(pf);
11170 
11171 	/* flush memory to make sure state is correct before next watchdog */
11172 	smp_mb__before_atomic();
11173 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11174 
11175 	/* If the tasks have taken longer than one timer cycle or there
11176 	 * is more work to be done, reschedule the service task now
11177 	 * rather than wait for the timer to tick again.
11178 	 */
11179 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11180 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11181 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11182 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11183 		i40e_service_event_schedule(pf);
11184 }
11185 
11186 /**
11187  * i40e_service_timer - timer callback
11188  * @t: timer list pointer
11189  **/
11190 static void i40e_service_timer(struct timer_list *t)
11191 {
11192 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11193 
11194 	mod_timer(&pf->service_timer,
11195 		  round_jiffies(jiffies + pf->service_timer_period));
11196 	i40e_service_event_schedule(pf);
11197 }
11198 
11199 /**
11200  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11201  * @vsi: the VSI being configured
11202  **/
11203 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11204 {
11205 	struct i40e_pf *pf = vsi->back;
11206 
11207 	switch (vsi->type) {
11208 	case I40E_VSI_MAIN:
11209 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11210 		if (!vsi->num_tx_desc)
11211 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11212 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11213 		if (!vsi->num_rx_desc)
11214 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11215 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11216 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11217 			vsi->num_q_vectors = pf->num_lan_msix;
11218 		else
11219 			vsi->num_q_vectors = 1;
11220 
11221 		break;
11222 
11223 	case I40E_VSI_FDIR:
11224 		vsi->alloc_queue_pairs = 1;
11225 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11226 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11227 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11228 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11229 		vsi->num_q_vectors = pf->num_fdsb_msix;
11230 		break;
11231 
11232 	case I40E_VSI_VMDQ2:
11233 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11234 		if (!vsi->num_tx_desc)
11235 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11236 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11237 		if (!vsi->num_rx_desc)
11238 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11239 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11240 		vsi->num_q_vectors = pf->num_vmdq_msix;
11241 		break;
11242 
11243 	case I40E_VSI_SRIOV:
11244 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11245 		if (!vsi->num_tx_desc)
11246 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11247 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11248 		if (!vsi->num_rx_desc)
11249 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11250 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11251 		break;
11252 
11253 	default:
11254 		WARN_ON(1);
11255 		return -ENODATA;
11256 	}
11257 
11258 	if (is_kdump_kernel()) {
11259 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11260 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11261 	}
11262 
11263 	return 0;
11264 }
11265 
11266 /**
11267  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11268  * @vsi: VSI pointer
11269  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11270  *
11271  * On error: returns error code (negative)
11272  * On success: returns 0
11273  **/
11274 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11275 {
11276 	struct i40e_ring **next_rings;
11277 	int size;
11278 	int ret = 0;
11279 
11280 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11281 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11282 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11283 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11284 	if (!vsi->tx_rings)
11285 		return -ENOMEM;
11286 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11287 	if (i40e_enabled_xdp_vsi(vsi)) {
11288 		vsi->xdp_rings = next_rings;
11289 		next_rings += vsi->alloc_queue_pairs;
11290 	}
11291 	vsi->rx_rings = next_rings;
11292 
11293 	if (alloc_qvectors) {
11294 		/* allocate memory for q_vector pointers */
11295 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11296 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11297 		if (!vsi->q_vectors) {
11298 			ret = -ENOMEM;
11299 			goto err_vectors;
11300 		}
11301 	}
11302 	return ret;
11303 
11304 err_vectors:
11305 	kfree(vsi->tx_rings);
11306 	return ret;
11307 }
11308 
11309 /**
11310  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11311  * @pf: board private structure
11312  * @type: type of VSI
11313  *
11314  * On error: returns error code (negative)
11315  * On success: returns vsi index in PF (positive)
11316  **/
11317 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11318 {
11319 	int ret = -ENODEV;
11320 	struct i40e_vsi *vsi;
11321 	int vsi_idx;
11322 	int i;
11323 
11324 	/* Need to protect the allocation of the VSIs at the PF level */
11325 	mutex_lock(&pf->switch_mutex);
11326 
11327 	/* VSI list may be fragmented if VSI creation/destruction has
11328 	 * been happening.  We can afford to do a quick scan to look
11329 	 * for any free VSIs in the list.
11330 	 *
11331 	 * find next empty vsi slot, looping back around if necessary
11332 	 */
11333 	i = pf->next_vsi;
11334 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11335 		i++;
11336 	if (i >= pf->num_alloc_vsi) {
11337 		i = 0;
11338 		while (i < pf->next_vsi && pf->vsi[i])
11339 			i++;
11340 	}
11341 
11342 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11343 		vsi_idx = i;             /* Found one! */
11344 	} else {
11345 		ret = -ENODEV;
11346 		goto unlock_pf;  /* out of VSI slots! */
11347 	}
11348 	pf->next_vsi = ++i;
11349 
11350 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11351 	if (!vsi) {
11352 		ret = -ENOMEM;
11353 		goto unlock_pf;
11354 	}
11355 	vsi->type = type;
11356 	vsi->back = pf;
11357 	set_bit(__I40E_VSI_DOWN, vsi->state);
11358 	vsi->flags = 0;
11359 	vsi->idx = vsi_idx;
11360 	vsi->int_rate_limit = 0;
11361 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11362 				pf->rss_table_size : 64;
11363 	vsi->netdev_registered = false;
11364 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11365 	hash_init(vsi->mac_filter_hash);
11366 	vsi->irqs_ready = false;
11367 
11368 	if (type == I40E_VSI_MAIN) {
11369 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11370 		if (!vsi->af_xdp_zc_qps)
11371 			goto err_rings;
11372 	}
11373 
11374 	ret = i40e_set_num_rings_in_vsi(vsi);
11375 	if (ret)
11376 		goto err_rings;
11377 
11378 	ret = i40e_vsi_alloc_arrays(vsi, true);
11379 	if (ret)
11380 		goto err_rings;
11381 
11382 	/* Setup default MSIX irq handler for VSI */
11383 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11384 
11385 	/* Initialize VSI lock */
11386 	spin_lock_init(&vsi->mac_filter_hash_lock);
11387 	pf->vsi[vsi_idx] = vsi;
11388 	ret = vsi_idx;
11389 	goto unlock_pf;
11390 
11391 err_rings:
11392 	bitmap_free(vsi->af_xdp_zc_qps);
11393 	pf->next_vsi = i - 1;
11394 	kfree(vsi);
11395 unlock_pf:
11396 	mutex_unlock(&pf->switch_mutex);
11397 	return ret;
11398 }
11399 
11400 /**
11401  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11402  * @vsi: VSI pointer
11403  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11404  *
11405  * On error: returns error code (negative)
11406  * On success: returns 0
11407  **/
11408 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11409 {
11410 	/* free the ring and vector containers */
11411 	if (free_qvectors) {
11412 		kfree(vsi->q_vectors);
11413 		vsi->q_vectors = NULL;
11414 	}
11415 	kfree(vsi->tx_rings);
11416 	vsi->tx_rings = NULL;
11417 	vsi->rx_rings = NULL;
11418 	vsi->xdp_rings = NULL;
11419 }
11420 
11421 /**
11422  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11423  * and lookup table
11424  * @vsi: Pointer to VSI structure
11425  */
11426 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11427 {
11428 	if (!vsi)
11429 		return;
11430 
11431 	kfree(vsi->rss_hkey_user);
11432 	vsi->rss_hkey_user = NULL;
11433 
11434 	kfree(vsi->rss_lut_user);
11435 	vsi->rss_lut_user = NULL;
11436 }
11437 
11438 /**
11439  * i40e_vsi_clear - Deallocate the VSI provided
11440  * @vsi: the VSI being un-configured
11441  **/
11442 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11443 {
11444 	struct i40e_pf *pf;
11445 
11446 	if (!vsi)
11447 		return 0;
11448 
11449 	if (!vsi->back)
11450 		goto free_vsi;
11451 	pf = vsi->back;
11452 
11453 	mutex_lock(&pf->switch_mutex);
11454 	if (!pf->vsi[vsi->idx]) {
11455 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11456 			vsi->idx, vsi->idx, vsi->type);
11457 		goto unlock_vsi;
11458 	}
11459 
11460 	if (pf->vsi[vsi->idx] != vsi) {
11461 		dev_err(&pf->pdev->dev,
11462 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11463 			pf->vsi[vsi->idx]->idx,
11464 			pf->vsi[vsi->idx]->type,
11465 			vsi->idx, vsi->type);
11466 		goto unlock_vsi;
11467 	}
11468 
11469 	/* updates the PF for this cleared vsi */
11470 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11471 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11472 
11473 	bitmap_free(vsi->af_xdp_zc_qps);
11474 	i40e_vsi_free_arrays(vsi, true);
11475 	i40e_clear_rss_config_user(vsi);
11476 
11477 	pf->vsi[vsi->idx] = NULL;
11478 	if (vsi->idx < pf->next_vsi)
11479 		pf->next_vsi = vsi->idx;
11480 
11481 unlock_vsi:
11482 	mutex_unlock(&pf->switch_mutex);
11483 free_vsi:
11484 	kfree(vsi);
11485 
11486 	return 0;
11487 }
11488 
11489 /**
11490  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11491  * @vsi: the VSI being cleaned
11492  **/
11493 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11494 {
11495 	int i;
11496 
11497 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11498 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11499 			kfree_rcu(vsi->tx_rings[i], rcu);
11500 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11501 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11502 			if (vsi->xdp_rings)
11503 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11504 		}
11505 	}
11506 }
11507 
11508 /**
11509  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11510  * @vsi: the VSI being configured
11511  **/
11512 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11513 {
11514 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11515 	struct i40e_pf *pf = vsi->back;
11516 	struct i40e_ring *ring;
11517 
11518 	/* Set basic values in the rings to be used later during open() */
11519 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11520 		/* allocate space for both Tx and Rx in one shot */
11521 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11522 		if (!ring)
11523 			goto err_out;
11524 
11525 		ring->queue_index = i;
11526 		ring->reg_idx = vsi->base_queue + i;
11527 		ring->ring_active = false;
11528 		ring->vsi = vsi;
11529 		ring->netdev = vsi->netdev;
11530 		ring->dev = &pf->pdev->dev;
11531 		ring->count = vsi->num_tx_desc;
11532 		ring->size = 0;
11533 		ring->dcb_tc = 0;
11534 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11535 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11536 		ring->itr_setting = pf->tx_itr_default;
11537 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11538 
11539 		if (!i40e_enabled_xdp_vsi(vsi))
11540 			goto setup_rx;
11541 
11542 		ring->queue_index = vsi->alloc_queue_pairs + i;
11543 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11544 		ring->ring_active = false;
11545 		ring->vsi = vsi;
11546 		ring->netdev = NULL;
11547 		ring->dev = &pf->pdev->dev;
11548 		ring->count = vsi->num_tx_desc;
11549 		ring->size = 0;
11550 		ring->dcb_tc = 0;
11551 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11552 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11553 		set_ring_xdp(ring);
11554 		ring->itr_setting = pf->tx_itr_default;
11555 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11556 
11557 setup_rx:
11558 		ring->queue_index = i;
11559 		ring->reg_idx = vsi->base_queue + i;
11560 		ring->ring_active = false;
11561 		ring->vsi = vsi;
11562 		ring->netdev = vsi->netdev;
11563 		ring->dev = &pf->pdev->dev;
11564 		ring->count = vsi->num_rx_desc;
11565 		ring->size = 0;
11566 		ring->dcb_tc = 0;
11567 		ring->itr_setting = pf->rx_itr_default;
11568 		WRITE_ONCE(vsi->rx_rings[i], ring);
11569 	}
11570 
11571 	return 0;
11572 
11573 err_out:
11574 	i40e_vsi_clear_rings(vsi);
11575 	return -ENOMEM;
11576 }
11577 
11578 /**
11579  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11580  * @pf: board private structure
11581  * @vectors: the number of MSI-X vectors to request
11582  *
11583  * Returns the number of vectors reserved, or error
11584  **/
11585 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11586 {
11587 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11588 					I40E_MIN_MSIX, vectors);
11589 	if (vectors < 0) {
11590 		dev_info(&pf->pdev->dev,
11591 			 "MSI-X vector reservation failed: %d\n", vectors);
11592 		vectors = 0;
11593 	}
11594 
11595 	return vectors;
11596 }
11597 
11598 /**
11599  * i40e_init_msix - Setup the MSIX capability
11600  * @pf: board private structure
11601  *
11602  * Work with the OS to set up the MSIX vectors needed.
11603  *
11604  * Returns the number of vectors reserved or negative on failure
11605  **/
11606 static int i40e_init_msix(struct i40e_pf *pf)
11607 {
11608 	struct i40e_hw *hw = &pf->hw;
11609 	int cpus, extra_vectors;
11610 	int vectors_left;
11611 	int v_budget, i;
11612 	int v_actual;
11613 	int iwarp_requested = 0;
11614 
11615 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11616 		return -ENODEV;
11617 
11618 	/* The number of vectors we'll request will be comprised of:
11619 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11620 	 *   - The number of LAN queue pairs
11621 	 *	- Queues being used for RSS.
11622 	 *		We don't need as many as max_rss_size vectors.
11623 	 *		use rss_size instead in the calculation since that
11624 	 *		is governed by number of cpus in the system.
11625 	 *	- assumes symmetric Tx/Rx pairing
11626 	 *   - The number of VMDq pairs
11627 	 *   - The CPU count within the NUMA node if iWARP is enabled
11628 	 * Once we count this up, try the request.
11629 	 *
11630 	 * If we can't get what we want, we'll simplify to nearly nothing
11631 	 * and try again.  If that still fails, we punt.
11632 	 */
11633 	vectors_left = hw->func_caps.num_msix_vectors;
11634 	v_budget = 0;
11635 
11636 	/* reserve one vector for miscellaneous handler */
11637 	if (vectors_left) {
11638 		v_budget++;
11639 		vectors_left--;
11640 	}
11641 
11642 	/* reserve some vectors for the main PF traffic queues. Initially we
11643 	 * only reserve at most 50% of the available vectors, in the case that
11644 	 * the number of online CPUs is large. This ensures that we can enable
11645 	 * extra features as well. Once we've enabled the other features, we
11646 	 * will use any remaining vectors to reach as close as we can to the
11647 	 * number of online CPUs.
11648 	 */
11649 	cpus = num_online_cpus();
11650 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11651 	vectors_left -= pf->num_lan_msix;
11652 
11653 	/* reserve one vector for sideband flow director */
11654 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11655 		if (vectors_left) {
11656 			pf->num_fdsb_msix = 1;
11657 			v_budget++;
11658 			vectors_left--;
11659 		} else {
11660 			pf->num_fdsb_msix = 0;
11661 		}
11662 	}
11663 
11664 	/* can we reserve enough for iWARP? */
11665 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11666 		iwarp_requested = pf->num_iwarp_msix;
11667 
11668 		if (!vectors_left)
11669 			pf->num_iwarp_msix = 0;
11670 		else if (vectors_left < pf->num_iwarp_msix)
11671 			pf->num_iwarp_msix = 1;
11672 		v_budget += pf->num_iwarp_msix;
11673 		vectors_left -= pf->num_iwarp_msix;
11674 	}
11675 
11676 	/* any vectors left over go for VMDq support */
11677 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11678 		if (!vectors_left) {
11679 			pf->num_vmdq_msix = 0;
11680 			pf->num_vmdq_qps = 0;
11681 		} else {
11682 			int vmdq_vecs_wanted =
11683 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11684 			int vmdq_vecs =
11685 				min_t(int, vectors_left, vmdq_vecs_wanted);
11686 
11687 			/* if we're short on vectors for what's desired, we limit
11688 			 * the queues per vmdq.  If this is still more than are
11689 			 * available, the user will need to change the number of
11690 			 * queues/vectors used by the PF later with the ethtool
11691 			 * channels command
11692 			 */
11693 			if (vectors_left < vmdq_vecs_wanted) {
11694 				pf->num_vmdq_qps = 1;
11695 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11696 				vmdq_vecs = min_t(int,
11697 						  vectors_left,
11698 						  vmdq_vecs_wanted);
11699 			}
11700 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11701 
11702 			v_budget += vmdq_vecs;
11703 			vectors_left -= vmdq_vecs;
11704 		}
11705 	}
11706 
11707 	/* On systems with a large number of SMP cores, we previously limited
11708 	 * the number of vectors for num_lan_msix to be at most 50% of the
11709 	 * available vectors, to allow for other features. Now, we add back
11710 	 * the remaining vectors. However, we ensure that the total
11711 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11712 	 * calculate the number of vectors we can add without going over the
11713 	 * cap of CPUs. For systems with a small number of CPUs this will be
11714 	 * zero.
11715 	 */
11716 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11717 	pf->num_lan_msix += extra_vectors;
11718 	vectors_left -= extra_vectors;
11719 
11720 	WARN(vectors_left < 0,
11721 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11722 
11723 	v_budget += pf->num_lan_msix;
11724 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11725 				   GFP_KERNEL);
11726 	if (!pf->msix_entries)
11727 		return -ENOMEM;
11728 
11729 	for (i = 0; i < v_budget; i++)
11730 		pf->msix_entries[i].entry = i;
11731 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11732 
11733 	if (v_actual < I40E_MIN_MSIX) {
11734 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11735 		kfree(pf->msix_entries);
11736 		pf->msix_entries = NULL;
11737 		pci_disable_msix(pf->pdev);
11738 		return -ENODEV;
11739 
11740 	} else if (v_actual == I40E_MIN_MSIX) {
11741 		/* Adjust for minimal MSIX use */
11742 		pf->num_vmdq_vsis = 0;
11743 		pf->num_vmdq_qps = 0;
11744 		pf->num_lan_qps = 1;
11745 		pf->num_lan_msix = 1;
11746 
11747 	} else if (v_actual != v_budget) {
11748 		/* If we have limited resources, we will start with no vectors
11749 		 * for the special features and then allocate vectors to some
11750 		 * of these features based on the policy and at the end disable
11751 		 * the features that did not get any vectors.
11752 		 */
11753 		int vec;
11754 
11755 		dev_info(&pf->pdev->dev,
11756 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11757 			 v_actual, v_budget);
11758 		/* reserve the misc vector */
11759 		vec = v_actual - 1;
11760 
11761 		/* Scale vector usage down */
11762 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11763 		pf->num_vmdq_vsis = 1;
11764 		pf->num_vmdq_qps = 1;
11765 
11766 		/* partition out the remaining vectors */
11767 		switch (vec) {
11768 		case 2:
11769 			pf->num_lan_msix = 1;
11770 			break;
11771 		case 3:
11772 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11773 				pf->num_lan_msix = 1;
11774 				pf->num_iwarp_msix = 1;
11775 			} else {
11776 				pf->num_lan_msix = 2;
11777 			}
11778 			break;
11779 		default:
11780 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11781 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11782 						 iwarp_requested);
11783 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11784 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11785 			} else {
11786 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11787 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11788 			}
11789 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11790 				pf->num_fdsb_msix = 1;
11791 				vec--;
11792 			}
11793 			pf->num_lan_msix = min_t(int,
11794 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11795 							      pf->num_lan_msix);
11796 			pf->num_lan_qps = pf->num_lan_msix;
11797 			break;
11798 		}
11799 	}
11800 
11801 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11802 	    (pf->num_fdsb_msix == 0)) {
11803 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11804 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11805 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11806 	}
11807 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11808 	    (pf->num_vmdq_msix == 0)) {
11809 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11810 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11811 	}
11812 
11813 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11814 	    (pf->num_iwarp_msix == 0)) {
11815 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11816 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11817 	}
11818 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11819 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11820 		   pf->num_lan_msix,
11821 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11822 		   pf->num_fdsb_msix,
11823 		   pf->num_iwarp_msix);
11824 
11825 	return v_actual;
11826 }
11827 
11828 /**
11829  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11830  * @vsi: the VSI being configured
11831  * @v_idx: index of the vector in the vsi struct
11832  *
11833  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11834  **/
11835 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11836 {
11837 	struct i40e_q_vector *q_vector;
11838 
11839 	/* allocate q_vector */
11840 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11841 	if (!q_vector)
11842 		return -ENOMEM;
11843 
11844 	q_vector->vsi = vsi;
11845 	q_vector->v_idx = v_idx;
11846 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11847 
11848 	if (vsi->netdev)
11849 		netif_napi_add(vsi->netdev, &q_vector->napi,
11850 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11851 
11852 	/* tie q_vector and vsi together */
11853 	vsi->q_vectors[v_idx] = q_vector;
11854 
11855 	return 0;
11856 }
11857 
11858 /**
11859  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11860  * @vsi: the VSI being configured
11861  *
11862  * We allocate one q_vector per queue interrupt.  If allocation fails we
11863  * return -ENOMEM.
11864  **/
11865 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11866 {
11867 	struct i40e_pf *pf = vsi->back;
11868 	int err, v_idx, num_q_vectors;
11869 
11870 	/* if not MSIX, give the one vector only to the LAN VSI */
11871 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11872 		num_q_vectors = vsi->num_q_vectors;
11873 	else if (vsi == pf->vsi[pf->lan_vsi])
11874 		num_q_vectors = 1;
11875 	else
11876 		return -EINVAL;
11877 
11878 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11879 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11880 		if (err)
11881 			goto err_out;
11882 	}
11883 
11884 	return 0;
11885 
11886 err_out:
11887 	while (v_idx--)
11888 		i40e_free_q_vector(vsi, v_idx);
11889 
11890 	return err;
11891 }
11892 
11893 /**
11894  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11895  * @pf: board private structure to initialize
11896  **/
11897 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11898 {
11899 	int vectors = 0;
11900 	ssize_t size;
11901 
11902 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11903 		vectors = i40e_init_msix(pf);
11904 		if (vectors < 0) {
11905 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11906 				       I40E_FLAG_IWARP_ENABLED	|
11907 				       I40E_FLAG_RSS_ENABLED	|
11908 				       I40E_FLAG_DCB_CAPABLE	|
11909 				       I40E_FLAG_DCB_ENABLED	|
11910 				       I40E_FLAG_SRIOV_ENABLED	|
11911 				       I40E_FLAG_FD_SB_ENABLED	|
11912 				       I40E_FLAG_FD_ATR_ENABLED	|
11913 				       I40E_FLAG_VMDQ_ENABLED);
11914 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11915 
11916 			/* rework the queue expectations without MSIX */
11917 			i40e_determine_queue_usage(pf);
11918 		}
11919 	}
11920 
11921 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11922 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11923 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11924 		vectors = pci_enable_msi(pf->pdev);
11925 		if (vectors < 0) {
11926 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11927 				 vectors);
11928 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11929 		}
11930 		vectors = 1;  /* one MSI or Legacy vector */
11931 	}
11932 
11933 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11934 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11935 
11936 	/* set up vector assignment tracking */
11937 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11938 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11939 	if (!pf->irq_pile)
11940 		return -ENOMEM;
11941 
11942 	pf->irq_pile->num_entries = vectors;
11943 
11944 	/* track first vector for misc interrupts, ignore return */
11945 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11946 
11947 	return 0;
11948 }
11949 
11950 /**
11951  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11952  * @pf: private board data structure
11953  *
11954  * Restore the interrupt scheme that was cleared when we suspended the
11955  * device. This should be called during resume to re-allocate the q_vectors
11956  * and reacquire IRQs.
11957  */
11958 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11959 {
11960 	int err, i;
11961 
11962 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11963 	 * scheme. We need to re-enabled them here in order to attempt to
11964 	 * re-acquire the MSI or MSI-X vectors
11965 	 */
11966 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11967 
11968 	err = i40e_init_interrupt_scheme(pf);
11969 	if (err)
11970 		return err;
11971 
11972 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11973 	 * rings together again.
11974 	 */
11975 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11976 		if (pf->vsi[i]) {
11977 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11978 			if (err)
11979 				goto err_unwind;
11980 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11981 		}
11982 	}
11983 
11984 	err = i40e_setup_misc_vector(pf);
11985 	if (err)
11986 		goto err_unwind;
11987 
11988 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11989 		i40e_client_update_msix_info(pf);
11990 
11991 	return 0;
11992 
11993 err_unwind:
11994 	while (i--) {
11995 		if (pf->vsi[i])
11996 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11997 	}
11998 
11999 	return err;
12000 }
12001 
12002 /**
12003  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12004  * non queue events in recovery mode
12005  * @pf: board private structure
12006  *
12007  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12008  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12009  * This is handled differently than in recovery mode since no Tx/Rx resources
12010  * are being allocated.
12011  **/
12012 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12013 {
12014 	int err;
12015 
12016 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12017 		err = i40e_setup_misc_vector(pf);
12018 
12019 		if (err) {
12020 			dev_info(&pf->pdev->dev,
12021 				 "MSI-X misc vector request failed, error %d\n",
12022 				 err);
12023 			return err;
12024 		}
12025 	} else {
12026 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12027 
12028 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12029 				  pf->int_name, pf);
12030 
12031 		if (err) {
12032 			dev_info(&pf->pdev->dev,
12033 				 "MSI/legacy misc vector request failed, error %d\n",
12034 				 err);
12035 			return err;
12036 		}
12037 		i40e_enable_misc_int_causes(pf);
12038 		i40e_irq_dynamic_enable_icr0(pf);
12039 	}
12040 
12041 	return 0;
12042 }
12043 
12044 /**
12045  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12046  * @pf: board private structure
12047  *
12048  * This sets up the handler for MSIX 0, which is used to manage the
12049  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12050  * when in MSI or Legacy interrupt mode.
12051  **/
12052 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12053 {
12054 	struct i40e_hw *hw = &pf->hw;
12055 	int err = 0;
12056 
12057 	/* Only request the IRQ once, the first time through. */
12058 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12059 		err = request_irq(pf->msix_entries[0].vector,
12060 				  i40e_intr, 0, pf->int_name, pf);
12061 		if (err) {
12062 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12063 			dev_info(&pf->pdev->dev,
12064 				 "request_irq for %s failed: %d\n",
12065 				 pf->int_name, err);
12066 			return -EFAULT;
12067 		}
12068 	}
12069 
12070 	i40e_enable_misc_int_causes(pf);
12071 
12072 	/* associate no queues to the misc vector */
12073 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12074 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12075 
12076 	i40e_flush(hw);
12077 
12078 	i40e_irq_dynamic_enable_icr0(pf);
12079 
12080 	return err;
12081 }
12082 
12083 /**
12084  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12085  * @vsi: Pointer to vsi structure
12086  * @seed: Buffter to store the hash keys
12087  * @lut: Buffer to store the lookup table entries
12088  * @lut_size: Size of buffer to store the lookup table entries
12089  *
12090  * Return 0 on success, negative on failure
12091  */
12092 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12093 			   u8 *lut, u16 lut_size)
12094 {
12095 	struct i40e_pf *pf = vsi->back;
12096 	struct i40e_hw *hw = &pf->hw;
12097 	int ret = 0;
12098 
12099 	if (seed) {
12100 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12101 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12102 		if (ret) {
12103 			dev_info(&pf->pdev->dev,
12104 				 "Cannot get RSS key, err %s aq_err %s\n",
12105 				 i40e_stat_str(&pf->hw, ret),
12106 				 i40e_aq_str(&pf->hw,
12107 					     pf->hw.aq.asq_last_status));
12108 			return ret;
12109 		}
12110 	}
12111 
12112 	if (lut) {
12113 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12114 
12115 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12116 		if (ret) {
12117 			dev_info(&pf->pdev->dev,
12118 				 "Cannot get RSS lut, err %s aq_err %s\n",
12119 				 i40e_stat_str(&pf->hw, ret),
12120 				 i40e_aq_str(&pf->hw,
12121 					     pf->hw.aq.asq_last_status));
12122 			return ret;
12123 		}
12124 	}
12125 
12126 	return ret;
12127 }
12128 
12129 /**
12130  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12131  * @vsi: Pointer to vsi structure
12132  * @seed: RSS hash seed
12133  * @lut: Lookup table
12134  * @lut_size: Lookup table size
12135  *
12136  * Returns 0 on success, negative on failure
12137  **/
12138 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12139 			       const u8 *lut, u16 lut_size)
12140 {
12141 	struct i40e_pf *pf = vsi->back;
12142 	struct i40e_hw *hw = &pf->hw;
12143 	u16 vf_id = vsi->vf_id;
12144 	u8 i;
12145 
12146 	/* Fill out hash function seed */
12147 	if (seed) {
12148 		u32 *seed_dw = (u32 *)seed;
12149 
12150 		if (vsi->type == I40E_VSI_MAIN) {
12151 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12152 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12153 		} else if (vsi->type == I40E_VSI_SRIOV) {
12154 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12155 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12156 		} else {
12157 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12158 		}
12159 	}
12160 
12161 	if (lut) {
12162 		u32 *lut_dw = (u32 *)lut;
12163 
12164 		if (vsi->type == I40E_VSI_MAIN) {
12165 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12166 				return -EINVAL;
12167 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12168 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12169 		} else if (vsi->type == I40E_VSI_SRIOV) {
12170 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12171 				return -EINVAL;
12172 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12173 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12174 		} else {
12175 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12176 		}
12177 	}
12178 	i40e_flush(hw);
12179 
12180 	return 0;
12181 }
12182 
12183 /**
12184  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12185  * @vsi: Pointer to VSI structure
12186  * @seed: Buffer to store the keys
12187  * @lut: Buffer to store the lookup table entries
12188  * @lut_size: Size of buffer to store the lookup table entries
12189  *
12190  * Returns 0 on success, negative on failure
12191  */
12192 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12193 			    u8 *lut, u16 lut_size)
12194 {
12195 	struct i40e_pf *pf = vsi->back;
12196 	struct i40e_hw *hw = &pf->hw;
12197 	u16 i;
12198 
12199 	if (seed) {
12200 		u32 *seed_dw = (u32 *)seed;
12201 
12202 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12203 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12204 	}
12205 	if (lut) {
12206 		u32 *lut_dw = (u32 *)lut;
12207 
12208 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12209 			return -EINVAL;
12210 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12211 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12212 	}
12213 
12214 	return 0;
12215 }
12216 
12217 /**
12218  * i40e_config_rss - Configure RSS keys and lut
12219  * @vsi: Pointer to VSI structure
12220  * @seed: RSS hash seed
12221  * @lut: Lookup table
12222  * @lut_size: Lookup table size
12223  *
12224  * Returns 0 on success, negative on failure
12225  */
12226 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12227 {
12228 	struct i40e_pf *pf = vsi->back;
12229 
12230 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12231 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12232 	else
12233 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12234 }
12235 
12236 /**
12237  * i40e_get_rss - Get RSS keys and lut
12238  * @vsi: Pointer to VSI structure
12239  * @seed: Buffer to store the keys
12240  * @lut: Buffer to store the lookup table entries
12241  * @lut_size: Size of buffer to store the lookup table entries
12242  *
12243  * Returns 0 on success, negative on failure
12244  */
12245 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12246 {
12247 	struct i40e_pf *pf = vsi->back;
12248 
12249 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12250 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12251 	else
12252 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12253 }
12254 
12255 /**
12256  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12257  * @pf: Pointer to board private structure
12258  * @lut: Lookup table
12259  * @rss_table_size: Lookup table size
12260  * @rss_size: Range of queue number for hashing
12261  */
12262 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12263 		       u16 rss_table_size, u16 rss_size)
12264 {
12265 	u16 i;
12266 
12267 	for (i = 0; i < rss_table_size; i++)
12268 		lut[i] = i % rss_size;
12269 }
12270 
12271 /**
12272  * i40e_pf_config_rss - Prepare for RSS if used
12273  * @pf: board private structure
12274  **/
12275 static int i40e_pf_config_rss(struct i40e_pf *pf)
12276 {
12277 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12278 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12279 	u8 *lut;
12280 	struct i40e_hw *hw = &pf->hw;
12281 	u32 reg_val;
12282 	u64 hena;
12283 	int ret;
12284 
12285 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12286 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12287 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12288 	hena |= i40e_pf_get_default_rss_hena(pf);
12289 
12290 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12291 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12292 
12293 	/* Determine the RSS table size based on the hardware capabilities */
12294 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12295 	reg_val = (pf->rss_table_size == 512) ?
12296 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12297 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12298 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12299 
12300 	/* Determine the RSS size of the VSI */
12301 	if (!vsi->rss_size) {
12302 		u16 qcount;
12303 		/* If the firmware does something weird during VSI init, we
12304 		 * could end up with zero TCs. Check for that to avoid
12305 		 * divide-by-zero. It probably won't pass traffic, but it also
12306 		 * won't panic.
12307 		 */
12308 		qcount = vsi->num_queue_pairs /
12309 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12310 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12311 	}
12312 	if (!vsi->rss_size)
12313 		return -EINVAL;
12314 
12315 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12316 	if (!lut)
12317 		return -ENOMEM;
12318 
12319 	/* Use user configured lut if there is one, otherwise use default */
12320 	if (vsi->rss_lut_user)
12321 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12322 	else
12323 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12324 
12325 	/* Use user configured hash key if there is one, otherwise
12326 	 * use default.
12327 	 */
12328 	if (vsi->rss_hkey_user)
12329 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12330 	else
12331 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12332 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12333 	kfree(lut);
12334 
12335 	return ret;
12336 }
12337 
12338 /**
12339  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12340  * @pf: board private structure
12341  * @queue_count: the requested queue count for rss.
12342  *
12343  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12344  * count which may be different from the requested queue count.
12345  * Note: expects to be called while under rtnl_lock()
12346  **/
12347 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12348 {
12349 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12350 	int new_rss_size;
12351 
12352 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12353 		return 0;
12354 
12355 	queue_count = min_t(int, queue_count, num_online_cpus());
12356 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12357 
12358 	if (queue_count != vsi->num_queue_pairs) {
12359 		u16 qcount;
12360 
12361 		vsi->req_queue_pairs = queue_count;
12362 		i40e_prep_for_reset(pf);
12363 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12364 			return pf->alloc_rss_size;
12365 
12366 		pf->alloc_rss_size = new_rss_size;
12367 
12368 		i40e_reset_and_rebuild(pf, true, true);
12369 
12370 		/* Discard the user configured hash keys and lut, if less
12371 		 * queues are enabled.
12372 		 */
12373 		if (queue_count < vsi->rss_size) {
12374 			i40e_clear_rss_config_user(vsi);
12375 			dev_dbg(&pf->pdev->dev,
12376 				"discard user configured hash keys and lut\n");
12377 		}
12378 
12379 		/* Reset vsi->rss_size, as number of enabled queues changed */
12380 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12381 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12382 
12383 		i40e_pf_config_rss(pf);
12384 	}
12385 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12386 		 vsi->req_queue_pairs, pf->rss_size_max);
12387 	return pf->alloc_rss_size;
12388 }
12389 
12390 /**
12391  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12392  * @pf: board private structure
12393  **/
12394 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12395 {
12396 	i40e_status status;
12397 	bool min_valid, max_valid;
12398 	u32 max_bw, min_bw;
12399 
12400 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12401 					   &min_valid, &max_valid);
12402 
12403 	if (!status) {
12404 		if (min_valid)
12405 			pf->min_bw = min_bw;
12406 		if (max_valid)
12407 			pf->max_bw = max_bw;
12408 	}
12409 
12410 	return status;
12411 }
12412 
12413 /**
12414  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12415  * @pf: board private structure
12416  **/
12417 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12418 {
12419 	struct i40e_aqc_configure_partition_bw_data bw_data;
12420 	i40e_status status;
12421 
12422 	memset(&bw_data, 0, sizeof(bw_data));
12423 
12424 	/* Set the valid bit for this PF */
12425 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12426 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12427 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12428 
12429 	/* Set the new bandwidths */
12430 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12431 
12432 	return status;
12433 }
12434 
12435 /**
12436  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12437  * @pf: board private structure
12438  **/
12439 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12440 {
12441 	/* Commit temporary BW setting to permanent NVM image */
12442 	enum i40e_admin_queue_err last_aq_status;
12443 	i40e_status ret;
12444 	u16 nvm_word;
12445 
12446 	if (pf->hw.partition_id != 1) {
12447 		dev_info(&pf->pdev->dev,
12448 			 "Commit BW only works on partition 1! This is partition %d",
12449 			 pf->hw.partition_id);
12450 		ret = I40E_NOT_SUPPORTED;
12451 		goto bw_commit_out;
12452 	}
12453 
12454 	/* Acquire NVM for read access */
12455 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12456 	last_aq_status = pf->hw.aq.asq_last_status;
12457 	if (ret) {
12458 		dev_info(&pf->pdev->dev,
12459 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12460 			 i40e_stat_str(&pf->hw, ret),
12461 			 i40e_aq_str(&pf->hw, last_aq_status));
12462 		goto bw_commit_out;
12463 	}
12464 
12465 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12466 	ret = i40e_aq_read_nvm(&pf->hw,
12467 			       I40E_SR_NVM_CONTROL_WORD,
12468 			       0x10, sizeof(nvm_word), &nvm_word,
12469 			       false, NULL);
12470 	/* Save off last admin queue command status before releasing
12471 	 * the NVM
12472 	 */
12473 	last_aq_status = pf->hw.aq.asq_last_status;
12474 	i40e_release_nvm(&pf->hw);
12475 	if (ret) {
12476 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12477 			 i40e_stat_str(&pf->hw, ret),
12478 			 i40e_aq_str(&pf->hw, last_aq_status));
12479 		goto bw_commit_out;
12480 	}
12481 
12482 	/* Wait a bit for NVM release to complete */
12483 	msleep(50);
12484 
12485 	/* Acquire NVM for write access */
12486 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12487 	last_aq_status = pf->hw.aq.asq_last_status;
12488 	if (ret) {
12489 		dev_info(&pf->pdev->dev,
12490 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12491 			 i40e_stat_str(&pf->hw, ret),
12492 			 i40e_aq_str(&pf->hw, last_aq_status));
12493 		goto bw_commit_out;
12494 	}
12495 	/* Write it back out unchanged to initiate update NVM,
12496 	 * which will force a write of the shadow (alt) RAM to
12497 	 * the NVM - thus storing the bandwidth values permanently.
12498 	 */
12499 	ret = i40e_aq_update_nvm(&pf->hw,
12500 				 I40E_SR_NVM_CONTROL_WORD,
12501 				 0x10, sizeof(nvm_word),
12502 				 &nvm_word, true, 0, NULL);
12503 	/* Save off last admin queue command status before releasing
12504 	 * the NVM
12505 	 */
12506 	last_aq_status = pf->hw.aq.asq_last_status;
12507 	i40e_release_nvm(&pf->hw);
12508 	if (ret)
12509 		dev_info(&pf->pdev->dev,
12510 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12511 			 i40e_stat_str(&pf->hw, ret),
12512 			 i40e_aq_str(&pf->hw, last_aq_status));
12513 bw_commit_out:
12514 
12515 	return ret;
12516 }
12517 
12518 /**
12519  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12520  * if total port shutdown feature is enabled for this PF
12521  * @pf: board private structure
12522  **/
12523 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12524 {
12525 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12526 #define I40E_FEATURES_ENABLE_PTR		0x2A
12527 #define I40E_CURRENT_SETTING_PTR		0x2B
12528 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12529 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12530 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12531 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12532 	i40e_status read_status = I40E_SUCCESS;
12533 	u16 sr_emp_sr_settings_ptr = 0;
12534 	u16 features_enable = 0;
12535 	u16 link_behavior = 0;
12536 	bool ret = false;
12537 
12538 	read_status = i40e_read_nvm_word(&pf->hw,
12539 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12540 					 &sr_emp_sr_settings_ptr);
12541 	if (read_status)
12542 		goto err_nvm;
12543 	read_status = i40e_read_nvm_word(&pf->hw,
12544 					 sr_emp_sr_settings_ptr +
12545 					 I40E_FEATURES_ENABLE_PTR,
12546 					 &features_enable);
12547 	if (read_status)
12548 		goto err_nvm;
12549 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12550 		read_status = i40e_read_nvm_module_data(&pf->hw,
12551 							I40E_SR_EMP_SR_SETTINGS_PTR,
12552 							I40E_CURRENT_SETTING_PTR,
12553 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12554 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12555 							&link_behavior);
12556 		if (read_status)
12557 			goto err_nvm;
12558 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12559 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12560 	}
12561 	return ret;
12562 
12563 err_nvm:
12564 	dev_warn(&pf->pdev->dev,
12565 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12566 		 i40e_stat_str(&pf->hw, read_status));
12567 	return ret;
12568 }
12569 
12570 /**
12571  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12572  * @pf: board private structure to initialize
12573  *
12574  * i40e_sw_init initializes the Adapter private data structure.
12575  * Fields are initialized based on PCI device information and
12576  * OS network device settings (MTU size).
12577  **/
12578 static int i40e_sw_init(struct i40e_pf *pf)
12579 {
12580 	int err = 0;
12581 	int size;
12582 	u16 pow;
12583 
12584 	/* Set default capability flags */
12585 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12586 		    I40E_FLAG_MSI_ENABLED     |
12587 		    I40E_FLAG_MSIX_ENABLED;
12588 
12589 	/* Set default ITR */
12590 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12591 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12592 
12593 	/* Depending on PF configurations, it is possible that the RSS
12594 	 * maximum might end up larger than the available queues
12595 	 */
12596 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12597 	pf->alloc_rss_size = 1;
12598 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12599 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12600 				 pf->hw.func_caps.num_tx_qp);
12601 
12602 	/* find the next higher power-of-2 of num cpus */
12603 	pow = roundup_pow_of_two(num_online_cpus());
12604 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12605 
12606 	if (pf->hw.func_caps.rss) {
12607 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12608 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12609 					   num_online_cpus());
12610 	}
12611 
12612 	/* MFP mode enabled */
12613 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12614 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12615 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12616 		if (i40e_get_partition_bw_setting(pf)) {
12617 			dev_warn(&pf->pdev->dev,
12618 				 "Could not get partition bw settings\n");
12619 		} else {
12620 			dev_info(&pf->pdev->dev,
12621 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12622 				 pf->min_bw, pf->max_bw);
12623 
12624 			/* nudge the Tx scheduler */
12625 			i40e_set_partition_bw_setting(pf);
12626 		}
12627 	}
12628 
12629 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12630 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12631 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12632 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12633 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12634 		    pf->hw.num_partitions > 1)
12635 			dev_info(&pf->pdev->dev,
12636 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12637 		else
12638 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12639 		pf->fdir_pf_filter_count =
12640 				 pf->hw.func_caps.fd_filters_guaranteed;
12641 		pf->hw.fdir_shared_filter_count =
12642 				 pf->hw.func_caps.fd_filters_best_effort;
12643 	}
12644 
12645 	if (pf->hw.mac.type == I40E_MAC_X722) {
12646 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12647 				    I40E_HW_128_QP_RSS_CAPABLE |
12648 				    I40E_HW_ATR_EVICT_CAPABLE |
12649 				    I40E_HW_WB_ON_ITR_CAPABLE |
12650 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12651 				    I40E_HW_NO_PCI_LINK_CHECK |
12652 				    I40E_HW_USE_SET_LLDP_MIB |
12653 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12654 				    I40E_HW_PTP_L4_CAPABLE |
12655 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12656 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12657 
12658 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12659 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12660 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12661 			dev_warn(&pf->pdev->dev,
12662 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12663 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12664 		}
12665 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12666 		   ((pf->hw.aq.api_maj_ver == 1) &&
12667 		    (pf->hw.aq.api_min_ver > 4))) {
12668 		/* Supported in FW API version higher than 1.4 */
12669 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12670 	}
12671 
12672 	/* Enable HW ATR eviction if possible */
12673 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12674 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12675 
12676 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12677 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12678 	    (pf->hw.aq.fw_maj_ver < 4))) {
12679 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12680 		/* No DCB support  for FW < v4.33 */
12681 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12682 	}
12683 
12684 	/* Disable FW LLDP if FW < v4.3 */
12685 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12686 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12687 	    (pf->hw.aq.fw_maj_ver < 4)))
12688 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12689 
12690 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12691 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12692 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12693 	    (pf->hw.aq.fw_maj_ver >= 5)))
12694 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12695 
12696 	/* Enable PTP L4 if FW > v6.0 */
12697 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12698 	    pf->hw.aq.fw_maj_ver >= 6)
12699 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12700 
12701 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12702 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12703 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12704 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12705 	}
12706 
12707 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12708 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12709 		/* IWARP needs one extra vector for CQP just like MISC.*/
12710 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12711 	}
12712 	/* Stopping FW LLDP engine is supported on XL710 and X722
12713 	 * starting from FW versions determined in i40e_init_adminq.
12714 	 * Stopping the FW LLDP engine is not supported on XL710
12715 	 * if NPAR is functioning so unset this hw flag in this case.
12716 	 */
12717 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12718 	    pf->hw.func_caps.npar_enable &&
12719 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12720 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12721 
12722 #ifdef CONFIG_PCI_IOV
12723 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12724 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12725 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12726 		pf->num_req_vfs = min_t(int,
12727 					pf->hw.func_caps.num_vfs,
12728 					I40E_MAX_VF_COUNT);
12729 	}
12730 #endif /* CONFIG_PCI_IOV */
12731 	pf->eeprom_version = 0xDEAD;
12732 	pf->lan_veb = I40E_NO_VEB;
12733 	pf->lan_vsi = I40E_NO_VSI;
12734 
12735 	/* By default FW has this off for performance reasons */
12736 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12737 
12738 	/* set up queue assignment tracking */
12739 	size = sizeof(struct i40e_lump_tracking)
12740 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12741 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12742 	if (!pf->qp_pile) {
12743 		err = -ENOMEM;
12744 		goto sw_init_done;
12745 	}
12746 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12747 
12748 	pf->tx_timeout_recovery_level = 1;
12749 
12750 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12751 	    i40e_is_total_port_shutdown_enabled(pf)) {
12752 		/* Link down on close must be on when total port shutdown
12753 		 * is enabled for a given port
12754 		 */
12755 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12756 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12757 		dev_info(&pf->pdev->dev,
12758 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12759 	}
12760 	mutex_init(&pf->switch_mutex);
12761 
12762 sw_init_done:
12763 	return err;
12764 }
12765 
12766 /**
12767  * i40e_set_ntuple - set the ntuple feature flag and take action
12768  * @pf: board private structure to initialize
12769  * @features: the feature set that the stack is suggesting
12770  *
12771  * returns a bool to indicate if reset needs to happen
12772  **/
12773 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12774 {
12775 	bool need_reset = false;
12776 
12777 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12778 	 * the state changed, we need to reset.
12779 	 */
12780 	if (features & NETIF_F_NTUPLE) {
12781 		/* Enable filters and mark for reset */
12782 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12783 			need_reset = true;
12784 		/* enable FD_SB only if there is MSI-X vector and no cloud
12785 		 * filters exist
12786 		 */
12787 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12788 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12789 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12790 		}
12791 	} else {
12792 		/* turn off filters, mark for reset and clear SW filter list */
12793 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12794 			need_reset = true;
12795 			i40e_fdir_filter_exit(pf);
12796 		}
12797 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12798 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12799 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12800 
12801 		/* reset fd counters */
12802 		pf->fd_add_err = 0;
12803 		pf->fd_atr_cnt = 0;
12804 		/* if ATR was auto disabled it can be re-enabled. */
12805 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12806 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12807 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12808 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12809 	}
12810 	return need_reset;
12811 }
12812 
12813 /**
12814  * i40e_clear_rss_lut - clear the rx hash lookup table
12815  * @vsi: the VSI being configured
12816  **/
12817 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12818 {
12819 	struct i40e_pf *pf = vsi->back;
12820 	struct i40e_hw *hw = &pf->hw;
12821 	u16 vf_id = vsi->vf_id;
12822 	u8 i;
12823 
12824 	if (vsi->type == I40E_VSI_MAIN) {
12825 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12826 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12827 	} else if (vsi->type == I40E_VSI_SRIOV) {
12828 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12829 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12830 	} else {
12831 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12832 	}
12833 }
12834 
12835 /**
12836  * i40e_set_features - set the netdev feature flags
12837  * @netdev: ptr to the netdev being adjusted
12838  * @features: the feature set that the stack is suggesting
12839  * Note: expects to be called while under rtnl_lock()
12840  **/
12841 static int i40e_set_features(struct net_device *netdev,
12842 			     netdev_features_t features)
12843 {
12844 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12845 	struct i40e_vsi *vsi = np->vsi;
12846 	struct i40e_pf *pf = vsi->back;
12847 	bool need_reset;
12848 
12849 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12850 		i40e_pf_config_rss(pf);
12851 	else if (!(features & NETIF_F_RXHASH) &&
12852 		 netdev->features & NETIF_F_RXHASH)
12853 		i40e_clear_rss_lut(vsi);
12854 
12855 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12856 		i40e_vlan_stripping_enable(vsi);
12857 	else
12858 		i40e_vlan_stripping_disable(vsi);
12859 
12860 	if (!(features & NETIF_F_HW_TC) &&
12861 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12862 		dev_err(&pf->pdev->dev,
12863 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12864 		return -EINVAL;
12865 	}
12866 
12867 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12868 		i40e_del_all_macvlans(vsi);
12869 
12870 	need_reset = i40e_set_ntuple(pf, features);
12871 
12872 	if (need_reset)
12873 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12874 
12875 	return 0;
12876 }
12877 
12878 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12879 				    unsigned int table, unsigned int idx,
12880 				    struct udp_tunnel_info *ti)
12881 {
12882 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12883 	struct i40e_hw *hw = &np->vsi->back->hw;
12884 	u8 type, filter_index;
12885 	i40e_status ret;
12886 
12887 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12888 						   I40E_AQC_TUNNEL_TYPE_NGE;
12889 
12890 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12891 				     NULL);
12892 	if (ret) {
12893 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12894 			    i40e_stat_str(hw, ret),
12895 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12896 		return -EIO;
12897 	}
12898 
12899 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12900 	return 0;
12901 }
12902 
12903 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12904 				      unsigned int table, unsigned int idx,
12905 				      struct udp_tunnel_info *ti)
12906 {
12907 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12908 	struct i40e_hw *hw = &np->vsi->back->hw;
12909 	i40e_status ret;
12910 
12911 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12912 	if (ret) {
12913 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12914 			    i40e_stat_str(hw, ret),
12915 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12916 		return -EIO;
12917 	}
12918 
12919 	return 0;
12920 }
12921 
12922 static int i40e_get_phys_port_id(struct net_device *netdev,
12923 				 struct netdev_phys_item_id *ppid)
12924 {
12925 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12926 	struct i40e_pf *pf = np->vsi->back;
12927 	struct i40e_hw *hw = &pf->hw;
12928 
12929 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12930 		return -EOPNOTSUPP;
12931 
12932 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12933 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12934 
12935 	return 0;
12936 }
12937 
12938 /**
12939  * i40e_ndo_fdb_add - add an entry to the hardware database
12940  * @ndm: the input from the stack
12941  * @tb: pointer to array of nladdr (unused)
12942  * @dev: the net device pointer
12943  * @addr: the MAC address entry being added
12944  * @vid: VLAN ID
12945  * @flags: instructions from stack about fdb operation
12946  * @extack: netlink extended ack, unused currently
12947  */
12948 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12949 			    struct net_device *dev,
12950 			    const unsigned char *addr, u16 vid,
12951 			    u16 flags,
12952 			    struct netlink_ext_ack *extack)
12953 {
12954 	struct i40e_netdev_priv *np = netdev_priv(dev);
12955 	struct i40e_pf *pf = np->vsi->back;
12956 	int err = 0;
12957 
12958 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12959 		return -EOPNOTSUPP;
12960 
12961 	if (vid) {
12962 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12963 		return -EINVAL;
12964 	}
12965 
12966 	/* Hardware does not support aging addresses so if a
12967 	 * ndm_state is given only allow permanent addresses
12968 	 */
12969 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12970 		netdev_info(dev, "FDB only supports static addresses\n");
12971 		return -EINVAL;
12972 	}
12973 
12974 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12975 		err = dev_uc_add_excl(dev, addr);
12976 	else if (is_multicast_ether_addr(addr))
12977 		err = dev_mc_add_excl(dev, addr);
12978 	else
12979 		err = -EINVAL;
12980 
12981 	/* Only return duplicate errors if NLM_F_EXCL is set */
12982 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12983 		err = 0;
12984 
12985 	return err;
12986 }
12987 
12988 /**
12989  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12990  * @dev: the netdev being configured
12991  * @nlh: RTNL message
12992  * @flags: bridge flags
12993  * @extack: netlink extended ack
12994  *
12995  * Inserts a new hardware bridge if not already created and
12996  * enables the bridging mode requested (VEB or VEPA). If the
12997  * hardware bridge has already been inserted and the request
12998  * is to change the mode then that requires a PF reset to
12999  * allow rebuild of the components with required hardware
13000  * bridge mode enabled.
13001  *
13002  * Note: expects to be called while under rtnl_lock()
13003  **/
13004 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13005 				   struct nlmsghdr *nlh,
13006 				   u16 flags,
13007 				   struct netlink_ext_ack *extack)
13008 {
13009 	struct i40e_netdev_priv *np = netdev_priv(dev);
13010 	struct i40e_vsi *vsi = np->vsi;
13011 	struct i40e_pf *pf = vsi->back;
13012 	struct i40e_veb *veb = NULL;
13013 	struct nlattr *attr, *br_spec;
13014 	int i, rem;
13015 
13016 	/* Only for PF VSI for now */
13017 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13018 		return -EOPNOTSUPP;
13019 
13020 	/* Find the HW bridge for PF VSI */
13021 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13022 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13023 			veb = pf->veb[i];
13024 	}
13025 
13026 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13027 
13028 	nla_for_each_nested(attr, br_spec, rem) {
13029 		__u16 mode;
13030 
13031 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13032 			continue;
13033 
13034 		mode = nla_get_u16(attr);
13035 		if ((mode != BRIDGE_MODE_VEPA) &&
13036 		    (mode != BRIDGE_MODE_VEB))
13037 			return -EINVAL;
13038 
13039 		/* Insert a new HW bridge */
13040 		if (!veb) {
13041 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13042 					     vsi->tc_config.enabled_tc);
13043 			if (veb) {
13044 				veb->bridge_mode = mode;
13045 				i40e_config_bridge_mode(veb);
13046 			} else {
13047 				/* No Bridge HW offload available */
13048 				return -ENOENT;
13049 			}
13050 			break;
13051 		} else if (mode != veb->bridge_mode) {
13052 			/* Existing HW bridge but different mode needs reset */
13053 			veb->bridge_mode = mode;
13054 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13055 			if (mode == BRIDGE_MODE_VEB)
13056 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13057 			else
13058 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13059 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13060 			break;
13061 		}
13062 	}
13063 
13064 	return 0;
13065 }
13066 
13067 /**
13068  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13069  * @skb: skb buff
13070  * @pid: process id
13071  * @seq: RTNL message seq #
13072  * @dev: the netdev being configured
13073  * @filter_mask: unused
13074  * @nlflags: netlink flags passed in
13075  *
13076  * Return the mode in which the hardware bridge is operating in
13077  * i.e VEB or VEPA.
13078  **/
13079 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13080 				   struct net_device *dev,
13081 				   u32 __always_unused filter_mask,
13082 				   int nlflags)
13083 {
13084 	struct i40e_netdev_priv *np = netdev_priv(dev);
13085 	struct i40e_vsi *vsi = np->vsi;
13086 	struct i40e_pf *pf = vsi->back;
13087 	struct i40e_veb *veb = NULL;
13088 	int i;
13089 
13090 	/* Only for PF VSI for now */
13091 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13092 		return -EOPNOTSUPP;
13093 
13094 	/* Find the HW bridge for the PF VSI */
13095 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13096 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13097 			veb = pf->veb[i];
13098 	}
13099 
13100 	if (!veb)
13101 		return 0;
13102 
13103 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13104 				       0, 0, nlflags, filter_mask, NULL);
13105 }
13106 
13107 /**
13108  * i40e_features_check - Validate encapsulated packet conforms to limits
13109  * @skb: skb buff
13110  * @dev: This physical port's netdev
13111  * @features: Offload features that the stack believes apply
13112  **/
13113 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13114 					     struct net_device *dev,
13115 					     netdev_features_t features)
13116 {
13117 	size_t len;
13118 
13119 	/* No point in doing any of this if neither checksum nor GSO are
13120 	 * being requested for this frame.  We can rule out both by just
13121 	 * checking for CHECKSUM_PARTIAL
13122 	 */
13123 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13124 		return features;
13125 
13126 	/* We cannot support GSO if the MSS is going to be less than
13127 	 * 64 bytes.  If it is then we need to drop support for GSO.
13128 	 */
13129 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13130 		features &= ~NETIF_F_GSO_MASK;
13131 
13132 	/* MACLEN can support at most 63 words */
13133 	len = skb_network_header(skb) - skb->data;
13134 	if (len & ~(63 * 2))
13135 		goto out_err;
13136 
13137 	/* IPLEN and EIPLEN can support at most 127 dwords */
13138 	len = skb_transport_header(skb) - skb_network_header(skb);
13139 	if (len & ~(127 * 4))
13140 		goto out_err;
13141 
13142 	if (skb->encapsulation) {
13143 		/* L4TUNLEN can support 127 words */
13144 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13145 		if (len & ~(127 * 2))
13146 			goto out_err;
13147 
13148 		/* IPLEN can support at most 127 dwords */
13149 		len = skb_inner_transport_header(skb) -
13150 		      skb_inner_network_header(skb);
13151 		if (len & ~(127 * 4))
13152 			goto out_err;
13153 	}
13154 
13155 	/* No need to validate L4LEN as TCP is the only protocol with a
13156 	 * a flexible value and we support all possible values supported
13157 	 * by TCP, which is at most 15 dwords
13158 	 */
13159 
13160 	return features;
13161 out_err:
13162 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13163 }
13164 
13165 /**
13166  * i40e_xdp_setup - add/remove an XDP program
13167  * @vsi: VSI to changed
13168  * @prog: XDP program
13169  * @extack: netlink extended ack
13170  **/
13171 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13172 			  struct netlink_ext_ack *extack)
13173 {
13174 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13175 	struct i40e_pf *pf = vsi->back;
13176 	struct bpf_prog *old_prog;
13177 	bool need_reset;
13178 	int i;
13179 
13180 	/* Don't allow frames that span over multiple buffers */
13181 	if (frame_size > vsi->rx_buf_len) {
13182 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13183 		return -EINVAL;
13184 	}
13185 
13186 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13187 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13188 
13189 	if (need_reset)
13190 		i40e_prep_for_reset(pf);
13191 
13192 	/* VSI shall be deleted in a moment, just return EINVAL */
13193 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13194 		return -EINVAL;
13195 
13196 	old_prog = xchg(&vsi->xdp_prog, prog);
13197 
13198 	if (need_reset) {
13199 		if (!prog)
13200 			/* Wait until ndo_xsk_wakeup completes. */
13201 			synchronize_rcu();
13202 		i40e_reset_and_rebuild(pf, true, true);
13203 	}
13204 
13205 	for (i = 0; i < vsi->num_queue_pairs; i++)
13206 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13207 
13208 	if (old_prog)
13209 		bpf_prog_put(old_prog);
13210 
13211 	/* Kick start the NAPI context if there is an AF_XDP socket open
13212 	 * on that queue id. This so that receiving will start.
13213 	 */
13214 	if (need_reset && prog)
13215 		for (i = 0; i < vsi->num_queue_pairs; i++)
13216 			if (vsi->xdp_rings[i]->xsk_pool)
13217 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13218 						      XDP_WAKEUP_RX);
13219 
13220 	return 0;
13221 }
13222 
13223 /**
13224  * i40e_enter_busy_conf - Enters busy config state
13225  * @vsi: vsi
13226  *
13227  * Returns 0 on success, <0 for failure.
13228  **/
13229 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13230 {
13231 	struct i40e_pf *pf = vsi->back;
13232 	int timeout = 50;
13233 
13234 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13235 		timeout--;
13236 		if (!timeout)
13237 			return -EBUSY;
13238 		usleep_range(1000, 2000);
13239 	}
13240 
13241 	return 0;
13242 }
13243 
13244 /**
13245  * i40e_exit_busy_conf - Exits busy config state
13246  * @vsi: vsi
13247  **/
13248 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13249 {
13250 	struct i40e_pf *pf = vsi->back;
13251 
13252 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13253 }
13254 
13255 /**
13256  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13257  * @vsi: vsi
13258  * @queue_pair: queue pair
13259  **/
13260 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13261 {
13262 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13263 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13264 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13265 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13266 	if (i40e_enabled_xdp_vsi(vsi)) {
13267 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13268 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13269 	}
13270 }
13271 
13272 /**
13273  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13274  * @vsi: vsi
13275  * @queue_pair: queue pair
13276  **/
13277 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13278 {
13279 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13280 	if (i40e_enabled_xdp_vsi(vsi)) {
13281 		/* Make sure that in-progress ndo_xdp_xmit calls are
13282 		 * completed.
13283 		 */
13284 		synchronize_rcu();
13285 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13286 	}
13287 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13288 }
13289 
13290 /**
13291  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13292  * @vsi: vsi
13293  * @queue_pair: queue pair
13294  * @enable: true for enable, false for disable
13295  **/
13296 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13297 					bool enable)
13298 {
13299 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13300 	struct i40e_q_vector *q_vector = rxr->q_vector;
13301 
13302 	if (!vsi->netdev)
13303 		return;
13304 
13305 	/* All rings in a qp belong to the same qvector. */
13306 	if (q_vector->rx.ring || q_vector->tx.ring) {
13307 		if (enable)
13308 			napi_enable(&q_vector->napi);
13309 		else
13310 			napi_disable(&q_vector->napi);
13311 	}
13312 }
13313 
13314 /**
13315  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13316  * @vsi: vsi
13317  * @queue_pair: queue pair
13318  * @enable: true for enable, false for disable
13319  *
13320  * Returns 0 on success, <0 on failure.
13321  **/
13322 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13323 					bool enable)
13324 {
13325 	struct i40e_pf *pf = vsi->back;
13326 	int pf_q, ret = 0;
13327 
13328 	pf_q = vsi->base_queue + queue_pair;
13329 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13330 				     false /*is xdp*/, enable);
13331 	if (ret) {
13332 		dev_info(&pf->pdev->dev,
13333 			 "VSI seid %d Tx ring %d %sable timeout\n",
13334 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13335 		return ret;
13336 	}
13337 
13338 	i40e_control_rx_q(pf, pf_q, enable);
13339 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13340 	if (ret) {
13341 		dev_info(&pf->pdev->dev,
13342 			 "VSI seid %d Rx ring %d %sable timeout\n",
13343 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13344 		return ret;
13345 	}
13346 
13347 	/* Due to HW errata, on Rx disable only, the register can
13348 	 * indicate done before it really is. Needs 50ms to be sure
13349 	 */
13350 	if (!enable)
13351 		mdelay(50);
13352 
13353 	if (!i40e_enabled_xdp_vsi(vsi))
13354 		return ret;
13355 
13356 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13357 				     pf_q + vsi->alloc_queue_pairs,
13358 				     true /*is xdp*/, enable);
13359 	if (ret) {
13360 		dev_info(&pf->pdev->dev,
13361 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13362 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13363 	}
13364 
13365 	return ret;
13366 }
13367 
13368 /**
13369  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13370  * @vsi: vsi
13371  * @queue_pair: queue_pair
13372  **/
13373 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13374 {
13375 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13376 	struct i40e_pf *pf = vsi->back;
13377 	struct i40e_hw *hw = &pf->hw;
13378 
13379 	/* All rings in a qp belong to the same qvector. */
13380 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13381 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13382 	else
13383 		i40e_irq_dynamic_enable_icr0(pf);
13384 
13385 	i40e_flush(hw);
13386 }
13387 
13388 /**
13389  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13390  * @vsi: vsi
13391  * @queue_pair: queue_pair
13392  **/
13393 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13394 {
13395 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13396 	struct i40e_pf *pf = vsi->back;
13397 	struct i40e_hw *hw = &pf->hw;
13398 
13399 	/* For simplicity, instead of removing the qp interrupt causes
13400 	 * from the interrupt linked list, we simply disable the interrupt, and
13401 	 * leave the list intact.
13402 	 *
13403 	 * All rings in a qp belong to the same qvector.
13404 	 */
13405 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13406 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13407 
13408 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13409 		i40e_flush(hw);
13410 		synchronize_irq(pf->msix_entries[intpf].vector);
13411 	} else {
13412 		/* Legacy and MSI mode - this stops all interrupt handling */
13413 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13414 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13415 		i40e_flush(hw);
13416 		synchronize_irq(pf->pdev->irq);
13417 	}
13418 }
13419 
13420 /**
13421  * i40e_queue_pair_disable - Disables a queue pair
13422  * @vsi: vsi
13423  * @queue_pair: queue pair
13424  *
13425  * Returns 0 on success, <0 on failure.
13426  **/
13427 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13428 {
13429 	int err;
13430 
13431 	err = i40e_enter_busy_conf(vsi);
13432 	if (err)
13433 		return err;
13434 
13435 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13436 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13437 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13438 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13439 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13440 
13441 	return err;
13442 }
13443 
13444 /**
13445  * i40e_queue_pair_enable - Enables a queue pair
13446  * @vsi: vsi
13447  * @queue_pair: queue pair
13448  *
13449  * Returns 0 on success, <0 on failure.
13450  **/
13451 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13452 {
13453 	int err;
13454 
13455 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13456 	if (err)
13457 		return err;
13458 
13459 	if (i40e_enabled_xdp_vsi(vsi)) {
13460 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13461 		if (err)
13462 			return err;
13463 	}
13464 
13465 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13466 	if (err)
13467 		return err;
13468 
13469 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13470 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13471 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13472 
13473 	i40e_exit_busy_conf(vsi);
13474 
13475 	return err;
13476 }
13477 
13478 /**
13479  * i40e_xdp - implements ndo_bpf for i40e
13480  * @dev: netdevice
13481  * @xdp: XDP command
13482  **/
13483 static int i40e_xdp(struct net_device *dev,
13484 		    struct netdev_bpf *xdp)
13485 {
13486 	struct i40e_netdev_priv *np = netdev_priv(dev);
13487 	struct i40e_vsi *vsi = np->vsi;
13488 
13489 	if (vsi->type != I40E_VSI_MAIN)
13490 		return -EINVAL;
13491 
13492 	switch (xdp->command) {
13493 	case XDP_SETUP_PROG:
13494 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13495 	case XDP_SETUP_XSK_POOL:
13496 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13497 					   xdp->xsk.queue_id);
13498 	default:
13499 		return -EINVAL;
13500 	}
13501 }
13502 
13503 static const struct net_device_ops i40e_netdev_ops = {
13504 	.ndo_open		= i40e_open,
13505 	.ndo_stop		= i40e_close,
13506 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13507 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13508 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13509 	.ndo_validate_addr	= eth_validate_addr,
13510 	.ndo_set_mac_address	= i40e_set_mac,
13511 	.ndo_change_mtu		= i40e_change_mtu,
13512 	.ndo_eth_ioctl		= i40e_ioctl,
13513 	.ndo_tx_timeout		= i40e_tx_timeout,
13514 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13515 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13516 #ifdef CONFIG_NET_POLL_CONTROLLER
13517 	.ndo_poll_controller	= i40e_netpoll,
13518 #endif
13519 	.ndo_setup_tc		= __i40e_setup_tc,
13520 	.ndo_select_queue	= i40e_lan_select_queue,
13521 	.ndo_set_features	= i40e_set_features,
13522 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13523 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13524 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13525 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13526 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13527 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13528 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13529 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13530 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13531 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13532 	.ndo_features_check	= i40e_features_check,
13533 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13534 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13535 	.ndo_bpf		= i40e_xdp,
13536 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13537 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13538 	.ndo_dfwd_add_station	= i40e_fwd_add,
13539 	.ndo_dfwd_del_station	= i40e_fwd_del,
13540 };
13541 
13542 /**
13543  * i40e_config_netdev - Setup the netdev flags
13544  * @vsi: the VSI being configured
13545  *
13546  * Returns 0 on success, negative value on failure
13547  **/
13548 static int i40e_config_netdev(struct i40e_vsi *vsi)
13549 {
13550 	struct i40e_pf *pf = vsi->back;
13551 	struct i40e_hw *hw = &pf->hw;
13552 	struct i40e_netdev_priv *np;
13553 	struct net_device *netdev;
13554 	u8 broadcast[ETH_ALEN];
13555 	u8 mac_addr[ETH_ALEN];
13556 	int etherdev_size;
13557 	netdev_features_t hw_enc_features;
13558 	netdev_features_t hw_features;
13559 
13560 	etherdev_size = sizeof(struct i40e_netdev_priv);
13561 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13562 	if (!netdev)
13563 		return -ENOMEM;
13564 
13565 	vsi->netdev = netdev;
13566 	np = netdev_priv(netdev);
13567 	np->vsi = vsi;
13568 
13569 	hw_enc_features = NETIF_F_SG			|
13570 			  NETIF_F_HW_CSUM		|
13571 			  NETIF_F_HIGHDMA		|
13572 			  NETIF_F_SOFT_FEATURES		|
13573 			  NETIF_F_TSO			|
13574 			  NETIF_F_TSO_ECN		|
13575 			  NETIF_F_TSO6			|
13576 			  NETIF_F_GSO_GRE		|
13577 			  NETIF_F_GSO_GRE_CSUM		|
13578 			  NETIF_F_GSO_PARTIAL		|
13579 			  NETIF_F_GSO_IPXIP4		|
13580 			  NETIF_F_GSO_IPXIP6		|
13581 			  NETIF_F_GSO_UDP_TUNNEL	|
13582 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13583 			  NETIF_F_GSO_UDP_L4		|
13584 			  NETIF_F_SCTP_CRC		|
13585 			  NETIF_F_RXHASH		|
13586 			  NETIF_F_RXCSUM		|
13587 			  0;
13588 
13589 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13590 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13591 
13592 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13593 
13594 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13595 
13596 	netdev->hw_enc_features |= hw_enc_features;
13597 
13598 	/* record features VLANs can make use of */
13599 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13600 
13601 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13602 				   NETIF_F_GSO_GRE_CSUM |	\
13603 				   NETIF_F_GSO_IPXIP4 |		\
13604 				   NETIF_F_GSO_IPXIP6 |		\
13605 				   NETIF_F_GSO_UDP_TUNNEL |	\
13606 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13607 
13608 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13609 	netdev->features |= NETIF_F_GSO_PARTIAL |
13610 			    I40E_GSO_PARTIAL_FEATURES;
13611 
13612 	netdev->mpls_features |= NETIF_F_SG;
13613 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13614 	netdev->mpls_features |= NETIF_F_TSO;
13615 	netdev->mpls_features |= NETIF_F_TSO6;
13616 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13617 
13618 	/* enable macvlan offloads */
13619 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13620 
13621 	hw_features = hw_enc_features		|
13622 		      NETIF_F_HW_VLAN_CTAG_TX	|
13623 		      NETIF_F_HW_VLAN_CTAG_RX;
13624 
13625 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13626 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13627 
13628 	netdev->hw_features |= hw_features;
13629 
13630 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13631 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13632 
13633 	netdev->features &= ~NETIF_F_HW_TC;
13634 
13635 	if (vsi->type == I40E_VSI_MAIN) {
13636 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13637 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13638 		/* The following steps are necessary for two reasons. First,
13639 		 * some older NVM configurations load a default MAC-VLAN
13640 		 * filter that will accept any tagged packet, and we want to
13641 		 * replace this with a normal filter. Additionally, it is
13642 		 * possible our MAC address was provided by the platform using
13643 		 * Open Firmware or similar.
13644 		 *
13645 		 * Thus, we need to remove the default filter and install one
13646 		 * specific to the MAC address.
13647 		 */
13648 		i40e_rm_default_mac_filter(vsi, mac_addr);
13649 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13650 		i40e_add_mac_filter(vsi, mac_addr);
13651 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13652 	} else {
13653 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13654 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13655 		 * the end, which is 4 bytes long, so force truncation of the
13656 		 * original name by IFNAMSIZ - 4
13657 		 */
13658 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13659 			 IFNAMSIZ - 4,
13660 			 pf->vsi[pf->lan_vsi]->netdev->name);
13661 		eth_random_addr(mac_addr);
13662 
13663 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13664 		i40e_add_mac_filter(vsi, mac_addr);
13665 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13666 	}
13667 
13668 	/* Add the broadcast filter so that we initially will receive
13669 	 * broadcast packets. Note that when a new VLAN is first added the
13670 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13671 	 * specific filters as part of transitioning into "vlan" operation.
13672 	 * When more VLANs are added, the driver will copy each existing MAC
13673 	 * filter and add it for the new VLAN.
13674 	 *
13675 	 * Broadcast filters are handled specially by
13676 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13677 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13678 	 * filter. The subtask will update the correct broadcast promiscuous
13679 	 * bits as VLANs become active or inactive.
13680 	 */
13681 	eth_broadcast_addr(broadcast);
13682 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13683 	i40e_add_mac_filter(vsi, broadcast);
13684 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13685 
13686 	eth_hw_addr_set(netdev, mac_addr);
13687 	ether_addr_copy(netdev->perm_addr, mac_addr);
13688 
13689 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13690 	netdev->neigh_priv_len = sizeof(u32) * 4;
13691 
13692 	netdev->priv_flags |= IFF_UNICAST_FLT;
13693 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13694 	/* Setup netdev TC information */
13695 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13696 
13697 	netdev->netdev_ops = &i40e_netdev_ops;
13698 	netdev->watchdog_timeo = 5 * HZ;
13699 	i40e_set_ethtool_ops(netdev);
13700 
13701 	/* MTU range: 68 - 9706 */
13702 	netdev->min_mtu = ETH_MIN_MTU;
13703 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13704 
13705 	return 0;
13706 }
13707 
13708 /**
13709  * i40e_vsi_delete - Delete a VSI from the switch
13710  * @vsi: the VSI being removed
13711  *
13712  * Returns 0 on success, negative value on failure
13713  **/
13714 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13715 {
13716 	/* remove default VSI is not allowed */
13717 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13718 		return;
13719 
13720 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13721 }
13722 
13723 /**
13724  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13725  * @vsi: the VSI being queried
13726  *
13727  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13728  **/
13729 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13730 {
13731 	struct i40e_veb *veb;
13732 	struct i40e_pf *pf = vsi->back;
13733 
13734 	/* Uplink is not a bridge so default to VEB */
13735 	if (vsi->veb_idx >= I40E_MAX_VEB)
13736 		return 1;
13737 
13738 	veb = pf->veb[vsi->veb_idx];
13739 	if (!veb) {
13740 		dev_info(&pf->pdev->dev,
13741 			 "There is no veb associated with the bridge\n");
13742 		return -ENOENT;
13743 	}
13744 
13745 	/* Uplink is a bridge in VEPA mode */
13746 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13747 		return 0;
13748 	} else {
13749 		/* Uplink is a bridge in VEB mode */
13750 		return 1;
13751 	}
13752 
13753 	/* VEPA is now default bridge, so return 0 */
13754 	return 0;
13755 }
13756 
13757 /**
13758  * i40e_add_vsi - Add a VSI to the switch
13759  * @vsi: the VSI being configured
13760  *
13761  * This initializes a VSI context depending on the VSI type to be added and
13762  * passes it down to the add_vsi aq command.
13763  **/
13764 static int i40e_add_vsi(struct i40e_vsi *vsi)
13765 {
13766 	int ret = -ENODEV;
13767 	struct i40e_pf *pf = vsi->back;
13768 	struct i40e_hw *hw = &pf->hw;
13769 	struct i40e_vsi_context ctxt;
13770 	struct i40e_mac_filter *f;
13771 	struct hlist_node *h;
13772 	int bkt;
13773 
13774 	u8 enabled_tc = 0x1; /* TC0 enabled */
13775 	int f_count = 0;
13776 
13777 	memset(&ctxt, 0, sizeof(ctxt));
13778 	switch (vsi->type) {
13779 	case I40E_VSI_MAIN:
13780 		/* The PF's main VSI is already setup as part of the
13781 		 * device initialization, so we'll not bother with
13782 		 * the add_vsi call, but we will retrieve the current
13783 		 * VSI context.
13784 		 */
13785 		ctxt.seid = pf->main_vsi_seid;
13786 		ctxt.pf_num = pf->hw.pf_id;
13787 		ctxt.vf_num = 0;
13788 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13789 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13790 		if (ret) {
13791 			dev_info(&pf->pdev->dev,
13792 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13793 				 i40e_stat_str(&pf->hw, ret),
13794 				 i40e_aq_str(&pf->hw,
13795 					     pf->hw.aq.asq_last_status));
13796 			return -ENOENT;
13797 		}
13798 		vsi->info = ctxt.info;
13799 		vsi->info.valid_sections = 0;
13800 
13801 		vsi->seid = ctxt.seid;
13802 		vsi->id = ctxt.vsi_number;
13803 
13804 		enabled_tc = i40e_pf_get_tc_map(pf);
13805 
13806 		/* Source pruning is enabled by default, so the flag is
13807 		 * negative logic - if it's set, we need to fiddle with
13808 		 * the VSI to disable source pruning.
13809 		 */
13810 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13811 			memset(&ctxt, 0, sizeof(ctxt));
13812 			ctxt.seid = pf->main_vsi_seid;
13813 			ctxt.pf_num = pf->hw.pf_id;
13814 			ctxt.vf_num = 0;
13815 			ctxt.info.valid_sections |=
13816 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13817 			ctxt.info.switch_id =
13818 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13819 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13820 			if (ret) {
13821 				dev_info(&pf->pdev->dev,
13822 					 "update vsi failed, err %s aq_err %s\n",
13823 					 i40e_stat_str(&pf->hw, ret),
13824 					 i40e_aq_str(&pf->hw,
13825 						     pf->hw.aq.asq_last_status));
13826 				ret = -ENOENT;
13827 				goto err;
13828 			}
13829 		}
13830 
13831 		/* MFP mode setup queue map and update VSI */
13832 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13833 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13834 			memset(&ctxt, 0, sizeof(ctxt));
13835 			ctxt.seid = pf->main_vsi_seid;
13836 			ctxt.pf_num = pf->hw.pf_id;
13837 			ctxt.vf_num = 0;
13838 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13839 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13840 			if (ret) {
13841 				dev_info(&pf->pdev->dev,
13842 					 "update vsi failed, err %s aq_err %s\n",
13843 					 i40e_stat_str(&pf->hw, ret),
13844 					 i40e_aq_str(&pf->hw,
13845 						    pf->hw.aq.asq_last_status));
13846 				ret = -ENOENT;
13847 				goto err;
13848 			}
13849 			/* update the local VSI info queue map */
13850 			i40e_vsi_update_queue_map(vsi, &ctxt);
13851 			vsi->info.valid_sections = 0;
13852 		} else {
13853 			/* Default/Main VSI is only enabled for TC0
13854 			 * reconfigure it to enable all TCs that are
13855 			 * available on the port in SFP mode.
13856 			 * For MFP case the iSCSI PF would use this
13857 			 * flow to enable LAN+iSCSI TC.
13858 			 */
13859 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13860 			if (ret) {
13861 				/* Single TC condition is not fatal,
13862 				 * message and continue
13863 				 */
13864 				dev_info(&pf->pdev->dev,
13865 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13866 					 enabled_tc,
13867 					 i40e_stat_str(&pf->hw, ret),
13868 					 i40e_aq_str(&pf->hw,
13869 						    pf->hw.aq.asq_last_status));
13870 			}
13871 		}
13872 		break;
13873 
13874 	case I40E_VSI_FDIR:
13875 		ctxt.pf_num = hw->pf_id;
13876 		ctxt.vf_num = 0;
13877 		ctxt.uplink_seid = vsi->uplink_seid;
13878 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13879 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13880 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13881 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13882 			ctxt.info.valid_sections |=
13883 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13884 			ctxt.info.switch_id =
13885 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13886 		}
13887 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13888 		break;
13889 
13890 	case I40E_VSI_VMDQ2:
13891 		ctxt.pf_num = hw->pf_id;
13892 		ctxt.vf_num = 0;
13893 		ctxt.uplink_seid = vsi->uplink_seid;
13894 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13895 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13896 
13897 		/* This VSI is connected to VEB so the switch_id
13898 		 * should be set to zero by default.
13899 		 */
13900 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13901 			ctxt.info.valid_sections |=
13902 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13903 			ctxt.info.switch_id =
13904 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13905 		}
13906 
13907 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13908 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13909 		break;
13910 
13911 	case I40E_VSI_SRIOV:
13912 		ctxt.pf_num = hw->pf_id;
13913 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13914 		ctxt.uplink_seid = vsi->uplink_seid;
13915 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13916 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13917 
13918 		/* This VSI is connected to VEB so the switch_id
13919 		 * should be set to zero by default.
13920 		 */
13921 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13922 			ctxt.info.valid_sections |=
13923 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13924 			ctxt.info.switch_id =
13925 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13926 		}
13927 
13928 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13929 			ctxt.info.valid_sections |=
13930 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13931 			ctxt.info.queueing_opt_flags |=
13932 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13933 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13934 		}
13935 
13936 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13937 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13938 		if (pf->vf[vsi->vf_id].spoofchk) {
13939 			ctxt.info.valid_sections |=
13940 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13941 			ctxt.info.sec_flags |=
13942 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13943 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13944 		}
13945 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13946 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13947 		break;
13948 
13949 	case I40E_VSI_IWARP:
13950 		/* send down message to iWARP */
13951 		break;
13952 
13953 	default:
13954 		return -ENODEV;
13955 	}
13956 
13957 	if (vsi->type != I40E_VSI_MAIN) {
13958 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13959 		if (ret) {
13960 			dev_info(&vsi->back->pdev->dev,
13961 				 "add vsi failed, err %s aq_err %s\n",
13962 				 i40e_stat_str(&pf->hw, ret),
13963 				 i40e_aq_str(&pf->hw,
13964 					     pf->hw.aq.asq_last_status));
13965 			ret = -ENOENT;
13966 			goto err;
13967 		}
13968 		vsi->info = ctxt.info;
13969 		vsi->info.valid_sections = 0;
13970 		vsi->seid = ctxt.seid;
13971 		vsi->id = ctxt.vsi_number;
13972 	}
13973 
13974 	vsi->active_filters = 0;
13975 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13976 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13977 	/* If macvlan filters already exist, force them to get loaded */
13978 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13979 		f->state = I40E_FILTER_NEW;
13980 		f_count++;
13981 	}
13982 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13983 
13984 	if (f_count) {
13985 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13986 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13987 	}
13988 
13989 	/* Update VSI BW information */
13990 	ret = i40e_vsi_get_bw_info(vsi);
13991 	if (ret) {
13992 		dev_info(&pf->pdev->dev,
13993 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13994 			 i40e_stat_str(&pf->hw, ret),
13995 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13996 		/* VSI is already added so not tearing that up */
13997 		ret = 0;
13998 	}
13999 
14000 err:
14001 	return ret;
14002 }
14003 
14004 /**
14005  * i40e_vsi_release - Delete a VSI and free its resources
14006  * @vsi: the VSI being removed
14007  *
14008  * Returns 0 on success or < 0 on error
14009  **/
14010 int i40e_vsi_release(struct i40e_vsi *vsi)
14011 {
14012 	struct i40e_mac_filter *f;
14013 	struct hlist_node *h;
14014 	struct i40e_veb *veb = NULL;
14015 	struct i40e_pf *pf;
14016 	u16 uplink_seid;
14017 	int i, n, bkt;
14018 
14019 	pf = vsi->back;
14020 
14021 	/* release of a VEB-owner or last VSI is not allowed */
14022 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14023 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14024 			 vsi->seid, vsi->uplink_seid);
14025 		return -ENODEV;
14026 	}
14027 	if (vsi == pf->vsi[pf->lan_vsi] &&
14028 	    !test_bit(__I40E_DOWN, pf->state)) {
14029 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14030 		return -ENODEV;
14031 	}
14032 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14033 	uplink_seid = vsi->uplink_seid;
14034 	if (vsi->type != I40E_VSI_SRIOV) {
14035 		if (vsi->netdev_registered) {
14036 			vsi->netdev_registered = false;
14037 			if (vsi->netdev) {
14038 				/* results in a call to i40e_close() */
14039 				unregister_netdev(vsi->netdev);
14040 			}
14041 		} else {
14042 			i40e_vsi_close(vsi);
14043 		}
14044 		i40e_vsi_disable_irq(vsi);
14045 	}
14046 
14047 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14048 
14049 	/* clear the sync flag on all filters */
14050 	if (vsi->netdev) {
14051 		__dev_uc_unsync(vsi->netdev, NULL);
14052 		__dev_mc_unsync(vsi->netdev, NULL);
14053 	}
14054 
14055 	/* make sure any remaining filters are marked for deletion */
14056 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14057 		__i40e_del_filter(vsi, f);
14058 
14059 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14060 
14061 	i40e_sync_vsi_filters(vsi);
14062 
14063 	i40e_vsi_delete(vsi);
14064 	i40e_vsi_free_q_vectors(vsi);
14065 	if (vsi->netdev) {
14066 		free_netdev(vsi->netdev);
14067 		vsi->netdev = NULL;
14068 	}
14069 	i40e_vsi_clear_rings(vsi);
14070 	i40e_vsi_clear(vsi);
14071 
14072 	/* If this was the last thing on the VEB, except for the
14073 	 * controlling VSI, remove the VEB, which puts the controlling
14074 	 * VSI onto the next level down in the switch.
14075 	 *
14076 	 * Well, okay, there's one more exception here: don't remove
14077 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14078 	 * from up the network stack.
14079 	 */
14080 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14081 		if (pf->vsi[i] &&
14082 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14083 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14084 			n++;      /* count the VSIs */
14085 		}
14086 	}
14087 	for (i = 0; i < I40E_MAX_VEB; i++) {
14088 		if (!pf->veb[i])
14089 			continue;
14090 		if (pf->veb[i]->uplink_seid == uplink_seid)
14091 			n++;     /* count the VEBs */
14092 		if (pf->veb[i]->seid == uplink_seid)
14093 			veb = pf->veb[i];
14094 	}
14095 	if (n == 0 && veb && veb->uplink_seid != 0)
14096 		i40e_veb_release(veb);
14097 
14098 	return 0;
14099 }
14100 
14101 /**
14102  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14103  * @vsi: ptr to the VSI
14104  *
14105  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14106  * corresponding SW VSI structure and initializes num_queue_pairs for the
14107  * newly allocated VSI.
14108  *
14109  * Returns 0 on success or negative on failure
14110  **/
14111 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14112 {
14113 	int ret = -ENOENT;
14114 	struct i40e_pf *pf = vsi->back;
14115 
14116 	if (vsi->q_vectors[0]) {
14117 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14118 			 vsi->seid);
14119 		return -EEXIST;
14120 	}
14121 
14122 	if (vsi->base_vector) {
14123 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14124 			 vsi->seid, vsi->base_vector);
14125 		return -EEXIST;
14126 	}
14127 
14128 	ret = i40e_vsi_alloc_q_vectors(vsi);
14129 	if (ret) {
14130 		dev_info(&pf->pdev->dev,
14131 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14132 			 vsi->num_q_vectors, vsi->seid, ret);
14133 		vsi->num_q_vectors = 0;
14134 		goto vector_setup_out;
14135 	}
14136 
14137 	/* In Legacy mode, we do not have to get any other vector since we
14138 	 * piggyback on the misc/ICR0 for queue interrupts.
14139 	*/
14140 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14141 		return ret;
14142 	if (vsi->num_q_vectors)
14143 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14144 						 vsi->num_q_vectors, vsi->idx);
14145 	if (vsi->base_vector < 0) {
14146 		dev_info(&pf->pdev->dev,
14147 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14148 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14149 		i40e_vsi_free_q_vectors(vsi);
14150 		ret = -ENOENT;
14151 		goto vector_setup_out;
14152 	}
14153 
14154 vector_setup_out:
14155 	return ret;
14156 }
14157 
14158 /**
14159  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14160  * @vsi: pointer to the vsi.
14161  *
14162  * This re-allocates a vsi's queue resources.
14163  *
14164  * Returns pointer to the successfully allocated and configured VSI sw struct
14165  * on success, otherwise returns NULL on failure.
14166  **/
14167 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14168 {
14169 	u16 alloc_queue_pairs;
14170 	struct i40e_pf *pf;
14171 	u8 enabled_tc;
14172 	int ret;
14173 
14174 	if (!vsi)
14175 		return NULL;
14176 
14177 	pf = vsi->back;
14178 
14179 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14180 	i40e_vsi_clear_rings(vsi);
14181 
14182 	i40e_vsi_free_arrays(vsi, false);
14183 	i40e_set_num_rings_in_vsi(vsi);
14184 	ret = i40e_vsi_alloc_arrays(vsi, false);
14185 	if (ret)
14186 		goto err_vsi;
14187 
14188 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14189 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14190 
14191 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14192 	if (ret < 0) {
14193 		dev_info(&pf->pdev->dev,
14194 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14195 			 alloc_queue_pairs, vsi->seid, ret);
14196 		goto err_vsi;
14197 	}
14198 	vsi->base_queue = ret;
14199 
14200 	/* Update the FW view of the VSI. Force a reset of TC and queue
14201 	 * layout configurations.
14202 	 */
14203 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14204 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14205 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14206 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14207 	if (vsi->type == I40E_VSI_MAIN)
14208 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14209 
14210 	/* assign it some queues */
14211 	ret = i40e_alloc_rings(vsi);
14212 	if (ret)
14213 		goto err_rings;
14214 
14215 	/* map all of the rings to the q_vectors */
14216 	i40e_vsi_map_rings_to_vectors(vsi);
14217 	return vsi;
14218 
14219 err_rings:
14220 	i40e_vsi_free_q_vectors(vsi);
14221 	if (vsi->netdev_registered) {
14222 		vsi->netdev_registered = false;
14223 		unregister_netdev(vsi->netdev);
14224 		free_netdev(vsi->netdev);
14225 		vsi->netdev = NULL;
14226 	}
14227 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14228 err_vsi:
14229 	i40e_vsi_clear(vsi);
14230 	return NULL;
14231 }
14232 
14233 /**
14234  * i40e_vsi_setup - Set up a VSI by a given type
14235  * @pf: board private structure
14236  * @type: VSI type
14237  * @uplink_seid: the switch element to link to
14238  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14239  *
14240  * This allocates the sw VSI structure and its queue resources, then add a VSI
14241  * to the identified VEB.
14242  *
14243  * Returns pointer to the successfully allocated and configure VSI sw struct on
14244  * success, otherwise returns NULL on failure.
14245  **/
14246 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14247 				u16 uplink_seid, u32 param1)
14248 {
14249 	struct i40e_vsi *vsi = NULL;
14250 	struct i40e_veb *veb = NULL;
14251 	u16 alloc_queue_pairs;
14252 	int ret, i;
14253 	int v_idx;
14254 
14255 	/* The requested uplink_seid must be either
14256 	 *     - the PF's port seid
14257 	 *              no VEB is needed because this is the PF
14258 	 *              or this is a Flow Director special case VSI
14259 	 *     - seid of an existing VEB
14260 	 *     - seid of a VSI that owns an existing VEB
14261 	 *     - seid of a VSI that doesn't own a VEB
14262 	 *              a new VEB is created and the VSI becomes the owner
14263 	 *     - seid of the PF VSI, which is what creates the first VEB
14264 	 *              this is a special case of the previous
14265 	 *
14266 	 * Find which uplink_seid we were given and create a new VEB if needed
14267 	 */
14268 	for (i = 0; i < I40E_MAX_VEB; i++) {
14269 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14270 			veb = pf->veb[i];
14271 			break;
14272 		}
14273 	}
14274 
14275 	if (!veb && uplink_seid != pf->mac_seid) {
14276 
14277 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14278 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14279 				vsi = pf->vsi[i];
14280 				break;
14281 			}
14282 		}
14283 		if (!vsi) {
14284 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14285 				 uplink_seid);
14286 			return NULL;
14287 		}
14288 
14289 		if (vsi->uplink_seid == pf->mac_seid)
14290 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14291 					     vsi->tc_config.enabled_tc);
14292 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14293 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14294 					     vsi->tc_config.enabled_tc);
14295 		if (veb) {
14296 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14297 				dev_info(&vsi->back->pdev->dev,
14298 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14299 				return NULL;
14300 			}
14301 			/* We come up by default in VEPA mode if SRIOV is not
14302 			 * already enabled, in which case we can't force VEPA
14303 			 * mode.
14304 			 */
14305 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14306 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14307 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14308 			}
14309 			i40e_config_bridge_mode(veb);
14310 		}
14311 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14312 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14313 				veb = pf->veb[i];
14314 		}
14315 		if (!veb) {
14316 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14317 			return NULL;
14318 		}
14319 
14320 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14321 		uplink_seid = veb->seid;
14322 	}
14323 
14324 	/* get vsi sw struct */
14325 	v_idx = i40e_vsi_mem_alloc(pf, type);
14326 	if (v_idx < 0)
14327 		goto err_alloc;
14328 	vsi = pf->vsi[v_idx];
14329 	if (!vsi)
14330 		goto err_alloc;
14331 	vsi->type = type;
14332 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14333 
14334 	if (type == I40E_VSI_MAIN)
14335 		pf->lan_vsi = v_idx;
14336 	else if (type == I40E_VSI_SRIOV)
14337 		vsi->vf_id = param1;
14338 	/* assign it some queues */
14339 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14340 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14341 
14342 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14343 	if (ret < 0) {
14344 		dev_info(&pf->pdev->dev,
14345 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14346 			 alloc_queue_pairs, vsi->seid, ret);
14347 		goto err_vsi;
14348 	}
14349 	vsi->base_queue = ret;
14350 
14351 	/* get a VSI from the hardware */
14352 	vsi->uplink_seid = uplink_seid;
14353 	ret = i40e_add_vsi(vsi);
14354 	if (ret)
14355 		goto err_vsi;
14356 
14357 	switch (vsi->type) {
14358 	/* setup the netdev if needed */
14359 	case I40E_VSI_MAIN:
14360 	case I40E_VSI_VMDQ2:
14361 		ret = i40e_config_netdev(vsi);
14362 		if (ret)
14363 			goto err_netdev;
14364 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14365 		if (ret)
14366 			goto err_netdev;
14367 		ret = register_netdev(vsi->netdev);
14368 		if (ret)
14369 			goto err_netdev;
14370 		vsi->netdev_registered = true;
14371 		netif_carrier_off(vsi->netdev);
14372 #ifdef CONFIG_I40E_DCB
14373 		/* Setup DCB netlink interface */
14374 		i40e_dcbnl_setup(vsi);
14375 #endif /* CONFIG_I40E_DCB */
14376 		fallthrough;
14377 	case I40E_VSI_FDIR:
14378 		/* set up vectors and rings if needed */
14379 		ret = i40e_vsi_setup_vectors(vsi);
14380 		if (ret)
14381 			goto err_msix;
14382 
14383 		ret = i40e_alloc_rings(vsi);
14384 		if (ret)
14385 			goto err_rings;
14386 
14387 		/* map all of the rings to the q_vectors */
14388 		i40e_vsi_map_rings_to_vectors(vsi);
14389 
14390 		i40e_vsi_reset_stats(vsi);
14391 		break;
14392 	default:
14393 		/* no netdev or rings for the other VSI types */
14394 		break;
14395 	}
14396 
14397 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14398 	    (vsi->type == I40E_VSI_VMDQ2)) {
14399 		ret = i40e_vsi_config_rss(vsi);
14400 	}
14401 	return vsi;
14402 
14403 err_rings:
14404 	i40e_vsi_free_q_vectors(vsi);
14405 err_msix:
14406 	if (vsi->netdev_registered) {
14407 		vsi->netdev_registered = false;
14408 		unregister_netdev(vsi->netdev);
14409 		free_netdev(vsi->netdev);
14410 		vsi->netdev = NULL;
14411 	}
14412 err_netdev:
14413 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14414 err_vsi:
14415 	i40e_vsi_clear(vsi);
14416 err_alloc:
14417 	return NULL;
14418 }
14419 
14420 /**
14421  * i40e_veb_get_bw_info - Query VEB BW information
14422  * @veb: the veb to query
14423  *
14424  * Query the Tx scheduler BW configuration data for given VEB
14425  **/
14426 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14427 {
14428 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14429 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14430 	struct i40e_pf *pf = veb->pf;
14431 	struct i40e_hw *hw = &pf->hw;
14432 	u32 tc_bw_max;
14433 	int ret = 0;
14434 	int i;
14435 
14436 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14437 						  &bw_data, NULL);
14438 	if (ret) {
14439 		dev_info(&pf->pdev->dev,
14440 			 "query veb bw config failed, err %s aq_err %s\n",
14441 			 i40e_stat_str(&pf->hw, ret),
14442 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14443 		goto out;
14444 	}
14445 
14446 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14447 						   &ets_data, NULL);
14448 	if (ret) {
14449 		dev_info(&pf->pdev->dev,
14450 			 "query veb bw ets config failed, err %s aq_err %s\n",
14451 			 i40e_stat_str(&pf->hw, ret),
14452 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14453 		goto out;
14454 	}
14455 
14456 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14457 	veb->bw_max_quanta = ets_data.tc_bw_max;
14458 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14459 	veb->enabled_tc = ets_data.tc_valid_bits;
14460 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14461 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14462 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14463 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14464 		veb->bw_tc_limit_credits[i] =
14465 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14466 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14467 	}
14468 
14469 out:
14470 	return ret;
14471 }
14472 
14473 /**
14474  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14475  * @pf: board private structure
14476  *
14477  * On error: returns error code (negative)
14478  * On success: returns vsi index in PF (positive)
14479  **/
14480 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14481 {
14482 	int ret = -ENOENT;
14483 	struct i40e_veb *veb;
14484 	int i;
14485 
14486 	/* Need to protect the allocation of switch elements at the PF level */
14487 	mutex_lock(&pf->switch_mutex);
14488 
14489 	/* VEB list may be fragmented if VEB creation/destruction has
14490 	 * been happening.  We can afford to do a quick scan to look
14491 	 * for any free slots in the list.
14492 	 *
14493 	 * find next empty veb slot, looping back around if necessary
14494 	 */
14495 	i = 0;
14496 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14497 		i++;
14498 	if (i >= I40E_MAX_VEB) {
14499 		ret = -ENOMEM;
14500 		goto err_alloc_veb;  /* out of VEB slots! */
14501 	}
14502 
14503 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14504 	if (!veb) {
14505 		ret = -ENOMEM;
14506 		goto err_alloc_veb;
14507 	}
14508 	veb->pf = pf;
14509 	veb->idx = i;
14510 	veb->enabled_tc = 1;
14511 
14512 	pf->veb[i] = veb;
14513 	ret = i;
14514 err_alloc_veb:
14515 	mutex_unlock(&pf->switch_mutex);
14516 	return ret;
14517 }
14518 
14519 /**
14520  * i40e_switch_branch_release - Delete a branch of the switch tree
14521  * @branch: where to start deleting
14522  *
14523  * This uses recursion to find the tips of the branch to be
14524  * removed, deleting until we get back to and can delete this VEB.
14525  **/
14526 static void i40e_switch_branch_release(struct i40e_veb *branch)
14527 {
14528 	struct i40e_pf *pf = branch->pf;
14529 	u16 branch_seid = branch->seid;
14530 	u16 veb_idx = branch->idx;
14531 	int i;
14532 
14533 	/* release any VEBs on this VEB - RECURSION */
14534 	for (i = 0; i < I40E_MAX_VEB; i++) {
14535 		if (!pf->veb[i])
14536 			continue;
14537 		if (pf->veb[i]->uplink_seid == branch->seid)
14538 			i40e_switch_branch_release(pf->veb[i]);
14539 	}
14540 
14541 	/* Release the VSIs on this VEB, but not the owner VSI.
14542 	 *
14543 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14544 	 *       the VEB itself, so don't use (*branch) after this loop.
14545 	 */
14546 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14547 		if (!pf->vsi[i])
14548 			continue;
14549 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14550 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14551 			i40e_vsi_release(pf->vsi[i]);
14552 		}
14553 	}
14554 
14555 	/* There's one corner case where the VEB might not have been
14556 	 * removed, so double check it here and remove it if needed.
14557 	 * This case happens if the veb was created from the debugfs
14558 	 * commands and no VSIs were added to it.
14559 	 */
14560 	if (pf->veb[veb_idx])
14561 		i40e_veb_release(pf->veb[veb_idx]);
14562 }
14563 
14564 /**
14565  * i40e_veb_clear - remove veb struct
14566  * @veb: the veb to remove
14567  **/
14568 static void i40e_veb_clear(struct i40e_veb *veb)
14569 {
14570 	if (!veb)
14571 		return;
14572 
14573 	if (veb->pf) {
14574 		struct i40e_pf *pf = veb->pf;
14575 
14576 		mutex_lock(&pf->switch_mutex);
14577 		if (pf->veb[veb->idx] == veb)
14578 			pf->veb[veb->idx] = NULL;
14579 		mutex_unlock(&pf->switch_mutex);
14580 	}
14581 
14582 	kfree(veb);
14583 }
14584 
14585 /**
14586  * i40e_veb_release - Delete a VEB and free its resources
14587  * @veb: the VEB being removed
14588  **/
14589 void i40e_veb_release(struct i40e_veb *veb)
14590 {
14591 	struct i40e_vsi *vsi = NULL;
14592 	struct i40e_pf *pf;
14593 	int i, n = 0;
14594 
14595 	pf = veb->pf;
14596 
14597 	/* find the remaining VSI and check for extras */
14598 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14599 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14600 			n++;
14601 			vsi = pf->vsi[i];
14602 		}
14603 	}
14604 	if (n != 1) {
14605 		dev_info(&pf->pdev->dev,
14606 			 "can't remove VEB %d with %d VSIs left\n",
14607 			 veb->seid, n);
14608 		return;
14609 	}
14610 
14611 	/* move the remaining VSI to uplink veb */
14612 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14613 	if (veb->uplink_seid) {
14614 		vsi->uplink_seid = veb->uplink_seid;
14615 		if (veb->uplink_seid == pf->mac_seid)
14616 			vsi->veb_idx = I40E_NO_VEB;
14617 		else
14618 			vsi->veb_idx = veb->veb_idx;
14619 	} else {
14620 		/* floating VEB */
14621 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14622 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14623 	}
14624 
14625 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14626 	i40e_veb_clear(veb);
14627 }
14628 
14629 /**
14630  * i40e_add_veb - create the VEB in the switch
14631  * @veb: the VEB to be instantiated
14632  * @vsi: the controlling VSI
14633  **/
14634 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14635 {
14636 	struct i40e_pf *pf = veb->pf;
14637 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14638 	int ret;
14639 
14640 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14641 			      veb->enabled_tc, false,
14642 			      &veb->seid, enable_stats, NULL);
14643 
14644 	/* get a VEB from the hardware */
14645 	if (ret) {
14646 		dev_info(&pf->pdev->dev,
14647 			 "couldn't add VEB, err %s aq_err %s\n",
14648 			 i40e_stat_str(&pf->hw, ret),
14649 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14650 		return -EPERM;
14651 	}
14652 
14653 	/* get statistics counter */
14654 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14655 					 &veb->stats_idx, NULL, NULL, NULL);
14656 	if (ret) {
14657 		dev_info(&pf->pdev->dev,
14658 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14659 			 i40e_stat_str(&pf->hw, ret),
14660 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14661 		return -EPERM;
14662 	}
14663 	ret = i40e_veb_get_bw_info(veb);
14664 	if (ret) {
14665 		dev_info(&pf->pdev->dev,
14666 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14667 			 i40e_stat_str(&pf->hw, ret),
14668 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14669 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14670 		return -ENOENT;
14671 	}
14672 
14673 	vsi->uplink_seid = veb->seid;
14674 	vsi->veb_idx = veb->idx;
14675 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14676 
14677 	return 0;
14678 }
14679 
14680 /**
14681  * i40e_veb_setup - Set up a VEB
14682  * @pf: board private structure
14683  * @flags: VEB setup flags
14684  * @uplink_seid: the switch element to link to
14685  * @vsi_seid: the initial VSI seid
14686  * @enabled_tc: Enabled TC bit-map
14687  *
14688  * This allocates the sw VEB structure and links it into the switch
14689  * It is possible and legal for this to be a duplicate of an already
14690  * existing VEB.  It is also possible for both uplink and vsi seids
14691  * to be zero, in order to create a floating VEB.
14692  *
14693  * Returns pointer to the successfully allocated VEB sw struct on
14694  * success, otherwise returns NULL on failure.
14695  **/
14696 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14697 				u16 uplink_seid, u16 vsi_seid,
14698 				u8 enabled_tc)
14699 {
14700 	struct i40e_veb *veb, *uplink_veb = NULL;
14701 	int vsi_idx, veb_idx;
14702 	int ret;
14703 
14704 	/* if one seid is 0, the other must be 0 to create a floating relay */
14705 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14706 	    (uplink_seid + vsi_seid != 0)) {
14707 		dev_info(&pf->pdev->dev,
14708 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14709 			 uplink_seid, vsi_seid);
14710 		return NULL;
14711 	}
14712 
14713 	/* make sure there is such a vsi and uplink */
14714 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14715 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14716 			break;
14717 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14718 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14719 			 vsi_seid);
14720 		return NULL;
14721 	}
14722 
14723 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14724 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14725 			if (pf->veb[veb_idx] &&
14726 			    pf->veb[veb_idx]->seid == uplink_seid) {
14727 				uplink_veb = pf->veb[veb_idx];
14728 				break;
14729 			}
14730 		}
14731 		if (!uplink_veb) {
14732 			dev_info(&pf->pdev->dev,
14733 				 "uplink seid %d not found\n", uplink_seid);
14734 			return NULL;
14735 		}
14736 	}
14737 
14738 	/* get veb sw struct */
14739 	veb_idx = i40e_veb_mem_alloc(pf);
14740 	if (veb_idx < 0)
14741 		goto err_alloc;
14742 	veb = pf->veb[veb_idx];
14743 	veb->flags = flags;
14744 	veb->uplink_seid = uplink_seid;
14745 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14746 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14747 
14748 	/* create the VEB in the switch */
14749 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14750 	if (ret)
14751 		goto err_veb;
14752 	if (vsi_idx == pf->lan_vsi)
14753 		pf->lan_veb = veb->idx;
14754 
14755 	return veb;
14756 
14757 err_veb:
14758 	i40e_veb_clear(veb);
14759 err_alloc:
14760 	return NULL;
14761 }
14762 
14763 /**
14764  * i40e_setup_pf_switch_element - set PF vars based on switch type
14765  * @pf: board private structure
14766  * @ele: element we are building info from
14767  * @num_reported: total number of elements
14768  * @printconfig: should we print the contents
14769  *
14770  * helper function to assist in extracting a few useful SEID values.
14771  **/
14772 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14773 				struct i40e_aqc_switch_config_element_resp *ele,
14774 				u16 num_reported, bool printconfig)
14775 {
14776 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14777 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14778 	u8 element_type = ele->element_type;
14779 	u16 seid = le16_to_cpu(ele->seid);
14780 
14781 	if (printconfig)
14782 		dev_info(&pf->pdev->dev,
14783 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14784 			 element_type, seid, uplink_seid, downlink_seid);
14785 
14786 	switch (element_type) {
14787 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14788 		pf->mac_seid = seid;
14789 		break;
14790 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14791 		/* Main VEB? */
14792 		if (uplink_seid != pf->mac_seid)
14793 			break;
14794 		if (pf->lan_veb >= I40E_MAX_VEB) {
14795 			int v;
14796 
14797 			/* find existing or else empty VEB */
14798 			for (v = 0; v < I40E_MAX_VEB; v++) {
14799 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14800 					pf->lan_veb = v;
14801 					break;
14802 				}
14803 			}
14804 			if (pf->lan_veb >= I40E_MAX_VEB) {
14805 				v = i40e_veb_mem_alloc(pf);
14806 				if (v < 0)
14807 					break;
14808 				pf->lan_veb = v;
14809 			}
14810 		}
14811 		if (pf->lan_veb >= I40E_MAX_VEB)
14812 			break;
14813 
14814 		pf->veb[pf->lan_veb]->seid = seid;
14815 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14816 		pf->veb[pf->lan_veb]->pf = pf;
14817 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14818 		break;
14819 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14820 		if (num_reported != 1)
14821 			break;
14822 		/* This is immediately after a reset so we can assume this is
14823 		 * the PF's VSI
14824 		 */
14825 		pf->mac_seid = uplink_seid;
14826 		pf->pf_seid = downlink_seid;
14827 		pf->main_vsi_seid = seid;
14828 		if (printconfig)
14829 			dev_info(&pf->pdev->dev,
14830 				 "pf_seid=%d main_vsi_seid=%d\n",
14831 				 pf->pf_seid, pf->main_vsi_seid);
14832 		break;
14833 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14834 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14835 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14836 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14837 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14838 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14839 		/* ignore these for now */
14840 		break;
14841 	default:
14842 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14843 			 element_type, seid);
14844 		break;
14845 	}
14846 }
14847 
14848 /**
14849  * i40e_fetch_switch_configuration - Get switch config from firmware
14850  * @pf: board private structure
14851  * @printconfig: should we print the contents
14852  *
14853  * Get the current switch configuration from the device and
14854  * extract a few useful SEID values.
14855  **/
14856 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14857 {
14858 	struct i40e_aqc_get_switch_config_resp *sw_config;
14859 	u16 next_seid = 0;
14860 	int ret = 0;
14861 	u8 *aq_buf;
14862 	int i;
14863 
14864 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14865 	if (!aq_buf)
14866 		return -ENOMEM;
14867 
14868 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14869 	do {
14870 		u16 num_reported, num_total;
14871 
14872 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14873 						I40E_AQ_LARGE_BUF,
14874 						&next_seid, NULL);
14875 		if (ret) {
14876 			dev_info(&pf->pdev->dev,
14877 				 "get switch config failed err %s aq_err %s\n",
14878 				 i40e_stat_str(&pf->hw, ret),
14879 				 i40e_aq_str(&pf->hw,
14880 					     pf->hw.aq.asq_last_status));
14881 			kfree(aq_buf);
14882 			return -ENOENT;
14883 		}
14884 
14885 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14886 		num_total = le16_to_cpu(sw_config->header.num_total);
14887 
14888 		if (printconfig)
14889 			dev_info(&pf->pdev->dev,
14890 				 "header: %d reported %d total\n",
14891 				 num_reported, num_total);
14892 
14893 		for (i = 0; i < num_reported; i++) {
14894 			struct i40e_aqc_switch_config_element_resp *ele =
14895 				&sw_config->element[i];
14896 
14897 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14898 						     printconfig);
14899 		}
14900 	} while (next_seid != 0);
14901 
14902 	kfree(aq_buf);
14903 	return ret;
14904 }
14905 
14906 /**
14907  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14908  * @pf: board private structure
14909  * @reinit: if the Main VSI needs to re-initialized.
14910  * @lock_acquired: indicates whether or not the lock has been acquired
14911  *
14912  * Returns 0 on success, negative value on failure
14913  **/
14914 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14915 {
14916 	u16 flags = 0;
14917 	int ret;
14918 
14919 	/* find out what's out there already */
14920 	ret = i40e_fetch_switch_configuration(pf, false);
14921 	if (ret) {
14922 		dev_info(&pf->pdev->dev,
14923 			 "couldn't fetch switch config, err %s aq_err %s\n",
14924 			 i40e_stat_str(&pf->hw, ret),
14925 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14926 		return ret;
14927 	}
14928 	i40e_pf_reset_stats(pf);
14929 
14930 	/* set the switch config bit for the whole device to
14931 	 * support limited promisc or true promisc
14932 	 * when user requests promisc. The default is limited
14933 	 * promisc.
14934 	*/
14935 
14936 	if ((pf->hw.pf_id == 0) &&
14937 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14938 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14939 		pf->last_sw_conf_flags = flags;
14940 	}
14941 
14942 	if (pf->hw.pf_id == 0) {
14943 		u16 valid_flags;
14944 
14945 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14946 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14947 						NULL);
14948 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14949 			dev_info(&pf->pdev->dev,
14950 				 "couldn't set switch config bits, err %s aq_err %s\n",
14951 				 i40e_stat_str(&pf->hw, ret),
14952 				 i40e_aq_str(&pf->hw,
14953 					     pf->hw.aq.asq_last_status));
14954 			/* not a fatal problem, just keep going */
14955 		}
14956 		pf->last_sw_conf_valid_flags = valid_flags;
14957 	}
14958 
14959 	/* first time setup */
14960 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14961 		struct i40e_vsi *vsi = NULL;
14962 		u16 uplink_seid;
14963 
14964 		/* Set up the PF VSI associated with the PF's main VSI
14965 		 * that is already in the HW switch
14966 		 */
14967 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14968 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14969 		else
14970 			uplink_seid = pf->mac_seid;
14971 		if (pf->lan_vsi == I40E_NO_VSI)
14972 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14973 		else if (reinit)
14974 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14975 		if (!vsi) {
14976 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14977 			i40e_cloud_filter_exit(pf);
14978 			i40e_fdir_teardown(pf);
14979 			return -EAGAIN;
14980 		}
14981 	} else {
14982 		/* force a reset of TC and queue layout configurations */
14983 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14984 
14985 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14986 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14987 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14988 	}
14989 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14990 
14991 	i40e_fdir_sb_setup(pf);
14992 
14993 	/* Setup static PF queue filter control settings */
14994 	ret = i40e_setup_pf_filter_control(pf);
14995 	if (ret) {
14996 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14997 			 ret);
14998 		/* Failure here should not stop continuing other steps */
14999 	}
15000 
15001 	/* enable RSS in the HW, even for only one queue, as the stack can use
15002 	 * the hash
15003 	 */
15004 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15005 		i40e_pf_config_rss(pf);
15006 
15007 	/* fill in link information and enable LSE reporting */
15008 	i40e_link_event(pf);
15009 
15010 	/* Initialize user-specific link properties */
15011 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15012 				  I40E_AQ_AN_COMPLETED) ? true : false);
15013 
15014 	i40e_ptp_init(pf);
15015 
15016 	if (!lock_acquired)
15017 		rtnl_lock();
15018 
15019 	/* repopulate tunnel port filters */
15020 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15021 
15022 	if (!lock_acquired)
15023 		rtnl_unlock();
15024 
15025 	return ret;
15026 }
15027 
15028 /**
15029  * i40e_determine_queue_usage - Work out queue distribution
15030  * @pf: board private structure
15031  **/
15032 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15033 {
15034 	int queues_left;
15035 	int q_max;
15036 
15037 	pf->num_lan_qps = 0;
15038 
15039 	/* Find the max queues to be put into basic use.  We'll always be
15040 	 * using TC0, whether or not DCB is running, and TC0 will get the
15041 	 * big RSS set.
15042 	 */
15043 	queues_left = pf->hw.func_caps.num_tx_qp;
15044 
15045 	if ((queues_left == 1) ||
15046 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15047 		/* one qp for PF, no queues for anything else */
15048 		queues_left = 0;
15049 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15050 
15051 		/* make sure all the fancies are disabled */
15052 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15053 			       I40E_FLAG_IWARP_ENABLED	|
15054 			       I40E_FLAG_FD_SB_ENABLED	|
15055 			       I40E_FLAG_FD_ATR_ENABLED	|
15056 			       I40E_FLAG_DCB_CAPABLE	|
15057 			       I40E_FLAG_DCB_ENABLED	|
15058 			       I40E_FLAG_SRIOV_ENABLED	|
15059 			       I40E_FLAG_VMDQ_ENABLED);
15060 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15061 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15062 				  I40E_FLAG_FD_SB_ENABLED |
15063 				  I40E_FLAG_FD_ATR_ENABLED |
15064 				  I40E_FLAG_DCB_CAPABLE))) {
15065 		/* one qp for PF */
15066 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15067 		queues_left -= pf->num_lan_qps;
15068 
15069 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15070 			       I40E_FLAG_IWARP_ENABLED	|
15071 			       I40E_FLAG_FD_SB_ENABLED	|
15072 			       I40E_FLAG_FD_ATR_ENABLED	|
15073 			       I40E_FLAG_DCB_ENABLED	|
15074 			       I40E_FLAG_VMDQ_ENABLED);
15075 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15076 	} else {
15077 		/* Not enough queues for all TCs */
15078 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15079 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15080 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15081 					I40E_FLAG_DCB_ENABLED);
15082 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15083 		}
15084 
15085 		/* limit lan qps to the smaller of qps, cpus or msix */
15086 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15087 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15088 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15089 		pf->num_lan_qps = q_max;
15090 
15091 		queues_left -= pf->num_lan_qps;
15092 	}
15093 
15094 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15095 		if (queues_left > 1) {
15096 			queues_left -= 1; /* save 1 queue for FD */
15097 		} else {
15098 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15099 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15100 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15101 		}
15102 	}
15103 
15104 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15105 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15106 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15107 					(queues_left / pf->num_vf_qps));
15108 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15109 	}
15110 
15111 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15112 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15113 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15114 					  (queues_left / pf->num_vmdq_qps));
15115 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15116 	}
15117 
15118 	pf->queues_left = queues_left;
15119 	dev_dbg(&pf->pdev->dev,
15120 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15121 		pf->hw.func_caps.num_tx_qp,
15122 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15123 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15124 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15125 		queues_left);
15126 }
15127 
15128 /**
15129  * i40e_setup_pf_filter_control - Setup PF static filter control
15130  * @pf: PF to be setup
15131  *
15132  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15133  * settings. If PE/FCoE are enabled then it will also set the per PF
15134  * based filter sizes required for them. It also enables Flow director,
15135  * ethertype and macvlan type filter settings for the pf.
15136  *
15137  * Returns 0 on success, negative on failure
15138  **/
15139 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15140 {
15141 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15142 
15143 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15144 
15145 	/* Flow Director is enabled */
15146 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15147 		settings->enable_fdir = true;
15148 
15149 	/* Ethtype and MACVLAN filters enabled for PF */
15150 	settings->enable_ethtype = true;
15151 	settings->enable_macvlan = true;
15152 
15153 	if (i40e_set_filter_control(&pf->hw, settings))
15154 		return -ENOENT;
15155 
15156 	return 0;
15157 }
15158 
15159 #define INFO_STRING_LEN 255
15160 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15161 static void i40e_print_features(struct i40e_pf *pf)
15162 {
15163 	struct i40e_hw *hw = &pf->hw;
15164 	char *buf;
15165 	int i;
15166 
15167 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15168 	if (!buf)
15169 		return;
15170 
15171 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15172 #ifdef CONFIG_PCI_IOV
15173 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15174 #endif
15175 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15176 		      pf->hw.func_caps.num_vsis,
15177 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15178 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15179 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15180 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15181 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15182 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15183 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15184 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15185 	}
15186 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15187 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15188 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15189 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15190 	if (pf->flags & I40E_FLAG_PTP)
15191 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15192 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15193 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15194 	else
15195 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15196 
15197 	dev_info(&pf->pdev->dev, "%s\n", buf);
15198 	kfree(buf);
15199 	WARN_ON(i > INFO_STRING_LEN);
15200 }
15201 
15202 /**
15203  * i40e_get_platform_mac_addr - get platform-specific MAC address
15204  * @pdev: PCI device information struct
15205  * @pf: board private structure
15206  *
15207  * Look up the MAC address for the device. First we'll try
15208  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15209  * specific fallback. Otherwise, we'll default to the stored value in
15210  * firmware.
15211  **/
15212 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15213 {
15214 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15215 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15216 }
15217 
15218 /**
15219  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15220  * @fec_cfg: FEC option to set in flags
15221  * @flags: ptr to flags in which we set FEC option
15222  **/
15223 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15224 {
15225 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15226 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15227 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15228 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15229 		*flags |= I40E_FLAG_RS_FEC;
15230 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15231 	}
15232 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15233 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15234 		*flags |= I40E_FLAG_BASE_R_FEC;
15235 		*flags &= ~I40E_FLAG_RS_FEC;
15236 	}
15237 	if (fec_cfg == 0)
15238 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15239 }
15240 
15241 /**
15242  * i40e_check_recovery_mode - check if we are running transition firmware
15243  * @pf: board private structure
15244  *
15245  * Check registers indicating the firmware runs in recovery mode. Sets the
15246  * appropriate driver state.
15247  *
15248  * Returns true if the recovery mode was detected, false otherwise
15249  **/
15250 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15251 {
15252 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15253 
15254 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15255 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15256 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15257 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15258 
15259 		return true;
15260 	}
15261 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15262 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15263 
15264 	return false;
15265 }
15266 
15267 /**
15268  * i40e_pf_loop_reset - perform reset in a loop.
15269  * @pf: board private structure
15270  *
15271  * This function is useful when a NIC is about to enter recovery mode.
15272  * When a NIC's internal data structures are corrupted the NIC's
15273  * firmware is going to enter recovery mode.
15274  * Right after a POR it takes about 7 minutes for firmware to enter
15275  * recovery mode. Until that time a NIC is in some kind of intermediate
15276  * state. After that time period the NIC almost surely enters
15277  * recovery mode. The only way for a driver to detect intermediate
15278  * state is to issue a series of pf-resets and check a return value.
15279  * If a PF reset returns success then the firmware could be in recovery
15280  * mode so the caller of this code needs to check for recovery mode
15281  * if this function returns success. There is a little chance that
15282  * firmware will hang in intermediate state forever.
15283  * Since waiting 7 minutes is quite a lot of time this function waits
15284  * 10 seconds and then gives up by returning an error.
15285  *
15286  * Return 0 on success, negative on failure.
15287  **/
15288 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15289 {
15290 	/* wait max 10 seconds for PF reset to succeed */
15291 	const unsigned long time_end = jiffies + 10 * HZ;
15292 
15293 	struct i40e_hw *hw = &pf->hw;
15294 	i40e_status ret;
15295 
15296 	ret = i40e_pf_reset(hw);
15297 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15298 		usleep_range(10000, 20000);
15299 		ret = i40e_pf_reset(hw);
15300 	}
15301 
15302 	if (ret == I40E_SUCCESS)
15303 		pf->pfr_count++;
15304 	else
15305 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15306 
15307 	return ret;
15308 }
15309 
15310 /**
15311  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15312  * @pf: board private structure
15313  *
15314  * Check FW registers to determine if FW issued unexpected EMP Reset.
15315  * Every time when unexpected EMP Reset occurs the FW increments
15316  * a counter of unexpected EMP Resets. When the counter reaches 10
15317  * the FW should enter the Recovery mode
15318  *
15319  * Returns true if FW issued unexpected EMP Reset
15320  **/
15321 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15322 {
15323 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15324 			   I40E_GL_FWSTS_FWS1B_MASK;
15325 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15326 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15327 }
15328 
15329 /**
15330  * i40e_handle_resets - handle EMP resets and PF resets
15331  * @pf: board private structure
15332  *
15333  * Handle both EMP resets and PF resets and conclude whether there are
15334  * any issues regarding these resets. If there are any issues then
15335  * generate log entry.
15336  *
15337  * Return 0 if NIC is healthy or negative value when there are issues
15338  * with resets
15339  **/
15340 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15341 {
15342 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15343 	const bool is_empr = i40e_check_fw_empr(pf);
15344 
15345 	if (is_empr || pfr != I40E_SUCCESS)
15346 		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");
15347 
15348 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15349 }
15350 
15351 /**
15352  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15353  * @pf: board private structure
15354  * @hw: ptr to the hardware info
15355  *
15356  * This function does a minimal setup of all subsystems needed for running
15357  * recovery mode.
15358  *
15359  * Returns 0 on success, negative on failure
15360  **/
15361 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15362 {
15363 	struct i40e_vsi *vsi;
15364 	int err;
15365 	int v_idx;
15366 
15367 	pci_save_state(pf->pdev);
15368 
15369 	/* set up periodic task facility */
15370 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15371 	pf->service_timer_period = HZ;
15372 
15373 	INIT_WORK(&pf->service_task, i40e_service_task);
15374 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15375 
15376 	err = i40e_init_interrupt_scheme(pf);
15377 	if (err)
15378 		goto err_switch_setup;
15379 
15380 	/* The number of VSIs reported by the FW is the minimum guaranteed
15381 	 * to us; HW supports far more and we share the remaining pool with
15382 	 * the other PFs. We allocate space for more than the guarantee with
15383 	 * the understanding that we might not get them all later.
15384 	 */
15385 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15386 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15387 	else
15388 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15389 
15390 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15391 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15392 			  GFP_KERNEL);
15393 	if (!pf->vsi) {
15394 		err = -ENOMEM;
15395 		goto err_switch_setup;
15396 	}
15397 
15398 	/* We allocate one VSI which is needed as absolute minimum
15399 	 * in order to register the netdev
15400 	 */
15401 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15402 	if (v_idx < 0) {
15403 		err = v_idx;
15404 		goto err_switch_setup;
15405 	}
15406 	pf->lan_vsi = v_idx;
15407 	vsi = pf->vsi[v_idx];
15408 	if (!vsi) {
15409 		err = -EFAULT;
15410 		goto err_switch_setup;
15411 	}
15412 	vsi->alloc_queue_pairs = 1;
15413 	err = i40e_config_netdev(vsi);
15414 	if (err)
15415 		goto err_switch_setup;
15416 	err = register_netdev(vsi->netdev);
15417 	if (err)
15418 		goto err_switch_setup;
15419 	vsi->netdev_registered = true;
15420 	i40e_dbg_pf_init(pf);
15421 
15422 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15423 	if (err)
15424 		goto err_switch_setup;
15425 
15426 	/* tell the firmware that we're starting */
15427 	i40e_send_version(pf);
15428 
15429 	/* since everything's happy, start the service_task timer */
15430 	mod_timer(&pf->service_timer,
15431 		  round_jiffies(jiffies + pf->service_timer_period));
15432 
15433 	return 0;
15434 
15435 err_switch_setup:
15436 	i40e_reset_interrupt_capability(pf);
15437 	del_timer_sync(&pf->service_timer);
15438 	i40e_shutdown_adminq(hw);
15439 	iounmap(hw->hw_addr);
15440 	pci_disable_pcie_error_reporting(pf->pdev);
15441 	pci_release_mem_regions(pf->pdev);
15442 	pci_disable_device(pf->pdev);
15443 	kfree(pf);
15444 
15445 	return err;
15446 }
15447 
15448 /**
15449  * i40e_set_subsystem_device_id - set subsystem device id
15450  * @hw: pointer to the hardware info
15451  *
15452  * Set PCI subsystem device id either from a pci_dev structure or
15453  * a specific FW register.
15454  **/
15455 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15456 {
15457 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15458 
15459 	hw->subsystem_device_id = pdev->subsystem_device ?
15460 		pdev->subsystem_device :
15461 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15462 }
15463 
15464 /**
15465  * i40e_probe - Device initialization routine
15466  * @pdev: PCI device information struct
15467  * @ent: entry in i40e_pci_tbl
15468  *
15469  * i40e_probe initializes a PF identified by a pci_dev structure.
15470  * The OS initialization, configuring of the PF private structure,
15471  * and a hardware reset occur.
15472  *
15473  * Returns 0 on success, negative on failure
15474  **/
15475 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15476 {
15477 	struct i40e_aq_get_phy_abilities_resp abilities;
15478 #ifdef CONFIG_I40E_DCB
15479 	enum i40e_get_fw_lldp_status_resp lldp_status;
15480 	i40e_status status;
15481 #endif /* CONFIG_I40E_DCB */
15482 	struct i40e_pf *pf;
15483 	struct i40e_hw *hw;
15484 	static u16 pfs_found;
15485 	u16 wol_nvm_bits;
15486 	u16 link_status;
15487 	int err;
15488 	u32 val;
15489 	u32 i;
15490 
15491 	err = pci_enable_device_mem(pdev);
15492 	if (err)
15493 		return err;
15494 
15495 	/* set up for high or low dma */
15496 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15497 	if (err) {
15498 		dev_err(&pdev->dev,
15499 			"DMA configuration failed: 0x%x\n", err);
15500 		goto err_dma;
15501 	}
15502 
15503 	/* set up pci connections */
15504 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15505 	if (err) {
15506 		dev_info(&pdev->dev,
15507 			 "pci_request_selected_regions failed %d\n", err);
15508 		goto err_pci_reg;
15509 	}
15510 
15511 	pci_enable_pcie_error_reporting(pdev);
15512 	pci_set_master(pdev);
15513 
15514 	/* Now that we have a PCI connection, we need to do the
15515 	 * low level device setup.  This is primarily setting up
15516 	 * the Admin Queue structures and then querying for the
15517 	 * device's current profile information.
15518 	 */
15519 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15520 	if (!pf) {
15521 		err = -ENOMEM;
15522 		goto err_pf_alloc;
15523 	}
15524 	pf->next_vsi = 0;
15525 	pf->pdev = pdev;
15526 	set_bit(__I40E_DOWN, pf->state);
15527 
15528 	hw = &pf->hw;
15529 	hw->back = pf;
15530 
15531 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15532 				I40E_MAX_CSR_SPACE);
15533 	/* We believe that the highest register to read is
15534 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15535 	 * is not less than that before mapping to prevent a
15536 	 * kernel panic.
15537 	 */
15538 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15539 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15540 			pf->ioremap_len);
15541 		err = -ENOMEM;
15542 		goto err_ioremap;
15543 	}
15544 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15545 	if (!hw->hw_addr) {
15546 		err = -EIO;
15547 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15548 			 (unsigned int)pci_resource_start(pdev, 0),
15549 			 pf->ioremap_len, err);
15550 		goto err_ioremap;
15551 	}
15552 	hw->vendor_id = pdev->vendor;
15553 	hw->device_id = pdev->device;
15554 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15555 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15556 	i40e_set_subsystem_device_id(hw);
15557 	hw->bus.device = PCI_SLOT(pdev->devfn);
15558 	hw->bus.func = PCI_FUNC(pdev->devfn);
15559 	hw->bus.bus_id = pdev->bus->number;
15560 	pf->instance = pfs_found;
15561 
15562 	/* Select something other than the 802.1ad ethertype for the
15563 	 * switch to use internally and drop on ingress.
15564 	 */
15565 	hw->switch_tag = 0xffff;
15566 	hw->first_tag = ETH_P_8021AD;
15567 	hw->second_tag = ETH_P_8021Q;
15568 
15569 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15570 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15571 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15572 
15573 	/* set up the locks for the AQ, do this only once in probe
15574 	 * and destroy them only once in remove
15575 	 */
15576 	mutex_init(&hw->aq.asq_mutex);
15577 	mutex_init(&hw->aq.arq_mutex);
15578 
15579 	pf->msg_enable = netif_msg_init(debug,
15580 					NETIF_MSG_DRV |
15581 					NETIF_MSG_PROBE |
15582 					NETIF_MSG_LINK);
15583 	if (debug < -1)
15584 		pf->hw.debug_mask = debug;
15585 
15586 	/* do a special CORER for clearing PXE mode once at init */
15587 	if (hw->revision_id == 0 &&
15588 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15589 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15590 		i40e_flush(hw);
15591 		msleep(200);
15592 		pf->corer_count++;
15593 
15594 		i40e_clear_pxe_mode(hw);
15595 	}
15596 
15597 	/* Reset here to make sure all is clean and to define PF 'n' */
15598 	i40e_clear_hw(hw);
15599 
15600 	err = i40e_set_mac_type(hw);
15601 	if (err) {
15602 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15603 			 err);
15604 		goto err_pf_reset;
15605 	}
15606 
15607 	err = i40e_handle_resets(pf);
15608 	if (err)
15609 		goto err_pf_reset;
15610 
15611 	i40e_check_recovery_mode(pf);
15612 
15613 	if (is_kdump_kernel()) {
15614 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15615 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15616 	} else {
15617 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15618 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15619 	}
15620 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15621 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15622 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15623 
15624 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15625 		 "%s-%s:misc",
15626 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15627 
15628 	err = i40e_init_shared_code(hw);
15629 	if (err) {
15630 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15631 			 err);
15632 		goto err_pf_reset;
15633 	}
15634 
15635 	/* set up a default setting for link flow control */
15636 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15637 
15638 	err = i40e_init_adminq(hw);
15639 	if (err) {
15640 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15641 			dev_info(&pdev->dev,
15642 				 "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",
15643 				 hw->aq.api_maj_ver,
15644 				 hw->aq.api_min_ver,
15645 				 I40E_FW_API_VERSION_MAJOR,
15646 				 I40E_FW_MINOR_VERSION(hw));
15647 		else
15648 			dev_info(&pdev->dev,
15649 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15650 
15651 		goto err_pf_reset;
15652 	}
15653 	i40e_get_oem_version(hw);
15654 
15655 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15656 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15657 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15658 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15659 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15660 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15661 
15662 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15663 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15664 		dev_dbg(&pdev->dev,
15665 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15666 			 hw->aq.api_maj_ver,
15667 			 hw->aq.api_min_ver,
15668 			 I40E_FW_API_VERSION_MAJOR,
15669 			 I40E_FW_MINOR_VERSION(hw));
15670 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15671 		dev_info(&pdev->dev,
15672 			 "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",
15673 			 hw->aq.api_maj_ver,
15674 			 hw->aq.api_min_ver,
15675 			 I40E_FW_API_VERSION_MAJOR,
15676 			 I40E_FW_MINOR_VERSION(hw));
15677 
15678 	i40e_verify_eeprom(pf);
15679 
15680 	/* Rev 0 hardware was never productized */
15681 	if (hw->revision_id < 1)
15682 		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");
15683 
15684 	i40e_clear_pxe_mode(hw);
15685 
15686 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15687 	if (err)
15688 		goto err_adminq_setup;
15689 
15690 	err = i40e_sw_init(pf);
15691 	if (err) {
15692 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15693 		goto err_sw_init;
15694 	}
15695 
15696 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15697 		return i40e_init_recovery_mode(pf, hw);
15698 
15699 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15700 				hw->func_caps.num_rx_qp, 0, 0);
15701 	if (err) {
15702 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15703 		goto err_init_lan_hmc;
15704 	}
15705 
15706 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15707 	if (err) {
15708 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15709 		err = -ENOENT;
15710 		goto err_configure_lan_hmc;
15711 	}
15712 
15713 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15714 	 * Ignore error return codes because if it was already disabled via
15715 	 * hardware settings this will fail
15716 	 */
15717 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15718 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15719 		i40e_aq_stop_lldp(hw, true, false, NULL);
15720 	}
15721 
15722 	/* allow a platform config to override the HW addr */
15723 	i40e_get_platform_mac_addr(pdev, pf);
15724 
15725 	if (!is_valid_ether_addr(hw->mac.addr)) {
15726 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15727 		err = -EIO;
15728 		goto err_mac_addr;
15729 	}
15730 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15731 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15732 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15733 	if (is_valid_ether_addr(hw->mac.port_addr))
15734 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15735 
15736 	i40e_ptp_alloc_pins(pf);
15737 	pci_set_drvdata(pdev, pf);
15738 	pci_save_state(pdev);
15739 
15740 #ifdef CONFIG_I40E_DCB
15741 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15742 	(!status &&
15743 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15744 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15745 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15746 	dev_info(&pdev->dev,
15747 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15748 			"FW LLDP is disabled\n" :
15749 			"FW LLDP is enabled\n");
15750 
15751 	/* Enable FW to write default DCB config on link-up */
15752 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15753 
15754 	err = i40e_init_pf_dcb(pf);
15755 	if (err) {
15756 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15757 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15758 		/* Continue without DCB enabled */
15759 	}
15760 #endif /* CONFIG_I40E_DCB */
15761 
15762 	/* set up periodic task facility */
15763 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15764 	pf->service_timer_period = HZ;
15765 
15766 	INIT_WORK(&pf->service_task, i40e_service_task);
15767 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15768 
15769 	/* NVM bit on means WoL disabled for the port */
15770 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15771 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15772 		pf->wol_en = false;
15773 	else
15774 		pf->wol_en = true;
15775 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15776 
15777 	/* set up the main switch operations */
15778 	i40e_determine_queue_usage(pf);
15779 	err = i40e_init_interrupt_scheme(pf);
15780 	if (err)
15781 		goto err_switch_setup;
15782 
15783 	/* Reduce Tx and Rx pairs for kdump
15784 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15785 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15786 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15787 	 */
15788 	if (is_kdump_kernel())
15789 		pf->num_lan_msix = 1;
15790 
15791 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15792 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15793 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15794 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15795 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15796 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15797 						    UDP_TUNNEL_TYPE_GENEVE;
15798 
15799 	/* The number of VSIs reported by the FW is the minimum guaranteed
15800 	 * to us; HW supports far more and we share the remaining pool with
15801 	 * the other PFs. We allocate space for more than the guarantee with
15802 	 * the understanding that we might not get them all later.
15803 	 */
15804 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15805 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15806 	else
15807 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15808 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15809 		dev_warn(&pf->pdev->dev,
15810 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15811 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15812 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15813 	}
15814 
15815 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15816 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15817 			  GFP_KERNEL);
15818 	if (!pf->vsi) {
15819 		err = -ENOMEM;
15820 		goto err_switch_setup;
15821 	}
15822 
15823 #ifdef CONFIG_PCI_IOV
15824 	/* prep for VF support */
15825 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15826 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15827 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15828 		if (pci_num_vf(pdev))
15829 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15830 	}
15831 #endif
15832 	err = i40e_setup_pf_switch(pf, false, false);
15833 	if (err) {
15834 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15835 		goto err_vsis;
15836 	}
15837 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15838 
15839 	/* if FDIR VSI was set up, start it now */
15840 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15841 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15842 			i40e_vsi_open(pf->vsi[i]);
15843 			break;
15844 		}
15845 	}
15846 
15847 	/* The driver only wants link up/down and module qualification
15848 	 * reports from firmware.  Note the negative logic.
15849 	 */
15850 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15851 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15852 					 I40E_AQ_EVENT_MEDIA_NA |
15853 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15854 	if (err)
15855 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15856 			 i40e_stat_str(&pf->hw, err),
15857 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15858 
15859 	/* Reconfigure hardware for allowing smaller MSS in the case
15860 	 * of TSO, so that we avoid the MDD being fired and causing
15861 	 * a reset in the case of small MSS+TSO.
15862 	 */
15863 	val = rd32(hw, I40E_REG_MSS);
15864 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15865 		val &= ~I40E_REG_MSS_MIN_MASK;
15866 		val |= I40E_64BYTE_MSS;
15867 		wr32(hw, I40E_REG_MSS, val);
15868 	}
15869 
15870 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15871 		msleep(75);
15872 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15873 		if (err)
15874 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15875 				 i40e_stat_str(&pf->hw, err),
15876 				 i40e_aq_str(&pf->hw,
15877 					     pf->hw.aq.asq_last_status));
15878 	}
15879 	/* The main driver is (mostly) up and happy. We need to set this state
15880 	 * before setting up the misc vector or we get a race and the vector
15881 	 * ends up disabled forever.
15882 	 */
15883 	clear_bit(__I40E_DOWN, pf->state);
15884 
15885 	/* In case of MSIX we are going to setup the misc vector right here
15886 	 * to handle admin queue events etc. In case of legacy and MSI
15887 	 * the misc functionality and queue processing is combined in
15888 	 * the same vector and that gets setup at open.
15889 	 */
15890 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15891 		err = i40e_setup_misc_vector(pf);
15892 		if (err) {
15893 			dev_info(&pdev->dev,
15894 				 "setup of misc vector failed: %d\n", err);
15895 			i40e_cloud_filter_exit(pf);
15896 			i40e_fdir_teardown(pf);
15897 			goto err_vsis;
15898 		}
15899 	}
15900 
15901 #ifdef CONFIG_PCI_IOV
15902 	/* prep for VF support */
15903 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15904 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15905 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15906 		/* disable link interrupts for VFs */
15907 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15908 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15909 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15910 		i40e_flush(hw);
15911 
15912 		if (pci_num_vf(pdev)) {
15913 			dev_info(&pdev->dev,
15914 				 "Active VFs found, allocating resources.\n");
15915 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15916 			if (err)
15917 				dev_info(&pdev->dev,
15918 					 "Error %d allocating resources for existing VFs\n",
15919 					 err);
15920 		}
15921 	}
15922 #endif /* CONFIG_PCI_IOV */
15923 
15924 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15925 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15926 						      pf->num_iwarp_msix,
15927 						      I40E_IWARP_IRQ_PILE_ID);
15928 		if (pf->iwarp_base_vector < 0) {
15929 			dev_info(&pdev->dev,
15930 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15931 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15932 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15933 		}
15934 	}
15935 
15936 	i40e_dbg_pf_init(pf);
15937 
15938 	/* tell the firmware that we're starting */
15939 	i40e_send_version(pf);
15940 
15941 	/* since everything's happy, start the service_task timer */
15942 	mod_timer(&pf->service_timer,
15943 		  round_jiffies(jiffies + pf->service_timer_period));
15944 
15945 	/* add this PF to client device list and launch a client service task */
15946 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15947 		err = i40e_lan_add_device(pf);
15948 		if (err)
15949 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15950 				 err);
15951 	}
15952 
15953 #define PCI_SPEED_SIZE 8
15954 #define PCI_WIDTH_SIZE 8
15955 	/* Devices on the IOSF bus do not have this information
15956 	 * and will report PCI Gen 1 x 1 by default so don't bother
15957 	 * checking them.
15958 	 */
15959 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15960 		char speed[PCI_SPEED_SIZE] = "Unknown";
15961 		char width[PCI_WIDTH_SIZE] = "Unknown";
15962 
15963 		/* Get the negotiated link width and speed from PCI config
15964 		 * space
15965 		 */
15966 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15967 					  &link_status);
15968 
15969 		i40e_set_pci_config_data(hw, link_status);
15970 
15971 		switch (hw->bus.speed) {
15972 		case i40e_bus_speed_8000:
15973 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15974 		case i40e_bus_speed_5000:
15975 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15976 		case i40e_bus_speed_2500:
15977 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15978 		default:
15979 			break;
15980 		}
15981 		switch (hw->bus.width) {
15982 		case i40e_bus_width_pcie_x8:
15983 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15984 		case i40e_bus_width_pcie_x4:
15985 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15986 		case i40e_bus_width_pcie_x2:
15987 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15988 		case i40e_bus_width_pcie_x1:
15989 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15990 		default:
15991 			break;
15992 		}
15993 
15994 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15995 			 speed, width);
15996 
15997 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15998 		    hw->bus.speed < i40e_bus_speed_8000) {
15999 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16000 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16001 		}
16002 	}
16003 
16004 	/* get the requested speeds from the fw */
16005 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16006 	if (err)
16007 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
16008 			i40e_stat_str(&pf->hw, err),
16009 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16010 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16011 
16012 	/* set the FEC config due to the board capabilities */
16013 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16014 
16015 	/* get the supported phy types from the fw */
16016 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16017 	if (err)
16018 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
16019 			i40e_stat_str(&pf->hw, err),
16020 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16021 
16022 	/* make sure the MFS hasn't been set lower than the default */
16023 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16024 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16025 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16026 	if (val < MAX_FRAME_SIZE_DEFAULT)
16027 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16028 			 i, val);
16029 
16030 	/* Add a filter to drop all Flow control frames from any VSI from being
16031 	 * transmitted. By doing so we stop a malicious VF from sending out
16032 	 * PAUSE or PFC frames and potentially controlling traffic for other
16033 	 * PF/VF VSIs.
16034 	 * The FW can still send Flow control frames if enabled.
16035 	 */
16036 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16037 						       pf->main_vsi_seid);
16038 
16039 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16040 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16041 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16042 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16043 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16044 	/* print a string summarizing features */
16045 	i40e_print_features(pf);
16046 
16047 	return 0;
16048 
16049 	/* Unwind what we've done if something failed in the setup */
16050 err_vsis:
16051 	set_bit(__I40E_DOWN, pf->state);
16052 	i40e_clear_interrupt_scheme(pf);
16053 	kfree(pf->vsi);
16054 err_switch_setup:
16055 	i40e_reset_interrupt_capability(pf);
16056 	del_timer_sync(&pf->service_timer);
16057 err_mac_addr:
16058 err_configure_lan_hmc:
16059 	(void)i40e_shutdown_lan_hmc(hw);
16060 err_init_lan_hmc:
16061 	kfree(pf->qp_pile);
16062 err_sw_init:
16063 err_adminq_setup:
16064 err_pf_reset:
16065 	iounmap(hw->hw_addr);
16066 err_ioremap:
16067 	kfree(pf);
16068 err_pf_alloc:
16069 	pci_disable_pcie_error_reporting(pdev);
16070 	pci_release_mem_regions(pdev);
16071 err_pci_reg:
16072 err_dma:
16073 	pci_disable_device(pdev);
16074 	return err;
16075 }
16076 
16077 /**
16078  * i40e_remove - Device removal routine
16079  * @pdev: PCI device information struct
16080  *
16081  * i40e_remove is called by the PCI subsystem to alert the driver
16082  * that is should release a PCI device.  This could be caused by a
16083  * Hot-Plug event, or because the driver is going to be removed from
16084  * memory.
16085  **/
16086 static void i40e_remove(struct pci_dev *pdev)
16087 {
16088 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16089 	struct i40e_hw *hw = &pf->hw;
16090 	i40e_status ret_code;
16091 	int i;
16092 
16093 	i40e_dbg_pf_exit(pf);
16094 
16095 	i40e_ptp_stop(pf);
16096 
16097 	/* Disable RSS in hw */
16098 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16099 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16100 
16101 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16102 	 * flags, once they are set, i40e_rebuild should not be called as
16103 	 * i40e_prep_for_reset always returns early.
16104 	 */
16105 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16106 		usleep_range(1000, 2000);
16107 	set_bit(__I40E_IN_REMOVE, pf->state);
16108 
16109 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16110 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16111 		i40e_free_vfs(pf);
16112 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16113 	}
16114 	/* no more scheduling of any task */
16115 	set_bit(__I40E_SUSPENDED, pf->state);
16116 	set_bit(__I40E_DOWN, pf->state);
16117 	if (pf->service_timer.function)
16118 		del_timer_sync(&pf->service_timer);
16119 	if (pf->service_task.func)
16120 		cancel_work_sync(&pf->service_task);
16121 
16122 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16123 		struct i40e_vsi *vsi = pf->vsi[0];
16124 
16125 		/* We know that we have allocated only one vsi for this PF,
16126 		 * it was just for registering netdevice, so the interface
16127 		 * could be visible in the 'ifconfig' output
16128 		 */
16129 		unregister_netdev(vsi->netdev);
16130 		free_netdev(vsi->netdev);
16131 
16132 		goto unmap;
16133 	}
16134 
16135 	/* Client close must be called explicitly here because the timer
16136 	 * has been stopped.
16137 	 */
16138 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16139 
16140 	i40e_fdir_teardown(pf);
16141 
16142 	/* If there is a switch structure or any orphans, remove them.
16143 	 * This will leave only the PF's VSI remaining.
16144 	 */
16145 	for (i = 0; i < I40E_MAX_VEB; i++) {
16146 		if (!pf->veb[i])
16147 			continue;
16148 
16149 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16150 		    pf->veb[i]->uplink_seid == 0)
16151 			i40e_switch_branch_release(pf->veb[i]);
16152 	}
16153 
16154 	/* Now we can shutdown the PF's VSI, just before we kill
16155 	 * adminq and hmc.
16156 	 */
16157 	if (pf->vsi[pf->lan_vsi])
16158 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16159 
16160 	i40e_cloud_filter_exit(pf);
16161 
16162 	/* remove attached clients */
16163 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16164 		ret_code = i40e_lan_del_device(pf);
16165 		if (ret_code)
16166 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16167 				 ret_code);
16168 	}
16169 
16170 	/* shutdown and destroy the HMC */
16171 	if (hw->hmc.hmc_obj) {
16172 		ret_code = i40e_shutdown_lan_hmc(hw);
16173 		if (ret_code)
16174 			dev_warn(&pdev->dev,
16175 				 "Failed to destroy the HMC resources: %d\n",
16176 				 ret_code);
16177 	}
16178 
16179 unmap:
16180 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16181 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16182 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16183 		free_irq(pf->pdev->irq, pf);
16184 
16185 	/* shutdown the adminq */
16186 	i40e_shutdown_adminq(hw);
16187 
16188 	/* destroy the locks only once, here */
16189 	mutex_destroy(&hw->aq.arq_mutex);
16190 	mutex_destroy(&hw->aq.asq_mutex);
16191 
16192 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16193 	rtnl_lock();
16194 	i40e_clear_interrupt_scheme(pf);
16195 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16196 		if (pf->vsi[i]) {
16197 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16198 				i40e_vsi_clear_rings(pf->vsi[i]);
16199 			i40e_vsi_clear(pf->vsi[i]);
16200 			pf->vsi[i] = NULL;
16201 		}
16202 	}
16203 	rtnl_unlock();
16204 
16205 	for (i = 0; i < I40E_MAX_VEB; i++) {
16206 		kfree(pf->veb[i]);
16207 		pf->veb[i] = NULL;
16208 	}
16209 
16210 	kfree(pf->qp_pile);
16211 	kfree(pf->vsi);
16212 
16213 	iounmap(hw->hw_addr);
16214 	kfree(pf);
16215 	pci_release_mem_regions(pdev);
16216 
16217 	pci_disable_pcie_error_reporting(pdev);
16218 	pci_disable_device(pdev);
16219 }
16220 
16221 /**
16222  * i40e_pci_error_detected - warning that something funky happened in PCI land
16223  * @pdev: PCI device information struct
16224  * @error: the type of PCI error
16225  *
16226  * Called to warn that something happened and the error handling steps
16227  * are in progress.  Allows the driver to quiesce things, be ready for
16228  * remediation.
16229  **/
16230 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16231 						pci_channel_state_t error)
16232 {
16233 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16234 
16235 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16236 
16237 	if (!pf) {
16238 		dev_info(&pdev->dev,
16239 			 "Cannot recover - error happened during device probe\n");
16240 		return PCI_ERS_RESULT_DISCONNECT;
16241 	}
16242 
16243 	/* shutdown all operations */
16244 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16245 		i40e_prep_for_reset(pf);
16246 
16247 	/* Request a slot reset */
16248 	return PCI_ERS_RESULT_NEED_RESET;
16249 }
16250 
16251 /**
16252  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16253  * @pdev: PCI device information struct
16254  *
16255  * Called to find if the driver can work with the device now that
16256  * the pci slot has been reset.  If a basic connection seems good
16257  * (registers are readable and have sane content) then return a
16258  * happy little PCI_ERS_RESULT_xxx.
16259  **/
16260 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16261 {
16262 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16263 	pci_ers_result_t result;
16264 	u32 reg;
16265 
16266 	dev_dbg(&pdev->dev, "%s\n", __func__);
16267 	if (pci_enable_device_mem(pdev)) {
16268 		dev_info(&pdev->dev,
16269 			 "Cannot re-enable PCI device after reset.\n");
16270 		result = PCI_ERS_RESULT_DISCONNECT;
16271 	} else {
16272 		pci_set_master(pdev);
16273 		pci_restore_state(pdev);
16274 		pci_save_state(pdev);
16275 		pci_wake_from_d3(pdev, false);
16276 
16277 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16278 		if (reg == 0)
16279 			result = PCI_ERS_RESULT_RECOVERED;
16280 		else
16281 			result = PCI_ERS_RESULT_DISCONNECT;
16282 	}
16283 
16284 	return result;
16285 }
16286 
16287 /**
16288  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16289  * @pdev: PCI device information struct
16290  */
16291 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16292 {
16293 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16294 
16295 	i40e_prep_for_reset(pf);
16296 }
16297 
16298 /**
16299  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16300  * @pdev: PCI device information struct
16301  */
16302 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16303 {
16304 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16305 
16306 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16307 		return;
16308 
16309 	i40e_reset_and_rebuild(pf, false, false);
16310 }
16311 
16312 /**
16313  * i40e_pci_error_resume - restart operations after PCI error recovery
16314  * @pdev: PCI device information struct
16315  *
16316  * Called to allow the driver to bring things back up after PCI error
16317  * and/or reset recovery has finished.
16318  **/
16319 static void i40e_pci_error_resume(struct pci_dev *pdev)
16320 {
16321 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16322 
16323 	dev_dbg(&pdev->dev, "%s\n", __func__);
16324 	if (test_bit(__I40E_SUSPENDED, pf->state))
16325 		return;
16326 
16327 	i40e_handle_reset_warning(pf, false);
16328 }
16329 
16330 /**
16331  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16332  * using the mac_address_write admin q function
16333  * @pf: pointer to i40e_pf struct
16334  **/
16335 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16336 {
16337 	struct i40e_hw *hw = &pf->hw;
16338 	i40e_status ret;
16339 	u8 mac_addr[6];
16340 	u16 flags = 0;
16341 
16342 	/* Get current MAC address in case it's an LAA */
16343 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16344 		ether_addr_copy(mac_addr,
16345 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16346 	} else {
16347 		dev_err(&pf->pdev->dev,
16348 			"Failed to retrieve MAC address; using default\n");
16349 		ether_addr_copy(mac_addr, hw->mac.addr);
16350 	}
16351 
16352 	/* The FW expects the mac address write cmd to first be called with
16353 	 * one of these flags before calling it again with the multicast
16354 	 * enable flags.
16355 	 */
16356 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16357 
16358 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16359 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16360 
16361 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16362 	if (ret) {
16363 		dev_err(&pf->pdev->dev,
16364 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16365 		return;
16366 	}
16367 
16368 	flags = I40E_AQC_MC_MAG_EN
16369 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16370 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16371 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16372 	if (ret)
16373 		dev_err(&pf->pdev->dev,
16374 			"Failed to enable Multicast Magic Packet wake up\n");
16375 }
16376 
16377 /**
16378  * i40e_shutdown - PCI callback for shutting down
16379  * @pdev: PCI device information struct
16380  **/
16381 static void i40e_shutdown(struct pci_dev *pdev)
16382 {
16383 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16384 	struct i40e_hw *hw = &pf->hw;
16385 
16386 	set_bit(__I40E_SUSPENDED, pf->state);
16387 	set_bit(__I40E_DOWN, pf->state);
16388 
16389 	del_timer_sync(&pf->service_timer);
16390 	cancel_work_sync(&pf->service_task);
16391 	i40e_cloud_filter_exit(pf);
16392 	i40e_fdir_teardown(pf);
16393 
16394 	/* Client close must be called explicitly here because the timer
16395 	 * has been stopped.
16396 	 */
16397 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16398 
16399 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16400 		i40e_enable_mc_magic_wake(pf);
16401 
16402 	i40e_prep_for_reset(pf);
16403 
16404 	wr32(hw, I40E_PFPM_APM,
16405 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16406 	wr32(hw, I40E_PFPM_WUFC,
16407 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16408 
16409 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16410 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16411 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16412 		free_irq(pf->pdev->irq, pf);
16413 
16414 	/* Since we're going to destroy queues during the
16415 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16416 	 * whole section
16417 	 */
16418 	rtnl_lock();
16419 	i40e_clear_interrupt_scheme(pf);
16420 	rtnl_unlock();
16421 
16422 	if (system_state == SYSTEM_POWER_OFF) {
16423 		pci_wake_from_d3(pdev, pf->wol_en);
16424 		pci_set_power_state(pdev, PCI_D3hot);
16425 	}
16426 }
16427 
16428 /**
16429  * i40e_suspend - PM callback for moving to D3
16430  * @dev: generic device information structure
16431  **/
16432 static int __maybe_unused i40e_suspend(struct device *dev)
16433 {
16434 	struct i40e_pf *pf = dev_get_drvdata(dev);
16435 	struct i40e_hw *hw = &pf->hw;
16436 
16437 	/* If we're already suspended, then there is nothing to do */
16438 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16439 		return 0;
16440 
16441 	set_bit(__I40E_DOWN, pf->state);
16442 
16443 	/* Ensure service task will not be running */
16444 	del_timer_sync(&pf->service_timer);
16445 	cancel_work_sync(&pf->service_task);
16446 
16447 	/* Client close must be called explicitly here because the timer
16448 	 * has been stopped.
16449 	 */
16450 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16451 
16452 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16453 		i40e_enable_mc_magic_wake(pf);
16454 
16455 	/* Since we're going to destroy queues during the
16456 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16457 	 * whole section
16458 	 */
16459 	rtnl_lock();
16460 
16461 	i40e_prep_for_reset(pf);
16462 
16463 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16464 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16465 
16466 	/* Clear the interrupt scheme and release our IRQs so that the system
16467 	 * can safely hibernate even when there are a large number of CPUs.
16468 	 * Otherwise hibernation might fail when mapping all the vectors back
16469 	 * to CPU0.
16470 	 */
16471 	i40e_clear_interrupt_scheme(pf);
16472 
16473 	rtnl_unlock();
16474 
16475 	return 0;
16476 }
16477 
16478 /**
16479  * i40e_resume - PM callback for waking up from D3
16480  * @dev: generic device information structure
16481  **/
16482 static int __maybe_unused i40e_resume(struct device *dev)
16483 {
16484 	struct i40e_pf *pf = dev_get_drvdata(dev);
16485 	int err;
16486 
16487 	/* If we're not suspended, then there is nothing to do */
16488 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16489 		return 0;
16490 
16491 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16492 	 * since we're going to be restoring queues
16493 	 */
16494 	rtnl_lock();
16495 
16496 	/* We cleared the interrupt scheme when we suspended, so we need to
16497 	 * restore it now to resume device functionality.
16498 	 */
16499 	err = i40e_restore_interrupt_scheme(pf);
16500 	if (err) {
16501 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16502 			err);
16503 	}
16504 
16505 	clear_bit(__I40E_DOWN, pf->state);
16506 	i40e_reset_and_rebuild(pf, false, true);
16507 
16508 	rtnl_unlock();
16509 
16510 	/* Clear suspended state last after everything is recovered */
16511 	clear_bit(__I40E_SUSPENDED, pf->state);
16512 
16513 	/* Restart the service task */
16514 	mod_timer(&pf->service_timer,
16515 		  round_jiffies(jiffies + pf->service_timer_period));
16516 
16517 	return 0;
16518 }
16519 
16520 static const struct pci_error_handlers i40e_err_handler = {
16521 	.error_detected = i40e_pci_error_detected,
16522 	.slot_reset = i40e_pci_error_slot_reset,
16523 	.reset_prepare = i40e_pci_error_reset_prepare,
16524 	.reset_done = i40e_pci_error_reset_done,
16525 	.resume = i40e_pci_error_resume,
16526 };
16527 
16528 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16529 
16530 static struct pci_driver i40e_driver = {
16531 	.name     = i40e_driver_name,
16532 	.id_table = i40e_pci_tbl,
16533 	.probe    = i40e_probe,
16534 	.remove   = i40e_remove,
16535 	.driver   = {
16536 		.pm = &i40e_pm_ops,
16537 	},
16538 	.shutdown = i40e_shutdown,
16539 	.err_handler = &i40e_err_handler,
16540 	.sriov_configure = i40e_pci_sriov_configure,
16541 };
16542 
16543 /**
16544  * i40e_init_module - Driver registration routine
16545  *
16546  * i40e_init_module is the first routine called when the driver is
16547  * loaded. All it does is register with the PCI subsystem.
16548  **/
16549 static int __init i40e_init_module(void)
16550 {
16551 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16552 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16553 
16554 	/* There is no need to throttle the number of active tasks because
16555 	 * each device limits its own task using a state bit for scheduling
16556 	 * the service task, and the device tasks do not interfere with each
16557 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16558 	 * since we need to be able to guarantee forward progress even under
16559 	 * memory pressure.
16560 	 */
16561 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16562 	if (!i40e_wq) {
16563 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16564 		return -ENOMEM;
16565 	}
16566 
16567 	i40e_dbg_init();
16568 	return pci_register_driver(&i40e_driver);
16569 }
16570 module_init(i40e_init_module);
16571 
16572 /**
16573  * i40e_exit_module - Driver exit cleanup routine
16574  *
16575  * i40e_exit_module is called just before the driver is removed
16576  * from memory.
16577  **/
16578 static void __exit i40e_exit_module(void)
16579 {
16580 	pci_unregister_driver(&i40e_driver);
16581 	destroy_workqueue(i40e_wq);
16582 	ida_destroy(&i40e_client_ida);
16583 	i40e_dbg_exit();
16584 }
16585 module_exit(i40e_exit_module);
16586