xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision dbf8fe85a16a33d6b6bd01f2bc606fc017771465)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <generated/utsrelease.h>
5 #include <linux/crash_dump.h>
6 #include <linux/net/intel/libie/pctype.h>
7 #include <linux/if_bridge.h>
8 #include <linux/if_macvlan.h>
9 #include <linux/module.h>
10 #include <net/pkt_cls.h>
11 #include <net/xdp_sock_drv.h>
12 
13 /* Local includes */
14 #include "i40e.h"
15 #include "i40e_devids.h"
16 #include "i40e_diag.h"
17 #include "i40e_lan_hmc.h"
18 #include "i40e_virtchnl_pf.h"
19 #include "i40e_xsk.h"
20 
21 /* All i40e tracepoints are defined by the include below, which
22  * must be included exactly once across the whole kernel with
23  * CREATE_TRACE_POINTS defined
24  */
25 #define CREATE_TRACE_POINTS
26 #include "i40e_trace.h"
27 
28 const char i40e_driver_name[] = "i40e";
29 static const char i40e_driver_string[] =
30 			"Intel(R) Ethernet Connection XL710 Network Driver";
31 
32 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
33 
34 /* a bit of forward declarations */
35 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
36 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
37 static int i40e_add_vsi(struct i40e_vsi *vsi);
38 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
39 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
40 static int i40e_setup_misc_vector(struct i40e_pf *pf);
41 static void i40e_determine_queue_usage(struct i40e_pf *pf);
42 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
43 static void i40e_prep_for_reset(struct i40e_pf *pf);
44 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
45 				   bool lock_acquired);
46 static int i40e_reset(struct i40e_pf *pf);
47 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
48 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
49 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
50 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
51 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
52 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
53 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
54 static int i40e_get_capabilities(struct i40e_pf *pf,
55 				 enum i40e_admin_queue_opc list_type);
56 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
57 
58 /* i40e_pci_tbl - PCI Device ID Table
59  *
60  * Last entry must be all 0s
61  *
62  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
63  *   Class, Class Mask, private data (not used) }
64  */
65 static const struct pci_device_id i40e_pci_tbl[] = {
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
91 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
92 	/* required last entry */
93 	{0, }
94 };
95 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
96 
97 #define I40E_MAX_VF_COUNT 128
98 static int debug = -1;
99 module_param(debug, uint, 0);
100 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
101 
102 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103 MODULE_IMPORT_NS("LIBIE");
104 MODULE_IMPORT_NS("LIBIE_ADMINQ");
105 MODULE_LICENSE("GPL v2");
106 
107 static struct workqueue_struct *i40e_wq;
108 
netdev_hw_addr_refcnt(struct i40e_mac_filter * f,struct net_device * netdev,int delta)109 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
110 				  struct net_device *netdev, int delta)
111 {
112 	struct netdev_hw_addr_list *ha_list;
113 	struct netdev_hw_addr *ha;
114 
115 	if (!f || !netdev)
116 		return;
117 
118 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
119 		ha_list = &netdev->uc;
120 	else
121 		ha_list = &netdev->mc;
122 
123 	netdev_hw_addr_list_for_each(ha, ha_list) {
124 		if (ether_addr_equal(ha->addr, f->macaddr)) {
125 			ha->refcount += delta;
126 			if (ha->refcount <= 0)
127 				ha->refcount = 1;
128 			break;
129 		}
130 	}
131 }
132 
133 /**
134  * i40e_hw_to_dev - get device pointer from the hardware structure
135  * @hw: pointer to the device HW structure
136  **/
i40e_hw_to_dev(struct i40e_hw * hw)137 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
138 {
139 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
140 
141 	return &pf->pdev->dev;
142 }
143 
144 /**
145  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
146  * @hw:   pointer to the HW structure
147  * @mem:  ptr to mem struct to fill out
148  * @size: size of memory requested
149  * @alignment: what to align the allocation to
150  **/
i40e_allocate_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem,u64 size,u32 alignment)151 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
152 			  u64 size, u32 alignment)
153 {
154 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
155 
156 	mem->size = ALIGN(size, alignment);
157 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
158 				     GFP_KERNEL);
159 	if (!mem->va)
160 		return -ENOMEM;
161 
162 	return 0;
163 }
164 
165 /**
166  * i40e_free_dma_mem - OS specific memory free for shared code
167  * @hw:   pointer to the HW structure
168  * @mem:  ptr to mem struct to free
169  **/
i40e_free_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem)170 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
171 {
172 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
173 
174 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
175 	mem->va = NULL;
176 	mem->pa = 0;
177 	mem->size = 0;
178 
179 	return 0;
180 }
181 
182 /**
183  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
184  * @hw:   pointer to the HW structure
185  * @mem:  ptr to mem struct to fill out
186  * @size: size of memory requested
187  **/
i40e_allocate_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)188 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
189 			   u32 size)
190 {
191 	mem->size = size;
192 	mem->va = kzalloc(size, GFP_KERNEL);
193 
194 	if (!mem->va)
195 		return -ENOMEM;
196 
197 	return 0;
198 }
199 
200 /**
201  * i40e_free_virt_mem - OS specific memory free for shared code
202  * @hw:   pointer to the HW structure
203  * @mem:  ptr to mem struct to free
204  **/
i40e_free_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem)205 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
206 {
207 	/* it's ok to kfree a NULL pointer */
208 	kfree(mem->va);
209 	mem->va = NULL;
210 	mem->size = 0;
211 
212 	return 0;
213 }
214 
215 /**
216  * i40e_get_lump - find a lump of free generic resource
217  * @pf: board private structure
218  * @pile: the pile of resource to search
219  * @needed: the number of items needed
220  * @id: an owner id to stick on the items assigned
221  *
222  * Returns the base item index of the lump, or negative for error
223  **/
i40e_get_lump(struct i40e_pf * pf,struct i40e_lump_tracking * pile,u16 needed,u16 id)224 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
225 			 u16 needed, u16 id)
226 {
227 	int ret = -ENOMEM;
228 	int i, j;
229 
230 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
231 		dev_info(&pf->pdev->dev,
232 			 "param err: pile=%s needed=%d id=0x%04x\n",
233 			 pile ? "<valid>" : "<null>", needed, id);
234 		return -EINVAL;
235 	}
236 
237 	/* Allocate last queue in the pile for FDIR VSI queue
238 	 * so it doesn't fragment the qp_pile
239 	 */
240 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
241 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
242 			dev_err(&pf->pdev->dev,
243 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
244 				pile->num_entries - 1);
245 			return -ENOMEM;
246 		}
247 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
248 		return pile->num_entries - 1;
249 	}
250 
251 	i = 0;
252 	while (i < pile->num_entries) {
253 		/* skip already allocated entries */
254 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
255 			i++;
256 			continue;
257 		}
258 
259 		/* do we have enough in this lump? */
260 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
261 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
262 				break;
263 		}
264 
265 		if (j == needed) {
266 			/* there was enough, so assign it to the requestor */
267 			for (j = 0; j < needed; j++)
268 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
269 			ret = i;
270 			break;
271 		}
272 
273 		/* not enough, so skip over it and continue looking */
274 		i += j;
275 	}
276 
277 	return ret;
278 }
279 
280 /**
281  * i40e_put_lump - return a lump of generic resource
282  * @pile: the pile of resource to search
283  * @index: the base item index
284  * @id: the owner id of the items assigned
285  *
286  * Returns the count of items in the lump
287  **/
i40e_put_lump(struct i40e_lump_tracking * pile,u16 index,u16 id)288 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
289 {
290 	int valid_id = (id | I40E_PILE_VALID_BIT);
291 	int count = 0;
292 	u16 i;
293 
294 	if (!pile || index >= pile->num_entries)
295 		return -EINVAL;
296 
297 	for (i = index;
298 	     i < pile->num_entries && pile->list[i] == valid_id;
299 	     i++) {
300 		pile->list[i] = 0;
301 		count++;
302 	}
303 
304 
305 	return count;
306 }
307 
308 /**
309  * i40e_find_vsi_from_id - searches for the vsi with the given id
310  * @pf: the pf structure to search for the vsi
311  * @id: id of the vsi it is searching for
312  **/
i40e_find_vsi_from_id(struct i40e_pf * pf,u16 id)313 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
314 {
315 	struct i40e_vsi *vsi;
316 	int i;
317 
318 	i40e_pf_for_each_vsi(pf, i, vsi)
319 		if (vsi->id == id)
320 			return vsi;
321 
322 	return NULL;
323 }
324 
325 /**
326  * i40e_service_event_schedule - Schedule the service task to wake up
327  * @pf: board private structure
328  *
329  * If not already scheduled, this puts the task into the work queue
330  **/
i40e_service_event_schedule(struct i40e_pf * pf)331 void i40e_service_event_schedule(struct i40e_pf *pf)
332 {
333 	if ((!test_bit(__I40E_DOWN, pf->state) &&
334 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
335 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
336 		queue_work(i40e_wq, &pf->service_task);
337 }
338 
339 /**
340  * i40e_tx_timeout - Respond to a Tx Hang
341  * @netdev: network interface device structure
342  * @txqueue: queue number timing out
343  *
344  * If any port has noticed a Tx timeout, it is likely that the whole
345  * device is munged, not just the one netdev port, so go for the full
346  * reset.
347  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)348 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
349 {
350 	struct i40e_netdev_priv *np = netdev_priv(netdev);
351 	struct i40e_vsi *vsi = np->vsi;
352 	struct i40e_pf *pf = vsi->back;
353 	struct i40e_ring *tx_ring = NULL;
354 	unsigned int i;
355 	u32 head, val;
356 
357 	pf->tx_timeout_count++;
358 
359 	/* with txqueue index, find the tx_ring struct */
360 	for (i = 0; i < vsi->num_queue_pairs; i++) {
361 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
362 			if (txqueue ==
363 			    vsi->tx_rings[i]->queue_index) {
364 				tx_ring = vsi->tx_rings[i];
365 				break;
366 			}
367 		}
368 	}
369 
370 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
371 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
372 	else if (time_before(jiffies,
373 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
374 		return;   /* don't do any new action before the next timeout */
375 
376 	/* don't kick off another recovery if one is already pending */
377 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
378 		return;
379 
380 	if (tx_ring) {
381 		head = i40e_get_head(tx_ring);
382 		/* Read interrupt register */
383 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
384 			val = rd32(&pf->hw,
385 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
386 						tx_ring->vsi->base_vector - 1));
387 		else
388 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
389 
390 		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",
391 			    vsi->seid, txqueue, tx_ring->next_to_clean,
392 			    head, tx_ring->next_to_use,
393 			    readl(tx_ring->tail), val);
394 	}
395 
396 	pf->tx_timeout_last_recovery = jiffies;
397 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
398 		    pf->tx_timeout_recovery_level, txqueue);
399 
400 	switch (pf->tx_timeout_recovery_level) {
401 	case 1:
402 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
403 		break;
404 	case 2:
405 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
406 		break;
407 	case 3:
408 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
409 		break;
410 	default:
411 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
412 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
413 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
414 		break;
415 	}
416 
417 	i40e_service_event_schedule(pf);
418 	pf->tx_timeout_recovery_level++;
419 }
420 
421 /**
422  * i40e_get_vsi_stats_struct - Get System Network Statistics
423  * @vsi: the VSI we care about
424  *
425  * Returns the address of the device statistics structure.
426  * The statistics are actually updated from the service task.
427  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)428 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
429 {
430 	return &vsi->net_stats;
431 }
432 
433 /**
434  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
435  * @ring: Tx ring to get statistics from
436  * @stats: statistics entry to be updated
437  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)438 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
439 					    struct rtnl_link_stats64 *stats)
440 {
441 	u64 bytes, packets;
442 	unsigned int start;
443 
444 	do {
445 		start = u64_stats_fetch_begin(&ring->syncp);
446 		packets = ring->stats.packets;
447 		bytes   = ring->stats.bytes;
448 	} while (u64_stats_fetch_retry(&ring->syncp, start));
449 
450 	stats->tx_packets += packets;
451 	stats->tx_bytes   += bytes;
452 }
453 
454 /**
455  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
456  * @netdev: network interface device structure
457  * @stats: data structure to store statistics
458  *
459  * Returns the address of the device statistics structure.
460  * The statistics are actually updated from the service task.
461  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)462 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
463 				  struct rtnl_link_stats64 *stats)
464 {
465 	struct i40e_netdev_priv *np = netdev_priv(netdev);
466 	struct i40e_vsi *vsi = np->vsi;
467 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
468 	struct i40e_ring *ring;
469 	int i;
470 
471 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
472 		return;
473 
474 	if (!vsi->tx_rings)
475 		return;
476 
477 	rcu_read_lock();
478 	for (i = 0; i < vsi->num_queue_pairs; i++) {
479 		u64 bytes, packets;
480 		unsigned int start;
481 
482 		ring = READ_ONCE(vsi->tx_rings[i]);
483 		if (!ring)
484 			continue;
485 		i40e_get_netdev_stats_struct_tx(ring, stats);
486 
487 		if (i40e_enabled_xdp_vsi(vsi)) {
488 			ring = READ_ONCE(vsi->xdp_rings[i]);
489 			if (!ring)
490 				continue;
491 			i40e_get_netdev_stats_struct_tx(ring, stats);
492 		}
493 
494 		ring = READ_ONCE(vsi->rx_rings[i]);
495 		if (!ring)
496 			continue;
497 		do {
498 			start   = u64_stats_fetch_begin(&ring->syncp);
499 			packets = ring->stats.packets;
500 			bytes   = ring->stats.bytes;
501 		} while (u64_stats_fetch_retry(&ring->syncp, start));
502 
503 		stats->rx_packets += packets;
504 		stats->rx_bytes   += bytes;
505 
506 	}
507 	rcu_read_unlock();
508 
509 	/* following stats updated by i40e_watchdog_subtask() */
510 	stats->multicast	= vsi_stats->multicast;
511 	stats->tx_errors	= vsi_stats->tx_errors;
512 	stats->tx_dropped	= vsi_stats->tx_dropped;
513 	stats->rx_errors	= vsi_stats->rx_errors;
514 	stats->rx_dropped	= vsi_stats->rx_dropped;
515 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
516 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
517 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
518 }
519 
520 /**
521  * i40e_vsi_reset_stats - Resets all stats of the given vsi
522  * @vsi: the VSI to have its stats reset
523  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)524 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
525 {
526 	struct rtnl_link_stats64 *ns;
527 	int i;
528 
529 	if (!vsi)
530 		return;
531 
532 	ns = i40e_get_vsi_stats_struct(vsi);
533 	memset(ns, 0, sizeof(*ns));
534 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
535 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
536 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
537 	if (vsi->rx_rings && vsi->rx_rings[0]) {
538 		for (i = 0; i < vsi->num_queue_pairs; i++) {
539 			memset(&vsi->rx_rings[i]->stats, 0,
540 			       sizeof(vsi->rx_rings[i]->stats));
541 			memset(&vsi->rx_rings[i]->rx_stats, 0,
542 			       sizeof(vsi->rx_rings[i]->rx_stats));
543 			memset(&vsi->tx_rings[i]->stats, 0,
544 			       sizeof(vsi->tx_rings[i]->stats));
545 			memset(&vsi->tx_rings[i]->tx_stats, 0,
546 			       sizeof(vsi->tx_rings[i]->tx_stats));
547 		}
548 	}
549 	vsi->stat_offsets_loaded = false;
550 }
551 
552 /**
553  * i40e_pf_reset_stats - Reset all of the stats for the given PF
554  * @pf: the PF to be reset
555  **/
i40e_pf_reset_stats(struct i40e_pf * pf)556 void i40e_pf_reset_stats(struct i40e_pf *pf)
557 {
558 	struct i40e_veb *veb;
559 	int i;
560 
561 	memset(&pf->stats, 0, sizeof(pf->stats));
562 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
563 	pf->stat_offsets_loaded = false;
564 
565 	i40e_pf_for_each_veb(pf, i, veb) {
566 		memset(&veb->stats, 0, sizeof(veb->stats));
567 		memset(&veb->stats_offsets, 0, sizeof(veb->stats_offsets));
568 		memset(&veb->tc_stats, 0, sizeof(veb->tc_stats));
569 		memset(&veb->tc_stats_offsets, 0, sizeof(veb->tc_stats_offsets));
570 		veb->stat_offsets_loaded = false;
571 	}
572 	pf->hw_csum_rx_error = 0;
573 }
574 
575 /**
576  * i40e_compute_pci_to_hw_id - compute index form PCI function.
577  * @vsi: ptr to the VSI to read from.
578  * @hw: ptr to the hardware info.
579  **/
i40e_compute_pci_to_hw_id(struct i40e_vsi * vsi,struct i40e_hw * hw)580 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
581 {
582 	int pf_count = i40e_get_pf_count(hw);
583 
584 	if (vsi->type == I40E_VSI_SRIOV)
585 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
586 
587 	return hw->port + BIT(7);
588 }
589 
590 /**
591  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
592  * @hw: ptr to the hardware info.
593  * @hireg: the high 32 bit reg to read.
594  * @loreg: the low 32 bit reg to read.
595  * @offset_loaded: has the initial offset been loaded yet.
596  * @offset: ptr to current offset value.
597  * @stat: ptr to the stat.
598  *
599  * Since the device stats are not reset at PFReset, they will not
600  * be zeroed when the driver starts.  We'll save the first values read
601  * and use them as offsets to be subtracted from the raw values in order
602  * to report stats that count from zero.
603  **/
i40e_stat_update64(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)604 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
605 			       bool offset_loaded, u64 *offset, u64 *stat)
606 {
607 	u64 new_data;
608 
609 	new_data = rd64(hw, loreg);
610 
611 	if (!offset_loaded || new_data < *offset)
612 		*offset = new_data;
613 	*stat = new_data - *offset;
614 }
615 
616 /**
617  * i40e_stat_update48 - read and update a 48 bit stat from the chip
618  * @hw: ptr to the hardware info
619  * @hireg: the high 32 bit reg to read
620  * @loreg: the low 32 bit reg to read
621  * @offset_loaded: has the initial offset been loaded yet
622  * @offset: ptr to current offset value
623  * @stat: ptr to the stat
624  *
625  * Since the device stats are not reset at PFReset, they likely will not
626  * be zeroed when the driver starts.  We'll save the first values read
627  * and use them as offsets to be subtracted from the raw values in order
628  * to report stats that count from zero.  In the process, we also manage
629  * the potential roll-over.
630  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)631 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
632 			       bool offset_loaded, u64 *offset, u64 *stat)
633 {
634 	u64 new_data;
635 
636 	if (hw->device_id == I40E_DEV_ID_QEMU) {
637 		new_data = rd32(hw, loreg);
638 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
639 	} else {
640 		new_data = rd64(hw, loreg);
641 	}
642 	if (!offset_loaded)
643 		*offset = new_data;
644 	if (likely(new_data >= *offset))
645 		*stat = new_data - *offset;
646 	else
647 		*stat = (new_data + BIT_ULL(48)) - *offset;
648 	*stat &= 0xFFFFFFFFFFFFULL;
649 }
650 
651 /**
652  * i40e_stat_update32 - read and update a 32 bit stat from the chip
653  * @hw: ptr to the hardware info
654  * @reg: the hw reg to read
655  * @offset_loaded: has the initial offset been loaded yet
656  * @offset: ptr to current offset value
657  * @stat: ptr to the stat
658  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)659 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
660 			       bool offset_loaded, u64 *offset, u64 *stat)
661 {
662 	u32 new_data;
663 
664 	new_data = rd32(hw, reg);
665 	if (!offset_loaded)
666 		*offset = new_data;
667 	if (likely(new_data >= *offset))
668 		*stat = (u32)(new_data - *offset);
669 	else
670 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
671 }
672 
673 /**
674  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
675  * @hw: ptr to the hardware info
676  * @reg: the hw reg to read and clear
677  * @stat: ptr to the stat
678  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)679 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
680 {
681 	u32 new_data = rd32(hw, reg);
682 
683 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
684 	*stat += new_data;
685 }
686 
687 /**
688  * i40e_stats_update_rx_discards - update rx_discards.
689  * @vsi: ptr to the VSI to be updated.
690  * @hw: ptr to the hardware info.
691  * @stat_idx: VSI's stat_counter_idx.
692  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
693  * @stat_offset: ptr to stat_offset to store first read of specific register.
694  * @stat: ptr to VSI's stat to be updated.
695  **/
696 static void
i40e_stats_update_rx_discards(struct i40e_vsi * vsi,struct i40e_hw * hw,int stat_idx,bool offset_loaded,struct i40e_eth_stats * stat_offset,struct i40e_eth_stats * stat)697 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
698 			      int stat_idx, bool offset_loaded,
699 			      struct i40e_eth_stats *stat_offset,
700 			      struct i40e_eth_stats *stat)
701 {
702 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
703 			   &stat_offset->rx_discards, &stat->rx_discards);
704 	i40e_stat_update64(hw,
705 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
706 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
707 			   offset_loaded, &stat_offset->rx_discards_other,
708 			   &stat->rx_discards_other);
709 }
710 
711 /**
712  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
713  * @vsi: the VSI to be updated
714  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)715 void i40e_update_eth_stats(struct i40e_vsi *vsi)
716 {
717 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
718 	struct i40e_pf *pf = vsi->back;
719 	struct i40e_hw *hw = &pf->hw;
720 	struct i40e_eth_stats *oes;
721 	struct i40e_eth_stats *es;     /* device's eth stats */
722 
723 	es = &vsi->eth_stats;
724 	oes = &vsi->eth_stats_offsets;
725 
726 	/* Gather up the stats that the hw collects */
727 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
728 			   vsi->stat_offsets_loaded,
729 			   &oes->tx_errors, &es->tx_errors);
730 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
731 			   vsi->stat_offsets_loaded,
732 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
733 
734 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
735 			   I40E_GLV_GORCL(stat_idx),
736 			   vsi->stat_offsets_loaded,
737 			   &oes->rx_bytes, &es->rx_bytes);
738 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
739 			   I40E_GLV_UPRCL(stat_idx),
740 			   vsi->stat_offsets_loaded,
741 			   &oes->rx_unicast, &es->rx_unicast);
742 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
743 			   I40E_GLV_MPRCL(stat_idx),
744 			   vsi->stat_offsets_loaded,
745 			   &oes->rx_multicast, &es->rx_multicast);
746 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
747 			   I40E_GLV_BPRCL(stat_idx),
748 			   vsi->stat_offsets_loaded,
749 			   &oes->rx_broadcast, &es->rx_broadcast);
750 
751 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
752 			   I40E_GLV_GOTCL(stat_idx),
753 			   vsi->stat_offsets_loaded,
754 			   &oes->tx_bytes, &es->tx_bytes);
755 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
756 			   I40E_GLV_UPTCL(stat_idx),
757 			   vsi->stat_offsets_loaded,
758 			   &oes->tx_unicast, &es->tx_unicast);
759 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
760 			   I40E_GLV_MPTCL(stat_idx),
761 			   vsi->stat_offsets_loaded,
762 			   &oes->tx_multicast, &es->tx_multicast);
763 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
764 			   I40E_GLV_BPTCL(stat_idx),
765 			   vsi->stat_offsets_loaded,
766 			   &oes->tx_broadcast, &es->tx_broadcast);
767 
768 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
769 				      vsi->stat_offsets_loaded, oes, es);
770 
771 	vsi->stat_offsets_loaded = true;
772 }
773 
774 /**
775  * i40e_update_veb_stats - Update Switch component statistics
776  * @veb: the VEB being updated
777  **/
i40e_update_veb_stats(struct i40e_veb * veb)778 void i40e_update_veb_stats(struct i40e_veb *veb)
779 {
780 	struct i40e_pf *pf = veb->pf;
781 	struct i40e_hw *hw = &pf->hw;
782 	struct i40e_eth_stats *oes;
783 	struct i40e_eth_stats *es;     /* device's eth stats */
784 	struct i40e_veb_tc_stats *veb_oes;
785 	struct i40e_veb_tc_stats *veb_es;
786 	int i, idx = 0;
787 
788 	idx = veb->stats_idx;
789 	es = &veb->stats;
790 	oes = &veb->stats_offsets;
791 	veb_es = &veb->tc_stats;
792 	veb_oes = &veb->tc_stats_offsets;
793 
794 	/* Gather up the stats that the hw collects */
795 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
796 			   veb->stat_offsets_loaded,
797 			   &oes->tx_discards, &es->tx_discards);
798 	if (hw->revision_id > 0)
799 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
800 				   veb->stat_offsets_loaded,
801 				   &oes->rx_unknown_protocol,
802 				   &es->rx_unknown_protocol);
803 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
804 			   veb->stat_offsets_loaded,
805 			   &oes->rx_bytes, &es->rx_bytes);
806 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
807 			   veb->stat_offsets_loaded,
808 			   &oes->rx_unicast, &es->rx_unicast);
809 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
810 			   veb->stat_offsets_loaded,
811 			   &oes->rx_multicast, &es->rx_multicast);
812 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
813 			   veb->stat_offsets_loaded,
814 			   &oes->rx_broadcast, &es->rx_broadcast);
815 
816 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
817 			   veb->stat_offsets_loaded,
818 			   &oes->tx_bytes, &es->tx_bytes);
819 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
820 			   veb->stat_offsets_loaded,
821 			   &oes->tx_unicast, &es->tx_unicast);
822 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
823 			   veb->stat_offsets_loaded,
824 			   &oes->tx_multicast, &es->tx_multicast);
825 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
826 			   veb->stat_offsets_loaded,
827 			   &oes->tx_broadcast, &es->tx_broadcast);
828 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
829 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
830 				   I40E_GLVEBTC_RPCL(i, idx),
831 				   veb->stat_offsets_loaded,
832 				   &veb_oes->tc_rx_packets[i],
833 				   &veb_es->tc_rx_packets[i]);
834 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
835 				   I40E_GLVEBTC_RBCL(i, idx),
836 				   veb->stat_offsets_loaded,
837 				   &veb_oes->tc_rx_bytes[i],
838 				   &veb_es->tc_rx_bytes[i]);
839 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
840 				   I40E_GLVEBTC_TPCL(i, idx),
841 				   veb->stat_offsets_loaded,
842 				   &veb_oes->tc_tx_packets[i],
843 				   &veb_es->tc_tx_packets[i]);
844 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
845 				   I40E_GLVEBTC_TBCL(i, idx),
846 				   veb->stat_offsets_loaded,
847 				   &veb_oes->tc_tx_bytes[i],
848 				   &veb_es->tc_tx_bytes[i]);
849 	}
850 	veb->stat_offsets_loaded = true;
851 }
852 
853 /**
854  * i40e_update_vsi_stats - Update the vsi statistics counters.
855  * @vsi: the VSI to be updated
856  *
857  * There are a few instances where we store the same stat in a
858  * couple of different structs.  This is partly because we have
859  * the netdev stats that need to be filled out, which is slightly
860  * different from the "eth_stats" defined by the chip and used in
861  * VF communications.  We sort it out here.
862  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)863 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
864 {
865 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
866 	struct i40e_pf *pf = vsi->back;
867 	struct rtnl_link_stats64 *ons;
868 	struct rtnl_link_stats64 *ns;   /* netdev stats */
869 	struct i40e_eth_stats *oes;
870 	struct i40e_eth_stats *es;     /* device's eth stats */
871 	u64 tx_restart, tx_busy;
872 	struct i40e_ring *p;
873 	u64 bytes, packets;
874 	unsigned int start;
875 	u64 tx_linearize;
876 	u64 tx_force_wb;
877 	u64 tx_stopped;
878 	u64 rx_p, rx_b;
879 	u64 tx_p, tx_b;
880 	u16 q;
881 
882 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
883 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
884 		return;
885 
886 	ns = i40e_get_vsi_stats_struct(vsi);
887 	ons = &vsi->net_stats_offsets;
888 	es = &vsi->eth_stats;
889 	oes = &vsi->eth_stats_offsets;
890 
891 	/* Gather up the netdev and vsi stats that the driver collects
892 	 * on the fly during packet processing
893 	 */
894 	rx_b = rx_p = 0;
895 	tx_b = tx_p = 0;
896 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
897 	tx_stopped = 0;
898 	rx_page = 0;
899 	rx_buf = 0;
900 	rx_reuse = 0;
901 	rx_alloc = 0;
902 	rx_waive = 0;
903 	rx_busy = 0;
904 	rcu_read_lock();
905 	for (q = 0; q < vsi->num_queue_pairs; q++) {
906 		/* locate Tx ring */
907 		p = READ_ONCE(vsi->tx_rings[q]);
908 		if (!p)
909 			continue;
910 
911 		do {
912 			start = u64_stats_fetch_begin(&p->syncp);
913 			packets = p->stats.packets;
914 			bytes = p->stats.bytes;
915 		} while (u64_stats_fetch_retry(&p->syncp, start));
916 		tx_b += bytes;
917 		tx_p += packets;
918 		tx_restart += p->tx_stats.restart_queue;
919 		tx_busy += p->tx_stats.tx_busy;
920 		tx_linearize += p->tx_stats.tx_linearize;
921 		tx_force_wb += p->tx_stats.tx_force_wb;
922 		tx_stopped += p->tx_stats.tx_stopped;
923 
924 		/* locate Rx ring */
925 		p = READ_ONCE(vsi->rx_rings[q]);
926 		if (!p)
927 			continue;
928 
929 		do {
930 			start = u64_stats_fetch_begin(&p->syncp);
931 			packets = p->stats.packets;
932 			bytes = p->stats.bytes;
933 		} while (u64_stats_fetch_retry(&p->syncp, start));
934 		rx_b += bytes;
935 		rx_p += packets;
936 		rx_buf += p->rx_stats.alloc_buff_failed;
937 		rx_page += p->rx_stats.alloc_page_failed;
938 		rx_reuse += p->rx_stats.page_reuse_count;
939 		rx_alloc += p->rx_stats.page_alloc_count;
940 		rx_waive += p->rx_stats.page_waive_count;
941 		rx_busy += p->rx_stats.page_busy_count;
942 
943 		if (i40e_enabled_xdp_vsi(vsi)) {
944 			/* locate XDP ring */
945 			p = READ_ONCE(vsi->xdp_rings[q]);
946 			if (!p)
947 				continue;
948 
949 			do {
950 				start = u64_stats_fetch_begin(&p->syncp);
951 				packets = p->stats.packets;
952 				bytes = p->stats.bytes;
953 			} while (u64_stats_fetch_retry(&p->syncp, start));
954 			tx_b += bytes;
955 			tx_p += packets;
956 			tx_restart += p->tx_stats.restart_queue;
957 			tx_busy += p->tx_stats.tx_busy;
958 			tx_linearize += p->tx_stats.tx_linearize;
959 			tx_force_wb += p->tx_stats.tx_force_wb;
960 		}
961 	}
962 	rcu_read_unlock();
963 	vsi->tx_restart = tx_restart;
964 	vsi->tx_busy = tx_busy;
965 	vsi->tx_linearize = tx_linearize;
966 	vsi->tx_force_wb = tx_force_wb;
967 	vsi->tx_stopped = tx_stopped;
968 	vsi->rx_page_failed = rx_page;
969 	vsi->rx_buf_failed = rx_buf;
970 	vsi->rx_page_reuse = rx_reuse;
971 	vsi->rx_page_alloc = rx_alloc;
972 	vsi->rx_page_waive = rx_waive;
973 	vsi->rx_page_busy = rx_busy;
974 
975 	ns->rx_packets = rx_p;
976 	ns->rx_bytes = rx_b;
977 	ns->tx_packets = tx_p;
978 	ns->tx_bytes = tx_b;
979 
980 	/* update netdev stats from eth stats */
981 	i40e_update_eth_stats(vsi);
982 	ons->tx_errors = oes->tx_errors;
983 	ns->tx_errors = es->tx_errors;
984 	ons->multicast = oes->rx_multicast;
985 	ns->multicast = es->rx_multicast;
986 	ons->rx_dropped = oes->rx_discards_other;
987 	ns->rx_dropped = es->rx_discards_other;
988 	ons->rx_missed_errors = oes->rx_discards;
989 	ns->rx_missed_errors = es->rx_discards;
990 	ons->tx_dropped = oes->tx_discards;
991 	ns->tx_dropped = es->tx_discards;
992 
993 	/* pull in a couple PF stats if this is the main vsi */
994 	if (vsi->type == I40E_VSI_MAIN) {
995 		ns->rx_crc_errors = pf->stats.crc_errors;
996 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
997 		ns->rx_length_errors = pf->stats.rx_length_errors;
998 	}
999 }
1000 
1001 /**
1002  * i40e_update_pf_stats - Update the PF statistics counters.
1003  * @pf: the PF to be updated
1004  **/
i40e_update_pf_stats(struct i40e_pf * pf)1005 static void i40e_update_pf_stats(struct i40e_pf *pf)
1006 {
1007 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1008 	struct i40e_hw_port_stats *nsd = &pf->stats;
1009 	struct i40e_hw *hw = &pf->hw;
1010 	u32 val;
1011 	int i;
1012 
1013 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1014 			   I40E_GLPRT_GORCL(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1017 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1018 			   I40E_GLPRT_GOTCL(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1021 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1022 			   pf->stat_offsets_loaded,
1023 			   &osd->eth.rx_discards,
1024 			   &nsd->eth.rx_discards);
1025 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1026 			   I40E_GLPRT_UPRCL(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->eth.rx_unicast,
1029 			   &nsd->eth.rx_unicast);
1030 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1031 			   I40E_GLPRT_MPRCL(hw->port),
1032 			   pf->stat_offsets_loaded,
1033 			   &osd->eth.rx_multicast,
1034 			   &nsd->eth.rx_multicast);
1035 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1036 			   I40E_GLPRT_BPRCL(hw->port),
1037 			   pf->stat_offsets_loaded,
1038 			   &osd->eth.rx_broadcast,
1039 			   &nsd->eth.rx_broadcast);
1040 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1041 			   I40E_GLPRT_UPTCL(hw->port),
1042 			   pf->stat_offsets_loaded,
1043 			   &osd->eth.tx_unicast,
1044 			   &nsd->eth.tx_unicast);
1045 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1046 			   I40E_GLPRT_MPTCL(hw->port),
1047 			   pf->stat_offsets_loaded,
1048 			   &osd->eth.tx_multicast,
1049 			   &nsd->eth.tx_multicast);
1050 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1051 			   I40E_GLPRT_BPTCL(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->eth.tx_broadcast,
1054 			   &nsd->eth.tx_broadcast);
1055 
1056 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1057 			   pf->stat_offsets_loaded,
1058 			   &osd->tx_dropped_link_down,
1059 			   &nsd->tx_dropped_link_down);
1060 
1061 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1062 			   pf->stat_offsets_loaded,
1063 			   &osd->crc_errors, &nsd->crc_errors);
1064 
1065 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1066 			   pf->stat_offsets_loaded,
1067 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1068 
1069 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->mac_local_faults,
1072 			   &nsd->mac_local_faults);
1073 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1074 			   pf->stat_offsets_loaded,
1075 			   &osd->mac_remote_faults,
1076 			   &nsd->mac_remote_faults);
1077 
1078 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->rx_length_errors,
1081 			   &nsd->rx_length_errors);
1082 
1083 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1084 			   pf->stat_offsets_loaded,
1085 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1086 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1087 			   pf->stat_offsets_loaded,
1088 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1089 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1090 			   pf->stat_offsets_loaded,
1091 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1092 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1093 			   pf->stat_offsets_loaded,
1094 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1095 
1096 	for (i = 0; i < 8; i++) {
1097 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1098 				   pf->stat_offsets_loaded,
1099 				   &osd->priority_xoff_rx[i],
1100 				   &nsd->priority_xoff_rx[i]);
1101 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1102 				   pf->stat_offsets_loaded,
1103 				   &osd->priority_xon_rx[i],
1104 				   &nsd->priority_xon_rx[i]);
1105 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1106 				   pf->stat_offsets_loaded,
1107 				   &osd->priority_xon_tx[i],
1108 				   &nsd->priority_xon_tx[i]);
1109 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1110 				   pf->stat_offsets_loaded,
1111 				   &osd->priority_xoff_tx[i],
1112 				   &nsd->priority_xoff_tx[i]);
1113 		i40e_stat_update32(hw,
1114 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1115 				   pf->stat_offsets_loaded,
1116 				   &osd->priority_xon_2_xoff[i],
1117 				   &nsd->priority_xon_2_xoff[i]);
1118 	}
1119 
1120 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1121 			   I40E_GLPRT_PRC64L(hw->port),
1122 			   pf->stat_offsets_loaded,
1123 			   &osd->rx_size_64, &nsd->rx_size_64);
1124 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1125 			   I40E_GLPRT_PRC127L(hw->port),
1126 			   pf->stat_offsets_loaded,
1127 			   &osd->rx_size_127, &nsd->rx_size_127);
1128 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1129 			   I40E_GLPRT_PRC255L(hw->port),
1130 			   pf->stat_offsets_loaded,
1131 			   &osd->rx_size_255, &nsd->rx_size_255);
1132 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1133 			   I40E_GLPRT_PRC511L(hw->port),
1134 			   pf->stat_offsets_loaded,
1135 			   &osd->rx_size_511, &nsd->rx_size_511);
1136 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1137 			   I40E_GLPRT_PRC1023L(hw->port),
1138 			   pf->stat_offsets_loaded,
1139 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1140 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1141 			   I40E_GLPRT_PRC1522L(hw->port),
1142 			   pf->stat_offsets_loaded,
1143 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1144 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1145 			   I40E_GLPRT_PRC9522L(hw->port),
1146 			   pf->stat_offsets_loaded,
1147 			   &osd->rx_size_big, &nsd->rx_size_big);
1148 
1149 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1150 			   I40E_GLPRT_PTC64L(hw->port),
1151 			   pf->stat_offsets_loaded,
1152 			   &osd->tx_size_64, &nsd->tx_size_64);
1153 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1154 			   I40E_GLPRT_PTC127L(hw->port),
1155 			   pf->stat_offsets_loaded,
1156 			   &osd->tx_size_127, &nsd->tx_size_127);
1157 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1158 			   I40E_GLPRT_PTC255L(hw->port),
1159 			   pf->stat_offsets_loaded,
1160 			   &osd->tx_size_255, &nsd->tx_size_255);
1161 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1162 			   I40E_GLPRT_PTC511L(hw->port),
1163 			   pf->stat_offsets_loaded,
1164 			   &osd->tx_size_511, &nsd->tx_size_511);
1165 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1166 			   I40E_GLPRT_PTC1023L(hw->port),
1167 			   pf->stat_offsets_loaded,
1168 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1169 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1170 			   I40E_GLPRT_PTC1522L(hw->port),
1171 			   pf->stat_offsets_loaded,
1172 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1173 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1174 			   I40E_GLPRT_PTC9522L(hw->port),
1175 			   pf->stat_offsets_loaded,
1176 			   &osd->tx_size_big, &nsd->tx_size_big);
1177 
1178 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1179 			   pf->stat_offsets_loaded,
1180 			   &osd->rx_undersize, &nsd->rx_undersize);
1181 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1182 			   pf->stat_offsets_loaded,
1183 			   &osd->rx_fragments, &nsd->rx_fragments);
1184 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1185 			   pf->stat_offsets_loaded,
1186 			   &osd->rx_oversize, &nsd->rx_oversize);
1187 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1188 			   pf->stat_offsets_loaded,
1189 			   &osd->rx_jabber, &nsd->rx_jabber);
1190 
1191 	/* FDIR stats */
1192 	i40e_stat_update_and_clear32(hw,
1193 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1194 			&nsd->fd_atr_match);
1195 	i40e_stat_update_and_clear32(hw,
1196 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1197 			&nsd->fd_sb_match);
1198 	i40e_stat_update_and_clear32(hw,
1199 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1200 			&nsd->fd_atr_tunnel_match);
1201 
1202 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1203 	nsd->tx_lpi_status =
1204 		       FIELD_GET(I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK, val);
1205 	nsd->rx_lpi_status =
1206 		       FIELD_GET(I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK, val);
1207 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1208 			   pf->stat_offsets_loaded,
1209 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1210 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1211 			   pf->stat_offsets_loaded,
1212 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1213 
1214 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
1215 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1216 		nsd->fd_sb_status = true;
1217 	else
1218 		nsd->fd_sb_status = false;
1219 
1220 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
1221 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1222 		nsd->fd_atr_status = true;
1223 	else
1224 		nsd->fd_atr_status = false;
1225 
1226 	pf->stat_offsets_loaded = true;
1227 }
1228 
1229 /**
1230  * i40e_update_stats - Update the various statistics counters.
1231  * @vsi: the VSI to be updated
1232  *
1233  * Update the various stats for this VSI and its related entities.
1234  **/
i40e_update_stats(struct i40e_vsi * vsi)1235 void i40e_update_stats(struct i40e_vsi *vsi)
1236 {
1237 	struct i40e_pf *pf = vsi->back;
1238 
1239 	if (vsi->type == I40E_VSI_MAIN)
1240 		i40e_update_pf_stats(pf);
1241 
1242 	i40e_update_vsi_stats(vsi);
1243 }
1244 
1245 /**
1246  * i40e_count_all_filters - counts VSI MAC filters
1247  * @vsi: the VSI to be searched
1248  *
1249  * Return: count of MAC filters in any state.
1250  */
i40e_count_all_filters(struct i40e_vsi * vsi)1251 int i40e_count_all_filters(struct i40e_vsi *vsi)
1252 {
1253 	struct i40e_mac_filter *f;
1254 	struct hlist_node *h;
1255 	int bkt, cnt = 0;
1256 
1257 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1258 		cnt++;
1259 
1260 	return cnt;
1261 }
1262 
1263 /**
1264  * i40e_count_active_filters - counts VSI MAC filters
1265  * @vsi: the VSI to be searched
1266  *
1267  * Return: count of active MAC filters.
1268  */
i40e_count_active_filters(struct i40e_vsi * vsi)1269 int i40e_count_active_filters(struct i40e_vsi *vsi)
1270 {
1271 	struct i40e_mac_filter *f;
1272 	struct hlist_node *h;
1273 	int bkt;
1274 	int cnt = 0;
1275 
1276 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1277 		if (f->state == I40E_FILTER_NEW ||
1278 		    f->state == I40E_FILTER_NEW_SYNC ||
1279 		    f->state == I40E_FILTER_ACTIVE)
1280 			++cnt;
1281 	}
1282 
1283 	return cnt;
1284 }
1285 
1286 /**
1287  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1288  * @vsi: the VSI to be searched
1289  * @macaddr: the MAC address
1290  * @vlan: the vlan
1291  *
1292  * Returns ptr to the filter object or NULL
1293  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1294 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1295 						const u8 *macaddr, s16 vlan)
1296 {
1297 	struct i40e_mac_filter *f;
1298 	u64 key;
1299 
1300 	if (!vsi || !macaddr)
1301 		return NULL;
1302 
1303 	key = i40e_addr_to_hkey(macaddr);
1304 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1305 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1306 		    (vlan == f->vlan))
1307 			return f;
1308 	}
1309 	return NULL;
1310 }
1311 
1312 /**
1313  * i40e_find_mac - Find a mac addr in the macvlan filters list
1314  * @vsi: the VSI to be searched
1315  * @macaddr: the MAC address we are searching for
1316  *
1317  * Returns the first filter with the provided MAC address or NULL if
1318  * MAC address was not found
1319  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1320 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1321 {
1322 	struct i40e_mac_filter *f;
1323 	u64 key;
1324 
1325 	if (!vsi || !macaddr)
1326 		return NULL;
1327 
1328 	key = i40e_addr_to_hkey(macaddr);
1329 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1330 		if ((ether_addr_equal(macaddr, f->macaddr)))
1331 			return f;
1332 	}
1333 	return NULL;
1334 }
1335 
1336 /**
1337  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1338  * @vsi: the VSI to be searched
1339  *
1340  * Returns true if VSI is in vlan mode or false otherwise
1341  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1342 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1343 {
1344 	/* If we have a PVID, always operate in VLAN mode */
1345 	if (vsi->info.pvid)
1346 		return true;
1347 
1348 	/* We need to operate in VLAN mode whenever we have any filters with
1349 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1350 	 * time, incurring search cost repeatedly. However, we can notice two
1351 	 * things:
1352 	 *
1353 	 * 1) the only place where we can gain a VLAN filter is in
1354 	 *    i40e_add_filter.
1355 	 *
1356 	 * 2) the only place where filters are actually removed is in
1357 	 *    i40e_sync_filters_subtask.
1358 	 *
1359 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1360 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1361 	 * we have to perform the full search after deleting filters in
1362 	 * i40e_sync_filters_subtask, but we already have to search
1363 	 * filters here and can perform the check at the same time. This
1364 	 * results in avoiding embedding a loop for VLAN mode inside another
1365 	 * loop over all the filters, and should maintain correctness as noted
1366 	 * above.
1367 	 */
1368 	return vsi->has_vlan_filter;
1369 }
1370 
1371 /**
1372  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1373  * @vsi: the VSI to configure
1374  * @tmp_add_list: list of filters ready to be added
1375  * @tmp_del_list: list of filters ready to be deleted
1376  * @vlan_filters: the number of active VLAN filters
1377  *
1378  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1379  * behave as expected. If we have any active VLAN filters remaining or about
1380  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1381  * so that they only match against untagged traffic. If we no longer have any
1382  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1383  * so that they match against both tagged and untagged traffic. In this way,
1384  * we ensure that we correctly receive the desired traffic. This ensures that
1385  * when we have an active VLAN we will receive only untagged traffic and
1386  * traffic matching active VLANs. If we have no active VLANs then we will
1387  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1388  *
1389  * Finally, in a similar fashion, this function also corrects filters when
1390  * there is an active PVID assigned to this VSI.
1391  *
1392  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1393  *
1394  * This function is only expected to be called from within
1395  * i40e_sync_vsi_filters.
1396  *
1397  * NOTE: This function expects to be called while under the
1398  * mac_filter_hash_lock
1399  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1400 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1401 					 struct hlist_head *tmp_add_list,
1402 					 struct hlist_head *tmp_del_list,
1403 					 int vlan_filters)
1404 {
1405 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1406 	struct i40e_mac_filter *f, *add_head;
1407 	struct i40e_new_mac_filter *new;
1408 	struct hlist_node *h;
1409 	int bkt, new_vlan;
1410 
1411 	/* To determine if a particular filter needs to be replaced we
1412 	 * have the three following conditions:
1413 	 *
1414 	 * a) if we have a PVID assigned, then all filters which are
1415 	 *    not marked as VLAN=PVID must be replaced with filters that
1416 	 *    are.
1417 	 * b) otherwise, if we have any active VLANS, all filters
1418 	 *    which are marked as VLAN=-1 must be replaced with
1419 	 *    filters marked as VLAN=0
1420 	 * c) finally, if we do not have any active VLANS, all filters
1421 	 *    which are marked as VLAN=0 must be replaced with filters
1422 	 *    marked as VLAN=-1
1423 	 */
1424 
1425 	/* Update the filters about to be added in place */
1426 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1427 		if (pvid && new->f->vlan != pvid)
1428 			new->f->vlan = pvid;
1429 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1430 			new->f->vlan = 0;
1431 		else if (!vlan_filters && new->f->vlan == 0)
1432 			new->f->vlan = I40E_VLAN_ANY;
1433 	}
1434 
1435 	/* Update the remaining active filters */
1436 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1437 		/* Combine the checks for whether a filter needs to be changed
1438 		 * and then determine the new VLAN inside the if block, in
1439 		 * order to avoid duplicating code for adding the new filter
1440 		 * then deleting the old filter.
1441 		 */
1442 		if ((pvid && f->vlan != pvid) ||
1443 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1444 		    (!vlan_filters && f->vlan == 0)) {
1445 			/* Determine the new vlan we will be adding */
1446 			if (pvid)
1447 				new_vlan = pvid;
1448 			else if (vlan_filters)
1449 				new_vlan = 0;
1450 			else
1451 				new_vlan = I40E_VLAN_ANY;
1452 
1453 			/* Create the new filter */
1454 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1455 			if (!add_head)
1456 				return -ENOMEM;
1457 
1458 			/* Create a temporary i40e_new_mac_filter */
1459 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1460 			if (!new)
1461 				return -ENOMEM;
1462 
1463 			new->f = add_head;
1464 			new->state = add_head->state;
1465 			if (add_head->state == I40E_FILTER_NEW)
1466 				add_head->state = I40E_FILTER_NEW_SYNC;
1467 
1468 			/* Add the new filter to the tmp list */
1469 			hlist_add_head(&new->hlist, tmp_add_list);
1470 
1471 			/* Put the original filter into the delete list */
1472 			f->state = I40E_FILTER_REMOVE;
1473 			hash_del(&f->hlist);
1474 			hlist_add_head(&f->hlist, tmp_del_list);
1475 		}
1476 	}
1477 
1478 	vsi->has_vlan_filter = !!vlan_filters;
1479 
1480 	return 0;
1481 }
1482 
1483 /**
1484  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1485  * @vsi: the vsi to configure
1486  * @new_mac: new mac filter to be added
1487  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1488  * @vlan_filters: the number of active VLAN filters
1489  * @trusted: flag if the VF is trusted
1490  *
1491  * Get new VLAN id based on current VLAN filters, trust, PVID
1492  * and vf-vlan-prune-disable flag.
1493  *
1494  * Returns the value of the new vlan filter or
1495  * the old value if no new filter is needed.
1496  */
i40e_get_vf_new_vlan(struct i40e_vsi * vsi,struct i40e_new_mac_filter * new_mac,struct i40e_mac_filter * f,int vlan_filters,bool trusted)1497 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1498 				struct i40e_new_mac_filter *new_mac,
1499 				struct i40e_mac_filter *f,
1500 				int vlan_filters,
1501 				bool trusted)
1502 {
1503 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1504 	struct i40e_pf *pf = vsi->back;
1505 	bool is_any;
1506 
1507 	if (new_mac)
1508 		f = new_mac->f;
1509 
1510 	if (pvid && f->vlan != pvid)
1511 		return pvid;
1512 
1513 	is_any = (trusted ||
1514 		  !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
1515 
1516 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1517 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1518 	    (is_any && !vlan_filters && f->vlan == 0)) {
1519 		if (is_any)
1520 			return I40E_VLAN_ANY;
1521 		else
1522 			return 0;
1523 	}
1524 
1525 	return f->vlan;
1526 }
1527 
1528 /**
1529  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1530  * @vsi: the vsi to configure
1531  * @tmp_add_list: list of filters ready to be added
1532  * @tmp_del_list: list of filters ready to be deleted
1533  * @vlan_filters: the number of active VLAN filters
1534  * @trusted: flag if the VF is trusted
1535  *
1536  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1537  * and vf-vlan-prune-disable flag.
1538  *
1539  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1540  *
1541  * This function is only expected to be called from within
1542  * i40e_sync_vsi_filters.
1543  *
1544  * NOTE: This function expects to be called while under the
1545  * mac_filter_hash_lock
1546  */
i40e_correct_vf_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters,bool trusted)1547 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1548 					    struct hlist_head *tmp_add_list,
1549 					    struct hlist_head *tmp_del_list,
1550 					    int vlan_filters,
1551 					    bool trusted)
1552 {
1553 	struct i40e_mac_filter *f, *add_head;
1554 	struct i40e_new_mac_filter *new_mac;
1555 	struct hlist_node *h;
1556 	int bkt, new_vlan;
1557 
1558 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1559 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1560 							vlan_filters, trusted);
1561 	}
1562 
1563 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1564 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1565 						trusted);
1566 		if (new_vlan != f->vlan) {
1567 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1568 			if (!add_head)
1569 				return -ENOMEM;
1570 			/* Create a temporary i40e_new_mac_filter */
1571 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1572 			if (!new_mac)
1573 				return -ENOMEM;
1574 			new_mac->f = add_head;
1575 			new_mac->state = add_head->state;
1576 			if (add_head->state == I40E_FILTER_NEW)
1577 				add_head->state = I40E_FILTER_NEW_SYNC;
1578 
1579 			/* Add the new filter to the tmp list */
1580 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1581 
1582 			/* Put the original filter into the delete list */
1583 			f->state = I40E_FILTER_REMOVE;
1584 			hash_del(&f->hlist);
1585 			hlist_add_head(&f->hlist, tmp_del_list);
1586 		}
1587 	}
1588 
1589 	vsi->has_vlan_filter = !!vlan_filters;
1590 	return 0;
1591 }
1592 
1593 /**
1594  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1595  * @vsi: the PF Main VSI - inappropriate for any other VSI
1596  * @macaddr: the MAC address
1597  *
1598  * Remove whatever filter the firmware set up so the driver can manage
1599  * its own filtering intelligently.
1600  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1601 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1602 {
1603 	struct i40e_aqc_remove_macvlan_element_data element;
1604 	struct i40e_pf *pf = vsi->back;
1605 
1606 	/* Only appropriate for the PF main VSI */
1607 	if (vsi->type != I40E_VSI_MAIN)
1608 		return;
1609 
1610 	memset(&element, 0, sizeof(element));
1611 	ether_addr_copy(element.mac_addr, macaddr);
1612 	element.vlan_tag = 0;
1613 	/* Ignore error returns, some firmware does it this way... */
1614 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1615 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1616 
1617 	memset(&element, 0, sizeof(element));
1618 	ether_addr_copy(element.mac_addr, macaddr);
1619 	element.vlan_tag = 0;
1620 	/* ...and some firmware does it this way. */
1621 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1622 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1623 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1624 }
1625 
1626 /**
1627  * i40e_add_filter - Add a mac/vlan filter to the VSI
1628  * @vsi: the VSI to be searched
1629  * @macaddr: the MAC address
1630  * @vlan: the vlan
1631  *
1632  * Returns ptr to the filter object or NULL when no memory available.
1633  *
1634  * NOTE: This function is expected to be called with mac_filter_hash_lock
1635  * being held.
1636  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1637 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1638 					const u8 *macaddr, s16 vlan)
1639 {
1640 	struct i40e_mac_filter *f;
1641 	u64 key;
1642 
1643 	if (!vsi || !macaddr)
1644 		return NULL;
1645 
1646 	f = i40e_find_filter(vsi, macaddr, vlan);
1647 	if (!f) {
1648 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1649 		if (!f)
1650 			return NULL;
1651 
1652 		/* Update the boolean indicating if we need to function in
1653 		 * VLAN mode.
1654 		 */
1655 		if (vlan >= 0)
1656 			vsi->has_vlan_filter = true;
1657 
1658 		ether_addr_copy(f->macaddr, macaddr);
1659 		f->vlan = vlan;
1660 		f->state = I40E_FILTER_NEW;
1661 		INIT_HLIST_NODE(&f->hlist);
1662 
1663 		key = i40e_addr_to_hkey(macaddr);
1664 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1665 
1666 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1667 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1668 	}
1669 
1670 	/* If we're asked to add a filter that has been marked for removal, it
1671 	 * is safe to simply restore it to active state. __i40e_del_filter
1672 	 * will have simply deleted any filters which were previously marked
1673 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1674 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1675 	 * task, just restore this filter to the ACTIVE state so that the
1676 	 * sync task leaves it in place
1677 	 */
1678 	if (f->state == I40E_FILTER_REMOVE)
1679 		f->state = I40E_FILTER_ACTIVE;
1680 
1681 	return f;
1682 }
1683 
1684 /**
1685  * __i40e_del_filter - Remove a specific filter from the VSI
1686  * @vsi: VSI to remove from
1687  * @f: the filter to remove from the list
1688  *
1689  * This function requires you've found * the exact filter you will remove
1690  * already, such as via i40e_find_filter or i40e_find_mac.
1691  *
1692  * NOTE: This function is expected to be called with mac_filter_hash_lock
1693  * being held.
1694  * ANOTHER NOTE: This function MUST be called from within the context of
1695  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1696  * instead of list_for_each_entry().
1697  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1698 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1699 {
1700 	if (!f)
1701 		return;
1702 
1703 	/* If the filter was never added to firmware then we can just delete it
1704 	 * directly and we don't want to set the status to remove or else an
1705 	 * admin queue command will unnecessarily fire.
1706 	 */
1707 	if ((f->state == I40E_FILTER_FAILED) ||
1708 	    (f->state == I40E_FILTER_NEW)) {
1709 		hash_del(&f->hlist);
1710 		kfree(f);
1711 	} else {
1712 		f->state = I40E_FILTER_REMOVE;
1713 	}
1714 
1715 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1716 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1717 }
1718 
1719 /**
1720  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1721  * @vsi: the VSI to be searched
1722  * @macaddr: the mac address to be filtered
1723  *
1724  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1725  * go through all the macvlan filters and add a macvlan filter for each
1726  * unique vlan that already exists. If a PVID has been assigned, instead only
1727  * add the macaddr to that VLAN.
1728  *
1729  * Returns last filter added on success, else NULL
1730  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1731 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1732 					    const u8 *macaddr)
1733 {
1734 	struct i40e_mac_filter *f, *add = NULL;
1735 	struct hlist_node *h;
1736 	int bkt;
1737 
1738 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1739 	if (vsi->info.pvid)
1740 		return i40e_add_filter(vsi, macaddr,
1741 				       le16_to_cpu(vsi->info.pvid));
1742 
1743 	if (!i40e_is_vsi_in_vlan(vsi))
1744 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1745 
1746 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1747 		if (f->state == I40E_FILTER_REMOVE)
1748 			continue;
1749 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1750 		if (!add)
1751 			return NULL;
1752 	}
1753 
1754 	return add;
1755 }
1756 
1757 /**
1758  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1759  * @vsi: the VSI to be searched
1760  * @macaddr: the mac address to be removed
1761  *
1762  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1763  * associated with.
1764  *
1765  * Returns 0 for success, or error
1766  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1767 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1768 {
1769 	struct i40e_mac_filter *f;
1770 	struct hlist_node *h;
1771 	bool found = false;
1772 	int bkt;
1773 
1774 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1775 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1776 		if (ether_addr_equal(macaddr, f->macaddr)) {
1777 			__i40e_del_filter(vsi, f);
1778 			found = true;
1779 		}
1780 	}
1781 
1782 	if (found)
1783 		return 0;
1784 	else
1785 		return -ENOENT;
1786 }
1787 
1788 /**
1789  * i40e_set_mac - NDO callback to set mac address
1790  * @netdev: network interface device structure
1791  * @p: pointer to an address structure
1792  *
1793  * Returns 0 on success, negative on failure
1794  **/
i40e_set_mac(struct net_device * netdev,void * p)1795 static int i40e_set_mac(struct net_device *netdev, void *p)
1796 {
1797 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1798 	struct i40e_vsi *vsi = np->vsi;
1799 	struct i40e_pf *pf = vsi->back;
1800 	struct i40e_hw *hw = &pf->hw;
1801 	struct sockaddr *addr = p;
1802 
1803 	if (!is_valid_ether_addr(addr->sa_data))
1804 		return -EADDRNOTAVAIL;
1805 
1806 	if (test_bit(__I40E_DOWN, pf->state) ||
1807 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1808 		return -EADDRNOTAVAIL;
1809 
1810 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1811 		netdev_info(netdev, "returning to hw mac address %pM\n",
1812 			    hw->mac.addr);
1813 	else
1814 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1815 
1816 	/* Copy the address first, so that we avoid a possible race with
1817 	 * .set_rx_mode().
1818 	 * - Remove old address from MAC filter
1819 	 * - Copy new address
1820 	 * - Add new address to MAC filter
1821 	 */
1822 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1823 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1824 	eth_hw_addr_set(netdev, addr->sa_data);
1825 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1826 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1827 
1828 	if (vsi->type == I40E_VSI_MAIN) {
1829 		int ret;
1830 
1831 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1832 						addr->sa_data, NULL);
1833 		if (ret)
1834 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1835 				    ERR_PTR(ret),
1836 				    libie_aq_str(hw->aq.asq_last_status));
1837 	}
1838 
1839 	/* schedule our worker thread which will take care of
1840 	 * applying the new filter changes
1841 	 */
1842 	i40e_service_event_schedule(pf);
1843 	return 0;
1844 }
1845 
1846 /**
1847  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1848  * @vsi: vsi structure
1849  * @seed: RSS hash seed
1850  * @lut: pointer to lookup table of lut_size
1851  * @lut_size: size of the lookup table
1852  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1853 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1854 			      u8 *lut, u16 lut_size)
1855 {
1856 	struct i40e_pf *pf = vsi->back;
1857 	struct i40e_hw *hw = &pf->hw;
1858 	int ret = 0;
1859 
1860 	if (seed) {
1861 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1862 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1863 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1864 		if (ret) {
1865 			dev_info(&pf->pdev->dev,
1866 				 "Cannot set RSS key, err %pe aq_err %s\n",
1867 				 ERR_PTR(ret),
1868 				 libie_aq_str(hw->aq.asq_last_status));
1869 			return ret;
1870 		}
1871 	}
1872 	if (lut) {
1873 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1874 
1875 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1876 		if (ret) {
1877 			dev_info(&pf->pdev->dev,
1878 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1879 				 ERR_PTR(ret),
1880 				 libie_aq_str(hw->aq.asq_last_status));
1881 			return ret;
1882 		}
1883 	}
1884 	return ret;
1885 }
1886 
1887 /**
1888  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1889  * @vsi: VSI structure
1890  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1891 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1892 {
1893 	struct i40e_pf *pf = vsi->back;
1894 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1895 	u8 *lut;
1896 	int ret;
1897 
1898 	if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
1899 		return 0;
1900 	if (!vsi->rss_size)
1901 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1902 				      vsi->num_queue_pairs);
1903 	if (!vsi->rss_size)
1904 		return -EINVAL;
1905 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1906 	if (!lut)
1907 		return -ENOMEM;
1908 
1909 	/* Use the user configured hash keys and lookup table if there is one,
1910 	 * otherwise use default
1911 	 */
1912 	if (vsi->rss_lut_user)
1913 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1914 	else
1915 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1916 	if (vsi->rss_hkey_user)
1917 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1918 	else
1919 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1920 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1921 	kfree(lut);
1922 	return ret;
1923 }
1924 
1925 /**
1926  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1927  * @vsi: the VSI being configured,
1928  * @ctxt: VSI context structure
1929  * @enabled_tc: number of traffic classes to enable
1930  *
1931  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1932  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1933 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1934 					   struct i40e_vsi_context *ctxt,
1935 					   u8 enabled_tc)
1936 {
1937 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1938 	int i, override_q, pow, num_qps, ret;
1939 	u8 netdev_tc = 0, offset = 0;
1940 
1941 	if (vsi->type != I40E_VSI_MAIN)
1942 		return -EINVAL;
1943 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1944 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1945 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1946 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1947 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1948 
1949 	/* find the next higher power-of-2 of num queue pairs */
1950 	pow = ilog2(num_qps);
1951 	if (!is_power_of_2(num_qps))
1952 		pow++;
1953 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1954 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1955 
1956 	/* Setup queue offset/count for all TCs for given VSI */
1957 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1958 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1959 		/* See if the given TC is enabled for the given VSI */
1960 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1961 			offset = vsi->mqprio_qopt.qopt.offset[i];
1962 			qcount = vsi->mqprio_qopt.qopt.count[i];
1963 			if (qcount > max_qcount)
1964 				max_qcount = qcount;
1965 			vsi->tc_config.tc_info[i].qoffset = offset;
1966 			vsi->tc_config.tc_info[i].qcount = qcount;
1967 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1968 		} else {
1969 			/* TC is not enabled so set the offset to
1970 			 * default queue and allocate one queue
1971 			 * for the given TC.
1972 			 */
1973 			vsi->tc_config.tc_info[i].qoffset = 0;
1974 			vsi->tc_config.tc_info[i].qcount = 1;
1975 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1976 		}
1977 	}
1978 
1979 	/* Set actual Tx/Rx queue pairs */
1980 	vsi->num_queue_pairs = offset + qcount;
1981 
1982 	/* Setup queue TC[0].qmap for given VSI context */
1983 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1984 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1985 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1986 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1987 
1988 	/* Reconfigure RSS for main VSI with max queue count */
1989 	vsi->rss_size = max_qcount;
1990 	ret = i40e_vsi_config_rss(vsi);
1991 	if (ret) {
1992 		dev_info(&vsi->back->pdev->dev,
1993 			 "Failed to reconfig rss for num_queues (%u)\n",
1994 			 max_qcount);
1995 		return ret;
1996 	}
1997 	vsi->reconfig_rss = true;
1998 	dev_dbg(&vsi->back->pdev->dev,
1999 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
2000 
2001 	/* Find queue count available for channel VSIs and starting offset
2002 	 * for channel VSIs
2003 	 */
2004 	override_q = vsi->mqprio_qopt.qopt.count[0];
2005 	if (override_q && override_q < vsi->num_queue_pairs) {
2006 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2007 		vsi->next_base_queue = override_q;
2008 	}
2009 	return 0;
2010 }
2011 
2012 /**
2013  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2014  * @vsi: the VSI being setup
2015  * @ctxt: VSI context structure
2016  * @enabled_tc: Enabled TCs bitmap
2017  * @is_add: True if called before Add VSI
2018  *
2019  * Setup VSI queue mapping for enabled traffic classes.
2020  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)2021 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2022 				     struct i40e_vsi_context *ctxt,
2023 				     u8 enabled_tc,
2024 				     bool is_add)
2025 {
2026 	struct i40e_pf *pf = vsi->back;
2027 	u16 num_tc_qps = 0;
2028 	u16 sections = 0;
2029 	u8 netdev_tc = 0;
2030 	u16 numtc = 1;
2031 	u16 qcount;
2032 	u8 offset;
2033 	u16 qmap;
2034 	int i;
2035 
2036 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2037 	offset = 0;
2038 	/* zero out queue mapping, it will get updated on the end of the function */
2039 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2040 
2041 	if (vsi->type == I40E_VSI_MAIN) {
2042 		/* This code helps add more queue to the VSI if we have
2043 		 * more cores than RSS can support, the higher cores will
2044 		 * be served by ATR or other filters. Furthermore, the
2045 		 * non-zero req_queue_pairs says that user requested a new
2046 		 * queue count via ethtool's set_channels, so use this
2047 		 * value for queues distribution across traffic classes
2048 		 * We need at least one queue pair for the interface
2049 		 * to be usable as we see in else statement.
2050 		 */
2051 		if (vsi->req_queue_pairs > 0)
2052 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2053 		else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2054 			vsi->num_queue_pairs = pf->num_lan_msix;
2055 		else
2056 			vsi->num_queue_pairs = 1;
2057 	}
2058 
2059 	/* Number of queues per enabled TC */
2060 	if (vsi->type == I40E_VSI_MAIN ||
2061 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2062 		num_tc_qps = vsi->num_queue_pairs;
2063 	else
2064 		num_tc_qps = vsi->alloc_queue_pairs;
2065 
2066 	if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
2067 		/* Find numtc from enabled TC bitmap */
2068 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2069 			if (enabled_tc & BIT(i)) /* TC is enabled */
2070 				numtc++;
2071 		}
2072 		if (!numtc) {
2073 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2074 			numtc = 1;
2075 		}
2076 		num_tc_qps = num_tc_qps / numtc;
2077 		num_tc_qps = min_t(int, num_tc_qps,
2078 				   i40e_pf_get_max_q_per_tc(pf));
2079 	}
2080 
2081 	vsi->tc_config.numtc = numtc;
2082 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2083 
2084 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2085 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2086 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2087 
2088 	/* Setup queue offset/count for all TCs for given VSI */
2089 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2090 		/* See if the given TC is enabled for the given VSI */
2091 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2092 			/* TC is enabled */
2093 			int pow, num_qps;
2094 
2095 			switch (vsi->type) {
2096 			case I40E_VSI_MAIN:
2097 				if ((!test_bit(I40E_FLAG_FD_SB_ENA,
2098 					       pf->flags) &&
2099 				     !test_bit(I40E_FLAG_FD_ATR_ENA,
2100 					       pf->flags)) ||
2101 				    vsi->tc_config.enabled_tc != 1) {
2102 					qcount = min_t(int, pf->alloc_rss_size,
2103 						       num_tc_qps);
2104 					break;
2105 				}
2106 				fallthrough;
2107 			case I40E_VSI_FDIR:
2108 			case I40E_VSI_SRIOV:
2109 			case I40E_VSI_VMDQ2:
2110 			default:
2111 				qcount = num_tc_qps;
2112 				WARN_ON(i != 0);
2113 				break;
2114 			}
2115 			vsi->tc_config.tc_info[i].qoffset = offset;
2116 			vsi->tc_config.tc_info[i].qcount = qcount;
2117 
2118 			/* find the next higher power-of-2 of num queue pairs */
2119 			num_qps = qcount;
2120 			pow = 0;
2121 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2122 				pow++;
2123 				num_qps >>= 1;
2124 			}
2125 
2126 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2127 			qmap =
2128 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2129 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2130 
2131 			offset += qcount;
2132 		} else {
2133 			/* TC is not enabled so set the offset to
2134 			 * default queue and allocate one queue
2135 			 * for the given TC.
2136 			 */
2137 			vsi->tc_config.tc_info[i].qoffset = 0;
2138 			vsi->tc_config.tc_info[i].qcount = 1;
2139 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2140 
2141 			qmap = 0;
2142 		}
2143 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2144 	}
2145 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2146 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2147 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2148 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2149 		vsi->num_queue_pairs = offset;
2150 
2151 	/* Scheduler section valid can only be set for ADD VSI */
2152 	if (is_add) {
2153 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2154 
2155 		ctxt->info.up_enable_bits = enabled_tc;
2156 	}
2157 	if (vsi->type == I40E_VSI_SRIOV) {
2158 		ctxt->info.mapping_flags |=
2159 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2160 		for (i = 0; i < vsi->num_queue_pairs; i++)
2161 			ctxt->info.queue_mapping[i] =
2162 					       cpu_to_le16(vsi->base_queue + i);
2163 	} else {
2164 		ctxt->info.mapping_flags |=
2165 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2166 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2167 	}
2168 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2169 }
2170 
2171 /**
2172  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2173  * @netdev: the netdevice
2174  * @addr: address to add
2175  *
2176  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2177  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2178  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)2179 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2180 {
2181 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2182 	struct i40e_vsi *vsi = np->vsi;
2183 
2184 	if (i40e_add_mac_filter(vsi, addr))
2185 		return 0;
2186 	else
2187 		return -ENOMEM;
2188 }
2189 
2190 /**
2191  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2192  * @netdev: the netdevice
2193  * @addr: address to add
2194  *
2195  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2196  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2197  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)2198 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2199 {
2200 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2201 	struct i40e_vsi *vsi = np->vsi;
2202 
2203 	/* Under some circumstances, we might receive a request to delete
2204 	 * our own device address from our uc list. Because we store the
2205 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2206 	 * such requests and not delete our device address from this list.
2207 	 */
2208 	if (ether_addr_equal(addr, netdev->dev_addr))
2209 		return 0;
2210 
2211 	i40e_del_mac_filter(vsi, addr);
2212 
2213 	return 0;
2214 }
2215 
2216 /**
2217  * i40e_set_rx_mode - NDO callback to set the netdev filters
2218  * @netdev: network interface device structure
2219  **/
i40e_set_rx_mode(struct net_device * netdev)2220 static void i40e_set_rx_mode(struct net_device *netdev)
2221 {
2222 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2223 	struct i40e_vsi *vsi = np->vsi;
2224 
2225 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2226 
2227 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2228 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2229 
2230 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2231 
2232 	/* check for other flag changes */
2233 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2234 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2235 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2236 	}
2237 	i40e_service_event_schedule(vsi->back);
2238 }
2239 
2240 /**
2241  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2242  * @vsi: Pointer to VSI struct
2243  * @from: Pointer to list which contains MAC filter entries - changes to
2244  *        those entries needs to be undone.
2245  *
2246  * MAC filter entries from this list were slated for deletion.
2247  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2248 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2249 					 struct hlist_head *from)
2250 {
2251 	struct i40e_mac_filter *f;
2252 	struct hlist_node *h;
2253 
2254 	hlist_for_each_entry_safe(f, h, from, hlist) {
2255 		u64 key = i40e_addr_to_hkey(f->macaddr);
2256 
2257 		/* Move the element back into MAC filter list*/
2258 		hlist_del(&f->hlist);
2259 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2260 	}
2261 }
2262 
2263 /**
2264  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2265  * @vsi: Pointer to vsi struct
2266  * @from: Pointer to list which contains MAC filter entries - changes to
2267  *        those entries needs to be undone.
2268  *
2269  * MAC filter entries from this list were slated for addition.
2270  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2271 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2272 					 struct hlist_head *from)
2273 {
2274 	struct i40e_new_mac_filter *new;
2275 	struct hlist_node *h;
2276 
2277 	hlist_for_each_entry_safe(new, h, from, hlist) {
2278 		/* We can simply free the wrapper structure */
2279 		hlist_del(&new->hlist);
2280 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2281 		kfree(new);
2282 	}
2283 }
2284 
2285 /**
2286  * i40e_next_filter - Get the next non-broadcast filter from a list
2287  * @next: pointer to filter in list
2288  *
2289  * Returns the next non-broadcast filter in the list. Required so that we
2290  * ignore broadcast filters within the list, since these are not handled via
2291  * the normal firmware update path.
2292  */
2293 static
i40e_next_filter(struct i40e_new_mac_filter * next)2294 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2295 {
2296 	hlist_for_each_entry_continue(next, hlist) {
2297 		if (!is_broadcast_ether_addr(next->f->macaddr))
2298 			return next;
2299 	}
2300 
2301 	return NULL;
2302 }
2303 
2304 /**
2305  * i40e_update_filter_state - Update filter state based on return data
2306  * from firmware
2307  * @count: Number of filters added
2308  * @add_list: return data from fw
2309  * @add_head: pointer to first filter in current batch
2310  *
2311  * MAC filter entries from list were slated to be added to device. Returns
2312  * number of successful filters. Note that 0 does NOT mean success!
2313  **/
2314 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2315 i40e_update_filter_state(int count,
2316 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2317 			 struct i40e_new_mac_filter *add_head)
2318 {
2319 	int retval = 0;
2320 	int i;
2321 
2322 	for (i = 0; i < count; i++) {
2323 		/* Always check status of each filter. We don't need to check
2324 		 * the firmware return status because we pre-set the filter
2325 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2326 		 * request to the adminq. Thus, if it no longer matches then
2327 		 * we know the filter is active.
2328 		 */
2329 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2330 			add_head->state = I40E_FILTER_FAILED;
2331 		} else {
2332 			add_head->state = I40E_FILTER_ACTIVE;
2333 			retval++;
2334 		}
2335 
2336 		add_head = i40e_next_filter(add_head);
2337 		if (!add_head)
2338 			break;
2339 	}
2340 
2341 	return retval;
2342 }
2343 
2344 /**
2345  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2346  * @vsi: ptr to the VSI
2347  * @vsi_name: name to display in messages
2348  * @list: the list of filters to send to firmware
2349  * @num_del: the number of filters to delete
2350  * @retval: Set to -EIO on failure to delete
2351  *
2352  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2353  * *retval instead of a return value so that success does not force ret_val to
2354  * be set to 0. This ensures that a sequence of calls to this function
2355  * preserve the previous value of *retval on successful delete.
2356  */
2357 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)2358 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2359 			  struct i40e_aqc_remove_macvlan_element_data *list,
2360 			  int num_del, int *retval)
2361 {
2362 	struct i40e_hw *hw = &vsi->back->hw;
2363 	enum libie_aq_err aq_status;
2364 	int aq_ret;
2365 
2366 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2367 					   &aq_status);
2368 
2369 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2370 	if (aq_ret && !(aq_status == LIBIE_AQ_RC_ENOENT)) {
2371 		*retval = -EIO;
2372 		dev_info(&vsi->back->pdev->dev,
2373 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2374 			 vsi_name, ERR_PTR(aq_ret), libie_aq_str(aq_status));
2375 	}
2376 }
2377 
2378 /**
2379  * i40e_aqc_add_filters - Request firmware to add a set of filters
2380  * @vsi: ptr to the VSI
2381  * @vsi_name: name to display in messages
2382  * @list: the list of filters to send to firmware
2383  * @add_head: Position in the add hlist
2384  * @num_add: the number of filters to add
2385  *
2386  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2387  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2388  * space for more filters.
2389  */
2390 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)2391 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2392 			  struct i40e_aqc_add_macvlan_element_data *list,
2393 			  struct i40e_new_mac_filter *add_head,
2394 			  int num_add)
2395 {
2396 	struct i40e_hw *hw = &vsi->back->hw;
2397 	enum libie_aq_err aq_status;
2398 	int fcnt;
2399 
2400 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2401 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2402 
2403 	if (fcnt != num_add) {
2404 		if (vsi->type == I40E_VSI_MAIN) {
2405 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2406 			dev_warn(&vsi->back->pdev->dev,
2407 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2408 				 libie_aq_str(aq_status), vsi_name);
2409 		} else if (vsi->type == I40E_VSI_SRIOV ||
2410 			   vsi->type == I40E_VSI_VMDQ1 ||
2411 			   vsi->type == I40E_VSI_VMDQ2) {
2412 			dev_warn(&vsi->back->pdev->dev,
2413 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2414 				 libie_aq_str(aq_status), vsi_name, vsi_name);
2415 		} else {
2416 			dev_warn(&vsi->back->pdev->dev,
2417 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2418 				 libie_aq_str(aq_status), vsi_name, vsi->type);
2419 		}
2420 	}
2421 }
2422 
2423 /**
2424  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2425  * @vsi: pointer to the VSI
2426  * @vsi_name: the VSI name
2427  * @f: filter data
2428  *
2429  * This function sets or clears the promiscuous broadcast flags for VLAN
2430  * filters in order to properly receive broadcast frames. Assumes that only
2431  * broadcast filters are passed.
2432  *
2433  * Returns status indicating success or failure;
2434  **/
2435 static int
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2436 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2437 			  struct i40e_mac_filter *f)
2438 {
2439 	bool enable = f->state == I40E_FILTER_NEW ||
2440 		      f->state == I40E_FILTER_NEW_SYNC;
2441 	struct i40e_hw *hw = &vsi->back->hw;
2442 	int aq_ret;
2443 
2444 	if (f->vlan == I40E_VLAN_ANY) {
2445 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2446 						   vsi->seid,
2447 						   enable,
2448 						   NULL);
2449 	} else {
2450 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2451 							    vsi->seid,
2452 							    enable,
2453 							    f->vlan,
2454 							    NULL);
2455 	}
2456 
2457 	if (aq_ret) {
2458 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2459 		dev_warn(&vsi->back->pdev->dev,
2460 			 "Error %s, forcing overflow promiscuous on %s\n",
2461 			 libie_aq_str(hw->aq.asq_last_status), vsi_name);
2462 	}
2463 
2464 	return aq_ret;
2465 }
2466 
2467 /**
2468  * i40e_set_promiscuous - set promiscuous mode
2469  * @pf: board private structure
2470  * @promisc: promisc on or off
2471  *
2472  * There are different ways of setting promiscuous mode on a PF depending on
2473  * what state/environment we're in.  This identifies and sets it appropriately.
2474  * Returns 0 on success.
2475  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2476 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2477 {
2478 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
2479 	struct i40e_hw *hw = &pf->hw;
2480 	int aq_ret;
2481 
2482 	if (vsi->type == I40E_VSI_MAIN &&
2483 	    i40e_pf_get_main_veb(pf) &&
2484 	    !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
2485 		/* set defport ON for Main VSI instead of true promisc
2486 		 * this way we will get all unicast/multicast and VLAN
2487 		 * promisc behavior but will not get VF or VMDq traffic
2488 		 * replicated on the Main VSI.
2489 		 */
2490 		if (promisc)
2491 			aq_ret = i40e_aq_set_default_vsi(hw,
2492 							 vsi->seid,
2493 							 NULL);
2494 		else
2495 			aq_ret = i40e_aq_clear_default_vsi(hw,
2496 							   vsi->seid,
2497 							   NULL);
2498 		if (aq_ret) {
2499 			dev_info(&pf->pdev->dev,
2500 				 "Set default VSI failed, err %pe, aq_err %s\n",
2501 				 ERR_PTR(aq_ret),
2502 				 libie_aq_str(hw->aq.asq_last_status));
2503 		}
2504 	} else {
2505 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2506 						  hw,
2507 						  vsi->seid,
2508 						  promisc, NULL,
2509 						  true);
2510 		if (aq_ret) {
2511 			dev_info(&pf->pdev->dev,
2512 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2513 				 ERR_PTR(aq_ret),
2514 				 libie_aq_str(hw->aq.asq_last_status));
2515 		}
2516 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2517 						  hw,
2518 						  vsi->seid,
2519 						  promisc, NULL);
2520 		if (aq_ret) {
2521 			dev_info(&pf->pdev->dev,
2522 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2523 				 ERR_PTR(aq_ret),
2524 				 libie_aq_str(hw->aq.asq_last_status));
2525 		}
2526 	}
2527 
2528 	if (!aq_ret)
2529 		pf->cur_promisc = promisc;
2530 
2531 	return aq_ret;
2532 }
2533 
2534 /**
2535  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2536  * @vsi: ptr to the VSI
2537  *
2538  * Push any outstanding VSI filter changes through the AdminQ.
2539  *
2540  * Returns 0 or error value
2541  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2542 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2543 {
2544 	struct hlist_head tmp_add_list, tmp_del_list;
2545 	struct i40e_mac_filter *f;
2546 	struct i40e_new_mac_filter *new, *add_head = NULL;
2547 	struct i40e_hw *hw = &vsi->back->hw;
2548 	bool old_overflow, new_overflow;
2549 	unsigned int failed_filters = 0;
2550 	unsigned int vlan_filters = 0;
2551 	char vsi_name[16] = "PF";
2552 	int filter_list_len = 0;
2553 	u32 changed_flags = 0;
2554 	struct hlist_node *h;
2555 	struct i40e_pf *pf;
2556 	int num_add = 0;
2557 	int num_del = 0;
2558 	int aq_ret = 0;
2559 	int retval = 0;
2560 	u16 cmd_flags;
2561 	int list_size;
2562 	int bkt;
2563 
2564 	/* empty array typed pointers, kcalloc later */
2565 	struct i40e_aqc_add_macvlan_element_data *add_list;
2566 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2567 
2568 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2569 		usleep_range(1000, 2000);
2570 	pf = vsi->back;
2571 
2572 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2573 
2574 	if (vsi->netdev) {
2575 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2576 		vsi->current_netdev_flags = vsi->netdev->flags;
2577 	}
2578 
2579 	INIT_HLIST_HEAD(&tmp_add_list);
2580 	INIT_HLIST_HEAD(&tmp_del_list);
2581 
2582 	if (vsi->type == I40E_VSI_SRIOV)
2583 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2584 	else if (vsi->type != I40E_VSI_MAIN)
2585 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2586 
2587 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2588 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2589 
2590 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2591 		/* Create a list of filters to delete. */
2592 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2593 			if (f->state == I40E_FILTER_REMOVE) {
2594 				/* Move the element into temporary del_list */
2595 				hash_del(&f->hlist);
2596 				hlist_add_head(&f->hlist, &tmp_del_list);
2597 
2598 				/* Avoid counting removed filters */
2599 				continue;
2600 			}
2601 			if (f->state == I40E_FILTER_NEW) {
2602 				/* Create a temporary i40e_new_mac_filter */
2603 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2604 				if (!new)
2605 					goto err_no_memory_locked;
2606 
2607 				/* Store pointer to the real filter */
2608 				new->f = f;
2609 				new->state = f->state;
2610 
2611 				/* Add it to the hash list */
2612 				hlist_add_head(&new->hlist, &tmp_add_list);
2613 				f->state = I40E_FILTER_NEW_SYNC;
2614 			}
2615 
2616 			/* Count the number of active (current and new) VLAN
2617 			 * filters we have now. Does not count filters which
2618 			 * are marked for deletion.
2619 			 */
2620 			if (f->vlan > 0)
2621 				vlan_filters++;
2622 		}
2623 
2624 		if (vsi->type != I40E_VSI_SRIOV)
2625 			retval = i40e_correct_mac_vlan_filters
2626 				(vsi, &tmp_add_list, &tmp_del_list,
2627 				 vlan_filters);
2628 		else if (pf->vf)
2629 			retval = i40e_correct_vf_mac_vlan_filters
2630 				(vsi, &tmp_add_list, &tmp_del_list,
2631 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2632 
2633 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2634 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2635 
2636 		if (retval)
2637 			goto err_no_memory_locked;
2638 
2639 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2640 	}
2641 
2642 	/* Now process 'del_list' outside the lock */
2643 	if (!hlist_empty(&tmp_del_list)) {
2644 		filter_list_len = hw->aq.asq_buf_size /
2645 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2646 		list_size = filter_list_len *
2647 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2648 		del_list = kzalloc(list_size, GFP_ATOMIC);
2649 		if (!del_list)
2650 			goto err_no_memory;
2651 
2652 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2653 			cmd_flags = 0;
2654 
2655 			/* handle broadcast filters by updating the broadcast
2656 			 * promiscuous flag and release filter list.
2657 			 */
2658 			if (is_broadcast_ether_addr(f->macaddr)) {
2659 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2660 
2661 				hlist_del(&f->hlist);
2662 				kfree(f);
2663 				continue;
2664 			}
2665 
2666 			/* add to delete list */
2667 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2668 			if (f->vlan == I40E_VLAN_ANY) {
2669 				del_list[num_del].vlan_tag = 0;
2670 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2671 			} else {
2672 				del_list[num_del].vlan_tag =
2673 					cpu_to_le16((u16)(f->vlan));
2674 			}
2675 
2676 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2677 			del_list[num_del].flags = cmd_flags;
2678 			num_del++;
2679 
2680 			/* flush a full buffer */
2681 			if (num_del == filter_list_len) {
2682 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2683 						     num_del, &retval);
2684 				memset(del_list, 0, list_size);
2685 				num_del = 0;
2686 			}
2687 			/* Release memory for MAC filter entries which were
2688 			 * synced up with HW.
2689 			 */
2690 			hlist_del(&f->hlist);
2691 			kfree(f);
2692 		}
2693 
2694 		if (num_del) {
2695 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2696 					     num_del, &retval);
2697 		}
2698 
2699 		kfree(del_list);
2700 		del_list = NULL;
2701 	}
2702 
2703 	if (!hlist_empty(&tmp_add_list)) {
2704 		/* Do all the adds now. */
2705 		filter_list_len = hw->aq.asq_buf_size /
2706 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2707 		list_size = filter_list_len *
2708 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2709 		add_list = kzalloc(list_size, GFP_ATOMIC);
2710 		if (!add_list)
2711 			goto err_no_memory;
2712 
2713 		num_add = 0;
2714 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2715 			/* handle broadcast filters by updating the broadcast
2716 			 * promiscuous flag instead of adding a MAC filter.
2717 			 */
2718 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2719 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2720 							      new->f))
2721 					new->state = I40E_FILTER_FAILED;
2722 				else
2723 					new->state = I40E_FILTER_ACTIVE;
2724 				continue;
2725 			}
2726 
2727 			/* add to add array */
2728 			if (num_add == 0)
2729 				add_head = new;
2730 			cmd_flags = 0;
2731 			ether_addr_copy(add_list[num_add].mac_addr,
2732 					new->f->macaddr);
2733 			if (new->f->vlan == I40E_VLAN_ANY) {
2734 				add_list[num_add].vlan_tag = 0;
2735 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2736 			} else {
2737 				add_list[num_add].vlan_tag =
2738 					cpu_to_le16((u16)(new->f->vlan));
2739 			}
2740 			add_list[num_add].queue_number = 0;
2741 			/* set invalid match method for later detection */
2742 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2743 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2744 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2745 			num_add++;
2746 
2747 			/* flush a full buffer */
2748 			if (num_add == filter_list_len) {
2749 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2750 						     add_head, num_add);
2751 				memset(add_list, 0, list_size);
2752 				num_add = 0;
2753 			}
2754 		}
2755 		if (num_add) {
2756 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2757 					     num_add);
2758 		}
2759 		/* Now move all of the filters from the temp add list back to
2760 		 * the VSI's list.
2761 		 */
2762 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2763 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2764 			/* Only update the state if we're still NEW */
2765 			if (new->f->state == I40E_FILTER_NEW ||
2766 			    new->f->state == I40E_FILTER_NEW_SYNC)
2767 				new->f->state = new->state;
2768 			hlist_del(&new->hlist);
2769 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2770 			kfree(new);
2771 		}
2772 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2773 		kfree(add_list);
2774 		add_list = NULL;
2775 	}
2776 
2777 	/* Determine the number of active and failed filters. */
2778 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2779 	vsi->active_filters = 0;
2780 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2781 		if (f->state == I40E_FILTER_ACTIVE)
2782 			vsi->active_filters++;
2783 		else if (f->state == I40E_FILTER_FAILED)
2784 			failed_filters++;
2785 	}
2786 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2787 
2788 	/* Check if we are able to exit overflow promiscuous mode. We can
2789 	 * safely exit if we didn't just enter, we no longer have any failed
2790 	 * filters, and we have reduced filters below the threshold value.
2791 	 */
2792 	if (old_overflow && !failed_filters &&
2793 	    vsi->active_filters < vsi->promisc_threshold) {
2794 		dev_info(&pf->pdev->dev,
2795 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2796 			 vsi_name);
2797 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2798 		vsi->promisc_threshold = 0;
2799 	}
2800 
2801 	/* if the VF is not trusted do not do promisc */
2802 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2803 	    !pf->vf[vsi->vf_id].trusted) {
2804 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2805 		goto out;
2806 	}
2807 
2808 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2809 
2810 	/* If we are entering overflow promiscuous, we need to calculate a new
2811 	 * threshold for when we are safe to exit
2812 	 */
2813 	if (!old_overflow && new_overflow)
2814 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2815 
2816 	/* check for changes in promiscuous modes */
2817 	if (changed_flags & IFF_ALLMULTI) {
2818 		bool cur_multipromisc;
2819 
2820 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2821 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2822 							       vsi->seid,
2823 							       cur_multipromisc,
2824 							       NULL);
2825 		if (aq_ret) {
2826 			retval = i40e_aq_rc_to_posix(aq_ret,
2827 						     hw->aq.asq_last_status);
2828 			dev_info(&pf->pdev->dev,
2829 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2830 				 vsi_name,
2831 				 ERR_PTR(aq_ret),
2832 				 libie_aq_str(hw->aq.asq_last_status));
2833 		} else {
2834 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2835 				 cur_multipromisc ? "entering" : "leaving");
2836 		}
2837 	}
2838 
2839 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2840 		bool cur_promisc;
2841 
2842 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2843 			       new_overflow);
2844 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2845 		if (aq_ret) {
2846 			retval = i40e_aq_rc_to_posix(aq_ret,
2847 						     hw->aq.asq_last_status);
2848 			dev_info(&pf->pdev->dev,
2849 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2850 				 cur_promisc ? "on" : "off",
2851 				 vsi_name,
2852 				 ERR_PTR(aq_ret),
2853 				 libie_aq_str(hw->aq.asq_last_status));
2854 		}
2855 	}
2856 out:
2857 	/* if something went wrong then set the changed flag so we try again */
2858 	if (retval)
2859 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2860 
2861 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2862 	return retval;
2863 
2864 err_no_memory:
2865 	/* Restore elements on the temporary add and delete lists */
2866 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2867 err_no_memory_locked:
2868 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2869 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2870 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2871 
2872 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2873 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2874 	return -ENOMEM;
2875 }
2876 
2877 /**
2878  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2879  * @pf: board private structure
2880  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2881 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2882 {
2883 	struct i40e_vsi *vsi;
2884 	int v;
2885 
2886 	if (!pf)
2887 		return;
2888 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2889 		return;
2890 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2891 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2892 		return;
2893 	}
2894 
2895 	i40e_pf_for_each_vsi(pf, v, vsi) {
2896 		if ((vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2897 		    !test_bit(__I40E_VSI_RELEASING, vsi->state)) {
2898 			int ret = i40e_sync_vsi_filters(vsi);
2899 
2900 			if (ret) {
2901 				/* come back and try again later */
2902 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2903 					pf->state);
2904 				break;
2905 			}
2906 		}
2907 	}
2908 }
2909 
2910 /**
2911  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2912  *
2913  * @vsi: VSI to calculate rx_buf_len from
2914  */
i40e_calculate_vsi_rx_buf_len(struct i40e_vsi * vsi)2915 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2916 {
2917 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
2918 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2919 
2920 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2921 }
2922 
2923 /**
2924  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2925  * @vsi: the vsi
2926  * @xdp_prog: XDP program
2927  **/
i40e_max_vsi_frame_size(struct i40e_vsi * vsi,struct bpf_prog * xdp_prog)2928 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2929 				   struct bpf_prog *xdp_prog)
2930 {
2931 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2932 	u16 chain_len;
2933 
2934 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2935 		chain_len = 1;
2936 	else
2937 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2938 
2939 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2940 }
2941 
2942 /**
2943  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2944  * @netdev: network interface device structure
2945  * @new_mtu: new value for maximum frame size
2946  *
2947  * Returns 0 on success, negative on failure
2948  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2949 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2950 {
2951 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2952 	struct i40e_vsi *vsi = np->vsi;
2953 	struct i40e_pf *pf = vsi->back;
2954 	int frame_size;
2955 
2956 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2957 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2958 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2959 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2960 		return -EINVAL;
2961 	}
2962 
2963 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2964 		   netdev->mtu, new_mtu);
2965 	WRITE_ONCE(netdev->mtu, new_mtu);
2966 	if (netif_running(netdev))
2967 		i40e_vsi_reinit_locked(vsi);
2968 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2969 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2970 	return 0;
2971 }
2972 
2973 /**
2974  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2975  * @vsi: the vsi being adjusted
2976  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)2977 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2978 {
2979 	struct i40e_vsi_context ctxt;
2980 	int ret;
2981 
2982 	/* Don't modify stripping options if a port VLAN is active */
2983 	if (vsi->info.pvid)
2984 		return;
2985 
2986 	if ((vsi->info.valid_sections &
2987 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2988 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2989 		return;  /* already enabled */
2990 
2991 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2992 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2993 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2994 
2995 	ctxt.seid = vsi->seid;
2996 	ctxt.info = vsi->info;
2997 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2998 	if (ret) {
2999 		dev_info(&vsi->back->pdev->dev,
3000 			 "update vlan stripping failed, err %pe aq_err %s\n",
3001 			 ERR_PTR(ret),
3002 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3003 	}
3004 }
3005 
3006 /**
3007  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3008  * @vsi: the vsi being adjusted
3009  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)3010 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3011 {
3012 	struct i40e_vsi_context ctxt;
3013 	int ret;
3014 
3015 	/* Don't modify stripping options if a port VLAN is active */
3016 	if (vsi->info.pvid)
3017 		return;
3018 
3019 	if ((vsi->info.valid_sections &
3020 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3021 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3022 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3023 		return;  /* already disabled */
3024 
3025 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3026 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3027 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3028 
3029 	ctxt.seid = vsi->seid;
3030 	ctxt.info = vsi->info;
3031 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3032 	if (ret) {
3033 		dev_info(&vsi->back->pdev->dev,
3034 			 "update vlan stripping failed, err %pe aq_err %s\n",
3035 			 ERR_PTR(ret),
3036 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3037 	}
3038 }
3039 
3040 /**
3041  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3042  * @vsi: the vsi being configured
3043  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3044  *
3045  * This is a helper function for adding a new MAC/VLAN filter with the
3046  * specified VLAN for each existing MAC address already in the hash table.
3047  * This function does *not* perform any accounting to update filters based on
3048  * VLAN mode.
3049  *
3050  * NOTE: this function expects to be called while under the
3051  * mac_filter_hash_lock
3052  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3053 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3054 {
3055 	struct i40e_mac_filter *f, *add_f;
3056 	struct hlist_node *h;
3057 	int bkt;
3058 
3059 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3060 		/* If we're asked to add a filter that has been marked for
3061 		 * removal, it is safe to simply restore it to active state.
3062 		 * __i40e_del_filter will have simply deleted any filters which
3063 		 * were previously marked NEW or FAILED, so if it is currently
3064 		 * marked REMOVE it must have previously been ACTIVE. Since we
3065 		 * haven't yet run the sync filters task, just restore this
3066 		 * filter to the ACTIVE state so that the sync task leaves it
3067 		 * in place.
3068 		 */
3069 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3070 			f->state = I40E_FILTER_ACTIVE;
3071 			continue;
3072 		} else if (f->state == I40E_FILTER_REMOVE) {
3073 			continue;
3074 		}
3075 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3076 		if (!add_f) {
3077 			dev_info(&vsi->back->pdev->dev,
3078 				 "Could not add vlan filter %d for %pM\n",
3079 				 vid, f->macaddr);
3080 			return -ENOMEM;
3081 		}
3082 	}
3083 
3084 	return 0;
3085 }
3086 
3087 /**
3088  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3089  * @vsi: the VSI being configured
3090  * @vid: VLAN id to be added
3091  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)3092 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3093 {
3094 	int err;
3095 
3096 	if (vsi->info.pvid)
3097 		return -EINVAL;
3098 
3099 	/* The network stack will attempt to add VID=0, with the intention to
3100 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3101 	 * these packets by default when configured to receive untagged
3102 	 * packets, so we don't need to add a filter for this case.
3103 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3104 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3105 	 * Thus, we do not want to actually add a filter for VID=0
3106 	 */
3107 	if (!vid)
3108 		return 0;
3109 
3110 	/* Locked once because all functions invoked below iterates list*/
3111 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3112 	err = i40e_add_vlan_all_mac(vsi, vid);
3113 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3114 	if (err)
3115 		return err;
3116 
3117 	/* schedule our worker thread which will take care of
3118 	 * applying the new filter changes
3119 	 */
3120 	i40e_service_event_schedule(vsi->back);
3121 	return 0;
3122 }
3123 
3124 /**
3125  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3126  * @vsi: the vsi being configured
3127  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3128  *
3129  * This function should be used to remove all VLAN filters which match the
3130  * given VID. It does not schedule the service event and does not take the
3131  * mac_filter_hash_lock so it may be combined with other operations under
3132  * a single invocation of the mac_filter_hash_lock.
3133  *
3134  * NOTE: this function expects to be called while under the
3135  * mac_filter_hash_lock
3136  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3137 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3138 {
3139 	struct i40e_mac_filter *f;
3140 	struct hlist_node *h;
3141 	int bkt;
3142 
3143 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3144 		if (f->vlan == vid)
3145 			__i40e_del_filter(vsi, f);
3146 	}
3147 }
3148 
3149 /**
3150  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3151  * @vsi: the VSI being configured
3152  * @vid: VLAN id to be removed
3153  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)3154 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3155 {
3156 	if (!vid || vsi->info.pvid)
3157 		return;
3158 
3159 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3160 	i40e_rm_vlan_all_mac(vsi, vid);
3161 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3162 
3163 	/* schedule our worker thread which will take care of
3164 	 * applying the new filter changes
3165 	 */
3166 	i40e_service_event_schedule(vsi->back);
3167 }
3168 
3169 /**
3170  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3171  * @netdev: network interface to be adjusted
3172  * @proto: unused protocol value
3173  * @vid: vlan id to be added
3174  *
3175  * net_device_ops implementation for adding vlan ids
3176  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3177 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3178 				__always_unused __be16 proto, u16 vid)
3179 {
3180 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3181 	struct i40e_vsi *vsi = np->vsi;
3182 	int ret = 0;
3183 
3184 	if (vid >= VLAN_N_VID)
3185 		return -EINVAL;
3186 
3187 	ret = i40e_vsi_add_vlan(vsi, vid);
3188 	if (!ret)
3189 		set_bit(vid, vsi->active_vlans);
3190 
3191 	return ret;
3192 }
3193 
3194 /**
3195  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3196  * @netdev: network interface to be adjusted
3197  * @proto: unused protocol value
3198  * @vid: vlan id to be added
3199  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3200 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3201 				    __always_unused __be16 proto, u16 vid)
3202 {
3203 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3204 	struct i40e_vsi *vsi = np->vsi;
3205 
3206 	if (vid >= VLAN_N_VID)
3207 		return;
3208 	set_bit(vid, vsi->active_vlans);
3209 }
3210 
3211 /**
3212  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3213  * @netdev: network interface to be adjusted
3214  * @proto: unused protocol value
3215  * @vid: vlan id to be removed
3216  *
3217  * net_device_ops implementation for removing vlan ids
3218  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3219 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3220 				 __always_unused __be16 proto, u16 vid)
3221 {
3222 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3223 	struct i40e_vsi *vsi = np->vsi;
3224 
3225 	/* return code is ignored as there is nothing a user
3226 	 * can do about failure to remove and a log message was
3227 	 * already printed from the other function
3228 	 */
3229 	i40e_vsi_kill_vlan(vsi, vid);
3230 
3231 	clear_bit(vid, vsi->active_vlans);
3232 
3233 	return 0;
3234 }
3235 
3236 /**
3237  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3238  * @vsi: the vsi being brought back up
3239  **/
i40e_restore_vlan(struct i40e_vsi * vsi)3240 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3241 {
3242 	u16 vid;
3243 
3244 	if (!vsi->netdev)
3245 		return;
3246 
3247 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3248 		i40e_vlan_stripping_enable(vsi);
3249 	else
3250 		i40e_vlan_stripping_disable(vsi);
3251 
3252 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3253 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3254 					vid);
3255 }
3256 
3257 /**
3258  * i40e_vsi_add_pvid - Add pvid for the VSI
3259  * @vsi: the vsi being adjusted
3260  * @vid: the vlan id to set as a PVID
3261  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)3262 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3263 {
3264 	struct i40e_vsi_context ctxt;
3265 	int ret;
3266 
3267 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3268 	vsi->info.pvid = cpu_to_le16(vid);
3269 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3270 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3271 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3272 
3273 	ctxt.seid = vsi->seid;
3274 	ctxt.info = vsi->info;
3275 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3276 	if (ret) {
3277 		dev_info(&vsi->back->pdev->dev,
3278 			 "add pvid failed, err %pe aq_err %s\n",
3279 			 ERR_PTR(ret),
3280 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3281 		return -ENOENT;
3282 	}
3283 
3284 	return 0;
3285 }
3286 
3287 /**
3288  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3289  * @vsi: the vsi being adjusted
3290  *
3291  * Just use the vlan_rx_register() service to put it back to normal
3292  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3293 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3294 {
3295 	vsi->info.pvid = 0;
3296 
3297 	i40e_vlan_stripping_disable(vsi);
3298 }
3299 
3300 /**
3301  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3302  * @vsi: ptr to the VSI
3303  *
3304  * If this function returns with an error, then it's possible one or
3305  * more of the rings is populated (while the rest are not).  It is the
3306  * callers duty to clean those orphaned rings.
3307  *
3308  * Return 0 on success, negative on failure
3309  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3310 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3311 {
3312 	int i, err = 0;
3313 
3314 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3315 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3316 
3317 	if (!i40e_enabled_xdp_vsi(vsi))
3318 		return err;
3319 
3320 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3321 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3322 
3323 	return err;
3324 }
3325 
3326 /**
3327  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3328  * @vsi: ptr to the VSI
3329  *
3330  * Free VSI's transmit software resources
3331  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3332 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3333 {
3334 	int i;
3335 
3336 	if (vsi->tx_rings) {
3337 		for (i = 0; i < vsi->num_queue_pairs; i++)
3338 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3339 				i40e_free_tx_resources(vsi->tx_rings[i]);
3340 	}
3341 
3342 	if (vsi->xdp_rings) {
3343 		for (i = 0; i < vsi->num_queue_pairs; i++)
3344 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3345 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3346 	}
3347 }
3348 
3349 /**
3350  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3351  * @vsi: ptr to the VSI
3352  *
3353  * If this function returns with an error, then it's possible one or
3354  * more of the rings is populated (while the rest are not).  It is the
3355  * callers duty to clean those orphaned rings.
3356  *
3357  * Return 0 on success, negative on failure
3358  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3359 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3360 {
3361 	int i, err = 0;
3362 
3363 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3364 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3365 	return err;
3366 }
3367 
3368 /**
3369  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3370  * @vsi: ptr to the VSI
3371  *
3372  * Free all receive software resources
3373  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3374 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3375 {
3376 	int i;
3377 
3378 	if (!vsi->rx_rings)
3379 		return;
3380 
3381 	for (i = 0; i < vsi->num_queue_pairs; i++)
3382 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3383 			i40e_free_rx_resources(vsi->rx_rings[i]);
3384 }
3385 
3386 /**
3387  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3388  * @ring: The Tx ring to configure
3389  *
3390  * This enables/disables XPS for a given Tx descriptor ring
3391  * based on the TCs enabled for the VSI that ring belongs to.
3392  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3393 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3394 {
3395 	int cpu;
3396 
3397 	if (!ring->q_vector || !ring->netdev || ring->ch)
3398 		return;
3399 
3400 	/* We only initialize XPS once, so as not to overwrite user settings */
3401 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3402 		return;
3403 
3404 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3405 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3406 			    ring->queue_index);
3407 }
3408 
3409 /**
3410  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3411  * @ring: The Tx or Rx ring
3412  *
3413  * Returns the AF_XDP buffer pool or NULL.
3414  **/
i40e_xsk_pool(struct i40e_ring * ring)3415 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3416 {
3417 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3418 	int qid = ring->queue_index;
3419 
3420 	if (ring_is_xdp(ring))
3421 		qid -= ring->vsi->alloc_queue_pairs;
3422 
3423 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3424 		return NULL;
3425 
3426 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3427 }
3428 
3429 /**
3430  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3431  * @ring: The Tx ring to configure
3432  *
3433  * Configure the Tx descriptor ring in the HMC context.
3434  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3435 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3436 {
3437 	struct i40e_vsi *vsi = ring->vsi;
3438 	u16 pf_q = vsi->base_queue + ring->queue_index;
3439 	struct i40e_hw *hw = &vsi->back->hw;
3440 	struct i40e_hmc_obj_txq tx_ctx;
3441 	u32 qtx_ctl = 0;
3442 	int err = 0;
3443 
3444 	if (ring_is_xdp(ring))
3445 		ring->xsk_pool = i40e_xsk_pool(ring);
3446 
3447 	/* some ATR related tx ring init */
3448 	if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
3449 		ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
3450 		ring->atr_count = 0;
3451 	} else {
3452 		ring->atr_sample_rate = 0;
3453 	}
3454 
3455 	/* configure XPS */
3456 	i40e_config_xps_tx_ring(ring);
3457 
3458 	/* clear the context structure first */
3459 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3460 
3461 	tx_ctx.new_context = 1;
3462 	tx_ctx.base = (ring->dma / 128);
3463 	tx_ctx.qlen = ring->count;
3464 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
3465 	    test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
3466 		tx_ctx.fd_ena = 1;
3467 	if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
3468 		tx_ctx.timesync_ena = 1;
3469 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3470 	if (vsi->type != I40E_VSI_FDIR)
3471 		tx_ctx.head_wb_ena = 1;
3472 	tx_ctx.head_wb_addr = ring->dma +
3473 			      (ring->count * sizeof(struct i40e_tx_desc));
3474 
3475 	/* As part of VSI creation/update, FW allocates certain
3476 	 * Tx arbitration queue sets for each TC enabled for
3477 	 * the VSI. The FW returns the handles to these queue
3478 	 * sets as part of the response buffer to Add VSI,
3479 	 * Update VSI, etc. AQ commands. It is expected that
3480 	 * these queue set handles be associated with the Tx
3481 	 * queues by the driver as part of the TX queue context
3482 	 * initialization. This has to be done regardless of
3483 	 * DCB as by default everything is mapped to TC0.
3484 	 */
3485 
3486 	if (ring->ch)
3487 		tx_ctx.rdylist =
3488 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3489 
3490 	else
3491 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3492 
3493 	tx_ctx.rdylist_act = 0;
3494 
3495 	/* clear the context in the HMC */
3496 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3497 	if (err) {
3498 		dev_info(&vsi->back->pdev->dev,
3499 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3500 			 ring->queue_index, pf_q, err);
3501 		return -ENOMEM;
3502 	}
3503 
3504 	/* set the context in the HMC */
3505 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3506 	if (err) {
3507 		dev_info(&vsi->back->pdev->dev,
3508 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3509 			 ring->queue_index, pf_q, err);
3510 		return -ENOMEM;
3511 	}
3512 
3513 	/* Now associate this queue with this PCI function */
3514 	if (ring->ch) {
3515 		if (ring->ch->type == I40E_VSI_VMDQ2)
3516 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3517 		else
3518 			return -EINVAL;
3519 
3520 		qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3521 				      ring->ch->vsi_number);
3522 	} else {
3523 		if (vsi->type == I40E_VSI_VMDQ2) {
3524 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3525 			qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3526 					      vsi->id);
3527 		} else {
3528 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3529 		}
3530 	}
3531 
3532 	qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_PF_INDX_MASK, hw->pf_id);
3533 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3534 	i40e_flush(hw);
3535 
3536 	/* cache tail off for easier writes later */
3537 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3538 
3539 	return 0;
3540 }
3541 
3542 /**
3543  * i40e_rx_offset - Return expected offset into page to access data
3544  * @rx_ring: Ring we are requesting offset of
3545  *
3546  * Returns the offset value for ring into the data buffer.
3547  */
i40e_rx_offset(struct i40e_ring * rx_ring)3548 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3549 {
3550 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3551 }
3552 
3553 /**
3554  * i40e_configure_rx_ring - Configure a receive ring context
3555  * @ring: The Rx ring to configure
3556  *
3557  * Configure the Rx descriptor ring in the HMC context.
3558  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3559 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3560 {
3561 	struct i40e_vsi *vsi = ring->vsi;
3562 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3563 	u16 pf_q = vsi->base_queue + ring->queue_index;
3564 	struct i40e_hw *hw = &vsi->back->hw;
3565 	struct i40e_hmc_obj_rxq rx_ctx;
3566 	int err = 0;
3567 	bool ok;
3568 
3569 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3570 
3571 	/* clear the context structure first */
3572 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3573 
3574 	ring->rx_buf_len = vsi->rx_buf_len;
3575 
3576 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3577 	if (ring->vsi->type != I40E_VSI_MAIN)
3578 		goto skip;
3579 
3580 	if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
3581 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3582 					 ring->queue_index,
3583 					 ring->q_vector->napi.napi_id,
3584 					 ring->rx_buf_len);
3585 		if (err)
3586 			return err;
3587 	}
3588 
3589 	ring->xsk_pool = i40e_xsk_pool(ring);
3590 	if (ring->xsk_pool) {
3591 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3592 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3593 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3594 					 ring->queue_index,
3595 					 ring->q_vector->napi.napi_id,
3596 					 ring->rx_buf_len);
3597 		if (err)
3598 			return err;
3599 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3600 						 MEM_TYPE_XSK_BUFF_POOL,
3601 						 NULL);
3602 		if (err)
3603 			return err;
3604 		dev_info(&vsi->back->pdev->dev,
3605 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3606 			 ring->queue_index);
3607 
3608 	} else {
3609 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3610 						 MEM_TYPE_PAGE_SHARED,
3611 						 NULL);
3612 		if (err)
3613 			return err;
3614 	}
3615 
3616 skip:
3617 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3618 
3619 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3620 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3621 
3622 	rx_ctx.base = (ring->dma / 128);
3623 	rx_ctx.qlen = ring->count;
3624 
3625 	/* use 16 byte descriptors */
3626 	rx_ctx.dsize = 0;
3627 
3628 	/* descriptor type is always zero
3629 	 * rx_ctx.dtype = 0;
3630 	 */
3631 	rx_ctx.hsplit_0 = 0;
3632 
3633 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3634 	if (hw->revision_id == 0)
3635 		rx_ctx.lrxqthresh = 0;
3636 	else
3637 		rx_ctx.lrxqthresh = 1;
3638 	rx_ctx.crcstrip = 1;
3639 	rx_ctx.l2tsel = 1;
3640 	/* this controls whether VLAN is stripped from inner headers */
3641 	rx_ctx.showiv = 0;
3642 	/* set the prefena field to 1 because the manual says to */
3643 	rx_ctx.prefena = 1;
3644 
3645 	/* clear the context in the HMC */
3646 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3647 	if (err) {
3648 		dev_info(&vsi->back->pdev->dev,
3649 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3650 			 ring->queue_index, pf_q, err);
3651 		return -ENOMEM;
3652 	}
3653 
3654 	/* set the context in the HMC */
3655 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3656 	if (err) {
3657 		dev_info(&vsi->back->pdev->dev,
3658 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3659 			 ring->queue_index, pf_q, err);
3660 		return -ENOMEM;
3661 	}
3662 
3663 	/* configure Rx buffer alignment */
3664 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
3665 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3666 			dev_info(&vsi->back->pdev->dev,
3667 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3668 			return -EOPNOTSUPP;
3669 		}
3670 		clear_ring_build_skb_enabled(ring);
3671 	} else {
3672 		set_ring_build_skb_enabled(ring);
3673 	}
3674 
3675 	ring->rx_offset = i40e_rx_offset(ring);
3676 
3677 	/* cache tail for quicker writes, and clear the reg before use */
3678 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3679 	writel(0, ring->tail);
3680 
3681 	if (ring->xsk_pool) {
3682 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3683 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3684 	} else {
3685 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3686 	}
3687 	if (!ok) {
3688 		/* Log this in case the user has forgotten to give the kernel
3689 		 * any buffers, even later in the application.
3690 		 */
3691 		dev_info(&vsi->back->pdev->dev,
3692 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3693 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3694 			 ring->queue_index, pf_q);
3695 	}
3696 
3697 	return 0;
3698 }
3699 
3700 /**
3701  * i40e_vsi_configure_tx - Configure the VSI for Tx
3702  * @vsi: VSI structure describing this set of rings and resources
3703  *
3704  * Configure the Tx VSI for operation.
3705  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3706 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3707 {
3708 	int err = 0;
3709 	u16 i;
3710 
3711 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3712 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3713 
3714 	if (err || !i40e_enabled_xdp_vsi(vsi))
3715 		return err;
3716 
3717 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3718 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3719 
3720 	return err;
3721 }
3722 
3723 /**
3724  * i40e_vsi_configure_rx - Configure the VSI for Rx
3725  * @vsi: the VSI being configured
3726  *
3727  * Configure the Rx VSI for operation.
3728  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3729 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3730 {
3731 	int err = 0;
3732 	u16 i;
3733 
3734 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3735 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3736 
3737 #if (PAGE_SIZE < 8192)
3738 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3739 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3740 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3741 		vsi->max_frame = vsi->rx_buf_len;
3742 	}
3743 #endif
3744 
3745 	/* set up individual rings */
3746 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3747 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3748 
3749 	return err;
3750 }
3751 
3752 /**
3753  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3754  * @vsi: ptr to the VSI
3755  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3756 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3757 {
3758 	struct i40e_ring *tx_ring, *rx_ring;
3759 	u16 qoffset, qcount;
3760 	int i, n;
3761 
3762 	if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
3763 		/* Reset the TC information */
3764 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3765 			rx_ring = vsi->rx_rings[i];
3766 			tx_ring = vsi->tx_rings[i];
3767 			rx_ring->dcb_tc = 0;
3768 			tx_ring->dcb_tc = 0;
3769 		}
3770 		return;
3771 	}
3772 
3773 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3774 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3775 			continue;
3776 
3777 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3778 		qcount = vsi->tc_config.tc_info[n].qcount;
3779 		for (i = qoffset; i < (qoffset + qcount); i++) {
3780 			rx_ring = vsi->rx_rings[i];
3781 			tx_ring = vsi->tx_rings[i];
3782 			rx_ring->dcb_tc = n;
3783 			tx_ring->dcb_tc = n;
3784 		}
3785 	}
3786 }
3787 
3788 /**
3789  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3790  * @vsi: ptr to the VSI
3791  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3792 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3793 {
3794 	if (vsi->netdev)
3795 		i40e_set_rx_mode(vsi->netdev);
3796 }
3797 
3798 /**
3799  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3800  * @pf: Pointer to the targeted PF
3801  *
3802  * Set all flow director counters to 0.
3803  */
i40e_reset_fdir_filter_cnt(struct i40e_pf * pf)3804 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3805 {
3806 	pf->fd_tcp4_filter_cnt = 0;
3807 	pf->fd_udp4_filter_cnt = 0;
3808 	pf->fd_sctp4_filter_cnt = 0;
3809 	pf->fd_ip4_filter_cnt = 0;
3810 	pf->fd_tcp6_filter_cnt = 0;
3811 	pf->fd_udp6_filter_cnt = 0;
3812 	pf->fd_sctp6_filter_cnt = 0;
3813 	pf->fd_ip6_filter_cnt = 0;
3814 }
3815 
3816 /**
3817  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3818  * @vsi: Pointer to the targeted VSI
3819  *
3820  * This function replays the hlist on the hw where all the SB Flow Director
3821  * filters were saved.
3822  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3823 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3824 {
3825 	struct i40e_fdir_filter *filter;
3826 	struct i40e_pf *pf = vsi->back;
3827 	struct hlist_node *node;
3828 
3829 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
3830 		return;
3831 
3832 	/* Reset FDir counters as we're replaying all existing filters */
3833 	i40e_reset_fdir_filter_cnt(pf);
3834 
3835 	hlist_for_each_entry_safe(filter, node,
3836 				  &pf->fdir_filter_list, fdir_node) {
3837 		i40e_add_del_fdir(vsi, filter, true);
3838 	}
3839 }
3840 
3841 /**
3842  * i40e_vsi_configure - Set up the VSI for action
3843  * @vsi: the VSI being configured
3844  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3845 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3846 {
3847 	int err;
3848 
3849 	i40e_set_vsi_rx_mode(vsi);
3850 	i40e_restore_vlan(vsi);
3851 	i40e_vsi_config_dcb_rings(vsi);
3852 	err = i40e_vsi_configure_tx(vsi);
3853 	if (!err)
3854 		err = i40e_vsi_configure_rx(vsi);
3855 
3856 	return err;
3857 }
3858 
3859 /**
3860  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3861  * @vsi: the VSI being configured
3862  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3863 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3864 {
3865 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3866 	struct i40e_pf *pf = vsi->back;
3867 	struct i40e_hw *hw = &pf->hw;
3868 	u16 vector;
3869 	int i, q;
3870 	u32 qp;
3871 
3872 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3873 	 * and PFINT_LNKLSTn registers, e.g.:
3874 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3875 	 */
3876 	qp = vsi->base_queue;
3877 	vector = vsi->base_vector;
3878 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3879 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3880 
3881 		q_vector->rx.next_update = jiffies + 1;
3882 		q_vector->rx.target_itr =
3883 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3884 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3885 		     q_vector->rx.target_itr >> 1);
3886 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3887 
3888 		q_vector->tx.next_update = jiffies + 1;
3889 		q_vector->tx.target_itr =
3890 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3891 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3892 		     q_vector->tx.target_itr >> 1);
3893 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3894 
3895 		/* Set ITR for software interrupts triggered after exiting
3896 		 * busy-loop polling.
3897 		 */
3898 		wr32(hw, I40E_PFINT_ITRN(I40E_SW_ITR, vector - 1),
3899 		     I40E_ITR_20K);
3900 
3901 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3902 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3903 
3904 		/* begin of linked list for RX queue assigned to this vector */
3905 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3906 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3907 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3908 			u32 val;
3909 
3910 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3911 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3912 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3913 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3914 			      (I40E_QUEUE_TYPE_TX <<
3915 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3916 
3917 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3918 
3919 			if (has_xdp) {
3920 				/* TX queue with next queue set to TX */
3921 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3922 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3923 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3924 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3925 				      (I40E_QUEUE_TYPE_TX <<
3926 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3927 
3928 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3929 			}
3930 			/* TX queue with next RX or end of linked list */
3931 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3932 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3933 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3934 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3935 			      (I40E_QUEUE_TYPE_RX <<
3936 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3937 
3938 			/* Terminate the linked list */
3939 			if (q == (q_vector->num_ringpairs - 1))
3940 				val |= (I40E_QUEUE_END_OF_LIST <<
3941 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3942 
3943 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3944 			qp++;
3945 		}
3946 	}
3947 
3948 	i40e_flush(hw);
3949 }
3950 
3951 /**
3952  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3953  * @pf: pointer to private device data structure
3954  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3955 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3956 {
3957 	struct i40e_hw *hw = &pf->hw;
3958 	u32 val;
3959 
3960 	/* clear things first */
3961 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3962 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3963 
3964 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3965 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3966 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3967 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3968 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3969 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3970 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3971 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3972 
3973 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
3974 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3975 
3976 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
3977 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3978 
3979 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3980 
3981 	/* SW_ITR_IDX = 0, but don't change INTENA */
3982 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3983 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3984 
3985 	/* OTHER_ITR_IDX = 0 */
3986 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3987 }
3988 
3989 /**
3990  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3991  * @vsi: the VSI being configured
3992  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)3993 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3994 {
3995 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3996 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3997 	struct i40e_pf *pf = vsi->back;
3998 	struct i40e_hw *hw = &pf->hw;
3999 
4000 	/* set the ITR configuration */
4001 	q_vector->rx.next_update = jiffies + 1;
4002 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4003 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4004 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4005 	q_vector->tx.next_update = jiffies + 1;
4006 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4007 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4008 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4009 
4010 	i40e_enable_misc_int_causes(pf);
4011 
4012 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4013 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4014 
4015 	/* Associate the queue pair to the vector and enable the queue
4016 	 * interrupt RX queue in linked list with next queue set to TX
4017 	 */
4018 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4019 
4020 	if (i40e_enabled_xdp_vsi(vsi)) {
4021 		/* TX queue in linked list with next queue set to TX */
4022 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4023 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4024 	}
4025 
4026 	/* last TX queue so the next RX queue doesn't matter */
4027 	wr32(hw, I40E_QINT_TQCTL(0),
4028 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4029 	i40e_flush(hw);
4030 }
4031 
4032 /**
4033  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4034  * @pf: board private structure
4035  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)4036 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4037 {
4038 	struct i40e_hw *hw = &pf->hw;
4039 
4040 	wr32(hw, I40E_PFINT_DYN_CTL0,
4041 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4042 	i40e_flush(hw);
4043 }
4044 
4045 /**
4046  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4047  * @pf: board private structure
4048  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)4049 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4050 {
4051 	struct i40e_hw *hw = &pf->hw;
4052 	u32 val;
4053 
4054 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4055 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4056 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4057 
4058 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4059 	i40e_flush(hw);
4060 }
4061 
4062 /**
4063  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4064  * @irq: interrupt number
4065  * @data: pointer to a q_vector
4066  **/
i40e_msix_clean_rings(int irq,void * data)4067 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4068 {
4069 	struct i40e_q_vector *q_vector = data;
4070 
4071 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4072 		return IRQ_HANDLED;
4073 
4074 	napi_schedule_irqoff(&q_vector->napi);
4075 
4076 	return IRQ_HANDLED;
4077 }
4078 
4079 /**
4080  * i40e_irq_affinity_notify - Callback for affinity changes
4081  * @notify: context as to what irq was changed
4082  * @mask: the new affinity mask
4083  *
4084  * This is a callback function used by the irq_set_affinity_notifier function
4085  * so that we may register to receive changes to the irq affinity masks.
4086  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)4087 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4088 				     const cpumask_t *mask)
4089 {
4090 	struct i40e_q_vector *q_vector =
4091 		container_of(notify, struct i40e_q_vector, affinity_notify);
4092 
4093 	cpumask_copy(&q_vector->affinity_mask, mask);
4094 }
4095 
4096 /**
4097  * i40e_irq_affinity_release - Callback for affinity notifier release
4098  * @ref: internal core kernel usage
4099  *
4100  * This is a callback function used by the irq_set_affinity_notifier function
4101  * to inform the current notification subscriber that they will no longer
4102  * receive notifications.
4103  **/
i40e_irq_affinity_release(struct kref * ref)4104 static void i40e_irq_affinity_release(struct kref *ref) {}
4105 
4106 /**
4107  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4108  * @vsi: the VSI being configured
4109  * @basename: name for the vector
4110  *
4111  * Allocates MSI-X vectors and requests interrupts from the kernel.
4112  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)4113 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4114 {
4115 	int q_vectors = vsi->num_q_vectors;
4116 	struct i40e_pf *pf = vsi->back;
4117 	int base = vsi->base_vector;
4118 	int rx_int_idx = 0;
4119 	int tx_int_idx = 0;
4120 	int vector, err;
4121 	int irq_num;
4122 	int cpu;
4123 
4124 	for (vector = 0; vector < q_vectors; vector++) {
4125 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4126 
4127 		irq_num = pf->msix_entries[base + vector].vector;
4128 
4129 		if (q_vector->tx.ring && q_vector->rx.ring) {
4130 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4131 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4132 			tx_int_idx++;
4133 		} else if (q_vector->rx.ring) {
4134 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4135 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4136 		} else if (q_vector->tx.ring) {
4137 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4138 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4139 		} else {
4140 			/* skip this unused q_vector */
4141 			continue;
4142 		}
4143 		err = request_irq(irq_num,
4144 				  vsi->irq_handler,
4145 				  0,
4146 				  q_vector->name,
4147 				  q_vector);
4148 		if (err) {
4149 			dev_info(&pf->pdev->dev,
4150 				 "MSIX request_irq failed, error: %d\n", err);
4151 			goto free_queue_irqs;
4152 		}
4153 
4154 		/* register for affinity change notifications */
4155 		q_vector->irq_num = irq_num;
4156 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4157 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4158 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4159 		/* Spread affinity hints out across online CPUs.
4160 		 *
4161 		 * get_cpu_mask returns a static constant mask with
4162 		 * a permanent lifetime so it's ok to pass to
4163 		 * irq_update_affinity_hint without making a copy.
4164 		 */
4165 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4166 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4167 	}
4168 
4169 	vsi->irqs_ready = true;
4170 	return 0;
4171 
4172 free_queue_irqs:
4173 	while (vector) {
4174 		vector--;
4175 		irq_num = pf->msix_entries[base + vector].vector;
4176 		irq_set_affinity_notifier(irq_num, NULL);
4177 		irq_update_affinity_hint(irq_num, NULL);
4178 		free_irq(irq_num, vsi->q_vectors[vector]);
4179 	}
4180 	return err;
4181 }
4182 
4183 /**
4184  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4185  * @vsi: the VSI being un-configured
4186  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)4187 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4188 {
4189 	struct i40e_pf *pf = vsi->back;
4190 	struct i40e_hw *hw = &pf->hw;
4191 	int base = vsi->base_vector;
4192 	int i;
4193 
4194 	/* disable interrupt causation from each queue */
4195 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4196 		u32 val;
4197 
4198 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4199 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4200 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4201 
4202 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4203 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4204 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4205 
4206 		if (!i40e_enabled_xdp_vsi(vsi))
4207 			continue;
4208 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4209 	}
4210 
4211 	/* disable each interrupt */
4212 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4213 		for (i = vsi->base_vector;
4214 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4215 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4216 
4217 		i40e_flush(hw);
4218 		for (i = 0; i < vsi->num_q_vectors; i++)
4219 			synchronize_irq(pf->msix_entries[i + base].vector);
4220 	} else {
4221 		/* Legacy and MSI mode - this stops all interrupt handling */
4222 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4223 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4224 		i40e_flush(hw);
4225 		synchronize_irq(pf->pdev->irq);
4226 	}
4227 }
4228 
4229 /**
4230  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4231  * @vsi: the VSI being configured
4232  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)4233 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4234 {
4235 	struct i40e_pf *pf = vsi->back;
4236 	int i;
4237 
4238 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4239 		for (i = 0; i < vsi->num_q_vectors; i++)
4240 			i40e_irq_dynamic_enable(vsi, i);
4241 	} else {
4242 		i40e_irq_dynamic_enable_icr0(pf);
4243 	}
4244 
4245 	i40e_flush(&pf->hw);
4246 	return 0;
4247 }
4248 
4249 /**
4250  * i40e_free_misc_vector - Free the vector that handles non-queue events
4251  * @pf: board private structure
4252  **/
i40e_free_misc_vector(struct i40e_pf * pf)4253 static void i40e_free_misc_vector(struct i40e_pf *pf)
4254 {
4255 	/* Disable ICR 0 */
4256 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4257 	i40e_flush(&pf->hw);
4258 
4259 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
4260 		free_irq(pf->msix_entries[0].vector, pf);
4261 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4262 	}
4263 }
4264 
4265 /**
4266  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4267  * @irq: interrupt number
4268  * @data: pointer to a q_vector
4269  *
4270  * This is the handler used for all MSI/Legacy interrupts, and deals
4271  * with both queue and non-queue interrupts.  This is also used in
4272  * MSIX mode to handle the non-queue interrupts.
4273  **/
i40e_intr(int irq,void * data)4274 static irqreturn_t i40e_intr(int irq, void *data)
4275 {
4276 	struct i40e_pf *pf = (struct i40e_pf *)data;
4277 	struct i40e_hw *hw = &pf->hw;
4278 	irqreturn_t ret = IRQ_NONE;
4279 	u32 icr0, icr0_remaining;
4280 	u32 val, ena_mask;
4281 
4282 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4283 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4284 
4285 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4286 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4287 		goto enable_intr;
4288 
4289 	/* if interrupt but no bits showing, must be SWINT */
4290 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4291 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4292 		pf->sw_int_count++;
4293 
4294 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
4295 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4296 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4297 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4298 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4299 	}
4300 
4301 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4302 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4303 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
4304 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4305 
4306 		/* We do not have a way to disarm Queue causes while leaving
4307 		 * interrupt enabled for all other causes, ideally
4308 		 * interrupt should be disabled while we are in NAPI but
4309 		 * this is not a performance path and napi_schedule()
4310 		 * can deal with rescheduling.
4311 		 */
4312 		if (!test_bit(__I40E_DOWN, pf->state))
4313 			napi_schedule_irqoff(&q_vector->napi);
4314 	}
4315 
4316 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4317 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4318 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4319 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4320 	}
4321 
4322 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4323 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4324 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4325 	}
4326 
4327 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4328 		/* disable any further VFLR event notifications */
4329 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4330 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4331 
4332 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4333 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4334 		} else {
4335 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4336 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4337 		}
4338 	}
4339 
4340 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4341 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4342 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4343 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4344 		val = rd32(hw, I40E_GLGEN_RSTAT);
4345 		val = FIELD_GET(I40E_GLGEN_RSTAT_RESET_TYPE_MASK, val);
4346 		if (val == I40E_RESET_CORER) {
4347 			pf->corer_count++;
4348 		} else if (val == I40E_RESET_GLOBR) {
4349 			pf->globr_count++;
4350 		} else if (val == I40E_RESET_EMPR) {
4351 			pf->empr_count++;
4352 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4353 		}
4354 	}
4355 
4356 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4357 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4358 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4359 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4360 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4361 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4362 	}
4363 
4364 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4365 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4366 
4367 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4368 			schedule_work(&pf->ptp_extts0_work);
4369 
4370 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4371 			i40e_ptp_tx_hwtstamp(pf);
4372 
4373 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4374 	}
4375 
4376 	/* If a critical error is pending we have no choice but to reset the
4377 	 * device.
4378 	 * Report and mask out any remaining unexpected interrupts.
4379 	 */
4380 	icr0_remaining = icr0 & ena_mask;
4381 	if (icr0_remaining) {
4382 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4383 			 icr0_remaining);
4384 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4385 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4386 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4387 			dev_info(&pf->pdev->dev, "device will be reset\n");
4388 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4389 			i40e_service_event_schedule(pf);
4390 		}
4391 		ena_mask &= ~icr0_remaining;
4392 	}
4393 	ret = IRQ_HANDLED;
4394 
4395 enable_intr:
4396 	/* re-enable interrupt causes */
4397 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4398 	if (!test_bit(__I40E_DOWN, pf->state) ||
4399 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4400 		i40e_service_event_schedule(pf);
4401 		i40e_irq_dynamic_enable_icr0(pf);
4402 	}
4403 
4404 	return ret;
4405 }
4406 
4407 /**
4408  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4409  * @tx_ring:  tx ring to clean
4410  * @budget:   how many cleans we're allowed
4411  *
4412  * Returns true if there's any budget left (e.g. the clean is finished)
4413  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4414 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4415 {
4416 	struct i40e_vsi *vsi = tx_ring->vsi;
4417 	u16 i = tx_ring->next_to_clean;
4418 	struct i40e_tx_buffer *tx_buf;
4419 	struct i40e_tx_desc *tx_desc;
4420 
4421 	tx_buf = &tx_ring->tx_bi[i];
4422 	tx_desc = I40E_TX_DESC(tx_ring, i);
4423 	i -= tx_ring->count;
4424 
4425 	do {
4426 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4427 
4428 		/* if next_to_watch is not set then there is no work pending */
4429 		if (!eop_desc)
4430 			break;
4431 
4432 		/* prevent any other reads prior to eop_desc */
4433 		smp_rmb();
4434 
4435 		/* if the descriptor isn't done, no work yet to do */
4436 		if (!(eop_desc->cmd_type_offset_bsz &
4437 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4438 			break;
4439 
4440 		/* clear next_to_watch to prevent false hangs */
4441 		tx_buf->next_to_watch = NULL;
4442 
4443 		tx_desc->buffer_addr = 0;
4444 		tx_desc->cmd_type_offset_bsz = 0;
4445 		/* move past filter desc */
4446 		tx_buf++;
4447 		tx_desc++;
4448 		i++;
4449 		if (unlikely(!i)) {
4450 			i -= tx_ring->count;
4451 			tx_buf = tx_ring->tx_bi;
4452 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4453 		}
4454 		/* unmap skb header data */
4455 		dma_unmap_single(tx_ring->dev,
4456 				 dma_unmap_addr(tx_buf, dma),
4457 				 dma_unmap_len(tx_buf, len),
4458 				 DMA_TO_DEVICE);
4459 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4460 			kfree(tx_buf->raw_buf);
4461 
4462 		tx_buf->raw_buf = NULL;
4463 		tx_buf->tx_flags = 0;
4464 		tx_buf->next_to_watch = NULL;
4465 		dma_unmap_len_set(tx_buf, len, 0);
4466 		tx_desc->buffer_addr = 0;
4467 		tx_desc->cmd_type_offset_bsz = 0;
4468 
4469 		/* move us past the eop_desc for start of next FD desc */
4470 		tx_buf++;
4471 		tx_desc++;
4472 		i++;
4473 		if (unlikely(!i)) {
4474 			i -= tx_ring->count;
4475 			tx_buf = tx_ring->tx_bi;
4476 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4477 		}
4478 
4479 		/* update budget accounting */
4480 		budget--;
4481 	} while (likely(budget));
4482 
4483 	i += tx_ring->count;
4484 	tx_ring->next_to_clean = i;
4485 
4486 	if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
4487 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4488 
4489 	return budget > 0;
4490 }
4491 
4492 /**
4493  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4494  * @irq: interrupt number
4495  * @data: pointer to a q_vector
4496  **/
i40e_fdir_clean_ring(int irq,void * data)4497 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4498 {
4499 	struct i40e_q_vector *q_vector = data;
4500 	struct i40e_vsi *vsi;
4501 
4502 	if (!q_vector->tx.ring)
4503 		return IRQ_HANDLED;
4504 
4505 	vsi = q_vector->tx.ring->vsi;
4506 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4507 
4508 	return IRQ_HANDLED;
4509 }
4510 
4511 /**
4512  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4513  * @vsi: the VSI being configured
4514  * @v_idx: vector index
4515  * @qp_idx: queue pair index
4516  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4517 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4518 {
4519 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4520 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4521 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4522 
4523 	tx_ring->q_vector = q_vector;
4524 	tx_ring->next = q_vector->tx.ring;
4525 	q_vector->tx.ring = tx_ring;
4526 	q_vector->tx.count++;
4527 
4528 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4529 	if (i40e_enabled_xdp_vsi(vsi)) {
4530 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4531 
4532 		xdp_ring->q_vector = q_vector;
4533 		xdp_ring->next = q_vector->tx.ring;
4534 		q_vector->tx.ring = xdp_ring;
4535 		q_vector->tx.count++;
4536 	}
4537 
4538 	rx_ring->q_vector = q_vector;
4539 	rx_ring->next = q_vector->rx.ring;
4540 	q_vector->rx.ring = rx_ring;
4541 	q_vector->rx.count++;
4542 }
4543 
4544 /**
4545  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4546  * @vsi: the VSI being configured
4547  *
4548  * This function maps descriptor rings to the queue-specific vectors
4549  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4550  * one vector per queue pair, but on a constrained vector budget, we
4551  * group the queue pairs as "efficiently" as possible.
4552  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4553 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4554 {
4555 	int qp_remaining = vsi->num_queue_pairs;
4556 	int q_vectors = vsi->num_q_vectors;
4557 	int num_ringpairs;
4558 	int v_start = 0;
4559 	int qp_idx = 0;
4560 
4561 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4562 	 * group them so there are multiple queues per vector.
4563 	 * It is also important to go through all the vectors available to be
4564 	 * sure that if we don't use all the vectors, that the remaining vectors
4565 	 * are cleared. This is especially important when decreasing the
4566 	 * number of queues in use.
4567 	 */
4568 	for (; v_start < q_vectors; v_start++) {
4569 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4570 
4571 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4572 
4573 		q_vector->num_ringpairs = num_ringpairs;
4574 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4575 
4576 		q_vector->rx.count = 0;
4577 		q_vector->tx.count = 0;
4578 		q_vector->rx.ring = NULL;
4579 		q_vector->tx.ring = NULL;
4580 
4581 		while (num_ringpairs--) {
4582 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4583 			qp_idx++;
4584 			qp_remaining--;
4585 		}
4586 	}
4587 }
4588 
4589 /**
4590  * i40e_vsi_request_irq - Request IRQ from the OS
4591  * @vsi: the VSI being configured
4592  * @basename: name for the vector
4593  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4594 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4595 {
4596 	struct i40e_pf *pf = vsi->back;
4597 	int err;
4598 
4599 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
4600 		err = i40e_vsi_request_irq_msix(vsi, basename);
4601 	else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
4602 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4603 				  pf->int_name, pf);
4604 	else
4605 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4606 				  pf->int_name, pf);
4607 
4608 	if (err)
4609 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4610 
4611 	return err;
4612 }
4613 
4614 #ifdef CONFIG_NET_POLL_CONTROLLER
4615 /**
4616  * i40e_netpoll - A Polling 'interrupt' handler
4617  * @netdev: network interface device structure
4618  *
4619  * This is used by netconsole to send skbs without having to re-enable
4620  * interrupts.  It's not called while the normal interrupt routine is executing.
4621  **/
i40e_netpoll(struct net_device * netdev)4622 static void i40e_netpoll(struct net_device *netdev)
4623 {
4624 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4625 	struct i40e_vsi *vsi = np->vsi;
4626 	struct i40e_pf *pf = vsi->back;
4627 	int i;
4628 
4629 	/* if interface is down do nothing */
4630 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4631 		return;
4632 
4633 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4634 		for (i = 0; i < vsi->num_q_vectors; i++)
4635 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4636 	} else {
4637 		i40e_intr(pf->pdev->irq, netdev);
4638 	}
4639 }
4640 #endif
4641 
4642 #define I40E_QTX_ENA_WAIT_COUNT 50
4643 
4644 /**
4645  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4646  * @pf: the PF being configured
4647  * @pf_q: the PF queue
4648  * @enable: enable or disable state of the queue
4649  *
4650  * This routine will wait for the given Tx queue of the PF to reach the
4651  * enabled or disabled state.
4652  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4653  * multiple retries; else will return 0 in case of success.
4654  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4655 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4656 {
4657 	int i;
4658 	u32 tx_reg;
4659 
4660 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4661 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4662 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4663 			break;
4664 
4665 		usleep_range(10, 20);
4666 	}
4667 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4668 		return -ETIMEDOUT;
4669 
4670 	return 0;
4671 }
4672 
4673 /**
4674  * i40e_control_tx_q - Start or stop a particular Tx queue
4675  * @pf: the PF structure
4676  * @pf_q: the PF queue to configure
4677  * @enable: start or stop the queue
4678  *
4679  * This function enables or disables a single queue. Note that any delay
4680  * required after the operation is expected to be handled by the caller of
4681  * this function.
4682  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4683 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4684 {
4685 	struct i40e_hw *hw = &pf->hw;
4686 	u32 tx_reg;
4687 	int i;
4688 
4689 	/* warn the TX unit of coming changes */
4690 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4691 	if (!enable)
4692 		usleep_range(10, 20);
4693 
4694 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4695 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4696 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4697 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4698 			break;
4699 		usleep_range(1000, 2000);
4700 	}
4701 
4702 	/* Skip if the queue is already in the requested state */
4703 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4704 		return;
4705 
4706 	/* turn on/off the queue */
4707 	if (enable) {
4708 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4709 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4710 	} else {
4711 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4712 	}
4713 
4714 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4715 }
4716 
4717 /**
4718  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4719  * @seid: VSI SEID
4720  * @pf: the PF structure
4721  * @pf_q: the PF queue to configure
4722  * @is_xdp: true if the queue is used for XDP
4723  * @enable: start or stop the queue
4724  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4725 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4726 			   bool is_xdp, bool enable)
4727 {
4728 	int ret;
4729 
4730 	i40e_control_tx_q(pf, pf_q, enable);
4731 
4732 	/* wait for the change to finish */
4733 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4734 	if (ret) {
4735 		dev_info(&pf->pdev->dev,
4736 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4737 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4738 			 (enable ? "en" : "dis"));
4739 	}
4740 
4741 	return ret;
4742 }
4743 
4744 /**
4745  * i40e_vsi_enable_tx - Start a VSI's rings
4746  * @vsi: the VSI being configured
4747  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4748 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4749 {
4750 	struct i40e_pf *pf = vsi->back;
4751 	int i, pf_q, ret = 0;
4752 
4753 	pf_q = vsi->base_queue;
4754 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4755 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4756 					     pf_q,
4757 					     false /*is xdp*/, true);
4758 		if (ret)
4759 			break;
4760 
4761 		if (!i40e_enabled_xdp_vsi(vsi))
4762 			continue;
4763 
4764 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4765 					     pf_q + vsi->alloc_queue_pairs,
4766 					     true /*is xdp*/, true);
4767 		if (ret)
4768 			break;
4769 	}
4770 	return ret;
4771 }
4772 
4773 /**
4774  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4775  * @pf: the PF being configured
4776  * @pf_q: the PF queue
4777  * @enable: enable or disable state of the queue
4778  *
4779  * This routine will wait for the given Rx queue of the PF to reach the
4780  * enabled or disabled state.
4781  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4782  * multiple retries; else will return 0 in case of success.
4783  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4784 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4785 {
4786 	int i;
4787 	u32 rx_reg;
4788 
4789 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4790 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4791 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4792 			break;
4793 
4794 		usleep_range(10, 20);
4795 	}
4796 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4797 		return -ETIMEDOUT;
4798 
4799 	return 0;
4800 }
4801 
4802 /**
4803  * i40e_control_rx_q - Start or stop a particular Rx queue
4804  * @pf: the PF structure
4805  * @pf_q: the PF queue to configure
4806  * @enable: start or stop the queue
4807  *
4808  * This function enables or disables a single queue. Note that
4809  * any delay required after the operation is expected to be
4810  * handled by the caller of this function.
4811  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4812 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4813 {
4814 	struct i40e_hw *hw = &pf->hw;
4815 	u32 rx_reg;
4816 	int i;
4817 
4818 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4819 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4820 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4821 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4822 			break;
4823 		usleep_range(1000, 2000);
4824 	}
4825 
4826 	/* Skip if the queue is already in the requested state */
4827 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4828 		return;
4829 
4830 	/* turn on/off the queue */
4831 	if (enable)
4832 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4833 	else
4834 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4835 
4836 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4837 }
4838 
4839 /**
4840  * i40e_control_wait_rx_q
4841  * @pf: the PF structure
4842  * @pf_q: queue being configured
4843  * @enable: start or stop the rings
4844  *
4845  * This function enables or disables a single queue along with waiting
4846  * for the change to finish. The caller of this function should handle
4847  * the delays needed in the case of disabling queues.
4848  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4849 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4850 {
4851 	int ret = 0;
4852 
4853 	i40e_control_rx_q(pf, pf_q, enable);
4854 
4855 	/* wait for the change to finish */
4856 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4857 	if (ret)
4858 		return ret;
4859 
4860 	return ret;
4861 }
4862 
4863 /**
4864  * i40e_vsi_enable_rx - Start a VSI's rings
4865  * @vsi: the VSI being configured
4866  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4867 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4868 {
4869 	struct i40e_pf *pf = vsi->back;
4870 	int i, pf_q, ret = 0;
4871 
4872 	pf_q = vsi->base_queue;
4873 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4874 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4875 		if (ret) {
4876 			dev_info(&pf->pdev->dev,
4877 				 "VSI seid %d Rx ring %d enable timeout\n",
4878 				 vsi->seid, pf_q);
4879 			break;
4880 		}
4881 	}
4882 
4883 	return ret;
4884 }
4885 
4886 /**
4887  * i40e_vsi_start_rings - Start a VSI's rings
4888  * @vsi: the VSI being configured
4889  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4890 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4891 {
4892 	int ret = 0;
4893 
4894 	/* do rx first for enable and last for disable */
4895 	ret = i40e_vsi_enable_rx(vsi);
4896 	if (ret)
4897 		return ret;
4898 	ret = i40e_vsi_enable_tx(vsi);
4899 
4900 	return ret;
4901 }
4902 
4903 #define I40E_DISABLE_TX_GAP_MSEC	50
4904 
4905 /**
4906  * i40e_vsi_stop_rings - Stop a VSI's rings
4907  * @vsi: the VSI being configured
4908  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4909 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4910 {
4911 	struct i40e_pf *pf = vsi->back;
4912 	u32 pf_q, tx_q_end, rx_q_end;
4913 
4914 	/* When port TX is suspended, don't wait */
4915 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4916 		return i40e_vsi_stop_rings_no_wait(vsi);
4917 
4918 	tx_q_end = vsi->base_queue +
4919 		vsi->alloc_queue_pairs * (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
4920 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4921 		i40e_pre_tx_queue_cfg(&pf->hw, pf_q, false);
4922 
4923 	rx_q_end = vsi->base_queue + vsi->num_queue_pairs;
4924 	for (pf_q = vsi->base_queue; pf_q < rx_q_end; pf_q++)
4925 		i40e_control_rx_q(pf, pf_q, false);
4926 
4927 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4928 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4929 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4930 
4931 	i40e_vsi_wait_queues_disabled(vsi);
4932 }
4933 
4934 /**
4935  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4936  * @vsi: the VSI being shutdown
4937  *
4938  * This function stops all the rings for a VSI but does not delay to verify
4939  * that rings have been disabled. It is expected that the caller is shutting
4940  * down multiple VSIs at once and will delay together for all the VSIs after
4941  * initiating the shutdown. This is particularly useful for shutting down lots
4942  * of VFs together. Otherwise, a large delay can be incurred while configuring
4943  * each VSI in serial.
4944  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4945 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4946 {
4947 	struct i40e_pf *pf = vsi->back;
4948 	int i, pf_q;
4949 
4950 	pf_q = vsi->base_queue;
4951 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4952 		i40e_control_tx_q(pf, pf_q, false);
4953 		i40e_control_rx_q(pf, pf_q, false);
4954 	}
4955 }
4956 
4957 /**
4958  * i40e_vsi_free_irq - Free the irq association with the OS
4959  * @vsi: the VSI being configured
4960  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)4961 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4962 {
4963 	struct i40e_pf *pf = vsi->back;
4964 	struct i40e_hw *hw = &pf->hw;
4965 	int base = vsi->base_vector;
4966 	u32 val, qp;
4967 	int i;
4968 
4969 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4970 		if (!vsi->q_vectors)
4971 			return;
4972 
4973 		if (!vsi->irqs_ready)
4974 			return;
4975 
4976 		vsi->irqs_ready = false;
4977 		for (i = 0; i < vsi->num_q_vectors; i++) {
4978 			int irq_num;
4979 			u16 vector;
4980 
4981 			vector = i + base;
4982 			irq_num = pf->msix_entries[vector].vector;
4983 
4984 			/* free only the irqs that were actually requested */
4985 			if (!vsi->q_vectors[i] ||
4986 			    !vsi->q_vectors[i]->num_ringpairs)
4987 				continue;
4988 
4989 			/* clear the affinity notifier in the IRQ descriptor */
4990 			irq_set_affinity_notifier(irq_num, NULL);
4991 			/* remove our suggested affinity mask for this IRQ */
4992 			irq_update_affinity_hint(irq_num, NULL);
4993 			free_irq(irq_num, vsi->q_vectors[i]);
4994 
4995 			/* Tear down the interrupt queue link list
4996 			 *
4997 			 * We know that they come in pairs and always
4998 			 * the Rx first, then the Tx.  To clear the
4999 			 * link list, stick the EOL value into the
5000 			 * next_q field of the registers.
5001 			 */
5002 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5003 			qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK,
5004 				       val);
5005 			val |= I40E_QUEUE_END_OF_LIST
5006 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5007 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5008 
5009 			while (qp != I40E_QUEUE_END_OF_LIST) {
5010 				u32 next;
5011 
5012 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5013 
5014 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5015 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5016 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5017 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5018 
5019 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5020 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5021 
5022 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5023 
5024 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5025 
5026 				next = FIELD_GET(I40E_QINT_TQCTL_NEXTQ_INDX_MASK,
5027 						 val);
5028 
5029 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5030 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5031 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5032 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5033 
5034 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5035 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5036 
5037 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5038 				qp = next;
5039 			}
5040 		}
5041 	} else {
5042 		free_irq(pf->pdev->irq, pf);
5043 
5044 		val = rd32(hw, I40E_PFINT_LNKLST0);
5045 		qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK, val);
5046 		val |= I40E_QUEUE_END_OF_LIST
5047 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5048 		wr32(hw, I40E_PFINT_LNKLST0, val);
5049 
5050 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5051 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5052 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5053 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5054 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5055 
5056 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5057 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5058 
5059 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5060 
5061 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5062 
5063 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5064 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5065 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5066 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5067 
5068 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5069 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5070 
5071 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5072 	}
5073 }
5074 
5075 /**
5076  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5077  * @vsi: the VSI being configured
5078  * @v_idx: Index of vector to be freed
5079  *
5080  * This function frees the memory allocated to the q_vector.  In addition if
5081  * NAPI is enabled it will delete any references to the NAPI struct prior
5082  * to freeing the q_vector.
5083  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)5084 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5085 {
5086 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5087 	struct i40e_ring *ring;
5088 
5089 	if (!q_vector)
5090 		return;
5091 
5092 	/* disassociate q_vector from rings */
5093 	i40e_for_each_ring(ring, q_vector->tx)
5094 		ring->q_vector = NULL;
5095 
5096 	i40e_for_each_ring(ring, q_vector->rx)
5097 		ring->q_vector = NULL;
5098 
5099 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5100 	if (vsi->netdev)
5101 		netif_napi_del(&q_vector->napi);
5102 
5103 	vsi->q_vectors[v_idx] = NULL;
5104 
5105 	kfree_rcu(q_vector, rcu);
5106 }
5107 
5108 /**
5109  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5110  * @vsi: the VSI being un-configured
5111  *
5112  * This frees the memory allocated to the q_vectors and
5113  * deletes references to the NAPI struct.
5114  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)5115 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5116 {
5117 	int v_idx;
5118 
5119 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5120 		i40e_free_q_vector(vsi, v_idx);
5121 }
5122 
5123 /**
5124  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5125  * @pf: board private structure
5126  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)5127 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5128 {
5129 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5130 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5131 		pci_disable_msix(pf->pdev);
5132 		kfree(pf->msix_entries);
5133 		pf->msix_entries = NULL;
5134 		kfree(pf->irq_pile);
5135 		pf->irq_pile = NULL;
5136 	} else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
5137 		pci_disable_msi(pf->pdev);
5138 	}
5139 	clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
5140 	clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
5141 }
5142 
5143 /**
5144  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5145  * @pf: board private structure
5146  *
5147  * We go through and clear interrupt specific resources and reset the structure
5148  * to pre-load conditions
5149  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)5150 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5151 {
5152 	struct i40e_vsi *vsi;
5153 	int i;
5154 
5155 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5156 		i40e_free_misc_vector(pf);
5157 
5158 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5159 		      I40E_IWARP_IRQ_PILE_ID);
5160 
5161 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5162 
5163 	i40e_pf_for_each_vsi(pf, i, vsi)
5164 		i40e_vsi_free_q_vectors(vsi);
5165 
5166 	i40e_reset_interrupt_capability(pf);
5167 }
5168 
5169 /**
5170  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5171  * @vsi: the VSI being configured
5172  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)5173 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5174 {
5175 	int q_idx;
5176 
5177 	if (!vsi->netdev)
5178 		return;
5179 
5180 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5181 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5182 
5183 		if (q_vector->rx.ring || q_vector->tx.ring)
5184 			napi_enable(&q_vector->napi);
5185 	}
5186 }
5187 
5188 /**
5189  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5190  * @vsi: the VSI being configured
5191  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)5192 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5193 {
5194 	int q_idx;
5195 
5196 	if (!vsi->netdev)
5197 		return;
5198 
5199 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5200 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5201 
5202 		if (q_vector->rx.ring || q_vector->tx.ring)
5203 			napi_disable(&q_vector->napi);
5204 	}
5205 }
5206 
5207 /**
5208  * i40e_vsi_close - Shut down a VSI
5209  * @vsi: the vsi to be quelled
5210  **/
i40e_vsi_close(struct i40e_vsi * vsi)5211 static void i40e_vsi_close(struct i40e_vsi *vsi)
5212 {
5213 	struct i40e_pf *pf = vsi->back;
5214 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5215 		i40e_down(vsi);
5216 	i40e_vsi_free_irq(vsi);
5217 	i40e_vsi_free_tx_resources(vsi);
5218 	i40e_vsi_free_rx_resources(vsi);
5219 	vsi->current_netdev_flags = 0;
5220 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5221 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5222 		set_bit(__I40E_CLIENT_RESET, pf->state);
5223 }
5224 
5225 /**
5226  * i40e_quiesce_vsi - Pause a given VSI
5227  * @vsi: the VSI being paused
5228  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)5229 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5230 {
5231 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5232 		return;
5233 
5234 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5235 	if (vsi->netdev && netif_running(vsi->netdev))
5236 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5237 	else
5238 		i40e_vsi_close(vsi);
5239 }
5240 
5241 /**
5242  * i40e_unquiesce_vsi - Resume a given VSI
5243  * @vsi: the VSI being resumed
5244  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)5245 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5246 {
5247 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5248 		return;
5249 
5250 	if (vsi->netdev && netif_running(vsi->netdev))
5251 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5252 	else
5253 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5254 }
5255 
5256 /**
5257  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5258  * @pf: the PF
5259  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)5260 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5261 {
5262 	struct i40e_vsi *vsi;
5263 	int v;
5264 
5265 	i40e_pf_for_each_vsi(pf, v, vsi)
5266 		i40e_quiesce_vsi(vsi);
5267 }
5268 
5269 /**
5270  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5271  * @pf: the PF
5272  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5273 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5274 {
5275 	struct i40e_vsi *vsi;
5276 	int v;
5277 
5278 	i40e_pf_for_each_vsi(pf, v, vsi)
5279 		i40e_unquiesce_vsi(vsi);
5280 }
5281 
5282 /**
5283  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5284  * @vsi: the VSI being configured
5285  *
5286  * Wait until all queues on a given VSI have been disabled.
5287  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5288 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5289 {
5290 	struct i40e_pf *pf = vsi->back;
5291 	int i, pf_q, ret;
5292 
5293 	pf_q = vsi->base_queue;
5294 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5295 		/* Check and wait for the Tx queue */
5296 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5297 		if (ret) {
5298 			dev_info(&pf->pdev->dev,
5299 				 "VSI seid %d Tx ring %d disable timeout\n",
5300 				 vsi->seid, pf_q);
5301 			return ret;
5302 		}
5303 
5304 		if (!i40e_enabled_xdp_vsi(vsi))
5305 			goto wait_rx;
5306 
5307 		/* Check and wait for the XDP Tx queue */
5308 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5309 				       false);
5310 		if (ret) {
5311 			dev_info(&pf->pdev->dev,
5312 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5313 				 vsi->seid, pf_q);
5314 			return ret;
5315 		}
5316 wait_rx:
5317 		/* Check and wait for the Rx queue */
5318 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5319 		if (ret) {
5320 			dev_info(&pf->pdev->dev,
5321 				 "VSI seid %d Rx ring %d disable timeout\n",
5322 				 vsi->seid, pf_q);
5323 			return ret;
5324 		}
5325 	}
5326 
5327 	return 0;
5328 }
5329 
5330 #ifdef CONFIG_I40E_DCB
5331 /**
5332  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5333  * @pf: the PF
5334  *
5335  * This function waits for the queues to be in disabled state for all the
5336  * VSIs that are managed by this PF.
5337  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5338 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5339 {
5340 	struct i40e_vsi *vsi;
5341 	int v, ret = 0;
5342 
5343 	i40e_pf_for_each_vsi(pf, v, vsi) {
5344 		ret = i40e_vsi_wait_queues_disabled(vsi);
5345 		if (ret)
5346 			break;
5347 	}
5348 
5349 	return ret;
5350 }
5351 
5352 #endif
5353 
5354 /**
5355  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5356  * @pf: pointer to PF
5357  *
5358  * Get TC map for ISCSI PF type that will include iSCSI TC
5359  * and LAN TC.
5360  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5361 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5362 {
5363 	struct i40e_dcb_app_priority_table app;
5364 	struct i40e_hw *hw = &pf->hw;
5365 	u8 enabled_tc = 1; /* TC0 is always enabled */
5366 	u8 tc, i;
5367 	/* Get the iSCSI APP TLV */
5368 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5369 
5370 	for (i = 0; i < dcbcfg->numapps; i++) {
5371 		app = dcbcfg->app[i];
5372 		if (app.selector == I40E_APP_SEL_TCPIP &&
5373 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5374 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5375 			enabled_tc |= BIT(tc);
5376 			break;
5377 		}
5378 	}
5379 
5380 	return enabled_tc;
5381 }
5382 
5383 /**
5384  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5385  * @dcbcfg: the corresponding DCBx configuration structure
5386  *
5387  * Return the number of TCs from given DCBx configuration
5388  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5389 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5390 {
5391 	int i, tc_unused = 0;
5392 	u8 num_tc = 0;
5393 	u8 ret = 0;
5394 
5395 	/* Scan the ETS Config Priority Table to find
5396 	 * traffic class enabled for a given priority
5397 	 * and create a bitmask of enabled TCs
5398 	 */
5399 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5400 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5401 
5402 	/* Now scan the bitmask to check for
5403 	 * contiguous TCs starting with TC0
5404 	 */
5405 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5406 		if (num_tc & BIT(i)) {
5407 			if (!tc_unused) {
5408 				ret++;
5409 			} else {
5410 				pr_err("Non-contiguous TC - Disabling DCB\n");
5411 				return 1;
5412 			}
5413 		} else {
5414 			tc_unused = 1;
5415 		}
5416 	}
5417 
5418 	/* There is always at least TC0 */
5419 	if (!ret)
5420 		ret = 1;
5421 
5422 	return ret;
5423 }
5424 
5425 /**
5426  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5427  * @dcbcfg: the corresponding DCBx configuration structure
5428  *
5429  * Query the current DCB configuration and return the number of
5430  * traffic classes enabled from the given DCBX config
5431  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5432 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5433 {
5434 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5435 	u8 enabled_tc = 1;
5436 	u8 i;
5437 
5438 	for (i = 0; i < num_tc; i++)
5439 		enabled_tc |= BIT(i);
5440 
5441 	return enabled_tc;
5442 }
5443 
5444 /**
5445  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5446  * @pf: PF being queried
5447  *
5448  * Query the current MQPRIO configuration and return the number of
5449  * traffic classes enabled.
5450  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5451 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5452 {
5453 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5454 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5455 	u8 enabled_tc = 1, i;
5456 
5457 	for (i = 1; i < num_tc; i++)
5458 		enabled_tc |= BIT(i);
5459 	return enabled_tc;
5460 }
5461 
5462 /**
5463  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5464  * @pf: PF being queried
5465  *
5466  * Return number of traffic classes enabled for the given PF
5467  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5468 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5469 {
5470 	u8 i, enabled_tc = 1;
5471 	u8 num_tc = 0;
5472 
5473 	if (i40e_is_tc_mqprio_enabled(pf)) {
5474 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5475 
5476 		return vsi->mqprio_qopt.qopt.num_tc;
5477 	}
5478 
5479 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5480 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5481 		return 1;
5482 
5483 	/* SFP mode will be enabled for all TCs on port */
5484 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5485 		return i40e_dcb_get_num_tc(&pf->hw.local_dcbx_config);
5486 
5487 	/* MFP mode return count of enabled TCs for this PF */
5488 	if (pf->hw.func_caps.iscsi)
5489 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5490 	else
5491 		return 1; /* Only TC0 */
5492 
5493 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5494 		if (enabled_tc & BIT(i))
5495 			num_tc++;
5496 	}
5497 	return num_tc;
5498 }
5499 
5500 /**
5501  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5502  * @pf: PF being queried
5503  *
5504  * Return a bitmap for enabled traffic classes for this PF.
5505  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5506 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5507 {
5508 	if (i40e_is_tc_mqprio_enabled(pf))
5509 		return i40e_mqprio_get_enabled_tc(pf);
5510 
5511 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5512 	 * default TC
5513 	 */
5514 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5515 		return I40E_DEFAULT_TRAFFIC_CLASS;
5516 
5517 	/* SFP mode we want PF to be enabled for all TCs */
5518 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5519 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5520 
5521 	/* MFP enabled and iSCSI PF type */
5522 	if (pf->hw.func_caps.iscsi)
5523 		return i40e_get_iscsi_tc_map(pf);
5524 	else
5525 		return I40E_DEFAULT_TRAFFIC_CLASS;
5526 }
5527 
5528 /**
5529  * i40e_vsi_get_bw_info - Query VSI BW Information
5530  * @vsi: the VSI being queried
5531  *
5532  * Returns 0 on success, negative value on failure
5533  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5534 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5535 {
5536 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5537 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5538 	struct i40e_pf *pf = vsi->back;
5539 	struct i40e_hw *hw = &pf->hw;
5540 	u32 tc_bw_max;
5541 	int ret;
5542 	int i;
5543 
5544 	/* Get the VSI level BW configuration */
5545 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5546 	if (ret) {
5547 		dev_info(&pf->pdev->dev,
5548 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5549 			 ERR_PTR(ret),
5550 			 libie_aq_str(pf->hw.aq.asq_last_status));
5551 		return -EINVAL;
5552 	}
5553 
5554 	/* Get the VSI level BW configuration per TC */
5555 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5556 					       NULL);
5557 	if (ret) {
5558 		dev_info(&pf->pdev->dev,
5559 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5560 			 ERR_PTR(ret),
5561 			 libie_aq_str(pf->hw.aq.asq_last_status));
5562 		return -EINVAL;
5563 	}
5564 
5565 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5566 		dev_info(&pf->pdev->dev,
5567 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5568 			 bw_config.tc_valid_bits,
5569 			 bw_ets_config.tc_valid_bits);
5570 		/* Still continuing */
5571 	}
5572 
5573 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5574 	vsi->bw_max_quanta = bw_config.max_bw;
5575 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5576 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5577 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5578 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5579 		vsi->bw_ets_limit_credits[i] =
5580 					le16_to_cpu(bw_ets_config.credits[i]);
5581 		/* 3 bits out of 4 for each TC */
5582 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5583 	}
5584 
5585 	return 0;
5586 }
5587 
5588 /**
5589  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5590  * @vsi: the VSI being configured
5591  * @enabled_tc: TC bitmap
5592  * @bw_share: BW shared credits per TC
5593  *
5594  * Returns 0 on success, negative value on failure
5595  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5596 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5597 				       u8 *bw_share)
5598 {
5599 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5600 	struct i40e_pf *pf = vsi->back;
5601 	int ret;
5602 	int i;
5603 
5604 	/* There is no need to reset BW when mqprio mode is on.  */
5605 	if (i40e_is_tc_mqprio_enabled(pf))
5606 		return 0;
5607 	if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
5608 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5609 		if (ret)
5610 			dev_info(&pf->pdev->dev,
5611 				 "Failed to reset tx rate for vsi->seid %u\n",
5612 				 vsi->seid);
5613 		return ret;
5614 	}
5615 	memset(&bw_data, 0, sizeof(bw_data));
5616 	bw_data.tc_valid_bits = enabled_tc;
5617 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5618 		bw_data.tc_bw_credits[i] = bw_share[i];
5619 
5620 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5621 	if (ret) {
5622 		dev_info(&pf->pdev->dev,
5623 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5624 			 pf->hw.aq.asq_last_status);
5625 		return -EINVAL;
5626 	}
5627 
5628 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5629 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5630 
5631 	return 0;
5632 }
5633 
5634 /**
5635  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5636  * @vsi: the VSI being configured
5637  * @enabled_tc: TC map to be enabled
5638  *
5639  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5640 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5641 {
5642 	struct net_device *netdev = vsi->netdev;
5643 	struct i40e_pf *pf = vsi->back;
5644 	struct i40e_hw *hw = &pf->hw;
5645 	u8 netdev_tc = 0;
5646 	int i;
5647 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5648 
5649 	if (!netdev)
5650 		return;
5651 
5652 	if (!enabled_tc) {
5653 		netdev_reset_tc(netdev);
5654 		return;
5655 	}
5656 
5657 	/* Set up actual enabled TCs on the VSI */
5658 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5659 		return;
5660 
5661 	/* set per TC queues for the VSI */
5662 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5663 		/* Only set TC queues for enabled tcs
5664 		 *
5665 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5666 		 * enabled_tc bitmap would be 0x00001001; the driver
5667 		 * will set the numtc for netdev as 2 that will be
5668 		 * referenced by the netdev layer as TC 0 and 1.
5669 		 */
5670 		if (vsi->tc_config.enabled_tc & BIT(i))
5671 			netdev_set_tc_queue(netdev,
5672 					vsi->tc_config.tc_info[i].netdev_tc,
5673 					vsi->tc_config.tc_info[i].qcount,
5674 					vsi->tc_config.tc_info[i].qoffset);
5675 	}
5676 
5677 	if (i40e_is_tc_mqprio_enabled(pf))
5678 		return;
5679 
5680 	/* Assign UP2TC map for the VSI */
5681 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5682 		/* Get the actual TC# for the UP */
5683 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5684 		/* Get the mapped netdev TC# for the UP */
5685 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5686 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5687 	}
5688 }
5689 
5690 /**
5691  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5692  * @vsi: the VSI being configured
5693  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5694  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5695 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5696 				      struct i40e_vsi_context *ctxt)
5697 {
5698 	/* copy just the sections touched not the entire info
5699 	 * since not all sections are valid as returned by
5700 	 * update vsi params
5701 	 */
5702 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5703 	memcpy(&vsi->info.queue_mapping,
5704 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5705 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5706 	       sizeof(vsi->info.tc_mapping));
5707 }
5708 
5709 /**
5710  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5711  * @vsi: the VSI being reconfigured
5712  * @vsi_offset: offset from main VF VSI
5713  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5714 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5715 {
5716 	struct i40e_vsi_context ctxt = {};
5717 	struct i40e_pf *pf;
5718 	struct i40e_hw *hw;
5719 	int ret;
5720 
5721 	if (!vsi)
5722 		return -EINVAL;
5723 	pf = vsi->back;
5724 	hw = &pf->hw;
5725 
5726 	ctxt.seid = vsi->seid;
5727 	ctxt.pf_num = hw->pf_id;
5728 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5729 	ctxt.uplink_seid = vsi->uplink_seid;
5730 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5731 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5732 	ctxt.info = vsi->info;
5733 
5734 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5735 				 false);
5736 	if (vsi->reconfig_rss) {
5737 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5738 				      vsi->num_queue_pairs);
5739 		ret = i40e_vsi_config_rss(vsi);
5740 		if (ret) {
5741 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5742 			return ret;
5743 		}
5744 		vsi->reconfig_rss = false;
5745 	}
5746 
5747 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5748 	if (ret) {
5749 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5750 			 ERR_PTR(ret),
5751 			 libie_aq_str(hw->aq.asq_last_status));
5752 		return ret;
5753 	}
5754 	/* update the local VSI info with updated queue map */
5755 	i40e_vsi_update_queue_map(vsi, &ctxt);
5756 	vsi->info.valid_sections = 0;
5757 
5758 	return ret;
5759 }
5760 
5761 /**
5762  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5763  * @vsi: VSI to be configured
5764  * @enabled_tc: TC bitmap
5765  *
5766  * This configures a particular VSI for TCs that are mapped to the
5767  * given TC bitmap. It uses default bandwidth share for TCs across
5768  * VSIs to configure TC for a particular VSI.
5769  *
5770  * NOTE:
5771  * It is expected that the VSI queues have been quisced before calling
5772  * this function.
5773  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5774 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5775 {
5776 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5777 	struct i40e_pf *pf = vsi->back;
5778 	struct i40e_hw *hw = &pf->hw;
5779 	struct i40e_vsi_context ctxt;
5780 	int ret = 0;
5781 	int i;
5782 
5783 	/* Check if enabled_tc is same as existing or new TCs */
5784 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5785 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5786 		return ret;
5787 
5788 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5789 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5790 		if (enabled_tc & BIT(i))
5791 			bw_share[i] = 1;
5792 	}
5793 
5794 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5795 	if (ret) {
5796 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5797 
5798 		dev_info(&pf->pdev->dev,
5799 			 "Failed configuring TC map %d for VSI %d\n",
5800 			 enabled_tc, vsi->seid);
5801 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5802 						  &bw_config, NULL);
5803 		if (ret) {
5804 			dev_info(&pf->pdev->dev,
5805 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5806 				 ERR_PTR(ret),
5807 				 libie_aq_str(hw->aq.asq_last_status));
5808 			goto out;
5809 		}
5810 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5811 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5812 
5813 			if (!valid_tc)
5814 				valid_tc = bw_config.tc_valid_bits;
5815 			/* Always enable TC0, no matter what */
5816 			valid_tc |= 1;
5817 			dev_info(&pf->pdev->dev,
5818 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5819 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5820 			enabled_tc = valid_tc;
5821 		}
5822 
5823 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5824 		if (ret) {
5825 			dev_err(&pf->pdev->dev,
5826 				"Unable to  configure TC map %d for VSI %d\n",
5827 				enabled_tc, vsi->seid);
5828 			goto out;
5829 		}
5830 	}
5831 
5832 	/* Update Queue Pairs Mapping for currently enabled UPs */
5833 	ctxt.seid = vsi->seid;
5834 	ctxt.pf_num = vsi->back->hw.pf_id;
5835 	ctxt.vf_num = 0;
5836 	ctxt.uplink_seid = vsi->uplink_seid;
5837 	ctxt.info = vsi->info;
5838 	if (i40e_is_tc_mqprio_enabled(pf)) {
5839 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5840 		if (ret)
5841 			goto out;
5842 	} else {
5843 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5844 	}
5845 
5846 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5847 	 * queues changed.
5848 	 */
5849 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5850 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5851 				      vsi->num_queue_pairs);
5852 		ret = i40e_vsi_config_rss(vsi);
5853 		if (ret) {
5854 			dev_info(&vsi->back->pdev->dev,
5855 				 "Failed to reconfig rss for num_queues\n");
5856 			return ret;
5857 		}
5858 		vsi->reconfig_rss = false;
5859 	}
5860 	if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
5861 		ctxt.info.valid_sections |=
5862 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5863 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5864 	}
5865 
5866 	/* Update the VSI after updating the VSI queue-mapping
5867 	 * information
5868 	 */
5869 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5870 	if (ret) {
5871 		dev_info(&pf->pdev->dev,
5872 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5873 			 ERR_PTR(ret),
5874 			 libie_aq_str(hw->aq.asq_last_status));
5875 		goto out;
5876 	}
5877 	/* update the local VSI info with updated queue map */
5878 	i40e_vsi_update_queue_map(vsi, &ctxt);
5879 	vsi->info.valid_sections = 0;
5880 
5881 	/* Update current VSI BW information */
5882 	ret = i40e_vsi_get_bw_info(vsi);
5883 	if (ret) {
5884 		dev_info(&pf->pdev->dev,
5885 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5886 			 ERR_PTR(ret),
5887 			 libie_aq_str(hw->aq.asq_last_status));
5888 		goto out;
5889 	}
5890 
5891 	/* Update the netdev TC setup */
5892 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5893 out:
5894 	return ret;
5895 }
5896 
5897 /**
5898  * i40e_vsi_reconfig_tc - Reconfigure VSI Tx Scheduler for stored TC map
5899  * @vsi: VSI to be reconfigured
5900  *
5901  * This reconfigures a particular VSI for TCs that are mapped to the
5902  * TC bitmap stored previously for the VSI.
5903  *
5904  * Context: It is expected that the VSI queues have been quisced before
5905  *          calling this function.
5906  *
5907  * Return: 0 on success, negative value on failure
5908  **/
i40e_vsi_reconfig_tc(struct i40e_vsi * vsi)5909 static int i40e_vsi_reconfig_tc(struct i40e_vsi *vsi)
5910 {
5911 	u8 enabled_tc;
5912 
5913 	enabled_tc = vsi->tc_config.enabled_tc;
5914 	vsi->tc_config.enabled_tc = 0;
5915 
5916 	return i40e_vsi_config_tc(vsi, enabled_tc);
5917 }
5918 
5919 /**
5920  * i40e_get_link_speed - Returns link speed for the interface
5921  * @vsi: VSI to be configured
5922  *
5923  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5924 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5925 {
5926 	struct i40e_pf *pf = vsi->back;
5927 
5928 	switch (pf->hw.phy.link_info.link_speed) {
5929 	case I40E_LINK_SPEED_40GB:
5930 		return 40000;
5931 	case I40E_LINK_SPEED_25GB:
5932 		return 25000;
5933 	case I40E_LINK_SPEED_20GB:
5934 		return 20000;
5935 	case I40E_LINK_SPEED_10GB:
5936 		return 10000;
5937 	case I40E_LINK_SPEED_1GB:
5938 		return 1000;
5939 	default:
5940 		return -EINVAL;
5941 	}
5942 }
5943 
5944 /**
5945  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5946  * @vsi: Pointer to vsi structure
5947  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5948  *
5949  * Helper function to convert units before send to set BW limit
5950  **/
i40e_bw_bytes_to_mbits(struct i40e_vsi * vsi,u64 max_tx_rate)5951 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5952 {
5953 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5954 		dev_warn(&vsi->back->pdev->dev,
5955 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5956 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5957 	} else {
5958 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5959 	}
5960 
5961 	return max_tx_rate;
5962 }
5963 
5964 /**
5965  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5966  * @vsi: VSI to be configured
5967  * @seid: seid of the channel/VSI
5968  * @max_tx_rate: max TX rate to be configured as BW limit
5969  *
5970  * Helper function to set BW limit for a given VSI
5971  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5972 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5973 {
5974 	struct i40e_pf *pf = vsi->back;
5975 	u64 credits = 0;
5976 	int speed = 0;
5977 	int ret = 0;
5978 
5979 	speed = i40e_get_link_speed(vsi);
5980 	if (max_tx_rate > speed) {
5981 		dev_err(&pf->pdev->dev,
5982 			"Invalid max tx rate %llu specified for VSI seid %d.",
5983 			max_tx_rate, seid);
5984 		return -EINVAL;
5985 	}
5986 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5987 		dev_warn(&pf->pdev->dev,
5988 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5989 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5990 	}
5991 
5992 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5993 	credits = max_tx_rate;
5994 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5995 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5996 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5997 	if (ret)
5998 		dev_err(&pf->pdev->dev,
5999 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
6000 			max_tx_rate, seid, ERR_PTR(ret),
6001 			libie_aq_str(pf->hw.aq.asq_last_status));
6002 	return ret;
6003 }
6004 
6005 /**
6006  * i40e_remove_queue_channels - Remove queue channels for the TCs
6007  * @vsi: VSI to be configured
6008  *
6009  * Remove queue channels for the TCs
6010  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)6011 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
6012 {
6013 	struct i40e_cloud_filter *cfilter;
6014 	enum libie_aq_err last_aq_status;
6015 	struct i40e_channel *ch, *ch_tmp;
6016 	struct i40e_pf *pf = vsi->back;
6017 	struct hlist_node *node;
6018 	int ret, i;
6019 
6020 	/* Reset rss size that was stored when reconfiguring rss for
6021 	 * channel VSIs with non-power-of-2 queue count.
6022 	 */
6023 	vsi->current_rss_size = 0;
6024 
6025 	/* perform cleanup for channels if they exist */
6026 	if (list_empty(&vsi->ch_list))
6027 		return;
6028 
6029 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6030 		struct i40e_vsi *p_vsi;
6031 
6032 		list_del(&ch->list);
6033 		p_vsi = ch->parent_vsi;
6034 		if (!p_vsi || !ch->initialized) {
6035 			kfree(ch);
6036 			continue;
6037 		}
6038 		/* Reset queue contexts */
6039 		for (i = 0; i < ch->num_queue_pairs; i++) {
6040 			struct i40e_ring *tx_ring, *rx_ring;
6041 			u16 pf_q;
6042 
6043 			pf_q = ch->base_queue + i;
6044 			tx_ring = vsi->tx_rings[pf_q];
6045 			tx_ring->ch = NULL;
6046 
6047 			rx_ring = vsi->rx_rings[pf_q];
6048 			rx_ring->ch = NULL;
6049 		}
6050 
6051 		/* Reset BW configured for this VSI via mqprio */
6052 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6053 		if (ret)
6054 			dev_info(&vsi->back->pdev->dev,
6055 				 "Failed to reset tx rate for ch->seid %u\n",
6056 				 ch->seid);
6057 
6058 		/* delete cloud filters associated with this channel */
6059 		hlist_for_each_entry_safe(cfilter, node,
6060 					  &pf->cloud_filter_list, cloud_node) {
6061 			if (cfilter->seid != ch->seid)
6062 				continue;
6063 
6064 			hash_del(&cfilter->cloud_node);
6065 			if (cfilter->dst_port)
6066 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6067 									cfilter,
6068 									false);
6069 			else
6070 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6071 								false);
6072 			last_aq_status = pf->hw.aq.asq_last_status;
6073 			if (ret)
6074 				dev_info(&pf->pdev->dev,
6075 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6076 					 ERR_PTR(ret),
6077 					 libie_aq_str(last_aq_status));
6078 			kfree(cfilter);
6079 		}
6080 
6081 		/* delete VSI from FW */
6082 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6083 					     NULL);
6084 		if (ret)
6085 			dev_err(&vsi->back->pdev->dev,
6086 				"unable to remove channel (%d) for parent VSI(%d)\n",
6087 				ch->seid, p_vsi->seid);
6088 		kfree(ch);
6089 	}
6090 	INIT_LIST_HEAD(&vsi->ch_list);
6091 }
6092 
6093 /**
6094  * i40e_get_max_queues_for_channel
6095  * @vsi: ptr to VSI to which channels are associated with
6096  *
6097  * Helper function which returns max value among the queue counts set on the
6098  * channels/TCs created.
6099  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)6100 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6101 {
6102 	struct i40e_channel *ch, *ch_tmp;
6103 	int max = 0;
6104 
6105 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6106 		if (!ch->initialized)
6107 			continue;
6108 		if (ch->num_queue_pairs > max)
6109 			max = ch->num_queue_pairs;
6110 	}
6111 
6112 	return max;
6113 }
6114 
6115 /**
6116  * i40e_validate_num_queues - validate num_queues w.r.t channel
6117  * @pf: ptr to PF device
6118  * @num_queues: number of queues
6119  * @vsi: the parent VSI
6120  * @reconfig_rss: indicates should the RSS be reconfigured or not
6121  *
6122  * This function validates number of queues in the context of new channel
6123  * which is being established and determines if RSS should be reconfigured
6124  * or not for parent VSI.
6125  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)6126 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6127 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6128 {
6129 	int max_ch_queues;
6130 
6131 	if (!reconfig_rss)
6132 		return -EINVAL;
6133 
6134 	*reconfig_rss = false;
6135 	if (vsi->current_rss_size) {
6136 		if (num_queues > vsi->current_rss_size) {
6137 			dev_dbg(&pf->pdev->dev,
6138 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6139 				num_queues, vsi->current_rss_size);
6140 			return -EINVAL;
6141 		} else if ((num_queues < vsi->current_rss_size) &&
6142 			   (!is_power_of_2(num_queues))) {
6143 			dev_dbg(&pf->pdev->dev,
6144 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6145 				num_queues, vsi->current_rss_size);
6146 			return -EINVAL;
6147 		}
6148 	}
6149 
6150 	if (!is_power_of_2(num_queues)) {
6151 		/* Find the max num_queues configured for channel if channel
6152 		 * exist.
6153 		 * if channel exist, then enforce 'num_queues' to be more than
6154 		 * max ever queues configured for channel.
6155 		 */
6156 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6157 		if (num_queues < max_ch_queues) {
6158 			dev_dbg(&pf->pdev->dev,
6159 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6160 				num_queues, max_ch_queues);
6161 			return -EINVAL;
6162 		}
6163 		*reconfig_rss = true;
6164 	}
6165 
6166 	return 0;
6167 }
6168 
6169 /**
6170  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6171  * @vsi: the VSI being setup
6172  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6173  *
6174  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6175  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)6176 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6177 {
6178 	struct i40e_pf *pf = vsi->back;
6179 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6180 	struct i40e_hw *hw = &pf->hw;
6181 	int local_rss_size;
6182 	u8 *lut;
6183 	int ret;
6184 
6185 	if (!vsi->rss_size)
6186 		return -EINVAL;
6187 
6188 	if (rss_size > vsi->rss_size)
6189 		return -EINVAL;
6190 
6191 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6192 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6193 	if (!lut)
6194 		return -ENOMEM;
6195 
6196 	/* Ignoring user configured lut if there is one */
6197 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6198 
6199 	/* Use user configured hash key if there is one, otherwise
6200 	 * use default.
6201 	 */
6202 	if (vsi->rss_hkey_user)
6203 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6204 	else
6205 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6206 
6207 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6208 	if (ret) {
6209 		dev_info(&pf->pdev->dev,
6210 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6211 			 ERR_PTR(ret),
6212 			 libie_aq_str(hw->aq.asq_last_status));
6213 		kfree(lut);
6214 		return ret;
6215 	}
6216 	kfree(lut);
6217 
6218 	/* Do the update w.r.t. storing rss_size */
6219 	if (!vsi->orig_rss_size)
6220 		vsi->orig_rss_size = vsi->rss_size;
6221 	vsi->current_rss_size = local_rss_size;
6222 
6223 	return ret;
6224 }
6225 
6226 /**
6227  * i40e_channel_setup_queue_map - Setup a channel queue map
6228  * @pf: ptr to PF device
6229  * @ctxt: VSI context structure
6230  * @ch: ptr to channel structure
6231  *
6232  * Setup queue map for a specific channel
6233  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)6234 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6235 					 struct i40e_vsi_context *ctxt,
6236 					 struct i40e_channel *ch)
6237 {
6238 	u16 qcount, qmap, sections = 0;
6239 	u8 offset = 0;
6240 	int pow;
6241 
6242 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6243 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6244 
6245 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6246 	ch->num_queue_pairs = qcount;
6247 
6248 	/* find the next higher power-of-2 of num queue pairs */
6249 	pow = ilog2(qcount);
6250 	if (!is_power_of_2(qcount))
6251 		pow++;
6252 
6253 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6254 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6255 
6256 	/* Setup queue TC[0].qmap for given VSI context */
6257 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6258 
6259 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6260 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6261 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6262 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6263 }
6264 
6265 /**
6266  * i40e_add_channel - add a channel by adding VSI
6267  * @pf: ptr to PF device
6268  * @uplink_seid: underlying HW switching element (VEB) ID
6269  * @ch: ptr to channel structure
6270  *
6271  * Add a channel (VSI) using add_vsi and queue_map
6272  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)6273 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6274 			    struct i40e_channel *ch)
6275 {
6276 	struct i40e_hw *hw = &pf->hw;
6277 	struct i40e_vsi_context ctxt;
6278 	u8 enabled_tc = 0x1; /* TC0 enabled */
6279 	int ret;
6280 
6281 	if (ch->type != I40E_VSI_VMDQ2) {
6282 		dev_info(&pf->pdev->dev,
6283 			 "add new vsi failed, ch->type %d\n", ch->type);
6284 		return -EINVAL;
6285 	}
6286 
6287 	memset(&ctxt, 0, sizeof(ctxt));
6288 	ctxt.pf_num = hw->pf_id;
6289 	ctxt.vf_num = 0;
6290 	ctxt.uplink_seid = uplink_seid;
6291 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6292 	if (ch->type == I40E_VSI_VMDQ2)
6293 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6294 
6295 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6296 		ctxt.info.valid_sections |=
6297 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6298 		ctxt.info.switch_id =
6299 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6300 	}
6301 
6302 	/* Set queue map for a given VSI context */
6303 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6304 
6305 	/* Now time to create VSI */
6306 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6307 	if (ret) {
6308 		dev_info(&pf->pdev->dev,
6309 			 "add new vsi failed, err %pe aq_err %s\n",
6310 			 ERR_PTR(ret),
6311 			 libie_aq_str(pf->hw.aq.asq_last_status));
6312 		return -ENOENT;
6313 	}
6314 
6315 	/* Success, update channel, set enabled_tc only if the channel
6316 	 * is not a macvlan
6317 	 */
6318 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6319 	ch->seid = ctxt.seid;
6320 	ch->vsi_number = ctxt.vsi_number;
6321 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6322 
6323 	/* copy just the sections touched not the entire info
6324 	 * since not all sections are valid as returned by
6325 	 * update vsi params
6326 	 */
6327 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6328 	memcpy(&ch->info.queue_mapping,
6329 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6330 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6331 	       sizeof(ctxt.info.tc_mapping));
6332 
6333 	return 0;
6334 }
6335 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6336 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6337 				  u8 *bw_share)
6338 {
6339 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6340 	int ret;
6341 	int i;
6342 
6343 	memset(&bw_data, 0, sizeof(bw_data));
6344 	bw_data.tc_valid_bits = ch->enabled_tc;
6345 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6346 		bw_data.tc_bw_credits[i] = bw_share[i];
6347 
6348 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6349 				       &bw_data, NULL);
6350 	if (ret) {
6351 		dev_info(&vsi->back->pdev->dev,
6352 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6353 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6354 		return -EINVAL;
6355 	}
6356 
6357 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6358 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6359 
6360 	return 0;
6361 }
6362 
6363 /**
6364  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6365  * @pf: ptr to PF device
6366  * @vsi: the VSI being setup
6367  * @ch: ptr to channel structure
6368  *
6369  * Configure TX rings associated with channel (VSI) since queues are being
6370  * from parent VSI.
6371  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6372 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6373 				       struct i40e_vsi *vsi,
6374 				       struct i40e_channel *ch)
6375 {
6376 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6377 	int ret;
6378 	int i;
6379 
6380 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6381 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6382 		if (ch->enabled_tc & BIT(i))
6383 			bw_share[i] = 1;
6384 	}
6385 
6386 	/* configure BW for new VSI */
6387 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6388 	if (ret) {
6389 		dev_info(&vsi->back->pdev->dev,
6390 			 "Failed configuring TC map %d for channel (seid %u)\n",
6391 			 ch->enabled_tc, ch->seid);
6392 		return ret;
6393 	}
6394 
6395 	for (i = 0; i < ch->num_queue_pairs; i++) {
6396 		struct i40e_ring *tx_ring, *rx_ring;
6397 		u16 pf_q;
6398 
6399 		pf_q = ch->base_queue + i;
6400 
6401 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6402 		 * context
6403 		 */
6404 		tx_ring = vsi->tx_rings[pf_q];
6405 		tx_ring->ch = ch;
6406 
6407 		/* Get the RX ring ptr */
6408 		rx_ring = vsi->rx_rings[pf_q];
6409 		rx_ring->ch = ch;
6410 	}
6411 
6412 	return 0;
6413 }
6414 
6415 /**
6416  * i40e_setup_hw_channel - setup new channel
6417  * @pf: ptr to PF device
6418  * @vsi: the VSI being setup
6419  * @ch: ptr to channel structure
6420  * @uplink_seid: underlying HW switching element (VEB) ID
6421  * @type: type of channel to be created (VMDq2/VF)
6422  *
6423  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6424  * and configures TX rings accordingly
6425  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6426 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6427 					struct i40e_vsi *vsi,
6428 					struct i40e_channel *ch,
6429 					u16 uplink_seid, u8 type)
6430 {
6431 	int ret;
6432 
6433 	ch->initialized = false;
6434 	ch->base_queue = vsi->next_base_queue;
6435 	ch->type = type;
6436 
6437 	/* Proceed with creation of channel (VMDq2) VSI */
6438 	ret = i40e_add_channel(pf, uplink_seid, ch);
6439 	if (ret) {
6440 		dev_info(&pf->pdev->dev,
6441 			 "failed to add_channel using uplink_seid %u\n",
6442 			 uplink_seid);
6443 		return ret;
6444 	}
6445 
6446 	/* Mark the successful creation of channel */
6447 	ch->initialized = true;
6448 
6449 	/* Reconfigure TX queues using QTX_CTL register */
6450 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6451 	if (ret) {
6452 		dev_info(&pf->pdev->dev,
6453 			 "failed to configure TX rings for channel %u\n",
6454 			 ch->seid);
6455 		return ret;
6456 	}
6457 
6458 	/* update 'next_base_queue' */
6459 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6460 	dev_dbg(&pf->pdev->dev,
6461 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6462 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6463 		ch->num_queue_pairs,
6464 		vsi->next_base_queue);
6465 	return ret;
6466 }
6467 
6468 /**
6469  * i40e_setup_channel - setup new channel using uplink element
6470  * @pf: ptr to PF device
6471  * @vsi: pointer to the VSI to set up the channel within
6472  * @ch: ptr to channel structure
6473  *
6474  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6475  * and uplink switching element (uplink_seid)
6476  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6477 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6478 			       struct i40e_channel *ch)
6479 {
6480 	struct i40e_vsi *main_vsi;
6481 	u8 vsi_type;
6482 	u16 seid;
6483 	int ret;
6484 
6485 	if (vsi->type == I40E_VSI_MAIN) {
6486 		vsi_type = I40E_VSI_VMDQ2;
6487 	} else {
6488 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6489 			vsi->type);
6490 		return false;
6491 	}
6492 
6493 	/* underlying switching element */
6494 	main_vsi = i40e_pf_get_main_vsi(pf);
6495 	seid = main_vsi->uplink_seid;
6496 
6497 	/* create channel (VSI), configure TX rings */
6498 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6499 	if (ret) {
6500 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6501 		return false;
6502 	}
6503 
6504 	return ch->initialized ? true : false;
6505 }
6506 
6507 /**
6508  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6509  * @vsi: ptr to VSI which has PF backing
6510  *
6511  * Sets up switch mode correctly if it needs to be changed and perform
6512  * what are allowed modes.
6513  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6514 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6515 {
6516 	u8 mode;
6517 	struct i40e_pf *pf = vsi->back;
6518 	struct i40e_hw *hw = &pf->hw;
6519 	int ret;
6520 
6521 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6522 	if (ret)
6523 		return -EINVAL;
6524 
6525 	if (hw->dev_caps.switch_mode) {
6526 		/* if switch mode is set, support mode2 (non-tunneled for
6527 		 * cloud filter) for now
6528 		 */
6529 		u32 switch_mode = hw->dev_caps.switch_mode &
6530 				  I40E_SWITCH_MODE_MASK;
6531 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6532 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6533 				return 0;
6534 			dev_err(&pf->pdev->dev,
6535 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6536 				hw->dev_caps.switch_mode);
6537 			return -EINVAL;
6538 		}
6539 	}
6540 
6541 	/* Set Bit 7 to be valid */
6542 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6543 
6544 	/* Set L4type for TCP support */
6545 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6546 
6547 	/* Set cloud filter mode */
6548 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6549 
6550 	/* Prep mode field for set_switch_config */
6551 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6552 					pf->last_sw_conf_valid_flags,
6553 					mode, NULL);
6554 	if (ret && hw->aq.asq_last_status != LIBIE_AQ_RC_ESRCH)
6555 		dev_err(&pf->pdev->dev,
6556 			"couldn't set switch config bits, err %pe aq_err %s\n",
6557 			ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
6558 
6559 	return ret;
6560 }
6561 
6562 /**
6563  * i40e_create_queue_channel - function to create channel
6564  * @vsi: VSI to be configured
6565  * @ch: ptr to channel (it contains channel specific params)
6566  *
6567  * This function creates channel (VSI) using num_queues specified by user,
6568  * reconfigs RSS if needed.
6569  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6570 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6571 			      struct i40e_channel *ch)
6572 {
6573 	struct i40e_pf *pf = vsi->back;
6574 	bool reconfig_rss;
6575 	int err;
6576 
6577 	if (!ch)
6578 		return -EINVAL;
6579 
6580 	if (!ch->num_queue_pairs) {
6581 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6582 			ch->num_queue_pairs);
6583 		return -EINVAL;
6584 	}
6585 
6586 	/* validate user requested num_queues for channel */
6587 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6588 				       &reconfig_rss);
6589 	if (err) {
6590 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6591 			 ch->num_queue_pairs);
6592 		return -EINVAL;
6593 	}
6594 
6595 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6596 	 * VSI to be added switch to VEB mode.
6597 	 */
6598 
6599 	if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6600 		set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
6601 
6602 		if (vsi->type == I40E_VSI_MAIN) {
6603 			if (i40e_is_tc_mqprio_enabled(pf))
6604 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6605 			else
6606 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6607 		}
6608 		/* now onwards for main VSI, number of queues will be value
6609 		 * of TC0's queue count
6610 		 */
6611 	}
6612 
6613 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6614 	 * it should be more than num_queues
6615 	 */
6616 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6617 		dev_dbg(&pf->pdev->dev,
6618 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6619 			vsi->cnt_q_avail, ch->num_queue_pairs);
6620 		return -EINVAL;
6621 	}
6622 
6623 	/* reconfig_rss only if vsi type is MAIN_VSI */
6624 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6625 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6626 		if (err) {
6627 			dev_info(&pf->pdev->dev,
6628 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6629 				 ch->num_queue_pairs);
6630 			return -EINVAL;
6631 		}
6632 	}
6633 
6634 	if (!i40e_setup_channel(pf, vsi, ch)) {
6635 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6636 		return -EINVAL;
6637 	}
6638 
6639 	dev_info(&pf->pdev->dev,
6640 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6641 		 ch->seid, ch->num_queue_pairs);
6642 
6643 	/* configure VSI for BW limit */
6644 	if (ch->max_tx_rate) {
6645 		u64 credits = ch->max_tx_rate;
6646 
6647 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6648 			return -EINVAL;
6649 
6650 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6651 		dev_dbg(&pf->pdev->dev,
6652 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6653 			ch->max_tx_rate,
6654 			credits,
6655 			ch->seid);
6656 	}
6657 
6658 	/* in case of VF, this will be main SRIOV VSI */
6659 	ch->parent_vsi = vsi;
6660 
6661 	/* and update main_vsi's count for queue_available to use */
6662 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6663 
6664 	return 0;
6665 }
6666 
6667 /**
6668  * i40e_configure_queue_channels - Add queue channel for the given TCs
6669  * @vsi: VSI to be configured
6670  *
6671  * Configures queue channel mapping to the given TCs
6672  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6673 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6674 {
6675 	struct i40e_channel *ch;
6676 	u64 max_rate = 0;
6677 	int ret = 0, i;
6678 
6679 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6680 	vsi->tc_seid_map[0] = vsi->seid;
6681 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6682 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6683 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6684 			if (!ch) {
6685 				ret = -ENOMEM;
6686 				goto err_free;
6687 			}
6688 
6689 			INIT_LIST_HEAD(&ch->list);
6690 			ch->num_queue_pairs =
6691 				vsi->tc_config.tc_info[i].qcount;
6692 			ch->base_queue =
6693 				vsi->tc_config.tc_info[i].qoffset;
6694 
6695 			/* Bandwidth limit through tc interface is in bytes/s,
6696 			 * change to Mbit/s
6697 			 */
6698 			max_rate = vsi->mqprio_qopt.max_rate[i];
6699 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6700 			ch->max_tx_rate = max_rate;
6701 
6702 			list_add_tail(&ch->list, &vsi->ch_list);
6703 
6704 			ret = i40e_create_queue_channel(vsi, ch);
6705 			if (ret) {
6706 				dev_err(&vsi->back->pdev->dev,
6707 					"Failed creating queue channel with TC%d: queues %d\n",
6708 					i, ch->num_queue_pairs);
6709 				goto err_free;
6710 			}
6711 			vsi->tc_seid_map[i] = ch->seid;
6712 		}
6713 	}
6714 
6715 	/* reset to reconfigure TX queue contexts */
6716 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6717 	return ret;
6718 
6719 err_free:
6720 	i40e_remove_queue_channels(vsi);
6721 	return ret;
6722 }
6723 
6724 /**
6725  * i40e_veb_config_tc - Configure TCs for given VEB
6726  * @veb: given VEB
6727  * @enabled_tc: TC bitmap
6728  *
6729  * Configures given TC bitmap for VEB (switching) element
6730  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6731 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6732 {
6733 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6734 	struct i40e_pf *pf = veb->pf;
6735 	int ret = 0;
6736 	int i;
6737 
6738 	/* No TCs or already enabled TCs just return */
6739 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6740 		return ret;
6741 
6742 	bw_data.tc_valid_bits = enabled_tc;
6743 	/* bw_data.absolute_credits is not set (relative) */
6744 
6745 	/* Enable ETS TCs with equal BW Share for now */
6746 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6747 		if (enabled_tc & BIT(i))
6748 			bw_data.tc_bw_share_credits[i] = 1;
6749 	}
6750 
6751 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6752 						   &bw_data, NULL);
6753 	if (ret) {
6754 		dev_info(&pf->pdev->dev,
6755 			 "VEB bw config failed, err %pe aq_err %s\n",
6756 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6757 		goto out;
6758 	}
6759 
6760 	/* Update the BW information */
6761 	ret = i40e_veb_get_bw_info(veb);
6762 	if (ret) {
6763 		dev_info(&pf->pdev->dev,
6764 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6765 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6766 	}
6767 
6768 out:
6769 	return ret;
6770 }
6771 
6772 #ifdef CONFIG_I40E_DCB
6773 /**
6774  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6775  * @pf: PF struct
6776  *
6777  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6778  * the caller would've quiesce all the VSIs before calling
6779  * this function
6780  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6781 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6782 {
6783 	struct i40e_vsi *vsi;
6784 	struct i40e_veb *veb;
6785 	u8 tc_map = 0;
6786 	int ret;
6787 	int v;
6788 
6789 	/* Enable the TCs available on PF to all VEBs */
6790 	tc_map = i40e_pf_get_tc_map(pf);
6791 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6792 		return;
6793 
6794 	i40e_pf_for_each_veb(pf, v, veb) {
6795 		ret = i40e_veb_config_tc(veb, tc_map);
6796 		if (ret) {
6797 			dev_info(&pf->pdev->dev,
6798 				 "Failed configuring TC for VEB seid=%d\n",
6799 				 veb->seid);
6800 			/* Will try to configure as many components */
6801 		}
6802 	}
6803 
6804 	/* Update each VSI */
6805 	i40e_pf_for_each_vsi(pf, v, vsi) {
6806 		/* - Enable all TCs for the LAN VSI
6807 		 * - For all others keep them at TC0 for now
6808 		 */
6809 		if (vsi->type == I40E_VSI_MAIN)
6810 			tc_map = i40e_pf_get_tc_map(pf);
6811 		else
6812 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6813 
6814 		ret = i40e_vsi_config_tc(vsi, tc_map);
6815 		if (ret) {
6816 			dev_info(&pf->pdev->dev,
6817 				 "Failed configuring TC for VSI seid=%d\n",
6818 				 vsi->seid);
6819 			/* Will try to configure as many components */
6820 		} else {
6821 			/* Re-configure VSI vectors based on updated TC map */
6822 			i40e_vsi_map_rings_to_vectors(vsi);
6823 			if (vsi->netdev)
6824 				i40e_dcbnl_set_all(vsi);
6825 		}
6826 	}
6827 }
6828 
6829 /**
6830  * i40e_resume_port_tx - Resume port Tx
6831  * @pf: PF struct
6832  *
6833  * Resume a port's Tx and issue a PF reset in case of failure to
6834  * resume.
6835  **/
i40e_resume_port_tx(struct i40e_pf * pf)6836 static int i40e_resume_port_tx(struct i40e_pf *pf)
6837 {
6838 	struct i40e_hw *hw = &pf->hw;
6839 	int ret;
6840 
6841 	ret = i40e_aq_resume_port_tx(hw, NULL);
6842 	if (ret) {
6843 		dev_info(&pf->pdev->dev,
6844 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6845 			  ERR_PTR(ret),
6846 			  libie_aq_str(pf->hw.aq.asq_last_status));
6847 		/* Schedule PF reset to recover */
6848 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6849 		i40e_service_event_schedule(pf);
6850 	}
6851 
6852 	return ret;
6853 }
6854 
6855 /**
6856  * i40e_suspend_port_tx - Suspend port Tx
6857  * @pf: PF struct
6858  *
6859  * Suspend a port's Tx and issue a PF reset in case of failure.
6860  **/
i40e_suspend_port_tx(struct i40e_pf * pf)6861 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6862 {
6863 	struct i40e_hw *hw = &pf->hw;
6864 	int ret;
6865 
6866 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6867 	if (ret) {
6868 		dev_info(&pf->pdev->dev,
6869 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6870 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6871 		/* Schedule PF reset to recover */
6872 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6873 		i40e_service_event_schedule(pf);
6874 	}
6875 
6876 	return ret;
6877 }
6878 
6879 /**
6880  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6881  * @pf: PF being configured
6882  * @new_cfg: New DCBX configuration
6883  *
6884  * Program DCB settings into HW and reconfigure VEB/VSIs on
6885  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6886  **/
i40e_hw_set_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6887 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6888 				  struct i40e_dcbx_config *new_cfg)
6889 {
6890 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6891 	int ret;
6892 
6893 	/* Check if need reconfiguration */
6894 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6895 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6896 		return 0;
6897 	}
6898 
6899 	/* Config change disable all VSIs */
6900 	i40e_pf_quiesce_all_vsi(pf);
6901 
6902 	/* Copy the new config to the current config */
6903 	*old_cfg = *new_cfg;
6904 	old_cfg->etsrec = old_cfg->etscfg;
6905 	ret = i40e_set_dcb_config(&pf->hw);
6906 	if (ret) {
6907 		dev_info(&pf->pdev->dev,
6908 			 "Set DCB Config failed, err %pe aq_err %s\n",
6909 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6910 		goto out;
6911 	}
6912 
6913 	/* Changes in configuration update VEB/VSI */
6914 	i40e_dcb_reconfigure(pf);
6915 out:
6916 	/* In case of reset do not try to resume anything */
6917 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6918 		/* Re-start the VSIs if disabled */
6919 		ret = i40e_resume_port_tx(pf);
6920 		/* In case of error no point in resuming VSIs */
6921 		if (ret)
6922 			goto err;
6923 		i40e_pf_unquiesce_all_vsi(pf);
6924 	}
6925 err:
6926 	return ret;
6927 }
6928 
6929 /**
6930  * i40e_hw_dcb_config - Program new DCBX settings into HW
6931  * @pf: PF being configured
6932  * @new_cfg: New DCBX configuration
6933  *
6934  * Program DCB settings into HW and reconfigure VEB/VSIs on
6935  * given PF
6936  **/
i40e_hw_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6937 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6938 {
6939 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6940 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6941 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6942 	struct i40e_dcbx_config *old_cfg;
6943 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6944 	struct i40e_rx_pb_config pb_cfg;
6945 	struct i40e_hw *hw = &pf->hw;
6946 	u8 num_ports = hw->num_ports;
6947 	bool need_reconfig;
6948 	int ret = -EINVAL;
6949 	u8 lltc_map = 0;
6950 	u8 tc_map = 0;
6951 	u8 new_numtc;
6952 	u8 i;
6953 
6954 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6955 	/* Un-pack information to Program ETS HW via shared API
6956 	 * numtc, tcmap
6957 	 * LLTC map
6958 	 * ETS/NON-ETS arbiter mode
6959 	 * max exponent (credit refills)
6960 	 * Total number of ports
6961 	 * PFC priority bit-map
6962 	 * Priority Table
6963 	 * BW % per TC
6964 	 * Arbiter mode between UPs sharing same TC
6965 	 * TSA table (ETS or non-ETS)
6966 	 * EEE enabled or not
6967 	 * MFS TC table
6968 	 */
6969 
6970 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6971 
6972 	memset(&ets_data, 0, sizeof(ets_data));
6973 	for (i = 0; i < new_numtc; i++) {
6974 		tc_map |= BIT(i);
6975 		switch (new_cfg->etscfg.tsatable[i]) {
6976 		case I40E_IEEE_TSA_ETS:
6977 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6978 			ets_data.tc_bw_share_credits[i] =
6979 					new_cfg->etscfg.tcbwtable[i];
6980 			break;
6981 		case I40E_IEEE_TSA_STRICT:
6982 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6983 			lltc_map |= BIT(i);
6984 			ets_data.tc_bw_share_credits[i] =
6985 					I40E_DCB_STRICT_PRIO_CREDITS;
6986 			break;
6987 		default:
6988 			/* Invalid TSA type */
6989 			need_reconfig = false;
6990 			goto out;
6991 		}
6992 	}
6993 
6994 	old_cfg = &hw->local_dcbx_config;
6995 	/* Check if need reconfiguration */
6996 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6997 
6998 	/* If needed, enable/disable frame tagging, disable all VSIs
6999 	 * and suspend port tx
7000 	 */
7001 	if (need_reconfig) {
7002 		/* Enable DCB tagging only when more than one TC */
7003 		if (new_numtc > 1)
7004 			set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7005 		else
7006 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7007 
7008 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7009 		/* Reconfiguration needed quiesce all VSIs */
7010 		i40e_pf_quiesce_all_vsi(pf);
7011 		ret = i40e_suspend_port_tx(pf);
7012 		if (ret)
7013 			goto err;
7014 	}
7015 
7016 	/* Configure Port ETS Tx Scheduler */
7017 	ets_data.tc_valid_bits = tc_map;
7018 	ets_data.tc_strict_priority_flags = lltc_map;
7019 	ret = i40e_aq_config_switch_comp_ets
7020 		(hw, pf->mac_seid, &ets_data,
7021 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7022 	if (ret) {
7023 		dev_info(&pf->pdev->dev,
7024 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7025 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
7026 		goto out;
7027 	}
7028 
7029 	/* Configure Rx ETS HW */
7030 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7031 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7032 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7033 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7034 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7035 				   lltc_map);
7036 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7037 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7038 				     prio_type);
7039 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7040 			       new_cfg->etscfg.prioritytable);
7041 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7042 
7043 	/* Configure Rx Packet Buffers in HW */
7044 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7045 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
7046 
7047 		mfs_tc[i] = main_vsi->netdev->mtu;
7048 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7049 	}
7050 
7051 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7052 					 false, new_cfg->pfc.pfcenable,
7053 					 mfs_tc, &pb_cfg);
7054 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7055 
7056 	/* Update the local Rx Packet buffer config */
7057 	pf->pb_cfg = pb_cfg;
7058 
7059 	/* Inform the FW about changes to DCB configuration */
7060 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7061 	if (ret) {
7062 		dev_info(&pf->pdev->dev,
7063 			 "DCB Updated failed, err %pe aq_err %s\n",
7064 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
7065 		goto out;
7066 	}
7067 
7068 	/* Update the port DCBx configuration */
7069 	*old_cfg = *new_cfg;
7070 
7071 	/* Changes in configuration update VEB/VSI */
7072 	i40e_dcb_reconfigure(pf);
7073 out:
7074 	/* Re-start the VSIs if disabled */
7075 	if (need_reconfig) {
7076 		ret = i40e_resume_port_tx(pf);
7077 
7078 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7079 		/* In case of error no point in resuming VSIs */
7080 		if (ret)
7081 			goto err;
7082 
7083 		/* Wait for the PF's queues to be disabled */
7084 		ret = i40e_pf_wait_queues_disabled(pf);
7085 		if (ret) {
7086 			/* Schedule PF reset to recover */
7087 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7088 			i40e_service_event_schedule(pf);
7089 			goto err;
7090 		} else {
7091 			i40e_pf_unquiesce_all_vsi(pf);
7092 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7093 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7094 		}
7095 		/* registers are set, lets apply */
7096 		if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
7097 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7098 	}
7099 
7100 err:
7101 	return ret;
7102 }
7103 
7104 /**
7105  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7106  * @pf: PF being queried
7107  *
7108  * Set default DCB configuration in case DCB is to be done in SW.
7109  **/
i40e_dcb_sw_default_config(struct i40e_pf * pf)7110 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7111 {
7112 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7113 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7114 	struct i40e_hw *hw = &pf->hw;
7115 	int err;
7116 
7117 	if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
7118 		/* Update the local cached instance with TC0 ETS */
7119 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7120 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7121 		pf->tmp_cfg.etscfg.maxtcs = 0;
7122 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7123 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7124 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7125 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7126 		/* FW needs one App to configure HW */
7127 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7128 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7129 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7130 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7131 
7132 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7133 	}
7134 
7135 	memset(&ets_data, 0, sizeof(ets_data));
7136 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7137 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7138 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7139 
7140 	/* Enable ETS on the Physical port */
7141 	err = i40e_aq_config_switch_comp_ets
7142 		(hw, pf->mac_seid, &ets_data,
7143 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7144 	if (err) {
7145 		dev_info(&pf->pdev->dev,
7146 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7147 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7148 		err = -ENOENT;
7149 		goto out;
7150 	}
7151 
7152 	/* Update the local cached instance with TC0 ETS */
7153 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7154 	dcb_cfg->etscfg.cbs = 0;
7155 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7156 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7157 
7158 out:
7159 	return err;
7160 }
7161 
7162 /**
7163  * i40e_init_pf_dcb - Initialize DCB configuration
7164  * @pf: PF being configured
7165  *
7166  * Query the current DCB configuration and cache it
7167  * in the hardware structure
7168  **/
i40e_init_pf_dcb(struct i40e_pf * pf)7169 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7170 {
7171 	struct i40e_hw *hw = &pf->hw;
7172 	int err;
7173 
7174 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7175 	 * Also do not enable DCBx if FW LLDP agent is disabled
7176 	 */
7177 	if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
7178 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7179 		err = -EOPNOTSUPP;
7180 		goto out;
7181 	}
7182 	if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
7183 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7184 		err = i40e_dcb_sw_default_config(pf);
7185 		if (err) {
7186 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7187 			goto out;
7188 		}
7189 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7190 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7191 			       DCB_CAP_DCBX_VER_IEEE;
7192 		/* at init capable but disabled */
7193 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7194 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7195 		goto out;
7196 	}
7197 	err = i40e_init_dcb(hw, true);
7198 	if (!err) {
7199 		/* Device/Function is not DCBX capable */
7200 		if ((!hw->func_caps.dcb) ||
7201 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7202 			dev_info(&pf->pdev->dev,
7203 				 "DCBX offload is not supported or is disabled for this PF.\n");
7204 		} else {
7205 			/* When status is not DISABLED then DCBX in FW */
7206 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7207 				       DCB_CAP_DCBX_VER_IEEE;
7208 
7209 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7210 			/* Enable DCB tagging only when more than one TC
7211 			 * or explicitly disable if only one TC
7212 			 */
7213 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7214 				set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7215 			else
7216 				clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7217 			dev_dbg(&pf->pdev->dev,
7218 				"DCBX offload is supported for this PF.\n");
7219 		}
7220 	} else if (pf->hw.aq.asq_last_status == LIBIE_AQ_RC_EPERM) {
7221 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7222 		set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
7223 	} else {
7224 		dev_info(&pf->pdev->dev,
7225 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7226 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7227 	}
7228 
7229 out:
7230 	return err;
7231 }
7232 #endif /* CONFIG_I40E_DCB */
7233 
i40e_print_link_message_eee(struct i40e_vsi * vsi,const char * speed,const char * fc)7234 static void i40e_print_link_message_eee(struct i40e_vsi *vsi,
7235 					const char *speed, const char *fc)
7236 {
7237 	struct ethtool_keee kedata;
7238 
7239 	memzero_explicit(&kedata, sizeof(kedata));
7240 	if (vsi->netdev->ethtool_ops->get_eee)
7241 		vsi->netdev->ethtool_ops->get_eee(vsi->netdev, &kedata);
7242 
7243 	if (!linkmode_empty(kedata.supported))
7244 		netdev_info(vsi->netdev,
7245 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s, EEE: %s\n",
7246 			    speed, fc,
7247 			    kedata.eee_enabled ? "Enabled" : "Disabled");
7248 	else
7249 		netdev_info(vsi->netdev,
7250 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7251 			    speed, fc);
7252 }
7253 
7254 /**
7255  * i40e_print_link_message - print link up or down
7256  * @vsi: the VSI for which link needs a message
7257  * @isup: true of link is up, false otherwise
7258  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)7259 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7260 {
7261 	enum i40e_aq_link_speed new_speed;
7262 	struct i40e_pf *pf = vsi->back;
7263 	char *speed = "Unknown";
7264 	char *fc = "Unknown";
7265 	char *fec = "";
7266 	char *req_fec = "";
7267 	char *an = "";
7268 
7269 	if (isup)
7270 		new_speed = pf->hw.phy.link_info.link_speed;
7271 	else
7272 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7273 
7274 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7275 		return;
7276 	vsi->current_isup = isup;
7277 	vsi->current_speed = new_speed;
7278 	if (!isup) {
7279 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7280 		return;
7281 	}
7282 
7283 	/* Warn user if link speed on NPAR enabled partition is not at
7284 	 * least 10GB
7285 	 */
7286 	if (pf->hw.func_caps.npar_enable &&
7287 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7288 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7289 		netdev_warn(vsi->netdev,
7290 			    "The partition detected link speed that is less than 10Gbps\n");
7291 
7292 	switch (pf->hw.phy.link_info.link_speed) {
7293 	case I40E_LINK_SPEED_40GB:
7294 		speed = "40 G";
7295 		break;
7296 	case I40E_LINK_SPEED_20GB:
7297 		speed = "20 G";
7298 		break;
7299 	case I40E_LINK_SPEED_25GB:
7300 		speed = "25 G";
7301 		break;
7302 	case I40E_LINK_SPEED_10GB:
7303 		speed = "10 G";
7304 		break;
7305 	case I40E_LINK_SPEED_5GB:
7306 		speed = "5 G";
7307 		break;
7308 	case I40E_LINK_SPEED_2_5GB:
7309 		speed = "2.5 G";
7310 		break;
7311 	case I40E_LINK_SPEED_1GB:
7312 		speed = "1000 M";
7313 		break;
7314 	case I40E_LINK_SPEED_100MB:
7315 		speed = "100 M";
7316 		break;
7317 	default:
7318 		break;
7319 	}
7320 
7321 	switch (pf->hw.fc.current_mode) {
7322 	case I40E_FC_FULL:
7323 		fc = "RX/TX";
7324 		break;
7325 	case I40E_FC_TX_PAUSE:
7326 		fc = "TX";
7327 		break;
7328 	case I40E_FC_RX_PAUSE:
7329 		fc = "RX";
7330 		break;
7331 	default:
7332 		fc = "None";
7333 		break;
7334 	}
7335 
7336 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7337 		req_fec = "None";
7338 		fec = "None";
7339 		an = "False";
7340 
7341 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7342 			an = "True";
7343 
7344 		if (pf->hw.phy.link_info.fec_info &
7345 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7346 			fec = "CL74 FC-FEC/BASE-R";
7347 		else if (pf->hw.phy.link_info.fec_info &
7348 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7349 			fec = "CL108 RS-FEC";
7350 
7351 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7352 		 * both RS and FC are requested
7353 		 */
7354 		if (vsi->back->hw.phy.link_info.req_fec_info &
7355 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7356 			if (vsi->back->hw.phy.link_info.req_fec_info &
7357 			    I40E_AQ_REQUEST_FEC_RS)
7358 				req_fec = "CL108 RS-FEC";
7359 			else
7360 				req_fec = "CL74 FC-FEC/BASE-R";
7361 		}
7362 		netdev_info(vsi->netdev,
7363 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7364 			    speed, req_fec, fec, an, fc);
7365 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7366 		req_fec = "None";
7367 		fec = "None";
7368 		an = "False";
7369 
7370 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7371 			an = "True";
7372 
7373 		if (pf->hw.phy.link_info.fec_info &
7374 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7375 			fec = "CL74 FC-FEC/BASE-R";
7376 
7377 		if (pf->hw.phy.link_info.req_fec_info &
7378 		    I40E_AQ_REQUEST_FEC_KR)
7379 			req_fec = "CL74 FC-FEC/BASE-R";
7380 
7381 		netdev_info(vsi->netdev,
7382 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7383 			    speed, req_fec, fec, an, fc);
7384 	} else {
7385 		i40e_print_link_message_eee(vsi, speed, fc);
7386 	}
7387 
7388 }
7389 
7390 /**
7391  * i40e_up_complete - Finish the last steps of bringing up a connection
7392  * @vsi: the VSI being configured
7393  **/
i40e_up_complete(struct i40e_vsi * vsi)7394 static int i40e_up_complete(struct i40e_vsi *vsi)
7395 {
7396 	struct i40e_pf *pf = vsi->back;
7397 	int err;
7398 
7399 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
7400 		i40e_vsi_configure_msix(vsi);
7401 	else
7402 		i40e_configure_msi_and_legacy(vsi);
7403 
7404 	/* start rings */
7405 	err = i40e_vsi_start_rings(vsi);
7406 	if (err)
7407 		return err;
7408 
7409 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7410 	i40e_napi_enable_all(vsi);
7411 	i40e_vsi_enable_irq(vsi);
7412 
7413 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7414 	    (vsi->netdev)) {
7415 		i40e_print_link_message(vsi, true);
7416 		netif_tx_start_all_queues(vsi->netdev);
7417 		netif_carrier_on(vsi->netdev);
7418 	}
7419 
7420 	/* replay FDIR SB filters */
7421 	if (vsi->type == I40E_VSI_FDIR) {
7422 		/* reset fd counters */
7423 		pf->fd_add_err = 0;
7424 		pf->fd_atr_cnt = 0;
7425 		i40e_fdir_filter_restore(vsi);
7426 	}
7427 
7428 	/* On the next run of the service_task, notify any clients of the new
7429 	 * opened netdev
7430 	 */
7431 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7432 	i40e_service_event_schedule(pf);
7433 
7434 	return 0;
7435 }
7436 
7437 /**
7438  * i40e_vsi_reinit_locked - Reset the VSI
7439  * @vsi: the VSI being configured
7440  *
7441  * Rebuild the ring structs after some configuration
7442  * has changed, e.g. MTU size.
7443  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)7444 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7445 {
7446 	struct i40e_pf *pf = vsi->back;
7447 
7448 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7449 		usleep_range(1000, 2000);
7450 	i40e_down(vsi);
7451 
7452 	i40e_up(vsi);
7453 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7454 }
7455 
7456 /**
7457  * i40e_force_link_state - Force the link status
7458  * @pf: board private structure
7459  * @is_up: whether the link state should be forced up or down
7460  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)7461 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7462 {
7463 	struct i40e_aq_get_phy_abilities_resp abilities;
7464 	struct i40e_aq_set_phy_config config = {0};
7465 	bool non_zero_phy_type = is_up;
7466 	struct i40e_hw *hw = &pf->hw;
7467 	u64 mask;
7468 	u8 speed;
7469 	int err;
7470 
7471 	/* Card might've been put in an unstable state by other drivers
7472 	 * and applications, which causes incorrect speed values being
7473 	 * set on startup. In order to clear speed registers, we call
7474 	 * get_phy_capabilities twice, once to get initial state of
7475 	 * available speeds, and once to get current PHY config.
7476 	 */
7477 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7478 					   NULL);
7479 	if (err) {
7480 		dev_err(&pf->pdev->dev,
7481 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7482 			ERR_PTR(err), libie_aq_str(hw->aq.asq_last_status));
7483 		return err;
7484 	}
7485 	speed = abilities.link_speed;
7486 
7487 	/* Get the current phy config */
7488 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7489 					   NULL);
7490 	if (err) {
7491 		dev_err(&pf->pdev->dev,
7492 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7493 			ERR_PTR(err), libie_aq_str(hw->aq.asq_last_status));
7494 		return err;
7495 	}
7496 
7497 	/* If link needs to go up, but was not forced to go down,
7498 	 * and its speed values are OK, no need for a flap
7499 	 * if non_zero_phy_type was set, still need to force up
7500 	 */
7501 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
7502 		non_zero_phy_type = true;
7503 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7504 		return 0;
7505 
7506 	/* To force link we need to set bits for all supported PHY types,
7507 	 * but there are now more than 32, so we need to split the bitmap
7508 	 * across two fields.
7509 	 */
7510 	mask = I40E_PHY_TYPES_BITMASK;
7511 	config.phy_type =
7512 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7513 	config.phy_type_ext =
7514 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7515 	/* Copy the old settings, except of phy_type */
7516 	config.abilities = abilities.abilities;
7517 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
7518 		if (is_up)
7519 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7520 		else
7521 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7522 	}
7523 	if (abilities.link_speed != 0)
7524 		config.link_speed = abilities.link_speed;
7525 	else
7526 		config.link_speed = speed;
7527 	config.eee_capability = abilities.eee_capability;
7528 	config.eeer = abilities.eeer_val;
7529 	config.low_power_ctrl = abilities.d3_lpan;
7530 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7531 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7532 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7533 
7534 	if (err) {
7535 		dev_err(&pf->pdev->dev,
7536 			"set phy config ret =  %pe last_status =  %s\n",
7537 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7538 		return err;
7539 	}
7540 
7541 	/* Update the link info */
7542 	err = i40e_update_link_info(hw);
7543 	if (err) {
7544 		/* Wait a little bit (on 40G cards it sometimes takes a really
7545 		 * long time for link to come back from the atomic reset)
7546 		 * and try once more
7547 		 */
7548 		msleep(1000);
7549 		i40e_update_link_info(hw);
7550 	}
7551 
7552 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7553 
7554 	return 0;
7555 }
7556 
7557 /**
7558  * i40e_up - Bring the connection back up after being down
7559  * @vsi: the VSI being configured
7560  **/
i40e_up(struct i40e_vsi * vsi)7561 int i40e_up(struct i40e_vsi *vsi)
7562 {
7563 	int err;
7564 
7565 	if (vsi->type == I40E_VSI_MAIN &&
7566 	    (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7567 	     test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7568 		i40e_force_link_state(vsi->back, true);
7569 
7570 	err = i40e_vsi_configure(vsi);
7571 	if (!err)
7572 		err = i40e_up_complete(vsi);
7573 
7574 	return err;
7575 }
7576 
7577 /**
7578  * i40e_down - Shutdown the connection processing
7579  * @vsi: the VSI being stopped
7580  **/
i40e_down(struct i40e_vsi * vsi)7581 void i40e_down(struct i40e_vsi *vsi)
7582 {
7583 	int i;
7584 
7585 	/* It is assumed that the caller of this function
7586 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7587 	 */
7588 	if (vsi->netdev) {
7589 		netif_carrier_off(vsi->netdev);
7590 		netif_tx_disable(vsi->netdev);
7591 	}
7592 	i40e_vsi_disable_irq(vsi);
7593 	i40e_vsi_stop_rings(vsi);
7594 	if (vsi->type == I40E_VSI_MAIN &&
7595 	   (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7596 	    test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7597 		i40e_force_link_state(vsi->back, false);
7598 	i40e_napi_disable_all(vsi);
7599 
7600 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7601 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7602 		if (i40e_enabled_xdp_vsi(vsi)) {
7603 			/* Make sure that in-progress ndo_xdp_xmit and
7604 			 * ndo_xsk_wakeup calls are completed.
7605 			 */
7606 			synchronize_rcu();
7607 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7608 		}
7609 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7610 	}
7611 
7612 }
7613 
7614 /**
7615  * i40e_validate_mqprio_qopt- validate queue mapping info
7616  * @vsi: the VSI being configured
7617  * @mqprio_qopt: queue parametrs
7618  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7619 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7620 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7621 {
7622 	u64 sum_max_rate = 0;
7623 	u64 max_rate = 0;
7624 	int i;
7625 
7626 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7627 	    mqprio_qopt->qopt.num_tc < 1 ||
7628 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7629 		return -EINVAL;
7630 	for (i = 0; ; i++) {
7631 		if (!mqprio_qopt->qopt.count[i])
7632 			return -EINVAL;
7633 		if (mqprio_qopt->min_rate[i]) {
7634 			dev_err(&vsi->back->pdev->dev,
7635 				"Invalid min tx rate (greater than 0) specified\n");
7636 			return -EINVAL;
7637 		}
7638 		max_rate = mqprio_qopt->max_rate[i];
7639 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7640 		sum_max_rate += max_rate;
7641 
7642 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7643 			break;
7644 		if (mqprio_qopt->qopt.offset[i + 1] !=
7645 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7646 			return -EINVAL;
7647 	}
7648 	if (vsi->num_queue_pairs <
7649 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7650 		dev_err(&vsi->back->pdev->dev,
7651 			"Failed to create traffic channel, insufficient number of queues.\n");
7652 		return -EINVAL;
7653 	}
7654 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7655 		dev_err(&vsi->back->pdev->dev,
7656 			"Invalid max tx rate specified\n");
7657 		return -EINVAL;
7658 	}
7659 	return 0;
7660 }
7661 
7662 /**
7663  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7664  * @vsi: the VSI being configured
7665  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7666 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7667 {
7668 	u16 qcount;
7669 	int i;
7670 
7671 	/* Only TC0 is enabled */
7672 	vsi->tc_config.numtc = 1;
7673 	vsi->tc_config.enabled_tc = 1;
7674 	qcount = min_t(int, vsi->alloc_queue_pairs,
7675 		       i40e_pf_get_max_q_per_tc(vsi->back));
7676 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7677 		/* For the TC that is not enabled set the offset to default
7678 		 * queue and allocate one queue for the given TC.
7679 		 */
7680 		vsi->tc_config.tc_info[i].qoffset = 0;
7681 		if (i == 0)
7682 			vsi->tc_config.tc_info[i].qcount = qcount;
7683 		else
7684 			vsi->tc_config.tc_info[i].qcount = 1;
7685 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7686 	}
7687 }
7688 
7689 /**
7690  * i40e_del_macvlan_filter
7691  * @hw: pointer to the HW structure
7692  * @seid: seid of the channel VSI
7693  * @macaddr: the mac address to apply as a filter
7694  * @aq_err: store the admin Q error
7695  *
7696  * This function deletes a mac filter on the channel VSI which serves as the
7697  * macvlan. Returns 0 on success.
7698  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7699 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7700 				   const u8 *macaddr, int *aq_err)
7701 {
7702 	struct i40e_aqc_remove_macvlan_element_data element;
7703 	int status;
7704 
7705 	memset(&element, 0, sizeof(element));
7706 	ether_addr_copy(element.mac_addr, macaddr);
7707 	element.vlan_tag = 0;
7708 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7709 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7710 	*aq_err = hw->aq.asq_last_status;
7711 
7712 	return status;
7713 }
7714 
7715 /**
7716  * i40e_add_macvlan_filter
7717  * @hw: pointer to the HW structure
7718  * @seid: seid of the channel VSI
7719  * @macaddr: the mac address to apply as a filter
7720  * @aq_err: store the admin Q error
7721  *
7722  * This function adds a mac filter on the channel VSI which serves as the
7723  * macvlan. Returns 0 on success.
7724  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7725 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7726 				   const u8 *macaddr, int *aq_err)
7727 {
7728 	struct i40e_aqc_add_macvlan_element_data element;
7729 	u16 cmd_flags = 0;
7730 	int status;
7731 
7732 	ether_addr_copy(element.mac_addr, macaddr);
7733 	element.vlan_tag = 0;
7734 	element.queue_number = 0;
7735 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7736 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7737 	element.flags = cpu_to_le16(cmd_flags);
7738 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7739 	*aq_err = hw->aq.asq_last_status;
7740 
7741 	return status;
7742 }
7743 
7744 /**
7745  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7746  * @vsi: the VSI we want to access
7747  * @ch: the channel we want to access
7748  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7749 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7750 {
7751 	struct i40e_ring *tx_ring, *rx_ring;
7752 	u16 pf_q;
7753 	int i;
7754 
7755 	for (i = 0; i < ch->num_queue_pairs; i++) {
7756 		pf_q = ch->base_queue + i;
7757 		tx_ring = vsi->tx_rings[pf_q];
7758 		tx_ring->ch = NULL;
7759 		rx_ring = vsi->rx_rings[pf_q];
7760 		rx_ring->ch = NULL;
7761 	}
7762 }
7763 
7764 /**
7765  * i40e_free_macvlan_channels
7766  * @vsi: the VSI we want to access
7767  *
7768  * This function frees the Qs of the channel VSI from
7769  * the stack and also deletes the channel VSIs which
7770  * serve as macvlans.
7771  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7772 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7773 {
7774 	struct i40e_channel *ch, *ch_tmp;
7775 	int ret;
7776 
7777 	if (list_empty(&vsi->macvlan_list))
7778 		return;
7779 
7780 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7781 		struct i40e_vsi *parent_vsi;
7782 
7783 		if (i40e_is_channel_macvlan(ch)) {
7784 			i40e_reset_ch_rings(vsi, ch);
7785 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7786 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7787 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7788 			kfree(ch->fwd);
7789 			ch->fwd = NULL;
7790 		}
7791 
7792 		list_del(&ch->list);
7793 		parent_vsi = ch->parent_vsi;
7794 		if (!parent_vsi || !ch->initialized) {
7795 			kfree(ch);
7796 			continue;
7797 		}
7798 
7799 		/* remove the VSI */
7800 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7801 					     NULL);
7802 		if (ret)
7803 			dev_err(&vsi->back->pdev->dev,
7804 				"unable to remove channel (%d) for parent VSI(%d)\n",
7805 				ch->seid, parent_vsi->seid);
7806 		kfree(ch);
7807 	}
7808 	vsi->macvlan_cnt = 0;
7809 }
7810 
7811 /**
7812  * i40e_fwd_ring_up - bring the macvlan device up
7813  * @vsi: the VSI we want to access
7814  * @vdev: macvlan netdevice
7815  * @fwd: the private fwd structure
7816  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7817 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7818 			    struct i40e_fwd_adapter *fwd)
7819 {
7820 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7821 	int ret = 0, num_tc = 1,  i, aq_err;
7822 	struct i40e_pf *pf = vsi->back;
7823 	struct i40e_hw *hw = &pf->hw;
7824 
7825 	/* Go through the list and find an available channel */
7826 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7827 		if (!i40e_is_channel_macvlan(iter)) {
7828 			iter->fwd = fwd;
7829 			/* record configuration for macvlan interface in vdev */
7830 			for (i = 0; i < num_tc; i++)
7831 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7832 							     i,
7833 							     iter->num_queue_pairs,
7834 							     iter->base_queue);
7835 			for (i = 0; i < iter->num_queue_pairs; i++) {
7836 				struct i40e_ring *tx_ring, *rx_ring;
7837 				u16 pf_q;
7838 
7839 				pf_q = iter->base_queue + i;
7840 
7841 				/* Get to TX ring ptr */
7842 				tx_ring = vsi->tx_rings[pf_q];
7843 				tx_ring->ch = iter;
7844 
7845 				/* Get the RX ring ptr */
7846 				rx_ring = vsi->rx_rings[pf_q];
7847 				rx_ring->ch = iter;
7848 			}
7849 			ch = iter;
7850 			break;
7851 		}
7852 	}
7853 
7854 	if (!ch)
7855 		return -EINVAL;
7856 
7857 	/* Guarantee all rings are updated before we update the
7858 	 * MAC address filter.
7859 	 */
7860 	wmb();
7861 
7862 	/* Add a mac filter */
7863 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7864 	if (ret) {
7865 		/* if we cannot add the MAC rule then disable the offload */
7866 		macvlan_release_l2fw_offload(vdev);
7867 		for (i = 0; i < ch->num_queue_pairs; i++) {
7868 			struct i40e_ring *rx_ring;
7869 			u16 pf_q;
7870 
7871 			pf_q = ch->base_queue + i;
7872 			rx_ring = vsi->rx_rings[pf_q];
7873 			rx_ring->netdev = NULL;
7874 		}
7875 		dev_info(&pf->pdev->dev,
7876 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7877 			  ERR_PTR(ret), libie_aq_str(aq_err));
7878 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7879 	}
7880 
7881 	return ret;
7882 }
7883 
7884 /**
7885  * i40e_setup_macvlans - create the channels which will be macvlans
7886  * @vsi: the VSI we want to access
7887  * @macvlan_cnt: no. of macvlans to be setup
7888  * @qcnt: no. of Qs per macvlan
7889  * @vdev: macvlan netdevice
7890  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7891 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7892 			       struct net_device *vdev)
7893 {
7894 	struct i40e_pf *pf = vsi->back;
7895 	struct i40e_hw *hw = &pf->hw;
7896 	struct i40e_vsi_context ctxt;
7897 	u16 sections, qmap, num_qps;
7898 	struct i40e_channel *ch;
7899 	int i, pow, ret = 0;
7900 	u8 offset = 0;
7901 
7902 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7903 		return -EINVAL;
7904 
7905 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7906 
7907 	/* find the next higher power-of-2 of num queue pairs */
7908 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7909 
7910 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7911 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7912 
7913 	/* Setup context bits for the main VSI */
7914 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7915 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7916 	memset(&ctxt, 0, sizeof(ctxt));
7917 	ctxt.seid = vsi->seid;
7918 	ctxt.pf_num = vsi->back->hw.pf_id;
7919 	ctxt.vf_num = 0;
7920 	ctxt.uplink_seid = vsi->uplink_seid;
7921 	ctxt.info = vsi->info;
7922 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7923 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7924 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7925 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7926 
7927 	/* Reconfigure RSS for main VSI with new max queue count */
7928 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7929 	ret = i40e_vsi_config_rss(vsi);
7930 	if (ret) {
7931 		dev_info(&pf->pdev->dev,
7932 			 "Failed to reconfig RSS for num_queues (%u)\n",
7933 			 vsi->rss_size);
7934 		return ret;
7935 	}
7936 	vsi->reconfig_rss = true;
7937 	dev_dbg(&vsi->back->pdev->dev,
7938 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7939 	vsi->next_base_queue = num_qps;
7940 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7941 
7942 	/* Update the VSI after updating the VSI queue-mapping
7943 	 * information
7944 	 */
7945 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7946 	if (ret) {
7947 		dev_info(&pf->pdev->dev,
7948 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7949 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
7950 		return ret;
7951 	}
7952 	/* update the local VSI info with updated queue map */
7953 	i40e_vsi_update_queue_map(vsi, &ctxt);
7954 	vsi->info.valid_sections = 0;
7955 
7956 	/* Create channels for macvlans */
7957 	INIT_LIST_HEAD(&vsi->macvlan_list);
7958 	for (i = 0; i < macvlan_cnt; i++) {
7959 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7960 		if (!ch) {
7961 			ret = -ENOMEM;
7962 			goto err_free;
7963 		}
7964 		INIT_LIST_HEAD(&ch->list);
7965 		ch->num_queue_pairs = qcnt;
7966 		if (!i40e_setup_channel(pf, vsi, ch)) {
7967 			ret = -EINVAL;
7968 			kfree(ch);
7969 			goto err_free;
7970 		}
7971 		ch->parent_vsi = vsi;
7972 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7973 		vsi->macvlan_cnt++;
7974 		list_add_tail(&ch->list, &vsi->macvlan_list);
7975 	}
7976 
7977 	return ret;
7978 
7979 err_free:
7980 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7981 	i40e_free_macvlan_channels(vsi);
7982 
7983 	return ret;
7984 }
7985 
7986 /**
7987  * i40e_fwd_add - configure macvlans
7988  * @netdev: net device to configure
7989  * @vdev: macvlan netdevice
7990  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)7991 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7992 {
7993 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7994 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7995 	struct i40e_vsi *vsi = np->vsi;
7996 	struct i40e_pf *pf = vsi->back;
7997 	struct i40e_fwd_adapter *fwd;
7998 	int avail_macvlan, ret;
7999 
8000 	if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8001 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
8002 		return ERR_PTR(-EINVAL);
8003 	}
8004 	if (i40e_is_tc_mqprio_enabled(pf)) {
8005 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
8006 		return ERR_PTR(-EINVAL);
8007 	}
8008 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
8009 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
8010 		return ERR_PTR(-EINVAL);
8011 	}
8012 
8013 	/* The macvlan device has to be a single Q device so that the
8014 	 * tc_to_txq field can be reused to pick the tx queue.
8015 	 */
8016 	if (netif_is_multiqueue(vdev))
8017 		return ERR_PTR(-ERANGE);
8018 
8019 	if (!vsi->macvlan_cnt) {
8020 		/* reserve bit 0 for the pf device */
8021 		set_bit(0, vsi->fwd_bitmask);
8022 
8023 		/* Try to reserve as many queues as possible for macvlans. First
8024 		 * reserve 3/4th of max vectors, then half, then quarter and
8025 		 * calculate Qs per macvlan as you go
8026 		 */
8027 		vectors = pf->num_lan_msix;
8028 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8029 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8030 			q_per_macvlan = 4;
8031 			macvlan_cnt = (vectors - 32) / 4;
8032 		} else if (vectors <= 64 && vectors > 32) {
8033 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8034 			q_per_macvlan = 2;
8035 			macvlan_cnt = (vectors - 16) / 2;
8036 		} else if (vectors <= 32 && vectors > 16) {
8037 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8038 			q_per_macvlan = 1;
8039 			macvlan_cnt = vectors - 16;
8040 		} else if (vectors <= 16 && vectors > 8) {
8041 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8042 			q_per_macvlan = 1;
8043 			macvlan_cnt = vectors - 8;
8044 		} else {
8045 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8046 			q_per_macvlan = 1;
8047 			macvlan_cnt = vectors - 1;
8048 		}
8049 
8050 		if (macvlan_cnt == 0)
8051 			return ERR_PTR(-EBUSY);
8052 
8053 		/* Quiesce VSI queues */
8054 		i40e_quiesce_vsi(vsi);
8055 
8056 		/* sets up the macvlans but does not "enable" them */
8057 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8058 					  vdev);
8059 		if (ret)
8060 			return ERR_PTR(ret);
8061 
8062 		/* Unquiesce VSI */
8063 		i40e_unquiesce_vsi(vsi);
8064 	}
8065 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8066 					    vsi->macvlan_cnt);
8067 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8068 		return ERR_PTR(-EBUSY);
8069 
8070 	/* create the fwd struct */
8071 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8072 	if (!fwd)
8073 		return ERR_PTR(-ENOMEM);
8074 
8075 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8076 	fwd->bit_no = avail_macvlan;
8077 	netdev_set_sb_channel(vdev, avail_macvlan);
8078 	fwd->netdev = vdev;
8079 
8080 	if (!netif_running(netdev))
8081 		return fwd;
8082 
8083 	/* Set fwd ring up */
8084 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8085 	if (ret) {
8086 		/* unbind the queues and drop the subordinate channel config */
8087 		netdev_unbind_sb_channel(netdev, vdev);
8088 		netdev_set_sb_channel(vdev, 0);
8089 
8090 		kfree(fwd);
8091 		return ERR_PTR(-EINVAL);
8092 	}
8093 
8094 	return fwd;
8095 }
8096 
8097 /**
8098  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8099  * @vsi: the VSI we want to access
8100  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)8101 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8102 {
8103 	struct i40e_channel *ch, *ch_tmp;
8104 	struct i40e_pf *pf = vsi->back;
8105 	struct i40e_hw *hw = &pf->hw;
8106 	int aq_err, ret = 0;
8107 
8108 	if (list_empty(&vsi->macvlan_list))
8109 		return;
8110 
8111 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8112 		if (i40e_is_channel_macvlan(ch)) {
8113 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8114 						      i40e_channel_mac(ch),
8115 						      &aq_err);
8116 			if (!ret) {
8117 				/* Reset queue contexts */
8118 				i40e_reset_ch_rings(vsi, ch);
8119 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8120 				netdev_unbind_sb_channel(vsi->netdev,
8121 							 ch->fwd->netdev);
8122 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8123 				kfree(ch->fwd);
8124 				ch->fwd = NULL;
8125 			}
8126 		}
8127 	}
8128 }
8129 
8130 /**
8131  * i40e_fwd_del - delete macvlan interfaces
8132  * @netdev: net device to configure
8133  * @vdev: macvlan netdevice
8134  */
i40e_fwd_del(struct net_device * netdev,void * vdev)8135 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8136 {
8137 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8138 	struct i40e_fwd_adapter *fwd = vdev;
8139 	struct i40e_channel *ch, *ch_tmp;
8140 	struct i40e_vsi *vsi = np->vsi;
8141 	struct i40e_pf *pf = vsi->back;
8142 	struct i40e_hw *hw = &pf->hw;
8143 	int aq_err, ret = 0;
8144 
8145 	/* Find the channel associated with the macvlan and del mac filter */
8146 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8147 		if (i40e_is_channel_macvlan(ch) &&
8148 		    ether_addr_equal(i40e_channel_mac(ch),
8149 				     fwd->netdev->dev_addr)) {
8150 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8151 						      i40e_channel_mac(ch),
8152 						      &aq_err);
8153 			if (!ret) {
8154 				/* Reset queue contexts */
8155 				i40e_reset_ch_rings(vsi, ch);
8156 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8157 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8158 				netdev_set_sb_channel(fwd->netdev, 0);
8159 				kfree(ch->fwd);
8160 				ch->fwd = NULL;
8161 			} else {
8162 				dev_info(&pf->pdev->dev,
8163 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8164 					  ERR_PTR(ret), libie_aq_str(aq_err));
8165 			}
8166 			break;
8167 		}
8168 	}
8169 }
8170 
8171 /**
8172  * i40e_setup_tc - configure multiple traffic classes
8173  * @netdev: net device to configure
8174  * @type_data: tc offload data
8175  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)8176 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8177 {
8178 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8179 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8180 	struct i40e_vsi *vsi = np->vsi;
8181 	struct i40e_pf *pf = vsi->back;
8182 	u8 enabled_tc = 0, num_tc, hw;
8183 	bool need_reset = false;
8184 	int old_queue_pairs;
8185 	int ret = -EINVAL;
8186 	u16 mode;
8187 	int i;
8188 
8189 	old_queue_pairs = vsi->num_queue_pairs;
8190 	num_tc = mqprio_qopt->qopt.num_tc;
8191 	hw = mqprio_qopt->qopt.hw;
8192 	mode = mqprio_qopt->mode;
8193 	if (!hw) {
8194 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8195 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8196 		goto config_tc;
8197 	}
8198 
8199 	/* Check if MFP enabled */
8200 	if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
8201 		netdev_info(netdev,
8202 			    "Configuring TC not supported in MFP mode\n");
8203 		return ret;
8204 	}
8205 	switch (mode) {
8206 	case TC_MQPRIO_MODE_DCB:
8207 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8208 
8209 		/* Check if DCB enabled to continue */
8210 		if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8211 			netdev_info(netdev,
8212 				    "DCB is not enabled for adapter\n");
8213 			return ret;
8214 		}
8215 
8216 		/* Check whether tc count is within enabled limit */
8217 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8218 			netdev_info(netdev,
8219 				    "TC count greater than enabled on link for adapter\n");
8220 			return ret;
8221 		}
8222 		break;
8223 	case TC_MQPRIO_MODE_CHANNEL:
8224 		if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8225 			netdev_info(netdev,
8226 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8227 			return ret;
8228 		}
8229 		if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
8230 			return ret;
8231 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8232 		if (ret)
8233 			return ret;
8234 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8235 		       sizeof(*mqprio_qopt));
8236 		set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8237 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
8238 		break;
8239 	default:
8240 		return -EINVAL;
8241 	}
8242 
8243 config_tc:
8244 	/* Generate TC map for number of tc requested */
8245 	for (i = 0; i < num_tc; i++)
8246 		enabled_tc |= BIT(i);
8247 
8248 	/* Requesting same TC configuration as already enabled */
8249 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8250 	    mode != TC_MQPRIO_MODE_CHANNEL)
8251 		return 0;
8252 
8253 	/* Quiesce VSI queues */
8254 	i40e_quiesce_vsi(vsi);
8255 
8256 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8257 		i40e_remove_queue_channels(vsi);
8258 
8259 	/* Configure VSI for enabled TCs */
8260 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8261 	if (ret) {
8262 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8263 			    vsi->seid);
8264 		need_reset = true;
8265 		goto exit;
8266 	} else if (enabled_tc &&
8267 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8268 		netdev_info(netdev,
8269 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8270 			    vsi->tc_config.tc_info[0].qcount);
8271 		ret = -EINVAL;
8272 		need_reset = true;
8273 		goto exit;
8274 	}
8275 
8276 	dev_info(&vsi->back->pdev->dev,
8277 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8278 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8279 
8280 	if (i40e_is_tc_mqprio_enabled(pf)) {
8281 		if (vsi->mqprio_qopt.max_rate[0]) {
8282 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8283 						  vsi->mqprio_qopt.max_rate[0]);
8284 
8285 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8286 			if (!ret) {
8287 				u64 credits = max_tx_rate;
8288 
8289 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8290 				dev_dbg(&vsi->back->pdev->dev,
8291 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8292 					max_tx_rate,
8293 					credits,
8294 					vsi->seid);
8295 			} else {
8296 				need_reset = true;
8297 				goto exit;
8298 			}
8299 		}
8300 		ret = i40e_configure_queue_channels(vsi);
8301 		if (ret) {
8302 			vsi->num_queue_pairs = old_queue_pairs;
8303 			netdev_info(netdev,
8304 				    "Failed configuring queue channels\n");
8305 			need_reset = true;
8306 			goto exit;
8307 		}
8308 	}
8309 
8310 exit:
8311 	/* Reset the configuration data to defaults, only TC0 is enabled */
8312 	if (need_reset) {
8313 		i40e_vsi_set_default_tc_config(vsi);
8314 		need_reset = false;
8315 	}
8316 
8317 	/* Unquiesce VSI */
8318 	i40e_unquiesce_vsi(vsi);
8319 	return ret;
8320 }
8321 
8322 /**
8323  * i40e_set_cld_element - sets cloud filter element data
8324  * @filter: cloud filter rule
8325  * @cld: ptr to cloud filter element data
8326  *
8327  * This is helper function to copy data into cloud filter element
8328  **/
8329 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)8330 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8331 		     struct i40e_aqc_cloud_filters_element_data *cld)
8332 {
8333 	u32 ipa;
8334 	int i;
8335 
8336 	memset(cld, 0, sizeof(*cld));
8337 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8338 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8339 
8340 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8341 		return;
8342 
8343 	if (filter->n_proto == ETH_P_IPV6) {
8344 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8345 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8346 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8347 
8348 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8349 		}
8350 	} else {
8351 		ipa = be32_to_cpu(filter->dst_ipv4);
8352 
8353 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8354 	}
8355 
8356 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8357 
8358 	/* tenant_id is not supported by FW now, once the support is enabled
8359 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8360 	 */
8361 	if (filter->tenant_id)
8362 		return;
8363 }
8364 
8365 /**
8366  * i40e_add_del_cloud_filter - Add/del cloud filter
8367  * @vsi: pointer to VSI
8368  * @filter: cloud filter rule
8369  * @add: if true, add, if false, delete
8370  *
8371  * Add or delete a cloud filter for a specific flow spec.
8372  * Returns 0 if the filter were successfully added.
8373  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8374 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8375 			      struct i40e_cloud_filter *filter, bool add)
8376 {
8377 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8378 	struct i40e_pf *pf = vsi->back;
8379 	int ret;
8380 	static const u16 flag_table[128] = {
8381 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8382 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8383 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8384 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8385 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8386 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8387 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8388 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8389 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8390 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8391 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8392 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8393 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8394 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8395 	};
8396 
8397 	if (filter->flags >= ARRAY_SIZE(flag_table))
8398 		return -EIO;
8399 
8400 	memset(&cld_filter, 0, sizeof(cld_filter));
8401 
8402 	/* copy element needed to add cloud filter from filter */
8403 	i40e_set_cld_element(filter, &cld_filter);
8404 
8405 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8406 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8407 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8408 
8409 	if (filter->n_proto == ETH_P_IPV6)
8410 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8411 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8412 	else
8413 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8414 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8415 
8416 	if (add)
8417 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8418 						&cld_filter, 1);
8419 	else
8420 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8421 						&cld_filter, 1);
8422 	if (ret)
8423 		dev_dbg(&pf->pdev->dev,
8424 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8425 			add ? "add" : "delete", filter->dst_port, ret,
8426 			pf->hw.aq.asq_last_status);
8427 	else
8428 		dev_info(&pf->pdev->dev,
8429 			 "%s cloud filter for VSI: %d\n",
8430 			 add ? "Added" : "Deleted", filter->seid);
8431 	return ret;
8432 }
8433 
8434 /**
8435  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8436  * @vsi: pointer to VSI
8437  * @filter: cloud filter rule
8438  * @add: if true, add, if false, delete
8439  *
8440  * Add or delete a cloud filter for a specific flow spec using big buffer.
8441  * Returns 0 if the filter were successfully added.
8442  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8443 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8444 				      struct i40e_cloud_filter *filter,
8445 				      bool add)
8446 {
8447 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8448 	struct i40e_pf *pf = vsi->back;
8449 	int ret;
8450 
8451 	/* Both (src/dst) valid mac_addr are not supported */
8452 	if ((is_valid_ether_addr(filter->dst_mac) &&
8453 	     is_valid_ether_addr(filter->src_mac)) ||
8454 	    (is_multicast_ether_addr(filter->dst_mac) &&
8455 	     is_multicast_ether_addr(filter->src_mac)))
8456 		return -EOPNOTSUPP;
8457 
8458 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8459 	 * ports are not supported via big buffer now.
8460 	 */
8461 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8462 		return -EOPNOTSUPP;
8463 
8464 	/* adding filter using src_port/src_ip is not supported at this stage */
8465 	if (filter->src_port ||
8466 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8467 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8468 		return -EOPNOTSUPP;
8469 
8470 	memset(&cld_filter, 0, sizeof(cld_filter));
8471 
8472 	/* copy element needed to add cloud filter from filter */
8473 	i40e_set_cld_element(filter, &cld_filter.element);
8474 
8475 	if (is_valid_ether_addr(filter->dst_mac) ||
8476 	    is_valid_ether_addr(filter->src_mac) ||
8477 	    is_multicast_ether_addr(filter->dst_mac) ||
8478 	    is_multicast_ether_addr(filter->src_mac)) {
8479 		/* MAC + IP : unsupported mode */
8480 		if (filter->dst_ipv4)
8481 			return -EOPNOTSUPP;
8482 
8483 		/* since we validated that L4 port must be valid before
8484 		 * we get here, start with respective "flags" value
8485 		 * and update if vlan is present or not
8486 		 */
8487 		cld_filter.element.flags =
8488 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8489 
8490 		if (filter->vlan_id) {
8491 			cld_filter.element.flags =
8492 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8493 		}
8494 
8495 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8496 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8497 		cld_filter.element.flags =
8498 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8499 		if (filter->n_proto == ETH_P_IPV6)
8500 			cld_filter.element.flags |=
8501 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8502 		else
8503 			cld_filter.element.flags |=
8504 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8505 	} else {
8506 		dev_err(&pf->pdev->dev,
8507 			"either mac or ip has to be valid for cloud filter\n");
8508 		return -EINVAL;
8509 	}
8510 
8511 	/* Now copy L4 port in Byte 6..7 in general fields */
8512 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8513 						be16_to_cpu(filter->dst_port);
8514 
8515 	if (add) {
8516 		/* Validate current device switch mode, change if necessary */
8517 		ret = i40e_validate_and_set_switch_mode(vsi);
8518 		if (ret) {
8519 			dev_err(&pf->pdev->dev,
8520 				"failed to set switch mode, ret %d\n",
8521 				ret);
8522 			return ret;
8523 		}
8524 
8525 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8526 						   &cld_filter, 1);
8527 	} else {
8528 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8529 						   &cld_filter, 1);
8530 	}
8531 
8532 	if (ret)
8533 		dev_dbg(&pf->pdev->dev,
8534 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8535 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8536 	else
8537 		dev_info(&pf->pdev->dev,
8538 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8539 			 add ? "add" : "delete", filter->seid,
8540 			 ntohs(filter->dst_port));
8541 	return ret;
8542 }
8543 
8544 /**
8545  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8546  * @vsi: Pointer to VSI
8547  * @f: Pointer to struct flow_cls_offload
8548  * @filter: Pointer to cloud filter structure
8549  *
8550  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)8551 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8552 				 struct flow_cls_offload *f,
8553 				 struct i40e_cloud_filter *filter)
8554 {
8555 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8556 	struct flow_dissector *dissector = rule->match.dissector;
8557 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8558 	struct i40e_pf *pf = vsi->back;
8559 	u8 field_flags = 0;
8560 
8561 	if (dissector->used_keys &
8562 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8563 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8564 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8565 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8566 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8567 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8568 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8569 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8570 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8571 			dissector->used_keys);
8572 		return -EOPNOTSUPP;
8573 	}
8574 
8575 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8576 		struct flow_match_enc_keyid match;
8577 
8578 		flow_rule_match_enc_keyid(rule, &match);
8579 		if (match.mask->keyid != 0)
8580 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8581 
8582 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8583 	}
8584 
8585 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8586 		struct flow_match_basic match;
8587 
8588 		flow_rule_match_basic(rule, &match);
8589 		n_proto_key = ntohs(match.key->n_proto);
8590 		n_proto_mask = ntohs(match.mask->n_proto);
8591 
8592 		if (n_proto_key == ETH_P_ALL) {
8593 			n_proto_key = 0;
8594 			n_proto_mask = 0;
8595 		}
8596 		filter->n_proto = n_proto_key & n_proto_mask;
8597 		filter->ip_proto = match.key->ip_proto;
8598 	}
8599 
8600 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8601 		struct flow_match_eth_addrs match;
8602 
8603 		flow_rule_match_eth_addrs(rule, &match);
8604 
8605 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8606 		if (!is_zero_ether_addr(match.mask->dst)) {
8607 			if (is_broadcast_ether_addr(match.mask->dst)) {
8608 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8609 			} else {
8610 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8611 					match.mask->dst);
8612 				return -EIO;
8613 			}
8614 		}
8615 
8616 		if (!is_zero_ether_addr(match.mask->src)) {
8617 			if (is_broadcast_ether_addr(match.mask->src)) {
8618 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8619 			} else {
8620 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8621 					match.mask->src);
8622 				return -EIO;
8623 			}
8624 		}
8625 		ether_addr_copy(filter->dst_mac, match.key->dst);
8626 		ether_addr_copy(filter->src_mac, match.key->src);
8627 	}
8628 
8629 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8630 		struct flow_match_vlan match;
8631 
8632 		flow_rule_match_vlan(rule, &match);
8633 		if (match.mask->vlan_id) {
8634 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8635 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8636 
8637 			} else {
8638 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8639 					match.mask->vlan_id);
8640 				return -EIO;
8641 			}
8642 		}
8643 
8644 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8645 	}
8646 
8647 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8648 		struct flow_match_control match;
8649 
8650 		flow_rule_match_control(rule, &match);
8651 		addr_type = match.key->addr_type;
8652 
8653 		if (flow_rule_has_control_flags(match.mask->flags,
8654 						f->common.extack))
8655 			return -EOPNOTSUPP;
8656 	}
8657 
8658 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8659 		struct flow_match_ipv4_addrs match;
8660 
8661 		flow_rule_match_ipv4_addrs(rule, &match);
8662 		if (match.mask->dst) {
8663 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8664 				field_flags |= I40E_CLOUD_FIELD_IIP;
8665 			} else {
8666 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8667 					&match.mask->dst);
8668 				return -EIO;
8669 			}
8670 		}
8671 
8672 		if (match.mask->src) {
8673 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8674 				field_flags |= I40E_CLOUD_FIELD_IIP;
8675 			} else {
8676 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8677 					&match.mask->src);
8678 				return -EIO;
8679 			}
8680 		}
8681 
8682 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8683 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8684 			return -EIO;
8685 		}
8686 		filter->dst_ipv4 = match.key->dst;
8687 		filter->src_ipv4 = match.key->src;
8688 	}
8689 
8690 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8691 		struct flow_match_ipv6_addrs match;
8692 
8693 		flow_rule_match_ipv6_addrs(rule, &match);
8694 
8695 		/* src and dest IPV6 address should not be LOOPBACK
8696 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8697 		 */
8698 		if (ipv6_addr_loopback(&match.key->dst) ||
8699 		    ipv6_addr_loopback(&match.key->src)) {
8700 			dev_err(&pf->pdev->dev,
8701 				"Bad ipv6, addr is LOOPBACK\n");
8702 			return -EIO;
8703 		}
8704 		if (!ipv6_addr_any(&match.mask->dst) ||
8705 		    !ipv6_addr_any(&match.mask->src))
8706 			field_flags |= I40E_CLOUD_FIELD_IIP;
8707 
8708 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8709 		       sizeof(filter->src_ipv6));
8710 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8711 		       sizeof(filter->dst_ipv6));
8712 	}
8713 
8714 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8715 		struct flow_match_ports match;
8716 
8717 		flow_rule_match_ports(rule, &match);
8718 		if (match.mask->src) {
8719 			if (match.mask->src == cpu_to_be16(0xffff)) {
8720 				field_flags |= I40E_CLOUD_FIELD_IIP;
8721 			} else {
8722 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8723 					be16_to_cpu(match.mask->src));
8724 				return -EIO;
8725 			}
8726 		}
8727 
8728 		if (match.mask->dst) {
8729 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8730 				field_flags |= I40E_CLOUD_FIELD_IIP;
8731 			} else {
8732 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8733 					be16_to_cpu(match.mask->dst));
8734 				return -EIO;
8735 			}
8736 		}
8737 
8738 		filter->dst_port = match.key->dst;
8739 		filter->src_port = match.key->src;
8740 
8741 		switch (filter->ip_proto) {
8742 		case IPPROTO_TCP:
8743 		case IPPROTO_UDP:
8744 			break;
8745 		default:
8746 			dev_err(&pf->pdev->dev,
8747 				"Only UDP and TCP transport are supported\n");
8748 			return -EINVAL;
8749 		}
8750 	}
8751 	filter->flags = field_flags;
8752 	return 0;
8753 }
8754 
8755 /**
8756  * i40e_handle_tclass: Forward to a traffic class on the device
8757  * @vsi: Pointer to VSI
8758  * @tc: traffic class index on the device
8759  * @filter: Pointer to cloud filter structure
8760  *
8761  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8762 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8763 			      struct i40e_cloud_filter *filter)
8764 {
8765 	struct i40e_channel *ch, *ch_tmp;
8766 
8767 	/* direct to a traffic class on the same device */
8768 	if (tc == 0) {
8769 		filter->seid = vsi->seid;
8770 		return 0;
8771 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8772 		if (!filter->dst_port) {
8773 			dev_err(&vsi->back->pdev->dev,
8774 				"Specify destination port to direct to traffic class that is not default\n");
8775 			return -EINVAL;
8776 		}
8777 		if (list_empty(&vsi->ch_list))
8778 			return -EINVAL;
8779 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8780 					 list) {
8781 			if (ch->seid == vsi->tc_seid_map[tc])
8782 				filter->seid = ch->seid;
8783 		}
8784 		return 0;
8785 	}
8786 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8787 	return -EINVAL;
8788 }
8789 
8790 /**
8791  * i40e_configure_clsflower - Configure tc flower filters
8792  * @vsi: Pointer to VSI
8793  * @cls_flower: Pointer to struct flow_cls_offload
8794  *
8795  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8796 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8797 				    struct flow_cls_offload *cls_flower)
8798 {
8799 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8800 	struct i40e_cloud_filter *filter = NULL;
8801 	struct i40e_pf *pf = vsi->back;
8802 	int err = 0;
8803 
8804 	if (tc < 0) {
8805 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8806 		return -EOPNOTSUPP;
8807 	}
8808 
8809 	if (!tc) {
8810 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8811 		return -EINVAL;
8812 	}
8813 
8814 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8815 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8816 		return -EBUSY;
8817 
8818 	if (pf->fdir_pf_active_filters ||
8819 	    (!hlist_empty(&pf->fdir_filter_list))) {
8820 		dev_err(&vsi->back->pdev->dev,
8821 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8822 		return -EINVAL;
8823 	}
8824 
8825 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
8826 		dev_err(&vsi->back->pdev->dev,
8827 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8828 		clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
8829 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
8830 	}
8831 
8832 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8833 	if (!filter)
8834 		return -ENOMEM;
8835 
8836 	filter->cookie = cls_flower->cookie;
8837 
8838 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8839 	if (err < 0)
8840 		goto err;
8841 
8842 	err = i40e_handle_tclass(vsi, tc, filter);
8843 	if (err < 0)
8844 		goto err;
8845 
8846 	/* Add cloud filter */
8847 	if (filter->dst_port)
8848 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8849 	else
8850 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8851 
8852 	if (err) {
8853 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8854 			err);
8855 		goto err;
8856 	}
8857 
8858 	/* add filter to the ordered list */
8859 	INIT_HLIST_NODE(&filter->cloud_node);
8860 
8861 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8862 
8863 	pf->num_cloud_filters++;
8864 
8865 	return err;
8866 err:
8867 	kfree(filter);
8868 	return err;
8869 }
8870 
8871 /**
8872  * i40e_find_cloud_filter - Find the could filter in the list
8873  * @vsi: Pointer to VSI
8874  * @cookie: filter specific cookie
8875  *
8876  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8877 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8878 							unsigned long *cookie)
8879 {
8880 	struct i40e_cloud_filter *filter = NULL;
8881 	struct hlist_node *node2;
8882 
8883 	hlist_for_each_entry_safe(filter, node2,
8884 				  &vsi->back->cloud_filter_list, cloud_node)
8885 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8886 			return filter;
8887 	return NULL;
8888 }
8889 
8890 /**
8891  * i40e_delete_clsflower - Remove tc flower filters
8892  * @vsi: Pointer to VSI
8893  * @cls_flower: Pointer to struct flow_cls_offload
8894  *
8895  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8896 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8897 				 struct flow_cls_offload *cls_flower)
8898 {
8899 	struct i40e_cloud_filter *filter = NULL;
8900 	struct i40e_pf *pf = vsi->back;
8901 	int err = 0;
8902 
8903 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8904 
8905 	if (!filter)
8906 		return -EINVAL;
8907 
8908 	hash_del(&filter->cloud_node);
8909 
8910 	if (filter->dst_port)
8911 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8912 	else
8913 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8914 
8915 	kfree(filter);
8916 	if (err) {
8917 		dev_err(&pf->pdev->dev,
8918 			"Failed to delete cloud filter, err %pe\n",
8919 			ERR_PTR(err));
8920 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8921 	}
8922 
8923 	pf->num_cloud_filters--;
8924 	if (!pf->num_cloud_filters)
8925 		if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
8926 		    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
8927 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
8928 			clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
8929 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
8930 		}
8931 	return 0;
8932 }
8933 
8934 /**
8935  * i40e_setup_tc_cls_flower - flower classifier offloads
8936  * @np: net device to configure
8937  * @cls_flower: offload data
8938  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8939 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8940 				    struct flow_cls_offload *cls_flower)
8941 {
8942 	struct i40e_vsi *vsi = np->vsi;
8943 
8944 	switch (cls_flower->command) {
8945 	case FLOW_CLS_REPLACE:
8946 		return i40e_configure_clsflower(vsi, cls_flower);
8947 	case FLOW_CLS_DESTROY:
8948 		return i40e_delete_clsflower(vsi, cls_flower);
8949 	case FLOW_CLS_STATS:
8950 		return -EOPNOTSUPP;
8951 	default:
8952 		return -EOPNOTSUPP;
8953 	}
8954 }
8955 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8956 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8957 				  void *cb_priv)
8958 {
8959 	struct i40e_netdev_priv *np = cb_priv;
8960 
8961 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8962 		return -EOPNOTSUPP;
8963 
8964 	switch (type) {
8965 	case TC_SETUP_CLSFLOWER:
8966 		return i40e_setup_tc_cls_flower(np, type_data);
8967 
8968 	default:
8969 		return -EOPNOTSUPP;
8970 	}
8971 }
8972 
8973 static LIST_HEAD(i40e_block_cb_list);
8974 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8975 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8976 			   void *type_data)
8977 {
8978 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8979 
8980 	switch (type) {
8981 	case TC_SETUP_QDISC_MQPRIO:
8982 		return i40e_setup_tc(netdev, type_data);
8983 	case TC_SETUP_BLOCK:
8984 		return flow_block_cb_setup_simple(type_data,
8985 						  &i40e_block_cb_list,
8986 						  i40e_setup_tc_block_cb,
8987 						  np, np, true);
8988 	default:
8989 		return -EOPNOTSUPP;
8990 	}
8991 }
8992 
8993 /**
8994  * i40e_open - Called when a network interface is made active
8995  * @netdev: network interface device structure
8996  *
8997  * The open entry point is called when a network interface is made
8998  * active by the system (IFF_UP).  At this point all resources needed
8999  * for transmit and receive operations are allocated, the interrupt
9000  * handler is registered with the OS, the netdev watchdog subtask is
9001  * enabled, and the stack is notified that the interface is ready.
9002  *
9003  * Returns 0 on success, negative value on failure
9004  **/
i40e_open(struct net_device * netdev)9005 int i40e_open(struct net_device *netdev)
9006 {
9007 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9008 	struct i40e_vsi *vsi = np->vsi;
9009 	struct i40e_pf *pf = vsi->back;
9010 	int err;
9011 
9012 	/* disallow open during test or if eeprom is broken */
9013 	if (test_bit(__I40E_TESTING, pf->state) ||
9014 	    test_bit(__I40E_BAD_EEPROM, pf->state))
9015 		return -EBUSY;
9016 
9017 	netif_carrier_off(netdev);
9018 
9019 	if (i40e_force_link_state(pf, true))
9020 		return -EAGAIN;
9021 
9022 	err = i40e_vsi_open(vsi);
9023 	if (err)
9024 		return err;
9025 
9026 	/* configure global TSO hardware offload settings */
9027 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9028 						       TCP_FLAG_FIN) >> 16);
9029 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9030 						       TCP_FLAG_FIN |
9031 						       TCP_FLAG_CWR) >> 16);
9032 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9033 	udp_tunnel_get_rx_info(netdev);
9034 
9035 	return 0;
9036 }
9037 
9038 /**
9039  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9040  * @vsi: vsi structure
9041  *
9042  * This updates netdev's number of tx/rx queues
9043  *
9044  * Returns status of setting tx/rx queues
9045  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)9046 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9047 {
9048 	int ret;
9049 
9050 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9051 					   vsi->num_queue_pairs);
9052 	if (ret)
9053 		return ret;
9054 
9055 	return netif_set_real_num_tx_queues(vsi->netdev,
9056 					    vsi->num_queue_pairs);
9057 }
9058 
9059 /**
9060  * i40e_vsi_open -
9061  * @vsi: the VSI to open
9062  *
9063  * Finish initialization of the VSI.
9064  *
9065  * Returns 0 on success, negative value on failure
9066  *
9067  * Note: expects to be called while under rtnl_lock()
9068  **/
i40e_vsi_open(struct i40e_vsi * vsi)9069 int i40e_vsi_open(struct i40e_vsi *vsi)
9070 {
9071 	struct i40e_pf *pf = vsi->back;
9072 	char int_name[I40E_INT_NAME_STR_LEN];
9073 	int err;
9074 
9075 	/* allocate descriptors */
9076 	err = i40e_vsi_setup_tx_resources(vsi);
9077 	if (err)
9078 		goto err_setup_tx;
9079 	err = i40e_vsi_setup_rx_resources(vsi);
9080 	if (err)
9081 		goto err_setup_rx;
9082 
9083 	err = i40e_vsi_configure(vsi);
9084 	if (err)
9085 		goto err_setup_rx;
9086 
9087 	if (vsi->netdev) {
9088 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9089 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9090 		err = i40e_vsi_request_irq(vsi, int_name);
9091 		if (err)
9092 			goto err_setup_rx;
9093 
9094 		/* Notify the stack of the actual queue counts. */
9095 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9096 		if (err)
9097 			goto err_set_queues;
9098 
9099 	} else if (vsi->type == I40E_VSI_FDIR) {
9100 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9101 			 dev_driver_string(&pf->pdev->dev),
9102 			 dev_name(&pf->pdev->dev));
9103 		err = i40e_vsi_request_irq(vsi, int_name);
9104 		if (err)
9105 			goto err_setup_rx;
9106 
9107 	} else {
9108 		err = -EINVAL;
9109 		goto err_setup_rx;
9110 	}
9111 
9112 	err = i40e_up_complete(vsi);
9113 	if (err)
9114 		goto err_up_complete;
9115 
9116 	return 0;
9117 
9118 err_up_complete:
9119 	i40e_down(vsi);
9120 err_set_queues:
9121 	i40e_vsi_free_irq(vsi);
9122 err_setup_rx:
9123 	i40e_vsi_free_rx_resources(vsi);
9124 err_setup_tx:
9125 	i40e_vsi_free_tx_resources(vsi);
9126 	if (vsi->type == I40E_VSI_MAIN)
9127 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9128 
9129 	return err;
9130 }
9131 
9132 /**
9133  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9134  * @pf: Pointer to PF
9135  *
9136  * This function destroys the hlist where all the Flow Director
9137  * filters were saved.
9138  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)9139 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9140 {
9141 	struct i40e_fdir_filter *filter;
9142 	struct i40e_flex_pit *pit_entry, *tmp;
9143 	struct hlist_node *node2;
9144 
9145 	hlist_for_each_entry_safe(filter, node2,
9146 				  &pf->fdir_filter_list, fdir_node) {
9147 		hlist_del(&filter->fdir_node);
9148 		kfree(filter);
9149 	}
9150 
9151 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9152 		list_del(&pit_entry->list);
9153 		kfree(pit_entry);
9154 	}
9155 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9156 
9157 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9158 		list_del(&pit_entry->list);
9159 		kfree(pit_entry);
9160 	}
9161 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9162 
9163 	pf->fdir_pf_active_filters = 0;
9164 	i40e_reset_fdir_filter_cnt(pf);
9165 
9166 	/* Reprogram the default input set for TCP/IPv4 */
9167 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9168 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9169 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9170 
9171 	/* Reprogram the default input set for TCP/IPv6 */
9172 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_TCP,
9173 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9174 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9175 
9176 	/* Reprogram the default input set for UDP/IPv4 */
9177 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_UDP,
9178 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9179 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9180 
9181 	/* Reprogram the default input set for UDP/IPv6 */
9182 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_UDP,
9183 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9184 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9185 
9186 	/* Reprogram the default input set for SCTP/IPv4 */
9187 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_SCTP,
9188 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9189 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9190 
9191 	/* Reprogram the default input set for SCTP/IPv6 */
9192 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_SCTP,
9193 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9194 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9195 
9196 	/* Reprogram the default input set for Other/IPv4 */
9197 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_OTHER,
9198 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9199 
9200 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_FRAG_IPV4,
9201 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9202 
9203 	/* Reprogram the default input set for Other/IPv6 */
9204 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_OTHER,
9205 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9206 
9207 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_FRAG_IPV6,
9208 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9209 }
9210 
9211 /**
9212  * i40e_cloud_filter_exit - Cleans up the cloud filters
9213  * @pf: Pointer to PF
9214  *
9215  * This function destroys the hlist where all the cloud filters
9216  * were saved.
9217  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)9218 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9219 {
9220 	struct i40e_cloud_filter *cfilter;
9221 	struct hlist_node *node;
9222 
9223 	hlist_for_each_entry_safe(cfilter, node,
9224 				  &pf->cloud_filter_list, cloud_node) {
9225 		hlist_del(&cfilter->cloud_node);
9226 		kfree(cfilter);
9227 	}
9228 	pf->num_cloud_filters = 0;
9229 
9230 	if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
9231 	    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
9232 		set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
9233 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
9234 		clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
9235 	}
9236 }
9237 
9238 /**
9239  * i40e_close - Disables a network interface
9240  * @netdev: network interface device structure
9241  *
9242  * The close entry point is called when an interface is de-activated
9243  * by the OS.  The hardware is still under the driver's control, but
9244  * this netdev interface is disabled.
9245  *
9246  * Returns 0, this is not allowed to fail
9247  **/
i40e_close(struct net_device * netdev)9248 int i40e_close(struct net_device *netdev)
9249 {
9250 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9251 	struct i40e_vsi *vsi = np->vsi;
9252 
9253 	i40e_vsi_close(vsi);
9254 
9255 	return 0;
9256 }
9257 
9258 /**
9259  * i40e_do_reset - Start a PF or Core Reset sequence
9260  * @pf: board private structure
9261  * @reset_flags: which reset is requested
9262  * @lock_acquired: indicates whether or not the lock has been acquired
9263  * before this function was called.
9264  *
9265  * The essential difference in resets is that the PF Reset
9266  * doesn't clear the packet buffers, doesn't reset the PE
9267  * firmware, and doesn't bother the other PFs on the chip.
9268  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)9269 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9270 {
9271 	struct i40e_vsi *vsi;
9272 	u32 val;
9273 	int i;
9274 
9275 	/* do the biggest reset indicated */
9276 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9277 
9278 		/* Request a Global Reset
9279 		 *
9280 		 * This will start the chip's countdown to the actual full
9281 		 * chip reset event, and a warning interrupt to be sent
9282 		 * to all PFs, including the requestor.  Our handler
9283 		 * for the warning interrupt will deal with the shutdown
9284 		 * and recovery of the switch setup.
9285 		 */
9286 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9287 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9288 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9289 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9290 
9291 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9292 
9293 		/* Request a Core Reset
9294 		 *
9295 		 * Same as Global Reset, except does *not* include the MAC/PHY
9296 		 */
9297 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9298 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9299 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9300 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9301 		i40e_flush(&pf->hw);
9302 
9303 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9304 
9305 		/* Request a PF Reset
9306 		 *
9307 		 * Resets only the PF-specific registers
9308 		 *
9309 		 * This goes directly to the tear-down and rebuild of
9310 		 * the switch, since we need to do all the recovery as
9311 		 * for the Core Reset.
9312 		 */
9313 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9314 		i40e_handle_reset_warning(pf, lock_acquired);
9315 
9316 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9317 		/* Request a PF Reset
9318 		 *
9319 		 * Resets PF and reinitializes PFs VSI.
9320 		 */
9321 		i40e_prep_for_reset(pf);
9322 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9323 		dev_info(&pf->pdev->dev,
9324 			 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
9325 			 "FW LLDP is disabled\n" :
9326 			 "FW LLDP is enabled\n");
9327 
9328 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9329 		/* Find the VSI(s) that requested a re-init */
9330 		dev_info(&pf->pdev->dev, "VSI reinit requested\n");
9331 
9332 		i40e_pf_for_each_vsi(pf, i, vsi) {
9333 			if (test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9334 					       vsi->state))
9335 				i40e_vsi_reinit_locked(vsi);
9336 		}
9337 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9338 		/* Find the VSI(s) that needs to be brought down */
9339 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9340 
9341 		i40e_pf_for_each_vsi(pf, i, vsi) {
9342 			if (test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9343 					       vsi->state)) {
9344 				set_bit(__I40E_VSI_DOWN, vsi->state);
9345 				i40e_down(vsi);
9346 			}
9347 		}
9348 	} else {
9349 		dev_info(&pf->pdev->dev,
9350 			 "bad reset request 0x%08x\n", reset_flags);
9351 	}
9352 }
9353 
9354 #ifdef CONFIG_I40E_DCB
9355 /**
9356  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9357  * @pf: board private structure
9358  * @old_cfg: current DCB config
9359  * @new_cfg: new DCB config
9360  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)9361 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9362 			    struct i40e_dcbx_config *old_cfg,
9363 			    struct i40e_dcbx_config *new_cfg)
9364 {
9365 	bool need_reconfig = false;
9366 
9367 	/* Check if ETS configuration has changed */
9368 	if (memcmp(&new_cfg->etscfg,
9369 		   &old_cfg->etscfg,
9370 		   sizeof(new_cfg->etscfg))) {
9371 		/* If Priority Table has changed reconfig is needed */
9372 		if (memcmp(&new_cfg->etscfg.prioritytable,
9373 			   &old_cfg->etscfg.prioritytable,
9374 			   sizeof(new_cfg->etscfg.prioritytable))) {
9375 			need_reconfig = true;
9376 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9377 		}
9378 
9379 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9380 			   &old_cfg->etscfg.tcbwtable,
9381 			   sizeof(new_cfg->etscfg.tcbwtable)))
9382 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9383 
9384 		if (memcmp(&new_cfg->etscfg.tsatable,
9385 			   &old_cfg->etscfg.tsatable,
9386 			   sizeof(new_cfg->etscfg.tsatable)))
9387 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9388 	}
9389 
9390 	/* Check if PFC configuration has changed */
9391 	if (memcmp(&new_cfg->pfc,
9392 		   &old_cfg->pfc,
9393 		   sizeof(new_cfg->pfc))) {
9394 		need_reconfig = true;
9395 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9396 	}
9397 
9398 	/* Check if APP Table has changed */
9399 	if (memcmp(&new_cfg->app,
9400 		   &old_cfg->app,
9401 		   sizeof(new_cfg->app))) {
9402 		need_reconfig = true;
9403 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9404 	}
9405 
9406 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9407 	return need_reconfig;
9408 }
9409 
9410 /**
9411  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9412  * @pf: board private structure
9413  * @e: event info posted on ARQ
9414  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9415 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9416 				  struct i40e_arq_event_info *e)
9417 {
9418 	struct i40e_aqc_lldp_get_mib *mib = libie_aq_raw(&e->desc);
9419 	struct i40e_hw *hw = &pf->hw;
9420 	struct i40e_dcbx_config tmp_dcbx_cfg;
9421 	bool need_reconfig = false;
9422 	int ret = 0;
9423 	u8 type;
9424 
9425 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9426 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9427 	    (hw->phy.link_info.link_speed &
9428 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9429 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9430 		/* let firmware decide if the DCB should be disabled */
9431 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9432 
9433 	/* Not DCB capable or capability disabled */
9434 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9435 		return ret;
9436 
9437 	/* Ignore if event is not for Nearest Bridge */
9438 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9439 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9440 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9441 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9442 		return ret;
9443 
9444 	/* Check MIB Type and return if event for Remote MIB update */
9445 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9446 	dev_dbg(&pf->pdev->dev,
9447 		"LLDP event mib type %s\n", type ? "remote" : "local");
9448 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9449 		/* Update the remote cached instance and return */
9450 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9451 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9452 				&hw->remote_dcbx_config);
9453 		goto exit;
9454 	}
9455 
9456 	/* Store the old configuration */
9457 	tmp_dcbx_cfg = hw->local_dcbx_config;
9458 
9459 	/* Reset the old DCBx configuration data */
9460 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9461 	/* Get updated DCBX data from firmware */
9462 	ret = i40e_get_dcb_config(&pf->hw);
9463 	if (ret) {
9464 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9465 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9466 		    (hw->phy.link_info.link_speed &
9467 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9468 			dev_warn(&pf->pdev->dev,
9469 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9470 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9471 		} else {
9472 			dev_info(&pf->pdev->dev,
9473 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9474 				 ERR_PTR(ret),
9475 				 libie_aq_str(pf->hw.aq.asq_last_status));
9476 		}
9477 		goto exit;
9478 	}
9479 
9480 	/* No change detected in DCBX configs */
9481 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9482 		    sizeof(tmp_dcbx_cfg))) {
9483 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9484 		goto exit;
9485 	}
9486 
9487 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9488 					       &hw->local_dcbx_config);
9489 
9490 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9491 
9492 	if (!need_reconfig)
9493 		goto exit;
9494 
9495 	/* Enable DCB tagging only when more than one TC */
9496 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9497 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9498 	else
9499 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9500 
9501 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9502 	/* Reconfiguration needed quiesce all VSIs */
9503 	i40e_pf_quiesce_all_vsi(pf);
9504 
9505 	/* Changes in configuration update VEB/VSI */
9506 	i40e_dcb_reconfigure(pf);
9507 
9508 	ret = i40e_resume_port_tx(pf);
9509 
9510 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9511 	/* In case of error no point in resuming VSIs */
9512 	if (ret)
9513 		goto exit;
9514 
9515 	/* Wait for the PF's queues to be disabled */
9516 	ret = i40e_pf_wait_queues_disabled(pf);
9517 	if (ret) {
9518 		/* Schedule PF reset to recover */
9519 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9520 		i40e_service_event_schedule(pf);
9521 	} else {
9522 		i40e_pf_unquiesce_all_vsi(pf);
9523 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9524 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9525 	}
9526 
9527 exit:
9528 	return ret;
9529 }
9530 #endif /* CONFIG_I40E_DCB */
9531 
9532 /**
9533  * i40e_do_reset_safe - Protected reset path for userland calls.
9534  * @pf: board private structure
9535  * @reset_flags: which reset is requested
9536  *
9537  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)9538 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9539 {
9540 	rtnl_lock();
9541 	i40e_do_reset(pf, reset_flags, true);
9542 	rtnl_unlock();
9543 }
9544 
9545 /**
9546  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9547  * @pf: board private structure
9548  * @e: event info posted on ARQ
9549  *
9550  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9551  * and VF queues
9552  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9553 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9554 					   struct i40e_arq_event_info *e)
9555 {
9556 	struct i40e_aqc_lan_overflow *data = libie_aq_raw(&e->desc);
9557 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9558 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9559 	struct i40e_hw *hw = &pf->hw;
9560 	struct i40e_vf *vf;
9561 	u16 vf_id;
9562 
9563 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9564 		queue, qtx_ctl);
9565 
9566 	if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
9567 	    I40E_QTX_CTL_VF_QUEUE)
9568 		return;
9569 
9570 	/* Queue belongs to VF, find the VF and issue VF reset */
9571 	vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
9572 	vf_id -= hw->func_caps.vf_base_id;
9573 	vf = &pf->vf[vf_id];
9574 	i40e_vc_notify_vf_reset(vf);
9575 	/* Allow VF to process pending reset notification */
9576 	msleep(20);
9577 	i40e_reset_vf(vf, false);
9578 }
9579 
9580 /**
9581  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9582  * @pf: board private structure
9583  **/
i40e_get_current_fd_count(struct i40e_pf * pf)9584 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9585 {
9586 	u32 val, fcnt_prog;
9587 
9588 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9589 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9590 		    FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
9591 	return fcnt_prog;
9592 }
9593 
9594 /**
9595  * i40e_get_global_fd_count - Get total FD filters programmed on device
9596  * @pf: board private structure
9597  **/
i40e_get_global_fd_count(struct i40e_pf * pf)9598 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9599 {
9600 	u32 val, fcnt_prog;
9601 
9602 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9603 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9604 		    FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
9605 	return fcnt_prog;
9606 }
9607 
9608 /**
9609  * i40e_reenable_fdir_sb - Restore FDir SB capability
9610  * @pf: board private structure
9611  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)9612 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9613 {
9614 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9615 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9616 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9617 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9618 }
9619 
9620 /**
9621  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9622  * @pf: board private structure
9623  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)9624 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9625 {
9626 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9627 		/* ATR uses the same filtering logic as SB rules. It only
9628 		 * functions properly if the input set mask is at the default
9629 		 * settings. It is safe to restore the default input set
9630 		 * because there are no active TCPv4 filter rules.
9631 		 */
9632 		i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9633 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9634 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9635 
9636 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9637 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9638 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9639 	}
9640 }
9641 
9642 /**
9643  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9644  * @pf: board private structure
9645  * @filter: FDir filter to remove
9646  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)9647 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9648 				       struct i40e_fdir_filter *filter)
9649 {
9650 	/* Update counters */
9651 	pf->fdir_pf_active_filters--;
9652 	pf->fd_inv = 0;
9653 
9654 	switch (filter->flow_type) {
9655 	case TCP_V4_FLOW:
9656 		pf->fd_tcp4_filter_cnt--;
9657 		break;
9658 	case UDP_V4_FLOW:
9659 		pf->fd_udp4_filter_cnt--;
9660 		break;
9661 	case SCTP_V4_FLOW:
9662 		pf->fd_sctp4_filter_cnt--;
9663 		break;
9664 	case TCP_V6_FLOW:
9665 		pf->fd_tcp6_filter_cnt--;
9666 		break;
9667 	case UDP_V6_FLOW:
9668 		pf->fd_udp6_filter_cnt--;
9669 		break;
9670 	case SCTP_V6_FLOW:
9671 		pf->fd_udp6_filter_cnt--;
9672 		break;
9673 	case IP_USER_FLOW:
9674 		switch (filter->ipl4_proto) {
9675 		case IPPROTO_TCP:
9676 			pf->fd_tcp4_filter_cnt--;
9677 			break;
9678 		case IPPROTO_UDP:
9679 			pf->fd_udp4_filter_cnt--;
9680 			break;
9681 		case IPPROTO_SCTP:
9682 			pf->fd_sctp4_filter_cnt--;
9683 			break;
9684 		case IPPROTO_IP:
9685 			pf->fd_ip4_filter_cnt--;
9686 			break;
9687 		}
9688 		break;
9689 	case IPV6_USER_FLOW:
9690 		switch (filter->ipl4_proto) {
9691 		case IPPROTO_TCP:
9692 			pf->fd_tcp6_filter_cnt--;
9693 			break;
9694 		case IPPROTO_UDP:
9695 			pf->fd_udp6_filter_cnt--;
9696 			break;
9697 		case IPPROTO_SCTP:
9698 			pf->fd_sctp6_filter_cnt--;
9699 			break;
9700 		case IPPROTO_IP:
9701 			pf->fd_ip6_filter_cnt--;
9702 			break;
9703 		}
9704 		break;
9705 	}
9706 
9707 	/* Remove the filter from the list and free memory */
9708 	hlist_del(&filter->fdir_node);
9709 	kfree(filter);
9710 }
9711 
9712 /**
9713  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9714  * @pf: board private structure
9715  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9716 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9717 {
9718 	struct i40e_fdir_filter *filter;
9719 	u32 fcnt_prog, fcnt_avail;
9720 	struct hlist_node *node;
9721 
9722 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9723 		return;
9724 
9725 	/* Check if we have enough room to re-enable FDir SB capability. */
9726 	fcnt_prog = i40e_get_global_fd_count(pf);
9727 	fcnt_avail = pf->fdir_pf_filter_count;
9728 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9729 	    (pf->fd_add_err == 0) ||
9730 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9731 		i40e_reenable_fdir_sb(pf);
9732 
9733 	/* We should wait for even more space before re-enabling ATR.
9734 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9735 	 * rules active.
9736 	 */
9737 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9738 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9739 		i40e_reenable_fdir_atr(pf);
9740 
9741 	/* if hw had a problem adding a filter, delete it */
9742 	if (pf->fd_inv > 0) {
9743 		hlist_for_each_entry_safe(filter, node,
9744 					  &pf->fdir_filter_list, fdir_node)
9745 			if (filter->fd_id == pf->fd_inv)
9746 				i40e_delete_invalid_filter(pf, filter);
9747 	}
9748 }
9749 
9750 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9751 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9752 /**
9753  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9754  * @pf: board private structure
9755  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9756 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9757 {
9758 	unsigned long min_flush_time;
9759 	int flush_wait_retry = 50;
9760 	bool disable_atr = false;
9761 	int fd_room;
9762 	int reg;
9763 
9764 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9765 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9766 		return;
9767 
9768 	/* If the flush is happening too quick and we have mostly SB rules we
9769 	 * should not re-enable ATR for some time.
9770 	 */
9771 	min_flush_time = pf->fd_flush_timestamp +
9772 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9773 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9774 
9775 	if (!(time_after(jiffies, min_flush_time)) &&
9776 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9777 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9778 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9779 		disable_atr = true;
9780 	}
9781 
9782 	pf->fd_flush_timestamp = jiffies;
9783 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9784 	/* flush all filters */
9785 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9786 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9787 	i40e_flush(&pf->hw);
9788 	pf->fd_flush_cnt++;
9789 	pf->fd_add_err = 0;
9790 	do {
9791 		/* Check FD flush status every 5-6msec */
9792 		usleep_range(5000, 6000);
9793 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9794 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9795 			break;
9796 	} while (flush_wait_retry--);
9797 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9798 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9799 	} else {
9800 		/* replay sideband filters */
9801 		i40e_fdir_filter_restore(i40e_pf_get_main_vsi(pf));
9802 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9803 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9804 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9805 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9806 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9807 	}
9808 }
9809 
9810 /**
9811  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9812  * @pf: board private structure
9813  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9814 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9815 {
9816 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9817 }
9818 
9819 /**
9820  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9821  * @pf: board private structure
9822  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9823 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9824 {
9825 
9826 	/* if interface is down do nothing */
9827 	if (test_bit(__I40E_DOWN, pf->state))
9828 		return;
9829 
9830 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9831 		i40e_fdir_flush_and_replay(pf);
9832 
9833 	i40e_fdir_check_and_reenable(pf);
9834 
9835 }
9836 
9837 /**
9838  * i40e_vsi_link_event - notify VSI of a link event
9839  * @vsi: vsi to be notified
9840  * @link_up: link up or down
9841  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9842 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9843 {
9844 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9845 		return;
9846 
9847 	switch (vsi->type) {
9848 	case I40E_VSI_MAIN:
9849 		if (!vsi->netdev || !vsi->netdev_registered)
9850 			break;
9851 
9852 		if (link_up) {
9853 			netif_carrier_on(vsi->netdev);
9854 			netif_tx_wake_all_queues(vsi->netdev);
9855 		} else {
9856 			netif_carrier_off(vsi->netdev);
9857 			netif_tx_stop_all_queues(vsi->netdev);
9858 		}
9859 		break;
9860 
9861 	case I40E_VSI_SRIOV:
9862 	case I40E_VSI_VMDQ2:
9863 	case I40E_VSI_CTRL:
9864 	case I40E_VSI_IWARP:
9865 	case I40E_VSI_MIRROR:
9866 	default:
9867 		/* there is no notification for other VSIs */
9868 		break;
9869 	}
9870 }
9871 
9872 /**
9873  * i40e_veb_link_event - notify elements on the veb of a link event
9874  * @veb: veb to be notified
9875  * @link_up: link up or down
9876  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9877 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9878 {
9879 	struct i40e_vsi *vsi;
9880 	struct i40e_pf *pf;
9881 	int i;
9882 
9883 	if (!veb || !veb->pf)
9884 		return;
9885 	pf = veb->pf;
9886 
9887 	/* Send link event to contained VSIs */
9888 	i40e_pf_for_each_vsi(pf, i, vsi)
9889 		if (vsi->uplink_seid == veb->seid)
9890 			i40e_vsi_link_event(vsi, link_up);
9891 }
9892 
9893 /**
9894  * i40e_link_event - Update netif_carrier status
9895  * @pf: board private structure
9896  **/
i40e_link_event(struct i40e_pf * pf)9897 static void i40e_link_event(struct i40e_pf *pf)
9898 {
9899 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
9900 	struct i40e_veb *veb = i40e_pf_get_main_veb(pf);
9901 	u8 new_link_speed, old_link_speed;
9902 	bool new_link, old_link;
9903 	int status;
9904 #ifdef CONFIG_I40E_DCB
9905 	int err;
9906 #endif /* CONFIG_I40E_DCB */
9907 
9908 	/* set this to force the get_link_status call to refresh state */
9909 	pf->hw.phy.get_link_info = true;
9910 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9911 	status = i40e_get_link_status(&pf->hw, &new_link);
9912 
9913 	/* On success, disable temp link polling */
9914 	if (status == 0) {
9915 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9916 	} else {
9917 		/* Enable link polling temporarily until i40e_get_link_status
9918 		 * returns 0
9919 		 */
9920 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9921 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9922 			status);
9923 		return;
9924 	}
9925 
9926 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9927 	new_link_speed = pf->hw.phy.link_info.link_speed;
9928 
9929 	if (new_link == old_link &&
9930 	    new_link_speed == old_link_speed &&
9931 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9932 	     new_link == netif_carrier_ok(vsi->netdev)))
9933 		return;
9934 
9935 	if (!new_link && old_link)
9936 		pf->link_down_events++;
9937 
9938 	i40e_print_link_message(vsi, new_link);
9939 
9940 	/* Notify the base of the switch tree connected to
9941 	 * the link.  Floating VEBs are not notified.
9942 	 */
9943 	if (veb)
9944 		i40e_veb_link_event(veb, new_link);
9945 	else
9946 		i40e_vsi_link_event(vsi, new_link);
9947 
9948 	if (pf->vf)
9949 		i40e_vc_notify_link_state(pf);
9950 
9951 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
9952 		i40e_ptp_set_increment(pf);
9953 #ifdef CONFIG_I40E_DCB
9954 	if (new_link == old_link)
9955 		return;
9956 	/* Not SW DCB so firmware will take care of default settings */
9957 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9958 		return;
9959 
9960 	/* We cover here only link down, as after link up in case of SW DCB
9961 	 * SW LLDP agent will take care of setting it up
9962 	 */
9963 	if (!new_link) {
9964 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9965 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9966 		err = i40e_dcb_sw_default_config(pf);
9967 		if (err) {
9968 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9969 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9970 		} else {
9971 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9972 				       DCB_CAP_DCBX_VER_IEEE;
9973 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9974 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9975 		}
9976 	}
9977 #endif /* CONFIG_I40E_DCB */
9978 }
9979 
9980 /**
9981  * i40e_watchdog_subtask - periodic checks not using event driven response
9982  * @pf: board private structure
9983  **/
i40e_watchdog_subtask(struct i40e_pf * pf)9984 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9985 {
9986 	struct i40e_vsi *vsi;
9987 	struct i40e_veb *veb;
9988 	int i;
9989 
9990 	/* if interface is down do nothing */
9991 	if (test_bit(__I40E_DOWN, pf->state) ||
9992 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9993 		return;
9994 
9995 	/* make sure we don't do these things too often */
9996 	if (time_before(jiffies, (pf->service_timer_previous +
9997 				  pf->service_timer_period)))
9998 		return;
9999 	pf->service_timer_previous = jiffies;
10000 
10001 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
10002 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10003 		i40e_link_event(pf);
10004 
10005 	/* Update the stats for active netdevs so the network stack
10006 	 * can look at updated numbers whenever it cares to
10007 	 */
10008 	i40e_pf_for_each_vsi(pf, i, vsi)
10009 		if (vsi->netdev)
10010 			i40e_update_stats(vsi);
10011 
10012 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
10013 		/* Update the stats for the active switching components */
10014 		i40e_pf_for_each_veb(pf, i, veb)
10015 			i40e_update_veb_stats(veb);
10016 	}
10017 
10018 	i40e_ptp_rx_hang(pf);
10019 	i40e_ptp_tx_hang(pf);
10020 }
10021 
10022 /**
10023  * i40e_reset_subtask - Set up for resetting the device and driver
10024  * @pf: board private structure
10025  **/
i40e_reset_subtask(struct i40e_pf * pf)10026 static void i40e_reset_subtask(struct i40e_pf *pf)
10027 {
10028 	u32 reset_flags = 0;
10029 
10030 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10031 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10032 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10033 	}
10034 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10035 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10036 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10037 	}
10038 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10039 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10040 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10041 	}
10042 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10043 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10044 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10045 	}
10046 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10047 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10048 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10049 	}
10050 
10051 	/* If there's a recovery already waiting, it takes
10052 	 * precedence before starting a new reset sequence.
10053 	 */
10054 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10055 		i40e_prep_for_reset(pf);
10056 		i40e_reset(pf);
10057 		i40e_rebuild(pf, false, false);
10058 	}
10059 
10060 	/* If we're already down or resetting, just bail */
10061 	if (reset_flags &&
10062 	    !test_bit(__I40E_DOWN, pf->state) &&
10063 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10064 		i40e_do_reset(pf, reset_flags, false);
10065 	}
10066 }
10067 
10068 /**
10069  * i40e_handle_link_event - Handle link event
10070  * @pf: board private structure
10071  * @e: event info posted on ARQ
10072  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)10073 static void i40e_handle_link_event(struct i40e_pf *pf,
10074 				   struct i40e_arq_event_info *e)
10075 {
10076 	struct i40e_aqc_get_link_status *status = libie_aq_raw(&e->desc);
10077 
10078 	/* Do a new status request to re-enable LSE reporting
10079 	 * and load new status information into the hw struct
10080 	 * This completely ignores any state information
10081 	 * in the ARQ event info, instead choosing to always
10082 	 * issue the AQ update link status command.
10083 	 */
10084 	i40e_link_event(pf);
10085 
10086 	/* Check if module meets thermal requirements */
10087 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10088 		dev_err(&pf->pdev->dev,
10089 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10090 		dev_err(&pf->pdev->dev,
10091 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10092 	} else {
10093 		/* check for unqualified module, if link is down, suppress
10094 		 * the message if link was forced to be down.
10095 		 */
10096 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10097 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10098 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10099 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10100 			dev_err(&pf->pdev->dev,
10101 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10102 			dev_err(&pf->pdev->dev,
10103 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10104 		}
10105 	}
10106 }
10107 
10108 /**
10109  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10110  * @pf: board private structure
10111  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)10112 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10113 {
10114 	struct i40e_arq_event_info event;
10115 	struct i40e_hw *hw = &pf->hw;
10116 	u16 pending, i = 0;
10117 	u16 opcode;
10118 	u32 oldval;
10119 	int ret;
10120 	u32 val;
10121 
10122 	/* Do not run clean AQ when PF reset fails */
10123 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10124 		return;
10125 
10126 	/* check for error indications */
10127 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10128 	oldval = val;
10129 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10130 		if (hw->debug_mask & I40E_DEBUG_AQ)
10131 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10132 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10133 	}
10134 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10135 		if (hw->debug_mask & I40E_DEBUG_AQ)
10136 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10137 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10138 		pf->arq_overflows++;
10139 	}
10140 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10141 		if (hw->debug_mask & I40E_DEBUG_AQ)
10142 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10143 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10144 	}
10145 	if (oldval != val)
10146 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10147 
10148 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10149 	oldval = val;
10150 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10151 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10152 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10153 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10154 	}
10155 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10156 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10157 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10158 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10159 	}
10160 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10161 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10162 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10163 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10164 	}
10165 	if (oldval != val)
10166 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10167 
10168 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10169 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10170 	if (!event.msg_buf)
10171 		return;
10172 
10173 	do {
10174 		ret = i40e_clean_arq_element(hw, &event, &pending);
10175 		if (ret == -EALREADY)
10176 			break;
10177 		else if (ret) {
10178 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10179 			break;
10180 		}
10181 
10182 		opcode = le16_to_cpu(event.desc.opcode);
10183 		switch (opcode) {
10184 
10185 		case i40e_aqc_opc_get_link_status:
10186 			rtnl_lock();
10187 			i40e_handle_link_event(pf, &event);
10188 			rtnl_unlock();
10189 			break;
10190 		case i40e_aqc_opc_send_msg_to_pf:
10191 			ret = i40e_vc_process_vf_msg(pf,
10192 					le16_to_cpu(event.desc.retval),
10193 					le32_to_cpu(event.desc.cookie_high),
10194 					le32_to_cpu(event.desc.cookie_low),
10195 					event.msg_buf,
10196 					event.msg_len);
10197 			break;
10198 		case i40e_aqc_opc_lldp_update_mib:
10199 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10200 #ifdef CONFIG_I40E_DCB
10201 			rtnl_lock();
10202 			i40e_handle_lldp_event(pf, &event);
10203 			rtnl_unlock();
10204 #endif /* CONFIG_I40E_DCB */
10205 			break;
10206 		case i40e_aqc_opc_event_lan_overflow:
10207 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10208 			i40e_handle_lan_overflow_event(pf, &event);
10209 			break;
10210 		case i40e_aqc_opc_send_msg_to_peer:
10211 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10212 			break;
10213 		case i40e_aqc_opc_nvm_erase:
10214 		case i40e_aqc_opc_nvm_update:
10215 		case i40e_aqc_opc_oem_post_update:
10216 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10217 				   "ARQ NVM operation 0x%04x completed\n",
10218 				   opcode);
10219 			break;
10220 		default:
10221 			dev_info(&pf->pdev->dev,
10222 				 "ARQ: Unknown event 0x%04x ignored\n",
10223 				 opcode);
10224 			break;
10225 		}
10226 	} while (i++ < I40E_AQ_WORK_LIMIT);
10227 
10228 	if (i < I40E_AQ_WORK_LIMIT)
10229 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10230 
10231 	/* re-enable Admin queue interrupt cause */
10232 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10233 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10234 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10235 	i40e_flush(hw);
10236 
10237 	kfree(event.msg_buf);
10238 }
10239 
10240 /**
10241  * i40e_verify_eeprom - make sure eeprom is good to use
10242  * @pf: board private structure
10243  **/
i40e_verify_eeprom(struct i40e_pf * pf)10244 static void i40e_verify_eeprom(struct i40e_pf *pf)
10245 {
10246 	int err;
10247 
10248 	err = i40e_diag_eeprom_test(&pf->hw);
10249 	if (err) {
10250 		/* retry in case of garbage read */
10251 		err = i40e_diag_eeprom_test(&pf->hw);
10252 		if (err) {
10253 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10254 				 err);
10255 			set_bit(__I40E_BAD_EEPROM, pf->state);
10256 		}
10257 	}
10258 
10259 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10260 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10261 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10262 	}
10263 }
10264 
10265 /**
10266  * i40e_enable_pf_switch_lb
10267  * @pf: pointer to the PF structure
10268  *
10269  * enable switch loop back or die - no point in a return value
10270  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)10271 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10272 {
10273 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10274 	struct i40e_vsi_context ctxt;
10275 	int ret;
10276 
10277 	ctxt.seid = pf->main_vsi_seid;
10278 	ctxt.pf_num = pf->hw.pf_id;
10279 	ctxt.vf_num = 0;
10280 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10281 	if (ret) {
10282 		dev_info(&pf->pdev->dev,
10283 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10284 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10285 		return;
10286 	}
10287 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10288 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10289 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10290 
10291 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10292 	if (ret) {
10293 		dev_info(&pf->pdev->dev,
10294 			 "update vsi switch failed, err %pe aq_err %s\n",
10295 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10296 	}
10297 }
10298 
10299 /**
10300  * i40e_disable_pf_switch_lb
10301  * @pf: pointer to the PF structure
10302  *
10303  * disable switch loop back or die - no point in a return value
10304  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)10305 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10306 {
10307 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10308 	struct i40e_vsi_context ctxt;
10309 	int ret;
10310 
10311 	ctxt.seid = pf->main_vsi_seid;
10312 	ctxt.pf_num = pf->hw.pf_id;
10313 	ctxt.vf_num = 0;
10314 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10315 	if (ret) {
10316 		dev_info(&pf->pdev->dev,
10317 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10318 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10319 		return;
10320 	}
10321 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10322 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10323 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10324 
10325 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10326 	if (ret) {
10327 		dev_info(&pf->pdev->dev,
10328 			 "update vsi switch failed, err %pe aq_err %s\n",
10329 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10330 	}
10331 }
10332 
10333 /**
10334  * i40e_config_bridge_mode - Configure the HW bridge mode
10335  * @veb: pointer to the bridge instance
10336  *
10337  * Configure the loop back mode for the LAN VSI that is downlink to the
10338  * specified HW bridge instance. It is expected this function is called
10339  * when a new HW bridge is instantiated.
10340  **/
i40e_config_bridge_mode(struct i40e_veb * veb)10341 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10342 {
10343 	struct i40e_pf *pf = veb->pf;
10344 
10345 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10346 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10347 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10348 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10349 		i40e_disable_pf_switch_lb(pf);
10350 	else
10351 		i40e_enable_pf_switch_lb(pf);
10352 }
10353 
10354 /**
10355  * i40e_reconstitute_veb - rebuild the VEB and VSIs connected to it
10356  * @veb: pointer to the VEB instance
10357  *
10358  * This is a function that builds the attached VSIs. We track the connections
10359  * through our own index numbers because the seid's from the HW could change
10360  * across the reset.
10361  **/
i40e_reconstitute_veb(struct i40e_veb * veb)10362 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10363 {
10364 	struct i40e_vsi *ctl_vsi = NULL;
10365 	struct i40e_pf *pf = veb->pf;
10366 	struct i40e_vsi *vsi;
10367 	int v, ret;
10368 
10369 	/* As we do not maintain PV (port virtualizer) switch element then
10370 	 * there can be only one non-floating VEB that have uplink to MAC SEID
10371 	 * and its control VSI is the main one.
10372 	 */
10373 	if (WARN_ON(veb->uplink_seid && veb->uplink_seid != pf->mac_seid)) {
10374 		dev_err(&pf->pdev->dev,
10375 			"Invalid uplink SEID for VEB %d\n", veb->idx);
10376 		return -ENOENT;
10377 	}
10378 
10379 	if (veb->uplink_seid == pf->mac_seid) {
10380 		/* Check that the LAN VSI has VEB owning flag set */
10381 		ctl_vsi = i40e_pf_get_main_vsi(pf);
10382 
10383 		if (WARN_ON(ctl_vsi->veb_idx != veb->idx ||
10384 			    !(ctl_vsi->flags & I40E_VSI_FLAG_VEB_OWNER))) {
10385 			dev_err(&pf->pdev->dev,
10386 				"Invalid control VSI for VEB %d\n", veb->idx);
10387 			return -ENOENT;
10388 		}
10389 
10390 		/* Add the control VSI to switch */
10391 		ret = i40e_add_vsi(ctl_vsi);
10392 		if (ret) {
10393 			dev_err(&pf->pdev->dev,
10394 				"Rebuild of owner VSI for VEB %d failed: %d\n",
10395 				veb->idx, ret);
10396 			return ret;
10397 		}
10398 
10399 		i40e_vsi_reset_stats(ctl_vsi);
10400 	}
10401 
10402 	/* create the VEB in the switch and move the VSI onto the VEB */
10403 	ret = i40e_add_veb(veb, ctl_vsi);
10404 	if (ret)
10405 		return ret;
10406 
10407 	if (veb->uplink_seid) {
10408 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10409 			veb->bridge_mode = BRIDGE_MODE_VEB;
10410 		else
10411 			veb->bridge_mode = BRIDGE_MODE_VEPA;
10412 		i40e_config_bridge_mode(veb);
10413 	}
10414 
10415 	/* create the remaining VSIs attached to this VEB */
10416 	i40e_pf_for_each_vsi(pf, v, vsi) {
10417 		if (vsi == ctl_vsi)
10418 			continue;
10419 
10420 		if (vsi->veb_idx == veb->idx) {
10421 			vsi->uplink_seid = veb->seid;
10422 			ret = i40e_add_vsi(vsi);
10423 			if (ret) {
10424 				dev_info(&pf->pdev->dev,
10425 					 "rebuild of vsi_idx %d failed: %d\n",
10426 					 v, ret);
10427 				return ret;
10428 			}
10429 			i40e_vsi_reset_stats(vsi);
10430 		}
10431 	}
10432 
10433 	return ret;
10434 }
10435 
10436 /**
10437  * i40e_get_capabilities - get info about the HW
10438  * @pf: the PF struct
10439  * @list_type: AQ capability to be queried
10440  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)10441 static int i40e_get_capabilities(struct i40e_pf *pf,
10442 				 enum i40e_admin_queue_opc list_type)
10443 {
10444 	struct libie_aqc_list_caps_elem *cap_buf;
10445 	u16 data_size;
10446 	int buf_len;
10447 	int err;
10448 
10449 	buf_len = 40 * sizeof(struct libie_aqc_list_caps_elem);
10450 	do {
10451 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10452 		if (!cap_buf)
10453 			return -ENOMEM;
10454 
10455 		/* this loads the data into the hw struct for us */
10456 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10457 						    &data_size, list_type,
10458 						    NULL);
10459 		/* data loaded, buffer no longer needed */
10460 		kfree(cap_buf);
10461 
10462 		if (pf->hw.aq.asq_last_status == LIBIE_AQ_RC_ENOMEM) {
10463 			/* retry with a larger buffer */
10464 			buf_len = data_size;
10465 		} else if (pf->hw.aq.asq_last_status != LIBIE_AQ_RC_OK || err) {
10466 			dev_info(&pf->pdev->dev,
10467 				 "capability discovery failed, err %pe aq_err %s\n",
10468 				 ERR_PTR(err),
10469 				 libie_aq_str(pf->hw.aq.asq_last_status));
10470 			return -ENODEV;
10471 		}
10472 	} while (err);
10473 
10474 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10475 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10476 			dev_info(&pf->pdev->dev,
10477 				 "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",
10478 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10479 				 pf->hw.func_caps.num_msix_vectors,
10480 				 pf->hw.func_caps.num_msix_vectors_vf,
10481 				 pf->hw.func_caps.fd_filters_guaranteed,
10482 				 pf->hw.func_caps.fd_filters_best_effort,
10483 				 pf->hw.func_caps.num_tx_qp,
10484 				 pf->hw.func_caps.num_vsis);
10485 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10486 			dev_info(&pf->pdev->dev,
10487 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10488 				 pf->hw.dev_caps.switch_mode,
10489 				 pf->hw.dev_caps.valid_functions);
10490 			dev_info(&pf->pdev->dev,
10491 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10492 				 pf->hw.dev_caps.sr_iov_1_1,
10493 				 pf->hw.dev_caps.num_vfs);
10494 			dev_info(&pf->pdev->dev,
10495 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10496 				 pf->hw.dev_caps.num_vsis,
10497 				 pf->hw.dev_caps.num_rx_qp,
10498 				 pf->hw.dev_caps.num_tx_qp);
10499 		}
10500 	}
10501 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10502 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10503 		       + pf->hw.func_caps.num_vfs)
10504 		if (pf->hw.revision_id == 0 &&
10505 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10506 			dev_info(&pf->pdev->dev,
10507 				 "got num_vsis %d, setting num_vsis to %d\n",
10508 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10509 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10510 		}
10511 	}
10512 	return 0;
10513 }
10514 
10515 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10516 
10517 /**
10518  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10519  * @pf: board private structure
10520  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)10521 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10522 {
10523 	struct i40e_vsi *main_vsi, *vsi;
10524 
10525 	/* quick workaround for an NVM issue that leaves a critical register
10526 	 * uninitialized
10527 	 */
10528 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10529 		static const u32 hkey[] = {
10530 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10531 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10532 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10533 			0x95b3a76d};
10534 		int i;
10535 
10536 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10537 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10538 	}
10539 
10540 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10541 		return;
10542 
10543 	/* find existing VSI and see if it needs configuring */
10544 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10545 
10546 	/* create a new VSI if none exists */
10547 	if (!vsi) {
10548 		main_vsi = i40e_pf_get_main_vsi(pf);
10549 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, main_vsi->seid, 0);
10550 		if (!vsi) {
10551 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10552 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10553 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10554 			return;
10555 		}
10556 	}
10557 
10558 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10559 }
10560 
10561 /**
10562  * i40e_fdir_teardown - release the Flow Director resources
10563  * @pf: board private structure
10564  **/
i40e_fdir_teardown(struct i40e_pf * pf)10565 static void i40e_fdir_teardown(struct i40e_pf *pf)
10566 {
10567 	struct i40e_vsi *vsi;
10568 
10569 	i40e_fdir_filter_exit(pf);
10570 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10571 	if (vsi)
10572 		i40e_vsi_release(vsi);
10573 }
10574 
10575 /**
10576  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10577  * @vsi: PF main vsi
10578  * @seid: seid of main or channel VSIs
10579  *
10580  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10581  * existed before reset
10582  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)10583 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10584 {
10585 	struct i40e_cloud_filter *cfilter;
10586 	struct i40e_pf *pf = vsi->back;
10587 	struct hlist_node *node;
10588 	int ret;
10589 
10590 	/* Add cloud filters back if they exist */
10591 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10592 				  cloud_node) {
10593 		if (cfilter->seid != seid)
10594 			continue;
10595 
10596 		if (cfilter->dst_port)
10597 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10598 								true);
10599 		else
10600 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10601 
10602 		if (ret) {
10603 			dev_dbg(&pf->pdev->dev,
10604 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10605 				ERR_PTR(ret),
10606 				libie_aq_str(pf->hw.aq.asq_last_status));
10607 			return ret;
10608 		}
10609 	}
10610 	return 0;
10611 }
10612 
10613 /**
10614  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10615  * @vsi: PF main vsi
10616  *
10617  * Rebuilds channel VSIs if they existed before reset
10618  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)10619 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10620 {
10621 	struct i40e_channel *ch, *ch_tmp;
10622 	int ret;
10623 
10624 	if (list_empty(&vsi->ch_list))
10625 		return 0;
10626 
10627 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10628 		if (!ch->initialized)
10629 			break;
10630 		/* Proceed with creation of channel (VMDq2) VSI */
10631 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10632 		if (ret) {
10633 			dev_info(&vsi->back->pdev->dev,
10634 				 "failed to rebuild channels using uplink_seid %u\n",
10635 				 vsi->uplink_seid);
10636 			return ret;
10637 		}
10638 		/* Reconfigure TX queues using QTX_CTL register */
10639 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10640 		if (ret) {
10641 			dev_info(&vsi->back->pdev->dev,
10642 				 "failed to configure TX rings for channel %u\n",
10643 				 ch->seid);
10644 			return ret;
10645 		}
10646 		/* update 'next_base_queue' */
10647 		vsi->next_base_queue = vsi->next_base_queue +
10648 							ch->num_queue_pairs;
10649 		if (ch->max_tx_rate) {
10650 			u64 credits = ch->max_tx_rate;
10651 
10652 			if (i40e_set_bw_limit(vsi, ch->seid,
10653 					      ch->max_tx_rate))
10654 				return -EINVAL;
10655 
10656 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10657 			dev_dbg(&vsi->back->pdev->dev,
10658 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10659 				ch->max_tx_rate,
10660 				credits,
10661 				ch->seid);
10662 		}
10663 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10664 		if (ret) {
10665 			dev_dbg(&vsi->back->pdev->dev,
10666 				"Failed to rebuild cloud filters for channel VSI %u\n",
10667 				ch->seid);
10668 			return ret;
10669 		}
10670 	}
10671 	return 0;
10672 }
10673 
10674 /**
10675  * i40e_clean_xps_state - clean xps state for every tx_ring
10676  * @vsi: ptr to the VSI
10677  **/
i40e_clean_xps_state(struct i40e_vsi * vsi)10678 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10679 {
10680 	int i;
10681 
10682 	if (vsi->tx_rings)
10683 		for (i = 0; i < vsi->num_queue_pairs; i++)
10684 			if (vsi->tx_rings[i])
10685 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10686 					  vsi->tx_rings[i]->state);
10687 }
10688 
10689 /**
10690  * i40e_prep_for_reset - prep for the core to reset
10691  * @pf: board private structure
10692  *
10693  * Close up the VFs and other things in prep for PF Reset.
10694   **/
i40e_prep_for_reset(struct i40e_pf * pf)10695 static void i40e_prep_for_reset(struct i40e_pf *pf)
10696 {
10697 	struct i40e_hw *hw = &pf->hw;
10698 	struct i40e_vsi *vsi;
10699 	int ret = 0;
10700 	u32 v;
10701 
10702 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10703 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10704 		return;
10705 	if (i40e_check_asq_alive(&pf->hw))
10706 		i40e_vc_notify_reset(pf);
10707 
10708 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10709 
10710 	/* quiesce the VSIs and their queues that are not already DOWN */
10711 	i40e_pf_quiesce_all_vsi(pf);
10712 
10713 	i40e_pf_for_each_vsi(pf, v, vsi) {
10714 		i40e_clean_xps_state(vsi);
10715 		vsi->seid = 0;
10716 	}
10717 
10718 	i40e_shutdown_adminq(&pf->hw);
10719 
10720 	/* call shutdown HMC */
10721 	if (hw->hmc.hmc_obj) {
10722 		ret = i40e_shutdown_lan_hmc(hw);
10723 		if (ret)
10724 			dev_warn(&pf->pdev->dev,
10725 				 "shutdown_lan_hmc failed: %d\n", ret);
10726 	}
10727 
10728 	/* Save the current PTP time so that we can restore the time after the
10729 	 * reset completes.
10730 	 */
10731 	i40e_ptp_save_hw_time(pf);
10732 }
10733 
10734 /**
10735  * i40e_send_version - update firmware with driver version
10736  * @pf: PF struct
10737  */
i40e_send_version(struct i40e_pf * pf)10738 static void i40e_send_version(struct i40e_pf *pf)
10739 {
10740 	struct i40e_driver_version dv;
10741 
10742 	dv.major_version = 0xff;
10743 	dv.minor_version = 0xff;
10744 	dv.build_version = 0xff;
10745 	dv.subbuild_version = 0;
10746 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10747 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10748 }
10749 
10750 /**
10751  * i40e_get_oem_version - get OEM specific version information
10752  * @hw: pointer to the hardware structure
10753  **/
i40e_get_oem_version(struct i40e_hw * hw)10754 static void i40e_get_oem_version(struct i40e_hw *hw)
10755 {
10756 	u16 block_offset = 0xffff;
10757 	u16 block_length = 0;
10758 	u16 capabilities = 0;
10759 	u16 gen_snap = 0;
10760 	u16 release = 0;
10761 
10762 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10763 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10764 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10765 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10766 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10767 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10768 #define I40E_NVM_OEM_LENGTH			3
10769 
10770 	/* Check if pointer to OEM version block is valid. */
10771 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10772 	if (block_offset == 0xffff)
10773 		return;
10774 
10775 	/* Check if OEM version block has correct length. */
10776 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10777 			   &block_length);
10778 	if (block_length < I40E_NVM_OEM_LENGTH)
10779 		return;
10780 
10781 	/* Check if OEM version format is as expected. */
10782 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10783 			   &capabilities);
10784 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10785 		return;
10786 
10787 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10788 			   &gen_snap);
10789 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10790 			   &release);
10791 	hw->nvm.oem_ver =
10792 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10793 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10794 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10795 }
10796 
10797 /**
10798  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10799  * @pf: board private structure
10800  **/
i40e_reset(struct i40e_pf * pf)10801 static int i40e_reset(struct i40e_pf *pf)
10802 {
10803 	struct i40e_hw *hw = &pf->hw;
10804 	int ret;
10805 
10806 	ret = i40e_pf_reset(hw);
10807 	if (ret) {
10808 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10809 		set_bit(__I40E_RESET_FAILED, pf->state);
10810 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10811 	} else {
10812 		pf->pfr_count++;
10813 	}
10814 	return ret;
10815 }
10816 
10817 /**
10818  * i40e_rebuild - rebuild using a saved config
10819  * @pf: board private structure
10820  * @reinit: if the Main VSI needs to re-initialized.
10821  * @lock_acquired: indicates whether or not the lock has been acquired
10822  * before this function was called.
10823  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10824 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10825 {
10826 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10827 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10828 	struct i40e_hw *hw = &pf->hw;
10829 	struct i40e_veb *veb;
10830 	int ret;
10831 	u32 val;
10832 	int v;
10833 
10834 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10835 	    is_recovery_mode_reported)
10836 		i40e_set_ethtool_ops(vsi->netdev);
10837 
10838 	if (test_bit(__I40E_DOWN, pf->state) &&
10839 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10840 		goto clear_recovery;
10841 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10842 
10843 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10844 	ret = i40e_init_adminq(&pf->hw);
10845 	if (ret) {
10846 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10847 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10848 		goto clear_recovery;
10849 	}
10850 	i40e_get_oem_version(&pf->hw);
10851 
10852 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10853 		/* The following delay is necessary for firmware update. */
10854 		mdelay(1000);
10855 	}
10856 
10857 	/* re-verify the eeprom if we just had an EMP reset */
10858 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10859 		i40e_verify_eeprom(pf);
10860 
10861 	/* if we are going out of or into recovery mode we have to act
10862 	 * accordingly with regard to resources initialization
10863 	 * and deinitialization
10864 	 */
10865 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10866 		if (i40e_get_capabilities(pf,
10867 					  i40e_aqc_opc_list_func_capabilities))
10868 			goto end_unlock;
10869 
10870 		if (is_recovery_mode_reported) {
10871 			/* we're staying in recovery mode so we'll reinitialize
10872 			 * misc vector here
10873 			 */
10874 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10875 				goto end_unlock;
10876 		} else {
10877 			if (!lock_acquired)
10878 				rtnl_lock();
10879 			/* we're going out of recovery mode so we'll free
10880 			 * the IRQ allocated specifically for recovery mode
10881 			 * and restore the interrupt scheme
10882 			 */
10883 			free_irq(pf->pdev->irq, pf);
10884 			i40e_clear_interrupt_scheme(pf);
10885 			if (i40e_restore_interrupt_scheme(pf))
10886 				goto end_unlock;
10887 		}
10888 
10889 		/* tell the firmware that we're starting */
10890 		i40e_send_version(pf);
10891 
10892 		/* bail out in case recovery mode was detected, as there is
10893 		 * no need for further configuration.
10894 		 */
10895 		goto end_unlock;
10896 	}
10897 
10898 	i40e_clear_pxe_mode(hw);
10899 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10900 	if (ret)
10901 		goto end_core_reset;
10902 
10903 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10904 				hw->func_caps.num_rx_qp, 0, 0);
10905 	if (ret) {
10906 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10907 		goto end_core_reset;
10908 	}
10909 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10910 	if (ret) {
10911 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10912 		goto end_core_reset;
10913 	}
10914 
10915 #ifdef CONFIG_I40E_DCB
10916 	/* Enable FW to write a default DCB config on link-up
10917 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10918 	 * is not supported with new link speed
10919 	 */
10920 	if (i40e_is_tc_mqprio_enabled(pf)) {
10921 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10922 	} else {
10923 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10924 		    (hw->phy.link_info.link_speed &
10925 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10926 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10927 			dev_warn(&pf->pdev->dev,
10928 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10929 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10930 		} else {
10931 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10932 			ret = i40e_init_pf_dcb(pf);
10933 			if (ret) {
10934 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10935 					 ret);
10936 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10937 				/* Continue without DCB enabled */
10938 			}
10939 		}
10940 	}
10941 
10942 #endif /* CONFIG_I40E_DCB */
10943 	if (!lock_acquired)
10944 		rtnl_lock();
10945 	ret = i40e_setup_pf_switch(pf, reinit, true);
10946 	if (ret)
10947 		goto end_unlock;
10948 
10949 	/* The driver only wants link up/down and module qualification
10950 	 * reports from firmware.  Note the negative logic.
10951 	 */
10952 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10953 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10954 					 I40E_AQ_EVENT_MEDIA_NA |
10955 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10956 	if (ret)
10957 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10958 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10959 
10960 	/* Rebuild the VSIs and VEBs that existed before reset.
10961 	 * They are still in our local switch element arrays, so only
10962 	 * need to rebuild the switch model in the HW.
10963 	 *
10964 	 * If there were VEBs but the reconstitution failed, we'll try
10965 	 * to recover minimal use by getting the basic PF VSI working.
10966 	 */
10967 	if (vsi->uplink_seid != pf->mac_seid) {
10968 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10969 
10970 		/* Rebuild VEBs */
10971 		i40e_pf_for_each_veb(pf, v, veb) {
10972 			ret = i40e_reconstitute_veb(veb);
10973 			if (!ret)
10974 				continue;
10975 
10976 			/* If Main VEB failed, we're in deep doodoo,
10977 			 * so give up rebuilding the switch and set up
10978 			 * for minimal rebuild of PF VSI.
10979 			 * If orphan failed, we'll report the error
10980 			 * but try to keep going.
10981 			 */
10982 			if (veb->uplink_seid == pf->mac_seid) {
10983 				dev_info(&pf->pdev->dev,
10984 					 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10985 					 ret);
10986 				vsi->uplink_seid = pf->mac_seid;
10987 				break;
10988 			} else if (veb->uplink_seid == 0) {
10989 				dev_info(&pf->pdev->dev,
10990 					 "rebuild of orphan VEB failed: %d\n",
10991 					 ret);
10992 			}
10993 		}
10994 	}
10995 
10996 	if (vsi->uplink_seid == pf->mac_seid) {
10997 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10998 		/* no VEB, so rebuild only the Main VSI */
10999 		ret = i40e_add_vsi(vsi);
11000 		if (ret) {
11001 			dev_info(&pf->pdev->dev,
11002 				 "rebuild of Main VSI failed: %d\n", ret);
11003 			goto end_unlock;
11004 		}
11005 	}
11006 
11007 	if (vsi->mqprio_qopt.max_rate[0]) {
11008 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11009 						  vsi->mqprio_qopt.max_rate[0]);
11010 		u64 credits = 0;
11011 
11012 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11013 		if (ret)
11014 			goto end_unlock;
11015 
11016 		credits = max_tx_rate;
11017 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11018 		dev_dbg(&vsi->back->pdev->dev,
11019 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11020 			max_tx_rate,
11021 			credits,
11022 			vsi->seid);
11023 	}
11024 
11025 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11026 	if (ret)
11027 		goto end_unlock;
11028 
11029 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11030 	 * for this main VSI if they exist
11031 	 */
11032 	ret = i40e_rebuild_channels(vsi);
11033 	if (ret)
11034 		goto end_unlock;
11035 
11036 	/* Reconfigure hardware for allowing smaller MSS in the case
11037 	 * of TSO, so that we avoid the MDD being fired and causing
11038 	 * a reset in the case of small MSS+TSO.
11039 	 */
11040 #define I40E_REG_MSS          0x000E64DC
11041 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11042 #define I40E_64BYTE_MSS       0x400000
11043 	val = rd32(hw, I40E_REG_MSS);
11044 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11045 		val &= ~I40E_REG_MSS_MIN_MASK;
11046 		val |= I40E_64BYTE_MSS;
11047 		wr32(hw, I40E_REG_MSS, val);
11048 	}
11049 
11050 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11051 		msleep(75);
11052 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11053 		if (ret)
11054 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11055 				 ERR_PTR(ret),
11056 				 libie_aq_str(pf->hw.aq.asq_last_status));
11057 	}
11058 	/* reinit the misc interrupt */
11059 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11060 		ret = i40e_setup_misc_vector(pf);
11061 		if (ret)
11062 			goto end_unlock;
11063 	}
11064 
11065 	/* Add a filter to drop all Flow control frames from any VSI from being
11066 	 * transmitted. By doing so we stop a malicious VF from sending out
11067 	 * PAUSE or PFC frames and potentially controlling traffic for other
11068 	 * PF/VF VSIs.
11069 	 * The FW can still send Flow control frames if enabled.
11070 	 */
11071 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11072 						       pf->main_vsi_seid);
11073 
11074 	/* restart the VSIs that were rebuilt and running before the reset */
11075 	i40e_pf_unquiesce_all_vsi(pf);
11076 
11077 	/* Release the RTNL lock before we start resetting VFs */
11078 	if (!lock_acquired)
11079 		rtnl_unlock();
11080 
11081 	/* Restore promiscuous settings */
11082 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11083 	if (ret)
11084 		dev_warn(&pf->pdev->dev,
11085 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11086 			 pf->cur_promisc ? "on" : "off",
11087 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
11088 
11089 	i40e_reset_all_vfs(pf, true);
11090 
11091 	/* tell the firmware that we're starting */
11092 	i40e_send_version(pf);
11093 
11094 	/* We've already released the lock, so don't do it again */
11095 	goto end_core_reset;
11096 
11097 end_unlock:
11098 	if (!lock_acquired)
11099 		rtnl_unlock();
11100 end_core_reset:
11101 	clear_bit(__I40E_RESET_FAILED, pf->state);
11102 clear_recovery:
11103 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11104 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11105 }
11106 
11107 /**
11108  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11109  * @pf: board private structure
11110  * @reinit: if the Main VSI needs to re-initialized.
11111  * @lock_acquired: indicates whether or not the lock has been acquired
11112  * before this function was called.
11113  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)11114 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11115 				   bool lock_acquired)
11116 {
11117 	int ret;
11118 
11119 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11120 		return;
11121 	/* Now we wait for GRST to settle out.
11122 	 * We don't have to delete the VEBs or VSIs from the hw switch
11123 	 * because the reset will make them disappear.
11124 	 */
11125 	ret = i40e_reset(pf);
11126 	if (!ret)
11127 		i40e_rebuild(pf, reinit, lock_acquired);
11128 	else
11129 		dev_err(&pf->pdev->dev, "%s: i40e_reset() FAILED", __func__);
11130 }
11131 
11132 /**
11133  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11134  * @pf: board private structure
11135  *
11136  * Close up the VFs and other things in prep for a Core Reset,
11137  * then get ready to rebuild the world.
11138  * @lock_acquired: indicates whether or not the lock has been acquired
11139  * before this function was called.
11140  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)11141 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11142 {
11143 	i40e_prep_for_reset(pf);
11144 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11145 }
11146 
11147 /**
11148  * i40e_print_vf_mdd_event - print VF Tx/Rx malicious driver detect event
11149  * @pf: board private structure
11150  * @vf: pointer to the VF structure
11151  * @is_tx: true - for Tx event, false - for  Rx
11152  */
i40e_print_vf_mdd_event(struct i40e_pf * pf,struct i40e_vf * vf,bool is_tx)11153 static void i40e_print_vf_mdd_event(struct i40e_pf *pf, struct i40e_vf *vf,
11154 				    bool is_tx)
11155 {
11156 	dev_err(&pf->pdev->dev, is_tx ?
11157 		"%lld Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n" :
11158 		"%lld Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n",
11159 		is_tx ? vf->mdd_tx_events.count : vf->mdd_rx_events.count,
11160 		pf->hw.pf_id,
11161 		vf->vf_id,
11162 		vf->default_lan_addr.addr,
11163 		str_on_off(test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)));
11164 }
11165 
11166 /**
11167  * i40e_print_vfs_mdd_events - print VFs malicious driver detect event
11168  * @pf: pointer to the PF structure
11169  *
11170  * Called from i40e_handle_mdd_event to rate limit and print VFs MDD events.
11171  */
i40e_print_vfs_mdd_events(struct i40e_pf * pf)11172 static void i40e_print_vfs_mdd_events(struct i40e_pf *pf)
11173 {
11174 	unsigned int i;
11175 
11176 	/* check that there are pending MDD events to print */
11177 	if (!test_and_clear_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state))
11178 		return;
11179 
11180 	if (!__ratelimit(&pf->mdd_message_rate_limit))
11181 		return;
11182 
11183 	for (i = 0; i < pf->num_alloc_vfs; i++) {
11184 		struct i40e_vf *vf = &pf->vf[i];
11185 		bool is_printed = false;
11186 
11187 		/* only print Rx MDD event message if there are new events */
11188 		if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
11189 			vf->mdd_rx_events.last_printed = vf->mdd_rx_events.count;
11190 			i40e_print_vf_mdd_event(pf, vf, false);
11191 			is_printed = true;
11192 		}
11193 
11194 		/* only print Tx MDD event message if there are new events */
11195 		if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
11196 			vf->mdd_tx_events.last_printed = vf->mdd_tx_events.count;
11197 			i40e_print_vf_mdd_event(pf, vf, true);
11198 			is_printed = true;
11199 		}
11200 
11201 		if (is_printed && !test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags))
11202 			dev_info(&pf->pdev->dev,
11203 				 "Use PF Control I/F to re-enable the VF #%d\n",
11204 				 i);
11205 	}
11206 }
11207 
11208 /**
11209  * i40e_handle_mdd_event
11210  * @pf: pointer to the PF structure
11211  *
11212  * Called from the MDD irq handler to identify possibly malicious vfs
11213  **/
i40e_handle_mdd_event(struct i40e_pf * pf)11214 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11215 {
11216 	struct i40e_hw *hw = &pf->hw;
11217 	bool mdd_detected = false;
11218 	struct i40e_vf *vf;
11219 	u32 reg;
11220 	int i;
11221 
11222 	if (!test_and_clear_bit(__I40E_MDD_EVENT_PENDING, pf->state)) {
11223 		/* Since the VF MDD event logging is rate limited, check if
11224 		 * there are pending MDD events.
11225 		 */
11226 		i40e_print_vfs_mdd_events(pf);
11227 		return;
11228 	}
11229 
11230 	/* find what triggered the MDD event */
11231 	reg = rd32(hw, I40E_GL_MDET_TX);
11232 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11233 		u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
11234 		u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
11235 		u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
11236 		u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
11237 				pf->hw.func_caps.base_queue;
11238 		if (netif_msg_tx_err(pf))
11239 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11240 				 event, queue, pf_num, vf_num);
11241 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11242 		mdd_detected = true;
11243 	}
11244 	reg = rd32(hw, I40E_GL_MDET_RX);
11245 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11246 		u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
11247 		u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
11248 		u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
11249 				pf->hw.func_caps.base_queue;
11250 		if (netif_msg_rx_err(pf))
11251 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11252 				 event, queue, func);
11253 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11254 		mdd_detected = true;
11255 	}
11256 
11257 	if (mdd_detected) {
11258 		reg = rd32(hw, I40E_PF_MDET_TX);
11259 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11260 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11261 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11262 		}
11263 		reg = rd32(hw, I40E_PF_MDET_RX);
11264 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11265 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11266 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11267 		}
11268 	}
11269 
11270 	/* see if one of the VFs needs its hand slapped */
11271 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11272 		bool is_mdd_on_tx = false;
11273 		bool is_mdd_on_rx = false;
11274 
11275 		vf = &(pf->vf[i]);
11276 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11277 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11278 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11279 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11280 			vf->mdd_tx_events.count++;
11281 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11282 			is_mdd_on_tx = true;
11283 		}
11284 
11285 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11286 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11287 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11288 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11289 			vf->mdd_rx_events.count++;
11290 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11291 			is_mdd_on_rx = true;
11292 		}
11293 
11294 		if ((is_mdd_on_tx || is_mdd_on_rx) &&
11295 		    test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
11296 			/* VF MDD event counters will be cleared by
11297 			 * reset, so print the event prior to reset.
11298 			 */
11299 			if (is_mdd_on_rx)
11300 				i40e_print_vf_mdd_event(pf, vf, false);
11301 			if (is_mdd_on_tx)
11302 				i40e_print_vf_mdd_event(pf, vf, true);
11303 
11304 			i40e_vc_reset_vf(vf, true);
11305 		}
11306 	}
11307 
11308 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11309 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11310 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11311 	i40e_flush(hw);
11312 
11313 	i40e_print_vfs_mdd_events(pf);
11314 }
11315 
11316 /**
11317  * i40e_service_task - Run the driver's async subtasks
11318  * @work: pointer to work_struct containing our data
11319  **/
i40e_service_task(struct work_struct * work)11320 static void i40e_service_task(struct work_struct *work)
11321 {
11322 	struct i40e_pf *pf = container_of(work,
11323 					  struct i40e_pf,
11324 					  service_task);
11325 	unsigned long start_time = jiffies;
11326 
11327 	/* don't bother with service tasks if a reset is in progress */
11328 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11329 	    test_bit(__I40E_SUSPENDED, pf->state))
11330 		return;
11331 
11332 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11333 		return;
11334 
11335 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11336 		i40e_detect_recover_hung(pf);
11337 		i40e_sync_filters_subtask(pf);
11338 		i40e_reset_subtask(pf);
11339 		i40e_handle_mdd_event(pf);
11340 		i40e_vc_process_vflr_event(pf);
11341 		i40e_watchdog_subtask(pf);
11342 		i40e_fdir_reinit_subtask(pf);
11343 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11344 			/* Client subtask will reopen next time through. */
11345 			i40e_notify_client_of_netdev_close(pf, true);
11346 		} else {
11347 			i40e_client_subtask(pf);
11348 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11349 					       pf->state))
11350 				i40e_notify_client_of_l2_param_changes(pf);
11351 		}
11352 		i40e_sync_filters_subtask(pf);
11353 	} else {
11354 		i40e_reset_subtask(pf);
11355 	}
11356 
11357 	i40e_clean_adminq_subtask(pf);
11358 
11359 	/* flush memory to make sure state is correct before next watchdog */
11360 	smp_mb__before_atomic();
11361 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11362 
11363 	/* If the tasks have taken longer than one timer cycle or there
11364 	 * is more work to be done, reschedule the service task now
11365 	 * rather than wait for the timer to tick again.
11366 	 */
11367 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11368 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11369 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11370 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11371 		i40e_service_event_schedule(pf);
11372 }
11373 
11374 /**
11375  * i40e_service_timer - timer callback
11376  * @t: timer list pointer
11377  **/
i40e_service_timer(struct timer_list * t)11378 static void i40e_service_timer(struct timer_list *t)
11379 {
11380 	struct i40e_pf *pf = timer_container_of(pf, t, service_timer);
11381 
11382 	mod_timer(&pf->service_timer,
11383 		  round_jiffies(jiffies + pf->service_timer_period));
11384 	i40e_service_event_schedule(pf);
11385 }
11386 
11387 /**
11388  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11389  * @vsi: the VSI being configured
11390  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)11391 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11392 {
11393 	struct i40e_pf *pf = vsi->back;
11394 
11395 	switch (vsi->type) {
11396 	case I40E_VSI_MAIN:
11397 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11398 		if (!vsi->num_tx_desc)
11399 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11400 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11401 		if (!vsi->num_rx_desc)
11402 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11403 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11404 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11405 			vsi->num_q_vectors = pf->num_lan_msix;
11406 		else
11407 			vsi->num_q_vectors = 1;
11408 
11409 		break;
11410 
11411 	case I40E_VSI_FDIR:
11412 		vsi->alloc_queue_pairs = 1;
11413 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11414 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11415 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11416 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11417 		vsi->num_q_vectors = pf->num_fdsb_msix;
11418 		break;
11419 
11420 	case I40E_VSI_VMDQ2:
11421 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11422 		if (!vsi->num_tx_desc)
11423 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11424 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11425 		if (!vsi->num_rx_desc)
11426 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11427 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11428 		vsi->num_q_vectors = pf->num_vmdq_msix;
11429 		break;
11430 
11431 	case I40E_VSI_SRIOV:
11432 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11433 		if (!vsi->num_tx_desc)
11434 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11435 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11436 		if (!vsi->num_rx_desc)
11437 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11438 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11439 		break;
11440 
11441 	default:
11442 		WARN_ON(1);
11443 		return -ENODATA;
11444 	}
11445 
11446 	if (is_kdump_kernel()) {
11447 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11448 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11449 	}
11450 
11451 	return 0;
11452 }
11453 
11454 /**
11455  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11456  * @vsi: VSI pointer
11457  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11458  *
11459  * On error: returns error code (negative)
11460  * On success: returns 0
11461  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)11462 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11463 {
11464 	struct i40e_ring **next_rings;
11465 	int size;
11466 	int ret = 0;
11467 
11468 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11469 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11470 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11471 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11472 	if (!vsi->tx_rings)
11473 		return -ENOMEM;
11474 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11475 	if (i40e_enabled_xdp_vsi(vsi)) {
11476 		vsi->xdp_rings = next_rings;
11477 		next_rings += vsi->alloc_queue_pairs;
11478 	}
11479 	vsi->rx_rings = next_rings;
11480 
11481 	if (alloc_qvectors) {
11482 		/* allocate memory for q_vector pointers */
11483 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11484 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11485 		if (!vsi->q_vectors) {
11486 			ret = -ENOMEM;
11487 			goto err_vectors;
11488 		}
11489 	}
11490 	return ret;
11491 
11492 err_vectors:
11493 	kfree(vsi->tx_rings);
11494 	return ret;
11495 }
11496 
11497 /**
11498  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11499  * @pf: board private structure
11500  * @type: type of VSI
11501  *
11502  * On error: returns error code (negative)
11503  * On success: returns vsi index in PF (positive)
11504  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)11505 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11506 {
11507 	int ret = -ENODEV;
11508 	struct i40e_vsi *vsi;
11509 	int vsi_idx;
11510 	int i;
11511 
11512 	/* Need to protect the allocation of the VSIs at the PF level */
11513 	mutex_lock(&pf->switch_mutex);
11514 
11515 	/* VSI list may be fragmented if VSI creation/destruction has
11516 	 * been happening.  We can afford to do a quick scan to look
11517 	 * for any free VSIs in the list.
11518 	 *
11519 	 * find next empty vsi slot, looping back around if necessary
11520 	 */
11521 	i = pf->next_vsi;
11522 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11523 		i++;
11524 	if (i >= pf->num_alloc_vsi) {
11525 		i = 0;
11526 		while (i < pf->next_vsi && pf->vsi[i])
11527 			i++;
11528 	}
11529 
11530 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11531 		vsi_idx = i;             /* Found one! */
11532 	} else {
11533 		ret = -ENODEV;
11534 		goto unlock_pf;  /* out of VSI slots! */
11535 	}
11536 	pf->next_vsi = ++i;
11537 
11538 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11539 	if (!vsi) {
11540 		ret = -ENOMEM;
11541 		goto unlock_pf;
11542 	}
11543 	vsi->type = type;
11544 	vsi->back = pf;
11545 	set_bit(__I40E_VSI_DOWN, vsi->state);
11546 	vsi->flags = 0;
11547 	vsi->idx = vsi_idx;
11548 	vsi->int_rate_limit = 0;
11549 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11550 				pf->rss_table_size : 64;
11551 	vsi->netdev_registered = false;
11552 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11553 	hash_init(vsi->mac_filter_hash);
11554 	vsi->irqs_ready = false;
11555 
11556 	if (type == I40E_VSI_MAIN) {
11557 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11558 		if (!vsi->af_xdp_zc_qps)
11559 			goto err_rings;
11560 	}
11561 
11562 	ret = i40e_set_num_rings_in_vsi(vsi);
11563 	if (ret)
11564 		goto err_rings;
11565 
11566 	ret = i40e_vsi_alloc_arrays(vsi, true);
11567 	if (ret)
11568 		goto err_rings;
11569 
11570 	/* Setup default MSIX irq handler for VSI */
11571 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11572 
11573 	/* Initialize VSI lock */
11574 	spin_lock_init(&vsi->mac_filter_hash_lock);
11575 	pf->vsi[vsi_idx] = vsi;
11576 	ret = vsi_idx;
11577 	goto unlock_pf;
11578 
11579 err_rings:
11580 	bitmap_free(vsi->af_xdp_zc_qps);
11581 	pf->next_vsi = i - 1;
11582 	kfree(vsi);
11583 unlock_pf:
11584 	mutex_unlock(&pf->switch_mutex);
11585 	return ret;
11586 }
11587 
11588 /**
11589  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11590  * @vsi: VSI pointer
11591  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11592  *
11593  * On error: returns error code (negative)
11594  * On success: returns 0
11595  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)11596 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11597 {
11598 	/* free the ring and vector containers */
11599 	if (free_qvectors) {
11600 		kfree(vsi->q_vectors);
11601 		vsi->q_vectors = NULL;
11602 	}
11603 	kfree(vsi->tx_rings);
11604 	vsi->tx_rings = NULL;
11605 	vsi->rx_rings = NULL;
11606 	vsi->xdp_rings = NULL;
11607 }
11608 
11609 /**
11610  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11611  * and lookup table
11612  * @vsi: Pointer to VSI structure
11613  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)11614 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11615 {
11616 	if (!vsi)
11617 		return;
11618 
11619 	kfree(vsi->rss_hkey_user);
11620 	vsi->rss_hkey_user = NULL;
11621 
11622 	kfree(vsi->rss_lut_user);
11623 	vsi->rss_lut_user = NULL;
11624 }
11625 
11626 /**
11627  * i40e_vsi_clear - Deallocate the VSI provided
11628  * @vsi: the VSI being un-configured
11629  **/
i40e_vsi_clear(struct i40e_vsi * vsi)11630 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11631 {
11632 	struct i40e_pf *pf;
11633 
11634 	if (!vsi)
11635 		return 0;
11636 
11637 	if (!vsi->back)
11638 		goto free_vsi;
11639 	pf = vsi->back;
11640 
11641 	mutex_lock(&pf->switch_mutex);
11642 	if (!pf->vsi[vsi->idx]) {
11643 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11644 			vsi->idx, vsi->idx, vsi->type);
11645 		goto unlock_vsi;
11646 	}
11647 
11648 	if (pf->vsi[vsi->idx] != vsi) {
11649 		dev_err(&pf->pdev->dev,
11650 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11651 			pf->vsi[vsi->idx]->idx,
11652 			pf->vsi[vsi->idx]->type,
11653 			vsi->idx, vsi->type);
11654 		goto unlock_vsi;
11655 	}
11656 
11657 	/* updates the PF for this cleared vsi */
11658 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11659 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11660 
11661 	bitmap_free(vsi->af_xdp_zc_qps);
11662 	i40e_vsi_free_arrays(vsi, true);
11663 	i40e_clear_rss_config_user(vsi);
11664 
11665 	pf->vsi[vsi->idx] = NULL;
11666 	if (vsi->idx < pf->next_vsi)
11667 		pf->next_vsi = vsi->idx;
11668 
11669 unlock_vsi:
11670 	mutex_unlock(&pf->switch_mutex);
11671 free_vsi:
11672 	kfree(vsi);
11673 
11674 	return 0;
11675 }
11676 
11677 /**
11678  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11679  * @vsi: the VSI being cleaned
11680  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)11681 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11682 {
11683 	int i;
11684 
11685 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11686 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11687 			kfree_rcu(vsi->tx_rings[i], rcu);
11688 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11689 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11690 			if (vsi->xdp_rings)
11691 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11692 		}
11693 	}
11694 }
11695 
11696 /**
11697  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11698  * @vsi: the VSI being configured
11699  **/
i40e_alloc_rings(struct i40e_vsi * vsi)11700 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11701 {
11702 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11703 	struct i40e_pf *pf = vsi->back;
11704 	struct i40e_ring *ring;
11705 
11706 	/* Set basic values in the rings to be used later during open() */
11707 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11708 		/* allocate space for both Tx and Rx in one shot */
11709 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11710 		if (!ring)
11711 			goto err_out;
11712 
11713 		ring->queue_index = i;
11714 		ring->reg_idx = vsi->base_queue + i;
11715 		ring->ring_active = false;
11716 		ring->vsi = vsi;
11717 		ring->netdev = vsi->netdev;
11718 		ring->dev = &pf->pdev->dev;
11719 		ring->count = vsi->num_tx_desc;
11720 		ring->size = 0;
11721 		ring->dcb_tc = 0;
11722 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11723 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11724 		ring->itr_setting = pf->tx_itr_default;
11725 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11726 
11727 		if (!i40e_enabled_xdp_vsi(vsi))
11728 			goto setup_rx;
11729 
11730 		ring->queue_index = vsi->alloc_queue_pairs + i;
11731 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11732 		ring->ring_active = false;
11733 		ring->vsi = vsi;
11734 		ring->netdev = NULL;
11735 		ring->dev = &pf->pdev->dev;
11736 		ring->count = vsi->num_tx_desc;
11737 		ring->size = 0;
11738 		ring->dcb_tc = 0;
11739 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11740 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11741 		set_ring_xdp(ring);
11742 		ring->itr_setting = pf->tx_itr_default;
11743 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11744 
11745 setup_rx:
11746 		ring->queue_index = i;
11747 		ring->reg_idx = vsi->base_queue + i;
11748 		ring->ring_active = false;
11749 		ring->vsi = vsi;
11750 		ring->netdev = vsi->netdev;
11751 		ring->dev = &pf->pdev->dev;
11752 		ring->count = vsi->num_rx_desc;
11753 		ring->size = 0;
11754 		ring->dcb_tc = 0;
11755 		ring->itr_setting = pf->rx_itr_default;
11756 		WRITE_ONCE(vsi->rx_rings[i], ring);
11757 	}
11758 
11759 	return 0;
11760 
11761 err_out:
11762 	i40e_vsi_clear_rings(vsi);
11763 	return -ENOMEM;
11764 }
11765 
11766 /**
11767  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11768  * @pf: board private structure
11769  * @vectors: the number of MSI-X vectors to request
11770  *
11771  * Returns the number of vectors reserved, or error
11772  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)11773 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11774 {
11775 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11776 					I40E_MIN_MSIX, vectors);
11777 	if (vectors < 0) {
11778 		dev_info(&pf->pdev->dev,
11779 			 "MSI-X vector reservation failed: %d\n", vectors);
11780 		vectors = 0;
11781 	}
11782 
11783 	return vectors;
11784 }
11785 
11786 /**
11787  * i40e_init_msix - Setup the MSIX capability
11788  * @pf: board private structure
11789  *
11790  * Work with the OS to set up the MSIX vectors needed.
11791  *
11792  * Returns the number of vectors reserved or negative on failure
11793  **/
i40e_init_msix(struct i40e_pf * pf)11794 static int i40e_init_msix(struct i40e_pf *pf)
11795 {
11796 	struct i40e_hw *hw = &pf->hw;
11797 	int cpus, extra_vectors;
11798 	int vectors_left;
11799 	int v_budget, i;
11800 	int v_actual;
11801 	int iwarp_requested = 0;
11802 
11803 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11804 		return -ENODEV;
11805 
11806 	/* The number of vectors we'll request will be comprised of:
11807 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11808 	 *   - The number of LAN queue pairs
11809 	 *	- Queues being used for RSS.
11810 	 *		We don't need as many as max_rss_size vectors.
11811 	 *		use rss_size instead in the calculation since that
11812 	 *		is governed by number of cpus in the system.
11813 	 *	- assumes symmetric Tx/Rx pairing
11814 	 *   - The number of VMDq pairs
11815 	 *   - The CPU count within the NUMA node if iWARP is enabled
11816 	 * Once we count this up, try the request.
11817 	 *
11818 	 * If we can't get what we want, we'll simplify to nearly nothing
11819 	 * and try again.  If that still fails, we punt.
11820 	 */
11821 	vectors_left = hw->func_caps.num_msix_vectors;
11822 	v_budget = 0;
11823 
11824 	/* reserve one vector for miscellaneous handler */
11825 	if (vectors_left) {
11826 		v_budget++;
11827 		vectors_left--;
11828 	}
11829 
11830 	/* reserve some vectors for the main PF traffic queues. Initially we
11831 	 * only reserve at most 50% of the available vectors, in the case that
11832 	 * the number of online CPUs is large. This ensures that we can enable
11833 	 * extra features as well. Once we've enabled the other features, we
11834 	 * will use any remaining vectors to reach as close as we can to the
11835 	 * number of online CPUs.
11836 	 */
11837 	cpus = num_online_cpus();
11838 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11839 	vectors_left -= pf->num_lan_msix;
11840 
11841 	/* reserve one vector for sideband flow director */
11842 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11843 		if (vectors_left) {
11844 			pf->num_fdsb_msix = 1;
11845 			v_budget++;
11846 			vectors_left--;
11847 		} else {
11848 			pf->num_fdsb_msix = 0;
11849 		}
11850 	}
11851 
11852 	/* can we reserve enough for iWARP? */
11853 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11854 		iwarp_requested = pf->num_iwarp_msix;
11855 
11856 		if (!vectors_left)
11857 			pf->num_iwarp_msix = 0;
11858 		else if (vectors_left < pf->num_iwarp_msix)
11859 			pf->num_iwarp_msix = 1;
11860 		v_budget += pf->num_iwarp_msix;
11861 		vectors_left -= pf->num_iwarp_msix;
11862 	}
11863 
11864 	/* any vectors left over go for VMDq support */
11865 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11866 		if (!vectors_left) {
11867 			pf->num_vmdq_msix = 0;
11868 			pf->num_vmdq_qps = 0;
11869 		} else {
11870 			int vmdq_vecs_wanted =
11871 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11872 			int vmdq_vecs =
11873 				min_t(int, vectors_left, vmdq_vecs_wanted);
11874 
11875 			/* if we're short on vectors for what's desired, we limit
11876 			 * the queues per vmdq.  If this is still more than are
11877 			 * available, the user will need to change the number of
11878 			 * queues/vectors used by the PF later with the ethtool
11879 			 * channels command
11880 			 */
11881 			if (vectors_left < vmdq_vecs_wanted) {
11882 				pf->num_vmdq_qps = 1;
11883 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11884 				vmdq_vecs = min_t(int,
11885 						  vectors_left,
11886 						  vmdq_vecs_wanted);
11887 			}
11888 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11889 
11890 			v_budget += vmdq_vecs;
11891 			vectors_left -= vmdq_vecs;
11892 		}
11893 	}
11894 
11895 	/* On systems with a large number of SMP cores, we previously limited
11896 	 * the number of vectors for num_lan_msix to be at most 50% of the
11897 	 * available vectors, to allow for other features. Now, we add back
11898 	 * the remaining vectors. However, we ensure that the total
11899 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11900 	 * calculate the number of vectors we can add without going over the
11901 	 * cap of CPUs. For systems with a small number of CPUs this will be
11902 	 * zero.
11903 	 */
11904 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11905 	pf->num_lan_msix += extra_vectors;
11906 	vectors_left -= extra_vectors;
11907 
11908 	WARN(vectors_left < 0,
11909 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11910 
11911 	v_budget += pf->num_lan_msix;
11912 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11913 				   GFP_KERNEL);
11914 	if (!pf->msix_entries)
11915 		return -ENOMEM;
11916 
11917 	for (i = 0; i < v_budget; i++)
11918 		pf->msix_entries[i].entry = i;
11919 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11920 
11921 	if (v_actual < I40E_MIN_MSIX) {
11922 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11923 		kfree(pf->msix_entries);
11924 		pf->msix_entries = NULL;
11925 		pci_disable_msix(pf->pdev);
11926 		return -ENODEV;
11927 
11928 	} else if (v_actual == I40E_MIN_MSIX) {
11929 		/* Adjust for minimal MSIX use */
11930 		pf->num_vmdq_vsis = 0;
11931 		pf->num_vmdq_qps = 0;
11932 		pf->num_lan_qps = 1;
11933 		pf->num_lan_msix = 1;
11934 
11935 	} else if (v_actual != v_budget) {
11936 		/* If we have limited resources, we will start with no vectors
11937 		 * for the special features and then allocate vectors to some
11938 		 * of these features based on the policy and at the end disable
11939 		 * the features that did not get any vectors.
11940 		 */
11941 		int vec;
11942 
11943 		dev_info(&pf->pdev->dev,
11944 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11945 			 v_actual, v_budget);
11946 		/* reserve the misc vector */
11947 		vec = v_actual - 1;
11948 
11949 		/* Scale vector usage down */
11950 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11951 		pf->num_vmdq_vsis = 1;
11952 		pf->num_vmdq_qps = 1;
11953 
11954 		/* partition out the remaining vectors */
11955 		switch (vec) {
11956 		case 2:
11957 			pf->num_lan_msix = 1;
11958 			break;
11959 		case 3:
11960 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11961 				pf->num_lan_msix = 1;
11962 				pf->num_iwarp_msix = 1;
11963 			} else {
11964 				pf->num_lan_msix = 2;
11965 			}
11966 			break;
11967 		default:
11968 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11969 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11970 						 iwarp_requested);
11971 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11972 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11973 			} else {
11974 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11975 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11976 			}
11977 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11978 				pf->num_fdsb_msix = 1;
11979 				vec--;
11980 			}
11981 			pf->num_lan_msix = min_t(int,
11982 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11983 							      pf->num_lan_msix);
11984 			pf->num_lan_qps = pf->num_lan_msix;
11985 			break;
11986 		}
11987 	}
11988 
11989 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
11990 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11991 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
11992 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
11993 	}
11994 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
11995 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11996 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
11997 	}
11998 
11999 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
12000 	    pf->num_iwarp_msix == 0) {
12001 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
12002 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12003 	}
12004 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
12005 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
12006 		   pf->num_lan_msix,
12007 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
12008 		   pf->num_fdsb_msix,
12009 		   pf->num_iwarp_msix);
12010 
12011 	return v_actual;
12012 }
12013 
12014 /**
12015  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
12016  * @vsi: the VSI being configured
12017  * @v_idx: index of the vector in the vsi struct
12018  *
12019  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
12020  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)12021 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
12022 {
12023 	struct i40e_q_vector *q_vector;
12024 
12025 	/* allocate q_vector */
12026 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
12027 	if (!q_vector)
12028 		return -ENOMEM;
12029 
12030 	q_vector->vsi = vsi;
12031 	q_vector->v_idx = v_idx;
12032 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
12033 
12034 	if (vsi->netdev)
12035 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
12036 
12037 	/* tie q_vector and vsi together */
12038 	vsi->q_vectors[v_idx] = q_vector;
12039 
12040 	return 0;
12041 }
12042 
12043 /**
12044  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12045  * @vsi: the VSI being configured
12046  *
12047  * We allocate one q_vector per queue interrupt.  If allocation fails we
12048  * return -ENOMEM.
12049  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)12050 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12051 {
12052 	struct i40e_pf *pf = vsi->back;
12053 	int err, v_idx, num_q_vectors;
12054 
12055 	/* if not MSIX, give the one vector only to the LAN VSI */
12056 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12057 		num_q_vectors = vsi->num_q_vectors;
12058 	else if (vsi->type == I40E_VSI_MAIN)
12059 		num_q_vectors = 1;
12060 	else
12061 		return -EINVAL;
12062 
12063 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12064 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12065 		if (err)
12066 			goto err_out;
12067 	}
12068 
12069 	return 0;
12070 
12071 err_out:
12072 	while (v_idx--)
12073 		i40e_free_q_vector(vsi, v_idx);
12074 
12075 	return err;
12076 }
12077 
12078 /**
12079  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12080  * @pf: board private structure to initialize
12081  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)12082 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12083 {
12084 	int vectors = 0;
12085 	ssize_t size;
12086 
12087 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12088 		vectors = i40e_init_msix(pf);
12089 		if (vectors < 0) {
12090 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12091 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12092 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12093 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12094 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12095 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12096 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12097 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12098 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12099 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12100 
12101 			/* rework the queue expectations without MSIX */
12102 			i40e_determine_queue_usage(pf);
12103 		}
12104 	}
12105 
12106 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12107 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12108 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12109 		vectors = pci_enable_msi(pf->pdev);
12110 		if (vectors < 0) {
12111 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12112 				 vectors);
12113 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12114 		}
12115 		vectors = 1;  /* one MSI or Legacy vector */
12116 	}
12117 
12118 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12119 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12120 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12121 
12122 	/* set up vector assignment tracking */
12123 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12124 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12125 	if (!pf->irq_pile)
12126 		return -ENOMEM;
12127 
12128 	pf->irq_pile->num_entries = vectors;
12129 
12130 	/* track first vector for misc interrupts, ignore return */
12131 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12132 
12133 	return 0;
12134 }
12135 
12136 /**
12137  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12138  * @pf: private board data structure
12139  *
12140  * Restore the interrupt scheme that was cleared when we suspended the
12141  * device. This should be called during resume to re-allocate the q_vectors
12142  * and reacquire IRQs.
12143  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)12144 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12145 {
12146 	struct i40e_vsi *vsi;
12147 	int err, i;
12148 
12149 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12150 	 * scheme. We need to re-enabled them here in order to attempt to
12151 	 * re-acquire the MSI or MSI-X vectors
12152 	 */
12153 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12154 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12155 
12156 	err = i40e_init_interrupt_scheme(pf);
12157 	if (err)
12158 		return err;
12159 
12160 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12161 	 * rings together again.
12162 	 */
12163 	i40e_pf_for_each_vsi(pf, i, vsi) {
12164 		err = i40e_vsi_alloc_q_vectors(vsi);
12165 		if (err)
12166 			goto err_unwind;
12167 
12168 		i40e_vsi_map_rings_to_vectors(vsi);
12169 	}
12170 
12171 	err = i40e_setup_misc_vector(pf);
12172 	if (err)
12173 		goto err_unwind;
12174 
12175 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12176 		i40e_client_update_msix_info(pf);
12177 
12178 	return 0;
12179 
12180 err_unwind:
12181 	while (i--) {
12182 		if (pf->vsi[i])
12183 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12184 	}
12185 
12186 	return err;
12187 }
12188 
12189 /**
12190  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12191  * non queue events in recovery mode
12192  * @pf: board private structure
12193  *
12194  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12195  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12196  * This is handled differently than in recovery mode since no Tx/Rx resources
12197  * are being allocated.
12198  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)12199 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12200 {
12201 	int err;
12202 
12203 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12204 		err = i40e_setup_misc_vector(pf);
12205 
12206 		if (err) {
12207 			dev_info(&pf->pdev->dev,
12208 				 "MSI-X misc vector request failed, error %d\n",
12209 				 err);
12210 			return err;
12211 		}
12212 	} else {
12213 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12214 
12215 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12216 				  pf->int_name, pf);
12217 
12218 		if (err) {
12219 			dev_info(&pf->pdev->dev,
12220 				 "MSI/legacy misc vector request failed, error %d\n",
12221 				 err);
12222 			return err;
12223 		}
12224 		i40e_enable_misc_int_causes(pf);
12225 		i40e_irq_dynamic_enable_icr0(pf);
12226 	}
12227 
12228 	return 0;
12229 }
12230 
12231 /**
12232  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12233  * @pf: board private structure
12234  *
12235  * This sets up the handler for MSIX 0, which is used to manage the
12236  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12237  * when in MSI or Legacy interrupt mode.
12238  **/
i40e_setup_misc_vector(struct i40e_pf * pf)12239 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12240 {
12241 	struct i40e_hw *hw = &pf->hw;
12242 	int err = 0;
12243 
12244 	/* Only request the IRQ once, the first time through. */
12245 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12246 		err = request_irq(pf->msix_entries[0].vector,
12247 				  i40e_intr, 0, pf->int_name, pf);
12248 		if (err) {
12249 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12250 			dev_info(&pf->pdev->dev,
12251 				 "request_irq for %s failed: %d\n",
12252 				 pf->int_name, err);
12253 			return -EFAULT;
12254 		}
12255 	}
12256 
12257 	i40e_enable_misc_int_causes(pf);
12258 
12259 	/* associate no queues to the misc vector */
12260 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12261 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12262 
12263 	i40e_flush(hw);
12264 
12265 	i40e_irq_dynamic_enable_icr0(pf);
12266 
12267 	return err;
12268 }
12269 
12270 /**
12271  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12272  * @vsi: Pointer to vsi structure
12273  * @seed: Buffter to store the hash keys
12274  * @lut: Buffer to store the lookup table entries
12275  * @lut_size: Size of buffer to store the lookup table entries
12276  *
12277  * Return 0 on success, negative on failure
12278  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)12279 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12280 			   u8 *lut, u16 lut_size)
12281 {
12282 	struct i40e_pf *pf = vsi->back;
12283 	struct i40e_hw *hw = &pf->hw;
12284 	int ret = 0;
12285 
12286 	if (seed) {
12287 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12288 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12289 		if (ret) {
12290 			dev_info(&pf->pdev->dev,
12291 				 "Cannot get RSS key, err %pe aq_err %s\n",
12292 				 ERR_PTR(ret),
12293 				 libie_aq_str(pf->hw.aq.asq_last_status));
12294 			return ret;
12295 		}
12296 	}
12297 
12298 	if (lut) {
12299 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12300 
12301 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12302 		if (ret) {
12303 			dev_info(&pf->pdev->dev,
12304 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12305 				 ERR_PTR(ret),
12306 				 libie_aq_str(pf->hw.aq.asq_last_status));
12307 			return ret;
12308 		}
12309 	}
12310 
12311 	return ret;
12312 }
12313 
12314 /**
12315  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12316  * @vsi: Pointer to vsi structure
12317  * @seed: RSS hash seed
12318  * @lut: Lookup table
12319  * @lut_size: Lookup table size
12320  *
12321  * Returns 0 on success, negative on failure
12322  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)12323 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12324 			       const u8 *lut, u16 lut_size)
12325 {
12326 	struct i40e_pf *pf = vsi->back;
12327 	struct i40e_hw *hw = &pf->hw;
12328 	u16 vf_id = vsi->vf_id;
12329 	u8 i;
12330 
12331 	/* Fill out hash function seed */
12332 	if (seed) {
12333 		u32 *seed_dw = (u32 *)seed;
12334 
12335 		if (vsi->type == I40E_VSI_MAIN) {
12336 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12337 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12338 		} else if (vsi->type == I40E_VSI_SRIOV) {
12339 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12340 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12341 		} else {
12342 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12343 		}
12344 	}
12345 
12346 	if (lut) {
12347 		u32 *lut_dw = (u32 *)lut;
12348 
12349 		if (vsi->type == I40E_VSI_MAIN) {
12350 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12351 				return -EINVAL;
12352 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12353 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12354 		} else if (vsi->type == I40E_VSI_SRIOV) {
12355 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12356 				return -EINVAL;
12357 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12358 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12359 		} else {
12360 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12361 		}
12362 	}
12363 	i40e_flush(hw);
12364 
12365 	return 0;
12366 }
12367 
12368 /**
12369  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12370  * @vsi: Pointer to VSI structure
12371  * @seed: Buffer to store the keys
12372  * @lut: Buffer to store the lookup table entries
12373  * @lut_size: Size of buffer to store the lookup table entries
12374  *
12375  * Returns 0 on success, negative on failure
12376  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12377 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12378 			    u8 *lut, u16 lut_size)
12379 {
12380 	struct i40e_pf *pf = vsi->back;
12381 	struct i40e_hw *hw = &pf->hw;
12382 	u16 i;
12383 
12384 	if (seed) {
12385 		u32 *seed_dw = (u32 *)seed;
12386 
12387 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12388 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12389 	}
12390 	if (lut) {
12391 		u32 *lut_dw = (u32 *)lut;
12392 
12393 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12394 			return -EINVAL;
12395 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12396 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12397 	}
12398 
12399 	return 0;
12400 }
12401 
12402 /**
12403  * i40e_config_rss - Configure RSS keys and lut
12404  * @vsi: Pointer to VSI structure
12405  * @seed: RSS hash seed
12406  * @lut: Lookup table
12407  * @lut_size: Lookup table size
12408  *
12409  * Returns 0 on success, negative on failure
12410  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12411 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12412 {
12413 	struct i40e_pf *pf = vsi->back;
12414 
12415 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12416 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12417 	else
12418 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12419 }
12420 
12421 /**
12422  * i40e_get_rss - Get RSS keys and lut
12423  * @vsi: Pointer to VSI structure
12424  * @seed: Buffer to store the keys
12425  * @lut: Buffer to store the lookup table entries
12426  * @lut_size: Size of buffer to store the lookup table entries
12427  *
12428  * Returns 0 on success, negative on failure
12429  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12430 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12431 {
12432 	struct i40e_pf *pf = vsi->back;
12433 
12434 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12435 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12436 	else
12437 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12438 }
12439 
12440 /**
12441  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12442  * @pf: Pointer to board private structure
12443  * @lut: Lookup table
12444  * @rss_table_size: Lookup table size
12445  * @rss_size: Range of queue number for hashing
12446  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)12447 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12448 		       u16 rss_table_size, u16 rss_size)
12449 {
12450 	u16 i;
12451 
12452 	for (i = 0; i < rss_table_size; i++)
12453 		lut[i] = i % rss_size;
12454 }
12455 
12456 /**
12457  * i40e_pf_config_rss - Prepare for RSS if used
12458  * @pf: board private structure
12459  **/
i40e_pf_config_rss(struct i40e_pf * pf)12460 static int i40e_pf_config_rss(struct i40e_pf *pf)
12461 {
12462 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12463 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12464 	u8 *lut;
12465 	struct i40e_hw *hw = &pf->hw;
12466 	u32 reg_val;
12467 	u64 hena;
12468 	int ret;
12469 
12470 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12471 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12472 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12473 	hena |= i40e_pf_get_default_rss_hashcfg(pf);
12474 
12475 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12476 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12477 
12478 	/* Determine the RSS table size based on the hardware capabilities */
12479 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12480 	reg_val = (pf->rss_table_size == 512) ?
12481 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12482 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12483 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12484 
12485 	/* Determine the RSS size of the VSI */
12486 	if (!vsi->rss_size) {
12487 		u16 qcount;
12488 		/* If the firmware does something weird during VSI init, we
12489 		 * could end up with zero TCs. Check for that to avoid
12490 		 * divide-by-zero. It probably won't pass traffic, but it also
12491 		 * won't panic.
12492 		 */
12493 		qcount = vsi->num_queue_pairs /
12494 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12495 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12496 	}
12497 	if (!vsi->rss_size)
12498 		return -EINVAL;
12499 
12500 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12501 	if (!lut)
12502 		return -ENOMEM;
12503 
12504 	/* Use user configured lut if there is one, otherwise use default */
12505 	if (vsi->rss_lut_user)
12506 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12507 	else
12508 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12509 
12510 	/* Use user configured hash key if there is one, otherwise
12511 	 * use default.
12512 	 */
12513 	if (vsi->rss_hkey_user)
12514 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12515 	else
12516 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12517 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12518 	kfree(lut);
12519 
12520 	return ret;
12521 }
12522 
12523 /**
12524  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12525  * @pf: board private structure
12526  * @queue_count: the requested queue count for rss.
12527  *
12528  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12529  * count which may be different from the requested queue count.
12530  * Note: expects to be called while under rtnl_lock()
12531  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)12532 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12533 {
12534 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12535 	int new_rss_size;
12536 
12537 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12538 		return 0;
12539 
12540 	queue_count = min_t(int, queue_count, num_online_cpus());
12541 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12542 
12543 	if (queue_count != vsi->num_queue_pairs) {
12544 		u16 qcount;
12545 
12546 		vsi->req_queue_pairs = queue_count;
12547 		i40e_prep_for_reset(pf);
12548 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12549 			return pf->alloc_rss_size;
12550 
12551 		pf->alloc_rss_size = new_rss_size;
12552 
12553 		i40e_reset_and_rebuild(pf, true, true);
12554 
12555 		/* Discard the user configured hash keys and lut, if less
12556 		 * queues are enabled.
12557 		 */
12558 		if (queue_count < vsi->rss_size) {
12559 			i40e_clear_rss_config_user(vsi);
12560 			dev_dbg(&pf->pdev->dev,
12561 				"discard user configured hash keys and lut\n");
12562 		}
12563 
12564 		/* Reset vsi->rss_size, as number of enabled queues changed */
12565 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12566 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12567 
12568 		i40e_pf_config_rss(pf);
12569 	}
12570 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12571 		 vsi->req_queue_pairs, pf->rss_size_max);
12572 	return pf->alloc_rss_size;
12573 }
12574 
12575 /**
12576  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12577  * @pf: board private structure
12578  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)12579 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12580 {
12581 	bool min_valid, max_valid;
12582 	u32 max_bw, min_bw;
12583 	int status;
12584 
12585 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12586 					   &min_valid, &max_valid);
12587 
12588 	if (!status) {
12589 		if (min_valid)
12590 			pf->min_bw = min_bw;
12591 		if (max_valid)
12592 			pf->max_bw = max_bw;
12593 	}
12594 
12595 	return status;
12596 }
12597 
12598 /**
12599  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12600  * @pf: board private structure
12601  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)12602 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12603 {
12604 	struct i40e_aqc_configure_partition_bw_data bw_data;
12605 	int status;
12606 
12607 	memset(&bw_data, 0, sizeof(bw_data));
12608 
12609 	/* Set the valid bit for this PF */
12610 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12611 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12612 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12613 
12614 	/* Set the new bandwidths */
12615 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12616 
12617 	return status;
12618 }
12619 
12620 /**
12621  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12622  * if total port shutdown feature is enabled for this PF
12623  * @pf: board private structure
12624  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)12625 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12626 {
12627 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12628 #define I40E_FEATURES_ENABLE_PTR		0x2A
12629 #define I40E_CURRENT_SETTING_PTR		0x2B
12630 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12631 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12632 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12633 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12634 	u16 sr_emp_sr_settings_ptr = 0;
12635 	u16 features_enable = 0;
12636 	u16 link_behavior = 0;
12637 	int read_status = 0;
12638 	bool ret = false;
12639 
12640 	read_status = i40e_read_nvm_word(&pf->hw,
12641 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12642 					 &sr_emp_sr_settings_ptr);
12643 	if (read_status)
12644 		goto err_nvm;
12645 	read_status = i40e_read_nvm_word(&pf->hw,
12646 					 sr_emp_sr_settings_ptr +
12647 					 I40E_FEATURES_ENABLE_PTR,
12648 					 &features_enable);
12649 	if (read_status)
12650 		goto err_nvm;
12651 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12652 		read_status = i40e_read_nvm_module_data(&pf->hw,
12653 							I40E_SR_EMP_SR_SETTINGS_PTR,
12654 							I40E_CURRENT_SETTING_PTR,
12655 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12656 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12657 							&link_behavior);
12658 		if (read_status)
12659 			goto err_nvm;
12660 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12661 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12662 	}
12663 	return ret;
12664 
12665 err_nvm:
12666 	dev_warn(&pf->pdev->dev,
12667 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12668 		 ERR_PTR(read_status));
12669 	return ret;
12670 }
12671 
12672 /**
12673  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12674  * @pf: board private structure to initialize
12675  *
12676  * i40e_sw_init initializes the Adapter private data structure.
12677  * Fields are initialized based on PCI device information and
12678  * OS network device settings (MTU size).
12679  **/
i40e_sw_init(struct i40e_pf * pf)12680 static int i40e_sw_init(struct i40e_pf *pf)
12681 {
12682 	int err = 0;
12683 	int size;
12684 	u16 pow;
12685 
12686 	/* Set default capability flags */
12687 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12688 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12689 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12690 
12691 	/* Set default ITR */
12692 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12693 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12694 
12695 	/* Depending on PF configurations, it is possible that the RSS
12696 	 * maximum might end up larger than the available queues
12697 	 */
12698 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12699 	pf->alloc_rss_size = 1;
12700 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12701 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12702 				 pf->hw.func_caps.num_tx_qp);
12703 
12704 	/* find the next higher power-of-2 of num cpus */
12705 	pow = roundup_pow_of_two(num_online_cpus());
12706 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12707 
12708 	if (pf->hw.func_caps.rss) {
12709 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12710 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12711 					   num_online_cpus());
12712 	}
12713 
12714 	/* MFP mode enabled */
12715 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12716 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12717 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12718 		if (i40e_get_partition_bw_setting(pf)) {
12719 			dev_warn(&pf->pdev->dev,
12720 				 "Could not get partition bw settings\n");
12721 		} else {
12722 			dev_info(&pf->pdev->dev,
12723 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12724 				 pf->min_bw, pf->max_bw);
12725 
12726 			/* nudge the Tx scheduler */
12727 			i40e_set_partition_bw_setting(pf);
12728 		}
12729 	}
12730 
12731 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12732 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12733 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12734 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12735 		    pf->hw.num_partitions > 1)
12736 			dev_info(&pf->pdev->dev,
12737 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12738 		else
12739 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12740 		pf->fdir_pf_filter_count =
12741 				 pf->hw.func_caps.fd_filters_guaranteed;
12742 		pf->hw.fdir_shared_filter_count =
12743 				 pf->hw.func_caps.fd_filters_best_effort;
12744 	}
12745 
12746 	/* Enable HW ATR eviction if possible */
12747 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12748 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12749 
12750 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12751 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12752 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12753 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12754 	}
12755 
12756 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12757 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12758 		/* IWARP needs one extra vector for CQP just like MISC.*/
12759 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12760 	}
12761 	/* Stopping FW LLDP engine is supported on XL710 and X722
12762 	 * starting from FW versions determined in i40e_init_adminq.
12763 	 * Stopping the FW LLDP engine is not supported on XL710
12764 	 * if NPAR is functioning so unset this hw flag in this case.
12765 	 */
12766 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12767 	    pf->hw.func_caps.npar_enable)
12768 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12769 
12770 #ifdef CONFIG_PCI_IOV
12771 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12772 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12773 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12774 		pf->num_req_vfs = min_t(int,
12775 					pf->hw.func_caps.num_vfs,
12776 					I40E_MAX_VF_COUNT);
12777 	}
12778 #endif /* CONFIG_PCI_IOV */
12779 	pf->lan_veb = I40E_NO_VEB;
12780 	pf->lan_vsi = I40E_NO_VSI;
12781 
12782 	/* By default FW has this off for performance reasons */
12783 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12784 
12785 	/* set up queue assignment tracking */
12786 	size = sizeof(struct i40e_lump_tracking)
12787 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12788 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12789 	if (!pf->qp_pile) {
12790 		err = -ENOMEM;
12791 		goto sw_init_done;
12792 	}
12793 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12794 
12795 	pf->tx_timeout_recovery_level = 1;
12796 
12797 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12798 	    i40e_is_total_port_shutdown_enabled(pf)) {
12799 		/* Link down on close must be on when total port shutdown
12800 		 * is enabled for a given port
12801 		 */
12802 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12803 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12804 		dev_info(&pf->pdev->dev,
12805 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12806 	}
12807 	mutex_init(&pf->switch_mutex);
12808 
12809 sw_init_done:
12810 	return err;
12811 }
12812 
12813 /**
12814  * i40e_set_ntuple - set the ntuple feature flag and take action
12815  * @pf: board private structure to initialize
12816  * @features: the feature set that the stack is suggesting
12817  *
12818  * returns a bool to indicate if reset needs to happen
12819  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12820 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12821 {
12822 	bool need_reset = false;
12823 
12824 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12825 	 * the state changed, we need to reset.
12826 	 */
12827 	if (features & NETIF_F_NTUPLE) {
12828 		/* Enable filters and mark for reset */
12829 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12830 			need_reset = true;
12831 		/* enable FD_SB only if there is MSI-X vector and no cloud
12832 		 * filters exist
12833 		 */
12834 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12835 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12836 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12837 		}
12838 	} else {
12839 		/* turn off filters, mark for reset and clear SW filter list */
12840 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12841 			need_reset = true;
12842 			i40e_fdir_filter_exit(pf);
12843 		}
12844 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12845 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12846 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12847 
12848 		/* reset fd counters */
12849 		pf->fd_add_err = 0;
12850 		pf->fd_atr_cnt = 0;
12851 		/* if ATR was auto disabled it can be re-enabled. */
12852 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12853 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12854 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12855 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12856 	}
12857 	return need_reset;
12858 }
12859 
12860 /**
12861  * i40e_clear_rss_lut - clear the rx hash lookup table
12862  * @vsi: the VSI being configured
12863  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12864 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12865 {
12866 	struct i40e_pf *pf = vsi->back;
12867 	struct i40e_hw *hw = &pf->hw;
12868 	u16 vf_id = vsi->vf_id;
12869 	u8 i;
12870 
12871 	if (vsi->type == I40E_VSI_MAIN) {
12872 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12873 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12874 	} else if (vsi->type == I40E_VSI_SRIOV) {
12875 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12876 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12877 	} else {
12878 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12879 	}
12880 }
12881 
12882 /**
12883  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12884  * @vsi: ptr to VSI
12885  * @ena: flag to indicate the on/off setting
12886  */
i40e_set_loopback(struct i40e_vsi * vsi,bool ena)12887 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12888 {
12889 	bool if_running = netif_running(vsi->netdev) &&
12890 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12891 	int ret;
12892 
12893 	if (if_running)
12894 		i40e_down(vsi);
12895 
12896 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12897 	if (ret)
12898 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12899 	if (if_running)
12900 		i40e_up(vsi);
12901 
12902 	return ret;
12903 }
12904 
12905 /**
12906  * i40e_set_features - set the netdev feature flags
12907  * @netdev: ptr to the netdev being adjusted
12908  * @features: the feature set that the stack is suggesting
12909  * Note: expects to be called while under rtnl_lock()
12910  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)12911 static int i40e_set_features(struct net_device *netdev,
12912 			     netdev_features_t features)
12913 {
12914 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12915 	struct i40e_vsi *vsi = np->vsi;
12916 	struct i40e_pf *pf = vsi->back;
12917 	bool need_reset;
12918 
12919 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12920 		i40e_pf_config_rss(pf);
12921 	else if (!(features & NETIF_F_RXHASH) &&
12922 		 netdev->features & NETIF_F_RXHASH)
12923 		i40e_clear_rss_lut(vsi);
12924 
12925 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12926 		i40e_vlan_stripping_enable(vsi);
12927 	else
12928 		i40e_vlan_stripping_disable(vsi);
12929 
12930 	if (!(features & NETIF_F_HW_TC) &&
12931 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12932 		dev_err(&pf->pdev->dev,
12933 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12934 		return -EINVAL;
12935 	}
12936 
12937 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12938 		i40e_del_all_macvlans(vsi);
12939 
12940 	need_reset = i40e_set_ntuple(pf, features);
12941 
12942 	if (need_reset)
12943 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12944 
12945 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
12946 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
12947 
12948 	return 0;
12949 }
12950 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12951 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12952 				    unsigned int table, unsigned int idx,
12953 				    struct udp_tunnel_info *ti)
12954 {
12955 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12956 	struct i40e_hw *hw = &np->vsi->back->hw;
12957 	u8 type, filter_index;
12958 	int ret;
12959 
12960 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12961 						   I40E_AQC_TUNNEL_TYPE_NGE;
12962 
12963 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12964 				     NULL);
12965 	if (ret) {
12966 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
12967 			    ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
12968 		return -EIO;
12969 	}
12970 
12971 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12972 	return 0;
12973 }
12974 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12975 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12976 				      unsigned int table, unsigned int idx,
12977 				      struct udp_tunnel_info *ti)
12978 {
12979 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12980 	struct i40e_hw *hw = &np->vsi->back->hw;
12981 	int ret;
12982 
12983 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12984 	if (ret) {
12985 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
12986 			    ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
12987 		return -EIO;
12988 	}
12989 
12990 	return 0;
12991 }
12992 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)12993 static int i40e_get_phys_port_id(struct net_device *netdev,
12994 				 struct netdev_phys_item_id *ppid)
12995 {
12996 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12997 	struct i40e_pf *pf = np->vsi->back;
12998 	struct i40e_hw *hw = &pf->hw;
12999 
13000 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
13001 		return -EOPNOTSUPP;
13002 
13003 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13004 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13005 
13006 	return 0;
13007 }
13008 
13009 /**
13010  * i40e_ndo_fdb_add - add an entry to the hardware database
13011  * @ndm: the input from the stack
13012  * @tb: pointer to array of nladdr (unused)
13013  * @dev: the net device pointer
13014  * @addr: the MAC address entry being added
13015  * @vid: VLAN ID
13016  * @flags: instructions from stack about fdb operation
13017  * @notified: whether notification was emitted
13018  * @extack: netlink extended ack, unused currently
13019  */
i40e_ndo_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags,bool * notified,struct netlink_ext_ack * extack)13020 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13021 			    struct net_device *dev,
13022 			    const unsigned char *addr, u16 vid,
13023 			    u16 flags, bool *notified,
13024 			    struct netlink_ext_ack *extack)
13025 {
13026 	struct i40e_netdev_priv *np = netdev_priv(dev);
13027 	struct i40e_pf *pf = np->vsi->back;
13028 	int err = 0;
13029 
13030 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13031 		return -EOPNOTSUPP;
13032 
13033 	if (vid) {
13034 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13035 		return -EINVAL;
13036 	}
13037 
13038 	/* Hardware does not support aging addresses so if a
13039 	 * ndm_state is given only allow permanent addresses
13040 	 */
13041 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13042 		netdev_info(dev, "FDB only supports static addresses\n");
13043 		return -EINVAL;
13044 	}
13045 
13046 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13047 		err = dev_uc_add_excl(dev, addr);
13048 	else if (is_multicast_ether_addr(addr))
13049 		err = dev_mc_add_excl(dev, addr);
13050 	else
13051 		err = -EINVAL;
13052 
13053 	/* Only return duplicate errors if NLM_F_EXCL is set */
13054 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13055 		err = 0;
13056 
13057 	return err;
13058 }
13059 
13060 /**
13061  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13062  * @dev: the netdev being configured
13063  * @nlh: RTNL message
13064  * @flags: bridge flags
13065  * @extack: netlink extended ack
13066  *
13067  * Inserts a new hardware bridge if not already created and
13068  * enables the bridging mode requested (VEB or VEPA). If the
13069  * hardware bridge has already been inserted and the request
13070  * is to change the mode then that requires a PF reset to
13071  * allow rebuild of the components with required hardware
13072  * bridge mode enabled.
13073  *
13074  * Note: expects to be called while under rtnl_lock()
13075  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)13076 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13077 				   struct nlmsghdr *nlh,
13078 				   u16 flags,
13079 				   struct netlink_ext_ack *extack)
13080 {
13081 	struct i40e_netdev_priv *np = netdev_priv(dev);
13082 	struct i40e_vsi *vsi = np->vsi;
13083 	struct i40e_pf *pf = vsi->back;
13084 	struct nlattr *attr, *br_spec;
13085 	struct i40e_veb *veb;
13086 	int rem;
13087 
13088 	/* Only for PF VSI for now */
13089 	if (vsi->type != I40E_VSI_MAIN)
13090 		return -EOPNOTSUPP;
13091 
13092 	/* Find the HW bridge for PF VSI */
13093 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13094 
13095 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13096 	if (!br_spec)
13097 		return -EINVAL;
13098 
13099 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
13100 		__u16 mode = nla_get_u16(attr);
13101 
13102 		if ((mode != BRIDGE_MODE_VEPA) &&
13103 		    (mode != BRIDGE_MODE_VEB))
13104 			return -EINVAL;
13105 
13106 		/* Insert a new HW bridge */
13107 		if (!veb) {
13108 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
13109 					     vsi->tc_config.enabled_tc);
13110 			if (veb) {
13111 				veb->bridge_mode = mode;
13112 				i40e_config_bridge_mode(veb);
13113 			} else {
13114 				/* No Bridge HW offload available */
13115 				return -ENOENT;
13116 			}
13117 			break;
13118 		} else if (mode != veb->bridge_mode) {
13119 			/* Existing HW bridge but different mode needs reset */
13120 			veb->bridge_mode = mode;
13121 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13122 			if (mode == BRIDGE_MODE_VEB)
13123 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13124 			else
13125 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13126 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13127 			break;
13128 		}
13129 	}
13130 
13131 	return 0;
13132 }
13133 
13134 /**
13135  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13136  * @skb: skb buff
13137  * @pid: process id
13138  * @seq: RTNL message seq #
13139  * @dev: the netdev being configured
13140  * @filter_mask: unused
13141  * @nlflags: netlink flags passed in
13142  *
13143  * Return the mode in which the hardware bridge is operating in
13144  * i.e VEB or VEPA.
13145  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)13146 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13147 				   struct net_device *dev,
13148 				   u32 __always_unused filter_mask,
13149 				   int nlflags)
13150 {
13151 	struct i40e_netdev_priv *np = netdev_priv(dev);
13152 	struct i40e_vsi *vsi = np->vsi;
13153 	struct i40e_pf *pf = vsi->back;
13154 	struct i40e_veb *veb;
13155 
13156 	/* Only for PF VSI for now */
13157 	if (vsi->type != I40E_VSI_MAIN)
13158 		return -EOPNOTSUPP;
13159 
13160 	/* Find the HW bridge for the PF VSI */
13161 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13162 	if (!veb)
13163 		return 0;
13164 
13165 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13166 				       0, 0, nlflags, filter_mask, NULL);
13167 }
13168 
13169 /**
13170  * i40e_features_check - Validate encapsulated packet conforms to limits
13171  * @skb: skb buff
13172  * @dev: This physical port's netdev
13173  * @features: Offload features that the stack believes apply
13174  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13175 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13176 					     struct net_device *dev,
13177 					     netdev_features_t features)
13178 {
13179 	size_t len;
13180 
13181 	/* No point in doing any of this if neither checksum nor GSO are
13182 	 * being requested for this frame.  We can rule out both by just
13183 	 * checking for CHECKSUM_PARTIAL
13184 	 */
13185 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13186 		return features;
13187 
13188 	/* We cannot support GSO if the MSS is going to be less than
13189 	 * 64 bytes.  If it is then we need to drop support for GSO.
13190 	 */
13191 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13192 		features &= ~NETIF_F_GSO_MASK;
13193 
13194 	/* MACLEN can support at most 63 words */
13195 	len = skb_network_offset(skb);
13196 	if (len & ~(63 * 2))
13197 		goto out_err;
13198 
13199 	/* IPLEN and EIPLEN can support at most 127 dwords */
13200 	len = skb_network_header_len(skb);
13201 	if (len & ~(127 * 4))
13202 		goto out_err;
13203 
13204 	if (skb->encapsulation) {
13205 		/* L4TUNLEN can support 127 words */
13206 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13207 		if (len & ~(127 * 2))
13208 			goto out_err;
13209 
13210 		/* IPLEN can support at most 127 dwords */
13211 		len = skb_inner_transport_header(skb) -
13212 		      skb_inner_network_header(skb);
13213 		if (len & ~(127 * 4))
13214 			goto out_err;
13215 	}
13216 
13217 	/* No need to validate L4LEN as TCP is the only protocol with a
13218 	 * flexible value and we support all possible values supported
13219 	 * by TCP, which is at most 15 dwords
13220 	 */
13221 
13222 	return features;
13223 out_err:
13224 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13225 }
13226 
13227 /**
13228  * i40e_xdp_setup - add/remove an XDP program
13229  * @vsi: VSI to changed
13230  * @prog: XDP program
13231  * @extack: netlink extended ack
13232  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)13233 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13234 			  struct netlink_ext_ack *extack)
13235 {
13236 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13237 	struct i40e_pf *pf = vsi->back;
13238 	struct bpf_prog *old_prog;
13239 	bool need_reset;
13240 	int i;
13241 
13242 	/* VSI shall be deleted in a moment, block loading new programs */
13243 	if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
13244 		return -EINVAL;
13245 
13246 	/* Don't allow frames that span over multiple buffers */
13247 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13248 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13249 		return -EINVAL;
13250 	}
13251 
13252 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13253 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13254 	if (need_reset)
13255 		i40e_prep_for_reset(pf);
13256 
13257 	old_prog = xchg(&vsi->xdp_prog, prog);
13258 
13259 	if (need_reset) {
13260 		if (!prog) {
13261 			xdp_features_clear_redirect_target(vsi->netdev);
13262 			/* Wait until ndo_xsk_wakeup completes. */
13263 			synchronize_rcu();
13264 		}
13265 		i40e_reset_and_rebuild(pf, true, true);
13266 	}
13267 
13268 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13269 		if (i40e_realloc_rx_bi_zc(vsi, true))
13270 			return -ENOMEM;
13271 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13272 		if (i40e_realloc_rx_bi_zc(vsi, false))
13273 			return -ENOMEM;
13274 	}
13275 
13276 	for (i = 0; i < vsi->num_queue_pairs; i++)
13277 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13278 
13279 	if (old_prog)
13280 		bpf_prog_put(old_prog);
13281 
13282 	/* Kick start the NAPI context if there is an AF_XDP socket open
13283 	 * on that queue id. This so that receiving will start.
13284 	 */
13285 	if (need_reset && prog) {
13286 		for (i = 0; i < vsi->num_queue_pairs; i++)
13287 			if (vsi->xdp_rings[i]->xsk_pool)
13288 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13289 						      XDP_WAKEUP_RX);
13290 		xdp_features_set_redirect_target(vsi->netdev, true);
13291 	}
13292 
13293 	return 0;
13294 }
13295 
13296 /**
13297  * i40e_enter_busy_conf - Enters busy config state
13298  * @vsi: vsi
13299  *
13300  * Returns 0 on success, <0 for failure.
13301  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)13302 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13303 {
13304 	struct i40e_pf *pf = vsi->back;
13305 	int timeout = 50;
13306 
13307 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13308 		timeout--;
13309 		if (!timeout)
13310 			return -EBUSY;
13311 		usleep_range(1000, 2000);
13312 	}
13313 
13314 	return 0;
13315 }
13316 
13317 /**
13318  * i40e_exit_busy_conf - Exits busy config state
13319  * @vsi: vsi
13320  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)13321 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13322 {
13323 	struct i40e_pf *pf = vsi->back;
13324 
13325 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13326 }
13327 
13328 /**
13329  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13330  * @vsi: vsi
13331  * @queue_pair: queue pair
13332  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)13333 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13334 {
13335 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13336 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13337 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13338 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13339 	if (i40e_enabled_xdp_vsi(vsi)) {
13340 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13341 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13342 	}
13343 }
13344 
13345 /**
13346  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13347  * @vsi: vsi
13348  * @queue_pair: queue pair
13349  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)13350 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13351 {
13352 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13353 	if (i40e_enabled_xdp_vsi(vsi)) {
13354 		/* Make sure that in-progress ndo_xdp_xmit calls are
13355 		 * completed.
13356 		 */
13357 		synchronize_rcu();
13358 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13359 	}
13360 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13361 }
13362 
13363 /**
13364  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13365  * @vsi: vsi
13366  * @queue_pair: queue pair
13367  * @enable: true for enable, false for disable
13368  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)13369 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13370 					bool enable)
13371 {
13372 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13373 	struct i40e_q_vector *q_vector = rxr->q_vector;
13374 
13375 	if (!vsi->netdev)
13376 		return;
13377 
13378 	/* All rings in a qp belong to the same qvector. */
13379 	if (q_vector->rx.ring || q_vector->tx.ring) {
13380 		if (enable)
13381 			napi_enable(&q_vector->napi);
13382 		else
13383 			napi_disable(&q_vector->napi);
13384 	}
13385 }
13386 
13387 /**
13388  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13389  * @vsi: vsi
13390  * @queue_pair: queue pair
13391  * @enable: true for enable, false for disable
13392  *
13393  * Returns 0 on success, <0 on failure.
13394  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)13395 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13396 					bool enable)
13397 {
13398 	struct i40e_pf *pf = vsi->back;
13399 	int pf_q, ret = 0;
13400 
13401 	pf_q = vsi->base_queue + queue_pair;
13402 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13403 				     false /*is xdp*/, enable);
13404 	if (ret) {
13405 		dev_info(&pf->pdev->dev,
13406 			 "VSI seid %d Tx ring %d %sable timeout\n",
13407 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13408 		return ret;
13409 	}
13410 
13411 	i40e_control_rx_q(pf, pf_q, enable);
13412 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13413 	if (ret) {
13414 		dev_info(&pf->pdev->dev,
13415 			 "VSI seid %d Rx ring %d %sable timeout\n",
13416 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13417 		return ret;
13418 	}
13419 
13420 	/* Due to HW errata, on Rx disable only, the register can
13421 	 * indicate done before it really is. Needs 50ms to be sure
13422 	 */
13423 	if (!enable)
13424 		mdelay(50);
13425 
13426 	if (!i40e_enabled_xdp_vsi(vsi))
13427 		return ret;
13428 
13429 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13430 				     pf_q + vsi->alloc_queue_pairs,
13431 				     true /*is xdp*/, enable);
13432 	if (ret) {
13433 		dev_info(&pf->pdev->dev,
13434 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13435 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13436 	}
13437 
13438 	return ret;
13439 }
13440 
13441 /**
13442  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13443  * @vsi: vsi
13444  * @queue_pair: queue_pair
13445  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)13446 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13447 {
13448 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13449 	struct i40e_pf *pf = vsi->back;
13450 	struct i40e_hw *hw = &pf->hw;
13451 
13452 	/* All rings in a qp belong to the same qvector. */
13453 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13454 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13455 	else
13456 		i40e_irq_dynamic_enable_icr0(pf);
13457 
13458 	i40e_flush(hw);
13459 }
13460 
13461 /**
13462  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13463  * @vsi: vsi
13464  * @queue_pair: queue_pair
13465  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)13466 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13467 {
13468 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13469 	struct i40e_pf *pf = vsi->back;
13470 	struct i40e_hw *hw = &pf->hw;
13471 
13472 	/* For simplicity, instead of removing the qp interrupt causes
13473 	 * from the interrupt linked list, we simply disable the interrupt, and
13474 	 * leave the list intact.
13475 	 *
13476 	 * All rings in a qp belong to the same qvector.
13477 	 */
13478 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13479 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13480 
13481 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13482 		i40e_flush(hw);
13483 		synchronize_irq(pf->msix_entries[intpf].vector);
13484 	} else {
13485 		/* Legacy and MSI mode - this stops all interrupt handling */
13486 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13487 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13488 		i40e_flush(hw);
13489 		synchronize_irq(pf->pdev->irq);
13490 	}
13491 }
13492 
13493 /**
13494  * i40e_queue_pair_disable - Disables a queue pair
13495  * @vsi: vsi
13496  * @queue_pair: queue pair
13497  *
13498  * Returns 0 on success, <0 on failure.
13499  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)13500 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13501 {
13502 	int err;
13503 
13504 	err = i40e_enter_busy_conf(vsi);
13505 	if (err)
13506 		return err;
13507 
13508 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13509 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13510 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13511 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13512 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13513 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13514 
13515 	return err;
13516 }
13517 
13518 /**
13519  * i40e_queue_pair_enable - Enables a queue pair
13520  * @vsi: vsi
13521  * @queue_pair: queue pair
13522  *
13523  * Returns 0 on success, <0 on failure.
13524  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)13525 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13526 {
13527 	int err;
13528 
13529 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13530 	if (err)
13531 		return err;
13532 
13533 	if (i40e_enabled_xdp_vsi(vsi)) {
13534 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13535 		if (err)
13536 			return err;
13537 	}
13538 
13539 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13540 	if (err)
13541 		return err;
13542 
13543 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13544 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13545 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13546 
13547 	i40e_exit_busy_conf(vsi);
13548 
13549 	return err;
13550 }
13551 
13552 /**
13553  * i40e_xdp - implements ndo_bpf for i40e
13554  * @dev: netdevice
13555  * @xdp: XDP command
13556  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)13557 static int i40e_xdp(struct net_device *dev,
13558 		    struct netdev_bpf *xdp)
13559 {
13560 	struct i40e_netdev_priv *np = netdev_priv(dev);
13561 	struct i40e_vsi *vsi = np->vsi;
13562 
13563 	if (vsi->type != I40E_VSI_MAIN)
13564 		return -EINVAL;
13565 
13566 	switch (xdp->command) {
13567 	case XDP_SETUP_PROG:
13568 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13569 	case XDP_SETUP_XSK_POOL:
13570 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13571 					   xdp->xsk.queue_id);
13572 	default:
13573 		return -EINVAL;
13574 	}
13575 }
13576 
13577 static const struct net_device_ops i40e_netdev_ops = {
13578 	.ndo_open		= i40e_open,
13579 	.ndo_stop		= i40e_close,
13580 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13581 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13582 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13583 	.ndo_validate_addr	= eth_validate_addr,
13584 	.ndo_set_mac_address	= i40e_set_mac,
13585 	.ndo_change_mtu		= i40e_change_mtu,
13586 	.ndo_tx_timeout		= i40e_tx_timeout,
13587 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13588 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13589 #ifdef CONFIG_NET_POLL_CONTROLLER
13590 	.ndo_poll_controller	= i40e_netpoll,
13591 #endif
13592 	.ndo_setup_tc		= __i40e_setup_tc,
13593 	.ndo_select_queue	= i40e_lan_select_queue,
13594 	.ndo_set_features	= i40e_set_features,
13595 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13596 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13597 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13598 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13599 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13600 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13601 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13602 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13603 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13604 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13605 	.ndo_features_check	= i40e_features_check,
13606 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13607 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13608 	.ndo_bpf		= i40e_xdp,
13609 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13610 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13611 	.ndo_dfwd_add_station	= i40e_fwd_add,
13612 	.ndo_dfwd_del_station	= i40e_fwd_del,
13613 	.ndo_hwtstamp_get	= i40e_ptp_hwtstamp_get,
13614 	.ndo_hwtstamp_set	= i40e_ptp_hwtstamp_set,
13615 };
13616 
13617 /**
13618  * i40e_config_netdev - Setup the netdev flags
13619  * @vsi: the VSI being configured
13620  *
13621  * Returns 0 on success, negative value on failure
13622  **/
i40e_config_netdev(struct i40e_vsi * vsi)13623 static int i40e_config_netdev(struct i40e_vsi *vsi)
13624 {
13625 	struct i40e_pf *pf = vsi->back;
13626 	struct i40e_hw *hw = &pf->hw;
13627 	struct i40e_netdev_priv *np;
13628 	struct net_device *netdev;
13629 	u8 broadcast[ETH_ALEN];
13630 	u8 mac_addr[ETH_ALEN];
13631 	int etherdev_size;
13632 	netdev_features_t hw_enc_features;
13633 	netdev_features_t hw_features;
13634 
13635 	etherdev_size = sizeof(struct i40e_netdev_priv);
13636 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13637 	if (!netdev)
13638 		return -ENOMEM;
13639 
13640 	vsi->netdev = netdev;
13641 	np = netdev_priv(netdev);
13642 	np->vsi = vsi;
13643 
13644 	hw_enc_features = NETIF_F_SG			|
13645 			  NETIF_F_HW_CSUM		|
13646 			  NETIF_F_HIGHDMA		|
13647 			  NETIF_F_SOFT_FEATURES		|
13648 			  NETIF_F_TSO			|
13649 			  NETIF_F_TSO_ECN		|
13650 			  NETIF_F_TSO6			|
13651 			  NETIF_F_GSO_GRE		|
13652 			  NETIF_F_GSO_GRE_CSUM		|
13653 			  NETIF_F_GSO_PARTIAL		|
13654 			  NETIF_F_GSO_IPXIP4		|
13655 			  NETIF_F_GSO_IPXIP6		|
13656 			  NETIF_F_GSO_UDP_TUNNEL	|
13657 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13658 			  NETIF_F_GSO_UDP_L4		|
13659 			  NETIF_F_SCTP_CRC		|
13660 			  NETIF_F_RXHASH		|
13661 			  NETIF_F_RXCSUM		|
13662 			  0;
13663 
13664 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13665 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13666 
13667 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13668 
13669 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13670 
13671 	netdev->hw_enc_features |= hw_enc_features;
13672 
13673 	/* record features VLANs can make use of */
13674 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13675 
13676 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13677 				   NETIF_F_GSO_GRE_CSUM |	\
13678 				   NETIF_F_GSO_IPXIP4 |		\
13679 				   NETIF_F_GSO_IPXIP6 |		\
13680 				   NETIF_F_GSO_UDP_TUNNEL |	\
13681 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13682 
13683 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13684 	netdev->features |= NETIF_F_GSO_PARTIAL |
13685 			    I40E_GSO_PARTIAL_FEATURES;
13686 
13687 	netdev->mpls_features |= NETIF_F_SG;
13688 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13689 	netdev->mpls_features |= NETIF_F_TSO;
13690 	netdev->mpls_features |= NETIF_F_TSO6;
13691 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13692 
13693 	/* enable macvlan offloads */
13694 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13695 
13696 	hw_features = hw_enc_features		|
13697 		      NETIF_F_HW_VLAN_CTAG_TX	|
13698 		      NETIF_F_HW_VLAN_CTAG_RX;
13699 
13700 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13701 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13702 
13703 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13704 
13705 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13706 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13707 
13708 	netdev->features &= ~NETIF_F_HW_TC;
13709 
13710 	if (vsi->type == I40E_VSI_MAIN) {
13711 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13712 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13713 		/* The following steps are necessary for two reasons. First,
13714 		 * some older NVM configurations load a default MAC-VLAN
13715 		 * filter that will accept any tagged packet, and we want to
13716 		 * replace this with a normal filter. Additionally, it is
13717 		 * possible our MAC address was provided by the platform using
13718 		 * Open Firmware or similar.
13719 		 *
13720 		 * Thus, we need to remove the default filter and install one
13721 		 * specific to the MAC address.
13722 		 */
13723 		i40e_rm_default_mac_filter(vsi, mac_addr);
13724 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13725 		i40e_add_mac_filter(vsi, mac_addr);
13726 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13727 
13728 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13729 				       NETDEV_XDP_ACT_REDIRECT |
13730 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13731 				       NETDEV_XDP_ACT_RX_SG;
13732 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13733 	} else {
13734 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13735 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13736 		 * the end, which is 4 bytes long, so force truncation of the
13737 		 * original name by IFNAMSIZ - 4
13738 		 */
13739 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
13740 
13741 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d", IFNAMSIZ - 4,
13742 			 main_vsi->netdev->name);
13743 		eth_random_addr(mac_addr);
13744 
13745 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13746 		i40e_add_mac_filter(vsi, mac_addr);
13747 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13748 	}
13749 
13750 	/* Add the broadcast filter so that we initially will receive
13751 	 * broadcast packets. Note that when a new VLAN is first added the
13752 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13753 	 * specific filters as part of transitioning into "vlan" operation.
13754 	 * When more VLANs are added, the driver will copy each existing MAC
13755 	 * filter and add it for the new VLAN.
13756 	 *
13757 	 * Broadcast filters are handled specially by
13758 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13759 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13760 	 * filter. The subtask will update the correct broadcast promiscuous
13761 	 * bits as VLANs become active or inactive.
13762 	 */
13763 	eth_broadcast_addr(broadcast);
13764 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13765 	i40e_add_mac_filter(vsi, broadcast);
13766 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13767 
13768 	eth_hw_addr_set(netdev, mac_addr);
13769 	ether_addr_copy(netdev->perm_addr, mac_addr);
13770 
13771 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13772 	netdev->neigh_priv_len = sizeof(u32) * 4;
13773 
13774 	netdev->priv_flags |= IFF_UNICAST_FLT;
13775 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13776 	/* Setup netdev TC information */
13777 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13778 
13779 	netdev->netdev_ops = &i40e_netdev_ops;
13780 	netdev->watchdog_timeo = 5 * HZ;
13781 	i40e_set_ethtool_ops(netdev);
13782 
13783 	/* MTU range: 68 - 9706 */
13784 	netdev->min_mtu = ETH_MIN_MTU;
13785 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13786 
13787 	return 0;
13788 }
13789 
13790 /**
13791  * i40e_vsi_delete - Delete a VSI from the switch
13792  * @vsi: the VSI being removed
13793  *
13794  * Returns 0 on success, negative value on failure
13795  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13796 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13797 {
13798 	/* remove default VSI is not allowed */
13799 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13800 		return;
13801 
13802 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13803 }
13804 
13805 /**
13806  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13807  * @vsi: the VSI being queried
13808  *
13809  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13810  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13811 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13812 {
13813 	struct i40e_veb *veb;
13814 	struct i40e_pf *pf = vsi->back;
13815 
13816 	/* Uplink is not a bridge so default to VEB */
13817 	if (vsi->veb_idx >= I40E_MAX_VEB)
13818 		return 1;
13819 
13820 	veb = pf->veb[vsi->veb_idx];
13821 	if (!veb) {
13822 		dev_info(&pf->pdev->dev,
13823 			 "There is no veb associated with the bridge\n");
13824 		return -ENOENT;
13825 	}
13826 
13827 	/* Uplink is a bridge in VEPA mode */
13828 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13829 		return 0;
13830 	} else {
13831 		/* Uplink is a bridge in VEB mode */
13832 		return 1;
13833 	}
13834 
13835 	/* VEPA is now default bridge, so return 0 */
13836 	return 0;
13837 }
13838 
13839 /**
13840  * i40e_add_vsi - Add a VSI to the switch
13841  * @vsi: the VSI being configured
13842  *
13843  * This initializes a VSI context depending on the VSI type to be added and
13844  * passes it down to the add_vsi aq command.
13845  **/
i40e_add_vsi(struct i40e_vsi * vsi)13846 static int i40e_add_vsi(struct i40e_vsi *vsi)
13847 {
13848 	int ret = -ENODEV;
13849 	struct i40e_pf *pf = vsi->back;
13850 	struct i40e_hw *hw = &pf->hw;
13851 	struct i40e_vsi_context ctxt;
13852 	struct i40e_mac_filter *f;
13853 	struct hlist_node *h;
13854 	int bkt;
13855 
13856 	u8 enabled_tc = 0x1; /* TC0 enabled */
13857 	int f_count = 0;
13858 
13859 	memset(&ctxt, 0, sizeof(ctxt));
13860 	switch (vsi->type) {
13861 	case I40E_VSI_MAIN:
13862 		/* The PF's main VSI is already setup as part of the
13863 		 * device initialization, so we'll not bother with
13864 		 * the add_vsi call, but we will retrieve the current
13865 		 * VSI context.
13866 		 */
13867 		ctxt.seid = pf->main_vsi_seid;
13868 		ctxt.pf_num = pf->hw.pf_id;
13869 		ctxt.vf_num = 0;
13870 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13871 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13872 		if (ret) {
13873 			dev_info(&pf->pdev->dev,
13874 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13875 				 ERR_PTR(ret),
13876 				 libie_aq_str(pf->hw.aq.asq_last_status));
13877 			return -ENOENT;
13878 		}
13879 		vsi->info = ctxt.info;
13880 		vsi->info.valid_sections = 0;
13881 
13882 		vsi->seid = ctxt.seid;
13883 		vsi->id = ctxt.vsi_number;
13884 
13885 		enabled_tc = i40e_pf_get_tc_map(pf);
13886 
13887 		/* Source pruning is enabled by default, so the flag is
13888 		 * negative logic - if it's set, we need to fiddle with
13889 		 * the VSI to disable source pruning.
13890 		 */
13891 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13892 			memset(&ctxt, 0, sizeof(ctxt));
13893 			ctxt.seid = pf->main_vsi_seid;
13894 			ctxt.pf_num = pf->hw.pf_id;
13895 			ctxt.vf_num = 0;
13896 			ctxt.info.valid_sections |=
13897 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13898 			ctxt.info.switch_id =
13899 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13900 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13901 			if (ret) {
13902 				dev_info(&pf->pdev->dev,
13903 					 "update vsi failed, err %d aq_err %s\n",
13904 					 ret,
13905 					 libie_aq_str(pf->hw.aq.asq_last_status));
13906 				ret = -ENOENT;
13907 				goto err;
13908 			}
13909 		}
13910 
13911 		/* MFP mode setup queue map and update VSI */
13912 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
13913 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13914 			memset(&ctxt, 0, sizeof(ctxt));
13915 			ctxt.seid = pf->main_vsi_seid;
13916 			ctxt.pf_num = pf->hw.pf_id;
13917 			ctxt.vf_num = 0;
13918 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13919 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13920 			if (ret) {
13921 				dev_info(&pf->pdev->dev,
13922 					 "update vsi failed, err %pe aq_err %s\n",
13923 					 ERR_PTR(ret),
13924 					 libie_aq_str(pf->hw.aq.asq_last_status));
13925 				ret = -ENOENT;
13926 				goto err;
13927 			}
13928 			/* update the local VSI info queue map */
13929 			i40e_vsi_update_queue_map(vsi, &ctxt);
13930 			vsi->info.valid_sections = 0;
13931 		} else {
13932 			/* Default/Main VSI is only enabled for TC0
13933 			 * reconfigure it to enable all TCs that are
13934 			 * available on the port in SFP mode.
13935 			 * For MFP case the iSCSI PF would use this
13936 			 * flow to enable LAN+iSCSI TC.
13937 			 */
13938 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13939 			if (ret) {
13940 				/* Single TC condition is not fatal,
13941 				 * message and continue
13942 				 */
13943 				dev_info(&pf->pdev->dev,
13944 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
13945 					 enabled_tc,
13946 					 ERR_PTR(ret),
13947 					 libie_aq_str(pf->hw.aq.asq_last_status));
13948 			}
13949 		}
13950 		break;
13951 
13952 	case I40E_VSI_FDIR:
13953 		ctxt.pf_num = hw->pf_id;
13954 		ctxt.vf_num = 0;
13955 		ctxt.uplink_seid = vsi->uplink_seid;
13956 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13957 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13958 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
13959 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13960 			ctxt.info.valid_sections |=
13961 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13962 			ctxt.info.switch_id =
13963 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13964 		}
13965 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13966 		break;
13967 
13968 	case I40E_VSI_VMDQ2:
13969 		ctxt.pf_num = hw->pf_id;
13970 		ctxt.vf_num = 0;
13971 		ctxt.uplink_seid = vsi->uplink_seid;
13972 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13973 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13974 
13975 		/* This VSI is connected to VEB so the switch_id
13976 		 * should be set to zero by default.
13977 		 */
13978 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13979 			ctxt.info.valid_sections |=
13980 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13981 			ctxt.info.switch_id =
13982 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13983 		}
13984 
13985 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13986 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13987 		break;
13988 
13989 	case I40E_VSI_SRIOV:
13990 		ctxt.pf_num = hw->pf_id;
13991 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13992 		ctxt.uplink_seid = vsi->uplink_seid;
13993 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13994 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13995 
13996 		/* This VSI is connected to VEB so the switch_id
13997 		 * should be set to zero by default.
13998 		 */
13999 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14000 			ctxt.info.valid_sections |=
14001 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14002 			ctxt.info.switch_id =
14003 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14004 		}
14005 
14006 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
14007 			ctxt.info.valid_sections |=
14008 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14009 			ctxt.info.queueing_opt_flags |=
14010 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14011 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14012 		}
14013 
14014 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14015 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14016 		if (pf->vf[vsi->vf_id].spoofchk) {
14017 			ctxt.info.valid_sections |=
14018 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14019 			ctxt.info.sec_flags |=
14020 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14021 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14022 		}
14023 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14024 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14025 		break;
14026 
14027 	case I40E_VSI_IWARP:
14028 		/* send down message to iWARP */
14029 		break;
14030 
14031 	default:
14032 		return -ENODEV;
14033 	}
14034 
14035 	if (vsi->type != I40E_VSI_MAIN) {
14036 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14037 		if (ret) {
14038 			dev_info(&vsi->back->pdev->dev,
14039 				 "add vsi failed, err %pe aq_err %s\n",
14040 				 ERR_PTR(ret),
14041 				 libie_aq_str(pf->hw.aq.asq_last_status));
14042 			ret = -ENOENT;
14043 			goto err;
14044 		}
14045 		vsi->info = ctxt.info;
14046 		vsi->info.valid_sections = 0;
14047 		vsi->seid = ctxt.seid;
14048 		vsi->id = ctxt.vsi_number;
14049 	}
14050 
14051 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14052 	vsi->active_filters = 0;
14053 	/* If macvlan filters already exist, force them to get loaded */
14054 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14055 		f->state = I40E_FILTER_NEW;
14056 		f_count++;
14057 	}
14058 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14059 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14060 
14061 	if (f_count) {
14062 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14063 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14064 	}
14065 
14066 	/* Update VSI BW information */
14067 	ret = i40e_vsi_get_bw_info(vsi);
14068 	if (ret) {
14069 		dev_info(&pf->pdev->dev,
14070 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14071 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14072 		/* VSI is already added so not tearing that up */
14073 		ret = 0;
14074 	}
14075 
14076 err:
14077 	return ret;
14078 }
14079 
14080 /**
14081  * i40e_vsi_release - Delete a VSI and free its resources
14082  * @vsi: the VSI being removed
14083  *
14084  * Returns 0 on success or < 0 on error
14085  **/
i40e_vsi_release(struct i40e_vsi * vsi)14086 int i40e_vsi_release(struct i40e_vsi *vsi)
14087 {
14088 	struct i40e_mac_filter *f;
14089 	struct hlist_node *h;
14090 	struct i40e_veb *veb;
14091 	struct i40e_pf *pf;
14092 	u16 uplink_seid;
14093 	int i, n, bkt;
14094 
14095 	pf = vsi->back;
14096 
14097 	/* release of a VEB-owner or last VSI is not allowed */
14098 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14099 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14100 			 vsi->seid, vsi->uplink_seid);
14101 		return -ENODEV;
14102 	}
14103 	if (vsi->type == I40E_VSI_MAIN && !test_bit(__I40E_DOWN, pf->state)) {
14104 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14105 		return -ENODEV;
14106 	}
14107 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14108 	uplink_seid = vsi->uplink_seid;
14109 
14110 	if (vsi->type != I40E_VSI_SRIOV) {
14111 		if (vsi->netdev_registered) {
14112 			vsi->netdev_registered = false;
14113 			if (vsi->netdev) {
14114 				/* results in a call to i40e_close() */
14115 				unregister_netdev(vsi->netdev);
14116 			}
14117 		} else {
14118 			i40e_vsi_close(vsi);
14119 		}
14120 		i40e_vsi_disable_irq(vsi);
14121 	}
14122 
14123 	if (vsi->type == I40E_VSI_MAIN)
14124 		i40e_devlink_destroy_port(pf);
14125 
14126 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14127 
14128 	/* clear the sync flag on all filters */
14129 	if (vsi->netdev) {
14130 		__dev_uc_unsync(vsi->netdev, NULL);
14131 		__dev_mc_unsync(vsi->netdev, NULL);
14132 	}
14133 
14134 	/* make sure any remaining filters are marked for deletion */
14135 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14136 		__i40e_del_filter(vsi, f);
14137 
14138 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14139 
14140 	i40e_sync_vsi_filters(vsi);
14141 
14142 	i40e_vsi_delete(vsi);
14143 	i40e_vsi_free_q_vectors(vsi);
14144 	if (vsi->netdev) {
14145 		free_netdev(vsi->netdev);
14146 		vsi->netdev = NULL;
14147 	}
14148 	i40e_vsi_clear_rings(vsi);
14149 	i40e_vsi_clear(vsi);
14150 
14151 	/* If this was the last thing on the VEB, except for the
14152 	 * controlling VSI, remove the VEB, which puts the controlling
14153 	 * VSI onto the uplink port.
14154 	 *
14155 	 * Well, okay, there's one more exception here: don't remove
14156 	 * the floating VEBs yet.  We'll wait for an explicit remove request
14157 	 * from up the network stack.
14158 	 */
14159 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14160 	if (veb && veb->uplink_seid) {
14161 		n = 0;
14162 
14163 		/* Count non-controlling VSIs present on  the VEB */
14164 		i40e_pf_for_each_vsi(pf, i, vsi)
14165 			if (vsi->uplink_seid == uplink_seid &&
14166 			    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14167 				n++;
14168 
14169 		/* If there is no VSI except the control one then release
14170 		 * the VEB and put the control VSI onto VEB uplink.
14171 		 */
14172 		if (!n)
14173 			i40e_veb_release(veb);
14174 	}
14175 
14176 	return 0;
14177 }
14178 
14179 /**
14180  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14181  * @vsi: ptr to the VSI
14182  *
14183  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14184  * corresponding SW VSI structure and initializes num_queue_pairs for the
14185  * newly allocated VSI.
14186  *
14187  * Returns 0 on success or negative on failure
14188  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)14189 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14190 {
14191 	int ret = -ENOENT;
14192 	struct i40e_pf *pf = vsi->back;
14193 
14194 	if (vsi->q_vectors[0]) {
14195 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14196 			 vsi->seid);
14197 		return -EEXIST;
14198 	}
14199 
14200 	if (vsi->base_vector) {
14201 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14202 			 vsi->seid, vsi->base_vector);
14203 		return -EEXIST;
14204 	}
14205 
14206 	ret = i40e_vsi_alloc_q_vectors(vsi);
14207 	if (ret) {
14208 		dev_info(&pf->pdev->dev,
14209 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14210 			 vsi->num_q_vectors, vsi->seid, ret);
14211 		vsi->num_q_vectors = 0;
14212 		goto vector_setup_out;
14213 	}
14214 
14215 	/* In Legacy mode, we do not have to get any other vector since we
14216 	 * piggyback on the misc/ICR0 for queue interrupts.
14217 	*/
14218 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14219 		return ret;
14220 	if (vsi->num_q_vectors)
14221 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14222 						 vsi->num_q_vectors, vsi->idx);
14223 	if (vsi->base_vector < 0) {
14224 		dev_info(&pf->pdev->dev,
14225 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14226 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14227 		i40e_vsi_free_q_vectors(vsi);
14228 		ret = -ENOENT;
14229 		goto vector_setup_out;
14230 	}
14231 
14232 vector_setup_out:
14233 	return ret;
14234 }
14235 
14236 /**
14237  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14238  * @vsi: pointer to the vsi.
14239  *
14240  * This re-allocates a vsi's queue resources.
14241  *
14242  * Returns pointer to the successfully allocated and configured VSI sw struct
14243  * on success, otherwise returns NULL on failure.
14244  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)14245 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14246 {
14247 	struct i40e_vsi *main_vsi;
14248 	u16 alloc_queue_pairs;
14249 	struct i40e_pf *pf;
14250 	int ret;
14251 
14252 	if (!vsi)
14253 		return NULL;
14254 
14255 	pf = vsi->back;
14256 
14257 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14258 	i40e_vsi_clear_rings(vsi);
14259 
14260 	i40e_vsi_free_arrays(vsi, false);
14261 	i40e_set_num_rings_in_vsi(vsi);
14262 	ret = i40e_vsi_alloc_arrays(vsi, false);
14263 	if (ret)
14264 		goto err_vsi;
14265 
14266 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14267 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14268 
14269 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14270 	if (ret < 0) {
14271 		dev_info(&pf->pdev->dev,
14272 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14273 			 alloc_queue_pairs, vsi->seid, ret);
14274 		goto err_vsi;
14275 	}
14276 	vsi->base_queue = ret;
14277 
14278 	/* Update the FW view of the VSI. Force a reset of TC and queue
14279 	 * layout configurations.
14280 	 */
14281 	main_vsi = i40e_pf_get_main_vsi(pf);
14282 	main_vsi->seid = pf->main_vsi_seid;
14283 	i40e_vsi_reconfig_tc(main_vsi);
14284 
14285 	if (vsi->type == I40E_VSI_MAIN)
14286 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14287 
14288 	/* assign it some queues */
14289 	ret = i40e_alloc_rings(vsi);
14290 	if (ret)
14291 		goto err_rings;
14292 
14293 	/* map all of the rings to the q_vectors */
14294 	i40e_vsi_map_rings_to_vectors(vsi);
14295 	return vsi;
14296 
14297 err_rings:
14298 	i40e_vsi_free_q_vectors(vsi);
14299 	if (vsi->netdev_registered) {
14300 		vsi->netdev_registered = false;
14301 		unregister_netdev(vsi->netdev);
14302 		free_netdev(vsi->netdev);
14303 		vsi->netdev = NULL;
14304 	}
14305 	if (vsi->type == I40E_VSI_MAIN)
14306 		i40e_devlink_destroy_port(pf);
14307 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14308 err_vsi:
14309 	i40e_vsi_clear(vsi);
14310 	return NULL;
14311 }
14312 
14313 /**
14314  * i40e_vsi_setup - Set up a VSI by a given type
14315  * @pf: board private structure
14316  * @type: VSI type
14317  * @uplink_seid: the switch element to link to
14318  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14319  *
14320  * This allocates the sw VSI structure and its queue resources, then add a VSI
14321  * to the identified VEB.
14322  *
14323  * Returns pointer to the successfully allocated and configure VSI sw struct on
14324  * success, otherwise returns NULL on failure.
14325  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)14326 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14327 				u16 uplink_seid, u32 param1)
14328 {
14329 	struct i40e_vsi *vsi = NULL;
14330 	struct i40e_veb *veb = NULL;
14331 	u16 alloc_queue_pairs;
14332 	int v_idx;
14333 	int ret;
14334 
14335 	/* The requested uplink_seid must be either
14336 	 *     - the PF's port seid
14337 	 *              no VEB is needed because this is the PF
14338 	 *              or this is a Flow Director special case VSI
14339 	 *     - seid of an existing VEB
14340 	 *     - seid of a VSI that owns an existing VEB
14341 	 *     - seid of a VSI that doesn't own a VEB
14342 	 *              a new VEB is created and the VSI becomes the owner
14343 	 *     - seid of the PF VSI, which is what creates the first VEB
14344 	 *              this is a special case of the previous
14345 	 *
14346 	 * Find which uplink_seid we were given and create a new VEB if needed
14347 	 */
14348 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14349 	if (!veb && uplink_seid != pf->mac_seid) {
14350 		vsi = i40e_pf_get_vsi_by_seid(pf, uplink_seid);
14351 		if (!vsi) {
14352 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14353 				 uplink_seid);
14354 			return NULL;
14355 		}
14356 
14357 		if (vsi->uplink_seid == pf->mac_seid)
14358 			veb = i40e_veb_setup(pf, pf->mac_seid, vsi->seid,
14359 					     vsi->tc_config.enabled_tc);
14360 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14361 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
14362 					     vsi->tc_config.enabled_tc);
14363 		if (veb) {
14364 			if (vsi->type != I40E_VSI_MAIN) {
14365 				dev_info(&vsi->back->pdev->dev,
14366 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14367 				return NULL;
14368 			}
14369 			/* We come up by default in VEPA mode if SRIOV is not
14370 			 * already enabled, in which case we can't force VEPA
14371 			 * mode.
14372 			 */
14373 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14374 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14375 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14376 			}
14377 			i40e_config_bridge_mode(veb);
14378 		}
14379 		veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
14380 		if (!veb) {
14381 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14382 			return NULL;
14383 		}
14384 
14385 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14386 		uplink_seid = veb->seid;
14387 	}
14388 
14389 	/* get vsi sw struct */
14390 	v_idx = i40e_vsi_mem_alloc(pf, type);
14391 	if (v_idx < 0)
14392 		goto err_alloc;
14393 	vsi = pf->vsi[v_idx];
14394 	if (!vsi)
14395 		goto err_alloc;
14396 	vsi->type = type;
14397 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14398 
14399 	if (type == I40E_VSI_MAIN)
14400 		pf->lan_vsi = v_idx;
14401 	else if (type == I40E_VSI_SRIOV)
14402 		vsi->vf_id = param1;
14403 	/* assign it some queues */
14404 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14405 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14406 
14407 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14408 	if (ret < 0) {
14409 		dev_info(&pf->pdev->dev,
14410 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14411 			 alloc_queue_pairs, vsi->seid, ret);
14412 		goto err_vsi;
14413 	}
14414 	vsi->base_queue = ret;
14415 
14416 	/* get a VSI from the hardware */
14417 	vsi->uplink_seid = uplink_seid;
14418 	ret = i40e_add_vsi(vsi);
14419 	if (ret)
14420 		goto err_vsi;
14421 
14422 	switch (vsi->type) {
14423 	/* setup the netdev if needed */
14424 	case I40E_VSI_MAIN:
14425 	case I40E_VSI_VMDQ2:
14426 		ret = i40e_config_netdev(vsi);
14427 		if (ret)
14428 			goto err_netdev;
14429 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14430 		if (ret)
14431 			goto err_netdev;
14432 		if (vsi->type == I40E_VSI_MAIN) {
14433 			ret = i40e_devlink_create_port(pf);
14434 			if (ret)
14435 				goto err_netdev;
14436 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14437 		}
14438 		ret = register_netdev(vsi->netdev);
14439 		if (ret)
14440 			goto err_dl_port;
14441 		vsi->netdev_registered = true;
14442 		netif_carrier_off(vsi->netdev);
14443 #ifdef CONFIG_I40E_DCB
14444 		/* Setup DCB netlink interface */
14445 		i40e_dcbnl_setup(vsi);
14446 #endif /* CONFIG_I40E_DCB */
14447 		fallthrough;
14448 	case I40E_VSI_FDIR:
14449 		/* set up vectors and rings if needed */
14450 		ret = i40e_vsi_setup_vectors(vsi);
14451 		if (ret)
14452 			goto err_msix;
14453 
14454 		ret = i40e_alloc_rings(vsi);
14455 		if (ret)
14456 			goto err_rings;
14457 
14458 		/* map all of the rings to the q_vectors */
14459 		i40e_vsi_map_rings_to_vectors(vsi);
14460 
14461 		i40e_vsi_reset_stats(vsi);
14462 		break;
14463 	default:
14464 		/* no netdev or rings for the other VSI types */
14465 		break;
14466 	}
14467 
14468 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14469 	    vsi->type == I40E_VSI_VMDQ2) {
14470 		ret = i40e_vsi_config_rss(vsi);
14471 		if (ret)
14472 			goto err_config;
14473 	}
14474 	return vsi;
14475 
14476 err_config:
14477 	i40e_vsi_clear_rings(vsi);
14478 err_rings:
14479 	i40e_vsi_free_q_vectors(vsi);
14480 err_msix:
14481 	if (vsi->netdev_registered) {
14482 		vsi->netdev_registered = false;
14483 		unregister_netdev(vsi->netdev);
14484 		free_netdev(vsi->netdev);
14485 		vsi->netdev = NULL;
14486 	}
14487 err_dl_port:
14488 	if (vsi->type == I40E_VSI_MAIN)
14489 		i40e_devlink_destroy_port(pf);
14490 err_netdev:
14491 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14492 err_vsi:
14493 	i40e_vsi_clear(vsi);
14494 err_alloc:
14495 	return NULL;
14496 }
14497 
14498 /**
14499  * i40e_veb_get_bw_info - Query VEB BW information
14500  * @veb: the veb to query
14501  *
14502  * Query the Tx scheduler BW configuration data for given VEB
14503  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)14504 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14505 {
14506 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14507 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14508 	struct i40e_pf *pf = veb->pf;
14509 	struct i40e_hw *hw = &pf->hw;
14510 	u32 tc_bw_max;
14511 	int ret = 0;
14512 	int i;
14513 
14514 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14515 						  &bw_data, NULL);
14516 	if (ret) {
14517 		dev_info(&pf->pdev->dev,
14518 			 "query veb bw config failed, err %pe aq_err %s\n",
14519 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
14520 		goto out;
14521 	}
14522 
14523 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14524 						   &ets_data, NULL);
14525 	if (ret) {
14526 		dev_info(&pf->pdev->dev,
14527 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14528 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
14529 		goto out;
14530 	}
14531 
14532 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14533 	veb->bw_max_quanta = ets_data.tc_bw_max;
14534 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14535 	veb->enabled_tc = ets_data.tc_valid_bits;
14536 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14537 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14538 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14539 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14540 		veb->bw_tc_limit_credits[i] =
14541 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14542 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14543 	}
14544 
14545 out:
14546 	return ret;
14547 }
14548 
14549 /**
14550  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14551  * @pf: board private structure
14552  *
14553  * On error: returns error code (negative)
14554  * On success: returns vsi index in PF (positive)
14555  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)14556 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14557 {
14558 	int ret = -ENOENT;
14559 	struct i40e_veb *veb;
14560 	int i;
14561 
14562 	/* Need to protect the allocation of switch elements at the PF level */
14563 	mutex_lock(&pf->switch_mutex);
14564 
14565 	/* VEB list may be fragmented if VEB creation/destruction has
14566 	 * been happening.  We can afford to do a quick scan to look
14567 	 * for any free slots in the list.
14568 	 *
14569 	 * find next empty veb slot, looping back around if necessary
14570 	 */
14571 	i = 0;
14572 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14573 		i++;
14574 	if (i >= I40E_MAX_VEB) {
14575 		ret = -ENOMEM;
14576 		goto err_alloc_veb;  /* out of VEB slots! */
14577 	}
14578 
14579 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14580 	if (!veb) {
14581 		ret = -ENOMEM;
14582 		goto err_alloc_veb;
14583 	}
14584 	veb->pf = pf;
14585 	veb->idx = i;
14586 	veb->enabled_tc = 1;
14587 
14588 	pf->veb[i] = veb;
14589 	ret = i;
14590 err_alloc_veb:
14591 	mutex_unlock(&pf->switch_mutex);
14592 	return ret;
14593 }
14594 
14595 /**
14596  * i40e_switch_branch_release - Delete a branch of the switch tree
14597  * @branch: where to start deleting
14598  *
14599  * This uses recursion to find the tips of the branch to be
14600  * removed, deleting until we get back to and can delete this VEB.
14601  **/
i40e_switch_branch_release(struct i40e_veb * branch)14602 static void i40e_switch_branch_release(struct i40e_veb *branch)
14603 {
14604 	struct i40e_pf *pf = branch->pf;
14605 	u16 branch_seid = branch->seid;
14606 	u16 veb_idx = branch->idx;
14607 	struct i40e_vsi *vsi;
14608 	struct i40e_veb *veb;
14609 	int i;
14610 
14611 	/* release any VEBs on this VEB - RECURSION */
14612 	i40e_pf_for_each_veb(pf, i, veb)
14613 		if (veb->uplink_seid == branch->seid)
14614 			i40e_switch_branch_release(veb);
14615 
14616 	/* Release the VSIs on this VEB, but not the owner VSI.
14617 	 *
14618 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14619 	 *       the VEB itself, so don't use (*branch) after this loop.
14620 	 */
14621 	i40e_pf_for_each_vsi(pf, i, vsi)
14622 		if (vsi->uplink_seid == branch_seid &&
14623 		    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14624 			i40e_vsi_release(vsi);
14625 
14626 	/* There's one corner case where the VEB might not have been
14627 	 * removed, so double check it here and remove it if needed.
14628 	 * This case happens if the veb was created from the debugfs
14629 	 * commands and no VSIs were added to it.
14630 	 */
14631 	if (pf->veb[veb_idx])
14632 		i40e_veb_release(pf->veb[veb_idx]);
14633 }
14634 
14635 /**
14636  * i40e_veb_clear - remove veb struct
14637  * @veb: the veb to remove
14638  **/
i40e_veb_clear(struct i40e_veb * veb)14639 static void i40e_veb_clear(struct i40e_veb *veb)
14640 {
14641 	if (!veb)
14642 		return;
14643 
14644 	if (veb->pf) {
14645 		struct i40e_pf *pf = veb->pf;
14646 
14647 		mutex_lock(&pf->switch_mutex);
14648 		if (pf->veb[veb->idx] == veb)
14649 			pf->veb[veb->idx] = NULL;
14650 		mutex_unlock(&pf->switch_mutex);
14651 	}
14652 
14653 	kfree(veb);
14654 }
14655 
14656 /**
14657  * i40e_veb_release - Delete a VEB and free its resources
14658  * @veb: the VEB being removed
14659  **/
i40e_veb_release(struct i40e_veb * veb)14660 void i40e_veb_release(struct i40e_veb *veb)
14661 {
14662 	struct i40e_vsi *vsi, *vsi_it;
14663 	struct i40e_pf *pf;
14664 	int i, n = 0;
14665 
14666 	pf = veb->pf;
14667 
14668 	/* find the remaining VSI and check for extras */
14669 	i40e_pf_for_each_vsi(pf, i, vsi_it)
14670 		if (vsi_it->uplink_seid == veb->seid) {
14671 			if (vsi_it->flags & I40E_VSI_FLAG_VEB_OWNER)
14672 				vsi = vsi_it;
14673 			n++;
14674 		}
14675 
14676 	/* Floating VEB has to be empty and regular one must have
14677 	 * single owner VSI.
14678 	 */
14679 	if ((veb->uplink_seid && n != 1) || (!veb->uplink_seid && n != 0)) {
14680 		dev_info(&pf->pdev->dev,
14681 			 "can't remove VEB %d with %d VSIs left\n",
14682 			 veb->seid, n);
14683 		return;
14684 	}
14685 
14686 	/* For regular VEB move the owner VSI to uplink port */
14687 	if (veb->uplink_seid) {
14688 		vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14689 		vsi->uplink_seid = veb->uplink_seid;
14690 		vsi->veb_idx = I40E_NO_VEB;
14691 	}
14692 
14693 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14694 	i40e_veb_clear(veb);
14695 }
14696 
14697 /**
14698  * i40e_add_veb - create the VEB in the switch
14699  * @veb: the VEB to be instantiated
14700  * @vsi: the controlling VSI
14701  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)14702 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14703 {
14704 	struct i40e_pf *pf = veb->pf;
14705 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14706 	int ret;
14707 
14708 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi ? vsi->seid : 0,
14709 			      veb->enabled_tc, vsi ? false : true,
14710 			      &veb->seid, enable_stats, NULL);
14711 
14712 	/* get a VEB from the hardware */
14713 	if (ret) {
14714 		dev_info(&pf->pdev->dev,
14715 			 "couldn't add VEB, err %pe aq_err %s\n",
14716 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14717 		return -EPERM;
14718 	}
14719 
14720 	/* get statistics counter */
14721 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14722 					 &veb->stats_idx, NULL, NULL, NULL);
14723 	if (ret) {
14724 		dev_info(&pf->pdev->dev,
14725 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14726 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14727 		return -EPERM;
14728 	}
14729 	ret = i40e_veb_get_bw_info(veb);
14730 	if (ret) {
14731 		dev_info(&pf->pdev->dev,
14732 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14733 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14734 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14735 		return -ENOENT;
14736 	}
14737 
14738 	if (vsi) {
14739 		vsi->uplink_seid = veb->seid;
14740 		vsi->veb_idx = veb->idx;
14741 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14742 	}
14743 
14744 	return 0;
14745 }
14746 
14747 /**
14748  * i40e_veb_setup - Set up a VEB
14749  * @pf: board private structure
14750  * @uplink_seid: the switch element to link to
14751  * @vsi_seid: the initial VSI seid
14752  * @enabled_tc: Enabled TC bit-map
14753  *
14754  * This allocates the sw VEB structure and links it into the switch
14755  * It is possible and legal for this to be a duplicate of an already
14756  * existing VEB.  It is also possible for both uplink and vsi seids
14757  * to be zero, in order to create a floating VEB.
14758  *
14759  * Returns pointer to the successfully allocated VEB sw struct on
14760  * success, otherwise returns NULL on failure.
14761  **/
i40e_veb_setup(struct i40e_pf * pf,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14762 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 uplink_seid,
14763 				u16 vsi_seid, u8 enabled_tc)
14764 {
14765 	struct i40e_vsi *vsi = NULL;
14766 	struct i40e_veb *veb;
14767 	int veb_idx;
14768 	int ret;
14769 
14770 	/* if one seid is 0, the other must be 0 to create a floating relay */
14771 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14772 	    (uplink_seid + vsi_seid != 0)) {
14773 		dev_info(&pf->pdev->dev,
14774 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14775 			 uplink_seid, vsi_seid);
14776 		return NULL;
14777 	}
14778 
14779 	/* make sure there is such a vsi and uplink */
14780 	if (vsi_seid) {
14781 		vsi = i40e_pf_get_vsi_by_seid(pf, vsi_seid);
14782 		if (!vsi) {
14783 			dev_err(&pf->pdev->dev, "vsi seid %d not found\n",
14784 				vsi_seid);
14785 			return NULL;
14786 		}
14787 	}
14788 
14789 	/* get veb sw struct */
14790 	veb_idx = i40e_veb_mem_alloc(pf);
14791 	if (veb_idx < 0)
14792 		goto err_alloc;
14793 	veb = pf->veb[veb_idx];
14794 	veb->uplink_seid = uplink_seid;
14795 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14796 
14797 	/* create the VEB in the switch */
14798 	ret = i40e_add_veb(veb, vsi);
14799 	if (ret)
14800 		goto err_veb;
14801 
14802 	if (vsi && vsi->idx == pf->lan_vsi)
14803 		pf->lan_veb = veb->idx;
14804 
14805 	return veb;
14806 
14807 err_veb:
14808 	i40e_veb_clear(veb);
14809 err_alloc:
14810 	return NULL;
14811 }
14812 
14813 /**
14814  * i40e_setup_pf_switch_element - set PF vars based on switch type
14815  * @pf: board private structure
14816  * @ele: element we are building info from
14817  * @num_reported: total number of elements
14818  * @printconfig: should we print the contents
14819  *
14820  * helper function to assist in extracting a few useful SEID values.
14821  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14822 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14823 				struct i40e_aqc_switch_config_element_resp *ele,
14824 				u16 num_reported, bool printconfig)
14825 {
14826 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14827 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14828 	u8 element_type = ele->element_type;
14829 	u16 seid = le16_to_cpu(ele->seid);
14830 	struct i40e_veb *veb;
14831 
14832 	if (printconfig)
14833 		dev_info(&pf->pdev->dev,
14834 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14835 			 element_type, seid, uplink_seid, downlink_seid);
14836 
14837 	switch (element_type) {
14838 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14839 		pf->mac_seid = seid;
14840 		break;
14841 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14842 		/* Main VEB? */
14843 		if (uplink_seid != pf->mac_seid)
14844 			break;
14845 		veb = i40e_pf_get_main_veb(pf);
14846 		if (!veb) {
14847 			int v;
14848 
14849 			/* find existing or else empty VEB */
14850 			veb = i40e_pf_get_veb_by_seid(pf, seid);
14851 			if (veb) {
14852 				pf->lan_veb = veb->idx;
14853 			} else {
14854 				v = i40e_veb_mem_alloc(pf);
14855 				if (v < 0)
14856 					break;
14857 				pf->lan_veb = v;
14858 			}
14859 		}
14860 
14861 		/* Try to get again main VEB as pf->lan_veb may have changed */
14862 		veb = i40e_pf_get_main_veb(pf);
14863 		if (!veb)
14864 			break;
14865 
14866 		veb->seid = seid;
14867 		veb->uplink_seid = pf->mac_seid;
14868 		veb->pf = pf;
14869 		break;
14870 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14871 		if (num_reported != 1)
14872 			break;
14873 		/* This is immediately after a reset so we can assume this is
14874 		 * the PF's VSI
14875 		 */
14876 		pf->mac_seid = uplink_seid;
14877 		pf->main_vsi_seid = seid;
14878 		if (printconfig)
14879 			dev_info(&pf->pdev->dev,
14880 				 "pf_seid=%d main_vsi_seid=%d\n",
14881 				 downlink_seid, pf->main_vsi_seid);
14882 		break;
14883 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14884 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14885 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14886 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14887 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14888 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14889 		/* ignore these for now */
14890 		break;
14891 	default:
14892 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14893 			 element_type, seid);
14894 		break;
14895 	}
14896 }
14897 
14898 /**
14899  * i40e_fetch_switch_configuration - Get switch config from firmware
14900  * @pf: board private structure
14901  * @printconfig: should we print the contents
14902  *
14903  * Get the current switch configuration from the device and
14904  * extract a few useful SEID values.
14905  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)14906 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14907 {
14908 	struct i40e_aqc_get_switch_config_resp *sw_config;
14909 	u16 next_seid = 0;
14910 	int ret = 0;
14911 	u8 *aq_buf;
14912 	int i;
14913 
14914 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14915 	if (!aq_buf)
14916 		return -ENOMEM;
14917 
14918 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14919 	do {
14920 		u16 num_reported, num_total;
14921 
14922 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14923 						I40E_AQ_LARGE_BUF,
14924 						&next_seid, NULL);
14925 		if (ret) {
14926 			dev_info(&pf->pdev->dev,
14927 				 "get switch config failed err %d aq_err %s\n",
14928 				 ret, libie_aq_str(pf->hw.aq.asq_last_status));
14929 			kfree(aq_buf);
14930 			return -ENOENT;
14931 		}
14932 
14933 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14934 		num_total = le16_to_cpu(sw_config->header.num_total);
14935 
14936 		if (printconfig)
14937 			dev_info(&pf->pdev->dev,
14938 				 "header: %d reported %d total\n",
14939 				 num_reported, num_total);
14940 
14941 		for (i = 0; i < num_reported; i++) {
14942 			struct i40e_aqc_switch_config_element_resp *ele =
14943 				&sw_config->element[i];
14944 
14945 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14946 						     printconfig);
14947 		}
14948 	} while (next_seid != 0);
14949 
14950 	kfree(aq_buf);
14951 	return ret;
14952 }
14953 
14954 /**
14955  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14956  * @pf: board private structure
14957  * @reinit: if the Main VSI needs to re-initialized.
14958  * @lock_acquired: indicates whether or not the lock has been acquired
14959  *
14960  * Returns 0 on success, negative value on failure
14961  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)14962 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14963 {
14964 	struct i40e_vsi *main_vsi;
14965 	u16 flags = 0;
14966 	int ret;
14967 
14968 	/* find out what's out there already */
14969 	ret = i40e_fetch_switch_configuration(pf, false);
14970 	if (ret) {
14971 		dev_info(&pf->pdev->dev,
14972 			 "couldn't fetch switch config, err %pe aq_err %s\n",
14973 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14974 		return ret;
14975 	}
14976 	i40e_pf_reset_stats(pf);
14977 
14978 	/* set the switch config bit for the whole device to
14979 	 * support limited promisc or true promisc
14980 	 * when user requests promisc. The default is limited
14981 	 * promisc.
14982 	*/
14983 
14984 	if ((pf->hw.pf_id == 0) &&
14985 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
14986 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14987 		pf->last_sw_conf_flags = flags;
14988 	}
14989 
14990 	if (pf->hw.pf_id == 0) {
14991 		u16 valid_flags;
14992 
14993 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14994 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14995 						NULL);
14996 		if (ret && pf->hw.aq.asq_last_status != LIBIE_AQ_RC_ESRCH) {
14997 			dev_info(&pf->pdev->dev,
14998 				 "couldn't set switch config bits, err %pe aq_err %s\n",
14999 				 ERR_PTR(ret),
15000 				 libie_aq_str(pf->hw.aq.asq_last_status));
15001 			/* not a fatal problem, just keep going */
15002 		}
15003 		pf->last_sw_conf_valid_flags = valid_flags;
15004 	}
15005 
15006 	/* first time setup */
15007 	main_vsi = i40e_pf_get_main_vsi(pf);
15008 	if (!main_vsi || reinit) {
15009 		struct i40e_veb *veb;
15010 		u16 uplink_seid;
15011 
15012 		/* Set up the PF VSI associated with the PF's main VSI
15013 		 * that is already in the HW switch
15014 		 */
15015 		veb = i40e_pf_get_main_veb(pf);
15016 		if (veb)
15017 			uplink_seid = veb->seid;
15018 		else
15019 			uplink_seid = pf->mac_seid;
15020 		if (!main_vsi)
15021 			main_vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN,
15022 						  uplink_seid, 0);
15023 		else if (reinit)
15024 			main_vsi = i40e_vsi_reinit_setup(main_vsi);
15025 		if (!main_vsi) {
15026 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15027 			i40e_cloud_filter_exit(pf);
15028 			i40e_fdir_teardown(pf);
15029 			return -EAGAIN;
15030 		}
15031 	} else {
15032 		/* force a reset of TC and queue layout configurations */
15033 		main_vsi->seid = pf->main_vsi_seid;
15034 		i40e_vsi_reconfig_tc(main_vsi);
15035 	}
15036 	i40e_vlan_stripping_disable(main_vsi);
15037 
15038 	i40e_fdir_sb_setup(pf);
15039 
15040 	/* Setup static PF queue filter control settings */
15041 	ret = i40e_setup_pf_filter_control(pf);
15042 	if (ret) {
15043 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15044 			 ret);
15045 		/* Failure here should not stop continuing other steps */
15046 	}
15047 
15048 	/* enable RSS in the HW, even for only one queue, as the stack can use
15049 	 * the hash
15050 	 */
15051 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15052 		i40e_pf_config_rss(pf);
15053 
15054 	/* fill in link information and enable LSE reporting */
15055 	i40e_link_event(pf);
15056 
15057 	i40e_ptp_init(pf);
15058 
15059 	if (!lock_acquired)
15060 		rtnl_lock();
15061 
15062 	/* repopulate tunnel port filters */
15063 	udp_tunnel_nic_reset_ntf(main_vsi->netdev);
15064 
15065 	if (!lock_acquired)
15066 		rtnl_unlock();
15067 
15068 	return ret;
15069 }
15070 
15071 /**
15072  * i40e_determine_queue_usage - Work out queue distribution
15073  * @pf: board private structure
15074  **/
i40e_determine_queue_usage(struct i40e_pf * pf)15075 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15076 {
15077 	int queues_left;
15078 	int q_max;
15079 
15080 	pf->num_lan_qps = 0;
15081 
15082 	/* Find the max queues to be put into basic use.  We'll always be
15083 	 * using TC0, whether or not DCB is running, and TC0 will get the
15084 	 * big RSS set.
15085 	 */
15086 	queues_left = pf->hw.func_caps.num_tx_qp;
15087 
15088 	if ((queues_left == 1) ||
15089 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15090 		/* one qp for PF, no queues for anything else */
15091 		queues_left = 0;
15092 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15093 
15094 		/* make sure all the fancies are disabled */
15095 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15096 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15097 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15098 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15099 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15100 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15101 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15102 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15103 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15104 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15105 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15106 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15107 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15108 		/* one qp for PF */
15109 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15110 		queues_left -= pf->num_lan_qps;
15111 
15112 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15113 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15114 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15115 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15116 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15117 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15118 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15119 	} else {
15120 		/* Not enough queues for all TCs */
15121 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15122 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15123 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15124 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15125 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15126 		}
15127 
15128 		/* limit lan qps to the smaller of qps, cpus or msix */
15129 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15130 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15131 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15132 		pf->num_lan_qps = q_max;
15133 
15134 		queues_left -= pf->num_lan_qps;
15135 	}
15136 
15137 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15138 		if (queues_left > 1) {
15139 			queues_left -= 1; /* save 1 queue for FD */
15140 		} else {
15141 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15142 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15143 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15144 		}
15145 	}
15146 
15147 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15148 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15149 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15150 					(queues_left / pf->num_vf_qps));
15151 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15152 	}
15153 
15154 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15155 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15156 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15157 					  (queues_left / pf->num_vmdq_qps));
15158 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15159 	}
15160 
15161 	pf->queues_left = queues_left;
15162 	dev_dbg(&pf->pdev->dev,
15163 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15164 		pf->hw.func_caps.num_tx_qp,
15165 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15166 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15167 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15168 		queues_left);
15169 }
15170 
15171 /**
15172  * i40e_setup_pf_filter_control - Setup PF static filter control
15173  * @pf: PF to be setup
15174  *
15175  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15176  * settings. If PE/FCoE are enabled then it will also set the per PF
15177  * based filter sizes required for them. It also enables Flow director,
15178  * ethertype and macvlan type filter settings for the pf.
15179  *
15180  * Returns 0 on success, negative on failure
15181  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)15182 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15183 {
15184 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15185 
15186 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15187 
15188 	/* Flow Director is enabled */
15189 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15190 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15191 		settings->enable_fdir = true;
15192 
15193 	/* Ethtype and MACVLAN filters enabled for PF */
15194 	settings->enable_ethtype = true;
15195 	settings->enable_macvlan = true;
15196 
15197 	if (i40e_set_filter_control(&pf->hw, settings))
15198 		return -ENOENT;
15199 
15200 	return 0;
15201 }
15202 
15203 #define INFO_STRING_LEN 255
15204 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)15205 static void i40e_print_features(struct i40e_pf *pf)
15206 {
15207 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
15208 	struct i40e_hw *hw = &pf->hw;
15209 	char *buf;
15210 	int i;
15211 
15212 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15213 	if (!buf)
15214 		return;
15215 
15216 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15217 #ifdef CONFIG_PCI_IOV
15218 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15219 #endif
15220 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15221 		       pf->hw.func_caps.num_vsis, main_vsi->num_queue_pairs);
15222 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15223 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15224 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15225 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15226 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15227 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15228 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15229 	}
15230 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15231 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15232 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15233 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15234 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15235 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15236 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15237 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15238 	else
15239 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15240 
15241 	dev_info(&pf->pdev->dev, "%s\n", buf);
15242 	kfree(buf);
15243 	WARN_ON(i > INFO_STRING_LEN);
15244 }
15245 
15246 /**
15247  * i40e_get_platform_mac_addr - get platform-specific MAC address
15248  * @pdev: PCI device information struct
15249  * @pf: board private structure
15250  *
15251  * Look up the MAC address for the device. First we'll try
15252  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15253  * specific fallback. Otherwise, we'll default to the stored value in
15254  * firmware.
15255  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)15256 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15257 {
15258 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15259 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15260 }
15261 
15262 /**
15263  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15264  * @fec_cfg: FEC option to set in flags
15265  * @flags: ptr to flags in which we set FEC option
15266  **/
i40e_set_fec_in_flags(u8 fec_cfg,unsigned long * flags)15267 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15268 {
15269 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15270 		set_bit(I40E_FLAG_RS_FEC, flags);
15271 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15272 	}
15273 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15274 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15275 		set_bit(I40E_FLAG_RS_FEC, flags);
15276 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15277 	}
15278 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15279 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15280 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15281 		clear_bit(I40E_FLAG_RS_FEC, flags);
15282 	}
15283 	if (fec_cfg == 0) {
15284 		clear_bit(I40E_FLAG_RS_FEC, flags);
15285 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15286 	}
15287 }
15288 
15289 /**
15290  * i40e_check_recovery_mode - check if we are running transition firmware
15291  * @pf: board private structure
15292  *
15293  * Check registers indicating the firmware runs in recovery mode. Sets the
15294  * appropriate driver state.
15295  *
15296  * Returns true if the recovery mode was detected, false otherwise
15297  **/
i40e_check_recovery_mode(struct i40e_pf * pf)15298 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15299 {
15300 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15301 
15302 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15303 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15304 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15305 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15306 
15307 		return true;
15308 	}
15309 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15310 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15311 
15312 	return false;
15313 }
15314 
15315 /**
15316  * i40e_pf_loop_reset - perform reset in a loop.
15317  * @pf: board private structure
15318  *
15319  * This function is useful when a NIC is about to enter recovery mode.
15320  * When a NIC's internal data structures are corrupted the NIC's
15321  * firmware is going to enter recovery mode.
15322  * Right after a POR it takes about 7 minutes for firmware to enter
15323  * recovery mode. Until that time a NIC is in some kind of intermediate
15324  * state. After that time period the NIC almost surely enters
15325  * recovery mode. The only way for a driver to detect intermediate
15326  * state is to issue a series of pf-resets and check a return value.
15327  * If a PF reset returns success then the firmware could be in recovery
15328  * mode so the caller of this code needs to check for recovery mode
15329  * if this function returns success. There is a little chance that
15330  * firmware will hang in intermediate state forever.
15331  * Since waiting 7 minutes is quite a lot of time this function waits
15332  * 10 seconds and then gives up by returning an error.
15333  *
15334  * Return 0 on success, negative on failure.
15335  **/
i40e_pf_loop_reset(struct i40e_pf * pf)15336 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15337 {
15338 	/* wait max 10 seconds for PF reset to succeed */
15339 	const unsigned long time_end = jiffies + 10 * HZ;
15340 	struct i40e_hw *hw = &pf->hw;
15341 	int ret;
15342 
15343 	ret = i40e_pf_reset(hw);
15344 	while (ret != 0 && time_before(jiffies, time_end)) {
15345 		usleep_range(10000, 20000);
15346 		ret = i40e_pf_reset(hw);
15347 	}
15348 
15349 	if (ret == 0)
15350 		pf->pfr_count++;
15351 	else
15352 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15353 
15354 	return ret;
15355 }
15356 
15357 /**
15358  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15359  * @pf: board private structure
15360  *
15361  * Check FW registers to determine if FW issued unexpected EMP Reset.
15362  * Every time when unexpected EMP Reset occurs the FW increments
15363  * a counter of unexpected EMP Resets. When the counter reaches 10
15364  * the FW should enter the Recovery mode
15365  *
15366  * Returns true if FW issued unexpected EMP Reset
15367  **/
i40e_check_fw_empr(struct i40e_pf * pf)15368 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15369 {
15370 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15371 			   I40E_GL_FWSTS_FWS1B_MASK;
15372 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15373 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15374 }
15375 
15376 /**
15377  * i40e_handle_resets - handle EMP resets and PF resets
15378  * @pf: board private structure
15379  *
15380  * Handle both EMP resets and PF resets and conclude whether there are
15381  * any issues regarding these resets. If there are any issues then
15382  * generate log entry.
15383  *
15384  * Return 0 if NIC is healthy or negative value when there are issues
15385  * with resets
15386  **/
i40e_handle_resets(struct i40e_pf * pf)15387 static int i40e_handle_resets(struct i40e_pf *pf)
15388 {
15389 	const int pfr = i40e_pf_loop_reset(pf);
15390 	const bool is_empr = i40e_check_fw_empr(pf);
15391 
15392 	if (is_empr || pfr != 0)
15393 		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");
15394 
15395 	return is_empr ? -EIO : pfr;
15396 }
15397 
15398 /**
15399  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15400  * @pf: board private structure
15401  * @hw: ptr to the hardware info
15402  *
15403  * This function does a minimal setup of all subsystems needed for running
15404  * recovery mode.
15405  *
15406  * Returns 0 on success, negative on failure
15407  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)15408 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15409 {
15410 	struct i40e_vsi *vsi;
15411 	int err;
15412 	int v_idx;
15413 
15414 	pci_set_drvdata(pf->pdev, pf);
15415 	pci_save_state(pf->pdev);
15416 
15417 	/* set up periodic task facility */
15418 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15419 	pf->service_timer_period = HZ;
15420 
15421 	INIT_WORK(&pf->service_task, i40e_service_task);
15422 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15423 
15424 	err = i40e_init_interrupt_scheme(pf);
15425 	if (err)
15426 		goto err_switch_setup;
15427 
15428 	/* The number of VSIs reported by the FW is the minimum guaranteed
15429 	 * to us; HW supports far more and we share the remaining pool with
15430 	 * the other PFs. We allocate space for more than the guarantee with
15431 	 * the understanding that we might not get them all later.
15432 	 */
15433 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15434 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15435 	else
15436 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15437 
15438 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15439 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15440 			  GFP_KERNEL);
15441 	if (!pf->vsi) {
15442 		err = -ENOMEM;
15443 		goto err_switch_setup;
15444 	}
15445 
15446 	/* We allocate one VSI which is needed as absolute minimum
15447 	 * in order to register the netdev
15448 	 */
15449 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15450 	if (v_idx < 0) {
15451 		err = v_idx;
15452 		goto err_switch_setup;
15453 	}
15454 	pf->lan_vsi = v_idx;
15455 	vsi = pf->vsi[v_idx];
15456 	if (!vsi) {
15457 		err = -EFAULT;
15458 		goto err_switch_setup;
15459 	}
15460 	vsi->alloc_queue_pairs = 1;
15461 	err = i40e_config_netdev(vsi);
15462 	if (err)
15463 		goto err_switch_setup;
15464 	err = register_netdev(vsi->netdev);
15465 	if (err)
15466 		goto err_switch_setup;
15467 	vsi->netdev_registered = true;
15468 	i40e_dbg_pf_init(pf);
15469 
15470 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15471 	if (err)
15472 		goto err_switch_setup;
15473 
15474 	/* tell the firmware that we're starting */
15475 	i40e_send_version(pf);
15476 
15477 	/* since everything's happy, start the service_task timer */
15478 	mod_timer(&pf->service_timer,
15479 		  round_jiffies(jiffies + pf->service_timer_period));
15480 
15481 	return 0;
15482 
15483 err_switch_setup:
15484 	i40e_reset_interrupt_capability(pf);
15485 	timer_shutdown_sync(&pf->service_timer);
15486 	i40e_shutdown_adminq(hw);
15487 	iounmap(hw->hw_addr);
15488 	pci_release_mem_regions(pf->pdev);
15489 	pci_disable_device(pf->pdev);
15490 	i40e_free_pf(pf);
15491 
15492 	return err;
15493 }
15494 
15495 /**
15496  * i40e_set_subsystem_device_id - set subsystem device id
15497  * @hw: pointer to the hardware info
15498  *
15499  * Set PCI subsystem device id either from a pci_dev structure or
15500  * a specific FW register.
15501  **/
i40e_set_subsystem_device_id(struct i40e_hw * hw)15502 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15503 {
15504 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15505 
15506 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15507 		pf->pdev->subsystem_device :
15508 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15509 }
15510 
15511 /**
15512  * i40e_probe - Device initialization routine
15513  * @pdev: PCI device information struct
15514  * @ent: entry in i40e_pci_tbl
15515  *
15516  * i40e_probe initializes a PF identified by a pci_dev structure.
15517  * The OS initialization, configuring of the PF private structure,
15518  * and a hardware reset occur.
15519  *
15520  * Returns 0 on success, negative on failure
15521  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)15522 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15523 {
15524 	struct i40e_aq_get_phy_abilities_resp abilities;
15525 #ifdef CONFIG_I40E_DCB
15526 	enum i40e_get_fw_lldp_status_resp lldp_status;
15527 #endif /* CONFIG_I40E_DCB */
15528 	struct i40e_vsi *vsi;
15529 	struct i40e_pf *pf;
15530 	struct i40e_hw *hw;
15531 	u16 wol_nvm_bits;
15532 	char nvm_ver[32];
15533 	u16 link_status;
15534 #ifdef CONFIG_I40E_DCB
15535 	int status;
15536 #endif /* CONFIG_I40E_DCB */
15537 	int err;
15538 	u32 val;
15539 
15540 	err = pci_enable_device_mem(pdev);
15541 	if (err)
15542 		return err;
15543 
15544 	/* set up for high or low dma */
15545 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15546 	if (err) {
15547 		dev_err(&pdev->dev,
15548 			"DMA configuration failed: 0x%x\n", err);
15549 		goto err_dma;
15550 	}
15551 
15552 	/* set up pci connections */
15553 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15554 	if (err) {
15555 		dev_info(&pdev->dev,
15556 			 "pci_request_selected_regions failed %d\n", err);
15557 		goto err_pci_reg;
15558 	}
15559 
15560 	pci_set_master(pdev);
15561 
15562 	/* Now that we have a PCI connection, we need to do the
15563 	 * low level device setup.  This is primarily setting up
15564 	 * the Admin Queue structures and then querying for the
15565 	 * device's current profile information.
15566 	 */
15567 	pf = i40e_alloc_pf(&pdev->dev);
15568 	if (!pf) {
15569 		err = -ENOMEM;
15570 		goto err_pf_alloc;
15571 	}
15572 	pf->next_vsi = 0;
15573 	pf->pdev = pdev;
15574 	set_bit(__I40E_DOWN, pf->state);
15575 
15576 	hw = &pf->hw;
15577 
15578 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15579 				I40E_MAX_CSR_SPACE);
15580 	/* We believe that the highest register to read is
15581 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15582 	 * is not less than that before mapping to prevent a
15583 	 * kernel panic.
15584 	 */
15585 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15586 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15587 			pf->ioremap_len);
15588 		err = -ENOMEM;
15589 		goto err_ioremap;
15590 	}
15591 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15592 	if (!hw->hw_addr) {
15593 		err = -EIO;
15594 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15595 			 (unsigned int)pci_resource_start(pdev, 0),
15596 			 pf->ioremap_len, err);
15597 		goto err_ioremap;
15598 	}
15599 	hw->vendor_id = pdev->vendor;
15600 	hw->device_id = pdev->device;
15601 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15602 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15603 	i40e_set_subsystem_device_id(hw);
15604 	hw->bus.device = PCI_SLOT(pdev->devfn);
15605 	hw->bus.func = PCI_FUNC(pdev->devfn);
15606 	hw->bus.bus_id = pdev->bus->number;
15607 
15608 	/* Select something other than the 802.1ad ethertype for the
15609 	 * switch to use internally and drop on ingress.
15610 	 */
15611 	hw->switch_tag = 0xffff;
15612 	hw->first_tag = ETH_P_8021AD;
15613 	hw->second_tag = ETH_P_8021Q;
15614 
15615 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15616 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15617 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15618 
15619 	/* set up the locks for the AQ, do this only once in probe
15620 	 * and destroy them only once in remove
15621 	 */
15622 	mutex_init(&hw->aq.asq_mutex);
15623 	mutex_init(&hw->aq.arq_mutex);
15624 
15625 	pf->msg_enable = netif_msg_init(debug,
15626 					NETIF_MSG_DRV |
15627 					NETIF_MSG_PROBE |
15628 					NETIF_MSG_LINK);
15629 	if (debug < -1)
15630 		pf->hw.debug_mask = debug;
15631 
15632 	/* do a special CORER for clearing PXE mode once at init */
15633 	if (hw->revision_id == 0 &&
15634 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15635 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15636 		i40e_flush(hw);
15637 		msleep(200);
15638 		pf->corer_count++;
15639 
15640 		i40e_clear_pxe_mode(hw);
15641 	}
15642 
15643 	/* Reset here to make sure all is clean and to define PF 'n' */
15644 	i40e_clear_hw(hw);
15645 
15646 	err = i40e_set_mac_type(hw);
15647 	if (err) {
15648 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15649 			 err);
15650 		goto err_pf_reset;
15651 	}
15652 
15653 	err = i40e_handle_resets(pf);
15654 	if (err)
15655 		goto err_pf_reset;
15656 
15657 	i40e_check_recovery_mode(pf);
15658 
15659 	if (is_kdump_kernel()) {
15660 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15661 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15662 	} else {
15663 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15664 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15665 	}
15666 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15667 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15668 
15669 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15670 		 "%s-%s:misc",
15671 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15672 
15673 	err = i40e_init_shared_code(hw);
15674 	if (err) {
15675 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15676 			 err);
15677 		goto err_pf_reset;
15678 	}
15679 
15680 	/* set up a default setting for link flow control */
15681 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15682 
15683 	err = i40e_init_adminq(hw);
15684 	if (err) {
15685 		if (err == -EIO)
15686 			dev_info(&pdev->dev,
15687 				 "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",
15688 				 hw->aq.api_maj_ver,
15689 				 hw->aq.api_min_ver,
15690 				 I40E_FW_API_VERSION_MAJOR,
15691 				 I40E_FW_MINOR_VERSION(hw));
15692 		else
15693 			dev_info(&pdev->dev,
15694 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15695 
15696 		goto err_pf_reset;
15697 	}
15698 	i40e_get_oem_version(hw);
15699 	i40e_get_pba_string(hw);
15700 
15701 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15702 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15703 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15704 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15705 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15706 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15707 		 hw->subsystem_device_id);
15708 
15709 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15710 				  I40E_FW_MINOR_VERSION(hw) + 1))
15711 		dev_dbg(&pdev->dev,
15712 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15713 			 hw->aq.api_maj_ver,
15714 			 hw->aq.api_min_ver,
15715 			 I40E_FW_API_VERSION_MAJOR,
15716 			 I40E_FW_MINOR_VERSION(hw));
15717 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15718 		dev_info(&pdev->dev,
15719 			 "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",
15720 			 hw->aq.api_maj_ver,
15721 			 hw->aq.api_min_ver,
15722 			 I40E_FW_API_VERSION_MAJOR,
15723 			 I40E_FW_MINOR_VERSION(hw));
15724 
15725 	i40e_verify_eeprom(pf);
15726 
15727 	/* Rev 0 hardware was never productized */
15728 	if (hw->revision_id < 1)
15729 		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");
15730 
15731 	i40e_clear_pxe_mode(hw);
15732 
15733 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15734 	if (err)
15735 		goto err_adminq_setup;
15736 
15737 	err = i40e_sw_init(pf);
15738 	if (err) {
15739 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15740 		goto err_sw_init;
15741 	}
15742 
15743 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15744 		return i40e_init_recovery_mode(pf, hw);
15745 
15746 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15747 				hw->func_caps.num_rx_qp, 0, 0);
15748 	if (err) {
15749 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15750 		goto err_init_lan_hmc;
15751 	}
15752 
15753 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15754 	if (err) {
15755 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15756 		err = -ENOENT;
15757 		goto err_configure_lan_hmc;
15758 	}
15759 
15760 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15761 	 * Ignore error return codes because if it was already disabled via
15762 	 * hardware settings this will fail
15763 	 */
15764 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15765 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15766 		i40e_aq_stop_lldp(hw, true, false, NULL);
15767 	}
15768 
15769 	/* allow a platform config to override the HW addr */
15770 	i40e_get_platform_mac_addr(pdev, pf);
15771 
15772 	if (!is_valid_ether_addr(hw->mac.addr)) {
15773 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15774 		err = -EIO;
15775 		goto err_mac_addr;
15776 	}
15777 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15778 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15779 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15780 	if (is_valid_ether_addr(hw->mac.port_addr))
15781 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15782 
15783 	i40e_ptp_alloc_pins(pf);
15784 	pci_set_drvdata(pdev, pf);
15785 	pci_save_state(pdev);
15786 
15787 #ifdef CONFIG_I40E_DCB
15788 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15789 	(!status &&
15790 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15791 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15792 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15793 	dev_info(&pdev->dev,
15794 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15795 			"FW LLDP is disabled\n" :
15796 			"FW LLDP is enabled\n");
15797 
15798 	/* Enable FW to write default DCB config on link-up */
15799 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15800 
15801 	err = i40e_init_pf_dcb(pf);
15802 	if (err) {
15803 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15804 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15805 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15806 		/* Continue without DCB enabled */
15807 	}
15808 #endif /* CONFIG_I40E_DCB */
15809 
15810 	/* set up periodic task facility */
15811 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15812 	pf->service_timer_period = HZ;
15813 
15814 	INIT_WORK(&pf->service_task, i40e_service_task);
15815 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15816 
15817 	/* NVM bit on means WoL disabled for the port */
15818 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15819 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15820 		pf->wol_en = false;
15821 	else
15822 		pf->wol_en = true;
15823 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15824 
15825 	/* set up the main switch operations */
15826 	i40e_determine_queue_usage(pf);
15827 	err = i40e_init_interrupt_scheme(pf);
15828 	if (err)
15829 		goto err_switch_setup;
15830 
15831 	/* Reduce Tx and Rx pairs for kdump
15832 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15833 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15834 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15835 	 */
15836 	if (is_kdump_kernel())
15837 		pf->num_lan_msix = 1;
15838 
15839 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15840 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15841 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15842 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15843 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15844 						    UDP_TUNNEL_TYPE_GENEVE;
15845 
15846 	/* The number of VSIs reported by the FW is the minimum guaranteed
15847 	 * to us; HW supports far more and we share the remaining pool with
15848 	 * the other PFs. We allocate space for more than the guarantee with
15849 	 * the understanding that we might not get them all later.
15850 	 */
15851 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15852 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15853 	else
15854 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15855 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15856 		dev_warn(&pf->pdev->dev,
15857 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15858 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15859 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15860 	}
15861 
15862 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15863 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15864 			  GFP_KERNEL);
15865 	if (!pf->vsi) {
15866 		err = -ENOMEM;
15867 		goto err_switch_setup;
15868 	}
15869 
15870 #ifdef CONFIG_PCI_IOV
15871 	/* prep for VF support */
15872 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15873 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15874 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15875 		if (pci_num_vf(pdev))
15876 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15877 	}
15878 #endif
15879 	err = i40e_setup_pf_switch(pf, false, false);
15880 	if (err) {
15881 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15882 		goto err_vsis;
15883 	}
15884 
15885 	vsi = i40e_pf_get_main_vsi(pf);
15886 	INIT_LIST_HEAD(&vsi->ch_list);
15887 
15888 	/* if FDIR VSI was set up, start it now */
15889 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
15890 	if (vsi)
15891 		i40e_vsi_open(vsi);
15892 
15893 	/* The driver only wants link up/down and module qualification
15894 	 * reports from firmware.  Note the negative logic.
15895 	 */
15896 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15897 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15898 					 I40E_AQ_EVENT_MEDIA_NA |
15899 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15900 	if (err)
15901 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
15902 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
15903 
15904 	/* VF MDD event logs are rate limited to one second intervals */
15905 	ratelimit_state_init(&pf->mdd_message_rate_limit, 1 * HZ, 1);
15906 
15907 	/* Reconfigure hardware for allowing smaller MSS in the case
15908 	 * of TSO, so that we avoid the MDD being fired and causing
15909 	 * a reset in the case of small MSS+TSO.
15910 	 */
15911 	val = rd32(hw, I40E_REG_MSS);
15912 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15913 		val &= ~I40E_REG_MSS_MIN_MASK;
15914 		val |= I40E_64BYTE_MSS;
15915 		wr32(hw, I40E_REG_MSS, val);
15916 	}
15917 
15918 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
15919 		msleep(75);
15920 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15921 		if (err)
15922 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
15923 				 ERR_PTR(err),
15924 				 libie_aq_str(pf->hw.aq.asq_last_status));
15925 	}
15926 	/* The main driver is (mostly) up and happy. We need to set this state
15927 	 * before setting up the misc vector or we get a race and the vector
15928 	 * ends up disabled forever.
15929 	 */
15930 	clear_bit(__I40E_DOWN, pf->state);
15931 
15932 	/* In case of MSIX we are going to setup the misc vector right here
15933 	 * to handle admin queue events etc. In case of legacy and MSI
15934 	 * the misc functionality and queue processing is combined in
15935 	 * the same vector and that gets setup at open.
15936 	 */
15937 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15938 		err = i40e_setup_misc_vector(pf);
15939 		if (err) {
15940 			dev_info(&pdev->dev,
15941 				 "setup of misc vector failed: %d\n", err);
15942 			i40e_cloud_filter_exit(pf);
15943 			i40e_fdir_teardown(pf);
15944 			goto err_vsis;
15945 		}
15946 	}
15947 
15948 #ifdef CONFIG_PCI_IOV
15949 	/* prep for VF support */
15950 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15951 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15952 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15953 		/* disable link interrupts for VFs */
15954 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15955 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15956 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15957 		i40e_flush(hw);
15958 
15959 		if (pci_num_vf(pdev)) {
15960 			dev_info(&pdev->dev,
15961 				 "Active VFs found, allocating resources.\n");
15962 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15963 			if (err)
15964 				dev_info(&pdev->dev,
15965 					 "Error %d allocating resources for existing VFs\n",
15966 					 err);
15967 		}
15968 	}
15969 #endif /* CONFIG_PCI_IOV */
15970 
15971 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
15972 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15973 						      pf->num_iwarp_msix,
15974 						      I40E_IWARP_IRQ_PILE_ID);
15975 		if (pf->iwarp_base_vector < 0) {
15976 			dev_info(&pdev->dev,
15977 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15978 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15979 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15980 		}
15981 	}
15982 
15983 	i40e_dbg_pf_init(pf);
15984 
15985 	/* tell the firmware that we're starting */
15986 	i40e_send_version(pf);
15987 
15988 	/* since everything's happy, start the service_task timer */
15989 	mod_timer(&pf->service_timer,
15990 		  round_jiffies(jiffies + pf->service_timer_period));
15991 
15992 	/* add this PF to client device list and launch a client service task */
15993 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
15994 		err = i40e_lan_add_device(pf);
15995 		if (err)
15996 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15997 				 err);
15998 	}
15999 
16000 #define PCI_SPEED_SIZE 8
16001 #define PCI_WIDTH_SIZE 8
16002 	/* Devices on the IOSF bus do not have this information
16003 	 * and will report PCI Gen 1 x 1 by default so don't bother
16004 	 * checking them.
16005 	 */
16006 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
16007 		char speed[PCI_SPEED_SIZE] = "Unknown";
16008 		char width[PCI_WIDTH_SIZE] = "Unknown";
16009 
16010 		/* Get the negotiated link width and speed from PCI config
16011 		 * space
16012 		 */
16013 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16014 					  &link_status);
16015 
16016 		i40e_set_pci_config_data(hw, link_status);
16017 
16018 		switch (hw->bus.speed) {
16019 		case i40e_bus_speed_8000:
16020 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16021 		case i40e_bus_speed_5000:
16022 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16023 		case i40e_bus_speed_2500:
16024 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16025 		default:
16026 			break;
16027 		}
16028 		switch (hw->bus.width) {
16029 		case i40e_bus_width_pcie_x8:
16030 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16031 		case i40e_bus_width_pcie_x4:
16032 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16033 		case i40e_bus_width_pcie_x2:
16034 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16035 		case i40e_bus_width_pcie_x1:
16036 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16037 		default:
16038 			break;
16039 		}
16040 
16041 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16042 			 speed, width);
16043 
16044 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16045 		    hw->bus.speed < i40e_bus_speed_8000) {
16046 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16047 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16048 		}
16049 	}
16050 
16051 	/* get the requested speeds from the fw */
16052 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16053 	if (err)
16054 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16055 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16056 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16057 
16058 	/* set the FEC config due to the board capabilities */
16059 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16060 
16061 	/* get the supported phy types from the fw */
16062 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16063 	if (err)
16064 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16065 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16066 
16067 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16068 
16069 	err = i40e_aq_set_mac_config(hw, MAX_FRAME_SIZE_DEFAULT, NULL);
16070 	if (err)
16071 		dev_warn(&pdev->dev, "set mac config ret = %pe last_status = %s\n",
16072 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16073 
16074 	/* Make sure the MFS is set to the expected value */
16075 	val = rd32(hw, I40E_PRTGL_SAH);
16076 	FIELD_MODIFY(I40E_PRTGL_SAH_MFS_MASK, &val, MAX_FRAME_SIZE_DEFAULT);
16077 	wr32(hw, I40E_PRTGL_SAH, val);
16078 
16079 	/* Add a filter to drop all Flow control frames from any VSI from being
16080 	 * transmitted. By doing so we stop a malicious VF from sending out
16081 	 * PAUSE or PFC frames and potentially controlling traffic for other
16082 	 * PF/VF VSIs.
16083 	 * The FW can still send Flow control frames if enabled.
16084 	 */
16085 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16086 						       pf->main_vsi_seid);
16087 
16088 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16089 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16090 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16091 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16092 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16093 	/* print a string summarizing features */
16094 	i40e_print_features(pf);
16095 
16096 	i40e_devlink_register(pf);
16097 
16098 	return 0;
16099 
16100 	/* Unwind what we've done if something failed in the setup */
16101 err_vsis:
16102 	set_bit(__I40E_DOWN, pf->state);
16103 	i40e_clear_interrupt_scheme(pf);
16104 	kfree(pf->vsi);
16105 err_switch_setup:
16106 	i40e_reset_interrupt_capability(pf);
16107 	timer_shutdown_sync(&pf->service_timer);
16108 err_mac_addr:
16109 err_configure_lan_hmc:
16110 	(void)i40e_shutdown_lan_hmc(hw);
16111 err_init_lan_hmc:
16112 	kfree(pf->qp_pile);
16113 err_sw_init:
16114 err_adminq_setup:
16115 err_pf_reset:
16116 	iounmap(hw->hw_addr);
16117 err_ioremap:
16118 	i40e_free_pf(pf);
16119 err_pf_alloc:
16120 	pci_release_mem_regions(pdev);
16121 err_pci_reg:
16122 err_dma:
16123 	pci_disable_device(pdev);
16124 	return err;
16125 }
16126 
16127 /**
16128  * i40e_remove - Device removal routine
16129  * @pdev: PCI device information struct
16130  *
16131  * i40e_remove is called by the PCI subsystem to alert the driver
16132  * that is should release a PCI device.  This could be caused by a
16133  * Hot-Plug event, or because the driver is going to be removed from
16134  * memory.
16135  **/
i40e_remove(struct pci_dev * pdev)16136 static void i40e_remove(struct pci_dev *pdev)
16137 {
16138 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16139 	struct i40e_hw *hw = &pf->hw;
16140 	struct i40e_vsi *vsi;
16141 	struct i40e_veb *veb;
16142 	int ret_code;
16143 	int i;
16144 
16145 	i40e_devlink_unregister(pf);
16146 
16147 	i40e_dbg_pf_exit(pf);
16148 
16149 	i40e_ptp_stop(pf);
16150 
16151 	/* Disable RSS in hw */
16152 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16153 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16154 
16155 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16156 	 * flags, once they are set, i40e_rebuild should not be called as
16157 	 * i40e_prep_for_reset always returns early.
16158 	 */
16159 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16160 		usleep_range(1000, 2000);
16161 	set_bit(__I40E_IN_REMOVE, pf->state);
16162 
16163 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16164 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16165 		i40e_free_vfs(pf);
16166 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16167 	}
16168 	/* no more scheduling of any task */
16169 	set_bit(__I40E_SUSPENDED, pf->state);
16170 	set_bit(__I40E_DOWN, pf->state);
16171 	if (pf->service_timer.function)
16172 		timer_shutdown_sync(&pf->service_timer);
16173 	if (pf->service_task.func)
16174 		cancel_work_sync(&pf->service_task);
16175 
16176 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16177 		struct i40e_vsi *vsi = pf->vsi[0];
16178 
16179 		/* We know that we have allocated only one vsi for this PF,
16180 		 * it was just for registering netdevice, so the interface
16181 		 * could be visible in the 'ifconfig' output
16182 		 */
16183 		unregister_netdev(vsi->netdev);
16184 		free_netdev(vsi->netdev);
16185 
16186 		goto unmap;
16187 	}
16188 
16189 	/* Client close must be called explicitly here because the timer
16190 	 * has been stopped.
16191 	 */
16192 	i40e_notify_client_of_netdev_close(pf, false);
16193 
16194 	i40e_fdir_teardown(pf);
16195 
16196 	/* If there is a switch structure or any orphans, remove them.
16197 	 * This will leave only the PF's VSI remaining.
16198 	 */
16199 	i40e_pf_for_each_veb(pf, i, veb)
16200 		if (veb->uplink_seid == pf->mac_seid ||
16201 		    veb->uplink_seid == 0)
16202 			i40e_switch_branch_release(veb);
16203 
16204 	/* Now we can shutdown the PF's VSIs, just before we kill
16205 	 * adminq and hmc.
16206 	 */
16207 	i40e_pf_for_each_vsi(pf, i, vsi) {
16208 		i40e_vsi_close(vsi);
16209 		i40e_vsi_release(vsi);
16210 		pf->vsi[i] = NULL;
16211 	}
16212 
16213 	i40e_cloud_filter_exit(pf);
16214 
16215 	/* remove attached clients */
16216 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16217 		ret_code = i40e_lan_del_device(pf);
16218 		if (ret_code)
16219 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16220 				 ret_code);
16221 	}
16222 
16223 	/* shutdown and destroy the HMC */
16224 	if (hw->hmc.hmc_obj) {
16225 		ret_code = i40e_shutdown_lan_hmc(hw);
16226 		if (ret_code)
16227 			dev_warn(&pdev->dev,
16228 				 "Failed to destroy the HMC resources: %d\n",
16229 				 ret_code);
16230 	}
16231 
16232 unmap:
16233 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16234 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16235 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16236 		free_irq(pf->pdev->irq, pf);
16237 
16238 	/* shutdown the adminq */
16239 	i40e_shutdown_adminq(hw);
16240 
16241 	/* destroy the locks only once, here */
16242 	mutex_destroy(&hw->aq.arq_mutex);
16243 	mutex_destroy(&hw->aq.asq_mutex);
16244 
16245 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16246 	rtnl_lock();
16247 	i40e_clear_interrupt_scheme(pf);
16248 	i40e_pf_for_each_vsi(pf, i, vsi) {
16249 		if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16250 			i40e_vsi_clear_rings(vsi);
16251 
16252 		i40e_vsi_clear(vsi);
16253 		pf->vsi[i] = NULL;
16254 	}
16255 	rtnl_unlock();
16256 
16257 	i40e_pf_for_each_veb(pf, i, veb) {
16258 		kfree(veb);
16259 		pf->veb[i] = NULL;
16260 	}
16261 
16262 	kfree(pf->qp_pile);
16263 	kfree(pf->vsi);
16264 
16265 	iounmap(hw->hw_addr);
16266 	i40e_free_pf(pf);
16267 	pci_release_mem_regions(pdev);
16268 
16269 	pci_disable_device(pdev);
16270 }
16271 
16272 /**
16273  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16274  * using the mac_address_write admin q function
16275  * @pf: pointer to i40e_pf struct
16276  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)16277 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16278 {
16279 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
16280 	struct i40e_hw *hw = &pf->hw;
16281 	u8 mac_addr[6];
16282 	u16 flags = 0;
16283 	int ret;
16284 
16285 	/* Get current MAC address in case it's an LAA */
16286 	if (main_vsi && main_vsi->netdev) {
16287 		ether_addr_copy(mac_addr, main_vsi->netdev->dev_addr);
16288 	} else {
16289 		dev_err(&pf->pdev->dev,
16290 			"Failed to retrieve MAC address; using default\n");
16291 		ether_addr_copy(mac_addr, hw->mac.addr);
16292 	}
16293 
16294 	/* The FW expects the mac address write cmd to first be called with
16295 	 * one of these flags before calling it again with the multicast
16296 	 * enable flags.
16297 	 */
16298 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16299 
16300 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16301 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16302 
16303 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16304 	if (ret) {
16305 		dev_err(&pf->pdev->dev,
16306 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16307 		return;
16308 	}
16309 
16310 	flags = I40E_AQC_MC_MAG_EN
16311 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16312 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16313 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16314 	if (ret)
16315 		dev_err(&pf->pdev->dev,
16316 			"Failed to enable Multicast Magic Packet wake up\n");
16317 }
16318 
16319 /**
16320  * i40e_io_suspend - suspend all IO operations
16321  * @pf: pointer to i40e_pf struct
16322  *
16323  **/
i40e_io_suspend(struct i40e_pf * pf)16324 static int i40e_io_suspend(struct i40e_pf *pf)
16325 {
16326 	struct i40e_hw *hw = &pf->hw;
16327 
16328 	set_bit(__I40E_DOWN, pf->state);
16329 
16330 	/* Ensure service task will not be running */
16331 	timer_delete_sync(&pf->service_timer);
16332 	cancel_work_sync(&pf->service_task);
16333 
16334 	/* Client close must be called explicitly here because the timer
16335 	 * has been stopped.
16336 	 */
16337 	i40e_notify_client_of_netdev_close(pf, false);
16338 
16339 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16340 	    pf->wol_en)
16341 		i40e_enable_mc_magic_wake(pf);
16342 
16343 	/* Since we're going to destroy queues during the
16344 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16345 	 * whole section
16346 	 */
16347 	rtnl_lock();
16348 
16349 	i40e_prep_for_reset(pf);
16350 
16351 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16352 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16353 
16354 	/* Clear the interrupt scheme and release our IRQs so that the system
16355 	 * can safely hibernate even when there are a large number of CPUs.
16356 	 * Otherwise hibernation might fail when mapping all the vectors back
16357 	 * to CPU0.
16358 	 */
16359 	i40e_clear_interrupt_scheme(pf);
16360 
16361 	rtnl_unlock();
16362 
16363 	return 0;
16364 }
16365 
16366 /**
16367  * i40e_io_resume - resume IO operations
16368  * @pf: pointer to i40e_pf struct
16369  *
16370  **/
i40e_io_resume(struct i40e_pf * pf)16371 static int i40e_io_resume(struct i40e_pf *pf)
16372 {
16373 	struct device *dev = &pf->pdev->dev;
16374 	int err;
16375 
16376 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16377 	 * since we're going to be restoring queues
16378 	 */
16379 	rtnl_lock();
16380 
16381 	/* We cleared the interrupt scheme when we suspended, so we need to
16382 	 * restore it now to resume device functionality.
16383 	 */
16384 	err = i40e_restore_interrupt_scheme(pf);
16385 	if (err) {
16386 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16387 			err);
16388 	}
16389 
16390 	clear_bit(__I40E_DOWN, pf->state);
16391 	i40e_reset_and_rebuild(pf, false, true);
16392 
16393 	rtnl_unlock();
16394 
16395 	/* Clear suspended state last after everything is recovered */
16396 	clear_bit(__I40E_SUSPENDED, pf->state);
16397 
16398 	/* Restart the service task */
16399 	mod_timer(&pf->service_timer,
16400 		  round_jiffies(jiffies + pf->service_timer_period));
16401 
16402 	return 0;
16403 }
16404 
16405 /**
16406  * i40e_pci_error_detected - warning that something funky happened in PCI land
16407  * @pdev: PCI device information struct
16408  * @error: the type of PCI error
16409  *
16410  * Called to warn that something happened and the error handling steps
16411  * are in progress.  Allows the driver to quiesce things, be ready for
16412  * remediation.
16413  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)16414 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16415 						pci_channel_state_t error)
16416 {
16417 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16418 
16419 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16420 
16421 	if (!pf) {
16422 		dev_info(&pdev->dev,
16423 			 "Cannot recover - error happened during device probe\n");
16424 		return PCI_ERS_RESULT_DISCONNECT;
16425 	}
16426 
16427 	/* shutdown all operations */
16428 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16429 		i40e_io_suspend(pf);
16430 
16431 	/* Request a slot reset */
16432 	return PCI_ERS_RESULT_NEED_RESET;
16433 }
16434 
16435 /**
16436  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16437  * @pdev: PCI device information struct
16438  *
16439  * Called to find if the driver can work with the device now that
16440  * the pci slot has been reset.  If a basic connection seems good
16441  * (registers are readable and have sane content) then return a
16442  * happy little PCI_ERS_RESULT_xxx.
16443  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)16444 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16445 {
16446 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16447 	pci_ers_result_t result;
16448 	u32 reg;
16449 
16450 	dev_dbg(&pdev->dev, "%s\n", __func__);
16451 	/* enable I/O and memory of the device  */
16452 	if (pci_enable_device(pdev)) {
16453 		dev_info(&pdev->dev,
16454 			 "Cannot re-enable PCI device after reset.\n");
16455 		result = PCI_ERS_RESULT_DISCONNECT;
16456 	} else {
16457 		pci_set_master(pdev);
16458 		pci_restore_state(pdev);
16459 		pci_wake_from_d3(pdev, false);
16460 
16461 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16462 		if (reg == 0)
16463 			result = PCI_ERS_RESULT_RECOVERED;
16464 		else
16465 			result = PCI_ERS_RESULT_DISCONNECT;
16466 	}
16467 
16468 	return result;
16469 }
16470 
16471 /**
16472  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16473  * @pdev: PCI device information struct
16474  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)16475 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16476 {
16477 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16478 
16479 	i40e_prep_for_reset(pf);
16480 }
16481 
16482 /**
16483  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16484  * @pdev: PCI device information struct
16485  */
i40e_pci_error_reset_done(struct pci_dev * pdev)16486 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16487 {
16488 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16489 
16490 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16491 		return;
16492 
16493 	i40e_reset_and_rebuild(pf, false, false);
16494 #ifdef CONFIG_PCI_IOV
16495 	i40e_restore_all_vfs_msi_state(pdev);
16496 #endif /* CONFIG_PCI_IOV */
16497 }
16498 
16499 /**
16500  * i40e_pci_error_resume - restart operations after PCI error recovery
16501  * @pdev: PCI device information struct
16502  *
16503  * Called to allow the driver to bring things back up after PCI error
16504  * and/or reset recovery has finished.
16505  **/
i40e_pci_error_resume(struct pci_dev * pdev)16506 static void i40e_pci_error_resume(struct pci_dev *pdev)
16507 {
16508 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16509 
16510 	dev_dbg(&pdev->dev, "%s\n", __func__);
16511 	if (test_bit(__I40E_SUSPENDED, pf->state))
16512 		return;
16513 
16514 	i40e_io_resume(pf);
16515 }
16516 
16517 /**
16518  * i40e_shutdown - PCI callback for shutting down
16519  * @pdev: PCI device information struct
16520  **/
i40e_shutdown(struct pci_dev * pdev)16521 static void i40e_shutdown(struct pci_dev *pdev)
16522 {
16523 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16524 	struct i40e_hw *hw = &pf->hw;
16525 
16526 	set_bit(__I40E_SUSPENDED, pf->state);
16527 	set_bit(__I40E_DOWN, pf->state);
16528 
16529 	timer_delete_sync(&pf->service_timer);
16530 	cancel_work_sync(&pf->service_task);
16531 	i40e_cloud_filter_exit(pf);
16532 	i40e_fdir_teardown(pf);
16533 
16534 	/* Client close must be called explicitly here because the timer
16535 	 * has been stopped.
16536 	 */
16537 	i40e_notify_client_of_netdev_close(pf, false);
16538 
16539 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16540 	    pf->wol_en)
16541 		i40e_enable_mc_magic_wake(pf);
16542 
16543 	i40e_prep_for_reset(pf);
16544 
16545 	wr32(hw, I40E_PFPM_APM,
16546 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16547 	wr32(hw, I40E_PFPM_WUFC,
16548 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16549 
16550 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16551 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16552 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16553 		free_irq(pf->pdev->irq, pf);
16554 
16555 	/* Since we're going to destroy queues during the
16556 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16557 	 * whole section
16558 	 */
16559 	rtnl_lock();
16560 	i40e_clear_interrupt_scheme(pf);
16561 	rtnl_unlock();
16562 
16563 	if (system_state == SYSTEM_POWER_OFF) {
16564 		pci_wake_from_d3(pdev, pf->wol_en);
16565 		pci_set_power_state(pdev, PCI_D3hot);
16566 	}
16567 }
16568 
16569 /**
16570  * i40e_suspend - PM callback for moving to D3
16571  * @dev: generic device information structure
16572  **/
i40e_suspend(struct device * dev)16573 static int i40e_suspend(struct device *dev)
16574 {
16575 	struct i40e_pf *pf = dev_get_drvdata(dev);
16576 
16577 	/* If we're already suspended, then there is nothing to do */
16578 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16579 		return 0;
16580 	return i40e_io_suspend(pf);
16581 }
16582 
16583 /**
16584  * i40e_resume - PM callback for waking up from D3
16585  * @dev: generic device information structure
16586  **/
i40e_resume(struct device * dev)16587 static int i40e_resume(struct device *dev)
16588 {
16589 	struct i40e_pf *pf = dev_get_drvdata(dev);
16590 
16591 	/* If we're not suspended, then there is nothing to do */
16592 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16593 		return 0;
16594 	return i40e_io_resume(pf);
16595 }
16596 
16597 static const struct pci_error_handlers i40e_err_handler = {
16598 	.error_detected = i40e_pci_error_detected,
16599 	.slot_reset = i40e_pci_error_slot_reset,
16600 	.reset_prepare = i40e_pci_error_reset_prepare,
16601 	.reset_done = i40e_pci_error_reset_done,
16602 	.resume = i40e_pci_error_resume,
16603 };
16604 
16605 static DEFINE_SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16606 
16607 static struct pci_driver i40e_driver = {
16608 	.name     = i40e_driver_name,
16609 	.id_table = i40e_pci_tbl,
16610 	.probe    = i40e_probe,
16611 	.remove   = i40e_remove,
16612 	.driver.pm = pm_sleep_ptr(&i40e_pm_ops),
16613 	.shutdown = i40e_shutdown,
16614 	.err_handler = &i40e_err_handler,
16615 	.sriov_configure = i40e_pci_sriov_configure,
16616 };
16617 
16618 /**
16619  * i40e_init_module - Driver registration routine
16620  *
16621  * i40e_init_module is the first routine called when the driver is
16622  * loaded. All it does is register with the PCI subsystem.
16623  **/
i40e_init_module(void)16624 static int __init i40e_init_module(void)
16625 {
16626 	int err;
16627 
16628 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16629 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16630 
16631 	/* There is no need to throttle the number of active tasks because
16632 	 * each device limits its own task using a state bit for scheduling
16633 	 * the service task, and the device tasks do not interfere with each
16634 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16635 	 * since we need to be able to guarantee forward progress even under
16636 	 * memory pressure.
16637 	 */
16638 	i40e_wq = alloc_workqueue("%s", WQ_PERCPU, 0, i40e_driver_name);
16639 	if (!i40e_wq) {
16640 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16641 		return -ENOMEM;
16642 	}
16643 
16644 	i40e_dbg_init();
16645 	err = pci_register_driver(&i40e_driver);
16646 	if (err) {
16647 		destroy_workqueue(i40e_wq);
16648 		i40e_dbg_exit();
16649 		return err;
16650 	}
16651 
16652 	return 0;
16653 }
16654 module_init(i40e_init_module);
16655 
16656 /**
16657  * i40e_exit_module - Driver exit cleanup routine
16658  *
16659  * i40e_exit_module is called just before the driver is removed
16660  * from memory.
16661  **/
i40e_exit_module(void)16662 static void __exit i40e_exit_module(void)
16663 {
16664 	pci_unregister_driver(&i40e_driver);
16665 	destroy_workqueue(i40e_wq);
16666 	ida_destroy(&i40e_client_ida);
16667 	i40e_dbg_exit();
16668 }
16669 module_exit(i40e_exit_module);
16670