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, device is in non-recoverable state.\n");
388 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
389 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
390 		break;
391 	}
392 
393 	i40e_service_event_schedule(pf);
394 	pf->tx_timeout_recovery_level++;
395 }
396 
397 /**
398  * i40e_get_vsi_stats_struct - Get System Network Statistics
399  * @vsi: the VSI we care about
400  *
401  * Returns the address of the device statistics structure.
402  * The statistics are actually updated from the service task.
403  **/
404 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
405 {
406 	return &vsi->net_stats;
407 }
408 
409 /**
410  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
411  * @ring: Tx ring to get statistics from
412  * @stats: statistics entry to be updated
413  **/
414 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
415 					    struct rtnl_link_stats64 *stats)
416 {
417 	u64 bytes, packets;
418 	unsigned int start;
419 
420 	do {
421 		start = u64_stats_fetch_begin_irq(&ring->syncp);
422 		packets = ring->stats.packets;
423 		bytes   = ring->stats.bytes;
424 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
425 
426 	stats->tx_packets += packets;
427 	stats->tx_bytes   += bytes;
428 }
429 
430 /**
431  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
432  * @netdev: network interface device structure
433  * @stats: data structure to store statistics
434  *
435  * Returns the address of the device statistics structure.
436  * The statistics are actually updated from the service task.
437  **/
438 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
439 				  struct rtnl_link_stats64 *stats)
440 {
441 	struct i40e_netdev_priv *np = netdev_priv(netdev);
442 	struct i40e_vsi *vsi = np->vsi;
443 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
444 	struct i40e_ring *ring;
445 	int i;
446 
447 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
448 		return;
449 
450 	if (!vsi->tx_rings)
451 		return;
452 
453 	rcu_read_lock();
454 	for (i = 0; i < vsi->num_queue_pairs; i++) {
455 		u64 bytes, packets;
456 		unsigned int start;
457 
458 		ring = READ_ONCE(vsi->tx_rings[i]);
459 		if (!ring)
460 			continue;
461 		i40e_get_netdev_stats_struct_tx(ring, stats);
462 
463 		if (i40e_enabled_xdp_vsi(vsi)) {
464 			ring = READ_ONCE(vsi->xdp_rings[i]);
465 			if (!ring)
466 				continue;
467 			i40e_get_netdev_stats_struct_tx(ring, stats);
468 		}
469 
470 		ring = READ_ONCE(vsi->rx_rings[i]);
471 		if (!ring)
472 			continue;
473 		do {
474 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
475 			packets = ring->stats.packets;
476 			bytes   = ring->stats.bytes;
477 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
478 
479 		stats->rx_packets += packets;
480 		stats->rx_bytes   += bytes;
481 
482 	}
483 	rcu_read_unlock();
484 
485 	/* following stats updated by i40e_watchdog_subtask() */
486 	stats->multicast	= vsi_stats->multicast;
487 	stats->tx_errors	= vsi_stats->tx_errors;
488 	stats->tx_dropped	= vsi_stats->tx_dropped;
489 	stats->rx_errors	= vsi_stats->rx_errors;
490 	stats->rx_dropped	= vsi_stats->rx_dropped;
491 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
492 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
493 }
494 
495 /**
496  * i40e_vsi_reset_stats - Resets all stats of the given vsi
497  * @vsi: the VSI to have its stats reset
498  **/
499 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
500 {
501 	struct rtnl_link_stats64 *ns;
502 	int i;
503 
504 	if (!vsi)
505 		return;
506 
507 	ns = i40e_get_vsi_stats_struct(vsi);
508 	memset(ns, 0, sizeof(*ns));
509 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
510 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
511 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
512 	if (vsi->rx_rings && vsi->rx_rings[0]) {
513 		for (i = 0; i < vsi->num_queue_pairs; i++) {
514 			memset(&vsi->rx_rings[i]->stats, 0,
515 			       sizeof(vsi->rx_rings[i]->stats));
516 			memset(&vsi->rx_rings[i]->rx_stats, 0,
517 			       sizeof(vsi->rx_rings[i]->rx_stats));
518 			memset(&vsi->tx_rings[i]->stats, 0,
519 			       sizeof(vsi->tx_rings[i]->stats));
520 			memset(&vsi->tx_rings[i]->tx_stats, 0,
521 			       sizeof(vsi->tx_rings[i]->tx_stats));
522 		}
523 	}
524 	vsi->stat_offsets_loaded = false;
525 }
526 
527 /**
528  * i40e_pf_reset_stats - Reset all of the stats for the given PF
529  * @pf: the PF to be reset
530  **/
531 void i40e_pf_reset_stats(struct i40e_pf *pf)
532 {
533 	int i;
534 
535 	memset(&pf->stats, 0, sizeof(pf->stats));
536 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
537 	pf->stat_offsets_loaded = false;
538 
539 	for (i = 0; i < I40E_MAX_VEB; i++) {
540 		if (pf->veb[i]) {
541 			memset(&pf->veb[i]->stats, 0,
542 			       sizeof(pf->veb[i]->stats));
543 			memset(&pf->veb[i]->stats_offsets, 0,
544 			       sizeof(pf->veb[i]->stats_offsets));
545 			memset(&pf->veb[i]->tc_stats, 0,
546 			       sizeof(pf->veb[i]->tc_stats));
547 			memset(&pf->veb[i]->tc_stats_offsets, 0,
548 			       sizeof(pf->veb[i]->tc_stats_offsets));
549 			pf->veb[i]->stat_offsets_loaded = false;
550 		}
551 	}
552 	pf->hw_csum_rx_error = 0;
553 }
554 
555 /**
556  * i40e_compute_pci_to_hw_id - compute index form PCI function.
557  * @vsi: ptr to the VSI to read from.
558  * @hw: ptr to the hardware info.
559  **/
560 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
561 {
562 	int pf_count = i40e_get_pf_count(hw);
563 
564 	if (vsi->type == I40E_VSI_SRIOV)
565 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
566 
567 	return hw->port + BIT(7);
568 }
569 
570 /**
571  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
572  * @hw: ptr to the hardware info.
573  * @hireg: the high 32 bit reg to read.
574  * @loreg: the low 32 bit reg to read.
575  * @offset_loaded: has the initial offset been loaded yet.
576  * @offset: ptr to current offset value.
577  * @stat: ptr to the stat.
578  *
579  * Since the device stats are not reset at PFReset, they will not
580  * be zeroed when the driver starts.  We'll save the first values read
581  * and use them as offsets to be subtracted from the raw values in order
582  * to report stats that count from zero.
583  **/
584 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
585 			       bool offset_loaded, u64 *offset, u64 *stat)
586 {
587 	u64 new_data;
588 
589 	new_data = rd64(hw, loreg);
590 
591 	if (!offset_loaded || new_data < *offset)
592 		*offset = new_data;
593 	*stat = new_data - *offset;
594 }
595 
596 /**
597  * i40e_stat_update48 - read and update a 48 bit stat from the chip
598  * @hw: ptr to the hardware info
599  * @hireg: the high 32 bit reg to read
600  * @loreg: the low 32 bit reg to read
601  * @offset_loaded: has the initial offset been loaded yet
602  * @offset: ptr to current offset value
603  * @stat: ptr to the stat
604  *
605  * Since the device stats are not reset at PFReset, they likely will not
606  * be zeroed when the driver starts.  We'll save the first values read
607  * and use them as offsets to be subtracted from the raw values in order
608  * to report stats that count from zero.  In the process, we also manage
609  * the potential roll-over.
610  **/
611 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
612 			       bool offset_loaded, u64 *offset, u64 *stat)
613 {
614 	u64 new_data;
615 
616 	if (hw->device_id == I40E_DEV_ID_QEMU) {
617 		new_data = rd32(hw, loreg);
618 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
619 	} else {
620 		new_data = rd64(hw, loreg);
621 	}
622 	if (!offset_loaded)
623 		*offset = new_data;
624 	if (likely(new_data >= *offset))
625 		*stat = new_data - *offset;
626 	else
627 		*stat = (new_data + BIT_ULL(48)) - *offset;
628 	*stat &= 0xFFFFFFFFFFFFULL;
629 }
630 
631 /**
632  * i40e_stat_update32 - read and update a 32 bit stat from the chip
633  * @hw: ptr to the hardware info
634  * @reg: the hw reg to read
635  * @offset_loaded: has the initial offset been loaded yet
636  * @offset: ptr to current offset value
637  * @stat: ptr to the stat
638  **/
639 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
640 			       bool offset_loaded, u64 *offset, u64 *stat)
641 {
642 	u32 new_data;
643 
644 	new_data = rd32(hw, reg);
645 	if (!offset_loaded)
646 		*offset = new_data;
647 	if (likely(new_data >= *offset))
648 		*stat = (u32)(new_data - *offset);
649 	else
650 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
651 }
652 
653 /**
654  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
655  * @hw: ptr to the hardware info
656  * @reg: the hw reg to read and clear
657  * @stat: ptr to the stat
658  **/
659 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
660 {
661 	u32 new_data = rd32(hw, reg);
662 
663 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
664 	*stat += new_data;
665 }
666 
667 /**
668  * i40e_stats_update_rx_discards - update rx_discards.
669  * @vsi: ptr to the VSI to be updated.
670  * @hw: ptr to the hardware info.
671  * @stat_idx: VSI's stat_counter_idx.
672  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
673  * @stat_offset: ptr to stat_offset to store first read of specific register.
674  * @stat: ptr to VSI's stat to be updated.
675  **/
676 static void
677 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
678 			      int stat_idx, bool offset_loaded,
679 			      struct i40e_eth_stats *stat_offset,
680 			      struct i40e_eth_stats *stat)
681 {
682 	u64 rx_rdpc, rx_rxerr;
683 
684 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
685 			   &stat_offset->rx_discards, &rx_rdpc);
686 	i40e_stat_update64(hw,
687 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
688 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
689 			   offset_loaded, &stat_offset->rx_discards_other,
690 			   &rx_rxerr);
691 
692 	stat->rx_discards = rx_rdpc + rx_rxerr;
693 }
694 
695 /**
696  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
697  * @vsi: the VSI to be updated
698  **/
699 void i40e_update_eth_stats(struct i40e_vsi *vsi)
700 {
701 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
702 	struct i40e_pf *pf = vsi->back;
703 	struct i40e_hw *hw = &pf->hw;
704 	struct i40e_eth_stats *oes;
705 	struct i40e_eth_stats *es;     /* device's eth stats */
706 
707 	es = &vsi->eth_stats;
708 	oes = &vsi->eth_stats_offsets;
709 
710 	/* Gather up the stats that the hw collects */
711 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
712 			   vsi->stat_offsets_loaded,
713 			   &oes->tx_errors, &es->tx_errors);
714 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
715 			   vsi->stat_offsets_loaded,
716 			   &oes->rx_discards, &es->rx_discards);
717 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
718 			   vsi->stat_offsets_loaded,
719 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
720 
721 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
722 			   I40E_GLV_GORCL(stat_idx),
723 			   vsi->stat_offsets_loaded,
724 			   &oes->rx_bytes, &es->rx_bytes);
725 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
726 			   I40E_GLV_UPRCL(stat_idx),
727 			   vsi->stat_offsets_loaded,
728 			   &oes->rx_unicast, &es->rx_unicast);
729 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
730 			   I40E_GLV_MPRCL(stat_idx),
731 			   vsi->stat_offsets_loaded,
732 			   &oes->rx_multicast, &es->rx_multicast);
733 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
734 			   I40E_GLV_BPRCL(stat_idx),
735 			   vsi->stat_offsets_loaded,
736 			   &oes->rx_broadcast, &es->rx_broadcast);
737 
738 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
739 			   I40E_GLV_GOTCL(stat_idx),
740 			   vsi->stat_offsets_loaded,
741 			   &oes->tx_bytes, &es->tx_bytes);
742 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
743 			   I40E_GLV_UPTCL(stat_idx),
744 			   vsi->stat_offsets_loaded,
745 			   &oes->tx_unicast, &es->tx_unicast);
746 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
747 			   I40E_GLV_MPTCL(stat_idx),
748 			   vsi->stat_offsets_loaded,
749 			   &oes->tx_multicast, &es->tx_multicast);
750 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
751 			   I40E_GLV_BPTCL(stat_idx),
752 			   vsi->stat_offsets_loaded,
753 			   &oes->tx_broadcast, &es->tx_broadcast);
754 
755 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
756 				      vsi->stat_offsets_loaded, oes, es);
757 
758 	vsi->stat_offsets_loaded = true;
759 }
760 
761 /**
762  * i40e_update_veb_stats - Update Switch component statistics
763  * @veb: the VEB being updated
764  **/
765 void i40e_update_veb_stats(struct i40e_veb *veb)
766 {
767 	struct i40e_pf *pf = veb->pf;
768 	struct i40e_hw *hw = &pf->hw;
769 	struct i40e_eth_stats *oes;
770 	struct i40e_eth_stats *es;     /* device's eth stats */
771 	struct i40e_veb_tc_stats *veb_oes;
772 	struct i40e_veb_tc_stats *veb_es;
773 	int i, idx = 0;
774 
775 	idx = veb->stats_idx;
776 	es = &veb->stats;
777 	oes = &veb->stats_offsets;
778 	veb_es = &veb->tc_stats;
779 	veb_oes = &veb->tc_stats_offsets;
780 
781 	/* Gather up the stats that the hw collects */
782 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
783 			   veb->stat_offsets_loaded,
784 			   &oes->tx_discards, &es->tx_discards);
785 	if (hw->revision_id > 0)
786 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
787 				   veb->stat_offsets_loaded,
788 				   &oes->rx_unknown_protocol,
789 				   &es->rx_unknown_protocol);
790 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
791 			   veb->stat_offsets_loaded,
792 			   &oes->rx_bytes, &es->rx_bytes);
793 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
794 			   veb->stat_offsets_loaded,
795 			   &oes->rx_unicast, &es->rx_unicast);
796 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
797 			   veb->stat_offsets_loaded,
798 			   &oes->rx_multicast, &es->rx_multicast);
799 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
800 			   veb->stat_offsets_loaded,
801 			   &oes->rx_broadcast, &es->rx_broadcast);
802 
803 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
804 			   veb->stat_offsets_loaded,
805 			   &oes->tx_bytes, &es->tx_bytes);
806 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
807 			   veb->stat_offsets_loaded,
808 			   &oes->tx_unicast, &es->tx_unicast);
809 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
810 			   veb->stat_offsets_loaded,
811 			   &oes->tx_multicast, &es->tx_multicast);
812 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
813 			   veb->stat_offsets_loaded,
814 			   &oes->tx_broadcast, &es->tx_broadcast);
815 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
816 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
817 				   I40E_GLVEBTC_RPCL(i, idx),
818 				   veb->stat_offsets_loaded,
819 				   &veb_oes->tc_rx_packets[i],
820 				   &veb_es->tc_rx_packets[i]);
821 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
822 				   I40E_GLVEBTC_RBCL(i, idx),
823 				   veb->stat_offsets_loaded,
824 				   &veb_oes->tc_rx_bytes[i],
825 				   &veb_es->tc_rx_bytes[i]);
826 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
827 				   I40E_GLVEBTC_TPCL(i, idx),
828 				   veb->stat_offsets_loaded,
829 				   &veb_oes->tc_tx_packets[i],
830 				   &veb_es->tc_tx_packets[i]);
831 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
832 				   I40E_GLVEBTC_TBCL(i, idx),
833 				   veb->stat_offsets_loaded,
834 				   &veb_oes->tc_tx_bytes[i],
835 				   &veb_es->tc_tx_bytes[i]);
836 	}
837 	veb->stat_offsets_loaded = true;
838 }
839 
840 /**
841  * i40e_update_vsi_stats - Update the vsi statistics counters.
842  * @vsi: the VSI to be updated
843  *
844  * There are a few instances where we store the same stat in a
845  * couple of different structs.  This is partly because we have
846  * the netdev stats that need to be filled out, which is slightly
847  * different from the "eth_stats" defined by the chip and used in
848  * VF communications.  We sort it out here.
849  **/
850 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
851 {
852 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
853 	struct i40e_pf *pf = vsi->back;
854 	struct rtnl_link_stats64 *ons;
855 	struct rtnl_link_stats64 *ns;   /* netdev stats */
856 	struct i40e_eth_stats *oes;
857 	struct i40e_eth_stats *es;     /* device's eth stats */
858 	u64 tx_restart, tx_busy;
859 	struct i40e_ring *p;
860 	u64 bytes, packets;
861 	unsigned int start;
862 	u64 tx_linearize;
863 	u64 tx_force_wb;
864 	u64 tx_stopped;
865 	u64 rx_p, rx_b;
866 	u64 tx_p, tx_b;
867 	u16 q;
868 
869 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
870 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
871 		return;
872 
873 	ns = i40e_get_vsi_stats_struct(vsi);
874 	ons = &vsi->net_stats_offsets;
875 	es = &vsi->eth_stats;
876 	oes = &vsi->eth_stats_offsets;
877 
878 	/* Gather up the netdev and vsi stats that the driver collects
879 	 * on the fly during packet processing
880 	 */
881 	rx_b = rx_p = 0;
882 	tx_b = tx_p = 0;
883 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
884 	tx_stopped = 0;
885 	rx_page = 0;
886 	rx_buf = 0;
887 	rx_reuse = 0;
888 	rx_alloc = 0;
889 	rx_waive = 0;
890 	rx_busy = 0;
891 	rcu_read_lock();
892 	for (q = 0; q < vsi->num_queue_pairs; q++) {
893 		/* locate Tx ring */
894 		p = READ_ONCE(vsi->tx_rings[q]);
895 		if (!p)
896 			continue;
897 
898 		do {
899 			start = u64_stats_fetch_begin_irq(&p->syncp);
900 			packets = p->stats.packets;
901 			bytes = p->stats.bytes;
902 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
903 		tx_b += bytes;
904 		tx_p += packets;
905 		tx_restart += p->tx_stats.restart_queue;
906 		tx_busy += p->tx_stats.tx_busy;
907 		tx_linearize += p->tx_stats.tx_linearize;
908 		tx_force_wb += p->tx_stats.tx_force_wb;
909 		tx_stopped += p->tx_stats.tx_stopped;
910 
911 		/* locate Rx ring */
912 		p = READ_ONCE(vsi->rx_rings[q]);
913 		if (!p)
914 			continue;
915 
916 		do {
917 			start = u64_stats_fetch_begin_irq(&p->syncp);
918 			packets = p->stats.packets;
919 			bytes = p->stats.bytes;
920 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
921 		rx_b += bytes;
922 		rx_p += packets;
923 		rx_buf += p->rx_stats.alloc_buff_failed;
924 		rx_page += p->rx_stats.alloc_page_failed;
925 		rx_reuse += p->rx_stats.page_reuse_count;
926 		rx_alloc += p->rx_stats.page_alloc_count;
927 		rx_waive += p->rx_stats.page_waive_count;
928 		rx_busy += p->rx_stats.page_busy_count;
929 
930 		if (i40e_enabled_xdp_vsi(vsi)) {
931 			/* locate XDP ring */
932 			p = READ_ONCE(vsi->xdp_rings[q]);
933 			if (!p)
934 				continue;
935 
936 			do {
937 				start = u64_stats_fetch_begin_irq(&p->syncp);
938 				packets = p->stats.packets;
939 				bytes = p->stats.bytes;
940 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
941 			tx_b += bytes;
942 			tx_p += packets;
943 			tx_restart += p->tx_stats.restart_queue;
944 			tx_busy += p->tx_stats.tx_busy;
945 			tx_linearize += p->tx_stats.tx_linearize;
946 			tx_force_wb += p->tx_stats.tx_force_wb;
947 		}
948 	}
949 	rcu_read_unlock();
950 	vsi->tx_restart = tx_restart;
951 	vsi->tx_busy = tx_busy;
952 	vsi->tx_linearize = tx_linearize;
953 	vsi->tx_force_wb = tx_force_wb;
954 	vsi->tx_stopped = tx_stopped;
955 	vsi->rx_page_failed = rx_page;
956 	vsi->rx_buf_failed = rx_buf;
957 	vsi->rx_page_reuse = rx_reuse;
958 	vsi->rx_page_alloc = rx_alloc;
959 	vsi->rx_page_waive = rx_waive;
960 	vsi->rx_page_busy = rx_busy;
961 
962 	ns->rx_packets = rx_p;
963 	ns->rx_bytes = rx_b;
964 	ns->tx_packets = tx_p;
965 	ns->tx_bytes = tx_b;
966 
967 	/* update netdev stats from eth stats */
968 	i40e_update_eth_stats(vsi);
969 	ons->tx_errors = oes->tx_errors;
970 	ns->tx_errors = es->tx_errors;
971 	ons->multicast = oes->rx_multicast;
972 	ns->multicast = es->rx_multicast;
973 	ons->rx_dropped = oes->rx_discards;
974 	ns->rx_dropped = es->rx_discards;
975 	ons->tx_dropped = oes->tx_discards;
976 	ns->tx_dropped = es->tx_discards;
977 
978 	/* pull in a couple PF stats if this is the main vsi */
979 	if (vsi == pf->vsi[pf->lan_vsi]) {
980 		ns->rx_crc_errors = pf->stats.crc_errors;
981 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
982 		ns->rx_length_errors = pf->stats.rx_length_errors;
983 	}
984 }
985 
986 /**
987  * i40e_update_pf_stats - Update the PF statistics counters.
988  * @pf: the PF to be updated
989  **/
990 static void i40e_update_pf_stats(struct i40e_pf *pf)
991 {
992 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
993 	struct i40e_hw_port_stats *nsd = &pf->stats;
994 	struct i40e_hw *hw = &pf->hw;
995 	u32 val;
996 	int i;
997 
998 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
999 			   I40E_GLPRT_GORCL(hw->port),
1000 			   pf->stat_offsets_loaded,
1001 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1002 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1003 			   I40E_GLPRT_GOTCL(hw->port),
1004 			   pf->stat_offsets_loaded,
1005 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1006 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1007 			   pf->stat_offsets_loaded,
1008 			   &osd->eth.rx_discards,
1009 			   &nsd->eth.rx_discards);
1010 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1011 			   I40E_GLPRT_UPRCL(hw->port),
1012 			   pf->stat_offsets_loaded,
1013 			   &osd->eth.rx_unicast,
1014 			   &nsd->eth.rx_unicast);
1015 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1016 			   I40E_GLPRT_MPRCL(hw->port),
1017 			   pf->stat_offsets_loaded,
1018 			   &osd->eth.rx_multicast,
1019 			   &nsd->eth.rx_multicast);
1020 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1021 			   I40E_GLPRT_BPRCL(hw->port),
1022 			   pf->stat_offsets_loaded,
1023 			   &osd->eth.rx_broadcast,
1024 			   &nsd->eth.rx_broadcast);
1025 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1026 			   I40E_GLPRT_UPTCL(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->eth.tx_unicast,
1029 			   &nsd->eth.tx_unicast);
1030 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1031 			   I40E_GLPRT_MPTCL(hw->port),
1032 			   pf->stat_offsets_loaded,
1033 			   &osd->eth.tx_multicast,
1034 			   &nsd->eth.tx_multicast);
1035 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1036 			   I40E_GLPRT_BPTCL(hw->port),
1037 			   pf->stat_offsets_loaded,
1038 			   &osd->eth.tx_broadcast,
1039 			   &nsd->eth.tx_broadcast);
1040 
1041 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1042 			   pf->stat_offsets_loaded,
1043 			   &osd->tx_dropped_link_down,
1044 			   &nsd->tx_dropped_link_down);
1045 
1046 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1047 			   pf->stat_offsets_loaded,
1048 			   &osd->crc_errors, &nsd->crc_errors);
1049 
1050 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1051 			   pf->stat_offsets_loaded,
1052 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1053 
1054 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1055 			   pf->stat_offsets_loaded,
1056 			   &osd->mac_local_faults,
1057 			   &nsd->mac_local_faults);
1058 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1059 			   pf->stat_offsets_loaded,
1060 			   &osd->mac_remote_faults,
1061 			   &nsd->mac_remote_faults);
1062 
1063 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->rx_length_errors,
1066 			   &nsd->rx_length_errors);
1067 
1068 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1069 			   pf->stat_offsets_loaded,
1070 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1071 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1074 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1075 			   pf->stat_offsets_loaded,
1076 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1077 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1078 			   pf->stat_offsets_loaded,
1079 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1080 
1081 	for (i = 0; i < 8; i++) {
1082 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1083 				   pf->stat_offsets_loaded,
1084 				   &osd->priority_xoff_rx[i],
1085 				   &nsd->priority_xoff_rx[i]);
1086 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1087 				   pf->stat_offsets_loaded,
1088 				   &osd->priority_xon_rx[i],
1089 				   &nsd->priority_xon_rx[i]);
1090 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1091 				   pf->stat_offsets_loaded,
1092 				   &osd->priority_xon_tx[i],
1093 				   &nsd->priority_xon_tx[i]);
1094 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1095 				   pf->stat_offsets_loaded,
1096 				   &osd->priority_xoff_tx[i],
1097 				   &nsd->priority_xoff_tx[i]);
1098 		i40e_stat_update32(hw,
1099 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1100 				   pf->stat_offsets_loaded,
1101 				   &osd->priority_xon_2_xoff[i],
1102 				   &nsd->priority_xon_2_xoff[i]);
1103 	}
1104 
1105 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1106 			   I40E_GLPRT_PRC64L(hw->port),
1107 			   pf->stat_offsets_loaded,
1108 			   &osd->rx_size_64, &nsd->rx_size_64);
1109 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1110 			   I40E_GLPRT_PRC127L(hw->port),
1111 			   pf->stat_offsets_loaded,
1112 			   &osd->rx_size_127, &nsd->rx_size_127);
1113 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1114 			   I40E_GLPRT_PRC255L(hw->port),
1115 			   pf->stat_offsets_loaded,
1116 			   &osd->rx_size_255, &nsd->rx_size_255);
1117 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1118 			   I40E_GLPRT_PRC511L(hw->port),
1119 			   pf->stat_offsets_loaded,
1120 			   &osd->rx_size_511, &nsd->rx_size_511);
1121 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1122 			   I40E_GLPRT_PRC1023L(hw->port),
1123 			   pf->stat_offsets_loaded,
1124 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1125 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1126 			   I40E_GLPRT_PRC1522L(hw->port),
1127 			   pf->stat_offsets_loaded,
1128 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1129 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1130 			   I40E_GLPRT_PRC9522L(hw->port),
1131 			   pf->stat_offsets_loaded,
1132 			   &osd->rx_size_big, &nsd->rx_size_big);
1133 
1134 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1135 			   I40E_GLPRT_PTC64L(hw->port),
1136 			   pf->stat_offsets_loaded,
1137 			   &osd->tx_size_64, &nsd->tx_size_64);
1138 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1139 			   I40E_GLPRT_PTC127L(hw->port),
1140 			   pf->stat_offsets_loaded,
1141 			   &osd->tx_size_127, &nsd->tx_size_127);
1142 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1143 			   I40E_GLPRT_PTC255L(hw->port),
1144 			   pf->stat_offsets_loaded,
1145 			   &osd->tx_size_255, &nsd->tx_size_255);
1146 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1147 			   I40E_GLPRT_PTC511L(hw->port),
1148 			   pf->stat_offsets_loaded,
1149 			   &osd->tx_size_511, &nsd->tx_size_511);
1150 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1151 			   I40E_GLPRT_PTC1023L(hw->port),
1152 			   pf->stat_offsets_loaded,
1153 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1154 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1155 			   I40E_GLPRT_PTC1522L(hw->port),
1156 			   pf->stat_offsets_loaded,
1157 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1158 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1159 			   I40E_GLPRT_PTC9522L(hw->port),
1160 			   pf->stat_offsets_loaded,
1161 			   &osd->tx_size_big, &nsd->tx_size_big);
1162 
1163 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1164 			   pf->stat_offsets_loaded,
1165 			   &osd->rx_undersize, &nsd->rx_undersize);
1166 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1167 			   pf->stat_offsets_loaded,
1168 			   &osd->rx_fragments, &nsd->rx_fragments);
1169 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1170 			   pf->stat_offsets_loaded,
1171 			   &osd->rx_oversize, &nsd->rx_oversize);
1172 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1173 			   pf->stat_offsets_loaded,
1174 			   &osd->rx_jabber, &nsd->rx_jabber);
1175 
1176 	/* FDIR stats */
1177 	i40e_stat_update_and_clear32(hw,
1178 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1179 			&nsd->fd_atr_match);
1180 	i40e_stat_update_and_clear32(hw,
1181 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1182 			&nsd->fd_sb_match);
1183 	i40e_stat_update_and_clear32(hw,
1184 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1185 			&nsd->fd_atr_tunnel_match);
1186 
1187 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1188 	nsd->tx_lpi_status =
1189 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1190 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1191 	nsd->rx_lpi_status =
1192 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1193 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1194 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1195 			   pf->stat_offsets_loaded,
1196 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1197 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1198 			   pf->stat_offsets_loaded,
1199 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1200 
1201 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1202 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1203 		nsd->fd_sb_status = true;
1204 	else
1205 		nsd->fd_sb_status = false;
1206 
1207 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1208 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1209 		nsd->fd_atr_status = true;
1210 	else
1211 		nsd->fd_atr_status = false;
1212 
1213 	pf->stat_offsets_loaded = true;
1214 }
1215 
1216 /**
1217  * i40e_update_stats - Update the various statistics counters.
1218  * @vsi: the VSI to be updated
1219  *
1220  * Update the various stats for this VSI and its related entities.
1221  **/
1222 void i40e_update_stats(struct i40e_vsi *vsi)
1223 {
1224 	struct i40e_pf *pf = vsi->back;
1225 
1226 	if (vsi == pf->vsi[pf->lan_vsi])
1227 		i40e_update_pf_stats(pf);
1228 
1229 	i40e_update_vsi_stats(vsi);
1230 }
1231 
1232 /**
1233  * i40e_count_filters - counts VSI mac filters
1234  * @vsi: the VSI to be searched
1235  *
1236  * Returns count of mac filters
1237  **/
1238 int i40e_count_filters(struct i40e_vsi *vsi)
1239 {
1240 	struct i40e_mac_filter *f;
1241 	struct hlist_node *h;
1242 	int bkt;
1243 	int cnt = 0;
1244 
1245 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1246 		++cnt;
1247 
1248 	return cnt;
1249 }
1250 
1251 /**
1252  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1253  * @vsi: the VSI to be searched
1254  * @macaddr: the MAC address
1255  * @vlan: the vlan
1256  *
1257  * Returns ptr to the filter object or NULL
1258  **/
1259 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1260 						const u8 *macaddr, s16 vlan)
1261 {
1262 	struct i40e_mac_filter *f;
1263 	u64 key;
1264 
1265 	if (!vsi || !macaddr)
1266 		return NULL;
1267 
1268 	key = i40e_addr_to_hkey(macaddr);
1269 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1270 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1271 		    (vlan == f->vlan))
1272 			return f;
1273 	}
1274 	return NULL;
1275 }
1276 
1277 /**
1278  * i40e_find_mac - Find a mac addr in the macvlan filters list
1279  * @vsi: the VSI to be searched
1280  * @macaddr: the MAC address we are searching for
1281  *
1282  * Returns the first filter with the provided MAC address or NULL if
1283  * MAC address was not found
1284  **/
1285 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1286 {
1287 	struct i40e_mac_filter *f;
1288 	u64 key;
1289 
1290 	if (!vsi || !macaddr)
1291 		return NULL;
1292 
1293 	key = i40e_addr_to_hkey(macaddr);
1294 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1295 		if ((ether_addr_equal(macaddr, f->macaddr)))
1296 			return f;
1297 	}
1298 	return NULL;
1299 }
1300 
1301 /**
1302  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1303  * @vsi: the VSI to be searched
1304  *
1305  * Returns true if VSI is in vlan mode or false otherwise
1306  **/
1307 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1308 {
1309 	/* If we have a PVID, always operate in VLAN mode */
1310 	if (vsi->info.pvid)
1311 		return true;
1312 
1313 	/* We need to operate in VLAN mode whenever we have any filters with
1314 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1315 	 * time, incurring search cost repeatedly. However, we can notice two
1316 	 * things:
1317 	 *
1318 	 * 1) the only place where we can gain a VLAN filter is in
1319 	 *    i40e_add_filter.
1320 	 *
1321 	 * 2) the only place where filters are actually removed is in
1322 	 *    i40e_sync_filters_subtask.
1323 	 *
1324 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1325 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1326 	 * we have to perform the full search after deleting filters in
1327 	 * i40e_sync_filters_subtask, but we already have to search
1328 	 * filters here and can perform the check at the same time. This
1329 	 * results in avoiding embedding a loop for VLAN mode inside another
1330 	 * loop over all the filters, and should maintain correctness as noted
1331 	 * above.
1332 	 */
1333 	return vsi->has_vlan_filter;
1334 }
1335 
1336 /**
1337  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1338  * @vsi: the VSI to configure
1339  * @tmp_add_list: list of filters ready to be added
1340  * @tmp_del_list: list of filters ready to be deleted
1341  * @vlan_filters: the number of active VLAN filters
1342  *
1343  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1344  * behave as expected. If we have any active VLAN filters remaining or about
1345  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1346  * so that they only match against untagged traffic. If we no longer have any
1347  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1348  * so that they match against both tagged and untagged traffic. In this way,
1349  * we ensure that we correctly receive the desired traffic. This ensures that
1350  * when we have an active VLAN we will receive only untagged traffic and
1351  * traffic matching active VLANs. If we have no active VLANs then we will
1352  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1353  *
1354  * Finally, in a similar fashion, this function also corrects filters when
1355  * there is an active PVID assigned to this VSI.
1356  *
1357  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1358  *
1359  * This function is only expected to be called from within
1360  * i40e_sync_vsi_filters.
1361  *
1362  * NOTE: This function expects to be called while under the
1363  * mac_filter_hash_lock
1364  */
1365 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1366 					 struct hlist_head *tmp_add_list,
1367 					 struct hlist_head *tmp_del_list,
1368 					 int vlan_filters)
1369 {
1370 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1371 	struct i40e_mac_filter *f, *add_head;
1372 	struct i40e_new_mac_filter *new;
1373 	struct hlist_node *h;
1374 	int bkt, new_vlan;
1375 
1376 	/* To determine if a particular filter needs to be replaced we
1377 	 * have the three following conditions:
1378 	 *
1379 	 * a) if we have a PVID assigned, then all filters which are
1380 	 *    not marked as VLAN=PVID must be replaced with filters that
1381 	 *    are.
1382 	 * b) otherwise, if we have any active VLANS, all filters
1383 	 *    which are marked as VLAN=-1 must be replaced with
1384 	 *    filters marked as VLAN=0
1385 	 * c) finally, if we do not have any active VLANS, all filters
1386 	 *    which are marked as VLAN=0 must be replaced with filters
1387 	 *    marked as VLAN=-1
1388 	 */
1389 
1390 	/* Update the filters about to be added in place */
1391 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1392 		if (pvid && new->f->vlan != pvid)
1393 			new->f->vlan = pvid;
1394 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1395 			new->f->vlan = 0;
1396 		else if (!vlan_filters && new->f->vlan == 0)
1397 			new->f->vlan = I40E_VLAN_ANY;
1398 	}
1399 
1400 	/* Update the remaining active filters */
1401 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1402 		/* Combine the checks for whether a filter needs to be changed
1403 		 * and then determine the new VLAN inside the if block, in
1404 		 * order to avoid duplicating code for adding the new filter
1405 		 * then deleting the old filter.
1406 		 */
1407 		if ((pvid && f->vlan != pvid) ||
1408 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1409 		    (!vlan_filters && f->vlan == 0)) {
1410 			/* Determine the new vlan we will be adding */
1411 			if (pvid)
1412 				new_vlan = pvid;
1413 			else if (vlan_filters)
1414 				new_vlan = 0;
1415 			else
1416 				new_vlan = I40E_VLAN_ANY;
1417 
1418 			/* Create the new filter */
1419 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1420 			if (!add_head)
1421 				return -ENOMEM;
1422 
1423 			/* Create a temporary i40e_new_mac_filter */
1424 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1425 			if (!new)
1426 				return -ENOMEM;
1427 
1428 			new->f = add_head;
1429 			new->state = add_head->state;
1430 
1431 			/* Add the new filter to the tmp list */
1432 			hlist_add_head(&new->hlist, tmp_add_list);
1433 
1434 			/* Put the original filter into the delete list */
1435 			f->state = I40E_FILTER_REMOVE;
1436 			hash_del(&f->hlist);
1437 			hlist_add_head(&f->hlist, tmp_del_list);
1438 		}
1439 	}
1440 
1441 	vsi->has_vlan_filter = !!vlan_filters;
1442 
1443 	return 0;
1444 }
1445 
1446 /**
1447  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1448  * @vsi: the vsi to configure
1449  * @new_mac: new mac filter to be added
1450  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1451  * @vlan_filters: the number of active VLAN filters
1452  * @trusted: flag if the VF is trusted
1453  *
1454  * Get new VLAN id based on current VLAN filters, trust, PVID
1455  * and vf-vlan-prune-disable flag.
1456  *
1457  * Returns the value of the new vlan filter or
1458  * the old value if no new filter is needed.
1459  */
1460 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1461 				struct i40e_new_mac_filter *new_mac,
1462 				struct i40e_mac_filter *f,
1463 				int vlan_filters,
1464 				bool trusted)
1465 {
1466 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1467 	struct i40e_pf *pf = vsi->back;
1468 	bool is_any;
1469 
1470 	if (new_mac)
1471 		f = new_mac->f;
1472 
1473 	if (pvid && f->vlan != pvid)
1474 		return pvid;
1475 
1476 	is_any = (trusted ||
1477 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1478 
1479 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1480 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1481 	    (is_any && !vlan_filters && f->vlan == 0)) {
1482 		if (is_any)
1483 			return I40E_VLAN_ANY;
1484 		else
1485 			return 0;
1486 	}
1487 
1488 	return f->vlan;
1489 }
1490 
1491 /**
1492  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1493  * @vsi: the vsi to configure
1494  * @tmp_add_list: list of filters ready to be added
1495  * @tmp_del_list: list of filters ready to be deleted
1496  * @vlan_filters: the number of active VLAN filters
1497  * @trusted: flag if the VF is trusted
1498  *
1499  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1500  * and vf-vlan-prune-disable flag.
1501  *
1502  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1503  *
1504  * This function is only expected to be called from within
1505  * i40e_sync_vsi_filters.
1506  *
1507  * NOTE: This function expects to be called while under the
1508  * mac_filter_hash_lock
1509  */
1510 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1511 					    struct hlist_head *tmp_add_list,
1512 					    struct hlist_head *tmp_del_list,
1513 					    int vlan_filters,
1514 					    bool trusted)
1515 {
1516 	struct i40e_mac_filter *f, *add_head;
1517 	struct i40e_new_mac_filter *new_mac;
1518 	struct hlist_node *h;
1519 	int bkt, new_vlan;
1520 
1521 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1522 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1523 							vlan_filters, trusted);
1524 	}
1525 
1526 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1527 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1528 						trusted);
1529 		if (new_vlan != f->vlan) {
1530 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1531 			if (!add_head)
1532 				return -ENOMEM;
1533 			/* Create a temporary i40e_new_mac_filter */
1534 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1535 			if (!new_mac)
1536 				return -ENOMEM;
1537 			new_mac->f = add_head;
1538 			new_mac->state = add_head->state;
1539 
1540 			/* Add the new filter to the tmp list */
1541 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1542 
1543 			/* Put the original filter into the delete list */
1544 			f->state = I40E_FILTER_REMOVE;
1545 			hash_del(&f->hlist);
1546 			hlist_add_head(&f->hlist, tmp_del_list);
1547 		}
1548 	}
1549 
1550 	vsi->has_vlan_filter = !!vlan_filters;
1551 	return 0;
1552 }
1553 
1554 /**
1555  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1556  * @vsi: the PF Main VSI - inappropriate for any other VSI
1557  * @macaddr: the MAC address
1558  *
1559  * Remove whatever filter the firmware set up so the driver can manage
1560  * its own filtering intelligently.
1561  **/
1562 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1563 {
1564 	struct i40e_aqc_remove_macvlan_element_data element;
1565 	struct i40e_pf *pf = vsi->back;
1566 
1567 	/* Only appropriate for the PF main VSI */
1568 	if (vsi->type != I40E_VSI_MAIN)
1569 		return;
1570 
1571 	memset(&element, 0, sizeof(element));
1572 	ether_addr_copy(element.mac_addr, macaddr);
1573 	element.vlan_tag = 0;
1574 	/* Ignore error returns, some firmware does it this way... */
1575 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1576 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1577 
1578 	memset(&element, 0, sizeof(element));
1579 	ether_addr_copy(element.mac_addr, macaddr);
1580 	element.vlan_tag = 0;
1581 	/* ...and some firmware does it this way. */
1582 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1583 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1584 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1585 }
1586 
1587 /**
1588  * i40e_add_filter - Add a mac/vlan filter to the VSI
1589  * @vsi: the VSI to be searched
1590  * @macaddr: the MAC address
1591  * @vlan: the vlan
1592  *
1593  * Returns ptr to the filter object or NULL when no memory available.
1594  *
1595  * NOTE: This function is expected to be called with mac_filter_hash_lock
1596  * being held.
1597  **/
1598 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1599 					const u8 *macaddr, s16 vlan)
1600 {
1601 	struct i40e_mac_filter *f;
1602 	u64 key;
1603 
1604 	if (!vsi || !macaddr)
1605 		return NULL;
1606 
1607 	f = i40e_find_filter(vsi, macaddr, vlan);
1608 	if (!f) {
1609 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1610 		if (!f)
1611 			return NULL;
1612 
1613 		/* Update the boolean indicating if we need to function in
1614 		 * VLAN mode.
1615 		 */
1616 		if (vlan >= 0)
1617 			vsi->has_vlan_filter = true;
1618 
1619 		ether_addr_copy(f->macaddr, macaddr);
1620 		f->vlan = vlan;
1621 		f->state = I40E_FILTER_NEW;
1622 		INIT_HLIST_NODE(&f->hlist);
1623 
1624 		key = i40e_addr_to_hkey(macaddr);
1625 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1626 
1627 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1628 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1629 	}
1630 
1631 	/* If we're asked to add a filter that has been marked for removal, it
1632 	 * is safe to simply restore it to active state. __i40e_del_filter
1633 	 * will have simply deleted any filters which were previously marked
1634 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1635 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1636 	 * task, just restore this filter to the ACTIVE state so that the
1637 	 * sync task leaves it in place
1638 	 */
1639 	if (f->state == I40E_FILTER_REMOVE)
1640 		f->state = I40E_FILTER_ACTIVE;
1641 
1642 	return f;
1643 }
1644 
1645 /**
1646  * __i40e_del_filter - Remove a specific filter from the VSI
1647  * @vsi: VSI to remove from
1648  * @f: the filter to remove from the list
1649  *
1650  * This function should be called instead of i40e_del_filter only if you know
1651  * the exact filter you will remove already, such as via i40e_find_filter or
1652  * i40e_find_mac.
1653  *
1654  * NOTE: This function is expected to be called with mac_filter_hash_lock
1655  * being held.
1656  * ANOTHER NOTE: This function MUST be called from within the context of
1657  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1658  * instead of list_for_each_entry().
1659  **/
1660 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1661 {
1662 	if (!f)
1663 		return;
1664 
1665 	/* If the filter was never added to firmware then we can just delete it
1666 	 * directly and we don't want to set the status to remove or else an
1667 	 * admin queue command will unnecessarily fire.
1668 	 */
1669 	if ((f->state == I40E_FILTER_FAILED) ||
1670 	    (f->state == I40E_FILTER_NEW)) {
1671 		hash_del(&f->hlist);
1672 		kfree(f);
1673 	} else {
1674 		f->state = I40E_FILTER_REMOVE;
1675 	}
1676 
1677 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1678 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1679 }
1680 
1681 /**
1682  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1683  * @vsi: the VSI to be searched
1684  * @macaddr: the MAC address
1685  * @vlan: the VLAN
1686  *
1687  * NOTE: This function is expected to be called with mac_filter_hash_lock
1688  * being held.
1689  * ANOTHER NOTE: This function MUST be called from within the context of
1690  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1691  * instead of list_for_each_entry().
1692  **/
1693 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1694 {
1695 	struct i40e_mac_filter *f;
1696 
1697 	if (!vsi || !macaddr)
1698 		return;
1699 
1700 	f = i40e_find_filter(vsi, macaddr, vlan);
1701 	__i40e_del_filter(vsi, f);
1702 }
1703 
1704 /**
1705  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1706  * @vsi: the VSI to be searched
1707  * @macaddr: the mac address to be filtered
1708  *
1709  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1710  * go through all the macvlan filters and add a macvlan filter for each
1711  * unique vlan that already exists. If a PVID has been assigned, instead only
1712  * add the macaddr to that VLAN.
1713  *
1714  * Returns last filter added on success, else NULL
1715  **/
1716 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1717 					    const u8 *macaddr)
1718 {
1719 	struct i40e_mac_filter *f, *add = NULL;
1720 	struct hlist_node *h;
1721 	int bkt;
1722 
1723 	if (vsi->info.pvid)
1724 		return i40e_add_filter(vsi, macaddr,
1725 				       le16_to_cpu(vsi->info.pvid));
1726 
1727 	if (!i40e_is_vsi_in_vlan(vsi))
1728 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1729 
1730 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1731 		if (f->state == I40E_FILTER_REMOVE)
1732 			continue;
1733 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1734 		if (!add)
1735 			return NULL;
1736 	}
1737 
1738 	return add;
1739 }
1740 
1741 /**
1742  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1743  * @vsi: the VSI to be searched
1744  * @macaddr: the mac address to be removed
1745  *
1746  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1747  * associated with.
1748  *
1749  * Returns 0 for success, or error
1750  **/
1751 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1752 {
1753 	struct i40e_mac_filter *f;
1754 	struct hlist_node *h;
1755 	bool found = false;
1756 	int bkt;
1757 
1758 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1759 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1760 		if (ether_addr_equal(macaddr, f->macaddr)) {
1761 			__i40e_del_filter(vsi, f);
1762 			found = true;
1763 		}
1764 	}
1765 
1766 	if (found)
1767 		return 0;
1768 	else
1769 		return -ENOENT;
1770 }
1771 
1772 /**
1773  * i40e_set_mac - NDO callback to set mac address
1774  * @netdev: network interface device structure
1775  * @p: pointer to an address structure
1776  *
1777  * Returns 0 on success, negative on failure
1778  **/
1779 static int i40e_set_mac(struct net_device *netdev, void *p)
1780 {
1781 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1782 	struct i40e_vsi *vsi = np->vsi;
1783 	struct i40e_pf *pf = vsi->back;
1784 	struct i40e_hw *hw = &pf->hw;
1785 	struct sockaddr *addr = p;
1786 
1787 	if (!is_valid_ether_addr(addr->sa_data))
1788 		return -EADDRNOTAVAIL;
1789 
1790 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1791 		netdev_info(netdev, "already using mac address %pM\n",
1792 			    addr->sa_data);
1793 		return 0;
1794 	}
1795 
1796 	if (test_bit(__I40E_DOWN, pf->state) ||
1797 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1798 		return -EADDRNOTAVAIL;
1799 
1800 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1801 		netdev_info(netdev, "returning to hw mac address %pM\n",
1802 			    hw->mac.addr);
1803 	else
1804 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1805 
1806 	/* Copy the address first, so that we avoid a possible race with
1807 	 * .set_rx_mode().
1808 	 * - Remove old address from MAC filter
1809 	 * - Copy new address
1810 	 * - Add new address to MAC filter
1811 	 */
1812 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1813 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1814 	eth_hw_addr_set(netdev, addr->sa_data);
1815 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1816 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1817 
1818 	if (vsi->type == I40E_VSI_MAIN) {
1819 		i40e_status ret;
1820 
1821 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1822 						addr->sa_data, NULL);
1823 		if (ret)
1824 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1825 				    i40e_stat_str(hw, ret),
1826 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1827 	}
1828 
1829 	/* schedule our worker thread which will take care of
1830 	 * applying the new filter changes
1831 	 */
1832 	i40e_service_event_schedule(pf);
1833 	return 0;
1834 }
1835 
1836 /**
1837  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1838  * @vsi: vsi structure
1839  * @seed: RSS hash seed
1840  * @lut: pointer to lookup table of lut_size
1841  * @lut_size: size of the lookup table
1842  **/
1843 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1844 			      u8 *lut, u16 lut_size)
1845 {
1846 	struct i40e_pf *pf = vsi->back;
1847 	struct i40e_hw *hw = &pf->hw;
1848 	int ret = 0;
1849 
1850 	if (seed) {
1851 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1852 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1853 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1854 		if (ret) {
1855 			dev_info(&pf->pdev->dev,
1856 				 "Cannot set RSS key, err %s aq_err %s\n",
1857 				 i40e_stat_str(hw, ret),
1858 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1859 			return ret;
1860 		}
1861 	}
1862 	if (lut) {
1863 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1864 
1865 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1866 		if (ret) {
1867 			dev_info(&pf->pdev->dev,
1868 				 "Cannot set RSS lut, err %s aq_err %s\n",
1869 				 i40e_stat_str(hw, ret),
1870 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1871 			return ret;
1872 		}
1873 	}
1874 	return ret;
1875 }
1876 
1877 /**
1878  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1879  * @vsi: VSI structure
1880  **/
1881 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1882 {
1883 	struct i40e_pf *pf = vsi->back;
1884 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1885 	u8 *lut;
1886 	int ret;
1887 
1888 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1889 		return 0;
1890 	if (!vsi->rss_size)
1891 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1892 				      vsi->num_queue_pairs);
1893 	if (!vsi->rss_size)
1894 		return -EINVAL;
1895 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1896 	if (!lut)
1897 		return -ENOMEM;
1898 
1899 	/* Use the user configured hash keys and lookup table if there is one,
1900 	 * otherwise use default
1901 	 */
1902 	if (vsi->rss_lut_user)
1903 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1904 	else
1905 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1906 	if (vsi->rss_hkey_user)
1907 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1908 	else
1909 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1910 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1911 	kfree(lut);
1912 	return ret;
1913 }
1914 
1915 /**
1916  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1917  * @vsi: the VSI being configured,
1918  * @ctxt: VSI context structure
1919  * @enabled_tc: number of traffic classes to enable
1920  *
1921  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1922  **/
1923 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1924 					   struct i40e_vsi_context *ctxt,
1925 					   u8 enabled_tc)
1926 {
1927 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1928 	int i, override_q, pow, num_qps, ret;
1929 	u8 netdev_tc = 0, offset = 0;
1930 
1931 	if (vsi->type != I40E_VSI_MAIN)
1932 		return -EINVAL;
1933 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1934 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1935 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1936 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1937 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1938 
1939 	/* find the next higher power-of-2 of num queue pairs */
1940 	pow = ilog2(num_qps);
1941 	if (!is_power_of_2(num_qps))
1942 		pow++;
1943 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1944 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1945 
1946 	/* Setup queue offset/count for all TCs for given VSI */
1947 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1948 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1949 		/* See if the given TC is enabled for the given VSI */
1950 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1951 			offset = vsi->mqprio_qopt.qopt.offset[i];
1952 			qcount = vsi->mqprio_qopt.qopt.count[i];
1953 			if (qcount > max_qcount)
1954 				max_qcount = qcount;
1955 			vsi->tc_config.tc_info[i].qoffset = offset;
1956 			vsi->tc_config.tc_info[i].qcount = qcount;
1957 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1958 		} else {
1959 			/* TC is not enabled so set the offset to
1960 			 * default queue and allocate one queue
1961 			 * for the given TC.
1962 			 */
1963 			vsi->tc_config.tc_info[i].qoffset = 0;
1964 			vsi->tc_config.tc_info[i].qcount = 1;
1965 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1966 		}
1967 	}
1968 
1969 	/* Set actual Tx/Rx queue pairs */
1970 	vsi->num_queue_pairs = offset + qcount;
1971 
1972 	/* Setup queue TC[0].qmap for given VSI context */
1973 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1974 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1975 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1976 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1977 
1978 	/* Reconfigure RSS for main VSI with max queue count */
1979 	vsi->rss_size = max_qcount;
1980 	ret = i40e_vsi_config_rss(vsi);
1981 	if (ret) {
1982 		dev_info(&vsi->back->pdev->dev,
1983 			 "Failed to reconfig rss for num_queues (%u)\n",
1984 			 max_qcount);
1985 		return ret;
1986 	}
1987 	vsi->reconfig_rss = true;
1988 	dev_dbg(&vsi->back->pdev->dev,
1989 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1990 
1991 	/* Find queue count available for channel VSIs and starting offset
1992 	 * for channel VSIs
1993 	 */
1994 	override_q = vsi->mqprio_qopt.qopt.count[0];
1995 	if (override_q && override_q < vsi->num_queue_pairs) {
1996 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1997 		vsi->next_base_queue = override_q;
1998 	}
1999 	return 0;
2000 }
2001 
2002 /**
2003  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2004  * @vsi: the VSI being setup
2005  * @ctxt: VSI context structure
2006  * @enabled_tc: Enabled TCs bitmap
2007  * @is_add: True if called before Add VSI
2008  *
2009  * Setup VSI queue mapping for enabled traffic classes.
2010  **/
2011 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2012 				     struct i40e_vsi_context *ctxt,
2013 				     u8 enabled_tc,
2014 				     bool is_add)
2015 {
2016 	struct i40e_pf *pf = vsi->back;
2017 	u16 num_tc_qps = 0;
2018 	u16 sections = 0;
2019 	u8 netdev_tc = 0;
2020 	u16 numtc = 1;
2021 	u16 qcount;
2022 	u8 offset;
2023 	u16 qmap;
2024 	int i;
2025 
2026 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2027 	offset = 0;
2028 	/* zero out queue mapping, it will get updated on the end of the function */
2029 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2030 
2031 	if (vsi->type == I40E_VSI_MAIN) {
2032 		/* This code helps add more queue to the VSI if we have
2033 		 * more cores than RSS can support, the higher cores will
2034 		 * be served by ATR or other filters. Furthermore, the
2035 		 * non-zero req_queue_pairs says that user requested a new
2036 		 * queue count via ethtool's set_channels, so use this
2037 		 * value for queues distribution across traffic classes
2038 		 * We need at least one queue pair for the interface
2039 		 * to be usable as we see in else statement.
2040 		 */
2041 		if (vsi->req_queue_pairs > 0)
2042 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2043 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2044 			vsi->num_queue_pairs = pf->num_lan_msix;
2045 		else
2046 			vsi->num_queue_pairs = 1;
2047 	}
2048 
2049 	/* Number of queues per enabled TC */
2050 	if (vsi->type == I40E_VSI_MAIN ||
2051 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2052 		num_tc_qps = vsi->num_queue_pairs;
2053 	else
2054 		num_tc_qps = vsi->alloc_queue_pairs;
2055 
2056 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2057 		/* Find numtc from enabled TC bitmap */
2058 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2059 			if (enabled_tc & BIT(i)) /* TC is enabled */
2060 				numtc++;
2061 		}
2062 		if (!numtc) {
2063 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2064 			numtc = 1;
2065 		}
2066 		num_tc_qps = num_tc_qps / numtc;
2067 		num_tc_qps = min_t(int, num_tc_qps,
2068 				   i40e_pf_get_max_q_per_tc(pf));
2069 	}
2070 
2071 	vsi->tc_config.numtc = numtc;
2072 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2073 
2074 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2075 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2076 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2077 
2078 	/* Setup queue offset/count for all TCs for given VSI */
2079 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2080 		/* See if the given TC is enabled for the given VSI */
2081 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2082 			/* TC is enabled */
2083 			int pow, num_qps;
2084 
2085 			switch (vsi->type) {
2086 			case I40E_VSI_MAIN:
2087 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2088 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2089 				    vsi->tc_config.enabled_tc != 1) {
2090 					qcount = min_t(int, pf->alloc_rss_size,
2091 						       num_tc_qps);
2092 					break;
2093 				}
2094 				fallthrough;
2095 			case I40E_VSI_FDIR:
2096 			case I40E_VSI_SRIOV:
2097 			case I40E_VSI_VMDQ2:
2098 			default:
2099 				qcount = num_tc_qps;
2100 				WARN_ON(i != 0);
2101 				break;
2102 			}
2103 			vsi->tc_config.tc_info[i].qoffset = offset;
2104 			vsi->tc_config.tc_info[i].qcount = qcount;
2105 
2106 			/* find the next higher power-of-2 of num queue pairs */
2107 			num_qps = qcount;
2108 			pow = 0;
2109 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2110 				pow++;
2111 				num_qps >>= 1;
2112 			}
2113 
2114 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2115 			qmap =
2116 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2117 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2118 
2119 			offset += qcount;
2120 		} else {
2121 			/* TC is not enabled so set the offset to
2122 			 * default queue and allocate one queue
2123 			 * for the given TC.
2124 			 */
2125 			vsi->tc_config.tc_info[i].qoffset = 0;
2126 			vsi->tc_config.tc_info[i].qcount = 1;
2127 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2128 
2129 			qmap = 0;
2130 		}
2131 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2132 	}
2133 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2134 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2135 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2136 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2137 		vsi->num_queue_pairs = offset;
2138 
2139 	/* Scheduler section valid can only be set for ADD VSI */
2140 	if (is_add) {
2141 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2142 
2143 		ctxt->info.up_enable_bits = enabled_tc;
2144 	}
2145 	if (vsi->type == I40E_VSI_SRIOV) {
2146 		ctxt->info.mapping_flags |=
2147 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2148 		for (i = 0; i < vsi->num_queue_pairs; i++)
2149 			ctxt->info.queue_mapping[i] =
2150 					       cpu_to_le16(vsi->base_queue + i);
2151 	} else {
2152 		ctxt->info.mapping_flags |=
2153 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2154 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2155 	}
2156 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2157 }
2158 
2159 /**
2160  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2161  * @netdev: the netdevice
2162  * @addr: address to add
2163  *
2164  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2165  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2166  */
2167 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2168 {
2169 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2170 	struct i40e_vsi *vsi = np->vsi;
2171 
2172 	if (i40e_add_mac_filter(vsi, addr))
2173 		return 0;
2174 	else
2175 		return -ENOMEM;
2176 }
2177 
2178 /**
2179  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2180  * @netdev: the netdevice
2181  * @addr: address to add
2182  *
2183  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2184  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2185  */
2186 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2187 {
2188 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2189 	struct i40e_vsi *vsi = np->vsi;
2190 
2191 	/* Under some circumstances, we might receive a request to delete
2192 	 * our own device address from our uc list. Because we store the
2193 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2194 	 * such requests and not delete our device address from this list.
2195 	 */
2196 	if (ether_addr_equal(addr, netdev->dev_addr))
2197 		return 0;
2198 
2199 	i40e_del_mac_filter(vsi, addr);
2200 
2201 	return 0;
2202 }
2203 
2204 /**
2205  * i40e_set_rx_mode - NDO callback to set the netdev filters
2206  * @netdev: network interface device structure
2207  **/
2208 static void i40e_set_rx_mode(struct net_device *netdev)
2209 {
2210 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2211 	struct i40e_vsi *vsi = np->vsi;
2212 
2213 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2214 
2215 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2216 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2217 
2218 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2219 
2220 	/* check for other flag changes */
2221 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2222 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2223 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2224 	}
2225 }
2226 
2227 /**
2228  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2229  * @vsi: Pointer to VSI struct
2230  * @from: Pointer to list which contains MAC filter entries - changes to
2231  *        those entries needs to be undone.
2232  *
2233  * MAC filter entries from this list were slated for deletion.
2234  **/
2235 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2236 					 struct hlist_head *from)
2237 {
2238 	struct i40e_mac_filter *f;
2239 	struct hlist_node *h;
2240 
2241 	hlist_for_each_entry_safe(f, h, from, hlist) {
2242 		u64 key = i40e_addr_to_hkey(f->macaddr);
2243 
2244 		/* Move the element back into MAC filter list*/
2245 		hlist_del(&f->hlist);
2246 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2247 	}
2248 }
2249 
2250 /**
2251  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2252  * @vsi: Pointer to vsi struct
2253  * @from: Pointer to list which contains MAC filter entries - changes to
2254  *        those entries needs to be undone.
2255  *
2256  * MAC filter entries from this list were slated for addition.
2257  **/
2258 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2259 					 struct hlist_head *from)
2260 {
2261 	struct i40e_new_mac_filter *new;
2262 	struct hlist_node *h;
2263 
2264 	hlist_for_each_entry_safe(new, h, from, hlist) {
2265 		/* We can simply free the wrapper structure */
2266 		hlist_del(&new->hlist);
2267 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2268 		kfree(new);
2269 	}
2270 }
2271 
2272 /**
2273  * i40e_next_filter - Get the next non-broadcast filter from a list
2274  * @next: pointer to filter in list
2275  *
2276  * Returns the next non-broadcast filter in the list. Required so that we
2277  * ignore broadcast filters within the list, since these are not handled via
2278  * the normal firmware update path.
2279  */
2280 static
2281 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2282 {
2283 	hlist_for_each_entry_continue(next, hlist) {
2284 		if (!is_broadcast_ether_addr(next->f->macaddr))
2285 			return next;
2286 	}
2287 
2288 	return NULL;
2289 }
2290 
2291 /**
2292  * i40e_update_filter_state - Update filter state based on return data
2293  * from firmware
2294  * @count: Number of filters added
2295  * @add_list: return data from fw
2296  * @add_head: pointer to first filter in current batch
2297  *
2298  * MAC filter entries from list were slated to be added to device. Returns
2299  * number of successful filters. Note that 0 does NOT mean success!
2300  **/
2301 static int
2302 i40e_update_filter_state(int count,
2303 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2304 			 struct i40e_new_mac_filter *add_head)
2305 {
2306 	int retval = 0;
2307 	int i;
2308 
2309 	for (i = 0; i < count; i++) {
2310 		/* Always check status of each filter. We don't need to check
2311 		 * the firmware return status because we pre-set the filter
2312 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2313 		 * request to the adminq. Thus, if it no longer matches then
2314 		 * we know the filter is active.
2315 		 */
2316 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2317 			add_head->state = I40E_FILTER_FAILED;
2318 		} else {
2319 			add_head->state = I40E_FILTER_ACTIVE;
2320 			retval++;
2321 		}
2322 
2323 		add_head = i40e_next_filter(add_head);
2324 		if (!add_head)
2325 			break;
2326 	}
2327 
2328 	return retval;
2329 }
2330 
2331 /**
2332  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2333  * @vsi: ptr to the VSI
2334  * @vsi_name: name to display in messages
2335  * @list: the list of filters to send to firmware
2336  * @num_del: the number of filters to delete
2337  * @retval: Set to -EIO on failure to delete
2338  *
2339  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2340  * *retval instead of a return value so that success does not force ret_val to
2341  * be set to 0. This ensures that a sequence of calls to this function
2342  * preserve the previous value of *retval on successful delete.
2343  */
2344 static
2345 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2346 			  struct i40e_aqc_remove_macvlan_element_data *list,
2347 			  int num_del, int *retval)
2348 {
2349 	struct i40e_hw *hw = &vsi->back->hw;
2350 	enum i40e_admin_queue_err aq_status;
2351 	i40e_status aq_ret;
2352 
2353 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2354 					   &aq_status);
2355 
2356 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2357 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2358 		*retval = -EIO;
2359 		dev_info(&vsi->back->pdev->dev,
2360 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2361 			 vsi_name, i40e_stat_str(hw, aq_ret),
2362 			 i40e_aq_str(hw, aq_status));
2363 	}
2364 }
2365 
2366 /**
2367  * i40e_aqc_add_filters - Request firmware to add a set of filters
2368  * @vsi: ptr to the VSI
2369  * @vsi_name: name to display in messages
2370  * @list: the list of filters to send to firmware
2371  * @add_head: Position in the add hlist
2372  * @num_add: the number of filters to add
2373  *
2374  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2375  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2376  * space for more filters.
2377  */
2378 static
2379 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2380 			  struct i40e_aqc_add_macvlan_element_data *list,
2381 			  struct i40e_new_mac_filter *add_head,
2382 			  int num_add)
2383 {
2384 	struct i40e_hw *hw = &vsi->back->hw;
2385 	enum i40e_admin_queue_err aq_status;
2386 	int fcnt;
2387 
2388 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2389 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2390 
2391 	if (fcnt != num_add) {
2392 		if (vsi->type == I40E_VSI_MAIN) {
2393 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2394 			dev_warn(&vsi->back->pdev->dev,
2395 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2396 				 i40e_aq_str(hw, aq_status), vsi_name);
2397 		} else if (vsi->type == I40E_VSI_SRIOV ||
2398 			   vsi->type == I40E_VSI_VMDQ1 ||
2399 			   vsi->type == I40E_VSI_VMDQ2) {
2400 			dev_warn(&vsi->back->pdev->dev,
2401 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2402 				 i40e_aq_str(hw, aq_status), vsi_name,
2403 					     vsi_name);
2404 		} else {
2405 			dev_warn(&vsi->back->pdev->dev,
2406 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2407 				 i40e_aq_str(hw, aq_status), vsi_name,
2408 					     vsi->type);
2409 		}
2410 	}
2411 }
2412 
2413 /**
2414  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2415  * @vsi: pointer to the VSI
2416  * @vsi_name: the VSI name
2417  * @f: filter data
2418  *
2419  * This function sets or clears the promiscuous broadcast flags for VLAN
2420  * filters in order to properly receive broadcast frames. Assumes that only
2421  * broadcast filters are passed.
2422  *
2423  * Returns status indicating success or failure;
2424  **/
2425 static i40e_status
2426 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2427 			  struct i40e_mac_filter *f)
2428 {
2429 	bool enable = f->state == I40E_FILTER_NEW;
2430 	struct i40e_hw *hw = &vsi->back->hw;
2431 	i40e_status aq_ret;
2432 
2433 	if (f->vlan == I40E_VLAN_ANY) {
2434 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2435 						   vsi->seid,
2436 						   enable,
2437 						   NULL);
2438 	} else {
2439 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2440 							    vsi->seid,
2441 							    enable,
2442 							    f->vlan,
2443 							    NULL);
2444 	}
2445 
2446 	if (aq_ret) {
2447 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2448 		dev_warn(&vsi->back->pdev->dev,
2449 			 "Error %s, forcing overflow promiscuous on %s\n",
2450 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2451 			 vsi_name);
2452 	}
2453 
2454 	return aq_ret;
2455 }
2456 
2457 /**
2458  * i40e_set_promiscuous - set promiscuous mode
2459  * @pf: board private structure
2460  * @promisc: promisc on or off
2461  *
2462  * There are different ways of setting promiscuous mode on a PF depending on
2463  * what state/environment we're in.  This identifies and sets it appropriately.
2464  * Returns 0 on success.
2465  **/
2466 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2467 {
2468 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2469 	struct i40e_hw *hw = &pf->hw;
2470 	i40e_status aq_ret;
2471 
2472 	if (vsi->type == I40E_VSI_MAIN &&
2473 	    pf->lan_veb != I40E_NO_VEB &&
2474 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2475 		/* set defport ON for Main VSI instead of true promisc
2476 		 * this way we will get all unicast/multicast and VLAN
2477 		 * promisc behavior but will not get VF or VMDq traffic
2478 		 * replicated on the Main VSI.
2479 		 */
2480 		if (promisc)
2481 			aq_ret = i40e_aq_set_default_vsi(hw,
2482 							 vsi->seid,
2483 							 NULL);
2484 		else
2485 			aq_ret = i40e_aq_clear_default_vsi(hw,
2486 							   vsi->seid,
2487 							   NULL);
2488 		if (aq_ret) {
2489 			dev_info(&pf->pdev->dev,
2490 				 "Set default VSI failed, err %s, aq_err %s\n",
2491 				 i40e_stat_str(hw, aq_ret),
2492 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2493 		}
2494 	} else {
2495 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2496 						  hw,
2497 						  vsi->seid,
2498 						  promisc, NULL,
2499 						  true);
2500 		if (aq_ret) {
2501 			dev_info(&pf->pdev->dev,
2502 				 "set unicast promisc failed, err %s, aq_err %s\n",
2503 				 i40e_stat_str(hw, aq_ret),
2504 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2505 		}
2506 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2507 						  hw,
2508 						  vsi->seid,
2509 						  promisc, NULL);
2510 		if (aq_ret) {
2511 			dev_info(&pf->pdev->dev,
2512 				 "set multicast promisc failed, err %s, aq_err %s\n",
2513 				 i40e_stat_str(hw, aq_ret),
2514 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2515 		}
2516 	}
2517 
2518 	if (!aq_ret)
2519 		pf->cur_promisc = promisc;
2520 
2521 	return aq_ret;
2522 }
2523 
2524 /**
2525  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2526  * @vsi: ptr to the VSI
2527  *
2528  * Push any outstanding VSI filter changes through the AdminQ.
2529  *
2530  * Returns 0 or error value
2531  **/
2532 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2533 {
2534 	struct hlist_head tmp_add_list, tmp_del_list;
2535 	struct i40e_mac_filter *f;
2536 	struct i40e_new_mac_filter *new, *add_head = NULL;
2537 	struct i40e_hw *hw = &vsi->back->hw;
2538 	bool old_overflow, new_overflow;
2539 	unsigned int failed_filters = 0;
2540 	unsigned int vlan_filters = 0;
2541 	char vsi_name[16] = "PF";
2542 	int filter_list_len = 0;
2543 	i40e_status aq_ret = 0;
2544 	u32 changed_flags = 0;
2545 	struct hlist_node *h;
2546 	struct i40e_pf *pf;
2547 	int num_add = 0;
2548 	int num_del = 0;
2549 	int retval = 0;
2550 	u16 cmd_flags;
2551 	int list_size;
2552 	int bkt;
2553 
2554 	/* empty array typed pointers, kcalloc later */
2555 	struct i40e_aqc_add_macvlan_element_data *add_list;
2556 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2557 
2558 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2559 		usleep_range(1000, 2000);
2560 	pf = vsi->back;
2561 
2562 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2563 
2564 	if (vsi->netdev) {
2565 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2566 		vsi->current_netdev_flags = vsi->netdev->flags;
2567 	}
2568 
2569 	INIT_HLIST_HEAD(&tmp_add_list);
2570 	INIT_HLIST_HEAD(&tmp_del_list);
2571 
2572 	if (vsi->type == I40E_VSI_SRIOV)
2573 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2574 	else if (vsi->type != I40E_VSI_MAIN)
2575 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2576 
2577 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2578 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2579 
2580 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2581 		/* Create a list of filters to delete. */
2582 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2583 			if (f->state == I40E_FILTER_REMOVE) {
2584 				/* Move the element into temporary del_list */
2585 				hash_del(&f->hlist);
2586 				hlist_add_head(&f->hlist, &tmp_del_list);
2587 
2588 				/* Avoid counting removed filters */
2589 				continue;
2590 			}
2591 			if (f->state == I40E_FILTER_NEW) {
2592 				/* Create a temporary i40e_new_mac_filter */
2593 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2594 				if (!new)
2595 					goto err_no_memory_locked;
2596 
2597 				/* Store pointer to the real filter */
2598 				new->f = f;
2599 				new->state = f->state;
2600 
2601 				/* Add it to the hash list */
2602 				hlist_add_head(&new->hlist, &tmp_add_list);
2603 			}
2604 
2605 			/* Count the number of active (current and new) VLAN
2606 			 * filters we have now. Does not count filters which
2607 			 * are marked for deletion.
2608 			 */
2609 			if (f->vlan > 0)
2610 				vlan_filters++;
2611 		}
2612 
2613 		if (vsi->type != I40E_VSI_SRIOV)
2614 			retval = i40e_correct_mac_vlan_filters
2615 				(vsi, &tmp_add_list, &tmp_del_list,
2616 				 vlan_filters);
2617 		else
2618 			retval = i40e_correct_vf_mac_vlan_filters
2619 				(vsi, &tmp_add_list, &tmp_del_list,
2620 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2621 
2622 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2623 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2624 
2625 		if (retval)
2626 			goto err_no_memory_locked;
2627 
2628 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2629 	}
2630 
2631 	/* Now process 'del_list' outside the lock */
2632 	if (!hlist_empty(&tmp_del_list)) {
2633 		filter_list_len = hw->aq.asq_buf_size /
2634 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2635 		list_size = filter_list_len *
2636 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2637 		del_list = kzalloc(list_size, GFP_ATOMIC);
2638 		if (!del_list)
2639 			goto err_no_memory;
2640 
2641 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2642 			cmd_flags = 0;
2643 
2644 			/* handle broadcast filters by updating the broadcast
2645 			 * promiscuous flag and release filter list.
2646 			 */
2647 			if (is_broadcast_ether_addr(f->macaddr)) {
2648 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2649 
2650 				hlist_del(&f->hlist);
2651 				kfree(f);
2652 				continue;
2653 			}
2654 
2655 			/* add to delete list */
2656 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2657 			if (f->vlan == I40E_VLAN_ANY) {
2658 				del_list[num_del].vlan_tag = 0;
2659 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2660 			} else {
2661 				del_list[num_del].vlan_tag =
2662 					cpu_to_le16((u16)(f->vlan));
2663 			}
2664 
2665 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2666 			del_list[num_del].flags = cmd_flags;
2667 			num_del++;
2668 
2669 			/* flush a full buffer */
2670 			if (num_del == filter_list_len) {
2671 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2672 						     num_del, &retval);
2673 				memset(del_list, 0, list_size);
2674 				num_del = 0;
2675 			}
2676 			/* Release memory for MAC filter entries which were
2677 			 * synced up with HW.
2678 			 */
2679 			hlist_del(&f->hlist);
2680 			kfree(f);
2681 		}
2682 
2683 		if (num_del) {
2684 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2685 					     num_del, &retval);
2686 		}
2687 
2688 		kfree(del_list);
2689 		del_list = NULL;
2690 	}
2691 
2692 	if (!hlist_empty(&tmp_add_list)) {
2693 		/* Do all the adds now. */
2694 		filter_list_len = hw->aq.asq_buf_size /
2695 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2696 		list_size = filter_list_len *
2697 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2698 		add_list = kzalloc(list_size, GFP_ATOMIC);
2699 		if (!add_list)
2700 			goto err_no_memory;
2701 
2702 		num_add = 0;
2703 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2704 			/* handle broadcast filters by updating the broadcast
2705 			 * promiscuous flag instead of adding a MAC filter.
2706 			 */
2707 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2708 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2709 							      new->f))
2710 					new->state = I40E_FILTER_FAILED;
2711 				else
2712 					new->state = I40E_FILTER_ACTIVE;
2713 				continue;
2714 			}
2715 
2716 			/* add to add array */
2717 			if (num_add == 0)
2718 				add_head = new;
2719 			cmd_flags = 0;
2720 			ether_addr_copy(add_list[num_add].mac_addr,
2721 					new->f->macaddr);
2722 			if (new->f->vlan == I40E_VLAN_ANY) {
2723 				add_list[num_add].vlan_tag = 0;
2724 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2725 			} else {
2726 				add_list[num_add].vlan_tag =
2727 					cpu_to_le16((u16)(new->f->vlan));
2728 			}
2729 			add_list[num_add].queue_number = 0;
2730 			/* set invalid match method for later detection */
2731 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2732 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2733 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2734 			num_add++;
2735 
2736 			/* flush a full buffer */
2737 			if (num_add == filter_list_len) {
2738 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2739 						     add_head, num_add);
2740 				memset(add_list, 0, list_size);
2741 				num_add = 0;
2742 			}
2743 		}
2744 		if (num_add) {
2745 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2746 					     num_add);
2747 		}
2748 		/* Now move all of the filters from the temp add list back to
2749 		 * the VSI's list.
2750 		 */
2751 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2752 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2753 			/* Only update the state if we're still NEW */
2754 			if (new->f->state == I40E_FILTER_NEW)
2755 				new->f->state = new->state;
2756 			hlist_del(&new->hlist);
2757 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2758 			kfree(new);
2759 		}
2760 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2761 		kfree(add_list);
2762 		add_list = NULL;
2763 	}
2764 
2765 	/* Determine the number of active and failed filters. */
2766 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2767 	vsi->active_filters = 0;
2768 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2769 		if (f->state == I40E_FILTER_ACTIVE)
2770 			vsi->active_filters++;
2771 		else if (f->state == I40E_FILTER_FAILED)
2772 			failed_filters++;
2773 	}
2774 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2775 
2776 	/* Check if we are able to exit overflow promiscuous mode. We can
2777 	 * safely exit if we didn't just enter, we no longer have any failed
2778 	 * filters, and we have reduced filters below the threshold value.
2779 	 */
2780 	if (old_overflow && !failed_filters &&
2781 	    vsi->active_filters < vsi->promisc_threshold) {
2782 		dev_info(&pf->pdev->dev,
2783 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2784 			 vsi_name);
2785 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2786 		vsi->promisc_threshold = 0;
2787 	}
2788 
2789 	/* if the VF is not trusted do not do promisc */
2790 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2791 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2792 		goto out;
2793 	}
2794 
2795 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2796 
2797 	/* If we are entering overflow promiscuous, we need to calculate a new
2798 	 * threshold for when we are safe to exit
2799 	 */
2800 	if (!old_overflow && new_overflow)
2801 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2802 
2803 	/* check for changes in promiscuous modes */
2804 	if (changed_flags & IFF_ALLMULTI) {
2805 		bool cur_multipromisc;
2806 
2807 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2808 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2809 							       vsi->seid,
2810 							       cur_multipromisc,
2811 							       NULL);
2812 		if (aq_ret) {
2813 			retval = i40e_aq_rc_to_posix(aq_ret,
2814 						     hw->aq.asq_last_status);
2815 			dev_info(&pf->pdev->dev,
2816 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2817 				 vsi_name,
2818 				 i40e_stat_str(hw, aq_ret),
2819 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2820 		} else {
2821 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2822 				 cur_multipromisc ? "entering" : "leaving");
2823 		}
2824 	}
2825 
2826 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2827 		bool cur_promisc;
2828 
2829 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2830 			       new_overflow);
2831 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2832 		if (aq_ret) {
2833 			retval = i40e_aq_rc_to_posix(aq_ret,
2834 						     hw->aq.asq_last_status);
2835 			dev_info(&pf->pdev->dev,
2836 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2837 				 cur_promisc ? "on" : "off",
2838 				 vsi_name,
2839 				 i40e_stat_str(hw, aq_ret),
2840 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2841 		}
2842 	}
2843 out:
2844 	/* if something went wrong then set the changed flag so we try again */
2845 	if (retval)
2846 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2847 
2848 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2849 	return retval;
2850 
2851 err_no_memory:
2852 	/* Restore elements on the temporary add and delete lists */
2853 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2854 err_no_memory_locked:
2855 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2856 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2857 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2858 
2859 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2860 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2861 	return -ENOMEM;
2862 }
2863 
2864 /**
2865  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2866  * @pf: board private structure
2867  **/
2868 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2869 {
2870 	int v;
2871 
2872 	if (!pf)
2873 		return;
2874 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2875 		return;
2876 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2877 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2878 		return;
2879 	}
2880 
2881 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2882 		if (pf->vsi[v] &&
2883 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2884 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2885 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2886 
2887 			if (ret) {
2888 				/* come back and try again later */
2889 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2890 					pf->state);
2891 				break;
2892 			}
2893 		}
2894 	}
2895 }
2896 
2897 /**
2898  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2899  * @vsi: the vsi
2900  **/
2901 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2902 {
2903 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2904 		return I40E_RXBUFFER_2048;
2905 	else
2906 		return I40E_RXBUFFER_3072;
2907 }
2908 
2909 /**
2910  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2911  * @netdev: network interface device structure
2912  * @new_mtu: new value for maximum frame size
2913  *
2914  * Returns 0 on success, negative on failure
2915  **/
2916 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2917 {
2918 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2919 	struct i40e_vsi *vsi = np->vsi;
2920 	struct i40e_pf *pf = vsi->back;
2921 
2922 	if (i40e_enabled_xdp_vsi(vsi)) {
2923 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2924 
2925 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2926 			return -EINVAL;
2927 	}
2928 
2929 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2930 		   netdev->mtu, new_mtu);
2931 	netdev->mtu = new_mtu;
2932 	if (netif_running(netdev))
2933 		i40e_vsi_reinit_locked(vsi);
2934 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2935 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2936 	return 0;
2937 }
2938 
2939 /**
2940  * i40e_ioctl - Access the hwtstamp interface
2941  * @netdev: network interface device structure
2942  * @ifr: interface request data
2943  * @cmd: ioctl command
2944  **/
2945 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2946 {
2947 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2948 	struct i40e_pf *pf = np->vsi->back;
2949 
2950 	switch (cmd) {
2951 	case SIOCGHWTSTAMP:
2952 		return i40e_ptp_get_ts_config(pf, ifr);
2953 	case SIOCSHWTSTAMP:
2954 		return i40e_ptp_set_ts_config(pf, ifr);
2955 	default:
2956 		return -EOPNOTSUPP;
2957 	}
2958 }
2959 
2960 /**
2961  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2962  * @vsi: the vsi being adjusted
2963  **/
2964 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2965 {
2966 	struct i40e_vsi_context ctxt;
2967 	i40e_status ret;
2968 
2969 	/* Don't modify stripping options if a port VLAN is active */
2970 	if (vsi->info.pvid)
2971 		return;
2972 
2973 	if ((vsi->info.valid_sections &
2974 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2975 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2976 		return;  /* already enabled */
2977 
2978 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2979 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2980 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2981 
2982 	ctxt.seid = vsi->seid;
2983 	ctxt.info = vsi->info;
2984 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2985 	if (ret) {
2986 		dev_info(&vsi->back->pdev->dev,
2987 			 "update vlan stripping failed, err %s aq_err %s\n",
2988 			 i40e_stat_str(&vsi->back->hw, ret),
2989 			 i40e_aq_str(&vsi->back->hw,
2990 				     vsi->back->hw.aq.asq_last_status));
2991 	}
2992 }
2993 
2994 /**
2995  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2996  * @vsi: the vsi being adjusted
2997  **/
2998 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2999 {
3000 	struct i40e_vsi_context ctxt;
3001 	i40e_status ret;
3002 
3003 	/* Don't modify stripping options if a port VLAN is active */
3004 	if (vsi->info.pvid)
3005 		return;
3006 
3007 	if ((vsi->info.valid_sections &
3008 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3009 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3010 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3011 		return;  /* already disabled */
3012 
3013 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3014 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3015 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3016 
3017 	ctxt.seid = vsi->seid;
3018 	ctxt.info = vsi->info;
3019 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3020 	if (ret) {
3021 		dev_info(&vsi->back->pdev->dev,
3022 			 "update vlan stripping failed, err %s aq_err %s\n",
3023 			 i40e_stat_str(&vsi->back->hw, ret),
3024 			 i40e_aq_str(&vsi->back->hw,
3025 				     vsi->back->hw.aq.asq_last_status));
3026 	}
3027 }
3028 
3029 /**
3030  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3031  * @vsi: the vsi being configured
3032  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3033  *
3034  * This is a helper function for adding a new MAC/VLAN filter with the
3035  * specified VLAN for each existing MAC address already in the hash table.
3036  * This function does *not* perform any accounting to update filters based on
3037  * VLAN mode.
3038  *
3039  * NOTE: this function expects to be called while under the
3040  * mac_filter_hash_lock
3041  **/
3042 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3043 {
3044 	struct i40e_mac_filter *f, *add_f;
3045 	struct hlist_node *h;
3046 	int bkt;
3047 
3048 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3049 		/* If we're asked to add a filter that has been marked for
3050 		 * removal, it is safe to simply restore it to active state.
3051 		 * __i40e_del_filter will have simply deleted any filters which
3052 		 * were previously marked NEW or FAILED, so if it is currently
3053 		 * marked REMOVE it must have previously been ACTIVE. Since we
3054 		 * haven't yet run the sync filters task, just restore this
3055 		 * filter to the ACTIVE state so that the sync task leaves it
3056 		 * in place.
3057 		 */
3058 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3059 			f->state = I40E_FILTER_ACTIVE;
3060 			continue;
3061 		} else if (f->state == I40E_FILTER_REMOVE) {
3062 			continue;
3063 		}
3064 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3065 		if (!add_f) {
3066 			dev_info(&vsi->back->pdev->dev,
3067 				 "Could not add vlan filter %d for %pM\n",
3068 				 vid, f->macaddr);
3069 			return -ENOMEM;
3070 		}
3071 	}
3072 
3073 	return 0;
3074 }
3075 
3076 /**
3077  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3078  * @vsi: the VSI being configured
3079  * @vid: VLAN id to be added
3080  **/
3081 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3082 {
3083 	int err;
3084 
3085 	if (vsi->info.pvid)
3086 		return -EINVAL;
3087 
3088 	/* The network stack will attempt to add VID=0, with the intention to
3089 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3090 	 * these packets by default when configured to receive untagged
3091 	 * packets, so we don't need to add a filter for this case.
3092 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3093 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3094 	 * Thus, we do not want to actually add a filter for VID=0
3095 	 */
3096 	if (!vid)
3097 		return 0;
3098 
3099 	/* Locked once because all functions invoked below iterates list*/
3100 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3101 	err = i40e_add_vlan_all_mac(vsi, vid);
3102 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3103 	if (err)
3104 		return err;
3105 
3106 	/* schedule our worker thread which will take care of
3107 	 * applying the new filter changes
3108 	 */
3109 	i40e_service_event_schedule(vsi->back);
3110 	return 0;
3111 }
3112 
3113 /**
3114  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3115  * @vsi: the vsi being configured
3116  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3117  *
3118  * This function should be used to remove all VLAN filters which match the
3119  * given VID. It does not schedule the service event and does not take the
3120  * mac_filter_hash_lock so it may be combined with other operations under
3121  * a single invocation of the mac_filter_hash_lock.
3122  *
3123  * NOTE: this function expects to be called while under the
3124  * mac_filter_hash_lock
3125  */
3126 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3127 {
3128 	struct i40e_mac_filter *f;
3129 	struct hlist_node *h;
3130 	int bkt;
3131 
3132 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3133 		if (f->vlan == vid)
3134 			__i40e_del_filter(vsi, f);
3135 	}
3136 }
3137 
3138 /**
3139  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3140  * @vsi: the VSI being configured
3141  * @vid: VLAN id to be removed
3142  **/
3143 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3144 {
3145 	if (!vid || vsi->info.pvid)
3146 		return;
3147 
3148 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3149 	i40e_rm_vlan_all_mac(vsi, vid);
3150 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3151 
3152 	/* schedule our worker thread which will take care of
3153 	 * applying the new filter changes
3154 	 */
3155 	i40e_service_event_schedule(vsi->back);
3156 }
3157 
3158 /**
3159  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3160  * @netdev: network interface to be adjusted
3161  * @proto: unused protocol value
3162  * @vid: vlan id to be added
3163  *
3164  * net_device_ops implementation for adding vlan ids
3165  **/
3166 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3167 				__always_unused __be16 proto, u16 vid)
3168 {
3169 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3170 	struct i40e_vsi *vsi = np->vsi;
3171 	int ret = 0;
3172 
3173 	if (vid >= VLAN_N_VID)
3174 		return -EINVAL;
3175 
3176 	ret = i40e_vsi_add_vlan(vsi, vid);
3177 	if (!ret)
3178 		set_bit(vid, vsi->active_vlans);
3179 
3180 	return ret;
3181 }
3182 
3183 /**
3184  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3185  * @netdev: network interface to be adjusted
3186  * @proto: unused protocol value
3187  * @vid: vlan id to be added
3188  **/
3189 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3190 				    __always_unused __be16 proto, u16 vid)
3191 {
3192 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3193 	struct i40e_vsi *vsi = np->vsi;
3194 
3195 	if (vid >= VLAN_N_VID)
3196 		return;
3197 	set_bit(vid, vsi->active_vlans);
3198 }
3199 
3200 /**
3201  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3202  * @netdev: network interface to be adjusted
3203  * @proto: unused protocol value
3204  * @vid: vlan id to be removed
3205  *
3206  * net_device_ops implementation for removing vlan ids
3207  **/
3208 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3209 				 __always_unused __be16 proto, u16 vid)
3210 {
3211 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3212 	struct i40e_vsi *vsi = np->vsi;
3213 
3214 	/* return code is ignored as there is nothing a user
3215 	 * can do about failure to remove and a log message was
3216 	 * already printed from the other function
3217 	 */
3218 	i40e_vsi_kill_vlan(vsi, vid);
3219 
3220 	clear_bit(vid, vsi->active_vlans);
3221 
3222 	return 0;
3223 }
3224 
3225 /**
3226  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3227  * @vsi: the vsi being brought back up
3228  **/
3229 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3230 {
3231 	u16 vid;
3232 
3233 	if (!vsi->netdev)
3234 		return;
3235 
3236 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3237 		i40e_vlan_stripping_enable(vsi);
3238 	else
3239 		i40e_vlan_stripping_disable(vsi);
3240 
3241 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3242 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3243 					vid);
3244 }
3245 
3246 /**
3247  * i40e_vsi_add_pvid - Add pvid for the VSI
3248  * @vsi: the vsi being adjusted
3249  * @vid: the vlan id to set as a PVID
3250  **/
3251 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3252 {
3253 	struct i40e_vsi_context ctxt;
3254 	i40e_status ret;
3255 
3256 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3257 	vsi->info.pvid = cpu_to_le16(vid);
3258 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3259 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3260 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3261 
3262 	ctxt.seid = vsi->seid;
3263 	ctxt.info = vsi->info;
3264 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3265 	if (ret) {
3266 		dev_info(&vsi->back->pdev->dev,
3267 			 "add pvid failed, err %s aq_err %s\n",
3268 			 i40e_stat_str(&vsi->back->hw, ret),
3269 			 i40e_aq_str(&vsi->back->hw,
3270 				     vsi->back->hw.aq.asq_last_status));
3271 		return -ENOENT;
3272 	}
3273 
3274 	return 0;
3275 }
3276 
3277 /**
3278  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3279  * @vsi: the vsi being adjusted
3280  *
3281  * Just use the vlan_rx_register() service to put it back to normal
3282  **/
3283 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3284 {
3285 	vsi->info.pvid = 0;
3286 
3287 	i40e_vlan_stripping_disable(vsi);
3288 }
3289 
3290 /**
3291  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3292  * @vsi: ptr to the VSI
3293  *
3294  * If this function returns with an error, then it's possible one or
3295  * more of the rings is populated (while the rest are not).  It is the
3296  * callers duty to clean those orphaned rings.
3297  *
3298  * Return 0 on success, negative on failure
3299  **/
3300 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3301 {
3302 	int i, err = 0;
3303 
3304 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3305 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3306 
3307 	if (!i40e_enabled_xdp_vsi(vsi))
3308 		return err;
3309 
3310 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3311 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3312 
3313 	return err;
3314 }
3315 
3316 /**
3317  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3318  * @vsi: ptr to the VSI
3319  *
3320  * Free VSI's transmit software resources
3321  **/
3322 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3323 {
3324 	int i;
3325 
3326 	if (vsi->tx_rings) {
3327 		for (i = 0; i < vsi->num_queue_pairs; i++)
3328 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3329 				i40e_free_tx_resources(vsi->tx_rings[i]);
3330 	}
3331 
3332 	if (vsi->xdp_rings) {
3333 		for (i = 0; i < vsi->num_queue_pairs; i++)
3334 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3335 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3336 	}
3337 }
3338 
3339 /**
3340  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3341  * @vsi: ptr to the VSI
3342  *
3343  * If this function returns with an error, then it's possible one or
3344  * more of the rings is populated (while the rest are not).  It is the
3345  * callers duty to clean those orphaned rings.
3346  *
3347  * Return 0 on success, negative on failure
3348  **/
3349 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3350 {
3351 	int i, err = 0;
3352 
3353 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3354 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3355 	return err;
3356 }
3357 
3358 /**
3359  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3360  * @vsi: ptr to the VSI
3361  *
3362  * Free all receive software resources
3363  **/
3364 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3365 {
3366 	int i;
3367 
3368 	if (!vsi->rx_rings)
3369 		return;
3370 
3371 	for (i = 0; i < vsi->num_queue_pairs; i++)
3372 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3373 			i40e_free_rx_resources(vsi->rx_rings[i]);
3374 }
3375 
3376 /**
3377  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3378  * @ring: The Tx ring to configure
3379  *
3380  * This enables/disables XPS for a given Tx descriptor ring
3381  * based on the TCs enabled for the VSI that ring belongs to.
3382  **/
3383 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3384 {
3385 	int cpu;
3386 
3387 	if (!ring->q_vector || !ring->netdev || ring->ch)
3388 		return;
3389 
3390 	/* We only initialize XPS once, so as not to overwrite user settings */
3391 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3392 		return;
3393 
3394 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3395 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3396 			    ring->queue_index);
3397 }
3398 
3399 /**
3400  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3401  * @ring: The Tx or Rx ring
3402  *
3403  * Returns the AF_XDP buffer pool or NULL.
3404  **/
3405 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3406 {
3407 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3408 	int qid = ring->queue_index;
3409 
3410 	if (ring_is_xdp(ring))
3411 		qid -= ring->vsi->alloc_queue_pairs;
3412 
3413 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3414 		return NULL;
3415 
3416 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3417 }
3418 
3419 /**
3420  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3421  * @ring: The Tx ring to configure
3422  *
3423  * Configure the Tx descriptor ring in the HMC context.
3424  **/
3425 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3426 {
3427 	struct i40e_vsi *vsi = ring->vsi;
3428 	u16 pf_q = vsi->base_queue + ring->queue_index;
3429 	struct i40e_hw *hw = &vsi->back->hw;
3430 	struct i40e_hmc_obj_txq tx_ctx;
3431 	i40e_status err = 0;
3432 	u32 qtx_ctl = 0;
3433 
3434 	if (ring_is_xdp(ring))
3435 		ring->xsk_pool = i40e_xsk_pool(ring);
3436 
3437 	/* some ATR related tx ring init */
3438 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3439 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3440 		ring->atr_count = 0;
3441 	} else {
3442 		ring->atr_sample_rate = 0;
3443 	}
3444 
3445 	/* configure XPS */
3446 	i40e_config_xps_tx_ring(ring);
3447 
3448 	/* clear the context structure first */
3449 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3450 
3451 	tx_ctx.new_context = 1;
3452 	tx_ctx.base = (ring->dma / 128);
3453 	tx_ctx.qlen = ring->count;
3454 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3455 					       I40E_FLAG_FD_ATR_ENABLED));
3456 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3457 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3458 	if (vsi->type != I40E_VSI_FDIR)
3459 		tx_ctx.head_wb_ena = 1;
3460 	tx_ctx.head_wb_addr = ring->dma +
3461 			      (ring->count * sizeof(struct i40e_tx_desc));
3462 
3463 	/* As part of VSI creation/update, FW allocates certain
3464 	 * Tx arbitration queue sets for each TC enabled for
3465 	 * the VSI. The FW returns the handles to these queue
3466 	 * sets as part of the response buffer to Add VSI,
3467 	 * Update VSI, etc. AQ commands. It is expected that
3468 	 * these queue set handles be associated with the Tx
3469 	 * queues by the driver as part of the TX queue context
3470 	 * initialization. This has to be done regardless of
3471 	 * DCB as by default everything is mapped to TC0.
3472 	 */
3473 
3474 	if (ring->ch)
3475 		tx_ctx.rdylist =
3476 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3477 
3478 	else
3479 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3480 
3481 	tx_ctx.rdylist_act = 0;
3482 
3483 	/* clear the context in the HMC */
3484 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3485 	if (err) {
3486 		dev_info(&vsi->back->pdev->dev,
3487 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3488 			 ring->queue_index, pf_q, err);
3489 		return -ENOMEM;
3490 	}
3491 
3492 	/* set the context in the HMC */
3493 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3494 	if (err) {
3495 		dev_info(&vsi->back->pdev->dev,
3496 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3497 			 ring->queue_index, pf_q, err);
3498 		return -ENOMEM;
3499 	}
3500 
3501 	/* Now associate this queue with this PCI function */
3502 	if (ring->ch) {
3503 		if (ring->ch->type == I40E_VSI_VMDQ2)
3504 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3505 		else
3506 			return -EINVAL;
3507 
3508 		qtx_ctl |= (ring->ch->vsi_number <<
3509 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3510 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3511 	} else {
3512 		if (vsi->type == I40E_VSI_VMDQ2) {
3513 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3514 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3515 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3516 		} else {
3517 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3518 		}
3519 	}
3520 
3521 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3522 		    I40E_QTX_CTL_PF_INDX_MASK);
3523 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3524 	i40e_flush(hw);
3525 
3526 	/* cache tail off for easier writes later */
3527 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3528 
3529 	return 0;
3530 }
3531 
3532 /**
3533  * i40e_rx_offset - Return expected offset into page to access data
3534  * @rx_ring: Ring we are requesting offset of
3535  *
3536  * Returns the offset value for ring into the data buffer.
3537  */
3538 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3539 {
3540 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3541 }
3542 
3543 /**
3544  * i40e_configure_rx_ring - Configure a receive ring context
3545  * @ring: The Rx ring to configure
3546  *
3547  * Configure the Rx descriptor ring in the HMC context.
3548  **/
3549 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3550 {
3551 	struct i40e_vsi *vsi = ring->vsi;
3552 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3553 	u16 pf_q = vsi->base_queue + ring->queue_index;
3554 	struct i40e_hw *hw = &vsi->back->hw;
3555 	struct i40e_hmc_obj_rxq rx_ctx;
3556 	i40e_status err = 0;
3557 	bool ok;
3558 	int ret;
3559 
3560 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3561 
3562 	/* clear the context structure first */
3563 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3564 
3565 	if (ring->vsi->type == I40E_VSI_MAIN)
3566 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3567 
3568 	kfree(ring->rx_bi);
3569 	ring->xsk_pool = i40e_xsk_pool(ring);
3570 	if (ring->xsk_pool) {
3571 		ret = i40e_alloc_rx_bi_zc(ring);
3572 		if (ret)
3573 			return ret;
3574 		ring->rx_buf_len =
3575 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3576 		/* For AF_XDP ZC, we disallow packets to span on
3577 		 * multiple buffers, thus letting us skip that
3578 		 * handling in the fast-path.
3579 		 */
3580 		chain_len = 1;
3581 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3582 						 MEM_TYPE_XSK_BUFF_POOL,
3583 						 NULL);
3584 		if (ret)
3585 			return ret;
3586 		dev_info(&vsi->back->pdev->dev,
3587 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3588 			 ring->queue_index);
3589 
3590 	} else {
3591 		ret = i40e_alloc_rx_bi(ring);
3592 		if (ret)
3593 			return ret;
3594 		ring->rx_buf_len = vsi->rx_buf_len;
3595 		if (ring->vsi->type == I40E_VSI_MAIN) {
3596 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3597 							 MEM_TYPE_PAGE_SHARED,
3598 							 NULL);
3599 			if (ret)
3600 				return ret;
3601 		}
3602 	}
3603 
3604 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3605 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3606 
3607 	rx_ctx.base = (ring->dma / 128);
3608 	rx_ctx.qlen = ring->count;
3609 
3610 	/* use 16 byte descriptors */
3611 	rx_ctx.dsize = 0;
3612 
3613 	/* descriptor type is always zero
3614 	 * rx_ctx.dtype = 0;
3615 	 */
3616 	rx_ctx.hsplit_0 = 0;
3617 
3618 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3619 	if (hw->revision_id == 0)
3620 		rx_ctx.lrxqthresh = 0;
3621 	else
3622 		rx_ctx.lrxqthresh = 1;
3623 	rx_ctx.crcstrip = 1;
3624 	rx_ctx.l2tsel = 1;
3625 	/* this controls whether VLAN is stripped from inner headers */
3626 	rx_ctx.showiv = 0;
3627 	/* set the prefena field to 1 because the manual says to */
3628 	rx_ctx.prefena = 1;
3629 
3630 	/* clear the context in the HMC */
3631 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3632 	if (err) {
3633 		dev_info(&vsi->back->pdev->dev,
3634 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3635 			 ring->queue_index, pf_q, err);
3636 		return -ENOMEM;
3637 	}
3638 
3639 	/* set the context in the HMC */
3640 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3641 	if (err) {
3642 		dev_info(&vsi->back->pdev->dev,
3643 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3644 			 ring->queue_index, pf_q, err);
3645 		return -ENOMEM;
3646 	}
3647 
3648 	/* configure Rx buffer alignment */
3649 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3650 		clear_ring_build_skb_enabled(ring);
3651 	else
3652 		set_ring_build_skb_enabled(ring);
3653 
3654 	ring->rx_offset = i40e_rx_offset(ring);
3655 
3656 	/* cache tail for quicker writes, and clear the reg before use */
3657 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3658 	writel(0, ring->tail);
3659 
3660 	if (ring->xsk_pool) {
3661 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3662 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3663 	} else {
3664 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3665 	}
3666 	if (!ok) {
3667 		/* Log this in case the user has forgotten to give the kernel
3668 		 * any buffers, even later in the application.
3669 		 */
3670 		dev_info(&vsi->back->pdev->dev,
3671 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3672 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3673 			 ring->queue_index, pf_q);
3674 	}
3675 
3676 	return 0;
3677 }
3678 
3679 /**
3680  * i40e_vsi_configure_tx - Configure the VSI for Tx
3681  * @vsi: VSI structure describing this set of rings and resources
3682  *
3683  * Configure the Tx VSI for operation.
3684  **/
3685 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3686 {
3687 	int err = 0;
3688 	u16 i;
3689 
3690 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3691 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3692 
3693 	if (err || !i40e_enabled_xdp_vsi(vsi))
3694 		return err;
3695 
3696 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3697 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3698 
3699 	return err;
3700 }
3701 
3702 /**
3703  * i40e_vsi_configure_rx - Configure the VSI for Rx
3704  * @vsi: the VSI being configured
3705  *
3706  * Configure the Rx VSI for operation.
3707  **/
3708 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3709 {
3710 	int err = 0;
3711 	u16 i;
3712 
3713 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3714 		vsi->max_frame = I40E_MAX_RXBUFFER;
3715 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3716 #if (PAGE_SIZE < 8192)
3717 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3718 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3719 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3720 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3721 #endif
3722 	} else {
3723 		vsi->max_frame = I40E_MAX_RXBUFFER;
3724 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3725 						       I40E_RXBUFFER_2048;
3726 	}
3727 
3728 	/* set up individual rings */
3729 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3730 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3731 
3732 	return err;
3733 }
3734 
3735 /**
3736  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3737  * @vsi: ptr to the VSI
3738  **/
3739 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3740 {
3741 	struct i40e_ring *tx_ring, *rx_ring;
3742 	u16 qoffset, qcount;
3743 	int i, n;
3744 
3745 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3746 		/* Reset the TC information */
3747 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3748 			rx_ring = vsi->rx_rings[i];
3749 			tx_ring = vsi->tx_rings[i];
3750 			rx_ring->dcb_tc = 0;
3751 			tx_ring->dcb_tc = 0;
3752 		}
3753 		return;
3754 	}
3755 
3756 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3757 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3758 			continue;
3759 
3760 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3761 		qcount = vsi->tc_config.tc_info[n].qcount;
3762 		for (i = qoffset; i < (qoffset + qcount); i++) {
3763 			rx_ring = vsi->rx_rings[i];
3764 			tx_ring = vsi->tx_rings[i];
3765 			rx_ring->dcb_tc = n;
3766 			tx_ring->dcb_tc = n;
3767 		}
3768 	}
3769 }
3770 
3771 /**
3772  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3773  * @vsi: ptr to the VSI
3774  **/
3775 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3776 {
3777 	if (vsi->netdev)
3778 		i40e_set_rx_mode(vsi->netdev);
3779 }
3780 
3781 /**
3782  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3783  * @pf: Pointer to the targeted PF
3784  *
3785  * Set all flow director counters to 0.
3786  */
3787 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3788 {
3789 	pf->fd_tcp4_filter_cnt = 0;
3790 	pf->fd_udp4_filter_cnt = 0;
3791 	pf->fd_sctp4_filter_cnt = 0;
3792 	pf->fd_ip4_filter_cnt = 0;
3793 	pf->fd_tcp6_filter_cnt = 0;
3794 	pf->fd_udp6_filter_cnt = 0;
3795 	pf->fd_sctp6_filter_cnt = 0;
3796 	pf->fd_ip6_filter_cnt = 0;
3797 }
3798 
3799 /**
3800  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3801  * @vsi: Pointer to the targeted VSI
3802  *
3803  * This function replays the hlist on the hw where all the SB Flow Director
3804  * filters were saved.
3805  **/
3806 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3807 {
3808 	struct i40e_fdir_filter *filter;
3809 	struct i40e_pf *pf = vsi->back;
3810 	struct hlist_node *node;
3811 
3812 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3813 		return;
3814 
3815 	/* Reset FDir counters as we're replaying all existing filters */
3816 	i40e_reset_fdir_filter_cnt(pf);
3817 
3818 	hlist_for_each_entry_safe(filter, node,
3819 				  &pf->fdir_filter_list, fdir_node) {
3820 		i40e_add_del_fdir(vsi, filter, true);
3821 	}
3822 }
3823 
3824 /**
3825  * i40e_vsi_configure - Set up the VSI for action
3826  * @vsi: the VSI being configured
3827  **/
3828 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3829 {
3830 	int err;
3831 
3832 	i40e_set_vsi_rx_mode(vsi);
3833 	i40e_restore_vlan(vsi);
3834 	i40e_vsi_config_dcb_rings(vsi);
3835 	err = i40e_vsi_configure_tx(vsi);
3836 	if (!err)
3837 		err = i40e_vsi_configure_rx(vsi);
3838 
3839 	return err;
3840 }
3841 
3842 /**
3843  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3844  * @vsi: the VSI being configured
3845  **/
3846 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3847 {
3848 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3849 	struct i40e_pf *pf = vsi->back;
3850 	struct i40e_hw *hw = &pf->hw;
3851 	u16 vector;
3852 	int i, q;
3853 	u32 qp;
3854 
3855 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3856 	 * and PFINT_LNKLSTn registers, e.g.:
3857 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3858 	 */
3859 	qp = vsi->base_queue;
3860 	vector = vsi->base_vector;
3861 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3862 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3863 
3864 		q_vector->rx.next_update = jiffies + 1;
3865 		q_vector->rx.target_itr =
3866 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3867 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3868 		     q_vector->rx.target_itr >> 1);
3869 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3870 
3871 		q_vector->tx.next_update = jiffies + 1;
3872 		q_vector->tx.target_itr =
3873 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3874 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3875 		     q_vector->tx.target_itr >> 1);
3876 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3877 
3878 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3879 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3880 
3881 		/* Linked list for the queuepairs assigned to this vector */
3882 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3883 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3884 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3885 			u32 val;
3886 
3887 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3888 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3889 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3890 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3891 			      (I40E_QUEUE_TYPE_TX <<
3892 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3893 
3894 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3895 
3896 			if (has_xdp) {
3897 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3898 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3899 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3900 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3901 				      (I40E_QUEUE_TYPE_TX <<
3902 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3903 
3904 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3905 			}
3906 
3907 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3908 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3909 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3910 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3911 			      (I40E_QUEUE_TYPE_RX <<
3912 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3913 
3914 			/* Terminate the linked list */
3915 			if (q == (q_vector->num_ringpairs - 1))
3916 				val |= (I40E_QUEUE_END_OF_LIST <<
3917 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3918 
3919 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3920 			qp++;
3921 		}
3922 	}
3923 
3924 	i40e_flush(hw);
3925 }
3926 
3927 /**
3928  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3929  * @pf: pointer to private device data structure
3930  **/
3931 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3932 {
3933 	struct i40e_hw *hw = &pf->hw;
3934 	u32 val;
3935 
3936 	/* clear things first */
3937 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3938 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3939 
3940 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3941 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3942 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3943 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3944 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3945 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3946 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3947 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3948 
3949 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3950 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3951 
3952 	if (pf->flags & I40E_FLAG_PTP)
3953 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3954 
3955 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3956 
3957 	/* SW_ITR_IDX = 0, but don't change INTENA */
3958 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3959 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3960 
3961 	/* OTHER_ITR_IDX = 0 */
3962 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3963 }
3964 
3965 /**
3966  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3967  * @vsi: the VSI being configured
3968  **/
3969 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3970 {
3971 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3972 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3973 	struct i40e_pf *pf = vsi->back;
3974 	struct i40e_hw *hw = &pf->hw;
3975 	u32 val;
3976 
3977 	/* set the ITR configuration */
3978 	q_vector->rx.next_update = jiffies + 1;
3979 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3980 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3981 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3982 	q_vector->tx.next_update = jiffies + 1;
3983 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3984 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3985 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3986 
3987 	i40e_enable_misc_int_causes(pf);
3988 
3989 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3990 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3991 
3992 	/* Associate the queue pair to the vector and enable the queue int */
3993 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3994 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3995 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3996 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3997 
3998 	wr32(hw, I40E_QINT_RQCTL(0), val);
3999 
4000 	if (i40e_enabled_xdp_vsi(vsi)) {
4001 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
4002 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
4003 		      (I40E_QUEUE_TYPE_TX
4004 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
4005 
4006 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
4007 	}
4008 
4009 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
4010 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
4011 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
4012 
4013 	wr32(hw, I40E_QINT_TQCTL(0), val);
4014 	i40e_flush(hw);
4015 }
4016 
4017 /**
4018  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4019  * @pf: board private structure
4020  **/
4021 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4022 {
4023 	struct i40e_hw *hw = &pf->hw;
4024 
4025 	wr32(hw, I40E_PFINT_DYN_CTL0,
4026 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4027 	i40e_flush(hw);
4028 }
4029 
4030 /**
4031  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4032  * @pf: board private structure
4033  **/
4034 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4035 {
4036 	struct i40e_hw *hw = &pf->hw;
4037 	u32 val;
4038 
4039 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4040 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4041 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4042 
4043 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4044 	i40e_flush(hw);
4045 }
4046 
4047 /**
4048  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4049  * @irq: interrupt number
4050  * @data: pointer to a q_vector
4051  **/
4052 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4053 {
4054 	struct i40e_q_vector *q_vector = data;
4055 
4056 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4057 		return IRQ_HANDLED;
4058 
4059 	napi_schedule_irqoff(&q_vector->napi);
4060 
4061 	return IRQ_HANDLED;
4062 }
4063 
4064 /**
4065  * i40e_irq_affinity_notify - Callback for affinity changes
4066  * @notify: context as to what irq was changed
4067  * @mask: the new affinity mask
4068  *
4069  * This is a callback function used by the irq_set_affinity_notifier function
4070  * so that we may register to receive changes to the irq affinity masks.
4071  **/
4072 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4073 				     const cpumask_t *mask)
4074 {
4075 	struct i40e_q_vector *q_vector =
4076 		container_of(notify, struct i40e_q_vector, affinity_notify);
4077 
4078 	cpumask_copy(&q_vector->affinity_mask, mask);
4079 }
4080 
4081 /**
4082  * i40e_irq_affinity_release - Callback for affinity notifier release
4083  * @ref: internal core kernel usage
4084  *
4085  * This is a callback function used by the irq_set_affinity_notifier function
4086  * to inform the current notification subscriber that they will no longer
4087  * receive notifications.
4088  **/
4089 static void i40e_irq_affinity_release(struct kref *ref) {}
4090 
4091 /**
4092  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4093  * @vsi: the VSI being configured
4094  * @basename: name for the vector
4095  *
4096  * Allocates MSI-X vectors and requests interrupts from the kernel.
4097  **/
4098 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4099 {
4100 	int q_vectors = vsi->num_q_vectors;
4101 	struct i40e_pf *pf = vsi->back;
4102 	int base = vsi->base_vector;
4103 	int rx_int_idx = 0;
4104 	int tx_int_idx = 0;
4105 	int vector, err;
4106 	int irq_num;
4107 	int cpu;
4108 
4109 	for (vector = 0; vector < q_vectors; vector++) {
4110 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4111 
4112 		irq_num = pf->msix_entries[base + vector].vector;
4113 
4114 		if (q_vector->tx.ring && q_vector->rx.ring) {
4115 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4116 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4117 			tx_int_idx++;
4118 		} else if (q_vector->rx.ring) {
4119 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4120 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4121 		} else if (q_vector->tx.ring) {
4122 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4123 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4124 		} else {
4125 			/* skip this unused q_vector */
4126 			continue;
4127 		}
4128 		err = request_irq(irq_num,
4129 				  vsi->irq_handler,
4130 				  0,
4131 				  q_vector->name,
4132 				  q_vector);
4133 		if (err) {
4134 			dev_info(&pf->pdev->dev,
4135 				 "MSIX request_irq failed, error: %d\n", err);
4136 			goto free_queue_irqs;
4137 		}
4138 
4139 		/* register for affinity change notifications */
4140 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4141 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4142 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4143 		/* Spread affinity hints out across online CPUs.
4144 		 *
4145 		 * get_cpu_mask returns a static constant mask with
4146 		 * a permanent lifetime so it's ok to pass to
4147 		 * irq_update_affinity_hint without making a copy.
4148 		 */
4149 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4150 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4151 	}
4152 
4153 	vsi->irqs_ready = true;
4154 	return 0;
4155 
4156 free_queue_irqs:
4157 	while (vector) {
4158 		vector--;
4159 		irq_num = pf->msix_entries[base + vector].vector;
4160 		irq_set_affinity_notifier(irq_num, NULL);
4161 		irq_update_affinity_hint(irq_num, NULL);
4162 		free_irq(irq_num, &vsi->q_vectors[vector]);
4163 	}
4164 	return err;
4165 }
4166 
4167 /**
4168  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4169  * @vsi: the VSI being un-configured
4170  **/
4171 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4172 {
4173 	struct i40e_pf *pf = vsi->back;
4174 	struct i40e_hw *hw = &pf->hw;
4175 	int base = vsi->base_vector;
4176 	int i;
4177 
4178 	/* disable interrupt causation from each queue */
4179 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4180 		u32 val;
4181 
4182 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4183 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4184 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4185 
4186 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4187 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4188 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4189 
4190 		if (!i40e_enabled_xdp_vsi(vsi))
4191 			continue;
4192 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4193 	}
4194 
4195 	/* disable each interrupt */
4196 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4197 		for (i = vsi->base_vector;
4198 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4199 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4200 
4201 		i40e_flush(hw);
4202 		for (i = 0; i < vsi->num_q_vectors; i++)
4203 			synchronize_irq(pf->msix_entries[i + base].vector);
4204 	} else {
4205 		/* Legacy and MSI mode - this stops all interrupt handling */
4206 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4207 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4208 		i40e_flush(hw);
4209 		synchronize_irq(pf->pdev->irq);
4210 	}
4211 }
4212 
4213 /**
4214  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4215  * @vsi: the VSI being configured
4216  **/
4217 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4218 {
4219 	struct i40e_pf *pf = vsi->back;
4220 	int i;
4221 
4222 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4223 		for (i = 0; i < vsi->num_q_vectors; i++)
4224 			i40e_irq_dynamic_enable(vsi, i);
4225 	} else {
4226 		i40e_irq_dynamic_enable_icr0(pf);
4227 	}
4228 
4229 	i40e_flush(&pf->hw);
4230 	return 0;
4231 }
4232 
4233 /**
4234  * i40e_free_misc_vector - Free the vector that handles non-queue events
4235  * @pf: board private structure
4236  **/
4237 static void i40e_free_misc_vector(struct i40e_pf *pf)
4238 {
4239 	/* Disable ICR 0 */
4240 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4241 	i40e_flush(&pf->hw);
4242 
4243 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4244 		free_irq(pf->msix_entries[0].vector, pf);
4245 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4246 	}
4247 }
4248 
4249 /**
4250  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4251  * @irq: interrupt number
4252  * @data: pointer to a q_vector
4253  *
4254  * This is the handler used for all MSI/Legacy interrupts, and deals
4255  * with both queue and non-queue interrupts.  This is also used in
4256  * MSIX mode to handle the non-queue interrupts.
4257  **/
4258 static irqreturn_t i40e_intr(int irq, void *data)
4259 {
4260 	struct i40e_pf *pf = (struct i40e_pf *)data;
4261 	struct i40e_hw *hw = &pf->hw;
4262 	irqreturn_t ret = IRQ_NONE;
4263 	u32 icr0, icr0_remaining;
4264 	u32 val, ena_mask;
4265 
4266 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4267 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4268 
4269 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4270 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4271 		goto enable_intr;
4272 
4273 	/* if interrupt but no bits showing, must be SWINT */
4274 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4275 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4276 		pf->sw_int_count++;
4277 
4278 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4279 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4280 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4281 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4282 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4283 	}
4284 
4285 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4286 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4287 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4288 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4289 
4290 		/* We do not have a way to disarm Queue causes while leaving
4291 		 * interrupt enabled for all other causes, ideally
4292 		 * interrupt should be disabled while we are in NAPI but
4293 		 * this is not a performance path and napi_schedule()
4294 		 * can deal with rescheduling.
4295 		 */
4296 		if (!test_bit(__I40E_DOWN, pf->state))
4297 			napi_schedule_irqoff(&q_vector->napi);
4298 	}
4299 
4300 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4301 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4302 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4303 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4304 	}
4305 
4306 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4307 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4308 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4309 	}
4310 
4311 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4312 		/* disable any further VFLR event notifications */
4313 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4314 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4315 
4316 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4317 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4318 		} else {
4319 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4320 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4321 		}
4322 	}
4323 
4324 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4325 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4326 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4327 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4328 		val = rd32(hw, I40E_GLGEN_RSTAT);
4329 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4330 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4331 		if (val == I40E_RESET_CORER) {
4332 			pf->corer_count++;
4333 		} else if (val == I40E_RESET_GLOBR) {
4334 			pf->globr_count++;
4335 		} else if (val == I40E_RESET_EMPR) {
4336 			pf->empr_count++;
4337 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4338 		}
4339 	}
4340 
4341 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4342 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4343 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4344 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4345 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4346 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4347 	}
4348 
4349 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4350 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4351 
4352 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4353 			schedule_work(&pf->ptp_extts0_work);
4354 
4355 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4356 			i40e_ptp_tx_hwtstamp(pf);
4357 
4358 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4359 	}
4360 
4361 	/* If a critical error is pending we have no choice but to reset the
4362 	 * device.
4363 	 * Report and mask out any remaining unexpected interrupts.
4364 	 */
4365 	icr0_remaining = icr0 & ena_mask;
4366 	if (icr0_remaining) {
4367 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4368 			 icr0_remaining);
4369 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4370 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4371 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4372 			dev_info(&pf->pdev->dev, "device will be reset\n");
4373 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4374 			i40e_service_event_schedule(pf);
4375 		}
4376 		ena_mask &= ~icr0_remaining;
4377 	}
4378 	ret = IRQ_HANDLED;
4379 
4380 enable_intr:
4381 	/* re-enable interrupt causes */
4382 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4383 	if (!test_bit(__I40E_DOWN, pf->state) ||
4384 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4385 		i40e_service_event_schedule(pf);
4386 		i40e_irq_dynamic_enable_icr0(pf);
4387 	}
4388 
4389 	return ret;
4390 }
4391 
4392 /**
4393  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4394  * @tx_ring:  tx ring to clean
4395  * @budget:   how many cleans we're allowed
4396  *
4397  * Returns true if there's any budget left (e.g. the clean is finished)
4398  **/
4399 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4400 {
4401 	struct i40e_vsi *vsi = tx_ring->vsi;
4402 	u16 i = tx_ring->next_to_clean;
4403 	struct i40e_tx_buffer *tx_buf;
4404 	struct i40e_tx_desc *tx_desc;
4405 
4406 	tx_buf = &tx_ring->tx_bi[i];
4407 	tx_desc = I40E_TX_DESC(tx_ring, i);
4408 	i -= tx_ring->count;
4409 
4410 	do {
4411 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4412 
4413 		/* if next_to_watch is not set then there is no work pending */
4414 		if (!eop_desc)
4415 			break;
4416 
4417 		/* prevent any other reads prior to eop_desc */
4418 		smp_rmb();
4419 
4420 		/* if the descriptor isn't done, no work yet to do */
4421 		if (!(eop_desc->cmd_type_offset_bsz &
4422 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4423 			break;
4424 
4425 		/* clear next_to_watch to prevent false hangs */
4426 		tx_buf->next_to_watch = NULL;
4427 
4428 		tx_desc->buffer_addr = 0;
4429 		tx_desc->cmd_type_offset_bsz = 0;
4430 		/* move past filter desc */
4431 		tx_buf++;
4432 		tx_desc++;
4433 		i++;
4434 		if (unlikely(!i)) {
4435 			i -= tx_ring->count;
4436 			tx_buf = tx_ring->tx_bi;
4437 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4438 		}
4439 		/* unmap skb header data */
4440 		dma_unmap_single(tx_ring->dev,
4441 				 dma_unmap_addr(tx_buf, dma),
4442 				 dma_unmap_len(tx_buf, len),
4443 				 DMA_TO_DEVICE);
4444 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4445 			kfree(tx_buf->raw_buf);
4446 
4447 		tx_buf->raw_buf = NULL;
4448 		tx_buf->tx_flags = 0;
4449 		tx_buf->next_to_watch = NULL;
4450 		dma_unmap_len_set(tx_buf, len, 0);
4451 		tx_desc->buffer_addr = 0;
4452 		tx_desc->cmd_type_offset_bsz = 0;
4453 
4454 		/* move us past the eop_desc for start of next FD desc */
4455 		tx_buf++;
4456 		tx_desc++;
4457 		i++;
4458 		if (unlikely(!i)) {
4459 			i -= tx_ring->count;
4460 			tx_buf = tx_ring->tx_bi;
4461 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4462 		}
4463 
4464 		/* update budget accounting */
4465 		budget--;
4466 	} while (likely(budget));
4467 
4468 	i += tx_ring->count;
4469 	tx_ring->next_to_clean = i;
4470 
4471 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4472 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4473 
4474 	return budget > 0;
4475 }
4476 
4477 /**
4478  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4479  * @irq: interrupt number
4480  * @data: pointer to a q_vector
4481  **/
4482 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4483 {
4484 	struct i40e_q_vector *q_vector = data;
4485 	struct i40e_vsi *vsi;
4486 
4487 	if (!q_vector->tx.ring)
4488 		return IRQ_HANDLED;
4489 
4490 	vsi = q_vector->tx.ring->vsi;
4491 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4492 
4493 	return IRQ_HANDLED;
4494 }
4495 
4496 /**
4497  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4498  * @vsi: the VSI being configured
4499  * @v_idx: vector index
4500  * @qp_idx: queue pair index
4501  **/
4502 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4503 {
4504 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4505 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4506 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4507 
4508 	tx_ring->q_vector = q_vector;
4509 	tx_ring->next = q_vector->tx.ring;
4510 	q_vector->tx.ring = tx_ring;
4511 	q_vector->tx.count++;
4512 
4513 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4514 	if (i40e_enabled_xdp_vsi(vsi)) {
4515 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4516 
4517 		xdp_ring->q_vector = q_vector;
4518 		xdp_ring->next = q_vector->tx.ring;
4519 		q_vector->tx.ring = xdp_ring;
4520 		q_vector->tx.count++;
4521 	}
4522 
4523 	rx_ring->q_vector = q_vector;
4524 	rx_ring->next = q_vector->rx.ring;
4525 	q_vector->rx.ring = rx_ring;
4526 	q_vector->rx.count++;
4527 }
4528 
4529 /**
4530  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4531  * @vsi: the VSI being configured
4532  *
4533  * This function maps descriptor rings to the queue-specific vectors
4534  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4535  * one vector per queue pair, but on a constrained vector budget, we
4536  * group the queue pairs as "efficiently" as possible.
4537  **/
4538 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4539 {
4540 	int qp_remaining = vsi->num_queue_pairs;
4541 	int q_vectors = vsi->num_q_vectors;
4542 	int num_ringpairs;
4543 	int v_start = 0;
4544 	int qp_idx = 0;
4545 
4546 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4547 	 * group them so there are multiple queues per vector.
4548 	 * It is also important to go through all the vectors available to be
4549 	 * sure that if we don't use all the vectors, that the remaining vectors
4550 	 * are cleared. This is especially important when decreasing the
4551 	 * number of queues in use.
4552 	 */
4553 	for (; v_start < q_vectors; v_start++) {
4554 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4555 
4556 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4557 
4558 		q_vector->num_ringpairs = num_ringpairs;
4559 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4560 
4561 		q_vector->rx.count = 0;
4562 		q_vector->tx.count = 0;
4563 		q_vector->rx.ring = NULL;
4564 		q_vector->tx.ring = NULL;
4565 
4566 		while (num_ringpairs--) {
4567 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4568 			qp_idx++;
4569 			qp_remaining--;
4570 		}
4571 	}
4572 }
4573 
4574 /**
4575  * i40e_vsi_request_irq - Request IRQ from the OS
4576  * @vsi: the VSI being configured
4577  * @basename: name for the vector
4578  **/
4579 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4580 {
4581 	struct i40e_pf *pf = vsi->back;
4582 	int err;
4583 
4584 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4585 		err = i40e_vsi_request_irq_msix(vsi, basename);
4586 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4587 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4588 				  pf->int_name, pf);
4589 	else
4590 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4591 				  pf->int_name, pf);
4592 
4593 	if (err)
4594 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4595 
4596 	return err;
4597 }
4598 
4599 #ifdef CONFIG_NET_POLL_CONTROLLER
4600 /**
4601  * i40e_netpoll - A Polling 'interrupt' handler
4602  * @netdev: network interface device structure
4603  *
4604  * This is used by netconsole to send skbs without having to re-enable
4605  * interrupts.  It's not called while the normal interrupt routine is executing.
4606  **/
4607 static void i40e_netpoll(struct net_device *netdev)
4608 {
4609 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4610 	struct i40e_vsi *vsi = np->vsi;
4611 	struct i40e_pf *pf = vsi->back;
4612 	int i;
4613 
4614 	/* if interface is down do nothing */
4615 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4616 		return;
4617 
4618 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4619 		for (i = 0; i < vsi->num_q_vectors; i++)
4620 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4621 	} else {
4622 		i40e_intr(pf->pdev->irq, netdev);
4623 	}
4624 }
4625 #endif
4626 
4627 #define I40E_QTX_ENA_WAIT_COUNT 50
4628 
4629 /**
4630  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4631  * @pf: the PF being configured
4632  * @pf_q: the PF queue
4633  * @enable: enable or disable state of the queue
4634  *
4635  * This routine will wait for the given Tx queue of the PF to reach the
4636  * enabled or disabled state.
4637  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4638  * multiple retries; else will return 0 in case of success.
4639  **/
4640 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4641 {
4642 	int i;
4643 	u32 tx_reg;
4644 
4645 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4646 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4647 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4648 			break;
4649 
4650 		usleep_range(10, 20);
4651 	}
4652 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4653 		return -ETIMEDOUT;
4654 
4655 	return 0;
4656 }
4657 
4658 /**
4659  * i40e_control_tx_q - Start or stop a particular Tx queue
4660  * @pf: the PF structure
4661  * @pf_q: the PF queue to configure
4662  * @enable: start or stop the queue
4663  *
4664  * This function enables or disables a single queue. Note that any delay
4665  * required after the operation is expected to be handled by the caller of
4666  * this function.
4667  **/
4668 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4669 {
4670 	struct i40e_hw *hw = &pf->hw;
4671 	u32 tx_reg;
4672 	int i;
4673 
4674 	/* warn the TX unit of coming changes */
4675 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4676 	if (!enable)
4677 		usleep_range(10, 20);
4678 
4679 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4680 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4681 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4682 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4683 			break;
4684 		usleep_range(1000, 2000);
4685 	}
4686 
4687 	/* Skip if the queue is already in the requested state */
4688 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4689 		return;
4690 
4691 	/* turn on/off the queue */
4692 	if (enable) {
4693 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4694 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4695 	} else {
4696 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4697 	}
4698 
4699 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4700 }
4701 
4702 /**
4703  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4704  * @seid: VSI SEID
4705  * @pf: the PF structure
4706  * @pf_q: the PF queue to configure
4707  * @is_xdp: true if the queue is used for XDP
4708  * @enable: start or stop the queue
4709  **/
4710 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4711 			   bool is_xdp, bool enable)
4712 {
4713 	int ret;
4714 
4715 	i40e_control_tx_q(pf, pf_q, enable);
4716 
4717 	/* wait for the change to finish */
4718 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4719 	if (ret) {
4720 		dev_info(&pf->pdev->dev,
4721 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4722 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4723 			 (enable ? "en" : "dis"));
4724 	}
4725 
4726 	return ret;
4727 }
4728 
4729 /**
4730  * i40e_vsi_enable_tx - Start a VSI's rings
4731  * @vsi: the VSI being configured
4732  **/
4733 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4734 {
4735 	struct i40e_pf *pf = vsi->back;
4736 	int i, pf_q, ret = 0;
4737 
4738 	pf_q = vsi->base_queue;
4739 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4740 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4741 					     pf_q,
4742 					     false /*is xdp*/, true);
4743 		if (ret)
4744 			break;
4745 
4746 		if (!i40e_enabled_xdp_vsi(vsi))
4747 			continue;
4748 
4749 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4750 					     pf_q + vsi->alloc_queue_pairs,
4751 					     true /*is xdp*/, true);
4752 		if (ret)
4753 			break;
4754 	}
4755 	return ret;
4756 }
4757 
4758 /**
4759  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4760  * @pf: the PF being configured
4761  * @pf_q: the PF queue
4762  * @enable: enable or disable state of the queue
4763  *
4764  * This routine will wait for the given Rx queue of the PF to reach the
4765  * enabled or disabled state.
4766  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4767  * multiple retries; else will return 0 in case of success.
4768  **/
4769 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4770 {
4771 	int i;
4772 	u32 rx_reg;
4773 
4774 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4775 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4776 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4777 			break;
4778 
4779 		usleep_range(10, 20);
4780 	}
4781 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4782 		return -ETIMEDOUT;
4783 
4784 	return 0;
4785 }
4786 
4787 /**
4788  * i40e_control_rx_q - Start or stop a particular Rx queue
4789  * @pf: the PF structure
4790  * @pf_q: the PF queue to configure
4791  * @enable: start or stop the queue
4792  *
4793  * This function enables or disables a single queue. Note that
4794  * any delay required after the operation is expected to be
4795  * handled by the caller of this function.
4796  **/
4797 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4798 {
4799 	struct i40e_hw *hw = &pf->hw;
4800 	u32 rx_reg;
4801 	int i;
4802 
4803 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4804 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4805 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4806 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4807 			break;
4808 		usleep_range(1000, 2000);
4809 	}
4810 
4811 	/* Skip if the queue is already in the requested state */
4812 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4813 		return;
4814 
4815 	/* turn on/off the queue */
4816 	if (enable)
4817 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4818 	else
4819 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4820 
4821 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4822 }
4823 
4824 /**
4825  * i40e_control_wait_rx_q
4826  * @pf: the PF structure
4827  * @pf_q: queue being configured
4828  * @enable: start or stop the rings
4829  *
4830  * This function enables or disables a single queue along with waiting
4831  * for the change to finish. The caller of this function should handle
4832  * the delays needed in the case of disabling queues.
4833  **/
4834 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4835 {
4836 	int ret = 0;
4837 
4838 	i40e_control_rx_q(pf, pf_q, enable);
4839 
4840 	/* wait for the change to finish */
4841 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4842 	if (ret)
4843 		return ret;
4844 
4845 	return ret;
4846 }
4847 
4848 /**
4849  * i40e_vsi_enable_rx - Start a VSI's rings
4850  * @vsi: the VSI being configured
4851  **/
4852 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4853 {
4854 	struct i40e_pf *pf = vsi->back;
4855 	int i, pf_q, ret = 0;
4856 
4857 	pf_q = vsi->base_queue;
4858 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4859 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4860 		if (ret) {
4861 			dev_info(&pf->pdev->dev,
4862 				 "VSI seid %d Rx ring %d enable timeout\n",
4863 				 vsi->seid, pf_q);
4864 			break;
4865 		}
4866 	}
4867 
4868 	return ret;
4869 }
4870 
4871 /**
4872  * i40e_vsi_start_rings - Start a VSI's rings
4873  * @vsi: the VSI being configured
4874  **/
4875 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4876 {
4877 	int ret = 0;
4878 
4879 	/* do rx first for enable and last for disable */
4880 	ret = i40e_vsi_enable_rx(vsi);
4881 	if (ret)
4882 		return ret;
4883 	ret = i40e_vsi_enable_tx(vsi);
4884 
4885 	return ret;
4886 }
4887 
4888 #define I40E_DISABLE_TX_GAP_MSEC	50
4889 
4890 /**
4891  * i40e_vsi_stop_rings - Stop a VSI's rings
4892  * @vsi: the VSI being configured
4893  **/
4894 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4895 {
4896 	struct i40e_pf *pf = vsi->back;
4897 	int pf_q, err, q_end;
4898 
4899 	/* When port TX is suspended, don't wait */
4900 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4901 		return i40e_vsi_stop_rings_no_wait(vsi);
4902 
4903 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4904 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4905 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4906 
4907 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4908 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4909 		if (err)
4910 			dev_info(&pf->pdev->dev,
4911 				 "VSI seid %d Rx ring %d disable timeout\n",
4912 				 vsi->seid, pf_q);
4913 	}
4914 
4915 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4916 	pf_q = vsi->base_queue;
4917 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4918 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4919 
4920 	i40e_vsi_wait_queues_disabled(vsi);
4921 }
4922 
4923 /**
4924  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4925  * @vsi: the VSI being shutdown
4926  *
4927  * This function stops all the rings for a VSI but does not delay to verify
4928  * that rings have been disabled. It is expected that the caller is shutting
4929  * down multiple VSIs at once and will delay together for all the VSIs after
4930  * initiating the shutdown. This is particularly useful for shutting down lots
4931  * of VFs together. Otherwise, a large delay can be incurred while configuring
4932  * each VSI in serial.
4933  **/
4934 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4935 {
4936 	struct i40e_pf *pf = vsi->back;
4937 	int i, pf_q;
4938 
4939 	pf_q = vsi->base_queue;
4940 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4941 		i40e_control_tx_q(pf, pf_q, false);
4942 		i40e_control_rx_q(pf, pf_q, false);
4943 	}
4944 }
4945 
4946 /**
4947  * i40e_vsi_free_irq - Free the irq association with the OS
4948  * @vsi: the VSI being configured
4949  **/
4950 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4951 {
4952 	struct i40e_pf *pf = vsi->back;
4953 	struct i40e_hw *hw = &pf->hw;
4954 	int base = vsi->base_vector;
4955 	u32 val, qp;
4956 	int i;
4957 
4958 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4959 		if (!vsi->q_vectors)
4960 			return;
4961 
4962 		if (!vsi->irqs_ready)
4963 			return;
4964 
4965 		vsi->irqs_ready = false;
4966 		for (i = 0; i < vsi->num_q_vectors; i++) {
4967 			int irq_num;
4968 			u16 vector;
4969 
4970 			vector = i + base;
4971 			irq_num = pf->msix_entries[vector].vector;
4972 
4973 			/* free only the irqs that were actually requested */
4974 			if (!vsi->q_vectors[i] ||
4975 			    !vsi->q_vectors[i]->num_ringpairs)
4976 				continue;
4977 
4978 			/* clear the affinity notifier in the IRQ descriptor */
4979 			irq_set_affinity_notifier(irq_num, NULL);
4980 			/* remove our suggested affinity mask for this IRQ */
4981 			irq_update_affinity_hint(irq_num, NULL);
4982 			free_irq(irq_num, vsi->q_vectors[i]);
4983 
4984 			/* Tear down the interrupt queue link list
4985 			 *
4986 			 * We know that they come in pairs and always
4987 			 * the Rx first, then the Tx.  To clear the
4988 			 * link list, stick the EOL value into the
4989 			 * next_q field of the registers.
4990 			 */
4991 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4992 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4993 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4994 			val |= I40E_QUEUE_END_OF_LIST
4995 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4996 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4997 
4998 			while (qp != I40E_QUEUE_END_OF_LIST) {
4999 				u32 next;
5000 
5001 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5002 
5003 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5004 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5005 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5006 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5007 
5008 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5009 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5010 
5011 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5012 
5013 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5014 
5015 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5016 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5017 
5018 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5019 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5020 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5021 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5022 
5023 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5024 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5025 
5026 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5027 				qp = next;
5028 			}
5029 		}
5030 	} else {
5031 		free_irq(pf->pdev->irq, pf);
5032 
5033 		val = rd32(hw, I40E_PFINT_LNKLST0);
5034 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5035 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5036 		val |= I40E_QUEUE_END_OF_LIST
5037 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5038 		wr32(hw, I40E_PFINT_LNKLST0, val);
5039 
5040 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5041 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5042 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5043 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5044 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5045 
5046 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5047 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5048 
5049 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5050 
5051 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5052 
5053 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5054 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5055 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5056 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5057 
5058 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5059 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5060 
5061 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5062 	}
5063 }
5064 
5065 /**
5066  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5067  * @vsi: the VSI being configured
5068  * @v_idx: Index of vector to be freed
5069  *
5070  * This function frees the memory allocated to the q_vector.  In addition if
5071  * NAPI is enabled it will delete any references to the NAPI struct prior
5072  * to freeing the q_vector.
5073  **/
5074 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5075 {
5076 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5077 	struct i40e_ring *ring;
5078 
5079 	if (!q_vector)
5080 		return;
5081 
5082 	/* disassociate q_vector from rings */
5083 	i40e_for_each_ring(ring, q_vector->tx)
5084 		ring->q_vector = NULL;
5085 
5086 	i40e_for_each_ring(ring, q_vector->rx)
5087 		ring->q_vector = NULL;
5088 
5089 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5090 	if (vsi->netdev)
5091 		netif_napi_del(&q_vector->napi);
5092 
5093 	vsi->q_vectors[v_idx] = NULL;
5094 
5095 	kfree_rcu(q_vector, rcu);
5096 }
5097 
5098 /**
5099  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5100  * @vsi: the VSI being un-configured
5101  *
5102  * This frees the memory allocated to the q_vectors and
5103  * deletes references to the NAPI struct.
5104  **/
5105 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5106 {
5107 	int v_idx;
5108 
5109 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5110 		i40e_free_q_vector(vsi, v_idx);
5111 }
5112 
5113 /**
5114  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5115  * @pf: board private structure
5116  **/
5117 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5118 {
5119 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5120 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5121 		pci_disable_msix(pf->pdev);
5122 		kfree(pf->msix_entries);
5123 		pf->msix_entries = NULL;
5124 		kfree(pf->irq_pile);
5125 		pf->irq_pile = NULL;
5126 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5127 		pci_disable_msi(pf->pdev);
5128 	}
5129 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5130 }
5131 
5132 /**
5133  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5134  * @pf: board private structure
5135  *
5136  * We go through and clear interrupt specific resources and reset the structure
5137  * to pre-load conditions
5138  **/
5139 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5140 {
5141 	int i;
5142 
5143 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5144 		i40e_free_misc_vector(pf);
5145 
5146 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5147 		      I40E_IWARP_IRQ_PILE_ID);
5148 
5149 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5150 	for (i = 0; i < pf->num_alloc_vsi; i++)
5151 		if (pf->vsi[i])
5152 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5153 	i40e_reset_interrupt_capability(pf);
5154 }
5155 
5156 /**
5157  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5158  * @vsi: the VSI being configured
5159  **/
5160 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5161 {
5162 	int q_idx;
5163 
5164 	if (!vsi->netdev)
5165 		return;
5166 
5167 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5168 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5169 
5170 		if (q_vector->rx.ring || q_vector->tx.ring)
5171 			napi_enable(&q_vector->napi);
5172 	}
5173 }
5174 
5175 /**
5176  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5177  * @vsi: the VSI being configured
5178  **/
5179 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5180 {
5181 	int q_idx;
5182 
5183 	if (!vsi->netdev)
5184 		return;
5185 
5186 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5187 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5188 
5189 		if (q_vector->rx.ring || q_vector->tx.ring)
5190 			napi_disable(&q_vector->napi);
5191 	}
5192 }
5193 
5194 /**
5195  * i40e_vsi_close - Shut down a VSI
5196  * @vsi: the vsi to be quelled
5197  **/
5198 static void i40e_vsi_close(struct i40e_vsi *vsi)
5199 {
5200 	struct i40e_pf *pf = vsi->back;
5201 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5202 		i40e_down(vsi);
5203 	i40e_vsi_free_irq(vsi);
5204 	i40e_vsi_free_tx_resources(vsi);
5205 	i40e_vsi_free_rx_resources(vsi);
5206 	vsi->current_netdev_flags = 0;
5207 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5208 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5209 		set_bit(__I40E_CLIENT_RESET, pf->state);
5210 }
5211 
5212 /**
5213  * i40e_quiesce_vsi - Pause a given VSI
5214  * @vsi: the VSI being paused
5215  **/
5216 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5217 {
5218 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5219 		return;
5220 
5221 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5222 	if (vsi->netdev && netif_running(vsi->netdev))
5223 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5224 	else
5225 		i40e_vsi_close(vsi);
5226 }
5227 
5228 /**
5229  * i40e_unquiesce_vsi - Resume a given VSI
5230  * @vsi: the VSI being resumed
5231  **/
5232 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5233 {
5234 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5235 		return;
5236 
5237 	if (vsi->netdev && netif_running(vsi->netdev))
5238 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5239 	else
5240 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5241 }
5242 
5243 /**
5244  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5245  * @pf: the PF
5246  **/
5247 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5248 {
5249 	int v;
5250 
5251 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5252 		if (pf->vsi[v])
5253 			i40e_quiesce_vsi(pf->vsi[v]);
5254 	}
5255 }
5256 
5257 /**
5258  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5259  * @pf: the PF
5260  **/
5261 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5262 {
5263 	int v;
5264 
5265 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5266 		if (pf->vsi[v])
5267 			i40e_unquiesce_vsi(pf->vsi[v]);
5268 	}
5269 }
5270 
5271 /**
5272  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5273  * @vsi: the VSI being configured
5274  *
5275  * Wait until all queues on a given VSI have been disabled.
5276  **/
5277 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5278 {
5279 	struct i40e_pf *pf = vsi->back;
5280 	int i, pf_q, ret;
5281 
5282 	pf_q = vsi->base_queue;
5283 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5284 		/* Check and wait for the Tx queue */
5285 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5286 		if (ret) {
5287 			dev_info(&pf->pdev->dev,
5288 				 "VSI seid %d Tx ring %d disable timeout\n",
5289 				 vsi->seid, pf_q);
5290 			return ret;
5291 		}
5292 
5293 		if (!i40e_enabled_xdp_vsi(vsi))
5294 			goto wait_rx;
5295 
5296 		/* Check and wait for the XDP Tx queue */
5297 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5298 				       false);
5299 		if (ret) {
5300 			dev_info(&pf->pdev->dev,
5301 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5302 				 vsi->seid, pf_q);
5303 			return ret;
5304 		}
5305 wait_rx:
5306 		/* Check and wait for the Rx queue */
5307 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5308 		if (ret) {
5309 			dev_info(&pf->pdev->dev,
5310 				 "VSI seid %d Rx ring %d disable timeout\n",
5311 				 vsi->seid, pf_q);
5312 			return ret;
5313 		}
5314 	}
5315 
5316 	return 0;
5317 }
5318 
5319 #ifdef CONFIG_I40E_DCB
5320 /**
5321  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5322  * @pf: the PF
5323  *
5324  * This function waits for the queues to be in disabled state for all the
5325  * VSIs that are managed by this PF.
5326  **/
5327 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5328 {
5329 	int v, ret = 0;
5330 
5331 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5332 		if (pf->vsi[v]) {
5333 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5334 			if (ret)
5335 				break;
5336 		}
5337 	}
5338 
5339 	return ret;
5340 }
5341 
5342 #endif
5343 
5344 /**
5345  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5346  * @pf: pointer to PF
5347  *
5348  * Get TC map for ISCSI PF type that will include iSCSI TC
5349  * and LAN TC.
5350  **/
5351 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5352 {
5353 	struct i40e_dcb_app_priority_table app;
5354 	struct i40e_hw *hw = &pf->hw;
5355 	u8 enabled_tc = 1; /* TC0 is always enabled */
5356 	u8 tc, i;
5357 	/* Get the iSCSI APP TLV */
5358 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5359 
5360 	for (i = 0; i < dcbcfg->numapps; i++) {
5361 		app = dcbcfg->app[i];
5362 		if (app.selector == I40E_APP_SEL_TCPIP &&
5363 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5364 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5365 			enabled_tc |= BIT(tc);
5366 			break;
5367 		}
5368 	}
5369 
5370 	return enabled_tc;
5371 }
5372 
5373 /**
5374  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5375  * @dcbcfg: the corresponding DCBx configuration structure
5376  *
5377  * Return the number of TCs from given DCBx configuration
5378  **/
5379 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5380 {
5381 	int i, tc_unused = 0;
5382 	u8 num_tc = 0;
5383 	u8 ret = 0;
5384 
5385 	/* Scan the ETS Config Priority Table to find
5386 	 * traffic class enabled for a given priority
5387 	 * and create a bitmask of enabled TCs
5388 	 */
5389 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5390 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5391 
5392 	/* Now scan the bitmask to check for
5393 	 * contiguous TCs starting with TC0
5394 	 */
5395 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5396 		if (num_tc & BIT(i)) {
5397 			if (!tc_unused) {
5398 				ret++;
5399 			} else {
5400 				pr_err("Non-contiguous TC - Disabling DCB\n");
5401 				return 1;
5402 			}
5403 		} else {
5404 			tc_unused = 1;
5405 		}
5406 	}
5407 
5408 	/* There is always at least TC0 */
5409 	if (!ret)
5410 		ret = 1;
5411 
5412 	return ret;
5413 }
5414 
5415 /**
5416  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5417  * @dcbcfg: the corresponding DCBx configuration structure
5418  *
5419  * Query the current DCB configuration and return the number of
5420  * traffic classes enabled from the given DCBX config
5421  **/
5422 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5423 {
5424 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5425 	u8 enabled_tc = 1;
5426 	u8 i;
5427 
5428 	for (i = 0; i < num_tc; i++)
5429 		enabled_tc |= BIT(i);
5430 
5431 	return enabled_tc;
5432 }
5433 
5434 /**
5435  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5436  * @pf: PF being queried
5437  *
5438  * Query the current MQPRIO configuration and return the number of
5439  * traffic classes enabled.
5440  **/
5441 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5442 {
5443 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5444 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5445 	u8 enabled_tc = 1, i;
5446 
5447 	for (i = 1; i < num_tc; i++)
5448 		enabled_tc |= BIT(i);
5449 	return enabled_tc;
5450 }
5451 
5452 /**
5453  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5454  * @pf: PF being queried
5455  *
5456  * Return number of traffic classes enabled for the given PF
5457  **/
5458 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5459 {
5460 	struct i40e_hw *hw = &pf->hw;
5461 	u8 i, enabled_tc = 1;
5462 	u8 num_tc = 0;
5463 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5464 
5465 	if (i40e_is_tc_mqprio_enabled(pf))
5466 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5467 
5468 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5469 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5470 		return 1;
5471 
5472 	/* SFP mode will be enabled for all TCs on port */
5473 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5474 		return i40e_dcb_get_num_tc(dcbcfg);
5475 
5476 	/* MFP mode return count of enabled TCs for this PF */
5477 	if (pf->hw.func_caps.iscsi)
5478 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5479 	else
5480 		return 1; /* Only TC0 */
5481 
5482 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5483 		if (enabled_tc & BIT(i))
5484 			num_tc++;
5485 	}
5486 	return num_tc;
5487 }
5488 
5489 /**
5490  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5491  * @pf: PF being queried
5492  *
5493  * Return a bitmap for enabled traffic classes for this PF.
5494  **/
5495 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5496 {
5497 	if (i40e_is_tc_mqprio_enabled(pf))
5498 		return i40e_mqprio_get_enabled_tc(pf);
5499 
5500 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5501 	 * default TC
5502 	 */
5503 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5504 		return I40E_DEFAULT_TRAFFIC_CLASS;
5505 
5506 	/* SFP mode we want PF to be enabled for all TCs */
5507 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5508 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5509 
5510 	/* MFP enabled and iSCSI PF type */
5511 	if (pf->hw.func_caps.iscsi)
5512 		return i40e_get_iscsi_tc_map(pf);
5513 	else
5514 		return I40E_DEFAULT_TRAFFIC_CLASS;
5515 }
5516 
5517 /**
5518  * i40e_vsi_get_bw_info - Query VSI BW Information
5519  * @vsi: the VSI being queried
5520  *
5521  * Returns 0 on success, negative value on failure
5522  **/
5523 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5524 {
5525 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5526 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5527 	struct i40e_pf *pf = vsi->back;
5528 	struct i40e_hw *hw = &pf->hw;
5529 	i40e_status ret;
5530 	u32 tc_bw_max;
5531 	int i;
5532 
5533 	/* Get the VSI level BW configuration */
5534 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5535 	if (ret) {
5536 		dev_info(&pf->pdev->dev,
5537 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5538 			 i40e_stat_str(&pf->hw, ret),
5539 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5540 		return -EINVAL;
5541 	}
5542 
5543 	/* Get the VSI level BW configuration per TC */
5544 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5545 					       NULL);
5546 	if (ret) {
5547 		dev_info(&pf->pdev->dev,
5548 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5549 			 i40e_stat_str(&pf->hw, ret),
5550 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5551 		return -EINVAL;
5552 	}
5553 
5554 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5555 		dev_info(&pf->pdev->dev,
5556 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5557 			 bw_config.tc_valid_bits,
5558 			 bw_ets_config.tc_valid_bits);
5559 		/* Still continuing */
5560 	}
5561 
5562 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5563 	vsi->bw_max_quanta = bw_config.max_bw;
5564 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5565 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5566 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5567 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5568 		vsi->bw_ets_limit_credits[i] =
5569 					le16_to_cpu(bw_ets_config.credits[i]);
5570 		/* 3 bits out of 4 for each TC */
5571 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5572 	}
5573 
5574 	return 0;
5575 }
5576 
5577 /**
5578  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5579  * @vsi: the VSI being configured
5580  * @enabled_tc: TC bitmap
5581  * @bw_share: BW shared credits per TC
5582  *
5583  * Returns 0 on success, negative value on failure
5584  **/
5585 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5586 				       u8 *bw_share)
5587 {
5588 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5589 	struct i40e_pf *pf = vsi->back;
5590 	i40e_status ret;
5591 	int i;
5592 
5593 	/* There is no need to reset BW when mqprio mode is on.  */
5594 	if (i40e_is_tc_mqprio_enabled(pf))
5595 		return 0;
5596 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5597 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5598 		if (ret)
5599 			dev_info(&pf->pdev->dev,
5600 				 "Failed to reset tx rate for vsi->seid %u\n",
5601 				 vsi->seid);
5602 		return ret;
5603 	}
5604 	memset(&bw_data, 0, sizeof(bw_data));
5605 	bw_data.tc_valid_bits = enabled_tc;
5606 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5607 		bw_data.tc_bw_credits[i] = bw_share[i];
5608 
5609 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5610 	if (ret) {
5611 		dev_info(&pf->pdev->dev,
5612 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5613 			 pf->hw.aq.asq_last_status);
5614 		return -EINVAL;
5615 	}
5616 
5617 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5618 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5619 
5620 	return 0;
5621 }
5622 
5623 /**
5624  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5625  * @vsi: the VSI being configured
5626  * @enabled_tc: TC map to be enabled
5627  *
5628  **/
5629 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5630 {
5631 	struct net_device *netdev = vsi->netdev;
5632 	struct i40e_pf *pf = vsi->back;
5633 	struct i40e_hw *hw = &pf->hw;
5634 	u8 netdev_tc = 0;
5635 	int i;
5636 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5637 
5638 	if (!netdev)
5639 		return;
5640 
5641 	if (!enabled_tc) {
5642 		netdev_reset_tc(netdev);
5643 		return;
5644 	}
5645 
5646 	/* Set up actual enabled TCs on the VSI */
5647 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5648 		return;
5649 
5650 	/* set per TC queues for the VSI */
5651 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5652 		/* Only set TC queues for enabled tcs
5653 		 *
5654 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5655 		 * enabled_tc bitmap would be 0x00001001; the driver
5656 		 * will set the numtc for netdev as 2 that will be
5657 		 * referenced by the netdev layer as TC 0 and 1.
5658 		 */
5659 		if (vsi->tc_config.enabled_tc & BIT(i))
5660 			netdev_set_tc_queue(netdev,
5661 					vsi->tc_config.tc_info[i].netdev_tc,
5662 					vsi->tc_config.tc_info[i].qcount,
5663 					vsi->tc_config.tc_info[i].qoffset);
5664 	}
5665 
5666 	if (i40e_is_tc_mqprio_enabled(pf))
5667 		return;
5668 
5669 	/* Assign UP2TC map for the VSI */
5670 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5671 		/* Get the actual TC# for the UP */
5672 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5673 		/* Get the mapped netdev TC# for the UP */
5674 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5675 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5676 	}
5677 }
5678 
5679 /**
5680  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5681  * @vsi: the VSI being configured
5682  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5683  **/
5684 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5685 				      struct i40e_vsi_context *ctxt)
5686 {
5687 	/* copy just the sections touched not the entire info
5688 	 * since not all sections are valid as returned by
5689 	 * update vsi params
5690 	 */
5691 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5692 	memcpy(&vsi->info.queue_mapping,
5693 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5694 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5695 	       sizeof(vsi->info.tc_mapping));
5696 }
5697 
5698 /**
5699  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5700  * @vsi: the VSI being reconfigured
5701  * @vsi_offset: offset from main VF VSI
5702  */
5703 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5704 {
5705 	struct i40e_vsi_context ctxt = {};
5706 	struct i40e_pf *pf;
5707 	struct i40e_hw *hw;
5708 	int ret;
5709 
5710 	if (!vsi)
5711 		return I40E_ERR_PARAM;
5712 	pf = vsi->back;
5713 	hw = &pf->hw;
5714 
5715 	ctxt.seid = vsi->seid;
5716 	ctxt.pf_num = hw->pf_id;
5717 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5718 	ctxt.uplink_seid = vsi->uplink_seid;
5719 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5720 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5721 	ctxt.info = vsi->info;
5722 
5723 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5724 				 false);
5725 	if (vsi->reconfig_rss) {
5726 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5727 				      vsi->num_queue_pairs);
5728 		ret = i40e_vsi_config_rss(vsi);
5729 		if (ret) {
5730 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5731 			return ret;
5732 		}
5733 		vsi->reconfig_rss = false;
5734 	}
5735 
5736 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5737 	if (ret) {
5738 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5739 			 i40e_stat_str(hw, ret),
5740 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5741 		return ret;
5742 	}
5743 	/* update the local VSI info with updated queue map */
5744 	i40e_vsi_update_queue_map(vsi, &ctxt);
5745 	vsi->info.valid_sections = 0;
5746 
5747 	return ret;
5748 }
5749 
5750 /**
5751  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5752  * @vsi: VSI to be configured
5753  * @enabled_tc: TC bitmap
5754  *
5755  * This configures a particular VSI for TCs that are mapped to the
5756  * given TC bitmap. It uses default bandwidth share for TCs across
5757  * VSIs to configure TC for a particular VSI.
5758  *
5759  * NOTE:
5760  * It is expected that the VSI queues have been quisced before calling
5761  * this function.
5762  **/
5763 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5764 {
5765 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5766 	struct i40e_pf *pf = vsi->back;
5767 	struct i40e_hw *hw = &pf->hw;
5768 	struct i40e_vsi_context ctxt;
5769 	int ret = 0;
5770 	int i;
5771 
5772 	/* Check if enabled_tc is same as existing or new TCs */
5773 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5774 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5775 		return ret;
5776 
5777 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5778 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5779 		if (enabled_tc & BIT(i))
5780 			bw_share[i] = 1;
5781 	}
5782 
5783 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5784 	if (ret) {
5785 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5786 
5787 		dev_info(&pf->pdev->dev,
5788 			 "Failed configuring TC map %d for VSI %d\n",
5789 			 enabled_tc, vsi->seid);
5790 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5791 						  &bw_config, NULL);
5792 		if (ret) {
5793 			dev_info(&pf->pdev->dev,
5794 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5795 				 i40e_stat_str(hw, ret),
5796 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5797 			goto out;
5798 		}
5799 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5800 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5801 
5802 			if (!valid_tc)
5803 				valid_tc = bw_config.tc_valid_bits;
5804 			/* Always enable TC0, no matter what */
5805 			valid_tc |= 1;
5806 			dev_info(&pf->pdev->dev,
5807 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5808 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5809 			enabled_tc = valid_tc;
5810 		}
5811 
5812 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5813 		if (ret) {
5814 			dev_err(&pf->pdev->dev,
5815 				"Unable to  configure TC map %d for VSI %d\n",
5816 				enabled_tc, vsi->seid);
5817 			goto out;
5818 		}
5819 	}
5820 
5821 	/* Update Queue Pairs Mapping for currently enabled UPs */
5822 	ctxt.seid = vsi->seid;
5823 	ctxt.pf_num = vsi->back->hw.pf_id;
5824 	ctxt.vf_num = 0;
5825 	ctxt.uplink_seid = vsi->uplink_seid;
5826 	ctxt.info = vsi->info;
5827 	if (i40e_is_tc_mqprio_enabled(pf)) {
5828 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5829 		if (ret)
5830 			goto out;
5831 	} else {
5832 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5833 	}
5834 
5835 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5836 	 * queues changed.
5837 	 */
5838 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5839 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5840 				      vsi->num_queue_pairs);
5841 		ret = i40e_vsi_config_rss(vsi);
5842 		if (ret) {
5843 			dev_info(&vsi->back->pdev->dev,
5844 				 "Failed to reconfig rss for num_queues\n");
5845 			return ret;
5846 		}
5847 		vsi->reconfig_rss = false;
5848 	}
5849 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5850 		ctxt.info.valid_sections |=
5851 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5852 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5853 	}
5854 
5855 	/* Update the VSI after updating the VSI queue-mapping
5856 	 * information
5857 	 */
5858 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5859 	if (ret) {
5860 		dev_info(&pf->pdev->dev,
5861 			 "Update vsi tc config failed, err %s aq_err %s\n",
5862 			 i40e_stat_str(hw, ret),
5863 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5864 		goto out;
5865 	}
5866 	/* update the local VSI info with updated queue map */
5867 	i40e_vsi_update_queue_map(vsi, &ctxt);
5868 	vsi->info.valid_sections = 0;
5869 
5870 	/* Update current VSI BW information */
5871 	ret = i40e_vsi_get_bw_info(vsi);
5872 	if (ret) {
5873 		dev_info(&pf->pdev->dev,
5874 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5875 			 i40e_stat_str(hw, ret),
5876 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5877 		goto out;
5878 	}
5879 
5880 	/* Update the netdev TC setup */
5881 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5882 out:
5883 	return ret;
5884 }
5885 
5886 /**
5887  * i40e_get_link_speed - Returns link speed for the interface
5888  * @vsi: VSI to be configured
5889  *
5890  **/
5891 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5892 {
5893 	struct i40e_pf *pf = vsi->back;
5894 
5895 	switch (pf->hw.phy.link_info.link_speed) {
5896 	case I40E_LINK_SPEED_40GB:
5897 		return 40000;
5898 	case I40E_LINK_SPEED_25GB:
5899 		return 25000;
5900 	case I40E_LINK_SPEED_20GB:
5901 		return 20000;
5902 	case I40E_LINK_SPEED_10GB:
5903 		return 10000;
5904 	case I40E_LINK_SPEED_1GB:
5905 		return 1000;
5906 	default:
5907 		return -EINVAL;
5908 	}
5909 }
5910 
5911 /**
5912  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5913  * @vsi: VSI to be configured
5914  * @seid: seid of the channel/VSI
5915  * @max_tx_rate: max TX rate to be configured as BW limit
5916  *
5917  * Helper function to set BW limit for a given VSI
5918  **/
5919 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5920 {
5921 	struct i40e_pf *pf = vsi->back;
5922 	u64 credits = 0;
5923 	int speed = 0;
5924 	int ret = 0;
5925 
5926 	speed = i40e_get_link_speed(vsi);
5927 	if (max_tx_rate > speed) {
5928 		dev_err(&pf->pdev->dev,
5929 			"Invalid max tx rate %llu specified for VSI seid %d.",
5930 			max_tx_rate, seid);
5931 		return -EINVAL;
5932 	}
5933 	if (max_tx_rate && max_tx_rate < 50) {
5934 		dev_warn(&pf->pdev->dev,
5935 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5936 		max_tx_rate = 50;
5937 	}
5938 
5939 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5940 	credits = max_tx_rate;
5941 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5942 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5943 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5944 	if (ret)
5945 		dev_err(&pf->pdev->dev,
5946 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5947 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5948 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5949 	return ret;
5950 }
5951 
5952 /**
5953  * i40e_remove_queue_channels - Remove queue channels for the TCs
5954  * @vsi: VSI to be configured
5955  *
5956  * Remove queue channels for the TCs
5957  **/
5958 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5959 {
5960 	enum i40e_admin_queue_err last_aq_status;
5961 	struct i40e_cloud_filter *cfilter;
5962 	struct i40e_channel *ch, *ch_tmp;
5963 	struct i40e_pf *pf = vsi->back;
5964 	struct hlist_node *node;
5965 	int ret, i;
5966 
5967 	/* Reset rss size that was stored when reconfiguring rss for
5968 	 * channel VSIs with non-power-of-2 queue count.
5969 	 */
5970 	vsi->current_rss_size = 0;
5971 
5972 	/* perform cleanup for channels if they exist */
5973 	if (list_empty(&vsi->ch_list))
5974 		return;
5975 
5976 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5977 		struct i40e_vsi *p_vsi;
5978 
5979 		list_del(&ch->list);
5980 		p_vsi = ch->parent_vsi;
5981 		if (!p_vsi || !ch->initialized) {
5982 			kfree(ch);
5983 			continue;
5984 		}
5985 		/* Reset queue contexts */
5986 		for (i = 0; i < ch->num_queue_pairs; i++) {
5987 			struct i40e_ring *tx_ring, *rx_ring;
5988 			u16 pf_q;
5989 
5990 			pf_q = ch->base_queue + i;
5991 			tx_ring = vsi->tx_rings[pf_q];
5992 			tx_ring->ch = NULL;
5993 
5994 			rx_ring = vsi->rx_rings[pf_q];
5995 			rx_ring->ch = NULL;
5996 		}
5997 
5998 		/* Reset BW configured for this VSI via mqprio */
5999 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6000 		if (ret)
6001 			dev_info(&vsi->back->pdev->dev,
6002 				 "Failed to reset tx rate for ch->seid %u\n",
6003 				 ch->seid);
6004 
6005 		/* delete cloud filters associated with this channel */
6006 		hlist_for_each_entry_safe(cfilter, node,
6007 					  &pf->cloud_filter_list, cloud_node) {
6008 			if (cfilter->seid != ch->seid)
6009 				continue;
6010 
6011 			hash_del(&cfilter->cloud_node);
6012 			if (cfilter->dst_port)
6013 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6014 									cfilter,
6015 									false);
6016 			else
6017 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6018 								false);
6019 			last_aq_status = pf->hw.aq.asq_last_status;
6020 			if (ret)
6021 				dev_info(&pf->pdev->dev,
6022 					 "Failed to delete cloud filter, err %s aq_err %s\n",
6023 					 i40e_stat_str(&pf->hw, ret),
6024 					 i40e_aq_str(&pf->hw, last_aq_status));
6025 			kfree(cfilter);
6026 		}
6027 
6028 		/* delete VSI from FW */
6029 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6030 					     NULL);
6031 		if (ret)
6032 			dev_err(&vsi->back->pdev->dev,
6033 				"unable to remove channel (%d) for parent VSI(%d)\n",
6034 				ch->seid, p_vsi->seid);
6035 		kfree(ch);
6036 	}
6037 	INIT_LIST_HEAD(&vsi->ch_list);
6038 }
6039 
6040 /**
6041  * i40e_get_max_queues_for_channel
6042  * @vsi: ptr to VSI to which channels are associated with
6043  *
6044  * Helper function which returns max value among the queue counts set on the
6045  * channels/TCs created.
6046  **/
6047 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6048 {
6049 	struct i40e_channel *ch, *ch_tmp;
6050 	int max = 0;
6051 
6052 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6053 		if (!ch->initialized)
6054 			continue;
6055 		if (ch->num_queue_pairs > max)
6056 			max = ch->num_queue_pairs;
6057 	}
6058 
6059 	return max;
6060 }
6061 
6062 /**
6063  * i40e_validate_num_queues - validate num_queues w.r.t channel
6064  * @pf: ptr to PF device
6065  * @num_queues: number of queues
6066  * @vsi: the parent VSI
6067  * @reconfig_rss: indicates should the RSS be reconfigured or not
6068  *
6069  * This function validates number of queues in the context of new channel
6070  * which is being established and determines if RSS should be reconfigured
6071  * or not for parent VSI.
6072  **/
6073 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6074 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6075 {
6076 	int max_ch_queues;
6077 
6078 	if (!reconfig_rss)
6079 		return -EINVAL;
6080 
6081 	*reconfig_rss = false;
6082 	if (vsi->current_rss_size) {
6083 		if (num_queues > vsi->current_rss_size) {
6084 			dev_dbg(&pf->pdev->dev,
6085 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6086 				num_queues, vsi->current_rss_size);
6087 			return -EINVAL;
6088 		} else if ((num_queues < vsi->current_rss_size) &&
6089 			   (!is_power_of_2(num_queues))) {
6090 			dev_dbg(&pf->pdev->dev,
6091 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6092 				num_queues, vsi->current_rss_size);
6093 			return -EINVAL;
6094 		}
6095 	}
6096 
6097 	if (!is_power_of_2(num_queues)) {
6098 		/* Find the max num_queues configured for channel if channel
6099 		 * exist.
6100 		 * if channel exist, then enforce 'num_queues' to be more than
6101 		 * max ever queues configured for channel.
6102 		 */
6103 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6104 		if (num_queues < max_ch_queues) {
6105 			dev_dbg(&pf->pdev->dev,
6106 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6107 				num_queues, max_ch_queues);
6108 			return -EINVAL;
6109 		}
6110 		*reconfig_rss = true;
6111 	}
6112 
6113 	return 0;
6114 }
6115 
6116 /**
6117  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6118  * @vsi: the VSI being setup
6119  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6120  *
6121  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6122  **/
6123 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6124 {
6125 	struct i40e_pf *pf = vsi->back;
6126 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6127 	struct i40e_hw *hw = &pf->hw;
6128 	int local_rss_size;
6129 	u8 *lut;
6130 	int ret;
6131 
6132 	if (!vsi->rss_size)
6133 		return -EINVAL;
6134 
6135 	if (rss_size > vsi->rss_size)
6136 		return -EINVAL;
6137 
6138 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6139 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6140 	if (!lut)
6141 		return -ENOMEM;
6142 
6143 	/* Ignoring user configured lut if there is one */
6144 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6145 
6146 	/* Use user configured hash key if there is one, otherwise
6147 	 * use default.
6148 	 */
6149 	if (vsi->rss_hkey_user)
6150 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6151 	else
6152 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6153 
6154 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6155 	if (ret) {
6156 		dev_info(&pf->pdev->dev,
6157 			 "Cannot set RSS lut, err %s aq_err %s\n",
6158 			 i40e_stat_str(hw, ret),
6159 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6160 		kfree(lut);
6161 		return ret;
6162 	}
6163 	kfree(lut);
6164 
6165 	/* Do the update w.r.t. storing rss_size */
6166 	if (!vsi->orig_rss_size)
6167 		vsi->orig_rss_size = vsi->rss_size;
6168 	vsi->current_rss_size = local_rss_size;
6169 
6170 	return ret;
6171 }
6172 
6173 /**
6174  * i40e_channel_setup_queue_map - Setup a channel queue map
6175  * @pf: ptr to PF device
6176  * @ctxt: VSI context structure
6177  * @ch: ptr to channel structure
6178  *
6179  * Setup queue map for a specific channel
6180  **/
6181 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6182 					 struct i40e_vsi_context *ctxt,
6183 					 struct i40e_channel *ch)
6184 {
6185 	u16 qcount, qmap, sections = 0;
6186 	u8 offset = 0;
6187 	int pow;
6188 
6189 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6190 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6191 
6192 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6193 	ch->num_queue_pairs = qcount;
6194 
6195 	/* find the next higher power-of-2 of num queue pairs */
6196 	pow = ilog2(qcount);
6197 	if (!is_power_of_2(qcount))
6198 		pow++;
6199 
6200 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6201 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6202 
6203 	/* Setup queue TC[0].qmap for given VSI context */
6204 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6205 
6206 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6207 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6208 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6209 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6210 }
6211 
6212 /**
6213  * i40e_add_channel - add a channel by adding VSI
6214  * @pf: ptr to PF device
6215  * @uplink_seid: underlying HW switching element (VEB) ID
6216  * @ch: ptr to channel structure
6217  *
6218  * Add a channel (VSI) using add_vsi and queue_map
6219  **/
6220 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6221 			    struct i40e_channel *ch)
6222 {
6223 	struct i40e_hw *hw = &pf->hw;
6224 	struct i40e_vsi_context ctxt;
6225 	u8 enabled_tc = 0x1; /* TC0 enabled */
6226 	int ret;
6227 
6228 	if (ch->type != I40E_VSI_VMDQ2) {
6229 		dev_info(&pf->pdev->dev,
6230 			 "add new vsi failed, ch->type %d\n", ch->type);
6231 		return -EINVAL;
6232 	}
6233 
6234 	memset(&ctxt, 0, sizeof(ctxt));
6235 	ctxt.pf_num = hw->pf_id;
6236 	ctxt.vf_num = 0;
6237 	ctxt.uplink_seid = uplink_seid;
6238 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6239 	if (ch->type == I40E_VSI_VMDQ2)
6240 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6241 
6242 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6243 		ctxt.info.valid_sections |=
6244 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6245 		ctxt.info.switch_id =
6246 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6247 	}
6248 
6249 	/* Set queue map for a given VSI context */
6250 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6251 
6252 	/* Now time to create VSI */
6253 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6254 	if (ret) {
6255 		dev_info(&pf->pdev->dev,
6256 			 "add new vsi failed, err %s aq_err %s\n",
6257 			 i40e_stat_str(&pf->hw, ret),
6258 			 i40e_aq_str(&pf->hw,
6259 				     pf->hw.aq.asq_last_status));
6260 		return -ENOENT;
6261 	}
6262 
6263 	/* Success, update channel, set enabled_tc only if the channel
6264 	 * is not a macvlan
6265 	 */
6266 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6267 	ch->seid = ctxt.seid;
6268 	ch->vsi_number = ctxt.vsi_number;
6269 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6270 
6271 	/* copy just the sections touched not the entire info
6272 	 * since not all sections are valid as returned by
6273 	 * update vsi params
6274 	 */
6275 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6276 	memcpy(&ch->info.queue_mapping,
6277 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6278 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6279 	       sizeof(ctxt.info.tc_mapping));
6280 
6281 	return 0;
6282 }
6283 
6284 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6285 				  u8 *bw_share)
6286 {
6287 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6288 	i40e_status ret;
6289 	int i;
6290 
6291 	memset(&bw_data, 0, sizeof(bw_data));
6292 	bw_data.tc_valid_bits = ch->enabled_tc;
6293 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6294 		bw_data.tc_bw_credits[i] = bw_share[i];
6295 
6296 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6297 				       &bw_data, NULL);
6298 	if (ret) {
6299 		dev_info(&vsi->back->pdev->dev,
6300 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6301 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6302 		return -EINVAL;
6303 	}
6304 
6305 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6306 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6307 
6308 	return 0;
6309 }
6310 
6311 /**
6312  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6313  * @pf: ptr to PF device
6314  * @vsi: the VSI being setup
6315  * @ch: ptr to channel structure
6316  *
6317  * Configure TX rings associated with channel (VSI) since queues are being
6318  * from parent VSI.
6319  **/
6320 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6321 				       struct i40e_vsi *vsi,
6322 				       struct i40e_channel *ch)
6323 {
6324 	i40e_status ret;
6325 	int i;
6326 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6327 
6328 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6329 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6330 		if (ch->enabled_tc & BIT(i))
6331 			bw_share[i] = 1;
6332 	}
6333 
6334 	/* configure BW for new VSI */
6335 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6336 	if (ret) {
6337 		dev_info(&vsi->back->pdev->dev,
6338 			 "Failed configuring TC map %d for channel (seid %u)\n",
6339 			 ch->enabled_tc, ch->seid);
6340 		return ret;
6341 	}
6342 
6343 	for (i = 0; i < ch->num_queue_pairs; i++) {
6344 		struct i40e_ring *tx_ring, *rx_ring;
6345 		u16 pf_q;
6346 
6347 		pf_q = ch->base_queue + i;
6348 
6349 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6350 		 * context
6351 		 */
6352 		tx_ring = vsi->tx_rings[pf_q];
6353 		tx_ring->ch = ch;
6354 
6355 		/* Get the RX ring ptr */
6356 		rx_ring = vsi->rx_rings[pf_q];
6357 		rx_ring->ch = ch;
6358 	}
6359 
6360 	return 0;
6361 }
6362 
6363 /**
6364  * i40e_setup_hw_channel - setup new channel
6365  * @pf: ptr to PF device
6366  * @vsi: the VSI being setup
6367  * @ch: ptr to channel structure
6368  * @uplink_seid: underlying HW switching element (VEB) ID
6369  * @type: type of channel to be created (VMDq2/VF)
6370  *
6371  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6372  * and configures TX rings accordingly
6373  **/
6374 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6375 					struct i40e_vsi *vsi,
6376 					struct i40e_channel *ch,
6377 					u16 uplink_seid, u8 type)
6378 {
6379 	int ret;
6380 
6381 	ch->initialized = false;
6382 	ch->base_queue = vsi->next_base_queue;
6383 	ch->type = type;
6384 
6385 	/* Proceed with creation of channel (VMDq2) VSI */
6386 	ret = i40e_add_channel(pf, uplink_seid, ch);
6387 	if (ret) {
6388 		dev_info(&pf->pdev->dev,
6389 			 "failed to add_channel using uplink_seid %u\n",
6390 			 uplink_seid);
6391 		return ret;
6392 	}
6393 
6394 	/* Mark the successful creation of channel */
6395 	ch->initialized = true;
6396 
6397 	/* Reconfigure TX queues using QTX_CTL register */
6398 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6399 	if (ret) {
6400 		dev_info(&pf->pdev->dev,
6401 			 "failed to configure TX rings for channel %u\n",
6402 			 ch->seid);
6403 		return ret;
6404 	}
6405 
6406 	/* update 'next_base_queue' */
6407 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6408 	dev_dbg(&pf->pdev->dev,
6409 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6410 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6411 		ch->num_queue_pairs,
6412 		vsi->next_base_queue);
6413 	return ret;
6414 }
6415 
6416 /**
6417  * i40e_setup_channel - setup new channel using uplink element
6418  * @pf: ptr to PF device
6419  * @vsi: pointer to the VSI to set up the channel within
6420  * @ch: ptr to channel structure
6421  *
6422  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6423  * and uplink switching element (uplink_seid)
6424  **/
6425 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6426 			       struct i40e_channel *ch)
6427 {
6428 	u8 vsi_type;
6429 	u16 seid;
6430 	int ret;
6431 
6432 	if (vsi->type == I40E_VSI_MAIN) {
6433 		vsi_type = I40E_VSI_VMDQ2;
6434 	} else {
6435 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6436 			vsi->type);
6437 		return false;
6438 	}
6439 
6440 	/* underlying switching element */
6441 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6442 
6443 	/* create channel (VSI), configure TX rings */
6444 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6445 	if (ret) {
6446 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6447 		return false;
6448 	}
6449 
6450 	return ch->initialized ? true : false;
6451 }
6452 
6453 /**
6454  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6455  * @vsi: ptr to VSI which has PF backing
6456  *
6457  * Sets up switch mode correctly if it needs to be changed and perform
6458  * what are allowed modes.
6459  **/
6460 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6461 {
6462 	u8 mode;
6463 	struct i40e_pf *pf = vsi->back;
6464 	struct i40e_hw *hw = &pf->hw;
6465 	int ret;
6466 
6467 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6468 	if (ret)
6469 		return -EINVAL;
6470 
6471 	if (hw->dev_caps.switch_mode) {
6472 		/* if switch mode is set, support mode2 (non-tunneled for
6473 		 * cloud filter) for now
6474 		 */
6475 		u32 switch_mode = hw->dev_caps.switch_mode &
6476 				  I40E_SWITCH_MODE_MASK;
6477 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6478 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6479 				return 0;
6480 			dev_err(&pf->pdev->dev,
6481 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6482 				hw->dev_caps.switch_mode);
6483 			return -EINVAL;
6484 		}
6485 	}
6486 
6487 	/* Set Bit 7 to be valid */
6488 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6489 
6490 	/* Set L4type for TCP support */
6491 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6492 
6493 	/* Set cloud filter mode */
6494 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6495 
6496 	/* Prep mode field for set_switch_config */
6497 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6498 					pf->last_sw_conf_valid_flags,
6499 					mode, NULL);
6500 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6501 		dev_err(&pf->pdev->dev,
6502 			"couldn't set switch config bits, err %s aq_err %s\n",
6503 			i40e_stat_str(hw, ret),
6504 			i40e_aq_str(hw,
6505 				    hw->aq.asq_last_status));
6506 
6507 	return ret;
6508 }
6509 
6510 /**
6511  * i40e_create_queue_channel - function to create channel
6512  * @vsi: VSI to be configured
6513  * @ch: ptr to channel (it contains channel specific params)
6514  *
6515  * This function creates channel (VSI) using num_queues specified by user,
6516  * reconfigs RSS if needed.
6517  **/
6518 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6519 			      struct i40e_channel *ch)
6520 {
6521 	struct i40e_pf *pf = vsi->back;
6522 	bool reconfig_rss;
6523 	int err;
6524 
6525 	if (!ch)
6526 		return -EINVAL;
6527 
6528 	if (!ch->num_queue_pairs) {
6529 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6530 			ch->num_queue_pairs);
6531 		return -EINVAL;
6532 	}
6533 
6534 	/* validate user requested num_queues for channel */
6535 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6536 				       &reconfig_rss);
6537 	if (err) {
6538 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6539 			 ch->num_queue_pairs);
6540 		return -EINVAL;
6541 	}
6542 
6543 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6544 	 * VSI to be added switch to VEB mode.
6545 	 */
6546 
6547 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6548 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6549 
6550 		if (vsi->type == I40E_VSI_MAIN) {
6551 			if (i40e_is_tc_mqprio_enabled(pf))
6552 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6553 			else
6554 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6555 		}
6556 		/* now onwards for main VSI, number of queues will be value
6557 		 * of TC0's queue count
6558 		 */
6559 	}
6560 
6561 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6562 	 * it should be more than num_queues
6563 	 */
6564 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6565 		dev_dbg(&pf->pdev->dev,
6566 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6567 			vsi->cnt_q_avail, ch->num_queue_pairs);
6568 		return -EINVAL;
6569 	}
6570 
6571 	/* reconfig_rss only if vsi type is MAIN_VSI */
6572 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6573 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6574 		if (err) {
6575 			dev_info(&pf->pdev->dev,
6576 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6577 				 ch->num_queue_pairs);
6578 			return -EINVAL;
6579 		}
6580 	}
6581 
6582 	if (!i40e_setup_channel(pf, vsi, ch)) {
6583 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6584 		return -EINVAL;
6585 	}
6586 
6587 	dev_info(&pf->pdev->dev,
6588 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6589 		 ch->seid, ch->num_queue_pairs);
6590 
6591 	/* configure VSI for BW limit */
6592 	if (ch->max_tx_rate) {
6593 		u64 credits = ch->max_tx_rate;
6594 
6595 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6596 			return -EINVAL;
6597 
6598 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6599 		dev_dbg(&pf->pdev->dev,
6600 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6601 			ch->max_tx_rate,
6602 			credits,
6603 			ch->seid);
6604 	}
6605 
6606 	/* in case of VF, this will be main SRIOV VSI */
6607 	ch->parent_vsi = vsi;
6608 
6609 	/* and update main_vsi's count for queue_available to use */
6610 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6611 
6612 	return 0;
6613 }
6614 
6615 /**
6616  * i40e_configure_queue_channels - Add queue channel for the given TCs
6617  * @vsi: VSI to be configured
6618  *
6619  * Configures queue channel mapping to the given TCs
6620  **/
6621 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6622 {
6623 	struct i40e_channel *ch;
6624 	u64 max_rate = 0;
6625 	int ret = 0, i;
6626 
6627 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6628 	vsi->tc_seid_map[0] = vsi->seid;
6629 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6630 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6631 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6632 			if (!ch) {
6633 				ret = -ENOMEM;
6634 				goto err_free;
6635 			}
6636 
6637 			INIT_LIST_HEAD(&ch->list);
6638 			ch->num_queue_pairs =
6639 				vsi->tc_config.tc_info[i].qcount;
6640 			ch->base_queue =
6641 				vsi->tc_config.tc_info[i].qoffset;
6642 
6643 			/* Bandwidth limit through tc interface is in bytes/s,
6644 			 * change to Mbit/s
6645 			 */
6646 			max_rate = vsi->mqprio_qopt.max_rate[i];
6647 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6648 			ch->max_tx_rate = max_rate;
6649 
6650 			list_add_tail(&ch->list, &vsi->ch_list);
6651 
6652 			ret = i40e_create_queue_channel(vsi, ch);
6653 			if (ret) {
6654 				dev_err(&vsi->back->pdev->dev,
6655 					"Failed creating queue channel with TC%d: queues %d\n",
6656 					i, ch->num_queue_pairs);
6657 				goto err_free;
6658 			}
6659 			vsi->tc_seid_map[i] = ch->seid;
6660 		}
6661 	}
6662 	return ret;
6663 
6664 err_free:
6665 	i40e_remove_queue_channels(vsi);
6666 	return ret;
6667 }
6668 
6669 /**
6670  * i40e_veb_config_tc - Configure TCs for given VEB
6671  * @veb: given VEB
6672  * @enabled_tc: TC bitmap
6673  *
6674  * Configures given TC bitmap for VEB (switching) element
6675  **/
6676 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6677 {
6678 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6679 	struct i40e_pf *pf = veb->pf;
6680 	int ret = 0;
6681 	int i;
6682 
6683 	/* No TCs or already enabled TCs just return */
6684 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6685 		return ret;
6686 
6687 	bw_data.tc_valid_bits = enabled_tc;
6688 	/* bw_data.absolute_credits is not set (relative) */
6689 
6690 	/* Enable ETS TCs with equal BW Share for now */
6691 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6692 		if (enabled_tc & BIT(i))
6693 			bw_data.tc_bw_share_credits[i] = 1;
6694 	}
6695 
6696 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6697 						   &bw_data, NULL);
6698 	if (ret) {
6699 		dev_info(&pf->pdev->dev,
6700 			 "VEB bw config failed, err %s aq_err %s\n",
6701 			 i40e_stat_str(&pf->hw, ret),
6702 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6703 		goto out;
6704 	}
6705 
6706 	/* Update the BW information */
6707 	ret = i40e_veb_get_bw_info(veb);
6708 	if (ret) {
6709 		dev_info(&pf->pdev->dev,
6710 			 "Failed getting veb bw config, err %s aq_err %s\n",
6711 			 i40e_stat_str(&pf->hw, ret),
6712 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6713 	}
6714 
6715 out:
6716 	return ret;
6717 }
6718 
6719 #ifdef CONFIG_I40E_DCB
6720 /**
6721  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6722  * @pf: PF struct
6723  *
6724  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6725  * the caller would've quiesce all the VSIs before calling
6726  * this function
6727  **/
6728 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6729 {
6730 	u8 tc_map = 0;
6731 	int ret;
6732 	u8 v;
6733 
6734 	/* Enable the TCs available on PF to all VEBs */
6735 	tc_map = i40e_pf_get_tc_map(pf);
6736 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6737 		return;
6738 
6739 	for (v = 0; v < I40E_MAX_VEB; v++) {
6740 		if (!pf->veb[v])
6741 			continue;
6742 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6743 		if (ret) {
6744 			dev_info(&pf->pdev->dev,
6745 				 "Failed configuring TC for VEB seid=%d\n",
6746 				 pf->veb[v]->seid);
6747 			/* Will try to configure as many components */
6748 		}
6749 	}
6750 
6751 	/* Update each VSI */
6752 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6753 		if (!pf->vsi[v])
6754 			continue;
6755 
6756 		/* - Enable all TCs for the LAN VSI
6757 		 * - For all others keep them at TC0 for now
6758 		 */
6759 		if (v == pf->lan_vsi)
6760 			tc_map = i40e_pf_get_tc_map(pf);
6761 		else
6762 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6763 
6764 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6765 		if (ret) {
6766 			dev_info(&pf->pdev->dev,
6767 				 "Failed configuring TC for VSI seid=%d\n",
6768 				 pf->vsi[v]->seid);
6769 			/* Will try to configure as many components */
6770 		} else {
6771 			/* Re-configure VSI vectors based on updated TC map */
6772 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6773 			if (pf->vsi[v]->netdev)
6774 				i40e_dcbnl_set_all(pf->vsi[v]);
6775 		}
6776 	}
6777 }
6778 
6779 /**
6780  * i40e_resume_port_tx - Resume port Tx
6781  * @pf: PF struct
6782  *
6783  * Resume a port's Tx and issue a PF reset in case of failure to
6784  * resume.
6785  **/
6786 static int i40e_resume_port_tx(struct i40e_pf *pf)
6787 {
6788 	struct i40e_hw *hw = &pf->hw;
6789 	int ret;
6790 
6791 	ret = i40e_aq_resume_port_tx(hw, NULL);
6792 	if (ret) {
6793 		dev_info(&pf->pdev->dev,
6794 			 "Resume Port Tx failed, err %s aq_err %s\n",
6795 			  i40e_stat_str(&pf->hw, ret),
6796 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6797 		/* Schedule PF reset to recover */
6798 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6799 		i40e_service_event_schedule(pf);
6800 	}
6801 
6802 	return ret;
6803 }
6804 
6805 /**
6806  * i40e_suspend_port_tx - Suspend port Tx
6807  * @pf: PF struct
6808  *
6809  * Suspend a port's Tx and issue a PF reset in case of failure.
6810  **/
6811 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6812 {
6813 	struct i40e_hw *hw = &pf->hw;
6814 	int ret;
6815 
6816 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6817 	if (ret) {
6818 		dev_info(&pf->pdev->dev,
6819 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6820 			 i40e_stat_str(&pf->hw, ret),
6821 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6822 		/* Schedule PF reset to recover */
6823 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6824 		i40e_service_event_schedule(pf);
6825 	}
6826 
6827 	return ret;
6828 }
6829 
6830 /**
6831  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6832  * @pf: PF being configured
6833  * @new_cfg: New DCBX configuration
6834  *
6835  * Program DCB settings into HW and reconfigure VEB/VSIs on
6836  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6837  **/
6838 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6839 				  struct i40e_dcbx_config *new_cfg)
6840 {
6841 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6842 	int ret;
6843 
6844 	/* Check if need reconfiguration */
6845 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6846 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6847 		return 0;
6848 	}
6849 
6850 	/* Config change disable all VSIs */
6851 	i40e_pf_quiesce_all_vsi(pf);
6852 
6853 	/* Copy the new config to the current config */
6854 	*old_cfg = *new_cfg;
6855 	old_cfg->etsrec = old_cfg->etscfg;
6856 	ret = i40e_set_dcb_config(&pf->hw);
6857 	if (ret) {
6858 		dev_info(&pf->pdev->dev,
6859 			 "Set DCB Config failed, err %s aq_err %s\n",
6860 			 i40e_stat_str(&pf->hw, ret),
6861 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6862 		goto out;
6863 	}
6864 
6865 	/* Changes in configuration update VEB/VSI */
6866 	i40e_dcb_reconfigure(pf);
6867 out:
6868 	/* In case of reset do not try to resume anything */
6869 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6870 		/* Re-start the VSIs if disabled */
6871 		ret = i40e_resume_port_tx(pf);
6872 		/* In case of error no point in resuming VSIs */
6873 		if (ret)
6874 			goto err;
6875 		i40e_pf_unquiesce_all_vsi(pf);
6876 	}
6877 err:
6878 	return ret;
6879 }
6880 
6881 /**
6882  * i40e_hw_dcb_config - Program new DCBX settings into HW
6883  * @pf: PF being configured
6884  * @new_cfg: New DCBX configuration
6885  *
6886  * Program DCB settings into HW and reconfigure VEB/VSIs on
6887  * given PF
6888  **/
6889 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6890 {
6891 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6892 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6893 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6894 	struct i40e_dcbx_config *old_cfg;
6895 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6896 	struct i40e_rx_pb_config pb_cfg;
6897 	struct i40e_hw *hw = &pf->hw;
6898 	u8 num_ports = hw->num_ports;
6899 	bool need_reconfig;
6900 	int ret = -EINVAL;
6901 	u8 lltc_map = 0;
6902 	u8 tc_map = 0;
6903 	u8 new_numtc;
6904 	u8 i;
6905 
6906 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6907 	/* Un-pack information to Program ETS HW via shared API
6908 	 * numtc, tcmap
6909 	 * LLTC map
6910 	 * ETS/NON-ETS arbiter mode
6911 	 * max exponent (credit refills)
6912 	 * Total number of ports
6913 	 * PFC priority bit-map
6914 	 * Priority Table
6915 	 * BW % per TC
6916 	 * Arbiter mode between UPs sharing same TC
6917 	 * TSA table (ETS or non-ETS)
6918 	 * EEE enabled or not
6919 	 * MFS TC table
6920 	 */
6921 
6922 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6923 
6924 	memset(&ets_data, 0, sizeof(ets_data));
6925 	for (i = 0; i < new_numtc; i++) {
6926 		tc_map |= BIT(i);
6927 		switch (new_cfg->etscfg.tsatable[i]) {
6928 		case I40E_IEEE_TSA_ETS:
6929 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6930 			ets_data.tc_bw_share_credits[i] =
6931 					new_cfg->etscfg.tcbwtable[i];
6932 			break;
6933 		case I40E_IEEE_TSA_STRICT:
6934 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6935 			lltc_map |= BIT(i);
6936 			ets_data.tc_bw_share_credits[i] =
6937 					I40E_DCB_STRICT_PRIO_CREDITS;
6938 			break;
6939 		default:
6940 			/* Invalid TSA type */
6941 			need_reconfig = false;
6942 			goto out;
6943 		}
6944 	}
6945 
6946 	old_cfg = &hw->local_dcbx_config;
6947 	/* Check if need reconfiguration */
6948 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6949 
6950 	/* If needed, enable/disable frame tagging, disable all VSIs
6951 	 * and suspend port tx
6952 	 */
6953 	if (need_reconfig) {
6954 		/* Enable DCB tagging only when more than one TC */
6955 		if (new_numtc > 1)
6956 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6957 		else
6958 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6959 
6960 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6961 		/* Reconfiguration needed quiesce all VSIs */
6962 		i40e_pf_quiesce_all_vsi(pf);
6963 		ret = i40e_suspend_port_tx(pf);
6964 		if (ret)
6965 			goto err;
6966 	}
6967 
6968 	/* Configure Port ETS Tx Scheduler */
6969 	ets_data.tc_valid_bits = tc_map;
6970 	ets_data.tc_strict_priority_flags = lltc_map;
6971 	ret = i40e_aq_config_switch_comp_ets
6972 		(hw, pf->mac_seid, &ets_data,
6973 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6974 	if (ret) {
6975 		dev_info(&pf->pdev->dev,
6976 			 "Modify Port ETS failed, err %s aq_err %s\n",
6977 			 i40e_stat_str(&pf->hw, ret),
6978 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6979 		goto out;
6980 	}
6981 
6982 	/* Configure Rx ETS HW */
6983 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6984 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6985 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6986 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6987 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6988 				   lltc_map);
6989 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6990 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6991 				     prio_type);
6992 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6993 			       new_cfg->etscfg.prioritytable);
6994 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6995 
6996 	/* Configure Rx Packet Buffers in HW */
6997 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6998 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6999 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7000 	}
7001 
7002 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7003 					 false, new_cfg->pfc.pfcenable,
7004 					 mfs_tc, &pb_cfg);
7005 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7006 
7007 	/* Update the local Rx Packet buffer config */
7008 	pf->pb_cfg = pb_cfg;
7009 
7010 	/* Inform the FW about changes to DCB configuration */
7011 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7012 	if (ret) {
7013 		dev_info(&pf->pdev->dev,
7014 			 "DCB Updated failed, err %s aq_err %s\n",
7015 			 i40e_stat_str(&pf->hw, ret),
7016 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7017 		goto out;
7018 	}
7019 
7020 	/* Update the port DCBx configuration */
7021 	*old_cfg = *new_cfg;
7022 
7023 	/* Changes in configuration update VEB/VSI */
7024 	i40e_dcb_reconfigure(pf);
7025 out:
7026 	/* Re-start the VSIs if disabled */
7027 	if (need_reconfig) {
7028 		ret = i40e_resume_port_tx(pf);
7029 
7030 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7031 		/* In case of error no point in resuming VSIs */
7032 		if (ret)
7033 			goto err;
7034 
7035 		/* Wait for the PF's queues to be disabled */
7036 		ret = i40e_pf_wait_queues_disabled(pf);
7037 		if (ret) {
7038 			/* Schedule PF reset to recover */
7039 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7040 			i40e_service_event_schedule(pf);
7041 			goto err;
7042 		} else {
7043 			i40e_pf_unquiesce_all_vsi(pf);
7044 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7045 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7046 		}
7047 		/* registers are set, lets apply */
7048 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7049 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7050 	}
7051 
7052 err:
7053 	return ret;
7054 }
7055 
7056 /**
7057  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7058  * @pf: PF being queried
7059  *
7060  * Set default DCB configuration in case DCB is to be done in SW.
7061  **/
7062 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7063 {
7064 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7065 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7066 	struct i40e_hw *hw = &pf->hw;
7067 	int err;
7068 
7069 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7070 		/* Update the local cached instance with TC0 ETS */
7071 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7072 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7073 		pf->tmp_cfg.etscfg.maxtcs = 0;
7074 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7075 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7076 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7077 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7078 		/* FW needs one App to configure HW */
7079 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7080 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7081 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7082 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7083 
7084 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7085 	}
7086 
7087 	memset(&ets_data, 0, sizeof(ets_data));
7088 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7089 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7090 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7091 
7092 	/* Enable ETS on the Physical port */
7093 	err = i40e_aq_config_switch_comp_ets
7094 		(hw, pf->mac_seid, &ets_data,
7095 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7096 	if (err) {
7097 		dev_info(&pf->pdev->dev,
7098 			 "Enable Port ETS failed, err %s aq_err %s\n",
7099 			 i40e_stat_str(&pf->hw, err),
7100 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7101 		err = -ENOENT;
7102 		goto out;
7103 	}
7104 
7105 	/* Update the local cached instance with TC0 ETS */
7106 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7107 	dcb_cfg->etscfg.cbs = 0;
7108 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7109 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7110 
7111 out:
7112 	return err;
7113 }
7114 
7115 /**
7116  * i40e_init_pf_dcb - Initialize DCB configuration
7117  * @pf: PF being configured
7118  *
7119  * Query the current DCB configuration and cache it
7120  * in the hardware structure
7121  **/
7122 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7123 {
7124 	struct i40e_hw *hw = &pf->hw;
7125 	int err;
7126 
7127 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7128 	 * Also do not enable DCBx if FW LLDP agent is disabled
7129 	 */
7130 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7131 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7132 		err = I40E_NOT_SUPPORTED;
7133 		goto out;
7134 	}
7135 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7136 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7137 		err = i40e_dcb_sw_default_config(pf);
7138 		if (err) {
7139 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7140 			goto out;
7141 		}
7142 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7143 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7144 			       DCB_CAP_DCBX_VER_IEEE;
7145 		/* at init capable but disabled */
7146 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7147 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7148 		goto out;
7149 	}
7150 	err = i40e_init_dcb(hw, true);
7151 	if (!err) {
7152 		/* Device/Function is not DCBX capable */
7153 		if ((!hw->func_caps.dcb) ||
7154 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7155 			dev_info(&pf->pdev->dev,
7156 				 "DCBX offload is not supported or is disabled for this PF.\n");
7157 		} else {
7158 			/* When status is not DISABLED then DCBX in FW */
7159 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7160 				       DCB_CAP_DCBX_VER_IEEE;
7161 
7162 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7163 			/* Enable DCB tagging only when more than one TC
7164 			 * or explicitly disable if only one TC
7165 			 */
7166 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7167 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7168 			else
7169 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7170 			dev_dbg(&pf->pdev->dev,
7171 				"DCBX offload is supported for this PF.\n");
7172 		}
7173 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7174 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7175 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7176 	} else {
7177 		dev_info(&pf->pdev->dev,
7178 			 "Query for DCB configuration failed, err %s aq_err %s\n",
7179 			 i40e_stat_str(&pf->hw, err),
7180 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7181 	}
7182 
7183 out:
7184 	return err;
7185 }
7186 #endif /* CONFIG_I40E_DCB */
7187 
7188 /**
7189  * i40e_print_link_message - print link up or down
7190  * @vsi: the VSI for which link needs a message
7191  * @isup: true of link is up, false otherwise
7192  */
7193 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7194 {
7195 	enum i40e_aq_link_speed new_speed;
7196 	struct i40e_pf *pf = vsi->back;
7197 	char *speed = "Unknown";
7198 	char *fc = "Unknown";
7199 	char *fec = "";
7200 	char *req_fec = "";
7201 	char *an = "";
7202 
7203 	if (isup)
7204 		new_speed = pf->hw.phy.link_info.link_speed;
7205 	else
7206 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7207 
7208 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7209 		return;
7210 	vsi->current_isup = isup;
7211 	vsi->current_speed = new_speed;
7212 	if (!isup) {
7213 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7214 		return;
7215 	}
7216 
7217 	/* Warn user if link speed on NPAR enabled partition is not at
7218 	 * least 10GB
7219 	 */
7220 	if (pf->hw.func_caps.npar_enable &&
7221 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7222 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7223 		netdev_warn(vsi->netdev,
7224 			    "The partition detected link speed that is less than 10Gbps\n");
7225 
7226 	switch (pf->hw.phy.link_info.link_speed) {
7227 	case I40E_LINK_SPEED_40GB:
7228 		speed = "40 G";
7229 		break;
7230 	case I40E_LINK_SPEED_20GB:
7231 		speed = "20 G";
7232 		break;
7233 	case I40E_LINK_SPEED_25GB:
7234 		speed = "25 G";
7235 		break;
7236 	case I40E_LINK_SPEED_10GB:
7237 		speed = "10 G";
7238 		break;
7239 	case I40E_LINK_SPEED_5GB:
7240 		speed = "5 G";
7241 		break;
7242 	case I40E_LINK_SPEED_2_5GB:
7243 		speed = "2.5 G";
7244 		break;
7245 	case I40E_LINK_SPEED_1GB:
7246 		speed = "1000 M";
7247 		break;
7248 	case I40E_LINK_SPEED_100MB:
7249 		speed = "100 M";
7250 		break;
7251 	default:
7252 		break;
7253 	}
7254 
7255 	switch (pf->hw.fc.current_mode) {
7256 	case I40E_FC_FULL:
7257 		fc = "RX/TX";
7258 		break;
7259 	case I40E_FC_TX_PAUSE:
7260 		fc = "TX";
7261 		break;
7262 	case I40E_FC_RX_PAUSE:
7263 		fc = "RX";
7264 		break;
7265 	default:
7266 		fc = "None";
7267 		break;
7268 	}
7269 
7270 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7271 		req_fec = "None";
7272 		fec = "None";
7273 		an = "False";
7274 
7275 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7276 			an = "True";
7277 
7278 		if (pf->hw.phy.link_info.fec_info &
7279 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7280 			fec = "CL74 FC-FEC/BASE-R";
7281 		else if (pf->hw.phy.link_info.fec_info &
7282 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7283 			fec = "CL108 RS-FEC";
7284 
7285 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7286 		 * both RS and FC are requested
7287 		 */
7288 		if (vsi->back->hw.phy.link_info.req_fec_info &
7289 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7290 			if (vsi->back->hw.phy.link_info.req_fec_info &
7291 			    I40E_AQ_REQUEST_FEC_RS)
7292 				req_fec = "CL108 RS-FEC";
7293 			else
7294 				req_fec = "CL74 FC-FEC/BASE-R";
7295 		}
7296 		netdev_info(vsi->netdev,
7297 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7298 			    speed, req_fec, fec, an, fc);
7299 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7300 		req_fec = "None";
7301 		fec = "None";
7302 		an = "False";
7303 
7304 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7305 			an = "True";
7306 
7307 		if (pf->hw.phy.link_info.fec_info &
7308 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7309 			fec = "CL74 FC-FEC/BASE-R";
7310 
7311 		if (pf->hw.phy.link_info.req_fec_info &
7312 		    I40E_AQ_REQUEST_FEC_KR)
7313 			req_fec = "CL74 FC-FEC/BASE-R";
7314 
7315 		netdev_info(vsi->netdev,
7316 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7317 			    speed, req_fec, fec, an, fc);
7318 	} else {
7319 		netdev_info(vsi->netdev,
7320 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7321 			    speed, fc);
7322 	}
7323 
7324 }
7325 
7326 /**
7327  * i40e_up_complete - Finish the last steps of bringing up a connection
7328  * @vsi: the VSI being configured
7329  **/
7330 static int i40e_up_complete(struct i40e_vsi *vsi)
7331 {
7332 	struct i40e_pf *pf = vsi->back;
7333 	int err;
7334 
7335 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7336 		i40e_vsi_configure_msix(vsi);
7337 	else
7338 		i40e_configure_msi_and_legacy(vsi);
7339 
7340 	/* start rings */
7341 	err = i40e_vsi_start_rings(vsi);
7342 	if (err)
7343 		return err;
7344 
7345 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7346 	i40e_napi_enable_all(vsi);
7347 	i40e_vsi_enable_irq(vsi);
7348 
7349 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7350 	    (vsi->netdev)) {
7351 		i40e_print_link_message(vsi, true);
7352 		netif_tx_start_all_queues(vsi->netdev);
7353 		netif_carrier_on(vsi->netdev);
7354 	}
7355 
7356 	/* replay FDIR SB filters */
7357 	if (vsi->type == I40E_VSI_FDIR) {
7358 		/* reset fd counters */
7359 		pf->fd_add_err = 0;
7360 		pf->fd_atr_cnt = 0;
7361 		i40e_fdir_filter_restore(vsi);
7362 	}
7363 
7364 	/* On the next run of the service_task, notify any clients of the new
7365 	 * opened netdev
7366 	 */
7367 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7368 	i40e_service_event_schedule(pf);
7369 
7370 	return 0;
7371 }
7372 
7373 /**
7374  * i40e_vsi_reinit_locked - Reset the VSI
7375  * @vsi: the VSI being configured
7376  *
7377  * Rebuild the ring structs after some configuration
7378  * has changed, e.g. MTU size.
7379  **/
7380 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7381 {
7382 	struct i40e_pf *pf = vsi->back;
7383 
7384 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7385 		usleep_range(1000, 2000);
7386 	i40e_down(vsi);
7387 
7388 	i40e_up(vsi);
7389 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7390 }
7391 
7392 /**
7393  * i40e_force_link_state - Force the link status
7394  * @pf: board private structure
7395  * @is_up: whether the link state should be forced up or down
7396  **/
7397 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7398 {
7399 	struct i40e_aq_get_phy_abilities_resp abilities;
7400 	struct i40e_aq_set_phy_config config = {0};
7401 	bool non_zero_phy_type = is_up;
7402 	struct i40e_hw *hw = &pf->hw;
7403 	i40e_status err;
7404 	u64 mask;
7405 	u8 speed;
7406 
7407 	/* Card might've been put in an unstable state by other drivers
7408 	 * and applications, which causes incorrect speed values being
7409 	 * set on startup. In order to clear speed registers, we call
7410 	 * get_phy_capabilities twice, once to get initial state of
7411 	 * available speeds, and once to get current PHY config.
7412 	 */
7413 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7414 					   NULL);
7415 	if (err) {
7416 		dev_err(&pf->pdev->dev,
7417 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7418 			i40e_stat_str(hw, err),
7419 			i40e_aq_str(hw, hw->aq.asq_last_status));
7420 		return err;
7421 	}
7422 	speed = abilities.link_speed;
7423 
7424 	/* Get the current phy config */
7425 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7426 					   NULL);
7427 	if (err) {
7428 		dev_err(&pf->pdev->dev,
7429 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7430 			i40e_stat_str(hw, err),
7431 			i40e_aq_str(hw, hw->aq.asq_last_status));
7432 		return err;
7433 	}
7434 
7435 	/* If link needs to go up, but was not forced to go down,
7436 	 * and its speed values are OK, no need for a flap
7437 	 * if non_zero_phy_type was set, still need to force up
7438 	 */
7439 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7440 		non_zero_phy_type = true;
7441 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7442 		return I40E_SUCCESS;
7443 
7444 	/* To force link we need to set bits for all supported PHY types,
7445 	 * but there are now more than 32, so we need to split the bitmap
7446 	 * across two fields.
7447 	 */
7448 	mask = I40E_PHY_TYPES_BITMASK;
7449 	config.phy_type =
7450 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7451 	config.phy_type_ext =
7452 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7453 	/* Copy the old settings, except of phy_type */
7454 	config.abilities = abilities.abilities;
7455 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7456 		if (is_up)
7457 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7458 		else
7459 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7460 	}
7461 	if (abilities.link_speed != 0)
7462 		config.link_speed = abilities.link_speed;
7463 	else
7464 		config.link_speed = speed;
7465 	config.eee_capability = abilities.eee_capability;
7466 	config.eeer = abilities.eeer_val;
7467 	config.low_power_ctrl = abilities.d3_lpan;
7468 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7469 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7470 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7471 
7472 	if (err) {
7473 		dev_err(&pf->pdev->dev,
7474 			"set phy config ret =  %s last_status =  %s\n",
7475 			i40e_stat_str(&pf->hw, err),
7476 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7477 		return err;
7478 	}
7479 
7480 	/* Update the link info */
7481 	err = i40e_update_link_info(hw);
7482 	if (err) {
7483 		/* Wait a little bit (on 40G cards it sometimes takes a really
7484 		 * long time for link to come back from the atomic reset)
7485 		 * and try once more
7486 		 */
7487 		msleep(1000);
7488 		i40e_update_link_info(hw);
7489 	}
7490 
7491 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7492 
7493 	return I40E_SUCCESS;
7494 }
7495 
7496 /**
7497  * i40e_up - Bring the connection back up after being down
7498  * @vsi: the VSI being configured
7499  **/
7500 int i40e_up(struct i40e_vsi *vsi)
7501 {
7502 	int err;
7503 
7504 	if (vsi->type == I40E_VSI_MAIN &&
7505 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7506 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7507 		i40e_force_link_state(vsi->back, true);
7508 
7509 	err = i40e_vsi_configure(vsi);
7510 	if (!err)
7511 		err = i40e_up_complete(vsi);
7512 
7513 	return err;
7514 }
7515 
7516 /**
7517  * i40e_down - Shutdown the connection processing
7518  * @vsi: the VSI being stopped
7519  **/
7520 void i40e_down(struct i40e_vsi *vsi)
7521 {
7522 	int i;
7523 
7524 	/* It is assumed that the caller of this function
7525 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7526 	 */
7527 	if (vsi->netdev) {
7528 		netif_carrier_off(vsi->netdev);
7529 		netif_tx_disable(vsi->netdev);
7530 	}
7531 	i40e_vsi_disable_irq(vsi);
7532 	i40e_vsi_stop_rings(vsi);
7533 	if (vsi->type == I40E_VSI_MAIN &&
7534 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7535 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7536 		i40e_force_link_state(vsi->back, false);
7537 	i40e_napi_disable_all(vsi);
7538 
7539 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7540 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7541 		if (i40e_enabled_xdp_vsi(vsi)) {
7542 			/* Make sure that in-progress ndo_xdp_xmit and
7543 			 * ndo_xsk_wakeup calls are completed.
7544 			 */
7545 			synchronize_rcu();
7546 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7547 		}
7548 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7549 	}
7550 
7551 }
7552 
7553 /**
7554  * i40e_validate_mqprio_qopt- validate queue mapping info
7555  * @vsi: the VSI being configured
7556  * @mqprio_qopt: queue parametrs
7557  **/
7558 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7559 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7560 {
7561 	u64 sum_max_rate = 0;
7562 	u64 max_rate = 0;
7563 	int i;
7564 
7565 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7566 	    mqprio_qopt->qopt.num_tc < 1 ||
7567 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7568 		return -EINVAL;
7569 	for (i = 0; ; i++) {
7570 		if (!mqprio_qopt->qopt.count[i])
7571 			return -EINVAL;
7572 		if (mqprio_qopt->min_rate[i]) {
7573 			dev_err(&vsi->back->pdev->dev,
7574 				"Invalid min tx rate (greater than 0) specified\n");
7575 			return -EINVAL;
7576 		}
7577 		max_rate = mqprio_qopt->max_rate[i];
7578 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7579 		sum_max_rate += max_rate;
7580 
7581 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7582 			break;
7583 		if (mqprio_qopt->qopt.offset[i + 1] !=
7584 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7585 			return -EINVAL;
7586 	}
7587 	if (vsi->num_queue_pairs <
7588 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7589 		dev_err(&vsi->back->pdev->dev,
7590 			"Failed to create traffic channel, insufficient number of queues.\n");
7591 		return -EINVAL;
7592 	}
7593 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7594 		dev_err(&vsi->back->pdev->dev,
7595 			"Invalid max tx rate specified\n");
7596 		return -EINVAL;
7597 	}
7598 	return 0;
7599 }
7600 
7601 /**
7602  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7603  * @vsi: the VSI being configured
7604  **/
7605 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7606 {
7607 	u16 qcount;
7608 	int i;
7609 
7610 	/* Only TC0 is enabled */
7611 	vsi->tc_config.numtc = 1;
7612 	vsi->tc_config.enabled_tc = 1;
7613 	qcount = min_t(int, vsi->alloc_queue_pairs,
7614 		       i40e_pf_get_max_q_per_tc(vsi->back));
7615 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7616 		/* For the TC that is not enabled set the offset to default
7617 		 * queue and allocate one queue for the given TC.
7618 		 */
7619 		vsi->tc_config.tc_info[i].qoffset = 0;
7620 		if (i == 0)
7621 			vsi->tc_config.tc_info[i].qcount = qcount;
7622 		else
7623 			vsi->tc_config.tc_info[i].qcount = 1;
7624 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7625 	}
7626 }
7627 
7628 /**
7629  * i40e_del_macvlan_filter
7630  * @hw: pointer to the HW structure
7631  * @seid: seid of the channel VSI
7632  * @macaddr: the mac address to apply as a filter
7633  * @aq_err: store the admin Q error
7634  *
7635  * This function deletes a mac filter on the channel VSI which serves as the
7636  * macvlan. Returns 0 on success.
7637  **/
7638 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7639 					   const u8 *macaddr, int *aq_err)
7640 {
7641 	struct i40e_aqc_remove_macvlan_element_data element;
7642 	i40e_status status;
7643 
7644 	memset(&element, 0, sizeof(element));
7645 	ether_addr_copy(element.mac_addr, macaddr);
7646 	element.vlan_tag = 0;
7647 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7648 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7649 	*aq_err = hw->aq.asq_last_status;
7650 
7651 	return status;
7652 }
7653 
7654 /**
7655  * i40e_add_macvlan_filter
7656  * @hw: pointer to the HW structure
7657  * @seid: seid of the channel VSI
7658  * @macaddr: the mac address to apply as a filter
7659  * @aq_err: store the admin Q error
7660  *
7661  * This function adds a mac filter on the channel VSI which serves as the
7662  * macvlan. Returns 0 on success.
7663  **/
7664 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7665 					   const u8 *macaddr, int *aq_err)
7666 {
7667 	struct i40e_aqc_add_macvlan_element_data element;
7668 	i40e_status status;
7669 	u16 cmd_flags = 0;
7670 
7671 	ether_addr_copy(element.mac_addr, macaddr);
7672 	element.vlan_tag = 0;
7673 	element.queue_number = 0;
7674 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7675 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7676 	element.flags = cpu_to_le16(cmd_flags);
7677 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7678 	*aq_err = hw->aq.asq_last_status;
7679 
7680 	return status;
7681 }
7682 
7683 /**
7684  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7685  * @vsi: the VSI we want to access
7686  * @ch: the channel we want to access
7687  */
7688 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7689 {
7690 	struct i40e_ring *tx_ring, *rx_ring;
7691 	u16 pf_q;
7692 	int i;
7693 
7694 	for (i = 0; i < ch->num_queue_pairs; i++) {
7695 		pf_q = ch->base_queue + i;
7696 		tx_ring = vsi->tx_rings[pf_q];
7697 		tx_ring->ch = NULL;
7698 		rx_ring = vsi->rx_rings[pf_q];
7699 		rx_ring->ch = NULL;
7700 	}
7701 }
7702 
7703 /**
7704  * i40e_free_macvlan_channels
7705  * @vsi: the VSI we want to access
7706  *
7707  * This function frees the Qs of the channel VSI from
7708  * the stack and also deletes the channel VSIs which
7709  * serve as macvlans.
7710  */
7711 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7712 {
7713 	struct i40e_channel *ch, *ch_tmp;
7714 	int ret;
7715 
7716 	if (list_empty(&vsi->macvlan_list))
7717 		return;
7718 
7719 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7720 		struct i40e_vsi *parent_vsi;
7721 
7722 		if (i40e_is_channel_macvlan(ch)) {
7723 			i40e_reset_ch_rings(vsi, ch);
7724 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7725 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7726 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7727 			kfree(ch->fwd);
7728 			ch->fwd = NULL;
7729 		}
7730 
7731 		list_del(&ch->list);
7732 		parent_vsi = ch->parent_vsi;
7733 		if (!parent_vsi || !ch->initialized) {
7734 			kfree(ch);
7735 			continue;
7736 		}
7737 
7738 		/* remove the VSI */
7739 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7740 					     NULL);
7741 		if (ret)
7742 			dev_err(&vsi->back->pdev->dev,
7743 				"unable to remove channel (%d) for parent VSI(%d)\n",
7744 				ch->seid, parent_vsi->seid);
7745 		kfree(ch);
7746 	}
7747 	vsi->macvlan_cnt = 0;
7748 }
7749 
7750 /**
7751  * i40e_fwd_ring_up - bring the macvlan device up
7752  * @vsi: the VSI we want to access
7753  * @vdev: macvlan netdevice
7754  * @fwd: the private fwd structure
7755  */
7756 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7757 			    struct i40e_fwd_adapter *fwd)
7758 {
7759 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7760 	int ret = 0, num_tc = 1,  i, aq_err;
7761 	struct i40e_pf *pf = vsi->back;
7762 	struct i40e_hw *hw = &pf->hw;
7763 
7764 	/* Go through the list and find an available channel */
7765 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7766 		if (!i40e_is_channel_macvlan(iter)) {
7767 			iter->fwd = fwd;
7768 			/* record configuration for macvlan interface in vdev */
7769 			for (i = 0; i < num_tc; i++)
7770 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7771 							     i,
7772 							     iter->num_queue_pairs,
7773 							     iter->base_queue);
7774 			for (i = 0; i < iter->num_queue_pairs; i++) {
7775 				struct i40e_ring *tx_ring, *rx_ring;
7776 				u16 pf_q;
7777 
7778 				pf_q = iter->base_queue + i;
7779 
7780 				/* Get to TX ring ptr */
7781 				tx_ring = vsi->tx_rings[pf_q];
7782 				tx_ring->ch = iter;
7783 
7784 				/* Get the RX ring ptr */
7785 				rx_ring = vsi->rx_rings[pf_q];
7786 				rx_ring->ch = iter;
7787 			}
7788 			ch = iter;
7789 			break;
7790 		}
7791 	}
7792 
7793 	if (!ch)
7794 		return -EINVAL;
7795 
7796 	/* Guarantee all rings are updated before we update the
7797 	 * MAC address filter.
7798 	 */
7799 	wmb();
7800 
7801 	/* Add a mac filter */
7802 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7803 	if (ret) {
7804 		/* if we cannot add the MAC rule then disable the offload */
7805 		macvlan_release_l2fw_offload(vdev);
7806 		for (i = 0; i < ch->num_queue_pairs; i++) {
7807 			struct i40e_ring *rx_ring;
7808 			u16 pf_q;
7809 
7810 			pf_q = ch->base_queue + i;
7811 			rx_ring = vsi->rx_rings[pf_q];
7812 			rx_ring->netdev = NULL;
7813 		}
7814 		dev_info(&pf->pdev->dev,
7815 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7816 			  i40e_stat_str(hw, ret),
7817 			  i40e_aq_str(hw, aq_err));
7818 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7819 	}
7820 
7821 	return ret;
7822 }
7823 
7824 /**
7825  * i40e_setup_macvlans - create the channels which will be macvlans
7826  * @vsi: the VSI we want to access
7827  * @macvlan_cnt: no. of macvlans to be setup
7828  * @qcnt: no. of Qs per macvlan
7829  * @vdev: macvlan netdevice
7830  */
7831 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7832 			       struct net_device *vdev)
7833 {
7834 	struct i40e_pf *pf = vsi->back;
7835 	struct i40e_hw *hw = &pf->hw;
7836 	struct i40e_vsi_context ctxt;
7837 	u16 sections, qmap, num_qps;
7838 	struct i40e_channel *ch;
7839 	int i, pow, ret = 0;
7840 	u8 offset = 0;
7841 
7842 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7843 		return -EINVAL;
7844 
7845 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7846 
7847 	/* find the next higher power-of-2 of num queue pairs */
7848 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7849 
7850 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7851 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7852 
7853 	/* Setup context bits for the main VSI */
7854 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7855 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7856 	memset(&ctxt, 0, sizeof(ctxt));
7857 	ctxt.seid = vsi->seid;
7858 	ctxt.pf_num = vsi->back->hw.pf_id;
7859 	ctxt.vf_num = 0;
7860 	ctxt.uplink_seid = vsi->uplink_seid;
7861 	ctxt.info = vsi->info;
7862 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7863 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7864 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7865 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7866 
7867 	/* Reconfigure RSS for main VSI with new max queue count */
7868 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7869 	ret = i40e_vsi_config_rss(vsi);
7870 	if (ret) {
7871 		dev_info(&pf->pdev->dev,
7872 			 "Failed to reconfig RSS for num_queues (%u)\n",
7873 			 vsi->rss_size);
7874 		return ret;
7875 	}
7876 	vsi->reconfig_rss = true;
7877 	dev_dbg(&vsi->back->pdev->dev,
7878 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7879 	vsi->next_base_queue = num_qps;
7880 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7881 
7882 	/* Update the VSI after updating the VSI queue-mapping
7883 	 * information
7884 	 */
7885 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7886 	if (ret) {
7887 		dev_info(&pf->pdev->dev,
7888 			 "Update vsi tc config failed, err %s aq_err %s\n",
7889 			 i40e_stat_str(hw, ret),
7890 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7891 		return ret;
7892 	}
7893 	/* update the local VSI info with updated queue map */
7894 	i40e_vsi_update_queue_map(vsi, &ctxt);
7895 	vsi->info.valid_sections = 0;
7896 
7897 	/* Create channels for macvlans */
7898 	INIT_LIST_HEAD(&vsi->macvlan_list);
7899 	for (i = 0; i < macvlan_cnt; i++) {
7900 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7901 		if (!ch) {
7902 			ret = -ENOMEM;
7903 			goto err_free;
7904 		}
7905 		INIT_LIST_HEAD(&ch->list);
7906 		ch->num_queue_pairs = qcnt;
7907 		if (!i40e_setup_channel(pf, vsi, ch)) {
7908 			ret = -EINVAL;
7909 			kfree(ch);
7910 			goto err_free;
7911 		}
7912 		ch->parent_vsi = vsi;
7913 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7914 		vsi->macvlan_cnt++;
7915 		list_add_tail(&ch->list, &vsi->macvlan_list);
7916 	}
7917 
7918 	return ret;
7919 
7920 err_free:
7921 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7922 	i40e_free_macvlan_channels(vsi);
7923 
7924 	return ret;
7925 }
7926 
7927 /**
7928  * i40e_fwd_add - configure macvlans
7929  * @netdev: net device to configure
7930  * @vdev: macvlan netdevice
7931  **/
7932 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7933 {
7934 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7935 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7936 	struct i40e_vsi *vsi = np->vsi;
7937 	struct i40e_pf *pf = vsi->back;
7938 	struct i40e_fwd_adapter *fwd;
7939 	int avail_macvlan, ret;
7940 
7941 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7942 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7943 		return ERR_PTR(-EINVAL);
7944 	}
7945 	if (i40e_is_tc_mqprio_enabled(pf)) {
7946 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7947 		return ERR_PTR(-EINVAL);
7948 	}
7949 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7950 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7951 		return ERR_PTR(-EINVAL);
7952 	}
7953 
7954 	/* The macvlan device has to be a single Q device so that the
7955 	 * tc_to_txq field can be reused to pick the tx queue.
7956 	 */
7957 	if (netif_is_multiqueue(vdev))
7958 		return ERR_PTR(-ERANGE);
7959 
7960 	if (!vsi->macvlan_cnt) {
7961 		/* reserve bit 0 for the pf device */
7962 		set_bit(0, vsi->fwd_bitmask);
7963 
7964 		/* Try to reserve as many queues as possible for macvlans. First
7965 		 * reserve 3/4th of max vectors, then half, then quarter and
7966 		 * calculate Qs per macvlan as you go
7967 		 */
7968 		vectors = pf->num_lan_msix;
7969 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7970 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7971 			q_per_macvlan = 4;
7972 			macvlan_cnt = (vectors - 32) / 4;
7973 		} else if (vectors <= 64 && vectors > 32) {
7974 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7975 			q_per_macvlan = 2;
7976 			macvlan_cnt = (vectors - 16) / 2;
7977 		} else if (vectors <= 32 && vectors > 16) {
7978 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7979 			q_per_macvlan = 1;
7980 			macvlan_cnt = vectors - 16;
7981 		} else if (vectors <= 16 && vectors > 8) {
7982 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7983 			q_per_macvlan = 1;
7984 			macvlan_cnt = vectors - 8;
7985 		} else {
7986 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7987 			q_per_macvlan = 1;
7988 			macvlan_cnt = vectors - 1;
7989 		}
7990 
7991 		if (macvlan_cnt == 0)
7992 			return ERR_PTR(-EBUSY);
7993 
7994 		/* Quiesce VSI queues */
7995 		i40e_quiesce_vsi(vsi);
7996 
7997 		/* sets up the macvlans but does not "enable" them */
7998 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7999 					  vdev);
8000 		if (ret)
8001 			return ERR_PTR(ret);
8002 
8003 		/* Unquiesce VSI */
8004 		i40e_unquiesce_vsi(vsi);
8005 	}
8006 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8007 					    vsi->macvlan_cnt);
8008 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8009 		return ERR_PTR(-EBUSY);
8010 
8011 	/* create the fwd struct */
8012 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8013 	if (!fwd)
8014 		return ERR_PTR(-ENOMEM);
8015 
8016 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8017 	fwd->bit_no = avail_macvlan;
8018 	netdev_set_sb_channel(vdev, avail_macvlan);
8019 	fwd->netdev = vdev;
8020 
8021 	if (!netif_running(netdev))
8022 		return fwd;
8023 
8024 	/* Set fwd ring up */
8025 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8026 	if (ret) {
8027 		/* unbind the queues and drop the subordinate channel config */
8028 		netdev_unbind_sb_channel(netdev, vdev);
8029 		netdev_set_sb_channel(vdev, 0);
8030 
8031 		kfree(fwd);
8032 		return ERR_PTR(-EINVAL);
8033 	}
8034 
8035 	return fwd;
8036 }
8037 
8038 /**
8039  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8040  * @vsi: the VSI we want to access
8041  */
8042 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8043 {
8044 	struct i40e_channel *ch, *ch_tmp;
8045 	struct i40e_pf *pf = vsi->back;
8046 	struct i40e_hw *hw = &pf->hw;
8047 	int aq_err, ret = 0;
8048 
8049 	if (list_empty(&vsi->macvlan_list))
8050 		return;
8051 
8052 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8053 		if (i40e_is_channel_macvlan(ch)) {
8054 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8055 						      i40e_channel_mac(ch),
8056 						      &aq_err);
8057 			if (!ret) {
8058 				/* Reset queue contexts */
8059 				i40e_reset_ch_rings(vsi, ch);
8060 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8061 				netdev_unbind_sb_channel(vsi->netdev,
8062 							 ch->fwd->netdev);
8063 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8064 				kfree(ch->fwd);
8065 				ch->fwd = NULL;
8066 			}
8067 		}
8068 	}
8069 }
8070 
8071 /**
8072  * i40e_fwd_del - delete macvlan interfaces
8073  * @netdev: net device to configure
8074  * @vdev: macvlan netdevice
8075  */
8076 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8077 {
8078 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8079 	struct i40e_fwd_adapter *fwd = vdev;
8080 	struct i40e_channel *ch, *ch_tmp;
8081 	struct i40e_vsi *vsi = np->vsi;
8082 	struct i40e_pf *pf = vsi->back;
8083 	struct i40e_hw *hw = &pf->hw;
8084 	int aq_err, ret = 0;
8085 
8086 	/* Find the channel associated with the macvlan and del mac filter */
8087 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8088 		if (i40e_is_channel_macvlan(ch) &&
8089 		    ether_addr_equal(i40e_channel_mac(ch),
8090 				     fwd->netdev->dev_addr)) {
8091 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8092 						      i40e_channel_mac(ch),
8093 						      &aq_err);
8094 			if (!ret) {
8095 				/* Reset queue contexts */
8096 				i40e_reset_ch_rings(vsi, ch);
8097 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8098 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8099 				netdev_set_sb_channel(fwd->netdev, 0);
8100 				kfree(ch->fwd);
8101 				ch->fwd = NULL;
8102 			} else {
8103 				dev_info(&pf->pdev->dev,
8104 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8105 					  i40e_stat_str(hw, ret),
8106 					  i40e_aq_str(hw, aq_err));
8107 			}
8108 			break;
8109 		}
8110 	}
8111 }
8112 
8113 /**
8114  * i40e_setup_tc - configure multiple traffic classes
8115  * @netdev: net device to configure
8116  * @type_data: tc offload data
8117  **/
8118 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8119 {
8120 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8121 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8122 	struct i40e_vsi *vsi = np->vsi;
8123 	struct i40e_pf *pf = vsi->back;
8124 	u8 enabled_tc = 0, num_tc, hw;
8125 	bool need_reset = false;
8126 	int old_queue_pairs;
8127 	int ret = -EINVAL;
8128 	u16 mode;
8129 	int i;
8130 
8131 	old_queue_pairs = vsi->num_queue_pairs;
8132 	num_tc = mqprio_qopt->qopt.num_tc;
8133 	hw = mqprio_qopt->qopt.hw;
8134 	mode = mqprio_qopt->mode;
8135 	if (!hw) {
8136 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8137 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8138 		goto config_tc;
8139 	}
8140 
8141 	/* Check if MFP enabled */
8142 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8143 		netdev_info(netdev,
8144 			    "Configuring TC not supported in MFP mode\n");
8145 		return ret;
8146 	}
8147 	switch (mode) {
8148 	case TC_MQPRIO_MODE_DCB:
8149 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8150 
8151 		/* Check if DCB enabled to continue */
8152 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8153 			netdev_info(netdev,
8154 				    "DCB is not enabled for adapter\n");
8155 			return ret;
8156 		}
8157 
8158 		/* Check whether tc count is within enabled limit */
8159 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8160 			netdev_info(netdev,
8161 				    "TC count greater than enabled on link for adapter\n");
8162 			return ret;
8163 		}
8164 		break;
8165 	case TC_MQPRIO_MODE_CHANNEL:
8166 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8167 			netdev_info(netdev,
8168 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8169 			return ret;
8170 		}
8171 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8172 			return ret;
8173 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8174 		if (ret)
8175 			return ret;
8176 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8177 		       sizeof(*mqprio_qopt));
8178 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8179 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8180 		break;
8181 	default:
8182 		return -EINVAL;
8183 	}
8184 
8185 config_tc:
8186 	/* Generate TC map for number of tc requested */
8187 	for (i = 0; i < num_tc; i++)
8188 		enabled_tc |= BIT(i);
8189 
8190 	/* Requesting same TC configuration as already enabled */
8191 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8192 	    mode != TC_MQPRIO_MODE_CHANNEL)
8193 		return 0;
8194 
8195 	/* Quiesce VSI queues */
8196 	i40e_quiesce_vsi(vsi);
8197 
8198 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8199 		i40e_remove_queue_channels(vsi);
8200 
8201 	/* Configure VSI for enabled TCs */
8202 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8203 	if (ret) {
8204 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8205 			    vsi->seid);
8206 		need_reset = true;
8207 		goto exit;
8208 	} else if (enabled_tc &&
8209 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8210 		netdev_info(netdev,
8211 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8212 			    vsi->tc_config.tc_info[0].qcount);
8213 		ret = -EINVAL;
8214 		need_reset = true;
8215 		goto exit;
8216 	}
8217 
8218 	dev_info(&vsi->back->pdev->dev,
8219 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8220 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8221 
8222 	if (i40e_is_tc_mqprio_enabled(pf)) {
8223 		if (vsi->mqprio_qopt.max_rate[0]) {
8224 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8225 
8226 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8227 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8228 			if (!ret) {
8229 				u64 credits = max_tx_rate;
8230 
8231 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8232 				dev_dbg(&vsi->back->pdev->dev,
8233 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8234 					max_tx_rate,
8235 					credits,
8236 					vsi->seid);
8237 			} else {
8238 				need_reset = true;
8239 				goto exit;
8240 			}
8241 		}
8242 		ret = i40e_configure_queue_channels(vsi);
8243 		if (ret) {
8244 			vsi->num_queue_pairs = old_queue_pairs;
8245 			netdev_info(netdev,
8246 				    "Failed configuring queue channels\n");
8247 			need_reset = true;
8248 			goto exit;
8249 		}
8250 	}
8251 
8252 exit:
8253 	/* Reset the configuration data to defaults, only TC0 is enabled */
8254 	if (need_reset) {
8255 		i40e_vsi_set_default_tc_config(vsi);
8256 		need_reset = false;
8257 	}
8258 
8259 	/* Unquiesce VSI */
8260 	i40e_unquiesce_vsi(vsi);
8261 	return ret;
8262 }
8263 
8264 /**
8265  * i40e_set_cld_element - sets cloud filter element data
8266  * @filter: cloud filter rule
8267  * @cld: ptr to cloud filter element data
8268  *
8269  * This is helper function to copy data into cloud filter element
8270  **/
8271 static inline void
8272 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8273 		     struct i40e_aqc_cloud_filters_element_data *cld)
8274 {
8275 	u32 ipa;
8276 	int i;
8277 
8278 	memset(cld, 0, sizeof(*cld));
8279 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8280 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8281 
8282 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8283 		return;
8284 
8285 	if (filter->n_proto == ETH_P_IPV6) {
8286 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8287 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8288 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8289 
8290 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8291 		}
8292 	} else {
8293 		ipa = be32_to_cpu(filter->dst_ipv4);
8294 
8295 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8296 	}
8297 
8298 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8299 
8300 	/* tenant_id is not supported by FW now, once the support is enabled
8301 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8302 	 */
8303 	if (filter->tenant_id)
8304 		return;
8305 }
8306 
8307 /**
8308  * i40e_add_del_cloud_filter - Add/del cloud filter
8309  * @vsi: pointer to VSI
8310  * @filter: cloud filter rule
8311  * @add: if true, add, if false, delete
8312  *
8313  * Add or delete a cloud filter for a specific flow spec.
8314  * Returns 0 if the filter were successfully added.
8315  **/
8316 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8317 			      struct i40e_cloud_filter *filter, bool add)
8318 {
8319 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8320 	struct i40e_pf *pf = vsi->back;
8321 	int ret;
8322 	static const u16 flag_table[128] = {
8323 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8324 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8325 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8326 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8327 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8328 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8329 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8330 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8331 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8332 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8333 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8334 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8335 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8336 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8337 	};
8338 
8339 	if (filter->flags >= ARRAY_SIZE(flag_table))
8340 		return I40E_ERR_CONFIG;
8341 
8342 	memset(&cld_filter, 0, sizeof(cld_filter));
8343 
8344 	/* copy element needed to add cloud filter from filter */
8345 	i40e_set_cld_element(filter, &cld_filter);
8346 
8347 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8348 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8349 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8350 
8351 	if (filter->n_proto == ETH_P_IPV6)
8352 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8353 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8354 	else
8355 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8356 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8357 
8358 	if (add)
8359 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8360 						&cld_filter, 1);
8361 	else
8362 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8363 						&cld_filter, 1);
8364 	if (ret)
8365 		dev_dbg(&pf->pdev->dev,
8366 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8367 			add ? "add" : "delete", filter->dst_port, ret,
8368 			pf->hw.aq.asq_last_status);
8369 	else
8370 		dev_info(&pf->pdev->dev,
8371 			 "%s cloud filter for VSI: %d\n",
8372 			 add ? "Added" : "Deleted", filter->seid);
8373 	return ret;
8374 }
8375 
8376 /**
8377  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8378  * @vsi: pointer to VSI
8379  * @filter: cloud filter rule
8380  * @add: if true, add, if false, delete
8381  *
8382  * Add or delete a cloud filter for a specific flow spec using big buffer.
8383  * Returns 0 if the filter were successfully added.
8384  **/
8385 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8386 				      struct i40e_cloud_filter *filter,
8387 				      bool add)
8388 {
8389 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8390 	struct i40e_pf *pf = vsi->back;
8391 	int ret;
8392 
8393 	/* Both (src/dst) valid mac_addr are not supported */
8394 	if ((is_valid_ether_addr(filter->dst_mac) &&
8395 	     is_valid_ether_addr(filter->src_mac)) ||
8396 	    (is_multicast_ether_addr(filter->dst_mac) &&
8397 	     is_multicast_ether_addr(filter->src_mac)))
8398 		return -EOPNOTSUPP;
8399 
8400 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8401 	 * ports are not supported via big buffer now.
8402 	 */
8403 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8404 		return -EOPNOTSUPP;
8405 
8406 	/* adding filter using src_port/src_ip is not supported at this stage */
8407 	if (filter->src_port ||
8408 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8409 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8410 		return -EOPNOTSUPP;
8411 
8412 	memset(&cld_filter, 0, sizeof(cld_filter));
8413 
8414 	/* copy element needed to add cloud filter from filter */
8415 	i40e_set_cld_element(filter, &cld_filter.element);
8416 
8417 	if (is_valid_ether_addr(filter->dst_mac) ||
8418 	    is_valid_ether_addr(filter->src_mac) ||
8419 	    is_multicast_ether_addr(filter->dst_mac) ||
8420 	    is_multicast_ether_addr(filter->src_mac)) {
8421 		/* MAC + IP : unsupported mode */
8422 		if (filter->dst_ipv4)
8423 			return -EOPNOTSUPP;
8424 
8425 		/* since we validated that L4 port must be valid before
8426 		 * we get here, start with respective "flags" value
8427 		 * and update if vlan is present or not
8428 		 */
8429 		cld_filter.element.flags =
8430 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8431 
8432 		if (filter->vlan_id) {
8433 			cld_filter.element.flags =
8434 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8435 		}
8436 
8437 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8438 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8439 		cld_filter.element.flags =
8440 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8441 		if (filter->n_proto == ETH_P_IPV6)
8442 			cld_filter.element.flags |=
8443 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8444 		else
8445 			cld_filter.element.flags |=
8446 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8447 	} else {
8448 		dev_err(&pf->pdev->dev,
8449 			"either mac or ip has to be valid for cloud filter\n");
8450 		return -EINVAL;
8451 	}
8452 
8453 	/* Now copy L4 port in Byte 6..7 in general fields */
8454 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8455 						be16_to_cpu(filter->dst_port);
8456 
8457 	if (add) {
8458 		/* Validate current device switch mode, change if necessary */
8459 		ret = i40e_validate_and_set_switch_mode(vsi);
8460 		if (ret) {
8461 			dev_err(&pf->pdev->dev,
8462 				"failed to set switch mode, ret %d\n",
8463 				ret);
8464 			return ret;
8465 		}
8466 
8467 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8468 						   &cld_filter, 1);
8469 	} else {
8470 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8471 						   &cld_filter, 1);
8472 	}
8473 
8474 	if (ret)
8475 		dev_dbg(&pf->pdev->dev,
8476 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8477 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8478 	else
8479 		dev_info(&pf->pdev->dev,
8480 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8481 			 add ? "add" : "delete", filter->seid,
8482 			 ntohs(filter->dst_port));
8483 	return ret;
8484 }
8485 
8486 /**
8487  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8488  * @vsi: Pointer to VSI
8489  * @f: Pointer to struct flow_cls_offload
8490  * @filter: Pointer to cloud filter structure
8491  *
8492  **/
8493 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8494 				 struct flow_cls_offload *f,
8495 				 struct i40e_cloud_filter *filter)
8496 {
8497 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8498 	struct flow_dissector *dissector = rule->match.dissector;
8499 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8500 	struct i40e_pf *pf = vsi->back;
8501 	u8 field_flags = 0;
8502 
8503 	if (dissector->used_keys &
8504 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8505 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8506 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8507 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8508 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8509 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8510 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8511 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8512 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8513 			dissector->used_keys);
8514 		return -EOPNOTSUPP;
8515 	}
8516 
8517 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8518 		struct flow_match_enc_keyid match;
8519 
8520 		flow_rule_match_enc_keyid(rule, &match);
8521 		if (match.mask->keyid != 0)
8522 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8523 
8524 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8525 	}
8526 
8527 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8528 		struct flow_match_basic match;
8529 
8530 		flow_rule_match_basic(rule, &match);
8531 		n_proto_key = ntohs(match.key->n_proto);
8532 		n_proto_mask = ntohs(match.mask->n_proto);
8533 
8534 		if (n_proto_key == ETH_P_ALL) {
8535 			n_proto_key = 0;
8536 			n_proto_mask = 0;
8537 		}
8538 		filter->n_proto = n_proto_key & n_proto_mask;
8539 		filter->ip_proto = match.key->ip_proto;
8540 	}
8541 
8542 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8543 		struct flow_match_eth_addrs match;
8544 
8545 		flow_rule_match_eth_addrs(rule, &match);
8546 
8547 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8548 		if (!is_zero_ether_addr(match.mask->dst)) {
8549 			if (is_broadcast_ether_addr(match.mask->dst)) {
8550 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8551 			} else {
8552 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8553 					match.mask->dst);
8554 				return I40E_ERR_CONFIG;
8555 			}
8556 		}
8557 
8558 		if (!is_zero_ether_addr(match.mask->src)) {
8559 			if (is_broadcast_ether_addr(match.mask->src)) {
8560 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8561 			} else {
8562 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8563 					match.mask->src);
8564 				return I40E_ERR_CONFIG;
8565 			}
8566 		}
8567 		ether_addr_copy(filter->dst_mac, match.key->dst);
8568 		ether_addr_copy(filter->src_mac, match.key->src);
8569 	}
8570 
8571 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8572 		struct flow_match_vlan match;
8573 
8574 		flow_rule_match_vlan(rule, &match);
8575 		if (match.mask->vlan_id) {
8576 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8577 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8578 
8579 			} else {
8580 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8581 					match.mask->vlan_id);
8582 				return I40E_ERR_CONFIG;
8583 			}
8584 		}
8585 
8586 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8587 	}
8588 
8589 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8590 		struct flow_match_control match;
8591 
8592 		flow_rule_match_control(rule, &match);
8593 		addr_type = match.key->addr_type;
8594 	}
8595 
8596 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8597 		struct flow_match_ipv4_addrs match;
8598 
8599 		flow_rule_match_ipv4_addrs(rule, &match);
8600 		if (match.mask->dst) {
8601 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8602 				field_flags |= I40E_CLOUD_FIELD_IIP;
8603 			} else {
8604 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8605 					&match.mask->dst);
8606 				return I40E_ERR_CONFIG;
8607 			}
8608 		}
8609 
8610 		if (match.mask->src) {
8611 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8612 				field_flags |= I40E_CLOUD_FIELD_IIP;
8613 			} else {
8614 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8615 					&match.mask->src);
8616 				return I40E_ERR_CONFIG;
8617 			}
8618 		}
8619 
8620 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8621 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8622 			return I40E_ERR_CONFIG;
8623 		}
8624 		filter->dst_ipv4 = match.key->dst;
8625 		filter->src_ipv4 = match.key->src;
8626 	}
8627 
8628 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8629 		struct flow_match_ipv6_addrs match;
8630 
8631 		flow_rule_match_ipv6_addrs(rule, &match);
8632 
8633 		/* src and dest IPV6 address should not be LOOPBACK
8634 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8635 		 */
8636 		if (ipv6_addr_loopback(&match.key->dst) ||
8637 		    ipv6_addr_loopback(&match.key->src)) {
8638 			dev_err(&pf->pdev->dev,
8639 				"Bad ipv6, addr is LOOPBACK\n");
8640 			return I40E_ERR_CONFIG;
8641 		}
8642 		if (!ipv6_addr_any(&match.mask->dst) ||
8643 		    !ipv6_addr_any(&match.mask->src))
8644 			field_flags |= I40E_CLOUD_FIELD_IIP;
8645 
8646 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8647 		       sizeof(filter->src_ipv6));
8648 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8649 		       sizeof(filter->dst_ipv6));
8650 	}
8651 
8652 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8653 		struct flow_match_ports match;
8654 
8655 		flow_rule_match_ports(rule, &match);
8656 		if (match.mask->src) {
8657 			if (match.mask->src == cpu_to_be16(0xffff)) {
8658 				field_flags |= I40E_CLOUD_FIELD_IIP;
8659 			} else {
8660 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8661 					be16_to_cpu(match.mask->src));
8662 				return I40E_ERR_CONFIG;
8663 			}
8664 		}
8665 
8666 		if (match.mask->dst) {
8667 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8668 				field_flags |= I40E_CLOUD_FIELD_IIP;
8669 			} else {
8670 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8671 					be16_to_cpu(match.mask->dst));
8672 				return I40E_ERR_CONFIG;
8673 			}
8674 		}
8675 
8676 		filter->dst_port = match.key->dst;
8677 		filter->src_port = match.key->src;
8678 
8679 		switch (filter->ip_proto) {
8680 		case IPPROTO_TCP:
8681 		case IPPROTO_UDP:
8682 			break;
8683 		default:
8684 			dev_err(&pf->pdev->dev,
8685 				"Only UDP and TCP transport are supported\n");
8686 			return -EINVAL;
8687 		}
8688 	}
8689 	filter->flags = field_flags;
8690 	return 0;
8691 }
8692 
8693 /**
8694  * i40e_handle_tclass: Forward to a traffic class on the device
8695  * @vsi: Pointer to VSI
8696  * @tc: traffic class index on the device
8697  * @filter: Pointer to cloud filter structure
8698  *
8699  **/
8700 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8701 			      struct i40e_cloud_filter *filter)
8702 {
8703 	struct i40e_channel *ch, *ch_tmp;
8704 
8705 	/* direct to a traffic class on the same device */
8706 	if (tc == 0) {
8707 		filter->seid = vsi->seid;
8708 		return 0;
8709 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8710 		if (!filter->dst_port) {
8711 			dev_err(&vsi->back->pdev->dev,
8712 				"Specify destination port to direct to traffic class that is not default\n");
8713 			return -EINVAL;
8714 		}
8715 		if (list_empty(&vsi->ch_list))
8716 			return -EINVAL;
8717 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8718 					 list) {
8719 			if (ch->seid == vsi->tc_seid_map[tc])
8720 				filter->seid = ch->seid;
8721 		}
8722 		return 0;
8723 	}
8724 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8725 	return -EINVAL;
8726 }
8727 
8728 /**
8729  * i40e_configure_clsflower - Configure tc flower filters
8730  * @vsi: Pointer to VSI
8731  * @cls_flower: Pointer to struct flow_cls_offload
8732  *
8733  **/
8734 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8735 				    struct flow_cls_offload *cls_flower)
8736 {
8737 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8738 	struct i40e_cloud_filter *filter = NULL;
8739 	struct i40e_pf *pf = vsi->back;
8740 	int err = 0;
8741 
8742 	if (tc < 0) {
8743 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8744 		return -EOPNOTSUPP;
8745 	}
8746 
8747 	if (!tc) {
8748 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8749 		return -EINVAL;
8750 	}
8751 
8752 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8753 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8754 		return -EBUSY;
8755 
8756 	if (pf->fdir_pf_active_filters ||
8757 	    (!hlist_empty(&pf->fdir_filter_list))) {
8758 		dev_err(&vsi->back->pdev->dev,
8759 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8760 		return -EINVAL;
8761 	}
8762 
8763 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8764 		dev_err(&vsi->back->pdev->dev,
8765 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8766 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8767 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8768 	}
8769 
8770 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8771 	if (!filter)
8772 		return -ENOMEM;
8773 
8774 	filter->cookie = cls_flower->cookie;
8775 
8776 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8777 	if (err < 0)
8778 		goto err;
8779 
8780 	err = i40e_handle_tclass(vsi, tc, filter);
8781 	if (err < 0)
8782 		goto err;
8783 
8784 	/* Add cloud filter */
8785 	if (filter->dst_port)
8786 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8787 	else
8788 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8789 
8790 	if (err) {
8791 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8792 			err);
8793 		goto err;
8794 	}
8795 
8796 	/* add filter to the ordered list */
8797 	INIT_HLIST_NODE(&filter->cloud_node);
8798 
8799 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8800 
8801 	pf->num_cloud_filters++;
8802 
8803 	return err;
8804 err:
8805 	kfree(filter);
8806 	return err;
8807 }
8808 
8809 /**
8810  * i40e_find_cloud_filter - Find the could filter in the list
8811  * @vsi: Pointer to VSI
8812  * @cookie: filter specific cookie
8813  *
8814  **/
8815 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8816 							unsigned long *cookie)
8817 {
8818 	struct i40e_cloud_filter *filter = NULL;
8819 	struct hlist_node *node2;
8820 
8821 	hlist_for_each_entry_safe(filter, node2,
8822 				  &vsi->back->cloud_filter_list, cloud_node)
8823 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8824 			return filter;
8825 	return NULL;
8826 }
8827 
8828 /**
8829  * i40e_delete_clsflower - Remove tc flower filters
8830  * @vsi: Pointer to VSI
8831  * @cls_flower: Pointer to struct flow_cls_offload
8832  *
8833  **/
8834 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8835 				 struct flow_cls_offload *cls_flower)
8836 {
8837 	struct i40e_cloud_filter *filter = NULL;
8838 	struct i40e_pf *pf = vsi->back;
8839 	int err = 0;
8840 
8841 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8842 
8843 	if (!filter)
8844 		return -EINVAL;
8845 
8846 	hash_del(&filter->cloud_node);
8847 
8848 	if (filter->dst_port)
8849 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8850 	else
8851 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8852 
8853 	kfree(filter);
8854 	if (err) {
8855 		dev_err(&pf->pdev->dev,
8856 			"Failed to delete cloud filter, err %s\n",
8857 			i40e_stat_str(&pf->hw, err));
8858 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8859 	}
8860 
8861 	pf->num_cloud_filters--;
8862 	if (!pf->num_cloud_filters)
8863 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8864 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8865 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8866 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8867 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8868 		}
8869 	return 0;
8870 }
8871 
8872 /**
8873  * i40e_setup_tc_cls_flower - flower classifier offloads
8874  * @np: net device to configure
8875  * @cls_flower: offload data
8876  **/
8877 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8878 				    struct flow_cls_offload *cls_flower)
8879 {
8880 	struct i40e_vsi *vsi = np->vsi;
8881 
8882 	switch (cls_flower->command) {
8883 	case FLOW_CLS_REPLACE:
8884 		return i40e_configure_clsflower(vsi, cls_flower);
8885 	case FLOW_CLS_DESTROY:
8886 		return i40e_delete_clsflower(vsi, cls_flower);
8887 	case FLOW_CLS_STATS:
8888 		return -EOPNOTSUPP;
8889 	default:
8890 		return -EOPNOTSUPP;
8891 	}
8892 }
8893 
8894 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8895 				  void *cb_priv)
8896 {
8897 	struct i40e_netdev_priv *np = cb_priv;
8898 
8899 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8900 		return -EOPNOTSUPP;
8901 
8902 	switch (type) {
8903 	case TC_SETUP_CLSFLOWER:
8904 		return i40e_setup_tc_cls_flower(np, type_data);
8905 
8906 	default:
8907 		return -EOPNOTSUPP;
8908 	}
8909 }
8910 
8911 static LIST_HEAD(i40e_block_cb_list);
8912 
8913 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8914 			   void *type_data)
8915 {
8916 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8917 
8918 	switch (type) {
8919 	case TC_SETUP_QDISC_MQPRIO:
8920 		return i40e_setup_tc(netdev, type_data);
8921 	case TC_SETUP_BLOCK:
8922 		return flow_block_cb_setup_simple(type_data,
8923 						  &i40e_block_cb_list,
8924 						  i40e_setup_tc_block_cb,
8925 						  np, np, true);
8926 	default:
8927 		return -EOPNOTSUPP;
8928 	}
8929 }
8930 
8931 /**
8932  * i40e_open - Called when a network interface is made active
8933  * @netdev: network interface device structure
8934  *
8935  * The open entry point is called when a network interface is made
8936  * active by the system (IFF_UP).  At this point all resources needed
8937  * for transmit and receive operations are allocated, the interrupt
8938  * handler is registered with the OS, the netdev watchdog subtask is
8939  * enabled, and the stack is notified that the interface is ready.
8940  *
8941  * Returns 0 on success, negative value on failure
8942  **/
8943 int i40e_open(struct net_device *netdev)
8944 {
8945 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8946 	struct i40e_vsi *vsi = np->vsi;
8947 	struct i40e_pf *pf = vsi->back;
8948 	int err;
8949 
8950 	/* disallow open during test or if eeprom is broken */
8951 	if (test_bit(__I40E_TESTING, pf->state) ||
8952 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8953 		return -EBUSY;
8954 
8955 	netif_carrier_off(netdev);
8956 
8957 	if (i40e_force_link_state(pf, true))
8958 		return -EAGAIN;
8959 
8960 	err = i40e_vsi_open(vsi);
8961 	if (err)
8962 		return err;
8963 
8964 	/* configure global TSO hardware offload settings */
8965 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8966 						       TCP_FLAG_FIN) >> 16);
8967 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8968 						       TCP_FLAG_FIN |
8969 						       TCP_FLAG_CWR) >> 16);
8970 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8971 	udp_tunnel_get_rx_info(netdev);
8972 
8973 	return 0;
8974 }
8975 
8976 /**
8977  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8978  * @vsi: vsi structure
8979  *
8980  * This updates netdev's number of tx/rx queues
8981  *
8982  * Returns status of setting tx/rx queues
8983  **/
8984 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8985 {
8986 	int ret;
8987 
8988 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8989 					   vsi->num_queue_pairs);
8990 	if (ret)
8991 		return ret;
8992 
8993 	return netif_set_real_num_tx_queues(vsi->netdev,
8994 					    vsi->num_queue_pairs);
8995 }
8996 
8997 /**
8998  * i40e_vsi_open -
8999  * @vsi: the VSI to open
9000  *
9001  * Finish initialization of the VSI.
9002  *
9003  * Returns 0 on success, negative value on failure
9004  *
9005  * Note: expects to be called while under rtnl_lock()
9006  **/
9007 int i40e_vsi_open(struct i40e_vsi *vsi)
9008 {
9009 	struct i40e_pf *pf = vsi->back;
9010 	char int_name[I40E_INT_NAME_STR_LEN];
9011 	int err;
9012 
9013 	/* allocate descriptors */
9014 	err = i40e_vsi_setup_tx_resources(vsi);
9015 	if (err)
9016 		goto err_setup_tx;
9017 	err = i40e_vsi_setup_rx_resources(vsi);
9018 	if (err)
9019 		goto err_setup_rx;
9020 
9021 	err = i40e_vsi_configure(vsi);
9022 	if (err)
9023 		goto err_setup_rx;
9024 
9025 	if (vsi->netdev) {
9026 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9027 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9028 		err = i40e_vsi_request_irq(vsi, int_name);
9029 		if (err)
9030 			goto err_setup_rx;
9031 
9032 		/* Notify the stack of the actual queue counts. */
9033 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9034 		if (err)
9035 			goto err_set_queues;
9036 
9037 	} else if (vsi->type == I40E_VSI_FDIR) {
9038 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9039 			 dev_driver_string(&pf->pdev->dev),
9040 			 dev_name(&pf->pdev->dev));
9041 		err = i40e_vsi_request_irq(vsi, int_name);
9042 		if (err)
9043 			goto err_setup_rx;
9044 
9045 	} else {
9046 		err = -EINVAL;
9047 		goto err_setup_rx;
9048 	}
9049 
9050 	err = i40e_up_complete(vsi);
9051 	if (err)
9052 		goto err_up_complete;
9053 
9054 	return 0;
9055 
9056 err_up_complete:
9057 	i40e_down(vsi);
9058 err_set_queues:
9059 	i40e_vsi_free_irq(vsi);
9060 err_setup_rx:
9061 	i40e_vsi_free_rx_resources(vsi);
9062 err_setup_tx:
9063 	i40e_vsi_free_tx_resources(vsi);
9064 	if (vsi == pf->vsi[pf->lan_vsi])
9065 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9066 
9067 	return err;
9068 }
9069 
9070 /**
9071  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9072  * @pf: Pointer to PF
9073  *
9074  * This function destroys the hlist where all the Flow Director
9075  * filters were saved.
9076  **/
9077 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9078 {
9079 	struct i40e_fdir_filter *filter;
9080 	struct i40e_flex_pit *pit_entry, *tmp;
9081 	struct hlist_node *node2;
9082 
9083 	hlist_for_each_entry_safe(filter, node2,
9084 				  &pf->fdir_filter_list, fdir_node) {
9085 		hlist_del(&filter->fdir_node);
9086 		kfree(filter);
9087 	}
9088 
9089 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9090 		list_del(&pit_entry->list);
9091 		kfree(pit_entry);
9092 	}
9093 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9094 
9095 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9096 		list_del(&pit_entry->list);
9097 		kfree(pit_entry);
9098 	}
9099 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9100 
9101 	pf->fdir_pf_active_filters = 0;
9102 	i40e_reset_fdir_filter_cnt(pf);
9103 
9104 	/* Reprogram the default input set for TCP/IPv4 */
9105 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9106 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9107 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9108 
9109 	/* Reprogram the default input set for TCP/IPv6 */
9110 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9111 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9112 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9113 
9114 	/* Reprogram the default input set for UDP/IPv4 */
9115 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9116 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9117 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9118 
9119 	/* Reprogram the default input set for UDP/IPv6 */
9120 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9121 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9122 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9123 
9124 	/* Reprogram the default input set for SCTP/IPv4 */
9125 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9126 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9127 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9128 
9129 	/* Reprogram the default input set for SCTP/IPv6 */
9130 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9131 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9132 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9133 
9134 	/* Reprogram the default input set for Other/IPv4 */
9135 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9136 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9137 
9138 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9139 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9140 
9141 	/* Reprogram the default input set for Other/IPv6 */
9142 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9143 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9144 
9145 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9146 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9147 }
9148 
9149 /**
9150  * i40e_cloud_filter_exit - Cleans up the cloud filters
9151  * @pf: Pointer to PF
9152  *
9153  * This function destroys the hlist where all the cloud filters
9154  * were saved.
9155  **/
9156 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9157 {
9158 	struct i40e_cloud_filter *cfilter;
9159 	struct hlist_node *node;
9160 
9161 	hlist_for_each_entry_safe(cfilter, node,
9162 				  &pf->cloud_filter_list, cloud_node) {
9163 		hlist_del(&cfilter->cloud_node);
9164 		kfree(cfilter);
9165 	}
9166 	pf->num_cloud_filters = 0;
9167 
9168 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9169 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9170 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9171 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9172 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9173 	}
9174 }
9175 
9176 /**
9177  * i40e_close - Disables a network interface
9178  * @netdev: network interface device structure
9179  *
9180  * The close entry point is called when an interface is de-activated
9181  * by the OS.  The hardware is still under the driver's control, but
9182  * this netdev interface is disabled.
9183  *
9184  * Returns 0, this is not allowed to fail
9185  **/
9186 int i40e_close(struct net_device *netdev)
9187 {
9188 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9189 	struct i40e_vsi *vsi = np->vsi;
9190 
9191 	i40e_vsi_close(vsi);
9192 
9193 	return 0;
9194 }
9195 
9196 /**
9197  * i40e_do_reset - Start a PF or Core Reset sequence
9198  * @pf: board private structure
9199  * @reset_flags: which reset is requested
9200  * @lock_acquired: indicates whether or not the lock has been acquired
9201  * before this function was called.
9202  *
9203  * The essential difference in resets is that the PF Reset
9204  * doesn't clear the packet buffers, doesn't reset the PE
9205  * firmware, and doesn't bother the other PFs on the chip.
9206  **/
9207 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9208 {
9209 	u32 val;
9210 
9211 	/* do the biggest reset indicated */
9212 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9213 
9214 		/* Request a Global Reset
9215 		 *
9216 		 * This will start the chip's countdown to the actual full
9217 		 * chip reset event, and a warning interrupt to be sent
9218 		 * to all PFs, including the requestor.  Our handler
9219 		 * for the warning interrupt will deal with the shutdown
9220 		 * and recovery of the switch setup.
9221 		 */
9222 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9223 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9224 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9225 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9226 
9227 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9228 
9229 		/* Request a Core Reset
9230 		 *
9231 		 * Same as Global Reset, except does *not* include the MAC/PHY
9232 		 */
9233 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9234 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9235 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9236 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9237 		i40e_flush(&pf->hw);
9238 
9239 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9240 
9241 		/* Request a PF Reset
9242 		 *
9243 		 * Resets only the PF-specific registers
9244 		 *
9245 		 * This goes directly to the tear-down and rebuild of
9246 		 * the switch, since we need to do all the recovery as
9247 		 * for the Core Reset.
9248 		 */
9249 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9250 		i40e_handle_reset_warning(pf, lock_acquired);
9251 
9252 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9253 		/* Request a PF Reset
9254 		 *
9255 		 * Resets PF and reinitializes PFs VSI.
9256 		 */
9257 		i40e_prep_for_reset(pf);
9258 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9259 		dev_info(&pf->pdev->dev,
9260 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9261 			 "FW LLDP is disabled\n" :
9262 			 "FW LLDP is enabled\n");
9263 
9264 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9265 		int v;
9266 
9267 		/* Find the VSI(s) that requested a re-init */
9268 		dev_info(&pf->pdev->dev,
9269 			 "VSI reinit requested\n");
9270 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9271 			struct i40e_vsi *vsi = pf->vsi[v];
9272 
9273 			if (vsi != NULL &&
9274 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9275 					       vsi->state))
9276 				i40e_vsi_reinit_locked(pf->vsi[v]);
9277 		}
9278 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9279 		int v;
9280 
9281 		/* Find the VSI(s) that needs to be brought down */
9282 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9283 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9284 			struct i40e_vsi *vsi = pf->vsi[v];
9285 
9286 			if (vsi != NULL &&
9287 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9288 					       vsi->state)) {
9289 				set_bit(__I40E_VSI_DOWN, vsi->state);
9290 				i40e_down(vsi);
9291 			}
9292 		}
9293 	} else {
9294 		dev_info(&pf->pdev->dev,
9295 			 "bad reset request 0x%08x\n", reset_flags);
9296 	}
9297 }
9298 
9299 #ifdef CONFIG_I40E_DCB
9300 /**
9301  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9302  * @pf: board private structure
9303  * @old_cfg: current DCB config
9304  * @new_cfg: new DCB config
9305  **/
9306 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9307 			    struct i40e_dcbx_config *old_cfg,
9308 			    struct i40e_dcbx_config *new_cfg)
9309 {
9310 	bool need_reconfig = false;
9311 
9312 	/* Check if ETS configuration has changed */
9313 	if (memcmp(&new_cfg->etscfg,
9314 		   &old_cfg->etscfg,
9315 		   sizeof(new_cfg->etscfg))) {
9316 		/* If Priority Table has changed reconfig is needed */
9317 		if (memcmp(&new_cfg->etscfg.prioritytable,
9318 			   &old_cfg->etscfg.prioritytable,
9319 			   sizeof(new_cfg->etscfg.prioritytable))) {
9320 			need_reconfig = true;
9321 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9322 		}
9323 
9324 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9325 			   &old_cfg->etscfg.tcbwtable,
9326 			   sizeof(new_cfg->etscfg.tcbwtable)))
9327 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9328 
9329 		if (memcmp(&new_cfg->etscfg.tsatable,
9330 			   &old_cfg->etscfg.tsatable,
9331 			   sizeof(new_cfg->etscfg.tsatable)))
9332 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9333 	}
9334 
9335 	/* Check if PFC configuration has changed */
9336 	if (memcmp(&new_cfg->pfc,
9337 		   &old_cfg->pfc,
9338 		   sizeof(new_cfg->pfc))) {
9339 		need_reconfig = true;
9340 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9341 	}
9342 
9343 	/* Check if APP Table has changed */
9344 	if (memcmp(&new_cfg->app,
9345 		   &old_cfg->app,
9346 		   sizeof(new_cfg->app))) {
9347 		need_reconfig = true;
9348 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9349 	}
9350 
9351 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9352 	return need_reconfig;
9353 }
9354 
9355 /**
9356  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9357  * @pf: board private structure
9358  * @e: event info posted on ARQ
9359  **/
9360 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9361 				  struct i40e_arq_event_info *e)
9362 {
9363 	struct i40e_aqc_lldp_get_mib *mib =
9364 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9365 	struct i40e_hw *hw = &pf->hw;
9366 	struct i40e_dcbx_config tmp_dcbx_cfg;
9367 	bool need_reconfig = false;
9368 	int ret = 0;
9369 	u8 type;
9370 
9371 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9372 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9373 	    (hw->phy.link_info.link_speed &
9374 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9375 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9376 		/* let firmware decide if the DCB should be disabled */
9377 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9378 
9379 	/* Not DCB capable or capability disabled */
9380 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9381 		return ret;
9382 
9383 	/* Ignore if event is not for Nearest Bridge */
9384 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9385 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9386 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9387 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9388 		return ret;
9389 
9390 	/* Check MIB Type and return if event for Remote MIB update */
9391 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9392 	dev_dbg(&pf->pdev->dev,
9393 		"LLDP event mib type %s\n", type ? "remote" : "local");
9394 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9395 		/* Update the remote cached instance and return */
9396 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9397 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9398 				&hw->remote_dcbx_config);
9399 		goto exit;
9400 	}
9401 
9402 	/* Store the old configuration */
9403 	tmp_dcbx_cfg = hw->local_dcbx_config;
9404 
9405 	/* Reset the old DCBx configuration data */
9406 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9407 	/* Get updated DCBX data from firmware */
9408 	ret = i40e_get_dcb_config(&pf->hw);
9409 	if (ret) {
9410 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9411 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9412 		    (hw->phy.link_info.link_speed &
9413 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9414 			dev_warn(&pf->pdev->dev,
9415 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9416 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9417 		} else {
9418 			dev_info(&pf->pdev->dev,
9419 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9420 				 i40e_stat_str(&pf->hw, ret),
9421 				 i40e_aq_str(&pf->hw,
9422 					     pf->hw.aq.asq_last_status));
9423 		}
9424 		goto exit;
9425 	}
9426 
9427 	/* No change detected in DCBX configs */
9428 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9429 		    sizeof(tmp_dcbx_cfg))) {
9430 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9431 		goto exit;
9432 	}
9433 
9434 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9435 					       &hw->local_dcbx_config);
9436 
9437 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9438 
9439 	if (!need_reconfig)
9440 		goto exit;
9441 
9442 	/* Enable DCB tagging only when more than one TC */
9443 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9444 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9445 	else
9446 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9447 
9448 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9449 	/* Reconfiguration needed quiesce all VSIs */
9450 	i40e_pf_quiesce_all_vsi(pf);
9451 
9452 	/* Changes in configuration update VEB/VSI */
9453 	i40e_dcb_reconfigure(pf);
9454 
9455 	ret = i40e_resume_port_tx(pf);
9456 
9457 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9458 	/* In case of error no point in resuming VSIs */
9459 	if (ret)
9460 		goto exit;
9461 
9462 	/* Wait for the PF's queues to be disabled */
9463 	ret = i40e_pf_wait_queues_disabled(pf);
9464 	if (ret) {
9465 		/* Schedule PF reset to recover */
9466 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9467 		i40e_service_event_schedule(pf);
9468 	} else {
9469 		i40e_pf_unquiesce_all_vsi(pf);
9470 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9471 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9472 	}
9473 
9474 exit:
9475 	return ret;
9476 }
9477 #endif /* CONFIG_I40E_DCB */
9478 
9479 /**
9480  * i40e_do_reset_safe - Protected reset path for userland calls.
9481  * @pf: board private structure
9482  * @reset_flags: which reset is requested
9483  *
9484  **/
9485 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9486 {
9487 	rtnl_lock();
9488 	i40e_do_reset(pf, reset_flags, true);
9489 	rtnl_unlock();
9490 }
9491 
9492 /**
9493  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9494  * @pf: board private structure
9495  * @e: event info posted on ARQ
9496  *
9497  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9498  * and VF queues
9499  **/
9500 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9501 					   struct i40e_arq_event_info *e)
9502 {
9503 	struct i40e_aqc_lan_overflow *data =
9504 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9505 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9506 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9507 	struct i40e_hw *hw = &pf->hw;
9508 	struct i40e_vf *vf;
9509 	u16 vf_id;
9510 
9511 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9512 		queue, qtx_ctl);
9513 
9514 	/* Queue belongs to VF, find the VF and issue VF reset */
9515 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9516 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9517 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9518 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9519 		vf_id -= hw->func_caps.vf_base_id;
9520 		vf = &pf->vf[vf_id];
9521 		i40e_vc_notify_vf_reset(vf);
9522 		/* Allow VF to process pending reset notification */
9523 		msleep(20);
9524 		i40e_reset_vf(vf, false);
9525 	}
9526 }
9527 
9528 /**
9529  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9530  * @pf: board private structure
9531  **/
9532 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9533 {
9534 	u32 val, fcnt_prog;
9535 
9536 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9537 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9538 	return fcnt_prog;
9539 }
9540 
9541 /**
9542  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9543  * @pf: board private structure
9544  **/
9545 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9546 {
9547 	u32 val, fcnt_prog;
9548 
9549 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9550 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9551 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9552 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9553 	return fcnt_prog;
9554 }
9555 
9556 /**
9557  * i40e_get_global_fd_count - Get total FD filters programmed on device
9558  * @pf: board private structure
9559  **/
9560 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9561 {
9562 	u32 val, fcnt_prog;
9563 
9564 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9565 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9566 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9567 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9568 	return fcnt_prog;
9569 }
9570 
9571 /**
9572  * i40e_reenable_fdir_sb - Restore FDir SB capability
9573  * @pf: board private structure
9574  **/
9575 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9576 {
9577 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9578 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9579 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9580 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9581 }
9582 
9583 /**
9584  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9585  * @pf: board private structure
9586  **/
9587 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9588 {
9589 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9590 		/* ATR uses the same filtering logic as SB rules. It only
9591 		 * functions properly if the input set mask is at the default
9592 		 * settings. It is safe to restore the default input set
9593 		 * because there are no active TCPv4 filter rules.
9594 		 */
9595 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9596 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9597 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9598 
9599 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9600 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9601 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9602 	}
9603 }
9604 
9605 /**
9606  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9607  * @pf: board private structure
9608  * @filter: FDir filter to remove
9609  */
9610 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9611 				       struct i40e_fdir_filter *filter)
9612 {
9613 	/* Update counters */
9614 	pf->fdir_pf_active_filters--;
9615 	pf->fd_inv = 0;
9616 
9617 	switch (filter->flow_type) {
9618 	case TCP_V4_FLOW:
9619 		pf->fd_tcp4_filter_cnt--;
9620 		break;
9621 	case UDP_V4_FLOW:
9622 		pf->fd_udp4_filter_cnt--;
9623 		break;
9624 	case SCTP_V4_FLOW:
9625 		pf->fd_sctp4_filter_cnt--;
9626 		break;
9627 	case TCP_V6_FLOW:
9628 		pf->fd_tcp6_filter_cnt--;
9629 		break;
9630 	case UDP_V6_FLOW:
9631 		pf->fd_udp6_filter_cnt--;
9632 		break;
9633 	case SCTP_V6_FLOW:
9634 		pf->fd_udp6_filter_cnt--;
9635 		break;
9636 	case IP_USER_FLOW:
9637 		switch (filter->ipl4_proto) {
9638 		case IPPROTO_TCP:
9639 			pf->fd_tcp4_filter_cnt--;
9640 			break;
9641 		case IPPROTO_UDP:
9642 			pf->fd_udp4_filter_cnt--;
9643 			break;
9644 		case IPPROTO_SCTP:
9645 			pf->fd_sctp4_filter_cnt--;
9646 			break;
9647 		case IPPROTO_IP:
9648 			pf->fd_ip4_filter_cnt--;
9649 			break;
9650 		}
9651 		break;
9652 	case IPV6_USER_FLOW:
9653 		switch (filter->ipl4_proto) {
9654 		case IPPROTO_TCP:
9655 			pf->fd_tcp6_filter_cnt--;
9656 			break;
9657 		case IPPROTO_UDP:
9658 			pf->fd_udp6_filter_cnt--;
9659 			break;
9660 		case IPPROTO_SCTP:
9661 			pf->fd_sctp6_filter_cnt--;
9662 			break;
9663 		case IPPROTO_IP:
9664 			pf->fd_ip6_filter_cnt--;
9665 			break;
9666 		}
9667 		break;
9668 	}
9669 
9670 	/* Remove the filter from the list and free memory */
9671 	hlist_del(&filter->fdir_node);
9672 	kfree(filter);
9673 }
9674 
9675 /**
9676  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9677  * @pf: board private structure
9678  **/
9679 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9680 {
9681 	struct i40e_fdir_filter *filter;
9682 	u32 fcnt_prog, fcnt_avail;
9683 	struct hlist_node *node;
9684 
9685 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9686 		return;
9687 
9688 	/* Check if we have enough room to re-enable FDir SB capability. */
9689 	fcnt_prog = i40e_get_global_fd_count(pf);
9690 	fcnt_avail = pf->fdir_pf_filter_count;
9691 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9692 	    (pf->fd_add_err == 0) ||
9693 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9694 		i40e_reenable_fdir_sb(pf);
9695 
9696 	/* We should wait for even more space before re-enabling ATR.
9697 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9698 	 * rules active.
9699 	 */
9700 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9701 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9702 		i40e_reenable_fdir_atr(pf);
9703 
9704 	/* if hw had a problem adding a filter, delete it */
9705 	if (pf->fd_inv > 0) {
9706 		hlist_for_each_entry_safe(filter, node,
9707 					  &pf->fdir_filter_list, fdir_node)
9708 			if (filter->fd_id == pf->fd_inv)
9709 				i40e_delete_invalid_filter(pf, filter);
9710 	}
9711 }
9712 
9713 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9714 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9715 /**
9716  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9717  * @pf: board private structure
9718  **/
9719 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9720 {
9721 	unsigned long min_flush_time;
9722 	int flush_wait_retry = 50;
9723 	bool disable_atr = false;
9724 	int fd_room;
9725 	int reg;
9726 
9727 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9728 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9729 		return;
9730 
9731 	/* If the flush is happening too quick and we have mostly SB rules we
9732 	 * should not re-enable ATR for some time.
9733 	 */
9734 	min_flush_time = pf->fd_flush_timestamp +
9735 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9736 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9737 
9738 	if (!(time_after(jiffies, min_flush_time)) &&
9739 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9740 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9741 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9742 		disable_atr = true;
9743 	}
9744 
9745 	pf->fd_flush_timestamp = jiffies;
9746 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9747 	/* flush all filters */
9748 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9749 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9750 	i40e_flush(&pf->hw);
9751 	pf->fd_flush_cnt++;
9752 	pf->fd_add_err = 0;
9753 	do {
9754 		/* Check FD flush status every 5-6msec */
9755 		usleep_range(5000, 6000);
9756 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9757 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9758 			break;
9759 	} while (flush_wait_retry--);
9760 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9761 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9762 	} else {
9763 		/* replay sideband filters */
9764 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9765 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9766 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9767 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9768 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9769 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9770 	}
9771 }
9772 
9773 /**
9774  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9775  * @pf: board private structure
9776  **/
9777 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9778 {
9779 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9780 }
9781 
9782 /**
9783  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9784  * @pf: board private structure
9785  **/
9786 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9787 {
9788 
9789 	/* if interface is down do nothing */
9790 	if (test_bit(__I40E_DOWN, pf->state))
9791 		return;
9792 
9793 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9794 		i40e_fdir_flush_and_replay(pf);
9795 
9796 	i40e_fdir_check_and_reenable(pf);
9797 
9798 }
9799 
9800 /**
9801  * i40e_vsi_link_event - notify VSI of a link event
9802  * @vsi: vsi to be notified
9803  * @link_up: link up or down
9804  **/
9805 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9806 {
9807 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9808 		return;
9809 
9810 	switch (vsi->type) {
9811 	case I40E_VSI_MAIN:
9812 		if (!vsi->netdev || !vsi->netdev_registered)
9813 			break;
9814 
9815 		if (link_up) {
9816 			netif_carrier_on(vsi->netdev);
9817 			netif_tx_wake_all_queues(vsi->netdev);
9818 		} else {
9819 			netif_carrier_off(vsi->netdev);
9820 			netif_tx_stop_all_queues(vsi->netdev);
9821 		}
9822 		break;
9823 
9824 	case I40E_VSI_SRIOV:
9825 	case I40E_VSI_VMDQ2:
9826 	case I40E_VSI_CTRL:
9827 	case I40E_VSI_IWARP:
9828 	case I40E_VSI_MIRROR:
9829 	default:
9830 		/* there is no notification for other VSIs */
9831 		break;
9832 	}
9833 }
9834 
9835 /**
9836  * i40e_veb_link_event - notify elements on the veb of a link event
9837  * @veb: veb to be notified
9838  * @link_up: link up or down
9839  **/
9840 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9841 {
9842 	struct i40e_pf *pf;
9843 	int i;
9844 
9845 	if (!veb || !veb->pf)
9846 		return;
9847 	pf = veb->pf;
9848 
9849 	/* depth first... */
9850 	for (i = 0; i < I40E_MAX_VEB; i++)
9851 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9852 			i40e_veb_link_event(pf->veb[i], link_up);
9853 
9854 	/* ... now the local VSIs */
9855 	for (i = 0; i < pf->num_alloc_vsi; i++)
9856 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9857 			i40e_vsi_link_event(pf->vsi[i], link_up);
9858 }
9859 
9860 /**
9861  * i40e_link_event - Update netif_carrier status
9862  * @pf: board private structure
9863  **/
9864 static void i40e_link_event(struct i40e_pf *pf)
9865 {
9866 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9867 	u8 new_link_speed, old_link_speed;
9868 	i40e_status status;
9869 	bool new_link, old_link;
9870 #ifdef CONFIG_I40E_DCB
9871 	int err;
9872 #endif /* CONFIG_I40E_DCB */
9873 
9874 	/* set this to force the get_link_status call to refresh state */
9875 	pf->hw.phy.get_link_info = true;
9876 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9877 	status = i40e_get_link_status(&pf->hw, &new_link);
9878 
9879 	/* On success, disable temp link polling */
9880 	if (status == I40E_SUCCESS) {
9881 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9882 	} else {
9883 		/* Enable link polling temporarily until i40e_get_link_status
9884 		 * returns I40E_SUCCESS
9885 		 */
9886 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9887 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9888 			status);
9889 		return;
9890 	}
9891 
9892 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9893 	new_link_speed = pf->hw.phy.link_info.link_speed;
9894 
9895 	if (new_link == old_link &&
9896 	    new_link_speed == old_link_speed &&
9897 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9898 	     new_link == netif_carrier_ok(vsi->netdev)))
9899 		return;
9900 
9901 	i40e_print_link_message(vsi, new_link);
9902 
9903 	/* Notify the base of the switch tree connected to
9904 	 * the link.  Floating VEBs are not notified.
9905 	 */
9906 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9907 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9908 	else
9909 		i40e_vsi_link_event(vsi, new_link);
9910 
9911 	if (pf->vf)
9912 		i40e_vc_notify_link_state(pf);
9913 
9914 	if (pf->flags & I40E_FLAG_PTP)
9915 		i40e_ptp_set_increment(pf);
9916 #ifdef CONFIG_I40E_DCB
9917 	if (new_link == old_link)
9918 		return;
9919 	/* Not SW DCB so firmware will take care of default settings */
9920 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9921 		return;
9922 
9923 	/* We cover here only link down, as after link up in case of SW DCB
9924 	 * SW LLDP agent will take care of setting it up
9925 	 */
9926 	if (!new_link) {
9927 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9928 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9929 		err = i40e_dcb_sw_default_config(pf);
9930 		if (err) {
9931 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9932 				       I40E_FLAG_DCB_ENABLED);
9933 		} else {
9934 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9935 				       DCB_CAP_DCBX_VER_IEEE;
9936 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9937 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9938 		}
9939 	}
9940 #endif /* CONFIG_I40E_DCB */
9941 }
9942 
9943 /**
9944  * i40e_watchdog_subtask - periodic checks not using event driven response
9945  * @pf: board private structure
9946  **/
9947 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9948 {
9949 	int i;
9950 
9951 	/* if interface is down do nothing */
9952 	if (test_bit(__I40E_DOWN, pf->state) ||
9953 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9954 		return;
9955 
9956 	/* make sure we don't do these things too often */
9957 	if (time_before(jiffies, (pf->service_timer_previous +
9958 				  pf->service_timer_period)))
9959 		return;
9960 	pf->service_timer_previous = jiffies;
9961 
9962 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9963 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9964 		i40e_link_event(pf);
9965 
9966 	/* Update the stats for active netdevs so the network stack
9967 	 * can look at updated numbers whenever it cares to
9968 	 */
9969 	for (i = 0; i < pf->num_alloc_vsi; i++)
9970 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9971 			i40e_update_stats(pf->vsi[i]);
9972 
9973 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9974 		/* Update the stats for the active switching components */
9975 		for (i = 0; i < I40E_MAX_VEB; i++)
9976 			if (pf->veb[i])
9977 				i40e_update_veb_stats(pf->veb[i]);
9978 	}
9979 
9980 	i40e_ptp_rx_hang(pf);
9981 	i40e_ptp_tx_hang(pf);
9982 }
9983 
9984 /**
9985  * i40e_reset_subtask - Set up for resetting the device and driver
9986  * @pf: board private structure
9987  **/
9988 static void i40e_reset_subtask(struct i40e_pf *pf)
9989 {
9990 	u32 reset_flags = 0;
9991 
9992 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9993 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9994 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9995 	}
9996 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9997 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9998 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9999 	}
10000 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10001 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10002 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10003 	}
10004 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10005 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10006 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10007 	}
10008 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10009 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10010 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10011 	}
10012 
10013 	/* If there's a recovery already waiting, it takes
10014 	 * precedence before starting a new reset sequence.
10015 	 */
10016 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10017 		i40e_prep_for_reset(pf);
10018 		i40e_reset(pf);
10019 		i40e_rebuild(pf, false, false);
10020 	}
10021 
10022 	/* If we're already down or resetting, just bail */
10023 	if (reset_flags &&
10024 	    !test_bit(__I40E_DOWN, pf->state) &&
10025 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10026 		i40e_do_reset(pf, reset_flags, false);
10027 	}
10028 }
10029 
10030 /**
10031  * i40e_handle_link_event - Handle link event
10032  * @pf: board private structure
10033  * @e: event info posted on ARQ
10034  **/
10035 static void i40e_handle_link_event(struct i40e_pf *pf,
10036 				   struct i40e_arq_event_info *e)
10037 {
10038 	struct i40e_aqc_get_link_status *status =
10039 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10040 
10041 	/* Do a new status request to re-enable LSE reporting
10042 	 * and load new status information into the hw struct
10043 	 * This completely ignores any state information
10044 	 * in the ARQ event info, instead choosing to always
10045 	 * issue the AQ update link status command.
10046 	 */
10047 	i40e_link_event(pf);
10048 
10049 	/* Check if module meets thermal requirements */
10050 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10051 		dev_err(&pf->pdev->dev,
10052 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10053 		dev_err(&pf->pdev->dev,
10054 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10055 	} else {
10056 		/* check for unqualified module, if link is down, suppress
10057 		 * the message if link was forced to be down.
10058 		 */
10059 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10060 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10061 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10062 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10063 			dev_err(&pf->pdev->dev,
10064 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10065 			dev_err(&pf->pdev->dev,
10066 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10067 		}
10068 	}
10069 }
10070 
10071 /**
10072  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10073  * @pf: board private structure
10074  **/
10075 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10076 {
10077 	struct i40e_arq_event_info event;
10078 	struct i40e_hw *hw = &pf->hw;
10079 	u16 pending, i = 0;
10080 	i40e_status ret;
10081 	u16 opcode;
10082 	u32 oldval;
10083 	u32 val;
10084 
10085 	/* Do not run clean AQ when PF reset fails */
10086 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10087 		return;
10088 
10089 	/* check for error indications */
10090 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10091 	oldval = val;
10092 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10093 		if (hw->debug_mask & I40E_DEBUG_AQ)
10094 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10095 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10096 	}
10097 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10098 		if (hw->debug_mask & I40E_DEBUG_AQ)
10099 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10100 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10101 		pf->arq_overflows++;
10102 	}
10103 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10104 		if (hw->debug_mask & I40E_DEBUG_AQ)
10105 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10106 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10107 	}
10108 	if (oldval != val)
10109 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10110 
10111 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10112 	oldval = val;
10113 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10114 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10115 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10116 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10117 	}
10118 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10119 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10120 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10121 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10122 	}
10123 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10124 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10125 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10126 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10127 	}
10128 	if (oldval != val)
10129 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10130 
10131 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10132 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10133 	if (!event.msg_buf)
10134 		return;
10135 
10136 	do {
10137 		ret = i40e_clean_arq_element(hw, &event, &pending);
10138 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10139 			break;
10140 		else if (ret) {
10141 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10142 			break;
10143 		}
10144 
10145 		opcode = le16_to_cpu(event.desc.opcode);
10146 		switch (opcode) {
10147 
10148 		case i40e_aqc_opc_get_link_status:
10149 			rtnl_lock();
10150 			i40e_handle_link_event(pf, &event);
10151 			rtnl_unlock();
10152 			break;
10153 		case i40e_aqc_opc_send_msg_to_pf:
10154 			ret = i40e_vc_process_vf_msg(pf,
10155 					le16_to_cpu(event.desc.retval),
10156 					le32_to_cpu(event.desc.cookie_high),
10157 					le32_to_cpu(event.desc.cookie_low),
10158 					event.msg_buf,
10159 					event.msg_len);
10160 			break;
10161 		case i40e_aqc_opc_lldp_update_mib:
10162 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10163 #ifdef CONFIG_I40E_DCB
10164 			rtnl_lock();
10165 			i40e_handle_lldp_event(pf, &event);
10166 			rtnl_unlock();
10167 #endif /* CONFIG_I40E_DCB */
10168 			break;
10169 		case i40e_aqc_opc_event_lan_overflow:
10170 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10171 			i40e_handle_lan_overflow_event(pf, &event);
10172 			break;
10173 		case i40e_aqc_opc_send_msg_to_peer:
10174 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10175 			break;
10176 		case i40e_aqc_opc_nvm_erase:
10177 		case i40e_aqc_opc_nvm_update:
10178 		case i40e_aqc_opc_oem_post_update:
10179 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10180 				   "ARQ NVM operation 0x%04x completed\n",
10181 				   opcode);
10182 			break;
10183 		default:
10184 			dev_info(&pf->pdev->dev,
10185 				 "ARQ: Unknown event 0x%04x ignored\n",
10186 				 opcode);
10187 			break;
10188 		}
10189 	} while (i++ < pf->adminq_work_limit);
10190 
10191 	if (i < pf->adminq_work_limit)
10192 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10193 
10194 	/* re-enable Admin queue interrupt cause */
10195 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10196 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10197 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10198 	i40e_flush(hw);
10199 
10200 	kfree(event.msg_buf);
10201 }
10202 
10203 /**
10204  * i40e_verify_eeprom - make sure eeprom is good to use
10205  * @pf: board private structure
10206  **/
10207 static void i40e_verify_eeprom(struct i40e_pf *pf)
10208 {
10209 	int err;
10210 
10211 	err = i40e_diag_eeprom_test(&pf->hw);
10212 	if (err) {
10213 		/* retry in case of garbage read */
10214 		err = i40e_diag_eeprom_test(&pf->hw);
10215 		if (err) {
10216 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10217 				 err);
10218 			set_bit(__I40E_BAD_EEPROM, pf->state);
10219 		}
10220 	}
10221 
10222 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10223 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10224 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10225 	}
10226 }
10227 
10228 /**
10229  * i40e_enable_pf_switch_lb
10230  * @pf: pointer to the PF structure
10231  *
10232  * enable switch loop back or die - no point in a return value
10233  **/
10234 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10235 {
10236 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10237 	struct i40e_vsi_context ctxt;
10238 	int ret;
10239 
10240 	ctxt.seid = pf->main_vsi_seid;
10241 	ctxt.pf_num = pf->hw.pf_id;
10242 	ctxt.vf_num = 0;
10243 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10244 	if (ret) {
10245 		dev_info(&pf->pdev->dev,
10246 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10247 			 i40e_stat_str(&pf->hw, ret),
10248 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10249 		return;
10250 	}
10251 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10252 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10253 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10254 
10255 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10256 	if (ret) {
10257 		dev_info(&pf->pdev->dev,
10258 			 "update vsi switch failed, err %s aq_err %s\n",
10259 			 i40e_stat_str(&pf->hw, ret),
10260 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10261 	}
10262 }
10263 
10264 /**
10265  * i40e_disable_pf_switch_lb
10266  * @pf: pointer to the PF structure
10267  *
10268  * disable switch loop back or die - no point in a return value
10269  **/
10270 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10271 {
10272 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10273 	struct i40e_vsi_context ctxt;
10274 	int ret;
10275 
10276 	ctxt.seid = pf->main_vsi_seid;
10277 	ctxt.pf_num = pf->hw.pf_id;
10278 	ctxt.vf_num = 0;
10279 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10280 	if (ret) {
10281 		dev_info(&pf->pdev->dev,
10282 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10283 			 i40e_stat_str(&pf->hw, ret),
10284 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10285 		return;
10286 	}
10287 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10288 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10289 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10290 
10291 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10292 	if (ret) {
10293 		dev_info(&pf->pdev->dev,
10294 			 "update vsi switch failed, err %s aq_err %s\n",
10295 			 i40e_stat_str(&pf->hw, ret),
10296 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10297 	}
10298 }
10299 
10300 /**
10301  * i40e_config_bridge_mode - Configure the HW bridge mode
10302  * @veb: pointer to the bridge instance
10303  *
10304  * Configure the loop back mode for the LAN VSI that is downlink to the
10305  * specified HW bridge instance. It is expected this function is called
10306  * when a new HW bridge is instantiated.
10307  **/
10308 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10309 {
10310 	struct i40e_pf *pf = veb->pf;
10311 
10312 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10313 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10314 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10315 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10316 		i40e_disable_pf_switch_lb(pf);
10317 	else
10318 		i40e_enable_pf_switch_lb(pf);
10319 }
10320 
10321 /**
10322  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10323  * @veb: pointer to the VEB instance
10324  *
10325  * This is a recursive function that first builds the attached VSIs then
10326  * recurses in to build the next layer of VEB.  We track the connections
10327  * through our own index numbers because the seid's from the HW could
10328  * change across the reset.
10329  **/
10330 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10331 {
10332 	struct i40e_vsi *ctl_vsi = NULL;
10333 	struct i40e_pf *pf = veb->pf;
10334 	int v, veb_idx;
10335 	int ret;
10336 
10337 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10338 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10339 		if (pf->vsi[v] &&
10340 		    pf->vsi[v]->veb_idx == veb->idx &&
10341 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10342 			ctl_vsi = pf->vsi[v];
10343 			break;
10344 		}
10345 	}
10346 	if (!ctl_vsi) {
10347 		dev_info(&pf->pdev->dev,
10348 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10349 		ret = -ENOENT;
10350 		goto end_reconstitute;
10351 	}
10352 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10353 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10354 	ret = i40e_add_vsi(ctl_vsi);
10355 	if (ret) {
10356 		dev_info(&pf->pdev->dev,
10357 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10358 			 veb->idx, ret);
10359 		goto end_reconstitute;
10360 	}
10361 	i40e_vsi_reset_stats(ctl_vsi);
10362 
10363 	/* create the VEB in the switch and move the VSI onto the VEB */
10364 	ret = i40e_add_veb(veb, ctl_vsi);
10365 	if (ret)
10366 		goto end_reconstitute;
10367 
10368 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10369 		veb->bridge_mode = BRIDGE_MODE_VEB;
10370 	else
10371 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10372 	i40e_config_bridge_mode(veb);
10373 
10374 	/* create the remaining VSIs attached to this VEB */
10375 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10376 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10377 			continue;
10378 
10379 		if (pf->vsi[v]->veb_idx == veb->idx) {
10380 			struct i40e_vsi *vsi = pf->vsi[v];
10381 
10382 			vsi->uplink_seid = veb->seid;
10383 			ret = i40e_add_vsi(vsi);
10384 			if (ret) {
10385 				dev_info(&pf->pdev->dev,
10386 					 "rebuild of vsi_idx %d failed: %d\n",
10387 					 v, ret);
10388 				goto end_reconstitute;
10389 			}
10390 			i40e_vsi_reset_stats(vsi);
10391 		}
10392 	}
10393 
10394 	/* create any VEBs attached to this VEB - RECURSION */
10395 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10396 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10397 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10398 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10399 			if (ret)
10400 				break;
10401 		}
10402 	}
10403 
10404 end_reconstitute:
10405 	return ret;
10406 }
10407 
10408 /**
10409  * i40e_get_capabilities - get info about the HW
10410  * @pf: the PF struct
10411  * @list_type: AQ capability to be queried
10412  **/
10413 static int i40e_get_capabilities(struct i40e_pf *pf,
10414 				 enum i40e_admin_queue_opc list_type)
10415 {
10416 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10417 	u16 data_size;
10418 	int buf_len;
10419 	int err;
10420 
10421 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10422 	do {
10423 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10424 		if (!cap_buf)
10425 			return -ENOMEM;
10426 
10427 		/* this loads the data into the hw struct for us */
10428 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10429 						    &data_size, list_type,
10430 						    NULL);
10431 		/* data loaded, buffer no longer needed */
10432 		kfree(cap_buf);
10433 
10434 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10435 			/* retry with a larger buffer */
10436 			buf_len = data_size;
10437 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10438 			dev_info(&pf->pdev->dev,
10439 				 "capability discovery failed, err %s aq_err %s\n",
10440 				 i40e_stat_str(&pf->hw, err),
10441 				 i40e_aq_str(&pf->hw,
10442 					     pf->hw.aq.asq_last_status));
10443 			return -ENODEV;
10444 		}
10445 	} while (err);
10446 
10447 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10448 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10449 			dev_info(&pf->pdev->dev,
10450 				 "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",
10451 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10452 				 pf->hw.func_caps.num_msix_vectors,
10453 				 pf->hw.func_caps.num_msix_vectors_vf,
10454 				 pf->hw.func_caps.fd_filters_guaranteed,
10455 				 pf->hw.func_caps.fd_filters_best_effort,
10456 				 pf->hw.func_caps.num_tx_qp,
10457 				 pf->hw.func_caps.num_vsis);
10458 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10459 			dev_info(&pf->pdev->dev,
10460 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10461 				 pf->hw.dev_caps.switch_mode,
10462 				 pf->hw.dev_caps.valid_functions);
10463 			dev_info(&pf->pdev->dev,
10464 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10465 				 pf->hw.dev_caps.sr_iov_1_1,
10466 				 pf->hw.dev_caps.num_vfs);
10467 			dev_info(&pf->pdev->dev,
10468 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10469 				 pf->hw.dev_caps.num_vsis,
10470 				 pf->hw.dev_caps.num_rx_qp,
10471 				 pf->hw.dev_caps.num_tx_qp);
10472 		}
10473 	}
10474 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10475 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10476 		       + pf->hw.func_caps.num_vfs)
10477 		if (pf->hw.revision_id == 0 &&
10478 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10479 			dev_info(&pf->pdev->dev,
10480 				 "got num_vsis %d, setting num_vsis to %d\n",
10481 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10482 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10483 		}
10484 	}
10485 	return 0;
10486 }
10487 
10488 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10489 
10490 /**
10491  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10492  * @pf: board private structure
10493  **/
10494 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10495 {
10496 	struct i40e_vsi *vsi;
10497 
10498 	/* quick workaround for an NVM issue that leaves a critical register
10499 	 * uninitialized
10500 	 */
10501 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10502 		static const u32 hkey[] = {
10503 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10504 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10505 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10506 			0x95b3a76d};
10507 		int i;
10508 
10509 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10510 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10511 	}
10512 
10513 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10514 		return;
10515 
10516 	/* find existing VSI and see if it needs configuring */
10517 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10518 
10519 	/* create a new VSI if none exists */
10520 	if (!vsi) {
10521 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10522 				     pf->vsi[pf->lan_vsi]->seid, 0);
10523 		if (!vsi) {
10524 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10525 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10526 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10527 			return;
10528 		}
10529 	}
10530 
10531 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10532 }
10533 
10534 /**
10535  * i40e_fdir_teardown - release the Flow Director resources
10536  * @pf: board private structure
10537  **/
10538 static void i40e_fdir_teardown(struct i40e_pf *pf)
10539 {
10540 	struct i40e_vsi *vsi;
10541 
10542 	i40e_fdir_filter_exit(pf);
10543 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10544 	if (vsi)
10545 		i40e_vsi_release(vsi);
10546 }
10547 
10548 /**
10549  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10550  * @vsi: PF main vsi
10551  * @seid: seid of main or channel VSIs
10552  *
10553  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10554  * existed before reset
10555  **/
10556 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10557 {
10558 	struct i40e_cloud_filter *cfilter;
10559 	struct i40e_pf *pf = vsi->back;
10560 	struct hlist_node *node;
10561 	i40e_status ret;
10562 
10563 	/* Add cloud filters back if they exist */
10564 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10565 				  cloud_node) {
10566 		if (cfilter->seid != seid)
10567 			continue;
10568 
10569 		if (cfilter->dst_port)
10570 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10571 								true);
10572 		else
10573 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10574 
10575 		if (ret) {
10576 			dev_dbg(&pf->pdev->dev,
10577 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10578 				i40e_stat_str(&pf->hw, ret),
10579 				i40e_aq_str(&pf->hw,
10580 					    pf->hw.aq.asq_last_status));
10581 			return ret;
10582 		}
10583 	}
10584 	return 0;
10585 }
10586 
10587 /**
10588  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10589  * @vsi: PF main vsi
10590  *
10591  * Rebuilds channel VSIs if they existed before reset
10592  **/
10593 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10594 {
10595 	struct i40e_channel *ch, *ch_tmp;
10596 	i40e_status ret;
10597 
10598 	if (list_empty(&vsi->ch_list))
10599 		return 0;
10600 
10601 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10602 		if (!ch->initialized)
10603 			break;
10604 		/* Proceed with creation of channel (VMDq2) VSI */
10605 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10606 		if (ret) {
10607 			dev_info(&vsi->back->pdev->dev,
10608 				 "failed to rebuild channels using uplink_seid %u\n",
10609 				 vsi->uplink_seid);
10610 			return ret;
10611 		}
10612 		/* Reconfigure TX queues using QTX_CTL register */
10613 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10614 		if (ret) {
10615 			dev_info(&vsi->back->pdev->dev,
10616 				 "failed to configure TX rings for channel %u\n",
10617 				 ch->seid);
10618 			return ret;
10619 		}
10620 		/* update 'next_base_queue' */
10621 		vsi->next_base_queue = vsi->next_base_queue +
10622 							ch->num_queue_pairs;
10623 		if (ch->max_tx_rate) {
10624 			u64 credits = ch->max_tx_rate;
10625 
10626 			if (i40e_set_bw_limit(vsi, ch->seid,
10627 					      ch->max_tx_rate))
10628 				return -EINVAL;
10629 
10630 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10631 			dev_dbg(&vsi->back->pdev->dev,
10632 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10633 				ch->max_tx_rate,
10634 				credits,
10635 				ch->seid);
10636 		}
10637 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10638 		if (ret) {
10639 			dev_dbg(&vsi->back->pdev->dev,
10640 				"Failed to rebuild cloud filters for channel VSI %u\n",
10641 				ch->seid);
10642 			return ret;
10643 		}
10644 	}
10645 	return 0;
10646 }
10647 
10648 /**
10649  * i40e_prep_for_reset - prep for the core to reset
10650  * @pf: board private structure
10651  *
10652  * Close up the VFs and other things in prep for PF Reset.
10653   **/
10654 static void i40e_prep_for_reset(struct i40e_pf *pf)
10655 {
10656 	struct i40e_hw *hw = &pf->hw;
10657 	i40e_status ret = 0;
10658 	u32 v;
10659 
10660 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10661 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10662 		return;
10663 	if (i40e_check_asq_alive(&pf->hw))
10664 		i40e_vc_notify_reset(pf);
10665 
10666 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10667 
10668 	/* quiesce the VSIs and their queues that are not already DOWN */
10669 	i40e_pf_quiesce_all_vsi(pf);
10670 
10671 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10672 		if (pf->vsi[v])
10673 			pf->vsi[v]->seid = 0;
10674 	}
10675 
10676 	i40e_shutdown_adminq(&pf->hw);
10677 
10678 	/* call shutdown HMC */
10679 	if (hw->hmc.hmc_obj) {
10680 		ret = i40e_shutdown_lan_hmc(hw);
10681 		if (ret)
10682 			dev_warn(&pf->pdev->dev,
10683 				 "shutdown_lan_hmc failed: %d\n", ret);
10684 	}
10685 
10686 	/* Save the current PTP time so that we can restore the time after the
10687 	 * reset completes.
10688 	 */
10689 	i40e_ptp_save_hw_time(pf);
10690 }
10691 
10692 /**
10693  * i40e_send_version - update firmware with driver version
10694  * @pf: PF struct
10695  */
10696 static void i40e_send_version(struct i40e_pf *pf)
10697 {
10698 	struct i40e_driver_version dv;
10699 
10700 	dv.major_version = 0xff;
10701 	dv.minor_version = 0xff;
10702 	dv.build_version = 0xff;
10703 	dv.subbuild_version = 0;
10704 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10705 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10706 }
10707 
10708 /**
10709  * i40e_get_oem_version - get OEM specific version information
10710  * @hw: pointer to the hardware structure
10711  **/
10712 static void i40e_get_oem_version(struct i40e_hw *hw)
10713 {
10714 	u16 block_offset = 0xffff;
10715 	u16 block_length = 0;
10716 	u16 capabilities = 0;
10717 	u16 gen_snap = 0;
10718 	u16 release = 0;
10719 
10720 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10721 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10722 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10723 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10724 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10725 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10726 #define I40E_NVM_OEM_LENGTH			3
10727 
10728 	/* Check if pointer to OEM version block is valid. */
10729 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10730 	if (block_offset == 0xffff)
10731 		return;
10732 
10733 	/* Check if OEM version block has correct length. */
10734 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10735 			   &block_length);
10736 	if (block_length < I40E_NVM_OEM_LENGTH)
10737 		return;
10738 
10739 	/* Check if OEM version format is as expected. */
10740 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10741 			   &capabilities);
10742 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10743 		return;
10744 
10745 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10746 			   &gen_snap);
10747 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10748 			   &release);
10749 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10750 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10751 }
10752 
10753 /**
10754  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10755  * @pf: board private structure
10756  **/
10757 static int i40e_reset(struct i40e_pf *pf)
10758 {
10759 	struct i40e_hw *hw = &pf->hw;
10760 	i40e_status ret;
10761 
10762 	ret = i40e_pf_reset(hw);
10763 	if (ret) {
10764 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10765 		set_bit(__I40E_RESET_FAILED, pf->state);
10766 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10767 	} else {
10768 		pf->pfr_count++;
10769 	}
10770 	return ret;
10771 }
10772 
10773 /**
10774  * i40e_rebuild - rebuild using a saved config
10775  * @pf: board private structure
10776  * @reinit: if the Main VSI needs to re-initialized.
10777  * @lock_acquired: indicates whether or not the lock has been acquired
10778  * before this function was called.
10779  **/
10780 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10781 {
10782 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10783 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10784 	struct i40e_hw *hw = &pf->hw;
10785 	i40e_status ret;
10786 	u32 val;
10787 	int v;
10788 
10789 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10790 	    is_recovery_mode_reported)
10791 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10792 
10793 	if (test_bit(__I40E_DOWN, pf->state) &&
10794 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10795 		goto clear_recovery;
10796 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10797 
10798 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10799 	ret = i40e_init_adminq(&pf->hw);
10800 	if (ret) {
10801 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10802 			 i40e_stat_str(&pf->hw, ret),
10803 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10804 		goto clear_recovery;
10805 	}
10806 	i40e_get_oem_version(&pf->hw);
10807 
10808 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10809 		/* The following delay is necessary for firmware update. */
10810 		mdelay(1000);
10811 	}
10812 
10813 	/* re-verify the eeprom if we just had an EMP reset */
10814 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10815 		i40e_verify_eeprom(pf);
10816 
10817 	/* if we are going out of or into recovery mode we have to act
10818 	 * accordingly with regard to resources initialization
10819 	 * and deinitialization
10820 	 */
10821 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10822 		if (i40e_get_capabilities(pf,
10823 					  i40e_aqc_opc_list_func_capabilities))
10824 			goto end_unlock;
10825 
10826 		if (is_recovery_mode_reported) {
10827 			/* we're staying in recovery mode so we'll reinitialize
10828 			 * misc vector here
10829 			 */
10830 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10831 				goto end_unlock;
10832 		} else {
10833 			if (!lock_acquired)
10834 				rtnl_lock();
10835 			/* we're going out of recovery mode so we'll free
10836 			 * the IRQ allocated specifically for recovery mode
10837 			 * and restore the interrupt scheme
10838 			 */
10839 			free_irq(pf->pdev->irq, pf);
10840 			i40e_clear_interrupt_scheme(pf);
10841 			if (i40e_restore_interrupt_scheme(pf))
10842 				goto end_unlock;
10843 		}
10844 
10845 		/* tell the firmware that we're starting */
10846 		i40e_send_version(pf);
10847 
10848 		/* bail out in case recovery mode was detected, as there is
10849 		 * no need for further configuration.
10850 		 */
10851 		goto end_unlock;
10852 	}
10853 
10854 	i40e_clear_pxe_mode(hw);
10855 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10856 	if (ret)
10857 		goto end_core_reset;
10858 
10859 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10860 				hw->func_caps.num_rx_qp, 0, 0);
10861 	if (ret) {
10862 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10863 		goto end_core_reset;
10864 	}
10865 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10866 	if (ret) {
10867 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10868 		goto end_core_reset;
10869 	}
10870 
10871 #ifdef CONFIG_I40E_DCB
10872 	/* Enable FW to write a default DCB config on link-up
10873 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10874 	 * is not supported with new link speed
10875 	 */
10876 	if (i40e_is_tc_mqprio_enabled(pf)) {
10877 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10878 	} else {
10879 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10880 		    (hw->phy.link_info.link_speed &
10881 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10882 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10883 			dev_warn(&pf->pdev->dev,
10884 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10885 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10886 		} else {
10887 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10888 			ret = i40e_init_pf_dcb(pf);
10889 			if (ret) {
10890 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10891 					 ret);
10892 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10893 				/* Continue without DCB enabled */
10894 			}
10895 		}
10896 	}
10897 
10898 #endif /* CONFIG_I40E_DCB */
10899 	if (!lock_acquired)
10900 		rtnl_lock();
10901 	ret = i40e_setup_pf_switch(pf, reinit, true);
10902 	if (ret)
10903 		goto end_unlock;
10904 
10905 	/* The driver only wants link up/down and module qualification
10906 	 * reports from firmware.  Note the negative logic.
10907 	 */
10908 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10909 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10910 					 I40E_AQ_EVENT_MEDIA_NA |
10911 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10912 	if (ret)
10913 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10914 			 i40e_stat_str(&pf->hw, ret),
10915 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10916 
10917 	/* Rebuild the VSIs and VEBs that existed before reset.
10918 	 * They are still in our local switch element arrays, so only
10919 	 * need to rebuild the switch model in the HW.
10920 	 *
10921 	 * If there were VEBs but the reconstitution failed, we'll try
10922 	 * to recover minimal use by getting the basic PF VSI working.
10923 	 */
10924 	if (vsi->uplink_seid != pf->mac_seid) {
10925 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10926 		/* find the one VEB connected to the MAC, and find orphans */
10927 		for (v = 0; v < I40E_MAX_VEB; v++) {
10928 			if (!pf->veb[v])
10929 				continue;
10930 
10931 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10932 			    pf->veb[v]->uplink_seid == 0) {
10933 				ret = i40e_reconstitute_veb(pf->veb[v]);
10934 
10935 				if (!ret)
10936 					continue;
10937 
10938 				/* If Main VEB failed, we're in deep doodoo,
10939 				 * so give up rebuilding the switch and set up
10940 				 * for minimal rebuild of PF VSI.
10941 				 * If orphan failed, we'll report the error
10942 				 * but try to keep going.
10943 				 */
10944 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10945 					dev_info(&pf->pdev->dev,
10946 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10947 						 ret);
10948 					vsi->uplink_seid = pf->mac_seid;
10949 					break;
10950 				} else if (pf->veb[v]->uplink_seid == 0) {
10951 					dev_info(&pf->pdev->dev,
10952 						 "rebuild of orphan VEB failed: %d\n",
10953 						 ret);
10954 				}
10955 			}
10956 		}
10957 	}
10958 
10959 	if (vsi->uplink_seid == pf->mac_seid) {
10960 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10961 		/* no VEB, so rebuild only the Main VSI */
10962 		ret = i40e_add_vsi(vsi);
10963 		if (ret) {
10964 			dev_info(&pf->pdev->dev,
10965 				 "rebuild of Main VSI failed: %d\n", ret);
10966 			goto end_unlock;
10967 		}
10968 	}
10969 
10970 	if (vsi->mqprio_qopt.max_rate[0]) {
10971 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10972 		u64 credits = 0;
10973 
10974 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10975 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10976 		if (ret)
10977 			goto end_unlock;
10978 
10979 		credits = max_tx_rate;
10980 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10981 		dev_dbg(&vsi->back->pdev->dev,
10982 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10983 			max_tx_rate,
10984 			credits,
10985 			vsi->seid);
10986 	}
10987 
10988 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10989 	if (ret)
10990 		goto end_unlock;
10991 
10992 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10993 	 * for this main VSI if they exist
10994 	 */
10995 	ret = i40e_rebuild_channels(vsi);
10996 	if (ret)
10997 		goto end_unlock;
10998 
10999 	/* Reconfigure hardware for allowing smaller MSS in the case
11000 	 * of TSO, so that we avoid the MDD being fired and causing
11001 	 * a reset in the case of small MSS+TSO.
11002 	 */
11003 #define I40E_REG_MSS          0x000E64DC
11004 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11005 #define I40E_64BYTE_MSS       0x400000
11006 	val = rd32(hw, I40E_REG_MSS);
11007 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11008 		val &= ~I40E_REG_MSS_MIN_MASK;
11009 		val |= I40E_64BYTE_MSS;
11010 		wr32(hw, I40E_REG_MSS, val);
11011 	}
11012 
11013 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11014 		msleep(75);
11015 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11016 		if (ret)
11017 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11018 				 i40e_stat_str(&pf->hw, ret),
11019 				 i40e_aq_str(&pf->hw,
11020 					     pf->hw.aq.asq_last_status));
11021 	}
11022 	/* reinit the misc interrupt */
11023 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11024 		ret = i40e_setup_misc_vector(pf);
11025 
11026 	/* Add a filter to drop all Flow control frames from any VSI from being
11027 	 * transmitted. By doing so we stop a malicious VF from sending out
11028 	 * PAUSE or PFC frames and potentially controlling traffic for other
11029 	 * PF/VF VSIs.
11030 	 * The FW can still send Flow control frames if enabled.
11031 	 */
11032 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11033 						       pf->main_vsi_seid);
11034 
11035 	/* restart the VSIs that were rebuilt and running before the reset */
11036 	i40e_pf_unquiesce_all_vsi(pf);
11037 
11038 	/* Release the RTNL lock before we start resetting VFs */
11039 	if (!lock_acquired)
11040 		rtnl_unlock();
11041 
11042 	/* Restore promiscuous settings */
11043 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11044 	if (ret)
11045 		dev_warn(&pf->pdev->dev,
11046 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
11047 			 pf->cur_promisc ? "on" : "off",
11048 			 i40e_stat_str(&pf->hw, ret),
11049 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11050 
11051 	i40e_reset_all_vfs(pf, true);
11052 
11053 	/* tell the firmware that we're starting */
11054 	i40e_send_version(pf);
11055 
11056 	/* We've already released the lock, so don't do it again */
11057 	goto end_core_reset;
11058 
11059 end_unlock:
11060 	if (!lock_acquired)
11061 		rtnl_unlock();
11062 end_core_reset:
11063 	clear_bit(__I40E_RESET_FAILED, pf->state);
11064 clear_recovery:
11065 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11066 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11067 }
11068 
11069 /**
11070  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11071  * @pf: board private structure
11072  * @reinit: if the Main VSI needs to re-initialized.
11073  * @lock_acquired: indicates whether or not the lock has been acquired
11074  * before this function was called.
11075  **/
11076 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11077 				   bool lock_acquired)
11078 {
11079 	int ret;
11080 
11081 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11082 		return;
11083 	/* Now we wait for GRST to settle out.
11084 	 * We don't have to delete the VEBs or VSIs from the hw switch
11085 	 * because the reset will make them disappear.
11086 	 */
11087 	ret = i40e_reset(pf);
11088 	if (!ret)
11089 		i40e_rebuild(pf, reinit, lock_acquired);
11090 }
11091 
11092 /**
11093  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11094  * @pf: board private structure
11095  *
11096  * Close up the VFs and other things in prep for a Core Reset,
11097  * then get ready to rebuild the world.
11098  * @lock_acquired: indicates whether or not the lock has been acquired
11099  * before this function was called.
11100  **/
11101 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11102 {
11103 	i40e_prep_for_reset(pf);
11104 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11105 }
11106 
11107 /**
11108  * i40e_handle_mdd_event
11109  * @pf: pointer to the PF structure
11110  *
11111  * Called from the MDD irq handler to identify possibly malicious vfs
11112  **/
11113 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11114 {
11115 	struct i40e_hw *hw = &pf->hw;
11116 	bool mdd_detected = false;
11117 	struct i40e_vf *vf;
11118 	u32 reg;
11119 	int i;
11120 
11121 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11122 		return;
11123 
11124 	/* find what triggered the MDD event */
11125 	reg = rd32(hw, I40E_GL_MDET_TX);
11126 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11127 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11128 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11129 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11130 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11131 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11132 				I40E_GL_MDET_TX_EVENT_SHIFT;
11133 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11134 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11135 				pf->hw.func_caps.base_queue;
11136 		if (netif_msg_tx_err(pf))
11137 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11138 				 event, queue, pf_num, vf_num);
11139 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11140 		mdd_detected = true;
11141 	}
11142 	reg = rd32(hw, I40E_GL_MDET_RX);
11143 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11144 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11145 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11146 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11147 				I40E_GL_MDET_RX_EVENT_SHIFT;
11148 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11149 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11150 				pf->hw.func_caps.base_queue;
11151 		if (netif_msg_rx_err(pf))
11152 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11153 				 event, queue, func);
11154 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11155 		mdd_detected = true;
11156 	}
11157 
11158 	if (mdd_detected) {
11159 		reg = rd32(hw, I40E_PF_MDET_TX);
11160 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11161 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11162 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11163 		}
11164 		reg = rd32(hw, I40E_PF_MDET_RX);
11165 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11166 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11167 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11168 		}
11169 	}
11170 
11171 	/* see if one of the VFs needs its hand slapped */
11172 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11173 		vf = &(pf->vf[i]);
11174 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11175 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11176 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11177 			vf->num_mdd_events++;
11178 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11179 				 i);
11180 			dev_info(&pf->pdev->dev,
11181 				 "Use PF Control I/F to re-enable the VF\n");
11182 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11183 		}
11184 
11185 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11186 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11187 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11188 			vf->num_mdd_events++;
11189 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11190 				 i);
11191 			dev_info(&pf->pdev->dev,
11192 				 "Use PF Control I/F to re-enable the VF\n");
11193 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11194 		}
11195 	}
11196 
11197 	/* re-enable mdd interrupt cause */
11198 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11199 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11200 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11201 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11202 	i40e_flush(hw);
11203 }
11204 
11205 /**
11206  * i40e_service_task - Run the driver's async subtasks
11207  * @work: pointer to work_struct containing our data
11208  **/
11209 static void i40e_service_task(struct work_struct *work)
11210 {
11211 	struct i40e_pf *pf = container_of(work,
11212 					  struct i40e_pf,
11213 					  service_task);
11214 	unsigned long start_time = jiffies;
11215 
11216 	/* don't bother with service tasks if a reset is in progress */
11217 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11218 	    test_bit(__I40E_SUSPENDED, pf->state))
11219 		return;
11220 
11221 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11222 		return;
11223 
11224 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11225 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11226 		i40e_sync_filters_subtask(pf);
11227 		i40e_reset_subtask(pf);
11228 		i40e_handle_mdd_event(pf);
11229 		i40e_vc_process_vflr_event(pf);
11230 		i40e_watchdog_subtask(pf);
11231 		i40e_fdir_reinit_subtask(pf);
11232 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11233 			/* Client subtask will reopen next time through. */
11234 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11235 							   true);
11236 		} else {
11237 			i40e_client_subtask(pf);
11238 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11239 					       pf->state))
11240 				i40e_notify_client_of_l2_param_changes(
11241 								pf->vsi[pf->lan_vsi]);
11242 		}
11243 		i40e_sync_filters_subtask(pf);
11244 	} else {
11245 		i40e_reset_subtask(pf);
11246 	}
11247 
11248 	i40e_clean_adminq_subtask(pf);
11249 
11250 	/* flush memory to make sure state is correct before next watchdog */
11251 	smp_mb__before_atomic();
11252 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11253 
11254 	/* If the tasks have taken longer than one timer cycle or there
11255 	 * is more work to be done, reschedule the service task now
11256 	 * rather than wait for the timer to tick again.
11257 	 */
11258 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11259 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11260 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11261 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11262 		i40e_service_event_schedule(pf);
11263 }
11264 
11265 /**
11266  * i40e_service_timer - timer callback
11267  * @t: timer list pointer
11268  **/
11269 static void i40e_service_timer(struct timer_list *t)
11270 {
11271 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11272 
11273 	mod_timer(&pf->service_timer,
11274 		  round_jiffies(jiffies + pf->service_timer_period));
11275 	i40e_service_event_schedule(pf);
11276 }
11277 
11278 /**
11279  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11280  * @vsi: the VSI being configured
11281  **/
11282 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11283 {
11284 	struct i40e_pf *pf = vsi->back;
11285 
11286 	switch (vsi->type) {
11287 	case I40E_VSI_MAIN:
11288 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11289 		if (!vsi->num_tx_desc)
11290 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11291 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11292 		if (!vsi->num_rx_desc)
11293 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11294 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11295 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11296 			vsi->num_q_vectors = pf->num_lan_msix;
11297 		else
11298 			vsi->num_q_vectors = 1;
11299 
11300 		break;
11301 
11302 	case I40E_VSI_FDIR:
11303 		vsi->alloc_queue_pairs = 1;
11304 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11305 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11306 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11307 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11308 		vsi->num_q_vectors = pf->num_fdsb_msix;
11309 		break;
11310 
11311 	case I40E_VSI_VMDQ2:
11312 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11313 		if (!vsi->num_tx_desc)
11314 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11315 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11316 		if (!vsi->num_rx_desc)
11317 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11318 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11319 		vsi->num_q_vectors = pf->num_vmdq_msix;
11320 		break;
11321 
11322 	case I40E_VSI_SRIOV:
11323 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11324 		if (!vsi->num_tx_desc)
11325 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11326 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11327 		if (!vsi->num_rx_desc)
11328 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11329 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11330 		break;
11331 
11332 	default:
11333 		WARN_ON(1);
11334 		return -ENODATA;
11335 	}
11336 
11337 	if (is_kdump_kernel()) {
11338 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11339 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11340 	}
11341 
11342 	return 0;
11343 }
11344 
11345 /**
11346  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11347  * @vsi: VSI pointer
11348  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11349  *
11350  * On error: returns error code (negative)
11351  * On success: returns 0
11352  **/
11353 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11354 {
11355 	struct i40e_ring **next_rings;
11356 	int size;
11357 	int ret = 0;
11358 
11359 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11360 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11361 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11362 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11363 	if (!vsi->tx_rings)
11364 		return -ENOMEM;
11365 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11366 	if (i40e_enabled_xdp_vsi(vsi)) {
11367 		vsi->xdp_rings = next_rings;
11368 		next_rings += vsi->alloc_queue_pairs;
11369 	}
11370 	vsi->rx_rings = next_rings;
11371 
11372 	if (alloc_qvectors) {
11373 		/* allocate memory for q_vector pointers */
11374 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11375 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11376 		if (!vsi->q_vectors) {
11377 			ret = -ENOMEM;
11378 			goto err_vectors;
11379 		}
11380 	}
11381 	return ret;
11382 
11383 err_vectors:
11384 	kfree(vsi->tx_rings);
11385 	return ret;
11386 }
11387 
11388 /**
11389  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11390  * @pf: board private structure
11391  * @type: type of VSI
11392  *
11393  * On error: returns error code (negative)
11394  * On success: returns vsi index in PF (positive)
11395  **/
11396 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11397 {
11398 	int ret = -ENODEV;
11399 	struct i40e_vsi *vsi;
11400 	int vsi_idx;
11401 	int i;
11402 
11403 	/* Need to protect the allocation of the VSIs at the PF level */
11404 	mutex_lock(&pf->switch_mutex);
11405 
11406 	/* VSI list may be fragmented if VSI creation/destruction has
11407 	 * been happening.  We can afford to do a quick scan to look
11408 	 * for any free VSIs in the list.
11409 	 *
11410 	 * find next empty vsi slot, looping back around if necessary
11411 	 */
11412 	i = pf->next_vsi;
11413 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11414 		i++;
11415 	if (i >= pf->num_alloc_vsi) {
11416 		i = 0;
11417 		while (i < pf->next_vsi && pf->vsi[i])
11418 			i++;
11419 	}
11420 
11421 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11422 		vsi_idx = i;             /* Found one! */
11423 	} else {
11424 		ret = -ENODEV;
11425 		goto unlock_pf;  /* out of VSI slots! */
11426 	}
11427 	pf->next_vsi = ++i;
11428 
11429 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11430 	if (!vsi) {
11431 		ret = -ENOMEM;
11432 		goto unlock_pf;
11433 	}
11434 	vsi->type = type;
11435 	vsi->back = pf;
11436 	set_bit(__I40E_VSI_DOWN, vsi->state);
11437 	vsi->flags = 0;
11438 	vsi->idx = vsi_idx;
11439 	vsi->int_rate_limit = 0;
11440 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11441 				pf->rss_table_size : 64;
11442 	vsi->netdev_registered = false;
11443 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11444 	hash_init(vsi->mac_filter_hash);
11445 	vsi->irqs_ready = false;
11446 
11447 	if (type == I40E_VSI_MAIN) {
11448 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11449 		if (!vsi->af_xdp_zc_qps)
11450 			goto err_rings;
11451 	}
11452 
11453 	ret = i40e_set_num_rings_in_vsi(vsi);
11454 	if (ret)
11455 		goto err_rings;
11456 
11457 	ret = i40e_vsi_alloc_arrays(vsi, true);
11458 	if (ret)
11459 		goto err_rings;
11460 
11461 	/* Setup default MSIX irq handler for VSI */
11462 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11463 
11464 	/* Initialize VSI lock */
11465 	spin_lock_init(&vsi->mac_filter_hash_lock);
11466 	pf->vsi[vsi_idx] = vsi;
11467 	ret = vsi_idx;
11468 	goto unlock_pf;
11469 
11470 err_rings:
11471 	bitmap_free(vsi->af_xdp_zc_qps);
11472 	pf->next_vsi = i - 1;
11473 	kfree(vsi);
11474 unlock_pf:
11475 	mutex_unlock(&pf->switch_mutex);
11476 	return ret;
11477 }
11478 
11479 /**
11480  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11481  * @vsi: VSI pointer
11482  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11483  *
11484  * On error: returns error code (negative)
11485  * On success: returns 0
11486  **/
11487 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11488 {
11489 	/* free the ring and vector containers */
11490 	if (free_qvectors) {
11491 		kfree(vsi->q_vectors);
11492 		vsi->q_vectors = NULL;
11493 	}
11494 	kfree(vsi->tx_rings);
11495 	vsi->tx_rings = NULL;
11496 	vsi->rx_rings = NULL;
11497 	vsi->xdp_rings = NULL;
11498 }
11499 
11500 /**
11501  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11502  * and lookup table
11503  * @vsi: Pointer to VSI structure
11504  */
11505 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11506 {
11507 	if (!vsi)
11508 		return;
11509 
11510 	kfree(vsi->rss_hkey_user);
11511 	vsi->rss_hkey_user = NULL;
11512 
11513 	kfree(vsi->rss_lut_user);
11514 	vsi->rss_lut_user = NULL;
11515 }
11516 
11517 /**
11518  * i40e_vsi_clear - Deallocate the VSI provided
11519  * @vsi: the VSI being un-configured
11520  **/
11521 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11522 {
11523 	struct i40e_pf *pf;
11524 
11525 	if (!vsi)
11526 		return 0;
11527 
11528 	if (!vsi->back)
11529 		goto free_vsi;
11530 	pf = vsi->back;
11531 
11532 	mutex_lock(&pf->switch_mutex);
11533 	if (!pf->vsi[vsi->idx]) {
11534 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11535 			vsi->idx, vsi->idx, vsi->type);
11536 		goto unlock_vsi;
11537 	}
11538 
11539 	if (pf->vsi[vsi->idx] != vsi) {
11540 		dev_err(&pf->pdev->dev,
11541 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11542 			pf->vsi[vsi->idx]->idx,
11543 			pf->vsi[vsi->idx]->type,
11544 			vsi->idx, vsi->type);
11545 		goto unlock_vsi;
11546 	}
11547 
11548 	/* updates the PF for this cleared vsi */
11549 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11550 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11551 
11552 	bitmap_free(vsi->af_xdp_zc_qps);
11553 	i40e_vsi_free_arrays(vsi, true);
11554 	i40e_clear_rss_config_user(vsi);
11555 
11556 	pf->vsi[vsi->idx] = NULL;
11557 	if (vsi->idx < pf->next_vsi)
11558 		pf->next_vsi = vsi->idx;
11559 
11560 unlock_vsi:
11561 	mutex_unlock(&pf->switch_mutex);
11562 free_vsi:
11563 	kfree(vsi);
11564 
11565 	return 0;
11566 }
11567 
11568 /**
11569  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11570  * @vsi: the VSI being cleaned
11571  **/
11572 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11573 {
11574 	int i;
11575 
11576 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11577 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11578 			kfree_rcu(vsi->tx_rings[i], rcu);
11579 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11580 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11581 			if (vsi->xdp_rings)
11582 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11583 		}
11584 	}
11585 }
11586 
11587 /**
11588  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11589  * @vsi: the VSI being configured
11590  **/
11591 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11592 {
11593 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11594 	struct i40e_pf *pf = vsi->back;
11595 	struct i40e_ring *ring;
11596 
11597 	/* Set basic values in the rings to be used later during open() */
11598 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11599 		/* allocate space for both Tx and Rx in one shot */
11600 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11601 		if (!ring)
11602 			goto err_out;
11603 
11604 		ring->queue_index = i;
11605 		ring->reg_idx = vsi->base_queue + i;
11606 		ring->ring_active = false;
11607 		ring->vsi = vsi;
11608 		ring->netdev = vsi->netdev;
11609 		ring->dev = &pf->pdev->dev;
11610 		ring->count = vsi->num_tx_desc;
11611 		ring->size = 0;
11612 		ring->dcb_tc = 0;
11613 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11614 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11615 		ring->itr_setting = pf->tx_itr_default;
11616 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11617 
11618 		if (!i40e_enabled_xdp_vsi(vsi))
11619 			goto setup_rx;
11620 
11621 		ring->queue_index = vsi->alloc_queue_pairs + i;
11622 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11623 		ring->ring_active = false;
11624 		ring->vsi = vsi;
11625 		ring->netdev = NULL;
11626 		ring->dev = &pf->pdev->dev;
11627 		ring->count = vsi->num_tx_desc;
11628 		ring->size = 0;
11629 		ring->dcb_tc = 0;
11630 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11631 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11632 		set_ring_xdp(ring);
11633 		ring->itr_setting = pf->tx_itr_default;
11634 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11635 
11636 setup_rx:
11637 		ring->queue_index = i;
11638 		ring->reg_idx = vsi->base_queue + i;
11639 		ring->ring_active = false;
11640 		ring->vsi = vsi;
11641 		ring->netdev = vsi->netdev;
11642 		ring->dev = &pf->pdev->dev;
11643 		ring->count = vsi->num_rx_desc;
11644 		ring->size = 0;
11645 		ring->dcb_tc = 0;
11646 		ring->itr_setting = pf->rx_itr_default;
11647 		WRITE_ONCE(vsi->rx_rings[i], ring);
11648 	}
11649 
11650 	return 0;
11651 
11652 err_out:
11653 	i40e_vsi_clear_rings(vsi);
11654 	return -ENOMEM;
11655 }
11656 
11657 /**
11658  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11659  * @pf: board private structure
11660  * @vectors: the number of MSI-X vectors to request
11661  *
11662  * Returns the number of vectors reserved, or error
11663  **/
11664 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11665 {
11666 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11667 					I40E_MIN_MSIX, vectors);
11668 	if (vectors < 0) {
11669 		dev_info(&pf->pdev->dev,
11670 			 "MSI-X vector reservation failed: %d\n", vectors);
11671 		vectors = 0;
11672 	}
11673 
11674 	return vectors;
11675 }
11676 
11677 /**
11678  * i40e_init_msix - Setup the MSIX capability
11679  * @pf: board private structure
11680  *
11681  * Work with the OS to set up the MSIX vectors needed.
11682  *
11683  * Returns the number of vectors reserved or negative on failure
11684  **/
11685 static int i40e_init_msix(struct i40e_pf *pf)
11686 {
11687 	struct i40e_hw *hw = &pf->hw;
11688 	int cpus, extra_vectors;
11689 	int vectors_left;
11690 	int v_budget, i;
11691 	int v_actual;
11692 	int iwarp_requested = 0;
11693 
11694 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11695 		return -ENODEV;
11696 
11697 	/* The number of vectors we'll request will be comprised of:
11698 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11699 	 *   - The number of LAN queue pairs
11700 	 *	- Queues being used for RSS.
11701 	 *		We don't need as many as max_rss_size vectors.
11702 	 *		use rss_size instead in the calculation since that
11703 	 *		is governed by number of cpus in the system.
11704 	 *	- assumes symmetric Tx/Rx pairing
11705 	 *   - The number of VMDq pairs
11706 	 *   - The CPU count within the NUMA node if iWARP is enabled
11707 	 * Once we count this up, try the request.
11708 	 *
11709 	 * If we can't get what we want, we'll simplify to nearly nothing
11710 	 * and try again.  If that still fails, we punt.
11711 	 */
11712 	vectors_left = hw->func_caps.num_msix_vectors;
11713 	v_budget = 0;
11714 
11715 	/* reserve one vector for miscellaneous handler */
11716 	if (vectors_left) {
11717 		v_budget++;
11718 		vectors_left--;
11719 	}
11720 
11721 	/* reserve some vectors for the main PF traffic queues. Initially we
11722 	 * only reserve at most 50% of the available vectors, in the case that
11723 	 * the number of online CPUs is large. This ensures that we can enable
11724 	 * extra features as well. Once we've enabled the other features, we
11725 	 * will use any remaining vectors to reach as close as we can to the
11726 	 * number of online CPUs.
11727 	 */
11728 	cpus = num_online_cpus();
11729 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11730 	vectors_left -= pf->num_lan_msix;
11731 
11732 	/* reserve one vector for sideband flow director */
11733 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11734 		if (vectors_left) {
11735 			pf->num_fdsb_msix = 1;
11736 			v_budget++;
11737 			vectors_left--;
11738 		} else {
11739 			pf->num_fdsb_msix = 0;
11740 		}
11741 	}
11742 
11743 	/* can we reserve enough for iWARP? */
11744 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11745 		iwarp_requested = pf->num_iwarp_msix;
11746 
11747 		if (!vectors_left)
11748 			pf->num_iwarp_msix = 0;
11749 		else if (vectors_left < pf->num_iwarp_msix)
11750 			pf->num_iwarp_msix = 1;
11751 		v_budget += pf->num_iwarp_msix;
11752 		vectors_left -= pf->num_iwarp_msix;
11753 	}
11754 
11755 	/* any vectors left over go for VMDq support */
11756 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11757 		if (!vectors_left) {
11758 			pf->num_vmdq_msix = 0;
11759 			pf->num_vmdq_qps = 0;
11760 		} else {
11761 			int vmdq_vecs_wanted =
11762 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11763 			int vmdq_vecs =
11764 				min_t(int, vectors_left, vmdq_vecs_wanted);
11765 
11766 			/* if we're short on vectors for what's desired, we limit
11767 			 * the queues per vmdq.  If this is still more than are
11768 			 * available, the user will need to change the number of
11769 			 * queues/vectors used by the PF later with the ethtool
11770 			 * channels command
11771 			 */
11772 			if (vectors_left < vmdq_vecs_wanted) {
11773 				pf->num_vmdq_qps = 1;
11774 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11775 				vmdq_vecs = min_t(int,
11776 						  vectors_left,
11777 						  vmdq_vecs_wanted);
11778 			}
11779 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11780 
11781 			v_budget += vmdq_vecs;
11782 			vectors_left -= vmdq_vecs;
11783 		}
11784 	}
11785 
11786 	/* On systems with a large number of SMP cores, we previously limited
11787 	 * the number of vectors for num_lan_msix to be at most 50% of the
11788 	 * available vectors, to allow for other features. Now, we add back
11789 	 * the remaining vectors. However, we ensure that the total
11790 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11791 	 * calculate the number of vectors we can add without going over the
11792 	 * cap of CPUs. For systems with a small number of CPUs this will be
11793 	 * zero.
11794 	 */
11795 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11796 	pf->num_lan_msix += extra_vectors;
11797 	vectors_left -= extra_vectors;
11798 
11799 	WARN(vectors_left < 0,
11800 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11801 
11802 	v_budget += pf->num_lan_msix;
11803 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11804 				   GFP_KERNEL);
11805 	if (!pf->msix_entries)
11806 		return -ENOMEM;
11807 
11808 	for (i = 0; i < v_budget; i++)
11809 		pf->msix_entries[i].entry = i;
11810 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11811 
11812 	if (v_actual < I40E_MIN_MSIX) {
11813 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11814 		kfree(pf->msix_entries);
11815 		pf->msix_entries = NULL;
11816 		pci_disable_msix(pf->pdev);
11817 		return -ENODEV;
11818 
11819 	} else if (v_actual == I40E_MIN_MSIX) {
11820 		/* Adjust for minimal MSIX use */
11821 		pf->num_vmdq_vsis = 0;
11822 		pf->num_vmdq_qps = 0;
11823 		pf->num_lan_qps = 1;
11824 		pf->num_lan_msix = 1;
11825 
11826 	} else if (v_actual != v_budget) {
11827 		/* If we have limited resources, we will start with no vectors
11828 		 * for the special features and then allocate vectors to some
11829 		 * of these features based on the policy and at the end disable
11830 		 * the features that did not get any vectors.
11831 		 */
11832 		int vec;
11833 
11834 		dev_info(&pf->pdev->dev,
11835 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11836 			 v_actual, v_budget);
11837 		/* reserve the misc vector */
11838 		vec = v_actual - 1;
11839 
11840 		/* Scale vector usage down */
11841 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11842 		pf->num_vmdq_vsis = 1;
11843 		pf->num_vmdq_qps = 1;
11844 
11845 		/* partition out the remaining vectors */
11846 		switch (vec) {
11847 		case 2:
11848 			pf->num_lan_msix = 1;
11849 			break;
11850 		case 3:
11851 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11852 				pf->num_lan_msix = 1;
11853 				pf->num_iwarp_msix = 1;
11854 			} else {
11855 				pf->num_lan_msix = 2;
11856 			}
11857 			break;
11858 		default:
11859 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11860 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11861 						 iwarp_requested);
11862 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11863 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11864 			} else {
11865 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11866 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11867 			}
11868 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11869 				pf->num_fdsb_msix = 1;
11870 				vec--;
11871 			}
11872 			pf->num_lan_msix = min_t(int,
11873 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11874 							      pf->num_lan_msix);
11875 			pf->num_lan_qps = pf->num_lan_msix;
11876 			break;
11877 		}
11878 	}
11879 
11880 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11881 	    (pf->num_fdsb_msix == 0)) {
11882 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11883 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11884 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11885 	}
11886 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11887 	    (pf->num_vmdq_msix == 0)) {
11888 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11889 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11890 	}
11891 
11892 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11893 	    (pf->num_iwarp_msix == 0)) {
11894 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11895 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11896 	}
11897 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11898 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11899 		   pf->num_lan_msix,
11900 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11901 		   pf->num_fdsb_msix,
11902 		   pf->num_iwarp_msix);
11903 
11904 	return v_actual;
11905 }
11906 
11907 /**
11908  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11909  * @vsi: the VSI being configured
11910  * @v_idx: index of the vector in the vsi struct
11911  *
11912  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11913  **/
11914 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11915 {
11916 	struct i40e_q_vector *q_vector;
11917 
11918 	/* allocate q_vector */
11919 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11920 	if (!q_vector)
11921 		return -ENOMEM;
11922 
11923 	q_vector->vsi = vsi;
11924 	q_vector->v_idx = v_idx;
11925 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11926 
11927 	if (vsi->netdev)
11928 		netif_napi_add(vsi->netdev, &q_vector->napi,
11929 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11930 
11931 	/* tie q_vector and vsi together */
11932 	vsi->q_vectors[v_idx] = q_vector;
11933 
11934 	return 0;
11935 }
11936 
11937 /**
11938  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11939  * @vsi: the VSI being configured
11940  *
11941  * We allocate one q_vector per queue interrupt.  If allocation fails we
11942  * return -ENOMEM.
11943  **/
11944 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11945 {
11946 	struct i40e_pf *pf = vsi->back;
11947 	int err, v_idx, num_q_vectors;
11948 
11949 	/* if not MSIX, give the one vector only to the LAN VSI */
11950 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11951 		num_q_vectors = vsi->num_q_vectors;
11952 	else if (vsi == pf->vsi[pf->lan_vsi])
11953 		num_q_vectors = 1;
11954 	else
11955 		return -EINVAL;
11956 
11957 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11958 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11959 		if (err)
11960 			goto err_out;
11961 	}
11962 
11963 	return 0;
11964 
11965 err_out:
11966 	while (v_idx--)
11967 		i40e_free_q_vector(vsi, v_idx);
11968 
11969 	return err;
11970 }
11971 
11972 /**
11973  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11974  * @pf: board private structure to initialize
11975  **/
11976 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11977 {
11978 	int vectors = 0;
11979 	ssize_t size;
11980 
11981 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11982 		vectors = i40e_init_msix(pf);
11983 		if (vectors < 0) {
11984 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11985 				       I40E_FLAG_IWARP_ENABLED	|
11986 				       I40E_FLAG_RSS_ENABLED	|
11987 				       I40E_FLAG_DCB_CAPABLE	|
11988 				       I40E_FLAG_DCB_ENABLED	|
11989 				       I40E_FLAG_SRIOV_ENABLED	|
11990 				       I40E_FLAG_FD_SB_ENABLED	|
11991 				       I40E_FLAG_FD_ATR_ENABLED	|
11992 				       I40E_FLAG_VMDQ_ENABLED);
11993 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11994 
11995 			/* rework the queue expectations without MSIX */
11996 			i40e_determine_queue_usage(pf);
11997 		}
11998 	}
11999 
12000 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12001 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12002 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12003 		vectors = pci_enable_msi(pf->pdev);
12004 		if (vectors < 0) {
12005 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12006 				 vectors);
12007 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12008 		}
12009 		vectors = 1;  /* one MSI or Legacy vector */
12010 	}
12011 
12012 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12013 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12014 
12015 	/* set up vector assignment tracking */
12016 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12017 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12018 	if (!pf->irq_pile)
12019 		return -ENOMEM;
12020 
12021 	pf->irq_pile->num_entries = vectors;
12022 
12023 	/* track first vector for misc interrupts, ignore return */
12024 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12025 
12026 	return 0;
12027 }
12028 
12029 /**
12030  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12031  * @pf: private board data structure
12032  *
12033  * Restore the interrupt scheme that was cleared when we suspended the
12034  * device. This should be called during resume to re-allocate the q_vectors
12035  * and reacquire IRQs.
12036  */
12037 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12038 {
12039 	int err, i;
12040 
12041 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12042 	 * scheme. We need to re-enabled them here in order to attempt to
12043 	 * re-acquire the MSI or MSI-X vectors
12044 	 */
12045 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12046 
12047 	err = i40e_init_interrupt_scheme(pf);
12048 	if (err)
12049 		return err;
12050 
12051 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12052 	 * rings together again.
12053 	 */
12054 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12055 		if (pf->vsi[i]) {
12056 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12057 			if (err)
12058 				goto err_unwind;
12059 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12060 		}
12061 	}
12062 
12063 	err = i40e_setup_misc_vector(pf);
12064 	if (err)
12065 		goto err_unwind;
12066 
12067 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12068 		i40e_client_update_msix_info(pf);
12069 
12070 	return 0;
12071 
12072 err_unwind:
12073 	while (i--) {
12074 		if (pf->vsi[i])
12075 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12076 	}
12077 
12078 	return err;
12079 }
12080 
12081 /**
12082  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12083  * non queue events in recovery mode
12084  * @pf: board private structure
12085  *
12086  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12087  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12088  * This is handled differently than in recovery mode since no Tx/Rx resources
12089  * are being allocated.
12090  **/
12091 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12092 {
12093 	int err;
12094 
12095 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12096 		err = i40e_setup_misc_vector(pf);
12097 
12098 		if (err) {
12099 			dev_info(&pf->pdev->dev,
12100 				 "MSI-X misc vector request failed, error %d\n",
12101 				 err);
12102 			return err;
12103 		}
12104 	} else {
12105 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12106 
12107 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12108 				  pf->int_name, pf);
12109 
12110 		if (err) {
12111 			dev_info(&pf->pdev->dev,
12112 				 "MSI/legacy misc vector request failed, error %d\n",
12113 				 err);
12114 			return err;
12115 		}
12116 		i40e_enable_misc_int_causes(pf);
12117 		i40e_irq_dynamic_enable_icr0(pf);
12118 	}
12119 
12120 	return 0;
12121 }
12122 
12123 /**
12124  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12125  * @pf: board private structure
12126  *
12127  * This sets up the handler for MSIX 0, which is used to manage the
12128  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12129  * when in MSI or Legacy interrupt mode.
12130  **/
12131 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12132 {
12133 	struct i40e_hw *hw = &pf->hw;
12134 	int err = 0;
12135 
12136 	/* Only request the IRQ once, the first time through. */
12137 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12138 		err = request_irq(pf->msix_entries[0].vector,
12139 				  i40e_intr, 0, pf->int_name, pf);
12140 		if (err) {
12141 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12142 			dev_info(&pf->pdev->dev,
12143 				 "request_irq for %s failed: %d\n",
12144 				 pf->int_name, err);
12145 			return -EFAULT;
12146 		}
12147 	}
12148 
12149 	i40e_enable_misc_int_causes(pf);
12150 
12151 	/* associate no queues to the misc vector */
12152 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12153 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12154 
12155 	i40e_flush(hw);
12156 
12157 	i40e_irq_dynamic_enable_icr0(pf);
12158 
12159 	return err;
12160 }
12161 
12162 /**
12163  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12164  * @vsi: Pointer to vsi structure
12165  * @seed: Buffter to store the hash keys
12166  * @lut: Buffer to store the lookup table entries
12167  * @lut_size: Size of buffer to store the lookup table entries
12168  *
12169  * Return 0 on success, negative on failure
12170  */
12171 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12172 			   u8 *lut, u16 lut_size)
12173 {
12174 	struct i40e_pf *pf = vsi->back;
12175 	struct i40e_hw *hw = &pf->hw;
12176 	int ret = 0;
12177 
12178 	if (seed) {
12179 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12180 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12181 		if (ret) {
12182 			dev_info(&pf->pdev->dev,
12183 				 "Cannot get RSS key, err %s aq_err %s\n",
12184 				 i40e_stat_str(&pf->hw, ret),
12185 				 i40e_aq_str(&pf->hw,
12186 					     pf->hw.aq.asq_last_status));
12187 			return ret;
12188 		}
12189 	}
12190 
12191 	if (lut) {
12192 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12193 
12194 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12195 		if (ret) {
12196 			dev_info(&pf->pdev->dev,
12197 				 "Cannot get RSS lut, err %s aq_err %s\n",
12198 				 i40e_stat_str(&pf->hw, ret),
12199 				 i40e_aq_str(&pf->hw,
12200 					     pf->hw.aq.asq_last_status));
12201 			return ret;
12202 		}
12203 	}
12204 
12205 	return ret;
12206 }
12207 
12208 /**
12209  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12210  * @vsi: Pointer to vsi structure
12211  * @seed: RSS hash seed
12212  * @lut: Lookup table
12213  * @lut_size: Lookup table size
12214  *
12215  * Returns 0 on success, negative on failure
12216  **/
12217 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12218 			       const u8 *lut, u16 lut_size)
12219 {
12220 	struct i40e_pf *pf = vsi->back;
12221 	struct i40e_hw *hw = &pf->hw;
12222 	u16 vf_id = vsi->vf_id;
12223 	u8 i;
12224 
12225 	/* Fill out hash function seed */
12226 	if (seed) {
12227 		u32 *seed_dw = (u32 *)seed;
12228 
12229 		if (vsi->type == I40E_VSI_MAIN) {
12230 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12231 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12232 		} else if (vsi->type == I40E_VSI_SRIOV) {
12233 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12234 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12235 		} else {
12236 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12237 		}
12238 	}
12239 
12240 	if (lut) {
12241 		u32 *lut_dw = (u32 *)lut;
12242 
12243 		if (vsi->type == I40E_VSI_MAIN) {
12244 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12245 				return -EINVAL;
12246 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12247 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12248 		} else if (vsi->type == I40E_VSI_SRIOV) {
12249 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12250 				return -EINVAL;
12251 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12252 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12253 		} else {
12254 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12255 		}
12256 	}
12257 	i40e_flush(hw);
12258 
12259 	return 0;
12260 }
12261 
12262 /**
12263  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12264  * @vsi: Pointer to VSI structure
12265  * @seed: Buffer to store the keys
12266  * @lut: Buffer to store the lookup table entries
12267  * @lut_size: Size of buffer to store the lookup table entries
12268  *
12269  * Returns 0 on success, negative on failure
12270  */
12271 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12272 			    u8 *lut, u16 lut_size)
12273 {
12274 	struct i40e_pf *pf = vsi->back;
12275 	struct i40e_hw *hw = &pf->hw;
12276 	u16 i;
12277 
12278 	if (seed) {
12279 		u32 *seed_dw = (u32 *)seed;
12280 
12281 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12282 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12283 	}
12284 	if (lut) {
12285 		u32 *lut_dw = (u32 *)lut;
12286 
12287 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12288 			return -EINVAL;
12289 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12290 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12291 	}
12292 
12293 	return 0;
12294 }
12295 
12296 /**
12297  * i40e_config_rss - Configure RSS keys and lut
12298  * @vsi: Pointer to VSI structure
12299  * @seed: RSS hash seed
12300  * @lut: Lookup table
12301  * @lut_size: Lookup table size
12302  *
12303  * Returns 0 on success, negative on failure
12304  */
12305 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12306 {
12307 	struct i40e_pf *pf = vsi->back;
12308 
12309 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12310 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12311 	else
12312 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12313 }
12314 
12315 /**
12316  * i40e_get_rss - Get RSS keys and lut
12317  * @vsi: Pointer to VSI structure
12318  * @seed: Buffer to store the keys
12319  * @lut: Buffer to store the lookup table entries
12320  * @lut_size: Size of buffer to store the lookup table entries
12321  *
12322  * Returns 0 on success, negative on failure
12323  */
12324 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12325 {
12326 	struct i40e_pf *pf = vsi->back;
12327 
12328 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12329 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12330 	else
12331 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12332 }
12333 
12334 /**
12335  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12336  * @pf: Pointer to board private structure
12337  * @lut: Lookup table
12338  * @rss_table_size: Lookup table size
12339  * @rss_size: Range of queue number for hashing
12340  */
12341 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12342 		       u16 rss_table_size, u16 rss_size)
12343 {
12344 	u16 i;
12345 
12346 	for (i = 0; i < rss_table_size; i++)
12347 		lut[i] = i % rss_size;
12348 }
12349 
12350 /**
12351  * i40e_pf_config_rss - Prepare for RSS if used
12352  * @pf: board private structure
12353  **/
12354 static int i40e_pf_config_rss(struct i40e_pf *pf)
12355 {
12356 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12357 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12358 	u8 *lut;
12359 	struct i40e_hw *hw = &pf->hw;
12360 	u32 reg_val;
12361 	u64 hena;
12362 	int ret;
12363 
12364 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12365 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12366 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12367 	hena |= i40e_pf_get_default_rss_hena(pf);
12368 
12369 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12370 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12371 
12372 	/* Determine the RSS table size based on the hardware capabilities */
12373 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12374 	reg_val = (pf->rss_table_size == 512) ?
12375 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12376 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12377 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12378 
12379 	/* Determine the RSS size of the VSI */
12380 	if (!vsi->rss_size) {
12381 		u16 qcount;
12382 		/* If the firmware does something weird during VSI init, we
12383 		 * could end up with zero TCs. Check for that to avoid
12384 		 * divide-by-zero. It probably won't pass traffic, but it also
12385 		 * won't panic.
12386 		 */
12387 		qcount = vsi->num_queue_pairs /
12388 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12389 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12390 	}
12391 	if (!vsi->rss_size)
12392 		return -EINVAL;
12393 
12394 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12395 	if (!lut)
12396 		return -ENOMEM;
12397 
12398 	/* Use user configured lut if there is one, otherwise use default */
12399 	if (vsi->rss_lut_user)
12400 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12401 	else
12402 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12403 
12404 	/* Use user configured hash key if there is one, otherwise
12405 	 * use default.
12406 	 */
12407 	if (vsi->rss_hkey_user)
12408 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12409 	else
12410 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12411 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12412 	kfree(lut);
12413 
12414 	return ret;
12415 }
12416 
12417 /**
12418  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12419  * @pf: board private structure
12420  * @queue_count: the requested queue count for rss.
12421  *
12422  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12423  * count which may be different from the requested queue count.
12424  * Note: expects to be called while under rtnl_lock()
12425  **/
12426 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12427 {
12428 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12429 	int new_rss_size;
12430 
12431 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12432 		return 0;
12433 
12434 	queue_count = min_t(int, queue_count, num_online_cpus());
12435 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12436 
12437 	if (queue_count != vsi->num_queue_pairs) {
12438 		u16 qcount;
12439 
12440 		vsi->req_queue_pairs = queue_count;
12441 		i40e_prep_for_reset(pf);
12442 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12443 			return pf->alloc_rss_size;
12444 
12445 		pf->alloc_rss_size = new_rss_size;
12446 
12447 		i40e_reset_and_rebuild(pf, true, true);
12448 
12449 		/* Discard the user configured hash keys and lut, if less
12450 		 * queues are enabled.
12451 		 */
12452 		if (queue_count < vsi->rss_size) {
12453 			i40e_clear_rss_config_user(vsi);
12454 			dev_dbg(&pf->pdev->dev,
12455 				"discard user configured hash keys and lut\n");
12456 		}
12457 
12458 		/* Reset vsi->rss_size, as number of enabled queues changed */
12459 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12460 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12461 
12462 		i40e_pf_config_rss(pf);
12463 	}
12464 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12465 		 vsi->req_queue_pairs, pf->rss_size_max);
12466 	return pf->alloc_rss_size;
12467 }
12468 
12469 /**
12470  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12471  * @pf: board private structure
12472  **/
12473 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12474 {
12475 	i40e_status status;
12476 	bool min_valid, max_valid;
12477 	u32 max_bw, min_bw;
12478 
12479 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12480 					   &min_valid, &max_valid);
12481 
12482 	if (!status) {
12483 		if (min_valid)
12484 			pf->min_bw = min_bw;
12485 		if (max_valid)
12486 			pf->max_bw = max_bw;
12487 	}
12488 
12489 	return status;
12490 }
12491 
12492 /**
12493  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12494  * @pf: board private structure
12495  **/
12496 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12497 {
12498 	struct i40e_aqc_configure_partition_bw_data bw_data;
12499 	i40e_status status;
12500 
12501 	memset(&bw_data, 0, sizeof(bw_data));
12502 
12503 	/* Set the valid bit for this PF */
12504 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12505 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12506 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12507 
12508 	/* Set the new bandwidths */
12509 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12510 
12511 	return status;
12512 }
12513 
12514 /**
12515  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12516  * @pf: board private structure
12517  **/
12518 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12519 {
12520 	/* Commit temporary BW setting to permanent NVM image */
12521 	enum i40e_admin_queue_err last_aq_status;
12522 	i40e_status ret;
12523 	u16 nvm_word;
12524 
12525 	if (pf->hw.partition_id != 1) {
12526 		dev_info(&pf->pdev->dev,
12527 			 "Commit BW only works on partition 1! This is partition %d",
12528 			 pf->hw.partition_id);
12529 		ret = I40E_NOT_SUPPORTED;
12530 		goto bw_commit_out;
12531 	}
12532 
12533 	/* Acquire NVM for read access */
12534 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12535 	last_aq_status = pf->hw.aq.asq_last_status;
12536 	if (ret) {
12537 		dev_info(&pf->pdev->dev,
12538 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12539 			 i40e_stat_str(&pf->hw, ret),
12540 			 i40e_aq_str(&pf->hw, last_aq_status));
12541 		goto bw_commit_out;
12542 	}
12543 
12544 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12545 	ret = i40e_aq_read_nvm(&pf->hw,
12546 			       I40E_SR_NVM_CONTROL_WORD,
12547 			       0x10, sizeof(nvm_word), &nvm_word,
12548 			       false, NULL);
12549 	/* Save off last admin queue command status before releasing
12550 	 * the NVM
12551 	 */
12552 	last_aq_status = pf->hw.aq.asq_last_status;
12553 	i40e_release_nvm(&pf->hw);
12554 	if (ret) {
12555 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12556 			 i40e_stat_str(&pf->hw, ret),
12557 			 i40e_aq_str(&pf->hw, last_aq_status));
12558 		goto bw_commit_out;
12559 	}
12560 
12561 	/* Wait a bit for NVM release to complete */
12562 	msleep(50);
12563 
12564 	/* Acquire NVM for write access */
12565 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12566 	last_aq_status = pf->hw.aq.asq_last_status;
12567 	if (ret) {
12568 		dev_info(&pf->pdev->dev,
12569 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12570 			 i40e_stat_str(&pf->hw, ret),
12571 			 i40e_aq_str(&pf->hw, last_aq_status));
12572 		goto bw_commit_out;
12573 	}
12574 	/* Write it back out unchanged to initiate update NVM,
12575 	 * which will force a write of the shadow (alt) RAM to
12576 	 * the NVM - thus storing the bandwidth values permanently.
12577 	 */
12578 	ret = i40e_aq_update_nvm(&pf->hw,
12579 				 I40E_SR_NVM_CONTROL_WORD,
12580 				 0x10, sizeof(nvm_word),
12581 				 &nvm_word, true, 0, NULL);
12582 	/* Save off last admin queue command status before releasing
12583 	 * the NVM
12584 	 */
12585 	last_aq_status = pf->hw.aq.asq_last_status;
12586 	i40e_release_nvm(&pf->hw);
12587 	if (ret)
12588 		dev_info(&pf->pdev->dev,
12589 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12590 			 i40e_stat_str(&pf->hw, ret),
12591 			 i40e_aq_str(&pf->hw, last_aq_status));
12592 bw_commit_out:
12593 
12594 	return ret;
12595 }
12596 
12597 /**
12598  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12599  * if total port shutdown feature is enabled for this PF
12600  * @pf: board private structure
12601  **/
12602 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12603 {
12604 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12605 #define I40E_FEATURES_ENABLE_PTR		0x2A
12606 #define I40E_CURRENT_SETTING_PTR		0x2B
12607 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12608 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12609 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12610 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12611 	i40e_status read_status = I40E_SUCCESS;
12612 	u16 sr_emp_sr_settings_ptr = 0;
12613 	u16 features_enable = 0;
12614 	u16 link_behavior = 0;
12615 	bool ret = false;
12616 
12617 	read_status = i40e_read_nvm_word(&pf->hw,
12618 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12619 					 &sr_emp_sr_settings_ptr);
12620 	if (read_status)
12621 		goto err_nvm;
12622 	read_status = i40e_read_nvm_word(&pf->hw,
12623 					 sr_emp_sr_settings_ptr +
12624 					 I40E_FEATURES_ENABLE_PTR,
12625 					 &features_enable);
12626 	if (read_status)
12627 		goto err_nvm;
12628 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12629 		read_status = i40e_read_nvm_module_data(&pf->hw,
12630 							I40E_SR_EMP_SR_SETTINGS_PTR,
12631 							I40E_CURRENT_SETTING_PTR,
12632 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12633 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12634 							&link_behavior);
12635 		if (read_status)
12636 			goto err_nvm;
12637 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12638 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12639 	}
12640 	return ret;
12641 
12642 err_nvm:
12643 	dev_warn(&pf->pdev->dev,
12644 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12645 		 i40e_stat_str(&pf->hw, read_status));
12646 	return ret;
12647 }
12648 
12649 /**
12650  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12651  * @pf: board private structure to initialize
12652  *
12653  * i40e_sw_init initializes the Adapter private data structure.
12654  * Fields are initialized based on PCI device information and
12655  * OS network device settings (MTU size).
12656  **/
12657 static int i40e_sw_init(struct i40e_pf *pf)
12658 {
12659 	int err = 0;
12660 	int size;
12661 	u16 pow;
12662 
12663 	/* Set default capability flags */
12664 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12665 		    I40E_FLAG_MSI_ENABLED     |
12666 		    I40E_FLAG_MSIX_ENABLED;
12667 
12668 	/* Set default ITR */
12669 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12670 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12671 
12672 	/* Depending on PF configurations, it is possible that the RSS
12673 	 * maximum might end up larger than the available queues
12674 	 */
12675 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12676 	pf->alloc_rss_size = 1;
12677 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12678 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12679 				 pf->hw.func_caps.num_tx_qp);
12680 
12681 	/* find the next higher power-of-2 of num cpus */
12682 	pow = roundup_pow_of_two(num_online_cpus());
12683 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12684 
12685 	if (pf->hw.func_caps.rss) {
12686 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12687 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12688 					   num_online_cpus());
12689 	}
12690 
12691 	/* MFP mode enabled */
12692 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12693 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12694 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12695 		if (i40e_get_partition_bw_setting(pf)) {
12696 			dev_warn(&pf->pdev->dev,
12697 				 "Could not get partition bw settings\n");
12698 		} else {
12699 			dev_info(&pf->pdev->dev,
12700 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12701 				 pf->min_bw, pf->max_bw);
12702 
12703 			/* nudge the Tx scheduler */
12704 			i40e_set_partition_bw_setting(pf);
12705 		}
12706 	}
12707 
12708 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12709 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12710 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12711 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12712 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12713 		    pf->hw.num_partitions > 1)
12714 			dev_info(&pf->pdev->dev,
12715 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12716 		else
12717 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12718 		pf->fdir_pf_filter_count =
12719 				 pf->hw.func_caps.fd_filters_guaranteed;
12720 		pf->hw.fdir_shared_filter_count =
12721 				 pf->hw.func_caps.fd_filters_best_effort;
12722 	}
12723 
12724 	if (pf->hw.mac.type == I40E_MAC_X722) {
12725 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12726 				    I40E_HW_128_QP_RSS_CAPABLE |
12727 				    I40E_HW_ATR_EVICT_CAPABLE |
12728 				    I40E_HW_WB_ON_ITR_CAPABLE |
12729 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12730 				    I40E_HW_NO_PCI_LINK_CHECK |
12731 				    I40E_HW_USE_SET_LLDP_MIB |
12732 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12733 				    I40E_HW_PTP_L4_CAPABLE |
12734 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12735 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12736 
12737 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12738 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12739 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12740 			dev_warn(&pf->pdev->dev,
12741 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12742 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12743 		}
12744 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12745 		   ((pf->hw.aq.api_maj_ver == 1) &&
12746 		    (pf->hw.aq.api_min_ver > 4))) {
12747 		/* Supported in FW API version higher than 1.4 */
12748 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12749 	}
12750 
12751 	/* Enable HW ATR eviction if possible */
12752 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12753 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12754 
12755 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12756 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12757 	    (pf->hw.aq.fw_maj_ver < 4))) {
12758 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12759 		/* No DCB support  for FW < v4.33 */
12760 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12761 	}
12762 
12763 	/* Disable FW LLDP if FW < v4.3 */
12764 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12765 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12766 	    (pf->hw.aq.fw_maj_ver < 4)))
12767 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12768 
12769 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12770 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12771 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12772 	    (pf->hw.aq.fw_maj_ver >= 5)))
12773 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12774 
12775 	/* Enable PTP L4 if FW > v6.0 */
12776 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12777 	    pf->hw.aq.fw_maj_ver >= 6)
12778 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12779 
12780 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12781 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12782 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12783 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12784 	}
12785 
12786 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12787 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12788 		/* IWARP needs one extra vector for CQP just like MISC.*/
12789 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12790 	}
12791 	/* Stopping FW LLDP engine is supported on XL710 and X722
12792 	 * starting from FW versions determined in i40e_init_adminq.
12793 	 * Stopping the FW LLDP engine is not supported on XL710
12794 	 * if NPAR is functioning so unset this hw flag in this case.
12795 	 */
12796 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12797 	    pf->hw.func_caps.npar_enable &&
12798 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12799 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12800 
12801 #ifdef CONFIG_PCI_IOV
12802 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12803 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12804 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12805 		pf->num_req_vfs = min_t(int,
12806 					pf->hw.func_caps.num_vfs,
12807 					I40E_MAX_VF_COUNT);
12808 	}
12809 #endif /* CONFIG_PCI_IOV */
12810 	pf->eeprom_version = 0xDEAD;
12811 	pf->lan_veb = I40E_NO_VEB;
12812 	pf->lan_vsi = I40E_NO_VSI;
12813 
12814 	/* By default FW has this off for performance reasons */
12815 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12816 
12817 	/* set up queue assignment tracking */
12818 	size = sizeof(struct i40e_lump_tracking)
12819 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12820 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12821 	if (!pf->qp_pile) {
12822 		err = -ENOMEM;
12823 		goto sw_init_done;
12824 	}
12825 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12826 
12827 	pf->tx_timeout_recovery_level = 1;
12828 
12829 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12830 	    i40e_is_total_port_shutdown_enabled(pf)) {
12831 		/* Link down on close must be on when total port shutdown
12832 		 * is enabled for a given port
12833 		 */
12834 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12835 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12836 		dev_info(&pf->pdev->dev,
12837 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12838 	}
12839 	mutex_init(&pf->switch_mutex);
12840 
12841 sw_init_done:
12842 	return err;
12843 }
12844 
12845 /**
12846  * i40e_set_ntuple - set the ntuple feature flag and take action
12847  * @pf: board private structure to initialize
12848  * @features: the feature set that the stack is suggesting
12849  *
12850  * returns a bool to indicate if reset needs to happen
12851  **/
12852 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12853 {
12854 	bool need_reset = false;
12855 
12856 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12857 	 * the state changed, we need to reset.
12858 	 */
12859 	if (features & NETIF_F_NTUPLE) {
12860 		/* Enable filters and mark for reset */
12861 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12862 			need_reset = true;
12863 		/* enable FD_SB only if there is MSI-X vector and no cloud
12864 		 * filters exist
12865 		 */
12866 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12867 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12868 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12869 		}
12870 	} else {
12871 		/* turn off filters, mark for reset and clear SW filter list */
12872 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12873 			need_reset = true;
12874 			i40e_fdir_filter_exit(pf);
12875 		}
12876 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12877 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12878 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12879 
12880 		/* reset fd counters */
12881 		pf->fd_add_err = 0;
12882 		pf->fd_atr_cnt = 0;
12883 		/* if ATR was auto disabled it can be re-enabled. */
12884 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12885 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12886 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12887 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12888 	}
12889 	return need_reset;
12890 }
12891 
12892 /**
12893  * i40e_clear_rss_lut - clear the rx hash lookup table
12894  * @vsi: the VSI being configured
12895  **/
12896 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12897 {
12898 	struct i40e_pf *pf = vsi->back;
12899 	struct i40e_hw *hw = &pf->hw;
12900 	u16 vf_id = vsi->vf_id;
12901 	u8 i;
12902 
12903 	if (vsi->type == I40E_VSI_MAIN) {
12904 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12905 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12906 	} else if (vsi->type == I40E_VSI_SRIOV) {
12907 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12908 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12909 	} else {
12910 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12911 	}
12912 }
12913 
12914 /**
12915  * i40e_set_features - set the netdev feature flags
12916  * @netdev: ptr to the netdev being adjusted
12917  * @features: the feature set that the stack is suggesting
12918  * Note: expects to be called while under rtnl_lock()
12919  **/
12920 static int i40e_set_features(struct net_device *netdev,
12921 			     netdev_features_t features)
12922 {
12923 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12924 	struct i40e_vsi *vsi = np->vsi;
12925 	struct i40e_pf *pf = vsi->back;
12926 	bool need_reset;
12927 
12928 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12929 		i40e_pf_config_rss(pf);
12930 	else if (!(features & NETIF_F_RXHASH) &&
12931 		 netdev->features & NETIF_F_RXHASH)
12932 		i40e_clear_rss_lut(vsi);
12933 
12934 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12935 		i40e_vlan_stripping_enable(vsi);
12936 	else
12937 		i40e_vlan_stripping_disable(vsi);
12938 
12939 	if (!(features & NETIF_F_HW_TC) &&
12940 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12941 		dev_err(&pf->pdev->dev,
12942 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12943 		return -EINVAL;
12944 	}
12945 
12946 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12947 		i40e_del_all_macvlans(vsi);
12948 
12949 	need_reset = i40e_set_ntuple(pf, features);
12950 
12951 	if (need_reset)
12952 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12953 
12954 	return 0;
12955 }
12956 
12957 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12958 				    unsigned int table, unsigned int idx,
12959 				    struct udp_tunnel_info *ti)
12960 {
12961 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12962 	struct i40e_hw *hw = &np->vsi->back->hw;
12963 	u8 type, filter_index;
12964 	i40e_status ret;
12965 
12966 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12967 						   I40E_AQC_TUNNEL_TYPE_NGE;
12968 
12969 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12970 				     NULL);
12971 	if (ret) {
12972 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12973 			    i40e_stat_str(hw, ret),
12974 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12975 		return -EIO;
12976 	}
12977 
12978 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12979 	return 0;
12980 }
12981 
12982 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12983 				      unsigned int table, unsigned int idx,
12984 				      struct udp_tunnel_info *ti)
12985 {
12986 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12987 	struct i40e_hw *hw = &np->vsi->back->hw;
12988 	i40e_status ret;
12989 
12990 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12991 	if (ret) {
12992 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12993 			    i40e_stat_str(hw, ret),
12994 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12995 		return -EIO;
12996 	}
12997 
12998 	return 0;
12999 }
13000 
13001 static int i40e_get_phys_port_id(struct net_device *netdev,
13002 				 struct netdev_phys_item_id *ppid)
13003 {
13004 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13005 	struct i40e_pf *pf = np->vsi->back;
13006 	struct i40e_hw *hw = &pf->hw;
13007 
13008 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13009 		return -EOPNOTSUPP;
13010 
13011 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13012 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13013 
13014 	return 0;
13015 }
13016 
13017 /**
13018  * i40e_ndo_fdb_add - add an entry to the hardware database
13019  * @ndm: the input from the stack
13020  * @tb: pointer to array of nladdr (unused)
13021  * @dev: the net device pointer
13022  * @addr: the MAC address entry being added
13023  * @vid: VLAN ID
13024  * @flags: instructions from stack about fdb operation
13025  * @extack: netlink extended ack, unused currently
13026  */
13027 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13028 			    struct net_device *dev,
13029 			    const unsigned char *addr, u16 vid,
13030 			    u16 flags,
13031 			    struct netlink_ext_ack *extack)
13032 {
13033 	struct i40e_netdev_priv *np = netdev_priv(dev);
13034 	struct i40e_pf *pf = np->vsi->back;
13035 	int err = 0;
13036 
13037 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13038 		return -EOPNOTSUPP;
13039 
13040 	if (vid) {
13041 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13042 		return -EINVAL;
13043 	}
13044 
13045 	/* Hardware does not support aging addresses so if a
13046 	 * ndm_state is given only allow permanent addresses
13047 	 */
13048 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13049 		netdev_info(dev, "FDB only supports static addresses\n");
13050 		return -EINVAL;
13051 	}
13052 
13053 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13054 		err = dev_uc_add_excl(dev, addr);
13055 	else if (is_multicast_ether_addr(addr))
13056 		err = dev_mc_add_excl(dev, addr);
13057 	else
13058 		err = -EINVAL;
13059 
13060 	/* Only return duplicate errors if NLM_F_EXCL is set */
13061 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13062 		err = 0;
13063 
13064 	return err;
13065 }
13066 
13067 /**
13068  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13069  * @dev: the netdev being configured
13070  * @nlh: RTNL message
13071  * @flags: bridge flags
13072  * @extack: netlink extended ack
13073  *
13074  * Inserts a new hardware bridge if not already created and
13075  * enables the bridging mode requested (VEB or VEPA). If the
13076  * hardware bridge has already been inserted and the request
13077  * is to change the mode then that requires a PF reset to
13078  * allow rebuild of the components with required hardware
13079  * bridge mode enabled.
13080  *
13081  * Note: expects to be called while under rtnl_lock()
13082  **/
13083 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13084 				   struct nlmsghdr *nlh,
13085 				   u16 flags,
13086 				   struct netlink_ext_ack *extack)
13087 {
13088 	struct i40e_netdev_priv *np = netdev_priv(dev);
13089 	struct i40e_vsi *vsi = np->vsi;
13090 	struct i40e_pf *pf = vsi->back;
13091 	struct i40e_veb *veb = NULL;
13092 	struct nlattr *attr, *br_spec;
13093 	int i, rem;
13094 
13095 	/* Only for PF VSI for now */
13096 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13097 		return -EOPNOTSUPP;
13098 
13099 	/* Find the HW bridge for PF VSI */
13100 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13101 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13102 			veb = pf->veb[i];
13103 	}
13104 
13105 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13106 
13107 	nla_for_each_nested(attr, br_spec, rem) {
13108 		__u16 mode;
13109 
13110 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13111 			continue;
13112 
13113 		mode = nla_get_u16(attr);
13114 		if ((mode != BRIDGE_MODE_VEPA) &&
13115 		    (mode != BRIDGE_MODE_VEB))
13116 			return -EINVAL;
13117 
13118 		/* Insert a new HW bridge */
13119 		if (!veb) {
13120 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13121 					     vsi->tc_config.enabled_tc);
13122 			if (veb) {
13123 				veb->bridge_mode = mode;
13124 				i40e_config_bridge_mode(veb);
13125 			} else {
13126 				/* No Bridge HW offload available */
13127 				return -ENOENT;
13128 			}
13129 			break;
13130 		} else if (mode != veb->bridge_mode) {
13131 			/* Existing HW bridge but different mode needs reset */
13132 			veb->bridge_mode = mode;
13133 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13134 			if (mode == BRIDGE_MODE_VEB)
13135 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13136 			else
13137 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13138 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13139 			break;
13140 		}
13141 	}
13142 
13143 	return 0;
13144 }
13145 
13146 /**
13147  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13148  * @skb: skb buff
13149  * @pid: process id
13150  * @seq: RTNL message seq #
13151  * @dev: the netdev being configured
13152  * @filter_mask: unused
13153  * @nlflags: netlink flags passed in
13154  *
13155  * Return the mode in which the hardware bridge is operating in
13156  * i.e VEB or VEPA.
13157  **/
13158 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13159 				   struct net_device *dev,
13160 				   u32 __always_unused filter_mask,
13161 				   int nlflags)
13162 {
13163 	struct i40e_netdev_priv *np = netdev_priv(dev);
13164 	struct i40e_vsi *vsi = np->vsi;
13165 	struct i40e_pf *pf = vsi->back;
13166 	struct i40e_veb *veb = NULL;
13167 	int i;
13168 
13169 	/* Only for PF VSI for now */
13170 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13171 		return -EOPNOTSUPP;
13172 
13173 	/* Find the HW bridge for the PF VSI */
13174 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13175 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13176 			veb = pf->veb[i];
13177 	}
13178 
13179 	if (!veb)
13180 		return 0;
13181 
13182 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13183 				       0, 0, nlflags, filter_mask, NULL);
13184 }
13185 
13186 /**
13187  * i40e_features_check - Validate encapsulated packet conforms to limits
13188  * @skb: skb buff
13189  * @dev: This physical port's netdev
13190  * @features: Offload features that the stack believes apply
13191  **/
13192 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13193 					     struct net_device *dev,
13194 					     netdev_features_t features)
13195 {
13196 	size_t len;
13197 
13198 	/* No point in doing any of this if neither checksum nor GSO are
13199 	 * being requested for this frame.  We can rule out both by just
13200 	 * checking for CHECKSUM_PARTIAL
13201 	 */
13202 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13203 		return features;
13204 
13205 	/* We cannot support GSO if the MSS is going to be less than
13206 	 * 64 bytes.  If it is then we need to drop support for GSO.
13207 	 */
13208 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13209 		features &= ~NETIF_F_GSO_MASK;
13210 
13211 	/* MACLEN can support at most 63 words */
13212 	len = skb_network_header(skb) - skb->data;
13213 	if (len & ~(63 * 2))
13214 		goto out_err;
13215 
13216 	/* IPLEN and EIPLEN can support at most 127 dwords */
13217 	len = skb_transport_header(skb) - skb_network_header(skb);
13218 	if (len & ~(127 * 4))
13219 		goto out_err;
13220 
13221 	if (skb->encapsulation) {
13222 		/* L4TUNLEN can support 127 words */
13223 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13224 		if (len & ~(127 * 2))
13225 			goto out_err;
13226 
13227 		/* IPLEN can support at most 127 dwords */
13228 		len = skb_inner_transport_header(skb) -
13229 		      skb_inner_network_header(skb);
13230 		if (len & ~(127 * 4))
13231 			goto out_err;
13232 	}
13233 
13234 	/* No need to validate L4LEN as TCP is the only protocol with a
13235 	 * flexible value and we support all possible values supported
13236 	 * by TCP, which is at most 15 dwords
13237 	 */
13238 
13239 	return features;
13240 out_err:
13241 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13242 }
13243 
13244 /**
13245  * i40e_xdp_setup - add/remove an XDP program
13246  * @vsi: VSI to changed
13247  * @prog: XDP program
13248  * @extack: netlink extended ack
13249  **/
13250 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13251 			  struct netlink_ext_ack *extack)
13252 {
13253 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13254 	struct i40e_pf *pf = vsi->back;
13255 	struct bpf_prog *old_prog;
13256 	bool need_reset;
13257 	int i;
13258 
13259 	/* Don't allow frames that span over multiple buffers */
13260 	if (frame_size > vsi->rx_buf_len) {
13261 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13262 		return -EINVAL;
13263 	}
13264 
13265 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13266 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13267 
13268 	if (need_reset)
13269 		i40e_prep_for_reset(pf);
13270 
13271 	/* VSI shall be deleted in a moment, just return EINVAL */
13272 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13273 		return -EINVAL;
13274 
13275 	old_prog = xchg(&vsi->xdp_prog, prog);
13276 
13277 	if (need_reset) {
13278 		if (!prog)
13279 			/* Wait until ndo_xsk_wakeup completes. */
13280 			synchronize_rcu();
13281 		i40e_reset_and_rebuild(pf, true, true);
13282 	}
13283 
13284 	for (i = 0; i < vsi->num_queue_pairs; i++)
13285 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13286 
13287 	if (old_prog)
13288 		bpf_prog_put(old_prog);
13289 
13290 	/* Kick start the NAPI context if there is an AF_XDP socket open
13291 	 * on that queue id. This so that receiving will start.
13292 	 */
13293 	if (need_reset && prog)
13294 		for (i = 0; i < vsi->num_queue_pairs; i++)
13295 			if (vsi->xdp_rings[i]->xsk_pool)
13296 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13297 						      XDP_WAKEUP_RX);
13298 
13299 	return 0;
13300 }
13301 
13302 /**
13303  * i40e_enter_busy_conf - Enters busy config state
13304  * @vsi: vsi
13305  *
13306  * Returns 0 on success, <0 for failure.
13307  **/
13308 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13309 {
13310 	struct i40e_pf *pf = vsi->back;
13311 	int timeout = 50;
13312 
13313 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13314 		timeout--;
13315 		if (!timeout)
13316 			return -EBUSY;
13317 		usleep_range(1000, 2000);
13318 	}
13319 
13320 	return 0;
13321 }
13322 
13323 /**
13324  * i40e_exit_busy_conf - Exits busy config state
13325  * @vsi: vsi
13326  **/
13327 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13328 {
13329 	struct i40e_pf *pf = vsi->back;
13330 
13331 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13332 }
13333 
13334 /**
13335  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13336  * @vsi: vsi
13337  * @queue_pair: queue pair
13338  **/
13339 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13340 {
13341 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13342 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13343 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13344 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13345 	if (i40e_enabled_xdp_vsi(vsi)) {
13346 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13347 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13348 	}
13349 }
13350 
13351 /**
13352  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13353  * @vsi: vsi
13354  * @queue_pair: queue pair
13355  **/
13356 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13357 {
13358 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13359 	if (i40e_enabled_xdp_vsi(vsi)) {
13360 		/* Make sure that in-progress ndo_xdp_xmit calls are
13361 		 * completed.
13362 		 */
13363 		synchronize_rcu();
13364 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13365 	}
13366 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13367 }
13368 
13369 /**
13370  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13371  * @vsi: vsi
13372  * @queue_pair: queue pair
13373  * @enable: true for enable, false for disable
13374  **/
13375 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13376 					bool enable)
13377 {
13378 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13379 	struct i40e_q_vector *q_vector = rxr->q_vector;
13380 
13381 	if (!vsi->netdev)
13382 		return;
13383 
13384 	/* All rings in a qp belong to the same qvector. */
13385 	if (q_vector->rx.ring || q_vector->tx.ring) {
13386 		if (enable)
13387 			napi_enable(&q_vector->napi);
13388 		else
13389 			napi_disable(&q_vector->napi);
13390 	}
13391 }
13392 
13393 /**
13394  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13395  * @vsi: vsi
13396  * @queue_pair: queue pair
13397  * @enable: true for enable, false for disable
13398  *
13399  * Returns 0 on success, <0 on failure.
13400  **/
13401 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13402 					bool enable)
13403 {
13404 	struct i40e_pf *pf = vsi->back;
13405 	int pf_q, ret = 0;
13406 
13407 	pf_q = vsi->base_queue + queue_pair;
13408 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13409 				     false /*is xdp*/, enable);
13410 	if (ret) {
13411 		dev_info(&pf->pdev->dev,
13412 			 "VSI seid %d Tx ring %d %sable timeout\n",
13413 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13414 		return ret;
13415 	}
13416 
13417 	i40e_control_rx_q(pf, pf_q, enable);
13418 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13419 	if (ret) {
13420 		dev_info(&pf->pdev->dev,
13421 			 "VSI seid %d Rx ring %d %sable timeout\n",
13422 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13423 		return ret;
13424 	}
13425 
13426 	/* Due to HW errata, on Rx disable only, the register can
13427 	 * indicate done before it really is. Needs 50ms to be sure
13428 	 */
13429 	if (!enable)
13430 		mdelay(50);
13431 
13432 	if (!i40e_enabled_xdp_vsi(vsi))
13433 		return ret;
13434 
13435 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13436 				     pf_q + vsi->alloc_queue_pairs,
13437 				     true /*is xdp*/, enable);
13438 	if (ret) {
13439 		dev_info(&pf->pdev->dev,
13440 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13441 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13442 	}
13443 
13444 	return ret;
13445 }
13446 
13447 /**
13448  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13449  * @vsi: vsi
13450  * @queue_pair: queue_pair
13451  **/
13452 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13453 {
13454 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13455 	struct i40e_pf *pf = vsi->back;
13456 	struct i40e_hw *hw = &pf->hw;
13457 
13458 	/* All rings in a qp belong to the same qvector. */
13459 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13460 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13461 	else
13462 		i40e_irq_dynamic_enable_icr0(pf);
13463 
13464 	i40e_flush(hw);
13465 }
13466 
13467 /**
13468  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13469  * @vsi: vsi
13470  * @queue_pair: queue_pair
13471  **/
13472 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13473 {
13474 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13475 	struct i40e_pf *pf = vsi->back;
13476 	struct i40e_hw *hw = &pf->hw;
13477 
13478 	/* For simplicity, instead of removing the qp interrupt causes
13479 	 * from the interrupt linked list, we simply disable the interrupt, and
13480 	 * leave the list intact.
13481 	 *
13482 	 * All rings in a qp belong to the same qvector.
13483 	 */
13484 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13485 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13486 
13487 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13488 		i40e_flush(hw);
13489 		synchronize_irq(pf->msix_entries[intpf].vector);
13490 	} else {
13491 		/* Legacy and MSI mode - this stops all interrupt handling */
13492 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13493 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13494 		i40e_flush(hw);
13495 		synchronize_irq(pf->pdev->irq);
13496 	}
13497 }
13498 
13499 /**
13500  * i40e_queue_pair_disable - Disables a queue pair
13501  * @vsi: vsi
13502  * @queue_pair: queue pair
13503  *
13504  * Returns 0 on success, <0 on failure.
13505  **/
13506 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13507 {
13508 	int err;
13509 
13510 	err = i40e_enter_busy_conf(vsi);
13511 	if (err)
13512 		return err;
13513 
13514 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13515 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13516 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13517 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13518 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13519 
13520 	return err;
13521 }
13522 
13523 /**
13524  * i40e_queue_pair_enable - Enables a queue pair
13525  * @vsi: vsi
13526  * @queue_pair: queue pair
13527  *
13528  * Returns 0 on success, <0 on failure.
13529  **/
13530 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13531 {
13532 	int err;
13533 
13534 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13535 	if (err)
13536 		return err;
13537 
13538 	if (i40e_enabled_xdp_vsi(vsi)) {
13539 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13540 		if (err)
13541 			return err;
13542 	}
13543 
13544 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13545 	if (err)
13546 		return err;
13547 
13548 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13549 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13550 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13551 
13552 	i40e_exit_busy_conf(vsi);
13553 
13554 	return err;
13555 }
13556 
13557 /**
13558  * i40e_xdp - implements ndo_bpf for i40e
13559  * @dev: netdevice
13560  * @xdp: XDP command
13561  **/
13562 static int i40e_xdp(struct net_device *dev,
13563 		    struct netdev_bpf *xdp)
13564 {
13565 	struct i40e_netdev_priv *np = netdev_priv(dev);
13566 	struct i40e_vsi *vsi = np->vsi;
13567 
13568 	if (vsi->type != I40E_VSI_MAIN)
13569 		return -EINVAL;
13570 
13571 	switch (xdp->command) {
13572 	case XDP_SETUP_PROG:
13573 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13574 	case XDP_SETUP_XSK_POOL:
13575 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13576 					   xdp->xsk.queue_id);
13577 	default:
13578 		return -EINVAL;
13579 	}
13580 }
13581 
13582 static const struct net_device_ops i40e_netdev_ops = {
13583 	.ndo_open		= i40e_open,
13584 	.ndo_stop		= i40e_close,
13585 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13586 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13587 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13588 	.ndo_validate_addr	= eth_validate_addr,
13589 	.ndo_set_mac_address	= i40e_set_mac,
13590 	.ndo_change_mtu		= i40e_change_mtu,
13591 	.ndo_eth_ioctl		= i40e_ioctl,
13592 	.ndo_tx_timeout		= i40e_tx_timeout,
13593 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13594 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13595 #ifdef CONFIG_NET_POLL_CONTROLLER
13596 	.ndo_poll_controller	= i40e_netpoll,
13597 #endif
13598 	.ndo_setup_tc		= __i40e_setup_tc,
13599 	.ndo_select_queue	= i40e_lan_select_queue,
13600 	.ndo_set_features	= i40e_set_features,
13601 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13602 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13603 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13604 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13605 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13606 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13607 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13608 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13609 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13610 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13611 	.ndo_features_check	= i40e_features_check,
13612 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13613 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13614 	.ndo_bpf		= i40e_xdp,
13615 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13616 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13617 	.ndo_dfwd_add_station	= i40e_fwd_add,
13618 	.ndo_dfwd_del_station	= i40e_fwd_del,
13619 };
13620 
13621 /**
13622  * i40e_config_netdev - Setup the netdev flags
13623  * @vsi: the VSI being configured
13624  *
13625  * Returns 0 on success, negative value on failure
13626  **/
13627 static int i40e_config_netdev(struct i40e_vsi *vsi)
13628 {
13629 	struct i40e_pf *pf = vsi->back;
13630 	struct i40e_hw *hw = &pf->hw;
13631 	struct i40e_netdev_priv *np;
13632 	struct net_device *netdev;
13633 	u8 broadcast[ETH_ALEN];
13634 	u8 mac_addr[ETH_ALEN];
13635 	int etherdev_size;
13636 	netdev_features_t hw_enc_features;
13637 	netdev_features_t hw_features;
13638 
13639 	etherdev_size = sizeof(struct i40e_netdev_priv);
13640 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13641 	if (!netdev)
13642 		return -ENOMEM;
13643 
13644 	vsi->netdev = netdev;
13645 	np = netdev_priv(netdev);
13646 	np->vsi = vsi;
13647 
13648 	hw_enc_features = NETIF_F_SG			|
13649 			  NETIF_F_HW_CSUM		|
13650 			  NETIF_F_HIGHDMA		|
13651 			  NETIF_F_SOFT_FEATURES		|
13652 			  NETIF_F_TSO			|
13653 			  NETIF_F_TSO_ECN		|
13654 			  NETIF_F_TSO6			|
13655 			  NETIF_F_GSO_GRE		|
13656 			  NETIF_F_GSO_GRE_CSUM		|
13657 			  NETIF_F_GSO_PARTIAL		|
13658 			  NETIF_F_GSO_IPXIP4		|
13659 			  NETIF_F_GSO_IPXIP6		|
13660 			  NETIF_F_GSO_UDP_TUNNEL	|
13661 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13662 			  NETIF_F_GSO_UDP_L4		|
13663 			  NETIF_F_SCTP_CRC		|
13664 			  NETIF_F_RXHASH		|
13665 			  NETIF_F_RXCSUM		|
13666 			  0;
13667 
13668 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13669 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13670 
13671 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13672 
13673 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13674 
13675 	netdev->hw_enc_features |= hw_enc_features;
13676 
13677 	/* record features VLANs can make use of */
13678 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13679 
13680 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13681 				   NETIF_F_GSO_GRE_CSUM |	\
13682 				   NETIF_F_GSO_IPXIP4 |		\
13683 				   NETIF_F_GSO_IPXIP6 |		\
13684 				   NETIF_F_GSO_UDP_TUNNEL |	\
13685 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13686 
13687 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13688 	netdev->features |= NETIF_F_GSO_PARTIAL |
13689 			    I40E_GSO_PARTIAL_FEATURES;
13690 
13691 	netdev->mpls_features |= NETIF_F_SG;
13692 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13693 	netdev->mpls_features |= NETIF_F_TSO;
13694 	netdev->mpls_features |= NETIF_F_TSO6;
13695 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13696 
13697 	/* enable macvlan offloads */
13698 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13699 
13700 	hw_features = hw_enc_features		|
13701 		      NETIF_F_HW_VLAN_CTAG_TX	|
13702 		      NETIF_F_HW_VLAN_CTAG_RX;
13703 
13704 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13705 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13706 
13707 	netdev->hw_features |= hw_features;
13708 
13709 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13710 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13711 
13712 	netdev->features &= ~NETIF_F_HW_TC;
13713 
13714 	if (vsi->type == I40E_VSI_MAIN) {
13715 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13716 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13717 		/* The following steps are necessary for two reasons. First,
13718 		 * some older NVM configurations load a default MAC-VLAN
13719 		 * filter that will accept any tagged packet, and we want to
13720 		 * replace this with a normal filter. Additionally, it is
13721 		 * possible our MAC address was provided by the platform using
13722 		 * Open Firmware or similar.
13723 		 *
13724 		 * Thus, we need to remove the default filter and install one
13725 		 * specific to the MAC address.
13726 		 */
13727 		i40e_rm_default_mac_filter(vsi, mac_addr);
13728 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13729 		i40e_add_mac_filter(vsi, mac_addr);
13730 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13731 	} else {
13732 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13733 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13734 		 * the end, which is 4 bytes long, so force truncation of the
13735 		 * original name by IFNAMSIZ - 4
13736 		 */
13737 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13738 			 IFNAMSIZ - 4,
13739 			 pf->vsi[pf->lan_vsi]->netdev->name);
13740 		eth_random_addr(mac_addr);
13741 
13742 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13743 		i40e_add_mac_filter(vsi, mac_addr);
13744 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13745 	}
13746 
13747 	/* Add the broadcast filter so that we initially will receive
13748 	 * broadcast packets. Note that when a new VLAN is first added the
13749 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13750 	 * specific filters as part of transitioning into "vlan" operation.
13751 	 * When more VLANs are added, the driver will copy each existing MAC
13752 	 * filter and add it for the new VLAN.
13753 	 *
13754 	 * Broadcast filters are handled specially by
13755 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13756 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13757 	 * filter. The subtask will update the correct broadcast promiscuous
13758 	 * bits as VLANs become active or inactive.
13759 	 */
13760 	eth_broadcast_addr(broadcast);
13761 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13762 	i40e_add_mac_filter(vsi, broadcast);
13763 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13764 
13765 	eth_hw_addr_set(netdev, mac_addr);
13766 	ether_addr_copy(netdev->perm_addr, mac_addr);
13767 
13768 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13769 	netdev->neigh_priv_len = sizeof(u32) * 4;
13770 
13771 	netdev->priv_flags |= IFF_UNICAST_FLT;
13772 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13773 	/* Setup netdev TC information */
13774 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13775 
13776 	netdev->netdev_ops = &i40e_netdev_ops;
13777 	netdev->watchdog_timeo = 5 * HZ;
13778 	i40e_set_ethtool_ops(netdev);
13779 
13780 	/* MTU range: 68 - 9706 */
13781 	netdev->min_mtu = ETH_MIN_MTU;
13782 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13783 
13784 	return 0;
13785 }
13786 
13787 /**
13788  * i40e_vsi_delete - Delete a VSI from the switch
13789  * @vsi: the VSI being removed
13790  *
13791  * Returns 0 on success, negative value on failure
13792  **/
13793 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13794 {
13795 	/* remove default VSI is not allowed */
13796 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13797 		return;
13798 
13799 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13800 }
13801 
13802 /**
13803  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13804  * @vsi: the VSI being queried
13805  *
13806  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13807  **/
13808 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13809 {
13810 	struct i40e_veb *veb;
13811 	struct i40e_pf *pf = vsi->back;
13812 
13813 	/* Uplink is not a bridge so default to VEB */
13814 	if (vsi->veb_idx >= I40E_MAX_VEB)
13815 		return 1;
13816 
13817 	veb = pf->veb[vsi->veb_idx];
13818 	if (!veb) {
13819 		dev_info(&pf->pdev->dev,
13820 			 "There is no veb associated with the bridge\n");
13821 		return -ENOENT;
13822 	}
13823 
13824 	/* Uplink is a bridge in VEPA mode */
13825 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13826 		return 0;
13827 	} else {
13828 		/* Uplink is a bridge in VEB mode */
13829 		return 1;
13830 	}
13831 
13832 	/* VEPA is now default bridge, so return 0 */
13833 	return 0;
13834 }
13835 
13836 /**
13837  * i40e_add_vsi - Add a VSI to the switch
13838  * @vsi: the VSI being configured
13839  *
13840  * This initializes a VSI context depending on the VSI type to be added and
13841  * passes it down to the add_vsi aq command.
13842  **/
13843 static int i40e_add_vsi(struct i40e_vsi *vsi)
13844 {
13845 	int ret = -ENODEV;
13846 	struct i40e_pf *pf = vsi->back;
13847 	struct i40e_hw *hw = &pf->hw;
13848 	struct i40e_vsi_context ctxt;
13849 	struct i40e_mac_filter *f;
13850 	struct hlist_node *h;
13851 	int bkt;
13852 
13853 	u8 enabled_tc = 0x1; /* TC0 enabled */
13854 	int f_count = 0;
13855 
13856 	memset(&ctxt, 0, sizeof(ctxt));
13857 	switch (vsi->type) {
13858 	case I40E_VSI_MAIN:
13859 		/* The PF's main VSI is already setup as part of the
13860 		 * device initialization, so we'll not bother with
13861 		 * the add_vsi call, but we will retrieve the current
13862 		 * VSI context.
13863 		 */
13864 		ctxt.seid = pf->main_vsi_seid;
13865 		ctxt.pf_num = pf->hw.pf_id;
13866 		ctxt.vf_num = 0;
13867 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13868 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13869 		if (ret) {
13870 			dev_info(&pf->pdev->dev,
13871 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13872 				 i40e_stat_str(&pf->hw, ret),
13873 				 i40e_aq_str(&pf->hw,
13874 					     pf->hw.aq.asq_last_status));
13875 			return -ENOENT;
13876 		}
13877 		vsi->info = ctxt.info;
13878 		vsi->info.valid_sections = 0;
13879 
13880 		vsi->seid = ctxt.seid;
13881 		vsi->id = ctxt.vsi_number;
13882 
13883 		enabled_tc = i40e_pf_get_tc_map(pf);
13884 
13885 		/* Source pruning is enabled by default, so the flag is
13886 		 * negative logic - if it's set, we need to fiddle with
13887 		 * the VSI to disable source pruning.
13888 		 */
13889 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13890 			memset(&ctxt, 0, sizeof(ctxt));
13891 			ctxt.seid = pf->main_vsi_seid;
13892 			ctxt.pf_num = pf->hw.pf_id;
13893 			ctxt.vf_num = 0;
13894 			ctxt.info.valid_sections |=
13895 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13896 			ctxt.info.switch_id =
13897 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13898 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13899 			if (ret) {
13900 				dev_info(&pf->pdev->dev,
13901 					 "update vsi failed, err %s aq_err %s\n",
13902 					 i40e_stat_str(&pf->hw, ret),
13903 					 i40e_aq_str(&pf->hw,
13904 						     pf->hw.aq.asq_last_status));
13905 				ret = -ENOENT;
13906 				goto err;
13907 			}
13908 		}
13909 
13910 		/* MFP mode setup queue map and update VSI */
13911 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13912 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13913 			memset(&ctxt, 0, sizeof(ctxt));
13914 			ctxt.seid = pf->main_vsi_seid;
13915 			ctxt.pf_num = pf->hw.pf_id;
13916 			ctxt.vf_num = 0;
13917 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13918 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13919 			if (ret) {
13920 				dev_info(&pf->pdev->dev,
13921 					 "update vsi failed, err %s aq_err %s\n",
13922 					 i40e_stat_str(&pf->hw, ret),
13923 					 i40e_aq_str(&pf->hw,
13924 						    pf->hw.aq.asq_last_status));
13925 				ret = -ENOENT;
13926 				goto err;
13927 			}
13928 			/* update the local VSI info queue map */
13929 			i40e_vsi_update_queue_map(vsi, &ctxt);
13930 			vsi->info.valid_sections = 0;
13931 		} else {
13932 			/* Default/Main VSI is only enabled for TC0
13933 			 * reconfigure it to enable all TCs that are
13934 			 * available on the port in SFP mode.
13935 			 * For MFP case the iSCSI PF would use this
13936 			 * flow to enable LAN+iSCSI TC.
13937 			 */
13938 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13939 			if (ret) {
13940 				/* Single TC condition is not fatal,
13941 				 * message and continue
13942 				 */
13943 				dev_info(&pf->pdev->dev,
13944 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13945 					 enabled_tc,
13946 					 i40e_stat_str(&pf->hw, ret),
13947 					 i40e_aq_str(&pf->hw,
13948 						    pf->hw.aq.asq_last_status));
13949 			}
13950 		}
13951 		break;
13952 
13953 	case I40E_VSI_FDIR:
13954 		ctxt.pf_num = hw->pf_id;
13955 		ctxt.vf_num = 0;
13956 		ctxt.uplink_seid = vsi->uplink_seid;
13957 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13958 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13959 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13960 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13961 			ctxt.info.valid_sections |=
13962 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13963 			ctxt.info.switch_id =
13964 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13965 		}
13966 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13967 		break;
13968 
13969 	case I40E_VSI_VMDQ2:
13970 		ctxt.pf_num = hw->pf_id;
13971 		ctxt.vf_num = 0;
13972 		ctxt.uplink_seid = vsi->uplink_seid;
13973 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13974 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13975 
13976 		/* This VSI is connected to VEB so the switch_id
13977 		 * should be set to zero by default.
13978 		 */
13979 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13980 			ctxt.info.valid_sections |=
13981 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13982 			ctxt.info.switch_id =
13983 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13984 		}
13985 
13986 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13987 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13988 		break;
13989 
13990 	case I40E_VSI_SRIOV:
13991 		ctxt.pf_num = hw->pf_id;
13992 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13993 		ctxt.uplink_seid = vsi->uplink_seid;
13994 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13995 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13996 
13997 		/* This VSI is connected to VEB so the switch_id
13998 		 * should be set to zero by default.
13999 		 */
14000 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14001 			ctxt.info.valid_sections |=
14002 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14003 			ctxt.info.switch_id =
14004 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14005 		}
14006 
14007 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14008 			ctxt.info.valid_sections |=
14009 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14010 			ctxt.info.queueing_opt_flags |=
14011 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14012 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14013 		}
14014 
14015 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14016 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14017 		if (pf->vf[vsi->vf_id].spoofchk) {
14018 			ctxt.info.valid_sections |=
14019 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14020 			ctxt.info.sec_flags |=
14021 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14022 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14023 		}
14024 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14025 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14026 		break;
14027 
14028 	case I40E_VSI_IWARP:
14029 		/* send down message to iWARP */
14030 		break;
14031 
14032 	default:
14033 		return -ENODEV;
14034 	}
14035 
14036 	if (vsi->type != I40E_VSI_MAIN) {
14037 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14038 		if (ret) {
14039 			dev_info(&vsi->back->pdev->dev,
14040 				 "add vsi failed, err %s aq_err %s\n",
14041 				 i40e_stat_str(&pf->hw, ret),
14042 				 i40e_aq_str(&pf->hw,
14043 					     pf->hw.aq.asq_last_status));
14044 			ret = -ENOENT;
14045 			goto err;
14046 		}
14047 		vsi->info = ctxt.info;
14048 		vsi->info.valid_sections = 0;
14049 		vsi->seid = ctxt.seid;
14050 		vsi->id = ctxt.vsi_number;
14051 	}
14052 
14053 	vsi->active_filters = 0;
14054 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14055 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14056 	/* If macvlan filters already exist, force them to get loaded */
14057 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14058 		f->state = I40E_FILTER_NEW;
14059 		f_count++;
14060 	}
14061 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14062 
14063 	if (f_count) {
14064 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14065 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14066 	}
14067 
14068 	/* Update VSI BW information */
14069 	ret = i40e_vsi_get_bw_info(vsi);
14070 	if (ret) {
14071 		dev_info(&pf->pdev->dev,
14072 			 "couldn't get vsi bw info, err %s aq_err %s\n",
14073 			 i40e_stat_str(&pf->hw, ret),
14074 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14075 		/* VSI is already added so not tearing that up */
14076 		ret = 0;
14077 	}
14078 
14079 err:
14080 	return ret;
14081 }
14082 
14083 /**
14084  * i40e_vsi_release - Delete a VSI and free its resources
14085  * @vsi: the VSI being removed
14086  *
14087  * Returns 0 on success or < 0 on error
14088  **/
14089 int i40e_vsi_release(struct i40e_vsi *vsi)
14090 {
14091 	struct i40e_mac_filter *f;
14092 	struct hlist_node *h;
14093 	struct i40e_veb *veb = NULL;
14094 	struct i40e_pf *pf;
14095 	u16 uplink_seid;
14096 	int i, n, bkt;
14097 
14098 	pf = vsi->back;
14099 
14100 	/* release of a VEB-owner or last VSI is not allowed */
14101 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14102 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14103 			 vsi->seid, vsi->uplink_seid);
14104 		return -ENODEV;
14105 	}
14106 	if (vsi == pf->vsi[pf->lan_vsi] &&
14107 	    !test_bit(__I40E_DOWN, pf->state)) {
14108 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14109 		return -ENODEV;
14110 	}
14111 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14112 	uplink_seid = vsi->uplink_seid;
14113 	if (vsi->type != I40E_VSI_SRIOV) {
14114 		if (vsi->netdev_registered) {
14115 			vsi->netdev_registered = false;
14116 			if (vsi->netdev) {
14117 				/* results in a call to i40e_close() */
14118 				unregister_netdev(vsi->netdev);
14119 			}
14120 		} else {
14121 			i40e_vsi_close(vsi);
14122 		}
14123 		i40e_vsi_disable_irq(vsi);
14124 	}
14125 
14126 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14127 
14128 	/* clear the sync flag on all filters */
14129 	if (vsi->netdev) {
14130 		__dev_uc_unsync(vsi->netdev, NULL);
14131 		__dev_mc_unsync(vsi->netdev, NULL);
14132 	}
14133 
14134 	/* make sure any remaining filters are marked for deletion */
14135 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14136 		__i40e_del_filter(vsi, f);
14137 
14138 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14139 
14140 	i40e_sync_vsi_filters(vsi);
14141 
14142 	i40e_vsi_delete(vsi);
14143 	i40e_vsi_free_q_vectors(vsi);
14144 	if (vsi->netdev) {
14145 		free_netdev(vsi->netdev);
14146 		vsi->netdev = NULL;
14147 	}
14148 	i40e_vsi_clear_rings(vsi);
14149 	i40e_vsi_clear(vsi);
14150 
14151 	/* If this was the last thing on the VEB, except for the
14152 	 * controlling VSI, remove the VEB, which puts the controlling
14153 	 * VSI onto the next level down in the switch.
14154 	 *
14155 	 * Well, okay, there's one more exception here: don't remove
14156 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14157 	 * from up the network stack.
14158 	 */
14159 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14160 		if (pf->vsi[i] &&
14161 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14162 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14163 			n++;      /* count the VSIs */
14164 		}
14165 	}
14166 	for (i = 0; i < I40E_MAX_VEB; i++) {
14167 		if (!pf->veb[i])
14168 			continue;
14169 		if (pf->veb[i]->uplink_seid == uplink_seid)
14170 			n++;     /* count the VEBs */
14171 		if (pf->veb[i]->seid == uplink_seid)
14172 			veb = pf->veb[i];
14173 	}
14174 	if (n == 0 && veb && veb->uplink_seid != 0)
14175 		i40e_veb_release(veb);
14176 
14177 	return 0;
14178 }
14179 
14180 /**
14181  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14182  * @vsi: ptr to the VSI
14183  *
14184  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14185  * corresponding SW VSI structure and initializes num_queue_pairs for the
14186  * newly allocated VSI.
14187  *
14188  * Returns 0 on success or negative on failure
14189  **/
14190 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14191 {
14192 	int ret = -ENOENT;
14193 	struct i40e_pf *pf = vsi->back;
14194 
14195 	if (vsi->q_vectors[0]) {
14196 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14197 			 vsi->seid);
14198 		return -EEXIST;
14199 	}
14200 
14201 	if (vsi->base_vector) {
14202 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14203 			 vsi->seid, vsi->base_vector);
14204 		return -EEXIST;
14205 	}
14206 
14207 	ret = i40e_vsi_alloc_q_vectors(vsi);
14208 	if (ret) {
14209 		dev_info(&pf->pdev->dev,
14210 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14211 			 vsi->num_q_vectors, vsi->seid, ret);
14212 		vsi->num_q_vectors = 0;
14213 		goto vector_setup_out;
14214 	}
14215 
14216 	/* In Legacy mode, we do not have to get any other vector since we
14217 	 * piggyback on the misc/ICR0 for queue interrupts.
14218 	*/
14219 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14220 		return ret;
14221 	if (vsi->num_q_vectors)
14222 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14223 						 vsi->num_q_vectors, vsi->idx);
14224 	if (vsi->base_vector < 0) {
14225 		dev_info(&pf->pdev->dev,
14226 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14227 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14228 		i40e_vsi_free_q_vectors(vsi);
14229 		ret = -ENOENT;
14230 		goto vector_setup_out;
14231 	}
14232 
14233 vector_setup_out:
14234 	return ret;
14235 }
14236 
14237 /**
14238  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14239  * @vsi: pointer to the vsi.
14240  *
14241  * This re-allocates a vsi's queue resources.
14242  *
14243  * Returns pointer to the successfully allocated and configured VSI sw struct
14244  * on success, otherwise returns NULL on failure.
14245  **/
14246 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14247 {
14248 	u16 alloc_queue_pairs;
14249 	struct i40e_pf *pf;
14250 	u8 enabled_tc;
14251 	int ret;
14252 
14253 	if (!vsi)
14254 		return NULL;
14255 
14256 	pf = vsi->back;
14257 
14258 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14259 	i40e_vsi_clear_rings(vsi);
14260 
14261 	i40e_vsi_free_arrays(vsi, false);
14262 	i40e_set_num_rings_in_vsi(vsi);
14263 	ret = i40e_vsi_alloc_arrays(vsi, false);
14264 	if (ret)
14265 		goto err_vsi;
14266 
14267 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14268 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14269 
14270 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14271 	if (ret < 0) {
14272 		dev_info(&pf->pdev->dev,
14273 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14274 			 alloc_queue_pairs, vsi->seid, ret);
14275 		goto err_vsi;
14276 	}
14277 	vsi->base_queue = ret;
14278 
14279 	/* Update the FW view of the VSI. Force a reset of TC and queue
14280 	 * layout configurations.
14281 	 */
14282 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14283 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14284 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14285 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14286 	if (vsi->type == I40E_VSI_MAIN)
14287 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14288 
14289 	/* assign it some queues */
14290 	ret = i40e_alloc_rings(vsi);
14291 	if (ret)
14292 		goto err_rings;
14293 
14294 	/* map all of the rings to the q_vectors */
14295 	i40e_vsi_map_rings_to_vectors(vsi);
14296 	return vsi;
14297 
14298 err_rings:
14299 	i40e_vsi_free_q_vectors(vsi);
14300 	if (vsi->netdev_registered) {
14301 		vsi->netdev_registered = false;
14302 		unregister_netdev(vsi->netdev);
14303 		free_netdev(vsi->netdev);
14304 		vsi->netdev = NULL;
14305 	}
14306 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14307 err_vsi:
14308 	i40e_vsi_clear(vsi);
14309 	return NULL;
14310 }
14311 
14312 /**
14313  * i40e_vsi_setup - Set up a VSI by a given type
14314  * @pf: board private structure
14315  * @type: VSI type
14316  * @uplink_seid: the switch element to link to
14317  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14318  *
14319  * This allocates the sw VSI structure and its queue resources, then add a VSI
14320  * to the identified VEB.
14321  *
14322  * Returns pointer to the successfully allocated and configure VSI sw struct on
14323  * success, otherwise returns NULL on failure.
14324  **/
14325 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14326 				u16 uplink_seid, u32 param1)
14327 {
14328 	struct i40e_vsi *vsi = NULL;
14329 	struct i40e_veb *veb = NULL;
14330 	u16 alloc_queue_pairs;
14331 	int ret, i;
14332 	int v_idx;
14333 
14334 	/* The requested uplink_seid must be either
14335 	 *     - the PF's port seid
14336 	 *              no VEB is needed because this is the PF
14337 	 *              or this is a Flow Director special case VSI
14338 	 *     - seid of an existing VEB
14339 	 *     - seid of a VSI that owns an existing VEB
14340 	 *     - seid of a VSI that doesn't own a VEB
14341 	 *              a new VEB is created and the VSI becomes the owner
14342 	 *     - seid of the PF VSI, which is what creates the first VEB
14343 	 *              this is a special case of the previous
14344 	 *
14345 	 * Find which uplink_seid we were given and create a new VEB if needed
14346 	 */
14347 	for (i = 0; i < I40E_MAX_VEB; i++) {
14348 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14349 			veb = pf->veb[i];
14350 			break;
14351 		}
14352 	}
14353 
14354 	if (!veb && uplink_seid != pf->mac_seid) {
14355 
14356 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14357 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14358 				vsi = pf->vsi[i];
14359 				break;
14360 			}
14361 		}
14362 		if (!vsi) {
14363 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14364 				 uplink_seid);
14365 			return NULL;
14366 		}
14367 
14368 		if (vsi->uplink_seid == pf->mac_seid)
14369 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14370 					     vsi->tc_config.enabled_tc);
14371 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14372 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14373 					     vsi->tc_config.enabled_tc);
14374 		if (veb) {
14375 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14376 				dev_info(&vsi->back->pdev->dev,
14377 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14378 				return NULL;
14379 			}
14380 			/* We come up by default in VEPA mode if SRIOV is not
14381 			 * already enabled, in which case we can't force VEPA
14382 			 * mode.
14383 			 */
14384 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14385 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14386 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14387 			}
14388 			i40e_config_bridge_mode(veb);
14389 		}
14390 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14391 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14392 				veb = pf->veb[i];
14393 		}
14394 		if (!veb) {
14395 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14396 			return NULL;
14397 		}
14398 
14399 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14400 		uplink_seid = veb->seid;
14401 	}
14402 
14403 	/* get vsi sw struct */
14404 	v_idx = i40e_vsi_mem_alloc(pf, type);
14405 	if (v_idx < 0)
14406 		goto err_alloc;
14407 	vsi = pf->vsi[v_idx];
14408 	if (!vsi)
14409 		goto err_alloc;
14410 	vsi->type = type;
14411 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14412 
14413 	if (type == I40E_VSI_MAIN)
14414 		pf->lan_vsi = v_idx;
14415 	else if (type == I40E_VSI_SRIOV)
14416 		vsi->vf_id = param1;
14417 	/* assign it some queues */
14418 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14419 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14420 
14421 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14422 	if (ret < 0) {
14423 		dev_info(&pf->pdev->dev,
14424 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14425 			 alloc_queue_pairs, vsi->seid, ret);
14426 		goto err_vsi;
14427 	}
14428 	vsi->base_queue = ret;
14429 
14430 	/* get a VSI from the hardware */
14431 	vsi->uplink_seid = uplink_seid;
14432 	ret = i40e_add_vsi(vsi);
14433 	if (ret)
14434 		goto err_vsi;
14435 
14436 	switch (vsi->type) {
14437 	/* setup the netdev if needed */
14438 	case I40E_VSI_MAIN:
14439 	case I40E_VSI_VMDQ2:
14440 		ret = i40e_config_netdev(vsi);
14441 		if (ret)
14442 			goto err_netdev;
14443 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14444 		if (ret)
14445 			goto err_netdev;
14446 		ret = register_netdev(vsi->netdev);
14447 		if (ret)
14448 			goto err_netdev;
14449 		vsi->netdev_registered = true;
14450 		netif_carrier_off(vsi->netdev);
14451 #ifdef CONFIG_I40E_DCB
14452 		/* Setup DCB netlink interface */
14453 		i40e_dcbnl_setup(vsi);
14454 #endif /* CONFIG_I40E_DCB */
14455 		fallthrough;
14456 	case I40E_VSI_FDIR:
14457 		/* set up vectors and rings if needed */
14458 		ret = i40e_vsi_setup_vectors(vsi);
14459 		if (ret)
14460 			goto err_msix;
14461 
14462 		ret = i40e_alloc_rings(vsi);
14463 		if (ret)
14464 			goto err_rings;
14465 
14466 		/* map all of the rings to the q_vectors */
14467 		i40e_vsi_map_rings_to_vectors(vsi);
14468 
14469 		i40e_vsi_reset_stats(vsi);
14470 		break;
14471 	default:
14472 		/* no netdev or rings for the other VSI types */
14473 		break;
14474 	}
14475 
14476 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14477 	    (vsi->type == I40E_VSI_VMDQ2)) {
14478 		ret = i40e_vsi_config_rss(vsi);
14479 	}
14480 	return vsi;
14481 
14482 err_rings:
14483 	i40e_vsi_free_q_vectors(vsi);
14484 err_msix:
14485 	if (vsi->netdev_registered) {
14486 		vsi->netdev_registered = false;
14487 		unregister_netdev(vsi->netdev);
14488 		free_netdev(vsi->netdev);
14489 		vsi->netdev = NULL;
14490 	}
14491 err_netdev:
14492 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14493 err_vsi:
14494 	i40e_vsi_clear(vsi);
14495 err_alloc:
14496 	return NULL;
14497 }
14498 
14499 /**
14500  * i40e_veb_get_bw_info - Query VEB BW information
14501  * @veb: the veb to query
14502  *
14503  * Query the Tx scheduler BW configuration data for given VEB
14504  **/
14505 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14506 {
14507 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14508 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14509 	struct i40e_pf *pf = veb->pf;
14510 	struct i40e_hw *hw = &pf->hw;
14511 	u32 tc_bw_max;
14512 	int ret = 0;
14513 	int i;
14514 
14515 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14516 						  &bw_data, NULL);
14517 	if (ret) {
14518 		dev_info(&pf->pdev->dev,
14519 			 "query veb bw config failed, err %s aq_err %s\n",
14520 			 i40e_stat_str(&pf->hw, ret),
14521 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14522 		goto out;
14523 	}
14524 
14525 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14526 						   &ets_data, NULL);
14527 	if (ret) {
14528 		dev_info(&pf->pdev->dev,
14529 			 "query veb bw ets config failed, err %s aq_err %s\n",
14530 			 i40e_stat_str(&pf->hw, ret),
14531 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14532 		goto out;
14533 	}
14534 
14535 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14536 	veb->bw_max_quanta = ets_data.tc_bw_max;
14537 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14538 	veb->enabled_tc = ets_data.tc_valid_bits;
14539 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14540 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14541 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14542 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14543 		veb->bw_tc_limit_credits[i] =
14544 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14545 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14546 	}
14547 
14548 out:
14549 	return ret;
14550 }
14551 
14552 /**
14553  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14554  * @pf: board private structure
14555  *
14556  * On error: returns error code (negative)
14557  * On success: returns vsi index in PF (positive)
14558  **/
14559 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14560 {
14561 	int ret = -ENOENT;
14562 	struct i40e_veb *veb;
14563 	int i;
14564 
14565 	/* Need to protect the allocation of switch elements at the PF level */
14566 	mutex_lock(&pf->switch_mutex);
14567 
14568 	/* VEB list may be fragmented if VEB creation/destruction has
14569 	 * been happening.  We can afford to do a quick scan to look
14570 	 * for any free slots in the list.
14571 	 *
14572 	 * find next empty veb slot, looping back around if necessary
14573 	 */
14574 	i = 0;
14575 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14576 		i++;
14577 	if (i >= I40E_MAX_VEB) {
14578 		ret = -ENOMEM;
14579 		goto err_alloc_veb;  /* out of VEB slots! */
14580 	}
14581 
14582 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14583 	if (!veb) {
14584 		ret = -ENOMEM;
14585 		goto err_alloc_veb;
14586 	}
14587 	veb->pf = pf;
14588 	veb->idx = i;
14589 	veb->enabled_tc = 1;
14590 
14591 	pf->veb[i] = veb;
14592 	ret = i;
14593 err_alloc_veb:
14594 	mutex_unlock(&pf->switch_mutex);
14595 	return ret;
14596 }
14597 
14598 /**
14599  * i40e_switch_branch_release - Delete a branch of the switch tree
14600  * @branch: where to start deleting
14601  *
14602  * This uses recursion to find the tips of the branch to be
14603  * removed, deleting until we get back to and can delete this VEB.
14604  **/
14605 static void i40e_switch_branch_release(struct i40e_veb *branch)
14606 {
14607 	struct i40e_pf *pf = branch->pf;
14608 	u16 branch_seid = branch->seid;
14609 	u16 veb_idx = branch->idx;
14610 	int i;
14611 
14612 	/* release any VEBs on this VEB - RECURSION */
14613 	for (i = 0; i < I40E_MAX_VEB; i++) {
14614 		if (!pf->veb[i])
14615 			continue;
14616 		if (pf->veb[i]->uplink_seid == branch->seid)
14617 			i40e_switch_branch_release(pf->veb[i]);
14618 	}
14619 
14620 	/* Release the VSIs on this VEB, but not the owner VSI.
14621 	 *
14622 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14623 	 *       the VEB itself, so don't use (*branch) after this loop.
14624 	 */
14625 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14626 		if (!pf->vsi[i])
14627 			continue;
14628 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14629 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14630 			i40e_vsi_release(pf->vsi[i]);
14631 		}
14632 	}
14633 
14634 	/* There's one corner case where the VEB might not have been
14635 	 * removed, so double check it here and remove it if needed.
14636 	 * This case happens if the veb was created from the debugfs
14637 	 * commands and no VSIs were added to it.
14638 	 */
14639 	if (pf->veb[veb_idx])
14640 		i40e_veb_release(pf->veb[veb_idx]);
14641 }
14642 
14643 /**
14644  * i40e_veb_clear - remove veb struct
14645  * @veb: the veb to remove
14646  **/
14647 static void i40e_veb_clear(struct i40e_veb *veb)
14648 {
14649 	if (!veb)
14650 		return;
14651 
14652 	if (veb->pf) {
14653 		struct i40e_pf *pf = veb->pf;
14654 
14655 		mutex_lock(&pf->switch_mutex);
14656 		if (pf->veb[veb->idx] == veb)
14657 			pf->veb[veb->idx] = NULL;
14658 		mutex_unlock(&pf->switch_mutex);
14659 	}
14660 
14661 	kfree(veb);
14662 }
14663 
14664 /**
14665  * i40e_veb_release - Delete a VEB and free its resources
14666  * @veb: the VEB being removed
14667  **/
14668 void i40e_veb_release(struct i40e_veb *veb)
14669 {
14670 	struct i40e_vsi *vsi = NULL;
14671 	struct i40e_pf *pf;
14672 	int i, n = 0;
14673 
14674 	pf = veb->pf;
14675 
14676 	/* find the remaining VSI and check for extras */
14677 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14678 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14679 			n++;
14680 			vsi = pf->vsi[i];
14681 		}
14682 	}
14683 	if (n != 1) {
14684 		dev_info(&pf->pdev->dev,
14685 			 "can't remove VEB %d with %d VSIs left\n",
14686 			 veb->seid, n);
14687 		return;
14688 	}
14689 
14690 	/* move the remaining VSI to uplink veb */
14691 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14692 	if (veb->uplink_seid) {
14693 		vsi->uplink_seid = veb->uplink_seid;
14694 		if (veb->uplink_seid == pf->mac_seid)
14695 			vsi->veb_idx = I40E_NO_VEB;
14696 		else
14697 			vsi->veb_idx = veb->veb_idx;
14698 	} else {
14699 		/* floating VEB */
14700 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14701 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14702 	}
14703 
14704 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14705 	i40e_veb_clear(veb);
14706 }
14707 
14708 /**
14709  * i40e_add_veb - create the VEB in the switch
14710  * @veb: the VEB to be instantiated
14711  * @vsi: the controlling VSI
14712  **/
14713 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14714 {
14715 	struct i40e_pf *pf = veb->pf;
14716 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14717 	int ret;
14718 
14719 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14720 			      veb->enabled_tc, false,
14721 			      &veb->seid, enable_stats, NULL);
14722 
14723 	/* get a VEB from the hardware */
14724 	if (ret) {
14725 		dev_info(&pf->pdev->dev,
14726 			 "couldn't add VEB, err %s aq_err %s\n",
14727 			 i40e_stat_str(&pf->hw, ret),
14728 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14729 		return -EPERM;
14730 	}
14731 
14732 	/* get statistics counter */
14733 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14734 					 &veb->stats_idx, NULL, NULL, NULL);
14735 	if (ret) {
14736 		dev_info(&pf->pdev->dev,
14737 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14738 			 i40e_stat_str(&pf->hw, ret),
14739 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14740 		return -EPERM;
14741 	}
14742 	ret = i40e_veb_get_bw_info(veb);
14743 	if (ret) {
14744 		dev_info(&pf->pdev->dev,
14745 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14746 			 i40e_stat_str(&pf->hw, ret),
14747 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14748 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14749 		return -ENOENT;
14750 	}
14751 
14752 	vsi->uplink_seid = veb->seid;
14753 	vsi->veb_idx = veb->idx;
14754 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14755 
14756 	return 0;
14757 }
14758 
14759 /**
14760  * i40e_veb_setup - Set up a VEB
14761  * @pf: board private structure
14762  * @flags: VEB setup flags
14763  * @uplink_seid: the switch element to link to
14764  * @vsi_seid: the initial VSI seid
14765  * @enabled_tc: Enabled TC bit-map
14766  *
14767  * This allocates the sw VEB structure and links it into the switch
14768  * It is possible and legal for this to be a duplicate of an already
14769  * existing VEB.  It is also possible for both uplink and vsi seids
14770  * to be zero, in order to create a floating VEB.
14771  *
14772  * Returns pointer to the successfully allocated VEB sw struct on
14773  * success, otherwise returns NULL on failure.
14774  **/
14775 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14776 				u16 uplink_seid, u16 vsi_seid,
14777 				u8 enabled_tc)
14778 {
14779 	struct i40e_veb *veb, *uplink_veb = NULL;
14780 	int vsi_idx, veb_idx;
14781 	int ret;
14782 
14783 	/* if one seid is 0, the other must be 0 to create a floating relay */
14784 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14785 	    (uplink_seid + vsi_seid != 0)) {
14786 		dev_info(&pf->pdev->dev,
14787 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14788 			 uplink_seid, vsi_seid);
14789 		return NULL;
14790 	}
14791 
14792 	/* make sure there is such a vsi and uplink */
14793 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14794 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14795 			break;
14796 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14797 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14798 			 vsi_seid);
14799 		return NULL;
14800 	}
14801 
14802 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14803 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14804 			if (pf->veb[veb_idx] &&
14805 			    pf->veb[veb_idx]->seid == uplink_seid) {
14806 				uplink_veb = pf->veb[veb_idx];
14807 				break;
14808 			}
14809 		}
14810 		if (!uplink_veb) {
14811 			dev_info(&pf->pdev->dev,
14812 				 "uplink seid %d not found\n", uplink_seid);
14813 			return NULL;
14814 		}
14815 	}
14816 
14817 	/* get veb sw struct */
14818 	veb_idx = i40e_veb_mem_alloc(pf);
14819 	if (veb_idx < 0)
14820 		goto err_alloc;
14821 	veb = pf->veb[veb_idx];
14822 	veb->flags = flags;
14823 	veb->uplink_seid = uplink_seid;
14824 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14825 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14826 
14827 	/* create the VEB in the switch */
14828 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14829 	if (ret)
14830 		goto err_veb;
14831 	if (vsi_idx == pf->lan_vsi)
14832 		pf->lan_veb = veb->idx;
14833 
14834 	return veb;
14835 
14836 err_veb:
14837 	i40e_veb_clear(veb);
14838 err_alloc:
14839 	return NULL;
14840 }
14841 
14842 /**
14843  * i40e_setup_pf_switch_element - set PF vars based on switch type
14844  * @pf: board private structure
14845  * @ele: element we are building info from
14846  * @num_reported: total number of elements
14847  * @printconfig: should we print the contents
14848  *
14849  * helper function to assist in extracting a few useful SEID values.
14850  **/
14851 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14852 				struct i40e_aqc_switch_config_element_resp *ele,
14853 				u16 num_reported, bool printconfig)
14854 {
14855 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14856 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14857 	u8 element_type = ele->element_type;
14858 	u16 seid = le16_to_cpu(ele->seid);
14859 
14860 	if (printconfig)
14861 		dev_info(&pf->pdev->dev,
14862 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14863 			 element_type, seid, uplink_seid, downlink_seid);
14864 
14865 	switch (element_type) {
14866 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14867 		pf->mac_seid = seid;
14868 		break;
14869 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14870 		/* Main VEB? */
14871 		if (uplink_seid != pf->mac_seid)
14872 			break;
14873 		if (pf->lan_veb >= I40E_MAX_VEB) {
14874 			int v;
14875 
14876 			/* find existing or else empty VEB */
14877 			for (v = 0; v < I40E_MAX_VEB; v++) {
14878 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14879 					pf->lan_veb = v;
14880 					break;
14881 				}
14882 			}
14883 			if (pf->lan_veb >= I40E_MAX_VEB) {
14884 				v = i40e_veb_mem_alloc(pf);
14885 				if (v < 0)
14886 					break;
14887 				pf->lan_veb = v;
14888 			}
14889 		}
14890 		if (pf->lan_veb >= I40E_MAX_VEB)
14891 			break;
14892 
14893 		pf->veb[pf->lan_veb]->seid = seid;
14894 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14895 		pf->veb[pf->lan_veb]->pf = pf;
14896 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14897 		break;
14898 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14899 		if (num_reported != 1)
14900 			break;
14901 		/* This is immediately after a reset so we can assume this is
14902 		 * the PF's VSI
14903 		 */
14904 		pf->mac_seid = uplink_seid;
14905 		pf->pf_seid = downlink_seid;
14906 		pf->main_vsi_seid = seid;
14907 		if (printconfig)
14908 			dev_info(&pf->pdev->dev,
14909 				 "pf_seid=%d main_vsi_seid=%d\n",
14910 				 pf->pf_seid, pf->main_vsi_seid);
14911 		break;
14912 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14913 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14914 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14915 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14916 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14917 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14918 		/* ignore these for now */
14919 		break;
14920 	default:
14921 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14922 			 element_type, seid);
14923 		break;
14924 	}
14925 }
14926 
14927 /**
14928  * i40e_fetch_switch_configuration - Get switch config from firmware
14929  * @pf: board private structure
14930  * @printconfig: should we print the contents
14931  *
14932  * Get the current switch configuration from the device and
14933  * extract a few useful SEID values.
14934  **/
14935 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14936 {
14937 	struct i40e_aqc_get_switch_config_resp *sw_config;
14938 	u16 next_seid = 0;
14939 	int ret = 0;
14940 	u8 *aq_buf;
14941 	int i;
14942 
14943 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14944 	if (!aq_buf)
14945 		return -ENOMEM;
14946 
14947 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14948 	do {
14949 		u16 num_reported, num_total;
14950 
14951 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14952 						I40E_AQ_LARGE_BUF,
14953 						&next_seid, NULL);
14954 		if (ret) {
14955 			dev_info(&pf->pdev->dev,
14956 				 "get switch config failed err %s aq_err %s\n",
14957 				 i40e_stat_str(&pf->hw, ret),
14958 				 i40e_aq_str(&pf->hw,
14959 					     pf->hw.aq.asq_last_status));
14960 			kfree(aq_buf);
14961 			return -ENOENT;
14962 		}
14963 
14964 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14965 		num_total = le16_to_cpu(sw_config->header.num_total);
14966 
14967 		if (printconfig)
14968 			dev_info(&pf->pdev->dev,
14969 				 "header: %d reported %d total\n",
14970 				 num_reported, num_total);
14971 
14972 		for (i = 0; i < num_reported; i++) {
14973 			struct i40e_aqc_switch_config_element_resp *ele =
14974 				&sw_config->element[i];
14975 
14976 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14977 						     printconfig);
14978 		}
14979 	} while (next_seid != 0);
14980 
14981 	kfree(aq_buf);
14982 	return ret;
14983 }
14984 
14985 /**
14986  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14987  * @pf: board private structure
14988  * @reinit: if the Main VSI needs to re-initialized.
14989  * @lock_acquired: indicates whether or not the lock has been acquired
14990  *
14991  * Returns 0 on success, negative value on failure
14992  **/
14993 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14994 {
14995 	u16 flags = 0;
14996 	int ret;
14997 
14998 	/* find out what's out there already */
14999 	ret = i40e_fetch_switch_configuration(pf, false);
15000 	if (ret) {
15001 		dev_info(&pf->pdev->dev,
15002 			 "couldn't fetch switch config, err %s aq_err %s\n",
15003 			 i40e_stat_str(&pf->hw, ret),
15004 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15005 		return ret;
15006 	}
15007 	i40e_pf_reset_stats(pf);
15008 
15009 	/* set the switch config bit for the whole device to
15010 	 * support limited promisc or true promisc
15011 	 * when user requests promisc. The default is limited
15012 	 * promisc.
15013 	*/
15014 
15015 	if ((pf->hw.pf_id == 0) &&
15016 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15017 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15018 		pf->last_sw_conf_flags = flags;
15019 	}
15020 
15021 	if (pf->hw.pf_id == 0) {
15022 		u16 valid_flags;
15023 
15024 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15025 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15026 						NULL);
15027 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15028 			dev_info(&pf->pdev->dev,
15029 				 "couldn't set switch config bits, err %s aq_err %s\n",
15030 				 i40e_stat_str(&pf->hw, ret),
15031 				 i40e_aq_str(&pf->hw,
15032 					     pf->hw.aq.asq_last_status));
15033 			/* not a fatal problem, just keep going */
15034 		}
15035 		pf->last_sw_conf_valid_flags = valid_flags;
15036 	}
15037 
15038 	/* first time setup */
15039 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15040 		struct i40e_vsi *vsi = NULL;
15041 		u16 uplink_seid;
15042 
15043 		/* Set up the PF VSI associated with the PF's main VSI
15044 		 * that is already in the HW switch
15045 		 */
15046 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15047 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15048 		else
15049 			uplink_seid = pf->mac_seid;
15050 		if (pf->lan_vsi == I40E_NO_VSI)
15051 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15052 		else if (reinit)
15053 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15054 		if (!vsi) {
15055 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15056 			i40e_cloud_filter_exit(pf);
15057 			i40e_fdir_teardown(pf);
15058 			return -EAGAIN;
15059 		}
15060 	} else {
15061 		/* force a reset of TC and queue layout configurations */
15062 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15063 
15064 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15065 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15066 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15067 	}
15068 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15069 
15070 	i40e_fdir_sb_setup(pf);
15071 
15072 	/* Setup static PF queue filter control settings */
15073 	ret = i40e_setup_pf_filter_control(pf);
15074 	if (ret) {
15075 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15076 			 ret);
15077 		/* Failure here should not stop continuing other steps */
15078 	}
15079 
15080 	/* enable RSS in the HW, even for only one queue, as the stack can use
15081 	 * the hash
15082 	 */
15083 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15084 		i40e_pf_config_rss(pf);
15085 
15086 	/* fill in link information and enable LSE reporting */
15087 	i40e_link_event(pf);
15088 
15089 	/* Initialize user-specific link properties */
15090 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15091 				  I40E_AQ_AN_COMPLETED) ? true : false);
15092 
15093 	i40e_ptp_init(pf);
15094 
15095 	if (!lock_acquired)
15096 		rtnl_lock();
15097 
15098 	/* repopulate tunnel port filters */
15099 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15100 
15101 	if (!lock_acquired)
15102 		rtnl_unlock();
15103 
15104 	return ret;
15105 }
15106 
15107 /**
15108  * i40e_determine_queue_usage - Work out queue distribution
15109  * @pf: board private structure
15110  **/
15111 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15112 {
15113 	int queues_left;
15114 	int q_max;
15115 
15116 	pf->num_lan_qps = 0;
15117 
15118 	/* Find the max queues to be put into basic use.  We'll always be
15119 	 * using TC0, whether or not DCB is running, and TC0 will get the
15120 	 * big RSS set.
15121 	 */
15122 	queues_left = pf->hw.func_caps.num_tx_qp;
15123 
15124 	if ((queues_left == 1) ||
15125 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15126 		/* one qp for PF, no queues for anything else */
15127 		queues_left = 0;
15128 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15129 
15130 		/* make sure all the fancies are disabled */
15131 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15132 			       I40E_FLAG_IWARP_ENABLED	|
15133 			       I40E_FLAG_FD_SB_ENABLED	|
15134 			       I40E_FLAG_FD_ATR_ENABLED	|
15135 			       I40E_FLAG_DCB_CAPABLE	|
15136 			       I40E_FLAG_DCB_ENABLED	|
15137 			       I40E_FLAG_SRIOV_ENABLED	|
15138 			       I40E_FLAG_VMDQ_ENABLED);
15139 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15140 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15141 				  I40E_FLAG_FD_SB_ENABLED |
15142 				  I40E_FLAG_FD_ATR_ENABLED |
15143 				  I40E_FLAG_DCB_CAPABLE))) {
15144 		/* one qp for PF */
15145 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15146 		queues_left -= pf->num_lan_qps;
15147 
15148 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15149 			       I40E_FLAG_IWARP_ENABLED	|
15150 			       I40E_FLAG_FD_SB_ENABLED	|
15151 			       I40E_FLAG_FD_ATR_ENABLED	|
15152 			       I40E_FLAG_DCB_ENABLED	|
15153 			       I40E_FLAG_VMDQ_ENABLED);
15154 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15155 	} else {
15156 		/* Not enough queues for all TCs */
15157 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15158 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15159 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15160 					I40E_FLAG_DCB_ENABLED);
15161 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15162 		}
15163 
15164 		/* limit lan qps to the smaller of qps, cpus or msix */
15165 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15166 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15167 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15168 		pf->num_lan_qps = q_max;
15169 
15170 		queues_left -= pf->num_lan_qps;
15171 	}
15172 
15173 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15174 		if (queues_left > 1) {
15175 			queues_left -= 1; /* save 1 queue for FD */
15176 		} else {
15177 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15178 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15179 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15180 		}
15181 	}
15182 
15183 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15184 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15185 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15186 					(queues_left / pf->num_vf_qps));
15187 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15188 	}
15189 
15190 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15191 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15192 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15193 					  (queues_left / pf->num_vmdq_qps));
15194 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15195 	}
15196 
15197 	pf->queues_left = queues_left;
15198 	dev_dbg(&pf->pdev->dev,
15199 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15200 		pf->hw.func_caps.num_tx_qp,
15201 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15202 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15203 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15204 		queues_left);
15205 }
15206 
15207 /**
15208  * i40e_setup_pf_filter_control - Setup PF static filter control
15209  * @pf: PF to be setup
15210  *
15211  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15212  * settings. If PE/FCoE are enabled then it will also set the per PF
15213  * based filter sizes required for them. It also enables Flow director,
15214  * ethertype and macvlan type filter settings for the pf.
15215  *
15216  * Returns 0 on success, negative on failure
15217  **/
15218 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15219 {
15220 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15221 
15222 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15223 
15224 	/* Flow Director is enabled */
15225 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15226 		settings->enable_fdir = true;
15227 
15228 	/* Ethtype and MACVLAN filters enabled for PF */
15229 	settings->enable_ethtype = true;
15230 	settings->enable_macvlan = true;
15231 
15232 	if (i40e_set_filter_control(&pf->hw, settings))
15233 		return -ENOENT;
15234 
15235 	return 0;
15236 }
15237 
15238 #define INFO_STRING_LEN 255
15239 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15240 static void i40e_print_features(struct i40e_pf *pf)
15241 {
15242 	struct i40e_hw *hw = &pf->hw;
15243 	char *buf;
15244 	int i;
15245 
15246 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15247 	if (!buf)
15248 		return;
15249 
15250 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15251 #ifdef CONFIG_PCI_IOV
15252 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15253 #endif
15254 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15255 		      pf->hw.func_caps.num_vsis,
15256 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15257 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15258 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15259 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15260 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15261 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15262 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15263 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15264 	}
15265 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15266 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15267 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15268 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15269 	if (pf->flags & I40E_FLAG_PTP)
15270 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15271 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15272 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15273 	else
15274 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15275 
15276 	dev_info(&pf->pdev->dev, "%s\n", buf);
15277 	kfree(buf);
15278 	WARN_ON(i > INFO_STRING_LEN);
15279 }
15280 
15281 /**
15282  * i40e_get_platform_mac_addr - get platform-specific MAC address
15283  * @pdev: PCI device information struct
15284  * @pf: board private structure
15285  *
15286  * Look up the MAC address for the device. First we'll try
15287  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15288  * specific fallback. Otherwise, we'll default to the stored value in
15289  * firmware.
15290  **/
15291 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15292 {
15293 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15294 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15295 }
15296 
15297 /**
15298  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15299  * @fec_cfg: FEC option to set in flags
15300  * @flags: ptr to flags in which we set FEC option
15301  **/
15302 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15303 {
15304 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15305 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15306 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15307 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15308 		*flags |= I40E_FLAG_RS_FEC;
15309 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15310 	}
15311 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15312 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15313 		*flags |= I40E_FLAG_BASE_R_FEC;
15314 		*flags &= ~I40E_FLAG_RS_FEC;
15315 	}
15316 	if (fec_cfg == 0)
15317 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15318 }
15319 
15320 /**
15321  * i40e_check_recovery_mode - check if we are running transition firmware
15322  * @pf: board private structure
15323  *
15324  * Check registers indicating the firmware runs in recovery mode. Sets the
15325  * appropriate driver state.
15326  *
15327  * Returns true if the recovery mode was detected, false otherwise
15328  **/
15329 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15330 {
15331 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15332 
15333 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15334 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15335 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15336 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15337 
15338 		return true;
15339 	}
15340 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15341 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15342 
15343 	return false;
15344 }
15345 
15346 /**
15347  * i40e_pf_loop_reset - perform reset in a loop.
15348  * @pf: board private structure
15349  *
15350  * This function is useful when a NIC is about to enter recovery mode.
15351  * When a NIC's internal data structures are corrupted the NIC's
15352  * firmware is going to enter recovery mode.
15353  * Right after a POR it takes about 7 minutes for firmware to enter
15354  * recovery mode. Until that time a NIC is in some kind of intermediate
15355  * state. After that time period the NIC almost surely enters
15356  * recovery mode. The only way for a driver to detect intermediate
15357  * state is to issue a series of pf-resets and check a return value.
15358  * If a PF reset returns success then the firmware could be in recovery
15359  * mode so the caller of this code needs to check for recovery mode
15360  * if this function returns success. There is a little chance that
15361  * firmware will hang in intermediate state forever.
15362  * Since waiting 7 minutes is quite a lot of time this function waits
15363  * 10 seconds and then gives up by returning an error.
15364  *
15365  * Return 0 on success, negative on failure.
15366  **/
15367 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15368 {
15369 	/* wait max 10 seconds for PF reset to succeed */
15370 	const unsigned long time_end = jiffies + 10 * HZ;
15371 
15372 	struct i40e_hw *hw = &pf->hw;
15373 	i40e_status ret;
15374 
15375 	ret = i40e_pf_reset(hw);
15376 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15377 		usleep_range(10000, 20000);
15378 		ret = i40e_pf_reset(hw);
15379 	}
15380 
15381 	if (ret == I40E_SUCCESS)
15382 		pf->pfr_count++;
15383 	else
15384 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15385 
15386 	return ret;
15387 }
15388 
15389 /**
15390  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15391  * @pf: board private structure
15392  *
15393  * Check FW registers to determine if FW issued unexpected EMP Reset.
15394  * Every time when unexpected EMP Reset occurs the FW increments
15395  * a counter of unexpected EMP Resets. When the counter reaches 10
15396  * the FW should enter the Recovery mode
15397  *
15398  * Returns true if FW issued unexpected EMP Reset
15399  **/
15400 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15401 {
15402 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15403 			   I40E_GL_FWSTS_FWS1B_MASK;
15404 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15405 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15406 }
15407 
15408 /**
15409  * i40e_handle_resets - handle EMP resets and PF resets
15410  * @pf: board private structure
15411  *
15412  * Handle both EMP resets and PF resets and conclude whether there are
15413  * any issues regarding these resets. If there are any issues then
15414  * generate log entry.
15415  *
15416  * Return 0 if NIC is healthy or negative value when there are issues
15417  * with resets
15418  **/
15419 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15420 {
15421 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15422 	const bool is_empr = i40e_check_fw_empr(pf);
15423 
15424 	if (is_empr || pfr != I40E_SUCCESS)
15425 		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");
15426 
15427 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15428 }
15429 
15430 /**
15431  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15432  * @pf: board private structure
15433  * @hw: ptr to the hardware info
15434  *
15435  * This function does a minimal setup of all subsystems needed for running
15436  * recovery mode.
15437  *
15438  * Returns 0 on success, negative on failure
15439  **/
15440 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15441 {
15442 	struct i40e_vsi *vsi;
15443 	int err;
15444 	int v_idx;
15445 
15446 	pci_save_state(pf->pdev);
15447 
15448 	/* set up periodic task facility */
15449 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15450 	pf->service_timer_period = HZ;
15451 
15452 	INIT_WORK(&pf->service_task, i40e_service_task);
15453 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15454 
15455 	err = i40e_init_interrupt_scheme(pf);
15456 	if (err)
15457 		goto err_switch_setup;
15458 
15459 	/* The number of VSIs reported by the FW is the minimum guaranteed
15460 	 * to us; HW supports far more and we share the remaining pool with
15461 	 * the other PFs. We allocate space for more than the guarantee with
15462 	 * the understanding that we might not get them all later.
15463 	 */
15464 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15465 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15466 	else
15467 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15468 
15469 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15470 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15471 			  GFP_KERNEL);
15472 	if (!pf->vsi) {
15473 		err = -ENOMEM;
15474 		goto err_switch_setup;
15475 	}
15476 
15477 	/* We allocate one VSI which is needed as absolute minimum
15478 	 * in order to register the netdev
15479 	 */
15480 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15481 	if (v_idx < 0) {
15482 		err = v_idx;
15483 		goto err_switch_setup;
15484 	}
15485 	pf->lan_vsi = v_idx;
15486 	vsi = pf->vsi[v_idx];
15487 	if (!vsi) {
15488 		err = -EFAULT;
15489 		goto err_switch_setup;
15490 	}
15491 	vsi->alloc_queue_pairs = 1;
15492 	err = i40e_config_netdev(vsi);
15493 	if (err)
15494 		goto err_switch_setup;
15495 	err = register_netdev(vsi->netdev);
15496 	if (err)
15497 		goto err_switch_setup;
15498 	vsi->netdev_registered = true;
15499 	i40e_dbg_pf_init(pf);
15500 
15501 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15502 	if (err)
15503 		goto err_switch_setup;
15504 
15505 	/* tell the firmware that we're starting */
15506 	i40e_send_version(pf);
15507 
15508 	/* since everything's happy, start the service_task timer */
15509 	mod_timer(&pf->service_timer,
15510 		  round_jiffies(jiffies + pf->service_timer_period));
15511 
15512 	return 0;
15513 
15514 err_switch_setup:
15515 	i40e_reset_interrupt_capability(pf);
15516 	del_timer_sync(&pf->service_timer);
15517 	i40e_shutdown_adminq(hw);
15518 	iounmap(hw->hw_addr);
15519 	pci_disable_pcie_error_reporting(pf->pdev);
15520 	pci_release_mem_regions(pf->pdev);
15521 	pci_disable_device(pf->pdev);
15522 	kfree(pf);
15523 
15524 	return err;
15525 }
15526 
15527 /**
15528  * i40e_set_subsystem_device_id - set subsystem device id
15529  * @hw: pointer to the hardware info
15530  *
15531  * Set PCI subsystem device id either from a pci_dev structure or
15532  * a specific FW register.
15533  **/
15534 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15535 {
15536 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15537 
15538 	hw->subsystem_device_id = pdev->subsystem_device ?
15539 		pdev->subsystem_device :
15540 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15541 }
15542 
15543 /**
15544  * i40e_probe - Device initialization routine
15545  * @pdev: PCI device information struct
15546  * @ent: entry in i40e_pci_tbl
15547  *
15548  * i40e_probe initializes a PF identified by a pci_dev structure.
15549  * The OS initialization, configuring of the PF private structure,
15550  * and a hardware reset occur.
15551  *
15552  * Returns 0 on success, negative on failure
15553  **/
15554 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15555 {
15556 	struct i40e_aq_get_phy_abilities_resp abilities;
15557 #ifdef CONFIG_I40E_DCB
15558 	enum i40e_get_fw_lldp_status_resp lldp_status;
15559 	i40e_status status;
15560 #endif /* CONFIG_I40E_DCB */
15561 	struct i40e_pf *pf;
15562 	struct i40e_hw *hw;
15563 	static u16 pfs_found;
15564 	u16 wol_nvm_bits;
15565 	u16 link_status;
15566 	int err;
15567 	u32 val;
15568 	u32 i;
15569 
15570 	err = pci_enable_device_mem(pdev);
15571 	if (err)
15572 		return err;
15573 
15574 	/* set up for high or low dma */
15575 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15576 	if (err) {
15577 		dev_err(&pdev->dev,
15578 			"DMA configuration failed: 0x%x\n", err);
15579 		goto err_dma;
15580 	}
15581 
15582 	/* set up pci connections */
15583 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15584 	if (err) {
15585 		dev_info(&pdev->dev,
15586 			 "pci_request_selected_regions failed %d\n", err);
15587 		goto err_pci_reg;
15588 	}
15589 
15590 	pci_enable_pcie_error_reporting(pdev);
15591 	pci_set_master(pdev);
15592 
15593 	/* Now that we have a PCI connection, we need to do the
15594 	 * low level device setup.  This is primarily setting up
15595 	 * the Admin Queue structures and then querying for the
15596 	 * device's current profile information.
15597 	 */
15598 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15599 	if (!pf) {
15600 		err = -ENOMEM;
15601 		goto err_pf_alloc;
15602 	}
15603 	pf->next_vsi = 0;
15604 	pf->pdev = pdev;
15605 	set_bit(__I40E_DOWN, pf->state);
15606 
15607 	hw = &pf->hw;
15608 	hw->back = pf;
15609 
15610 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15611 				I40E_MAX_CSR_SPACE);
15612 	/* We believe that the highest register to read is
15613 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15614 	 * is not less than that before mapping to prevent a
15615 	 * kernel panic.
15616 	 */
15617 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15618 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15619 			pf->ioremap_len);
15620 		err = -ENOMEM;
15621 		goto err_ioremap;
15622 	}
15623 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15624 	if (!hw->hw_addr) {
15625 		err = -EIO;
15626 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15627 			 (unsigned int)pci_resource_start(pdev, 0),
15628 			 pf->ioremap_len, err);
15629 		goto err_ioremap;
15630 	}
15631 	hw->vendor_id = pdev->vendor;
15632 	hw->device_id = pdev->device;
15633 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15634 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15635 	i40e_set_subsystem_device_id(hw);
15636 	hw->bus.device = PCI_SLOT(pdev->devfn);
15637 	hw->bus.func = PCI_FUNC(pdev->devfn);
15638 	hw->bus.bus_id = pdev->bus->number;
15639 	pf->instance = pfs_found;
15640 
15641 	/* Select something other than the 802.1ad ethertype for the
15642 	 * switch to use internally and drop on ingress.
15643 	 */
15644 	hw->switch_tag = 0xffff;
15645 	hw->first_tag = ETH_P_8021AD;
15646 	hw->second_tag = ETH_P_8021Q;
15647 
15648 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15649 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15650 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15651 
15652 	/* set up the locks for the AQ, do this only once in probe
15653 	 * and destroy them only once in remove
15654 	 */
15655 	mutex_init(&hw->aq.asq_mutex);
15656 	mutex_init(&hw->aq.arq_mutex);
15657 
15658 	pf->msg_enable = netif_msg_init(debug,
15659 					NETIF_MSG_DRV |
15660 					NETIF_MSG_PROBE |
15661 					NETIF_MSG_LINK);
15662 	if (debug < -1)
15663 		pf->hw.debug_mask = debug;
15664 
15665 	/* do a special CORER for clearing PXE mode once at init */
15666 	if (hw->revision_id == 0 &&
15667 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15668 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15669 		i40e_flush(hw);
15670 		msleep(200);
15671 		pf->corer_count++;
15672 
15673 		i40e_clear_pxe_mode(hw);
15674 	}
15675 
15676 	/* Reset here to make sure all is clean and to define PF 'n' */
15677 	i40e_clear_hw(hw);
15678 
15679 	err = i40e_set_mac_type(hw);
15680 	if (err) {
15681 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15682 			 err);
15683 		goto err_pf_reset;
15684 	}
15685 
15686 	err = i40e_handle_resets(pf);
15687 	if (err)
15688 		goto err_pf_reset;
15689 
15690 	i40e_check_recovery_mode(pf);
15691 
15692 	if (is_kdump_kernel()) {
15693 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15694 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15695 	} else {
15696 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15697 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15698 	}
15699 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15700 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15701 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15702 
15703 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15704 		 "%s-%s:misc",
15705 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15706 
15707 	err = i40e_init_shared_code(hw);
15708 	if (err) {
15709 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15710 			 err);
15711 		goto err_pf_reset;
15712 	}
15713 
15714 	/* set up a default setting for link flow control */
15715 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15716 
15717 	err = i40e_init_adminq(hw);
15718 	if (err) {
15719 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15720 			dev_info(&pdev->dev,
15721 				 "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",
15722 				 hw->aq.api_maj_ver,
15723 				 hw->aq.api_min_ver,
15724 				 I40E_FW_API_VERSION_MAJOR,
15725 				 I40E_FW_MINOR_VERSION(hw));
15726 		else
15727 			dev_info(&pdev->dev,
15728 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15729 
15730 		goto err_pf_reset;
15731 	}
15732 	i40e_get_oem_version(hw);
15733 
15734 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15735 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15736 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15737 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15738 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15739 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15740 
15741 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15742 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15743 		dev_dbg(&pdev->dev,
15744 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15745 			 hw->aq.api_maj_ver,
15746 			 hw->aq.api_min_ver,
15747 			 I40E_FW_API_VERSION_MAJOR,
15748 			 I40E_FW_MINOR_VERSION(hw));
15749 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15750 		dev_info(&pdev->dev,
15751 			 "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",
15752 			 hw->aq.api_maj_ver,
15753 			 hw->aq.api_min_ver,
15754 			 I40E_FW_API_VERSION_MAJOR,
15755 			 I40E_FW_MINOR_VERSION(hw));
15756 
15757 	i40e_verify_eeprom(pf);
15758 
15759 	/* Rev 0 hardware was never productized */
15760 	if (hw->revision_id < 1)
15761 		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");
15762 
15763 	i40e_clear_pxe_mode(hw);
15764 
15765 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15766 	if (err)
15767 		goto err_adminq_setup;
15768 
15769 	err = i40e_sw_init(pf);
15770 	if (err) {
15771 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15772 		goto err_sw_init;
15773 	}
15774 
15775 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15776 		return i40e_init_recovery_mode(pf, hw);
15777 
15778 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15779 				hw->func_caps.num_rx_qp, 0, 0);
15780 	if (err) {
15781 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15782 		goto err_init_lan_hmc;
15783 	}
15784 
15785 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15786 	if (err) {
15787 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15788 		err = -ENOENT;
15789 		goto err_configure_lan_hmc;
15790 	}
15791 
15792 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15793 	 * Ignore error return codes because if it was already disabled via
15794 	 * hardware settings this will fail
15795 	 */
15796 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15797 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15798 		i40e_aq_stop_lldp(hw, true, false, NULL);
15799 	}
15800 
15801 	/* allow a platform config to override the HW addr */
15802 	i40e_get_platform_mac_addr(pdev, pf);
15803 
15804 	if (!is_valid_ether_addr(hw->mac.addr)) {
15805 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15806 		err = -EIO;
15807 		goto err_mac_addr;
15808 	}
15809 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15810 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15811 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15812 	if (is_valid_ether_addr(hw->mac.port_addr))
15813 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15814 
15815 	i40e_ptp_alloc_pins(pf);
15816 	pci_set_drvdata(pdev, pf);
15817 	pci_save_state(pdev);
15818 
15819 #ifdef CONFIG_I40E_DCB
15820 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15821 	(!status &&
15822 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15823 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15824 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15825 	dev_info(&pdev->dev,
15826 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15827 			"FW LLDP is disabled\n" :
15828 			"FW LLDP is enabled\n");
15829 
15830 	/* Enable FW to write default DCB config on link-up */
15831 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15832 
15833 	err = i40e_init_pf_dcb(pf);
15834 	if (err) {
15835 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15836 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15837 		/* Continue without DCB enabled */
15838 	}
15839 #endif /* CONFIG_I40E_DCB */
15840 
15841 	/* set up periodic task facility */
15842 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15843 	pf->service_timer_period = HZ;
15844 
15845 	INIT_WORK(&pf->service_task, i40e_service_task);
15846 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15847 
15848 	/* NVM bit on means WoL disabled for the port */
15849 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15850 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15851 		pf->wol_en = false;
15852 	else
15853 		pf->wol_en = true;
15854 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15855 
15856 	/* set up the main switch operations */
15857 	i40e_determine_queue_usage(pf);
15858 	err = i40e_init_interrupt_scheme(pf);
15859 	if (err)
15860 		goto err_switch_setup;
15861 
15862 	/* Reduce Tx and Rx pairs for kdump
15863 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15864 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15865 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15866 	 */
15867 	if (is_kdump_kernel())
15868 		pf->num_lan_msix = 1;
15869 
15870 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15871 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15872 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15873 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15874 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15875 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15876 						    UDP_TUNNEL_TYPE_GENEVE;
15877 
15878 	/* The number of VSIs reported by the FW is the minimum guaranteed
15879 	 * to us; HW supports far more and we share the remaining pool with
15880 	 * the other PFs. We allocate space for more than the guarantee with
15881 	 * the understanding that we might not get them all later.
15882 	 */
15883 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15884 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15885 	else
15886 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15887 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15888 		dev_warn(&pf->pdev->dev,
15889 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15890 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15891 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15892 	}
15893 
15894 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15895 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15896 			  GFP_KERNEL);
15897 	if (!pf->vsi) {
15898 		err = -ENOMEM;
15899 		goto err_switch_setup;
15900 	}
15901 
15902 #ifdef CONFIG_PCI_IOV
15903 	/* prep for VF support */
15904 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15905 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15906 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15907 		if (pci_num_vf(pdev))
15908 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15909 	}
15910 #endif
15911 	err = i40e_setup_pf_switch(pf, false, false);
15912 	if (err) {
15913 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15914 		goto err_vsis;
15915 	}
15916 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15917 
15918 	/* if FDIR VSI was set up, start it now */
15919 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15920 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15921 			i40e_vsi_open(pf->vsi[i]);
15922 			break;
15923 		}
15924 	}
15925 
15926 	/* The driver only wants link up/down and module qualification
15927 	 * reports from firmware.  Note the negative logic.
15928 	 */
15929 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15930 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15931 					 I40E_AQ_EVENT_MEDIA_NA |
15932 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15933 	if (err)
15934 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15935 			 i40e_stat_str(&pf->hw, err),
15936 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15937 
15938 	/* Reconfigure hardware for allowing smaller MSS in the case
15939 	 * of TSO, so that we avoid the MDD being fired and causing
15940 	 * a reset in the case of small MSS+TSO.
15941 	 */
15942 	val = rd32(hw, I40E_REG_MSS);
15943 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15944 		val &= ~I40E_REG_MSS_MIN_MASK;
15945 		val |= I40E_64BYTE_MSS;
15946 		wr32(hw, I40E_REG_MSS, val);
15947 	}
15948 
15949 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15950 		msleep(75);
15951 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15952 		if (err)
15953 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15954 				 i40e_stat_str(&pf->hw, err),
15955 				 i40e_aq_str(&pf->hw,
15956 					     pf->hw.aq.asq_last_status));
15957 	}
15958 	/* The main driver is (mostly) up and happy. We need to set this state
15959 	 * before setting up the misc vector or we get a race and the vector
15960 	 * ends up disabled forever.
15961 	 */
15962 	clear_bit(__I40E_DOWN, pf->state);
15963 
15964 	/* In case of MSIX we are going to setup the misc vector right here
15965 	 * to handle admin queue events etc. In case of legacy and MSI
15966 	 * the misc functionality and queue processing is combined in
15967 	 * the same vector and that gets setup at open.
15968 	 */
15969 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15970 		err = i40e_setup_misc_vector(pf);
15971 		if (err) {
15972 			dev_info(&pdev->dev,
15973 				 "setup of misc vector failed: %d\n", err);
15974 			i40e_cloud_filter_exit(pf);
15975 			i40e_fdir_teardown(pf);
15976 			goto err_vsis;
15977 		}
15978 	}
15979 
15980 #ifdef CONFIG_PCI_IOV
15981 	/* prep for VF support */
15982 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15983 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15984 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15985 		/* disable link interrupts for VFs */
15986 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15987 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15988 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15989 		i40e_flush(hw);
15990 
15991 		if (pci_num_vf(pdev)) {
15992 			dev_info(&pdev->dev,
15993 				 "Active VFs found, allocating resources.\n");
15994 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15995 			if (err)
15996 				dev_info(&pdev->dev,
15997 					 "Error %d allocating resources for existing VFs\n",
15998 					 err);
15999 		}
16000 	}
16001 #endif /* CONFIG_PCI_IOV */
16002 
16003 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16004 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16005 						      pf->num_iwarp_msix,
16006 						      I40E_IWARP_IRQ_PILE_ID);
16007 		if (pf->iwarp_base_vector < 0) {
16008 			dev_info(&pdev->dev,
16009 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16010 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16011 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16012 		}
16013 	}
16014 
16015 	i40e_dbg_pf_init(pf);
16016 
16017 	/* tell the firmware that we're starting */
16018 	i40e_send_version(pf);
16019 
16020 	/* since everything's happy, start the service_task timer */
16021 	mod_timer(&pf->service_timer,
16022 		  round_jiffies(jiffies + pf->service_timer_period));
16023 
16024 	/* add this PF to client device list and launch a client service task */
16025 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16026 		err = i40e_lan_add_device(pf);
16027 		if (err)
16028 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16029 				 err);
16030 	}
16031 
16032 #define PCI_SPEED_SIZE 8
16033 #define PCI_WIDTH_SIZE 8
16034 	/* Devices on the IOSF bus do not have this information
16035 	 * and will report PCI Gen 1 x 1 by default so don't bother
16036 	 * checking them.
16037 	 */
16038 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16039 		char speed[PCI_SPEED_SIZE] = "Unknown";
16040 		char width[PCI_WIDTH_SIZE] = "Unknown";
16041 
16042 		/* Get the negotiated link width and speed from PCI config
16043 		 * space
16044 		 */
16045 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16046 					  &link_status);
16047 
16048 		i40e_set_pci_config_data(hw, link_status);
16049 
16050 		switch (hw->bus.speed) {
16051 		case i40e_bus_speed_8000:
16052 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
16053 		case i40e_bus_speed_5000:
16054 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
16055 		case i40e_bus_speed_2500:
16056 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
16057 		default:
16058 			break;
16059 		}
16060 		switch (hw->bus.width) {
16061 		case i40e_bus_width_pcie_x8:
16062 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
16063 		case i40e_bus_width_pcie_x4:
16064 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
16065 		case i40e_bus_width_pcie_x2:
16066 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
16067 		case i40e_bus_width_pcie_x1:
16068 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
16069 		default:
16070 			break;
16071 		}
16072 
16073 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16074 			 speed, width);
16075 
16076 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16077 		    hw->bus.speed < i40e_bus_speed_8000) {
16078 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16079 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16080 		}
16081 	}
16082 
16083 	/* get the requested speeds from the fw */
16084 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16085 	if (err)
16086 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
16087 			i40e_stat_str(&pf->hw, err),
16088 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16089 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16090 
16091 	/* set the FEC config due to the board capabilities */
16092 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16093 
16094 	/* get the supported phy types from the fw */
16095 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16096 	if (err)
16097 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
16098 			i40e_stat_str(&pf->hw, err),
16099 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16100 
16101 	/* make sure the MFS hasn't been set lower than the default */
16102 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16103 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16104 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16105 	if (val < MAX_FRAME_SIZE_DEFAULT)
16106 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16107 			 i, val);
16108 
16109 	/* Add a filter to drop all Flow control frames from any VSI from being
16110 	 * transmitted. By doing so we stop a malicious VF from sending out
16111 	 * PAUSE or PFC frames and potentially controlling traffic for other
16112 	 * PF/VF VSIs.
16113 	 * The FW can still send Flow control frames if enabled.
16114 	 */
16115 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16116 						       pf->main_vsi_seid);
16117 
16118 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16119 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16120 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16121 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16122 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16123 	/* print a string summarizing features */
16124 	i40e_print_features(pf);
16125 
16126 	return 0;
16127 
16128 	/* Unwind what we've done if something failed in the setup */
16129 err_vsis:
16130 	set_bit(__I40E_DOWN, pf->state);
16131 	i40e_clear_interrupt_scheme(pf);
16132 	kfree(pf->vsi);
16133 err_switch_setup:
16134 	i40e_reset_interrupt_capability(pf);
16135 	del_timer_sync(&pf->service_timer);
16136 err_mac_addr:
16137 err_configure_lan_hmc:
16138 	(void)i40e_shutdown_lan_hmc(hw);
16139 err_init_lan_hmc:
16140 	kfree(pf->qp_pile);
16141 err_sw_init:
16142 err_adminq_setup:
16143 err_pf_reset:
16144 	iounmap(hw->hw_addr);
16145 err_ioremap:
16146 	kfree(pf);
16147 err_pf_alloc:
16148 	pci_disable_pcie_error_reporting(pdev);
16149 	pci_release_mem_regions(pdev);
16150 err_pci_reg:
16151 err_dma:
16152 	pci_disable_device(pdev);
16153 	return err;
16154 }
16155 
16156 /**
16157  * i40e_remove - Device removal routine
16158  * @pdev: PCI device information struct
16159  *
16160  * i40e_remove is called by the PCI subsystem to alert the driver
16161  * that is should release a PCI device.  This could be caused by a
16162  * Hot-Plug event, or because the driver is going to be removed from
16163  * memory.
16164  **/
16165 static void i40e_remove(struct pci_dev *pdev)
16166 {
16167 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16168 	struct i40e_hw *hw = &pf->hw;
16169 	i40e_status ret_code;
16170 	int i;
16171 
16172 	i40e_dbg_pf_exit(pf);
16173 
16174 	i40e_ptp_stop(pf);
16175 
16176 	/* Disable RSS in hw */
16177 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16178 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16179 
16180 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16181 	 * flags, once they are set, i40e_rebuild should not be called as
16182 	 * i40e_prep_for_reset always returns early.
16183 	 */
16184 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16185 		usleep_range(1000, 2000);
16186 	set_bit(__I40E_IN_REMOVE, pf->state);
16187 
16188 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16189 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16190 		i40e_free_vfs(pf);
16191 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16192 	}
16193 	/* no more scheduling of any task */
16194 	set_bit(__I40E_SUSPENDED, pf->state);
16195 	set_bit(__I40E_DOWN, pf->state);
16196 	if (pf->service_timer.function)
16197 		del_timer_sync(&pf->service_timer);
16198 	if (pf->service_task.func)
16199 		cancel_work_sync(&pf->service_task);
16200 
16201 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16202 		struct i40e_vsi *vsi = pf->vsi[0];
16203 
16204 		/* We know that we have allocated only one vsi for this PF,
16205 		 * it was just for registering netdevice, so the interface
16206 		 * could be visible in the 'ifconfig' output
16207 		 */
16208 		unregister_netdev(vsi->netdev);
16209 		free_netdev(vsi->netdev);
16210 
16211 		goto unmap;
16212 	}
16213 
16214 	/* Client close must be called explicitly here because the timer
16215 	 * has been stopped.
16216 	 */
16217 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16218 
16219 	i40e_fdir_teardown(pf);
16220 
16221 	/* If there is a switch structure or any orphans, remove them.
16222 	 * This will leave only the PF's VSI remaining.
16223 	 */
16224 	for (i = 0; i < I40E_MAX_VEB; i++) {
16225 		if (!pf->veb[i])
16226 			continue;
16227 
16228 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16229 		    pf->veb[i]->uplink_seid == 0)
16230 			i40e_switch_branch_release(pf->veb[i]);
16231 	}
16232 
16233 	/* Now we can shutdown the PF's VSI, just before we kill
16234 	 * adminq and hmc.
16235 	 */
16236 	if (pf->vsi[pf->lan_vsi])
16237 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16238 
16239 	i40e_cloud_filter_exit(pf);
16240 
16241 	/* remove attached clients */
16242 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16243 		ret_code = i40e_lan_del_device(pf);
16244 		if (ret_code)
16245 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16246 				 ret_code);
16247 	}
16248 
16249 	/* shutdown and destroy the HMC */
16250 	if (hw->hmc.hmc_obj) {
16251 		ret_code = i40e_shutdown_lan_hmc(hw);
16252 		if (ret_code)
16253 			dev_warn(&pdev->dev,
16254 				 "Failed to destroy the HMC resources: %d\n",
16255 				 ret_code);
16256 	}
16257 
16258 unmap:
16259 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16260 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16261 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16262 		free_irq(pf->pdev->irq, pf);
16263 
16264 	/* shutdown the adminq */
16265 	i40e_shutdown_adminq(hw);
16266 
16267 	/* destroy the locks only once, here */
16268 	mutex_destroy(&hw->aq.arq_mutex);
16269 	mutex_destroy(&hw->aq.asq_mutex);
16270 
16271 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16272 	rtnl_lock();
16273 	i40e_clear_interrupt_scheme(pf);
16274 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16275 		if (pf->vsi[i]) {
16276 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16277 				i40e_vsi_clear_rings(pf->vsi[i]);
16278 			i40e_vsi_clear(pf->vsi[i]);
16279 			pf->vsi[i] = NULL;
16280 		}
16281 	}
16282 	rtnl_unlock();
16283 
16284 	for (i = 0; i < I40E_MAX_VEB; i++) {
16285 		kfree(pf->veb[i]);
16286 		pf->veb[i] = NULL;
16287 	}
16288 
16289 	kfree(pf->qp_pile);
16290 	kfree(pf->vsi);
16291 
16292 	iounmap(hw->hw_addr);
16293 	kfree(pf);
16294 	pci_release_mem_regions(pdev);
16295 
16296 	pci_disable_pcie_error_reporting(pdev);
16297 	pci_disable_device(pdev);
16298 }
16299 
16300 /**
16301  * i40e_pci_error_detected - warning that something funky happened in PCI land
16302  * @pdev: PCI device information struct
16303  * @error: the type of PCI error
16304  *
16305  * Called to warn that something happened and the error handling steps
16306  * are in progress.  Allows the driver to quiesce things, be ready for
16307  * remediation.
16308  **/
16309 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16310 						pci_channel_state_t error)
16311 {
16312 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16313 
16314 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16315 
16316 	if (!pf) {
16317 		dev_info(&pdev->dev,
16318 			 "Cannot recover - error happened during device probe\n");
16319 		return PCI_ERS_RESULT_DISCONNECT;
16320 	}
16321 
16322 	/* shutdown all operations */
16323 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16324 		i40e_prep_for_reset(pf);
16325 
16326 	/* Request a slot reset */
16327 	return PCI_ERS_RESULT_NEED_RESET;
16328 }
16329 
16330 /**
16331  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16332  * @pdev: PCI device information struct
16333  *
16334  * Called to find if the driver can work with the device now that
16335  * the pci slot has been reset.  If a basic connection seems good
16336  * (registers are readable and have sane content) then return a
16337  * happy little PCI_ERS_RESULT_xxx.
16338  **/
16339 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16340 {
16341 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16342 	pci_ers_result_t result;
16343 	u32 reg;
16344 
16345 	dev_dbg(&pdev->dev, "%s\n", __func__);
16346 	if (pci_enable_device_mem(pdev)) {
16347 		dev_info(&pdev->dev,
16348 			 "Cannot re-enable PCI device after reset.\n");
16349 		result = PCI_ERS_RESULT_DISCONNECT;
16350 	} else {
16351 		pci_set_master(pdev);
16352 		pci_restore_state(pdev);
16353 		pci_save_state(pdev);
16354 		pci_wake_from_d3(pdev, false);
16355 
16356 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16357 		if (reg == 0)
16358 			result = PCI_ERS_RESULT_RECOVERED;
16359 		else
16360 			result = PCI_ERS_RESULT_DISCONNECT;
16361 	}
16362 
16363 	return result;
16364 }
16365 
16366 /**
16367  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16368  * @pdev: PCI device information struct
16369  */
16370 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16371 {
16372 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16373 
16374 	i40e_prep_for_reset(pf);
16375 }
16376 
16377 /**
16378  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16379  * @pdev: PCI device information struct
16380  */
16381 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16382 {
16383 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16384 
16385 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16386 		return;
16387 
16388 	i40e_reset_and_rebuild(pf, false, false);
16389 }
16390 
16391 /**
16392  * i40e_pci_error_resume - restart operations after PCI error recovery
16393  * @pdev: PCI device information struct
16394  *
16395  * Called to allow the driver to bring things back up after PCI error
16396  * and/or reset recovery has finished.
16397  **/
16398 static void i40e_pci_error_resume(struct pci_dev *pdev)
16399 {
16400 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16401 
16402 	dev_dbg(&pdev->dev, "%s\n", __func__);
16403 	if (test_bit(__I40E_SUSPENDED, pf->state))
16404 		return;
16405 
16406 	i40e_handle_reset_warning(pf, false);
16407 }
16408 
16409 /**
16410  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16411  * using the mac_address_write admin q function
16412  * @pf: pointer to i40e_pf struct
16413  **/
16414 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16415 {
16416 	struct i40e_hw *hw = &pf->hw;
16417 	i40e_status ret;
16418 	u8 mac_addr[6];
16419 	u16 flags = 0;
16420 
16421 	/* Get current MAC address in case it's an LAA */
16422 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16423 		ether_addr_copy(mac_addr,
16424 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16425 	} else {
16426 		dev_err(&pf->pdev->dev,
16427 			"Failed to retrieve MAC address; using default\n");
16428 		ether_addr_copy(mac_addr, hw->mac.addr);
16429 	}
16430 
16431 	/* The FW expects the mac address write cmd to first be called with
16432 	 * one of these flags before calling it again with the multicast
16433 	 * enable flags.
16434 	 */
16435 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16436 
16437 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16438 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16439 
16440 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16441 	if (ret) {
16442 		dev_err(&pf->pdev->dev,
16443 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16444 		return;
16445 	}
16446 
16447 	flags = I40E_AQC_MC_MAG_EN
16448 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16449 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16450 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16451 	if (ret)
16452 		dev_err(&pf->pdev->dev,
16453 			"Failed to enable Multicast Magic Packet wake up\n");
16454 }
16455 
16456 /**
16457  * i40e_shutdown - PCI callback for shutting down
16458  * @pdev: PCI device information struct
16459  **/
16460 static void i40e_shutdown(struct pci_dev *pdev)
16461 {
16462 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16463 	struct i40e_hw *hw = &pf->hw;
16464 
16465 	set_bit(__I40E_SUSPENDED, pf->state);
16466 	set_bit(__I40E_DOWN, pf->state);
16467 
16468 	del_timer_sync(&pf->service_timer);
16469 	cancel_work_sync(&pf->service_task);
16470 	i40e_cloud_filter_exit(pf);
16471 	i40e_fdir_teardown(pf);
16472 
16473 	/* Client close must be called explicitly here because the timer
16474 	 * has been stopped.
16475 	 */
16476 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16477 
16478 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16479 		i40e_enable_mc_magic_wake(pf);
16480 
16481 	i40e_prep_for_reset(pf);
16482 
16483 	wr32(hw, I40E_PFPM_APM,
16484 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16485 	wr32(hw, I40E_PFPM_WUFC,
16486 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16487 
16488 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16489 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16490 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16491 		free_irq(pf->pdev->irq, pf);
16492 
16493 	/* Since we're going to destroy queues during the
16494 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16495 	 * whole section
16496 	 */
16497 	rtnl_lock();
16498 	i40e_clear_interrupt_scheme(pf);
16499 	rtnl_unlock();
16500 
16501 	if (system_state == SYSTEM_POWER_OFF) {
16502 		pci_wake_from_d3(pdev, pf->wol_en);
16503 		pci_set_power_state(pdev, PCI_D3hot);
16504 	}
16505 }
16506 
16507 /**
16508  * i40e_suspend - PM callback for moving to D3
16509  * @dev: generic device information structure
16510  **/
16511 static int __maybe_unused i40e_suspend(struct device *dev)
16512 {
16513 	struct i40e_pf *pf = dev_get_drvdata(dev);
16514 	struct i40e_hw *hw = &pf->hw;
16515 
16516 	/* If we're already suspended, then there is nothing to do */
16517 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16518 		return 0;
16519 
16520 	set_bit(__I40E_DOWN, pf->state);
16521 
16522 	/* Ensure service task will not be running */
16523 	del_timer_sync(&pf->service_timer);
16524 	cancel_work_sync(&pf->service_task);
16525 
16526 	/* Client close must be called explicitly here because the timer
16527 	 * has been stopped.
16528 	 */
16529 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16530 
16531 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16532 		i40e_enable_mc_magic_wake(pf);
16533 
16534 	/* Since we're going to destroy queues during the
16535 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16536 	 * whole section
16537 	 */
16538 	rtnl_lock();
16539 
16540 	i40e_prep_for_reset(pf);
16541 
16542 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16543 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16544 
16545 	/* Clear the interrupt scheme and release our IRQs so that the system
16546 	 * can safely hibernate even when there are a large number of CPUs.
16547 	 * Otherwise hibernation might fail when mapping all the vectors back
16548 	 * to CPU0.
16549 	 */
16550 	i40e_clear_interrupt_scheme(pf);
16551 
16552 	rtnl_unlock();
16553 
16554 	return 0;
16555 }
16556 
16557 /**
16558  * i40e_resume - PM callback for waking up from D3
16559  * @dev: generic device information structure
16560  **/
16561 static int __maybe_unused i40e_resume(struct device *dev)
16562 {
16563 	struct i40e_pf *pf = dev_get_drvdata(dev);
16564 	int err;
16565 
16566 	/* If we're not suspended, then there is nothing to do */
16567 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16568 		return 0;
16569 
16570 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16571 	 * since we're going to be restoring queues
16572 	 */
16573 	rtnl_lock();
16574 
16575 	/* We cleared the interrupt scheme when we suspended, so we need to
16576 	 * restore it now to resume device functionality.
16577 	 */
16578 	err = i40e_restore_interrupt_scheme(pf);
16579 	if (err) {
16580 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16581 			err);
16582 	}
16583 
16584 	clear_bit(__I40E_DOWN, pf->state);
16585 	i40e_reset_and_rebuild(pf, false, true);
16586 
16587 	rtnl_unlock();
16588 
16589 	/* Clear suspended state last after everything is recovered */
16590 	clear_bit(__I40E_SUSPENDED, pf->state);
16591 
16592 	/* Restart the service task */
16593 	mod_timer(&pf->service_timer,
16594 		  round_jiffies(jiffies + pf->service_timer_period));
16595 
16596 	return 0;
16597 }
16598 
16599 static const struct pci_error_handlers i40e_err_handler = {
16600 	.error_detected = i40e_pci_error_detected,
16601 	.slot_reset = i40e_pci_error_slot_reset,
16602 	.reset_prepare = i40e_pci_error_reset_prepare,
16603 	.reset_done = i40e_pci_error_reset_done,
16604 	.resume = i40e_pci_error_resume,
16605 };
16606 
16607 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16608 
16609 static struct pci_driver i40e_driver = {
16610 	.name     = i40e_driver_name,
16611 	.id_table = i40e_pci_tbl,
16612 	.probe    = i40e_probe,
16613 	.remove   = i40e_remove,
16614 	.driver   = {
16615 		.pm = &i40e_pm_ops,
16616 	},
16617 	.shutdown = i40e_shutdown,
16618 	.err_handler = &i40e_err_handler,
16619 	.sriov_configure = i40e_pci_sriov_configure,
16620 };
16621 
16622 /**
16623  * i40e_init_module - Driver registration routine
16624  *
16625  * i40e_init_module is the first routine called when the driver is
16626  * loaded. All it does is register with the PCI subsystem.
16627  **/
16628 static int __init i40e_init_module(void)
16629 {
16630 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16631 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16632 
16633 	/* There is no need to throttle the number of active tasks because
16634 	 * each device limits its own task using a state bit for scheduling
16635 	 * the service task, and the device tasks do not interfere with each
16636 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16637 	 * since we need to be able to guarantee forward progress even under
16638 	 * memory pressure.
16639 	 */
16640 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16641 	if (!i40e_wq) {
16642 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16643 		return -ENOMEM;
16644 	}
16645 
16646 	i40e_dbg_init();
16647 	return pci_register_driver(&i40e_driver);
16648 }
16649 module_init(i40e_init_module);
16650 
16651 /**
16652  * i40e_exit_module - Driver exit cleanup routine
16653  *
16654  * i40e_exit_module is called just before the driver is removed
16655  * from memory.
16656  **/
16657 static void __exit i40e_exit_module(void)
16658 {
16659 	pci_unregister_driver(&i40e_driver);
16660 	destroy_workqueue(i40e_wq);
16661 	ida_destroy(&i40e_client_ida);
16662 	i40e_dbg_exit();
16663 }
16664 module_exit(i40e_exit_module);
16665