1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2017 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26 
27 #include <linux/etherdevice.h>
28 #include <linux/of_net.h>
29 #include <linux/pci.h>
30 #include <linux/bpf.h>
31 
32 /* Local includes */
33 #include "i40e.h"
34 #include "i40e_diag.h"
35 #include <net/udp_tunnel.h>
36 /* All i40e tracepoints are defined by the include below, which
37  * must be included exactly once across the whole kernel with
38  * CREATE_TRACE_POINTS defined
39  */
40 #define CREATE_TRACE_POINTS
41 #include "i40e_trace.h"
42 
43 const char i40e_driver_name[] = "i40e";
44 static const char i40e_driver_string[] =
45 			"Intel(R) Ethernet Connection XL710 Network Driver";
46 
47 #define DRV_KERN "-k"
48 
49 #define DRV_VERSION_MAJOR 2
50 #define DRV_VERSION_MINOR 3
51 #define DRV_VERSION_BUILD 2
52 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
53 	     __stringify(DRV_VERSION_MINOR) "." \
54 	     __stringify(DRV_VERSION_BUILD)    DRV_KERN
55 const char i40e_driver_version_str[] = DRV_VERSION;
56 static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
57 
58 /* a bit of forward declarations */
59 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
60 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
61 static int i40e_add_vsi(struct i40e_vsi *vsi);
62 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
63 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
64 static int i40e_setup_misc_vector(struct i40e_pf *pf);
65 static void i40e_determine_queue_usage(struct i40e_pf *pf);
66 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
67 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
68 static int i40e_reset(struct i40e_pf *pf);
69 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
70 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
71 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
72 static int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
73 				     struct i40e_cloud_filter *filter,
74 				     bool add);
75 static int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
76 					     struct i40e_cloud_filter *filter,
77 					     bool add);
78 static int i40e_get_capabilities(struct i40e_pf *pf,
79 				 enum i40e_admin_queue_opc list_type);
80 
81 
82 /* i40e_pci_tbl - PCI Device ID Table
83  *
84  * Last entry must be all 0s
85  *
86  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
87  *   Class, Class Mask, private data (not used) }
88  */
89 static const struct pci_device_id i40e_pci_tbl[] = {
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
91 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
92 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
93 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
94 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
95 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
96 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
97 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
98 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
99 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
100 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
101 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
102 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
103 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
104 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
105 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
106 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
107 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
108 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
109 	/* required last entry */
110 	{0, }
111 };
112 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
113 
114 #define I40E_MAX_VF_COUNT 128
115 static int debug = -1;
116 module_param(debug, uint, 0);
117 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
118 
119 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
120 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
121 MODULE_LICENSE("GPL");
122 MODULE_VERSION(DRV_VERSION);
123 
124 static struct workqueue_struct *i40e_wq;
125 
126 /**
127  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
128  * @hw:   pointer to the HW structure
129  * @mem:  ptr to mem struct to fill out
130  * @size: size of memory requested
131  * @alignment: what to align the allocation to
132  **/
133 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
134 			    u64 size, u32 alignment)
135 {
136 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
137 
138 	mem->size = ALIGN(size, alignment);
139 	mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
140 				      &mem->pa, GFP_KERNEL);
141 	if (!mem->va)
142 		return -ENOMEM;
143 
144 	return 0;
145 }
146 
147 /**
148  * i40e_free_dma_mem_d - OS specific memory free for shared code
149  * @hw:   pointer to the HW structure
150  * @mem:  ptr to mem struct to free
151  **/
152 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
153 {
154 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
155 
156 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
157 	mem->va = NULL;
158 	mem->pa = 0;
159 	mem->size = 0;
160 
161 	return 0;
162 }
163 
164 /**
165  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
166  * @hw:   pointer to the HW structure
167  * @mem:  ptr to mem struct to fill out
168  * @size: size of memory requested
169  **/
170 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
171 			     u32 size)
172 {
173 	mem->size = size;
174 	mem->va = kzalloc(size, GFP_KERNEL);
175 
176 	if (!mem->va)
177 		return -ENOMEM;
178 
179 	return 0;
180 }
181 
182 /**
183  * i40e_free_virt_mem_d - OS specific memory free for shared code
184  * @hw:   pointer to the HW structure
185  * @mem:  ptr to mem struct to free
186  **/
187 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
188 {
189 	/* it's ok to kfree a NULL pointer */
190 	kfree(mem->va);
191 	mem->va = NULL;
192 	mem->size = 0;
193 
194 	return 0;
195 }
196 
197 /**
198  * i40e_get_lump - find a lump of free generic resource
199  * @pf: board private structure
200  * @pile: the pile of resource to search
201  * @needed: the number of items needed
202  * @id: an owner id to stick on the items assigned
203  *
204  * Returns the base item index of the lump, or negative for error
205  *
206  * The search_hint trick and lack of advanced fit-finding only work
207  * because we're highly likely to have all the same size lump requests.
208  * Linear search time and any fragmentation should be minimal.
209  **/
210 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
211 			 u16 needed, u16 id)
212 {
213 	int ret = -ENOMEM;
214 	int i, j;
215 
216 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
217 		dev_info(&pf->pdev->dev,
218 			 "param err: pile=%p needed=%d id=0x%04x\n",
219 			 pile, needed, id);
220 		return -EINVAL;
221 	}
222 
223 	/* start the linear search with an imperfect hint */
224 	i = pile->search_hint;
225 	while (i < pile->num_entries) {
226 		/* skip already allocated entries */
227 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
228 			i++;
229 			continue;
230 		}
231 
232 		/* do we have enough in this lump? */
233 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
234 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
235 				break;
236 		}
237 
238 		if (j == needed) {
239 			/* there was enough, so assign it to the requestor */
240 			for (j = 0; j < needed; j++)
241 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
242 			ret = i;
243 			pile->search_hint = i + j;
244 			break;
245 		}
246 
247 		/* not enough, so skip over it and continue looking */
248 		i += j;
249 	}
250 
251 	return ret;
252 }
253 
254 /**
255  * i40e_put_lump - return a lump of generic resource
256  * @pile: the pile of resource to search
257  * @index: the base item index
258  * @id: the owner id of the items assigned
259  *
260  * Returns the count of items in the lump
261  **/
262 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
263 {
264 	int valid_id = (id | I40E_PILE_VALID_BIT);
265 	int count = 0;
266 	int i;
267 
268 	if (!pile || index >= pile->num_entries)
269 		return -EINVAL;
270 
271 	for (i = index;
272 	     i < pile->num_entries && pile->list[i] == valid_id;
273 	     i++) {
274 		pile->list[i] = 0;
275 		count++;
276 	}
277 
278 	if (count && index < pile->search_hint)
279 		pile->search_hint = index;
280 
281 	return count;
282 }
283 
284 /**
285  * i40e_find_vsi_from_id - searches for the vsi with the given id
286  * @pf - the pf structure to search for the vsi
287  * @id - id of the vsi it is searching for
288  **/
289 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
290 {
291 	int i;
292 
293 	for (i = 0; i < pf->num_alloc_vsi; i++)
294 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
295 			return pf->vsi[i];
296 
297 	return NULL;
298 }
299 
300 /**
301  * i40e_service_event_schedule - Schedule the service task to wake up
302  * @pf: board private structure
303  *
304  * If not already scheduled, this puts the task into the work queue
305  **/
306 void i40e_service_event_schedule(struct i40e_pf *pf)
307 {
308 	if (!test_bit(__I40E_DOWN, pf->state) &&
309 	    !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
310 		queue_work(i40e_wq, &pf->service_task);
311 }
312 
313 /**
314  * i40e_tx_timeout - Respond to a Tx Hang
315  * @netdev: network interface device structure
316  *
317  * If any port has noticed a Tx timeout, it is likely that the whole
318  * device is munged, not just the one netdev port, so go for the full
319  * reset.
320  **/
321 static void i40e_tx_timeout(struct net_device *netdev)
322 {
323 	struct i40e_netdev_priv *np = netdev_priv(netdev);
324 	struct i40e_vsi *vsi = np->vsi;
325 	struct i40e_pf *pf = vsi->back;
326 	struct i40e_ring *tx_ring = NULL;
327 	unsigned int i, hung_queue = 0;
328 	u32 head, val;
329 
330 	pf->tx_timeout_count++;
331 
332 	/* find the stopped queue the same way the stack does */
333 	for (i = 0; i < netdev->num_tx_queues; i++) {
334 		struct netdev_queue *q;
335 		unsigned long trans_start;
336 
337 		q = netdev_get_tx_queue(netdev, i);
338 		trans_start = q->trans_start;
339 		if (netif_xmit_stopped(q) &&
340 		    time_after(jiffies,
341 			       (trans_start + netdev->watchdog_timeo))) {
342 			hung_queue = i;
343 			break;
344 		}
345 	}
346 
347 	if (i == netdev->num_tx_queues) {
348 		netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
349 	} else {
350 		/* now that we have an index, find the tx_ring struct */
351 		for (i = 0; i < vsi->num_queue_pairs; i++) {
352 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
353 				if (hung_queue ==
354 				    vsi->tx_rings[i]->queue_index) {
355 					tx_ring = vsi->tx_rings[i];
356 					break;
357 				}
358 			}
359 		}
360 	}
361 
362 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
363 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
364 	else if (time_before(jiffies,
365 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
366 		return;   /* don't do any new action before the next timeout */
367 
368 	if (tx_ring) {
369 		head = i40e_get_head(tx_ring);
370 		/* Read interrupt register */
371 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
372 			val = rd32(&pf->hw,
373 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
374 						tx_ring->vsi->base_vector - 1));
375 		else
376 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
377 
378 		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",
379 			    vsi->seid, hung_queue, tx_ring->next_to_clean,
380 			    head, tx_ring->next_to_use,
381 			    readl(tx_ring->tail), val);
382 	}
383 
384 	pf->tx_timeout_last_recovery = jiffies;
385 	netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
386 		    pf->tx_timeout_recovery_level, hung_queue);
387 
388 	switch (pf->tx_timeout_recovery_level) {
389 	case 1:
390 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
391 		break;
392 	case 2:
393 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
394 		break;
395 	case 3:
396 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
397 		break;
398 	default:
399 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
400 		break;
401 	}
402 
403 	i40e_service_event_schedule(pf);
404 	pf->tx_timeout_recovery_level++;
405 }
406 
407 /**
408  * i40e_get_vsi_stats_struct - Get System Network Statistics
409  * @vsi: the VSI we care about
410  *
411  * Returns the address of the device statistics structure.
412  * The statistics are actually updated from the service task.
413  **/
414 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
415 {
416 	return &vsi->net_stats;
417 }
418 
419 /**
420  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
421  * @ring: Tx ring to get statistics from
422  * @stats: statistics entry to be updated
423  **/
424 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
425 					    struct rtnl_link_stats64 *stats)
426 {
427 	u64 bytes, packets;
428 	unsigned int start;
429 
430 	do {
431 		start = u64_stats_fetch_begin_irq(&ring->syncp);
432 		packets = ring->stats.packets;
433 		bytes   = ring->stats.bytes;
434 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
435 
436 	stats->tx_packets += packets;
437 	stats->tx_bytes   += bytes;
438 }
439 
440 /**
441  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
442  * @netdev: network interface device structure
443  *
444  * Returns the address of the device statistics structure.
445  * The statistics are actually updated from the service task.
446  **/
447 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
448 				  struct rtnl_link_stats64 *stats)
449 {
450 	struct i40e_netdev_priv *np = netdev_priv(netdev);
451 	struct i40e_ring *tx_ring, *rx_ring;
452 	struct i40e_vsi *vsi = np->vsi;
453 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
454 	int i;
455 
456 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
457 		return;
458 
459 	if (!vsi->tx_rings)
460 		return;
461 
462 	rcu_read_lock();
463 	for (i = 0; i < vsi->num_queue_pairs; i++) {
464 		u64 bytes, packets;
465 		unsigned int start;
466 
467 		tx_ring = READ_ONCE(vsi->tx_rings[i]);
468 		if (!tx_ring)
469 			continue;
470 		i40e_get_netdev_stats_struct_tx(tx_ring, stats);
471 
472 		rx_ring = &tx_ring[1];
473 
474 		do {
475 			start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
476 			packets = rx_ring->stats.packets;
477 			bytes   = rx_ring->stats.bytes;
478 		} while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
479 
480 		stats->rx_packets += packets;
481 		stats->rx_bytes   += bytes;
482 
483 		if (i40e_enabled_xdp_vsi(vsi))
484 			i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
485 	}
486 	rcu_read_unlock();
487 
488 	/* following stats updated by i40e_watchdog_subtask() */
489 	stats->multicast	= vsi_stats->multicast;
490 	stats->tx_errors	= vsi_stats->tx_errors;
491 	stats->tx_dropped	= vsi_stats->tx_dropped;
492 	stats->rx_errors	= vsi_stats->rx_errors;
493 	stats->rx_dropped	= vsi_stats->rx_dropped;
494 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
495 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
496 }
497 
498 /**
499  * i40e_vsi_reset_stats - Resets all stats of the given vsi
500  * @vsi: the VSI to have its stats reset
501  **/
502 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
503 {
504 	struct rtnl_link_stats64 *ns;
505 	int i;
506 
507 	if (!vsi)
508 		return;
509 
510 	ns = i40e_get_vsi_stats_struct(vsi);
511 	memset(ns, 0, sizeof(*ns));
512 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
513 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
514 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
515 	if (vsi->rx_rings && vsi->rx_rings[0]) {
516 		for (i = 0; i < vsi->num_queue_pairs; i++) {
517 			memset(&vsi->rx_rings[i]->stats, 0,
518 			       sizeof(vsi->rx_rings[i]->stats));
519 			memset(&vsi->rx_rings[i]->rx_stats, 0,
520 			       sizeof(vsi->rx_rings[i]->rx_stats));
521 			memset(&vsi->tx_rings[i]->stats, 0,
522 			       sizeof(vsi->tx_rings[i]->stats));
523 			memset(&vsi->tx_rings[i]->tx_stats, 0,
524 			       sizeof(vsi->tx_rings[i]->tx_stats));
525 		}
526 	}
527 	vsi->stat_offsets_loaded = false;
528 }
529 
530 /**
531  * i40e_pf_reset_stats - Reset all of the stats for the given PF
532  * @pf: the PF to be reset
533  **/
534 void i40e_pf_reset_stats(struct i40e_pf *pf)
535 {
536 	int i;
537 
538 	memset(&pf->stats, 0, sizeof(pf->stats));
539 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
540 	pf->stat_offsets_loaded = false;
541 
542 	for (i = 0; i < I40E_MAX_VEB; i++) {
543 		if (pf->veb[i]) {
544 			memset(&pf->veb[i]->stats, 0,
545 			       sizeof(pf->veb[i]->stats));
546 			memset(&pf->veb[i]->stats_offsets, 0,
547 			       sizeof(pf->veb[i]->stats_offsets));
548 			pf->veb[i]->stat_offsets_loaded = false;
549 		}
550 	}
551 	pf->hw_csum_rx_error = 0;
552 }
553 
554 /**
555  * i40e_stat_update48 - read and update a 48 bit stat from the chip
556  * @hw: ptr to the hardware info
557  * @hireg: the high 32 bit reg to read
558  * @loreg: the low 32 bit reg to read
559  * @offset_loaded: has the initial offset been loaded yet
560  * @offset: ptr to current offset value
561  * @stat: ptr to the stat
562  *
563  * Since the device stats are not reset at PFReset, they likely will not
564  * be zeroed when the driver starts.  We'll save the first values read
565  * and use them as offsets to be subtracted from the raw values in order
566  * to report stats that count from zero.  In the process, we also manage
567  * the potential roll-over.
568  **/
569 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
570 			       bool offset_loaded, u64 *offset, u64 *stat)
571 {
572 	u64 new_data;
573 
574 	if (hw->device_id == I40E_DEV_ID_QEMU) {
575 		new_data = rd32(hw, loreg);
576 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
577 	} else {
578 		new_data = rd64(hw, loreg);
579 	}
580 	if (!offset_loaded)
581 		*offset = new_data;
582 	if (likely(new_data >= *offset))
583 		*stat = new_data - *offset;
584 	else
585 		*stat = (new_data + BIT_ULL(48)) - *offset;
586 	*stat &= 0xFFFFFFFFFFFFULL;
587 }
588 
589 /**
590  * i40e_stat_update32 - read and update a 32 bit stat from the chip
591  * @hw: ptr to the hardware info
592  * @reg: the hw reg to read
593  * @offset_loaded: has the initial offset been loaded yet
594  * @offset: ptr to current offset value
595  * @stat: ptr to the stat
596  **/
597 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
598 			       bool offset_loaded, u64 *offset, u64 *stat)
599 {
600 	u32 new_data;
601 
602 	new_data = rd32(hw, reg);
603 	if (!offset_loaded)
604 		*offset = new_data;
605 	if (likely(new_data >= *offset))
606 		*stat = (u32)(new_data - *offset);
607 	else
608 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
609 }
610 
611 /**
612  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
613  * @hw: ptr to the hardware info
614  * @reg: the hw reg to read and clear
615  * @stat: ptr to the stat
616  **/
617 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
618 {
619 	u32 new_data = rd32(hw, reg);
620 
621 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
622 	*stat += new_data;
623 }
624 
625 /**
626  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
627  * @vsi: the VSI to be updated
628  **/
629 void i40e_update_eth_stats(struct i40e_vsi *vsi)
630 {
631 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
632 	struct i40e_pf *pf = vsi->back;
633 	struct i40e_hw *hw = &pf->hw;
634 	struct i40e_eth_stats *oes;
635 	struct i40e_eth_stats *es;     /* device's eth stats */
636 
637 	es = &vsi->eth_stats;
638 	oes = &vsi->eth_stats_offsets;
639 
640 	/* Gather up the stats that the hw collects */
641 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
642 			   vsi->stat_offsets_loaded,
643 			   &oes->tx_errors, &es->tx_errors);
644 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
645 			   vsi->stat_offsets_loaded,
646 			   &oes->rx_discards, &es->rx_discards);
647 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
648 			   vsi->stat_offsets_loaded,
649 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
650 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
651 			   vsi->stat_offsets_loaded,
652 			   &oes->tx_errors, &es->tx_errors);
653 
654 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
655 			   I40E_GLV_GORCL(stat_idx),
656 			   vsi->stat_offsets_loaded,
657 			   &oes->rx_bytes, &es->rx_bytes);
658 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
659 			   I40E_GLV_UPRCL(stat_idx),
660 			   vsi->stat_offsets_loaded,
661 			   &oes->rx_unicast, &es->rx_unicast);
662 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
663 			   I40E_GLV_MPRCL(stat_idx),
664 			   vsi->stat_offsets_loaded,
665 			   &oes->rx_multicast, &es->rx_multicast);
666 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
667 			   I40E_GLV_BPRCL(stat_idx),
668 			   vsi->stat_offsets_loaded,
669 			   &oes->rx_broadcast, &es->rx_broadcast);
670 
671 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
672 			   I40E_GLV_GOTCL(stat_idx),
673 			   vsi->stat_offsets_loaded,
674 			   &oes->tx_bytes, &es->tx_bytes);
675 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
676 			   I40E_GLV_UPTCL(stat_idx),
677 			   vsi->stat_offsets_loaded,
678 			   &oes->tx_unicast, &es->tx_unicast);
679 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
680 			   I40E_GLV_MPTCL(stat_idx),
681 			   vsi->stat_offsets_loaded,
682 			   &oes->tx_multicast, &es->tx_multicast);
683 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
684 			   I40E_GLV_BPTCL(stat_idx),
685 			   vsi->stat_offsets_loaded,
686 			   &oes->tx_broadcast, &es->tx_broadcast);
687 	vsi->stat_offsets_loaded = true;
688 }
689 
690 /**
691  * i40e_update_veb_stats - Update Switch component statistics
692  * @veb: the VEB being updated
693  **/
694 static void i40e_update_veb_stats(struct i40e_veb *veb)
695 {
696 	struct i40e_pf *pf = veb->pf;
697 	struct i40e_hw *hw = &pf->hw;
698 	struct i40e_eth_stats *oes;
699 	struct i40e_eth_stats *es;     /* device's eth stats */
700 	struct i40e_veb_tc_stats *veb_oes;
701 	struct i40e_veb_tc_stats *veb_es;
702 	int i, idx = 0;
703 
704 	idx = veb->stats_idx;
705 	es = &veb->stats;
706 	oes = &veb->stats_offsets;
707 	veb_es = &veb->tc_stats;
708 	veb_oes = &veb->tc_stats_offsets;
709 
710 	/* Gather up the stats that the hw collects */
711 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
712 			   veb->stat_offsets_loaded,
713 			   &oes->tx_discards, &es->tx_discards);
714 	if (hw->revision_id > 0)
715 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
716 				   veb->stat_offsets_loaded,
717 				   &oes->rx_unknown_protocol,
718 				   &es->rx_unknown_protocol);
719 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
720 			   veb->stat_offsets_loaded,
721 			   &oes->rx_bytes, &es->rx_bytes);
722 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
723 			   veb->stat_offsets_loaded,
724 			   &oes->rx_unicast, &es->rx_unicast);
725 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
726 			   veb->stat_offsets_loaded,
727 			   &oes->rx_multicast, &es->rx_multicast);
728 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
729 			   veb->stat_offsets_loaded,
730 			   &oes->rx_broadcast, &es->rx_broadcast);
731 
732 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
733 			   veb->stat_offsets_loaded,
734 			   &oes->tx_bytes, &es->tx_bytes);
735 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
736 			   veb->stat_offsets_loaded,
737 			   &oes->tx_unicast, &es->tx_unicast);
738 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
739 			   veb->stat_offsets_loaded,
740 			   &oes->tx_multicast, &es->tx_multicast);
741 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
742 			   veb->stat_offsets_loaded,
743 			   &oes->tx_broadcast, &es->tx_broadcast);
744 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
745 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
746 				   I40E_GLVEBTC_RPCL(i, idx),
747 				   veb->stat_offsets_loaded,
748 				   &veb_oes->tc_rx_packets[i],
749 				   &veb_es->tc_rx_packets[i]);
750 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
751 				   I40E_GLVEBTC_RBCL(i, idx),
752 				   veb->stat_offsets_loaded,
753 				   &veb_oes->tc_rx_bytes[i],
754 				   &veb_es->tc_rx_bytes[i]);
755 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
756 				   I40E_GLVEBTC_TPCL(i, idx),
757 				   veb->stat_offsets_loaded,
758 				   &veb_oes->tc_tx_packets[i],
759 				   &veb_es->tc_tx_packets[i]);
760 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
761 				   I40E_GLVEBTC_TBCL(i, idx),
762 				   veb->stat_offsets_loaded,
763 				   &veb_oes->tc_tx_bytes[i],
764 				   &veb_es->tc_tx_bytes[i]);
765 	}
766 	veb->stat_offsets_loaded = true;
767 }
768 
769 /**
770  * i40e_update_vsi_stats - Update the vsi statistics counters.
771  * @vsi: the VSI to be updated
772  *
773  * There are a few instances where we store the same stat in a
774  * couple of different structs.  This is partly because we have
775  * the netdev stats that need to be filled out, which is slightly
776  * different from the "eth_stats" defined by the chip and used in
777  * VF communications.  We sort it out here.
778  **/
779 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
780 {
781 	struct i40e_pf *pf = vsi->back;
782 	struct rtnl_link_stats64 *ons;
783 	struct rtnl_link_stats64 *ns;   /* netdev stats */
784 	struct i40e_eth_stats *oes;
785 	struct i40e_eth_stats *es;     /* device's eth stats */
786 	u32 tx_restart, tx_busy;
787 	struct i40e_ring *p;
788 	u32 rx_page, rx_buf;
789 	u64 bytes, packets;
790 	unsigned int start;
791 	u64 tx_linearize;
792 	u64 tx_force_wb;
793 	u64 rx_p, rx_b;
794 	u64 tx_p, tx_b;
795 	u16 q;
796 
797 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
798 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
799 		return;
800 
801 	ns = i40e_get_vsi_stats_struct(vsi);
802 	ons = &vsi->net_stats_offsets;
803 	es = &vsi->eth_stats;
804 	oes = &vsi->eth_stats_offsets;
805 
806 	/* Gather up the netdev and vsi stats that the driver collects
807 	 * on the fly during packet processing
808 	 */
809 	rx_b = rx_p = 0;
810 	tx_b = tx_p = 0;
811 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
812 	rx_page = 0;
813 	rx_buf = 0;
814 	rcu_read_lock();
815 	for (q = 0; q < vsi->num_queue_pairs; q++) {
816 		/* locate Tx ring */
817 		p = READ_ONCE(vsi->tx_rings[q]);
818 
819 		do {
820 			start = u64_stats_fetch_begin_irq(&p->syncp);
821 			packets = p->stats.packets;
822 			bytes = p->stats.bytes;
823 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
824 		tx_b += bytes;
825 		tx_p += packets;
826 		tx_restart += p->tx_stats.restart_queue;
827 		tx_busy += p->tx_stats.tx_busy;
828 		tx_linearize += p->tx_stats.tx_linearize;
829 		tx_force_wb += p->tx_stats.tx_force_wb;
830 
831 		/* Rx queue is part of the same block as Tx queue */
832 		p = &p[1];
833 		do {
834 			start = u64_stats_fetch_begin_irq(&p->syncp);
835 			packets = p->stats.packets;
836 			bytes = p->stats.bytes;
837 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
838 		rx_b += bytes;
839 		rx_p += packets;
840 		rx_buf += p->rx_stats.alloc_buff_failed;
841 		rx_page += p->rx_stats.alloc_page_failed;
842 	}
843 	rcu_read_unlock();
844 	vsi->tx_restart = tx_restart;
845 	vsi->tx_busy = tx_busy;
846 	vsi->tx_linearize = tx_linearize;
847 	vsi->tx_force_wb = tx_force_wb;
848 	vsi->rx_page_failed = rx_page;
849 	vsi->rx_buf_failed = rx_buf;
850 
851 	ns->rx_packets = rx_p;
852 	ns->rx_bytes = rx_b;
853 	ns->tx_packets = tx_p;
854 	ns->tx_bytes = tx_b;
855 
856 	/* update netdev stats from eth stats */
857 	i40e_update_eth_stats(vsi);
858 	ons->tx_errors = oes->tx_errors;
859 	ns->tx_errors = es->tx_errors;
860 	ons->multicast = oes->rx_multicast;
861 	ns->multicast = es->rx_multicast;
862 	ons->rx_dropped = oes->rx_discards;
863 	ns->rx_dropped = es->rx_discards;
864 	ons->tx_dropped = oes->tx_discards;
865 	ns->tx_dropped = es->tx_discards;
866 
867 	/* pull in a couple PF stats if this is the main vsi */
868 	if (vsi == pf->vsi[pf->lan_vsi]) {
869 		ns->rx_crc_errors = pf->stats.crc_errors;
870 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
871 		ns->rx_length_errors = pf->stats.rx_length_errors;
872 	}
873 }
874 
875 /**
876  * i40e_update_pf_stats - Update the PF statistics counters.
877  * @pf: the PF to be updated
878  **/
879 static void i40e_update_pf_stats(struct i40e_pf *pf)
880 {
881 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
882 	struct i40e_hw_port_stats *nsd = &pf->stats;
883 	struct i40e_hw *hw = &pf->hw;
884 	u32 val;
885 	int i;
886 
887 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
888 			   I40E_GLPRT_GORCL(hw->port),
889 			   pf->stat_offsets_loaded,
890 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
891 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
892 			   I40E_GLPRT_GOTCL(hw->port),
893 			   pf->stat_offsets_loaded,
894 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
895 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
896 			   pf->stat_offsets_loaded,
897 			   &osd->eth.rx_discards,
898 			   &nsd->eth.rx_discards);
899 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
900 			   I40E_GLPRT_UPRCL(hw->port),
901 			   pf->stat_offsets_loaded,
902 			   &osd->eth.rx_unicast,
903 			   &nsd->eth.rx_unicast);
904 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
905 			   I40E_GLPRT_MPRCL(hw->port),
906 			   pf->stat_offsets_loaded,
907 			   &osd->eth.rx_multicast,
908 			   &nsd->eth.rx_multicast);
909 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
910 			   I40E_GLPRT_BPRCL(hw->port),
911 			   pf->stat_offsets_loaded,
912 			   &osd->eth.rx_broadcast,
913 			   &nsd->eth.rx_broadcast);
914 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
915 			   I40E_GLPRT_UPTCL(hw->port),
916 			   pf->stat_offsets_loaded,
917 			   &osd->eth.tx_unicast,
918 			   &nsd->eth.tx_unicast);
919 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
920 			   I40E_GLPRT_MPTCL(hw->port),
921 			   pf->stat_offsets_loaded,
922 			   &osd->eth.tx_multicast,
923 			   &nsd->eth.tx_multicast);
924 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
925 			   I40E_GLPRT_BPTCL(hw->port),
926 			   pf->stat_offsets_loaded,
927 			   &osd->eth.tx_broadcast,
928 			   &nsd->eth.tx_broadcast);
929 
930 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
931 			   pf->stat_offsets_loaded,
932 			   &osd->tx_dropped_link_down,
933 			   &nsd->tx_dropped_link_down);
934 
935 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
936 			   pf->stat_offsets_loaded,
937 			   &osd->crc_errors, &nsd->crc_errors);
938 
939 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
940 			   pf->stat_offsets_loaded,
941 			   &osd->illegal_bytes, &nsd->illegal_bytes);
942 
943 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
944 			   pf->stat_offsets_loaded,
945 			   &osd->mac_local_faults,
946 			   &nsd->mac_local_faults);
947 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
948 			   pf->stat_offsets_loaded,
949 			   &osd->mac_remote_faults,
950 			   &nsd->mac_remote_faults);
951 
952 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
953 			   pf->stat_offsets_loaded,
954 			   &osd->rx_length_errors,
955 			   &nsd->rx_length_errors);
956 
957 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
958 			   pf->stat_offsets_loaded,
959 			   &osd->link_xon_rx, &nsd->link_xon_rx);
960 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
961 			   pf->stat_offsets_loaded,
962 			   &osd->link_xon_tx, &nsd->link_xon_tx);
963 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
964 			   pf->stat_offsets_loaded,
965 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
966 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
967 			   pf->stat_offsets_loaded,
968 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
969 
970 	for (i = 0; i < 8; i++) {
971 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
972 				   pf->stat_offsets_loaded,
973 				   &osd->priority_xoff_rx[i],
974 				   &nsd->priority_xoff_rx[i]);
975 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
976 				   pf->stat_offsets_loaded,
977 				   &osd->priority_xon_rx[i],
978 				   &nsd->priority_xon_rx[i]);
979 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
980 				   pf->stat_offsets_loaded,
981 				   &osd->priority_xon_tx[i],
982 				   &nsd->priority_xon_tx[i]);
983 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
984 				   pf->stat_offsets_loaded,
985 				   &osd->priority_xoff_tx[i],
986 				   &nsd->priority_xoff_tx[i]);
987 		i40e_stat_update32(hw,
988 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
989 				   pf->stat_offsets_loaded,
990 				   &osd->priority_xon_2_xoff[i],
991 				   &nsd->priority_xon_2_xoff[i]);
992 	}
993 
994 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
995 			   I40E_GLPRT_PRC64L(hw->port),
996 			   pf->stat_offsets_loaded,
997 			   &osd->rx_size_64, &nsd->rx_size_64);
998 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
999 			   I40E_GLPRT_PRC127L(hw->port),
1000 			   pf->stat_offsets_loaded,
1001 			   &osd->rx_size_127, &nsd->rx_size_127);
1002 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1003 			   I40E_GLPRT_PRC255L(hw->port),
1004 			   pf->stat_offsets_loaded,
1005 			   &osd->rx_size_255, &nsd->rx_size_255);
1006 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1007 			   I40E_GLPRT_PRC511L(hw->port),
1008 			   pf->stat_offsets_loaded,
1009 			   &osd->rx_size_511, &nsd->rx_size_511);
1010 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1011 			   I40E_GLPRT_PRC1023L(hw->port),
1012 			   pf->stat_offsets_loaded,
1013 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1014 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1015 			   I40E_GLPRT_PRC1522L(hw->port),
1016 			   pf->stat_offsets_loaded,
1017 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1018 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1019 			   I40E_GLPRT_PRC9522L(hw->port),
1020 			   pf->stat_offsets_loaded,
1021 			   &osd->rx_size_big, &nsd->rx_size_big);
1022 
1023 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1024 			   I40E_GLPRT_PTC64L(hw->port),
1025 			   pf->stat_offsets_loaded,
1026 			   &osd->tx_size_64, &nsd->tx_size_64);
1027 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1028 			   I40E_GLPRT_PTC127L(hw->port),
1029 			   pf->stat_offsets_loaded,
1030 			   &osd->tx_size_127, &nsd->tx_size_127);
1031 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1032 			   I40E_GLPRT_PTC255L(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->tx_size_255, &nsd->tx_size_255);
1035 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1036 			   I40E_GLPRT_PTC511L(hw->port),
1037 			   pf->stat_offsets_loaded,
1038 			   &osd->tx_size_511, &nsd->tx_size_511);
1039 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1040 			   I40E_GLPRT_PTC1023L(hw->port),
1041 			   pf->stat_offsets_loaded,
1042 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1043 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1044 			   I40E_GLPRT_PTC1522L(hw->port),
1045 			   pf->stat_offsets_loaded,
1046 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1047 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1048 			   I40E_GLPRT_PTC9522L(hw->port),
1049 			   pf->stat_offsets_loaded,
1050 			   &osd->tx_size_big, &nsd->tx_size_big);
1051 
1052 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1053 			   pf->stat_offsets_loaded,
1054 			   &osd->rx_undersize, &nsd->rx_undersize);
1055 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->rx_fragments, &nsd->rx_fragments);
1058 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1059 			   pf->stat_offsets_loaded,
1060 			   &osd->rx_oversize, &nsd->rx_oversize);
1061 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1062 			   pf->stat_offsets_loaded,
1063 			   &osd->rx_jabber, &nsd->rx_jabber);
1064 
1065 	/* FDIR stats */
1066 	i40e_stat_update_and_clear32(hw,
1067 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1068 			&nsd->fd_atr_match);
1069 	i40e_stat_update_and_clear32(hw,
1070 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1071 			&nsd->fd_sb_match);
1072 	i40e_stat_update_and_clear32(hw,
1073 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1074 			&nsd->fd_atr_tunnel_match);
1075 
1076 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1077 	nsd->tx_lpi_status =
1078 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1079 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1080 	nsd->rx_lpi_status =
1081 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1082 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1083 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1084 			   pf->stat_offsets_loaded,
1085 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1086 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1087 			   pf->stat_offsets_loaded,
1088 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1089 
1090 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1091 	    !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED))
1092 		nsd->fd_sb_status = true;
1093 	else
1094 		nsd->fd_sb_status = false;
1095 
1096 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1097 	    !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
1098 		nsd->fd_atr_status = true;
1099 	else
1100 		nsd->fd_atr_status = false;
1101 
1102 	pf->stat_offsets_loaded = true;
1103 }
1104 
1105 /**
1106  * i40e_update_stats - Update the various statistics counters.
1107  * @vsi: the VSI to be updated
1108  *
1109  * Update the various stats for this VSI and its related entities.
1110  **/
1111 void i40e_update_stats(struct i40e_vsi *vsi)
1112 {
1113 	struct i40e_pf *pf = vsi->back;
1114 
1115 	if (vsi == pf->vsi[pf->lan_vsi])
1116 		i40e_update_pf_stats(pf);
1117 
1118 	i40e_update_vsi_stats(vsi);
1119 }
1120 
1121 /**
1122  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1123  * @vsi: the VSI to be searched
1124  * @macaddr: the MAC address
1125  * @vlan: the vlan
1126  *
1127  * Returns ptr to the filter object or NULL
1128  **/
1129 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1130 						const u8 *macaddr, s16 vlan)
1131 {
1132 	struct i40e_mac_filter *f;
1133 	u64 key;
1134 
1135 	if (!vsi || !macaddr)
1136 		return NULL;
1137 
1138 	key = i40e_addr_to_hkey(macaddr);
1139 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1140 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1141 		    (vlan == f->vlan))
1142 			return f;
1143 	}
1144 	return NULL;
1145 }
1146 
1147 /**
1148  * i40e_find_mac - Find a mac addr in the macvlan filters list
1149  * @vsi: the VSI to be searched
1150  * @macaddr: the MAC address we are searching for
1151  *
1152  * Returns the first filter with the provided MAC address or NULL if
1153  * MAC address was not found
1154  **/
1155 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1156 {
1157 	struct i40e_mac_filter *f;
1158 	u64 key;
1159 
1160 	if (!vsi || !macaddr)
1161 		return NULL;
1162 
1163 	key = i40e_addr_to_hkey(macaddr);
1164 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1165 		if ((ether_addr_equal(macaddr, f->macaddr)))
1166 			return f;
1167 	}
1168 	return NULL;
1169 }
1170 
1171 /**
1172  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1173  * @vsi: the VSI to be searched
1174  *
1175  * Returns true if VSI is in vlan mode or false otherwise
1176  **/
1177 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1178 {
1179 	/* If we have a PVID, always operate in VLAN mode */
1180 	if (vsi->info.pvid)
1181 		return true;
1182 
1183 	/* We need to operate in VLAN mode whenever we have any filters with
1184 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1185 	 * time, incurring search cost repeatedly. However, we can notice two
1186 	 * things:
1187 	 *
1188 	 * 1) the only place where we can gain a VLAN filter is in
1189 	 *    i40e_add_filter.
1190 	 *
1191 	 * 2) the only place where filters are actually removed is in
1192 	 *    i40e_sync_filters_subtask.
1193 	 *
1194 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1195 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1196 	 * we have to perform the full search after deleting filters in
1197 	 * i40e_sync_filters_subtask, but we already have to search
1198 	 * filters here and can perform the check at the same time. This
1199 	 * results in avoiding embedding a loop for VLAN mode inside another
1200 	 * loop over all the filters, and should maintain correctness as noted
1201 	 * above.
1202 	 */
1203 	return vsi->has_vlan_filter;
1204 }
1205 
1206 /**
1207  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1208  * @vsi: the VSI to configure
1209  * @tmp_add_list: list of filters ready to be added
1210  * @tmp_del_list: list of filters ready to be deleted
1211  * @vlan_filters: the number of active VLAN filters
1212  *
1213  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1214  * behave as expected. If we have any active VLAN filters remaining or about
1215  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1216  * so that they only match against untagged traffic. If we no longer have any
1217  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1218  * so that they match against both tagged and untagged traffic. In this way,
1219  * we ensure that we correctly receive the desired traffic. This ensures that
1220  * when we have an active VLAN we will receive only untagged traffic and
1221  * traffic matching active VLANs. If we have no active VLANs then we will
1222  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1223  *
1224  * Finally, in a similar fashion, this function also corrects filters when
1225  * there is an active PVID assigned to this VSI.
1226  *
1227  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1228  *
1229  * This function is only expected to be called from within
1230  * i40e_sync_vsi_filters.
1231  *
1232  * NOTE: This function expects to be called while under the
1233  * mac_filter_hash_lock
1234  */
1235 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1236 					 struct hlist_head *tmp_add_list,
1237 					 struct hlist_head *tmp_del_list,
1238 					 int vlan_filters)
1239 {
1240 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1241 	struct i40e_mac_filter *f, *add_head;
1242 	struct i40e_new_mac_filter *new;
1243 	struct hlist_node *h;
1244 	int bkt, new_vlan;
1245 
1246 	/* To determine if a particular filter needs to be replaced we
1247 	 * have the three following conditions:
1248 	 *
1249 	 * a) if we have a PVID assigned, then all filters which are
1250 	 *    not marked as VLAN=PVID must be replaced with filters that
1251 	 *    are.
1252 	 * b) otherwise, if we have any active VLANS, all filters
1253 	 *    which are marked as VLAN=-1 must be replaced with
1254 	 *    filters marked as VLAN=0
1255 	 * c) finally, if we do not have any active VLANS, all filters
1256 	 *    which are marked as VLAN=0 must be replaced with filters
1257 	 *    marked as VLAN=-1
1258 	 */
1259 
1260 	/* Update the filters about to be added in place */
1261 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1262 		if (pvid && new->f->vlan != pvid)
1263 			new->f->vlan = pvid;
1264 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1265 			new->f->vlan = 0;
1266 		else if (!vlan_filters && new->f->vlan == 0)
1267 			new->f->vlan = I40E_VLAN_ANY;
1268 	}
1269 
1270 	/* Update the remaining active filters */
1271 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1272 		/* Combine the checks for whether a filter needs to be changed
1273 		 * and then determine the new VLAN inside the if block, in
1274 		 * order to avoid duplicating code for adding the new filter
1275 		 * then deleting the old filter.
1276 		 */
1277 		if ((pvid && f->vlan != pvid) ||
1278 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1279 		    (!vlan_filters && f->vlan == 0)) {
1280 			/* Determine the new vlan we will be adding */
1281 			if (pvid)
1282 				new_vlan = pvid;
1283 			else if (vlan_filters)
1284 				new_vlan = 0;
1285 			else
1286 				new_vlan = I40E_VLAN_ANY;
1287 
1288 			/* Create the new filter */
1289 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1290 			if (!add_head)
1291 				return -ENOMEM;
1292 
1293 			/* Create a temporary i40e_new_mac_filter */
1294 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1295 			if (!new)
1296 				return -ENOMEM;
1297 
1298 			new->f = add_head;
1299 			new->state = add_head->state;
1300 
1301 			/* Add the new filter to the tmp list */
1302 			hlist_add_head(&new->hlist, tmp_add_list);
1303 
1304 			/* Put the original filter into the delete list */
1305 			f->state = I40E_FILTER_REMOVE;
1306 			hash_del(&f->hlist);
1307 			hlist_add_head(&f->hlist, tmp_del_list);
1308 		}
1309 	}
1310 
1311 	vsi->has_vlan_filter = !!vlan_filters;
1312 
1313 	return 0;
1314 }
1315 
1316 /**
1317  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1318  * @vsi: the PF Main VSI - inappropriate for any other VSI
1319  * @macaddr: the MAC address
1320  *
1321  * Remove whatever filter the firmware set up so the driver can manage
1322  * its own filtering intelligently.
1323  **/
1324 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1325 {
1326 	struct i40e_aqc_remove_macvlan_element_data element;
1327 	struct i40e_pf *pf = vsi->back;
1328 
1329 	/* Only appropriate for the PF main VSI */
1330 	if (vsi->type != I40E_VSI_MAIN)
1331 		return;
1332 
1333 	memset(&element, 0, sizeof(element));
1334 	ether_addr_copy(element.mac_addr, macaddr);
1335 	element.vlan_tag = 0;
1336 	/* Ignore error returns, some firmware does it this way... */
1337 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1338 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1339 
1340 	memset(&element, 0, sizeof(element));
1341 	ether_addr_copy(element.mac_addr, macaddr);
1342 	element.vlan_tag = 0;
1343 	/* ...and some firmware does it this way. */
1344 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1345 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1346 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1347 }
1348 
1349 /**
1350  * i40e_add_filter - Add a mac/vlan filter to the VSI
1351  * @vsi: the VSI to be searched
1352  * @macaddr: the MAC address
1353  * @vlan: the vlan
1354  *
1355  * Returns ptr to the filter object or NULL when no memory available.
1356  *
1357  * NOTE: This function is expected to be called with mac_filter_hash_lock
1358  * being held.
1359  **/
1360 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1361 					const u8 *macaddr, s16 vlan)
1362 {
1363 	struct i40e_mac_filter *f;
1364 	u64 key;
1365 
1366 	if (!vsi || !macaddr)
1367 		return NULL;
1368 
1369 	f = i40e_find_filter(vsi, macaddr, vlan);
1370 	if (!f) {
1371 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1372 		if (!f)
1373 			return NULL;
1374 
1375 		/* Update the boolean indicating if we need to function in
1376 		 * VLAN mode.
1377 		 */
1378 		if (vlan >= 0)
1379 			vsi->has_vlan_filter = true;
1380 
1381 		ether_addr_copy(f->macaddr, macaddr);
1382 		f->vlan = vlan;
1383 		/* If we're in overflow promisc mode, set the state directly
1384 		 * to failed, so we don't bother to try sending the filter
1385 		 * to the hardware.
1386 		 */
1387 		if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state))
1388 			f->state = I40E_FILTER_FAILED;
1389 		else
1390 			f->state = I40E_FILTER_NEW;
1391 		INIT_HLIST_NODE(&f->hlist);
1392 
1393 		key = i40e_addr_to_hkey(macaddr);
1394 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1395 
1396 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1397 		vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1398 	}
1399 
1400 	/* If we're asked to add a filter that has been marked for removal, it
1401 	 * is safe to simply restore it to active state. __i40e_del_filter
1402 	 * will have simply deleted any filters which were previously marked
1403 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1404 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1405 	 * task, just restore this filter to the ACTIVE state so that the
1406 	 * sync task leaves it in place
1407 	 */
1408 	if (f->state == I40E_FILTER_REMOVE)
1409 		f->state = I40E_FILTER_ACTIVE;
1410 
1411 	return f;
1412 }
1413 
1414 /**
1415  * __i40e_del_filter - Remove a specific filter from the VSI
1416  * @vsi: VSI to remove from
1417  * @f: the filter to remove from the list
1418  *
1419  * This function should be called instead of i40e_del_filter only if you know
1420  * the exact filter you will remove already, such as via i40e_find_filter or
1421  * i40e_find_mac.
1422  *
1423  * NOTE: This function is expected to be called with mac_filter_hash_lock
1424  * being held.
1425  * ANOTHER NOTE: This function MUST be called from within the context of
1426  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1427  * instead of list_for_each_entry().
1428  **/
1429 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1430 {
1431 	if (!f)
1432 		return;
1433 
1434 	/* If the filter was never added to firmware then we can just delete it
1435 	 * directly and we don't want to set the status to remove or else an
1436 	 * admin queue command will unnecessarily fire.
1437 	 */
1438 	if ((f->state == I40E_FILTER_FAILED) ||
1439 	    (f->state == I40E_FILTER_NEW)) {
1440 		hash_del(&f->hlist);
1441 		kfree(f);
1442 	} else {
1443 		f->state = I40E_FILTER_REMOVE;
1444 	}
1445 
1446 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1447 	vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1448 }
1449 
1450 /**
1451  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1452  * @vsi: the VSI to be searched
1453  * @macaddr: the MAC address
1454  * @vlan: the VLAN
1455  *
1456  * NOTE: This function is expected to be called with mac_filter_hash_lock
1457  * being held.
1458  * ANOTHER NOTE: This function MUST be called from within the context of
1459  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1460  * instead of list_for_each_entry().
1461  **/
1462 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1463 {
1464 	struct i40e_mac_filter *f;
1465 
1466 	if (!vsi || !macaddr)
1467 		return;
1468 
1469 	f = i40e_find_filter(vsi, macaddr, vlan);
1470 	__i40e_del_filter(vsi, f);
1471 }
1472 
1473 /**
1474  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1475  * @vsi: the VSI to be searched
1476  * @macaddr: the mac address to be filtered
1477  *
1478  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1479  * go through all the macvlan filters and add a macvlan filter for each
1480  * unique vlan that already exists. If a PVID has been assigned, instead only
1481  * add the macaddr to that VLAN.
1482  *
1483  * Returns last filter added on success, else NULL
1484  **/
1485 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1486 					    const u8 *macaddr)
1487 {
1488 	struct i40e_mac_filter *f, *add = NULL;
1489 	struct hlist_node *h;
1490 	int bkt;
1491 
1492 	if (vsi->info.pvid)
1493 		return i40e_add_filter(vsi, macaddr,
1494 				       le16_to_cpu(vsi->info.pvid));
1495 
1496 	if (!i40e_is_vsi_in_vlan(vsi))
1497 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1498 
1499 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1500 		if (f->state == I40E_FILTER_REMOVE)
1501 			continue;
1502 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1503 		if (!add)
1504 			return NULL;
1505 	}
1506 
1507 	return add;
1508 }
1509 
1510 /**
1511  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1512  * @vsi: the VSI to be searched
1513  * @macaddr: the mac address to be removed
1514  *
1515  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1516  * associated with.
1517  *
1518  * Returns 0 for success, or error
1519  **/
1520 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1521 {
1522 	struct i40e_mac_filter *f;
1523 	struct hlist_node *h;
1524 	bool found = false;
1525 	int bkt;
1526 
1527 	WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1528 	     "Missing mac_filter_hash_lock\n");
1529 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1530 		if (ether_addr_equal(macaddr, f->macaddr)) {
1531 			__i40e_del_filter(vsi, f);
1532 			found = true;
1533 		}
1534 	}
1535 
1536 	if (found)
1537 		return 0;
1538 	else
1539 		return -ENOENT;
1540 }
1541 
1542 /**
1543  * i40e_set_mac - NDO callback to set mac address
1544  * @netdev: network interface device structure
1545  * @p: pointer to an address structure
1546  *
1547  * Returns 0 on success, negative on failure
1548  **/
1549 static int i40e_set_mac(struct net_device *netdev, void *p)
1550 {
1551 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1552 	struct i40e_vsi *vsi = np->vsi;
1553 	struct i40e_pf *pf = vsi->back;
1554 	struct i40e_hw *hw = &pf->hw;
1555 	struct sockaddr *addr = p;
1556 
1557 	if (!is_valid_ether_addr(addr->sa_data))
1558 		return -EADDRNOTAVAIL;
1559 
1560 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1561 		netdev_info(netdev, "already using mac address %pM\n",
1562 			    addr->sa_data);
1563 		return 0;
1564 	}
1565 
1566 	if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
1567 	    test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
1568 		return -EADDRNOTAVAIL;
1569 
1570 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1571 		netdev_info(netdev, "returning to hw mac address %pM\n",
1572 			    hw->mac.addr);
1573 	else
1574 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1575 
1576 	/* Copy the address first, so that we avoid a possible race with
1577 	 * .set_rx_mode(). If we copy after changing the address in the filter
1578 	 * list, we might open ourselves to a narrow race window where
1579 	 * .set_rx_mode could delete our dev_addr filter and prevent traffic
1580 	 * from passing.
1581 	 */
1582 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1583 
1584 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1585 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1586 	i40e_add_mac_filter(vsi, addr->sa_data);
1587 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1588 	if (vsi->type == I40E_VSI_MAIN) {
1589 		i40e_status ret;
1590 
1591 		ret = i40e_aq_mac_address_write(&vsi->back->hw,
1592 						I40E_AQC_WRITE_TYPE_LAA_WOL,
1593 						addr->sa_data, NULL);
1594 		if (ret)
1595 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1596 				    i40e_stat_str(hw, ret),
1597 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1598 	}
1599 
1600 	/* schedule our worker thread which will take care of
1601 	 * applying the new filter changes
1602 	 */
1603 	i40e_service_event_schedule(vsi->back);
1604 	return 0;
1605 }
1606 
1607 /**
1608  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1609  * @vsi: vsi structure
1610  * @seed: RSS hash seed
1611  **/
1612 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1613 			      u8 *lut, u16 lut_size)
1614 {
1615 	struct i40e_pf *pf = vsi->back;
1616 	struct i40e_hw *hw = &pf->hw;
1617 	int ret = 0;
1618 
1619 	if (seed) {
1620 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1621 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1622 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1623 		if (ret) {
1624 			dev_info(&pf->pdev->dev,
1625 				 "Cannot set RSS key, err %s aq_err %s\n",
1626 				 i40e_stat_str(hw, ret),
1627 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1628 			return ret;
1629 		}
1630 	}
1631 	if (lut) {
1632 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1633 
1634 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1635 		if (ret) {
1636 			dev_info(&pf->pdev->dev,
1637 				 "Cannot set RSS lut, err %s aq_err %s\n",
1638 				 i40e_stat_str(hw, ret),
1639 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1640 			return ret;
1641 		}
1642 	}
1643 	return ret;
1644 }
1645 
1646 /**
1647  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1648  * @vsi: VSI structure
1649  **/
1650 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1651 {
1652 	struct i40e_pf *pf = vsi->back;
1653 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1654 	u8 *lut;
1655 	int ret;
1656 
1657 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1658 		return 0;
1659 	if (!vsi->rss_size)
1660 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1661 				      vsi->num_queue_pairs);
1662 	if (!vsi->rss_size)
1663 		return -EINVAL;
1664 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1665 	if (!lut)
1666 		return -ENOMEM;
1667 
1668 	/* Use the user configured hash keys and lookup table if there is one,
1669 	 * otherwise use default
1670 	 */
1671 	if (vsi->rss_lut_user)
1672 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1673 	else
1674 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1675 	if (vsi->rss_hkey_user)
1676 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1677 	else
1678 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1679 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1680 	kfree(lut);
1681 	return ret;
1682 }
1683 
1684 /**
1685  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1686  * @vsi: the VSI being configured,
1687  * @ctxt: VSI context structure
1688  * @enabled_tc: number of traffic classes to enable
1689  *
1690  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1691  **/
1692 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1693 					   struct i40e_vsi_context *ctxt,
1694 					   u8 enabled_tc)
1695 {
1696 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1697 	int i, override_q, pow, num_qps, ret;
1698 	u8 netdev_tc = 0, offset = 0;
1699 
1700 	if (vsi->type != I40E_VSI_MAIN)
1701 		return -EINVAL;
1702 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1703 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1704 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1705 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1706 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1707 
1708 	/* find the next higher power-of-2 of num queue pairs */
1709 	pow = ilog2(num_qps);
1710 	if (!is_power_of_2(num_qps))
1711 		pow++;
1712 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1713 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1714 
1715 	/* Setup queue offset/count for all TCs for given VSI */
1716 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1717 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1718 		/* See if the given TC is enabled for the given VSI */
1719 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1720 			offset = vsi->mqprio_qopt.qopt.offset[i];
1721 			qcount = vsi->mqprio_qopt.qopt.count[i];
1722 			if (qcount > max_qcount)
1723 				max_qcount = qcount;
1724 			vsi->tc_config.tc_info[i].qoffset = offset;
1725 			vsi->tc_config.tc_info[i].qcount = qcount;
1726 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1727 		} else {
1728 			/* TC is not enabled so set the offset to
1729 			 * default queue and allocate one queue
1730 			 * for the given TC.
1731 			 */
1732 			vsi->tc_config.tc_info[i].qoffset = 0;
1733 			vsi->tc_config.tc_info[i].qcount = 1;
1734 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1735 		}
1736 	}
1737 
1738 	/* Set actual Tx/Rx queue pairs */
1739 	vsi->num_queue_pairs = offset + qcount;
1740 
1741 	/* Setup queue TC[0].qmap for given VSI context */
1742 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1743 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1744 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1745 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1746 
1747 	/* Reconfigure RSS for main VSI with max queue count */
1748 	vsi->rss_size = max_qcount;
1749 	ret = i40e_vsi_config_rss(vsi);
1750 	if (ret) {
1751 		dev_info(&vsi->back->pdev->dev,
1752 			 "Failed to reconfig rss for num_queues (%u)\n",
1753 			 max_qcount);
1754 		return ret;
1755 	}
1756 	vsi->reconfig_rss = true;
1757 	dev_dbg(&vsi->back->pdev->dev,
1758 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1759 
1760 	/* Find queue count available for channel VSIs and starting offset
1761 	 * for channel VSIs
1762 	 */
1763 	override_q = vsi->mqprio_qopt.qopt.count[0];
1764 	if (override_q && override_q < vsi->num_queue_pairs) {
1765 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1766 		vsi->next_base_queue = override_q;
1767 	}
1768 	return 0;
1769 }
1770 
1771 /**
1772  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1773  * @vsi: the VSI being setup
1774  * @ctxt: VSI context structure
1775  * @enabled_tc: Enabled TCs bitmap
1776  * @is_add: True if called before Add VSI
1777  *
1778  * Setup VSI queue mapping for enabled traffic classes.
1779  **/
1780 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1781 				     struct i40e_vsi_context *ctxt,
1782 				     u8 enabled_tc,
1783 				     bool is_add)
1784 {
1785 	struct i40e_pf *pf = vsi->back;
1786 	u16 sections = 0;
1787 	u8 netdev_tc = 0;
1788 	u16 numtc = 1;
1789 	u16 qcount;
1790 	u8 offset;
1791 	u16 qmap;
1792 	int i;
1793 	u16 num_tc_qps = 0;
1794 
1795 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1796 	offset = 0;
1797 
1798 	/* Number of queues per enabled TC */
1799 	num_tc_qps = vsi->alloc_queue_pairs;
1800 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1801 		/* Find numtc from enabled TC bitmap */
1802 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1803 			if (enabled_tc & BIT(i)) /* TC is enabled */
1804 				numtc++;
1805 		}
1806 		if (!numtc) {
1807 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1808 			numtc = 1;
1809 		}
1810 		num_tc_qps = num_tc_qps / numtc;
1811 		num_tc_qps = min_t(int, num_tc_qps,
1812 				   i40e_pf_get_max_q_per_tc(pf));
1813 	}
1814 
1815 	vsi->tc_config.numtc = numtc;
1816 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1817 
1818 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1819 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1820 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1821 
1822 	/* Setup queue offset/count for all TCs for given VSI */
1823 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1824 		/* See if the given TC is enabled for the given VSI */
1825 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1826 			/* TC is enabled */
1827 			int pow, num_qps;
1828 
1829 			switch (vsi->type) {
1830 			case I40E_VSI_MAIN:
1831 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1832 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1833 				    vsi->tc_config.enabled_tc != 1) {
1834 					qcount = min_t(int, pf->alloc_rss_size,
1835 						       num_tc_qps);
1836 					break;
1837 				}
1838 			case I40E_VSI_FDIR:
1839 			case I40E_VSI_SRIOV:
1840 			case I40E_VSI_VMDQ2:
1841 			default:
1842 				qcount = num_tc_qps;
1843 				WARN_ON(i != 0);
1844 				break;
1845 			}
1846 			vsi->tc_config.tc_info[i].qoffset = offset;
1847 			vsi->tc_config.tc_info[i].qcount = qcount;
1848 
1849 			/* find the next higher power-of-2 of num queue pairs */
1850 			num_qps = qcount;
1851 			pow = 0;
1852 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1853 				pow++;
1854 				num_qps >>= 1;
1855 			}
1856 
1857 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1858 			qmap =
1859 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1860 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1861 
1862 			offset += qcount;
1863 		} else {
1864 			/* TC is not enabled so set the offset to
1865 			 * default queue and allocate one queue
1866 			 * for the given TC.
1867 			 */
1868 			vsi->tc_config.tc_info[i].qoffset = 0;
1869 			vsi->tc_config.tc_info[i].qcount = 1;
1870 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1871 
1872 			qmap = 0;
1873 		}
1874 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1875 	}
1876 
1877 	/* Set actual Tx/Rx queue pairs */
1878 	vsi->num_queue_pairs = offset;
1879 	if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1880 		if (vsi->req_queue_pairs > 0)
1881 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1882 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1883 			vsi->num_queue_pairs = pf->num_lan_msix;
1884 	}
1885 
1886 	/* Scheduler section valid can only be set for ADD VSI */
1887 	if (is_add) {
1888 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1889 
1890 		ctxt->info.up_enable_bits = enabled_tc;
1891 	}
1892 	if (vsi->type == I40E_VSI_SRIOV) {
1893 		ctxt->info.mapping_flags |=
1894 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1895 		for (i = 0; i < vsi->num_queue_pairs; i++)
1896 			ctxt->info.queue_mapping[i] =
1897 					       cpu_to_le16(vsi->base_queue + i);
1898 	} else {
1899 		ctxt->info.mapping_flags |=
1900 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1901 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1902 	}
1903 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1904 }
1905 
1906 /**
1907  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1908  * @netdev: the netdevice
1909  * @addr: address to add
1910  *
1911  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1912  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1913  */
1914 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1915 {
1916 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1917 	struct i40e_vsi *vsi = np->vsi;
1918 
1919 	if (i40e_add_mac_filter(vsi, addr))
1920 		return 0;
1921 	else
1922 		return -ENOMEM;
1923 }
1924 
1925 /**
1926  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1927  * @netdev: the netdevice
1928  * @addr: address to add
1929  *
1930  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1931  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1932  */
1933 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1934 {
1935 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1936 	struct i40e_vsi *vsi = np->vsi;
1937 
1938 	/* Under some circumstances, we might receive a request to delete
1939 	 * our own device address from our uc list. Because we store the
1940 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1941 	 * such requests and not delete our device address from this list.
1942 	 */
1943 	if (ether_addr_equal(addr, netdev->dev_addr))
1944 		return 0;
1945 
1946 	i40e_del_mac_filter(vsi, addr);
1947 
1948 	return 0;
1949 }
1950 
1951 /**
1952  * i40e_set_rx_mode - NDO callback to set the netdev filters
1953  * @netdev: network interface device structure
1954  **/
1955 static void i40e_set_rx_mode(struct net_device *netdev)
1956 {
1957 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1958 	struct i40e_vsi *vsi = np->vsi;
1959 
1960 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1961 
1962 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1963 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1964 
1965 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1966 
1967 	/* check for other flag changes */
1968 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
1969 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1970 		vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1971 	}
1972 }
1973 
1974 /**
1975  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1976  * @vsi: Pointer to VSI struct
1977  * @from: Pointer to list which contains MAC filter entries - changes to
1978  *        those entries needs to be undone.
1979  *
1980  * MAC filter entries from this list were slated for deletion.
1981  **/
1982 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1983 					 struct hlist_head *from)
1984 {
1985 	struct i40e_mac_filter *f;
1986 	struct hlist_node *h;
1987 
1988 	hlist_for_each_entry_safe(f, h, from, hlist) {
1989 		u64 key = i40e_addr_to_hkey(f->macaddr);
1990 
1991 		/* Move the element back into MAC filter list*/
1992 		hlist_del(&f->hlist);
1993 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1994 	}
1995 }
1996 
1997 /**
1998  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1999  * @vsi: Pointer to vsi struct
2000  * @from: Pointer to list which contains MAC filter entries - changes to
2001  *        those entries needs to be undone.
2002  *
2003  * MAC filter entries from this list were slated for addition.
2004  **/
2005 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2006 					 struct hlist_head *from)
2007 {
2008 	struct i40e_new_mac_filter *new;
2009 	struct hlist_node *h;
2010 
2011 	hlist_for_each_entry_safe(new, h, from, hlist) {
2012 		/* We can simply free the wrapper structure */
2013 		hlist_del(&new->hlist);
2014 		kfree(new);
2015 	}
2016 }
2017 
2018 /**
2019  * i40e_next_entry - Get the next non-broadcast filter from a list
2020  * @next: pointer to filter in list
2021  *
2022  * Returns the next non-broadcast filter in the list. Required so that we
2023  * ignore broadcast filters within the list, since these are not handled via
2024  * the normal firmware update path.
2025  */
2026 static
2027 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2028 {
2029 	hlist_for_each_entry_continue(next, hlist) {
2030 		if (!is_broadcast_ether_addr(next->f->macaddr))
2031 			return next;
2032 	}
2033 
2034 	return NULL;
2035 }
2036 
2037 /**
2038  * i40e_update_filter_state - Update filter state based on return data
2039  * from firmware
2040  * @count: Number of filters added
2041  * @add_list: return data from fw
2042  * @head: pointer to first filter in current batch
2043  *
2044  * MAC filter entries from list were slated to be added to device. Returns
2045  * number of successful filters. Note that 0 does NOT mean success!
2046  **/
2047 static int
2048 i40e_update_filter_state(int count,
2049 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2050 			 struct i40e_new_mac_filter *add_head)
2051 {
2052 	int retval = 0;
2053 	int i;
2054 
2055 	for (i = 0; i < count; i++) {
2056 		/* Always check status of each filter. We don't need to check
2057 		 * the firmware return status because we pre-set the filter
2058 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2059 		 * request to the adminq. Thus, if it no longer matches then
2060 		 * we know the filter is active.
2061 		 */
2062 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2063 			add_head->state = I40E_FILTER_FAILED;
2064 		} else {
2065 			add_head->state = I40E_FILTER_ACTIVE;
2066 			retval++;
2067 		}
2068 
2069 		add_head = i40e_next_filter(add_head);
2070 		if (!add_head)
2071 			break;
2072 	}
2073 
2074 	return retval;
2075 }
2076 
2077 /**
2078  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2079  * @vsi: ptr to the VSI
2080  * @vsi_name: name to display in messages
2081  * @list: the list of filters to send to firmware
2082  * @num_del: the number of filters to delete
2083  * @retval: Set to -EIO on failure to delete
2084  *
2085  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2086  * *retval instead of a return value so that success does not force ret_val to
2087  * be set to 0. This ensures that a sequence of calls to this function
2088  * preserve the previous value of *retval on successful delete.
2089  */
2090 static
2091 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2092 			  struct i40e_aqc_remove_macvlan_element_data *list,
2093 			  int num_del, int *retval)
2094 {
2095 	struct i40e_hw *hw = &vsi->back->hw;
2096 	i40e_status aq_ret;
2097 	int aq_err;
2098 
2099 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2100 	aq_err = hw->aq.asq_last_status;
2101 
2102 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2103 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2104 		*retval = -EIO;
2105 		dev_info(&vsi->back->pdev->dev,
2106 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2107 			 vsi_name, i40e_stat_str(hw, aq_ret),
2108 			 i40e_aq_str(hw, aq_err));
2109 	}
2110 }
2111 
2112 /**
2113  * i40e_aqc_add_filters - Request firmware to add a set of filters
2114  * @vsi: ptr to the VSI
2115  * @vsi_name: name to display in messages
2116  * @list: the list of filters to send to firmware
2117  * @add_head: Position in the add hlist
2118  * @num_add: the number of filters to add
2119  * @promisc_change: set to true on exit if promiscuous mode was forced on
2120  *
2121  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2122  * promisc_changed to true if the firmware has run out of space for more
2123  * filters.
2124  */
2125 static
2126 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2127 			  struct i40e_aqc_add_macvlan_element_data *list,
2128 			  struct i40e_new_mac_filter *add_head,
2129 			  int num_add, bool *promisc_changed)
2130 {
2131 	struct i40e_hw *hw = &vsi->back->hw;
2132 	int aq_err, fcnt;
2133 
2134 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2135 	aq_err = hw->aq.asq_last_status;
2136 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2137 
2138 	if (fcnt != num_add) {
2139 		*promisc_changed = true;
2140 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2141 		dev_warn(&vsi->back->pdev->dev,
2142 			 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2143 			 i40e_aq_str(hw, aq_err),
2144 			 vsi_name);
2145 	}
2146 }
2147 
2148 /**
2149  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2150  * @vsi: pointer to the VSI
2151  * @f: filter data
2152  *
2153  * This function sets or clears the promiscuous broadcast flags for VLAN
2154  * filters in order to properly receive broadcast frames. Assumes that only
2155  * broadcast filters are passed.
2156  *
2157  * Returns status indicating success or failure;
2158  **/
2159 static i40e_status
2160 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2161 			  struct i40e_mac_filter *f)
2162 {
2163 	bool enable = f->state == I40E_FILTER_NEW;
2164 	struct i40e_hw *hw = &vsi->back->hw;
2165 	i40e_status aq_ret;
2166 
2167 	if (f->vlan == I40E_VLAN_ANY) {
2168 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2169 						   vsi->seid,
2170 						   enable,
2171 						   NULL);
2172 	} else {
2173 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2174 							    vsi->seid,
2175 							    enable,
2176 							    f->vlan,
2177 							    NULL);
2178 	}
2179 
2180 	if (aq_ret)
2181 		dev_warn(&vsi->back->pdev->dev,
2182 			 "Error %s setting broadcast promiscuous mode on %s\n",
2183 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2184 			 vsi_name);
2185 
2186 	return aq_ret;
2187 }
2188 
2189 /**
2190  * i40e_set_promiscuous - set promiscuous mode
2191  * @pf: board private structure
2192  * @promisc: promisc on or off
2193  *
2194  * There are different ways of setting promiscuous mode on a PF depending on
2195  * what state/environment we're in.  This identifies and sets it appropriately.
2196  * Returns 0 on success.
2197  **/
2198 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2199 {
2200 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2201 	struct i40e_hw *hw = &pf->hw;
2202 	i40e_status aq_ret;
2203 
2204 	if (vsi->type == I40E_VSI_MAIN &&
2205 	    pf->lan_veb != I40E_NO_VEB &&
2206 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2207 		/* set defport ON for Main VSI instead of true promisc
2208 		 * this way we will get all unicast/multicast and VLAN
2209 		 * promisc behavior but will not get VF or VMDq traffic
2210 		 * replicated on the Main VSI.
2211 		 */
2212 		if (promisc)
2213 			aq_ret = i40e_aq_set_default_vsi(hw,
2214 							 vsi->seid,
2215 							 NULL);
2216 		else
2217 			aq_ret = i40e_aq_clear_default_vsi(hw,
2218 							   vsi->seid,
2219 							   NULL);
2220 		if (aq_ret) {
2221 			dev_info(&pf->pdev->dev,
2222 				 "Set default VSI failed, err %s, aq_err %s\n",
2223 				 i40e_stat_str(hw, aq_ret),
2224 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2225 		}
2226 	} else {
2227 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2228 						  hw,
2229 						  vsi->seid,
2230 						  promisc, NULL,
2231 						  true);
2232 		if (aq_ret) {
2233 			dev_info(&pf->pdev->dev,
2234 				 "set unicast promisc failed, err %s, aq_err %s\n",
2235 				 i40e_stat_str(hw, aq_ret),
2236 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2237 		}
2238 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2239 						  hw,
2240 						  vsi->seid,
2241 						  promisc, NULL);
2242 		if (aq_ret) {
2243 			dev_info(&pf->pdev->dev,
2244 				 "set multicast promisc failed, err %s, aq_err %s\n",
2245 				 i40e_stat_str(hw, aq_ret),
2246 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2247 		}
2248 	}
2249 
2250 	if (!aq_ret)
2251 		pf->cur_promisc = promisc;
2252 
2253 	return aq_ret;
2254 }
2255 
2256 /**
2257  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2258  * @vsi: ptr to the VSI
2259  *
2260  * Push any outstanding VSI filter changes through the AdminQ.
2261  *
2262  * Returns 0 or error value
2263  **/
2264 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2265 {
2266 	struct hlist_head tmp_add_list, tmp_del_list;
2267 	struct i40e_mac_filter *f;
2268 	struct i40e_new_mac_filter *new, *add_head = NULL;
2269 	struct i40e_hw *hw = &vsi->back->hw;
2270 	unsigned int failed_filters = 0;
2271 	unsigned int vlan_filters = 0;
2272 	bool promisc_changed = false;
2273 	char vsi_name[16] = "PF";
2274 	int filter_list_len = 0;
2275 	i40e_status aq_ret = 0;
2276 	u32 changed_flags = 0;
2277 	struct hlist_node *h;
2278 	struct i40e_pf *pf;
2279 	int num_add = 0;
2280 	int num_del = 0;
2281 	int retval = 0;
2282 	u16 cmd_flags;
2283 	int list_size;
2284 	int bkt;
2285 
2286 	/* empty array typed pointers, kcalloc later */
2287 	struct i40e_aqc_add_macvlan_element_data *add_list;
2288 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2289 
2290 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2291 		usleep_range(1000, 2000);
2292 	pf = vsi->back;
2293 
2294 	if (vsi->netdev) {
2295 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2296 		vsi->current_netdev_flags = vsi->netdev->flags;
2297 	}
2298 
2299 	INIT_HLIST_HEAD(&tmp_add_list);
2300 	INIT_HLIST_HEAD(&tmp_del_list);
2301 
2302 	if (vsi->type == I40E_VSI_SRIOV)
2303 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2304 	else if (vsi->type != I40E_VSI_MAIN)
2305 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2306 
2307 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2308 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2309 
2310 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2311 		/* Create a list of filters to delete. */
2312 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2313 			if (f->state == I40E_FILTER_REMOVE) {
2314 				/* Move the element into temporary del_list */
2315 				hash_del(&f->hlist);
2316 				hlist_add_head(&f->hlist, &tmp_del_list);
2317 
2318 				/* Avoid counting removed filters */
2319 				continue;
2320 			}
2321 			if (f->state == I40E_FILTER_NEW) {
2322 				/* Create a temporary i40e_new_mac_filter */
2323 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2324 				if (!new)
2325 					goto err_no_memory_locked;
2326 
2327 				/* Store pointer to the real filter */
2328 				new->f = f;
2329 				new->state = f->state;
2330 
2331 				/* Add it to the hash list */
2332 				hlist_add_head(&new->hlist, &tmp_add_list);
2333 			}
2334 
2335 			/* Count the number of active (current and new) VLAN
2336 			 * filters we have now. Does not count filters which
2337 			 * are marked for deletion.
2338 			 */
2339 			if (f->vlan > 0)
2340 				vlan_filters++;
2341 		}
2342 
2343 		retval = i40e_correct_mac_vlan_filters(vsi,
2344 						       &tmp_add_list,
2345 						       &tmp_del_list,
2346 						       vlan_filters);
2347 		if (retval)
2348 			goto err_no_memory_locked;
2349 
2350 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2351 	}
2352 
2353 	/* Now process 'del_list' outside the lock */
2354 	if (!hlist_empty(&tmp_del_list)) {
2355 		filter_list_len = hw->aq.asq_buf_size /
2356 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2357 		list_size = filter_list_len *
2358 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2359 		del_list = kzalloc(list_size, GFP_ATOMIC);
2360 		if (!del_list)
2361 			goto err_no_memory;
2362 
2363 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2364 			cmd_flags = 0;
2365 
2366 			/* handle broadcast filters by updating the broadcast
2367 			 * promiscuous flag and release filter list.
2368 			 */
2369 			if (is_broadcast_ether_addr(f->macaddr)) {
2370 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2371 
2372 				hlist_del(&f->hlist);
2373 				kfree(f);
2374 				continue;
2375 			}
2376 
2377 			/* add to delete list */
2378 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2379 			if (f->vlan == I40E_VLAN_ANY) {
2380 				del_list[num_del].vlan_tag = 0;
2381 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2382 			} else {
2383 				del_list[num_del].vlan_tag =
2384 					cpu_to_le16((u16)(f->vlan));
2385 			}
2386 
2387 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2388 			del_list[num_del].flags = cmd_flags;
2389 			num_del++;
2390 
2391 			/* flush a full buffer */
2392 			if (num_del == filter_list_len) {
2393 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2394 						     num_del, &retval);
2395 				memset(del_list, 0, list_size);
2396 				num_del = 0;
2397 			}
2398 			/* Release memory for MAC filter entries which were
2399 			 * synced up with HW.
2400 			 */
2401 			hlist_del(&f->hlist);
2402 			kfree(f);
2403 		}
2404 
2405 		if (num_del) {
2406 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2407 					     num_del, &retval);
2408 		}
2409 
2410 		kfree(del_list);
2411 		del_list = NULL;
2412 	}
2413 
2414 	if (!hlist_empty(&tmp_add_list)) {
2415 		/* Do all the adds now. */
2416 		filter_list_len = hw->aq.asq_buf_size /
2417 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2418 		list_size = filter_list_len *
2419 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2420 		add_list = kzalloc(list_size, GFP_ATOMIC);
2421 		if (!add_list)
2422 			goto err_no_memory;
2423 
2424 		num_add = 0;
2425 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2426 			if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
2427 				     vsi->state)) {
2428 				new->state = I40E_FILTER_FAILED;
2429 				continue;
2430 			}
2431 
2432 			/* handle broadcast filters by updating the broadcast
2433 			 * promiscuous flag instead of adding a MAC filter.
2434 			 */
2435 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2436 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2437 							      new->f))
2438 					new->state = I40E_FILTER_FAILED;
2439 				else
2440 					new->state = I40E_FILTER_ACTIVE;
2441 				continue;
2442 			}
2443 
2444 			/* add to add array */
2445 			if (num_add == 0)
2446 				add_head = new;
2447 			cmd_flags = 0;
2448 			ether_addr_copy(add_list[num_add].mac_addr,
2449 					new->f->macaddr);
2450 			if (new->f->vlan == I40E_VLAN_ANY) {
2451 				add_list[num_add].vlan_tag = 0;
2452 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2453 			} else {
2454 				add_list[num_add].vlan_tag =
2455 					cpu_to_le16((u16)(new->f->vlan));
2456 			}
2457 			add_list[num_add].queue_number = 0;
2458 			/* set invalid match method for later detection */
2459 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2460 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2461 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2462 			num_add++;
2463 
2464 			/* flush a full buffer */
2465 			if (num_add == filter_list_len) {
2466 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2467 						     add_head, num_add,
2468 						     &promisc_changed);
2469 				memset(add_list, 0, list_size);
2470 				num_add = 0;
2471 			}
2472 		}
2473 		if (num_add) {
2474 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2475 					     num_add, &promisc_changed);
2476 		}
2477 		/* Now move all of the filters from the temp add list back to
2478 		 * the VSI's list.
2479 		 */
2480 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2481 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2482 			/* Only update the state if we're still NEW */
2483 			if (new->f->state == I40E_FILTER_NEW)
2484 				new->f->state = new->state;
2485 			hlist_del(&new->hlist);
2486 			kfree(new);
2487 		}
2488 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2489 		kfree(add_list);
2490 		add_list = NULL;
2491 	}
2492 
2493 	/* Determine the number of active and failed filters. */
2494 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2495 	vsi->active_filters = 0;
2496 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2497 		if (f->state == I40E_FILTER_ACTIVE)
2498 			vsi->active_filters++;
2499 		else if (f->state == I40E_FILTER_FAILED)
2500 			failed_filters++;
2501 	}
2502 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2503 
2504 	/* If promiscuous mode has changed, we need to calculate a new
2505 	 * threshold for when we are safe to exit
2506 	 */
2507 	if (promisc_changed)
2508 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2509 
2510 	/* Check if we are able to exit overflow promiscuous mode. We can
2511 	 * safely exit if we didn't just enter, we no longer have any failed
2512 	 * filters, and we have reduced filters below the threshold value.
2513 	 */
2514 	if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
2515 	    !promisc_changed && !failed_filters &&
2516 	    (vsi->active_filters < vsi->promisc_threshold)) {
2517 		dev_info(&pf->pdev->dev,
2518 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2519 			 vsi_name);
2520 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2521 		promisc_changed = true;
2522 		vsi->promisc_threshold = 0;
2523 	}
2524 
2525 	/* if the VF is not trusted do not do promisc */
2526 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2527 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2528 		goto out;
2529 	}
2530 
2531 	/* check for changes in promiscuous modes */
2532 	if (changed_flags & IFF_ALLMULTI) {
2533 		bool cur_multipromisc;
2534 
2535 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2536 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2537 							       vsi->seid,
2538 							       cur_multipromisc,
2539 							       NULL);
2540 		if (aq_ret) {
2541 			retval = i40e_aq_rc_to_posix(aq_ret,
2542 						     hw->aq.asq_last_status);
2543 			dev_info(&pf->pdev->dev,
2544 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2545 				 vsi_name,
2546 				 i40e_stat_str(hw, aq_ret),
2547 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2548 		}
2549 	}
2550 
2551 	if ((changed_flags & IFF_PROMISC) || promisc_changed) {
2552 		bool cur_promisc;
2553 
2554 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2555 			       test_bit(__I40E_VSI_OVERFLOW_PROMISC,
2556 					vsi->state));
2557 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2558 		if (aq_ret) {
2559 			retval = i40e_aq_rc_to_posix(aq_ret,
2560 						     hw->aq.asq_last_status);
2561 			dev_info(&pf->pdev->dev,
2562 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2563 				 cur_promisc ? "on" : "off",
2564 				 vsi_name,
2565 				 i40e_stat_str(hw, aq_ret),
2566 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2567 		}
2568 	}
2569 out:
2570 	/* if something went wrong then set the changed flag so we try again */
2571 	if (retval)
2572 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2573 
2574 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2575 	return retval;
2576 
2577 err_no_memory:
2578 	/* Restore elements on the temporary add and delete lists */
2579 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2580 err_no_memory_locked:
2581 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2582 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2583 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2584 
2585 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2586 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2587 	return -ENOMEM;
2588 }
2589 
2590 /**
2591  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2592  * @pf: board private structure
2593  **/
2594 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2595 {
2596 	int v;
2597 
2598 	if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2599 		return;
2600 	pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2601 
2602 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2603 		if (pf->vsi[v] &&
2604 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2605 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2606 
2607 			if (ret) {
2608 				/* come back and try again later */
2609 				pf->flags |= I40E_FLAG_FILTER_SYNC;
2610 				break;
2611 			}
2612 		}
2613 	}
2614 }
2615 
2616 /**
2617  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2618  * @vsi: the vsi
2619  **/
2620 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2621 {
2622 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2623 		return I40E_RXBUFFER_2048;
2624 	else
2625 		return I40E_RXBUFFER_3072;
2626 }
2627 
2628 /**
2629  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2630  * @netdev: network interface device structure
2631  * @new_mtu: new value for maximum frame size
2632  *
2633  * Returns 0 on success, negative on failure
2634  **/
2635 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2636 {
2637 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2638 	struct i40e_vsi *vsi = np->vsi;
2639 	struct i40e_pf *pf = vsi->back;
2640 
2641 	if (i40e_enabled_xdp_vsi(vsi)) {
2642 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2643 
2644 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2645 			return -EINVAL;
2646 	}
2647 
2648 	netdev_info(netdev, "changing MTU from %d to %d\n",
2649 		    netdev->mtu, new_mtu);
2650 	netdev->mtu = new_mtu;
2651 	if (netif_running(netdev))
2652 		i40e_vsi_reinit_locked(vsi);
2653 	pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2654 		      I40E_FLAG_CLIENT_L2_CHANGE);
2655 	return 0;
2656 }
2657 
2658 /**
2659  * i40e_ioctl - Access the hwtstamp interface
2660  * @netdev: network interface device structure
2661  * @ifr: interface request data
2662  * @cmd: ioctl command
2663  **/
2664 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2665 {
2666 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2667 	struct i40e_pf *pf = np->vsi->back;
2668 
2669 	switch (cmd) {
2670 	case SIOCGHWTSTAMP:
2671 		return i40e_ptp_get_ts_config(pf, ifr);
2672 	case SIOCSHWTSTAMP:
2673 		return i40e_ptp_set_ts_config(pf, ifr);
2674 	default:
2675 		return -EOPNOTSUPP;
2676 	}
2677 }
2678 
2679 /**
2680  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2681  * @vsi: the vsi being adjusted
2682  **/
2683 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2684 {
2685 	struct i40e_vsi_context ctxt;
2686 	i40e_status ret;
2687 
2688 	if ((vsi->info.valid_sections &
2689 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2690 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2691 		return;  /* already enabled */
2692 
2693 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2694 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2695 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2696 
2697 	ctxt.seid = vsi->seid;
2698 	ctxt.info = vsi->info;
2699 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2700 	if (ret) {
2701 		dev_info(&vsi->back->pdev->dev,
2702 			 "update vlan stripping failed, err %s aq_err %s\n",
2703 			 i40e_stat_str(&vsi->back->hw, ret),
2704 			 i40e_aq_str(&vsi->back->hw,
2705 				     vsi->back->hw.aq.asq_last_status));
2706 	}
2707 }
2708 
2709 /**
2710  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2711  * @vsi: the vsi being adjusted
2712  **/
2713 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2714 {
2715 	struct i40e_vsi_context ctxt;
2716 	i40e_status ret;
2717 
2718 	if ((vsi->info.valid_sections &
2719 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2720 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2721 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2722 		return;  /* already disabled */
2723 
2724 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2725 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2726 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2727 
2728 	ctxt.seid = vsi->seid;
2729 	ctxt.info = vsi->info;
2730 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2731 	if (ret) {
2732 		dev_info(&vsi->back->pdev->dev,
2733 			 "update vlan stripping failed, err %s aq_err %s\n",
2734 			 i40e_stat_str(&vsi->back->hw, ret),
2735 			 i40e_aq_str(&vsi->back->hw,
2736 				     vsi->back->hw.aq.asq_last_status));
2737 	}
2738 }
2739 
2740 /**
2741  * i40e_vlan_rx_register - Setup or shutdown vlan offload
2742  * @netdev: network interface to be adjusted
2743  * @features: netdev features to test if VLAN offload is enabled or not
2744  **/
2745 static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2746 {
2747 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2748 	struct i40e_vsi *vsi = np->vsi;
2749 
2750 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
2751 		i40e_vlan_stripping_enable(vsi);
2752 	else
2753 		i40e_vlan_stripping_disable(vsi);
2754 }
2755 
2756 /**
2757  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2758  * @vsi: the vsi being configured
2759  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2760  *
2761  * This is a helper function for adding a new MAC/VLAN filter with the
2762  * specified VLAN for each existing MAC address already in the hash table.
2763  * This function does *not* perform any accounting to update filters based on
2764  * VLAN mode.
2765  *
2766  * NOTE: this function expects to be called while under the
2767  * mac_filter_hash_lock
2768  **/
2769 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2770 {
2771 	struct i40e_mac_filter *f, *add_f;
2772 	struct hlist_node *h;
2773 	int bkt;
2774 
2775 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2776 		if (f->state == I40E_FILTER_REMOVE)
2777 			continue;
2778 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2779 		if (!add_f) {
2780 			dev_info(&vsi->back->pdev->dev,
2781 				 "Could not add vlan filter %d for %pM\n",
2782 				 vid, f->macaddr);
2783 			return -ENOMEM;
2784 		}
2785 	}
2786 
2787 	return 0;
2788 }
2789 
2790 /**
2791  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2792  * @vsi: the VSI being configured
2793  * @vid: VLAN id to be added
2794  **/
2795 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2796 {
2797 	int err;
2798 
2799 	if (vsi->info.pvid)
2800 		return -EINVAL;
2801 
2802 	/* The network stack will attempt to add VID=0, with the intention to
2803 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2804 	 * these packets by default when configured to receive untagged
2805 	 * packets, so we don't need to add a filter for this case.
2806 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2807 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2808 	 * Thus, we do not want to actually add a filter for VID=0
2809 	 */
2810 	if (!vid)
2811 		return 0;
2812 
2813 	/* Locked once because all functions invoked below iterates list*/
2814 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2815 	err = i40e_add_vlan_all_mac(vsi, vid);
2816 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2817 	if (err)
2818 		return err;
2819 
2820 	/* schedule our worker thread which will take care of
2821 	 * applying the new filter changes
2822 	 */
2823 	i40e_service_event_schedule(vsi->back);
2824 	return 0;
2825 }
2826 
2827 /**
2828  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2829  * @vsi: the vsi being configured
2830  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2831  *
2832  * This function should be used to remove all VLAN filters which match the
2833  * given VID. It does not schedule the service event and does not take the
2834  * mac_filter_hash_lock so it may be combined with other operations under
2835  * a single invocation of the mac_filter_hash_lock.
2836  *
2837  * NOTE: this function expects to be called while under the
2838  * mac_filter_hash_lock
2839  */
2840 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2841 {
2842 	struct i40e_mac_filter *f;
2843 	struct hlist_node *h;
2844 	int bkt;
2845 
2846 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2847 		if (f->vlan == vid)
2848 			__i40e_del_filter(vsi, f);
2849 	}
2850 }
2851 
2852 /**
2853  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2854  * @vsi: the VSI being configured
2855  * @vid: VLAN id to be removed
2856  **/
2857 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2858 {
2859 	if (!vid || vsi->info.pvid)
2860 		return;
2861 
2862 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2863 	i40e_rm_vlan_all_mac(vsi, vid);
2864 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2865 
2866 	/* schedule our worker thread which will take care of
2867 	 * applying the new filter changes
2868 	 */
2869 	i40e_service_event_schedule(vsi->back);
2870 }
2871 
2872 /**
2873  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2874  * @netdev: network interface to be adjusted
2875  * @vid: vlan id to be added
2876  *
2877  * net_device_ops implementation for adding vlan ids
2878  **/
2879 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2880 				__always_unused __be16 proto, u16 vid)
2881 {
2882 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2883 	struct i40e_vsi *vsi = np->vsi;
2884 	int ret = 0;
2885 
2886 	if (vid >= VLAN_N_VID)
2887 		return -EINVAL;
2888 
2889 	ret = i40e_vsi_add_vlan(vsi, vid);
2890 	if (!ret)
2891 		set_bit(vid, vsi->active_vlans);
2892 
2893 	return ret;
2894 }
2895 
2896 /**
2897  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2898  * @netdev: network interface to be adjusted
2899  * @vid: vlan id to be removed
2900  *
2901  * net_device_ops implementation for removing vlan ids
2902  **/
2903 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2904 				 __always_unused __be16 proto, u16 vid)
2905 {
2906 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2907 	struct i40e_vsi *vsi = np->vsi;
2908 
2909 	/* return code is ignored as there is nothing a user
2910 	 * can do about failure to remove and a log message was
2911 	 * already printed from the other function
2912 	 */
2913 	i40e_vsi_kill_vlan(vsi, vid);
2914 
2915 	clear_bit(vid, vsi->active_vlans);
2916 
2917 	return 0;
2918 }
2919 
2920 /**
2921  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2922  * @vsi: the vsi being brought back up
2923  **/
2924 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2925 {
2926 	u16 vid;
2927 
2928 	if (!vsi->netdev)
2929 		return;
2930 
2931 	i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2932 
2933 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2934 		i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2935 				     vid);
2936 }
2937 
2938 /**
2939  * i40e_vsi_add_pvid - Add pvid for the VSI
2940  * @vsi: the vsi being adjusted
2941  * @vid: the vlan id to set as a PVID
2942  **/
2943 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2944 {
2945 	struct i40e_vsi_context ctxt;
2946 	i40e_status ret;
2947 
2948 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2949 	vsi->info.pvid = cpu_to_le16(vid);
2950 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2951 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2952 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2953 
2954 	ctxt.seid = vsi->seid;
2955 	ctxt.info = vsi->info;
2956 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2957 	if (ret) {
2958 		dev_info(&vsi->back->pdev->dev,
2959 			 "add pvid failed, err %s aq_err %s\n",
2960 			 i40e_stat_str(&vsi->back->hw, ret),
2961 			 i40e_aq_str(&vsi->back->hw,
2962 				     vsi->back->hw.aq.asq_last_status));
2963 		return -ENOENT;
2964 	}
2965 
2966 	return 0;
2967 }
2968 
2969 /**
2970  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2971  * @vsi: the vsi being adjusted
2972  *
2973  * Just use the vlan_rx_register() service to put it back to normal
2974  **/
2975 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2976 {
2977 	i40e_vlan_stripping_disable(vsi);
2978 
2979 	vsi->info.pvid = 0;
2980 }
2981 
2982 /**
2983  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2984  * @vsi: ptr to the VSI
2985  *
2986  * If this function returns with an error, then it's possible one or
2987  * more of the rings is populated (while the rest are not).  It is the
2988  * callers duty to clean those orphaned rings.
2989  *
2990  * Return 0 on success, negative on failure
2991  **/
2992 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2993 {
2994 	int i, err = 0;
2995 
2996 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2997 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
2998 
2999 	if (!i40e_enabled_xdp_vsi(vsi))
3000 		return err;
3001 
3002 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3003 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3004 
3005 	return err;
3006 }
3007 
3008 /**
3009  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3010  * @vsi: ptr to the VSI
3011  *
3012  * Free VSI's transmit software resources
3013  **/
3014 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3015 {
3016 	int i;
3017 
3018 	if (vsi->tx_rings) {
3019 		for (i = 0; i < vsi->num_queue_pairs; i++)
3020 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3021 				i40e_free_tx_resources(vsi->tx_rings[i]);
3022 	}
3023 
3024 	if (vsi->xdp_rings) {
3025 		for (i = 0; i < vsi->num_queue_pairs; i++)
3026 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3027 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3028 	}
3029 }
3030 
3031 /**
3032  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3033  * @vsi: ptr to the VSI
3034  *
3035  * If this function returns with an error, then it's possible one or
3036  * more of the rings is populated (while the rest are not).  It is the
3037  * callers duty to clean those orphaned rings.
3038  *
3039  * Return 0 on success, negative on failure
3040  **/
3041 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3042 {
3043 	int i, err = 0;
3044 
3045 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3046 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3047 	return err;
3048 }
3049 
3050 /**
3051  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3052  * @vsi: ptr to the VSI
3053  *
3054  * Free all receive software resources
3055  **/
3056 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3057 {
3058 	int i;
3059 
3060 	if (!vsi->rx_rings)
3061 		return;
3062 
3063 	for (i = 0; i < vsi->num_queue_pairs; i++)
3064 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3065 			i40e_free_rx_resources(vsi->rx_rings[i]);
3066 }
3067 
3068 /**
3069  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3070  * @ring: The Tx ring to configure
3071  *
3072  * This enables/disables XPS for a given Tx descriptor ring
3073  * based on the TCs enabled for the VSI that ring belongs to.
3074  **/
3075 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3076 {
3077 	int cpu;
3078 
3079 	if (!ring->q_vector || !ring->netdev || ring->ch)
3080 		return;
3081 
3082 	/* We only initialize XPS once, so as not to overwrite user settings */
3083 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3084 		return;
3085 
3086 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3087 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3088 			    ring->queue_index);
3089 }
3090 
3091 /**
3092  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3093  * @ring: The Tx ring to configure
3094  *
3095  * Configure the Tx descriptor ring in the HMC context.
3096  **/
3097 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3098 {
3099 	struct i40e_vsi *vsi = ring->vsi;
3100 	u16 pf_q = vsi->base_queue + ring->queue_index;
3101 	struct i40e_hw *hw = &vsi->back->hw;
3102 	struct i40e_hmc_obj_txq tx_ctx;
3103 	i40e_status err = 0;
3104 	u32 qtx_ctl = 0;
3105 
3106 	/* some ATR related tx ring init */
3107 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3108 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3109 		ring->atr_count = 0;
3110 	} else {
3111 		ring->atr_sample_rate = 0;
3112 	}
3113 
3114 	/* configure XPS */
3115 	i40e_config_xps_tx_ring(ring);
3116 
3117 	/* clear the context structure first */
3118 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3119 
3120 	tx_ctx.new_context = 1;
3121 	tx_ctx.base = (ring->dma / 128);
3122 	tx_ctx.qlen = ring->count;
3123 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3124 					       I40E_FLAG_FD_ATR_ENABLED));
3125 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3126 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3127 	if (vsi->type != I40E_VSI_FDIR)
3128 		tx_ctx.head_wb_ena = 1;
3129 	tx_ctx.head_wb_addr = ring->dma +
3130 			      (ring->count * sizeof(struct i40e_tx_desc));
3131 
3132 	/* As part of VSI creation/update, FW allocates certain
3133 	 * Tx arbitration queue sets for each TC enabled for
3134 	 * the VSI. The FW returns the handles to these queue
3135 	 * sets as part of the response buffer to Add VSI,
3136 	 * Update VSI, etc. AQ commands. It is expected that
3137 	 * these queue set handles be associated with the Tx
3138 	 * queues by the driver as part of the TX queue context
3139 	 * initialization. This has to be done regardless of
3140 	 * DCB as by default everything is mapped to TC0.
3141 	 */
3142 
3143 	if (ring->ch)
3144 		tx_ctx.rdylist =
3145 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3146 
3147 	else
3148 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3149 
3150 	tx_ctx.rdylist_act = 0;
3151 
3152 	/* clear the context in the HMC */
3153 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3154 	if (err) {
3155 		dev_info(&vsi->back->pdev->dev,
3156 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3157 			 ring->queue_index, pf_q, err);
3158 		return -ENOMEM;
3159 	}
3160 
3161 	/* set the context in the HMC */
3162 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3163 	if (err) {
3164 		dev_info(&vsi->back->pdev->dev,
3165 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3166 			 ring->queue_index, pf_q, err);
3167 		return -ENOMEM;
3168 	}
3169 
3170 	/* Now associate this queue with this PCI function */
3171 	if (ring->ch) {
3172 		if (ring->ch->type == I40E_VSI_VMDQ2)
3173 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3174 		else
3175 			return -EINVAL;
3176 
3177 		qtx_ctl |= (ring->ch->vsi_number <<
3178 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3179 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3180 	} else {
3181 		if (vsi->type == I40E_VSI_VMDQ2) {
3182 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3183 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3184 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3185 		} else {
3186 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3187 		}
3188 	}
3189 
3190 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3191 		    I40E_QTX_CTL_PF_INDX_MASK);
3192 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3193 	i40e_flush(hw);
3194 
3195 	/* cache tail off for easier writes later */
3196 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3197 
3198 	return 0;
3199 }
3200 
3201 /**
3202  * i40e_configure_rx_ring - Configure a receive ring context
3203  * @ring: The Rx ring to configure
3204  *
3205  * Configure the Rx descriptor ring in the HMC context.
3206  **/
3207 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3208 {
3209 	struct i40e_vsi *vsi = ring->vsi;
3210 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3211 	u16 pf_q = vsi->base_queue + ring->queue_index;
3212 	struct i40e_hw *hw = &vsi->back->hw;
3213 	struct i40e_hmc_obj_rxq rx_ctx;
3214 	i40e_status err = 0;
3215 
3216 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3217 
3218 	/* clear the context structure first */
3219 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3220 
3221 	ring->rx_buf_len = vsi->rx_buf_len;
3222 
3223 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3224 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3225 
3226 	rx_ctx.base = (ring->dma / 128);
3227 	rx_ctx.qlen = ring->count;
3228 
3229 	/* use 32 byte descriptors */
3230 	rx_ctx.dsize = 1;
3231 
3232 	/* descriptor type is always zero
3233 	 * rx_ctx.dtype = 0;
3234 	 */
3235 	rx_ctx.hsplit_0 = 0;
3236 
3237 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3238 	if (hw->revision_id == 0)
3239 		rx_ctx.lrxqthresh = 0;
3240 	else
3241 		rx_ctx.lrxqthresh = 1;
3242 	rx_ctx.crcstrip = 1;
3243 	rx_ctx.l2tsel = 1;
3244 	/* this controls whether VLAN is stripped from inner headers */
3245 	rx_ctx.showiv = 0;
3246 	/* set the prefena field to 1 because the manual says to */
3247 	rx_ctx.prefena = 1;
3248 
3249 	/* clear the context in the HMC */
3250 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3251 	if (err) {
3252 		dev_info(&vsi->back->pdev->dev,
3253 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3254 			 ring->queue_index, pf_q, err);
3255 		return -ENOMEM;
3256 	}
3257 
3258 	/* set the context in the HMC */
3259 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3260 	if (err) {
3261 		dev_info(&vsi->back->pdev->dev,
3262 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3263 			 ring->queue_index, pf_q, err);
3264 		return -ENOMEM;
3265 	}
3266 
3267 	/* configure Rx buffer alignment */
3268 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3269 		clear_ring_build_skb_enabled(ring);
3270 	else
3271 		set_ring_build_skb_enabled(ring);
3272 
3273 	/* cache tail for quicker writes, and clear the reg before use */
3274 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3275 	writel(0, ring->tail);
3276 
3277 	i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3278 
3279 	return 0;
3280 }
3281 
3282 /**
3283  * i40e_vsi_configure_tx - Configure the VSI for Tx
3284  * @vsi: VSI structure describing this set of rings and resources
3285  *
3286  * Configure the Tx VSI for operation.
3287  **/
3288 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3289 {
3290 	int err = 0;
3291 	u16 i;
3292 
3293 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3294 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3295 
3296 	if (!i40e_enabled_xdp_vsi(vsi))
3297 		return err;
3298 
3299 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3300 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3301 
3302 	return err;
3303 }
3304 
3305 /**
3306  * i40e_vsi_configure_rx - Configure the VSI for Rx
3307  * @vsi: the VSI being configured
3308  *
3309  * Configure the Rx VSI for operation.
3310  **/
3311 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3312 {
3313 	int err = 0;
3314 	u16 i;
3315 
3316 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3317 		vsi->max_frame = I40E_MAX_RXBUFFER;
3318 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3319 #if (PAGE_SIZE < 8192)
3320 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3321 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3322 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3323 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3324 #endif
3325 	} else {
3326 		vsi->max_frame = I40E_MAX_RXBUFFER;
3327 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3328 						       I40E_RXBUFFER_2048;
3329 	}
3330 
3331 	/* set up individual rings */
3332 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3333 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3334 
3335 	return err;
3336 }
3337 
3338 /**
3339  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3340  * @vsi: ptr to the VSI
3341  **/
3342 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3343 {
3344 	struct i40e_ring *tx_ring, *rx_ring;
3345 	u16 qoffset, qcount;
3346 	int i, n;
3347 
3348 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3349 		/* Reset the TC information */
3350 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3351 			rx_ring = vsi->rx_rings[i];
3352 			tx_ring = vsi->tx_rings[i];
3353 			rx_ring->dcb_tc = 0;
3354 			tx_ring->dcb_tc = 0;
3355 		}
3356 		return;
3357 	}
3358 
3359 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3360 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3361 			continue;
3362 
3363 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3364 		qcount = vsi->tc_config.tc_info[n].qcount;
3365 		for (i = qoffset; i < (qoffset + qcount); i++) {
3366 			rx_ring = vsi->rx_rings[i];
3367 			tx_ring = vsi->tx_rings[i];
3368 			rx_ring->dcb_tc = n;
3369 			tx_ring->dcb_tc = n;
3370 		}
3371 	}
3372 }
3373 
3374 /**
3375  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3376  * @vsi: ptr to the VSI
3377  **/
3378 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3379 {
3380 	if (vsi->netdev)
3381 		i40e_set_rx_mode(vsi->netdev);
3382 }
3383 
3384 /**
3385  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3386  * @vsi: Pointer to the targeted VSI
3387  *
3388  * This function replays the hlist on the hw where all the SB Flow Director
3389  * filters were saved.
3390  **/
3391 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3392 {
3393 	struct i40e_fdir_filter *filter;
3394 	struct i40e_pf *pf = vsi->back;
3395 	struct hlist_node *node;
3396 
3397 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3398 		return;
3399 
3400 	/* Reset FDir counters as we're replaying all existing filters */
3401 	pf->fd_tcp4_filter_cnt = 0;
3402 	pf->fd_udp4_filter_cnt = 0;
3403 	pf->fd_sctp4_filter_cnt = 0;
3404 	pf->fd_ip4_filter_cnt = 0;
3405 
3406 	hlist_for_each_entry_safe(filter, node,
3407 				  &pf->fdir_filter_list, fdir_node) {
3408 		i40e_add_del_fdir(vsi, filter, true);
3409 	}
3410 }
3411 
3412 /**
3413  * i40e_vsi_configure - Set up the VSI for action
3414  * @vsi: the VSI being configured
3415  **/
3416 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3417 {
3418 	int err;
3419 
3420 	i40e_set_vsi_rx_mode(vsi);
3421 	i40e_restore_vlan(vsi);
3422 	i40e_vsi_config_dcb_rings(vsi);
3423 	err = i40e_vsi_configure_tx(vsi);
3424 	if (!err)
3425 		err = i40e_vsi_configure_rx(vsi);
3426 
3427 	return err;
3428 }
3429 
3430 /**
3431  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3432  * @vsi: the VSI being configured
3433  **/
3434 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3435 {
3436 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3437 	struct i40e_pf *pf = vsi->back;
3438 	struct i40e_hw *hw = &pf->hw;
3439 	u16 vector;
3440 	int i, q;
3441 	u32 qp;
3442 
3443 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3444 	 * and PFINT_LNKLSTn registers, e.g.:
3445 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3446 	 */
3447 	qp = vsi->base_queue;
3448 	vector = vsi->base_vector;
3449 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3450 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3451 
3452 		q_vector->itr_countdown = ITR_COUNTDOWN_START;
3453 		q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
3454 		q_vector->rx.latency_range = I40E_LOW_LATENCY;
3455 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3456 		     q_vector->rx.itr);
3457 		q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
3458 		q_vector->tx.latency_range = I40E_LOW_LATENCY;
3459 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3460 		     q_vector->tx.itr);
3461 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3462 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3463 
3464 		/* Linked list for the queuepairs assigned to this vector */
3465 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3466 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3467 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3468 			u32 val;
3469 
3470 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3471 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3472 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3473 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3474 			      (I40E_QUEUE_TYPE_TX <<
3475 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3476 
3477 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3478 
3479 			if (has_xdp) {
3480 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3481 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3482 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3483 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3484 				      (I40E_QUEUE_TYPE_TX <<
3485 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3486 
3487 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3488 			}
3489 
3490 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3491 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3492 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3493 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3494 			      (I40E_QUEUE_TYPE_RX <<
3495 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3496 
3497 			/* Terminate the linked list */
3498 			if (q == (q_vector->num_ringpairs - 1))
3499 				val |= (I40E_QUEUE_END_OF_LIST <<
3500 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3501 
3502 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3503 			qp++;
3504 		}
3505 	}
3506 
3507 	i40e_flush(hw);
3508 }
3509 
3510 /**
3511  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3512  * @hw: ptr to the hardware info
3513  **/
3514 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3515 {
3516 	struct i40e_hw *hw = &pf->hw;
3517 	u32 val;
3518 
3519 	/* clear things first */
3520 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3521 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3522 
3523 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3524 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3525 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3526 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3527 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3528 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3529 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3530 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3531 
3532 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3533 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3534 
3535 	if (pf->flags & I40E_FLAG_PTP)
3536 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3537 
3538 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3539 
3540 	/* SW_ITR_IDX = 0, but don't change INTENA */
3541 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3542 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3543 
3544 	/* OTHER_ITR_IDX = 0 */
3545 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3546 }
3547 
3548 /**
3549  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3550  * @vsi: the VSI being configured
3551  **/
3552 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3553 {
3554 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3555 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3556 	struct i40e_pf *pf = vsi->back;
3557 	struct i40e_hw *hw = &pf->hw;
3558 	u32 val;
3559 
3560 	/* set the ITR configuration */
3561 	q_vector->itr_countdown = ITR_COUNTDOWN_START;
3562 	q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
3563 	q_vector->rx.latency_range = I40E_LOW_LATENCY;
3564 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
3565 	q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
3566 	q_vector->tx.latency_range = I40E_LOW_LATENCY;
3567 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3568 
3569 	i40e_enable_misc_int_causes(pf);
3570 
3571 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3572 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3573 
3574 	/* Associate the queue pair to the vector and enable the queue int */
3575 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3576 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3577 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3578 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3579 
3580 	wr32(hw, I40E_QINT_RQCTL(0), val);
3581 
3582 	if (i40e_enabled_xdp_vsi(vsi)) {
3583 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3584 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3585 		      (I40E_QUEUE_TYPE_TX
3586 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3587 
3588 	       wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3589 	}
3590 
3591 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3592 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3593 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3594 
3595 	wr32(hw, I40E_QINT_TQCTL(0), val);
3596 	i40e_flush(hw);
3597 }
3598 
3599 /**
3600  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3601  * @pf: board private structure
3602  **/
3603 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3604 {
3605 	struct i40e_hw *hw = &pf->hw;
3606 
3607 	wr32(hw, I40E_PFINT_DYN_CTL0,
3608 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3609 	i40e_flush(hw);
3610 }
3611 
3612 /**
3613  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3614  * @pf: board private structure
3615  **/
3616 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3617 {
3618 	struct i40e_hw *hw = &pf->hw;
3619 	u32 val;
3620 
3621 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3622 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3623 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3624 
3625 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3626 	i40e_flush(hw);
3627 }
3628 
3629 /**
3630  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3631  * @irq: interrupt number
3632  * @data: pointer to a q_vector
3633  **/
3634 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3635 {
3636 	struct i40e_q_vector *q_vector = data;
3637 
3638 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3639 		return IRQ_HANDLED;
3640 
3641 	napi_schedule_irqoff(&q_vector->napi);
3642 
3643 	return IRQ_HANDLED;
3644 }
3645 
3646 /**
3647  * i40e_irq_affinity_notify - Callback for affinity changes
3648  * @notify: context as to what irq was changed
3649  * @mask: the new affinity mask
3650  *
3651  * This is a callback function used by the irq_set_affinity_notifier function
3652  * so that we may register to receive changes to the irq affinity masks.
3653  **/
3654 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3655 				     const cpumask_t *mask)
3656 {
3657 	struct i40e_q_vector *q_vector =
3658 		container_of(notify, struct i40e_q_vector, affinity_notify);
3659 
3660 	cpumask_copy(&q_vector->affinity_mask, mask);
3661 }
3662 
3663 /**
3664  * i40e_irq_affinity_release - Callback for affinity notifier release
3665  * @ref: internal core kernel usage
3666  *
3667  * This is a callback function used by the irq_set_affinity_notifier function
3668  * to inform the current notification subscriber that they will no longer
3669  * receive notifications.
3670  **/
3671 static void i40e_irq_affinity_release(struct kref *ref) {}
3672 
3673 /**
3674  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3675  * @vsi: the VSI being configured
3676  * @basename: name for the vector
3677  *
3678  * Allocates MSI-X vectors and requests interrupts from the kernel.
3679  **/
3680 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3681 {
3682 	int q_vectors = vsi->num_q_vectors;
3683 	struct i40e_pf *pf = vsi->back;
3684 	int base = vsi->base_vector;
3685 	int rx_int_idx = 0;
3686 	int tx_int_idx = 0;
3687 	int vector, err;
3688 	int irq_num;
3689 	int cpu;
3690 
3691 	for (vector = 0; vector < q_vectors; vector++) {
3692 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3693 
3694 		irq_num = pf->msix_entries[base + vector].vector;
3695 
3696 		if (q_vector->tx.ring && q_vector->rx.ring) {
3697 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3698 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3699 			tx_int_idx++;
3700 		} else if (q_vector->rx.ring) {
3701 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3702 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3703 		} else if (q_vector->tx.ring) {
3704 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3705 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3706 		} else {
3707 			/* skip this unused q_vector */
3708 			continue;
3709 		}
3710 		err = request_irq(irq_num,
3711 				  vsi->irq_handler,
3712 				  0,
3713 				  q_vector->name,
3714 				  q_vector);
3715 		if (err) {
3716 			dev_info(&pf->pdev->dev,
3717 				 "MSIX request_irq failed, error: %d\n", err);
3718 			goto free_queue_irqs;
3719 		}
3720 
3721 		/* register for affinity change notifications */
3722 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3723 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3724 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3725 		/* Spread affinity hints out across online CPUs.
3726 		 *
3727 		 * get_cpu_mask returns a static constant mask with
3728 		 * a permanent lifetime so it's ok to pass to
3729 		 * irq_set_affinity_hint without making a copy.
3730 		 */
3731 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3732 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3733 	}
3734 
3735 	vsi->irqs_ready = true;
3736 	return 0;
3737 
3738 free_queue_irqs:
3739 	while (vector) {
3740 		vector--;
3741 		irq_num = pf->msix_entries[base + vector].vector;
3742 		irq_set_affinity_notifier(irq_num, NULL);
3743 		irq_set_affinity_hint(irq_num, NULL);
3744 		free_irq(irq_num, &vsi->q_vectors[vector]);
3745 	}
3746 	return err;
3747 }
3748 
3749 /**
3750  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3751  * @vsi: the VSI being un-configured
3752  **/
3753 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3754 {
3755 	struct i40e_pf *pf = vsi->back;
3756 	struct i40e_hw *hw = &pf->hw;
3757 	int base = vsi->base_vector;
3758 	int i;
3759 
3760 	/* disable interrupt causation from each queue */
3761 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3762 		u32 val;
3763 
3764 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3765 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3766 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3767 
3768 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3769 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3770 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3771 
3772 		if (!i40e_enabled_xdp_vsi(vsi))
3773 			continue;
3774 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3775 	}
3776 
3777 	/* disable each interrupt */
3778 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3779 		for (i = vsi->base_vector;
3780 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3781 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3782 
3783 		i40e_flush(hw);
3784 		for (i = 0; i < vsi->num_q_vectors; i++)
3785 			synchronize_irq(pf->msix_entries[i + base].vector);
3786 	} else {
3787 		/* Legacy and MSI mode - this stops all interrupt handling */
3788 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3789 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3790 		i40e_flush(hw);
3791 		synchronize_irq(pf->pdev->irq);
3792 	}
3793 }
3794 
3795 /**
3796  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3797  * @vsi: the VSI being configured
3798  **/
3799 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3800 {
3801 	struct i40e_pf *pf = vsi->back;
3802 	int i;
3803 
3804 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3805 		for (i = 0; i < vsi->num_q_vectors; i++)
3806 			i40e_irq_dynamic_enable(vsi, i);
3807 	} else {
3808 		i40e_irq_dynamic_enable_icr0(pf);
3809 	}
3810 
3811 	i40e_flush(&pf->hw);
3812 	return 0;
3813 }
3814 
3815 /**
3816  * i40e_free_misc_vector - Free the vector that handles non-queue events
3817  * @pf: board private structure
3818  **/
3819 static void i40e_free_misc_vector(struct i40e_pf *pf)
3820 {
3821 	/* Disable ICR 0 */
3822 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3823 	i40e_flush(&pf->hw);
3824 
3825 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3826 		synchronize_irq(pf->msix_entries[0].vector);
3827 		free_irq(pf->msix_entries[0].vector, pf);
3828 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3829 	}
3830 }
3831 
3832 /**
3833  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3834  * @irq: interrupt number
3835  * @data: pointer to a q_vector
3836  *
3837  * This is the handler used for all MSI/Legacy interrupts, and deals
3838  * with both queue and non-queue interrupts.  This is also used in
3839  * MSIX mode to handle the non-queue interrupts.
3840  **/
3841 static irqreturn_t i40e_intr(int irq, void *data)
3842 {
3843 	struct i40e_pf *pf = (struct i40e_pf *)data;
3844 	struct i40e_hw *hw = &pf->hw;
3845 	irqreturn_t ret = IRQ_NONE;
3846 	u32 icr0, icr0_remaining;
3847 	u32 val, ena_mask;
3848 
3849 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3850 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3851 
3852 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
3853 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3854 		goto enable_intr;
3855 
3856 	/* if interrupt but no bits showing, must be SWINT */
3857 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3858 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3859 		pf->sw_int_count++;
3860 
3861 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3862 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3863 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3864 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3865 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3866 	}
3867 
3868 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3869 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3870 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3871 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3872 
3873 		/* We do not have a way to disarm Queue causes while leaving
3874 		 * interrupt enabled for all other causes, ideally
3875 		 * interrupt should be disabled while we are in NAPI but
3876 		 * this is not a performance path and napi_schedule()
3877 		 * can deal with rescheduling.
3878 		 */
3879 		if (!test_bit(__I40E_DOWN, pf->state))
3880 			napi_schedule_irqoff(&q_vector->napi);
3881 	}
3882 
3883 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3884 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3885 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
3886 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
3887 	}
3888 
3889 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3890 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3891 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
3892 	}
3893 
3894 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3895 		ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3896 		set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3897 	}
3898 
3899 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3900 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3901 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
3902 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3903 		val = rd32(hw, I40E_GLGEN_RSTAT);
3904 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3905 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
3906 		if (val == I40E_RESET_CORER) {
3907 			pf->corer_count++;
3908 		} else if (val == I40E_RESET_GLOBR) {
3909 			pf->globr_count++;
3910 		} else if (val == I40E_RESET_EMPR) {
3911 			pf->empr_count++;
3912 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
3913 		}
3914 	}
3915 
3916 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3917 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3918 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
3919 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3920 			 rd32(hw, I40E_PFHMC_ERRORINFO),
3921 			 rd32(hw, I40E_PFHMC_ERRORDATA));
3922 	}
3923 
3924 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3925 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3926 
3927 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
3928 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3929 			i40e_ptp_tx_hwtstamp(pf);
3930 		}
3931 	}
3932 
3933 	/* If a critical error is pending we have no choice but to reset the
3934 	 * device.
3935 	 * Report and mask out any remaining unexpected interrupts.
3936 	 */
3937 	icr0_remaining = icr0 & ena_mask;
3938 	if (icr0_remaining) {
3939 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3940 			 icr0_remaining);
3941 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
3942 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
3943 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
3944 			dev_info(&pf->pdev->dev, "device will be reset\n");
3945 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
3946 			i40e_service_event_schedule(pf);
3947 		}
3948 		ena_mask &= ~icr0_remaining;
3949 	}
3950 	ret = IRQ_HANDLED;
3951 
3952 enable_intr:
3953 	/* re-enable interrupt causes */
3954 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
3955 	if (!test_bit(__I40E_DOWN, pf->state)) {
3956 		i40e_service_event_schedule(pf);
3957 		i40e_irq_dynamic_enable_icr0(pf);
3958 	}
3959 
3960 	return ret;
3961 }
3962 
3963 /**
3964  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3965  * @tx_ring:  tx ring to clean
3966  * @budget:   how many cleans we're allowed
3967  *
3968  * Returns true if there's any budget left (e.g. the clean is finished)
3969  **/
3970 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3971 {
3972 	struct i40e_vsi *vsi = tx_ring->vsi;
3973 	u16 i = tx_ring->next_to_clean;
3974 	struct i40e_tx_buffer *tx_buf;
3975 	struct i40e_tx_desc *tx_desc;
3976 
3977 	tx_buf = &tx_ring->tx_bi[i];
3978 	tx_desc = I40E_TX_DESC(tx_ring, i);
3979 	i -= tx_ring->count;
3980 
3981 	do {
3982 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3983 
3984 		/* if next_to_watch is not set then there is no work pending */
3985 		if (!eop_desc)
3986 			break;
3987 
3988 		/* prevent any other reads prior to eop_desc */
3989 		smp_rmb();
3990 
3991 		/* if the descriptor isn't done, no work yet to do */
3992 		if (!(eop_desc->cmd_type_offset_bsz &
3993 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3994 			break;
3995 
3996 		/* clear next_to_watch to prevent false hangs */
3997 		tx_buf->next_to_watch = NULL;
3998 
3999 		tx_desc->buffer_addr = 0;
4000 		tx_desc->cmd_type_offset_bsz = 0;
4001 		/* move past filter desc */
4002 		tx_buf++;
4003 		tx_desc++;
4004 		i++;
4005 		if (unlikely(!i)) {
4006 			i -= tx_ring->count;
4007 			tx_buf = tx_ring->tx_bi;
4008 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4009 		}
4010 		/* unmap skb header data */
4011 		dma_unmap_single(tx_ring->dev,
4012 				 dma_unmap_addr(tx_buf, dma),
4013 				 dma_unmap_len(tx_buf, len),
4014 				 DMA_TO_DEVICE);
4015 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4016 			kfree(tx_buf->raw_buf);
4017 
4018 		tx_buf->raw_buf = NULL;
4019 		tx_buf->tx_flags = 0;
4020 		tx_buf->next_to_watch = NULL;
4021 		dma_unmap_len_set(tx_buf, len, 0);
4022 		tx_desc->buffer_addr = 0;
4023 		tx_desc->cmd_type_offset_bsz = 0;
4024 
4025 		/* move us past the eop_desc for start of next FD desc */
4026 		tx_buf++;
4027 		tx_desc++;
4028 		i++;
4029 		if (unlikely(!i)) {
4030 			i -= tx_ring->count;
4031 			tx_buf = tx_ring->tx_bi;
4032 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4033 		}
4034 
4035 		/* update budget accounting */
4036 		budget--;
4037 	} while (likely(budget));
4038 
4039 	i += tx_ring->count;
4040 	tx_ring->next_to_clean = i;
4041 
4042 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4043 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4044 
4045 	return budget > 0;
4046 }
4047 
4048 /**
4049  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4050  * @irq: interrupt number
4051  * @data: pointer to a q_vector
4052  **/
4053 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4054 {
4055 	struct i40e_q_vector *q_vector = data;
4056 	struct i40e_vsi *vsi;
4057 
4058 	if (!q_vector->tx.ring)
4059 		return IRQ_HANDLED;
4060 
4061 	vsi = q_vector->tx.ring->vsi;
4062 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4063 
4064 	return IRQ_HANDLED;
4065 }
4066 
4067 /**
4068  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4069  * @vsi: the VSI being configured
4070  * @v_idx: vector index
4071  * @qp_idx: queue pair index
4072  **/
4073 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4074 {
4075 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4076 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4077 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4078 
4079 	tx_ring->q_vector = q_vector;
4080 	tx_ring->next = q_vector->tx.ring;
4081 	q_vector->tx.ring = tx_ring;
4082 	q_vector->tx.count++;
4083 
4084 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4085 	if (i40e_enabled_xdp_vsi(vsi)) {
4086 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4087 
4088 		xdp_ring->q_vector = q_vector;
4089 		xdp_ring->next = q_vector->tx.ring;
4090 		q_vector->tx.ring = xdp_ring;
4091 		q_vector->tx.count++;
4092 	}
4093 
4094 	rx_ring->q_vector = q_vector;
4095 	rx_ring->next = q_vector->rx.ring;
4096 	q_vector->rx.ring = rx_ring;
4097 	q_vector->rx.count++;
4098 }
4099 
4100 /**
4101  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4102  * @vsi: the VSI being configured
4103  *
4104  * This function maps descriptor rings to the queue-specific vectors
4105  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4106  * one vector per queue pair, but on a constrained vector budget, we
4107  * group the queue pairs as "efficiently" as possible.
4108  **/
4109 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4110 {
4111 	int qp_remaining = vsi->num_queue_pairs;
4112 	int q_vectors = vsi->num_q_vectors;
4113 	int num_ringpairs;
4114 	int v_start = 0;
4115 	int qp_idx = 0;
4116 
4117 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4118 	 * group them so there are multiple queues per vector.
4119 	 * It is also important to go through all the vectors available to be
4120 	 * sure that if we don't use all the vectors, that the remaining vectors
4121 	 * are cleared. This is especially important when decreasing the
4122 	 * number of queues in use.
4123 	 */
4124 	for (; v_start < q_vectors; v_start++) {
4125 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4126 
4127 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4128 
4129 		q_vector->num_ringpairs = num_ringpairs;
4130 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4131 
4132 		q_vector->rx.count = 0;
4133 		q_vector->tx.count = 0;
4134 		q_vector->rx.ring = NULL;
4135 		q_vector->tx.ring = NULL;
4136 
4137 		while (num_ringpairs--) {
4138 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4139 			qp_idx++;
4140 			qp_remaining--;
4141 		}
4142 	}
4143 }
4144 
4145 /**
4146  * i40e_vsi_request_irq - Request IRQ from the OS
4147  * @vsi: the VSI being configured
4148  * @basename: name for the vector
4149  **/
4150 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4151 {
4152 	struct i40e_pf *pf = vsi->back;
4153 	int err;
4154 
4155 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4156 		err = i40e_vsi_request_irq_msix(vsi, basename);
4157 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4158 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4159 				  pf->int_name, pf);
4160 	else
4161 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4162 				  pf->int_name, pf);
4163 
4164 	if (err)
4165 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4166 
4167 	return err;
4168 }
4169 
4170 #ifdef CONFIG_NET_POLL_CONTROLLER
4171 /**
4172  * i40e_netpoll - A Polling 'interrupt' handler
4173  * @netdev: network interface device structure
4174  *
4175  * This is used by netconsole to send skbs without having to re-enable
4176  * interrupts.  It's not called while the normal interrupt routine is executing.
4177  **/
4178 static void i40e_netpoll(struct net_device *netdev)
4179 {
4180 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4181 	struct i40e_vsi *vsi = np->vsi;
4182 	struct i40e_pf *pf = vsi->back;
4183 	int i;
4184 
4185 	/* if interface is down do nothing */
4186 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4187 		return;
4188 
4189 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4190 		for (i = 0; i < vsi->num_q_vectors; i++)
4191 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4192 	} else {
4193 		i40e_intr(pf->pdev->irq, netdev);
4194 	}
4195 }
4196 #endif
4197 
4198 #define I40E_QTX_ENA_WAIT_COUNT 50
4199 
4200 /**
4201  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4202  * @pf: the PF being configured
4203  * @pf_q: the PF queue
4204  * @enable: enable or disable state of the queue
4205  *
4206  * This routine will wait for the given Tx queue of the PF to reach the
4207  * enabled or disabled state.
4208  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4209  * multiple retries; else will return 0 in case of success.
4210  **/
4211 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4212 {
4213 	int i;
4214 	u32 tx_reg;
4215 
4216 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4217 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4218 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4219 			break;
4220 
4221 		usleep_range(10, 20);
4222 	}
4223 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4224 		return -ETIMEDOUT;
4225 
4226 	return 0;
4227 }
4228 
4229 /**
4230  * i40e_control_tx_q - Start or stop a particular Tx queue
4231  * @pf: the PF structure
4232  * @pf_q: the PF queue to configure
4233  * @enable: start or stop the queue
4234  *
4235  * This function enables or disables a single queue. Note that any delay
4236  * required after the operation is expected to be handled by the caller of
4237  * this function.
4238  **/
4239 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4240 {
4241 	struct i40e_hw *hw = &pf->hw;
4242 	u32 tx_reg;
4243 	int i;
4244 
4245 	/* warn the TX unit of coming changes */
4246 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4247 	if (!enable)
4248 		usleep_range(10, 20);
4249 
4250 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4251 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4252 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4253 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4254 			break;
4255 		usleep_range(1000, 2000);
4256 	}
4257 
4258 	/* Skip if the queue is already in the requested state */
4259 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4260 		return;
4261 
4262 	/* turn on/off the queue */
4263 	if (enable) {
4264 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4265 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4266 	} else {
4267 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4268 	}
4269 
4270 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4271 }
4272 
4273 /**
4274  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4275  * @seid: VSI SEID
4276  * @pf: the PF structure
4277  * @pf_q: the PF queue to configure
4278  * @is_xdp: true if the queue is used for XDP
4279  * @enable: start or stop the queue
4280  **/
4281 static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4282 				  bool is_xdp, bool enable)
4283 {
4284 	int ret;
4285 
4286 	i40e_control_tx_q(pf, pf_q, enable);
4287 
4288 	/* wait for the change to finish */
4289 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4290 	if (ret) {
4291 		dev_info(&pf->pdev->dev,
4292 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4293 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4294 			 (enable ? "en" : "dis"));
4295 	}
4296 
4297 	return ret;
4298 }
4299 
4300 /**
4301  * i40e_vsi_control_tx - Start or stop a VSI's rings
4302  * @vsi: the VSI being configured
4303  * @enable: start or stop the rings
4304  **/
4305 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4306 {
4307 	struct i40e_pf *pf = vsi->back;
4308 	int i, pf_q, ret = 0;
4309 
4310 	pf_q = vsi->base_queue;
4311 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4312 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4313 					     pf_q,
4314 					     false /*is xdp*/, enable);
4315 		if (ret)
4316 			break;
4317 
4318 		if (!i40e_enabled_xdp_vsi(vsi))
4319 			continue;
4320 
4321 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4322 					     pf_q + vsi->alloc_queue_pairs,
4323 					     true /*is xdp*/, enable);
4324 		if (ret)
4325 			break;
4326 	}
4327 
4328 	return ret;
4329 }
4330 
4331 /**
4332  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4333  * @pf: the PF being configured
4334  * @pf_q: the PF queue
4335  * @enable: enable or disable state of the queue
4336  *
4337  * This routine will wait for the given Rx queue of the PF to reach the
4338  * enabled or disabled state.
4339  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4340  * multiple retries; else will return 0 in case of success.
4341  **/
4342 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4343 {
4344 	int i;
4345 	u32 rx_reg;
4346 
4347 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4348 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4349 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4350 			break;
4351 
4352 		usleep_range(10, 20);
4353 	}
4354 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4355 		return -ETIMEDOUT;
4356 
4357 	return 0;
4358 }
4359 
4360 /**
4361  * i40e_control_rx_q - Start or stop a particular Rx queue
4362  * @pf: the PF structure
4363  * @pf_q: the PF queue to configure
4364  * @enable: start or stop the queue
4365  *
4366  * This function enables or disables a single queue. Note that any delay
4367  * required after the operation is expected to be handled by the caller of
4368  * this function.
4369  **/
4370 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4371 {
4372 	struct i40e_hw *hw = &pf->hw;
4373 	u32 rx_reg;
4374 	int i;
4375 
4376 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4377 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4378 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4379 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4380 			break;
4381 		usleep_range(1000, 2000);
4382 	}
4383 
4384 	/* Skip if the queue is already in the requested state */
4385 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4386 		return;
4387 
4388 	/* turn on/off the queue */
4389 	if (enable)
4390 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4391 	else
4392 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4393 
4394 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4395 }
4396 
4397 /**
4398  * i40e_vsi_control_rx - Start or stop a VSI's rings
4399  * @vsi: the VSI being configured
4400  * @enable: start or stop the rings
4401  **/
4402 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4403 {
4404 	struct i40e_pf *pf = vsi->back;
4405 	int i, pf_q, ret = 0;
4406 
4407 	pf_q = vsi->base_queue;
4408 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4409 		i40e_control_rx_q(pf, pf_q, enable);
4410 
4411 		/* wait for the change to finish */
4412 		ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4413 		if (ret) {
4414 			dev_info(&pf->pdev->dev,
4415 				 "VSI seid %d Rx ring %d %sable timeout\n",
4416 				 vsi->seid, pf_q, (enable ? "en" : "dis"));
4417 			break;
4418 		}
4419 	}
4420 
4421 	/* Due to HW errata, on Rx disable only, the register can indicate done
4422 	 * before it really is. Needs 50ms to be sure
4423 	 */
4424 	if (!enable)
4425 		mdelay(50);
4426 
4427 	return ret;
4428 }
4429 
4430 /**
4431  * i40e_vsi_start_rings - Start a VSI's rings
4432  * @vsi: the VSI being configured
4433  **/
4434 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4435 {
4436 	int ret = 0;
4437 
4438 	/* do rx first for enable and last for disable */
4439 	ret = i40e_vsi_control_rx(vsi, true);
4440 	if (ret)
4441 		return ret;
4442 	ret = i40e_vsi_control_tx(vsi, true);
4443 
4444 	return ret;
4445 }
4446 
4447 /**
4448  * i40e_vsi_stop_rings - Stop a VSI's rings
4449  * @vsi: the VSI being configured
4450  **/
4451 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4452 {
4453 	/* When port TX is suspended, don't wait */
4454 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4455 		return i40e_vsi_stop_rings_no_wait(vsi);
4456 
4457 	/* do rx first for enable and last for disable
4458 	 * Ignore return value, we need to shutdown whatever we can
4459 	 */
4460 	i40e_vsi_control_tx(vsi, false);
4461 	i40e_vsi_control_rx(vsi, false);
4462 }
4463 
4464 /**
4465  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4466  * @vsi: the VSI being shutdown
4467  *
4468  * This function stops all the rings for a VSI but does not delay to verify
4469  * that rings have been disabled. It is expected that the caller is shutting
4470  * down multiple VSIs at once and will delay together for all the VSIs after
4471  * initiating the shutdown. This is particularly useful for shutting down lots
4472  * of VFs together. Otherwise, a large delay can be incurred while configuring
4473  * each VSI in serial.
4474  **/
4475 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4476 {
4477 	struct i40e_pf *pf = vsi->back;
4478 	int i, pf_q;
4479 
4480 	pf_q = vsi->base_queue;
4481 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4482 		i40e_control_tx_q(pf, pf_q, false);
4483 		i40e_control_rx_q(pf, pf_q, false);
4484 	}
4485 }
4486 
4487 /**
4488  * i40e_vsi_free_irq - Free the irq association with the OS
4489  * @vsi: the VSI being configured
4490  **/
4491 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4492 {
4493 	struct i40e_pf *pf = vsi->back;
4494 	struct i40e_hw *hw = &pf->hw;
4495 	int base = vsi->base_vector;
4496 	u32 val, qp;
4497 	int i;
4498 
4499 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4500 		if (!vsi->q_vectors)
4501 			return;
4502 
4503 		if (!vsi->irqs_ready)
4504 			return;
4505 
4506 		vsi->irqs_ready = false;
4507 		for (i = 0; i < vsi->num_q_vectors; i++) {
4508 			int irq_num;
4509 			u16 vector;
4510 
4511 			vector = i + base;
4512 			irq_num = pf->msix_entries[vector].vector;
4513 
4514 			/* free only the irqs that were actually requested */
4515 			if (!vsi->q_vectors[i] ||
4516 			    !vsi->q_vectors[i]->num_ringpairs)
4517 				continue;
4518 
4519 			/* clear the affinity notifier in the IRQ descriptor */
4520 			irq_set_affinity_notifier(irq_num, NULL);
4521 			/* remove our suggested affinity mask for this IRQ */
4522 			irq_set_affinity_hint(irq_num, NULL);
4523 			synchronize_irq(irq_num);
4524 			free_irq(irq_num, vsi->q_vectors[i]);
4525 
4526 			/* Tear down the interrupt queue link list
4527 			 *
4528 			 * We know that they come in pairs and always
4529 			 * the Rx first, then the Tx.  To clear the
4530 			 * link list, stick the EOL value into the
4531 			 * next_q field of the registers.
4532 			 */
4533 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4534 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4535 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4536 			val |= I40E_QUEUE_END_OF_LIST
4537 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4538 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4539 
4540 			while (qp != I40E_QUEUE_END_OF_LIST) {
4541 				u32 next;
4542 
4543 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4544 
4545 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4546 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4547 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4548 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4549 
4550 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4551 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4552 
4553 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4554 
4555 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4556 
4557 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4558 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4559 
4560 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4561 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4562 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4563 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4564 
4565 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4566 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4567 
4568 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4569 				qp = next;
4570 			}
4571 		}
4572 	} else {
4573 		free_irq(pf->pdev->irq, pf);
4574 
4575 		val = rd32(hw, I40E_PFINT_LNKLST0);
4576 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4577 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4578 		val |= I40E_QUEUE_END_OF_LIST
4579 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4580 		wr32(hw, I40E_PFINT_LNKLST0, val);
4581 
4582 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4583 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4584 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4585 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4586 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4587 
4588 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4589 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4590 
4591 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4592 
4593 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4594 
4595 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4596 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4597 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4598 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4599 
4600 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4601 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4602 
4603 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4604 	}
4605 }
4606 
4607 /**
4608  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4609  * @vsi: the VSI being configured
4610  * @v_idx: Index of vector to be freed
4611  *
4612  * This function frees the memory allocated to the q_vector.  In addition if
4613  * NAPI is enabled it will delete any references to the NAPI struct prior
4614  * to freeing the q_vector.
4615  **/
4616 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4617 {
4618 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4619 	struct i40e_ring *ring;
4620 
4621 	if (!q_vector)
4622 		return;
4623 
4624 	/* disassociate q_vector from rings */
4625 	i40e_for_each_ring(ring, q_vector->tx)
4626 		ring->q_vector = NULL;
4627 
4628 	i40e_for_each_ring(ring, q_vector->rx)
4629 		ring->q_vector = NULL;
4630 
4631 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4632 	if (vsi->netdev)
4633 		netif_napi_del(&q_vector->napi);
4634 
4635 	vsi->q_vectors[v_idx] = NULL;
4636 
4637 	kfree_rcu(q_vector, rcu);
4638 }
4639 
4640 /**
4641  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4642  * @vsi: the VSI being un-configured
4643  *
4644  * This frees the memory allocated to the q_vectors and
4645  * deletes references to the NAPI struct.
4646  **/
4647 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4648 {
4649 	int v_idx;
4650 
4651 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4652 		i40e_free_q_vector(vsi, v_idx);
4653 }
4654 
4655 /**
4656  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4657  * @pf: board private structure
4658  **/
4659 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4660 {
4661 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4662 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4663 		pci_disable_msix(pf->pdev);
4664 		kfree(pf->msix_entries);
4665 		pf->msix_entries = NULL;
4666 		kfree(pf->irq_pile);
4667 		pf->irq_pile = NULL;
4668 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4669 		pci_disable_msi(pf->pdev);
4670 	}
4671 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4672 }
4673 
4674 /**
4675  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4676  * @pf: board private structure
4677  *
4678  * We go through and clear interrupt specific resources and reset the structure
4679  * to pre-load conditions
4680  **/
4681 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4682 {
4683 	int i;
4684 
4685 	i40e_free_misc_vector(pf);
4686 
4687 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4688 		      I40E_IWARP_IRQ_PILE_ID);
4689 
4690 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4691 	for (i = 0; i < pf->num_alloc_vsi; i++)
4692 		if (pf->vsi[i])
4693 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4694 	i40e_reset_interrupt_capability(pf);
4695 }
4696 
4697 /**
4698  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4699  * @vsi: the VSI being configured
4700  **/
4701 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4702 {
4703 	int q_idx;
4704 
4705 	if (!vsi->netdev)
4706 		return;
4707 
4708 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4709 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4710 
4711 		if (q_vector->rx.ring || q_vector->tx.ring)
4712 			napi_enable(&q_vector->napi);
4713 	}
4714 }
4715 
4716 /**
4717  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4718  * @vsi: the VSI being configured
4719  **/
4720 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4721 {
4722 	int q_idx;
4723 
4724 	if (!vsi->netdev)
4725 		return;
4726 
4727 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4728 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4729 
4730 		if (q_vector->rx.ring || q_vector->tx.ring)
4731 			napi_disable(&q_vector->napi);
4732 	}
4733 }
4734 
4735 /**
4736  * i40e_vsi_close - Shut down a VSI
4737  * @vsi: the vsi to be quelled
4738  **/
4739 static void i40e_vsi_close(struct i40e_vsi *vsi)
4740 {
4741 	struct i40e_pf *pf = vsi->back;
4742 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4743 		i40e_down(vsi);
4744 	i40e_vsi_free_irq(vsi);
4745 	i40e_vsi_free_tx_resources(vsi);
4746 	i40e_vsi_free_rx_resources(vsi);
4747 	vsi->current_netdev_flags = 0;
4748 	pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
4749 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4750 		pf->flags |=  I40E_FLAG_CLIENT_RESET;
4751 }
4752 
4753 /**
4754  * i40e_quiesce_vsi - Pause a given VSI
4755  * @vsi: the VSI being paused
4756  **/
4757 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4758 {
4759 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4760 		return;
4761 
4762 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4763 	if (vsi->netdev && netif_running(vsi->netdev))
4764 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4765 	else
4766 		i40e_vsi_close(vsi);
4767 }
4768 
4769 /**
4770  * i40e_unquiesce_vsi - Resume a given VSI
4771  * @vsi: the VSI being resumed
4772  **/
4773 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4774 {
4775 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4776 		return;
4777 
4778 	if (vsi->netdev && netif_running(vsi->netdev))
4779 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4780 	else
4781 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4782 }
4783 
4784 /**
4785  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4786  * @pf: the PF
4787  **/
4788 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4789 {
4790 	int v;
4791 
4792 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4793 		if (pf->vsi[v])
4794 			i40e_quiesce_vsi(pf->vsi[v]);
4795 	}
4796 }
4797 
4798 /**
4799  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4800  * @pf: the PF
4801  **/
4802 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4803 {
4804 	int v;
4805 
4806 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4807 		if (pf->vsi[v])
4808 			i40e_unquiesce_vsi(pf->vsi[v]);
4809 	}
4810 }
4811 
4812 /**
4813  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4814  * @vsi: the VSI being configured
4815  *
4816  * Wait until all queues on a given VSI have been disabled.
4817  **/
4818 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4819 {
4820 	struct i40e_pf *pf = vsi->back;
4821 	int i, pf_q, ret;
4822 
4823 	pf_q = vsi->base_queue;
4824 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4825 		/* Check and wait for the Tx queue */
4826 		ret = i40e_pf_txq_wait(pf, pf_q, false);
4827 		if (ret) {
4828 			dev_info(&pf->pdev->dev,
4829 				 "VSI seid %d Tx ring %d disable timeout\n",
4830 				 vsi->seid, pf_q);
4831 			return ret;
4832 		}
4833 
4834 		if (!i40e_enabled_xdp_vsi(vsi))
4835 			goto wait_rx;
4836 
4837 		/* Check and wait for the XDP Tx queue */
4838 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4839 				       false);
4840 		if (ret) {
4841 			dev_info(&pf->pdev->dev,
4842 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
4843 				 vsi->seid, pf_q);
4844 			return ret;
4845 		}
4846 wait_rx:
4847 		/* Check and wait for the Rx queue */
4848 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
4849 		if (ret) {
4850 			dev_info(&pf->pdev->dev,
4851 				 "VSI seid %d Rx ring %d disable timeout\n",
4852 				 vsi->seid, pf_q);
4853 			return ret;
4854 		}
4855 	}
4856 
4857 	return 0;
4858 }
4859 
4860 #ifdef CONFIG_I40E_DCB
4861 /**
4862  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
4863  * @pf: the PF
4864  *
4865  * This function waits for the queues to be in disabled state for all the
4866  * VSIs that are managed by this PF.
4867  **/
4868 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
4869 {
4870 	int v, ret = 0;
4871 
4872 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4873 		if (pf->vsi[v]) {
4874 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
4875 			if (ret)
4876 				break;
4877 		}
4878 	}
4879 
4880 	return ret;
4881 }
4882 
4883 #endif
4884 
4885 /**
4886  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
4887  * @pf: pointer to PF
4888  *
4889  * Get TC map for ISCSI PF type that will include iSCSI TC
4890  * and LAN TC.
4891  **/
4892 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4893 {
4894 	struct i40e_dcb_app_priority_table app;
4895 	struct i40e_hw *hw = &pf->hw;
4896 	u8 enabled_tc = 1; /* TC0 is always enabled */
4897 	u8 tc, i;
4898 	/* Get the iSCSI APP TLV */
4899 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4900 
4901 	for (i = 0; i < dcbcfg->numapps; i++) {
4902 		app = dcbcfg->app[i];
4903 		if (app.selector == I40E_APP_SEL_TCPIP &&
4904 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
4905 			tc = dcbcfg->etscfg.prioritytable[app.priority];
4906 			enabled_tc |= BIT(tc);
4907 			break;
4908 		}
4909 	}
4910 
4911 	return enabled_tc;
4912 }
4913 
4914 /**
4915  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
4916  * @dcbcfg: the corresponding DCBx configuration structure
4917  *
4918  * Return the number of TCs from given DCBx configuration
4919  **/
4920 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4921 {
4922 	int i, tc_unused = 0;
4923 	u8 num_tc = 0;
4924 	u8 ret = 0;
4925 
4926 	/* Scan the ETS Config Priority Table to find
4927 	 * traffic class enabled for a given priority
4928 	 * and create a bitmask of enabled TCs
4929 	 */
4930 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4931 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4932 
4933 	/* Now scan the bitmask to check for
4934 	 * contiguous TCs starting with TC0
4935 	 */
4936 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4937 		if (num_tc & BIT(i)) {
4938 			if (!tc_unused) {
4939 				ret++;
4940 			} else {
4941 				pr_err("Non-contiguous TC - Disabling DCB\n");
4942 				return 1;
4943 			}
4944 		} else {
4945 			tc_unused = 1;
4946 		}
4947 	}
4948 
4949 	/* There is always at least TC0 */
4950 	if (!ret)
4951 		ret = 1;
4952 
4953 	return ret;
4954 }
4955 
4956 /**
4957  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4958  * @dcbcfg: the corresponding DCBx configuration structure
4959  *
4960  * Query the current DCB configuration and return the number of
4961  * traffic classes enabled from the given DCBX config
4962  **/
4963 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4964 {
4965 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4966 	u8 enabled_tc = 1;
4967 	u8 i;
4968 
4969 	for (i = 0; i < num_tc; i++)
4970 		enabled_tc |= BIT(i);
4971 
4972 	return enabled_tc;
4973 }
4974 
4975 /**
4976  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
4977  * @pf: PF being queried
4978  *
4979  * Query the current MQPRIO configuration and return the number of
4980  * traffic classes enabled.
4981  **/
4982 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
4983 {
4984 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4985 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
4986 	u8 enabled_tc = 1, i;
4987 
4988 	for (i = 1; i < num_tc; i++)
4989 		enabled_tc |= BIT(i);
4990 	return enabled_tc;
4991 }
4992 
4993 /**
4994  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4995  * @pf: PF being queried
4996  *
4997  * Return number of traffic classes enabled for the given PF
4998  **/
4999 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5000 {
5001 	struct i40e_hw *hw = &pf->hw;
5002 	u8 i, enabled_tc = 1;
5003 	u8 num_tc = 0;
5004 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5005 
5006 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5007 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5008 
5009 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5010 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5011 		return 1;
5012 
5013 	/* SFP mode will be enabled for all TCs on port */
5014 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5015 		return i40e_dcb_get_num_tc(dcbcfg);
5016 
5017 	/* MFP mode return count of enabled TCs for this PF */
5018 	if (pf->hw.func_caps.iscsi)
5019 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5020 	else
5021 		return 1; /* Only TC0 */
5022 
5023 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5024 		if (enabled_tc & BIT(i))
5025 			num_tc++;
5026 	}
5027 	return num_tc;
5028 }
5029 
5030 /**
5031  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5032  * @pf: PF being queried
5033  *
5034  * Return a bitmap for enabled traffic classes for this PF.
5035  **/
5036 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5037 {
5038 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5039 		return i40e_mqprio_get_enabled_tc(pf);
5040 
5041 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5042 	 * default TC
5043 	 */
5044 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5045 		return I40E_DEFAULT_TRAFFIC_CLASS;
5046 
5047 	/* SFP mode we want PF to be enabled for all TCs */
5048 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5049 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5050 
5051 	/* MFP enabled and iSCSI PF type */
5052 	if (pf->hw.func_caps.iscsi)
5053 		return i40e_get_iscsi_tc_map(pf);
5054 	else
5055 		return I40E_DEFAULT_TRAFFIC_CLASS;
5056 }
5057 
5058 /**
5059  * i40e_vsi_get_bw_info - Query VSI BW Information
5060  * @vsi: the VSI being queried
5061  *
5062  * Returns 0 on success, negative value on failure
5063  **/
5064 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5065 {
5066 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5067 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5068 	struct i40e_pf *pf = vsi->back;
5069 	struct i40e_hw *hw = &pf->hw;
5070 	i40e_status ret;
5071 	u32 tc_bw_max;
5072 	int i;
5073 
5074 	/* Get the VSI level BW configuration */
5075 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5076 	if (ret) {
5077 		dev_info(&pf->pdev->dev,
5078 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5079 			 i40e_stat_str(&pf->hw, ret),
5080 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5081 		return -EINVAL;
5082 	}
5083 
5084 	/* Get the VSI level BW configuration per TC */
5085 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5086 					       NULL);
5087 	if (ret) {
5088 		dev_info(&pf->pdev->dev,
5089 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5090 			 i40e_stat_str(&pf->hw, ret),
5091 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5092 		return -EINVAL;
5093 	}
5094 
5095 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5096 		dev_info(&pf->pdev->dev,
5097 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5098 			 bw_config.tc_valid_bits,
5099 			 bw_ets_config.tc_valid_bits);
5100 		/* Still continuing */
5101 	}
5102 
5103 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5104 	vsi->bw_max_quanta = bw_config.max_bw;
5105 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5106 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5107 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5108 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5109 		vsi->bw_ets_limit_credits[i] =
5110 					le16_to_cpu(bw_ets_config.credits[i]);
5111 		/* 3 bits out of 4 for each TC */
5112 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5113 	}
5114 
5115 	return 0;
5116 }
5117 
5118 /**
5119  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5120  * @vsi: the VSI being configured
5121  * @enabled_tc: TC bitmap
5122  * @bw_credits: BW shared credits per TC
5123  *
5124  * Returns 0 on success, negative value on failure
5125  **/
5126 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5127 				       u8 *bw_share)
5128 {
5129 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5130 	i40e_status ret;
5131 	int i;
5132 
5133 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO)
5134 		return 0;
5135 	if (!vsi->mqprio_qopt.qopt.hw) {
5136 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5137 		if (ret)
5138 			dev_info(&vsi->back->pdev->dev,
5139 				 "Failed to reset tx rate for vsi->seid %u\n",
5140 				 vsi->seid);
5141 		return ret;
5142 	}
5143 	bw_data.tc_valid_bits = enabled_tc;
5144 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5145 		bw_data.tc_bw_credits[i] = bw_share[i];
5146 
5147 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
5148 				       NULL);
5149 	if (ret) {
5150 		dev_info(&vsi->back->pdev->dev,
5151 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5152 			 vsi->back->hw.aq.asq_last_status);
5153 		return -EINVAL;
5154 	}
5155 
5156 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5157 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5158 
5159 	return 0;
5160 }
5161 
5162 /**
5163  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5164  * @vsi: the VSI being configured
5165  * @enabled_tc: TC map to be enabled
5166  *
5167  **/
5168 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5169 {
5170 	struct net_device *netdev = vsi->netdev;
5171 	struct i40e_pf *pf = vsi->back;
5172 	struct i40e_hw *hw = &pf->hw;
5173 	u8 netdev_tc = 0;
5174 	int i;
5175 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5176 
5177 	if (!netdev)
5178 		return;
5179 
5180 	if (!enabled_tc) {
5181 		netdev_reset_tc(netdev);
5182 		return;
5183 	}
5184 
5185 	/* Set up actual enabled TCs on the VSI */
5186 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5187 		return;
5188 
5189 	/* set per TC queues for the VSI */
5190 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5191 		/* Only set TC queues for enabled tcs
5192 		 *
5193 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5194 		 * enabled_tc bitmap would be 0x00001001; the driver
5195 		 * will set the numtc for netdev as 2 that will be
5196 		 * referenced by the netdev layer as TC 0 and 1.
5197 		 */
5198 		if (vsi->tc_config.enabled_tc & BIT(i))
5199 			netdev_set_tc_queue(netdev,
5200 					vsi->tc_config.tc_info[i].netdev_tc,
5201 					vsi->tc_config.tc_info[i].qcount,
5202 					vsi->tc_config.tc_info[i].qoffset);
5203 	}
5204 
5205 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5206 		return;
5207 
5208 	/* Assign UP2TC map for the VSI */
5209 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5210 		/* Get the actual TC# for the UP */
5211 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5212 		/* Get the mapped netdev TC# for the UP */
5213 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5214 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5215 	}
5216 }
5217 
5218 /**
5219  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5220  * @vsi: the VSI being configured
5221  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5222  **/
5223 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5224 				      struct i40e_vsi_context *ctxt)
5225 {
5226 	/* copy just the sections touched not the entire info
5227 	 * since not all sections are valid as returned by
5228 	 * update vsi params
5229 	 */
5230 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5231 	memcpy(&vsi->info.queue_mapping,
5232 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5233 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5234 	       sizeof(vsi->info.tc_mapping));
5235 }
5236 
5237 /**
5238  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5239  * @vsi: VSI to be configured
5240  * @enabled_tc: TC bitmap
5241  *
5242  * This configures a particular VSI for TCs that are mapped to the
5243  * given TC bitmap. It uses default bandwidth share for TCs across
5244  * VSIs to configure TC for a particular VSI.
5245  *
5246  * NOTE:
5247  * It is expected that the VSI queues have been quisced before calling
5248  * this function.
5249  **/
5250 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5251 {
5252 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5253 	struct i40e_pf *pf = vsi->back;
5254 	struct i40e_hw *hw = &pf->hw;
5255 	struct i40e_vsi_context ctxt;
5256 	int ret = 0;
5257 	int i;
5258 
5259 	/* Check if enabled_tc is same as existing or new TCs */
5260 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5261 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5262 		return ret;
5263 
5264 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5265 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5266 		if (enabled_tc & BIT(i))
5267 			bw_share[i] = 1;
5268 	}
5269 
5270 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5271 	if (ret) {
5272 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5273 
5274 		dev_info(&pf->pdev->dev,
5275 			 "Failed configuring TC map %d for VSI %d\n",
5276 			 enabled_tc, vsi->seid);
5277 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5278 						  &bw_config, NULL);
5279 		if (ret) {
5280 			dev_info(&pf->pdev->dev,
5281 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5282 				 i40e_stat_str(hw, ret),
5283 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5284 			goto out;
5285 		}
5286 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5287 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5288 
5289 			if (!valid_tc)
5290 				valid_tc = bw_config.tc_valid_bits;
5291 			/* Always enable TC0, no matter what */
5292 			valid_tc |= 1;
5293 			dev_info(&pf->pdev->dev,
5294 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5295 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5296 			enabled_tc = valid_tc;
5297 		}
5298 
5299 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5300 		if (ret) {
5301 			dev_err(&pf->pdev->dev,
5302 				"Unable to  configure TC map %d for VSI %d\n",
5303 				enabled_tc, vsi->seid);
5304 			goto out;
5305 		}
5306 	}
5307 
5308 	/* Update Queue Pairs Mapping for currently enabled UPs */
5309 	ctxt.seid = vsi->seid;
5310 	ctxt.pf_num = vsi->back->hw.pf_id;
5311 	ctxt.vf_num = 0;
5312 	ctxt.uplink_seid = vsi->uplink_seid;
5313 	ctxt.info = vsi->info;
5314 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5315 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5316 		if (ret)
5317 			goto out;
5318 	} else {
5319 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5320 	}
5321 
5322 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5323 	 * queues changed.
5324 	 */
5325 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5326 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5327 				      vsi->num_queue_pairs);
5328 		ret = i40e_vsi_config_rss(vsi);
5329 		if (ret) {
5330 			dev_info(&vsi->back->pdev->dev,
5331 				 "Failed to reconfig rss for num_queues\n");
5332 			return ret;
5333 		}
5334 		vsi->reconfig_rss = false;
5335 	}
5336 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5337 		ctxt.info.valid_sections |=
5338 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5339 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5340 	}
5341 
5342 	/* Update the VSI after updating the VSI queue-mapping
5343 	 * information
5344 	 */
5345 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5346 	if (ret) {
5347 		dev_info(&pf->pdev->dev,
5348 			 "Update vsi tc config failed, err %s aq_err %s\n",
5349 			 i40e_stat_str(hw, ret),
5350 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5351 		goto out;
5352 	}
5353 	/* update the local VSI info with updated queue map */
5354 	i40e_vsi_update_queue_map(vsi, &ctxt);
5355 	vsi->info.valid_sections = 0;
5356 
5357 	/* Update current VSI BW information */
5358 	ret = i40e_vsi_get_bw_info(vsi);
5359 	if (ret) {
5360 		dev_info(&pf->pdev->dev,
5361 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5362 			 i40e_stat_str(hw, ret),
5363 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5364 		goto out;
5365 	}
5366 
5367 	/* Update the netdev TC setup */
5368 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5369 out:
5370 	return ret;
5371 }
5372 
5373 /**
5374  * i40e_get_link_speed - Returns link speed for the interface
5375  * @vsi: VSI to be configured
5376  *
5377  **/
5378 int i40e_get_link_speed(struct i40e_vsi *vsi)
5379 {
5380 	struct i40e_pf *pf = vsi->back;
5381 
5382 	switch (pf->hw.phy.link_info.link_speed) {
5383 	case I40E_LINK_SPEED_40GB:
5384 		return 40000;
5385 	case I40E_LINK_SPEED_25GB:
5386 		return 25000;
5387 	case I40E_LINK_SPEED_20GB:
5388 		return 20000;
5389 	case I40E_LINK_SPEED_10GB:
5390 		return 10000;
5391 	case I40E_LINK_SPEED_1GB:
5392 		return 1000;
5393 	default:
5394 		return -EINVAL;
5395 	}
5396 }
5397 
5398 /**
5399  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5400  * @vsi: VSI to be configured
5401  * @seid: seid of the channel/VSI
5402  * @max_tx_rate: max TX rate to be configured as BW limit
5403  *
5404  * Helper function to set BW limit for a given VSI
5405  **/
5406 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5407 {
5408 	struct i40e_pf *pf = vsi->back;
5409 	u64 credits = 0;
5410 	int speed = 0;
5411 	int ret = 0;
5412 
5413 	speed = i40e_get_link_speed(vsi);
5414 	if (max_tx_rate > speed) {
5415 		dev_err(&pf->pdev->dev,
5416 			"Invalid max tx rate %llu specified for VSI seid %d.",
5417 			max_tx_rate, seid);
5418 		return -EINVAL;
5419 	}
5420 	if (max_tx_rate && max_tx_rate < 50) {
5421 		dev_warn(&pf->pdev->dev,
5422 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5423 		max_tx_rate = 50;
5424 	}
5425 
5426 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5427 	credits = max_tx_rate;
5428 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5429 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5430 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5431 	if (ret)
5432 		dev_err(&pf->pdev->dev,
5433 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5434 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5435 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5436 	return ret;
5437 }
5438 
5439 /**
5440  * i40e_remove_queue_channels - Remove queue channels for the TCs
5441  * @vsi: VSI to be configured
5442  *
5443  * Remove queue channels for the TCs
5444  **/
5445 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5446 {
5447 	enum i40e_admin_queue_err last_aq_status;
5448 	struct i40e_cloud_filter *cfilter;
5449 	struct i40e_channel *ch, *ch_tmp;
5450 	struct i40e_pf *pf = vsi->back;
5451 	struct hlist_node *node;
5452 	int ret, i;
5453 
5454 	/* Reset rss size that was stored when reconfiguring rss for
5455 	 * channel VSIs with non-power-of-2 queue count.
5456 	 */
5457 	vsi->current_rss_size = 0;
5458 
5459 	/* perform cleanup for channels if they exist */
5460 	if (list_empty(&vsi->ch_list))
5461 		return;
5462 
5463 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5464 		struct i40e_vsi *p_vsi;
5465 
5466 		list_del(&ch->list);
5467 		p_vsi = ch->parent_vsi;
5468 		if (!p_vsi || !ch->initialized) {
5469 			kfree(ch);
5470 			continue;
5471 		}
5472 		/* Reset queue contexts */
5473 		for (i = 0; i < ch->num_queue_pairs; i++) {
5474 			struct i40e_ring *tx_ring, *rx_ring;
5475 			u16 pf_q;
5476 
5477 			pf_q = ch->base_queue + i;
5478 			tx_ring = vsi->tx_rings[pf_q];
5479 			tx_ring->ch = NULL;
5480 
5481 			rx_ring = vsi->rx_rings[pf_q];
5482 			rx_ring->ch = NULL;
5483 		}
5484 
5485 		/* Reset BW configured for this VSI via mqprio */
5486 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5487 		if (ret)
5488 			dev_info(&vsi->back->pdev->dev,
5489 				 "Failed to reset tx rate for ch->seid %u\n",
5490 				 ch->seid);
5491 
5492 		/* delete cloud filters associated with this channel */
5493 		hlist_for_each_entry_safe(cfilter, node,
5494 					  &pf->cloud_filter_list, cloud_node) {
5495 			if (cfilter->seid != ch->seid)
5496 				continue;
5497 
5498 			hash_del(&cfilter->cloud_node);
5499 			if (cfilter->dst_port)
5500 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5501 									cfilter,
5502 									false);
5503 			else
5504 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5505 								false);
5506 			last_aq_status = pf->hw.aq.asq_last_status;
5507 			if (ret)
5508 				dev_info(&pf->pdev->dev,
5509 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5510 					 i40e_stat_str(&pf->hw, ret),
5511 					 i40e_aq_str(&pf->hw, last_aq_status));
5512 			kfree(cfilter);
5513 		}
5514 
5515 		/* delete VSI from FW */
5516 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5517 					     NULL);
5518 		if (ret)
5519 			dev_err(&vsi->back->pdev->dev,
5520 				"unable to remove channel (%d) for parent VSI(%d)\n",
5521 				ch->seid, p_vsi->seid);
5522 		kfree(ch);
5523 	}
5524 	INIT_LIST_HEAD(&vsi->ch_list);
5525 }
5526 
5527 /**
5528  * i40e_is_any_channel - channel exist or not
5529  * @vsi: ptr to VSI to which channels are associated with
5530  *
5531  * Returns true or false if channel(s) exist for associated VSI or not
5532  **/
5533 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5534 {
5535 	struct i40e_channel *ch, *ch_tmp;
5536 
5537 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5538 		if (ch->initialized)
5539 			return true;
5540 	}
5541 
5542 	return false;
5543 }
5544 
5545 /**
5546  * i40e_get_max_queues_for_channel
5547  * @vsi: ptr to VSI to which channels are associated with
5548  *
5549  * Helper function which returns max value among the queue counts set on the
5550  * channels/TCs created.
5551  **/
5552 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5553 {
5554 	struct i40e_channel *ch, *ch_tmp;
5555 	int max = 0;
5556 
5557 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5558 		if (!ch->initialized)
5559 			continue;
5560 		if (ch->num_queue_pairs > max)
5561 			max = ch->num_queue_pairs;
5562 	}
5563 
5564 	return max;
5565 }
5566 
5567 /**
5568  * i40e_validate_num_queues - validate num_queues w.r.t channel
5569  * @pf: ptr to PF device
5570  * @num_queues: number of queues
5571  * @vsi: the parent VSI
5572  * @reconfig_rss: indicates should the RSS be reconfigured or not
5573  *
5574  * This function validates number of queues in the context of new channel
5575  * which is being established and determines if RSS should be reconfigured
5576  * or not for parent VSI.
5577  **/
5578 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5579 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5580 {
5581 	int max_ch_queues;
5582 
5583 	if (!reconfig_rss)
5584 		return -EINVAL;
5585 
5586 	*reconfig_rss = false;
5587 	if (vsi->current_rss_size) {
5588 		if (num_queues > vsi->current_rss_size) {
5589 			dev_dbg(&pf->pdev->dev,
5590 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5591 				num_queues, vsi->current_rss_size);
5592 			return -EINVAL;
5593 		} else if ((num_queues < vsi->current_rss_size) &&
5594 			   (!is_power_of_2(num_queues))) {
5595 			dev_dbg(&pf->pdev->dev,
5596 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5597 				num_queues, vsi->current_rss_size);
5598 			return -EINVAL;
5599 		}
5600 	}
5601 
5602 	if (!is_power_of_2(num_queues)) {
5603 		/* Find the max num_queues configured for channel if channel
5604 		 * exist.
5605 		 * if channel exist, then enforce 'num_queues' to be more than
5606 		 * max ever queues configured for channel.
5607 		 */
5608 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5609 		if (num_queues < max_ch_queues) {
5610 			dev_dbg(&pf->pdev->dev,
5611 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5612 				num_queues, max_ch_queues);
5613 			return -EINVAL;
5614 		}
5615 		*reconfig_rss = true;
5616 	}
5617 
5618 	return 0;
5619 }
5620 
5621 /**
5622  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5623  * @vsi: the VSI being setup
5624  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5625  *
5626  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5627  **/
5628 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5629 {
5630 	struct i40e_pf *pf = vsi->back;
5631 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5632 	struct i40e_hw *hw = &pf->hw;
5633 	int local_rss_size;
5634 	u8 *lut;
5635 	int ret;
5636 
5637 	if (!vsi->rss_size)
5638 		return -EINVAL;
5639 
5640 	if (rss_size > vsi->rss_size)
5641 		return -EINVAL;
5642 
5643 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5644 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5645 	if (!lut)
5646 		return -ENOMEM;
5647 
5648 	/* Ignoring user configured lut if there is one */
5649 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5650 
5651 	/* Use user configured hash key if there is one, otherwise
5652 	 * use default.
5653 	 */
5654 	if (vsi->rss_hkey_user)
5655 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5656 	else
5657 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5658 
5659 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5660 	if (ret) {
5661 		dev_info(&pf->pdev->dev,
5662 			 "Cannot set RSS lut, err %s aq_err %s\n",
5663 			 i40e_stat_str(hw, ret),
5664 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5665 		kfree(lut);
5666 		return ret;
5667 	}
5668 	kfree(lut);
5669 
5670 	/* Do the update w.r.t. storing rss_size */
5671 	if (!vsi->orig_rss_size)
5672 		vsi->orig_rss_size = vsi->rss_size;
5673 	vsi->current_rss_size = local_rss_size;
5674 
5675 	return ret;
5676 }
5677 
5678 /**
5679  * i40e_channel_setup_queue_map - Setup a channel queue map
5680  * @pf: ptr to PF device
5681  * @vsi: the VSI being setup
5682  * @ctxt: VSI context structure
5683  * @ch: ptr to channel structure
5684  *
5685  * Setup queue map for a specific channel
5686  **/
5687 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5688 					 struct i40e_vsi_context *ctxt,
5689 					 struct i40e_channel *ch)
5690 {
5691 	u16 qcount, qmap, sections = 0;
5692 	u8 offset = 0;
5693 	int pow;
5694 
5695 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5696 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5697 
5698 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5699 	ch->num_queue_pairs = qcount;
5700 
5701 	/* find the next higher power-of-2 of num queue pairs */
5702 	pow = ilog2(qcount);
5703 	if (!is_power_of_2(qcount))
5704 		pow++;
5705 
5706 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5707 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5708 
5709 	/* Setup queue TC[0].qmap for given VSI context */
5710 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5711 
5712 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5713 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5714 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5715 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5716 }
5717 
5718 /**
5719  * i40e_add_channel - add a channel by adding VSI
5720  * @pf: ptr to PF device
5721  * @uplink_seid: underlying HW switching element (VEB) ID
5722  * @ch: ptr to channel structure
5723  *
5724  * Add a channel (VSI) using add_vsi and queue_map
5725  **/
5726 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5727 			    struct i40e_channel *ch)
5728 {
5729 	struct i40e_hw *hw = &pf->hw;
5730 	struct i40e_vsi_context ctxt;
5731 	u8 enabled_tc = 0x1; /* TC0 enabled */
5732 	int ret;
5733 
5734 	if (ch->type != I40E_VSI_VMDQ2) {
5735 		dev_info(&pf->pdev->dev,
5736 			 "add new vsi failed, ch->type %d\n", ch->type);
5737 		return -EINVAL;
5738 	}
5739 
5740 	memset(&ctxt, 0, sizeof(ctxt));
5741 	ctxt.pf_num = hw->pf_id;
5742 	ctxt.vf_num = 0;
5743 	ctxt.uplink_seid = uplink_seid;
5744 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5745 	if (ch->type == I40E_VSI_VMDQ2)
5746 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5747 
5748 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5749 		ctxt.info.valid_sections |=
5750 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5751 		ctxt.info.switch_id =
5752 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5753 	}
5754 
5755 	/* Set queue map for a given VSI context */
5756 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5757 
5758 	/* Now time to create VSI */
5759 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5760 	if (ret) {
5761 		dev_info(&pf->pdev->dev,
5762 			 "add new vsi failed, err %s aq_err %s\n",
5763 			 i40e_stat_str(&pf->hw, ret),
5764 			 i40e_aq_str(&pf->hw,
5765 				     pf->hw.aq.asq_last_status));
5766 		return -ENOENT;
5767 	}
5768 
5769 	/* Success, update channel */
5770 	ch->enabled_tc = enabled_tc;
5771 	ch->seid = ctxt.seid;
5772 	ch->vsi_number = ctxt.vsi_number;
5773 	ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5774 
5775 	/* copy just the sections touched not the entire info
5776 	 * since not all sections are valid as returned by
5777 	 * update vsi params
5778 	 */
5779 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5780 	memcpy(&ch->info.queue_mapping,
5781 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5782 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5783 	       sizeof(ctxt.info.tc_mapping));
5784 
5785 	return 0;
5786 }
5787 
5788 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5789 				  u8 *bw_share)
5790 {
5791 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5792 	i40e_status ret;
5793 	int i;
5794 
5795 	bw_data.tc_valid_bits = ch->enabled_tc;
5796 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5797 		bw_data.tc_bw_credits[i] = bw_share[i];
5798 
5799 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5800 				       &bw_data, NULL);
5801 	if (ret) {
5802 		dev_info(&vsi->back->pdev->dev,
5803 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5804 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5805 		return -EINVAL;
5806 	}
5807 
5808 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5809 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5810 
5811 	return 0;
5812 }
5813 
5814 /**
5815  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5816  * @pf: ptr to PF device
5817  * @vsi: the VSI being setup
5818  * @ch: ptr to channel structure
5819  *
5820  * Configure TX rings associated with channel (VSI) since queues are being
5821  * from parent VSI.
5822  **/
5823 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5824 				       struct i40e_vsi *vsi,
5825 				       struct i40e_channel *ch)
5826 {
5827 	i40e_status ret;
5828 	int i;
5829 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5830 
5831 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5832 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5833 		if (ch->enabled_tc & BIT(i))
5834 			bw_share[i] = 1;
5835 	}
5836 
5837 	/* configure BW for new VSI */
5838 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
5839 	if (ret) {
5840 		dev_info(&vsi->back->pdev->dev,
5841 			 "Failed configuring TC map %d for channel (seid %u)\n",
5842 			 ch->enabled_tc, ch->seid);
5843 		return ret;
5844 	}
5845 
5846 	for (i = 0; i < ch->num_queue_pairs; i++) {
5847 		struct i40e_ring *tx_ring, *rx_ring;
5848 		u16 pf_q;
5849 
5850 		pf_q = ch->base_queue + i;
5851 
5852 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
5853 		 * context
5854 		 */
5855 		tx_ring = vsi->tx_rings[pf_q];
5856 		tx_ring->ch = ch;
5857 
5858 		/* Get the RX ring ptr */
5859 		rx_ring = vsi->rx_rings[pf_q];
5860 		rx_ring->ch = ch;
5861 	}
5862 
5863 	return 0;
5864 }
5865 
5866 /**
5867  * i40e_setup_hw_channel - setup new channel
5868  * @pf: ptr to PF device
5869  * @vsi: the VSI being setup
5870  * @ch: ptr to channel structure
5871  * @uplink_seid: underlying HW switching element (VEB) ID
5872  * @type: type of channel to be created (VMDq2/VF)
5873  *
5874  * Setup new channel (VSI) based on specified type (VMDq2/VF)
5875  * and configures TX rings accordingly
5876  **/
5877 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5878 					struct i40e_vsi *vsi,
5879 					struct i40e_channel *ch,
5880 					u16 uplink_seid, u8 type)
5881 {
5882 	int ret;
5883 
5884 	ch->initialized = false;
5885 	ch->base_queue = vsi->next_base_queue;
5886 	ch->type = type;
5887 
5888 	/* Proceed with creation of channel (VMDq2) VSI */
5889 	ret = i40e_add_channel(pf, uplink_seid, ch);
5890 	if (ret) {
5891 		dev_info(&pf->pdev->dev,
5892 			 "failed to add_channel using uplink_seid %u\n",
5893 			 uplink_seid);
5894 		return ret;
5895 	}
5896 
5897 	/* Mark the successful creation of channel */
5898 	ch->initialized = true;
5899 
5900 	/* Reconfigure TX queues using QTX_CTL register */
5901 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5902 	if (ret) {
5903 		dev_info(&pf->pdev->dev,
5904 			 "failed to configure TX rings for channel %u\n",
5905 			 ch->seid);
5906 		return ret;
5907 	}
5908 
5909 	/* update 'next_base_queue' */
5910 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
5911 	dev_dbg(&pf->pdev->dev,
5912 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
5913 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
5914 		ch->num_queue_pairs,
5915 		vsi->next_base_queue);
5916 	return ret;
5917 }
5918 
5919 /**
5920  * i40e_setup_channel - setup new channel using uplink element
5921  * @pf: ptr to PF device
5922  * @type: type of channel to be created (VMDq2/VF)
5923  * @uplink_seid: underlying HW switching element (VEB) ID
5924  * @ch: ptr to channel structure
5925  *
5926  * Setup new channel (VSI) based on specified type (VMDq2/VF)
5927  * and uplink switching element (uplink_seid)
5928  **/
5929 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
5930 			       struct i40e_channel *ch)
5931 {
5932 	u8 vsi_type;
5933 	u16 seid;
5934 	int ret;
5935 
5936 	if (vsi->type == I40E_VSI_MAIN) {
5937 		vsi_type = I40E_VSI_VMDQ2;
5938 	} else {
5939 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
5940 			vsi->type);
5941 		return false;
5942 	}
5943 
5944 	/* underlying switching element */
5945 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5946 
5947 	/* create channel (VSI), configure TX rings */
5948 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
5949 	if (ret) {
5950 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
5951 		return false;
5952 	}
5953 
5954 	return ch->initialized ? true : false;
5955 }
5956 
5957 /**
5958  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
5959  * @vsi: ptr to VSI which has PF backing
5960  *
5961  * Sets up switch mode correctly if it needs to be changed and perform
5962  * what are allowed modes.
5963  **/
5964 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
5965 {
5966 	u8 mode;
5967 	struct i40e_pf *pf = vsi->back;
5968 	struct i40e_hw *hw = &pf->hw;
5969 	int ret;
5970 
5971 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
5972 	if (ret)
5973 		return -EINVAL;
5974 
5975 	if (hw->dev_caps.switch_mode) {
5976 		/* if switch mode is set, support mode2 (non-tunneled for
5977 		 * cloud filter) for now
5978 		 */
5979 		u32 switch_mode = hw->dev_caps.switch_mode &
5980 				  I40E_SWITCH_MODE_MASK;
5981 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
5982 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
5983 				return 0;
5984 			dev_err(&pf->pdev->dev,
5985 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
5986 				hw->dev_caps.switch_mode);
5987 			return -EINVAL;
5988 		}
5989 	}
5990 
5991 	/* Set Bit 7 to be valid */
5992 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
5993 
5994 	/* Set L4type for TCP support */
5995 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
5996 
5997 	/* Set cloud filter mode */
5998 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
5999 
6000 	/* Prep mode field for set_switch_config */
6001 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6002 					pf->last_sw_conf_valid_flags,
6003 					mode, NULL);
6004 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6005 		dev_err(&pf->pdev->dev,
6006 			"couldn't set switch config bits, err %s aq_err %s\n",
6007 			i40e_stat_str(hw, ret),
6008 			i40e_aq_str(hw,
6009 				    hw->aq.asq_last_status));
6010 
6011 	return ret;
6012 }
6013 
6014 /**
6015  * i40e_create_queue_channel - function to create channel
6016  * @vsi: VSI to be configured
6017  * @ch: ptr to channel (it contains channel specific params)
6018  *
6019  * This function creates channel (VSI) using num_queues specified by user,
6020  * reconfigs RSS if needed.
6021  **/
6022 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6023 			      struct i40e_channel *ch)
6024 {
6025 	struct i40e_pf *pf = vsi->back;
6026 	bool reconfig_rss;
6027 	int err;
6028 
6029 	if (!ch)
6030 		return -EINVAL;
6031 
6032 	if (!ch->num_queue_pairs) {
6033 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6034 			ch->num_queue_pairs);
6035 		return -EINVAL;
6036 	}
6037 
6038 	/* validate user requested num_queues for channel */
6039 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6040 				       &reconfig_rss);
6041 	if (err) {
6042 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6043 			 ch->num_queue_pairs);
6044 		return -EINVAL;
6045 	}
6046 
6047 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6048 	 * VSI to be added switch to VEB mode.
6049 	 */
6050 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6051 	    (!i40e_is_any_channel(vsi))) {
6052 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6053 			dev_dbg(&pf->pdev->dev,
6054 				"Failed to create channel. Override queues (%u) not power of 2\n",
6055 				vsi->tc_config.tc_info[0].qcount);
6056 			return -EINVAL;
6057 		}
6058 
6059 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6060 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6061 
6062 			if (vsi->type == I40E_VSI_MAIN) {
6063 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6064 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6065 						      true);
6066 				else
6067 					i40e_do_reset_safe(pf,
6068 							   I40E_PF_RESET_FLAG);
6069 			}
6070 		}
6071 		/* now onwards for main VSI, number of queues will be value
6072 		 * of TC0's queue count
6073 		 */
6074 	}
6075 
6076 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6077 	 * it should be more than num_queues
6078 	 */
6079 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6080 		dev_dbg(&pf->pdev->dev,
6081 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6082 			vsi->cnt_q_avail, ch->num_queue_pairs);
6083 		return -EINVAL;
6084 	}
6085 
6086 	/* reconfig_rss only if vsi type is MAIN_VSI */
6087 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6088 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6089 		if (err) {
6090 			dev_info(&pf->pdev->dev,
6091 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6092 				 ch->num_queue_pairs);
6093 			return -EINVAL;
6094 		}
6095 	}
6096 
6097 	if (!i40e_setup_channel(pf, vsi, ch)) {
6098 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6099 		return -EINVAL;
6100 	}
6101 
6102 	dev_info(&pf->pdev->dev,
6103 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6104 		 ch->seid, ch->num_queue_pairs);
6105 
6106 	/* configure VSI for BW limit */
6107 	if (ch->max_tx_rate) {
6108 		u64 credits = ch->max_tx_rate;
6109 
6110 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6111 			return -EINVAL;
6112 
6113 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6114 		dev_dbg(&pf->pdev->dev,
6115 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6116 			ch->max_tx_rate,
6117 			credits,
6118 			ch->seid);
6119 	}
6120 
6121 	/* in case of VF, this will be main SRIOV VSI */
6122 	ch->parent_vsi = vsi;
6123 
6124 	/* and update main_vsi's count for queue_available to use */
6125 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6126 
6127 	return 0;
6128 }
6129 
6130 /**
6131  * i40e_configure_queue_channels - Add queue channel for the given TCs
6132  * @vsi: VSI to be configured
6133  *
6134  * Configures queue channel mapping to the given TCs
6135  **/
6136 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6137 {
6138 	struct i40e_channel *ch;
6139 	u64 max_rate = 0;
6140 	int ret = 0, i;
6141 
6142 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6143 	vsi->tc_seid_map[0] = vsi->seid;
6144 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6145 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6146 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6147 			if (!ch) {
6148 				ret = -ENOMEM;
6149 				goto err_free;
6150 			}
6151 
6152 			INIT_LIST_HEAD(&ch->list);
6153 			ch->num_queue_pairs =
6154 				vsi->tc_config.tc_info[i].qcount;
6155 			ch->base_queue =
6156 				vsi->tc_config.tc_info[i].qoffset;
6157 
6158 			/* Bandwidth limit through tc interface is in bytes/s,
6159 			 * change to Mbit/s
6160 			 */
6161 			max_rate = vsi->mqprio_qopt.max_rate[i];
6162 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6163 			ch->max_tx_rate = max_rate;
6164 
6165 			list_add_tail(&ch->list, &vsi->ch_list);
6166 
6167 			ret = i40e_create_queue_channel(vsi, ch);
6168 			if (ret) {
6169 				dev_err(&vsi->back->pdev->dev,
6170 					"Failed creating queue channel with TC%d: queues %d\n",
6171 					i, ch->num_queue_pairs);
6172 				goto err_free;
6173 			}
6174 			vsi->tc_seid_map[i] = ch->seid;
6175 		}
6176 	}
6177 	return ret;
6178 
6179 err_free:
6180 	i40e_remove_queue_channels(vsi);
6181 	return ret;
6182 }
6183 
6184 /**
6185  * i40e_veb_config_tc - Configure TCs for given VEB
6186  * @veb: given VEB
6187  * @enabled_tc: TC bitmap
6188  *
6189  * Configures given TC bitmap for VEB (switching) element
6190  **/
6191 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6192 {
6193 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6194 	struct i40e_pf *pf = veb->pf;
6195 	int ret = 0;
6196 	int i;
6197 
6198 	/* No TCs or already enabled TCs just return */
6199 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6200 		return ret;
6201 
6202 	bw_data.tc_valid_bits = enabled_tc;
6203 	/* bw_data.absolute_credits is not set (relative) */
6204 
6205 	/* Enable ETS TCs with equal BW Share for now */
6206 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6207 		if (enabled_tc & BIT(i))
6208 			bw_data.tc_bw_share_credits[i] = 1;
6209 	}
6210 
6211 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6212 						   &bw_data, NULL);
6213 	if (ret) {
6214 		dev_info(&pf->pdev->dev,
6215 			 "VEB bw config failed, err %s aq_err %s\n",
6216 			 i40e_stat_str(&pf->hw, ret),
6217 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6218 		goto out;
6219 	}
6220 
6221 	/* Update the BW information */
6222 	ret = i40e_veb_get_bw_info(veb);
6223 	if (ret) {
6224 		dev_info(&pf->pdev->dev,
6225 			 "Failed getting veb bw config, err %s aq_err %s\n",
6226 			 i40e_stat_str(&pf->hw, ret),
6227 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6228 	}
6229 
6230 out:
6231 	return ret;
6232 }
6233 
6234 #ifdef CONFIG_I40E_DCB
6235 /**
6236  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6237  * @pf: PF struct
6238  *
6239  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6240  * the caller would've quiesce all the VSIs before calling
6241  * this function
6242  **/
6243 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6244 {
6245 	u8 tc_map = 0;
6246 	int ret;
6247 	u8 v;
6248 
6249 	/* Enable the TCs available on PF to all VEBs */
6250 	tc_map = i40e_pf_get_tc_map(pf);
6251 	for (v = 0; v < I40E_MAX_VEB; v++) {
6252 		if (!pf->veb[v])
6253 			continue;
6254 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6255 		if (ret) {
6256 			dev_info(&pf->pdev->dev,
6257 				 "Failed configuring TC for VEB seid=%d\n",
6258 				 pf->veb[v]->seid);
6259 			/* Will try to configure as many components */
6260 		}
6261 	}
6262 
6263 	/* Update each VSI */
6264 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6265 		if (!pf->vsi[v])
6266 			continue;
6267 
6268 		/* - Enable all TCs for the LAN VSI
6269 		 * - For all others keep them at TC0 for now
6270 		 */
6271 		if (v == pf->lan_vsi)
6272 			tc_map = i40e_pf_get_tc_map(pf);
6273 		else
6274 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6275 
6276 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6277 		if (ret) {
6278 			dev_info(&pf->pdev->dev,
6279 				 "Failed configuring TC for VSI seid=%d\n",
6280 				 pf->vsi[v]->seid);
6281 			/* Will try to configure as many components */
6282 		} else {
6283 			/* Re-configure VSI vectors based on updated TC map */
6284 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6285 			if (pf->vsi[v]->netdev)
6286 				i40e_dcbnl_set_all(pf->vsi[v]);
6287 		}
6288 	}
6289 }
6290 
6291 /**
6292  * i40e_resume_port_tx - Resume port Tx
6293  * @pf: PF struct
6294  *
6295  * Resume a port's Tx and issue a PF reset in case of failure to
6296  * resume.
6297  **/
6298 static int i40e_resume_port_tx(struct i40e_pf *pf)
6299 {
6300 	struct i40e_hw *hw = &pf->hw;
6301 	int ret;
6302 
6303 	ret = i40e_aq_resume_port_tx(hw, NULL);
6304 	if (ret) {
6305 		dev_info(&pf->pdev->dev,
6306 			 "Resume Port Tx failed, err %s aq_err %s\n",
6307 			  i40e_stat_str(&pf->hw, ret),
6308 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6309 		/* Schedule PF reset to recover */
6310 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6311 		i40e_service_event_schedule(pf);
6312 	}
6313 
6314 	return ret;
6315 }
6316 
6317 /**
6318  * i40e_init_pf_dcb - Initialize DCB configuration
6319  * @pf: PF being configured
6320  *
6321  * Query the current DCB configuration and cache it
6322  * in the hardware structure
6323  **/
6324 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6325 {
6326 	struct i40e_hw *hw = &pf->hw;
6327 	int err = 0;
6328 
6329 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6330 	 * Also do not enable DCBx if FW LLDP agent is disabled
6331 	 */
6332 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6333 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
6334 		goto out;
6335 
6336 	/* Get the initial DCB configuration */
6337 	err = i40e_init_dcb(hw);
6338 	if (!err) {
6339 		/* Device/Function is not DCBX capable */
6340 		if ((!hw->func_caps.dcb) ||
6341 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6342 			dev_info(&pf->pdev->dev,
6343 				 "DCBX offload is not supported or is disabled for this PF.\n");
6344 		} else {
6345 			/* When status is not DISABLED then DCBX in FW */
6346 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6347 				       DCB_CAP_DCBX_VER_IEEE;
6348 
6349 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6350 			/* Enable DCB tagging only when more than one TC
6351 			 * or explicitly disable if only one TC
6352 			 */
6353 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6354 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6355 			else
6356 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6357 			dev_dbg(&pf->pdev->dev,
6358 				"DCBX offload is supported for this PF.\n");
6359 		}
6360 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6361 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6362 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6363 	} else {
6364 		dev_info(&pf->pdev->dev,
6365 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6366 			 i40e_stat_str(&pf->hw, err),
6367 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6368 	}
6369 
6370 out:
6371 	return err;
6372 }
6373 #endif /* CONFIG_I40E_DCB */
6374 #define SPEED_SIZE 14
6375 #define FC_SIZE 8
6376 /**
6377  * i40e_print_link_message - print link up or down
6378  * @vsi: the VSI for which link needs a message
6379  */
6380 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6381 {
6382 	enum i40e_aq_link_speed new_speed;
6383 	struct i40e_pf *pf = vsi->back;
6384 	char *speed = "Unknown";
6385 	char *fc = "Unknown";
6386 	char *fec = "";
6387 	char *req_fec = "";
6388 	char *an = "";
6389 
6390 	new_speed = pf->hw.phy.link_info.link_speed;
6391 
6392 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6393 		return;
6394 	vsi->current_isup = isup;
6395 	vsi->current_speed = new_speed;
6396 	if (!isup) {
6397 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6398 		return;
6399 	}
6400 
6401 	/* Warn user if link speed on NPAR enabled partition is not at
6402 	 * least 10GB
6403 	 */
6404 	if (pf->hw.func_caps.npar_enable &&
6405 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6406 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6407 		netdev_warn(vsi->netdev,
6408 			    "The partition detected link speed that is less than 10Gbps\n");
6409 
6410 	switch (pf->hw.phy.link_info.link_speed) {
6411 	case I40E_LINK_SPEED_40GB:
6412 		speed = "40 G";
6413 		break;
6414 	case I40E_LINK_SPEED_20GB:
6415 		speed = "20 G";
6416 		break;
6417 	case I40E_LINK_SPEED_25GB:
6418 		speed = "25 G";
6419 		break;
6420 	case I40E_LINK_SPEED_10GB:
6421 		speed = "10 G";
6422 		break;
6423 	case I40E_LINK_SPEED_1GB:
6424 		speed = "1000 M";
6425 		break;
6426 	case I40E_LINK_SPEED_100MB:
6427 		speed = "100 M";
6428 		break;
6429 	default:
6430 		break;
6431 	}
6432 
6433 	switch (pf->hw.fc.current_mode) {
6434 	case I40E_FC_FULL:
6435 		fc = "RX/TX";
6436 		break;
6437 	case I40E_FC_TX_PAUSE:
6438 		fc = "TX";
6439 		break;
6440 	case I40E_FC_RX_PAUSE:
6441 		fc = "RX";
6442 		break;
6443 	default:
6444 		fc = "None";
6445 		break;
6446 	}
6447 
6448 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6449 		req_fec = ", Requested FEC: None";
6450 		fec = ", FEC: None";
6451 		an = ", Autoneg: False";
6452 
6453 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6454 			an = ", Autoneg: True";
6455 
6456 		if (pf->hw.phy.link_info.fec_info &
6457 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6458 			fec = ", FEC: CL74 FC-FEC/BASE-R";
6459 		else if (pf->hw.phy.link_info.fec_info &
6460 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6461 			fec = ", FEC: CL108 RS-FEC";
6462 
6463 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6464 		 * both RS and FC are requested
6465 		 */
6466 		if (vsi->back->hw.phy.link_info.req_fec_info &
6467 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6468 			if (vsi->back->hw.phy.link_info.req_fec_info &
6469 			    I40E_AQ_REQUEST_FEC_RS)
6470 				req_fec = ", Requested FEC: CL108 RS-FEC";
6471 			else
6472 				req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6473 		}
6474 	}
6475 
6476 	netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6477 		    speed, req_fec, fec, an, fc);
6478 }
6479 
6480 /**
6481  * i40e_up_complete - Finish the last steps of bringing up a connection
6482  * @vsi: the VSI being configured
6483  **/
6484 static int i40e_up_complete(struct i40e_vsi *vsi)
6485 {
6486 	struct i40e_pf *pf = vsi->back;
6487 	int err;
6488 
6489 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6490 		i40e_vsi_configure_msix(vsi);
6491 	else
6492 		i40e_configure_msi_and_legacy(vsi);
6493 
6494 	/* start rings */
6495 	err = i40e_vsi_start_rings(vsi);
6496 	if (err)
6497 		return err;
6498 
6499 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6500 	i40e_napi_enable_all(vsi);
6501 	i40e_vsi_enable_irq(vsi);
6502 
6503 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6504 	    (vsi->netdev)) {
6505 		i40e_print_link_message(vsi, true);
6506 		netif_tx_start_all_queues(vsi->netdev);
6507 		netif_carrier_on(vsi->netdev);
6508 	}
6509 
6510 	/* replay FDIR SB filters */
6511 	if (vsi->type == I40E_VSI_FDIR) {
6512 		/* reset fd counters */
6513 		pf->fd_add_err = 0;
6514 		pf->fd_atr_cnt = 0;
6515 		i40e_fdir_filter_restore(vsi);
6516 	}
6517 
6518 	/* On the next run of the service_task, notify any clients of the new
6519 	 * opened netdev
6520 	 */
6521 	pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
6522 	i40e_service_event_schedule(pf);
6523 
6524 	return 0;
6525 }
6526 
6527 /**
6528  * i40e_vsi_reinit_locked - Reset the VSI
6529  * @vsi: the VSI being configured
6530  *
6531  * Rebuild the ring structs after some configuration
6532  * has changed, e.g. MTU size.
6533  **/
6534 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6535 {
6536 	struct i40e_pf *pf = vsi->back;
6537 
6538 	WARN_ON(in_interrupt());
6539 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6540 		usleep_range(1000, 2000);
6541 	i40e_down(vsi);
6542 
6543 	i40e_up(vsi);
6544 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6545 }
6546 
6547 /**
6548  * i40e_up - Bring the connection back up after being down
6549  * @vsi: the VSI being configured
6550  **/
6551 int i40e_up(struct i40e_vsi *vsi)
6552 {
6553 	int err;
6554 
6555 	err = i40e_vsi_configure(vsi);
6556 	if (!err)
6557 		err = i40e_up_complete(vsi);
6558 
6559 	return err;
6560 }
6561 
6562 /**
6563  * i40e_down - Shutdown the connection processing
6564  * @vsi: the VSI being stopped
6565  **/
6566 void i40e_down(struct i40e_vsi *vsi)
6567 {
6568 	int i;
6569 
6570 	/* It is assumed that the caller of this function
6571 	 * sets the vsi->state __I40E_VSI_DOWN bit.
6572 	 */
6573 	if (vsi->netdev) {
6574 		netif_carrier_off(vsi->netdev);
6575 		netif_tx_disable(vsi->netdev);
6576 	}
6577 	i40e_vsi_disable_irq(vsi);
6578 	i40e_vsi_stop_rings(vsi);
6579 	i40e_napi_disable_all(vsi);
6580 
6581 	for (i = 0; i < vsi->num_queue_pairs; i++) {
6582 		i40e_clean_tx_ring(vsi->tx_rings[i]);
6583 		if (i40e_enabled_xdp_vsi(vsi))
6584 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
6585 		i40e_clean_rx_ring(vsi->rx_rings[i]);
6586 	}
6587 
6588 }
6589 
6590 /**
6591  * i40e_validate_mqprio_qopt- validate queue mapping info
6592  * @vsi: the VSI being configured
6593  * @mqprio_qopt: queue parametrs
6594  **/
6595 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6596 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
6597 {
6598 	u64 sum_max_rate = 0;
6599 	u64 max_rate = 0;
6600 	int i;
6601 
6602 	if (mqprio_qopt->qopt.offset[0] != 0 ||
6603 	    mqprio_qopt->qopt.num_tc < 1 ||
6604 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6605 		return -EINVAL;
6606 	for (i = 0; ; i++) {
6607 		if (!mqprio_qopt->qopt.count[i])
6608 			return -EINVAL;
6609 		if (mqprio_qopt->min_rate[i]) {
6610 			dev_err(&vsi->back->pdev->dev,
6611 				"Invalid min tx rate (greater than 0) specified\n");
6612 			return -EINVAL;
6613 		}
6614 		max_rate = mqprio_qopt->max_rate[i];
6615 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6616 		sum_max_rate += max_rate;
6617 
6618 		if (i >= mqprio_qopt->qopt.num_tc - 1)
6619 			break;
6620 		if (mqprio_qopt->qopt.offset[i + 1] !=
6621 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6622 			return -EINVAL;
6623 	}
6624 	if (vsi->num_queue_pairs <
6625 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6626 		return -EINVAL;
6627 	}
6628 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
6629 		dev_err(&vsi->back->pdev->dev,
6630 			"Invalid max tx rate specified\n");
6631 		return -EINVAL;
6632 	}
6633 	return 0;
6634 }
6635 
6636 /**
6637  * i40e_vsi_set_default_tc_config - set default values for tc configuration
6638  * @vsi: the VSI being configured
6639  **/
6640 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6641 {
6642 	u16 qcount;
6643 	int i;
6644 
6645 	/* Only TC0 is enabled */
6646 	vsi->tc_config.numtc = 1;
6647 	vsi->tc_config.enabled_tc = 1;
6648 	qcount = min_t(int, vsi->alloc_queue_pairs,
6649 		       i40e_pf_get_max_q_per_tc(vsi->back));
6650 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6651 		/* For the TC that is not enabled set the offset to to default
6652 		 * queue and allocate one queue for the given TC.
6653 		 */
6654 		vsi->tc_config.tc_info[i].qoffset = 0;
6655 		if (i == 0)
6656 			vsi->tc_config.tc_info[i].qcount = qcount;
6657 		else
6658 			vsi->tc_config.tc_info[i].qcount = 1;
6659 		vsi->tc_config.tc_info[i].netdev_tc = 0;
6660 	}
6661 }
6662 
6663 /**
6664  * i40e_setup_tc - configure multiple traffic classes
6665  * @netdev: net device to configure
6666  * @type_data: tc offload data
6667  **/
6668 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
6669 {
6670 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
6671 	struct i40e_netdev_priv *np = netdev_priv(netdev);
6672 	struct i40e_vsi *vsi = np->vsi;
6673 	struct i40e_pf *pf = vsi->back;
6674 	u8 enabled_tc = 0, num_tc, hw;
6675 	bool need_reset = false;
6676 	int ret = -EINVAL;
6677 	u16 mode;
6678 	int i;
6679 
6680 	num_tc = mqprio_qopt->qopt.num_tc;
6681 	hw = mqprio_qopt->qopt.hw;
6682 	mode = mqprio_qopt->mode;
6683 	if (!hw) {
6684 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6685 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
6686 		goto config_tc;
6687 	}
6688 
6689 	/* Check if MFP enabled */
6690 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6691 		netdev_info(netdev,
6692 			    "Configuring TC not supported in MFP mode\n");
6693 		return ret;
6694 	}
6695 	switch (mode) {
6696 	case TC_MQPRIO_MODE_DCB:
6697 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6698 
6699 		/* Check if DCB enabled to continue */
6700 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6701 			netdev_info(netdev,
6702 				    "DCB is not enabled for adapter\n");
6703 			return ret;
6704 		}
6705 
6706 		/* Check whether tc count is within enabled limit */
6707 		if (num_tc > i40e_pf_get_num_tc(pf)) {
6708 			netdev_info(netdev,
6709 				    "TC count greater than enabled on link for adapter\n");
6710 			return ret;
6711 		}
6712 		break;
6713 	case TC_MQPRIO_MODE_CHANNEL:
6714 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
6715 			netdev_info(netdev,
6716 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
6717 			return ret;
6718 		}
6719 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6720 			return ret;
6721 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
6722 		if (ret)
6723 			return ret;
6724 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
6725 		       sizeof(*mqprio_qopt));
6726 		pf->flags |= I40E_FLAG_TC_MQPRIO;
6727 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6728 		break;
6729 	default:
6730 		return -EINVAL;
6731 	}
6732 
6733 config_tc:
6734 	/* Generate TC map for number of tc requested */
6735 	for (i = 0; i < num_tc; i++)
6736 		enabled_tc |= BIT(i);
6737 
6738 	/* Requesting same TC configuration as already enabled */
6739 	if (enabled_tc == vsi->tc_config.enabled_tc &&
6740 	    mode != TC_MQPRIO_MODE_CHANNEL)
6741 		return 0;
6742 
6743 	/* Quiesce VSI queues */
6744 	i40e_quiesce_vsi(vsi);
6745 
6746 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
6747 		i40e_remove_queue_channels(vsi);
6748 
6749 	/* Configure VSI for enabled TCs */
6750 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
6751 	if (ret) {
6752 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6753 			    vsi->seid);
6754 		need_reset = true;
6755 		goto exit;
6756 	}
6757 
6758 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
6759 		if (vsi->mqprio_qopt.max_rate[0]) {
6760 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
6761 
6762 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
6763 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
6764 			if (!ret) {
6765 				u64 credits = max_tx_rate;
6766 
6767 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
6768 				dev_dbg(&vsi->back->pdev->dev,
6769 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6770 					max_tx_rate,
6771 					credits,
6772 					vsi->seid);
6773 			} else {
6774 				need_reset = true;
6775 				goto exit;
6776 			}
6777 		}
6778 		ret = i40e_configure_queue_channels(vsi);
6779 		if (ret) {
6780 			netdev_info(netdev,
6781 				    "Failed configuring queue channels\n");
6782 			need_reset = true;
6783 			goto exit;
6784 		}
6785 	}
6786 
6787 exit:
6788 	/* Reset the configuration data to defaults, only TC0 is enabled */
6789 	if (need_reset) {
6790 		i40e_vsi_set_default_tc_config(vsi);
6791 		need_reset = false;
6792 	}
6793 
6794 	/* Unquiesce VSI */
6795 	i40e_unquiesce_vsi(vsi);
6796 	return ret;
6797 }
6798 
6799 /**
6800  * i40e_set_cld_element - sets cloud filter element data
6801  * @filter: cloud filter rule
6802  * @cld: ptr to cloud filter element data
6803  *
6804  * This is helper function to copy data into cloud filter element
6805  **/
6806 static inline void
6807 i40e_set_cld_element(struct i40e_cloud_filter *filter,
6808 		     struct i40e_aqc_cloud_filters_element_data *cld)
6809 {
6810 	int i, j;
6811 	u32 ipa;
6812 
6813 	memset(cld, 0, sizeof(*cld));
6814 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
6815 	ether_addr_copy(cld->inner_mac, filter->src_mac);
6816 
6817 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
6818 		return;
6819 
6820 	if (filter->n_proto == ETH_P_IPV6) {
6821 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
6822 		for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
6823 		     i++, j += 2) {
6824 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
6825 			ipa = cpu_to_le32(ipa);
6826 			memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
6827 		}
6828 	} else {
6829 		ipa = be32_to_cpu(filter->dst_ipv4);
6830 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
6831 	}
6832 
6833 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
6834 
6835 	/* tenant_id is not supported by FW now, once the support is enabled
6836 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
6837 	 */
6838 	if (filter->tenant_id)
6839 		return;
6840 }
6841 
6842 /**
6843  * i40e_add_del_cloud_filter - Add/del cloud filter
6844  * @vsi: pointer to VSI
6845  * @filter: cloud filter rule
6846  * @add: if true, add, if false, delete
6847  *
6848  * Add or delete a cloud filter for a specific flow spec.
6849  * Returns 0 if the filter were successfully added.
6850  **/
6851 static int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
6852 				     struct i40e_cloud_filter *filter, bool add)
6853 {
6854 	struct i40e_aqc_cloud_filters_element_data cld_filter;
6855 	struct i40e_pf *pf = vsi->back;
6856 	int ret;
6857 	static const u16 flag_table[128] = {
6858 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
6859 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
6860 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
6861 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
6862 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
6863 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
6864 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
6865 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
6866 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
6867 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
6868 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
6869 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
6870 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
6871 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
6872 	};
6873 
6874 	if (filter->flags >= ARRAY_SIZE(flag_table))
6875 		return I40E_ERR_CONFIG;
6876 
6877 	/* copy element needed to add cloud filter from filter */
6878 	i40e_set_cld_element(filter, &cld_filter);
6879 
6880 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
6881 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
6882 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
6883 
6884 	if (filter->n_proto == ETH_P_IPV6)
6885 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6886 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
6887 	else
6888 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6889 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
6890 
6891 	if (add)
6892 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
6893 						&cld_filter, 1);
6894 	else
6895 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
6896 						&cld_filter, 1);
6897 	if (ret)
6898 		dev_dbg(&pf->pdev->dev,
6899 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
6900 			add ? "add" : "delete", filter->dst_port, ret,
6901 			pf->hw.aq.asq_last_status);
6902 	else
6903 		dev_info(&pf->pdev->dev,
6904 			 "%s cloud filter for VSI: %d\n",
6905 			 add ? "Added" : "Deleted", filter->seid);
6906 	return ret;
6907 }
6908 
6909 /**
6910  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
6911  * @vsi: pointer to VSI
6912  * @filter: cloud filter rule
6913  * @add: if true, add, if false, delete
6914  *
6915  * Add or delete a cloud filter for a specific flow spec using big buffer.
6916  * Returns 0 if the filter were successfully added.
6917  **/
6918 static int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
6919 					     struct i40e_cloud_filter *filter,
6920 					     bool add)
6921 {
6922 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
6923 	struct i40e_pf *pf = vsi->back;
6924 	int ret;
6925 
6926 	/* Both (src/dst) valid mac_addr are not supported */
6927 	if ((is_valid_ether_addr(filter->dst_mac) &&
6928 	     is_valid_ether_addr(filter->src_mac)) ||
6929 	    (is_multicast_ether_addr(filter->dst_mac) &&
6930 	     is_multicast_ether_addr(filter->src_mac)))
6931 		return -EOPNOTSUPP;
6932 
6933 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
6934 	 * ports are not supported via big buffer now.
6935 	 */
6936 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
6937 		return -EOPNOTSUPP;
6938 
6939 	/* adding filter using src_port/src_ip is not supported at this stage */
6940 	if (filter->src_port || filter->src_ipv4 ||
6941 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
6942 		return -EOPNOTSUPP;
6943 
6944 	/* copy element needed to add cloud filter from filter */
6945 	i40e_set_cld_element(filter, &cld_filter.element);
6946 
6947 	if (is_valid_ether_addr(filter->dst_mac) ||
6948 	    is_valid_ether_addr(filter->src_mac) ||
6949 	    is_multicast_ether_addr(filter->dst_mac) ||
6950 	    is_multicast_ether_addr(filter->src_mac)) {
6951 		/* MAC + IP : unsupported mode */
6952 		if (filter->dst_ipv4)
6953 			return -EOPNOTSUPP;
6954 
6955 		/* since we validated that L4 port must be valid before
6956 		 * we get here, start with respective "flags" value
6957 		 * and update if vlan is present or not
6958 		 */
6959 		cld_filter.element.flags =
6960 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
6961 
6962 		if (filter->vlan_id) {
6963 			cld_filter.element.flags =
6964 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
6965 		}
6966 
6967 	} else if (filter->dst_ipv4 ||
6968 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
6969 		cld_filter.element.flags =
6970 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
6971 		if (filter->n_proto == ETH_P_IPV6)
6972 			cld_filter.element.flags |=
6973 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
6974 		else
6975 			cld_filter.element.flags |=
6976 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
6977 	} else {
6978 		dev_err(&pf->pdev->dev,
6979 			"either mac or ip has to be valid for cloud filter\n");
6980 		return -EINVAL;
6981 	}
6982 
6983 	/* Now copy L4 port in Byte 6..7 in general fields */
6984 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
6985 						be16_to_cpu(filter->dst_port);
6986 
6987 	if (add) {
6988 		/* Validate current device switch mode, change if necessary */
6989 		ret = i40e_validate_and_set_switch_mode(vsi);
6990 		if (ret) {
6991 			dev_err(&pf->pdev->dev,
6992 				"failed to set switch mode, ret %d\n",
6993 				ret);
6994 			return ret;
6995 		}
6996 
6997 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
6998 						   &cld_filter, 1);
6999 	} else {
7000 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7001 						   &cld_filter, 1);
7002 	}
7003 
7004 	if (ret)
7005 		dev_dbg(&pf->pdev->dev,
7006 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7007 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7008 	else
7009 		dev_info(&pf->pdev->dev,
7010 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7011 			 add ? "add" : "delete", filter->seid,
7012 			 ntohs(filter->dst_port));
7013 	return ret;
7014 }
7015 
7016 /**
7017  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7018  * @vsi: Pointer to VSI
7019  * @cls_flower: Pointer to struct tc_cls_flower_offload
7020  * @filter: Pointer to cloud filter structure
7021  *
7022  **/
7023 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7024 				 struct tc_cls_flower_offload *f,
7025 				 struct i40e_cloud_filter *filter)
7026 {
7027 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7028 	struct i40e_pf *pf = vsi->back;
7029 	u8 field_flags = 0;
7030 
7031 	if (f->dissector->used_keys &
7032 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7033 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
7034 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7035 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
7036 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7037 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7038 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
7039 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7040 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7041 			f->dissector->used_keys);
7042 		return -EOPNOTSUPP;
7043 	}
7044 
7045 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7046 		struct flow_dissector_key_keyid *key =
7047 			skb_flow_dissector_target(f->dissector,
7048 						  FLOW_DISSECTOR_KEY_ENC_KEYID,
7049 						  f->key);
7050 
7051 		struct flow_dissector_key_keyid *mask =
7052 			skb_flow_dissector_target(f->dissector,
7053 						  FLOW_DISSECTOR_KEY_ENC_KEYID,
7054 						  f->mask);
7055 
7056 		if (mask->keyid != 0)
7057 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7058 
7059 		filter->tenant_id = be32_to_cpu(key->keyid);
7060 	}
7061 
7062 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
7063 		struct flow_dissector_key_basic *key =
7064 			skb_flow_dissector_target(f->dissector,
7065 						  FLOW_DISSECTOR_KEY_BASIC,
7066 						  f->key);
7067 
7068 		struct flow_dissector_key_basic *mask =
7069 			skb_flow_dissector_target(f->dissector,
7070 						  FLOW_DISSECTOR_KEY_BASIC,
7071 						  f->mask);
7072 
7073 		n_proto_key = ntohs(key->n_proto);
7074 		n_proto_mask = ntohs(mask->n_proto);
7075 
7076 		if (n_proto_key == ETH_P_ALL) {
7077 			n_proto_key = 0;
7078 			n_proto_mask = 0;
7079 		}
7080 		filter->n_proto = n_proto_key & n_proto_mask;
7081 		filter->ip_proto = key->ip_proto;
7082 	}
7083 
7084 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7085 		struct flow_dissector_key_eth_addrs *key =
7086 			skb_flow_dissector_target(f->dissector,
7087 						  FLOW_DISSECTOR_KEY_ETH_ADDRS,
7088 						  f->key);
7089 
7090 		struct flow_dissector_key_eth_addrs *mask =
7091 			skb_flow_dissector_target(f->dissector,
7092 						  FLOW_DISSECTOR_KEY_ETH_ADDRS,
7093 						  f->mask);
7094 
7095 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
7096 		if (!is_zero_ether_addr(mask->dst)) {
7097 			if (is_broadcast_ether_addr(mask->dst)) {
7098 				field_flags |= I40E_CLOUD_FIELD_OMAC;
7099 			} else {
7100 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7101 					mask->dst);
7102 				return I40E_ERR_CONFIG;
7103 			}
7104 		}
7105 
7106 		if (!is_zero_ether_addr(mask->src)) {
7107 			if (is_broadcast_ether_addr(mask->src)) {
7108 				field_flags |= I40E_CLOUD_FIELD_IMAC;
7109 			} else {
7110 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7111 					mask->src);
7112 				return I40E_ERR_CONFIG;
7113 			}
7114 		}
7115 		ether_addr_copy(filter->dst_mac, key->dst);
7116 		ether_addr_copy(filter->src_mac, key->src);
7117 	}
7118 
7119 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
7120 		struct flow_dissector_key_vlan *key =
7121 			skb_flow_dissector_target(f->dissector,
7122 						  FLOW_DISSECTOR_KEY_VLAN,
7123 						  f->key);
7124 		struct flow_dissector_key_vlan *mask =
7125 			skb_flow_dissector_target(f->dissector,
7126 						  FLOW_DISSECTOR_KEY_VLAN,
7127 						  f->mask);
7128 
7129 		if (mask->vlan_id) {
7130 			if (mask->vlan_id == VLAN_VID_MASK) {
7131 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
7132 
7133 			} else {
7134 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7135 					mask->vlan_id);
7136 				return I40E_ERR_CONFIG;
7137 			}
7138 		}
7139 
7140 		filter->vlan_id = cpu_to_be16(key->vlan_id);
7141 	}
7142 
7143 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
7144 		struct flow_dissector_key_control *key =
7145 			skb_flow_dissector_target(f->dissector,
7146 						  FLOW_DISSECTOR_KEY_CONTROL,
7147 						  f->key);
7148 
7149 		addr_type = key->addr_type;
7150 	}
7151 
7152 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7153 		struct flow_dissector_key_ipv4_addrs *key =
7154 			skb_flow_dissector_target(f->dissector,
7155 						  FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7156 						  f->key);
7157 		struct flow_dissector_key_ipv4_addrs *mask =
7158 			skb_flow_dissector_target(f->dissector,
7159 						  FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7160 						  f->mask);
7161 
7162 		if (mask->dst) {
7163 			if (mask->dst == cpu_to_be32(0xffffffff)) {
7164 				field_flags |= I40E_CLOUD_FIELD_IIP;
7165 			} else {
7166 				mask->dst = be32_to_cpu(mask->dst);
7167 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4\n",
7168 					&mask->dst);
7169 				return I40E_ERR_CONFIG;
7170 			}
7171 		}
7172 
7173 		if (mask->src) {
7174 			if (mask->src == cpu_to_be32(0xffffffff)) {
7175 				field_flags |= I40E_CLOUD_FIELD_IIP;
7176 			} else {
7177 				mask->src = be32_to_cpu(mask->src);
7178 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4\n",
7179 					&mask->src);
7180 				return I40E_ERR_CONFIG;
7181 			}
7182 		}
7183 
7184 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7185 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7186 			return I40E_ERR_CONFIG;
7187 		}
7188 		filter->dst_ipv4 = key->dst;
7189 		filter->src_ipv4 = key->src;
7190 	}
7191 
7192 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7193 		struct flow_dissector_key_ipv6_addrs *key =
7194 			skb_flow_dissector_target(f->dissector,
7195 						  FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7196 						  f->key);
7197 		struct flow_dissector_key_ipv6_addrs *mask =
7198 			skb_flow_dissector_target(f->dissector,
7199 						  FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7200 						  f->mask);
7201 
7202 		/* src and dest IPV6 address should not be LOOPBACK
7203 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7204 		 */
7205 		if (ipv6_addr_loopback(&key->dst) ||
7206 		    ipv6_addr_loopback(&key->src)) {
7207 			dev_err(&pf->pdev->dev,
7208 				"Bad ipv6, addr is LOOPBACK\n");
7209 			return I40E_ERR_CONFIG;
7210 		}
7211 		if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
7212 			field_flags |= I40E_CLOUD_FIELD_IIP;
7213 
7214 		memcpy(&filter->src_ipv6, &key->src.s6_addr32,
7215 		       sizeof(filter->src_ipv6));
7216 		memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
7217 		       sizeof(filter->dst_ipv6));
7218 	}
7219 
7220 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
7221 		struct flow_dissector_key_ports *key =
7222 			skb_flow_dissector_target(f->dissector,
7223 						  FLOW_DISSECTOR_KEY_PORTS,
7224 						  f->key);
7225 		struct flow_dissector_key_ports *mask =
7226 			skb_flow_dissector_target(f->dissector,
7227 						  FLOW_DISSECTOR_KEY_PORTS,
7228 						  f->mask);
7229 
7230 		if (mask->src) {
7231 			if (mask->src == cpu_to_be16(0xffff)) {
7232 				field_flags |= I40E_CLOUD_FIELD_IIP;
7233 			} else {
7234 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7235 					be16_to_cpu(mask->src));
7236 				return I40E_ERR_CONFIG;
7237 			}
7238 		}
7239 
7240 		if (mask->dst) {
7241 			if (mask->dst == cpu_to_be16(0xffff)) {
7242 				field_flags |= I40E_CLOUD_FIELD_IIP;
7243 			} else {
7244 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7245 					be16_to_cpu(mask->dst));
7246 				return I40E_ERR_CONFIG;
7247 			}
7248 		}
7249 
7250 		filter->dst_port = key->dst;
7251 		filter->src_port = key->src;
7252 
7253 		switch (filter->ip_proto) {
7254 		case IPPROTO_TCP:
7255 		case IPPROTO_UDP:
7256 			break;
7257 		default:
7258 			dev_err(&pf->pdev->dev,
7259 				"Only UDP and TCP transport are supported\n");
7260 			return -EINVAL;
7261 		}
7262 	}
7263 	filter->flags = field_flags;
7264 	return 0;
7265 }
7266 
7267 /**
7268  * i40e_handle_tclass: Forward to a traffic class on the device
7269  * @vsi: Pointer to VSI
7270  * @tc: traffic class index on the device
7271  * @filter: Pointer to cloud filter structure
7272  *
7273  **/
7274 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7275 			      struct i40e_cloud_filter *filter)
7276 {
7277 	struct i40e_channel *ch, *ch_tmp;
7278 
7279 	/* direct to a traffic class on the same device */
7280 	if (tc == 0) {
7281 		filter->seid = vsi->seid;
7282 		return 0;
7283 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7284 		if (!filter->dst_port) {
7285 			dev_err(&vsi->back->pdev->dev,
7286 				"Specify destination port to direct to traffic class that is not default\n");
7287 			return -EINVAL;
7288 		}
7289 		if (list_empty(&vsi->ch_list))
7290 			return -EINVAL;
7291 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7292 					 list) {
7293 			if (ch->seid == vsi->tc_seid_map[tc])
7294 				filter->seid = ch->seid;
7295 		}
7296 		return 0;
7297 	}
7298 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
7299 	return -EINVAL;
7300 }
7301 
7302 /**
7303  * i40e_configure_clsflower - Configure tc flower filters
7304  * @vsi: Pointer to VSI
7305  * @cls_flower: Pointer to struct tc_cls_flower_offload
7306  *
7307  **/
7308 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
7309 				    struct tc_cls_flower_offload *cls_flower)
7310 {
7311 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
7312 	struct i40e_cloud_filter *filter = NULL;
7313 	struct i40e_pf *pf = vsi->back;
7314 	int err = 0;
7315 
7316 	if (tc < 0) {
7317 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
7318 		return -EOPNOTSUPP;
7319 	}
7320 
7321 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
7322 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
7323 		return -EBUSY;
7324 
7325 	if (pf->fdir_pf_active_filters ||
7326 	    (!hlist_empty(&pf->fdir_filter_list))) {
7327 		dev_err(&vsi->back->pdev->dev,
7328 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
7329 		return -EINVAL;
7330 	}
7331 
7332 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
7333 		dev_err(&vsi->back->pdev->dev,
7334 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
7335 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7336 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7337 	}
7338 
7339 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
7340 	if (!filter)
7341 		return -ENOMEM;
7342 
7343 	filter->cookie = cls_flower->cookie;
7344 
7345 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
7346 	if (err < 0)
7347 		goto err;
7348 
7349 	err = i40e_handle_tclass(vsi, tc, filter);
7350 	if (err < 0)
7351 		goto err;
7352 
7353 	/* Add cloud filter */
7354 	if (filter->dst_port)
7355 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
7356 	else
7357 		err = i40e_add_del_cloud_filter(vsi, filter, true);
7358 
7359 	if (err) {
7360 		dev_err(&pf->pdev->dev,
7361 			"Failed to add cloud filter, err %s\n",
7362 			i40e_stat_str(&pf->hw, err));
7363 		goto err;
7364 	}
7365 
7366 	/* add filter to the ordered list */
7367 	INIT_HLIST_NODE(&filter->cloud_node);
7368 
7369 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
7370 
7371 	pf->num_cloud_filters++;
7372 
7373 	return err;
7374 err:
7375 	kfree(filter);
7376 	return err;
7377 }
7378 
7379 /**
7380  * i40e_find_cloud_filter - Find the could filter in the list
7381  * @vsi: Pointer to VSI
7382  * @cookie: filter specific cookie
7383  *
7384  **/
7385 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
7386 							unsigned long *cookie)
7387 {
7388 	struct i40e_cloud_filter *filter = NULL;
7389 	struct hlist_node *node2;
7390 
7391 	hlist_for_each_entry_safe(filter, node2,
7392 				  &vsi->back->cloud_filter_list, cloud_node)
7393 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
7394 			return filter;
7395 	return NULL;
7396 }
7397 
7398 /**
7399  * i40e_delete_clsflower - Remove tc flower filters
7400  * @vsi: Pointer to VSI
7401  * @cls_flower: Pointer to struct tc_cls_flower_offload
7402  *
7403  **/
7404 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
7405 				 struct tc_cls_flower_offload *cls_flower)
7406 {
7407 	struct i40e_cloud_filter *filter = NULL;
7408 	struct i40e_pf *pf = vsi->back;
7409 	int err = 0;
7410 
7411 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
7412 
7413 	if (!filter)
7414 		return -EINVAL;
7415 
7416 	hash_del(&filter->cloud_node);
7417 
7418 	if (filter->dst_port)
7419 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
7420 	else
7421 		err = i40e_add_del_cloud_filter(vsi, filter, false);
7422 
7423 	kfree(filter);
7424 	if (err) {
7425 		dev_err(&pf->pdev->dev,
7426 			"Failed to delete cloud filter, err %s\n",
7427 			i40e_stat_str(&pf->hw, err));
7428 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
7429 	}
7430 
7431 	pf->num_cloud_filters--;
7432 	if (!pf->num_cloud_filters)
7433 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7434 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7435 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7436 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7437 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7438 		}
7439 	return 0;
7440 }
7441 
7442 /**
7443  * i40e_setup_tc_cls_flower - flower classifier offloads
7444  * @netdev: net device to configure
7445  * @type_data: offload data
7446  **/
7447 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
7448 				    struct tc_cls_flower_offload *cls_flower)
7449 {
7450 	struct i40e_vsi *vsi = np->vsi;
7451 
7452 	switch (cls_flower->command) {
7453 	case TC_CLSFLOWER_REPLACE:
7454 		return i40e_configure_clsflower(vsi, cls_flower);
7455 	case TC_CLSFLOWER_DESTROY:
7456 		return i40e_delete_clsflower(vsi, cls_flower);
7457 	case TC_CLSFLOWER_STATS:
7458 		return -EOPNOTSUPP;
7459 	default:
7460 		return -EINVAL;
7461 	}
7462 }
7463 
7464 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7465 				  void *cb_priv)
7466 {
7467 	struct i40e_netdev_priv *np = cb_priv;
7468 
7469 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
7470 		return -EOPNOTSUPP;
7471 
7472 	switch (type) {
7473 	case TC_SETUP_CLSFLOWER:
7474 		return i40e_setup_tc_cls_flower(np, type_data);
7475 
7476 	default:
7477 		return -EOPNOTSUPP;
7478 	}
7479 }
7480 
7481 static int i40e_setup_tc_block(struct net_device *dev,
7482 			       struct tc_block_offload *f)
7483 {
7484 	struct i40e_netdev_priv *np = netdev_priv(dev);
7485 
7486 	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7487 		return -EOPNOTSUPP;
7488 
7489 	switch (f->command) {
7490 	case TC_BLOCK_BIND:
7491 		return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
7492 					     np, np);
7493 	case TC_BLOCK_UNBIND:
7494 		tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
7495 		return 0;
7496 	default:
7497 		return -EOPNOTSUPP;
7498 	}
7499 }
7500 
7501 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
7502 			   void *type_data)
7503 {
7504 	switch (type) {
7505 	case TC_SETUP_QDISC_MQPRIO:
7506 		return i40e_setup_tc(netdev, type_data);
7507 	case TC_SETUP_BLOCK:
7508 		return i40e_setup_tc_block(netdev, type_data);
7509 	default:
7510 		return -EOPNOTSUPP;
7511 	}
7512 }
7513 
7514 /**
7515  * i40e_open - Called when a network interface is made active
7516  * @netdev: network interface device structure
7517  *
7518  * The open entry point is called when a network interface is made
7519  * active by the system (IFF_UP).  At this point all resources needed
7520  * for transmit and receive operations are allocated, the interrupt
7521  * handler is registered with the OS, the netdev watchdog subtask is
7522  * enabled, and the stack is notified that the interface is ready.
7523  *
7524  * Returns 0 on success, negative value on failure
7525  **/
7526 int i40e_open(struct net_device *netdev)
7527 {
7528 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7529 	struct i40e_vsi *vsi = np->vsi;
7530 	struct i40e_pf *pf = vsi->back;
7531 	int err;
7532 
7533 	/* disallow open during test or if eeprom is broken */
7534 	if (test_bit(__I40E_TESTING, pf->state) ||
7535 	    test_bit(__I40E_BAD_EEPROM, pf->state))
7536 		return -EBUSY;
7537 
7538 	netif_carrier_off(netdev);
7539 
7540 	err = i40e_vsi_open(vsi);
7541 	if (err)
7542 		return err;
7543 
7544 	/* configure global TSO hardware offload settings */
7545 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
7546 						       TCP_FLAG_FIN) >> 16);
7547 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
7548 						       TCP_FLAG_FIN |
7549 						       TCP_FLAG_CWR) >> 16);
7550 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
7551 
7552 	udp_tunnel_get_rx_info(netdev);
7553 
7554 	return 0;
7555 }
7556 
7557 /**
7558  * i40e_vsi_open -
7559  * @vsi: the VSI to open
7560  *
7561  * Finish initialization of the VSI.
7562  *
7563  * Returns 0 on success, negative value on failure
7564  *
7565  * Note: expects to be called while under rtnl_lock()
7566  **/
7567 int i40e_vsi_open(struct i40e_vsi *vsi)
7568 {
7569 	struct i40e_pf *pf = vsi->back;
7570 	char int_name[I40E_INT_NAME_STR_LEN];
7571 	int err;
7572 
7573 	/* allocate descriptors */
7574 	err = i40e_vsi_setup_tx_resources(vsi);
7575 	if (err)
7576 		goto err_setup_tx;
7577 	err = i40e_vsi_setup_rx_resources(vsi);
7578 	if (err)
7579 		goto err_setup_rx;
7580 
7581 	err = i40e_vsi_configure(vsi);
7582 	if (err)
7583 		goto err_setup_rx;
7584 
7585 	if (vsi->netdev) {
7586 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7587 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
7588 		err = i40e_vsi_request_irq(vsi, int_name);
7589 		if (err)
7590 			goto err_setup_rx;
7591 
7592 		/* Notify the stack of the actual queue counts. */
7593 		err = netif_set_real_num_tx_queues(vsi->netdev,
7594 						   vsi->num_queue_pairs);
7595 		if (err)
7596 			goto err_set_queues;
7597 
7598 		err = netif_set_real_num_rx_queues(vsi->netdev,
7599 						   vsi->num_queue_pairs);
7600 		if (err)
7601 			goto err_set_queues;
7602 
7603 	} else if (vsi->type == I40E_VSI_FDIR) {
7604 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
7605 			 dev_driver_string(&pf->pdev->dev),
7606 			 dev_name(&pf->pdev->dev));
7607 		err = i40e_vsi_request_irq(vsi, int_name);
7608 
7609 	} else {
7610 		err = -EINVAL;
7611 		goto err_setup_rx;
7612 	}
7613 
7614 	err = i40e_up_complete(vsi);
7615 	if (err)
7616 		goto err_up_complete;
7617 
7618 	return 0;
7619 
7620 err_up_complete:
7621 	i40e_down(vsi);
7622 err_set_queues:
7623 	i40e_vsi_free_irq(vsi);
7624 err_setup_rx:
7625 	i40e_vsi_free_rx_resources(vsi);
7626 err_setup_tx:
7627 	i40e_vsi_free_tx_resources(vsi);
7628 	if (vsi == pf->vsi[pf->lan_vsi])
7629 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
7630 
7631 	return err;
7632 }
7633 
7634 /**
7635  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
7636  * @pf: Pointer to PF
7637  *
7638  * This function destroys the hlist where all the Flow Director
7639  * filters were saved.
7640  **/
7641 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
7642 {
7643 	struct i40e_fdir_filter *filter;
7644 	struct i40e_flex_pit *pit_entry, *tmp;
7645 	struct hlist_node *node2;
7646 
7647 	hlist_for_each_entry_safe(filter, node2,
7648 				  &pf->fdir_filter_list, fdir_node) {
7649 		hlist_del(&filter->fdir_node);
7650 		kfree(filter);
7651 	}
7652 
7653 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
7654 		list_del(&pit_entry->list);
7655 		kfree(pit_entry);
7656 	}
7657 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
7658 
7659 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
7660 		list_del(&pit_entry->list);
7661 		kfree(pit_entry);
7662 	}
7663 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
7664 
7665 	pf->fdir_pf_active_filters = 0;
7666 	pf->fd_tcp4_filter_cnt = 0;
7667 	pf->fd_udp4_filter_cnt = 0;
7668 	pf->fd_sctp4_filter_cnt = 0;
7669 	pf->fd_ip4_filter_cnt = 0;
7670 
7671 	/* Reprogram the default input set for TCP/IPv4 */
7672 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
7673 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7674 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7675 
7676 	/* Reprogram the default input set for UDP/IPv4 */
7677 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
7678 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7679 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7680 
7681 	/* Reprogram the default input set for SCTP/IPv4 */
7682 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
7683 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7684 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7685 
7686 	/* Reprogram the default input set for Other/IPv4 */
7687 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
7688 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7689 
7690 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
7691 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7692 }
7693 
7694 /**
7695  * i40e_cloud_filter_exit - Cleans up the cloud filters
7696  * @pf: Pointer to PF
7697  *
7698  * This function destroys the hlist where all the cloud filters
7699  * were saved.
7700  **/
7701 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
7702 {
7703 	struct i40e_cloud_filter *cfilter;
7704 	struct hlist_node *node;
7705 
7706 	hlist_for_each_entry_safe(cfilter, node,
7707 				  &pf->cloud_filter_list, cloud_node) {
7708 		hlist_del(&cfilter->cloud_node);
7709 		kfree(cfilter);
7710 	}
7711 	pf->num_cloud_filters = 0;
7712 
7713 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7714 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7715 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7716 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7717 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7718 	}
7719 }
7720 
7721 /**
7722  * i40e_close - Disables a network interface
7723  * @netdev: network interface device structure
7724  *
7725  * The close entry point is called when an interface is de-activated
7726  * by the OS.  The hardware is still under the driver's control, but
7727  * this netdev interface is disabled.
7728  *
7729  * Returns 0, this is not allowed to fail
7730  **/
7731 int i40e_close(struct net_device *netdev)
7732 {
7733 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7734 	struct i40e_vsi *vsi = np->vsi;
7735 
7736 	i40e_vsi_close(vsi);
7737 
7738 	return 0;
7739 }
7740 
7741 /**
7742  * i40e_do_reset - Start a PF or Core Reset sequence
7743  * @pf: board private structure
7744  * @reset_flags: which reset is requested
7745  * @lock_acquired: indicates whether or not the lock has been acquired
7746  * before this function was called.
7747  *
7748  * The essential difference in resets is that the PF Reset
7749  * doesn't clear the packet buffers, doesn't reset the PE
7750  * firmware, and doesn't bother the other PFs on the chip.
7751  **/
7752 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
7753 {
7754 	u32 val;
7755 
7756 	WARN_ON(in_interrupt());
7757 
7758 
7759 	/* do the biggest reset indicated */
7760 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
7761 
7762 		/* Request a Global Reset
7763 		 *
7764 		 * This will start the chip's countdown to the actual full
7765 		 * chip reset event, and a warning interrupt to be sent
7766 		 * to all PFs, including the requestor.  Our handler
7767 		 * for the warning interrupt will deal with the shutdown
7768 		 * and recovery of the switch setup.
7769 		 */
7770 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
7771 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7772 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
7773 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7774 
7775 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
7776 
7777 		/* Request a Core Reset
7778 		 *
7779 		 * Same as Global Reset, except does *not* include the MAC/PHY
7780 		 */
7781 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
7782 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7783 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
7784 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7785 		i40e_flush(&pf->hw);
7786 
7787 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
7788 
7789 		/* Request a PF Reset
7790 		 *
7791 		 * Resets only the PF-specific registers
7792 		 *
7793 		 * This goes directly to the tear-down and rebuild of
7794 		 * the switch, since we need to do all the recovery as
7795 		 * for the Core Reset.
7796 		 */
7797 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
7798 		i40e_handle_reset_warning(pf, lock_acquired);
7799 
7800 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
7801 		int v;
7802 
7803 		/* Find the VSI(s) that requested a re-init */
7804 		dev_info(&pf->pdev->dev,
7805 			 "VSI reinit requested\n");
7806 		for (v = 0; v < pf->num_alloc_vsi; v++) {
7807 			struct i40e_vsi *vsi = pf->vsi[v];
7808 
7809 			if (vsi != NULL &&
7810 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
7811 					       vsi->state))
7812 				i40e_vsi_reinit_locked(pf->vsi[v]);
7813 		}
7814 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
7815 		int v;
7816 
7817 		/* Find the VSI(s) that needs to be brought down */
7818 		dev_info(&pf->pdev->dev, "VSI down requested\n");
7819 		for (v = 0; v < pf->num_alloc_vsi; v++) {
7820 			struct i40e_vsi *vsi = pf->vsi[v];
7821 
7822 			if (vsi != NULL &&
7823 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
7824 					       vsi->state)) {
7825 				set_bit(__I40E_VSI_DOWN, vsi->state);
7826 				i40e_down(vsi);
7827 			}
7828 		}
7829 	} else {
7830 		dev_info(&pf->pdev->dev,
7831 			 "bad reset request 0x%08x\n", reset_flags);
7832 	}
7833 }
7834 
7835 #ifdef CONFIG_I40E_DCB
7836 /**
7837  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
7838  * @pf: board private structure
7839  * @old_cfg: current DCB config
7840  * @new_cfg: new DCB config
7841  **/
7842 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
7843 			    struct i40e_dcbx_config *old_cfg,
7844 			    struct i40e_dcbx_config *new_cfg)
7845 {
7846 	bool need_reconfig = false;
7847 
7848 	/* Check if ETS configuration has changed */
7849 	if (memcmp(&new_cfg->etscfg,
7850 		   &old_cfg->etscfg,
7851 		   sizeof(new_cfg->etscfg))) {
7852 		/* If Priority Table has changed reconfig is needed */
7853 		if (memcmp(&new_cfg->etscfg.prioritytable,
7854 			   &old_cfg->etscfg.prioritytable,
7855 			   sizeof(new_cfg->etscfg.prioritytable))) {
7856 			need_reconfig = true;
7857 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
7858 		}
7859 
7860 		if (memcmp(&new_cfg->etscfg.tcbwtable,
7861 			   &old_cfg->etscfg.tcbwtable,
7862 			   sizeof(new_cfg->etscfg.tcbwtable)))
7863 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
7864 
7865 		if (memcmp(&new_cfg->etscfg.tsatable,
7866 			   &old_cfg->etscfg.tsatable,
7867 			   sizeof(new_cfg->etscfg.tsatable)))
7868 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
7869 	}
7870 
7871 	/* Check if PFC configuration has changed */
7872 	if (memcmp(&new_cfg->pfc,
7873 		   &old_cfg->pfc,
7874 		   sizeof(new_cfg->pfc))) {
7875 		need_reconfig = true;
7876 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
7877 	}
7878 
7879 	/* Check if APP Table has changed */
7880 	if (memcmp(&new_cfg->app,
7881 		   &old_cfg->app,
7882 		   sizeof(new_cfg->app))) {
7883 		need_reconfig = true;
7884 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
7885 	}
7886 
7887 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
7888 	return need_reconfig;
7889 }
7890 
7891 /**
7892  * i40e_handle_lldp_event - Handle LLDP Change MIB event
7893  * @pf: board private structure
7894  * @e: event info posted on ARQ
7895  **/
7896 static int i40e_handle_lldp_event(struct i40e_pf *pf,
7897 				  struct i40e_arq_event_info *e)
7898 {
7899 	struct i40e_aqc_lldp_get_mib *mib =
7900 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
7901 	struct i40e_hw *hw = &pf->hw;
7902 	struct i40e_dcbx_config tmp_dcbx_cfg;
7903 	bool need_reconfig = false;
7904 	int ret = 0;
7905 	u8 type;
7906 
7907 	/* Not DCB capable or capability disabled */
7908 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
7909 		return ret;
7910 
7911 	/* Ignore if event is not for Nearest Bridge */
7912 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
7913 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
7914 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
7915 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
7916 		return ret;
7917 
7918 	/* Check MIB Type and return if event for Remote MIB update */
7919 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
7920 	dev_dbg(&pf->pdev->dev,
7921 		"LLDP event mib type %s\n", type ? "remote" : "local");
7922 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
7923 		/* Update the remote cached instance and return */
7924 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
7925 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
7926 				&hw->remote_dcbx_config);
7927 		goto exit;
7928 	}
7929 
7930 	/* Store the old configuration */
7931 	tmp_dcbx_cfg = hw->local_dcbx_config;
7932 
7933 	/* Reset the old DCBx configuration data */
7934 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
7935 	/* Get updated DCBX data from firmware */
7936 	ret = i40e_get_dcb_config(&pf->hw);
7937 	if (ret) {
7938 		dev_info(&pf->pdev->dev,
7939 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
7940 			 i40e_stat_str(&pf->hw, ret),
7941 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7942 		goto exit;
7943 	}
7944 
7945 	/* No change detected in DCBX configs */
7946 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
7947 		    sizeof(tmp_dcbx_cfg))) {
7948 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
7949 		goto exit;
7950 	}
7951 
7952 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
7953 					       &hw->local_dcbx_config);
7954 
7955 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
7956 
7957 	if (!need_reconfig)
7958 		goto exit;
7959 
7960 	/* Enable DCB tagging only when more than one TC */
7961 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7962 		pf->flags |= I40E_FLAG_DCB_ENABLED;
7963 	else
7964 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7965 
7966 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
7967 	/* Reconfiguration needed quiesce all VSIs */
7968 	i40e_pf_quiesce_all_vsi(pf);
7969 
7970 	/* Changes in configuration update VEB/VSI */
7971 	i40e_dcb_reconfigure(pf);
7972 
7973 	ret = i40e_resume_port_tx(pf);
7974 
7975 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7976 	/* In case of error no point in resuming VSIs */
7977 	if (ret)
7978 		goto exit;
7979 
7980 	/* Wait for the PF's queues to be disabled */
7981 	ret = i40e_pf_wait_queues_disabled(pf);
7982 	if (ret) {
7983 		/* Schedule PF reset to recover */
7984 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7985 		i40e_service_event_schedule(pf);
7986 	} else {
7987 		i40e_pf_unquiesce_all_vsi(pf);
7988 	pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
7989 		      I40E_FLAG_CLIENT_L2_CHANGE);
7990 	}
7991 
7992 exit:
7993 	return ret;
7994 }
7995 #endif /* CONFIG_I40E_DCB */
7996 
7997 /**
7998  * i40e_do_reset_safe - Protected reset path for userland calls.
7999  * @pf: board private structure
8000  * @reset_flags: which reset is requested
8001  *
8002  **/
8003 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8004 {
8005 	rtnl_lock();
8006 	i40e_do_reset(pf, reset_flags, true);
8007 	rtnl_unlock();
8008 }
8009 
8010 /**
8011  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8012  * @pf: board private structure
8013  * @e: event info posted on ARQ
8014  *
8015  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8016  * and VF queues
8017  **/
8018 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8019 					   struct i40e_arq_event_info *e)
8020 {
8021 	struct i40e_aqc_lan_overflow *data =
8022 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8023 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8024 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8025 	struct i40e_hw *hw = &pf->hw;
8026 	struct i40e_vf *vf;
8027 	u16 vf_id;
8028 
8029 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8030 		queue, qtx_ctl);
8031 
8032 	/* Queue belongs to VF, find the VF and issue VF reset */
8033 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8034 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8035 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8036 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8037 		vf_id -= hw->func_caps.vf_base_id;
8038 		vf = &pf->vf[vf_id];
8039 		i40e_vc_notify_vf_reset(vf);
8040 		/* Allow VF to process pending reset notification */
8041 		msleep(20);
8042 		i40e_reset_vf(vf, false);
8043 	}
8044 }
8045 
8046 /**
8047  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8048  * @pf: board private structure
8049  **/
8050 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8051 {
8052 	u32 val, fcnt_prog;
8053 
8054 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8055 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8056 	return fcnt_prog;
8057 }
8058 
8059 /**
8060  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8061  * @pf: board private structure
8062  **/
8063 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8064 {
8065 	u32 val, fcnt_prog;
8066 
8067 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8068 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8069 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8070 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8071 	return fcnt_prog;
8072 }
8073 
8074 /**
8075  * i40e_get_global_fd_count - Get total FD filters programmed on device
8076  * @pf: board private structure
8077  **/
8078 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8079 {
8080 	u32 val, fcnt_prog;
8081 
8082 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8083 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8084 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8085 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8086 	return fcnt_prog;
8087 }
8088 
8089 /**
8090  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8091  * @pf: board private structure
8092  **/
8093 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8094 {
8095 	struct i40e_fdir_filter *filter;
8096 	u32 fcnt_prog, fcnt_avail;
8097 	struct hlist_node *node;
8098 
8099 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8100 		return;
8101 
8102 	/* Check if we have enough room to re-enable FDir SB capability. */
8103 	fcnt_prog = i40e_get_global_fd_count(pf);
8104 	fcnt_avail = pf->fdir_pf_filter_count;
8105 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8106 	    (pf->fd_add_err == 0) ||
8107 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
8108 		if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
8109 			pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
8110 			if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8111 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
8112 				dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8113 		}
8114 	}
8115 
8116 	/* We should wait for even more space before re-enabling ATR.
8117 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
8118 	 * rules active.
8119 	 */
8120 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8121 	    (pf->fd_tcp4_filter_cnt == 0)) {
8122 		if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
8123 			pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
8124 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8125 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
8126 				dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8127 		}
8128 	}
8129 
8130 	/* if hw had a problem adding a filter, delete it */
8131 	if (pf->fd_inv > 0) {
8132 		hlist_for_each_entry_safe(filter, node,
8133 					  &pf->fdir_filter_list, fdir_node) {
8134 			if (filter->fd_id == pf->fd_inv) {
8135 				hlist_del(&filter->fdir_node);
8136 				kfree(filter);
8137 				pf->fdir_pf_active_filters--;
8138 				pf->fd_inv = 0;
8139 			}
8140 		}
8141 	}
8142 }
8143 
8144 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8145 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8146 /**
8147  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8148  * @pf: board private structure
8149  **/
8150 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8151 {
8152 	unsigned long min_flush_time;
8153 	int flush_wait_retry = 50;
8154 	bool disable_atr = false;
8155 	int fd_room;
8156 	int reg;
8157 
8158 	if (!time_after(jiffies, pf->fd_flush_timestamp +
8159 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8160 		return;
8161 
8162 	/* If the flush is happening too quick and we have mostly SB rules we
8163 	 * should not re-enable ATR for some time.
8164 	 */
8165 	min_flush_time = pf->fd_flush_timestamp +
8166 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8167 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8168 
8169 	if (!(time_after(jiffies, min_flush_time)) &&
8170 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8171 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8172 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8173 		disable_atr = true;
8174 	}
8175 
8176 	pf->fd_flush_timestamp = jiffies;
8177 	pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
8178 	/* flush all filters */
8179 	wr32(&pf->hw, I40E_PFQF_CTL_1,
8180 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8181 	i40e_flush(&pf->hw);
8182 	pf->fd_flush_cnt++;
8183 	pf->fd_add_err = 0;
8184 	do {
8185 		/* Check FD flush status every 5-6msec */
8186 		usleep_range(5000, 6000);
8187 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8188 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8189 			break;
8190 	} while (flush_wait_retry--);
8191 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8192 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8193 	} else {
8194 		/* replay sideband filters */
8195 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8196 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8197 			pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
8198 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8199 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8200 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8201 	}
8202 }
8203 
8204 /**
8205  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8206  * @pf: board private structure
8207  **/
8208 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
8209 {
8210 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8211 }
8212 
8213 /* We can see up to 256 filter programming desc in transit if the filters are
8214  * being applied really fast; before we see the first
8215  * filter miss error on Rx queue 0. Accumulating enough error messages before
8216  * reacting will make sure we don't cause flush too often.
8217  */
8218 #define I40E_MAX_FD_PROGRAM_ERROR 256
8219 
8220 /**
8221  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8222  * @pf: board private structure
8223  **/
8224 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8225 {
8226 
8227 	/* if interface is down do nothing */
8228 	if (test_bit(__I40E_DOWN, pf->state))
8229 		return;
8230 
8231 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8232 		i40e_fdir_flush_and_replay(pf);
8233 
8234 	i40e_fdir_check_and_reenable(pf);
8235 
8236 }
8237 
8238 /**
8239  * i40e_vsi_link_event - notify VSI of a link event
8240  * @vsi: vsi to be notified
8241  * @link_up: link up or down
8242  **/
8243 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
8244 {
8245 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
8246 		return;
8247 
8248 	switch (vsi->type) {
8249 	case I40E_VSI_MAIN:
8250 		if (!vsi->netdev || !vsi->netdev_registered)
8251 			break;
8252 
8253 		if (link_up) {
8254 			netif_carrier_on(vsi->netdev);
8255 			netif_tx_wake_all_queues(vsi->netdev);
8256 		} else {
8257 			netif_carrier_off(vsi->netdev);
8258 			netif_tx_stop_all_queues(vsi->netdev);
8259 		}
8260 		break;
8261 
8262 	case I40E_VSI_SRIOV:
8263 	case I40E_VSI_VMDQ2:
8264 	case I40E_VSI_CTRL:
8265 	case I40E_VSI_IWARP:
8266 	case I40E_VSI_MIRROR:
8267 	default:
8268 		/* there is no notification for other VSIs */
8269 		break;
8270 	}
8271 }
8272 
8273 /**
8274  * i40e_veb_link_event - notify elements on the veb of a link event
8275  * @veb: veb to be notified
8276  * @link_up: link up or down
8277  **/
8278 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
8279 {
8280 	struct i40e_pf *pf;
8281 	int i;
8282 
8283 	if (!veb || !veb->pf)
8284 		return;
8285 	pf = veb->pf;
8286 
8287 	/* depth first... */
8288 	for (i = 0; i < I40E_MAX_VEB; i++)
8289 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
8290 			i40e_veb_link_event(pf->veb[i], link_up);
8291 
8292 	/* ... now the local VSIs */
8293 	for (i = 0; i < pf->num_alloc_vsi; i++)
8294 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
8295 			i40e_vsi_link_event(pf->vsi[i], link_up);
8296 }
8297 
8298 /**
8299  * i40e_link_event - Update netif_carrier status
8300  * @pf: board private structure
8301  **/
8302 static void i40e_link_event(struct i40e_pf *pf)
8303 {
8304 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8305 	u8 new_link_speed, old_link_speed;
8306 	i40e_status status;
8307 	bool new_link, old_link;
8308 
8309 	/* save off old link status information */
8310 	pf->hw.phy.link_info_old = pf->hw.phy.link_info;
8311 
8312 	/* set this to force the get_link_status call to refresh state */
8313 	pf->hw.phy.get_link_info = true;
8314 
8315 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
8316 
8317 	status = i40e_get_link_status(&pf->hw, &new_link);
8318 
8319 	/* On success, disable temp link polling */
8320 	if (status == I40E_SUCCESS) {
8321 		if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
8322 			pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
8323 	} else {
8324 		/* Enable link polling temporarily until i40e_get_link_status
8325 		 * returns I40E_SUCCESS
8326 		 */
8327 		pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
8328 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
8329 			status);
8330 		return;
8331 	}
8332 
8333 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
8334 	new_link_speed = pf->hw.phy.link_info.link_speed;
8335 
8336 	if (new_link == old_link &&
8337 	    new_link_speed == old_link_speed &&
8338 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
8339 	     new_link == netif_carrier_ok(vsi->netdev)))
8340 		return;
8341 
8342 	i40e_print_link_message(vsi, new_link);
8343 
8344 	/* Notify the base of the switch tree connected to
8345 	 * the link.  Floating VEBs are not notified.
8346 	 */
8347 	if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8348 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
8349 	else
8350 		i40e_vsi_link_event(vsi, new_link);
8351 
8352 	if (pf->vf)
8353 		i40e_vc_notify_link_state(pf);
8354 
8355 	if (pf->flags & I40E_FLAG_PTP)
8356 		i40e_ptp_set_increment(pf);
8357 }
8358 
8359 /**
8360  * i40e_watchdog_subtask - periodic checks not using event driven response
8361  * @pf: board private structure
8362  **/
8363 static void i40e_watchdog_subtask(struct i40e_pf *pf)
8364 {
8365 	int i;
8366 
8367 	/* if interface is down do nothing */
8368 	if (test_bit(__I40E_DOWN, pf->state) ||
8369 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
8370 		return;
8371 
8372 	/* make sure we don't do these things too often */
8373 	if (time_before(jiffies, (pf->service_timer_previous +
8374 				  pf->service_timer_period)))
8375 		return;
8376 	pf->service_timer_previous = jiffies;
8377 
8378 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
8379 	    (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
8380 		i40e_link_event(pf);
8381 
8382 	/* Update the stats for active netdevs so the network stack
8383 	 * can look at updated numbers whenever it cares to
8384 	 */
8385 	for (i = 0; i < pf->num_alloc_vsi; i++)
8386 		if (pf->vsi[i] && pf->vsi[i]->netdev)
8387 			i40e_update_stats(pf->vsi[i]);
8388 
8389 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
8390 		/* Update the stats for the active switching components */
8391 		for (i = 0; i < I40E_MAX_VEB; i++)
8392 			if (pf->veb[i])
8393 				i40e_update_veb_stats(pf->veb[i]);
8394 	}
8395 
8396 	i40e_ptp_rx_hang(pf);
8397 	i40e_ptp_tx_hang(pf);
8398 }
8399 
8400 /**
8401  * i40e_reset_subtask - Set up for resetting the device and driver
8402  * @pf: board private structure
8403  **/
8404 static void i40e_reset_subtask(struct i40e_pf *pf)
8405 {
8406 	u32 reset_flags = 0;
8407 
8408 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
8409 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
8410 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
8411 	}
8412 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
8413 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
8414 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8415 	}
8416 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
8417 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
8418 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
8419 	}
8420 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
8421 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
8422 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
8423 	}
8424 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
8425 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
8426 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
8427 	}
8428 
8429 	/* If there's a recovery already waiting, it takes
8430 	 * precedence before starting a new reset sequence.
8431 	 */
8432 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
8433 		i40e_prep_for_reset(pf, false);
8434 		i40e_reset(pf);
8435 		i40e_rebuild(pf, false, false);
8436 	}
8437 
8438 	/* If we're already down or resetting, just bail */
8439 	if (reset_flags &&
8440 	    !test_bit(__I40E_DOWN, pf->state) &&
8441 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
8442 		i40e_do_reset(pf, reset_flags, false);
8443 	}
8444 }
8445 
8446 /**
8447  * i40e_handle_link_event - Handle link event
8448  * @pf: board private structure
8449  * @e: event info posted on ARQ
8450  **/
8451 static void i40e_handle_link_event(struct i40e_pf *pf,
8452 				   struct i40e_arq_event_info *e)
8453 {
8454 	struct i40e_aqc_get_link_status *status =
8455 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
8456 
8457 	/* Do a new status request to re-enable LSE reporting
8458 	 * and load new status information into the hw struct
8459 	 * This completely ignores any state information
8460 	 * in the ARQ event info, instead choosing to always
8461 	 * issue the AQ update link status command.
8462 	 */
8463 	i40e_link_event(pf);
8464 
8465 	/* Check if module meets thermal requirements */
8466 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
8467 		dev_err(&pf->pdev->dev,
8468 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
8469 		dev_err(&pf->pdev->dev,
8470 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8471 	} else {
8472 		/* check for unqualified module, if link is down, suppress
8473 		 * the message if link was forced to be down.
8474 		 */
8475 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
8476 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
8477 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
8478 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
8479 			dev_err(&pf->pdev->dev,
8480 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
8481 			dev_err(&pf->pdev->dev,
8482 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8483 		}
8484 	}
8485 }
8486 
8487 /**
8488  * i40e_clean_adminq_subtask - Clean the AdminQ rings
8489  * @pf: board private structure
8490  **/
8491 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
8492 {
8493 	struct i40e_arq_event_info event;
8494 	struct i40e_hw *hw = &pf->hw;
8495 	u16 pending, i = 0;
8496 	i40e_status ret;
8497 	u16 opcode;
8498 	u32 oldval;
8499 	u32 val;
8500 
8501 	/* Do not run clean AQ when PF reset fails */
8502 	if (test_bit(__I40E_RESET_FAILED, pf->state))
8503 		return;
8504 
8505 	/* check for error indications */
8506 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
8507 	oldval = val;
8508 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
8509 		if (hw->debug_mask & I40E_DEBUG_AQ)
8510 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
8511 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
8512 	}
8513 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
8514 		if (hw->debug_mask & I40E_DEBUG_AQ)
8515 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
8516 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
8517 		pf->arq_overflows++;
8518 	}
8519 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
8520 		if (hw->debug_mask & I40E_DEBUG_AQ)
8521 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
8522 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
8523 	}
8524 	if (oldval != val)
8525 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
8526 
8527 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
8528 	oldval = val;
8529 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
8530 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8531 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
8532 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
8533 	}
8534 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
8535 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8536 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
8537 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
8538 	}
8539 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
8540 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8541 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
8542 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
8543 	}
8544 	if (oldval != val)
8545 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
8546 
8547 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
8548 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
8549 	if (!event.msg_buf)
8550 		return;
8551 
8552 	do {
8553 		ret = i40e_clean_arq_element(hw, &event, &pending);
8554 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
8555 			break;
8556 		else if (ret) {
8557 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
8558 			break;
8559 		}
8560 
8561 		opcode = le16_to_cpu(event.desc.opcode);
8562 		switch (opcode) {
8563 
8564 		case i40e_aqc_opc_get_link_status:
8565 			i40e_handle_link_event(pf, &event);
8566 			break;
8567 		case i40e_aqc_opc_send_msg_to_pf:
8568 			ret = i40e_vc_process_vf_msg(pf,
8569 					le16_to_cpu(event.desc.retval),
8570 					le32_to_cpu(event.desc.cookie_high),
8571 					le32_to_cpu(event.desc.cookie_low),
8572 					event.msg_buf,
8573 					event.msg_len);
8574 			break;
8575 		case i40e_aqc_opc_lldp_update_mib:
8576 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
8577 #ifdef CONFIG_I40E_DCB
8578 			rtnl_lock();
8579 			ret = i40e_handle_lldp_event(pf, &event);
8580 			rtnl_unlock();
8581 #endif /* CONFIG_I40E_DCB */
8582 			break;
8583 		case i40e_aqc_opc_event_lan_overflow:
8584 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
8585 			i40e_handle_lan_overflow_event(pf, &event);
8586 			break;
8587 		case i40e_aqc_opc_send_msg_to_peer:
8588 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
8589 			break;
8590 		case i40e_aqc_opc_nvm_erase:
8591 		case i40e_aqc_opc_nvm_update:
8592 		case i40e_aqc_opc_oem_post_update:
8593 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
8594 				   "ARQ NVM operation 0x%04x completed\n",
8595 				   opcode);
8596 			break;
8597 		default:
8598 			dev_info(&pf->pdev->dev,
8599 				 "ARQ: Unknown event 0x%04x ignored\n",
8600 				 opcode);
8601 			break;
8602 		}
8603 	} while (i++ < pf->adminq_work_limit);
8604 
8605 	if (i < pf->adminq_work_limit)
8606 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
8607 
8608 	/* re-enable Admin queue interrupt cause */
8609 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
8610 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
8611 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
8612 	i40e_flush(hw);
8613 
8614 	kfree(event.msg_buf);
8615 }
8616 
8617 /**
8618  * i40e_verify_eeprom - make sure eeprom is good to use
8619  * @pf: board private structure
8620  **/
8621 static void i40e_verify_eeprom(struct i40e_pf *pf)
8622 {
8623 	int err;
8624 
8625 	err = i40e_diag_eeprom_test(&pf->hw);
8626 	if (err) {
8627 		/* retry in case of garbage read */
8628 		err = i40e_diag_eeprom_test(&pf->hw);
8629 		if (err) {
8630 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
8631 				 err);
8632 			set_bit(__I40E_BAD_EEPROM, pf->state);
8633 		}
8634 	}
8635 
8636 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
8637 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
8638 		clear_bit(__I40E_BAD_EEPROM, pf->state);
8639 	}
8640 }
8641 
8642 /**
8643  * i40e_enable_pf_switch_lb
8644  * @pf: pointer to the PF structure
8645  *
8646  * enable switch loop back or die - no point in a return value
8647  **/
8648 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
8649 {
8650 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8651 	struct i40e_vsi_context ctxt;
8652 	int ret;
8653 
8654 	ctxt.seid = pf->main_vsi_seid;
8655 	ctxt.pf_num = pf->hw.pf_id;
8656 	ctxt.vf_num = 0;
8657 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8658 	if (ret) {
8659 		dev_info(&pf->pdev->dev,
8660 			 "couldn't get PF vsi config, err %s aq_err %s\n",
8661 			 i40e_stat_str(&pf->hw, ret),
8662 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8663 		return;
8664 	}
8665 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8666 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8667 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8668 
8669 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8670 	if (ret) {
8671 		dev_info(&pf->pdev->dev,
8672 			 "update vsi switch failed, err %s aq_err %s\n",
8673 			 i40e_stat_str(&pf->hw, ret),
8674 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8675 	}
8676 }
8677 
8678 /**
8679  * i40e_disable_pf_switch_lb
8680  * @pf: pointer to the PF structure
8681  *
8682  * disable switch loop back or die - no point in a return value
8683  **/
8684 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
8685 {
8686 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8687 	struct i40e_vsi_context ctxt;
8688 	int ret;
8689 
8690 	ctxt.seid = pf->main_vsi_seid;
8691 	ctxt.pf_num = pf->hw.pf_id;
8692 	ctxt.vf_num = 0;
8693 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8694 	if (ret) {
8695 		dev_info(&pf->pdev->dev,
8696 			 "couldn't get PF vsi config, err %s aq_err %s\n",
8697 			 i40e_stat_str(&pf->hw, ret),
8698 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8699 		return;
8700 	}
8701 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8702 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8703 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8704 
8705 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8706 	if (ret) {
8707 		dev_info(&pf->pdev->dev,
8708 			 "update vsi switch failed, err %s aq_err %s\n",
8709 			 i40e_stat_str(&pf->hw, ret),
8710 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8711 	}
8712 }
8713 
8714 /**
8715  * i40e_config_bridge_mode - Configure the HW bridge mode
8716  * @veb: pointer to the bridge instance
8717  *
8718  * Configure the loop back mode for the LAN VSI that is downlink to the
8719  * specified HW bridge instance. It is expected this function is called
8720  * when a new HW bridge is instantiated.
8721  **/
8722 static void i40e_config_bridge_mode(struct i40e_veb *veb)
8723 {
8724 	struct i40e_pf *pf = veb->pf;
8725 
8726 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
8727 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
8728 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
8729 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
8730 		i40e_disable_pf_switch_lb(pf);
8731 	else
8732 		i40e_enable_pf_switch_lb(pf);
8733 }
8734 
8735 /**
8736  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
8737  * @veb: pointer to the VEB instance
8738  *
8739  * This is a recursive function that first builds the attached VSIs then
8740  * recurses in to build the next layer of VEB.  We track the connections
8741  * through our own index numbers because the seid's from the HW could
8742  * change across the reset.
8743  **/
8744 static int i40e_reconstitute_veb(struct i40e_veb *veb)
8745 {
8746 	struct i40e_vsi *ctl_vsi = NULL;
8747 	struct i40e_pf *pf = veb->pf;
8748 	int v, veb_idx;
8749 	int ret;
8750 
8751 	/* build VSI that owns this VEB, temporarily attached to base VEB */
8752 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
8753 		if (pf->vsi[v] &&
8754 		    pf->vsi[v]->veb_idx == veb->idx &&
8755 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
8756 			ctl_vsi = pf->vsi[v];
8757 			break;
8758 		}
8759 	}
8760 	if (!ctl_vsi) {
8761 		dev_info(&pf->pdev->dev,
8762 			 "missing owner VSI for veb_idx %d\n", veb->idx);
8763 		ret = -ENOENT;
8764 		goto end_reconstitute;
8765 	}
8766 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
8767 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
8768 	ret = i40e_add_vsi(ctl_vsi);
8769 	if (ret) {
8770 		dev_info(&pf->pdev->dev,
8771 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
8772 			 veb->idx, ret);
8773 		goto end_reconstitute;
8774 	}
8775 	i40e_vsi_reset_stats(ctl_vsi);
8776 
8777 	/* create the VEB in the switch and move the VSI onto the VEB */
8778 	ret = i40e_add_veb(veb, ctl_vsi);
8779 	if (ret)
8780 		goto end_reconstitute;
8781 
8782 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
8783 		veb->bridge_mode = BRIDGE_MODE_VEB;
8784 	else
8785 		veb->bridge_mode = BRIDGE_MODE_VEPA;
8786 	i40e_config_bridge_mode(veb);
8787 
8788 	/* create the remaining VSIs attached to this VEB */
8789 	for (v = 0; v < pf->num_alloc_vsi; v++) {
8790 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
8791 			continue;
8792 
8793 		if (pf->vsi[v]->veb_idx == veb->idx) {
8794 			struct i40e_vsi *vsi = pf->vsi[v];
8795 
8796 			vsi->uplink_seid = veb->seid;
8797 			ret = i40e_add_vsi(vsi);
8798 			if (ret) {
8799 				dev_info(&pf->pdev->dev,
8800 					 "rebuild of vsi_idx %d failed: %d\n",
8801 					 v, ret);
8802 				goto end_reconstitute;
8803 			}
8804 			i40e_vsi_reset_stats(vsi);
8805 		}
8806 	}
8807 
8808 	/* create any VEBs attached to this VEB - RECURSION */
8809 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
8810 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
8811 			pf->veb[veb_idx]->uplink_seid = veb->seid;
8812 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
8813 			if (ret)
8814 				break;
8815 		}
8816 	}
8817 
8818 end_reconstitute:
8819 	return ret;
8820 }
8821 
8822 /**
8823  * i40e_get_capabilities - get info about the HW
8824  * @pf: the PF struct
8825  **/
8826 static int i40e_get_capabilities(struct i40e_pf *pf,
8827 				 enum i40e_admin_queue_opc list_type)
8828 {
8829 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
8830 	u16 data_size;
8831 	int buf_len;
8832 	int err;
8833 
8834 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
8835 	do {
8836 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
8837 		if (!cap_buf)
8838 			return -ENOMEM;
8839 
8840 		/* this loads the data into the hw struct for us */
8841 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
8842 						    &data_size, list_type,
8843 						    NULL);
8844 		/* data loaded, buffer no longer needed */
8845 		kfree(cap_buf);
8846 
8847 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
8848 			/* retry with a larger buffer */
8849 			buf_len = data_size;
8850 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
8851 			dev_info(&pf->pdev->dev,
8852 				 "capability discovery failed, err %s aq_err %s\n",
8853 				 i40e_stat_str(&pf->hw, err),
8854 				 i40e_aq_str(&pf->hw,
8855 					     pf->hw.aq.asq_last_status));
8856 			return -ENODEV;
8857 		}
8858 	} while (err);
8859 
8860 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
8861 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
8862 			dev_info(&pf->pdev->dev,
8863 				 "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",
8864 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
8865 				 pf->hw.func_caps.num_msix_vectors,
8866 				 pf->hw.func_caps.num_msix_vectors_vf,
8867 				 pf->hw.func_caps.fd_filters_guaranteed,
8868 				 pf->hw.func_caps.fd_filters_best_effort,
8869 				 pf->hw.func_caps.num_tx_qp,
8870 				 pf->hw.func_caps.num_vsis);
8871 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
8872 			dev_info(&pf->pdev->dev,
8873 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
8874 				 pf->hw.dev_caps.switch_mode,
8875 				 pf->hw.dev_caps.valid_functions);
8876 			dev_info(&pf->pdev->dev,
8877 				 "SR-IOV=%d, num_vfs for all function=%u\n",
8878 				 pf->hw.dev_caps.sr_iov_1_1,
8879 				 pf->hw.dev_caps.num_vfs);
8880 			dev_info(&pf->pdev->dev,
8881 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
8882 				 pf->hw.dev_caps.num_vsis,
8883 				 pf->hw.dev_caps.num_rx_qp,
8884 				 pf->hw.dev_caps.num_tx_qp);
8885 		}
8886 	}
8887 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
8888 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
8889 		       + pf->hw.func_caps.num_vfs)
8890 		if (pf->hw.revision_id == 0 &&
8891 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
8892 			dev_info(&pf->pdev->dev,
8893 				 "got num_vsis %d, setting num_vsis to %d\n",
8894 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
8895 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
8896 		}
8897 	}
8898 	return 0;
8899 }
8900 
8901 static int i40e_vsi_clear(struct i40e_vsi *vsi);
8902 
8903 /**
8904  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
8905  * @pf: board private structure
8906  **/
8907 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
8908 {
8909 	struct i40e_vsi *vsi;
8910 
8911 	/* quick workaround for an NVM issue that leaves a critical register
8912 	 * uninitialized
8913 	 */
8914 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
8915 		static const u32 hkey[] = {
8916 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
8917 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
8918 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
8919 			0x95b3a76d};
8920 		int i;
8921 
8922 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
8923 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
8924 	}
8925 
8926 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8927 		return;
8928 
8929 	/* find existing VSI and see if it needs configuring */
8930 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
8931 
8932 	/* create a new VSI if none exists */
8933 	if (!vsi) {
8934 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
8935 				     pf->vsi[pf->lan_vsi]->seid, 0);
8936 		if (!vsi) {
8937 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
8938 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8939 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
8940 			return;
8941 		}
8942 	}
8943 
8944 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
8945 }
8946 
8947 /**
8948  * i40e_fdir_teardown - release the Flow Director resources
8949  * @pf: board private structure
8950  **/
8951 static void i40e_fdir_teardown(struct i40e_pf *pf)
8952 {
8953 	struct i40e_vsi *vsi;
8954 
8955 	i40e_fdir_filter_exit(pf);
8956 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
8957 	if (vsi)
8958 		i40e_vsi_release(vsi);
8959 }
8960 
8961 /**
8962  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
8963  * @vsi: PF main vsi
8964  * @seid: seid of main or channel VSIs
8965  *
8966  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
8967  * existed before reset
8968  **/
8969 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
8970 {
8971 	struct i40e_cloud_filter *cfilter;
8972 	struct i40e_pf *pf = vsi->back;
8973 	struct hlist_node *node;
8974 	i40e_status ret;
8975 
8976 	/* Add cloud filters back if they exist */
8977 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
8978 				  cloud_node) {
8979 		if (cfilter->seid != seid)
8980 			continue;
8981 
8982 		if (cfilter->dst_port)
8983 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
8984 								true);
8985 		else
8986 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
8987 
8988 		if (ret) {
8989 			dev_dbg(&pf->pdev->dev,
8990 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
8991 				i40e_stat_str(&pf->hw, ret),
8992 				i40e_aq_str(&pf->hw,
8993 					    pf->hw.aq.asq_last_status));
8994 			return ret;
8995 		}
8996 	}
8997 	return 0;
8998 }
8999 
9000 /**
9001  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9002  * @vsi: PF main vsi
9003  *
9004  * Rebuilds channel VSIs if they existed before reset
9005  **/
9006 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9007 {
9008 	struct i40e_channel *ch, *ch_tmp;
9009 	i40e_status ret;
9010 
9011 	if (list_empty(&vsi->ch_list))
9012 		return 0;
9013 
9014 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9015 		if (!ch->initialized)
9016 			break;
9017 		/* Proceed with creation of channel (VMDq2) VSI */
9018 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9019 		if (ret) {
9020 			dev_info(&vsi->back->pdev->dev,
9021 				 "failed to rebuild channels using uplink_seid %u\n",
9022 				 vsi->uplink_seid);
9023 			return ret;
9024 		}
9025 		/* Reconfigure TX queues using QTX_CTL register */
9026 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9027 		if (ret) {
9028 			dev_info(&vsi->back->pdev->dev,
9029 				 "failed to configure TX rings for channel %u\n",
9030 				 ch->seid);
9031 			return ret;
9032 		}
9033 		/* update 'next_base_queue' */
9034 		vsi->next_base_queue = vsi->next_base_queue +
9035 							ch->num_queue_pairs;
9036 		if (ch->max_tx_rate) {
9037 			u64 credits = ch->max_tx_rate;
9038 
9039 			if (i40e_set_bw_limit(vsi, ch->seid,
9040 					      ch->max_tx_rate))
9041 				return -EINVAL;
9042 
9043 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9044 			dev_dbg(&vsi->back->pdev->dev,
9045 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9046 				ch->max_tx_rate,
9047 				credits,
9048 				ch->seid);
9049 		}
9050 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9051 		if (ret) {
9052 			dev_dbg(&vsi->back->pdev->dev,
9053 				"Failed to rebuild cloud filters for channel VSI %u\n",
9054 				ch->seid);
9055 			return ret;
9056 		}
9057 	}
9058 	return 0;
9059 }
9060 
9061 /**
9062  * i40e_prep_for_reset - prep for the core to reset
9063  * @pf: board private structure
9064  * @lock_acquired: indicates whether or not the lock has been acquired
9065  * before this function was called.
9066  *
9067  * Close up the VFs and other things in prep for PF Reset.
9068   **/
9069 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9070 {
9071 	struct i40e_hw *hw = &pf->hw;
9072 	i40e_status ret = 0;
9073 	u32 v;
9074 
9075 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9076 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9077 		return;
9078 	if (i40e_check_asq_alive(&pf->hw))
9079 		i40e_vc_notify_reset(pf);
9080 
9081 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9082 
9083 	/* quiesce the VSIs and their queues that are not already DOWN */
9084 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9085 	if (!lock_acquired)
9086 		rtnl_lock();
9087 	i40e_pf_quiesce_all_vsi(pf);
9088 	if (!lock_acquired)
9089 		rtnl_unlock();
9090 
9091 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9092 		if (pf->vsi[v])
9093 			pf->vsi[v]->seid = 0;
9094 	}
9095 
9096 	i40e_shutdown_adminq(&pf->hw);
9097 
9098 	/* call shutdown HMC */
9099 	if (hw->hmc.hmc_obj) {
9100 		ret = i40e_shutdown_lan_hmc(hw);
9101 		if (ret)
9102 			dev_warn(&pf->pdev->dev,
9103 				 "shutdown_lan_hmc failed: %d\n", ret);
9104 	}
9105 }
9106 
9107 /**
9108  * i40e_send_version - update firmware with driver version
9109  * @pf: PF struct
9110  */
9111 static void i40e_send_version(struct i40e_pf *pf)
9112 {
9113 	struct i40e_driver_version dv;
9114 
9115 	dv.major_version = DRV_VERSION_MAJOR;
9116 	dv.minor_version = DRV_VERSION_MINOR;
9117 	dv.build_version = DRV_VERSION_BUILD;
9118 	dv.subbuild_version = 0;
9119 	strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
9120 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9121 }
9122 
9123 /**
9124  * i40e_get_oem_version - get OEM specific version information
9125  * @hw: pointer to the hardware structure
9126  **/
9127 static void i40e_get_oem_version(struct i40e_hw *hw)
9128 {
9129 	u16 block_offset = 0xffff;
9130 	u16 block_length = 0;
9131 	u16 capabilities = 0;
9132 	u16 gen_snap = 0;
9133 	u16 release = 0;
9134 
9135 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
9136 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
9137 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
9138 #define I40E_NVM_OEM_GEN_OFFSET			0x02
9139 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
9140 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
9141 #define I40E_NVM_OEM_LENGTH			3
9142 
9143 	/* Check if pointer to OEM version block is valid. */
9144 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9145 	if (block_offset == 0xffff)
9146 		return;
9147 
9148 	/* Check if OEM version block has correct length. */
9149 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9150 			   &block_length);
9151 	if (block_length < I40E_NVM_OEM_LENGTH)
9152 		return;
9153 
9154 	/* Check if OEM version format is as expected. */
9155 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9156 			   &capabilities);
9157 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9158 		return;
9159 
9160 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9161 			   &gen_snap);
9162 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9163 			   &release);
9164 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9165 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9166 }
9167 
9168 /**
9169  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9170  * @pf: board private structure
9171  **/
9172 static int i40e_reset(struct i40e_pf *pf)
9173 {
9174 	struct i40e_hw *hw = &pf->hw;
9175 	i40e_status ret;
9176 
9177 	ret = i40e_pf_reset(hw);
9178 	if (ret) {
9179 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9180 		set_bit(__I40E_RESET_FAILED, pf->state);
9181 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9182 	} else {
9183 		pf->pfr_count++;
9184 	}
9185 	return ret;
9186 }
9187 
9188 /**
9189  * i40e_rebuild - rebuild using a saved config
9190  * @pf: board private structure
9191  * @reinit: if the Main VSI needs to re-initialized.
9192  * @lock_acquired: indicates whether or not the lock has been acquired
9193  * before this function was called.
9194  **/
9195 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9196 {
9197 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9198 	struct i40e_hw *hw = &pf->hw;
9199 	u8 set_fc_aq_fail = 0;
9200 	i40e_status ret;
9201 	u32 val;
9202 	int v;
9203 
9204 	if (test_bit(__I40E_DOWN, pf->state))
9205 		goto clear_recovery;
9206 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
9207 
9208 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9209 	ret = i40e_init_adminq(&pf->hw);
9210 	if (ret) {
9211 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9212 			 i40e_stat_str(&pf->hw, ret),
9213 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9214 		goto clear_recovery;
9215 	}
9216 	i40e_get_oem_version(&pf->hw);
9217 
9218 	/* re-verify the eeprom if we just had an EMP reset */
9219 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
9220 		i40e_verify_eeprom(pf);
9221 
9222 	i40e_clear_pxe_mode(hw);
9223 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
9224 	if (ret)
9225 		goto end_core_reset;
9226 
9227 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
9228 				hw->func_caps.num_rx_qp, 0, 0);
9229 	if (ret) {
9230 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
9231 		goto end_core_reset;
9232 	}
9233 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9234 	if (ret) {
9235 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
9236 		goto end_core_reset;
9237 	}
9238 
9239 	/* Enable FW to write a default DCB config on link-up */
9240 	i40e_aq_set_dcb_parameters(hw, true, NULL);
9241 
9242 #ifdef CONFIG_I40E_DCB
9243 	ret = i40e_init_pf_dcb(pf);
9244 	if (ret) {
9245 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
9246 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9247 		/* Continue without DCB enabled */
9248 	}
9249 #endif /* CONFIG_I40E_DCB */
9250 	/* do basic switch setup */
9251 	if (!lock_acquired)
9252 		rtnl_lock();
9253 	ret = i40e_setup_pf_switch(pf, reinit);
9254 	if (ret)
9255 		goto end_unlock;
9256 
9257 	/* The driver only wants link up/down and module qualification
9258 	 * reports from firmware.  Note the negative logic.
9259 	 */
9260 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
9261 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
9262 					 I40E_AQ_EVENT_MEDIA_NA |
9263 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
9264 	if (ret)
9265 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
9266 			 i40e_stat_str(&pf->hw, ret),
9267 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9268 
9269 	/* make sure our flow control settings are restored */
9270 	ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
9271 	if (ret)
9272 		dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
9273 			i40e_stat_str(&pf->hw, ret),
9274 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9275 
9276 	/* Rebuild the VSIs and VEBs that existed before reset.
9277 	 * They are still in our local switch element arrays, so only
9278 	 * need to rebuild the switch model in the HW.
9279 	 *
9280 	 * If there were VEBs but the reconstitution failed, we'll try
9281 	 * try to recover minimal use by getting the basic PF VSI working.
9282 	 */
9283 	if (vsi->uplink_seid != pf->mac_seid) {
9284 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
9285 		/* find the one VEB connected to the MAC, and find orphans */
9286 		for (v = 0; v < I40E_MAX_VEB; v++) {
9287 			if (!pf->veb[v])
9288 				continue;
9289 
9290 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
9291 			    pf->veb[v]->uplink_seid == 0) {
9292 				ret = i40e_reconstitute_veb(pf->veb[v]);
9293 
9294 				if (!ret)
9295 					continue;
9296 
9297 				/* If Main VEB failed, we're in deep doodoo,
9298 				 * so give up rebuilding the switch and set up
9299 				 * for minimal rebuild of PF VSI.
9300 				 * If orphan failed, we'll report the error
9301 				 * but try to keep going.
9302 				 */
9303 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
9304 					dev_info(&pf->pdev->dev,
9305 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
9306 						 ret);
9307 					vsi->uplink_seid = pf->mac_seid;
9308 					break;
9309 				} else if (pf->veb[v]->uplink_seid == 0) {
9310 					dev_info(&pf->pdev->dev,
9311 						 "rebuild of orphan VEB failed: %d\n",
9312 						 ret);
9313 				}
9314 			}
9315 		}
9316 	}
9317 
9318 	if (vsi->uplink_seid == pf->mac_seid) {
9319 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
9320 		/* no VEB, so rebuild only the Main VSI */
9321 		ret = i40e_add_vsi(vsi);
9322 		if (ret) {
9323 			dev_info(&pf->pdev->dev,
9324 				 "rebuild of Main VSI failed: %d\n", ret);
9325 			goto end_unlock;
9326 		}
9327 	}
9328 
9329 	if (vsi->mqprio_qopt.max_rate[0]) {
9330 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9331 		u64 credits = 0;
9332 
9333 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
9334 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
9335 		if (ret)
9336 			goto end_unlock;
9337 
9338 		credits = max_tx_rate;
9339 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
9340 		dev_dbg(&vsi->back->pdev->dev,
9341 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9342 			max_tx_rate,
9343 			credits,
9344 			vsi->seid);
9345 	}
9346 
9347 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
9348 	if (ret)
9349 		goto end_unlock;
9350 
9351 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
9352 	 * for this main VSI if they exist
9353 	 */
9354 	ret = i40e_rebuild_channels(vsi);
9355 	if (ret)
9356 		goto end_unlock;
9357 
9358 	/* Reconfigure hardware for allowing smaller MSS in the case
9359 	 * of TSO, so that we avoid the MDD being fired and causing
9360 	 * a reset in the case of small MSS+TSO.
9361 	 */
9362 #define I40E_REG_MSS          0x000E64DC
9363 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
9364 #define I40E_64BYTE_MSS       0x400000
9365 	val = rd32(hw, I40E_REG_MSS);
9366 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
9367 		val &= ~I40E_REG_MSS_MIN_MASK;
9368 		val |= I40E_64BYTE_MSS;
9369 		wr32(hw, I40E_REG_MSS, val);
9370 	}
9371 
9372 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
9373 		msleep(75);
9374 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9375 		if (ret)
9376 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
9377 				 i40e_stat_str(&pf->hw, ret),
9378 				 i40e_aq_str(&pf->hw,
9379 					     pf->hw.aq.asq_last_status));
9380 	}
9381 	/* reinit the misc interrupt */
9382 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9383 		ret = i40e_setup_misc_vector(pf);
9384 
9385 	/* Add a filter to drop all Flow control frames from any VSI from being
9386 	 * transmitted. By doing so we stop a malicious VF from sending out
9387 	 * PAUSE or PFC frames and potentially controlling traffic for other
9388 	 * PF/VF VSIs.
9389 	 * The FW can still send Flow control frames if enabled.
9390 	 */
9391 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
9392 						       pf->main_vsi_seid);
9393 
9394 	/* restart the VSIs that were rebuilt and running before the reset */
9395 	i40e_pf_unquiesce_all_vsi(pf);
9396 
9397 	/* Release the RTNL lock before we start resetting VFs */
9398 	if (!lock_acquired)
9399 		rtnl_unlock();
9400 
9401 	/* Restore promiscuous settings */
9402 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
9403 	if (ret)
9404 		dev_warn(&pf->pdev->dev,
9405 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
9406 			 pf->cur_promisc ? "on" : "off",
9407 			 i40e_stat_str(&pf->hw, ret),
9408 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9409 
9410 	i40e_reset_all_vfs(pf, true);
9411 
9412 	/* tell the firmware that we're starting */
9413 	i40e_send_version(pf);
9414 
9415 	/* We've already released the lock, so don't do it again */
9416 	goto end_core_reset;
9417 
9418 end_unlock:
9419 	if (!lock_acquired)
9420 		rtnl_unlock();
9421 end_core_reset:
9422 	clear_bit(__I40E_RESET_FAILED, pf->state);
9423 clear_recovery:
9424 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9425 }
9426 
9427 /**
9428  * i40e_reset_and_rebuild - reset and rebuild using a saved config
9429  * @pf: board private structure
9430  * @reinit: if the Main VSI needs to re-initialized.
9431  * @lock_acquired: indicates whether or not the lock has been acquired
9432  * before this function was called.
9433  **/
9434 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
9435 				   bool lock_acquired)
9436 {
9437 	int ret;
9438 	/* Now we wait for GRST to settle out.
9439 	 * We don't have to delete the VEBs or VSIs from the hw switch
9440 	 * because the reset will make them disappear.
9441 	 */
9442 	ret = i40e_reset(pf);
9443 	if (!ret)
9444 		i40e_rebuild(pf, reinit, lock_acquired);
9445 }
9446 
9447 /**
9448  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
9449  * @pf: board private structure
9450  *
9451  * Close up the VFs and other things in prep for a Core Reset,
9452  * then get ready to rebuild the world.
9453  * @lock_acquired: indicates whether or not the lock has been acquired
9454  * before this function was called.
9455  **/
9456 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
9457 {
9458 	i40e_prep_for_reset(pf, lock_acquired);
9459 	i40e_reset_and_rebuild(pf, false, lock_acquired);
9460 }
9461 
9462 /**
9463  * i40e_handle_mdd_event
9464  * @pf: pointer to the PF structure
9465  *
9466  * Called from the MDD irq handler to identify possibly malicious vfs
9467  **/
9468 static void i40e_handle_mdd_event(struct i40e_pf *pf)
9469 {
9470 	struct i40e_hw *hw = &pf->hw;
9471 	bool mdd_detected = false;
9472 	bool pf_mdd_detected = false;
9473 	struct i40e_vf *vf;
9474 	u32 reg;
9475 	int i;
9476 
9477 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
9478 		return;
9479 
9480 	/* find what triggered the MDD event */
9481 	reg = rd32(hw, I40E_GL_MDET_TX);
9482 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
9483 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
9484 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
9485 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
9486 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
9487 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
9488 				I40E_GL_MDET_TX_EVENT_SHIFT;
9489 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
9490 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
9491 				pf->hw.func_caps.base_queue;
9492 		if (netif_msg_tx_err(pf))
9493 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
9494 				 event, queue, pf_num, vf_num);
9495 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
9496 		mdd_detected = true;
9497 	}
9498 	reg = rd32(hw, I40E_GL_MDET_RX);
9499 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
9500 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
9501 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
9502 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
9503 				I40E_GL_MDET_RX_EVENT_SHIFT;
9504 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
9505 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
9506 				pf->hw.func_caps.base_queue;
9507 		if (netif_msg_rx_err(pf))
9508 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
9509 				 event, queue, func);
9510 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
9511 		mdd_detected = true;
9512 	}
9513 
9514 	if (mdd_detected) {
9515 		reg = rd32(hw, I40E_PF_MDET_TX);
9516 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
9517 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
9518 			dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
9519 			pf_mdd_detected = true;
9520 		}
9521 		reg = rd32(hw, I40E_PF_MDET_RX);
9522 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
9523 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
9524 			dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
9525 			pf_mdd_detected = true;
9526 		}
9527 		/* Queue belongs to the PF, initiate a reset */
9528 		if (pf_mdd_detected) {
9529 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9530 			i40e_service_event_schedule(pf);
9531 		}
9532 	}
9533 
9534 	/* see if one of the VFs needs its hand slapped */
9535 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
9536 		vf = &(pf->vf[i]);
9537 		reg = rd32(hw, I40E_VP_MDET_TX(i));
9538 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
9539 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
9540 			vf->num_mdd_events++;
9541 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
9542 				 i);
9543 		}
9544 
9545 		reg = rd32(hw, I40E_VP_MDET_RX(i));
9546 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
9547 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
9548 			vf->num_mdd_events++;
9549 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
9550 				 i);
9551 		}
9552 
9553 		if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
9554 			dev_info(&pf->pdev->dev,
9555 				 "Too many MDD events on VF %d, disabled\n", i);
9556 			dev_info(&pf->pdev->dev,
9557 				 "Use PF Control I/F to re-enable the VF\n");
9558 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
9559 		}
9560 	}
9561 
9562 	/* re-enable mdd interrupt cause */
9563 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
9564 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
9565 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
9566 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
9567 	i40e_flush(hw);
9568 }
9569 
9570 static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
9571 {
9572 	switch (port->type) {
9573 	case UDP_TUNNEL_TYPE_VXLAN:
9574 		return "vxlan";
9575 	case UDP_TUNNEL_TYPE_GENEVE:
9576 		return "geneve";
9577 	default:
9578 		return "unknown";
9579 	}
9580 }
9581 
9582 /**
9583  * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
9584  * @pf: board private structure
9585  **/
9586 static void i40e_sync_udp_filters(struct i40e_pf *pf)
9587 {
9588 	int i;
9589 
9590 	/* loop through and set pending bit for all active UDP filters */
9591 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9592 		if (pf->udp_ports[i].port)
9593 			pf->pending_udp_bitmap |= BIT_ULL(i);
9594 	}
9595 
9596 	pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9597 }
9598 
9599 /**
9600  * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
9601  * @pf: board private structure
9602  **/
9603 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
9604 {
9605 	struct i40e_hw *hw = &pf->hw;
9606 	i40e_status ret;
9607 	u16 port;
9608 	int i;
9609 
9610 	if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
9611 		return;
9612 
9613 	pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
9614 
9615 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9616 		if (pf->pending_udp_bitmap & BIT_ULL(i)) {
9617 			pf->pending_udp_bitmap &= ~BIT_ULL(i);
9618 			port = pf->udp_ports[i].port;
9619 			if (port)
9620 				ret = i40e_aq_add_udp_tunnel(hw, port,
9621 							pf->udp_ports[i].type,
9622 							NULL, NULL);
9623 			else
9624 				ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
9625 
9626 			if (ret) {
9627 				dev_info(&pf->pdev->dev,
9628 					 "%s %s port %d, index %d failed, err %s aq_err %s\n",
9629 					 i40e_tunnel_name(&pf->udp_ports[i]),
9630 					 port ? "add" : "delete",
9631 					 port, i,
9632 					 i40e_stat_str(&pf->hw, ret),
9633 					 i40e_aq_str(&pf->hw,
9634 						     pf->hw.aq.asq_last_status));
9635 				pf->udp_ports[i].port = 0;
9636 			}
9637 		}
9638 	}
9639 }
9640 
9641 /**
9642  * i40e_service_task - Run the driver's async subtasks
9643  * @work: pointer to work_struct containing our data
9644  **/
9645 static void i40e_service_task(struct work_struct *work)
9646 {
9647 	struct i40e_pf *pf = container_of(work,
9648 					  struct i40e_pf,
9649 					  service_task);
9650 	unsigned long start_time = jiffies;
9651 
9652 	/* don't bother with service tasks if a reset is in progress */
9653 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9654 		return;
9655 
9656 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
9657 		return;
9658 
9659 	i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
9660 	i40e_sync_filters_subtask(pf);
9661 	i40e_reset_subtask(pf);
9662 	i40e_handle_mdd_event(pf);
9663 	i40e_vc_process_vflr_event(pf);
9664 	i40e_watchdog_subtask(pf);
9665 	i40e_fdir_reinit_subtask(pf);
9666 	if (pf->flags & I40E_FLAG_CLIENT_RESET) {
9667 		/* Client subtask will reopen next time through. */
9668 		i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
9669 		pf->flags &= ~I40E_FLAG_CLIENT_RESET;
9670 	} else {
9671 		i40e_client_subtask(pf);
9672 		if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
9673 			i40e_notify_client_of_l2_param_changes(
9674 							pf->vsi[pf->lan_vsi]);
9675 			pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
9676 		}
9677 	}
9678 	i40e_sync_filters_subtask(pf);
9679 	i40e_sync_udp_filters_subtask(pf);
9680 	i40e_clean_adminq_subtask(pf);
9681 
9682 	/* flush memory to make sure state is correct before next watchdog */
9683 	smp_mb__before_atomic();
9684 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
9685 
9686 	/* If the tasks have taken longer than one timer cycle or there
9687 	 * is more work to be done, reschedule the service task now
9688 	 * rather than wait for the timer to tick again.
9689 	 */
9690 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
9691 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
9692 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
9693 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
9694 		i40e_service_event_schedule(pf);
9695 }
9696 
9697 /**
9698  * i40e_service_timer - timer callback
9699  * @data: pointer to PF struct
9700  **/
9701 static void i40e_service_timer(struct timer_list *t)
9702 {
9703 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
9704 
9705 	mod_timer(&pf->service_timer,
9706 		  round_jiffies(jiffies + pf->service_timer_period));
9707 	i40e_service_event_schedule(pf);
9708 }
9709 
9710 /**
9711  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
9712  * @vsi: the VSI being configured
9713  **/
9714 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
9715 {
9716 	struct i40e_pf *pf = vsi->back;
9717 
9718 	switch (vsi->type) {
9719 	case I40E_VSI_MAIN:
9720 		vsi->alloc_queue_pairs = pf->num_lan_qps;
9721 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9722 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9723 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9724 			vsi->num_q_vectors = pf->num_lan_msix;
9725 		else
9726 			vsi->num_q_vectors = 1;
9727 
9728 		break;
9729 
9730 	case I40E_VSI_FDIR:
9731 		vsi->alloc_queue_pairs = 1;
9732 		vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
9733 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9734 		vsi->num_q_vectors = pf->num_fdsb_msix;
9735 		break;
9736 
9737 	case I40E_VSI_VMDQ2:
9738 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
9739 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9740 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9741 		vsi->num_q_vectors = pf->num_vmdq_msix;
9742 		break;
9743 
9744 	case I40E_VSI_SRIOV:
9745 		vsi->alloc_queue_pairs = pf->num_vf_qps;
9746 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9747 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9748 		break;
9749 
9750 	default:
9751 		WARN_ON(1);
9752 		return -ENODATA;
9753 	}
9754 
9755 	return 0;
9756 }
9757 
9758 /**
9759  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
9760  * @vsi: VSI pointer
9761  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
9762  *
9763  * On error: returns error code (negative)
9764  * On success: returns 0
9765  **/
9766 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
9767 {
9768 	struct i40e_ring **next_rings;
9769 	int size;
9770 	int ret = 0;
9771 
9772 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
9773 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
9774 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
9775 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
9776 	if (!vsi->tx_rings)
9777 		return -ENOMEM;
9778 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
9779 	if (i40e_enabled_xdp_vsi(vsi)) {
9780 		vsi->xdp_rings = next_rings;
9781 		next_rings += vsi->alloc_queue_pairs;
9782 	}
9783 	vsi->rx_rings = next_rings;
9784 
9785 	if (alloc_qvectors) {
9786 		/* allocate memory for q_vector pointers */
9787 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
9788 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
9789 		if (!vsi->q_vectors) {
9790 			ret = -ENOMEM;
9791 			goto err_vectors;
9792 		}
9793 	}
9794 	return ret;
9795 
9796 err_vectors:
9797 	kfree(vsi->tx_rings);
9798 	return ret;
9799 }
9800 
9801 /**
9802  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
9803  * @pf: board private structure
9804  * @type: type of VSI
9805  *
9806  * On error: returns error code (negative)
9807  * On success: returns vsi index in PF (positive)
9808  **/
9809 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
9810 {
9811 	int ret = -ENODEV;
9812 	struct i40e_vsi *vsi;
9813 	int vsi_idx;
9814 	int i;
9815 
9816 	/* Need to protect the allocation of the VSIs at the PF level */
9817 	mutex_lock(&pf->switch_mutex);
9818 
9819 	/* VSI list may be fragmented if VSI creation/destruction has
9820 	 * been happening.  We can afford to do a quick scan to look
9821 	 * for any free VSIs in the list.
9822 	 *
9823 	 * find next empty vsi slot, looping back around if necessary
9824 	 */
9825 	i = pf->next_vsi;
9826 	while (i < pf->num_alloc_vsi && pf->vsi[i])
9827 		i++;
9828 	if (i >= pf->num_alloc_vsi) {
9829 		i = 0;
9830 		while (i < pf->next_vsi && pf->vsi[i])
9831 			i++;
9832 	}
9833 
9834 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
9835 		vsi_idx = i;             /* Found one! */
9836 	} else {
9837 		ret = -ENODEV;
9838 		goto unlock_pf;  /* out of VSI slots! */
9839 	}
9840 	pf->next_vsi = ++i;
9841 
9842 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
9843 	if (!vsi) {
9844 		ret = -ENOMEM;
9845 		goto unlock_pf;
9846 	}
9847 	vsi->type = type;
9848 	vsi->back = pf;
9849 	set_bit(__I40E_VSI_DOWN, vsi->state);
9850 	vsi->flags = 0;
9851 	vsi->idx = vsi_idx;
9852 	vsi->int_rate_limit = 0;
9853 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
9854 				pf->rss_table_size : 64;
9855 	vsi->netdev_registered = false;
9856 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
9857 	hash_init(vsi->mac_filter_hash);
9858 	vsi->irqs_ready = false;
9859 
9860 	ret = i40e_set_num_rings_in_vsi(vsi);
9861 	if (ret)
9862 		goto err_rings;
9863 
9864 	ret = i40e_vsi_alloc_arrays(vsi, true);
9865 	if (ret)
9866 		goto err_rings;
9867 
9868 	/* Setup default MSIX irq handler for VSI */
9869 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
9870 
9871 	/* Initialize VSI lock */
9872 	spin_lock_init(&vsi->mac_filter_hash_lock);
9873 	pf->vsi[vsi_idx] = vsi;
9874 	ret = vsi_idx;
9875 	goto unlock_pf;
9876 
9877 err_rings:
9878 	pf->next_vsi = i - 1;
9879 	kfree(vsi);
9880 unlock_pf:
9881 	mutex_unlock(&pf->switch_mutex);
9882 	return ret;
9883 }
9884 
9885 /**
9886  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
9887  * @type: VSI pointer
9888  * @free_qvectors: a bool to specify if q_vectors need to be freed.
9889  *
9890  * On error: returns error code (negative)
9891  * On success: returns 0
9892  **/
9893 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
9894 {
9895 	/* free the ring and vector containers */
9896 	if (free_qvectors) {
9897 		kfree(vsi->q_vectors);
9898 		vsi->q_vectors = NULL;
9899 	}
9900 	kfree(vsi->tx_rings);
9901 	vsi->tx_rings = NULL;
9902 	vsi->rx_rings = NULL;
9903 	vsi->xdp_rings = NULL;
9904 }
9905 
9906 /**
9907  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
9908  * and lookup table
9909  * @vsi: Pointer to VSI structure
9910  */
9911 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
9912 {
9913 	if (!vsi)
9914 		return;
9915 
9916 	kfree(vsi->rss_hkey_user);
9917 	vsi->rss_hkey_user = NULL;
9918 
9919 	kfree(vsi->rss_lut_user);
9920 	vsi->rss_lut_user = NULL;
9921 }
9922 
9923 /**
9924  * i40e_vsi_clear - Deallocate the VSI provided
9925  * @vsi: the VSI being un-configured
9926  **/
9927 static int i40e_vsi_clear(struct i40e_vsi *vsi)
9928 {
9929 	struct i40e_pf *pf;
9930 
9931 	if (!vsi)
9932 		return 0;
9933 
9934 	if (!vsi->back)
9935 		goto free_vsi;
9936 	pf = vsi->back;
9937 
9938 	mutex_lock(&pf->switch_mutex);
9939 	if (!pf->vsi[vsi->idx]) {
9940 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
9941 			vsi->idx, vsi->idx, vsi, vsi->type);
9942 		goto unlock_vsi;
9943 	}
9944 
9945 	if (pf->vsi[vsi->idx] != vsi) {
9946 		dev_err(&pf->pdev->dev,
9947 			"pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
9948 			pf->vsi[vsi->idx]->idx,
9949 			pf->vsi[vsi->idx],
9950 			pf->vsi[vsi->idx]->type,
9951 			vsi->idx, vsi, vsi->type);
9952 		goto unlock_vsi;
9953 	}
9954 
9955 	/* updates the PF for this cleared vsi */
9956 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9957 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
9958 
9959 	i40e_vsi_free_arrays(vsi, true);
9960 	i40e_clear_rss_config_user(vsi);
9961 
9962 	pf->vsi[vsi->idx] = NULL;
9963 	if (vsi->idx < pf->next_vsi)
9964 		pf->next_vsi = vsi->idx;
9965 
9966 unlock_vsi:
9967 	mutex_unlock(&pf->switch_mutex);
9968 free_vsi:
9969 	kfree(vsi);
9970 
9971 	return 0;
9972 }
9973 
9974 /**
9975  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
9976  * @vsi: the VSI being cleaned
9977  **/
9978 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
9979 {
9980 	int i;
9981 
9982 	if (vsi->tx_rings && vsi->tx_rings[0]) {
9983 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
9984 			kfree_rcu(vsi->tx_rings[i], rcu);
9985 			vsi->tx_rings[i] = NULL;
9986 			vsi->rx_rings[i] = NULL;
9987 			if (vsi->xdp_rings)
9988 				vsi->xdp_rings[i] = NULL;
9989 		}
9990 	}
9991 }
9992 
9993 /**
9994  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
9995  * @vsi: the VSI being configured
9996  **/
9997 static int i40e_alloc_rings(struct i40e_vsi *vsi)
9998 {
9999 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10000 	struct i40e_pf *pf = vsi->back;
10001 	struct i40e_ring *ring;
10002 
10003 	/* Set basic values in the rings to be used later during open() */
10004 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10005 		/* allocate space for both Tx and Rx in one shot */
10006 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10007 		if (!ring)
10008 			goto err_out;
10009 
10010 		ring->queue_index = i;
10011 		ring->reg_idx = vsi->base_queue + i;
10012 		ring->ring_active = false;
10013 		ring->vsi = vsi;
10014 		ring->netdev = vsi->netdev;
10015 		ring->dev = &pf->pdev->dev;
10016 		ring->count = vsi->num_desc;
10017 		ring->size = 0;
10018 		ring->dcb_tc = 0;
10019 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10020 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10021 		ring->tx_itr_setting = pf->tx_itr_default;
10022 		vsi->tx_rings[i] = ring++;
10023 
10024 		if (!i40e_enabled_xdp_vsi(vsi))
10025 			goto setup_rx;
10026 
10027 		ring->queue_index = vsi->alloc_queue_pairs + i;
10028 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10029 		ring->ring_active = false;
10030 		ring->vsi = vsi;
10031 		ring->netdev = NULL;
10032 		ring->dev = &pf->pdev->dev;
10033 		ring->count = vsi->num_desc;
10034 		ring->size = 0;
10035 		ring->dcb_tc = 0;
10036 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10037 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10038 		set_ring_xdp(ring);
10039 		ring->tx_itr_setting = pf->tx_itr_default;
10040 		vsi->xdp_rings[i] = ring++;
10041 
10042 setup_rx:
10043 		ring->queue_index = i;
10044 		ring->reg_idx = vsi->base_queue + i;
10045 		ring->ring_active = false;
10046 		ring->vsi = vsi;
10047 		ring->netdev = vsi->netdev;
10048 		ring->dev = &pf->pdev->dev;
10049 		ring->count = vsi->num_desc;
10050 		ring->size = 0;
10051 		ring->dcb_tc = 0;
10052 		ring->rx_itr_setting = pf->rx_itr_default;
10053 		vsi->rx_rings[i] = ring;
10054 	}
10055 
10056 	return 0;
10057 
10058 err_out:
10059 	i40e_vsi_clear_rings(vsi);
10060 	return -ENOMEM;
10061 }
10062 
10063 /**
10064  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10065  * @pf: board private structure
10066  * @vectors: the number of MSI-X vectors to request
10067  *
10068  * Returns the number of vectors reserved, or error
10069  **/
10070 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10071 {
10072 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10073 					I40E_MIN_MSIX, vectors);
10074 	if (vectors < 0) {
10075 		dev_info(&pf->pdev->dev,
10076 			 "MSI-X vector reservation failed: %d\n", vectors);
10077 		vectors = 0;
10078 	}
10079 
10080 	return vectors;
10081 }
10082 
10083 /**
10084  * i40e_init_msix - Setup the MSIX capability
10085  * @pf: board private structure
10086  *
10087  * Work with the OS to set up the MSIX vectors needed.
10088  *
10089  * Returns the number of vectors reserved or negative on failure
10090  **/
10091 static int i40e_init_msix(struct i40e_pf *pf)
10092 {
10093 	struct i40e_hw *hw = &pf->hw;
10094 	int cpus, extra_vectors;
10095 	int vectors_left;
10096 	int v_budget, i;
10097 	int v_actual;
10098 	int iwarp_requested = 0;
10099 
10100 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10101 		return -ENODEV;
10102 
10103 	/* The number of vectors we'll request will be comprised of:
10104 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
10105 	 *   - The number of LAN queue pairs
10106 	 *	- Queues being used for RSS.
10107 	 *		We don't need as many as max_rss_size vectors.
10108 	 *		use rss_size instead in the calculation since that
10109 	 *		is governed by number of cpus in the system.
10110 	 *	- assumes symmetric Tx/Rx pairing
10111 	 *   - The number of VMDq pairs
10112 	 *   - The CPU count within the NUMA node if iWARP is enabled
10113 	 * Once we count this up, try the request.
10114 	 *
10115 	 * If we can't get what we want, we'll simplify to nearly nothing
10116 	 * and try again.  If that still fails, we punt.
10117 	 */
10118 	vectors_left = hw->func_caps.num_msix_vectors;
10119 	v_budget = 0;
10120 
10121 	/* reserve one vector for miscellaneous handler */
10122 	if (vectors_left) {
10123 		v_budget++;
10124 		vectors_left--;
10125 	}
10126 
10127 	/* reserve some vectors for the main PF traffic queues. Initially we
10128 	 * only reserve at most 50% of the available vectors, in the case that
10129 	 * the number of online CPUs is large. This ensures that we can enable
10130 	 * extra features as well. Once we've enabled the other features, we
10131 	 * will use any remaining vectors to reach as close as we can to the
10132 	 * number of online CPUs.
10133 	 */
10134 	cpus = num_online_cpus();
10135 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10136 	vectors_left -= pf->num_lan_msix;
10137 
10138 	/* reserve one vector for sideband flow director */
10139 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10140 		if (vectors_left) {
10141 			pf->num_fdsb_msix = 1;
10142 			v_budget++;
10143 			vectors_left--;
10144 		} else {
10145 			pf->num_fdsb_msix = 0;
10146 		}
10147 	}
10148 
10149 	/* can we reserve enough for iWARP? */
10150 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10151 		iwarp_requested = pf->num_iwarp_msix;
10152 
10153 		if (!vectors_left)
10154 			pf->num_iwarp_msix = 0;
10155 		else if (vectors_left < pf->num_iwarp_msix)
10156 			pf->num_iwarp_msix = 1;
10157 		v_budget += pf->num_iwarp_msix;
10158 		vectors_left -= pf->num_iwarp_msix;
10159 	}
10160 
10161 	/* any vectors left over go for VMDq support */
10162 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10163 		int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
10164 		int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
10165 
10166 		if (!vectors_left) {
10167 			pf->num_vmdq_msix = 0;
10168 			pf->num_vmdq_qps = 0;
10169 		} else {
10170 			/* if we're short on vectors for what's desired, we limit
10171 			 * the queues per vmdq.  If this is still more than are
10172 			 * available, the user will need to change the number of
10173 			 * queues/vectors used by the PF later with the ethtool
10174 			 * channels command
10175 			 */
10176 			if (vmdq_vecs < vmdq_vecs_wanted)
10177 				pf->num_vmdq_qps = 1;
10178 			pf->num_vmdq_msix = pf->num_vmdq_qps;
10179 
10180 			v_budget += vmdq_vecs;
10181 			vectors_left -= vmdq_vecs;
10182 		}
10183 	}
10184 
10185 	/* On systems with a large number of SMP cores, we previously limited
10186 	 * the number of vectors for num_lan_msix to be at most 50% of the
10187 	 * available vectors, to allow for other features. Now, we add back
10188 	 * the remaining vectors. However, we ensure that the total
10189 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
10190 	 * calculate the number of vectors we can add without going over the
10191 	 * cap of CPUs. For systems with a small number of CPUs this will be
10192 	 * zero.
10193 	 */
10194 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
10195 	pf->num_lan_msix += extra_vectors;
10196 	vectors_left -= extra_vectors;
10197 
10198 	WARN(vectors_left < 0,
10199 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
10200 
10201 	v_budget += pf->num_lan_msix;
10202 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
10203 				   GFP_KERNEL);
10204 	if (!pf->msix_entries)
10205 		return -ENOMEM;
10206 
10207 	for (i = 0; i < v_budget; i++)
10208 		pf->msix_entries[i].entry = i;
10209 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
10210 
10211 	if (v_actual < I40E_MIN_MSIX) {
10212 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
10213 		kfree(pf->msix_entries);
10214 		pf->msix_entries = NULL;
10215 		pci_disable_msix(pf->pdev);
10216 		return -ENODEV;
10217 
10218 	} else if (v_actual == I40E_MIN_MSIX) {
10219 		/* Adjust for minimal MSIX use */
10220 		pf->num_vmdq_vsis = 0;
10221 		pf->num_vmdq_qps = 0;
10222 		pf->num_lan_qps = 1;
10223 		pf->num_lan_msix = 1;
10224 
10225 	} else if (v_actual != v_budget) {
10226 		/* If we have limited resources, we will start with no vectors
10227 		 * for the special features and then allocate vectors to some
10228 		 * of these features based on the policy and at the end disable
10229 		 * the features that did not get any vectors.
10230 		 */
10231 		int vec;
10232 
10233 		dev_info(&pf->pdev->dev,
10234 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
10235 			 v_actual, v_budget);
10236 		/* reserve the misc vector */
10237 		vec = v_actual - 1;
10238 
10239 		/* Scale vector usage down */
10240 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
10241 		pf->num_vmdq_vsis = 1;
10242 		pf->num_vmdq_qps = 1;
10243 
10244 		/* partition out the remaining vectors */
10245 		switch (vec) {
10246 		case 2:
10247 			pf->num_lan_msix = 1;
10248 			break;
10249 		case 3:
10250 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10251 				pf->num_lan_msix = 1;
10252 				pf->num_iwarp_msix = 1;
10253 			} else {
10254 				pf->num_lan_msix = 2;
10255 			}
10256 			break;
10257 		default:
10258 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10259 				pf->num_iwarp_msix = min_t(int, (vec / 3),
10260 						 iwarp_requested);
10261 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
10262 						  I40E_DEFAULT_NUM_VMDQ_VSI);
10263 			} else {
10264 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
10265 						  I40E_DEFAULT_NUM_VMDQ_VSI);
10266 			}
10267 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10268 				pf->num_fdsb_msix = 1;
10269 				vec--;
10270 			}
10271 			pf->num_lan_msix = min_t(int,
10272 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
10273 							      pf->num_lan_msix);
10274 			pf->num_lan_qps = pf->num_lan_msix;
10275 			break;
10276 		}
10277 	}
10278 
10279 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
10280 	    (pf->num_fdsb_msix == 0)) {
10281 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
10282 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10283 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10284 	}
10285 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10286 	    (pf->num_vmdq_msix == 0)) {
10287 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
10288 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
10289 	}
10290 
10291 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
10292 	    (pf->num_iwarp_msix == 0)) {
10293 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
10294 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
10295 	}
10296 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
10297 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
10298 		   pf->num_lan_msix,
10299 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
10300 		   pf->num_fdsb_msix,
10301 		   pf->num_iwarp_msix);
10302 
10303 	return v_actual;
10304 }
10305 
10306 /**
10307  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
10308  * @vsi: the VSI being configured
10309  * @v_idx: index of the vector in the vsi struct
10310  * @cpu: cpu to be used on affinity_mask
10311  *
10312  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
10313  **/
10314 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
10315 {
10316 	struct i40e_q_vector *q_vector;
10317 
10318 	/* allocate q_vector */
10319 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
10320 	if (!q_vector)
10321 		return -ENOMEM;
10322 
10323 	q_vector->vsi = vsi;
10324 	q_vector->v_idx = v_idx;
10325 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
10326 
10327 	if (vsi->netdev)
10328 		netif_napi_add(vsi->netdev, &q_vector->napi,
10329 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
10330 
10331 	q_vector->rx.latency_range = I40E_LOW_LATENCY;
10332 	q_vector->tx.latency_range = I40E_LOW_LATENCY;
10333 
10334 	/* tie q_vector and vsi together */
10335 	vsi->q_vectors[v_idx] = q_vector;
10336 
10337 	return 0;
10338 }
10339 
10340 /**
10341  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
10342  * @vsi: the VSI being configured
10343  *
10344  * We allocate one q_vector per queue interrupt.  If allocation fails we
10345  * return -ENOMEM.
10346  **/
10347 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
10348 {
10349 	struct i40e_pf *pf = vsi->back;
10350 	int err, v_idx, num_q_vectors, current_cpu;
10351 
10352 	/* if not MSIX, give the one vector only to the LAN VSI */
10353 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10354 		num_q_vectors = vsi->num_q_vectors;
10355 	else if (vsi == pf->vsi[pf->lan_vsi])
10356 		num_q_vectors = 1;
10357 	else
10358 		return -EINVAL;
10359 
10360 	current_cpu = cpumask_first(cpu_online_mask);
10361 
10362 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
10363 		err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
10364 		if (err)
10365 			goto err_out;
10366 		current_cpu = cpumask_next(current_cpu, cpu_online_mask);
10367 		if (unlikely(current_cpu >= nr_cpu_ids))
10368 			current_cpu = cpumask_first(cpu_online_mask);
10369 	}
10370 
10371 	return 0;
10372 
10373 err_out:
10374 	while (v_idx--)
10375 		i40e_free_q_vector(vsi, v_idx);
10376 
10377 	return err;
10378 }
10379 
10380 /**
10381  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
10382  * @pf: board private structure to initialize
10383  **/
10384 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
10385 {
10386 	int vectors = 0;
10387 	ssize_t size;
10388 
10389 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10390 		vectors = i40e_init_msix(pf);
10391 		if (vectors < 0) {
10392 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
10393 				       I40E_FLAG_IWARP_ENABLED	|
10394 				       I40E_FLAG_RSS_ENABLED	|
10395 				       I40E_FLAG_DCB_CAPABLE	|
10396 				       I40E_FLAG_DCB_ENABLED	|
10397 				       I40E_FLAG_SRIOV_ENABLED	|
10398 				       I40E_FLAG_FD_SB_ENABLED	|
10399 				       I40E_FLAG_FD_ATR_ENABLED	|
10400 				       I40E_FLAG_VMDQ_ENABLED);
10401 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10402 
10403 			/* rework the queue expectations without MSIX */
10404 			i40e_determine_queue_usage(pf);
10405 		}
10406 	}
10407 
10408 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10409 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
10410 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
10411 		vectors = pci_enable_msi(pf->pdev);
10412 		if (vectors < 0) {
10413 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
10414 				 vectors);
10415 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
10416 		}
10417 		vectors = 1;  /* one MSI or Legacy vector */
10418 	}
10419 
10420 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
10421 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
10422 
10423 	/* set up vector assignment tracking */
10424 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
10425 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
10426 	if (!pf->irq_pile)
10427 		return -ENOMEM;
10428 
10429 	pf->irq_pile->num_entries = vectors;
10430 	pf->irq_pile->search_hint = 0;
10431 
10432 	/* track first vector for misc interrupts, ignore return */
10433 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
10434 
10435 	return 0;
10436 }
10437 
10438 /**
10439  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
10440  * @pf: private board data structure
10441  *
10442  * Restore the interrupt scheme that was cleared when we suspended the
10443  * device. This should be called during resume to re-allocate the q_vectors
10444  * and reacquire IRQs.
10445  */
10446 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
10447 {
10448 	int err, i;
10449 
10450 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
10451 	 * scheme. We need to re-enabled them here in order to attempt to
10452 	 * re-acquire the MSI or MSI-X vectors
10453 	 */
10454 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
10455 
10456 	err = i40e_init_interrupt_scheme(pf);
10457 	if (err)
10458 		return err;
10459 
10460 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
10461 	 * rings together again.
10462 	 */
10463 	for (i = 0; i < pf->num_alloc_vsi; i++) {
10464 		if (pf->vsi[i]) {
10465 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
10466 			if (err)
10467 				goto err_unwind;
10468 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
10469 		}
10470 	}
10471 
10472 	err = i40e_setup_misc_vector(pf);
10473 	if (err)
10474 		goto err_unwind;
10475 
10476 	return 0;
10477 
10478 err_unwind:
10479 	while (i--) {
10480 		if (pf->vsi[i])
10481 			i40e_vsi_free_q_vectors(pf->vsi[i]);
10482 	}
10483 
10484 	return err;
10485 }
10486 
10487 /**
10488  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
10489  * @pf: board private structure
10490  *
10491  * This sets up the handler for MSIX 0, which is used to manage the
10492  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
10493  * when in MSI or Legacy interrupt mode.
10494  **/
10495 static int i40e_setup_misc_vector(struct i40e_pf *pf)
10496 {
10497 	struct i40e_hw *hw = &pf->hw;
10498 	int err = 0;
10499 
10500 	/* Only request the IRQ once, the first time through. */
10501 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
10502 		err = request_irq(pf->msix_entries[0].vector,
10503 				  i40e_intr, 0, pf->int_name, pf);
10504 		if (err) {
10505 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
10506 			dev_info(&pf->pdev->dev,
10507 				 "request_irq for %s failed: %d\n",
10508 				 pf->int_name, err);
10509 			return -EFAULT;
10510 		}
10511 	}
10512 
10513 	i40e_enable_misc_int_causes(pf);
10514 
10515 	/* associate no queues to the misc vector */
10516 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
10517 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
10518 
10519 	i40e_flush(hw);
10520 
10521 	i40e_irq_dynamic_enable_icr0(pf);
10522 
10523 	return err;
10524 }
10525 
10526 /**
10527  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
10528  * @vsi: Pointer to vsi structure
10529  * @seed: Buffter to store the hash keys
10530  * @lut: Buffer to store the lookup table entries
10531  * @lut_size: Size of buffer to store the lookup table entries
10532  *
10533  * Return 0 on success, negative on failure
10534  */
10535 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
10536 			   u8 *lut, u16 lut_size)
10537 {
10538 	struct i40e_pf *pf = vsi->back;
10539 	struct i40e_hw *hw = &pf->hw;
10540 	int ret = 0;
10541 
10542 	if (seed) {
10543 		ret = i40e_aq_get_rss_key(hw, vsi->id,
10544 			(struct i40e_aqc_get_set_rss_key_data *)seed);
10545 		if (ret) {
10546 			dev_info(&pf->pdev->dev,
10547 				 "Cannot get RSS key, err %s aq_err %s\n",
10548 				 i40e_stat_str(&pf->hw, ret),
10549 				 i40e_aq_str(&pf->hw,
10550 					     pf->hw.aq.asq_last_status));
10551 			return ret;
10552 		}
10553 	}
10554 
10555 	if (lut) {
10556 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
10557 
10558 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
10559 		if (ret) {
10560 			dev_info(&pf->pdev->dev,
10561 				 "Cannot get RSS lut, err %s aq_err %s\n",
10562 				 i40e_stat_str(&pf->hw, ret),
10563 				 i40e_aq_str(&pf->hw,
10564 					     pf->hw.aq.asq_last_status));
10565 			return ret;
10566 		}
10567 	}
10568 
10569 	return ret;
10570 }
10571 
10572 /**
10573  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
10574  * @vsi: Pointer to vsi structure
10575  * @seed: RSS hash seed
10576  * @lut: Lookup table
10577  * @lut_size: Lookup table size
10578  *
10579  * Returns 0 on success, negative on failure
10580  **/
10581 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
10582 			       const u8 *lut, u16 lut_size)
10583 {
10584 	struct i40e_pf *pf = vsi->back;
10585 	struct i40e_hw *hw = &pf->hw;
10586 	u16 vf_id = vsi->vf_id;
10587 	u8 i;
10588 
10589 	/* Fill out hash function seed */
10590 	if (seed) {
10591 		u32 *seed_dw = (u32 *)seed;
10592 
10593 		if (vsi->type == I40E_VSI_MAIN) {
10594 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10595 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
10596 		} else if (vsi->type == I40E_VSI_SRIOV) {
10597 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
10598 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
10599 		} else {
10600 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
10601 		}
10602 	}
10603 
10604 	if (lut) {
10605 		u32 *lut_dw = (u32 *)lut;
10606 
10607 		if (vsi->type == I40E_VSI_MAIN) {
10608 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
10609 				return -EINVAL;
10610 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10611 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
10612 		} else if (vsi->type == I40E_VSI_SRIOV) {
10613 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
10614 				return -EINVAL;
10615 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
10616 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
10617 		} else {
10618 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
10619 		}
10620 	}
10621 	i40e_flush(hw);
10622 
10623 	return 0;
10624 }
10625 
10626 /**
10627  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
10628  * @vsi: Pointer to VSI structure
10629  * @seed: Buffer to store the keys
10630  * @lut: Buffer to store the lookup table entries
10631  * @lut_size: Size of buffer to store the lookup table entries
10632  *
10633  * Returns 0 on success, negative on failure
10634  */
10635 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
10636 			    u8 *lut, u16 lut_size)
10637 {
10638 	struct i40e_pf *pf = vsi->back;
10639 	struct i40e_hw *hw = &pf->hw;
10640 	u16 i;
10641 
10642 	if (seed) {
10643 		u32 *seed_dw = (u32 *)seed;
10644 
10645 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10646 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
10647 	}
10648 	if (lut) {
10649 		u32 *lut_dw = (u32 *)lut;
10650 
10651 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
10652 			return -EINVAL;
10653 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10654 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
10655 	}
10656 
10657 	return 0;
10658 }
10659 
10660 /**
10661  * i40e_config_rss - Configure RSS keys and lut
10662  * @vsi: Pointer to VSI structure
10663  * @seed: RSS hash seed
10664  * @lut: Lookup table
10665  * @lut_size: Lookup table size
10666  *
10667  * Returns 0 on success, negative on failure
10668  */
10669 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10670 {
10671 	struct i40e_pf *pf = vsi->back;
10672 
10673 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10674 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
10675 	else
10676 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
10677 }
10678 
10679 /**
10680  * i40e_get_rss - Get RSS keys and lut
10681  * @vsi: Pointer to VSI structure
10682  * @seed: Buffer to store the keys
10683  * @lut: Buffer to store the lookup table entries
10684  * lut_size: Size of buffer to store the lookup table entries
10685  *
10686  * Returns 0 on success, negative on failure
10687  */
10688 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10689 {
10690 	struct i40e_pf *pf = vsi->back;
10691 
10692 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10693 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
10694 	else
10695 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
10696 }
10697 
10698 /**
10699  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
10700  * @pf: Pointer to board private structure
10701  * @lut: Lookup table
10702  * @rss_table_size: Lookup table size
10703  * @rss_size: Range of queue number for hashing
10704  */
10705 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
10706 		       u16 rss_table_size, u16 rss_size)
10707 {
10708 	u16 i;
10709 
10710 	for (i = 0; i < rss_table_size; i++)
10711 		lut[i] = i % rss_size;
10712 }
10713 
10714 /**
10715  * i40e_pf_config_rss - Prepare for RSS if used
10716  * @pf: board private structure
10717  **/
10718 static int i40e_pf_config_rss(struct i40e_pf *pf)
10719 {
10720 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10721 	u8 seed[I40E_HKEY_ARRAY_SIZE];
10722 	u8 *lut;
10723 	struct i40e_hw *hw = &pf->hw;
10724 	u32 reg_val;
10725 	u64 hena;
10726 	int ret;
10727 
10728 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
10729 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
10730 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
10731 	hena |= i40e_pf_get_default_rss_hena(pf);
10732 
10733 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
10734 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
10735 
10736 	/* Determine the RSS table size based on the hardware capabilities */
10737 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
10738 	reg_val = (pf->rss_table_size == 512) ?
10739 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
10740 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
10741 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
10742 
10743 	/* Determine the RSS size of the VSI */
10744 	if (!vsi->rss_size) {
10745 		u16 qcount;
10746 		/* If the firmware does something weird during VSI init, we
10747 		 * could end up with zero TCs. Check for that to avoid
10748 		 * divide-by-zero. It probably won't pass traffic, but it also
10749 		 * won't panic.
10750 		 */
10751 		qcount = vsi->num_queue_pairs /
10752 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
10753 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
10754 	}
10755 	if (!vsi->rss_size)
10756 		return -EINVAL;
10757 
10758 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
10759 	if (!lut)
10760 		return -ENOMEM;
10761 
10762 	/* Use user configured lut if there is one, otherwise use default */
10763 	if (vsi->rss_lut_user)
10764 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
10765 	else
10766 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
10767 
10768 	/* Use user configured hash key if there is one, otherwise
10769 	 * use default.
10770 	 */
10771 	if (vsi->rss_hkey_user)
10772 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
10773 	else
10774 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
10775 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
10776 	kfree(lut);
10777 
10778 	return ret;
10779 }
10780 
10781 /**
10782  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
10783  * @pf: board private structure
10784  * @queue_count: the requested queue count for rss.
10785  *
10786  * returns 0 if rss is not enabled, if enabled returns the final rss queue
10787  * count which may be different from the requested queue count.
10788  * Note: expects to be called while under rtnl_lock()
10789  **/
10790 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
10791 {
10792 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10793 	int new_rss_size;
10794 
10795 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
10796 		return 0;
10797 
10798 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
10799 
10800 	if (queue_count != vsi->num_queue_pairs) {
10801 		u16 qcount;
10802 
10803 		vsi->req_queue_pairs = queue_count;
10804 		i40e_prep_for_reset(pf, true);
10805 
10806 		pf->alloc_rss_size = new_rss_size;
10807 
10808 		i40e_reset_and_rebuild(pf, true, true);
10809 
10810 		/* Discard the user configured hash keys and lut, if less
10811 		 * queues are enabled.
10812 		 */
10813 		if (queue_count < vsi->rss_size) {
10814 			i40e_clear_rss_config_user(vsi);
10815 			dev_dbg(&pf->pdev->dev,
10816 				"discard user configured hash keys and lut\n");
10817 		}
10818 
10819 		/* Reset vsi->rss_size, as number of enabled queues changed */
10820 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
10821 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
10822 
10823 		i40e_pf_config_rss(pf);
10824 	}
10825 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
10826 		 vsi->req_queue_pairs, pf->rss_size_max);
10827 	return pf->alloc_rss_size;
10828 }
10829 
10830 /**
10831  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
10832  * @pf: board private structure
10833  **/
10834 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
10835 {
10836 	i40e_status status;
10837 	bool min_valid, max_valid;
10838 	u32 max_bw, min_bw;
10839 
10840 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
10841 					   &min_valid, &max_valid);
10842 
10843 	if (!status) {
10844 		if (min_valid)
10845 			pf->min_bw = min_bw;
10846 		if (max_valid)
10847 			pf->max_bw = max_bw;
10848 	}
10849 
10850 	return status;
10851 }
10852 
10853 /**
10854  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
10855  * @pf: board private structure
10856  **/
10857 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
10858 {
10859 	struct i40e_aqc_configure_partition_bw_data bw_data;
10860 	i40e_status status;
10861 
10862 	/* Set the valid bit for this PF */
10863 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
10864 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
10865 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
10866 
10867 	/* Set the new bandwidths */
10868 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
10869 
10870 	return status;
10871 }
10872 
10873 /**
10874  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
10875  * @pf: board private structure
10876  **/
10877 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
10878 {
10879 	/* Commit temporary BW setting to permanent NVM image */
10880 	enum i40e_admin_queue_err last_aq_status;
10881 	i40e_status ret;
10882 	u16 nvm_word;
10883 
10884 	if (pf->hw.partition_id != 1) {
10885 		dev_info(&pf->pdev->dev,
10886 			 "Commit BW only works on partition 1! This is partition %d",
10887 			 pf->hw.partition_id);
10888 		ret = I40E_NOT_SUPPORTED;
10889 		goto bw_commit_out;
10890 	}
10891 
10892 	/* Acquire NVM for read access */
10893 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
10894 	last_aq_status = pf->hw.aq.asq_last_status;
10895 	if (ret) {
10896 		dev_info(&pf->pdev->dev,
10897 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
10898 			 i40e_stat_str(&pf->hw, ret),
10899 			 i40e_aq_str(&pf->hw, last_aq_status));
10900 		goto bw_commit_out;
10901 	}
10902 
10903 	/* Read word 0x10 of NVM - SW compatibility word 1 */
10904 	ret = i40e_aq_read_nvm(&pf->hw,
10905 			       I40E_SR_NVM_CONTROL_WORD,
10906 			       0x10, sizeof(nvm_word), &nvm_word,
10907 			       false, NULL);
10908 	/* Save off last admin queue command status before releasing
10909 	 * the NVM
10910 	 */
10911 	last_aq_status = pf->hw.aq.asq_last_status;
10912 	i40e_release_nvm(&pf->hw);
10913 	if (ret) {
10914 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
10915 			 i40e_stat_str(&pf->hw, ret),
10916 			 i40e_aq_str(&pf->hw, last_aq_status));
10917 		goto bw_commit_out;
10918 	}
10919 
10920 	/* Wait a bit for NVM release to complete */
10921 	msleep(50);
10922 
10923 	/* Acquire NVM for write access */
10924 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
10925 	last_aq_status = pf->hw.aq.asq_last_status;
10926 	if (ret) {
10927 		dev_info(&pf->pdev->dev,
10928 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
10929 			 i40e_stat_str(&pf->hw, ret),
10930 			 i40e_aq_str(&pf->hw, last_aq_status));
10931 		goto bw_commit_out;
10932 	}
10933 	/* Write it back out unchanged to initiate update NVM,
10934 	 * which will force a write of the shadow (alt) RAM to
10935 	 * the NVM - thus storing the bandwidth values permanently.
10936 	 */
10937 	ret = i40e_aq_update_nvm(&pf->hw,
10938 				 I40E_SR_NVM_CONTROL_WORD,
10939 				 0x10, sizeof(nvm_word),
10940 				 &nvm_word, true, 0, NULL);
10941 	/* Save off last admin queue command status before releasing
10942 	 * the NVM
10943 	 */
10944 	last_aq_status = pf->hw.aq.asq_last_status;
10945 	i40e_release_nvm(&pf->hw);
10946 	if (ret)
10947 		dev_info(&pf->pdev->dev,
10948 			 "BW settings NOT SAVED, err %s aq_err %s\n",
10949 			 i40e_stat_str(&pf->hw, ret),
10950 			 i40e_aq_str(&pf->hw, last_aq_status));
10951 bw_commit_out:
10952 
10953 	return ret;
10954 }
10955 
10956 /**
10957  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
10958  * @pf: board private structure to initialize
10959  *
10960  * i40e_sw_init initializes the Adapter private data structure.
10961  * Fields are initialized based on PCI device information and
10962  * OS network device settings (MTU size).
10963  **/
10964 static int i40e_sw_init(struct i40e_pf *pf)
10965 {
10966 	int err = 0;
10967 	int size;
10968 
10969 	/* Set default capability flags */
10970 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
10971 		    I40E_FLAG_MSI_ENABLED     |
10972 		    I40E_FLAG_MSIX_ENABLED;
10973 
10974 	/* Set default ITR */
10975 	pf->rx_itr_default = I40E_ITR_RX_DEF;
10976 	pf->tx_itr_default = I40E_ITR_TX_DEF;
10977 
10978 	/* Depending on PF configurations, it is possible that the RSS
10979 	 * maximum might end up larger than the available queues
10980 	 */
10981 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
10982 	pf->alloc_rss_size = 1;
10983 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
10984 	pf->rss_size_max = min_t(int, pf->rss_size_max,
10985 				 pf->hw.func_caps.num_tx_qp);
10986 	if (pf->hw.func_caps.rss) {
10987 		pf->flags |= I40E_FLAG_RSS_ENABLED;
10988 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
10989 					   num_online_cpus());
10990 	}
10991 
10992 	/* MFP mode enabled */
10993 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
10994 		pf->flags |= I40E_FLAG_MFP_ENABLED;
10995 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
10996 		if (i40e_get_partition_bw_setting(pf)) {
10997 			dev_warn(&pf->pdev->dev,
10998 				 "Could not get partition bw settings\n");
10999 		} else {
11000 			dev_info(&pf->pdev->dev,
11001 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
11002 				 pf->min_bw, pf->max_bw);
11003 
11004 			/* nudge the Tx scheduler */
11005 			i40e_set_partition_bw_setting(pf);
11006 		}
11007 	}
11008 
11009 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11010 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11011 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11012 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11013 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11014 		    pf->hw.num_partitions > 1)
11015 			dev_info(&pf->pdev->dev,
11016 				 "Flow Director Sideband mode Disabled in MFP mode\n");
11017 		else
11018 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11019 		pf->fdir_pf_filter_count =
11020 				 pf->hw.func_caps.fd_filters_guaranteed;
11021 		pf->hw.fdir_shared_filter_count =
11022 				 pf->hw.func_caps.fd_filters_best_effort;
11023 	}
11024 
11025 	if (pf->hw.mac.type == I40E_MAC_X722) {
11026 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11027 				    I40E_HW_128_QP_RSS_CAPABLE |
11028 				    I40E_HW_ATR_EVICT_CAPABLE |
11029 				    I40E_HW_WB_ON_ITR_CAPABLE |
11030 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11031 				    I40E_HW_NO_PCI_LINK_CHECK |
11032 				    I40E_HW_USE_SET_LLDP_MIB |
11033 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11034 				    I40E_HW_PTP_L4_CAPABLE |
11035 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11036 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11037 
11038 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11039 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11040 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
11041 			dev_warn(&pf->pdev->dev,
11042 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11043 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11044 		}
11045 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
11046 		   ((pf->hw.aq.api_maj_ver == 1) &&
11047 		    (pf->hw.aq.api_min_ver > 4))) {
11048 		/* Supported in FW API version higher than 1.4 */
11049 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11050 	}
11051 
11052 	/* Enable HW ATR eviction if possible */
11053 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11054 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11055 
11056 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11057 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11058 	    (pf->hw.aq.fw_maj_ver < 4))) {
11059 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11060 		/* No DCB support  for FW < v4.33 */
11061 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11062 	}
11063 
11064 	/* Disable FW LLDP if FW < v4.3 */
11065 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11066 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11067 	    (pf->hw.aq.fw_maj_ver < 4)))
11068 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11069 
11070 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
11071 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11072 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11073 	    (pf->hw.aq.fw_maj_ver >= 5)))
11074 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11075 
11076 	/* Enable PTP L4 if FW > v6.0 */
11077 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11078 	    pf->hw.aq.fw_maj_ver >= 6)
11079 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11080 
11081 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
11082 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11083 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
11084 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
11085 	}
11086 
11087 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
11088 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
11089 		/* IWARP needs one extra vector for CQP just like MISC.*/
11090 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11091 	}
11092 
11093 #ifdef CONFIG_PCI_IOV
11094 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
11095 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
11096 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
11097 		pf->num_req_vfs = min_t(int,
11098 					pf->hw.func_caps.num_vfs,
11099 					I40E_MAX_VF_COUNT);
11100 	}
11101 #endif /* CONFIG_PCI_IOV */
11102 	pf->eeprom_version = 0xDEAD;
11103 	pf->lan_veb = I40E_NO_VEB;
11104 	pf->lan_vsi = I40E_NO_VSI;
11105 
11106 	/* By default FW has this off for performance reasons */
11107 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
11108 
11109 	/* set up queue assignment tracking */
11110 	size = sizeof(struct i40e_lump_tracking)
11111 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
11112 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
11113 	if (!pf->qp_pile) {
11114 		err = -ENOMEM;
11115 		goto sw_init_done;
11116 	}
11117 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
11118 	pf->qp_pile->search_hint = 0;
11119 
11120 	pf->tx_timeout_recovery_level = 1;
11121 
11122 	mutex_init(&pf->switch_mutex);
11123 
11124 sw_init_done:
11125 	return err;
11126 }
11127 
11128 /**
11129  * i40e_set_ntuple - set the ntuple feature flag and take action
11130  * @pf: board private structure to initialize
11131  * @features: the feature set that the stack is suggesting
11132  *
11133  * returns a bool to indicate if reset needs to happen
11134  **/
11135 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
11136 {
11137 	bool need_reset = false;
11138 
11139 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
11140 	 * the state changed, we need to reset.
11141 	 */
11142 	if (features & NETIF_F_NTUPLE) {
11143 		/* Enable filters and mark for reset */
11144 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
11145 			need_reset = true;
11146 		/* enable FD_SB only if there is MSI-X vector and no cloud
11147 		 * filters exist
11148 		 */
11149 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
11150 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11151 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
11152 		}
11153 	} else {
11154 		/* turn off filters, mark for reset and clear SW filter list */
11155 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11156 			need_reset = true;
11157 			i40e_fdir_filter_exit(pf);
11158 		}
11159 		pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
11160 			       I40E_FLAG_FD_SB_AUTO_DISABLED);
11161 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11162 
11163 		/* reset fd counters */
11164 		pf->fd_add_err = 0;
11165 		pf->fd_atr_cnt = 0;
11166 		/* if ATR was auto disabled it can be re-enabled. */
11167 		if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
11168 			pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
11169 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
11170 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
11171 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
11172 		}
11173 	}
11174 	return need_reset;
11175 }
11176 
11177 /**
11178  * i40e_clear_rss_lut - clear the rx hash lookup table
11179  * @vsi: the VSI being configured
11180  **/
11181 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
11182 {
11183 	struct i40e_pf *pf = vsi->back;
11184 	struct i40e_hw *hw = &pf->hw;
11185 	u16 vf_id = vsi->vf_id;
11186 	u8 i;
11187 
11188 	if (vsi->type == I40E_VSI_MAIN) {
11189 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11190 			wr32(hw, I40E_PFQF_HLUT(i), 0);
11191 	} else if (vsi->type == I40E_VSI_SRIOV) {
11192 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11193 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
11194 	} else {
11195 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11196 	}
11197 }
11198 
11199 /**
11200  * i40e_set_features - set the netdev feature flags
11201  * @netdev: ptr to the netdev being adjusted
11202  * @features: the feature set that the stack is suggesting
11203  * Note: expects to be called while under rtnl_lock()
11204  **/
11205 static int i40e_set_features(struct net_device *netdev,
11206 			     netdev_features_t features)
11207 {
11208 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11209 	struct i40e_vsi *vsi = np->vsi;
11210 	struct i40e_pf *pf = vsi->back;
11211 	bool need_reset;
11212 
11213 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
11214 		i40e_pf_config_rss(pf);
11215 	else if (!(features & NETIF_F_RXHASH) &&
11216 		 netdev->features & NETIF_F_RXHASH)
11217 		i40e_clear_rss_lut(vsi);
11218 
11219 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
11220 		i40e_vlan_stripping_enable(vsi);
11221 	else
11222 		i40e_vlan_stripping_disable(vsi);
11223 
11224 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
11225 		dev_err(&pf->pdev->dev,
11226 			"Offloaded tc filters active, can't turn hw_tc_offload off");
11227 		return -EINVAL;
11228 	}
11229 
11230 	need_reset = i40e_set_ntuple(pf, features);
11231 
11232 	if (need_reset)
11233 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11234 
11235 	return 0;
11236 }
11237 
11238 /**
11239  * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
11240  * @pf: board private structure
11241  * @port: The UDP port to look up
11242  *
11243  * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
11244  **/
11245 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
11246 {
11247 	u8 i;
11248 
11249 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
11250 		if (pf->udp_ports[i].port == port)
11251 			return i;
11252 	}
11253 
11254 	return i;
11255 }
11256 
11257 /**
11258  * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
11259  * @netdev: This physical port's netdev
11260  * @ti: Tunnel endpoint information
11261  **/
11262 static void i40e_udp_tunnel_add(struct net_device *netdev,
11263 				struct udp_tunnel_info *ti)
11264 {
11265 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11266 	struct i40e_vsi *vsi = np->vsi;
11267 	struct i40e_pf *pf = vsi->back;
11268 	u16 port = ntohs(ti->port);
11269 	u8 next_idx;
11270 	u8 idx;
11271 
11272 	idx = i40e_get_udp_port_idx(pf, port);
11273 
11274 	/* Check if port already exists */
11275 	if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11276 		netdev_info(netdev, "port %d already offloaded\n", port);
11277 		return;
11278 	}
11279 
11280 	/* Now check if there is space to add the new port */
11281 	next_idx = i40e_get_udp_port_idx(pf, 0);
11282 
11283 	if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11284 		netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
11285 			    port);
11286 		return;
11287 	}
11288 
11289 	switch (ti->type) {
11290 	case UDP_TUNNEL_TYPE_VXLAN:
11291 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
11292 		break;
11293 	case UDP_TUNNEL_TYPE_GENEVE:
11294 		if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
11295 			return;
11296 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
11297 		break;
11298 	default:
11299 		return;
11300 	}
11301 
11302 	/* New port: add it and mark its index in the bitmap */
11303 	pf->udp_ports[next_idx].port = port;
11304 	pf->pending_udp_bitmap |= BIT_ULL(next_idx);
11305 	pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
11306 }
11307 
11308 /**
11309  * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
11310  * @netdev: This physical port's netdev
11311  * @ti: Tunnel endpoint information
11312  **/
11313 static void i40e_udp_tunnel_del(struct net_device *netdev,
11314 				struct udp_tunnel_info *ti)
11315 {
11316 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11317 	struct i40e_vsi *vsi = np->vsi;
11318 	struct i40e_pf *pf = vsi->back;
11319 	u16 port = ntohs(ti->port);
11320 	u8 idx;
11321 
11322 	idx = i40e_get_udp_port_idx(pf, port);
11323 
11324 	/* Check if port already exists */
11325 	if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
11326 		goto not_found;
11327 
11328 	switch (ti->type) {
11329 	case UDP_TUNNEL_TYPE_VXLAN:
11330 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
11331 			goto not_found;
11332 		break;
11333 	case UDP_TUNNEL_TYPE_GENEVE:
11334 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
11335 			goto not_found;
11336 		break;
11337 	default:
11338 		goto not_found;
11339 	}
11340 
11341 	/* if port exists, set it to 0 (mark for deletion)
11342 	 * and make it pending
11343 	 */
11344 	pf->udp_ports[idx].port = 0;
11345 	pf->pending_udp_bitmap |= BIT_ULL(idx);
11346 	pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
11347 
11348 	return;
11349 not_found:
11350 	netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
11351 		    port);
11352 }
11353 
11354 static int i40e_get_phys_port_id(struct net_device *netdev,
11355 				 struct netdev_phys_item_id *ppid)
11356 {
11357 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11358 	struct i40e_pf *pf = np->vsi->back;
11359 	struct i40e_hw *hw = &pf->hw;
11360 
11361 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
11362 		return -EOPNOTSUPP;
11363 
11364 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
11365 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
11366 
11367 	return 0;
11368 }
11369 
11370 /**
11371  * i40e_ndo_fdb_add - add an entry to the hardware database
11372  * @ndm: the input from the stack
11373  * @tb: pointer to array of nladdr (unused)
11374  * @dev: the net device pointer
11375  * @addr: the MAC address entry being added
11376  * @flags: instructions from stack about fdb operation
11377  */
11378 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
11379 			    struct net_device *dev,
11380 			    const unsigned char *addr, u16 vid,
11381 			    u16 flags)
11382 {
11383 	struct i40e_netdev_priv *np = netdev_priv(dev);
11384 	struct i40e_pf *pf = np->vsi->back;
11385 	int err = 0;
11386 
11387 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
11388 		return -EOPNOTSUPP;
11389 
11390 	if (vid) {
11391 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
11392 		return -EINVAL;
11393 	}
11394 
11395 	/* Hardware does not support aging addresses so if a
11396 	 * ndm_state is given only allow permanent addresses
11397 	 */
11398 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
11399 		netdev_info(dev, "FDB only supports static addresses\n");
11400 		return -EINVAL;
11401 	}
11402 
11403 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
11404 		err = dev_uc_add_excl(dev, addr);
11405 	else if (is_multicast_ether_addr(addr))
11406 		err = dev_mc_add_excl(dev, addr);
11407 	else
11408 		err = -EINVAL;
11409 
11410 	/* Only return duplicate errors if NLM_F_EXCL is set */
11411 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
11412 		err = 0;
11413 
11414 	return err;
11415 }
11416 
11417 /**
11418  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
11419  * @dev: the netdev being configured
11420  * @nlh: RTNL message
11421  *
11422  * Inserts a new hardware bridge if not already created and
11423  * enables the bridging mode requested (VEB or VEPA). If the
11424  * hardware bridge has already been inserted and the request
11425  * is to change the mode then that requires a PF reset to
11426  * allow rebuild of the components with required hardware
11427  * bridge mode enabled.
11428  *
11429  * Note: expects to be called while under rtnl_lock()
11430  **/
11431 static int i40e_ndo_bridge_setlink(struct net_device *dev,
11432 				   struct nlmsghdr *nlh,
11433 				   u16 flags)
11434 {
11435 	struct i40e_netdev_priv *np = netdev_priv(dev);
11436 	struct i40e_vsi *vsi = np->vsi;
11437 	struct i40e_pf *pf = vsi->back;
11438 	struct i40e_veb *veb = NULL;
11439 	struct nlattr *attr, *br_spec;
11440 	int i, rem;
11441 
11442 	/* Only for PF VSI for now */
11443 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11444 		return -EOPNOTSUPP;
11445 
11446 	/* Find the HW bridge for PF VSI */
11447 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11448 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11449 			veb = pf->veb[i];
11450 	}
11451 
11452 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11453 
11454 	nla_for_each_nested(attr, br_spec, rem) {
11455 		__u16 mode;
11456 
11457 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
11458 			continue;
11459 
11460 		mode = nla_get_u16(attr);
11461 		if ((mode != BRIDGE_MODE_VEPA) &&
11462 		    (mode != BRIDGE_MODE_VEB))
11463 			return -EINVAL;
11464 
11465 		/* Insert a new HW bridge */
11466 		if (!veb) {
11467 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11468 					     vsi->tc_config.enabled_tc);
11469 			if (veb) {
11470 				veb->bridge_mode = mode;
11471 				i40e_config_bridge_mode(veb);
11472 			} else {
11473 				/* No Bridge HW offload available */
11474 				return -ENOENT;
11475 			}
11476 			break;
11477 		} else if (mode != veb->bridge_mode) {
11478 			/* Existing HW bridge but different mode needs reset */
11479 			veb->bridge_mode = mode;
11480 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
11481 			if (mode == BRIDGE_MODE_VEB)
11482 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11483 			else
11484 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
11485 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11486 			break;
11487 		}
11488 	}
11489 
11490 	return 0;
11491 }
11492 
11493 /**
11494  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
11495  * @skb: skb buff
11496  * @pid: process id
11497  * @seq: RTNL message seq #
11498  * @dev: the netdev being configured
11499  * @filter_mask: unused
11500  * @nlflags: netlink flags passed in
11501  *
11502  * Return the mode in which the hardware bridge is operating in
11503  * i.e VEB or VEPA.
11504  **/
11505 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11506 				   struct net_device *dev,
11507 				   u32 __always_unused filter_mask,
11508 				   int nlflags)
11509 {
11510 	struct i40e_netdev_priv *np = netdev_priv(dev);
11511 	struct i40e_vsi *vsi = np->vsi;
11512 	struct i40e_pf *pf = vsi->back;
11513 	struct i40e_veb *veb = NULL;
11514 	int i;
11515 
11516 	/* Only for PF VSI for now */
11517 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11518 		return -EOPNOTSUPP;
11519 
11520 	/* Find the HW bridge for the PF VSI */
11521 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11522 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11523 			veb = pf->veb[i];
11524 	}
11525 
11526 	if (!veb)
11527 		return 0;
11528 
11529 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
11530 				       0, 0, nlflags, filter_mask, NULL);
11531 }
11532 
11533 /**
11534  * i40e_features_check - Validate encapsulated packet conforms to limits
11535  * @skb: skb buff
11536  * @dev: This physical port's netdev
11537  * @features: Offload features that the stack believes apply
11538  **/
11539 static netdev_features_t i40e_features_check(struct sk_buff *skb,
11540 					     struct net_device *dev,
11541 					     netdev_features_t features)
11542 {
11543 	size_t len;
11544 
11545 	/* No point in doing any of this if neither checksum nor GSO are
11546 	 * being requested for this frame.  We can rule out both by just
11547 	 * checking for CHECKSUM_PARTIAL
11548 	 */
11549 	if (skb->ip_summed != CHECKSUM_PARTIAL)
11550 		return features;
11551 
11552 	/* We cannot support GSO if the MSS is going to be less than
11553 	 * 64 bytes.  If it is then we need to drop support for GSO.
11554 	 */
11555 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
11556 		features &= ~NETIF_F_GSO_MASK;
11557 
11558 	/* MACLEN can support at most 63 words */
11559 	len = skb_network_header(skb) - skb->data;
11560 	if (len & ~(63 * 2))
11561 		goto out_err;
11562 
11563 	/* IPLEN and EIPLEN can support at most 127 dwords */
11564 	len = skb_transport_header(skb) - skb_network_header(skb);
11565 	if (len & ~(127 * 4))
11566 		goto out_err;
11567 
11568 	if (skb->encapsulation) {
11569 		/* L4TUNLEN can support 127 words */
11570 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
11571 		if (len & ~(127 * 2))
11572 			goto out_err;
11573 
11574 		/* IPLEN can support at most 127 dwords */
11575 		len = skb_inner_transport_header(skb) -
11576 		      skb_inner_network_header(skb);
11577 		if (len & ~(127 * 4))
11578 			goto out_err;
11579 	}
11580 
11581 	/* No need to validate L4LEN as TCP is the only protocol with a
11582 	 * a flexible value and we support all possible values supported
11583 	 * by TCP, which is at most 15 dwords
11584 	 */
11585 
11586 	return features;
11587 out_err:
11588 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11589 }
11590 
11591 /**
11592  * i40e_xdp_setup - add/remove an XDP program
11593  * @vsi: VSI to changed
11594  * @prog: XDP program
11595  **/
11596 static int i40e_xdp_setup(struct i40e_vsi *vsi,
11597 			  struct bpf_prog *prog)
11598 {
11599 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
11600 	struct i40e_pf *pf = vsi->back;
11601 	struct bpf_prog *old_prog;
11602 	bool need_reset;
11603 	int i;
11604 
11605 	/* Don't allow frames that span over multiple buffers */
11606 	if (frame_size > vsi->rx_buf_len)
11607 		return -EINVAL;
11608 
11609 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
11610 		return 0;
11611 
11612 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
11613 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
11614 
11615 	if (need_reset)
11616 		i40e_prep_for_reset(pf, true);
11617 
11618 	old_prog = xchg(&vsi->xdp_prog, prog);
11619 
11620 	if (need_reset)
11621 		i40e_reset_and_rebuild(pf, true, true);
11622 
11623 	for (i = 0; i < vsi->num_queue_pairs; i++)
11624 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
11625 
11626 	if (old_prog)
11627 		bpf_prog_put(old_prog);
11628 
11629 	return 0;
11630 }
11631 
11632 /**
11633  * i40e_xdp - implements ndo_bpf for i40e
11634  * @dev: netdevice
11635  * @xdp: XDP command
11636  **/
11637 static int i40e_xdp(struct net_device *dev,
11638 		    struct netdev_bpf *xdp)
11639 {
11640 	struct i40e_netdev_priv *np = netdev_priv(dev);
11641 	struct i40e_vsi *vsi = np->vsi;
11642 
11643 	if (vsi->type != I40E_VSI_MAIN)
11644 		return -EINVAL;
11645 
11646 	switch (xdp->command) {
11647 	case XDP_SETUP_PROG:
11648 		return i40e_xdp_setup(vsi, xdp->prog);
11649 	case XDP_QUERY_PROG:
11650 		xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
11651 		xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
11652 		return 0;
11653 	default:
11654 		return -EINVAL;
11655 	}
11656 }
11657 
11658 static const struct net_device_ops i40e_netdev_ops = {
11659 	.ndo_open		= i40e_open,
11660 	.ndo_stop		= i40e_close,
11661 	.ndo_start_xmit		= i40e_lan_xmit_frame,
11662 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
11663 	.ndo_set_rx_mode	= i40e_set_rx_mode,
11664 	.ndo_validate_addr	= eth_validate_addr,
11665 	.ndo_set_mac_address	= i40e_set_mac,
11666 	.ndo_change_mtu		= i40e_change_mtu,
11667 	.ndo_do_ioctl		= i40e_ioctl,
11668 	.ndo_tx_timeout		= i40e_tx_timeout,
11669 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
11670 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
11671 #ifdef CONFIG_NET_POLL_CONTROLLER
11672 	.ndo_poll_controller	= i40e_netpoll,
11673 #endif
11674 	.ndo_setup_tc		= __i40e_setup_tc,
11675 	.ndo_set_features	= i40e_set_features,
11676 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
11677 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
11678 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
11679 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
11680 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
11681 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
11682 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
11683 	.ndo_udp_tunnel_add	= i40e_udp_tunnel_add,
11684 	.ndo_udp_tunnel_del	= i40e_udp_tunnel_del,
11685 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
11686 	.ndo_fdb_add		= i40e_ndo_fdb_add,
11687 	.ndo_features_check	= i40e_features_check,
11688 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
11689 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
11690 	.ndo_bpf		= i40e_xdp,
11691 };
11692 
11693 /**
11694  * i40e_config_netdev - Setup the netdev flags
11695  * @vsi: the VSI being configured
11696  *
11697  * Returns 0 on success, negative value on failure
11698  **/
11699 static int i40e_config_netdev(struct i40e_vsi *vsi)
11700 {
11701 	struct i40e_pf *pf = vsi->back;
11702 	struct i40e_hw *hw = &pf->hw;
11703 	struct i40e_netdev_priv *np;
11704 	struct net_device *netdev;
11705 	u8 broadcast[ETH_ALEN];
11706 	u8 mac_addr[ETH_ALEN];
11707 	int etherdev_size;
11708 	netdev_features_t hw_enc_features;
11709 	netdev_features_t hw_features;
11710 
11711 	etherdev_size = sizeof(struct i40e_netdev_priv);
11712 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
11713 	if (!netdev)
11714 		return -ENOMEM;
11715 
11716 	vsi->netdev = netdev;
11717 	np = netdev_priv(netdev);
11718 	np->vsi = vsi;
11719 
11720 	hw_enc_features = NETIF_F_SG			|
11721 			  NETIF_F_IP_CSUM		|
11722 			  NETIF_F_IPV6_CSUM		|
11723 			  NETIF_F_HIGHDMA		|
11724 			  NETIF_F_SOFT_FEATURES		|
11725 			  NETIF_F_TSO			|
11726 			  NETIF_F_TSO_ECN		|
11727 			  NETIF_F_TSO6			|
11728 			  NETIF_F_GSO_GRE		|
11729 			  NETIF_F_GSO_GRE_CSUM		|
11730 			  NETIF_F_GSO_PARTIAL		|
11731 			  NETIF_F_GSO_UDP_TUNNEL	|
11732 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
11733 			  NETIF_F_SCTP_CRC		|
11734 			  NETIF_F_RXHASH		|
11735 			  NETIF_F_RXCSUM		|
11736 			  0;
11737 
11738 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
11739 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
11740 
11741 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
11742 
11743 	netdev->hw_enc_features |= hw_enc_features;
11744 
11745 	/* record features VLANs can make use of */
11746 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
11747 
11748 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
11749 		netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
11750 
11751 	hw_features = hw_enc_features		|
11752 		      NETIF_F_HW_VLAN_CTAG_TX	|
11753 		      NETIF_F_HW_VLAN_CTAG_RX;
11754 
11755 	netdev->hw_features |= hw_features;
11756 
11757 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
11758 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
11759 
11760 	if (vsi->type == I40E_VSI_MAIN) {
11761 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
11762 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
11763 		/* The following steps are necessary for two reasons. First,
11764 		 * some older NVM configurations load a default MAC-VLAN
11765 		 * filter that will accept any tagged packet, and we want to
11766 		 * replace this with a normal filter. Additionally, it is
11767 		 * possible our MAC address was provided by the platform using
11768 		 * Open Firmware or similar.
11769 		 *
11770 		 * Thus, we need to remove the default filter and install one
11771 		 * specific to the MAC address.
11772 		 */
11773 		i40e_rm_default_mac_filter(vsi, mac_addr);
11774 		spin_lock_bh(&vsi->mac_filter_hash_lock);
11775 		i40e_add_mac_filter(vsi, mac_addr);
11776 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
11777 	} else {
11778 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
11779 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
11780 		 * the end, which is 4 bytes long, so force truncation of the
11781 		 * original name by IFNAMSIZ - 4
11782 		 */
11783 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
11784 			 IFNAMSIZ - 4,
11785 			 pf->vsi[pf->lan_vsi]->netdev->name);
11786 		random_ether_addr(mac_addr);
11787 
11788 		spin_lock_bh(&vsi->mac_filter_hash_lock);
11789 		i40e_add_mac_filter(vsi, mac_addr);
11790 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
11791 	}
11792 
11793 	/* Add the broadcast filter so that we initially will receive
11794 	 * broadcast packets. Note that when a new VLAN is first added the
11795 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
11796 	 * specific filters as part of transitioning into "vlan" operation.
11797 	 * When more VLANs are added, the driver will copy each existing MAC
11798 	 * filter and add it for the new VLAN.
11799 	 *
11800 	 * Broadcast filters are handled specially by
11801 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
11802 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
11803 	 * filter. The subtask will update the correct broadcast promiscuous
11804 	 * bits as VLANs become active or inactive.
11805 	 */
11806 	eth_broadcast_addr(broadcast);
11807 	spin_lock_bh(&vsi->mac_filter_hash_lock);
11808 	i40e_add_mac_filter(vsi, broadcast);
11809 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
11810 
11811 	ether_addr_copy(netdev->dev_addr, mac_addr);
11812 	ether_addr_copy(netdev->perm_addr, mac_addr);
11813 
11814 	netdev->priv_flags |= IFF_UNICAST_FLT;
11815 	netdev->priv_flags |= IFF_SUPP_NOFCS;
11816 	/* Setup netdev TC information */
11817 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
11818 
11819 	netdev->netdev_ops = &i40e_netdev_ops;
11820 	netdev->watchdog_timeo = 5 * HZ;
11821 	i40e_set_ethtool_ops(netdev);
11822 
11823 	/* MTU range: 68 - 9706 */
11824 	netdev->min_mtu = ETH_MIN_MTU;
11825 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
11826 
11827 	return 0;
11828 }
11829 
11830 /**
11831  * i40e_vsi_delete - Delete a VSI from the switch
11832  * @vsi: the VSI being removed
11833  *
11834  * Returns 0 on success, negative value on failure
11835  **/
11836 static void i40e_vsi_delete(struct i40e_vsi *vsi)
11837 {
11838 	/* remove default VSI is not allowed */
11839 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
11840 		return;
11841 
11842 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
11843 }
11844 
11845 /**
11846  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
11847  * @vsi: the VSI being queried
11848  *
11849  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
11850  **/
11851 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
11852 {
11853 	struct i40e_veb *veb;
11854 	struct i40e_pf *pf = vsi->back;
11855 
11856 	/* Uplink is not a bridge so default to VEB */
11857 	if (vsi->veb_idx == I40E_NO_VEB)
11858 		return 1;
11859 
11860 	veb = pf->veb[vsi->veb_idx];
11861 	if (!veb) {
11862 		dev_info(&pf->pdev->dev,
11863 			 "There is no veb associated with the bridge\n");
11864 		return -ENOENT;
11865 	}
11866 
11867 	/* Uplink is a bridge in VEPA mode */
11868 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
11869 		return 0;
11870 	} else {
11871 		/* Uplink is a bridge in VEB mode */
11872 		return 1;
11873 	}
11874 
11875 	/* VEPA is now default bridge, so return 0 */
11876 	return 0;
11877 }
11878 
11879 /**
11880  * i40e_add_vsi - Add a VSI to the switch
11881  * @vsi: the VSI being configured
11882  *
11883  * This initializes a VSI context depending on the VSI type to be added and
11884  * passes it down to the add_vsi aq command.
11885  **/
11886 static int i40e_add_vsi(struct i40e_vsi *vsi)
11887 {
11888 	int ret = -ENODEV;
11889 	struct i40e_pf *pf = vsi->back;
11890 	struct i40e_hw *hw = &pf->hw;
11891 	struct i40e_vsi_context ctxt;
11892 	struct i40e_mac_filter *f;
11893 	struct hlist_node *h;
11894 	int bkt;
11895 
11896 	u8 enabled_tc = 0x1; /* TC0 enabled */
11897 	int f_count = 0;
11898 
11899 	memset(&ctxt, 0, sizeof(ctxt));
11900 	switch (vsi->type) {
11901 	case I40E_VSI_MAIN:
11902 		/* The PF's main VSI is already setup as part of the
11903 		 * device initialization, so we'll not bother with
11904 		 * the add_vsi call, but we will retrieve the current
11905 		 * VSI context.
11906 		 */
11907 		ctxt.seid = pf->main_vsi_seid;
11908 		ctxt.pf_num = pf->hw.pf_id;
11909 		ctxt.vf_num = 0;
11910 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
11911 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
11912 		if (ret) {
11913 			dev_info(&pf->pdev->dev,
11914 				 "couldn't get PF vsi config, err %s aq_err %s\n",
11915 				 i40e_stat_str(&pf->hw, ret),
11916 				 i40e_aq_str(&pf->hw,
11917 					     pf->hw.aq.asq_last_status));
11918 			return -ENOENT;
11919 		}
11920 		vsi->info = ctxt.info;
11921 		vsi->info.valid_sections = 0;
11922 
11923 		vsi->seid = ctxt.seid;
11924 		vsi->id = ctxt.vsi_number;
11925 
11926 		enabled_tc = i40e_pf_get_tc_map(pf);
11927 
11928 		/* Source pruning is enabled by default, so the flag is
11929 		 * negative logic - if it's set, we need to fiddle with
11930 		 * the VSI to disable source pruning.
11931 		 */
11932 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
11933 			memset(&ctxt, 0, sizeof(ctxt));
11934 			ctxt.seid = pf->main_vsi_seid;
11935 			ctxt.pf_num = pf->hw.pf_id;
11936 			ctxt.vf_num = 0;
11937 			ctxt.info.valid_sections |=
11938 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
11939 			ctxt.info.switch_id =
11940 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
11941 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
11942 			if (ret) {
11943 				dev_info(&pf->pdev->dev,
11944 					 "update vsi failed, err %s aq_err %s\n",
11945 					 i40e_stat_str(&pf->hw, ret),
11946 					 i40e_aq_str(&pf->hw,
11947 						     pf->hw.aq.asq_last_status));
11948 				ret = -ENOENT;
11949 				goto err;
11950 			}
11951 		}
11952 
11953 		/* MFP mode setup queue map and update VSI */
11954 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
11955 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
11956 			memset(&ctxt, 0, sizeof(ctxt));
11957 			ctxt.seid = pf->main_vsi_seid;
11958 			ctxt.pf_num = pf->hw.pf_id;
11959 			ctxt.vf_num = 0;
11960 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
11961 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
11962 			if (ret) {
11963 				dev_info(&pf->pdev->dev,
11964 					 "update vsi failed, err %s aq_err %s\n",
11965 					 i40e_stat_str(&pf->hw, ret),
11966 					 i40e_aq_str(&pf->hw,
11967 						    pf->hw.aq.asq_last_status));
11968 				ret = -ENOENT;
11969 				goto err;
11970 			}
11971 			/* update the local VSI info queue map */
11972 			i40e_vsi_update_queue_map(vsi, &ctxt);
11973 			vsi->info.valid_sections = 0;
11974 		} else {
11975 			/* Default/Main VSI is only enabled for TC0
11976 			 * reconfigure it to enable all TCs that are
11977 			 * available on the port in SFP mode.
11978 			 * For MFP case the iSCSI PF would use this
11979 			 * flow to enable LAN+iSCSI TC.
11980 			 */
11981 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
11982 			if (ret) {
11983 				/* Single TC condition is not fatal,
11984 				 * message and continue
11985 				 */
11986 				dev_info(&pf->pdev->dev,
11987 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
11988 					 enabled_tc,
11989 					 i40e_stat_str(&pf->hw, ret),
11990 					 i40e_aq_str(&pf->hw,
11991 						    pf->hw.aq.asq_last_status));
11992 			}
11993 		}
11994 		break;
11995 
11996 	case I40E_VSI_FDIR:
11997 		ctxt.pf_num = hw->pf_id;
11998 		ctxt.vf_num = 0;
11999 		ctxt.uplink_seid = vsi->uplink_seid;
12000 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12001 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12002 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
12003 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
12004 			ctxt.info.valid_sections |=
12005 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12006 			ctxt.info.switch_id =
12007 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12008 		}
12009 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12010 		break;
12011 
12012 	case I40E_VSI_VMDQ2:
12013 		ctxt.pf_num = hw->pf_id;
12014 		ctxt.vf_num = 0;
12015 		ctxt.uplink_seid = vsi->uplink_seid;
12016 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12017 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
12018 
12019 		/* This VSI is connected to VEB so the switch_id
12020 		 * should be set to zero by default.
12021 		 */
12022 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12023 			ctxt.info.valid_sections |=
12024 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12025 			ctxt.info.switch_id =
12026 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12027 		}
12028 
12029 		/* Setup the VSI tx/rx queue map for TC0 only for now */
12030 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12031 		break;
12032 
12033 	case I40E_VSI_SRIOV:
12034 		ctxt.pf_num = hw->pf_id;
12035 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
12036 		ctxt.uplink_seid = vsi->uplink_seid;
12037 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12038 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
12039 
12040 		/* This VSI is connected to VEB so the switch_id
12041 		 * should be set to zero by default.
12042 		 */
12043 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12044 			ctxt.info.valid_sections |=
12045 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12046 			ctxt.info.switch_id =
12047 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12048 		}
12049 
12050 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
12051 			ctxt.info.valid_sections |=
12052 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
12053 			ctxt.info.queueing_opt_flags |=
12054 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
12055 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
12056 		}
12057 
12058 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
12059 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
12060 		if (pf->vf[vsi->vf_id].spoofchk) {
12061 			ctxt.info.valid_sections |=
12062 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
12063 			ctxt.info.sec_flags |=
12064 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
12065 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
12066 		}
12067 		/* Setup the VSI tx/rx queue map for TC0 only for now */
12068 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12069 		break;
12070 
12071 	case I40E_VSI_IWARP:
12072 		/* send down message to iWARP */
12073 		break;
12074 
12075 	default:
12076 		return -ENODEV;
12077 	}
12078 
12079 	if (vsi->type != I40E_VSI_MAIN) {
12080 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
12081 		if (ret) {
12082 			dev_info(&vsi->back->pdev->dev,
12083 				 "add vsi failed, err %s aq_err %s\n",
12084 				 i40e_stat_str(&pf->hw, ret),
12085 				 i40e_aq_str(&pf->hw,
12086 					     pf->hw.aq.asq_last_status));
12087 			ret = -ENOENT;
12088 			goto err;
12089 		}
12090 		vsi->info = ctxt.info;
12091 		vsi->info.valid_sections = 0;
12092 		vsi->seid = ctxt.seid;
12093 		vsi->id = ctxt.vsi_number;
12094 	}
12095 
12096 	vsi->active_filters = 0;
12097 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
12098 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12099 	/* If macvlan filters already exist, force them to get loaded */
12100 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
12101 		f->state = I40E_FILTER_NEW;
12102 		f_count++;
12103 	}
12104 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12105 
12106 	if (f_count) {
12107 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
12108 		pf->flags |= I40E_FLAG_FILTER_SYNC;
12109 	}
12110 
12111 	/* Update VSI BW information */
12112 	ret = i40e_vsi_get_bw_info(vsi);
12113 	if (ret) {
12114 		dev_info(&pf->pdev->dev,
12115 			 "couldn't get vsi bw info, err %s aq_err %s\n",
12116 			 i40e_stat_str(&pf->hw, ret),
12117 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12118 		/* VSI is already added so not tearing that up */
12119 		ret = 0;
12120 	}
12121 
12122 err:
12123 	return ret;
12124 }
12125 
12126 /**
12127  * i40e_vsi_release - Delete a VSI and free its resources
12128  * @vsi: the VSI being removed
12129  *
12130  * Returns 0 on success or < 0 on error
12131  **/
12132 int i40e_vsi_release(struct i40e_vsi *vsi)
12133 {
12134 	struct i40e_mac_filter *f;
12135 	struct hlist_node *h;
12136 	struct i40e_veb *veb = NULL;
12137 	struct i40e_pf *pf;
12138 	u16 uplink_seid;
12139 	int i, n, bkt;
12140 
12141 	pf = vsi->back;
12142 
12143 	/* release of a VEB-owner or last VSI is not allowed */
12144 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
12145 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
12146 			 vsi->seid, vsi->uplink_seid);
12147 		return -ENODEV;
12148 	}
12149 	if (vsi == pf->vsi[pf->lan_vsi] &&
12150 	    !test_bit(__I40E_DOWN, pf->state)) {
12151 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
12152 		return -ENODEV;
12153 	}
12154 
12155 	uplink_seid = vsi->uplink_seid;
12156 	if (vsi->type != I40E_VSI_SRIOV) {
12157 		if (vsi->netdev_registered) {
12158 			vsi->netdev_registered = false;
12159 			if (vsi->netdev) {
12160 				/* results in a call to i40e_close() */
12161 				unregister_netdev(vsi->netdev);
12162 			}
12163 		} else {
12164 			i40e_vsi_close(vsi);
12165 		}
12166 		i40e_vsi_disable_irq(vsi);
12167 	}
12168 
12169 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12170 
12171 	/* clear the sync flag on all filters */
12172 	if (vsi->netdev) {
12173 		__dev_uc_unsync(vsi->netdev, NULL);
12174 		__dev_mc_unsync(vsi->netdev, NULL);
12175 	}
12176 
12177 	/* make sure any remaining filters are marked for deletion */
12178 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
12179 		__i40e_del_filter(vsi, f);
12180 
12181 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12182 
12183 	i40e_sync_vsi_filters(vsi);
12184 
12185 	i40e_vsi_delete(vsi);
12186 	i40e_vsi_free_q_vectors(vsi);
12187 	if (vsi->netdev) {
12188 		free_netdev(vsi->netdev);
12189 		vsi->netdev = NULL;
12190 	}
12191 	i40e_vsi_clear_rings(vsi);
12192 	i40e_vsi_clear(vsi);
12193 
12194 	/* If this was the last thing on the VEB, except for the
12195 	 * controlling VSI, remove the VEB, which puts the controlling
12196 	 * VSI onto the next level down in the switch.
12197 	 *
12198 	 * Well, okay, there's one more exception here: don't remove
12199 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
12200 	 * from up the network stack.
12201 	 */
12202 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
12203 		if (pf->vsi[i] &&
12204 		    pf->vsi[i]->uplink_seid == uplink_seid &&
12205 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12206 			n++;      /* count the VSIs */
12207 		}
12208 	}
12209 	for (i = 0; i < I40E_MAX_VEB; i++) {
12210 		if (!pf->veb[i])
12211 			continue;
12212 		if (pf->veb[i]->uplink_seid == uplink_seid)
12213 			n++;     /* count the VEBs */
12214 		if (pf->veb[i]->seid == uplink_seid)
12215 			veb = pf->veb[i];
12216 	}
12217 	if (n == 0 && veb && veb->uplink_seid != 0)
12218 		i40e_veb_release(veb);
12219 
12220 	return 0;
12221 }
12222 
12223 /**
12224  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
12225  * @vsi: ptr to the VSI
12226  *
12227  * This should only be called after i40e_vsi_mem_alloc() which allocates the
12228  * corresponding SW VSI structure and initializes num_queue_pairs for the
12229  * newly allocated VSI.
12230  *
12231  * Returns 0 on success or negative on failure
12232  **/
12233 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
12234 {
12235 	int ret = -ENOENT;
12236 	struct i40e_pf *pf = vsi->back;
12237 
12238 	if (vsi->q_vectors[0]) {
12239 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
12240 			 vsi->seid);
12241 		return -EEXIST;
12242 	}
12243 
12244 	if (vsi->base_vector) {
12245 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
12246 			 vsi->seid, vsi->base_vector);
12247 		return -EEXIST;
12248 	}
12249 
12250 	ret = i40e_vsi_alloc_q_vectors(vsi);
12251 	if (ret) {
12252 		dev_info(&pf->pdev->dev,
12253 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
12254 			 vsi->num_q_vectors, vsi->seid, ret);
12255 		vsi->num_q_vectors = 0;
12256 		goto vector_setup_out;
12257 	}
12258 
12259 	/* In Legacy mode, we do not have to get any other vector since we
12260 	 * piggyback on the misc/ICR0 for queue interrupts.
12261 	*/
12262 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
12263 		return ret;
12264 	if (vsi->num_q_vectors)
12265 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
12266 						 vsi->num_q_vectors, vsi->idx);
12267 	if (vsi->base_vector < 0) {
12268 		dev_info(&pf->pdev->dev,
12269 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
12270 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
12271 		i40e_vsi_free_q_vectors(vsi);
12272 		ret = -ENOENT;
12273 		goto vector_setup_out;
12274 	}
12275 
12276 vector_setup_out:
12277 	return ret;
12278 }
12279 
12280 /**
12281  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
12282  * @vsi: pointer to the vsi.
12283  *
12284  * This re-allocates a vsi's queue resources.
12285  *
12286  * Returns pointer to the successfully allocated and configured VSI sw struct
12287  * on success, otherwise returns NULL on failure.
12288  **/
12289 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
12290 {
12291 	u16 alloc_queue_pairs;
12292 	struct i40e_pf *pf;
12293 	u8 enabled_tc;
12294 	int ret;
12295 
12296 	if (!vsi)
12297 		return NULL;
12298 
12299 	pf = vsi->back;
12300 
12301 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
12302 	i40e_vsi_clear_rings(vsi);
12303 
12304 	i40e_vsi_free_arrays(vsi, false);
12305 	i40e_set_num_rings_in_vsi(vsi);
12306 	ret = i40e_vsi_alloc_arrays(vsi, false);
12307 	if (ret)
12308 		goto err_vsi;
12309 
12310 	alloc_queue_pairs = vsi->alloc_queue_pairs *
12311 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12312 
12313 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12314 	if (ret < 0) {
12315 		dev_info(&pf->pdev->dev,
12316 			 "failed to get tracking for %d queues for VSI %d err %d\n",
12317 			 alloc_queue_pairs, vsi->seid, ret);
12318 		goto err_vsi;
12319 	}
12320 	vsi->base_queue = ret;
12321 
12322 	/* Update the FW view of the VSI. Force a reset of TC and queue
12323 	 * layout configurations.
12324 	 */
12325 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
12326 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
12327 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
12328 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
12329 	if (vsi->type == I40E_VSI_MAIN)
12330 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
12331 
12332 	/* assign it some queues */
12333 	ret = i40e_alloc_rings(vsi);
12334 	if (ret)
12335 		goto err_rings;
12336 
12337 	/* map all of the rings to the q_vectors */
12338 	i40e_vsi_map_rings_to_vectors(vsi);
12339 	return vsi;
12340 
12341 err_rings:
12342 	i40e_vsi_free_q_vectors(vsi);
12343 	if (vsi->netdev_registered) {
12344 		vsi->netdev_registered = false;
12345 		unregister_netdev(vsi->netdev);
12346 		free_netdev(vsi->netdev);
12347 		vsi->netdev = NULL;
12348 	}
12349 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12350 err_vsi:
12351 	i40e_vsi_clear(vsi);
12352 	return NULL;
12353 }
12354 
12355 /**
12356  * i40e_vsi_setup - Set up a VSI by a given type
12357  * @pf: board private structure
12358  * @type: VSI type
12359  * @uplink_seid: the switch element to link to
12360  * @param1: usage depends upon VSI type. For VF types, indicates VF id
12361  *
12362  * This allocates the sw VSI structure and its queue resources, then add a VSI
12363  * to the identified VEB.
12364  *
12365  * Returns pointer to the successfully allocated and configure VSI sw struct on
12366  * success, otherwise returns NULL on failure.
12367  **/
12368 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
12369 				u16 uplink_seid, u32 param1)
12370 {
12371 	struct i40e_vsi *vsi = NULL;
12372 	struct i40e_veb *veb = NULL;
12373 	u16 alloc_queue_pairs;
12374 	int ret, i;
12375 	int v_idx;
12376 
12377 	/* The requested uplink_seid must be either
12378 	 *     - the PF's port seid
12379 	 *              no VEB is needed because this is the PF
12380 	 *              or this is a Flow Director special case VSI
12381 	 *     - seid of an existing VEB
12382 	 *     - seid of a VSI that owns an existing VEB
12383 	 *     - seid of a VSI that doesn't own a VEB
12384 	 *              a new VEB is created and the VSI becomes the owner
12385 	 *     - seid of the PF VSI, which is what creates the first VEB
12386 	 *              this is a special case of the previous
12387 	 *
12388 	 * Find which uplink_seid we were given and create a new VEB if needed
12389 	 */
12390 	for (i = 0; i < I40E_MAX_VEB; i++) {
12391 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
12392 			veb = pf->veb[i];
12393 			break;
12394 		}
12395 	}
12396 
12397 	if (!veb && uplink_seid != pf->mac_seid) {
12398 
12399 		for (i = 0; i < pf->num_alloc_vsi; i++) {
12400 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
12401 				vsi = pf->vsi[i];
12402 				break;
12403 			}
12404 		}
12405 		if (!vsi) {
12406 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
12407 				 uplink_seid);
12408 			return NULL;
12409 		}
12410 
12411 		if (vsi->uplink_seid == pf->mac_seid)
12412 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
12413 					     vsi->tc_config.enabled_tc);
12414 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
12415 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12416 					     vsi->tc_config.enabled_tc);
12417 		if (veb) {
12418 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
12419 				dev_info(&vsi->back->pdev->dev,
12420 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
12421 				return NULL;
12422 			}
12423 			/* We come up by default in VEPA mode if SRIOV is not
12424 			 * already enabled, in which case we can't force VEPA
12425 			 * mode.
12426 			 */
12427 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
12428 				veb->bridge_mode = BRIDGE_MODE_VEPA;
12429 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12430 			}
12431 			i40e_config_bridge_mode(veb);
12432 		}
12433 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12434 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12435 				veb = pf->veb[i];
12436 		}
12437 		if (!veb) {
12438 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
12439 			return NULL;
12440 		}
12441 
12442 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12443 		uplink_seid = veb->seid;
12444 	}
12445 
12446 	/* get vsi sw struct */
12447 	v_idx = i40e_vsi_mem_alloc(pf, type);
12448 	if (v_idx < 0)
12449 		goto err_alloc;
12450 	vsi = pf->vsi[v_idx];
12451 	if (!vsi)
12452 		goto err_alloc;
12453 	vsi->type = type;
12454 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
12455 
12456 	if (type == I40E_VSI_MAIN)
12457 		pf->lan_vsi = v_idx;
12458 	else if (type == I40E_VSI_SRIOV)
12459 		vsi->vf_id = param1;
12460 	/* assign it some queues */
12461 	alloc_queue_pairs = vsi->alloc_queue_pairs *
12462 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12463 
12464 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12465 	if (ret < 0) {
12466 		dev_info(&pf->pdev->dev,
12467 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
12468 			 alloc_queue_pairs, vsi->seid, ret);
12469 		goto err_vsi;
12470 	}
12471 	vsi->base_queue = ret;
12472 
12473 	/* get a VSI from the hardware */
12474 	vsi->uplink_seid = uplink_seid;
12475 	ret = i40e_add_vsi(vsi);
12476 	if (ret)
12477 		goto err_vsi;
12478 
12479 	switch (vsi->type) {
12480 	/* setup the netdev if needed */
12481 	case I40E_VSI_MAIN:
12482 	case I40E_VSI_VMDQ2:
12483 		ret = i40e_config_netdev(vsi);
12484 		if (ret)
12485 			goto err_netdev;
12486 		ret = register_netdev(vsi->netdev);
12487 		if (ret)
12488 			goto err_netdev;
12489 		vsi->netdev_registered = true;
12490 		netif_carrier_off(vsi->netdev);
12491 #ifdef CONFIG_I40E_DCB
12492 		/* Setup DCB netlink interface */
12493 		i40e_dcbnl_setup(vsi);
12494 #endif /* CONFIG_I40E_DCB */
12495 		/* fall through */
12496 
12497 	case I40E_VSI_FDIR:
12498 		/* set up vectors and rings if needed */
12499 		ret = i40e_vsi_setup_vectors(vsi);
12500 		if (ret)
12501 			goto err_msix;
12502 
12503 		ret = i40e_alloc_rings(vsi);
12504 		if (ret)
12505 			goto err_rings;
12506 
12507 		/* map all of the rings to the q_vectors */
12508 		i40e_vsi_map_rings_to_vectors(vsi);
12509 
12510 		i40e_vsi_reset_stats(vsi);
12511 		break;
12512 
12513 	default:
12514 		/* no netdev or rings for the other VSI types */
12515 		break;
12516 	}
12517 
12518 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
12519 	    (vsi->type == I40E_VSI_VMDQ2)) {
12520 		ret = i40e_vsi_config_rss(vsi);
12521 	}
12522 	return vsi;
12523 
12524 err_rings:
12525 	i40e_vsi_free_q_vectors(vsi);
12526 err_msix:
12527 	if (vsi->netdev_registered) {
12528 		vsi->netdev_registered = false;
12529 		unregister_netdev(vsi->netdev);
12530 		free_netdev(vsi->netdev);
12531 		vsi->netdev = NULL;
12532 	}
12533 err_netdev:
12534 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12535 err_vsi:
12536 	i40e_vsi_clear(vsi);
12537 err_alloc:
12538 	return NULL;
12539 }
12540 
12541 /**
12542  * i40e_veb_get_bw_info - Query VEB BW information
12543  * @veb: the veb to query
12544  *
12545  * Query the Tx scheduler BW configuration data for given VEB
12546  **/
12547 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
12548 {
12549 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
12550 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
12551 	struct i40e_pf *pf = veb->pf;
12552 	struct i40e_hw *hw = &pf->hw;
12553 	u32 tc_bw_max;
12554 	int ret = 0;
12555 	int i;
12556 
12557 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
12558 						  &bw_data, NULL);
12559 	if (ret) {
12560 		dev_info(&pf->pdev->dev,
12561 			 "query veb bw config failed, err %s aq_err %s\n",
12562 			 i40e_stat_str(&pf->hw, ret),
12563 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
12564 		goto out;
12565 	}
12566 
12567 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
12568 						   &ets_data, NULL);
12569 	if (ret) {
12570 		dev_info(&pf->pdev->dev,
12571 			 "query veb bw ets config failed, err %s aq_err %s\n",
12572 			 i40e_stat_str(&pf->hw, ret),
12573 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
12574 		goto out;
12575 	}
12576 
12577 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
12578 	veb->bw_max_quanta = ets_data.tc_bw_max;
12579 	veb->is_abs_credits = bw_data.absolute_credits_enable;
12580 	veb->enabled_tc = ets_data.tc_valid_bits;
12581 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
12582 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
12583 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
12584 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
12585 		veb->bw_tc_limit_credits[i] =
12586 					le16_to_cpu(bw_data.tc_bw_limits[i]);
12587 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
12588 	}
12589 
12590 out:
12591 	return ret;
12592 }
12593 
12594 /**
12595  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
12596  * @pf: board private structure
12597  *
12598  * On error: returns error code (negative)
12599  * On success: returns vsi index in PF (positive)
12600  **/
12601 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
12602 {
12603 	int ret = -ENOENT;
12604 	struct i40e_veb *veb;
12605 	int i;
12606 
12607 	/* Need to protect the allocation of switch elements at the PF level */
12608 	mutex_lock(&pf->switch_mutex);
12609 
12610 	/* VEB list may be fragmented if VEB creation/destruction has
12611 	 * been happening.  We can afford to do a quick scan to look
12612 	 * for any free slots in the list.
12613 	 *
12614 	 * find next empty veb slot, looping back around if necessary
12615 	 */
12616 	i = 0;
12617 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
12618 		i++;
12619 	if (i >= I40E_MAX_VEB) {
12620 		ret = -ENOMEM;
12621 		goto err_alloc_veb;  /* out of VEB slots! */
12622 	}
12623 
12624 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
12625 	if (!veb) {
12626 		ret = -ENOMEM;
12627 		goto err_alloc_veb;
12628 	}
12629 	veb->pf = pf;
12630 	veb->idx = i;
12631 	veb->enabled_tc = 1;
12632 
12633 	pf->veb[i] = veb;
12634 	ret = i;
12635 err_alloc_veb:
12636 	mutex_unlock(&pf->switch_mutex);
12637 	return ret;
12638 }
12639 
12640 /**
12641  * i40e_switch_branch_release - Delete a branch of the switch tree
12642  * @branch: where to start deleting
12643  *
12644  * This uses recursion to find the tips of the branch to be
12645  * removed, deleting until we get back to and can delete this VEB.
12646  **/
12647 static void i40e_switch_branch_release(struct i40e_veb *branch)
12648 {
12649 	struct i40e_pf *pf = branch->pf;
12650 	u16 branch_seid = branch->seid;
12651 	u16 veb_idx = branch->idx;
12652 	int i;
12653 
12654 	/* release any VEBs on this VEB - RECURSION */
12655 	for (i = 0; i < I40E_MAX_VEB; i++) {
12656 		if (!pf->veb[i])
12657 			continue;
12658 		if (pf->veb[i]->uplink_seid == branch->seid)
12659 			i40e_switch_branch_release(pf->veb[i]);
12660 	}
12661 
12662 	/* Release the VSIs on this VEB, but not the owner VSI.
12663 	 *
12664 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
12665 	 *       the VEB itself, so don't use (*branch) after this loop.
12666 	 */
12667 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12668 		if (!pf->vsi[i])
12669 			continue;
12670 		if (pf->vsi[i]->uplink_seid == branch_seid &&
12671 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12672 			i40e_vsi_release(pf->vsi[i]);
12673 		}
12674 	}
12675 
12676 	/* There's one corner case where the VEB might not have been
12677 	 * removed, so double check it here and remove it if needed.
12678 	 * This case happens if the veb was created from the debugfs
12679 	 * commands and no VSIs were added to it.
12680 	 */
12681 	if (pf->veb[veb_idx])
12682 		i40e_veb_release(pf->veb[veb_idx]);
12683 }
12684 
12685 /**
12686  * i40e_veb_clear - remove veb struct
12687  * @veb: the veb to remove
12688  **/
12689 static void i40e_veb_clear(struct i40e_veb *veb)
12690 {
12691 	if (!veb)
12692 		return;
12693 
12694 	if (veb->pf) {
12695 		struct i40e_pf *pf = veb->pf;
12696 
12697 		mutex_lock(&pf->switch_mutex);
12698 		if (pf->veb[veb->idx] == veb)
12699 			pf->veb[veb->idx] = NULL;
12700 		mutex_unlock(&pf->switch_mutex);
12701 	}
12702 
12703 	kfree(veb);
12704 }
12705 
12706 /**
12707  * i40e_veb_release - Delete a VEB and free its resources
12708  * @veb: the VEB being removed
12709  **/
12710 void i40e_veb_release(struct i40e_veb *veb)
12711 {
12712 	struct i40e_vsi *vsi = NULL;
12713 	struct i40e_pf *pf;
12714 	int i, n = 0;
12715 
12716 	pf = veb->pf;
12717 
12718 	/* find the remaining VSI and check for extras */
12719 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12720 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
12721 			n++;
12722 			vsi = pf->vsi[i];
12723 		}
12724 	}
12725 	if (n != 1) {
12726 		dev_info(&pf->pdev->dev,
12727 			 "can't remove VEB %d with %d VSIs left\n",
12728 			 veb->seid, n);
12729 		return;
12730 	}
12731 
12732 	/* move the remaining VSI to uplink veb */
12733 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
12734 	if (veb->uplink_seid) {
12735 		vsi->uplink_seid = veb->uplink_seid;
12736 		if (veb->uplink_seid == pf->mac_seid)
12737 			vsi->veb_idx = I40E_NO_VEB;
12738 		else
12739 			vsi->veb_idx = veb->veb_idx;
12740 	} else {
12741 		/* floating VEB */
12742 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
12743 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
12744 	}
12745 
12746 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
12747 	i40e_veb_clear(veb);
12748 }
12749 
12750 /**
12751  * i40e_add_veb - create the VEB in the switch
12752  * @veb: the VEB to be instantiated
12753  * @vsi: the controlling VSI
12754  **/
12755 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
12756 {
12757 	struct i40e_pf *pf = veb->pf;
12758 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
12759 	int ret;
12760 
12761 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
12762 			      veb->enabled_tc, false,
12763 			      &veb->seid, enable_stats, NULL);
12764 
12765 	/* get a VEB from the hardware */
12766 	if (ret) {
12767 		dev_info(&pf->pdev->dev,
12768 			 "couldn't add VEB, err %s aq_err %s\n",
12769 			 i40e_stat_str(&pf->hw, ret),
12770 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12771 		return -EPERM;
12772 	}
12773 
12774 	/* get statistics counter */
12775 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
12776 					 &veb->stats_idx, NULL, NULL, NULL);
12777 	if (ret) {
12778 		dev_info(&pf->pdev->dev,
12779 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
12780 			 i40e_stat_str(&pf->hw, ret),
12781 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12782 		return -EPERM;
12783 	}
12784 	ret = i40e_veb_get_bw_info(veb);
12785 	if (ret) {
12786 		dev_info(&pf->pdev->dev,
12787 			 "couldn't get VEB bw info, err %s aq_err %s\n",
12788 			 i40e_stat_str(&pf->hw, ret),
12789 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12790 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
12791 		return -ENOENT;
12792 	}
12793 
12794 	vsi->uplink_seid = veb->seid;
12795 	vsi->veb_idx = veb->idx;
12796 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12797 
12798 	return 0;
12799 }
12800 
12801 /**
12802  * i40e_veb_setup - Set up a VEB
12803  * @pf: board private structure
12804  * @flags: VEB setup flags
12805  * @uplink_seid: the switch element to link to
12806  * @vsi_seid: the initial VSI seid
12807  * @enabled_tc: Enabled TC bit-map
12808  *
12809  * This allocates the sw VEB structure and links it into the switch
12810  * It is possible and legal for this to be a duplicate of an already
12811  * existing VEB.  It is also possible for both uplink and vsi seids
12812  * to be zero, in order to create a floating VEB.
12813  *
12814  * Returns pointer to the successfully allocated VEB sw struct on
12815  * success, otherwise returns NULL on failure.
12816  **/
12817 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
12818 				u16 uplink_seid, u16 vsi_seid,
12819 				u8 enabled_tc)
12820 {
12821 	struct i40e_veb *veb, *uplink_veb = NULL;
12822 	int vsi_idx, veb_idx;
12823 	int ret;
12824 
12825 	/* if one seid is 0, the other must be 0 to create a floating relay */
12826 	if ((uplink_seid == 0 || vsi_seid == 0) &&
12827 	    (uplink_seid + vsi_seid != 0)) {
12828 		dev_info(&pf->pdev->dev,
12829 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
12830 			 uplink_seid, vsi_seid);
12831 		return NULL;
12832 	}
12833 
12834 	/* make sure there is such a vsi and uplink */
12835 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
12836 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
12837 			break;
12838 	if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
12839 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
12840 			 vsi_seid);
12841 		return NULL;
12842 	}
12843 
12844 	if (uplink_seid && uplink_seid != pf->mac_seid) {
12845 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
12846 			if (pf->veb[veb_idx] &&
12847 			    pf->veb[veb_idx]->seid == uplink_seid) {
12848 				uplink_veb = pf->veb[veb_idx];
12849 				break;
12850 			}
12851 		}
12852 		if (!uplink_veb) {
12853 			dev_info(&pf->pdev->dev,
12854 				 "uplink seid %d not found\n", uplink_seid);
12855 			return NULL;
12856 		}
12857 	}
12858 
12859 	/* get veb sw struct */
12860 	veb_idx = i40e_veb_mem_alloc(pf);
12861 	if (veb_idx < 0)
12862 		goto err_alloc;
12863 	veb = pf->veb[veb_idx];
12864 	veb->flags = flags;
12865 	veb->uplink_seid = uplink_seid;
12866 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
12867 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
12868 
12869 	/* create the VEB in the switch */
12870 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
12871 	if (ret)
12872 		goto err_veb;
12873 	if (vsi_idx == pf->lan_vsi)
12874 		pf->lan_veb = veb->idx;
12875 
12876 	return veb;
12877 
12878 err_veb:
12879 	i40e_veb_clear(veb);
12880 err_alloc:
12881 	return NULL;
12882 }
12883 
12884 /**
12885  * i40e_setup_pf_switch_element - set PF vars based on switch type
12886  * @pf: board private structure
12887  * @ele: element we are building info from
12888  * @num_reported: total number of elements
12889  * @printconfig: should we print the contents
12890  *
12891  * helper function to assist in extracting a few useful SEID values.
12892  **/
12893 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
12894 				struct i40e_aqc_switch_config_element_resp *ele,
12895 				u16 num_reported, bool printconfig)
12896 {
12897 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
12898 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
12899 	u8 element_type = ele->element_type;
12900 	u16 seid = le16_to_cpu(ele->seid);
12901 
12902 	if (printconfig)
12903 		dev_info(&pf->pdev->dev,
12904 			 "type=%d seid=%d uplink=%d downlink=%d\n",
12905 			 element_type, seid, uplink_seid, downlink_seid);
12906 
12907 	switch (element_type) {
12908 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
12909 		pf->mac_seid = seid;
12910 		break;
12911 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
12912 		/* Main VEB? */
12913 		if (uplink_seid != pf->mac_seid)
12914 			break;
12915 		if (pf->lan_veb == I40E_NO_VEB) {
12916 			int v;
12917 
12918 			/* find existing or else empty VEB */
12919 			for (v = 0; v < I40E_MAX_VEB; v++) {
12920 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
12921 					pf->lan_veb = v;
12922 					break;
12923 				}
12924 			}
12925 			if (pf->lan_veb == I40E_NO_VEB) {
12926 				v = i40e_veb_mem_alloc(pf);
12927 				if (v < 0)
12928 					break;
12929 				pf->lan_veb = v;
12930 			}
12931 		}
12932 
12933 		pf->veb[pf->lan_veb]->seid = seid;
12934 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
12935 		pf->veb[pf->lan_veb]->pf = pf;
12936 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
12937 		break;
12938 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
12939 		if (num_reported != 1)
12940 			break;
12941 		/* This is immediately after a reset so we can assume this is
12942 		 * the PF's VSI
12943 		 */
12944 		pf->mac_seid = uplink_seid;
12945 		pf->pf_seid = downlink_seid;
12946 		pf->main_vsi_seid = seid;
12947 		if (printconfig)
12948 			dev_info(&pf->pdev->dev,
12949 				 "pf_seid=%d main_vsi_seid=%d\n",
12950 				 pf->pf_seid, pf->main_vsi_seid);
12951 		break;
12952 	case I40E_SWITCH_ELEMENT_TYPE_PF:
12953 	case I40E_SWITCH_ELEMENT_TYPE_VF:
12954 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
12955 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
12956 	case I40E_SWITCH_ELEMENT_TYPE_PE:
12957 	case I40E_SWITCH_ELEMENT_TYPE_PA:
12958 		/* ignore these for now */
12959 		break;
12960 	default:
12961 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
12962 			 element_type, seid);
12963 		break;
12964 	}
12965 }
12966 
12967 /**
12968  * i40e_fetch_switch_configuration - Get switch config from firmware
12969  * @pf: board private structure
12970  * @printconfig: should we print the contents
12971  *
12972  * Get the current switch configuration from the device and
12973  * extract a few useful SEID values.
12974  **/
12975 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
12976 {
12977 	struct i40e_aqc_get_switch_config_resp *sw_config;
12978 	u16 next_seid = 0;
12979 	int ret = 0;
12980 	u8 *aq_buf;
12981 	int i;
12982 
12983 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
12984 	if (!aq_buf)
12985 		return -ENOMEM;
12986 
12987 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
12988 	do {
12989 		u16 num_reported, num_total;
12990 
12991 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
12992 						I40E_AQ_LARGE_BUF,
12993 						&next_seid, NULL);
12994 		if (ret) {
12995 			dev_info(&pf->pdev->dev,
12996 				 "get switch config failed err %s aq_err %s\n",
12997 				 i40e_stat_str(&pf->hw, ret),
12998 				 i40e_aq_str(&pf->hw,
12999 					     pf->hw.aq.asq_last_status));
13000 			kfree(aq_buf);
13001 			return -ENOENT;
13002 		}
13003 
13004 		num_reported = le16_to_cpu(sw_config->header.num_reported);
13005 		num_total = le16_to_cpu(sw_config->header.num_total);
13006 
13007 		if (printconfig)
13008 			dev_info(&pf->pdev->dev,
13009 				 "header: %d reported %d total\n",
13010 				 num_reported, num_total);
13011 
13012 		for (i = 0; i < num_reported; i++) {
13013 			struct i40e_aqc_switch_config_element_resp *ele =
13014 				&sw_config->element[i];
13015 
13016 			i40e_setup_pf_switch_element(pf, ele, num_reported,
13017 						     printconfig);
13018 		}
13019 	} while (next_seid != 0);
13020 
13021 	kfree(aq_buf);
13022 	return ret;
13023 }
13024 
13025 /**
13026  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
13027  * @pf: board private structure
13028  * @reinit: if the Main VSI needs to re-initialized.
13029  *
13030  * Returns 0 on success, negative value on failure
13031  **/
13032 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
13033 {
13034 	u16 flags = 0;
13035 	int ret;
13036 
13037 	/* find out what's out there already */
13038 	ret = i40e_fetch_switch_configuration(pf, false);
13039 	if (ret) {
13040 		dev_info(&pf->pdev->dev,
13041 			 "couldn't fetch switch config, err %s aq_err %s\n",
13042 			 i40e_stat_str(&pf->hw, ret),
13043 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13044 		return ret;
13045 	}
13046 	i40e_pf_reset_stats(pf);
13047 
13048 	/* set the switch config bit for the whole device to
13049 	 * support limited promisc or true promisc
13050 	 * when user requests promisc. The default is limited
13051 	 * promisc.
13052 	*/
13053 
13054 	if ((pf->hw.pf_id == 0) &&
13055 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
13056 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13057 		pf->last_sw_conf_flags = flags;
13058 	}
13059 
13060 	if (pf->hw.pf_id == 0) {
13061 		u16 valid_flags;
13062 
13063 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13064 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
13065 						NULL);
13066 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
13067 			dev_info(&pf->pdev->dev,
13068 				 "couldn't set switch config bits, err %s aq_err %s\n",
13069 				 i40e_stat_str(&pf->hw, ret),
13070 				 i40e_aq_str(&pf->hw,
13071 					     pf->hw.aq.asq_last_status));
13072 			/* not a fatal problem, just keep going */
13073 		}
13074 		pf->last_sw_conf_valid_flags = valid_flags;
13075 	}
13076 
13077 	/* first time setup */
13078 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
13079 		struct i40e_vsi *vsi = NULL;
13080 		u16 uplink_seid;
13081 
13082 		/* Set up the PF VSI associated with the PF's main VSI
13083 		 * that is already in the HW switch
13084 		 */
13085 		if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
13086 			uplink_seid = pf->veb[pf->lan_veb]->seid;
13087 		else
13088 			uplink_seid = pf->mac_seid;
13089 		if (pf->lan_vsi == I40E_NO_VSI)
13090 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
13091 		else if (reinit)
13092 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
13093 		if (!vsi) {
13094 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
13095 			i40e_cloud_filter_exit(pf);
13096 			i40e_fdir_teardown(pf);
13097 			return -EAGAIN;
13098 		}
13099 	} else {
13100 		/* force a reset of TC and queue layout configurations */
13101 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13102 
13103 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13104 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13105 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13106 	}
13107 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
13108 
13109 	i40e_fdir_sb_setup(pf);
13110 
13111 	/* Setup static PF queue filter control settings */
13112 	ret = i40e_setup_pf_filter_control(pf);
13113 	if (ret) {
13114 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
13115 			 ret);
13116 		/* Failure here should not stop continuing other steps */
13117 	}
13118 
13119 	/* enable RSS in the HW, even for only one queue, as the stack can use
13120 	 * the hash
13121 	 */
13122 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
13123 		i40e_pf_config_rss(pf);
13124 
13125 	/* fill in link information and enable LSE reporting */
13126 	i40e_link_event(pf);
13127 
13128 	/* Initialize user-specific link properties */
13129 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
13130 				  I40E_AQ_AN_COMPLETED) ? true : false);
13131 
13132 	i40e_ptp_init(pf);
13133 
13134 	/* repopulate tunnel port filters */
13135 	i40e_sync_udp_filters(pf);
13136 
13137 	return ret;
13138 }
13139 
13140 /**
13141  * i40e_determine_queue_usage - Work out queue distribution
13142  * @pf: board private structure
13143  **/
13144 static void i40e_determine_queue_usage(struct i40e_pf *pf)
13145 {
13146 	int queues_left;
13147 	int q_max;
13148 
13149 	pf->num_lan_qps = 0;
13150 
13151 	/* Find the max queues to be put into basic use.  We'll always be
13152 	 * using TC0, whether or not DCB is running, and TC0 will get the
13153 	 * big RSS set.
13154 	 */
13155 	queues_left = pf->hw.func_caps.num_tx_qp;
13156 
13157 	if ((queues_left == 1) ||
13158 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
13159 		/* one qp for PF, no queues for anything else */
13160 		queues_left = 0;
13161 		pf->alloc_rss_size = pf->num_lan_qps = 1;
13162 
13163 		/* make sure all the fancies are disabled */
13164 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
13165 			       I40E_FLAG_IWARP_ENABLED	|
13166 			       I40E_FLAG_FD_SB_ENABLED	|
13167 			       I40E_FLAG_FD_ATR_ENABLED	|
13168 			       I40E_FLAG_DCB_CAPABLE	|
13169 			       I40E_FLAG_DCB_ENABLED	|
13170 			       I40E_FLAG_SRIOV_ENABLED	|
13171 			       I40E_FLAG_VMDQ_ENABLED);
13172 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13173 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
13174 				  I40E_FLAG_FD_SB_ENABLED |
13175 				  I40E_FLAG_FD_ATR_ENABLED |
13176 				  I40E_FLAG_DCB_CAPABLE))) {
13177 		/* one qp for PF */
13178 		pf->alloc_rss_size = pf->num_lan_qps = 1;
13179 		queues_left -= pf->num_lan_qps;
13180 
13181 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
13182 			       I40E_FLAG_IWARP_ENABLED	|
13183 			       I40E_FLAG_FD_SB_ENABLED	|
13184 			       I40E_FLAG_FD_ATR_ENABLED	|
13185 			       I40E_FLAG_DCB_ENABLED	|
13186 			       I40E_FLAG_VMDQ_ENABLED);
13187 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13188 	} else {
13189 		/* Not enough queues for all TCs */
13190 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
13191 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
13192 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
13193 					I40E_FLAG_DCB_ENABLED);
13194 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
13195 		}
13196 
13197 		/* limit lan qps to the smaller of qps, cpus or msix */
13198 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
13199 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
13200 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
13201 		pf->num_lan_qps = q_max;
13202 
13203 		queues_left -= pf->num_lan_qps;
13204 	}
13205 
13206 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13207 		if (queues_left > 1) {
13208 			queues_left -= 1; /* save 1 queue for FD */
13209 		} else {
13210 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
13211 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13212 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
13213 		}
13214 	}
13215 
13216 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13217 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
13218 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
13219 					(queues_left / pf->num_vf_qps));
13220 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
13221 	}
13222 
13223 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
13224 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
13225 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
13226 					  (queues_left / pf->num_vmdq_qps));
13227 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
13228 	}
13229 
13230 	pf->queues_left = queues_left;
13231 	dev_dbg(&pf->pdev->dev,
13232 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
13233 		pf->hw.func_caps.num_tx_qp,
13234 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
13235 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
13236 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
13237 		queues_left);
13238 }
13239 
13240 /**
13241  * i40e_setup_pf_filter_control - Setup PF static filter control
13242  * @pf: PF to be setup
13243  *
13244  * i40e_setup_pf_filter_control sets up a PF's initial filter control
13245  * settings. If PE/FCoE are enabled then it will also set the per PF
13246  * based filter sizes required for them. It also enables Flow director,
13247  * ethertype and macvlan type filter settings for the pf.
13248  *
13249  * Returns 0 on success, negative on failure
13250  **/
13251 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
13252 {
13253 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
13254 
13255 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
13256 
13257 	/* Flow Director is enabled */
13258 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
13259 		settings->enable_fdir = true;
13260 
13261 	/* Ethtype and MACVLAN filters enabled for PF */
13262 	settings->enable_ethtype = true;
13263 	settings->enable_macvlan = true;
13264 
13265 	if (i40e_set_filter_control(&pf->hw, settings))
13266 		return -ENOENT;
13267 
13268 	return 0;
13269 }
13270 
13271 #define INFO_STRING_LEN 255
13272 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
13273 static void i40e_print_features(struct i40e_pf *pf)
13274 {
13275 	struct i40e_hw *hw = &pf->hw;
13276 	char *buf;
13277 	int i;
13278 
13279 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
13280 	if (!buf)
13281 		return;
13282 
13283 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
13284 #ifdef CONFIG_PCI_IOV
13285 	i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
13286 #endif
13287 	i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
13288 		      pf->hw.func_caps.num_vsis,
13289 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
13290 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
13291 		i += snprintf(&buf[i], REMAIN(i), " RSS");
13292 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
13293 		i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
13294 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13295 		i += snprintf(&buf[i], REMAIN(i), " FD_SB");
13296 		i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
13297 	}
13298 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
13299 		i += snprintf(&buf[i], REMAIN(i), " DCB");
13300 	i += snprintf(&buf[i], REMAIN(i), " VxLAN");
13301 	i += snprintf(&buf[i], REMAIN(i), " Geneve");
13302 	if (pf->flags & I40E_FLAG_PTP)
13303 		i += snprintf(&buf[i], REMAIN(i), " PTP");
13304 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
13305 		i += snprintf(&buf[i], REMAIN(i), " VEB");
13306 	else
13307 		i += snprintf(&buf[i], REMAIN(i), " VEPA");
13308 
13309 	dev_info(&pf->pdev->dev, "%s\n", buf);
13310 	kfree(buf);
13311 	WARN_ON(i > INFO_STRING_LEN);
13312 }
13313 
13314 /**
13315  * i40e_get_platform_mac_addr - get platform-specific MAC address
13316  * @pdev: PCI device information struct
13317  * @pf: board private structure
13318  *
13319  * Look up the MAC address for the device. First we'll try
13320  * eth_platform_get_mac_address, which will check Open Firmware, or arch
13321  * specific fallback. Otherwise, we'll default to the stored value in
13322  * firmware.
13323  **/
13324 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
13325 {
13326 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
13327 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
13328 }
13329 
13330 /**
13331  * i40e_probe - Device initialization routine
13332  * @pdev: PCI device information struct
13333  * @ent: entry in i40e_pci_tbl
13334  *
13335  * i40e_probe initializes a PF identified by a pci_dev structure.
13336  * The OS initialization, configuring of the PF private structure,
13337  * and a hardware reset occur.
13338  *
13339  * Returns 0 on success, negative on failure
13340  **/
13341 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
13342 {
13343 	struct i40e_aq_get_phy_abilities_resp abilities;
13344 	struct i40e_pf *pf;
13345 	struct i40e_hw *hw;
13346 	static u16 pfs_found;
13347 	u16 wol_nvm_bits;
13348 	u16 link_status;
13349 	int err;
13350 	u32 val;
13351 	u32 i;
13352 	u8 set_fc_aq_fail;
13353 
13354 	err = pci_enable_device_mem(pdev);
13355 	if (err)
13356 		return err;
13357 
13358 	/* set up for high or low dma */
13359 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
13360 	if (err) {
13361 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
13362 		if (err) {
13363 			dev_err(&pdev->dev,
13364 				"DMA configuration failed: 0x%x\n", err);
13365 			goto err_dma;
13366 		}
13367 	}
13368 
13369 	/* set up pci connections */
13370 	err = pci_request_mem_regions(pdev, i40e_driver_name);
13371 	if (err) {
13372 		dev_info(&pdev->dev,
13373 			 "pci_request_selected_regions failed %d\n", err);
13374 		goto err_pci_reg;
13375 	}
13376 
13377 	pci_enable_pcie_error_reporting(pdev);
13378 	pci_set_master(pdev);
13379 
13380 	/* Now that we have a PCI connection, we need to do the
13381 	 * low level device setup.  This is primarily setting up
13382 	 * the Admin Queue structures and then querying for the
13383 	 * device's current profile information.
13384 	 */
13385 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
13386 	if (!pf) {
13387 		err = -ENOMEM;
13388 		goto err_pf_alloc;
13389 	}
13390 	pf->next_vsi = 0;
13391 	pf->pdev = pdev;
13392 	set_bit(__I40E_DOWN, pf->state);
13393 
13394 	hw = &pf->hw;
13395 	hw->back = pf;
13396 
13397 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
13398 				I40E_MAX_CSR_SPACE);
13399 
13400 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
13401 	if (!hw->hw_addr) {
13402 		err = -EIO;
13403 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
13404 			 (unsigned int)pci_resource_start(pdev, 0),
13405 			 pf->ioremap_len, err);
13406 		goto err_ioremap;
13407 	}
13408 	hw->vendor_id = pdev->vendor;
13409 	hw->device_id = pdev->device;
13410 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
13411 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
13412 	hw->subsystem_device_id = pdev->subsystem_device;
13413 	hw->bus.device = PCI_SLOT(pdev->devfn);
13414 	hw->bus.func = PCI_FUNC(pdev->devfn);
13415 	hw->bus.bus_id = pdev->bus->number;
13416 	pf->instance = pfs_found;
13417 
13418 	/* Select something other than the 802.1ad ethertype for the
13419 	 * switch to use internally and drop on ingress.
13420 	 */
13421 	hw->switch_tag = 0xffff;
13422 	hw->first_tag = ETH_P_8021AD;
13423 	hw->second_tag = ETH_P_8021Q;
13424 
13425 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
13426 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
13427 
13428 	/* set up the locks for the AQ, do this only once in probe
13429 	 * and destroy them only once in remove
13430 	 */
13431 	mutex_init(&hw->aq.asq_mutex);
13432 	mutex_init(&hw->aq.arq_mutex);
13433 
13434 	pf->msg_enable = netif_msg_init(debug,
13435 					NETIF_MSG_DRV |
13436 					NETIF_MSG_PROBE |
13437 					NETIF_MSG_LINK);
13438 	if (debug < -1)
13439 		pf->hw.debug_mask = debug;
13440 
13441 	/* do a special CORER for clearing PXE mode once at init */
13442 	if (hw->revision_id == 0 &&
13443 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
13444 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
13445 		i40e_flush(hw);
13446 		msleep(200);
13447 		pf->corer_count++;
13448 
13449 		i40e_clear_pxe_mode(hw);
13450 	}
13451 
13452 	/* Reset here to make sure all is clean and to define PF 'n' */
13453 	i40e_clear_hw(hw);
13454 	err = i40e_pf_reset(hw);
13455 	if (err) {
13456 		dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
13457 		goto err_pf_reset;
13458 	}
13459 	pf->pfr_count++;
13460 
13461 	hw->aq.num_arq_entries = I40E_AQ_LEN;
13462 	hw->aq.num_asq_entries = I40E_AQ_LEN;
13463 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13464 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13465 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
13466 
13467 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
13468 		 "%s-%s:misc",
13469 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
13470 
13471 	err = i40e_init_shared_code(hw);
13472 	if (err) {
13473 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
13474 			 err);
13475 		goto err_pf_reset;
13476 	}
13477 
13478 	/* set up a default setting for link flow control */
13479 	pf->hw.fc.requested_mode = I40E_FC_NONE;
13480 
13481 	err = i40e_init_adminq(hw);
13482 	if (err) {
13483 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
13484 			dev_info(&pdev->dev,
13485 				 "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
13486 		else
13487 			dev_info(&pdev->dev,
13488 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
13489 
13490 		goto err_pf_reset;
13491 	}
13492 	i40e_get_oem_version(hw);
13493 
13494 	/* provide nvm, fw, api versions */
13495 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
13496 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
13497 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
13498 		 i40e_nvm_version_str(hw));
13499 
13500 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
13501 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
13502 		dev_info(&pdev->dev,
13503 			 "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
13504 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
13505 		dev_info(&pdev->dev,
13506 			 "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
13507 
13508 	i40e_verify_eeprom(pf);
13509 
13510 	/* Rev 0 hardware was never productized */
13511 	if (hw->revision_id < 1)
13512 		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");
13513 
13514 	i40e_clear_pxe_mode(hw);
13515 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
13516 	if (err)
13517 		goto err_adminq_setup;
13518 
13519 	err = i40e_sw_init(pf);
13520 	if (err) {
13521 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
13522 		goto err_sw_init;
13523 	}
13524 
13525 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
13526 				hw->func_caps.num_rx_qp, 0, 0);
13527 	if (err) {
13528 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
13529 		goto err_init_lan_hmc;
13530 	}
13531 
13532 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
13533 	if (err) {
13534 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
13535 		err = -ENOENT;
13536 		goto err_configure_lan_hmc;
13537 	}
13538 
13539 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
13540 	 * Ignore error return codes because if it was already disabled via
13541 	 * hardware settings this will fail
13542 	 */
13543 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
13544 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
13545 		i40e_aq_stop_lldp(hw, true, NULL);
13546 	}
13547 
13548 	/* allow a platform config to override the HW addr */
13549 	i40e_get_platform_mac_addr(pdev, pf);
13550 
13551 	if (!is_valid_ether_addr(hw->mac.addr)) {
13552 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
13553 		err = -EIO;
13554 		goto err_mac_addr;
13555 	}
13556 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
13557 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
13558 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
13559 	if (is_valid_ether_addr(hw->mac.port_addr))
13560 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
13561 
13562 	pci_set_drvdata(pdev, pf);
13563 	pci_save_state(pdev);
13564 
13565 	/* Enable FW to write default DCB config on link-up */
13566 	i40e_aq_set_dcb_parameters(hw, true, NULL);
13567 
13568 #ifdef CONFIG_I40E_DCB
13569 	err = i40e_init_pf_dcb(pf);
13570 	if (err) {
13571 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
13572 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
13573 		/* Continue without DCB enabled */
13574 	}
13575 #endif /* CONFIG_I40E_DCB */
13576 
13577 	/* set up periodic task facility */
13578 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
13579 	pf->service_timer_period = HZ;
13580 
13581 	INIT_WORK(&pf->service_task, i40e_service_task);
13582 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
13583 
13584 	/* NVM bit on means WoL disabled for the port */
13585 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
13586 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
13587 		pf->wol_en = false;
13588 	else
13589 		pf->wol_en = true;
13590 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
13591 
13592 	/* set up the main switch operations */
13593 	i40e_determine_queue_usage(pf);
13594 	err = i40e_init_interrupt_scheme(pf);
13595 	if (err)
13596 		goto err_switch_setup;
13597 
13598 	/* The number of VSIs reported by the FW is the minimum guaranteed
13599 	 * to us; HW supports far more and we share the remaining pool with
13600 	 * the other PFs. We allocate space for more than the guarantee with
13601 	 * the understanding that we might not get them all later.
13602 	 */
13603 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
13604 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
13605 	else
13606 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
13607 
13608 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
13609 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
13610 			  GFP_KERNEL);
13611 	if (!pf->vsi) {
13612 		err = -ENOMEM;
13613 		goto err_switch_setup;
13614 	}
13615 
13616 #ifdef CONFIG_PCI_IOV
13617 	/* prep for VF support */
13618 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13619 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
13620 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
13621 		if (pci_num_vf(pdev))
13622 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13623 	}
13624 #endif
13625 	err = i40e_setup_pf_switch(pf, false);
13626 	if (err) {
13627 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
13628 		goto err_vsis;
13629 	}
13630 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
13631 
13632 	/* Make sure flow control is set according to current settings */
13633 	err = i40e_set_fc(hw, &set_fc_aq_fail, true);
13634 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
13635 		dev_dbg(&pf->pdev->dev,
13636 			"Set fc with err %s aq_err %s on get_phy_cap\n",
13637 			i40e_stat_str(hw, err),
13638 			i40e_aq_str(hw, hw->aq.asq_last_status));
13639 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
13640 		dev_dbg(&pf->pdev->dev,
13641 			"Set fc with err %s aq_err %s on set_phy_config\n",
13642 			i40e_stat_str(hw, err),
13643 			i40e_aq_str(hw, hw->aq.asq_last_status));
13644 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
13645 		dev_dbg(&pf->pdev->dev,
13646 			"Set fc with err %s aq_err %s on get_link_info\n",
13647 			i40e_stat_str(hw, err),
13648 			i40e_aq_str(hw, hw->aq.asq_last_status));
13649 
13650 	/* if FDIR VSI was set up, start it now */
13651 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13652 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
13653 			i40e_vsi_open(pf->vsi[i]);
13654 			break;
13655 		}
13656 	}
13657 
13658 	/* The driver only wants link up/down and module qualification
13659 	 * reports from firmware.  Note the negative logic.
13660 	 */
13661 	err = i40e_aq_set_phy_int_mask(&pf->hw,
13662 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
13663 					 I40E_AQ_EVENT_MEDIA_NA |
13664 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
13665 	if (err)
13666 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
13667 			 i40e_stat_str(&pf->hw, err),
13668 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13669 
13670 	/* Reconfigure hardware for allowing smaller MSS in the case
13671 	 * of TSO, so that we avoid the MDD being fired and causing
13672 	 * a reset in the case of small MSS+TSO.
13673 	 */
13674 	val = rd32(hw, I40E_REG_MSS);
13675 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
13676 		val &= ~I40E_REG_MSS_MIN_MASK;
13677 		val |= I40E_64BYTE_MSS;
13678 		wr32(hw, I40E_REG_MSS, val);
13679 	}
13680 
13681 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
13682 		msleep(75);
13683 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
13684 		if (err)
13685 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
13686 				 i40e_stat_str(&pf->hw, err),
13687 				 i40e_aq_str(&pf->hw,
13688 					     pf->hw.aq.asq_last_status));
13689 	}
13690 	/* The main driver is (mostly) up and happy. We need to set this state
13691 	 * before setting up the misc vector or we get a race and the vector
13692 	 * ends up disabled forever.
13693 	 */
13694 	clear_bit(__I40E_DOWN, pf->state);
13695 
13696 	/* In case of MSIX we are going to setup the misc vector right here
13697 	 * to handle admin queue events etc. In case of legacy and MSI
13698 	 * the misc functionality and queue processing is combined in
13699 	 * the same vector and that gets setup at open.
13700 	 */
13701 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13702 		err = i40e_setup_misc_vector(pf);
13703 		if (err) {
13704 			dev_info(&pdev->dev,
13705 				 "setup of misc vector failed: %d\n", err);
13706 			goto err_vsis;
13707 		}
13708 	}
13709 
13710 #ifdef CONFIG_PCI_IOV
13711 	/* prep for VF support */
13712 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13713 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
13714 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
13715 		/* disable link interrupts for VFs */
13716 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
13717 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
13718 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
13719 		i40e_flush(hw);
13720 
13721 		if (pci_num_vf(pdev)) {
13722 			dev_info(&pdev->dev,
13723 				 "Active VFs found, allocating resources.\n");
13724 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
13725 			if (err)
13726 				dev_info(&pdev->dev,
13727 					 "Error %d allocating resources for existing VFs\n",
13728 					 err);
13729 		}
13730 	}
13731 #endif /* CONFIG_PCI_IOV */
13732 
13733 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13734 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
13735 						      pf->num_iwarp_msix,
13736 						      I40E_IWARP_IRQ_PILE_ID);
13737 		if (pf->iwarp_base_vector < 0) {
13738 			dev_info(&pdev->dev,
13739 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
13740 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
13741 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
13742 		}
13743 	}
13744 
13745 	i40e_dbg_pf_init(pf);
13746 
13747 	/* tell the firmware that we're starting */
13748 	i40e_send_version(pf);
13749 
13750 	/* since everything's happy, start the service_task timer */
13751 	mod_timer(&pf->service_timer,
13752 		  round_jiffies(jiffies + pf->service_timer_period));
13753 
13754 	/* add this PF to client device list and launch a client service task */
13755 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13756 		err = i40e_lan_add_device(pf);
13757 		if (err)
13758 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
13759 				 err);
13760 	}
13761 
13762 #define PCI_SPEED_SIZE 8
13763 #define PCI_WIDTH_SIZE 8
13764 	/* Devices on the IOSF bus do not have this information
13765 	 * and will report PCI Gen 1 x 1 by default so don't bother
13766 	 * checking them.
13767 	 */
13768 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
13769 		char speed[PCI_SPEED_SIZE] = "Unknown";
13770 		char width[PCI_WIDTH_SIZE] = "Unknown";
13771 
13772 		/* Get the negotiated link width and speed from PCI config
13773 		 * space
13774 		 */
13775 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
13776 					  &link_status);
13777 
13778 		i40e_set_pci_config_data(hw, link_status);
13779 
13780 		switch (hw->bus.speed) {
13781 		case i40e_bus_speed_8000:
13782 			strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
13783 		case i40e_bus_speed_5000:
13784 			strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
13785 		case i40e_bus_speed_2500:
13786 			strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
13787 		default:
13788 			break;
13789 		}
13790 		switch (hw->bus.width) {
13791 		case i40e_bus_width_pcie_x8:
13792 			strncpy(width, "8", PCI_WIDTH_SIZE); break;
13793 		case i40e_bus_width_pcie_x4:
13794 			strncpy(width, "4", PCI_WIDTH_SIZE); break;
13795 		case i40e_bus_width_pcie_x2:
13796 			strncpy(width, "2", PCI_WIDTH_SIZE); break;
13797 		case i40e_bus_width_pcie_x1:
13798 			strncpy(width, "1", PCI_WIDTH_SIZE); break;
13799 		default:
13800 			break;
13801 		}
13802 
13803 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
13804 			 speed, width);
13805 
13806 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
13807 		    hw->bus.speed < i40e_bus_speed_8000) {
13808 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
13809 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
13810 		}
13811 	}
13812 
13813 	/* get the requested speeds from the fw */
13814 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
13815 	if (err)
13816 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
13817 			i40e_stat_str(&pf->hw, err),
13818 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13819 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
13820 
13821 	/* get the supported phy types from the fw */
13822 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
13823 	if (err)
13824 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
13825 			i40e_stat_str(&pf->hw, err),
13826 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13827 
13828 	/* Add a filter to drop all Flow control frames from any VSI from being
13829 	 * transmitted. By doing so we stop a malicious VF from sending out
13830 	 * PAUSE or PFC frames and potentially controlling traffic for other
13831 	 * PF/VF VSIs.
13832 	 * The FW can still send Flow control frames if enabled.
13833 	 */
13834 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
13835 						       pf->main_vsi_seid);
13836 
13837 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
13838 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
13839 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
13840 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
13841 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
13842 	/* print a string summarizing features */
13843 	i40e_print_features(pf);
13844 
13845 	return 0;
13846 
13847 	/* Unwind what we've done if something failed in the setup */
13848 err_vsis:
13849 	set_bit(__I40E_DOWN, pf->state);
13850 	i40e_clear_interrupt_scheme(pf);
13851 	kfree(pf->vsi);
13852 err_switch_setup:
13853 	i40e_reset_interrupt_capability(pf);
13854 	del_timer_sync(&pf->service_timer);
13855 err_mac_addr:
13856 err_configure_lan_hmc:
13857 	(void)i40e_shutdown_lan_hmc(hw);
13858 err_init_lan_hmc:
13859 	kfree(pf->qp_pile);
13860 err_sw_init:
13861 err_adminq_setup:
13862 err_pf_reset:
13863 	iounmap(hw->hw_addr);
13864 err_ioremap:
13865 	kfree(pf);
13866 err_pf_alloc:
13867 	pci_disable_pcie_error_reporting(pdev);
13868 	pci_release_mem_regions(pdev);
13869 err_pci_reg:
13870 err_dma:
13871 	pci_disable_device(pdev);
13872 	return err;
13873 }
13874 
13875 /**
13876  * i40e_remove - Device removal routine
13877  * @pdev: PCI device information struct
13878  *
13879  * i40e_remove is called by the PCI subsystem to alert the driver
13880  * that is should release a PCI device.  This could be caused by a
13881  * Hot-Plug event, or because the driver is going to be removed from
13882  * memory.
13883  **/
13884 static void i40e_remove(struct pci_dev *pdev)
13885 {
13886 	struct i40e_pf *pf = pci_get_drvdata(pdev);
13887 	struct i40e_hw *hw = &pf->hw;
13888 	i40e_status ret_code;
13889 	int i;
13890 
13891 	i40e_dbg_pf_exit(pf);
13892 
13893 	i40e_ptp_stop(pf);
13894 
13895 	/* Disable RSS in hw */
13896 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
13897 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
13898 
13899 	/* no more scheduling of any task */
13900 	set_bit(__I40E_SUSPENDED, pf->state);
13901 	set_bit(__I40E_DOWN, pf->state);
13902 	if (pf->service_timer.function)
13903 		del_timer_sync(&pf->service_timer);
13904 	if (pf->service_task.func)
13905 		cancel_work_sync(&pf->service_task);
13906 
13907 	/* Client close must be called explicitly here because the timer
13908 	 * has been stopped.
13909 	 */
13910 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
13911 
13912 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
13913 		i40e_free_vfs(pf);
13914 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
13915 	}
13916 
13917 	i40e_fdir_teardown(pf);
13918 
13919 	/* If there is a switch structure or any orphans, remove them.
13920 	 * This will leave only the PF's VSI remaining.
13921 	 */
13922 	for (i = 0; i < I40E_MAX_VEB; i++) {
13923 		if (!pf->veb[i])
13924 			continue;
13925 
13926 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
13927 		    pf->veb[i]->uplink_seid == 0)
13928 			i40e_switch_branch_release(pf->veb[i]);
13929 	}
13930 
13931 	/* Now we can shutdown the PF's VSI, just before we kill
13932 	 * adminq and hmc.
13933 	 */
13934 	if (pf->vsi[pf->lan_vsi])
13935 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
13936 
13937 	i40e_cloud_filter_exit(pf);
13938 
13939 	/* remove attached clients */
13940 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13941 		ret_code = i40e_lan_del_device(pf);
13942 		if (ret_code)
13943 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
13944 				 ret_code);
13945 	}
13946 
13947 	/* shutdown and destroy the HMC */
13948 	if (hw->hmc.hmc_obj) {
13949 		ret_code = i40e_shutdown_lan_hmc(hw);
13950 		if (ret_code)
13951 			dev_warn(&pdev->dev,
13952 				 "Failed to destroy the HMC resources: %d\n",
13953 				 ret_code);
13954 	}
13955 
13956 	/* shutdown the adminq */
13957 	i40e_shutdown_adminq(hw);
13958 
13959 	/* destroy the locks only once, here */
13960 	mutex_destroy(&hw->aq.arq_mutex);
13961 	mutex_destroy(&hw->aq.asq_mutex);
13962 
13963 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
13964 	i40e_clear_interrupt_scheme(pf);
13965 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13966 		if (pf->vsi[i]) {
13967 			i40e_vsi_clear_rings(pf->vsi[i]);
13968 			i40e_vsi_clear(pf->vsi[i]);
13969 			pf->vsi[i] = NULL;
13970 		}
13971 	}
13972 
13973 	for (i = 0; i < I40E_MAX_VEB; i++) {
13974 		kfree(pf->veb[i]);
13975 		pf->veb[i] = NULL;
13976 	}
13977 
13978 	kfree(pf->qp_pile);
13979 	kfree(pf->vsi);
13980 
13981 	iounmap(hw->hw_addr);
13982 	kfree(pf);
13983 	pci_release_mem_regions(pdev);
13984 
13985 	pci_disable_pcie_error_reporting(pdev);
13986 	pci_disable_device(pdev);
13987 }
13988 
13989 /**
13990  * i40e_pci_error_detected - warning that something funky happened in PCI land
13991  * @pdev: PCI device information struct
13992  *
13993  * Called to warn that something happened and the error handling steps
13994  * are in progress.  Allows the driver to quiesce things, be ready for
13995  * remediation.
13996  **/
13997 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
13998 						enum pci_channel_state error)
13999 {
14000 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14001 
14002 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
14003 
14004 	if (!pf) {
14005 		dev_info(&pdev->dev,
14006 			 "Cannot recover - error happened during device probe\n");
14007 		return PCI_ERS_RESULT_DISCONNECT;
14008 	}
14009 
14010 	/* shutdown all operations */
14011 	if (!test_bit(__I40E_SUSPENDED, pf->state))
14012 		i40e_prep_for_reset(pf, false);
14013 
14014 	/* Request a slot reset */
14015 	return PCI_ERS_RESULT_NEED_RESET;
14016 }
14017 
14018 /**
14019  * i40e_pci_error_slot_reset - a PCI slot reset just happened
14020  * @pdev: PCI device information struct
14021  *
14022  * Called to find if the driver can work with the device now that
14023  * the pci slot has been reset.  If a basic connection seems good
14024  * (registers are readable and have sane content) then return a
14025  * happy little PCI_ERS_RESULT_xxx.
14026  **/
14027 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
14028 {
14029 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14030 	pci_ers_result_t result;
14031 	int err;
14032 	u32 reg;
14033 
14034 	dev_dbg(&pdev->dev, "%s\n", __func__);
14035 	if (pci_enable_device_mem(pdev)) {
14036 		dev_info(&pdev->dev,
14037 			 "Cannot re-enable PCI device after reset.\n");
14038 		result = PCI_ERS_RESULT_DISCONNECT;
14039 	} else {
14040 		pci_set_master(pdev);
14041 		pci_restore_state(pdev);
14042 		pci_save_state(pdev);
14043 		pci_wake_from_d3(pdev, false);
14044 
14045 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
14046 		if (reg == 0)
14047 			result = PCI_ERS_RESULT_RECOVERED;
14048 		else
14049 			result = PCI_ERS_RESULT_DISCONNECT;
14050 	}
14051 
14052 	err = pci_cleanup_aer_uncorrect_error_status(pdev);
14053 	if (err) {
14054 		dev_info(&pdev->dev,
14055 			 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
14056 			 err);
14057 		/* non-fatal, continue */
14058 	}
14059 
14060 	return result;
14061 }
14062 
14063 /**
14064  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
14065  * @pdev: PCI device information struct
14066  */
14067 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
14068 {
14069 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14070 
14071 	i40e_prep_for_reset(pf, false);
14072 }
14073 
14074 /**
14075  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
14076  * @pdev: PCI device information struct
14077  */
14078 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
14079 {
14080 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14081 
14082 	i40e_reset_and_rebuild(pf, false, false);
14083 }
14084 
14085 /**
14086  * i40e_pci_error_resume - restart operations after PCI error recovery
14087  * @pdev: PCI device information struct
14088  *
14089  * Called to allow the driver to bring things back up after PCI error
14090  * and/or reset recovery has finished.
14091  **/
14092 static void i40e_pci_error_resume(struct pci_dev *pdev)
14093 {
14094 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14095 
14096 	dev_dbg(&pdev->dev, "%s\n", __func__);
14097 	if (test_bit(__I40E_SUSPENDED, pf->state))
14098 		return;
14099 
14100 	i40e_handle_reset_warning(pf, false);
14101 }
14102 
14103 /**
14104  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
14105  * using the mac_address_write admin q function
14106  * @pf: pointer to i40e_pf struct
14107  **/
14108 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
14109 {
14110 	struct i40e_hw *hw = &pf->hw;
14111 	i40e_status ret;
14112 	u8 mac_addr[6];
14113 	u16 flags = 0;
14114 
14115 	/* Get current MAC address in case it's an LAA */
14116 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
14117 		ether_addr_copy(mac_addr,
14118 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
14119 	} else {
14120 		dev_err(&pf->pdev->dev,
14121 			"Failed to retrieve MAC address; using default\n");
14122 		ether_addr_copy(mac_addr, hw->mac.addr);
14123 	}
14124 
14125 	/* The FW expects the mac address write cmd to first be called with
14126 	 * one of these flags before calling it again with the multicast
14127 	 * enable flags.
14128 	 */
14129 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
14130 
14131 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
14132 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
14133 
14134 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14135 	if (ret) {
14136 		dev_err(&pf->pdev->dev,
14137 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
14138 		return;
14139 	}
14140 
14141 	flags = I40E_AQC_MC_MAG_EN
14142 			| I40E_AQC_WOL_PRESERVE_ON_PFR
14143 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
14144 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14145 	if (ret)
14146 		dev_err(&pf->pdev->dev,
14147 			"Failed to enable Multicast Magic Packet wake up\n");
14148 }
14149 
14150 /**
14151  * i40e_shutdown - PCI callback for shutting down
14152  * @pdev: PCI device information struct
14153  **/
14154 static void i40e_shutdown(struct pci_dev *pdev)
14155 {
14156 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14157 	struct i40e_hw *hw = &pf->hw;
14158 
14159 	set_bit(__I40E_SUSPENDED, pf->state);
14160 	set_bit(__I40E_DOWN, pf->state);
14161 	rtnl_lock();
14162 	i40e_prep_for_reset(pf, true);
14163 	rtnl_unlock();
14164 
14165 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14166 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14167 
14168 	del_timer_sync(&pf->service_timer);
14169 	cancel_work_sync(&pf->service_task);
14170 	i40e_cloud_filter_exit(pf);
14171 	i40e_fdir_teardown(pf);
14172 
14173 	/* Client close must be called explicitly here because the timer
14174 	 * has been stopped.
14175 	 */
14176 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14177 
14178 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14179 		i40e_enable_mc_magic_wake(pf);
14180 
14181 	i40e_prep_for_reset(pf, false);
14182 
14183 	wr32(hw, I40E_PFPM_APM,
14184 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14185 	wr32(hw, I40E_PFPM_WUFC,
14186 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14187 
14188 	i40e_clear_interrupt_scheme(pf);
14189 
14190 	if (system_state == SYSTEM_POWER_OFF) {
14191 		pci_wake_from_d3(pdev, pf->wol_en);
14192 		pci_set_power_state(pdev, PCI_D3hot);
14193 	}
14194 }
14195 
14196 /**
14197  * i40e_suspend - PM callback for moving to D3
14198  * @dev: generic device information structure
14199  **/
14200 static int __maybe_unused i40e_suspend(struct device *dev)
14201 {
14202 	struct pci_dev *pdev = to_pci_dev(dev);
14203 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14204 	struct i40e_hw *hw = &pf->hw;
14205 
14206 	/* If we're already suspended, then there is nothing to do */
14207 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
14208 		return 0;
14209 
14210 	set_bit(__I40E_DOWN, pf->state);
14211 
14212 	/* Ensure service task will not be running */
14213 	del_timer_sync(&pf->service_timer);
14214 	cancel_work_sync(&pf->service_task);
14215 
14216 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14217 		i40e_enable_mc_magic_wake(pf);
14218 
14219 	i40e_prep_for_reset(pf, false);
14220 
14221 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14222 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14223 
14224 	/* Clear the interrupt scheme and release our IRQs so that the system
14225 	 * can safely hibernate even when there are a large number of CPUs.
14226 	 * Otherwise hibernation might fail when mapping all the vectors back
14227 	 * to CPU0.
14228 	 */
14229 	i40e_clear_interrupt_scheme(pf);
14230 
14231 	return 0;
14232 }
14233 
14234 /**
14235  * i40e_resume - PM callback for waking up from D3
14236  * @dev: generic device information structure
14237  **/
14238 static int __maybe_unused i40e_resume(struct device *dev)
14239 {
14240 	struct pci_dev *pdev = to_pci_dev(dev);
14241 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14242 	int err;
14243 
14244 	/* If we're not suspended, then there is nothing to do */
14245 	if (!test_bit(__I40E_SUSPENDED, pf->state))
14246 		return 0;
14247 
14248 	/* We cleared the interrupt scheme when we suspended, so we need to
14249 	 * restore it now to resume device functionality.
14250 	 */
14251 	err = i40e_restore_interrupt_scheme(pf);
14252 	if (err) {
14253 		dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
14254 			err);
14255 	}
14256 
14257 	clear_bit(__I40E_DOWN, pf->state);
14258 	i40e_reset_and_rebuild(pf, false, false);
14259 
14260 	/* Clear suspended state last after everything is recovered */
14261 	clear_bit(__I40E_SUSPENDED, pf->state);
14262 
14263 	/* Restart the service task */
14264 	mod_timer(&pf->service_timer,
14265 		  round_jiffies(jiffies + pf->service_timer_period));
14266 
14267 	return 0;
14268 }
14269 
14270 static const struct pci_error_handlers i40e_err_handler = {
14271 	.error_detected = i40e_pci_error_detected,
14272 	.slot_reset = i40e_pci_error_slot_reset,
14273 	.reset_prepare = i40e_pci_error_reset_prepare,
14274 	.reset_done = i40e_pci_error_reset_done,
14275 	.resume = i40e_pci_error_resume,
14276 };
14277 
14278 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
14279 
14280 static struct pci_driver i40e_driver = {
14281 	.name     = i40e_driver_name,
14282 	.id_table = i40e_pci_tbl,
14283 	.probe    = i40e_probe,
14284 	.remove   = i40e_remove,
14285 	.driver   = {
14286 		.pm = &i40e_pm_ops,
14287 	},
14288 	.shutdown = i40e_shutdown,
14289 	.err_handler = &i40e_err_handler,
14290 	.sriov_configure = i40e_pci_sriov_configure,
14291 };
14292 
14293 /**
14294  * i40e_init_module - Driver registration routine
14295  *
14296  * i40e_init_module is the first routine called when the driver is
14297  * loaded. All it does is register with the PCI subsystem.
14298  **/
14299 static int __init i40e_init_module(void)
14300 {
14301 	pr_info("%s: %s - version %s\n", i40e_driver_name,
14302 		i40e_driver_string, i40e_driver_version_str);
14303 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
14304 
14305 	/* There is no need to throttle the number of active tasks because
14306 	 * each device limits its own task using a state bit for scheduling
14307 	 * the service task, and the device tasks do not interfere with each
14308 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
14309 	 * since we need to be able to guarantee forward progress even under
14310 	 * memory pressure.
14311 	 */
14312 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
14313 	if (!i40e_wq) {
14314 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
14315 		return -ENOMEM;
14316 	}
14317 
14318 	i40e_dbg_init();
14319 	return pci_register_driver(&i40e_driver);
14320 }
14321 module_init(i40e_init_module);
14322 
14323 /**
14324  * i40e_exit_module - Driver exit cleanup routine
14325  *
14326  * i40e_exit_module is called just before the driver is removed
14327  * from memory.
14328  **/
14329 static void __exit i40e_exit_module(void)
14330 {
14331 	pci_unregister_driver(&i40e_driver);
14332 	destroy_workqueue(i40e_wq);
14333 	i40e_dbg_exit();
14334 }
14335 module_exit(i40e_exit_module);
14336