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/if_bridge.h>
7 #include <linux/if_macvlan.h>
8 #include <linux/module.h>
9 #include <net/pkt_cls.h>
10 #include <net/xdp_sock_drv.h>
11 
12 /* Local includes */
13 #include "i40e.h"
14 #include "i40e_devids.h"
15 #include "i40e_diag.h"
16 #include "i40e_lan_hmc.h"
17 #include "i40e_virtchnl_pf.h"
18 #include "i40e_xsk.h"
19 
20 /* All i40e tracepoints are defined by the include below, which
21  * must be included exactly once across the whole kernel with
22  * CREATE_TRACE_POINTS defined
23  */
24 #define CREATE_TRACE_POINTS
25 #include "i40e_trace.h"
26 
27 const char i40e_driver_name[] = "i40e";
28 static const char i40e_driver_string[] =
29 			"Intel(R) Ethernet Connection XL710 Network Driver";
30 
31 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
32 
33 /* a bit of forward declarations */
34 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
35 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
36 static int i40e_add_vsi(struct i40e_vsi *vsi);
37 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
38 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
39 static int i40e_setup_misc_vector(struct i40e_pf *pf);
40 static void i40e_determine_queue_usage(struct i40e_pf *pf);
41 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
42 static void i40e_prep_for_reset(struct i40e_pf *pf);
43 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
44 				   bool lock_acquired);
45 static int i40e_reset(struct i40e_pf *pf);
46 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
47 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
48 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
49 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
50 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
51 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
52 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
53 static int i40e_get_capabilities(struct i40e_pf *pf,
54 				 enum i40e_admin_queue_opc list_type);
55 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
56 
57 /* i40e_pci_tbl - PCI Device ID Table
58  *
59  * Last entry must be all 0s
60  *
61  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
62  *   Class, Class Mask, private data (not used) }
63  */
64 static const struct pci_device_id i40e_pci_tbl[] = {
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
91 	/* required last entry */
92 	{0, }
93 };
94 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
95 
96 #define I40E_MAX_VF_COUNT 128
97 static int debug = -1;
98 module_param(debug, uint, 0);
99 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
100 
101 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
102 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103 MODULE_LICENSE("GPL v2");
104 
105 static struct workqueue_struct *i40e_wq;
106 
netdev_hw_addr_refcnt(struct i40e_mac_filter * f,struct net_device * netdev,int delta)107 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
108 				  struct net_device *netdev, int delta)
109 {
110 	struct netdev_hw_addr_list *ha_list;
111 	struct netdev_hw_addr *ha;
112 
113 	if (!f || !netdev)
114 		return;
115 
116 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
117 		ha_list = &netdev->uc;
118 	else
119 		ha_list = &netdev->mc;
120 
121 	netdev_hw_addr_list_for_each(ha, ha_list) {
122 		if (ether_addr_equal(ha->addr, f->macaddr)) {
123 			ha->refcount += delta;
124 			if (ha->refcount <= 0)
125 				ha->refcount = 1;
126 			break;
127 		}
128 	}
129 }
130 
131 /**
132  * i40e_hw_to_dev - get device pointer from the hardware structure
133  * @hw: pointer to the device HW structure
134  **/
i40e_hw_to_dev(struct i40e_hw * hw)135 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
136 {
137 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
138 
139 	return &pf->pdev->dev;
140 }
141 
142 /**
143  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to fill out
146  * @size: size of memory requested
147  * @alignment: what to align the allocation to
148  **/
i40e_allocate_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem,u64 size,u32 alignment)149 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
150 			  u64 size, u32 alignment)
151 {
152 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
153 
154 	mem->size = ALIGN(size, alignment);
155 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
156 				     GFP_KERNEL);
157 	if (!mem->va)
158 		return -ENOMEM;
159 
160 	return 0;
161 }
162 
163 /**
164  * i40e_free_dma_mem - OS specific memory free for shared code
165  * @hw:   pointer to the HW structure
166  * @mem:  ptr to mem struct to free
167  **/
i40e_free_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem)168 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
169 {
170 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
171 
172 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
173 	mem->va = NULL;
174 	mem->pa = 0;
175 	mem->size = 0;
176 
177 	return 0;
178 }
179 
180 /**
181  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
182  * @hw:   pointer to the HW structure
183  * @mem:  ptr to mem struct to fill out
184  * @size: size of memory requested
185  **/
i40e_allocate_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)186 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
187 			   u32 size)
188 {
189 	mem->size = size;
190 	mem->va = kzalloc(size, GFP_KERNEL);
191 
192 	if (!mem->va)
193 		return -ENOMEM;
194 
195 	return 0;
196 }
197 
198 /**
199  * i40e_free_virt_mem - OS specific memory free for shared code
200  * @hw:   pointer to the HW structure
201  * @mem:  ptr to mem struct to free
202  **/
i40e_free_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem)203 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
204 {
205 	/* it's ok to kfree a NULL pointer */
206 	kfree(mem->va);
207 	mem->va = NULL;
208 	mem->size = 0;
209 
210 	return 0;
211 }
212 
213 /**
214  * i40e_get_lump - find a lump of free generic resource
215  * @pf: board private structure
216  * @pile: the pile of resource to search
217  * @needed: the number of items needed
218  * @id: an owner id to stick on the items assigned
219  *
220  * Returns the base item index of the lump, or negative for error
221  **/
i40e_get_lump(struct i40e_pf * pf,struct i40e_lump_tracking * pile,u16 needed,u16 id)222 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
223 			 u16 needed, u16 id)
224 {
225 	int ret = -ENOMEM;
226 	int i, j;
227 
228 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
229 		dev_info(&pf->pdev->dev,
230 			 "param err: pile=%s needed=%d id=0x%04x\n",
231 			 pile ? "<valid>" : "<null>", needed, id);
232 		return -EINVAL;
233 	}
234 
235 	/* Allocate last queue in the pile for FDIR VSI queue
236 	 * so it doesn't fragment the qp_pile
237 	 */
238 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
239 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
240 			dev_err(&pf->pdev->dev,
241 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
242 				pile->num_entries - 1);
243 			return -ENOMEM;
244 		}
245 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
246 		return pile->num_entries - 1;
247 	}
248 
249 	i = 0;
250 	while (i < pile->num_entries) {
251 		/* skip already allocated entries */
252 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
253 			i++;
254 			continue;
255 		}
256 
257 		/* do we have enough in this lump? */
258 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
259 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
260 				break;
261 		}
262 
263 		if (j == needed) {
264 			/* there was enough, so assign it to the requestor */
265 			for (j = 0; j < needed; j++)
266 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
267 			ret = i;
268 			break;
269 		}
270 
271 		/* not enough, so skip over it and continue looking */
272 		i += j;
273 	}
274 
275 	return ret;
276 }
277 
278 /**
279  * i40e_put_lump - return a lump of generic resource
280  * @pile: the pile of resource to search
281  * @index: the base item index
282  * @id: the owner id of the items assigned
283  *
284  * Returns the count of items in the lump
285  **/
i40e_put_lump(struct i40e_lump_tracking * pile,u16 index,u16 id)286 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
287 {
288 	int valid_id = (id | I40E_PILE_VALID_BIT);
289 	int count = 0;
290 	u16 i;
291 
292 	if (!pile || index >= pile->num_entries)
293 		return -EINVAL;
294 
295 	for (i = index;
296 	     i < pile->num_entries && pile->list[i] == valid_id;
297 	     i++) {
298 		pile->list[i] = 0;
299 		count++;
300 	}
301 
302 
303 	return count;
304 }
305 
306 /**
307  * i40e_find_vsi_from_id - searches for the vsi with the given id
308  * @pf: the pf structure to search for the vsi
309  * @id: id of the vsi it is searching for
310  **/
i40e_find_vsi_from_id(struct i40e_pf * pf,u16 id)311 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
312 {
313 	int i;
314 
315 	for (i = 0; i < pf->num_alloc_vsi; i++)
316 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
317 			return pf->vsi[i];
318 
319 	return NULL;
320 }
321 
322 /**
323  * i40e_service_event_schedule - Schedule the service task to wake up
324  * @pf: board private structure
325  *
326  * If not already scheduled, this puts the task into the work queue
327  **/
i40e_service_event_schedule(struct i40e_pf * pf)328 void i40e_service_event_schedule(struct i40e_pf *pf)
329 {
330 	if ((!test_bit(__I40E_DOWN, pf->state) &&
331 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
332 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
333 		queue_work(i40e_wq, &pf->service_task);
334 }
335 
336 /**
337  * i40e_tx_timeout - Respond to a Tx Hang
338  * @netdev: network interface device structure
339  * @txqueue: queue number timing out
340  *
341  * If any port has noticed a Tx timeout, it is likely that the whole
342  * device is munged, not just the one netdev port, so go for the full
343  * reset.
344  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)345 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
346 {
347 	struct i40e_netdev_priv *np = netdev_priv(netdev);
348 	struct i40e_vsi *vsi = np->vsi;
349 	struct i40e_pf *pf = vsi->back;
350 	struct i40e_ring *tx_ring = NULL;
351 	unsigned int i;
352 	u32 head, val;
353 
354 	pf->tx_timeout_count++;
355 
356 	/* with txqueue index, find the tx_ring struct */
357 	for (i = 0; i < vsi->num_queue_pairs; i++) {
358 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
359 			if (txqueue ==
360 			    vsi->tx_rings[i]->queue_index) {
361 				tx_ring = vsi->tx_rings[i];
362 				break;
363 			}
364 		}
365 	}
366 
367 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
368 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
369 	else if (time_before(jiffies,
370 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
371 		return;   /* don't do any new action before the next timeout */
372 
373 	/* don't kick off another recovery if one is already pending */
374 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
375 		return;
376 
377 	if (tx_ring) {
378 		head = i40e_get_head(tx_ring);
379 		/* Read interrupt register */
380 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
381 			val = rd32(&pf->hw,
382 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
383 						tx_ring->vsi->base_vector - 1));
384 		else
385 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
386 
387 		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",
388 			    vsi->seid, txqueue, tx_ring->next_to_clean,
389 			    head, tx_ring->next_to_use,
390 			    readl(tx_ring->tail), val);
391 	}
392 
393 	pf->tx_timeout_last_recovery = jiffies;
394 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
395 		    pf->tx_timeout_recovery_level, txqueue);
396 
397 	switch (pf->tx_timeout_recovery_level) {
398 	case 1:
399 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
400 		break;
401 	case 2:
402 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
403 		break;
404 	case 3:
405 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
406 		break;
407 	default:
408 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
409 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
410 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
411 		break;
412 	}
413 
414 	i40e_service_event_schedule(pf);
415 	pf->tx_timeout_recovery_level++;
416 }
417 
418 /**
419  * i40e_get_vsi_stats_struct - Get System Network Statistics
420  * @vsi: the VSI we care about
421  *
422  * Returns the address of the device statistics structure.
423  * The statistics are actually updated from the service task.
424  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)425 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
426 {
427 	return &vsi->net_stats;
428 }
429 
430 /**
431  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
432  * @ring: Tx ring to get statistics from
433  * @stats: statistics entry to be updated
434  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)435 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
436 					    struct rtnl_link_stats64 *stats)
437 {
438 	u64 bytes, packets;
439 	unsigned int start;
440 
441 	do {
442 		start = u64_stats_fetch_begin(&ring->syncp);
443 		packets = ring->stats.packets;
444 		bytes   = ring->stats.bytes;
445 	} while (u64_stats_fetch_retry(&ring->syncp, start));
446 
447 	stats->tx_packets += packets;
448 	stats->tx_bytes   += bytes;
449 }
450 
451 /**
452  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
453  * @netdev: network interface device structure
454  * @stats: data structure to store statistics
455  *
456  * Returns the address of the device statistics structure.
457  * The statistics are actually updated from the service task.
458  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)459 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
460 				  struct rtnl_link_stats64 *stats)
461 {
462 	struct i40e_netdev_priv *np = netdev_priv(netdev);
463 	struct i40e_vsi *vsi = np->vsi;
464 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
465 	struct i40e_ring *ring;
466 	int i;
467 
468 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
469 		return;
470 
471 	if (!vsi->tx_rings)
472 		return;
473 
474 	rcu_read_lock();
475 	for (i = 0; i < vsi->num_queue_pairs; i++) {
476 		u64 bytes, packets;
477 		unsigned int start;
478 
479 		ring = READ_ONCE(vsi->tx_rings[i]);
480 		if (!ring)
481 			continue;
482 		i40e_get_netdev_stats_struct_tx(ring, stats);
483 
484 		if (i40e_enabled_xdp_vsi(vsi)) {
485 			ring = READ_ONCE(vsi->xdp_rings[i]);
486 			if (!ring)
487 				continue;
488 			i40e_get_netdev_stats_struct_tx(ring, stats);
489 		}
490 
491 		ring = READ_ONCE(vsi->rx_rings[i]);
492 		if (!ring)
493 			continue;
494 		do {
495 			start   = u64_stats_fetch_begin(&ring->syncp);
496 			packets = ring->stats.packets;
497 			bytes   = ring->stats.bytes;
498 		} while (u64_stats_fetch_retry(&ring->syncp, start));
499 
500 		stats->rx_packets += packets;
501 		stats->rx_bytes   += bytes;
502 
503 	}
504 	rcu_read_unlock();
505 
506 	/* following stats updated by i40e_watchdog_subtask() */
507 	stats->multicast	= vsi_stats->multicast;
508 	stats->tx_errors	= vsi_stats->tx_errors;
509 	stats->tx_dropped	= vsi_stats->tx_dropped;
510 	stats->rx_errors	= vsi_stats->rx_errors;
511 	stats->rx_dropped	= vsi_stats->rx_dropped;
512 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
513 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
514 }
515 
516 /**
517  * i40e_vsi_reset_stats - Resets all stats of the given vsi
518  * @vsi: the VSI to have its stats reset
519  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)520 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
521 {
522 	struct rtnl_link_stats64 *ns;
523 	int i;
524 
525 	if (!vsi)
526 		return;
527 
528 	ns = i40e_get_vsi_stats_struct(vsi);
529 	memset(ns, 0, sizeof(*ns));
530 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
531 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
532 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
533 	if (vsi->rx_rings && vsi->rx_rings[0]) {
534 		for (i = 0; i < vsi->num_queue_pairs; i++) {
535 			memset(&vsi->rx_rings[i]->stats, 0,
536 			       sizeof(vsi->rx_rings[i]->stats));
537 			memset(&vsi->rx_rings[i]->rx_stats, 0,
538 			       sizeof(vsi->rx_rings[i]->rx_stats));
539 			memset(&vsi->tx_rings[i]->stats, 0,
540 			       sizeof(vsi->tx_rings[i]->stats));
541 			memset(&vsi->tx_rings[i]->tx_stats, 0,
542 			       sizeof(vsi->tx_rings[i]->tx_stats));
543 		}
544 	}
545 	vsi->stat_offsets_loaded = false;
546 }
547 
548 /**
549  * i40e_pf_reset_stats - Reset all of the stats for the given PF
550  * @pf: the PF to be reset
551  **/
i40e_pf_reset_stats(struct i40e_pf * pf)552 void i40e_pf_reset_stats(struct i40e_pf *pf)
553 {
554 	int i;
555 
556 	memset(&pf->stats, 0, sizeof(pf->stats));
557 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
558 	pf->stat_offsets_loaded = false;
559 
560 	for (i = 0; i < I40E_MAX_VEB; i++) {
561 		if (pf->veb[i]) {
562 			memset(&pf->veb[i]->stats, 0,
563 			       sizeof(pf->veb[i]->stats));
564 			memset(&pf->veb[i]->stats_offsets, 0,
565 			       sizeof(pf->veb[i]->stats_offsets));
566 			memset(&pf->veb[i]->tc_stats, 0,
567 			       sizeof(pf->veb[i]->tc_stats));
568 			memset(&pf->veb[i]->tc_stats_offsets, 0,
569 			       sizeof(pf->veb[i]->tc_stats_offsets));
570 			pf->veb[i]->stat_offsets_loaded = false;
571 		}
572 	}
573 	pf->hw_csum_rx_error = 0;
574 }
575 
576 /**
577  * i40e_compute_pci_to_hw_id - compute index form PCI function.
578  * @vsi: ptr to the VSI to read from.
579  * @hw: ptr to the hardware info.
580  **/
i40e_compute_pci_to_hw_id(struct i40e_vsi * vsi,struct i40e_hw * hw)581 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
582 {
583 	int pf_count = i40e_get_pf_count(hw);
584 
585 	if (vsi->type == I40E_VSI_SRIOV)
586 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
587 
588 	return hw->port + BIT(7);
589 }
590 
591 /**
592  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
593  * @hw: ptr to the hardware info.
594  * @hireg: the high 32 bit reg to read.
595  * @loreg: the low 32 bit reg to read.
596  * @offset_loaded: has the initial offset been loaded yet.
597  * @offset: ptr to current offset value.
598  * @stat: ptr to the stat.
599  *
600  * Since the device stats are not reset at PFReset, they will not
601  * be zeroed when the driver starts.  We'll save the first values read
602  * and use them as offsets to be subtracted from the raw values in order
603  * to report stats that count from zero.
604  **/
i40e_stat_update64(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)605 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
606 			       bool offset_loaded, u64 *offset, u64 *stat)
607 {
608 	u64 new_data;
609 
610 	new_data = rd64(hw, loreg);
611 
612 	if (!offset_loaded || new_data < *offset)
613 		*offset = new_data;
614 	*stat = new_data - *offset;
615 }
616 
617 /**
618  * i40e_stat_update48 - read and update a 48 bit stat from the chip
619  * @hw: ptr to the hardware info
620  * @hireg: the high 32 bit reg to read
621  * @loreg: the low 32 bit reg to read
622  * @offset_loaded: has the initial offset been loaded yet
623  * @offset: ptr to current offset value
624  * @stat: ptr to the stat
625  *
626  * Since the device stats are not reset at PFReset, they likely will not
627  * be zeroed when the driver starts.  We'll save the first values read
628  * and use them as offsets to be subtracted from the raw values in order
629  * to report stats that count from zero.  In the process, we also manage
630  * the potential roll-over.
631  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)632 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
633 			       bool offset_loaded, u64 *offset, u64 *stat)
634 {
635 	u64 new_data;
636 
637 	if (hw->device_id == I40E_DEV_ID_QEMU) {
638 		new_data = rd32(hw, loreg);
639 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
640 	} else {
641 		new_data = rd64(hw, loreg);
642 	}
643 	if (!offset_loaded)
644 		*offset = new_data;
645 	if (likely(new_data >= *offset))
646 		*stat = new_data - *offset;
647 	else
648 		*stat = (new_data + BIT_ULL(48)) - *offset;
649 	*stat &= 0xFFFFFFFFFFFFULL;
650 }
651 
652 /**
653  * i40e_stat_update32 - read and update a 32 bit stat from the chip
654  * @hw: ptr to the hardware info
655  * @reg: the hw reg to read
656  * @offset_loaded: has the initial offset been loaded yet
657  * @offset: ptr to current offset value
658  * @stat: ptr to the stat
659  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)660 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
661 			       bool offset_loaded, u64 *offset, u64 *stat)
662 {
663 	u32 new_data;
664 
665 	new_data = rd32(hw, reg);
666 	if (!offset_loaded)
667 		*offset = new_data;
668 	if (likely(new_data >= *offset))
669 		*stat = (u32)(new_data - *offset);
670 	else
671 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
672 }
673 
674 /**
675  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
676  * @hw: ptr to the hardware info
677  * @reg: the hw reg to read and clear
678  * @stat: ptr to the stat
679  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)680 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
681 {
682 	u32 new_data = rd32(hw, reg);
683 
684 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
685 	*stat += new_data;
686 }
687 
688 /**
689  * i40e_stats_update_rx_discards - update rx_discards.
690  * @vsi: ptr to the VSI to be updated.
691  * @hw: ptr to the hardware info.
692  * @stat_idx: VSI's stat_counter_idx.
693  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
694  * @stat_offset: ptr to stat_offset to store first read of specific register.
695  * @stat: ptr to VSI's stat to be updated.
696  **/
697 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)698 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
699 			      int stat_idx, bool offset_loaded,
700 			      struct i40e_eth_stats *stat_offset,
701 			      struct i40e_eth_stats *stat)
702 {
703 	u64 rx_rdpc, rx_rxerr;
704 
705 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
706 			   &stat_offset->rx_discards, &rx_rdpc);
707 	i40e_stat_update64(hw,
708 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
709 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
710 			   offset_loaded, &stat_offset->rx_discards_other,
711 			   &rx_rxerr);
712 
713 	stat->rx_discards = rx_rdpc + rx_rxerr;
714 }
715 
716 /**
717  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
718  * @vsi: the VSI to be updated
719  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)720 void i40e_update_eth_stats(struct i40e_vsi *vsi)
721 {
722 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
723 	struct i40e_pf *pf = vsi->back;
724 	struct i40e_hw *hw = &pf->hw;
725 	struct i40e_eth_stats *oes;
726 	struct i40e_eth_stats *es;     /* device's eth stats */
727 
728 	es = &vsi->eth_stats;
729 	oes = &vsi->eth_stats_offsets;
730 
731 	/* Gather up the stats that the hw collects */
732 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
733 			   vsi->stat_offsets_loaded,
734 			   &oes->tx_errors, &es->tx_errors);
735 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
736 			   vsi->stat_offsets_loaded,
737 			   &oes->rx_discards, &es->rx_discards);
738 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
739 			   vsi->stat_offsets_loaded,
740 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
741 
742 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
743 			   I40E_GLV_GORCL(stat_idx),
744 			   vsi->stat_offsets_loaded,
745 			   &oes->rx_bytes, &es->rx_bytes);
746 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
747 			   I40E_GLV_UPRCL(stat_idx),
748 			   vsi->stat_offsets_loaded,
749 			   &oes->rx_unicast, &es->rx_unicast);
750 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
751 			   I40E_GLV_MPRCL(stat_idx),
752 			   vsi->stat_offsets_loaded,
753 			   &oes->rx_multicast, &es->rx_multicast);
754 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
755 			   I40E_GLV_BPRCL(stat_idx),
756 			   vsi->stat_offsets_loaded,
757 			   &oes->rx_broadcast, &es->rx_broadcast);
758 
759 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
760 			   I40E_GLV_GOTCL(stat_idx),
761 			   vsi->stat_offsets_loaded,
762 			   &oes->tx_bytes, &es->tx_bytes);
763 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
764 			   I40E_GLV_UPTCL(stat_idx),
765 			   vsi->stat_offsets_loaded,
766 			   &oes->tx_unicast, &es->tx_unicast);
767 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
768 			   I40E_GLV_MPTCL(stat_idx),
769 			   vsi->stat_offsets_loaded,
770 			   &oes->tx_multicast, &es->tx_multicast);
771 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
772 			   I40E_GLV_BPTCL(stat_idx),
773 			   vsi->stat_offsets_loaded,
774 			   &oes->tx_broadcast, &es->tx_broadcast);
775 
776 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
777 				      vsi->stat_offsets_loaded, oes, es);
778 
779 	vsi->stat_offsets_loaded = true;
780 }
781 
782 /**
783  * i40e_update_veb_stats - Update Switch component statistics
784  * @veb: the VEB being updated
785  **/
i40e_update_veb_stats(struct i40e_veb * veb)786 void i40e_update_veb_stats(struct i40e_veb *veb)
787 {
788 	struct i40e_pf *pf = veb->pf;
789 	struct i40e_hw *hw = &pf->hw;
790 	struct i40e_eth_stats *oes;
791 	struct i40e_eth_stats *es;     /* device's eth stats */
792 	struct i40e_veb_tc_stats *veb_oes;
793 	struct i40e_veb_tc_stats *veb_es;
794 	int i, idx = 0;
795 
796 	idx = veb->stats_idx;
797 	es = &veb->stats;
798 	oes = &veb->stats_offsets;
799 	veb_es = &veb->tc_stats;
800 	veb_oes = &veb->tc_stats_offsets;
801 
802 	/* Gather up the stats that the hw collects */
803 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
804 			   veb->stat_offsets_loaded,
805 			   &oes->tx_discards, &es->tx_discards);
806 	if (hw->revision_id > 0)
807 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
808 				   veb->stat_offsets_loaded,
809 				   &oes->rx_unknown_protocol,
810 				   &es->rx_unknown_protocol);
811 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
812 			   veb->stat_offsets_loaded,
813 			   &oes->rx_bytes, &es->rx_bytes);
814 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
815 			   veb->stat_offsets_loaded,
816 			   &oes->rx_unicast, &es->rx_unicast);
817 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
818 			   veb->stat_offsets_loaded,
819 			   &oes->rx_multicast, &es->rx_multicast);
820 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
821 			   veb->stat_offsets_loaded,
822 			   &oes->rx_broadcast, &es->rx_broadcast);
823 
824 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
825 			   veb->stat_offsets_loaded,
826 			   &oes->tx_bytes, &es->tx_bytes);
827 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
828 			   veb->stat_offsets_loaded,
829 			   &oes->tx_unicast, &es->tx_unicast);
830 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
831 			   veb->stat_offsets_loaded,
832 			   &oes->tx_multicast, &es->tx_multicast);
833 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
834 			   veb->stat_offsets_loaded,
835 			   &oes->tx_broadcast, &es->tx_broadcast);
836 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
837 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
838 				   I40E_GLVEBTC_RPCL(i, idx),
839 				   veb->stat_offsets_loaded,
840 				   &veb_oes->tc_rx_packets[i],
841 				   &veb_es->tc_rx_packets[i]);
842 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
843 				   I40E_GLVEBTC_RBCL(i, idx),
844 				   veb->stat_offsets_loaded,
845 				   &veb_oes->tc_rx_bytes[i],
846 				   &veb_es->tc_rx_bytes[i]);
847 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
848 				   I40E_GLVEBTC_TPCL(i, idx),
849 				   veb->stat_offsets_loaded,
850 				   &veb_oes->tc_tx_packets[i],
851 				   &veb_es->tc_tx_packets[i]);
852 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
853 				   I40E_GLVEBTC_TBCL(i, idx),
854 				   veb->stat_offsets_loaded,
855 				   &veb_oes->tc_tx_bytes[i],
856 				   &veb_es->tc_tx_bytes[i]);
857 	}
858 	veb->stat_offsets_loaded = true;
859 }
860 
861 /**
862  * i40e_update_vsi_stats - Update the vsi statistics counters.
863  * @vsi: the VSI to be updated
864  *
865  * There are a few instances where we store the same stat in a
866  * couple of different structs.  This is partly because we have
867  * the netdev stats that need to be filled out, which is slightly
868  * different from the "eth_stats" defined by the chip and used in
869  * VF communications.  We sort it out here.
870  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)871 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
872 {
873 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
874 	struct i40e_pf *pf = vsi->back;
875 	struct rtnl_link_stats64 *ons;
876 	struct rtnl_link_stats64 *ns;   /* netdev stats */
877 	struct i40e_eth_stats *oes;
878 	struct i40e_eth_stats *es;     /* device's eth stats */
879 	u64 tx_restart, tx_busy;
880 	struct i40e_ring *p;
881 	u64 bytes, packets;
882 	unsigned int start;
883 	u64 tx_linearize;
884 	u64 tx_force_wb;
885 	u64 tx_stopped;
886 	u64 rx_p, rx_b;
887 	u64 tx_p, tx_b;
888 	u16 q;
889 
890 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
891 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
892 		return;
893 
894 	ns = i40e_get_vsi_stats_struct(vsi);
895 	ons = &vsi->net_stats_offsets;
896 	es = &vsi->eth_stats;
897 	oes = &vsi->eth_stats_offsets;
898 
899 	/* Gather up the netdev and vsi stats that the driver collects
900 	 * on the fly during packet processing
901 	 */
902 	rx_b = rx_p = 0;
903 	tx_b = tx_p = 0;
904 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
905 	tx_stopped = 0;
906 	rx_page = 0;
907 	rx_buf = 0;
908 	rx_reuse = 0;
909 	rx_alloc = 0;
910 	rx_waive = 0;
911 	rx_busy = 0;
912 	rcu_read_lock();
913 	for (q = 0; q < vsi->num_queue_pairs; q++) {
914 		/* locate Tx ring */
915 		p = READ_ONCE(vsi->tx_rings[q]);
916 		if (!p)
917 			continue;
918 
919 		do {
920 			start = u64_stats_fetch_begin(&p->syncp);
921 			packets = p->stats.packets;
922 			bytes = p->stats.bytes;
923 		} while (u64_stats_fetch_retry(&p->syncp, start));
924 		tx_b += bytes;
925 		tx_p += packets;
926 		tx_restart += p->tx_stats.restart_queue;
927 		tx_busy += p->tx_stats.tx_busy;
928 		tx_linearize += p->tx_stats.tx_linearize;
929 		tx_force_wb += p->tx_stats.tx_force_wb;
930 		tx_stopped += p->tx_stats.tx_stopped;
931 
932 		/* locate Rx ring */
933 		p = READ_ONCE(vsi->rx_rings[q]);
934 		if (!p)
935 			continue;
936 
937 		do {
938 			start = u64_stats_fetch_begin(&p->syncp);
939 			packets = p->stats.packets;
940 			bytes = p->stats.bytes;
941 		} while (u64_stats_fetch_retry(&p->syncp, start));
942 		rx_b += bytes;
943 		rx_p += packets;
944 		rx_buf += p->rx_stats.alloc_buff_failed;
945 		rx_page += p->rx_stats.alloc_page_failed;
946 		rx_reuse += p->rx_stats.page_reuse_count;
947 		rx_alloc += p->rx_stats.page_alloc_count;
948 		rx_waive += p->rx_stats.page_waive_count;
949 		rx_busy += p->rx_stats.page_busy_count;
950 
951 		if (i40e_enabled_xdp_vsi(vsi)) {
952 			/* locate XDP ring */
953 			p = READ_ONCE(vsi->xdp_rings[q]);
954 			if (!p)
955 				continue;
956 
957 			do {
958 				start = u64_stats_fetch_begin(&p->syncp);
959 				packets = p->stats.packets;
960 				bytes = p->stats.bytes;
961 			} while (u64_stats_fetch_retry(&p->syncp, start));
962 			tx_b += bytes;
963 			tx_p += packets;
964 			tx_restart += p->tx_stats.restart_queue;
965 			tx_busy += p->tx_stats.tx_busy;
966 			tx_linearize += p->tx_stats.tx_linearize;
967 			tx_force_wb += p->tx_stats.tx_force_wb;
968 		}
969 	}
970 	rcu_read_unlock();
971 	vsi->tx_restart = tx_restart;
972 	vsi->tx_busy = tx_busy;
973 	vsi->tx_linearize = tx_linearize;
974 	vsi->tx_force_wb = tx_force_wb;
975 	vsi->tx_stopped = tx_stopped;
976 	vsi->rx_page_failed = rx_page;
977 	vsi->rx_buf_failed = rx_buf;
978 	vsi->rx_page_reuse = rx_reuse;
979 	vsi->rx_page_alloc = rx_alloc;
980 	vsi->rx_page_waive = rx_waive;
981 	vsi->rx_page_busy = rx_busy;
982 
983 	ns->rx_packets = rx_p;
984 	ns->rx_bytes = rx_b;
985 	ns->tx_packets = tx_p;
986 	ns->tx_bytes = tx_b;
987 
988 	/* update netdev stats from eth stats */
989 	i40e_update_eth_stats(vsi);
990 	ons->tx_errors = oes->tx_errors;
991 	ns->tx_errors = es->tx_errors;
992 	ons->multicast = oes->rx_multicast;
993 	ns->multicast = es->rx_multicast;
994 	ons->rx_dropped = oes->rx_discards;
995 	ns->rx_dropped = es->rx_discards;
996 	ons->tx_dropped = oes->tx_discards;
997 	ns->tx_dropped = es->tx_discards;
998 
999 	/* pull in a couple PF stats if this is the main vsi */
1000 	if (vsi == pf->vsi[pf->lan_vsi]) {
1001 		ns->rx_crc_errors = pf->stats.crc_errors;
1002 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
1003 		ns->rx_length_errors = pf->stats.rx_length_errors;
1004 	}
1005 }
1006 
1007 /**
1008  * i40e_update_pf_stats - Update the PF statistics counters.
1009  * @pf: the PF to be updated
1010  **/
i40e_update_pf_stats(struct i40e_pf * pf)1011 static void i40e_update_pf_stats(struct i40e_pf *pf)
1012 {
1013 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1014 	struct i40e_hw_port_stats *nsd = &pf->stats;
1015 	struct i40e_hw *hw = &pf->hw;
1016 	u32 val;
1017 	int i;
1018 
1019 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1020 			   I40E_GLPRT_GORCL(hw->port),
1021 			   pf->stat_offsets_loaded,
1022 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1023 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1024 			   I40E_GLPRT_GOTCL(hw->port),
1025 			   pf->stat_offsets_loaded,
1026 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1027 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1028 			   pf->stat_offsets_loaded,
1029 			   &osd->eth.rx_discards,
1030 			   &nsd->eth.rx_discards);
1031 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1032 			   I40E_GLPRT_UPRCL(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->eth.rx_unicast,
1035 			   &nsd->eth.rx_unicast);
1036 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1037 			   I40E_GLPRT_MPRCL(hw->port),
1038 			   pf->stat_offsets_loaded,
1039 			   &osd->eth.rx_multicast,
1040 			   &nsd->eth.rx_multicast);
1041 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1042 			   I40E_GLPRT_BPRCL(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->eth.rx_broadcast,
1045 			   &nsd->eth.rx_broadcast);
1046 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1047 			   I40E_GLPRT_UPTCL(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->eth.tx_unicast,
1050 			   &nsd->eth.tx_unicast);
1051 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1052 			   I40E_GLPRT_MPTCL(hw->port),
1053 			   pf->stat_offsets_loaded,
1054 			   &osd->eth.tx_multicast,
1055 			   &nsd->eth.tx_multicast);
1056 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1057 			   I40E_GLPRT_BPTCL(hw->port),
1058 			   pf->stat_offsets_loaded,
1059 			   &osd->eth.tx_broadcast,
1060 			   &nsd->eth.tx_broadcast);
1061 
1062 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1063 			   pf->stat_offsets_loaded,
1064 			   &osd->tx_dropped_link_down,
1065 			   &nsd->tx_dropped_link_down);
1066 
1067 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->crc_errors, &nsd->crc_errors);
1070 
1071 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1074 
1075 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->mac_local_faults,
1078 			   &nsd->mac_local_faults);
1079 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1080 			   pf->stat_offsets_loaded,
1081 			   &osd->mac_remote_faults,
1082 			   &nsd->mac_remote_faults);
1083 
1084 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1085 			   pf->stat_offsets_loaded,
1086 			   &osd->rx_length_errors,
1087 			   &nsd->rx_length_errors);
1088 
1089 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1090 			   pf->stat_offsets_loaded,
1091 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1092 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1093 			   pf->stat_offsets_loaded,
1094 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1095 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1096 			   pf->stat_offsets_loaded,
1097 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1098 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1099 			   pf->stat_offsets_loaded,
1100 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1101 
1102 	for (i = 0; i < 8; i++) {
1103 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1104 				   pf->stat_offsets_loaded,
1105 				   &osd->priority_xoff_rx[i],
1106 				   &nsd->priority_xoff_rx[i]);
1107 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1108 				   pf->stat_offsets_loaded,
1109 				   &osd->priority_xon_rx[i],
1110 				   &nsd->priority_xon_rx[i]);
1111 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1112 				   pf->stat_offsets_loaded,
1113 				   &osd->priority_xon_tx[i],
1114 				   &nsd->priority_xon_tx[i]);
1115 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1116 				   pf->stat_offsets_loaded,
1117 				   &osd->priority_xoff_tx[i],
1118 				   &nsd->priority_xoff_tx[i]);
1119 		i40e_stat_update32(hw,
1120 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1121 				   pf->stat_offsets_loaded,
1122 				   &osd->priority_xon_2_xoff[i],
1123 				   &nsd->priority_xon_2_xoff[i]);
1124 	}
1125 
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1127 			   I40E_GLPRT_PRC64L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_64, &nsd->rx_size_64);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1131 			   I40E_GLPRT_PRC127L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_127, &nsd->rx_size_127);
1134 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1135 			   I40E_GLPRT_PRC255L(hw->port),
1136 			   pf->stat_offsets_loaded,
1137 			   &osd->rx_size_255, &nsd->rx_size_255);
1138 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1139 			   I40E_GLPRT_PRC511L(hw->port),
1140 			   pf->stat_offsets_loaded,
1141 			   &osd->rx_size_511, &nsd->rx_size_511);
1142 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1143 			   I40E_GLPRT_PRC1023L(hw->port),
1144 			   pf->stat_offsets_loaded,
1145 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1146 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1147 			   I40E_GLPRT_PRC1522L(hw->port),
1148 			   pf->stat_offsets_loaded,
1149 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1150 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1151 			   I40E_GLPRT_PRC9522L(hw->port),
1152 			   pf->stat_offsets_loaded,
1153 			   &osd->rx_size_big, &nsd->rx_size_big);
1154 
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1156 			   I40E_GLPRT_PTC64L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_64, &nsd->tx_size_64);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1160 			   I40E_GLPRT_PTC127L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_127, &nsd->tx_size_127);
1163 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1164 			   I40E_GLPRT_PTC255L(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->tx_size_255, &nsd->tx_size_255);
1167 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1168 			   I40E_GLPRT_PTC511L(hw->port),
1169 			   pf->stat_offsets_loaded,
1170 			   &osd->tx_size_511, &nsd->tx_size_511);
1171 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1172 			   I40E_GLPRT_PTC1023L(hw->port),
1173 			   pf->stat_offsets_loaded,
1174 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1175 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1176 			   I40E_GLPRT_PTC1522L(hw->port),
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1179 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1180 			   I40E_GLPRT_PTC9522L(hw->port),
1181 			   pf->stat_offsets_loaded,
1182 			   &osd->tx_size_big, &nsd->tx_size_big);
1183 
1184 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1185 			   pf->stat_offsets_loaded,
1186 			   &osd->rx_undersize, &nsd->rx_undersize);
1187 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1188 			   pf->stat_offsets_loaded,
1189 			   &osd->rx_fragments, &nsd->rx_fragments);
1190 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1191 			   pf->stat_offsets_loaded,
1192 			   &osd->rx_oversize, &nsd->rx_oversize);
1193 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1194 			   pf->stat_offsets_loaded,
1195 			   &osd->rx_jabber, &nsd->rx_jabber);
1196 
1197 	/* FDIR stats */
1198 	i40e_stat_update_and_clear32(hw,
1199 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1200 			&nsd->fd_atr_match);
1201 	i40e_stat_update_and_clear32(hw,
1202 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1203 			&nsd->fd_sb_match);
1204 	i40e_stat_update_and_clear32(hw,
1205 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1206 			&nsd->fd_atr_tunnel_match);
1207 
1208 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1209 	nsd->tx_lpi_status =
1210 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1211 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1212 	nsd->rx_lpi_status =
1213 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1214 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1215 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1216 			   pf->stat_offsets_loaded,
1217 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1218 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1219 			   pf->stat_offsets_loaded,
1220 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1221 
1222 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1223 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1224 		nsd->fd_sb_status = true;
1225 	else
1226 		nsd->fd_sb_status = false;
1227 
1228 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1229 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1230 		nsd->fd_atr_status = true;
1231 	else
1232 		nsd->fd_atr_status = false;
1233 
1234 	pf->stat_offsets_loaded = true;
1235 }
1236 
1237 /**
1238  * i40e_update_stats - Update the various statistics counters.
1239  * @vsi: the VSI to be updated
1240  *
1241  * Update the various stats for this VSI and its related entities.
1242  **/
i40e_update_stats(struct i40e_vsi * vsi)1243 void i40e_update_stats(struct i40e_vsi *vsi)
1244 {
1245 	struct i40e_pf *pf = vsi->back;
1246 
1247 	if (vsi == pf->vsi[pf->lan_vsi])
1248 		i40e_update_pf_stats(pf);
1249 
1250 	i40e_update_vsi_stats(vsi);
1251 }
1252 
1253 /**
1254  * i40e_count_filters - counts VSI mac filters
1255  * @vsi: the VSI to be searched
1256  *
1257  * Returns count of mac filters
1258  **/
i40e_count_filters(struct i40e_vsi * vsi)1259 int i40e_count_filters(struct i40e_vsi *vsi)
1260 {
1261 	struct i40e_mac_filter *f;
1262 	struct hlist_node *h;
1263 	int bkt;
1264 	int cnt = 0;
1265 
1266 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1267 		if (f->state == I40E_FILTER_NEW ||
1268 		    f->state == I40E_FILTER_NEW_SYNC ||
1269 		    f->state == I40E_FILTER_ACTIVE)
1270 			++cnt;
1271 	}
1272 
1273 	return cnt;
1274 }
1275 
1276 /**
1277  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1278  * @vsi: the VSI to be searched
1279  * @macaddr: the MAC address
1280  * @vlan: the vlan
1281  *
1282  * Returns ptr to the filter object or NULL
1283  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1284 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1285 						const u8 *macaddr, s16 vlan)
1286 {
1287 	struct i40e_mac_filter *f;
1288 	u64 key;
1289 
1290 	if (!vsi || !macaddr)
1291 		return NULL;
1292 
1293 	key = i40e_addr_to_hkey(macaddr);
1294 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1295 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1296 		    (vlan == f->vlan))
1297 			return f;
1298 	}
1299 	return NULL;
1300 }
1301 
1302 /**
1303  * i40e_find_mac - Find a mac addr in the macvlan filters list
1304  * @vsi: the VSI to be searched
1305  * @macaddr: the MAC address we are searching for
1306  *
1307  * Returns the first filter with the provided MAC address or NULL if
1308  * MAC address was not found
1309  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1310 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1311 {
1312 	struct i40e_mac_filter *f;
1313 	u64 key;
1314 
1315 	if (!vsi || !macaddr)
1316 		return NULL;
1317 
1318 	key = i40e_addr_to_hkey(macaddr);
1319 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1320 		if ((ether_addr_equal(macaddr, f->macaddr)))
1321 			return f;
1322 	}
1323 	return NULL;
1324 }
1325 
1326 /**
1327  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1328  * @vsi: the VSI to be searched
1329  *
1330  * Returns true if VSI is in vlan mode or false otherwise
1331  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1332 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1333 {
1334 	/* If we have a PVID, always operate in VLAN mode */
1335 	if (vsi->info.pvid)
1336 		return true;
1337 
1338 	/* We need to operate in VLAN mode whenever we have any filters with
1339 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1340 	 * time, incurring search cost repeatedly. However, we can notice two
1341 	 * things:
1342 	 *
1343 	 * 1) the only place where we can gain a VLAN filter is in
1344 	 *    i40e_add_filter.
1345 	 *
1346 	 * 2) the only place where filters are actually removed is in
1347 	 *    i40e_sync_filters_subtask.
1348 	 *
1349 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1350 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1351 	 * we have to perform the full search after deleting filters in
1352 	 * i40e_sync_filters_subtask, but we already have to search
1353 	 * filters here and can perform the check at the same time. This
1354 	 * results in avoiding embedding a loop for VLAN mode inside another
1355 	 * loop over all the filters, and should maintain correctness as noted
1356 	 * above.
1357 	 */
1358 	return vsi->has_vlan_filter;
1359 }
1360 
1361 /**
1362  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1363  * @vsi: the VSI to configure
1364  * @tmp_add_list: list of filters ready to be added
1365  * @tmp_del_list: list of filters ready to be deleted
1366  * @vlan_filters: the number of active VLAN filters
1367  *
1368  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1369  * behave as expected. If we have any active VLAN filters remaining or about
1370  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1371  * so that they only match against untagged traffic. If we no longer have any
1372  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1373  * so that they match against both tagged and untagged traffic. In this way,
1374  * we ensure that we correctly receive the desired traffic. This ensures that
1375  * when we have an active VLAN we will receive only untagged traffic and
1376  * traffic matching active VLANs. If we have no active VLANs then we will
1377  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1378  *
1379  * Finally, in a similar fashion, this function also corrects filters when
1380  * there is an active PVID assigned to this VSI.
1381  *
1382  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1383  *
1384  * This function is only expected to be called from within
1385  * i40e_sync_vsi_filters.
1386  *
1387  * NOTE: This function expects to be called while under the
1388  * mac_filter_hash_lock
1389  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1390 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1391 					 struct hlist_head *tmp_add_list,
1392 					 struct hlist_head *tmp_del_list,
1393 					 int vlan_filters)
1394 {
1395 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1396 	struct i40e_mac_filter *f, *add_head;
1397 	struct i40e_new_mac_filter *new;
1398 	struct hlist_node *h;
1399 	int bkt, new_vlan;
1400 
1401 	/* To determine if a particular filter needs to be replaced we
1402 	 * have the three following conditions:
1403 	 *
1404 	 * a) if we have a PVID assigned, then all filters which are
1405 	 *    not marked as VLAN=PVID must be replaced with filters that
1406 	 *    are.
1407 	 * b) otherwise, if we have any active VLANS, all filters
1408 	 *    which are marked as VLAN=-1 must be replaced with
1409 	 *    filters marked as VLAN=0
1410 	 * c) finally, if we do not have any active VLANS, all filters
1411 	 *    which are marked as VLAN=0 must be replaced with filters
1412 	 *    marked as VLAN=-1
1413 	 */
1414 
1415 	/* Update the filters about to be added in place */
1416 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1417 		if (pvid && new->f->vlan != pvid)
1418 			new->f->vlan = pvid;
1419 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1420 			new->f->vlan = 0;
1421 		else if (!vlan_filters && new->f->vlan == 0)
1422 			new->f->vlan = I40E_VLAN_ANY;
1423 	}
1424 
1425 	/* Update the remaining active filters */
1426 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1427 		/* Combine the checks for whether a filter needs to be changed
1428 		 * and then determine the new VLAN inside the if block, in
1429 		 * order to avoid duplicating code for adding the new filter
1430 		 * then deleting the old filter.
1431 		 */
1432 		if ((pvid && f->vlan != pvid) ||
1433 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1434 		    (!vlan_filters && f->vlan == 0)) {
1435 			/* Determine the new vlan we will be adding */
1436 			if (pvid)
1437 				new_vlan = pvid;
1438 			else if (vlan_filters)
1439 				new_vlan = 0;
1440 			else
1441 				new_vlan = I40E_VLAN_ANY;
1442 
1443 			/* Create the new filter */
1444 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1445 			if (!add_head)
1446 				return -ENOMEM;
1447 
1448 			/* Create a temporary i40e_new_mac_filter */
1449 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1450 			if (!new)
1451 				return -ENOMEM;
1452 
1453 			new->f = add_head;
1454 			new->state = add_head->state;
1455 			if (add_head->state == I40E_FILTER_NEW)
1456 				add_head->state = I40E_FILTER_NEW_SYNC;
1457 
1458 			/* Add the new filter to the tmp list */
1459 			hlist_add_head(&new->hlist, tmp_add_list);
1460 
1461 			/* Put the original filter into the delete list */
1462 			f->state = I40E_FILTER_REMOVE;
1463 			hash_del(&f->hlist);
1464 			hlist_add_head(&f->hlist, tmp_del_list);
1465 		}
1466 	}
1467 
1468 	vsi->has_vlan_filter = !!vlan_filters;
1469 
1470 	return 0;
1471 }
1472 
1473 /**
1474  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1475  * @vsi: the vsi to configure
1476  * @new_mac: new mac filter to be added
1477  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1478  * @vlan_filters: the number of active VLAN filters
1479  * @trusted: flag if the VF is trusted
1480  *
1481  * Get new VLAN id based on current VLAN filters, trust, PVID
1482  * and vf-vlan-prune-disable flag.
1483  *
1484  * Returns the value of the new vlan filter or
1485  * the old value if no new filter is needed.
1486  */
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)1487 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1488 				struct i40e_new_mac_filter *new_mac,
1489 				struct i40e_mac_filter *f,
1490 				int vlan_filters,
1491 				bool trusted)
1492 {
1493 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1494 	struct i40e_pf *pf = vsi->back;
1495 	bool is_any;
1496 
1497 	if (new_mac)
1498 		f = new_mac->f;
1499 
1500 	if (pvid && f->vlan != pvid)
1501 		return pvid;
1502 
1503 	is_any = (trusted ||
1504 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1505 
1506 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1507 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1508 	    (is_any && !vlan_filters && f->vlan == 0)) {
1509 		if (is_any)
1510 			return I40E_VLAN_ANY;
1511 		else
1512 			return 0;
1513 	}
1514 
1515 	return f->vlan;
1516 }
1517 
1518 /**
1519  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1520  * @vsi: the vsi to configure
1521  * @tmp_add_list: list of filters ready to be added
1522  * @tmp_del_list: list of filters ready to be deleted
1523  * @vlan_filters: the number of active VLAN filters
1524  * @trusted: flag if the VF is trusted
1525  *
1526  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1527  * and vf-vlan-prune-disable flag.
1528  *
1529  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1530  *
1531  * This function is only expected to be called from within
1532  * i40e_sync_vsi_filters.
1533  *
1534  * NOTE: This function expects to be called while under the
1535  * mac_filter_hash_lock
1536  */
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)1537 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1538 					    struct hlist_head *tmp_add_list,
1539 					    struct hlist_head *tmp_del_list,
1540 					    int vlan_filters,
1541 					    bool trusted)
1542 {
1543 	struct i40e_mac_filter *f, *add_head;
1544 	struct i40e_new_mac_filter *new_mac;
1545 	struct hlist_node *h;
1546 	int bkt, new_vlan;
1547 
1548 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1549 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1550 							vlan_filters, trusted);
1551 	}
1552 
1553 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1554 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1555 						trusted);
1556 		if (new_vlan != f->vlan) {
1557 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1558 			if (!add_head)
1559 				return -ENOMEM;
1560 			/* Create a temporary i40e_new_mac_filter */
1561 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1562 			if (!new_mac)
1563 				return -ENOMEM;
1564 			new_mac->f = add_head;
1565 			new_mac->state = add_head->state;
1566 			if (add_head->state == I40E_FILTER_NEW)
1567 				add_head->state = I40E_FILTER_NEW_SYNC;
1568 
1569 			/* Add the new filter to the tmp list */
1570 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1571 
1572 			/* Put the original filter into the delete list */
1573 			f->state = I40E_FILTER_REMOVE;
1574 			hash_del(&f->hlist);
1575 			hlist_add_head(&f->hlist, tmp_del_list);
1576 		}
1577 	}
1578 
1579 	vsi->has_vlan_filter = !!vlan_filters;
1580 	return 0;
1581 }
1582 
1583 /**
1584  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1585  * @vsi: the PF Main VSI - inappropriate for any other VSI
1586  * @macaddr: the MAC address
1587  *
1588  * Remove whatever filter the firmware set up so the driver can manage
1589  * its own filtering intelligently.
1590  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1591 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1592 {
1593 	struct i40e_aqc_remove_macvlan_element_data element;
1594 	struct i40e_pf *pf = vsi->back;
1595 
1596 	/* Only appropriate for the PF main VSI */
1597 	if (vsi->type != I40E_VSI_MAIN)
1598 		return;
1599 
1600 	memset(&element, 0, sizeof(element));
1601 	ether_addr_copy(element.mac_addr, macaddr);
1602 	element.vlan_tag = 0;
1603 	/* Ignore error returns, some firmware does it this way... */
1604 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1605 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1606 
1607 	memset(&element, 0, sizeof(element));
1608 	ether_addr_copy(element.mac_addr, macaddr);
1609 	element.vlan_tag = 0;
1610 	/* ...and some firmware does it this way. */
1611 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1612 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1613 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1614 }
1615 
1616 /**
1617  * i40e_add_filter - Add a mac/vlan filter to the VSI
1618  * @vsi: the VSI to be searched
1619  * @macaddr: the MAC address
1620  * @vlan: the vlan
1621  *
1622  * Returns ptr to the filter object or NULL when no memory available.
1623  *
1624  * NOTE: This function is expected to be called with mac_filter_hash_lock
1625  * being held.
1626  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1627 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1628 					const u8 *macaddr, s16 vlan)
1629 {
1630 	struct i40e_mac_filter *f;
1631 	u64 key;
1632 
1633 	if (!vsi || !macaddr)
1634 		return NULL;
1635 
1636 	f = i40e_find_filter(vsi, macaddr, vlan);
1637 	if (!f) {
1638 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1639 		if (!f)
1640 			return NULL;
1641 
1642 		/* Update the boolean indicating if we need to function in
1643 		 * VLAN mode.
1644 		 */
1645 		if (vlan >= 0)
1646 			vsi->has_vlan_filter = true;
1647 
1648 		ether_addr_copy(f->macaddr, macaddr);
1649 		f->vlan = vlan;
1650 		f->state = I40E_FILTER_NEW;
1651 		INIT_HLIST_NODE(&f->hlist);
1652 
1653 		key = i40e_addr_to_hkey(macaddr);
1654 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1655 
1656 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1657 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1658 	}
1659 
1660 	/* If we're asked to add a filter that has been marked for removal, it
1661 	 * is safe to simply restore it to active state. __i40e_del_filter
1662 	 * will have simply deleted any filters which were previously marked
1663 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1664 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1665 	 * task, just restore this filter to the ACTIVE state so that the
1666 	 * sync task leaves it in place
1667 	 */
1668 	if (f->state == I40E_FILTER_REMOVE)
1669 		f->state = I40E_FILTER_ACTIVE;
1670 
1671 	return f;
1672 }
1673 
1674 /**
1675  * __i40e_del_filter - Remove a specific filter from the VSI
1676  * @vsi: VSI to remove from
1677  * @f: the filter to remove from the list
1678  *
1679  * This function should be called instead of i40e_del_filter only if you know
1680  * the exact filter you will remove already, such as via i40e_find_filter or
1681  * i40e_find_mac.
1682  *
1683  * NOTE: This function is expected to be called with mac_filter_hash_lock
1684  * being held.
1685  * ANOTHER NOTE: This function MUST be called from within the context of
1686  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1687  * instead of list_for_each_entry().
1688  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1689 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1690 {
1691 	if (!f)
1692 		return;
1693 
1694 	/* If the filter was never added to firmware then we can just delete it
1695 	 * directly and we don't want to set the status to remove or else an
1696 	 * admin queue command will unnecessarily fire.
1697 	 */
1698 	if ((f->state == I40E_FILTER_FAILED) ||
1699 	    (f->state == I40E_FILTER_NEW)) {
1700 		hash_del(&f->hlist);
1701 		kfree(f);
1702 	} else {
1703 		f->state = I40E_FILTER_REMOVE;
1704 	}
1705 
1706 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1707 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1708 }
1709 
1710 /**
1711  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1712  * @vsi: the VSI to be searched
1713  * @macaddr: the MAC address
1714  * @vlan: the VLAN
1715  *
1716  * NOTE: This function is expected to be called with mac_filter_hash_lock
1717  * being held.
1718  * ANOTHER NOTE: This function MUST be called from within the context of
1719  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1720  * instead of list_for_each_entry().
1721  **/
i40e_del_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1722 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1723 {
1724 	struct i40e_mac_filter *f;
1725 
1726 	if (!vsi || !macaddr)
1727 		return;
1728 
1729 	f = i40e_find_filter(vsi, macaddr, vlan);
1730 	__i40e_del_filter(vsi, f);
1731 }
1732 
1733 /**
1734  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1735  * @vsi: the VSI to be searched
1736  * @macaddr: the mac address to be filtered
1737  *
1738  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1739  * go through all the macvlan filters and add a macvlan filter for each
1740  * unique vlan that already exists. If a PVID has been assigned, instead only
1741  * add the macaddr to that VLAN.
1742  *
1743  * Returns last filter added on success, else NULL
1744  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1745 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1746 					    const u8 *macaddr)
1747 {
1748 	struct i40e_mac_filter *f, *add = NULL;
1749 	struct hlist_node *h;
1750 	int bkt;
1751 
1752 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1753 	if (vsi->info.pvid)
1754 		return i40e_add_filter(vsi, macaddr,
1755 				       le16_to_cpu(vsi->info.pvid));
1756 
1757 	if (!i40e_is_vsi_in_vlan(vsi))
1758 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1759 
1760 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1761 		if (f->state == I40E_FILTER_REMOVE)
1762 			continue;
1763 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1764 		if (!add)
1765 			return NULL;
1766 	}
1767 
1768 	return add;
1769 }
1770 
1771 /**
1772  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1773  * @vsi: the VSI to be searched
1774  * @macaddr: the mac address to be removed
1775  *
1776  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1777  * associated with.
1778  *
1779  * Returns 0 for success, or error
1780  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1781 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1782 {
1783 	struct i40e_mac_filter *f;
1784 	struct hlist_node *h;
1785 	bool found = false;
1786 	int bkt;
1787 
1788 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1789 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1790 		if (ether_addr_equal(macaddr, f->macaddr)) {
1791 			__i40e_del_filter(vsi, f);
1792 			found = true;
1793 		}
1794 	}
1795 
1796 	if (found)
1797 		return 0;
1798 	else
1799 		return -ENOENT;
1800 }
1801 
1802 /**
1803  * i40e_set_mac - NDO callback to set mac address
1804  * @netdev: network interface device structure
1805  * @p: pointer to an address structure
1806  *
1807  * Returns 0 on success, negative on failure
1808  **/
i40e_set_mac(struct net_device * netdev,void * p)1809 static int i40e_set_mac(struct net_device *netdev, void *p)
1810 {
1811 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1812 	struct i40e_vsi *vsi = np->vsi;
1813 	struct i40e_pf *pf = vsi->back;
1814 	struct i40e_hw *hw = &pf->hw;
1815 	struct sockaddr *addr = p;
1816 
1817 	if (!is_valid_ether_addr(addr->sa_data))
1818 		return -EADDRNOTAVAIL;
1819 
1820 	if (test_bit(__I40E_DOWN, pf->state) ||
1821 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1822 		return -EADDRNOTAVAIL;
1823 
1824 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1825 		netdev_info(netdev, "returning to hw mac address %pM\n",
1826 			    hw->mac.addr);
1827 	else
1828 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1829 
1830 	/* Copy the address first, so that we avoid a possible race with
1831 	 * .set_rx_mode().
1832 	 * - Remove old address from MAC filter
1833 	 * - Copy new address
1834 	 * - Add new address to MAC filter
1835 	 */
1836 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1837 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1838 	eth_hw_addr_set(netdev, addr->sa_data);
1839 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1840 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1841 
1842 	if (vsi->type == I40E_VSI_MAIN) {
1843 		int ret;
1844 
1845 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1846 						addr->sa_data, NULL);
1847 		if (ret)
1848 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1849 				    ERR_PTR(ret),
1850 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1851 	}
1852 
1853 	/* schedule our worker thread which will take care of
1854 	 * applying the new filter changes
1855 	 */
1856 	i40e_service_event_schedule(pf);
1857 	return 0;
1858 }
1859 
1860 /**
1861  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1862  * @vsi: vsi structure
1863  * @seed: RSS hash seed
1864  * @lut: pointer to lookup table of lut_size
1865  * @lut_size: size of the lookup table
1866  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1867 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1868 			      u8 *lut, u16 lut_size)
1869 {
1870 	struct i40e_pf *pf = vsi->back;
1871 	struct i40e_hw *hw = &pf->hw;
1872 	int ret = 0;
1873 
1874 	if (seed) {
1875 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1876 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1877 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1878 		if (ret) {
1879 			dev_info(&pf->pdev->dev,
1880 				 "Cannot set RSS key, err %pe aq_err %s\n",
1881 				 ERR_PTR(ret),
1882 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1883 			return ret;
1884 		}
1885 	}
1886 	if (lut) {
1887 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1888 
1889 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1890 		if (ret) {
1891 			dev_info(&pf->pdev->dev,
1892 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1893 				 ERR_PTR(ret),
1894 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1895 			return ret;
1896 		}
1897 	}
1898 	return ret;
1899 }
1900 
1901 /**
1902  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1903  * @vsi: VSI structure
1904  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1905 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1906 {
1907 	struct i40e_pf *pf = vsi->back;
1908 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1909 	u8 *lut;
1910 	int ret;
1911 
1912 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1913 		return 0;
1914 	if (!vsi->rss_size)
1915 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1916 				      vsi->num_queue_pairs);
1917 	if (!vsi->rss_size)
1918 		return -EINVAL;
1919 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1920 	if (!lut)
1921 		return -ENOMEM;
1922 
1923 	/* Use the user configured hash keys and lookup table if there is one,
1924 	 * otherwise use default
1925 	 */
1926 	if (vsi->rss_lut_user)
1927 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1928 	else
1929 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1930 	if (vsi->rss_hkey_user)
1931 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1932 	else
1933 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1934 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1935 	kfree(lut);
1936 	return ret;
1937 }
1938 
1939 /**
1940  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1941  * @vsi: the VSI being configured,
1942  * @ctxt: VSI context structure
1943  * @enabled_tc: number of traffic classes to enable
1944  *
1945  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1946  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1947 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1948 					   struct i40e_vsi_context *ctxt,
1949 					   u8 enabled_tc)
1950 {
1951 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1952 	int i, override_q, pow, num_qps, ret;
1953 	u8 netdev_tc = 0, offset = 0;
1954 
1955 	if (vsi->type != I40E_VSI_MAIN)
1956 		return -EINVAL;
1957 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1958 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1959 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1960 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1961 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1962 
1963 	/* find the next higher power-of-2 of num queue pairs */
1964 	pow = ilog2(num_qps);
1965 	if (!is_power_of_2(num_qps))
1966 		pow++;
1967 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1968 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1969 
1970 	/* Setup queue offset/count for all TCs for given VSI */
1971 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1972 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1973 		/* See if the given TC is enabled for the given VSI */
1974 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1975 			offset = vsi->mqprio_qopt.qopt.offset[i];
1976 			qcount = vsi->mqprio_qopt.qopt.count[i];
1977 			if (qcount > max_qcount)
1978 				max_qcount = qcount;
1979 			vsi->tc_config.tc_info[i].qoffset = offset;
1980 			vsi->tc_config.tc_info[i].qcount = qcount;
1981 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1982 		} else {
1983 			/* TC is not enabled so set the offset to
1984 			 * default queue and allocate one queue
1985 			 * for the given TC.
1986 			 */
1987 			vsi->tc_config.tc_info[i].qoffset = 0;
1988 			vsi->tc_config.tc_info[i].qcount = 1;
1989 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1990 		}
1991 	}
1992 
1993 	/* Set actual Tx/Rx queue pairs */
1994 	vsi->num_queue_pairs = offset + qcount;
1995 
1996 	/* Setup queue TC[0].qmap for given VSI context */
1997 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1998 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1999 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2000 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2001 
2002 	/* Reconfigure RSS for main VSI with max queue count */
2003 	vsi->rss_size = max_qcount;
2004 	ret = i40e_vsi_config_rss(vsi);
2005 	if (ret) {
2006 		dev_info(&vsi->back->pdev->dev,
2007 			 "Failed to reconfig rss for num_queues (%u)\n",
2008 			 max_qcount);
2009 		return ret;
2010 	}
2011 	vsi->reconfig_rss = true;
2012 	dev_dbg(&vsi->back->pdev->dev,
2013 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
2014 
2015 	/* Find queue count available for channel VSIs and starting offset
2016 	 * for channel VSIs
2017 	 */
2018 	override_q = vsi->mqprio_qopt.qopt.count[0];
2019 	if (override_q && override_q < vsi->num_queue_pairs) {
2020 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2021 		vsi->next_base_queue = override_q;
2022 	}
2023 	return 0;
2024 }
2025 
2026 /**
2027  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2028  * @vsi: the VSI being setup
2029  * @ctxt: VSI context structure
2030  * @enabled_tc: Enabled TCs bitmap
2031  * @is_add: True if called before Add VSI
2032  *
2033  * Setup VSI queue mapping for enabled traffic classes.
2034  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)2035 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2036 				     struct i40e_vsi_context *ctxt,
2037 				     u8 enabled_tc,
2038 				     bool is_add)
2039 {
2040 	struct i40e_pf *pf = vsi->back;
2041 	u16 num_tc_qps = 0;
2042 	u16 sections = 0;
2043 	u8 netdev_tc = 0;
2044 	u16 numtc = 1;
2045 	u16 qcount;
2046 	u8 offset;
2047 	u16 qmap;
2048 	int i;
2049 
2050 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2051 	offset = 0;
2052 	/* zero out queue mapping, it will get updated on the end of the function */
2053 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2054 
2055 	if (vsi->type == I40E_VSI_MAIN) {
2056 		/* This code helps add more queue to the VSI if we have
2057 		 * more cores than RSS can support, the higher cores will
2058 		 * be served by ATR or other filters. Furthermore, the
2059 		 * non-zero req_queue_pairs says that user requested a new
2060 		 * queue count via ethtool's set_channels, so use this
2061 		 * value for queues distribution across traffic classes
2062 		 * We need at least one queue pair for the interface
2063 		 * to be usable as we see in else statement.
2064 		 */
2065 		if (vsi->req_queue_pairs > 0)
2066 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2067 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2068 			vsi->num_queue_pairs = pf->num_lan_msix;
2069 		else
2070 			vsi->num_queue_pairs = 1;
2071 	}
2072 
2073 	/* Number of queues per enabled TC */
2074 	if (vsi->type == I40E_VSI_MAIN ||
2075 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2076 		num_tc_qps = vsi->num_queue_pairs;
2077 	else
2078 		num_tc_qps = vsi->alloc_queue_pairs;
2079 
2080 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2081 		/* Find numtc from enabled TC bitmap */
2082 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2083 			if (enabled_tc & BIT(i)) /* TC is enabled */
2084 				numtc++;
2085 		}
2086 		if (!numtc) {
2087 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2088 			numtc = 1;
2089 		}
2090 		num_tc_qps = num_tc_qps / numtc;
2091 		num_tc_qps = min_t(int, num_tc_qps,
2092 				   i40e_pf_get_max_q_per_tc(pf));
2093 	}
2094 
2095 	vsi->tc_config.numtc = numtc;
2096 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2097 
2098 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2099 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2100 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2101 
2102 	/* Setup queue offset/count for all TCs for given VSI */
2103 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2104 		/* See if the given TC is enabled for the given VSI */
2105 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2106 			/* TC is enabled */
2107 			int pow, num_qps;
2108 
2109 			switch (vsi->type) {
2110 			case I40E_VSI_MAIN:
2111 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2112 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2113 				    vsi->tc_config.enabled_tc != 1) {
2114 					qcount = min_t(int, pf->alloc_rss_size,
2115 						       num_tc_qps);
2116 					break;
2117 				}
2118 				fallthrough;
2119 			case I40E_VSI_FDIR:
2120 			case I40E_VSI_SRIOV:
2121 			case I40E_VSI_VMDQ2:
2122 			default:
2123 				qcount = num_tc_qps;
2124 				WARN_ON(i != 0);
2125 				break;
2126 			}
2127 			vsi->tc_config.tc_info[i].qoffset = offset;
2128 			vsi->tc_config.tc_info[i].qcount = qcount;
2129 
2130 			/* find the next higher power-of-2 of num queue pairs */
2131 			num_qps = qcount;
2132 			pow = 0;
2133 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2134 				pow++;
2135 				num_qps >>= 1;
2136 			}
2137 
2138 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2139 			qmap =
2140 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2141 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2142 
2143 			offset += qcount;
2144 		} else {
2145 			/* TC is not enabled so set the offset to
2146 			 * default queue and allocate one queue
2147 			 * for the given TC.
2148 			 */
2149 			vsi->tc_config.tc_info[i].qoffset = 0;
2150 			vsi->tc_config.tc_info[i].qcount = 1;
2151 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2152 
2153 			qmap = 0;
2154 		}
2155 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2156 	}
2157 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2158 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2159 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2160 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2161 		vsi->num_queue_pairs = offset;
2162 
2163 	/* Scheduler section valid can only be set for ADD VSI */
2164 	if (is_add) {
2165 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2166 
2167 		ctxt->info.up_enable_bits = enabled_tc;
2168 	}
2169 	if (vsi->type == I40E_VSI_SRIOV) {
2170 		ctxt->info.mapping_flags |=
2171 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2172 		for (i = 0; i < vsi->num_queue_pairs; i++)
2173 			ctxt->info.queue_mapping[i] =
2174 					       cpu_to_le16(vsi->base_queue + i);
2175 	} else {
2176 		ctxt->info.mapping_flags |=
2177 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2178 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2179 	}
2180 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2181 }
2182 
2183 /**
2184  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2185  * @netdev: the netdevice
2186  * @addr: address to add
2187  *
2188  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2189  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2190  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)2191 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2192 {
2193 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2194 	struct i40e_vsi *vsi = np->vsi;
2195 
2196 	if (i40e_add_mac_filter(vsi, addr))
2197 		return 0;
2198 	else
2199 		return -ENOMEM;
2200 }
2201 
2202 /**
2203  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2204  * @netdev: the netdevice
2205  * @addr: address to add
2206  *
2207  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2208  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2209  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)2210 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2211 {
2212 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2213 	struct i40e_vsi *vsi = np->vsi;
2214 
2215 	/* Under some circumstances, we might receive a request to delete
2216 	 * our own device address from our uc list. Because we store the
2217 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2218 	 * such requests and not delete our device address from this list.
2219 	 */
2220 	if (ether_addr_equal(addr, netdev->dev_addr))
2221 		return 0;
2222 
2223 	i40e_del_mac_filter(vsi, addr);
2224 
2225 	return 0;
2226 }
2227 
2228 /**
2229  * i40e_set_rx_mode - NDO callback to set the netdev filters
2230  * @netdev: network interface device structure
2231  **/
i40e_set_rx_mode(struct net_device * netdev)2232 static void i40e_set_rx_mode(struct net_device *netdev)
2233 {
2234 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2235 	struct i40e_vsi *vsi = np->vsi;
2236 
2237 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2238 
2239 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2240 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2241 
2242 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2243 
2244 	/* check for other flag changes */
2245 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2246 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2247 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2248 	}
2249 }
2250 
2251 /**
2252  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2253  * @vsi: Pointer to VSI struct
2254  * @from: Pointer to list which contains MAC filter entries - changes to
2255  *        those entries needs to be undone.
2256  *
2257  * MAC filter entries from this list were slated for deletion.
2258  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2259 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2260 					 struct hlist_head *from)
2261 {
2262 	struct i40e_mac_filter *f;
2263 	struct hlist_node *h;
2264 
2265 	hlist_for_each_entry_safe(f, h, from, hlist) {
2266 		u64 key = i40e_addr_to_hkey(f->macaddr);
2267 
2268 		/* Move the element back into MAC filter list*/
2269 		hlist_del(&f->hlist);
2270 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2271 	}
2272 }
2273 
2274 /**
2275  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2276  * @vsi: Pointer to vsi struct
2277  * @from: Pointer to list which contains MAC filter entries - changes to
2278  *        those entries needs to be undone.
2279  *
2280  * MAC filter entries from this list were slated for addition.
2281  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2282 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2283 					 struct hlist_head *from)
2284 {
2285 	struct i40e_new_mac_filter *new;
2286 	struct hlist_node *h;
2287 
2288 	hlist_for_each_entry_safe(new, h, from, hlist) {
2289 		/* We can simply free the wrapper structure */
2290 		hlist_del(&new->hlist);
2291 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2292 		kfree(new);
2293 	}
2294 }
2295 
2296 /**
2297  * i40e_next_filter - Get the next non-broadcast filter from a list
2298  * @next: pointer to filter in list
2299  *
2300  * Returns the next non-broadcast filter in the list. Required so that we
2301  * ignore broadcast filters within the list, since these are not handled via
2302  * the normal firmware update path.
2303  */
2304 static
i40e_next_filter(struct i40e_new_mac_filter * next)2305 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2306 {
2307 	hlist_for_each_entry_continue(next, hlist) {
2308 		if (!is_broadcast_ether_addr(next->f->macaddr))
2309 			return next;
2310 	}
2311 
2312 	return NULL;
2313 }
2314 
2315 /**
2316  * i40e_update_filter_state - Update filter state based on return data
2317  * from firmware
2318  * @count: Number of filters added
2319  * @add_list: return data from fw
2320  * @add_head: pointer to first filter in current batch
2321  *
2322  * MAC filter entries from list were slated to be added to device. Returns
2323  * number of successful filters. Note that 0 does NOT mean success!
2324  **/
2325 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2326 i40e_update_filter_state(int count,
2327 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2328 			 struct i40e_new_mac_filter *add_head)
2329 {
2330 	int retval = 0;
2331 	int i;
2332 
2333 	for (i = 0; i < count; i++) {
2334 		/* Always check status of each filter. We don't need to check
2335 		 * the firmware return status because we pre-set the filter
2336 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2337 		 * request to the adminq. Thus, if it no longer matches then
2338 		 * we know the filter is active.
2339 		 */
2340 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2341 			add_head->state = I40E_FILTER_FAILED;
2342 		} else {
2343 			add_head->state = I40E_FILTER_ACTIVE;
2344 			retval++;
2345 		}
2346 
2347 		add_head = i40e_next_filter(add_head);
2348 		if (!add_head)
2349 			break;
2350 	}
2351 
2352 	return retval;
2353 }
2354 
2355 /**
2356  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2357  * @vsi: ptr to the VSI
2358  * @vsi_name: name to display in messages
2359  * @list: the list of filters to send to firmware
2360  * @num_del: the number of filters to delete
2361  * @retval: Set to -EIO on failure to delete
2362  *
2363  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2364  * *retval instead of a return value so that success does not force ret_val to
2365  * be set to 0. This ensures that a sequence of calls to this function
2366  * preserve the previous value of *retval on successful delete.
2367  */
2368 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)2369 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2370 			  struct i40e_aqc_remove_macvlan_element_data *list,
2371 			  int num_del, int *retval)
2372 {
2373 	struct i40e_hw *hw = &vsi->back->hw;
2374 	enum i40e_admin_queue_err aq_status;
2375 	int aq_ret;
2376 
2377 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2378 					   &aq_status);
2379 
2380 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2381 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2382 		*retval = -EIO;
2383 		dev_info(&vsi->back->pdev->dev,
2384 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2385 			 vsi_name, ERR_PTR(aq_ret),
2386 			 i40e_aq_str(hw, aq_status));
2387 	}
2388 }
2389 
2390 /**
2391  * i40e_aqc_add_filters - Request firmware to add a set of filters
2392  * @vsi: ptr to the VSI
2393  * @vsi_name: name to display in messages
2394  * @list: the list of filters to send to firmware
2395  * @add_head: Position in the add hlist
2396  * @num_add: the number of filters to add
2397  *
2398  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2399  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2400  * space for more filters.
2401  */
2402 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)2403 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2404 			  struct i40e_aqc_add_macvlan_element_data *list,
2405 			  struct i40e_new_mac_filter *add_head,
2406 			  int num_add)
2407 {
2408 	struct i40e_hw *hw = &vsi->back->hw;
2409 	enum i40e_admin_queue_err aq_status;
2410 	int fcnt;
2411 
2412 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2413 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2414 
2415 	if (fcnt != num_add) {
2416 		if (vsi->type == I40E_VSI_MAIN) {
2417 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2418 			dev_warn(&vsi->back->pdev->dev,
2419 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2420 				 i40e_aq_str(hw, aq_status), vsi_name);
2421 		} else if (vsi->type == I40E_VSI_SRIOV ||
2422 			   vsi->type == I40E_VSI_VMDQ1 ||
2423 			   vsi->type == I40E_VSI_VMDQ2) {
2424 			dev_warn(&vsi->back->pdev->dev,
2425 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2426 				 i40e_aq_str(hw, aq_status), vsi_name,
2427 					     vsi_name);
2428 		} else {
2429 			dev_warn(&vsi->back->pdev->dev,
2430 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2431 				 i40e_aq_str(hw, aq_status), vsi_name,
2432 					     vsi->type);
2433 		}
2434 	}
2435 }
2436 
2437 /**
2438  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2439  * @vsi: pointer to the VSI
2440  * @vsi_name: the VSI name
2441  * @f: filter data
2442  *
2443  * This function sets or clears the promiscuous broadcast flags for VLAN
2444  * filters in order to properly receive broadcast frames. Assumes that only
2445  * broadcast filters are passed.
2446  *
2447  * Returns status indicating success or failure;
2448  **/
2449 static int
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2450 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2451 			  struct i40e_mac_filter *f)
2452 {
2453 	bool enable = f->state == I40E_FILTER_NEW ||
2454 		      f->state == I40E_FILTER_NEW_SYNC;
2455 	struct i40e_hw *hw = &vsi->back->hw;
2456 	int aq_ret;
2457 
2458 	if (f->vlan == I40E_VLAN_ANY) {
2459 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2460 						   vsi->seid,
2461 						   enable,
2462 						   NULL);
2463 	} else {
2464 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2465 							    vsi->seid,
2466 							    enable,
2467 							    f->vlan,
2468 							    NULL);
2469 	}
2470 
2471 	if (aq_ret) {
2472 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2473 		dev_warn(&vsi->back->pdev->dev,
2474 			 "Error %s, forcing overflow promiscuous on %s\n",
2475 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2476 			 vsi_name);
2477 	}
2478 
2479 	return aq_ret;
2480 }
2481 
2482 /**
2483  * i40e_set_promiscuous - set promiscuous mode
2484  * @pf: board private structure
2485  * @promisc: promisc on or off
2486  *
2487  * There are different ways of setting promiscuous mode on a PF depending on
2488  * what state/environment we're in.  This identifies and sets it appropriately.
2489  * Returns 0 on success.
2490  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2491 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2492 {
2493 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2494 	struct i40e_hw *hw = &pf->hw;
2495 	int aq_ret;
2496 
2497 	if (vsi->type == I40E_VSI_MAIN &&
2498 	    pf->lan_veb != I40E_NO_VEB &&
2499 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2500 		/* set defport ON for Main VSI instead of true promisc
2501 		 * this way we will get all unicast/multicast and VLAN
2502 		 * promisc behavior but will not get VF or VMDq traffic
2503 		 * replicated on the Main VSI.
2504 		 */
2505 		if (promisc)
2506 			aq_ret = i40e_aq_set_default_vsi(hw,
2507 							 vsi->seid,
2508 							 NULL);
2509 		else
2510 			aq_ret = i40e_aq_clear_default_vsi(hw,
2511 							   vsi->seid,
2512 							   NULL);
2513 		if (aq_ret) {
2514 			dev_info(&pf->pdev->dev,
2515 				 "Set default VSI failed, err %pe, aq_err %s\n",
2516 				 ERR_PTR(aq_ret),
2517 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2518 		}
2519 	} else {
2520 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2521 						  hw,
2522 						  vsi->seid,
2523 						  promisc, NULL,
2524 						  true);
2525 		if (aq_ret) {
2526 			dev_info(&pf->pdev->dev,
2527 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2528 				 ERR_PTR(aq_ret),
2529 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2530 		}
2531 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2532 						  hw,
2533 						  vsi->seid,
2534 						  promisc, NULL);
2535 		if (aq_ret) {
2536 			dev_info(&pf->pdev->dev,
2537 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2538 				 ERR_PTR(aq_ret),
2539 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2540 		}
2541 	}
2542 
2543 	if (!aq_ret)
2544 		pf->cur_promisc = promisc;
2545 
2546 	return aq_ret;
2547 }
2548 
2549 /**
2550  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2551  * @vsi: ptr to the VSI
2552  *
2553  * Push any outstanding VSI filter changes through the AdminQ.
2554  *
2555  * Returns 0 or error value
2556  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2557 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2558 {
2559 	struct hlist_head tmp_add_list, tmp_del_list;
2560 	struct i40e_mac_filter *f;
2561 	struct i40e_new_mac_filter *new, *add_head = NULL;
2562 	struct i40e_hw *hw = &vsi->back->hw;
2563 	bool old_overflow, new_overflow;
2564 	unsigned int failed_filters = 0;
2565 	unsigned int vlan_filters = 0;
2566 	char vsi_name[16] = "PF";
2567 	int filter_list_len = 0;
2568 	u32 changed_flags = 0;
2569 	struct hlist_node *h;
2570 	struct i40e_pf *pf;
2571 	int num_add = 0;
2572 	int num_del = 0;
2573 	int aq_ret = 0;
2574 	int retval = 0;
2575 	u16 cmd_flags;
2576 	int list_size;
2577 	int bkt;
2578 
2579 	/* empty array typed pointers, kcalloc later */
2580 	struct i40e_aqc_add_macvlan_element_data *add_list;
2581 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2582 
2583 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2584 		usleep_range(1000, 2000);
2585 	pf = vsi->back;
2586 
2587 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2588 
2589 	if (vsi->netdev) {
2590 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2591 		vsi->current_netdev_flags = vsi->netdev->flags;
2592 	}
2593 
2594 	INIT_HLIST_HEAD(&tmp_add_list);
2595 	INIT_HLIST_HEAD(&tmp_del_list);
2596 
2597 	if (vsi->type == I40E_VSI_SRIOV)
2598 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2599 	else if (vsi->type != I40E_VSI_MAIN)
2600 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2601 
2602 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2603 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2604 
2605 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2606 		/* Create a list of filters to delete. */
2607 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2608 			if (f->state == I40E_FILTER_REMOVE) {
2609 				/* Move the element into temporary del_list */
2610 				hash_del(&f->hlist);
2611 				hlist_add_head(&f->hlist, &tmp_del_list);
2612 
2613 				/* Avoid counting removed filters */
2614 				continue;
2615 			}
2616 			if (f->state == I40E_FILTER_NEW) {
2617 				/* Create a temporary i40e_new_mac_filter */
2618 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2619 				if (!new)
2620 					goto err_no_memory_locked;
2621 
2622 				/* Store pointer to the real filter */
2623 				new->f = f;
2624 				new->state = f->state;
2625 
2626 				/* Add it to the hash list */
2627 				hlist_add_head(&new->hlist, &tmp_add_list);
2628 				f->state = I40E_FILTER_NEW_SYNC;
2629 			}
2630 
2631 			/* Count the number of active (current and new) VLAN
2632 			 * filters we have now. Does not count filters which
2633 			 * are marked for deletion.
2634 			 */
2635 			if (f->vlan > 0)
2636 				vlan_filters++;
2637 		}
2638 
2639 		if (vsi->type != I40E_VSI_SRIOV)
2640 			retval = i40e_correct_mac_vlan_filters
2641 				(vsi, &tmp_add_list, &tmp_del_list,
2642 				 vlan_filters);
2643 		else if (pf->vf)
2644 			retval = i40e_correct_vf_mac_vlan_filters
2645 				(vsi, &tmp_add_list, &tmp_del_list,
2646 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2647 
2648 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2649 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2650 
2651 		if (retval)
2652 			goto err_no_memory_locked;
2653 
2654 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2655 	}
2656 
2657 	/* Now process 'del_list' outside the lock */
2658 	if (!hlist_empty(&tmp_del_list)) {
2659 		filter_list_len = hw->aq.asq_buf_size /
2660 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2661 		list_size = filter_list_len *
2662 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2663 		del_list = kzalloc(list_size, GFP_ATOMIC);
2664 		if (!del_list)
2665 			goto err_no_memory;
2666 
2667 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2668 			cmd_flags = 0;
2669 
2670 			/* handle broadcast filters by updating the broadcast
2671 			 * promiscuous flag and release filter list.
2672 			 */
2673 			if (is_broadcast_ether_addr(f->macaddr)) {
2674 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2675 
2676 				hlist_del(&f->hlist);
2677 				kfree(f);
2678 				continue;
2679 			}
2680 
2681 			/* add to delete list */
2682 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2683 			if (f->vlan == I40E_VLAN_ANY) {
2684 				del_list[num_del].vlan_tag = 0;
2685 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2686 			} else {
2687 				del_list[num_del].vlan_tag =
2688 					cpu_to_le16((u16)(f->vlan));
2689 			}
2690 
2691 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2692 			del_list[num_del].flags = cmd_flags;
2693 			num_del++;
2694 
2695 			/* flush a full buffer */
2696 			if (num_del == filter_list_len) {
2697 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2698 						     num_del, &retval);
2699 				memset(del_list, 0, list_size);
2700 				num_del = 0;
2701 			}
2702 			/* Release memory for MAC filter entries which were
2703 			 * synced up with HW.
2704 			 */
2705 			hlist_del(&f->hlist);
2706 			kfree(f);
2707 		}
2708 
2709 		if (num_del) {
2710 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2711 					     num_del, &retval);
2712 		}
2713 
2714 		kfree(del_list);
2715 		del_list = NULL;
2716 	}
2717 
2718 	if (!hlist_empty(&tmp_add_list)) {
2719 		/* Do all the adds now. */
2720 		filter_list_len = hw->aq.asq_buf_size /
2721 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2722 		list_size = filter_list_len *
2723 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2724 		add_list = kzalloc(list_size, GFP_ATOMIC);
2725 		if (!add_list)
2726 			goto err_no_memory;
2727 
2728 		num_add = 0;
2729 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2730 			/* handle broadcast filters by updating the broadcast
2731 			 * promiscuous flag instead of adding a MAC filter.
2732 			 */
2733 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2734 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2735 							      new->f))
2736 					new->state = I40E_FILTER_FAILED;
2737 				else
2738 					new->state = I40E_FILTER_ACTIVE;
2739 				continue;
2740 			}
2741 
2742 			/* add to add array */
2743 			if (num_add == 0)
2744 				add_head = new;
2745 			cmd_flags = 0;
2746 			ether_addr_copy(add_list[num_add].mac_addr,
2747 					new->f->macaddr);
2748 			if (new->f->vlan == I40E_VLAN_ANY) {
2749 				add_list[num_add].vlan_tag = 0;
2750 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2751 			} else {
2752 				add_list[num_add].vlan_tag =
2753 					cpu_to_le16((u16)(new->f->vlan));
2754 			}
2755 			add_list[num_add].queue_number = 0;
2756 			/* set invalid match method for later detection */
2757 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2758 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2759 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2760 			num_add++;
2761 
2762 			/* flush a full buffer */
2763 			if (num_add == filter_list_len) {
2764 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2765 						     add_head, num_add);
2766 				memset(add_list, 0, list_size);
2767 				num_add = 0;
2768 			}
2769 		}
2770 		if (num_add) {
2771 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2772 					     num_add);
2773 		}
2774 		/* Now move all of the filters from the temp add list back to
2775 		 * the VSI's list.
2776 		 */
2777 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2778 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2779 			/* Only update the state if we're still NEW */
2780 			if (new->f->state == I40E_FILTER_NEW ||
2781 			    new->f->state == I40E_FILTER_NEW_SYNC)
2782 				new->f->state = new->state;
2783 			hlist_del(&new->hlist);
2784 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2785 			kfree(new);
2786 		}
2787 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2788 		kfree(add_list);
2789 		add_list = NULL;
2790 	}
2791 
2792 	/* Determine the number of active and failed filters. */
2793 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2794 	vsi->active_filters = 0;
2795 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2796 		if (f->state == I40E_FILTER_ACTIVE)
2797 			vsi->active_filters++;
2798 		else if (f->state == I40E_FILTER_FAILED)
2799 			failed_filters++;
2800 	}
2801 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2802 
2803 	/* Check if we are able to exit overflow promiscuous mode. We can
2804 	 * safely exit if we didn't just enter, we no longer have any failed
2805 	 * filters, and we have reduced filters below the threshold value.
2806 	 */
2807 	if (old_overflow && !failed_filters &&
2808 	    vsi->active_filters < vsi->promisc_threshold) {
2809 		dev_info(&pf->pdev->dev,
2810 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2811 			 vsi_name);
2812 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2813 		vsi->promisc_threshold = 0;
2814 	}
2815 
2816 	/* if the VF is not trusted do not do promisc */
2817 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2818 	    !pf->vf[vsi->vf_id].trusted) {
2819 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2820 		goto out;
2821 	}
2822 
2823 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2824 
2825 	/* If we are entering overflow promiscuous, we need to calculate a new
2826 	 * threshold for when we are safe to exit
2827 	 */
2828 	if (!old_overflow && new_overflow)
2829 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2830 
2831 	/* check for changes in promiscuous modes */
2832 	if (changed_flags & IFF_ALLMULTI) {
2833 		bool cur_multipromisc;
2834 
2835 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2836 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2837 							       vsi->seid,
2838 							       cur_multipromisc,
2839 							       NULL);
2840 		if (aq_ret) {
2841 			retval = i40e_aq_rc_to_posix(aq_ret,
2842 						     hw->aq.asq_last_status);
2843 			dev_info(&pf->pdev->dev,
2844 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2845 				 vsi_name,
2846 				 ERR_PTR(aq_ret),
2847 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2848 		} else {
2849 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2850 				 cur_multipromisc ? "entering" : "leaving");
2851 		}
2852 	}
2853 
2854 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2855 		bool cur_promisc;
2856 
2857 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2858 			       new_overflow);
2859 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2860 		if (aq_ret) {
2861 			retval = i40e_aq_rc_to_posix(aq_ret,
2862 						     hw->aq.asq_last_status);
2863 			dev_info(&pf->pdev->dev,
2864 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2865 				 cur_promisc ? "on" : "off",
2866 				 vsi_name,
2867 				 ERR_PTR(aq_ret),
2868 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2869 		}
2870 	}
2871 out:
2872 	/* if something went wrong then set the changed flag so we try again */
2873 	if (retval)
2874 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2875 
2876 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2877 	return retval;
2878 
2879 err_no_memory:
2880 	/* Restore elements on the temporary add and delete lists */
2881 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2882 err_no_memory_locked:
2883 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2884 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2885 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2886 
2887 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2888 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2889 	return -ENOMEM;
2890 }
2891 
2892 /**
2893  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2894  * @pf: board private structure
2895  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2896 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2897 {
2898 	int v;
2899 
2900 	if (!pf)
2901 		return;
2902 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2903 		return;
2904 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2905 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2906 		return;
2907 	}
2908 
2909 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2910 		if (pf->vsi[v] &&
2911 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2912 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2913 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2914 
2915 			if (ret) {
2916 				/* come back and try again later */
2917 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2918 					pf->state);
2919 				break;
2920 			}
2921 		}
2922 	}
2923 }
2924 
2925 /**
2926  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2927  *
2928  * @vsi: VSI to calculate rx_buf_len from
2929  */
i40e_calculate_vsi_rx_buf_len(struct i40e_vsi * vsi)2930 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2931 {
2932 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2933 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2934 
2935 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2936 }
2937 
2938 /**
2939  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2940  * @vsi: the vsi
2941  * @xdp_prog: XDP program
2942  **/
i40e_max_vsi_frame_size(struct i40e_vsi * vsi,struct bpf_prog * xdp_prog)2943 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2944 				   struct bpf_prog *xdp_prog)
2945 {
2946 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2947 	u16 chain_len;
2948 
2949 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2950 		chain_len = 1;
2951 	else
2952 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2953 
2954 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2955 }
2956 
2957 /**
2958  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2959  * @netdev: network interface device structure
2960  * @new_mtu: new value for maximum frame size
2961  *
2962  * Returns 0 on success, negative on failure
2963  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2964 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2965 {
2966 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2967 	struct i40e_vsi *vsi = np->vsi;
2968 	struct i40e_pf *pf = vsi->back;
2969 	int frame_size;
2970 
2971 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2972 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2973 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2974 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2975 		return -EINVAL;
2976 	}
2977 
2978 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2979 		   netdev->mtu, new_mtu);
2980 	netdev->mtu = new_mtu;
2981 	if (netif_running(netdev))
2982 		i40e_vsi_reinit_locked(vsi);
2983 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2984 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2985 	return 0;
2986 }
2987 
2988 /**
2989  * i40e_ioctl - Access the hwtstamp interface
2990  * @netdev: network interface device structure
2991  * @ifr: interface request data
2992  * @cmd: ioctl command
2993  **/
i40e_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)2994 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2995 {
2996 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2997 	struct i40e_pf *pf = np->vsi->back;
2998 
2999 	switch (cmd) {
3000 	case SIOCGHWTSTAMP:
3001 		return i40e_ptp_get_ts_config(pf, ifr);
3002 	case SIOCSHWTSTAMP:
3003 		return i40e_ptp_set_ts_config(pf, ifr);
3004 	default:
3005 		return -EOPNOTSUPP;
3006 	}
3007 }
3008 
3009 /**
3010  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
3011  * @vsi: the vsi being adjusted
3012  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)3013 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
3014 {
3015 	struct i40e_vsi_context ctxt;
3016 	int ret;
3017 
3018 	/* Don't modify stripping options if a port VLAN is active */
3019 	if (vsi->info.pvid)
3020 		return;
3021 
3022 	if ((vsi->info.valid_sections &
3023 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3024 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
3025 		return;  /* already enabled */
3026 
3027 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3028 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3029 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3030 
3031 	ctxt.seid = vsi->seid;
3032 	ctxt.info = vsi->info;
3033 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3034 	if (ret) {
3035 		dev_info(&vsi->back->pdev->dev,
3036 			 "update vlan stripping failed, err %pe aq_err %s\n",
3037 			 ERR_PTR(ret),
3038 			 i40e_aq_str(&vsi->back->hw,
3039 				     vsi->back->hw.aq.asq_last_status));
3040 	}
3041 }
3042 
3043 /**
3044  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3045  * @vsi: the vsi being adjusted
3046  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)3047 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3048 {
3049 	struct i40e_vsi_context ctxt;
3050 	int ret;
3051 
3052 	/* Don't modify stripping options if a port VLAN is active */
3053 	if (vsi->info.pvid)
3054 		return;
3055 
3056 	if ((vsi->info.valid_sections &
3057 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3058 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3059 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3060 		return;  /* already disabled */
3061 
3062 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3063 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3064 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3065 
3066 	ctxt.seid = vsi->seid;
3067 	ctxt.info = vsi->info;
3068 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3069 	if (ret) {
3070 		dev_info(&vsi->back->pdev->dev,
3071 			 "update vlan stripping failed, err %pe aq_err %s\n",
3072 			 ERR_PTR(ret),
3073 			 i40e_aq_str(&vsi->back->hw,
3074 				     vsi->back->hw.aq.asq_last_status));
3075 	}
3076 }
3077 
3078 /**
3079  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3080  * @vsi: the vsi being configured
3081  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3082  *
3083  * This is a helper function for adding a new MAC/VLAN filter with the
3084  * specified VLAN for each existing MAC address already in the hash table.
3085  * This function does *not* perform any accounting to update filters based on
3086  * VLAN mode.
3087  *
3088  * NOTE: this function expects to be called while under the
3089  * mac_filter_hash_lock
3090  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3091 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3092 {
3093 	struct i40e_mac_filter *f, *add_f;
3094 	struct hlist_node *h;
3095 	int bkt;
3096 
3097 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3098 		/* If we're asked to add a filter that has been marked for
3099 		 * removal, it is safe to simply restore it to active state.
3100 		 * __i40e_del_filter will have simply deleted any filters which
3101 		 * were previously marked NEW or FAILED, so if it is currently
3102 		 * marked REMOVE it must have previously been ACTIVE. Since we
3103 		 * haven't yet run the sync filters task, just restore this
3104 		 * filter to the ACTIVE state so that the sync task leaves it
3105 		 * in place.
3106 		 */
3107 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3108 			f->state = I40E_FILTER_ACTIVE;
3109 			continue;
3110 		} else if (f->state == I40E_FILTER_REMOVE) {
3111 			continue;
3112 		}
3113 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3114 		if (!add_f) {
3115 			dev_info(&vsi->back->pdev->dev,
3116 				 "Could not add vlan filter %d for %pM\n",
3117 				 vid, f->macaddr);
3118 			return -ENOMEM;
3119 		}
3120 	}
3121 
3122 	return 0;
3123 }
3124 
3125 /**
3126  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3127  * @vsi: the VSI being configured
3128  * @vid: VLAN id to be added
3129  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)3130 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3131 {
3132 	int err;
3133 
3134 	if (vsi->info.pvid)
3135 		return -EINVAL;
3136 
3137 	/* The network stack will attempt to add VID=0, with the intention to
3138 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3139 	 * these packets by default when configured to receive untagged
3140 	 * packets, so we don't need to add a filter for this case.
3141 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3142 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3143 	 * Thus, we do not want to actually add a filter for VID=0
3144 	 */
3145 	if (!vid)
3146 		return 0;
3147 
3148 	/* Locked once because all functions invoked below iterates list*/
3149 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3150 	err = i40e_add_vlan_all_mac(vsi, vid);
3151 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3152 	if (err)
3153 		return err;
3154 
3155 	/* schedule our worker thread which will take care of
3156 	 * applying the new filter changes
3157 	 */
3158 	i40e_service_event_schedule(vsi->back);
3159 	return 0;
3160 }
3161 
3162 /**
3163  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3164  * @vsi: the vsi being configured
3165  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3166  *
3167  * This function should be used to remove all VLAN filters which match the
3168  * given VID. It does not schedule the service event and does not take the
3169  * mac_filter_hash_lock so it may be combined with other operations under
3170  * a single invocation of the mac_filter_hash_lock.
3171  *
3172  * NOTE: this function expects to be called while under the
3173  * mac_filter_hash_lock
3174  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3175 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3176 {
3177 	struct i40e_mac_filter *f;
3178 	struct hlist_node *h;
3179 	int bkt;
3180 
3181 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3182 		if (f->vlan == vid)
3183 			__i40e_del_filter(vsi, f);
3184 	}
3185 }
3186 
3187 /**
3188  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3189  * @vsi: the VSI being configured
3190  * @vid: VLAN id to be removed
3191  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)3192 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3193 {
3194 	if (!vid || vsi->info.pvid)
3195 		return;
3196 
3197 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3198 	i40e_rm_vlan_all_mac(vsi, vid);
3199 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3200 
3201 	/* schedule our worker thread which will take care of
3202 	 * applying the new filter changes
3203 	 */
3204 	i40e_service_event_schedule(vsi->back);
3205 }
3206 
3207 /**
3208  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3209  * @netdev: network interface to be adjusted
3210  * @proto: unused protocol value
3211  * @vid: vlan id to be added
3212  *
3213  * net_device_ops implementation for adding vlan ids
3214  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3215 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3216 				__always_unused __be16 proto, u16 vid)
3217 {
3218 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3219 	struct i40e_vsi *vsi = np->vsi;
3220 	int ret = 0;
3221 
3222 	if (vid >= VLAN_N_VID)
3223 		return -EINVAL;
3224 
3225 	ret = i40e_vsi_add_vlan(vsi, vid);
3226 	if (!ret)
3227 		set_bit(vid, vsi->active_vlans);
3228 
3229 	return ret;
3230 }
3231 
3232 /**
3233  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3234  * @netdev: network interface to be adjusted
3235  * @proto: unused protocol value
3236  * @vid: vlan id to be added
3237  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3238 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3239 				    __always_unused __be16 proto, u16 vid)
3240 {
3241 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3242 	struct i40e_vsi *vsi = np->vsi;
3243 
3244 	if (vid >= VLAN_N_VID)
3245 		return;
3246 	set_bit(vid, vsi->active_vlans);
3247 }
3248 
3249 /**
3250  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3251  * @netdev: network interface to be adjusted
3252  * @proto: unused protocol value
3253  * @vid: vlan id to be removed
3254  *
3255  * net_device_ops implementation for removing vlan ids
3256  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3257 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3258 				 __always_unused __be16 proto, u16 vid)
3259 {
3260 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3261 	struct i40e_vsi *vsi = np->vsi;
3262 
3263 	/* return code is ignored as there is nothing a user
3264 	 * can do about failure to remove and a log message was
3265 	 * already printed from the other function
3266 	 */
3267 	i40e_vsi_kill_vlan(vsi, vid);
3268 
3269 	clear_bit(vid, vsi->active_vlans);
3270 
3271 	return 0;
3272 }
3273 
3274 /**
3275  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3276  * @vsi: the vsi being brought back up
3277  **/
i40e_restore_vlan(struct i40e_vsi * vsi)3278 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3279 {
3280 	u16 vid;
3281 
3282 	if (!vsi->netdev)
3283 		return;
3284 
3285 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3286 		i40e_vlan_stripping_enable(vsi);
3287 	else
3288 		i40e_vlan_stripping_disable(vsi);
3289 
3290 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3291 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3292 					vid);
3293 }
3294 
3295 /**
3296  * i40e_vsi_add_pvid - Add pvid for the VSI
3297  * @vsi: the vsi being adjusted
3298  * @vid: the vlan id to set as a PVID
3299  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)3300 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3301 {
3302 	struct i40e_vsi_context ctxt;
3303 	int ret;
3304 
3305 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3306 	vsi->info.pvid = cpu_to_le16(vid);
3307 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3308 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3309 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3310 
3311 	ctxt.seid = vsi->seid;
3312 	ctxt.info = vsi->info;
3313 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3314 	if (ret) {
3315 		dev_info(&vsi->back->pdev->dev,
3316 			 "add pvid failed, err %pe aq_err %s\n",
3317 			 ERR_PTR(ret),
3318 			 i40e_aq_str(&vsi->back->hw,
3319 				     vsi->back->hw.aq.asq_last_status));
3320 		return -ENOENT;
3321 	}
3322 
3323 	return 0;
3324 }
3325 
3326 /**
3327  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3328  * @vsi: the vsi being adjusted
3329  *
3330  * Just use the vlan_rx_register() service to put it back to normal
3331  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3332 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3333 {
3334 	vsi->info.pvid = 0;
3335 
3336 	i40e_vlan_stripping_disable(vsi);
3337 }
3338 
3339 /**
3340  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3341  * @vsi: ptr to the VSI
3342  *
3343  * If this function returns with an error, then it's possible one or
3344  * more of the rings is populated (while the rest are not).  It is the
3345  * callers duty to clean those orphaned rings.
3346  *
3347  * Return 0 on success, negative on failure
3348  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3349 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3350 {
3351 	int i, err = 0;
3352 
3353 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3354 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3355 
3356 	if (!i40e_enabled_xdp_vsi(vsi))
3357 		return err;
3358 
3359 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3360 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3361 
3362 	return err;
3363 }
3364 
3365 /**
3366  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3367  * @vsi: ptr to the VSI
3368  *
3369  * Free VSI's transmit software resources
3370  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3371 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3372 {
3373 	int i;
3374 
3375 	if (vsi->tx_rings) {
3376 		for (i = 0; i < vsi->num_queue_pairs; i++)
3377 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3378 				i40e_free_tx_resources(vsi->tx_rings[i]);
3379 	}
3380 
3381 	if (vsi->xdp_rings) {
3382 		for (i = 0; i < vsi->num_queue_pairs; i++)
3383 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3384 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3385 	}
3386 }
3387 
3388 /**
3389  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3390  * @vsi: ptr to the VSI
3391  *
3392  * If this function returns with an error, then it's possible one or
3393  * more of the rings is populated (while the rest are not).  It is the
3394  * callers duty to clean those orphaned rings.
3395  *
3396  * Return 0 on success, negative on failure
3397  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3398 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3399 {
3400 	int i, err = 0;
3401 
3402 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3403 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3404 	return err;
3405 }
3406 
3407 /**
3408  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3409  * @vsi: ptr to the VSI
3410  *
3411  * Free all receive software resources
3412  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3413 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3414 {
3415 	int i;
3416 
3417 	if (!vsi->rx_rings)
3418 		return;
3419 
3420 	for (i = 0; i < vsi->num_queue_pairs; i++)
3421 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3422 			i40e_free_rx_resources(vsi->rx_rings[i]);
3423 }
3424 
3425 /**
3426  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3427  * @ring: The Tx ring to configure
3428  *
3429  * This enables/disables XPS for a given Tx descriptor ring
3430  * based on the TCs enabled for the VSI that ring belongs to.
3431  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3432 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3433 {
3434 	int cpu;
3435 
3436 	if (!ring->q_vector || !ring->netdev || ring->ch)
3437 		return;
3438 
3439 	/* We only initialize XPS once, so as not to overwrite user settings */
3440 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3441 		return;
3442 
3443 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3444 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3445 			    ring->queue_index);
3446 }
3447 
3448 /**
3449  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3450  * @ring: The Tx or Rx ring
3451  *
3452  * Returns the AF_XDP buffer pool or NULL.
3453  **/
i40e_xsk_pool(struct i40e_ring * ring)3454 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3455 {
3456 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3457 	int qid = ring->queue_index;
3458 
3459 	if (ring_is_xdp(ring))
3460 		qid -= ring->vsi->alloc_queue_pairs;
3461 
3462 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3463 		return NULL;
3464 
3465 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3466 }
3467 
3468 /**
3469  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3470  * @ring: The Tx ring to configure
3471  *
3472  * Configure the Tx descriptor ring in the HMC context.
3473  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3474 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3475 {
3476 	struct i40e_vsi *vsi = ring->vsi;
3477 	u16 pf_q = vsi->base_queue + ring->queue_index;
3478 	struct i40e_hw *hw = &vsi->back->hw;
3479 	struct i40e_hmc_obj_txq tx_ctx;
3480 	u32 qtx_ctl = 0;
3481 	int err = 0;
3482 
3483 	if (ring_is_xdp(ring))
3484 		ring->xsk_pool = i40e_xsk_pool(ring);
3485 
3486 	/* some ATR related tx ring init */
3487 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3488 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3489 		ring->atr_count = 0;
3490 	} else {
3491 		ring->atr_sample_rate = 0;
3492 	}
3493 
3494 	/* configure XPS */
3495 	i40e_config_xps_tx_ring(ring);
3496 
3497 	/* clear the context structure first */
3498 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3499 
3500 	tx_ctx.new_context = 1;
3501 	tx_ctx.base = (ring->dma / 128);
3502 	tx_ctx.qlen = ring->count;
3503 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3504 					       I40E_FLAG_FD_ATR_ENABLED));
3505 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3506 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3507 	if (vsi->type != I40E_VSI_FDIR)
3508 		tx_ctx.head_wb_ena = 1;
3509 	tx_ctx.head_wb_addr = ring->dma +
3510 			      (ring->count * sizeof(struct i40e_tx_desc));
3511 
3512 	/* As part of VSI creation/update, FW allocates certain
3513 	 * Tx arbitration queue sets for each TC enabled for
3514 	 * the VSI. The FW returns the handles to these queue
3515 	 * sets as part of the response buffer to Add VSI,
3516 	 * Update VSI, etc. AQ commands. It is expected that
3517 	 * these queue set handles be associated with the Tx
3518 	 * queues by the driver as part of the TX queue context
3519 	 * initialization. This has to be done regardless of
3520 	 * DCB as by default everything is mapped to TC0.
3521 	 */
3522 
3523 	if (ring->ch)
3524 		tx_ctx.rdylist =
3525 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3526 
3527 	else
3528 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3529 
3530 	tx_ctx.rdylist_act = 0;
3531 
3532 	/* clear the context in the HMC */
3533 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3534 	if (err) {
3535 		dev_info(&vsi->back->pdev->dev,
3536 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3537 			 ring->queue_index, pf_q, err);
3538 		return -ENOMEM;
3539 	}
3540 
3541 	/* set the context in the HMC */
3542 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3543 	if (err) {
3544 		dev_info(&vsi->back->pdev->dev,
3545 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3546 			 ring->queue_index, pf_q, err);
3547 		return -ENOMEM;
3548 	}
3549 
3550 	/* Now associate this queue with this PCI function */
3551 	if (ring->ch) {
3552 		if (ring->ch->type == I40E_VSI_VMDQ2)
3553 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3554 		else
3555 			return -EINVAL;
3556 
3557 		qtx_ctl |= (ring->ch->vsi_number <<
3558 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3559 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3560 	} else {
3561 		if (vsi->type == I40E_VSI_VMDQ2) {
3562 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3563 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3564 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3565 		} else {
3566 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3567 		}
3568 	}
3569 
3570 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3571 		    I40E_QTX_CTL_PF_INDX_MASK);
3572 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3573 	i40e_flush(hw);
3574 
3575 	/* cache tail off for easier writes later */
3576 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3577 
3578 	return 0;
3579 }
3580 
3581 /**
3582  * i40e_rx_offset - Return expected offset into page to access data
3583  * @rx_ring: Ring we are requesting offset of
3584  *
3585  * Returns the offset value for ring into the data buffer.
3586  */
i40e_rx_offset(struct i40e_ring * rx_ring)3587 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3588 {
3589 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3590 }
3591 
3592 /**
3593  * i40e_configure_rx_ring - Configure a receive ring context
3594  * @ring: The Rx ring to configure
3595  *
3596  * Configure the Rx descriptor ring in the HMC context.
3597  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3598 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3599 {
3600 	struct i40e_vsi *vsi = ring->vsi;
3601 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3602 	u16 pf_q = vsi->base_queue + ring->queue_index;
3603 	struct i40e_hw *hw = &vsi->back->hw;
3604 	struct i40e_hmc_obj_rxq rx_ctx;
3605 	int err = 0;
3606 	bool ok;
3607 
3608 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3609 
3610 	/* clear the context structure first */
3611 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3612 
3613 	ring->rx_buf_len = vsi->rx_buf_len;
3614 
3615 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3616 	if (ring->vsi->type != I40E_VSI_MAIN)
3617 		goto skip;
3618 
3619 	if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
3620 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3621 					 ring->queue_index,
3622 					 ring->q_vector->napi.napi_id,
3623 					 ring->rx_buf_len);
3624 		if (err)
3625 			return err;
3626 	}
3627 
3628 	ring->xsk_pool = i40e_xsk_pool(ring);
3629 	if (ring->xsk_pool) {
3630 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3631 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3632 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3633 					 ring->queue_index,
3634 					 ring->q_vector->napi.napi_id,
3635 					 ring->rx_buf_len);
3636 		if (err)
3637 			return err;
3638 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3639 						 MEM_TYPE_XSK_BUFF_POOL,
3640 						 NULL);
3641 		if (err)
3642 			return err;
3643 		dev_info(&vsi->back->pdev->dev,
3644 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3645 			 ring->queue_index);
3646 
3647 	} else {
3648 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3649 						 MEM_TYPE_PAGE_SHARED,
3650 						 NULL);
3651 		if (err)
3652 			return err;
3653 	}
3654 
3655 skip:
3656 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3657 
3658 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3659 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3660 
3661 	rx_ctx.base = (ring->dma / 128);
3662 	rx_ctx.qlen = ring->count;
3663 
3664 	/* use 16 byte descriptors */
3665 	rx_ctx.dsize = 0;
3666 
3667 	/* descriptor type is always zero
3668 	 * rx_ctx.dtype = 0;
3669 	 */
3670 	rx_ctx.hsplit_0 = 0;
3671 
3672 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3673 	if (hw->revision_id == 0)
3674 		rx_ctx.lrxqthresh = 0;
3675 	else
3676 		rx_ctx.lrxqthresh = 1;
3677 	rx_ctx.crcstrip = 1;
3678 	rx_ctx.l2tsel = 1;
3679 	/* this controls whether VLAN is stripped from inner headers */
3680 	rx_ctx.showiv = 0;
3681 	/* set the prefena field to 1 because the manual says to */
3682 	rx_ctx.prefena = 1;
3683 
3684 	/* clear the context in the HMC */
3685 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3686 	if (err) {
3687 		dev_info(&vsi->back->pdev->dev,
3688 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3689 			 ring->queue_index, pf_q, err);
3690 		return -ENOMEM;
3691 	}
3692 
3693 	/* set the context in the HMC */
3694 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3695 	if (err) {
3696 		dev_info(&vsi->back->pdev->dev,
3697 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3698 			 ring->queue_index, pf_q, err);
3699 		return -ENOMEM;
3700 	}
3701 
3702 	/* configure Rx buffer alignment */
3703 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3704 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3705 			dev_info(&vsi->back->pdev->dev,
3706 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3707 			return -EOPNOTSUPP;
3708 		}
3709 		clear_ring_build_skb_enabled(ring);
3710 	} else {
3711 		set_ring_build_skb_enabled(ring);
3712 	}
3713 
3714 	ring->rx_offset = i40e_rx_offset(ring);
3715 
3716 	/* cache tail for quicker writes, and clear the reg before use */
3717 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3718 	writel(0, ring->tail);
3719 
3720 	if (ring->xsk_pool) {
3721 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3722 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3723 	} else {
3724 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3725 	}
3726 	if (!ok) {
3727 		/* Log this in case the user has forgotten to give the kernel
3728 		 * any buffers, even later in the application.
3729 		 */
3730 		dev_info(&vsi->back->pdev->dev,
3731 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3732 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3733 			 ring->queue_index, pf_q);
3734 	}
3735 
3736 	return 0;
3737 }
3738 
3739 /**
3740  * i40e_vsi_configure_tx - Configure the VSI for Tx
3741  * @vsi: VSI structure describing this set of rings and resources
3742  *
3743  * Configure the Tx VSI for operation.
3744  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3745 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3746 {
3747 	int err = 0;
3748 	u16 i;
3749 
3750 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3751 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3752 
3753 	if (err || !i40e_enabled_xdp_vsi(vsi))
3754 		return err;
3755 
3756 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3757 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3758 
3759 	return err;
3760 }
3761 
3762 /**
3763  * i40e_vsi_configure_rx - Configure the VSI for Rx
3764  * @vsi: the VSI being configured
3765  *
3766  * Configure the Rx VSI for operation.
3767  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3768 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3769 {
3770 	int err = 0;
3771 	u16 i;
3772 
3773 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3774 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3775 
3776 #if (PAGE_SIZE < 8192)
3777 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3778 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3779 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3780 		vsi->max_frame = vsi->rx_buf_len;
3781 	}
3782 #endif
3783 
3784 	/* set up individual rings */
3785 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3786 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3787 
3788 	return err;
3789 }
3790 
3791 /**
3792  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3793  * @vsi: ptr to the VSI
3794  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3795 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3796 {
3797 	struct i40e_ring *tx_ring, *rx_ring;
3798 	u16 qoffset, qcount;
3799 	int i, n;
3800 
3801 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3802 		/* Reset the TC information */
3803 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3804 			rx_ring = vsi->rx_rings[i];
3805 			tx_ring = vsi->tx_rings[i];
3806 			rx_ring->dcb_tc = 0;
3807 			tx_ring->dcb_tc = 0;
3808 		}
3809 		return;
3810 	}
3811 
3812 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3813 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3814 			continue;
3815 
3816 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3817 		qcount = vsi->tc_config.tc_info[n].qcount;
3818 		for (i = qoffset; i < (qoffset + qcount); i++) {
3819 			rx_ring = vsi->rx_rings[i];
3820 			tx_ring = vsi->tx_rings[i];
3821 			rx_ring->dcb_tc = n;
3822 			tx_ring->dcb_tc = n;
3823 		}
3824 	}
3825 }
3826 
3827 /**
3828  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3829  * @vsi: ptr to the VSI
3830  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3831 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3832 {
3833 	if (vsi->netdev)
3834 		i40e_set_rx_mode(vsi->netdev);
3835 }
3836 
3837 /**
3838  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3839  * @pf: Pointer to the targeted PF
3840  *
3841  * Set all flow director counters to 0.
3842  */
i40e_reset_fdir_filter_cnt(struct i40e_pf * pf)3843 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3844 {
3845 	pf->fd_tcp4_filter_cnt = 0;
3846 	pf->fd_udp4_filter_cnt = 0;
3847 	pf->fd_sctp4_filter_cnt = 0;
3848 	pf->fd_ip4_filter_cnt = 0;
3849 	pf->fd_tcp6_filter_cnt = 0;
3850 	pf->fd_udp6_filter_cnt = 0;
3851 	pf->fd_sctp6_filter_cnt = 0;
3852 	pf->fd_ip6_filter_cnt = 0;
3853 }
3854 
3855 /**
3856  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3857  * @vsi: Pointer to the targeted VSI
3858  *
3859  * This function replays the hlist on the hw where all the SB Flow Director
3860  * filters were saved.
3861  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3862 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3863 {
3864 	struct i40e_fdir_filter *filter;
3865 	struct i40e_pf *pf = vsi->back;
3866 	struct hlist_node *node;
3867 
3868 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3869 		return;
3870 
3871 	/* Reset FDir counters as we're replaying all existing filters */
3872 	i40e_reset_fdir_filter_cnt(pf);
3873 
3874 	hlist_for_each_entry_safe(filter, node,
3875 				  &pf->fdir_filter_list, fdir_node) {
3876 		i40e_add_del_fdir(vsi, filter, true);
3877 	}
3878 }
3879 
3880 /**
3881  * i40e_vsi_configure - Set up the VSI for action
3882  * @vsi: the VSI being configured
3883  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3884 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3885 {
3886 	int err;
3887 
3888 	i40e_set_vsi_rx_mode(vsi);
3889 	i40e_restore_vlan(vsi);
3890 	i40e_vsi_config_dcb_rings(vsi);
3891 	err = i40e_vsi_configure_tx(vsi);
3892 	if (!err)
3893 		err = i40e_vsi_configure_rx(vsi);
3894 
3895 	return err;
3896 }
3897 
3898 /**
3899  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3900  * @vsi: the VSI being configured
3901  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3902 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3903 {
3904 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3905 	struct i40e_pf *pf = vsi->back;
3906 	struct i40e_hw *hw = &pf->hw;
3907 	u16 vector;
3908 	int i, q;
3909 	u32 qp;
3910 
3911 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3912 	 * and PFINT_LNKLSTn registers, e.g.:
3913 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3914 	 */
3915 	qp = vsi->base_queue;
3916 	vector = vsi->base_vector;
3917 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3918 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3919 
3920 		q_vector->rx.next_update = jiffies + 1;
3921 		q_vector->rx.target_itr =
3922 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3923 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3924 		     q_vector->rx.target_itr >> 1);
3925 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3926 
3927 		q_vector->tx.next_update = jiffies + 1;
3928 		q_vector->tx.target_itr =
3929 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3930 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3931 		     q_vector->tx.target_itr >> 1);
3932 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3933 
3934 		/* Set ITR for software interrupts triggered after exiting
3935 		 * busy-loop polling.
3936 		 */
3937 		wr32(hw, I40E_PFINT_ITRN(I40E_SW_ITR, vector - 1),
3938 		     I40E_ITR_20K);
3939 
3940 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3941 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3942 
3943 		/* begin of linked list for RX queue assigned to this vector */
3944 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3945 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3946 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3947 			u32 val;
3948 
3949 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3950 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3951 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3952 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3953 			      (I40E_QUEUE_TYPE_TX <<
3954 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3955 
3956 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3957 
3958 			if (has_xdp) {
3959 				/* TX queue with next queue set to TX */
3960 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3961 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3962 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3963 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3964 				      (I40E_QUEUE_TYPE_TX <<
3965 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3966 
3967 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3968 			}
3969 			/* TX queue with next RX or end of linked list */
3970 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3971 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3972 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3973 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3974 			      (I40E_QUEUE_TYPE_RX <<
3975 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3976 
3977 			/* Terminate the linked list */
3978 			if (q == (q_vector->num_ringpairs - 1))
3979 				val |= (I40E_QUEUE_END_OF_LIST <<
3980 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3981 
3982 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3983 			qp++;
3984 		}
3985 	}
3986 
3987 	i40e_flush(hw);
3988 }
3989 
3990 /**
3991  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3992  * @pf: pointer to private device data structure
3993  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3994 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3995 {
3996 	struct i40e_hw *hw = &pf->hw;
3997 	u32 val;
3998 
3999 	/* clear things first */
4000 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
4001 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
4002 
4003 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
4004 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
4005 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
4006 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
4007 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
4008 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
4009 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
4010 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4011 
4012 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
4013 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4014 
4015 	if (pf->flags & I40E_FLAG_PTP)
4016 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4017 
4018 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
4019 
4020 	/* SW_ITR_IDX = 0, but don't change INTENA */
4021 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
4022 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
4023 
4024 	/* OTHER_ITR_IDX = 0 */
4025 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
4026 }
4027 
4028 /**
4029  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
4030  * @vsi: the VSI being configured
4031  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)4032 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
4033 {
4034 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
4035 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4036 	struct i40e_pf *pf = vsi->back;
4037 	struct i40e_hw *hw = &pf->hw;
4038 
4039 	/* set the ITR configuration */
4040 	q_vector->rx.next_update = jiffies + 1;
4041 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4042 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4043 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4044 	q_vector->tx.next_update = jiffies + 1;
4045 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4046 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4047 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4048 
4049 	i40e_enable_misc_int_causes(pf);
4050 
4051 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4052 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4053 
4054 	/* Associate the queue pair to the vector and enable the queue
4055 	 * interrupt RX queue in linked list with next queue set to TX
4056 	 */
4057 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4058 
4059 	if (i40e_enabled_xdp_vsi(vsi)) {
4060 		/* TX queue in linked list with next queue set to TX */
4061 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4062 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4063 	}
4064 
4065 	/* last TX queue so the next RX queue doesn't matter */
4066 	wr32(hw, I40E_QINT_TQCTL(0),
4067 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4068 	i40e_flush(hw);
4069 }
4070 
4071 /**
4072  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4073  * @pf: board private structure
4074  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)4075 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4076 {
4077 	struct i40e_hw *hw = &pf->hw;
4078 
4079 	wr32(hw, I40E_PFINT_DYN_CTL0,
4080 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4081 	i40e_flush(hw);
4082 }
4083 
4084 /**
4085  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4086  * @pf: board private structure
4087  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)4088 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4089 {
4090 	struct i40e_hw *hw = &pf->hw;
4091 	u32 val;
4092 
4093 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4094 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4095 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4096 
4097 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4098 	i40e_flush(hw);
4099 }
4100 
4101 /**
4102  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4103  * @irq: interrupt number
4104  * @data: pointer to a q_vector
4105  **/
i40e_msix_clean_rings(int irq,void * data)4106 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4107 {
4108 	struct i40e_q_vector *q_vector = data;
4109 
4110 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4111 		return IRQ_HANDLED;
4112 
4113 	napi_schedule_irqoff(&q_vector->napi);
4114 
4115 	return IRQ_HANDLED;
4116 }
4117 
4118 /**
4119  * i40e_irq_affinity_notify - Callback for affinity changes
4120  * @notify: context as to what irq was changed
4121  * @mask: the new affinity mask
4122  *
4123  * This is a callback function used by the irq_set_affinity_notifier function
4124  * so that we may register to receive changes to the irq affinity masks.
4125  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)4126 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4127 				     const cpumask_t *mask)
4128 {
4129 	struct i40e_q_vector *q_vector =
4130 		container_of(notify, struct i40e_q_vector, affinity_notify);
4131 
4132 	cpumask_copy(&q_vector->affinity_mask, mask);
4133 }
4134 
4135 /**
4136  * i40e_irq_affinity_release - Callback for affinity notifier release
4137  * @ref: internal core kernel usage
4138  *
4139  * This is a callback function used by the irq_set_affinity_notifier function
4140  * to inform the current notification subscriber that they will no longer
4141  * receive notifications.
4142  **/
i40e_irq_affinity_release(struct kref * ref)4143 static void i40e_irq_affinity_release(struct kref *ref) {}
4144 
4145 /**
4146  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4147  * @vsi: the VSI being configured
4148  * @basename: name for the vector
4149  *
4150  * Allocates MSI-X vectors and requests interrupts from the kernel.
4151  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)4152 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4153 {
4154 	int q_vectors = vsi->num_q_vectors;
4155 	struct i40e_pf *pf = vsi->back;
4156 	int base = vsi->base_vector;
4157 	int rx_int_idx = 0;
4158 	int tx_int_idx = 0;
4159 	int vector, err;
4160 	int irq_num;
4161 	int cpu;
4162 
4163 	for (vector = 0; vector < q_vectors; vector++) {
4164 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4165 
4166 		irq_num = pf->msix_entries[base + vector].vector;
4167 
4168 		if (q_vector->tx.ring && q_vector->rx.ring) {
4169 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4170 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4171 			tx_int_idx++;
4172 		} else if (q_vector->rx.ring) {
4173 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4174 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4175 		} else if (q_vector->tx.ring) {
4176 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4177 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4178 		} else {
4179 			/* skip this unused q_vector */
4180 			continue;
4181 		}
4182 		err = request_irq(irq_num,
4183 				  vsi->irq_handler,
4184 				  0,
4185 				  q_vector->name,
4186 				  q_vector);
4187 		if (err) {
4188 			dev_info(&pf->pdev->dev,
4189 				 "MSIX request_irq failed, error: %d\n", err);
4190 			goto free_queue_irqs;
4191 		}
4192 
4193 		/* register for affinity change notifications */
4194 		q_vector->irq_num = irq_num;
4195 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4196 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4197 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4198 		/* Spread affinity hints out across online CPUs.
4199 		 *
4200 		 * get_cpu_mask returns a static constant mask with
4201 		 * a permanent lifetime so it's ok to pass to
4202 		 * irq_update_affinity_hint without making a copy.
4203 		 */
4204 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4205 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4206 	}
4207 
4208 	vsi->irqs_ready = true;
4209 	return 0;
4210 
4211 free_queue_irqs:
4212 	while (vector) {
4213 		vector--;
4214 		irq_num = pf->msix_entries[base + vector].vector;
4215 		irq_set_affinity_notifier(irq_num, NULL);
4216 		irq_update_affinity_hint(irq_num, NULL);
4217 		free_irq(irq_num, &vsi->q_vectors[vector]);
4218 	}
4219 	return err;
4220 }
4221 
4222 /**
4223  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4224  * @vsi: the VSI being un-configured
4225  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)4226 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4227 {
4228 	struct i40e_pf *pf = vsi->back;
4229 	struct i40e_hw *hw = &pf->hw;
4230 	int base = vsi->base_vector;
4231 	int i;
4232 
4233 	/* disable interrupt causation from each queue */
4234 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4235 		u32 val;
4236 
4237 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4238 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4239 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4240 
4241 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4242 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4243 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4244 
4245 		if (!i40e_enabled_xdp_vsi(vsi))
4246 			continue;
4247 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4248 	}
4249 
4250 	/* disable each interrupt */
4251 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4252 		for (i = vsi->base_vector;
4253 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4254 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4255 
4256 		i40e_flush(hw);
4257 		for (i = 0; i < vsi->num_q_vectors; i++)
4258 			synchronize_irq(pf->msix_entries[i + base].vector);
4259 	} else {
4260 		/* Legacy and MSI mode - this stops all interrupt handling */
4261 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4262 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4263 		i40e_flush(hw);
4264 		synchronize_irq(pf->pdev->irq);
4265 	}
4266 }
4267 
4268 /**
4269  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4270  * @vsi: the VSI being configured
4271  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)4272 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4273 {
4274 	struct i40e_pf *pf = vsi->back;
4275 	int i;
4276 
4277 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4278 		for (i = 0; i < vsi->num_q_vectors; i++)
4279 			i40e_irq_dynamic_enable(vsi, i);
4280 	} else {
4281 		i40e_irq_dynamic_enable_icr0(pf);
4282 	}
4283 
4284 	i40e_flush(&pf->hw);
4285 	return 0;
4286 }
4287 
4288 /**
4289  * i40e_free_misc_vector - Free the vector that handles non-queue events
4290  * @pf: board private structure
4291  **/
i40e_free_misc_vector(struct i40e_pf * pf)4292 static void i40e_free_misc_vector(struct i40e_pf *pf)
4293 {
4294 	/* Disable ICR 0 */
4295 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4296 	i40e_flush(&pf->hw);
4297 
4298 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4299 		free_irq(pf->msix_entries[0].vector, pf);
4300 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4301 	}
4302 }
4303 
4304 /**
4305  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4306  * @irq: interrupt number
4307  * @data: pointer to a q_vector
4308  *
4309  * This is the handler used for all MSI/Legacy interrupts, and deals
4310  * with both queue and non-queue interrupts.  This is also used in
4311  * MSIX mode to handle the non-queue interrupts.
4312  **/
i40e_intr(int irq,void * data)4313 static irqreturn_t i40e_intr(int irq, void *data)
4314 {
4315 	struct i40e_pf *pf = (struct i40e_pf *)data;
4316 	struct i40e_hw *hw = &pf->hw;
4317 	irqreturn_t ret = IRQ_NONE;
4318 	u32 icr0, icr0_remaining;
4319 	u32 val, ena_mask;
4320 
4321 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4322 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4323 
4324 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4325 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4326 		goto enable_intr;
4327 
4328 	/* if interrupt but no bits showing, must be SWINT */
4329 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4330 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4331 		pf->sw_int_count++;
4332 
4333 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4334 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4335 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4336 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4337 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4338 	}
4339 
4340 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4341 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4342 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4343 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4344 
4345 		/* We do not have a way to disarm Queue causes while leaving
4346 		 * interrupt enabled for all other causes, ideally
4347 		 * interrupt should be disabled while we are in NAPI but
4348 		 * this is not a performance path and napi_schedule()
4349 		 * can deal with rescheduling.
4350 		 */
4351 		if (!test_bit(__I40E_DOWN, pf->state))
4352 			napi_schedule_irqoff(&q_vector->napi);
4353 	}
4354 
4355 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4356 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4357 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4358 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4359 	}
4360 
4361 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4362 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4363 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4364 	}
4365 
4366 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4367 		/* disable any further VFLR event notifications */
4368 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4369 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4370 
4371 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4372 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4373 		} else {
4374 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4375 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4376 		}
4377 	}
4378 
4379 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4380 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4381 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4382 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4383 		val = rd32(hw, I40E_GLGEN_RSTAT);
4384 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4385 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4386 		if (val == I40E_RESET_CORER) {
4387 			pf->corer_count++;
4388 		} else if (val == I40E_RESET_GLOBR) {
4389 			pf->globr_count++;
4390 		} else if (val == I40E_RESET_EMPR) {
4391 			pf->empr_count++;
4392 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4393 		}
4394 	}
4395 
4396 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4397 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4398 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4399 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4400 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4401 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4402 	}
4403 
4404 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4405 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4406 
4407 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4408 			schedule_work(&pf->ptp_extts0_work);
4409 
4410 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4411 			i40e_ptp_tx_hwtstamp(pf);
4412 
4413 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4414 	}
4415 
4416 	/* If a critical error is pending we have no choice but to reset the
4417 	 * device.
4418 	 * Report and mask out any remaining unexpected interrupts.
4419 	 */
4420 	icr0_remaining = icr0 & ena_mask;
4421 	if (icr0_remaining) {
4422 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4423 			 icr0_remaining);
4424 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4425 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4426 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4427 			dev_info(&pf->pdev->dev, "device will be reset\n");
4428 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4429 			i40e_service_event_schedule(pf);
4430 		}
4431 		ena_mask &= ~icr0_remaining;
4432 	}
4433 	ret = IRQ_HANDLED;
4434 
4435 enable_intr:
4436 	/* re-enable interrupt causes */
4437 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4438 	if (!test_bit(__I40E_DOWN, pf->state) ||
4439 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4440 		i40e_service_event_schedule(pf);
4441 		i40e_irq_dynamic_enable_icr0(pf);
4442 	}
4443 
4444 	return ret;
4445 }
4446 
4447 /**
4448  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4449  * @tx_ring:  tx ring to clean
4450  * @budget:   how many cleans we're allowed
4451  *
4452  * Returns true if there's any budget left (e.g. the clean is finished)
4453  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4454 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4455 {
4456 	struct i40e_vsi *vsi = tx_ring->vsi;
4457 	u16 i = tx_ring->next_to_clean;
4458 	struct i40e_tx_buffer *tx_buf;
4459 	struct i40e_tx_desc *tx_desc;
4460 
4461 	tx_buf = &tx_ring->tx_bi[i];
4462 	tx_desc = I40E_TX_DESC(tx_ring, i);
4463 	i -= tx_ring->count;
4464 
4465 	do {
4466 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4467 
4468 		/* if next_to_watch is not set then there is no work pending */
4469 		if (!eop_desc)
4470 			break;
4471 
4472 		/* prevent any other reads prior to eop_desc */
4473 		smp_rmb();
4474 
4475 		/* if the descriptor isn't done, no work yet to do */
4476 		if (!(eop_desc->cmd_type_offset_bsz &
4477 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4478 			break;
4479 
4480 		/* clear next_to_watch to prevent false hangs */
4481 		tx_buf->next_to_watch = NULL;
4482 
4483 		tx_desc->buffer_addr = 0;
4484 		tx_desc->cmd_type_offset_bsz = 0;
4485 		/* move past filter desc */
4486 		tx_buf++;
4487 		tx_desc++;
4488 		i++;
4489 		if (unlikely(!i)) {
4490 			i -= tx_ring->count;
4491 			tx_buf = tx_ring->tx_bi;
4492 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4493 		}
4494 		/* unmap skb header data */
4495 		dma_unmap_single(tx_ring->dev,
4496 				 dma_unmap_addr(tx_buf, dma),
4497 				 dma_unmap_len(tx_buf, len),
4498 				 DMA_TO_DEVICE);
4499 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4500 			kfree(tx_buf->raw_buf);
4501 
4502 		tx_buf->raw_buf = NULL;
4503 		tx_buf->tx_flags = 0;
4504 		tx_buf->next_to_watch = NULL;
4505 		dma_unmap_len_set(tx_buf, len, 0);
4506 		tx_desc->buffer_addr = 0;
4507 		tx_desc->cmd_type_offset_bsz = 0;
4508 
4509 		/* move us past the eop_desc for start of next FD desc */
4510 		tx_buf++;
4511 		tx_desc++;
4512 		i++;
4513 		if (unlikely(!i)) {
4514 			i -= tx_ring->count;
4515 			tx_buf = tx_ring->tx_bi;
4516 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4517 		}
4518 
4519 		/* update budget accounting */
4520 		budget--;
4521 	} while (likely(budget));
4522 
4523 	i += tx_ring->count;
4524 	tx_ring->next_to_clean = i;
4525 
4526 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4527 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4528 
4529 	return budget > 0;
4530 }
4531 
4532 /**
4533  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4534  * @irq: interrupt number
4535  * @data: pointer to a q_vector
4536  **/
i40e_fdir_clean_ring(int irq,void * data)4537 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4538 {
4539 	struct i40e_q_vector *q_vector = data;
4540 	struct i40e_vsi *vsi;
4541 
4542 	if (!q_vector->tx.ring)
4543 		return IRQ_HANDLED;
4544 
4545 	vsi = q_vector->tx.ring->vsi;
4546 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4547 
4548 	return IRQ_HANDLED;
4549 }
4550 
4551 /**
4552  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4553  * @vsi: the VSI being configured
4554  * @v_idx: vector index
4555  * @qp_idx: queue pair index
4556  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4557 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4558 {
4559 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4560 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4561 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4562 
4563 	tx_ring->q_vector = q_vector;
4564 	tx_ring->next = q_vector->tx.ring;
4565 	q_vector->tx.ring = tx_ring;
4566 	q_vector->tx.count++;
4567 
4568 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4569 	if (i40e_enabled_xdp_vsi(vsi)) {
4570 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4571 
4572 		xdp_ring->q_vector = q_vector;
4573 		xdp_ring->next = q_vector->tx.ring;
4574 		q_vector->tx.ring = xdp_ring;
4575 		q_vector->tx.count++;
4576 	}
4577 
4578 	rx_ring->q_vector = q_vector;
4579 	rx_ring->next = q_vector->rx.ring;
4580 	q_vector->rx.ring = rx_ring;
4581 	q_vector->rx.count++;
4582 }
4583 
4584 /**
4585  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4586  * @vsi: the VSI being configured
4587  *
4588  * This function maps descriptor rings to the queue-specific vectors
4589  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4590  * one vector per queue pair, but on a constrained vector budget, we
4591  * group the queue pairs as "efficiently" as possible.
4592  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4593 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4594 {
4595 	int qp_remaining = vsi->num_queue_pairs;
4596 	int q_vectors = vsi->num_q_vectors;
4597 	int num_ringpairs;
4598 	int v_start = 0;
4599 	int qp_idx = 0;
4600 
4601 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4602 	 * group them so there are multiple queues per vector.
4603 	 * It is also important to go through all the vectors available to be
4604 	 * sure that if we don't use all the vectors, that the remaining vectors
4605 	 * are cleared. This is especially important when decreasing the
4606 	 * number of queues in use.
4607 	 */
4608 	for (; v_start < q_vectors; v_start++) {
4609 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4610 
4611 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4612 
4613 		q_vector->num_ringpairs = num_ringpairs;
4614 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4615 
4616 		q_vector->rx.count = 0;
4617 		q_vector->tx.count = 0;
4618 		q_vector->rx.ring = NULL;
4619 		q_vector->tx.ring = NULL;
4620 
4621 		while (num_ringpairs--) {
4622 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4623 			qp_idx++;
4624 			qp_remaining--;
4625 		}
4626 	}
4627 }
4628 
4629 /**
4630  * i40e_vsi_request_irq - Request IRQ from the OS
4631  * @vsi: the VSI being configured
4632  * @basename: name for the vector
4633  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4634 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4635 {
4636 	struct i40e_pf *pf = vsi->back;
4637 	int err;
4638 
4639 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4640 		err = i40e_vsi_request_irq_msix(vsi, basename);
4641 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4642 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4643 				  pf->int_name, pf);
4644 	else
4645 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4646 				  pf->int_name, pf);
4647 
4648 	if (err)
4649 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4650 
4651 	return err;
4652 }
4653 
4654 #ifdef CONFIG_NET_POLL_CONTROLLER
4655 /**
4656  * i40e_netpoll - A Polling 'interrupt' handler
4657  * @netdev: network interface device structure
4658  *
4659  * This is used by netconsole to send skbs without having to re-enable
4660  * interrupts.  It's not called while the normal interrupt routine is executing.
4661  **/
i40e_netpoll(struct net_device * netdev)4662 static void i40e_netpoll(struct net_device *netdev)
4663 {
4664 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4665 	struct i40e_vsi *vsi = np->vsi;
4666 	struct i40e_pf *pf = vsi->back;
4667 	int i;
4668 
4669 	/* if interface is down do nothing */
4670 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4671 		return;
4672 
4673 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4674 		for (i = 0; i < vsi->num_q_vectors; i++)
4675 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4676 	} else {
4677 		i40e_intr(pf->pdev->irq, netdev);
4678 	}
4679 }
4680 #endif
4681 
4682 #define I40E_QTX_ENA_WAIT_COUNT 50
4683 
4684 /**
4685  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4686  * @pf: the PF being configured
4687  * @pf_q: the PF queue
4688  * @enable: enable or disable state of the queue
4689  *
4690  * This routine will wait for the given Tx queue of the PF to reach the
4691  * enabled or disabled state.
4692  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4693  * multiple retries; else will return 0 in case of success.
4694  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4695 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4696 {
4697 	int i;
4698 	u32 tx_reg;
4699 
4700 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4701 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4702 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4703 			break;
4704 
4705 		usleep_range(10, 20);
4706 	}
4707 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4708 		return -ETIMEDOUT;
4709 
4710 	return 0;
4711 }
4712 
4713 /**
4714  * i40e_control_tx_q - Start or stop a particular Tx queue
4715  * @pf: the PF structure
4716  * @pf_q: the PF queue to configure
4717  * @enable: start or stop the queue
4718  *
4719  * This function enables or disables a single queue. Note that any delay
4720  * required after the operation is expected to be handled by the caller of
4721  * this function.
4722  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4723 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4724 {
4725 	struct i40e_hw *hw = &pf->hw;
4726 	u32 tx_reg;
4727 	int i;
4728 
4729 	/* warn the TX unit of coming changes */
4730 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4731 	if (!enable)
4732 		usleep_range(10, 20);
4733 
4734 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4735 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4736 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4737 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4738 			break;
4739 		usleep_range(1000, 2000);
4740 	}
4741 
4742 	/* Skip if the queue is already in the requested state */
4743 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4744 		return;
4745 
4746 	/* turn on/off the queue */
4747 	if (enable) {
4748 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4749 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4750 	} else {
4751 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4752 	}
4753 
4754 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4755 }
4756 
4757 /**
4758  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4759  * @seid: VSI SEID
4760  * @pf: the PF structure
4761  * @pf_q: the PF queue to configure
4762  * @is_xdp: true if the queue is used for XDP
4763  * @enable: start or stop the queue
4764  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4765 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4766 			   bool is_xdp, bool enable)
4767 {
4768 	int ret;
4769 
4770 	i40e_control_tx_q(pf, pf_q, enable);
4771 
4772 	/* wait for the change to finish */
4773 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4774 	if (ret) {
4775 		dev_info(&pf->pdev->dev,
4776 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4777 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4778 			 (enable ? "en" : "dis"));
4779 	}
4780 
4781 	return ret;
4782 }
4783 
4784 /**
4785  * i40e_vsi_enable_tx - Start a VSI's rings
4786  * @vsi: the VSI being configured
4787  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4788 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4789 {
4790 	struct i40e_pf *pf = vsi->back;
4791 	int i, pf_q, ret = 0;
4792 
4793 	pf_q = vsi->base_queue;
4794 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4795 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4796 					     pf_q,
4797 					     false /*is xdp*/, true);
4798 		if (ret)
4799 			break;
4800 
4801 		if (!i40e_enabled_xdp_vsi(vsi))
4802 			continue;
4803 
4804 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4805 					     pf_q + vsi->alloc_queue_pairs,
4806 					     true /*is xdp*/, true);
4807 		if (ret)
4808 			break;
4809 	}
4810 	return ret;
4811 }
4812 
4813 /**
4814  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4815  * @pf: the PF being configured
4816  * @pf_q: the PF queue
4817  * @enable: enable or disable state of the queue
4818  *
4819  * This routine will wait for the given Rx queue of the PF to reach the
4820  * enabled or disabled state.
4821  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4822  * multiple retries; else will return 0 in case of success.
4823  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4824 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4825 {
4826 	int i;
4827 	u32 rx_reg;
4828 
4829 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4830 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4831 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4832 			break;
4833 
4834 		usleep_range(10, 20);
4835 	}
4836 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4837 		return -ETIMEDOUT;
4838 
4839 	return 0;
4840 }
4841 
4842 /**
4843  * i40e_control_rx_q - Start or stop a particular Rx queue
4844  * @pf: the PF structure
4845  * @pf_q: the PF queue to configure
4846  * @enable: start or stop the queue
4847  *
4848  * This function enables or disables a single queue. Note that
4849  * any delay required after the operation is expected to be
4850  * handled by the caller of this function.
4851  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4852 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4853 {
4854 	struct i40e_hw *hw = &pf->hw;
4855 	u32 rx_reg;
4856 	int i;
4857 
4858 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4859 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4860 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4861 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4862 			break;
4863 		usleep_range(1000, 2000);
4864 	}
4865 
4866 	/* Skip if the queue is already in the requested state */
4867 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4868 		return;
4869 
4870 	/* turn on/off the queue */
4871 	if (enable)
4872 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4873 	else
4874 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4875 
4876 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4877 }
4878 
4879 /**
4880  * i40e_control_wait_rx_q
4881  * @pf: the PF structure
4882  * @pf_q: queue being configured
4883  * @enable: start or stop the rings
4884  *
4885  * This function enables or disables a single queue along with waiting
4886  * for the change to finish. The caller of this function should handle
4887  * the delays needed in the case of disabling queues.
4888  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4889 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4890 {
4891 	int ret = 0;
4892 
4893 	i40e_control_rx_q(pf, pf_q, enable);
4894 
4895 	/* wait for the change to finish */
4896 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4897 	if (ret)
4898 		return ret;
4899 
4900 	return ret;
4901 }
4902 
4903 /**
4904  * i40e_vsi_enable_rx - Start a VSI's rings
4905  * @vsi: the VSI being configured
4906  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4907 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4908 {
4909 	struct i40e_pf *pf = vsi->back;
4910 	int i, pf_q, ret = 0;
4911 
4912 	pf_q = vsi->base_queue;
4913 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4914 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4915 		if (ret) {
4916 			dev_info(&pf->pdev->dev,
4917 				 "VSI seid %d Rx ring %d enable timeout\n",
4918 				 vsi->seid, pf_q);
4919 			break;
4920 		}
4921 	}
4922 
4923 	return ret;
4924 }
4925 
4926 /**
4927  * i40e_vsi_start_rings - Start a VSI's rings
4928  * @vsi: the VSI being configured
4929  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4930 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4931 {
4932 	int ret = 0;
4933 
4934 	/* do rx first for enable and last for disable */
4935 	ret = i40e_vsi_enable_rx(vsi);
4936 	if (ret)
4937 		return ret;
4938 	ret = i40e_vsi_enable_tx(vsi);
4939 
4940 	return ret;
4941 }
4942 
4943 #define I40E_DISABLE_TX_GAP_MSEC	50
4944 
4945 /**
4946  * i40e_vsi_stop_rings - Stop a VSI's rings
4947  * @vsi: the VSI being configured
4948  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4949 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4950 {
4951 	struct i40e_pf *pf = vsi->back;
4952 	int pf_q, err, q_end;
4953 
4954 	/* When port TX is suspended, don't wait */
4955 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4956 		return i40e_vsi_stop_rings_no_wait(vsi);
4957 
4958 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4959 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4960 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4961 
4962 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4963 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4964 		if (err)
4965 			dev_info(&pf->pdev->dev,
4966 				 "VSI seid %d Rx ring %d disable timeout\n",
4967 				 vsi->seid, pf_q);
4968 	}
4969 
4970 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4971 	pf_q = vsi->base_queue;
4972 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4973 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4974 
4975 	i40e_vsi_wait_queues_disabled(vsi);
4976 }
4977 
4978 /**
4979  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4980  * @vsi: the VSI being shutdown
4981  *
4982  * This function stops all the rings for a VSI but does not delay to verify
4983  * that rings have been disabled. It is expected that the caller is shutting
4984  * down multiple VSIs at once and will delay together for all the VSIs after
4985  * initiating the shutdown. This is particularly useful for shutting down lots
4986  * of VFs together. Otherwise, a large delay can be incurred while configuring
4987  * each VSI in serial.
4988  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4989 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4990 {
4991 	struct i40e_pf *pf = vsi->back;
4992 	int i, pf_q;
4993 
4994 	pf_q = vsi->base_queue;
4995 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4996 		i40e_control_tx_q(pf, pf_q, false);
4997 		i40e_control_rx_q(pf, pf_q, false);
4998 	}
4999 }
5000 
5001 /**
5002  * i40e_vsi_free_irq - Free the irq association with the OS
5003  * @vsi: the VSI being configured
5004  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)5005 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
5006 {
5007 	struct i40e_pf *pf = vsi->back;
5008 	struct i40e_hw *hw = &pf->hw;
5009 	int base = vsi->base_vector;
5010 	u32 val, qp;
5011 	int i;
5012 
5013 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5014 		if (!vsi->q_vectors)
5015 			return;
5016 
5017 		if (!vsi->irqs_ready)
5018 			return;
5019 
5020 		vsi->irqs_ready = false;
5021 		for (i = 0; i < vsi->num_q_vectors; i++) {
5022 			int irq_num;
5023 			u16 vector;
5024 
5025 			vector = i + base;
5026 			irq_num = pf->msix_entries[vector].vector;
5027 
5028 			/* free only the irqs that were actually requested */
5029 			if (!vsi->q_vectors[i] ||
5030 			    !vsi->q_vectors[i]->num_ringpairs)
5031 				continue;
5032 
5033 			/* clear the affinity notifier in the IRQ descriptor */
5034 			irq_set_affinity_notifier(irq_num, NULL);
5035 			/* remove our suggested affinity mask for this IRQ */
5036 			irq_update_affinity_hint(irq_num, NULL);
5037 			free_irq(irq_num, vsi->q_vectors[i]);
5038 
5039 			/* Tear down the interrupt queue link list
5040 			 *
5041 			 * We know that they come in pairs and always
5042 			 * the Rx first, then the Tx.  To clear the
5043 			 * link list, stick the EOL value into the
5044 			 * next_q field of the registers.
5045 			 */
5046 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5047 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5048 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5049 			val |= I40E_QUEUE_END_OF_LIST
5050 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5051 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5052 
5053 			while (qp != I40E_QUEUE_END_OF_LIST) {
5054 				u32 next;
5055 
5056 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5057 
5058 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5059 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5060 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5061 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5062 
5063 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5064 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5065 
5066 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5067 
5068 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5069 
5070 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5071 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5072 
5073 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5074 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5075 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5076 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5077 
5078 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5079 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5080 
5081 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5082 				qp = next;
5083 			}
5084 		}
5085 	} else {
5086 		free_irq(pf->pdev->irq, pf);
5087 
5088 		val = rd32(hw, I40E_PFINT_LNKLST0);
5089 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5090 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5091 		val |= I40E_QUEUE_END_OF_LIST
5092 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5093 		wr32(hw, I40E_PFINT_LNKLST0, val);
5094 
5095 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5096 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5097 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5098 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5099 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5100 
5101 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5102 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5103 
5104 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5105 
5106 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5107 
5108 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5109 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5110 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5111 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5112 
5113 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5114 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5115 
5116 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5117 	}
5118 }
5119 
5120 /**
5121  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5122  * @vsi: the VSI being configured
5123  * @v_idx: Index of vector to be freed
5124  *
5125  * This function frees the memory allocated to the q_vector.  In addition if
5126  * NAPI is enabled it will delete any references to the NAPI struct prior
5127  * to freeing the q_vector.
5128  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)5129 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5130 {
5131 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5132 	struct i40e_ring *ring;
5133 
5134 	if (!q_vector)
5135 		return;
5136 
5137 	/* disassociate q_vector from rings */
5138 	i40e_for_each_ring(ring, q_vector->tx)
5139 		ring->q_vector = NULL;
5140 
5141 	i40e_for_each_ring(ring, q_vector->rx)
5142 		ring->q_vector = NULL;
5143 
5144 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5145 	if (vsi->netdev)
5146 		netif_napi_del(&q_vector->napi);
5147 
5148 	vsi->q_vectors[v_idx] = NULL;
5149 
5150 	kfree_rcu(q_vector, rcu);
5151 }
5152 
5153 /**
5154  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5155  * @vsi: the VSI being un-configured
5156  *
5157  * This frees the memory allocated to the q_vectors and
5158  * deletes references to the NAPI struct.
5159  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)5160 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5161 {
5162 	int v_idx;
5163 
5164 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5165 		i40e_free_q_vector(vsi, v_idx);
5166 }
5167 
5168 /**
5169  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5170  * @pf: board private structure
5171  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)5172 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5173 {
5174 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5175 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5176 		pci_disable_msix(pf->pdev);
5177 		kfree(pf->msix_entries);
5178 		pf->msix_entries = NULL;
5179 		kfree(pf->irq_pile);
5180 		pf->irq_pile = NULL;
5181 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5182 		pci_disable_msi(pf->pdev);
5183 	}
5184 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5185 }
5186 
5187 /**
5188  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5189  * @pf: board private structure
5190  *
5191  * We go through and clear interrupt specific resources and reset the structure
5192  * to pre-load conditions
5193  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)5194 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5195 {
5196 	int i;
5197 
5198 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5199 		i40e_free_misc_vector(pf);
5200 
5201 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5202 		      I40E_IWARP_IRQ_PILE_ID);
5203 
5204 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5205 	for (i = 0; i < pf->num_alloc_vsi; i++)
5206 		if (pf->vsi[i])
5207 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5208 	i40e_reset_interrupt_capability(pf);
5209 }
5210 
5211 /**
5212  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5213  * @vsi: the VSI being configured
5214  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)5215 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5216 {
5217 	int q_idx;
5218 
5219 	if (!vsi->netdev)
5220 		return;
5221 
5222 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5223 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5224 
5225 		if (q_vector->rx.ring || q_vector->tx.ring)
5226 			napi_enable(&q_vector->napi);
5227 	}
5228 }
5229 
5230 /**
5231  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5232  * @vsi: the VSI being configured
5233  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)5234 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5235 {
5236 	int q_idx;
5237 
5238 	if (!vsi->netdev)
5239 		return;
5240 
5241 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5242 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5243 
5244 		if (q_vector->rx.ring || q_vector->tx.ring)
5245 			napi_disable(&q_vector->napi);
5246 	}
5247 }
5248 
5249 /**
5250  * i40e_vsi_close - Shut down a VSI
5251  * @vsi: the vsi to be quelled
5252  **/
i40e_vsi_close(struct i40e_vsi * vsi)5253 static void i40e_vsi_close(struct i40e_vsi *vsi)
5254 {
5255 	struct i40e_pf *pf = vsi->back;
5256 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5257 		i40e_down(vsi);
5258 	i40e_vsi_free_irq(vsi);
5259 	i40e_vsi_free_tx_resources(vsi);
5260 	i40e_vsi_free_rx_resources(vsi);
5261 	vsi->current_netdev_flags = 0;
5262 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5263 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5264 		set_bit(__I40E_CLIENT_RESET, pf->state);
5265 }
5266 
5267 /**
5268  * i40e_quiesce_vsi - Pause a given VSI
5269  * @vsi: the VSI being paused
5270  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)5271 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5272 {
5273 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5274 		return;
5275 
5276 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5277 	if (vsi->netdev && netif_running(vsi->netdev))
5278 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5279 	else
5280 		i40e_vsi_close(vsi);
5281 }
5282 
5283 /**
5284  * i40e_unquiesce_vsi - Resume a given VSI
5285  * @vsi: the VSI being resumed
5286  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)5287 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5288 {
5289 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5290 		return;
5291 
5292 	if (vsi->netdev && netif_running(vsi->netdev))
5293 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5294 	else
5295 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5296 }
5297 
5298 /**
5299  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5300  * @pf: the PF
5301  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)5302 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5303 {
5304 	int v;
5305 
5306 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5307 		if (pf->vsi[v])
5308 			i40e_quiesce_vsi(pf->vsi[v]);
5309 	}
5310 }
5311 
5312 /**
5313  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5314  * @pf: the PF
5315  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5316 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5317 {
5318 	int v;
5319 
5320 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5321 		if (pf->vsi[v])
5322 			i40e_unquiesce_vsi(pf->vsi[v]);
5323 	}
5324 }
5325 
5326 /**
5327  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5328  * @vsi: the VSI being configured
5329  *
5330  * Wait until all queues on a given VSI have been disabled.
5331  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5332 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5333 {
5334 	struct i40e_pf *pf = vsi->back;
5335 	int i, pf_q, ret;
5336 
5337 	pf_q = vsi->base_queue;
5338 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5339 		/* Check and wait for the Tx queue */
5340 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5341 		if (ret) {
5342 			dev_info(&pf->pdev->dev,
5343 				 "VSI seid %d Tx ring %d disable timeout\n",
5344 				 vsi->seid, pf_q);
5345 			return ret;
5346 		}
5347 
5348 		if (!i40e_enabled_xdp_vsi(vsi))
5349 			goto wait_rx;
5350 
5351 		/* Check and wait for the XDP Tx queue */
5352 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5353 				       false);
5354 		if (ret) {
5355 			dev_info(&pf->pdev->dev,
5356 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5357 				 vsi->seid, pf_q);
5358 			return ret;
5359 		}
5360 wait_rx:
5361 		/* Check and wait for the Rx queue */
5362 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5363 		if (ret) {
5364 			dev_info(&pf->pdev->dev,
5365 				 "VSI seid %d Rx ring %d disable timeout\n",
5366 				 vsi->seid, pf_q);
5367 			return ret;
5368 		}
5369 	}
5370 
5371 	return 0;
5372 }
5373 
5374 #ifdef CONFIG_I40E_DCB
5375 /**
5376  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5377  * @pf: the PF
5378  *
5379  * This function waits for the queues to be in disabled state for all the
5380  * VSIs that are managed by this PF.
5381  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5382 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5383 {
5384 	int v, ret = 0;
5385 
5386 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5387 		if (pf->vsi[v]) {
5388 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5389 			if (ret)
5390 				break;
5391 		}
5392 	}
5393 
5394 	return ret;
5395 }
5396 
5397 #endif
5398 
5399 /**
5400  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5401  * @pf: pointer to PF
5402  *
5403  * Get TC map for ISCSI PF type that will include iSCSI TC
5404  * and LAN TC.
5405  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5406 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5407 {
5408 	struct i40e_dcb_app_priority_table app;
5409 	struct i40e_hw *hw = &pf->hw;
5410 	u8 enabled_tc = 1; /* TC0 is always enabled */
5411 	u8 tc, i;
5412 	/* Get the iSCSI APP TLV */
5413 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5414 
5415 	for (i = 0; i < dcbcfg->numapps; i++) {
5416 		app = dcbcfg->app[i];
5417 		if (app.selector == I40E_APP_SEL_TCPIP &&
5418 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5419 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5420 			enabled_tc |= BIT(tc);
5421 			break;
5422 		}
5423 	}
5424 
5425 	return enabled_tc;
5426 }
5427 
5428 /**
5429  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5430  * @dcbcfg: the corresponding DCBx configuration structure
5431  *
5432  * Return the number of TCs from given DCBx configuration
5433  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5434 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5435 {
5436 	int i, tc_unused = 0;
5437 	u8 num_tc = 0;
5438 	u8 ret = 0;
5439 
5440 	/* Scan the ETS Config Priority Table to find
5441 	 * traffic class enabled for a given priority
5442 	 * and create a bitmask of enabled TCs
5443 	 */
5444 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5445 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5446 
5447 	/* Now scan the bitmask to check for
5448 	 * contiguous TCs starting with TC0
5449 	 */
5450 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5451 		if (num_tc & BIT(i)) {
5452 			if (!tc_unused) {
5453 				ret++;
5454 			} else {
5455 				pr_err("Non-contiguous TC - Disabling DCB\n");
5456 				return 1;
5457 			}
5458 		} else {
5459 			tc_unused = 1;
5460 		}
5461 	}
5462 
5463 	/* There is always at least TC0 */
5464 	if (!ret)
5465 		ret = 1;
5466 
5467 	return ret;
5468 }
5469 
5470 /**
5471  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5472  * @dcbcfg: the corresponding DCBx configuration structure
5473  *
5474  * Query the current DCB configuration and return the number of
5475  * traffic classes enabled from the given DCBX config
5476  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5477 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5478 {
5479 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5480 	u8 enabled_tc = 1;
5481 	u8 i;
5482 
5483 	for (i = 0; i < num_tc; i++)
5484 		enabled_tc |= BIT(i);
5485 
5486 	return enabled_tc;
5487 }
5488 
5489 /**
5490  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5491  * @pf: PF being queried
5492  *
5493  * Query the current MQPRIO configuration and return the number of
5494  * traffic classes enabled.
5495  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5496 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5497 {
5498 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5499 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5500 	u8 enabled_tc = 1, i;
5501 
5502 	for (i = 1; i < num_tc; i++)
5503 		enabled_tc |= BIT(i);
5504 	return enabled_tc;
5505 }
5506 
5507 /**
5508  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5509  * @pf: PF being queried
5510  *
5511  * Return number of traffic classes enabled for the given PF
5512  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5513 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5514 {
5515 	struct i40e_hw *hw = &pf->hw;
5516 	u8 i, enabled_tc = 1;
5517 	u8 num_tc = 0;
5518 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5519 
5520 	if (i40e_is_tc_mqprio_enabled(pf))
5521 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5522 
5523 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5524 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5525 		return 1;
5526 
5527 	/* SFP mode will be enabled for all TCs on port */
5528 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5529 		return i40e_dcb_get_num_tc(dcbcfg);
5530 
5531 	/* MFP mode return count of enabled TCs for this PF */
5532 	if (pf->hw.func_caps.iscsi)
5533 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5534 	else
5535 		return 1; /* Only TC0 */
5536 
5537 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5538 		if (enabled_tc & BIT(i))
5539 			num_tc++;
5540 	}
5541 	return num_tc;
5542 }
5543 
5544 /**
5545  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5546  * @pf: PF being queried
5547  *
5548  * Return a bitmap for enabled traffic classes for this PF.
5549  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5550 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5551 {
5552 	if (i40e_is_tc_mqprio_enabled(pf))
5553 		return i40e_mqprio_get_enabled_tc(pf);
5554 
5555 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5556 	 * default TC
5557 	 */
5558 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5559 		return I40E_DEFAULT_TRAFFIC_CLASS;
5560 
5561 	/* SFP mode we want PF to be enabled for all TCs */
5562 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5563 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5564 
5565 	/* MFP enabled and iSCSI PF type */
5566 	if (pf->hw.func_caps.iscsi)
5567 		return i40e_get_iscsi_tc_map(pf);
5568 	else
5569 		return I40E_DEFAULT_TRAFFIC_CLASS;
5570 }
5571 
5572 /**
5573  * i40e_vsi_get_bw_info - Query VSI BW Information
5574  * @vsi: the VSI being queried
5575  *
5576  * Returns 0 on success, negative value on failure
5577  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5578 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5579 {
5580 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5581 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5582 	struct i40e_pf *pf = vsi->back;
5583 	struct i40e_hw *hw = &pf->hw;
5584 	u32 tc_bw_max;
5585 	int ret;
5586 	int i;
5587 
5588 	/* Get the VSI level BW configuration */
5589 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5590 	if (ret) {
5591 		dev_info(&pf->pdev->dev,
5592 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5593 			 ERR_PTR(ret),
5594 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5595 		return -EINVAL;
5596 	}
5597 
5598 	/* Get the VSI level BW configuration per TC */
5599 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5600 					       NULL);
5601 	if (ret) {
5602 		dev_info(&pf->pdev->dev,
5603 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5604 			 ERR_PTR(ret),
5605 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5606 		return -EINVAL;
5607 	}
5608 
5609 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5610 		dev_info(&pf->pdev->dev,
5611 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5612 			 bw_config.tc_valid_bits,
5613 			 bw_ets_config.tc_valid_bits);
5614 		/* Still continuing */
5615 	}
5616 
5617 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5618 	vsi->bw_max_quanta = bw_config.max_bw;
5619 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5620 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5621 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5622 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5623 		vsi->bw_ets_limit_credits[i] =
5624 					le16_to_cpu(bw_ets_config.credits[i]);
5625 		/* 3 bits out of 4 for each TC */
5626 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5627 	}
5628 
5629 	return 0;
5630 }
5631 
5632 /**
5633  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5634  * @vsi: the VSI being configured
5635  * @enabled_tc: TC bitmap
5636  * @bw_share: BW shared credits per TC
5637  *
5638  * Returns 0 on success, negative value on failure
5639  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5640 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5641 				       u8 *bw_share)
5642 {
5643 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5644 	struct i40e_pf *pf = vsi->back;
5645 	int ret;
5646 	int i;
5647 
5648 	/* There is no need to reset BW when mqprio mode is on.  */
5649 	if (i40e_is_tc_mqprio_enabled(pf))
5650 		return 0;
5651 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5652 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5653 		if (ret)
5654 			dev_info(&pf->pdev->dev,
5655 				 "Failed to reset tx rate for vsi->seid %u\n",
5656 				 vsi->seid);
5657 		return ret;
5658 	}
5659 	memset(&bw_data, 0, sizeof(bw_data));
5660 	bw_data.tc_valid_bits = enabled_tc;
5661 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5662 		bw_data.tc_bw_credits[i] = bw_share[i];
5663 
5664 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5665 	if (ret) {
5666 		dev_info(&pf->pdev->dev,
5667 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5668 			 pf->hw.aq.asq_last_status);
5669 		return -EINVAL;
5670 	}
5671 
5672 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5673 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5674 
5675 	return 0;
5676 }
5677 
5678 /**
5679  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5680  * @vsi: the VSI being configured
5681  * @enabled_tc: TC map to be enabled
5682  *
5683  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5684 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5685 {
5686 	struct net_device *netdev = vsi->netdev;
5687 	struct i40e_pf *pf = vsi->back;
5688 	struct i40e_hw *hw = &pf->hw;
5689 	u8 netdev_tc = 0;
5690 	int i;
5691 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5692 
5693 	if (!netdev)
5694 		return;
5695 
5696 	if (!enabled_tc) {
5697 		netdev_reset_tc(netdev);
5698 		return;
5699 	}
5700 
5701 	/* Set up actual enabled TCs on the VSI */
5702 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5703 		return;
5704 
5705 	/* set per TC queues for the VSI */
5706 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5707 		/* Only set TC queues for enabled tcs
5708 		 *
5709 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5710 		 * enabled_tc bitmap would be 0x00001001; the driver
5711 		 * will set the numtc for netdev as 2 that will be
5712 		 * referenced by the netdev layer as TC 0 and 1.
5713 		 */
5714 		if (vsi->tc_config.enabled_tc & BIT(i))
5715 			netdev_set_tc_queue(netdev,
5716 					vsi->tc_config.tc_info[i].netdev_tc,
5717 					vsi->tc_config.tc_info[i].qcount,
5718 					vsi->tc_config.tc_info[i].qoffset);
5719 	}
5720 
5721 	if (i40e_is_tc_mqprio_enabled(pf))
5722 		return;
5723 
5724 	/* Assign UP2TC map for the VSI */
5725 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5726 		/* Get the actual TC# for the UP */
5727 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5728 		/* Get the mapped netdev TC# for the UP */
5729 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5730 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5731 	}
5732 }
5733 
5734 /**
5735  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5736  * @vsi: the VSI being configured
5737  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5738  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5739 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5740 				      struct i40e_vsi_context *ctxt)
5741 {
5742 	/* copy just the sections touched not the entire info
5743 	 * since not all sections are valid as returned by
5744 	 * update vsi params
5745 	 */
5746 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5747 	memcpy(&vsi->info.queue_mapping,
5748 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5749 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5750 	       sizeof(vsi->info.tc_mapping));
5751 }
5752 
5753 /**
5754  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5755  * @vsi: the VSI being reconfigured
5756  * @vsi_offset: offset from main VF VSI
5757  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5758 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5759 {
5760 	struct i40e_vsi_context ctxt = {};
5761 	struct i40e_pf *pf;
5762 	struct i40e_hw *hw;
5763 	int ret;
5764 
5765 	if (!vsi)
5766 		return -EINVAL;
5767 	pf = vsi->back;
5768 	hw = &pf->hw;
5769 
5770 	ctxt.seid = vsi->seid;
5771 	ctxt.pf_num = hw->pf_id;
5772 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5773 	ctxt.uplink_seid = vsi->uplink_seid;
5774 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5775 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5776 	ctxt.info = vsi->info;
5777 
5778 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5779 				 false);
5780 	if (vsi->reconfig_rss) {
5781 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5782 				      vsi->num_queue_pairs);
5783 		ret = i40e_vsi_config_rss(vsi);
5784 		if (ret) {
5785 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5786 			return ret;
5787 		}
5788 		vsi->reconfig_rss = false;
5789 	}
5790 
5791 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5792 	if (ret) {
5793 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5794 			 ERR_PTR(ret),
5795 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5796 		return ret;
5797 	}
5798 	/* update the local VSI info with updated queue map */
5799 	i40e_vsi_update_queue_map(vsi, &ctxt);
5800 	vsi->info.valid_sections = 0;
5801 
5802 	return ret;
5803 }
5804 
5805 /**
5806  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5807  * @vsi: VSI to be configured
5808  * @enabled_tc: TC bitmap
5809  *
5810  * This configures a particular VSI for TCs that are mapped to the
5811  * given TC bitmap. It uses default bandwidth share for TCs across
5812  * VSIs to configure TC for a particular VSI.
5813  *
5814  * NOTE:
5815  * It is expected that the VSI queues have been quisced before calling
5816  * this function.
5817  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5818 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5819 {
5820 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5821 	struct i40e_pf *pf = vsi->back;
5822 	struct i40e_hw *hw = &pf->hw;
5823 	struct i40e_vsi_context ctxt;
5824 	int ret = 0;
5825 	int i;
5826 
5827 	/* Check if enabled_tc is same as existing or new TCs */
5828 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5829 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5830 		return ret;
5831 
5832 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5833 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5834 		if (enabled_tc & BIT(i))
5835 			bw_share[i] = 1;
5836 	}
5837 
5838 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5839 	if (ret) {
5840 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5841 
5842 		dev_info(&pf->pdev->dev,
5843 			 "Failed configuring TC map %d for VSI %d\n",
5844 			 enabled_tc, vsi->seid);
5845 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5846 						  &bw_config, NULL);
5847 		if (ret) {
5848 			dev_info(&pf->pdev->dev,
5849 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5850 				 ERR_PTR(ret),
5851 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5852 			goto out;
5853 		}
5854 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5855 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5856 
5857 			if (!valid_tc)
5858 				valid_tc = bw_config.tc_valid_bits;
5859 			/* Always enable TC0, no matter what */
5860 			valid_tc |= 1;
5861 			dev_info(&pf->pdev->dev,
5862 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5863 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5864 			enabled_tc = valid_tc;
5865 		}
5866 
5867 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5868 		if (ret) {
5869 			dev_err(&pf->pdev->dev,
5870 				"Unable to  configure TC map %d for VSI %d\n",
5871 				enabled_tc, vsi->seid);
5872 			goto out;
5873 		}
5874 	}
5875 
5876 	/* Update Queue Pairs Mapping for currently enabled UPs */
5877 	ctxt.seid = vsi->seid;
5878 	ctxt.pf_num = vsi->back->hw.pf_id;
5879 	ctxt.vf_num = 0;
5880 	ctxt.uplink_seid = vsi->uplink_seid;
5881 	ctxt.info = vsi->info;
5882 	if (i40e_is_tc_mqprio_enabled(pf)) {
5883 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5884 		if (ret)
5885 			goto out;
5886 	} else {
5887 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5888 	}
5889 
5890 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5891 	 * queues changed.
5892 	 */
5893 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5894 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5895 				      vsi->num_queue_pairs);
5896 		ret = i40e_vsi_config_rss(vsi);
5897 		if (ret) {
5898 			dev_info(&vsi->back->pdev->dev,
5899 				 "Failed to reconfig rss for num_queues\n");
5900 			return ret;
5901 		}
5902 		vsi->reconfig_rss = false;
5903 	}
5904 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5905 		ctxt.info.valid_sections |=
5906 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5907 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5908 	}
5909 
5910 	/* Update the VSI after updating the VSI queue-mapping
5911 	 * information
5912 	 */
5913 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5914 	if (ret) {
5915 		dev_info(&pf->pdev->dev,
5916 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5917 			 ERR_PTR(ret),
5918 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5919 		goto out;
5920 	}
5921 	/* update the local VSI info with updated queue map */
5922 	i40e_vsi_update_queue_map(vsi, &ctxt);
5923 	vsi->info.valid_sections = 0;
5924 
5925 	/* Update current VSI BW information */
5926 	ret = i40e_vsi_get_bw_info(vsi);
5927 	if (ret) {
5928 		dev_info(&pf->pdev->dev,
5929 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5930 			 ERR_PTR(ret),
5931 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5932 		goto out;
5933 	}
5934 
5935 	/* Update the netdev TC setup */
5936 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5937 out:
5938 	return ret;
5939 }
5940 
5941 /**
5942  * i40e_get_link_speed - Returns link speed for the interface
5943  * @vsi: VSI to be configured
5944  *
5945  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5946 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5947 {
5948 	struct i40e_pf *pf = vsi->back;
5949 
5950 	switch (pf->hw.phy.link_info.link_speed) {
5951 	case I40E_LINK_SPEED_40GB:
5952 		return 40000;
5953 	case I40E_LINK_SPEED_25GB:
5954 		return 25000;
5955 	case I40E_LINK_SPEED_20GB:
5956 		return 20000;
5957 	case I40E_LINK_SPEED_10GB:
5958 		return 10000;
5959 	case I40E_LINK_SPEED_1GB:
5960 		return 1000;
5961 	default:
5962 		return -EINVAL;
5963 	}
5964 }
5965 
5966 /**
5967  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5968  * @vsi: Pointer to vsi structure
5969  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5970  *
5971  * Helper function to convert units before send to set BW limit
5972  **/
i40e_bw_bytes_to_mbits(struct i40e_vsi * vsi,u64 max_tx_rate)5973 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5974 {
5975 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5976 		dev_warn(&vsi->back->pdev->dev,
5977 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5978 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5979 	} else {
5980 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5981 	}
5982 
5983 	return max_tx_rate;
5984 }
5985 
5986 /**
5987  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5988  * @vsi: VSI to be configured
5989  * @seid: seid of the channel/VSI
5990  * @max_tx_rate: max TX rate to be configured as BW limit
5991  *
5992  * Helper function to set BW limit for a given VSI
5993  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5994 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5995 {
5996 	struct i40e_pf *pf = vsi->back;
5997 	u64 credits = 0;
5998 	int speed = 0;
5999 	int ret = 0;
6000 
6001 	speed = i40e_get_link_speed(vsi);
6002 	if (max_tx_rate > speed) {
6003 		dev_err(&pf->pdev->dev,
6004 			"Invalid max tx rate %llu specified for VSI seid %d.",
6005 			max_tx_rate, seid);
6006 		return -EINVAL;
6007 	}
6008 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
6009 		dev_warn(&pf->pdev->dev,
6010 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
6011 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
6012 	}
6013 
6014 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
6015 	credits = max_tx_rate;
6016 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
6017 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
6018 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
6019 	if (ret)
6020 		dev_err(&pf->pdev->dev,
6021 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
6022 			max_tx_rate, seid, ERR_PTR(ret),
6023 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6024 	return ret;
6025 }
6026 
6027 /**
6028  * i40e_remove_queue_channels - Remove queue channels for the TCs
6029  * @vsi: VSI to be configured
6030  *
6031  * Remove queue channels for the TCs
6032  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)6033 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
6034 {
6035 	enum i40e_admin_queue_err last_aq_status;
6036 	struct i40e_cloud_filter *cfilter;
6037 	struct i40e_channel *ch, *ch_tmp;
6038 	struct i40e_pf *pf = vsi->back;
6039 	struct hlist_node *node;
6040 	int ret, i;
6041 
6042 	/* Reset rss size that was stored when reconfiguring rss for
6043 	 * channel VSIs with non-power-of-2 queue count.
6044 	 */
6045 	vsi->current_rss_size = 0;
6046 
6047 	/* perform cleanup for channels if they exist */
6048 	if (list_empty(&vsi->ch_list))
6049 		return;
6050 
6051 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6052 		struct i40e_vsi *p_vsi;
6053 
6054 		list_del(&ch->list);
6055 		p_vsi = ch->parent_vsi;
6056 		if (!p_vsi || !ch->initialized) {
6057 			kfree(ch);
6058 			continue;
6059 		}
6060 		/* Reset queue contexts */
6061 		for (i = 0; i < ch->num_queue_pairs; i++) {
6062 			struct i40e_ring *tx_ring, *rx_ring;
6063 			u16 pf_q;
6064 
6065 			pf_q = ch->base_queue + i;
6066 			tx_ring = vsi->tx_rings[pf_q];
6067 			tx_ring->ch = NULL;
6068 
6069 			rx_ring = vsi->rx_rings[pf_q];
6070 			rx_ring->ch = NULL;
6071 		}
6072 
6073 		/* Reset BW configured for this VSI via mqprio */
6074 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6075 		if (ret)
6076 			dev_info(&vsi->back->pdev->dev,
6077 				 "Failed to reset tx rate for ch->seid %u\n",
6078 				 ch->seid);
6079 
6080 		/* delete cloud filters associated with this channel */
6081 		hlist_for_each_entry_safe(cfilter, node,
6082 					  &pf->cloud_filter_list, cloud_node) {
6083 			if (cfilter->seid != ch->seid)
6084 				continue;
6085 
6086 			hash_del(&cfilter->cloud_node);
6087 			if (cfilter->dst_port)
6088 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6089 									cfilter,
6090 									false);
6091 			else
6092 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6093 								false);
6094 			last_aq_status = pf->hw.aq.asq_last_status;
6095 			if (ret)
6096 				dev_info(&pf->pdev->dev,
6097 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6098 					 ERR_PTR(ret),
6099 					 i40e_aq_str(&pf->hw, last_aq_status));
6100 			kfree(cfilter);
6101 		}
6102 
6103 		/* delete VSI from FW */
6104 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6105 					     NULL);
6106 		if (ret)
6107 			dev_err(&vsi->back->pdev->dev,
6108 				"unable to remove channel (%d) for parent VSI(%d)\n",
6109 				ch->seid, p_vsi->seid);
6110 		kfree(ch);
6111 	}
6112 	INIT_LIST_HEAD(&vsi->ch_list);
6113 }
6114 
6115 /**
6116  * i40e_get_max_queues_for_channel
6117  * @vsi: ptr to VSI to which channels are associated with
6118  *
6119  * Helper function which returns max value among the queue counts set on the
6120  * channels/TCs created.
6121  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)6122 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6123 {
6124 	struct i40e_channel *ch, *ch_tmp;
6125 	int max = 0;
6126 
6127 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6128 		if (!ch->initialized)
6129 			continue;
6130 		if (ch->num_queue_pairs > max)
6131 			max = ch->num_queue_pairs;
6132 	}
6133 
6134 	return max;
6135 }
6136 
6137 /**
6138  * i40e_validate_num_queues - validate num_queues w.r.t channel
6139  * @pf: ptr to PF device
6140  * @num_queues: number of queues
6141  * @vsi: the parent VSI
6142  * @reconfig_rss: indicates should the RSS be reconfigured or not
6143  *
6144  * This function validates number of queues in the context of new channel
6145  * which is being established and determines if RSS should be reconfigured
6146  * or not for parent VSI.
6147  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)6148 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6149 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6150 {
6151 	int max_ch_queues;
6152 
6153 	if (!reconfig_rss)
6154 		return -EINVAL;
6155 
6156 	*reconfig_rss = false;
6157 	if (vsi->current_rss_size) {
6158 		if (num_queues > vsi->current_rss_size) {
6159 			dev_dbg(&pf->pdev->dev,
6160 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6161 				num_queues, vsi->current_rss_size);
6162 			return -EINVAL;
6163 		} else if ((num_queues < vsi->current_rss_size) &&
6164 			   (!is_power_of_2(num_queues))) {
6165 			dev_dbg(&pf->pdev->dev,
6166 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6167 				num_queues, vsi->current_rss_size);
6168 			return -EINVAL;
6169 		}
6170 	}
6171 
6172 	if (!is_power_of_2(num_queues)) {
6173 		/* Find the max num_queues configured for channel if channel
6174 		 * exist.
6175 		 * if channel exist, then enforce 'num_queues' to be more than
6176 		 * max ever queues configured for channel.
6177 		 */
6178 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6179 		if (num_queues < max_ch_queues) {
6180 			dev_dbg(&pf->pdev->dev,
6181 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6182 				num_queues, max_ch_queues);
6183 			return -EINVAL;
6184 		}
6185 		*reconfig_rss = true;
6186 	}
6187 
6188 	return 0;
6189 }
6190 
6191 /**
6192  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6193  * @vsi: the VSI being setup
6194  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6195  *
6196  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6197  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)6198 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6199 {
6200 	struct i40e_pf *pf = vsi->back;
6201 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6202 	struct i40e_hw *hw = &pf->hw;
6203 	int local_rss_size;
6204 	u8 *lut;
6205 	int ret;
6206 
6207 	if (!vsi->rss_size)
6208 		return -EINVAL;
6209 
6210 	if (rss_size > vsi->rss_size)
6211 		return -EINVAL;
6212 
6213 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6214 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6215 	if (!lut)
6216 		return -ENOMEM;
6217 
6218 	/* Ignoring user configured lut if there is one */
6219 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6220 
6221 	/* Use user configured hash key if there is one, otherwise
6222 	 * use default.
6223 	 */
6224 	if (vsi->rss_hkey_user)
6225 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6226 	else
6227 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6228 
6229 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6230 	if (ret) {
6231 		dev_info(&pf->pdev->dev,
6232 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6233 			 ERR_PTR(ret),
6234 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6235 		kfree(lut);
6236 		return ret;
6237 	}
6238 	kfree(lut);
6239 
6240 	/* Do the update w.r.t. storing rss_size */
6241 	if (!vsi->orig_rss_size)
6242 		vsi->orig_rss_size = vsi->rss_size;
6243 	vsi->current_rss_size = local_rss_size;
6244 
6245 	return ret;
6246 }
6247 
6248 /**
6249  * i40e_channel_setup_queue_map - Setup a channel queue map
6250  * @pf: ptr to PF device
6251  * @ctxt: VSI context structure
6252  * @ch: ptr to channel structure
6253  *
6254  * Setup queue map for a specific channel
6255  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)6256 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6257 					 struct i40e_vsi_context *ctxt,
6258 					 struct i40e_channel *ch)
6259 {
6260 	u16 qcount, qmap, sections = 0;
6261 	u8 offset = 0;
6262 	int pow;
6263 
6264 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6265 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6266 
6267 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6268 	ch->num_queue_pairs = qcount;
6269 
6270 	/* find the next higher power-of-2 of num queue pairs */
6271 	pow = ilog2(qcount);
6272 	if (!is_power_of_2(qcount))
6273 		pow++;
6274 
6275 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6276 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6277 
6278 	/* Setup queue TC[0].qmap for given VSI context */
6279 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6280 
6281 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6282 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6283 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6284 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6285 }
6286 
6287 /**
6288  * i40e_add_channel - add a channel by adding VSI
6289  * @pf: ptr to PF device
6290  * @uplink_seid: underlying HW switching element (VEB) ID
6291  * @ch: ptr to channel structure
6292  *
6293  * Add a channel (VSI) using add_vsi and queue_map
6294  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)6295 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6296 			    struct i40e_channel *ch)
6297 {
6298 	struct i40e_hw *hw = &pf->hw;
6299 	struct i40e_vsi_context ctxt;
6300 	u8 enabled_tc = 0x1; /* TC0 enabled */
6301 	int ret;
6302 
6303 	if (ch->type != I40E_VSI_VMDQ2) {
6304 		dev_info(&pf->pdev->dev,
6305 			 "add new vsi failed, ch->type %d\n", ch->type);
6306 		return -EINVAL;
6307 	}
6308 
6309 	memset(&ctxt, 0, sizeof(ctxt));
6310 	ctxt.pf_num = hw->pf_id;
6311 	ctxt.vf_num = 0;
6312 	ctxt.uplink_seid = uplink_seid;
6313 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6314 	if (ch->type == I40E_VSI_VMDQ2)
6315 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6316 
6317 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6318 		ctxt.info.valid_sections |=
6319 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6320 		ctxt.info.switch_id =
6321 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6322 	}
6323 
6324 	/* Set queue map for a given VSI context */
6325 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6326 
6327 	/* Now time to create VSI */
6328 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6329 	if (ret) {
6330 		dev_info(&pf->pdev->dev,
6331 			 "add new vsi failed, err %pe aq_err %s\n",
6332 			 ERR_PTR(ret),
6333 			 i40e_aq_str(&pf->hw,
6334 				     pf->hw.aq.asq_last_status));
6335 		return -ENOENT;
6336 	}
6337 
6338 	/* Success, update channel, set enabled_tc only if the channel
6339 	 * is not a macvlan
6340 	 */
6341 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6342 	ch->seid = ctxt.seid;
6343 	ch->vsi_number = ctxt.vsi_number;
6344 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6345 
6346 	/* copy just the sections touched not the entire info
6347 	 * since not all sections are valid as returned by
6348 	 * update vsi params
6349 	 */
6350 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6351 	memcpy(&ch->info.queue_mapping,
6352 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6353 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6354 	       sizeof(ctxt.info.tc_mapping));
6355 
6356 	return 0;
6357 }
6358 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6359 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6360 				  u8 *bw_share)
6361 {
6362 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6363 	int ret;
6364 	int i;
6365 
6366 	memset(&bw_data, 0, sizeof(bw_data));
6367 	bw_data.tc_valid_bits = ch->enabled_tc;
6368 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6369 		bw_data.tc_bw_credits[i] = bw_share[i];
6370 
6371 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6372 				       &bw_data, NULL);
6373 	if (ret) {
6374 		dev_info(&vsi->back->pdev->dev,
6375 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6376 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6377 		return -EINVAL;
6378 	}
6379 
6380 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6381 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6382 
6383 	return 0;
6384 }
6385 
6386 /**
6387  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6388  * @pf: ptr to PF device
6389  * @vsi: the VSI being setup
6390  * @ch: ptr to channel structure
6391  *
6392  * Configure TX rings associated with channel (VSI) since queues are being
6393  * from parent VSI.
6394  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6395 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6396 				       struct i40e_vsi *vsi,
6397 				       struct i40e_channel *ch)
6398 {
6399 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6400 	int ret;
6401 	int i;
6402 
6403 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6404 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6405 		if (ch->enabled_tc & BIT(i))
6406 			bw_share[i] = 1;
6407 	}
6408 
6409 	/* configure BW for new VSI */
6410 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6411 	if (ret) {
6412 		dev_info(&vsi->back->pdev->dev,
6413 			 "Failed configuring TC map %d for channel (seid %u)\n",
6414 			 ch->enabled_tc, ch->seid);
6415 		return ret;
6416 	}
6417 
6418 	for (i = 0; i < ch->num_queue_pairs; i++) {
6419 		struct i40e_ring *tx_ring, *rx_ring;
6420 		u16 pf_q;
6421 
6422 		pf_q = ch->base_queue + i;
6423 
6424 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6425 		 * context
6426 		 */
6427 		tx_ring = vsi->tx_rings[pf_q];
6428 		tx_ring->ch = ch;
6429 
6430 		/* Get the RX ring ptr */
6431 		rx_ring = vsi->rx_rings[pf_q];
6432 		rx_ring->ch = ch;
6433 	}
6434 
6435 	return 0;
6436 }
6437 
6438 /**
6439  * i40e_setup_hw_channel - setup new channel
6440  * @pf: ptr to PF device
6441  * @vsi: the VSI being setup
6442  * @ch: ptr to channel structure
6443  * @uplink_seid: underlying HW switching element (VEB) ID
6444  * @type: type of channel to be created (VMDq2/VF)
6445  *
6446  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6447  * and configures TX rings accordingly
6448  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6449 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6450 					struct i40e_vsi *vsi,
6451 					struct i40e_channel *ch,
6452 					u16 uplink_seid, u8 type)
6453 {
6454 	int ret;
6455 
6456 	ch->initialized = false;
6457 	ch->base_queue = vsi->next_base_queue;
6458 	ch->type = type;
6459 
6460 	/* Proceed with creation of channel (VMDq2) VSI */
6461 	ret = i40e_add_channel(pf, uplink_seid, ch);
6462 	if (ret) {
6463 		dev_info(&pf->pdev->dev,
6464 			 "failed to add_channel using uplink_seid %u\n",
6465 			 uplink_seid);
6466 		return ret;
6467 	}
6468 
6469 	/* Mark the successful creation of channel */
6470 	ch->initialized = true;
6471 
6472 	/* Reconfigure TX queues using QTX_CTL register */
6473 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6474 	if (ret) {
6475 		dev_info(&pf->pdev->dev,
6476 			 "failed to configure TX rings for channel %u\n",
6477 			 ch->seid);
6478 		return ret;
6479 	}
6480 
6481 	/* update 'next_base_queue' */
6482 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6483 	dev_dbg(&pf->pdev->dev,
6484 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6485 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6486 		ch->num_queue_pairs,
6487 		vsi->next_base_queue);
6488 	return ret;
6489 }
6490 
6491 /**
6492  * i40e_setup_channel - setup new channel using uplink element
6493  * @pf: ptr to PF device
6494  * @vsi: pointer to the VSI to set up the channel within
6495  * @ch: ptr to channel structure
6496  *
6497  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6498  * and uplink switching element (uplink_seid)
6499  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6500 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6501 			       struct i40e_channel *ch)
6502 {
6503 	u8 vsi_type;
6504 	u16 seid;
6505 	int ret;
6506 
6507 	if (vsi->type == I40E_VSI_MAIN) {
6508 		vsi_type = I40E_VSI_VMDQ2;
6509 	} else {
6510 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6511 			vsi->type);
6512 		return false;
6513 	}
6514 
6515 	/* underlying switching element */
6516 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6517 
6518 	/* create channel (VSI), configure TX rings */
6519 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6520 	if (ret) {
6521 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6522 		return false;
6523 	}
6524 
6525 	return ch->initialized ? true : false;
6526 }
6527 
6528 /**
6529  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6530  * @vsi: ptr to VSI which has PF backing
6531  *
6532  * Sets up switch mode correctly if it needs to be changed and perform
6533  * what are allowed modes.
6534  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6535 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6536 {
6537 	u8 mode;
6538 	struct i40e_pf *pf = vsi->back;
6539 	struct i40e_hw *hw = &pf->hw;
6540 	int ret;
6541 
6542 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6543 	if (ret)
6544 		return -EINVAL;
6545 
6546 	if (hw->dev_caps.switch_mode) {
6547 		/* if switch mode is set, support mode2 (non-tunneled for
6548 		 * cloud filter) for now
6549 		 */
6550 		u32 switch_mode = hw->dev_caps.switch_mode &
6551 				  I40E_SWITCH_MODE_MASK;
6552 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6553 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6554 				return 0;
6555 			dev_err(&pf->pdev->dev,
6556 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6557 				hw->dev_caps.switch_mode);
6558 			return -EINVAL;
6559 		}
6560 	}
6561 
6562 	/* Set Bit 7 to be valid */
6563 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6564 
6565 	/* Set L4type for TCP support */
6566 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6567 
6568 	/* Set cloud filter mode */
6569 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6570 
6571 	/* Prep mode field for set_switch_config */
6572 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6573 					pf->last_sw_conf_valid_flags,
6574 					mode, NULL);
6575 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6576 		dev_err(&pf->pdev->dev,
6577 			"couldn't set switch config bits, err %pe aq_err %s\n",
6578 			ERR_PTR(ret),
6579 			i40e_aq_str(hw,
6580 				    hw->aq.asq_last_status));
6581 
6582 	return ret;
6583 }
6584 
6585 /**
6586  * i40e_create_queue_channel - function to create channel
6587  * @vsi: VSI to be configured
6588  * @ch: ptr to channel (it contains channel specific params)
6589  *
6590  * This function creates channel (VSI) using num_queues specified by user,
6591  * reconfigs RSS if needed.
6592  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6593 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6594 			      struct i40e_channel *ch)
6595 {
6596 	struct i40e_pf *pf = vsi->back;
6597 	bool reconfig_rss;
6598 	int err;
6599 
6600 	if (!ch)
6601 		return -EINVAL;
6602 
6603 	if (!ch->num_queue_pairs) {
6604 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6605 			ch->num_queue_pairs);
6606 		return -EINVAL;
6607 	}
6608 
6609 	/* validate user requested num_queues for channel */
6610 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6611 				       &reconfig_rss);
6612 	if (err) {
6613 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6614 			 ch->num_queue_pairs);
6615 		return -EINVAL;
6616 	}
6617 
6618 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6619 	 * VSI to be added switch to VEB mode.
6620 	 */
6621 
6622 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6623 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6624 
6625 		if (vsi->type == I40E_VSI_MAIN) {
6626 			if (i40e_is_tc_mqprio_enabled(pf))
6627 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6628 			else
6629 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6630 		}
6631 		/* now onwards for main VSI, number of queues will be value
6632 		 * of TC0's queue count
6633 		 */
6634 	}
6635 
6636 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6637 	 * it should be more than num_queues
6638 	 */
6639 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6640 		dev_dbg(&pf->pdev->dev,
6641 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6642 			vsi->cnt_q_avail, ch->num_queue_pairs);
6643 		return -EINVAL;
6644 	}
6645 
6646 	/* reconfig_rss only if vsi type is MAIN_VSI */
6647 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6648 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6649 		if (err) {
6650 			dev_info(&pf->pdev->dev,
6651 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6652 				 ch->num_queue_pairs);
6653 			return -EINVAL;
6654 		}
6655 	}
6656 
6657 	if (!i40e_setup_channel(pf, vsi, ch)) {
6658 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6659 		return -EINVAL;
6660 	}
6661 
6662 	dev_info(&pf->pdev->dev,
6663 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6664 		 ch->seid, ch->num_queue_pairs);
6665 
6666 	/* configure VSI for BW limit */
6667 	if (ch->max_tx_rate) {
6668 		u64 credits = ch->max_tx_rate;
6669 
6670 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6671 			return -EINVAL;
6672 
6673 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6674 		dev_dbg(&pf->pdev->dev,
6675 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6676 			ch->max_tx_rate,
6677 			credits,
6678 			ch->seid);
6679 	}
6680 
6681 	/* in case of VF, this will be main SRIOV VSI */
6682 	ch->parent_vsi = vsi;
6683 
6684 	/* and update main_vsi's count for queue_available to use */
6685 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6686 
6687 	return 0;
6688 }
6689 
6690 /**
6691  * i40e_configure_queue_channels - Add queue channel for the given TCs
6692  * @vsi: VSI to be configured
6693  *
6694  * Configures queue channel mapping to the given TCs
6695  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6696 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6697 {
6698 	struct i40e_channel *ch;
6699 	u64 max_rate = 0;
6700 	int ret = 0, i;
6701 
6702 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6703 	vsi->tc_seid_map[0] = vsi->seid;
6704 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6705 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6706 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6707 			if (!ch) {
6708 				ret = -ENOMEM;
6709 				goto err_free;
6710 			}
6711 
6712 			INIT_LIST_HEAD(&ch->list);
6713 			ch->num_queue_pairs =
6714 				vsi->tc_config.tc_info[i].qcount;
6715 			ch->base_queue =
6716 				vsi->tc_config.tc_info[i].qoffset;
6717 
6718 			/* Bandwidth limit through tc interface is in bytes/s,
6719 			 * change to Mbit/s
6720 			 */
6721 			max_rate = vsi->mqprio_qopt.max_rate[i];
6722 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6723 			ch->max_tx_rate = max_rate;
6724 
6725 			list_add_tail(&ch->list, &vsi->ch_list);
6726 
6727 			ret = i40e_create_queue_channel(vsi, ch);
6728 			if (ret) {
6729 				dev_err(&vsi->back->pdev->dev,
6730 					"Failed creating queue channel with TC%d: queues %d\n",
6731 					i, ch->num_queue_pairs);
6732 				goto err_free;
6733 			}
6734 			vsi->tc_seid_map[i] = ch->seid;
6735 		}
6736 	}
6737 
6738 	/* reset to reconfigure TX queue contexts */
6739 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6740 	return ret;
6741 
6742 err_free:
6743 	i40e_remove_queue_channels(vsi);
6744 	return ret;
6745 }
6746 
6747 /**
6748  * i40e_veb_config_tc - Configure TCs for given VEB
6749  * @veb: given VEB
6750  * @enabled_tc: TC bitmap
6751  *
6752  * Configures given TC bitmap for VEB (switching) element
6753  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6754 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6755 {
6756 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6757 	struct i40e_pf *pf = veb->pf;
6758 	int ret = 0;
6759 	int i;
6760 
6761 	/* No TCs or already enabled TCs just return */
6762 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6763 		return ret;
6764 
6765 	bw_data.tc_valid_bits = enabled_tc;
6766 	/* bw_data.absolute_credits is not set (relative) */
6767 
6768 	/* Enable ETS TCs with equal BW Share for now */
6769 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6770 		if (enabled_tc & BIT(i))
6771 			bw_data.tc_bw_share_credits[i] = 1;
6772 	}
6773 
6774 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6775 						   &bw_data, NULL);
6776 	if (ret) {
6777 		dev_info(&pf->pdev->dev,
6778 			 "VEB bw config failed, err %pe aq_err %s\n",
6779 			 ERR_PTR(ret),
6780 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6781 		goto out;
6782 	}
6783 
6784 	/* Update the BW information */
6785 	ret = i40e_veb_get_bw_info(veb);
6786 	if (ret) {
6787 		dev_info(&pf->pdev->dev,
6788 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6789 			 ERR_PTR(ret),
6790 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6791 	}
6792 
6793 out:
6794 	return ret;
6795 }
6796 
6797 #ifdef CONFIG_I40E_DCB
6798 /**
6799  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6800  * @pf: PF struct
6801  *
6802  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6803  * the caller would've quiesce all the VSIs before calling
6804  * this function
6805  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6806 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6807 {
6808 	u8 tc_map = 0;
6809 	int ret;
6810 	u8 v;
6811 
6812 	/* Enable the TCs available on PF to all VEBs */
6813 	tc_map = i40e_pf_get_tc_map(pf);
6814 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6815 		return;
6816 
6817 	for (v = 0; v < I40E_MAX_VEB; v++) {
6818 		if (!pf->veb[v])
6819 			continue;
6820 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6821 		if (ret) {
6822 			dev_info(&pf->pdev->dev,
6823 				 "Failed configuring TC for VEB seid=%d\n",
6824 				 pf->veb[v]->seid);
6825 			/* Will try to configure as many components */
6826 		}
6827 	}
6828 
6829 	/* Update each VSI */
6830 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6831 		if (!pf->vsi[v])
6832 			continue;
6833 
6834 		/* - Enable all TCs for the LAN VSI
6835 		 * - For all others keep them at TC0 for now
6836 		 */
6837 		if (v == pf->lan_vsi)
6838 			tc_map = i40e_pf_get_tc_map(pf);
6839 		else
6840 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6841 
6842 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6843 		if (ret) {
6844 			dev_info(&pf->pdev->dev,
6845 				 "Failed configuring TC for VSI seid=%d\n",
6846 				 pf->vsi[v]->seid);
6847 			/* Will try to configure as many components */
6848 		} else {
6849 			/* Re-configure VSI vectors based on updated TC map */
6850 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6851 			if (pf->vsi[v]->netdev)
6852 				i40e_dcbnl_set_all(pf->vsi[v]);
6853 		}
6854 	}
6855 }
6856 
6857 /**
6858  * i40e_resume_port_tx - Resume port Tx
6859  * @pf: PF struct
6860  *
6861  * Resume a port's Tx and issue a PF reset in case of failure to
6862  * resume.
6863  **/
i40e_resume_port_tx(struct i40e_pf * pf)6864 static int i40e_resume_port_tx(struct i40e_pf *pf)
6865 {
6866 	struct i40e_hw *hw = &pf->hw;
6867 	int ret;
6868 
6869 	ret = i40e_aq_resume_port_tx(hw, NULL);
6870 	if (ret) {
6871 		dev_info(&pf->pdev->dev,
6872 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6873 			  ERR_PTR(ret),
6874 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6875 		/* Schedule PF reset to recover */
6876 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6877 		i40e_service_event_schedule(pf);
6878 	}
6879 
6880 	return ret;
6881 }
6882 
6883 /**
6884  * i40e_suspend_port_tx - Suspend port Tx
6885  * @pf: PF struct
6886  *
6887  * Suspend a port's Tx and issue a PF reset in case of failure.
6888  **/
i40e_suspend_port_tx(struct i40e_pf * pf)6889 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6890 {
6891 	struct i40e_hw *hw = &pf->hw;
6892 	int ret;
6893 
6894 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6895 	if (ret) {
6896 		dev_info(&pf->pdev->dev,
6897 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6898 			 ERR_PTR(ret),
6899 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6900 		/* Schedule PF reset to recover */
6901 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6902 		i40e_service_event_schedule(pf);
6903 	}
6904 
6905 	return ret;
6906 }
6907 
6908 /**
6909  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6910  * @pf: PF being configured
6911  * @new_cfg: New DCBX configuration
6912  *
6913  * Program DCB settings into HW and reconfigure VEB/VSIs on
6914  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6915  **/
i40e_hw_set_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6916 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6917 				  struct i40e_dcbx_config *new_cfg)
6918 {
6919 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6920 	int ret;
6921 
6922 	/* Check if need reconfiguration */
6923 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6924 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6925 		return 0;
6926 	}
6927 
6928 	/* Config change disable all VSIs */
6929 	i40e_pf_quiesce_all_vsi(pf);
6930 
6931 	/* Copy the new config to the current config */
6932 	*old_cfg = *new_cfg;
6933 	old_cfg->etsrec = old_cfg->etscfg;
6934 	ret = i40e_set_dcb_config(&pf->hw);
6935 	if (ret) {
6936 		dev_info(&pf->pdev->dev,
6937 			 "Set DCB Config failed, err %pe aq_err %s\n",
6938 			 ERR_PTR(ret),
6939 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6940 		goto out;
6941 	}
6942 
6943 	/* Changes in configuration update VEB/VSI */
6944 	i40e_dcb_reconfigure(pf);
6945 out:
6946 	/* In case of reset do not try to resume anything */
6947 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6948 		/* Re-start the VSIs if disabled */
6949 		ret = i40e_resume_port_tx(pf);
6950 		/* In case of error no point in resuming VSIs */
6951 		if (ret)
6952 			goto err;
6953 		i40e_pf_unquiesce_all_vsi(pf);
6954 	}
6955 err:
6956 	return ret;
6957 }
6958 
6959 /**
6960  * i40e_hw_dcb_config - Program new DCBX settings into HW
6961  * @pf: PF being configured
6962  * @new_cfg: New DCBX configuration
6963  *
6964  * Program DCB settings into HW and reconfigure VEB/VSIs on
6965  * given PF
6966  **/
i40e_hw_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6967 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6968 {
6969 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6970 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6971 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6972 	struct i40e_dcbx_config *old_cfg;
6973 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6974 	struct i40e_rx_pb_config pb_cfg;
6975 	struct i40e_hw *hw = &pf->hw;
6976 	u8 num_ports = hw->num_ports;
6977 	bool need_reconfig;
6978 	int ret = -EINVAL;
6979 	u8 lltc_map = 0;
6980 	u8 tc_map = 0;
6981 	u8 new_numtc;
6982 	u8 i;
6983 
6984 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6985 	/* Un-pack information to Program ETS HW via shared API
6986 	 * numtc, tcmap
6987 	 * LLTC map
6988 	 * ETS/NON-ETS arbiter mode
6989 	 * max exponent (credit refills)
6990 	 * Total number of ports
6991 	 * PFC priority bit-map
6992 	 * Priority Table
6993 	 * BW % per TC
6994 	 * Arbiter mode between UPs sharing same TC
6995 	 * TSA table (ETS or non-ETS)
6996 	 * EEE enabled or not
6997 	 * MFS TC table
6998 	 */
6999 
7000 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
7001 
7002 	memset(&ets_data, 0, sizeof(ets_data));
7003 	for (i = 0; i < new_numtc; i++) {
7004 		tc_map |= BIT(i);
7005 		switch (new_cfg->etscfg.tsatable[i]) {
7006 		case I40E_IEEE_TSA_ETS:
7007 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
7008 			ets_data.tc_bw_share_credits[i] =
7009 					new_cfg->etscfg.tcbwtable[i];
7010 			break;
7011 		case I40E_IEEE_TSA_STRICT:
7012 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
7013 			lltc_map |= BIT(i);
7014 			ets_data.tc_bw_share_credits[i] =
7015 					I40E_DCB_STRICT_PRIO_CREDITS;
7016 			break;
7017 		default:
7018 			/* Invalid TSA type */
7019 			need_reconfig = false;
7020 			goto out;
7021 		}
7022 	}
7023 
7024 	old_cfg = &hw->local_dcbx_config;
7025 	/* Check if need reconfiguration */
7026 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
7027 
7028 	/* If needed, enable/disable frame tagging, disable all VSIs
7029 	 * and suspend port tx
7030 	 */
7031 	if (need_reconfig) {
7032 		/* Enable DCB tagging only when more than one TC */
7033 		if (new_numtc > 1)
7034 			pf->flags |= I40E_FLAG_DCB_ENABLED;
7035 		else
7036 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7037 
7038 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7039 		/* Reconfiguration needed quiesce all VSIs */
7040 		i40e_pf_quiesce_all_vsi(pf);
7041 		ret = i40e_suspend_port_tx(pf);
7042 		if (ret)
7043 			goto err;
7044 	}
7045 
7046 	/* Configure Port ETS Tx Scheduler */
7047 	ets_data.tc_valid_bits = tc_map;
7048 	ets_data.tc_strict_priority_flags = lltc_map;
7049 	ret = i40e_aq_config_switch_comp_ets
7050 		(hw, pf->mac_seid, &ets_data,
7051 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7052 	if (ret) {
7053 		dev_info(&pf->pdev->dev,
7054 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7055 			 ERR_PTR(ret),
7056 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7057 		goto out;
7058 	}
7059 
7060 	/* Configure Rx ETS HW */
7061 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7062 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7063 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7064 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7065 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7066 				   lltc_map);
7067 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7068 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7069 				     prio_type);
7070 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7071 			       new_cfg->etscfg.prioritytable);
7072 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7073 
7074 	/* Configure Rx Packet Buffers in HW */
7075 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7076 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7077 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7078 	}
7079 
7080 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7081 					 false, new_cfg->pfc.pfcenable,
7082 					 mfs_tc, &pb_cfg);
7083 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7084 
7085 	/* Update the local Rx Packet buffer config */
7086 	pf->pb_cfg = pb_cfg;
7087 
7088 	/* Inform the FW about changes to DCB configuration */
7089 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7090 	if (ret) {
7091 		dev_info(&pf->pdev->dev,
7092 			 "DCB Updated failed, err %pe aq_err %s\n",
7093 			 ERR_PTR(ret),
7094 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7095 		goto out;
7096 	}
7097 
7098 	/* Update the port DCBx configuration */
7099 	*old_cfg = *new_cfg;
7100 
7101 	/* Changes in configuration update VEB/VSI */
7102 	i40e_dcb_reconfigure(pf);
7103 out:
7104 	/* Re-start the VSIs if disabled */
7105 	if (need_reconfig) {
7106 		ret = i40e_resume_port_tx(pf);
7107 
7108 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7109 		/* In case of error no point in resuming VSIs */
7110 		if (ret)
7111 			goto err;
7112 
7113 		/* Wait for the PF's queues to be disabled */
7114 		ret = i40e_pf_wait_queues_disabled(pf);
7115 		if (ret) {
7116 			/* Schedule PF reset to recover */
7117 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7118 			i40e_service_event_schedule(pf);
7119 			goto err;
7120 		} else {
7121 			i40e_pf_unquiesce_all_vsi(pf);
7122 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7123 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7124 		}
7125 		/* registers are set, lets apply */
7126 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7127 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7128 	}
7129 
7130 err:
7131 	return ret;
7132 }
7133 
7134 /**
7135  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7136  * @pf: PF being queried
7137  *
7138  * Set default DCB configuration in case DCB is to be done in SW.
7139  **/
i40e_dcb_sw_default_config(struct i40e_pf * pf)7140 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7141 {
7142 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7143 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7144 	struct i40e_hw *hw = &pf->hw;
7145 	int err;
7146 
7147 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7148 		/* Update the local cached instance with TC0 ETS */
7149 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7150 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7151 		pf->tmp_cfg.etscfg.maxtcs = 0;
7152 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7153 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7154 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7155 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7156 		/* FW needs one App to configure HW */
7157 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7158 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7159 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7160 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7161 
7162 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7163 	}
7164 
7165 	memset(&ets_data, 0, sizeof(ets_data));
7166 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7167 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7168 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7169 
7170 	/* Enable ETS on the Physical port */
7171 	err = i40e_aq_config_switch_comp_ets
7172 		(hw, pf->mac_seid, &ets_data,
7173 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7174 	if (err) {
7175 		dev_info(&pf->pdev->dev,
7176 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7177 			 ERR_PTR(err),
7178 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7179 		err = -ENOENT;
7180 		goto out;
7181 	}
7182 
7183 	/* Update the local cached instance with TC0 ETS */
7184 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7185 	dcb_cfg->etscfg.cbs = 0;
7186 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7187 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7188 
7189 out:
7190 	return err;
7191 }
7192 
7193 /**
7194  * i40e_init_pf_dcb - Initialize DCB configuration
7195  * @pf: PF being configured
7196  *
7197  * Query the current DCB configuration and cache it
7198  * in the hardware structure
7199  **/
i40e_init_pf_dcb(struct i40e_pf * pf)7200 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7201 {
7202 	struct i40e_hw *hw = &pf->hw;
7203 	int err;
7204 
7205 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7206 	 * Also do not enable DCBx if FW LLDP agent is disabled
7207 	 */
7208 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7209 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7210 		err = -EOPNOTSUPP;
7211 		goto out;
7212 	}
7213 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7214 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7215 		err = i40e_dcb_sw_default_config(pf);
7216 		if (err) {
7217 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7218 			goto out;
7219 		}
7220 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7221 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7222 			       DCB_CAP_DCBX_VER_IEEE;
7223 		/* at init capable but disabled */
7224 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7225 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7226 		goto out;
7227 	}
7228 	err = i40e_init_dcb(hw, true);
7229 	if (!err) {
7230 		/* Device/Function is not DCBX capable */
7231 		if ((!hw->func_caps.dcb) ||
7232 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7233 			dev_info(&pf->pdev->dev,
7234 				 "DCBX offload is not supported or is disabled for this PF.\n");
7235 		} else {
7236 			/* When status is not DISABLED then DCBX in FW */
7237 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7238 				       DCB_CAP_DCBX_VER_IEEE;
7239 
7240 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7241 			/* Enable DCB tagging only when more than one TC
7242 			 * or explicitly disable if only one TC
7243 			 */
7244 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7245 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7246 			else
7247 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7248 			dev_dbg(&pf->pdev->dev,
7249 				"DCBX offload is supported for this PF.\n");
7250 		}
7251 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7252 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7253 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7254 	} else {
7255 		dev_info(&pf->pdev->dev,
7256 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7257 			 ERR_PTR(err),
7258 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7259 	}
7260 
7261 out:
7262 	return err;
7263 }
7264 #endif /* CONFIG_I40E_DCB */
7265 
7266 /**
7267  * i40e_print_link_message - print link up or down
7268  * @vsi: the VSI for which link needs a message
7269  * @isup: true of link is up, false otherwise
7270  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)7271 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7272 {
7273 	enum i40e_aq_link_speed new_speed;
7274 	struct i40e_pf *pf = vsi->back;
7275 	char *speed = "Unknown";
7276 	char *fc = "Unknown";
7277 	char *fec = "";
7278 	char *req_fec = "";
7279 	char *an = "";
7280 
7281 	if (isup)
7282 		new_speed = pf->hw.phy.link_info.link_speed;
7283 	else
7284 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7285 
7286 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7287 		return;
7288 	vsi->current_isup = isup;
7289 	vsi->current_speed = new_speed;
7290 	if (!isup) {
7291 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7292 		return;
7293 	}
7294 
7295 	/* Warn user if link speed on NPAR enabled partition is not at
7296 	 * least 10GB
7297 	 */
7298 	if (pf->hw.func_caps.npar_enable &&
7299 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7300 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7301 		netdev_warn(vsi->netdev,
7302 			    "The partition detected link speed that is less than 10Gbps\n");
7303 
7304 	switch (pf->hw.phy.link_info.link_speed) {
7305 	case I40E_LINK_SPEED_40GB:
7306 		speed = "40 G";
7307 		break;
7308 	case I40E_LINK_SPEED_20GB:
7309 		speed = "20 G";
7310 		break;
7311 	case I40E_LINK_SPEED_25GB:
7312 		speed = "25 G";
7313 		break;
7314 	case I40E_LINK_SPEED_10GB:
7315 		speed = "10 G";
7316 		break;
7317 	case I40E_LINK_SPEED_5GB:
7318 		speed = "5 G";
7319 		break;
7320 	case I40E_LINK_SPEED_2_5GB:
7321 		speed = "2.5 G";
7322 		break;
7323 	case I40E_LINK_SPEED_1GB:
7324 		speed = "1000 M";
7325 		break;
7326 	case I40E_LINK_SPEED_100MB:
7327 		speed = "100 M";
7328 		break;
7329 	default:
7330 		break;
7331 	}
7332 
7333 	switch (pf->hw.fc.current_mode) {
7334 	case I40E_FC_FULL:
7335 		fc = "RX/TX";
7336 		break;
7337 	case I40E_FC_TX_PAUSE:
7338 		fc = "TX";
7339 		break;
7340 	case I40E_FC_RX_PAUSE:
7341 		fc = "RX";
7342 		break;
7343 	default:
7344 		fc = "None";
7345 		break;
7346 	}
7347 
7348 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7349 		req_fec = "None";
7350 		fec = "None";
7351 		an = "False";
7352 
7353 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7354 			an = "True";
7355 
7356 		if (pf->hw.phy.link_info.fec_info &
7357 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7358 			fec = "CL74 FC-FEC/BASE-R";
7359 		else if (pf->hw.phy.link_info.fec_info &
7360 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7361 			fec = "CL108 RS-FEC";
7362 
7363 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7364 		 * both RS and FC are requested
7365 		 */
7366 		if (vsi->back->hw.phy.link_info.req_fec_info &
7367 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7368 			if (vsi->back->hw.phy.link_info.req_fec_info &
7369 			    I40E_AQ_REQUEST_FEC_RS)
7370 				req_fec = "CL108 RS-FEC";
7371 			else
7372 				req_fec = "CL74 FC-FEC/BASE-R";
7373 		}
7374 		netdev_info(vsi->netdev,
7375 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7376 			    speed, req_fec, fec, an, fc);
7377 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7378 		req_fec = "None";
7379 		fec = "None";
7380 		an = "False";
7381 
7382 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7383 			an = "True";
7384 
7385 		if (pf->hw.phy.link_info.fec_info &
7386 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7387 			fec = "CL74 FC-FEC/BASE-R";
7388 
7389 		if (pf->hw.phy.link_info.req_fec_info &
7390 		    I40E_AQ_REQUEST_FEC_KR)
7391 			req_fec = "CL74 FC-FEC/BASE-R";
7392 
7393 		netdev_info(vsi->netdev,
7394 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7395 			    speed, req_fec, fec, an, fc);
7396 	} else {
7397 		netdev_info(vsi->netdev,
7398 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7399 			    speed, fc);
7400 	}
7401 
7402 }
7403 
7404 /**
7405  * i40e_up_complete - Finish the last steps of bringing up a connection
7406  * @vsi: the VSI being configured
7407  **/
i40e_up_complete(struct i40e_vsi * vsi)7408 static int i40e_up_complete(struct i40e_vsi *vsi)
7409 {
7410 	struct i40e_pf *pf = vsi->back;
7411 	int err;
7412 
7413 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7414 		i40e_vsi_configure_msix(vsi);
7415 	else
7416 		i40e_configure_msi_and_legacy(vsi);
7417 
7418 	/* start rings */
7419 	err = i40e_vsi_start_rings(vsi);
7420 	if (err)
7421 		return err;
7422 
7423 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7424 	i40e_napi_enable_all(vsi);
7425 	i40e_vsi_enable_irq(vsi);
7426 
7427 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7428 	    (vsi->netdev)) {
7429 		i40e_print_link_message(vsi, true);
7430 		netif_tx_start_all_queues(vsi->netdev);
7431 		netif_carrier_on(vsi->netdev);
7432 	}
7433 
7434 	/* replay FDIR SB filters */
7435 	if (vsi->type == I40E_VSI_FDIR) {
7436 		/* reset fd counters */
7437 		pf->fd_add_err = 0;
7438 		pf->fd_atr_cnt = 0;
7439 		i40e_fdir_filter_restore(vsi);
7440 	}
7441 
7442 	/* On the next run of the service_task, notify any clients of the new
7443 	 * opened netdev
7444 	 */
7445 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7446 	i40e_service_event_schedule(pf);
7447 
7448 	return 0;
7449 }
7450 
7451 /**
7452  * i40e_vsi_reinit_locked - Reset the VSI
7453  * @vsi: the VSI being configured
7454  *
7455  * Rebuild the ring structs after some configuration
7456  * has changed, e.g. MTU size.
7457  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)7458 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7459 {
7460 	struct i40e_pf *pf = vsi->back;
7461 
7462 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7463 		usleep_range(1000, 2000);
7464 	i40e_down(vsi);
7465 
7466 	i40e_up(vsi);
7467 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7468 }
7469 
7470 /**
7471  * i40e_force_link_state - Force the link status
7472  * @pf: board private structure
7473  * @is_up: whether the link state should be forced up or down
7474  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)7475 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7476 {
7477 	struct i40e_aq_get_phy_abilities_resp abilities;
7478 	struct i40e_aq_set_phy_config config = {0};
7479 	bool non_zero_phy_type = is_up;
7480 	struct i40e_hw *hw = &pf->hw;
7481 	u64 mask;
7482 	u8 speed;
7483 	int err;
7484 
7485 	/* Card might've been put in an unstable state by other drivers
7486 	 * and applications, which causes incorrect speed values being
7487 	 * set on startup. In order to clear speed registers, we call
7488 	 * get_phy_capabilities twice, once to get initial state of
7489 	 * available speeds, and once to get current PHY config.
7490 	 */
7491 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7492 					   NULL);
7493 	if (err) {
7494 		dev_err(&pf->pdev->dev,
7495 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7496 			ERR_PTR(err),
7497 			i40e_aq_str(hw, hw->aq.asq_last_status));
7498 		return err;
7499 	}
7500 	speed = abilities.link_speed;
7501 
7502 	/* Get the current phy config */
7503 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7504 					   NULL);
7505 	if (err) {
7506 		dev_err(&pf->pdev->dev,
7507 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7508 			ERR_PTR(err),
7509 			i40e_aq_str(hw, hw->aq.asq_last_status));
7510 		return err;
7511 	}
7512 
7513 	/* If link needs to go up, but was not forced to go down,
7514 	 * and its speed values are OK, no need for a flap
7515 	 * if non_zero_phy_type was set, still need to force up
7516 	 */
7517 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7518 		non_zero_phy_type = true;
7519 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7520 		return 0;
7521 
7522 	/* To force link we need to set bits for all supported PHY types,
7523 	 * but there are now more than 32, so we need to split the bitmap
7524 	 * across two fields.
7525 	 */
7526 	mask = I40E_PHY_TYPES_BITMASK;
7527 	config.phy_type =
7528 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7529 	config.phy_type_ext =
7530 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7531 	/* Copy the old settings, except of phy_type */
7532 	config.abilities = abilities.abilities;
7533 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7534 		if (is_up)
7535 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7536 		else
7537 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7538 	}
7539 	if (abilities.link_speed != 0)
7540 		config.link_speed = abilities.link_speed;
7541 	else
7542 		config.link_speed = speed;
7543 	config.eee_capability = abilities.eee_capability;
7544 	config.eeer = abilities.eeer_val;
7545 	config.low_power_ctrl = abilities.d3_lpan;
7546 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7547 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7548 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7549 
7550 	if (err) {
7551 		dev_err(&pf->pdev->dev,
7552 			"set phy config ret =  %pe last_status =  %s\n",
7553 			ERR_PTR(err),
7554 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7555 		return err;
7556 	}
7557 
7558 	/* Update the link info */
7559 	err = i40e_update_link_info(hw);
7560 	if (err) {
7561 		/* Wait a little bit (on 40G cards it sometimes takes a really
7562 		 * long time for link to come back from the atomic reset)
7563 		 * and try once more
7564 		 */
7565 		msleep(1000);
7566 		i40e_update_link_info(hw);
7567 	}
7568 
7569 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7570 
7571 	return 0;
7572 }
7573 
7574 /**
7575  * i40e_up - Bring the connection back up after being down
7576  * @vsi: the VSI being configured
7577  **/
i40e_up(struct i40e_vsi * vsi)7578 int i40e_up(struct i40e_vsi *vsi)
7579 {
7580 	int err;
7581 
7582 	if (vsi->type == I40E_VSI_MAIN &&
7583 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7584 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7585 		i40e_force_link_state(vsi->back, true);
7586 
7587 	err = i40e_vsi_configure(vsi);
7588 	if (!err)
7589 		err = i40e_up_complete(vsi);
7590 
7591 	return err;
7592 }
7593 
7594 /**
7595  * i40e_down - Shutdown the connection processing
7596  * @vsi: the VSI being stopped
7597  **/
i40e_down(struct i40e_vsi * vsi)7598 void i40e_down(struct i40e_vsi *vsi)
7599 {
7600 	int i;
7601 
7602 	/* It is assumed that the caller of this function
7603 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7604 	 */
7605 	if (vsi->netdev) {
7606 		netif_carrier_off(vsi->netdev);
7607 		netif_tx_disable(vsi->netdev);
7608 	}
7609 	i40e_vsi_disable_irq(vsi);
7610 	i40e_vsi_stop_rings(vsi);
7611 	if (vsi->type == I40E_VSI_MAIN &&
7612 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7613 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7614 		i40e_force_link_state(vsi->back, false);
7615 	i40e_napi_disable_all(vsi);
7616 
7617 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7618 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7619 		if (i40e_enabled_xdp_vsi(vsi)) {
7620 			/* Make sure that in-progress ndo_xdp_xmit and
7621 			 * ndo_xsk_wakeup calls are completed.
7622 			 */
7623 			synchronize_rcu();
7624 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7625 		}
7626 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7627 	}
7628 
7629 }
7630 
7631 /**
7632  * i40e_validate_mqprio_qopt- validate queue mapping info
7633  * @vsi: the VSI being configured
7634  * @mqprio_qopt: queue parametrs
7635  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7636 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7637 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7638 {
7639 	u64 sum_max_rate = 0;
7640 	u64 max_rate = 0;
7641 	int i;
7642 
7643 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7644 	    mqprio_qopt->qopt.num_tc < 1 ||
7645 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7646 		return -EINVAL;
7647 	for (i = 0; ; i++) {
7648 		if (!mqprio_qopt->qopt.count[i])
7649 			return -EINVAL;
7650 		if (mqprio_qopt->min_rate[i]) {
7651 			dev_err(&vsi->back->pdev->dev,
7652 				"Invalid min tx rate (greater than 0) specified\n");
7653 			return -EINVAL;
7654 		}
7655 		max_rate = mqprio_qopt->max_rate[i];
7656 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7657 		sum_max_rate += max_rate;
7658 
7659 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7660 			break;
7661 		if (mqprio_qopt->qopt.offset[i + 1] !=
7662 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7663 			return -EINVAL;
7664 	}
7665 	if (vsi->num_queue_pairs <
7666 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7667 		dev_err(&vsi->back->pdev->dev,
7668 			"Failed to create traffic channel, insufficient number of queues.\n");
7669 		return -EINVAL;
7670 	}
7671 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7672 		dev_err(&vsi->back->pdev->dev,
7673 			"Invalid max tx rate specified\n");
7674 		return -EINVAL;
7675 	}
7676 	return 0;
7677 }
7678 
7679 /**
7680  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7681  * @vsi: the VSI being configured
7682  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7683 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7684 {
7685 	u16 qcount;
7686 	int i;
7687 
7688 	/* Only TC0 is enabled */
7689 	vsi->tc_config.numtc = 1;
7690 	vsi->tc_config.enabled_tc = 1;
7691 	qcount = min_t(int, vsi->alloc_queue_pairs,
7692 		       i40e_pf_get_max_q_per_tc(vsi->back));
7693 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7694 		/* For the TC that is not enabled set the offset to default
7695 		 * queue and allocate one queue for the given TC.
7696 		 */
7697 		vsi->tc_config.tc_info[i].qoffset = 0;
7698 		if (i == 0)
7699 			vsi->tc_config.tc_info[i].qcount = qcount;
7700 		else
7701 			vsi->tc_config.tc_info[i].qcount = 1;
7702 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7703 	}
7704 }
7705 
7706 /**
7707  * i40e_del_macvlan_filter
7708  * @hw: pointer to the HW structure
7709  * @seid: seid of the channel VSI
7710  * @macaddr: the mac address to apply as a filter
7711  * @aq_err: store the admin Q error
7712  *
7713  * This function deletes a mac filter on the channel VSI which serves as the
7714  * macvlan. Returns 0 on success.
7715  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7716 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7717 				   const u8 *macaddr, int *aq_err)
7718 {
7719 	struct i40e_aqc_remove_macvlan_element_data element;
7720 	int status;
7721 
7722 	memset(&element, 0, sizeof(element));
7723 	ether_addr_copy(element.mac_addr, macaddr);
7724 	element.vlan_tag = 0;
7725 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7726 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7727 	*aq_err = hw->aq.asq_last_status;
7728 
7729 	return status;
7730 }
7731 
7732 /**
7733  * i40e_add_macvlan_filter
7734  * @hw: pointer to the HW structure
7735  * @seid: seid of the channel VSI
7736  * @macaddr: the mac address to apply as a filter
7737  * @aq_err: store the admin Q error
7738  *
7739  * This function adds a mac filter on the channel VSI which serves as the
7740  * macvlan. Returns 0 on success.
7741  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7742 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7743 				   const u8 *macaddr, int *aq_err)
7744 {
7745 	struct i40e_aqc_add_macvlan_element_data element;
7746 	u16 cmd_flags = 0;
7747 	int status;
7748 
7749 	ether_addr_copy(element.mac_addr, macaddr);
7750 	element.vlan_tag = 0;
7751 	element.queue_number = 0;
7752 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7753 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7754 	element.flags = cpu_to_le16(cmd_flags);
7755 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7756 	*aq_err = hw->aq.asq_last_status;
7757 
7758 	return status;
7759 }
7760 
7761 /**
7762  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7763  * @vsi: the VSI we want to access
7764  * @ch: the channel we want to access
7765  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7766 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7767 {
7768 	struct i40e_ring *tx_ring, *rx_ring;
7769 	u16 pf_q;
7770 	int i;
7771 
7772 	for (i = 0; i < ch->num_queue_pairs; i++) {
7773 		pf_q = ch->base_queue + i;
7774 		tx_ring = vsi->tx_rings[pf_q];
7775 		tx_ring->ch = NULL;
7776 		rx_ring = vsi->rx_rings[pf_q];
7777 		rx_ring->ch = NULL;
7778 	}
7779 }
7780 
7781 /**
7782  * i40e_free_macvlan_channels
7783  * @vsi: the VSI we want to access
7784  *
7785  * This function frees the Qs of the channel VSI from
7786  * the stack and also deletes the channel VSIs which
7787  * serve as macvlans.
7788  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7789 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7790 {
7791 	struct i40e_channel *ch, *ch_tmp;
7792 	int ret;
7793 
7794 	if (list_empty(&vsi->macvlan_list))
7795 		return;
7796 
7797 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7798 		struct i40e_vsi *parent_vsi;
7799 
7800 		if (i40e_is_channel_macvlan(ch)) {
7801 			i40e_reset_ch_rings(vsi, ch);
7802 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7803 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7804 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7805 			kfree(ch->fwd);
7806 			ch->fwd = NULL;
7807 		}
7808 
7809 		list_del(&ch->list);
7810 		parent_vsi = ch->parent_vsi;
7811 		if (!parent_vsi || !ch->initialized) {
7812 			kfree(ch);
7813 			continue;
7814 		}
7815 
7816 		/* remove the VSI */
7817 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7818 					     NULL);
7819 		if (ret)
7820 			dev_err(&vsi->back->pdev->dev,
7821 				"unable to remove channel (%d) for parent VSI(%d)\n",
7822 				ch->seid, parent_vsi->seid);
7823 		kfree(ch);
7824 	}
7825 	vsi->macvlan_cnt = 0;
7826 }
7827 
7828 /**
7829  * i40e_fwd_ring_up - bring the macvlan device up
7830  * @vsi: the VSI we want to access
7831  * @vdev: macvlan netdevice
7832  * @fwd: the private fwd structure
7833  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7834 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7835 			    struct i40e_fwd_adapter *fwd)
7836 {
7837 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7838 	int ret = 0, num_tc = 1,  i, aq_err;
7839 	struct i40e_pf *pf = vsi->back;
7840 	struct i40e_hw *hw = &pf->hw;
7841 
7842 	/* Go through the list and find an available channel */
7843 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7844 		if (!i40e_is_channel_macvlan(iter)) {
7845 			iter->fwd = fwd;
7846 			/* record configuration for macvlan interface in vdev */
7847 			for (i = 0; i < num_tc; i++)
7848 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7849 							     i,
7850 							     iter->num_queue_pairs,
7851 							     iter->base_queue);
7852 			for (i = 0; i < iter->num_queue_pairs; i++) {
7853 				struct i40e_ring *tx_ring, *rx_ring;
7854 				u16 pf_q;
7855 
7856 				pf_q = iter->base_queue + i;
7857 
7858 				/* Get to TX ring ptr */
7859 				tx_ring = vsi->tx_rings[pf_q];
7860 				tx_ring->ch = iter;
7861 
7862 				/* Get the RX ring ptr */
7863 				rx_ring = vsi->rx_rings[pf_q];
7864 				rx_ring->ch = iter;
7865 			}
7866 			ch = iter;
7867 			break;
7868 		}
7869 	}
7870 
7871 	if (!ch)
7872 		return -EINVAL;
7873 
7874 	/* Guarantee all rings are updated before we update the
7875 	 * MAC address filter.
7876 	 */
7877 	wmb();
7878 
7879 	/* Add a mac filter */
7880 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7881 	if (ret) {
7882 		/* if we cannot add the MAC rule then disable the offload */
7883 		macvlan_release_l2fw_offload(vdev);
7884 		for (i = 0; i < ch->num_queue_pairs; i++) {
7885 			struct i40e_ring *rx_ring;
7886 			u16 pf_q;
7887 
7888 			pf_q = ch->base_queue + i;
7889 			rx_ring = vsi->rx_rings[pf_q];
7890 			rx_ring->netdev = NULL;
7891 		}
7892 		dev_info(&pf->pdev->dev,
7893 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7894 			  ERR_PTR(ret),
7895 			  i40e_aq_str(hw, aq_err));
7896 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7897 	}
7898 
7899 	return ret;
7900 }
7901 
7902 /**
7903  * i40e_setup_macvlans - create the channels which will be macvlans
7904  * @vsi: the VSI we want to access
7905  * @macvlan_cnt: no. of macvlans to be setup
7906  * @qcnt: no. of Qs per macvlan
7907  * @vdev: macvlan netdevice
7908  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7909 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7910 			       struct net_device *vdev)
7911 {
7912 	struct i40e_pf *pf = vsi->back;
7913 	struct i40e_hw *hw = &pf->hw;
7914 	struct i40e_vsi_context ctxt;
7915 	u16 sections, qmap, num_qps;
7916 	struct i40e_channel *ch;
7917 	int i, pow, ret = 0;
7918 	u8 offset = 0;
7919 
7920 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7921 		return -EINVAL;
7922 
7923 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7924 
7925 	/* find the next higher power-of-2 of num queue pairs */
7926 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7927 
7928 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7929 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7930 
7931 	/* Setup context bits for the main VSI */
7932 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7933 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7934 	memset(&ctxt, 0, sizeof(ctxt));
7935 	ctxt.seid = vsi->seid;
7936 	ctxt.pf_num = vsi->back->hw.pf_id;
7937 	ctxt.vf_num = 0;
7938 	ctxt.uplink_seid = vsi->uplink_seid;
7939 	ctxt.info = vsi->info;
7940 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7941 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7942 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7943 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7944 
7945 	/* Reconfigure RSS for main VSI with new max queue count */
7946 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7947 	ret = i40e_vsi_config_rss(vsi);
7948 	if (ret) {
7949 		dev_info(&pf->pdev->dev,
7950 			 "Failed to reconfig RSS for num_queues (%u)\n",
7951 			 vsi->rss_size);
7952 		return ret;
7953 	}
7954 	vsi->reconfig_rss = true;
7955 	dev_dbg(&vsi->back->pdev->dev,
7956 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7957 	vsi->next_base_queue = num_qps;
7958 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7959 
7960 	/* Update the VSI after updating the VSI queue-mapping
7961 	 * information
7962 	 */
7963 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7964 	if (ret) {
7965 		dev_info(&pf->pdev->dev,
7966 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7967 			 ERR_PTR(ret),
7968 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7969 		return ret;
7970 	}
7971 	/* update the local VSI info with updated queue map */
7972 	i40e_vsi_update_queue_map(vsi, &ctxt);
7973 	vsi->info.valid_sections = 0;
7974 
7975 	/* Create channels for macvlans */
7976 	INIT_LIST_HEAD(&vsi->macvlan_list);
7977 	for (i = 0; i < macvlan_cnt; i++) {
7978 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7979 		if (!ch) {
7980 			ret = -ENOMEM;
7981 			goto err_free;
7982 		}
7983 		INIT_LIST_HEAD(&ch->list);
7984 		ch->num_queue_pairs = qcnt;
7985 		if (!i40e_setup_channel(pf, vsi, ch)) {
7986 			ret = -EINVAL;
7987 			kfree(ch);
7988 			goto err_free;
7989 		}
7990 		ch->parent_vsi = vsi;
7991 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7992 		vsi->macvlan_cnt++;
7993 		list_add_tail(&ch->list, &vsi->macvlan_list);
7994 	}
7995 
7996 	return ret;
7997 
7998 err_free:
7999 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
8000 	i40e_free_macvlan_channels(vsi);
8001 
8002 	return ret;
8003 }
8004 
8005 /**
8006  * i40e_fwd_add - configure macvlans
8007  * @netdev: net device to configure
8008  * @vdev: macvlan netdevice
8009  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)8010 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
8011 {
8012 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8013 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
8014 	struct i40e_vsi *vsi = np->vsi;
8015 	struct i40e_pf *pf = vsi->back;
8016 	struct i40e_fwd_adapter *fwd;
8017 	int avail_macvlan, ret;
8018 
8019 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
8020 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
8021 		return ERR_PTR(-EINVAL);
8022 	}
8023 	if (i40e_is_tc_mqprio_enabled(pf)) {
8024 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
8025 		return ERR_PTR(-EINVAL);
8026 	}
8027 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
8028 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
8029 		return ERR_PTR(-EINVAL);
8030 	}
8031 
8032 	/* The macvlan device has to be a single Q device so that the
8033 	 * tc_to_txq field can be reused to pick the tx queue.
8034 	 */
8035 	if (netif_is_multiqueue(vdev))
8036 		return ERR_PTR(-ERANGE);
8037 
8038 	if (!vsi->macvlan_cnt) {
8039 		/* reserve bit 0 for the pf device */
8040 		set_bit(0, vsi->fwd_bitmask);
8041 
8042 		/* Try to reserve as many queues as possible for macvlans. First
8043 		 * reserve 3/4th of max vectors, then half, then quarter and
8044 		 * calculate Qs per macvlan as you go
8045 		 */
8046 		vectors = pf->num_lan_msix;
8047 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8048 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8049 			q_per_macvlan = 4;
8050 			macvlan_cnt = (vectors - 32) / 4;
8051 		} else if (vectors <= 64 && vectors > 32) {
8052 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8053 			q_per_macvlan = 2;
8054 			macvlan_cnt = (vectors - 16) / 2;
8055 		} else if (vectors <= 32 && vectors > 16) {
8056 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8057 			q_per_macvlan = 1;
8058 			macvlan_cnt = vectors - 16;
8059 		} else if (vectors <= 16 && vectors > 8) {
8060 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8061 			q_per_macvlan = 1;
8062 			macvlan_cnt = vectors - 8;
8063 		} else {
8064 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8065 			q_per_macvlan = 1;
8066 			macvlan_cnt = vectors - 1;
8067 		}
8068 
8069 		if (macvlan_cnt == 0)
8070 			return ERR_PTR(-EBUSY);
8071 
8072 		/* Quiesce VSI queues */
8073 		i40e_quiesce_vsi(vsi);
8074 
8075 		/* sets up the macvlans but does not "enable" them */
8076 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8077 					  vdev);
8078 		if (ret)
8079 			return ERR_PTR(ret);
8080 
8081 		/* Unquiesce VSI */
8082 		i40e_unquiesce_vsi(vsi);
8083 	}
8084 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8085 					    vsi->macvlan_cnt);
8086 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8087 		return ERR_PTR(-EBUSY);
8088 
8089 	/* create the fwd struct */
8090 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8091 	if (!fwd)
8092 		return ERR_PTR(-ENOMEM);
8093 
8094 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8095 	fwd->bit_no = avail_macvlan;
8096 	netdev_set_sb_channel(vdev, avail_macvlan);
8097 	fwd->netdev = vdev;
8098 
8099 	if (!netif_running(netdev))
8100 		return fwd;
8101 
8102 	/* Set fwd ring up */
8103 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8104 	if (ret) {
8105 		/* unbind the queues and drop the subordinate channel config */
8106 		netdev_unbind_sb_channel(netdev, vdev);
8107 		netdev_set_sb_channel(vdev, 0);
8108 
8109 		kfree(fwd);
8110 		return ERR_PTR(-EINVAL);
8111 	}
8112 
8113 	return fwd;
8114 }
8115 
8116 /**
8117  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8118  * @vsi: the VSI we want to access
8119  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)8120 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8121 {
8122 	struct i40e_channel *ch, *ch_tmp;
8123 	struct i40e_pf *pf = vsi->back;
8124 	struct i40e_hw *hw = &pf->hw;
8125 	int aq_err, ret = 0;
8126 
8127 	if (list_empty(&vsi->macvlan_list))
8128 		return;
8129 
8130 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8131 		if (i40e_is_channel_macvlan(ch)) {
8132 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8133 						      i40e_channel_mac(ch),
8134 						      &aq_err);
8135 			if (!ret) {
8136 				/* Reset queue contexts */
8137 				i40e_reset_ch_rings(vsi, ch);
8138 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8139 				netdev_unbind_sb_channel(vsi->netdev,
8140 							 ch->fwd->netdev);
8141 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8142 				kfree(ch->fwd);
8143 				ch->fwd = NULL;
8144 			}
8145 		}
8146 	}
8147 }
8148 
8149 /**
8150  * i40e_fwd_del - delete macvlan interfaces
8151  * @netdev: net device to configure
8152  * @vdev: macvlan netdevice
8153  */
i40e_fwd_del(struct net_device * netdev,void * vdev)8154 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8155 {
8156 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8157 	struct i40e_fwd_adapter *fwd = vdev;
8158 	struct i40e_channel *ch, *ch_tmp;
8159 	struct i40e_vsi *vsi = np->vsi;
8160 	struct i40e_pf *pf = vsi->back;
8161 	struct i40e_hw *hw = &pf->hw;
8162 	int aq_err, ret = 0;
8163 
8164 	/* Find the channel associated with the macvlan and del mac filter */
8165 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8166 		if (i40e_is_channel_macvlan(ch) &&
8167 		    ether_addr_equal(i40e_channel_mac(ch),
8168 				     fwd->netdev->dev_addr)) {
8169 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8170 						      i40e_channel_mac(ch),
8171 						      &aq_err);
8172 			if (!ret) {
8173 				/* Reset queue contexts */
8174 				i40e_reset_ch_rings(vsi, ch);
8175 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8176 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8177 				netdev_set_sb_channel(fwd->netdev, 0);
8178 				kfree(ch->fwd);
8179 				ch->fwd = NULL;
8180 			} else {
8181 				dev_info(&pf->pdev->dev,
8182 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8183 					  ERR_PTR(ret),
8184 					  i40e_aq_str(hw, aq_err));
8185 			}
8186 			break;
8187 		}
8188 	}
8189 }
8190 
8191 /**
8192  * i40e_setup_tc - configure multiple traffic classes
8193  * @netdev: net device to configure
8194  * @type_data: tc offload data
8195  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)8196 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8197 {
8198 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8199 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8200 	struct i40e_vsi *vsi = np->vsi;
8201 	struct i40e_pf *pf = vsi->back;
8202 	u8 enabled_tc = 0, num_tc, hw;
8203 	bool need_reset = false;
8204 	int old_queue_pairs;
8205 	int ret = -EINVAL;
8206 	u16 mode;
8207 	int i;
8208 
8209 	old_queue_pairs = vsi->num_queue_pairs;
8210 	num_tc = mqprio_qopt->qopt.num_tc;
8211 	hw = mqprio_qopt->qopt.hw;
8212 	mode = mqprio_qopt->mode;
8213 	if (!hw) {
8214 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8215 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8216 		goto config_tc;
8217 	}
8218 
8219 	/* Check if MFP enabled */
8220 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8221 		netdev_info(netdev,
8222 			    "Configuring TC not supported in MFP mode\n");
8223 		return ret;
8224 	}
8225 	switch (mode) {
8226 	case TC_MQPRIO_MODE_DCB:
8227 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8228 
8229 		/* Check if DCB enabled to continue */
8230 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8231 			netdev_info(netdev,
8232 				    "DCB is not enabled for adapter\n");
8233 			return ret;
8234 		}
8235 
8236 		/* Check whether tc count is within enabled limit */
8237 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8238 			netdev_info(netdev,
8239 				    "TC count greater than enabled on link for adapter\n");
8240 			return ret;
8241 		}
8242 		break;
8243 	case TC_MQPRIO_MODE_CHANNEL:
8244 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8245 			netdev_info(netdev,
8246 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8247 			return ret;
8248 		}
8249 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8250 			return ret;
8251 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8252 		if (ret)
8253 			return ret;
8254 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8255 		       sizeof(*mqprio_qopt));
8256 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8257 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8258 		break;
8259 	default:
8260 		return -EINVAL;
8261 	}
8262 
8263 config_tc:
8264 	/* Generate TC map for number of tc requested */
8265 	for (i = 0; i < num_tc; i++)
8266 		enabled_tc |= BIT(i);
8267 
8268 	/* Requesting same TC configuration as already enabled */
8269 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8270 	    mode != TC_MQPRIO_MODE_CHANNEL)
8271 		return 0;
8272 
8273 	/* Quiesce VSI queues */
8274 	i40e_quiesce_vsi(vsi);
8275 
8276 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8277 		i40e_remove_queue_channels(vsi);
8278 
8279 	/* Configure VSI for enabled TCs */
8280 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8281 	if (ret) {
8282 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8283 			    vsi->seid);
8284 		need_reset = true;
8285 		goto exit;
8286 	} else if (enabled_tc &&
8287 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8288 		netdev_info(netdev,
8289 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8290 			    vsi->tc_config.tc_info[0].qcount);
8291 		ret = -EINVAL;
8292 		need_reset = true;
8293 		goto exit;
8294 	}
8295 
8296 	dev_info(&vsi->back->pdev->dev,
8297 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8298 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8299 
8300 	if (i40e_is_tc_mqprio_enabled(pf)) {
8301 		if (vsi->mqprio_qopt.max_rate[0]) {
8302 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8303 						  vsi->mqprio_qopt.max_rate[0]);
8304 
8305 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8306 			if (!ret) {
8307 				u64 credits = max_tx_rate;
8308 
8309 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8310 				dev_dbg(&vsi->back->pdev->dev,
8311 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8312 					max_tx_rate,
8313 					credits,
8314 					vsi->seid);
8315 			} else {
8316 				need_reset = true;
8317 				goto exit;
8318 			}
8319 		}
8320 		ret = i40e_configure_queue_channels(vsi);
8321 		if (ret) {
8322 			vsi->num_queue_pairs = old_queue_pairs;
8323 			netdev_info(netdev,
8324 				    "Failed configuring queue channels\n");
8325 			need_reset = true;
8326 			goto exit;
8327 		}
8328 	}
8329 
8330 exit:
8331 	/* Reset the configuration data to defaults, only TC0 is enabled */
8332 	if (need_reset) {
8333 		i40e_vsi_set_default_tc_config(vsi);
8334 		need_reset = false;
8335 	}
8336 
8337 	/* Unquiesce VSI */
8338 	i40e_unquiesce_vsi(vsi);
8339 	return ret;
8340 }
8341 
8342 /**
8343  * i40e_set_cld_element - sets cloud filter element data
8344  * @filter: cloud filter rule
8345  * @cld: ptr to cloud filter element data
8346  *
8347  * This is helper function to copy data into cloud filter element
8348  **/
8349 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)8350 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8351 		     struct i40e_aqc_cloud_filters_element_data *cld)
8352 {
8353 	u32 ipa;
8354 	int i;
8355 
8356 	memset(cld, 0, sizeof(*cld));
8357 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8358 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8359 
8360 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8361 		return;
8362 
8363 	if (filter->n_proto == ETH_P_IPV6) {
8364 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8365 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8366 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8367 
8368 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8369 		}
8370 	} else {
8371 		ipa = be32_to_cpu(filter->dst_ipv4);
8372 
8373 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8374 	}
8375 
8376 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8377 
8378 	/* tenant_id is not supported by FW now, once the support is enabled
8379 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8380 	 */
8381 	if (filter->tenant_id)
8382 		return;
8383 }
8384 
8385 /**
8386  * i40e_add_del_cloud_filter - Add/del cloud filter
8387  * @vsi: pointer to VSI
8388  * @filter: cloud filter rule
8389  * @add: if true, add, if false, delete
8390  *
8391  * Add or delete a cloud filter for a specific flow spec.
8392  * Returns 0 if the filter were successfully added.
8393  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8394 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8395 			      struct i40e_cloud_filter *filter, bool add)
8396 {
8397 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8398 	struct i40e_pf *pf = vsi->back;
8399 	int ret;
8400 	static const u16 flag_table[128] = {
8401 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8402 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8403 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8404 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8405 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8406 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8407 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8408 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8409 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8410 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8411 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8412 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8413 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8414 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8415 	};
8416 
8417 	if (filter->flags >= ARRAY_SIZE(flag_table))
8418 		return -EIO;
8419 
8420 	memset(&cld_filter, 0, sizeof(cld_filter));
8421 
8422 	/* copy element needed to add cloud filter from filter */
8423 	i40e_set_cld_element(filter, &cld_filter);
8424 
8425 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8426 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8427 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8428 
8429 	if (filter->n_proto == ETH_P_IPV6)
8430 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8431 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8432 	else
8433 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8434 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8435 
8436 	if (add)
8437 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8438 						&cld_filter, 1);
8439 	else
8440 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8441 						&cld_filter, 1);
8442 	if (ret)
8443 		dev_dbg(&pf->pdev->dev,
8444 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8445 			add ? "add" : "delete", filter->dst_port, ret,
8446 			pf->hw.aq.asq_last_status);
8447 	else
8448 		dev_info(&pf->pdev->dev,
8449 			 "%s cloud filter for VSI: %d\n",
8450 			 add ? "Added" : "Deleted", filter->seid);
8451 	return ret;
8452 }
8453 
8454 /**
8455  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8456  * @vsi: pointer to VSI
8457  * @filter: cloud filter rule
8458  * @add: if true, add, if false, delete
8459  *
8460  * Add or delete a cloud filter for a specific flow spec using big buffer.
8461  * Returns 0 if the filter were successfully added.
8462  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8463 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8464 				      struct i40e_cloud_filter *filter,
8465 				      bool add)
8466 {
8467 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8468 	struct i40e_pf *pf = vsi->back;
8469 	int ret;
8470 
8471 	/* Both (src/dst) valid mac_addr are not supported */
8472 	if ((is_valid_ether_addr(filter->dst_mac) &&
8473 	     is_valid_ether_addr(filter->src_mac)) ||
8474 	    (is_multicast_ether_addr(filter->dst_mac) &&
8475 	     is_multicast_ether_addr(filter->src_mac)))
8476 		return -EOPNOTSUPP;
8477 
8478 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8479 	 * ports are not supported via big buffer now.
8480 	 */
8481 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8482 		return -EOPNOTSUPP;
8483 
8484 	/* adding filter using src_port/src_ip is not supported at this stage */
8485 	if (filter->src_port ||
8486 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8487 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8488 		return -EOPNOTSUPP;
8489 
8490 	memset(&cld_filter, 0, sizeof(cld_filter));
8491 
8492 	/* copy element needed to add cloud filter from filter */
8493 	i40e_set_cld_element(filter, &cld_filter.element);
8494 
8495 	if (is_valid_ether_addr(filter->dst_mac) ||
8496 	    is_valid_ether_addr(filter->src_mac) ||
8497 	    is_multicast_ether_addr(filter->dst_mac) ||
8498 	    is_multicast_ether_addr(filter->src_mac)) {
8499 		/* MAC + IP : unsupported mode */
8500 		if (filter->dst_ipv4)
8501 			return -EOPNOTSUPP;
8502 
8503 		/* since we validated that L4 port must be valid before
8504 		 * we get here, start with respective "flags" value
8505 		 * and update if vlan is present or not
8506 		 */
8507 		cld_filter.element.flags =
8508 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8509 
8510 		if (filter->vlan_id) {
8511 			cld_filter.element.flags =
8512 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8513 		}
8514 
8515 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8516 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8517 		cld_filter.element.flags =
8518 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8519 		if (filter->n_proto == ETH_P_IPV6)
8520 			cld_filter.element.flags |=
8521 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8522 		else
8523 			cld_filter.element.flags |=
8524 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8525 	} else {
8526 		dev_err(&pf->pdev->dev,
8527 			"either mac or ip has to be valid for cloud filter\n");
8528 		return -EINVAL;
8529 	}
8530 
8531 	/* Now copy L4 port in Byte 6..7 in general fields */
8532 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8533 						be16_to_cpu(filter->dst_port);
8534 
8535 	if (add) {
8536 		/* Validate current device switch mode, change if necessary */
8537 		ret = i40e_validate_and_set_switch_mode(vsi);
8538 		if (ret) {
8539 			dev_err(&pf->pdev->dev,
8540 				"failed to set switch mode, ret %d\n",
8541 				ret);
8542 			return ret;
8543 		}
8544 
8545 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8546 						   &cld_filter, 1);
8547 	} else {
8548 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8549 						   &cld_filter, 1);
8550 	}
8551 
8552 	if (ret)
8553 		dev_dbg(&pf->pdev->dev,
8554 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8555 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8556 	else
8557 		dev_info(&pf->pdev->dev,
8558 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8559 			 add ? "add" : "delete", filter->seid,
8560 			 ntohs(filter->dst_port));
8561 	return ret;
8562 }
8563 
8564 /**
8565  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8566  * @vsi: Pointer to VSI
8567  * @f: Pointer to struct flow_cls_offload
8568  * @filter: Pointer to cloud filter structure
8569  *
8570  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)8571 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8572 				 struct flow_cls_offload *f,
8573 				 struct i40e_cloud_filter *filter)
8574 {
8575 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8576 	struct flow_dissector *dissector = rule->match.dissector;
8577 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8578 	struct i40e_pf *pf = vsi->back;
8579 	u8 field_flags = 0;
8580 
8581 	if (dissector->used_keys &
8582 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8583 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8584 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8585 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8586 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8587 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8588 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8589 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8590 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8591 			dissector->used_keys);
8592 		return -EOPNOTSUPP;
8593 	}
8594 
8595 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8596 		struct flow_match_enc_keyid match;
8597 
8598 		flow_rule_match_enc_keyid(rule, &match);
8599 		if (match.mask->keyid != 0)
8600 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8601 
8602 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8603 	}
8604 
8605 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8606 		struct flow_match_basic match;
8607 
8608 		flow_rule_match_basic(rule, &match);
8609 		n_proto_key = ntohs(match.key->n_proto);
8610 		n_proto_mask = ntohs(match.mask->n_proto);
8611 
8612 		if (n_proto_key == ETH_P_ALL) {
8613 			n_proto_key = 0;
8614 			n_proto_mask = 0;
8615 		}
8616 		filter->n_proto = n_proto_key & n_proto_mask;
8617 		filter->ip_proto = match.key->ip_proto;
8618 	}
8619 
8620 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8621 		struct flow_match_eth_addrs match;
8622 
8623 		flow_rule_match_eth_addrs(rule, &match);
8624 
8625 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8626 		if (!is_zero_ether_addr(match.mask->dst)) {
8627 			if (is_broadcast_ether_addr(match.mask->dst)) {
8628 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8629 			} else {
8630 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8631 					match.mask->dst);
8632 				return -EIO;
8633 			}
8634 		}
8635 
8636 		if (!is_zero_ether_addr(match.mask->src)) {
8637 			if (is_broadcast_ether_addr(match.mask->src)) {
8638 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8639 			} else {
8640 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8641 					match.mask->src);
8642 				return -EIO;
8643 			}
8644 		}
8645 		ether_addr_copy(filter->dst_mac, match.key->dst);
8646 		ether_addr_copy(filter->src_mac, match.key->src);
8647 	}
8648 
8649 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8650 		struct flow_match_vlan match;
8651 
8652 		flow_rule_match_vlan(rule, &match);
8653 		if (match.mask->vlan_id) {
8654 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8655 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8656 
8657 			} else {
8658 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8659 					match.mask->vlan_id);
8660 				return -EIO;
8661 			}
8662 		}
8663 
8664 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8665 	}
8666 
8667 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8668 		struct flow_match_control match;
8669 
8670 		flow_rule_match_control(rule, &match);
8671 		addr_type = match.key->addr_type;
8672 	}
8673 
8674 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8675 		struct flow_match_ipv4_addrs match;
8676 
8677 		flow_rule_match_ipv4_addrs(rule, &match);
8678 		if (match.mask->dst) {
8679 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8680 				field_flags |= I40E_CLOUD_FIELD_IIP;
8681 			} else {
8682 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8683 					&match.mask->dst);
8684 				return -EIO;
8685 			}
8686 		}
8687 
8688 		if (match.mask->src) {
8689 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8690 				field_flags |= I40E_CLOUD_FIELD_IIP;
8691 			} else {
8692 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8693 					&match.mask->src);
8694 				return -EIO;
8695 			}
8696 		}
8697 
8698 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8699 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8700 			return -EIO;
8701 		}
8702 		filter->dst_ipv4 = match.key->dst;
8703 		filter->src_ipv4 = match.key->src;
8704 	}
8705 
8706 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8707 		struct flow_match_ipv6_addrs match;
8708 
8709 		flow_rule_match_ipv6_addrs(rule, &match);
8710 
8711 		/* src and dest IPV6 address should not be LOOPBACK
8712 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8713 		 */
8714 		if (ipv6_addr_loopback(&match.key->dst) ||
8715 		    ipv6_addr_loopback(&match.key->src)) {
8716 			dev_err(&pf->pdev->dev,
8717 				"Bad ipv6, addr is LOOPBACK\n");
8718 			return -EIO;
8719 		}
8720 		if (!ipv6_addr_any(&match.mask->dst) ||
8721 		    !ipv6_addr_any(&match.mask->src))
8722 			field_flags |= I40E_CLOUD_FIELD_IIP;
8723 
8724 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8725 		       sizeof(filter->src_ipv6));
8726 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8727 		       sizeof(filter->dst_ipv6));
8728 	}
8729 
8730 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8731 		struct flow_match_ports match;
8732 
8733 		flow_rule_match_ports(rule, &match);
8734 		if (match.mask->src) {
8735 			if (match.mask->src == cpu_to_be16(0xffff)) {
8736 				field_flags |= I40E_CLOUD_FIELD_IIP;
8737 			} else {
8738 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8739 					be16_to_cpu(match.mask->src));
8740 				return -EIO;
8741 			}
8742 		}
8743 
8744 		if (match.mask->dst) {
8745 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8746 				field_flags |= I40E_CLOUD_FIELD_IIP;
8747 			} else {
8748 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8749 					be16_to_cpu(match.mask->dst));
8750 				return -EIO;
8751 			}
8752 		}
8753 
8754 		filter->dst_port = match.key->dst;
8755 		filter->src_port = match.key->src;
8756 
8757 		switch (filter->ip_proto) {
8758 		case IPPROTO_TCP:
8759 		case IPPROTO_UDP:
8760 			break;
8761 		default:
8762 			dev_err(&pf->pdev->dev,
8763 				"Only UDP and TCP transport are supported\n");
8764 			return -EINVAL;
8765 		}
8766 	}
8767 	filter->flags = field_flags;
8768 	return 0;
8769 }
8770 
8771 /**
8772  * i40e_handle_tclass: Forward to a traffic class on the device
8773  * @vsi: Pointer to VSI
8774  * @tc: traffic class index on the device
8775  * @filter: Pointer to cloud filter structure
8776  *
8777  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8778 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8779 			      struct i40e_cloud_filter *filter)
8780 {
8781 	struct i40e_channel *ch, *ch_tmp;
8782 
8783 	/* direct to a traffic class on the same device */
8784 	if (tc == 0) {
8785 		filter->seid = vsi->seid;
8786 		return 0;
8787 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8788 		if (!filter->dst_port) {
8789 			dev_err(&vsi->back->pdev->dev,
8790 				"Specify destination port to direct to traffic class that is not default\n");
8791 			return -EINVAL;
8792 		}
8793 		if (list_empty(&vsi->ch_list))
8794 			return -EINVAL;
8795 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8796 					 list) {
8797 			if (ch->seid == vsi->tc_seid_map[tc])
8798 				filter->seid = ch->seid;
8799 		}
8800 		return 0;
8801 	}
8802 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8803 	return -EINVAL;
8804 }
8805 
8806 /**
8807  * i40e_configure_clsflower - Configure tc flower filters
8808  * @vsi: Pointer to VSI
8809  * @cls_flower: Pointer to struct flow_cls_offload
8810  *
8811  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8812 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8813 				    struct flow_cls_offload *cls_flower)
8814 {
8815 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8816 	struct i40e_cloud_filter *filter = NULL;
8817 	struct i40e_pf *pf = vsi->back;
8818 	int err = 0;
8819 
8820 	if (tc < 0) {
8821 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8822 		return -EOPNOTSUPP;
8823 	}
8824 
8825 	if (!tc) {
8826 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8827 		return -EINVAL;
8828 	}
8829 
8830 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8831 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8832 		return -EBUSY;
8833 
8834 	if (pf->fdir_pf_active_filters ||
8835 	    (!hlist_empty(&pf->fdir_filter_list))) {
8836 		dev_err(&vsi->back->pdev->dev,
8837 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8838 		return -EINVAL;
8839 	}
8840 
8841 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8842 		dev_err(&vsi->back->pdev->dev,
8843 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8844 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8845 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8846 	}
8847 
8848 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8849 	if (!filter)
8850 		return -ENOMEM;
8851 
8852 	filter->cookie = cls_flower->cookie;
8853 
8854 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8855 	if (err < 0)
8856 		goto err;
8857 
8858 	err = i40e_handle_tclass(vsi, tc, filter);
8859 	if (err < 0)
8860 		goto err;
8861 
8862 	/* Add cloud filter */
8863 	if (filter->dst_port)
8864 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8865 	else
8866 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8867 
8868 	if (err) {
8869 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8870 			err);
8871 		goto err;
8872 	}
8873 
8874 	/* add filter to the ordered list */
8875 	INIT_HLIST_NODE(&filter->cloud_node);
8876 
8877 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8878 
8879 	pf->num_cloud_filters++;
8880 
8881 	return err;
8882 err:
8883 	kfree(filter);
8884 	return err;
8885 }
8886 
8887 /**
8888  * i40e_find_cloud_filter - Find the could filter in the list
8889  * @vsi: Pointer to VSI
8890  * @cookie: filter specific cookie
8891  *
8892  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8893 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8894 							unsigned long *cookie)
8895 {
8896 	struct i40e_cloud_filter *filter = NULL;
8897 	struct hlist_node *node2;
8898 
8899 	hlist_for_each_entry_safe(filter, node2,
8900 				  &vsi->back->cloud_filter_list, cloud_node)
8901 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8902 			return filter;
8903 	return NULL;
8904 }
8905 
8906 /**
8907  * i40e_delete_clsflower - Remove tc flower filters
8908  * @vsi: Pointer to VSI
8909  * @cls_flower: Pointer to struct flow_cls_offload
8910  *
8911  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8912 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8913 				 struct flow_cls_offload *cls_flower)
8914 {
8915 	struct i40e_cloud_filter *filter = NULL;
8916 	struct i40e_pf *pf = vsi->back;
8917 	int err = 0;
8918 
8919 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8920 
8921 	if (!filter)
8922 		return -EINVAL;
8923 
8924 	hash_del(&filter->cloud_node);
8925 
8926 	if (filter->dst_port)
8927 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8928 	else
8929 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8930 
8931 	kfree(filter);
8932 	if (err) {
8933 		dev_err(&pf->pdev->dev,
8934 			"Failed to delete cloud filter, err %pe\n",
8935 			ERR_PTR(err));
8936 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8937 	}
8938 
8939 	pf->num_cloud_filters--;
8940 	if (!pf->num_cloud_filters)
8941 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8942 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8943 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8944 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8945 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8946 		}
8947 	return 0;
8948 }
8949 
8950 /**
8951  * i40e_setup_tc_cls_flower - flower classifier offloads
8952  * @np: net device to configure
8953  * @cls_flower: offload data
8954  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8955 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8956 				    struct flow_cls_offload *cls_flower)
8957 {
8958 	struct i40e_vsi *vsi = np->vsi;
8959 
8960 	switch (cls_flower->command) {
8961 	case FLOW_CLS_REPLACE:
8962 		return i40e_configure_clsflower(vsi, cls_flower);
8963 	case FLOW_CLS_DESTROY:
8964 		return i40e_delete_clsflower(vsi, cls_flower);
8965 	case FLOW_CLS_STATS:
8966 		return -EOPNOTSUPP;
8967 	default:
8968 		return -EOPNOTSUPP;
8969 	}
8970 }
8971 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8972 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8973 				  void *cb_priv)
8974 {
8975 	struct i40e_netdev_priv *np = cb_priv;
8976 
8977 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8978 		return -EOPNOTSUPP;
8979 
8980 	switch (type) {
8981 	case TC_SETUP_CLSFLOWER:
8982 		return i40e_setup_tc_cls_flower(np, type_data);
8983 
8984 	default:
8985 		return -EOPNOTSUPP;
8986 	}
8987 }
8988 
8989 static LIST_HEAD(i40e_block_cb_list);
8990 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8991 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8992 			   void *type_data)
8993 {
8994 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8995 
8996 	switch (type) {
8997 	case TC_SETUP_QDISC_MQPRIO:
8998 		return i40e_setup_tc(netdev, type_data);
8999 	case TC_SETUP_BLOCK:
9000 		return flow_block_cb_setup_simple(type_data,
9001 						  &i40e_block_cb_list,
9002 						  i40e_setup_tc_block_cb,
9003 						  np, np, true);
9004 	default:
9005 		return -EOPNOTSUPP;
9006 	}
9007 }
9008 
9009 /**
9010  * i40e_open - Called when a network interface is made active
9011  * @netdev: network interface device structure
9012  *
9013  * The open entry point is called when a network interface is made
9014  * active by the system (IFF_UP).  At this point all resources needed
9015  * for transmit and receive operations are allocated, the interrupt
9016  * handler is registered with the OS, the netdev watchdog subtask is
9017  * enabled, and the stack is notified that the interface is ready.
9018  *
9019  * Returns 0 on success, negative value on failure
9020  **/
i40e_open(struct net_device * netdev)9021 int i40e_open(struct net_device *netdev)
9022 {
9023 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9024 	struct i40e_vsi *vsi = np->vsi;
9025 	struct i40e_pf *pf = vsi->back;
9026 	int err;
9027 
9028 	/* disallow open during test or if eeprom is broken */
9029 	if (test_bit(__I40E_TESTING, pf->state) ||
9030 	    test_bit(__I40E_BAD_EEPROM, pf->state))
9031 		return -EBUSY;
9032 
9033 	netif_carrier_off(netdev);
9034 
9035 	if (i40e_force_link_state(pf, true))
9036 		return -EAGAIN;
9037 
9038 	err = i40e_vsi_open(vsi);
9039 	if (err)
9040 		return err;
9041 
9042 	/* configure global TSO hardware offload settings */
9043 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9044 						       TCP_FLAG_FIN) >> 16);
9045 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9046 						       TCP_FLAG_FIN |
9047 						       TCP_FLAG_CWR) >> 16);
9048 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9049 	udp_tunnel_get_rx_info(netdev);
9050 
9051 	return 0;
9052 }
9053 
9054 /**
9055  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9056  * @vsi: vsi structure
9057  *
9058  * This updates netdev's number of tx/rx queues
9059  *
9060  * Returns status of setting tx/rx queues
9061  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)9062 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9063 {
9064 	int ret;
9065 
9066 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9067 					   vsi->num_queue_pairs);
9068 	if (ret)
9069 		return ret;
9070 
9071 	return netif_set_real_num_tx_queues(vsi->netdev,
9072 					    vsi->num_queue_pairs);
9073 }
9074 
9075 /**
9076  * i40e_vsi_open -
9077  * @vsi: the VSI to open
9078  *
9079  * Finish initialization of the VSI.
9080  *
9081  * Returns 0 on success, negative value on failure
9082  *
9083  * Note: expects to be called while under rtnl_lock()
9084  **/
i40e_vsi_open(struct i40e_vsi * vsi)9085 int i40e_vsi_open(struct i40e_vsi *vsi)
9086 {
9087 	struct i40e_pf *pf = vsi->back;
9088 	char int_name[I40E_INT_NAME_STR_LEN];
9089 	int err;
9090 
9091 	/* allocate descriptors */
9092 	err = i40e_vsi_setup_tx_resources(vsi);
9093 	if (err)
9094 		goto err_setup_tx;
9095 	err = i40e_vsi_setup_rx_resources(vsi);
9096 	if (err)
9097 		goto err_setup_rx;
9098 
9099 	err = i40e_vsi_configure(vsi);
9100 	if (err)
9101 		goto err_setup_rx;
9102 
9103 	if (vsi->netdev) {
9104 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9105 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9106 		err = i40e_vsi_request_irq(vsi, int_name);
9107 		if (err)
9108 			goto err_setup_rx;
9109 
9110 		/* Notify the stack of the actual queue counts. */
9111 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9112 		if (err)
9113 			goto err_set_queues;
9114 
9115 	} else if (vsi->type == I40E_VSI_FDIR) {
9116 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9117 			 dev_driver_string(&pf->pdev->dev),
9118 			 dev_name(&pf->pdev->dev));
9119 		err = i40e_vsi_request_irq(vsi, int_name);
9120 		if (err)
9121 			goto err_setup_rx;
9122 
9123 	} else {
9124 		err = -EINVAL;
9125 		goto err_setup_rx;
9126 	}
9127 
9128 	err = i40e_up_complete(vsi);
9129 	if (err)
9130 		goto err_up_complete;
9131 
9132 	return 0;
9133 
9134 err_up_complete:
9135 	i40e_down(vsi);
9136 err_set_queues:
9137 	i40e_vsi_free_irq(vsi);
9138 err_setup_rx:
9139 	i40e_vsi_free_rx_resources(vsi);
9140 err_setup_tx:
9141 	i40e_vsi_free_tx_resources(vsi);
9142 	if (vsi == pf->vsi[pf->lan_vsi])
9143 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9144 
9145 	return err;
9146 }
9147 
9148 /**
9149  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9150  * @pf: Pointer to PF
9151  *
9152  * This function destroys the hlist where all the Flow Director
9153  * filters were saved.
9154  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)9155 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9156 {
9157 	struct i40e_fdir_filter *filter;
9158 	struct i40e_flex_pit *pit_entry, *tmp;
9159 	struct hlist_node *node2;
9160 
9161 	hlist_for_each_entry_safe(filter, node2,
9162 				  &pf->fdir_filter_list, fdir_node) {
9163 		hlist_del(&filter->fdir_node);
9164 		kfree(filter);
9165 	}
9166 
9167 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9168 		list_del(&pit_entry->list);
9169 		kfree(pit_entry);
9170 	}
9171 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9172 
9173 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9174 		list_del(&pit_entry->list);
9175 		kfree(pit_entry);
9176 	}
9177 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9178 
9179 	pf->fdir_pf_active_filters = 0;
9180 	i40e_reset_fdir_filter_cnt(pf);
9181 
9182 	/* Reprogram the default input set for TCP/IPv4 */
9183 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9184 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9185 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9186 
9187 	/* Reprogram the default input set for TCP/IPv6 */
9188 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9189 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9190 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9191 
9192 	/* Reprogram the default input set for UDP/IPv4 */
9193 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9194 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9195 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9196 
9197 	/* Reprogram the default input set for UDP/IPv6 */
9198 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9199 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9200 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9201 
9202 	/* Reprogram the default input set for SCTP/IPv4 */
9203 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9204 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9205 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9206 
9207 	/* Reprogram the default input set for SCTP/IPv6 */
9208 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9209 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9210 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9211 
9212 	/* Reprogram the default input set for Other/IPv4 */
9213 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9214 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9215 
9216 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9217 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9218 
9219 	/* Reprogram the default input set for Other/IPv6 */
9220 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9221 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9222 
9223 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9224 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9225 }
9226 
9227 /**
9228  * i40e_cloud_filter_exit - Cleans up the cloud filters
9229  * @pf: Pointer to PF
9230  *
9231  * This function destroys the hlist where all the cloud filters
9232  * were saved.
9233  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)9234 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9235 {
9236 	struct i40e_cloud_filter *cfilter;
9237 	struct hlist_node *node;
9238 
9239 	hlist_for_each_entry_safe(cfilter, node,
9240 				  &pf->cloud_filter_list, cloud_node) {
9241 		hlist_del(&cfilter->cloud_node);
9242 		kfree(cfilter);
9243 	}
9244 	pf->num_cloud_filters = 0;
9245 
9246 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9247 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9248 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9249 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9250 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9251 	}
9252 }
9253 
9254 /**
9255  * i40e_close - Disables a network interface
9256  * @netdev: network interface device structure
9257  *
9258  * The close entry point is called when an interface is de-activated
9259  * by the OS.  The hardware is still under the driver's control, but
9260  * this netdev interface is disabled.
9261  *
9262  * Returns 0, this is not allowed to fail
9263  **/
i40e_close(struct net_device * netdev)9264 int i40e_close(struct net_device *netdev)
9265 {
9266 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9267 	struct i40e_vsi *vsi = np->vsi;
9268 
9269 	i40e_vsi_close(vsi);
9270 
9271 	return 0;
9272 }
9273 
9274 /**
9275  * i40e_do_reset - Start a PF or Core Reset sequence
9276  * @pf: board private structure
9277  * @reset_flags: which reset is requested
9278  * @lock_acquired: indicates whether or not the lock has been acquired
9279  * before this function was called.
9280  *
9281  * The essential difference in resets is that the PF Reset
9282  * doesn't clear the packet buffers, doesn't reset the PE
9283  * firmware, and doesn't bother the other PFs on the chip.
9284  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)9285 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9286 {
9287 	u32 val;
9288 
9289 	/* do the biggest reset indicated */
9290 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9291 
9292 		/* Request a Global Reset
9293 		 *
9294 		 * This will start the chip's countdown to the actual full
9295 		 * chip reset event, and a warning interrupt to be sent
9296 		 * to all PFs, including the requestor.  Our handler
9297 		 * for the warning interrupt will deal with the shutdown
9298 		 * and recovery of the switch setup.
9299 		 */
9300 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9301 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9302 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9303 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9304 
9305 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9306 
9307 		/* Request a Core Reset
9308 		 *
9309 		 * Same as Global Reset, except does *not* include the MAC/PHY
9310 		 */
9311 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9312 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9313 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9314 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9315 		i40e_flush(&pf->hw);
9316 
9317 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9318 
9319 		/* Request a PF Reset
9320 		 *
9321 		 * Resets only the PF-specific registers
9322 		 *
9323 		 * This goes directly to the tear-down and rebuild of
9324 		 * the switch, since we need to do all the recovery as
9325 		 * for the Core Reset.
9326 		 */
9327 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9328 		i40e_handle_reset_warning(pf, lock_acquired);
9329 
9330 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9331 		/* Request a PF Reset
9332 		 *
9333 		 * Resets PF and reinitializes PFs VSI.
9334 		 */
9335 		i40e_prep_for_reset(pf);
9336 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9337 		dev_info(&pf->pdev->dev,
9338 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9339 			 "FW LLDP is disabled\n" :
9340 			 "FW LLDP is enabled\n");
9341 
9342 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9343 		int v;
9344 
9345 		/* Find the VSI(s) that requested a re-init */
9346 		dev_info(&pf->pdev->dev,
9347 			 "VSI reinit requested\n");
9348 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9349 			struct i40e_vsi *vsi = pf->vsi[v];
9350 
9351 			if (vsi != NULL &&
9352 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9353 					       vsi->state))
9354 				i40e_vsi_reinit_locked(pf->vsi[v]);
9355 		}
9356 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9357 		int v;
9358 
9359 		/* Find the VSI(s) that needs to be brought down */
9360 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9361 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9362 			struct i40e_vsi *vsi = pf->vsi[v];
9363 
9364 			if (vsi != NULL &&
9365 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9366 					       vsi->state)) {
9367 				set_bit(__I40E_VSI_DOWN, vsi->state);
9368 				i40e_down(vsi);
9369 			}
9370 		}
9371 	} else {
9372 		dev_info(&pf->pdev->dev,
9373 			 "bad reset request 0x%08x\n", reset_flags);
9374 	}
9375 }
9376 
9377 #ifdef CONFIG_I40E_DCB
9378 /**
9379  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9380  * @pf: board private structure
9381  * @old_cfg: current DCB config
9382  * @new_cfg: new DCB config
9383  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)9384 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9385 			    struct i40e_dcbx_config *old_cfg,
9386 			    struct i40e_dcbx_config *new_cfg)
9387 {
9388 	bool need_reconfig = false;
9389 
9390 	/* Check if ETS configuration has changed */
9391 	if (memcmp(&new_cfg->etscfg,
9392 		   &old_cfg->etscfg,
9393 		   sizeof(new_cfg->etscfg))) {
9394 		/* If Priority Table has changed reconfig is needed */
9395 		if (memcmp(&new_cfg->etscfg.prioritytable,
9396 			   &old_cfg->etscfg.prioritytable,
9397 			   sizeof(new_cfg->etscfg.prioritytable))) {
9398 			need_reconfig = true;
9399 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9400 		}
9401 
9402 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9403 			   &old_cfg->etscfg.tcbwtable,
9404 			   sizeof(new_cfg->etscfg.tcbwtable)))
9405 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9406 
9407 		if (memcmp(&new_cfg->etscfg.tsatable,
9408 			   &old_cfg->etscfg.tsatable,
9409 			   sizeof(new_cfg->etscfg.tsatable)))
9410 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9411 	}
9412 
9413 	/* Check if PFC configuration has changed */
9414 	if (memcmp(&new_cfg->pfc,
9415 		   &old_cfg->pfc,
9416 		   sizeof(new_cfg->pfc))) {
9417 		need_reconfig = true;
9418 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9419 	}
9420 
9421 	/* Check if APP Table has changed */
9422 	if (memcmp(&new_cfg->app,
9423 		   &old_cfg->app,
9424 		   sizeof(new_cfg->app))) {
9425 		need_reconfig = true;
9426 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9427 	}
9428 
9429 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9430 	return need_reconfig;
9431 }
9432 
9433 /**
9434  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9435  * @pf: board private structure
9436  * @e: event info posted on ARQ
9437  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9438 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9439 				  struct i40e_arq_event_info *e)
9440 {
9441 	struct i40e_aqc_lldp_get_mib *mib =
9442 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9443 	struct i40e_hw *hw = &pf->hw;
9444 	struct i40e_dcbx_config tmp_dcbx_cfg;
9445 	bool need_reconfig = false;
9446 	int ret = 0;
9447 	u8 type;
9448 
9449 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9450 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9451 	    (hw->phy.link_info.link_speed &
9452 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9453 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9454 		/* let firmware decide if the DCB should be disabled */
9455 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9456 
9457 	/* Not DCB capable or capability disabled */
9458 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9459 		return ret;
9460 
9461 	/* Ignore if event is not for Nearest Bridge */
9462 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9463 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9464 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9465 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9466 		return ret;
9467 
9468 	/* Check MIB Type and return if event for Remote MIB update */
9469 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9470 	dev_dbg(&pf->pdev->dev,
9471 		"LLDP event mib type %s\n", type ? "remote" : "local");
9472 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9473 		/* Update the remote cached instance and return */
9474 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9475 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9476 				&hw->remote_dcbx_config);
9477 		goto exit;
9478 	}
9479 
9480 	/* Store the old configuration */
9481 	tmp_dcbx_cfg = hw->local_dcbx_config;
9482 
9483 	/* Reset the old DCBx configuration data */
9484 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9485 	/* Get updated DCBX data from firmware */
9486 	ret = i40e_get_dcb_config(&pf->hw);
9487 	if (ret) {
9488 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9489 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9490 		    (hw->phy.link_info.link_speed &
9491 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9492 			dev_warn(&pf->pdev->dev,
9493 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9494 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9495 		} else {
9496 			dev_info(&pf->pdev->dev,
9497 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9498 				 ERR_PTR(ret),
9499 				 i40e_aq_str(&pf->hw,
9500 					     pf->hw.aq.asq_last_status));
9501 		}
9502 		goto exit;
9503 	}
9504 
9505 	/* No change detected in DCBX configs */
9506 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9507 		    sizeof(tmp_dcbx_cfg))) {
9508 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9509 		goto exit;
9510 	}
9511 
9512 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9513 					       &hw->local_dcbx_config);
9514 
9515 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9516 
9517 	if (!need_reconfig)
9518 		goto exit;
9519 
9520 	/* Enable DCB tagging only when more than one TC */
9521 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9522 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9523 	else
9524 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9525 
9526 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9527 	/* Reconfiguration needed quiesce all VSIs */
9528 	i40e_pf_quiesce_all_vsi(pf);
9529 
9530 	/* Changes in configuration update VEB/VSI */
9531 	i40e_dcb_reconfigure(pf);
9532 
9533 	ret = i40e_resume_port_tx(pf);
9534 
9535 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9536 	/* In case of error no point in resuming VSIs */
9537 	if (ret)
9538 		goto exit;
9539 
9540 	/* Wait for the PF's queues to be disabled */
9541 	ret = i40e_pf_wait_queues_disabled(pf);
9542 	if (ret) {
9543 		/* Schedule PF reset to recover */
9544 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9545 		i40e_service_event_schedule(pf);
9546 	} else {
9547 		i40e_pf_unquiesce_all_vsi(pf);
9548 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9549 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9550 	}
9551 
9552 exit:
9553 	return ret;
9554 }
9555 #endif /* CONFIG_I40E_DCB */
9556 
9557 /**
9558  * i40e_do_reset_safe - Protected reset path for userland calls.
9559  * @pf: board private structure
9560  * @reset_flags: which reset is requested
9561  *
9562  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)9563 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9564 {
9565 	rtnl_lock();
9566 	i40e_do_reset(pf, reset_flags, true);
9567 	rtnl_unlock();
9568 }
9569 
9570 /**
9571  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9572  * @pf: board private structure
9573  * @e: event info posted on ARQ
9574  *
9575  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9576  * and VF queues
9577  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9578 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9579 					   struct i40e_arq_event_info *e)
9580 {
9581 	struct i40e_aqc_lan_overflow *data =
9582 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9583 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9584 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9585 	struct i40e_hw *hw = &pf->hw;
9586 	struct i40e_vf *vf;
9587 	u16 vf_id;
9588 
9589 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9590 		queue, qtx_ctl);
9591 
9592 	/* Queue belongs to VF, find the VF and issue VF reset */
9593 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9594 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9595 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9596 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9597 		vf_id -= hw->func_caps.vf_base_id;
9598 		vf = &pf->vf[vf_id];
9599 		i40e_vc_notify_vf_reset(vf);
9600 		/* Allow VF to process pending reset notification */
9601 		msleep(20);
9602 		i40e_reset_vf(vf, false);
9603 	}
9604 }
9605 
9606 /**
9607  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9608  * @pf: board private structure
9609  **/
i40e_get_cur_guaranteed_fd_count(struct i40e_pf * pf)9610 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9611 {
9612 	u32 val, fcnt_prog;
9613 
9614 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9615 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9616 	return fcnt_prog;
9617 }
9618 
9619 /**
9620  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9621  * @pf: board private structure
9622  **/
i40e_get_current_fd_count(struct i40e_pf * pf)9623 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9624 {
9625 	u32 val, fcnt_prog;
9626 
9627 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9628 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9629 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9630 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9631 	return fcnt_prog;
9632 }
9633 
9634 /**
9635  * i40e_get_global_fd_count - Get total FD filters programmed on device
9636  * @pf: board private structure
9637  **/
i40e_get_global_fd_count(struct i40e_pf * pf)9638 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9639 {
9640 	u32 val, fcnt_prog;
9641 
9642 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9643 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9644 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9645 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9646 	return fcnt_prog;
9647 }
9648 
9649 /**
9650  * i40e_reenable_fdir_sb - Restore FDir SB capability
9651  * @pf: board private structure
9652  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)9653 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9654 {
9655 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9656 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9657 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9658 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9659 }
9660 
9661 /**
9662  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9663  * @pf: board private structure
9664  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)9665 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9666 {
9667 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9668 		/* ATR uses the same filtering logic as SB rules. It only
9669 		 * functions properly if the input set mask is at the default
9670 		 * settings. It is safe to restore the default input set
9671 		 * because there are no active TCPv4 filter rules.
9672 		 */
9673 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9674 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9675 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9676 
9677 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9678 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9679 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9680 	}
9681 }
9682 
9683 /**
9684  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9685  * @pf: board private structure
9686  * @filter: FDir filter to remove
9687  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)9688 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9689 				       struct i40e_fdir_filter *filter)
9690 {
9691 	/* Update counters */
9692 	pf->fdir_pf_active_filters--;
9693 	pf->fd_inv = 0;
9694 
9695 	switch (filter->flow_type) {
9696 	case TCP_V4_FLOW:
9697 		pf->fd_tcp4_filter_cnt--;
9698 		break;
9699 	case UDP_V4_FLOW:
9700 		pf->fd_udp4_filter_cnt--;
9701 		break;
9702 	case SCTP_V4_FLOW:
9703 		pf->fd_sctp4_filter_cnt--;
9704 		break;
9705 	case TCP_V6_FLOW:
9706 		pf->fd_tcp6_filter_cnt--;
9707 		break;
9708 	case UDP_V6_FLOW:
9709 		pf->fd_udp6_filter_cnt--;
9710 		break;
9711 	case SCTP_V6_FLOW:
9712 		pf->fd_udp6_filter_cnt--;
9713 		break;
9714 	case IP_USER_FLOW:
9715 		switch (filter->ipl4_proto) {
9716 		case IPPROTO_TCP:
9717 			pf->fd_tcp4_filter_cnt--;
9718 			break;
9719 		case IPPROTO_UDP:
9720 			pf->fd_udp4_filter_cnt--;
9721 			break;
9722 		case IPPROTO_SCTP:
9723 			pf->fd_sctp4_filter_cnt--;
9724 			break;
9725 		case IPPROTO_IP:
9726 			pf->fd_ip4_filter_cnt--;
9727 			break;
9728 		}
9729 		break;
9730 	case IPV6_USER_FLOW:
9731 		switch (filter->ipl4_proto) {
9732 		case IPPROTO_TCP:
9733 			pf->fd_tcp6_filter_cnt--;
9734 			break;
9735 		case IPPROTO_UDP:
9736 			pf->fd_udp6_filter_cnt--;
9737 			break;
9738 		case IPPROTO_SCTP:
9739 			pf->fd_sctp6_filter_cnt--;
9740 			break;
9741 		case IPPROTO_IP:
9742 			pf->fd_ip6_filter_cnt--;
9743 			break;
9744 		}
9745 		break;
9746 	}
9747 
9748 	/* Remove the filter from the list and free memory */
9749 	hlist_del(&filter->fdir_node);
9750 	kfree(filter);
9751 }
9752 
9753 /**
9754  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9755  * @pf: board private structure
9756  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9757 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9758 {
9759 	struct i40e_fdir_filter *filter;
9760 	u32 fcnt_prog, fcnt_avail;
9761 	struct hlist_node *node;
9762 
9763 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9764 		return;
9765 
9766 	/* Check if we have enough room to re-enable FDir SB capability. */
9767 	fcnt_prog = i40e_get_global_fd_count(pf);
9768 	fcnt_avail = pf->fdir_pf_filter_count;
9769 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9770 	    (pf->fd_add_err == 0) ||
9771 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9772 		i40e_reenable_fdir_sb(pf);
9773 
9774 	/* We should wait for even more space before re-enabling ATR.
9775 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9776 	 * rules active.
9777 	 */
9778 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9779 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9780 		i40e_reenable_fdir_atr(pf);
9781 
9782 	/* if hw had a problem adding a filter, delete it */
9783 	if (pf->fd_inv > 0) {
9784 		hlist_for_each_entry_safe(filter, node,
9785 					  &pf->fdir_filter_list, fdir_node)
9786 			if (filter->fd_id == pf->fd_inv)
9787 				i40e_delete_invalid_filter(pf, filter);
9788 	}
9789 }
9790 
9791 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9792 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9793 /**
9794  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9795  * @pf: board private structure
9796  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9797 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9798 {
9799 	unsigned long min_flush_time;
9800 	int flush_wait_retry = 50;
9801 	bool disable_atr = false;
9802 	int fd_room;
9803 	int reg;
9804 
9805 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9806 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9807 		return;
9808 
9809 	/* If the flush is happening too quick and we have mostly SB rules we
9810 	 * should not re-enable ATR for some time.
9811 	 */
9812 	min_flush_time = pf->fd_flush_timestamp +
9813 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9814 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9815 
9816 	if (!(time_after(jiffies, min_flush_time)) &&
9817 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9818 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9819 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9820 		disable_atr = true;
9821 	}
9822 
9823 	pf->fd_flush_timestamp = jiffies;
9824 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9825 	/* flush all filters */
9826 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9827 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9828 	i40e_flush(&pf->hw);
9829 	pf->fd_flush_cnt++;
9830 	pf->fd_add_err = 0;
9831 	do {
9832 		/* Check FD flush status every 5-6msec */
9833 		usleep_range(5000, 6000);
9834 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9835 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9836 			break;
9837 	} while (flush_wait_retry--);
9838 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9839 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9840 	} else {
9841 		/* replay sideband filters */
9842 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9843 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9844 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9845 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9846 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9847 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9848 	}
9849 }
9850 
9851 /**
9852  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9853  * @pf: board private structure
9854  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9855 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9856 {
9857 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9858 }
9859 
9860 /**
9861  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9862  * @pf: board private structure
9863  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9864 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9865 {
9866 
9867 	/* if interface is down do nothing */
9868 	if (test_bit(__I40E_DOWN, pf->state))
9869 		return;
9870 
9871 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9872 		i40e_fdir_flush_and_replay(pf);
9873 
9874 	i40e_fdir_check_and_reenable(pf);
9875 
9876 }
9877 
9878 /**
9879  * i40e_vsi_link_event - notify VSI of a link event
9880  * @vsi: vsi to be notified
9881  * @link_up: link up or down
9882  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9883 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9884 {
9885 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9886 		return;
9887 
9888 	switch (vsi->type) {
9889 	case I40E_VSI_MAIN:
9890 		if (!vsi->netdev || !vsi->netdev_registered)
9891 			break;
9892 
9893 		if (link_up) {
9894 			netif_carrier_on(vsi->netdev);
9895 			netif_tx_wake_all_queues(vsi->netdev);
9896 		} else {
9897 			netif_carrier_off(vsi->netdev);
9898 			netif_tx_stop_all_queues(vsi->netdev);
9899 		}
9900 		break;
9901 
9902 	case I40E_VSI_SRIOV:
9903 	case I40E_VSI_VMDQ2:
9904 	case I40E_VSI_CTRL:
9905 	case I40E_VSI_IWARP:
9906 	case I40E_VSI_MIRROR:
9907 	default:
9908 		/* there is no notification for other VSIs */
9909 		break;
9910 	}
9911 }
9912 
9913 /**
9914  * i40e_veb_link_event - notify elements on the veb of a link event
9915  * @veb: veb to be notified
9916  * @link_up: link up or down
9917  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9918 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9919 {
9920 	struct i40e_pf *pf;
9921 	int i;
9922 
9923 	if (!veb || !veb->pf)
9924 		return;
9925 	pf = veb->pf;
9926 
9927 	/* depth first... */
9928 	for (i = 0; i < I40E_MAX_VEB; i++)
9929 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9930 			i40e_veb_link_event(pf->veb[i], link_up);
9931 
9932 	/* ... now the local VSIs */
9933 	for (i = 0; i < pf->num_alloc_vsi; i++)
9934 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9935 			i40e_vsi_link_event(pf->vsi[i], link_up);
9936 }
9937 
9938 /**
9939  * i40e_link_event - Update netif_carrier status
9940  * @pf: board private structure
9941  **/
i40e_link_event(struct i40e_pf * pf)9942 static void i40e_link_event(struct i40e_pf *pf)
9943 {
9944 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9945 	u8 new_link_speed, old_link_speed;
9946 	bool new_link, old_link;
9947 	int status;
9948 #ifdef CONFIG_I40E_DCB
9949 	int err;
9950 #endif /* CONFIG_I40E_DCB */
9951 
9952 	/* set this to force the get_link_status call to refresh state */
9953 	pf->hw.phy.get_link_info = true;
9954 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9955 	status = i40e_get_link_status(&pf->hw, &new_link);
9956 
9957 	/* On success, disable temp link polling */
9958 	if (status == 0) {
9959 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9960 	} else {
9961 		/* Enable link polling temporarily until i40e_get_link_status
9962 		 * returns 0
9963 		 */
9964 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9965 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9966 			status);
9967 		return;
9968 	}
9969 
9970 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9971 	new_link_speed = pf->hw.phy.link_info.link_speed;
9972 
9973 	if (new_link == old_link &&
9974 	    new_link_speed == old_link_speed &&
9975 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9976 	     new_link == netif_carrier_ok(vsi->netdev)))
9977 		return;
9978 
9979 	i40e_print_link_message(vsi, new_link);
9980 
9981 	/* Notify the base of the switch tree connected to
9982 	 * the link.  Floating VEBs are not notified.
9983 	 */
9984 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9985 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9986 	else
9987 		i40e_vsi_link_event(vsi, new_link);
9988 
9989 	if (pf->vf)
9990 		i40e_vc_notify_link_state(pf);
9991 
9992 	if (pf->flags & I40E_FLAG_PTP)
9993 		i40e_ptp_set_increment(pf);
9994 #ifdef CONFIG_I40E_DCB
9995 	if (new_link == old_link)
9996 		return;
9997 	/* Not SW DCB so firmware will take care of default settings */
9998 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9999 		return;
10000 
10001 	/* We cover here only link down, as after link up in case of SW DCB
10002 	 * SW LLDP agent will take care of setting it up
10003 	 */
10004 	if (!new_link) {
10005 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
10006 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
10007 		err = i40e_dcb_sw_default_config(pf);
10008 		if (err) {
10009 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10010 				       I40E_FLAG_DCB_ENABLED);
10011 		} else {
10012 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
10013 				       DCB_CAP_DCBX_VER_IEEE;
10014 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
10015 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
10016 		}
10017 	}
10018 #endif /* CONFIG_I40E_DCB */
10019 }
10020 
10021 /**
10022  * i40e_watchdog_subtask - periodic checks not using event driven response
10023  * @pf: board private structure
10024  **/
i40e_watchdog_subtask(struct i40e_pf * pf)10025 static void i40e_watchdog_subtask(struct i40e_pf *pf)
10026 {
10027 	int i;
10028 
10029 	/* if interface is down do nothing */
10030 	if (test_bit(__I40E_DOWN, pf->state) ||
10031 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
10032 		return;
10033 
10034 	/* make sure we don't do these things too often */
10035 	if (time_before(jiffies, (pf->service_timer_previous +
10036 				  pf->service_timer_period)))
10037 		return;
10038 	pf->service_timer_previous = jiffies;
10039 
10040 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
10041 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10042 		i40e_link_event(pf);
10043 
10044 	/* Update the stats for active netdevs so the network stack
10045 	 * can look at updated numbers whenever it cares to
10046 	 */
10047 	for (i = 0; i < pf->num_alloc_vsi; i++)
10048 		if (pf->vsi[i] && pf->vsi[i]->netdev)
10049 			i40e_update_stats(pf->vsi[i]);
10050 
10051 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
10052 		/* Update the stats for the active switching components */
10053 		for (i = 0; i < I40E_MAX_VEB; i++)
10054 			if (pf->veb[i])
10055 				i40e_update_veb_stats(pf->veb[i]);
10056 	}
10057 
10058 	i40e_ptp_rx_hang(pf);
10059 	i40e_ptp_tx_hang(pf);
10060 }
10061 
10062 /**
10063  * i40e_reset_subtask - Set up for resetting the device and driver
10064  * @pf: board private structure
10065  **/
i40e_reset_subtask(struct i40e_pf * pf)10066 static void i40e_reset_subtask(struct i40e_pf *pf)
10067 {
10068 	u32 reset_flags = 0;
10069 
10070 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10071 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10072 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10073 	}
10074 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10075 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10076 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10077 	}
10078 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10079 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10080 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10081 	}
10082 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10083 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10084 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10085 	}
10086 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10087 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10088 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10089 	}
10090 
10091 	/* If there's a recovery already waiting, it takes
10092 	 * precedence before starting a new reset sequence.
10093 	 */
10094 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10095 		i40e_prep_for_reset(pf);
10096 		i40e_reset(pf);
10097 		i40e_rebuild(pf, false, false);
10098 	}
10099 
10100 	/* If we're already down or resetting, just bail */
10101 	if (reset_flags &&
10102 	    !test_bit(__I40E_DOWN, pf->state) &&
10103 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10104 		i40e_do_reset(pf, reset_flags, false);
10105 	}
10106 }
10107 
10108 /**
10109  * i40e_handle_link_event - Handle link event
10110  * @pf: board private structure
10111  * @e: event info posted on ARQ
10112  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)10113 static void i40e_handle_link_event(struct i40e_pf *pf,
10114 				   struct i40e_arq_event_info *e)
10115 {
10116 	struct i40e_aqc_get_link_status *status =
10117 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10118 
10119 	/* Do a new status request to re-enable LSE reporting
10120 	 * and load new status information into the hw struct
10121 	 * This completely ignores any state information
10122 	 * in the ARQ event info, instead choosing to always
10123 	 * issue the AQ update link status command.
10124 	 */
10125 	i40e_link_event(pf);
10126 
10127 	/* Check if module meets thermal requirements */
10128 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10129 		dev_err(&pf->pdev->dev,
10130 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10131 		dev_err(&pf->pdev->dev,
10132 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10133 	} else {
10134 		/* check for unqualified module, if link is down, suppress
10135 		 * the message if link was forced to be down.
10136 		 */
10137 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10138 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10139 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10140 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10141 			dev_err(&pf->pdev->dev,
10142 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10143 			dev_err(&pf->pdev->dev,
10144 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10145 		}
10146 	}
10147 }
10148 
10149 /**
10150  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10151  * @pf: board private structure
10152  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)10153 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10154 {
10155 	struct i40e_arq_event_info event;
10156 	struct i40e_hw *hw = &pf->hw;
10157 	u16 pending, i = 0;
10158 	u16 opcode;
10159 	u32 oldval;
10160 	int ret;
10161 	u32 val;
10162 
10163 	/* Do not run clean AQ when PF reset fails */
10164 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10165 		return;
10166 
10167 	/* check for error indications */
10168 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10169 	oldval = val;
10170 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10171 		if (hw->debug_mask & I40E_DEBUG_AQ)
10172 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10173 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10174 	}
10175 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10176 		if (hw->debug_mask & I40E_DEBUG_AQ)
10177 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10178 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10179 		pf->arq_overflows++;
10180 	}
10181 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10182 		if (hw->debug_mask & I40E_DEBUG_AQ)
10183 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10184 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10185 	}
10186 	if (oldval != val)
10187 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10188 
10189 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10190 	oldval = val;
10191 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10192 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10193 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10194 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10195 	}
10196 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10197 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10198 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10199 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10200 	}
10201 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10202 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10203 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10204 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10205 	}
10206 	if (oldval != val)
10207 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10208 
10209 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10210 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10211 	if (!event.msg_buf)
10212 		return;
10213 
10214 	do {
10215 		ret = i40e_clean_arq_element(hw, &event, &pending);
10216 		if (ret == -EALREADY)
10217 			break;
10218 		else if (ret) {
10219 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10220 			break;
10221 		}
10222 
10223 		opcode = le16_to_cpu(event.desc.opcode);
10224 		switch (opcode) {
10225 
10226 		case i40e_aqc_opc_get_link_status:
10227 			rtnl_lock();
10228 			i40e_handle_link_event(pf, &event);
10229 			rtnl_unlock();
10230 			break;
10231 		case i40e_aqc_opc_send_msg_to_pf:
10232 			ret = i40e_vc_process_vf_msg(pf,
10233 					le16_to_cpu(event.desc.retval),
10234 					le32_to_cpu(event.desc.cookie_high),
10235 					le32_to_cpu(event.desc.cookie_low),
10236 					event.msg_buf,
10237 					event.msg_len);
10238 			break;
10239 		case i40e_aqc_opc_lldp_update_mib:
10240 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10241 #ifdef CONFIG_I40E_DCB
10242 			rtnl_lock();
10243 			i40e_handle_lldp_event(pf, &event);
10244 			rtnl_unlock();
10245 #endif /* CONFIG_I40E_DCB */
10246 			break;
10247 		case i40e_aqc_opc_event_lan_overflow:
10248 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10249 			i40e_handle_lan_overflow_event(pf, &event);
10250 			break;
10251 		case i40e_aqc_opc_send_msg_to_peer:
10252 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10253 			break;
10254 		case i40e_aqc_opc_nvm_erase:
10255 		case i40e_aqc_opc_nvm_update:
10256 		case i40e_aqc_opc_oem_post_update:
10257 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10258 				   "ARQ NVM operation 0x%04x completed\n",
10259 				   opcode);
10260 			break;
10261 		default:
10262 			dev_info(&pf->pdev->dev,
10263 				 "ARQ: Unknown event 0x%04x ignored\n",
10264 				 opcode);
10265 			break;
10266 		}
10267 	} while (i++ < pf->adminq_work_limit);
10268 
10269 	if (i < pf->adminq_work_limit)
10270 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10271 
10272 	/* re-enable Admin queue interrupt cause */
10273 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10274 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10275 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10276 	i40e_flush(hw);
10277 
10278 	kfree(event.msg_buf);
10279 }
10280 
10281 /**
10282  * i40e_verify_eeprom - make sure eeprom is good to use
10283  * @pf: board private structure
10284  **/
i40e_verify_eeprom(struct i40e_pf * pf)10285 static void i40e_verify_eeprom(struct i40e_pf *pf)
10286 {
10287 	int err;
10288 
10289 	err = i40e_diag_eeprom_test(&pf->hw);
10290 	if (err) {
10291 		/* retry in case of garbage read */
10292 		err = i40e_diag_eeprom_test(&pf->hw);
10293 		if (err) {
10294 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10295 				 err);
10296 			set_bit(__I40E_BAD_EEPROM, pf->state);
10297 		}
10298 	}
10299 
10300 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10301 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10302 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10303 	}
10304 }
10305 
10306 /**
10307  * i40e_enable_pf_switch_lb
10308  * @pf: pointer to the PF structure
10309  *
10310  * enable switch loop back or die - no point in a return value
10311  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)10312 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10313 {
10314 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10315 	struct i40e_vsi_context ctxt;
10316 	int ret;
10317 
10318 	ctxt.seid = pf->main_vsi_seid;
10319 	ctxt.pf_num = pf->hw.pf_id;
10320 	ctxt.vf_num = 0;
10321 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10322 	if (ret) {
10323 		dev_info(&pf->pdev->dev,
10324 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10325 			 ERR_PTR(ret),
10326 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10327 		return;
10328 	}
10329 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10330 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10331 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10332 
10333 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10334 	if (ret) {
10335 		dev_info(&pf->pdev->dev,
10336 			 "update vsi switch failed, err %pe aq_err %s\n",
10337 			 ERR_PTR(ret),
10338 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10339 	}
10340 }
10341 
10342 /**
10343  * i40e_disable_pf_switch_lb
10344  * @pf: pointer to the PF structure
10345  *
10346  * disable switch loop back or die - no point in a return value
10347  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)10348 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10349 {
10350 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10351 	struct i40e_vsi_context ctxt;
10352 	int ret;
10353 
10354 	ctxt.seid = pf->main_vsi_seid;
10355 	ctxt.pf_num = pf->hw.pf_id;
10356 	ctxt.vf_num = 0;
10357 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10358 	if (ret) {
10359 		dev_info(&pf->pdev->dev,
10360 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10361 			 ERR_PTR(ret),
10362 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10363 		return;
10364 	}
10365 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10366 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10367 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10368 
10369 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10370 	if (ret) {
10371 		dev_info(&pf->pdev->dev,
10372 			 "update vsi switch failed, err %pe aq_err %s\n",
10373 			 ERR_PTR(ret),
10374 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10375 	}
10376 }
10377 
10378 /**
10379  * i40e_config_bridge_mode - Configure the HW bridge mode
10380  * @veb: pointer to the bridge instance
10381  *
10382  * Configure the loop back mode for the LAN VSI that is downlink to the
10383  * specified HW bridge instance. It is expected this function is called
10384  * when a new HW bridge is instantiated.
10385  **/
i40e_config_bridge_mode(struct i40e_veb * veb)10386 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10387 {
10388 	struct i40e_pf *pf = veb->pf;
10389 
10390 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10391 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10392 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10393 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10394 		i40e_disable_pf_switch_lb(pf);
10395 	else
10396 		i40e_enable_pf_switch_lb(pf);
10397 }
10398 
10399 /**
10400  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10401  * @veb: pointer to the VEB instance
10402  *
10403  * This is a recursive function that first builds the attached VSIs then
10404  * recurses in to build the next layer of VEB.  We track the connections
10405  * through our own index numbers because the seid's from the HW could
10406  * change across the reset.
10407  **/
i40e_reconstitute_veb(struct i40e_veb * veb)10408 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10409 {
10410 	struct i40e_vsi *ctl_vsi = NULL;
10411 	struct i40e_pf *pf = veb->pf;
10412 	int v, veb_idx;
10413 	int ret;
10414 
10415 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10416 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10417 		if (pf->vsi[v] &&
10418 		    pf->vsi[v]->veb_idx == veb->idx &&
10419 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10420 			ctl_vsi = pf->vsi[v];
10421 			break;
10422 		}
10423 	}
10424 	if (!ctl_vsi) {
10425 		dev_info(&pf->pdev->dev,
10426 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10427 		ret = -ENOENT;
10428 		goto end_reconstitute;
10429 	}
10430 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10431 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10432 	ret = i40e_add_vsi(ctl_vsi);
10433 	if (ret) {
10434 		dev_info(&pf->pdev->dev,
10435 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10436 			 veb->idx, ret);
10437 		goto end_reconstitute;
10438 	}
10439 	i40e_vsi_reset_stats(ctl_vsi);
10440 
10441 	/* create the VEB in the switch and move the VSI onto the VEB */
10442 	ret = i40e_add_veb(veb, ctl_vsi);
10443 	if (ret)
10444 		goto end_reconstitute;
10445 
10446 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10447 		veb->bridge_mode = BRIDGE_MODE_VEB;
10448 	else
10449 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10450 	i40e_config_bridge_mode(veb);
10451 
10452 	/* create the remaining VSIs attached to this VEB */
10453 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10454 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10455 			continue;
10456 
10457 		if (pf->vsi[v]->veb_idx == veb->idx) {
10458 			struct i40e_vsi *vsi = pf->vsi[v];
10459 
10460 			vsi->uplink_seid = veb->seid;
10461 			ret = i40e_add_vsi(vsi);
10462 			if (ret) {
10463 				dev_info(&pf->pdev->dev,
10464 					 "rebuild of vsi_idx %d failed: %d\n",
10465 					 v, ret);
10466 				goto end_reconstitute;
10467 			}
10468 			i40e_vsi_reset_stats(vsi);
10469 		}
10470 	}
10471 
10472 	/* create any VEBs attached to this VEB - RECURSION */
10473 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10474 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10475 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10476 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10477 			if (ret)
10478 				break;
10479 		}
10480 	}
10481 
10482 end_reconstitute:
10483 	return ret;
10484 }
10485 
10486 /**
10487  * i40e_get_capabilities - get info about the HW
10488  * @pf: the PF struct
10489  * @list_type: AQ capability to be queried
10490  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)10491 static int i40e_get_capabilities(struct i40e_pf *pf,
10492 				 enum i40e_admin_queue_opc list_type)
10493 {
10494 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10495 	u16 data_size;
10496 	int buf_len;
10497 	int err;
10498 
10499 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10500 	do {
10501 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10502 		if (!cap_buf)
10503 			return -ENOMEM;
10504 
10505 		/* this loads the data into the hw struct for us */
10506 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10507 						    &data_size, list_type,
10508 						    NULL);
10509 		/* data loaded, buffer no longer needed */
10510 		kfree(cap_buf);
10511 
10512 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10513 			/* retry with a larger buffer */
10514 			buf_len = data_size;
10515 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10516 			dev_info(&pf->pdev->dev,
10517 				 "capability discovery failed, err %pe aq_err %s\n",
10518 				 ERR_PTR(err),
10519 				 i40e_aq_str(&pf->hw,
10520 					     pf->hw.aq.asq_last_status));
10521 			return -ENODEV;
10522 		}
10523 	} while (err);
10524 
10525 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10526 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10527 			dev_info(&pf->pdev->dev,
10528 				 "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",
10529 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10530 				 pf->hw.func_caps.num_msix_vectors,
10531 				 pf->hw.func_caps.num_msix_vectors_vf,
10532 				 pf->hw.func_caps.fd_filters_guaranteed,
10533 				 pf->hw.func_caps.fd_filters_best_effort,
10534 				 pf->hw.func_caps.num_tx_qp,
10535 				 pf->hw.func_caps.num_vsis);
10536 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10537 			dev_info(&pf->pdev->dev,
10538 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10539 				 pf->hw.dev_caps.switch_mode,
10540 				 pf->hw.dev_caps.valid_functions);
10541 			dev_info(&pf->pdev->dev,
10542 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10543 				 pf->hw.dev_caps.sr_iov_1_1,
10544 				 pf->hw.dev_caps.num_vfs);
10545 			dev_info(&pf->pdev->dev,
10546 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10547 				 pf->hw.dev_caps.num_vsis,
10548 				 pf->hw.dev_caps.num_rx_qp,
10549 				 pf->hw.dev_caps.num_tx_qp);
10550 		}
10551 	}
10552 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10553 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10554 		       + pf->hw.func_caps.num_vfs)
10555 		if (pf->hw.revision_id == 0 &&
10556 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10557 			dev_info(&pf->pdev->dev,
10558 				 "got num_vsis %d, setting num_vsis to %d\n",
10559 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10560 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10561 		}
10562 	}
10563 	return 0;
10564 }
10565 
10566 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10567 
10568 /**
10569  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10570  * @pf: board private structure
10571  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)10572 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10573 {
10574 	struct i40e_vsi *vsi;
10575 
10576 	/* quick workaround for an NVM issue that leaves a critical register
10577 	 * uninitialized
10578 	 */
10579 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10580 		static const u32 hkey[] = {
10581 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10582 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10583 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10584 			0x95b3a76d};
10585 		int i;
10586 
10587 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10588 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10589 	}
10590 
10591 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10592 		return;
10593 
10594 	/* find existing VSI and see if it needs configuring */
10595 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10596 
10597 	/* create a new VSI if none exists */
10598 	if (!vsi) {
10599 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10600 				     pf->vsi[pf->lan_vsi]->seid, 0);
10601 		if (!vsi) {
10602 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10603 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10604 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10605 			return;
10606 		}
10607 	}
10608 
10609 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10610 }
10611 
10612 /**
10613  * i40e_fdir_teardown - release the Flow Director resources
10614  * @pf: board private structure
10615  **/
i40e_fdir_teardown(struct i40e_pf * pf)10616 static void i40e_fdir_teardown(struct i40e_pf *pf)
10617 {
10618 	struct i40e_vsi *vsi;
10619 
10620 	i40e_fdir_filter_exit(pf);
10621 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10622 	if (vsi)
10623 		i40e_vsi_release(vsi);
10624 }
10625 
10626 /**
10627  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10628  * @vsi: PF main vsi
10629  * @seid: seid of main or channel VSIs
10630  *
10631  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10632  * existed before reset
10633  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)10634 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10635 {
10636 	struct i40e_cloud_filter *cfilter;
10637 	struct i40e_pf *pf = vsi->back;
10638 	struct hlist_node *node;
10639 	int ret;
10640 
10641 	/* Add cloud filters back if they exist */
10642 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10643 				  cloud_node) {
10644 		if (cfilter->seid != seid)
10645 			continue;
10646 
10647 		if (cfilter->dst_port)
10648 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10649 								true);
10650 		else
10651 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10652 
10653 		if (ret) {
10654 			dev_dbg(&pf->pdev->dev,
10655 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10656 				ERR_PTR(ret),
10657 				i40e_aq_str(&pf->hw,
10658 					    pf->hw.aq.asq_last_status));
10659 			return ret;
10660 		}
10661 	}
10662 	return 0;
10663 }
10664 
10665 /**
10666  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10667  * @vsi: PF main vsi
10668  *
10669  * Rebuilds channel VSIs if they existed before reset
10670  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)10671 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10672 {
10673 	struct i40e_channel *ch, *ch_tmp;
10674 	int ret;
10675 
10676 	if (list_empty(&vsi->ch_list))
10677 		return 0;
10678 
10679 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10680 		if (!ch->initialized)
10681 			break;
10682 		/* Proceed with creation of channel (VMDq2) VSI */
10683 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10684 		if (ret) {
10685 			dev_info(&vsi->back->pdev->dev,
10686 				 "failed to rebuild channels using uplink_seid %u\n",
10687 				 vsi->uplink_seid);
10688 			return ret;
10689 		}
10690 		/* Reconfigure TX queues using QTX_CTL register */
10691 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10692 		if (ret) {
10693 			dev_info(&vsi->back->pdev->dev,
10694 				 "failed to configure TX rings for channel %u\n",
10695 				 ch->seid);
10696 			return ret;
10697 		}
10698 		/* update 'next_base_queue' */
10699 		vsi->next_base_queue = vsi->next_base_queue +
10700 							ch->num_queue_pairs;
10701 		if (ch->max_tx_rate) {
10702 			u64 credits = ch->max_tx_rate;
10703 
10704 			if (i40e_set_bw_limit(vsi, ch->seid,
10705 					      ch->max_tx_rate))
10706 				return -EINVAL;
10707 
10708 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10709 			dev_dbg(&vsi->back->pdev->dev,
10710 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10711 				ch->max_tx_rate,
10712 				credits,
10713 				ch->seid);
10714 		}
10715 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10716 		if (ret) {
10717 			dev_dbg(&vsi->back->pdev->dev,
10718 				"Failed to rebuild cloud filters for channel VSI %u\n",
10719 				ch->seid);
10720 			return ret;
10721 		}
10722 	}
10723 	return 0;
10724 }
10725 
10726 /**
10727  * i40e_clean_xps_state - clean xps state for every tx_ring
10728  * @vsi: ptr to the VSI
10729  **/
i40e_clean_xps_state(struct i40e_vsi * vsi)10730 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10731 {
10732 	int i;
10733 
10734 	if (vsi->tx_rings)
10735 		for (i = 0; i < vsi->num_queue_pairs; i++)
10736 			if (vsi->tx_rings[i])
10737 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10738 					  vsi->tx_rings[i]->state);
10739 }
10740 
10741 /**
10742  * i40e_prep_for_reset - prep for the core to reset
10743  * @pf: board private structure
10744  *
10745  * Close up the VFs and other things in prep for PF Reset.
10746   **/
i40e_prep_for_reset(struct i40e_pf * pf)10747 static void i40e_prep_for_reset(struct i40e_pf *pf)
10748 {
10749 	struct i40e_hw *hw = &pf->hw;
10750 	int ret = 0;
10751 	u32 v;
10752 
10753 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10754 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10755 		return;
10756 	if (i40e_check_asq_alive(&pf->hw))
10757 		i40e_vc_notify_reset(pf);
10758 
10759 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10760 
10761 	/* quiesce the VSIs and their queues that are not already DOWN */
10762 	i40e_pf_quiesce_all_vsi(pf);
10763 
10764 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10765 		if (pf->vsi[v]) {
10766 			i40e_clean_xps_state(pf->vsi[v]);
10767 			pf->vsi[v]->seid = 0;
10768 		}
10769 	}
10770 
10771 	i40e_shutdown_adminq(&pf->hw);
10772 
10773 	/* call shutdown HMC */
10774 	if (hw->hmc.hmc_obj) {
10775 		ret = i40e_shutdown_lan_hmc(hw);
10776 		if (ret)
10777 			dev_warn(&pf->pdev->dev,
10778 				 "shutdown_lan_hmc failed: %d\n", ret);
10779 	}
10780 
10781 	/* Save the current PTP time so that we can restore the time after the
10782 	 * reset completes.
10783 	 */
10784 	i40e_ptp_save_hw_time(pf);
10785 }
10786 
10787 /**
10788  * i40e_send_version - update firmware with driver version
10789  * @pf: PF struct
10790  */
i40e_send_version(struct i40e_pf * pf)10791 static void i40e_send_version(struct i40e_pf *pf)
10792 {
10793 	struct i40e_driver_version dv;
10794 
10795 	dv.major_version = 0xff;
10796 	dv.minor_version = 0xff;
10797 	dv.build_version = 0xff;
10798 	dv.subbuild_version = 0;
10799 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10800 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10801 }
10802 
10803 /**
10804  * i40e_get_oem_version - get OEM specific version information
10805  * @hw: pointer to the hardware structure
10806  **/
i40e_get_oem_version(struct i40e_hw * hw)10807 static void i40e_get_oem_version(struct i40e_hw *hw)
10808 {
10809 	u16 block_offset = 0xffff;
10810 	u16 block_length = 0;
10811 	u16 capabilities = 0;
10812 	u16 gen_snap = 0;
10813 	u16 release = 0;
10814 
10815 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10816 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10817 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10818 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10819 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10820 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10821 #define I40E_NVM_OEM_LENGTH			3
10822 
10823 	/* Check if pointer to OEM version block is valid. */
10824 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10825 	if (block_offset == 0xffff)
10826 		return;
10827 
10828 	/* Check if OEM version block has correct length. */
10829 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10830 			   &block_length);
10831 	if (block_length < I40E_NVM_OEM_LENGTH)
10832 		return;
10833 
10834 	/* Check if OEM version format is as expected. */
10835 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10836 			   &capabilities);
10837 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10838 		return;
10839 
10840 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10841 			   &gen_snap);
10842 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10843 			   &release);
10844 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10845 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10846 }
10847 
10848 /**
10849  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10850  * @pf: board private structure
10851  **/
i40e_reset(struct i40e_pf * pf)10852 static int i40e_reset(struct i40e_pf *pf)
10853 {
10854 	struct i40e_hw *hw = &pf->hw;
10855 	int ret;
10856 
10857 	ret = i40e_pf_reset(hw);
10858 	if (ret) {
10859 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10860 		set_bit(__I40E_RESET_FAILED, pf->state);
10861 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10862 	} else {
10863 		pf->pfr_count++;
10864 	}
10865 	return ret;
10866 }
10867 
10868 /**
10869  * i40e_rebuild - rebuild using a saved config
10870  * @pf: board private structure
10871  * @reinit: if the Main VSI needs to re-initialized.
10872  * @lock_acquired: indicates whether or not the lock has been acquired
10873  * before this function was called.
10874  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10875 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10876 {
10877 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10878 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10879 	struct i40e_hw *hw = &pf->hw;
10880 	int ret;
10881 	u32 val;
10882 	int v;
10883 
10884 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10885 	    is_recovery_mode_reported)
10886 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10887 
10888 	if (test_bit(__I40E_DOWN, pf->state) &&
10889 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10890 		goto clear_recovery;
10891 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10892 
10893 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10894 	ret = i40e_init_adminq(&pf->hw);
10895 	if (ret) {
10896 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10897 			 ERR_PTR(ret),
10898 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10899 		goto clear_recovery;
10900 	}
10901 	i40e_get_oem_version(&pf->hw);
10902 
10903 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10904 		/* The following delay is necessary for firmware update. */
10905 		mdelay(1000);
10906 	}
10907 
10908 	/* re-verify the eeprom if we just had an EMP reset */
10909 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10910 		i40e_verify_eeprom(pf);
10911 
10912 	/* if we are going out of or into recovery mode we have to act
10913 	 * accordingly with regard to resources initialization
10914 	 * and deinitialization
10915 	 */
10916 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10917 		if (i40e_get_capabilities(pf,
10918 					  i40e_aqc_opc_list_func_capabilities))
10919 			goto end_unlock;
10920 
10921 		if (is_recovery_mode_reported) {
10922 			/* we're staying in recovery mode so we'll reinitialize
10923 			 * misc vector here
10924 			 */
10925 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10926 				goto end_unlock;
10927 		} else {
10928 			if (!lock_acquired)
10929 				rtnl_lock();
10930 			/* we're going out of recovery mode so we'll free
10931 			 * the IRQ allocated specifically for recovery mode
10932 			 * and restore the interrupt scheme
10933 			 */
10934 			free_irq(pf->pdev->irq, pf);
10935 			i40e_clear_interrupt_scheme(pf);
10936 			if (i40e_restore_interrupt_scheme(pf))
10937 				goto end_unlock;
10938 		}
10939 
10940 		/* tell the firmware that we're starting */
10941 		i40e_send_version(pf);
10942 
10943 		/* bail out in case recovery mode was detected, as there is
10944 		 * no need for further configuration.
10945 		 */
10946 		goto end_unlock;
10947 	}
10948 
10949 	i40e_clear_pxe_mode(hw);
10950 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10951 	if (ret)
10952 		goto end_core_reset;
10953 
10954 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10955 				hw->func_caps.num_rx_qp, 0, 0);
10956 	if (ret) {
10957 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10958 		goto end_core_reset;
10959 	}
10960 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10961 	if (ret) {
10962 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10963 		goto end_core_reset;
10964 	}
10965 
10966 #ifdef CONFIG_I40E_DCB
10967 	/* Enable FW to write a default DCB config on link-up
10968 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10969 	 * is not supported with new link speed
10970 	 */
10971 	if (i40e_is_tc_mqprio_enabled(pf)) {
10972 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10973 	} else {
10974 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10975 		    (hw->phy.link_info.link_speed &
10976 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10977 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10978 			dev_warn(&pf->pdev->dev,
10979 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10980 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10981 		} else {
10982 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10983 			ret = i40e_init_pf_dcb(pf);
10984 			if (ret) {
10985 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10986 					 ret);
10987 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10988 				/* Continue without DCB enabled */
10989 			}
10990 		}
10991 	}
10992 
10993 #endif /* CONFIG_I40E_DCB */
10994 	if (!lock_acquired)
10995 		rtnl_lock();
10996 	ret = i40e_setup_pf_switch(pf, reinit, true);
10997 	if (ret)
10998 		goto end_unlock;
10999 
11000 	/* The driver only wants link up/down and module qualification
11001 	 * reports from firmware.  Note the negative logic.
11002 	 */
11003 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
11004 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
11005 					 I40E_AQ_EVENT_MEDIA_NA |
11006 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
11007 	if (ret)
11008 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
11009 			 ERR_PTR(ret),
11010 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11011 
11012 	/* Rebuild the VSIs and VEBs that existed before reset.
11013 	 * They are still in our local switch element arrays, so only
11014 	 * need to rebuild the switch model in the HW.
11015 	 *
11016 	 * If there were VEBs but the reconstitution failed, we'll try
11017 	 * to recover minimal use by getting the basic PF VSI working.
11018 	 */
11019 	if (vsi->uplink_seid != pf->mac_seid) {
11020 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
11021 		/* find the one VEB connected to the MAC, and find orphans */
11022 		for (v = 0; v < I40E_MAX_VEB; v++) {
11023 			if (!pf->veb[v])
11024 				continue;
11025 
11026 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
11027 			    pf->veb[v]->uplink_seid == 0) {
11028 				ret = i40e_reconstitute_veb(pf->veb[v]);
11029 
11030 				if (!ret)
11031 					continue;
11032 
11033 				/* If Main VEB failed, we're in deep doodoo,
11034 				 * so give up rebuilding the switch and set up
11035 				 * for minimal rebuild of PF VSI.
11036 				 * If orphan failed, we'll report the error
11037 				 * but try to keep going.
11038 				 */
11039 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
11040 					dev_info(&pf->pdev->dev,
11041 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
11042 						 ret);
11043 					vsi->uplink_seid = pf->mac_seid;
11044 					break;
11045 				} else if (pf->veb[v]->uplink_seid == 0) {
11046 					dev_info(&pf->pdev->dev,
11047 						 "rebuild of orphan VEB failed: %d\n",
11048 						 ret);
11049 				}
11050 			}
11051 		}
11052 	}
11053 
11054 	if (vsi->uplink_seid == pf->mac_seid) {
11055 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11056 		/* no VEB, so rebuild only the Main VSI */
11057 		ret = i40e_add_vsi(vsi);
11058 		if (ret) {
11059 			dev_info(&pf->pdev->dev,
11060 				 "rebuild of Main VSI failed: %d\n", ret);
11061 			goto end_unlock;
11062 		}
11063 	}
11064 
11065 	if (vsi->mqprio_qopt.max_rate[0]) {
11066 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11067 						  vsi->mqprio_qopt.max_rate[0]);
11068 		u64 credits = 0;
11069 
11070 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11071 		if (ret)
11072 			goto end_unlock;
11073 
11074 		credits = max_tx_rate;
11075 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11076 		dev_dbg(&vsi->back->pdev->dev,
11077 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11078 			max_tx_rate,
11079 			credits,
11080 			vsi->seid);
11081 	}
11082 
11083 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11084 	if (ret)
11085 		goto end_unlock;
11086 
11087 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11088 	 * for this main VSI if they exist
11089 	 */
11090 	ret = i40e_rebuild_channels(vsi);
11091 	if (ret)
11092 		goto end_unlock;
11093 
11094 	/* Reconfigure hardware for allowing smaller MSS in the case
11095 	 * of TSO, so that we avoid the MDD being fired and causing
11096 	 * a reset in the case of small MSS+TSO.
11097 	 */
11098 #define I40E_REG_MSS          0x000E64DC
11099 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11100 #define I40E_64BYTE_MSS       0x400000
11101 	val = rd32(hw, I40E_REG_MSS);
11102 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11103 		val &= ~I40E_REG_MSS_MIN_MASK;
11104 		val |= I40E_64BYTE_MSS;
11105 		wr32(hw, I40E_REG_MSS, val);
11106 	}
11107 
11108 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11109 		msleep(75);
11110 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11111 		if (ret)
11112 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11113 				 ERR_PTR(ret),
11114 				 i40e_aq_str(&pf->hw,
11115 					     pf->hw.aq.asq_last_status));
11116 	}
11117 	/* reinit the misc interrupt */
11118 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11119 		ret = i40e_setup_misc_vector(pf);
11120 		if (ret)
11121 			goto end_unlock;
11122 	}
11123 
11124 	/* Add a filter to drop all Flow control frames from any VSI from being
11125 	 * transmitted. By doing so we stop a malicious VF from sending out
11126 	 * PAUSE or PFC frames and potentially controlling traffic for other
11127 	 * PF/VF VSIs.
11128 	 * The FW can still send Flow control frames if enabled.
11129 	 */
11130 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11131 						       pf->main_vsi_seid);
11132 
11133 	/* restart the VSIs that were rebuilt and running before the reset */
11134 	i40e_pf_unquiesce_all_vsi(pf);
11135 
11136 	/* Release the RTNL lock before we start resetting VFs */
11137 	if (!lock_acquired)
11138 		rtnl_unlock();
11139 
11140 	/* Restore promiscuous settings */
11141 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11142 	if (ret)
11143 		dev_warn(&pf->pdev->dev,
11144 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11145 			 pf->cur_promisc ? "on" : "off",
11146 			 ERR_PTR(ret),
11147 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11148 
11149 	i40e_reset_all_vfs(pf, true);
11150 
11151 	/* tell the firmware that we're starting */
11152 	i40e_send_version(pf);
11153 
11154 	/* We've already released the lock, so don't do it again */
11155 	goto end_core_reset;
11156 
11157 end_unlock:
11158 	if (!lock_acquired)
11159 		rtnl_unlock();
11160 end_core_reset:
11161 	clear_bit(__I40E_RESET_FAILED, pf->state);
11162 clear_recovery:
11163 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11164 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11165 }
11166 
11167 /**
11168  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11169  * @pf: board private structure
11170  * @reinit: if the Main VSI needs to re-initialized.
11171  * @lock_acquired: indicates whether or not the lock has been acquired
11172  * before this function was called.
11173  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)11174 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11175 				   bool lock_acquired)
11176 {
11177 	int ret;
11178 
11179 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11180 		return;
11181 	/* Now we wait for GRST to settle out.
11182 	 * We don't have to delete the VEBs or VSIs from the hw switch
11183 	 * because the reset will make them disappear.
11184 	 */
11185 	ret = i40e_reset(pf);
11186 	if (!ret)
11187 		i40e_rebuild(pf, reinit, lock_acquired);
11188 }
11189 
11190 /**
11191  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11192  * @pf: board private structure
11193  *
11194  * Close up the VFs and other things in prep for a Core Reset,
11195  * then get ready to rebuild the world.
11196  * @lock_acquired: indicates whether or not the lock has been acquired
11197  * before this function was called.
11198  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)11199 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11200 {
11201 	i40e_prep_for_reset(pf);
11202 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11203 }
11204 
11205 /**
11206  * i40e_handle_mdd_event
11207  * @pf: pointer to the PF structure
11208  *
11209  * Called from the MDD irq handler to identify possibly malicious vfs
11210  **/
i40e_handle_mdd_event(struct i40e_pf * pf)11211 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11212 {
11213 	struct i40e_hw *hw = &pf->hw;
11214 	bool mdd_detected = false;
11215 	struct i40e_vf *vf;
11216 	u32 reg;
11217 	int i;
11218 
11219 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11220 		return;
11221 
11222 	/* find what triggered the MDD event */
11223 	reg = rd32(hw, I40E_GL_MDET_TX);
11224 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11225 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11226 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11227 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11228 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11229 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11230 				I40E_GL_MDET_TX_EVENT_SHIFT;
11231 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11232 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11233 				pf->hw.func_caps.base_queue;
11234 		if (netif_msg_tx_err(pf))
11235 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11236 				 event, queue, pf_num, vf_num);
11237 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11238 		mdd_detected = true;
11239 	}
11240 	reg = rd32(hw, I40E_GL_MDET_RX);
11241 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11242 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11243 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11244 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11245 				I40E_GL_MDET_RX_EVENT_SHIFT;
11246 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11247 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11248 				pf->hw.func_caps.base_queue;
11249 		if (netif_msg_rx_err(pf))
11250 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11251 				 event, queue, func);
11252 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11253 		mdd_detected = true;
11254 	}
11255 
11256 	if (mdd_detected) {
11257 		reg = rd32(hw, I40E_PF_MDET_TX);
11258 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11259 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11260 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11261 		}
11262 		reg = rd32(hw, I40E_PF_MDET_RX);
11263 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11264 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11265 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11266 		}
11267 	}
11268 
11269 	/* see if one of the VFs needs its hand slapped */
11270 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11271 		vf = &(pf->vf[i]);
11272 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11273 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11274 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11275 			vf->num_mdd_events++;
11276 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11277 				 i);
11278 			dev_info(&pf->pdev->dev,
11279 				 "Use PF Control I/F to re-enable the VF\n");
11280 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11281 		}
11282 
11283 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11284 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11285 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11286 			vf->num_mdd_events++;
11287 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11288 				 i);
11289 			dev_info(&pf->pdev->dev,
11290 				 "Use PF Control I/F to re-enable the VF\n");
11291 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11292 		}
11293 	}
11294 
11295 	/* re-enable mdd interrupt cause */
11296 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11297 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11298 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11299 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11300 	i40e_flush(hw);
11301 }
11302 
11303 /**
11304  * i40e_service_task - Run the driver's async subtasks
11305  * @work: pointer to work_struct containing our data
11306  **/
i40e_service_task(struct work_struct * work)11307 static void i40e_service_task(struct work_struct *work)
11308 {
11309 	struct i40e_pf *pf = container_of(work,
11310 					  struct i40e_pf,
11311 					  service_task);
11312 	unsigned long start_time = jiffies;
11313 
11314 	/* don't bother with service tasks if a reset is in progress */
11315 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11316 	    test_bit(__I40E_SUSPENDED, pf->state))
11317 		return;
11318 
11319 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11320 		return;
11321 
11322 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11323 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11324 		i40e_sync_filters_subtask(pf);
11325 		i40e_reset_subtask(pf);
11326 		i40e_handle_mdd_event(pf);
11327 		i40e_vc_process_vflr_event(pf);
11328 		i40e_watchdog_subtask(pf);
11329 		i40e_fdir_reinit_subtask(pf);
11330 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11331 			/* Client subtask will reopen next time through. */
11332 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11333 							   true);
11334 		} else {
11335 			i40e_client_subtask(pf);
11336 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11337 					       pf->state))
11338 				i40e_notify_client_of_l2_param_changes(
11339 								pf->vsi[pf->lan_vsi]);
11340 		}
11341 		i40e_sync_filters_subtask(pf);
11342 	} else {
11343 		i40e_reset_subtask(pf);
11344 	}
11345 
11346 	i40e_clean_adminq_subtask(pf);
11347 
11348 	/* flush memory to make sure state is correct before next watchdog */
11349 	smp_mb__before_atomic();
11350 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11351 
11352 	/* If the tasks have taken longer than one timer cycle or there
11353 	 * is more work to be done, reschedule the service task now
11354 	 * rather than wait for the timer to tick again.
11355 	 */
11356 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11357 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11358 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11359 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11360 		i40e_service_event_schedule(pf);
11361 }
11362 
11363 /**
11364  * i40e_service_timer - timer callback
11365  * @t: timer list pointer
11366  **/
i40e_service_timer(struct timer_list * t)11367 static void i40e_service_timer(struct timer_list *t)
11368 {
11369 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11370 
11371 	mod_timer(&pf->service_timer,
11372 		  round_jiffies(jiffies + pf->service_timer_period));
11373 	i40e_service_event_schedule(pf);
11374 }
11375 
11376 /**
11377  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11378  * @vsi: the VSI being configured
11379  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)11380 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11381 {
11382 	struct i40e_pf *pf = vsi->back;
11383 
11384 	switch (vsi->type) {
11385 	case I40E_VSI_MAIN:
11386 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11387 		if (!vsi->num_tx_desc)
11388 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11389 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11390 		if (!vsi->num_rx_desc)
11391 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11392 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11393 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11394 			vsi->num_q_vectors = pf->num_lan_msix;
11395 		else
11396 			vsi->num_q_vectors = 1;
11397 
11398 		break;
11399 
11400 	case I40E_VSI_FDIR:
11401 		vsi->alloc_queue_pairs = 1;
11402 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11403 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11404 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11405 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11406 		vsi->num_q_vectors = pf->num_fdsb_msix;
11407 		break;
11408 
11409 	case I40E_VSI_VMDQ2:
11410 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11411 		if (!vsi->num_tx_desc)
11412 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11413 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11414 		if (!vsi->num_rx_desc)
11415 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11416 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11417 		vsi->num_q_vectors = pf->num_vmdq_msix;
11418 		break;
11419 
11420 	case I40E_VSI_SRIOV:
11421 		vsi->alloc_queue_pairs = pf->num_vf_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 		break;
11429 
11430 	default:
11431 		WARN_ON(1);
11432 		return -ENODATA;
11433 	}
11434 
11435 	if (is_kdump_kernel()) {
11436 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11437 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11438 	}
11439 
11440 	return 0;
11441 }
11442 
11443 /**
11444  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11445  * @vsi: VSI pointer
11446  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11447  *
11448  * On error: returns error code (negative)
11449  * On success: returns 0
11450  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)11451 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11452 {
11453 	struct i40e_ring **next_rings;
11454 	int size;
11455 	int ret = 0;
11456 
11457 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11458 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11459 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11460 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11461 	if (!vsi->tx_rings)
11462 		return -ENOMEM;
11463 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11464 	if (i40e_enabled_xdp_vsi(vsi)) {
11465 		vsi->xdp_rings = next_rings;
11466 		next_rings += vsi->alloc_queue_pairs;
11467 	}
11468 	vsi->rx_rings = next_rings;
11469 
11470 	if (alloc_qvectors) {
11471 		/* allocate memory for q_vector pointers */
11472 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11473 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11474 		if (!vsi->q_vectors) {
11475 			ret = -ENOMEM;
11476 			goto err_vectors;
11477 		}
11478 	}
11479 	return ret;
11480 
11481 err_vectors:
11482 	kfree(vsi->tx_rings);
11483 	return ret;
11484 }
11485 
11486 /**
11487  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11488  * @pf: board private structure
11489  * @type: type of VSI
11490  *
11491  * On error: returns error code (negative)
11492  * On success: returns vsi index in PF (positive)
11493  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)11494 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11495 {
11496 	int ret = -ENODEV;
11497 	struct i40e_vsi *vsi;
11498 	int vsi_idx;
11499 	int i;
11500 
11501 	/* Need to protect the allocation of the VSIs at the PF level */
11502 	mutex_lock(&pf->switch_mutex);
11503 
11504 	/* VSI list may be fragmented if VSI creation/destruction has
11505 	 * been happening.  We can afford to do a quick scan to look
11506 	 * for any free VSIs in the list.
11507 	 *
11508 	 * find next empty vsi slot, looping back around if necessary
11509 	 */
11510 	i = pf->next_vsi;
11511 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11512 		i++;
11513 	if (i >= pf->num_alloc_vsi) {
11514 		i = 0;
11515 		while (i < pf->next_vsi && pf->vsi[i])
11516 			i++;
11517 	}
11518 
11519 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11520 		vsi_idx = i;             /* Found one! */
11521 	} else {
11522 		ret = -ENODEV;
11523 		goto unlock_pf;  /* out of VSI slots! */
11524 	}
11525 	pf->next_vsi = ++i;
11526 
11527 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11528 	if (!vsi) {
11529 		ret = -ENOMEM;
11530 		goto unlock_pf;
11531 	}
11532 	vsi->type = type;
11533 	vsi->back = pf;
11534 	set_bit(__I40E_VSI_DOWN, vsi->state);
11535 	vsi->flags = 0;
11536 	vsi->idx = vsi_idx;
11537 	vsi->int_rate_limit = 0;
11538 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11539 				pf->rss_table_size : 64;
11540 	vsi->netdev_registered = false;
11541 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11542 	hash_init(vsi->mac_filter_hash);
11543 	vsi->irqs_ready = false;
11544 
11545 	if (type == I40E_VSI_MAIN) {
11546 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11547 		if (!vsi->af_xdp_zc_qps)
11548 			goto err_rings;
11549 	}
11550 
11551 	ret = i40e_set_num_rings_in_vsi(vsi);
11552 	if (ret)
11553 		goto err_rings;
11554 
11555 	ret = i40e_vsi_alloc_arrays(vsi, true);
11556 	if (ret)
11557 		goto err_rings;
11558 
11559 	/* Setup default MSIX irq handler for VSI */
11560 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11561 
11562 	/* Initialize VSI lock */
11563 	spin_lock_init(&vsi->mac_filter_hash_lock);
11564 	pf->vsi[vsi_idx] = vsi;
11565 	ret = vsi_idx;
11566 	goto unlock_pf;
11567 
11568 err_rings:
11569 	bitmap_free(vsi->af_xdp_zc_qps);
11570 	pf->next_vsi = i - 1;
11571 	kfree(vsi);
11572 unlock_pf:
11573 	mutex_unlock(&pf->switch_mutex);
11574 	return ret;
11575 }
11576 
11577 /**
11578  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11579  * @vsi: VSI pointer
11580  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11581  *
11582  * On error: returns error code (negative)
11583  * On success: returns 0
11584  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)11585 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11586 {
11587 	/* free the ring and vector containers */
11588 	if (free_qvectors) {
11589 		kfree(vsi->q_vectors);
11590 		vsi->q_vectors = NULL;
11591 	}
11592 	kfree(vsi->tx_rings);
11593 	vsi->tx_rings = NULL;
11594 	vsi->rx_rings = NULL;
11595 	vsi->xdp_rings = NULL;
11596 }
11597 
11598 /**
11599  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11600  * and lookup table
11601  * @vsi: Pointer to VSI structure
11602  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)11603 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11604 {
11605 	if (!vsi)
11606 		return;
11607 
11608 	kfree(vsi->rss_hkey_user);
11609 	vsi->rss_hkey_user = NULL;
11610 
11611 	kfree(vsi->rss_lut_user);
11612 	vsi->rss_lut_user = NULL;
11613 }
11614 
11615 /**
11616  * i40e_vsi_clear - Deallocate the VSI provided
11617  * @vsi: the VSI being un-configured
11618  **/
i40e_vsi_clear(struct i40e_vsi * vsi)11619 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11620 {
11621 	struct i40e_pf *pf;
11622 
11623 	if (!vsi)
11624 		return 0;
11625 
11626 	if (!vsi->back)
11627 		goto free_vsi;
11628 	pf = vsi->back;
11629 
11630 	mutex_lock(&pf->switch_mutex);
11631 	if (!pf->vsi[vsi->idx]) {
11632 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11633 			vsi->idx, vsi->idx, vsi->type);
11634 		goto unlock_vsi;
11635 	}
11636 
11637 	if (pf->vsi[vsi->idx] != vsi) {
11638 		dev_err(&pf->pdev->dev,
11639 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11640 			pf->vsi[vsi->idx]->idx,
11641 			pf->vsi[vsi->idx]->type,
11642 			vsi->idx, vsi->type);
11643 		goto unlock_vsi;
11644 	}
11645 
11646 	/* updates the PF for this cleared vsi */
11647 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11648 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11649 
11650 	bitmap_free(vsi->af_xdp_zc_qps);
11651 	i40e_vsi_free_arrays(vsi, true);
11652 	i40e_clear_rss_config_user(vsi);
11653 
11654 	pf->vsi[vsi->idx] = NULL;
11655 	if (vsi->idx < pf->next_vsi)
11656 		pf->next_vsi = vsi->idx;
11657 
11658 unlock_vsi:
11659 	mutex_unlock(&pf->switch_mutex);
11660 free_vsi:
11661 	kfree(vsi);
11662 
11663 	return 0;
11664 }
11665 
11666 /**
11667  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11668  * @vsi: the VSI being cleaned
11669  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)11670 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11671 {
11672 	int i;
11673 
11674 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11675 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11676 			kfree_rcu(vsi->tx_rings[i], rcu);
11677 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11678 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11679 			if (vsi->xdp_rings)
11680 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11681 		}
11682 	}
11683 }
11684 
11685 /**
11686  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11687  * @vsi: the VSI being configured
11688  **/
i40e_alloc_rings(struct i40e_vsi * vsi)11689 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11690 {
11691 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11692 	struct i40e_pf *pf = vsi->back;
11693 	struct i40e_ring *ring;
11694 
11695 	/* Set basic values in the rings to be used later during open() */
11696 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11697 		/* allocate space for both Tx and Rx in one shot */
11698 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11699 		if (!ring)
11700 			goto err_out;
11701 
11702 		ring->queue_index = i;
11703 		ring->reg_idx = vsi->base_queue + i;
11704 		ring->ring_active = false;
11705 		ring->vsi = vsi;
11706 		ring->netdev = vsi->netdev;
11707 		ring->dev = &pf->pdev->dev;
11708 		ring->count = vsi->num_tx_desc;
11709 		ring->size = 0;
11710 		ring->dcb_tc = 0;
11711 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11712 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11713 		ring->itr_setting = pf->tx_itr_default;
11714 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11715 
11716 		if (!i40e_enabled_xdp_vsi(vsi))
11717 			goto setup_rx;
11718 
11719 		ring->queue_index = vsi->alloc_queue_pairs + i;
11720 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11721 		ring->ring_active = false;
11722 		ring->vsi = vsi;
11723 		ring->netdev = NULL;
11724 		ring->dev = &pf->pdev->dev;
11725 		ring->count = vsi->num_tx_desc;
11726 		ring->size = 0;
11727 		ring->dcb_tc = 0;
11728 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11729 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11730 		set_ring_xdp(ring);
11731 		ring->itr_setting = pf->tx_itr_default;
11732 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11733 
11734 setup_rx:
11735 		ring->queue_index = i;
11736 		ring->reg_idx = vsi->base_queue + i;
11737 		ring->ring_active = false;
11738 		ring->vsi = vsi;
11739 		ring->netdev = vsi->netdev;
11740 		ring->dev = &pf->pdev->dev;
11741 		ring->count = vsi->num_rx_desc;
11742 		ring->size = 0;
11743 		ring->dcb_tc = 0;
11744 		ring->itr_setting = pf->rx_itr_default;
11745 		WRITE_ONCE(vsi->rx_rings[i], ring);
11746 	}
11747 
11748 	return 0;
11749 
11750 err_out:
11751 	i40e_vsi_clear_rings(vsi);
11752 	return -ENOMEM;
11753 }
11754 
11755 /**
11756  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11757  * @pf: board private structure
11758  * @vectors: the number of MSI-X vectors to request
11759  *
11760  * Returns the number of vectors reserved, or error
11761  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)11762 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11763 {
11764 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11765 					I40E_MIN_MSIX, vectors);
11766 	if (vectors < 0) {
11767 		dev_info(&pf->pdev->dev,
11768 			 "MSI-X vector reservation failed: %d\n", vectors);
11769 		vectors = 0;
11770 	}
11771 
11772 	return vectors;
11773 }
11774 
11775 /**
11776  * i40e_init_msix - Setup the MSIX capability
11777  * @pf: board private structure
11778  *
11779  * Work with the OS to set up the MSIX vectors needed.
11780  *
11781  * Returns the number of vectors reserved or negative on failure
11782  **/
i40e_init_msix(struct i40e_pf * pf)11783 static int i40e_init_msix(struct i40e_pf *pf)
11784 {
11785 	struct i40e_hw *hw = &pf->hw;
11786 	int cpus, extra_vectors;
11787 	int vectors_left;
11788 	int v_budget, i;
11789 	int v_actual;
11790 	int iwarp_requested = 0;
11791 
11792 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11793 		return -ENODEV;
11794 
11795 	/* The number of vectors we'll request will be comprised of:
11796 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11797 	 *   - The number of LAN queue pairs
11798 	 *	- Queues being used for RSS.
11799 	 *		We don't need as many as max_rss_size vectors.
11800 	 *		use rss_size instead in the calculation since that
11801 	 *		is governed by number of cpus in the system.
11802 	 *	- assumes symmetric Tx/Rx pairing
11803 	 *   - The number of VMDq pairs
11804 	 *   - The CPU count within the NUMA node if iWARP is enabled
11805 	 * Once we count this up, try the request.
11806 	 *
11807 	 * If we can't get what we want, we'll simplify to nearly nothing
11808 	 * and try again.  If that still fails, we punt.
11809 	 */
11810 	vectors_left = hw->func_caps.num_msix_vectors;
11811 	v_budget = 0;
11812 
11813 	/* reserve one vector for miscellaneous handler */
11814 	if (vectors_left) {
11815 		v_budget++;
11816 		vectors_left--;
11817 	}
11818 
11819 	/* reserve some vectors for the main PF traffic queues. Initially we
11820 	 * only reserve at most 50% of the available vectors, in the case that
11821 	 * the number of online CPUs is large. This ensures that we can enable
11822 	 * extra features as well. Once we've enabled the other features, we
11823 	 * will use any remaining vectors to reach as close as we can to the
11824 	 * number of online CPUs.
11825 	 */
11826 	cpus = num_online_cpus();
11827 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11828 	vectors_left -= pf->num_lan_msix;
11829 
11830 	/* reserve one vector for sideband flow director */
11831 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11832 		if (vectors_left) {
11833 			pf->num_fdsb_msix = 1;
11834 			v_budget++;
11835 			vectors_left--;
11836 		} else {
11837 			pf->num_fdsb_msix = 0;
11838 		}
11839 	}
11840 
11841 	/* can we reserve enough for iWARP? */
11842 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11843 		iwarp_requested = pf->num_iwarp_msix;
11844 
11845 		if (!vectors_left)
11846 			pf->num_iwarp_msix = 0;
11847 		else if (vectors_left < pf->num_iwarp_msix)
11848 			pf->num_iwarp_msix = 1;
11849 		v_budget += pf->num_iwarp_msix;
11850 		vectors_left -= pf->num_iwarp_msix;
11851 	}
11852 
11853 	/* any vectors left over go for VMDq support */
11854 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11855 		if (!vectors_left) {
11856 			pf->num_vmdq_msix = 0;
11857 			pf->num_vmdq_qps = 0;
11858 		} else {
11859 			int vmdq_vecs_wanted =
11860 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11861 			int vmdq_vecs =
11862 				min_t(int, vectors_left, vmdq_vecs_wanted);
11863 
11864 			/* if we're short on vectors for what's desired, we limit
11865 			 * the queues per vmdq.  If this is still more than are
11866 			 * available, the user will need to change the number of
11867 			 * queues/vectors used by the PF later with the ethtool
11868 			 * channels command
11869 			 */
11870 			if (vectors_left < vmdq_vecs_wanted) {
11871 				pf->num_vmdq_qps = 1;
11872 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11873 				vmdq_vecs = min_t(int,
11874 						  vectors_left,
11875 						  vmdq_vecs_wanted);
11876 			}
11877 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11878 
11879 			v_budget += vmdq_vecs;
11880 			vectors_left -= vmdq_vecs;
11881 		}
11882 	}
11883 
11884 	/* On systems with a large number of SMP cores, we previously limited
11885 	 * the number of vectors for num_lan_msix to be at most 50% of the
11886 	 * available vectors, to allow for other features. Now, we add back
11887 	 * the remaining vectors. However, we ensure that the total
11888 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11889 	 * calculate the number of vectors we can add without going over the
11890 	 * cap of CPUs. For systems with a small number of CPUs this will be
11891 	 * zero.
11892 	 */
11893 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11894 	pf->num_lan_msix += extra_vectors;
11895 	vectors_left -= extra_vectors;
11896 
11897 	WARN(vectors_left < 0,
11898 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11899 
11900 	v_budget += pf->num_lan_msix;
11901 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11902 				   GFP_KERNEL);
11903 	if (!pf->msix_entries)
11904 		return -ENOMEM;
11905 
11906 	for (i = 0; i < v_budget; i++)
11907 		pf->msix_entries[i].entry = i;
11908 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11909 
11910 	if (v_actual < I40E_MIN_MSIX) {
11911 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11912 		kfree(pf->msix_entries);
11913 		pf->msix_entries = NULL;
11914 		pci_disable_msix(pf->pdev);
11915 		return -ENODEV;
11916 
11917 	} else if (v_actual == I40E_MIN_MSIX) {
11918 		/* Adjust for minimal MSIX use */
11919 		pf->num_vmdq_vsis = 0;
11920 		pf->num_vmdq_qps = 0;
11921 		pf->num_lan_qps = 1;
11922 		pf->num_lan_msix = 1;
11923 
11924 	} else if (v_actual != v_budget) {
11925 		/* If we have limited resources, we will start with no vectors
11926 		 * for the special features and then allocate vectors to some
11927 		 * of these features based on the policy and at the end disable
11928 		 * the features that did not get any vectors.
11929 		 */
11930 		int vec;
11931 
11932 		dev_info(&pf->pdev->dev,
11933 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11934 			 v_actual, v_budget);
11935 		/* reserve the misc vector */
11936 		vec = v_actual - 1;
11937 
11938 		/* Scale vector usage down */
11939 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11940 		pf->num_vmdq_vsis = 1;
11941 		pf->num_vmdq_qps = 1;
11942 
11943 		/* partition out the remaining vectors */
11944 		switch (vec) {
11945 		case 2:
11946 			pf->num_lan_msix = 1;
11947 			break;
11948 		case 3:
11949 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11950 				pf->num_lan_msix = 1;
11951 				pf->num_iwarp_msix = 1;
11952 			} else {
11953 				pf->num_lan_msix = 2;
11954 			}
11955 			break;
11956 		default:
11957 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11958 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11959 						 iwarp_requested);
11960 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11961 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11962 			} else {
11963 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11964 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11965 			}
11966 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11967 				pf->num_fdsb_msix = 1;
11968 				vec--;
11969 			}
11970 			pf->num_lan_msix = min_t(int,
11971 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11972 							      pf->num_lan_msix);
11973 			pf->num_lan_qps = pf->num_lan_msix;
11974 			break;
11975 		}
11976 	}
11977 
11978 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11979 	    (pf->num_fdsb_msix == 0)) {
11980 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11981 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11982 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11983 	}
11984 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11985 	    (pf->num_vmdq_msix == 0)) {
11986 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11987 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11988 	}
11989 
11990 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11991 	    (pf->num_iwarp_msix == 0)) {
11992 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11993 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11994 	}
11995 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11996 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11997 		   pf->num_lan_msix,
11998 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11999 		   pf->num_fdsb_msix,
12000 		   pf->num_iwarp_msix);
12001 
12002 	return v_actual;
12003 }
12004 
12005 /**
12006  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
12007  * @vsi: the VSI being configured
12008  * @v_idx: index of the vector in the vsi struct
12009  *
12010  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
12011  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)12012 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
12013 {
12014 	struct i40e_q_vector *q_vector;
12015 
12016 	/* allocate q_vector */
12017 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
12018 	if (!q_vector)
12019 		return -ENOMEM;
12020 
12021 	q_vector->vsi = vsi;
12022 	q_vector->v_idx = v_idx;
12023 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
12024 
12025 	if (vsi->netdev)
12026 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
12027 
12028 	/* tie q_vector and vsi together */
12029 	vsi->q_vectors[v_idx] = q_vector;
12030 
12031 	return 0;
12032 }
12033 
12034 /**
12035  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12036  * @vsi: the VSI being configured
12037  *
12038  * We allocate one q_vector per queue interrupt.  If allocation fails we
12039  * return -ENOMEM.
12040  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)12041 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12042 {
12043 	struct i40e_pf *pf = vsi->back;
12044 	int err, v_idx, num_q_vectors;
12045 
12046 	/* if not MSIX, give the one vector only to the LAN VSI */
12047 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12048 		num_q_vectors = vsi->num_q_vectors;
12049 	else if (vsi == pf->vsi[pf->lan_vsi])
12050 		num_q_vectors = 1;
12051 	else
12052 		return -EINVAL;
12053 
12054 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12055 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12056 		if (err)
12057 			goto err_out;
12058 	}
12059 
12060 	return 0;
12061 
12062 err_out:
12063 	while (v_idx--)
12064 		i40e_free_q_vector(vsi, v_idx);
12065 
12066 	return err;
12067 }
12068 
12069 /**
12070  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12071  * @pf: board private structure to initialize
12072  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)12073 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12074 {
12075 	int vectors = 0;
12076 	ssize_t size;
12077 
12078 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12079 		vectors = i40e_init_msix(pf);
12080 		if (vectors < 0) {
12081 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
12082 				       I40E_FLAG_IWARP_ENABLED	|
12083 				       I40E_FLAG_RSS_ENABLED	|
12084 				       I40E_FLAG_DCB_CAPABLE	|
12085 				       I40E_FLAG_DCB_ENABLED	|
12086 				       I40E_FLAG_SRIOV_ENABLED	|
12087 				       I40E_FLAG_FD_SB_ENABLED	|
12088 				       I40E_FLAG_FD_ATR_ENABLED	|
12089 				       I40E_FLAG_VMDQ_ENABLED);
12090 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12091 
12092 			/* rework the queue expectations without MSIX */
12093 			i40e_determine_queue_usage(pf);
12094 		}
12095 	}
12096 
12097 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12098 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12099 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12100 		vectors = pci_enable_msi(pf->pdev);
12101 		if (vectors < 0) {
12102 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12103 				 vectors);
12104 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12105 		}
12106 		vectors = 1;  /* one MSI or Legacy vector */
12107 	}
12108 
12109 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12110 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12111 
12112 	/* set up vector assignment tracking */
12113 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12114 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12115 	if (!pf->irq_pile)
12116 		return -ENOMEM;
12117 
12118 	pf->irq_pile->num_entries = vectors;
12119 
12120 	/* track first vector for misc interrupts, ignore return */
12121 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12122 
12123 	return 0;
12124 }
12125 
12126 /**
12127  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12128  * @pf: private board data structure
12129  *
12130  * Restore the interrupt scheme that was cleared when we suspended the
12131  * device. This should be called during resume to re-allocate the q_vectors
12132  * and reacquire IRQs.
12133  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)12134 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12135 {
12136 	int err, i;
12137 
12138 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12139 	 * scheme. We need to re-enabled them here in order to attempt to
12140 	 * re-acquire the MSI or MSI-X vectors
12141 	 */
12142 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12143 
12144 	err = i40e_init_interrupt_scheme(pf);
12145 	if (err)
12146 		return err;
12147 
12148 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12149 	 * rings together again.
12150 	 */
12151 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12152 		if (pf->vsi[i]) {
12153 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12154 			if (err)
12155 				goto err_unwind;
12156 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12157 		}
12158 	}
12159 
12160 	err = i40e_setup_misc_vector(pf);
12161 	if (err)
12162 		goto err_unwind;
12163 
12164 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12165 		i40e_client_update_msix_info(pf);
12166 
12167 	return 0;
12168 
12169 err_unwind:
12170 	while (i--) {
12171 		if (pf->vsi[i])
12172 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12173 	}
12174 
12175 	return err;
12176 }
12177 
12178 /**
12179  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12180  * non queue events in recovery mode
12181  * @pf: board private structure
12182  *
12183  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12184  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12185  * This is handled differently than in recovery mode since no Tx/Rx resources
12186  * are being allocated.
12187  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)12188 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12189 {
12190 	int err;
12191 
12192 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12193 		err = i40e_setup_misc_vector(pf);
12194 
12195 		if (err) {
12196 			dev_info(&pf->pdev->dev,
12197 				 "MSI-X misc vector request failed, error %d\n",
12198 				 err);
12199 			return err;
12200 		}
12201 	} else {
12202 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12203 
12204 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12205 				  pf->int_name, pf);
12206 
12207 		if (err) {
12208 			dev_info(&pf->pdev->dev,
12209 				 "MSI/legacy misc vector request failed, error %d\n",
12210 				 err);
12211 			return err;
12212 		}
12213 		i40e_enable_misc_int_causes(pf);
12214 		i40e_irq_dynamic_enable_icr0(pf);
12215 	}
12216 
12217 	return 0;
12218 }
12219 
12220 /**
12221  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12222  * @pf: board private structure
12223  *
12224  * This sets up the handler for MSIX 0, which is used to manage the
12225  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12226  * when in MSI or Legacy interrupt mode.
12227  **/
i40e_setup_misc_vector(struct i40e_pf * pf)12228 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12229 {
12230 	struct i40e_hw *hw = &pf->hw;
12231 	int err = 0;
12232 
12233 	/* Only request the IRQ once, the first time through. */
12234 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12235 		err = request_irq(pf->msix_entries[0].vector,
12236 				  i40e_intr, 0, pf->int_name, pf);
12237 		if (err) {
12238 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12239 			dev_info(&pf->pdev->dev,
12240 				 "request_irq for %s failed: %d\n",
12241 				 pf->int_name, err);
12242 			return -EFAULT;
12243 		}
12244 	}
12245 
12246 	i40e_enable_misc_int_causes(pf);
12247 
12248 	/* associate no queues to the misc vector */
12249 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12250 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12251 
12252 	i40e_flush(hw);
12253 
12254 	i40e_irq_dynamic_enable_icr0(pf);
12255 
12256 	return err;
12257 }
12258 
12259 /**
12260  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12261  * @vsi: Pointer to vsi structure
12262  * @seed: Buffter to store the hash keys
12263  * @lut: Buffer to store the lookup table entries
12264  * @lut_size: Size of buffer to store the lookup table entries
12265  *
12266  * Return 0 on success, negative on failure
12267  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)12268 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12269 			   u8 *lut, u16 lut_size)
12270 {
12271 	struct i40e_pf *pf = vsi->back;
12272 	struct i40e_hw *hw = &pf->hw;
12273 	int ret = 0;
12274 
12275 	if (seed) {
12276 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12277 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12278 		if (ret) {
12279 			dev_info(&pf->pdev->dev,
12280 				 "Cannot get RSS key, err %pe aq_err %s\n",
12281 				 ERR_PTR(ret),
12282 				 i40e_aq_str(&pf->hw,
12283 					     pf->hw.aq.asq_last_status));
12284 			return ret;
12285 		}
12286 	}
12287 
12288 	if (lut) {
12289 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12290 
12291 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12292 		if (ret) {
12293 			dev_info(&pf->pdev->dev,
12294 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12295 				 ERR_PTR(ret),
12296 				 i40e_aq_str(&pf->hw,
12297 					     pf->hw.aq.asq_last_status));
12298 			return ret;
12299 		}
12300 	}
12301 
12302 	return ret;
12303 }
12304 
12305 /**
12306  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12307  * @vsi: Pointer to vsi structure
12308  * @seed: RSS hash seed
12309  * @lut: Lookup table
12310  * @lut_size: Lookup table size
12311  *
12312  * Returns 0 on success, negative on failure
12313  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)12314 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12315 			       const u8 *lut, u16 lut_size)
12316 {
12317 	struct i40e_pf *pf = vsi->back;
12318 	struct i40e_hw *hw = &pf->hw;
12319 	u16 vf_id = vsi->vf_id;
12320 	u8 i;
12321 
12322 	/* Fill out hash function seed */
12323 	if (seed) {
12324 		u32 *seed_dw = (u32 *)seed;
12325 
12326 		if (vsi->type == I40E_VSI_MAIN) {
12327 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12328 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12329 		} else if (vsi->type == I40E_VSI_SRIOV) {
12330 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12331 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12332 		} else {
12333 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12334 		}
12335 	}
12336 
12337 	if (lut) {
12338 		u32 *lut_dw = (u32 *)lut;
12339 
12340 		if (vsi->type == I40E_VSI_MAIN) {
12341 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12342 				return -EINVAL;
12343 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12344 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12345 		} else if (vsi->type == I40E_VSI_SRIOV) {
12346 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12347 				return -EINVAL;
12348 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12349 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12350 		} else {
12351 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12352 		}
12353 	}
12354 	i40e_flush(hw);
12355 
12356 	return 0;
12357 }
12358 
12359 /**
12360  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12361  * @vsi: Pointer to VSI structure
12362  * @seed: Buffer to store the keys
12363  * @lut: Buffer to store the lookup table entries
12364  * @lut_size: Size of buffer to store the lookup table entries
12365  *
12366  * Returns 0 on success, negative on failure
12367  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12368 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12369 			    u8 *lut, u16 lut_size)
12370 {
12371 	struct i40e_pf *pf = vsi->back;
12372 	struct i40e_hw *hw = &pf->hw;
12373 	u16 i;
12374 
12375 	if (seed) {
12376 		u32 *seed_dw = (u32 *)seed;
12377 
12378 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12379 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12380 	}
12381 	if (lut) {
12382 		u32 *lut_dw = (u32 *)lut;
12383 
12384 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12385 			return -EINVAL;
12386 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12387 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12388 	}
12389 
12390 	return 0;
12391 }
12392 
12393 /**
12394  * i40e_config_rss - Configure RSS keys and lut
12395  * @vsi: Pointer to VSI structure
12396  * @seed: RSS hash seed
12397  * @lut: Lookup table
12398  * @lut_size: Lookup table size
12399  *
12400  * Returns 0 on success, negative on failure
12401  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12402 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12403 {
12404 	struct i40e_pf *pf = vsi->back;
12405 
12406 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12407 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12408 	else
12409 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12410 }
12411 
12412 /**
12413  * i40e_get_rss - Get RSS keys and lut
12414  * @vsi: Pointer to VSI structure
12415  * @seed: Buffer to store the keys
12416  * @lut: Buffer to store the lookup table entries
12417  * @lut_size: Size of buffer to store the lookup table entries
12418  *
12419  * Returns 0 on success, negative on failure
12420  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12421 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12422 {
12423 	struct i40e_pf *pf = vsi->back;
12424 
12425 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12426 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12427 	else
12428 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12429 }
12430 
12431 /**
12432  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12433  * @pf: Pointer to board private structure
12434  * @lut: Lookup table
12435  * @rss_table_size: Lookup table size
12436  * @rss_size: Range of queue number for hashing
12437  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)12438 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12439 		       u16 rss_table_size, u16 rss_size)
12440 {
12441 	u16 i;
12442 
12443 	for (i = 0; i < rss_table_size; i++)
12444 		lut[i] = i % rss_size;
12445 }
12446 
12447 /**
12448  * i40e_pf_config_rss - Prepare for RSS if used
12449  * @pf: board private structure
12450  **/
i40e_pf_config_rss(struct i40e_pf * pf)12451 static int i40e_pf_config_rss(struct i40e_pf *pf)
12452 {
12453 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12454 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12455 	u8 *lut;
12456 	struct i40e_hw *hw = &pf->hw;
12457 	u32 reg_val;
12458 	u64 hena;
12459 	int ret;
12460 
12461 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12462 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12463 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12464 	hena |= i40e_pf_get_default_rss_hena(pf);
12465 
12466 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12467 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12468 
12469 	/* Determine the RSS table size based on the hardware capabilities */
12470 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12471 	reg_val = (pf->rss_table_size == 512) ?
12472 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12473 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12474 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12475 
12476 	/* Determine the RSS size of the VSI */
12477 	if (!vsi->rss_size) {
12478 		u16 qcount;
12479 		/* If the firmware does something weird during VSI init, we
12480 		 * could end up with zero TCs. Check for that to avoid
12481 		 * divide-by-zero. It probably won't pass traffic, but it also
12482 		 * won't panic.
12483 		 */
12484 		qcount = vsi->num_queue_pairs /
12485 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12486 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12487 	}
12488 	if (!vsi->rss_size)
12489 		return -EINVAL;
12490 
12491 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12492 	if (!lut)
12493 		return -ENOMEM;
12494 
12495 	/* Use user configured lut if there is one, otherwise use default */
12496 	if (vsi->rss_lut_user)
12497 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12498 	else
12499 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12500 
12501 	/* Use user configured hash key if there is one, otherwise
12502 	 * use default.
12503 	 */
12504 	if (vsi->rss_hkey_user)
12505 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12506 	else
12507 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12508 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12509 	kfree(lut);
12510 
12511 	return ret;
12512 }
12513 
12514 /**
12515  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12516  * @pf: board private structure
12517  * @queue_count: the requested queue count for rss.
12518  *
12519  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12520  * count which may be different from the requested queue count.
12521  * Note: expects to be called while under rtnl_lock()
12522  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)12523 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12524 {
12525 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12526 	int new_rss_size;
12527 
12528 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12529 		return 0;
12530 
12531 	queue_count = min_t(int, queue_count, num_online_cpus());
12532 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12533 
12534 	if (queue_count != vsi->num_queue_pairs) {
12535 		u16 qcount;
12536 
12537 		vsi->req_queue_pairs = queue_count;
12538 		i40e_prep_for_reset(pf);
12539 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12540 			return pf->alloc_rss_size;
12541 
12542 		pf->alloc_rss_size = new_rss_size;
12543 
12544 		i40e_reset_and_rebuild(pf, true, true);
12545 
12546 		/* Discard the user configured hash keys and lut, if less
12547 		 * queues are enabled.
12548 		 */
12549 		if (queue_count < vsi->rss_size) {
12550 			i40e_clear_rss_config_user(vsi);
12551 			dev_dbg(&pf->pdev->dev,
12552 				"discard user configured hash keys and lut\n");
12553 		}
12554 
12555 		/* Reset vsi->rss_size, as number of enabled queues changed */
12556 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12557 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12558 
12559 		i40e_pf_config_rss(pf);
12560 	}
12561 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12562 		 vsi->req_queue_pairs, pf->rss_size_max);
12563 	return pf->alloc_rss_size;
12564 }
12565 
12566 /**
12567  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12568  * @pf: board private structure
12569  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)12570 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12571 {
12572 	bool min_valid, max_valid;
12573 	u32 max_bw, min_bw;
12574 	int status;
12575 
12576 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12577 					   &min_valid, &max_valid);
12578 
12579 	if (!status) {
12580 		if (min_valid)
12581 			pf->min_bw = min_bw;
12582 		if (max_valid)
12583 			pf->max_bw = max_bw;
12584 	}
12585 
12586 	return status;
12587 }
12588 
12589 /**
12590  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12591  * @pf: board private structure
12592  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)12593 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12594 {
12595 	struct i40e_aqc_configure_partition_bw_data bw_data;
12596 	int status;
12597 
12598 	memset(&bw_data, 0, sizeof(bw_data));
12599 
12600 	/* Set the valid bit for this PF */
12601 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12602 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12603 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12604 
12605 	/* Set the new bandwidths */
12606 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12607 
12608 	return status;
12609 }
12610 
12611 /**
12612  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12613  * @pf: board private structure
12614  **/
i40e_commit_partition_bw_setting(struct i40e_pf * pf)12615 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12616 {
12617 	/* Commit temporary BW setting to permanent NVM image */
12618 	enum i40e_admin_queue_err last_aq_status;
12619 	u16 nvm_word;
12620 	int ret;
12621 
12622 	if (pf->hw.partition_id != 1) {
12623 		dev_info(&pf->pdev->dev,
12624 			 "Commit BW only works on partition 1! This is partition %d",
12625 			 pf->hw.partition_id);
12626 		ret = -EOPNOTSUPP;
12627 		goto bw_commit_out;
12628 	}
12629 
12630 	/* Acquire NVM for read access */
12631 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12632 	last_aq_status = pf->hw.aq.asq_last_status;
12633 	if (ret) {
12634 		dev_info(&pf->pdev->dev,
12635 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12636 			 ERR_PTR(ret),
12637 			 i40e_aq_str(&pf->hw, last_aq_status));
12638 		goto bw_commit_out;
12639 	}
12640 
12641 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12642 	ret = i40e_aq_read_nvm(&pf->hw,
12643 			       I40E_SR_NVM_CONTROL_WORD,
12644 			       0x10, sizeof(nvm_word), &nvm_word,
12645 			       false, NULL);
12646 	/* Save off last admin queue command status before releasing
12647 	 * the NVM
12648 	 */
12649 	last_aq_status = pf->hw.aq.asq_last_status;
12650 	i40e_release_nvm(&pf->hw);
12651 	if (ret) {
12652 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12653 			 ERR_PTR(ret),
12654 			 i40e_aq_str(&pf->hw, last_aq_status));
12655 		goto bw_commit_out;
12656 	}
12657 
12658 	/* Wait a bit for NVM release to complete */
12659 	msleep(50);
12660 
12661 	/* Acquire NVM for write access */
12662 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12663 	last_aq_status = pf->hw.aq.asq_last_status;
12664 	if (ret) {
12665 		dev_info(&pf->pdev->dev,
12666 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12667 			 ERR_PTR(ret),
12668 			 i40e_aq_str(&pf->hw, last_aq_status));
12669 		goto bw_commit_out;
12670 	}
12671 	/* Write it back out unchanged to initiate update NVM,
12672 	 * which will force a write of the shadow (alt) RAM to
12673 	 * the NVM - thus storing the bandwidth values permanently.
12674 	 */
12675 	ret = i40e_aq_update_nvm(&pf->hw,
12676 				 I40E_SR_NVM_CONTROL_WORD,
12677 				 0x10, sizeof(nvm_word),
12678 				 &nvm_word, true, 0, NULL);
12679 	/* Save off last admin queue command status before releasing
12680 	 * the NVM
12681 	 */
12682 	last_aq_status = pf->hw.aq.asq_last_status;
12683 	i40e_release_nvm(&pf->hw);
12684 	if (ret)
12685 		dev_info(&pf->pdev->dev,
12686 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12687 			 ERR_PTR(ret),
12688 			 i40e_aq_str(&pf->hw, last_aq_status));
12689 bw_commit_out:
12690 
12691 	return ret;
12692 }
12693 
12694 /**
12695  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12696  * if total port shutdown feature is enabled for this PF
12697  * @pf: board private structure
12698  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)12699 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12700 {
12701 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12702 #define I40E_FEATURES_ENABLE_PTR		0x2A
12703 #define I40E_CURRENT_SETTING_PTR		0x2B
12704 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12705 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12706 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12707 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12708 	u16 sr_emp_sr_settings_ptr = 0;
12709 	u16 features_enable = 0;
12710 	u16 link_behavior = 0;
12711 	int read_status = 0;
12712 	bool ret = false;
12713 
12714 	read_status = i40e_read_nvm_word(&pf->hw,
12715 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12716 					 &sr_emp_sr_settings_ptr);
12717 	if (read_status)
12718 		goto err_nvm;
12719 	read_status = i40e_read_nvm_word(&pf->hw,
12720 					 sr_emp_sr_settings_ptr +
12721 					 I40E_FEATURES_ENABLE_PTR,
12722 					 &features_enable);
12723 	if (read_status)
12724 		goto err_nvm;
12725 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12726 		read_status = i40e_read_nvm_module_data(&pf->hw,
12727 							I40E_SR_EMP_SR_SETTINGS_PTR,
12728 							I40E_CURRENT_SETTING_PTR,
12729 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12730 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12731 							&link_behavior);
12732 		if (read_status)
12733 			goto err_nvm;
12734 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12735 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12736 	}
12737 	return ret;
12738 
12739 err_nvm:
12740 	dev_warn(&pf->pdev->dev,
12741 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12742 		 ERR_PTR(read_status));
12743 	return ret;
12744 }
12745 
12746 /**
12747  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12748  * @pf: board private structure to initialize
12749  *
12750  * i40e_sw_init initializes the Adapter private data structure.
12751  * Fields are initialized based on PCI device information and
12752  * OS network device settings (MTU size).
12753  **/
i40e_sw_init(struct i40e_pf * pf)12754 static int i40e_sw_init(struct i40e_pf *pf)
12755 {
12756 	int err = 0;
12757 	int size;
12758 	u16 pow;
12759 
12760 	/* Set default capability flags */
12761 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12762 		    I40E_FLAG_MSI_ENABLED     |
12763 		    I40E_FLAG_MSIX_ENABLED;
12764 
12765 	/* Set default ITR */
12766 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12767 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12768 
12769 	/* Depending on PF configurations, it is possible that the RSS
12770 	 * maximum might end up larger than the available queues
12771 	 */
12772 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12773 	pf->alloc_rss_size = 1;
12774 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12775 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12776 				 pf->hw.func_caps.num_tx_qp);
12777 
12778 	/* find the next higher power-of-2 of num cpus */
12779 	pow = roundup_pow_of_two(num_online_cpus());
12780 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12781 
12782 	if (pf->hw.func_caps.rss) {
12783 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12784 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12785 					   num_online_cpus());
12786 	}
12787 
12788 	/* MFP mode enabled */
12789 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12790 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12791 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12792 		if (i40e_get_partition_bw_setting(pf)) {
12793 			dev_warn(&pf->pdev->dev,
12794 				 "Could not get partition bw settings\n");
12795 		} else {
12796 			dev_info(&pf->pdev->dev,
12797 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12798 				 pf->min_bw, pf->max_bw);
12799 
12800 			/* nudge the Tx scheduler */
12801 			i40e_set_partition_bw_setting(pf);
12802 		}
12803 	}
12804 
12805 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12806 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12807 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12808 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12809 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12810 		    pf->hw.num_partitions > 1)
12811 			dev_info(&pf->pdev->dev,
12812 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12813 		else
12814 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12815 		pf->fdir_pf_filter_count =
12816 				 pf->hw.func_caps.fd_filters_guaranteed;
12817 		pf->hw.fdir_shared_filter_count =
12818 				 pf->hw.func_caps.fd_filters_best_effort;
12819 	}
12820 
12821 	if (pf->hw.mac.type == I40E_MAC_X722) {
12822 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12823 				    I40E_HW_128_QP_RSS_CAPABLE |
12824 				    I40E_HW_ATR_EVICT_CAPABLE |
12825 				    I40E_HW_WB_ON_ITR_CAPABLE |
12826 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12827 				    I40E_HW_NO_PCI_LINK_CHECK |
12828 				    I40E_HW_USE_SET_LLDP_MIB |
12829 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12830 				    I40E_HW_PTP_L4_CAPABLE |
12831 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12832 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12833 
12834 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12835 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12836 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12837 			dev_warn(&pf->pdev->dev,
12838 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12839 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12840 		}
12841 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12842 		   ((pf->hw.aq.api_maj_ver == 1) &&
12843 		    (pf->hw.aq.api_min_ver > 4))) {
12844 		/* Supported in FW API version higher than 1.4 */
12845 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12846 	}
12847 
12848 	/* Enable HW ATR eviction if possible */
12849 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12850 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12851 
12852 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12853 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12854 	    (pf->hw.aq.fw_maj_ver < 4))) {
12855 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12856 		/* No DCB support  for FW < v4.33 */
12857 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12858 	}
12859 
12860 	/* Disable FW LLDP if FW < v4.3 */
12861 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12862 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12863 	    (pf->hw.aq.fw_maj_ver < 4)))
12864 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12865 
12866 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12867 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12868 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12869 	    (pf->hw.aq.fw_maj_ver >= 5)))
12870 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12871 
12872 	/* Enable PTP L4 if FW > v6.0 */
12873 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12874 	    pf->hw.aq.fw_maj_ver >= 6)
12875 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12876 
12877 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12878 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12879 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12880 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12881 	}
12882 
12883 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12884 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12885 		/* IWARP needs one extra vector for CQP just like MISC.*/
12886 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12887 	}
12888 	/* Stopping FW LLDP engine is supported on XL710 and X722
12889 	 * starting from FW versions determined in i40e_init_adminq.
12890 	 * Stopping the FW LLDP engine is not supported on XL710
12891 	 * if NPAR is functioning so unset this hw flag in this case.
12892 	 */
12893 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12894 	    pf->hw.func_caps.npar_enable &&
12895 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12896 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12897 
12898 #ifdef CONFIG_PCI_IOV
12899 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12900 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12901 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12902 		pf->num_req_vfs = min_t(int,
12903 					pf->hw.func_caps.num_vfs,
12904 					I40E_MAX_VF_COUNT);
12905 	}
12906 #endif /* CONFIG_PCI_IOV */
12907 	pf->eeprom_version = 0xDEAD;
12908 	pf->lan_veb = I40E_NO_VEB;
12909 	pf->lan_vsi = I40E_NO_VSI;
12910 
12911 	/* By default FW has this off for performance reasons */
12912 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12913 
12914 	/* set up queue assignment tracking */
12915 	size = sizeof(struct i40e_lump_tracking)
12916 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12917 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12918 	if (!pf->qp_pile) {
12919 		err = -ENOMEM;
12920 		goto sw_init_done;
12921 	}
12922 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12923 
12924 	pf->tx_timeout_recovery_level = 1;
12925 
12926 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12927 	    i40e_is_total_port_shutdown_enabled(pf)) {
12928 		/* Link down on close must be on when total port shutdown
12929 		 * is enabled for a given port
12930 		 */
12931 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12932 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12933 		dev_info(&pf->pdev->dev,
12934 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12935 	}
12936 	mutex_init(&pf->switch_mutex);
12937 
12938 sw_init_done:
12939 	return err;
12940 }
12941 
12942 /**
12943  * i40e_set_ntuple - set the ntuple feature flag and take action
12944  * @pf: board private structure to initialize
12945  * @features: the feature set that the stack is suggesting
12946  *
12947  * returns a bool to indicate if reset needs to happen
12948  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12949 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12950 {
12951 	bool need_reset = false;
12952 
12953 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12954 	 * the state changed, we need to reset.
12955 	 */
12956 	if (features & NETIF_F_NTUPLE) {
12957 		/* Enable filters and mark for reset */
12958 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12959 			need_reset = true;
12960 		/* enable FD_SB only if there is MSI-X vector and no cloud
12961 		 * filters exist
12962 		 */
12963 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12964 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12965 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12966 		}
12967 	} else {
12968 		/* turn off filters, mark for reset and clear SW filter list */
12969 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12970 			need_reset = true;
12971 			i40e_fdir_filter_exit(pf);
12972 		}
12973 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12974 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12975 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12976 
12977 		/* reset fd counters */
12978 		pf->fd_add_err = 0;
12979 		pf->fd_atr_cnt = 0;
12980 		/* if ATR was auto disabled it can be re-enabled. */
12981 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12982 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12983 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12984 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12985 	}
12986 	return need_reset;
12987 }
12988 
12989 /**
12990  * i40e_clear_rss_lut - clear the rx hash lookup table
12991  * @vsi: the VSI being configured
12992  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12993 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12994 {
12995 	struct i40e_pf *pf = vsi->back;
12996 	struct i40e_hw *hw = &pf->hw;
12997 	u16 vf_id = vsi->vf_id;
12998 	u8 i;
12999 
13000 	if (vsi->type == I40E_VSI_MAIN) {
13001 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
13002 			wr32(hw, I40E_PFQF_HLUT(i), 0);
13003 	} else if (vsi->type == I40E_VSI_SRIOV) {
13004 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
13005 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
13006 	} else {
13007 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
13008 	}
13009 }
13010 
13011 /**
13012  * i40e_set_loopback - turn on/off loopback mode on underlying PF
13013  * @vsi: ptr to VSI
13014  * @ena: flag to indicate the on/off setting
13015  */
i40e_set_loopback(struct i40e_vsi * vsi,bool ena)13016 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
13017 {
13018 	bool if_running = netif_running(vsi->netdev) &&
13019 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
13020 	int ret;
13021 
13022 	if (if_running)
13023 		i40e_down(vsi);
13024 
13025 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
13026 	if (ret)
13027 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
13028 	if (if_running)
13029 		i40e_up(vsi);
13030 
13031 	return ret;
13032 }
13033 
13034 /**
13035  * i40e_set_features - set the netdev feature flags
13036  * @netdev: ptr to the netdev being adjusted
13037  * @features: the feature set that the stack is suggesting
13038  * Note: expects to be called while under rtnl_lock()
13039  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)13040 static int i40e_set_features(struct net_device *netdev,
13041 			     netdev_features_t features)
13042 {
13043 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13044 	struct i40e_vsi *vsi = np->vsi;
13045 	struct i40e_pf *pf = vsi->back;
13046 	bool need_reset;
13047 
13048 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
13049 		i40e_pf_config_rss(pf);
13050 	else if (!(features & NETIF_F_RXHASH) &&
13051 		 netdev->features & NETIF_F_RXHASH)
13052 		i40e_clear_rss_lut(vsi);
13053 
13054 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
13055 		i40e_vlan_stripping_enable(vsi);
13056 	else
13057 		i40e_vlan_stripping_disable(vsi);
13058 
13059 	if (!(features & NETIF_F_HW_TC) &&
13060 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
13061 		dev_err(&pf->pdev->dev,
13062 			"Offloaded tc filters active, can't turn hw_tc_offload off");
13063 		return -EINVAL;
13064 	}
13065 
13066 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
13067 		i40e_del_all_macvlans(vsi);
13068 
13069 	need_reset = i40e_set_ntuple(pf, features);
13070 
13071 	if (need_reset)
13072 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13073 
13074 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
13075 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
13076 
13077 	return 0;
13078 }
13079 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)13080 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
13081 				    unsigned int table, unsigned int idx,
13082 				    struct udp_tunnel_info *ti)
13083 {
13084 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13085 	struct i40e_hw *hw = &np->vsi->back->hw;
13086 	u8 type, filter_index;
13087 	int ret;
13088 
13089 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13090 						   I40E_AQC_TUNNEL_TYPE_NGE;
13091 
13092 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13093 				     NULL);
13094 	if (ret) {
13095 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13096 			    ERR_PTR(ret),
13097 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13098 		return -EIO;
13099 	}
13100 
13101 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13102 	return 0;
13103 }
13104 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)13105 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13106 				      unsigned int table, unsigned int idx,
13107 				      struct udp_tunnel_info *ti)
13108 {
13109 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13110 	struct i40e_hw *hw = &np->vsi->back->hw;
13111 	int ret;
13112 
13113 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13114 	if (ret) {
13115 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13116 			    ERR_PTR(ret),
13117 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13118 		return -EIO;
13119 	}
13120 
13121 	return 0;
13122 }
13123 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)13124 static int i40e_get_phys_port_id(struct net_device *netdev,
13125 				 struct netdev_phys_item_id *ppid)
13126 {
13127 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13128 	struct i40e_pf *pf = np->vsi->back;
13129 	struct i40e_hw *hw = &pf->hw;
13130 
13131 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13132 		return -EOPNOTSUPP;
13133 
13134 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13135 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13136 
13137 	return 0;
13138 }
13139 
13140 /**
13141  * i40e_ndo_fdb_add - add an entry to the hardware database
13142  * @ndm: the input from the stack
13143  * @tb: pointer to array of nladdr (unused)
13144  * @dev: the net device pointer
13145  * @addr: the MAC address entry being added
13146  * @vid: VLAN ID
13147  * @flags: instructions from stack about fdb operation
13148  * @extack: netlink extended ack, unused currently
13149  */
i40e_ndo_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags,struct netlink_ext_ack * extack)13150 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13151 			    struct net_device *dev,
13152 			    const unsigned char *addr, u16 vid,
13153 			    u16 flags,
13154 			    struct netlink_ext_ack *extack)
13155 {
13156 	struct i40e_netdev_priv *np = netdev_priv(dev);
13157 	struct i40e_pf *pf = np->vsi->back;
13158 	int err = 0;
13159 
13160 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13161 		return -EOPNOTSUPP;
13162 
13163 	if (vid) {
13164 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13165 		return -EINVAL;
13166 	}
13167 
13168 	/* Hardware does not support aging addresses so if a
13169 	 * ndm_state is given only allow permanent addresses
13170 	 */
13171 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13172 		netdev_info(dev, "FDB only supports static addresses\n");
13173 		return -EINVAL;
13174 	}
13175 
13176 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13177 		err = dev_uc_add_excl(dev, addr);
13178 	else if (is_multicast_ether_addr(addr))
13179 		err = dev_mc_add_excl(dev, addr);
13180 	else
13181 		err = -EINVAL;
13182 
13183 	/* Only return duplicate errors if NLM_F_EXCL is set */
13184 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13185 		err = 0;
13186 
13187 	return err;
13188 }
13189 
13190 /**
13191  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13192  * @dev: the netdev being configured
13193  * @nlh: RTNL message
13194  * @flags: bridge flags
13195  * @extack: netlink extended ack
13196  *
13197  * Inserts a new hardware bridge if not already created and
13198  * enables the bridging mode requested (VEB or VEPA). If the
13199  * hardware bridge has already been inserted and the request
13200  * is to change the mode then that requires a PF reset to
13201  * allow rebuild of the components with required hardware
13202  * bridge mode enabled.
13203  *
13204  * Note: expects to be called while under rtnl_lock()
13205  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)13206 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13207 				   struct nlmsghdr *nlh,
13208 				   u16 flags,
13209 				   struct netlink_ext_ack *extack)
13210 {
13211 	struct i40e_netdev_priv *np = netdev_priv(dev);
13212 	struct i40e_vsi *vsi = np->vsi;
13213 	struct i40e_pf *pf = vsi->back;
13214 	struct i40e_veb *veb = NULL;
13215 	struct nlattr *attr, *br_spec;
13216 	int i, rem;
13217 
13218 	/* Only for PF VSI for now */
13219 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13220 		return -EOPNOTSUPP;
13221 
13222 	/* Find the HW bridge for PF VSI */
13223 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13224 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13225 			veb = pf->veb[i];
13226 	}
13227 
13228 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13229 	if (!br_spec)
13230 		return -EINVAL;
13231 
13232 	nla_for_each_nested(attr, br_spec, rem) {
13233 		__u16 mode;
13234 
13235 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13236 			continue;
13237 
13238 		mode = nla_get_u16(attr);
13239 		if ((mode != BRIDGE_MODE_VEPA) &&
13240 		    (mode != BRIDGE_MODE_VEB))
13241 			return -EINVAL;
13242 
13243 		/* Insert a new HW bridge */
13244 		if (!veb) {
13245 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13246 					     vsi->tc_config.enabled_tc);
13247 			if (veb) {
13248 				veb->bridge_mode = mode;
13249 				i40e_config_bridge_mode(veb);
13250 			} else {
13251 				/* No Bridge HW offload available */
13252 				return -ENOENT;
13253 			}
13254 			break;
13255 		} else if (mode != veb->bridge_mode) {
13256 			/* Existing HW bridge but different mode needs reset */
13257 			veb->bridge_mode = mode;
13258 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13259 			if (mode == BRIDGE_MODE_VEB)
13260 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13261 			else
13262 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13263 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13264 			break;
13265 		}
13266 	}
13267 
13268 	return 0;
13269 }
13270 
13271 /**
13272  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13273  * @skb: skb buff
13274  * @pid: process id
13275  * @seq: RTNL message seq #
13276  * @dev: the netdev being configured
13277  * @filter_mask: unused
13278  * @nlflags: netlink flags passed in
13279  *
13280  * Return the mode in which the hardware bridge is operating in
13281  * i.e VEB or VEPA.
13282  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)13283 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13284 				   struct net_device *dev,
13285 				   u32 __always_unused filter_mask,
13286 				   int nlflags)
13287 {
13288 	struct i40e_netdev_priv *np = netdev_priv(dev);
13289 	struct i40e_vsi *vsi = np->vsi;
13290 	struct i40e_pf *pf = vsi->back;
13291 	struct i40e_veb *veb = NULL;
13292 	int i;
13293 
13294 	/* Only for PF VSI for now */
13295 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13296 		return -EOPNOTSUPP;
13297 
13298 	/* Find the HW bridge for the PF VSI */
13299 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13300 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13301 			veb = pf->veb[i];
13302 	}
13303 
13304 	if (!veb)
13305 		return 0;
13306 
13307 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13308 				       0, 0, nlflags, filter_mask, NULL);
13309 }
13310 
13311 /**
13312  * i40e_features_check - Validate encapsulated packet conforms to limits
13313  * @skb: skb buff
13314  * @dev: This physical port's netdev
13315  * @features: Offload features that the stack believes apply
13316  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13317 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13318 					     struct net_device *dev,
13319 					     netdev_features_t features)
13320 {
13321 	size_t len;
13322 
13323 	/* No point in doing any of this if neither checksum nor GSO are
13324 	 * being requested for this frame.  We can rule out both by just
13325 	 * checking for CHECKSUM_PARTIAL
13326 	 */
13327 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13328 		return features;
13329 
13330 	/* We cannot support GSO if the MSS is going to be less than
13331 	 * 64 bytes.  If it is then we need to drop support for GSO.
13332 	 */
13333 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13334 		features &= ~NETIF_F_GSO_MASK;
13335 
13336 	/* MACLEN can support at most 63 words */
13337 	len = skb_network_header(skb) - skb->data;
13338 	if (len & ~(63 * 2))
13339 		goto out_err;
13340 
13341 	/* IPLEN and EIPLEN can support at most 127 dwords */
13342 	len = skb_transport_header(skb) - skb_network_header(skb);
13343 	if (len & ~(127 * 4))
13344 		goto out_err;
13345 
13346 	if (skb->encapsulation) {
13347 		/* L4TUNLEN can support 127 words */
13348 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13349 		if (len & ~(127 * 2))
13350 			goto out_err;
13351 
13352 		/* IPLEN can support at most 127 dwords */
13353 		len = skb_inner_transport_header(skb) -
13354 		      skb_inner_network_header(skb);
13355 		if (len & ~(127 * 4))
13356 			goto out_err;
13357 	}
13358 
13359 	/* No need to validate L4LEN as TCP is the only protocol with a
13360 	 * flexible value and we support all possible values supported
13361 	 * by TCP, which is at most 15 dwords
13362 	 */
13363 
13364 	return features;
13365 out_err:
13366 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13367 }
13368 
13369 /**
13370  * i40e_xdp_setup - add/remove an XDP program
13371  * @vsi: VSI to changed
13372  * @prog: XDP program
13373  * @extack: netlink extended ack
13374  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)13375 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13376 			  struct netlink_ext_ack *extack)
13377 {
13378 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13379 	struct i40e_pf *pf = vsi->back;
13380 	struct bpf_prog *old_prog;
13381 	bool need_reset;
13382 	int i;
13383 
13384 	/* VSI shall be deleted in a moment, block loading new programs */
13385 	if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
13386 		return -EINVAL;
13387 
13388 	/* Don't allow frames that span over multiple buffers */
13389 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13390 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13391 		return -EINVAL;
13392 	}
13393 
13394 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13395 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13396 	if (need_reset)
13397 		i40e_prep_for_reset(pf);
13398 
13399 	old_prog = xchg(&vsi->xdp_prog, prog);
13400 
13401 	if (need_reset) {
13402 		if (!prog) {
13403 			xdp_features_clear_redirect_target(vsi->netdev);
13404 			/* Wait until ndo_xsk_wakeup completes. */
13405 			synchronize_rcu();
13406 		}
13407 		i40e_reset_and_rebuild(pf, true, true);
13408 	}
13409 
13410 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13411 		if (i40e_realloc_rx_bi_zc(vsi, true))
13412 			return -ENOMEM;
13413 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13414 		if (i40e_realloc_rx_bi_zc(vsi, false))
13415 			return -ENOMEM;
13416 	}
13417 
13418 	for (i = 0; i < vsi->num_queue_pairs; i++)
13419 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13420 
13421 	if (old_prog)
13422 		bpf_prog_put(old_prog);
13423 
13424 	/* Kick start the NAPI context if there is an AF_XDP socket open
13425 	 * on that queue id. This so that receiving will start.
13426 	 */
13427 	if (need_reset && prog) {
13428 		for (i = 0; i < vsi->num_queue_pairs; i++)
13429 			if (vsi->xdp_rings[i]->xsk_pool)
13430 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13431 						      XDP_WAKEUP_RX);
13432 		xdp_features_set_redirect_target(vsi->netdev, true);
13433 	}
13434 
13435 	return 0;
13436 }
13437 
13438 /**
13439  * i40e_enter_busy_conf - Enters busy config state
13440  * @vsi: vsi
13441  *
13442  * Returns 0 on success, <0 for failure.
13443  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)13444 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13445 {
13446 	struct i40e_pf *pf = vsi->back;
13447 	int timeout = 50;
13448 
13449 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13450 		timeout--;
13451 		if (!timeout)
13452 			return -EBUSY;
13453 		usleep_range(1000, 2000);
13454 	}
13455 
13456 	return 0;
13457 }
13458 
13459 /**
13460  * i40e_exit_busy_conf - Exits busy config state
13461  * @vsi: vsi
13462  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)13463 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13464 {
13465 	struct i40e_pf *pf = vsi->back;
13466 
13467 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13468 }
13469 
13470 /**
13471  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13472  * @vsi: vsi
13473  * @queue_pair: queue pair
13474  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)13475 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13476 {
13477 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13478 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13479 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13480 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13481 	if (i40e_enabled_xdp_vsi(vsi)) {
13482 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13483 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13484 	}
13485 }
13486 
13487 /**
13488  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13489  * @vsi: vsi
13490  * @queue_pair: queue pair
13491  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)13492 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13493 {
13494 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13495 	if (i40e_enabled_xdp_vsi(vsi)) {
13496 		/* Make sure that in-progress ndo_xdp_xmit calls are
13497 		 * completed.
13498 		 */
13499 		synchronize_rcu();
13500 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13501 	}
13502 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13503 }
13504 
13505 /**
13506  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13507  * @vsi: vsi
13508  * @queue_pair: queue pair
13509  * @enable: true for enable, false for disable
13510  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)13511 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13512 					bool enable)
13513 {
13514 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13515 	struct i40e_q_vector *q_vector = rxr->q_vector;
13516 
13517 	if (!vsi->netdev)
13518 		return;
13519 
13520 	/* All rings in a qp belong to the same qvector. */
13521 	if (q_vector->rx.ring || q_vector->tx.ring) {
13522 		if (enable)
13523 			napi_enable(&q_vector->napi);
13524 		else
13525 			napi_disable(&q_vector->napi);
13526 	}
13527 }
13528 
13529 /**
13530  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13531  * @vsi: vsi
13532  * @queue_pair: queue pair
13533  * @enable: true for enable, false for disable
13534  *
13535  * Returns 0 on success, <0 on failure.
13536  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)13537 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13538 					bool enable)
13539 {
13540 	struct i40e_pf *pf = vsi->back;
13541 	int pf_q, ret = 0;
13542 
13543 	pf_q = vsi->base_queue + queue_pair;
13544 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13545 				     false /*is xdp*/, enable);
13546 	if (ret) {
13547 		dev_info(&pf->pdev->dev,
13548 			 "VSI seid %d Tx ring %d %sable timeout\n",
13549 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13550 		return ret;
13551 	}
13552 
13553 	i40e_control_rx_q(pf, pf_q, enable);
13554 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13555 	if (ret) {
13556 		dev_info(&pf->pdev->dev,
13557 			 "VSI seid %d Rx ring %d %sable timeout\n",
13558 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13559 		return ret;
13560 	}
13561 
13562 	/* Due to HW errata, on Rx disable only, the register can
13563 	 * indicate done before it really is. Needs 50ms to be sure
13564 	 */
13565 	if (!enable)
13566 		mdelay(50);
13567 
13568 	if (!i40e_enabled_xdp_vsi(vsi))
13569 		return ret;
13570 
13571 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13572 				     pf_q + vsi->alloc_queue_pairs,
13573 				     true /*is xdp*/, enable);
13574 	if (ret) {
13575 		dev_info(&pf->pdev->dev,
13576 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13577 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13578 	}
13579 
13580 	return ret;
13581 }
13582 
13583 /**
13584  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13585  * @vsi: vsi
13586  * @queue_pair: queue_pair
13587  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)13588 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13589 {
13590 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13591 	struct i40e_pf *pf = vsi->back;
13592 	struct i40e_hw *hw = &pf->hw;
13593 
13594 	/* All rings in a qp belong to the same qvector. */
13595 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13596 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13597 	else
13598 		i40e_irq_dynamic_enable_icr0(pf);
13599 
13600 	i40e_flush(hw);
13601 }
13602 
13603 /**
13604  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13605  * @vsi: vsi
13606  * @queue_pair: queue_pair
13607  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)13608 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13609 {
13610 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13611 	struct i40e_pf *pf = vsi->back;
13612 	struct i40e_hw *hw = &pf->hw;
13613 
13614 	/* For simplicity, instead of removing the qp interrupt causes
13615 	 * from the interrupt linked list, we simply disable the interrupt, and
13616 	 * leave the list intact.
13617 	 *
13618 	 * All rings in a qp belong to the same qvector.
13619 	 */
13620 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13621 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13622 
13623 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13624 		i40e_flush(hw);
13625 		synchronize_irq(pf->msix_entries[intpf].vector);
13626 	} else {
13627 		/* Legacy and MSI mode - this stops all interrupt handling */
13628 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13629 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13630 		i40e_flush(hw);
13631 		synchronize_irq(pf->pdev->irq);
13632 	}
13633 }
13634 
13635 /**
13636  * i40e_queue_pair_disable - Disables a queue pair
13637  * @vsi: vsi
13638  * @queue_pair: queue pair
13639  *
13640  * Returns 0 on success, <0 on failure.
13641  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)13642 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13643 {
13644 	int err;
13645 
13646 	err = i40e_enter_busy_conf(vsi);
13647 	if (err)
13648 		return err;
13649 
13650 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13651 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13652 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13653 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13654 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13655 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13656 
13657 	return err;
13658 }
13659 
13660 /**
13661  * i40e_queue_pair_enable - Enables a queue pair
13662  * @vsi: vsi
13663  * @queue_pair: queue pair
13664  *
13665  * Returns 0 on success, <0 on failure.
13666  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)13667 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13668 {
13669 	int err;
13670 
13671 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13672 	if (err)
13673 		return err;
13674 
13675 	if (i40e_enabled_xdp_vsi(vsi)) {
13676 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13677 		if (err)
13678 			return err;
13679 	}
13680 
13681 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13682 	if (err)
13683 		return err;
13684 
13685 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13686 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13687 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13688 
13689 	i40e_exit_busy_conf(vsi);
13690 
13691 	return err;
13692 }
13693 
13694 /**
13695  * i40e_xdp - implements ndo_bpf for i40e
13696  * @dev: netdevice
13697  * @xdp: XDP command
13698  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)13699 static int i40e_xdp(struct net_device *dev,
13700 		    struct netdev_bpf *xdp)
13701 {
13702 	struct i40e_netdev_priv *np = netdev_priv(dev);
13703 	struct i40e_vsi *vsi = np->vsi;
13704 
13705 	if (vsi->type != I40E_VSI_MAIN)
13706 		return -EINVAL;
13707 
13708 	switch (xdp->command) {
13709 	case XDP_SETUP_PROG:
13710 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13711 	case XDP_SETUP_XSK_POOL:
13712 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13713 					   xdp->xsk.queue_id);
13714 	default:
13715 		return -EINVAL;
13716 	}
13717 }
13718 
13719 static const struct net_device_ops i40e_netdev_ops = {
13720 	.ndo_open		= i40e_open,
13721 	.ndo_stop		= i40e_close,
13722 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13723 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13724 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13725 	.ndo_validate_addr	= eth_validate_addr,
13726 	.ndo_set_mac_address	= i40e_set_mac,
13727 	.ndo_change_mtu		= i40e_change_mtu,
13728 	.ndo_eth_ioctl		= i40e_ioctl,
13729 	.ndo_tx_timeout		= i40e_tx_timeout,
13730 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13731 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13732 #ifdef CONFIG_NET_POLL_CONTROLLER
13733 	.ndo_poll_controller	= i40e_netpoll,
13734 #endif
13735 	.ndo_setup_tc		= __i40e_setup_tc,
13736 	.ndo_select_queue	= i40e_lan_select_queue,
13737 	.ndo_set_features	= i40e_set_features,
13738 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13739 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13740 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13741 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13742 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13743 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13744 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13745 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13746 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13747 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13748 	.ndo_features_check	= i40e_features_check,
13749 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13750 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13751 	.ndo_bpf		= i40e_xdp,
13752 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13753 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13754 	.ndo_dfwd_add_station	= i40e_fwd_add,
13755 	.ndo_dfwd_del_station	= i40e_fwd_del,
13756 };
13757 
13758 /**
13759  * i40e_config_netdev - Setup the netdev flags
13760  * @vsi: the VSI being configured
13761  *
13762  * Returns 0 on success, negative value on failure
13763  **/
i40e_config_netdev(struct i40e_vsi * vsi)13764 static int i40e_config_netdev(struct i40e_vsi *vsi)
13765 {
13766 	struct i40e_pf *pf = vsi->back;
13767 	struct i40e_hw *hw = &pf->hw;
13768 	struct i40e_netdev_priv *np;
13769 	struct net_device *netdev;
13770 	u8 broadcast[ETH_ALEN];
13771 	u8 mac_addr[ETH_ALEN];
13772 	int etherdev_size;
13773 	netdev_features_t hw_enc_features;
13774 	netdev_features_t hw_features;
13775 
13776 	etherdev_size = sizeof(struct i40e_netdev_priv);
13777 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13778 	if (!netdev)
13779 		return -ENOMEM;
13780 
13781 	vsi->netdev = netdev;
13782 	np = netdev_priv(netdev);
13783 	np->vsi = vsi;
13784 
13785 	hw_enc_features = NETIF_F_SG			|
13786 			  NETIF_F_HW_CSUM		|
13787 			  NETIF_F_HIGHDMA		|
13788 			  NETIF_F_SOFT_FEATURES		|
13789 			  NETIF_F_TSO			|
13790 			  NETIF_F_TSO_ECN		|
13791 			  NETIF_F_TSO6			|
13792 			  NETIF_F_GSO_GRE		|
13793 			  NETIF_F_GSO_GRE_CSUM		|
13794 			  NETIF_F_GSO_PARTIAL		|
13795 			  NETIF_F_GSO_IPXIP4		|
13796 			  NETIF_F_GSO_IPXIP6		|
13797 			  NETIF_F_GSO_UDP_TUNNEL	|
13798 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13799 			  NETIF_F_GSO_UDP_L4		|
13800 			  NETIF_F_SCTP_CRC		|
13801 			  NETIF_F_RXHASH		|
13802 			  NETIF_F_RXCSUM		|
13803 			  0;
13804 
13805 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13806 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13807 
13808 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13809 
13810 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13811 
13812 	netdev->hw_enc_features |= hw_enc_features;
13813 
13814 	/* record features VLANs can make use of */
13815 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13816 
13817 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13818 				   NETIF_F_GSO_GRE_CSUM |	\
13819 				   NETIF_F_GSO_IPXIP4 |		\
13820 				   NETIF_F_GSO_IPXIP6 |		\
13821 				   NETIF_F_GSO_UDP_TUNNEL |	\
13822 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13823 
13824 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13825 	netdev->features |= NETIF_F_GSO_PARTIAL |
13826 			    I40E_GSO_PARTIAL_FEATURES;
13827 
13828 	netdev->mpls_features |= NETIF_F_SG;
13829 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13830 	netdev->mpls_features |= NETIF_F_TSO;
13831 	netdev->mpls_features |= NETIF_F_TSO6;
13832 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13833 
13834 	/* enable macvlan offloads */
13835 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13836 
13837 	hw_features = hw_enc_features		|
13838 		      NETIF_F_HW_VLAN_CTAG_TX	|
13839 		      NETIF_F_HW_VLAN_CTAG_RX;
13840 
13841 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13842 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13843 
13844 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13845 
13846 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13847 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13848 
13849 	netdev->features &= ~NETIF_F_HW_TC;
13850 
13851 	if (vsi->type == I40E_VSI_MAIN) {
13852 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13853 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13854 		/* The following steps are necessary for two reasons. First,
13855 		 * some older NVM configurations load a default MAC-VLAN
13856 		 * filter that will accept any tagged packet, and we want to
13857 		 * replace this with a normal filter. Additionally, it is
13858 		 * possible our MAC address was provided by the platform using
13859 		 * Open Firmware or similar.
13860 		 *
13861 		 * Thus, we need to remove the default filter and install one
13862 		 * specific to the MAC address.
13863 		 */
13864 		i40e_rm_default_mac_filter(vsi, mac_addr);
13865 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13866 		i40e_add_mac_filter(vsi, mac_addr);
13867 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13868 
13869 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13870 				       NETDEV_XDP_ACT_REDIRECT |
13871 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13872 				       NETDEV_XDP_ACT_RX_SG;
13873 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13874 	} else {
13875 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13876 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13877 		 * the end, which is 4 bytes long, so force truncation of the
13878 		 * original name by IFNAMSIZ - 4
13879 		 */
13880 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13881 			 IFNAMSIZ - 4,
13882 			 pf->vsi[pf->lan_vsi]->netdev->name);
13883 		eth_random_addr(mac_addr);
13884 
13885 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13886 		i40e_add_mac_filter(vsi, mac_addr);
13887 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13888 	}
13889 
13890 	/* Add the broadcast filter so that we initially will receive
13891 	 * broadcast packets. Note that when a new VLAN is first added the
13892 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13893 	 * specific filters as part of transitioning into "vlan" operation.
13894 	 * When more VLANs are added, the driver will copy each existing MAC
13895 	 * filter and add it for the new VLAN.
13896 	 *
13897 	 * Broadcast filters are handled specially by
13898 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13899 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13900 	 * filter. The subtask will update the correct broadcast promiscuous
13901 	 * bits as VLANs become active or inactive.
13902 	 */
13903 	eth_broadcast_addr(broadcast);
13904 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13905 	i40e_add_mac_filter(vsi, broadcast);
13906 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13907 
13908 	eth_hw_addr_set(netdev, mac_addr);
13909 	ether_addr_copy(netdev->perm_addr, mac_addr);
13910 
13911 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13912 	netdev->neigh_priv_len = sizeof(u32) * 4;
13913 
13914 	netdev->priv_flags |= IFF_UNICAST_FLT;
13915 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13916 	/* Setup netdev TC information */
13917 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13918 
13919 	netdev->netdev_ops = &i40e_netdev_ops;
13920 	netdev->watchdog_timeo = 5 * HZ;
13921 	i40e_set_ethtool_ops(netdev);
13922 
13923 	/* MTU range: 68 - 9706 */
13924 	netdev->min_mtu = ETH_MIN_MTU;
13925 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13926 
13927 	return 0;
13928 }
13929 
13930 /**
13931  * i40e_vsi_delete - Delete a VSI from the switch
13932  * @vsi: the VSI being removed
13933  *
13934  * Returns 0 on success, negative value on failure
13935  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13936 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13937 {
13938 	/* remove default VSI is not allowed */
13939 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13940 		return;
13941 
13942 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13943 }
13944 
13945 /**
13946  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13947  * @vsi: the VSI being queried
13948  *
13949  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13950  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13951 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13952 {
13953 	struct i40e_veb *veb;
13954 	struct i40e_pf *pf = vsi->back;
13955 
13956 	/* Uplink is not a bridge so default to VEB */
13957 	if (vsi->veb_idx >= I40E_MAX_VEB)
13958 		return 1;
13959 
13960 	veb = pf->veb[vsi->veb_idx];
13961 	if (!veb) {
13962 		dev_info(&pf->pdev->dev,
13963 			 "There is no veb associated with the bridge\n");
13964 		return -ENOENT;
13965 	}
13966 
13967 	/* Uplink is a bridge in VEPA mode */
13968 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13969 		return 0;
13970 	} else {
13971 		/* Uplink is a bridge in VEB mode */
13972 		return 1;
13973 	}
13974 
13975 	/* VEPA is now default bridge, so return 0 */
13976 	return 0;
13977 }
13978 
13979 /**
13980  * i40e_add_vsi - Add a VSI to the switch
13981  * @vsi: the VSI being configured
13982  *
13983  * This initializes a VSI context depending on the VSI type to be added and
13984  * passes it down to the add_vsi aq command.
13985  **/
i40e_add_vsi(struct i40e_vsi * vsi)13986 static int i40e_add_vsi(struct i40e_vsi *vsi)
13987 {
13988 	int ret = -ENODEV;
13989 	struct i40e_pf *pf = vsi->back;
13990 	struct i40e_hw *hw = &pf->hw;
13991 	struct i40e_vsi_context ctxt;
13992 	struct i40e_mac_filter *f;
13993 	struct hlist_node *h;
13994 	int bkt;
13995 
13996 	u8 enabled_tc = 0x1; /* TC0 enabled */
13997 	int f_count = 0;
13998 
13999 	memset(&ctxt, 0, sizeof(ctxt));
14000 	switch (vsi->type) {
14001 	case I40E_VSI_MAIN:
14002 		/* The PF's main VSI is already setup as part of the
14003 		 * device initialization, so we'll not bother with
14004 		 * the add_vsi call, but we will retrieve the current
14005 		 * VSI context.
14006 		 */
14007 		ctxt.seid = pf->main_vsi_seid;
14008 		ctxt.pf_num = pf->hw.pf_id;
14009 		ctxt.vf_num = 0;
14010 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
14011 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14012 		if (ret) {
14013 			dev_info(&pf->pdev->dev,
14014 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
14015 				 ERR_PTR(ret),
14016 				 i40e_aq_str(&pf->hw,
14017 					     pf->hw.aq.asq_last_status));
14018 			return -ENOENT;
14019 		}
14020 		vsi->info = ctxt.info;
14021 		vsi->info.valid_sections = 0;
14022 
14023 		vsi->seid = ctxt.seid;
14024 		vsi->id = ctxt.vsi_number;
14025 
14026 		enabled_tc = i40e_pf_get_tc_map(pf);
14027 
14028 		/* Source pruning is enabled by default, so the flag is
14029 		 * negative logic - if it's set, we need to fiddle with
14030 		 * the VSI to disable source pruning.
14031 		 */
14032 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
14033 			memset(&ctxt, 0, sizeof(ctxt));
14034 			ctxt.seid = pf->main_vsi_seid;
14035 			ctxt.pf_num = pf->hw.pf_id;
14036 			ctxt.vf_num = 0;
14037 			ctxt.info.valid_sections |=
14038 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14039 			ctxt.info.switch_id =
14040 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
14041 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14042 			if (ret) {
14043 				dev_info(&pf->pdev->dev,
14044 					 "update vsi failed, err %d aq_err %s\n",
14045 					 ret,
14046 					 i40e_aq_str(&pf->hw,
14047 						     pf->hw.aq.asq_last_status));
14048 				ret = -ENOENT;
14049 				goto err;
14050 			}
14051 		}
14052 
14053 		/* MFP mode setup queue map and update VSI */
14054 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
14055 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
14056 			memset(&ctxt, 0, sizeof(ctxt));
14057 			ctxt.seid = pf->main_vsi_seid;
14058 			ctxt.pf_num = pf->hw.pf_id;
14059 			ctxt.vf_num = 0;
14060 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
14061 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14062 			if (ret) {
14063 				dev_info(&pf->pdev->dev,
14064 					 "update vsi failed, err %pe aq_err %s\n",
14065 					 ERR_PTR(ret),
14066 					 i40e_aq_str(&pf->hw,
14067 						    pf->hw.aq.asq_last_status));
14068 				ret = -ENOENT;
14069 				goto err;
14070 			}
14071 			/* update the local VSI info queue map */
14072 			i40e_vsi_update_queue_map(vsi, &ctxt);
14073 			vsi->info.valid_sections = 0;
14074 		} else {
14075 			/* Default/Main VSI is only enabled for TC0
14076 			 * reconfigure it to enable all TCs that are
14077 			 * available on the port in SFP mode.
14078 			 * For MFP case the iSCSI PF would use this
14079 			 * flow to enable LAN+iSCSI TC.
14080 			 */
14081 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
14082 			if (ret) {
14083 				/* Single TC condition is not fatal,
14084 				 * message and continue
14085 				 */
14086 				dev_info(&pf->pdev->dev,
14087 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14088 					 enabled_tc,
14089 					 ERR_PTR(ret),
14090 					 i40e_aq_str(&pf->hw,
14091 						    pf->hw.aq.asq_last_status));
14092 			}
14093 		}
14094 		break;
14095 
14096 	case I40E_VSI_FDIR:
14097 		ctxt.pf_num = hw->pf_id;
14098 		ctxt.vf_num = 0;
14099 		ctxt.uplink_seid = vsi->uplink_seid;
14100 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14101 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14102 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
14103 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14104 			ctxt.info.valid_sections |=
14105 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14106 			ctxt.info.switch_id =
14107 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14108 		}
14109 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14110 		break;
14111 
14112 	case I40E_VSI_VMDQ2:
14113 		ctxt.pf_num = hw->pf_id;
14114 		ctxt.vf_num = 0;
14115 		ctxt.uplink_seid = vsi->uplink_seid;
14116 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14117 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14118 
14119 		/* This VSI is connected to VEB so the switch_id
14120 		 * should be set to zero by default.
14121 		 */
14122 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14123 			ctxt.info.valid_sections |=
14124 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14125 			ctxt.info.switch_id =
14126 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14127 		}
14128 
14129 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14130 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14131 		break;
14132 
14133 	case I40E_VSI_SRIOV:
14134 		ctxt.pf_num = hw->pf_id;
14135 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14136 		ctxt.uplink_seid = vsi->uplink_seid;
14137 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14138 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14139 
14140 		/* This VSI is connected to VEB so the switch_id
14141 		 * should be set to zero by default.
14142 		 */
14143 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14144 			ctxt.info.valid_sections |=
14145 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14146 			ctxt.info.switch_id =
14147 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14148 		}
14149 
14150 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14151 			ctxt.info.valid_sections |=
14152 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14153 			ctxt.info.queueing_opt_flags |=
14154 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14155 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14156 		}
14157 
14158 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14159 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14160 		if (pf->vf[vsi->vf_id].spoofchk) {
14161 			ctxt.info.valid_sections |=
14162 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14163 			ctxt.info.sec_flags |=
14164 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14165 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14166 		}
14167 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14168 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14169 		break;
14170 
14171 	case I40E_VSI_IWARP:
14172 		/* send down message to iWARP */
14173 		break;
14174 
14175 	default:
14176 		return -ENODEV;
14177 	}
14178 
14179 	if (vsi->type != I40E_VSI_MAIN) {
14180 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14181 		if (ret) {
14182 			dev_info(&vsi->back->pdev->dev,
14183 				 "add vsi failed, err %pe aq_err %s\n",
14184 				 ERR_PTR(ret),
14185 				 i40e_aq_str(&pf->hw,
14186 					     pf->hw.aq.asq_last_status));
14187 			ret = -ENOENT;
14188 			goto err;
14189 		}
14190 		vsi->info = ctxt.info;
14191 		vsi->info.valid_sections = 0;
14192 		vsi->seid = ctxt.seid;
14193 		vsi->id = ctxt.vsi_number;
14194 	}
14195 
14196 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14197 	vsi->active_filters = 0;
14198 	/* If macvlan filters already exist, force them to get loaded */
14199 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14200 		f->state = I40E_FILTER_NEW;
14201 		f_count++;
14202 	}
14203 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14204 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14205 
14206 	if (f_count) {
14207 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14208 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14209 	}
14210 
14211 	/* Update VSI BW information */
14212 	ret = i40e_vsi_get_bw_info(vsi);
14213 	if (ret) {
14214 		dev_info(&pf->pdev->dev,
14215 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14216 			 ERR_PTR(ret),
14217 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14218 		/* VSI is already added so not tearing that up */
14219 		ret = 0;
14220 	}
14221 
14222 err:
14223 	return ret;
14224 }
14225 
14226 /**
14227  * i40e_vsi_release - Delete a VSI and free its resources
14228  * @vsi: the VSI being removed
14229  *
14230  * Returns 0 on success or < 0 on error
14231  **/
i40e_vsi_release(struct i40e_vsi * vsi)14232 int i40e_vsi_release(struct i40e_vsi *vsi)
14233 {
14234 	struct i40e_mac_filter *f;
14235 	struct hlist_node *h;
14236 	struct i40e_veb *veb = NULL;
14237 	struct i40e_pf *pf;
14238 	u16 uplink_seid;
14239 	int i, n, bkt;
14240 
14241 	pf = vsi->back;
14242 
14243 	/* release of a VEB-owner or last VSI is not allowed */
14244 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14245 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14246 			 vsi->seid, vsi->uplink_seid);
14247 		return -ENODEV;
14248 	}
14249 	if (vsi == pf->vsi[pf->lan_vsi] &&
14250 	    !test_bit(__I40E_DOWN, pf->state)) {
14251 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14252 		return -ENODEV;
14253 	}
14254 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14255 	uplink_seid = vsi->uplink_seid;
14256 	if (vsi->type != I40E_VSI_SRIOV) {
14257 		if (vsi->netdev_registered) {
14258 			vsi->netdev_registered = false;
14259 			if (vsi->netdev) {
14260 				/* results in a call to i40e_close() */
14261 				unregister_netdev(vsi->netdev);
14262 			}
14263 		} else {
14264 			i40e_vsi_close(vsi);
14265 		}
14266 		i40e_vsi_disable_irq(vsi);
14267 	}
14268 
14269 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14270 
14271 	/* clear the sync flag on all filters */
14272 	if (vsi->netdev) {
14273 		__dev_uc_unsync(vsi->netdev, NULL);
14274 		__dev_mc_unsync(vsi->netdev, NULL);
14275 	}
14276 
14277 	/* make sure any remaining filters are marked for deletion */
14278 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14279 		__i40e_del_filter(vsi, f);
14280 
14281 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14282 
14283 	i40e_sync_vsi_filters(vsi);
14284 
14285 	i40e_vsi_delete(vsi);
14286 	i40e_vsi_free_q_vectors(vsi);
14287 	if (vsi->netdev) {
14288 		free_netdev(vsi->netdev);
14289 		vsi->netdev = NULL;
14290 	}
14291 	i40e_vsi_clear_rings(vsi);
14292 	i40e_vsi_clear(vsi);
14293 
14294 	/* If this was the last thing on the VEB, except for the
14295 	 * controlling VSI, remove the VEB, which puts the controlling
14296 	 * VSI onto the next level down in the switch.
14297 	 *
14298 	 * Well, okay, there's one more exception here: don't remove
14299 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14300 	 * from up the network stack.
14301 	 */
14302 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14303 		if (pf->vsi[i] &&
14304 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14305 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14306 			n++;      /* count the VSIs */
14307 		}
14308 	}
14309 	for (i = 0; i < I40E_MAX_VEB; i++) {
14310 		if (!pf->veb[i])
14311 			continue;
14312 		if (pf->veb[i]->uplink_seid == uplink_seid)
14313 			n++;     /* count the VEBs */
14314 		if (pf->veb[i]->seid == uplink_seid)
14315 			veb = pf->veb[i];
14316 	}
14317 	if (n == 0 && veb && veb->uplink_seid != 0)
14318 		i40e_veb_release(veb);
14319 
14320 	return 0;
14321 }
14322 
14323 /**
14324  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14325  * @vsi: ptr to the VSI
14326  *
14327  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14328  * corresponding SW VSI structure and initializes num_queue_pairs for the
14329  * newly allocated VSI.
14330  *
14331  * Returns 0 on success or negative on failure
14332  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)14333 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14334 {
14335 	int ret = -ENOENT;
14336 	struct i40e_pf *pf = vsi->back;
14337 
14338 	if (vsi->q_vectors[0]) {
14339 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14340 			 vsi->seid);
14341 		return -EEXIST;
14342 	}
14343 
14344 	if (vsi->base_vector) {
14345 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14346 			 vsi->seid, vsi->base_vector);
14347 		return -EEXIST;
14348 	}
14349 
14350 	ret = i40e_vsi_alloc_q_vectors(vsi);
14351 	if (ret) {
14352 		dev_info(&pf->pdev->dev,
14353 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14354 			 vsi->num_q_vectors, vsi->seid, ret);
14355 		vsi->num_q_vectors = 0;
14356 		goto vector_setup_out;
14357 	}
14358 
14359 	/* In Legacy mode, we do not have to get any other vector since we
14360 	 * piggyback on the misc/ICR0 for queue interrupts.
14361 	*/
14362 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14363 		return ret;
14364 	if (vsi->num_q_vectors)
14365 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14366 						 vsi->num_q_vectors, vsi->idx);
14367 	if (vsi->base_vector < 0) {
14368 		dev_info(&pf->pdev->dev,
14369 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14370 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14371 		i40e_vsi_free_q_vectors(vsi);
14372 		ret = -ENOENT;
14373 		goto vector_setup_out;
14374 	}
14375 
14376 vector_setup_out:
14377 	return ret;
14378 }
14379 
14380 /**
14381  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14382  * @vsi: pointer to the vsi.
14383  *
14384  * This re-allocates a vsi's queue resources.
14385  *
14386  * Returns pointer to the successfully allocated and configured VSI sw struct
14387  * on success, otherwise returns NULL on failure.
14388  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)14389 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14390 {
14391 	u16 alloc_queue_pairs;
14392 	struct i40e_pf *pf;
14393 	u8 enabled_tc;
14394 	int ret;
14395 
14396 	if (!vsi)
14397 		return NULL;
14398 
14399 	pf = vsi->back;
14400 
14401 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14402 	i40e_vsi_clear_rings(vsi);
14403 
14404 	i40e_vsi_free_arrays(vsi, false);
14405 	i40e_set_num_rings_in_vsi(vsi);
14406 	ret = i40e_vsi_alloc_arrays(vsi, false);
14407 	if (ret)
14408 		goto err_vsi;
14409 
14410 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14411 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14412 
14413 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14414 	if (ret < 0) {
14415 		dev_info(&pf->pdev->dev,
14416 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14417 			 alloc_queue_pairs, vsi->seid, ret);
14418 		goto err_vsi;
14419 	}
14420 	vsi->base_queue = ret;
14421 
14422 	/* Update the FW view of the VSI. Force a reset of TC and queue
14423 	 * layout configurations.
14424 	 */
14425 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14426 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14427 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14428 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14429 	if (vsi->type == I40E_VSI_MAIN)
14430 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14431 
14432 	/* assign it some queues */
14433 	ret = i40e_alloc_rings(vsi);
14434 	if (ret)
14435 		goto err_rings;
14436 
14437 	/* map all of the rings to the q_vectors */
14438 	i40e_vsi_map_rings_to_vectors(vsi);
14439 	return vsi;
14440 
14441 err_rings:
14442 	i40e_vsi_free_q_vectors(vsi);
14443 	if (vsi->netdev_registered) {
14444 		vsi->netdev_registered = false;
14445 		unregister_netdev(vsi->netdev);
14446 		free_netdev(vsi->netdev);
14447 		vsi->netdev = NULL;
14448 	}
14449 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14450 err_vsi:
14451 	i40e_vsi_clear(vsi);
14452 	return NULL;
14453 }
14454 
14455 /**
14456  * i40e_vsi_setup - Set up a VSI by a given type
14457  * @pf: board private structure
14458  * @type: VSI type
14459  * @uplink_seid: the switch element to link to
14460  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14461  *
14462  * This allocates the sw VSI structure and its queue resources, then add a VSI
14463  * to the identified VEB.
14464  *
14465  * Returns pointer to the successfully allocated and configure VSI sw struct on
14466  * success, otherwise returns NULL on failure.
14467  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)14468 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14469 				u16 uplink_seid, u32 param1)
14470 {
14471 	struct i40e_vsi *vsi = NULL;
14472 	struct i40e_veb *veb = NULL;
14473 	u16 alloc_queue_pairs;
14474 	int ret, i;
14475 	int v_idx;
14476 
14477 	/* The requested uplink_seid must be either
14478 	 *     - the PF's port seid
14479 	 *              no VEB is needed because this is the PF
14480 	 *              or this is a Flow Director special case VSI
14481 	 *     - seid of an existing VEB
14482 	 *     - seid of a VSI that owns an existing VEB
14483 	 *     - seid of a VSI that doesn't own a VEB
14484 	 *              a new VEB is created and the VSI becomes the owner
14485 	 *     - seid of the PF VSI, which is what creates the first VEB
14486 	 *              this is a special case of the previous
14487 	 *
14488 	 * Find which uplink_seid we were given and create a new VEB if needed
14489 	 */
14490 	for (i = 0; i < I40E_MAX_VEB; i++) {
14491 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14492 			veb = pf->veb[i];
14493 			break;
14494 		}
14495 	}
14496 
14497 	if (!veb && uplink_seid != pf->mac_seid) {
14498 
14499 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14500 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14501 				vsi = pf->vsi[i];
14502 				break;
14503 			}
14504 		}
14505 		if (!vsi) {
14506 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14507 				 uplink_seid);
14508 			return NULL;
14509 		}
14510 
14511 		if (vsi->uplink_seid == pf->mac_seid)
14512 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14513 					     vsi->tc_config.enabled_tc);
14514 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14515 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14516 					     vsi->tc_config.enabled_tc);
14517 		if (veb) {
14518 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14519 				dev_info(&vsi->back->pdev->dev,
14520 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14521 				return NULL;
14522 			}
14523 			/* We come up by default in VEPA mode if SRIOV is not
14524 			 * already enabled, in which case we can't force VEPA
14525 			 * mode.
14526 			 */
14527 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14528 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14529 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14530 			}
14531 			i40e_config_bridge_mode(veb);
14532 		}
14533 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14534 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14535 				veb = pf->veb[i];
14536 		}
14537 		if (!veb) {
14538 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14539 			return NULL;
14540 		}
14541 
14542 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14543 		uplink_seid = veb->seid;
14544 	}
14545 
14546 	/* get vsi sw struct */
14547 	v_idx = i40e_vsi_mem_alloc(pf, type);
14548 	if (v_idx < 0)
14549 		goto err_alloc;
14550 	vsi = pf->vsi[v_idx];
14551 	if (!vsi)
14552 		goto err_alloc;
14553 	vsi->type = type;
14554 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14555 
14556 	if (type == I40E_VSI_MAIN)
14557 		pf->lan_vsi = v_idx;
14558 	else if (type == I40E_VSI_SRIOV)
14559 		vsi->vf_id = param1;
14560 	/* assign it some queues */
14561 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14562 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14563 
14564 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14565 	if (ret < 0) {
14566 		dev_info(&pf->pdev->dev,
14567 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14568 			 alloc_queue_pairs, vsi->seid, ret);
14569 		goto err_vsi;
14570 	}
14571 	vsi->base_queue = ret;
14572 
14573 	/* get a VSI from the hardware */
14574 	vsi->uplink_seid = uplink_seid;
14575 	ret = i40e_add_vsi(vsi);
14576 	if (ret)
14577 		goto err_vsi;
14578 
14579 	switch (vsi->type) {
14580 	/* setup the netdev if needed */
14581 	case I40E_VSI_MAIN:
14582 	case I40E_VSI_VMDQ2:
14583 		ret = i40e_config_netdev(vsi);
14584 		if (ret)
14585 			goto err_netdev;
14586 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14587 		if (ret)
14588 			goto err_netdev;
14589 		ret = register_netdev(vsi->netdev);
14590 		if (ret)
14591 			goto err_netdev;
14592 		vsi->netdev_registered = true;
14593 		netif_carrier_off(vsi->netdev);
14594 #ifdef CONFIG_I40E_DCB
14595 		/* Setup DCB netlink interface */
14596 		i40e_dcbnl_setup(vsi);
14597 #endif /* CONFIG_I40E_DCB */
14598 		fallthrough;
14599 	case I40E_VSI_FDIR:
14600 		/* set up vectors and rings if needed */
14601 		ret = i40e_vsi_setup_vectors(vsi);
14602 		if (ret)
14603 			goto err_msix;
14604 
14605 		ret = i40e_alloc_rings(vsi);
14606 		if (ret)
14607 			goto err_rings;
14608 
14609 		/* map all of the rings to the q_vectors */
14610 		i40e_vsi_map_rings_to_vectors(vsi);
14611 
14612 		i40e_vsi_reset_stats(vsi);
14613 		break;
14614 	default:
14615 		/* no netdev or rings for the other VSI types */
14616 		break;
14617 	}
14618 
14619 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14620 	    (vsi->type == I40E_VSI_VMDQ2)) {
14621 		ret = i40e_vsi_config_rss(vsi);
14622 	}
14623 	return vsi;
14624 
14625 err_rings:
14626 	i40e_vsi_free_q_vectors(vsi);
14627 err_msix:
14628 	if (vsi->netdev_registered) {
14629 		vsi->netdev_registered = false;
14630 		unregister_netdev(vsi->netdev);
14631 		free_netdev(vsi->netdev);
14632 		vsi->netdev = NULL;
14633 	}
14634 err_netdev:
14635 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14636 err_vsi:
14637 	i40e_vsi_clear(vsi);
14638 err_alloc:
14639 	return NULL;
14640 }
14641 
14642 /**
14643  * i40e_veb_get_bw_info - Query VEB BW information
14644  * @veb: the veb to query
14645  *
14646  * Query the Tx scheduler BW configuration data for given VEB
14647  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)14648 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14649 {
14650 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14651 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14652 	struct i40e_pf *pf = veb->pf;
14653 	struct i40e_hw *hw = &pf->hw;
14654 	u32 tc_bw_max;
14655 	int ret = 0;
14656 	int i;
14657 
14658 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14659 						  &bw_data, NULL);
14660 	if (ret) {
14661 		dev_info(&pf->pdev->dev,
14662 			 "query veb bw config failed, err %pe aq_err %s\n",
14663 			 ERR_PTR(ret),
14664 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14665 		goto out;
14666 	}
14667 
14668 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14669 						   &ets_data, NULL);
14670 	if (ret) {
14671 		dev_info(&pf->pdev->dev,
14672 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14673 			 ERR_PTR(ret),
14674 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14675 		goto out;
14676 	}
14677 
14678 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14679 	veb->bw_max_quanta = ets_data.tc_bw_max;
14680 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14681 	veb->enabled_tc = ets_data.tc_valid_bits;
14682 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14683 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14684 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14685 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14686 		veb->bw_tc_limit_credits[i] =
14687 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14688 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14689 	}
14690 
14691 out:
14692 	return ret;
14693 }
14694 
14695 /**
14696  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14697  * @pf: board private structure
14698  *
14699  * On error: returns error code (negative)
14700  * On success: returns vsi index in PF (positive)
14701  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)14702 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14703 {
14704 	int ret = -ENOENT;
14705 	struct i40e_veb *veb;
14706 	int i;
14707 
14708 	/* Need to protect the allocation of switch elements at the PF level */
14709 	mutex_lock(&pf->switch_mutex);
14710 
14711 	/* VEB list may be fragmented if VEB creation/destruction has
14712 	 * been happening.  We can afford to do a quick scan to look
14713 	 * for any free slots in the list.
14714 	 *
14715 	 * find next empty veb slot, looping back around if necessary
14716 	 */
14717 	i = 0;
14718 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14719 		i++;
14720 	if (i >= I40E_MAX_VEB) {
14721 		ret = -ENOMEM;
14722 		goto err_alloc_veb;  /* out of VEB slots! */
14723 	}
14724 
14725 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14726 	if (!veb) {
14727 		ret = -ENOMEM;
14728 		goto err_alloc_veb;
14729 	}
14730 	veb->pf = pf;
14731 	veb->idx = i;
14732 	veb->enabled_tc = 1;
14733 
14734 	pf->veb[i] = veb;
14735 	ret = i;
14736 err_alloc_veb:
14737 	mutex_unlock(&pf->switch_mutex);
14738 	return ret;
14739 }
14740 
14741 /**
14742  * i40e_switch_branch_release - Delete a branch of the switch tree
14743  * @branch: where to start deleting
14744  *
14745  * This uses recursion to find the tips of the branch to be
14746  * removed, deleting until we get back to and can delete this VEB.
14747  **/
i40e_switch_branch_release(struct i40e_veb * branch)14748 static void i40e_switch_branch_release(struct i40e_veb *branch)
14749 {
14750 	struct i40e_pf *pf = branch->pf;
14751 	u16 branch_seid = branch->seid;
14752 	u16 veb_idx = branch->idx;
14753 	int i;
14754 
14755 	/* release any VEBs on this VEB - RECURSION */
14756 	for (i = 0; i < I40E_MAX_VEB; i++) {
14757 		if (!pf->veb[i])
14758 			continue;
14759 		if (pf->veb[i]->uplink_seid == branch->seid)
14760 			i40e_switch_branch_release(pf->veb[i]);
14761 	}
14762 
14763 	/* Release the VSIs on this VEB, but not the owner VSI.
14764 	 *
14765 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14766 	 *       the VEB itself, so don't use (*branch) after this loop.
14767 	 */
14768 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14769 		if (!pf->vsi[i])
14770 			continue;
14771 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14772 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14773 			i40e_vsi_release(pf->vsi[i]);
14774 		}
14775 	}
14776 
14777 	/* There's one corner case where the VEB might not have been
14778 	 * removed, so double check it here and remove it if needed.
14779 	 * This case happens if the veb was created from the debugfs
14780 	 * commands and no VSIs were added to it.
14781 	 */
14782 	if (pf->veb[veb_idx])
14783 		i40e_veb_release(pf->veb[veb_idx]);
14784 }
14785 
14786 /**
14787  * i40e_veb_clear - remove veb struct
14788  * @veb: the veb to remove
14789  **/
i40e_veb_clear(struct i40e_veb * veb)14790 static void i40e_veb_clear(struct i40e_veb *veb)
14791 {
14792 	if (!veb)
14793 		return;
14794 
14795 	if (veb->pf) {
14796 		struct i40e_pf *pf = veb->pf;
14797 
14798 		mutex_lock(&pf->switch_mutex);
14799 		if (pf->veb[veb->idx] == veb)
14800 			pf->veb[veb->idx] = NULL;
14801 		mutex_unlock(&pf->switch_mutex);
14802 	}
14803 
14804 	kfree(veb);
14805 }
14806 
14807 /**
14808  * i40e_veb_release - Delete a VEB and free its resources
14809  * @veb: the VEB being removed
14810  **/
i40e_veb_release(struct i40e_veb * veb)14811 void i40e_veb_release(struct i40e_veb *veb)
14812 {
14813 	struct i40e_vsi *vsi = NULL;
14814 	struct i40e_pf *pf;
14815 	int i, n = 0;
14816 
14817 	pf = veb->pf;
14818 
14819 	/* find the remaining VSI and check for extras */
14820 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14821 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14822 			n++;
14823 			vsi = pf->vsi[i];
14824 		}
14825 	}
14826 	if (n != 1) {
14827 		dev_info(&pf->pdev->dev,
14828 			 "can't remove VEB %d with %d VSIs left\n",
14829 			 veb->seid, n);
14830 		return;
14831 	}
14832 
14833 	/* move the remaining VSI to uplink veb */
14834 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14835 	if (veb->uplink_seid) {
14836 		vsi->uplink_seid = veb->uplink_seid;
14837 		if (veb->uplink_seid == pf->mac_seid)
14838 			vsi->veb_idx = I40E_NO_VEB;
14839 		else
14840 			vsi->veb_idx = veb->veb_idx;
14841 	} else {
14842 		/* floating VEB */
14843 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14844 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14845 	}
14846 
14847 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14848 	i40e_veb_clear(veb);
14849 }
14850 
14851 /**
14852  * i40e_add_veb - create the VEB in the switch
14853  * @veb: the VEB to be instantiated
14854  * @vsi: the controlling VSI
14855  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)14856 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14857 {
14858 	struct i40e_pf *pf = veb->pf;
14859 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14860 	int ret;
14861 
14862 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14863 			      veb->enabled_tc, false,
14864 			      &veb->seid, enable_stats, NULL);
14865 
14866 	/* get a VEB from the hardware */
14867 	if (ret) {
14868 		dev_info(&pf->pdev->dev,
14869 			 "couldn't add VEB, err %pe aq_err %s\n",
14870 			 ERR_PTR(ret),
14871 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14872 		return -EPERM;
14873 	}
14874 
14875 	/* get statistics counter */
14876 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14877 					 &veb->stats_idx, NULL, NULL, NULL);
14878 	if (ret) {
14879 		dev_info(&pf->pdev->dev,
14880 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14881 			 ERR_PTR(ret),
14882 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14883 		return -EPERM;
14884 	}
14885 	ret = i40e_veb_get_bw_info(veb);
14886 	if (ret) {
14887 		dev_info(&pf->pdev->dev,
14888 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14889 			 ERR_PTR(ret),
14890 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14891 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14892 		return -ENOENT;
14893 	}
14894 
14895 	vsi->uplink_seid = veb->seid;
14896 	vsi->veb_idx = veb->idx;
14897 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14898 
14899 	return 0;
14900 }
14901 
14902 /**
14903  * i40e_veb_setup - Set up a VEB
14904  * @pf: board private structure
14905  * @flags: VEB setup flags
14906  * @uplink_seid: the switch element to link to
14907  * @vsi_seid: the initial VSI seid
14908  * @enabled_tc: Enabled TC bit-map
14909  *
14910  * This allocates the sw VEB structure and links it into the switch
14911  * It is possible and legal for this to be a duplicate of an already
14912  * existing VEB.  It is also possible for both uplink and vsi seids
14913  * to be zero, in order to create a floating VEB.
14914  *
14915  * Returns pointer to the successfully allocated VEB sw struct on
14916  * success, otherwise returns NULL on failure.
14917  **/
i40e_veb_setup(struct i40e_pf * pf,u16 flags,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14918 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14919 				u16 uplink_seid, u16 vsi_seid,
14920 				u8 enabled_tc)
14921 {
14922 	struct i40e_veb *veb, *uplink_veb = NULL;
14923 	int vsi_idx, veb_idx;
14924 	int ret;
14925 
14926 	/* if one seid is 0, the other must be 0 to create a floating relay */
14927 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14928 	    (uplink_seid + vsi_seid != 0)) {
14929 		dev_info(&pf->pdev->dev,
14930 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14931 			 uplink_seid, vsi_seid);
14932 		return NULL;
14933 	}
14934 
14935 	/* make sure there is such a vsi and uplink */
14936 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14937 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14938 			break;
14939 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14940 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14941 			 vsi_seid);
14942 		return NULL;
14943 	}
14944 
14945 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14946 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14947 			if (pf->veb[veb_idx] &&
14948 			    pf->veb[veb_idx]->seid == uplink_seid) {
14949 				uplink_veb = pf->veb[veb_idx];
14950 				break;
14951 			}
14952 		}
14953 		if (!uplink_veb) {
14954 			dev_info(&pf->pdev->dev,
14955 				 "uplink seid %d not found\n", uplink_seid);
14956 			return NULL;
14957 		}
14958 	}
14959 
14960 	/* get veb sw struct */
14961 	veb_idx = i40e_veb_mem_alloc(pf);
14962 	if (veb_idx < 0)
14963 		goto err_alloc;
14964 	veb = pf->veb[veb_idx];
14965 	veb->flags = flags;
14966 	veb->uplink_seid = uplink_seid;
14967 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14968 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14969 
14970 	/* create the VEB in the switch */
14971 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14972 	if (ret)
14973 		goto err_veb;
14974 	if (vsi_idx == pf->lan_vsi)
14975 		pf->lan_veb = veb->idx;
14976 
14977 	return veb;
14978 
14979 err_veb:
14980 	i40e_veb_clear(veb);
14981 err_alloc:
14982 	return NULL;
14983 }
14984 
14985 /**
14986  * i40e_setup_pf_switch_element - set PF vars based on switch type
14987  * @pf: board private structure
14988  * @ele: element we are building info from
14989  * @num_reported: total number of elements
14990  * @printconfig: should we print the contents
14991  *
14992  * helper function to assist in extracting a few useful SEID values.
14993  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14994 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14995 				struct i40e_aqc_switch_config_element_resp *ele,
14996 				u16 num_reported, bool printconfig)
14997 {
14998 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14999 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
15000 	u8 element_type = ele->element_type;
15001 	u16 seid = le16_to_cpu(ele->seid);
15002 
15003 	if (printconfig)
15004 		dev_info(&pf->pdev->dev,
15005 			 "type=%d seid=%d uplink=%d downlink=%d\n",
15006 			 element_type, seid, uplink_seid, downlink_seid);
15007 
15008 	switch (element_type) {
15009 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
15010 		pf->mac_seid = seid;
15011 		break;
15012 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
15013 		/* Main VEB? */
15014 		if (uplink_seid != pf->mac_seid)
15015 			break;
15016 		if (pf->lan_veb >= I40E_MAX_VEB) {
15017 			int v;
15018 
15019 			/* find existing or else empty VEB */
15020 			for (v = 0; v < I40E_MAX_VEB; v++) {
15021 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
15022 					pf->lan_veb = v;
15023 					break;
15024 				}
15025 			}
15026 			if (pf->lan_veb >= I40E_MAX_VEB) {
15027 				v = i40e_veb_mem_alloc(pf);
15028 				if (v < 0)
15029 					break;
15030 				pf->lan_veb = v;
15031 			}
15032 		}
15033 		if (pf->lan_veb >= I40E_MAX_VEB)
15034 			break;
15035 
15036 		pf->veb[pf->lan_veb]->seid = seid;
15037 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
15038 		pf->veb[pf->lan_veb]->pf = pf;
15039 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
15040 		break;
15041 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
15042 		if (num_reported != 1)
15043 			break;
15044 		/* This is immediately after a reset so we can assume this is
15045 		 * the PF's VSI
15046 		 */
15047 		pf->mac_seid = uplink_seid;
15048 		pf->pf_seid = downlink_seid;
15049 		pf->main_vsi_seid = seid;
15050 		if (printconfig)
15051 			dev_info(&pf->pdev->dev,
15052 				 "pf_seid=%d main_vsi_seid=%d\n",
15053 				 pf->pf_seid, pf->main_vsi_seid);
15054 		break;
15055 	case I40E_SWITCH_ELEMENT_TYPE_PF:
15056 	case I40E_SWITCH_ELEMENT_TYPE_VF:
15057 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
15058 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
15059 	case I40E_SWITCH_ELEMENT_TYPE_PE:
15060 	case I40E_SWITCH_ELEMENT_TYPE_PA:
15061 		/* ignore these for now */
15062 		break;
15063 	default:
15064 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
15065 			 element_type, seid);
15066 		break;
15067 	}
15068 }
15069 
15070 /**
15071  * i40e_fetch_switch_configuration - Get switch config from firmware
15072  * @pf: board private structure
15073  * @printconfig: should we print the contents
15074  *
15075  * Get the current switch configuration from the device and
15076  * extract a few useful SEID values.
15077  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)15078 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
15079 {
15080 	struct i40e_aqc_get_switch_config_resp *sw_config;
15081 	u16 next_seid = 0;
15082 	int ret = 0;
15083 	u8 *aq_buf;
15084 	int i;
15085 
15086 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15087 	if (!aq_buf)
15088 		return -ENOMEM;
15089 
15090 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15091 	do {
15092 		u16 num_reported, num_total;
15093 
15094 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15095 						I40E_AQ_LARGE_BUF,
15096 						&next_seid, NULL);
15097 		if (ret) {
15098 			dev_info(&pf->pdev->dev,
15099 				 "get switch config failed err %d aq_err %s\n",
15100 				 ret,
15101 				 i40e_aq_str(&pf->hw,
15102 					     pf->hw.aq.asq_last_status));
15103 			kfree(aq_buf);
15104 			return -ENOENT;
15105 		}
15106 
15107 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15108 		num_total = le16_to_cpu(sw_config->header.num_total);
15109 
15110 		if (printconfig)
15111 			dev_info(&pf->pdev->dev,
15112 				 "header: %d reported %d total\n",
15113 				 num_reported, num_total);
15114 
15115 		for (i = 0; i < num_reported; i++) {
15116 			struct i40e_aqc_switch_config_element_resp *ele =
15117 				&sw_config->element[i];
15118 
15119 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15120 						     printconfig);
15121 		}
15122 	} while (next_seid != 0);
15123 
15124 	kfree(aq_buf);
15125 	return ret;
15126 }
15127 
15128 /**
15129  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15130  * @pf: board private structure
15131  * @reinit: if the Main VSI needs to re-initialized.
15132  * @lock_acquired: indicates whether or not the lock has been acquired
15133  *
15134  * Returns 0 on success, negative value on failure
15135  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)15136 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15137 {
15138 	u16 flags = 0;
15139 	int ret;
15140 
15141 	/* find out what's out there already */
15142 	ret = i40e_fetch_switch_configuration(pf, false);
15143 	if (ret) {
15144 		dev_info(&pf->pdev->dev,
15145 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15146 			 ERR_PTR(ret),
15147 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15148 		return ret;
15149 	}
15150 	i40e_pf_reset_stats(pf);
15151 
15152 	/* set the switch config bit for the whole device to
15153 	 * support limited promisc or true promisc
15154 	 * when user requests promisc. The default is limited
15155 	 * promisc.
15156 	*/
15157 
15158 	if ((pf->hw.pf_id == 0) &&
15159 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15160 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15161 		pf->last_sw_conf_flags = flags;
15162 	}
15163 
15164 	if (pf->hw.pf_id == 0) {
15165 		u16 valid_flags;
15166 
15167 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15168 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15169 						NULL);
15170 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15171 			dev_info(&pf->pdev->dev,
15172 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15173 				 ERR_PTR(ret),
15174 				 i40e_aq_str(&pf->hw,
15175 					     pf->hw.aq.asq_last_status));
15176 			/* not a fatal problem, just keep going */
15177 		}
15178 		pf->last_sw_conf_valid_flags = valid_flags;
15179 	}
15180 
15181 	/* first time setup */
15182 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15183 		struct i40e_vsi *vsi = NULL;
15184 		u16 uplink_seid;
15185 
15186 		/* Set up the PF VSI associated with the PF's main VSI
15187 		 * that is already in the HW switch
15188 		 */
15189 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15190 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15191 		else
15192 			uplink_seid = pf->mac_seid;
15193 		if (pf->lan_vsi == I40E_NO_VSI)
15194 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15195 		else if (reinit)
15196 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15197 		if (!vsi) {
15198 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15199 			i40e_cloud_filter_exit(pf);
15200 			i40e_fdir_teardown(pf);
15201 			return -EAGAIN;
15202 		}
15203 	} else {
15204 		/* force a reset of TC and queue layout configurations */
15205 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15206 
15207 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15208 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15209 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15210 	}
15211 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15212 
15213 	i40e_fdir_sb_setup(pf);
15214 
15215 	/* Setup static PF queue filter control settings */
15216 	ret = i40e_setup_pf_filter_control(pf);
15217 	if (ret) {
15218 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15219 			 ret);
15220 		/* Failure here should not stop continuing other steps */
15221 	}
15222 
15223 	/* enable RSS in the HW, even for only one queue, as the stack can use
15224 	 * the hash
15225 	 */
15226 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15227 		i40e_pf_config_rss(pf);
15228 
15229 	/* fill in link information and enable LSE reporting */
15230 	i40e_link_event(pf);
15231 
15232 	/* Initialize user-specific link properties */
15233 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15234 				  I40E_AQ_AN_COMPLETED) ? true : false);
15235 
15236 	i40e_ptp_init(pf);
15237 
15238 	if (!lock_acquired)
15239 		rtnl_lock();
15240 
15241 	/* repopulate tunnel port filters */
15242 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15243 
15244 	if (!lock_acquired)
15245 		rtnl_unlock();
15246 
15247 	return ret;
15248 }
15249 
15250 /**
15251  * i40e_determine_queue_usage - Work out queue distribution
15252  * @pf: board private structure
15253  **/
i40e_determine_queue_usage(struct i40e_pf * pf)15254 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15255 {
15256 	int queues_left;
15257 	int q_max;
15258 
15259 	pf->num_lan_qps = 0;
15260 
15261 	/* Find the max queues to be put into basic use.  We'll always be
15262 	 * using TC0, whether or not DCB is running, and TC0 will get the
15263 	 * big RSS set.
15264 	 */
15265 	queues_left = pf->hw.func_caps.num_tx_qp;
15266 
15267 	if ((queues_left == 1) ||
15268 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15269 		/* one qp for PF, no queues for anything else */
15270 		queues_left = 0;
15271 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15272 
15273 		/* make sure all the fancies are disabled */
15274 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15275 			       I40E_FLAG_IWARP_ENABLED	|
15276 			       I40E_FLAG_FD_SB_ENABLED	|
15277 			       I40E_FLAG_FD_ATR_ENABLED	|
15278 			       I40E_FLAG_DCB_CAPABLE	|
15279 			       I40E_FLAG_DCB_ENABLED	|
15280 			       I40E_FLAG_SRIOV_ENABLED	|
15281 			       I40E_FLAG_VMDQ_ENABLED);
15282 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15283 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15284 				  I40E_FLAG_FD_SB_ENABLED |
15285 				  I40E_FLAG_FD_ATR_ENABLED |
15286 				  I40E_FLAG_DCB_CAPABLE))) {
15287 		/* one qp for PF */
15288 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15289 		queues_left -= pf->num_lan_qps;
15290 
15291 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15292 			       I40E_FLAG_IWARP_ENABLED	|
15293 			       I40E_FLAG_FD_SB_ENABLED	|
15294 			       I40E_FLAG_FD_ATR_ENABLED	|
15295 			       I40E_FLAG_DCB_ENABLED	|
15296 			       I40E_FLAG_VMDQ_ENABLED);
15297 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15298 	} else {
15299 		/* Not enough queues for all TCs */
15300 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15301 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15302 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15303 					I40E_FLAG_DCB_ENABLED);
15304 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15305 		}
15306 
15307 		/* limit lan qps to the smaller of qps, cpus or msix */
15308 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15309 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15310 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15311 		pf->num_lan_qps = q_max;
15312 
15313 		queues_left -= pf->num_lan_qps;
15314 	}
15315 
15316 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15317 		if (queues_left > 1) {
15318 			queues_left -= 1; /* save 1 queue for FD */
15319 		} else {
15320 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15321 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15322 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15323 		}
15324 	}
15325 
15326 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15327 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15328 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15329 					(queues_left / pf->num_vf_qps));
15330 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15331 	}
15332 
15333 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15334 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15335 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15336 					  (queues_left / pf->num_vmdq_qps));
15337 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15338 	}
15339 
15340 	pf->queues_left = queues_left;
15341 	dev_dbg(&pf->pdev->dev,
15342 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15343 		pf->hw.func_caps.num_tx_qp,
15344 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15345 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15346 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15347 		queues_left);
15348 }
15349 
15350 /**
15351  * i40e_setup_pf_filter_control - Setup PF static filter control
15352  * @pf: PF to be setup
15353  *
15354  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15355  * settings. If PE/FCoE are enabled then it will also set the per PF
15356  * based filter sizes required for them. It also enables Flow director,
15357  * ethertype and macvlan type filter settings for the pf.
15358  *
15359  * Returns 0 on success, negative on failure
15360  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)15361 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15362 {
15363 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15364 
15365 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15366 
15367 	/* Flow Director is enabled */
15368 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15369 		settings->enable_fdir = true;
15370 
15371 	/* Ethtype and MACVLAN filters enabled for PF */
15372 	settings->enable_ethtype = true;
15373 	settings->enable_macvlan = true;
15374 
15375 	if (i40e_set_filter_control(&pf->hw, settings))
15376 		return -ENOENT;
15377 
15378 	return 0;
15379 }
15380 
15381 #define INFO_STRING_LEN 255
15382 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)15383 static void i40e_print_features(struct i40e_pf *pf)
15384 {
15385 	struct i40e_hw *hw = &pf->hw;
15386 	char *buf;
15387 	int i;
15388 
15389 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15390 	if (!buf)
15391 		return;
15392 
15393 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15394 #ifdef CONFIG_PCI_IOV
15395 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15396 #endif
15397 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15398 		      pf->hw.func_caps.num_vsis,
15399 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15400 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15401 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15402 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15403 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15404 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15405 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15406 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15407 	}
15408 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15409 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15410 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15411 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15412 	if (pf->flags & I40E_FLAG_PTP)
15413 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15414 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15415 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15416 	else
15417 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15418 
15419 	dev_info(&pf->pdev->dev, "%s\n", buf);
15420 	kfree(buf);
15421 	WARN_ON(i > INFO_STRING_LEN);
15422 }
15423 
15424 /**
15425  * i40e_get_platform_mac_addr - get platform-specific MAC address
15426  * @pdev: PCI device information struct
15427  * @pf: board private structure
15428  *
15429  * Look up the MAC address for the device. First we'll try
15430  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15431  * specific fallback. Otherwise, we'll default to the stored value in
15432  * firmware.
15433  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)15434 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15435 {
15436 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15437 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15438 }
15439 
15440 /**
15441  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15442  * @fec_cfg: FEC option to set in flags
15443  * @flags: ptr to flags in which we set FEC option
15444  **/
i40e_set_fec_in_flags(u8 fec_cfg,u32 * flags)15445 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15446 {
15447 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15448 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15449 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15450 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15451 		*flags |= I40E_FLAG_RS_FEC;
15452 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15453 	}
15454 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15455 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15456 		*flags |= I40E_FLAG_BASE_R_FEC;
15457 		*flags &= ~I40E_FLAG_RS_FEC;
15458 	}
15459 	if (fec_cfg == 0)
15460 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15461 }
15462 
15463 /**
15464  * i40e_check_recovery_mode - check if we are running transition firmware
15465  * @pf: board private structure
15466  *
15467  * Check registers indicating the firmware runs in recovery mode. Sets the
15468  * appropriate driver state.
15469  *
15470  * Returns true if the recovery mode was detected, false otherwise
15471  **/
i40e_check_recovery_mode(struct i40e_pf * pf)15472 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15473 {
15474 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15475 
15476 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15477 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15478 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15479 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15480 
15481 		return true;
15482 	}
15483 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15484 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15485 
15486 	return false;
15487 }
15488 
15489 /**
15490  * i40e_pf_loop_reset - perform reset in a loop.
15491  * @pf: board private structure
15492  *
15493  * This function is useful when a NIC is about to enter recovery mode.
15494  * When a NIC's internal data structures are corrupted the NIC's
15495  * firmware is going to enter recovery mode.
15496  * Right after a POR it takes about 7 minutes for firmware to enter
15497  * recovery mode. Until that time a NIC is in some kind of intermediate
15498  * state. After that time period the NIC almost surely enters
15499  * recovery mode. The only way for a driver to detect intermediate
15500  * state is to issue a series of pf-resets and check a return value.
15501  * If a PF reset returns success then the firmware could be in recovery
15502  * mode so the caller of this code needs to check for recovery mode
15503  * if this function returns success. There is a little chance that
15504  * firmware will hang in intermediate state forever.
15505  * Since waiting 7 minutes is quite a lot of time this function waits
15506  * 10 seconds and then gives up by returning an error.
15507  *
15508  * Return 0 on success, negative on failure.
15509  **/
i40e_pf_loop_reset(struct i40e_pf * pf)15510 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15511 {
15512 	/* wait max 10 seconds for PF reset to succeed */
15513 	const unsigned long time_end = jiffies + 10 * HZ;
15514 	struct i40e_hw *hw = &pf->hw;
15515 	int ret;
15516 
15517 	ret = i40e_pf_reset(hw);
15518 	while (ret != 0 && time_before(jiffies, time_end)) {
15519 		usleep_range(10000, 20000);
15520 		ret = i40e_pf_reset(hw);
15521 	}
15522 
15523 	if (ret == 0)
15524 		pf->pfr_count++;
15525 	else
15526 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15527 
15528 	return ret;
15529 }
15530 
15531 /**
15532  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15533  * @pf: board private structure
15534  *
15535  * Check FW registers to determine if FW issued unexpected EMP Reset.
15536  * Every time when unexpected EMP Reset occurs the FW increments
15537  * a counter of unexpected EMP Resets. When the counter reaches 10
15538  * the FW should enter the Recovery mode
15539  *
15540  * Returns true if FW issued unexpected EMP Reset
15541  **/
i40e_check_fw_empr(struct i40e_pf * pf)15542 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15543 {
15544 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15545 			   I40E_GL_FWSTS_FWS1B_MASK;
15546 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15547 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15548 }
15549 
15550 /**
15551  * i40e_handle_resets - handle EMP resets and PF resets
15552  * @pf: board private structure
15553  *
15554  * Handle both EMP resets and PF resets and conclude whether there are
15555  * any issues regarding these resets. If there are any issues then
15556  * generate log entry.
15557  *
15558  * Return 0 if NIC is healthy or negative value when there are issues
15559  * with resets
15560  **/
i40e_handle_resets(struct i40e_pf * pf)15561 static int i40e_handle_resets(struct i40e_pf *pf)
15562 {
15563 	const int pfr = i40e_pf_loop_reset(pf);
15564 	const bool is_empr = i40e_check_fw_empr(pf);
15565 
15566 	if (is_empr || pfr != 0)
15567 		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");
15568 
15569 	return is_empr ? -EIO : pfr;
15570 }
15571 
15572 /**
15573  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15574  * @pf: board private structure
15575  * @hw: ptr to the hardware info
15576  *
15577  * This function does a minimal setup of all subsystems needed for running
15578  * recovery mode.
15579  *
15580  * Returns 0 on success, negative on failure
15581  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)15582 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15583 {
15584 	struct i40e_vsi *vsi;
15585 	int err;
15586 	int v_idx;
15587 
15588 	pci_set_drvdata(pf->pdev, pf);
15589 	pci_save_state(pf->pdev);
15590 
15591 	/* set up periodic task facility */
15592 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15593 	pf->service_timer_period = HZ;
15594 
15595 	INIT_WORK(&pf->service_task, i40e_service_task);
15596 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15597 
15598 	err = i40e_init_interrupt_scheme(pf);
15599 	if (err)
15600 		goto err_switch_setup;
15601 
15602 	/* The number of VSIs reported by the FW is the minimum guaranteed
15603 	 * to us; HW supports far more and we share the remaining pool with
15604 	 * the other PFs. We allocate space for more than the guarantee with
15605 	 * the understanding that we might not get them all later.
15606 	 */
15607 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15608 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15609 	else
15610 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15611 
15612 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15613 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15614 			  GFP_KERNEL);
15615 	if (!pf->vsi) {
15616 		err = -ENOMEM;
15617 		goto err_switch_setup;
15618 	}
15619 
15620 	/* We allocate one VSI which is needed as absolute minimum
15621 	 * in order to register the netdev
15622 	 */
15623 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15624 	if (v_idx < 0) {
15625 		err = v_idx;
15626 		goto err_switch_setup;
15627 	}
15628 	pf->lan_vsi = v_idx;
15629 	vsi = pf->vsi[v_idx];
15630 	if (!vsi) {
15631 		err = -EFAULT;
15632 		goto err_switch_setup;
15633 	}
15634 	vsi->alloc_queue_pairs = 1;
15635 	err = i40e_config_netdev(vsi);
15636 	if (err)
15637 		goto err_switch_setup;
15638 	err = register_netdev(vsi->netdev);
15639 	if (err)
15640 		goto err_switch_setup;
15641 	vsi->netdev_registered = true;
15642 	i40e_dbg_pf_init(pf);
15643 
15644 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15645 	if (err)
15646 		goto err_switch_setup;
15647 
15648 	/* tell the firmware that we're starting */
15649 	i40e_send_version(pf);
15650 
15651 	/* since everything's happy, start the service_task timer */
15652 	mod_timer(&pf->service_timer,
15653 		  round_jiffies(jiffies + pf->service_timer_period));
15654 
15655 	return 0;
15656 
15657 err_switch_setup:
15658 	i40e_reset_interrupt_capability(pf);
15659 	timer_shutdown_sync(&pf->service_timer);
15660 	i40e_shutdown_adminq(hw);
15661 	iounmap(hw->hw_addr);
15662 	pci_release_mem_regions(pf->pdev);
15663 	pci_disable_device(pf->pdev);
15664 	kfree(pf);
15665 
15666 	return err;
15667 }
15668 
15669 /**
15670  * i40e_set_subsystem_device_id - set subsystem device id
15671  * @hw: pointer to the hardware info
15672  *
15673  * Set PCI subsystem device id either from a pci_dev structure or
15674  * a specific FW register.
15675  **/
i40e_set_subsystem_device_id(struct i40e_hw * hw)15676 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15677 {
15678 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15679 
15680 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15681 		pf->pdev->subsystem_device :
15682 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15683 }
15684 
15685 /**
15686  * i40e_probe - Device initialization routine
15687  * @pdev: PCI device information struct
15688  * @ent: entry in i40e_pci_tbl
15689  *
15690  * i40e_probe initializes a PF identified by a pci_dev structure.
15691  * The OS initialization, configuring of the PF private structure,
15692  * and a hardware reset occur.
15693  *
15694  * Returns 0 on success, negative on failure
15695  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)15696 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15697 {
15698 	struct i40e_aq_get_phy_abilities_resp abilities;
15699 #ifdef CONFIG_I40E_DCB
15700 	enum i40e_get_fw_lldp_status_resp lldp_status;
15701 #endif /* CONFIG_I40E_DCB */
15702 	struct i40e_pf *pf;
15703 	struct i40e_hw *hw;
15704 	static u16 pfs_found;
15705 	u16 wol_nvm_bits;
15706 	u16 link_status;
15707 #ifdef CONFIG_I40E_DCB
15708 	int status;
15709 #endif /* CONFIG_I40E_DCB */
15710 	int err;
15711 	u32 val;
15712 	u32 i;
15713 
15714 	err = pci_enable_device_mem(pdev);
15715 	if (err)
15716 		return err;
15717 
15718 	/* set up for high or low dma */
15719 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15720 	if (err) {
15721 		dev_err(&pdev->dev,
15722 			"DMA configuration failed: 0x%x\n", err);
15723 		goto err_dma;
15724 	}
15725 
15726 	/* set up pci connections */
15727 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15728 	if (err) {
15729 		dev_info(&pdev->dev,
15730 			 "pci_request_selected_regions failed %d\n", err);
15731 		goto err_pci_reg;
15732 	}
15733 
15734 	pci_set_master(pdev);
15735 
15736 	/* Now that we have a PCI connection, we need to do the
15737 	 * low level device setup.  This is primarily setting up
15738 	 * the Admin Queue structures and then querying for the
15739 	 * device's current profile information.
15740 	 */
15741 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15742 	if (!pf) {
15743 		err = -ENOMEM;
15744 		goto err_pf_alloc;
15745 	}
15746 	pf->next_vsi = 0;
15747 	pf->pdev = pdev;
15748 	set_bit(__I40E_DOWN, pf->state);
15749 
15750 	hw = &pf->hw;
15751 
15752 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15753 				I40E_MAX_CSR_SPACE);
15754 	/* We believe that the highest register to read is
15755 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15756 	 * is not less than that before mapping to prevent a
15757 	 * kernel panic.
15758 	 */
15759 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15760 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15761 			pf->ioremap_len);
15762 		err = -ENOMEM;
15763 		goto err_ioremap;
15764 	}
15765 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15766 	if (!hw->hw_addr) {
15767 		err = -EIO;
15768 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15769 			 (unsigned int)pci_resource_start(pdev, 0),
15770 			 pf->ioremap_len, err);
15771 		goto err_ioremap;
15772 	}
15773 	hw->vendor_id = pdev->vendor;
15774 	hw->device_id = pdev->device;
15775 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15776 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15777 	i40e_set_subsystem_device_id(hw);
15778 	hw->bus.device = PCI_SLOT(pdev->devfn);
15779 	hw->bus.func = PCI_FUNC(pdev->devfn);
15780 	hw->bus.bus_id = pdev->bus->number;
15781 	pf->instance = pfs_found;
15782 
15783 	/* Select something other than the 802.1ad ethertype for the
15784 	 * switch to use internally and drop on ingress.
15785 	 */
15786 	hw->switch_tag = 0xffff;
15787 	hw->first_tag = ETH_P_8021AD;
15788 	hw->second_tag = ETH_P_8021Q;
15789 
15790 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15791 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15792 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15793 
15794 	/* set up the locks for the AQ, do this only once in probe
15795 	 * and destroy them only once in remove
15796 	 */
15797 	mutex_init(&hw->aq.asq_mutex);
15798 	mutex_init(&hw->aq.arq_mutex);
15799 
15800 	pf->msg_enable = netif_msg_init(debug,
15801 					NETIF_MSG_DRV |
15802 					NETIF_MSG_PROBE |
15803 					NETIF_MSG_LINK);
15804 	if (debug < -1)
15805 		pf->hw.debug_mask = debug;
15806 
15807 	/* do a special CORER for clearing PXE mode once at init */
15808 	if (hw->revision_id == 0 &&
15809 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15810 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15811 		i40e_flush(hw);
15812 		msleep(200);
15813 		pf->corer_count++;
15814 
15815 		i40e_clear_pxe_mode(hw);
15816 	}
15817 
15818 	/* Reset here to make sure all is clean and to define PF 'n' */
15819 	i40e_clear_hw(hw);
15820 
15821 	err = i40e_set_mac_type(hw);
15822 	if (err) {
15823 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15824 			 err);
15825 		goto err_pf_reset;
15826 	}
15827 
15828 	err = i40e_handle_resets(pf);
15829 	if (err)
15830 		goto err_pf_reset;
15831 
15832 	i40e_check_recovery_mode(pf);
15833 
15834 	if (is_kdump_kernel()) {
15835 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15836 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15837 	} else {
15838 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15839 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15840 	}
15841 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15842 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15843 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15844 
15845 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15846 		 "%s-%s:misc",
15847 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15848 
15849 	err = i40e_init_shared_code(hw);
15850 	if (err) {
15851 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15852 			 err);
15853 		goto err_pf_reset;
15854 	}
15855 
15856 	/* set up a default setting for link flow control */
15857 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15858 
15859 	err = i40e_init_adminq(hw);
15860 	if (err) {
15861 		if (err == -EIO)
15862 			dev_info(&pdev->dev,
15863 				 "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",
15864 				 hw->aq.api_maj_ver,
15865 				 hw->aq.api_min_ver,
15866 				 I40E_FW_API_VERSION_MAJOR,
15867 				 I40E_FW_MINOR_VERSION(hw));
15868 		else
15869 			dev_info(&pdev->dev,
15870 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15871 
15872 		goto err_pf_reset;
15873 	}
15874 	i40e_get_oem_version(hw);
15875 
15876 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15877 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15878 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15879 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15880 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15881 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15882 
15883 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15884 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15885 		dev_dbg(&pdev->dev,
15886 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15887 			 hw->aq.api_maj_ver,
15888 			 hw->aq.api_min_ver,
15889 			 I40E_FW_API_VERSION_MAJOR,
15890 			 I40E_FW_MINOR_VERSION(hw));
15891 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15892 		dev_info(&pdev->dev,
15893 			 "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",
15894 			 hw->aq.api_maj_ver,
15895 			 hw->aq.api_min_ver,
15896 			 I40E_FW_API_VERSION_MAJOR,
15897 			 I40E_FW_MINOR_VERSION(hw));
15898 
15899 	i40e_verify_eeprom(pf);
15900 
15901 	/* Rev 0 hardware was never productized */
15902 	if (hw->revision_id < 1)
15903 		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");
15904 
15905 	i40e_clear_pxe_mode(hw);
15906 
15907 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15908 	if (err)
15909 		goto err_adminq_setup;
15910 
15911 	err = i40e_sw_init(pf);
15912 	if (err) {
15913 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15914 		goto err_sw_init;
15915 	}
15916 
15917 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15918 		return i40e_init_recovery_mode(pf, hw);
15919 
15920 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15921 				hw->func_caps.num_rx_qp, 0, 0);
15922 	if (err) {
15923 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15924 		goto err_init_lan_hmc;
15925 	}
15926 
15927 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15928 	if (err) {
15929 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15930 		err = -ENOENT;
15931 		goto err_configure_lan_hmc;
15932 	}
15933 
15934 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15935 	 * Ignore error return codes because if it was already disabled via
15936 	 * hardware settings this will fail
15937 	 */
15938 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15939 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15940 		i40e_aq_stop_lldp(hw, true, false, NULL);
15941 	}
15942 
15943 	/* allow a platform config to override the HW addr */
15944 	i40e_get_platform_mac_addr(pdev, pf);
15945 
15946 	if (!is_valid_ether_addr(hw->mac.addr)) {
15947 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15948 		err = -EIO;
15949 		goto err_mac_addr;
15950 	}
15951 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15952 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15953 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15954 	if (is_valid_ether_addr(hw->mac.port_addr))
15955 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15956 
15957 	i40e_ptp_alloc_pins(pf);
15958 	pci_set_drvdata(pdev, pf);
15959 	pci_save_state(pdev);
15960 
15961 #ifdef CONFIG_I40E_DCB
15962 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15963 	(!status &&
15964 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15965 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15966 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15967 	dev_info(&pdev->dev,
15968 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15969 			"FW LLDP is disabled\n" :
15970 			"FW LLDP is enabled\n");
15971 
15972 	/* Enable FW to write default DCB config on link-up */
15973 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15974 
15975 	err = i40e_init_pf_dcb(pf);
15976 	if (err) {
15977 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15978 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15979 		/* Continue without DCB enabled */
15980 	}
15981 #endif /* CONFIG_I40E_DCB */
15982 
15983 	/* set up periodic task facility */
15984 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15985 	pf->service_timer_period = HZ;
15986 
15987 	INIT_WORK(&pf->service_task, i40e_service_task);
15988 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15989 
15990 	/* NVM bit on means WoL disabled for the port */
15991 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15992 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15993 		pf->wol_en = false;
15994 	else
15995 		pf->wol_en = true;
15996 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15997 
15998 	/* set up the main switch operations */
15999 	i40e_determine_queue_usage(pf);
16000 	err = i40e_init_interrupt_scheme(pf);
16001 	if (err)
16002 		goto err_switch_setup;
16003 
16004 	/* Reduce Tx and Rx pairs for kdump
16005 	 * When MSI-X is enabled, it's not allowed to use more TC queue
16006 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
16007 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
16008 	 */
16009 	if (is_kdump_kernel())
16010 		pf->num_lan_msix = 1;
16011 
16012 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
16013 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
16014 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
16015 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
16016 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
16017 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
16018 						    UDP_TUNNEL_TYPE_GENEVE;
16019 
16020 	/* The number of VSIs reported by the FW is the minimum guaranteed
16021 	 * to us; HW supports far more and we share the remaining pool with
16022 	 * the other PFs. We allocate space for more than the guarantee with
16023 	 * the understanding that we might not get them all later.
16024 	 */
16025 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
16026 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
16027 	else
16028 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
16029 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
16030 		dev_warn(&pf->pdev->dev,
16031 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
16032 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
16033 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
16034 	}
16035 
16036 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
16037 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
16038 			  GFP_KERNEL);
16039 	if (!pf->vsi) {
16040 		err = -ENOMEM;
16041 		goto err_switch_setup;
16042 	}
16043 
16044 #ifdef CONFIG_PCI_IOV
16045 	/* prep for VF support */
16046 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16047 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16048 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16049 		if (pci_num_vf(pdev))
16050 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
16051 	}
16052 #endif
16053 	err = i40e_setup_pf_switch(pf, false, false);
16054 	if (err) {
16055 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
16056 		goto err_vsis;
16057 	}
16058 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
16059 
16060 	/* if FDIR VSI was set up, start it now */
16061 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16062 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
16063 			i40e_vsi_open(pf->vsi[i]);
16064 			break;
16065 		}
16066 	}
16067 
16068 	/* The driver only wants link up/down and module qualification
16069 	 * reports from firmware.  Note the negative logic.
16070 	 */
16071 	err = i40e_aq_set_phy_int_mask(&pf->hw,
16072 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
16073 					 I40E_AQ_EVENT_MEDIA_NA |
16074 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
16075 	if (err)
16076 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
16077 			 ERR_PTR(err),
16078 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16079 
16080 	/* Reconfigure hardware for allowing smaller MSS in the case
16081 	 * of TSO, so that we avoid the MDD being fired and causing
16082 	 * a reset in the case of small MSS+TSO.
16083 	 */
16084 	val = rd32(hw, I40E_REG_MSS);
16085 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16086 		val &= ~I40E_REG_MSS_MIN_MASK;
16087 		val |= I40E_64BYTE_MSS;
16088 		wr32(hw, I40E_REG_MSS, val);
16089 	}
16090 
16091 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
16092 		msleep(75);
16093 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16094 		if (err)
16095 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16096 				 ERR_PTR(err),
16097 				 i40e_aq_str(&pf->hw,
16098 					     pf->hw.aq.asq_last_status));
16099 	}
16100 	/* The main driver is (mostly) up and happy. We need to set this state
16101 	 * before setting up the misc vector or we get a race and the vector
16102 	 * ends up disabled forever.
16103 	 */
16104 	clear_bit(__I40E_DOWN, pf->state);
16105 
16106 	/* In case of MSIX we are going to setup the misc vector right here
16107 	 * to handle admin queue events etc. In case of legacy and MSI
16108 	 * the misc functionality and queue processing is combined in
16109 	 * the same vector and that gets setup at open.
16110 	 */
16111 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
16112 		err = i40e_setup_misc_vector(pf);
16113 		if (err) {
16114 			dev_info(&pdev->dev,
16115 				 "setup of misc vector failed: %d\n", err);
16116 			i40e_cloud_filter_exit(pf);
16117 			i40e_fdir_teardown(pf);
16118 			goto err_vsis;
16119 		}
16120 	}
16121 
16122 #ifdef CONFIG_PCI_IOV
16123 	/* prep for VF support */
16124 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16125 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16126 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16127 		/* disable link interrupts for VFs */
16128 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16129 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16130 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16131 		i40e_flush(hw);
16132 
16133 		if (pci_num_vf(pdev)) {
16134 			dev_info(&pdev->dev,
16135 				 "Active VFs found, allocating resources.\n");
16136 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16137 			if (err)
16138 				dev_info(&pdev->dev,
16139 					 "Error %d allocating resources for existing VFs\n",
16140 					 err);
16141 		}
16142 	}
16143 #endif /* CONFIG_PCI_IOV */
16144 
16145 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16146 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16147 						      pf->num_iwarp_msix,
16148 						      I40E_IWARP_IRQ_PILE_ID);
16149 		if (pf->iwarp_base_vector < 0) {
16150 			dev_info(&pdev->dev,
16151 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16152 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16153 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16154 		}
16155 	}
16156 
16157 	i40e_dbg_pf_init(pf);
16158 
16159 	/* tell the firmware that we're starting */
16160 	i40e_send_version(pf);
16161 
16162 	/* since everything's happy, start the service_task timer */
16163 	mod_timer(&pf->service_timer,
16164 		  round_jiffies(jiffies + pf->service_timer_period));
16165 
16166 	/* add this PF to client device list and launch a client service task */
16167 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16168 		err = i40e_lan_add_device(pf);
16169 		if (err)
16170 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16171 				 err);
16172 	}
16173 
16174 #define PCI_SPEED_SIZE 8
16175 #define PCI_WIDTH_SIZE 8
16176 	/* Devices on the IOSF bus do not have this information
16177 	 * and will report PCI Gen 1 x 1 by default so don't bother
16178 	 * checking them.
16179 	 */
16180 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16181 		char speed[PCI_SPEED_SIZE] = "Unknown";
16182 		char width[PCI_WIDTH_SIZE] = "Unknown";
16183 
16184 		/* Get the negotiated link width and speed from PCI config
16185 		 * space
16186 		 */
16187 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16188 					  &link_status);
16189 
16190 		i40e_set_pci_config_data(hw, link_status);
16191 
16192 		switch (hw->bus.speed) {
16193 		case i40e_bus_speed_8000:
16194 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16195 		case i40e_bus_speed_5000:
16196 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16197 		case i40e_bus_speed_2500:
16198 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16199 		default:
16200 			break;
16201 		}
16202 		switch (hw->bus.width) {
16203 		case i40e_bus_width_pcie_x8:
16204 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16205 		case i40e_bus_width_pcie_x4:
16206 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16207 		case i40e_bus_width_pcie_x2:
16208 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16209 		case i40e_bus_width_pcie_x1:
16210 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16211 		default:
16212 			break;
16213 		}
16214 
16215 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16216 			 speed, width);
16217 
16218 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16219 		    hw->bus.speed < i40e_bus_speed_8000) {
16220 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16221 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16222 		}
16223 	}
16224 
16225 	/* get the requested speeds from the fw */
16226 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16227 	if (err)
16228 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16229 			ERR_PTR(err),
16230 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16231 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16232 
16233 	/* set the FEC config due to the board capabilities */
16234 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16235 
16236 	/* get the supported phy types from the fw */
16237 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16238 	if (err)
16239 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16240 			ERR_PTR(err),
16241 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16242 
16243 	/* make sure the MFS hasn't been set lower than the default */
16244 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16245 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16246 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16247 	if (val < MAX_FRAME_SIZE_DEFAULT)
16248 		dev_warn(&pdev->dev, "MFS for port %x (%d) has been set below the default (%d)\n",
16249 			 pf->hw.port, val, MAX_FRAME_SIZE_DEFAULT);
16250 
16251 	/* Add a filter to drop all Flow control frames from any VSI from being
16252 	 * transmitted. By doing so we stop a malicious VF from sending out
16253 	 * PAUSE or PFC frames and potentially controlling traffic for other
16254 	 * PF/VF VSIs.
16255 	 * The FW can still send Flow control frames if enabled.
16256 	 */
16257 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16258 						       pf->main_vsi_seid);
16259 
16260 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16261 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16262 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16263 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16264 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16265 	/* print a string summarizing features */
16266 	i40e_print_features(pf);
16267 
16268 	return 0;
16269 
16270 	/* Unwind what we've done if something failed in the setup */
16271 err_vsis:
16272 	set_bit(__I40E_DOWN, pf->state);
16273 	i40e_clear_interrupt_scheme(pf);
16274 	kfree(pf->vsi);
16275 err_switch_setup:
16276 	i40e_reset_interrupt_capability(pf);
16277 	timer_shutdown_sync(&pf->service_timer);
16278 err_mac_addr:
16279 err_configure_lan_hmc:
16280 	(void)i40e_shutdown_lan_hmc(hw);
16281 err_init_lan_hmc:
16282 	kfree(pf->qp_pile);
16283 err_sw_init:
16284 err_adminq_setup:
16285 err_pf_reset:
16286 	iounmap(hw->hw_addr);
16287 err_ioremap:
16288 	kfree(pf);
16289 err_pf_alloc:
16290 	pci_release_mem_regions(pdev);
16291 err_pci_reg:
16292 err_dma:
16293 	pci_disable_device(pdev);
16294 	return err;
16295 }
16296 
16297 /**
16298  * i40e_remove - Device removal routine
16299  * @pdev: PCI device information struct
16300  *
16301  * i40e_remove is called by the PCI subsystem to alert the driver
16302  * that is should release a PCI device.  This could be caused by a
16303  * Hot-Plug event, or because the driver is going to be removed from
16304  * memory.
16305  **/
i40e_remove(struct pci_dev * pdev)16306 static void i40e_remove(struct pci_dev *pdev)
16307 {
16308 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16309 	struct i40e_hw *hw = &pf->hw;
16310 	int ret_code;
16311 	int i;
16312 
16313 	i40e_dbg_pf_exit(pf);
16314 
16315 	i40e_ptp_stop(pf);
16316 
16317 	/* Disable RSS in hw */
16318 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16319 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16320 
16321 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16322 	 * flags, once they are set, i40e_rebuild should not be called as
16323 	 * i40e_prep_for_reset always returns early.
16324 	 */
16325 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16326 		usleep_range(1000, 2000);
16327 	set_bit(__I40E_IN_REMOVE, pf->state);
16328 
16329 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16330 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16331 		i40e_free_vfs(pf);
16332 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16333 	}
16334 	/* no more scheduling of any task */
16335 	set_bit(__I40E_SUSPENDED, pf->state);
16336 	set_bit(__I40E_DOWN, pf->state);
16337 	if (pf->service_timer.function)
16338 		timer_shutdown_sync(&pf->service_timer);
16339 	if (pf->service_task.func)
16340 		cancel_work_sync(&pf->service_task);
16341 
16342 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16343 		struct i40e_vsi *vsi = pf->vsi[0];
16344 
16345 		/* We know that we have allocated only one vsi for this PF,
16346 		 * it was just for registering netdevice, so the interface
16347 		 * could be visible in the 'ifconfig' output
16348 		 */
16349 		unregister_netdev(vsi->netdev);
16350 		free_netdev(vsi->netdev);
16351 
16352 		goto unmap;
16353 	}
16354 
16355 	/* Client close must be called explicitly here because the timer
16356 	 * has been stopped.
16357 	 */
16358 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16359 
16360 	i40e_fdir_teardown(pf);
16361 
16362 	/* If there is a switch structure or any orphans, remove them.
16363 	 * This will leave only the PF's VSI remaining.
16364 	 */
16365 	for (i = 0; i < I40E_MAX_VEB; i++) {
16366 		if (!pf->veb[i])
16367 			continue;
16368 
16369 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16370 		    pf->veb[i]->uplink_seid == 0)
16371 			i40e_switch_branch_release(pf->veb[i]);
16372 	}
16373 
16374 	/* Now we can shutdown the PF's VSIs, just before we kill
16375 	 * adminq and hmc.
16376 	 */
16377 	for (i = pf->num_alloc_vsi; i--;)
16378 		if (pf->vsi[i]) {
16379 			i40e_vsi_close(pf->vsi[i]);
16380 			i40e_vsi_release(pf->vsi[i]);
16381 			pf->vsi[i] = NULL;
16382 		}
16383 
16384 	i40e_cloud_filter_exit(pf);
16385 
16386 	/* remove attached clients */
16387 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16388 		ret_code = i40e_lan_del_device(pf);
16389 		if (ret_code)
16390 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16391 				 ret_code);
16392 	}
16393 
16394 	/* shutdown and destroy the HMC */
16395 	if (hw->hmc.hmc_obj) {
16396 		ret_code = i40e_shutdown_lan_hmc(hw);
16397 		if (ret_code)
16398 			dev_warn(&pdev->dev,
16399 				 "Failed to destroy the HMC resources: %d\n",
16400 				 ret_code);
16401 	}
16402 
16403 unmap:
16404 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16405 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16406 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16407 		free_irq(pf->pdev->irq, pf);
16408 
16409 	/* shutdown the adminq */
16410 	i40e_shutdown_adminq(hw);
16411 
16412 	/* destroy the locks only once, here */
16413 	mutex_destroy(&hw->aq.arq_mutex);
16414 	mutex_destroy(&hw->aq.asq_mutex);
16415 
16416 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16417 	rtnl_lock();
16418 	i40e_clear_interrupt_scheme(pf);
16419 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16420 		if (pf->vsi[i]) {
16421 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16422 				i40e_vsi_clear_rings(pf->vsi[i]);
16423 			i40e_vsi_clear(pf->vsi[i]);
16424 			pf->vsi[i] = NULL;
16425 		}
16426 	}
16427 	rtnl_unlock();
16428 
16429 	for (i = 0; i < I40E_MAX_VEB; i++) {
16430 		kfree(pf->veb[i]);
16431 		pf->veb[i] = NULL;
16432 	}
16433 
16434 	kfree(pf->qp_pile);
16435 	kfree(pf->vsi);
16436 
16437 	iounmap(hw->hw_addr);
16438 	kfree(pf);
16439 	pci_release_mem_regions(pdev);
16440 
16441 	pci_disable_device(pdev);
16442 }
16443 
16444 /**
16445  * i40e_pci_error_detected - warning that something funky happened in PCI land
16446  * @pdev: PCI device information struct
16447  * @error: the type of PCI error
16448  *
16449  * Called to warn that something happened and the error handling steps
16450  * are in progress.  Allows the driver to quiesce things, be ready for
16451  * remediation.
16452  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)16453 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16454 						pci_channel_state_t error)
16455 {
16456 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16457 
16458 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16459 
16460 	if (!pf) {
16461 		dev_info(&pdev->dev,
16462 			 "Cannot recover - error happened during device probe\n");
16463 		return PCI_ERS_RESULT_DISCONNECT;
16464 	}
16465 
16466 	/* shutdown all operations */
16467 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16468 		i40e_prep_for_reset(pf);
16469 
16470 	/* Request a slot reset */
16471 	return PCI_ERS_RESULT_NEED_RESET;
16472 }
16473 
16474 /**
16475  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16476  * @pdev: PCI device information struct
16477  *
16478  * Called to find if the driver can work with the device now that
16479  * the pci slot has been reset.  If a basic connection seems good
16480  * (registers are readable and have sane content) then return a
16481  * happy little PCI_ERS_RESULT_xxx.
16482  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)16483 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16484 {
16485 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16486 	pci_ers_result_t result;
16487 	u32 reg;
16488 
16489 	dev_dbg(&pdev->dev, "%s\n", __func__);
16490 	if (pci_enable_device_mem(pdev)) {
16491 		dev_info(&pdev->dev,
16492 			 "Cannot re-enable PCI device after reset.\n");
16493 		result = PCI_ERS_RESULT_DISCONNECT;
16494 	} else {
16495 		pci_set_master(pdev);
16496 		pci_restore_state(pdev);
16497 		pci_save_state(pdev);
16498 		pci_wake_from_d3(pdev, false);
16499 
16500 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16501 		if (reg == 0)
16502 			result = PCI_ERS_RESULT_RECOVERED;
16503 		else
16504 			result = PCI_ERS_RESULT_DISCONNECT;
16505 	}
16506 
16507 	return result;
16508 }
16509 
16510 /**
16511  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16512  * @pdev: PCI device information struct
16513  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)16514 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16515 {
16516 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16517 
16518 	i40e_prep_for_reset(pf);
16519 }
16520 
16521 /**
16522  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16523  * @pdev: PCI device information struct
16524  */
i40e_pci_error_reset_done(struct pci_dev * pdev)16525 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16526 {
16527 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16528 
16529 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16530 		return;
16531 
16532 	i40e_reset_and_rebuild(pf, false, false);
16533 #ifdef CONFIG_PCI_IOV
16534 	i40e_restore_all_vfs_msi_state(pdev);
16535 #endif /* CONFIG_PCI_IOV */
16536 }
16537 
16538 /**
16539  * i40e_pci_error_resume - restart operations after PCI error recovery
16540  * @pdev: PCI device information struct
16541  *
16542  * Called to allow the driver to bring things back up after PCI error
16543  * and/or reset recovery has finished.
16544  **/
i40e_pci_error_resume(struct pci_dev * pdev)16545 static void i40e_pci_error_resume(struct pci_dev *pdev)
16546 {
16547 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16548 
16549 	dev_dbg(&pdev->dev, "%s\n", __func__);
16550 	if (test_bit(__I40E_SUSPENDED, pf->state))
16551 		return;
16552 
16553 	i40e_handle_reset_warning(pf, false);
16554 }
16555 
16556 /**
16557  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16558  * using the mac_address_write admin q function
16559  * @pf: pointer to i40e_pf struct
16560  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)16561 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16562 {
16563 	struct i40e_hw *hw = &pf->hw;
16564 	u8 mac_addr[6];
16565 	u16 flags = 0;
16566 	int ret;
16567 
16568 	/* Get current MAC address in case it's an LAA */
16569 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16570 		ether_addr_copy(mac_addr,
16571 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16572 	} else {
16573 		dev_err(&pf->pdev->dev,
16574 			"Failed to retrieve MAC address; using default\n");
16575 		ether_addr_copy(mac_addr, hw->mac.addr);
16576 	}
16577 
16578 	/* The FW expects the mac address write cmd to first be called with
16579 	 * one of these flags before calling it again with the multicast
16580 	 * enable flags.
16581 	 */
16582 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16583 
16584 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16585 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16586 
16587 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16588 	if (ret) {
16589 		dev_err(&pf->pdev->dev,
16590 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16591 		return;
16592 	}
16593 
16594 	flags = I40E_AQC_MC_MAG_EN
16595 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16596 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16597 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16598 	if (ret)
16599 		dev_err(&pf->pdev->dev,
16600 			"Failed to enable Multicast Magic Packet wake up\n");
16601 }
16602 
16603 /**
16604  * i40e_shutdown - PCI callback for shutting down
16605  * @pdev: PCI device information struct
16606  **/
i40e_shutdown(struct pci_dev * pdev)16607 static void i40e_shutdown(struct pci_dev *pdev)
16608 {
16609 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16610 	struct i40e_hw *hw = &pf->hw;
16611 
16612 	set_bit(__I40E_SUSPENDED, pf->state);
16613 	set_bit(__I40E_DOWN, pf->state);
16614 
16615 	del_timer_sync(&pf->service_timer);
16616 	cancel_work_sync(&pf->service_task);
16617 	i40e_cloud_filter_exit(pf);
16618 	i40e_fdir_teardown(pf);
16619 
16620 	/* Client close must be called explicitly here because the timer
16621 	 * has been stopped.
16622 	 */
16623 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16624 
16625 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16626 		i40e_enable_mc_magic_wake(pf);
16627 
16628 	i40e_prep_for_reset(pf);
16629 
16630 	wr32(hw, I40E_PFPM_APM,
16631 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16632 	wr32(hw, I40E_PFPM_WUFC,
16633 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16634 
16635 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16636 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16637 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16638 		free_irq(pf->pdev->irq, pf);
16639 
16640 	/* Since we're going to destroy queues during the
16641 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16642 	 * whole section
16643 	 */
16644 	rtnl_lock();
16645 	i40e_clear_interrupt_scheme(pf);
16646 	rtnl_unlock();
16647 
16648 	if (system_state == SYSTEM_POWER_OFF) {
16649 		pci_wake_from_d3(pdev, pf->wol_en);
16650 		pci_set_power_state(pdev, PCI_D3hot);
16651 	}
16652 }
16653 
16654 /**
16655  * i40e_suspend - PM callback for moving to D3
16656  * @dev: generic device information structure
16657  **/
i40e_suspend(struct device * dev)16658 static int __maybe_unused i40e_suspend(struct device *dev)
16659 {
16660 	struct i40e_pf *pf = dev_get_drvdata(dev);
16661 	struct i40e_hw *hw = &pf->hw;
16662 
16663 	/* If we're already suspended, then there is nothing to do */
16664 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16665 		return 0;
16666 
16667 	set_bit(__I40E_DOWN, pf->state);
16668 
16669 	/* Ensure service task will not be running */
16670 	del_timer_sync(&pf->service_timer);
16671 	cancel_work_sync(&pf->service_task);
16672 
16673 	/* Client close must be called explicitly here because the timer
16674 	 * has been stopped.
16675 	 */
16676 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16677 
16678 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16679 		i40e_enable_mc_magic_wake(pf);
16680 
16681 	/* Since we're going to destroy queues during the
16682 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16683 	 * whole section
16684 	 */
16685 	rtnl_lock();
16686 
16687 	i40e_prep_for_reset(pf);
16688 
16689 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16690 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16691 
16692 	/* Clear the interrupt scheme and release our IRQs so that the system
16693 	 * can safely hibernate even when there are a large number of CPUs.
16694 	 * Otherwise hibernation might fail when mapping all the vectors back
16695 	 * to CPU0.
16696 	 */
16697 	i40e_clear_interrupt_scheme(pf);
16698 
16699 	rtnl_unlock();
16700 
16701 	return 0;
16702 }
16703 
16704 /**
16705  * i40e_resume - PM callback for waking up from D3
16706  * @dev: generic device information structure
16707  **/
i40e_resume(struct device * dev)16708 static int __maybe_unused i40e_resume(struct device *dev)
16709 {
16710 	struct i40e_pf *pf = dev_get_drvdata(dev);
16711 	int err;
16712 
16713 	/* If we're not suspended, then there is nothing to do */
16714 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16715 		return 0;
16716 
16717 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16718 	 * since we're going to be restoring queues
16719 	 */
16720 	rtnl_lock();
16721 
16722 	/* We cleared the interrupt scheme when we suspended, so we need to
16723 	 * restore it now to resume device functionality.
16724 	 */
16725 	err = i40e_restore_interrupt_scheme(pf);
16726 	if (err) {
16727 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16728 			err);
16729 	}
16730 
16731 	clear_bit(__I40E_DOWN, pf->state);
16732 	i40e_reset_and_rebuild(pf, false, true);
16733 
16734 	rtnl_unlock();
16735 
16736 	/* Clear suspended state last after everything is recovered */
16737 	clear_bit(__I40E_SUSPENDED, pf->state);
16738 
16739 	/* Restart the service task */
16740 	mod_timer(&pf->service_timer,
16741 		  round_jiffies(jiffies + pf->service_timer_period));
16742 
16743 	return 0;
16744 }
16745 
16746 static const struct pci_error_handlers i40e_err_handler = {
16747 	.error_detected = i40e_pci_error_detected,
16748 	.slot_reset = i40e_pci_error_slot_reset,
16749 	.reset_prepare = i40e_pci_error_reset_prepare,
16750 	.reset_done = i40e_pci_error_reset_done,
16751 	.resume = i40e_pci_error_resume,
16752 };
16753 
16754 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16755 
16756 static struct pci_driver i40e_driver = {
16757 	.name     = i40e_driver_name,
16758 	.id_table = i40e_pci_tbl,
16759 	.probe    = i40e_probe,
16760 	.remove   = i40e_remove,
16761 	.driver   = {
16762 		.pm = &i40e_pm_ops,
16763 	},
16764 	.shutdown = i40e_shutdown,
16765 	.err_handler = &i40e_err_handler,
16766 	.sriov_configure = i40e_pci_sriov_configure,
16767 };
16768 
16769 /**
16770  * i40e_init_module - Driver registration routine
16771  *
16772  * i40e_init_module is the first routine called when the driver is
16773  * loaded. All it does is register with the PCI subsystem.
16774  **/
i40e_init_module(void)16775 static int __init i40e_init_module(void)
16776 {
16777 	int err;
16778 
16779 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16780 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16781 
16782 	/* There is no need to throttle the number of active tasks because
16783 	 * each device limits its own task using a state bit for scheduling
16784 	 * the service task, and the device tasks do not interfere with each
16785 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16786 	 * since we need to be able to guarantee forward progress even under
16787 	 * memory pressure.
16788 	 */
16789 	i40e_wq = alloc_workqueue("%s", 0, 0, i40e_driver_name);
16790 	if (!i40e_wq) {
16791 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16792 		return -ENOMEM;
16793 	}
16794 
16795 	i40e_dbg_init();
16796 	err = pci_register_driver(&i40e_driver);
16797 	if (err) {
16798 		destroy_workqueue(i40e_wq);
16799 		i40e_dbg_exit();
16800 		return err;
16801 	}
16802 
16803 	return 0;
16804 }
16805 module_init(i40e_init_module);
16806 
16807 /**
16808  * i40e_exit_module - Driver exit cleanup routine
16809  *
16810  * i40e_exit_module is called just before the driver is removed
16811  * from memory.
16812  **/
i40e_exit_module(void)16813 static void __exit i40e_exit_module(void)
16814 {
16815 	pci_unregister_driver(&i40e_driver);
16816 	destroy_workqueue(i40e_wq);
16817 	ida_destroy(&i40e_client_ida);
16818 	i40e_dbg_exit();
16819 }
16820 module_exit(i40e_exit_module);
16821