• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 
10 /* Local includes */
11 #include "i40e.h"
12 #include "i40e_diag.h"
13 #include "i40e_xsk.h"
14 #include <net/udp_tunnel.h>
15 #include <net/xdp_sock_drv.h>
16 /* All i40e tracepoints are defined by the include below, which
17  * must be included exactly once across the whole kernel with
18  * CREATE_TRACE_POINTS defined
19  */
20 #define CREATE_TRACE_POINTS
21 #include "i40e_trace.h"
22 
23 const char i40e_driver_name[] = "i40e";
24 static const char i40e_driver_string[] =
25 			"Intel(R) Ethernet Connection XL710 Network Driver";
26 
27 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
28 
29 /* a bit of forward declarations */
30 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
31 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
32 static int i40e_add_vsi(struct i40e_vsi *vsi);
33 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
34 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
35 static int i40e_setup_misc_vector(struct i40e_pf *pf);
36 static void i40e_determine_queue_usage(struct i40e_pf *pf);
37 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
38 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
39 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
40 				   bool lock_acquired);
41 static int i40e_reset(struct i40e_pf *pf);
42 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
43 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
44 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
45 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
46 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
47 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
48 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
49 static int i40e_get_capabilities(struct i40e_pf *pf,
50 				 enum i40e_admin_queue_opc list_type);
51 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
52 
53 /* i40e_pci_tbl - PCI Device ID Table
54  *
55  * Last entry must be all 0s
56  *
57  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
58  *   Class, Class Mask, private data (not used) }
59  */
60 static const struct pci_device_id i40e_pci_tbl[] = {
61 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
85 	/* required last entry */
86 	{0, }
87 };
88 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
89 
90 #define I40E_MAX_VF_COUNT 128
91 static int debug = -1;
92 module_param(debug, uint, 0);
93 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
94 
95 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
96 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
97 MODULE_LICENSE("GPL v2");
98 
99 static struct workqueue_struct *i40e_wq;
100 
101 /**
102  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
103  * @hw:   pointer to the HW structure
104  * @mem:  ptr to mem struct to fill out
105  * @size: size of memory requested
106  * @alignment: what to align the allocation to
107  **/
i40e_allocate_dma_mem_d(struct i40e_hw * hw,struct i40e_dma_mem * mem,u64 size,u32 alignment)108 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
109 			    u64 size, u32 alignment)
110 {
111 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
112 
113 	mem->size = ALIGN(size, alignment);
114 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
115 				     GFP_KERNEL);
116 	if (!mem->va)
117 		return -ENOMEM;
118 
119 	return 0;
120 }
121 
122 /**
123  * i40e_free_dma_mem_d - OS specific memory free for shared code
124  * @hw:   pointer to the HW structure
125  * @mem:  ptr to mem struct to free
126  **/
i40e_free_dma_mem_d(struct i40e_hw * hw,struct i40e_dma_mem * mem)127 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
128 {
129 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
130 
131 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
132 	mem->va = NULL;
133 	mem->pa = 0;
134 	mem->size = 0;
135 
136 	return 0;
137 }
138 
139 /**
140  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
141  * @hw:   pointer to the HW structure
142  * @mem:  ptr to mem struct to fill out
143  * @size: size of memory requested
144  **/
i40e_allocate_virt_mem_d(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)145 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
146 			     u32 size)
147 {
148 	mem->size = size;
149 	mem->va = kzalloc(size, GFP_KERNEL);
150 
151 	if (!mem->va)
152 		return -ENOMEM;
153 
154 	return 0;
155 }
156 
157 /**
158  * i40e_free_virt_mem_d - OS specific memory free for shared code
159  * @hw:   pointer to the HW structure
160  * @mem:  ptr to mem struct to free
161  **/
i40e_free_virt_mem_d(struct i40e_hw * hw,struct i40e_virt_mem * mem)162 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
163 {
164 	/* it's ok to kfree a NULL pointer */
165 	kfree(mem->va);
166 	mem->va = NULL;
167 	mem->size = 0;
168 
169 	return 0;
170 }
171 
172 /**
173  * i40e_get_lump - find a lump of free generic resource
174  * @pf: board private structure
175  * @pile: the pile of resource to search
176  * @needed: the number of items needed
177  * @id: an owner id to stick on the items assigned
178  *
179  * Returns the base item index of the lump, or negative for error
180  *
181  * The search_hint trick and lack of advanced fit-finding only work
182  * because we're highly likely to have all the same size lump requests.
183  * Linear search time and any fragmentation should be minimal.
184  **/
i40e_get_lump(struct i40e_pf * pf,struct i40e_lump_tracking * pile,u16 needed,u16 id)185 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
186 			 u16 needed, u16 id)
187 {
188 	int ret = -ENOMEM;
189 	int i, j;
190 
191 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
192 		dev_info(&pf->pdev->dev,
193 			 "param err: pile=%s needed=%d id=0x%04x\n",
194 			 pile ? "<valid>" : "<null>", needed, id);
195 		return -EINVAL;
196 	}
197 
198 	/* start the linear search with an imperfect hint */
199 	i = pile->search_hint;
200 	while (i < pile->num_entries) {
201 		/* skip already allocated entries */
202 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
203 			i++;
204 			continue;
205 		}
206 
207 		/* do we have enough in this lump? */
208 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
209 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
210 				break;
211 		}
212 
213 		if (j == needed) {
214 			/* there was enough, so assign it to the requestor */
215 			for (j = 0; j < needed; j++)
216 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
217 			ret = i;
218 			pile->search_hint = i + j;
219 			break;
220 		}
221 
222 		/* not enough, so skip over it and continue looking */
223 		i += j;
224 	}
225 
226 	return ret;
227 }
228 
229 /**
230  * i40e_put_lump - return a lump of generic resource
231  * @pile: the pile of resource to search
232  * @index: the base item index
233  * @id: the owner id of the items assigned
234  *
235  * Returns the count of items in the lump
236  **/
i40e_put_lump(struct i40e_lump_tracking * pile,u16 index,u16 id)237 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
238 {
239 	int valid_id = (id | I40E_PILE_VALID_BIT);
240 	int count = 0;
241 	int i;
242 
243 	if (!pile || index >= pile->num_entries)
244 		return -EINVAL;
245 
246 	for (i = index;
247 	     i < pile->num_entries && pile->list[i] == valid_id;
248 	     i++) {
249 		pile->list[i] = 0;
250 		count++;
251 	}
252 
253 	if (count && index < pile->search_hint)
254 		pile->search_hint = index;
255 
256 	return count;
257 }
258 
259 /**
260  * i40e_find_vsi_from_id - searches for the vsi with the given id
261  * @pf: the pf structure to search for the vsi
262  * @id: id of the vsi it is searching for
263  **/
i40e_find_vsi_from_id(struct i40e_pf * pf,u16 id)264 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
265 {
266 	int i;
267 
268 	for (i = 0; i < pf->num_alloc_vsi; i++)
269 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
270 			return pf->vsi[i];
271 
272 	return NULL;
273 }
274 
275 /**
276  * i40e_service_event_schedule - Schedule the service task to wake up
277  * @pf: board private structure
278  *
279  * If not already scheduled, this puts the task into the work queue
280  **/
i40e_service_event_schedule(struct i40e_pf * pf)281 void i40e_service_event_schedule(struct i40e_pf *pf)
282 {
283 	if ((!test_bit(__I40E_DOWN, pf->state) &&
284 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
285 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
286 		queue_work(i40e_wq, &pf->service_task);
287 }
288 
289 /**
290  * i40e_tx_timeout - Respond to a Tx Hang
291  * @netdev: network interface device structure
292  * @txqueue: queue number timing out
293  *
294  * If any port has noticed a Tx timeout, it is likely that the whole
295  * device is munged, not just the one netdev port, so go for the full
296  * reset.
297  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)298 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
299 {
300 	struct i40e_netdev_priv *np = netdev_priv(netdev);
301 	struct i40e_vsi *vsi = np->vsi;
302 	struct i40e_pf *pf = vsi->back;
303 	struct i40e_ring *tx_ring = NULL;
304 	unsigned int i;
305 	u32 head, val;
306 
307 	pf->tx_timeout_count++;
308 
309 	/* with txqueue index, find the tx_ring struct */
310 	for (i = 0; i < vsi->num_queue_pairs; i++) {
311 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
312 			if (txqueue ==
313 			    vsi->tx_rings[i]->queue_index) {
314 				tx_ring = vsi->tx_rings[i];
315 				break;
316 			}
317 		}
318 	}
319 
320 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
321 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
322 	else if (time_before(jiffies,
323 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
324 		return;   /* don't do any new action before the next timeout */
325 
326 	/* don't kick off another recovery if one is already pending */
327 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
328 		return;
329 
330 	if (tx_ring) {
331 		head = i40e_get_head(tx_ring);
332 		/* Read interrupt register */
333 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
334 			val = rd32(&pf->hw,
335 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
336 						tx_ring->vsi->base_vector - 1));
337 		else
338 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
339 
340 		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",
341 			    vsi->seid, txqueue, tx_ring->next_to_clean,
342 			    head, tx_ring->next_to_use,
343 			    readl(tx_ring->tail), val);
344 	}
345 
346 	pf->tx_timeout_last_recovery = jiffies;
347 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
348 		    pf->tx_timeout_recovery_level, txqueue);
349 
350 	switch (pf->tx_timeout_recovery_level) {
351 	case 1:
352 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
353 		break;
354 	case 2:
355 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
356 		break;
357 	case 3:
358 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
359 		break;
360 	default:
361 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
362 		break;
363 	}
364 
365 	i40e_service_event_schedule(pf);
366 	pf->tx_timeout_recovery_level++;
367 }
368 
369 /**
370  * i40e_get_vsi_stats_struct - Get System Network Statistics
371  * @vsi: the VSI we care about
372  *
373  * Returns the address of the device statistics structure.
374  * The statistics are actually updated from the service task.
375  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)376 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
377 {
378 	return &vsi->net_stats;
379 }
380 
381 /**
382  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
383  * @ring: Tx ring to get statistics from
384  * @stats: statistics entry to be updated
385  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)386 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
387 					    struct rtnl_link_stats64 *stats)
388 {
389 	u64 bytes, packets;
390 	unsigned int start;
391 
392 	do {
393 		start = u64_stats_fetch_begin_irq(&ring->syncp);
394 		packets = ring->stats.packets;
395 		bytes   = ring->stats.bytes;
396 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
397 
398 	stats->tx_packets += packets;
399 	stats->tx_bytes   += bytes;
400 }
401 
402 /**
403  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
404  * @netdev: network interface device structure
405  * @stats: data structure to store statistics
406  *
407  * Returns the address of the device statistics structure.
408  * The statistics are actually updated from the service task.
409  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)410 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
411 				  struct rtnl_link_stats64 *stats)
412 {
413 	struct i40e_netdev_priv *np = netdev_priv(netdev);
414 	struct i40e_vsi *vsi = np->vsi;
415 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
416 	struct i40e_ring *ring;
417 	int i;
418 
419 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
420 		return;
421 
422 	if (!vsi->tx_rings)
423 		return;
424 
425 	rcu_read_lock();
426 	for (i = 0; i < vsi->num_queue_pairs; i++) {
427 		u64 bytes, packets;
428 		unsigned int start;
429 
430 		ring = READ_ONCE(vsi->tx_rings[i]);
431 		if (!ring)
432 			continue;
433 		i40e_get_netdev_stats_struct_tx(ring, stats);
434 
435 		if (i40e_enabled_xdp_vsi(vsi)) {
436 			ring = READ_ONCE(vsi->xdp_rings[i]);
437 			if (!ring)
438 				continue;
439 			i40e_get_netdev_stats_struct_tx(ring, stats);
440 		}
441 
442 		ring = READ_ONCE(vsi->rx_rings[i]);
443 		if (!ring)
444 			continue;
445 		do {
446 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
447 			packets = ring->stats.packets;
448 			bytes   = ring->stats.bytes;
449 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
450 
451 		stats->rx_packets += packets;
452 		stats->rx_bytes   += bytes;
453 
454 	}
455 	rcu_read_unlock();
456 
457 	/* following stats updated by i40e_watchdog_subtask() */
458 	stats->multicast	= vsi_stats->multicast;
459 	stats->tx_errors	= vsi_stats->tx_errors;
460 	stats->tx_dropped	= vsi_stats->tx_dropped;
461 	stats->rx_errors	= vsi_stats->rx_errors;
462 	stats->rx_dropped	= vsi_stats->rx_dropped;
463 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
464 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
465 }
466 
467 /**
468  * i40e_vsi_reset_stats - Resets all stats of the given vsi
469  * @vsi: the VSI to have its stats reset
470  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)471 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
472 {
473 	struct rtnl_link_stats64 *ns;
474 	int i;
475 
476 	if (!vsi)
477 		return;
478 
479 	ns = i40e_get_vsi_stats_struct(vsi);
480 	memset(ns, 0, sizeof(*ns));
481 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
482 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
483 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
484 	if (vsi->rx_rings && vsi->rx_rings[0]) {
485 		for (i = 0; i < vsi->num_queue_pairs; i++) {
486 			memset(&vsi->rx_rings[i]->stats, 0,
487 			       sizeof(vsi->rx_rings[i]->stats));
488 			memset(&vsi->rx_rings[i]->rx_stats, 0,
489 			       sizeof(vsi->rx_rings[i]->rx_stats));
490 			memset(&vsi->tx_rings[i]->stats, 0,
491 			       sizeof(vsi->tx_rings[i]->stats));
492 			memset(&vsi->tx_rings[i]->tx_stats, 0,
493 			       sizeof(vsi->tx_rings[i]->tx_stats));
494 		}
495 	}
496 	vsi->stat_offsets_loaded = false;
497 }
498 
499 /**
500  * i40e_pf_reset_stats - Reset all of the stats for the given PF
501  * @pf: the PF to be reset
502  **/
i40e_pf_reset_stats(struct i40e_pf * pf)503 void i40e_pf_reset_stats(struct i40e_pf *pf)
504 {
505 	int i;
506 
507 	memset(&pf->stats, 0, sizeof(pf->stats));
508 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
509 	pf->stat_offsets_loaded = false;
510 
511 	for (i = 0; i < I40E_MAX_VEB; i++) {
512 		if (pf->veb[i]) {
513 			memset(&pf->veb[i]->stats, 0,
514 			       sizeof(pf->veb[i]->stats));
515 			memset(&pf->veb[i]->stats_offsets, 0,
516 			       sizeof(pf->veb[i]->stats_offsets));
517 			memset(&pf->veb[i]->tc_stats, 0,
518 			       sizeof(pf->veb[i]->tc_stats));
519 			memset(&pf->veb[i]->tc_stats_offsets, 0,
520 			       sizeof(pf->veb[i]->tc_stats_offsets));
521 			pf->veb[i]->stat_offsets_loaded = false;
522 		}
523 	}
524 	pf->hw_csum_rx_error = 0;
525 }
526 
527 /**
528  * i40e_stat_update48 - read and update a 48 bit stat from the chip
529  * @hw: ptr to the hardware info
530  * @hireg: the high 32 bit reg to read
531  * @loreg: the low 32 bit reg to read
532  * @offset_loaded: has the initial offset been loaded yet
533  * @offset: ptr to current offset value
534  * @stat: ptr to the stat
535  *
536  * Since the device stats are not reset at PFReset, they likely will not
537  * be zeroed when the driver starts.  We'll save the first values read
538  * and use them as offsets to be subtracted from the raw values in order
539  * to report stats that count from zero.  In the process, we also manage
540  * the potential roll-over.
541  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)542 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
543 			       bool offset_loaded, u64 *offset, u64 *stat)
544 {
545 	u64 new_data;
546 
547 	if (hw->device_id == I40E_DEV_ID_QEMU) {
548 		new_data = rd32(hw, loreg);
549 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
550 	} else {
551 		new_data = rd64(hw, loreg);
552 	}
553 	if (!offset_loaded)
554 		*offset = new_data;
555 	if (likely(new_data >= *offset))
556 		*stat = new_data - *offset;
557 	else
558 		*stat = (new_data + BIT_ULL(48)) - *offset;
559 	*stat &= 0xFFFFFFFFFFFFULL;
560 }
561 
562 /**
563  * i40e_stat_update32 - read and update a 32 bit stat from the chip
564  * @hw: ptr to the hardware info
565  * @reg: the hw reg to read
566  * @offset_loaded: has the initial offset been loaded yet
567  * @offset: ptr to current offset value
568  * @stat: ptr to the stat
569  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)570 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
571 			       bool offset_loaded, u64 *offset, u64 *stat)
572 {
573 	u32 new_data;
574 
575 	new_data = rd32(hw, reg);
576 	if (!offset_loaded)
577 		*offset = new_data;
578 	if (likely(new_data >= *offset))
579 		*stat = (u32)(new_data - *offset);
580 	else
581 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
582 }
583 
584 /**
585  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
586  * @hw: ptr to the hardware info
587  * @reg: the hw reg to read and clear
588  * @stat: ptr to the stat
589  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)590 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
591 {
592 	u32 new_data = rd32(hw, reg);
593 
594 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
595 	*stat += new_data;
596 }
597 
598 /**
599  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
600  * @vsi: the VSI to be updated
601  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)602 void i40e_update_eth_stats(struct i40e_vsi *vsi)
603 {
604 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
605 	struct i40e_pf *pf = vsi->back;
606 	struct i40e_hw *hw = &pf->hw;
607 	struct i40e_eth_stats *oes;
608 	struct i40e_eth_stats *es;     /* device's eth stats */
609 
610 	es = &vsi->eth_stats;
611 	oes = &vsi->eth_stats_offsets;
612 
613 	/* Gather up the stats that the hw collects */
614 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
615 			   vsi->stat_offsets_loaded,
616 			   &oes->tx_errors, &es->tx_errors);
617 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
618 			   vsi->stat_offsets_loaded,
619 			   &oes->rx_discards, &es->rx_discards);
620 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
621 			   vsi->stat_offsets_loaded,
622 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
623 
624 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
625 			   I40E_GLV_GORCL(stat_idx),
626 			   vsi->stat_offsets_loaded,
627 			   &oes->rx_bytes, &es->rx_bytes);
628 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
629 			   I40E_GLV_UPRCL(stat_idx),
630 			   vsi->stat_offsets_loaded,
631 			   &oes->rx_unicast, &es->rx_unicast);
632 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
633 			   I40E_GLV_MPRCL(stat_idx),
634 			   vsi->stat_offsets_loaded,
635 			   &oes->rx_multicast, &es->rx_multicast);
636 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
637 			   I40E_GLV_BPRCL(stat_idx),
638 			   vsi->stat_offsets_loaded,
639 			   &oes->rx_broadcast, &es->rx_broadcast);
640 
641 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
642 			   I40E_GLV_GOTCL(stat_idx),
643 			   vsi->stat_offsets_loaded,
644 			   &oes->tx_bytes, &es->tx_bytes);
645 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
646 			   I40E_GLV_UPTCL(stat_idx),
647 			   vsi->stat_offsets_loaded,
648 			   &oes->tx_unicast, &es->tx_unicast);
649 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
650 			   I40E_GLV_MPTCL(stat_idx),
651 			   vsi->stat_offsets_loaded,
652 			   &oes->tx_multicast, &es->tx_multicast);
653 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
654 			   I40E_GLV_BPTCL(stat_idx),
655 			   vsi->stat_offsets_loaded,
656 			   &oes->tx_broadcast, &es->tx_broadcast);
657 	vsi->stat_offsets_loaded = true;
658 }
659 
660 /**
661  * i40e_update_veb_stats - Update Switch component statistics
662  * @veb: the VEB being updated
663  **/
i40e_update_veb_stats(struct i40e_veb * veb)664 void i40e_update_veb_stats(struct i40e_veb *veb)
665 {
666 	struct i40e_pf *pf = veb->pf;
667 	struct i40e_hw *hw = &pf->hw;
668 	struct i40e_eth_stats *oes;
669 	struct i40e_eth_stats *es;     /* device's eth stats */
670 	struct i40e_veb_tc_stats *veb_oes;
671 	struct i40e_veb_tc_stats *veb_es;
672 	int i, idx = 0;
673 
674 	idx = veb->stats_idx;
675 	es = &veb->stats;
676 	oes = &veb->stats_offsets;
677 	veb_es = &veb->tc_stats;
678 	veb_oes = &veb->tc_stats_offsets;
679 
680 	/* Gather up the stats that the hw collects */
681 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
682 			   veb->stat_offsets_loaded,
683 			   &oes->tx_discards, &es->tx_discards);
684 	if (hw->revision_id > 0)
685 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
686 				   veb->stat_offsets_loaded,
687 				   &oes->rx_unknown_protocol,
688 				   &es->rx_unknown_protocol);
689 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
690 			   veb->stat_offsets_loaded,
691 			   &oes->rx_bytes, &es->rx_bytes);
692 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
693 			   veb->stat_offsets_loaded,
694 			   &oes->rx_unicast, &es->rx_unicast);
695 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
696 			   veb->stat_offsets_loaded,
697 			   &oes->rx_multicast, &es->rx_multicast);
698 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
699 			   veb->stat_offsets_loaded,
700 			   &oes->rx_broadcast, &es->rx_broadcast);
701 
702 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
703 			   veb->stat_offsets_loaded,
704 			   &oes->tx_bytes, &es->tx_bytes);
705 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
706 			   veb->stat_offsets_loaded,
707 			   &oes->tx_unicast, &es->tx_unicast);
708 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
709 			   veb->stat_offsets_loaded,
710 			   &oes->tx_multicast, &es->tx_multicast);
711 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
712 			   veb->stat_offsets_loaded,
713 			   &oes->tx_broadcast, &es->tx_broadcast);
714 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
715 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
716 				   I40E_GLVEBTC_RPCL(i, idx),
717 				   veb->stat_offsets_loaded,
718 				   &veb_oes->tc_rx_packets[i],
719 				   &veb_es->tc_rx_packets[i]);
720 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
721 				   I40E_GLVEBTC_RBCL(i, idx),
722 				   veb->stat_offsets_loaded,
723 				   &veb_oes->tc_rx_bytes[i],
724 				   &veb_es->tc_rx_bytes[i]);
725 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
726 				   I40E_GLVEBTC_TPCL(i, idx),
727 				   veb->stat_offsets_loaded,
728 				   &veb_oes->tc_tx_packets[i],
729 				   &veb_es->tc_tx_packets[i]);
730 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
731 				   I40E_GLVEBTC_TBCL(i, idx),
732 				   veb->stat_offsets_loaded,
733 				   &veb_oes->tc_tx_bytes[i],
734 				   &veb_es->tc_tx_bytes[i]);
735 	}
736 	veb->stat_offsets_loaded = true;
737 }
738 
739 /**
740  * i40e_update_vsi_stats - Update the vsi statistics counters.
741  * @vsi: the VSI to be updated
742  *
743  * There are a few instances where we store the same stat in a
744  * couple of different structs.  This is partly because we have
745  * the netdev stats that need to be filled out, which is slightly
746  * different from the "eth_stats" defined by the chip and used in
747  * VF communications.  We sort it out here.
748  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)749 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
750 {
751 	struct i40e_pf *pf = vsi->back;
752 	struct rtnl_link_stats64 *ons;
753 	struct rtnl_link_stats64 *ns;   /* netdev stats */
754 	struct i40e_eth_stats *oes;
755 	struct i40e_eth_stats *es;     /* device's eth stats */
756 	u32 tx_restart, tx_busy;
757 	struct i40e_ring *p;
758 	u32 rx_page, rx_buf;
759 	u64 bytes, packets;
760 	unsigned int start;
761 	u64 tx_linearize;
762 	u64 tx_force_wb;
763 	u64 rx_p, rx_b;
764 	u64 tx_p, tx_b;
765 	u16 q;
766 
767 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
768 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
769 		return;
770 
771 	ns = i40e_get_vsi_stats_struct(vsi);
772 	ons = &vsi->net_stats_offsets;
773 	es = &vsi->eth_stats;
774 	oes = &vsi->eth_stats_offsets;
775 
776 	/* Gather up the netdev and vsi stats that the driver collects
777 	 * on the fly during packet processing
778 	 */
779 	rx_b = rx_p = 0;
780 	tx_b = tx_p = 0;
781 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
782 	rx_page = 0;
783 	rx_buf = 0;
784 	rcu_read_lock();
785 	for (q = 0; q < vsi->num_queue_pairs; q++) {
786 		/* locate Tx ring */
787 		p = READ_ONCE(vsi->tx_rings[q]);
788 		if (!p)
789 			continue;
790 
791 		do {
792 			start = u64_stats_fetch_begin_irq(&p->syncp);
793 			packets = p->stats.packets;
794 			bytes = p->stats.bytes;
795 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
796 		tx_b += bytes;
797 		tx_p += packets;
798 		tx_restart += p->tx_stats.restart_queue;
799 		tx_busy += p->tx_stats.tx_busy;
800 		tx_linearize += p->tx_stats.tx_linearize;
801 		tx_force_wb += p->tx_stats.tx_force_wb;
802 
803 		/* locate Rx ring */
804 		p = READ_ONCE(vsi->rx_rings[q]);
805 		if (!p)
806 			continue;
807 
808 		do {
809 			start = u64_stats_fetch_begin_irq(&p->syncp);
810 			packets = p->stats.packets;
811 			bytes = p->stats.bytes;
812 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
813 		rx_b += bytes;
814 		rx_p += packets;
815 		rx_buf += p->rx_stats.alloc_buff_failed;
816 		rx_page += p->rx_stats.alloc_page_failed;
817 
818 		if (i40e_enabled_xdp_vsi(vsi)) {
819 			/* locate XDP ring */
820 			p = READ_ONCE(vsi->xdp_rings[q]);
821 			if (!p)
822 				continue;
823 
824 			do {
825 				start = u64_stats_fetch_begin_irq(&p->syncp);
826 				packets = p->stats.packets;
827 				bytes = p->stats.bytes;
828 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
829 			tx_b += bytes;
830 			tx_p += packets;
831 			tx_restart += p->tx_stats.restart_queue;
832 			tx_busy += p->tx_stats.tx_busy;
833 			tx_linearize += p->tx_stats.tx_linearize;
834 			tx_force_wb += p->tx_stats.tx_force_wb;
835 		}
836 	}
837 	rcu_read_unlock();
838 	vsi->tx_restart = tx_restart;
839 	vsi->tx_busy = tx_busy;
840 	vsi->tx_linearize = tx_linearize;
841 	vsi->tx_force_wb = tx_force_wb;
842 	vsi->rx_page_failed = rx_page;
843 	vsi->rx_buf_failed = rx_buf;
844 
845 	ns->rx_packets = rx_p;
846 	ns->rx_bytes = rx_b;
847 	ns->tx_packets = tx_p;
848 	ns->tx_bytes = tx_b;
849 
850 	/* update netdev stats from eth stats */
851 	i40e_update_eth_stats(vsi);
852 	ons->tx_errors = oes->tx_errors;
853 	ns->tx_errors = es->tx_errors;
854 	ons->multicast = oes->rx_multicast;
855 	ns->multicast = es->rx_multicast;
856 	ons->rx_dropped = oes->rx_discards;
857 	ns->rx_dropped = es->rx_discards;
858 	ons->tx_dropped = oes->tx_discards;
859 	ns->tx_dropped = es->tx_discards;
860 
861 	/* pull in a couple PF stats if this is the main vsi */
862 	if (vsi == pf->vsi[pf->lan_vsi]) {
863 		ns->rx_crc_errors = pf->stats.crc_errors;
864 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
865 		ns->rx_length_errors = pf->stats.rx_length_errors;
866 	}
867 }
868 
869 /**
870  * i40e_update_pf_stats - Update the PF statistics counters.
871  * @pf: the PF to be updated
872  **/
i40e_update_pf_stats(struct i40e_pf * pf)873 static void i40e_update_pf_stats(struct i40e_pf *pf)
874 {
875 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
876 	struct i40e_hw_port_stats *nsd = &pf->stats;
877 	struct i40e_hw *hw = &pf->hw;
878 	u32 val;
879 	int i;
880 
881 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
882 			   I40E_GLPRT_GORCL(hw->port),
883 			   pf->stat_offsets_loaded,
884 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
885 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
886 			   I40E_GLPRT_GOTCL(hw->port),
887 			   pf->stat_offsets_loaded,
888 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
889 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
890 			   pf->stat_offsets_loaded,
891 			   &osd->eth.rx_discards,
892 			   &nsd->eth.rx_discards);
893 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
894 			   I40E_GLPRT_UPRCL(hw->port),
895 			   pf->stat_offsets_loaded,
896 			   &osd->eth.rx_unicast,
897 			   &nsd->eth.rx_unicast);
898 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
899 			   I40E_GLPRT_MPRCL(hw->port),
900 			   pf->stat_offsets_loaded,
901 			   &osd->eth.rx_multicast,
902 			   &nsd->eth.rx_multicast);
903 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
904 			   I40E_GLPRT_BPRCL(hw->port),
905 			   pf->stat_offsets_loaded,
906 			   &osd->eth.rx_broadcast,
907 			   &nsd->eth.rx_broadcast);
908 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
909 			   I40E_GLPRT_UPTCL(hw->port),
910 			   pf->stat_offsets_loaded,
911 			   &osd->eth.tx_unicast,
912 			   &nsd->eth.tx_unicast);
913 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
914 			   I40E_GLPRT_MPTCL(hw->port),
915 			   pf->stat_offsets_loaded,
916 			   &osd->eth.tx_multicast,
917 			   &nsd->eth.tx_multicast);
918 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
919 			   I40E_GLPRT_BPTCL(hw->port),
920 			   pf->stat_offsets_loaded,
921 			   &osd->eth.tx_broadcast,
922 			   &nsd->eth.tx_broadcast);
923 
924 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
925 			   pf->stat_offsets_loaded,
926 			   &osd->tx_dropped_link_down,
927 			   &nsd->tx_dropped_link_down);
928 
929 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
930 			   pf->stat_offsets_loaded,
931 			   &osd->crc_errors, &nsd->crc_errors);
932 
933 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
934 			   pf->stat_offsets_loaded,
935 			   &osd->illegal_bytes, &nsd->illegal_bytes);
936 
937 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
938 			   pf->stat_offsets_loaded,
939 			   &osd->mac_local_faults,
940 			   &nsd->mac_local_faults);
941 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
942 			   pf->stat_offsets_loaded,
943 			   &osd->mac_remote_faults,
944 			   &nsd->mac_remote_faults);
945 
946 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->rx_length_errors,
949 			   &nsd->rx_length_errors);
950 
951 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
952 			   pf->stat_offsets_loaded,
953 			   &osd->link_xon_rx, &nsd->link_xon_rx);
954 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
955 			   pf->stat_offsets_loaded,
956 			   &osd->link_xon_tx, &nsd->link_xon_tx);
957 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
958 			   pf->stat_offsets_loaded,
959 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
960 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
961 			   pf->stat_offsets_loaded,
962 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
963 
964 	for (i = 0; i < 8; i++) {
965 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
966 				   pf->stat_offsets_loaded,
967 				   &osd->priority_xoff_rx[i],
968 				   &nsd->priority_xoff_rx[i]);
969 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
970 				   pf->stat_offsets_loaded,
971 				   &osd->priority_xon_rx[i],
972 				   &nsd->priority_xon_rx[i]);
973 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
974 				   pf->stat_offsets_loaded,
975 				   &osd->priority_xon_tx[i],
976 				   &nsd->priority_xon_tx[i]);
977 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
978 				   pf->stat_offsets_loaded,
979 				   &osd->priority_xoff_tx[i],
980 				   &nsd->priority_xoff_tx[i]);
981 		i40e_stat_update32(hw,
982 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
983 				   pf->stat_offsets_loaded,
984 				   &osd->priority_xon_2_xoff[i],
985 				   &nsd->priority_xon_2_xoff[i]);
986 	}
987 
988 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
989 			   I40E_GLPRT_PRC64L(hw->port),
990 			   pf->stat_offsets_loaded,
991 			   &osd->rx_size_64, &nsd->rx_size_64);
992 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
993 			   I40E_GLPRT_PRC127L(hw->port),
994 			   pf->stat_offsets_loaded,
995 			   &osd->rx_size_127, &nsd->rx_size_127);
996 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
997 			   I40E_GLPRT_PRC255L(hw->port),
998 			   pf->stat_offsets_loaded,
999 			   &osd->rx_size_255, &nsd->rx_size_255);
1000 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1001 			   I40E_GLPRT_PRC511L(hw->port),
1002 			   pf->stat_offsets_loaded,
1003 			   &osd->rx_size_511, &nsd->rx_size_511);
1004 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1005 			   I40E_GLPRT_PRC1023L(hw->port),
1006 			   pf->stat_offsets_loaded,
1007 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1008 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1009 			   I40E_GLPRT_PRC1522L(hw->port),
1010 			   pf->stat_offsets_loaded,
1011 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1012 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1013 			   I40E_GLPRT_PRC9522L(hw->port),
1014 			   pf->stat_offsets_loaded,
1015 			   &osd->rx_size_big, &nsd->rx_size_big);
1016 
1017 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1018 			   I40E_GLPRT_PTC64L(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->tx_size_64, &nsd->tx_size_64);
1021 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1022 			   I40E_GLPRT_PTC127L(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->tx_size_127, &nsd->tx_size_127);
1025 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1026 			   I40E_GLPRT_PTC255L(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->tx_size_255, &nsd->tx_size_255);
1029 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1030 			   I40E_GLPRT_PTC511L(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->tx_size_511, &nsd->tx_size_511);
1033 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1034 			   I40E_GLPRT_PTC1023L(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1037 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1038 			   I40E_GLPRT_PTC1522L(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1041 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1042 			   I40E_GLPRT_PTC9522L(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->tx_size_big, &nsd->tx_size_big);
1045 
1046 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1047 			   pf->stat_offsets_loaded,
1048 			   &osd->rx_undersize, &nsd->rx_undersize);
1049 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1050 			   pf->stat_offsets_loaded,
1051 			   &osd->rx_fragments, &nsd->rx_fragments);
1052 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1053 			   pf->stat_offsets_loaded,
1054 			   &osd->rx_oversize, &nsd->rx_oversize);
1055 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->rx_jabber, &nsd->rx_jabber);
1058 
1059 	/* FDIR stats */
1060 	i40e_stat_update_and_clear32(hw,
1061 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1062 			&nsd->fd_atr_match);
1063 	i40e_stat_update_and_clear32(hw,
1064 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1065 			&nsd->fd_sb_match);
1066 	i40e_stat_update_and_clear32(hw,
1067 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1068 			&nsd->fd_atr_tunnel_match);
1069 
1070 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1071 	nsd->tx_lpi_status =
1072 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1073 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1074 	nsd->rx_lpi_status =
1075 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1076 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1077 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1078 			   pf->stat_offsets_loaded,
1079 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1080 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1083 
1084 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1085 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1086 		nsd->fd_sb_status = true;
1087 	else
1088 		nsd->fd_sb_status = false;
1089 
1090 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1091 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1092 		nsd->fd_atr_status = true;
1093 	else
1094 		nsd->fd_atr_status = false;
1095 
1096 	pf->stat_offsets_loaded = true;
1097 }
1098 
1099 /**
1100  * i40e_update_stats - Update the various statistics counters.
1101  * @vsi: the VSI to be updated
1102  *
1103  * Update the various stats for this VSI and its related entities.
1104  **/
i40e_update_stats(struct i40e_vsi * vsi)1105 void i40e_update_stats(struct i40e_vsi *vsi)
1106 {
1107 	struct i40e_pf *pf = vsi->back;
1108 
1109 	if (vsi == pf->vsi[pf->lan_vsi])
1110 		i40e_update_pf_stats(pf);
1111 
1112 	i40e_update_vsi_stats(vsi);
1113 }
1114 
1115 /**
1116  * i40e_count_filters - counts VSI mac filters
1117  * @vsi: the VSI to be searched
1118  *
1119  * Returns count of mac filters
1120  **/
i40e_count_filters(struct i40e_vsi * vsi)1121 int i40e_count_filters(struct i40e_vsi *vsi)
1122 {
1123 	struct i40e_mac_filter *f;
1124 	struct hlist_node *h;
1125 	int bkt;
1126 	int cnt = 0;
1127 
1128 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1129 		++cnt;
1130 
1131 	return cnt;
1132 }
1133 
1134 /**
1135  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1136  * @vsi: the VSI to be searched
1137  * @macaddr: the MAC address
1138  * @vlan: the vlan
1139  *
1140  * Returns ptr to the filter object or NULL
1141  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1142 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1143 						const u8 *macaddr, s16 vlan)
1144 {
1145 	struct i40e_mac_filter *f;
1146 	u64 key;
1147 
1148 	if (!vsi || !macaddr)
1149 		return NULL;
1150 
1151 	key = i40e_addr_to_hkey(macaddr);
1152 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1153 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1154 		    (vlan == f->vlan))
1155 			return f;
1156 	}
1157 	return NULL;
1158 }
1159 
1160 /**
1161  * i40e_find_mac - Find a mac addr in the macvlan filters list
1162  * @vsi: the VSI to be searched
1163  * @macaddr: the MAC address we are searching for
1164  *
1165  * Returns the first filter with the provided MAC address or NULL if
1166  * MAC address was not found
1167  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1168 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1169 {
1170 	struct i40e_mac_filter *f;
1171 	u64 key;
1172 
1173 	if (!vsi || !macaddr)
1174 		return NULL;
1175 
1176 	key = i40e_addr_to_hkey(macaddr);
1177 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1178 		if ((ether_addr_equal(macaddr, f->macaddr)))
1179 			return f;
1180 	}
1181 	return NULL;
1182 }
1183 
1184 /**
1185  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1186  * @vsi: the VSI to be searched
1187  *
1188  * Returns true if VSI is in vlan mode or false otherwise
1189  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1190 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1191 {
1192 	/* If we have a PVID, always operate in VLAN mode */
1193 	if (vsi->info.pvid)
1194 		return true;
1195 
1196 	/* We need to operate in VLAN mode whenever we have any filters with
1197 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1198 	 * time, incurring search cost repeatedly. However, we can notice two
1199 	 * things:
1200 	 *
1201 	 * 1) the only place where we can gain a VLAN filter is in
1202 	 *    i40e_add_filter.
1203 	 *
1204 	 * 2) the only place where filters are actually removed is in
1205 	 *    i40e_sync_filters_subtask.
1206 	 *
1207 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1208 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1209 	 * we have to perform the full search after deleting filters in
1210 	 * i40e_sync_filters_subtask, but we already have to search
1211 	 * filters here and can perform the check at the same time. This
1212 	 * results in avoiding embedding a loop for VLAN mode inside another
1213 	 * loop over all the filters, and should maintain correctness as noted
1214 	 * above.
1215 	 */
1216 	return vsi->has_vlan_filter;
1217 }
1218 
1219 /**
1220  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1221  * @vsi: the VSI to configure
1222  * @tmp_add_list: list of filters ready to be added
1223  * @tmp_del_list: list of filters ready to be deleted
1224  * @vlan_filters: the number of active VLAN filters
1225  *
1226  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1227  * behave as expected. If we have any active VLAN filters remaining or about
1228  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1229  * so that they only match against untagged traffic. If we no longer have any
1230  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1231  * so that they match against both tagged and untagged traffic. In this way,
1232  * we ensure that we correctly receive the desired traffic. This ensures that
1233  * when we have an active VLAN we will receive only untagged traffic and
1234  * traffic matching active VLANs. If we have no active VLANs then we will
1235  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1236  *
1237  * Finally, in a similar fashion, this function also corrects filters when
1238  * there is an active PVID assigned to this VSI.
1239  *
1240  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1241  *
1242  * This function is only expected to be called from within
1243  * i40e_sync_vsi_filters.
1244  *
1245  * NOTE: This function expects to be called while under the
1246  * mac_filter_hash_lock
1247  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1248 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1249 					 struct hlist_head *tmp_add_list,
1250 					 struct hlist_head *tmp_del_list,
1251 					 int vlan_filters)
1252 {
1253 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1254 	struct i40e_mac_filter *f, *add_head;
1255 	struct i40e_new_mac_filter *new;
1256 	struct hlist_node *h;
1257 	int bkt, new_vlan;
1258 
1259 	/* To determine if a particular filter needs to be replaced we
1260 	 * have the three following conditions:
1261 	 *
1262 	 * a) if we have a PVID assigned, then all filters which are
1263 	 *    not marked as VLAN=PVID must be replaced with filters that
1264 	 *    are.
1265 	 * b) otherwise, if we have any active VLANS, all filters
1266 	 *    which are marked as VLAN=-1 must be replaced with
1267 	 *    filters marked as VLAN=0
1268 	 * c) finally, if we do not have any active VLANS, all filters
1269 	 *    which are marked as VLAN=0 must be replaced with filters
1270 	 *    marked as VLAN=-1
1271 	 */
1272 
1273 	/* Update the filters about to be added in place */
1274 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1275 		if (pvid && new->f->vlan != pvid)
1276 			new->f->vlan = pvid;
1277 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1278 			new->f->vlan = 0;
1279 		else if (!vlan_filters && new->f->vlan == 0)
1280 			new->f->vlan = I40E_VLAN_ANY;
1281 	}
1282 
1283 	/* Update the remaining active filters */
1284 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1285 		/* Combine the checks for whether a filter needs to be changed
1286 		 * and then determine the new VLAN inside the if block, in
1287 		 * order to avoid duplicating code for adding the new filter
1288 		 * then deleting the old filter.
1289 		 */
1290 		if ((pvid && f->vlan != pvid) ||
1291 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1292 		    (!vlan_filters && f->vlan == 0)) {
1293 			/* Determine the new vlan we will be adding */
1294 			if (pvid)
1295 				new_vlan = pvid;
1296 			else if (vlan_filters)
1297 				new_vlan = 0;
1298 			else
1299 				new_vlan = I40E_VLAN_ANY;
1300 
1301 			/* Create the new filter */
1302 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1303 			if (!add_head)
1304 				return -ENOMEM;
1305 
1306 			/* Create a temporary i40e_new_mac_filter */
1307 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1308 			if (!new)
1309 				return -ENOMEM;
1310 
1311 			new->f = add_head;
1312 			new->state = add_head->state;
1313 
1314 			/* Add the new filter to the tmp list */
1315 			hlist_add_head(&new->hlist, tmp_add_list);
1316 
1317 			/* Put the original filter into the delete list */
1318 			f->state = I40E_FILTER_REMOVE;
1319 			hash_del(&f->hlist);
1320 			hlist_add_head(&f->hlist, tmp_del_list);
1321 		}
1322 	}
1323 
1324 	vsi->has_vlan_filter = !!vlan_filters;
1325 
1326 	return 0;
1327 }
1328 
1329 /**
1330  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1331  * @vsi: the PF Main VSI - inappropriate for any other VSI
1332  * @macaddr: the MAC address
1333  *
1334  * Remove whatever filter the firmware set up so the driver can manage
1335  * its own filtering intelligently.
1336  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1337 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1338 {
1339 	struct i40e_aqc_remove_macvlan_element_data element;
1340 	struct i40e_pf *pf = vsi->back;
1341 
1342 	/* Only appropriate for the PF main VSI */
1343 	if (vsi->type != I40E_VSI_MAIN)
1344 		return;
1345 
1346 	memset(&element, 0, sizeof(element));
1347 	ether_addr_copy(element.mac_addr, macaddr);
1348 	element.vlan_tag = 0;
1349 	/* Ignore error returns, some firmware does it this way... */
1350 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1351 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1352 
1353 	memset(&element, 0, sizeof(element));
1354 	ether_addr_copy(element.mac_addr, macaddr);
1355 	element.vlan_tag = 0;
1356 	/* ...and some firmware does it this way. */
1357 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1358 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1359 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1360 }
1361 
1362 /**
1363  * i40e_add_filter - Add a mac/vlan filter to the VSI
1364  * @vsi: the VSI to be searched
1365  * @macaddr: the MAC address
1366  * @vlan: the vlan
1367  *
1368  * Returns ptr to the filter object or NULL when no memory available.
1369  *
1370  * NOTE: This function is expected to be called with mac_filter_hash_lock
1371  * being held.
1372  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1373 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1374 					const u8 *macaddr, s16 vlan)
1375 {
1376 	struct i40e_mac_filter *f;
1377 	u64 key;
1378 
1379 	if (!vsi || !macaddr)
1380 		return NULL;
1381 
1382 	f = i40e_find_filter(vsi, macaddr, vlan);
1383 	if (!f) {
1384 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1385 		if (!f)
1386 			return NULL;
1387 
1388 		/* Update the boolean indicating if we need to function in
1389 		 * VLAN mode.
1390 		 */
1391 		if (vlan >= 0)
1392 			vsi->has_vlan_filter = true;
1393 
1394 		ether_addr_copy(f->macaddr, macaddr);
1395 		f->vlan = vlan;
1396 		f->state = I40E_FILTER_NEW;
1397 		INIT_HLIST_NODE(&f->hlist);
1398 
1399 		key = i40e_addr_to_hkey(macaddr);
1400 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1401 
1402 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1403 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1404 	}
1405 
1406 	/* If we're asked to add a filter that has been marked for removal, it
1407 	 * is safe to simply restore it to active state. __i40e_del_filter
1408 	 * will have simply deleted any filters which were previously marked
1409 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1410 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1411 	 * task, just restore this filter to the ACTIVE state so that the
1412 	 * sync task leaves it in place
1413 	 */
1414 	if (f->state == I40E_FILTER_REMOVE)
1415 		f->state = I40E_FILTER_ACTIVE;
1416 
1417 	return f;
1418 }
1419 
1420 /**
1421  * __i40e_del_filter - Remove a specific filter from the VSI
1422  * @vsi: VSI to remove from
1423  * @f: the filter to remove from the list
1424  *
1425  * This function should be called instead of i40e_del_filter only if you know
1426  * the exact filter you will remove already, such as via i40e_find_filter or
1427  * i40e_find_mac.
1428  *
1429  * NOTE: This function is expected to be called with mac_filter_hash_lock
1430  * being held.
1431  * ANOTHER NOTE: This function MUST be called from within the context of
1432  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1433  * instead of list_for_each_entry().
1434  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1435 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1436 {
1437 	if (!f)
1438 		return;
1439 
1440 	/* If the filter was never added to firmware then we can just delete it
1441 	 * directly and we don't want to set the status to remove or else an
1442 	 * admin queue command will unnecessarily fire.
1443 	 */
1444 	if ((f->state == I40E_FILTER_FAILED) ||
1445 	    (f->state == I40E_FILTER_NEW)) {
1446 		hash_del(&f->hlist);
1447 		kfree(f);
1448 	} else {
1449 		f->state = I40E_FILTER_REMOVE;
1450 	}
1451 
1452 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1453 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1454 }
1455 
1456 /**
1457  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1458  * @vsi: the VSI to be searched
1459  * @macaddr: the MAC address
1460  * @vlan: the VLAN
1461  *
1462  * NOTE: This function is expected to be called with mac_filter_hash_lock
1463  * being held.
1464  * ANOTHER NOTE: This function MUST be called from within the context of
1465  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1466  * instead of list_for_each_entry().
1467  **/
i40e_del_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1468 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1469 {
1470 	struct i40e_mac_filter *f;
1471 
1472 	if (!vsi || !macaddr)
1473 		return;
1474 
1475 	f = i40e_find_filter(vsi, macaddr, vlan);
1476 	__i40e_del_filter(vsi, f);
1477 }
1478 
1479 /**
1480  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1481  * @vsi: the VSI to be searched
1482  * @macaddr: the mac address to be filtered
1483  *
1484  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1485  * go through all the macvlan filters and add a macvlan filter for each
1486  * unique vlan that already exists. If a PVID has been assigned, instead only
1487  * add the macaddr to that VLAN.
1488  *
1489  * Returns last filter added on success, else NULL
1490  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1491 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1492 					    const u8 *macaddr)
1493 {
1494 	struct i40e_mac_filter *f, *add = NULL;
1495 	struct hlist_node *h;
1496 	int bkt;
1497 
1498 	if (vsi->info.pvid)
1499 		return i40e_add_filter(vsi, macaddr,
1500 				       le16_to_cpu(vsi->info.pvid));
1501 
1502 	if (!i40e_is_vsi_in_vlan(vsi))
1503 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1504 
1505 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1506 		if (f->state == I40E_FILTER_REMOVE)
1507 			continue;
1508 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1509 		if (!add)
1510 			return NULL;
1511 	}
1512 
1513 	return add;
1514 }
1515 
1516 /**
1517  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1518  * @vsi: the VSI to be searched
1519  * @macaddr: the mac address to be removed
1520  *
1521  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1522  * associated with.
1523  *
1524  * Returns 0 for success, or error
1525  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1526 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1527 {
1528 	struct i40e_mac_filter *f;
1529 	struct hlist_node *h;
1530 	bool found = false;
1531 	int bkt;
1532 
1533 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1534 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1535 		if (ether_addr_equal(macaddr, f->macaddr)) {
1536 			__i40e_del_filter(vsi, f);
1537 			found = true;
1538 		}
1539 	}
1540 
1541 	if (found)
1542 		return 0;
1543 	else
1544 		return -ENOENT;
1545 }
1546 
1547 /**
1548  * i40e_set_mac - NDO callback to set mac address
1549  * @netdev: network interface device structure
1550  * @p: pointer to an address structure
1551  *
1552  * Returns 0 on success, negative on failure
1553  **/
i40e_set_mac(struct net_device * netdev,void * p)1554 static int i40e_set_mac(struct net_device *netdev, void *p)
1555 {
1556 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1557 	struct i40e_vsi *vsi = np->vsi;
1558 	struct i40e_pf *pf = vsi->back;
1559 	struct i40e_hw *hw = &pf->hw;
1560 	struct sockaddr *addr = p;
1561 
1562 	if (!is_valid_ether_addr(addr->sa_data))
1563 		return -EADDRNOTAVAIL;
1564 
1565 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1566 		netdev_info(netdev, "already using mac address %pM\n",
1567 			    addr->sa_data);
1568 		return 0;
1569 	}
1570 
1571 	if (test_bit(__I40E_DOWN, pf->state) ||
1572 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1573 		return -EADDRNOTAVAIL;
1574 
1575 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1576 		netdev_info(netdev, "returning to hw mac address %pM\n",
1577 			    hw->mac.addr);
1578 	else
1579 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1580 
1581 	/* Copy the address first, so that we avoid a possible race with
1582 	 * .set_rx_mode().
1583 	 * - Remove old address from MAC filter
1584 	 * - Copy new address
1585 	 * - Add new address to MAC filter
1586 	 */
1587 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1588 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1589 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1590 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1591 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1592 
1593 	if (vsi->type == I40E_VSI_MAIN) {
1594 		i40e_status ret;
1595 
1596 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1597 						addr->sa_data, NULL);
1598 		if (ret)
1599 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1600 				    i40e_stat_str(hw, ret),
1601 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1602 	}
1603 
1604 	/* schedule our worker thread which will take care of
1605 	 * applying the new filter changes
1606 	 */
1607 	i40e_service_event_schedule(pf);
1608 	return 0;
1609 }
1610 
1611 /**
1612  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1613  * @vsi: vsi structure
1614  * @seed: RSS hash seed
1615  * @lut: pointer to lookup table of lut_size
1616  * @lut_size: size of the lookup table
1617  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1618 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1619 			      u8 *lut, u16 lut_size)
1620 {
1621 	struct i40e_pf *pf = vsi->back;
1622 	struct i40e_hw *hw = &pf->hw;
1623 	int ret = 0;
1624 
1625 	if (seed) {
1626 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1627 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1628 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1629 		if (ret) {
1630 			dev_info(&pf->pdev->dev,
1631 				 "Cannot set RSS key, err %s aq_err %s\n",
1632 				 i40e_stat_str(hw, ret),
1633 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1634 			return ret;
1635 		}
1636 	}
1637 	if (lut) {
1638 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1639 
1640 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1641 		if (ret) {
1642 			dev_info(&pf->pdev->dev,
1643 				 "Cannot set RSS lut, err %s aq_err %s\n",
1644 				 i40e_stat_str(hw, ret),
1645 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1646 			return ret;
1647 		}
1648 	}
1649 	return ret;
1650 }
1651 
1652 /**
1653  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1654  * @vsi: VSI structure
1655  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1656 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1657 {
1658 	struct i40e_pf *pf = vsi->back;
1659 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1660 	u8 *lut;
1661 	int ret;
1662 
1663 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1664 		return 0;
1665 	if (!vsi->rss_size)
1666 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1667 				      vsi->num_queue_pairs);
1668 	if (!vsi->rss_size)
1669 		return -EINVAL;
1670 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1671 	if (!lut)
1672 		return -ENOMEM;
1673 
1674 	/* Use the user configured hash keys and lookup table if there is one,
1675 	 * otherwise use default
1676 	 */
1677 	if (vsi->rss_lut_user)
1678 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1679 	else
1680 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1681 	if (vsi->rss_hkey_user)
1682 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1683 	else
1684 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1685 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1686 	kfree(lut);
1687 	return ret;
1688 }
1689 
1690 /**
1691  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1692  * @vsi: the VSI being configured,
1693  * @ctxt: VSI context structure
1694  * @enabled_tc: number of traffic classes to enable
1695  *
1696  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1697  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1698 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1699 					   struct i40e_vsi_context *ctxt,
1700 					   u8 enabled_tc)
1701 {
1702 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1703 	int i, override_q, pow, num_qps, ret;
1704 	u8 netdev_tc = 0, offset = 0;
1705 
1706 	if (vsi->type != I40E_VSI_MAIN)
1707 		return -EINVAL;
1708 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1709 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1710 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1711 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1712 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1713 
1714 	/* find the next higher power-of-2 of num queue pairs */
1715 	pow = ilog2(num_qps);
1716 	if (!is_power_of_2(num_qps))
1717 		pow++;
1718 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1719 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1720 
1721 	/* Setup queue offset/count for all TCs for given VSI */
1722 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1723 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1724 		/* See if the given TC is enabled for the given VSI */
1725 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1726 			offset = vsi->mqprio_qopt.qopt.offset[i];
1727 			qcount = vsi->mqprio_qopt.qopt.count[i];
1728 			if (qcount > max_qcount)
1729 				max_qcount = qcount;
1730 			vsi->tc_config.tc_info[i].qoffset = offset;
1731 			vsi->tc_config.tc_info[i].qcount = qcount;
1732 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1733 		} else {
1734 			/* TC is not enabled so set the offset to
1735 			 * default queue and allocate one queue
1736 			 * for the given TC.
1737 			 */
1738 			vsi->tc_config.tc_info[i].qoffset = 0;
1739 			vsi->tc_config.tc_info[i].qcount = 1;
1740 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1741 		}
1742 	}
1743 
1744 	/* Set actual Tx/Rx queue pairs */
1745 	vsi->num_queue_pairs = offset + qcount;
1746 
1747 	/* Setup queue TC[0].qmap for given VSI context */
1748 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1749 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1750 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1751 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1752 
1753 	/* Reconfigure RSS for main VSI with max queue count */
1754 	vsi->rss_size = max_qcount;
1755 	ret = i40e_vsi_config_rss(vsi);
1756 	if (ret) {
1757 		dev_info(&vsi->back->pdev->dev,
1758 			 "Failed to reconfig rss for num_queues (%u)\n",
1759 			 max_qcount);
1760 		return ret;
1761 	}
1762 	vsi->reconfig_rss = true;
1763 	dev_dbg(&vsi->back->pdev->dev,
1764 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1765 
1766 	/* Find queue count available for channel VSIs and starting offset
1767 	 * for channel VSIs
1768 	 */
1769 	override_q = vsi->mqprio_qopt.qopt.count[0];
1770 	if (override_q && override_q < vsi->num_queue_pairs) {
1771 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1772 		vsi->next_base_queue = override_q;
1773 	}
1774 	return 0;
1775 }
1776 
1777 /**
1778  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1779  * @vsi: the VSI being setup
1780  * @ctxt: VSI context structure
1781  * @enabled_tc: Enabled TCs bitmap
1782  * @is_add: True if called before Add VSI
1783  *
1784  * Setup VSI queue mapping for enabled traffic classes.
1785  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)1786 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1787 				     struct i40e_vsi_context *ctxt,
1788 				     u8 enabled_tc,
1789 				     bool is_add)
1790 {
1791 	struct i40e_pf *pf = vsi->back;
1792 	u16 sections = 0;
1793 	u8 netdev_tc = 0;
1794 	u16 numtc = 1;
1795 	u16 qcount;
1796 	u8 offset;
1797 	u16 qmap;
1798 	int i;
1799 	u16 num_tc_qps = 0;
1800 
1801 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1802 	offset = 0;
1803 
1804 	/* Number of queues per enabled TC */
1805 	num_tc_qps = vsi->alloc_queue_pairs;
1806 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1807 		/* Find numtc from enabled TC bitmap */
1808 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1809 			if (enabled_tc & BIT(i)) /* TC is enabled */
1810 				numtc++;
1811 		}
1812 		if (!numtc) {
1813 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1814 			numtc = 1;
1815 		}
1816 		num_tc_qps = num_tc_qps / numtc;
1817 		num_tc_qps = min_t(int, num_tc_qps,
1818 				   i40e_pf_get_max_q_per_tc(pf));
1819 	}
1820 
1821 	vsi->tc_config.numtc = numtc;
1822 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1823 
1824 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1825 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1826 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1827 
1828 	/* Setup queue offset/count for all TCs for given VSI */
1829 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1830 		/* See if the given TC is enabled for the given VSI */
1831 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1832 			/* TC is enabled */
1833 			int pow, num_qps;
1834 
1835 			switch (vsi->type) {
1836 			case I40E_VSI_MAIN:
1837 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1838 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1839 				    vsi->tc_config.enabled_tc != 1) {
1840 					qcount = min_t(int, pf->alloc_rss_size,
1841 						       num_tc_qps);
1842 					break;
1843 				}
1844 				fallthrough;
1845 			case I40E_VSI_FDIR:
1846 			case I40E_VSI_SRIOV:
1847 			case I40E_VSI_VMDQ2:
1848 			default:
1849 				qcount = num_tc_qps;
1850 				WARN_ON(i != 0);
1851 				break;
1852 			}
1853 			vsi->tc_config.tc_info[i].qoffset = offset;
1854 			vsi->tc_config.tc_info[i].qcount = qcount;
1855 
1856 			/* find the next higher power-of-2 of num queue pairs */
1857 			num_qps = qcount;
1858 			pow = 0;
1859 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1860 				pow++;
1861 				num_qps >>= 1;
1862 			}
1863 
1864 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1865 			qmap =
1866 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1867 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1868 
1869 			offset += qcount;
1870 		} else {
1871 			/* TC is not enabled so set the offset to
1872 			 * default queue and allocate one queue
1873 			 * for the given TC.
1874 			 */
1875 			vsi->tc_config.tc_info[i].qoffset = 0;
1876 			vsi->tc_config.tc_info[i].qcount = 1;
1877 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1878 
1879 			qmap = 0;
1880 		}
1881 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1882 	}
1883 
1884 	/* Set actual Tx/Rx queue pairs */
1885 	vsi->num_queue_pairs = offset;
1886 	if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1887 		if (vsi->req_queue_pairs > 0)
1888 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1889 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1890 			vsi->num_queue_pairs = pf->num_lan_msix;
1891 	}
1892 
1893 	/* Scheduler section valid can only be set for ADD VSI */
1894 	if (is_add) {
1895 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1896 
1897 		ctxt->info.up_enable_bits = enabled_tc;
1898 	}
1899 	if (vsi->type == I40E_VSI_SRIOV) {
1900 		ctxt->info.mapping_flags |=
1901 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1902 		for (i = 0; i < vsi->num_queue_pairs; i++)
1903 			ctxt->info.queue_mapping[i] =
1904 					       cpu_to_le16(vsi->base_queue + i);
1905 	} else {
1906 		ctxt->info.mapping_flags |=
1907 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1908 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1909 	}
1910 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1911 }
1912 
1913 /**
1914  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1915  * @netdev: the netdevice
1916  * @addr: address to add
1917  *
1918  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1919  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1920  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)1921 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1922 {
1923 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1924 	struct i40e_vsi *vsi = np->vsi;
1925 
1926 	if (i40e_add_mac_filter(vsi, addr))
1927 		return 0;
1928 	else
1929 		return -ENOMEM;
1930 }
1931 
1932 /**
1933  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1934  * @netdev: the netdevice
1935  * @addr: address to add
1936  *
1937  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1938  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1939  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)1940 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1941 {
1942 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1943 	struct i40e_vsi *vsi = np->vsi;
1944 
1945 	/* Under some circumstances, we might receive a request to delete
1946 	 * our own device address from our uc list. Because we store the
1947 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1948 	 * such requests and not delete our device address from this list.
1949 	 */
1950 	if (ether_addr_equal(addr, netdev->dev_addr))
1951 		return 0;
1952 
1953 	i40e_del_mac_filter(vsi, addr);
1954 
1955 	return 0;
1956 }
1957 
1958 /**
1959  * i40e_set_rx_mode - NDO callback to set the netdev filters
1960  * @netdev: network interface device structure
1961  **/
i40e_set_rx_mode(struct net_device * netdev)1962 static void i40e_set_rx_mode(struct net_device *netdev)
1963 {
1964 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1965 	struct i40e_vsi *vsi = np->vsi;
1966 
1967 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1968 
1969 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1970 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1971 
1972 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1973 
1974 	/* check for other flag changes */
1975 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
1976 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1977 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1978 	}
1979 }
1980 
1981 /**
1982  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1983  * @vsi: Pointer to VSI struct
1984  * @from: Pointer to list which contains MAC filter entries - changes to
1985  *        those entries needs to be undone.
1986  *
1987  * MAC filter entries from this list were slated for deletion.
1988  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)1989 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1990 					 struct hlist_head *from)
1991 {
1992 	struct i40e_mac_filter *f;
1993 	struct hlist_node *h;
1994 
1995 	hlist_for_each_entry_safe(f, h, from, hlist) {
1996 		u64 key = i40e_addr_to_hkey(f->macaddr);
1997 
1998 		/* Move the element back into MAC filter list*/
1999 		hlist_del(&f->hlist);
2000 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2001 	}
2002 }
2003 
2004 /**
2005  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2006  * @vsi: Pointer to vsi struct
2007  * @from: Pointer to list which contains MAC filter entries - changes to
2008  *        those entries needs to be undone.
2009  *
2010  * MAC filter entries from this list were slated for addition.
2011  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2012 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2013 					 struct hlist_head *from)
2014 {
2015 	struct i40e_new_mac_filter *new;
2016 	struct hlist_node *h;
2017 
2018 	hlist_for_each_entry_safe(new, h, from, hlist) {
2019 		/* We can simply free the wrapper structure */
2020 		hlist_del(&new->hlist);
2021 		kfree(new);
2022 	}
2023 }
2024 
2025 /**
2026  * i40e_next_entry - Get the next non-broadcast filter from a list
2027  * @next: pointer to filter in list
2028  *
2029  * Returns the next non-broadcast filter in the list. Required so that we
2030  * ignore broadcast filters within the list, since these are not handled via
2031  * the normal firmware update path.
2032  */
2033 static
i40e_next_filter(struct i40e_new_mac_filter * next)2034 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2035 {
2036 	hlist_for_each_entry_continue(next, hlist) {
2037 		if (!is_broadcast_ether_addr(next->f->macaddr))
2038 			return next;
2039 	}
2040 
2041 	return NULL;
2042 }
2043 
2044 /**
2045  * i40e_update_filter_state - Update filter state based on return data
2046  * from firmware
2047  * @count: Number of filters added
2048  * @add_list: return data from fw
2049  * @add_head: pointer to first filter in current batch
2050  *
2051  * MAC filter entries from list were slated to be added to device. Returns
2052  * number of successful filters. Note that 0 does NOT mean success!
2053  **/
2054 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2055 i40e_update_filter_state(int count,
2056 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2057 			 struct i40e_new_mac_filter *add_head)
2058 {
2059 	int retval = 0;
2060 	int i;
2061 
2062 	for (i = 0; i < count; i++) {
2063 		/* Always check status of each filter. We don't need to check
2064 		 * the firmware return status because we pre-set the filter
2065 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2066 		 * request to the adminq. Thus, if it no longer matches then
2067 		 * we know the filter is active.
2068 		 */
2069 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2070 			add_head->state = I40E_FILTER_FAILED;
2071 		} else {
2072 			add_head->state = I40E_FILTER_ACTIVE;
2073 			retval++;
2074 		}
2075 
2076 		add_head = i40e_next_filter(add_head);
2077 		if (!add_head)
2078 			break;
2079 	}
2080 
2081 	return retval;
2082 }
2083 
2084 /**
2085  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2086  * @vsi: ptr to the VSI
2087  * @vsi_name: name to display in messages
2088  * @list: the list of filters to send to firmware
2089  * @num_del: the number of filters to delete
2090  * @retval: Set to -EIO on failure to delete
2091  *
2092  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2093  * *retval instead of a return value so that success does not force ret_val to
2094  * be set to 0. This ensures that a sequence of calls to this function
2095  * preserve the previous value of *retval on successful delete.
2096  */
2097 static
i40e_aqc_del_filters(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_aqc_remove_macvlan_element_data * list,int num_del,int * retval)2098 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2099 			  struct i40e_aqc_remove_macvlan_element_data *list,
2100 			  int num_del, int *retval)
2101 {
2102 	struct i40e_hw *hw = &vsi->back->hw;
2103 	i40e_status aq_ret;
2104 	int aq_err;
2105 
2106 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2107 	aq_err = hw->aq.asq_last_status;
2108 
2109 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2110 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2111 		*retval = -EIO;
2112 		dev_info(&vsi->back->pdev->dev,
2113 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2114 			 vsi_name, i40e_stat_str(hw, aq_ret),
2115 			 i40e_aq_str(hw, aq_err));
2116 	}
2117 }
2118 
2119 /**
2120  * i40e_aqc_add_filters - Request firmware to add a set of filters
2121  * @vsi: ptr to the VSI
2122  * @vsi_name: name to display in messages
2123  * @list: the list of filters to send to firmware
2124  * @add_head: Position in the add hlist
2125  * @num_add: the number of filters to add
2126  *
2127  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2128  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2129  * space for more filters.
2130  */
2131 static
i40e_aqc_add_filters(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_aqc_add_macvlan_element_data * list,struct i40e_new_mac_filter * add_head,int num_add)2132 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2133 			  struct i40e_aqc_add_macvlan_element_data *list,
2134 			  struct i40e_new_mac_filter *add_head,
2135 			  int num_add)
2136 {
2137 	struct i40e_hw *hw = &vsi->back->hw;
2138 	int aq_err, fcnt;
2139 
2140 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2141 	aq_err = hw->aq.asq_last_status;
2142 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2143 
2144 	if (fcnt != num_add) {
2145 		if (vsi->type == I40E_VSI_MAIN) {
2146 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2147 			dev_warn(&vsi->back->pdev->dev,
2148 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2149 				 i40e_aq_str(hw, aq_err), vsi_name);
2150 		} else if (vsi->type == I40E_VSI_SRIOV ||
2151 			   vsi->type == I40E_VSI_VMDQ1 ||
2152 			   vsi->type == I40E_VSI_VMDQ2) {
2153 			dev_warn(&vsi->back->pdev->dev,
2154 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2155 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2156 		} else {
2157 			dev_warn(&vsi->back->pdev->dev,
2158 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2159 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2160 		}
2161 	}
2162 }
2163 
2164 /**
2165  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2166  * @vsi: pointer to the VSI
2167  * @vsi_name: the VSI name
2168  * @f: filter data
2169  *
2170  * This function sets or clears the promiscuous broadcast flags for VLAN
2171  * filters in order to properly receive broadcast frames. Assumes that only
2172  * broadcast filters are passed.
2173  *
2174  * Returns status indicating success or failure;
2175  **/
2176 static i40e_status
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2177 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2178 			  struct i40e_mac_filter *f)
2179 {
2180 	bool enable = f->state == I40E_FILTER_NEW;
2181 	struct i40e_hw *hw = &vsi->back->hw;
2182 	i40e_status aq_ret;
2183 
2184 	if (f->vlan == I40E_VLAN_ANY) {
2185 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2186 						   vsi->seid,
2187 						   enable,
2188 						   NULL);
2189 	} else {
2190 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2191 							    vsi->seid,
2192 							    enable,
2193 							    f->vlan,
2194 							    NULL);
2195 	}
2196 
2197 	if (aq_ret) {
2198 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2199 		dev_warn(&vsi->back->pdev->dev,
2200 			 "Error %s, forcing overflow promiscuous on %s\n",
2201 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2202 			 vsi_name);
2203 	}
2204 
2205 	return aq_ret;
2206 }
2207 
2208 /**
2209  * i40e_set_promiscuous - set promiscuous mode
2210  * @pf: board private structure
2211  * @promisc: promisc on or off
2212  *
2213  * There are different ways of setting promiscuous mode on a PF depending on
2214  * what state/environment we're in.  This identifies and sets it appropriately.
2215  * Returns 0 on success.
2216  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2217 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2218 {
2219 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2220 	struct i40e_hw *hw = &pf->hw;
2221 	i40e_status aq_ret;
2222 
2223 	if (vsi->type == I40E_VSI_MAIN &&
2224 	    pf->lan_veb != I40E_NO_VEB &&
2225 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2226 		/* set defport ON for Main VSI instead of true promisc
2227 		 * this way we will get all unicast/multicast and VLAN
2228 		 * promisc behavior but will not get VF or VMDq traffic
2229 		 * replicated on the Main VSI.
2230 		 */
2231 		if (promisc)
2232 			aq_ret = i40e_aq_set_default_vsi(hw,
2233 							 vsi->seid,
2234 							 NULL);
2235 		else
2236 			aq_ret = i40e_aq_clear_default_vsi(hw,
2237 							   vsi->seid,
2238 							   NULL);
2239 		if (aq_ret) {
2240 			dev_info(&pf->pdev->dev,
2241 				 "Set default VSI failed, err %s, aq_err %s\n",
2242 				 i40e_stat_str(hw, aq_ret),
2243 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2244 		}
2245 	} else {
2246 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2247 						  hw,
2248 						  vsi->seid,
2249 						  promisc, NULL,
2250 						  true);
2251 		if (aq_ret) {
2252 			dev_info(&pf->pdev->dev,
2253 				 "set unicast promisc failed, err %s, aq_err %s\n",
2254 				 i40e_stat_str(hw, aq_ret),
2255 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2256 		}
2257 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2258 						  hw,
2259 						  vsi->seid,
2260 						  promisc, NULL);
2261 		if (aq_ret) {
2262 			dev_info(&pf->pdev->dev,
2263 				 "set multicast promisc failed, err %s, aq_err %s\n",
2264 				 i40e_stat_str(hw, aq_ret),
2265 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2266 		}
2267 	}
2268 
2269 	if (!aq_ret)
2270 		pf->cur_promisc = promisc;
2271 
2272 	return aq_ret;
2273 }
2274 
2275 /**
2276  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2277  * @vsi: ptr to the VSI
2278  *
2279  * Push any outstanding VSI filter changes through the AdminQ.
2280  *
2281  * Returns 0 or error value
2282  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2283 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2284 {
2285 	struct hlist_head tmp_add_list, tmp_del_list;
2286 	struct i40e_mac_filter *f;
2287 	struct i40e_new_mac_filter *new, *add_head = NULL;
2288 	struct i40e_hw *hw = &vsi->back->hw;
2289 	bool old_overflow, new_overflow;
2290 	unsigned int failed_filters = 0;
2291 	unsigned int vlan_filters = 0;
2292 	char vsi_name[16] = "PF";
2293 	int filter_list_len = 0;
2294 	i40e_status aq_ret = 0;
2295 	u32 changed_flags = 0;
2296 	struct hlist_node *h;
2297 	struct i40e_pf *pf;
2298 	int num_add = 0;
2299 	int num_del = 0;
2300 	int retval = 0;
2301 	u16 cmd_flags;
2302 	int list_size;
2303 	int bkt;
2304 
2305 	/* empty array typed pointers, kcalloc later */
2306 	struct i40e_aqc_add_macvlan_element_data *add_list;
2307 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2308 
2309 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2310 		usleep_range(1000, 2000);
2311 	pf = vsi->back;
2312 
2313 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2314 
2315 	if (vsi->netdev) {
2316 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2317 		vsi->current_netdev_flags = vsi->netdev->flags;
2318 	}
2319 
2320 	INIT_HLIST_HEAD(&tmp_add_list);
2321 	INIT_HLIST_HEAD(&tmp_del_list);
2322 
2323 	if (vsi->type == I40E_VSI_SRIOV)
2324 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2325 	else if (vsi->type != I40E_VSI_MAIN)
2326 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2327 
2328 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2329 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2330 
2331 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2332 		/* Create a list of filters to delete. */
2333 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2334 			if (f->state == I40E_FILTER_REMOVE) {
2335 				/* Move the element into temporary del_list */
2336 				hash_del(&f->hlist);
2337 				hlist_add_head(&f->hlist, &tmp_del_list);
2338 
2339 				/* Avoid counting removed filters */
2340 				continue;
2341 			}
2342 			if (f->state == I40E_FILTER_NEW) {
2343 				/* Create a temporary i40e_new_mac_filter */
2344 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2345 				if (!new)
2346 					goto err_no_memory_locked;
2347 
2348 				/* Store pointer to the real filter */
2349 				new->f = f;
2350 				new->state = f->state;
2351 
2352 				/* Add it to the hash list */
2353 				hlist_add_head(&new->hlist, &tmp_add_list);
2354 			}
2355 
2356 			/* Count the number of active (current and new) VLAN
2357 			 * filters we have now. Does not count filters which
2358 			 * are marked for deletion.
2359 			 */
2360 			if (f->vlan > 0)
2361 				vlan_filters++;
2362 		}
2363 
2364 		retval = i40e_correct_mac_vlan_filters(vsi,
2365 						       &tmp_add_list,
2366 						       &tmp_del_list,
2367 						       vlan_filters);
2368 		if (retval)
2369 			goto err_no_memory_locked;
2370 
2371 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2372 	}
2373 
2374 	/* Now process 'del_list' outside the lock */
2375 	if (!hlist_empty(&tmp_del_list)) {
2376 		filter_list_len = hw->aq.asq_buf_size /
2377 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2378 		list_size = filter_list_len *
2379 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2380 		del_list = kzalloc(list_size, GFP_ATOMIC);
2381 		if (!del_list)
2382 			goto err_no_memory;
2383 
2384 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2385 			cmd_flags = 0;
2386 
2387 			/* handle broadcast filters by updating the broadcast
2388 			 * promiscuous flag and release filter list.
2389 			 */
2390 			if (is_broadcast_ether_addr(f->macaddr)) {
2391 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2392 
2393 				hlist_del(&f->hlist);
2394 				kfree(f);
2395 				continue;
2396 			}
2397 
2398 			/* add to delete list */
2399 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2400 			if (f->vlan == I40E_VLAN_ANY) {
2401 				del_list[num_del].vlan_tag = 0;
2402 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2403 			} else {
2404 				del_list[num_del].vlan_tag =
2405 					cpu_to_le16((u16)(f->vlan));
2406 			}
2407 
2408 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2409 			del_list[num_del].flags = cmd_flags;
2410 			num_del++;
2411 
2412 			/* flush a full buffer */
2413 			if (num_del == filter_list_len) {
2414 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2415 						     num_del, &retval);
2416 				memset(del_list, 0, list_size);
2417 				num_del = 0;
2418 			}
2419 			/* Release memory for MAC filter entries which were
2420 			 * synced up with HW.
2421 			 */
2422 			hlist_del(&f->hlist);
2423 			kfree(f);
2424 		}
2425 
2426 		if (num_del) {
2427 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2428 					     num_del, &retval);
2429 		}
2430 
2431 		kfree(del_list);
2432 		del_list = NULL;
2433 	}
2434 
2435 	if (!hlist_empty(&tmp_add_list)) {
2436 		/* Do all the adds now. */
2437 		filter_list_len = hw->aq.asq_buf_size /
2438 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2439 		list_size = filter_list_len *
2440 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2441 		add_list = kzalloc(list_size, GFP_ATOMIC);
2442 		if (!add_list)
2443 			goto err_no_memory;
2444 
2445 		num_add = 0;
2446 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2447 			/* handle broadcast filters by updating the broadcast
2448 			 * promiscuous flag instead of adding a MAC filter.
2449 			 */
2450 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2451 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2452 							      new->f))
2453 					new->state = I40E_FILTER_FAILED;
2454 				else
2455 					new->state = I40E_FILTER_ACTIVE;
2456 				continue;
2457 			}
2458 
2459 			/* add to add array */
2460 			if (num_add == 0)
2461 				add_head = new;
2462 			cmd_flags = 0;
2463 			ether_addr_copy(add_list[num_add].mac_addr,
2464 					new->f->macaddr);
2465 			if (new->f->vlan == I40E_VLAN_ANY) {
2466 				add_list[num_add].vlan_tag = 0;
2467 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2468 			} else {
2469 				add_list[num_add].vlan_tag =
2470 					cpu_to_le16((u16)(new->f->vlan));
2471 			}
2472 			add_list[num_add].queue_number = 0;
2473 			/* set invalid match method for later detection */
2474 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2475 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2476 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2477 			num_add++;
2478 
2479 			/* flush a full buffer */
2480 			if (num_add == filter_list_len) {
2481 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2482 						     add_head, num_add);
2483 				memset(add_list, 0, list_size);
2484 				num_add = 0;
2485 			}
2486 		}
2487 		if (num_add) {
2488 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2489 					     num_add);
2490 		}
2491 		/* Now move all of the filters from the temp add list back to
2492 		 * the VSI's list.
2493 		 */
2494 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2495 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2496 			/* Only update the state if we're still NEW */
2497 			if (new->f->state == I40E_FILTER_NEW)
2498 				new->f->state = new->state;
2499 			hlist_del(&new->hlist);
2500 			kfree(new);
2501 		}
2502 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2503 		kfree(add_list);
2504 		add_list = NULL;
2505 	}
2506 
2507 	/* Determine the number of active and failed filters. */
2508 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2509 	vsi->active_filters = 0;
2510 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2511 		if (f->state == I40E_FILTER_ACTIVE)
2512 			vsi->active_filters++;
2513 		else if (f->state == I40E_FILTER_FAILED)
2514 			failed_filters++;
2515 	}
2516 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2517 
2518 	/* Check if we are able to exit overflow promiscuous mode. We can
2519 	 * safely exit if we didn't just enter, we no longer have any failed
2520 	 * filters, and we have reduced filters below the threshold value.
2521 	 */
2522 	if (old_overflow && !failed_filters &&
2523 	    vsi->active_filters < vsi->promisc_threshold) {
2524 		dev_info(&pf->pdev->dev,
2525 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2526 			 vsi_name);
2527 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2528 		vsi->promisc_threshold = 0;
2529 	}
2530 
2531 	/* if the VF is not trusted do not do promisc */
2532 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2533 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2534 		goto out;
2535 	}
2536 
2537 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2538 
2539 	/* If we are entering overflow promiscuous, we need to calculate a new
2540 	 * threshold for when we are safe to exit
2541 	 */
2542 	if (!old_overflow && new_overflow)
2543 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2544 
2545 	/* check for changes in promiscuous modes */
2546 	if (changed_flags & IFF_ALLMULTI) {
2547 		bool cur_multipromisc;
2548 
2549 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2550 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2551 							       vsi->seid,
2552 							       cur_multipromisc,
2553 							       NULL);
2554 		if (aq_ret) {
2555 			retval = i40e_aq_rc_to_posix(aq_ret,
2556 						     hw->aq.asq_last_status);
2557 			dev_info(&pf->pdev->dev,
2558 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2559 				 vsi_name,
2560 				 i40e_stat_str(hw, aq_ret),
2561 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2562 		} else {
2563 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2564 				 cur_multipromisc ? "entering" : "leaving");
2565 		}
2566 	}
2567 
2568 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2569 		bool cur_promisc;
2570 
2571 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2572 			       new_overflow);
2573 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2574 		if (aq_ret) {
2575 			retval = i40e_aq_rc_to_posix(aq_ret,
2576 						     hw->aq.asq_last_status);
2577 			dev_info(&pf->pdev->dev,
2578 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2579 				 cur_promisc ? "on" : "off",
2580 				 vsi_name,
2581 				 i40e_stat_str(hw, aq_ret),
2582 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2583 		}
2584 	}
2585 out:
2586 	/* if something went wrong then set the changed flag so we try again */
2587 	if (retval)
2588 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2589 
2590 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2591 	return retval;
2592 
2593 err_no_memory:
2594 	/* Restore elements on the temporary add and delete lists */
2595 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2596 err_no_memory_locked:
2597 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2598 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2599 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2600 
2601 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2602 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2603 	return -ENOMEM;
2604 }
2605 
2606 /**
2607  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2608  * @pf: board private structure
2609  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2610 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2611 {
2612 	int v;
2613 
2614 	if (!pf)
2615 		return;
2616 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2617 		return;
2618 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2619 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2620 		return;
2621 	}
2622 
2623 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2624 		if (pf->vsi[v] &&
2625 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2626 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2627 
2628 			if (ret) {
2629 				/* come back and try again later */
2630 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2631 					pf->state);
2632 				break;
2633 			}
2634 		}
2635 	}
2636 }
2637 
2638 /**
2639  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2640  * @vsi: the vsi
2641  **/
i40e_max_xdp_frame_size(struct i40e_vsi * vsi)2642 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2643 {
2644 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2645 		return I40E_RXBUFFER_2048;
2646 	else
2647 		return I40E_RXBUFFER_3072;
2648 }
2649 
2650 /**
2651  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2652  * @netdev: network interface device structure
2653  * @new_mtu: new value for maximum frame size
2654  *
2655  * Returns 0 on success, negative on failure
2656  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2657 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2658 {
2659 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2660 	struct i40e_vsi *vsi = np->vsi;
2661 	struct i40e_pf *pf = vsi->back;
2662 
2663 	if (i40e_enabled_xdp_vsi(vsi)) {
2664 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2665 
2666 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2667 			return -EINVAL;
2668 	}
2669 
2670 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2671 		   netdev->mtu, new_mtu);
2672 	netdev->mtu = new_mtu;
2673 	if (netif_running(netdev))
2674 		i40e_vsi_reinit_locked(vsi);
2675 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2676 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2677 	return 0;
2678 }
2679 
2680 /**
2681  * i40e_ioctl - Access the hwtstamp interface
2682  * @netdev: network interface device structure
2683  * @ifr: interface request data
2684  * @cmd: ioctl command
2685  **/
i40e_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)2686 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2687 {
2688 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2689 	struct i40e_pf *pf = np->vsi->back;
2690 
2691 	switch (cmd) {
2692 	case SIOCGHWTSTAMP:
2693 		return i40e_ptp_get_ts_config(pf, ifr);
2694 	case SIOCSHWTSTAMP:
2695 		return i40e_ptp_set_ts_config(pf, ifr);
2696 	default:
2697 		return -EOPNOTSUPP;
2698 	}
2699 }
2700 
2701 /**
2702  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2703  * @vsi: the vsi being adjusted
2704  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)2705 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2706 {
2707 	struct i40e_vsi_context ctxt;
2708 	i40e_status ret;
2709 
2710 	/* Don't modify stripping options if a port VLAN is active */
2711 	if (vsi->info.pvid)
2712 		return;
2713 
2714 	if ((vsi->info.valid_sections &
2715 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2716 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2717 		return;  /* already enabled */
2718 
2719 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2720 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2721 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2722 
2723 	ctxt.seid = vsi->seid;
2724 	ctxt.info = vsi->info;
2725 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2726 	if (ret) {
2727 		dev_info(&vsi->back->pdev->dev,
2728 			 "update vlan stripping failed, err %s aq_err %s\n",
2729 			 i40e_stat_str(&vsi->back->hw, ret),
2730 			 i40e_aq_str(&vsi->back->hw,
2731 				     vsi->back->hw.aq.asq_last_status));
2732 	}
2733 }
2734 
2735 /**
2736  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2737  * @vsi: the vsi being adjusted
2738  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)2739 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2740 {
2741 	struct i40e_vsi_context ctxt;
2742 	i40e_status ret;
2743 
2744 	/* Don't modify stripping options if a port VLAN is active */
2745 	if (vsi->info.pvid)
2746 		return;
2747 
2748 	if ((vsi->info.valid_sections &
2749 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2750 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2751 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2752 		return;  /* already disabled */
2753 
2754 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2755 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2756 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2757 
2758 	ctxt.seid = vsi->seid;
2759 	ctxt.info = vsi->info;
2760 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2761 	if (ret) {
2762 		dev_info(&vsi->back->pdev->dev,
2763 			 "update vlan stripping failed, err %s aq_err %s\n",
2764 			 i40e_stat_str(&vsi->back->hw, ret),
2765 			 i40e_aq_str(&vsi->back->hw,
2766 				     vsi->back->hw.aq.asq_last_status));
2767 	}
2768 }
2769 
2770 /**
2771  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2772  * @vsi: the vsi being configured
2773  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2774  *
2775  * This is a helper function for adding a new MAC/VLAN filter with the
2776  * specified VLAN for each existing MAC address already in the hash table.
2777  * This function does *not* perform any accounting to update filters based on
2778  * VLAN mode.
2779  *
2780  * NOTE: this function expects to be called while under the
2781  * mac_filter_hash_lock
2782  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)2783 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2784 {
2785 	struct i40e_mac_filter *f, *add_f;
2786 	struct hlist_node *h;
2787 	int bkt;
2788 
2789 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2790 		if (f->state == I40E_FILTER_REMOVE)
2791 			continue;
2792 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2793 		if (!add_f) {
2794 			dev_info(&vsi->back->pdev->dev,
2795 				 "Could not add vlan filter %d for %pM\n",
2796 				 vid, f->macaddr);
2797 			return -ENOMEM;
2798 		}
2799 	}
2800 
2801 	return 0;
2802 }
2803 
2804 /**
2805  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2806  * @vsi: the VSI being configured
2807  * @vid: VLAN id to be added
2808  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)2809 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2810 {
2811 	int err;
2812 
2813 	if (vsi->info.pvid)
2814 		return -EINVAL;
2815 
2816 	/* The network stack will attempt to add VID=0, with the intention to
2817 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2818 	 * these packets by default when configured to receive untagged
2819 	 * packets, so we don't need to add a filter for this case.
2820 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2821 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2822 	 * Thus, we do not want to actually add a filter for VID=0
2823 	 */
2824 	if (!vid)
2825 		return 0;
2826 
2827 	/* Locked once because all functions invoked below iterates list*/
2828 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2829 	err = i40e_add_vlan_all_mac(vsi, vid);
2830 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2831 	if (err)
2832 		return err;
2833 
2834 	/* schedule our worker thread which will take care of
2835 	 * applying the new filter changes
2836 	 */
2837 	i40e_service_event_schedule(vsi->back);
2838 	return 0;
2839 }
2840 
2841 /**
2842  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2843  * @vsi: the vsi being configured
2844  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2845  *
2846  * This function should be used to remove all VLAN filters which match the
2847  * given VID. It does not schedule the service event and does not take the
2848  * mac_filter_hash_lock so it may be combined with other operations under
2849  * a single invocation of the mac_filter_hash_lock.
2850  *
2851  * NOTE: this function expects to be called while under the
2852  * mac_filter_hash_lock
2853  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)2854 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2855 {
2856 	struct i40e_mac_filter *f;
2857 	struct hlist_node *h;
2858 	int bkt;
2859 
2860 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2861 		if (f->vlan == vid)
2862 			__i40e_del_filter(vsi, f);
2863 	}
2864 }
2865 
2866 /**
2867  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2868  * @vsi: the VSI being configured
2869  * @vid: VLAN id to be removed
2870  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)2871 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2872 {
2873 	if (!vid || vsi->info.pvid)
2874 		return;
2875 
2876 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2877 	i40e_rm_vlan_all_mac(vsi, vid);
2878 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2879 
2880 	/* schedule our worker thread which will take care of
2881 	 * applying the new filter changes
2882 	 */
2883 	i40e_service_event_schedule(vsi->back);
2884 }
2885 
2886 /**
2887  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2888  * @netdev: network interface to be adjusted
2889  * @proto: unused protocol value
2890  * @vid: vlan id to be added
2891  *
2892  * net_device_ops implementation for adding vlan ids
2893  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)2894 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2895 				__always_unused __be16 proto, u16 vid)
2896 {
2897 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2898 	struct i40e_vsi *vsi = np->vsi;
2899 	int ret = 0;
2900 
2901 	if (vid >= VLAN_N_VID)
2902 		return -EINVAL;
2903 
2904 	ret = i40e_vsi_add_vlan(vsi, vid);
2905 	if (!ret)
2906 		set_bit(vid, vsi->active_vlans);
2907 
2908 	return ret;
2909 }
2910 
2911 /**
2912  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2913  * @netdev: network interface to be adjusted
2914  * @proto: unused protocol value
2915  * @vid: vlan id to be added
2916  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)2917 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2918 				    __always_unused __be16 proto, u16 vid)
2919 {
2920 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2921 	struct i40e_vsi *vsi = np->vsi;
2922 
2923 	if (vid >= VLAN_N_VID)
2924 		return;
2925 	set_bit(vid, vsi->active_vlans);
2926 }
2927 
2928 /**
2929  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2930  * @netdev: network interface to be adjusted
2931  * @proto: unused protocol value
2932  * @vid: vlan id to be removed
2933  *
2934  * net_device_ops implementation for removing vlan ids
2935  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)2936 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2937 				 __always_unused __be16 proto, u16 vid)
2938 {
2939 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2940 	struct i40e_vsi *vsi = np->vsi;
2941 
2942 	/* return code is ignored as there is nothing a user
2943 	 * can do about failure to remove and a log message was
2944 	 * already printed from the other function
2945 	 */
2946 	i40e_vsi_kill_vlan(vsi, vid);
2947 
2948 	clear_bit(vid, vsi->active_vlans);
2949 
2950 	return 0;
2951 }
2952 
2953 /**
2954  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2955  * @vsi: the vsi being brought back up
2956  **/
i40e_restore_vlan(struct i40e_vsi * vsi)2957 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2958 {
2959 	u16 vid;
2960 
2961 	if (!vsi->netdev)
2962 		return;
2963 
2964 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2965 		i40e_vlan_stripping_enable(vsi);
2966 	else
2967 		i40e_vlan_stripping_disable(vsi);
2968 
2969 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2970 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2971 					vid);
2972 }
2973 
2974 /**
2975  * i40e_vsi_add_pvid - Add pvid for the VSI
2976  * @vsi: the vsi being adjusted
2977  * @vid: the vlan id to set as a PVID
2978  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)2979 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2980 {
2981 	struct i40e_vsi_context ctxt;
2982 	i40e_status ret;
2983 
2984 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2985 	vsi->info.pvid = cpu_to_le16(vid);
2986 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2987 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2988 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2989 
2990 	ctxt.seid = vsi->seid;
2991 	ctxt.info = vsi->info;
2992 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2993 	if (ret) {
2994 		dev_info(&vsi->back->pdev->dev,
2995 			 "add pvid failed, err %s aq_err %s\n",
2996 			 i40e_stat_str(&vsi->back->hw, ret),
2997 			 i40e_aq_str(&vsi->back->hw,
2998 				     vsi->back->hw.aq.asq_last_status));
2999 		return -ENOENT;
3000 	}
3001 
3002 	return 0;
3003 }
3004 
3005 /**
3006  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3007  * @vsi: the vsi being adjusted
3008  *
3009  * Just use the vlan_rx_register() service to put it back to normal
3010  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3011 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3012 {
3013 	vsi->info.pvid = 0;
3014 
3015 	i40e_vlan_stripping_disable(vsi);
3016 }
3017 
3018 /**
3019  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3020  * @vsi: ptr to the VSI
3021  *
3022  * If this function returns with an error, then it's possible one or
3023  * more of the rings is populated (while the rest are not).  It is the
3024  * callers duty to clean those orphaned rings.
3025  *
3026  * Return 0 on success, negative on failure
3027  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3028 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3029 {
3030 	int i, err = 0;
3031 
3032 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3033 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3034 
3035 	if (!i40e_enabled_xdp_vsi(vsi))
3036 		return err;
3037 
3038 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3039 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3040 
3041 	return err;
3042 }
3043 
3044 /**
3045  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3046  * @vsi: ptr to the VSI
3047  *
3048  * Free VSI's transmit software resources
3049  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3050 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3051 {
3052 	int i;
3053 
3054 	if (vsi->tx_rings) {
3055 		for (i = 0; i < vsi->num_queue_pairs; i++)
3056 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3057 				i40e_free_tx_resources(vsi->tx_rings[i]);
3058 	}
3059 
3060 	if (vsi->xdp_rings) {
3061 		for (i = 0; i < vsi->num_queue_pairs; i++)
3062 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3063 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3064 	}
3065 }
3066 
3067 /**
3068  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3069  * @vsi: ptr to the VSI
3070  *
3071  * If this function returns with an error, then it's possible one or
3072  * more of the rings is populated (while the rest are not).  It is the
3073  * callers duty to clean those orphaned rings.
3074  *
3075  * Return 0 on success, negative on failure
3076  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3077 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3078 {
3079 	int i, err = 0;
3080 
3081 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3082 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3083 	return err;
3084 }
3085 
3086 /**
3087  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3088  * @vsi: ptr to the VSI
3089  *
3090  * Free all receive software resources
3091  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3092 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3093 {
3094 	int i;
3095 
3096 	if (!vsi->rx_rings)
3097 		return;
3098 
3099 	for (i = 0; i < vsi->num_queue_pairs; i++)
3100 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3101 			i40e_free_rx_resources(vsi->rx_rings[i]);
3102 }
3103 
3104 /**
3105  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3106  * @ring: The Tx ring to configure
3107  *
3108  * This enables/disables XPS for a given Tx descriptor ring
3109  * based on the TCs enabled for the VSI that ring belongs to.
3110  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3111 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3112 {
3113 	int cpu;
3114 
3115 	if (!ring->q_vector || !ring->netdev || ring->ch)
3116 		return;
3117 
3118 	/* We only initialize XPS once, so as not to overwrite user settings */
3119 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3120 		return;
3121 
3122 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3123 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3124 			    ring->queue_index);
3125 }
3126 
3127 /**
3128  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3129  * @ring: The Tx or Rx ring
3130  *
3131  * Returns the AF_XDP buffer pool or NULL.
3132  **/
i40e_xsk_pool(struct i40e_ring * ring)3133 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3134 {
3135 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3136 	int qid = ring->queue_index;
3137 
3138 	if (ring_is_xdp(ring))
3139 		qid -= ring->vsi->alloc_queue_pairs;
3140 
3141 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3142 		return NULL;
3143 
3144 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3145 }
3146 
3147 /**
3148  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3149  * @ring: The Tx ring to configure
3150  *
3151  * Configure the Tx descriptor ring in the HMC context.
3152  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3153 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3154 {
3155 	struct i40e_vsi *vsi = ring->vsi;
3156 	u16 pf_q = vsi->base_queue + ring->queue_index;
3157 	struct i40e_hw *hw = &vsi->back->hw;
3158 	struct i40e_hmc_obj_txq tx_ctx;
3159 	i40e_status err = 0;
3160 	u32 qtx_ctl = 0;
3161 
3162 	if (ring_is_xdp(ring))
3163 		ring->xsk_pool = i40e_xsk_pool(ring);
3164 
3165 	/* some ATR related tx ring init */
3166 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3167 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3168 		ring->atr_count = 0;
3169 	} else {
3170 		ring->atr_sample_rate = 0;
3171 	}
3172 
3173 	/* configure XPS */
3174 	i40e_config_xps_tx_ring(ring);
3175 
3176 	/* clear the context structure first */
3177 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3178 
3179 	tx_ctx.new_context = 1;
3180 	tx_ctx.base = (ring->dma / 128);
3181 	tx_ctx.qlen = ring->count;
3182 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3183 					       I40E_FLAG_FD_ATR_ENABLED));
3184 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3185 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3186 	if (vsi->type != I40E_VSI_FDIR)
3187 		tx_ctx.head_wb_ena = 1;
3188 	tx_ctx.head_wb_addr = ring->dma +
3189 			      (ring->count * sizeof(struct i40e_tx_desc));
3190 
3191 	/* As part of VSI creation/update, FW allocates certain
3192 	 * Tx arbitration queue sets for each TC enabled for
3193 	 * the VSI. The FW returns the handles to these queue
3194 	 * sets as part of the response buffer to Add VSI,
3195 	 * Update VSI, etc. AQ commands. It is expected that
3196 	 * these queue set handles be associated with the Tx
3197 	 * queues by the driver as part of the TX queue context
3198 	 * initialization. This has to be done regardless of
3199 	 * DCB as by default everything is mapped to TC0.
3200 	 */
3201 
3202 	if (ring->ch)
3203 		tx_ctx.rdylist =
3204 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3205 
3206 	else
3207 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3208 
3209 	tx_ctx.rdylist_act = 0;
3210 
3211 	/* clear the context in the HMC */
3212 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3213 	if (err) {
3214 		dev_info(&vsi->back->pdev->dev,
3215 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3216 			 ring->queue_index, pf_q, err);
3217 		return -ENOMEM;
3218 	}
3219 
3220 	/* set the context in the HMC */
3221 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3222 	if (err) {
3223 		dev_info(&vsi->back->pdev->dev,
3224 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3225 			 ring->queue_index, pf_q, err);
3226 		return -ENOMEM;
3227 	}
3228 
3229 	/* Now associate this queue with this PCI function */
3230 	if (ring->ch) {
3231 		if (ring->ch->type == I40E_VSI_VMDQ2)
3232 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3233 		else
3234 			return -EINVAL;
3235 
3236 		qtx_ctl |= (ring->ch->vsi_number <<
3237 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3238 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3239 	} else {
3240 		if (vsi->type == I40E_VSI_VMDQ2) {
3241 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3242 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3243 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3244 		} else {
3245 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3246 		}
3247 	}
3248 
3249 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3250 		    I40E_QTX_CTL_PF_INDX_MASK);
3251 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3252 	i40e_flush(hw);
3253 
3254 	/* cache tail off for easier writes later */
3255 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3256 
3257 	return 0;
3258 }
3259 
3260 /**
3261  * i40e_configure_rx_ring - Configure a receive ring context
3262  * @ring: The Rx ring to configure
3263  *
3264  * Configure the Rx descriptor ring in the HMC context.
3265  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3266 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3267 {
3268 	struct i40e_vsi *vsi = ring->vsi;
3269 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3270 	u16 pf_q = vsi->base_queue + ring->queue_index;
3271 	struct i40e_hw *hw = &vsi->back->hw;
3272 	struct i40e_hmc_obj_rxq rx_ctx;
3273 	i40e_status err = 0;
3274 	bool ok;
3275 	int ret;
3276 
3277 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3278 
3279 	/* clear the context structure first */
3280 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3281 
3282 	if (ring->vsi->type == I40E_VSI_MAIN)
3283 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3284 
3285 	kfree(ring->rx_bi);
3286 	ring->xsk_pool = i40e_xsk_pool(ring);
3287 	if (ring->xsk_pool) {
3288 		ret = i40e_alloc_rx_bi_zc(ring);
3289 		if (ret)
3290 			return ret;
3291 		ring->rx_buf_len =
3292 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3293 		/* For AF_XDP ZC, we disallow packets to span on
3294 		 * multiple buffers, thus letting us skip that
3295 		 * handling in the fast-path.
3296 		 */
3297 		chain_len = 1;
3298 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3299 						 MEM_TYPE_XSK_BUFF_POOL,
3300 						 NULL);
3301 		if (ret)
3302 			return ret;
3303 		dev_info(&vsi->back->pdev->dev,
3304 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3305 			 ring->queue_index);
3306 
3307 	} else {
3308 		ret = i40e_alloc_rx_bi(ring);
3309 		if (ret)
3310 			return ret;
3311 		ring->rx_buf_len = vsi->rx_buf_len;
3312 		if (ring->vsi->type == I40E_VSI_MAIN) {
3313 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3314 							 MEM_TYPE_PAGE_SHARED,
3315 							 NULL);
3316 			if (ret)
3317 				return ret;
3318 		}
3319 	}
3320 
3321 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3322 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3323 
3324 	rx_ctx.base = (ring->dma / 128);
3325 	rx_ctx.qlen = ring->count;
3326 
3327 	/* use 16 byte descriptors */
3328 	rx_ctx.dsize = 0;
3329 
3330 	/* descriptor type is always zero
3331 	 * rx_ctx.dtype = 0;
3332 	 */
3333 	rx_ctx.hsplit_0 = 0;
3334 
3335 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3336 	if (hw->revision_id == 0)
3337 		rx_ctx.lrxqthresh = 0;
3338 	else
3339 		rx_ctx.lrxqthresh = 1;
3340 	rx_ctx.crcstrip = 1;
3341 	rx_ctx.l2tsel = 1;
3342 	/* this controls whether VLAN is stripped from inner headers */
3343 	rx_ctx.showiv = 0;
3344 	/* set the prefena field to 1 because the manual says to */
3345 	rx_ctx.prefena = 1;
3346 
3347 	/* clear the context in the HMC */
3348 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3349 	if (err) {
3350 		dev_info(&vsi->back->pdev->dev,
3351 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3352 			 ring->queue_index, pf_q, err);
3353 		return -ENOMEM;
3354 	}
3355 
3356 	/* set the context in the HMC */
3357 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3358 	if (err) {
3359 		dev_info(&vsi->back->pdev->dev,
3360 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3361 			 ring->queue_index, pf_q, err);
3362 		return -ENOMEM;
3363 	}
3364 
3365 	/* configure Rx buffer alignment */
3366 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3367 		clear_ring_build_skb_enabled(ring);
3368 	else
3369 		set_ring_build_skb_enabled(ring);
3370 
3371 	/* cache tail for quicker writes, and clear the reg before use */
3372 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3373 	writel(0, ring->tail);
3374 
3375 	if (ring->xsk_pool) {
3376 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3377 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3378 	} else {
3379 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3380 	}
3381 	if (!ok) {
3382 		/* Log this in case the user has forgotten to give the kernel
3383 		 * any buffers, even later in the application.
3384 		 */
3385 		dev_info(&vsi->back->pdev->dev,
3386 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3387 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3388 			 ring->queue_index, pf_q);
3389 	}
3390 
3391 	return 0;
3392 }
3393 
3394 /**
3395  * i40e_vsi_configure_tx - Configure the VSI for Tx
3396  * @vsi: VSI structure describing this set of rings and resources
3397  *
3398  * Configure the Tx VSI for operation.
3399  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3400 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3401 {
3402 	int err = 0;
3403 	u16 i;
3404 
3405 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3406 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3407 
3408 	if (err || !i40e_enabled_xdp_vsi(vsi))
3409 		return err;
3410 
3411 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3412 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3413 
3414 	return err;
3415 }
3416 
3417 /**
3418  * i40e_vsi_configure_rx - Configure the VSI for Rx
3419  * @vsi: the VSI being configured
3420  *
3421  * Configure the Rx VSI for operation.
3422  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3423 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3424 {
3425 	int err = 0;
3426 	u16 i;
3427 
3428 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3429 		vsi->max_frame = I40E_MAX_RXBUFFER;
3430 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3431 #if (PAGE_SIZE < 8192)
3432 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3433 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3434 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3435 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3436 #endif
3437 	} else {
3438 		vsi->max_frame = I40E_MAX_RXBUFFER;
3439 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3440 						       I40E_RXBUFFER_2048;
3441 	}
3442 
3443 	/* set up individual rings */
3444 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3445 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3446 
3447 	return err;
3448 }
3449 
3450 /**
3451  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3452  * @vsi: ptr to the VSI
3453  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3454 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3455 {
3456 	struct i40e_ring *tx_ring, *rx_ring;
3457 	u16 qoffset, qcount;
3458 	int i, n;
3459 
3460 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3461 		/* Reset the TC information */
3462 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3463 			rx_ring = vsi->rx_rings[i];
3464 			tx_ring = vsi->tx_rings[i];
3465 			rx_ring->dcb_tc = 0;
3466 			tx_ring->dcb_tc = 0;
3467 		}
3468 		return;
3469 	}
3470 
3471 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3472 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3473 			continue;
3474 
3475 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3476 		qcount = vsi->tc_config.tc_info[n].qcount;
3477 		for (i = qoffset; i < (qoffset + qcount); i++) {
3478 			rx_ring = vsi->rx_rings[i];
3479 			tx_ring = vsi->tx_rings[i];
3480 			rx_ring->dcb_tc = n;
3481 			tx_ring->dcb_tc = n;
3482 		}
3483 	}
3484 }
3485 
3486 /**
3487  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3488  * @vsi: ptr to the VSI
3489  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3490 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3491 {
3492 	if (vsi->netdev)
3493 		i40e_set_rx_mode(vsi->netdev);
3494 }
3495 
3496 /**
3497  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3498  * @vsi: Pointer to the targeted VSI
3499  *
3500  * This function replays the hlist on the hw where all the SB Flow Director
3501  * filters were saved.
3502  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3503 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3504 {
3505 	struct i40e_fdir_filter *filter;
3506 	struct i40e_pf *pf = vsi->back;
3507 	struct hlist_node *node;
3508 
3509 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3510 		return;
3511 
3512 	/* Reset FDir counters as we're replaying all existing filters */
3513 	pf->fd_tcp4_filter_cnt = 0;
3514 	pf->fd_udp4_filter_cnt = 0;
3515 	pf->fd_sctp4_filter_cnt = 0;
3516 	pf->fd_ip4_filter_cnt = 0;
3517 
3518 	hlist_for_each_entry_safe(filter, node,
3519 				  &pf->fdir_filter_list, fdir_node) {
3520 		i40e_add_del_fdir(vsi, filter, true);
3521 	}
3522 }
3523 
3524 /**
3525  * i40e_vsi_configure - Set up the VSI for action
3526  * @vsi: the VSI being configured
3527  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3528 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3529 {
3530 	int err;
3531 
3532 	i40e_set_vsi_rx_mode(vsi);
3533 	i40e_restore_vlan(vsi);
3534 	i40e_vsi_config_dcb_rings(vsi);
3535 	err = i40e_vsi_configure_tx(vsi);
3536 	if (!err)
3537 		err = i40e_vsi_configure_rx(vsi);
3538 
3539 	return err;
3540 }
3541 
3542 /**
3543  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3544  * @vsi: the VSI being configured
3545  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3546 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3547 {
3548 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3549 	struct i40e_pf *pf = vsi->back;
3550 	struct i40e_hw *hw = &pf->hw;
3551 	u16 vector;
3552 	int i, q;
3553 	u32 qp;
3554 
3555 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3556 	 * and PFINT_LNKLSTn registers, e.g.:
3557 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3558 	 */
3559 	qp = vsi->base_queue;
3560 	vector = vsi->base_vector;
3561 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3562 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3563 
3564 		q_vector->rx.next_update = jiffies + 1;
3565 		q_vector->rx.target_itr =
3566 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3567 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3568 		     q_vector->rx.target_itr >> 1);
3569 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3570 
3571 		q_vector->tx.next_update = jiffies + 1;
3572 		q_vector->tx.target_itr =
3573 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3574 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3575 		     q_vector->tx.target_itr >> 1);
3576 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3577 
3578 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3579 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3580 
3581 		/* Linked list for the queuepairs assigned to this vector */
3582 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3583 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3584 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3585 			u32 val;
3586 
3587 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3588 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3589 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3590 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3591 			      (I40E_QUEUE_TYPE_TX <<
3592 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3593 
3594 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3595 
3596 			if (has_xdp) {
3597 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3598 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3599 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3600 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3601 				      (I40E_QUEUE_TYPE_TX <<
3602 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3603 
3604 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3605 			}
3606 
3607 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3608 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3609 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3610 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3611 			      (I40E_QUEUE_TYPE_RX <<
3612 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3613 
3614 			/* Terminate the linked list */
3615 			if (q == (q_vector->num_ringpairs - 1))
3616 				val |= (I40E_QUEUE_END_OF_LIST <<
3617 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3618 
3619 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3620 			qp++;
3621 		}
3622 	}
3623 
3624 	i40e_flush(hw);
3625 }
3626 
3627 /**
3628  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3629  * @pf: pointer to private device data structure
3630  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3631 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3632 {
3633 	struct i40e_hw *hw = &pf->hw;
3634 	u32 val;
3635 
3636 	/* clear things first */
3637 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3638 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3639 
3640 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3641 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3642 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3643 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3644 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3645 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3646 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3647 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3648 
3649 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3650 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3651 
3652 	if (pf->flags & I40E_FLAG_PTP)
3653 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3654 
3655 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3656 
3657 	/* SW_ITR_IDX = 0, but don't change INTENA */
3658 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3659 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3660 
3661 	/* OTHER_ITR_IDX = 0 */
3662 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3663 }
3664 
3665 /**
3666  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3667  * @vsi: the VSI being configured
3668  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)3669 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3670 {
3671 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3672 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3673 	struct i40e_pf *pf = vsi->back;
3674 	struct i40e_hw *hw = &pf->hw;
3675 	u32 val;
3676 
3677 	/* set the ITR configuration */
3678 	q_vector->rx.next_update = jiffies + 1;
3679 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3680 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3681 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3682 	q_vector->tx.next_update = jiffies + 1;
3683 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3684 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3685 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3686 
3687 	i40e_enable_misc_int_causes(pf);
3688 
3689 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3690 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3691 
3692 	/* Associate the queue pair to the vector and enable the queue int */
3693 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3694 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3695 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3696 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3697 
3698 	wr32(hw, I40E_QINT_RQCTL(0), val);
3699 
3700 	if (i40e_enabled_xdp_vsi(vsi)) {
3701 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3702 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3703 		      (I40E_QUEUE_TYPE_TX
3704 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3705 
3706 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3707 	}
3708 
3709 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3710 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3711 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3712 
3713 	wr32(hw, I40E_QINT_TQCTL(0), val);
3714 	i40e_flush(hw);
3715 }
3716 
3717 /**
3718  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3719  * @pf: board private structure
3720  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)3721 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3722 {
3723 	struct i40e_hw *hw = &pf->hw;
3724 
3725 	wr32(hw, I40E_PFINT_DYN_CTL0,
3726 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3727 	i40e_flush(hw);
3728 }
3729 
3730 /**
3731  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3732  * @pf: board private structure
3733  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)3734 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3735 {
3736 	struct i40e_hw *hw = &pf->hw;
3737 	u32 val;
3738 
3739 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3740 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3741 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3742 
3743 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3744 	i40e_flush(hw);
3745 }
3746 
3747 /**
3748  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3749  * @irq: interrupt number
3750  * @data: pointer to a q_vector
3751  **/
i40e_msix_clean_rings(int irq,void * data)3752 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3753 {
3754 	struct i40e_q_vector *q_vector = data;
3755 
3756 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3757 		return IRQ_HANDLED;
3758 
3759 	napi_schedule_irqoff(&q_vector->napi);
3760 
3761 	return IRQ_HANDLED;
3762 }
3763 
3764 /**
3765  * i40e_irq_affinity_notify - Callback for affinity changes
3766  * @notify: context as to what irq was changed
3767  * @mask: the new affinity mask
3768  *
3769  * This is a callback function used by the irq_set_affinity_notifier function
3770  * so that we may register to receive changes to the irq affinity masks.
3771  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)3772 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3773 				     const cpumask_t *mask)
3774 {
3775 	struct i40e_q_vector *q_vector =
3776 		container_of(notify, struct i40e_q_vector, affinity_notify);
3777 
3778 	cpumask_copy(&q_vector->affinity_mask, mask);
3779 }
3780 
3781 /**
3782  * i40e_irq_affinity_release - Callback for affinity notifier release
3783  * @ref: internal core kernel usage
3784  *
3785  * This is a callback function used by the irq_set_affinity_notifier function
3786  * to inform the current notification subscriber that they will no longer
3787  * receive notifications.
3788  **/
i40e_irq_affinity_release(struct kref * ref)3789 static void i40e_irq_affinity_release(struct kref *ref) {}
3790 
3791 /**
3792  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3793  * @vsi: the VSI being configured
3794  * @basename: name for the vector
3795  *
3796  * Allocates MSI-X vectors and requests interrupts from the kernel.
3797  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)3798 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3799 {
3800 	int q_vectors = vsi->num_q_vectors;
3801 	struct i40e_pf *pf = vsi->back;
3802 	int base = vsi->base_vector;
3803 	int rx_int_idx = 0;
3804 	int tx_int_idx = 0;
3805 	int vector, err;
3806 	int irq_num;
3807 	int cpu;
3808 
3809 	for (vector = 0; vector < q_vectors; vector++) {
3810 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3811 
3812 		irq_num = pf->msix_entries[base + vector].vector;
3813 
3814 		if (q_vector->tx.ring && q_vector->rx.ring) {
3815 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3816 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3817 			tx_int_idx++;
3818 		} else if (q_vector->rx.ring) {
3819 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3820 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3821 		} else if (q_vector->tx.ring) {
3822 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3823 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3824 		} else {
3825 			/* skip this unused q_vector */
3826 			continue;
3827 		}
3828 		err = request_irq(irq_num,
3829 				  vsi->irq_handler,
3830 				  0,
3831 				  q_vector->name,
3832 				  q_vector);
3833 		if (err) {
3834 			dev_info(&pf->pdev->dev,
3835 				 "MSIX request_irq failed, error: %d\n", err);
3836 			goto free_queue_irqs;
3837 		}
3838 
3839 		/* register for affinity change notifications */
3840 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3841 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3842 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3843 		/* Spread affinity hints out across online CPUs.
3844 		 *
3845 		 * get_cpu_mask returns a static constant mask with
3846 		 * a permanent lifetime so it's ok to pass to
3847 		 * irq_set_affinity_hint without making a copy.
3848 		 */
3849 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3850 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3851 	}
3852 
3853 	vsi->irqs_ready = true;
3854 	return 0;
3855 
3856 free_queue_irqs:
3857 	while (vector) {
3858 		vector--;
3859 		irq_num = pf->msix_entries[base + vector].vector;
3860 		irq_set_affinity_notifier(irq_num, NULL);
3861 		irq_set_affinity_hint(irq_num, NULL);
3862 		free_irq(irq_num, &vsi->q_vectors[vector]);
3863 	}
3864 	return err;
3865 }
3866 
3867 /**
3868  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3869  * @vsi: the VSI being un-configured
3870  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)3871 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3872 {
3873 	struct i40e_pf *pf = vsi->back;
3874 	struct i40e_hw *hw = &pf->hw;
3875 	int base = vsi->base_vector;
3876 	int i;
3877 
3878 	/* disable interrupt causation from each queue */
3879 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3880 		u32 val;
3881 
3882 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3883 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3884 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3885 
3886 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3887 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3888 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3889 
3890 		if (!i40e_enabled_xdp_vsi(vsi))
3891 			continue;
3892 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3893 	}
3894 
3895 	/* disable each interrupt */
3896 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3897 		for (i = vsi->base_vector;
3898 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3899 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3900 
3901 		i40e_flush(hw);
3902 		for (i = 0; i < vsi->num_q_vectors; i++)
3903 			synchronize_irq(pf->msix_entries[i + base].vector);
3904 	} else {
3905 		/* Legacy and MSI mode - this stops all interrupt handling */
3906 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3907 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3908 		i40e_flush(hw);
3909 		synchronize_irq(pf->pdev->irq);
3910 	}
3911 }
3912 
3913 /**
3914  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3915  * @vsi: the VSI being configured
3916  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)3917 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3918 {
3919 	struct i40e_pf *pf = vsi->back;
3920 	int i;
3921 
3922 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3923 		for (i = 0; i < vsi->num_q_vectors; i++)
3924 			i40e_irq_dynamic_enable(vsi, i);
3925 	} else {
3926 		i40e_irq_dynamic_enable_icr0(pf);
3927 	}
3928 
3929 	i40e_flush(&pf->hw);
3930 	return 0;
3931 }
3932 
3933 /**
3934  * i40e_free_misc_vector - Free the vector that handles non-queue events
3935  * @pf: board private structure
3936  **/
i40e_free_misc_vector(struct i40e_pf * pf)3937 static void i40e_free_misc_vector(struct i40e_pf *pf)
3938 {
3939 	/* Disable ICR 0 */
3940 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3941 	i40e_flush(&pf->hw);
3942 
3943 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3944 		synchronize_irq(pf->msix_entries[0].vector);
3945 		free_irq(pf->msix_entries[0].vector, pf);
3946 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3947 	}
3948 }
3949 
3950 /**
3951  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3952  * @irq: interrupt number
3953  * @data: pointer to a q_vector
3954  *
3955  * This is the handler used for all MSI/Legacy interrupts, and deals
3956  * with both queue and non-queue interrupts.  This is also used in
3957  * MSIX mode to handle the non-queue interrupts.
3958  **/
i40e_intr(int irq,void * data)3959 static irqreturn_t i40e_intr(int irq, void *data)
3960 {
3961 	struct i40e_pf *pf = (struct i40e_pf *)data;
3962 	struct i40e_hw *hw = &pf->hw;
3963 	irqreturn_t ret = IRQ_NONE;
3964 	u32 icr0, icr0_remaining;
3965 	u32 val, ena_mask;
3966 
3967 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3968 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3969 
3970 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
3971 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3972 		goto enable_intr;
3973 
3974 	/* if interrupt but no bits showing, must be SWINT */
3975 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3976 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3977 		pf->sw_int_count++;
3978 
3979 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3980 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3981 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3982 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3983 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3984 	}
3985 
3986 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3987 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3988 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3989 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3990 
3991 		/* We do not have a way to disarm Queue causes while leaving
3992 		 * interrupt enabled for all other causes, ideally
3993 		 * interrupt should be disabled while we are in NAPI but
3994 		 * this is not a performance path and napi_schedule()
3995 		 * can deal with rescheduling.
3996 		 */
3997 		if (!test_bit(__I40E_DOWN, pf->state))
3998 			napi_schedule_irqoff(&q_vector->napi);
3999 	}
4000 
4001 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4002 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4003 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4004 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4005 	}
4006 
4007 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4008 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4009 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4010 	}
4011 
4012 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4013 		/* disable any further VFLR event notifications */
4014 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4015 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4016 
4017 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4018 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4019 		} else {
4020 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4021 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4022 		}
4023 	}
4024 
4025 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4026 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4027 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4028 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4029 		val = rd32(hw, I40E_GLGEN_RSTAT);
4030 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4031 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4032 		if (val == I40E_RESET_CORER) {
4033 			pf->corer_count++;
4034 		} else if (val == I40E_RESET_GLOBR) {
4035 			pf->globr_count++;
4036 		} else if (val == I40E_RESET_EMPR) {
4037 			pf->empr_count++;
4038 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4039 		}
4040 	}
4041 
4042 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4043 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4044 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4045 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4046 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4047 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4048 	}
4049 
4050 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4051 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4052 
4053 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
4054 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4055 			i40e_ptp_tx_hwtstamp(pf);
4056 		}
4057 	}
4058 
4059 	/* If a critical error is pending we have no choice but to reset the
4060 	 * device.
4061 	 * Report and mask out any remaining unexpected interrupts.
4062 	 */
4063 	icr0_remaining = icr0 & ena_mask;
4064 	if (icr0_remaining) {
4065 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4066 			 icr0_remaining);
4067 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4068 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4069 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4070 			dev_info(&pf->pdev->dev, "device will be reset\n");
4071 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4072 			i40e_service_event_schedule(pf);
4073 		}
4074 		ena_mask &= ~icr0_remaining;
4075 	}
4076 	ret = IRQ_HANDLED;
4077 
4078 enable_intr:
4079 	/* re-enable interrupt causes */
4080 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4081 	if (!test_bit(__I40E_DOWN, pf->state) ||
4082 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4083 		i40e_service_event_schedule(pf);
4084 		i40e_irq_dynamic_enable_icr0(pf);
4085 	}
4086 
4087 	return ret;
4088 }
4089 
4090 /**
4091  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4092  * @tx_ring:  tx ring to clean
4093  * @budget:   how many cleans we're allowed
4094  *
4095  * Returns true if there's any budget left (e.g. the clean is finished)
4096  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4097 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4098 {
4099 	struct i40e_vsi *vsi = tx_ring->vsi;
4100 	u16 i = tx_ring->next_to_clean;
4101 	struct i40e_tx_buffer *tx_buf;
4102 	struct i40e_tx_desc *tx_desc;
4103 
4104 	tx_buf = &tx_ring->tx_bi[i];
4105 	tx_desc = I40E_TX_DESC(tx_ring, i);
4106 	i -= tx_ring->count;
4107 
4108 	do {
4109 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4110 
4111 		/* if next_to_watch is not set then there is no work pending */
4112 		if (!eop_desc)
4113 			break;
4114 
4115 		/* prevent any other reads prior to eop_desc */
4116 		smp_rmb();
4117 
4118 		/* if the descriptor isn't done, no work yet to do */
4119 		if (!(eop_desc->cmd_type_offset_bsz &
4120 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4121 			break;
4122 
4123 		/* clear next_to_watch to prevent false hangs */
4124 		tx_buf->next_to_watch = NULL;
4125 
4126 		tx_desc->buffer_addr = 0;
4127 		tx_desc->cmd_type_offset_bsz = 0;
4128 		/* move past filter desc */
4129 		tx_buf++;
4130 		tx_desc++;
4131 		i++;
4132 		if (unlikely(!i)) {
4133 			i -= tx_ring->count;
4134 			tx_buf = tx_ring->tx_bi;
4135 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4136 		}
4137 		/* unmap skb header data */
4138 		dma_unmap_single(tx_ring->dev,
4139 				 dma_unmap_addr(tx_buf, dma),
4140 				 dma_unmap_len(tx_buf, len),
4141 				 DMA_TO_DEVICE);
4142 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4143 			kfree(tx_buf->raw_buf);
4144 
4145 		tx_buf->raw_buf = NULL;
4146 		tx_buf->tx_flags = 0;
4147 		tx_buf->next_to_watch = NULL;
4148 		dma_unmap_len_set(tx_buf, len, 0);
4149 		tx_desc->buffer_addr = 0;
4150 		tx_desc->cmd_type_offset_bsz = 0;
4151 
4152 		/* move us past the eop_desc for start of next FD desc */
4153 		tx_buf++;
4154 		tx_desc++;
4155 		i++;
4156 		if (unlikely(!i)) {
4157 			i -= tx_ring->count;
4158 			tx_buf = tx_ring->tx_bi;
4159 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4160 		}
4161 
4162 		/* update budget accounting */
4163 		budget--;
4164 	} while (likely(budget));
4165 
4166 	i += tx_ring->count;
4167 	tx_ring->next_to_clean = i;
4168 
4169 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4170 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4171 
4172 	return budget > 0;
4173 }
4174 
4175 /**
4176  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4177  * @irq: interrupt number
4178  * @data: pointer to a q_vector
4179  **/
i40e_fdir_clean_ring(int irq,void * data)4180 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4181 {
4182 	struct i40e_q_vector *q_vector = data;
4183 	struct i40e_vsi *vsi;
4184 
4185 	if (!q_vector->tx.ring)
4186 		return IRQ_HANDLED;
4187 
4188 	vsi = q_vector->tx.ring->vsi;
4189 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4190 
4191 	return IRQ_HANDLED;
4192 }
4193 
4194 /**
4195  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4196  * @vsi: the VSI being configured
4197  * @v_idx: vector index
4198  * @qp_idx: queue pair index
4199  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4200 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4201 {
4202 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4203 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4204 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4205 
4206 	tx_ring->q_vector = q_vector;
4207 	tx_ring->next = q_vector->tx.ring;
4208 	q_vector->tx.ring = tx_ring;
4209 	q_vector->tx.count++;
4210 
4211 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4212 	if (i40e_enabled_xdp_vsi(vsi)) {
4213 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4214 
4215 		xdp_ring->q_vector = q_vector;
4216 		xdp_ring->next = q_vector->tx.ring;
4217 		q_vector->tx.ring = xdp_ring;
4218 		q_vector->tx.count++;
4219 	}
4220 
4221 	rx_ring->q_vector = q_vector;
4222 	rx_ring->next = q_vector->rx.ring;
4223 	q_vector->rx.ring = rx_ring;
4224 	q_vector->rx.count++;
4225 }
4226 
4227 /**
4228  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4229  * @vsi: the VSI being configured
4230  *
4231  * This function maps descriptor rings to the queue-specific vectors
4232  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4233  * one vector per queue pair, but on a constrained vector budget, we
4234  * group the queue pairs as "efficiently" as possible.
4235  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4236 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4237 {
4238 	int qp_remaining = vsi->num_queue_pairs;
4239 	int q_vectors = vsi->num_q_vectors;
4240 	int num_ringpairs;
4241 	int v_start = 0;
4242 	int qp_idx = 0;
4243 
4244 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4245 	 * group them so there are multiple queues per vector.
4246 	 * It is also important to go through all the vectors available to be
4247 	 * sure that if we don't use all the vectors, that the remaining vectors
4248 	 * are cleared. This is especially important when decreasing the
4249 	 * number of queues in use.
4250 	 */
4251 	for (; v_start < q_vectors; v_start++) {
4252 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4253 
4254 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4255 
4256 		q_vector->num_ringpairs = num_ringpairs;
4257 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4258 
4259 		q_vector->rx.count = 0;
4260 		q_vector->tx.count = 0;
4261 		q_vector->rx.ring = NULL;
4262 		q_vector->tx.ring = NULL;
4263 
4264 		while (num_ringpairs--) {
4265 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4266 			qp_idx++;
4267 			qp_remaining--;
4268 		}
4269 	}
4270 }
4271 
4272 /**
4273  * i40e_vsi_request_irq - Request IRQ from the OS
4274  * @vsi: the VSI being configured
4275  * @basename: name for the vector
4276  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4277 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4278 {
4279 	struct i40e_pf *pf = vsi->back;
4280 	int err;
4281 
4282 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4283 		err = i40e_vsi_request_irq_msix(vsi, basename);
4284 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4285 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4286 				  pf->int_name, pf);
4287 	else
4288 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4289 				  pf->int_name, pf);
4290 
4291 	if (err)
4292 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4293 
4294 	return err;
4295 }
4296 
4297 #ifdef CONFIG_NET_POLL_CONTROLLER
4298 /**
4299  * i40e_netpoll - A Polling 'interrupt' handler
4300  * @netdev: network interface device structure
4301  *
4302  * This is used by netconsole to send skbs without having to re-enable
4303  * interrupts.  It's not called while the normal interrupt routine is executing.
4304  **/
i40e_netpoll(struct net_device * netdev)4305 static void i40e_netpoll(struct net_device *netdev)
4306 {
4307 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4308 	struct i40e_vsi *vsi = np->vsi;
4309 	struct i40e_pf *pf = vsi->back;
4310 	int i;
4311 
4312 	/* if interface is down do nothing */
4313 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4314 		return;
4315 
4316 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4317 		for (i = 0; i < vsi->num_q_vectors; i++)
4318 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4319 	} else {
4320 		i40e_intr(pf->pdev->irq, netdev);
4321 	}
4322 }
4323 #endif
4324 
4325 #define I40E_QTX_ENA_WAIT_COUNT 50
4326 
4327 /**
4328  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4329  * @pf: the PF being configured
4330  * @pf_q: the PF queue
4331  * @enable: enable or disable state of the queue
4332  *
4333  * This routine will wait for the given Tx queue of the PF to reach the
4334  * enabled or disabled state.
4335  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4336  * multiple retries; else will return 0 in case of success.
4337  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4338 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4339 {
4340 	int i;
4341 	u32 tx_reg;
4342 
4343 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4344 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4345 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4346 			break;
4347 
4348 		usleep_range(10, 20);
4349 	}
4350 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4351 		return -ETIMEDOUT;
4352 
4353 	return 0;
4354 }
4355 
4356 /**
4357  * i40e_control_tx_q - Start or stop a particular Tx queue
4358  * @pf: the PF structure
4359  * @pf_q: the PF queue to configure
4360  * @enable: start or stop the queue
4361  *
4362  * This function enables or disables a single queue. Note that any delay
4363  * required after the operation is expected to be handled by the caller of
4364  * this function.
4365  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4366 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4367 {
4368 	struct i40e_hw *hw = &pf->hw;
4369 	u32 tx_reg;
4370 	int i;
4371 
4372 	/* warn the TX unit of coming changes */
4373 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4374 	if (!enable)
4375 		usleep_range(10, 20);
4376 
4377 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4378 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4379 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4380 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4381 			break;
4382 		usleep_range(1000, 2000);
4383 	}
4384 
4385 	/* Skip if the queue is already in the requested state */
4386 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4387 		return;
4388 
4389 	/* turn on/off the queue */
4390 	if (enable) {
4391 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4392 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4393 	} else {
4394 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4395 	}
4396 
4397 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4398 }
4399 
4400 /**
4401  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4402  * @seid: VSI SEID
4403  * @pf: the PF structure
4404  * @pf_q: the PF queue to configure
4405  * @is_xdp: true if the queue is used for XDP
4406  * @enable: start or stop the queue
4407  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4408 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4409 			   bool is_xdp, bool enable)
4410 {
4411 	int ret;
4412 
4413 	i40e_control_tx_q(pf, pf_q, enable);
4414 
4415 	/* wait for the change to finish */
4416 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4417 	if (ret) {
4418 		dev_info(&pf->pdev->dev,
4419 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4420 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4421 			 (enable ? "en" : "dis"));
4422 	}
4423 
4424 	return ret;
4425 }
4426 
4427 /**
4428  * i40e_vsi_enable_tx - Start a VSI's rings
4429  * @vsi: the VSI being configured
4430  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4431 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4432 {
4433 	struct i40e_pf *pf = vsi->back;
4434 	int i, pf_q, ret = 0;
4435 
4436 	pf_q = vsi->base_queue;
4437 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4438 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4439 					     pf_q,
4440 					     false /*is xdp*/, true);
4441 		if (ret)
4442 			break;
4443 
4444 		if (!i40e_enabled_xdp_vsi(vsi))
4445 			continue;
4446 
4447 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4448 					     pf_q + vsi->alloc_queue_pairs,
4449 					     true /*is xdp*/, true);
4450 		if (ret)
4451 			break;
4452 	}
4453 	return ret;
4454 }
4455 
4456 /**
4457  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4458  * @pf: the PF being configured
4459  * @pf_q: the PF queue
4460  * @enable: enable or disable state of the queue
4461  *
4462  * This routine will wait for the given Rx queue of the PF to reach the
4463  * enabled or disabled state.
4464  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4465  * multiple retries; else will return 0 in case of success.
4466  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4467 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4468 {
4469 	int i;
4470 	u32 rx_reg;
4471 
4472 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4473 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4474 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4475 			break;
4476 
4477 		usleep_range(10, 20);
4478 	}
4479 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4480 		return -ETIMEDOUT;
4481 
4482 	return 0;
4483 }
4484 
4485 /**
4486  * i40e_control_rx_q - Start or stop a particular Rx queue
4487  * @pf: the PF structure
4488  * @pf_q: the PF queue to configure
4489  * @enable: start or stop the queue
4490  *
4491  * This function enables or disables a single queue. Note that
4492  * any delay required after the operation is expected to be
4493  * handled by the caller of this function.
4494  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4495 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4496 {
4497 	struct i40e_hw *hw = &pf->hw;
4498 	u32 rx_reg;
4499 	int i;
4500 
4501 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4502 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4503 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4504 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4505 			break;
4506 		usleep_range(1000, 2000);
4507 	}
4508 
4509 	/* Skip if the queue is already in the requested state */
4510 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4511 		return;
4512 
4513 	/* turn on/off the queue */
4514 	if (enable)
4515 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4516 	else
4517 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4518 
4519 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4520 }
4521 
4522 /**
4523  * i40e_control_wait_rx_q
4524  * @pf: the PF structure
4525  * @pf_q: queue being configured
4526  * @enable: start or stop the rings
4527  *
4528  * This function enables or disables a single queue along with waiting
4529  * for the change to finish. The caller of this function should handle
4530  * the delays needed in the case of disabling queues.
4531  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4532 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4533 {
4534 	int ret = 0;
4535 
4536 	i40e_control_rx_q(pf, pf_q, enable);
4537 
4538 	/* wait for the change to finish */
4539 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4540 	if (ret)
4541 		return ret;
4542 
4543 	return ret;
4544 }
4545 
4546 /**
4547  * i40e_vsi_enable_rx - Start a VSI's rings
4548  * @vsi: the VSI being configured
4549  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4550 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4551 {
4552 	struct i40e_pf *pf = vsi->back;
4553 	int i, pf_q, ret = 0;
4554 
4555 	pf_q = vsi->base_queue;
4556 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4557 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4558 		if (ret) {
4559 			dev_info(&pf->pdev->dev,
4560 				 "VSI seid %d Rx ring %d enable timeout\n",
4561 				 vsi->seid, pf_q);
4562 			break;
4563 		}
4564 	}
4565 
4566 	return ret;
4567 }
4568 
4569 /**
4570  * i40e_vsi_start_rings - Start a VSI's rings
4571  * @vsi: the VSI being configured
4572  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4573 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4574 {
4575 	int ret = 0;
4576 
4577 	/* do rx first for enable and last for disable */
4578 	ret = i40e_vsi_enable_rx(vsi);
4579 	if (ret)
4580 		return ret;
4581 	ret = i40e_vsi_enable_tx(vsi);
4582 
4583 	return ret;
4584 }
4585 
4586 #define I40E_DISABLE_TX_GAP_MSEC	50
4587 
4588 /**
4589  * i40e_vsi_stop_rings - Stop a VSI's rings
4590  * @vsi: the VSI being configured
4591  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4592 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4593 {
4594 	struct i40e_pf *pf = vsi->back;
4595 	int pf_q, err, q_end;
4596 
4597 	/* When port TX is suspended, don't wait */
4598 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4599 		return i40e_vsi_stop_rings_no_wait(vsi);
4600 
4601 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4602 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4603 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4604 
4605 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4606 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4607 		if (err)
4608 			dev_info(&pf->pdev->dev,
4609 				 "VSI seid %d Rx ring %d dissable timeout\n",
4610 				 vsi->seid, pf_q);
4611 	}
4612 
4613 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4614 	pf_q = vsi->base_queue;
4615 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4616 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4617 
4618 	i40e_vsi_wait_queues_disabled(vsi);
4619 }
4620 
4621 /**
4622  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4623  * @vsi: the VSI being shutdown
4624  *
4625  * This function stops all the rings for a VSI but does not delay to verify
4626  * that rings have been disabled. It is expected that the caller is shutting
4627  * down multiple VSIs at once and will delay together for all the VSIs after
4628  * initiating the shutdown. This is particularly useful for shutting down lots
4629  * of VFs together. Otherwise, a large delay can be incurred while configuring
4630  * each VSI in serial.
4631  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4632 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4633 {
4634 	struct i40e_pf *pf = vsi->back;
4635 	int i, pf_q;
4636 
4637 	pf_q = vsi->base_queue;
4638 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4639 		i40e_control_tx_q(pf, pf_q, false);
4640 		i40e_control_rx_q(pf, pf_q, false);
4641 	}
4642 }
4643 
4644 /**
4645  * i40e_vsi_free_irq - Free the irq association with the OS
4646  * @vsi: the VSI being configured
4647  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)4648 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4649 {
4650 	struct i40e_pf *pf = vsi->back;
4651 	struct i40e_hw *hw = &pf->hw;
4652 	int base = vsi->base_vector;
4653 	u32 val, qp;
4654 	int i;
4655 
4656 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4657 		if (!vsi->q_vectors)
4658 			return;
4659 
4660 		if (!vsi->irqs_ready)
4661 			return;
4662 
4663 		vsi->irqs_ready = false;
4664 		for (i = 0; i < vsi->num_q_vectors; i++) {
4665 			int irq_num;
4666 			u16 vector;
4667 
4668 			vector = i + base;
4669 			irq_num = pf->msix_entries[vector].vector;
4670 
4671 			/* free only the irqs that were actually requested */
4672 			if (!vsi->q_vectors[i] ||
4673 			    !vsi->q_vectors[i]->num_ringpairs)
4674 				continue;
4675 
4676 			/* clear the affinity notifier in the IRQ descriptor */
4677 			irq_set_affinity_notifier(irq_num, NULL);
4678 			/* remove our suggested affinity mask for this IRQ */
4679 			irq_set_affinity_hint(irq_num, NULL);
4680 			synchronize_irq(irq_num);
4681 			free_irq(irq_num, vsi->q_vectors[i]);
4682 
4683 			/* Tear down the interrupt queue link list
4684 			 *
4685 			 * We know that they come in pairs and always
4686 			 * the Rx first, then the Tx.  To clear the
4687 			 * link list, stick the EOL value into the
4688 			 * next_q field of the registers.
4689 			 */
4690 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4691 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4692 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4693 			val |= I40E_QUEUE_END_OF_LIST
4694 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4695 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4696 
4697 			while (qp != I40E_QUEUE_END_OF_LIST) {
4698 				u32 next;
4699 
4700 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4701 
4702 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4703 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4704 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4705 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4706 
4707 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4708 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4709 
4710 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4711 
4712 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4713 
4714 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4715 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4716 
4717 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4718 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4719 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4720 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4721 
4722 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4723 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4724 
4725 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4726 				qp = next;
4727 			}
4728 		}
4729 	} else {
4730 		free_irq(pf->pdev->irq, pf);
4731 
4732 		val = rd32(hw, I40E_PFINT_LNKLST0);
4733 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4734 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4735 		val |= I40E_QUEUE_END_OF_LIST
4736 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4737 		wr32(hw, I40E_PFINT_LNKLST0, val);
4738 
4739 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4740 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4741 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4742 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4743 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4744 
4745 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4746 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4747 
4748 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4749 
4750 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4751 
4752 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4753 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4754 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4755 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4756 
4757 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4758 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4759 
4760 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4761 	}
4762 }
4763 
4764 /**
4765  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4766  * @vsi: the VSI being configured
4767  * @v_idx: Index of vector to be freed
4768  *
4769  * This function frees the memory allocated to the q_vector.  In addition if
4770  * NAPI is enabled it will delete any references to the NAPI struct prior
4771  * to freeing the q_vector.
4772  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)4773 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4774 {
4775 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4776 	struct i40e_ring *ring;
4777 
4778 	if (!q_vector)
4779 		return;
4780 
4781 	/* disassociate q_vector from rings */
4782 	i40e_for_each_ring(ring, q_vector->tx)
4783 		ring->q_vector = NULL;
4784 
4785 	i40e_for_each_ring(ring, q_vector->rx)
4786 		ring->q_vector = NULL;
4787 
4788 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4789 	if (vsi->netdev)
4790 		netif_napi_del(&q_vector->napi);
4791 
4792 	vsi->q_vectors[v_idx] = NULL;
4793 
4794 	kfree_rcu(q_vector, rcu);
4795 }
4796 
4797 /**
4798  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4799  * @vsi: the VSI being un-configured
4800  *
4801  * This frees the memory allocated to the q_vectors and
4802  * deletes references to the NAPI struct.
4803  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)4804 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4805 {
4806 	int v_idx;
4807 
4808 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4809 		i40e_free_q_vector(vsi, v_idx);
4810 }
4811 
4812 /**
4813  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4814  * @pf: board private structure
4815  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)4816 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4817 {
4818 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4819 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4820 		pci_disable_msix(pf->pdev);
4821 		kfree(pf->msix_entries);
4822 		pf->msix_entries = NULL;
4823 		kfree(pf->irq_pile);
4824 		pf->irq_pile = NULL;
4825 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4826 		pci_disable_msi(pf->pdev);
4827 	}
4828 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4829 }
4830 
4831 /**
4832  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4833  * @pf: board private structure
4834  *
4835  * We go through and clear interrupt specific resources and reset the structure
4836  * to pre-load conditions
4837  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)4838 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4839 {
4840 	int i;
4841 
4842 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
4843 		i40e_free_misc_vector(pf);
4844 
4845 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4846 		      I40E_IWARP_IRQ_PILE_ID);
4847 
4848 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4849 	for (i = 0; i < pf->num_alloc_vsi; i++)
4850 		if (pf->vsi[i])
4851 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4852 	i40e_reset_interrupt_capability(pf);
4853 }
4854 
4855 /**
4856  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4857  * @vsi: the VSI being configured
4858  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)4859 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4860 {
4861 	int q_idx;
4862 
4863 	if (!vsi->netdev)
4864 		return;
4865 
4866 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4867 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4868 
4869 		if (q_vector->rx.ring || q_vector->tx.ring)
4870 			napi_enable(&q_vector->napi);
4871 	}
4872 }
4873 
4874 /**
4875  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4876  * @vsi: the VSI being configured
4877  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)4878 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4879 {
4880 	int q_idx;
4881 
4882 	if (!vsi->netdev)
4883 		return;
4884 
4885 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4886 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4887 
4888 		if (q_vector->rx.ring || q_vector->tx.ring)
4889 			napi_disable(&q_vector->napi);
4890 	}
4891 }
4892 
4893 /**
4894  * i40e_vsi_close - Shut down a VSI
4895  * @vsi: the vsi to be quelled
4896  **/
i40e_vsi_close(struct i40e_vsi * vsi)4897 static void i40e_vsi_close(struct i40e_vsi *vsi)
4898 {
4899 	struct i40e_pf *pf = vsi->back;
4900 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4901 		i40e_down(vsi);
4902 	i40e_vsi_free_irq(vsi);
4903 	i40e_vsi_free_tx_resources(vsi);
4904 	i40e_vsi_free_rx_resources(vsi);
4905 	vsi->current_netdev_flags = 0;
4906 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4907 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4908 		set_bit(__I40E_CLIENT_RESET, pf->state);
4909 }
4910 
4911 /**
4912  * i40e_quiesce_vsi - Pause a given VSI
4913  * @vsi: the VSI being paused
4914  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)4915 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4916 {
4917 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4918 		return;
4919 
4920 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4921 	if (vsi->netdev && netif_running(vsi->netdev))
4922 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4923 	else
4924 		i40e_vsi_close(vsi);
4925 }
4926 
4927 /**
4928  * i40e_unquiesce_vsi - Resume a given VSI
4929  * @vsi: the VSI being resumed
4930  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)4931 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4932 {
4933 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4934 		return;
4935 
4936 	if (vsi->netdev && netif_running(vsi->netdev))
4937 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4938 	else
4939 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4940 }
4941 
4942 /**
4943  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4944  * @pf: the PF
4945  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)4946 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4947 {
4948 	int v;
4949 
4950 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4951 		if (pf->vsi[v])
4952 			i40e_quiesce_vsi(pf->vsi[v]);
4953 	}
4954 }
4955 
4956 /**
4957  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4958  * @pf: the PF
4959  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)4960 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4961 {
4962 	int v;
4963 
4964 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4965 		if (pf->vsi[v])
4966 			i40e_unquiesce_vsi(pf->vsi[v]);
4967 	}
4968 }
4969 
4970 /**
4971  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4972  * @vsi: the VSI being configured
4973  *
4974  * Wait until all queues on a given VSI have been disabled.
4975  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)4976 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4977 {
4978 	struct i40e_pf *pf = vsi->back;
4979 	int i, pf_q, ret;
4980 
4981 	pf_q = vsi->base_queue;
4982 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4983 		/* Check and wait for the Tx queue */
4984 		ret = i40e_pf_txq_wait(pf, pf_q, false);
4985 		if (ret) {
4986 			dev_info(&pf->pdev->dev,
4987 				 "VSI seid %d Tx ring %d disable timeout\n",
4988 				 vsi->seid, pf_q);
4989 			return ret;
4990 		}
4991 
4992 		if (!i40e_enabled_xdp_vsi(vsi))
4993 			goto wait_rx;
4994 
4995 		/* Check and wait for the XDP Tx queue */
4996 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4997 				       false);
4998 		if (ret) {
4999 			dev_info(&pf->pdev->dev,
5000 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5001 				 vsi->seid, pf_q);
5002 			return ret;
5003 		}
5004 wait_rx:
5005 		/* Check and wait for the Rx queue */
5006 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5007 		if (ret) {
5008 			dev_info(&pf->pdev->dev,
5009 				 "VSI seid %d Rx ring %d disable timeout\n",
5010 				 vsi->seid, pf_q);
5011 			return ret;
5012 		}
5013 	}
5014 
5015 	return 0;
5016 }
5017 
5018 #ifdef CONFIG_I40E_DCB
5019 /**
5020  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5021  * @pf: the PF
5022  *
5023  * This function waits for the queues to be in disabled state for all the
5024  * VSIs that are managed by this PF.
5025  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5026 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5027 {
5028 	int v, ret = 0;
5029 
5030 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5031 		if (pf->vsi[v]) {
5032 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5033 			if (ret)
5034 				break;
5035 		}
5036 	}
5037 
5038 	return ret;
5039 }
5040 
5041 #endif
5042 
5043 /**
5044  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5045  * @pf: pointer to PF
5046  *
5047  * Get TC map for ISCSI PF type that will include iSCSI TC
5048  * and LAN TC.
5049  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5050 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5051 {
5052 	struct i40e_dcb_app_priority_table app;
5053 	struct i40e_hw *hw = &pf->hw;
5054 	u8 enabled_tc = 1; /* TC0 is always enabled */
5055 	u8 tc, i;
5056 	/* Get the iSCSI APP TLV */
5057 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5058 
5059 	for (i = 0; i < dcbcfg->numapps; i++) {
5060 		app = dcbcfg->app[i];
5061 		if (app.selector == I40E_APP_SEL_TCPIP &&
5062 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5063 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5064 			enabled_tc |= BIT(tc);
5065 			break;
5066 		}
5067 	}
5068 
5069 	return enabled_tc;
5070 }
5071 
5072 /**
5073  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5074  * @dcbcfg: the corresponding DCBx configuration structure
5075  *
5076  * Return the number of TCs from given DCBx configuration
5077  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5078 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5079 {
5080 	int i, tc_unused = 0;
5081 	u8 num_tc = 0;
5082 	u8 ret = 0;
5083 
5084 	/* Scan the ETS Config Priority Table to find
5085 	 * traffic class enabled for a given priority
5086 	 * and create a bitmask of enabled TCs
5087 	 */
5088 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5089 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5090 
5091 	/* Now scan the bitmask to check for
5092 	 * contiguous TCs starting with TC0
5093 	 */
5094 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5095 		if (num_tc & BIT(i)) {
5096 			if (!tc_unused) {
5097 				ret++;
5098 			} else {
5099 				pr_err("Non-contiguous TC - Disabling DCB\n");
5100 				return 1;
5101 			}
5102 		} else {
5103 			tc_unused = 1;
5104 		}
5105 	}
5106 
5107 	/* There is always at least TC0 */
5108 	if (!ret)
5109 		ret = 1;
5110 
5111 	return ret;
5112 }
5113 
5114 /**
5115  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5116  * @dcbcfg: the corresponding DCBx configuration structure
5117  *
5118  * Query the current DCB configuration and return the number of
5119  * traffic classes enabled from the given DCBX config
5120  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5121 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5122 {
5123 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5124 	u8 enabled_tc = 1;
5125 	u8 i;
5126 
5127 	for (i = 0; i < num_tc; i++)
5128 		enabled_tc |= BIT(i);
5129 
5130 	return enabled_tc;
5131 }
5132 
5133 /**
5134  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5135  * @pf: PF being queried
5136  *
5137  * Query the current MQPRIO configuration and return the number of
5138  * traffic classes enabled.
5139  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5140 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5141 {
5142 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5143 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5144 	u8 enabled_tc = 1, i;
5145 
5146 	for (i = 1; i < num_tc; i++)
5147 		enabled_tc |= BIT(i);
5148 	return enabled_tc;
5149 }
5150 
5151 /**
5152  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5153  * @pf: PF being queried
5154  *
5155  * Return number of traffic classes enabled for the given PF
5156  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5157 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5158 {
5159 	struct i40e_hw *hw = &pf->hw;
5160 	u8 i, enabled_tc = 1;
5161 	u8 num_tc = 0;
5162 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5163 
5164 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5165 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5166 
5167 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5168 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5169 		return 1;
5170 
5171 	/* SFP mode will be enabled for all TCs on port */
5172 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5173 		return i40e_dcb_get_num_tc(dcbcfg);
5174 
5175 	/* MFP mode return count of enabled TCs for this PF */
5176 	if (pf->hw.func_caps.iscsi)
5177 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5178 	else
5179 		return 1; /* Only TC0 */
5180 
5181 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5182 		if (enabled_tc & BIT(i))
5183 			num_tc++;
5184 	}
5185 	return num_tc;
5186 }
5187 
5188 /**
5189  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5190  * @pf: PF being queried
5191  *
5192  * Return a bitmap for enabled traffic classes for this PF.
5193  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5194 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5195 {
5196 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5197 		return i40e_mqprio_get_enabled_tc(pf);
5198 
5199 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5200 	 * default TC
5201 	 */
5202 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5203 		return I40E_DEFAULT_TRAFFIC_CLASS;
5204 
5205 	/* SFP mode we want PF to be enabled for all TCs */
5206 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5207 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5208 
5209 	/* MFP enabled and iSCSI PF type */
5210 	if (pf->hw.func_caps.iscsi)
5211 		return i40e_get_iscsi_tc_map(pf);
5212 	else
5213 		return I40E_DEFAULT_TRAFFIC_CLASS;
5214 }
5215 
5216 /**
5217  * i40e_vsi_get_bw_info - Query VSI BW Information
5218  * @vsi: the VSI being queried
5219  *
5220  * Returns 0 on success, negative value on failure
5221  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5222 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5223 {
5224 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5225 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5226 	struct i40e_pf *pf = vsi->back;
5227 	struct i40e_hw *hw = &pf->hw;
5228 	i40e_status ret;
5229 	u32 tc_bw_max;
5230 	int i;
5231 
5232 	/* Get the VSI level BW configuration */
5233 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5234 	if (ret) {
5235 		dev_info(&pf->pdev->dev,
5236 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5237 			 i40e_stat_str(&pf->hw, ret),
5238 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5239 		return -EINVAL;
5240 	}
5241 
5242 	/* Get the VSI level BW configuration per TC */
5243 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5244 					       NULL);
5245 	if (ret) {
5246 		dev_info(&pf->pdev->dev,
5247 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5248 			 i40e_stat_str(&pf->hw, ret),
5249 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5250 		return -EINVAL;
5251 	}
5252 
5253 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5254 		dev_info(&pf->pdev->dev,
5255 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5256 			 bw_config.tc_valid_bits,
5257 			 bw_ets_config.tc_valid_bits);
5258 		/* Still continuing */
5259 	}
5260 
5261 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5262 	vsi->bw_max_quanta = bw_config.max_bw;
5263 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5264 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5265 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5266 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5267 		vsi->bw_ets_limit_credits[i] =
5268 					le16_to_cpu(bw_ets_config.credits[i]);
5269 		/* 3 bits out of 4 for each TC */
5270 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5271 	}
5272 
5273 	return 0;
5274 }
5275 
5276 /**
5277  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5278  * @vsi: the VSI being configured
5279  * @enabled_tc: TC bitmap
5280  * @bw_share: BW shared credits per TC
5281  *
5282  * Returns 0 on success, negative value on failure
5283  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5284 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5285 				       u8 *bw_share)
5286 {
5287 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5288 	struct i40e_pf *pf = vsi->back;
5289 	i40e_status ret;
5290 	int i;
5291 
5292 	/* There is no need to reset BW when mqprio mode is on.  */
5293 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5294 		return 0;
5295 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5296 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5297 		if (ret)
5298 			dev_info(&pf->pdev->dev,
5299 				 "Failed to reset tx rate for vsi->seid %u\n",
5300 				 vsi->seid);
5301 		return ret;
5302 	}
5303 	bw_data.tc_valid_bits = enabled_tc;
5304 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5305 		bw_data.tc_bw_credits[i] = bw_share[i];
5306 
5307 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5308 	if (ret) {
5309 		dev_info(&pf->pdev->dev,
5310 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5311 			 pf->hw.aq.asq_last_status);
5312 		return -EINVAL;
5313 	}
5314 
5315 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5316 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5317 
5318 	return 0;
5319 }
5320 
5321 /**
5322  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5323  * @vsi: the VSI being configured
5324  * @enabled_tc: TC map to be enabled
5325  *
5326  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5327 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5328 {
5329 	struct net_device *netdev = vsi->netdev;
5330 	struct i40e_pf *pf = vsi->back;
5331 	struct i40e_hw *hw = &pf->hw;
5332 	u8 netdev_tc = 0;
5333 	int i;
5334 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5335 
5336 	if (!netdev)
5337 		return;
5338 
5339 	if (!enabled_tc) {
5340 		netdev_reset_tc(netdev);
5341 		return;
5342 	}
5343 
5344 	/* Set up actual enabled TCs on the VSI */
5345 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5346 		return;
5347 
5348 	/* set per TC queues for the VSI */
5349 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5350 		/* Only set TC queues for enabled tcs
5351 		 *
5352 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5353 		 * enabled_tc bitmap would be 0x00001001; the driver
5354 		 * will set the numtc for netdev as 2 that will be
5355 		 * referenced by the netdev layer as TC 0 and 1.
5356 		 */
5357 		if (vsi->tc_config.enabled_tc & BIT(i))
5358 			netdev_set_tc_queue(netdev,
5359 					vsi->tc_config.tc_info[i].netdev_tc,
5360 					vsi->tc_config.tc_info[i].qcount,
5361 					vsi->tc_config.tc_info[i].qoffset);
5362 	}
5363 
5364 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5365 		return;
5366 
5367 	/* Assign UP2TC map for the VSI */
5368 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5369 		/* Get the actual TC# for the UP */
5370 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5371 		/* Get the mapped netdev TC# for the UP */
5372 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5373 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5374 	}
5375 }
5376 
5377 /**
5378  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5379  * @vsi: the VSI being configured
5380  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5381  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5382 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5383 				      struct i40e_vsi_context *ctxt)
5384 {
5385 	/* copy just the sections touched not the entire info
5386 	 * since not all sections are valid as returned by
5387 	 * update vsi params
5388 	 */
5389 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5390 	memcpy(&vsi->info.queue_mapping,
5391 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5392 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5393 	       sizeof(vsi->info.tc_mapping));
5394 }
5395 
5396 /**
5397  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5398  * @vsi: VSI to be configured
5399  * @enabled_tc: TC bitmap
5400  *
5401  * This configures a particular VSI for TCs that are mapped to the
5402  * given TC bitmap. It uses default bandwidth share for TCs across
5403  * VSIs to configure TC for a particular VSI.
5404  *
5405  * NOTE:
5406  * It is expected that the VSI queues have been quisced before calling
5407  * this function.
5408  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5409 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5410 {
5411 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5412 	struct i40e_pf *pf = vsi->back;
5413 	struct i40e_hw *hw = &pf->hw;
5414 	struct i40e_vsi_context ctxt;
5415 	int ret = 0;
5416 	int i;
5417 
5418 	/* Check if enabled_tc is same as existing or new TCs */
5419 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5420 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5421 		return ret;
5422 
5423 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5424 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5425 		if (enabled_tc & BIT(i))
5426 			bw_share[i] = 1;
5427 	}
5428 
5429 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5430 	if (ret) {
5431 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5432 
5433 		dev_info(&pf->pdev->dev,
5434 			 "Failed configuring TC map %d for VSI %d\n",
5435 			 enabled_tc, vsi->seid);
5436 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5437 						  &bw_config, NULL);
5438 		if (ret) {
5439 			dev_info(&pf->pdev->dev,
5440 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5441 				 i40e_stat_str(hw, ret),
5442 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5443 			goto out;
5444 		}
5445 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5446 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5447 
5448 			if (!valid_tc)
5449 				valid_tc = bw_config.tc_valid_bits;
5450 			/* Always enable TC0, no matter what */
5451 			valid_tc |= 1;
5452 			dev_info(&pf->pdev->dev,
5453 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5454 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5455 			enabled_tc = valid_tc;
5456 		}
5457 
5458 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5459 		if (ret) {
5460 			dev_err(&pf->pdev->dev,
5461 				"Unable to  configure TC map %d for VSI %d\n",
5462 				enabled_tc, vsi->seid);
5463 			goto out;
5464 		}
5465 	}
5466 
5467 	/* Update Queue Pairs Mapping for currently enabled UPs */
5468 	ctxt.seid = vsi->seid;
5469 	ctxt.pf_num = vsi->back->hw.pf_id;
5470 	ctxt.vf_num = 0;
5471 	ctxt.uplink_seid = vsi->uplink_seid;
5472 	ctxt.info = vsi->info;
5473 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5474 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5475 		if (ret)
5476 			goto out;
5477 	} else {
5478 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5479 	}
5480 
5481 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5482 	 * queues changed.
5483 	 */
5484 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5485 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5486 				      vsi->num_queue_pairs);
5487 		ret = i40e_vsi_config_rss(vsi);
5488 		if (ret) {
5489 			dev_info(&vsi->back->pdev->dev,
5490 				 "Failed to reconfig rss for num_queues\n");
5491 			return ret;
5492 		}
5493 		vsi->reconfig_rss = false;
5494 	}
5495 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5496 		ctxt.info.valid_sections |=
5497 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5498 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5499 	}
5500 
5501 	/* Update the VSI after updating the VSI queue-mapping
5502 	 * information
5503 	 */
5504 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5505 	if (ret) {
5506 		dev_info(&pf->pdev->dev,
5507 			 "Update vsi tc config failed, err %s aq_err %s\n",
5508 			 i40e_stat_str(hw, ret),
5509 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5510 		goto out;
5511 	}
5512 	/* update the local VSI info with updated queue map */
5513 	i40e_vsi_update_queue_map(vsi, &ctxt);
5514 	vsi->info.valid_sections = 0;
5515 
5516 	/* Update current VSI BW information */
5517 	ret = i40e_vsi_get_bw_info(vsi);
5518 	if (ret) {
5519 		dev_info(&pf->pdev->dev,
5520 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5521 			 i40e_stat_str(hw, ret),
5522 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5523 		goto out;
5524 	}
5525 
5526 	/* Update the netdev TC setup */
5527 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5528 out:
5529 	return ret;
5530 }
5531 
5532 /**
5533  * i40e_get_link_speed - Returns link speed for the interface
5534  * @vsi: VSI to be configured
5535  *
5536  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5537 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5538 {
5539 	struct i40e_pf *pf = vsi->back;
5540 
5541 	switch (pf->hw.phy.link_info.link_speed) {
5542 	case I40E_LINK_SPEED_40GB:
5543 		return 40000;
5544 	case I40E_LINK_SPEED_25GB:
5545 		return 25000;
5546 	case I40E_LINK_SPEED_20GB:
5547 		return 20000;
5548 	case I40E_LINK_SPEED_10GB:
5549 		return 10000;
5550 	case I40E_LINK_SPEED_1GB:
5551 		return 1000;
5552 	default:
5553 		return -EINVAL;
5554 	}
5555 }
5556 
5557 /**
5558  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5559  * @vsi: VSI to be configured
5560  * @seid: seid of the channel/VSI
5561  * @max_tx_rate: max TX rate to be configured as BW limit
5562  *
5563  * Helper function to set BW limit for a given VSI
5564  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5565 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5566 {
5567 	struct i40e_pf *pf = vsi->back;
5568 	u64 credits = 0;
5569 	int speed = 0;
5570 	int ret = 0;
5571 
5572 	speed = i40e_get_link_speed(vsi);
5573 	if (max_tx_rate > speed) {
5574 		dev_err(&pf->pdev->dev,
5575 			"Invalid max tx rate %llu specified for VSI seid %d.",
5576 			max_tx_rate, seid);
5577 		return -EINVAL;
5578 	}
5579 	if (max_tx_rate && max_tx_rate < 50) {
5580 		dev_warn(&pf->pdev->dev,
5581 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5582 		max_tx_rate = 50;
5583 	}
5584 
5585 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5586 	credits = max_tx_rate;
5587 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5588 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5589 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5590 	if (ret)
5591 		dev_err(&pf->pdev->dev,
5592 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5593 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5594 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5595 	return ret;
5596 }
5597 
5598 /**
5599  * i40e_remove_queue_channels - Remove queue channels for the TCs
5600  * @vsi: VSI to be configured
5601  *
5602  * Remove queue channels for the TCs
5603  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)5604 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5605 {
5606 	enum i40e_admin_queue_err last_aq_status;
5607 	struct i40e_cloud_filter *cfilter;
5608 	struct i40e_channel *ch, *ch_tmp;
5609 	struct i40e_pf *pf = vsi->back;
5610 	struct hlist_node *node;
5611 	int ret, i;
5612 
5613 	/* Reset rss size that was stored when reconfiguring rss for
5614 	 * channel VSIs with non-power-of-2 queue count.
5615 	 */
5616 	vsi->current_rss_size = 0;
5617 
5618 	/* perform cleanup for channels if they exist */
5619 	if (list_empty(&vsi->ch_list))
5620 		return;
5621 
5622 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5623 		struct i40e_vsi *p_vsi;
5624 
5625 		list_del(&ch->list);
5626 		p_vsi = ch->parent_vsi;
5627 		if (!p_vsi || !ch->initialized) {
5628 			kfree(ch);
5629 			continue;
5630 		}
5631 		/* Reset queue contexts */
5632 		for (i = 0; i < ch->num_queue_pairs; i++) {
5633 			struct i40e_ring *tx_ring, *rx_ring;
5634 			u16 pf_q;
5635 
5636 			pf_q = ch->base_queue + i;
5637 			tx_ring = vsi->tx_rings[pf_q];
5638 			tx_ring->ch = NULL;
5639 
5640 			rx_ring = vsi->rx_rings[pf_q];
5641 			rx_ring->ch = NULL;
5642 		}
5643 
5644 		/* Reset BW configured for this VSI via mqprio */
5645 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5646 		if (ret)
5647 			dev_info(&vsi->back->pdev->dev,
5648 				 "Failed to reset tx rate for ch->seid %u\n",
5649 				 ch->seid);
5650 
5651 		/* delete cloud filters associated with this channel */
5652 		hlist_for_each_entry_safe(cfilter, node,
5653 					  &pf->cloud_filter_list, cloud_node) {
5654 			if (cfilter->seid != ch->seid)
5655 				continue;
5656 
5657 			hash_del(&cfilter->cloud_node);
5658 			if (cfilter->dst_port)
5659 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5660 									cfilter,
5661 									false);
5662 			else
5663 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5664 								false);
5665 			last_aq_status = pf->hw.aq.asq_last_status;
5666 			if (ret)
5667 				dev_info(&pf->pdev->dev,
5668 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5669 					 i40e_stat_str(&pf->hw, ret),
5670 					 i40e_aq_str(&pf->hw, last_aq_status));
5671 			kfree(cfilter);
5672 		}
5673 
5674 		/* delete VSI from FW */
5675 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5676 					     NULL);
5677 		if (ret)
5678 			dev_err(&vsi->back->pdev->dev,
5679 				"unable to remove channel (%d) for parent VSI(%d)\n",
5680 				ch->seid, p_vsi->seid);
5681 		kfree(ch);
5682 	}
5683 	INIT_LIST_HEAD(&vsi->ch_list);
5684 }
5685 
5686 /**
5687  * i40e_is_any_channel - channel exist or not
5688  * @vsi: ptr to VSI to which channels are associated with
5689  *
5690  * Returns true or false if channel(s) exist for associated VSI or not
5691  **/
i40e_is_any_channel(struct i40e_vsi * vsi)5692 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5693 {
5694 	struct i40e_channel *ch, *ch_tmp;
5695 
5696 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5697 		if (ch->initialized)
5698 			return true;
5699 	}
5700 
5701 	return false;
5702 }
5703 
5704 /**
5705  * i40e_get_max_queues_for_channel
5706  * @vsi: ptr to VSI to which channels are associated with
5707  *
5708  * Helper function which returns max value among the queue counts set on the
5709  * channels/TCs created.
5710  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)5711 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5712 {
5713 	struct i40e_channel *ch, *ch_tmp;
5714 	int max = 0;
5715 
5716 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5717 		if (!ch->initialized)
5718 			continue;
5719 		if (ch->num_queue_pairs > max)
5720 			max = ch->num_queue_pairs;
5721 	}
5722 
5723 	return max;
5724 }
5725 
5726 /**
5727  * i40e_validate_num_queues - validate num_queues w.r.t channel
5728  * @pf: ptr to PF device
5729  * @num_queues: number of queues
5730  * @vsi: the parent VSI
5731  * @reconfig_rss: indicates should the RSS be reconfigured or not
5732  *
5733  * This function validates number of queues in the context of new channel
5734  * which is being established and determines if RSS should be reconfigured
5735  * or not for parent VSI.
5736  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)5737 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5738 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5739 {
5740 	int max_ch_queues;
5741 
5742 	if (!reconfig_rss)
5743 		return -EINVAL;
5744 
5745 	*reconfig_rss = false;
5746 	if (vsi->current_rss_size) {
5747 		if (num_queues > vsi->current_rss_size) {
5748 			dev_dbg(&pf->pdev->dev,
5749 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5750 				num_queues, vsi->current_rss_size);
5751 			return -EINVAL;
5752 		} else if ((num_queues < vsi->current_rss_size) &&
5753 			   (!is_power_of_2(num_queues))) {
5754 			dev_dbg(&pf->pdev->dev,
5755 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5756 				num_queues, vsi->current_rss_size);
5757 			return -EINVAL;
5758 		}
5759 	}
5760 
5761 	if (!is_power_of_2(num_queues)) {
5762 		/* Find the max num_queues configured for channel if channel
5763 		 * exist.
5764 		 * if channel exist, then enforce 'num_queues' to be more than
5765 		 * max ever queues configured for channel.
5766 		 */
5767 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5768 		if (num_queues < max_ch_queues) {
5769 			dev_dbg(&pf->pdev->dev,
5770 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5771 				num_queues, max_ch_queues);
5772 			return -EINVAL;
5773 		}
5774 		*reconfig_rss = true;
5775 	}
5776 
5777 	return 0;
5778 }
5779 
5780 /**
5781  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5782  * @vsi: the VSI being setup
5783  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5784  *
5785  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5786  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)5787 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5788 {
5789 	struct i40e_pf *pf = vsi->back;
5790 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5791 	struct i40e_hw *hw = &pf->hw;
5792 	int local_rss_size;
5793 	u8 *lut;
5794 	int ret;
5795 
5796 	if (!vsi->rss_size)
5797 		return -EINVAL;
5798 
5799 	if (rss_size > vsi->rss_size)
5800 		return -EINVAL;
5801 
5802 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5803 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5804 	if (!lut)
5805 		return -ENOMEM;
5806 
5807 	/* Ignoring user configured lut if there is one */
5808 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5809 
5810 	/* Use user configured hash key if there is one, otherwise
5811 	 * use default.
5812 	 */
5813 	if (vsi->rss_hkey_user)
5814 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5815 	else
5816 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5817 
5818 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5819 	if (ret) {
5820 		dev_info(&pf->pdev->dev,
5821 			 "Cannot set RSS lut, err %s aq_err %s\n",
5822 			 i40e_stat_str(hw, ret),
5823 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5824 		kfree(lut);
5825 		return ret;
5826 	}
5827 	kfree(lut);
5828 
5829 	/* Do the update w.r.t. storing rss_size */
5830 	if (!vsi->orig_rss_size)
5831 		vsi->orig_rss_size = vsi->rss_size;
5832 	vsi->current_rss_size = local_rss_size;
5833 
5834 	return ret;
5835 }
5836 
5837 /**
5838  * i40e_channel_setup_queue_map - Setup a channel queue map
5839  * @pf: ptr to PF device
5840  * @ctxt: VSI context structure
5841  * @ch: ptr to channel structure
5842  *
5843  * Setup queue map for a specific channel
5844  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)5845 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5846 					 struct i40e_vsi_context *ctxt,
5847 					 struct i40e_channel *ch)
5848 {
5849 	u16 qcount, qmap, sections = 0;
5850 	u8 offset = 0;
5851 	int pow;
5852 
5853 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5854 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5855 
5856 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5857 	ch->num_queue_pairs = qcount;
5858 
5859 	/* find the next higher power-of-2 of num queue pairs */
5860 	pow = ilog2(qcount);
5861 	if (!is_power_of_2(qcount))
5862 		pow++;
5863 
5864 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5865 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5866 
5867 	/* Setup queue TC[0].qmap for given VSI context */
5868 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5869 
5870 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5871 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5872 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5873 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5874 }
5875 
5876 /**
5877  * i40e_add_channel - add a channel by adding VSI
5878  * @pf: ptr to PF device
5879  * @uplink_seid: underlying HW switching element (VEB) ID
5880  * @ch: ptr to channel structure
5881  *
5882  * Add a channel (VSI) using add_vsi and queue_map
5883  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)5884 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5885 			    struct i40e_channel *ch)
5886 {
5887 	struct i40e_hw *hw = &pf->hw;
5888 	struct i40e_vsi_context ctxt;
5889 	u8 enabled_tc = 0x1; /* TC0 enabled */
5890 	int ret;
5891 
5892 	if (ch->type != I40E_VSI_VMDQ2) {
5893 		dev_info(&pf->pdev->dev,
5894 			 "add new vsi failed, ch->type %d\n", ch->type);
5895 		return -EINVAL;
5896 	}
5897 
5898 	memset(&ctxt, 0, sizeof(ctxt));
5899 	ctxt.pf_num = hw->pf_id;
5900 	ctxt.vf_num = 0;
5901 	ctxt.uplink_seid = uplink_seid;
5902 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5903 	if (ch->type == I40E_VSI_VMDQ2)
5904 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5905 
5906 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5907 		ctxt.info.valid_sections |=
5908 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5909 		ctxt.info.switch_id =
5910 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5911 	}
5912 
5913 	/* Set queue map for a given VSI context */
5914 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5915 
5916 	/* Now time to create VSI */
5917 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5918 	if (ret) {
5919 		dev_info(&pf->pdev->dev,
5920 			 "add new vsi failed, err %s aq_err %s\n",
5921 			 i40e_stat_str(&pf->hw, ret),
5922 			 i40e_aq_str(&pf->hw,
5923 				     pf->hw.aq.asq_last_status));
5924 		return -ENOENT;
5925 	}
5926 
5927 	/* Success, update channel, set enabled_tc only if the channel
5928 	 * is not a macvlan
5929 	 */
5930 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
5931 	ch->seid = ctxt.seid;
5932 	ch->vsi_number = ctxt.vsi_number;
5933 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
5934 
5935 	/* copy just the sections touched not the entire info
5936 	 * since not all sections are valid as returned by
5937 	 * update vsi params
5938 	 */
5939 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5940 	memcpy(&ch->info.queue_mapping,
5941 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5942 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5943 	       sizeof(ctxt.info.tc_mapping));
5944 
5945 	return 0;
5946 }
5947 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)5948 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5949 				  u8 *bw_share)
5950 {
5951 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5952 	i40e_status ret;
5953 	int i;
5954 
5955 	bw_data.tc_valid_bits = ch->enabled_tc;
5956 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5957 		bw_data.tc_bw_credits[i] = bw_share[i];
5958 
5959 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5960 				       &bw_data, NULL);
5961 	if (ret) {
5962 		dev_info(&vsi->back->pdev->dev,
5963 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5964 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5965 		return -EINVAL;
5966 	}
5967 
5968 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5969 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5970 
5971 	return 0;
5972 }
5973 
5974 /**
5975  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5976  * @pf: ptr to PF device
5977  * @vsi: the VSI being setup
5978  * @ch: ptr to channel structure
5979  *
5980  * Configure TX rings associated with channel (VSI) since queues are being
5981  * from parent VSI.
5982  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)5983 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5984 				       struct i40e_vsi *vsi,
5985 				       struct i40e_channel *ch)
5986 {
5987 	i40e_status ret;
5988 	int i;
5989 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5990 
5991 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5992 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5993 		if (ch->enabled_tc & BIT(i))
5994 			bw_share[i] = 1;
5995 	}
5996 
5997 	/* configure BW for new VSI */
5998 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
5999 	if (ret) {
6000 		dev_info(&vsi->back->pdev->dev,
6001 			 "Failed configuring TC map %d for channel (seid %u)\n",
6002 			 ch->enabled_tc, ch->seid);
6003 		return ret;
6004 	}
6005 
6006 	for (i = 0; i < ch->num_queue_pairs; i++) {
6007 		struct i40e_ring *tx_ring, *rx_ring;
6008 		u16 pf_q;
6009 
6010 		pf_q = ch->base_queue + i;
6011 
6012 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6013 		 * context
6014 		 */
6015 		tx_ring = vsi->tx_rings[pf_q];
6016 		tx_ring->ch = ch;
6017 
6018 		/* Get the RX ring ptr */
6019 		rx_ring = vsi->rx_rings[pf_q];
6020 		rx_ring->ch = ch;
6021 	}
6022 
6023 	return 0;
6024 }
6025 
6026 /**
6027  * i40e_setup_hw_channel - setup new channel
6028  * @pf: ptr to PF device
6029  * @vsi: the VSI being setup
6030  * @ch: ptr to channel structure
6031  * @uplink_seid: underlying HW switching element (VEB) ID
6032  * @type: type of channel to be created (VMDq2/VF)
6033  *
6034  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6035  * and configures TX rings accordingly
6036  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6037 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6038 					struct i40e_vsi *vsi,
6039 					struct i40e_channel *ch,
6040 					u16 uplink_seid, u8 type)
6041 {
6042 	int ret;
6043 
6044 	ch->initialized = false;
6045 	ch->base_queue = vsi->next_base_queue;
6046 	ch->type = type;
6047 
6048 	/* Proceed with creation of channel (VMDq2) VSI */
6049 	ret = i40e_add_channel(pf, uplink_seid, ch);
6050 	if (ret) {
6051 		dev_info(&pf->pdev->dev,
6052 			 "failed to add_channel using uplink_seid %u\n",
6053 			 uplink_seid);
6054 		return ret;
6055 	}
6056 
6057 	/* Mark the successful creation of channel */
6058 	ch->initialized = true;
6059 
6060 	/* Reconfigure TX queues using QTX_CTL register */
6061 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6062 	if (ret) {
6063 		dev_info(&pf->pdev->dev,
6064 			 "failed to configure TX rings for channel %u\n",
6065 			 ch->seid);
6066 		return ret;
6067 	}
6068 
6069 	/* update 'next_base_queue' */
6070 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6071 	dev_dbg(&pf->pdev->dev,
6072 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6073 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6074 		ch->num_queue_pairs,
6075 		vsi->next_base_queue);
6076 	return ret;
6077 }
6078 
6079 /**
6080  * i40e_setup_channel - setup new channel using uplink element
6081  * @pf: ptr to PF device
6082  * @vsi: pointer to the VSI to set up the channel within
6083  * @ch: ptr to channel structure
6084  *
6085  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6086  * and uplink switching element (uplink_seid)
6087  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6088 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6089 			       struct i40e_channel *ch)
6090 {
6091 	u8 vsi_type;
6092 	u16 seid;
6093 	int ret;
6094 
6095 	if (vsi->type == I40E_VSI_MAIN) {
6096 		vsi_type = I40E_VSI_VMDQ2;
6097 	} else {
6098 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6099 			vsi->type);
6100 		return false;
6101 	}
6102 
6103 	/* underlying switching element */
6104 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6105 
6106 	/* create channel (VSI), configure TX rings */
6107 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6108 	if (ret) {
6109 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6110 		return false;
6111 	}
6112 
6113 	return ch->initialized ? true : false;
6114 }
6115 
6116 /**
6117  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6118  * @vsi: ptr to VSI which has PF backing
6119  *
6120  * Sets up switch mode correctly if it needs to be changed and perform
6121  * what are allowed modes.
6122  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6123 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6124 {
6125 	u8 mode;
6126 	struct i40e_pf *pf = vsi->back;
6127 	struct i40e_hw *hw = &pf->hw;
6128 	int ret;
6129 
6130 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6131 	if (ret)
6132 		return -EINVAL;
6133 
6134 	if (hw->dev_caps.switch_mode) {
6135 		/* if switch mode is set, support mode2 (non-tunneled for
6136 		 * cloud filter) for now
6137 		 */
6138 		u32 switch_mode = hw->dev_caps.switch_mode &
6139 				  I40E_SWITCH_MODE_MASK;
6140 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6141 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6142 				return 0;
6143 			dev_err(&pf->pdev->dev,
6144 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6145 				hw->dev_caps.switch_mode);
6146 			return -EINVAL;
6147 		}
6148 	}
6149 
6150 	/* Set Bit 7 to be valid */
6151 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6152 
6153 	/* Set L4type for TCP support */
6154 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6155 
6156 	/* Set cloud filter mode */
6157 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6158 
6159 	/* Prep mode field for set_switch_config */
6160 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6161 					pf->last_sw_conf_valid_flags,
6162 					mode, NULL);
6163 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6164 		dev_err(&pf->pdev->dev,
6165 			"couldn't set switch config bits, err %s aq_err %s\n",
6166 			i40e_stat_str(hw, ret),
6167 			i40e_aq_str(hw,
6168 				    hw->aq.asq_last_status));
6169 
6170 	return ret;
6171 }
6172 
6173 /**
6174  * i40e_create_queue_channel - function to create channel
6175  * @vsi: VSI to be configured
6176  * @ch: ptr to channel (it contains channel specific params)
6177  *
6178  * This function creates channel (VSI) using num_queues specified by user,
6179  * reconfigs RSS if needed.
6180  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6181 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6182 			      struct i40e_channel *ch)
6183 {
6184 	struct i40e_pf *pf = vsi->back;
6185 	bool reconfig_rss;
6186 	int err;
6187 
6188 	if (!ch)
6189 		return -EINVAL;
6190 
6191 	if (!ch->num_queue_pairs) {
6192 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6193 			ch->num_queue_pairs);
6194 		return -EINVAL;
6195 	}
6196 
6197 	/* validate user requested num_queues for channel */
6198 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6199 				       &reconfig_rss);
6200 	if (err) {
6201 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6202 			 ch->num_queue_pairs);
6203 		return -EINVAL;
6204 	}
6205 
6206 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6207 	 * VSI to be added switch to VEB mode.
6208 	 */
6209 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6210 	    (!i40e_is_any_channel(vsi))) {
6211 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6212 			dev_dbg(&pf->pdev->dev,
6213 				"Failed to create channel. Override queues (%u) not power of 2\n",
6214 				vsi->tc_config.tc_info[0].qcount);
6215 			return -EINVAL;
6216 		}
6217 
6218 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6219 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6220 
6221 			if (vsi->type == I40E_VSI_MAIN) {
6222 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6223 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6224 						      true);
6225 				else
6226 					i40e_do_reset_safe(pf,
6227 							   I40E_PF_RESET_FLAG);
6228 			}
6229 		}
6230 		/* now onwards for main VSI, number of queues will be value
6231 		 * of TC0's queue count
6232 		 */
6233 	}
6234 
6235 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6236 	 * it should be more than num_queues
6237 	 */
6238 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6239 		dev_dbg(&pf->pdev->dev,
6240 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6241 			vsi->cnt_q_avail, ch->num_queue_pairs);
6242 		return -EINVAL;
6243 	}
6244 
6245 	/* reconfig_rss only if vsi type is MAIN_VSI */
6246 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6247 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6248 		if (err) {
6249 			dev_info(&pf->pdev->dev,
6250 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6251 				 ch->num_queue_pairs);
6252 			return -EINVAL;
6253 		}
6254 	}
6255 
6256 	if (!i40e_setup_channel(pf, vsi, ch)) {
6257 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6258 		return -EINVAL;
6259 	}
6260 
6261 	dev_info(&pf->pdev->dev,
6262 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6263 		 ch->seid, ch->num_queue_pairs);
6264 
6265 	/* configure VSI for BW limit */
6266 	if (ch->max_tx_rate) {
6267 		u64 credits = ch->max_tx_rate;
6268 
6269 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6270 			return -EINVAL;
6271 
6272 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6273 		dev_dbg(&pf->pdev->dev,
6274 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6275 			ch->max_tx_rate,
6276 			credits,
6277 			ch->seid);
6278 	}
6279 
6280 	/* in case of VF, this will be main SRIOV VSI */
6281 	ch->parent_vsi = vsi;
6282 
6283 	/* and update main_vsi's count for queue_available to use */
6284 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6285 
6286 	return 0;
6287 }
6288 
6289 /**
6290  * i40e_configure_queue_channels - Add queue channel for the given TCs
6291  * @vsi: VSI to be configured
6292  *
6293  * Configures queue channel mapping to the given TCs
6294  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6295 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6296 {
6297 	struct i40e_channel *ch;
6298 	u64 max_rate = 0;
6299 	int ret = 0, i;
6300 
6301 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6302 	vsi->tc_seid_map[0] = vsi->seid;
6303 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6304 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6305 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6306 			if (!ch) {
6307 				ret = -ENOMEM;
6308 				goto err_free;
6309 			}
6310 
6311 			INIT_LIST_HEAD(&ch->list);
6312 			ch->num_queue_pairs =
6313 				vsi->tc_config.tc_info[i].qcount;
6314 			ch->base_queue =
6315 				vsi->tc_config.tc_info[i].qoffset;
6316 
6317 			/* Bandwidth limit through tc interface is in bytes/s,
6318 			 * change to Mbit/s
6319 			 */
6320 			max_rate = vsi->mqprio_qopt.max_rate[i];
6321 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6322 			ch->max_tx_rate = max_rate;
6323 
6324 			list_add_tail(&ch->list, &vsi->ch_list);
6325 
6326 			ret = i40e_create_queue_channel(vsi, ch);
6327 			if (ret) {
6328 				dev_err(&vsi->back->pdev->dev,
6329 					"Failed creating queue channel with TC%d: queues %d\n",
6330 					i, ch->num_queue_pairs);
6331 				goto err_free;
6332 			}
6333 			vsi->tc_seid_map[i] = ch->seid;
6334 		}
6335 	}
6336 	return ret;
6337 
6338 err_free:
6339 	i40e_remove_queue_channels(vsi);
6340 	return ret;
6341 }
6342 
6343 /**
6344  * i40e_veb_config_tc - Configure TCs for given VEB
6345  * @veb: given VEB
6346  * @enabled_tc: TC bitmap
6347  *
6348  * Configures given TC bitmap for VEB (switching) element
6349  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6350 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6351 {
6352 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6353 	struct i40e_pf *pf = veb->pf;
6354 	int ret = 0;
6355 	int i;
6356 
6357 	/* No TCs or already enabled TCs just return */
6358 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6359 		return ret;
6360 
6361 	bw_data.tc_valid_bits = enabled_tc;
6362 	/* bw_data.absolute_credits is not set (relative) */
6363 
6364 	/* Enable ETS TCs with equal BW Share for now */
6365 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6366 		if (enabled_tc & BIT(i))
6367 			bw_data.tc_bw_share_credits[i] = 1;
6368 	}
6369 
6370 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6371 						   &bw_data, NULL);
6372 	if (ret) {
6373 		dev_info(&pf->pdev->dev,
6374 			 "VEB bw config failed, err %s aq_err %s\n",
6375 			 i40e_stat_str(&pf->hw, ret),
6376 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6377 		goto out;
6378 	}
6379 
6380 	/* Update the BW information */
6381 	ret = i40e_veb_get_bw_info(veb);
6382 	if (ret) {
6383 		dev_info(&pf->pdev->dev,
6384 			 "Failed getting veb bw config, err %s aq_err %s\n",
6385 			 i40e_stat_str(&pf->hw, ret),
6386 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6387 	}
6388 
6389 out:
6390 	return ret;
6391 }
6392 
6393 #ifdef CONFIG_I40E_DCB
6394 /**
6395  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6396  * @pf: PF struct
6397  *
6398  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6399  * the caller would've quiesce all the VSIs before calling
6400  * this function
6401  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6402 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6403 {
6404 	u8 tc_map = 0;
6405 	int ret;
6406 	u8 v;
6407 
6408 	/* Enable the TCs available on PF to all VEBs */
6409 	tc_map = i40e_pf_get_tc_map(pf);
6410 	for (v = 0; v < I40E_MAX_VEB; v++) {
6411 		if (!pf->veb[v])
6412 			continue;
6413 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6414 		if (ret) {
6415 			dev_info(&pf->pdev->dev,
6416 				 "Failed configuring TC for VEB seid=%d\n",
6417 				 pf->veb[v]->seid);
6418 			/* Will try to configure as many components */
6419 		}
6420 	}
6421 
6422 	/* Update each VSI */
6423 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6424 		if (!pf->vsi[v])
6425 			continue;
6426 
6427 		/* - Enable all TCs for the LAN VSI
6428 		 * - For all others keep them at TC0 for now
6429 		 */
6430 		if (v == pf->lan_vsi)
6431 			tc_map = i40e_pf_get_tc_map(pf);
6432 		else
6433 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6434 
6435 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6436 		if (ret) {
6437 			dev_info(&pf->pdev->dev,
6438 				 "Failed configuring TC for VSI seid=%d\n",
6439 				 pf->vsi[v]->seid);
6440 			/* Will try to configure as many components */
6441 		} else {
6442 			/* Re-configure VSI vectors based on updated TC map */
6443 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6444 			if (pf->vsi[v]->netdev)
6445 				i40e_dcbnl_set_all(pf->vsi[v]);
6446 		}
6447 	}
6448 }
6449 
6450 /**
6451  * i40e_resume_port_tx - Resume port Tx
6452  * @pf: PF struct
6453  *
6454  * Resume a port's Tx and issue a PF reset in case of failure to
6455  * resume.
6456  **/
i40e_resume_port_tx(struct i40e_pf * pf)6457 static int i40e_resume_port_tx(struct i40e_pf *pf)
6458 {
6459 	struct i40e_hw *hw = &pf->hw;
6460 	int ret;
6461 
6462 	ret = i40e_aq_resume_port_tx(hw, NULL);
6463 	if (ret) {
6464 		dev_info(&pf->pdev->dev,
6465 			 "Resume Port Tx failed, err %s aq_err %s\n",
6466 			  i40e_stat_str(&pf->hw, ret),
6467 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6468 		/* Schedule PF reset to recover */
6469 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6470 		i40e_service_event_schedule(pf);
6471 	}
6472 
6473 	return ret;
6474 }
6475 
6476 /**
6477  * i40e_init_pf_dcb - Initialize DCB configuration
6478  * @pf: PF being configured
6479  *
6480  * Query the current DCB configuration and cache it
6481  * in the hardware structure
6482  **/
i40e_init_pf_dcb(struct i40e_pf * pf)6483 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6484 {
6485 	struct i40e_hw *hw = &pf->hw;
6486 	int err = 0;
6487 
6488 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6489 	 * Also do not enable DCBx if FW LLDP agent is disabled
6490 	 */
6491 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6492 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP)) {
6493 		dev_info(&pf->pdev->dev, "DCB is not supported or FW LLDP is disabled\n");
6494 		err = I40E_NOT_SUPPORTED;
6495 		goto out;
6496 	}
6497 
6498 	err = i40e_init_dcb(hw, true);
6499 	if (!err) {
6500 		/* Device/Function is not DCBX capable */
6501 		if ((!hw->func_caps.dcb) ||
6502 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6503 			dev_info(&pf->pdev->dev,
6504 				 "DCBX offload is not supported or is disabled for this PF.\n");
6505 		} else {
6506 			/* When status is not DISABLED then DCBX in FW */
6507 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6508 				       DCB_CAP_DCBX_VER_IEEE;
6509 
6510 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6511 			/* Enable DCB tagging only when more than one TC
6512 			 * or explicitly disable if only one TC
6513 			 */
6514 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6515 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6516 			else
6517 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6518 			dev_dbg(&pf->pdev->dev,
6519 				"DCBX offload is supported for this PF.\n");
6520 		}
6521 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6522 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6523 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6524 	} else {
6525 		dev_info(&pf->pdev->dev,
6526 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6527 			 i40e_stat_str(&pf->hw, err),
6528 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6529 	}
6530 
6531 out:
6532 	return err;
6533 }
6534 #endif /* CONFIG_I40E_DCB */
6535 
6536 /**
6537  * i40e_print_link_message - print link up or down
6538  * @vsi: the VSI for which link needs a message
6539  * @isup: true of link is up, false otherwise
6540  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)6541 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6542 {
6543 	enum i40e_aq_link_speed new_speed;
6544 	struct i40e_pf *pf = vsi->back;
6545 	char *speed = "Unknown";
6546 	char *fc = "Unknown";
6547 	char *fec = "";
6548 	char *req_fec = "";
6549 	char *an = "";
6550 
6551 	if (isup)
6552 		new_speed = pf->hw.phy.link_info.link_speed;
6553 	else
6554 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6555 
6556 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6557 		return;
6558 	vsi->current_isup = isup;
6559 	vsi->current_speed = new_speed;
6560 	if (!isup) {
6561 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6562 		return;
6563 	}
6564 
6565 	/* Warn user if link speed on NPAR enabled partition is not at
6566 	 * least 10GB
6567 	 */
6568 	if (pf->hw.func_caps.npar_enable &&
6569 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6570 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6571 		netdev_warn(vsi->netdev,
6572 			    "The partition detected link speed that is less than 10Gbps\n");
6573 
6574 	switch (pf->hw.phy.link_info.link_speed) {
6575 	case I40E_LINK_SPEED_40GB:
6576 		speed = "40 G";
6577 		break;
6578 	case I40E_LINK_SPEED_20GB:
6579 		speed = "20 G";
6580 		break;
6581 	case I40E_LINK_SPEED_25GB:
6582 		speed = "25 G";
6583 		break;
6584 	case I40E_LINK_SPEED_10GB:
6585 		speed = "10 G";
6586 		break;
6587 	case I40E_LINK_SPEED_5GB:
6588 		speed = "5 G";
6589 		break;
6590 	case I40E_LINK_SPEED_2_5GB:
6591 		speed = "2.5 G";
6592 		break;
6593 	case I40E_LINK_SPEED_1GB:
6594 		speed = "1000 M";
6595 		break;
6596 	case I40E_LINK_SPEED_100MB:
6597 		speed = "100 M";
6598 		break;
6599 	default:
6600 		break;
6601 	}
6602 
6603 	switch (pf->hw.fc.current_mode) {
6604 	case I40E_FC_FULL:
6605 		fc = "RX/TX";
6606 		break;
6607 	case I40E_FC_TX_PAUSE:
6608 		fc = "TX";
6609 		break;
6610 	case I40E_FC_RX_PAUSE:
6611 		fc = "RX";
6612 		break;
6613 	default:
6614 		fc = "None";
6615 		break;
6616 	}
6617 
6618 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6619 		req_fec = "None";
6620 		fec = "None";
6621 		an = "False";
6622 
6623 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6624 			an = "True";
6625 
6626 		if (pf->hw.phy.link_info.fec_info &
6627 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6628 			fec = "CL74 FC-FEC/BASE-R";
6629 		else if (pf->hw.phy.link_info.fec_info &
6630 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6631 			fec = "CL108 RS-FEC";
6632 
6633 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6634 		 * both RS and FC are requested
6635 		 */
6636 		if (vsi->back->hw.phy.link_info.req_fec_info &
6637 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6638 			if (vsi->back->hw.phy.link_info.req_fec_info &
6639 			    I40E_AQ_REQUEST_FEC_RS)
6640 				req_fec = "CL108 RS-FEC";
6641 			else
6642 				req_fec = "CL74 FC-FEC/BASE-R";
6643 		}
6644 		netdev_info(vsi->netdev,
6645 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
6646 			    speed, req_fec, fec, an, fc);
6647 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
6648 		req_fec = "None";
6649 		fec = "None";
6650 		an = "False";
6651 
6652 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6653 			an = "True";
6654 
6655 		if (pf->hw.phy.link_info.fec_info &
6656 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6657 			fec = "CL74 FC-FEC/BASE-R";
6658 
6659 		if (pf->hw.phy.link_info.req_fec_info &
6660 		    I40E_AQ_REQUEST_FEC_KR)
6661 			req_fec = "CL74 FC-FEC/BASE-R";
6662 
6663 		netdev_info(vsi->netdev,
6664 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
6665 			    speed, req_fec, fec, an, fc);
6666 	} else {
6667 		netdev_info(vsi->netdev,
6668 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
6669 			    speed, fc);
6670 	}
6671 
6672 }
6673 
6674 /**
6675  * i40e_up_complete - Finish the last steps of bringing up a connection
6676  * @vsi: the VSI being configured
6677  **/
i40e_up_complete(struct i40e_vsi * vsi)6678 static int i40e_up_complete(struct i40e_vsi *vsi)
6679 {
6680 	struct i40e_pf *pf = vsi->back;
6681 	int err;
6682 
6683 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6684 		i40e_vsi_configure_msix(vsi);
6685 	else
6686 		i40e_configure_msi_and_legacy(vsi);
6687 
6688 	/* start rings */
6689 	err = i40e_vsi_start_rings(vsi);
6690 	if (err)
6691 		return err;
6692 
6693 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6694 	i40e_napi_enable_all(vsi);
6695 	i40e_vsi_enable_irq(vsi);
6696 
6697 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6698 	    (vsi->netdev)) {
6699 		i40e_print_link_message(vsi, true);
6700 		netif_tx_start_all_queues(vsi->netdev);
6701 		netif_carrier_on(vsi->netdev);
6702 	}
6703 
6704 	/* replay FDIR SB filters */
6705 	if (vsi->type == I40E_VSI_FDIR) {
6706 		/* reset fd counters */
6707 		pf->fd_add_err = 0;
6708 		pf->fd_atr_cnt = 0;
6709 		i40e_fdir_filter_restore(vsi);
6710 	}
6711 
6712 	/* On the next run of the service_task, notify any clients of the new
6713 	 * opened netdev
6714 	 */
6715 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6716 	i40e_service_event_schedule(pf);
6717 
6718 	return 0;
6719 }
6720 
6721 /**
6722  * i40e_vsi_reinit_locked - Reset the VSI
6723  * @vsi: the VSI being configured
6724  *
6725  * Rebuild the ring structs after some configuration
6726  * has changed, e.g. MTU size.
6727  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)6728 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6729 {
6730 	struct i40e_pf *pf = vsi->back;
6731 
6732 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6733 		usleep_range(1000, 2000);
6734 	i40e_down(vsi);
6735 
6736 	i40e_up(vsi);
6737 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6738 }
6739 
6740 /**
6741  * i40e_force_link_state - Force the link status
6742  * @pf: board private structure
6743  * @is_up: whether the link state should be forced up or down
6744  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)6745 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6746 {
6747 	struct i40e_aq_get_phy_abilities_resp abilities;
6748 	struct i40e_aq_set_phy_config config = {0};
6749 	bool non_zero_phy_type = is_up;
6750 	struct i40e_hw *hw = &pf->hw;
6751 	i40e_status err;
6752 	u64 mask;
6753 	u8 speed;
6754 
6755 	/* Card might've been put in an unstable state by other drivers
6756 	 * and applications, which causes incorrect speed values being
6757 	 * set on startup. In order to clear speed registers, we call
6758 	 * get_phy_capabilities twice, once to get initial state of
6759 	 * available speeds, and once to get current PHY config.
6760 	 */
6761 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
6762 					   NULL);
6763 	if (err) {
6764 		dev_err(&pf->pdev->dev,
6765 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6766 			i40e_stat_str(hw, err),
6767 			i40e_aq_str(hw, hw->aq.asq_last_status));
6768 		return err;
6769 	}
6770 	speed = abilities.link_speed;
6771 
6772 	/* Get the current phy config */
6773 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6774 					   NULL);
6775 	if (err) {
6776 		dev_err(&pf->pdev->dev,
6777 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6778 			i40e_stat_str(hw, err),
6779 			i40e_aq_str(hw, hw->aq.asq_last_status));
6780 		return err;
6781 	}
6782 
6783 	/* If link needs to go up, but was not forced to go down,
6784 	 * and its speed values are OK, no need for a flap
6785 	 * if non_zero_phy_type was set, still need to force up
6786 	 */
6787 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
6788 		non_zero_phy_type = true;
6789 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
6790 		return I40E_SUCCESS;
6791 
6792 	/* To force link we need to set bits for all supported PHY types,
6793 	 * but there are now more than 32, so we need to split the bitmap
6794 	 * across two fields.
6795 	 */
6796 	mask = I40E_PHY_TYPES_BITMASK;
6797 	config.phy_type =
6798 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6799 	config.phy_type_ext =
6800 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
6801 	/* Copy the old settings, except of phy_type */
6802 	config.abilities = abilities.abilities;
6803 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
6804 		if (is_up)
6805 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
6806 		else
6807 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
6808 	}
6809 	if (abilities.link_speed != 0)
6810 		config.link_speed = abilities.link_speed;
6811 	else
6812 		config.link_speed = speed;
6813 	config.eee_capability = abilities.eee_capability;
6814 	config.eeer = abilities.eeer_val;
6815 	config.low_power_ctrl = abilities.d3_lpan;
6816 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6817 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
6818 	err = i40e_aq_set_phy_config(hw, &config, NULL);
6819 
6820 	if (err) {
6821 		dev_err(&pf->pdev->dev,
6822 			"set phy config ret =  %s last_status =  %s\n",
6823 			i40e_stat_str(&pf->hw, err),
6824 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6825 		return err;
6826 	}
6827 
6828 	/* Update the link info */
6829 	err = i40e_update_link_info(hw);
6830 	if (err) {
6831 		/* Wait a little bit (on 40G cards it sometimes takes a really
6832 		 * long time for link to come back from the atomic reset)
6833 		 * and try once more
6834 		 */
6835 		msleep(1000);
6836 		i40e_update_link_info(hw);
6837 	}
6838 
6839 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
6840 
6841 	return I40E_SUCCESS;
6842 }
6843 
6844 /**
6845  * i40e_up - Bring the connection back up after being down
6846  * @vsi: the VSI being configured
6847  **/
i40e_up(struct i40e_vsi * vsi)6848 int i40e_up(struct i40e_vsi *vsi)
6849 {
6850 	int err;
6851 
6852 	if (vsi->type == I40E_VSI_MAIN &&
6853 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
6854 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
6855 		i40e_force_link_state(vsi->back, true);
6856 
6857 	err = i40e_vsi_configure(vsi);
6858 	if (!err)
6859 		err = i40e_up_complete(vsi);
6860 
6861 	return err;
6862 }
6863 
6864 /**
6865  * i40e_down - Shutdown the connection processing
6866  * @vsi: the VSI being stopped
6867  **/
i40e_down(struct i40e_vsi * vsi)6868 void i40e_down(struct i40e_vsi *vsi)
6869 {
6870 	int i;
6871 
6872 	/* It is assumed that the caller of this function
6873 	 * sets the vsi->state __I40E_VSI_DOWN bit.
6874 	 */
6875 	if (vsi->netdev) {
6876 		netif_carrier_off(vsi->netdev);
6877 		netif_tx_disable(vsi->netdev);
6878 	}
6879 	i40e_vsi_disable_irq(vsi);
6880 	i40e_vsi_stop_rings(vsi);
6881 	if (vsi->type == I40E_VSI_MAIN &&
6882 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
6883 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
6884 		i40e_force_link_state(vsi->back, false);
6885 	i40e_napi_disable_all(vsi);
6886 
6887 	for (i = 0; i < vsi->num_queue_pairs; i++) {
6888 		i40e_clean_tx_ring(vsi->tx_rings[i]);
6889 		if (i40e_enabled_xdp_vsi(vsi)) {
6890 			/* Make sure that in-progress ndo_xdp_xmit and
6891 			 * ndo_xsk_wakeup calls are completed.
6892 			 */
6893 			synchronize_rcu();
6894 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
6895 		}
6896 		i40e_clean_rx_ring(vsi->rx_rings[i]);
6897 	}
6898 
6899 }
6900 
6901 /**
6902  * i40e_validate_mqprio_qopt- validate queue mapping info
6903  * @vsi: the VSI being configured
6904  * @mqprio_qopt: queue parametrs
6905  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)6906 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6907 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
6908 {
6909 	u64 sum_max_rate = 0;
6910 	u64 max_rate = 0;
6911 	int i;
6912 
6913 	if (mqprio_qopt->qopt.offset[0] != 0 ||
6914 	    mqprio_qopt->qopt.num_tc < 1 ||
6915 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6916 		return -EINVAL;
6917 	for (i = 0; ; i++) {
6918 		if (!mqprio_qopt->qopt.count[i])
6919 			return -EINVAL;
6920 		if (mqprio_qopt->min_rate[i]) {
6921 			dev_err(&vsi->back->pdev->dev,
6922 				"Invalid min tx rate (greater than 0) specified\n");
6923 			return -EINVAL;
6924 		}
6925 		max_rate = mqprio_qopt->max_rate[i];
6926 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6927 		sum_max_rate += max_rate;
6928 
6929 		if (i >= mqprio_qopt->qopt.num_tc - 1)
6930 			break;
6931 		if (mqprio_qopt->qopt.offset[i + 1] !=
6932 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6933 			return -EINVAL;
6934 	}
6935 	if (vsi->num_queue_pairs <
6936 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6937 		dev_err(&vsi->back->pdev->dev,
6938 			"Failed to create traffic channel, insufficient number of queues.\n");
6939 		return -EINVAL;
6940 	}
6941 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
6942 		dev_err(&vsi->back->pdev->dev,
6943 			"Invalid max tx rate specified\n");
6944 		return -EINVAL;
6945 	}
6946 	return 0;
6947 }
6948 
6949 /**
6950  * i40e_vsi_set_default_tc_config - set default values for tc configuration
6951  * @vsi: the VSI being configured
6952  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)6953 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6954 {
6955 	u16 qcount;
6956 	int i;
6957 
6958 	/* Only TC0 is enabled */
6959 	vsi->tc_config.numtc = 1;
6960 	vsi->tc_config.enabled_tc = 1;
6961 	qcount = min_t(int, vsi->alloc_queue_pairs,
6962 		       i40e_pf_get_max_q_per_tc(vsi->back));
6963 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6964 		/* For the TC that is not enabled set the offset to to default
6965 		 * queue and allocate one queue for the given TC.
6966 		 */
6967 		vsi->tc_config.tc_info[i].qoffset = 0;
6968 		if (i == 0)
6969 			vsi->tc_config.tc_info[i].qcount = qcount;
6970 		else
6971 			vsi->tc_config.tc_info[i].qcount = 1;
6972 		vsi->tc_config.tc_info[i].netdev_tc = 0;
6973 	}
6974 }
6975 
6976 /**
6977  * i40e_del_macvlan_filter
6978  * @hw: pointer to the HW structure
6979  * @seid: seid of the channel VSI
6980  * @macaddr: the mac address to apply as a filter
6981  * @aq_err: store the admin Q error
6982  *
6983  * This function deletes a mac filter on the channel VSI which serves as the
6984  * macvlan. Returns 0 on success.
6985  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)6986 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
6987 					   const u8 *macaddr, int *aq_err)
6988 {
6989 	struct i40e_aqc_remove_macvlan_element_data element;
6990 	i40e_status status;
6991 
6992 	memset(&element, 0, sizeof(element));
6993 	ether_addr_copy(element.mac_addr, macaddr);
6994 	element.vlan_tag = 0;
6995 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
6996 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
6997 	*aq_err = hw->aq.asq_last_status;
6998 
6999 	return status;
7000 }
7001 
7002 /**
7003  * i40e_add_macvlan_filter
7004  * @hw: pointer to the HW structure
7005  * @seid: seid of the channel VSI
7006  * @macaddr: the mac address to apply as a filter
7007  * @aq_err: store the admin Q error
7008  *
7009  * This function adds a mac filter on the channel VSI which serves as the
7010  * macvlan. Returns 0 on success.
7011  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7012 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7013 					   const u8 *macaddr, int *aq_err)
7014 {
7015 	struct i40e_aqc_add_macvlan_element_data element;
7016 	i40e_status status;
7017 	u16 cmd_flags = 0;
7018 
7019 	ether_addr_copy(element.mac_addr, macaddr);
7020 	element.vlan_tag = 0;
7021 	element.queue_number = 0;
7022 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7023 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7024 	element.flags = cpu_to_le16(cmd_flags);
7025 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7026 	*aq_err = hw->aq.asq_last_status;
7027 
7028 	return status;
7029 }
7030 
7031 /**
7032  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7033  * @vsi: the VSI we want to access
7034  * @ch: the channel we want to access
7035  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7036 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7037 {
7038 	struct i40e_ring *tx_ring, *rx_ring;
7039 	u16 pf_q;
7040 	int i;
7041 
7042 	for (i = 0; i < ch->num_queue_pairs; i++) {
7043 		pf_q = ch->base_queue + i;
7044 		tx_ring = vsi->tx_rings[pf_q];
7045 		tx_ring->ch = NULL;
7046 		rx_ring = vsi->rx_rings[pf_q];
7047 		rx_ring->ch = NULL;
7048 	}
7049 }
7050 
7051 /**
7052  * i40e_free_macvlan_channels
7053  * @vsi: the VSI we want to access
7054  *
7055  * This function frees the Qs of the channel VSI from
7056  * the stack and also deletes the channel VSIs which
7057  * serve as macvlans.
7058  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7059 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7060 {
7061 	struct i40e_channel *ch, *ch_tmp;
7062 	int ret;
7063 
7064 	if (list_empty(&vsi->macvlan_list))
7065 		return;
7066 
7067 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7068 		struct i40e_vsi *parent_vsi;
7069 
7070 		if (i40e_is_channel_macvlan(ch)) {
7071 			i40e_reset_ch_rings(vsi, ch);
7072 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7073 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7074 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7075 			kfree(ch->fwd);
7076 			ch->fwd = NULL;
7077 		}
7078 
7079 		list_del(&ch->list);
7080 		parent_vsi = ch->parent_vsi;
7081 		if (!parent_vsi || !ch->initialized) {
7082 			kfree(ch);
7083 			continue;
7084 		}
7085 
7086 		/* remove the VSI */
7087 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7088 					     NULL);
7089 		if (ret)
7090 			dev_err(&vsi->back->pdev->dev,
7091 				"unable to remove channel (%d) for parent VSI(%d)\n",
7092 				ch->seid, parent_vsi->seid);
7093 		kfree(ch);
7094 	}
7095 	vsi->macvlan_cnt = 0;
7096 }
7097 
7098 /**
7099  * i40e_fwd_ring_up - bring the macvlan device up
7100  * @vsi: the VSI we want to access
7101  * @vdev: macvlan netdevice
7102  * @fwd: the private fwd structure
7103  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7104 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7105 			    struct i40e_fwd_adapter *fwd)
7106 {
7107 	int ret = 0, num_tc = 1,  i, aq_err;
7108 	struct i40e_channel *ch, *ch_tmp;
7109 	struct i40e_pf *pf = vsi->back;
7110 	struct i40e_hw *hw = &pf->hw;
7111 
7112 	if (list_empty(&vsi->macvlan_list))
7113 		return -EINVAL;
7114 
7115 	/* Go through the list and find an available channel */
7116 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7117 		if (!i40e_is_channel_macvlan(ch)) {
7118 			ch->fwd = fwd;
7119 			/* record configuration for macvlan interface in vdev */
7120 			for (i = 0; i < num_tc; i++)
7121 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7122 							     i,
7123 							     ch->num_queue_pairs,
7124 							     ch->base_queue);
7125 			for (i = 0; i < ch->num_queue_pairs; i++) {
7126 				struct i40e_ring *tx_ring, *rx_ring;
7127 				u16 pf_q;
7128 
7129 				pf_q = ch->base_queue + i;
7130 
7131 				/* Get to TX ring ptr */
7132 				tx_ring = vsi->tx_rings[pf_q];
7133 				tx_ring->ch = ch;
7134 
7135 				/* Get the RX ring ptr */
7136 				rx_ring = vsi->rx_rings[pf_q];
7137 				rx_ring->ch = ch;
7138 			}
7139 			break;
7140 		}
7141 	}
7142 
7143 	/* Guarantee all rings are updated before we update the
7144 	 * MAC address filter.
7145 	 */
7146 	wmb();
7147 
7148 	/* Add a mac filter */
7149 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7150 	if (ret) {
7151 		/* if we cannot add the MAC rule then disable the offload */
7152 		macvlan_release_l2fw_offload(vdev);
7153 		for (i = 0; i < ch->num_queue_pairs; i++) {
7154 			struct i40e_ring *rx_ring;
7155 			u16 pf_q;
7156 
7157 			pf_q = ch->base_queue + i;
7158 			rx_ring = vsi->rx_rings[pf_q];
7159 			rx_ring->netdev = NULL;
7160 		}
7161 		dev_info(&pf->pdev->dev,
7162 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7163 			  i40e_stat_str(hw, ret),
7164 			  i40e_aq_str(hw, aq_err));
7165 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7166 	}
7167 
7168 	return ret;
7169 }
7170 
7171 /**
7172  * i40e_setup_macvlans - create the channels which will be macvlans
7173  * @vsi: the VSI we want to access
7174  * @macvlan_cnt: no. of macvlans to be setup
7175  * @qcnt: no. of Qs per macvlan
7176  * @vdev: macvlan netdevice
7177  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7178 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7179 			       struct net_device *vdev)
7180 {
7181 	struct i40e_pf *pf = vsi->back;
7182 	struct i40e_hw *hw = &pf->hw;
7183 	struct i40e_vsi_context ctxt;
7184 	u16 sections, qmap, num_qps;
7185 	struct i40e_channel *ch;
7186 	int i, pow, ret = 0;
7187 	u8 offset = 0;
7188 
7189 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7190 		return -EINVAL;
7191 
7192 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7193 
7194 	/* find the next higher power-of-2 of num queue pairs */
7195 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7196 
7197 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7198 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7199 
7200 	/* Setup context bits for the main VSI */
7201 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7202 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7203 	memset(&ctxt, 0, sizeof(ctxt));
7204 	ctxt.seid = vsi->seid;
7205 	ctxt.pf_num = vsi->back->hw.pf_id;
7206 	ctxt.vf_num = 0;
7207 	ctxt.uplink_seid = vsi->uplink_seid;
7208 	ctxt.info = vsi->info;
7209 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7210 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7211 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7212 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7213 
7214 	/* Reconfigure RSS for main VSI with new max queue count */
7215 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7216 	ret = i40e_vsi_config_rss(vsi);
7217 	if (ret) {
7218 		dev_info(&pf->pdev->dev,
7219 			 "Failed to reconfig RSS for num_queues (%u)\n",
7220 			 vsi->rss_size);
7221 		return ret;
7222 	}
7223 	vsi->reconfig_rss = true;
7224 	dev_dbg(&vsi->back->pdev->dev,
7225 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7226 	vsi->next_base_queue = num_qps;
7227 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7228 
7229 	/* Update the VSI after updating the VSI queue-mapping
7230 	 * information
7231 	 */
7232 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7233 	if (ret) {
7234 		dev_info(&pf->pdev->dev,
7235 			 "Update vsi tc config failed, err %s aq_err %s\n",
7236 			 i40e_stat_str(hw, ret),
7237 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7238 		return ret;
7239 	}
7240 	/* update the local VSI info with updated queue map */
7241 	i40e_vsi_update_queue_map(vsi, &ctxt);
7242 	vsi->info.valid_sections = 0;
7243 
7244 	/* Create channels for macvlans */
7245 	INIT_LIST_HEAD(&vsi->macvlan_list);
7246 	for (i = 0; i < macvlan_cnt; i++) {
7247 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7248 		if (!ch) {
7249 			ret = -ENOMEM;
7250 			goto err_free;
7251 		}
7252 		INIT_LIST_HEAD(&ch->list);
7253 		ch->num_queue_pairs = qcnt;
7254 		if (!i40e_setup_channel(pf, vsi, ch)) {
7255 			ret = -EINVAL;
7256 			kfree(ch);
7257 			goto err_free;
7258 		}
7259 		ch->parent_vsi = vsi;
7260 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7261 		vsi->macvlan_cnt++;
7262 		list_add_tail(&ch->list, &vsi->macvlan_list);
7263 	}
7264 
7265 	return ret;
7266 
7267 err_free:
7268 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7269 	i40e_free_macvlan_channels(vsi);
7270 
7271 	return ret;
7272 }
7273 
7274 /**
7275  * i40e_fwd_add - configure macvlans
7276  * @netdev: net device to configure
7277  * @vdev: macvlan netdevice
7278  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)7279 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7280 {
7281 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7282 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7283 	struct i40e_vsi *vsi = np->vsi;
7284 	struct i40e_pf *pf = vsi->back;
7285 	struct i40e_fwd_adapter *fwd;
7286 	int avail_macvlan, ret;
7287 
7288 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7289 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7290 		return ERR_PTR(-EINVAL);
7291 	}
7292 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7293 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7294 		return ERR_PTR(-EINVAL);
7295 	}
7296 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7297 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7298 		return ERR_PTR(-EINVAL);
7299 	}
7300 
7301 	/* The macvlan device has to be a single Q device so that the
7302 	 * tc_to_txq field can be reused to pick the tx queue.
7303 	 */
7304 	if (netif_is_multiqueue(vdev))
7305 		return ERR_PTR(-ERANGE);
7306 
7307 	if (!vsi->macvlan_cnt) {
7308 		/* reserve bit 0 for the pf device */
7309 		set_bit(0, vsi->fwd_bitmask);
7310 
7311 		/* Try to reserve as many queues as possible for macvlans. First
7312 		 * reserve 3/4th of max vectors, then half, then quarter and
7313 		 * calculate Qs per macvlan as you go
7314 		 */
7315 		vectors = pf->num_lan_msix;
7316 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7317 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7318 			q_per_macvlan = 4;
7319 			macvlan_cnt = (vectors - 32) / 4;
7320 		} else if (vectors <= 64 && vectors > 32) {
7321 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7322 			q_per_macvlan = 2;
7323 			macvlan_cnt = (vectors - 16) / 2;
7324 		} else if (vectors <= 32 && vectors > 16) {
7325 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7326 			q_per_macvlan = 1;
7327 			macvlan_cnt = vectors - 16;
7328 		} else if (vectors <= 16 && vectors > 8) {
7329 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7330 			q_per_macvlan = 1;
7331 			macvlan_cnt = vectors - 8;
7332 		} else {
7333 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7334 			q_per_macvlan = 1;
7335 			macvlan_cnt = vectors - 1;
7336 		}
7337 
7338 		if (macvlan_cnt == 0)
7339 			return ERR_PTR(-EBUSY);
7340 
7341 		/* Quiesce VSI queues */
7342 		i40e_quiesce_vsi(vsi);
7343 
7344 		/* sets up the macvlans but does not "enable" them */
7345 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7346 					  vdev);
7347 		if (ret)
7348 			return ERR_PTR(ret);
7349 
7350 		/* Unquiesce VSI */
7351 		i40e_unquiesce_vsi(vsi);
7352 	}
7353 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7354 					    vsi->macvlan_cnt);
7355 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7356 		return ERR_PTR(-EBUSY);
7357 
7358 	/* create the fwd struct */
7359 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7360 	if (!fwd)
7361 		return ERR_PTR(-ENOMEM);
7362 
7363 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7364 	fwd->bit_no = avail_macvlan;
7365 	netdev_set_sb_channel(vdev, avail_macvlan);
7366 	fwd->netdev = vdev;
7367 
7368 	if (!netif_running(netdev))
7369 		return fwd;
7370 
7371 	/* Set fwd ring up */
7372 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7373 	if (ret) {
7374 		/* unbind the queues and drop the subordinate channel config */
7375 		netdev_unbind_sb_channel(netdev, vdev);
7376 		netdev_set_sb_channel(vdev, 0);
7377 
7378 		kfree(fwd);
7379 		return ERR_PTR(-EINVAL);
7380 	}
7381 
7382 	return fwd;
7383 }
7384 
7385 /**
7386  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7387  * @vsi: the VSI we want to access
7388  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)7389 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7390 {
7391 	struct i40e_channel *ch, *ch_tmp;
7392 	struct i40e_pf *pf = vsi->back;
7393 	struct i40e_hw *hw = &pf->hw;
7394 	int aq_err, ret = 0;
7395 
7396 	if (list_empty(&vsi->macvlan_list))
7397 		return;
7398 
7399 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7400 		if (i40e_is_channel_macvlan(ch)) {
7401 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7402 						      i40e_channel_mac(ch),
7403 						      &aq_err);
7404 			if (!ret) {
7405 				/* Reset queue contexts */
7406 				i40e_reset_ch_rings(vsi, ch);
7407 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7408 				netdev_unbind_sb_channel(vsi->netdev,
7409 							 ch->fwd->netdev);
7410 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7411 				kfree(ch->fwd);
7412 				ch->fwd = NULL;
7413 			}
7414 		}
7415 	}
7416 }
7417 
7418 /**
7419  * i40e_fwd_del - delete macvlan interfaces
7420  * @netdev: net device to configure
7421  * @vdev: macvlan netdevice
7422  */
i40e_fwd_del(struct net_device * netdev,void * vdev)7423 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7424 {
7425 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7426 	struct i40e_fwd_adapter *fwd = vdev;
7427 	struct i40e_channel *ch, *ch_tmp;
7428 	struct i40e_vsi *vsi = np->vsi;
7429 	struct i40e_pf *pf = vsi->back;
7430 	struct i40e_hw *hw = &pf->hw;
7431 	int aq_err, ret = 0;
7432 
7433 	/* Find the channel associated with the macvlan and del mac filter */
7434 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7435 		if (i40e_is_channel_macvlan(ch) &&
7436 		    ether_addr_equal(i40e_channel_mac(ch),
7437 				     fwd->netdev->dev_addr)) {
7438 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7439 						      i40e_channel_mac(ch),
7440 						      &aq_err);
7441 			if (!ret) {
7442 				/* Reset queue contexts */
7443 				i40e_reset_ch_rings(vsi, ch);
7444 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7445 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7446 				netdev_set_sb_channel(fwd->netdev, 0);
7447 				kfree(ch->fwd);
7448 				ch->fwd = NULL;
7449 			} else {
7450 				dev_info(&pf->pdev->dev,
7451 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7452 					  i40e_stat_str(hw, ret),
7453 					  i40e_aq_str(hw, aq_err));
7454 			}
7455 			break;
7456 		}
7457 	}
7458 }
7459 
7460 /**
7461  * i40e_setup_tc - configure multiple traffic classes
7462  * @netdev: net device to configure
7463  * @type_data: tc offload data
7464  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)7465 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7466 {
7467 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7468 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7469 	struct i40e_vsi *vsi = np->vsi;
7470 	struct i40e_pf *pf = vsi->back;
7471 	u8 enabled_tc = 0, num_tc, hw;
7472 	bool need_reset = false;
7473 	int old_queue_pairs;
7474 	int ret = -EINVAL;
7475 	u16 mode;
7476 	int i;
7477 
7478 	old_queue_pairs = vsi->num_queue_pairs;
7479 	num_tc = mqprio_qopt->qopt.num_tc;
7480 	hw = mqprio_qopt->qopt.hw;
7481 	mode = mqprio_qopt->mode;
7482 	if (!hw) {
7483 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7484 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7485 		goto config_tc;
7486 	}
7487 
7488 	/* Check if MFP enabled */
7489 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7490 		netdev_info(netdev,
7491 			    "Configuring TC not supported in MFP mode\n");
7492 		return ret;
7493 	}
7494 	switch (mode) {
7495 	case TC_MQPRIO_MODE_DCB:
7496 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7497 
7498 		/* Check if DCB enabled to continue */
7499 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7500 			netdev_info(netdev,
7501 				    "DCB is not enabled for adapter\n");
7502 			return ret;
7503 		}
7504 
7505 		/* Check whether tc count is within enabled limit */
7506 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7507 			netdev_info(netdev,
7508 				    "TC count greater than enabled on link for adapter\n");
7509 			return ret;
7510 		}
7511 		break;
7512 	case TC_MQPRIO_MODE_CHANNEL:
7513 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7514 			netdev_info(netdev,
7515 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7516 			return ret;
7517 		}
7518 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7519 			return ret;
7520 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7521 		if (ret)
7522 			return ret;
7523 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7524 		       sizeof(*mqprio_qopt));
7525 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7526 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7527 		break;
7528 	default:
7529 		return -EINVAL;
7530 	}
7531 
7532 config_tc:
7533 	/* Generate TC map for number of tc requested */
7534 	for (i = 0; i < num_tc; i++)
7535 		enabled_tc |= BIT(i);
7536 
7537 	/* Requesting same TC configuration as already enabled */
7538 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7539 	    mode != TC_MQPRIO_MODE_CHANNEL)
7540 		return 0;
7541 
7542 	/* Quiesce VSI queues */
7543 	i40e_quiesce_vsi(vsi);
7544 
7545 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7546 		i40e_remove_queue_channels(vsi);
7547 
7548 	/* Configure VSI for enabled TCs */
7549 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7550 	if (ret) {
7551 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7552 			    vsi->seid);
7553 		need_reset = true;
7554 		goto exit;
7555 	} else {
7556 		dev_info(&vsi->back->pdev->dev,
7557 			 "Setup channel (id:%u) utilizing num_queues %d\n",
7558 			 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7559 	}
7560 
7561 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
7562 		if (vsi->mqprio_qopt.max_rate[0]) {
7563 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
7564 
7565 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
7566 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
7567 			if (!ret) {
7568 				u64 credits = max_tx_rate;
7569 
7570 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
7571 				dev_dbg(&vsi->back->pdev->dev,
7572 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
7573 					max_tx_rate,
7574 					credits,
7575 					vsi->seid);
7576 			} else {
7577 				need_reset = true;
7578 				goto exit;
7579 			}
7580 		}
7581 		ret = i40e_configure_queue_channels(vsi);
7582 		if (ret) {
7583 			vsi->num_queue_pairs = old_queue_pairs;
7584 			netdev_info(netdev,
7585 				    "Failed configuring queue channels\n");
7586 			need_reset = true;
7587 			goto exit;
7588 		}
7589 	}
7590 
7591 exit:
7592 	/* Reset the configuration data to defaults, only TC0 is enabled */
7593 	if (need_reset) {
7594 		i40e_vsi_set_default_tc_config(vsi);
7595 		need_reset = false;
7596 	}
7597 
7598 	/* Unquiesce VSI */
7599 	i40e_unquiesce_vsi(vsi);
7600 	return ret;
7601 }
7602 
7603 /**
7604  * i40e_set_cld_element - sets cloud filter element data
7605  * @filter: cloud filter rule
7606  * @cld: ptr to cloud filter element data
7607  *
7608  * This is helper function to copy data into cloud filter element
7609  **/
7610 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)7611 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7612 		     struct i40e_aqc_cloud_filters_element_data *cld)
7613 {
7614 	u32 ipa;
7615 	int i;
7616 
7617 	memset(cld, 0, sizeof(*cld));
7618 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7619 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7620 
7621 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
7622 		return;
7623 
7624 	if (filter->n_proto == ETH_P_IPV6) {
7625 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
7626 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
7627 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
7628 
7629 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
7630 		}
7631 	} else {
7632 		ipa = be32_to_cpu(filter->dst_ipv4);
7633 
7634 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
7635 	}
7636 
7637 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
7638 
7639 	/* tenant_id is not supported by FW now, once the support is enabled
7640 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
7641 	 */
7642 	if (filter->tenant_id)
7643 		return;
7644 }
7645 
7646 /**
7647  * i40e_add_del_cloud_filter - Add/del cloud filter
7648  * @vsi: pointer to VSI
7649  * @filter: cloud filter rule
7650  * @add: if true, add, if false, delete
7651  *
7652  * Add or delete a cloud filter for a specific flow spec.
7653  * Returns 0 if the filter were successfully added.
7654  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)7655 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
7656 			      struct i40e_cloud_filter *filter, bool add)
7657 {
7658 	struct i40e_aqc_cloud_filters_element_data cld_filter;
7659 	struct i40e_pf *pf = vsi->back;
7660 	int ret;
7661 	static const u16 flag_table[128] = {
7662 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
7663 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
7664 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
7665 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
7666 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
7667 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
7668 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
7669 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
7670 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
7671 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
7672 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
7673 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
7674 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
7675 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
7676 	};
7677 
7678 	if (filter->flags >= ARRAY_SIZE(flag_table))
7679 		return I40E_ERR_CONFIG;
7680 
7681 	memset(&cld_filter, 0, sizeof(cld_filter));
7682 
7683 	/* copy element needed to add cloud filter from filter */
7684 	i40e_set_cld_element(filter, &cld_filter);
7685 
7686 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
7687 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
7688 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
7689 
7690 	if (filter->n_proto == ETH_P_IPV6)
7691 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7692 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7693 	else
7694 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7695 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7696 
7697 	if (add)
7698 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
7699 						&cld_filter, 1);
7700 	else
7701 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
7702 						&cld_filter, 1);
7703 	if (ret)
7704 		dev_dbg(&pf->pdev->dev,
7705 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
7706 			add ? "add" : "delete", filter->dst_port, ret,
7707 			pf->hw.aq.asq_last_status);
7708 	else
7709 		dev_info(&pf->pdev->dev,
7710 			 "%s cloud filter for VSI: %d\n",
7711 			 add ? "Added" : "Deleted", filter->seid);
7712 	return ret;
7713 }
7714 
7715 /**
7716  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
7717  * @vsi: pointer to VSI
7718  * @filter: cloud filter rule
7719  * @add: if true, add, if false, delete
7720  *
7721  * Add or delete a cloud filter for a specific flow spec using big buffer.
7722  * Returns 0 if the filter were successfully added.
7723  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)7724 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
7725 				      struct i40e_cloud_filter *filter,
7726 				      bool add)
7727 {
7728 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
7729 	struct i40e_pf *pf = vsi->back;
7730 	int ret;
7731 
7732 	/* Both (src/dst) valid mac_addr are not supported */
7733 	if ((is_valid_ether_addr(filter->dst_mac) &&
7734 	     is_valid_ether_addr(filter->src_mac)) ||
7735 	    (is_multicast_ether_addr(filter->dst_mac) &&
7736 	     is_multicast_ether_addr(filter->src_mac)))
7737 		return -EOPNOTSUPP;
7738 
7739 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7740 	 * ports are not supported via big buffer now.
7741 	 */
7742 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7743 		return -EOPNOTSUPP;
7744 
7745 	/* adding filter using src_port/src_ip is not supported at this stage */
7746 	if (filter->src_port ||
7747 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
7748 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
7749 		return -EOPNOTSUPP;
7750 
7751 	memset(&cld_filter, 0, sizeof(cld_filter));
7752 
7753 	/* copy element needed to add cloud filter from filter */
7754 	i40e_set_cld_element(filter, &cld_filter.element);
7755 
7756 	if (is_valid_ether_addr(filter->dst_mac) ||
7757 	    is_valid_ether_addr(filter->src_mac) ||
7758 	    is_multicast_ether_addr(filter->dst_mac) ||
7759 	    is_multicast_ether_addr(filter->src_mac)) {
7760 		/* MAC + IP : unsupported mode */
7761 		if (filter->dst_ipv4)
7762 			return -EOPNOTSUPP;
7763 
7764 		/* since we validated that L4 port must be valid before
7765 		 * we get here, start with respective "flags" value
7766 		 * and update if vlan is present or not
7767 		 */
7768 		cld_filter.element.flags =
7769 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7770 
7771 		if (filter->vlan_id) {
7772 			cld_filter.element.flags =
7773 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7774 		}
7775 
7776 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
7777 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7778 		cld_filter.element.flags =
7779 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7780 		if (filter->n_proto == ETH_P_IPV6)
7781 			cld_filter.element.flags |=
7782 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7783 		else
7784 			cld_filter.element.flags |=
7785 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7786 	} else {
7787 		dev_err(&pf->pdev->dev,
7788 			"either mac or ip has to be valid for cloud filter\n");
7789 		return -EINVAL;
7790 	}
7791 
7792 	/* Now copy L4 port in Byte 6..7 in general fields */
7793 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7794 						be16_to_cpu(filter->dst_port);
7795 
7796 	if (add) {
7797 		/* Validate current device switch mode, change if necessary */
7798 		ret = i40e_validate_and_set_switch_mode(vsi);
7799 		if (ret) {
7800 			dev_err(&pf->pdev->dev,
7801 				"failed to set switch mode, ret %d\n",
7802 				ret);
7803 			return ret;
7804 		}
7805 
7806 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7807 						   &cld_filter, 1);
7808 	} else {
7809 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7810 						   &cld_filter, 1);
7811 	}
7812 
7813 	if (ret)
7814 		dev_dbg(&pf->pdev->dev,
7815 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7816 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7817 	else
7818 		dev_info(&pf->pdev->dev,
7819 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7820 			 add ? "add" : "delete", filter->seid,
7821 			 ntohs(filter->dst_port));
7822 	return ret;
7823 }
7824 
7825 /**
7826  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7827  * @vsi: Pointer to VSI
7828  * @f: Pointer to struct flow_cls_offload
7829  * @filter: Pointer to cloud filter structure
7830  *
7831  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)7832 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7833 				 struct flow_cls_offload *f,
7834 				 struct i40e_cloud_filter *filter)
7835 {
7836 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
7837 	struct flow_dissector *dissector = rule->match.dissector;
7838 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7839 	struct i40e_pf *pf = vsi->back;
7840 	u8 field_flags = 0;
7841 
7842 	if (dissector->used_keys &
7843 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7844 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
7845 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7846 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
7847 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7848 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7849 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
7850 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7851 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7852 			dissector->used_keys);
7853 		return -EOPNOTSUPP;
7854 	}
7855 
7856 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7857 		struct flow_match_enc_keyid match;
7858 
7859 		flow_rule_match_enc_keyid(rule, &match);
7860 		if (match.mask->keyid != 0)
7861 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7862 
7863 		filter->tenant_id = be32_to_cpu(match.key->keyid);
7864 	}
7865 
7866 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
7867 		struct flow_match_basic match;
7868 
7869 		flow_rule_match_basic(rule, &match);
7870 		n_proto_key = ntohs(match.key->n_proto);
7871 		n_proto_mask = ntohs(match.mask->n_proto);
7872 
7873 		if (n_proto_key == ETH_P_ALL) {
7874 			n_proto_key = 0;
7875 			n_proto_mask = 0;
7876 		}
7877 		filter->n_proto = n_proto_key & n_proto_mask;
7878 		filter->ip_proto = match.key->ip_proto;
7879 	}
7880 
7881 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7882 		struct flow_match_eth_addrs match;
7883 
7884 		flow_rule_match_eth_addrs(rule, &match);
7885 
7886 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
7887 		if (!is_zero_ether_addr(match.mask->dst)) {
7888 			if (is_broadcast_ether_addr(match.mask->dst)) {
7889 				field_flags |= I40E_CLOUD_FIELD_OMAC;
7890 			} else {
7891 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7892 					match.mask->dst);
7893 				return I40E_ERR_CONFIG;
7894 			}
7895 		}
7896 
7897 		if (!is_zero_ether_addr(match.mask->src)) {
7898 			if (is_broadcast_ether_addr(match.mask->src)) {
7899 				field_flags |= I40E_CLOUD_FIELD_IMAC;
7900 			} else {
7901 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7902 					match.mask->src);
7903 				return I40E_ERR_CONFIG;
7904 			}
7905 		}
7906 		ether_addr_copy(filter->dst_mac, match.key->dst);
7907 		ether_addr_copy(filter->src_mac, match.key->src);
7908 	}
7909 
7910 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
7911 		struct flow_match_vlan match;
7912 
7913 		flow_rule_match_vlan(rule, &match);
7914 		if (match.mask->vlan_id) {
7915 			if (match.mask->vlan_id == VLAN_VID_MASK) {
7916 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
7917 
7918 			} else {
7919 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7920 					match.mask->vlan_id);
7921 				return I40E_ERR_CONFIG;
7922 			}
7923 		}
7924 
7925 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
7926 	}
7927 
7928 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
7929 		struct flow_match_control match;
7930 
7931 		flow_rule_match_control(rule, &match);
7932 		addr_type = match.key->addr_type;
7933 	}
7934 
7935 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7936 		struct flow_match_ipv4_addrs match;
7937 
7938 		flow_rule_match_ipv4_addrs(rule, &match);
7939 		if (match.mask->dst) {
7940 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
7941 				field_flags |= I40E_CLOUD_FIELD_IIP;
7942 			} else {
7943 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
7944 					&match.mask->dst);
7945 				return I40E_ERR_CONFIG;
7946 			}
7947 		}
7948 
7949 		if (match.mask->src) {
7950 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
7951 				field_flags |= I40E_CLOUD_FIELD_IIP;
7952 			} else {
7953 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
7954 					&match.mask->src);
7955 				return I40E_ERR_CONFIG;
7956 			}
7957 		}
7958 
7959 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7960 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7961 			return I40E_ERR_CONFIG;
7962 		}
7963 		filter->dst_ipv4 = match.key->dst;
7964 		filter->src_ipv4 = match.key->src;
7965 	}
7966 
7967 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7968 		struct flow_match_ipv6_addrs match;
7969 
7970 		flow_rule_match_ipv6_addrs(rule, &match);
7971 
7972 		/* src and dest IPV6 address should not be LOOPBACK
7973 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7974 		 */
7975 		if (ipv6_addr_loopback(&match.key->dst) ||
7976 		    ipv6_addr_loopback(&match.key->src)) {
7977 			dev_err(&pf->pdev->dev,
7978 				"Bad ipv6, addr is LOOPBACK\n");
7979 			return I40E_ERR_CONFIG;
7980 		}
7981 		if (!ipv6_addr_any(&match.mask->dst) ||
7982 		    !ipv6_addr_any(&match.mask->src))
7983 			field_flags |= I40E_CLOUD_FIELD_IIP;
7984 
7985 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
7986 		       sizeof(filter->src_ipv6));
7987 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
7988 		       sizeof(filter->dst_ipv6));
7989 	}
7990 
7991 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
7992 		struct flow_match_ports match;
7993 
7994 		flow_rule_match_ports(rule, &match);
7995 		if (match.mask->src) {
7996 			if (match.mask->src == cpu_to_be16(0xffff)) {
7997 				field_flags |= I40E_CLOUD_FIELD_IIP;
7998 			} else {
7999 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8000 					be16_to_cpu(match.mask->src));
8001 				return I40E_ERR_CONFIG;
8002 			}
8003 		}
8004 
8005 		if (match.mask->dst) {
8006 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8007 				field_flags |= I40E_CLOUD_FIELD_IIP;
8008 			} else {
8009 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8010 					be16_to_cpu(match.mask->dst));
8011 				return I40E_ERR_CONFIG;
8012 			}
8013 		}
8014 
8015 		filter->dst_port = match.key->dst;
8016 		filter->src_port = match.key->src;
8017 
8018 		switch (filter->ip_proto) {
8019 		case IPPROTO_TCP:
8020 		case IPPROTO_UDP:
8021 			break;
8022 		default:
8023 			dev_err(&pf->pdev->dev,
8024 				"Only UDP and TCP transport are supported\n");
8025 			return -EINVAL;
8026 		}
8027 	}
8028 	filter->flags = field_flags;
8029 	return 0;
8030 }
8031 
8032 /**
8033  * i40e_handle_tclass: Forward to a traffic class on the device
8034  * @vsi: Pointer to VSI
8035  * @tc: traffic class index on the device
8036  * @filter: Pointer to cloud filter structure
8037  *
8038  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8039 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8040 			      struct i40e_cloud_filter *filter)
8041 {
8042 	struct i40e_channel *ch, *ch_tmp;
8043 
8044 	/* direct to a traffic class on the same device */
8045 	if (tc == 0) {
8046 		filter->seid = vsi->seid;
8047 		return 0;
8048 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8049 		if (!filter->dst_port) {
8050 			dev_err(&vsi->back->pdev->dev,
8051 				"Specify destination port to direct to traffic class that is not default\n");
8052 			return -EINVAL;
8053 		}
8054 		if (list_empty(&vsi->ch_list))
8055 			return -EINVAL;
8056 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8057 					 list) {
8058 			if (ch->seid == vsi->tc_seid_map[tc])
8059 				filter->seid = ch->seid;
8060 		}
8061 		return 0;
8062 	}
8063 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8064 	return -EINVAL;
8065 }
8066 
8067 /**
8068  * i40e_configure_clsflower - Configure tc flower filters
8069  * @vsi: Pointer to VSI
8070  * @cls_flower: Pointer to struct flow_cls_offload
8071  *
8072  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8073 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8074 				    struct flow_cls_offload *cls_flower)
8075 {
8076 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8077 	struct i40e_cloud_filter *filter = NULL;
8078 	struct i40e_pf *pf = vsi->back;
8079 	int err = 0;
8080 
8081 	if (tc < 0) {
8082 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8083 		return -EOPNOTSUPP;
8084 	}
8085 
8086 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8087 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8088 		return -EBUSY;
8089 
8090 	if (pf->fdir_pf_active_filters ||
8091 	    (!hlist_empty(&pf->fdir_filter_list))) {
8092 		dev_err(&vsi->back->pdev->dev,
8093 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8094 		return -EINVAL;
8095 	}
8096 
8097 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8098 		dev_err(&vsi->back->pdev->dev,
8099 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8100 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8101 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8102 	}
8103 
8104 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8105 	if (!filter)
8106 		return -ENOMEM;
8107 
8108 	filter->cookie = cls_flower->cookie;
8109 
8110 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8111 	if (err < 0)
8112 		goto err;
8113 
8114 	err = i40e_handle_tclass(vsi, tc, filter);
8115 	if (err < 0)
8116 		goto err;
8117 
8118 	/* Add cloud filter */
8119 	if (filter->dst_port)
8120 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8121 	else
8122 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8123 
8124 	if (err) {
8125 		dev_err(&pf->pdev->dev,
8126 			"Failed to add cloud filter, err %s\n",
8127 			i40e_stat_str(&pf->hw, err));
8128 		goto err;
8129 	}
8130 
8131 	/* add filter to the ordered list */
8132 	INIT_HLIST_NODE(&filter->cloud_node);
8133 
8134 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8135 
8136 	pf->num_cloud_filters++;
8137 
8138 	return err;
8139 err:
8140 	kfree(filter);
8141 	return err;
8142 }
8143 
8144 /**
8145  * i40e_find_cloud_filter - Find the could filter in the list
8146  * @vsi: Pointer to VSI
8147  * @cookie: filter specific cookie
8148  *
8149  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8150 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8151 							unsigned long *cookie)
8152 {
8153 	struct i40e_cloud_filter *filter = NULL;
8154 	struct hlist_node *node2;
8155 
8156 	hlist_for_each_entry_safe(filter, node2,
8157 				  &vsi->back->cloud_filter_list, cloud_node)
8158 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8159 			return filter;
8160 	return NULL;
8161 }
8162 
8163 /**
8164  * i40e_delete_clsflower - Remove tc flower filters
8165  * @vsi: Pointer to VSI
8166  * @cls_flower: Pointer to struct flow_cls_offload
8167  *
8168  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8169 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8170 				 struct flow_cls_offload *cls_flower)
8171 {
8172 	struct i40e_cloud_filter *filter = NULL;
8173 	struct i40e_pf *pf = vsi->back;
8174 	int err = 0;
8175 
8176 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8177 
8178 	if (!filter)
8179 		return -EINVAL;
8180 
8181 	hash_del(&filter->cloud_node);
8182 
8183 	if (filter->dst_port)
8184 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8185 	else
8186 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8187 
8188 	kfree(filter);
8189 	if (err) {
8190 		dev_err(&pf->pdev->dev,
8191 			"Failed to delete cloud filter, err %s\n",
8192 			i40e_stat_str(&pf->hw, err));
8193 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8194 	}
8195 
8196 	pf->num_cloud_filters--;
8197 	if (!pf->num_cloud_filters)
8198 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8199 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8200 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8201 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8202 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8203 		}
8204 	return 0;
8205 }
8206 
8207 /**
8208  * i40e_setup_tc_cls_flower - flower classifier offloads
8209  * @np: net device to configure
8210  * @cls_flower: offload data
8211  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8212 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8213 				    struct flow_cls_offload *cls_flower)
8214 {
8215 	struct i40e_vsi *vsi = np->vsi;
8216 
8217 	switch (cls_flower->command) {
8218 	case FLOW_CLS_REPLACE:
8219 		return i40e_configure_clsflower(vsi, cls_flower);
8220 	case FLOW_CLS_DESTROY:
8221 		return i40e_delete_clsflower(vsi, cls_flower);
8222 	case FLOW_CLS_STATS:
8223 		return -EOPNOTSUPP;
8224 	default:
8225 		return -EOPNOTSUPP;
8226 	}
8227 }
8228 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8229 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8230 				  void *cb_priv)
8231 {
8232 	struct i40e_netdev_priv *np = cb_priv;
8233 
8234 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8235 		return -EOPNOTSUPP;
8236 
8237 	switch (type) {
8238 	case TC_SETUP_CLSFLOWER:
8239 		return i40e_setup_tc_cls_flower(np, type_data);
8240 
8241 	default:
8242 		return -EOPNOTSUPP;
8243 	}
8244 }
8245 
8246 static LIST_HEAD(i40e_block_cb_list);
8247 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8248 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8249 			   void *type_data)
8250 {
8251 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8252 
8253 	switch (type) {
8254 	case TC_SETUP_QDISC_MQPRIO:
8255 		return i40e_setup_tc(netdev, type_data);
8256 	case TC_SETUP_BLOCK:
8257 		return flow_block_cb_setup_simple(type_data,
8258 						  &i40e_block_cb_list,
8259 						  i40e_setup_tc_block_cb,
8260 						  np, np, true);
8261 	default:
8262 		return -EOPNOTSUPP;
8263 	}
8264 }
8265 
8266 /**
8267  * i40e_open - Called when a network interface is made active
8268  * @netdev: network interface device structure
8269  *
8270  * The open entry point is called when a network interface is made
8271  * active by the system (IFF_UP).  At this point all resources needed
8272  * for transmit and receive operations are allocated, the interrupt
8273  * handler is registered with the OS, the netdev watchdog subtask is
8274  * enabled, and the stack is notified that the interface is ready.
8275  *
8276  * Returns 0 on success, negative value on failure
8277  **/
i40e_open(struct net_device * netdev)8278 int i40e_open(struct net_device *netdev)
8279 {
8280 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8281 	struct i40e_vsi *vsi = np->vsi;
8282 	struct i40e_pf *pf = vsi->back;
8283 	int err;
8284 
8285 	/* disallow open during test or if eeprom is broken */
8286 	if (test_bit(__I40E_TESTING, pf->state) ||
8287 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8288 		return -EBUSY;
8289 
8290 	netif_carrier_off(netdev);
8291 
8292 	if (i40e_force_link_state(pf, true))
8293 		return -EAGAIN;
8294 
8295 	err = i40e_vsi_open(vsi);
8296 	if (err)
8297 		return err;
8298 
8299 	/* configure global TSO hardware offload settings */
8300 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8301 						       TCP_FLAG_FIN) >> 16);
8302 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8303 						       TCP_FLAG_FIN |
8304 						       TCP_FLAG_CWR) >> 16);
8305 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8306 
8307 	udp_tunnel_get_rx_info(netdev);
8308 
8309 	return 0;
8310 }
8311 
8312 /**
8313  * i40e_vsi_open -
8314  * @vsi: the VSI to open
8315  *
8316  * Finish initialization of the VSI.
8317  *
8318  * Returns 0 on success, negative value on failure
8319  *
8320  * Note: expects to be called while under rtnl_lock()
8321  **/
i40e_vsi_open(struct i40e_vsi * vsi)8322 int i40e_vsi_open(struct i40e_vsi *vsi)
8323 {
8324 	struct i40e_pf *pf = vsi->back;
8325 	char int_name[I40E_INT_NAME_STR_LEN];
8326 	int err;
8327 
8328 	/* allocate descriptors */
8329 	err = i40e_vsi_setup_tx_resources(vsi);
8330 	if (err)
8331 		goto err_setup_tx;
8332 	err = i40e_vsi_setup_rx_resources(vsi);
8333 	if (err)
8334 		goto err_setup_rx;
8335 
8336 	err = i40e_vsi_configure(vsi);
8337 	if (err)
8338 		goto err_setup_rx;
8339 
8340 	if (vsi->netdev) {
8341 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8342 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8343 		err = i40e_vsi_request_irq(vsi, int_name);
8344 		if (err)
8345 			goto err_setup_rx;
8346 
8347 		/* Notify the stack of the actual queue counts. */
8348 		err = netif_set_real_num_tx_queues(vsi->netdev,
8349 						   vsi->num_queue_pairs);
8350 		if (err)
8351 			goto err_set_queues;
8352 
8353 		err = netif_set_real_num_rx_queues(vsi->netdev,
8354 						   vsi->num_queue_pairs);
8355 		if (err)
8356 			goto err_set_queues;
8357 
8358 	} else if (vsi->type == I40E_VSI_FDIR) {
8359 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8360 			 dev_driver_string(&pf->pdev->dev),
8361 			 dev_name(&pf->pdev->dev));
8362 		err = i40e_vsi_request_irq(vsi, int_name);
8363 		if (err)
8364 			goto err_setup_rx;
8365 
8366 	} else {
8367 		err = -EINVAL;
8368 		goto err_setup_rx;
8369 	}
8370 
8371 	err = i40e_up_complete(vsi);
8372 	if (err)
8373 		goto err_up_complete;
8374 
8375 	return 0;
8376 
8377 err_up_complete:
8378 	i40e_down(vsi);
8379 err_set_queues:
8380 	i40e_vsi_free_irq(vsi);
8381 err_setup_rx:
8382 	i40e_vsi_free_rx_resources(vsi);
8383 err_setup_tx:
8384 	i40e_vsi_free_tx_resources(vsi);
8385 	if (vsi == pf->vsi[pf->lan_vsi])
8386 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8387 
8388 	return err;
8389 }
8390 
8391 /**
8392  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8393  * @pf: Pointer to PF
8394  *
8395  * This function destroys the hlist where all the Flow Director
8396  * filters were saved.
8397  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)8398 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8399 {
8400 	struct i40e_fdir_filter *filter;
8401 	struct i40e_flex_pit *pit_entry, *tmp;
8402 	struct hlist_node *node2;
8403 
8404 	hlist_for_each_entry_safe(filter, node2,
8405 				  &pf->fdir_filter_list, fdir_node) {
8406 		hlist_del(&filter->fdir_node);
8407 		kfree(filter);
8408 	}
8409 
8410 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8411 		list_del(&pit_entry->list);
8412 		kfree(pit_entry);
8413 	}
8414 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8415 
8416 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8417 		list_del(&pit_entry->list);
8418 		kfree(pit_entry);
8419 	}
8420 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8421 
8422 	pf->fdir_pf_active_filters = 0;
8423 	pf->fd_tcp4_filter_cnt = 0;
8424 	pf->fd_udp4_filter_cnt = 0;
8425 	pf->fd_sctp4_filter_cnt = 0;
8426 	pf->fd_ip4_filter_cnt = 0;
8427 
8428 	/* Reprogram the default input set for TCP/IPv4 */
8429 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8430 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8431 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8432 
8433 	/* Reprogram the default input set for UDP/IPv4 */
8434 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8435 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8436 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8437 
8438 	/* Reprogram the default input set for SCTP/IPv4 */
8439 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8440 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8441 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8442 
8443 	/* Reprogram the default input set for Other/IPv4 */
8444 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8445 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8446 
8447 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8448 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8449 }
8450 
8451 /**
8452  * i40e_cloud_filter_exit - Cleans up the cloud filters
8453  * @pf: Pointer to PF
8454  *
8455  * This function destroys the hlist where all the cloud filters
8456  * were saved.
8457  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)8458 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8459 {
8460 	struct i40e_cloud_filter *cfilter;
8461 	struct hlist_node *node;
8462 
8463 	hlist_for_each_entry_safe(cfilter, node,
8464 				  &pf->cloud_filter_list, cloud_node) {
8465 		hlist_del(&cfilter->cloud_node);
8466 		kfree(cfilter);
8467 	}
8468 	pf->num_cloud_filters = 0;
8469 
8470 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8471 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8472 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8473 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8474 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8475 	}
8476 }
8477 
8478 /**
8479  * i40e_close - Disables a network interface
8480  * @netdev: network interface device structure
8481  *
8482  * The close entry point is called when an interface is de-activated
8483  * by the OS.  The hardware is still under the driver's control, but
8484  * this netdev interface is disabled.
8485  *
8486  * Returns 0, this is not allowed to fail
8487  **/
i40e_close(struct net_device * netdev)8488 int i40e_close(struct net_device *netdev)
8489 {
8490 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8491 	struct i40e_vsi *vsi = np->vsi;
8492 
8493 	i40e_vsi_close(vsi);
8494 
8495 	return 0;
8496 }
8497 
8498 /**
8499  * i40e_do_reset - Start a PF or Core Reset sequence
8500  * @pf: board private structure
8501  * @reset_flags: which reset is requested
8502  * @lock_acquired: indicates whether or not the lock has been acquired
8503  * before this function was called.
8504  *
8505  * The essential difference in resets is that the PF Reset
8506  * doesn't clear the packet buffers, doesn't reset the PE
8507  * firmware, and doesn't bother the other PFs on the chip.
8508  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)8509 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8510 {
8511 	u32 val;
8512 
8513 	/* do the biggest reset indicated */
8514 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8515 
8516 		/* Request a Global Reset
8517 		 *
8518 		 * This will start the chip's countdown to the actual full
8519 		 * chip reset event, and a warning interrupt to be sent
8520 		 * to all PFs, including the requestor.  Our handler
8521 		 * for the warning interrupt will deal with the shutdown
8522 		 * and recovery of the switch setup.
8523 		 */
8524 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8525 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8526 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8527 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8528 
8529 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
8530 
8531 		/* Request a Core Reset
8532 		 *
8533 		 * Same as Global Reset, except does *not* include the MAC/PHY
8534 		 */
8535 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
8536 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8537 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
8538 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8539 		i40e_flush(&pf->hw);
8540 
8541 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
8542 
8543 		/* Request a PF Reset
8544 		 *
8545 		 * Resets only the PF-specific registers
8546 		 *
8547 		 * This goes directly to the tear-down and rebuild of
8548 		 * the switch, since we need to do all the recovery as
8549 		 * for the Core Reset.
8550 		 */
8551 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
8552 		i40e_handle_reset_warning(pf, lock_acquired);
8553 
8554 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
8555 		/* Request a PF Reset
8556 		 *
8557 		 * Resets PF and reinitializes PFs VSI.
8558 		 */
8559 		i40e_prep_for_reset(pf, lock_acquired);
8560 		i40e_reset_and_rebuild(pf, true, lock_acquired);
8561 		dev_info(&pf->pdev->dev,
8562 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
8563 			 "FW LLDP is disabled\n" :
8564 			 "FW LLDP is enabled\n");
8565 
8566 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
8567 		int v;
8568 
8569 		/* Find the VSI(s) that requested a re-init */
8570 		dev_info(&pf->pdev->dev,
8571 			 "VSI reinit requested\n");
8572 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8573 			struct i40e_vsi *vsi = pf->vsi[v];
8574 
8575 			if (vsi != NULL &&
8576 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
8577 					       vsi->state))
8578 				i40e_vsi_reinit_locked(pf->vsi[v]);
8579 		}
8580 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
8581 		int v;
8582 
8583 		/* Find the VSI(s) that needs to be brought down */
8584 		dev_info(&pf->pdev->dev, "VSI down requested\n");
8585 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8586 			struct i40e_vsi *vsi = pf->vsi[v];
8587 
8588 			if (vsi != NULL &&
8589 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
8590 					       vsi->state)) {
8591 				set_bit(__I40E_VSI_DOWN, vsi->state);
8592 				i40e_down(vsi);
8593 			}
8594 		}
8595 	} else {
8596 		dev_info(&pf->pdev->dev,
8597 			 "bad reset request 0x%08x\n", reset_flags);
8598 	}
8599 }
8600 
8601 #ifdef CONFIG_I40E_DCB
8602 /**
8603  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8604  * @pf: board private structure
8605  * @old_cfg: current DCB config
8606  * @new_cfg: new DCB config
8607  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)8608 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
8609 			    struct i40e_dcbx_config *old_cfg,
8610 			    struct i40e_dcbx_config *new_cfg)
8611 {
8612 	bool need_reconfig = false;
8613 
8614 	/* Check if ETS configuration has changed */
8615 	if (memcmp(&new_cfg->etscfg,
8616 		   &old_cfg->etscfg,
8617 		   sizeof(new_cfg->etscfg))) {
8618 		/* If Priority Table has changed reconfig is needed */
8619 		if (memcmp(&new_cfg->etscfg.prioritytable,
8620 			   &old_cfg->etscfg.prioritytable,
8621 			   sizeof(new_cfg->etscfg.prioritytable))) {
8622 			need_reconfig = true;
8623 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8624 		}
8625 
8626 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8627 			   &old_cfg->etscfg.tcbwtable,
8628 			   sizeof(new_cfg->etscfg.tcbwtable)))
8629 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8630 
8631 		if (memcmp(&new_cfg->etscfg.tsatable,
8632 			   &old_cfg->etscfg.tsatable,
8633 			   sizeof(new_cfg->etscfg.tsatable)))
8634 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
8635 	}
8636 
8637 	/* Check if PFC configuration has changed */
8638 	if (memcmp(&new_cfg->pfc,
8639 		   &old_cfg->pfc,
8640 		   sizeof(new_cfg->pfc))) {
8641 		need_reconfig = true;
8642 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
8643 	}
8644 
8645 	/* Check if APP Table has changed */
8646 	if (memcmp(&new_cfg->app,
8647 		   &old_cfg->app,
8648 		   sizeof(new_cfg->app))) {
8649 		need_reconfig = true;
8650 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
8651 	}
8652 
8653 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
8654 	return need_reconfig;
8655 }
8656 
8657 /**
8658  * i40e_handle_lldp_event - Handle LLDP Change MIB event
8659  * @pf: board private structure
8660  * @e: event info posted on ARQ
8661  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)8662 static int i40e_handle_lldp_event(struct i40e_pf *pf,
8663 				  struct i40e_arq_event_info *e)
8664 {
8665 	struct i40e_aqc_lldp_get_mib *mib =
8666 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
8667 	struct i40e_hw *hw = &pf->hw;
8668 	struct i40e_dcbx_config tmp_dcbx_cfg;
8669 	bool need_reconfig = false;
8670 	int ret = 0;
8671 	u8 type;
8672 
8673 	/* Not DCB capable or capability disabled */
8674 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
8675 		return ret;
8676 
8677 	/* Ignore if event is not for Nearest Bridge */
8678 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
8679 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
8680 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
8681 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
8682 		return ret;
8683 
8684 	/* Check MIB Type and return if event for Remote MIB update */
8685 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
8686 	dev_dbg(&pf->pdev->dev,
8687 		"LLDP event mib type %s\n", type ? "remote" : "local");
8688 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8689 		/* Update the remote cached instance and return */
8690 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8691 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8692 				&hw->remote_dcbx_config);
8693 		goto exit;
8694 	}
8695 
8696 	/* Store the old configuration */
8697 	tmp_dcbx_cfg = hw->local_dcbx_config;
8698 
8699 	/* Reset the old DCBx configuration data */
8700 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8701 	/* Get updated DCBX data from firmware */
8702 	ret = i40e_get_dcb_config(&pf->hw);
8703 	if (ret) {
8704 		dev_info(&pf->pdev->dev,
8705 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8706 			 i40e_stat_str(&pf->hw, ret),
8707 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8708 		goto exit;
8709 	}
8710 
8711 	/* No change detected in DCBX configs */
8712 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8713 		    sizeof(tmp_dcbx_cfg))) {
8714 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8715 		goto exit;
8716 	}
8717 
8718 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8719 					       &hw->local_dcbx_config);
8720 
8721 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8722 
8723 	if (!need_reconfig)
8724 		goto exit;
8725 
8726 	/* Enable DCB tagging only when more than one TC */
8727 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8728 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8729 	else
8730 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8731 
8732 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8733 	/* Reconfiguration needed quiesce all VSIs */
8734 	i40e_pf_quiesce_all_vsi(pf);
8735 
8736 	/* Changes in configuration update VEB/VSI */
8737 	i40e_dcb_reconfigure(pf);
8738 
8739 	ret = i40e_resume_port_tx(pf);
8740 
8741 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8742 	/* In case of error no point in resuming VSIs */
8743 	if (ret)
8744 		goto exit;
8745 
8746 	/* Wait for the PF's queues to be disabled */
8747 	ret = i40e_pf_wait_queues_disabled(pf);
8748 	if (ret) {
8749 		/* Schedule PF reset to recover */
8750 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8751 		i40e_service_event_schedule(pf);
8752 	} else {
8753 		i40e_pf_unquiesce_all_vsi(pf);
8754 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8755 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8756 	}
8757 
8758 exit:
8759 	return ret;
8760 }
8761 #endif /* CONFIG_I40E_DCB */
8762 
8763 /**
8764  * i40e_do_reset_safe - Protected reset path for userland calls.
8765  * @pf: board private structure
8766  * @reset_flags: which reset is requested
8767  *
8768  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)8769 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8770 {
8771 	rtnl_lock();
8772 	i40e_do_reset(pf, reset_flags, true);
8773 	rtnl_unlock();
8774 }
8775 
8776 /**
8777  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8778  * @pf: board private structure
8779  * @e: event info posted on ARQ
8780  *
8781  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8782  * and VF queues
8783  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)8784 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8785 					   struct i40e_arq_event_info *e)
8786 {
8787 	struct i40e_aqc_lan_overflow *data =
8788 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8789 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8790 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8791 	struct i40e_hw *hw = &pf->hw;
8792 	struct i40e_vf *vf;
8793 	u16 vf_id;
8794 
8795 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8796 		queue, qtx_ctl);
8797 
8798 	/* Queue belongs to VF, find the VF and issue VF reset */
8799 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8800 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8801 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8802 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8803 		vf_id -= hw->func_caps.vf_base_id;
8804 		vf = &pf->vf[vf_id];
8805 		i40e_vc_notify_vf_reset(vf);
8806 		/* Allow VF to process pending reset notification */
8807 		msleep(20);
8808 		i40e_reset_vf(vf, false);
8809 	}
8810 }
8811 
8812 /**
8813  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8814  * @pf: board private structure
8815  **/
i40e_get_cur_guaranteed_fd_count(struct i40e_pf * pf)8816 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8817 {
8818 	u32 val, fcnt_prog;
8819 
8820 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8821 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8822 	return fcnt_prog;
8823 }
8824 
8825 /**
8826  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8827  * @pf: board private structure
8828  **/
i40e_get_current_fd_count(struct i40e_pf * pf)8829 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8830 {
8831 	u32 val, fcnt_prog;
8832 
8833 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8834 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8835 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8836 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8837 	return fcnt_prog;
8838 }
8839 
8840 /**
8841  * i40e_get_global_fd_count - Get total FD filters programmed on device
8842  * @pf: board private structure
8843  **/
i40e_get_global_fd_count(struct i40e_pf * pf)8844 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8845 {
8846 	u32 val, fcnt_prog;
8847 
8848 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8849 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8850 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8851 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8852 	return fcnt_prog;
8853 }
8854 
8855 /**
8856  * i40e_reenable_fdir_sb - Restore FDir SB capability
8857  * @pf: board private structure
8858  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)8859 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8860 {
8861 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8862 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8863 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8864 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8865 }
8866 
8867 /**
8868  * i40e_reenable_fdir_atr - Restore FDir ATR capability
8869  * @pf: board private structure
8870  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)8871 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8872 {
8873 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8874 		/* ATR uses the same filtering logic as SB rules. It only
8875 		 * functions properly if the input set mask is at the default
8876 		 * settings. It is safe to restore the default input set
8877 		 * because there are no active TCPv4 filter rules.
8878 		 */
8879 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8880 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8881 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8882 
8883 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8884 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8885 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8886 	}
8887 }
8888 
8889 /**
8890  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8891  * @pf: board private structure
8892  * @filter: FDir filter to remove
8893  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)8894 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8895 				       struct i40e_fdir_filter *filter)
8896 {
8897 	/* Update counters */
8898 	pf->fdir_pf_active_filters--;
8899 	pf->fd_inv = 0;
8900 
8901 	switch (filter->flow_type) {
8902 	case TCP_V4_FLOW:
8903 		pf->fd_tcp4_filter_cnt--;
8904 		break;
8905 	case UDP_V4_FLOW:
8906 		pf->fd_udp4_filter_cnt--;
8907 		break;
8908 	case SCTP_V4_FLOW:
8909 		pf->fd_sctp4_filter_cnt--;
8910 		break;
8911 	case IP_USER_FLOW:
8912 		switch (filter->ip4_proto) {
8913 		case IPPROTO_TCP:
8914 			pf->fd_tcp4_filter_cnt--;
8915 			break;
8916 		case IPPROTO_UDP:
8917 			pf->fd_udp4_filter_cnt--;
8918 			break;
8919 		case IPPROTO_SCTP:
8920 			pf->fd_sctp4_filter_cnt--;
8921 			break;
8922 		case IPPROTO_IP:
8923 			pf->fd_ip4_filter_cnt--;
8924 			break;
8925 		}
8926 		break;
8927 	}
8928 
8929 	/* Remove the filter from the list and free memory */
8930 	hlist_del(&filter->fdir_node);
8931 	kfree(filter);
8932 }
8933 
8934 /**
8935  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8936  * @pf: board private structure
8937  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)8938 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8939 {
8940 	struct i40e_fdir_filter *filter;
8941 	u32 fcnt_prog, fcnt_avail;
8942 	struct hlist_node *node;
8943 
8944 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8945 		return;
8946 
8947 	/* Check if we have enough room to re-enable FDir SB capability. */
8948 	fcnt_prog = i40e_get_global_fd_count(pf);
8949 	fcnt_avail = pf->fdir_pf_filter_count;
8950 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8951 	    (pf->fd_add_err == 0) ||
8952 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
8953 		i40e_reenable_fdir_sb(pf);
8954 
8955 	/* We should wait for even more space before re-enabling ATR.
8956 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
8957 	 * rules active.
8958 	 */
8959 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8960 	    (pf->fd_tcp4_filter_cnt == 0))
8961 		i40e_reenable_fdir_atr(pf);
8962 
8963 	/* if hw had a problem adding a filter, delete it */
8964 	if (pf->fd_inv > 0) {
8965 		hlist_for_each_entry_safe(filter, node,
8966 					  &pf->fdir_filter_list, fdir_node)
8967 			if (filter->fd_id == pf->fd_inv)
8968 				i40e_delete_invalid_filter(pf, filter);
8969 	}
8970 }
8971 
8972 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8973 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8974 /**
8975  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8976  * @pf: board private structure
8977  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)8978 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8979 {
8980 	unsigned long min_flush_time;
8981 	int flush_wait_retry = 50;
8982 	bool disable_atr = false;
8983 	int fd_room;
8984 	int reg;
8985 
8986 	if (!time_after(jiffies, pf->fd_flush_timestamp +
8987 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8988 		return;
8989 
8990 	/* If the flush is happening too quick and we have mostly SB rules we
8991 	 * should not re-enable ATR for some time.
8992 	 */
8993 	min_flush_time = pf->fd_flush_timestamp +
8994 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8995 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8996 
8997 	if (!(time_after(jiffies, min_flush_time)) &&
8998 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8999 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9000 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9001 		disable_atr = true;
9002 	}
9003 
9004 	pf->fd_flush_timestamp = jiffies;
9005 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9006 	/* flush all filters */
9007 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9008 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9009 	i40e_flush(&pf->hw);
9010 	pf->fd_flush_cnt++;
9011 	pf->fd_add_err = 0;
9012 	do {
9013 		/* Check FD flush status every 5-6msec */
9014 		usleep_range(5000, 6000);
9015 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9016 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9017 			break;
9018 	} while (flush_wait_retry--);
9019 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9020 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9021 	} else {
9022 		/* replay sideband filters */
9023 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9024 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9025 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9026 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9027 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9028 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9029 	}
9030 }
9031 
9032 /**
9033  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
9034  * @pf: board private structure
9035  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9036 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9037 {
9038 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9039 }
9040 
9041 /**
9042  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9043  * @pf: board private structure
9044  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9045 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9046 {
9047 
9048 	/* if interface is down do nothing */
9049 	if (test_bit(__I40E_DOWN, pf->state))
9050 		return;
9051 
9052 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9053 		i40e_fdir_flush_and_replay(pf);
9054 
9055 	i40e_fdir_check_and_reenable(pf);
9056 
9057 }
9058 
9059 /**
9060  * i40e_vsi_link_event - notify VSI of a link event
9061  * @vsi: vsi to be notified
9062  * @link_up: link up or down
9063  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9064 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9065 {
9066 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9067 		return;
9068 
9069 	switch (vsi->type) {
9070 	case I40E_VSI_MAIN:
9071 		if (!vsi->netdev || !vsi->netdev_registered)
9072 			break;
9073 
9074 		if (link_up) {
9075 			netif_carrier_on(vsi->netdev);
9076 			netif_tx_wake_all_queues(vsi->netdev);
9077 		} else {
9078 			netif_carrier_off(vsi->netdev);
9079 			netif_tx_stop_all_queues(vsi->netdev);
9080 		}
9081 		break;
9082 
9083 	case I40E_VSI_SRIOV:
9084 	case I40E_VSI_VMDQ2:
9085 	case I40E_VSI_CTRL:
9086 	case I40E_VSI_IWARP:
9087 	case I40E_VSI_MIRROR:
9088 	default:
9089 		/* there is no notification for other VSIs */
9090 		break;
9091 	}
9092 }
9093 
9094 /**
9095  * i40e_veb_link_event - notify elements on the veb of a link event
9096  * @veb: veb to be notified
9097  * @link_up: link up or down
9098  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9099 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9100 {
9101 	struct i40e_pf *pf;
9102 	int i;
9103 
9104 	if (!veb || !veb->pf)
9105 		return;
9106 	pf = veb->pf;
9107 
9108 	/* depth first... */
9109 	for (i = 0; i < I40E_MAX_VEB; i++)
9110 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9111 			i40e_veb_link_event(pf->veb[i], link_up);
9112 
9113 	/* ... now the local VSIs */
9114 	for (i = 0; i < pf->num_alloc_vsi; i++)
9115 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9116 			i40e_vsi_link_event(pf->vsi[i], link_up);
9117 }
9118 
9119 /**
9120  * i40e_link_event - Update netif_carrier status
9121  * @pf: board private structure
9122  **/
i40e_link_event(struct i40e_pf * pf)9123 static void i40e_link_event(struct i40e_pf *pf)
9124 {
9125 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9126 	u8 new_link_speed, old_link_speed;
9127 	i40e_status status;
9128 	bool new_link, old_link;
9129 
9130 	/* set this to force the get_link_status call to refresh state */
9131 	pf->hw.phy.get_link_info = true;
9132 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9133 	status = i40e_get_link_status(&pf->hw, &new_link);
9134 
9135 	/* On success, disable temp link polling */
9136 	if (status == I40E_SUCCESS) {
9137 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9138 	} else {
9139 		/* Enable link polling temporarily until i40e_get_link_status
9140 		 * returns I40E_SUCCESS
9141 		 */
9142 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9143 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9144 			status);
9145 		return;
9146 	}
9147 
9148 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9149 	new_link_speed = pf->hw.phy.link_info.link_speed;
9150 
9151 	if (new_link == old_link &&
9152 	    new_link_speed == old_link_speed &&
9153 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9154 	     new_link == netif_carrier_ok(vsi->netdev)))
9155 		return;
9156 
9157 	i40e_print_link_message(vsi, new_link);
9158 
9159 	/* Notify the base of the switch tree connected to
9160 	 * the link.  Floating VEBs are not notified.
9161 	 */
9162 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9163 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9164 	else
9165 		i40e_vsi_link_event(vsi, new_link);
9166 
9167 	if (pf->vf)
9168 		i40e_vc_notify_link_state(pf);
9169 
9170 	if (pf->flags & I40E_FLAG_PTP)
9171 		i40e_ptp_set_increment(pf);
9172 }
9173 
9174 /**
9175  * i40e_watchdog_subtask - periodic checks not using event driven response
9176  * @pf: board private structure
9177  **/
i40e_watchdog_subtask(struct i40e_pf * pf)9178 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9179 {
9180 	int i;
9181 
9182 	/* if interface is down do nothing */
9183 	if (test_bit(__I40E_DOWN, pf->state) ||
9184 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9185 		return;
9186 
9187 	/* make sure we don't do these things too often */
9188 	if (time_before(jiffies, (pf->service_timer_previous +
9189 				  pf->service_timer_period)))
9190 		return;
9191 	pf->service_timer_previous = jiffies;
9192 
9193 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9194 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9195 		i40e_link_event(pf);
9196 
9197 	/* Update the stats for active netdevs so the network stack
9198 	 * can look at updated numbers whenever it cares to
9199 	 */
9200 	for (i = 0; i < pf->num_alloc_vsi; i++)
9201 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9202 			i40e_update_stats(pf->vsi[i]);
9203 
9204 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9205 		/* Update the stats for the active switching components */
9206 		for (i = 0; i < I40E_MAX_VEB; i++)
9207 			if (pf->veb[i])
9208 				i40e_update_veb_stats(pf->veb[i]);
9209 	}
9210 
9211 	i40e_ptp_rx_hang(pf);
9212 	i40e_ptp_tx_hang(pf);
9213 }
9214 
9215 /**
9216  * i40e_reset_subtask - Set up for resetting the device and driver
9217  * @pf: board private structure
9218  **/
i40e_reset_subtask(struct i40e_pf * pf)9219 static void i40e_reset_subtask(struct i40e_pf *pf)
9220 {
9221 	u32 reset_flags = 0;
9222 
9223 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9224 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9225 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9226 	}
9227 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9228 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9229 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9230 	}
9231 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9232 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9233 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9234 	}
9235 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9236 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9237 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9238 	}
9239 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9240 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9241 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9242 	}
9243 
9244 	/* If there's a recovery already waiting, it takes
9245 	 * precedence before starting a new reset sequence.
9246 	 */
9247 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9248 		i40e_prep_for_reset(pf, false);
9249 		i40e_reset(pf);
9250 		i40e_rebuild(pf, false, false);
9251 	}
9252 
9253 	/* If we're already down or resetting, just bail */
9254 	if (reset_flags &&
9255 	    !test_bit(__I40E_DOWN, pf->state) &&
9256 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9257 		i40e_do_reset(pf, reset_flags, false);
9258 	}
9259 }
9260 
9261 /**
9262  * i40e_handle_link_event - Handle link event
9263  * @pf: board private structure
9264  * @e: event info posted on ARQ
9265  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9266 static void i40e_handle_link_event(struct i40e_pf *pf,
9267 				   struct i40e_arq_event_info *e)
9268 {
9269 	struct i40e_aqc_get_link_status *status =
9270 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9271 
9272 	/* Do a new status request to re-enable LSE reporting
9273 	 * and load new status information into the hw struct
9274 	 * This completely ignores any state information
9275 	 * in the ARQ event info, instead choosing to always
9276 	 * issue the AQ update link status command.
9277 	 */
9278 	i40e_link_event(pf);
9279 
9280 	/* Check if module meets thermal requirements */
9281 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9282 		dev_err(&pf->pdev->dev,
9283 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9284 		dev_err(&pf->pdev->dev,
9285 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9286 	} else {
9287 		/* check for unqualified module, if link is down, suppress
9288 		 * the message if link was forced to be down.
9289 		 */
9290 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9291 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9292 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9293 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9294 			dev_err(&pf->pdev->dev,
9295 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9296 			dev_err(&pf->pdev->dev,
9297 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9298 		}
9299 	}
9300 }
9301 
9302 /**
9303  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9304  * @pf: board private structure
9305  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)9306 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9307 {
9308 	struct i40e_arq_event_info event;
9309 	struct i40e_hw *hw = &pf->hw;
9310 	u16 pending, i = 0;
9311 	i40e_status ret;
9312 	u16 opcode;
9313 	u32 oldval;
9314 	u32 val;
9315 
9316 	/* Do not run clean AQ when PF reset fails */
9317 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9318 		return;
9319 
9320 	/* check for error indications */
9321 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9322 	oldval = val;
9323 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9324 		if (hw->debug_mask & I40E_DEBUG_AQ)
9325 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9326 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9327 	}
9328 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9329 		if (hw->debug_mask & I40E_DEBUG_AQ)
9330 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9331 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9332 		pf->arq_overflows++;
9333 	}
9334 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9335 		if (hw->debug_mask & I40E_DEBUG_AQ)
9336 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9337 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9338 	}
9339 	if (oldval != val)
9340 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9341 
9342 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9343 	oldval = val;
9344 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9345 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9346 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9347 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9348 	}
9349 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9350 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9351 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9352 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9353 	}
9354 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9355 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9356 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9357 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9358 	}
9359 	if (oldval != val)
9360 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9361 
9362 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9363 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9364 	if (!event.msg_buf)
9365 		return;
9366 
9367 	do {
9368 		ret = i40e_clean_arq_element(hw, &event, &pending);
9369 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9370 			break;
9371 		else if (ret) {
9372 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9373 			break;
9374 		}
9375 
9376 		opcode = le16_to_cpu(event.desc.opcode);
9377 		switch (opcode) {
9378 
9379 		case i40e_aqc_opc_get_link_status:
9380 			i40e_handle_link_event(pf, &event);
9381 			break;
9382 		case i40e_aqc_opc_send_msg_to_pf:
9383 			ret = i40e_vc_process_vf_msg(pf,
9384 					le16_to_cpu(event.desc.retval),
9385 					le32_to_cpu(event.desc.cookie_high),
9386 					le32_to_cpu(event.desc.cookie_low),
9387 					event.msg_buf,
9388 					event.msg_len);
9389 			break;
9390 		case i40e_aqc_opc_lldp_update_mib:
9391 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9392 #ifdef CONFIG_I40E_DCB
9393 			rtnl_lock();
9394 			ret = i40e_handle_lldp_event(pf, &event);
9395 			rtnl_unlock();
9396 #endif /* CONFIG_I40E_DCB */
9397 			break;
9398 		case i40e_aqc_opc_event_lan_overflow:
9399 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9400 			i40e_handle_lan_overflow_event(pf, &event);
9401 			break;
9402 		case i40e_aqc_opc_send_msg_to_peer:
9403 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9404 			break;
9405 		case i40e_aqc_opc_nvm_erase:
9406 		case i40e_aqc_opc_nvm_update:
9407 		case i40e_aqc_opc_oem_post_update:
9408 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9409 				   "ARQ NVM operation 0x%04x completed\n",
9410 				   opcode);
9411 			break;
9412 		default:
9413 			dev_info(&pf->pdev->dev,
9414 				 "ARQ: Unknown event 0x%04x ignored\n",
9415 				 opcode);
9416 			break;
9417 		}
9418 	} while (i++ < pf->adminq_work_limit);
9419 
9420 	if (i < pf->adminq_work_limit)
9421 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9422 
9423 	/* re-enable Admin queue interrupt cause */
9424 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9425 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9426 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9427 	i40e_flush(hw);
9428 
9429 	kfree(event.msg_buf);
9430 }
9431 
9432 /**
9433  * i40e_verify_eeprom - make sure eeprom is good to use
9434  * @pf: board private structure
9435  **/
i40e_verify_eeprom(struct i40e_pf * pf)9436 static void i40e_verify_eeprom(struct i40e_pf *pf)
9437 {
9438 	int err;
9439 
9440 	err = i40e_diag_eeprom_test(&pf->hw);
9441 	if (err) {
9442 		/* retry in case of garbage read */
9443 		err = i40e_diag_eeprom_test(&pf->hw);
9444 		if (err) {
9445 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9446 				 err);
9447 			set_bit(__I40E_BAD_EEPROM, pf->state);
9448 		}
9449 	}
9450 
9451 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9452 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9453 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9454 	}
9455 }
9456 
9457 /**
9458  * i40e_enable_pf_switch_lb
9459  * @pf: pointer to the PF structure
9460  *
9461  * enable switch loop back or die - no point in a return value
9462  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)9463 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
9464 {
9465 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9466 	struct i40e_vsi_context ctxt;
9467 	int ret;
9468 
9469 	ctxt.seid = pf->main_vsi_seid;
9470 	ctxt.pf_num = pf->hw.pf_id;
9471 	ctxt.vf_num = 0;
9472 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9473 	if (ret) {
9474 		dev_info(&pf->pdev->dev,
9475 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9476 			 i40e_stat_str(&pf->hw, ret),
9477 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9478 		return;
9479 	}
9480 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9481 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9482 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9483 
9484 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9485 	if (ret) {
9486 		dev_info(&pf->pdev->dev,
9487 			 "update vsi switch failed, err %s aq_err %s\n",
9488 			 i40e_stat_str(&pf->hw, ret),
9489 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9490 	}
9491 }
9492 
9493 /**
9494  * i40e_disable_pf_switch_lb
9495  * @pf: pointer to the PF structure
9496  *
9497  * disable switch loop back or die - no point in a return value
9498  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)9499 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
9500 {
9501 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9502 	struct i40e_vsi_context ctxt;
9503 	int ret;
9504 
9505 	ctxt.seid = pf->main_vsi_seid;
9506 	ctxt.pf_num = pf->hw.pf_id;
9507 	ctxt.vf_num = 0;
9508 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9509 	if (ret) {
9510 		dev_info(&pf->pdev->dev,
9511 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9512 			 i40e_stat_str(&pf->hw, ret),
9513 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9514 		return;
9515 	}
9516 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9517 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9518 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9519 
9520 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9521 	if (ret) {
9522 		dev_info(&pf->pdev->dev,
9523 			 "update vsi switch failed, err %s aq_err %s\n",
9524 			 i40e_stat_str(&pf->hw, ret),
9525 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9526 	}
9527 }
9528 
9529 /**
9530  * i40e_config_bridge_mode - Configure the HW bridge mode
9531  * @veb: pointer to the bridge instance
9532  *
9533  * Configure the loop back mode for the LAN VSI that is downlink to the
9534  * specified HW bridge instance. It is expected this function is called
9535  * when a new HW bridge is instantiated.
9536  **/
i40e_config_bridge_mode(struct i40e_veb * veb)9537 static void i40e_config_bridge_mode(struct i40e_veb *veb)
9538 {
9539 	struct i40e_pf *pf = veb->pf;
9540 
9541 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
9542 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
9543 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
9544 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
9545 		i40e_disable_pf_switch_lb(pf);
9546 	else
9547 		i40e_enable_pf_switch_lb(pf);
9548 }
9549 
9550 /**
9551  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
9552  * @veb: pointer to the VEB instance
9553  *
9554  * This is a recursive function that first builds the attached VSIs then
9555  * recurses in to build the next layer of VEB.  We track the connections
9556  * through our own index numbers because the seid's from the HW could
9557  * change across the reset.
9558  **/
i40e_reconstitute_veb(struct i40e_veb * veb)9559 static int i40e_reconstitute_veb(struct i40e_veb *veb)
9560 {
9561 	struct i40e_vsi *ctl_vsi = NULL;
9562 	struct i40e_pf *pf = veb->pf;
9563 	int v, veb_idx;
9564 	int ret;
9565 
9566 	/* build VSI that owns this VEB, temporarily attached to base VEB */
9567 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
9568 		if (pf->vsi[v] &&
9569 		    pf->vsi[v]->veb_idx == veb->idx &&
9570 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
9571 			ctl_vsi = pf->vsi[v];
9572 			break;
9573 		}
9574 	}
9575 	if (!ctl_vsi) {
9576 		dev_info(&pf->pdev->dev,
9577 			 "missing owner VSI for veb_idx %d\n", veb->idx);
9578 		ret = -ENOENT;
9579 		goto end_reconstitute;
9580 	}
9581 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
9582 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9583 	ret = i40e_add_vsi(ctl_vsi);
9584 	if (ret) {
9585 		dev_info(&pf->pdev->dev,
9586 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
9587 			 veb->idx, ret);
9588 		goto end_reconstitute;
9589 	}
9590 	i40e_vsi_reset_stats(ctl_vsi);
9591 
9592 	/* create the VEB in the switch and move the VSI onto the VEB */
9593 	ret = i40e_add_veb(veb, ctl_vsi);
9594 	if (ret)
9595 		goto end_reconstitute;
9596 
9597 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
9598 		veb->bridge_mode = BRIDGE_MODE_VEB;
9599 	else
9600 		veb->bridge_mode = BRIDGE_MODE_VEPA;
9601 	i40e_config_bridge_mode(veb);
9602 
9603 	/* create the remaining VSIs attached to this VEB */
9604 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9605 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
9606 			continue;
9607 
9608 		if (pf->vsi[v]->veb_idx == veb->idx) {
9609 			struct i40e_vsi *vsi = pf->vsi[v];
9610 
9611 			vsi->uplink_seid = veb->seid;
9612 			ret = i40e_add_vsi(vsi);
9613 			if (ret) {
9614 				dev_info(&pf->pdev->dev,
9615 					 "rebuild of vsi_idx %d failed: %d\n",
9616 					 v, ret);
9617 				goto end_reconstitute;
9618 			}
9619 			i40e_vsi_reset_stats(vsi);
9620 		}
9621 	}
9622 
9623 	/* create any VEBs attached to this VEB - RECURSION */
9624 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9625 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
9626 			pf->veb[veb_idx]->uplink_seid = veb->seid;
9627 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
9628 			if (ret)
9629 				break;
9630 		}
9631 	}
9632 
9633 end_reconstitute:
9634 	return ret;
9635 }
9636 
9637 /**
9638  * i40e_get_capabilities - get info about the HW
9639  * @pf: the PF struct
9640  * @list_type: AQ capability to be queried
9641  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)9642 static int i40e_get_capabilities(struct i40e_pf *pf,
9643 				 enum i40e_admin_queue_opc list_type)
9644 {
9645 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
9646 	u16 data_size;
9647 	int buf_len;
9648 	int err;
9649 
9650 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
9651 	do {
9652 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
9653 		if (!cap_buf)
9654 			return -ENOMEM;
9655 
9656 		/* this loads the data into the hw struct for us */
9657 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
9658 						    &data_size, list_type,
9659 						    NULL);
9660 		/* data loaded, buffer no longer needed */
9661 		kfree(cap_buf);
9662 
9663 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
9664 			/* retry with a larger buffer */
9665 			buf_len = data_size;
9666 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
9667 			dev_info(&pf->pdev->dev,
9668 				 "capability discovery failed, err %s aq_err %s\n",
9669 				 i40e_stat_str(&pf->hw, err),
9670 				 i40e_aq_str(&pf->hw,
9671 					     pf->hw.aq.asq_last_status));
9672 			return -ENODEV;
9673 		}
9674 	} while (err);
9675 
9676 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9677 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
9678 			dev_info(&pf->pdev->dev,
9679 				 "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",
9680 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9681 				 pf->hw.func_caps.num_msix_vectors,
9682 				 pf->hw.func_caps.num_msix_vectors_vf,
9683 				 pf->hw.func_caps.fd_filters_guaranteed,
9684 				 pf->hw.func_caps.fd_filters_best_effort,
9685 				 pf->hw.func_caps.num_tx_qp,
9686 				 pf->hw.func_caps.num_vsis);
9687 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9688 			dev_info(&pf->pdev->dev,
9689 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9690 				 pf->hw.dev_caps.switch_mode,
9691 				 pf->hw.dev_caps.valid_functions);
9692 			dev_info(&pf->pdev->dev,
9693 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9694 				 pf->hw.dev_caps.sr_iov_1_1,
9695 				 pf->hw.dev_caps.num_vfs);
9696 			dev_info(&pf->pdev->dev,
9697 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9698 				 pf->hw.dev_caps.num_vsis,
9699 				 pf->hw.dev_caps.num_rx_qp,
9700 				 pf->hw.dev_caps.num_tx_qp);
9701 		}
9702 	}
9703 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9704 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9705 		       + pf->hw.func_caps.num_vfs)
9706 		if (pf->hw.revision_id == 0 &&
9707 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9708 			dev_info(&pf->pdev->dev,
9709 				 "got num_vsis %d, setting num_vsis to %d\n",
9710 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9711 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9712 		}
9713 	}
9714 	return 0;
9715 }
9716 
9717 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9718 
9719 /**
9720  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9721  * @pf: board private structure
9722  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)9723 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9724 {
9725 	struct i40e_vsi *vsi;
9726 
9727 	/* quick workaround for an NVM issue that leaves a critical register
9728 	 * uninitialized
9729 	 */
9730 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9731 		static const u32 hkey[] = {
9732 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9733 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9734 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9735 			0x95b3a76d};
9736 		int i;
9737 
9738 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9739 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9740 	}
9741 
9742 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9743 		return;
9744 
9745 	/* find existing VSI and see if it needs configuring */
9746 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9747 
9748 	/* create a new VSI if none exists */
9749 	if (!vsi) {
9750 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9751 				     pf->vsi[pf->lan_vsi]->seid, 0);
9752 		if (!vsi) {
9753 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9754 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9755 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9756 			return;
9757 		}
9758 	}
9759 
9760 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9761 }
9762 
9763 /**
9764  * i40e_fdir_teardown - release the Flow Director resources
9765  * @pf: board private structure
9766  **/
i40e_fdir_teardown(struct i40e_pf * pf)9767 static void i40e_fdir_teardown(struct i40e_pf *pf)
9768 {
9769 	struct i40e_vsi *vsi;
9770 
9771 	i40e_fdir_filter_exit(pf);
9772 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9773 	if (vsi)
9774 		i40e_vsi_release(vsi);
9775 }
9776 
9777 /**
9778  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9779  * @vsi: PF main vsi
9780  * @seid: seid of main or channel VSIs
9781  *
9782  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9783  * existed before reset
9784  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)9785 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9786 {
9787 	struct i40e_cloud_filter *cfilter;
9788 	struct i40e_pf *pf = vsi->back;
9789 	struct hlist_node *node;
9790 	i40e_status ret;
9791 
9792 	/* Add cloud filters back if they exist */
9793 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9794 				  cloud_node) {
9795 		if (cfilter->seid != seid)
9796 			continue;
9797 
9798 		if (cfilter->dst_port)
9799 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9800 								true);
9801 		else
9802 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9803 
9804 		if (ret) {
9805 			dev_dbg(&pf->pdev->dev,
9806 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9807 				i40e_stat_str(&pf->hw, ret),
9808 				i40e_aq_str(&pf->hw,
9809 					    pf->hw.aq.asq_last_status));
9810 			return ret;
9811 		}
9812 	}
9813 	return 0;
9814 }
9815 
9816 /**
9817  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9818  * @vsi: PF main vsi
9819  *
9820  * Rebuilds channel VSIs if they existed before reset
9821  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)9822 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9823 {
9824 	struct i40e_channel *ch, *ch_tmp;
9825 	i40e_status ret;
9826 
9827 	if (list_empty(&vsi->ch_list))
9828 		return 0;
9829 
9830 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9831 		if (!ch->initialized)
9832 			break;
9833 		/* Proceed with creation of channel (VMDq2) VSI */
9834 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9835 		if (ret) {
9836 			dev_info(&vsi->back->pdev->dev,
9837 				 "failed to rebuild channels using uplink_seid %u\n",
9838 				 vsi->uplink_seid);
9839 			return ret;
9840 		}
9841 		/* Reconfigure TX queues using QTX_CTL register */
9842 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9843 		if (ret) {
9844 			dev_info(&vsi->back->pdev->dev,
9845 				 "failed to configure TX rings for channel %u\n",
9846 				 ch->seid);
9847 			return ret;
9848 		}
9849 		/* update 'next_base_queue' */
9850 		vsi->next_base_queue = vsi->next_base_queue +
9851 							ch->num_queue_pairs;
9852 		if (ch->max_tx_rate) {
9853 			u64 credits = ch->max_tx_rate;
9854 
9855 			if (i40e_set_bw_limit(vsi, ch->seid,
9856 					      ch->max_tx_rate))
9857 				return -EINVAL;
9858 
9859 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9860 			dev_dbg(&vsi->back->pdev->dev,
9861 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9862 				ch->max_tx_rate,
9863 				credits,
9864 				ch->seid);
9865 		}
9866 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9867 		if (ret) {
9868 			dev_dbg(&vsi->back->pdev->dev,
9869 				"Failed to rebuild cloud filters for channel VSI %u\n",
9870 				ch->seid);
9871 			return ret;
9872 		}
9873 	}
9874 	return 0;
9875 }
9876 
9877 /**
9878  * i40e_prep_for_reset - prep for the core to reset
9879  * @pf: board private structure
9880  * @lock_acquired: indicates whether or not the lock has been acquired
9881  * before this function was called.
9882  *
9883  * Close up the VFs and other things in prep for PF Reset.
9884   **/
i40e_prep_for_reset(struct i40e_pf * pf,bool lock_acquired)9885 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9886 {
9887 	struct i40e_hw *hw = &pf->hw;
9888 	i40e_status ret = 0;
9889 	u32 v;
9890 
9891 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9892 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9893 		return;
9894 	if (i40e_check_asq_alive(&pf->hw))
9895 		i40e_vc_notify_reset(pf);
9896 
9897 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9898 
9899 	/* quiesce the VSIs and their queues that are not already DOWN */
9900 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9901 	if (!lock_acquired)
9902 		rtnl_lock();
9903 	i40e_pf_quiesce_all_vsi(pf);
9904 	if (!lock_acquired)
9905 		rtnl_unlock();
9906 
9907 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9908 		if (pf->vsi[v])
9909 			pf->vsi[v]->seid = 0;
9910 	}
9911 
9912 	i40e_shutdown_adminq(&pf->hw);
9913 
9914 	/* call shutdown HMC */
9915 	if (hw->hmc.hmc_obj) {
9916 		ret = i40e_shutdown_lan_hmc(hw);
9917 		if (ret)
9918 			dev_warn(&pf->pdev->dev,
9919 				 "shutdown_lan_hmc failed: %d\n", ret);
9920 	}
9921 
9922 	/* Save the current PTP time so that we can restore the time after the
9923 	 * reset completes.
9924 	 */
9925 	i40e_ptp_save_hw_time(pf);
9926 }
9927 
9928 /**
9929  * i40e_send_version - update firmware with driver version
9930  * @pf: PF struct
9931  */
i40e_send_version(struct i40e_pf * pf)9932 static void i40e_send_version(struct i40e_pf *pf)
9933 {
9934 	struct i40e_driver_version dv;
9935 
9936 	dv.major_version = 0xff;
9937 	dv.minor_version = 0xff;
9938 	dv.build_version = 0xff;
9939 	dv.subbuild_version = 0;
9940 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
9941 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9942 }
9943 
9944 /**
9945  * i40e_get_oem_version - get OEM specific version information
9946  * @hw: pointer to the hardware structure
9947  **/
i40e_get_oem_version(struct i40e_hw * hw)9948 static void i40e_get_oem_version(struct i40e_hw *hw)
9949 {
9950 	u16 block_offset = 0xffff;
9951 	u16 block_length = 0;
9952 	u16 capabilities = 0;
9953 	u16 gen_snap = 0;
9954 	u16 release = 0;
9955 
9956 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
9957 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
9958 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
9959 #define I40E_NVM_OEM_GEN_OFFSET			0x02
9960 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
9961 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
9962 #define I40E_NVM_OEM_LENGTH			3
9963 
9964 	/* Check if pointer to OEM version block is valid. */
9965 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9966 	if (block_offset == 0xffff)
9967 		return;
9968 
9969 	/* Check if OEM version block has correct length. */
9970 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9971 			   &block_length);
9972 	if (block_length < I40E_NVM_OEM_LENGTH)
9973 		return;
9974 
9975 	/* Check if OEM version format is as expected. */
9976 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9977 			   &capabilities);
9978 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9979 		return;
9980 
9981 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9982 			   &gen_snap);
9983 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9984 			   &release);
9985 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9986 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9987 }
9988 
9989 /**
9990  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9991  * @pf: board private structure
9992  **/
i40e_reset(struct i40e_pf * pf)9993 static int i40e_reset(struct i40e_pf *pf)
9994 {
9995 	struct i40e_hw *hw = &pf->hw;
9996 	i40e_status ret;
9997 
9998 	ret = i40e_pf_reset(hw);
9999 	if (ret) {
10000 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10001 		set_bit(__I40E_RESET_FAILED, pf->state);
10002 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10003 	} else {
10004 		pf->pfr_count++;
10005 	}
10006 	return ret;
10007 }
10008 
10009 /**
10010  * i40e_rebuild - rebuild using a saved config
10011  * @pf: board private structure
10012  * @reinit: if the Main VSI needs to re-initialized.
10013  * @lock_acquired: indicates whether or not the lock has been acquired
10014  * before this function was called.
10015  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10016 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10017 {
10018 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10019 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10020 	struct i40e_hw *hw = &pf->hw;
10021 	i40e_status ret;
10022 	u32 val;
10023 	int v;
10024 
10025 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10026 	    i40e_check_recovery_mode(pf)) {
10027 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10028 	}
10029 
10030 	if (test_bit(__I40E_DOWN, pf->state) &&
10031 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10032 	    !old_recovery_mode_bit)
10033 		goto clear_recovery;
10034 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10035 
10036 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10037 	ret = i40e_init_adminq(&pf->hw);
10038 	if (ret) {
10039 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10040 			 i40e_stat_str(&pf->hw, ret),
10041 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10042 		goto clear_recovery;
10043 	}
10044 	i40e_get_oem_version(&pf->hw);
10045 
10046 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10047 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
10048 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
10049 		/* The following delay is necessary for 4.33 firmware and older
10050 		 * to recover after EMP reset. 200 ms should suffice but we
10051 		 * put here 300 ms to be sure that FW is ready to operate
10052 		 * after reset.
10053 		 */
10054 		mdelay(300);
10055 	}
10056 
10057 	/* re-verify the eeprom if we just had an EMP reset */
10058 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10059 		i40e_verify_eeprom(pf);
10060 
10061 	/* if we are going out of or into recovery mode we have to act
10062 	 * accordingly with regard to resources initialization
10063 	 * and deinitialization
10064 	 */
10065 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10066 	    old_recovery_mode_bit) {
10067 		if (i40e_get_capabilities(pf,
10068 					  i40e_aqc_opc_list_func_capabilities))
10069 			goto end_unlock;
10070 
10071 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10072 			/* we're staying in recovery mode so we'll reinitialize
10073 			 * misc vector here
10074 			 */
10075 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10076 				goto end_unlock;
10077 		} else {
10078 			if (!lock_acquired)
10079 				rtnl_lock();
10080 			/* we're going out of recovery mode so we'll free
10081 			 * the IRQ allocated specifically for recovery mode
10082 			 * and restore the interrupt scheme
10083 			 */
10084 			free_irq(pf->pdev->irq, pf);
10085 			i40e_clear_interrupt_scheme(pf);
10086 			if (i40e_restore_interrupt_scheme(pf))
10087 				goto end_unlock;
10088 		}
10089 
10090 		/* tell the firmware that we're starting */
10091 		i40e_send_version(pf);
10092 
10093 		/* bail out in case recovery mode was detected, as there is
10094 		 * no need for further configuration.
10095 		 */
10096 		goto end_unlock;
10097 	}
10098 
10099 	i40e_clear_pxe_mode(hw);
10100 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10101 	if (ret)
10102 		goto end_core_reset;
10103 
10104 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10105 				hw->func_caps.num_rx_qp, 0, 0);
10106 	if (ret) {
10107 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10108 		goto end_core_reset;
10109 	}
10110 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10111 	if (ret) {
10112 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10113 		goto end_core_reset;
10114 	}
10115 
10116 	/* Enable FW to write a default DCB config on link-up */
10117 	i40e_aq_set_dcb_parameters(hw, true, NULL);
10118 
10119 #ifdef CONFIG_I40E_DCB
10120 	ret = i40e_init_pf_dcb(pf);
10121 	if (ret) {
10122 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
10123 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10124 		/* Continue without DCB enabled */
10125 	}
10126 #endif /* CONFIG_I40E_DCB */
10127 	/* do basic switch setup */
10128 	if (!lock_acquired)
10129 		rtnl_lock();
10130 	ret = i40e_setup_pf_switch(pf, reinit, true);
10131 	if (ret)
10132 		goto end_unlock;
10133 
10134 	/* The driver only wants link up/down and module qualification
10135 	 * reports from firmware.  Note the negative logic.
10136 	 */
10137 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10138 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10139 					 I40E_AQ_EVENT_MEDIA_NA |
10140 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10141 	if (ret)
10142 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10143 			 i40e_stat_str(&pf->hw, ret),
10144 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10145 
10146 	/* Rebuild the VSIs and VEBs that existed before reset.
10147 	 * They are still in our local switch element arrays, so only
10148 	 * need to rebuild the switch model in the HW.
10149 	 *
10150 	 * If there were VEBs but the reconstitution failed, we'll try
10151 	 * try to recover minimal use by getting the basic PF VSI working.
10152 	 */
10153 	if (vsi->uplink_seid != pf->mac_seid) {
10154 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10155 		/* find the one VEB connected to the MAC, and find orphans */
10156 		for (v = 0; v < I40E_MAX_VEB; v++) {
10157 			if (!pf->veb[v])
10158 				continue;
10159 
10160 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10161 			    pf->veb[v]->uplink_seid == 0) {
10162 				ret = i40e_reconstitute_veb(pf->veb[v]);
10163 
10164 				if (!ret)
10165 					continue;
10166 
10167 				/* If Main VEB failed, we're in deep doodoo,
10168 				 * so give up rebuilding the switch and set up
10169 				 * for minimal rebuild of PF VSI.
10170 				 * If orphan failed, we'll report the error
10171 				 * but try to keep going.
10172 				 */
10173 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10174 					dev_info(&pf->pdev->dev,
10175 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10176 						 ret);
10177 					vsi->uplink_seid = pf->mac_seid;
10178 					break;
10179 				} else if (pf->veb[v]->uplink_seid == 0) {
10180 					dev_info(&pf->pdev->dev,
10181 						 "rebuild of orphan VEB failed: %d\n",
10182 						 ret);
10183 				}
10184 			}
10185 		}
10186 	}
10187 
10188 	if (vsi->uplink_seid == pf->mac_seid) {
10189 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10190 		/* no VEB, so rebuild only the Main VSI */
10191 		ret = i40e_add_vsi(vsi);
10192 		if (ret) {
10193 			dev_info(&pf->pdev->dev,
10194 				 "rebuild of Main VSI failed: %d\n", ret);
10195 			goto end_unlock;
10196 		}
10197 	}
10198 
10199 	if (vsi->mqprio_qopt.max_rate[0]) {
10200 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10201 		u64 credits = 0;
10202 
10203 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10204 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10205 		if (ret)
10206 			goto end_unlock;
10207 
10208 		credits = max_tx_rate;
10209 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10210 		dev_dbg(&vsi->back->pdev->dev,
10211 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10212 			max_tx_rate,
10213 			credits,
10214 			vsi->seid);
10215 	}
10216 
10217 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10218 	if (ret)
10219 		goto end_unlock;
10220 
10221 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10222 	 * for this main VSI if they exist
10223 	 */
10224 	ret = i40e_rebuild_channels(vsi);
10225 	if (ret)
10226 		goto end_unlock;
10227 
10228 	/* Reconfigure hardware for allowing smaller MSS in the case
10229 	 * of TSO, so that we avoid the MDD being fired and causing
10230 	 * a reset in the case of small MSS+TSO.
10231 	 */
10232 #define I40E_REG_MSS          0x000E64DC
10233 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10234 #define I40E_64BYTE_MSS       0x400000
10235 	val = rd32(hw, I40E_REG_MSS);
10236 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10237 		val &= ~I40E_REG_MSS_MIN_MASK;
10238 		val |= I40E_64BYTE_MSS;
10239 		wr32(hw, I40E_REG_MSS, val);
10240 	}
10241 
10242 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10243 		msleep(75);
10244 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10245 		if (ret)
10246 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10247 				 i40e_stat_str(&pf->hw, ret),
10248 				 i40e_aq_str(&pf->hw,
10249 					     pf->hw.aq.asq_last_status));
10250 	}
10251 	/* reinit the misc interrupt */
10252 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10253 		ret = i40e_setup_misc_vector(pf);
10254 
10255 	/* Add a filter to drop all Flow control frames from any VSI from being
10256 	 * transmitted. By doing so we stop a malicious VF from sending out
10257 	 * PAUSE or PFC frames and potentially controlling traffic for other
10258 	 * PF/VF VSIs.
10259 	 * The FW can still send Flow control frames if enabled.
10260 	 */
10261 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10262 						       pf->main_vsi_seid);
10263 
10264 	/* restart the VSIs that were rebuilt and running before the reset */
10265 	i40e_pf_unquiesce_all_vsi(pf);
10266 
10267 	/* Release the RTNL lock before we start resetting VFs */
10268 	if (!lock_acquired)
10269 		rtnl_unlock();
10270 
10271 	/* Restore promiscuous settings */
10272 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10273 	if (ret)
10274 		dev_warn(&pf->pdev->dev,
10275 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10276 			 pf->cur_promisc ? "on" : "off",
10277 			 i40e_stat_str(&pf->hw, ret),
10278 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10279 
10280 	i40e_reset_all_vfs(pf, true);
10281 
10282 	/* tell the firmware that we're starting */
10283 	i40e_send_version(pf);
10284 
10285 	/* We've already released the lock, so don't do it again */
10286 	goto end_core_reset;
10287 
10288 end_unlock:
10289 	if (!lock_acquired)
10290 		rtnl_unlock();
10291 end_core_reset:
10292 	clear_bit(__I40E_RESET_FAILED, pf->state);
10293 clear_recovery:
10294 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10295 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10296 }
10297 
10298 /**
10299  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10300  * @pf: board private structure
10301  * @reinit: if the Main VSI needs to re-initialized.
10302  * @lock_acquired: indicates whether or not the lock has been acquired
10303  * before this function was called.
10304  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10305 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10306 				   bool lock_acquired)
10307 {
10308 	int ret;
10309 	/* Now we wait for GRST to settle out.
10310 	 * We don't have to delete the VEBs or VSIs from the hw switch
10311 	 * because the reset will make them disappear.
10312 	 */
10313 	ret = i40e_reset(pf);
10314 	if (!ret)
10315 		i40e_rebuild(pf, reinit, lock_acquired);
10316 }
10317 
10318 /**
10319  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10320  * @pf: board private structure
10321  *
10322  * Close up the VFs and other things in prep for a Core Reset,
10323  * then get ready to rebuild the world.
10324  * @lock_acquired: indicates whether or not the lock has been acquired
10325  * before this function was called.
10326  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)10327 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10328 {
10329 	i40e_prep_for_reset(pf, lock_acquired);
10330 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10331 }
10332 
10333 /**
10334  * i40e_handle_mdd_event
10335  * @pf: pointer to the PF structure
10336  *
10337  * Called from the MDD irq handler to identify possibly malicious vfs
10338  **/
i40e_handle_mdd_event(struct i40e_pf * pf)10339 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10340 {
10341 	struct i40e_hw *hw = &pf->hw;
10342 	bool mdd_detected = false;
10343 	struct i40e_vf *vf;
10344 	u32 reg;
10345 	int i;
10346 
10347 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10348 		return;
10349 
10350 	/* find what triggered the MDD event */
10351 	reg = rd32(hw, I40E_GL_MDET_TX);
10352 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10353 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10354 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10355 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10356 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10357 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10358 				I40E_GL_MDET_TX_EVENT_SHIFT;
10359 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10360 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10361 				pf->hw.func_caps.base_queue;
10362 		if (netif_msg_tx_err(pf))
10363 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10364 				 event, queue, pf_num, vf_num);
10365 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10366 		mdd_detected = true;
10367 	}
10368 	reg = rd32(hw, I40E_GL_MDET_RX);
10369 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10370 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10371 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10372 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10373 				I40E_GL_MDET_RX_EVENT_SHIFT;
10374 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10375 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10376 				pf->hw.func_caps.base_queue;
10377 		if (netif_msg_rx_err(pf))
10378 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10379 				 event, queue, func);
10380 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10381 		mdd_detected = true;
10382 	}
10383 
10384 	if (mdd_detected) {
10385 		reg = rd32(hw, I40E_PF_MDET_TX);
10386 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10387 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10388 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10389 		}
10390 		reg = rd32(hw, I40E_PF_MDET_RX);
10391 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10392 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10393 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10394 		}
10395 	}
10396 
10397 	/* see if one of the VFs needs its hand slapped */
10398 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10399 		vf = &(pf->vf[i]);
10400 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10401 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10402 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10403 			vf->num_mdd_events++;
10404 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10405 				 i);
10406 			dev_info(&pf->pdev->dev,
10407 				 "Use PF Control I/F to re-enable the VF\n");
10408 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10409 		}
10410 
10411 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10412 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10413 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10414 			vf->num_mdd_events++;
10415 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10416 				 i);
10417 			dev_info(&pf->pdev->dev,
10418 				 "Use PF Control I/F to re-enable the VF\n");
10419 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10420 		}
10421 	}
10422 
10423 	/* re-enable mdd interrupt cause */
10424 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10425 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10426 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10427 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10428 	i40e_flush(hw);
10429 }
10430 
10431 /**
10432  * i40e_service_task - Run the driver's async subtasks
10433  * @work: pointer to work_struct containing our data
10434  **/
i40e_service_task(struct work_struct * work)10435 static void i40e_service_task(struct work_struct *work)
10436 {
10437 	struct i40e_pf *pf = container_of(work,
10438 					  struct i40e_pf,
10439 					  service_task);
10440 	unsigned long start_time = jiffies;
10441 
10442 	/* don't bother with service tasks if a reset is in progress */
10443 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10444 	    test_bit(__I40E_SUSPENDED, pf->state))
10445 		return;
10446 
10447 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10448 		return;
10449 
10450 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10451 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10452 		i40e_sync_filters_subtask(pf);
10453 		i40e_reset_subtask(pf);
10454 		i40e_handle_mdd_event(pf);
10455 		i40e_vc_process_vflr_event(pf);
10456 		i40e_watchdog_subtask(pf);
10457 		i40e_fdir_reinit_subtask(pf);
10458 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
10459 			/* Client subtask will reopen next time through. */
10460 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
10461 							   true);
10462 		} else {
10463 			i40e_client_subtask(pf);
10464 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
10465 					       pf->state))
10466 				i40e_notify_client_of_l2_param_changes(
10467 								pf->vsi[pf->lan_vsi]);
10468 		}
10469 		i40e_sync_filters_subtask(pf);
10470 	} else {
10471 		i40e_reset_subtask(pf);
10472 	}
10473 
10474 	i40e_clean_adminq_subtask(pf);
10475 
10476 	/* flush memory to make sure state is correct before next watchdog */
10477 	smp_mb__before_atomic();
10478 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
10479 
10480 	/* If the tasks have taken longer than one timer cycle or there
10481 	 * is more work to be done, reschedule the service task now
10482 	 * rather than wait for the timer to tick again.
10483 	 */
10484 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10485 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10486 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10487 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10488 		i40e_service_event_schedule(pf);
10489 }
10490 
10491 /**
10492  * i40e_service_timer - timer callback
10493  * @t: timer list pointer
10494  **/
i40e_service_timer(struct timer_list * t)10495 static void i40e_service_timer(struct timer_list *t)
10496 {
10497 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10498 
10499 	mod_timer(&pf->service_timer,
10500 		  round_jiffies(jiffies + pf->service_timer_period));
10501 	i40e_service_event_schedule(pf);
10502 }
10503 
10504 /**
10505  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10506  * @vsi: the VSI being configured
10507  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)10508 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10509 {
10510 	struct i40e_pf *pf = vsi->back;
10511 
10512 	switch (vsi->type) {
10513 	case I40E_VSI_MAIN:
10514 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10515 		if (!vsi->num_tx_desc)
10516 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10517 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10518 		if (!vsi->num_rx_desc)
10519 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10520 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10521 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10522 			vsi->num_q_vectors = pf->num_lan_msix;
10523 		else
10524 			vsi->num_q_vectors = 1;
10525 
10526 		break;
10527 
10528 	case I40E_VSI_FDIR:
10529 		vsi->alloc_queue_pairs = 1;
10530 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10531 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10532 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10533 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10534 		vsi->num_q_vectors = pf->num_fdsb_msix;
10535 		break;
10536 
10537 	case I40E_VSI_VMDQ2:
10538 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
10539 		if (!vsi->num_tx_desc)
10540 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10541 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10542 		if (!vsi->num_rx_desc)
10543 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10544 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10545 		vsi->num_q_vectors = pf->num_vmdq_msix;
10546 		break;
10547 
10548 	case I40E_VSI_SRIOV:
10549 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10550 		if (!vsi->num_tx_desc)
10551 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10552 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10553 		if (!vsi->num_rx_desc)
10554 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10555 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10556 		break;
10557 
10558 	default:
10559 		WARN_ON(1);
10560 		return -ENODATA;
10561 	}
10562 
10563 	return 0;
10564 }
10565 
10566 /**
10567  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
10568  * @vsi: VSI pointer
10569  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
10570  *
10571  * On error: returns error code (negative)
10572  * On success: returns 0
10573  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)10574 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
10575 {
10576 	struct i40e_ring **next_rings;
10577 	int size;
10578 	int ret = 0;
10579 
10580 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
10581 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
10582 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
10583 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
10584 	if (!vsi->tx_rings)
10585 		return -ENOMEM;
10586 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
10587 	if (i40e_enabled_xdp_vsi(vsi)) {
10588 		vsi->xdp_rings = next_rings;
10589 		next_rings += vsi->alloc_queue_pairs;
10590 	}
10591 	vsi->rx_rings = next_rings;
10592 
10593 	if (alloc_qvectors) {
10594 		/* allocate memory for q_vector pointers */
10595 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
10596 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
10597 		if (!vsi->q_vectors) {
10598 			ret = -ENOMEM;
10599 			goto err_vectors;
10600 		}
10601 	}
10602 	return ret;
10603 
10604 err_vectors:
10605 	kfree(vsi->tx_rings);
10606 	return ret;
10607 }
10608 
10609 /**
10610  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
10611  * @pf: board private structure
10612  * @type: type of VSI
10613  *
10614  * On error: returns error code (negative)
10615  * On success: returns vsi index in PF (positive)
10616  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)10617 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
10618 {
10619 	int ret = -ENODEV;
10620 	struct i40e_vsi *vsi;
10621 	int vsi_idx;
10622 	int i;
10623 
10624 	/* Need to protect the allocation of the VSIs at the PF level */
10625 	mutex_lock(&pf->switch_mutex);
10626 
10627 	/* VSI list may be fragmented if VSI creation/destruction has
10628 	 * been happening.  We can afford to do a quick scan to look
10629 	 * for any free VSIs in the list.
10630 	 *
10631 	 * find next empty vsi slot, looping back around if necessary
10632 	 */
10633 	i = pf->next_vsi;
10634 	while (i < pf->num_alloc_vsi && pf->vsi[i])
10635 		i++;
10636 	if (i >= pf->num_alloc_vsi) {
10637 		i = 0;
10638 		while (i < pf->next_vsi && pf->vsi[i])
10639 			i++;
10640 	}
10641 
10642 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10643 		vsi_idx = i;             /* Found one! */
10644 	} else {
10645 		ret = -ENODEV;
10646 		goto unlock_pf;  /* out of VSI slots! */
10647 	}
10648 	pf->next_vsi = ++i;
10649 
10650 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10651 	if (!vsi) {
10652 		ret = -ENOMEM;
10653 		goto unlock_pf;
10654 	}
10655 	vsi->type = type;
10656 	vsi->back = pf;
10657 	set_bit(__I40E_VSI_DOWN, vsi->state);
10658 	vsi->flags = 0;
10659 	vsi->idx = vsi_idx;
10660 	vsi->int_rate_limit = 0;
10661 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10662 				pf->rss_table_size : 64;
10663 	vsi->netdev_registered = false;
10664 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10665 	hash_init(vsi->mac_filter_hash);
10666 	vsi->irqs_ready = false;
10667 
10668 	if (type == I40E_VSI_MAIN) {
10669 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
10670 		if (!vsi->af_xdp_zc_qps)
10671 			goto err_rings;
10672 	}
10673 
10674 	ret = i40e_set_num_rings_in_vsi(vsi);
10675 	if (ret)
10676 		goto err_rings;
10677 
10678 	ret = i40e_vsi_alloc_arrays(vsi, true);
10679 	if (ret)
10680 		goto err_rings;
10681 
10682 	/* Setup default MSIX irq handler for VSI */
10683 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10684 
10685 	/* Initialize VSI lock */
10686 	spin_lock_init(&vsi->mac_filter_hash_lock);
10687 	pf->vsi[vsi_idx] = vsi;
10688 	ret = vsi_idx;
10689 	goto unlock_pf;
10690 
10691 err_rings:
10692 	bitmap_free(vsi->af_xdp_zc_qps);
10693 	pf->next_vsi = i - 1;
10694 	kfree(vsi);
10695 unlock_pf:
10696 	mutex_unlock(&pf->switch_mutex);
10697 	return ret;
10698 }
10699 
10700 /**
10701  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10702  * @vsi: VSI pointer
10703  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10704  *
10705  * On error: returns error code (negative)
10706  * On success: returns 0
10707  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)10708 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10709 {
10710 	/* free the ring and vector containers */
10711 	if (free_qvectors) {
10712 		kfree(vsi->q_vectors);
10713 		vsi->q_vectors = NULL;
10714 	}
10715 	kfree(vsi->tx_rings);
10716 	vsi->tx_rings = NULL;
10717 	vsi->rx_rings = NULL;
10718 	vsi->xdp_rings = NULL;
10719 }
10720 
10721 /**
10722  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10723  * and lookup table
10724  * @vsi: Pointer to VSI structure
10725  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)10726 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10727 {
10728 	if (!vsi)
10729 		return;
10730 
10731 	kfree(vsi->rss_hkey_user);
10732 	vsi->rss_hkey_user = NULL;
10733 
10734 	kfree(vsi->rss_lut_user);
10735 	vsi->rss_lut_user = NULL;
10736 }
10737 
10738 /**
10739  * i40e_vsi_clear - Deallocate the VSI provided
10740  * @vsi: the VSI being un-configured
10741  **/
i40e_vsi_clear(struct i40e_vsi * vsi)10742 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10743 {
10744 	struct i40e_pf *pf;
10745 
10746 	if (!vsi)
10747 		return 0;
10748 
10749 	if (!vsi->back)
10750 		goto free_vsi;
10751 	pf = vsi->back;
10752 
10753 	mutex_lock(&pf->switch_mutex);
10754 	if (!pf->vsi[vsi->idx]) {
10755 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10756 			vsi->idx, vsi->idx, vsi->type);
10757 		goto unlock_vsi;
10758 	}
10759 
10760 	if (pf->vsi[vsi->idx] != vsi) {
10761 		dev_err(&pf->pdev->dev,
10762 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10763 			pf->vsi[vsi->idx]->idx,
10764 			pf->vsi[vsi->idx]->type,
10765 			vsi->idx, vsi->type);
10766 		goto unlock_vsi;
10767 	}
10768 
10769 	/* updates the PF for this cleared vsi */
10770 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10771 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10772 
10773 	bitmap_free(vsi->af_xdp_zc_qps);
10774 	i40e_vsi_free_arrays(vsi, true);
10775 	i40e_clear_rss_config_user(vsi);
10776 
10777 	pf->vsi[vsi->idx] = NULL;
10778 	if (vsi->idx < pf->next_vsi)
10779 		pf->next_vsi = vsi->idx;
10780 
10781 unlock_vsi:
10782 	mutex_unlock(&pf->switch_mutex);
10783 free_vsi:
10784 	kfree(vsi);
10785 
10786 	return 0;
10787 }
10788 
10789 /**
10790  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10791  * @vsi: the VSI being cleaned
10792  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)10793 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10794 {
10795 	int i;
10796 
10797 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10798 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10799 			kfree_rcu(vsi->tx_rings[i], rcu);
10800 			WRITE_ONCE(vsi->tx_rings[i], NULL);
10801 			WRITE_ONCE(vsi->rx_rings[i], NULL);
10802 			if (vsi->xdp_rings)
10803 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
10804 		}
10805 	}
10806 }
10807 
10808 /**
10809  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10810  * @vsi: the VSI being configured
10811  **/
i40e_alloc_rings(struct i40e_vsi * vsi)10812 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10813 {
10814 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10815 	struct i40e_pf *pf = vsi->back;
10816 	struct i40e_ring *ring;
10817 
10818 	/* Set basic values in the rings to be used later during open() */
10819 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10820 		/* allocate space for both Tx and Rx in one shot */
10821 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10822 		if (!ring)
10823 			goto err_out;
10824 
10825 		ring->queue_index = i;
10826 		ring->reg_idx = vsi->base_queue + i;
10827 		ring->ring_active = false;
10828 		ring->vsi = vsi;
10829 		ring->netdev = vsi->netdev;
10830 		ring->dev = &pf->pdev->dev;
10831 		ring->count = vsi->num_tx_desc;
10832 		ring->size = 0;
10833 		ring->dcb_tc = 0;
10834 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10835 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10836 		ring->itr_setting = pf->tx_itr_default;
10837 		WRITE_ONCE(vsi->tx_rings[i], ring++);
10838 
10839 		if (!i40e_enabled_xdp_vsi(vsi))
10840 			goto setup_rx;
10841 
10842 		ring->queue_index = vsi->alloc_queue_pairs + i;
10843 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10844 		ring->ring_active = false;
10845 		ring->vsi = vsi;
10846 		ring->netdev = NULL;
10847 		ring->dev = &pf->pdev->dev;
10848 		ring->count = vsi->num_tx_desc;
10849 		ring->size = 0;
10850 		ring->dcb_tc = 0;
10851 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10852 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10853 		set_ring_xdp(ring);
10854 		ring->itr_setting = pf->tx_itr_default;
10855 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
10856 
10857 setup_rx:
10858 		ring->queue_index = i;
10859 		ring->reg_idx = vsi->base_queue + i;
10860 		ring->ring_active = false;
10861 		ring->vsi = vsi;
10862 		ring->netdev = vsi->netdev;
10863 		ring->dev = &pf->pdev->dev;
10864 		ring->count = vsi->num_rx_desc;
10865 		ring->size = 0;
10866 		ring->dcb_tc = 0;
10867 		ring->itr_setting = pf->rx_itr_default;
10868 		WRITE_ONCE(vsi->rx_rings[i], ring);
10869 	}
10870 
10871 	return 0;
10872 
10873 err_out:
10874 	i40e_vsi_clear_rings(vsi);
10875 	return -ENOMEM;
10876 }
10877 
10878 /**
10879  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10880  * @pf: board private structure
10881  * @vectors: the number of MSI-X vectors to request
10882  *
10883  * Returns the number of vectors reserved, or error
10884  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)10885 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10886 {
10887 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10888 					I40E_MIN_MSIX, vectors);
10889 	if (vectors < 0) {
10890 		dev_info(&pf->pdev->dev,
10891 			 "MSI-X vector reservation failed: %d\n", vectors);
10892 		vectors = 0;
10893 	}
10894 
10895 	return vectors;
10896 }
10897 
10898 /**
10899  * i40e_init_msix - Setup the MSIX capability
10900  * @pf: board private structure
10901  *
10902  * Work with the OS to set up the MSIX vectors needed.
10903  *
10904  * Returns the number of vectors reserved or negative on failure
10905  **/
i40e_init_msix(struct i40e_pf * pf)10906 static int i40e_init_msix(struct i40e_pf *pf)
10907 {
10908 	struct i40e_hw *hw = &pf->hw;
10909 	int cpus, extra_vectors;
10910 	int vectors_left;
10911 	int v_budget, i;
10912 	int v_actual;
10913 	int iwarp_requested = 0;
10914 
10915 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10916 		return -ENODEV;
10917 
10918 	/* The number of vectors we'll request will be comprised of:
10919 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
10920 	 *   - The number of LAN queue pairs
10921 	 *	- Queues being used for RSS.
10922 	 *		We don't need as many as max_rss_size vectors.
10923 	 *		use rss_size instead in the calculation since that
10924 	 *		is governed by number of cpus in the system.
10925 	 *	- assumes symmetric Tx/Rx pairing
10926 	 *   - The number of VMDq pairs
10927 	 *   - The CPU count within the NUMA node if iWARP is enabled
10928 	 * Once we count this up, try the request.
10929 	 *
10930 	 * If we can't get what we want, we'll simplify to nearly nothing
10931 	 * and try again.  If that still fails, we punt.
10932 	 */
10933 	vectors_left = hw->func_caps.num_msix_vectors;
10934 	v_budget = 0;
10935 
10936 	/* reserve one vector for miscellaneous handler */
10937 	if (vectors_left) {
10938 		v_budget++;
10939 		vectors_left--;
10940 	}
10941 
10942 	/* reserve some vectors for the main PF traffic queues. Initially we
10943 	 * only reserve at most 50% of the available vectors, in the case that
10944 	 * the number of online CPUs is large. This ensures that we can enable
10945 	 * extra features as well. Once we've enabled the other features, we
10946 	 * will use any remaining vectors to reach as close as we can to the
10947 	 * number of online CPUs.
10948 	 */
10949 	cpus = num_online_cpus();
10950 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10951 	vectors_left -= pf->num_lan_msix;
10952 
10953 	/* reserve one vector for sideband flow director */
10954 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10955 		if (vectors_left) {
10956 			pf->num_fdsb_msix = 1;
10957 			v_budget++;
10958 			vectors_left--;
10959 		} else {
10960 			pf->num_fdsb_msix = 0;
10961 		}
10962 	}
10963 
10964 	/* can we reserve enough for iWARP? */
10965 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10966 		iwarp_requested = pf->num_iwarp_msix;
10967 
10968 		if (!vectors_left)
10969 			pf->num_iwarp_msix = 0;
10970 		else if (vectors_left < pf->num_iwarp_msix)
10971 			pf->num_iwarp_msix = 1;
10972 		v_budget += pf->num_iwarp_msix;
10973 		vectors_left -= pf->num_iwarp_msix;
10974 	}
10975 
10976 	/* any vectors left over go for VMDq support */
10977 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10978 		if (!vectors_left) {
10979 			pf->num_vmdq_msix = 0;
10980 			pf->num_vmdq_qps = 0;
10981 		} else {
10982 			int vmdq_vecs_wanted =
10983 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
10984 			int vmdq_vecs =
10985 				min_t(int, vectors_left, vmdq_vecs_wanted);
10986 
10987 			/* if we're short on vectors for what's desired, we limit
10988 			 * the queues per vmdq.  If this is still more than are
10989 			 * available, the user will need to change the number of
10990 			 * queues/vectors used by the PF later with the ethtool
10991 			 * channels command
10992 			 */
10993 			if (vectors_left < vmdq_vecs_wanted) {
10994 				pf->num_vmdq_qps = 1;
10995 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
10996 				vmdq_vecs = min_t(int,
10997 						  vectors_left,
10998 						  vmdq_vecs_wanted);
10999 			}
11000 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11001 
11002 			v_budget += vmdq_vecs;
11003 			vectors_left -= vmdq_vecs;
11004 		}
11005 	}
11006 
11007 	/* On systems with a large number of SMP cores, we previously limited
11008 	 * the number of vectors for num_lan_msix to be at most 50% of the
11009 	 * available vectors, to allow for other features. Now, we add back
11010 	 * the remaining vectors. However, we ensure that the total
11011 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11012 	 * calculate the number of vectors we can add without going over the
11013 	 * cap of CPUs. For systems with a small number of CPUs this will be
11014 	 * zero.
11015 	 */
11016 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11017 	pf->num_lan_msix += extra_vectors;
11018 	vectors_left -= extra_vectors;
11019 
11020 	WARN(vectors_left < 0,
11021 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11022 
11023 	v_budget += pf->num_lan_msix;
11024 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11025 				   GFP_KERNEL);
11026 	if (!pf->msix_entries)
11027 		return -ENOMEM;
11028 
11029 	for (i = 0; i < v_budget; i++)
11030 		pf->msix_entries[i].entry = i;
11031 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11032 
11033 	if (v_actual < I40E_MIN_MSIX) {
11034 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11035 		kfree(pf->msix_entries);
11036 		pf->msix_entries = NULL;
11037 		pci_disable_msix(pf->pdev);
11038 		return -ENODEV;
11039 
11040 	} else if (v_actual == I40E_MIN_MSIX) {
11041 		/* Adjust for minimal MSIX use */
11042 		pf->num_vmdq_vsis = 0;
11043 		pf->num_vmdq_qps = 0;
11044 		pf->num_lan_qps = 1;
11045 		pf->num_lan_msix = 1;
11046 
11047 	} else if (v_actual != v_budget) {
11048 		/* If we have limited resources, we will start with no vectors
11049 		 * for the special features and then allocate vectors to some
11050 		 * of these features based on the policy and at the end disable
11051 		 * the features that did not get any vectors.
11052 		 */
11053 		int vec;
11054 
11055 		dev_info(&pf->pdev->dev,
11056 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11057 			 v_actual, v_budget);
11058 		/* reserve the misc vector */
11059 		vec = v_actual - 1;
11060 
11061 		/* Scale vector usage down */
11062 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11063 		pf->num_vmdq_vsis = 1;
11064 		pf->num_vmdq_qps = 1;
11065 
11066 		/* partition out the remaining vectors */
11067 		switch (vec) {
11068 		case 2:
11069 			pf->num_lan_msix = 1;
11070 			break;
11071 		case 3:
11072 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11073 				pf->num_lan_msix = 1;
11074 				pf->num_iwarp_msix = 1;
11075 			} else {
11076 				pf->num_lan_msix = 2;
11077 			}
11078 			break;
11079 		default:
11080 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11081 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11082 						 iwarp_requested);
11083 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11084 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11085 			} else {
11086 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11087 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11088 			}
11089 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11090 				pf->num_fdsb_msix = 1;
11091 				vec--;
11092 			}
11093 			pf->num_lan_msix = min_t(int,
11094 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11095 							      pf->num_lan_msix);
11096 			pf->num_lan_qps = pf->num_lan_msix;
11097 			break;
11098 		}
11099 	}
11100 
11101 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11102 	    (pf->num_fdsb_msix == 0)) {
11103 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11104 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11105 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11106 	}
11107 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11108 	    (pf->num_vmdq_msix == 0)) {
11109 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11110 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11111 	}
11112 
11113 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11114 	    (pf->num_iwarp_msix == 0)) {
11115 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11116 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11117 	}
11118 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11119 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11120 		   pf->num_lan_msix,
11121 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11122 		   pf->num_fdsb_msix,
11123 		   pf->num_iwarp_msix);
11124 
11125 	return v_actual;
11126 }
11127 
11128 /**
11129  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11130  * @vsi: the VSI being configured
11131  * @v_idx: index of the vector in the vsi struct
11132  *
11133  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11134  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)11135 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11136 {
11137 	struct i40e_q_vector *q_vector;
11138 
11139 	/* allocate q_vector */
11140 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11141 	if (!q_vector)
11142 		return -ENOMEM;
11143 
11144 	q_vector->vsi = vsi;
11145 	q_vector->v_idx = v_idx;
11146 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11147 
11148 	if (vsi->netdev)
11149 		netif_napi_add(vsi->netdev, &q_vector->napi,
11150 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11151 
11152 	/* tie q_vector and vsi together */
11153 	vsi->q_vectors[v_idx] = q_vector;
11154 
11155 	return 0;
11156 }
11157 
11158 /**
11159  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11160  * @vsi: the VSI being configured
11161  *
11162  * We allocate one q_vector per queue interrupt.  If allocation fails we
11163  * return -ENOMEM.
11164  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)11165 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11166 {
11167 	struct i40e_pf *pf = vsi->back;
11168 	int err, v_idx, num_q_vectors;
11169 
11170 	/* if not MSIX, give the one vector only to the LAN VSI */
11171 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11172 		num_q_vectors = vsi->num_q_vectors;
11173 	else if (vsi == pf->vsi[pf->lan_vsi])
11174 		num_q_vectors = 1;
11175 	else
11176 		return -EINVAL;
11177 
11178 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11179 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11180 		if (err)
11181 			goto err_out;
11182 	}
11183 
11184 	return 0;
11185 
11186 err_out:
11187 	while (v_idx--)
11188 		i40e_free_q_vector(vsi, v_idx);
11189 
11190 	return err;
11191 }
11192 
11193 /**
11194  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11195  * @pf: board private structure to initialize
11196  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)11197 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11198 {
11199 	int vectors = 0;
11200 	ssize_t size;
11201 
11202 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11203 		vectors = i40e_init_msix(pf);
11204 		if (vectors < 0) {
11205 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11206 				       I40E_FLAG_IWARP_ENABLED	|
11207 				       I40E_FLAG_RSS_ENABLED	|
11208 				       I40E_FLAG_DCB_CAPABLE	|
11209 				       I40E_FLAG_DCB_ENABLED	|
11210 				       I40E_FLAG_SRIOV_ENABLED	|
11211 				       I40E_FLAG_FD_SB_ENABLED	|
11212 				       I40E_FLAG_FD_ATR_ENABLED	|
11213 				       I40E_FLAG_VMDQ_ENABLED);
11214 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11215 
11216 			/* rework the queue expectations without MSIX */
11217 			i40e_determine_queue_usage(pf);
11218 		}
11219 	}
11220 
11221 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11222 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11223 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11224 		vectors = pci_enable_msi(pf->pdev);
11225 		if (vectors < 0) {
11226 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11227 				 vectors);
11228 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11229 		}
11230 		vectors = 1;  /* one MSI or Legacy vector */
11231 	}
11232 
11233 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11234 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11235 
11236 	/* set up vector assignment tracking */
11237 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11238 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11239 	if (!pf->irq_pile)
11240 		return -ENOMEM;
11241 
11242 	pf->irq_pile->num_entries = vectors;
11243 	pf->irq_pile->search_hint = 0;
11244 
11245 	/* track first vector for misc interrupts, ignore return */
11246 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11247 
11248 	return 0;
11249 }
11250 
11251 /**
11252  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11253  * @pf: private board data structure
11254  *
11255  * Restore the interrupt scheme that was cleared when we suspended the
11256  * device. This should be called during resume to re-allocate the q_vectors
11257  * and reacquire IRQs.
11258  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)11259 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11260 {
11261 	int err, i;
11262 
11263 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11264 	 * scheme. We need to re-enabled them here in order to attempt to
11265 	 * re-acquire the MSI or MSI-X vectors
11266 	 */
11267 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11268 
11269 	err = i40e_init_interrupt_scheme(pf);
11270 	if (err)
11271 		return err;
11272 
11273 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11274 	 * rings together again.
11275 	 */
11276 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11277 		if (pf->vsi[i]) {
11278 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11279 			if (err)
11280 				goto err_unwind;
11281 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11282 		}
11283 	}
11284 
11285 	err = i40e_setup_misc_vector(pf);
11286 	if (err)
11287 		goto err_unwind;
11288 
11289 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11290 		i40e_client_update_msix_info(pf);
11291 
11292 	return 0;
11293 
11294 err_unwind:
11295 	while (i--) {
11296 		if (pf->vsi[i])
11297 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11298 	}
11299 
11300 	return err;
11301 }
11302 
11303 /**
11304  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11305  * non queue events in recovery mode
11306  * @pf: board private structure
11307  *
11308  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11309  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11310  * This is handled differently than in recovery mode since no Tx/Rx resources
11311  * are being allocated.
11312  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)11313 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11314 {
11315 	int err;
11316 
11317 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11318 		err = i40e_setup_misc_vector(pf);
11319 
11320 		if (err) {
11321 			dev_info(&pf->pdev->dev,
11322 				 "MSI-X misc vector request failed, error %d\n",
11323 				 err);
11324 			return err;
11325 		}
11326 	} else {
11327 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11328 
11329 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11330 				  pf->int_name, pf);
11331 
11332 		if (err) {
11333 			dev_info(&pf->pdev->dev,
11334 				 "MSI/legacy misc vector request failed, error %d\n",
11335 				 err);
11336 			return err;
11337 		}
11338 		i40e_enable_misc_int_causes(pf);
11339 		i40e_irq_dynamic_enable_icr0(pf);
11340 	}
11341 
11342 	return 0;
11343 }
11344 
11345 /**
11346  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11347  * @pf: board private structure
11348  *
11349  * This sets up the handler for MSIX 0, which is used to manage the
11350  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11351  * when in MSI or Legacy interrupt mode.
11352  **/
i40e_setup_misc_vector(struct i40e_pf * pf)11353 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11354 {
11355 	struct i40e_hw *hw = &pf->hw;
11356 	int err = 0;
11357 
11358 	/* Only request the IRQ once, the first time through. */
11359 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11360 		err = request_irq(pf->msix_entries[0].vector,
11361 				  i40e_intr, 0, pf->int_name, pf);
11362 		if (err) {
11363 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11364 			dev_info(&pf->pdev->dev,
11365 				 "request_irq for %s failed: %d\n",
11366 				 pf->int_name, err);
11367 			return -EFAULT;
11368 		}
11369 	}
11370 
11371 	i40e_enable_misc_int_causes(pf);
11372 
11373 	/* associate no queues to the misc vector */
11374 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11375 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11376 
11377 	i40e_flush(hw);
11378 
11379 	i40e_irq_dynamic_enable_icr0(pf);
11380 
11381 	return err;
11382 }
11383 
11384 /**
11385  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11386  * @vsi: Pointer to vsi structure
11387  * @seed: Buffter to store the hash keys
11388  * @lut: Buffer to store the lookup table entries
11389  * @lut_size: Size of buffer to store the lookup table entries
11390  *
11391  * Return 0 on success, negative on failure
11392  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)11393 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11394 			   u8 *lut, u16 lut_size)
11395 {
11396 	struct i40e_pf *pf = vsi->back;
11397 	struct i40e_hw *hw = &pf->hw;
11398 	int ret = 0;
11399 
11400 	if (seed) {
11401 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11402 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11403 		if (ret) {
11404 			dev_info(&pf->pdev->dev,
11405 				 "Cannot get RSS key, err %s aq_err %s\n",
11406 				 i40e_stat_str(&pf->hw, ret),
11407 				 i40e_aq_str(&pf->hw,
11408 					     pf->hw.aq.asq_last_status));
11409 			return ret;
11410 		}
11411 	}
11412 
11413 	if (lut) {
11414 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11415 
11416 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11417 		if (ret) {
11418 			dev_info(&pf->pdev->dev,
11419 				 "Cannot get RSS lut, err %s aq_err %s\n",
11420 				 i40e_stat_str(&pf->hw, ret),
11421 				 i40e_aq_str(&pf->hw,
11422 					     pf->hw.aq.asq_last_status));
11423 			return ret;
11424 		}
11425 	}
11426 
11427 	return ret;
11428 }
11429 
11430 /**
11431  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11432  * @vsi: Pointer to vsi structure
11433  * @seed: RSS hash seed
11434  * @lut: Lookup table
11435  * @lut_size: Lookup table size
11436  *
11437  * Returns 0 on success, negative on failure
11438  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)11439 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11440 			       const u8 *lut, u16 lut_size)
11441 {
11442 	struct i40e_pf *pf = vsi->back;
11443 	struct i40e_hw *hw = &pf->hw;
11444 	u16 vf_id = vsi->vf_id;
11445 	u8 i;
11446 
11447 	/* Fill out hash function seed */
11448 	if (seed) {
11449 		u32 *seed_dw = (u32 *)seed;
11450 
11451 		if (vsi->type == I40E_VSI_MAIN) {
11452 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11453 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
11454 		} else if (vsi->type == I40E_VSI_SRIOV) {
11455 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
11456 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
11457 		} else {
11458 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
11459 		}
11460 	}
11461 
11462 	if (lut) {
11463 		u32 *lut_dw = (u32 *)lut;
11464 
11465 		if (vsi->type == I40E_VSI_MAIN) {
11466 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
11467 				return -EINVAL;
11468 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11469 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
11470 		} else if (vsi->type == I40E_VSI_SRIOV) {
11471 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
11472 				return -EINVAL;
11473 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11474 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
11475 		} else {
11476 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11477 		}
11478 	}
11479 	i40e_flush(hw);
11480 
11481 	return 0;
11482 }
11483 
11484 /**
11485  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
11486  * @vsi: Pointer to VSI structure
11487  * @seed: Buffer to store the keys
11488  * @lut: Buffer to store the lookup table entries
11489  * @lut_size: Size of buffer to store the lookup table entries
11490  *
11491  * Returns 0 on success, negative on failure
11492  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)11493 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11494 			    u8 *lut, u16 lut_size)
11495 {
11496 	struct i40e_pf *pf = vsi->back;
11497 	struct i40e_hw *hw = &pf->hw;
11498 	u16 i;
11499 
11500 	if (seed) {
11501 		u32 *seed_dw = (u32 *)seed;
11502 
11503 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11504 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11505 	}
11506 	if (lut) {
11507 		u32 *lut_dw = (u32 *)lut;
11508 
11509 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11510 			return -EINVAL;
11511 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11512 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11513 	}
11514 
11515 	return 0;
11516 }
11517 
11518 /**
11519  * i40e_config_rss - Configure RSS keys and lut
11520  * @vsi: Pointer to VSI structure
11521  * @seed: RSS hash seed
11522  * @lut: Lookup table
11523  * @lut_size: Lookup table size
11524  *
11525  * Returns 0 on success, negative on failure
11526  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)11527 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11528 {
11529 	struct i40e_pf *pf = vsi->back;
11530 
11531 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11532 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
11533 	else
11534 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
11535 }
11536 
11537 /**
11538  * i40e_get_rss - Get RSS keys and lut
11539  * @vsi: Pointer to VSI structure
11540  * @seed: Buffer to store the keys
11541  * @lut: Buffer to store the lookup table entries
11542  * @lut_size: Size of buffer to store the lookup table entries
11543  *
11544  * Returns 0 on success, negative on failure
11545  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)11546 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11547 {
11548 	struct i40e_pf *pf = vsi->back;
11549 
11550 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11551 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
11552 	else
11553 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
11554 }
11555 
11556 /**
11557  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
11558  * @pf: Pointer to board private structure
11559  * @lut: Lookup table
11560  * @rss_table_size: Lookup table size
11561  * @rss_size: Range of queue number for hashing
11562  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)11563 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
11564 		       u16 rss_table_size, u16 rss_size)
11565 {
11566 	u16 i;
11567 
11568 	for (i = 0; i < rss_table_size; i++)
11569 		lut[i] = i % rss_size;
11570 }
11571 
11572 /**
11573  * i40e_pf_config_rss - Prepare for RSS if used
11574  * @pf: board private structure
11575  **/
i40e_pf_config_rss(struct i40e_pf * pf)11576 static int i40e_pf_config_rss(struct i40e_pf *pf)
11577 {
11578 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11579 	u8 seed[I40E_HKEY_ARRAY_SIZE];
11580 	u8 *lut;
11581 	struct i40e_hw *hw = &pf->hw;
11582 	u32 reg_val;
11583 	u64 hena;
11584 	int ret;
11585 
11586 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
11587 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
11588 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
11589 	hena |= i40e_pf_get_default_rss_hena(pf);
11590 
11591 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
11592 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
11593 
11594 	/* Determine the RSS table size based on the hardware capabilities */
11595 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
11596 	reg_val = (pf->rss_table_size == 512) ?
11597 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
11598 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
11599 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
11600 
11601 	/* Determine the RSS size of the VSI */
11602 	if (!vsi->rss_size) {
11603 		u16 qcount;
11604 		/* If the firmware does something weird during VSI init, we
11605 		 * could end up with zero TCs. Check for that to avoid
11606 		 * divide-by-zero. It probably won't pass traffic, but it also
11607 		 * won't panic.
11608 		 */
11609 		qcount = vsi->num_queue_pairs /
11610 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
11611 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11612 	}
11613 	if (!vsi->rss_size)
11614 		return -EINVAL;
11615 
11616 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
11617 	if (!lut)
11618 		return -ENOMEM;
11619 
11620 	/* Use user configured lut if there is one, otherwise use default */
11621 	if (vsi->rss_lut_user)
11622 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
11623 	else
11624 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
11625 
11626 	/* Use user configured hash key if there is one, otherwise
11627 	 * use default.
11628 	 */
11629 	if (vsi->rss_hkey_user)
11630 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
11631 	else
11632 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
11633 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
11634 	kfree(lut);
11635 
11636 	return ret;
11637 }
11638 
11639 /**
11640  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
11641  * @pf: board private structure
11642  * @queue_count: the requested queue count for rss.
11643  *
11644  * returns 0 if rss is not enabled, if enabled returns the final rss queue
11645  * count which may be different from the requested queue count.
11646  * Note: expects to be called while under rtnl_lock()
11647  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)11648 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
11649 {
11650 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11651 	int new_rss_size;
11652 
11653 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
11654 		return 0;
11655 
11656 	queue_count = min_t(int, queue_count, num_online_cpus());
11657 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
11658 
11659 	if (queue_count != vsi->num_queue_pairs) {
11660 		u16 qcount;
11661 
11662 		vsi->req_queue_pairs = queue_count;
11663 		i40e_prep_for_reset(pf, true);
11664 
11665 		pf->alloc_rss_size = new_rss_size;
11666 
11667 		i40e_reset_and_rebuild(pf, true, true);
11668 
11669 		/* Discard the user configured hash keys and lut, if less
11670 		 * queues are enabled.
11671 		 */
11672 		if (queue_count < vsi->rss_size) {
11673 			i40e_clear_rss_config_user(vsi);
11674 			dev_dbg(&pf->pdev->dev,
11675 				"discard user configured hash keys and lut\n");
11676 		}
11677 
11678 		/* Reset vsi->rss_size, as number of enabled queues changed */
11679 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11680 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11681 
11682 		i40e_pf_config_rss(pf);
11683 	}
11684 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11685 		 vsi->req_queue_pairs, pf->rss_size_max);
11686 	return pf->alloc_rss_size;
11687 }
11688 
11689 /**
11690  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11691  * @pf: board private structure
11692  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)11693 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11694 {
11695 	i40e_status status;
11696 	bool min_valid, max_valid;
11697 	u32 max_bw, min_bw;
11698 
11699 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11700 					   &min_valid, &max_valid);
11701 
11702 	if (!status) {
11703 		if (min_valid)
11704 			pf->min_bw = min_bw;
11705 		if (max_valid)
11706 			pf->max_bw = max_bw;
11707 	}
11708 
11709 	return status;
11710 }
11711 
11712 /**
11713  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11714  * @pf: board private structure
11715  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)11716 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11717 {
11718 	struct i40e_aqc_configure_partition_bw_data bw_data;
11719 	i40e_status status;
11720 
11721 	memset(&bw_data, 0, sizeof(bw_data));
11722 
11723 	/* Set the valid bit for this PF */
11724 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11725 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11726 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11727 
11728 	/* Set the new bandwidths */
11729 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11730 
11731 	return status;
11732 }
11733 
11734 /**
11735  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11736  * @pf: board private structure
11737  **/
i40e_commit_partition_bw_setting(struct i40e_pf * pf)11738 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11739 {
11740 	/* Commit temporary BW setting to permanent NVM image */
11741 	enum i40e_admin_queue_err last_aq_status;
11742 	i40e_status ret;
11743 	u16 nvm_word;
11744 
11745 	if (pf->hw.partition_id != 1) {
11746 		dev_info(&pf->pdev->dev,
11747 			 "Commit BW only works on partition 1! This is partition %d",
11748 			 pf->hw.partition_id);
11749 		ret = I40E_NOT_SUPPORTED;
11750 		goto bw_commit_out;
11751 	}
11752 
11753 	/* Acquire NVM for read access */
11754 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11755 	last_aq_status = pf->hw.aq.asq_last_status;
11756 	if (ret) {
11757 		dev_info(&pf->pdev->dev,
11758 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11759 			 i40e_stat_str(&pf->hw, ret),
11760 			 i40e_aq_str(&pf->hw, last_aq_status));
11761 		goto bw_commit_out;
11762 	}
11763 
11764 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11765 	ret = i40e_aq_read_nvm(&pf->hw,
11766 			       I40E_SR_NVM_CONTROL_WORD,
11767 			       0x10, sizeof(nvm_word), &nvm_word,
11768 			       false, NULL);
11769 	/* Save off last admin queue command status before releasing
11770 	 * the NVM
11771 	 */
11772 	last_aq_status = pf->hw.aq.asq_last_status;
11773 	i40e_release_nvm(&pf->hw);
11774 	if (ret) {
11775 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11776 			 i40e_stat_str(&pf->hw, ret),
11777 			 i40e_aq_str(&pf->hw, last_aq_status));
11778 		goto bw_commit_out;
11779 	}
11780 
11781 	/* Wait a bit for NVM release to complete */
11782 	msleep(50);
11783 
11784 	/* Acquire NVM for write access */
11785 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11786 	last_aq_status = pf->hw.aq.asq_last_status;
11787 	if (ret) {
11788 		dev_info(&pf->pdev->dev,
11789 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11790 			 i40e_stat_str(&pf->hw, ret),
11791 			 i40e_aq_str(&pf->hw, last_aq_status));
11792 		goto bw_commit_out;
11793 	}
11794 	/* Write it back out unchanged to initiate update NVM,
11795 	 * which will force a write of the shadow (alt) RAM to
11796 	 * the NVM - thus storing the bandwidth values permanently.
11797 	 */
11798 	ret = i40e_aq_update_nvm(&pf->hw,
11799 				 I40E_SR_NVM_CONTROL_WORD,
11800 				 0x10, sizeof(nvm_word),
11801 				 &nvm_word, true, 0, NULL);
11802 	/* Save off last admin queue command status before releasing
11803 	 * the NVM
11804 	 */
11805 	last_aq_status = pf->hw.aq.asq_last_status;
11806 	i40e_release_nvm(&pf->hw);
11807 	if (ret)
11808 		dev_info(&pf->pdev->dev,
11809 			 "BW settings NOT SAVED, err %s aq_err %s\n",
11810 			 i40e_stat_str(&pf->hw, ret),
11811 			 i40e_aq_str(&pf->hw, last_aq_status));
11812 bw_commit_out:
11813 
11814 	return ret;
11815 }
11816 
11817 /**
11818  * i40e_is_total_port_shutdown_enabled - read NVM and return value
11819  * if total port shutdown feature is enabled for this PF
11820  * @pf: board private structure
11821  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)11822 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
11823 {
11824 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
11825 #define I40E_FEATURES_ENABLE_PTR		0x2A
11826 #define I40E_CURRENT_SETTING_PTR		0x2B
11827 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
11828 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
11829 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
11830 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
11831 	i40e_status read_status = I40E_SUCCESS;
11832 	u16 sr_emp_sr_settings_ptr = 0;
11833 	u16 features_enable = 0;
11834 	u16 link_behavior = 0;
11835 	bool ret = false;
11836 
11837 	read_status = i40e_read_nvm_word(&pf->hw,
11838 					 I40E_SR_EMP_SR_SETTINGS_PTR,
11839 					 &sr_emp_sr_settings_ptr);
11840 	if (read_status)
11841 		goto err_nvm;
11842 	read_status = i40e_read_nvm_word(&pf->hw,
11843 					 sr_emp_sr_settings_ptr +
11844 					 I40E_FEATURES_ENABLE_PTR,
11845 					 &features_enable);
11846 	if (read_status)
11847 		goto err_nvm;
11848 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
11849 		read_status = i40e_read_nvm_module_data(&pf->hw,
11850 							I40E_SR_EMP_SR_SETTINGS_PTR,
11851 							I40E_CURRENT_SETTING_PTR,
11852 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
11853 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
11854 							&link_behavior);
11855 		if (read_status)
11856 			goto err_nvm;
11857 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
11858 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
11859 	}
11860 	return ret;
11861 
11862 err_nvm:
11863 	dev_warn(&pf->pdev->dev,
11864 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
11865 		 i40e_stat_str(&pf->hw, read_status));
11866 	return ret;
11867 }
11868 
11869 /**
11870  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11871  * @pf: board private structure to initialize
11872  *
11873  * i40e_sw_init initializes the Adapter private data structure.
11874  * Fields are initialized based on PCI device information and
11875  * OS network device settings (MTU size).
11876  **/
i40e_sw_init(struct i40e_pf * pf)11877 static int i40e_sw_init(struct i40e_pf *pf)
11878 {
11879 	int err = 0;
11880 	int size;
11881 	u16 pow;
11882 
11883 	/* Set default capability flags */
11884 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11885 		    I40E_FLAG_MSI_ENABLED     |
11886 		    I40E_FLAG_MSIX_ENABLED;
11887 
11888 	/* Set default ITR */
11889 	pf->rx_itr_default = I40E_ITR_RX_DEF;
11890 	pf->tx_itr_default = I40E_ITR_TX_DEF;
11891 
11892 	/* Depending on PF configurations, it is possible that the RSS
11893 	 * maximum might end up larger than the available queues
11894 	 */
11895 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11896 	pf->alloc_rss_size = 1;
11897 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11898 	pf->rss_size_max = min_t(int, pf->rss_size_max,
11899 				 pf->hw.func_caps.num_tx_qp);
11900 
11901 	/* find the next higher power-of-2 of num cpus */
11902 	pow = roundup_pow_of_two(num_online_cpus());
11903 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
11904 
11905 	if (pf->hw.func_caps.rss) {
11906 		pf->flags |= I40E_FLAG_RSS_ENABLED;
11907 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11908 					   num_online_cpus());
11909 	}
11910 
11911 	/* MFP mode enabled */
11912 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11913 		pf->flags |= I40E_FLAG_MFP_ENABLED;
11914 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11915 		if (i40e_get_partition_bw_setting(pf)) {
11916 			dev_warn(&pf->pdev->dev,
11917 				 "Could not get partition bw settings\n");
11918 		} else {
11919 			dev_info(&pf->pdev->dev,
11920 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
11921 				 pf->min_bw, pf->max_bw);
11922 
11923 			/* nudge the Tx scheduler */
11924 			i40e_set_partition_bw_setting(pf);
11925 		}
11926 	}
11927 
11928 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11929 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11930 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11931 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11932 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11933 		    pf->hw.num_partitions > 1)
11934 			dev_info(&pf->pdev->dev,
11935 				 "Flow Director Sideband mode Disabled in MFP mode\n");
11936 		else
11937 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11938 		pf->fdir_pf_filter_count =
11939 				 pf->hw.func_caps.fd_filters_guaranteed;
11940 		pf->hw.fdir_shared_filter_count =
11941 				 pf->hw.func_caps.fd_filters_best_effort;
11942 	}
11943 
11944 	if (pf->hw.mac.type == I40E_MAC_X722) {
11945 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11946 				    I40E_HW_128_QP_RSS_CAPABLE |
11947 				    I40E_HW_ATR_EVICT_CAPABLE |
11948 				    I40E_HW_WB_ON_ITR_CAPABLE |
11949 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11950 				    I40E_HW_NO_PCI_LINK_CHECK |
11951 				    I40E_HW_USE_SET_LLDP_MIB |
11952 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11953 				    I40E_HW_PTP_L4_CAPABLE |
11954 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11955 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11956 
11957 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11958 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11959 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
11960 			dev_warn(&pf->pdev->dev,
11961 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11962 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11963 		}
11964 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
11965 		   ((pf->hw.aq.api_maj_ver == 1) &&
11966 		    (pf->hw.aq.api_min_ver > 4))) {
11967 		/* Supported in FW API version higher than 1.4 */
11968 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11969 	}
11970 
11971 	/* Enable HW ATR eviction if possible */
11972 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11973 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11974 
11975 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11976 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11977 	    (pf->hw.aq.fw_maj_ver < 4))) {
11978 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11979 		/* No DCB support  for FW < v4.33 */
11980 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11981 	}
11982 
11983 	/* Disable FW LLDP if FW < v4.3 */
11984 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11985 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11986 	    (pf->hw.aq.fw_maj_ver < 4)))
11987 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11988 
11989 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
11990 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11991 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11992 	    (pf->hw.aq.fw_maj_ver >= 5)))
11993 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11994 
11995 	/* Enable PTP L4 if FW > v6.0 */
11996 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11997 	    pf->hw.aq.fw_maj_ver >= 6)
11998 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11999 
12000 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12001 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12002 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12003 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12004 	}
12005 
12006 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12007 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12008 		/* IWARP needs one extra vector for CQP just like MISC.*/
12009 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12010 	}
12011 	/* Stopping FW LLDP engine is supported on XL710 and X722
12012 	 * starting from FW versions determined in i40e_init_adminq.
12013 	 * Stopping the FW LLDP engine is not supported on XL710
12014 	 * if NPAR is functioning so unset this hw flag in this case.
12015 	 */
12016 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12017 	    pf->hw.func_caps.npar_enable &&
12018 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12019 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12020 
12021 #ifdef CONFIG_PCI_IOV
12022 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12023 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12024 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12025 		pf->num_req_vfs = min_t(int,
12026 					pf->hw.func_caps.num_vfs,
12027 					I40E_MAX_VF_COUNT);
12028 	}
12029 #endif /* CONFIG_PCI_IOV */
12030 	pf->eeprom_version = 0xDEAD;
12031 	pf->lan_veb = I40E_NO_VEB;
12032 	pf->lan_vsi = I40E_NO_VSI;
12033 
12034 	/* By default FW has this off for performance reasons */
12035 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12036 
12037 	/* set up queue assignment tracking */
12038 	size = sizeof(struct i40e_lump_tracking)
12039 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12040 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12041 	if (!pf->qp_pile) {
12042 		err = -ENOMEM;
12043 		goto sw_init_done;
12044 	}
12045 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12046 	pf->qp_pile->search_hint = 0;
12047 
12048 	pf->tx_timeout_recovery_level = 1;
12049 
12050 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12051 	    i40e_is_total_port_shutdown_enabled(pf)) {
12052 		/* Link down on close must be on when total port shutdown
12053 		 * is enabled for a given port
12054 		 */
12055 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12056 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12057 		dev_info(&pf->pdev->dev,
12058 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12059 	}
12060 	mutex_init(&pf->switch_mutex);
12061 
12062 sw_init_done:
12063 	return err;
12064 }
12065 
12066 /**
12067  * i40e_set_ntuple - set the ntuple feature flag and take action
12068  * @pf: board private structure to initialize
12069  * @features: the feature set that the stack is suggesting
12070  *
12071  * returns a bool to indicate if reset needs to happen
12072  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12073 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12074 {
12075 	bool need_reset = false;
12076 
12077 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12078 	 * the state changed, we need to reset.
12079 	 */
12080 	if (features & NETIF_F_NTUPLE) {
12081 		/* Enable filters and mark for reset */
12082 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12083 			need_reset = true;
12084 		/* enable FD_SB only if there is MSI-X vector and no cloud
12085 		 * filters exist
12086 		 */
12087 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12088 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12089 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12090 		}
12091 	} else {
12092 		/* turn off filters, mark for reset and clear SW filter list */
12093 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12094 			need_reset = true;
12095 			i40e_fdir_filter_exit(pf);
12096 		}
12097 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12098 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12099 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12100 
12101 		/* reset fd counters */
12102 		pf->fd_add_err = 0;
12103 		pf->fd_atr_cnt = 0;
12104 		/* if ATR was auto disabled it can be re-enabled. */
12105 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12106 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12107 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12108 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12109 	}
12110 	return need_reset;
12111 }
12112 
12113 /**
12114  * i40e_clear_rss_lut - clear the rx hash lookup table
12115  * @vsi: the VSI being configured
12116  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12117 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12118 {
12119 	struct i40e_pf *pf = vsi->back;
12120 	struct i40e_hw *hw = &pf->hw;
12121 	u16 vf_id = vsi->vf_id;
12122 	u8 i;
12123 
12124 	if (vsi->type == I40E_VSI_MAIN) {
12125 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12126 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12127 	} else if (vsi->type == I40E_VSI_SRIOV) {
12128 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12129 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12130 	} else {
12131 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12132 	}
12133 }
12134 
12135 /**
12136  * i40e_set_features - set the netdev feature flags
12137  * @netdev: ptr to the netdev being adjusted
12138  * @features: the feature set that the stack is suggesting
12139  * Note: expects to be called while under rtnl_lock()
12140  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)12141 static int i40e_set_features(struct net_device *netdev,
12142 			     netdev_features_t features)
12143 {
12144 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12145 	struct i40e_vsi *vsi = np->vsi;
12146 	struct i40e_pf *pf = vsi->back;
12147 	bool need_reset;
12148 
12149 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12150 		i40e_pf_config_rss(pf);
12151 	else if (!(features & NETIF_F_RXHASH) &&
12152 		 netdev->features & NETIF_F_RXHASH)
12153 		i40e_clear_rss_lut(vsi);
12154 
12155 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12156 		i40e_vlan_stripping_enable(vsi);
12157 	else
12158 		i40e_vlan_stripping_disable(vsi);
12159 
12160 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12161 		dev_err(&pf->pdev->dev,
12162 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12163 		return -EINVAL;
12164 	}
12165 
12166 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12167 		i40e_del_all_macvlans(vsi);
12168 
12169 	need_reset = i40e_set_ntuple(pf, features);
12170 
12171 	if (need_reset)
12172 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12173 
12174 	return 0;
12175 }
12176 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12177 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12178 				    unsigned int table, unsigned int idx,
12179 				    struct udp_tunnel_info *ti)
12180 {
12181 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12182 	struct i40e_hw *hw = &np->vsi->back->hw;
12183 	u8 type, filter_index;
12184 	i40e_status ret;
12185 
12186 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12187 						   I40E_AQC_TUNNEL_TYPE_NGE;
12188 
12189 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12190 				     NULL);
12191 	if (ret) {
12192 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12193 			    i40e_stat_str(hw, ret),
12194 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12195 		return -EIO;
12196 	}
12197 
12198 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12199 	return 0;
12200 }
12201 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12202 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12203 				      unsigned int table, unsigned int idx,
12204 				      struct udp_tunnel_info *ti)
12205 {
12206 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12207 	struct i40e_hw *hw = &np->vsi->back->hw;
12208 	i40e_status ret;
12209 
12210 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12211 	if (ret) {
12212 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12213 			    i40e_stat_str(hw, ret),
12214 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12215 		return -EIO;
12216 	}
12217 
12218 	return 0;
12219 }
12220 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)12221 static int i40e_get_phys_port_id(struct net_device *netdev,
12222 				 struct netdev_phys_item_id *ppid)
12223 {
12224 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12225 	struct i40e_pf *pf = np->vsi->back;
12226 	struct i40e_hw *hw = &pf->hw;
12227 
12228 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12229 		return -EOPNOTSUPP;
12230 
12231 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12232 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12233 
12234 	return 0;
12235 }
12236 
12237 /**
12238  * i40e_ndo_fdb_add - add an entry to the hardware database
12239  * @ndm: the input from the stack
12240  * @tb: pointer to array of nladdr (unused)
12241  * @dev: the net device pointer
12242  * @addr: the MAC address entry being added
12243  * @vid: VLAN ID
12244  * @flags: instructions from stack about fdb operation
12245  * @extack: netlink extended ack, unused currently
12246  */
i40e_ndo_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags,struct netlink_ext_ack * extack)12247 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12248 			    struct net_device *dev,
12249 			    const unsigned char *addr, u16 vid,
12250 			    u16 flags,
12251 			    struct netlink_ext_ack *extack)
12252 {
12253 	struct i40e_netdev_priv *np = netdev_priv(dev);
12254 	struct i40e_pf *pf = np->vsi->back;
12255 	int err = 0;
12256 
12257 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12258 		return -EOPNOTSUPP;
12259 
12260 	if (vid) {
12261 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12262 		return -EINVAL;
12263 	}
12264 
12265 	/* Hardware does not support aging addresses so if a
12266 	 * ndm_state is given only allow permanent addresses
12267 	 */
12268 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12269 		netdev_info(dev, "FDB only supports static addresses\n");
12270 		return -EINVAL;
12271 	}
12272 
12273 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12274 		err = dev_uc_add_excl(dev, addr);
12275 	else if (is_multicast_ether_addr(addr))
12276 		err = dev_mc_add_excl(dev, addr);
12277 	else
12278 		err = -EINVAL;
12279 
12280 	/* Only return duplicate errors if NLM_F_EXCL is set */
12281 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12282 		err = 0;
12283 
12284 	return err;
12285 }
12286 
12287 /**
12288  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12289  * @dev: the netdev being configured
12290  * @nlh: RTNL message
12291  * @flags: bridge flags
12292  * @extack: netlink extended ack
12293  *
12294  * Inserts a new hardware bridge if not already created and
12295  * enables the bridging mode requested (VEB or VEPA). If the
12296  * hardware bridge has already been inserted and the request
12297  * is to change the mode then that requires a PF reset to
12298  * allow rebuild of the components with required hardware
12299  * bridge mode enabled.
12300  *
12301  * Note: expects to be called while under rtnl_lock()
12302  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)12303 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12304 				   struct nlmsghdr *nlh,
12305 				   u16 flags,
12306 				   struct netlink_ext_ack *extack)
12307 {
12308 	struct i40e_netdev_priv *np = netdev_priv(dev);
12309 	struct i40e_vsi *vsi = np->vsi;
12310 	struct i40e_pf *pf = vsi->back;
12311 	struct i40e_veb *veb = NULL;
12312 	struct nlattr *attr, *br_spec;
12313 	int i, rem;
12314 
12315 	/* Only for PF VSI for now */
12316 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12317 		return -EOPNOTSUPP;
12318 
12319 	/* Find the HW bridge for PF VSI */
12320 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12321 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12322 			veb = pf->veb[i];
12323 	}
12324 
12325 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12326 
12327 	nla_for_each_nested(attr, br_spec, rem) {
12328 		__u16 mode;
12329 
12330 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12331 			continue;
12332 
12333 		mode = nla_get_u16(attr);
12334 		if ((mode != BRIDGE_MODE_VEPA) &&
12335 		    (mode != BRIDGE_MODE_VEB))
12336 			return -EINVAL;
12337 
12338 		/* Insert a new HW bridge */
12339 		if (!veb) {
12340 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12341 					     vsi->tc_config.enabled_tc);
12342 			if (veb) {
12343 				veb->bridge_mode = mode;
12344 				i40e_config_bridge_mode(veb);
12345 			} else {
12346 				/* No Bridge HW offload available */
12347 				return -ENOENT;
12348 			}
12349 			break;
12350 		} else if (mode != veb->bridge_mode) {
12351 			/* Existing HW bridge but different mode needs reset */
12352 			veb->bridge_mode = mode;
12353 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12354 			if (mode == BRIDGE_MODE_VEB)
12355 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12356 			else
12357 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12358 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12359 			break;
12360 		}
12361 	}
12362 
12363 	return 0;
12364 }
12365 
12366 /**
12367  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12368  * @skb: skb buff
12369  * @pid: process id
12370  * @seq: RTNL message seq #
12371  * @dev: the netdev being configured
12372  * @filter_mask: unused
12373  * @nlflags: netlink flags passed in
12374  *
12375  * Return the mode in which the hardware bridge is operating in
12376  * i.e VEB or VEPA.
12377  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)12378 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12379 				   struct net_device *dev,
12380 				   u32 __always_unused filter_mask,
12381 				   int nlflags)
12382 {
12383 	struct i40e_netdev_priv *np = netdev_priv(dev);
12384 	struct i40e_vsi *vsi = np->vsi;
12385 	struct i40e_pf *pf = vsi->back;
12386 	struct i40e_veb *veb = NULL;
12387 	int i;
12388 
12389 	/* Only for PF VSI for now */
12390 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12391 		return -EOPNOTSUPP;
12392 
12393 	/* Find the HW bridge for the PF VSI */
12394 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12395 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12396 			veb = pf->veb[i];
12397 	}
12398 
12399 	if (!veb)
12400 		return 0;
12401 
12402 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12403 				       0, 0, nlflags, filter_mask, NULL);
12404 }
12405 
12406 /**
12407  * i40e_features_check - Validate encapsulated packet conforms to limits
12408  * @skb: skb buff
12409  * @dev: This physical port's netdev
12410  * @features: Offload features that the stack believes apply
12411  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)12412 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12413 					     struct net_device *dev,
12414 					     netdev_features_t features)
12415 {
12416 	size_t len;
12417 
12418 	/* No point in doing any of this if neither checksum nor GSO are
12419 	 * being requested for this frame.  We can rule out both by just
12420 	 * checking for CHECKSUM_PARTIAL
12421 	 */
12422 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12423 		return features;
12424 
12425 	/* We cannot support GSO if the MSS is going to be less than
12426 	 * 64 bytes.  If it is then we need to drop support for GSO.
12427 	 */
12428 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12429 		features &= ~NETIF_F_GSO_MASK;
12430 
12431 	/* MACLEN can support at most 63 words */
12432 	len = skb_network_header(skb) - skb->data;
12433 	if (len & ~(63 * 2))
12434 		goto out_err;
12435 
12436 	/* IPLEN and EIPLEN can support at most 127 dwords */
12437 	len = skb_transport_header(skb) - skb_network_header(skb);
12438 	if (len & ~(127 * 4))
12439 		goto out_err;
12440 
12441 	if (skb->encapsulation) {
12442 		/* L4TUNLEN can support 127 words */
12443 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12444 		if (len & ~(127 * 2))
12445 			goto out_err;
12446 
12447 		/* IPLEN can support at most 127 dwords */
12448 		len = skb_inner_transport_header(skb) -
12449 		      skb_inner_network_header(skb);
12450 		if (len & ~(127 * 4))
12451 			goto out_err;
12452 	}
12453 
12454 	/* No need to validate L4LEN as TCP is the only protocol with a
12455 	 * a flexible value and we support all possible values supported
12456 	 * by TCP, which is at most 15 dwords
12457 	 */
12458 
12459 	return features;
12460 out_err:
12461 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12462 }
12463 
12464 /**
12465  * i40e_xdp_setup - add/remove an XDP program
12466  * @vsi: VSI to changed
12467  * @prog: XDP program
12468  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog)12469 static int i40e_xdp_setup(struct i40e_vsi *vsi,
12470 			  struct bpf_prog *prog)
12471 {
12472 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
12473 	struct i40e_pf *pf = vsi->back;
12474 	struct bpf_prog *old_prog;
12475 	bool need_reset;
12476 	int i;
12477 
12478 	/* Don't allow frames that span over multiple buffers */
12479 	if (frame_size > vsi->rx_buf_len)
12480 		return -EINVAL;
12481 
12482 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
12483 		return 0;
12484 
12485 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12486 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12487 
12488 	if (need_reset)
12489 		i40e_prep_for_reset(pf, true);
12490 
12491 	old_prog = xchg(&vsi->xdp_prog, prog);
12492 
12493 	if (need_reset) {
12494 		if (!prog)
12495 			/* Wait until ndo_xsk_wakeup completes. */
12496 			synchronize_rcu();
12497 		i40e_reset_and_rebuild(pf, true, true);
12498 	}
12499 
12500 	for (i = 0; i < vsi->num_queue_pairs; i++)
12501 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12502 
12503 	if (old_prog)
12504 		bpf_prog_put(old_prog);
12505 
12506 	/* Kick start the NAPI context if there is an AF_XDP socket open
12507 	 * on that queue id. This so that receiving will start.
12508 	 */
12509 	if (need_reset && prog)
12510 		for (i = 0; i < vsi->num_queue_pairs; i++)
12511 			if (vsi->xdp_rings[i]->xsk_pool)
12512 				(void)i40e_xsk_wakeup(vsi->netdev, i,
12513 						      XDP_WAKEUP_RX);
12514 
12515 	return 0;
12516 }
12517 
12518 /**
12519  * i40e_enter_busy_conf - Enters busy config state
12520  * @vsi: vsi
12521  *
12522  * Returns 0 on success, <0 for failure.
12523  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)12524 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
12525 {
12526 	struct i40e_pf *pf = vsi->back;
12527 	int timeout = 50;
12528 
12529 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
12530 		timeout--;
12531 		if (!timeout)
12532 			return -EBUSY;
12533 		usleep_range(1000, 2000);
12534 	}
12535 
12536 	return 0;
12537 }
12538 
12539 /**
12540  * i40e_exit_busy_conf - Exits busy config state
12541  * @vsi: vsi
12542  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)12543 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
12544 {
12545 	struct i40e_pf *pf = vsi->back;
12546 
12547 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
12548 }
12549 
12550 /**
12551  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
12552  * @vsi: vsi
12553  * @queue_pair: queue pair
12554  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)12555 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
12556 {
12557 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
12558 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
12559 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
12560 	       sizeof(vsi->tx_rings[queue_pair]->stats));
12561 	if (i40e_enabled_xdp_vsi(vsi)) {
12562 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
12563 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
12564 	}
12565 }
12566 
12567 /**
12568  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
12569  * @vsi: vsi
12570  * @queue_pair: queue pair
12571  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)12572 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
12573 {
12574 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
12575 	if (i40e_enabled_xdp_vsi(vsi)) {
12576 		/* Make sure that in-progress ndo_xdp_xmit calls are
12577 		 * completed.
12578 		 */
12579 		synchronize_rcu();
12580 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
12581 	}
12582 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
12583 }
12584 
12585 /**
12586  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
12587  * @vsi: vsi
12588  * @queue_pair: queue pair
12589  * @enable: true for enable, false for disable
12590  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)12591 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
12592 					bool enable)
12593 {
12594 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12595 	struct i40e_q_vector *q_vector = rxr->q_vector;
12596 
12597 	if (!vsi->netdev)
12598 		return;
12599 
12600 	/* All rings in a qp belong to the same qvector. */
12601 	if (q_vector->rx.ring || q_vector->tx.ring) {
12602 		if (enable)
12603 			napi_enable(&q_vector->napi);
12604 		else
12605 			napi_disable(&q_vector->napi);
12606 	}
12607 }
12608 
12609 /**
12610  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
12611  * @vsi: vsi
12612  * @queue_pair: queue pair
12613  * @enable: true for enable, false for disable
12614  *
12615  * Returns 0 on success, <0 on failure.
12616  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)12617 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
12618 					bool enable)
12619 {
12620 	struct i40e_pf *pf = vsi->back;
12621 	int pf_q, ret = 0;
12622 
12623 	pf_q = vsi->base_queue + queue_pair;
12624 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
12625 				     false /*is xdp*/, enable);
12626 	if (ret) {
12627 		dev_info(&pf->pdev->dev,
12628 			 "VSI seid %d Tx ring %d %sable timeout\n",
12629 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12630 		return ret;
12631 	}
12632 
12633 	i40e_control_rx_q(pf, pf_q, enable);
12634 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
12635 	if (ret) {
12636 		dev_info(&pf->pdev->dev,
12637 			 "VSI seid %d Rx ring %d %sable timeout\n",
12638 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12639 		return ret;
12640 	}
12641 
12642 	/* Due to HW errata, on Rx disable only, the register can
12643 	 * indicate done before it really is. Needs 50ms to be sure
12644 	 */
12645 	if (!enable)
12646 		mdelay(50);
12647 
12648 	if (!i40e_enabled_xdp_vsi(vsi))
12649 		return ret;
12650 
12651 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
12652 				     pf_q + vsi->alloc_queue_pairs,
12653 				     true /*is xdp*/, enable);
12654 	if (ret) {
12655 		dev_info(&pf->pdev->dev,
12656 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
12657 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12658 	}
12659 
12660 	return ret;
12661 }
12662 
12663 /**
12664  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
12665  * @vsi: vsi
12666  * @queue_pair: queue_pair
12667  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)12668 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
12669 {
12670 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12671 	struct i40e_pf *pf = vsi->back;
12672 	struct i40e_hw *hw = &pf->hw;
12673 
12674 	/* All rings in a qp belong to the same qvector. */
12675 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12676 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
12677 	else
12678 		i40e_irq_dynamic_enable_icr0(pf);
12679 
12680 	i40e_flush(hw);
12681 }
12682 
12683 /**
12684  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
12685  * @vsi: vsi
12686  * @queue_pair: queue_pair
12687  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)12688 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
12689 {
12690 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12691 	struct i40e_pf *pf = vsi->back;
12692 	struct i40e_hw *hw = &pf->hw;
12693 
12694 	/* For simplicity, instead of removing the qp interrupt causes
12695 	 * from the interrupt linked list, we simply disable the interrupt, and
12696 	 * leave the list intact.
12697 	 *
12698 	 * All rings in a qp belong to the same qvector.
12699 	 */
12700 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12701 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
12702 
12703 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
12704 		i40e_flush(hw);
12705 		synchronize_irq(pf->msix_entries[intpf].vector);
12706 	} else {
12707 		/* Legacy and MSI mode - this stops all interrupt handling */
12708 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
12709 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
12710 		i40e_flush(hw);
12711 		synchronize_irq(pf->pdev->irq);
12712 	}
12713 }
12714 
12715 /**
12716  * i40e_queue_pair_disable - Disables a queue pair
12717  * @vsi: vsi
12718  * @queue_pair: queue pair
12719  *
12720  * Returns 0 on success, <0 on failure.
12721  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)12722 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
12723 {
12724 	int err;
12725 
12726 	err = i40e_enter_busy_conf(vsi);
12727 	if (err)
12728 		return err;
12729 
12730 	i40e_queue_pair_disable_irq(vsi, queue_pair);
12731 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
12732 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
12733 	i40e_queue_pair_clean_rings(vsi, queue_pair);
12734 	i40e_queue_pair_reset_stats(vsi, queue_pair);
12735 
12736 	return err;
12737 }
12738 
12739 /**
12740  * i40e_queue_pair_enable - Enables a queue pair
12741  * @vsi: vsi
12742  * @queue_pair: queue pair
12743  *
12744  * Returns 0 on success, <0 on failure.
12745  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)12746 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
12747 {
12748 	int err;
12749 
12750 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
12751 	if (err)
12752 		return err;
12753 
12754 	if (i40e_enabled_xdp_vsi(vsi)) {
12755 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
12756 		if (err)
12757 			return err;
12758 	}
12759 
12760 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
12761 	if (err)
12762 		return err;
12763 
12764 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
12765 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
12766 	i40e_queue_pair_enable_irq(vsi, queue_pair);
12767 
12768 	i40e_exit_busy_conf(vsi);
12769 
12770 	return err;
12771 }
12772 
12773 /**
12774  * i40e_xdp - implements ndo_bpf for i40e
12775  * @dev: netdevice
12776  * @xdp: XDP command
12777  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)12778 static int i40e_xdp(struct net_device *dev,
12779 		    struct netdev_bpf *xdp)
12780 {
12781 	struct i40e_netdev_priv *np = netdev_priv(dev);
12782 	struct i40e_vsi *vsi = np->vsi;
12783 
12784 	if (vsi->type != I40E_VSI_MAIN)
12785 		return -EINVAL;
12786 
12787 	switch (xdp->command) {
12788 	case XDP_SETUP_PROG:
12789 		return i40e_xdp_setup(vsi, xdp->prog);
12790 	case XDP_SETUP_XSK_POOL:
12791 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
12792 					   xdp->xsk.queue_id);
12793 	default:
12794 		return -EINVAL;
12795 	}
12796 }
12797 
12798 static const struct net_device_ops i40e_netdev_ops = {
12799 	.ndo_open		= i40e_open,
12800 	.ndo_stop		= i40e_close,
12801 	.ndo_start_xmit		= i40e_lan_xmit_frame,
12802 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
12803 	.ndo_set_rx_mode	= i40e_set_rx_mode,
12804 	.ndo_validate_addr	= eth_validate_addr,
12805 	.ndo_set_mac_address	= i40e_set_mac,
12806 	.ndo_change_mtu		= i40e_change_mtu,
12807 	.ndo_do_ioctl		= i40e_ioctl,
12808 	.ndo_tx_timeout		= i40e_tx_timeout,
12809 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
12810 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
12811 #ifdef CONFIG_NET_POLL_CONTROLLER
12812 	.ndo_poll_controller	= i40e_netpoll,
12813 #endif
12814 	.ndo_setup_tc		= __i40e_setup_tc,
12815 	.ndo_select_queue	= i40e_lan_select_queue,
12816 	.ndo_set_features	= i40e_set_features,
12817 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
12818 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
12819 	.ndo_get_vf_stats	= i40e_get_vf_stats,
12820 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
12821 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
12822 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
12823 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
12824 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
12825 	.ndo_udp_tunnel_add	= udp_tunnel_nic_add_port,
12826 	.ndo_udp_tunnel_del	= udp_tunnel_nic_del_port,
12827 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
12828 	.ndo_fdb_add		= i40e_ndo_fdb_add,
12829 	.ndo_features_check	= i40e_features_check,
12830 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
12831 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
12832 	.ndo_bpf		= i40e_xdp,
12833 	.ndo_xdp_xmit		= i40e_xdp_xmit,
12834 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
12835 	.ndo_dfwd_add_station	= i40e_fwd_add,
12836 	.ndo_dfwd_del_station	= i40e_fwd_del,
12837 };
12838 
12839 /**
12840  * i40e_config_netdev - Setup the netdev flags
12841  * @vsi: the VSI being configured
12842  *
12843  * Returns 0 on success, negative value on failure
12844  **/
i40e_config_netdev(struct i40e_vsi * vsi)12845 static int i40e_config_netdev(struct i40e_vsi *vsi)
12846 {
12847 	struct i40e_pf *pf = vsi->back;
12848 	struct i40e_hw *hw = &pf->hw;
12849 	struct i40e_netdev_priv *np;
12850 	struct net_device *netdev;
12851 	u8 broadcast[ETH_ALEN];
12852 	u8 mac_addr[ETH_ALEN];
12853 	int etherdev_size;
12854 	netdev_features_t hw_enc_features;
12855 	netdev_features_t hw_features;
12856 
12857 	etherdev_size = sizeof(struct i40e_netdev_priv);
12858 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
12859 	if (!netdev)
12860 		return -ENOMEM;
12861 
12862 	vsi->netdev = netdev;
12863 	np = netdev_priv(netdev);
12864 	np->vsi = vsi;
12865 
12866 	hw_enc_features = NETIF_F_SG			|
12867 			  NETIF_F_IP_CSUM		|
12868 			  NETIF_F_IPV6_CSUM		|
12869 			  NETIF_F_HIGHDMA		|
12870 			  NETIF_F_SOFT_FEATURES		|
12871 			  NETIF_F_TSO			|
12872 			  NETIF_F_TSO_ECN		|
12873 			  NETIF_F_TSO6			|
12874 			  NETIF_F_GSO_GRE		|
12875 			  NETIF_F_GSO_GRE_CSUM		|
12876 			  NETIF_F_GSO_PARTIAL		|
12877 			  NETIF_F_GSO_IPXIP4		|
12878 			  NETIF_F_GSO_IPXIP6		|
12879 			  NETIF_F_GSO_UDP_TUNNEL	|
12880 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
12881 			  NETIF_F_GSO_UDP_L4		|
12882 			  NETIF_F_SCTP_CRC		|
12883 			  NETIF_F_RXHASH		|
12884 			  NETIF_F_RXCSUM		|
12885 			  0;
12886 
12887 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
12888 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
12889 
12890 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
12891 
12892 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
12893 
12894 	netdev->hw_enc_features |= hw_enc_features;
12895 
12896 	/* record features VLANs can make use of */
12897 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
12898 
12899 	/* enable macvlan offloads */
12900 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
12901 
12902 	hw_features = hw_enc_features		|
12903 		      NETIF_F_HW_VLAN_CTAG_TX	|
12904 		      NETIF_F_HW_VLAN_CTAG_RX;
12905 
12906 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
12907 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
12908 
12909 	netdev->hw_features |= hw_features;
12910 
12911 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
12912 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
12913 
12914 	if (vsi->type == I40E_VSI_MAIN) {
12915 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
12916 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
12917 		/* The following steps are necessary for two reasons. First,
12918 		 * some older NVM configurations load a default MAC-VLAN
12919 		 * filter that will accept any tagged packet, and we want to
12920 		 * replace this with a normal filter. Additionally, it is
12921 		 * possible our MAC address was provided by the platform using
12922 		 * Open Firmware or similar.
12923 		 *
12924 		 * Thus, we need to remove the default filter and install one
12925 		 * specific to the MAC address.
12926 		 */
12927 		i40e_rm_default_mac_filter(vsi, mac_addr);
12928 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12929 		i40e_add_mac_filter(vsi, mac_addr);
12930 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12931 	} else {
12932 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
12933 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
12934 		 * the end, which is 4 bytes long, so force truncation of the
12935 		 * original name by IFNAMSIZ - 4
12936 		 */
12937 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
12938 			 IFNAMSIZ - 4,
12939 			 pf->vsi[pf->lan_vsi]->netdev->name);
12940 		eth_random_addr(mac_addr);
12941 
12942 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12943 		i40e_add_mac_filter(vsi, mac_addr);
12944 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12945 	}
12946 
12947 	/* Add the broadcast filter so that we initially will receive
12948 	 * broadcast packets. Note that when a new VLAN is first added the
12949 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
12950 	 * specific filters as part of transitioning into "vlan" operation.
12951 	 * When more VLANs are added, the driver will copy each existing MAC
12952 	 * filter and add it for the new VLAN.
12953 	 *
12954 	 * Broadcast filters are handled specially by
12955 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
12956 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
12957 	 * filter. The subtask will update the correct broadcast promiscuous
12958 	 * bits as VLANs become active or inactive.
12959 	 */
12960 	eth_broadcast_addr(broadcast);
12961 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12962 	i40e_add_mac_filter(vsi, broadcast);
12963 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12964 
12965 	ether_addr_copy(netdev->dev_addr, mac_addr);
12966 	ether_addr_copy(netdev->perm_addr, mac_addr);
12967 
12968 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
12969 	netdev->neigh_priv_len = sizeof(u32) * 4;
12970 
12971 	netdev->priv_flags |= IFF_UNICAST_FLT;
12972 	netdev->priv_flags |= IFF_SUPP_NOFCS;
12973 	/* Setup netdev TC information */
12974 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
12975 
12976 	netdev->netdev_ops = &i40e_netdev_ops;
12977 	netdev->watchdog_timeo = 5 * HZ;
12978 	i40e_set_ethtool_ops(netdev);
12979 
12980 	/* MTU range: 68 - 9706 */
12981 	netdev->min_mtu = ETH_MIN_MTU;
12982 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
12983 
12984 	return 0;
12985 }
12986 
12987 /**
12988  * i40e_vsi_delete - Delete a VSI from the switch
12989  * @vsi: the VSI being removed
12990  *
12991  * Returns 0 on success, negative value on failure
12992  **/
i40e_vsi_delete(struct i40e_vsi * vsi)12993 static void i40e_vsi_delete(struct i40e_vsi *vsi)
12994 {
12995 	/* remove default VSI is not allowed */
12996 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
12997 		return;
12998 
12999 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13000 }
13001 
13002 /**
13003  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13004  * @vsi: the VSI being queried
13005  *
13006  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13007  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13008 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13009 {
13010 	struct i40e_veb *veb;
13011 	struct i40e_pf *pf = vsi->back;
13012 
13013 	/* Uplink is not a bridge so default to VEB */
13014 	if (vsi->veb_idx >= I40E_MAX_VEB)
13015 		return 1;
13016 
13017 	veb = pf->veb[vsi->veb_idx];
13018 	if (!veb) {
13019 		dev_info(&pf->pdev->dev,
13020 			 "There is no veb associated with the bridge\n");
13021 		return -ENOENT;
13022 	}
13023 
13024 	/* Uplink is a bridge in VEPA mode */
13025 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13026 		return 0;
13027 	} else {
13028 		/* Uplink is a bridge in VEB mode */
13029 		return 1;
13030 	}
13031 
13032 	/* VEPA is now default bridge, so return 0 */
13033 	return 0;
13034 }
13035 
13036 /**
13037  * i40e_add_vsi - Add a VSI to the switch
13038  * @vsi: the VSI being configured
13039  *
13040  * This initializes a VSI context depending on the VSI type to be added and
13041  * passes it down to the add_vsi aq command.
13042  **/
i40e_add_vsi(struct i40e_vsi * vsi)13043 static int i40e_add_vsi(struct i40e_vsi *vsi)
13044 {
13045 	int ret = -ENODEV;
13046 	struct i40e_pf *pf = vsi->back;
13047 	struct i40e_hw *hw = &pf->hw;
13048 	struct i40e_vsi_context ctxt;
13049 	struct i40e_mac_filter *f;
13050 	struct hlist_node *h;
13051 	int bkt;
13052 
13053 	u8 enabled_tc = 0x1; /* TC0 enabled */
13054 	int f_count = 0;
13055 
13056 	memset(&ctxt, 0, sizeof(ctxt));
13057 	switch (vsi->type) {
13058 	case I40E_VSI_MAIN:
13059 		/* The PF's main VSI is already setup as part of the
13060 		 * device initialization, so we'll not bother with
13061 		 * the add_vsi call, but we will retrieve the current
13062 		 * VSI context.
13063 		 */
13064 		ctxt.seid = pf->main_vsi_seid;
13065 		ctxt.pf_num = pf->hw.pf_id;
13066 		ctxt.vf_num = 0;
13067 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13068 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13069 		if (ret) {
13070 			dev_info(&pf->pdev->dev,
13071 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13072 				 i40e_stat_str(&pf->hw, ret),
13073 				 i40e_aq_str(&pf->hw,
13074 					     pf->hw.aq.asq_last_status));
13075 			return -ENOENT;
13076 		}
13077 		vsi->info = ctxt.info;
13078 		vsi->info.valid_sections = 0;
13079 
13080 		vsi->seid = ctxt.seid;
13081 		vsi->id = ctxt.vsi_number;
13082 
13083 		enabled_tc = i40e_pf_get_tc_map(pf);
13084 
13085 		/* Source pruning is enabled by default, so the flag is
13086 		 * negative logic - if it's set, we need to fiddle with
13087 		 * the VSI to disable source pruning.
13088 		 */
13089 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13090 			memset(&ctxt, 0, sizeof(ctxt));
13091 			ctxt.seid = pf->main_vsi_seid;
13092 			ctxt.pf_num = pf->hw.pf_id;
13093 			ctxt.vf_num = 0;
13094 			ctxt.info.valid_sections |=
13095 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13096 			ctxt.info.switch_id =
13097 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13098 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13099 			if (ret) {
13100 				dev_info(&pf->pdev->dev,
13101 					 "update vsi failed, err %s aq_err %s\n",
13102 					 i40e_stat_str(&pf->hw, ret),
13103 					 i40e_aq_str(&pf->hw,
13104 						     pf->hw.aq.asq_last_status));
13105 				ret = -ENOENT;
13106 				goto err;
13107 			}
13108 		}
13109 
13110 		/* MFP mode setup queue map and update VSI */
13111 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13112 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13113 			memset(&ctxt, 0, sizeof(ctxt));
13114 			ctxt.seid = pf->main_vsi_seid;
13115 			ctxt.pf_num = pf->hw.pf_id;
13116 			ctxt.vf_num = 0;
13117 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13118 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13119 			if (ret) {
13120 				dev_info(&pf->pdev->dev,
13121 					 "update vsi failed, err %s aq_err %s\n",
13122 					 i40e_stat_str(&pf->hw, ret),
13123 					 i40e_aq_str(&pf->hw,
13124 						    pf->hw.aq.asq_last_status));
13125 				ret = -ENOENT;
13126 				goto err;
13127 			}
13128 			/* update the local VSI info queue map */
13129 			i40e_vsi_update_queue_map(vsi, &ctxt);
13130 			vsi->info.valid_sections = 0;
13131 		} else {
13132 			/* Default/Main VSI is only enabled for TC0
13133 			 * reconfigure it to enable all TCs that are
13134 			 * available on the port in SFP mode.
13135 			 * For MFP case the iSCSI PF would use this
13136 			 * flow to enable LAN+iSCSI TC.
13137 			 */
13138 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13139 			if (ret) {
13140 				/* Single TC condition is not fatal,
13141 				 * message and continue
13142 				 */
13143 				dev_info(&pf->pdev->dev,
13144 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13145 					 enabled_tc,
13146 					 i40e_stat_str(&pf->hw, ret),
13147 					 i40e_aq_str(&pf->hw,
13148 						    pf->hw.aq.asq_last_status));
13149 			}
13150 		}
13151 		break;
13152 
13153 	case I40E_VSI_FDIR:
13154 		ctxt.pf_num = hw->pf_id;
13155 		ctxt.vf_num = 0;
13156 		ctxt.uplink_seid = vsi->uplink_seid;
13157 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13158 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13159 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13160 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13161 			ctxt.info.valid_sections |=
13162 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13163 			ctxt.info.switch_id =
13164 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13165 		}
13166 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13167 		break;
13168 
13169 	case I40E_VSI_VMDQ2:
13170 		ctxt.pf_num = hw->pf_id;
13171 		ctxt.vf_num = 0;
13172 		ctxt.uplink_seid = vsi->uplink_seid;
13173 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13174 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13175 
13176 		/* This VSI is connected to VEB so the switch_id
13177 		 * should be set to zero by default.
13178 		 */
13179 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13180 			ctxt.info.valid_sections |=
13181 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13182 			ctxt.info.switch_id =
13183 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13184 		}
13185 
13186 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13187 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13188 		break;
13189 
13190 	case I40E_VSI_SRIOV:
13191 		ctxt.pf_num = hw->pf_id;
13192 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13193 		ctxt.uplink_seid = vsi->uplink_seid;
13194 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13195 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13196 
13197 		/* This VSI is connected to VEB so the switch_id
13198 		 * should be set to zero by default.
13199 		 */
13200 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13201 			ctxt.info.valid_sections |=
13202 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13203 			ctxt.info.switch_id =
13204 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13205 		}
13206 
13207 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13208 			ctxt.info.valid_sections |=
13209 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13210 			ctxt.info.queueing_opt_flags |=
13211 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13212 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13213 		}
13214 
13215 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13216 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13217 		if (pf->vf[vsi->vf_id].spoofchk) {
13218 			ctxt.info.valid_sections |=
13219 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13220 			ctxt.info.sec_flags |=
13221 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13222 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13223 		}
13224 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13225 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13226 		break;
13227 
13228 	case I40E_VSI_IWARP:
13229 		/* send down message to iWARP */
13230 		break;
13231 
13232 	default:
13233 		return -ENODEV;
13234 	}
13235 
13236 	if (vsi->type != I40E_VSI_MAIN) {
13237 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13238 		if (ret) {
13239 			dev_info(&vsi->back->pdev->dev,
13240 				 "add vsi failed, err %s aq_err %s\n",
13241 				 i40e_stat_str(&pf->hw, ret),
13242 				 i40e_aq_str(&pf->hw,
13243 					     pf->hw.aq.asq_last_status));
13244 			ret = -ENOENT;
13245 			goto err;
13246 		}
13247 		vsi->info = ctxt.info;
13248 		vsi->info.valid_sections = 0;
13249 		vsi->seid = ctxt.seid;
13250 		vsi->id = ctxt.vsi_number;
13251 	}
13252 
13253 	vsi->active_filters = 0;
13254 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13255 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13256 	/* If macvlan filters already exist, force them to get loaded */
13257 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13258 		f->state = I40E_FILTER_NEW;
13259 		f_count++;
13260 	}
13261 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13262 
13263 	if (f_count) {
13264 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13265 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13266 	}
13267 
13268 	/* Update VSI BW information */
13269 	ret = i40e_vsi_get_bw_info(vsi);
13270 	if (ret) {
13271 		dev_info(&pf->pdev->dev,
13272 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13273 			 i40e_stat_str(&pf->hw, ret),
13274 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13275 		/* VSI is already added so not tearing that up */
13276 		ret = 0;
13277 	}
13278 
13279 err:
13280 	return ret;
13281 }
13282 
13283 /**
13284  * i40e_vsi_release - Delete a VSI and free its resources
13285  * @vsi: the VSI being removed
13286  *
13287  * Returns 0 on success or < 0 on error
13288  **/
i40e_vsi_release(struct i40e_vsi * vsi)13289 int i40e_vsi_release(struct i40e_vsi *vsi)
13290 {
13291 	struct i40e_mac_filter *f;
13292 	struct hlist_node *h;
13293 	struct i40e_veb *veb = NULL;
13294 	struct i40e_pf *pf;
13295 	u16 uplink_seid;
13296 	int i, n, bkt;
13297 
13298 	pf = vsi->back;
13299 
13300 	/* release of a VEB-owner or last VSI is not allowed */
13301 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13302 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13303 			 vsi->seid, vsi->uplink_seid);
13304 		return -ENODEV;
13305 	}
13306 	if (vsi == pf->vsi[pf->lan_vsi] &&
13307 	    !test_bit(__I40E_DOWN, pf->state)) {
13308 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13309 		return -ENODEV;
13310 	}
13311 
13312 	uplink_seid = vsi->uplink_seid;
13313 	if (vsi->type != I40E_VSI_SRIOV) {
13314 		if (vsi->netdev_registered) {
13315 			vsi->netdev_registered = false;
13316 			if (vsi->netdev) {
13317 				/* results in a call to i40e_close() */
13318 				unregister_netdev(vsi->netdev);
13319 			}
13320 		} else {
13321 			i40e_vsi_close(vsi);
13322 		}
13323 		i40e_vsi_disable_irq(vsi);
13324 	}
13325 
13326 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13327 
13328 	/* clear the sync flag on all filters */
13329 	if (vsi->netdev) {
13330 		__dev_uc_unsync(vsi->netdev, NULL);
13331 		__dev_mc_unsync(vsi->netdev, NULL);
13332 	}
13333 
13334 	/* make sure any remaining filters are marked for deletion */
13335 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13336 		__i40e_del_filter(vsi, f);
13337 
13338 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13339 
13340 	i40e_sync_vsi_filters(vsi);
13341 
13342 	i40e_vsi_delete(vsi);
13343 	i40e_vsi_free_q_vectors(vsi);
13344 	if (vsi->netdev) {
13345 		free_netdev(vsi->netdev);
13346 		vsi->netdev = NULL;
13347 	}
13348 	i40e_vsi_clear_rings(vsi);
13349 	i40e_vsi_clear(vsi);
13350 
13351 	/* If this was the last thing on the VEB, except for the
13352 	 * controlling VSI, remove the VEB, which puts the controlling
13353 	 * VSI onto the next level down in the switch.
13354 	 *
13355 	 * Well, okay, there's one more exception here: don't remove
13356 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13357 	 * from up the network stack.
13358 	 */
13359 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13360 		if (pf->vsi[i] &&
13361 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13362 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13363 			n++;      /* count the VSIs */
13364 		}
13365 	}
13366 	for (i = 0; i < I40E_MAX_VEB; i++) {
13367 		if (!pf->veb[i])
13368 			continue;
13369 		if (pf->veb[i]->uplink_seid == uplink_seid)
13370 			n++;     /* count the VEBs */
13371 		if (pf->veb[i]->seid == uplink_seid)
13372 			veb = pf->veb[i];
13373 	}
13374 	if (n == 0 && veb && veb->uplink_seid != 0)
13375 		i40e_veb_release(veb);
13376 
13377 	return 0;
13378 }
13379 
13380 /**
13381  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13382  * @vsi: ptr to the VSI
13383  *
13384  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13385  * corresponding SW VSI structure and initializes num_queue_pairs for the
13386  * newly allocated VSI.
13387  *
13388  * Returns 0 on success or negative on failure
13389  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)13390 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13391 {
13392 	int ret = -ENOENT;
13393 	struct i40e_pf *pf = vsi->back;
13394 
13395 	if (vsi->q_vectors[0]) {
13396 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13397 			 vsi->seid);
13398 		return -EEXIST;
13399 	}
13400 
13401 	if (vsi->base_vector) {
13402 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13403 			 vsi->seid, vsi->base_vector);
13404 		return -EEXIST;
13405 	}
13406 
13407 	ret = i40e_vsi_alloc_q_vectors(vsi);
13408 	if (ret) {
13409 		dev_info(&pf->pdev->dev,
13410 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13411 			 vsi->num_q_vectors, vsi->seid, ret);
13412 		vsi->num_q_vectors = 0;
13413 		goto vector_setup_out;
13414 	}
13415 
13416 	/* In Legacy mode, we do not have to get any other vector since we
13417 	 * piggyback on the misc/ICR0 for queue interrupts.
13418 	*/
13419 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13420 		return ret;
13421 	if (vsi->num_q_vectors)
13422 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13423 						 vsi->num_q_vectors, vsi->idx);
13424 	if (vsi->base_vector < 0) {
13425 		dev_info(&pf->pdev->dev,
13426 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13427 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13428 		i40e_vsi_free_q_vectors(vsi);
13429 		ret = -ENOENT;
13430 		goto vector_setup_out;
13431 	}
13432 
13433 vector_setup_out:
13434 	return ret;
13435 }
13436 
13437 /**
13438  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13439  * @vsi: pointer to the vsi.
13440  *
13441  * This re-allocates a vsi's queue resources.
13442  *
13443  * Returns pointer to the successfully allocated and configured VSI sw struct
13444  * on success, otherwise returns NULL on failure.
13445  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)13446 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13447 {
13448 	u16 alloc_queue_pairs;
13449 	struct i40e_pf *pf;
13450 	u8 enabled_tc;
13451 	int ret;
13452 
13453 	if (!vsi)
13454 		return NULL;
13455 
13456 	pf = vsi->back;
13457 
13458 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
13459 	i40e_vsi_clear_rings(vsi);
13460 
13461 	i40e_vsi_free_arrays(vsi, false);
13462 	i40e_set_num_rings_in_vsi(vsi);
13463 	ret = i40e_vsi_alloc_arrays(vsi, false);
13464 	if (ret)
13465 		goto err_vsi;
13466 
13467 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13468 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13469 
13470 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13471 	if (ret < 0) {
13472 		dev_info(&pf->pdev->dev,
13473 			 "failed to get tracking for %d queues for VSI %d err %d\n",
13474 			 alloc_queue_pairs, vsi->seid, ret);
13475 		goto err_vsi;
13476 	}
13477 	vsi->base_queue = ret;
13478 
13479 	/* Update the FW view of the VSI. Force a reset of TC and queue
13480 	 * layout configurations.
13481 	 */
13482 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13483 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13484 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13485 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13486 	if (vsi->type == I40E_VSI_MAIN)
13487 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
13488 
13489 	/* assign it some queues */
13490 	ret = i40e_alloc_rings(vsi);
13491 	if (ret)
13492 		goto err_rings;
13493 
13494 	/* map all of the rings to the q_vectors */
13495 	i40e_vsi_map_rings_to_vectors(vsi);
13496 	return vsi;
13497 
13498 err_rings:
13499 	i40e_vsi_free_q_vectors(vsi);
13500 	if (vsi->netdev_registered) {
13501 		vsi->netdev_registered = false;
13502 		unregister_netdev(vsi->netdev);
13503 		free_netdev(vsi->netdev);
13504 		vsi->netdev = NULL;
13505 	}
13506 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13507 err_vsi:
13508 	i40e_vsi_clear(vsi);
13509 	return NULL;
13510 }
13511 
13512 /**
13513  * i40e_vsi_setup - Set up a VSI by a given type
13514  * @pf: board private structure
13515  * @type: VSI type
13516  * @uplink_seid: the switch element to link to
13517  * @param1: usage depends upon VSI type. For VF types, indicates VF id
13518  *
13519  * This allocates the sw VSI structure and its queue resources, then add a VSI
13520  * to the identified VEB.
13521  *
13522  * Returns pointer to the successfully allocated and configure VSI sw struct on
13523  * success, otherwise returns NULL on failure.
13524  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)13525 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
13526 				u16 uplink_seid, u32 param1)
13527 {
13528 	struct i40e_vsi *vsi = NULL;
13529 	struct i40e_veb *veb = NULL;
13530 	u16 alloc_queue_pairs;
13531 	int ret, i;
13532 	int v_idx;
13533 
13534 	/* The requested uplink_seid must be either
13535 	 *     - the PF's port seid
13536 	 *              no VEB is needed because this is the PF
13537 	 *              or this is a Flow Director special case VSI
13538 	 *     - seid of an existing VEB
13539 	 *     - seid of a VSI that owns an existing VEB
13540 	 *     - seid of a VSI that doesn't own a VEB
13541 	 *              a new VEB is created and the VSI becomes the owner
13542 	 *     - seid of the PF VSI, which is what creates the first VEB
13543 	 *              this is a special case of the previous
13544 	 *
13545 	 * Find which uplink_seid we were given and create a new VEB if needed
13546 	 */
13547 	for (i = 0; i < I40E_MAX_VEB; i++) {
13548 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
13549 			veb = pf->veb[i];
13550 			break;
13551 		}
13552 	}
13553 
13554 	if (!veb && uplink_seid != pf->mac_seid) {
13555 
13556 		for (i = 0; i < pf->num_alloc_vsi; i++) {
13557 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
13558 				vsi = pf->vsi[i];
13559 				break;
13560 			}
13561 		}
13562 		if (!vsi) {
13563 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
13564 				 uplink_seid);
13565 			return NULL;
13566 		}
13567 
13568 		if (vsi->uplink_seid == pf->mac_seid)
13569 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
13570 					     vsi->tc_config.enabled_tc);
13571 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
13572 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13573 					     vsi->tc_config.enabled_tc);
13574 		if (veb) {
13575 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
13576 				dev_info(&vsi->back->pdev->dev,
13577 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
13578 				return NULL;
13579 			}
13580 			/* We come up by default in VEPA mode if SRIOV is not
13581 			 * already enabled, in which case we can't force VEPA
13582 			 * mode.
13583 			 */
13584 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
13585 				veb->bridge_mode = BRIDGE_MODE_VEPA;
13586 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13587 			}
13588 			i40e_config_bridge_mode(veb);
13589 		}
13590 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13591 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13592 				veb = pf->veb[i];
13593 		}
13594 		if (!veb) {
13595 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
13596 			return NULL;
13597 		}
13598 
13599 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13600 		uplink_seid = veb->seid;
13601 	}
13602 
13603 	/* get vsi sw struct */
13604 	v_idx = i40e_vsi_mem_alloc(pf, type);
13605 	if (v_idx < 0)
13606 		goto err_alloc;
13607 	vsi = pf->vsi[v_idx];
13608 	if (!vsi)
13609 		goto err_alloc;
13610 	vsi->type = type;
13611 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
13612 
13613 	if (type == I40E_VSI_MAIN)
13614 		pf->lan_vsi = v_idx;
13615 	else if (type == I40E_VSI_SRIOV)
13616 		vsi->vf_id = param1;
13617 	/* assign it some queues */
13618 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13619 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13620 
13621 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13622 	if (ret < 0) {
13623 		dev_info(&pf->pdev->dev,
13624 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
13625 			 alloc_queue_pairs, vsi->seid, ret);
13626 		goto err_vsi;
13627 	}
13628 	vsi->base_queue = ret;
13629 
13630 	/* get a VSI from the hardware */
13631 	vsi->uplink_seid = uplink_seid;
13632 	ret = i40e_add_vsi(vsi);
13633 	if (ret)
13634 		goto err_vsi;
13635 
13636 	switch (vsi->type) {
13637 	/* setup the netdev if needed */
13638 	case I40E_VSI_MAIN:
13639 	case I40E_VSI_VMDQ2:
13640 		ret = i40e_config_netdev(vsi);
13641 		if (ret)
13642 			goto err_netdev;
13643 		ret = register_netdev(vsi->netdev);
13644 		if (ret)
13645 			goto err_netdev;
13646 		vsi->netdev_registered = true;
13647 		netif_carrier_off(vsi->netdev);
13648 #ifdef CONFIG_I40E_DCB
13649 		/* Setup DCB netlink interface */
13650 		i40e_dcbnl_setup(vsi);
13651 #endif /* CONFIG_I40E_DCB */
13652 		fallthrough;
13653 	case I40E_VSI_FDIR:
13654 		/* set up vectors and rings if needed */
13655 		ret = i40e_vsi_setup_vectors(vsi);
13656 		if (ret)
13657 			goto err_msix;
13658 
13659 		ret = i40e_alloc_rings(vsi);
13660 		if (ret)
13661 			goto err_rings;
13662 
13663 		/* map all of the rings to the q_vectors */
13664 		i40e_vsi_map_rings_to_vectors(vsi);
13665 
13666 		i40e_vsi_reset_stats(vsi);
13667 		break;
13668 	default:
13669 		/* no netdev or rings for the other VSI types */
13670 		break;
13671 	}
13672 
13673 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
13674 	    (vsi->type == I40E_VSI_VMDQ2)) {
13675 		ret = i40e_vsi_config_rss(vsi);
13676 	}
13677 	return vsi;
13678 
13679 err_rings:
13680 	i40e_vsi_free_q_vectors(vsi);
13681 err_msix:
13682 	if (vsi->netdev_registered) {
13683 		vsi->netdev_registered = false;
13684 		unregister_netdev(vsi->netdev);
13685 		free_netdev(vsi->netdev);
13686 		vsi->netdev = NULL;
13687 	}
13688 err_netdev:
13689 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13690 err_vsi:
13691 	i40e_vsi_clear(vsi);
13692 err_alloc:
13693 	return NULL;
13694 }
13695 
13696 /**
13697  * i40e_veb_get_bw_info - Query VEB BW information
13698  * @veb: the veb to query
13699  *
13700  * Query the Tx scheduler BW configuration data for given VEB
13701  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)13702 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
13703 {
13704 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
13705 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
13706 	struct i40e_pf *pf = veb->pf;
13707 	struct i40e_hw *hw = &pf->hw;
13708 	u32 tc_bw_max;
13709 	int ret = 0;
13710 	int i;
13711 
13712 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
13713 						  &bw_data, NULL);
13714 	if (ret) {
13715 		dev_info(&pf->pdev->dev,
13716 			 "query veb bw config failed, err %s aq_err %s\n",
13717 			 i40e_stat_str(&pf->hw, ret),
13718 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13719 		goto out;
13720 	}
13721 
13722 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
13723 						   &ets_data, NULL);
13724 	if (ret) {
13725 		dev_info(&pf->pdev->dev,
13726 			 "query veb bw ets config failed, err %s aq_err %s\n",
13727 			 i40e_stat_str(&pf->hw, ret),
13728 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13729 		goto out;
13730 	}
13731 
13732 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
13733 	veb->bw_max_quanta = ets_data.tc_bw_max;
13734 	veb->is_abs_credits = bw_data.absolute_credits_enable;
13735 	veb->enabled_tc = ets_data.tc_valid_bits;
13736 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
13737 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
13738 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
13739 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
13740 		veb->bw_tc_limit_credits[i] =
13741 					le16_to_cpu(bw_data.tc_bw_limits[i]);
13742 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
13743 	}
13744 
13745 out:
13746 	return ret;
13747 }
13748 
13749 /**
13750  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
13751  * @pf: board private structure
13752  *
13753  * On error: returns error code (negative)
13754  * On success: returns vsi index in PF (positive)
13755  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)13756 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
13757 {
13758 	int ret = -ENOENT;
13759 	struct i40e_veb *veb;
13760 	int i;
13761 
13762 	/* Need to protect the allocation of switch elements at the PF level */
13763 	mutex_lock(&pf->switch_mutex);
13764 
13765 	/* VEB list may be fragmented if VEB creation/destruction has
13766 	 * been happening.  We can afford to do a quick scan to look
13767 	 * for any free slots in the list.
13768 	 *
13769 	 * find next empty veb slot, looping back around if necessary
13770 	 */
13771 	i = 0;
13772 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
13773 		i++;
13774 	if (i >= I40E_MAX_VEB) {
13775 		ret = -ENOMEM;
13776 		goto err_alloc_veb;  /* out of VEB slots! */
13777 	}
13778 
13779 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
13780 	if (!veb) {
13781 		ret = -ENOMEM;
13782 		goto err_alloc_veb;
13783 	}
13784 	veb->pf = pf;
13785 	veb->idx = i;
13786 	veb->enabled_tc = 1;
13787 
13788 	pf->veb[i] = veb;
13789 	ret = i;
13790 err_alloc_veb:
13791 	mutex_unlock(&pf->switch_mutex);
13792 	return ret;
13793 }
13794 
13795 /**
13796  * i40e_switch_branch_release - Delete a branch of the switch tree
13797  * @branch: where to start deleting
13798  *
13799  * This uses recursion to find the tips of the branch to be
13800  * removed, deleting until we get back to and can delete this VEB.
13801  **/
i40e_switch_branch_release(struct i40e_veb * branch)13802 static void i40e_switch_branch_release(struct i40e_veb *branch)
13803 {
13804 	struct i40e_pf *pf = branch->pf;
13805 	u16 branch_seid = branch->seid;
13806 	u16 veb_idx = branch->idx;
13807 	int i;
13808 
13809 	/* release any VEBs on this VEB - RECURSION */
13810 	for (i = 0; i < I40E_MAX_VEB; i++) {
13811 		if (!pf->veb[i])
13812 			continue;
13813 		if (pf->veb[i]->uplink_seid == branch->seid)
13814 			i40e_switch_branch_release(pf->veb[i]);
13815 	}
13816 
13817 	/* Release the VSIs on this VEB, but not the owner VSI.
13818 	 *
13819 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
13820 	 *       the VEB itself, so don't use (*branch) after this loop.
13821 	 */
13822 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13823 		if (!pf->vsi[i])
13824 			continue;
13825 		if (pf->vsi[i]->uplink_seid == branch_seid &&
13826 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13827 			i40e_vsi_release(pf->vsi[i]);
13828 		}
13829 	}
13830 
13831 	/* There's one corner case where the VEB might not have been
13832 	 * removed, so double check it here and remove it if needed.
13833 	 * This case happens if the veb was created from the debugfs
13834 	 * commands and no VSIs were added to it.
13835 	 */
13836 	if (pf->veb[veb_idx])
13837 		i40e_veb_release(pf->veb[veb_idx]);
13838 }
13839 
13840 /**
13841  * i40e_veb_clear - remove veb struct
13842  * @veb: the veb to remove
13843  **/
i40e_veb_clear(struct i40e_veb * veb)13844 static void i40e_veb_clear(struct i40e_veb *veb)
13845 {
13846 	if (!veb)
13847 		return;
13848 
13849 	if (veb->pf) {
13850 		struct i40e_pf *pf = veb->pf;
13851 
13852 		mutex_lock(&pf->switch_mutex);
13853 		if (pf->veb[veb->idx] == veb)
13854 			pf->veb[veb->idx] = NULL;
13855 		mutex_unlock(&pf->switch_mutex);
13856 	}
13857 
13858 	kfree(veb);
13859 }
13860 
13861 /**
13862  * i40e_veb_release - Delete a VEB and free its resources
13863  * @veb: the VEB being removed
13864  **/
i40e_veb_release(struct i40e_veb * veb)13865 void i40e_veb_release(struct i40e_veb *veb)
13866 {
13867 	struct i40e_vsi *vsi = NULL;
13868 	struct i40e_pf *pf;
13869 	int i, n = 0;
13870 
13871 	pf = veb->pf;
13872 
13873 	/* find the remaining VSI and check for extras */
13874 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13875 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
13876 			n++;
13877 			vsi = pf->vsi[i];
13878 		}
13879 	}
13880 	if (n != 1) {
13881 		dev_info(&pf->pdev->dev,
13882 			 "can't remove VEB %d with %d VSIs left\n",
13883 			 veb->seid, n);
13884 		return;
13885 	}
13886 
13887 	/* move the remaining VSI to uplink veb */
13888 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
13889 	if (veb->uplink_seid) {
13890 		vsi->uplink_seid = veb->uplink_seid;
13891 		if (veb->uplink_seid == pf->mac_seid)
13892 			vsi->veb_idx = I40E_NO_VEB;
13893 		else
13894 			vsi->veb_idx = veb->veb_idx;
13895 	} else {
13896 		/* floating VEB */
13897 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
13898 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
13899 	}
13900 
13901 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13902 	i40e_veb_clear(veb);
13903 }
13904 
13905 /**
13906  * i40e_add_veb - create the VEB in the switch
13907  * @veb: the VEB to be instantiated
13908  * @vsi: the controlling VSI
13909  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)13910 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
13911 {
13912 	struct i40e_pf *pf = veb->pf;
13913 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
13914 	int ret;
13915 
13916 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
13917 			      veb->enabled_tc, false,
13918 			      &veb->seid, enable_stats, NULL);
13919 
13920 	/* get a VEB from the hardware */
13921 	if (ret) {
13922 		dev_info(&pf->pdev->dev,
13923 			 "couldn't add VEB, err %s aq_err %s\n",
13924 			 i40e_stat_str(&pf->hw, ret),
13925 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13926 		return -EPERM;
13927 	}
13928 
13929 	/* get statistics counter */
13930 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
13931 					 &veb->stats_idx, NULL, NULL, NULL);
13932 	if (ret) {
13933 		dev_info(&pf->pdev->dev,
13934 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
13935 			 i40e_stat_str(&pf->hw, ret),
13936 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13937 		return -EPERM;
13938 	}
13939 	ret = i40e_veb_get_bw_info(veb);
13940 	if (ret) {
13941 		dev_info(&pf->pdev->dev,
13942 			 "couldn't get VEB bw info, err %s aq_err %s\n",
13943 			 i40e_stat_str(&pf->hw, ret),
13944 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13945 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13946 		return -ENOENT;
13947 	}
13948 
13949 	vsi->uplink_seid = veb->seid;
13950 	vsi->veb_idx = veb->idx;
13951 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13952 
13953 	return 0;
13954 }
13955 
13956 /**
13957  * i40e_veb_setup - Set up a VEB
13958  * @pf: board private structure
13959  * @flags: VEB setup flags
13960  * @uplink_seid: the switch element to link to
13961  * @vsi_seid: the initial VSI seid
13962  * @enabled_tc: Enabled TC bit-map
13963  *
13964  * This allocates the sw VEB structure and links it into the switch
13965  * It is possible and legal for this to be a duplicate of an already
13966  * existing VEB.  It is also possible for both uplink and vsi seids
13967  * to be zero, in order to create a floating VEB.
13968  *
13969  * Returns pointer to the successfully allocated VEB sw struct on
13970  * success, otherwise returns NULL on failure.
13971  **/
i40e_veb_setup(struct i40e_pf * pf,u16 flags,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)13972 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
13973 				u16 uplink_seid, u16 vsi_seid,
13974 				u8 enabled_tc)
13975 {
13976 	struct i40e_veb *veb, *uplink_veb = NULL;
13977 	int vsi_idx, veb_idx;
13978 	int ret;
13979 
13980 	/* if one seid is 0, the other must be 0 to create a floating relay */
13981 	if ((uplink_seid == 0 || vsi_seid == 0) &&
13982 	    (uplink_seid + vsi_seid != 0)) {
13983 		dev_info(&pf->pdev->dev,
13984 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
13985 			 uplink_seid, vsi_seid);
13986 		return NULL;
13987 	}
13988 
13989 	/* make sure there is such a vsi and uplink */
13990 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
13991 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
13992 			break;
13993 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
13994 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
13995 			 vsi_seid);
13996 		return NULL;
13997 	}
13998 
13999 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14000 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14001 			if (pf->veb[veb_idx] &&
14002 			    pf->veb[veb_idx]->seid == uplink_seid) {
14003 				uplink_veb = pf->veb[veb_idx];
14004 				break;
14005 			}
14006 		}
14007 		if (!uplink_veb) {
14008 			dev_info(&pf->pdev->dev,
14009 				 "uplink seid %d not found\n", uplink_seid);
14010 			return NULL;
14011 		}
14012 	}
14013 
14014 	/* get veb sw struct */
14015 	veb_idx = i40e_veb_mem_alloc(pf);
14016 	if (veb_idx < 0)
14017 		goto err_alloc;
14018 	veb = pf->veb[veb_idx];
14019 	veb->flags = flags;
14020 	veb->uplink_seid = uplink_seid;
14021 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14022 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14023 
14024 	/* create the VEB in the switch */
14025 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14026 	if (ret)
14027 		goto err_veb;
14028 	if (vsi_idx == pf->lan_vsi)
14029 		pf->lan_veb = veb->idx;
14030 
14031 	return veb;
14032 
14033 err_veb:
14034 	i40e_veb_clear(veb);
14035 err_alloc:
14036 	return NULL;
14037 }
14038 
14039 /**
14040  * i40e_setup_pf_switch_element - set PF vars based on switch type
14041  * @pf: board private structure
14042  * @ele: element we are building info from
14043  * @num_reported: total number of elements
14044  * @printconfig: should we print the contents
14045  *
14046  * helper function to assist in extracting a few useful SEID values.
14047  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14048 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14049 				struct i40e_aqc_switch_config_element_resp *ele,
14050 				u16 num_reported, bool printconfig)
14051 {
14052 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14053 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14054 	u8 element_type = ele->element_type;
14055 	u16 seid = le16_to_cpu(ele->seid);
14056 
14057 	if (printconfig)
14058 		dev_info(&pf->pdev->dev,
14059 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14060 			 element_type, seid, uplink_seid, downlink_seid);
14061 
14062 	switch (element_type) {
14063 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14064 		pf->mac_seid = seid;
14065 		break;
14066 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14067 		/* Main VEB? */
14068 		if (uplink_seid != pf->mac_seid)
14069 			break;
14070 		if (pf->lan_veb >= I40E_MAX_VEB) {
14071 			int v;
14072 
14073 			/* find existing or else empty VEB */
14074 			for (v = 0; v < I40E_MAX_VEB; v++) {
14075 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14076 					pf->lan_veb = v;
14077 					break;
14078 				}
14079 			}
14080 			if (pf->lan_veb >= I40E_MAX_VEB) {
14081 				v = i40e_veb_mem_alloc(pf);
14082 				if (v < 0)
14083 					break;
14084 				pf->lan_veb = v;
14085 			}
14086 		}
14087 		if (pf->lan_veb >= I40E_MAX_VEB)
14088 			break;
14089 
14090 		pf->veb[pf->lan_veb]->seid = seid;
14091 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14092 		pf->veb[pf->lan_veb]->pf = pf;
14093 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14094 		break;
14095 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14096 		if (num_reported != 1)
14097 			break;
14098 		/* This is immediately after a reset so we can assume this is
14099 		 * the PF's VSI
14100 		 */
14101 		pf->mac_seid = uplink_seid;
14102 		pf->pf_seid = downlink_seid;
14103 		pf->main_vsi_seid = seid;
14104 		if (printconfig)
14105 			dev_info(&pf->pdev->dev,
14106 				 "pf_seid=%d main_vsi_seid=%d\n",
14107 				 pf->pf_seid, pf->main_vsi_seid);
14108 		break;
14109 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14110 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14111 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14112 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14113 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14114 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14115 		/* ignore these for now */
14116 		break;
14117 	default:
14118 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14119 			 element_type, seid);
14120 		break;
14121 	}
14122 }
14123 
14124 /**
14125  * i40e_fetch_switch_configuration - Get switch config from firmware
14126  * @pf: board private structure
14127  * @printconfig: should we print the contents
14128  *
14129  * Get the current switch configuration from the device and
14130  * extract a few useful SEID values.
14131  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)14132 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14133 {
14134 	struct i40e_aqc_get_switch_config_resp *sw_config;
14135 	u16 next_seid = 0;
14136 	int ret = 0;
14137 	u8 *aq_buf;
14138 	int i;
14139 
14140 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14141 	if (!aq_buf)
14142 		return -ENOMEM;
14143 
14144 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14145 	do {
14146 		u16 num_reported, num_total;
14147 
14148 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14149 						I40E_AQ_LARGE_BUF,
14150 						&next_seid, NULL);
14151 		if (ret) {
14152 			dev_info(&pf->pdev->dev,
14153 				 "get switch config failed err %s aq_err %s\n",
14154 				 i40e_stat_str(&pf->hw, ret),
14155 				 i40e_aq_str(&pf->hw,
14156 					     pf->hw.aq.asq_last_status));
14157 			kfree(aq_buf);
14158 			return -ENOENT;
14159 		}
14160 
14161 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14162 		num_total = le16_to_cpu(sw_config->header.num_total);
14163 
14164 		if (printconfig)
14165 			dev_info(&pf->pdev->dev,
14166 				 "header: %d reported %d total\n",
14167 				 num_reported, num_total);
14168 
14169 		for (i = 0; i < num_reported; i++) {
14170 			struct i40e_aqc_switch_config_element_resp *ele =
14171 				&sw_config->element[i];
14172 
14173 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14174 						     printconfig);
14175 		}
14176 	} while (next_seid != 0);
14177 
14178 	kfree(aq_buf);
14179 	return ret;
14180 }
14181 
14182 /**
14183  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14184  * @pf: board private structure
14185  * @reinit: if the Main VSI needs to re-initialized.
14186  * @lock_acquired: indicates whether or not the lock has been acquired
14187  *
14188  * Returns 0 on success, negative value on failure
14189  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)14190 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14191 {
14192 	u16 flags = 0;
14193 	int ret;
14194 
14195 	/* find out what's out there already */
14196 	ret = i40e_fetch_switch_configuration(pf, false);
14197 	if (ret) {
14198 		dev_info(&pf->pdev->dev,
14199 			 "couldn't fetch switch config, err %s aq_err %s\n",
14200 			 i40e_stat_str(&pf->hw, ret),
14201 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14202 		return ret;
14203 	}
14204 	i40e_pf_reset_stats(pf);
14205 
14206 	/* set the switch config bit for the whole device to
14207 	 * support limited promisc or true promisc
14208 	 * when user requests promisc. The default is limited
14209 	 * promisc.
14210 	*/
14211 
14212 	if ((pf->hw.pf_id == 0) &&
14213 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14214 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14215 		pf->last_sw_conf_flags = flags;
14216 	}
14217 
14218 	if (pf->hw.pf_id == 0) {
14219 		u16 valid_flags;
14220 
14221 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14222 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14223 						NULL);
14224 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14225 			dev_info(&pf->pdev->dev,
14226 				 "couldn't set switch config bits, err %s aq_err %s\n",
14227 				 i40e_stat_str(&pf->hw, ret),
14228 				 i40e_aq_str(&pf->hw,
14229 					     pf->hw.aq.asq_last_status));
14230 			/* not a fatal problem, just keep going */
14231 		}
14232 		pf->last_sw_conf_valid_flags = valid_flags;
14233 	}
14234 
14235 	/* first time setup */
14236 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14237 		struct i40e_vsi *vsi = NULL;
14238 		u16 uplink_seid;
14239 
14240 		/* Set up the PF VSI associated with the PF's main VSI
14241 		 * that is already in the HW switch
14242 		 */
14243 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14244 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14245 		else
14246 			uplink_seid = pf->mac_seid;
14247 		if (pf->lan_vsi == I40E_NO_VSI)
14248 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14249 		else if (reinit)
14250 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14251 		if (!vsi) {
14252 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14253 			i40e_cloud_filter_exit(pf);
14254 			i40e_fdir_teardown(pf);
14255 			return -EAGAIN;
14256 		}
14257 	} else {
14258 		/* force a reset of TC and queue layout configurations */
14259 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14260 
14261 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14262 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14263 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14264 	}
14265 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14266 
14267 	i40e_fdir_sb_setup(pf);
14268 
14269 	/* Setup static PF queue filter control settings */
14270 	ret = i40e_setup_pf_filter_control(pf);
14271 	if (ret) {
14272 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14273 			 ret);
14274 		/* Failure here should not stop continuing other steps */
14275 	}
14276 
14277 	/* enable RSS in the HW, even for only one queue, as the stack can use
14278 	 * the hash
14279 	 */
14280 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14281 		i40e_pf_config_rss(pf);
14282 
14283 	/* fill in link information and enable LSE reporting */
14284 	i40e_link_event(pf);
14285 
14286 	/* Initialize user-specific link properties */
14287 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14288 				  I40E_AQ_AN_COMPLETED) ? true : false);
14289 
14290 	i40e_ptp_init(pf);
14291 
14292 	if (!lock_acquired)
14293 		rtnl_lock();
14294 
14295 	/* repopulate tunnel port filters */
14296 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14297 
14298 	if (!lock_acquired)
14299 		rtnl_unlock();
14300 
14301 	return ret;
14302 }
14303 
14304 /**
14305  * i40e_determine_queue_usage - Work out queue distribution
14306  * @pf: board private structure
14307  **/
i40e_determine_queue_usage(struct i40e_pf * pf)14308 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14309 {
14310 	int queues_left;
14311 	int q_max;
14312 
14313 	pf->num_lan_qps = 0;
14314 
14315 	/* Find the max queues to be put into basic use.  We'll always be
14316 	 * using TC0, whether or not DCB is running, and TC0 will get the
14317 	 * big RSS set.
14318 	 */
14319 	queues_left = pf->hw.func_caps.num_tx_qp;
14320 
14321 	if ((queues_left == 1) ||
14322 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14323 		/* one qp for PF, no queues for anything else */
14324 		queues_left = 0;
14325 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14326 
14327 		/* make sure all the fancies are disabled */
14328 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14329 			       I40E_FLAG_IWARP_ENABLED	|
14330 			       I40E_FLAG_FD_SB_ENABLED	|
14331 			       I40E_FLAG_FD_ATR_ENABLED	|
14332 			       I40E_FLAG_DCB_CAPABLE	|
14333 			       I40E_FLAG_DCB_ENABLED	|
14334 			       I40E_FLAG_SRIOV_ENABLED	|
14335 			       I40E_FLAG_VMDQ_ENABLED);
14336 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14337 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14338 				  I40E_FLAG_FD_SB_ENABLED |
14339 				  I40E_FLAG_FD_ATR_ENABLED |
14340 				  I40E_FLAG_DCB_CAPABLE))) {
14341 		/* one qp for PF */
14342 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14343 		queues_left -= pf->num_lan_qps;
14344 
14345 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14346 			       I40E_FLAG_IWARP_ENABLED	|
14347 			       I40E_FLAG_FD_SB_ENABLED	|
14348 			       I40E_FLAG_FD_ATR_ENABLED	|
14349 			       I40E_FLAG_DCB_ENABLED	|
14350 			       I40E_FLAG_VMDQ_ENABLED);
14351 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14352 	} else {
14353 		/* Not enough queues for all TCs */
14354 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14355 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14356 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14357 					I40E_FLAG_DCB_ENABLED);
14358 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14359 		}
14360 
14361 		/* limit lan qps to the smaller of qps, cpus or msix */
14362 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14363 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14364 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14365 		pf->num_lan_qps = q_max;
14366 
14367 		queues_left -= pf->num_lan_qps;
14368 	}
14369 
14370 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14371 		if (queues_left > 1) {
14372 			queues_left -= 1; /* save 1 queue for FD */
14373 		} else {
14374 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14375 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14376 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14377 		}
14378 	}
14379 
14380 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14381 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14382 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14383 					(queues_left / pf->num_vf_qps));
14384 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14385 	}
14386 
14387 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14388 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14389 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14390 					  (queues_left / pf->num_vmdq_qps));
14391 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14392 	}
14393 
14394 	pf->queues_left = queues_left;
14395 	dev_dbg(&pf->pdev->dev,
14396 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14397 		pf->hw.func_caps.num_tx_qp,
14398 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14399 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14400 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14401 		queues_left);
14402 }
14403 
14404 /**
14405  * i40e_setup_pf_filter_control - Setup PF static filter control
14406  * @pf: PF to be setup
14407  *
14408  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14409  * settings. If PE/FCoE are enabled then it will also set the per PF
14410  * based filter sizes required for them. It also enables Flow director,
14411  * ethertype and macvlan type filter settings for the pf.
14412  *
14413  * Returns 0 on success, negative on failure
14414  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)14415 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14416 {
14417 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14418 
14419 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14420 
14421 	/* Flow Director is enabled */
14422 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14423 		settings->enable_fdir = true;
14424 
14425 	/* Ethtype and MACVLAN filters enabled for PF */
14426 	settings->enable_ethtype = true;
14427 	settings->enable_macvlan = true;
14428 
14429 	if (i40e_set_filter_control(&pf->hw, settings))
14430 		return -ENOENT;
14431 
14432 	return 0;
14433 }
14434 
14435 #define INFO_STRING_LEN 255
14436 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)14437 static void i40e_print_features(struct i40e_pf *pf)
14438 {
14439 	struct i40e_hw *hw = &pf->hw;
14440 	char *buf;
14441 	int i;
14442 
14443 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14444 	if (!buf)
14445 		return;
14446 
14447 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14448 #ifdef CONFIG_PCI_IOV
14449 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
14450 #endif
14451 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
14452 		      pf->hw.func_caps.num_vsis,
14453 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
14454 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
14455 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
14456 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
14457 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
14458 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14459 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
14460 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
14461 	}
14462 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
14463 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
14464 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
14465 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
14466 	if (pf->flags & I40E_FLAG_PTP)
14467 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
14468 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
14469 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
14470 	else
14471 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
14472 
14473 	dev_info(&pf->pdev->dev, "%s\n", buf);
14474 	kfree(buf);
14475 	WARN_ON(i > INFO_STRING_LEN);
14476 }
14477 
14478 /**
14479  * i40e_get_platform_mac_addr - get platform-specific MAC address
14480  * @pdev: PCI device information struct
14481  * @pf: board private structure
14482  *
14483  * Look up the MAC address for the device. First we'll try
14484  * eth_platform_get_mac_address, which will check Open Firmware, or arch
14485  * specific fallback. Otherwise, we'll default to the stored value in
14486  * firmware.
14487  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)14488 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
14489 {
14490 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
14491 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
14492 }
14493 
14494 /**
14495  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
14496  * @fec_cfg: FEC option to set in flags
14497  * @flags: ptr to flags in which we set FEC option
14498  **/
i40e_set_fec_in_flags(u8 fec_cfg,u32 * flags)14499 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14500 {
14501 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14502 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14503 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14504 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14505 		*flags |= I40E_FLAG_RS_FEC;
14506 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14507 	}
14508 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14509 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14510 		*flags |= I40E_FLAG_BASE_R_FEC;
14511 		*flags &= ~I40E_FLAG_RS_FEC;
14512 	}
14513 	if (fec_cfg == 0)
14514 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14515 }
14516 
14517 /**
14518  * i40e_check_recovery_mode - check if we are running transition firmware
14519  * @pf: board private structure
14520  *
14521  * Check registers indicating the firmware runs in recovery mode. Sets the
14522  * appropriate driver state.
14523  *
14524  * Returns true if the recovery mode was detected, false otherwise
14525  **/
i40e_check_recovery_mode(struct i40e_pf * pf)14526 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
14527 {
14528 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
14529 
14530 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
14531 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
14532 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
14533 		set_bit(__I40E_RECOVERY_MODE, pf->state);
14534 
14535 		return true;
14536 	}
14537 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14538 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
14539 
14540 	return false;
14541 }
14542 
14543 /**
14544  * i40e_pf_loop_reset - perform reset in a loop.
14545  * @pf: board private structure
14546  *
14547  * This function is useful when a NIC is about to enter recovery mode.
14548  * When a NIC's internal data structures are corrupted the NIC's
14549  * firmware is going to enter recovery mode.
14550  * Right after a POR it takes about 7 minutes for firmware to enter
14551  * recovery mode. Until that time a NIC is in some kind of intermediate
14552  * state. After that time period the NIC almost surely enters
14553  * recovery mode. The only way for a driver to detect intermediate
14554  * state is to issue a series of pf-resets and check a return value.
14555  * If a PF reset returns success then the firmware could be in recovery
14556  * mode so the caller of this code needs to check for recovery mode
14557  * if this function returns success. There is a little chance that
14558  * firmware will hang in intermediate state forever.
14559  * Since waiting 7 minutes is quite a lot of time this function waits
14560  * 10 seconds and then gives up by returning an error.
14561  *
14562  * Return 0 on success, negative on failure.
14563  **/
i40e_pf_loop_reset(struct i40e_pf * pf)14564 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
14565 {
14566 	/* wait max 10 seconds for PF reset to succeed */
14567 	const unsigned long time_end = jiffies + 10 * HZ;
14568 
14569 	struct i40e_hw *hw = &pf->hw;
14570 	i40e_status ret;
14571 
14572 	ret = i40e_pf_reset(hw);
14573 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
14574 		usleep_range(10000, 20000);
14575 		ret = i40e_pf_reset(hw);
14576 	}
14577 
14578 	if (ret == I40E_SUCCESS)
14579 		pf->pfr_count++;
14580 	else
14581 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
14582 
14583 	return ret;
14584 }
14585 
14586 /**
14587  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
14588  * @pf: board private structure
14589  *
14590  * Check FW registers to determine if FW issued unexpected EMP Reset.
14591  * Every time when unexpected EMP Reset occurs the FW increments
14592  * a counter of unexpected EMP Resets. When the counter reaches 10
14593  * the FW should enter the Recovery mode
14594  *
14595  * Returns true if FW issued unexpected EMP Reset
14596  **/
i40e_check_fw_empr(struct i40e_pf * pf)14597 static bool i40e_check_fw_empr(struct i40e_pf *pf)
14598 {
14599 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
14600 			   I40E_GL_FWSTS_FWS1B_MASK;
14601 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
14602 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
14603 }
14604 
14605 /**
14606  * i40e_handle_resets - handle EMP resets and PF resets
14607  * @pf: board private structure
14608  *
14609  * Handle both EMP resets and PF resets and conclude whether there are
14610  * any issues regarding these resets. If there are any issues then
14611  * generate log entry.
14612  *
14613  * Return 0 if NIC is healthy or negative value when there are issues
14614  * with resets
14615  **/
i40e_handle_resets(struct i40e_pf * pf)14616 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
14617 {
14618 	const i40e_status pfr = i40e_pf_loop_reset(pf);
14619 	const bool is_empr = i40e_check_fw_empr(pf);
14620 
14621 	if (is_empr || pfr != I40E_SUCCESS)
14622 		dev_crit(&pf->pdev->dev, "Entering recovery mode due to repeated FW resets. This may take several minutes. Refer to the Intel(R) Ethernet Adapters and Devices User Guide.\n");
14623 
14624 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
14625 }
14626 
14627 /**
14628  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
14629  * @pf: board private structure
14630  * @hw: ptr to the hardware info
14631  *
14632  * This function does a minimal setup of all subsystems needed for running
14633  * recovery mode.
14634  *
14635  * Returns 0 on success, negative on failure
14636  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)14637 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
14638 {
14639 	struct i40e_vsi *vsi;
14640 	int err;
14641 	int v_idx;
14642 
14643 	pci_save_state(pf->pdev);
14644 
14645 	/* set up periodic task facility */
14646 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14647 	pf->service_timer_period = HZ;
14648 
14649 	INIT_WORK(&pf->service_task, i40e_service_task);
14650 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14651 
14652 	err = i40e_init_interrupt_scheme(pf);
14653 	if (err)
14654 		goto err_switch_setup;
14655 
14656 	/* The number of VSIs reported by the FW is the minimum guaranteed
14657 	 * to us; HW supports far more and we share the remaining pool with
14658 	 * the other PFs. We allocate space for more than the guarantee with
14659 	 * the understanding that we might not get them all later.
14660 	 */
14661 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14662 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14663 	else
14664 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14665 
14666 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
14667 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14668 			  GFP_KERNEL);
14669 	if (!pf->vsi) {
14670 		err = -ENOMEM;
14671 		goto err_switch_setup;
14672 	}
14673 
14674 	/* We allocate one VSI which is needed as absolute minimum
14675 	 * in order to register the netdev
14676 	 */
14677 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
14678 	if (v_idx < 0) {
14679 		err = v_idx;
14680 		goto err_switch_setup;
14681 	}
14682 	pf->lan_vsi = v_idx;
14683 	vsi = pf->vsi[v_idx];
14684 	if (!vsi) {
14685 		err = -EFAULT;
14686 		goto err_switch_setup;
14687 	}
14688 	vsi->alloc_queue_pairs = 1;
14689 	err = i40e_config_netdev(vsi);
14690 	if (err)
14691 		goto err_switch_setup;
14692 	err = register_netdev(vsi->netdev);
14693 	if (err)
14694 		goto err_switch_setup;
14695 	vsi->netdev_registered = true;
14696 	i40e_dbg_pf_init(pf);
14697 
14698 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
14699 	if (err)
14700 		goto err_switch_setup;
14701 
14702 	/* tell the firmware that we're starting */
14703 	i40e_send_version(pf);
14704 
14705 	/* since everything's happy, start the service_task timer */
14706 	mod_timer(&pf->service_timer,
14707 		  round_jiffies(jiffies + pf->service_timer_period));
14708 
14709 	return 0;
14710 
14711 err_switch_setup:
14712 	i40e_reset_interrupt_capability(pf);
14713 	del_timer_sync(&pf->service_timer);
14714 	i40e_shutdown_adminq(hw);
14715 	iounmap(hw->hw_addr);
14716 	pci_disable_pcie_error_reporting(pf->pdev);
14717 	pci_release_mem_regions(pf->pdev);
14718 	pci_disable_device(pf->pdev);
14719 	kfree(pf);
14720 
14721 	return err;
14722 }
14723 
14724 /**
14725  * i40e_probe - Device initialization routine
14726  * @pdev: PCI device information struct
14727  * @ent: entry in i40e_pci_tbl
14728  *
14729  * i40e_probe initializes a PF identified by a pci_dev structure.
14730  * The OS initialization, configuring of the PF private structure,
14731  * and a hardware reset occur.
14732  *
14733  * Returns 0 on success, negative on failure
14734  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)14735 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
14736 {
14737 	struct i40e_aq_get_phy_abilities_resp abilities;
14738 	struct i40e_pf *pf;
14739 	struct i40e_hw *hw;
14740 	static u16 pfs_found;
14741 	u16 wol_nvm_bits;
14742 	u16 link_status;
14743 	int err;
14744 	u32 val;
14745 	u32 i;
14746 
14747 	err = pci_enable_device_mem(pdev);
14748 	if (err)
14749 		return err;
14750 
14751 	/* set up for high or low dma */
14752 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
14753 	if (err) {
14754 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
14755 		if (err) {
14756 			dev_err(&pdev->dev,
14757 				"DMA configuration failed: 0x%x\n", err);
14758 			goto err_dma;
14759 		}
14760 	}
14761 
14762 	/* set up pci connections */
14763 	err = pci_request_mem_regions(pdev, i40e_driver_name);
14764 	if (err) {
14765 		dev_info(&pdev->dev,
14766 			 "pci_request_selected_regions failed %d\n", err);
14767 		goto err_pci_reg;
14768 	}
14769 
14770 	pci_enable_pcie_error_reporting(pdev);
14771 	pci_set_master(pdev);
14772 
14773 	/* Now that we have a PCI connection, we need to do the
14774 	 * low level device setup.  This is primarily setting up
14775 	 * the Admin Queue structures and then querying for the
14776 	 * device's current profile information.
14777 	 */
14778 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
14779 	if (!pf) {
14780 		err = -ENOMEM;
14781 		goto err_pf_alloc;
14782 	}
14783 	pf->next_vsi = 0;
14784 	pf->pdev = pdev;
14785 	set_bit(__I40E_DOWN, pf->state);
14786 
14787 	hw = &pf->hw;
14788 	hw->back = pf;
14789 
14790 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
14791 				I40E_MAX_CSR_SPACE);
14792 	/* We believe that the highest register to read is
14793 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
14794 	 * is not less than that before mapping to prevent a
14795 	 * kernel panic.
14796 	 */
14797 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
14798 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
14799 			pf->ioremap_len);
14800 		err = -ENOMEM;
14801 		goto err_ioremap;
14802 	}
14803 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
14804 	if (!hw->hw_addr) {
14805 		err = -EIO;
14806 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
14807 			 (unsigned int)pci_resource_start(pdev, 0),
14808 			 pf->ioremap_len, err);
14809 		goto err_ioremap;
14810 	}
14811 	hw->vendor_id = pdev->vendor;
14812 	hw->device_id = pdev->device;
14813 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
14814 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
14815 	hw->subsystem_device_id = pdev->subsystem_device;
14816 	hw->bus.device = PCI_SLOT(pdev->devfn);
14817 	hw->bus.func = PCI_FUNC(pdev->devfn);
14818 	hw->bus.bus_id = pdev->bus->number;
14819 	pf->instance = pfs_found;
14820 
14821 	/* Select something other than the 802.1ad ethertype for the
14822 	 * switch to use internally and drop on ingress.
14823 	 */
14824 	hw->switch_tag = 0xffff;
14825 	hw->first_tag = ETH_P_8021AD;
14826 	hw->second_tag = ETH_P_8021Q;
14827 
14828 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
14829 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
14830 	INIT_LIST_HEAD(&pf->ddp_old_prof);
14831 
14832 	/* set up the locks for the AQ, do this only once in probe
14833 	 * and destroy them only once in remove
14834 	 */
14835 	mutex_init(&hw->aq.asq_mutex);
14836 	mutex_init(&hw->aq.arq_mutex);
14837 
14838 	pf->msg_enable = netif_msg_init(debug,
14839 					NETIF_MSG_DRV |
14840 					NETIF_MSG_PROBE |
14841 					NETIF_MSG_LINK);
14842 	if (debug < -1)
14843 		pf->hw.debug_mask = debug;
14844 
14845 	/* do a special CORER for clearing PXE mode once at init */
14846 	if (hw->revision_id == 0 &&
14847 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
14848 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
14849 		i40e_flush(hw);
14850 		msleep(200);
14851 		pf->corer_count++;
14852 
14853 		i40e_clear_pxe_mode(hw);
14854 	}
14855 
14856 	/* Reset here to make sure all is clean and to define PF 'n' */
14857 	i40e_clear_hw(hw);
14858 
14859 	err = i40e_set_mac_type(hw);
14860 	if (err) {
14861 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14862 			 err);
14863 		goto err_pf_reset;
14864 	}
14865 
14866 	err = i40e_handle_resets(pf);
14867 	if (err)
14868 		goto err_pf_reset;
14869 
14870 	i40e_check_recovery_mode(pf);
14871 
14872 	hw->aq.num_arq_entries = I40E_AQ_LEN;
14873 	hw->aq.num_asq_entries = I40E_AQ_LEN;
14874 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14875 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14876 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
14877 
14878 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
14879 		 "%s-%s:misc",
14880 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
14881 
14882 	err = i40e_init_shared_code(hw);
14883 	if (err) {
14884 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14885 			 err);
14886 		goto err_pf_reset;
14887 	}
14888 
14889 	/* set up a default setting for link flow control */
14890 	pf->hw.fc.requested_mode = I40E_FC_NONE;
14891 
14892 	err = i40e_init_adminq(hw);
14893 	if (err) {
14894 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
14895 			dev_info(&pdev->dev,
14896 				 "The driver for the device stopped because the NVM image v%u.%u is newer than expected v%u.%u. You must install the most recent version of the network driver.\n",
14897 				 hw->aq.api_maj_ver,
14898 				 hw->aq.api_min_ver,
14899 				 I40E_FW_API_VERSION_MAJOR,
14900 				 I40E_FW_MINOR_VERSION(hw));
14901 		else
14902 			dev_info(&pdev->dev,
14903 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
14904 
14905 		goto err_pf_reset;
14906 	}
14907 	i40e_get_oem_version(hw);
14908 
14909 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
14910 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
14911 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
14912 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
14913 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
14914 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
14915 
14916 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
14917 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
14918 		dev_info(&pdev->dev,
14919 			 "The driver for the device detected a newer version of the NVM image v%u.%u than expected v%u.%u. Please install the most recent version of the network driver.\n",
14920 			 hw->aq.api_maj_ver,
14921 			 hw->aq.api_min_ver,
14922 			 I40E_FW_API_VERSION_MAJOR,
14923 			 I40E_FW_MINOR_VERSION(hw));
14924 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
14925 		dev_info(&pdev->dev,
14926 			 "The driver for the device detected an older version of the NVM image v%u.%u than expected v%u.%u. Please update the NVM image.\n",
14927 			 hw->aq.api_maj_ver,
14928 			 hw->aq.api_min_ver,
14929 			 I40E_FW_API_VERSION_MAJOR,
14930 			 I40E_FW_MINOR_VERSION(hw));
14931 
14932 	i40e_verify_eeprom(pf);
14933 
14934 	/* Rev 0 hardware was never productized */
14935 	if (hw->revision_id < 1)
14936 		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");
14937 
14938 	i40e_clear_pxe_mode(hw);
14939 
14940 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
14941 	if (err)
14942 		goto err_adminq_setup;
14943 
14944 	err = i40e_sw_init(pf);
14945 	if (err) {
14946 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
14947 		goto err_sw_init;
14948 	}
14949 
14950 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14951 		return i40e_init_recovery_mode(pf, hw);
14952 
14953 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
14954 				hw->func_caps.num_rx_qp, 0, 0);
14955 	if (err) {
14956 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
14957 		goto err_init_lan_hmc;
14958 	}
14959 
14960 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
14961 	if (err) {
14962 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
14963 		err = -ENOENT;
14964 		goto err_configure_lan_hmc;
14965 	}
14966 
14967 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
14968 	 * Ignore error return codes because if it was already disabled via
14969 	 * hardware settings this will fail
14970 	 */
14971 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
14972 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
14973 		i40e_aq_stop_lldp(hw, true, false, NULL);
14974 	}
14975 
14976 	/* allow a platform config to override the HW addr */
14977 	i40e_get_platform_mac_addr(pdev, pf);
14978 
14979 	if (!is_valid_ether_addr(hw->mac.addr)) {
14980 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
14981 		err = -EIO;
14982 		goto err_mac_addr;
14983 	}
14984 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
14985 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
14986 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
14987 	if (is_valid_ether_addr(hw->mac.port_addr))
14988 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
14989 
14990 	pci_set_drvdata(pdev, pf);
14991 	pci_save_state(pdev);
14992 
14993 	dev_info(&pdev->dev,
14994 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
14995 			"FW LLDP is disabled\n" :
14996 			"FW LLDP is enabled\n");
14997 
14998 	/* Enable FW to write default DCB config on link-up */
14999 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15000 
15001 #ifdef CONFIG_I40E_DCB
15002 	err = i40e_init_pf_dcb(pf);
15003 	if (err) {
15004 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15005 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15006 		/* Continue without DCB enabled */
15007 	}
15008 #endif /* CONFIG_I40E_DCB */
15009 
15010 	/* set up periodic task facility */
15011 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15012 	pf->service_timer_period = HZ;
15013 
15014 	INIT_WORK(&pf->service_task, i40e_service_task);
15015 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15016 
15017 	/* NVM bit on means WoL disabled for the port */
15018 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15019 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15020 		pf->wol_en = false;
15021 	else
15022 		pf->wol_en = true;
15023 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15024 
15025 	/* set up the main switch operations */
15026 	i40e_determine_queue_usage(pf);
15027 	err = i40e_init_interrupt_scheme(pf);
15028 	if (err)
15029 		goto err_switch_setup;
15030 
15031 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15032 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15033 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15034 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15035 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15036 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15037 						    UDP_TUNNEL_TYPE_GENEVE;
15038 
15039 	/* The number of VSIs reported by the FW is the minimum guaranteed
15040 	 * to us; HW supports far more and we share the remaining pool with
15041 	 * the other PFs. We allocate space for more than the guarantee with
15042 	 * the understanding that we might not get them all later.
15043 	 */
15044 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15045 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15046 	else
15047 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15048 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15049 		dev_warn(&pf->pdev->dev,
15050 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15051 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15052 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15053 	}
15054 
15055 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15056 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15057 			  GFP_KERNEL);
15058 	if (!pf->vsi) {
15059 		err = -ENOMEM;
15060 		goto err_switch_setup;
15061 	}
15062 
15063 #ifdef CONFIG_PCI_IOV
15064 	/* prep for VF support */
15065 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15066 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15067 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15068 		if (pci_num_vf(pdev))
15069 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15070 	}
15071 #endif
15072 	err = i40e_setup_pf_switch(pf, false, false);
15073 	if (err) {
15074 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15075 		goto err_vsis;
15076 	}
15077 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15078 
15079 	/* if FDIR VSI was set up, start it now */
15080 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15081 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15082 			i40e_vsi_open(pf->vsi[i]);
15083 			break;
15084 		}
15085 	}
15086 
15087 	/* The driver only wants link up/down and module qualification
15088 	 * reports from firmware.  Note the negative logic.
15089 	 */
15090 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15091 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15092 					 I40E_AQ_EVENT_MEDIA_NA |
15093 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15094 	if (err)
15095 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15096 			 i40e_stat_str(&pf->hw, err),
15097 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15098 
15099 	/* Reconfigure hardware for allowing smaller MSS in the case
15100 	 * of TSO, so that we avoid the MDD being fired and causing
15101 	 * a reset in the case of small MSS+TSO.
15102 	 */
15103 	val = rd32(hw, I40E_REG_MSS);
15104 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15105 		val &= ~I40E_REG_MSS_MIN_MASK;
15106 		val |= I40E_64BYTE_MSS;
15107 		wr32(hw, I40E_REG_MSS, val);
15108 	}
15109 
15110 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15111 		msleep(75);
15112 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15113 		if (err)
15114 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15115 				 i40e_stat_str(&pf->hw, err),
15116 				 i40e_aq_str(&pf->hw,
15117 					     pf->hw.aq.asq_last_status));
15118 	}
15119 	/* The main driver is (mostly) up and happy. We need to set this state
15120 	 * before setting up the misc vector or we get a race and the vector
15121 	 * ends up disabled forever.
15122 	 */
15123 	clear_bit(__I40E_DOWN, pf->state);
15124 
15125 	/* In case of MSIX we are going to setup the misc vector right here
15126 	 * to handle admin queue events etc. In case of legacy and MSI
15127 	 * the misc functionality and queue processing is combined in
15128 	 * the same vector and that gets setup at open.
15129 	 */
15130 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15131 		err = i40e_setup_misc_vector(pf);
15132 		if (err) {
15133 			dev_info(&pdev->dev,
15134 				 "setup of misc vector failed: %d\n", err);
15135 			i40e_cloud_filter_exit(pf);
15136 			i40e_fdir_teardown(pf);
15137 			goto err_vsis;
15138 		}
15139 	}
15140 
15141 #ifdef CONFIG_PCI_IOV
15142 	/* prep for VF support */
15143 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15144 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15145 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15146 		/* disable link interrupts for VFs */
15147 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15148 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15149 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15150 		i40e_flush(hw);
15151 
15152 		if (pci_num_vf(pdev)) {
15153 			dev_info(&pdev->dev,
15154 				 "Active VFs found, allocating resources.\n");
15155 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15156 			if (err)
15157 				dev_info(&pdev->dev,
15158 					 "Error %d allocating resources for existing VFs\n",
15159 					 err);
15160 		}
15161 	}
15162 #endif /* CONFIG_PCI_IOV */
15163 
15164 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15165 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15166 						      pf->num_iwarp_msix,
15167 						      I40E_IWARP_IRQ_PILE_ID);
15168 		if (pf->iwarp_base_vector < 0) {
15169 			dev_info(&pdev->dev,
15170 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15171 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15172 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15173 		}
15174 	}
15175 
15176 	i40e_dbg_pf_init(pf);
15177 
15178 	/* tell the firmware that we're starting */
15179 	i40e_send_version(pf);
15180 
15181 	/* since everything's happy, start the service_task timer */
15182 	mod_timer(&pf->service_timer,
15183 		  round_jiffies(jiffies + pf->service_timer_period));
15184 
15185 	/* add this PF to client device list and launch a client service task */
15186 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15187 		err = i40e_lan_add_device(pf);
15188 		if (err)
15189 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15190 				 err);
15191 	}
15192 
15193 #define PCI_SPEED_SIZE 8
15194 #define PCI_WIDTH_SIZE 8
15195 	/* Devices on the IOSF bus do not have this information
15196 	 * and will report PCI Gen 1 x 1 by default so don't bother
15197 	 * checking them.
15198 	 */
15199 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15200 		char speed[PCI_SPEED_SIZE] = "Unknown";
15201 		char width[PCI_WIDTH_SIZE] = "Unknown";
15202 
15203 		/* Get the negotiated link width and speed from PCI config
15204 		 * space
15205 		 */
15206 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15207 					  &link_status);
15208 
15209 		i40e_set_pci_config_data(hw, link_status);
15210 
15211 		switch (hw->bus.speed) {
15212 		case i40e_bus_speed_8000:
15213 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15214 		case i40e_bus_speed_5000:
15215 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15216 		case i40e_bus_speed_2500:
15217 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15218 		default:
15219 			break;
15220 		}
15221 		switch (hw->bus.width) {
15222 		case i40e_bus_width_pcie_x8:
15223 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15224 		case i40e_bus_width_pcie_x4:
15225 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15226 		case i40e_bus_width_pcie_x2:
15227 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15228 		case i40e_bus_width_pcie_x1:
15229 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15230 		default:
15231 			break;
15232 		}
15233 
15234 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15235 			 speed, width);
15236 
15237 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15238 		    hw->bus.speed < i40e_bus_speed_8000) {
15239 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15240 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15241 		}
15242 	}
15243 
15244 	/* get the requested speeds from the fw */
15245 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15246 	if (err)
15247 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15248 			i40e_stat_str(&pf->hw, err),
15249 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15250 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15251 
15252 	/* set the FEC config due to the board capabilities */
15253 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15254 
15255 	/* get the supported phy types from the fw */
15256 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15257 	if (err)
15258 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15259 			i40e_stat_str(&pf->hw, err),
15260 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15261 
15262 	/* make sure the MFS hasn't been set lower than the default */
15263 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15264 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15265 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15266 	if (val < MAX_FRAME_SIZE_DEFAULT)
15267 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15268 			 i, val);
15269 
15270 	/* Add a filter to drop all Flow control frames from any VSI from being
15271 	 * transmitted. By doing so we stop a malicious VF from sending out
15272 	 * PAUSE or PFC frames and potentially controlling traffic for other
15273 	 * PF/VF VSIs.
15274 	 * The FW can still send Flow control frames if enabled.
15275 	 */
15276 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15277 						       pf->main_vsi_seid);
15278 
15279 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15280 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15281 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15282 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15283 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15284 	/* print a string summarizing features */
15285 	i40e_print_features(pf);
15286 
15287 	return 0;
15288 
15289 	/* Unwind what we've done if something failed in the setup */
15290 err_vsis:
15291 	set_bit(__I40E_DOWN, pf->state);
15292 	i40e_clear_interrupt_scheme(pf);
15293 	kfree(pf->vsi);
15294 err_switch_setup:
15295 	i40e_reset_interrupt_capability(pf);
15296 	del_timer_sync(&pf->service_timer);
15297 err_mac_addr:
15298 err_configure_lan_hmc:
15299 	(void)i40e_shutdown_lan_hmc(hw);
15300 err_init_lan_hmc:
15301 	kfree(pf->qp_pile);
15302 err_sw_init:
15303 err_adminq_setup:
15304 err_pf_reset:
15305 	iounmap(hw->hw_addr);
15306 err_ioremap:
15307 	kfree(pf);
15308 err_pf_alloc:
15309 	pci_disable_pcie_error_reporting(pdev);
15310 	pci_release_mem_regions(pdev);
15311 err_pci_reg:
15312 err_dma:
15313 	pci_disable_device(pdev);
15314 	return err;
15315 }
15316 
15317 /**
15318  * i40e_remove - Device removal routine
15319  * @pdev: PCI device information struct
15320  *
15321  * i40e_remove is called by the PCI subsystem to alert the driver
15322  * that is should release a PCI device.  This could be caused by a
15323  * Hot-Plug event, or because the driver is going to be removed from
15324  * memory.
15325  **/
i40e_remove(struct pci_dev * pdev)15326 static void i40e_remove(struct pci_dev *pdev)
15327 {
15328 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15329 	struct i40e_hw *hw = &pf->hw;
15330 	i40e_status ret_code;
15331 	int i;
15332 
15333 	i40e_dbg_pf_exit(pf);
15334 
15335 	i40e_ptp_stop(pf);
15336 
15337 	/* Disable RSS in hw */
15338 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15339 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15340 
15341 	while (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15342 		usleep_range(1000, 2000);
15343 
15344 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15345 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15346 		i40e_free_vfs(pf);
15347 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15348 	}
15349 	/* no more scheduling of any task */
15350 	set_bit(__I40E_SUSPENDED, pf->state);
15351 	set_bit(__I40E_DOWN, pf->state);
15352 	if (pf->service_timer.function)
15353 		del_timer_sync(&pf->service_timer);
15354 	if (pf->service_task.func)
15355 		cancel_work_sync(&pf->service_task);
15356 
15357 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15358 		struct i40e_vsi *vsi = pf->vsi[0];
15359 
15360 		/* We know that we have allocated only one vsi for this PF,
15361 		 * it was just for registering netdevice, so the interface
15362 		 * could be visible in the 'ifconfig' output
15363 		 */
15364 		unregister_netdev(vsi->netdev);
15365 		free_netdev(vsi->netdev);
15366 
15367 		goto unmap;
15368 	}
15369 
15370 	/* Client close must be called explicitly here because the timer
15371 	 * has been stopped.
15372 	 */
15373 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15374 
15375 	i40e_fdir_teardown(pf);
15376 
15377 	/* If there is a switch structure or any orphans, remove them.
15378 	 * This will leave only the PF's VSI remaining.
15379 	 */
15380 	for (i = 0; i < I40E_MAX_VEB; i++) {
15381 		if (!pf->veb[i])
15382 			continue;
15383 
15384 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15385 		    pf->veb[i]->uplink_seid == 0)
15386 			i40e_switch_branch_release(pf->veb[i]);
15387 	}
15388 
15389 	/* Now we can shutdown the PF's VSI, just before we kill
15390 	 * adminq and hmc.
15391 	 */
15392 	if (pf->vsi[pf->lan_vsi])
15393 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15394 
15395 	i40e_cloud_filter_exit(pf);
15396 
15397 	/* remove attached clients */
15398 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15399 		ret_code = i40e_lan_del_device(pf);
15400 		if (ret_code)
15401 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15402 				 ret_code);
15403 	}
15404 
15405 	/* shutdown and destroy the HMC */
15406 	if (hw->hmc.hmc_obj) {
15407 		ret_code = i40e_shutdown_lan_hmc(hw);
15408 		if (ret_code)
15409 			dev_warn(&pdev->dev,
15410 				 "Failed to destroy the HMC resources: %d\n",
15411 				 ret_code);
15412 	}
15413 
15414 unmap:
15415 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15416 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15417 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15418 		free_irq(pf->pdev->irq, pf);
15419 
15420 	/* shutdown the adminq */
15421 	i40e_shutdown_adminq(hw);
15422 
15423 	/* destroy the locks only once, here */
15424 	mutex_destroy(&hw->aq.arq_mutex);
15425 	mutex_destroy(&hw->aq.asq_mutex);
15426 
15427 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
15428 	rtnl_lock();
15429 	i40e_clear_interrupt_scheme(pf);
15430 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15431 		if (pf->vsi[i]) {
15432 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
15433 				i40e_vsi_clear_rings(pf->vsi[i]);
15434 			i40e_vsi_clear(pf->vsi[i]);
15435 			pf->vsi[i] = NULL;
15436 		}
15437 	}
15438 	rtnl_unlock();
15439 
15440 	for (i = 0; i < I40E_MAX_VEB; i++) {
15441 		kfree(pf->veb[i]);
15442 		pf->veb[i] = NULL;
15443 	}
15444 
15445 	kfree(pf->qp_pile);
15446 	kfree(pf->vsi);
15447 
15448 	iounmap(hw->hw_addr);
15449 	kfree(pf);
15450 	pci_release_mem_regions(pdev);
15451 
15452 	pci_disable_pcie_error_reporting(pdev);
15453 	pci_disable_device(pdev);
15454 }
15455 
15456 /**
15457  * i40e_pci_error_detected - warning that something funky happened in PCI land
15458  * @pdev: PCI device information struct
15459  * @error: the type of PCI error
15460  *
15461  * Called to warn that something happened and the error handling steps
15462  * are in progress.  Allows the driver to quiesce things, be ready for
15463  * remediation.
15464  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)15465 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
15466 						pci_channel_state_t error)
15467 {
15468 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15469 
15470 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
15471 
15472 	if (!pf) {
15473 		dev_info(&pdev->dev,
15474 			 "Cannot recover - error happened during device probe\n");
15475 		return PCI_ERS_RESULT_DISCONNECT;
15476 	}
15477 
15478 	/* shutdown all operations */
15479 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15480 		i40e_prep_for_reset(pf, false);
15481 
15482 	/* Request a slot reset */
15483 	return PCI_ERS_RESULT_NEED_RESET;
15484 }
15485 
15486 /**
15487  * i40e_pci_error_slot_reset - a PCI slot reset just happened
15488  * @pdev: PCI device information struct
15489  *
15490  * Called to find if the driver can work with the device now that
15491  * the pci slot has been reset.  If a basic connection seems good
15492  * (registers are readable and have sane content) then return a
15493  * happy little PCI_ERS_RESULT_xxx.
15494  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)15495 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
15496 {
15497 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15498 	pci_ers_result_t result;
15499 	u32 reg;
15500 
15501 	dev_dbg(&pdev->dev, "%s\n", __func__);
15502 	if (pci_enable_device_mem(pdev)) {
15503 		dev_info(&pdev->dev,
15504 			 "Cannot re-enable PCI device after reset.\n");
15505 		result = PCI_ERS_RESULT_DISCONNECT;
15506 	} else {
15507 		pci_set_master(pdev);
15508 		pci_restore_state(pdev);
15509 		pci_save_state(pdev);
15510 		pci_wake_from_d3(pdev, false);
15511 
15512 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
15513 		if (reg == 0)
15514 			result = PCI_ERS_RESULT_RECOVERED;
15515 		else
15516 			result = PCI_ERS_RESULT_DISCONNECT;
15517 	}
15518 
15519 	return result;
15520 }
15521 
15522 /**
15523  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
15524  * @pdev: PCI device information struct
15525  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)15526 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
15527 {
15528 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15529 
15530 	i40e_prep_for_reset(pf, false);
15531 }
15532 
15533 /**
15534  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
15535  * @pdev: PCI device information struct
15536  */
i40e_pci_error_reset_done(struct pci_dev * pdev)15537 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
15538 {
15539 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15540 
15541 	i40e_reset_and_rebuild(pf, false, false);
15542 }
15543 
15544 /**
15545  * i40e_pci_error_resume - restart operations after PCI error recovery
15546  * @pdev: PCI device information struct
15547  *
15548  * Called to allow the driver to bring things back up after PCI error
15549  * and/or reset recovery has finished.
15550  **/
i40e_pci_error_resume(struct pci_dev * pdev)15551 static void i40e_pci_error_resume(struct pci_dev *pdev)
15552 {
15553 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15554 
15555 	dev_dbg(&pdev->dev, "%s\n", __func__);
15556 	if (test_bit(__I40E_SUSPENDED, pf->state))
15557 		return;
15558 
15559 	i40e_handle_reset_warning(pf, false);
15560 }
15561 
15562 /**
15563  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
15564  * using the mac_address_write admin q function
15565  * @pf: pointer to i40e_pf struct
15566  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)15567 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
15568 {
15569 	struct i40e_hw *hw = &pf->hw;
15570 	i40e_status ret;
15571 	u8 mac_addr[6];
15572 	u16 flags = 0;
15573 
15574 	/* Get current MAC address in case it's an LAA */
15575 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
15576 		ether_addr_copy(mac_addr,
15577 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
15578 	} else {
15579 		dev_err(&pf->pdev->dev,
15580 			"Failed to retrieve MAC address; using default\n");
15581 		ether_addr_copy(mac_addr, hw->mac.addr);
15582 	}
15583 
15584 	/* The FW expects the mac address write cmd to first be called with
15585 	 * one of these flags before calling it again with the multicast
15586 	 * enable flags.
15587 	 */
15588 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
15589 
15590 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
15591 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
15592 
15593 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15594 	if (ret) {
15595 		dev_err(&pf->pdev->dev,
15596 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
15597 		return;
15598 	}
15599 
15600 	flags = I40E_AQC_MC_MAG_EN
15601 			| I40E_AQC_WOL_PRESERVE_ON_PFR
15602 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
15603 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15604 	if (ret)
15605 		dev_err(&pf->pdev->dev,
15606 			"Failed to enable Multicast Magic Packet wake up\n");
15607 }
15608 
15609 /**
15610  * i40e_shutdown - PCI callback for shutting down
15611  * @pdev: PCI device information struct
15612  **/
i40e_shutdown(struct pci_dev * pdev)15613 static void i40e_shutdown(struct pci_dev *pdev)
15614 {
15615 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15616 	struct i40e_hw *hw = &pf->hw;
15617 
15618 	set_bit(__I40E_SUSPENDED, pf->state);
15619 	set_bit(__I40E_DOWN, pf->state);
15620 
15621 	del_timer_sync(&pf->service_timer);
15622 	cancel_work_sync(&pf->service_task);
15623 	i40e_cloud_filter_exit(pf);
15624 	i40e_fdir_teardown(pf);
15625 
15626 	/* Client close must be called explicitly here because the timer
15627 	 * has been stopped.
15628 	 */
15629 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15630 
15631 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15632 		i40e_enable_mc_magic_wake(pf);
15633 
15634 	i40e_prep_for_reset(pf, false);
15635 
15636 	wr32(hw, I40E_PFPM_APM,
15637 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15638 	wr32(hw, I40E_PFPM_WUFC,
15639 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15640 
15641 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15642 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15643 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15644 		free_irq(pf->pdev->irq, pf);
15645 
15646 	/* Since we're going to destroy queues during the
15647 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15648 	 * whole section
15649 	 */
15650 	rtnl_lock();
15651 	i40e_clear_interrupt_scheme(pf);
15652 	rtnl_unlock();
15653 
15654 	if (system_state == SYSTEM_POWER_OFF) {
15655 		pci_wake_from_d3(pdev, pf->wol_en);
15656 		pci_set_power_state(pdev, PCI_D3hot);
15657 	}
15658 }
15659 
15660 /**
15661  * i40e_suspend - PM callback for moving to D3
15662  * @dev: generic device information structure
15663  **/
i40e_suspend(struct device * dev)15664 static int __maybe_unused i40e_suspend(struct device *dev)
15665 {
15666 	struct i40e_pf *pf = dev_get_drvdata(dev);
15667 	struct i40e_hw *hw = &pf->hw;
15668 
15669 	/* If we're already suspended, then there is nothing to do */
15670 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
15671 		return 0;
15672 
15673 	set_bit(__I40E_DOWN, pf->state);
15674 
15675 	/* Ensure service task will not be running */
15676 	del_timer_sync(&pf->service_timer);
15677 	cancel_work_sync(&pf->service_task);
15678 
15679 	/* Client close must be called explicitly here because the timer
15680 	 * has been stopped.
15681 	 */
15682 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15683 
15684 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15685 		i40e_enable_mc_magic_wake(pf);
15686 
15687 	/* Since we're going to destroy queues during the
15688 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15689 	 * whole section
15690 	 */
15691 	rtnl_lock();
15692 
15693 	i40e_prep_for_reset(pf, true);
15694 
15695 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15696 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15697 
15698 	/* Clear the interrupt scheme and release our IRQs so that the system
15699 	 * can safely hibernate even when there are a large number of CPUs.
15700 	 * Otherwise hibernation might fail when mapping all the vectors back
15701 	 * to CPU0.
15702 	 */
15703 	i40e_clear_interrupt_scheme(pf);
15704 
15705 	rtnl_unlock();
15706 
15707 	return 0;
15708 }
15709 
15710 /**
15711  * i40e_resume - PM callback for waking up from D3
15712  * @dev: generic device information structure
15713  **/
i40e_resume(struct device * dev)15714 static int __maybe_unused i40e_resume(struct device *dev)
15715 {
15716 	struct i40e_pf *pf = dev_get_drvdata(dev);
15717 	int err;
15718 
15719 	/* If we're not suspended, then there is nothing to do */
15720 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15721 		return 0;
15722 
15723 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
15724 	 * since we're going to be restoring queues
15725 	 */
15726 	rtnl_lock();
15727 
15728 	/* We cleared the interrupt scheme when we suspended, so we need to
15729 	 * restore it now to resume device functionality.
15730 	 */
15731 	err = i40e_restore_interrupt_scheme(pf);
15732 	if (err) {
15733 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
15734 			err);
15735 	}
15736 
15737 	clear_bit(__I40E_DOWN, pf->state);
15738 	i40e_reset_and_rebuild(pf, false, true);
15739 
15740 	rtnl_unlock();
15741 
15742 	/* Clear suspended state last after everything is recovered */
15743 	clear_bit(__I40E_SUSPENDED, pf->state);
15744 
15745 	/* Restart the service task */
15746 	mod_timer(&pf->service_timer,
15747 		  round_jiffies(jiffies + pf->service_timer_period));
15748 
15749 	return 0;
15750 }
15751 
15752 static const struct pci_error_handlers i40e_err_handler = {
15753 	.error_detected = i40e_pci_error_detected,
15754 	.slot_reset = i40e_pci_error_slot_reset,
15755 	.reset_prepare = i40e_pci_error_reset_prepare,
15756 	.reset_done = i40e_pci_error_reset_done,
15757 	.resume = i40e_pci_error_resume,
15758 };
15759 
15760 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
15761 
15762 static struct pci_driver i40e_driver = {
15763 	.name     = i40e_driver_name,
15764 	.id_table = i40e_pci_tbl,
15765 	.probe    = i40e_probe,
15766 	.remove   = i40e_remove,
15767 	.driver   = {
15768 		.pm = &i40e_pm_ops,
15769 	},
15770 	.shutdown = i40e_shutdown,
15771 	.err_handler = &i40e_err_handler,
15772 	.sriov_configure = i40e_pci_sriov_configure,
15773 };
15774 
15775 /**
15776  * i40e_init_module - Driver registration routine
15777  *
15778  * i40e_init_module is the first routine called when the driver is
15779  * loaded. All it does is register with the PCI subsystem.
15780  **/
i40e_init_module(void)15781 static int __init i40e_init_module(void)
15782 {
15783 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
15784 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
15785 
15786 	/* There is no need to throttle the number of active tasks because
15787 	 * each device limits its own task using a state bit for scheduling
15788 	 * the service task, and the device tasks do not interfere with each
15789 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
15790 	 * since we need to be able to guarantee forward progress even under
15791 	 * memory pressure.
15792 	 */
15793 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
15794 	if (!i40e_wq) {
15795 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
15796 		return -ENOMEM;
15797 	}
15798 
15799 	i40e_dbg_init();
15800 	return pci_register_driver(&i40e_driver);
15801 }
15802 module_init(i40e_init_module);
15803 
15804 /**
15805  * i40e_exit_module - Driver exit cleanup routine
15806  *
15807  * i40e_exit_module is called just before the driver is removed
15808  * from memory.
15809  **/
i40e_exit_module(void)15810 static void __exit i40e_exit_module(void)
15811 {
15812 	pci_unregister_driver(&i40e_driver);
15813 	destroy_workqueue(i40e_wq);
15814 	i40e_dbg_exit();
15815 }
15816 module_exit(i40e_exit_module);
15817