1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
87 	/* required last entry */
88 	{0, }
89 };
90 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
91 
92 #define I40E_MAX_VF_COUNT 128
93 static int debug = -1;
94 module_param(debug, uint, 0);
95 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
96 
97 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
98 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
99 MODULE_LICENSE("GPL v2");
100 
101 static struct workqueue_struct *i40e_wq;
102 
103 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
104 				  struct net_device *netdev, int delta)
105 {
106 	struct netdev_hw_addr *ha;
107 
108 	if (!f || !netdev)
109 		return;
110 
111 	netdev_for_each_mc_addr(ha, netdev) {
112 		if (ether_addr_equal(ha->addr, f->macaddr)) {
113 			ha->refcount += delta;
114 			if (ha->refcount <= 0)
115 				ha->refcount = 1;
116 			break;
117 		}
118 	}
119 }
120 
121 /**
122  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
123  * @hw:   pointer to the HW structure
124  * @mem:  ptr to mem struct to fill out
125  * @size: size of memory requested
126  * @alignment: what to align the allocation to
127  **/
128 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
129 			    u64 size, u32 alignment)
130 {
131 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
132 
133 	mem->size = ALIGN(size, alignment);
134 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
135 				     GFP_KERNEL);
136 	if (!mem->va)
137 		return -ENOMEM;
138 
139 	return 0;
140 }
141 
142 /**
143  * i40e_free_dma_mem_d - OS specific memory free for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to free
146  **/
147 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
148 {
149 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
150 
151 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
152 	mem->va = NULL;
153 	mem->pa = 0;
154 	mem->size = 0;
155 
156 	return 0;
157 }
158 
159 /**
160  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
161  * @hw:   pointer to the HW structure
162  * @mem:  ptr to mem struct to fill out
163  * @size: size of memory requested
164  **/
165 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
166 			     u32 size)
167 {
168 	mem->size = size;
169 	mem->va = kzalloc(size, GFP_KERNEL);
170 
171 	if (!mem->va)
172 		return -ENOMEM;
173 
174 	return 0;
175 }
176 
177 /**
178  * i40e_free_virt_mem_d - OS specific memory free for shared code
179  * @hw:   pointer to the HW structure
180  * @mem:  ptr to mem struct to free
181  **/
182 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
183 {
184 	/* it's ok to kfree a NULL pointer */
185 	kfree(mem->va);
186 	mem->va = NULL;
187 	mem->size = 0;
188 
189 	return 0;
190 }
191 
192 /**
193  * i40e_get_lump - find a lump of free generic resource
194  * @pf: board private structure
195  * @pile: the pile of resource to search
196  * @needed: the number of items needed
197  * @id: an owner id to stick on the items assigned
198  *
199  * Returns the base item index of the lump, or negative for error
200  **/
201 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
202 			 u16 needed, u16 id)
203 {
204 	int ret = -ENOMEM;
205 	int i, j;
206 
207 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
208 		dev_info(&pf->pdev->dev,
209 			 "param err: pile=%s needed=%d id=0x%04x\n",
210 			 pile ? "<valid>" : "<null>", needed, id);
211 		return -EINVAL;
212 	}
213 
214 	/* Allocate last queue in the pile for FDIR VSI queue
215 	 * so it doesn't fragment the qp_pile
216 	 */
217 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
218 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
219 			dev_err(&pf->pdev->dev,
220 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
221 				pile->num_entries - 1);
222 			return -ENOMEM;
223 		}
224 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
225 		return pile->num_entries - 1;
226 	}
227 
228 	i = 0;
229 	while (i < pile->num_entries) {
230 		/* skip already allocated entries */
231 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
232 			i++;
233 			continue;
234 		}
235 
236 		/* do we have enough in this lump? */
237 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
238 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
239 				break;
240 		}
241 
242 		if (j == needed) {
243 			/* there was enough, so assign it to the requestor */
244 			for (j = 0; j < needed; j++)
245 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
246 			ret = i;
247 			break;
248 		}
249 
250 		/* not enough, so skip over it and continue looking */
251 		i += j;
252 	}
253 
254 	return ret;
255 }
256 
257 /**
258  * i40e_put_lump - return a lump of generic resource
259  * @pile: the pile of resource to search
260  * @index: the base item index
261  * @id: the owner id of the items assigned
262  *
263  * Returns the count of items in the lump
264  **/
265 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
266 {
267 	int valid_id = (id | I40E_PILE_VALID_BIT);
268 	int count = 0;
269 	u16 i;
270 
271 	if (!pile || index >= pile->num_entries)
272 		return -EINVAL;
273 
274 	for (i = index;
275 	     i < pile->num_entries && pile->list[i] == valid_id;
276 	     i++) {
277 		pile->list[i] = 0;
278 		count++;
279 	}
280 
281 
282 	return count;
283 }
284 
285 /**
286  * i40e_find_vsi_from_id - searches for the vsi with the given id
287  * @pf: the pf structure to search for the vsi
288  * @id: id of the vsi it is searching for
289  **/
290 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
291 {
292 	int i;
293 
294 	for (i = 0; i < pf->num_alloc_vsi; i++)
295 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
296 			return pf->vsi[i];
297 
298 	return NULL;
299 }
300 
301 /**
302  * i40e_service_event_schedule - Schedule the service task to wake up
303  * @pf: board private structure
304  *
305  * If not already scheduled, this puts the task into the work queue
306  **/
307 void i40e_service_event_schedule(struct i40e_pf *pf)
308 {
309 	if ((!test_bit(__I40E_DOWN, pf->state) &&
310 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
311 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
312 		queue_work(i40e_wq, &pf->service_task);
313 }
314 
315 /**
316  * i40e_tx_timeout - Respond to a Tx Hang
317  * @netdev: network interface device structure
318  * @txqueue: queue number timing out
319  *
320  * If any port has noticed a Tx timeout, it is likely that the whole
321  * device is munged, not just the one netdev port, so go for the full
322  * reset.
323  **/
324 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
325 {
326 	struct i40e_netdev_priv *np = netdev_priv(netdev);
327 	struct i40e_vsi *vsi = np->vsi;
328 	struct i40e_pf *pf = vsi->back;
329 	struct i40e_ring *tx_ring = NULL;
330 	unsigned int i;
331 	u32 head, val;
332 
333 	pf->tx_timeout_count++;
334 
335 	/* with txqueue index, find the tx_ring struct */
336 	for (i = 0; i < vsi->num_queue_pairs; i++) {
337 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
338 			if (txqueue ==
339 			    vsi->tx_rings[i]->queue_index) {
340 				tx_ring = vsi->tx_rings[i];
341 				break;
342 			}
343 		}
344 	}
345 
346 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
347 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
348 	else if (time_before(jiffies,
349 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
350 		return;   /* don't do any new action before the next timeout */
351 
352 	/* don't kick off another recovery if one is already pending */
353 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
354 		return;
355 
356 	if (tx_ring) {
357 		head = i40e_get_head(tx_ring);
358 		/* Read interrupt register */
359 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
360 			val = rd32(&pf->hw,
361 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
362 						tx_ring->vsi->base_vector - 1));
363 		else
364 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
365 
366 		netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
367 			    vsi->seid, txqueue, tx_ring->next_to_clean,
368 			    head, tx_ring->next_to_use,
369 			    readl(tx_ring->tail), val);
370 	}
371 
372 	pf->tx_timeout_last_recovery = jiffies;
373 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
374 		    pf->tx_timeout_recovery_level, txqueue);
375 
376 	switch (pf->tx_timeout_recovery_level) {
377 	case 1:
378 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
379 		break;
380 	case 2:
381 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
382 		break;
383 	case 3:
384 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
385 		break;
386 	default:
387 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
388 		break;
389 	}
390 
391 	i40e_service_event_schedule(pf);
392 	pf->tx_timeout_recovery_level++;
393 }
394 
395 /**
396  * i40e_get_vsi_stats_struct - Get System Network Statistics
397  * @vsi: the VSI we care about
398  *
399  * Returns the address of the device statistics structure.
400  * The statistics are actually updated from the service task.
401  **/
402 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
403 {
404 	return &vsi->net_stats;
405 }
406 
407 /**
408  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
409  * @ring: Tx ring to get statistics from
410  * @stats: statistics entry to be updated
411  **/
412 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
413 					    struct rtnl_link_stats64 *stats)
414 {
415 	u64 bytes, packets;
416 	unsigned int start;
417 
418 	do {
419 		start = u64_stats_fetch_begin_irq(&ring->syncp);
420 		packets = ring->stats.packets;
421 		bytes   = ring->stats.bytes;
422 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
423 
424 	stats->tx_packets += packets;
425 	stats->tx_bytes   += bytes;
426 }
427 
428 /**
429  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
430  * @netdev: network interface device structure
431  * @stats: data structure to store statistics
432  *
433  * Returns the address of the device statistics structure.
434  * The statistics are actually updated from the service task.
435  **/
436 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
437 				  struct rtnl_link_stats64 *stats)
438 {
439 	struct i40e_netdev_priv *np = netdev_priv(netdev);
440 	struct i40e_vsi *vsi = np->vsi;
441 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
442 	struct i40e_ring *ring;
443 	int i;
444 
445 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
446 		return;
447 
448 	if (!vsi->tx_rings)
449 		return;
450 
451 	rcu_read_lock();
452 	for (i = 0; i < vsi->num_queue_pairs; i++) {
453 		u64 bytes, packets;
454 		unsigned int start;
455 
456 		ring = READ_ONCE(vsi->tx_rings[i]);
457 		if (!ring)
458 			continue;
459 		i40e_get_netdev_stats_struct_tx(ring, stats);
460 
461 		if (i40e_enabled_xdp_vsi(vsi)) {
462 			ring = READ_ONCE(vsi->xdp_rings[i]);
463 			if (!ring)
464 				continue;
465 			i40e_get_netdev_stats_struct_tx(ring, stats);
466 		}
467 
468 		ring = READ_ONCE(vsi->rx_rings[i]);
469 		if (!ring)
470 			continue;
471 		do {
472 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
473 			packets = ring->stats.packets;
474 			bytes   = ring->stats.bytes;
475 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
476 
477 		stats->rx_packets += packets;
478 		stats->rx_bytes   += bytes;
479 
480 	}
481 	rcu_read_unlock();
482 
483 	/* following stats updated by i40e_watchdog_subtask() */
484 	stats->multicast	= vsi_stats->multicast;
485 	stats->tx_errors	= vsi_stats->tx_errors;
486 	stats->tx_dropped	= vsi_stats->tx_dropped;
487 	stats->rx_errors	= vsi_stats->rx_errors;
488 	stats->rx_dropped	= vsi_stats->rx_dropped;
489 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
490 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
491 }
492 
493 /**
494  * i40e_vsi_reset_stats - Resets all stats of the given vsi
495  * @vsi: the VSI to have its stats reset
496  **/
497 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
498 {
499 	struct rtnl_link_stats64 *ns;
500 	int i;
501 
502 	if (!vsi)
503 		return;
504 
505 	ns = i40e_get_vsi_stats_struct(vsi);
506 	memset(ns, 0, sizeof(*ns));
507 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
508 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
509 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
510 	if (vsi->rx_rings && vsi->rx_rings[0]) {
511 		for (i = 0; i < vsi->num_queue_pairs; i++) {
512 			memset(&vsi->rx_rings[i]->stats, 0,
513 			       sizeof(vsi->rx_rings[i]->stats));
514 			memset(&vsi->rx_rings[i]->rx_stats, 0,
515 			       sizeof(vsi->rx_rings[i]->rx_stats));
516 			memset(&vsi->tx_rings[i]->stats, 0,
517 			       sizeof(vsi->tx_rings[i]->stats));
518 			memset(&vsi->tx_rings[i]->tx_stats, 0,
519 			       sizeof(vsi->tx_rings[i]->tx_stats));
520 		}
521 	}
522 	vsi->stat_offsets_loaded = false;
523 }
524 
525 /**
526  * i40e_pf_reset_stats - Reset all of the stats for the given PF
527  * @pf: the PF to be reset
528  **/
529 void i40e_pf_reset_stats(struct i40e_pf *pf)
530 {
531 	int i;
532 
533 	memset(&pf->stats, 0, sizeof(pf->stats));
534 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
535 	pf->stat_offsets_loaded = false;
536 
537 	for (i = 0; i < I40E_MAX_VEB; i++) {
538 		if (pf->veb[i]) {
539 			memset(&pf->veb[i]->stats, 0,
540 			       sizeof(pf->veb[i]->stats));
541 			memset(&pf->veb[i]->stats_offsets, 0,
542 			       sizeof(pf->veb[i]->stats_offsets));
543 			memset(&pf->veb[i]->tc_stats, 0,
544 			       sizeof(pf->veb[i]->tc_stats));
545 			memset(&pf->veb[i]->tc_stats_offsets, 0,
546 			       sizeof(pf->veb[i]->tc_stats_offsets));
547 			pf->veb[i]->stat_offsets_loaded = false;
548 		}
549 	}
550 	pf->hw_csum_rx_error = 0;
551 }
552 
553 /**
554  * i40e_compute_pci_to_hw_id - compute index form PCI function.
555  * @vsi: ptr to the VSI to read from.
556  * @hw: ptr to the hardware info.
557  **/
558 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
559 {
560 	int pf_count = i40e_get_pf_count(hw);
561 
562 	if (vsi->type == I40E_VSI_SRIOV)
563 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
564 
565 	return hw->port + BIT(7);
566 }
567 
568 /**
569  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
570  * @hw: ptr to the hardware info.
571  * @hireg: the high 32 bit reg to read.
572  * @loreg: the low 32 bit reg to read.
573  * @offset_loaded: has the initial offset been loaded yet.
574  * @offset: ptr to current offset value.
575  * @stat: ptr to the stat.
576  *
577  * Since the device stats are not reset at PFReset, they will not
578  * be zeroed when the driver starts.  We'll save the first values read
579  * and use them as offsets to be subtracted from the raw values in order
580  * to report stats that count from zero.
581  **/
582 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
583 			       bool offset_loaded, u64 *offset, u64 *stat)
584 {
585 	u64 new_data;
586 
587 	new_data = rd64(hw, loreg);
588 
589 	if (!offset_loaded || new_data < *offset)
590 		*offset = new_data;
591 	*stat = new_data - *offset;
592 }
593 
594 /**
595  * i40e_stat_update48 - read and update a 48 bit stat from the chip
596  * @hw: ptr to the hardware info
597  * @hireg: the high 32 bit reg to read
598  * @loreg: the low 32 bit reg to read
599  * @offset_loaded: has the initial offset been loaded yet
600  * @offset: ptr to current offset value
601  * @stat: ptr to the stat
602  *
603  * Since the device stats are not reset at PFReset, they likely will not
604  * be zeroed when the driver starts.  We'll save the first values read
605  * and use them as offsets to be subtracted from the raw values in order
606  * to report stats that count from zero.  In the process, we also manage
607  * the potential roll-over.
608  **/
609 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
610 			       bool offset_loaded, u64 *offset, u64 *stat)
611 {
612 	u64 new_data;
613 
614 	if (hw->device_id == I40E_DEV_ID_QEMU) {
615 		new_data = rd32(hw, loreg);
616 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
617 	} else {
618 		new_data = rd64(hw, loreg);
619 	}
620 	if (!offset_loaded)
621 		*offset = new_data;
622 	if (likely(new_data >= *offset))
623 		*stat = new_data - *offset;
624 	else
625 		*stat = (new_data + BIT_ULL(48)) - *offset;
626 	*stat &= 0xFFFFFFFFFFFFULL;
627 }
628 
629 /**
630  * i40e_stat_update32 - read and update a 32 bit stat from the chip
631  * @hw: ptr to the hardware info
632  * @reg: the hw reg to read
633  * @offset_loaded: has the initial offset been loaded yet
634  * @offset: ptr to current offset value
635  * @stat: ptr to the stat
636  **/
637 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
638 			       bool offset_loaded, u64 *offset, u64 *stat)
639 {
640 	u32 new_data;
641 
642 	new_data = rd32(hw, reg);
643 	if (!offset_loaded)
644 		*offset = new_data;
645 	if (likely(new_data >= *offset))
646 		*stat = (u32)(new_data - *offset);
647 	else
648 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
649 }
650 
651 /**
652  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
653  * @hw: ptr to the hardware info
654  * @reg: the hw reg to read and clear
655  * @stat: ptr to the stat
656  **/
657 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
658 {
659 	u32 new_data = rd32(hw, reg);
660 
661 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
662 	*stat += new_data;
663 }
664 
665 /**
666  * i40e_stats_update_rx_discards - update rx_discards.
667  * @vsi: ptr to the VSI to be updated.
668  * @hw: ptr to the hardware info.
669  * @stat_idx: VSI's stat_counter_idx.
670  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
671  * @stat_offset: ptr to stat_offset to store first read of specific register.
672  * @stat: ptr to VSI's stat to be updated.
673  **/
674 static void
675 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
676 			      int stat_idx, bool offset_loaded,
677 			      struct i40e_eth_stats *stat_offset,
678 			      struct i40e_eth_stats *stat)
679 {
680 	u64 rx_rdpc, rx_rxerr;
681 
682 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
683 			   &stat_offset->rx_discards, &rx_rdpc);
684 	i40e_stat_update64(hw,
685 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
686 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
687 			   offset_loaded, &stat_offset->rx_discards_other,
688 			   &rx_rxerr);
689 
690 	stat->rx_discards = rx_rdpc + rx_rxerr;
691 }
692 
693 /**
694  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
695  * @vsi: the VSI to be updated
696  **/
697 void i40e_update_eth_stats(struct i40e_vsi *vsi)
698 {
699 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
700 	struct i40e_pf *pf = vsi->back;
701 	struct i40e_hw *hw = &pf->hw;
702 	struct i40e_eth_stats *oes;
703 	struct i40e_eth_stats *es;     /* device's eth stats */
704 
705 	es = &vsi->eth_stats;
706 	oes = &vsi->eth_stats_offsets;
707 
708 	/* Gather up the stats that the hw collects */
709 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
710 			   vsi->stat_offsets_loaded,
711 			   &oes->tx_errors, &es->tx_errors);
712 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
713 			   vsi->stat_offsets_loaded,
714 			   &oes->rx_discards, &es->rx_discards);
715 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
716 			   vsi->stat_offsets_loaded,
717 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
718 
719 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
720 			   I40E_GLV_GORCL(stat_idx),
721 			   vsi->stat_offsets_loaded,
722 			   &oes->rx_bytes, &es->rx_bytes);
723 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
724 			   I40E_GLV_UPRCL(stat_idx),
725 			   vsi->stat_offsets_loaded,
726 			   &oes->rx_unicast, &es->rx_unicast);
727 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
728 			   I40E_GLV_MPRCL(stat_idx),
729 			   vsi->stat_offsets_loaded,
730 			   &oes->rx_multicast, &es->rx_multicast);
731 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
732 			   I40E_GLV_BPRCL(stat_idx),
733 			   vsi->stat_offsets_loaded,
734 			   &oes->rx_broadcast, &es->rx_broadcast);
735 
736 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
737 			   I40E_GLV_GOTCL(stat_idx),
738 			   vsi->stat_offsets_loaded,
739 			   &oes->tx_bytes, &es->tx_bytes);
740 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
741 			   I40E_GLV_UPTCL(stat_idx),
742 			   vsi->stat_offsets_loaded,
743 			   &oes->tx_unicast, &es->tx_unicast);
744 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
745 			   I40E_GLV_MPTCL(stat_idx),
746 			   vsi->stat_offsets_loaded,
747 			   &oes->tx_multicast, &es->tx_multicast);
748 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
749 			   I40E_GLV_BPTCL(stat_idx),
750 			   vsi->stat_offsets_loaded,
751 			   &oes->tx_broadcast, &es->tx_broadcast);
752 
753 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
754 				      vsi->stat_offsets_loaded, oes, es);
755 
756 	vsi->stat_offsets_loaded = true;
757 }
758 
759 /**
760  * i40e_update_veb_stats - Update Switch component statistics
761  * @veb: the VEB being updated
762  **/
763 void i40e_update_veb_stats(struct i40e_veb *veb)
764 {
765 	struct i40e_pf *pf = veb->pf;
766 	struct i40e_hw *hw = &pf->hw;
767 	struct i40e_eth_stats *oes;
768 	struct i40e_eth_stats *es;     /* device's eth stats */
769 	struct i40e_veb_tc_stats *veb_oes;
770 	struct i40e_veb_tc_stats *veb_es;
771 	int i, idx = 0;
772 
773 	idx = veb->stats_idx;
774 	es = &veb->stats;
775 	oes = &veb->stats_offsets;
776 	veb_es = &veb->tc_stats;
777 	veb_oes = &veb->tc_stats_offsets;
778 
779 	/* Gather up the stats that the hw collects */
780 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
781 			   veb->stat_offsets_loaded,
782 			   &oes->tx_discards, &es->tx_discards);
783 	if (hw->revision_id > 0)
784 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
785 				   veb->stat_offsets_loaded,
786 				   &oes->rx_unknown_protocol,
787 				   &es->rx_unknown_protocol);
788 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
789 			   veb->stat_offsets_loaded,
790 			   &oes->rx_bytes, &es->rx_bytes);
791 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
792 			   veb->stat_offsets_loaded,
793 			   &oes->rx_unicast, &es->rx_unicast);
794 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
795 			   veb->stat_offsets_loaded,
796 			   &oes->rx_multicast, &es->rx_multicast);
797 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->rx_broadcast, &es->rx_broadcast);
800 
801 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
802 			   veb->stat_offsets_loaded,
803 			   &oes->tx_bytes, &es->tx_bytes);
804 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
805 			   veb->stat_offsets_loaded,
806 			   &oes->tx_unicast, &es->tx_unicast);
807 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
808 			   veb->stat_offsets_loaded,
809 			   &oes->tx_multicast, &es->tx_multicast);
810 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
811 			   veb->stat_offsets_loaded,
812 			   &oes->tx_broadcast, &es->tx_broadcast);
813 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
814 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
815 				   I40E_GLVEBTC_RPCL(i, idx),
816 				   veb->stat_offsets_loaded,
817 				   &veb_oes->tc_rx_packets[i],
818 				   &veb_es->tc_rx_packets[i]);
819 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
820 				   I40E_GLVEBTC_RBCL(i, idx),
821 				   veb->stat_offsets_loaded,
822 				   &veb_oes->tc_rx_bytes[i],
823 				   &veb_es->tc_rx_bytes[i]);
824 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
825 				   I40E_GLVEBTC_TPCL(i, idx),
826 				   veb->stat_offsets_loaded,
827 				   &veb_oes->tc_tx_packets[i],
828 				   &veb_es->tc_tx_packets[i]);
829 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
830 				   I40E_GLVEBTC_TBCL(i, idx),
831 				   veb->stat_offsets_loaded,
832 				   &veb_oes->tc_tx_bytes[i],
833 				   &veb_es->tc_tx_bytes[i]);
834 	}
835 	veb->stat_offsets_loaded = true;
836 }
837 
838 /**
839  * i40e_update_vsi_stats - Update the vsi statistics counters.
840  * @vsi: the VSI to be updated
841  *
842  * There are a few instances where we store the same stat in a
843  * couple of different structs.  This is partly because we have
844  * the netdev stats that need to be filled out, which is slightly
845  * different from the "eth_stats" defined by the chip and used in
846  * VF communications.  We sort it out here.
847  **/
848 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
849 {
850 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
851 	struct i40e_pf *pf = vsi->back;
852 	struct rtnl_link_stats64 *ons;
853 	struct rtnl_link_stats64 *ns;   /* netdev stats */
854 	struct i40e_eth_stats *oes;
855 	struct i40e_eth_stats *es;     /* device's eth stats */
856 	u64 tx_restart, tx_busy;
857 	struct i40e_ring *p;
858 	u64 bytes, packets;
859 	unsigned int start;
860 	u64 tx_linearize;
861 	u64 tx_force_wb;
862 	u64 tx_stopped;
863 	u64 rx_p, rx_b;
864 	u64 tx_p, tx_b;
865 	u16 q;
866 
867 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
868 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
869 		return;
870 
871 	ns = i40e_get_vsi_stats_struct(vsi);
872 	ons = &vsi->net_stats_offsets;
873 	es = &vsi->eth_stats;
874 	oes = &vsi->eth_stats_offsets;
875 
876 	/* Gather up the netdev and vsi stats that the driver collects
877 	 * on the fly during packet processing
878 	 */
879 	rx_b = rx_p = 0;
880 	tx_b = tx_p = 0;
881 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
882 	tx_stopped = 0;
883 	rx_page = 0;
884 	rx_buf = 0;
885 	rx_reuse = 0;
886 	rx_alloc = 0;
887 	rx_waive = 0;
888 	rx_busy = 0;
889 	rcu_read_lock();
890 	for (q = 0; q < vsi->num_queue_pairs; q++) {
891 		/* locate Tx ring */
892 		p = READ_ONCE(vsi->tx_rings[q]);
893 		if (!p)
894 			continue;
895 
896 		do {
897 			start = u64_stats_fetch_begin_irq(&p->syncp);
898 			packets = p->stats.packets;
899 			bytes = p->stats.bytes;
900 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
901 		tx_b += bytes;
902 		tx_p += packets;
903 		tx_restart += p->tx_stats.restart_queue;
904 		tx_busy += p->tx_stats.tx_busy;
905 		tx_linearize += p->tx_stats.tx_linearize;
906 		tx_force_wb += p->tx_stats.tx_force_wb;
907 		tx_stopped += p->tx_stats.tx_stopped;
908 
909 		/* locate Rx ring */
910 		p = READ_ONCE(vsi->rx_rings[q]);
911 		if (!p)
912 			continue;
913 
914 		do {
915 			start = u64_stats_fetch_begin_irq(&p->syncp);
916 			packets = p->stats.packets;
917 			bytes = p->stats.bytes;
918 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
919 		rx_b += bytes;
920 		rx_p += packets;
921 		rx_buf += p->rx_stats.alloc_buff_failed;
922 		rx_page += p->rx_stats.alloc_page_failed;
923 		rx_reuse += p->rx_stats.page_reuse_count;
924 		rx_alloc += p->rx_stats.page_alloc_count;
925 		rx_waive += p->rx_stats.page_waive_count;
926 		rx_busy += p->rx_stats.page_busy_count;
927 
928 		if (i40e_enabled_xdp_vsi(vsi)) {
929 			/* locate XDP ring */
930 			p = READ_ONCE(vsi->xdp_rings[q]);
931 			if (!p)
932 				continue;
933 
934 			do {
935 				start = u64_stats_fetch_begin_irq(&p->syncp);
936 				packets = p->stats.packets;
937 				bytes = p->stats.bytes;
938 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
939 			tx_b += bytes;
940 			tx_p += packets;
941 			tx_restart += p->tx_stats.restart_queue;
942 			tx_busy += p->tx_stats.tx_busy;
943 			tx_linearize += p->tx_stats.tx_linearize;
944 			tx_force_wb += p->tx_stats.tx_force_wb;
945 		}
946 	}
947 	rcu_read_unlock();
948 	vsi->tx_restart = tx_restart;
949 	vsi->tx_busy = tx_busy;
950 	vsi->tx_linearize = tx_linearize;
951 	vsi->tx_force_wb = tx_force_wb;
952 	vsi->tx_stopped = tx_stopped;
953 	vsi->rx_page_failed = rx_page;
954 	vsi->rx_buf_failed = rx_buf;
955 	vsi->rx_page_reuse = rx_reuse;
956 	vsi->rx_page_alloc = rx_alloc;
957 	vsi->rx_page_waive = rx_waive;
958 	vsi->rx_page_busy = rx_busy;
959 
960 	ns->rx_packets = rx_p;
961 	ns->rx_bytes = rx_b;
962 	ns->tx_packets = tx_p;
963 	ns->tx_bytes = tx_b;
964 
965 	/* update netdev stats from eth stats */
966 	i40e_update_eth_stats(vsi);
967 	ons->tx_errors = oes->tx_errors;
968 	ns->tx_errors = es->tx_errors;
969 	ons->multicast = oes->rx_multicast;
970 	ns->multicast = es->rx_multicast;
971 	ons->rx_dropped = oes->rx_discards;
972 	ns->rx_dropped = es->rx_discards;
973 	ons->tx_dropped = oes->tx_discards;
974 	ns->tx_dropped = es->tx_discards;
975 
976 	/* pull in a couple PF stats if this is the main vsi */
977 	if (vsi == pf->vsi[pf->lan_vsi]) {
978 		ns->rx_crc_errors = pf->stats.crc_errors;
979 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
980 		ns->rx_length_errors = pf->stats.rx_length_errors;
981 	}
982 }
983 
984 /**
985  * i40e_update_pf_stats - Update the PF statistics counters.
986  * @pf: the PF to be updated
987  **/
988 static void i40e_update_pf_stats(struct i40e_pf *pf)
989 {
990 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
991 	struct i40e_hw_port_stats *nsd = &pf->stats;
992 	struct i40e_hw *hw = &pf->hw;
993 	u32 val;
994 	int i;
995 
996 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
997 			   I40E_GLPRT_GORCL(hw->port),
998 			   pf->stat_offsets_loaded,
999 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1000 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1001 			   I40E_GLPRT_GOTCL(hw->port),
1002 			   pf->stat_offsets_loaded,
1003 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1004 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1005 			   pf->stat_offsets_loaded,
1006 			   &osd->eth.rx_discards,
1007 			   &nsd->eth.rx_discards);
1008 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1009 			   I40E_GLPRT_UPRCL(hw->port),
1010 			   pf->stat_offsets_loaded,
1011 			   &osd->eth.rx_unicast,
1012 			   &nsd->eth.rx_unicast);
1013 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1014 			   I40E_GLPRT_MPRCL(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->eth.rx_multicast,
1017 			   &nsd->eth.rx_multicast);
1018 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1019 			   I40E_GLPRT_BPRCL(hw->port),
1020 			   pf->stat_offsets_loaded,
1021 			   &osd->eth.rx_broadcast,
1022 			   &nsd->eth.rx_broadcast);
1023 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1024 			   I40E_GLPRT_UPTCL(hw->port),
1025 			   pf->stat_offsets_loaded,
1026 			   &osd->eth.tx_unicast,
1027 			   &nsd->eth.tx_unicast);
1028 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1029 			   I40E_GLPRT_MPTCL(hw->port),
1030 			   pf->stat_offsets_loaded,
1031 			   &osd->eth.tx_multicast,
1032 			   &nsd->eth.tx_multicast);
1033 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1034 			   I40E_GLPRT_BPTCL(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->eth.tx_broadcast,
1037 			   &nsd->eth.tx_broadcast);
1038 
1039 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1040 			   pf->stat_offsets_loaded,
1041 			   &osd->tx_dropped_link_down,
1042 			   &nsd->tx_dropped_link_down);
1043 
1044 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1045 			   pf->stat_offsets_loaded,
1046 			   &osd->crc_errors, &nsd->crc_errors);
1047 
1048 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1049 			   pf->stat_offsets_loaded,
1050 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1051 
1052 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1053 			   pf->stat_offsets_loaded,
1054 			   &osd->mac_local_faults,
1055 			   &nsd->mac_local_faults);
1056 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1057 			   pf->stat_offsets_loaded,
1058 			   &osd->mac_remote_faults,
1059 			   &nsd->mac_remote_faults);
1060 
1061 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1062 			   pf->stat_offsets_loaded,
1063 			   &osd->rx_length_errors,
1064 			   &nsd->rx_length_errors);
1065 
1066 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1067 			   pf->stat_offsets_loaded,
1068 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1069 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1072 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1075 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1078 
1079 	for (i = 0; i < 8; i++) {
1080 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1081 				   pf->stat_offsets_loaded,
1082 				   &osd->priority_xoff_rx[i],
1083 				   &nsd->priority_xoff_rx[i]);
1084 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1085 				   pf->stat_offsets_loaded,
1086 				   &osd->priority_xon_rx[i],
1087 				   &nsd->priority_xon_rx[i]);
1088 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1089 				   pf->stat_offsets_loaded,
1090 				   &osd->priority_xon_tx[i],
1091 				   &nsd->priority_xon_tx[i]);
1092 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1093 				   pf->stat_offsets_loaded,
1094 				   &osd->priority_xoff_tx[i],
1095 				   &nsd->priority_xoff_tx[i]);
1096 		i40e_stat_update32(hw,
1097 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1098 				   pf->stat_offsets_loaded,
1099 				   &osd->priority_xon_2_xoff[i],
1100 				   &nsd->priority_xon_2_xoff[i]);
1101 	}
1102 
1103 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1104 			   I40E_GLPRT_PRC64L(hw->port),
1105 			   pf->stat_offsets_loaded,
1106 			   &osd->rx_size_64, &nsd->rx_size_64);
1107 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1108 			   I40E_GLPRT_PRC127L(hw->port),
1109 			   pf->stat_offsets_loaded,
1110 			   &osd->rx_size_127, &nsd->rx_size_127);
1111 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1112 			   I40E_GLPRT_PRC255L(hw->port),
1113 			   pf->stat_offsets_loaded,
1114 			   &osd->rx_size_255, &nsd->rx_size_255);
1115 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1116 			   I40E_GLPRT_PRC511L(hw->port),
1117 			   pf->stat_offsets_loaded,
1118 			   &osd->rx_size_511, &nsd->rx_size_511);
1119 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1120 			   I40E_GLPRT_PRC1023L(hw->port),
1121 			   pf->stat_offsets_loaded,
1122 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1123 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1124 			   I40E_GLPRT_PRC1522L(hw->port),
1125 			   pf->stat_offsets_loaded,
1126 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1127 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1128 			   I40E_GLPRT_PRC9522L(hw->port),
1129 			   pf->stat_offsets_loaded,
1130 			   &osd->rx_size_big, &nsd->rx_size_big);
1131 
1132 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1133 			   I40E_GLPRT_PTC64L(hw->port),
1134 			   pf->stat_offsets_loaded,
1135 			   &osd->tx_size_64, &nsd->tx_size_64);
1136 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1137 			   I40E_GLPRT_PTC127L(hw->port),
1138 			   pf->stat_offsets_loaded,
1139 			   &osd->tx_size_127, &nsd->tx_size_127);
1140 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1141 			   I40E_GLPRT_PTC255L(hw->port),
1142 			   pf->stat_offsets_loaded,
1143 			   &osd->tx_size_255, &nsd->tx_size_255);
1144 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1145 			   I40E_GLPRT_PTC511L(hw->port),
1146 			   pf->stat_offsets_loaded,
1147 			   &osd->tx_size_511, &nsd->tx_size_511);
1148 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1149 			   I40E_GLPRT_PTC1023L(hw->port),
1150 			   pf->stat_offsets_loaded,
1151 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1152 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1153 			   I40E_GLPRT_PTC1522L(hw->port),
1154 			   pf->stat_offsets_loaded,
1155 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1156 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1157 			   I40E_GLPRT_PTC9522L(hw->port),
1158 			   pf->stat_offsets_loaded,
1159 			   &osd->tx_size_big, &nsd->tx_size_big);
1160 
1161 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1162 			   pf->stat_offsets_loaded,
1163 			   &osd->rx_undersize, &nsd->rx_undersize);
1164 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->rx_fragments, &nsd->rx_fragments);
1167 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1168 			   pf->stat_offsets_loaded,
1169 			   &osd->rx_oversize, &nsd->rx_oversize);
1170 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1171 			   pf->stat_offsets_loaded,
1172 			   &osd->rx_jabber, &nsd->rx_jabber);
1173 
1174 	/* FDIR stats */
1175 	i40e_stat_update_and_clear32(hw,
1176 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1177 			&nsd->fd_atr_match);
1178 	i40e_stat_update_and_clear32(hw,
1179 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1180 			&nsd->fd_sb_match);
1181 	i40e_stat_update_and_clear32(hw,
1182 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1183 			&nsd->fd_atr_tunnel_match);
1184 
1185 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1186 	nsd->tx_lpi_status =
1187 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1188 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1189 	nsd->rx_lpi_status =
1190 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1191 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1192 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1193 			   pf->stat_offsets_loaded,
1194 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1195 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1196 			   pf->stat_offsets_loaded,
1197 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1198 
1199 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1200 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1201 		nsd->fd_sb_status = true;
1202 	else
1203 		nsd->fd_sb_status = false;
1204 
1205 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1206 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1207 		nsd->fd_atr_status = true;
1208 	else
1209 		nsd->fd_atr_status = false;
1210 
1211 	pf->stat_offsets_loaded = true;
1212 }
1213 
1214 /**
1215  * i40e_update_stats - Update the various statistics counters.
1216  * @vsi: the VSI to be updated
1217  *
1218  * Update the various stats for this VSI and its related entities.
1219  **/
1220 void i40e_update_stats(struct i40e_vsi *vsi)
1221 {
1222 	struct i40e_pf *pf = vsi->back;
1223 
1224 	if (vsi == pf->vsi[pf->lan_vsi])
1225 		i40e_update_pf_stats(pf);
1226 
1227 	i40e_update_vsi_stats(vsi);
1228 }
1229 
1230 /**
1231  * i40e_count_filters - counts VSI mac filters
1232  * @vsi: the VSI to be searched
1233  *
1234  * Returns count of mac filters
1235  **/
1236 int i40e_count_filters(struct i40e_vsi *vsi)
1237 {
1238 	struct i40e_mac_filter *f;
1239 	struct hlist_node *h;
1240 	int bkt;
1241 	int cnt = 0;
1242 
1243 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1244 		++cnt;
1245 
1246 	return cnt;
1247 }
1248 
1249 /**
1250  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1251  * @vsi: the VSI to be searched
1252  * @macaddr: the MAC address
1253  * @vlan: the vlan
1254  *
1255  * Returns ptr to the filter object or NULL
1256  **/
1257 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1258 						const u8 *macaddr, s16 vlan)
1259 {
1260 	struct i40e_mac_filter *f;
1261 	u64 key;
1262 
1263 	if (!vsi || !macaddr)
1264 		return NULL;
1265 
1266 	key = i40e_addr_to_hkey(macaddr);
1267 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1268 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1269 		    (vlan == f->vlan))
1270 			return f;
1271 	}
1272 	return NULL;
1273 }
1274 
1275 /**
1276  * i40e_find_mac - Find a mac addr in the macvlan filters list
1277  * @vsi: the VSI to be searched
1278  * @macaddr: the MAC address we are searching for
1279  *
1280  * Returns the first filter with the provided MAC address or NULL if
1281  * MAC address was not found
1282  **/
1283 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1284 {
1285 	struct i40e_mac_filter *f;
1286 	u64 key;
1287 
1288 	if (!vsi || !macaddr)
1289 		return NULL;
1290 
1291 	key = i40e_addr_to_hkey(macaddr);
1292 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1293 		if ((ether_addr_equal(macaddr, f->macaddr)))
1294 			return f;
1295 	}
1296 	return NULL;
1297 }
1298 
1299 /**
1300  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1301  * @vsi: the VSI to be searched
1302  *
1303  * Returns true if VSI is in vlan mode or false otherwise
1304  **/
1305 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1306 {
1307 	/* If we have a PVID, always operate in VLAN mode */
1308 	if (vsi->info.pvid)
1309 		return true;
1310 
1311 	/* We need to operate in VLAN mode whenever we have any filters with
1312 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1313 	 * time, incurring search cost repeatedly. However, we can notice two
1314 	 * things:
1315 	 *
1316 	 * 1) the only place where we can gain a VLAN filter is in
1317 	 *    i40e_add_filter.
1318 	 *
1319 	 * 2) the only place where filters are actually removed is in
1320 	 *    i40e_sync_filters_subtask.
1321 	 *
1322 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1323 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1324 	 * we have to perform the full search after deleting filters in
1325 	 * i40e_sync_filters_subtask, but we already have to search
1326 	 * filters here and can perform the check at the same time. This
1327 	 * results in avoiding embedding a loop for VLAN mode inside another
1328 	 * loop over all the filters, and should maintain correctness as noted
1329 	 * above.
1330 	 */
1331 	return vsi->has_vlan_filter;
1332 }
1333 
1334 /**
1335  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1336  * @vsi: the VSI to configure
1337  * @tmp_add_list: list of filters ready to be added
1338  * @tmp_del_list: list of filters ready to be deleted
1339  * @vlan_filters: the number of active VLAN filters
1340  *
1341  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1342  * behave as expected. If we have any active VLAN filters remaining or about
1343  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1344  * so that they only match against untagged traffic. If we no longer have any
1345  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1346  * so that they match against both tagged and untagged traffic. In this way,
1347  * we ensure that we correctly receive the desired traffic. This ensures that
1348  * when we have an active VLAN we will receive only untagged traffic and
1349  * traffic matching active VLANs. If we have no active VLANs then we will
1350  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1351  *
1352  * Finally, in a similar fashion, this function also corrects filters when
1353  * there is an active PVID assigned to this VSI.
1354  *
1355  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1356  *
1357  * This function is only expected to be called from within
1358  * i40e_sync_vsi_filters.
1359  *
1360  * NOTE: This function expects to be called while under the
1361  * mac_filter_hash_lock
1362  */
1363 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1364 					 struct hlist_head *tmp_add_list,
1365 					 struct hlist_head *tmp_del_list,
1366 					 int vlan_filters)
1367 {
1368 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1369 	struct i40e_mac_filter *f, *add_head;
1370 	struct i40e_new_mac_filter *new;
1371 	struct hlist_node *h;
1372 	int bkt, new_vlan;
1373 
1374 	/* To determine if a particular filter needs to be replaced we
1375 	 * have the three following conditions:
1376 	 *
1377 	 * a) if we have a PVID assigned, then all filters which are
1378 	 *    not marked as VLAN=PVID must be replaced with filters that
1379 	 *    are.
1380 	 * b) otherwise, if we have any active VLANS, all filters
1381 	 *    which are marked as VLAN=-1 must be replaced with
1382 	 *    filters marked as VLAN=0
1383 	 * c) finally, if we do not have any active VLANS, all filters
1384 	 *    which are marked as VLAN=0 must be replaced with filters
1385 	 *    marked as VLAN=-1
1386 	 */
1387 
1388 	/* Update the filters about to be added in place */
1389 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1390 		if (pvid && new->f->vlan != pvid)
1391 			new->f->vlan = pvid;
1392 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1393 			new->f->vlan = 0;
1394 		else if (!vlan_filters && new->f->vlan == 0)
1395 			new->f->vlan = I40E_VLAN_ANY;
1396 	}
1397 
1398 	/* Update the remaining active filters */
1399 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1400 		/* Combine the checks for whether a filter needs to be changed
1401 		 * and then determine the new VLAN inside the if block, in
1402 		 * order to avoid duplicating code for adding the new filter
1403 		 * then deleting the old filter.
1404 		 */
1405 		if ((pvid && f->vlan != pvid) ||
1406 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1407 		    (!vlan_filters && f->vlan == 0)) {
1408 			/* Determine the new vlan we will be adding */
1409 			if (pvid)
1410 				new_vlan = pvid;
1411 			else if (vlan_filters)
1412 				new_vlan = 0;
1413 			else
1414 				new_vlan = I40E_VLAN_ANY;
1415 
1416 			/* Create the new filter */
1417 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1418 			if (!add_head)
1419 				return -ENOMEM;
1420 
1421 			/* Create a temporary i40e_new_mac_filter */
1422 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1423 			if (!new)
1424 				return -ENOMEM;
1425 
1426 			new->f = add_head;
1427 			new->state = add_head->state;
1428 
1429 			/* Add the new filter to the tmp list */
1430 			hlist_add_head(&new->hlist, tmp_add_list);
1431 
1432 			/* Put the original filter into the delete list */
1433 			f->state = I40E_FILTER_REMOVE;
1434 			hash_del(&f->hlist);
1435 			hlist_add_head(&f->hlist, tmp_del_list);
1436 		}
1437 	}
1438 
1439 	vsi->has_vlan_filter = !!vlan_filters;
1440 
1441 	return 0;
1442 }
1443 
1444 /**
1445  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1446  * @vsi: the vsi to configure
1447  * @new_mac: new mac filter to be added
1448  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1449  * @vlan_filters: the number of active VLAN filters
1450  * @trusted: flag if the VF is trusted
1451  *
1452  * Get new VLAN id based on current VLAN filters, trust, PVID
1453  * and vf-vlan-prune-disable flag.
1454  *
1455  * Returns the value of the new vlan filter or
1456  * the old value if no new filter is needed.
1457  */
1458 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1459 				struct i40e_new_mac_filter *new_mac,
1460 				struct i40e_mac_filter *f,
1461 				int vlan_filters,
1462 				bool trusted)
1463 {
1464 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1465 	struct i40e_pf *pf = vsi->back;
1466 	bool is_any;
1467 
1468 	if (new_mac)
1469 		f = new_mac->f;
1470 
1471 	if (pvid && f->vlan != pvid)
1472 		return pvid;
1473 
1474 	is_any = (trusted ||
1475 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1476 
1477 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1478 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1479 	    (is_any && !vlan_filters && f->vlan == 0)) {
1480 		if (is_any)
1481 			return I40E_VLAN_ANY;
1482 		else
1483 			return 0;
1484 	}
1485 
1486 	return f->vlan;
1487 }
1488 
1489 /**
1490  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1491  * @vsi: the vsi to configure
1492  * @tmp_add_list: list of filters ready to be added
1493  * @tmp_del_list: list of filters ready to be deleted
1494  * @vlan_filters: the number of active VLAN filters
1495  * @trusted: flag if the VF is trusted
1496  *
1497  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1498  * and vf-vlan-prune-disable flag.
1499  *
1500  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1501  *
1502  * This function is only expected to be called from within
1503  * i40e_sync_vsi_filters.
1504  *
1505  * NOTE: This function expects to be called while under the
1506  * mac_filter_hash_lock
1507  */
1508 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1509 					    struct hlist_head *tmp_add_list,
1510 					    struct hlist_head *tmp_del_list,
1511 					    int vlan_filters,
1512 					    bool trusted)
1513 {
1514 	struct i40e_mac_filter *f, *add_head;
1515 	struct i40e_new_mac_filter *new_mac;
1516 	struct hlist_node *h;
1517 	int bkt, new_vlan;
1518 
1519 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1520 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1521 							vlan_filters, trusted);
1522 	}
1523 
1524 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1525 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1526 						trusted);
1527 		if (new_vlan != f->vlan) {
1528 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1529 			if (!add_head)
1530 				return -ENOMEM;
1531 			/* Create a temporary i40e_new_mac_filter */
1532 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1533 			if (!new_mac)
1534 				return -ENOMEM;
1535 			new_mac->f = add_head;
1536 			new_mac->state = add_head->state;
1537 
1538 			/* Add the new filter to the tmp list */
1539 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1540 
1541 			/* Put the original filter into the delete list */
1542 			f->state = I40E_FILTER_REMOVE;
1543 			hash_del(&f->hlist);
1544 			hlist_add_head(&f->hlist, tmp_del_list);
1545 		}
1546 	}
1547 
1548 	vsi->has_vlan_filter = !!vlan_filters;
1549 	return 0;
1550 }
1551 
1552 /**
1553  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1554  * @vsi: the PF Main VSI - inappropriate for any other VSI
1555  * @macaddr: the MAC address
1556  *
1557  * Remove whatever filter the firmware set up so the driver can manage
1558  * its own filtering intelligently.
1559  **/
1560 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1561 {
1562 	struct i40e_aqc_remove_macvlan_element_data element;
1563 	struct i40e_pf *pf = vsi->back;
1564 
1565 	/* Only appropriate for the PF main VSI */
1566 	if (vsi->type != I40E_VSI_MAIN)
1567 		return;
1568 
1569 	memset(&element, 0, sizeof(element));
1570 	ether_addr_copy(element.mac_addr, macaddr);
1571 	element.vlan_tag = 0;
1572 	/* Ignore error returns, some firmware does it this way... */
1573 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1574 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1575 
1576 	memset(&element, 0, sizeof(element));
1577 	ether_addr_copy(element.mac_addr, macaddr);
1578 	element.vlan_tag = 0;
1579 	/* ...and some firmware does it this way. */
1580 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1581 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1582 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1583 }
1584 
1585 /**
1586  * i40e_add_filter - Add a mac/vlan filter to the VSI
1587  * @vsi: the VSI to be searched
1588  * @macaddr: the MAC address
1589  * @vlan: the vlan
1590  *
1591  * Returns ptr to the filter object or NULL when no memory available.
1592  *
1593  * NOTE: This function is expected to be called with mac_filter_hash_lock
1594  * being held.
1595  **/
1596 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1597 					const u8 *macaddr, s16 vlan)
1598 {
1599 	struct i40e_mac_filter *f;
1600 	u64 key;
1601 
1602 	if (!vsi || !macaddr)
1603 		return NULL;
1604 
1605 	f = i40e_find_filter(vsi, macaddr, vlan);
1606 	if (!f) {
1607 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1608 		if (!f)
1609 			return NULL;
1610 
1611 		/* Update the boolean indicating if we need to function in
1612 		 * VLAN mode.
1613 		 */
1614 		if (vlan >= 0)
1615 			vsi->has_vlan_filter = true;
1616 
1617 		ether_addr_copy(f->macaddr, macaddr);
1618 		f->vlan = vlan;
1619 		f->state = I40E_FILTER_NEW;
1620 		INIT_HLIST_NODE(&f->hlist);
1621 
1622 		key = i40e_addr_to_hkey(macaddr);
1623 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1624 
1625 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1626 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1627 	}
1628 
1629 	/* If we're asked to add a filter that has been marked for removal, it
1630 	 * is safe to simply restore it to active state. __i40e_del_filter
1631 	 * will have simply deleted any filters which were previously marked
1632 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1633 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1634 	 * task, just restore this filter to the ACTIVE state so that the
1635 	 * sync task leaves it in place
1636 	 */
1637 	if (f->state == I40E_FILTER_REMOVE)
1638 		f->state = I40E_FILTER_ACTIVE;
1639 
1640 	return f;
1641 }
1642 
1643 /**
1644  * __i40e_del_filter - Remove a specific filter from the VSI
1645  * @vsi: VSI to remove from
1646  * @f: the filter to remove from the list
1647  *
1648  * This function should be called instead of i40e_del_filter only if you know
1649  * the exact filter you will remove already, such as via i40e_find_filter or
1650  * i40e_find_mac.
1651  *
1652  * NOTE: This function is expected to be called with mac_filter_hash_lock
1653  * being held.
1654  * ANOTHER NOTE: This function MUST be called from within the context of
1655  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1656  * instead of list_for_each_entry().
1657  **/
1658 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1659 {
1660 	if (!f)
1661 		return;
1662 
1663 	/* If the filter was never added to firmware then we can just delete it
1664 	 * directly and we don't want to set the status to remove or else an
1665 	 * admin queue command will unnecessarily fire.
1666 	 */
1667 	if ((f->state == I40E_FILTER_FAILED) ||
1668 	    (f->state == I40E_FILTER_NEW)) {
1669 		hash_del(&f->hlist);
1670 		kfree(f);
1671 	} else {
1672 		f->state = I40E_FILTER_REMOVE;
1673 	}
1674 
1675 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1676 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1677 }
1678 
1679 /**
1680  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1681  * @vsi: the VSI to be searched
1682  * @macaddr: the MAC address
1683  * @vlan: the VLAN
1684  *
1685  * NOTE: This function is expected to be called with mac_filter_hash_lock
1686  * being held.
1687  * ANOTHER NOTE: This function MUST be called from within the context of
1688  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1689  * instead of list_for_each_entry().
1690  **/
1691 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1692 {
1693 	struct i40e_mac_filter *f;
1694 
1695 	if (!vsi || !macaddr)
1696 		return;
1697 
1698 	f = i40e_find_filter(vsi, macaddr, vlan);
1699 	__i40e_del_filter(vsi, f);
1700 }
1701 
1702 /**
1703  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1704  * @vsi: the VSI to be searched
1705  * @macaddr: the mac address to be filtered
1706  *
1707  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1708  * go through all the macvlan filters and add a macvlan filter for each
1709  * unique vlan that already exists. If a PVID has been assigned, instead only
1710  * add the macaddr to that VLAN.
1711  *
1712  * Returns last filter added on success, else NULL
1713  **/
1714 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1715 					    const u8 *macaddr)
1716 {
1717 	struct i40e_mac_filter *f, *add = NULL;
1718 	struct hlist_node *h;
1719 	int bkt;
1720 
1721 	if (vsi->info.pvid)
1722 		return i40e_add_filter(vsi, macaddr,
1723 				       le16_to_cpu(vsi->info.pvid));
1724 
1725 	if (!i40e_is_vsi_in_vlan(vsi))
1726 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1727 
1728 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1729 		if (f->state == I40E_FILTER_REMOVE)
1730 			continue;
1731 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1732 		if (!add)
1733 			return NULL;
1734 	}
1735 
1736 	return add;
1737 }
1738 
1739 /**
1740  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1741  * @vsi: the VSI to be searched
1742  * @macaddr: the mac address to be removed
1743  *
1744  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1745  * associated with.
1746  *
1747  * Returns 0 for success, or error
1748  **/
1749 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1750 {
1751 	struct i40e_mac_filter *f;
1752 	struct hlist_node *h;
1753 	bool found = false;
1754 	int bkt;
1755 
1756 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1757 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1758 		if (ether_addr_equal(macaddr, f->macaddr)) {
1759 			__i40e_del_filter(vsi, f);
1760 			found = true;
1761 		}
1762 	}
1763 
1764 	if (found)
1765 		return 0;
1766 	else
1767 		return -ENOENT;
1768 }
1769 
1770 /**
1771  * i40e_set_mac - NDO callback to set mac address
1772  * @netdev: network interface device structure
1773  * @p: pointer to an address structure
1774  *
1775  * Returns 0 on success, negative on failure
1776  **/
1777 static int i40e_set_mac(struct net_device *netdev, void *p)
1778 {
1779 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1780 	struct i40e_vsi *vsi = np->vsi;
1781 	struct i40e_pf *pf = vsi->back;
1782 	struct i40e_hw *hw = &pf->hw;
1783 	struct sockaddr *addr = p;
1784 
1785 	if (!is_valid_ether_addr(addr->sa_data))
1786 		return -EADDRNOTAVAIL;
1787 
1788 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1789 		netdev_info(netdev, "already using mac address %pM\n",
1790 			    addr->sa_data);
1791 		return 0;
1792 	}
1793 
1794 	if (test_bit(__I40E_DOWN, pf->state) ||
1795 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1796 		return -EADDRNOTAVAIL;
1797 
1798 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1799 		netdev_info(netdev, "returning to hw mac address %pM\n",
1800 			    hw->mac.addr);
1801 	else
1802 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1803 
1804 	/* Copy the address first, so that we avoid a possible race with
1805 	 * .set_rx_mode().
1806 	 * - Remove old address from MAC filter
1807 	 * - Copy new address
1808 	 * - Add new address to MAC filter
1809 	 */
1810 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1811 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1812 	eth_hw_addr_set(netdev, addr->sa_data);
1813 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1814 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1815 
1816 	if (vsi->type == I40E_VSI_MAIN) {
1817 		i40e_status ret;
1818 
1819 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1820 						addr->sa_data, NULL);
1821 		if (ret)
1822 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1823 				    i40e_stat_str(hw, ret),
1824 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1825 	}
1826 
1827 	/* schedule our worker thread which will take care of
1828 	 * applying the new filter changes
1829 	 */
1830 	i40e_service_event_schedule(pf);
1831 	return 0;
1832 }
1833 
1834 /**
1835  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1836  * @vsi: vsi structure
1837  * @seed: RSS hash seed
1838  * @lut: pointer to lookup table of lut_size
1839  * @lut_size: size of the lookup table
1840  **/
1841 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1842 			      u8 *lut, u16 lut_size)
1843 {
1844 	struct i40e_pf *pf = vsi->back;
1845 	struct i40e_hw *hw = &pf->hw;
1846 	int ret = 0;
1847 
1848 	if (seed) {
1849 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1850 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1851 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1852 		if (ret) {
1853 			dev_info(&pf->pdev->dev,
1854 				 "Cannot set RSS key, err %s aq_err %s\n",
1855 				 i40e_stat_str(hw, ret),
1856 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1857 			return ret;
1858 		}
1859 	}
1860 	if (lut) {
1861 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1862 
1863 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1864 		if (ret) {
1865 			dev_info(&pf->pdev->dev,
1866 				 "Cannot set RSS lut, err %s aq_err %s\n",
1867 				 i40e_stat_str(hw, ret),
1868 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1869 			return ret;
1870 		}
1871 	}
1872 	return ret;
1873 }
1874 
1875 /**
1876  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1877  * @vsi: VSI structure
1878  **/
1879 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1880 {
1881 	struct i40e_pf *pf = vsi->back;
1882 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1883 	u8 *lut;
1884 	int ret;
1885 
1886 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1887 		return 0;
1888 	if (!vsi->rss_size)
1889 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1890 				      vsi->num_queue_pairs);
1891 	if (!vsi->rss_size)
1892 		return -EINVAL;
1893 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1894 	if (!lut)
1895 		return -ENOMEM;
1896 
1897 	/* Use the user configured hash keys and lookup table if there is one,
1898 	 * otherwise use default
1899 	 */
1900 	if (vsi->rss_lut_user)
1901 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1902 	else
1903 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1904 	if (vsi->rss_hkey_user)
1905 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1906 	else
1907 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1908 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1909 	kfree(lut);
1910 	return ret;
1911 }
1912 
1913 /**
1914  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1915  * @vsi: the VSI being configured,
1916  * @ctxt: VSI context structure
1917  * @enabled_tc: number of traffic classes to enable
1918  *
1919  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1920  **/
1921 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1922 					   struct i40e_vsi_context *ctxt,
1923 					   u8 enabled_tc)
1924 {
1925 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1926 	int i, override_q, pow, num_qps, ret;
1927 	u8 netdev_tc = 0, offset = 0;
1928 
1929 	if (vsi->type != I40E_VSI_MAIN)
1930 		return -EINVAL;
1931 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1932 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1933 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1934 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1935 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1936 
1937 	/* find the next higher power-of-2 of num queue pairs */
1938 	pow = ilog2(num_qps);
1939 	if (!is_power_of_2(num_qps))
1940 		pow++;
1941 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1942 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1943 
1944 	/* Setup queue offset/count for all TCs for given VSI */
1945 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1946 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1947 		/* See if the given TC is enabled for the given VSI */
1948 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1949 			offset = vsi->mqprio_qopt.qopt.offset[i];
1950 			qcount = vsi->mqprio_qopt.qopt.count[i];
1951 			if (qcount > max_qcount)
1952 				max_qcount = qcount;
1953 			vsi->tc_config.tc_info[i].qoffset = offset;
1954 			vsi->tc_config.tc_info[i].qcount = qcount;
1955 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1956 		} else {
1957 			/* TC is not enabled so set the offset to
1958 			 * default queue and allocate one queue
1959 			 * for the given TC.
1960 			 */
1961 			vsi->tc_config.tc_info[i].qoffset = 0;
1962 			vsi->tc_config.tc_info[i].qcount = 1;
1963 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1964 		}
1965 	}
1966 
1967 	/* Set actual Tx/Rx queue pairs */
1968 	vsi->num_queue_pairs = offset + qcount;
1969 
1970 	/* Setup queue TC[0].qmap for given VSI context */
1971 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1972 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1973 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1974 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1975 
1976 	/* Reconfigure RSS for main VSI with max queue count */
1977 	vsi->rss_size = max_qcount;
1978 	ret = i40e_vsi_config_rss(vsi);
1979 	if (ret) {
1980 		dev_info(&vsi->back->pdev->dev,
1981 			 "Failed to reconfig rss for num_queues (%u)\n",
1982 			 max_qcount);
1983 		return ret;
1984 	}
1985 	vsi->reconfig_rss = true;
1986 	dev_dbg(&vsi->back->pdev->dev,
1987 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1988 
1989 	/* Find queue count available for channel VSIs and starting offset
1990 	 * for channel VSIs
1991 	 */
1992 	override_q = vsi->mqprio_qopt.qopt.count[0];
1993 	if (override_q && override_q < vsi->num_queue_pairs) {
1994 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1995 		vsi->next_base_queue = override_q;
1996 	}
1997 	return 0;
1998 }
1999 
2000 /**
2001  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2002  * @vsi: the VSI being setup
2003  * @ctxt: VSI context structure
2004  * @enabled_tc: Enabled TCs bitmap
2005  * @is_add: True if called before Add VSI
2006  *
2007  * Setup VSI queue mapping for enabled traffic classes.
2008  **/
2009 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2010 				     struct i40e_vsi_context *ctxt,
2011 				     u8 enabled_tc,
2012 				     bool is_add)
2013 {
2014 	struct i40e_pf *pf = vsi->back;
2015 	u16 num_tc_qps = 0;
2016 	u16 sections = 0;
2017 	u8 netdev_tc = 0;
2018 	u16 numtc = 1;
2019 	u16 qcount;
2020 	u8 offset;
2021 	u16 qmap;
2022 	int i;
2023 
2024 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2025 	offset = 0;
2026 	/* zero out queue mapping, it will get updated on the end of the function */
2027 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2028 
2029 	if (vsi->type == I40E_VSI_MAIN) {
2030 		/* This code helps add more queue to the VSI if we have
2031 		 * more cores than RSS can support, the higher cores will
2032 		 * be served by ATR or other filters. Furthermore, the
2033 		 * non-zero req_queue_pairs says that user requested a new
2034 		 * queue count via ethtool's set_channels, so use this
2035 		 * value for queues distribution across traffic classes
2036 		 * We need at least one queue pair for the interface
2037 		 * to be usable as we see in else statement.
2038 		 */
2039 		if (vsi->req_queue_pairs > 0)
2040 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2041 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2042 			vsi->num_queue_pairs = pf->num_lan_msix;
2043 		else
2044 			vsi->num_queue_pairs = 1;
2045 	}
2046 
2047 	/* Number of queues per enabled TC */
2048 	if (vsi->type == I40E_VSI_MAIN ||
2049 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2050 		num_tc_qps = vsi->num_queue_pairs;
2051 	else
2052 		num_tc_qps = vsi->alloc_queue_pairs;
2053 
2054 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2055 		/* Find numtc from enabled TC bitmap */
2056 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2057 			if (enabled_tc & BIT(i)) /* TC is enabled */
2058 				numtc++;
2059 		}
2060 		if (!numtc) {
2061 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2062 			numtc = 1;
2063 		}
2064 		num_tc_qps = num_tc_qps / numtc;
2065 		num_tc_qps = min_t(int, num_tc_qps,
2066 				   i40e_pf_get_max_q_per_tc(pf));
2067 	}
2068 
2069 	vsi->tc_config.numtc = numtc;
2070 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2071 
2072 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2073 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2074 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2075 
2076 	/* Setup queue offset/count for all TCs for given VSI */
2077 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2078 		/* See if the given TC is enabled for the given VSI */
2079 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2080 			/* TC is enabled */
2081 			int pow, num_qps;
2082 
2083 			switch (vsi->type) {
2084 			case I40E_VSI_MAIN:
2085 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2086 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2087 				    vsi->tc_config.enabled_tc != 1) {
2088 					qcount = min_t(int, pf->alloc_rss_size,
2089 						       num_tc_qps);
2090 					break;
2091 				}
2092 				fallthrough;
2093 			case I40E_VSI_FDIR:
2094 			case I40E_VSI_SRIOV:
2095 			case I40E_VSI_VMDQ2:
2096 			default:
2097 				qcount = num_tc_qps;
2098 				WARN_ON(i != 0);
2099 				break;
2100 			}
2101 			vsi->tc_config.tc_info[i].qoffset = offset;
2102 			vsi->tc_config.tc_info[i].qcount = qcount;
2103 
2104 			/* find the next higher power-of-2 of num queue pairs */
2105 			num_qps = qcount;
2106 			pow = 0;
2107 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2108 				pow++;
2109 				num_qps >>= 1;
2110 			}
2111 
2112 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2113 			qmap =
2114 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2115 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2116 
2117 			offset += qcount;
2118 		} else {
2119 			/* TC is not enabled so set the offset to
2120 			 * default queue and allocate one queue
2121 			 * for the given TC.
2122 			 */
2123 			vsi->tc_config.tc_info[i].qoffset = 0;
2124 			vsi->tc_config.tc_info[i].qcount = 1;
2125 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2126 
2127 			qmap = 0;
2128 		}
2129 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2130 	}
2131 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2132 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2133 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2134 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2135 		vsi->num_queue_pairs = offset;
2136 
2137 	/* Scheduler section valid can only be set for ADD VSI */
2138 	if (is_add) {
2139 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2140 
2141 		ctxt->info.up_enable_bits = enabled_tc;
2142 	}
2143 	if (vsi->type == I40E_VSI_SRIOV) {
2144 		ctxt->info.mapping_flags |=
2145 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2146 		for (i = 0; i < vsi->num_queue_pairs; i++)
2147 			ctxt->info.queue_mapping[i] =
2148 					       cpu_to_le16(vsi->base_queue + i);
2149 	} else {
2150 		ctxt->info.mapping_flags |=
2151 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2152 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2153 	}
2154 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2155 }
2156 
2157 /**
2158  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2159  * @netdev: the netdevice
2160  * @addr: address to add
2161  *
2162  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2163  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2164  */
2165 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2166 {
2167 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2168 	struct i40e_vsi *vsi = np->vsi;
2169 
2170 	if (i40e_add_mac_filter(vsi, addr))
2171 		return 0;
2172 	else
2173 		return -ENOMEM;
2174 }
2175 
2176 /**
2177  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2178  * @netdev: the netdevice
2179  * @addr: address to add
2180  *
2181  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2182  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2183  */
2184 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2185 {
2186 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2187 	struct i40e_vsi *vsi = np->vsi;
2188 
2189 	/* Under some circumstances, we might receive a request to delete
2190 	 * our own device address from our uc list. Because we store the
2191 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2192 	 * such requests and not delete our device address from this list.
2193 	 */
2194 	if (ether_addr_equal(addr, netdev->dev_addr))
2195 		return 0;
2196 
2197 	i40e_del_mac_filter(vsi, addr);
2198 
2199 	return 0;
2200 }
2201 
2202 /**
2203  * i40e_set_rx_mode - NDO callback to set the netdev filters
2204  * @netdev: network interface device structure
2205  **/
2206 static void i40e_set_rx_mode(struct net_device *netdev)
2207 {
2208 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2209 	struct i40e_vsi *vsi = np->vsi;
2210 
2211 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2212 
2213 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2214 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2215 
2216 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2217 
2218 	/* check for other flag changes */
2219 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2220 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2221 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2222 	}
2223 }
2224 
2225 /**
2226  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2227  * @vsi: Pointer to VSI struct
2228  * @from: Pointer to list which contains MAC filter entries - changes to
2229  *        those entries needs to be undone.
2230  *
2231  * MAC filter entries from this list were slated for deletion.
2232  **/
2233 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2234 					 struct hlist_head *from)
2235 {
2236 	struct i40e_mac_filter *f;
2237 	struct hlist_node *h;
2238 
2239 	hlist_for_each_entry_safe(f, h, from, hlist) {
2240 		u64 key = i40e_addr_to_hkey(f->macaddr);
2241 
2242 		/* Move the element back into MAC filter list*/
2243 		hlist_del(&f->hlist);
2244 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2245 	}
2246 }
2247 
2248 /**
2249  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2250  * @vsi: Pointer to vsi struct
2251  * @from: Pointer to list which contains MAC filter entries - changes to
2252  *        those entries needs to be undone.
2253  *
2254  * MAC filter entries from this list were slated for addition.
2255  **/
2256 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2257 					 struct hlist_head *from)
2258 {
2259 	struct i40e_new_mac_filter *new;
2260 	struct hlist_node *h;
2261 
2262 	hlist_for_each_entry_safe(new, h, from, hlist) {
2263 		/* We can simply free the wrapper structure */
2264 		hlist_del(&new->hlist);
2265 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2266 		kfree(new);
2267 	}
2268 }
2269 
2270 /**
2271  * i40e_next_filter - Get the next non-broadcast filter from a list
2272  * @next: pointer to filter in list
2273  *
2274  * Returns the next non-broadcast filter in the list. Required so that we
2275  * ignore broadcast filters within the list, since these are not handled via
2276  * the normal firmware update path.
2277  */
2278 static
2279 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2280 {
2281 	hlist_for_each_entry_continue(next, hlist) {
2282 		if (!is_broadcast_ether_addr(next->f->macaddr))
2283 			return next;
2284 	}
2285 
2286 	return NULL;
2287 }
2288 
2289 /**
2290  * i40e_update_filter_state - Update filter state based on return data
2291  * from firmware
2292  * @count: Number of filters added
2293  * @add_list: return data from fw
2294  * @add_head: pointer to first filter in current batch
2295  *
2296  * MAC filter entries from list were slated to be added to device. Returns
2297  * number of successful filters. Note that 0 does NOT mean success!
2298  **/
2299 static int
2300 i40e_update_filter_state(int count,
2301 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2302 			 struct i40e_new_mac_filter *add_head)
2303 {
2304 	int retval = 0;
2305 	int i;
2306 
2307 	for (i = 0; i < count; i++) {
2308 		/* Always check status of each filter. We don't need to check
2309 		 * the firmware return status because we pre-set the filter
2310 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2311 		 * request to the adminq. Thus, if it no longer matches then
2312 		 * we know the filter is active.
2313 		 */
2314 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2315 			add_head->state = I40E_FILTER_FAILED;
2316 		} else {
2317 			add_head->state = I40E_FILTER_ACTIVE;
2318 			retval++;
2319 		}
2320 
2321 		add_head = i40e_next_filter(add_head);
2322 		if (!add_head)
2323 			break;
2324 	}
2325 
2326 	return retval;
2327 }
2328 
2329 /**
2330  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2331  * @vsi: ptr to the VSI
2332  * @vsi_name: name to display in messages
2333  * @list: the list of filters to send to firmware
2334  * @num_del: the number of filters to delete
2335  * @retval: Set to -EIO on failure to delete
2336  *
2337  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2338  * *retval instead of a return value so that success does not force ret_val to
2339  * be set to 0. This ensures that a sequence of calls to this function
2340  * preserve the previous value of *retval on successful delete.
2341  */
2342 static
2343 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2344 			  struct i40e_aqc_remove_macvlan_element_data *list,
2345 			  int num_del, int *retval)
2346 {
2347 	struct i40e_hw *hw = &vsi->back->hw;
2348 	enum i40e_admin_queue_err aq_status;
2349 	i40e_status aq_ret;
2350 
2351 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2352 					   &aq_status);
2353 
2354 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2355 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2356 		*retval = -EIO;
2357 		dev_info(&vsi->back->pdev->dev,
2358 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2359 			 vsi_name, i40e_stat_str(hw, aq_ret),
2360 			 i40e_aq_str(hw, aq_status));
2361 	}
2362 }
2363 
2364 /**
2365  * i40e_aqc_add_filters - Request firmware to add a set of filters
2366  * @vsi: ptr to the VSI
2367  * @vsi_name: name to display in messages
2368  * @list: the list of filters to send to firmware
2369  * @add_head: Position in the add hlist
2370  * @num_add: the number of filters to add
2371  *
2372  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2373  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2374  * space for more filters.
2375  */
2376 static
2377 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2378 			  struct i40e_aqc_add_macvlan_element_data *list,
2379 			  struct i40e_new_mac_filter *add_head,
2380 			  int num_add)
2381 {
2382 	struct i40e_hw *hw = &vsi->back->hw;
2383 	enum i40e_admin_queue_err aq_status;
2384 	int fcnt;
2385 
2386 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2387 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2388 
2389 	if (fcnt != num_add) {
2390 		if (vsi->type == I40E_VSI_MAIN) {
2391 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2392 			dev_warn(&vsi->back->pdev->dev,
2393 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2394 				 i40e_aq_str(hw, aq_status), vsi_name);
2395 		} else if (vsi->type == I40E_VSI_SRIOV ||
2396 			   vsi->type == I40E_VSI_VMDQ1 ||
2397 			   vsi->type == I40E_VSI_VMDQ2) {
2398 			dev_warn(&vsi->back->pdev->dev,
2399 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2400 				 i40e_aq_str(hw, aq_status), vsi_name,
2401 					     vsi_name);
2402 		} else {
2403 			dev_warn(&vsi->back->pdev->dev,
2404 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2405 				 i40e_aq_str(hw, aq_status), vsi_name,
2406 					     vsi->type);
2407 		}
2408 	}
2409 }
2410 
2411 /**
2412  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2413  * @vsi: pointer to the VSI
2414  * @vsi_name: the VSI name
2415  * @f: filter data
2416  *
2417  * This function sets or clears the promiscuous broadcast flags for VLAN
2418  * filters in order to properly receive broadcast frames. Assumes that only
2419  * broadcast filters are passed.
2420  *
2421  * Returns status indicating success or failure;
2422  **/
2423 static i40e_status
2424 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2425 			  struct i40e_mac_filter *f)
2426 {
2427 	bool enable = f->state == I40E_FILTER_NEW;
2428 	struct i40e_hw *hw = &vsi->back->hw;
2429 	i40e_status aq_ret;
2430 
2431 	if (f->vlan == I40E_VLAN_ANY) {
2432 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2433 						   vsi->seid,
2434 						   enable,
2435 						   NULL);
2436 	} else {
2437 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2438 							    vsi->seid,
2439 							    enable,
2440 							    f->vlan,
2441 							    NULL);
2442 	}
2443 
2444 	if (aq_ret) {
2445 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2446 		dev_warn(&vsi->back->pdev->dev,
2447 			 "Error %s, forcing overflow promiscuous on %s\n",
2448 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2449 			 vsi_name);
2450 	}
2451 
2452 	return aq_ret;
2453 }
2454 
2455 /**
2456  * i40e_set_promiscuous - set promiscuous mode
2457  * @pf: board private structure
2458  * @promisc: promisc on or off
2459  *
2460  * There are different ways of setting promiscuous mode on a PF depending on
2461  * what state/environment we're in.  This identifies and sets it appropriately.
2462  * Returns 0 on success.
2463  **/
2464 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2465 {
2466 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2467 	struct i40e_hw *hw = &pf->hw;
2468 	i40e_status aq_ret;
2469 
2470 	if (vsi->type == I40E_VSI_MAIN &&
2471 	    pf->lan_veb != I40E_NO_VEB &&
2472 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2473 		/* set defport ON for Main VSI instead of true promisc
2474 		 * this way we will get all unicast/multicast and VLAN
2475 		 * promisc behavior but will not get VF or VMDq traffic
2476 		 * replicated on the Main VSI.
2477 		 */
2478 		if (promisc)
2479 			aq_ret = i40e_aq_set_default_vsi(hw,
2480 							 vsi->seid,
2481 							 NULL);
2482 		else
2483 			aq_ret = i40e_aq_clear_default_vsi(hw,
2484 							   vsi->seid,
2485 							   NULL);
2486 		if (aq_ret) {
2487 			dev_info(&pf->pdev->dev,
2488 				 "Set default VSI failed, err %s, aq_err %s\n",
2489 				 i40e_stat_str(hw, aq_ret),
2490 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2491 		}
2492 	} else {
2493 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2494 						  hw,
2495 						  vsi->seid,
2496 						  promisc, NULL,
2497 						  true);
2498 		if (aq_ret) {
2499 			dev_info(&pf->pdev->dev,
2500 				 "set unicast promisc failed, err %s, aq_err %s\n",
2501 				 i40e_stat_str(hw, aq_ret),
2502 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2503 		}
2504 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2505 						  hw,
2506 						  vsi->seid,
2507 						  promisc, NULL);
2508 		if (aq_ret) {
2509 			dev_info(&pf->pdev->dev,
2510 				 "set multicast promisc failed, err %s, aq_err %s\n",
2511 				 i40e_stat_str(hw, aq_ret),
2512 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2513 		}
2514 	}
2515 
2516 	if (!aq_ret)
2517 		pf->cur_promisc = promisc;
2518 
2519 	return aq_ret;
2520 }
2521 
2522 /**
2523  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2524  * @vsi: ptr to the VSI
2525  *
2526  * Push any outstanding VSI filter changes through the AdminQ.
2527  *
2528  * Returns 0 or error value
2529  **/
2530 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2531 {
2532 	struct hlist_head tmp_add_list, tmp_del_list;
2533 	struct i40e_mac_filter *f;
2534 	struct i40e_new_mac_filter *new, *add_head = NULL;
2535 	struct i40e_hw *hw = &vsi->back->hw;
2536 	bool old_overflow, new_overflow;
2537 	unsigned int failed_filters = 0;
2538 	unsigned int vlan_filters = 0;
2539 	char vsi_name[16] = "PF";
2540 	int filter_list_len = 0;
2541 	i40e_status aq_ret = 0;
2542 	u32 changed_flags = 0;
2543 	struct hlist_node *h;
2544 	struct i40e_pf *pf;
2545 	int num_add = 0;
2546 	int num_del = 0;
2547 	int retval = 0;
2548 	u16 cmd_flags;
2549 	int list_size;
2550 	int bkt;
2551 
2552 	/* empty array typed pointers, kcalloc later */
2553 	struct i40e_aqc_add_macvlan_element_data *add_list;
2554 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2555 
2556 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2557 		usleep_range(1000, 2000);
2558 	pf = vsi->back;
2559 
2560 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2561 
2562 	if (vsi->netdev) {
2563 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2564 		vsi->current_netdev_flags = vsi->netdev->flags;
2565 	}
2566 
2567 	INIT_HLIST_HEAD(&tmp_add_list);
2568 	INIT_HLIST_HEAD(&tmp_del_list);
2569 
2570 	if (vsi->type == I40E_VSI_SRIOV)
2571 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2572 	else if (vsi->type != I40E_VSI_MAIN)
2573 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2574 
2575 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2576 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2577 
2578 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2579 		/* Create a list of filters to delete. */
2580 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2581 			if (f->state == I40E_FILTER_REMOVE) {
2582 				/* Move the element into temporary del_list */
2583 				hash_del(&f->hlist);
2584 				hlist_add_head(&f->hlist, &tmp_del_list);
2585 
2586 				/* Avoid counting removed filters */
2587 				continue;
2588 			}
2589 			if (f->state == I40E_FILTER_NEW) {
2590 				/* Create a temporary i40e_new_mac_filter */
2591 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2592 				if (!new)
2593 					goto err_no_memory_locked;
2594 
2595 				/* Store pointer to the real filter */
2596 				new->f = f;
2597 				new->state = f->state;
2598 
2599 				/* Add it to the hash list */
2600 				hlist_add_head(&new->hlist, &tmp_add_list);
2601 			}
2602 
2603 			/* Count the number of active (current and new) VLAN
2604 			 * filters we have now. Does not count filters which
2605 			 * are marked for deletion.
2606 			 */
2607 			if (f->vlan > 0)
2608 				vlan_filters++;
2609 		}
2610 
2611 		if (vsi->type != I40E_VSI_SRIOV)
2612 			retval = i40e_correct_mac_vlan_filters
2613 				(vsi, &tmp_add_list, &tmp_del_list,
2614 				 vlan_filters);
2615 		else
2616 			retval = i40e_correct_vf_mac_vlan_filters
2617 				(vsi, &tmp_add_list, &tmp_del_list,
2618 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2619 
2620 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2621 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2622 
2623 		if (retval)
2624 			goto err_no_memory_locked;
2625 
2626 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2627 	}
2628 
2629 	/* Now process 'del_list' outside the lock */
2630 	if (!hlist_empty(&tmp_del_list)) {
2631 		filter_list_len = hw->aq.asq_buf_size /
2632 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2633 		list_size = filter_list_len *
2634 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2635 		del_list = kzalloc(list_size, GFP_ATOMIC);
2636 		if (!del_list)
2637 			goto err_no_memory;
2638 
2639 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2640 			cmd_flags = 0;
2641 
2642 			/* handle broadcast filters by updating the broadcast
2643 			 * promiscuous flag and release filter list.
2644 			 */
2645 			if (is_broadcast_ether_addr(f->macaddr)) {
2646 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2647 
2648 				hlist_del(&f->hlist);
2649 				kfree(f);
2650 				continue;
2651 			}
2652 
2653 			/* add to delete list */
2654 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2655 			if (f->vlan == I40E_VLAN_ANY) {
2656 				del_list[num_del].vlan_tag = 0;
2657 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2658 			} else {
2659 				del_list[num_del].vlan_tag =
2660 					cpu_to_le16((u16)(f->vlan));
2661 			}
2662 
2663 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2664 			del_list[num_del].flags = cmd_flags;
2665 			num_del++;
2666 
2667 			/* flush a full buffer */
2668 			if (num_del == filter_list_len) {
2669 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2670 						     num_del, &retval);
2671 				memset(del_list, 0, list_size);
2672 				num_del = 0;
2673 			}
2674 			/* Release memory for MAC filter entries which were
2675 			 * synced up with HW.
2676 			 */
2677 			hlist_del(&f->hlist);
2678 			kfree(f);
2679 		}
2680 
2681 		if (num_del) {
2682 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2683 					     num_del, &retval);
2684 		}
2685 
2686 		kfree(del_list);
2687 		del_list = NULL;
2688 	}
2689 
2690 	if (!hlist_empty(&tmp_add_list)) {
2691 		/* Do all the adds now. */
2692 		filter_list_len = hw->aq.asq_buf_size /
2693 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2694 		list_size = filter_list_len *
2695 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2696 		add_list = kzalloc(list_size, GFP_ATOMIC);
2697 		if (!add_list)
2698 			goto err_no_memory;
2699 
2700 		num_add = 0;
2701 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2702 			/* handle broadcast filters by updating the broadcast
2703 			 * promiscuous flag instead of adding a MAC filter.
2704 			 */
2705 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2706 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2707 							      new->f))
2708 					new->state = I40E_FILTER_FAILED;
2709 				else
2710 					new->state = I40E_FILTER_ACTIVE;
2711 				continue;
2712 			}
2713 
2714 			/* add to add array */
2715 			if (num_add == 0)
2716 				add_head = new;
2717 			cmd_flags = 0;
2718 			ether_addr_copy(add_list[num_add].mac_addr,
2719 					new->f->macaddr);
2720 			if (new->f->vlan == I40E_VLAN_ANY) {
2721 				add_list[num_add].vlan_tag = 0;
2722 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2723 			} else {
2724 				add_list[num_add].vlan_tag =
2725 					cpu_to_le16((u16)(new->f->vlan));
2726 			}
2727 			add_list[num_add].queue_number = 0;
2728 			/* set invalid match method for later detection */
2729 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2730 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2731 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2732 			num_add++;
2733 
2734 			/* flush a full buffer */
2735 			if (num_add == filter_list_len) {
2736 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2737 						     add_head, num_add);
2738 				memset(add_list, 0, list_size);
2739 				num_add = 0;
2740 			}
2741 		}
2742 		if (num_add) {
2743 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2744 					     num_add);
2745 		}
2746 		/* Now move all of the filters from the temp add list back to
2747 		 * the VSI's list.
2748 		 */
2749 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2750 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2751 			/* Only update the state if we're still NEW */
2752 			if (new->f->state == I40E_FILTER_NEW)
2753 				new->f->state = new->state;
2754 			hlist_del(&new->hlist);
2755 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2756 			kfree(new);
2757 		}
2758 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2759 		kfree(add_list);
2760 		add_list = NULL;
2761 	}
2762 
2763 	/* Determine the number of active and failed filters. */
2764 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2765 	vsi->active_filters = 0;
2766 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2767 		if (f->state == I40E_FILTER_ACTIVE)
2768 			vsi->active_filters++;
2769 		else if (f->state == I40E_FILTER_FAILED)
2770 			failed_filters++;
2771 	}
2772 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2773 
2774 	/* Check if we are able to exit overflow promiscuous mode. We can
2775 	 * safely exit if we didn't just enter, we no longer have any failed
2776 	 * filters, and we have reduced filters below the threshold value.
2777 	 */
2778 	if (old_overflow && !failed_filters &&
2779 	    vsi->active_filters < vsi->promisc_threshold) {
2780 		dev_info(&pf->pdev->dev,
2781 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2782 			 vsi_name);
2783 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2784 		vsi->promisc_threshold = 0;
2785 	}
2786 
2787 	/* if the VF is not trusted do not do promisc */
2788 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2789 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2790 		goto out;
2791 	}
2792 
2793 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2794 
2795 	/* If we are entering overflow promiscuous, we need to calculate a new
2796 	 * threshold for when we are safe to exit
2797 	 */
2798 	if (!old_overflow && new_overflow)
2799 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2800 
2801 	/* check for changes in promiscuous modes */
2802 	if (changed_flags & IFF_ALLMULTI) {
2803 		bool cur_multipromisc;
2804 
2805 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2806 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2807 							       vsi->seid,
2808 							       cur_multipromisc,
2809 							       NULL);
2810 		if (aq_ret) {
2811 			retval = i40e_aq_rc_to_posix(aq_ret,
2812 						     hw->aq.asq_last_status);
2813 			dev_info(&pf->pdev->dev,
2814 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2815 				 vsi_name,
2816 				 i40e_stat_str(hw, aq_ret),
2817 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2818 		} else {
2819 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2820 				 cur_multipromisc ? "entering" : "leaving");
2821 		}
2822 	}
2823 
2824 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2825 		bool cur_promisc;
2826 
2827 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2828 			       new_overflow);
2829 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2830 		if (aq_ret) {
2831 			retval = i40e_aq_rc_to_posix(aq_ret,
2832 						     hw->aq.asq_last_status);
2833 			dev_info(&pf->pdev->dev,
2834 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2835 				 cur_promisc ? "on" : "off",
2836 				 vsi_name,
2837 				 i40e_stat_str(hw, aq_ret),
2838 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2839 		}
2840 	}
2841 out:
2842 	/* if something went wrong then set the changed flag so we try again */
2843 	if (retval)
2844 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2845 
2846 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2847 	return retval;
2848 
2849 err_no_memory:
2850 	/* Restore elements on the temporary add and delete lists */
2851 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2852 err_no_memory_locked:
2853 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2854 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2855 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2856 
2857 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2858 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2859 	return -ENOMEM;
2860 }
2861 
2862 /**
2863  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2864  * @pf: board private structure
2865  **/
2866 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2867 {
2868 	int v;
2869 
2870 	if (!pf)
2871 		return;
2872 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2873 		return;
2874 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2875 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2876 		return;
2877 	}
2878 
2879 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2880 		if (pf->vsi[v] &&
2881 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2882 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2883 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2884 
2885 			if (ret) {
2886 				/* come back and try again later */
2887 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2888 					pf->state);
2889 				break;
2890 			}
2891 		}
2892 	}
2893 }
2894 
2895 /**
2896  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2897  * @vsi: the vsi
2898  **/
2899 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2900 {
2901 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2902 		return I40E_RXBUFFER_2048;
2903 	else
2904 		return I40E_RXBUFFER_3072;
2905 }
2906 
2907 /**
2908  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2909  * @netdev: network interface device structure
2910  * @new_mtu: new value for maximum frame size
2911  *
2912  * Returns 0 on success, negative on failure
2913  **/
2914 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2915 {
2916 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2917 	struct i40e_vsi *vsi = np->vsi;
2918 	struct i40e_pf *pf = vsi->back;
2919 
2920 	if (i40e_enabled_xdp_vsi(vsi)) {
2921 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2922 
2923 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2924 			return -EINVAL;
2925 	}
2926 
2927 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2928 		   netdev->mtu, new_mtu);
2929 	netdev->mtu = new_mtu;
2930 	if (netif_running(netdev))
2931 		i40e_vsi_reinit_locked(vsi);
2932 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2933 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2934 	return 0;
2935 }
2936 
2937 /**
2938  * i40e_ioctl - Access the hwtstamp interface
2939  * @netdev: network interface device structure
2940  * @ifr: interface request data
2941  * @cmd: ioctl command
2942  **/
2943 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2944 {
2945 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2946 	struct i40e_pf *pf = np->vsi->back;
2947 
2948 	switch (cmd) {
2949 	case SIOCGHWTSTAMP:
2950 		return i40e_ptp_get_ts_config(pf, ifr);
2951 	case SIOCSHWTSTAMP:
2952 		return i40e_ptp_set_ts_config(pf, ifr);
2953 	default:
2954 		return -EOPNOTSUPP;
2955 	}
2956 }
2957 
2958 /**
2959  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2960  * @vsi: the vsi being adjusted
2961  **/
2962 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2963 {
2964 	struct i40e_vsi_context ctxt;
2965 	i40e_status ret;
2966 
2967 	/* Don't modify stripping options if a port VLAN is active */
2968 	if (vsi->info.pvid)
2969 		return;
2970 
2971 	if ((vsi->info.valid_sections &
2972 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2973 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2974 		return;  /* already enabled */
2975 
2976 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2977 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2978 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2979 
2980 	ctxt.seid = vsi->seid;
2981 	ctxt.info = vsi->info;
2982 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2983 	if (ret) {
2984 		dev_info(&vsi->back->pdev->dev,
2985 			 "update vlan stripping failed, err %s aq_err %s\n",
2986 			 i40e_stat_str(&vsi->back->hw, ret),
2987 			 i40e_aq_str(&vsi->back->hw,
2988 				     vsi->back->hw.aq.asq_last_status));
2989 	}
2990 }
2991 
2992 /**
2993  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2994  * @vsi: the vsi being adjusted
2995  **/
2996 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2997 {
2998 	struct i40e_vsi_context ctxt;
2999 	i40e_status ret;
3000 
3001 	/* Don't modify stripping options if a port VLAN is active */
3002 	if (vsi->info.pvid)
3003 		return;
3004 
3005 	if ((vsi->info.valid_sections &
3006 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3007 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3008 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3009 		return;  /* already disabled */
3010 
3011 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3012 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3013 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3014 
3015 	ctxt.seid = vsi->seid;
3016 	ctxt.info = vsi->info;
3017 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3018 	if (ret) {
3019 		dev_info(&vsi->back->pdev->dev,
3020 			 "update vlan stripping failed, err %s aq_err %s\n",
3021 			 i40e_stat_str(&vsi->back->hw, ret),
3022 			 i40e_aq_str(&vsi->back->hw,
3023 				     vsi->back->hw.aq.asq_last_status));
3024 	}
3025 }
3026 
3027 /**
3028  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3029  * @vsi: the vsi being configured
3030  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3031  *
3032  * This is a helper function for adding a new MAC/VLAN filter with the
3033  * specified VLAN for each existing MAC address already in the hash table.
3034  * This function does *not* perform any accounting to update filters based on
3035  * VLAN mode.
3036  *
3037  * NOTE: this function expects to be called while under the
3038  * mac_filter_hash_lock
3039  **/
3040 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3041 {
3042 	struct i40e_mac_filter *f, *add_f;
3043 	struct hlist_node *h;
3044 	int bkt;
3045 
3046 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3047 		/* If we're asked to add a filter that has been marked for
3048 		 * removal, it is safe to simply restore it to active state.
3049 		 * __i40e_del_filter will have simply deleted any filters which
3050 		 * were previously marked NEW or FAILED, so if it is currently
3051 		 * marked REMOVE it must have previously been ACTIVE. Since we
3052 		 * haven't yet run the sync filters task, just restore this
3053 		 * filter to the ACTIVE state so that the sync task leaves it
3054 		 * in place.
3055 		 */
3056 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3057 			f->state = I40E_FILTER_ACTIVE;
3058 			continue;
3059 		} else if (f->state == I40E_FILTER_REMOVE) {
3060 			continue;
3061 		}
3062 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3063 		if (!add_f) {
3064 			dev_info(&vsi->back->pdev->dev,
3065 				 "Could not add vlan filter %d for %pM\n",
3066 				 vid, f->macaddr);
3067 			return -ENOMEM;
3068 		}
3069 	}
3070 
3071 	return 0;
3072 }
3073 
3074 /**
3075  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3076  * @vsi: the VSI being configured
3077  * @vid: VLAN id to be added
3078  **/
3079 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3080 {
3081 	int err;
3082 
3083 	if (vsi->info.pvid)
3084 		return -EINVAL;
3085 
3086 	/* The network stack will attempt to add VID=0, with the intention to
3087 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3088 	 * these packets by default when configured to receive untagged
3089 	 * packets, so we don't need to add a filter for this case.
3090 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3091 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3092 	 * Thus, we do not want to actually add a filter for VID=0
3093 	 */
3094 	if (!vid)
3095 		return 0;
3096 
3097 	/* Locked once because all functions invoked below iterates list*/
3098 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3099 	err = i40e_add_vlan_all_mac(vsi, vid);
3100 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3101 	if (err)
3102 		return err;
3103 
3104 	/* schedule our worker thread which will take care of
3105 	 * applying the new filter changes
3106 	 */
3107 	i40e_service_event_schedule(vsi->back);
3108 	return 0;
3109 }
3110 
3111 /**
3112  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3113  * @vsi: the vsi being configured
3114  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3115  *
3116  * This function should be used to remove all VLAN filters which match the
3117  * given VID. It does not schedule the service event and does not take the
3118  * mac_filter_hash_lock so it may be combined with other operations under
3119  * a single invocation of the mac_filter_hash_lock.
3120  *
3121  * NOTE: this function expects to be called while under the
3122  * mac_filter_hash_lock
3123  */
3124 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3125 {
3126 	struct i40e_mac_filter *f;
3127 	struct hlist_node *h;
3128 	int bkt;
3129 
3130 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3131 		if (f->vlan == vid)
3132 			__i40e_del_filter(vsi, f);
3133 	}
3134 }
3135 
3136 /**
3137  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3138  * @vsi: the VSI being configured
3139  * @vid: VLAN id to be removed
3140  **/
3141 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3142 {
3143 	if (!vid || vsi->info.pvid)
3144 		return;
3145 
3146 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3147 	i40e_rm_vlan_all_mac(vsi, vid);
3148 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3149 
3150 	/* schedule our worker thread which will take care of
3151 	 * applying the new filter changes
3152 	 */
3153 	i40e_service_event_schedule(vsi->back);
3154 }
3155 
3156 /**
3157  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3158  * @netdev: network interface to be adjusted
3159  * @proto: unused protocol value
3160  * @vid: vlan id to be added
3161  *
3162  * net_device_ops implementation for adding vlan ids
3163  **/
3164 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3165 				__always_unused __be16 proto, u16 vid)
3166 {
3167 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3168 	struct i40e_vsi *vsi = np->vsi;
3169 	int ret = 0;
3170 
3171 	if (vid >= VLAN_N_VID)
3172 		return -EINVAL;
3173 
3174 	ret = i40e_vsi_add_vlan(vsi, vid);
3175 	if (!ret)
3176 		set_bit(vid, vsi->active_vlans);
3177 
3178 	return ret;
3179 }
3180 
3181 /**
3182  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3183  * @netdev: network interface to be adjusted
3184  * @proto: unused protocol value
3185  * @vid: vlan id to be added
3186  **/
3187 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3188 				    __always_unused __be16 proto, u16 vid)
3189 {
3190 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3191 	struct i40e_vsi *vsi = np->vsi;
3192 
3193 	if (vid >= VLAN_N_VID)
3194 		return;
3195 	set_bit(vid, vsi->active_vlans);
3196 }
3197 
3198 /**
3199  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3200  * @netdev: network interface to be adjusted
3201  * @proto: unused protocol value
3202  * @vid: vlan id to be removed
3203  *
3204  * net_device_ops implementation for removing vlan ids
3205  **/
3206 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3207 				 __always_unused __be16 proto, u16 vid)
3208 {
3209 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3210 	struct i40e_vsi *vsi = np->vsi;
3211 
3212 	/* return code is ignored as there is nothing a user
3213 	 * can do about failure to remove and a log message was
3214 	 * already printed from the other function
3215 	 */
3216 	i40e_vsi_kill_vlan(vsi, vid);
3217 
3218 	clear_bit(vid, vsi->active_vlans);
3219 
3220 	return 0;
3221 }
3222 
3223 /**
3224  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3225  * @vsi: the vsi being brought back up
3226  **/
3227 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3228 {
3229 	u16 vid;
3230 
3231 	if (!vsi->netdev)
3232 		return;
3233 
3234 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3235 		i40e_vlan_stripping_enable(vsi);
3236 	else
3237 		i40e_vlan_stripping_disable(vsi);
3238 
3239 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3240 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3241 					vid);
3242 }
3243 
3244 /**
3245  * i40e_vsi_add_pvid - Add pvid for the VSI
3246  * @vsi: the vsi being adjusted
3247  * @vid: the vlan id to set as a PVID
3248  **/
3249 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3250 {
3251 	struct i40e_vsi_context ctxt;
3252 	i40e_status ret;
3253 
3254 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3255 	vsi->info.pvid = cpu_to_le16(vid);
3256 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3257 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3258 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3259 
3260 	ctxt.seid = vsi->seid;
3261 	ctxt.info = vsi->info;
3262 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3263 	if (ret) {
3264 		dev_info(&vsi->back->pdev->dev,
3265 			 "add pvid failed, err %s aq_err %s\n",
3266 			 i40e_stat_str(&vsi->back->hw, ret),
3267 			 i40e_aq_str(&vsi->back->hw,
3268 				     vsi->back->hw.aq.asq_last_status));
3269 		return -ENOENT;
3270 	}
3271 
3272 	return 0;
3273 }
3274 
3275 /**
3276  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3277  * @vsi: the vsi being adjusted
3278  *
3279  * Just use the vlan_rx_register() service to put it back to normal
3280  **/
3281 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3282 {
3283 	vsi->info.pvid = 0;
3284 
3285 	i40e_vlan_stripping_disable(vsi);
3286 }
3287 
3288 /**
3289  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3290  * @vsi: ptr to the VSI
3291  *
3292  * If this function returns with an error, then it's possible one or
3293  * more of the rings is populated (while the rest are not).  It is the
3294  * callers duty to clean those orphaned rings.
3295  *
3296  * Return 0 on success, negative on failure
3297  **/
3298 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3299 {
3300 	int i, err = 0;
3301 
3302 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3303 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3304 
3305 	if (!i40e_enabled_xdp_vsi(vsi))
3306 		return err;
3307 
3308 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3309 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3310 
3311 	return err;
3312 }
3313 
3314 /**
3315  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3316  * @vsi: ptr to the VSI
3317  *
3318  * Free VSI's transmit software resources
3319  **/
3320 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3321 {
3322 	int i;
3323 
3324 	if (vsi->tx_rings) {
3325 		for (i = 0; i < vsi->num_queue_pairs; i++)
3326 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3327 				i40e_free_tx_resources(vsi->tx_rings[i]);
3328 	}
3329 
3330 	if (vsi->xdp_rings) {
3331 		for (i = 0; i < vsi->num_queue_pairs; i++)
3332 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3333 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3334 	}
3335 }
3336 
3337 /**
3338  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3339  * @vsi: ptr to the VSI
3340  *
3341  * If this function returns with an error, then it's possible one or
3342  * more of the rings is populated (while the rest are not).  It is the
3343  * callers duty to clean those orphaned rings.
3344  *
3345  * Return 0 on success, negative on failure
3346  **/
3347 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3348 {
3349 	int i, err = 0;
3350 
3351 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3352 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3353 	return err;
3354 }
3355 
3356 /**
3357  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3358  * @vsi: ptr to the VSI
3359  *
3360  * Free all receive software resources
3361  **/
3362 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3363 {
3364 	int i;
3365 
3366 	if (!vsi->rx_rings)
3367 		return;
3368 
3369 	for (i = 0; i < vsi->num_queue_pairs; i++)
3370 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3371 			i40e_free_rx_resources(vsi->rx_rings[i]);
3372 }
3373 
3374 /**
3375  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3376  * @ring: The Tx ring to configure
3377  *
3378  * This enables/disables XPS for a given Tx descriptor ring
3379  * based on the TCs enabled for the VSI that ring belongs to.
3380  **/
3381 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3382 {
3383 	int cpu;
3384 
3385 	if (!ring->q_vector || !ring->netdev || ring->ch)
3386 		return;
3387 
3388 	/* We only initialize XPS once, so as not to overwrite user settings */
3389 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3390 		return;
3391 
3392 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3393 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3394 			    ring->queue_index);
3395 }
3396 
3397 /**
3398  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3399  * @ring: The Tx or Rx ring
3400  *
3401  * Returns the AF_XDP buffer pool or NULL.
3402  **/
3403 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3404 {
3405 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3406 	int qid = ring->queue_index;
3407 
3408 	if (ring_is_xdp(ring))
3409 		qid -= ring->vsi->alloc_queue_pairs;
3410 
3411 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3412 		return NULL;
3413 
3414 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3415 }
3416 
3417 /**
3418  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3419  * @ring: The Tx ring to configure
3420  *
3421  * Configure the Tx descriptor ring in the HMC context.
3422  **/
3423 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3424 {
3425 	struct i40e_vsi *vsi = ring->vsi;
3426 	u16 pf_q = vsi->base_queue + ring->queue_index;
3427 	struct i40e_hw *hw = &vsi->back->hw;
3428 	struct i40e_hmc_obj_txq tx_ctx;
3429 	i40e_status err = 0;
3430 	u32 qtx_ctl = 0;
3431 
3432 	if (ring_is_xdp(ring))
3433 		ring->xsk_pool = i40e_xsk_pool(ring);
3434 
3435 	/* some ATR related tx ring init */
3436 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3437 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3438 		ring->atr_count = 0;
3439 	} else {
3440 		ring->atr_sample_rate = 0;
3441 	}
3442 
3443 	/* configure XPS */
3444 	i40e_config_xps_tx_ring(ring);
3445 
3446 	/* clear the context structure first */
3447 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3448 
3449 	tx_ctx.new_context = 1;
3450 	tx_ctx.base = (ring->dma / 128);
3451 	tx_ctx.qlen = ring->count;
3452 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3453 					       I40E_FLAG_FD_ATR_ENABLED));
3454 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3455 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3456 	if (vsi->type != I40E_VSI_FDIR)
3457 		tx_ctx.head_wb_ena = 1;
3458 	tx_ctx.head_wb_addr = ring->dma +
3459 			      (ring->count * sizeof(struct i40e_tx_desc));
3460 
3461 	/* As part of VSI creation/update, FW allocates certain
3462 	 * Tx arbitration queue sets for each TC enabled for
3463 	 * the VSI. The FW returns the handles to these queue
3464 	 * sets as part of the response buffer to Add VSI,
3465 	 * Update VSI, etc. AQ commands. It is expected that
3466 	 * these queue set handles be associated with the Tx
3467 	 * queues by the driver as part of the TX queue context
3468 	 * initialization. This has to be done regardless of
3469 	 * DCB as by default everything is mapped to TC0.
3470 	 */
3471 
3472 	if (ring->ch)
3473 		tx_ctx.rdylist =
3474 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3475 
3476 	else
3477 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3478 
3479 	tx_ctx.rdylist_act = 0;
3480 
3481 	/* clear the context in the HMC */
3482 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3483 	if (err) {
3484 		dev_info(&vsi->back->pdev->dev,
3485 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3486 			 ring->queue_index, pf_q, err);
3487 		return -ENOMEM;
3488 	}
3489 
3490 	/* set the context in the HMC */
3491 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3492 	if (err) {
3493 		dev_info(&vsi->back->pdev->dev,
3494 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3495 			 ring->queue_index, pf_q, err);
3496 		return -ENOMEM;
3497 	}
3498 
3499 	/* Now associate this queue with this PCI function */
3500 	if (ring->ch) {
3501 		if (ring->ch->type == I40E_VSI_VMDQ2)
3502 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3503 		else
3504 			return -EINVAL;
3505 
3506 		qtx_ctl |= (ring->ch->vsi_number <<
3507 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3508 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3509 	} else {
3510 		if (vsi->type == I40E_VSI_VMDQ2) {
3511 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3512 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3513 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3514 		} else {
3515 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3516 		}
3517 	}
3518 
3519 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3520 		    I40E_QTX_CTL_PF_INDX_MASK);
3521 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3522 	i40e_flush(hw);
3523 
3524 	/* cache tail off for easier writes later */
3525 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3526 
3527 	return 0;
3528 }
3529 
3530 /**
3531  * i40e_rx_offset - Return expected offset into page to access data
3532  * @rx_ring: Ring we are requesting offset of
3533  *
3534  * Returns the offset value for ring into the data buffer.
3535  */
3536 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3537 {
3538 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3539 }
3540 
3541 /**
3542  * i40e_configure_rx_ring - Configure a receive ring context
3543  * @ring: The Rx ring to configure
3544  *
3545  * Configure the Rx descriptor ring in the HMC context.
3546  **/
3547 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3548 {
3549 	struct i40e_vsi *vsi = ring->vsi;
3550 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3551 	u16 pf_q = vsi->base_queue + ring->queue_index;
3552 	struct i40e_hw *hw = &vsi->back->hw;
3553 	struct i40e_hmc_obj_rxq rx_ctx;
3554 	i40e_status err = 0;
3555 	bool ok;
3556 	int ret;
3557 
3558 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3559 
3560 	/* clear the context structure first */
3561 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3562 
3563 	if (ring->vsi->type == I40E_VSI_MAIN)
3564 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3565 
3566 	kfree(ring->rx_bi);
3567 	ring->xsk_pool = i40e_xsk_pool(ring);
3568 	if (ring->xsk_pool) {
3569 		ret = i40e_alloc_rx_bi_zc(ring);
3570 		if (ret)
3571 			return ret;
3572 		ring->rx_buf_len =
3573 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3574 		/* For AF_XDP ZC, we disallow packets to span on
3575 		 * multiple buffers, thus letting us skip that
3576 		 * handling in the fast-path.
3577 		 */
3578 		chain_len = 1;
3579 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3580 						 MEM_TYPE_XSK_BUFF_POOL,
3581 						 NULL);
3582 		if (ret)
3583 			return ret;
3584 		dev_info(&vsi->back->pdev->dev,
3585 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3586 			 ring->queue_index);
3587 
3588 	} else {
3589 		ret = i40e_alloc_rx_bi(ring);
3590 		if (ret)
3591 			return ret;
3592 		ring->rx_buf_len = vsi->rx_buf_len;
3593 		if (ring->vsi->type == I40E_VSI_MAIN) {
3594 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3595 							 MEM_TYPE_PAGE_SHARED,
3596 							 NULL);
3597 			if (ret)
3598 				return ret;
3599 		}
3600 	}
3601 
3602 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3603 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3604 
3605 	rx_ctx.base = (ring->dma / 128);
3606 	rx_ctx.qlen = ring->count;
3607 
3608 	/* use 16 byte descriptors */
3609 	rx_ctx.dsize = 0;
3610 
3611 	/* descriptor type is always zero
3612 	 * rx_ctx.dtype = 0;
3613 	 */
3614 	rx_ctx.hsplit_0 = 0;
3615 
3616 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3617 	if (hw->revision_id == 0)
3618 		rx_ctx.lrxqthresh = 0;
3619 	else
3620 		rx_ctx.lrxqthresh = 1;
3621 	rx_ctx.crcstrip = 1;
3622 	rx_ctx.l2tsel = 1;
3623 	/* this controls whether VLAN is stripped from inner headers */
3624 	rx_ctx.showiv = 0;
3625 	/* set the prefena field to 1 because the manual says to */
3626 	rx_ctx.prefena = 1;
3627 
3628 	/* clear the context in the HMC */
3629 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3630 	if (err) {
3631 		dev_info(&vsi->back->pdev->dev,
3632 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3633 			 ring->queue_index, pf_q, err);
3634 		return -ENOMEM;
3635 	}
3636 
3637 	/* set the context in the HMC */
3638 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3639 	if (err) {
3640 		dev_info(&vsi->back->pdev->dev,
3641 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3642 			 ring->queue_index, pf_q, err);
3643 		return -ENOMEM;
3644 	}
3645 
3646 	/* configure Rx buffer alignment */
3647 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3648 		clear_ring_build_skb_enabled(ring);
3649 	else
3650 		set_ring_build_skb_enabled(ring);
3651 
3652 	ring->rx_offset = i40e_rx_offset(ring);
3653 
3654 	/* cache tail for quicker writes, and clear the reg before use */
3655 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3656 	writel(0, ring->tail);
3657 
3658 	if (ring->xsk_pool) {
3659 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3660 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3661 	} else {
3662 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3663 	}
3664 	if (!ok) {
3665 		/* Log this in case the user has forgotten to give the kernel
3666 		 * any buffers, even later in the application.
3667 		 */
3668 		dev_info(&vsi->back->pdev->dev,
3669 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3670 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3671 			 ring->queue_index, pf_q);
3672 	}
3673 
3674 	return 0;
3675 }
3676 
3677 /**
3678  * i40e_vsi_configure_tx - Configure the VSI for Tx
3679  * @vsi: VSI structure describing this set of rings and resources
3680  *
3681  * Configure the Tx VSI for operation.
3682  **/
3683 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3684 {
3685 	int err = 0;
3686 	u16 i;
3687 
3688 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3689 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3690 
3691 	if (err || !i40e_enabled_xdp_vsi(vsi))
3692 		return err;
3693 
3694 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3695 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3696 
3697 	return err;
3698 }
3699 
3700 /**
3701  * i40e_vsi_configure_rx - Configure the VSI for Rx
3702  * @vsi: the VSI being configured
3703  *
3704  * Configure the Rx VSI for operation.
3705  **/
3706 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3707 {
3708 	int err = 0;
3709 	u16 i;
3710 
3711 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3712 		vsi->max_frame = I40E_MAX_RXBUFFER;
3713 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3714 #if (PAGE_SIZE < 8192)
3715 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3716 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3717 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3718 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3719 #endif
3720 	} else {
3721 		vsi->max_frame = I40E_MAX_RXBUFFER;
3722 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3723 						       I40E_RXBUFFER_2048;
3724 	}
3725 
3726 	/* set up individual rings */
3727 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3728 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3729 
3730 	return err;
3731 }
3732 
3733 /**
3734  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3735  * @vsi: ptr to the VSI
3736  **/
3737 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3738 {
3739 	struct i40e_ring *tx_ring, *rx_ring;
3740 	u16 qoffset, qcount;
3741 	int i, n;
3742 
3743 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3744 		/* Reset the TC information */
3745 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3746 			rx_ring = vsi->rx_rings[i];
3747 			tx_ring = vsi->tx_rings[i];
3748 			rx_ring->dcb_tc = 0;
3749 			tx_ring->dcb_tc = 0;
3750 		}
3751 		return;
3752 	}
3753 
3754 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3755 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3756 			continue;
3757 
3758 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3759 		qcount = vsi->tc_config.tc_info[n].qcount;
3760 		for (i = qoffset; i < (qoffset + qcount); i++) {
3761 			rx_ring = vsi->rx_rings[i];
3762 			tx_ring = vsi->tx_rings[i];
3763 			rx_ring->dcb_tc = n;
3764 			tx_ring->dcb_tc = n;
3765 		}
3766 	}
3767 }
3768 
3769 /**
3770  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3771  * @vsi: ptr to the VSI
3772  **/
3773 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3774 {
3775 	if (vsi->netdev)
3776 		i40e_set_rx_mode(vsi->netdev);
3777 }
3778 
3779 /**
3780  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3781  * @pf: Pointer to the targeted PF
3782  *
3783  * Set all flow director counters to 0.
3784  */
3785 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3786 {
3787 	pf->fd_tcp4_filter_cnt = 0;
3788 	pf->fd_udp4_filter_cnt = 0;
3789 	pf->fd_sctp4_filter_cnt = 0;
3790 	pf->fd_ip4_filter_cnt = 0;
3791 	pf->fd_tcp6_filter_cnt = 0;
3792 	pf->fd_udp6_filter_cnt = 0;
3793 	pf->fd_sctp6_filter_cnt = 0;
3794 	pf->fd_ip6_filter_cnt = 0;
3795 }
3796 
3797 /**
3798  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3799  * @vsi: Pointer to the targeted VSI
3800  *
3801  * This function replays the hlist on the hw where all the SB Flow Director
3802  * filters were saved.
3803  **/
3804 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3805 {
3806 	struct i40e_fdir_filter *filter;
3807 	struct i40e_pf *pf = vsi->back;
3808 	struct hlist_node *node;
3809 
3810 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3811 		return;
3812 
3813 	/* Reset FDir counters as we're replaying all existing filters */
3814 	i40e_reset_fdir_filter_cnt(pf);
3815 
3816 	hlist_for_each_entry_safe(filter, node,
3817 				  &pf->fdir_filter_list, fdir_node) {
3818 		i40e_add_del_fdir(vsi, filter, true);
3819 	}
3820 }
3821 
3822 /**
3823  * i40e_vsi_configure - Set up the VSI for action
3824  * @vsi: the VSI being configured
3825  **/
3826 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3827 {
3828 	int err;
3829 
3830 	i40e_set_vsi_rx_mode(vsi);
3831 	i40e_restore_vlan(vsi);
3832 	i40e_vsi_config_dcb_rings(vsi);
3833 	err = i40e_vsi_configure_tx(vsi);
3834 	if (!err)
3835 		err = i40e_vsi_configure_rx(vsi);
3836 
3837 	return err;
3838 }
3839 
3840 /**
3841  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3842  * @vsi: the VSI being configured
3843  **/
3844 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3845 {
3846 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3847 	struct i40e_pf *pf = vsi->back;
3848 	struct i40e_hw *hw = &pf->hw;
3849 	u16 vector;
3850 	int i, q;
3851 	u32 qp;
3852 
3853 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3854 	 * and PFINT_LNKLSTn registers, e.g.:
3855 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3856 	 */
3857 	qp = vsi->base_queue;
3858 	vector = vsi->base_vector;
3859 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3860 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3861 
3862 		q_vector->rx.next_update = jiffies + 1;
3863 		q_vector->rx.target_itr =
3864 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3865 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3866 		     q_vector->rx.target_itr >> 1);
3867 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3868 
3869 		q_vector->tx.next_update = jiffies + 1;
3870 		q_vector->tx.target_itr =
3871 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3872 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3873 		     q_vector->tx.target_itr >> 1);
3874 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3875 
3876 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3877 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3878 
3879 		/* Linked list for the queuepairs assigned to this vector */
3880 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3881 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3882 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3883 			u32 val;
3884 
3885 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3886 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3887 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3888 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3889 			      (I40E_QUEUE_TYPE_TX <<
3890 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3891 
3892 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3893 
3894 			if (has_xdp) {
3895 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3896 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3897 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3898 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3899 				      (I40E_QUEUE_TYPE_TX <<
3900 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3901 
3902 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3903 			}
3904 
3905 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3906 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3907 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3908 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3909 			      (I40E_QUEUE_TYPE_RX <<
3910 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3911 
3912 			/* Terminate the linked list */
3913 			if (q == (q_vector->num_ringpairs - 1))
3914 				val |= (I40E_QUEUE_END_OF_LIST <<
3915 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3916 
3917 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3918 			qp++;
3919 		}
3920 	}
3921 
3922 	i40e_flush(hw);
3923 }
3924 
3925 /**
3926  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3927  * @pf: pointer to private device data structure
3928  **/
3929 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3930 {
3931 	struct i40e_hw *hw = &pf->hw;
3932 	u32 val;
3933 
3934 	/* clear things first */
3935 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3936 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3937 
3938 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3939 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3940 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3941 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3942 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3943 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3944 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3945 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3946 
3947 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3948 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3949 
3950 	if (pf->flags & I40E_FLAG_PTP)
3951 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3952 
3953 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3954 
3955 	/* SW_ITR_IDX = 0, but don't change INTENA */
3956 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3957 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3958 
3959 	/* OTHER_ITR_IDX = 0 */
3960 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3961 }
3962 
3963 /**
3964  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3965  * @vsi: the VSI being configured
3966  **/
3967 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3968 {
3969 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3970 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3971 	struct i40e_pf *pf = vsi->back;
3972 	struct i40e_hw *hw = &pf->hw;
3973 	u32 val;
3974 
3975 	/* set the ITR configuration */
3976 	q_vector->rx.next_update = jiffies + 1;
3977 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3978 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3979 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3980 	q_vector->tx.next_update = jiffies + 1;
3981 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3982 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3983 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3984 
3985 	i40e_enable_misc_int_causes(pf);
3986 
3987 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3988 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3989 
3990 	/* Associate the queue pair to the vector and enable the queue int */
3991 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3992 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3993 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3994 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3995 
3996 	wr32(hw, I40E_QINT_RQCTL(0), val);
3997 
3998 	if (i40e_enabled_xdp_vsi(vsi)) {
3999 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
4000 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
4001 		      (I40E_QUEUE_TYPE_TX
4002 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
4003 
4004 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
4005 	}
4006 
4007 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
4008 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
4009 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
4010 
4011 	wr32(hw, I40E_QINT_TQCTL(0), val);
4012 	i40e_flush(hw);
4013 }
4014 
4015 /**
4016  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4017  * @pf: board private structure
4018  **/
4019 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4020 {
4021 	struct i40e_hw *hw = &pf->hw;
4022 
4023 	wr32(hw, I40E_PFINT_DYN_CTL0,
4024 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4025 	i40e_flush(hw);
4026 }
4027 
4028 /**
4029  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4030  * @pf: board private structure
4031  **/
4032 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4033 {
4034 	struct i40e_hw *hw = &pf->hw;
4035 	u32 val;
4036 
4037 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4038 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4039 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4040 
4041 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4042 	i40e_flush(hw);
4043 }
4044 
4045 /**
4046  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4047  * @irq: interrupt number
4048  * @data: pointer to a q_vector
4049  **/
4050 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4051 {
4052 	struct i40e_q_vector *q_vector = data;
4053 
4054 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4055 		return IRQ_HANDLED;
4056 
4057 	napi_schedule_irqoff(&q_vector->napi);
4058 
4059 	return IRQ_HANDLED;
4060 }
4061 
4062 /**
4063  * i40e_irq_affinity_notify - Callback for affinity changes
4064  * @notify: context as to what irq was changed
4065  * @mask: the new affinity mask
4066  *
4067  * This is a callback function used by the irq_set_affinity_notifier function
4068  * so that we may register to receive changes to the irq affinity masks.
4069  **/
4070 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4071 				     const cpumask_t *mask)
4072 {
4073 	struct i40e_q_vector *q_vector =
4074 		container_of(notify, struct i40e_q_vector, affinity_notify);
4075 
4076 	cpumask_copy(&q_vector->affinity_mask, mask);
4077 }
4078 
4079 /**
4080  * i40e_irq_affinity_release - Callback for affinity notifier release
4081  * @ref: internal core kernel usage
4082  *
4083  * This is a callback function used by the irq_set_affinity_notifier function
4084  * to inform the current notification subscriber that they will no longer
4085  * receive notifications.
4086  **/
4087 static void i40e_irq_affinity_release(struct kref *ref) {}
4088 
4089 /**
4090  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4091  * @vsi: the VSI being configured
4092  * @basename: name for the vector
4093  *
4094  * Allocates MSI-X vectors and requests interrupts from the kernel.
4095  **/
4096 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4097 {
4098 	int q_vectors = vsi->num_q_vectors;
4099 	struct i40e_pf *pf = vsi->back;
4100 	int base = vsi->base_vector;
4101 	int rx_int_idx = 0;
4102 	int tx_int_idx = 0;
4103 	int vector, err;
4104 	int irq_num;
4105 	int cpu;
4106 
4107 	for (vector = 0; vector < q_vectors; vector++) {
4108 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4109 
4110 		irq_num = pf->msix_entries[base + vector].vector;
4111 
4112 		if (q_vector->tx.ring && q_vector->rx.ring) {
4113 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4114 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4115 			tx_int_idx++;
4116 		} else if (q_vector->rx.ring) {
4117 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4118 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4119 		} else if (q_vector->tx.ring) {
4120 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4121 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4122 		} else {
4123 			/* skip this unused q_vector */
4124 			continue;
4125 		}
4126 		err = request_irq(irq_num,
4127 				  vsi->irq_handler,
4128 				  0,
4129 				  q_vector->name,
4130 				  q_vector);
4131 		if (err) {
4132 			dev_info(&pf->pdev->dev,
4133 				 "MSIX request_irq failed, error: %d\n", err);
4134 			goto free_queue_irqs;
4135 		}
4136 
4137 		/* register for affinity change notifications */
4138 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4139 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4140 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4141 		/* Spread affinity hints out across online CPUs.
4142 		 *
4143 		 * get_cpu_mask returns a static constant mask with
4144 		 * a permanent lifetime so it's ok to pass to
4145 		 * irq_update_affinity_hint without making a copy.
4146 		 */
4147 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4148 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4149 	}
4150 
4151 	vsi->irqs_ready = true;
4152 	return 0;
4153 
4154 free_queue_irqs:
4155 	while (vector) {
4156 		vector--;
4157 		irq_num = pf->msix_entries[base + vector].vector;
4158 		irq_set_affinity_notifier(irq_num, NULL);
4159 		irq_update_affinity_hint(irq_num, NULL);
4160 		free_irq(irq_num, &vsi->q_vectors[vector]);
4161 	}
4162 	return err;
4163 }
4164 
4165 /**
4166  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4167  * @vsi: the VSI being un-configured
4168  **/
4169 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4170 {
4171 	struct i40e_pf *pf = vsi->back;
4172 	struct i40e_hw *hw = &pf->hw;
4173 	int base = vsi->base_vector;
4174 	int i;
4175 
4176 	/* disable interrupt causation from each queue */
4177 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4178 		u32 val;
4179 
4180 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4181 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4182 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4183 
4184 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4185 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4186 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4187 
4188 		if (!i40e_enabled_xdp_vsi(vsi))
4189 			continue;
4190 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4191 	}
4192 
4193 	/* disable each interrupt */
4194 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4195 		for (i = vsi->base_vector;
4196 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4197 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4198 
4199 		i40e_flush(hw);
4200 		for (i = 0; i < vsi->num_q_vectors; i++)
4201 			synchronize_irq(pf->msix_entries[i + base].vector);
4202 	} else {
4203 		/* Legacy and MSI mode - this stops all interrupt handling */
4204 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4205 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4206 		i40e_flush(hw);
4207 		synchronize_irq(pf->pdev->irq);
4208 	}
4209 }
4210 
4211 /**
4212  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4213  * @vsi: the VSI being configured
4214  **/
4215 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4216 {
4217 	struct i40e_pf *pf = vsi->back;
4218 	int i;
4219 
4220 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4221 		for (i = 0; i < vsi->num_q_vectors; i++)
4222 			i40e_irq_dynamic_enable(vsi, i);
4223 	} else {
4224 		i40e_irq_dynamic_enable_icr0(pf);
4225 	}
4226 
4227 	i40e_flush(&pf->hw);
4228 	return 0;
4229 }
4230 
4231 /**
4232  * i40e_free_misc_vector - Free the vector that handles non-queue events
4233  * @pf: board private structure
4234  **/
4235 static void i40e_free_misc_vector(struct i40e_pf *pf)
4236 {
4237 	/* Disable ICR 0 */
4238 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4239 	i40e_flush(&pf->hw);
4240 
4241 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4242 		free_irq(pf->msix_entries[0].vector, pf);
4243 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4244 	}
4245 }
4246 
4247 /**
4248  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4249  * @irq: interrupt number
4250  * @data: pointer to a q_vector
4251  *
4252  * This is the handler used for all MSI/Legacy interrupts, and deals
4253  * with both queue and non-queue interrupts.  This is also used in
4254  * MSIX mode to handle the non-queue interrupts.
4255  **/
4256 static irqreturn_t i40e_intr(int irq, void *data)
4257 {
4258 	struct i40e_pf *pf = (struct i40e_pf *)data;
4259 	struct i40e_hw *hw = &pf->hw;
4260 	irqreturn_t ret = IRQ_NONE;
4261 	u32 icr0, icr0_remaining;
4262 	u32 val, ena_mask;
4263 
4264 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4265 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4266 
4267 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4268 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4269 		goto enable_intr;
4270 
4271 	/* if interrupt but no bits showing, must be SWINT */
4272 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4273 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4274 		pf->sw_int_count++;
4275 
4276 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4277 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4278 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4279 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4280 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4281 	}
4282 
4283 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4284 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4285 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4286 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4287 
4288 		/* We do not have a way to disarm Queue causes while leaving
4289 		 * interrupt enabled for all other causes, ideally
4290 		 * interrupt should be disabled while we are in NAPI but
4291 		 * this is not a performance path and napi_schedule()
4292 		 * can deal with rescheduling.
4293 		 */
4294 		if (!test_bit(__I40E_DOWN, pf->state))
4295 			napi_schedule_irqoff(&q_vector->napi);
4296 	}
4297 
4298 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4299 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4300 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4301 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4302 	}
4303 
4304 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4305 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4306 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4307 	}
4308 
4309 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4310 		/* disable any further VFLR event notifications */
4311 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4312 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4313 
4314 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4315 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4316 		} else {
4317 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4318 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4319 		}
4320 	}
4321 
4322 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4323 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4324 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4325 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4326 		val = rd32(hw, I40E_GLGEN_RSTAT);
4327 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4328 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4329 		if (val == I40E_RESET_CORER) {
4330 			pf->corer_count++;
4331 		} else if (val == I40E_RESET_GLOBR) {
4332 			pf->globr_count++;
4333 		} else if (val == I40E_RESET_EMPR) {
4334 			pf->empr_count++;
4335 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4336 		}
4337 	}
4338 
4339 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4340 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4341 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4342 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4343 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4344 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4345 	}
4346 
4347 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4348 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4349 
4350 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4351 			schedule_work(&pf->ptp_extts0_work);
4352 
4353 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4354 			i40e_ptp_tx_hwtstamp(pf);
4355 
4356 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4357 	}
4358 
4359 	/* If a critical error is pending we have no choice but to reset the
4360 	 * device.
4361 	 * Report and mask out any remaining unexpected interrupts.
4362 	 */
4363 	icr0_remaining = icr0 & ena_mask;
4364 	if (icr0_remaining) {
4365 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4366 			 icr0_remaining);
4367 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4368 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4369 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4370 			dev_info(&pf->pdev->dev, "device will be reset\n");
4371 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4372 			i40e_service_event_schedule(pf);
4373 		}
4374 		ena_mask &= ~icr0_remaining;
4375 	}
4376 	ret = IRQ_HANDLED;
4377 
4378 enable_intr:
4379 	/* re-enable interrupt causes */
4380 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4381 	if (!test_bit(__I40E_DOWN, pf->state) ||
4382 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4383 		i40e_service_event_schedule(pf);
4384 		i40e_irq_dynamic_enable_icr0(pf);
4385 	}
4386 
4387 	return ret;
4388 }
4389 
4390 /**
4391  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4392  * @tx_ring:  tx ring to clean
4393  * @budget:   how many cleans we're allowed
4394  *
4395  * Returns true if there's any budget left (e.g. the clean is finished)
4396  **/
4397 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4398 {
4399 	struct i40e_vsi *vsi = tx_ring->vsi;
4400 	u16 i = tx_ring->next_to_clean;
4401 	struct i40e_tx_buffer *tx_buf;
4402 	struct i40e_tx_desc *tx_desc;
4403 
4404 	tx_buf = &tx_ring->tx_bi[i];
4405 	tx_desc = I40E_TX_DESC(tx_ring, i);
4406 	i -= tx_ring->count;
4407 
4408 	do {
4409 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4410 
4411 		/* if next_to_watch is not set then there is no work pending */
4412 		if (!eop_desc)
4413 			break;
4414 
4415 		/* prevent any other reads prior to eop_desc */
4416 		smp_rmb();
4417 
4418 		/* if the descriptor isn't done, no work yet to do */
4419 		if (!(eop_desc->cmd_type_offset_bsz &
4420 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4421 			break;
4422 
4423 		/* clear next_to_watch to prevent false hangs */
4424 		tx_buf->next_to_watch = NULL;
4425 
4426 		tx_desc->buffer_addr = 0;
4427 		tx_desc->cmd_type_offset_bsz = 0;
4428 		/* move past filter desc */
4429 		tx_buf++;
4430 		tx_desc++;
4431 		i++;
4432 		if (unlikely(!i)) {
4433 			i -= tx_ring->count;
4434 			tx_buf = tx_ring->tx_bi;
4435 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4436 		}
4437 		/* unmap skb header data */
4438 		dma_unmap_single(tx_ring->dev,
4439 				 dma_unmap_addr(tx_buf, dma),
4440 				 dma_unmap_len(tx_buf, len),
4441 				 DMA_TO_DEVICE);
4442 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4443 			kfree(tx_buf->raw_buf);
4444 
4445 		tx_buf->raw_buf = NULL;
4446 		tx_buf->tx_flags = 0;
4447 		tx_buf->next_to_watch = NULL;
4448 		dma_unmap_len_set(tx_buf, len, 0);
4449 		tx_desc->buffer_addr = 0;
4450 		tx_desc->cmd_type_offset_bsz = 0;
4451 
4452 		/* move us past the eop_desc for start of next FD desc */
4453 		tx_buf++;
4454 		tx_desc++;
4455 		i++;
4456 		if (unlikely(!i)) {
4457 			i -= tx_ring->count;
4458 			tx_buf = tx_ring->tx_bi;
4459 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4460 		}
4461 
4462 		/* update budget accounting */
4463 		budget--;
4464 	} while (likely(budget));
4465 
4466 	i += tx_ring->count;
4467 	tx_ring->next_to_clean = i;
4468 
4469 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4470 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4471 
4472 	return budget > 0;
4473 }
4474 
4475 /**
4476  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4477  * @irq: interrupt number
4478  * @data: pointer to a q_vector
4479  **/
4480 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4481 {
4482 	struct i40e_q_vector *q_vector = data;
4483 	struct i40e_vsi *vsi;
4484 
4485 	if (!q_vector->tx.ring)
4486 		return IRQ_HANDLED;
4487 
4488 	vsi = q_vector->tx.ring->vsi;
4489 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4490 
4491 	return IRQ_HANDLED;
4492 }
4493 
4494 /**
4495  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4496  * @vsi: the VSI being configured
4497  * @v_idx: vector index
4498  * @qp_idx: queue pair index
4499  **/
4500 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4501 {
4502 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4503 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4504 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4505 
4506 	tx_ring->q_vector = q_vector;
4507 	tx_ring->next = q_vector->tx.ring;
4508 	q_vector->tx.ring = tx_ring;
4509 	q_vector->tx.count++;
4510 
4511 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4512 	if (i40e_enabled_xdp_vsi(vsi)) {
4513 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4514 
4515 		xdp_ring->q_vector = q_vector;
4516 		xdp_ring->next = q_vector->tx.ring;
4517 		q_vector->tx.ring = xdp_ring;
4518 		q_vector->tx.count++;
4519 	}
4520 
4521 	rx_ring->q_vector = q_vector;
4522 	rx_ring->next = q_vector->rx.ring;
4523 	q_vector->rx.ring = rx_ring;
4524 	q_vector->rx.count++;
4525 }
4526 
4527 /**
4528  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4529  * @vsi: the VSI being configured
4530  *
4531  * This function maps descriptor rings to the queue-specific vectors
4532  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4533  * one vector per queue pair, but on a constrained vector budget, we
4534  * group the queue pairs as "efficiently" as possible.
4535  **/
4536 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4537 {
4538 	int qp_remaining = vsi->num_queue_pairs;
4539 	int q_vectors = vsi->num_q_vectors;
4540 	int num_ringpairs;
4541 	int v_start = 0;
4542 	int qp_idx = 0;
4543 
4544 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4545 	 * group them so there are multiple queues per vector.
4546 	 * It is also important to go through all the vectors available to be
4547 	 * sure that if we don't use all the vectors, that the remaining vectors
4548 	 * are cleared. This is especially important when decreasing the
4549 	 * number of queues in use.
4550 	 */
4551 	for (; v_start < q_vectors; v_start++) {
4552 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4553 
4554 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4555 
4556 		q_vector->num_ringpairs = num_ringpairs;
4557 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4558 
4559 		q_vector->rx.count = 0;
4560 		q_vector->tx.count = 0;
4561 		q_vector->rx.ring = NULL;
4562 		q_vector->tx.ring = NULL;
4563 
4564 		while (num_ringpairs--) {
4565 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4566 			qp_idx++;
4567 			qp_remaining--;
4568 		}
4569 	}
4570 }
4571 
4572 /**
4573  * i40e_vsi_request_irq - Request IRQ from the OS
4574  * @vsi: the VSI being configured
4575  * @basename: name for the vector
4576  **/
4577 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4578 {
4579 	struct i40e_pf *pf = vsi->back;
4580 	int err;
4581 
4582 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4583 		err = i40e_vsi_request_irq_msix(vsi, basename);
4584 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4585 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4586 				  pf->int_name, pf);
4587 	else
4588 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4589 				  pf->int_name, pf);
4590 
4591 	if (err)
4592 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4593 
4594 	return err;
4595 }
4596 
4597 #ifdef CONFIG_NET_POLL_CONTROLLER
4598 /**
4599  * i40e_netpoll - A Polling 'interrupt' handler
4600  * @netdev: network interface device structure
4601  *
4602  * This is used by netconsole to send skbs without having to re-enable
4603  * interrupts.  It's not called while the normal interrupt routine is executing.
4604  **/
4605 static void i40e_netpoll(struct net_device *netdev)
4606 {
4607 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4608 	struct i40e_vsi *vsi = np->vsi;
4609 	struct i40e_pf *pf = vsi->back;
4610 	int i;
4611 
4612 	/* if interface is down do nothing */
4613 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4614 		return;
4615 
4616 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4617 		for (i = 0; i < vsi->num_q_vectors; i++)
4618 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4619 	} else {
4620 		i40e_intr(pf->pdev->irq, netdev);
4621 	}
4622 }
4623 #endif
4624 
4625 #define I40E_QTX_ENA_WAIT_COUNT 50
4626 
4627 /**
4628  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4629  * @pf: the PF being configured
4630  * @pf_q: the PF queue
4631  * @enable: enable or disable state of the queue
4632  *
4633  * This routine will wait for the given Tx queue of the PF to reach the
4634  * enabled or disabled state.
4635  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4636  * multiple retries; else will return 0 in case of success.
4637  **/
4638 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4639 {
4640 	int i;
4641 	u32 tx_reg;
4642 
4643 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4644 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4645 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4646 			break;
4647 
4648 		usleep_range(10, 20);
4649 	}
4650 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4651 		return -ETIMEDOUT;
4652 
4653 	return 0;
4654 }
4655 
4656 /**
4657  * i40e_control_tx_q - Start or stop a particular Tx queue
4658  * @pf: the PF structure
4659  * @pf_q: the PF queue to configure
4660  * @enable: start or stop the queue
4661  *
4662  * This function enables or disables a single queue. Note that any delay
4663  * required after the operation is expected to be handled by the caller of
4664  * this function.
4665  **/
4666 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4667 {
4668 	struct i40e_hw *hw = &pf->hw;
4669 	u32 tx_reg;
4670 	int i;
4671 
4672 	/* warn the TX unit of coming changes */
4673 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4674 	if (!enable)
4675 		usleep_range(10, 20);
4676 
4677 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4678 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4679 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4680 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4681 			break;
4682 		usleep_range(1000, 2000);
4683 	}
4684 
4685 	/* Skip if the queue is already in the requested state */
4686 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4687 		return;
4688 
4689 	/* turn on/off the queue */
4690 	if (enable) {
4691 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4692 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4693 	} else {
4694 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4695 	}
4696 
4697 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4698 }
4699 
4700 /**
4701  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4702  * @seid: VSI SEID
4703  * @pf: the PF structure
4704  * @pf_q: the PF queue to configure
4705  * @is_xdp: true if the queue is used for XDP
4706  * @enable: start or stop the queue
4707  **/
4708 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4709 			   bool is_xdp, bool enable)
4710 {
4711 	int ret;
4712 
4713 	i40e_control_tx_q(pf, pf_q, enable);
4714 
4715 	/* wait for the change to finish */
4716 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4717 	if (ret) {
4718 		dev_info(&pf->pdev->dev,
4719 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4720 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4721 			 (enable ? "en" : "dis"));
4722 	}
4723 
4724 	return ret;
4725 }
4726 
4727 /**
4728  * i40e_vsi_enable_tx - Start a VSI's rings
4729  * @vsi: the VSI being configured
4730  **/
4731 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4732 {
4733 	struct i40e_pf *pf = vsi->back;
4734 	int i, pf_q, ret = 0;
4735 
4736 	pf_q = vsi->base_queue;
4737 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4738 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4739 					     pf_q,
4740 					     false /*is xdp*/, true);
4741 		if (ret)
4742 			break;
4743 
4744 		if (!i40e_enabled_xdp_vsi(vsi))
4745 			continue;
4746 
4747 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4748 					     pf_q + vsi->alloc_queue_pairs,
4749 					     true /*is xdp*/, true);
4750 		if (ret)
4751 			break;
4752 	}
4753 	return ret;
4754 }
4755 
4756 /**
4757  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4758  * @pf: the PF being configured
4759  * @pf_q: the PF queue
4760  * @enable: enable or disable state of the queue
4761  *
4762  * This routine will wait for the given Rx queue of the PF to reach the
4763  * enabled or disabled state.
4764  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4765  * multiple retries; else will return 0 in case of success.
4766  **/
4767 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4768 {
4769 	int i;
4770 	u32 rx_reg;
4771 
4772 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4773 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4774 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4775 			break;
4776 
4777 		usleep_range(10, 20);
4778 	}
4779 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4780 		return -ETIMEDOUT;
4781 
4782 	return 0;
4783 }
4784 
4785 /**
4786  * i40e_control_rx_q - Start or stop a particular Rx queue
4787  * @pf: the PF structure
4788  * @pf_q: the PF queue to configure
4789  * @enable: start or stop the queue
4790  *
4791  * This function enables or disables a single queue. Note that
4792  * any delay required after the operation is expected to be
4793  * handled by the caller of this function.
4794  **/
4795 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4796 {
4797 	struct i40e_hw *hw = &pf->hw;
4798 	u32 rx_reg;
4799 	int i;
4800 
4801 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4802 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4803 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4804 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4805 			break;
4806 		usleep_range(1000, 2000);
4807 	}
4808 
4809 	/* Skip if the queue is already in the requested state */
4810 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4811 		return;
4812 
4813 	/* turn on/off the queue */
4814 	if (enable)
4815 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4816 	else
4817 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4818 
4819 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4820 }
4821 
4822 /**
4823  * i40e_control_wait_rx_q
4824  * @pf: the PF structure
4825  * @pf_q: queue being configured
4826  * @enable: start or stop the rings
4827  *
4828  * This function enables or disables a single queue along with waiting
4829  * for the change to finish. The caller of this function should handle
4830  * the delays needed in the case of disabling queues.
4831  **/
4832 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4833 {
4834 	int ret = 0;
4835 
4836 	i40e_control_rx_q(pf, pf_q, enable);
4837 
4838 	/* wait for the change to finish */
4839 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4840 	if (ret)
4841 		return ret;
4842 
4843 	return ret;
4844 }
4845 
4846 /**
4847  * i40e_vsi_enable_rx - Start a VSI's rings
4848  * @vsi: the VSI being configured
4849  **/
4850 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4851 {
4852 	struct i40e_pf *pf = vsi->back;
4853 	int i, pf_q, ret = 0;
4854 
4855 	pf_q = vsi->base_queue;
4856 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4857 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4858 		if (ret) {
4859 			dev_info(&pf->pdev->dev,
4860 				 "VSI seid %d Rx ring %d enable timeout\n",
4861 				 vsi->seid, pf_q);
4862 			break;
4863 		}
4864 	}
4865 
4866 	return ret;
4867 }
4868 
4869 /**
4870  * i40e_vsi_start_rings - Start a VSI's rings
4871  * @vsi: the VSI being configured
4872  **/
4873 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4874 {
4875 	int ret = 0;
4876 
4877 	/* do rx first for enable and last for disable */
4878 	ret = i40e_vsi_enable_rx(vsi);
4879 	if (ret)
4880 		return ret;
4881 	ret = i40e_vsi_enable_tx(vsi);
4882 
4883 	return ret;
4884 }
4885 
4886 #define I40E_DISABLE_TX_GAP_MSEC	50
4887 
4888 /**
4889  * i40e_vsi_stop_rings - Stop a VSI's rings
4890  * @vsi: the VSI being configured
4891  **/
4892 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4893 {
4894 	struct i40e_pf *pf = vsi->back;
4895 	int pf_q, err, q_end;
4896 
4897 	/* When port TX is suspended, don't wait */
4898 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4899 		return i40e_vsi_stop_rings_no_wait(vsi);
4900 
4901 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4902 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4903 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4904 
4905 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4906 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4907 		if (err)
4908 			dev_info(&pf->pdev->dev,
4909 				 "VSI seid %d Rx ring %d disable timeout\n",
4910 				 vsi->seid, pf_q);
4911 	}
4912 
4913 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4914 	pf_q = vsi->base_queue;
4915 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4916 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4917 
4918 	i40e_vsi_wait_queues_disabled(vsi);
4919 }
4920 
4921 /**
4922  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4923  * @vsi: the VSI being shutdown
4924  *
4925  * This function stops all the rings for a VSI but does not delay to verify
4926  * that rings have been disabled. It is expected that the caller is shutting
4927  * down multiple VSIs at once and will delay together for all the VSIs after
4928  * initiating the shutdown. This is particularly useful for shutting down lots
4929  * of VFs together. Otherwise, a large delay can be incurred while configuring
4930  * each VSI in serial.
4931  **/
4932 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4933 {
4934 	struct i40e_pf *pf = vsi->back;
4935 	int i, pf_q;
4936 
4937 	pf_q = vsi->base_queue;
4938 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4939 		i40e_control_tx_q(pf, pf_q, false);
4940 		i40e_control_rx_q(pf, pf_q, false);
4941 	}
4942 }
4943 
4944 /**
4945  * i40e_vsi_free_irq - Free the irq association with the OS
4946  * @vsi: the VSI being configured
4947  **/
4948 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4949 {
4950 	struct i40e_pf *pf = vsi->back;
4951 	struct i40e_hw *hw = &pf->hw;
4952 	int base = vsi->base_vector;
4953 	u32 val, qp;
4954 	int i;
4955 
4956 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4957 		if (!vsi->q_vectors)
4958 			return;
4959 
4960 		if (!vsi->irqs_ready)
4961 			return;
4962 
4963 		vsi->irqs_ready = false;
4964 		for (i = 0; i < vsi->num_q_vectors; i++) {
4965 			int irq_num;
4966 			u16 vector;
4967 
4968 			vector = i + base;
4969 			irq_num = pf->msix_entries[vector].vector;
4970 
4971 			/* free only the irqs that were actually requested */
4972 			if (!vsi->q_vectors[i] ||
4973 			    !vsi->q_vectors[i]->num_ringpairs)
4974 				continue;
4975 
4976 			/* clear the affinity notifier in the IRQ descriptor */
4977 			irq_set_affinity_notifier(irq_num, NULL);
4978 			/* remove our suggested affinity mask for this IRQ */
4979 			irq_update_affinity_hint(irq_num, NULL);
4980 			free_irq(irq_num, vsi->q_vectors[i]);
4981 
4982 			/* Tear down the interrupt queue link list
4983 			 *
4984 			 * We know that they come in pairs and always
4985 			 * the Rx first, then the Tx.  To clear the
4986 			 * link list, stick the EOL value into the
4987 			 * next_q field of the registers.
4988 			 */
4989 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4990 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4991 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4992 			val |= I40E_QUEUE_END_OF_LIST
4993 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4994 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4995 
4996 			while (qp != I40E_QUEUE_END_OF_LIST) {
4997 				u32 next;
4998 
4999 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5000 
5001 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5002 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5003 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5004 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5005 
5006 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5007 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5008 
5009 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5010 
5011 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5012 
5013 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5014 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5015 
5016 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5017 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5018 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5019 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5020 
5021 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5022 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5023 
5024 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5025 				qp = next;
5026 			}
5027 		}
5028 	} else {
5029 		free_irq(pf->pdev->irq, pf);
5030 
5031 		val = rd32(hw, I40E_PFINT_LNKLST0);
5032 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5033 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5034 		val |= I40E_QUEUE_END_OF_LIST
5035 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5036 		wr32(hw, I40E_PFINT_LNKLST0, val);
5037 
5038 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5039 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5040 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5041 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5042 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5043 
5044 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5045 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5046 
5047 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5048 
5049 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5050 
5051 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5052 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5053 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5054 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5055 
5056 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5057 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5058 
5059 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5060 	}
5061 }
5062 
5063 /**
5064  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5065  * @vsi: the VSI being configured
5066  * @v_idx: Index of vector to be freed
5067  *
5068  * This function frees the memory allocated to the q_vector.  In addition if
5069  * NAPI is enabled it will delete any references to the NAPI struct prior
5070  * to freeing the q_vector.
5071  **/
5072 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5073 {
5074 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5075 	struct i40e_ring *ring;
5076 
5077 	if (!q_vector)
5078 		return;
5079 
5080 	/* disassociate q_vector from rings */
5081 	i40e_for_each_ring(ring, q_vector->tx)
5082 		ring->q_vector = NULL;
5083 
5084 	i40e_for_each_ring(ring, q_vector->rx)
5085 		ring->q_vector = NULL;
5086 
5087 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5088 	if (vsi->netdev)
5089 		netif_napi_del(&q_vector->napi);
5090 
5091 	vsi->q_vectors[v_idx] = NULL;
5092 
5093 	kfree_rcu(q_vector, rcu);
5094 }
5095 
5096 /**
5097  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5098  * @vsi: the VSI being un-configured
5099  *
5100  * This frees the memory allocated to the q_vectors and
5101  * deletes references to the NAPI struct.
5102  **/
5103 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5104 {
5105 	int v_idx;
5106 
5107 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5108 		i40e_free_q_vector(vsi, v_idx);
5109 }
5110 
5111 /**
5112  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5113  * @pf: board private structure
5114  **/
5115 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5116 {
5117 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5118 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5119 		pci_disable_msix(pf->pdev);
5120 		kfree(pf->msix_entries);
5121 		pf->msix_entries = NULL;
5122 		kfree(pf->irq_pile);
5123 		pf->irq_pile = NULL;
5124 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5125 		pci_disable_msi(pf->pdev);
5126 	}
5127 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5128 }
5129 
5130 /**
5131  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5132  * @pf: board private structure
5133  *
5134  * We go through and clear interrupt specific resources and reset the structure
5135  * to pre-load conditions
5136  **/
5137 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5138 {
5139 	int i;
5140 
5141 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5142 		i40e_free_misc_vector(pf);
5143 
5144 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5145 		      I40E_IWARP_IRQ_PILE_ID);
5146 
5147 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5148 	for (i = 0; i < pf->num_alloc_vsi; i++)
5149 		if (pf->vsi[i])
5150 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5151 	i40e_reset_interrupt_capability(pf);
5152 }
5153 
5154 /**
5155  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5156  * @vsi: the VSI being configured
5157  **/
5158 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5159 {
5160 	int q_idx;
5161 
5162 	if (!vsi->netdev)
5163 		return;
5164 
5165 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5166 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5167 
5168 		if (q_vector->rx.ring || q_vector->tx.ring)
5169 			napi_enable(&q_vector->napi);
5170 	}
5171 }
5172 
5173 /**
5174  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5175  * @vsi: the VSI being configured
5176  **/
5177 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5178 {
5179 	int q_idx;
5180 
5181 	if (!vsi->netdev)
5182 		return;
5183 
5184 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5185 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5186 
5187 		if (q_vector->rx.ring || q_vector->tx.ring)
5188 			napi_disable(&q_vector->napi);
5189 	}
5190 }
5191 
5192 /**
5193  * i40e_vsi_close - Shut down a VSI
5194  * @vsi: the vsi to be quelled
5195  **/
5196 static void i40e_vsi_close(struct i40e_vsi *vsi)
5197 {
5198 	struct i40e_pf *pf = vsi->back;
5199 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5200 		i40e_down(vsi);
5201 	i40e_vsi_free_irq(vsi);
5202 	i40e_vsi_free_tx_resources(vsi);
5203 	i40e_vsi_free_rx_resources(vsi);
5204 	vsi->current_netdev_flags = 0;
5205 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5206 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5207 		set_bit(__I40E_CLIENT_RESET, pf->state);
5208 }
5209 
5210 /**
5211  * i40e_quiesce_vsi - Pause a given VSI
5212  * @vsi: the VSI being paused
5213  **/
5214 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5215 {
5216 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5217 		return;
5218 
5219 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5220 	if (vsi->netdev && netif_running(vsi->netdev))
5221 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5222 	else
5223 		i40e_vsi_close(vsi);
5224 }
5225 
5226 /**
5227  * i40e_unquiesce_vsi - Resume a given VSI
5228  * @vsi: the VSI being resumed
5229  **/
5230 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5231 {
5232 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5233 		return;
5234 
5235 	if (vsi->netdev && netif_running(vsi->netdev))
5236 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5237 	else
5238 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5239 }
5240 
5241 /**
5242  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5243  * @pf: the PF
5244  **/
5245 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5246 {
5247 	int v;
5248 
5249 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5250 		if (pf->vsi[v])
5251 			i40e_quiesce_vsi(pf->vsi[v]);
5252 	}
5253 }
5254 
5255 /**
5256  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5257  * @pf: the PF
5258  **/
5259 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5260 {
5261 	int v;
5262 
5263 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5264 		if (pf->vsi[v])
5265 			i40e_unquiesce_vsi(pf->vsi[v]);
5266 	}
5267 }
5268 
5269 /**
5270  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5271  * @vsi: the VSI being configured
5272  *
5273  * Wait until all queues on a given VSI have been disabled.
5274  **/
5275 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5276 {
5277 	struct i40e_pf *pf = vsi->back;
5278 	int i, pf_q, ret;
5279 
5280 	pf_q = vsi->base_queue;
5281 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5282 		/* Check and wait for the Tx queue */
5283 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5284 		if (ret) {
5285 			dev_info(&pf->pdev->dev,
5286 				 "VSI seid %d Tx ring %d disable timeout\n",
5287 				 vsi->seid, pf_q);
5288 			return ret;
5289 		}
5290 
5291 		if (!i40e_enabled_xdp_vsi(vsi))
5292 			goto wait_rx;
5293 
5294 		/* Check and wait for the XDP Tx queue */
5295 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5296 				       false);
5297 		if (ret) {
5298 			dev_info(&pf->pdev->dev,
5299 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5300 				 vsi->seid, pf_q);
5301 			return ret;
5302 		}
5303 wait_rx:
5304 		/* Check and wait for the Rx queue */
5305 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5306 		if (ret) {
5307 			dev_info(&pf->pdev->dev,
5308 				 "VSI seid %d Rx ring %d disable timeout\n",
5309 				 vsi->seid, pf_q);
5310 			return ret;
5311 		}
5312 	}
5313 
5314 	return 0;
5315 }
5316 
5317 #ifdef CONFIG_I40E_DCB
5318 /**
5319  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5320  * @pf: the PF
5321  *
5322  * This function waits for the queues to be in disabled state for all the
5323  * VSIs that are managed by this PF.
5324  **/
5325 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5326 {
5327 	int v, ret = 0;
5328 
5329 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5330 		if (pf->vsi[v]) {
5331 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5332 			if (ret)
5333 				break;
5334 		}
5335 	}
5336 
5337 	return ret;
5338 }
5339 
5340 #endif
5341 
5342 /**
5343  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5344  * @pf: pointer to PF
5345  *
5346  * Get TC map for ISCSI PF type that will include iSCSI TC
5347  * and LAN TC.
5348  **/
5349 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5350 {
5351 	struct i40e_dcb_app_priority_table app;
5352 	struct i40e_hw *hw = &pf->hw;
5353 	u8 enabled_tc = 1; /* TC0 is always enabled */
5354 	u8 tc, i;
5355 	/* Get the iSCSI APP TLV */
5356 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5357 
5358 	for (i = 0; i < dcbcfg->numapps; i++) {
5359 		app = dcbcfg->app[i];
5360 		if (app.selector == I40E_APP_SEL_TCPIP &&
5361 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5362 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5363 			enabled_tc |= BIT(tc);
5364 			break;
5365 		}
5366 	}
5367 
5368 	return enabled_tc;
5369 }
5370 
5371 /**
5372  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5373  * @dcbcfg: the corresponding DCBx configuration structure
5374  *
5375  * Return the number of TCs from given DCBx configuration
5376  **/
5377 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5378 {
5379 	int i, tc_unused = 0;
5380 	u8 num_tc = 0;
5381 	u8 ret = 0;
5382 
5383 	/* Scan the ETS Config Priority Table to find
5384 	 * traffic class enabled for a given priority
5385 	 * and create a bitmask of enabled TCs
5386 	 */
5387 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5388 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5389 
5390 	/* Now scan the bitmask to check for
5391 	 * contiguous TCs starting with TC0
5392 	 */
5393 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5394 		if (num_tc & BIT(i)) {
5395 			if (!tc_unused) {
5396 				ret++;
5397 			} else {
5398 				pr_err("Non-contiguous TC - Disabling DCB\n");
5399 				return 1;
5400 			}
5401 		} else {
5402 			tc_unused = 1;
5403 		}
5404 	}
5405 
5406 	/* There is always at least TC0 */
5407 	if (!ret)
5408 		ret = 1;
5409 
5410 	return ret;
5411 }
5412 
5413 /**
5414  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5415  * @dcbcfg: the corresponding DCBx configuration structure
5416  *
5417  * Query the current DCB configuration and return the number of
5418  * traffic classes enabled from the given DCBX config
5419  **/
5420 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5421 {
5422 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5423 	u8 enabled_tc = 1;
5424 	u8 i;
5425 
5426 	for (i = 0; i < num_tc; i++)
5427 		enabled_tc |= BIT(i);
5428 
5429 	return enabled_tc;
5430 }
5431 
5432 /**
5433  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5434  * @pf: PF being queried
5435  *
5436  * Query the current MQPRIO configuration and return the number of
5437  * traffic classes enabled.
5438  **/
5439 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5440 {
5441 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5442 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5443 	u8 enabled_tc = 1, i;
5444 
5445 	for (i = 1; i < num_tc; i++)
5446 		enabled_tc |= BIT(i);
5447 	return enabled_tc;
5448 }
5449 
5450 /**
5451  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5452  * @pf: PF being queried
5453  *
5454  * Return number of traffic classes enabled for the given PF
5455  **/
5456 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5457 {
5458 	struct i40e_hw *hw = &pf->hw;
5459 	u8 i, enabled_tc = 1;
5460 	u8 num_tc = 0;
5461 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5462 
5463 	if (i40e_is_tc_mqprio_enabled(pf))
5464 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5465 
5466 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5467 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5468 		return 1;
5469 
5470 	/* SFP mode will be enabled for all TCs on port */
5471 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5472 		return i40e_dcb_get_num_tc(dcbcfg);
5473 
5474 	/* MFP mode return count of enabled TCs for this PF */
5475 	if (pf->hw.func_caps.iscsi)
5476 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5477 	else
5478 		return 1; /* Only TC0 */
5479 
5480 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5481 		if (enabled_tc & BIT(i))
5482 			num_tc++;
5483 	}
5484 	return num_tc;
5485 }
5486 
5487 /**
5488  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5489  * @pf: PF being queried
5490  *
5491  * Return a bitmap for enabled traffic classes for this PF.
5492  **/
5493 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5494 {
5495 	if (i40e_is_tc_mqprio_enabled(pf))
5496 		return i40e_mqprio_get_enabled_tc(pf);
5497 
5498 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5499 	 * default TC
5500 	 */
5501 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5502 		return I40E_DEFAULT_TRAFFIC_CLASS;
5503 
5504 	/* SFP mode we want PF to be enabled for all TCs */
5505 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5506 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5507 
5508 	/* MFP enabled and iSCSI PF type */
5509 	if (pf->hw.func_caps.iscsi)
5510 		return i40e_get_iscsi_tc_map(pf);
5511 	else
5512 		return I40E_DEFAULT_TRAFFIC_CLASS;
5513 }
5514 
5515 /**
5516  * i40e_vsi_get_bw_info - Query VSI BW Information
5517  * @vsi: the VSI being queried
5518  *
5519  * Returns 0 on success, negative value on failure
5520  **/
5521 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5522 {
5523 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5524 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5525 	struct i40e_pf *pf = vsi->back;
5526 	struct i40e_hw *hw = &pf->hw;
5527 	i40e_status ret;
5528 	u32 tc_bw_max;
5529 	int i;
5530 
5531 	/* Get the VSI level BW configuration */
5532 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5533 	if (ret) {
5534 		dev_info(&pf->pdev->dev,
5535 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5536 			 i40e_stat_str(&pf->hw, ret),
5537 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5538 		return -EINVAL;
5539 	}
5540 
5541 	/* Get the VSI level BW configuration per TC */
5542 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5543 					       NULL);
5544 	if (ret) {
5545 		dev_info(&pf->pdev->dev,
5546 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5547 			 i40e_stat_str(&pf->hw, ret),
5548 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5549 		return -EINVAL;
5550 	}
5551 
5552 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5553 		dev_info(&pf->pdev->dev,
5554 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5555 			 bw_config.tc_valid_bits,
5556 			 bw_ets_config.tc_valid_bits);
5557 		/* Still continuing */
5558 	}
5559 
5560 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5561 	vsi->bw_max_quanta = bw_config.max_bw;
5562 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5563 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5564 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5565 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5566 		vsi->bw_ets_limit_credits[i] =
5567 					le16_to_cpu(bw_ets_config.credits[i]);
5568 		/* 3 bits out of 4 for each TC */
5569 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5570 	}
5571 
5572 	return 0;
5573 }
5574 
5575 /**
5576  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5577  * @vsi: the VSI being configured
5578  * @enabled_tc: TC bitmap
5579  * @bw_share: BW shared credits per TC
5580  *
5581  * Returns 0 on success, negative value on failure
5582  **/
5583 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5584 				       u8 *bw_share)
5585 {
5586 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5587 	struct i40e_pf *pf = vsi->back;
5588 	i40e_status ret;
5589 	int i;
5590 
5591 	/* There is no need to reset BW when mqprio mode is on.  */
5592 	if (i40e_is_tc_mqprio_enabled(pf))
5593 		return 0;
5594 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5595 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5596 		if (ret)
5597 			dev_info(&pf->pdev->dev,
5598 				 "Failed to reset tx rate for vsi->seid %u\n",
5599 				 vsi->seid);
5600 		return ret;
5601 	}
5602 	memset(&bw_data, 0, sizeof(bw_data));
5603 	bw_data.tc_valid_bits = enabled_tc;
5604 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5605 		bw_data.tc_bw_credits[i] = bw_share[i];
5606 
5607 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5608 	if (ret) {
5609 		dev_info(&pf->pdev->dev,
5610 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5611 			 pf->hw.aq.asq_last_status);
5612 		return -EINVAL;
5613 	}
5614 
5615 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5616 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5617 
5618 	return 0;
5619 }
5620 
5621 /**
5622  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5623  * @vsi: the VSI being configured
5624  * @enabled_tc: TC map to be enabled
5625  *
5626  **/
5627 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5628 {
5629 	struct net_device *netdev = vsi->netdev;
5630 	struct i40e_pf *pf = vsi->back;
5631 	struct i40e_hw *hw = &pf->hw;
5632 	u8 netdev_tc = 0;
5633 	int i;
5634 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5635 
5636 	if (!netdev)
5637 		return;
5638 
5639 	if (!enabled_tc) {
5640 		netdev_reset_tc(netdev);
5641 		return;
5642 	}
5643 
5644 	/* Set up actual enabled TCs on the VSI */
5645 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5646 		return;
5647 
5648 	/* set per TC queues for the VSI */
5649 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5650 		/* Only set TC queues for enabled tcs
5651 		 *
5652 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5653 		 * enabled_tc bitmap would be 0x00001001; the driver
5654 		 * will set the numtc for netdev as 2 that will be
5655 		 * referenced by the netdev layer as TC 0 and 1.
5656 		 */
5657 		if (vsi->tc_config.enabled_tc & BIT(i))
5658 			netdev_set_tc_queue(netdev,
5659 					vsi->tc_config.tc_info[i].netdev_tc,
5660 					vsi->tc_config.tc_info[i].qcount,
5661 					vsi->tc_config.tc_info[i].qoffset);
5662 	}
5663 
5664 	if (i40e_is_tc_mqprio_enabled(pf))
5665 		return;
5666 
5667 	/* Assign UP2TC map for the VSI */
5668 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5669 		/* Get the actual TC# for the UP */
5670 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5671 		/* Get the mapped netdev TC# for the UP */
5672 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5673 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5674 	}
5675 }
5676 
5677 /**
5678  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5679  * @vsi: the VSI being configured
5680  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5681  **/
5682 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5683 				      struct i40e_vsi_context *ctxt)
5684 {
5685 	/* copy just the sections touched not the entire info
5686 	 * since not all sections are valid as returned by
5687 	 * update vsi params
5688 	 */
5689 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5690 	memcpy(&vsi->info.queue_mapping,
5691 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5692 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5693 	       sizeof(vsi->info.tc_mapping));
5694 }
5695 
5696 /**
5697  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5698  * @vsi: the VSI being reconfigured
5699  * @vsi_offset: offset from main VF VSI
5700  */
5701 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5702 {
5703 	struct i40e_vsi_context ctxt = {};
5704 	struct i40e_pf *pf;
5705 	struct i40e_hw *hw;
5706 	int ret;
5707 
5708 	if (!vsi)
5709 		return I40E_ERR_PARAM;
5710 	pf = vsi->back;
5711 	hw = &pf->hw;
5712 
5713 	ctxt.seid = vsi->seid;
5714 	ctxt.pf_num = hw->pf_id;
5715 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5716 	ctxt.uplink_seid = vsi->uplink_seid;
5717 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5718 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5719 	ctxt.info = vsi->info;
5720 
5721 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5722 				 false);
5723 	if (vsi->reconfig_rss) {
5724 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5725 				      vsi->num_queue_pairs);
5726 		ret = i40e_vsi_config_rss(vsi);
5727 		if (ret) {
5728 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5729 			return ret;
5730 		}
5731 		vsi->reconfig_rss = false;
5732 	}
5733 
5734 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5735 	if (ret) {
5736 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5737 			 i40e_stat_str(hw, ret),
5738 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5739 		return ret;
5740 	}
5741 	/* update the local VSI info with updated queue map */
5742 	i40e_vsi_update_queue_map(vsi, &ctxt);
5743 	vsi->info.valid_sections = 0;
5744 
5745 	return ret;
5746 }
5747 
5748 /**
5749  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5750  * @vsi: VSI to be configured
5751  * @enabled_tc: TC bitmap
5752  *
5753  * This configures a particular VSI for TCs that are mapped to the
5754  * given TC bitmap. It uses default bandwidth share for TCs across
5755  * VSIs to configure TC for a particular VSI.
5756  *
5757  * NOTE:
5758  * It is expected that the VSI queues have been quisced before calling
5759  * this function.
5760  **/
5761 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5762 {
5763 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5764 	struct i40e_pf *pf = vsi->back;
5765 	struct i40e_hw *hw = &pf->hw;
5766 	struct i40e_vsi_context ctxt;
5767 	int ret = 0;
5768 	int i;
5769 
5770 	/* Check if enabled_tc is same as existing or new TCs */
5771 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5772 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5773 		return ret;
5774 
5775 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5776 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5777 		if (enabled_tc & BIT(i))
5778 			bw_share[i] = 1;
5779 	}
5780 
5781 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5782 	if (ret) {
5783 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5784 
5785 		dev_info(&pf->pdev->dev,
5786 			 "Failed configuring TC map %d for VSI %d\n",
5787 			 enabled_tc, vsi->seid);
5788 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5789 						  &bw_config, NULL);
5790 		if (ret) {
5791 			dev_info(&pf->pdev->dev,
5792 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5793 				 i40e_stat_str(hw, ret),
5794 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5795 			goto out;
5796 		}
5797 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5798 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5799 
5800 			if (!valid_tc)
5801 				valid_tc = bw_config.tc_valid_bits;
5802 			/* Always enable TC0, no matter what */
5803 			valid_tc |= 1;
5804 			dev_info(&pf->pdev->dev,
5805 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5806 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5807 			enabled_tc = valid_tc;
5808 		}
5809 
5810 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5811 		if (ret) {
5812 			dev_err(&pf->pdev->dev,
5813 				"Unable to  configure TC map %d for VSI %d\n",
5814 				enabled_tc, vsi->seid);
5815 			goto out;
5816 		}
5817 	}
5818 
5819 	/* Update Queue Pairs Mapping for currently enabled UPs */
5820 	ctxt.seid = vsi->seid;
5821 	ctxt.pf_num = vsi->back->hw.pf_id;
5822 	ctxt.vf_num = 0;
5823 	ctxt.uplink_seid = vsi->uplink_seid;
5824 	ctxt.info = vsi->info;
5825 	if (i40e_is_tc_mqprio_enabled(pf)) {
5826 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5827 		if (ret)
5828 			goto out;
5829 	} else {
5830 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5831 	}
5832 
5833 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5834 	 * queues changed.
5835 	 */
5836 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5837 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5838 				      vsi->num_queue_pairs);
5839 		ret = i40e_vsi_config_rss(vsi);
5840 		if (ret) {
5841 			dev_info(&vsi->back->pdev->dev,
5842 				 "Failed to reconfig rss for num_queues\n");
5843 			return ret;
5844 		}
5845 		vsi->reconfig_rss = false;
5846 	}
5847 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5848 		ctxt.info.valid_sections |=
5849 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5850 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5851 	}
5852 
5853 	/* Update the VSI after updating the VSI queue-mapping
5854 	 * information
5855 	 */
5856 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5857 	if (ret) {
5858 		dev_info(&pf->pdev->dev,
5859 			 "Update vsi tc config failed, err %s aq_err %s\n",
5860 			 i40e_stat_str(hw, ret),
5861 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5862 		goto out;
5863 	}
5864 	/* update the local VSI info with updated queue map */
5865 	i40e_vsi_update_queue_map(vsi, &ctxt);
5866 	vsi->info.valid_sections = 0;
5867 
5868 	/* Update current VSI BW information */
5869 	ret = i40e_vsi_get_bw_info(vsi);
5870 	if (ret) {
5871 		dev_info(&pf->pdev->dev,
5872 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5873 			 i40e_stat_str(hw, ret),
5874 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5875 		goto out;
5876 	}
5877 
5878 	/* Update the netdev TC setup */
5879 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5880 out:
5881 	return ret;
5882 }
5883 
5884 /**
5885  * i40e_get_link_speed - Returns link speed for the interface
5886  * @vsi: VSI to be configured
5887  *
5888  **/
5889 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5890 {
5891 	struct i40e_pf *pf = vsi->back;
5892 
5893 	switch (pf->hw.phy.link_info.link_speed) {
5894 	case I40E_LINK_SPEED_40GB:
5895 		return 40000;
5896 	case I40E_LINK_SPEED_25GB:
5897 		return 25000;
5898 	case I40E_LINK_SPEED_20GB:
5899 		return 20000;
5900 	case I40E_LINK_SPEED_10GB:
5901 		return 10000;
5902 	case I40E_LINK_SPEED_1GB:
5903 		return 1000;
5904 	default:
5905 		return -EINVAL;
5906 	}
5907 }
5908 
5909 /**
5910  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5911  * @vsi: VSI to be configured
5912  * @seid: seid of the channel/VSI
5913  * @max_tx_rate: max TX rate to be configured as BW limit
5914  *
5915  * Helper function to set BW limit for a given VSI
5916  **/
5917 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5918 {
5919 	struct i40e_pf *pf = vsi->back;
5920 	u64 credits = 0;
5921 	int speed = 0;
5922 	int ret = 0;
5923 
5924 	speed = i40e_get_link_speed(vsi);
5925 	if (max_tx_rate > speed) {
5926 		dev_err(&pf->pdev->dev,
5927 			"Invalid max tx rate %llu specified for VSI seid %d.",
5928 			max_tx_rate, seid);
5929 		return -EINVAL;
5930 	}
5931 	if (max_tx_rate && max_tx_rate < 50) {
5932 		dev_warn(&pf->pdev->dev,
5933 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5934 		max_tx_rate = 50;
5935 	}
5936 
5937 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5938 	credits = max_tx_rate;
5939 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5940 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5941 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5942 	if (ret)
5943 		dev_err(&pf->pdev->dev,
5944 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5945 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5946 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5947 	return ret;
5948 }
5949 
5950 /**
5951  * i40e_remove_queue_channels - Remove queue channels for the TCs
5952  * @vsi: VSI to be configured
5953  *
5954  * Remove queue channels for the TCs
5955  **/
5956 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5957 {
5958 	enum i40e_admin_queue_err last_aq_status;
5959 	struct i40e_cloud_filter *cfilter;
5960 	struct i40e_channel *ch, *ch_tmp;
5961 	struct i40e_pf *pf = vsi->back;
5962 	struct hlist_node *node;
5963 	int ret, i;
5964 
5965 	/* Reset rss size that was stored when reconfiguring rss for
5966 	 * channel VSIs with non-power-of-2 queue count.
5967 	 */
5968 	vsi->current_rss_size = 0;
5969 
5970 	/* perform cleanup for channels if they exist */
5971 	if (list_empty(&vsi->ch_list))
5972 		return;
5973 
5974 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5975 		struct i40e_vsi *p_vsi;
5976 
5977 		list_del(&ch->list);
5978 		p_vsi = ch->parent_vsi;
5979 		if (!p_vsi || !ch->initialized) {
5980 			kfree(ch);
5981 			continue;
5982 		}
5983 		/* Reset queue contexts */
5984 		for (i = 0; i < ch->num_queue_pairs; i++) {
5985 			struct i40e_ring *tx_ring, *rx_ring;
5986 			u16 pf_q;
5987 
5988 			pf_q = ch->base_queue + i;
5989 			tx_ring = vsi->tx_rings[pf_q];
5990 			tx_ring->ch = NULL;
5991 
5992 			rx_ring = vsi->rx_rings[pf_q];
5993 			rx_ring->ch = NULL;
5994 		}
5995 
5996 		/* Reset BW configured for this VSI via mqprio */
5997 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5998 		if (ret)
5999 			dev_info(&vsi->back->pdev->dev,
6000 				 "Failed to reset tx rate for ch->seid %u\n",
6001 				 ch->seid);
6002 
6003 		/* delete cloud filters associated with this channel */
6004 		hlist_for_each_entry_safe(cfilter, node,
6005 					  &pf->cloud_filter_list, cloud_node) {
6006 			if (cfilter->seid != ch->seid)
6007 				continue;
6008 
6009 			hash_del(&cfilter->cloud_node);
6010 			if (cfilter->dst_port)
6011 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6012 									cfilter,
6013 									false);
6014 			else
6015 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6016 								false);
6017 			last_aq_status = pf->hw.aq.asq_last_status;
6018 			if (ret)
6019 				dev_info(&pf->pdev->dev,
6020 					 "Failed to delete cloud filter, err %s aq_err %s\n",
6021 					 i40e_stat_str(&pf->hw, ret),
6022 					 i40e_aq_str(&pf->hw, last_aq_status));
6023 			kfree(cfilter);
6024 		}
6025 
6026 		/* delete VSI from FW */
6027 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6028 					     NULL);
6029 		if (ret)
6030 			dev_err(&vsi->back->pdev->dev,
6031 				"unable to remove channel (%d) for parent VSI(%d)\n",
6032 				ch->seid, p_vsi->seid);
6033 		kfree(ch);
6034 	}
6035 	INIT_LIST_HEAD(&vsi->ch_list);
6036 }
6037 
6038 /**
6039  * i40e_get_max_queues_for_channel
6040  * @vsi: ptr to VSI to which channels are associated with
6041  *
6042  * Helper function which returns max value among the queue counts set on the
6043  * channels/TCs created.
6044  **/
6045 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6046 {
6047 	struct i40e_channel *ch, *ch_tmp;
6048 	int max = 0;
6049 
6050 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6051 		if (!ch->initialized)
6052 			continue;
6053 		if (ch->num_queue_pairs > max)
6054 			max = ch->num_queue_pairs;
6055 	}
6056 
6057 	return max;
6058 }
6059 
6060 /**
6061  * i40e_validate_num_queues - validate num_queues w.r.t channel
6062  * @pf: ptr to PF device
6063  * @num_queues: number of queues
6064  * @vsi: the parent VSI
6065  * @reconfig_rss: indicates should the RSS be reconfigured or not
6066  *
6067  * This function validates number of queues in the context of new channel
6068  * which is being established and determines if RSS should be reconfigured
6069  * or not for parent VSI.
6070  **/
6071 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6072 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6073 {
6074 	int max_ch_queues;
6075 
6076 	if (!reconfig_rss)
6077 		return -EINVAL;
6078 
6079 	*reconfig_rss = false;
6080 	if (vsi->current_rss_size) {
6081 		if (num_queues > vsi->current_rss_size) {
6082 			dev_dbg(&pf->pdev->dev,
6083 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6084 				num_queues, vsi->current_rss_size);
6085 			return -EINVAL;
6086 		} else if ((num_queues < vsi->current_rss_size) &&
6087 			   (!is_power_of_2(num_queues))) {
6088 			dev_dbg(&pf->pdev->dev,
6089 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6090 				num_queues, vsi->current_rss_size);
6091 			return -EINVAL;
6092 		}
6093 	}
6094 
6095 	if (!is_power_of_2(num_queues)) {
6096 		/* Find the max num_queues configured for channel if channel
6097 		 * exist.
6098 		 * if channel exist, then enforce 'num_queues' to be more than
6099 		 * max ever queues configured for channel.
6100 		 */
6101 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6102 		if (num_queues < max_ch_queues) {
6103 			dev_dbg(&pf->pdev->dev,
6104 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6105 				num_queues, max_ch_queues);
6106 			return -EINVAL;
6107 		}
6108 		*reconfig_rss = true;
6109 	}
6110 
6111 	return 0;
6112 }
6113 
6114 /**
6115  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6116  * @vsi: the VSI being setup
6117  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6118  *
6119  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6120  **/
6121 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6122 {
6123 	struct i40e_pf *pf = vsi->back;
6124 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6125 	struct i40e_hw *hw = &pf->hw;
6126 	int local_rss_size;
6127 	u8 *lut;
6128 	int ret;
6129 
6130 	if (!vsi->rss_size)
6131 		return -EINVAL;
6132 
6133 	if (rss_size > vsi->rss_size)
6134 		return -EINVAL;
6135 
6136 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6137 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6138 	if (!lut)
6139 		return -ENOMEM;
6140 
6141 	/* Ignoring user configured lut if there is one */
6142 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6143 
6144 	/* Use user configured hash key if there is one, otherwise
6145 	 * use default.
6146 	 */
6147 	if (vsi->rss_hkey_user)
6148 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6149 	else
6150 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6151 
6152 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6153 	if (ret) {
6154 		dev_info(&pf->pdev->dev,
6155 			 "Cannot set RSS lut, err %s aq_err %s\n",
6156 			 i40e_stat_str(hw, ret),
6157 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6158 		kfree(lut);
6159 		return ret;
6160 	}
6161 	kfree(lut);
6162 
6163 	/* Do the update w.r.t. storing rss_size */
6164 	if (!vsi->orig_rss_size)
6165 		vsi->orig_rss_size = vsi->rss_size;
6166 	vsi->current_rss_size = local_rss_size;
6167 
6168 	return ret;
6169 }
6170 
6171 /**
6172  * i40e_channel_setup_queue_map - Setup a channel queue map
6173  * @pf: ptr to PF device
6174  * @ctxt: VSI context structure
6175  * @ch: ptr to channel structure
6176  *
6177  * Setup queue map for a specific channel
6178  **/
6179 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6180 					 struct i40e_vsi_context *ctxt,
6181 					 struct i40e_channel *ch)
6182 {
6183 	u16 qcount, qmap, sections = 0;
6184 	u8 offset = 0;
6185 	int pow;
6186 
6187 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6188 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6189 
6190 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6191 	ch->num_queue_pairs = qcount;
6192 
6193 	/* find the next higher power-of-2 of num queue pairs */
6194 	pow = ilog2(qcount);
6195 	if (!is_power_of_2(qcount))
6196 		pow++;
6197 
6198 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6199 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6200 
6201 	/* Setup queue TC[0].qmap for given VSI context */
6202 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6203 
6204 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6205 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6206 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6207 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6208 }
6209 
6210 /**
6211  * i40e_add_channel - add a channel by adding VSI
6212  * @pf: ptr to PF device
6213  * @uplink_seid: underlying HW switching element (VEB) ID
6214  * @ch: ptr to channel structure
6215  *
6216  * Add a channel (VSI) using add_vsi and queue_map
6217  **/
6218 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6219 			    struct i40e_channel *ch)
6220 {
6221 	struct i40e_hw *hw = &pf->hw;
6222 	struct i40e_vsi_context ctxt;
6223 	u8 enabled_tc = 0x1; /* TC0 enabled */
6224 	int ret;
6225 
6226 	if (ch->type != I40E_VSI_VMDQ2) {
6227 		dev_info(&pf->pdev->dev,
6228 			 "add new vsi failed, ch->type %d\n", ch->type);
6229 		return -EINVAL;
6230 	}
6231 
6232 	memset(&ctxt, 0, sizeof(ctxt));
6233 	ctxt.pf_num = hw->pf_id;
6234 	ctxt.vf_num = 0;
6235 	ctxt.uplink_seid = uplink_seid;
6236 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6237 	if (ch->type == I40E_VSI_VMDQ2)
6238 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6239 
6240 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6241 		ctxt.info.valid_sections |=
6242 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6243 		ctxt.info.switch_id =
6244 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6245 	}
6246 
6247 	/* Set queue map for a given VSI context */
6248 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6249 
6250 	/* Now time to create VSI */
6251 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6252 	if (ret) {
6253 		dev_info(&pf->pdev->dev,
6254 			 "add new vsi failed, err %s aq_err %s\n",
6255 			 i40e_stat_str(&pf->hw, ret),
6256 			 i40e_aq_str(&pf->hw,
6257 				     pf->hw.aq.asq_last_status));
6258 		return -ENOENT;
6259 	}
6260 
6261 	/* Success, update channel, set enabled_tc only if the channel
6262 	 * is not a macvlan
6263 	 */
6264 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6265 	ch->seid = ctxt.seid;
6266 	ch->vsi_number = ctxt.vsi_number;
6267 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6268 
6269 	/* copy just the sections touched not the entire info
6270 	 * since not all sections are valid as returned by
6271 	 * update vsi params
6272 	 */
6273 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6274 	memcpy(&ch->info.queue_mapping,
6275 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6276 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6277 	       sizeof(ctxt.info.tc_mapping));
6278 
6279 	return 0;
6280 }
6281 
6282 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6283 				  u8 *bw_share)
6284 {
6285 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6286 	i40e_status ret;
6287 	int i;
6288 
6289 	memset(&bw_data, 0, sizeof(bw_data));
6290 	bw_data.tc_valid_bits = ch->enabled_tc;
6291 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6292 		bw_data.tc_bw_credits[i] = bw_share[i];
6293 
6294 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6295 				       &bw_data, NULL);
6296 	if (ret) {
6297 		dev_info(&vsi->back->pdev->dev,
6298 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6299 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6300 		return -EINVAL;
6301 	}
6302 
6303 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6304 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6305 
6306 	return 0;
6307 }
6308 
6309 /**
6310  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6311  * @pf: ptr to PF device
6312  * @vsi: the VSI being setup
6313  * @ch: ptr to channel structure
6314  *
6315  * Configure TX rings associated with channel (VSI) since queues are being
6316  * from parent VSI.
6317  **/
6318 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6319 				       struct i40e_vsi *vsi,
6320 				       struct i40e_channel *ch)
6321 {
6322 	i40e_status ret;
6323 	int i;
6324 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6325 
6326 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6327 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6328 		if (ch->enabled_tc & BIT(i))
6329 			bw_share[i] = 1;
6330 	}
6331 
6332 	/* configure BW for new VSI */
6333 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6334 	if (ret) {
6335 		dev_info(&vsi->back->pdev->dev,
6336 			 "Failed configuring TC map %d for channel (seid %u)\n",
6337 			 ch->enabled_tc, ch->seid);
6338 		return ret;
6339 	}
6340 
6341 	for (i = 0; i < ch->num_queue_pairs; i++) {
6342 		struct i40e_ring *tx_ring, *rx_ring;
6343 		u16 pf_q;
6344 
6345 		pf_q = ch->base_queue + i;
6346 
6347 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6348 		 * context
6349 		 */
6350 		tx_ring = vsi->tx_rings[pf_q];
6351 		tx_ring->ch = ch;
6352 
6353 		/* Get the RX ring ptr */
6354 		rx_ring = vsi->rx_rings[pf_q];
6355 		rx_ring->ch = ch;
6356 	}
6357 
6358 	return 0;
6359 }
6360 
6361 /**
6362  * i40e_setup_hw_channel - setup new channel
6363  * @pf: ptr to PF device
6364  * @vsi: the VSI being setup
6365  * @ch: ptr to channel structure
6366  * @uplink_seid: underlying HW switching element (VEB) ID
6367  * @type: type of channel to be created (VMDq2/VF)
6368  *
6369  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6370  * and configures TX rings accordingly
6371  **/
6372 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6373 					struct i40e_vsi *vsi,
6374 					struct i40e_channel *ch,
6375 					u16 uplink_seid, u8 type)
6376 {
6377 	int ret;
6378 
6379 	ch->initialized = false;
6380 	ch->base_queue = vsi->next_base_queue;
6381 	ch->type = type;
6382 
6383 	/* Proceed with creation of channel (VMDq2) VSI */
6384 	ret = i40e_add_channel(pf, uplink_seid, ch);
6385 	if (ret) {
6386 		dev_info(&pf->pdev->dev,
6387 			 "failed to add_channel using uplink_seid %u\n",
6388 			 uplink_seid);
6389 		return ret;
6390 	}
6391 
6392 	/* Mark the successful creation of channel */
6393 	ch->initialized = true;
6394 
6395 	/* Reconfigure TX queues using QTX_CTL register */
6396 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6397 	if (ret) {
6398 		dev_info(&pf->pdev->dev,
6399 			 "failed to configure TX rings for channel %u\n",
6400 			 ch->seid);
6401 		return ret;
6402 	}
6403 
6404 	/* update 'next_base_queue' */
6405 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6406 	dev_dbg(&pf->pdev->dev,
6407 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6408 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6409 		ch->num_queue_pairs,
6410 		vsi->next_base_queue);
6411 	return ret;
6412 }
6413 
6414 /**
6415  * i40e_setup_channel - setup new channel using uplink element
6416  * @pf: ptr to PF device
6417  * @vsi: pointer to the VSI to set up the channel within
6418  * @ch: ptr to channel structure
6419  *
6420  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6421  * and uplink switching element (uplink_seid)
6422  **/
6423 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6424 			       struct i40e_channel *ch)
6425 {
6426 	u8 vsi_type;
6427 	u16 seid;
6428 	int ret;
6429 
6430 	if (vsi->type == I40E_VSI_MAIN) {
6431 		vsi_type = I40E_VSI_VMDQ2;
6432 	} else {
6433 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6434 			vsi->type);
6435 		return false;
6436 	}
6437 
6438 	/* underlying switching element */
6439 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6440 
6441 	/* create channel (VSI), configure TX rings */
6442 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6443 	if (ret) {
6444 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6445 		return false;
6446 	}
6447 
6448 	return ch->initialized ? true : false;
6449 }
6450 
6451 /**
6452  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6453  * @vsi: ptr to VSI which has PF backing
6454  *
6455  * Sets up switch mode correctly if it needs to be changed and perform
6456  * what are allowed modes.
6457  **/
6458 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6459 {
6460 	u8 mode;
6461 	struct i40e_pf *pf = vsi->back;
6462 	struct i40e_hw *hw = &pf->hw;
6463 	int ret;
6464 
6465 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6466 	if (ret)
6467 		return -EINVAL;
6468 
6469 	if (hw->dev_caps.switch_mode) {
6470 		/* if switch mode is set, support mode2 (non-tunneled for
6471 		 * cloud filter) for now
6472 		 */
6473 		u32 switch_mode = hw->dev_caps.switch_mode &
6474 				  I40E_SWITCH_MODE_MASK;
6475 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6476 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6477 				return 0;
6478 			dev_err(&pf->pdev->dev,
6479 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6480 				hw->dev_caps.switch_mode);
6481 			return -EINVAL;
6482 		}
6483 	}
6484 
6485 	/* Set Bit 7 to be valid */
6486 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6487 
6488 	/* Set L4type for TCP support */
6489 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6490 
6491 	/* Set cloud filter mode */
6492 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6493 
6494 	/* Prep mode field for set_switch_config */
6495 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6496 					pf->last_sw_conf_valid_flags,
6497 					mode, NULL);
6498 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6499 		dev_err(&pf->pdev->dev,
6500 			"couldn't set switch config bits, err %s aq_err %s\n",
6501 			i40e_stat_str(hw, ret),
6502 			i40e_aq_str(hw,
6503 				    hw->aq.asq_last_status));
6504 
6505 	return ret;
6506 }
6507 
6508 /**
6509  * i40e_create_queue_channel - function to create channel
6510  * @vsi: VSI to be configured
6511  * @ch: ptr to channel (it contains channel specific params)
6512  *
6513  * This function creates channel (VSI) using num_queues specified by user,
6514  * reconfigs RSS if needed.
6515  **/
6516 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6517 			      struct i40e_channel *ch)
6518 {
6519 	struct i40e_pf *pf = vsi->back;
6520 	bool reconfig_rss;
6521 	int err;
6522 
6523 	if (!ch)
6524 		return -EINVAL;
6525 
6526 	if (!ch->num_queue_pairs) {
6527 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6528 			ch->num_queue_pairs);
6529 		return -EINVAL;
6530 	}
6531 
6532 	/* validate user requested num_queues for channel */
6533 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6534 				       &reconfig_rss);
6535 	if (err) {
6536 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6537 			 ch->num_queue_pairs);
6538 		return -EINVAL;
6539 	}
6540 
6541 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6542 	 * VSI to be added switch to VEB mode.
6543 	 */
6544 
6545 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6546 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6547 
6548 		if (vsi->type == I40E_VSI_MAIN) {
6549 			if (i40e_is_tc_mqprio_enabled(pf))
6550 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6551 			else
6552 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6553 		}
6554 		/* now onwards for main VSI, number of queues will be value
6555 		 * of TC0's queue count
6556 		 */
6557 	}
6558 
6559 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6560 	 * it should be more than num_queues
6561 	 */
6562 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6563 		dev_dbg(&pf->pdev->dev,
6564 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6565 			vsi->cnt_q_avail, ch->num_queue_pairs);
6566 		return -EINVAL;
6567 	}
6568 
6569 	/* reconfig_rss only if vsi type is MAIN_VSI */
6570 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6571 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6572 		if (err) {
6573 			dev_info(&pf->pdev->dev,
6574 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6575 				 ch->num_queue_pairs);
6576 			return -EINVAL;
6577 		}
6578 	}
6579 
6580 	if (!i40e_setup_channel(pf, vsi, ch)) {
6581 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6582 		return -EINVAL;
6583 	}
6584 
6585 	dev_info(&pf->pdev->dev,
6586 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6587 		 ch->seid, ch->num_queue_pairs);
6588 
6589 	/* configure VSI for BW limit */
6590 	if (ch->max_tx_rate) {
6591 		u64 credits = ch->max_tx_rate;
6592 
6593 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6594 			return -EINVAL;
6595 
6596 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6597 		dev_dbg(&pf->pdev->dev,
6598 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6599 			ch->max_tx_rate,
6600 			credits,
6601 			ch->seid);
6602 	}
6603 
6604 	/* in case of VF, this will be main SRIOV VSI */
6605 	ch->parent_vsi = vsi;
6606 
6607 	/* and update main_vsi's count for queue_available to use */
6608 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6609 
6610 	return 0;
6611 }
6612 
6613 /**
6614  * i40e_configure_queue_channels - Add queue channel for the given TCs
6615  * @vsi: VSI to be configured
6616  *
6617  * Configures queue channel mapping to the given TCs
6618  **/
6619 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6620 {
6621 	struct i40e_channel *ch;
6622 	u64 max_rate = 0;
6623 	int ret = 0, i;
6624 
6625 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6626 	vsi->tc_seid_map[0] = vsi->seid;
6627 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6628 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6629 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6630 			if (!ch) {
6631 				ret = -ENOMEM;
6632 				goto err_free;
6633 			}
6634 
6635 			INIT_LIST_HEAD(&ch->list);
6636 			ch->num_queue_pairs =
6637 				vsi->tc_config.tc_info[i].qcount;
6638 			ch->base_queue =
6639 				vsi->tc_config.tc_info[i].qoffset;
6640 
6641 			/* Bandwidth limit through tc interface is in bytes/s,
6642 			 * change to Mbit/s
6643 			 */
6644 			max_rate = vsi->mqprio_qopt.max_rate[i];
6645 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6646 			ch->max_tx_rate = max_rate;
6647 
6648 			list_add_tail(&ch->list, &vsi->ch_list);
6649 
6650 			ret = i40e_create_queue_channel(vsi, ch);
6651 			if (ret) {
6652 				dev_err(&vsi->back->pdev->dev,
6653 					"Failed creating queue channel with TC%d: queues %d\n",
6654 					i, ch->num_queue_pairs);
6655 				goto err_free;
6656 			}
6657 			vsi->tc_seid_map[i] = ch->seid;
6658 		}
6659 	}
6660 	return ret;
6661 
6662 err_free:
6663 	i40e_remove_queue_channels(vsi);
6664 	return ret;
6665 }
6666 
6667 /**
6668  * i40e_veb_config_tc - Configure TCs for given VEB
6669  * @veb: given VEB
6670  * @enabled_tc: TC bitmap
6671  *
6672  * Configures given TC bitmap for VEB (switching) element
6673  **/
6674 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6675 {
6676 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6677 	struct i40e_pf *pf = veb->pf;
6678 	int ret = 0;
6679 	int i;
6680 
6681 	/* No TCs or already enabled TCs just return */
6682 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6683 		return ret;
6684 
6685 	bw_data.tc_valid_bits = enabled_tc;
6686 	/* bw_data.absolute_credits is not set (relative) */
6687 
6688 	/* Enable ETS TCs with equal BW Share for now */
6689 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6690 		if (enabled_tc & BIT(i))
6691 			bw_data.tc_bw_share_credits[i] = 1;
6692 	}
6693 
6694 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6695 						   &bw_data, NULL);
6696 	if (ret) {
6697 		dev_info(&pf->pdev->dev,
6698 			 "VEB bw config failed, err %s aq_err %s\n",
6699 			 i40e_stat_str(&pf->hw, ret),
6700 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6701 		goto out;
6702 	}
6703 
6704 	/* Update the BW information */
6705 	ret = i40e_veb_get_bw_info(veb);
6706 	if (ret) {
6707 		dev_info(&pf->pdev->dev,
6708 			 "Failed getting veb bw config, err %s aq_err %s\n",
6709 			 i40e_stat_str(&pf->hw, ret),
6710 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6711 	}
6712 
6713 out:
6714 	return ret;
6715 }
6716 
6717 #ifdef CONFIG_I40E_DCB
6718 /**
6719  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6720  * @pf: PF struct
6721  *
6722  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6723  * the caller would've quiesce all the VSIs before calling
6724  * this function
6725  **/
6726 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6727 {
6728 	u8 tc_map = 0;
6729 	int ret;
6730 	u8 v;
6731 
6732 	/* Enable the TCs available on PF to all VEBs */
6733 	tc_map = i40e_pf_get_tc_map(pf);
6734 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6735 		return;
6736 
6737 	for (v = 0; v < I40E_MAX_VEB; v++) {
6738 		if (!pf->veb[v])
6739 			continue;
6740 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6741 		if (ret) {
6742 			dev_info(&pf->pdev->dev,
6743 				 "Failed configuring TC for VEB seid=%d\n",
6744 				 pf->veb[v]->seid);
6745 			/* Will try to configure as many components */
6746 		}
6747 	}
6748 
6749 	/* Update each VSI */
6750 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6751 		if (!pf->vsi[v])
6752 			continue;
6753 
6754 		/* - Enable all TCs for the LAN VSI
6755 		 * - For all others keep them at TC0 for now
6756 		 */
6757 		if (v == pf->lan_vsi)
6758 			tc_map = i40e_pf_get_tc_map(pf);
6759 		else
6760 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6761 
6762 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6763 		if (ret) {
6764 			dev_info(&pf->pdev->dev,
6765 				 "Failed configuring TC for VSI seid=%d\n",
6766 				 pf->vsi[v]->seid);
6767 			/* Will try to configure as many components */
6768 		} else {
6769 			/* Re-configure VSI vectors based on updated TC map */
6770 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6771 			if (pf->vsi[v]->netdev)
6772 				i40e_dcbnl_set_all(pf->vsi[v]);
6773 		}
6774 	}
6775 }
6776 
6777 /**
6778  * i40e_resume_port_tx - Resume port Tx
6779  * @pf: PF struct
6780  *
6781  * Resume a port's Tx and issue a PF reset in case of failure to
6782  * resume.
6783  **/
6784 static int i40e_resume_port_tx(struct i40e_pf *pf)
6785 {
6786 	struct i40e_hw *hw = &pf->hw;
6787 	int ret;
6788 
6789 	ret = i40e_aq_resume_port_tx(hw, NULL);
6790 	if (ret) {
6791 		dev_info(&pf->pdev->dev,
6792 			 "Resume Port Tx failed, err %s aq_err %s\n",
6793 			  i40e_stat_str(&pf->hw, ret),
6794 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6795 		/* Schedule PF reset to recover */
6796 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6797 		i40e_service_event_schedule(pf);
6798 	}
6799 
6800 	return ret;
6801 }
6802 
6803 /**
6804  * i40e_suspend_port_tx - Suspend port Tx
6805  * @pf: PF struct
6806  *
6807  * Suspend a port's Tx and issue a PF reset in case of failure.
6808  **/
6809 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6810 {
6811 	struct i40e_hw *hw = &pf->hw;
6812 	int ret;
6813 
6814 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6815 	if (ret) {
6816 		dev_info(&pf->pdev->dev,
6817 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6818 			 i40e_stat_str(&pf->hw, ret),
6819 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6820 		/* Schedule PF reset to recover */
6821 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6822 		i40e_service_event_schedule(pf);
6823 	}
6824 
6825 	return ret;
6826 }
6827 
6828 /**
6829  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6830  * @pf: PF being configured
6831  * @new_cfg: New DCBX configuration
6832  *
6833  * Program DCB settings into HW and reconfigure VEB/VSIs on
6834  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6835  **/
6836 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6837 				  struct i40e_dcbx_config *new_cfg)
6838 {
6839 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6840 	int ret;
6841 
6842 	/* Check if need reconfiguration */
6843 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6844 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6845 		return 0;
6846 	}
6847 
6848 	/* Config change disable all VSIs */
6849 	i40e_pf_quiesce_all_vsi(pf);
6850 
6851 	/* Copy the new config to the current config */
6852 	*old_cfg = *new_cfg;
6853 	old_cfg->etsrec = old_cfg->etscfg;
6854 	ret = i40e_set_dcb_config(&pf->hw);
6855 	if (ret) {
6856 		dev_info(&pf->pdev->dev,
6857 			 "Set DCB Config failed, err %s aq_err %s\n",
6858 			 i40e_stat_str(&pf->hw, ret),
6859 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6860 		goto out;
6861 	}
6862 
6863 	/* Changes in configuration update VEB/VSI */
6864 	i40e_dcb_reconfigure(pf);
6865 out:
6866 	/* In case of reset do not try to resume anything */
6867 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6868 		/* Re-start the VSIs if disabled */
6869 		ret = i40e_resume_port_tx(pf);
6870 		/* In case of error no point in resuming VSIs */
6871 		if (ret)
6872 			goto err;
6873 		i40e_pf_unquiesce_all_vsi(pf);
6874 	}
6875 err:
6876 	return ret;
6877 }
6878 
6879 /**
6880  * i40e_hw_dcb_config - Program new DCBX settings into HW
6881  * @pf: PF being configured
6882  * @new_cfg: New DCBX configuration
6883  *
6884  * Program DCB settings into HW and reconfigure VEB/VSIs on
6885  * given PF
6886  **/
6887 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6888 {
6889 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6890 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6891 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6892 	struct i40e_dcbx_config *old_cfg;
6893 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6894 	struct i40e_rx_pb_config pb_cfg;
6895 	struct i40e_hw *hw = &pf->hw;
6896 	u8 num_ports = hw->num_ports;
6897 	bool need_reconfig;
6898 	int ret = -EINVAL;
6899 	u8 lltc_map = 0;
6900 	u8 tc_map = 0;
6901 	u8 new_numtc;
6902 	u8 i;
6903 
6904 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6905 	/* Un-pack information to Program ETS HW via shared API
6906 	 * numtc, tcmap
6907 	 * LLTC map
6908 	 * ETS/NON-ETS arbiter mode
6909 	 * max exponent (credit refills)
6910 	 * Total number of ports
6911 	 * PFC priority bit-map
6912 	 * Priority Table
6913 	 * BW % per TC
6914 	 * Arbiter mode between UPs sharing same TC
6915 	 * TSA table (ETS or non-ETS)
6916 	 * EEE enabled or not
6917 	 * MFS TC table
6918 	 */
6919 
6920 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6921 
6922 	memset(&ets_data, 0, sizeof(ets_data));
6923 	for (i = 0; i < new_numtc; i++) {
6924 		tc_map |= BIT(i);
6925 		switch (new_cfg->etscfg.tsatable[i]) {
6926 		case I40E_IEEE_TSA_ETS:
6927 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6928 			ets_data.tc_bw_share_credits[i] =
6929 					new_cfg->etscfg.tcbwtable[i];
6930 			break;
6931 		case I40E_IEEE_TSA_STRICT:
6932 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6933 			lltc_map |= BIT(i);
6934 			ets_data.tc_bw_share_credits[i] =
6935 					I40E_DCB_STRICT_PRIO_CREDITS;
6936 			break;
6937 		default:
6938 			/* Invalid TSA type */
6939 			need_reconfig = false;
6940 			goto out;
6941 		}
6942 	}
6943 
6944 	old_cfg = &hw->local_dcbx_config;
6945 	/* Check if need reconfiguration */
6946 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6947 
6948 	/* If needed, enable/disable frame tagging, disable all VSIs
6949 	 * and suspend port tx
6950 	 */
6951 	if (need_reconfig) {
6952 		/* Enable DCB tagging only when more than one TC */
6953 		if (new_numtc > 1)
6954 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6955 		else
6956 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6957 
6958 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6959 		/* Reconfiguration needed quiesce all VSIs */
6960 		i40e_pf_quiesce_all_vsi(pf);
6961 		ret = i40e_suspend_port_tx(pf);
6962 		if (ret)
6963 			goto err;
6964 	}
6965 
6966 	/* Configure Port ETS Tx Scheduler */
6967 	ets_data.tc_valid_bits = tc_map;
6968 	ets_data.tc_strict_priority_flags = lltc_map;
6969 	ret = i40e_aq_config_switch_comp_ets
6970 		(hw, pf->mac_seid, &ets_data,
6971 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6972 	if (ret) {
6973 		dev_info(&pf->pdev->dev,
6974 			 "Modify Port ETS failed, err %s aq_err %s\n",
6975 			 i40e_stat_str(&pf->hw, ret),
6976 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6977 		goto out;
6978 	}
6979 
6980 	/* Configure Rx ETS HW */
6981 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6982 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6983 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6984 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6985 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6986 				   lltc_map);
6987 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6988 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6989 				     prio_type);
6990 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6991 			       new_cfg->etscfg.prioritytable);
6992 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6993 
6994 	/* Configure Rx Packet Buffers in HW */
6995 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6996 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6997 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6998 	}
6999 
7000 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7001 					 false, new_cfg->pfc.pfcenable,
7002 					 mfs_tc, &pb_cfg);
7003 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7004 
7005 	/* Update the local Rx Packet buffer config */
7006 	pf->pb_cfg = pb_cfg;
7007 
7008 	/* Inform the FW about changes to DCB configuration */
7009 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7010 	if (ret) {
7011 		dev_info(&pf->pdev->dev,
7012 			 "DCB Updated failed, err %s aq_err %s\n",
7013 			 i40e_stat_str(&pf->hw, ret),
7014 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7015 		goto out;
7016 	}
7017 
7018 	/* Update the port DCBx configuration */
7019 	*old_cfg = *new_cfg;
7020 
7021 	/* Changes in configuration update VEB/VSI */
7022 	i40e_dcb_reconfigure(pf);
7023 out:
7024 	/* Re-start the VSIs if disabled */
7025 	if (need_reconfig) {
7026 		ret = i40e_resume_port_tx(pf);
7027 
7028 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7029 		/* In case of error no point in resuming VSIs */
7030 		if (ret)
7031 			goto err;
7032 
7033 		/* Wait for the PF's queues to be disabled */
7034 		ret = i40e_pf_wait_queues_disabled(pf);
7035 		if (ret) {
7036 			/* Schedule PF reset to recover */
7037 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7038 			i40e_service_event_schedule(pf);
7039 			goto err;
7040 		} else {
7041 			i40e_pf_unquiesce_all_vsi(pf);
7042 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7043 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7044 		}
7045 		/* registers are set, lets apply */
7046 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7047 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7048 	}
7049 
7050 err:
7051 	return ret;
7052 }
7053 
7054 /**
7055  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7056  * @pf: PF being queried
7057  *
7058  * Set default DCB configuration in case DCB is to be done in SW.
7059  **/
7060 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7061 {
7062 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7063 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7064 	struct i40e_hw *hw = &pf->hw;
7065 	int err;
7066 
7067 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7068 		/* Update the local cached instance with TC0 ETS */
7069 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7070 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7071 		pf->tmp_cfg.etscfg.maxtcs = 0;
7072 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7073 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7074 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7075 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7076 		/* FW needs one App to configure HW */
7077 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7078 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7079 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7080 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7081 
7082 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7083 	}
7084 
7085 	memset(&ets_data, 0, sizeof(ets_data));
7086 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7087 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7088 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7089 
7090 	/* Enable ETS on the Physical port */
7091 	err = i40e_aq_config_switch_comp_ets
7092 		(hw, pf->mac_seid, &ets_data,
7093 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7094 	if (err) {
7095 		dev_info(&pf->pdev->dev,
7096 			 "Enable Port ETS failed, err %s aq_err %s\n",
7097 			 i40e_stat_str(&pf->hw, err),
7098 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7099 		err = -ENOENT;
7100 		goto out;
7101 	}
7102 
7103 	/* Update the local cached instance with TC0 ETS */
7104 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7105 	dcb_cfg->etscfg.cbs = 0;
7106 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7107 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7108 
7109 out:
7110 	return err;
7111 }
7112 
7113 /**
7114  * i40e_init_pf_dcb - Initialize DCB configuration
7115  * @pf: PF being configured
7116  *
7117  * Query the current DCB configuration and cache it
7118  * in the hardware structure
7119  **/
7120 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7121 {
7122 	struct i40e_hw *hw = &pf->hw;
7123 	int err;
7124 
7125 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7126 	 * Also do not enable DCBx if FW LLDP agent is disabled
7127 	 */
7128 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7129 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7130 		err = I40E_NOT_SUPPORTED;
7131 		goto out;
7132 	}
7133 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7134 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7135 		err = i40e_dcb_sw_default_config(pf);
7136 		if (err) {
7137 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7138 			goto out;
7139 		}
7140 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7141 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7142 			       DCB_CAP_DCBX_VER_IEEE;
7143 		/* at init capable but disabled */
7144 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7145 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7146 		goto out;
7147 	}
7148 	err = i40e_init_dcb(hw, true);
7149 	if (!err) {
7150 		/* Device/Function is not DCBX capable */
7151 		if ((!hw->func_caps.dcb) ||
7152 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7153 			dev_info(&pf->pdev->dev,
7154 				 "DCBX offload is not supported or is disabled for this PF.\n");
7155 		} else {
7156 			/* When status is not DISABLED then DCBX in FW */
7157 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7158 				       DCB_CAP_DCBX_VER_IEEE;
7159 
7160 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7161 			/* Enable DCB tagging only when more than one TC
7162 			 * or explicitly disable if only one TC
7163 			 */
7164 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7165 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7166 			else
7167 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7168 			dev_dbg(&pf->pdev->dev,
7169 				"DCBX offload is supported for this PF.\n");
7170 		}
7171 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7172 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7173 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7174 	} else {
7175 		dev_info(&pf->pdev->dev,
7176 			 "Query for DCB configuration failed, err %s aq_err %s\n",
7177 			 i40e_stat_str(&pf->hw, err),
7178 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7179 	}
7180 
7181 out:
7182 	return err;
7183 }
7184 #endif /* CONFIG_I40E_DCB */
7185 
7186 /**
7187  * i40e_print_link_message - print link up or down
7188  * @vsi: the VSI for which link needs a message
7189  * @isup: true of link is up, false otherwise
7190  */
7191 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7192 {
7193 	enum i40e_aq_link_speed new_speed;
7194 	struct i40e_pf *pf = vsi->back;
7195 	char *speed = "Unknown";
7196 	char *fc = "Unknown";
7197 	char *fec = "";
7198 	char *req_fec = "";
7199 	char *an = "";
7200 
7201 	if (isup)
7202 		new_speed = pf->hw.phy.link_info.link_speed;
7203 	else
7204 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7205 
7206 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7207 		return;
7208 	vsi->current_isup = isup;
7209 	vsi->current_speed = new_speed;
7210 	if (!isup) {
7211 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7212 		return;
7213 	}
7214 
7215 	/* Warn user if link speed on NPAR enabled partition is not at
7216 	 * least 10GB
7217 	 */
7218 	if (pf->hw.func_caps.npar_enable &&
7219 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7220 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7221 		netdev_warn(vsi->netdev,
7222 			    "The partition detected link speed that is less than 10Gbps\n");
7223 
7224 	switch (pf->hw.phy.link_info.link_speed) {
7225 	case I40E_LINK_SPEED_40GB:
7226 		speed = "40 G";
7227 		break;
7228 	case I40E_LINK_SPEED_20GB:
7229 		speed = "20 G";
7230 		break;
7231 	case I40E_LINK_SPEED_25GB:
7232 		speed = "25 G";
7233 		break;
7234 	case I40E_LINK_SPEED_10GB:
7235 		speed = "10 G";
7236 		break;
7237 	case I40E_LINK_SPEED_5GB:
7238 		speed = "5 G";
7239 		break;
7240 	case I40E_LINK_SPEED_2_5GB:
7241 		speed = "2.5 G";
7242 		break;
7243 	case I40E_LINK_SPEED_1GB:
7244 		speed = "1000 M";
7245 		break;
7246 	case I40E_LINK_SPEED_100MB:
7247 		speed = "100 M";
7248 		break;
7249 	default:
7250 		break;
7251 	}
7252 
7253 	switch (pf->hw.fc.current_mode) {
7254 	case I40E_FC_FULL:
7255 		fc = "RX/TX";
7256 		break;
7257 	case I40E_FC_TX_PAUSE:
7258 		fc = "TX";
7259 		break;
7260 	case I40E_FC_RX_PAUSE:
7261 		fc = "RX";
7262 		break;
7263 	default:
7264 		fc = "None";
7265 		break;
7266 	}
7267 
7268 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7269 		req_fec = "None";
7270 		fec = "None";
7271 		an = "False";
7272 
7273 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7274 			an = "True";
7275 
7276 		if (pf->hw.phy.link_info.fec_info &
7277 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7278 			fec = "CL74 FC-FEC/BASE-R";
7279 		else if (pf->hw.phy.link_info.fec_info &
7280 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7281 			fec = "CL108 RS-FEC";
7282 
7283 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7284 		 * both RS and FC are requested
7285 		 */
7286 		if (vsi->back->hw.phy.link_info.req_fec_info &
7287 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7288 			if (vsi->back->hw.phy.link_info.req_fec_info &
7289 			    I40E_AQ_REQUEST_FEC_RS)
7290 				req_fec = "CL108 RS-FEC";
7291 			else
7292 				req_fec = "CL74 FC-FEC/BASE-R";
7293 		}
7294 		netdev_info(vsi->netdev,
7295 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7296 			    speed, req_fec, fec, an, fc);
7297 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7298 		req_fec = "None";
7299 		fec = "None";
7300 		an = "False";
7301 
7302 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7303 			an = "True";
7304 
7305 		if (pf->hw.phy.link_info.fec_info &
7306 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7307 			fec = "CL74 FC-FEC/BASE-R";
7308 
7309 		if (pf->hw.phy.link_info.req_fec_info &
7310 		    I40E_AQ_REQUEST_FEC_KR)
7311 			req_fec = "CL74 FC-FEC/BASE-R";
7312 
7313 		netdev_info(vsi->netdev,
7314 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7315 			    speed, req_fec, fec, an, fc);
7316 	} else {
7317 		netdev_info(vsi->netdev,
7318 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7319 			    speed, fc);
7320 	}
7321 
7322 }
7323 
7324 /**
7325  * i40e_up_complete - Finish the last steps of bringing up a connection
7326  * @vsi: the VSI being configured
7327  **/
7328 static int i40e_up_complete(struct i40e_vsi *vsi)
7329 {
7330 	struct i40e_pf *pf = vsi->back;
7331 	int err;
7332 
7333 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7334 		i40e_vsi_configure_msix(vsi);
7335 	else
7336 		i40e_configure_msi_and_legacy(vsi);
7337 
7338 	/* start rings */
7339 	err = i40e_vsi_start_rings(vsi);
7340 	if (err)
7341 		return err;
7342 
7343 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7344 	i40e_napi_enable_all(vsi);
7345 	i40e_vsi_enable_irq(vsi);
7346 
7347 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7348 	    (vsi->netdev)) {
7349 		i40e_print_link_message(vsi, true);
7350 		netif_tx_start_all_queues(vsi->netdev);
7351 		netif_carrier_on(vsi->netdev);
7352 	}
7353 
7354 	/* replay FDIR SB filters */
7355 	if (vsi->type == I40E_VSI_FDIR) {
7356 		/* reset fd counters */
7357 		pf->fd_add_err = 0;
7358 		pf->fd_atr_cnt = 0;
7359 		i40e_fdir_filter_restore(vsi);
7360 	}
7361 
7362 	/* On the next run of the service_task, notify any clients of the new
7363 	 * opened netdev
7364 	 */
7365 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7366 	i40e_service_event_schedule(pf);
7367 
7368 	return 0;
7369 }
7370 
7371 /**
7372  * i40e_vsi_reinit_locked - Reset the VSI
7373  * @vsi: the VSI being configured
7374  *
7375  * Rebuild the ring structs after some configuration
7376  * has changed, e.g. MTU size.
7377  **/
7378 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7379 {
7380 	struct i40e_pf *pf = vsi->back;
7381 
7382 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7383 		usleep_range(1000, 2000);
7384 	i40e_down(vsi);
7385 
7386 	i40e_up(vsi);
7387 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7388 }
7389 
7390 /**
7391  * i40e_force_link_state - Force the link status
7392  * @pf: board private structure
7393  * @is_up: whether the link state should be forced up or down
7394  **/
7395 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7396 {
7397 	struct i40e_aq_get_phy_abilities_resp abilities;
7398 	struct i40e_aq_set_phy_config config = {0};
7399 	bool non_zero_phy_type = is_up;
7400 	struct i40e_hw *hw = &pf->hw;
7401 	i40e_status err;
7402 	u64 mask;
7403 	u8 speed;
7404 
7405 	/* Card might've been put in an unstable state by other drivers
7406 	 * and applications, which causes incorrect speed values being
7407 	 * set on startup. In order to clear speed registers, we call
7408 	 * get_phy_capabilities twice, once to get initial state of
7409 	 * available speeds, and once to get current PHY config.
7410 	 */
7411 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7412 					   NULL);
7413 	if (err) {
7414 		dev_err(&pf->pdev->dev,
7415 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7416 			i40e_stat_str(hw, err),
7417 			i40e_aq_str(hw, hw->aq.asq_last_status));
7418 		return err;
7419 	}
7420 	speed = abilities.link_speed;
7421 
7422 	/* Get the current phy config */
7423 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7424 					   NULL);
7425 	if (err) {
7426 		dev_err(&pf->pdev->dev,
7427 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7428 			i40e_stat_str(hw, err),
7429 			i40e_aq_str(hw, hw->aq.asq_last_status));
7430 		return err;
7431 	}
7432 
7433 	/* If link needs to go up, but was not forced to go down,
7434 	 * and its speed values are OK, no need for a flap
7435 	 * if non_zero_phy_type was set, still need to force up
7436 	 */
7437 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7438 		non_zero_phy_type = true;
7439 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7440 		return I40E_SUCCESS;
7441 
7442 	/* To force link we need to set bits for all supported PHY types,
7443 	 * but there are now more than 32, so we need to split the bitmap
7444 	 * across two fields.
7445 	 */
7446 	mask = I40E_PHY_TYPES_BITMASK;
7447 	config.phy_type =
7448 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7449 	config.phy_type_ext =
7450 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7451 	/* Copy the old settings, except of phy_type */
7452 	config.abilities = abilities.abilities;
7453 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7454 		if (is_up)
7455 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7456 		else
7457 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7458 	}
7459 	if (abilities.link_speed != 0)
7460 		config.link_speed = abilities.link_speed;
7461 	else
7462 		config.link_speed = speed;
7463 	config.eee_capability = abilities.eee_capability;
7464 	config.eeer = abilities.eeer_val;
7465 	config.low_power_ctrl = abilities.d3_lpan;
7466 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7467 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7468 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7469 
7470 	if (err) {
7471 		dev_err(&pf->pdev->dev,
7472 			"set phy config ret =  %s last_status =  %s\n",
7473 			i40e_stat_str(&pf->hw, err),
7474 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7475 		return err;
7476 	}
7477 
7478 	/* Update the link info */
7479 	err = i40e_update_link_info(hw);
7480 	if (err) {
7481 		/* Wait a little bit (on 40G cards it sometimes takes a really
7482 		 * long time for link to come back from the atomic reset)
7483 		 * and try once more
7484 		 */
7485 		msleep(1000);
7486 		i40e_update_link_info(hw);
7487 	}
7488 
7489 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7490 
7491 	return I40E_SUCCESS;
7492 }
7493 
7494 /**
7495  * i40e_up - Bring the connection back up after being down
7496  * @vsi: the VSI being configured
7497  **/
7498 int i40e_up(struct i40e_vsi *vsi)
7499 {
7500 	int err;
7501 
7502 	if (vsi->type == I40E_VSI_MAIN &&
7503 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7504 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7505 		i40e_force_link_state(vsi->back, true);
7506 
7507 	err = i40e_vsi_configure(vsi);
7508 	if (!err)
7509 		err = i40e_up_complete(vsi);
7510 
7511 	return err;
7512 }
7513 
7514 /**
7515  * i40e_down - Shutdown the connection processing
7516  * @vsi: the VSI being stopped
7517  **/
7518 void i40e_down(struct i40e_vsi *vsi)
7519 {
7520 	int i;
7521 
7522 	/* It is assumed that the caller of this function
7523 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7524 	 */
7525 	if (vsi->netdev) {
7526 		netif_carrier_off(vsi->netdev);
7527 		netif_tx_disable(vsi->netdev);
7528 	}
7529 	i40e_vsi_disable_irq(vsi);
7530 	i40e_vsi_stop_rings(vsi);
7531 	if (vsi->type == I40E_VSI_MAIN &&
7532 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7533 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7534 		i40e_force_link_state(vsi->back, false);
7535 	i40e_napi_disable_all(vsi);
7536 
7537 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7538 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7539 		if (i40e_enabled_xdp_vsi(vsi)) {
7540 			/* Make sure that in-progress ndo_xdp_xmit and
7541 			 * ndo_xsk_wakeup calls are completed.
7542 			 */
7543 			synchronize_rcu();
7544 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7545 		}
7546 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7547 	}
7548 
7549 }
7550 
7551 /**
7552  * i40e_validate_mqprio_qopt- validate queue mapping info
7553  * @vsi: the VSI being configured
7554  * @mqprio_qopt: queue parametrs
7555  **/
7556 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7557 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7558 {
7559 	u64 sum_max_rate = 0;
7560 	u64 max_rate = 0;
7561 	int i;
7562 
7563 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7564 	    mqprio_qopt->qopt.num_tc < 1 ||
7565 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7566 		return -EINVAL;
7567 	for (i = 0; ; i++) {
7568 		if (!mqprio_qopt->qopt.count[i])
7569 			return -EINVAL;
7570 		if (mqprio_qopt->min_rate[i]) {
7571 			dev_err(&vsi->back->pdev->dev,
7572 				"Invalid min tx rate (greater than 0) specified\n");
7573 			return -EINVAL;
7574 		}
7575 		max_rate = mqprio_qopt->max_rate[i];
7576 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7577 		sum_max_rate += max_rate;
7578 
7579 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7580 			break;
7581 		if (mqprio_qopt->qopt.offset[i + 1] !=
7582 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7583 			return -EINVAL;
7584 	}
7585 	if (vsi->num_queue_pairs <
7586 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7587 		dev_err(&vsi->back->pdev->dev,
7588 			"Failed to create traffic channel, insufficient number of queues.\n");
7589 		return -EINVAL;
7590 	}
7591 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7592 		dev_err(&vsi->back->pdev->dev,
7593 			"Invalid max tx rate specified\n");
7594 		return -EINVAL;
7595 	}
7596 	return 0;
7597 }
7598 
7599 /**
7600  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7601  * @vsi: the VSI being configured
7602  **/
7603 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7604 {
7605 	u16 qcount;
7606 	int i;
7607 
7608 	/* Only TC0 is enabled */
7609 	vsi->tc_config.numtc = 1;
7610 	vsi->tc_config.enabled_tc = 1;
7611 	qcount = min_t(int, vsi->alloc_queue_pairs,
7612 		       i40e_pf_get_max_q_per_tc(vsi->back));
7613 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7614 		/* For the TC that is not enabled set the offset to default
7615 		 * queue and allocate one queue for the given TC.
7616 		 */
7617 		vsi->tc_config.tc_info[i].qoffset = 0;
7618 		if (i == 0)
7619 			vsi->tc_config.tc_info[i].qcount = qcount;
7620 		else
7621 			vsi->tc_config.tc_info[i].qcount = 1;
7622 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7623 	}
7624 }
7625 
7626 /**
7627  * i40e_del_macvlan_filter
7628  * @hw: pointer to the HW structure
7629  * @seid: seid of the channel VSI
7630  * @macaddr: the mac address to apply as a filter
7631  * @aq_err: store the admin Q error
7632  *
7633  * This function deletes a mac filter on the channel VSI which serves as the
7634  * macvlan. Returns 0 on success.
7635  **/
7636 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7637 					   const u8 *macaddr, int *aq_err)
7638 {
7639 	struct i40e_aqc_remove_macvlan_element_data element;
7640 	i40e_status status;
7641 
7642 	memset(&element, 0, sizeof(element));
7643 	ether_addr_copy(element.mac_addr, macaddr);
7644 	element.vlan_tag = 0;
7645 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7646 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7647 	*aq_err = hw->aq.asq_last_status;
7648 
7649 	return status;
7650 }
7651 
7652 /**
7653  * i40e_add_macvlan_filter
7654  * @hw: pointer to the HW structure
7655  * @seid: seid of the channel VSI
7656  * @macaddr: the mac address to apply as a filter
7657  * @aq_err: store the admin Q error
7658  *
7659  * This function adds a mac filter on the channel VSI which serves as the
7660  * macvlan. Returns 0 on success.
7661  **/
7662 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7663 					   const u8 *macaddr, int *aq_err)
7664 {
7665 	struct i40e_aqc_add_macvlan_element_data element;
7666 	i40e_status status;
7667 	u16 cmd_flags = 0;
7668 
7669 	ether_addr_copy(element.mac_addr, macaddr);
7670 	element.vlan_tag = 0;
7671 	element.queue_number = 0;
7672 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7673 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7674 	element.flags = cpu_to_le16(cmd_flags);
7675 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7676 	*aq_err = hw->aq.asq_last_status;
7677 
7678 	return status;
7679 }
7680 
7681 /**
7682  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7683  * @vsi: the VSI we want to access
7684  * @ch: the channel we want to access
7685  */
7686 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7687 {
7688 	struct i40e_ring *tx_ring, *rx_ring;
7689 	u16 pf_q;
7690 	int i;
7691 
7692 	for (i = 0; i < ch->num_queue_pairs; i++) {
7693 		pf_q = ch->base_queue + i;
7694 		tx_ring = vsi->tx_rings[pf_q];
7695 		tx_ring->ch = NULL;
7696 		rx_ring = vsi->rx_rings[pf_q];
7697 		rx_ring->ch = NULL;
7698 	}
7699 }
7700 
7701 /**
7702  * i40e_free_macvlan_channels
7703  * @vsi: the VSI we want to access
7704  *
7705  * This function frees the Qs of the channel VSI from
7706  * the stack and also deletes the channel VSIs which
7707  * serve as macvlans.
7708  */
7709 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7710 {
7711 	struct i40e_channel *ch, *ch_tmp;
7712 	int ret;
7713 
7714 	if (list_empty(&vsi->macvlan_list))
7715 		return;
7716 
7717 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7718 		struct i40e_vsi *parent_vsi;
7719 
7720 		if (i40e_is_channel_macvlan(ch)) {
7721 			i40e_reset_ch_rings(vsi, ch);
7722 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7723 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7724 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7725 			kfree(ch->fwd);
7726 			ch->fwd = NULL;
7727 		}
7728 
7729 		list_del(&ch->list);
7730 		parent_vsi = ch->parent_vsi;
7731 		if (!parent_vsi || !ch->initialized) {
7732 			kfree(ch);
7733 			continue;
7734 		}
7735 
7736 		/* remove the VSI */
7737 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7738 					     NULL);
7739 		if (ret)
7740 			dev_err(&vsi->back->pdev->dev,
7741 				"unable to remove channel (%d) for parent VSI(%d)\n",
7742 				ch->seid, parent_vsi->seid);
7743 		kfree(ch);
7744 	}
7745 	vsi->macvlan_cnt = 0;
7746 }
7747 
7748 /**
7749  * i40e_fwd_ring_up - bring the macvlan device up
7750  * @vsi: the VSI we want to access
7751  * @vdev: macvlan netdevice
7752  * @fwd: the private fwd structure
7753  */
7754 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7755 			    struct i40e_fwd_adapter *fwd)
7756 {
7757 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7758 	int ret = 0, num_tc = 1,  i, aq_err;
7759 	struct i40e_pf *pf = vsi->back;
7760 	struct i40e_hw *hw = &pf->hw;
7761 
7762 	/* Go through the list and find an available channel */
7763 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7764 		if (!i40e_is_channel_macvlan(iter)) {
7765 			iter->fwd = fwd;
7766 			/* record configuration for macvlan interface in vdev */
7767 			for (i = 0; i < num_tc; i++)
7768 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7769 							     i,
7770 							     iter->num_queue_pairs,
7771 							     iter->base_queue);
7772 			for (i = 0; i < iter->num_queue_pairs; i++) {
7773 				struct i40e_ring *tx_ring, *rx_ring;
7774 				u16 pf_q;
7775 
7776 				pf_q = iter->base_queue + i;
7777 
7778 				/* Get to TX ring ptr */
7779 				tx_ring = vsi->tx_rings[pf_q];
7780 				tx_ring->ch = iter;
7781 
7782 				/* Get the RX ring ptr */
7783 				rx_ring = vsi->rx_rings[pf_q];
7784 				rx_ring->ch = iter;
7785 			}
7786 			ch = iter;
7787 			break;
7788 		}
7789 	}
7790 
7791 	if (!ch)
7792 		return -EINVAL;
7793 
7794 	/* Guarantee all rings are updated before we update the
7795 	 * MAC address filter.
7796 	 */
7797 	wmb();
7798 
7799 	/* Add a mac filter */
7800 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7801 	if (ret) {
7802 		/* if we cannot add the MAC rule then disable the offload */
7803 		macvlan_release_l2fw_offload(vdev);
7804 		for (i = 0; i < ch->num_queue_pairs; i++) {
7805 			struct i40e_ring *rx_ring;
7806 			u16 pf_q;
7807 
7808 			pf_q = ch->base_queue + i;
7809 			rx_ring = vsi->rx_rings[pf_q];
7810 			rx_ring->netdev = NULL;
7811 		}
7812 		dev_info(&pf->pdev->dev,
7813 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7814 			  i40e_stat_str(hw, ret),
7815 			  i40e_aq_str(hw, aq_err));
7816 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7817 	}
7818 
7819 	return ret;
7820 }
7821 
7822 /**
7823  * i40e_setup_macvlans - create the channels which will be macvlans
7824  * @vsi: the VSI we want to access
7825  * @macvlan_cnt: no. of macvlans to be setup
7826  * @qcnt: no. of Qs per macvlan
7827  * @vdev: macvlan netdevice
7828  */
7829 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7830 			       struct net_device *vdev)
7831 {
7832 	struct i40e_pf *pf = vsi->back;
7833 	struct i40e_hw *hw = &pf->hw;
7834 	struct i40e_vsi_context ctxt;
7835 	u16 sections, qmap, num_qps;
7836 	struct i40e_channel *ch;
7837 	int i, pow, ret = 0;
7838 	u8 offset = 0;
7839 
7840 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7841 		return -EINVAL;
7842 
7843 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7844 
7845 	/* find the next higher power-of-2 of num queue pairs */
7846 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7847 
7848 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7849 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7850 
7851 	/* Setup context bits for the main VSI */
7852 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7853 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7854 	memset(&ctxt, 0, sizeof(ctxt));
7855 	ctxt.seid = vsi->seid;
7856 	ctxt.pf_num = vsi->back->hw.pf_id;
7857 	ctxt.vf_num = 0;
7858 	ctxt.uplink_seid = vsi->uplink_seid;
7859 	ctxt.info = vsi->info;
7860 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7861 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7862 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7863 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7864 
7865 	/* Reconfigure RSS for main VSI with new max queue count */
7866 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7867 	ret = i40e_vsi_config_rss(vsi);
7868 	if (ret) {
7869 		dev_info(&pf->pdev->dev,
7870 			 "Failed to reconfig RSS for num_queues (%u)\n",
7871 			 vsi->rss_size);
7872 		return ret;
7873 	}
7874 	vsi->reconfig_rss = true;
7875 	dev_dbg(&vsi->back->pdev->dev,
7876 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7877 	vsi->next_base_queue = num_qps;
7878 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7879 
7880 	/* Update the VSI after updating the VSI queue-mapping
7881 	 * information
7882 	 */
7883 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7884 	if (ret) {
7885 		dev_info(&pf->pdev->dev,
7886 			 "Update vsi tc config failed, err %s aq_err %s\n",
7887 			 i40e_stat_str(hw, ret),
7888 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7889 		return ret;
7890 	}
7891 	/* update the local VSI info with updated queue map */
7892 	i40e_vsi_update_queue_map(vsi, &ctxt);
7893 	vsi->info.valid_sections = 0;
7894 
7895 	/* Create channels for macvlans */
7896 	INIT_LIST_HEAD(&vsi->macvlan_list);
7897 	for (i = 0; i < macvlan_cnt; i++) {
7898 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7899 		if (!ch) {
7900 			ret = -ENOMEM;
7901 			goto err_free;
7902 		}
7903 		INIT_LIST_HEAD(&ch->list);
7904 		ch->num_queue_pairs = qcnt;
7905 		if (!i40e_setup_channel(pf, vsi, ch)) {
7906 			ret = -EINVAL;
7907 			kfree(ch);
7908 			goto err_free;
7909 		}
7910 		ch->parent_vsi = vsi;
7911 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7912 		vsi->macvlan_cnt++;
7913 		list_add_tail(&ch->list, &vsi->macvlan_list);
7914 	}
7915 
7916 	return ret;
7917 
7918 err_free:
7919 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7920 	i40e_free_macvlan_channels(vsi);
7921 
7922 	return ret;
7923 }
7924 
7925 /**
7926  * i40e_fwd_add - configure macvlans
7927  * @netdev: net device to configure
7928  * @vdev: macvlan netdevice
7929  **/
7930 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7931 {
7932 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7933 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7934 	struct i40e_vsi *vsi = np->vsi;
7935 	struct i40e_pf *pf = vsi->back;
7936 	struct i40e_fwd_adapter *fwd;
7937 	int avail_macvlan, ret;
7938 
7939 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7940 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7941 		return ERR_PTR(-EINVAL);
7942 	}
7943 	if (i40e_is_tc_mqprio_enabled(pf)) {
7944 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7945 		return ERR_PTR(-EINVAL);
7946 	}
7947 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7948 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7949 		return ERR_PTR(-EINVAL);
7950 	}
7951 
7952 	/* The macvlan device has to be a single Q device so that the
7953 	 * tc_to_txq field can be reused to pick the tx queue.
7954 	 */
7955 	if (netif_is_multiqueue(vdev))
7956 		return ERR_PTR(-ERANGE);
7957 
7958 	if (!vsi->macvlan_cnt) {
7959 		/* reserve bit 0 for the pf device */
7960 		set_bit(0, vsi->fwd_bitmask);
7961 
7962 		/* Try to reserve as many queues as possible for macvlans. First
7963 		 * reserve 3/4th of max vectors, then half, then quarter and
7964 		 * calculate Qs per macvlan as you go
7965 		 */
7966 		vectors = pf->num_lan_msix;
7967 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7968 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7969 			q_per_macvlan = 4;
7970 			macvlan_cnt = (vectors - 32) / 4;
7971 		} else if (vectors <= 64 && vectors > 32) {
7972 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7973 			q_per_macvlan = 2;
7974 			macvlan_cnt = (vectors - 16) / 2;
7975 		} else if (vectors <= 32 && vectors > 16) {
7976 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7977 			q_per_macvlan = 1;
7978 			macvlan_cnt = vectors - 16;
7979 		} else if (vectors <= 16 && vectors > 8) {
7980 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7981 			q_per_macvlan = 1;
7982 			macvlan_cnt = vectors - 8;
7983 		} else {
7984 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7985 			q_per_macvlan = 1;
7986 			macvlan_cnt = vectors - 1;
7987 		}
7988 
7989 		if (macvlan_cnt == 0)
7990 			return ERR_PTR(-EBUSY);
7991 
7992 		/* Quiesce VSI queues */
7993 		i40e_quiesce_vsi(vsi);
7994 
7995 		/* sets up the macvlans but does not "enable" them */
7996 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7997 					  vdev);
7998 		if (ret)
7999 			return ERR_PTR(ret);
8000 
8001 		/* Unquiesce VSI */
8002 		i40e_unquiesce_vsi(vsi);
8003 	}
8004 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8005 					    vsi->macvlan_cnt);
8006 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8007 		return ERR_PTR(-EBUSY);
8008 
8009 	/* create the fwd struct */
8010 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8011 	if (!fwd)
8012 		return ERR_PTR(-ENOMEM);
8013 
8014 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8015 	fwd->bit_no = avail_macvlan;
8016 	netdev_set_sb_channel(vdev, avail_macvlan);
8017 	fwd->netdev = vdev;
8018 
8019 	if (!netif_running(netdev))
8020 		return fwd;
8021 
8022 	/* Set fwd ring up */
8023 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8024 	if (ret) {
8025 		/* unbind the queues and drop the subordinate channel config */
8026 		netdev_unbind_sb_channel(netdev, vdev);
8027 		netdev_set_sb_channel(vdev, 0);
8028 
8029 		kfree(fwd);
8030 		return ERR_PTR(-EINVAL);
8031 	}
8032 
8033 	return fwd;
8034 }
8035 
8036 /**
8037  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8038  * @vsi: the VSI we want to access
8039  */
8040 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8041 {
8042 	struct i40e_channel *ch, *ch_tmp;
8043 	struct i40e_pf *pf = vsi->back;
8044 	struct i40e_hw *hw = &pf->hw;
8045 	int aq_err, ret = 0;
8046 
8047 	if (list_empty(&vsi->macvlan_list))
8048 		return;
8049 
8050 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8051 		if (i40e_is_channel_macvlan(ch)) {
8052 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8053 						      i40e_channel_mac(ch),
8054 						      &aq_err);
8055 			if (!ret) {
8056 				/* Reset queue contexts */
8057 				i40e_reset_ch_rings(vsi, ch);
8058 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8059 				netdev_unbind_sb_channel(vsi->netdev,
8060 							 ch->fwd->netdev);
8061 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8062 				kfree(ch->fwd);
8063 				ch->fwd = NULL;
8064 			}
8065 		}
8066 	}
8067 }
8068 
8069 /**
8070  * i40e_fwd_del - delete macvlan interfaces
8071  * @netdev: net device to configure
8072  * @vdev: macvlan netdevice
8073  */
8074 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8075 {
8076 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8077 	struct i40e_fwd_adapter *fwd = vdev;
8078 	struct i40e_channel *ch, *ch_tmp;
8079 	struct i40e_vsi *vsi = np->vsi;
8080 	struct i40e_pf *pf = vsi->back;
8081 	struct i40e_hw *hw = &pf->hw;
8082 	int aq_err, ret = 0;
8083 
8084 	/* Find the channel associated with the macvlan and del mac filter */
8085 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8086 		if (i40e_is_channel_macvlan(ch) &&
8087 		    ether_addr_equal(i40e_channel_mac(ch),
8088 				     fwd->netdev->dev_addr)) {
8089 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8090 						      i40e_channel_mac(ch),
8091 						      &aq_err);
8092 			if (!ret) {
8093 				/* Reset queue contexts */
8094 				i40e_reset_ch_rings(vsi, ch);
8095 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8096 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8097 				netdev_set_sb_channel(fwd->netdev, 0);
8098 				kfree(ch->fwd);
8099 				ch->fwd = NULL;
8100 			} else {
8101 				dev_info(&pf->pdev->dev,
8102 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8103 					  i40e_stat_str(hw, ret),
8104 					  i40e_aq_str(hw, aq_err));
8105 			}
8106 			break;
8107 		}
8108 	}
8109 }
8110 
8111 /**
8112  * i40e_setup_tc - configure multiple traffic classes
8113  * @netdev: net device to configure
8114  * @type_data: tc offload data
8115  **/
8116 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8117 {
8118 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8119 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8120 	struct i40e_vsi *vsi = np->vsi;
8121 	struct i40e_pf *pf = vsi->back;
8122 	u8 enabled_tc = 0, num_tc, hw;
8123 	bool need_reset = false;
8124 	int old_queue_pairs;
8125 	int ret = -EINVAL;
8126 	u16 mode;
8127 	int i;
8128 
8129 	old_queue_pairs = vsi->num_queue_pairs;
8130 	num_tc = mqprio_qopt->qopt.num_tc;
8131 	hw = mqprio_qopt->qopt.hw;
8132 	mode = mqprio_qopt->mode;
8133 	if (!hw) {
8134 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8135 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8136 		goto config_tc;
8137 	}
8138 
8139 	/* Check if MFP enabled */
8140 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8141 		netdev_info(netdev,
8142 			    "Configuring TC not supported in MFP mode\n");
8143 		return ret;
8144 	}
8145 	switch (mode) {
8146 	case TC_MQPRIO_MODE_DCB:
8147 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8148 
8149 		/* Check if DCB enabled to continue */
8150 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8151 			netdev_info(netdev,
8152 				    "DCB is not enabled for adapter\n");
8153 			return ret;
8154 		}
8155 
8156 		/* Check whether tc count is within enabled limit */
8157 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8158 			netdev_info(netdev,
8159 				    "TC count greater than enabled on link for adapter\n");
8160 			return ret;
8161 		}
8162 		break;
8163 	case TC_MQPRIO_MODE_CHANNEL:
8164 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8165 			netdev_info(netdev,
8166 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8167 			return ret;
8168 		}
8169 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8170 			return ret;
8171 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8172 		if (ret)
8173 			return ret;
8174 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8175 		       sizeof(*mqprio_qopt));
8176 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8177 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8178 		break;
8179 	default:
8180 		return -EINVAL;
8181 	}
8182 
8183 config_tc:
8184 	/* Generate TC map for number of tc requested */
8185 	for (i = 0; i < num_tc; i++)
8186 		enabled_tc |= BIT(i);
8187 
8188 	/* Requesting same TC configuration as already enabled */
8189 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8190 	    mode != TC_MQPRIO_MODE_CHANNEL)
8191 		return 0;
8192 
8193 	/* Quiesce VSI queues */
8194 	i40e_quiesce_vsi(vsi);
8195 
8196 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8197 		i40e_remove_queue_channels(vsi);
8198 
8199 	/* Configure VSI for enabled TCs */
8200 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8201 	if (ret) {
8202 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8203 			    vsi->seid);
8204 		need_reset = true;
8205 		goto exit;
8206 	} else if (enabled_tc &&
8207 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8208 		netdev_info(netdev,
8209 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8210 			    vsi->tc_config.tc_info[0].qcount);
8211 		ret = -EINVAL;
8212 		need_reset = true;
8213 		goto exit;
8214 	}
8215 
8216 	dev_info(&vsi->back->pdev->dev,
8217 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8218 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8219 
8220 	if (i40e_is_tc_mqprio_enabled(pf)) {
8221 		if (vsi->mqprio_qopt.max_rate[0]) {
8222 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8223 
8224 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8225 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8226 			if (!ret) {
8227 				u64 credits = max_tx_rate;
8228 
8229 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8230 				dev_dbg(&vsi->back->pdev->dev,
8231 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8232 					max_tx_rate,
8233 					credits,
8234 					vsi->seid);
8235 			} else {
8236 				need_reset = true;
8237 				goto exit;
8238 			}
8239 		}
8240 		ret = i40e_configure_queue_channels(vsi);
8241 		if (ret) {
8242 			vsi->num_queue_pairs = old_queue_pairs;
8243 			netdev_info(netdev,
8244 				    "Failed configuring queue channels\n");
8245 			need_reset = true;
8246 			goto exit;
8247 		}
8248 	}
8249 
8250 exit:
8251 	/* Reset the configuration data to defaults, only TC0 is enabled */
8252 	if (need_reset) {
8253 		i40e_vsi_set_default_tc_config(vsi);
8254 		need_reset = false;
8255 	}
8256 
8257 	/* Unquiesce VSI */
8258 	i40e_unquiesce_vsi(vsi);
8259 	return ret;
8260 }
8261 
8262 /**
8263  * i40e_set_cld_element - sets cloud filter element data
8264  * @filter: cloud filter rule
8265  * @cld: ptr to cloud filter element data
8266  *
8267  * This is helper function to copy data into cloud filter element
8268  **/
8269 static inline void
8270 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8271 		     struct i40e_aqc_cloud_filters_element_data *cld)
8272 {
8273 	u32 ipa;
8274 	int i;
8275 
8276 	memset(cld, 0, sizeof(*cld));
8277 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8278 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8279 
8280 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8281 		return;
8282 
8283 	if (filter->n_proto == ETH_P_IPV6) {
8284 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8285 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8286 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8287 
8288 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8289 		}
8290 	} else {
8291 		ipa = be32_to_cpu(filter->dst_ipv4);
8292 
8293 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8294 	}
8295 
8296 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8297 
8298 	/* tenant_id is not supported by FW now, once the support is enabled
8299 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8300 	 */
8301 	if (filter->tenant_id)
8302 		return;
8303 }
8304 
8305 /**
8306  * i40e_add_del_cloud_filter - Add/del cloud filter
8307  * @vsi: pointer to VSI
8308  * @filter: cloud filter rule
8309  * @add: if true, add, if false, delete
8310  *
8311  * Add or delete a cloud filter for a specific flow spec.
8312  * Returns 0 if the filter were successfully added.
8313  **/
8314 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8315 			      struct i40e_cloud_filter *filter, bool add)
8316 {
8317 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8318 	struct i40e_pf *pf = vsi->back;
8319 	int ret;
8320 	static const u16 flag_table[128] = {
8321 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8322 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8323 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8324 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8325 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8326 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8327 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8328 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8329 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8330 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8331 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8332 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8333 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8334 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8335 	};
8336 
8337 	if (filter->flags >= ARRAY_SIZE(flag_table))
8338 		return I40E_ERR_CONFIG;
8339 
8340 	memset(&cld_filter, 0, sizeof(cld_filter));
8341 
8342 	/* copy element needed to add cloud filter from filter */
8343 	i40e_set_cld_element(filter, &cld_filter);
8344 
8345 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8346 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8347 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8348 
8349 	if (filter->n_proto == ETH_P_IPV6)
8350 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8351 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8352 	else
8353 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8354 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8355 
8356 	if (add)
8357 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8358 						&cld_filter, 1);
8359 	else
8360 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8361 						&cld_filter, 1);
8362 	if (ret)
8363 		dev_dbg(&pf->pdev->dev,
8364 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8365 			add ? "add" : "delete", filter->dst_port, ret,
8366 			pf->hw.aq.asq_last_status);
8367 	else
8368 		dev_info(&pf->pdev->dev,
8369 			 "%s cloud filter for VSI: %d\n",
8370 			 add ? "Added" : "Deleted", filter->seid);
8371 	return ret;
8372 }
8373 
8374 /**
8375  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8376  * @vsi: pointer to VSI
8377  * @filter: cloud filter rule
8378  * @add: if true, add, if false, delete
8379  *
8380  * Add or delete a cloud filter for a specific flow spec using big buffer.
8381  * Returns 0 if the filter were successfully added.
8382  **/
8383 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8384 				      struct i40e_cloud_filter *filter,
8385 				      bool add)
8386 {
8387 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8388 	struct i40e_pf *pf = vsi->back;
8389 	int ret;
8390 
8391 	/* Both (src/dst) valid mac_addr are not supported */
8392 	if ((is_valid_ether_addr(filter->dst_mac) &&
8393 	     is_valid_ether_addr(filter->src_mac)) ||
8394 	    (is_multicast_ether_addr(filter->dst_mac) &&
8395 	     is_multicast_ether_addr(filter->src_mac)))
8396 		return -EOPNOTSUPP;
8397 
8398 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8399 	 * ports are not supported via big buffer now.
8400 	 */
8401 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8402 		return -EOPNOTSUPP;
8403 
8404 	/* adding filter using src_port/src_ip is not supported at this stage */
8405 	if (filter->src_port ||
8406 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8407 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8408 		return -EOPNOTSUPP;
8409 
8410 	memset(&cld_filter, 0, sizeof(cld_filter));
8411 
8412 	/* copy element needed to add cloud filter from filter */
8413 	i40e_set_cld_element(filter, &cld_filter.element);
8414 
8415 	if (is_valid_ether_addr(filter->dst_mac) ||
8416 	    is_valid_ether_addr(filter->src_mac) ||
8417 	    is_multicast_ether_addr(filter->dst_mac) ||
8418 	    is_multicast_ether_addr(filter->src_mac)) {
8419 		/* MAC + IP : unsupported mode */
8420 		if (filter->dst_ipv4)
8421 			return -EOPNOTSUPP;
8422 
8423 		/* since we validated that L4 port must be valid before
8424 		 * we get here, start with respective "flags" value
8425 		 * and update if vlan is present or not
8426 		 */
8427 		cld_filter.element.flags =
8428 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8429 
8430 		if (filter->vlan_id) {
8431 			cld_filter.element.flags =
8432 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8433 		}
8434 
8435 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8436 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8437 		cld_filter.element.flags =
8438 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8439 		if (filter->n_proto == ETH_P_IPV6)
8440 			cld_filter.element.flags |=
8441 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8442 		else
8443 			cld_filter.element.flags |=
8444 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8445 	} else {
8446 		dev_err(&pf->pdev->dev,
8447 			"either mac or ip has to be valid for cloud filter\n");
8448 		return -EINVAL;
8449 	}
8450 
8451 	/* Now copy L4 port in Byte 6..7 in general fields */
8452 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8453 						be16_to_cpu(filter->dst_port);
8454 
8455 	if (add) {
8456 		/* Validate current device switch mode, change if necessary */
8457 		ret = i40e_validate_and_set_switch_mode(vsi);
8458 		if (ret) {
8459 			dev_err(&pf->pdev->dev,
8460 				"failed to set switch mode, ret %d\n",
8461 				ret);
8462 			return ret;
8463 		}
8464 
8465 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8466 						   &cld_filter, 1);
8467 	} else {
8468 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8469 						   &cld_filter, 1);
8470 	}
8471 
8472 	if (ret)
8473 		dev_dbg(&pf->pdev->dev,
8474 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8475 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8476 	else
8477 		dev_info(&pf->pdev->dev,
8478 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8479 			 add ? "add" : "delete", filter->seid,
8480 			 ntohs(filter->dst_port));
8481 	return ret;
8482 }
8483 
8484 /**
8485  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8486  * @vsi: Pointer to VSI
8487  * @f: Pointer to struct flow_cls_offload
8488  * @filter: Pointer to cloud filter structure
8489  *
8490  **/
8491 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8492 				 struct flow_cls_offload *f,
8493 				 struct i40e_cloud_filter *filter)
8494 {
8495 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8496 	struct flow_dissector *dissector = rule->match.dissector;
8497 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8498 	struct i40e_pf *pf = vsi->back;
8499 	u8 field_flags = 0;
8500 
8501 	if (dissector->used_keys &
8502 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8503 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8504 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8505 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8506 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8507 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8508 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8509 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8510 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8511 			dissector->used_keys);
8512 		return -EOPNOTSUPP;
8513 	}
8514 
8515 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8516 		struct flow_match_enc_keyid match;
8517 
8518 		flow_rule_match_enc_keyid(rule, &match);
8519 		if (match.mask->keyid != 0)
8520 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8521 
8522 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8523 	}
8524 
8525 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8526 		struct flow_match_basic match;
8527 
8528 		flow_rule_match_basic(rule, &match);
8529 		n_proto_key = ntohs(match.key->n_proto);
8530 		n_proto_mask = ntohs(match.mask->n_proto);
8531 
8532 		if (n_proto_key == ETH_P_ALL) {
8533 			n_proto_key = 0;
8534 			n_proto_mask = 0;
8535 		}
8536 		filter->n_proto = n_proto_key & n_proto_mask;
8537 		filter->ip_proto = match.key->ip_proto;
8538 	}
8539 
8540 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8541 		struct flow_match_eth_addrs match;
8542 
8543 		flow_rule_match_eth_addrs(rule, &match);
8544 
8545 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8546 		if (!is_zero_ether_addr(match.mask->dst)) {
8547 			if (is_broadcast_ether_addr(match.mask->dst)) {
8548 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8549 			} else {
8550 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8551 					match.mask->dst);
8552 				return I40E_ERR_CONFIG;
8553 			}
8554 		}
8555 
8556 		if (!is_zero_ether_addr(match.mask->src)) {
8557 			if (is_broadcast_ether_addr(match.mask->src)) {
8558 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8559 			} else {
8560 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8561 					match.mask->src);
8562 				return I40E_ERR_CONFIG;
8563 			}
8564 		}
8565 		ether_addr_copy(filter->dst_mac, match.key->dst);
8566 		ether_addr_copy(filter->src_mac, match.key->src);
8567 	}
8568 
8569 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8570 		struct flow_match_vlan match;
8571 
8572 		flow_rule_match_vlan(rule, &match);
8573 		if (match.mask->vlan_id) {
8574 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8575 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8576 
8577 			} else {
8578 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8579 					match.mask->vlan_id);
8580 				return I40E_ERR_CONFIG;
8581 			}
8582 		}
8583 
8584 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8585 	}
8586 
8587 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8588 		struct flow_match_control match;
8589 
8590 		flow_rule_match_control(rule, &match);
8591 		addr_type = match.key->addr_type;
8592 	}
8593 
8594 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8595 		struct flow_match_ipv4_addrs match;
8596 
8597 		flow_rule_match_ipv4_addrs(rule, &match);
8598 		if (match.mask->dst) {
8599 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8600 				field_flags |= I40E_CLOUD_FIELD_IIP;
8601 			} else {
8602 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8603 					&match.mask->dst);
8604 				return I40E_ERR_CONFIG;
8605 			}
8606 		}
8607 
8608 		if (match.mask->src) {
8609 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8610 				field_flags |= I40E_CLOUD_FIELD_IIP;
8611 			} else {
8612 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8613 					&match.mask->src);
8614 				return I40E_ERR_CONFIG;
8615 			}
8616 		}
8617 
8618 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8619 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8620 			return I40E_ERR_CONFIG;
8621 		}
8622 		filter->dst_ipv4 = match.key->dst;
8623 		filter->src_ipv4 = match.key->src;
8624 	}
8625 
8626 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8627 		struct flow_match_ipv6_addrs match;
8628 
8629 		flow_rule_match_ipv6_addrs(rule, &match);
8630 
8631 		/* src and dest IPV6 address should not be LOOPBACK
8632 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8633 		 */
8634 		if (ipv6_addr_loopback(&match.key->dst) ||
8635 		    ipv6_addr_loopback(&match.key->src)) {
8636 			dev_err(&pf->pdev->dev,
8637 				"Bad ipv6, addr is LOOPBACK\n");
8638 			return I40E_ERR_CONFIG;
8639 		}
8640 		if (!ipv6_addr_any(&match.mask->dst) ||
8641 		    !ipv6_addr_any(&match.mask->src))
8642 			field_flags |= I40E_CLOUD_FIELD_IIP;
8643 
8644 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8645 		       sizeof(filter->src_ipv6));
8646 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8647 		       sizeof(filter->dst_ipv6));
8648 	}
8649 
8650 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8651 		struct flow_match_ports match;
8652 
8653 		flow_rule_match_ports(rule, &match);
8654 		if (match.mask->src) {
8655 			if (match.mask->src == cpu_to_be16(0xffff)) {
8656 				field_flags |= I40E_CLOUD_FIELD_IIP;
8657 			} else {
8658 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8659 					be16_to_cpu(match.mask->src));
8660 				return I40E_ERR_CONFIG;
8661 			}
8662 		}
8663 
8664 		if (match.mask->dst) {
8665 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8666 				field_flags |= I40E_CLOUD_FIELD_IIP;
8667 			} else {
8668 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8669 					be16_to_cpu(match.mask->dst));
8670 				return I40E_ERR_CONFIG;
8671 			}
8672 		}
8673 
8674 		filter->dst_port = match.key->dst;
8675 		filter->src_port = match.key->src;
8676 
8677 		switch (filter->ip_proto) {
8678 		case IPPROTO_TCP:
8679 		case IPPROTO_UDP:
8680 			break;
8681 		default:
8682 			dev_err(&pf->pdev->dev,
8683 				"Only UDP and TCP transport are supported\n");
8684 			return -EINVAL;
8685 		}
8686 	}
8687 	filter->flags = field_flags;
8688 	return 0;
8689 }
8690 
8691 /**
8692  * i40e_handle_tclass: Forward to a traffic class on the device
8693  * @vsi: Pointer to VSI
8694  * @tc: traffic class index on the device
8695  * @filter: Pointer to cloud filter structure
8696  *
8697  **/
8698 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8699 			      struct i40e_cloud_filter *filter)
8700 {
8701 	struct i40e_channel *ch, *ch_tmp;
8702 
8703 	/* direct to a traffic class on the same device */
8704 	if (tc == 0) {
8705 		filter->seid = vsi->seid;
8706 		return 0;
8707 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8708 		if (!filter->dst_port) {
8709 			dev_err(&vsi->back->pdev->dev,
8710 				"Specify destination port to direct to traffic class that is not default\n");
8711 			return -EINVAL;
8712 		}
8713 		if (list_empty(&vsi->ch_list))
8714 			return -EINVAL;
8715 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8716 					 list) {
8717 			if (ch->seid == vsi->tc_seid_map[tc])
8718 				filter->seid = ch->seid;
8719 		}
8720 		return 0;
8721 	}
8722 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8723 	return -EINVAL;
8724 }
8725 
8726 /**
8727  * i40e_configure_clsflower - Configure tc flower filters
8728  * @vsi: Pointer to VSI
8729  * @cls_flower: Pointer to struct flow_cls_offload
8730  *
8731  **/
8732 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8733 				    struct flow_cls_offload *cls_flower)
8734 {
8735 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8736 	struct i40e_cloud_filter *filter = NULL;
8737 	struct i40e_pf *pf = vsi->back;
8738 	int err = 0;
8739 
8740 	if (tc < 0) {
8741 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8742 		return -EOPNOTSUPP;
8743 	}
8744 
8745 	if (!tc) {
8746 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8747 		return -EINVAL;
8748 	}
8749 
8750 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8751 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8752 		return -EBUSY;
8753 
8754 	if (pf->fdir_pf_active_filters ||
8755 	    (!hlist_empty(&pf->fdir_filter_list))) {
8756 		dev_err(&vsi->back->pdev->dev,
8757 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8758 		return -EINVAL;
8759 	}
8760 
8761 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8762 		dev_err(&vsi->back->pdev->dev,
8763 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8764 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8765 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8766 	}
8767 
8768 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8769 	if (!filter)
8770 		return -ENOMEM;
8771 
8772 	filter->cookie = cls_flower->cookie;
8773 
8774 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8775 	if (err < 0)
8776 		goto err;
8777 
8778 	err = i40e_handle_tclass(vsi, tc, filter);
8779 	if (err < 0)
8780 		goto err;
8781 
8782 	/* Add cloud filter */
8783 	if (filter->dst_port)
8784 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8785 	else
8786 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8787 
8788 	if (err) {
8789 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8790 			err);
8791 		goto err;
8792 	}
8793 
8794 	/* add filter to the ordered list */
8795 	INIT_HLIST_NODE(&filter->cloud_node);
8796 
8797 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8798 
8799 	pf->num_cloud_filters++;
8800 
8801 	return err;
8802 err:
8803 	kfree(filter);
8804 	return err;
8805 }
8806 
8807 /**
8808  * i40e_find_cloud_filter - Find the could filter in the list
8809  * @vsi: Pointer to VSI
8810  * @cookie: filter specific cookie
8811  *
8812  **/
8813 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8814 							unsigned long *cookie)
8815 {
8816 	struct i40e_cloud_filter *filter = NULL;
8817 	struct hlist_node *node2;
8818 
8819 	hlist_for_each_entry_safe(filter, node2,
8820 				  &vsi->back->cloud_filter_list, cloud_node)
8821 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8822 			return filter;
8823 	return NULL;
8824 }
8825 
8826 /**
8827  * i40e_delete_clsflower - Remove tc flower filters
8828  * @vsi: Pointer to VSI
8829  * @cls_flower: Pointer to struct flow_cls_offload
8830  *
8831  **/
8832 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8833 				 struct flow_cls_offload *cls_flower)
8834 {
8835 	struct i40e_cloud_filter *filter = NULL;
8836 	struct i40e_pf *pf = vsi->back;
8837 	int err = 0;
8838 
8839 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8840 
8841 	if (!filter)
8842 		return -EINVAL;
8843 
8844 	hash_del(&filter->cloud_node);
8845 
8846 	if (filter->dst_port)
8847 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8848 	else
8849 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8850 
8851 	kfree(filter);
8852 	if (err) {
8853 		dev_err(&pf->pdev->dev,
8854 			"Failed to delete cloud filter, err %s\n",
8855 			i40e_stat_str(&pf->hw, err));
8856 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8857 	}
8858 
8859 	pf->num_cloud_filters--;
8860 	if (!pf->num_cloud_filters)
8861 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8862 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8863 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8864 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8865 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8866 		}
8867 	return 0;
8868 }
8869 
8870 /**
8871  * i40e_setup_tc_cls_flower - flower classifier offloads
8872  * @np: net device to configure
8873  * @cls_flower: offload data
8874  **/
8875 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8876 				    struct flow_cls_offload *cls_flower)
8877 {
8878 	struct i40e_vsi *vsi = np->vsi;
8879 
8880 	switch (cls_flower->command) {
8881 	case FLOW_CLS_REPLACE:
8882 		return i40e_configure_clsflower(vsi, cls_flower);
8883 	case FLOW_CLS_DESTROY:
8884 		return i40e_delete_clsflower(vsi, cls_flower);
8885 	case FLOW_CLS_STATS:
8886 		return -EOPNOTSUPP;
8887 	default:
8888 		return -EOPNOTSUPP;
8889 	}
8890 }
8891 
8892 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8893 				  void *cb_priv)
8894 {
8895 	struct i40e_netdev_priv *np = cb_priv;
8896 
8897 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8898 		return -EOPNOTSUPP;
8899 
8900 	switch (type) {
8901 	case TC_SETUP_CLSFLOWER:
8902 		return i40e_setup_tc_cls_flower(np, type_data);
8903 
8904 	default:
8905 		return -EOPNOTSUPP;
8906 	}
8907 }
8908 
8909 static LIST_HEAD(i40e_block_cb_list);
8910 
8911 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8912 			   void *type_data)
8913 {
8914 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8915 
8916 	switch (type) {
8917 	case TC_SETUP_QDISC_MQPRIO:
8918 		return i40e_setup_tc(netdev, type_data);
8919 	case TC_SETUP_BLOCK:
8920 		return flow_block_cb_setup_simple(type_data,
8921 						  &i40e_block_cb_list,
8922 						  i40e_setup_tc_block_cb,
8923 						  np, np, true);
8924 	default:
8925 		return -EOPNOTSUPP;
8926 	}
8927 }
8928 
8929 /**
8930  * i40e_open - Called when a network interface is made active
8931  * @netdev: network interface device structure
8932  *
8933  * The open entry point is called when a network interface is made
8934  * active by the system (IFF_UP).  At this point all resources needed
8935  * for transmit and receive operations are allocated, the interrupt
8936  * handler is registered with the OS, the netdev watchdog subtask is
8937  * enabled, and the stack is notified that the interface is ready.
8938  *
8939  * Returns 0 on success, negative value on failure
8940  **/
8941 int i40e_open(struct net_device *netdev)
8942 {
8943 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8944 	struct i40e_vsi *vsi = np->vsi;
8945 	struct i40e_pf *pf = vsi->back;
8946 	int err;
8947 
8948 	/* disallow open during test or if eeprom is broken */
8949 	if (test_bit(__I40E_TESTING, pf->state) ||
8950 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8951 		return -EBUSY;
8952 
8953 	netif_carrier_off(netdev);
8954 
8955 	if (i40e_force_link_state(pf, true))
8956 		return -EAGAIN;
8957 
8958 	err = i40e_vsi_open(vsi);
8959 	if (err)
8960 		return err;
8961 
8962 	/* configure global TSO hardware offload settings */
8963 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8964 						       TCP_FLAG_FIN) >> 16);
8965 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8966 						       TCP_FLAG_FIN |
8967 						       TCP_FLAG_CWR) >> 16);
8968 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8969 	udp_tunnel_get_rx_info(netdev);
8970 
8971 	return 0;
8972 }
8973 
8974 /**
8975  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8976  * @vsi: vsi structure
8977  *
8978  * This updates netdev's number of tx/rx queues
8979  *
8980  * Returns status of setting tx/rx queues
8981  **/
8982 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8983 {
8984 	int ret;
8985 
8986 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8987 					   vsi->num_queue_pairs);
8988 	if (ret)
8989 		return ret;
8990 
8991 	return netif_set_real_num_tx_queues(vsi->netdev,
8992 					    vsi->num_queue_pairs);
8993 }
8994 
8995 /**
8996  * i40e_vsi_open -
8997  * @vsi: the VSI to open
8998  *
8999  * Finish initialization of the VSI.
9000  *
9001  * Returns 0 on success, negative value on failure
9002  *
9003  * Note: expects to be called while under rtnl_lock()
9004  **/
9005 int i40e_vsi_open(struct i40e_vsi *vsi)
9006 {
9007 	struct i40e_pf *pf = vsi->back;
9008 	char int_name[I40E_INT_NAME_STR_LEN];
9009 	int err;
9010 
9011 	/* allocate descriptors */
9012 	err = i40e_vsi_setup_tx_resources(vsi);
9013 	if (err)
9014 		goto err_setup_tx;
9015 	err = i40e_vsi_setup_rx_resources(vsi);
9016 	if (err)
9017 		goto err_setup_rx;
9018 
9019 	err = i40e_vsi_configure(vsi);
9020 	if (err)
9021 		goto err_setup_rx;
9022 
9023 	if (vsi->netdev) {
9024 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9025 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9026 		err = i40e_vsi_request_irq(vsi, int_name);
9027 		if (err)
9028 			goto err_setup_rx;
9029 
9030 		/* Notify the stack of the actual queue counts. */
9031 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9032 		if (err)
9033 			goto err_set_queues;
9034 
9035 	} else if (vsi->type == I40E_VSI_FDIR) {
9036 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9037 			 dev_driver_string(&pf->pdev->dev),
9038 			 dev_name(&pf->pdev->dev));
9039 		err = i40e_vsi_request_irq(vsi, int_name);
9040 		if (err)
9041 			goto err_setup_rx;
9042 
9043 	} else {
9044 		err = -EINVAL;
9045 		goto err_setup_rx;
9046 	}
9047 
9048 	err = i40e_up_complete(vsi);
9049 	if (err)
9050 		goto err_up_complete;
9051 
9052 	return 0;
9053 
9054 err_up_complete:
9055 	i40e_down(vsi);
9056 err_set_queues:
9057 	i40e_vsi_free_irq(vsi);
9058 err_setup_rx:
9059 	i40e_vsi_free_rx_resources(vsi);
9060 err_setup_tx:
9061 	i40e_vsi_free_tx_resources(vsi);
9062 	if (vsi == pf->vsi[pf->lan_vsi])
9063 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9064 
9065 	return err;
9066 }
9067 
9068 /**
9069  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9070  * @pf: Pointer to PF
9071  *
9072  * This function destroys the hlist where all the Flow Director
9073  * filters were saved.
9074  **/
9075 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9076 {
9077 	struct i40e_fdir_filter *filter;
9078 	struct i40e_flex_pit *pit_entry, *tmp;
9079 	struct hlist_node *node2;
9080 
9081 	hlist_for_each_entry_safe(filter, node2,
9082 				  &pf->fdir_filter_list, fdir_node) {
9083 		hlist_del(&filter->fdir_node);
9084 		kfree(filter);
9085 	}
9086 
9087 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9088 		list_del(&pit_entry->list);
9089 		kfree(pit_entry);
9090 	}
9091 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9092 
9093 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9094 		list_del(&pit_entry->list);
9095 		kfree(pit_entry);
9096 	}
9097 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9098 
9099 	pf->fdir_pf_active_filters = 0;
9100 	i40e_reset_fdir_filter_cnt(pf);
9101 
9102 	/* Reprogram the default input set for TCP/IPv4 */
9103 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9104 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9105 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9106 
9107 	/* Reprogram the default input set for TCP/IPv6 */
9108 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9109 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9110 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9111 
9112 	/* Reprogram the default input set for UDP/IPv4 */
9113 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9114 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9115 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9116 
9117 	/* Reprogram the default input set for UDP/IPv6 */
9118 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9119 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9120 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9121 
9122 	/* Reprogram the default input set for SCTP/IPv4 */
9123 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9124 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9125 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9126 
9127 	/* Reprogram the default input set for SCTP/IPv6 */
9128 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9129 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9130 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9131 
9132 	/* Reprogram the default input set for Other/IPv4 */
9133 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9134 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9135 
9136 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9137 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9138 
9139 	/* Reprogram the default input set for Other/IPv6 */
9140 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9141 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9142 
9143 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9144 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9145 }
9146 
9147 /**
9148  * i40e_cloud_filter_exit - Cleans up the cloud filters
9149  * @pf: Pointer to PF
9150  *
9151  * This function destroys the hlist where all the cloud filters
9152  * were saved.
9153  **/
9154 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9155 {
9156 	struct i40e_cloud_filter *cfilter;
9157 	struct hlist_node *node;
9158 
9159 	hlist_for_each_entry_safe(cfilter, node,
9160 				  &pf->cloud_filter_list, cloud_node) {
9161 		hlist_del(&cfilter->cloud_node);
9162 		kfree(cfilter);
9163 	}
9164 	pf->num_cloud_filters = 0;
9165 
9166 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9167 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9168 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9169 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9170 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9171 	}
9172 }
9173 
9174 /**
9175  * i40e_close - Disables a network interface
9176  * @netdev: network interface device structure
9177  *
9178  * The close entry point is called when an interface is de-activated
9179  * by the OS.  The hardware is still under the driver's control, but
9180  * this netdev interface is disabled.
9181  *
9182  * Returns 0, this is not allowed to fail
9183  **/
9184 int i40e_close(struct net_device *netdev)
9185 {
9186 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9187 	struct i40e_vsi *vsi = np->vsi;
9188 
9189 	i40e_vsi_close(vsi);
9190 
9191 	return 0;
9192 }
9193 
9194 /**
9195  * i40e_do_reset - Start a PF or Core Reset sequence
9196  * @pf: board private structure
9197  * @reset_flags: which reset is requested
9198  * @lock_acquired: indicates whether or not the lock has been acquired
9199  * before this function was called.
9200  *
9201  * The essential difference in resets is that the PF Reset
9202  * doesn't clear the packet buffers, doesn't reset the PE
9203  * firmware, and doesn't bother the other PFs on the chip.
9204  **/
9205 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9206 {
9207 	u32 val;
9208 
9209 	/* do the biggest reset indicated */
9210 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9211 
9212 		/* Request a Global Reset
9213 		 *
9214 		 * This will start the chip's countdown to the actual full
9215 		 * chip reset event, and a warning interrupt to be sent
9216 		 * to all PFs, including the requestor.  Our handler
9217 		 * for the warning interrupt will deal with the shutdown
9218 		 * and recovery of the switch setup.
9219 		 */
9220 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9221 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9222 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9223 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9224 
9225 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9226 
9227 		/* Request a Core Reset
9228 		 *
9229 		 * Same as Global Reset, except does *not* include the MAC/PHY
9230 		 */
9231 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9232 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9233 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9234 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9235 		i40e_flush(&pf->hw);
9236 
9237 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9238 
9239 		/* Request a PF Reset
9240 		 *
9241 		 * Resets only the PF-specific registers
9242 		 *
9243 		 * This goes directly to the tear-down and rebuild of
9244 		 * the switch, since we need to do all the recovery as
9245 		 * for the Core Reset.
9246 		 */
9247 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9248 		i40e_handle_reset_warning(pf, lock_acquired);
9249 
9250 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9251 		/* Request a PF Reset
9252 		 *
9253 		 * Resets PF and reinitializes PFs VSI.
9254 		 */
9255 		i40e_prep_for_reset(pf);
9256 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9257 		dev_info(&pf->pdev->dev,
9258 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9259 			 "FW LLDP is disabled\n" :
9260 			 "FW LLDP is enabled\n");
9261 
9262 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9263 		int v;
9264 
9265 		/* Find the VSI(s) that requested a re-init */
9266 		dev_info(&pf->pdev->dev,
9267 			 "VSI reinit requested\n");
9268 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9269 			struct i40e_vsi *vsi = pf->vsi[v];
9270 
9271 			if (vsi != NULL &&
9272 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9273 					       vsi->state))
9274 				i40e_vsi_reinit_locked(pf->vsi[v]);
9275 		}
9276 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9277 		int v;
9278 
9279 		/* Find the VSI(s) that needs to be brought down */
9280 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9281 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9282 			struct i40e_vsi *vsi = pf->vsi[v];
9283 
9284 			if (vsi != NULL &&
9285 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9286 					       vsi->state)) {
9287 				set_bit(__I40E_VSI_DOWN, vsi->state);
9288 				i40e_down(vsi);
9289 			}
9290 		}
9291 	} else {
9292 		dev_info(&pf->pdev->dev,
9293 			 "bad reset request 0x%08x\n", reset_flags);
9294 	}
9295 }
9296 
9297 #ifdef CONFIG_I40E_DCB
9298 /**
9299  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9300  * @pf: board private structure
9301  * @old_cfg: current DCB config
9302  * @new_cfg: new DCB config
9303  **/
9304 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9305 			    struct i40e_dcbx_config *old_cfg,
9306 			    struct i40e_dcbx_config *new_cfg)
9307 {
9308 	bool need_reconfig = false;
9309 
9310 	/* Check if ETS configuration has changed */
9311 	if (memcmp(&new_cfg->etscfg,
9312 		   &old_cfg->etscfg,
9313 		   sizeof(new_cfg->etscfg))) {
9314 		/* If Priority Table has changed reconfig is needed */
9315 		if (memcmp(&new_cfg->etscfg.prioritytable,
9316 			   &old_cfg->etscfg.prioritytable,
9317 			   sizeof(new_cfg->etscfg.prioritytable))) {
9318 			need_reconfig = true;
9319 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9320 		}
9321 
9322 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9323 			   &old_cfg->etscfg.tcbwtable,
9324 			   sizeof(new_cfg->etscfg.tcbwtable)))
9325 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9326 
9327 		if (memcmp(&new_cfg->etscfg.tsatable,
9328 			   &old_cfg->etscfg.tsatable,
9329 			   sizeof(new_cfg->etscfg.tsatable)))
9330 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9331 	}
9332 
9333 	/* Check if PFC configuration has changed */
9334 	if (memcmp(&new_cfg->pfc,
9335 		   &old_cfg->pfc,
9336 		   sizeof(new_cfg->pfc))) {
9337 		need_reconfig = true;
9338 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9339 	}
9340 
9341 	/* Check if APP Table has changed */
9342 	if (memcmp(&new_cfg->app,
9343 		   &old_cfg->app,
9344 		   sizeof(new_cfg->app))) {
9345 		need_reconfig = true;
9346 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9347 	}
9348 
9349 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9350 	return need_reconfig;
9351 }
9352 
9353 /**
9354  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9355  * @pf: board private structure
9356  * @e: event info posted on ARQ
9357  **/
9358 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9359 				  struct i40e_arq_event_info *e)
9360 {
9361 	struct i40e_aqc_lldp_get_mib *mib =
9362 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9363 	struct i40e_hw *hw = &pf->hw;
9364 	struct i40e_dcbx_config tmp_dcbx_cfg;
9365 	bool need_reconfig = false;
9366 	int ret = 0;
9367 	u8 type;
9368 
9369 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9370 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9371 	    (hw->phy.link_info.link_speed &
9372 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9373 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9374 		/* let firmware decide if the DCB should be disabled */
9375 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9376 
9377 	/* Not DCB capable or capability disabled */
9378 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9379 		return ret;
9380 
9381 	/* Ignore if event is not for Nearest Bridge */
9382 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9383 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9384 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9385 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9386 		return ret;
9387 
9388 	/* Check MIB Type and return if event for Remote MIB update */
9389 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9390 	dev_dbg(&pf->pdev->dev,
9391 		"LLDP event mib type %s\n", type ? "remote" : "local");
9392 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9393 		/* Update the remote cached instance and return */
9394 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9395 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9396 				&hw->remote_dcbx_config);
9397 		goto exit;
9398 	}
9399 
9400 	/* Store the old configuration */
9401 	tmp_dcbx_cfg = hw->local_dcbx_config;
9402 
9403 	/* Reset the old DCBx configuration data */
9404 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9405 	/* Get updated DCBX data from firmware */
9406 	ret = i40e_get_dcb_config(&pf->hw);
9407 	if (ret) {
9408 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9409 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9410 		    (hw->phy.link_info.link_speed &
9411 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9412 			dev_warn(&pf->pdev->dev,
9413 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9414 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9415 		} else {
9416 			dev_info(&pf->pdev->dev,
9417 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9418 				 i40e_stat_str(&pf->hw, ret),
9419 				 i40e_aq_str(&pf->hw,
9420 					     pf->hw.aq.asq_last_status));
9421 		}
9422 		goto exit;
9423 	}
9424 
9425 	/* No change detected in DCBX configs */
9426 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9427 		    sizeof(tmp_dcbx_cfg))) {
9428 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9429 		goto exit;
9430 	}
9431 
9432 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9433 					       &hw->local_dcbx_config);
9434 
9435 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9436 
9437 	if (!need_reconfig)
9438 		goto exit;
9439 
9440 	/* Enable DCB tagging only when more than one TC */
9441 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9442 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9443 	else
9444 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9445 
9446 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9447 	/* Reconfiguration needed quiesce all VSIs */
9448 	i40e_pf_quiesce_all_vsi(pf);
9449 
9450 	/* Changes in configuration update VEB/VSI */
9451 	i40e_dcb_reconfigure(pf);
9452 
9453 	ret = i40e_resume_port_tx(pf);
9454 
9455 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9456 	/* In case of error no point in resuming VSIs */
9457 	if (ret)
9458 		goto exit;
9459 
9460 	/* Wait for the PF's queues to be disabled */
9461 	ret = i40e_pf_wait_queues_disabled(pf);
9462 	if (ret) {
9463 		/* Schedule PF reset to recover */
9464 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9465 		i40e_service_event_schedule(pf);
9466 	} else {
9467 		i40e_pf_unquiesce_all_vsi(pf);
9468 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9469 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9470 	}
9471 
9472 exit:
9473 	return ret;
9474 }
9475 #endif /* CONFIG_I40E_DCB */
9476 
9477 /**
9478  * i40e_do_reset_safe - Protected reset path for userland calls.
9479  * @pf: board private structure
9480  * @reset_flags: which reset is requested
9481  *
9482  **/
9483 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9484 {
9485 	rtnl_lock();
9486 	i40e_do_reset(pf, reset_flags, true);
9487 	rtnl_unlock();
9488 }
9489 
9490 /**
9491  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9492  * @pf: board private structure
9493  * @e: event info posted on ARQ
9494  *
9495  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9496  * and VF queues
9497  **/
9498 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9499 					   struct i40e_arq_event_info *e)
9500 {
9501 	struct i40e_aqc_lan_overflow *data =
9502 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9503 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9504 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9505 	struct i40e_hw *hw = &pf->hw;
9506 	struct i40e_vf *vf;
9507 	u16 vf_id;
9508 
9509 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9510 		queue, qtx_ctl);
9511 
9512 	/* Queue belongs to VF, find the VF and issue VF reset */
9513 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9514 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9515 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9516 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9517 		vf_id -= hw->func_caps.vf_base_id;
9518 		vf = &pf->vf[vf_id];
9519 		i40e_vc_notify_vf_reset(vf);
9520 		/* Allow VF to process pending reset notification */
9521 		msleep(20);
9522 		i40e_reset_vf(vf, false);
9523 	}
9524 }
9525 
9526 /**
9527  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9528  * @pf: board private structure
9529  **/
9530 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9531 {
9532 	u32 val, fcnt_prog;
9533 
9534 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9535 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9536 	return fcnt_prog;
9537 }
9538 
9539 /**
9540  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9541  * @pf: board private structure
9542  **/
9543 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9544 {
9545 	u32 val, fcnt_prog;
9546 
9547 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9548 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9549 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9550 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9551 	return fcnt_prog;
9552 }
9553 
9554 /**
9555  * i40e_get_global_fd_count - Get total FD filters programmed on device
9556  * @pf: board private structure
9557  **/
9558 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9559 {
9560 	u32 val, fcnt_prog;
9561 
9562 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9563 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9564 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9565 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9566 	return fcnt_prog;
9567 }
9568 
9569 /**
9570  * i40e_reenable_fdir_sb - Restore FDir SB capability
9571  * @pf: board private structure
9572  **/
9573 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9574 {
9575 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9576 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9577 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9578 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9579 }
9580 
9581 /**
9582  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9583  * @pf: board private structure
9584  **/
9585 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9586 {
9587 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9588 		/* ATR uses the same filtering logic as SB rules. It only
9589 		 * functions properly if the input set mask is at the default
9590 		 * settings. It is safe to restore the default input set
9591 		 * because there are no active TCPv4 filter rules.
9592 		 */
9593 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9594 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9595 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9596 
9597 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9598 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9599 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9600 	}
9601 }
9602 
9603 /**
9604  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9605  * @pf: board private structure
9606  * @filter: FDir filter to remove
9607  */
9608 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9609 				       struct i40e_fdir_filter *filter)
9610 {
9611 	/* Update counters */
9612 	pf->fdir_pf_active_filters--;
9613 	pf->fd_inv = 0;
9614 
9615 	switch (filter->flow_type) {
9616 	case TCP_V4_FLOW:
9617 		pf->fd_tcp4_filter_cnt--;
9618 		break;
9619 	case UDP_V4_FLOW:
9620 		pf->fd_udp4_filter_cnt--;
9621 		break;
9622 	case SCTP_V4_FLOW:
9623 		pf->fd_sctp4_filter_cnt--;
9624 		break;
9625 	case TCP_V6_FLOW:
9626 		pf->fd_tcp6_filter_cnt--;
9627 		break;
9628 	case UDP_V6_FLOW:
9629 		pf->fd_udp6_filter_cnt--;
9630 		break;
9631 	case SCTP_V6_FLOW:
9632 		pf->fd_udp6_filter_cnt--;
9633 		break;
9634 	case IP_USER_FLOW:
9635 		switch (filter->ipl4_proto) {
9636 		case IPPROTO_TCP:
9637 			pf->fd_tcp4_filter_cnt--;
9638 			break;
9639 		case IPPROTO_UDP:
9640 			pf->fd_udp4_filter_cnt--;
9641 			break;
9642 		case IPPROTO_SCTP:
9643 			pf->fd_sctp4_filter_cnt--;
9644 			break;
9645 		case IPPROTO_IP:
9646 			pf->fd_ip4_filter_cnt--;
9647 			break;
9648 		}
9649 		break;
9650 	case IPV6_USER_FLOW:
9651 		switch (filter->ipl4_proto) {
9652 		case IPPROTO_TCP:
9653 			pf->fd_tcp6_filter_cnt--;
9654 			break;
9655 		case IPPROTO_UDP:
9656 			pf->fd_udp6_filter_cnt--;
9657 			break;
9658 		case IPPROTO_SCTP:
9659 			pf->fd_sctp6_filter_cnt--;
9660 			break;
9661 		case IPPROTO_IP:
9662 			pf->fd_ip6_filter_cnt--;
9663 			break;
9664 		}
9665 		break;
9666 	}
9667 
9668 	/* Remove the filter from the list and free memory */
9669 	hlist_del(&filter->fdir_node);
9670 	kfree(filter);
9671 }
9672 
9673 /**
9674  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9675  * @pf: board private structure
9676  **/
9677 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9678 {
9679 	struct i40e_fdir_filter *filter;
9680 	u32 fcnt_prog, fcnt_avail;
9681 	struct hlist_node *node;
9682 
9683 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9684 		return;
9685 
9686 	/* Check if we have enough room to re-enable FDir SB capability. */
9687 	fcnt_prog = i40e_get_global_fd_count(pf);
9688 	fcnt_avail = pf->fdir_pf_filter_count;
9689 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9690 	    (pf->fd_add_err == 0) ||
9691 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9692 		i40e_reenable_fdir_sb(pf);
9693 
9694 	/* We should wait for even more space before re-enabling ATR.
9695 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9696 	 * rules active.
9697 	 */
9698 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9699 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9700 		i40e_reenable_fdir_atr(pf);
9701 
9702 	/* if hw had a problem adding a filter, delete it */
9703 	if (pf->fd_inv > 0) {
9704 		hlist_for_each_entry_safe(filter, node,
9705 					  &pf->fdir_filter_list, fdir_node)
9706 			if (filter->fd_id == pf->fd_inv)
9707 				i40e_delete_invalid_filter(pf, filter);
9708 	}
9709 }
9710 
9711 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9712 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9713 /**
9714  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9715  * @pf: board private structure
9716  **/
9717 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9718 {
9719 	unsigned long min_flush_time;
9720 	int flush_wait_retry = 50;
9721 	bool disable_atr = false;
9722 	int fd_room;
9723 	int reg;
9724 
9725 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9726 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9727 		return;
9728 
9729 	/* If the flush is happening too quick and we have mostly SB rules we
9730 	 * should not re-enable ATR for some time.
9731 	 */
9732 	min_flush_time = pf->fd_flush_timestamp +
9733 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9734 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9735 
9736 	if (!(time_after(jiffies, min_flush_time)) &&
9737 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9738 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9739 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9740 		disable_atr = true;
9741 	}
9742 
9743 	pf->fd_flush_timestamp = jiffies;
9744 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9745 	/* flush all filters */
9746 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9747 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9748 	i40e_flush(&pf->hw);
9749 	pf->fd_flush_cnt++;
9750 	pf->fd_add_err = 0;
9751 	do {
9752 		/* Check FD flush status every 5-6msec */
9753 		usleep_range(5000, 6000);
9754 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9755 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9756 			break;
9757 	} while (flush_wait_retry--);
9758 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9759 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9760 	} else {
9761 		/* replay sideband filters */
9762 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9763 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9764 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9765 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9766 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9767 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9768 	}
9769 }
9770 
9771 /**
9772  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9773  * @pf: board private structure
9774  **/
9775 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9776 {
9777 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9778 }
9779 
9780 /**
9781  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9782  * @pf: board private structure
9783  **/
9784 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9785 {
9786 
9787 	/* if interface is down do nothing */
9788 	if (test_bit(__I40E_DOWN, pf->state))
9789 		return;
9790 
9791 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9792 		i40e_fdir_flush_and_replay(pf);
9793 
9794 	i40e_fdir_check_and_reenable(pf);
9795 
9796 }
9797 
9798 /**
9799  * i40e_vsi_link_event - notify VSI of a link event
9800  * @vsi: vsi to be notified
9801  * @link_up: link up or down
9802  **/
9803 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9804 {
9805 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9806 		return;
9807 
9808 	switch (vsi->type) {
9809 	case I40E_VSI_MAIN:
9810 		if (!vsi->netdev || !vsi->netdev_registered)
9811 			break;
9812 
9813 		if (link_up) {
9814 			netif_carrier_on(vsi->netdev);
9815 			netif_tx_wake_all_queues(vsi->netdev);
9816 		} else {
9817 			netif_carrier_off(vsi->netdev);
9818 			netif_tx_stop_all_queues(vsi->netdev);
9819 		}
9820 		break;
9821 
9822 	case I40E_VSI_SRIOV:
9823 	case I40E_VSI_VMDQ2:
9824 	case I40E_VSI_CTRL:
9825 	case I40E_VSI_IWARP:
9826 	case I40E_VSI_MIRROR:
9827 	default:
9828 		/* there is no notification for other VSIs */
9829 		break;
9830 	}
9831 }
9832 
9833 /**
9834  * i40e_veb_link_event - notify elements on the veb of a link event
9835  * @veb: veb to be notified
9836  * @link_up: link up or down
9837  **/
9838 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9839 {
9840 	struct i40e_pf *pf;
9841 	int i;
9842 
9843 	if (!veb || !veb->pf)
9844 		return;
9845 	pf = veb->pf;
9846 
9847 	/* depth first... */
9848 	for (i = 0; i < I40E_MAX_VEB; i++)
9849 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9850 			i40e_veb_link_event(pf->veb[i], link_up);
9851 
9852 	/* ... now the local VSIs */
9853 	for (i = 0; i < pf->num_alloc_vsi; i++)
9854 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9855 			i40e_vsi_link_event(pf->vsi[i], link_up);
9856 }
9857 
9858 /**
9859  * i40e_link_event - Update netif_carrier status
9860  * @pf: board private structure
9861  **/
9862 static void i40e_link_event(struct i40e_pf *pf)
9863 {
9864 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9865 	u8 new_link_speed, old_link_speed;
9866 	i40e_status status;
9867 	bool new_link, old_link;
9868 #ifdef CONFIG_I40E_DCB
9869 	int err;
9870 #endif /* CONFIG_I40E_DCB */
9871 
9872 	/* set this to force the get_link_status call to refresh state */
9873 	pf->hw.phy.get_link_info = true;
9874 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9875 	status = i40e_get_link_status(&pf->hw, &new_link);
9876 
9877 	/* On success, disable temp link polling */
9878 	if (status == I40E_SUCCESS) {
9879 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9880 	} else {
9881 		/* Enable link polling temporarily until i40e_get_link_status
9882 		 * returns I40E_SUCCESS
9883 		 */
9884 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9885 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9886 			status);
9887 		return;
9888 	}
9889 
9890 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9891 	new_link_speed = pf->hw.phy.link_info.link_speed;
9892 
9893 	if (new_link == old_link &&
9894 	    new_link_speed == old_link_speed &&
9895 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9896 	     new_link == netif_carrier_ok(vsi->netdev)))
9897 		return;
9898 
9899 	i40e_print_link_message(vsi, new_link);
9900 
9901 	/* Notify the base of the switch tree connected to
9902 	 * the link.  Floating VEBs are not notified.
9903 	 */
9904 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9905 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9906 	else
9907 		i40e_vsi_link_event(vsi, new_link);
9908 
9909 	if (pf->vf)
9910 		i40e_vc_notify_link_state(pf);
9911 
9912 	if (pf->flags & I40E_FLAG_PTP)
9913 		i40e_ptp_set_increment(pf);
9914 #ifdef CONFIG_I40E_DCB
9915 	if (new_link == old_link)
9916 		return;
9917 	/* Not SW DCB so firmware will take care of default settings */
9918 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9919 		return;
9920 
9921 	/* We cover here only link down, as after link up in case of SW DCB
9922 	 * SW LLDP agent will take care of setting it up
9923 	 */
9924 	if (!new_link) {
9925 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9926 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9927 		err = i40e_dcb_sw_default_config(pf);
9928 		if (err) {
9929 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9930 				       I40E_FLAG_DCB_ENABLED);
9931 		} else {
9932 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9933 				       DCB_CAP_DCBX_VER_IEEE;
9934 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9935 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9936 		}
9937 	}
9938 #endif /* CONFIG_I40E_DCB */
9939 }
9940 
9941 /**
9942  * i40e_watchdog_subtask - periodic checks not using event driven response
9943  * @pf: board private structure
9944  **/
9945 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9946 {
9947 	int i;
9948 
9949 	/* if interface is down do nothing */
9950 	if (test_bit(__I40E_DOWN, pf->state) ||
9951 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9952 		return;
9953 
9954 	/* make sure we don't do these things too often */
9955 	if (time_before(jiffies, (pf->service_timer_previous +
9956 				  pf->service_timer_period)))
9957 		return;
9958 	pf->service_timer_previous = jiffies;
9959 
9960 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9961 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9962 		i40e_link_event(pf);
9963 
9964 	/* Update the stats for active netdevs so the network stack
9965 	 * can look at updated numbers whenever it cares to
9966 	 */
9967 	for (i = 0; i < pf->num_alloc_vsi; i++)
9968 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9969 			i40e_update_stats(pf->vsi[i]);
9970 
9971 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9972 		/* Update the stats for the active switching components */
9973 		for (i = 0; i < I40E_MAX_VEB; i++)
9974 			if (pf->veb[i])
9975 				i40e_update_veb_stats(pf->veb[i]);
9976 	}
9977 
9978 	i40e_ptp_rx_hang(pf);
9979 	i40e_ptp_tx_hang(pf);
9980 }
9981 
9982 /**
9983  * i40e_reset_subtask - Set up for resetting the device and driver
9984  * @pf: board private structure
9985  **/
9986 static void i40e_reset_subtask(struct i40e_pf *pf)
9987 {
9988 	u32 reset_flags = 0;
9989 
9990 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9991 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9992 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9993 	}
9994 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9995 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9996 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9997 	}
9998 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9999 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10000 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10001 	}
10002 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10003 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10004 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10005 	}
10006 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10007 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10008 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10009 	}
10010 
10011 	/* If there's a recovery already waiting, it takes
10012 	 * precedence before starting a new reset sequence.
10013 	 */
10014 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10015 		i40e_prep_for_reset(pf);
10016 		i40e_reset(pf);
10017 		i40e_rebuild(pf, false, false);
10018 	}
10019 
10020 	/* If we're already down or resetting, just bail */
10021 	if (reset_flags &&
10022 	    !test_bit(__I40E_DOWN, pf->state) &&
10023 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10024 		i40e_do_reset(pf, reset_flags, false);
10025 	}
10026 }
10027 
10028 /**
10029  * i40e_handle_link_event - Handle link event
10030  * @pf: board private structure
10031  * @e: event info posted on ARQ
10032  **/
10033 static void i40e_handle_link_event(struct i40e_pf *pf,
10034 				   struct i40e_arq_event_info *e)
10035 {
10036 	struct i40e_aqc_get_link_status *status =
10037 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10038 
10039 	/* Do a new status request to re-enable LSE reporting
10040 	 * and load new status information into the hw struct
10041 	 * This completely ignores any state information
10042 	 * in the ARQ event info, instead choosing to always
10043 	 * issue the AQ update link status command.
10044 	 */
10045 	i40e_link_event(pf);
10046 
10047 	/* Check if module meets thermal requirements */
10048 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10049 		dev_err(&pf->pdev->dev,
10050 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10051 		dev_err(&pf->pdev->dev,
10052 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10053 	} else {
10054 		/* check for unqualified module, if link is down, suppress
10055 		 * the message if link was forced to be down.
10056 		 */
10057 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10058 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10059 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10060 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10061 			dev_err(&pf->pdev->dev,
10062 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10063 			dev_err(&pf->pdev->dev,
10064 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10065 		}
10066 	}
10067 }
10068 
10069 /**
10070  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10071  * @pf: board private structure
10072  **/
10073 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10074 {
10075 	struct i40e_arq_event_info event;
10076 	struct i40e_hw *hw = &pf->hw;
10077 	u16 pending, i = 0;
10078 	i40e_status ret;
10079 	u16 opcode;
10080 	u32 oldval;
10081 	u32 val;
10082 
10083 	/* Do not run clean AQ when PF reset fails */
10084 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10085 		return;
10086 
10087 	/* check for error indications */
10088 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10089 	oldval = val;
10090 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10091 		if (hw->debug_mask & I40E_DEBUG_AQ)
10092 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10093 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10094 	}
10095 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10096 		if (hw->debug_mask & I40E_DEBUG_AQ)
10097 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10098 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10099 		pf->arq_overflows++;
10100 	}
10101 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10102 		if (hw->debug_mask & I40E_DEBUG_AQ)
10103 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10104 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10105 	}
10106 	if (oldval != val)
10107 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10108 
10109 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10110 	oldval = val;
10111 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10112 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10113 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10114 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10115 	}
10116 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10117 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10118 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10119 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10120 	}
10121 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10122 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10123 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10124 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10125 	}
10126 	if (oldval != val)
10127 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10128 
10129 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10130 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10131 	if (!event.msg_buf)
10132 		return;
10133 
10134 	do {
10135 		ret = i40e_clean_arq_element(hw, &event, &pending);
10136 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10137 			break;
10138 		else if (ret) {
10139 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10140 			break;
10141 		}
10142 
10143 		opcode = le16_to_cpu(event.desc.opcode);
10144 		switch (opcode) {
10145 
10146 		case i40e_aqc_opc_get_link_status:
10147 			rtnl_lock();
10148 			i40e_handle_link_event(pf, &event);
10149 			rtnl_unlock();
10150 			break;
10151 		case i40e_aqc_opc_send_msg_to_pf:
10152 			ret = i40e_vc_process_vf_msg(pf,
10153 					le16_to_cpu(event.desc.retval),
10154 					le32_to_cpu(event.desc.cookie_high),
10155 					le32_to_cpu(event.desc.cookie_low),
10156 					event.msg_buf,
10157 					event.msg_len);
10158 			break;
10159 		case i40e_aqc_opc_lldp_update_mib:
10160 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10161 #ifdef CONFIG_I40E_DCB
10162 			rtnl_lock();
10163 			i40e_handle_lldp_event(pf, &event);
10164 			rtnl_unlock();
10165 #endif /* CONFIG_I40E_DCB */
10166 			break;
10167 		case i40e_aqc_opc_event_lan_overflow:
10168 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10169 			i40e_handle_lan_overflow_event(pf, &event);
10170 			break;
10171 		case i40e_aqc_opc_send_msg_to_peer:
10172 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10173 			break;
10174 		case i40e_aqc_opc_nvm_erase:
10175 		case i40e_aqc_opc_nvm_update:
10176 		case i40e_aqc_opc_oem_post_update:
10177 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10178 				   "ARQ NVM operation 0x%04x completed\n",
10179 				   opcode);
10180 			break;
10181 		default:
10182 			dev_info(&pf->pdev->dev,
10183 				 "ARQ: Unknown event 0x%04x ignored\n",
10184 				 opcode);
10185 			break;
10186 		}
10187 	} while (i++ < pf->adminq_work_limit);
10188 
10189 	if (i < pf->adminq_work_limit)
10190 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10191 
10192 	/* re-enable Admin queue interrupt cause */
10193 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10194 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10195 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10196 	i40e_flush(hw);
10197 
10198 	kfree(event.msg_buf);
10199 }
10200 
10201 /**
10202  * i40e_verify_eeprom - make sure eeprom is good to use
10203  * @pf: board private structure
10204  **/
10205 static void i40e_verify_eeprom(struct i40e_pf *pf)
10206 {
10207 	int err;
10208 
10209 	err = i40e_diag_eeprom_test(&pf->hw);
10210 	if (err) {
10211 		/* retry in case of garbage read */
10212 		err = i40e_diag_eeprom_test(&pf->hw);
10213 		if (err) {
10214 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10215 				 err);
10216 			set_bit(__I40E_BAD_EEPROM, pf->state);
10217 		}
10218 	}
10219 
10220 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10221 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10222 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10223 	}
10224 }
10225 
10226 /**
10227  * i40e_enable_pf_switch_lb
10228  * @pf: pointer to the PF structure
10229  *
10230  * enable switch loop back or die - no point in a return value
10231  **/
10232 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10233 {
10234 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10235 	struct i40e_vsi_context ctxt;
10236 	int ret;
10237 
10238 	ctxt.seid = pf->main_vsi_seid;
10239 	ctxt.pf_num = pf->hw.pf_id;
10240 	ctxt.vf_num = 0;
10241 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10242 	if (ret) {
10243 		dev_info(&pf->pdev->dev,
10244 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10245 			 i40e_stat_str(&pf->hw, ret),
10246 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10247 		return;
10248 	}
10249 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10250 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10251 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10252 
10253 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10254 	if (ret) {
10255 		dev_info(&pf->pdev->dev,
10256 			 "update vsi switch failed, err %s aq_err %s\n",
10257 			 i40e_stat_str(&pf->hw, ret),
10258 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10259 	}
10260 }
10261 
10262 /**
10263  * i40e_disable_pf_switch_lb
10264  * @pf: pointer to the PF structure
10265  *
10266  * disable switch loop back or die - no point in a return value
10267  **/
10268 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10269 {
10270 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10271 	struct i40e_vsi_context ctxt;
10272 	int ret;
10273 
10274 	ctxt.seid = pf->main_vsi_seid;
10275 	ctxt.pf_num = pf->hw.pf_id;
10276 	ctxt.vf_num = 0;
10277 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10278 	if (ret) {
10279 		dev_info(&pf->pdev->dev,
10280 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10281 			 i40e_stat_str(&pf->hw, ret),
10282 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10283 		return;
10284 	}
10285 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10286 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10287 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10288 
10289 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10290 	if (ret) {
10291 		dev_info(&pf->pdev->dev,
10292 			 "update vsi switch failed, err %s aq_err %s\n",
10293 			 i40e_stat_str(&pf->hw, ret),
10294 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10295 	}
10296 }
10297 
10298 /**
10299  * i40e_config_bridge_mode - Configure the HW bridge mode
10300  * @veb: pointer to the bridge instance
10301  *
10302  * Configure the loop back mode for the LAN VSI that is downlink to the
10303  * specified HW bridge instance. It is expected this function is called
10304  * when a new HW bridge is instantiated.
10305  **/
10306 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10307 {
10308 	struct i40e_pf *pf = veb->pf;
10309 
10310 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10311 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10312 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10313 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10314 		i40e_disable_pf_switch_lb(pf);
10315 	else
10316 		i40e_enable_pf_switch_lb(pf);
10317 }
10318 
10319 /**
10320  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10321  * @veb: pointer to the VEB instance
10322  *
10323  * This is a recursive function that first builds the attached VSIs then
10324  * recurses in to build the next layer of VEB.  We track the connections
10325  * through our own index numbers because the seid's from the HW could
10326  * change across the reset.
10327  **/
10328 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10329 {
10330 	struct i40e_vsi *ctl_vsi = NULL;
10331 	struct i40e_pf *pf = veb->pf;
10332 	int v, veb_idx;
10333 	int ret;
10334 
10335 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10336 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10337 		if (pf->vsi[v] &&
10338 		    pf->vsi[v]->veb_idx == veb->idx &&
10339 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10340 			ctl_vsi = pf->vsi[v];
10341 			break;
10342 		}
10343 	}
10344 	if (!ctl_vsi) {
10345 		dev_info(&pf->pdev->dev,
10346 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10347 		ret = -ENOENT;
10348 		goto end_reconstitute;
10349 	}
10350 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10351 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10352 	ret = i40e_add_vsi(ctl_vsi);
10353 	if (ret) {
10354 		dev_info(&pf->pdev->dev,
10355 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10356 			 veb->idx, ret);
10357 		goto end_reconstitute;
10358 	}
10359 	i40e_vsi_reset_stats(ctl_vsi);
10360 
10361 	/* create the VEB in the switch and move the VSI onto the VEB */
10362 	ret = i40e_add_veb(veb, ctl_vsi);
10363 	if (ret)
10364 		goto end_reconstitute;
10365 
10366 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10367 		veb->bridge_mode = BRIDGE_MODE_VEB;
10368 	else
10369 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10370 	i40e_config_bridge_mode(veb);
10371 
10372 	/* create the remaining VSIs attached to this VEB */
10373 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10374 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10375 			continue;
10376 
10377 		if (pf->vsi[v]->veb_idx == veb->idx) {
10378 			struct i40e_vsi *vsi = pf->vsi[v];
10379 
10380 			vsi->uplink_seid = veb->seid;
10381 			ret = i40e_add_vsi(vsi);
10382 			if (ret) {
10383 				dev_info(&pf->pdev->dev,
10384 					 "rebuild of vsi_idx %d failed: %d\n",
10385 					 v, ret);
10386 				goto end_reconstitute;
10387 			}
10388 			i40e_vsi_reset_stats(vsi);
10389 		}
10390 	}
10391 
10392 	/* create any VEBs attached to this VEB - RECURSION */
10393 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10394 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10395 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10396 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10397 			if (ret)
10398 				break;
10399 		}
10400 	}
10401 
10402 end_reconstitute:
10403 	return ret;
10404 }
10405 
10406 /**
10407  * i40e_get_capabilities - get info about the HW
10408  * @pf: the PF struct
10409  * @list_type: AQ capability to be queried
10410  **/
10411 static int i40e_get_capabilities(struct i40e_pf *pf,
10412 				 enum i40e_admin_queue_opc list_type)
10413 {
10414 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10415 	u16 data_size;
10416 	int buf_len;
10417 	int err;
10418 
10419 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10420 	do {
10421 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10422 		if (!cap_buf)
10423 			return -ENOMEM;
10424 
10425 		/* this loads the data into the hw struct for us */
10426 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10427 						    &data_size, list_type,
10428 						    NULL);
10429 		/* data loaded, buffer no longer needed */
10430 		kfree(cap_buf);
10431 
10432 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10433 			/* retry with a larger buffer */
10434 			buf_len = data_size;
10435 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10436 			dev_info(&pf->pdev->dev,
10437 				 "capability discovery failed, err %s aq_err %s\n",
10438 				 i40e_stat_str(&pf->hw, err),
10439 				 i40e_aq_str(&pf->hw,
10440 					     pf->hw.aq.asq_last_status));
10441 			return -ENODEV;
10442 		}
10443 	} while (err);
10444 
10445 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10446 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10447 			dev_info(&pf->pdev->dev,
10448 				 "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",
10449 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10450 				 pf->hw.func_caps.num_msix_vectors,
10451 				 pf->hw.func_caps.num_msix_vectors_vf,
10452 				 pf->hw.func_caps.fd_filters_guaranteed,
10453 				 pf->hw.func_caps.fd_filters_best_effort,
10454 				 pf->hw.func_caps.num_tx_qp,
10455 				 pf->hw.func_caps.num_vsis);
10456 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10457 			dev_info(&pf->pdev->dev,
10458 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10459 				 pf->hw.dev_caps.switch_mode,
10460 				 pf->hw.dev_caps.valid_functions);
10461 			dev_info(&pf->pdev->dev,
10462 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10463 				 pf->hw.dev_caps.sr_iov_1_1,
10464 				 pf->hw.dev_caps.num_vfs);
10465 			dev_info(&pf->pdev->dev,
10466 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10467 				 pf->hw.dev_caps.num_vsis,
10468 				 pf->hw.dev_caps.num_rx_qp,
10469 				 pf->hw.dev_caps.num_tx_qp);
10470 		}
10471 	}
10472 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10473 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10474 		       + pf->hw.func_caps.num_vfs)
10475 		if (pf->hw.revision_id == 0 &&
10476 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10477 			dev_info(&pf->pdev->dev,
10478 				 "got num_vsis %d, setting num_vsis to %d\n",
10479 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10480 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10481 		}
10482 	}
10483 	return 0;
10484 }
10485 
10486 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10487 
10488 /**
10489  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10490  * @pf: board private structure
10491  **/
10492 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10493 {
10494 	struct i40e_vsi *vsi;
10495 
10496 	/* quick workaround for an NVM issue that leaves a critical register
10497 	 * uninitialized
10498 	 */
10499 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10500 		static const u32 hkey[] = {
10501 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10502 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10503 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10504 			0x95b3a76d};
10505 		int i;
10506 
10507 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10508 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10509 	}
10510 
10511 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10512 		return;
10513 
10514 	/* find existing VSI and see if it needs configuring */
10515 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10516 
10517 	/* create a new VSI if none exists */
10518 	if (!vsi) {
10519 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10520 				     pf->vsi[pf->lan_vsi]->seid, 0);
10521 		if (!vsi) {
10522 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10523 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10524 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10525 			return;
10526 		}
10527 	}
10528 
10529 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10530 }
10531 
10532 /**
10533  * i40e_fdir_teardown - release the Flow Director resources
10534  * @pf: board private structure
10535  **/
10536 static void i40e_fdir_teardown(struct i40e_pf *pf)
10537 {
10538 	struct i40e_vsi *vsi;
10539 
10540 	i40e_fdir_filter_exit(pf);
10541 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10542 	if (vsi)
10543 		i40e_vsi_release(vsi);
10544 }
10545 
10546 /**
10547  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10548  * @vsi: PF main vsi
10549  * @seid: seid of main or channel VSIs
10550  *
10551  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10552  * existed before reset
10553  **/
10554 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10555 {
10556 	struct i40e_cloud_filter *cfilter;
10557 	struct i40e_pf *pf = vsi->back;
10558 	struct hlist_node *node;
10559 	i40e_status ret;
10560 
10561 	/* Add cloud filters back if they exist */
10562 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10563 				  cloud_node) {
10564 		if (cfilter->seid != seid)
10565 			continue;
10566 
10567 		if (cfilter->dst_port)
10568 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10569 								true);
10570 		else
10571 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10572 
10573 		if (ret) {
10574 			dev_dbg(&pf->pdev->dev,
10575 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10576 				i40e_stat_str(&pf->hw, ret),
10577 				i40e_aq_str(&pf->hw,
10578 					    pf->hw.aq.asq_last_status));
10579 			return ret;
10580 		}
10581 	}
10582 	return 0;
10583 }
10584 
10585 /**
10586  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10587  * @vsi: PF main vsi
10588  *
10589  * Rebuilds channel VSIs if they existed before reset
10590  **/
10591 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10592 {
10593 	struct i40e_channel *ch, *ch_tmp;
10594 	i40e_status ret;
10595 
10596 	if (list_empty(&vsi->ch_list))
10597 		return 0;
10598 
10599 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10600 		if (!ch->initialized)
10601 			break;
10602 		/* Proceed with creation of channel (VMDq2) VSI */
10603 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10604 		if (ret) {
10605 			dev_info(&vsi->back->pdev->dev,
10606 				 "failed to rebuild channels using uplink_seid %u\n",
10607 				 vsi->uplink_seid);
10608 			return ret;
10609 		}
10610 		/* Reconfigure TX queues using QTX_CTL register */
10611 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10612 		if (ret) {
10613 			dev_info(&vsi->back->pdev->dev,
10614 				 "failed to configure TX rings for channel %u\n",
10615 				 ch->seid);
10616 			return ret;
10617 		}
10618 		/* update 'next_base_queue' */
10619 		vsi->next_base_queue = vsi->next_base_queue +
10620 							ch->num_queue_pairs;
10621 		if (ch->max_tx_rate) {
10622 			u64 credits = ch->max_tx_rate;
10623 
10624 			if (i40e_set_bw_limit(vsi, ch->seid,
10625 					      ch->max_tx_rate))
10626 				return -EINVAL;
10627 
10628 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10629 			dev_dbg(&vsi->back->pdev->dev,
10630 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10631 				ch->max_tx_rate,
10632 				credits,
10633 				ch->seid);
10634 		}
10635 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10636 		if (ret) {
10637 			dev_dbg(&vsi->back->pdev->dev,
10638 				"Failed to rebuild cloud filters for channel VSI %u\n",
10639 				ch->seid);
10640 			return ret;
10641 		}
10642 	}
10643 	return 0;
10644 }
10645 
10646 /**
10647  * i40e_prep_for_reset - prep for the core to reset
10648  * @pf: board private structure
10649  *
10650  * Close up the VFs and other things in prep for PF Reset.
10651   **/
10652 static void i40e_prep_for_reset(struct i40e_pf *pf)
10653 {
10654 	struct i40e_hw *hw = &pf->hw;
10655 	i40e_status ret = 0;
10656 	u32 v;
10657 
10658 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10659 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10660 		return;
10661 	if (i40e_check_asq_alive(&pf->hw))
10662 		i40e_vc_notify_reset(pf);
10663 
10664 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10665 
10666 	/* quiesce the VSIs and their queues that are not already DOWN */
10667 	i40e_pf_quiesce_all_vsi(pf);
10668 
10669 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10670 		if (pf->vsi[v])
10671 			pf->vsi[v]->seid = 0;
10672 	}
10673 
10674 	i40e_shutdown_adminq(&pf->hw);
10675 
10676 	/* call shutdown HMC */
10677 	if (hw->hmc.hmc_obj) {
10678 		ret = i40e_shutdown_lan_hmc(hw);
10679 		if (ret)
10680 			dev_warn(&pf->pdev->dev,
10681 				 "shutdown_lan_hmc failed: %d\n", ret);
10682 	}
10683 
10684 	/* Save the current PTP time so that we can restore the time after the
10685 	 * reset completes.
10686 	 */
10687 	i40e_ptp_save_hw_time(pf);
10688 }
10689 
10690 /**
10691  * i40e_send_version - update firmware with driver version
10692  * @pf: PF struct
10693  */
10694 static void i40e_send_version(struct i40e_pf *pf)
10695 {
10696 	struct i40e_driver_version dv;
10697 
10698 	dv.major_version = 0xff;
10699 	dv.minor_version = 0xff;
10700 	dv.build_version = 0xff;
10701 	dv.subbuild_version = 0;
10702 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10703 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10704 }
10705 
10706 /**
10707  * i40e_get_oem_version - get OEM specific version information
10708  * @hw: pointer to the hardware structure
10709  **/
10710 static void i40e_get_oem_version(struct i40e_hw *hw)
10711 {
10712 	u16 block_offset = 0xffff;
10713 	u16 block_length = 0;
10714 	u16 capabilities = 0;
10715 	u16 gen_snap = 0;
10716 	u16 release = 0;
10717 
10718 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10719 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10720 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10721 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10722 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10723 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10724 #define I40E_NVM_OEM_LENGTH			3
10725 
10726 	/* Check if pointer to OEM version block is valid. */
10727 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10728 	if (block_offset == 0xffff)
10729 		return;
10730 
10731 	/* Check if OEM version block has correct length. */
10732 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10733 			   &block_length);
10734 	if (block_length < I40E_NVM_OEM_LENGTH)
10735 		return;
10736 
10737 	/* Check if OEM version format is as expected. */
10738 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10739 			   &capabilities);
10740 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10741 		return;
10742 
10743 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10744 			   &gen_snap);
10745 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10746 			   &release);
10747 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10748 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10749 }
10750 
10751 /**
10752  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10753  * @pf: board private structure
10754  **/
10755 static int i40e_reset(struct i40e_pf *pf)
10756 {
10757 	struct i40e_hw *hw = &pf->hw;
10758 	i40e_status ret;
10759 
10760 	ret = i40e_pf_reset(hw);
10761 	if (ret) {
10762 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10763 		set_bit(__I40E_RESET_FAILED, pf->state);
10764 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10765 	} else {
10766 		pf->pfr_count++;
10767 	}
10768 	return ret;
10769 }
10770 
10771 /**
10772  * i40e_rebuild - rebuild using a saved config
10773  * @pf: board private structure
10774  * @reinit: if the Main VSI needs to re-initialized.
10775  * @lock_acquired: indicates whether or not the lock has been acquired
10776  * before this function was called.
10777  **/
10778 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10779 {
10780 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10781 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10782 	struct i40e_hw *hw = &pf->hw;
10783 	i40e_status ret;
10784 	u32 val;
10785 	int v;
10786 
10787 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10788 	    is_recovery_mode_reported)
10789 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10790 
10791 	if (test_bit(__I40E_DOWN, pf->state) &&
10792 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10793 		goto clear_recovery;
10794 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10795 
10796 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10797 	ret = i40e_init_adminq(&pf->hw);
10798 	if (ret) {
10799 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10800 			 i40e_stat_str(&pf->hw, ret),
10801 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10802 		goto clear_recovery;
10803 	}
10804 	i40e_get_oem_version(&pf->hw);
10805 
10806 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10807 		/* The following delay is necessary for firmware update. */
10808 		mdelay(1000);
10809 	}
10810 
10811 	/* re-verify the eeprom if we just had an EMP reset */
10812 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10813 		i40e_verify_eeprom(pf);
10814 
10815 	/* if we are going out of or into recovery mode we have to act
10816 	 * accordingly with regard to resources initialization
10817 	 * and deinitialization
10818 	 */
10819 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10820 		if (i40e_get_capabilities(pf,
10821 					  i40e_aqc_opc_list_func_capabilities))
10822 			goto end_unlock;
10823 
10824 		if (is_recovery_mode_reported) {
10825 			/* we're staying in recovery mode so we'll reinitialize
10826 			 * misc vector here
10827 			 */
10828 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10829 				goto end_unlock;
10830 		} else {
10831 			if (!lock_acquired)
10832 				rtnl_lock();
10833 			/* we're going out of recovery mode so we'll free
10834 			 * the IRQ allocated specifically for recovery mode
10835 			 * and restore the interrupt scheme
10836 			 */
10837 			free_irq(pf->pdev->irq, pf);
10838 			i40e_clear_interrupt_scheme(pf);
10839 			if (i40e_restore_interrupt_scheme(pf))
10840 				goto end_unlock;
10841 		}
10842 
10843 		/* tell the firmware that we're starting */
10844 		i40e_send_version(pf);
10845 
10846 		/* bail out in case recovery mode was detected, as there is
10847 		 * no need for further configuration.
10848 		 */
10849 		goto end_unlock;
10850 	}
10851 
10852 	i40e_clear_pxe_mode(hw);
10853 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10854 	if (ret)
10855 		goto end_core_reset;
10856 
10857 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10858 				hw->func_caps.num_rx_qp, 0, 0);
10859 	if (ret) {
10860 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10861 		goto end_core_reset;
10862 	}
10863 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10864 	if (ret) {
10865 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10866 		goto end_core_reset;
10867 	}
10868 
10869 #ifdef CONFIG_I40E_DCB
10870 	/* Enable FW to write a default DCB config on link-up
10871 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10872 	 * is not supported with new link speed
10873 	 */
10874 	if (i40e_is_tc_mqprio_enabled(pf)) {
10875 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10876 	} else {
10877 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10878 		    (hw->phy.link_info.link_speed &
10879 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10880 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10881 			dev_warn(&pf->pdev->dev,
10882 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10883 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10884 		} else {
10885 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10886 			ret = i40e_init_pf_dcb(pf);
10887 			if (ret) {
10888 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10889 					 ret);
10890 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10891 				/* Continue without DCB enabled */
10892 			}
10893 		}
10894 	}
10895 
10896 #endif /* CONFIG_I40E_DCB */
10897 	if (!lock_acquired)
10898 		rtnl_lock();
10899 	ret = i40e_setup_pf_switch(pf, reinit, true);
10900 	if (ret)
10901 		goto end_unlock;
10902 
10903 	/* The driver only wants link up/down and module qualification
10904 	 * reports from firmware.  Note the negative logic.
10905 	 */
10906 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10907 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10908 					 I40E_AQ_EVENT_MEDIA_NA |
10909 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10910 	if (ret)
10911 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10912 			 i40e_stat_str(&pf->hw, ret),
10913 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10914 
10915 	/* Rebuild the VSIs and VEBs that existed before reset.
10916 	 * They are still in our local switch element arrays, so only
10917 	 * need to rebuild the switch model in the HW.
10918 	 *
10919 	 * If there were VEBs but the reconstitution failed, we'll try
10920 	 * to recover minimal use by getting the basic PF VSI working.
10921 	 */
10922 	if (vsi->uplink_seid != pf->mac_seid) {
10923 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10924 		/* find the one VEB connected to the MAC, and find orphans */
10925 		for (v = 0; v < I40E_MAX_VEB; v++) {
10926 			if (!pf->veb[v])
10927 				continue;
10928 
10929 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10930 			    pf->veb[v]->uplink_seid == 0) {
10931 				ret = i40e_reconstitute_veb(pf->veb[v]);
10932 
10933 				if (!ret)
10934 					continue;
10935 
10936 				/* If Main VEB failed, we're in deep doodoo,
10937 				 * so give up rebuilding the switch and set up
10938 				 * for minimal rebuild of PF VSI.
10939 				 * If orphan failed, we'll report the error
10940 				 * but try to keep going.
10941 				 */
10942 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10943 					dev_info(&pf->pdev->dev,
10944 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10945 						 ret);
10946 					vsi->uplink_seid = pf->mac_seid;
10947 					break;
10948 				} else if (pf->veb[v]->uplink_seid == 0) {
10949 					dev_info(&pf->pdev->dev,
10950 						 "rebuild of orphan VEB failed: %d\n",
10951 						 ret);
10952 				}
10953 			}
10954 		}
10955 	}
10956 
10957 	if (vsi->uplink_seid == pf->mac_seid) {
10958 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10959 		/* no VEB, so rebuild only the Main VSI */
10960 		ret = i40e_add_vsi(vsi);
10961 		if (ret) {
10962 			dev_info(&pf->pdev->dev,
10963 				 "rebuild of Main VSI failed: %d\n", ret);
10964 			goto end_unlock;
10965 		}
10966 	}
10967 
10968 	if (vsi->mqprio_qopt.max_rate[0]) {
10969 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10970 		u64 credits = 0;
10971 
10972 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10973 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10974 		if (ret)
10975 			goto end_unlock;
10976 
10977 		credits = max_tx_rate;
10978 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10979 		dev_dbg(&vsi->back->pdev->dev,
10980 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10981 			max_tx_rate,
10982 			credits,
10983 			vsi->seid);
10984 	}
10985 
10986 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10987 	if (ret)
10988 		goto end_unlock;
10989 
10990 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10991 	 * for this main VSI if they exist
10992 	 */
10993 	ret = i40e_rebuild_channels(vsi);
10994 	if (ret)
10995 		goto end_unlock;
10996 
10997 	/* Reconfigure hardware for allowing smaller MSS in the case
10998 	 * of TSO, so that we avoid the MDD being fired and causing
10999 	 * a reset in the case of small MSS+TSO.
11000 	 */
11001 #define I40E_REG_MSS          0x000E64DC
11002 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11003 #define I40E_64BYTE_MSS       0x400000
11004 	val = rd32(hw, I40E_REG_MSS);
11005 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11006 		val &= ~I40E_REG_MSS_MIN_MASK;
11007 		val |= I40E_64BYTE_MSS;
11008 		wr32(hw, I40E_REG_MSS, val);
11009 	}
11010 
11011 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11012 		msleep(75);
11013 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11014 		if (ret)
11015 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11016 				 i40e_stat_str(&pf->hw, ret),
11017 				 i40e_aq_str(&pf->hw,
11018 					     pf->hw.aq.asq_last_status));
11019 	}
11020 	/* reinit the misc interrupt */
11021 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11022 		ret = i40e_setup_misc_vector(pf);
11023 
11024 	/* Add a filter to drop all Flow control frames from any VSI from being
11025 	 * transmitted. By doing so we stop a malicious VF from sending out
11026 	 * PAUSE or PFC frames and potentially controlling traffic for other
11027 	 * PF/VF VSIs.
11028 	 * The FW can still send Flow control frames if enabled.
11029 	 */
11030 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11031 						       pf->main_vsi_seid);
11032 
11033 	/* restart the VSIs that were rebuilt and running before the reset */
11034 	i40e_pf_unquiesce_all_vsi(pf);
11035 
11036 	/* Release the RTNL lock before we start resetting VFs */
11037 	if (!lock_acquired)
11038 		rtnl_unlock();
11039 
11040 	/* Restore promiscuous settings */
11041 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11042 	if (ret)
11043 		dev_warn(&pf->pdev->dev,
11044 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
11045 			 pf->cur_promisc ? "on" : "off",
11046 			 i40e_stat_str(&pf->hw, ret),
11047 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11048 
11049 	i40e_reset_all_vfs(pf, true);
11050 
11051 	/* tell the firmware that we're starting */
11052 	i40e_send_version(pf);
11053 
11054 	/* We've already released the lock, so don't do it again */
11055 	goto end_core_reset;
11056 
11057 end_unlock:
11058 	if (!lock_acquired)
11059 		rtnl_unlock();
11060 end_core_reset:
11061 	clear_bit(__I40E_RESET_FAILED, pf->state);
11062 clear_recovery:
11063 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11064 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11065 }
11066 
11067 /**
11068  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11069  * @pf: board private structure
11070  * @reinit: if the Main VSI needs to re-initialized.
11071  * @lock_acquired: indicates whether or not the lock has been acquired
11072  * before this function was called.
11073  **/
11074 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11075 				   bool lock_acquired)
11076 {
11077 	int ret;
11078 
11079 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11080 		return;
11081 	/* Now we wait for GRST to settle out.
11082 	 * We don't have to delete the VEBs or VSIs from the hw switch
11083 	 * because the reset will make them disappear.
11084 	 */
11085 	ret = i40e_reset(pf);
11086 	if (!ret)
11087 		i40e_rebuild(pf, reinit, lock_acquired);
11088 }
11089 
11090 /**
11091  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11092  * @pf: board private structure
11093  *
11094  * Close up the VFs and other things in prep for a Core Reset,
11095  * then get ready to rebuild the world.
11096  * @lock_acquired: indicates whether or not the lock has been acquired
11097  * before this function was called.
11098  **/
11099 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11100 {
11101 	i40e_prep_for_reset(pf);
11102 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11103 }
11104 
11105 /**
11106  * i40e_handle_mdd_event
11107  * @pf: pointer to the PF structure
11108  *
11109  * Called from the MDD irq handler to identify possibly malicious vfs
11110  **/
11111 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11112 {
11113 	struct i40e_hw *hw = &pf->hw;
11114 	bool mdd_detected = false;
11115 	struct i40e_vf *vf;
11116 	u32 reg;
11117 	int i;
11118 
11119 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11120 		return;
11121 
11122 	/* find what triggered the MDD event */
11123 	reg = rd32(hw, I40E_GL_MDET_TX);
11124 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11125 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11126 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11127 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11128 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11129 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11130 				I40E_GL_MDET_TX_EVENT_SHIFT;
11131 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11132 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11133 				pf->hw.func_caps.base_queue;
11134 		if (netif_msg_tx_err(pf))
11135 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11136 				 event, queue, pf_num, vf_num);
11137 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11138 		mdd_detected = true;
11139 	}
11140 	reg = rd32(hw, I40E_GL_MDET_RX);
11141 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11142 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11143 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11144 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11145 				I40E_GL_MDET_RX_EVENT_SHIFT;
11146 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11147 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11148 				pf->hw.func_caps.base_queue;
11149 		if (netif_msg_rx_err(pf))
11150 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11151 				 event, queue, func);
11152 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11153 		mdd_detected = true;
11154 	}
11155 
11156 	if (mdd_detected) {
11157 		reg = rd32(hw, I40E_PF_MDET_TX);
11158 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11159 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11160 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11161 		}
11162 		reg = rd32(hw, I40E_PF_MDET_RX);
11163 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11164 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11165 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11166 		}
11167 	}
11168 
11169 	/* see if one of the VFs needs its hand slapped */
11170 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11171 		vf = &(pf->vf[i]);
11172 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11173 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11174 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11175 			vf->num_mdd_events++;
11176 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11177 				 i);
11178 			dev_info(&pf->pdev->dev,
11179 				 "Use PF Control I/F to re-enable the VF\n");
11180 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11181 		}
11182 
11183 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11184 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11185 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11186 			vf->num_mdd_events++;
11187 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11188 				 i);
11189 			dev_info(&pf->pdev->dev,
11190 				 "Use PF Control I/F to re-enable the VF\n");
11191 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11192 		}
11193 	}
11194 
11195 	/* re-enable mdd interrupt cause */
11196 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11197 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11198 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11199 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11200 	i40e_flush(hw);
11201 }
11202 
11203 /**
11204  * i40e_service_task - Run the driver's async subtasks
11205  * @work: pointer to work_struct containing our data
11206  **/
11207 static void i40e_service_task(struct work_struct *work)
11208 {
11209 	struct i40e_pf *pf = container_of(work,
11210 					  struct i40e_pf,
11211 					  service_task);
11212 	unsigned long start_time = jiffies;
11213 
11214 	/* don't bother with service tasks if a reset is in progress */
11215 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11216 	    test_bit(__I40E_SUSPENDED, pf->state))
11217 		return;
11218 
11219 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11220 		return;
11221 
11222 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11223 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11224 		i40e_sync_filters_subtask(pf);
11225 		i40e_reset_subtask(pf);
11226 		i40e_handle_mdd_event(pf);
11227 		i40e_vc_process_vflr_event(pf);
11228 		i40e_watchdog_subtask(pf);
11229 		i40e_fdir_reinit_subtask(pf);
11230 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11231 			/* Client subtask will reopen next time through. */
11232 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11233 							   true);
11234 		} else {
11235 			i40e_client_subtask(pf);
11236 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11237 					       pf->state))
11238 				i40e_notify_client_of_l2_param_changes(
11239 								pf->vsi[pf->lan_vsi]);
11240 		}
11241 		i40e_sync_filters_subtask(pf);
11242 	} else {
11243 		i40e_reset_subtask(pf);
11244 	}
11245 
11246 	i40e_clean_adminq_subtask(pf);
11247 
11248 	/* flush memory to make sure state is correct before next watchdog */
11249 	smp_mb__before_atomic();
11250 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11251 
11252 	/* If the tasks have taken longer than one timer cycle or there
11253 	 * is more work to be done, reschedule the service task now
11254 	 * rather than wait for the timer to tick again.
11255 	 */
11256 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11257 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11258 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11259 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11260 		i40e_service_event_schedule(pf);
11261 }
11262 
11263 /**
11264  * i40e_service_timer - timer callback
11265  * @t: timer list pointer
11266  **/
11267 static void i40e_service_timer(struct timer_list *t)
11268 {
11269 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11270 
11271 	mod_timer(&pf->service_timer,
11272 		  round_jiffies(jiffies + pf->service_timer_period));
11273 	i40e_service_event_schedule(pf);
11274 }
11275 
11276 /**
11277  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11278  * @vsi: the VSI being configured
11279  **/
11280 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11281 {
11282 	struct i40e_pf *pf = vsi->back;
11283 
11284 	switch (vsi->type) {
11285 	case I40E_VSI_MAIN:
11286 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11287 		if (!vsi->num_tx_desc)
11288 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11289 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11290 		if (!vsi->num_rx_desc)
11291 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11292 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11293 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11294 			vsi->num_q_vectors = pf->num_lan_msix;
11295 		else
11296 			vsi->num_q_vectors = 1;
11297 
11298 		break;
11299 
11300 	case I40E_VSI_FDIR:
11301 		vsi->alloc_queue_pairs = 1;
11302 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11303 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11304 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11305 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11306 		vsi->num_q_vectors = pf->num_fdsb_msix;
11307 		break;
11308 
11309 	case I40E_VSI_VMDQ2:
11310 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11311 		if (!vsi->num_tx_desc)
11312 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11313 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11314 		if (!vsi->num_rx_desc)
11315 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11316 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11317 		vsi->num_q_vectors = pf->num_vmdq_msix;
11318 		break;
11319 
11320 	case I40E_VSI_SRIOV:
11321 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11322 		if (!vsi->num_tx_desc)
11323 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11324 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11325 		if (!vsi->num_rx_desc)
11326 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11327 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11328 		break;
11329 
11330 	default:
11331 		WARN_ON(1);
11332 		return -ENODATA;
11333 	}
11334 
11335 	if (is_kdump_kernel()) {
11336 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11337 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11338 	}
11339 
11340 	return 0;
11341 }
11342 
11343 /**
11344  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11345  * @vsi: VSI pointer
11346  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11347  *
11348  * On error: returns error code (negative)
11349  * On success: returns 0
11350  **/
11351 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11352 {
11353 	struct i40e_ring **next_rings;
11354 	int size;
11355 	int ret = 0;
11356 
11357 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11358 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11359 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11360 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11361 	if (!vsi->tx_rings)
11362 		return -ENOMEM;
11363 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11364 	if (i40e_enabled_xdp_vsi(vsi)) {
11365 		vsi->xdp_rings = next_rings;
11366 		next_rings += vsi->alloc_queue_pairs;
11367 	}
11368 	vsi->rx_rings = next_rings;
11369 
11370 	if (alloc_qvectors) {
11371 		/* allocate memory for q_vector pointers */
11372 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11373 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11374 		if (!vsi->q_vectors) {
11375 			ret = -ENOMEM;
11376 			goto err_vectors;
11377 		}
11378 	}
11379 	return ret;
11380 
11381 err_vectors:
11382 	kfree(vsi->tx_rings);
11383 	return ret;
11384 }
11385 
11386 /**
11387  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11388  * @pf: board private structure
11389  * @type: type of VSI
11390  *
11391  * On error: returns error code (negative)
11392  * On success: returns vsi index in PF (positive)
11393  **/
11394 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11395 {
11396 	int ret = -ENODEV;
11397 	struct i40e_vsi *vsi;
11398 	int vsi_idx;
11399 	int i;
11400 
11401 	/* Need to protect the allocation of the VSIs at the PF level */
11402 	mutex_lock(&pf->switch_mutex);
11403 
11404 	/* VSI list may be fragmented if VSI creation/destruction has
11405 	 * been happening.  We can afford to do a quick scan to look
11406 	 * for any free VSIs in the list.
11407 	 *
11408 	 * find next empty vsi slot, looping back around if necessary
11409 	 */
11410 	i = pf->next_vsi;
11411 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11412 		i++;
11413 	if (i >= pf->num_alloc_vsi) {
11414 		i = 0;
11415 		while (i < pf->next_vsi && pf->vsi[i])
11416 			i++;
11417 	}
11418 
11419 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11420 		vsi_idx = i;             /* Found one! */
11421 	} else {
11422 		ret = -ENODEV;
11423 		goto unlock_pf;  /* out of VSI slots! */
11424 	}
11425 	pf->next_vsi = ++i;
11426 
11427 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11428 	if (!vsi) {
11429 		ret = -ENOMEM;
11430 		goto unlock_pf;
11431 	}
11432 	vsi->type = type;
11433 	vsi->back = pf;
11434 	set_bit(__I40E_VSI_DOWN, vsi->state);
11435 	vsi->flags = 0;
11436 	vsi->idx = vsi_idx;
11437 	vsi->int_rate_limit = 0;
11438 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11439 				pf->rss_table_size : 64;
11440 	vsi->netdev_registered = false;
11441 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11442 	hash_init(vsi->mac_filter_hash);
11443 	vsi->irqs_ready = false;
11444 
11445 	if (type == I40E_VSI_MAIN) {
11446 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11447 		if (!vsi->af_xdp_zc_qps)
11448 			goto err_rings;
11449 	}
11450 
11451 	ret = i40e_set_num_rings_in_vsi(vsi);
11452 	if (ret)
11453 		goto err_rings;
11454 
11455 	ret = i40e_vsi_alloc_arrays(vsi, true);
11456 	if (ret)
11457 		goto err_rings;
11458 
11459 	/* Setup default MSIX irq handler for VSI */
11460 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11461 
11462 	/* Initialize VSI lock */
11463 	spin_lock_init(&vsi->mac_filter_hash_lock);
11464 	pf->vsi[vsi_idx] = vsi;
11465 	ret = vsi_idx;
11466 	goto unlock_pf;
11467 
11468 err_rings:
11469 	bitmap_free(vsi->af_xdp_zc_qps);
11470 	pf->next_vsi = i - 1;
11471 	kfree(vsi);
11472 unlock_pf:
11473 	mutex_unlock(&pf->switch_mutex);
11474 	return ret;
11475 }
11476 
11477 /**
11478  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11479  * @vsi: VSI pointer
11480  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11481  *
11482  * On error: returns error code (negative)
11483  * On success: returns 0
11484  **/
11485 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11486 {
11487 	/* free the ring and vector containers */
11488 	if (free_qvectors) {
11489 		kfree(vsi->q_vectors);
11490 		vsi->q_vectors = NULL;
11491 	}
11492 	kfree(vsi->tx_rings);
11493 	vsi->tx_rings = NULL;
11494 	vsi->rx_rings = NULL;
11495 	vsi->xdp_rings = NULL;
11496 }
11497 
11498 /**
11499  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11500  * and lookup table
11501  * @vsi: Pointer to VSI structure
11502  */
11503 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11504 {
11505 	if (!vsi)
11506 		return;
11507 
11508 	kfree(vsi->rss_hkey_user);
11509 	vsi->rss_hkey_user = NULL;
11510 
11511 	kfree(vsi->rss_lut_user);
11512 	vsi->rss_lut_user = NULL;
11513 }
11514 
11515 /**
11516  * i40e_vsi_clear - Deallocate the VSI provided
11517  * @vsi: the VSI being un-configured
11518  **/
11519 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11520 {
11521 	struct i40e_pf *pf;
11522 
11523 	if (!vsi)
11524 		return 0;
11525 
11526 	if (!vsi->back)
11527 		goto free_vsi;
11528 	pf = vsi->back;
11529 
11530 	mutex_lock(&pf->switch_mutex);
11531 	if (!pf->vsi[vsi->idx]) {
11532 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11533 			vsi->idx, vsi->idx, vsi->type);
11534 		goto unlock_vsi;
11535 	}
11536 
11537 	if (pf->vsi[vsi->idx] != vsi) {
11538 		dev_err(&pf->pdev->dev,
11539 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11540 			pf->vsi[vsi->idx]->idx,
11541 			pf->vsi[vsi->idx]->type,
11542 			vsi->idx, vsi->type);
11543 		goto unlock_vsi;
11544 	}
11545 
11546 	/* updates the PF for this cleared vsi */
11547 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11548 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11549 
11550 	bitmap_free(vsi->af_xdp_zc_qps);
11551 	i40e_vsi_free_arrays(vsi, true);
11552 	i40e_clear_rss_config_user(vsi);
11553 
11554 	pf->vsi[vsi->idx] = NULL;
11555 	if (vsi->idx < pf->next_vsi)
11556 		pf->next_vsi = vsi->idx;
11557 
11558 unlock_vsi:
11559 	mutex_unlock(&pf->switch_mutex);
11560 free_vsi:
11561 	kfree(vsi);
11562 
11563 	return 0;
11564 }
11565 
11566 /**
11567  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11568  * @vsi: the VSI being cleaned
11569  **/
11570 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11571 {
11572 	int i;
11573 
11574 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11575 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11576 			kfree_rcu(vsi->tx_rings[i], rcu);
11577 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11578 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11579 			if (vsi->xdp_rings)
11580 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11581 		}
11582 	}
11583 }
11584 
11585 /**
11586  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11587  * @vsi: the VSI being configured
11588  **/
11589 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11590 {
11591 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11592 	struct i40e_pf *pf = vsi->back;
11593 	struct i40e_ring *ring;
11594 
11595 	/* Set basic values in the rings to be used later during open() */
11596 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11597 		/* allocate space for both Tx and Rx in one shot */
11598 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11599 		if (!ring)
11600 			goto err_out;
11601 
11602 		ring->queue_index = i;
11603 		ring->reg_idx = vsi->base_queue + i;
11604 		ring->ring_active = false;
11605 		ring->vsi = vsi;
11606 		ring->netdev = vsi->netdev;
11607 		ring->dev = &pf->pdev->dev;
11608 		ring->count = vsi->num_tx_desc;
11609 		ring->size = 0;
11610 		ring->dcb_tc = 0;
11611 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11612 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11613 		ring->itr_setting = pf->tx_itr_default;
11614 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11615 
11616 		if (!i40e_enabled_xdp_vsi(vsi))
11617 			goto setup_rx;
11618 
11619 		ring->queue_index = vsi->alloc_queue_pairs + i;
11620 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11621 		ring->ring_active = false;
11622 		ring->vsi = vsi;
11623 		ring->netdev = NULL;
11624 		ring->dev = &pf->pdev->dev;
11625 		ring->count = vsi->num_tx_desc;
11626 		ring->size = 0;
11627 		ring->dcb_tc = 0;
11628 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11629 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11630 		set_ring_xdp(ring);
11631 		ring->itr_setting = pf->tx_itr_default;
11632 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11633 
11634 setup_rx:
11635 		ring->queue_index = i;
11636 		ring->reg_idx = vsi->base_queue + i;
11637 		ring->ring_active = false;
11638 		ring->vsi = vsi;
11639 		ring->netdev = vsi->netdev;
11640 		ring->dev = &pf->pdev->dev;
11641 		ring->count = vsi->num_rx_desc;
11642 		ring->size = 0;
11643 		ring->dcb_tc = 0;
11644 		ring->itr_setting = pf->rx_itr_default;
11645 		WRITE_ONCE(vsi->rx_rings[i], ring);
11646 	}
11647 
11648 	return 0;
11649 
11650 err_out:
11651 	i40e_vsi_clear_rings(vsi);
11652 	return -ENOMEM;
11653 }
11654 
11655 /**
11656  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11657  * @pf: board private structure
11658  * @vectors: the number of MSI-X vectors to request
11659  *
11660  * Returns the number of vectors reserved, or error
11661  **/
11662 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11663 {
11664 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11665 					I40E_MIN_MSIX, vectors);
11666 	if (vectors < 0) {
11667 		dev_info(&pf->pdev->dev,
11668 			 "MSI-X vector reservation failed: %d\n", vectors);
11669 		vectors = 0;
11670 	}
11671 
11672 	return vectors;
11673 }
11674 
11675 /**
11676  * i40e_init_msix - Setup the MSIX capability
11677  * @pf: board private structure
11678  *
11679  * Work with the OS to set up the MSIX vectors needed.
11680  *
11681  * Returns the number of vectors reserved or negative on failure
11682  **/
11683 static int i40e_init_msix(struct i40e_pf *pf)
11684 {
11685 	struct i40e_hw *hw = &pf->hw;
11686 	int cpus, extra_vectors;
11687 	int vectors_left;
11688 	int v_budget, i;
11689 	int v_actual;
11690 	int iwarp_requested = 0;
11691 
11692 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11693 		return -ENODEV;
11694 
11695 	/* The number of vectors we'll request will be comprised of:
11696 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11697 	 *   - The number of LAN queue pairs
11698 	 *	- Queues being used for RSS.
11699 	 *		We don't need as many as max_rss_size vectors.
11700 	 *		use rss_size instead in the calculation since that
11701 	 *		is governed by number of cpus in the system.
11702 	 *	- assumes symmetric Tx/Rx pairing
11703 	 *   - The number of VMDq pairs
11704 	 *   - The CPU count within the NUMA node if iWARP is enabled
11705 	 * Once we count this up, try the request.
11706 	 *
11707 	 * If we can't get what we want, we'll simplify to nearly nothing
11708 	 * and try again.  If that still fails, we punt.
11709 	 */
11710 	vectors_left = hw->func_caps.num_msix_vectors;
11711 	v_budget = 0;
11712 
11713 	/* reserve one vector for miscellaneous handler */
11714 	if (vectors_left) {
11715 		v_budget++;
11716 		vectors_left--;
11717 	}
11718 
11719 	/* reserve some vectors for the main PF traffic queues. Initially we
11720 	 * only reserve at most 50% of the available vectors, in the case that
11721 	 * the number of online CPUs is large. This ensures that we can enable
11722 	 * extra features as well. Once we've enabled the other features, we
11723 	 * will use any remaining vectors to reach as close as we can to the
11724 	 * number of online CPUs.
11725 	 */
11726 	cpus = num_online_cpus();
11727 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11728 	vectors_left -= pf->num_lan_msix;
11729 
11730 	/* reserve one vector for sideband flow director */
11731 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11732 		if (vectors_left) {
11733 			pf->num_fdsb_msix = 1;
11734 			v_budget++;
11735 			vectors_left--;
11736 		} else {
11737 			pf->num_fdsb_msix = 0;
11738 		}
11739 	}
11740 
11741 	/* can we reserve enough for iWARP? */
11742 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11743 		iwarp_requested = pf->num_iwarp_msix;
11744 
11745 		if (!vectors_left)
11746 			pf->num_iwarp_msix = 0;
11747 		else if (vectors_left < pf->num_iwarp_msix)
11748 			pf->num_iwarp_msix = 1;
11749 		v_budget += pf->num_iwarp_msix;
11750 		vectors_left -= pf->num_iwarp_msix;
11751 	}
11752 
11753 	/* any vectors left over go for VMDq support */
11754 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11755 		if (!vectors_left) {
11756 			pf->num_vmdq_msix = 0;
11757 			pf->num_vmdq_qps = 0;
11758 		} else {
11759 			int vmdq_vecs_wanted =
11760 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11761 			int vmdq_vecs =
11762 				min_t(int, vectors_left, vmdq_vecs_wanted);
11763 
11764 			/* if we're short on vectors for what's desired, we limit
11765 			 * the queues per vmdq.  If this is still more than are
11766 			 * available, the user will need to change the number of
11767 			 * queues/vectors used by the PF later with the ethtool
11768 			 * channels command
11769 			 */
11770 			if (vectors_left < vmdq_vecs_wanted) {
11771 				pf->num_vmdq_qps = 1;
11772 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11773 				vmdq_vecs = min_t(int,
11774 						  vectors_left,
11775 						  vmdq_vecs_wanted);
11776 			}
11777 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11778 
11779 			v_budget += vmdq_vecs;
11780 			vectors_left -= vmdq_vecs;
11781 		}
11782 	}
11783 
11784 	/* On systems with a large number of SMP cores, we previously limited
11785 	 * the number of vectors for num_lan_msix to be at most 50% of the
11786 	 * available vectors, to allow for other features. Now, we add back
11787 	 * the remaining vectors. However, we ensure that the total
11788 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11789 	 * calculate the number of vectors we can add without going over the
11790 	 * cap of CPUs. For systems with a small number of CPUs this will be
11791 	 * zero.
11792 	 */
11793 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11794 	pf->num_lan_msix += extra_vectors;
11795 	vectors_left -= extra_vectors;
11796 
11797 	WARN(vectors_left < 0,
11798 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11799 
11800 	v_budget += pf->num_lan_msix;
11801 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11802 				   GFP_KERNEL);
11803 	if (!pf->msix_entries)
11804 		return -ENOMEM;
11805 
11806 	for (i = 0; i < v_budget; i++)
11807 		pf->msix_entries[i].entry = i;
11808 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11809 
11810 	if (v_actual < I40E_MIN_MSIX) {
11811 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11812 		kfree(pf->msix_entries);
11813 		pf->msix_entries = NULL;
11814 		pci_disable_msix(pf->pdev);
11815 		return -ENODEV;
11816 
11817 	} else if (v_actual == I40E_MIN_MSIX) {
11818 		/* Adjust for minimal MSIX use */
11819 		pf->num_vmdq_vsis = 0;
11820 		pf->num_vmdq_qps = 0;
11821 		pf->num_lan_qps = 1;
11822 		pf->num_lan_msix = 1;
11823 
11824 	} else if (v_actual != v_budget) {
11825 		/* If we have limited resources, we will start with no vectors
11826 		 * for the special features and then allocate vectors to some
11827 		 * of these features based on the policy and at the end disable
11828 		 * the features that did not get any vectors.
11829 		 */
11830 		int vec;
11831 
11832 		dev_info(&pf->pdev->dev,
11833 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11834 			 v_actual, v_budget);
11835 		/* reserve the misc vector */
11836 		vec = v_actual - 1;
11837 
11838 		/* Scale vector usage down */
11839 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11840 		pf->num_vmdq_vsis = 1;
11841 		pf->num_vmdq_qps = 1;
11842 
11843 		/* partition out the remaining vectors */
11844 		switch (vec) {
11845 		case 2:
11846 			pf->num_lan_msix = 1;
11847 			break;
11848 		case 3:
11849 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11850 				pf->num_lan_msix = 1;
11851 				pf->num_iwarp_msix = 1;
11852 			} else {
11853 				pf->num_lan_msix = 2;
11854 			}
11855 			break;
11856 		default:
11857 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11858 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11859 						 iwarp_requested);
11860 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11861 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11862 			} else {
11863 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11864 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11865 			}
11866 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11867 				pf->num_fdsb_msix = 1;
11868 				vec--;
11869 			}
11870 			pf->num_lan_msix = min_t(int,
11871 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11872 							      pf->num_lan_msix);
11873 			pf->num_lan_qps = pf->num_lan_msix;
11874 			break;
11875 		}
11876 	}
11877 
11878 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11879 	    (pf->num_fdsb_msix == 0)) {
11880 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11881 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11882 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11883 	}
11884 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11885 	    (pf->num_vmdq_msix == 0)) {
11886 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11887 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11888 	}
11889 
11890 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11891 	    (pf->num_iwarp_msix == 0)) {
11892 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11893 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11894 	}
11895 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11896 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11897 		   pf->num_lan_msix,
11898 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11899 		   pf->num_fdsb_msix,
11900 		   pf->num_iwarp_msix);
11901 
11902 	return v_actual;
11903 }
11904 
11905 /**
11906  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11907  * @vsi: the VSI being configured
11908  * @v_idx: index of the vector in the vsi struct
11909  *
11910  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11911  **/
11912 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11913 {
11914 	struct i40e_q_vector *q_vector;
11915 
11916 	/* allocate q_vector */
11917 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11918 	if (!q_vector)
11919 		return -ENOMEM;
11920 
11921 	q_vector->vsi = vsi;
11922 	q_vector->v_idx = v_idx;
11923 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11924 
11925 	if (vsi->netdev)
11926 		netif_napi_add(vsi->netdev, &q_vector->napi,
11927 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11928 
11929 	/* tie q_vector and vsi together */
11930 	vsi->q_vectors[v_idx] = q_vector;
11931 
11932 	return 0;
11933 }
11934 
11935 /**
11936  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11937  * @vsi: the VSI being configured
11938  *
11939  * We allocate one q_vector per queue interrupt.  If allocation fails we
11940  * return -ENOMEM.
11941  **/
11942 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11943 {
11944 	struct i40e_pf *pf = vsi->back;
11945 	int err, v_idx, num_q_vectors;
11946 
11947 	/* if not MSIX, give the one vector only to the LAN VSI */
11948 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11949 		num_q_vectors = vsi->num_q_vectors;
11950 	else if (vsi == pf->vsi[pf->lan_vsi])
11951 		num_q_vectors = 1;
11952 	else
11953 		return -EINVAL;
11954 
11955 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11956 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11957 		if (err)
11958 			goto err_out;
11959 	}
11960 
11961 	return 0;
11962 
11963 err_out:
11964 	while (v_idx--)
11965 		i40e_free_q_vector(vsi, v_idx);
11966 
11967 	return err;
11968 }
11969 
11970 /**
11971  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11972  * @pf: board private structure to initialize
11973  **/
11974 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11975 {
11976 	int vectors = 0;
11977 	ssize_t size;
11978 
11979 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11980 		vectors = i40e_init_msix(pf);
11981 		if (vectors < 0) {
11982 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11983 				       I40E_FLAG_IWARP_ENABLED	|
11984 				       I40E_FLAG_RSS_ENABLED	|
11985 				       I40E_FLAG_DCB_CAPABLE	|
11986 				       I40E_FLAG_DCB_ENABLED	|
11987 				       I40E_FLAG_SRIOV_ENABLED	|
11988 				       I40E_FLAG_FD_SB_ENABLED	|
11989 				       I40E_FLAG_FD_ATR_ENABLED	|
11990 				       I40E_FLAG_VMDQ_ENABLED);
11991 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11992 
11993 			/* rework the queue expectations without MSIX */
11994 			i40e_determine_queue_usage(pf);
11995 		}
11996 	}
11997 
11998 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11999 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12000 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12001 		vectors = pci_enable_msi(pf->pdev);
12002 		if (vectors < 0) {
12003 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12004 				 vectors);
12005 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12006 		}
12007 		vectors = 1;  /* one MSI or Legacy vector */
12008 	}
12009 
12010 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12011 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12012 
12013 	/* set up vector assignment tracking */
12014 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12015 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12016 	if (!pf->irq_pile)
12017 		return -ENOMEM;
12018 
12019 	pf->irq_pile->num_entries = vectors;
12020 
12021 	/* track first vector for misc interrupts, ignore return */
12022 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12023 
12024 	return 0;
12025 }
12026 
12027 /**
12028  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12029  * @pf: private board data structure
12030  *
12031  * Restore the interrupt scheme that was cleared when we suspended the
12032  * device. This should be called during resume to re-allocate the q_vectors
12033  * and reacquire IRQs.
12034  */
12035 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12036 {
12037 	int err, i;
12038 
12039 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12040 	 * scheme. We need to re-enabled them here in order to attempt to
12041 	 * re-acquire the MSI or MSI-X vectors
12042 	 */
12043 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12044 
12045 	err = i40e_init_interrupt_scheme(pf);
12046 	if (err)
12047 		return err;
12048 
12049 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12050 	 * rings together again.
12051 	 */
12052 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12053 		if (pf->vsi[i]) {
12054 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12055 			if (err)
12056 				goto err_unwind;
12057 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12058 		}
12059 	}
12060 
12061 	err = i40e_setup_misc_vector(pf);
12062 	if (err)
12063 		goto err_unwind;
12064 
12065 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12066 		i40e_client_update_msix_info(pf);
12067 
12068 	return 0;
12069 
12070 err_unwind:
12071 	while (i--) {
12072 		if (pf->vsi[i])
12073 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12074 	}
12075 
12076 	return err;
12077 }
12078 
12079 /**
12080  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12081  * non queue events in recovery mode
12082  * @pf: board private structure
12083  *
12084  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12085  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12086  * This is handled differently than in recovery mode since no Tx/Rx resources
12087  * are being allocated.
12088  **/
12089 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12090 {
12091 	int err;
12092 
12093 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12094 		err = i40e_setup_misc_vector(pf);
12095 
12096 		if (err) {
12097 			dev_info(&pf->pdev->dev,
12098 				 "MSI-X misc vector request failed, error %d\n",
12099 				 err);
12100 			return err;
12101 		}
12102 	} else {
12103 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12104 
12105 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12106 				  pf->int_name, pf);
12107 
12108 		if (err) {
12109 			dev_info(&pf->pdev->dev,
12110 				 "MSI/legacy misc vector request failed, error %d\n",
12111 				 err);
12112 			return err;
12113 		}
12114 		i40e_enable_misc_int_causes(pf);
12115 		i40e_irq_dynamic_enable_icr0(pf);
12116 	}
12117 
12118 	return 0;
12119 }
12120 
12121 /**
12122  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12123  * @pf: board private structure
12124  *
12125  * This sets up the handler for MSIX 0, which is used to manage the
12126  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12127  * when in MSI or Legacy interrupt mode.
12128  **/
12129 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12130 {
12131 	struct i40e_hw *hw = &pf->hw;
12132 	int err = 0;
12133 
12134 	/* Only request the IRQ once, the first time through. */
12135 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12136 		err = request_irq(pf->msix_entries[0].vector,
12137 				  i40e_intr, 0, pf->int_name, pf);
12138 		if (err) {
12139 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12140 			dev_info(&pf->pdev->dev,
12141 				 "request_irq for %s failed: %d\n",
12142 				 pf->int_name, err);
12143 			return -EFAULT;
12144 		}
12145 	}
12146 
12147 	i40e_enable_misc_int_causes(pf);
12148 
12149 	/* associate no queues to the misc vector */
12150 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12151 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12152 
12153 	i40e_flush(hw);
12154 
12155 	i40e_irq_dynamic_enable_icr0(pf);
12156 
12157 	return err;
12158 }
12159 
12160 /**
12161  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12162  * @vsi: Pointer to vsi structure
12163  * @seed: Buffter to store the hash keys
12164  * @lut: Buffer to store the lookup table entries
12165  * @lut_size: Size of buffer to store the lookup table entries
12166  *
12167  * Return 0 on success, negative on failure
12168  */
12169 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12170 			   u8 *lut, u16 lut_size)
12171 {
12172 	struct i40e_pf *pf = vsi->back;
12173 	struct i40e_hw *hw = &pf->hw;
12174 	int ret = 0;
12175 
12176 	if (seed) {
12177 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12178 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12179 		if (ret) {
12180 			dev_info(&pf->pdev->dev,
12181 				 "Cannot get RSS key, err %s aq_err %s\n",
12182 				 i40e_stat_str(&pf->hw, ret),
12183 				 i40e_aq_str(&pf->hw,
12184 					     pf->hw.aq.asq_last_status));
12185 			return ret;
12186 		}
12187 	}
12188 
12189 	if (lut) {
12190 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12191 
12192 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12193 		if (ret) {
12194 			dev_info(&pf->pdev->dev,
12195 				 "Cannot get RSS lut, err %s aq_err %s\n",
12196 				 i40e_stat_str(&pf->hw, ret),
12197 				 i40e_aq_str(&pf->hw,
12198 					     pf->hw.aq.asq_last_status));
12199 			return ret;
12200 		}
12201 	}
12202 
12203 	return ret;
12204 }
12205 
12206 /**
12207  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12208  * @vsi: Pointer to vsi structure
12209  * @seed: RSS hash seed
12210  * @lut: Lookup table
12211  * @lut_size: Lookup table size
12212  *
12213  * Returns 0 on success, negative on failure
12214  **/
12215 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12216 			       const u8 *lut, u16 lut_size)
12217 {
12218 	struct i40e_pf *pf = vsi->back;
12219 	struct i40e_hw *hw = &pf->hw;
12220 	u16 vf_id = vsi->vf_id;
12221 	u8 i;
12222 
12223 	/* Fill out hash function seed */
12224 	if (seed) {
12225 		u32 *seed_dw = (u32 *)seed;
12226 
12227 		if (vsi->type == I40E_VSI_MAIN) {
12228 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12229 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12230 		} else if (vsi->type == I40E_VSI_SRIOV) {
12231 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12232 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12233 		} else {
12234 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12235 		}
12236 	}
12237 
12238 	if (lut) {
12239 		u32 *lut_dw = (u32 *)lut;
12240 
12241 		if (vsi->type == I40E_VSI_MAIN) {
12242 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12243 				return -EINVAL;
12244 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12245 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12246 		} else if (vsi->type == I40E_VSI_SRIOV) {
12247 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12248 				return -EINVAL;
12249 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12250 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12251 		} else {
12252 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12253 		}
12254 	}
12255 	i40e_flush(hw);
12256 
12257 	return 0;
12258 }
12259 
12260 /**
12261  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12262  * @vsi: Pointer to VSI structure
12263  * @seed: Buffer to store the keys
12264  * @lut: Buffer to store the lookup table entries
12265  * @lut_size: Size of buffer to store the lookup table entries
12266  *
12267  * Returns 0 on success, negative on failure
12268  */
12269 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12270 			    u8 *lut, u16 lut_size)
12271 {
12272 	struct i40e_pf *pf = vsi->back;
12273 	struct i40e_hw *hw = &pf->hw;
12274 	u16 i;
12275 
12276 	if (seed) {
12277 		u32 *seed_dw = (u32 *)seed;
12278 
12279 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12280 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12281 	}
12282 	if (lut) {
12283 		u32 *lut_dw = (u32 *)lut;
12284 
12285 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12286 			return -EINVAL;
12287 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12288 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12289 	}
12290 
12291 	return 0;
12292 }
12293 
12294 /**
12295  * i40e_config_rss - Configure RSS keys and lut
12296  * @vsi: Pointer to VSI structure
12297  * @seed: RSS hash seed
12298  * @lut: Lookup table
12299  * @lut_size: Lookup table size
12300  *
12301  * Returns 0 on success, negative on failure
12302  */
12303 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12304 {
12305 	struct i40e_pf *pf = vsi->back;
12306 
12307 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12308 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12309 	else
12310 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12311 }
12312 
12313 /**
12314  * i40e_get_rss - Get RSS keys and lut
12315  * @vsi: Pointer to VSI structure
12316  * @seed: Buffer to store the keys
12317  * @lut: Buffer to store the lookup table entries
12318  * @lut_size: Size of buffer to store the lookup table entries
12319  *
12320  * Returns 0 on success, negative on failure
12321  */
12322 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12323 {
12324 	struct i40e_pf *pf = vsi->back;
12325 
12326 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12327 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12328 	else
12329 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12330 }
12331 
12332 /**
12333  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12334  * @pf: Pointer to board private structure
12335  * @lut: Lookup table
12336  * @rss_table_size: Lookup table size
12337  * @rss_size: Range of queue number for hashing
12338  */
12339 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12340 		       u16 rss_table_size, u16 rss_size)
12341 {
12342 	u16 i;
12343 
12344 	for (i = 0; i < rss_table_size; i++)
12345 		lut[i] = i % rss_size;
12346 }
12347 
12348 /**
12349  * i40e_pf_config_rss - Prepare for RSS if used
12350  * @pf: board private structure
12351  **/
12352 static int i40e_pf_config_rss(struct i40e_pf *pf)
12353 {
12354 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12355 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12356 	u8 *lut;
12357 	struct i40e_hw *hw = &pf->hw;
12358 	u32 reg_val;
12359 	u64 hena;
12360 	int ret;
12361 
12362 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12363 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12364 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12365 	hena |= i40e_pf_get_default_rss_hena(pf);
12366 
12367 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12368 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12369 
12370 	/* Determine the RSS table size based on the hardware capabilities */
12371 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12372 	reg_val = (pf->rss_table_size == 512) ?
12373 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12374 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12375 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12376 
12377 	/* Determine the RSS size of the VSI */
12378 	if (!vsi->rss_size) {
12379 		u16 qcount;
12380 		/* If the firmware does something weird during VSI init, we
12381 		 * could end up with zero TCs. Check for that to avoid
12382 		 * divide-by-zero. It probably won't pass traffic, but it also
12383 		 * won't panic.
12384 		 */
12385 		qcount = vsi->num_queue_pairs /
12386 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12387 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12388 	}
12389 	if (!vsi->rss_size)
12390 		return -EINVAL;
12391 
12392 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12393 	if (!lut)
12394 		return -ENOMEM;
12395 
12396 	/* Use user configured lut if there is one, otherwise use default */
12397 	if (vsi->rss_lut_user)
12398 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12399 	else
12400 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12401 
12402 	/* Use user configured hash key if there is one, otherwise
12403 	 * use default.
12404 	 */
12405 	if (vsi->rss_hkey_user)
12406 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12407 	else
12408 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12409 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12410 	kfree(lut);
12411 
12412 	return ret;
12413 }
12414 
12415 /**
12416  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12417  * @pf: board private structure
12418  * @queue_count: the requested queue count for rss.
12419  *
12420  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12421  * count which may be different from the requested queue count.
12422  * Note: expects to be called while under rtnl_lock()
12423  **/
12424 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12425 {
12426 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12427 	int new_rss_size;
12428 
12429 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12430 		return 0;
12431 
12432 	queue_count = min_t(int, queue_count, num_online_cpus());
12433 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12434 
12435 	if (queue_count != vsi->num_queue_pairs) {
12436 		u16 qcount;
12437 
12438 		vsi->req_queue_pairs = queue_count;
12439 		i40e_prep_for_reset(pf);
12440 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12441 			return pf->alloc_rss_size;
12442 
12443 		pf->alloc_rss_size = new_rss_size;
12444 
12445 		i40e_reset_and_rebuild(pf, true, true);
12446 
12447 		/* Discard the user configured hash keys and lut, if less
12448 		 * queues are enabled.
12449 		 */
12450 		if (queue_count < vsi->rss_size) {
12451 			i40e_clear_rss_config_user(vsi);
12452 			dev_dbg(&pf->pdev->dev,
12453 				"discard user configured hash keys and lut\n");
12454 		}
12455 
12456 		/* Reset vsi->rss_size, as number of enabled queues changed */
12457 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12458 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12459 
12460 		i40e_pf_config_rss(pf);
12461 	}
12462 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12463 		 vsi->req_queue_pairs, pf->rss_size_max);
12464 	return pf->alloc_rss_size;
12465 }
12466 
12467 /**
12468  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12469  * @pf: board private structure
12470  **/
12471 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12472 {
12473 	i40e_status status;
12474 	bool min_valid, max_valid;
12475 	u32 max_bw, min_bw;
12476 
12477 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12478 					   &min_valid, &max_valid);
12479 
12480 	if (!status) {
12481 		if (min_valid)
12482 			pf->min_bw = min_bw;
12483 		if (max_valid)
12484 			pf->max_bw = max_bw;
12485 	}
12486 
12487 	return status;
12488 }
12489 
12490 /**
12491  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12492  * @pf: board private structure
12493  **/
12494 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12495 {
12496 	struct i40e_aqc_configure_partition_bw_data bw_data;
12497 	i40e_status status;
12498 
12499 	memset(&bw_data, 0, sizeof(bw_data));
12500 
12501 	/* Set the valid bit for this PF */
12502 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12503 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12504 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12505 
12506 	/* Set the new bandwidths */
12507 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12508 
12509 	return status;
12510 }
12511 
12512 /**
12513  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12514  * @pf: board private structure
12515  **/
12516 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12517 {
12518 	/* Commit temporary BW setting to permanent NVM image */
12519 	enum i40e_admin_queue_err last_aq_status;
12520 	i40e_status ret;
12521 	u16 nvm_word;
12522 
12523 	if (pf->hw.partition_id != 1) {
12524 		dev_info(&pf->pdev->dev,
12525 			 "Commit BW only works on partition 1! This is partition %d",
12526 			 pf->hw.partition_id);
12527 		ret = I40E_NOT_SUPPORTED;
12528 		goto bw_commit_out;
12529 	}
12530 
12531 	/* Acquire NVM for read access */
12532 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12533 	last_aq_status = pf->hw.aq.asq_last_status;
12534 	if (ret) {
12535 		dev_info(&pf->pdev->dev,
12536 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12537 			 i40e_stat_str(&pf->hw, ret),
12538 			 i40e_aq_str(&pf->hw, last_aq_status));
12539 		goto bw_commit_out;
12540 	}
12541 
12542 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12543 	ret = i40e_aq_read_nvm(&pf->hw,
12544 			       I40E_SR_NVM_CONTROL_WORD,
12545 			       0x10, sizeof(nvm_word), &nvm_word,
12546 			       false, NULL);
12547 	/* Save off last admin queue command status before releasing
12548 	 * the NVM
12549 	 */
12550 	last_aq_status = pf->hw.aq.asq_last_status;
12551 	i40e_release_nvm(&pf->hw);
12552 	if (ret) {
12553 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12554 			 i40e_stat_str(&pf->hw, ret),
12555 			 i40e_aq_str(&pf->hw, last_aq_status));
12556 		goto bw_commit_out;
12557 	}
12558 
12559 	/* Wait a bit for NVM release to complete */
12560 	msleep(50);
12561 
12562 	/* Acquire NVM for write access */
12563 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12564 	last_aq_status = pf->hw.aq.asq_last_status;
12565 	if (ret) {
12566 		dev_info(&pf->pdev->dev,
12567 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12568 			 i40e_stat_str(&pf->hw, ret),
12569 			 i40e_aq_str(&pf->hw, last_aq_status));
12570 		goto bw_commit_out;
12571 	}
12572 	/* Write it back out unchanged to initiate update NVM,
12573 	 * which will force a write of the shadow (alt) RAM to
12574 	 * the NVM - thus storing the bandwidth values permanently.
12575 	 */
12576 	ret = i40e_aq_update_nvm(&pf->hw,
12577 				 I40E_SR_NVM_CONTROL_WORD,
12578 				 0x10, sizeof(nvm_word),
12579 				 &nvm_word, true, 0, NULL);
12580 	/* Save off last admin queue command status before releasing
12581 	 * the NVM
12582 	 */
12583 	last_aq_status = pf->hw.aq.asq_last_status;
12584 	i40e_release_nvm(&pf->hw);
12585 	if (ret)
12586 		dev_info(&pf->pdev->dev,
12587 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12588 			 i40e_stat_str(&pf->hw, ret),
12589 			 i40e_aq_str(&pf->hw, last_aq_status));
12590 bw_commit_out:
12591 
12592 	return ret;
12593 }
12594 
12595 /**
12596  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12597  * if total port shutdown feature is enabled for this PF
12598  * @pf: board private structure
12599  **/
12600 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12601 {
12602 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12603 #define I40E_FEATURES_ENABLE_PTR		0x2A
12604 #define I40E_CURRENT_SETTING_PTR		0x2B
12605 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12606 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12607 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12608 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12609 	i40e_status read_status = I40E_SUCCESS;
12610 	u16 sr_emp_sr_settings_ptr = 0;
12611 	u16 features_enable = 0;
12612 	u16 link_behavior = 0;
12613 	bool ret = false;
12614 
12615 	read_status = i40e_read_nvm_word(&pf->hw,
12616 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12617 					 &sr_emp_sr_settings_ptr);
12618 	if (read_status)
12619 		goto err_nvm;
12620 	read_status = i40e_read_nvm_word(&pf->hw,
12621 					 sr_emp_sr_settings_ptr +
12622 					 I40E_FEATURES_ENABLE_PTR,
12623 					 &features_enable);
12624 	if (read_status)
12625 		goto err_nvm;
12626 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12627 		read_status = i40e_read_nvm_module_data(&pf->hw,
12628 							I40E_SR_EMP_SR_SETTINGS_PTR,
12629 							I40E_CURRENT_SETTING_PTR,
12630 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12631 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12632 							&link_behavior);
12633 		if (read_status)
12634 			goto err_nvm;
12635 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12636 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12637 	}
12638 	return ret;
12639 
12640 err_nvm:
12641 	dev_warn(&pf->pdev->dev,
12642 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12643 		 i40e_stat_str(&pf->hw, read_status));
12644 	return ret;
12645 }
12646 
12647 /**
12648  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12649  * @pf: board private structure to initialize
12650  *
12651  * i40e_sw_init initializes the Adapter private data structure.
12652  * Fields are initialized based on PCI device information and
12653  * OS network device settings (MTU size).
12654  **/
12655 static int i40e_sw_init(struct i40e_pf *pf)
12656 {
12657 	int err = 0;
12658 	int size;
12659 	u16 pow;
12660 
12661 	/* Set default capability flags */
12662 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12663 		    I40E_FLAG_MSI_ENABLED     |
12664 		    I40E_FLAG_MSIX_ENABLED;
12665 
12666 	/* Set default ITR */
12667 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12668 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12669 
12670 	/* Depending on PF configurations, it is possible that the RSS
12671 	 * maximum might end up larger than the available queues
12672 	 */
12673 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12674 	pf->alloc_rss_size = 1;
12675 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12676 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12677 				 pf->hw.func_caps.num_tx_qp);
12678 
12679 	/* find the next higher power-of-2 of num cpus */
12680 	pow = roundup_pow_of_two(num_online_cpus());
12681 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12682 
12683 	if (pf->hw.func_caps.rss) {
12684 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12685 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12686 					   num_online_cpus());
12687 	}
12688 
12689 	/* MFP mode enabled */
12690 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12691 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12692 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12693 		if (i40e_get_partition_bw_setting(pf)) {
12694 			dev_warn(&pf->pdev->dev,
12695 				 "Could not get partition bw settings\n");
12696 		} else {
12697 			dev_info(&pf->pdev->dev,
12698 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12699 				 pf->min_bw, pf->max_bw);
12700 
12701 			/* nudge the Tx scheduler */
12702 			i40e_set_partition_bw_setting(pf);
12703 		}
12704 	}
12705 
12706 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12707 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12708 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12709 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12710 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12711 		    pf->hw.num_partitions > 1)
12712 			dev_info(&pf->pdev->dev,
12713 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12714 		else
12715 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12716 		pf->fdir_pf_filter_count =
12717 				 pf->hw.func_caps.fd_filters_guaranteed;
12718 		pf->hw.fdir_shared_filter_count =
12719 				 pf->hw.func_caps.fd_filters_best_effort;
12720 	}
12721 
12722 	if (pf->hw.mac.type == I40E_MAC_X722) {
12723 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12724 				    I40E_HW_128_QP_RSS_CAPABLE |
12725 				    I40E_HW_ATR_EVICT_CAPABLE |
12726 				    I40E_HW_WB_ON_ITR_CAPABLE |
12727 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12728 				    I40E_HW_NO_PCI_LINK_CHECK |
12729 				    I40E_HW_USE_SET_LLDP_MIB |
12730 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12731 				    I40E_HW_PTP_L4_CAPABLE |
12732 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12733 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12734 
12735 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12736 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12737 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12738 			dev_warn(&pf->pdev->dev,
12739 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12740 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12741 		}
12742 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12743 		   ((pf->hw.aq.api_maj_ver == 1) &&
12744 		    (pf->hw.aq.api_min_ver > 4))) {
12745 		/* Supported in FW API version higher than 1.4 */
12746 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12747 	}
12748 
12749 	/* Enable HW ATR eviction if possible */
12750 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12751 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12752 
12753 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12754 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12755 	    (pf->hw.aq.fw_maj_ver < 4))) {
12756 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12757 		/* No DCB support  for FW < v4.33 */
12758 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12759 	}
12760 
12761 	/* Disable FW LLDP if FW < v4.3 */
12762 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12763 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12764 	    (pf->hw.aq.fw_maj_ver < 4)))
12765 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12766 
12767 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12768 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12769 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12770 	    (pf->hw.aq.fw_maj_ver >= 5)))
12771 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12772 
12773 	/* Enable PTP L4 if FW > v6.0 */
12774 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12775 	    pf->hw.aq.fw_maj_ver >= 6)
12776 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12777 
12778 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12779 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12780 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12781 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12782 	}
12783 
12784 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12785 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12786 		/* IWARP needs one extra vector for CQP just like MISC.*/
12787 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12788 	}
12789 	/* Stopping FW LLDP engine is supported on XL710 and X722
12790 	 * starting from FW versions determined in i40e_init_adminq.
12791 	 * Stopping the FW LLDP engine is not supported on XL710
12792 	 * if NPAR is functioning so unset this hw flag in this case.
12793 	 */
12794 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12795 	    pf->hw.func_caps.npar_enable &&
12796 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12797 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12798 
12799 #ifdef CONFIG_PCI_IOV
12800 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12801 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12802 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12803 		pf->num_req_vfs = min_t(int,
12804 					pf->hw.func_caps.num_vfs,
12805 					I40E_MAX_VF_COUNT);
12806 	}
12807 #endif /* CONFIG_PCI_IOV */
12808 	pf->eeprom_version = 0xDEAD;
12809 	pf->lan_veb = I40E_NO_VEB;
12810 	pf->lan_vsi = I40E_NO_VSI;
12811 
12812 	/* By default FW has this off for performance reasons */
12813 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12814 
12815 	/* set up queue assignment tracking */
12816 	size = sizeof(struct i40e_lump_tracking)
12817 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12818 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12819 	if (!pf->qp_pile) {
12820 		err = -ENOMEM;
12821 		goto sw_init_done;
12822 	}
12823 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12824 
12825 	pf->tx_timeout_recovery_level = 1;
12826 
12827 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12828 	    i40e_is_total_port_shutdown_enabled(pf)) {
12829 		/* Link down on close must be on when total port shutdown
12830 		 * is enabled for a given port
12831 		 */
12832 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12833 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12834 		dev_info(&pf->pdev->dev,
12835 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12836 	}
12837 	mutex_init(&pf->switch_mutex);
12838 
12839 sw_init_done:
12840 	return err;
12841 }
12842 
12843 /**
12844  * i40e_set_ntuple - set the ntuple feature flag and take action
12845  * @pf: board private structure to initialize
12846  * @features: the feature set that the stack is suggesting
12847  *
12848  * returns a bool to indicate if reset needs to happen
12849  **/
12850 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12851 {
12852 	bool need_reset = false;
12853 
12854 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12855 	 * the state changed, we need to reset.
12856 	 */
12857 	if (features & NETIF_F_NTUPLE) {
12858 		/* Enable filters and mark for reset */
12859 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12860 			need_reset = true;
12861 		/* enable FD_SB only if there is MSI-X vector and no cloud
12862 		 * filters exist
12863 		 */
12864 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12865 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12866 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12867 		}
12868 	} else {
12869 		/* turn off filters, mark for reset and clear SW filter list */
12870 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12871 			need_reset = true;
12872 			i40e_fdir_filter_exit(pf);
12873 		}
12874 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12875 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12876 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12877 
12878 		/* reset fd counters */
12879 		pf->fd_add_err = 0;
12880 		pf->fd_atr_cnt = 0;
12881 		/* if ATR was auto disabled it can be re-enabled. */
12882 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12883 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12884 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12885 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12886 	}
12887 	return need_reset;
12888 }
12889 
12890 /**
12891  * i40e_clear_rss_lut - clear the rx hash lookup table
12892  * @vsi: the VSI being configured
12893  **/
12894 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12895 {
12896 	struct i40e_pf *pf = vsi->back;
12897 	struct i40e_hw *hw = &pf->hw;
12898 	u16 vf_id = vsi->vf_id;
12899 	u8 i;
12900 
12901 	if (vsi->type == I40E_VSI_MAIN) {
12902 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12903 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12904 	} else if (vsi->type == I40E_VSI_SRIOV) {
12905 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12906 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12907 	} else {
12908 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12909 	}
12910 }
12911 
12912 /**
12913  * i40e_set_features - set the netdev feature flags
12914  * @netdev: ptr to the netdev being adjusted
12915  * @features: the feature set that the stack is suggesting
12916  * Note: expects to be called while under rtnl_lock()
12917  **/
12918 static int i40e_set_features(struct net_device *netdev,
12919 			     netdev_features_t features)
12920 {
12921 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12922 	struct i40e_vsi *vsi = np->vsi;
12923 	struct i40e_pf *pf = vsi->back;
12924 	bool need_reset;
12925 
12926 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12927 		i40e_pf_config_rss(pf);
12928 	else if (!(features & NETIF_F_RXHASH) &&
12929 		 netdev->features & NETIF_F_RXHASH)
12930 		i40e_clear_rss_lut(vsi);
12931 
12932 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12933 		i40e_vlan_stripping_enable(vsi);
12934 	else
12935 		i40e_vlan_stripping_disable(vsi);
12936 
12937 	if (!(features & NETIF_F_HW_TC) &&
12938 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12939 		dev_err(&pf->pdev->dev,
12940 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12941 		return -EINVAL;
12942 	}
12943 
12944 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12945 		i40e_del_all_macvlans(vsi);
12946 
12947 	need_reset = i40e_set_ntuple(pf, features);
12948 
12949 	if (need_reset)
12950 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12951 
12952 	return 0;
12953 }
12954 
12955 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12956 				    unsigned int table, unsigned int idx,
12957 				    struct udp_tunnel_info *ti)
12958 {
12959 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12960 	struct i40e_hw *hw = &np->vsi->back->hw;
12961 	u8 type, filter_index;
12962 	i40e_status ret;
12963 
12964 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12965 						   I40E_AQC_TUNNEL_TYPE_NGE;
12966 
12967 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12968 				     NULL);
12969 	if (ret) {
12970 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12971 			    i40e_stat_str(hw, ret),
12972 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12973 		return -EIO;
12974 	}
12975 
12976 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12977 	return 0;
12978 }
12979 
12980 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12981 				      unsigned int table, unsigned int idx,
12982 				      struct udp_tunnel_info *ti)
12983 {
12984 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12985 	struct i40e_hw *hw = &np->vsi->back->hw;
12986 	i40e_status ret;
12987 
12988 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12989 	if (ret) {
12990 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12991 			    i40e_stat_str(hw, ret),
12992 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12993 		return -EIO;
12994 	}
12995 
12996 	return 0;
12997 }
12998 
12999 static int i40e_get_phys_port_id(struct net_device *netdev,
13000 				 struct netdev_phys_item_id *ppid)
13001 {
13002 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13003 	struct i40e_pf *pf = np->vsi->back;
13004 	struct i40e_hw *hw = &pf->hw;
13005 
13006 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13007 		return -EOPNOTSUPP;
13008 
13009 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13010 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13011 
13012 	return 0;
13013 }
13014 
13015 /**
13016  * i40e_ndo_fdb_add - add an entry to the hardware database
13017  * @ndm: the input from the stack
13018  * @tb: pointer to array of nladdr (unused)
13019  * @dev: the net device pointer
13020  * @addr: the MAC address entry being added
13021  * @vid: VLAN ID
13022  * @flags: instructions from stack about fdb operation
13023  * @extack: netlink extended ack, unused currently
13024  */
13025 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13026 			    struct net_device *dev,
13027 			    const unsigned char *addr, u16 vid,
13028 			    u16 flags,
13029 			    struct netlink_ext_ack *extack)
13030 {
13031 	struct i40e_netdev_priv *np = netdev_priv(dev);
13032 	struct i40e_pf *pf = np->vsi->back;
13033 	int err = 0;
13034 
13035 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13036 		return -EOPNOTSUPP;
13037 
13038 	if (vid) {
13039 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13040 		return -EINVAL;
13041 	}
13042 
13043 	/* Hardware does not support aging addresses so if a
13044 	 * ndm_state is given only allow permanent addresses
13045 	 */
13046 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13047 		netdev_info(dev, "FDB only supports static addresses\n");
13048 		return -EINVAL;
13049 	}
13050 
13051 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13052 		err = dev_uc_add_excl(dev, addr);
13053 	else if (is_multicast_ether_addr(addr))
13054 		err = dev_mc_add_excl(dev, addr);
13055 	else
13056 		err = -EINVAL;
13057 
13058 	/* Only return duplicate errors if NLM_F_EXCL is set */
13059 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13060 		err = 0;
13061 
13062 	return err;
13063 }
13064 
13065 /**
13066  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13067  * @dev: the netdev being configured
13068  * @nlh: RTNL message
13069  * @flags: bridge flags
13070  * @extack: netlink extended ack
13071  *
13072  * Inserts a new hardware bridge if not already created and
13073  * enables the bridging mode requested (VEB or VEPA). If the
13074  * hardware bridge has already been inserted and the request
13075  * is to change the mode then that requires a PF reset to
13076  * allow rebuild of the components with required hardware
13077  * bridge mode enabled.
13078  *
13079  * Note: expects to be called while under rtnl_lock()
13080  **/
13081 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13082 				   struct nlmsghdr *nlh,
13083 				   u16 flags,
13084 				   struct netlink_ext_ack *extack)
13085 {
13086 	struct i40e_netdev_priv *np = netdev_priv(dev);
13087 	struct i40e_vsi *vsi = np->vsi;
13088 	struct i40e_pf *pf = vsi->back;
13089 	struct i40e_veb *veb = NULL;
13090 	struct nlattr *attr, *br_spec;
13091 	int i, rem;
13092 
13093 	/* Only for PF VSI for now */
13094 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13095 		return -EOPNOTSUPP;
13096 
13097 	/* Find the HW bridge for PF VSI */
13098 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13099 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13100 			veb = pf->veb[i];
13101 	}
13102 
13103 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13104 
13105 	nla_for_each_nested(attr, br_spec, rem) {
13106 		__u16 mode;
13107 
13108 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13109 			continue;
13110 
13111 		mode = nla_get_u16(attr);
13112 		if ((mode != BRIDGE_MODE_VEPA) &&
13113 		    (mode != BRIDGE_MODE_VEB))
13114 			return -EINVAL;
13115 
13116 		/* Insert a new HW bridge */
13117 		if (!veb) {
13118 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13119 					     vsi->tc_config.enabled_tc);
13120 			if (veb) {
13121 				veb->bridge_mode = mode;
13122 				i40e_config_bridge_mode(veb);
13123 			} else {
13124 				/* No Bridge HW offload available */
13125 				return -ENOENT;
13126 			}
13127 			break;
13128 		} else if (mode != veb->bridge_mode) {
13129 			/* Existing HW bridge but different mode needs reset */
13130 			veb->bridge_mode = mode;
13131 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13132 			if (mode == BRIDGE_MODE_VEB)
13133 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13134 			else
13135 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13136 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13137 			break;
13138 		}
13139 	}
13140 
13141 	return 0;
13142 }
13143 
13144 /**
13145  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13146  * @skb: skb buff
13147  * @pid: process id
13148  * @seq: RTNL message seq #
13149  * @dev: the netdev being configured
13150  * @filter_mask: unused
13151  * @nlflags: netlink flags passed in
13152  *
13153  * Return the mode in which the hardware bridge is operating in
13154  * i.e VEB or VEPA.
13155  **/
13156 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13157 				   struct net_device *dev,
13158 				   u32 __always_unused filter_mask,
13159 				   int nlflags)
13160 {
13161 	struct i40e_netdev_priv *np = netdev_priv(dev);
13162 	struct i40e_vsi *vsi = np->vsi;
13163 	struct i40e_pf *pf = vsi->back;
13164 	struct i40e_veb *veb = NULL;
13165 	int i;
13166 
13167 	/* Only for PF VSI for now */
13168 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13169 		return -EOPNOTSUPP;
13170 
13171 	/* Find the HW bridge for the PF VSI */
13172 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13173 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13174 			veb = pf->veb[i];
13175 	}
13176 
13177 	if (!veb)
13178 		return 0;
13179 
13180 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13181 				       0, 0, nlflags, filter_mask, NULL);
13182 }
13183 
13184 /**
13185  * i40e_features_check - Validate encapsulated packet conforms to limits
13186  * @skb: skb buff
13187  * @dev: This physical port's netdev
13188  * @features: Offload features that the stack believes apply
13189  **/
13190 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13191 					     struct net_device *dev,
13192 					     netdev_features_t features)
13193 {
13194 	size_t len;
13195 
13196 	/* No point in doing any of this if neither checksum nor GSO are
13197 	 * being requested for this frame.  We can rule out both by just
13198 	 * checking for CHECKSUM_PARTIAL
13199 	 */
13200 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13201 		return features;
13202 
13203 	/* We cannot support GSO if the MSS is going to be less than
13204 	 * 64 bytes.  If it is then we need to drop support for GSO.
13205 	 */
13206 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13207 		features &= ~NETIF_F_GSO_MASK;
13208 
13209 	/* MACLEN can support at most 63 words */
13210 	len = skb_network_header(skb) - skb->data;
13211 	if (len & ~(63 * 2))
13212 		goto out_err;
13213 
13214 	/* IPLEN and EIPLEN can support at most 127 dwords */
13215 	len = skb_transport_header(skb) - skb_network_header(skb);
13216 	if (len & ~(127 * 4))
13217 		goto out_err;
13218 
13219 	if (skb->encapsulation) {
13220 		/* L4TUNLEN can support 127 words */
13221 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13222 		if (len & ~(127 * 2))
13223 			goto out_err;
13224 
13225 		/* IPLEN can support at most 127 dwords */
13226 		len = skb_inner_transport_header(skb) -
13227 		      skb_inner_network_header(skb);
13228 		if (len & ~(127 * 4))
13229 			goto out_err;
13230 	}
13231 
13232 	/* No need to validate L4LEN as TCP is the only protocol with a
13233 	 * flexible value and we support all possible values supported
13234 	 * by TCP, which is at most 15 dwords
13235 	 */
13236 
13237 	return features;
13238 out_err:
13239 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13240 }
13241 
13242 /**
13243  * i40e_xdp_setup - add/remove an XDP program
13244  * @vsi: VSI to changed
13245  * @prog: XDP program
13246  * @extack: netlink extended ack
13247  **/
13248 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13249 			  struct netlink_ext_ack *extack)
13250 {
13251 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13252 	struct i40e_pf *pf = vsi->back;
13253 	struct bpf_prog *old_prog;
13254 	bool need_reset;
13255 	int i;
13256 
13257 	/* Don't allow frames that span over multiple buffers */
13258 	if (frame_size > vsi->rx_buf_len) {
13259 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13260 		return -EINVAL;
13261 	}
13262 
13263 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13264 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13265 
13266 	if (need_reset)
13267 		i40e_prep_for_reset(pf);
13268 
13269 	/* VSI shall be deleted in a moment, just return EINVAL */
13270 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13271 		return -EINVAL;
13272 
13273 	old_prog = xchg(&vsi->xdp_prog, prog);
13274 
13275 	if (need_reset) {
13276 		if (!prog)
13277 			/* Wait until ndo_xsk_wakeup completes. */
13278 			synchronize_rcu();
13279 		i40e_reset_and_rebuild(pf, true, true);
13280 	}
13281 
13282 	for (i = 0; i < vsi->num_queue_pairs; i++)
13283 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13284 
13285 	if (old_prog)
13286 		bpf_prog_put(old_prog);
13287 
13288 	/* Kick start the NAPI context if there is an AF_XDP socket open
13289 	 * on that queue id. This so that receiving will start.
13290 	 */
13291 	if (need_reset && prog)
13292 		for (i = 0; i < vsi->num_queue_pairs; i++)
13293 			if (vsi->xdp_rings[i]->xsk_pool)
13294 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13295 						      XDP_WAKEUP_RX);
13296 
13297 	return 0;
13298 }
13299 
13300 /**
13301  * i40e_enter_busy_conf - Enters busy config state
13302  * @vsi: vsi
13303  *
13304  * Returns 0 on success, <0 for failure.
13305  **/
13306 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13307 {
13308 	struct i40e_pf *pf = vsi->back;
13309 	int timeout = 50;
13310 
13311 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13312 		timeout--;
13313 		if (!timeout)
13314 			return -EBUSY;
13315 		usleep_range(1000, 2000);
13316 	}
13317 
13318 	return 0;
13319 }
13320 
13321 /**
13322  * i40e_exit_busy_conf - Exits busy config state
13323  * @vsi: vsi
13324  **/
13325 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13326 {
13327 	struct i40e_pf *pf = vsi->back;
13328 
13329 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13330 }
13331 
13332 /**
13333  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13334  * @vsi: vsi
13335  * @queue_pair: queue pair
13336  **/
13337 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13338 {
13339 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13340 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13341 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13342 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13343 	if (i40e_enabled_xdp_vsi(vsi)) {
13344 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13345 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13346 	}
13347 }
13348 
13349 /**
13350  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13351  * @vsi: vsi
13352  * @queue_pair: queue pair
13353  **/
13354 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13355 {
13356 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13357 	if (i40e_enabled_xdp_vsi(vsi)) {
13358 		/* Make sure that in-progress ndo_xdp_xmit calls are
13359 		 * completed.
13360 		 */
13361 		synchronize_rcu();
13362 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13363 	}
13364 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13365 }
13366 
13367 /**
13368  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13369  * @vsi: vsi
13370  * @queue_pair: queue pair
13371  * @enable: true for enable, false for disable
13372  **/
13373 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13374 					bool enable)
13375 {
13376 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13377 	struct i40e_q_vector *q_vector = rxr->q_vector;
13378 
13379 	if (!vsi->netdev)
13380 		return;
13381 
13382 	/* All rings in a qp belong to the same qvector. */
13383 	if (q_vector->rx.ring || q_vector->tx.ring) {
13384 		if (enable)
13385 			napi_enable(&q_vector->napi);
13386 		else
13387 			napi_disable(&q_vector->napi);
13388 	}
13389 }
13390 
13391 /**
13392  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13393  * @vsi: vsi
13394  * @queue_pair: queue pair
13395  * @enable: true for enable, false for disable
13396  *
13397  * Returns 0 on success, <0 on failure.
13398  **/
13399 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13400 					bool enable)
13401 {
13402 	struct i40e_pf *pf = vsi->back;
13403 	int pf_q, ret = 0;
13404 
13405 	pf_q = vsi->base_queue + queue_pair;
13406 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13407 				     false /*is xdp*/, enable);
13408 	if (ret) {
13409 		dev_info(&pf->pdev->dev,
13410 			 "VSI seid %d Tx ring %d %sable timeout\n",
13411 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13412 		return ret;
13413 	}
13414 
13415 	i40e_control_rx_q(pf, pf_q, enable);
13416 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13417 	if (ret) {
13418 		dev_info(&pf->pdev->dev,
13419 			 "VSI seid %d Rx ring %d %sable timeout\n",
13420 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13421 		return ret;
13422 	}
13423 
13424 	/* Due to HW errata, on Rx disable only, the register can
13425 	 * indicate done before it really is. Needs 50ms to be sure
13426 	 */
13427 	if (!enable)
13428 		mdelay(50);
13429 
13430 	if (!i40e_enabled_xdp_vsi(vsi))
13431 		return ret;
13432 
13433 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13434 				     pf_q + vsi->alloc_queue_pairs,
13435 				     true /*is xdp*/, enable);
13436 	if (ret) {
13437 		dev_info(&pf->pdev->dev,
13438 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13439 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13440 	}
13441 
13442 	return ret;
13443 }
13444 
13445 /**
13446  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13447  * @vsi: vsi
13448  * @queue_pair: queue_pair
13449  **/
13450 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13451 {
13452 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13453 	struct i40e_pf *pf = vsi->back;
13454 	struct i40e_hw *hw = &pf->hw;
13455 
13456 	/* All rings in a qp belong to the same qvector. */
13457 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13458 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13459 	else
13460 		i40e_irq_dynamic_enable_icr0(pf);
13461 
13462 	i40e_flush(hw);
13463 }
13464 
13465 /**
13466  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13467  * @vsi: vsi
13468  * @queue_pair: queue_pair
13469  **/
13470 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13471 {
13472 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13473 	struct i40e_pf *pf = vsi->back;
13474 	struct i40e_hw *hw = &pf->hw;
13475 
13476 	/* For simplicity, instead of removing the qp interrupt causes
13477 	 * from the interrupt linked list, we simply disable the interrupt, and
13478 	 * leave the list intact.
13479 	 *
13480 	 * All rings in a qp belong to the same qvector.
13481 	 */
13482 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13483 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13484 
13485 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13486 		i40e_flush(hw);
13487 		synchronize_irq(pf->msix_entries[intpf].vector);
13488 	} else {
13489 		/* Legacy and MSI mode - this stops all interrupt handling */
13490 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13491 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13492 		i40e_flush(hw);
13493 		synchronize_irq(pf->pdev->irq);
13494 	}
13495 }
13496 
13497 /**
13498  * i40e_queue_pair_disable - Disables a queue pair
13499  * @vsi: vsi
13500  * @queue_pair: queue pair
13501  *
13502  * Returns 0 on success, <0 on failure.
13503  **/
13504 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13505 {
13506 	int err;
13507 
13508 	err = i40e_enter_busy_conf(vsi);
13509 	if (err)
13510 		return err;
13511 
13512 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13513 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13514 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13515 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13516 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13517 
13518 	return err;
13519 }
13520 
13521 /**
13522  * i40e_queue_pair_enable - Enables a queue pair
13523  * @vsi: vsi
13524  * @queue_pair: queue pair
13525  *
13526  * Returns 0 on success, <0 on failure.
13527  **/
13528 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13529 {
13530 	int err;
13531 
13532 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13533 	if (err)
13534 		return err;
13535 
13536 	if (i40e_enabled_xdp_vsi(vsi)) {
13537 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13538 		if (err)
13539 			return err;
13540 	}
13541 
13542 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13543 	if (err)
13544 		return err;
13545 
13546 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13547 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13548 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13549 
13550 	i40e_exit_busy_conf(vsi);
13551 
13552 	return err;
13553 }
13554 
13555 /**
13556  * i40e_xdp - implements ndo_bpf for i40e
13557  * @dev: netdevice
13558  * @xdp: XDP command
13559  **/
13560 static int i40e_xdp(struct net_device *dev,
13561 		    struct netdev_bpf *xdp)
13562 {
13563 	struct i40e_netdev_priv *np = netdev_priv(dev);
13564 	struct i40e_vsi *vsi = np->vsi;
13565 
13566 	if (vsi->type != I40E_VSI_MAIN)
13567 		return -EINVAL;
13568 
13569 	switch (xdp->command) {
13570 	case XDP_SETUP_PROG:
13571 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13572 	case XDP_SETUP_XSK_POOL:
13573 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13574 					   xdp->xsk.queue_id);
13575 	default:
13576 		return -EINVAL;
13577 	}
13578 }
13579 
13580 static const struct net_device_ops i40e_netdev_ops = {
13581 	.ndo_open		= i40e_open,
13582 	.ndo_stop		= i40e_close,
13583 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13584 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13585 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13586 	.ndo_validate_addr	= eth_validate_addr,
13587 	.ndo_set_mac_address	= i40e_set_mac,
13588 	.ndo_change_mtu		= i40e_change_mtu,
13589 	.ndo_eth_ioctl		= i40e_ioctl,
13590 	.ndo_tx_timeout		= i40e_tx_timeout,
13591 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13592 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13593 #ifdef CONFIG_NET_POLL_CONTROLLER
13594 	.ndo_poll_controller	= i40e_netpoll,
13595 #endif
13596 	.ndo_setup_tc		= __i40e_setup_tc,
13597 	.ndo_select_queue	= i40e_lan_select_queue,
13598 	.ndo_set_features	= i40e_set_features,
13599 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13600 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13601 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13602 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13603 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13604 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13605 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13606 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13607 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13608 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13609 	.ndo_features_check	= i40e_features_check,
13610 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13611 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13612 	.ndo_bpf		= i40e_xdp,
13613 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13614 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13615 	.ndo_dfwd_add_station	= i40e_fwd_add,
13616 	.ndo_dfwd_del_station	= i40e_fwd_del,
13617 };
13618 
13619 /**
13620  * i40e_config_netdev - Setup the netdev flags
13621  * @vsi: the VSI being configured
13622  *
13623  * Returns 0 on success, negative value on failure
13624  **/
13625 static int i40e_config_netdev(struct i40e_vsi *vsi)
13626 {
13627 	struct i40e_pf *pf = vsi->back;
13628 	struct i40e_hw *hw = &pf->hw;
13629 	struct i40e_netdev_priv *np;
13630 	struct net_device *netdev;
13631 	u8 broadcast[ETH_ALEN];
13632 	u8 mac_addr[ETH_ALEN];
13633 	int etherdev_size;
13634 	netdev_features_t hw_enc_features;
13635 	netdev_features_t hw_features;
13636 
13637 	etherdev_size = sizeof(struct i40e_netdev_priv);
13638 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13639 	if (!netdev)
13640 		return -ENOMEM;
13641 
13642 	vsi->netdev = netdev;
13643 	np = netdev_priv(netdev);
13644 	np->vsi = vsi;
13645 
13646 	hw_enc_features = NETIF_F_SG			|
13647 			  NETIF_F_HW_CSUM		|
13648 			  NETIF_F_HIGHDMA		|
13649 			  NETIF_F_SOFT_FEATURES		|
13650 			  NETIF_F_TSO			|
13651 			  NETIF_F_TSO_ECN		|
13652 			  NETIF_F_TSO6			|
13653 			  NETIF_F_GSO_GRE		|
13654 			  NETIF_F_GSO_GRE_CSUM		|
13655 			  NETIF_F_GSO_PARTIAL		|
13656 			  NETIF_F_GSO_IPXIP4		|
13657 			  NETIF_F_GSO_IPXIP6		|
13658 			  NETIF_F_GSO_UDP_TUNNEL	|
13659 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13660 			  NETIF_F_GSO_UDP_L4		|
13661 			  NETIF_F_SCTP_CRC		|
13662 			  NETIF_F_RXHASH		|
13663 			  NETIF_F_RXCSUM		|
13664 			  0;
13665 
13666 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13667 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13668 
13669 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13670 
13671 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13672 
13673 	netdev->hw_enc_features |= hw_enc_features;
13674 
13675 	/* record features VLANs can make use of */
13676 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13677 
13678 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13679 				   NETIF_F_GSO_GRE_CSUM |	\
13680 				   NETIF_F_GSO_IPXIP4 |		\
13681 				   NETIF_F_GSO_IPXIP6 |		\
13682 				   NETIF_F_GSO_UDP_TUNNEL |	\
13683 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13684 
13685 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13686 	netdev->features |= NETIF_F_GSO_PARTIAL |
13687 			    I40E_GSO_PARTIAL_FEATURES;
13688 
13689 	netdev->mpls_features |= NETIF_F_SG;
13690 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13691 	netdev->mpls_features |= NETIF_F_TSO;
13692 	netdev->mpls_features |= NETIF_F_TSO6;
13693 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13694 
13695 	/* enable macvlan offloads */
13696 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13697 
13698 	hw_features = hw_enc_features		|
13699 		      NETIF_F_HW_VLAN_CTAG_TX	|
13700 		      NETIF_F_HW_VLAN_CTAG_RX;
13701 
13702 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13703 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13704 
13705 	netdev->hw_features |= hw_features;
13706 
13707 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13708 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13709 
13710 	netdev->features &= ~NETIF_F_HW_TC;
13711 
13712 	if (vsi->type == I40E_VSI_MAIN) {
13713 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13714 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13715 		/* The following steps are necessary for two reasons. First,
13716 		 * some older NVM configurations load a default MAC-VLAN
13717 		 * filter that will accept any tagged packet, and we want to
13718 		 * replace this with a normal filter. Additionally, it is
13719 		 * possible our MAC address was provided by the platform using
13720 		 * Open Firmware or similar.
13721 		 *
13722 		 * Thus, we need to remove the default filter and install one
13723 		 * specific to the MAC address.
13724 		 */
13725 		i40e_rm_default_mac_filter(vsi, mac_addr);
13726 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13727 		i40e_add_mac_filter(vsi, mac_addr);
13728 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13729 	} else {
13730 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13731 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13732 		 * the end, which is 4 bytes long, so force truncation of the
13733 		 * original name by IFNAMSIZ - 4
13734 		 */
13735 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13736 			 IFNAMSIZ - 4,
13737 			 pf->vsi[pf->lan_vsi]->netdev->name);
13738 		eth_random_addr(mac_addr);
13739 
13740 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13741 		i40e_add_mac_filter(vsi, mac_addr);
13742 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13743 	}
13744 
13745 	/* Add the broadcast filter so that we initially will receive
13746 	 * broadcast packets. Note that when a new VLAN is first added the
13747 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13748 	 * specific filters as part of transitioning into "vlan" operation.
13749 	 * When more VLANs are added, the driver will copy each existing MAC
13750 	 * filter and add it for the new VLAN.
13751 	 *
13752 	 * Broadcast filters are handled specially by
13753 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13754 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13755 	 * filter. The subtask will update the correct broadcast promiscuous
13756 	 * bits as VLANs become active or inactive.
13757 	 */
13758 	eth_broadcast_addr(broadcast);
13759 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13760 	i40e_add_mac_filter(vsi, broadcast);
13761 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13762 
13763 	eth_hw_addr_set(netdev, mac_addr);
13764 	ether_addr_copy(netdev->perm_addr, mac_addr);
13765 
13766 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13767 	netdev->neigh_priv_len = sizeof(u32) * 4;
13768 
13769 	netdev->priv_flags |= IFF_UNICAST_FLT;
13770 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13771 	/* Setup netdev TC information */
13772 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13773 
13774 	netdev->netdev_ops = &i40e_netdev_ops;
13775 	netdev->watchdog_timeo = 5 * HZ;
13776 	i40e_set_ethtool_ops(netdev);
13777 
13778 	/* MTU range: 68 - 9706 */
13779 	netdev->min_mtu = ETH_MIN_MTU;
13780 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13781 
13782 	return 0;
13783 }
13784 
13785 /**
13786  * i40e_vsi_delete - Delete a VSI from the switch
13787  * @vsi: the VSI being removed
13788  *
13789  * Returns 0 on success, negative value on failure
13790  **/
13791 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13792 {
13793 	/* remove default VSI is not allowed */
13794 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13795 		return;
13796 
13797 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13798 }
13799 
13800 /**
13801  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13802  * @vsi: the VSI being queried
13803  *
13804  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13805  **/
13806 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13807 {
13808 	struct i40e_veb *veb;
13809 	struct i40e_pf *pf = vsi->back;
13810 
13811 	/* Uplink is not a bridge so default to VEB */
13812 	if (vsi->veb_idx >= I40E_MAX_VEB)
13813 		return 1;
13814 
13815 	veb = pf->veb[vsi->veb_idx];
13816 	if (!veb) {
13817 		dev_info(&pf->pdev->dev,
13818 			 "There is no veb associated with the bridge\n");
13819 		return -ENOENT;
13820 	}
13821 
13822 	/* Uplink is a bridge in VEPA mode */
13823 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13824 		return 0;
13825 	} else {
13826 		/* Uplink is a bridge in VEB mode */
13827 		return 1;
13828 	}
13829 
13830 	/* VEPA is now default bridge, so return 0 */
13831 	return 0;
13832 }
13833 
13834 /**
13835  * i40e_add_vsi - Add a VSI to the switch
13836  * @vsi: the VSI being configured
13837  *
13838  * This initializes a VSI context depending on the VSI type to be added and
13839  * passes it down to the add_vsi aq command.
13840  **/
13841 static int i40e_add_vsi(struct i40e_vsi *vsi)
13842 {
13843 	int ret = -ENODEV;
13844 	struct i40e_pf *pf = vsi->back;
13845 	struct i40e_hw *hw = &pf->hw;
13846 	struct i40e_vsi_context ctxt;
13847 	struct i40e_mac_filter *f;
13848 	struct hlist_node *h;
13849 	int bkt;
13850 
13851 	u8 enabled_tc = 0x1; /* TC0 enabled */
13852 	int f_count = 0;
13853 
13854 	memset(&ctxt, 0, sizeof(ctxt));
13855 	switch (vsi->type) {
13856 	case I40E_VSI_MAIN:
13857 		/* The PF's main VSI is already setup as part of the
13858 		 * device initialization, so we'll not bother with
13859 		 * the add_vsi call, but we will retrieve the current
13860 		 * VSI context.
13861 		 */
13862 		ctxt.seid = pf->main_vsi_seid;
13863 		ctxt.pf_num = pf->hw.pf_id;
13864 		ctxt.vf_num = 0;
13865 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13866 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13867 		if (ret) {
13868 			dev_info(&pf->pdev->dev,
13869 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13870 				 i40e_stat_str(&pf->hw, ret),
13871 				 i40e_aq_str(&pf->hw,
13872 					     pf->hw.aq.asq_last_status));
13873 			return -ENOENT;
13874 		}
13875 		vsi->info = ctxt.info;
13876 		vsi->info.valid_sections = 0;
13877 
13878 		vsi->seid = ctxt.seid;
13879 		vsi->id = ctxt.vsi_number;
13880 
13881 		enabled_tc = i40e_pf_get_tc_map(pf);
13882 
13883 		/* Source pruning is enabled by default, so the flag is
13884 		 * negative logic - if it's set, we need to fiddle with
13885 		 * the VSI to disable source pruning.
13886 		 */
13887 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13888 			memset(&ctxt, 0, sizeof(ctxt));
13889 			ctxt.seid = pf->main_vsi_seid;
13890 			ctxt.pf_num = pf->hw.pf_id;
13891 			ctxt.vf_num = 0;
13892 			ctxt.info.valid_sections |=
13893 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13894 			ctxt.info.switch_id =
13895 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13896 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13897 			if (ret) {
13898 				dev_info(&pf->pdev->dev,
13899 					 "update vsi failed, err %s aq_err %s\n",
13900 					 i40e_stat_str(&pf->hw, ret),
13901 					 i40e_aq_str(&pf->hw,
13902 						     pf->hw.aq.asq_last_status));
13903 				ret = -ENOENT;
13904 				goto err;
13905 			}
13906 		}
13907 
13908 		/* MFP mode setup queue map and update VSI */
13909 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13910 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13911 			memset(&ctxt, 0, sizeof(ctxt));
13912 			ctxt.seid = pf->main_vsi_seid;
13913 			ctxt.pf_num = pf->hw.pf_id;
13914 			ctxt.vf_num = 0;
13915 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13916 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13917 			if (ret) {
13918 				dev_info(&pf->pdev->dev,
13919 					 "update vsi failed, err %s aq_err %s\n",
13920 					 i40e_stat_str(&pf->hw, ret),
13921 					 i40e_aq_str(&pf->hw,
13922 						    pf->hw.aq.asq_last_status));
13923 				ret = -ENOENT;
13924 				goto err;
13925 			}
13926 			/* update the local VSI info queue map */
13927 			i40e_vsi_update_queue_map(vsi, &ctxt);
13928 			vsi->info.valid_sections = 0;
13929 		} else {
13930 			/* Default/Main VSI is only enabled for TC0
13931 			 * reconfigure it to enable all TCs that are
13932 			 * available on the port in SFP mode.
13933 			 * For MFP case the iSCSI PF would use this
13934 			 * flow to enable LAN+iSCSI TC.
13935 			 */
13936 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13937 			if (ret) {
13938 				/* Single TC condition is not fatal,
13939 				 * message and continue
13940 				 */
13941 				dev_info(&pf->pdev->dev,
13942 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13943 					 enabled_tc,
13944 					 i40e_stat_str(&pf->hw, ret),
13945 					 i40e_aq_str(&pf->hw,
13946 						    pf->hw.aq.asq_last_status));
13947 			}
13948 		}
13949 		break;
13950 
13951 	case I40E_VSI_FDIR:
13952 		ctxt.pf_num = hw->pf_id;
13953 		ctxt.vf_num = 0;
13954 		ctxt.uplink_seid = vsi->uplink_seid;
13955 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13956 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13957 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13958 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13959 			ctxt.info.valid_sections |=
13960 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13961 			ctxt.info.switch_id =
13962 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13963 		}
13964 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13965 		break;
13966 
13967 	case I40E_VSI_VMDQ2:
13968 		ctxt.pf_num = hw->pf_id;
13969 		ctxt.vf_num = 0;
13970 		ctxt.uplink_seid = vsi->uplink_seid;
13971 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13972 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13973 
13974 		/* This VSI is connected to VEB so the switch_id
13975 		 * should be set to zero by default.
13976 		 */
13977 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13978 			ctxt.info.valid_sections |=
13979 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13980 			ctxt.info.switch_id =
13981 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13982 		}
13983 
13984 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13985 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13986 		break;
13987 
13988 	case I40E_VSI_SRIOV:
13989 		ctxt.pf_num = hw->pf_id;
13990 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13991 		ctxt.uplink_seid = vsi->uplink_seid;
13992 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13993 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13994 
13995 		/* This VSI is connected to VEB so the switch_id
13996 		 * should be set to zero by default.
13997 		 */
13998 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13999 			ctxt.info.valid_sections |=
14000 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14001 			ctxt.info.switch_id =
14002 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14003 		}
14004 
14005 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14006 			ctxt.info.valid_sections |=
14007 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14008 			ctxt.info.queueing_opt_flags |=
14009 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14010 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14011 		}
14012 
14013 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14014 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14015 		if (pf->vf[vsi->vf_id].spoofchk) {
14016 			ctxt.info.valid_sections |=
14017 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14018 			ctxt.info.sec_flags |=
14019 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14020 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14021 		}
14022 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14023 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14024 		break;
14025 
14026 	case I40E_VSI_IWARP:
14027 		/* send down message to iWARP */
14028 		break;
14029 
14030 	default:
14031 		return -ENODEV;
14032 	}
14033 
14034 	if (vsi->type != I40E_VSI_MAIN) {
14035 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14036 		if (ret) {
14037 			dev_info(&vsi->back->pdev->dev,
14038 				 "add vsi failed, err %s aq_err %s\n",
14039 				 i40e_stat_str(&pf->hw, ret),
14040 				 i40e_aq_str(&pf->hw,
14041 					     pf->hw.aq.asq_last_status));
14042 			ret = -ENOENT;
14043 			goto err;
14044 		}
14045 		vsi->info = ctxt.info;
14046 		vsi->info.valid_sections = 0;
14047 		vsi->seid = ctxt.seid;
14048 		vsi->id = ctxt.vsi_number;
14049 	}
14050 
14051 	vsi->active_filters = 0;
14052 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14053 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14054 	/* If macvlan filters already exist, force them to get loaded */
14055 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14056 		f->state = I40E_FILTER_NEW;
14057 		f_count++;
14058 	}
14059 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14060 
14061 	if (f_count) {
14062 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14063 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14064 	}
14065 
14066 	/* Update VSI BW information */
14067 	ret = i40e_vsi_get_bw_info(vsi);
14068 	if (ret) {
14069 		dev_info(&pf->pdev->dev,
14070 			 "couldn't get vsi bw info, err %s aq_err %s\n",
14071 			 i40e_stat_str(&pf->hw, ret),
14072 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14073 		/* VSI is already added so not tearing that up */
14074 		ret = 0;
14075 	}
14076 
14077 err:
14078 	return ret;
14079 }
14080 
14081 /**
14082  * i40e_vsi_release - Delete a VSI and free its resources
14083  * @vsi: the VSI being removed
14084  *
14085  * Returns 0 on success or < 0 on error
14086  **/
14087 int i40e_vsi_release(struct i40e_vsi *vsi)
14088 {
14089 	struct i40e_mac_filter *f;
14090 	struct hlist_node *h;
14091 	struct i40e_veb *veb = NULL;
14092 	struct i40e_pf *pf;
14093 	u16 uplink_seid;
14094 	int i, n, bkt;
14095 
14096 	pf = vsi->back;
14097 
14098 	/* release of a VEB-owner or last VSI is not allowed */
14099 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14100 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14101 			 vsi->seid, vsi->uplink_seid);
14102 		return -ENODEV;
14103 	}
14104 	if (vsi == pf->vsi[pf->lan_vsi] &&
14105 	    !test_bit(__I40E_DOWN, pf->state)) {
14106 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14107 		return -ENODEV;
14108 	}
14109 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14110 	uplink_seid = vsi->uplink_seid;
14111 	if (vsi->type != I40E_VSI_SRIOV) {
14112 		if (vsi->netdev_registered) {
14113 			vsi->netdev_registered = false;
14114 			if (vsi->netdev) {
14115 				/* results in a call to i40e_close() */
14116 				unregister_netdev(vsi->netdev);
14117 			}
14118 		} else {
14119 			i40e_vsi_close(vsi);
14120 		}
14121 		i40e_vsi_disable_irq(vsi);
14122 	}
14123 
14124 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14125 
14126 	/* clear the sync flag on all filters */
14127 	if (vsi->netdev) {
14128 		__dev_uc_unsync(vsi->netdev, NULL);
14129 		__dev_mc_unsync(vsi->netdev, NULL);
14130 	}
14131 
14132 	/* make sure any remaining filters are marked for deletion */
14133 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14134 		__i40e_del_filter(vsi, f);
14135 
14136 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14137 
14138 	i40e_sync_vsi_filters(vsi);
14139 
14140 	i40e_vsi_delete(vsi);
14141 	i40e_vsi_free_q_vectors(vsi);
14142 	if (vsi->netdev) {
14143 		free_netdev(vsi->netdev);
14144 		vsi->netdev = NULL;
14145 	}
14146 	i40e_vsi_clear_rings(vsi);
14147 	i40e_vsi_clear(vsi);
14148 
14149 	/* If this was the last thing on the VEB, except for the
14150 	 * controlling VSI, remove the VEB, which puts the controlling
14151 	 * VSI onto the next level down in the switch.
14152 	 *
14153 	 * Well, okay, there's one more exception here: don't remove
14154 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14155 	 * from up the network stack.
14156 	 */
14157 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14158 		if (pf->vsi[i] &&
14159 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14160 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14161 			n++;      /* count the VSIs */
14162 		}
14163 	}
14164 	for (i = 0; i < I40E_MAX_VEB; i++) {
14165 		if (!pf->veb[i])
14166 			continue;
14167 		if (pf->veb[i]->uplink_seid == uplink_seid)
14168 			n++;     /* count the VEBs */
14169 		if (pf->veb[i]->seid == uplink_seid)
14170 			veb = pf->veb[i];
14171 	}
14172 	if (n == 0 && veb && veb->uplink_seid != 0)
14173 		i40e_veb_release(veb);
14174 
14175 	return 0;
14176 }
14177 
14178 /**
14179  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14180  * @vsi: ptr to the VSI
14181  *
14182  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14183  * corresponding SW VSI structure and initializes num_queue_pairs for the
14184  * newly allocated VSI.
14185  *
14186  * Returns 0 on success or negative on failure
14187  **/
14188 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14189 {
14190 	int ret = -ENOENT;
14191 	struct i40e_pf *pf = vsi->back;
14192 
14193 	if (vsi->q_vectors[0]) {
14194 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14195 			 vsi->seid);
14196 		return -EEXIST;
14197 	}
14198 
14199 	if (vsi->base_vector) {
14200 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14201 			 vsi->seid, vsi->base_vector);
14202 		return -EEXIST;
14203 	}
14204 
14205 	ret = i40e_vsi_alloc_q_vectors(vsi);
14206 	if (ret) {
14207 		dev_info(&pf->pdev->dev,
14208 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14209 			 vsi->num_q_vectors, vsi->seid, ret);
14210 		vsi->num_q_vectors = 0;
14211 		goto vector_setup_out;
14212 	}
14213 
14214 	/* In Legacy mode, we do not have to get any other vector since we
14215 	 * piggyback on the misc/ICR0 for queue interrupts.
14216 	*/
14217 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14218 		return ret;
14219 	if (vsi->num_q_vectors)
14220 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14221 						 vsi->num_q_vectors, vsi->idx);
14222 	if (vsi->base_vector < 0) {
14223 		dev_info(&pf->pdev->dev,
14224 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14225 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14226 		i40e_vsi_free_q_vectors(vsi);
14227 		ret = -ENOENT;
14228 		goto vector_setup_out;
14229 	}
14230 
14231 vector_setup_out:
14232 	return ret;
14233 }
14234 
14235 /**
14236  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14237  * @vsi: pointer to the vsi.
14238  *
14239  * This re-allocates a vsi's queue resources.
14240  *
14241  * Returns pointer to the successfully allocated and configured VSI sw struct
14242  * on success, otherwise returns NULL on failure.
14243  **/
14244 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14245 {
14246 	u16 alloc_queue_pairs;
14247 	struct i40e_pf *pf;
14248 	u8 enabled_tc;
14249 	int ret;
14250 
14251 	if (!vsi)
14252 		return NULL;
14253 
14254 	pf = vsi->back;
14255 
14256 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14257 	i40e_vsi_clear_rings(vsi);
14258 
14259 	i40e_vsi_free_arrays(vsi, false);
14260 	i40e_set_num_rings_in_vsi(vsi);
14261 	ret = i40e_vsi_alloc_arrays(vsi, false);
14262 	if (ret)
14263 		goto err_vsi;
14264 
14265 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14266 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14267 
14268 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14269 	if (ret < 0) {
14270 		dev_info(&pf->pdev->dev,
14271 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14272 			 alloc_queue_pairs, vsi->seid, ret);
14273 		goto err_vsi;
14274 	}
14275 	vsi->base_queue = ret;
14276 
14277 	/* Update the FW view of the VSI. Force a reset of TC and queue
14278 	 * layout configurations.
14279 	 */
14280 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14281 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14282 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14283 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14284 	if (vsi->type == I40E_VSI_MAIN)
14285 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14286 
14287 	/* assign it some queues */
14288 	ret = i40e_alloc_rings(vsi);
14289 	if (ret)
14290 		goto err_rings;
14291 
14292 	/* map all of the rings to the q_vectors */
14293 	i40e_vsi_map_rings_to_vectors(vsi);
14294 	return vsi;
14295 
14296 err_rings:
14297 	i40e_vsi_free_q_vectors(vsi);
14298 	if (vsi->netdev_registered) {
14299 		vsi->netdev_registered = false;
14300 		unregister_netdev(vsi->netdev);
14301 		free_netdev(vsi->netdev);
14302 		vsi->netdev = NULL;
14303 	}
14304 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14305 err_vsi:
14306 	i40e_vsi_clear(vsi);
14307 	return NULL;
14308 }
14309 
14310 /**
14311  * i40e_vsi_setup - Set up a VSI by a given type
14312  * @pf: board private structure
14313  * @type: VSI type
14314  * @uplink_seid: the switch element to link to
14315  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14316  *
14317  * This allocates the sw VSI structure and its queue resources, then add a VSI
14318  * to the identified VEB.
14319  *
14320  * Returns pointer to the successfully allocated and configure VSI sw struct on
14321  * success, otherwise returns NULL on failure.
14322  **/
14323 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14324 				u16 uplink_seid, u32 param1)
14325 {
14326 	struct i40e_vsi *vsi = NULL;
14327 	struct i40e_veb *veb = NULL;
14328 	u16 alloc_queue_pairs;
14329 	int ret, i;
14330 	int v_idx;
14331 
14332 	/* The requested uplink_seid must be either
14333 	 *     - the PF's port seid
14334 	 *              no VEB is needed because this is the PF
14335 	 *              or this is a Flow Director special case VSI
14336 	 *     - seid of an existing VEB
14337 	 *     - seid of a VSI that owns an existing VEB
14338 	 *     - seid of a VSI that doesn't own a VEB
14339 	 *              a new VEB is created and the VSI becomes the owner
14340 	 *     - seid of the PF VSI, which is what creates the first VEB
14341 	 *              this is a special case of the previous
14342 	 *
14343 	 * Find which uplink_seid we were given and create a new VEB if needed
14344 	 */
14345 	for (i = 0; i < I40E_MAX_VEB; i++) {
14346 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14347 			veb = pf->veb[i];
14348 			break;
14349 		}
14350 	}
14351 
14352 	if (!veb && uplink_seid != pf->mac_seid) {
14353 
14354 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14355 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14356 				vsi = pf->vsi[i];
14357 				break;
14358 			}
14359 		}
14360 		if (!vsi) {
14361 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14362 				 uplink_seid);
14363 			return NULL;
14364 		}
14365 
14366 		if (vsi->uplink_seid == pf->mac_seid)
14367 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14368 					     vsi->tc_config.enabled_tc);
14369 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14370 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14371 					     vsi->tc_config.enabled_tc);
14372 		if (veb) {
14373 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14374 				dev_info(&vsi->back->pdev->dev,
14375 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14376 				return NULL;
14377 			}
14378 			/* We come up by default in VEPA mode if SRIOV is not
14379 			 * already enabled, in which case we can't force VEPA
14380 			 * mode.
14381 			 */
14382 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14383 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14384 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14385 			}
14386 			i40e_config_bridge_mode(veb);
14387 		}
14388 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14389 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14390 				veb = pf->veb[i];
14391 		}
14392 		if (!veb) {
14393 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14394 			return NULL;
14395 		}
14396 
14397 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14398 		uplink_seid = veb->seid;
14399 	}
14400 
14401 	/* get vsi sw struct */
14402 	v_idx = i40e_vsi_mem_alloc(pf, type);
14403 	if (v_idx < 0)
14404 		goto err_alloc;
14405 	vsi = pf->vsi[v_idx];
14406 	if (!vsi)
14407 		goto err_alloc;
14408 	vsi->type = type;
14409 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14410 
14411 	if (type == I40E_VSI_MAIN)
14412 		pf->lan_vsi = v_idx;
14413 	else if (type == I40E_VSI_SRIOV)
14414 		vsi->vf_id = param1;
14415 	/* assign it some queues */
14416 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14417 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14418 
14419 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14420 	if (ret < 0) {
14421 		dev_info(&pf->pdev->dev,
14422 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14423 			 alloc_queue_pairs, vsi->seid, ret);
14424 		goto err_vsi;
14425 	}
14426 	vsi->base_queue = ret;
14427 
14428 	/* get a VSI from the hardware */
14429 	vsi->uplink_seid = uplink_seid;
14430 	ret = i40e_add_vsi(vsi);
14431 	if (ret)
14432 		goto err_vsi;
14433 
14434 	switch (vsi->type) {
14435 	/* setup the netdev if needed */
14436 	case I40E_VSI_MAIN:
14437 	case I40E_VSI_VMDQ2:
14438 		ret = i40e_config_netdev(vsi);
14439 		if (ret)
14440 			goto err_netdev;
14441 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14442 		if (ret)
14443 			goto err_netdev;
14444 		ret = register_netdev(vsi->netdev);
14445 		if (ret)
14446 			goto err_netdev;
14447 		vsi->netdev_registered = true;
14448 		netif_carrier_off(vsi->netdev);
14449 #ifdef CONFIG_I40E_DCB
14450 		/* Setup DCB netlink interface */
14451 		i40e_dcbnl_setup(vsi);
14452 #endif /* CONFIG_I40E_DCB */
14453 		fallthrough;
14454 	case I40E_VSI_FDIR:
14455 		/* set up vectors and rings if needed */
14456 		ret = i40e_vsi_setup_vectors(vsi);
14457 		if (ret)
14458 			goto err_msix;
14459 
14460 		ret = i40e_alloc_rings(vsi);
14461 		if (ret)
14462 			goto err_rings;
14463 
14464 		/* map all of the rings to the q_vectors */
14465 		i40e_vsi_map_rings_to_vectors(vsi);
14466 
14467 		i40e_vsi_reset_stats(vsi);
14468 		break;
14469 	default:
14470 		/* no netdev or rings for the other VSI types */
14471 		break;
14472 	}
14473 
14474 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14475 	    (vsi->type == I40E_VSI_VMDQ2)) {
14476 		ret = i40e_vsi_config_rss(vsi);
14477 	}
14478 	return vsi;
14479 
14480 err_rings:
14481 	i40e_vsi_free_q_vectors(vsi);
14482 err_msix:
14483 	if (vsi->netdev_registered) {
14484 		vsi->netdev_registered = false;
14485 		unregister_netdev(vsi->netdev);
14486 		free_netdev(vsi->netdev);
14487 		vsi->netdev = NULL;
14488 	}
14489 err_netdev:
14490 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14491 err_vsi:
14492 	i40e_vsi_clear(vsi);
14493 err_alloc:
14494 	return NULL;
14495 }
14496 
14497 /**
14498  * i40e_veb_get_bw_info - Query VEB BW information
14499  * @veb: the veb to query
14500  *
14501  * Query the Tx scheduler BW configuration data for given VEB
14502  **/
14503 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14504 {
14505 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14506 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14507 	struct i40e_pf *pf = veb->pf;
14508 	struct i40e_hw *hw = &pf->hw;
14509 	u32 tc_bw_max;
14510 	int ret = 0;
14511 	int i;
14512 
14513 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14514 						  &bw_data, NULL);
14515 	if (ret) {
14516 		dev_info(&pf->pdev->dev,
14517 			 "query veb bw config failed, err %s aq_err %s\n",
14518 			 i40e_stat_str(&pf->hw, ret),
14519 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14520 		goto out;
14521 	}
14522 
14523 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14524 						   &ets_data, NULL);
14525 	if (ret) {
14526 		dev_info(&pf->pdev->dev,
14527 			 "query veb bw ets config failed, err %s aq_err %s\n",
14528 			 i40e_stat_str(&pf->hw, ret),
14529 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14530 		goto out;
14531 	}
14532 
14533 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14534 	veb->bw_max_quanta = ets_data.tc_bw_max;
14535 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14536 	veb->enabled_tc = ets_data.tc_valid_bits;
14537 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14538 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14539 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14540 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14541 		veb->bw_tc_limit_credits[i] =
14542 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14543 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14544 	}
14545 
14546 out:
14547 	return ret;
14548 }
14549 
14550 /**
14551  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14552  * @pf: board private structure
14553  *
14554  * On error: returns error code (negative)
14555  * On success: returns vsi index in PF (positive)
14556  **/
14557 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14558 {
14559 	int ret = -ENOENT;
14560 	struct i40e_veb *veb;
14561 	int i;
14562 
14563 	/* Need to protect the allocation of switch elements at the PF level */
14564 	mutex_lock(&pf->switch_mutex);
14565 
14566 	/* VEB list may be fragmented if VEB creation/destruction has
14567 	 * been happening.  We can afford to do a quick scan to look
14568 	 * for any free slots in the list.
14569 	 *
14570 	 * find next empty veb slot, looping back around if necessary
14571 	 */
14572 	i = 0;
14573 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14574 		i++;
14575 	if (i >= I40E_MAX_VEB) {
14576 		ret = -ENOMEM;
14577 		goto err_alloc_veb;  /* out of VEB slots! */
14578 	}
14579 
14580 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14581 	if (!veb) {
14582 		ret = -ENOMEM;
14583 		goto err_alloc_veb;
14584 	}
14585 	veb->pf = pf;
14586 	veb->idx = i;
14587 	veb->enabled_tc = 1;
14588 
14589 	pf->veb[i] = veb;
14590 	ret = i;
14591 err_alloc_veb:
14592 	mutex_unlock(&pf->switch_mutex);
14593 	return ret;
14594 }
14595 
14596 /**
14597  * i40e_switch_branch_release - Delete a branch of the switch tree
14598  * @branch: where to start deleting
14599  *
14600  * This uses recursion to find the tips of the branch to be
14601  * removed, deleting until we get back to and can delete this VEB.
14602  **/
14603 static void i40e_switch_branch_release(struct i40e_veb *branch)
14604 {
14605 	struct i40e_pf *pf = branch->pf;
14606 	u16 branch_seid = branch->seid;
14607 	u16 veb_idx = branch->idx;
14608 	int i;
14609 
14610 	/* release any VEBs on this VEB - RECURSION */
14611 	for (i = 0; i < I40E_MAX_VEB; i++) {
14612 		if (!pf->veb[i])
14613 			continue;
14614 		if (pf->veb[i]->uplink_seid == branch->seid)
14615 			i40e_switch_branch_release(pf->veb[i]);
14616 	}
14617 
14618 	/* Release the VSIs on this VEB, but not the owner VSI.
14619 	 *
14620 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14621 	 *       the VEB itself, so don't use (*branch) after this loop.
14622 	 */
14623 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14624 		if (!pf->vsi[i])
14625 			continue;
14626 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14627 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14628 			i40e_vsi_release(pf->vsi[i]);
14629 		}
14630 	}
14631 
14632 	/* There's one corner case where the VEB might not have been
14633 	 * removed, so double check it here and remove it if needed.
14634 	 * This case happens if the veb was created from the debugfs
14635 	 * commands and no VSIs were added to it.
14636 	 */
14637 	if (pf->veb[veb_idx])
14638 		i40e_veb_release(pf->veb[veb_idx]);
14639 }
14640 
14641 /**
14642  * i40e_veb_clear - remove veb struct
14643  * @veb: the veb to remove
14644  **/
14645 static void i40e_veb_clear(struct i40e_veb *veb)
14646 {
14647 	if (!veb)
14648 		return;
14649 
14650 	if (veb->pf) {
14651 		struct i40e_pf *pf = veb->pf;
14652 
14653 		mutex_lock(&pf->switch_mutex);
14654 		if (pf->veb[veb->idx] == veb)
14655 			pf->veb[veb->idx] = NULL;
14656 		mutex_unlock(&pf->switch_mutex);
14657 	}
14658 
14659 	kfree(veb);
14660 }
14661 
14662 /**
14663  * i40e_veb_release - Delete a VEB and free its resources
14664  * @veb: the VEB being removed
14665  **/
14666 void i40e_veb_release(struct i40e_veb *veb)
14667 {
14668 	struct i40e_vsi *vsi = NULL;
14669 	struct i40e_pf *pf;
14670 	int i, n = 0;
14671 
14672 	pf = veb->pf;
14673 
14674 	/* find the remaining VSI and check for extras */
14675 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14676 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14677 			n++;
14678 			vsi = pf->vsi[i];
14679 		}
14680 	}
14681 	if (n != 1) {
14682 		dev_info(&pf->pdev->dev,
14683 			 "can't remove VEB %d with %d VSIs left\n",
14684 			 veb->seid, n);
14685 		return;
14686 	}
14687 
14688 	/* move the remaining VSI to uplink veb */
14689 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14690 	if (veb->uplink_seid) {
14691 		vsi->uplink_seid = veb->uplink_seid;
14692 		if (veb->uplink_seid == pf->mac_seid)
14693 			vsi->veb_idx = I40E_NO_VEB;
14694 		else
14695 			vsi->veb_idx = veb->veb_idx;
14696 	} else {
14697 		/* floating VEB */
14698 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14699 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14700 	}
14701 
14702 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14703 	i40e_veb_clear(veb);
14704 }
14705 
14706 /**
14707  * i40e_add_veb - create the VEB in the switch
14708  * @veb: the VEB to be instantiated
14709  * @vsi: the controlling VSI
14710  **/
14711 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14712 {
14713 	struct i40e_pf *pf = veb->pf;
14714 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14715 	int ret;
14716 
14717 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14718 			      veb->enabled_tc, false,
14719 			      &veb->seid, enable_stats, NULL);
14720 
14721 	/* get a VEB from the hardware */
14722 	if (ret) {
14723 		dev_info(&pf->pdev->dev,
14724 			 "couldn't add VEB, err %s aq_err %s\n",
14725 			 i40e_stat_str(&pf->hw, ret),
14726 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14727 		return -EPERM;
14728 	}
14729 
14730 	/* get statistics counter */
14731 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14732 					 &veb->stats_idx, NULL, NULL, NULL);
14733 	if (ret) {
14734 		dev_info(&pf->pdev->dev,
14735 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14736 			 i40e_stat_str(&pf->hw, ret),
14737 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14738 		return -EPERM;
14739 	}
14740 	ret = i40e_veb_get_bw_info(veb);
14741 	if (ret) {
14742 		dev_info(&pf->pdev->dev,
14743 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14744 			 i40e_stat_str(&pf->hw, ret),
14745 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14746 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14747 		return -ENOENT;
14748 	}
14749 
14750 	vsi->uplink_seid = veb->seid;
14751 	vsi->veb_idx = veb->idx;
14752 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14753 
14754 	return 0;
14755 }
14756 
14757 /**
14758  * i40e_veb_setup - Set up a VEB
14759  * @pf: board private structure
14760  * @flags: VEB setup flags
14761  * @uplink_seid: the switch element to link to
14762  * @vsi_seid: the initial VSI seid
14763  * @enabled_tc: Enabled TC bit-map
14764  *
14765  * This allocates the sw VEB structure and links it into the switch
14766  * It is possible and legal for this to be a duplicate of an already
14767  * existing VEB.  It is also possible for both uplink and vsi seids
14768  * to be zero, in order to create a floating VEB.
14769  *
14770  * Returns pointer to the successfully allocated VEB sw struct on
14771  * success, otherwise returns NULL on failure.
14772  **/
14773 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14774 				u16 uplink_seid, u16 vsi_seid,
14775 				u8 enabled_tc)
14776 {
14777 	struct i40e_veb *veb, *uplink_veb = NULL;
14778 	int vsi_idx, veb_idx;
14779 	int ret;
14780 
14781 	/* if one seid is 0, the other must be 0 to create a floating relay */
14782 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14783 	    (uplink_seid + vsi_seid != 0)) {
14784 		dev_info(&pf->pdev->dev,
14785 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14786 			 uplink_seid, vsi_seid);
14787 		return NULL;
14788 	}
14789 
14790 	/* make sure there is such a vsi and uplink */
14791 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14792 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14793 			break;
14794 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14795 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14796 			 vsi_seid);
14797 		return NULL;
14798 	}
14799 
14800 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14801 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14802 			if (pf->veb[veb_idx] &&
14803 			    pf->veb[veb_idx]->seid == uplink_seid) {
14804 				uplink_veb = pf->veb[veb_idx];
14805 				break;
14806 			}
14807 		}
14808 		if (!uplink_veb) {
14809 			dev_info(&pf->pdev->dev,
14810 				 "uplink seid %d not found\n", uplink_seid);
14811 			return NULL;
14812 		}
14813 	}
14814 
14815 	/* get veb sw struct */
14816 	veb_idx = i40e_veb_mem_alloc(pf);
14817 	if (veb_idx < 0)
14818 		goto err_alloc;
14819 	veb = pf->veb[veb_idx];
14820 	veb->flags = flags;
14821 	veb->uplink_seid = uplink_seid;
14822 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14823 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14824 
14825 	/* create the VEB in the switch */
14826 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14827 	if (ret)
14828 		goto err_veb;
14829 	if (vsi_idx == pf->lan_vsi)
14830 		pf->lan_veb = veb->idx;
14831 
14832 	return veb;
14833 
14834 err_veb:
14835 	i40e_veb_clear(veb);
14836 err_alloc:
14837 	return NULL;
14838 }
14839 
14840 /**
14841  * i40e_setup_pf_switch_element - set PF vars based on switch type
14842  * @pf: board private structure
14843  * @ele: element we are building info from
14844  * @num_reported: total number of elements
14845  * @printconfig: should we print the contents
14846  *
14847  * helper function to assist in extracting a few useful SEID values.
14848  **/
14849 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14850 				struct i40e_aqc_switch_config_element_resp *ele,
14851 				u16 num_reported, bool printconfig)
14852 {
14853 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14854 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14855 	u8 element_type = ele->element_type;
14856 	u16 seid = le16_to_cpu(ele->seid);
14857 
14858 	if (printconfig)
14859 		dev_info(&pf->pdev->dev,
14860 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14861 			 element_type, seid, uplink_seid, downlink_seid);
14862 
14863 	switch (element_type) {
14864 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14865 		pf->mac_seid = seid;
14866 		break;
14867 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14868 		/* Main VEB? */
14869 		if (uplink_seid != pf->mac_seid)
14870 			break;
14871 		if (pf->lan_veb >= I40E_MAX_VEB) {
14872 			int v;
14873 
14874 			/* find existing or else empty VEB */
14875 			for (v = 0; v < I40E_MAX_VEB; v++) {
14876 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14877 					pf->lan_veb = v;
14878 					break;
14879 				}
14880 			}
14881 			if (pf->lan_veb >= I40E_MAX_VEB) {
14882 				v = i40e_veb_mem_alloc(pf);
14883 				if (v < 0)
14884 					break;
14885 				pf->lan_veb = v;
14886 			}
14887 		}
14888 		if (pf->lan_veb >= I40E_MAX_VEB)
14889 			break;
14890 
14891 		pf->veb[pf->lan_veb]->seid = seid;
14892 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14893 		pf->veb[pf->lan_veb]->pf = pf;
14894 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14895 		break;
14896 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14897 		if (num_reported != 1)
14898 			break;
14899 		/* This is immediately after a reset so we can assume this is
14900 		 * the PF's VSI
14901 		 */
14902 		pf->mac_seid = uplink_seid;
14903 		pf->pf_seid = downlink_seid;
14904 		pf->main_vsi_seid = seid;
14905 		if (printconfig)
14906 			dev_info(&pf->pdev->dev,
14907 				 "pf_seid=%d main_vsi_seid=%d\n",
14908 				 pf->pf_seid, pf->main_vsi_seid);
14909 		break;
14910 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14911 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14912 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14913 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14914 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14915 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14916 		/* ignore these for now */
14917 		break;
14918 	default:
14919 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14920 			 element_type, seid);
14921 		break;
14922 	}
14923 }
14924 
14925 /**
14926  * i40e_fetch_switch_configuration - Get switch config from firmware
14927  * @pf: board private structure
14928  * @printconfig: should we print the contents
14929  *
14930  * Get the current switch configuration from the device and
14931  * extract a few useful SEID values.
14932  **/
14933 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14934 {
14935 	struct i40e_aqc_get_switch_config_resp *sw_config;
14936 	u16 next_seid = 0;
14937 	int ret = 0;
14938 	u8 *aq_buf;
14939 	int i;
14940 
14941 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14942 	if (!aq_buf)
14943 		return -ENOMEM;
14944 
14945 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14946 	do {
14947 		u16 num_reported, num_total;
14948 
14949 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14950 						I40E_AQ_LARGE_BUF,
14951 						&next_seid, NULL);
14952 		if (ret) {
14953 			dev_info(&pf->pdev->dev,
14954 				 "get switch config failed err %s aq_err %s\n",
14955 				 i40e_stat_str(&pf->hw, ret),
14956 				 i40e_aq_str(&pf->hw,
14957 					     pf->hw.aq.asq_last_status));
14958 			kfree(aq_buf);
14959 			return -ENOENT;
14960 		}
14961 
14962 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14963 		num_total = le16_to_cpu(sw_config->header.num_total);
14964 
14965 		if (printconfig)
14966 			dev_info(&pf->pdev->dev,
14967 				 "header: %d reported %d total\n",
14968 				 num_reported, num_total);
14969 
14970 		for (i = 0; i < num_reported; i++) {
14971 			struct i40e_aqc_switch_config_element_resp *ele =
14972 				&sw_config->element[i];
14973 
14974 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14975 						     printconfig);
14976 		}
14977 	} while (next_seid != 0);
14978 
14979 	kfree(aq_buf);
14980 	return ret;
14981 }
14982 
14983 /**
14984  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14985  * @pf: board private structure
14986  * @reinit: if the Main VSI needs to re-initialized.
14987  * @lock_acquired: indicates whether or not the lock has been acquired
14988  *
14989  * Returns 0 on success, negative value on failure
14990  **/
14991 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14992 {
14993 	u16 flags = 0;
14994 	int ret;
14995 
14996 	/* find out what's out there already */
14997 	ret = i40e_fetch_switch_configuration(pf, false);
14998 	if (ret) {
14999 		dev_info(&pf->pdev->dev,
15000 			 "couldn't fetch switch config, err %s aq_err %s\n",
15001 			 i40e_stat_str(&pf->hw, ret),
15002 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15003 		return ret;
15004 	}
15005 	i40e_pf_reset_stats(pf);
15006 
15007 	/* set the switch config bit for the whole device to
15008 	 * support limited promisc or true promisc
15009 	 * when user requests promisc. The default is limited
15010 	 * promisc.
15011 	*/
15012 
15013 	if ((pf->hw.pf_id == 0) &&
15014 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15015 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15016 		pf->last_sw_conf_flags = flags;
15017 	}
15018 
15019 	if (pf->hw.pf_id == 0) {
15020 		u16 valid_flags;
15021 
15022 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15023 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15024 						NULL);
15025 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15026 			dev_info(&pf->pdev->dev,
15027 				 "couldn't set switch config bits, err %s aq_err %s\n",
15028 				 i40e_stat_str(&pf->hw, ret),
15029 				 i40e_aq_str(&pf->hw,
15030 					     pf->hw.aq.asq_last_status));
15031 			/* not a fatal problem, just keep going */
15032 		}
15033 		pf->last_sw_conf_valid_flags = valid_flags;
15034 	}
15035 
15036 	/* first time setup */
15037 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15038 		struct i40e_vsi *vsi = NULL;
15039 		u16 uplink_seid;
15040 
15041 		/* Set up the PF VSI associated with the PF's main VSI
15042 		 * that is already in the HW switch
15043 		 */
15044 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15045 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15046 		else
15047 			uplink_seid = pf->mac_seid;
15048 		if (pf->lan_vsi == I40E_NO_VSI)
15049 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15050 		else if (reinit)
15051 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15052 		if (!vsi) {
15053 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15054 			i40e_cloud_filter_exit(pf);
15055 			i40e_fdir_teardown(pf);
15056 			return -EAGAIN;
15057 		}
15058 	} else {
15059 		/* force a reset of TC and queue layout configurations */
15060 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15061 
15062 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15063 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15064 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15065 	}
15066 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15067 
15068 	i40e_fdir_sb_setup(pf);
15069 
15070 	/* Setup static PF queue filter control settings */
15071 	ret = i40e_setup_pf_filter_control(pf);
15072 	if (ret) {
15073 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15074 			 ret);
15075 		/* Failure here should not stop continuing other steps */
15076 	}
15077 
15078 	/* enable RSS in the HW, even for only one queue, as the stack can use
15079 	 * the hash
15080 	 */
15081 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15082 		i40e_pf_config_rss(pf);
15083 
15084 	/* fill in link information and enable LSE reporting */
15085 	i40e_link_event(pf);
15086 
15087 	/* Initialize user-specific link properties */
15088 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15089 				  I40E_AQ_AN_COMPLETED) ? true : false);
15090 
15091 	i40e_ptp_init(pf);
15092 
15093 	if (!lock_acquired)
15094 		rtnl_lock();
15095 
15096 	/* repopulate tunnel port filters */
15097 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15098 
15099 	if (!lock_acquired)
15100 		rtnl_unlock();
15101 
15102 	return ret;
15103 }
15104 
15105 /**
15106  * i40e_determine_queue_usage - Work out queue distribution
15107  * @pf: board private structure
15108  **/
15109 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15110 {
15111 	int queues_left;
15112 	int q_max;
15113 
15114 	pf->num_lan_qps = 0;
15115 
15116 	/* Find the max queues to be put into basic use.  We'll always be
15117 	 * using TC0, whether or not DCB is running, and TC0 will get the
15118 	 * big RSS set.
15119 	 */
15120 	queues_left = pf->hw.func_caps.num_tx_qp;
15121 
15122 	if ((queues_left == 1) ||
15123 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15124 		/* one qp for PF, no queues for anything else */
15125 		queues_left = 0;
15126 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15127 
15128 		/* make sure all the fancies are disabled */
15129 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15130 			       I40E_FLAG_IWARP_ENABLED	|
15131 			       I40E_FLAG_FD_SB_ENABLED	|
15132 			       I40E_FLAG_FD_ATR_ENABLED	|
15133 			       I40E_FLAG_DCB_CAPABLE	|
15134 			       I40E_FLAG_DCB_ENABLED	|
15135 			       I40E_FLAG_SRIOV_ENABLED	|
15136 			       I40E_FLAG_VMDQ_ENABLED);
15137 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15138 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15139 				  I40E_FLAG_FD_SB_ENABLED |
15140 				  I40E_FLAG_FD_ATR_ENABLED |
15141 				  I40E_FLAG_DCB_CAPABLE))) {
15142 		/* one qp for PF */
15143 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15144 		queues_left -= pf->num_lan_qps;
15145 
15146 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15147 			       I40E_FLAG_IWARP_ENABLED	|
15148 			       I40E_FLAG_FD_SB_ENABLED	|
15149 			       I40E_FLAG_FD_ATR_ENABLED	|
15150 			       I40E_FLAG_DCB_ENABLED	|
15151 			       I40E_FLAG_VMDQ_ENABLED);
15152 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15153 	} else {
15154 		/* Not enough queues for all TCs */
15155 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15156 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15157 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15158 					I40E_FLAG_DCB_ENABLED);
15159 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15160 		}
15161 
15162 		/* limit lan qps to the smaller of qps, cpus or msix */
15163 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15164 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15165 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15166 		pf->num_lan_qps = q_max;
15167 
15168 		queues_left -= pf->num_lan_qps;
15169 	}
15170 
15171 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15172 		if (queues_left > 1) {
15173 			queues_left -= 1; /* save 1 queue for FD */
15174 		} else {
15175 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15176 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15177 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15178 		}
15179 	}
15180 
15181 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15182 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15183 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15184 					(queues_left / pf->num_vf_qps));
15185 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15186 	}
15187 
15188 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15189 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15190 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15191 					  (queues_left / pf->num_vmdq_qps));
15192 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15193 	}
15194 
15195 	pf->queues_left = queues_left;
15196 	dev_dbg(&pf->pdev->dev,
15197 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15198 		pf->hw.func_caps.num_tx_qp,
15199 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15200 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15201 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15202 		queues_left);
15203 }
15204 
15205 /**
15206  * i40e_setup_pf_filter_control - Setup PF static filter control
15207  * @pf: PF to be setup
15208  *
15209  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15210  * settings. If PE/FCoE are enabled then it will also set the per PF
15211  * based filter sizes required for them. It also enables Flow director,
15212  * ethertype and macvlan type filter settings for the pf.
15213  *
15214  * Returns 0 on success, negative on failure
15215  **/
15216 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15217 {
15218 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15219 
15220 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15221 
15222 	/* Flow Director is enabled */
15223 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15224 		settings->enable_fdir = true;
15225 
15226 	/* Ethtype and MACVLAN filters enabled for PF */
15227 	settings->enable_ethtype = true;
15228 	settings->enable_macvlan = true;
15229 
15230 	if (i40e_set_filter_control(&pf->hw, settings))
15231 		return -ENOENT;
15232 
15233 	return 0;
15234 }
15235 
15236 #define INFO_STRING_LEN 255
15237 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15238 static void i40e_print_features(struct i40e_pf *pf)
15239 {
15240 	struct i40e_hw *hw = &pf->hw;
15241 	char *buf;
15242 	int i;
15243 
15244 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15245 	if (!buf)
15246 		return;
15247 
15248 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15249 #ifdef CONFIG_PCI_IOV
15250 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15251 #endif
15252 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15253 		      pf->hw.func_caps.num_vsis,
15254 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15255 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15256 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15257 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15258 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15259 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15260 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15261 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15262 	}
15263 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15264 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15265 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15266 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15267 	if (pf->flags & I40E_FLAG_PTP)
15268 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15269 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15270 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15271 	else
15272 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15273 
15274 	dev_info(&pf->pdev->dev, "%s\n", buf);
15275 	kfree(buf);
15276 	WARN_ON(i > INFO_STRING_LEN);
15277 }
15278 
15279 /**
15280  * i40e_get_platform_mac_addr - get platform-specific MAC address
15281  * @pdev: PCI device information struct
15282  * @pf: board private structure
15283  *
15284  * Look up the MAC address for the device. First we'll try
15285  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15286  * specific fallback. Otherwise, we'll default to the stored value in
15287  * firmware.
15288  **/
15289 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15290 {
15291 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15292 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15293 }
15294 
15295 /**
15296  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15297  * @fec_cfg: FEC option to set in flags
15298  * @flags: ptr to flags in which we set FEC option
15299  **/
15300 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15301 {
15302 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15303 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15304 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15305 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15306 		*flags |= I40E_FLAG_RS_FEC;
15307 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15308 	}
15309 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15310 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15311 		*flags |= I40E_FLAG_BASE_R_FEC;
15312 		*flags &= ~I40E_FLAG_RS_FEC;
15313 	}
15314 	if (fec_cfg == 0)
15315 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15316 }
15317 
15318 /**
15319  * i40e_check_recovery_mode - check if we are running transition firmware
15320  * @pf: board private structure
15321  *
15322  * Check registers indicating the firmware runs in recovery mode. Sets the
15323  * appropriate driver state.
15324  *
15325  * Returns true if the recovery mode was detected, false otherwise
15326  **/
15327 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15328 {
15329 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15330 
15331 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15332 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15333 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15334 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15335 
15336 		return true;
15337 	}
15338 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15339 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15340 
15341 	return false;
15342 }
15343 
15344 /**
15345  * i40e_pf_loop_reset - perform reset in a loop.
15346  * @pf: board private structure
15347  *
15348  * This function is useful when a NIC is about to enter recovery mode.
15349  * When a NIC's internal data structures are corrupted the NIC's
15350  * firmware is going to enter recovery mode.
15351  * Right after a POR it takes about 7 minutes for firmware to enter
15352  * recovery mode. Until that time a NIC is in some kind of intermediate
15353  * state. After that time period the NIC almost surely enters
15354  * recovery mode. The only way for a driver to detect intermediate
15355  * state is to issue a series of pf-resets and check a return value.
15356  * If a PF reset returns success then the firmware could be in recovery
15357  * mode so the caller of this code needs to check for recovery mode
15358  * if this function returns success. There is a little chance that
15359  * firmware will hang in intermediate state forever.
15360  * Since waiting 7 minutes is quite a lot of time this function waits
15361  * 10 seconds and then gives up by returning an error.
15362  *
15363  * Return 0 on success, negative on failure.
15364  **/
15365 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15366 {
15367 	/* wait max 10 seconds for PF reset to succeed */
15368 	const unsigned long time_end = jiffies + 10 * HZ;
15369 
15370 	struct i40e_hw *hw = &pf->hw;
15371 	i40e_status ret;
15372 
15373 	ret = i40e_pf_reset(hw);
15374 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15375 		usleep_range(10000, 20000);
15376 		ret = i40e_pf_reset(hw);
15377 	}
15378 
15379 	if (ret == I40E_SUCCESS)
15380 		pf->pfr_count++;
15381 	else
15382 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15383 
15384 	return ret;
15385 }
15386 
15387 /**
15388  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15389  * @pf: board private structure
15390  *
15391  * Check FW registers to determine if FW issued unexpected EMP Reset.
15392  * Every time when unexpected EMP Reset occurs the FW increments
15393  * a counter of unexpected EMP Resets. When the counter reaches 10
15394  * the FW should enter the Recovery mode
15395  *
15396  * Returns true if FW issued unexpected EMP Reset
15397  **/
15398 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15399 {
15400 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15401 			   I40E_GL_FWSTS_FWS1B_MASK;
15402 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15403 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15404 }
15405 
15406 /**
15407  * i40e_handle_resets - handle EMP resets and PF resets
15408  * @pf: board private structure
15409  *
15410  * Handle both EMP resets and PF resets and conclude whether there are
15411  * any issues regarding these resets. If there are any issues then
15412  * generate log entry.
15413  *
15414  * Return 0 if NIC is healthy or negative value when there are issues
15415  * with resets
15416  **/
15417 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15418 {
15419 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15420 	const bool is_empr = i40e_check_fw_empr(pf);
15421 
15422 	if (is_empr || pfr != I40E_SUCCESS)
15423 		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");
15424 
15425 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15426 }
15427 
15428 /**
15429  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15430  * @pf: board private structure
15431  * @hw: ptr to the hardware info
15432  *
15433  * This function does a minimal setup of all subsystems needed for running
15434  * recovery mode.
15435  *
15436  * Returns 0 on success, negative on failure
15437  **/
15438 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15439 {
15440 	struct i40e_vsi *vsi;
15441 	int err;
15442 	int v_idx;
15443 
15444 	pci_save_state(pf->pdev);
15445 
15446 	/* set up periodic task facility */
15447 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15448 	pf->service_timer_period = HZ;
15449 
15450 	INIT_WORK(&pf->service_task, i40e_service_task);
15451 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15452 
15453 	err = i40e_init_interrupt_scheme(pf);
15454 	if (err)
15455 		goto err_switch_setup;
15456 
15457 	/* The number of VSIs reported by the FW is the minimum guaranteed
15458 	 * to us; HW supports far more and we share the remaining pool with
15459 	 * the other PFs. We allocate space for more than the guarantee with
15460 	 * the understanding that we might not get them all later.
15461 	 */
15462 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15463 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15464 	else
15465 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15466 
15467 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15468 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15469 			  GFP_KERNEL);
15470 	if (!pf->vsi) {
15471 		err = -ENOMEM;
15472 		goto err_switch_setup;
15473 	}
15474 
15475 	/* We allocate one VSI which is needed as absolute minimum
15476 	 * in order to register the netdev
15477 	 */
15478 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15479 	if (v_idx < 0) {
15480 		err = v_idx;
15481 		goto err_switch_setup;
15482 	}
15483 	pf->lan_vsi = v_idx;
15484 	vsi = pf->vsi[v_idx];
15485 	if (!vsi) {
15486 		err = -EFAULT;
15487 		goto err_switch_setup;
15488 	}
15489 	vsi->alloc_queue_pairs = 1;
15490 	err = i40e_config_netdev(vsi);
15491 	if (err)
15492 		goto err_switch_setup;
15493 	err = register_netdev(vsi->netdev);
15494 	if (err)
15495 		goto err_switch_setup;
15496 	vsi->netdev_registered = true;
15497 	i40e_dbg_pf_init(pf);
15498 
15499 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15500 	if (err)
15501 		goto err_switch_setup;
15502 
15503 	/* tell the firmware that we're starting */
15504 	i40e_send_version(pf);
15505 
15506 	/* since everything's happy, start the service_task timer */
15507 	mod_timer(&pf->service_timer,
15508 		  round_jiffies(jiffies + pf->service_timer_period));
15509 
15510 	return 0;
15511 
15512 err_switch_setup:
15513 	i40e_reset_interrupt_capability(pf);
15514 	del_timer_sync(&pf->service_timer);
15515 	i40e_shutdown_adminq(hw);
15516 	iounmap(hw->hw_addr);
15517 	pci_disable_pcie_error_reporting(pf->pdev);
15518 	pci_release_mem_regions(pf->pdev);
15519 	pci_disable_device(pf->pdev);
15520 	kfree(pf);
15521 
15522 	return err;
15523 }
15524 
15525 /**
15526  * i40e_set_subsystem_device_id - set subsystem device id
15527  * @hw: pointer to the hardware info
15528  *
15529  * Set PCI subsystem device id either from a pci_dev structure or
15530  * a specific FW register.
15531  **/
15532 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15533 {
15534 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15535 
15536 	hw->subsystem_device_id = pdev->subsystem_device ?
15537 		pdev->subsystem_device :
15538 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15539 }
15540 
15541 /**
15542  * i40e_probe - Device initialization routine
15543  * @pdev: PCI device information struct
15544  * @ent: entry in i40e_pci_tbl
15545  *
15546  * i40e_probe initializes a PF identified by a pci_dev structure.
15547  * The OS initialization, configuring of the PF private structure,
15548  * and a hardware reset occur.
15549  *
15550  * Returns 0 on success, negative on failure
15551  **/
15552 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15553 {
15554 	struct i40e_aq_get_phy_abilities_resp abilities;
15555 #ifdef CONFIG_I40E_DCB
15556 	enum i40e_get_fw_lldp_status_resp lldp_status;
15557 	i40e_status status;
15558 #endif /* CONFIG_I40E_DCB */
15559 	struct i40e_pf *pf;
15560 	struct i40e_hw *hw;
15561 	static u16 pfs_found;
15562 	u16 wol_nvm_bits;
15563 	u16 link_status;
15564 	int err;
15565 	u32 val;
15566 	u32 i;
15567 
15568 	err = pci_enable_device_mem(pdev);
15569 	if (err)
15570 		return err;
15571 
15572 	/* set up for high or low dma */
15573 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15574 	if (err) {
15575 		dev_err(&pdev->dev,
15576 			"DMA configuration failed: 0x%x\n", err);
15577 		goto err_dma;
15578 	}
15579 
15580 	/* set up pci connections */
15581 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15582 	if (err) {
15583 		dev_info(&pdev->dev,
15584 			 "pci_request_selected_regions failed %d\n", err);
15585 		goto err_pci_reg;
15586 	}
15587 
15588 	pci_enable_pcie_error_reporting(pdev);
15589 	pci_set_master(pdev);
15590 
15591 	/* Now that we have a PCI connection, we need to do the
15592 	 * low level device setup.  This is primarily setting up
15593 	 * the Admin Queue structures and then querying for the
15594 	 * device's current profile information.
15595 	 */
15596 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15597 	if (!pf) {
15598 		err = -ENOMEM;
15599 		goto err_pf_alloc;
15600 	}
15601 	pf->next_vsi = 0;
15602 	pf->pdev = pdev;
15603 	set_bit(__I40E_DOWN, pf->state);
15604 
15605 	hw = &pf->hw;
15606 	hw->back = pf;
15607 
15608 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15609 				I40E_MAX_CSR_SPACE);
15610 	/* We believe that the highest register to read is
15611 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15612 	 * is not less than that before mapping to prevent a
15613 	 * kernel panic.
15614 	 */
15615 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15616 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15617 			pf->ioremap_len);
15618 		err = -ENOMEM;
15619 		goto err_ioremap;
15620 	}
15621 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15622 	if (!hw->hw_addr) {
15623 		err = -EIO;
15624 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15625 			 (unsigned int)pci_resource_start(pdev, 0),
15626 			 pf->ioremap_len, err);
15627 		goto err_ioremap;
15628 	}
15629 	hw->vendor_id = pdev->vendor;
15630 	hw->device_id = pdev->device;
15631 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15632 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15633 	i40e_set_subsystem_device_id(hw);
15634 	hw->bus.device = PCI_SLOT(pdev->devfn);
15635 	hw->bus.func = PCI_FUNC(pdev->devfn);
15636 	hw->bus.bus_id = pdev->bus->number;
15637 	pf->instance = pfs_found;
15638 
15639 	/* Select something other than the 802.1ad ethertype for the
15640 	 * switch to use internally and drop on ingress.
15641 	 */
15642 	hw->switch_tag = 0xffff;
15643 	hw->first_tag = ETH_P_8021AD;
15644 	hw->second_tag = ETH_P_8021Q;
15645 
15646 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15647 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15648 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15649 
15650 	/* set up the locks for the AQ, do this only once in probe
15651 	 * and destroy them only once in remove
15652 	 */
15653 	mutex_init(&hw->aq.asq_mutex);
15654 	mutex_init(&hw->aq.arq_mutex);
15655 
15656 	pf->msg_enable = netif_msg_init(debug,
15657 					NETIF_MSG_DRV |
15658 					NETIF_MSG_PROBE |
15659 					NETIF_MSG_LINK);
15660 	if (debug < -1)
15661 		pf->hw.debug_mask = debug;
15662 
15663 	/* do a special CORER for clearing PXE mode once at init */
15664 	if (hw->revision_id == 0 &&
15665 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15666 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15667 		i40e_flush(hw);
15668 		msleep(200);
15669 		pf->corer_count++;
15670 
15671 		i40e_clear_pxe_mode(hw);
15672 	}
15673 
15674 	/* Reset here to make sure all is clean and to define PF 'n' */
15675 	i40e_clear_hw(hw);
15676 
15677 	err = i40e_set_mac_type(hw);
15678 	if (err) {
15679 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15680 			 err);
15681 		goto err_pf_reset;
15682 	}
15683 
15684 	err = i40e_handle_resets(pf);
15685 	if (err)
15686 		goto err_pf_reset;
15687 
15688 	i40e_check_recovery_mode(pf);
15689 
15690 	if (is_kdump_kernel()) {
15691 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15692 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15693 	} else {
15694 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15695 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15696 	}
15697 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15698 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15699 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15700 
15701 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15702 		 "%s-%s:misc",
15703 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15704 
15705 	err = i40e_init_shared_code(hw);
15706 	if (err) {
15707 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15708 			 err);
15709 		goto err_pf_reset;
15710 	}
15711 
15712 	/* set up a default setting for link flow control */
15713 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15714 
15715 	err = i40e_init_adminq(hw);
15716 	if (err) {
15717 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15718 			dev_info(&pdev->dev,
15719 				 "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",
15720 				 hw->aq.api_maj_ver,
15721 				 hw->aq.api_min_ver,
15722 				 I40E_FW_API_VERSION_MAJOR,
15723 				 I40E_FW_MINOR_VERSION(hw));
15724 		else
15725 			dev_info(&pdev->dev,
15726 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15727 
15728 		goto err_pf_reset;
15729 	}
15730 	i40e_get_oem_version(hw);
15731 
15732 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15733 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15734 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15735 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15736 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15737 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15738 
15739 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15740 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15741 		dev_dbg(&pdev->dev,
15742 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15743 			 hw->aq.api_maj_ver,
15744 			 hw->aq.api_min_ver,
15745 			 I40E_FW_API_VERSION_MAJOR,
15746 			 I40E_FW_MINOR_VERSION(hw));
15747 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15748 		dev_info(&pdev->dev,
15749 			 "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",
15750 			 hw->aq.api_maj_ver,
15751 			 hw->aq.api_min_ver,
15752 			 I40E_FW_API_VERSION_MAJOR,
15753 			 I40E_FW_MINOR_VERSION(hw));
15754 
15755 	i40e_verify_eeprom(pf);
15756 
15757 	/* Rev 0 hardware was never productized */
15758 	if (hw->revision_id < 1)
15759 		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");
15760 
15761 	i40e_clear_pxe_mode(hw);
15762 
15763 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15764 	if (err)
15765 		goto err_adminq_setup;
15766 
15767 	err = i40e_sw_init(pf);
15768 	if (err) {
15769 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15770 		goto err_sw_init;
15771 	}
15772 
15773 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15774 		return i40e_init_recovery_mode(pf, hw);
15775 
15776 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15777 				hw->func_caps.num_rx_qp, 0, 0);
15778 	if (err) {
15779 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15780 		goto err_init_lan_hmc;
15781 	}
15782 
15783 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15784 	if (err) {
15785 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15786 		err = -ENOENT;
15787 		goto err_configure_lan_hmc;
15788 	}
15789 
15790 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15791 	 * Ignore error return codes because if it was already disabled via
15792 	 * hardware settings this will fail
15793 	 */
15794 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15795 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15796 		i40e_aq_stop_lldp(hw, true, false, NULL);
15797 	}
15798 
15799 	/* allow a platform config to override the HW addr */
15800 	i40e_get_platform_mac_addr(pdev, pf);
15801 
15802 	if (!is_valid_ether_addr(hw->mac.addr)) {
15803 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15804 		err = -EIO;
15805 		goto err_mac_addr;
15806 	}
15807 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15808 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15809 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15810 	if (is_valid_ether_addr(hw->mac.port_addr))
15811 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15812 
15813 	i40e_ptp_alloc_pins(pf);
15814 	pci_set_drvdata(pdev, pf);
15815 	pci_save_state(pdev);
15816 
15817 #ifdef CONFIG_I40E_DCB
15818 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15819 	(!status &&
15820 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15821 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15822 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15823 	dev_info(&pdev->dev,
15824 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15825 			"FW LLDP is disabled\n" :
15826 			"FW LLDP is enabled\n");
15827 
15828 	/* Enable FW to write default DCB config on link-up */
15829 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15830 
15831 	err = i40e_init_pf_dcb(pf);
15832 	if (err) {
15833 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15834 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15835 		/* Continue without DCB enabled */
15836 	}
15837 #endif /* CONFIG_I40E_DCB */
15838 
15839 	/* set up periodic task facility */
15840 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15841 	pf->service_timer_period = HZ;
15842 
15843 	INIT_WORK(&pf->service_task, i40e_service_task);
15844 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15845 
15846 	/* NVM bit on means WoL disabled for the port */
15847 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15848 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15849 		pf->wol_en = false;
15850 	else
15851 		pf->wol_en = true;
15852 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15853 
15854 	/* set up the main switch operations */
15855 	i40e_determine_queue_usage(pf);
15856 	err = i40e_init_interrupt_scheme(pf);
15857 	if (err)
15858 		goto err_switch_setup;
15859 
15860 	/* Reduce Tx and Rx pairs for kdump
15861 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15862 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15863 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15864 	 */
15865 	if (is_kdump_kernel())
15866 		pf->num_lan_msix = 1;
15867 
15868 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15869 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15870 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15871 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15872 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15873 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15874 						    UDP_TUNNEL_TYPE_GENEVE;
15875 
15876 	/* The number of VSIs reported by the FW is the minimum guaranteed
15877 	 * to us; HW supports far more and we share the remaining pool with
15878 	 * the other PFs. We allocate space for more than the guarantee with
15879 	 * the understanding that we might not get them all later.
15880 	 */
15881 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15882 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15883 	else
15884 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15885 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15886 		dev_warn(&pf->pdev->dev,
15887 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15888 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15889 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15890 	}
15891 
15892 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15893 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15894 			  GFP_KERNEL);
15895 	if (!pf->vsi) {
15896 		err = -ENOMEM;
15897 		goto err_switch_setup;
15898 	}
15899 
15900 #ifdef CONFIG_PCI_IOV
15901 	/* prep for VF support */
15902 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15903 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15904 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15905 		if (pci_num_vf(pdev))
15906 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15907 	}
15908 #endif
15909 	err = i40e_setup_pf_switch(pf, false, false);
15910 	if (err) {
15911 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15912 		goto err_vsis;
15913 	}
15914 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15915 
15916 	/* if FDIR VSI was set up, start it now */
15917 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15918 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15919 			i40e_vsi_open(pf->vsi[i]);
15920 			break;
15921 		}
15922 	}
15923 
15924 	/* The driver only wants link up/down and module qualification
15925 	 * reports from firmware.  Note the negative logic.
15926 	 */
15927 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15928 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15929 					 I40E_AQ_EVENT_MEDIA_NA |
15930 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15931 	if (err)
15932 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15933 			 i40e_stat_str(&pf->hw, err),
15934 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15935 
15936 	/* Reconfigure hardware for allowing smaller MSS in the case
15937 	 * of TSO, so that we avoid the MDD being fired and causing
15938 	 * a reset in the case of small MSS+TSO.
15939 	 */
15940 	val = rd32(hw, I40E_REG_MSS);
15941 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15942 		val &= ~I40E_REG_MSS_MIN_MASK;
15943 		val |= I40E_64BYTE_MSS;
15944 		wr32(hw, I40E_REG_MSS, val);
15945 	}
15946 
15947 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15948 		msleep(75);
15949 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15950 		if (err)
15951 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15952 				 i40e_stat_str(&pf->hw, err),
15953 				 i40e_aq_str(&pf->hw,
15954 					     pf->hw.aq.asq_last_status));
15955 	}
15956 	/* The main driver is (mostly) up and happy. We need to set this state
15957 	 * before setting up the misc vector or we get a race and the vector
15958 	 * ends up disabled forever.
15959 	 */
15960 	clear_bit(__I40E_DOWN, pf->state);
15961 
15962 	/* In case of MSIX we are going to setup the misc vector right here
15963 	 * to handle admin queue events etc. In case of legacy and MSI
15964 	 * the misc functionality and queue processing is combined in
15965 	 * the same vector and that gets setup at open.
15966 	 */
15967 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15968 		err = i40e_setup_misc_vector(pf);
15969 		if (err) {
15970 			dev_info(&pdev->dev,
15971 				 "setup of misc vector failed: %d\n", err);
15972 			i40e_cloud_filter_exit(pf);
15973 			i40e_fdir_teardown(pf);
15974 			goto err_vsis;
15975 		}
15976 	}
15977 
15978 #ifdef CONFIG_PCI_IOV
15979 	/* prep for VF support */
15980 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15981 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15982 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15983 		/* disable link interrupts for VFs */
15984 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15985 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15986 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15987 		i40e_flush(hw);
15988 
15989 		if (pci_num_vf(pdev)) {
15990 			dev_info(&pdev->dev,
15991 				 "Active VFs found, allocating resources.\n");
15992 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15993 			if (err)
15994 				dev_info(&pdev->dev,
15995 					 "Error %d allocating resources for existing VFs\n",
15996 					 err);
15997 		}
15998 	}
15999 #endif /* CONFIG_PCI_IOV */
16000 
16001 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16002 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16003 						      pf->num_iwarp_msix,
16004 						      I40E_IWARP_IRQ_PILE_ID);
16005 		if (pf->iwarp_base_vector < 0) {
16006 			dev_info(&pdev->dev,
16007 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16008 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16009 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16010 		}
16011 	}
16012 
16013 	i40e_dbg_pf_init(pf);
16014 
16015 	/* tell the firmware that we're starting */
16016 	i40e_send_version(pf);
16017 
16018 	/* since everything's happy, start the service_task timer */
16019 	mod_timer(&pf->service_timer,
16020 		  round_jiffies(jiffies + pf->service_timer_period));
16021 
16022 	/* add this PF to client device list and launch a client service task */
16023 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16024 		err = i40e_lan_add_device(pf);
16025 		if (err)
16026 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16027 				 err);
16028 	}
16029 
16030 #define PCI_SPEED_SIZE 8
16031 #define PCI_WIDTH_SIZE 8
16032 	/* Devices on the IOSF bus do not have this information
16033 	 * and will report PCI Gen 1 x 1 by default so don't bother
16034 	 * checking them.
16035 	 */
16036 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16037 		char speed[PCI_SPEED_SIZE] = "Unknown";
16038 		char width[PCI_WIDTH_SIZE] = "Unknown";
16039 
16040 		/* Get the negotiated link width and speed from PCI config
16041 		 * space
16042 		 */
16043 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16044 					  &link_status);
16045 
16046 		i40e_set_pci_config_data(hw, link_status);
16047 
16048 		switch (hw->bus.speed) {
16049 		case i40e_bus_speed_8000:
16050 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
16051 		case i40e_bus_speed_5000:
16052 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
16053 		case i40e_bus_speed_2500:
16054 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
16055 		default:
16056 			break;
16057 		}
16058 		switch (hw->bus.width) {
16059 		case i40e_bus_width_pcie_x8:
16060 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
16061 		case i40e_bus_width_pcie_x4:
16062 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
16063 		case i40e_bus_width_pcie_x2:
16064 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
16065 		case i40e_bus_width_pcie_x1:
16066 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
16067 		default:
16068 			break;
16069 		}
16070 
16071 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16072 			 speed, width);
16073 
16074 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16075 		    hw->bus.speed < i40e_bus_speed_8000) {
16076 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16077 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16078 		}
16079 	}
16080 
16081 	/* get the requested speeds from the fw */
16082 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16083 	if (err)
16084 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
16085 			i40e_stat_str(&pf->hw, err),
16086 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16087 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16088 
16089 	/* set the FEC config due to the board capabilities */
16090 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16091 
16092 	/* get the supported phy types from the fw */
16093 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16094 	if (err)
16095 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
16096 			i40e_stat_str(&pf->hw, err),
16097 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16098 
16099 	/* make sure the MFS hasn't been set lower than the default */
16100 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16101 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16102 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16103 	if (val < MAX_FRAME_SIZE_DEFAULT)
16104 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16105 			 i, val);
16106 
16107 	/* Add a filter to drop all Flow control frames from any VSI from being
16108 	 * transmitted. By doing so we stop a malicious VF from sending out
16109 	 * PAUSE or PFC frames and potentially controlling traffic for other
16110 	 * PF/VF VSIs.
16111 	 * The FW can still send Flow control frames if enabled.
16112 	 */
16113 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16114 						       pf->main_vsi_seid);
16115 
16116 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16117 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16118 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16119 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16120 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16121 	/* print a string summarizing features */
16122 	i40e_print_features(pf);
16123 
16124 	return 0;
16125 
16126 	/* Unwind what we've done if something failed in the setup */
16127 err_vsis:
16128 	set_bit(__I40E_DOWN, pf->state);
16129 	i40e_clear_interrupt_scheme(pf);
16130 	kfree(pf->vsi);
16131 err_switch_setup:
16132 	i40e_reset_interrupt_capability(pf);
16133 	del_timer_sync(&pf->service_timer);
16134 err_mac_addr:
16135 err_configure_lan_hmc:
16136 	(void)i40e_shutdown_lan_hmc(hw);
16137 err_init_lan_hmc:
16138 	kfree(pf->qp_pile);
16139 err_sw_init:
16140 err_adminq_setup:
16141 err_pf_reset:
16142 	iounmap(hw->hw_addr);
16143 err_ioremap:
16144 	kfree(pf);
16145 err_pf_alloc:
16146 	pci_disable_pcie_error_reporting(pdev);
16147 	pci_release_mem_regions(pdev);
16148 err_pci_reg:
16149 err_dma:
16150 	pci_disable_device(pdev);
16151 	return err;
16152 }
16153 
16154 /**
16155  * i40e_remove - Device removal routine
16156  * @pdev: PCI device information struct
16157  *
16158  * i40e_remove is called by the PCI subsystem to alert the driver
16159  * that is should release a PCI device.  This could be caused by a
16160  * Hot-Plug event, or because the driver is going to be removed from
16161  * memory.
16162  **/
16163 static void i40e_remove(struct pci_dev *pdev)
16164 {
16165 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16166 	struct i40e_hw *hw = &pf->hw;
16167 	i40e_status ret_code;
16168 	int i;
16169 
16170 	i40e_dbg_pf_exit(pf);
16171 
16172 	i40e_ptp_stop(pf);
16173 
16174 	/* Disable RSS in hw */
16175 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16176 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16177 
16178 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16179 	 * flags, once they are set, i40e_rebuild should not be called as
16180 	 * i40e_prep_for_reset always returns early.
16181 	 */
16182 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16183 		usleep_range(1000, 2000);
16184 	set_bit(__I40E_IN_REMOVE, pf->state);
16185 
16186 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16187 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16188 		i40e_free_vfs(pf);
16189 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16190 	}
16191 	/* no more scheduling of any task */
16192 	set_bit(__I40E_SUSPENDED, pf->state);
16193 	set_bit(__I40E_DOWN, pf->state);
16194 	if (pf->service_timer.function)
16195 		del_timer_sync(&pf->service_timer);
16196 	if (pf->service_task.func)
16197 		cancel_work_sync(&pf->service_task);
16198 
16199 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16200 		struct i40e_vsi *vsi = pf->vsi[0];
16201 
16202 		/* We know that we have allocated only one vsi for this PF,
16203 		 * it was just for registering netdevice, so the interface
16204 		 * could be visible in the 'ifconfig' output
16205 		 */
16206 		unregister_netdev(vsi->netdev);
16207 		free_netdev(vsi->netdev);
16208 
16209 		goto unmap;
16210 	}
16211 
16212 	/* Client close must be called explicitly here because the timer
16213 	 * has been stopped.
16214 	 */
16215 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16216 
16217 	i40e_fdir_teardown(pf);
16218 
16219 	/* If there is a switch structure or any orphans, remove them.
16220 	 * This will leave only the PF's VSI remaining.
16221 	 */
16222 	for (i = 0; i < I40E_MAX_VEB; i++) {
16223 		if (!pf->veb[i])
16224 			continue;
16225 
16226 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16227 		    pf->veb[i]->uplink_seid == 0)
16228 			i40e_switch_branch_release(pf->veb[i]);
16229 	}
16230 
16231 	/* Now we can shutdown the PF's VSI, just before we kill
16232 	 * adminq and hmc.
16233 	 */
16234 	if (pf->vsi[pf->lan_vsi])
16235 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16236 
16237 	i40e_cloud_filter_exit(pf);
16238 
16239 	/* remove attached clients */
16240 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16241 		ret_code = i40e_lan_del_device(pf);
16242 		if (ret_code)
16243 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16244 				 ret_code);
16245 	}
16246 
16247 	/* shutdown and destroy the HMC */
16248 	if (hw->hmc.hmc_obj) {
16249 		ret_code = i40e_shutdown_lan_hmc(hw);
16250 		if (ret_code)
16251 			dev_warn(&pdev->dev,
16252 				 "Failed to destroy the HMC resources: %d\n",
16253 				 ret_code);
16254 	}
16255 
16256 unmap:
16257 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16258 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16259 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16260 		free_irq(pf->pdev->irq, pf);
16261 
16262 	/* shutdown the adminq */
16263 	i40e_shutdown_adminq(hw);
16264 
16265 	/* destroy the locks only once, here */
16266 	mutex_destroy(&hw->aq.arq_mutex);
16267 	mutex_destroy(&hw->aq.asq_mutex);
16268 
16269 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16270 	rtnl_lock();
16271 	i40e_clear_interrupt_scheme(pf);
16272 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16273 		if (pf->vsi[i]) {
16274 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16275 				i40e_vsi_clear_rings(pf->vsi[i]);
16276 			i40e_vsi_clear(pf->vsi[i]);
16277 			pf->vsi[i] = NULL;
16278 		}
16279 	}
16280 	rtnl_unlock();
16281 
16282 	for (i = 0; i < I40E_MAX_VEB; i++) {
16283 		kfree(pf->veb[i]);
16284 		pf->veb[i] = NULL;
16285 	}
16286 
16287 	kfree(pf->qp_pile);
16288 	kfree(pf->vsi);
16289 
16290 	iounmap(hw->hw_addr);
16291 	kfree(pf);
16292 	pci_release_mem_regions(pdev);
16293 
16294 	pci_disable_pcie_error_reporting(pdev);
16295 	pci_disable_device(pdev);
16296 }
16297 
16298 /**
16299  * i40e_pci_error_detected - warning that something funky happened in PCI land
16300  * @pdev: PCI device information struct
16301  * @error: the type of PCI error
16302  *
16303  * Called to warn that something happened and the error handling steps
16304  * are in progress.  Allows the driver to quiesce things, be ready for
16305  * remediation.
16306  **/
16307 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16308 						pci_channel_state_t error)
16309 {
16310 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16311 
16312 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16313 
16314 	if (!pf) {
16315 		dev_info(&pdev->dev,
16316 			 "Cannot recover - error happened during device probe\n");
16317 		return PCI_ERS_RESULT_DISCONNECT;
16318 	}
16319 
16320 	/* shutdown all operations */
16321 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16322 		i40e_prep_for_reset(pf);
16323 
16324 	/* Request a slot reset */
16325 	return PCI_ERS_RESULT_NEED_RESET;
16326 }
16327 
16328 /**
16329  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16330  * @pdev: PCI device information struct
16331  *
16332  * Called to find if the driver can work with the device now that
16333  * the pci slot has been reset.  If a basic connection seems good
16334  * (registers are readable and have sane content) then return a
16335  * happy little PCI_ERS_RESULT_xxx.
16336  **/
16337 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16338 {
16339 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16340 	pci_ers_result_t result;
16341 	u32 reg;
16342 
16343 	dev_dbg(&pdev->dev, "%s\n", __func__);
16344 	if (pci_enable_device_mem(pdev)) {
16345 		dev_info(&pdev->dev,
16346 			 "Cannot re-enable PCI device after reset.\n");
16347 		result = PCI_ERS_RESULT_DISCONNECT;
16348 	} else {
16349 		pci_set_master(pdev);
16350 		pci_restore_state(pdev);
16351 		pci_save_state(pdev);
16352 		pci_wake_from_d3(pdev, false);
16353 
16354 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16355 		if (reg == 0)
16356 			result = PCI_ERS_RESULT_RECOVERED;
16357 		else
16358 			result = PCI_ERS_RESULT_DISCONNECT;
16359 	}
16360 
16361 	return result;
16362 }
16363 
16364 /**
16365  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16366  * @pdev: PCI device information struct
16367  */
16368 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16369 {
16370 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16371 
16372 	i40e_prep_for_reset(pf);
16373 }
16374 
16375 /**
16376  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16377  * @pdev: PCI device information struct
16378  */
16379 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16380 {
16381 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16382 
16383 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16384 		return;
16385 
16386 	i40e_reset_and_rebuild(pf, false, false);
16387 }
16388 
16389 /**
16390  * i40e_pci_error_resume - restart operations after PCI error recovery
16391  * @pdev: PCI device information struct
16392  *
16393  * Called to allow the driver to bring things back up after PCI error
16394  * and/or reset recovery has finished.
16395  **/
16396 static void i40e_pci_error_resume(struct pci_dev *pdev)
16397 {
16398 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16399 
16400 	dev_dbg(&pdev->dev, "%s\n", __func__);
16401 	if (test_bit(__I40E_SUSPENDED, pf->state))
16402 		return;
16403 
16404 	i40e_handle_reset_warning(pf, false);
16405 }
16406 
16407 /**
16408  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16409  * using the mac_address_write admin q function
16410  * @pf: pointer to i40e_pf struct
16411  **/
16412 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16413 {
16414 	struct i40e_hw *hw = &pf->hw;
16415 	i40e_status ret;
16416 	u8 mac_addr[6];
16417 	u16 flags = 0;
16418 
16419 	/* Get current MAC address in case it's an LAA */
16420 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16421 		ether_addr_copy(mac_addr,
16422 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16423 	} else {
16424 		dev_err(&pf->pdev->dev,
16425 			"Failed to retrieve MAC address; using default\n");
16426 		ether_addr_copy(mac_addr, hw->mac.addr);
16427 	}
16428 
16429 	/* The FW expects the mac address write cmd to first be called with
16430 	 * one of these flags before calling it again with the multicast
16431 	 * enable flags.
16432 	 */
16433 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16434 
16435 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16436 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16437 
16438 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16439 	if (ret) {
16440 		dev_err(&pf->pdev->dev,
16441 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16442 		return;
16443 	}
16444 
16445 	flags = I40E_AQC_MC_MAG_EN
16446 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16447 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16448 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16449 	if (ret)
16450 		dev_err(&pf->pdev->dev,
16451 			"Failed to enable Multicast Magic Packet wake up\n");
16452 }
16453 
16454 /**
16455  * i40e_shutdown - PCI callback for shutting down
16456  * @pdev: PCI device information struct
16457  **/
16458 static void i40e_shutdown(struct pci_dev *pdev)
16459 {
16460 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16461 	struct i40e_hw *hw = &pf->hw;
16462 
16463 	set_bit(__I40E_SUSPENDED, pf->state);
16464 	set_bit(__I40E_DOWN, pf->state);
16465 
16466 	del_timer_sync(&pf->service_timer);
16467 	cancel_work_sync(&pf->service_task);
16468 	i40e_cloud_filter_exit(pf);
16469 	i40e_fdir_teardown(pf);
16470 
16471 	/* Client close must be called explicitly here because the timer
16472 	 * has been stopped.
16473 	 */
16474 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16475 
16476 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16477 		i40e_enable_mc_magic_wake(pf);
16478 
16479 	i40e_prep_for_reset(pf);
16480 
16481 	wr32(hw, I40E_PFPM_APM,
16482 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16483 	wr32(hw, I40E_PFPM_WUFC,
16484 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16485 
16486 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16487 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16488 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16489 		free_irq(pf->pdev->irq, pf);
16490 
16491 	/* Since we're going to destroy queues during the
16492 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16493 	 * whole section
16494 	 */
16495 	rtnl_lock();
16496 	i40e_clear_interrupt_scheme(pf);
16497 	rtnl_unlock();
16498 
16499 	if (system_state == SYSTEM_POWER_OFF) {
16500 		pci_wake_from_d3(pdev, pf->wol_en);
16501 		pci_set_power_state(pdev, PCI_D3hot);
16502 	}
16503 }
16504 
16505 /**
16506  * i40e_suspend - PM callback for moving to D3
16507  * @dev: generic device information structure
16508  **/
16509 static int __maybe_unused i40e_suspend(struct device *dev)
16510 {
16511 	struct i40e_pf *pf = dev_get_drvdata(dev);
16512 	struct i40e_hw *hw = &pf->hw;
16513 
16514 	/* If we're already suspended, then there is nothing to do */
16515 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16516 		return 0;
16517 
16518 	set_bit(__I40E_DOWN, pf->state);
16519 
16520 	/* Ensure service task will not be running */
16521 	del_timer_sync(&pf->service_timer);
16522 	cancel_work_sync(&pf->service_task);
16523 
16524 	/* Client close must be called explicitly here because the timer
16525 	 * has been stopped.
16526 	 */
16527 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16528 
16529 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16530 		i40e_enable_mc_magic_wake(pf);
16531 
16532 	/* Since we're going to destroy queues during the
16533 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16534 	 * whole section
16535 	 */
16536 	rtnl_lock();
16537 
16538 	i40e_prep_for_reset(pf);
16539 
16540 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16541 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16542 
16543 	/* Clear the interrupt scheme and release our IRQs so that the system
16544 	 * can safely hibernate even when there are a large number of CPUs.
16545 	 * Otherwise hibernation might fail when mapping all the vectors back
16546 	 * to CPU0.
16547 	 */
16548 	i40e_clear_interrupt_scheme(pf);
16549 
16550 	rtnl_unlock();
16551 
16552 	return 0;
16553 }
16554 
16555 /**
16556  * i40e_resume - PM callback for waking up from D3
16557  * @dev: generic device information structure
16558  **/
16559 static int __maybe_unused i40e_resume(struct device *dev)
16560 {
16561 	struct i40e_pf *pf = dev_get_drvdata(dev);
16562 	int err;
16563 
16564 	/* If we're not suspended, then there is nothing to do */
16565 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16566 		return 0;
16567 
16568 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16569 	 * since we're going to be restoring queues
16570 	 */
16571 	rtnl_lock();
16572 
16573 	/* We cleared the interrupt scheme when we suspended, so we need to
16574 	 * restore it now to resume device functionality.
16575 	 */
16576 	err = i40e_restore_interrupt_scheme(pf);
16577 	if (err) {
16578 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16579 			err);
16580 	}
16581 
16582 	clear_bit(__I40E_DOWN, pf->state);
16583 	i40e_reset_and_rebuild(pf, false, true);
16584 
16585 	rtnl_unlock();
16586 
16587 	/* Clear suspended state last after everything is recovered */
16588 	clear_bit(__I40E_SUSPENDED, pf->state);
16589 
16590 	/* Restart the service task */
16591 	mod_timer(&pf->service_timer,
16592 		  round_jiffies(jiffies + pf->service_timer_period));
16593 
16594 	return 0;
16595 }
16596 
16597 static const struct pci_error_handlers i40e_err_handler = {
16598 	.error_detected = i40e_pci_error_detected,
16599 	.slot_reset = i40e_pci_error_slot_reset,
16600 	.reset_prepare = i40e_pci_error_reset_prepare,
16601 	.reset_done = i40e_pci_error_reset_done,
16602 	.resume = i40e_pci_error_resume,
16603 };
16604 
16605 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16606 
16607 static struct pci_driver i40e_driver = {
16608 	.name     = i40e_driver_name,
16609 	.id_table = i40e_pci_tbl,
16610 	.probe    = i40e_probe,
16611 	.remove   = i40e_remove,
16612 	.driver   = {
16613 		.pm = &i40e_pm_ops,
16614 	},
16615 	.shutdown = i40e_shutdown,
16616 	.err_handler = &i40e_err_handler,
16617 	.sriov_configure = i40e_pci_sriov_configure,
16618 };
16619 
16620 /**
16621  * i40e_init_module - Driver registration routine
16622  *
16623  * i40e_init_module is the first routine called when the driver is
16624  * loaded. All it does is register with the PCI subsystem.
16625  **/
16626 static int __init i40e_init_module(void)
16627 {
16628 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16629 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16630 
16631 	/* There is no need to throttle the number of active tasks because
16632 	 * each device limits its own task using a state bit for scheduling
16633 	 * the service task, and the device tasks do not interfere with each
16634 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16635 	 * since we need to be able to guarantee forward progress even under
16636 	 * memory pressure.
16637 	 */
16638 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16639 	if (!i40e_wq) {
16640 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16641 		return -ENOMEM;
16642 	}
16643 
16644 	i40e_dbg_init();
16645 	return pci_register_driver(&i40e_driver);
16646 }
16647 module_init(i40e_init_module);
16648 
16649 /**
16650  * i40e_exit_module - Driver exit cleanup routine
16651  *
16652  * i40e_exit_module is called just before the driver is removed
16653  * from memory.
16654  **/
16655 static void __exit i40e_exit_module(void)
16656 {
16657 	pci_unregister_driver(&i40e_driver);
16658 	destroy_workqueue(i40e_wq);
16659 	ida_destroy(&i40e_client_ida);
16660 	i40e_dbg_exit();
16661 }
16662 module_exit(i40e_exit_module);
16663