1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
88 	/* required last entry */
89 	{0, }
90 };
91 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
92 
93 #define I40E_MAX_VF_COUNT 128
94 static int debug = -1;
95 module_param(debug, uint, 0);
96 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
97 
98 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
99 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
100 MODULE_LICENSE("GPL v2");
101 
102 static struct workqueue_struct *i40e_wq;
103 
netdev_hw_addr_refcnt(struct i40e_mac_filter * f,struct net_device * netdev,int delta)104 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
105 				  struct net_device *netdev, int delta)
106 {
107 	struct netdev_hw_addr *ha;
108 
109 	if (!f || !netdev)
110 		return;
111 
112 	netdev_for_each_mc_addr(ha, netdev) {
113 		if (ether_addr_equal(ha->addr, f->macaddr)) {
114 			ha->refcount += delta;
115 			if (ha->refcount <= 0)
116 				ha->refcount = 1;
117 			break;
118 		}
119 	}
120 }
121 
122 /**
123  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
124  * @hw:   pointer to the HW structure
125  * @mem:  ptr to mem struct to fill out
126  * @size: size of memory requested
127  * @alignment: what to align the allocation to
128  **/
i40e_allocate_dma_mem_d(struct i40e_hw * hw,struct i40e_dma_mem * mem,u64 size,u32 alignment)129 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
130 			    u64 size, u32 alignment)
131 {
132 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
133 
134 	mem->size = ALIGN(size, alignment);
135 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
136 				     GFP_KERNEL);
137 	if (!mem->va)
138 		return -ENOMEM;
139 
140 	return 0;
141 }
142 
143 /**
144  * i40e_free_dma_mem_d - OS specific memory free for shared code
145  * @hw:   pointer to the HW structure
146  * @mem:  ptr to mem struct to free
147  **/
i40e_free_dma_mem_d(struct i40e_hw * hw,struct i40e_dma_mem * mem)148 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
149 {
150 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
151 
152 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
153 	mem->va = NULL;
154 	mem->pa = 0;
155 	mem->size = 0;
156 
157 	return 0;
158 }
159 
160 /**
161  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
162  * @hw:   pointer to the HW structure
163  * @mem:  ptr to mem struct to fill out
164  * @size: size of memory requested
165  **/
i40e_allocate_virt_mem_d(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)166 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
167 			     u32 size)
168 {
169 	mem->size = size;
170 	mem->va = kzalloc(size, GFP_KERNEL);
171 
172 	if (!mem->va)
173 		return -ENOMEM;
174 
175 	return 0;
176 }
177 
178 /**
179  * i40e_free_virt_mem_d - OS specific memory free for shared code
180  * @hw:   pointer to the HW structure
181  * @mem:  ptr to mem struct to free
182  **/
i40e_free_virt_mem_d(struct i40e_hw * hw,struct i40e_virt_mem * mem)183 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
184 {
185 	/* it's ok to kfree a NULL pointer */
186 	kfree(mem->va);
187 	mem->va = NULL;
188 	mem->size = 0;
189 
190 	return 0;
191 }
192 
193 /**
194  * i40e_get_lump - find a lump of free generic resource
195  * @pf: board private structure
196  * @pile: the pile of resource to search
197  * @needed: the number of items needed
198  * @id: an owner id to stick on the items assigned
199  *
200  * Returns the base item index of the lump, or negative for error
201  **/
i40e_get_lump(struct i40e_pf * pf,struct i40e_lump_tracking * pile,u16 needed,u16 id)202 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
203 			 u16 needed, u16 id)
204 {
205 	int ret = -ENOMEM;
206 	int i, j;
207 
208 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
209 		dev_info(&pf->pdev->dev,
210 			 "param err: pile=%s needed=%d id=0x%04x\n",
211 			 pile ? "<valid>" : "<null>", needed, id);
212 		return -EINVAL;
213 	}
214 
215 	/* Allocate last queue in the pile for FDIR VSI queue
216 	 * so it doesn't fragment the qp_pile
217 	 */
218 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
219 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
220 			dev_err(&pf->pdev->dev,
221 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
222 				pile->num_entries - 1);
223 			return -ENOMEM;
224 		}
225 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
226 		return pile->num_entries - 1;
227 	}
228 
229 	i = 0;
230 	while (i < pile->num_entries) {
231 		/* skip already allocated entries */
232 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
233 			i++;
234 			continue;
235 		}
236 
237 		/* do we have enough in this lump? */
238 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
239 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
240 				break;
241 		}
242 
243 		if (j == needed) {
244 			/* there was enough, so assign it to the requestor */
245 			for (j = 0; j < needed; j++)
246 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
247 			ret = i;
248 			break;
249 		}
250 
251 		/* not enough, so skip over it and continue looking */
252 		i += j;
253 	}
254 
255 	return ret;
256 }
257 
258 /**
259  * i40e_put_lump - return a lump of generic resource
260  * @pile: the pile of resource to search
261  * @index: the base item index
262  * @id: the owner id of the items assigned
263  *
264  * Returns the count of items in the lump
265  **/
i40e_put_lump(struct i40e_lump_tracking * pile,u16 index,u16 id)266 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
267 {
268 	int valid_id = (id | I40E_PILE_VALID_BIT);
269 	int count = 0;
270 	u16 i;
271 
272 	if (!pile || index >= pile->num_entries)
273 		return -EINVAL;
274 
275 	for (i = index;
276 	     i < pile->num_entries && pile->list[i] == valid_id;
277 	     i++) {
278 		pile->list[i] = 0;
279 		count++;
280 	}
281 
282 
283 	return count;
284 }
285 
286 /**
287  * i40e_find_vsi_from_id - searches for the vsi with the given id
288  * @pf: the pf structure to search for the vsi
289  * @id: id of the vsi it is searching for
290  **/
i40e_find_vsi_from_id(struct i40e_pf * pf,u16 id)291 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
292 {
293 	int i;
294 
295 	for (i = 0; i < pf->num_alloc_vsi; i++)
296 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
297 			return pf->vsi[i];
298 
299 	return NULL;
300 }
301 
302 /**
303  * i40e_service_event_schedule - Schedule the service task to wake up
304  * @pf: board private structure
305  *
306  * If not already scheduled, this puts the task into the work queue
307  **/
i40e_service_event_schedule(struct i40e_pf * pf)308 void i40e_service_event_schedule(struct i40e_pf *pf)
309 {
310 	if ((!test_bit(__I40E_DOWN, pf->state) &&
311 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
312 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
313 		queue_work(i40e_wq, &pf->service_task);
314 }
315 
316 /**
317  * i40e_tx_timeout - Respond to a Tx Hang
318  * @netdev: network interface device structure
319  * @txqueue: queue number timing out
320  *
321  * If any port has noticed a Tx timeout, it is likely that the whole
322  * device is munged, not just the one netdev port, so go for the full
323  * reset.
324  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)325 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
326 {
327 	struct i40e_netdev_priv *np = netdev_priv(netdev);
328 	struct i40e_vsi *vsi = np->vsi;
329 	struct i40e_pf *pf = vsi->back;
330 	struct i40e_ring *tx_ring = NULL;
331 	unsigned int i;
332 	u32 head, val;
333 
334 	pf->tx_timeout_count++;
335 
336 	/* with txqueue index, find the tx_ring struct */
337 	for (i = 0; i < vsi->num_queue_pairs; i++) {
338 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
339 			if (txqueue ==
340 			    vsi->tx_rings[i]->queue_index) {
341 				tx_ring = vsi->tx_rings[i];
342 				break;
343 			}
344 		}
345 	}
346 
347 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
348 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
349 	else if (time_before(jiffies,
350 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
351 		return;   /* don't do any new action before the next timeout */
352 
353 	/* don't kick off another recovery if one is already pending */
354 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
355 		return;
356 
357 	if (tx_ring) {
358 		head = i40e_get_head(tx_ring);
359 		/* Read interrupt register */
360 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
361 			val = rd32(&pf->hw,
362 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
363 						tx_ring->vsi->base_vector - 1));
364 		else
365 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
366 
367 		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",
368 			    vsi->seid, txqueue, tx_ring->next_to_clean,
369 			    head, tx_ring->next_to_use,
370 			    readl(tx_ring->tail), val);
371 	}
372 
373 	pf->tx_timeout_last_recovery = jiffies;
374 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
375 		    pf->tx_timeout_recovery_level, txqueue);
376 
377 	switch (pf->tx_timeout_recovery_level) {
378 	case 1:
379 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
380 		break;
381 	case 2:
382 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
383 		break;
384 	case 3:
385 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
386 		break;
387 	default:
388 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
389 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
390 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
391 		break;
392 	}
393 
394 	i40e_service_event_schedule(pf);
395 	pf->tx_timeout_recovery_level++;
396 }
397 
398 /**
399  * i40e_get_vsi_stats_struct - Get System Network Statistics
400  * @vsi: the VSI we care about
401  *
402  * Returns the address of the device statistics structure.
403  * The statistics are actually updated from the service task.
404  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)405 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
406 {
407 	return &vsi->net_stats;
408 }
409 
410 /**
411  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
412  * @ring: Tx ring to get statistics from
413  * @stats: statistics entry to be updated
414  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)415 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
416 					    struct rtnl_link_stats64 *stats)
417 {
418 	u64 bytes, packets;
419 	unsigned int start;
420 
421 	do {
422 		start = u64_stats_fetch_begin_irq(&ring->syncp);
423 		packets = ring->stats.packets;
424 		bytes   = ring->stats.bytes;
425 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
426 
427 	stats->tx_packets += packets;
428 	stats->tx_bytes   += bytes;
429 }
430 
431 /**
432  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
433  * @netdev: network interface device structure
434  * @stats: data structure to store statistics
435  *
436  * Returns the address of the device statistics structure.
437  * The statistics are actually updated from the service task.
438  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)439 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
440 				  struct rtnl_link_stats64 *stats)
441 {
442 	struct i40e_netdev_priv *np = netdev_priv(netdev);
443 	struct i40e_vsi *vsi = np->vsi;
444 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
445 	struct i40e_ring *ring;
446 	int i;
447 
448 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
449 		return;
450 
451 	if (!vsi->tx_rings)
452 		return;
453 
454 	rcu_read_lock();
455 	for (i = 0; i < vsi->num_queue_pairs; i++) {
456 		u64 bytes, packets;
457 		unsigned int start;
458 
459 		ring = READ_ONCE(vsi->tx_rings[i]);
460 		if (!ring)
461 			continue;
462 		i40e_get_netdev_stats_struct_tx(ring, stats);
463 
464 		if (i40e_enabled_xdp_vsi(vsi)) {
465 			ring = READ_ONCE(vsi->xdp_rings[i]);
466 			if (!ring)
467 				continue;
468 			i40e_get_netdev_stats_struct_tx(ring, stats);
469 		}
470 
471 		ring = READ_ONCE(vsi->rx_rings[i]);
472 		if (!ring)
473 			continue;
474 		do {
475 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
476 			packets = ring->stats.packets;
477 			bytes   = ring->stats.bytes;
478 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
479 
480 		stats->rx_packets += packets;
481 		stats->rx_bytes   += bytes;
482 
483 	}
484 	rcu_read_unlock();
485 
486 	/* following stats updated by i40e_watchdog_subtask() */
487 	stats->multicast	= vsi_stats->multicast;
488 	stats->tx_errors	= vsi_stats->tx_errors;
489 	stats->tx_dropped	= vsi_stats->tx_dropped;
490 	stats->rx_errors	= vsi_stats->rx_errors;
491 	stats->rx_dropped	= vsi_stats->rx_dropped;
492 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
493 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
494 }
495 
496 /**
497  * i40e_vsi_reset_stats - Resets all stats of the given vsi
498  * @vsi: the VSI to have its stats reset
499  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)500 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
501 {
502 	struct rtnl_link_stats64 *ns;
503 	int i;
504 
505 	if (!vsi)
506 		return;
507 
508 	ns = i40e_get_vsi_stats_struct(vsi);
509 	memset(ns, 0, sizeof(*ns));
510 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
511 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
512 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
513 	if (vsi->rx_rings && vsi->rx_rings[0]) {
514 		for (i = 0; i < vsi->num_queue_pairs; i++) {
515 			memset(&vsi->rx_rings[i]->stats, 0,
516 			       sizeof(vsi->rx_rings[i]->stats));
517 			memset(&vsi->rx_rings[i]->rx_stats, 0,
518 			       sizeof(vsi->rx_rings[i]->rx_stats));
519 			memset(&vsi->tx_rings[i]->stats, 0,
520 			       sizeof(vsi->tx_rings[i]->stats));
521 			memset(&vsi->tx_rings[i]->tx_stats, 0,
522 			       sizeof(vsi->tx_rings[i]->tx_stats));
523 		}
524 	}
525 	vsi->stat_offsets_loaded = false;
526 }
527 
528 /**
529  * i40e_pf_reset_stats - Reset all of the stats for the given PF
530  * @pf: the PF to be reset
531  **/
i40e_pf_reset_stats(struct i40e_pf * pf)532 void i40e_pf_reset_stats(struct i40e_pf *pf)
533 {
534 	int i;
535 
536 	memset(&pf->stats, 0, sizeof(pf->stats));
537 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
538 	pf->stat_offsets_loaded = false;
539 
540 	for (i = 0; i < I40E_MAX_VEB; i++) {
541 		if (pf->veb[i]) {
542 			memset(&pf->veb[i]->stats, 0,
543 			       sizeof(pf->veb[i]->stats));
544 			memset(&pf->veb[i]->stats_offsets, 0,
545 			       sizeof(pf->veb[i]->stats_offsets));
546 			memset(&pf->veb[i]->tc_stats, 0,
547 			       sizeof(pf->veb[i]->tc_stats));
548 			memset(&pf->veb[i]->tc_stats_offsets, 0,
549 			       sizeof(pf->veb[i]->tc_stats_offsets));
550 			pf->veb[i]->stat_offsets_loaded = false;
551 		}
552 	}
553 	pf->hw_csum_rx_error = 0;
554 }
555 
556 /**
557  * i40e_compute_pci_to_hw_id - compute index form PCI function.
558  * @vsi: ptr to the VSI to read from.
559  * @hw: ptr to the hardware info.
560  **/
i40e_compute_pci_to_hw_id(struct i40e_vsi * vsi,struct i40e_hw * hw)561 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
562 {
563 	int pf_count = i40e_get_pf_count(hw);
564 
565 	if (vsi->type == I40E_VSI_SRIOV)
566 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
567 
568 	return hw->port + BIT(7);
569 }
570 
571 /**
572  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
573  * @hw: ptr to the hardware info.
574  * @hireg: the high 32 bit reg to read.
575  * @loreg: the low 32 bit reg to read.
576  * @offset_loaded: has the initial offset been loaded yet.
577  * @offset: ptr to current offset value.
578  * @stat: ptr to the stat.
579  *
580  * Since the device stats are not reset at PFReset, they will not
581  * be zeroed when the driver starts.  We'll save the first values read
582  * and use them as offsets to be subtracted from the raw values in order
583  * to report stats that count from zero.
584  **/
i40e_stat_update64(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)585 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
586 			       bool offset_loaded, u64 *offset, u64 *stat)
587 {
588 	u64 new_data;
589 
590 	new_data = rd64(hw, loreg);
591 
592 	if (!offset_loaded || new_data < *offset)
593 		*offset = new_data;
594 	*stat = new_data - *offset;
595 }
596 
597 /**
598  * i40e_stat_update48 - read and update a 48 bit stat from the chip
599  * @hw: ptr to the hardware info
600  * @hireg: the high 32 bit reg to read
601  * @loreg: the low 32 bit reg to read
602  * @offset_loaded: has the initial offset been loaded yet
603  * @offset: ptr to current offset value
604  * @stat: ptr to the stat
605  *
606  * Since the device stats are not reset at PFReset, they likely will not
607  * be zeroed when the driver starts.  We'll save the first values read
608  * and use them as offsets to be subtracted from the raw values in order
609  * to report stats that count from zero.  In the process, we also manage
610  * the potential roll-over.
611  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)612 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
613 			       bool offset_loaded, u64 *offset, u64 *stat)
614 {
615 	u64 new_data;
616 
617 	if (hw->device_id == I40E_DEV_ID_QEMU) {
618 		new_data = rd32(hw, loreg);
619 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
620 	} else {
621 		new_data = rd64(hw, loreg);
622 	}
623 	if (!offset_loaded)
624 		*offset = new_data;
625 	if (likely(new_data >= *offset))
626 		*stat = new_data - *offset;
627 	else
628 		*stat = (new_data + BIT_ULL(48)) - *offset;
629 	*stat &= 0xFFFFFFFFFFFFULL;
630 }
631 
632 /**
633  * i40e_stat_update32 - read and update a 32 bit stat from the chip
634  * @hw: ptr to the hardware info
635  * @reg: the hw reg to read
636  * @offset_loaded: has the initial offset been loaded yet
637  * @offset: ptr to current offset value
638  * @stat: ptr to the stat
639  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)640 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
641 			       bool offset_loaded, u64 *offset, u64 *stat)
642 {
643 	u32 new_data;
644 
645 	new_data = rd32(hw, reg);
646 	if (!offset_loaded)
647 		*offset = new_data;
648 	if (likely(new_data >= *offset))
649 		*stat = (u32)(new_data - *offset);
650 	else
651 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
652 }
653 
654 /**
655  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
656  * @hw: ptr to the hardware info
657  * @reg: the hw reg to read and clear
658  * @stat: ptr to the stat
659  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)660 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
661 {
662 	u32 new_data = rd32(hw, reg);
663 
664 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
665 	*stat += new_data;
666 }
667 
668 /**
669  * i40e_stats_update_rx_discards - update rx_discards.
670  * @vsi: ptr to the VSI to be updated.
671  * @hw: ptr to the hardware info.
672  * @stat_idx: VSI's stat_counter_idx.
673  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
674  * @stat_offset: ptr to stat_offset to store first read of specific register.
675  * @stat: ptr to VSI's stat to be updated.
676  **/
677 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)678 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
679 			      int stat_idx, bool offset_loaded,
680 			      struct i40e_eth_stats *stat_offset,
681 			      struct i40e_eth_stats *stat)
682 {
683 	u64 rx_rdpc, rx_rxerr;
684 
685 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
686 			   &stat_offset->rx_discards, &rx_rdpc);
687 	i40e_stat_update64(hw,
688 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
689 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
690 			   offset_loaded, &stat_offset->rx_discards_other,
691 			   &rx_rxerr);
692 
693 	stat->rx_discards = rx_rdpc + rx_rxerr;
694 }
695 
696 /**
697  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
698  * @vsi: the VSI to be updated
699  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)700 void i40e_update_eth_stats(struct i40e_vsi *vsi)
701 {
702 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
703 	struct i40e_pf *pf = vsi->back;
704 	struct i40e_hw *hw = &pf->hw;
705 	struct i40e_eth_stats *oes;
706 	struct i40e_eth_stats *es;     /* device's eth stats */
707 
708 	es = &vsi->eth_stats;
709 	oes = &vsi->eth_stats_offsets;
710 
711 	/* Gather up the stats that the hw collects */
712 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
713 			   vsi->stat_offsets_loaded,
714 			   &oes->tx_errors, &es->tx_errors);
715 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
716 			   vsi->stat_offsets_loaded,
717 			   &oes->rx_discards, &es->rx_discards);
718 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
719 			   vsi->stat_offsets_loaded,
720 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
721 
722 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
723 			   I40E_GLV_GORCL(stat_idx),
724 			   vsi->stat_offsets_loaded,
725 			   &oes->rx_bytes, &es->rx_bytes);
726 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
727 			   I40E_GLV_UPRCL(stat_idx),
728 			   vsi->stat_offsets_loaded,
729 			   &oes->rx_unicast, &es->rx_unicast);
730 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
731 			   I40E_GLV_MPRCL(stat_idx),
732 			   vsi->stat_offsets_loaded,
733 			   &oes->rx_multicast, &es->rx_multicast);
734 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
735 			   I40E_GLV_BPRCL(stat_idx),
736 			   vsi->stat_offsets_loaded,
737 			   &oes->rx_broadcast, &es->rx_broadcast);
738 
739 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
740 			   I40E_GLV_GOTCL(stat_idx),
741 			   vsi->stat_offsets_loaded,
742 			   &oes->tx_bytes, &es->tx_bytes);
743 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
744 			   I40E_GLV_UPTCL(stat_idx),
745 			   vsi->stat_offsets_loaded,
746 			   &oes->tx_unicast, &es->tx_unicast);
747 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
748 			   I40E_GLV_MPTCL(stat_idx),
749 			   vsi->stat_offsets_loaded,
750 			   &oes->tx_multicast, &es->tx_multicast);
751 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
752 			   I40E_GLV_BPTCL(stat_idx),
753 			   vsi->stat_offsets_loaded,
754 			   &oes->tx_broadcast, &es->tx_broadcast);
755 
756 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
757 				      vsi->stat_offsets_loaded, oes, es);
758 
759 	vsi->stat_offsets_loaded = true;
760 }
761 
762 /**
763  * i40e_update_veb_stats - Update Switch component statistics
764  * @veb: the VEB being updated
765  **/
i40e_update_veb_stats(struct i40e_veb * veb)766 void i40e_update_veb_stats(struct i40e_veb *veb)
767 {
768 	struct i40e_pf *pf = veb->pf;
769 	struct i40e_hw *hw = &pf->hw;
770 	struct i40e_eth_stats *oes;
771 	struct i40e_eth_stats *es;     /* device's eth stats */
772 	struct i40e_veb_tc_stats *veb_oes;
773 	struct i40e_veb_tc_stats *veb_es;
774 	int i, idx = 0;
775 
776 	idx = veb->stats_idx;
777 	es = &veb->stats;
778 	oes = &veb->stats_offsets;
779 	veb_es = &veb->tc_stats;
780 	veb_oes = &veb->tc_stats_offsets;
781 
782 	/* Gather up the stats that the hw collects */
783 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
784 			   veb->stat_offsets_loaded,
785 			   &oes->tx_discards, &es->tx_discards);
786 	if (hw->revision_id > 0)
787 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
788 				   veb->stat_offsets_loaded,
789 				   &oes->rx_unknown_protocol,
790 				   &es->rx_unknown_protocol);
791 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
792 			   veb->stat_offsets_loaded,
793 			   &oes->rx_bytes, &es->rx_bytes);
794 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
795 			   veb->stat_offsets_loaded,
796 			   &oes->rx_unicast, &es->rx_unicast);
797 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->rx_multicast, &es->rx_multicast);
800 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
801 			   veb->stat_offsets_loaded,
802 			   &oes->rx_broadcast, &es->rx_broadcast);
803 
804 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
805 			   veb->stat_offsets_loaded,
806 			   &oes->tx_bytes, &es->tx_bytes);
807 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
808 			   veb->stat_offsets_loaded,
809 			   &oes->tx_unicast, &es->tx_unicast);
810 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
811 			   veb->stat_offsets_loaded,
812 			   &oes->tx_multicast, &es->tx_multicast);
813 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
814 			   veb->stat_offsets_loaded,
815 			   &oes->tx_broadcast, &es->tx_broadcast);
816 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
817 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
818 				   I40E_GLVEBTC_RPCL(i, idx),
819 				   veb->stat_offsets_loaded,
820 				   &veb_oes->tc_rx_packets[i],
821 				   &veb_es->tc_rx_packets[i]);
822 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
823 				   I40E_GLVEBTC_RBCL(i, idx),
824 				   veb->stat_offsets_loaded,
825 				   &veb_oes->tc_rx_bytes[i],
826 				   &veb_es->tc_rx_bytes[i]);
827 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
828 				   I40E_GLVEBTC_TPCL(i, idx),
829 				   veb->stat_offsets_loaded,
830 				   &veb_oes->tc_tx_packets[i],
831 				   &veb_es->tc_tx_packets[i]);
832 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
833 				   I40E_GLVEBTC_TBCL(i, idx),
834 				   veb->stat_offsets_loaded,
835 				   &veb_oes->tc_tx_bytes[i],
836 				   &veb_es->tc_tx_bytes[i]);
837 	}
838 	veb->stat_offsets_loaded = true;
839 }
840 
841 /**
842  * i40e_update_vsi_stats - Update the vsi statistics counters.
843  * @vsi: the VSI to be updated
844  *
845  * There are a few instances where we store the same stat in a
846  * couple of different structs.  This is partly because we have
847  * the netdev stats that need to be filled out, which is slightly
848  * different from the "eth_stats" defined by the chip and used in
849  * VF communications.  We sort it out here.
850  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)851 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
852 {
853 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
854 	struct i40e_pf *pf = vsi->back;
855 	struct rtnl_link_stats64 *ons;
856 	struct rtnl_link_stats64 *ns;   /* netdev stats */
857 	struct i40e_eth_stats *oes;
858 	struct i40e_eth_stats *es;     /* device's eth stats */
859 	u64 tx_restart, tx_busy;
860 	struct i40e_ring *p;
861 	u64 bytes, packets;
862 	unsigned int start;
863 	u64 tx_linearize;
864 	u64 tx_force_wb;
865 	u64 tx_stopped;
866 	u64 rx_p, rx_b;
867 	u64 tx_p, tx_b;
868 	u16 q;
869 
870 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
871 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
872 		return;
873 
874 	ns = i40e_get_vsi_stats_struct(vsi);
875 	ons = &vsi->net_stats_offsets;
876 	es = &vsi->eth_stats;
877 	oes = &vsi->eth_stats_offsets;
878 
879 	/* Gather up the netdev and vsi stats that the driver collects
880 	 * on the fly during packet processing
881 	 */
882 	rx_b = rx_p = 0;
883 	tx_b = tx_p = 0;
884 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
885 	tx_stopped = 0;
886 	rx_page = 0;
887 	rx_buf = 0;
888 	rx_reuse = 0;
889 	rx_alloc = 0;
890 	rx_waive = 0;
891 	rx_busy = 0;
892 	rcu_read_lock();
893 	for (q = 0; q < vsi->num_queue_pairs; q++) {
894 		/* locate Tx ring */
895 		p = READ_ONCE(vsi->tx_rings[q]);
896 		if (!p)
897 			continue;
898 
899 		do {
900 			start = u64_stats_fetch_begin_irq(&p->syncp);
901 			packets = p->stats.packets;
902 			bytes = p->stats.bytes;
903 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
904 		tx_b += bytes;
905 		tx_p += packets;
906 		tx_restart += p->tx_stats.restart_queue;
907 		tx_busy += p->tx_stats.tx_busy;
908 		tx_linearize += p->tx_stats.tx_linearize;
909 		tx_force_wb += p->tx_stats.tx_force_wb;
910 		tx_stopped += p->tx_stats.tx_stopped;
911 
912 		/* locate Rx ring */
913 		p = READ_ONCE(vsi->rx_rings[q]);
914 		if (!p)
915 			continue;
916 
917 		do {
918 			start = u64_stats_fetch_begin_irq(&p->syncp);
919 			packets = p->stats.packets;
920 			bytes = p->stats.bytes;
921 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
922 		rx_b += bytes;
923 		rx_p += packets;
924 		rx_buf += p->rx_stats.alloc_buff_failed;
925 		rx_page += p->rx_stats.alloc_page_failed;
926 		rx_reuse += p->rx_stats.page_reuse_count;
927 		rx_alloc += p->rx_stats.page_alloc_count;
928 		rx_waive += p->rx_stats.page_waive_count;
929 		rx_busy += p->rx_stats.page_busy_count;
930 
931 		if (i40e_enabled_xdp_vsi(vsi)) {
932 			/* locate XDP ring */
933 			p = READ_ONCE(vsi->xdp_rings[q]);
934 			if (!p)
935 				continue;
936 
937 			do {
938 				start = u64_stats_fetch_begin_irq(&p->syncp);
939 				packets = p->stats.packets;
940 				bytes = p->stats.bytes;
941 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
942 			tx_b += bytes;
943 			tx_p += packets;
944 			tx_restart += p->tx_stats.restart_queue;
945 			tx_busy += p->tx_stats.tx_busy;
946 			tx_linearize += p->tx_stats.tx_linearize;
947 			tx_force_wb += p->tx_stats.tx_force_wb;
948 		}
949 	}
950 	rcu_read_unlock();
951 	vsi->tx_restart = tx_restart;
952 	vsi->tx_busy = tx_busy;
953 	vsi->tx_linearize = tx_linearize;
954 	vsi->tx_force_wb = tx_force_wb;
955 	vsi->tx_stopped = tx_stopped;
956 	vsi->rx_page_failed = rx_page;
957 	vsi->rx_buf_failed = rx_buf;
958 	vsi->rx_page_reuse = rx_reuse;
959 	vsi->rx_page_alloc = rx_alloc;
960 	vsi->rx_page_waive = rx_waive;
961 	vsi->rx_page_busy = rx_busy;
962 
963 	ns->rx_packets = rx_p;
964 	ns->rx_bytes = rx_b;
965 	ns->tx_packets = tx_p;
966 	ns->tx_bytes = tx_b;
967 
968 	/* update netdev stats from eth stats */
969 	i40e_update_eth_stats(vsi);
970 	ons->tx_errors = oes->tx_errors;
971 	ns->tx_errors = es->tx_errors;
972 	ons->multicast = oes->rx_multicast;
973 	ns->multicast = es->rx_multicast;
974 	ons->rx_dropped = oes->rx_discards;
975 	ns->rx_dropped = es->rx_discards;
976 	ons->tx_dropped = oes->tx_discards;
977 	ns->tx_dropped = es->tx_discards;
978 
979 	/* pull in a couple PF stats if this is the main vsi */
980 	if (vsi == pf->vsi[pf->lan_vsi]) {
981 		ns->rx_crc_errors = pf->stats.crc_errors;
982 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
983 		ns->rx_length_errors = pf->stats.rx_length_errors;
984 	}
985 }
986 
987 /**
988  * i40e_update_pf_stats - Update the PF statistics counters.
989  * @pf: the PF to be updated
990  **/
i40e_update_pf_stats(struct i40e_pf * pf)991 static void i40e_update_pf_stats(struct i40e_pf *pf)
992 {
993 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
994 	struct i40e_hw_port_stats *nsd = &pf->stats;
995 	struct i40e_hw *hw = &pf->hw;
996 	u32 val;
997 	int i;
998 
999 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1000 			   I40E_GLPRT_GORCL(hw->port),
1001 			   pf->stat_offsets_loaded,
1002 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1003 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1004 			   I40E_GLPRT_GOTCL(hw->port),
1005 			   pf->stat_offsets_loaded,
1006 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1007 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1008 			   pf->stat_offsets_loaded,
1009 			   &osd->eth.rx_discards,
1010 			   &nsd->eth.rx_discards);
1011 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1012 			   I40E_GLPRT_UPRCL(hw->port),
1013 			   pf->stat_offsets_loaded,
1014 			   &osd->eth.rx_unicast,
1015 			   &nsd->eth.rx_unicast);
1016 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1017 			   I40E_GLPRT_MPRCL(hw->port),
1018 			   pf->stat_offsets_loaded,
1019 			   &osd->eth.rx_multicast,
1020 			   &nsd->eth.rx_multicast);
1021 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1022 			   I40E_GLPRT_BPRCL(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->eth.rx_broadcast,
1025 			   &nsd->eth.rx_broadcast);
1026 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1027 			   I40E_GLPRT_UPTCL(hw->port),
1028 			   pf->stat_offsets_loaded,
1029 			   &osd->eth.tx_unicast,
1030 			   &nsd->eth.tx_unicast);
1031 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1032 			   I40E_GLPRT_MPTCL(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->eth.tx_multicast,
1035 			   &nsd->eth.tx_multicast);
1036 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1037 			   I40E_GLPRT_BPTCL(hw->port),
1038 			   pf->stat_offsets_loaded,
1039 			   &osd->eth.tx_broadcast,
1040 			   &nsd->eth.tx_broadcast);
1041 
1042 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->tx_dropped_link_down,
1045 			   &nsd->tx_dropped_link_down);
1046 
1047 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->crc_errors, &nsd->crc_errors);
1050 
1051 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1054 
1055 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->mac_local_faults,
1058 			   &nsd->mac_local_faults);
1059 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->mac_remote_faults,
1062 			   &nsd->mac_remote_faults);
1063 
1064 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1065 			   pf->stat_offsets_loaded,
1066 			   &osd->rx_length_errors,
1067 			   &nsd->rx_length_errors);
1068 
1069 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1072 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1075 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1078 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1081 
1082 	for (i = 0; i < 8; i++) {
1083 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1084 				   pf->stat_offsets_loaded,
1085 				   &osd->priority_xoff_rx[i],
1086 				   &nsd->priority_xoff_rx[i]);
1087 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1088 				   pf->stat_offsets_loaded,
1089 				   &osd->priority_xon_rx[i],
1090 				   &nsd->priority_xon_rx[i]);
1091 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1092 				   pf->stat_offsets_loaded,
1093 				   &osd->priority_xon_tx[i],
1094 				   &nsd->priority_xon_tx[i]);
1095 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1096 				   pf->stat_offsets_loaded,
1097 				   &osd->priority_xoff_tx[i],
1098 				   &nsd->priority_xoff_tx[i]);
1099 		i40e_stat_update32(hw,
1100 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1101 				   pf->stat_offsets_loaded,
1102 				   &osd->priority_xon_2_xoff[i],
1103 				   &nsd->priority_xon_2_xoff[i]);
1104 	}
1105 
1106 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1107 			   I40E_GLPRT_PRC64L(hw->port),
1108 			   pf->stat_offsets_loaded,
1109 			   &osd->rx_size_64, &nsd->rx_size_64);
1110 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1111 			   I40E_GLPRT_PRC127L(hw->port),
1112 			   pf->stat_offsets_loaded,
1113 			   &osd->rx_size_127, &nsd->rx_size_127);
1114 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1115 			   I40E_GLPRT_PRC255L(hw->port),
1116 			   pf->stat_offsets_loaded,
1117 			   &osd->rx_size_255, &nsd->rx_size_255);
1118 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1119 			   I40E_GLPRT_PRC511L(hw->port),
1120 			   pf->stat_offsets_loaded,
1121 			   &osd->rx_size_511, &nsd->rx_size_511);
1122 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1123 			   I40E_GLPRT_PRC1023L(hw->port),
1124 			   pf->stat_offsets_loaded,
1125 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1127 			   I40E_GLPRT_PRC1522L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1131 			   I40E_GLPRT_PRC9522L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_big, &nsd->rx_size_big);
1134 
1135 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1136 			   I40E_GLPRT_PTC64L(hw->port),
1137 			   pf->stat_offsets_loaded,
1138 			   &osd->tx_size_64, &nsd->tx_size_64);
1139 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1140 			   I40E_GLPRT_PTC127L(hw->port),
1141 			   pf->stat_offsets_loaded,
1142 			   &osd->tx_size_127, &nsd->tx_size_127);
1143 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1144 			   I40E_GLPRT_PTC255L(hw->port),
1145 			   pf->stat_offsets_loaded,
1146 			   &osd->tx_size_255, &nsd->tx_size_255);
1147 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1148 			   I40E_GLPRT_PTC511L(hw->port),
1149 			   pf->stat_offsets_loaded,
1150 			   &osd->tx_size_511, &nsd->tx_size_511);
1151 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1152 			   I40E_GLPRT_PTC1023L(hw->port),
1153 			   pf->stat_offsets_loaded,
1154 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1156 			   I40E_GLPRT_PTC1522L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1160 			   I40E_GLPRT_PTC9522L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_big, &nsd->tx_size_big);
1163 
1164 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->rx_undersize, &nsd->rx_undersize);
1167 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1168 			   pf->stat_offsets_loaded,
1169 			   &osd->rx_fragments, &nsd->rx_fragments);
1170 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1171 			   pf->stat_offsets_loaded,
1172 			   &osd->rx_oversize, &nsd->rx_oversize);
1173 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1174 			   pf->stat_offsets_loaded,
1175 			   &osd->rx_jabber, &nsd->rx_jabber);
1176 
1177 	/* FDIR stats */
1178 	i40e_stat_update_and_clear32(hw,
1179 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1180 			&nsd->fd_atr_match);
1181 	i40e_stat_update_and_clear32(hw,
1182 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1183 			&nsd->fd_sb_match);
1184 	i40e_stat_update_and_clear32(hw,
1185 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1186 			&nsd->fd_atr_tunnel_match);
1187 
1188 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1189 	nsd->tx_lpi_status =
1190 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1191 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1192 	nsd->rx_lpi_status =
1193 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1194 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1195 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1196 			   pf->stat_offsets_loaded,
1197 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1198 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1199 			   pf->stat_offsets_loaded,
1200 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1201 
1202 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1203 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1204 		nsd->fd_sb_status = true;
1205 	else
1206 		nsd->fd_sb_status = false;
1207 
1208 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1209 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1210 		nsd->fd_atr_status = true;
1211 	else
1212 		nsd->fd_atr_status = false;
1213 
1214 	pf->stat_offsets_loaded = true;
1215 }
1216 
1217 /**
1218  * i40e_update_stats - Update the various statistics counters.
1219  * @vsi: the VSI to be updated
1220  *
1221  * Update the various stats for this VSI and its related entities.
1222  **/
i40e_update_stats(struct i40e_vsi * vsi)1223 void i40e_update_stats(struct i40e_vsi *vsi)
1224 {
1225 	struct i40e_pf *pf = vsi->back;
1226 
1227 	if (vsi == pf->vsi[pf->lan_vsi])
1228 		i40e_update_pf_stats(pf);
1229 
1230 	i40e_update_vsi_stats(vsi);
1231 }
1232 
1233 /**
1234  * i40e_count_filters - counts VSI mac filters
1235  * @vsi: the VSI to be searched
1236  *
1237  * Returns count of mac filters
1238  **/
i40e_count_filters(struct i40e_vsi * vsi)1239 int i40e_count_filters(struct i40e_vsi *vsi)
1240 {
1241 	struct i40e_mac_filter *f;
1242 	struct hlist_node *h;
1243 	int bkt;
1244 	int cnt = 0;
1245 
1246 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1247 		++cnt;
1248 
1249 	return cnt;
1250 }
1251 
1252 /**
1253  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1254  * @vsi: the VSI to be searched
1255  * @macaddr: the MAC address
1256  * @vlan: the vlan
1257  *
1258  * Returns ptr to the filter object or NULL
1259  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1260 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1261 						const u8 *macaddr, s16 vlan)
1262 {
1263 	struct i40e_mac_filter *f;
1264 	u64 key;
1265 
1266 	if (!vsi || !macaddr)
1267 		return NULL;
1268 
1269 	key = i40e_addr_to_hkey(macaddr);
1270 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1271 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1272 		    (vlan == f->vlan))
1273 			return f;
1274 	}
1275 	return NULL;
1276 }
1277 
1278 /**
1279  * i40e_find_mac - Find a mac addr in the macvlan filters list
1280  * @vsi: the VSI to be searched
1281  * @macaddr: the MAC address we are searching for
1282  *
1283  * Returns the first filter with the provided MAC address or NULL if
1284  * MAC address was not found
1285  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1286 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1287 {
1288 	struct i40e_mac_filter *f;
1289 	u64 key;
1290 
1291 	if (!vsi || !macaddr)
1292 		return NULL;
1293 
1294 	key = i40e_addr_to_hkey(macaddr);
1295 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1296 		if ((ether_addr_equal(macaddr, f->macaddr)))
1297 			return f;
1298 	}
1299 	return NULL;
1300 }
1301 
1302 /**
1303  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1304  * @vsi: the VSI to be searched
1305  *
1306  * Returns true if VSI is in vlan mode or false otherwise
1307  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1308 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1309 {
1310 	/* If we have a PVID, always operate in VLAN mode */
1311 	if (vsi->info.pvid)
1312 		return true;
1313 
1314 	/* We need to operate in VLAN mode whenever we have any filters with
1315 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1316 	 * time, incurring search cost repeatedly. However, we can notice two
1317 	 * things:
1318 	 *
1319 	 * 1) the only place where we can gain a VLAN filter is in
1320 	 *    i40e_add_filter.
1321 	 *
1322 	 * 2) the only place where filters are actually removed is in
1323 	 *    i40e_sync_filters_subtask.
1324 	 *
1325 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1326 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1327 	 * we have to perform the full search after deleting filters in
1328 	 * i40e_sync_filters_subtask, but we already have to search
1329 	 * filters here and can perform the check at the same time. This
1330 	 * results in avoiding embedding a loop for VLAN mode inside another
1331 	 * loop over all the filters, and should maintain correctness as noted
1332 	 * above.
1333 	 */
1334 	return vsi->has_vlan_filter;
1335 }
1336 
1337 /**
1338  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1339  * @vsi: the VSI to configure
1340  * @tmp_add_list: list of filters ready to be added
1341  * @tmp_del_list: list of filters ready to be deleted
1342  * @vlan_filters: the number of active VLAN filters
1343  *
1344  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1345  * behave as expected. If we have any active VLAN filters remaining or about
1346  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1347  * so that they only match against untagged traffic. If we no longer have any
1348  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1349  * so that they match against both tagged and untagged traffic. In this way,
1350  * we ensure that we correctly receive the desired traffic. This ensures that
1351  * when we have an active VLAN we will receive only untagged traffic and
1352  * traffic matching active VLANs. If we have no active VLANs then we will
1353  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1354  *
1355  * Finally, in a similar fashion, this function also corrects filters when
1356  * there is an active PVID assigned to this VSI.
1357  *
1358  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1359  *
1360  * This function is only expected to be called from within
1361  * i40e_sync_vsi_filters.
1362  *
1363  * NOTE: This function expects to be called while under the
1364  * mac_filter_hash_lock
1365  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1366 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1367 					 struct hlist_head *tmp_add_list,
1368 					 struct hlist_head *tmp_del_list,
1369 					 int vlan_filters)
1370 {
1371 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1372 	struct i40e_mac_filter *f, *add_head;
1373 	struct i40e_new_mac_filter *new;
1374 	struct hlist_node *h;
1375 	int bkt, new_vlan;
1376 
1377 	/* To determine if a particular filter needs to be replaced we
1378 	 * have the three following conditions:
1379 	 *
1380 	 * a) if we have a PVID assigned, then all filters which are
1381 	 *    not marked as VLAN=PVID must be replaced with filters that
1382 	 *    are.
1383 	 * b) otherwise, if we have any active VLANS, all filters
1384 	 *    which are marked as VLAN=-1 must be replaced with
1385 	 *    filters marked as VLAN=0
1386 	 * c) finally, if we do not have any active VLANS, all filters
1387 	 *    which are marked as VLAN=0 must be replaced with filters
1388 	 *    marked as VLAN=-1
1389 	 */
1390 
1391 	/* Update the filters about to be added in place */
1392 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1393 		if (pvid && new->f->vlan != pvid)
1394 			new->f->vlan = pvid;
1395 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1396 			new->f->vlan = 0;
1397 		else if (!vlan_filters && new->f->vlan == 0)
1398 			new->f->vlan = I40E_VLAN_ANY;
1399 	}
1400 
1401 	/* Update the remaining active filters */
1402 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1403 		/* Combine the checks for whether a filter needs to be changed
1404 		 * and then determine the new VLAN inside the if block, in
1405 		 * order to avoid duplicating code for adding the new filter
1406 		 * then deleting the old filter.
1407 		 */
1408 		if ((pvid && f->vlan != pvid) ||
1409 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1410 		    (!vlan_filters && f->vlan == 0)) {
1411 			/* Determine the new vlan we will be adding */
1412 			if (pvid)
1413 				new_vlan = pvid;
1414 			else if (vlan_filters)
1415 				new_vlan = 0;
1416 			else
1417 				new_vlan = I40E_VLAN_ANY;
1418 
1419 			/* Create the new filter */
1420 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1421 			if (!add_head)
1422 				return -ENOMEM;
1423 
1424 			/* Create a temporary i40e_new_mac_filter */
1425 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1426 			if (!new)
1427 				return -ENOMEM;
1428 
1429 			new->f = add_head;
1430 			new->state = add_head->state;
1431 
1432 			/* Add the new filter to the tmp list */
1433 			hlist_add_head(&new->hlist, tmp_add_list);
1434 
1435 			/* Put the original filter into the delete list */
1436 			f->state = I40E_FILTER_REMOVE;
1437 			hash_del(&f->hlist);
1438 			hlist_add_head(&f->hlist, tmp_del_list);
1439 		}
1440 	}
1441 
1442 	vsi->has_vlan_filter = !!vlan_filters;
1443 
1444 	return 0;
1445 }
1446 
1447 /**
1448  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1449  * @vsi: the vsi to configure
1450  * @new_mac: new mac filter to be added
1451  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1452  * @vlan_filters: the number of active VLAN filters
1453  * @trusted: flag if the VF is trusted
1454  *
1455  * Get new VLAN id based on current VLAN filters, trust, PVID
1456  * and vf-vlan-prune-disable flag.
1457  *
1458  * Returns the value of the new vlan filter or
1459  * the old value if no new filter is needed.
1460  */
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)1461 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1462 				struct i40e_new_mac_filter *new_mac,
1463 				struct i40e_mac_filter *f,
1464 				int vlan_filters,
1465 				bool trusted)
1466 {
1467 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1468 	struct i40e_pf *pf = vsi->back;
1469 	bool is_any;
1470 
1471 	if (new_mac)
1472 		f = new_mac->f;
1473 
1474 	if (pvid && f->vlan != pvid)
1475 		return pvid;
1476 
1477 	is_any = (trusted ||
1478 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1479 
1480 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1481 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1482 	    (is_any && !vlan_filters && f->vlan == 0)) {
1483 		if (is_any)
1484 			return I40E_VLAN_ANY;
1485 		else
1486 			return 0;
1487 	}
1488 
1489 	return f->vlan;
1490 }
1491 
1492 /**
1493  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1494  * @vsi: the vsi to configure
1495  * @tmp_add_list: list of filters ready to be added
1496  * @tmp_del_list: list of filters ready to be deleted
1497  * @vlan_filters: the number of active VLAN filters
1498  * @trusted: flag if the VF is trusted
1499  *
1500  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1501  * and vf-vlan-prune-disable flag.
1502  *
1503  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1504  *
1505  * This function is only expected to be called from within
1506  * i40e_sync_vsi_filters.
1507  *
1508  * NOTE: This function expects to be called while under the
1509  * mac_filter_hash_lock
1510  */
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)1511 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1512 					    struct hlist_head *tmp_add_list,
1513 					    struct hlist_head *tmp_del_list,
1514 					    int vlan_filters,
1515 					    bool trusted)
1516 {
1517 	struct i40e_mac_filter *f, *add_head;
1518 	struct i40e_new_mac_filter *new_mac;
1519 	struct hlist_node *h;
1520 	int bkt, new_vlan;
1521 
1522 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1523 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1524 							vlan_filters, trusted);
1525 	}
1526 
1527 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1528 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1529 						trusted);
1530 		if (new_vlan != f->vlan) {
1531 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1532 			if (!add_head)
1533 				return -ENOMEM;
1534 			/* Create a temporary i40e_new_mac_filter */
1535 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1536 			if (!new_mac)
1537 				return -ENOMEM;
1538 			new_mac->f = add_head;
1539 			new_mac->state = add_head->state;
1540 
1541 			/* Add the new filter to the tmp list */
1542 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1543 
1544 			/* Put the original filter into the delete list */
1545 			f->state = I40E_FILTER_REMOVE;
1546 			hash_del(&f->hlist);
1547 			hlist_add_head(&f->hlist, tmp_del_list);
1548 		}
1549 	}
1550 
1551 	vsi->has_vlan_filter = !!vlan_filters;
1552 	return 0;
1553 }
1554 
1555 /**
1556  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1557  * @vsi: the PF Main VSI - inappropriate for any other VSI
1558  * @macaddr: the MAC address
1559  *
1560  * Remove whatever filter the firmware set up so the driver can manage
1561  * its own filtering intelligently.
1562  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1563 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1564 {
1565 	struct i40e_aqc_remove_macvlan_element_data element;
1566 	struct i40e_pf *pf = vsi->back;
1567 
1568 	/* Only appropriate for the PF main VSI */
1569 	if (vsi->type != I40E_VSI_MAIN)
1570 		return;
1571 
1572 	memset(&element, 0, sizeof(element));
1573 	ether_addr_copy(element.mac_addr, macaddr);
1574 	element.vlan_tag = 0;
1575 	/* Ignore error returns, some firmware does it this way... */
1576 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1577 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1578 
1579 	memset(&element, 0, sizeof(element));
1580 	ether_addr_copy(element.mac_addr, macaddr);
1581 	element.vlan_tag = 0;
1582 	/* ...and some firmware does it this way. */
1583 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1584 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1585 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1586 }
1587 
1588 /**
1589  * i40e_add_filter - Add a mac/vlan filter to the VSI
1590  * @vsi: the VSI to be searched
1591  * @macaddr: the MAC address
1592  * @vlan: the vlan
1593  *
1594  * Returns ptr to the filter object or NULL when no memory available.
1595  *
1596  * NOTE: This function is expected to be called with mac_filter_hash_lock
1597  * being held.
1598  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1599 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1600 					const u8 *macaddr, s16 vlan)
1601 {
1602 	struct i40e_mac_filter *f;
1603 	u64 key;
1604 
1605 	if (!vsi || !macaddr)
1606 		return NULL;
1607 
1608 	f = i40e_find_filter(vsi, macaddr, vlan);
1609 	if (!f) {
1610 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1611 		if (!f)
1612 			return NULL;
1613 
1614 		/* Update the boolean indicating if we need to function in
1615 		 * VLAN mode.
1616 		 */
1617 		if (vlan >= 0)
1618 			vsi->has_vlan_filter = true;
1619 
1620 		ether_addr_copy(f->macaddr, macaddr);
1621 		f->vlan = vlan;
1622 		f->state = I40E_FILTER_NEW;
1623 		INIT_HLIST_NODE(&f->hlist);
1624 
1625 		key = i40e_addr_to_hkey(macaddr);
1626 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1627 
1628 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1629 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1630 	}
1631 
1632 	/* If we're asked to add a filter that has been marked for removal, it
1633 	 * is safe to simply restore it to active state. __i40e_del_filter
1634 	 * will have simply deleted any filters which were previously marked
1635 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1636 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1637 	 * task, just restore this filter to the ACTIVE state so that the
1638 	 * sync task leaves it in place
1639 	 */
1640 	if (f->state == I40E_FILTER_REMOVE)
1641 		f->state = I40E_FILTER_ACTIVE;
1642 
1643 	return f;
1644 }
1645 
1646 /**
1647  * __i40e_del_filter - Remove a specific filter from the VSI
1648  * @vsi: VSI to remove from
1649  * @f: the filter to remove from the list
1650  *
1651  * This function should be called instead of i40e_del_filter only if you know
1652  * the exact filter you will remove already, such as via i40e_find_filter or
1653  * i40e_find_mac.
1654  *
1655  * NOTE: This function is expected to be called with mac_filter_hash_lock
1656  * being held.
1657  * ANOTHER NOTE: This function MUST be called from within the context of
1658  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1659  * instead of list_for_each_entry().
1660  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1661 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1662 {
1663 	if (!f)
1664 		return;
1665 
1666 	/* If the filter was never added to firmware then we can just delete it
1667 	 * directly and we don't want to set the status to remove or else an
1668 	 * admin queue command will unnecessarily fire.
1669 	 */
1670 	if ((f->state == I40E_FILTER_FAILED) ||
1671 	    (f->state == I40E_FILTER_NEW)) {
1672 		hash_del(&f->hlist);
1673 		kfree(f);
1674 	} else {
1675 		f->state = I40E_FILTER_REMOVE;
1676 	}
1677 
1678 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1679 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1680 }
1681 
1682 /**
1683  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1684  * @vsi: the VSI to be searched
1685  * @macaddr: the MAC address
1686  * @vlan: the VLAN
1687  *
1688  * NOTE: This function is expected to be called with mac_filter_hash_lock
1689  * being held.
1690  * ANOTHER NOTE: This function MUST be called from within the context of
1691  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1692  * instead of list_for_each_entry().
1693  **/
i40e_del_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1694 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1695 {
1696 	struct i40e_mac_filter *f;
1697 
1698 	if (!vsi || !macaddr)
1699 		return;
1700 
1701 	f = i40e_find_filter(vsi, macaddr, vlan);
1702 	__i40e_del_filter(vsi, f);
1703 }
1704 
1705 /**
1706  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1707  * @vsi: the VSI to be searched
1708  * @macaddr: the mac address to be filtered
1709  *
1710  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1711  * go through all the macvlan filters and add a macvlan filter for each
1712  * unique vlan that already exists. If a PVID has been assigned, instead only
1713  * add the macaddr to that VLAN.
1714  *
1715  * Returns last filter added on success, else NULL
1716  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1717 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1718 					    const u8 *macaddr)
1719 {
1720 	struct i40e_mac_filter *f, *add = NULL;
1721 	struct hlist_node *h;
1722 	int bkt;
1723 
1724 	if (vsi->info.pvid)
1725 		return i40e_add_filter(vsi, macaddr,
1726 				       le16_to_cpu(vsi->info.pvid));
1727 
1728 	if (!i40e_is_vsi_in_vlan(vsi))
1729 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1730 
1731 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1732 		if (f->state == I40E_FILTER_REMOVE)
1733 			continue;
1734 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1735 		if (!add)
1736 			return NULL;
1737 	}
1738 
1739 	return add;
1740 }
1741 
1742 /**
1743  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1744  * @vsi: the VSI to be searched
1745  * @macaddr: the mac address to be removed
1746  *
1747  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1748  * associated with.
1749  *
1750  * Returns 0 for success, or error
1751  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1752 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1753 {
1754 	struct i40e_mac_filter *f;
1755 	struct hlist_node *h;
1756 	bool found = false;
1757 	int bkt;
1758 
1759 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1760 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1761 		if (ether_addr_equal(macaddr, f->macaddr)) {
1762 			__i40e_del_filter(vsi, f);
1763 			found = true;
1764 		}
1765 	}
1766 
1767 	if (found)
1768 		return 0;
1769 	else
1770 		return -ENOENT;
1771 }
1772 
1773 /**
1774  * i40e_set_mac - NDO callback to set mac address
1775  * @netdev: network interface device structure
1776  * @p: pointer to an address structure
1777  *
1778  * Returns 0 on success, negative on failure
1779  **/
i40e_set_mac(struct net_device * netdev,void * p)1780 static int i40e_set_mac(struct net_device *netdev, void *p)
1781 {
1782 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1783 	struct i40e_vsi *vsi = np->vsi;
1784 	struct i40e_pf *pf = vsi->back;
1785 	struct i40e_hw *hw = &pf->hw;
1786 	struct sockaddr *addr = p;
1787 
1788 	if (!is_valid_ether_addr(addr->sa_data))
1789 		return -EADDRNOTAVAIL;
1790 
1791 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1792 		netdev_info(netdev, "already using mac address %pM\n",
1793 			    addr->sa_data);
1794 		return 0;
1795 	}
1796 
1797 	if (test_bit(__I40E_DOWN, pf->state) ||
1798 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1799 		return -EADDRNOTAVAIL;
1800 
1801 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1802 		netdev_info(netdev, "returning to hw mac address %pM\n",
1803 			    hw->mac.addr);
1804 	else
1805 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1806 
1807 	/* Copy the address first, so that we avoid a possible race with
1808 	 * .set_rx_mode().
1809 	 * - Remove old address from MAC filter
1810 	 * - Copy new address
1811 	 * - Add new address to MAC filter
1812 	 */
1813 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1814 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1815 	eth_hw_addr_set(netdev, addr->sa_data);
1816 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1817 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1818 
1819 	if (vsi->type == I40E_VSI_MAIN) {
1820 		i40e_status ret;
1821 
1822 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1823 						addr->sa_data, NULL);
1824 		if (ret)
1825 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1826 				    i40e_stat_str(hw, ret),
1827 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1828 	}
1829 
1830 	/* schedule our worker thread which will take care of
1831 	 * applying the new filter changes
1832 	 */
1833 	i40e_service_event_schedule(pf);
1834 	return 0;
1835 }
1836 
1837 /**
1838  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1839  * @vsi: vsi structure
1840  * @seed: RSS hash seed
1841  * @lut: pointer to lookup table of lut_size
1842  * @lut_size: size of the lookup table
1843  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1844 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1845 			      u8 *lut, u16 lut_size)
1846 {
1847 	struct i40e_pf *pf = vsi->back;
1848 	struct i40e_hw *hw = &pf->hw;
1849 	int ret = 0;
1850 
1851 	if (seed) {
1852 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1853 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1854 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1855 		if (ret) {
1856 			dev_info(&pf->pdev->dev,
1857 				 "Cannot set RSS key, err %s aq_err %s\n",
1858 				 i40e_stat_str(hw, ret),
1859 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1860 			return ret;
1861 		}
1862 	}
1863 	if (lut) {
1864 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1865 
1866 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1867 		if (ret) {
1868 			dev_info(&pf->pdev->dev,
1869 				 "Cannot set RSS lut, err %s aq_err %s\n",
1870 				 i40e_stat_str(hw, ret),
1871 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1872 			return ret;
1873 		}
1874 	}
1875 	return ret;
1876 }
1877 
1878 /**
1879  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1880  * @vsi: VSI structure
1881  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1882 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1883 {
1884 	struct i40e_pf *pf = vsi->back;
1885 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1886 	u8 *lut;
1887 	int ret;
1888 
1889 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1890 		return 0;
1891 	if (!vsi->rss_size)
1892 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1893 				      vsi->num_queue_pairs);
1894 	if (!vsi->rss_size)
1895 		return -EINVAL;
1896 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1897 	if (!lut)
1898 		return -ENOMEM;
1899 
1900 	/* Use the user configured hash keys and lookup table if there is one,
1901 	 * otherwise use default
1902 	 */
1903 	if (vsi->rss_lut_user)
1904 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1905 	else
1906 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1907 	if (vsi->rss_hkey_user)
1908 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1909 	else
1910 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1911 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1912 	kfree(lut);
1913 	return ret;
1914 }
1915 
1916 /**
1917  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1918  * @vsi: the VSI being configured,
1919  * @ctxt: VSI context structure
1920  * @enabled_tc: number of traffic classes to enable
1921  *
1922  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1923  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1924 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1925 					   struct i40e_vsi_context *ctxt,
1926 					   u8 enabled_tc)
1927 {
1928 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1929 	int i, override_q, pow, num_qps, ret;
1930 	u8 netdev_tc = 0, offset = 0;
1931 
1932 	if (vsi->type != I40E_VSI_MAIN)
1933 		return -EINVAL;
1934 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1935 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1936 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1937 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1938 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1939 
1940 	/* find the next higher power-of-2 of num queue pairs */
1941 	pow = ilog2(num_qps);
1942 	if (!is_power_of_2(num_qps))
1943 		pow++;
1944 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1945 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1946 
1947 	/* Setup queue offset/count for all TCs for given VSI */
1948 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1949 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1950 		/* See if the given TC is enabled for the given VSI */
1951 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1952 			offset = vsi->mqprio_qopt.qopt.offset[i];
1953 			qcount = vsi->mqprio_qopt.qopt.count[i];
1954 			if (qcount > max_qcount)
1955 				max_qcount = qcount;
1956 			vsi->tc_config.tc_info[i].qoffset = offset;
1957 			vsi->tc_config.tc_info[i].qcount = qcount;
1958 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1959 		} else {
1960 			/* TC is not enabled so set the offset to
1961 			 * default queue and allocate one queue
1962 			 * for the given TC.
1963 			 */
1964 			vsi->tc_config.tc_info[i].qoffset = 0;
1965 			vsi->tc_config.tc_info[i].qcount = 1;
1966 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1967 		}
1968 	}
1969 
1970 	/* Set actual Tx/Rx queue pairs */
1971 	vsi->num_queue_pairs = offset + qcount;
1972 
1973 	/* Setup queue TC[0].qmap for given VSI context */
1974 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1975 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1976 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1977 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1978 
1979 	/* Reconfigure RSS for main VSI with max queue count */
1980 	vsi->rss_size = max_qcount;
1981 	ret = i40e_vsi_config_rss(vsi);
1982 	if (ret) {
1983 		dev_info(&vsi->back->pdev->dev,
1984 			 "Failed to reconfig rss for num_queues (%u)\n",
1985 			 max_qcount);
1986 		return ret;
1987 	}
1988 	vsi->reconfig_rss = true;
1989 	dev_dbg(&vsi->back->pdev->dev,
1990 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1991 
1992 	/* Find queue count available for channel VSIs and starting offset
1993 	 * for channel VSIs
1994 	 */
1995 	override_q = vsi->mqprio_qopt.qopt.count[0];
1996 	if (override_q && override_q < vsi->num_queue_pairs) {
1997 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1998 		vsi->next_base_queue = override_q;
1999 	}
2000 	return 0;
2001 }
2002 
2003 /**
2004  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2005  * @vsi: the VSI being setup
2006  * @ctxt: VSI context structure
2007  * @enabled_tc: Enabled TCs bitmap
2008  * @is_add: True if called before Add VSI
2009  *
2010  * Setup VSI queue mapping for enabled traffic classes.
2011  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)2012 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2013 				     struct i40e_vsi_context *ctxt,
2014 				     u8 enabled_tc,
2015 				     bool is_add)
2016 {
2017 	struct i40e_pf *pf = vsi->back;
2018 	u16 num_tc_qps = 0;
2019 	u16 sections = 0;
2020 	u8 netdev_tc = 0;
2021 	u16 numtc = 1;
2022 	u16 qcount;
2023 	u8 offset;
2024 	u16 qmap;
2025 	int i;
2026 
2027 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2028 	offset = 0;
2029 	/* zero out queue mapping, it will get updated on the end of the function */
2030 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2031 
2032 	if (vsi->type == I40E_VSI_MAIN) {
2033 		/* This code helps add more queue to the VSI if we have
2034 		 * more cores than RSS can support, the higher cores will
2035 		 * be served by ATR or other filters. Furthermore, the
2036 		 * non-zero req_queue_pairs says that user requested a new
2037 		 * queue count via ethtool's set_channels, so use this
2038 		 * value for queues distribution across traffic classes
2039 		 * We need at least one queue pair for the interface
2040 		 * to be usable as we see in else statement.
2041 		 */
2042 		if (vsi->req_queue_pairs > 0)
2043 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2044 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2045 			vsi->num_queue_pairs = pf->num_lan_msix;
2046 		else
2047 			vsi->num_queue_pairs = 1;
2048 	}
2049 
2050 	/* Number of queues per enabled TC */
2051 	if (vsi->type == I40E_VSI_MAIN ||
2052 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2053 		num_tc_qps = vsi->num_queue_pairs;
2054 	else
2055 		num_tc_qps = vsi->alloc_queue_pairs;
2056 
2057 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2058 		/* Find numtc from enabled TC bitmap */
2059 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2060 			if (enabled_tc & BIT(i)) /* TC is enabled */
2061 				numtc++;
2062 		}
2063 		if (!numtc) {
2064 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2065 			numtc = 1;
2066 		}
2067 		num_tc_qps = num_tc_qps / numtc;
2068 		num_tc_qps = min_t(int, num_tc_qps,
2069 				   i40e_pf_get_max_q_per_tc(pf));
2070 	}
2071 
2072 	vsi->tc_config.numtc = numtc;
2073 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2074 
2075 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2076 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2077 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2078 
2079 	/* Setup queue offset/count for all TCs for given VSI */
2080 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2081 		/* See if the given TC is enabled for the given VSI */
2082 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2083 			/* TC is enabled */
2084 			int pow, num_qps;
2085 
2086 			switch (vsi->type) {
2087 			case I40E_VSI_MAIN:
2088 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2089 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2090 				    vsi->tc_config.enabled_tc != 1) {
2091 					qcount = min_t(int, pf->alloc_rss_size,
2092 						       num_tc_qps);
2093 					break;
2094 				}
2095 				fallthrough;
2096 			case I40E_VSI_FDIR:
2097 			case I40E_VSI_SRIOV:
2098 			case I40E_VSI_VMDQ2:
2099 			default:
2100 				qcount = num_tc_qps;
2101 				WARN_ON(i != 0);
2102 				break;
2103 			}
2104 			vsi->tc_config.tc_info[i].qoffset = offset;
2105 			vsi->tc_config.tc_info[i].qcount = qcount;
2106 
2107 			/* find the next higher power-of-2 of num queue pairs */
2108 			num_qps = qcount;
2109 			pow = 0;
2110 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2111 				pow++;
2112 				num_qps >>= 1;
2113 			}
2114 
2115 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2116 			qmap =
2117 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2118 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2119 
2120 			offset += qcount;
2121 		} else {
2122 			/* TC is not enabled so set the offset to
2123 			 * default queue and allocate one queue
2124 			 * for the given TC.
2125 			 */
2126 			vsi->tc_config.tc_info[i].qoffset = 0;
2127 			vsi->tc_config.tc_info[i].qcount = 1;
2128 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2129 
2130 			qmap = 0;
2131 		}
2132 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2133 	}
2134 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2135 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2136 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2137 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2138 		vsi->num_queue_pairs = offset;
2139 
2140 	/* Scheduler section valid can only be set for ADD VSI */
2141 	if (is_add) {
2142 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2143 
2144 		ctxt->info.up_enable_bits = enabled_tc;
2145 	}
2146 	if (vsi->type == I40E_VSI_SRIOV) {
2147 		ctxt->info.mapping_flags |=
2148 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2149 		for (i = 0; i < vsi->num_queue_pairs; i++)
2150 			ctxt->info.queue_mapping[i] =
2151 					       cpu_to_le16(vsi->base_queue + i);
2152 	} else {
2153 		ctxt->info.mapping_flags |=
2154 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2155 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2156 	}
2157 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2158 }
2159 
2160 /**
2161  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2162  * @netdev: the netdevice
2163  * @addr: address to add
2164  *
2165  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2166  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2167  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)2168 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2169 {
2170 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2171 	struct i40e_vsi *vsi = np->vsi;
2172 
2173 	if (i40e_add_mac_filter(vsi, addr))
2174 		return 0;
2175 	else
2176 		return -ENOMEM;
2177 }
2178 
2179 /**
2180  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2181  * @netdev: the netdevice
2182  * @addr: address to add
2183  *
2184  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2185  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2186  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)2187 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2188 {
2189 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2190 	struct i40e_vsi *vsi = np->vsi;
2191 
2192 	/* Under some circumstances, we might receive a request to delete
2193 	 * our own device address from our uc list. Because we store the
2194 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2195 	 * such requests and not delete our device address from this list.
2196 	 */
2197 	if (ether_addr_equal(addr, netdev->dev_addr))
2198 		return 0;
2199 
2200 	i40e_del_mac_filter(vsi, addr);
2201 
2202 	return 0;
2203 }
2204 
2205 /**
2206  * i40e_set_rx_mode - NDO callback to set the netdev filters
2207  * @netdev: network interface device structure
2208  **/
i40e_set_rx_mode(struct net_device * netdev)2209 static void i40e_set_rx_mode(struct net_device *netdev)
2210 {
2211 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2212 	struct i40e_vsi *vsi = np->vsi;
2213 
2214 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2215 
2216 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2217 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2218 
2219 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2220 
2221 	/* check for other flag changes */
2222 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2223 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2224 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2225 	}
2226 }
2227 
2228 /**
2229  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2230  * @vsi: Pointer to VSI struct
2231  * @from: Pointer to list which contains MAC filter entries - changes to
2232  *        those entries needs to be undone.
2233  *
2234  * MAC filter entries from this list were slated for deletion.
2235  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2236 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2237 					 struct hlist_head *from)
2238 {
2239 	struct i40e_mac_filter *f;
2240 	struct hlist_node *h;
2241 
2242 	hlist_for_each_entry_safe(f, h, from, hlist) {
2243 		u64 key = i40e_addr_to_hkey(f->macaddr);
2244 
2245 		/* Move the element back into MAC filter list*/
2246 		hlist_del(&f->hlist);
2247 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2248 	}
2249 }
2250 
2251 /**
2252  * i40e_undo_add_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 addition.
2258  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2259 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2260 					 struct hlist_head *from)
2261 {
2262 	struct i40e_new_mac_filter *new;
2263 	struct hlist_node *h;
2264 
2265 	hlist_for_each_entry_safe(new, h, from, hlist) {
2266 		/* We can simply free the wrapper structure */
2267 		hlist_del(&new->hlist);
2268 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2269 		kfree(new);
2270 	}
2271 }
2272 
2273 /**
2274  * i40e_next_filter - Get the next non-broadcast filter from a list
2275  * @next: pointer to filter in list
2276  *
2277  * Returns the next non-broadcast filter in the list. Required so that we
2278  * ignore broadcast filters within the list, since these are not handled via
2279  * the normal firmware update path.
2280  */
2281 static
i40e_next_filter(struct i40e_new_mac_filter * next)2282 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2283 {
2284 	hlist_for_each_entry_continue(next, hlist) {
2285 		if (!is_broadcast_ether_addr(next->f->macaddr))
2286 			return next;
2287 	}
2288 
2289 	return NULL;
2290 }
2291 
2292 /**
2293  * i40e_update_filter_state - Update filter state based on return data
2294  * from firmware
2295  * @count: Number of filters added
2296  * @add_list: return data from fw
2297  * @add_head: pointer to first filter in current batch
2298  *
2299  * MAC filter entries from list were slated to be added to device. Returns
2300  * number of successful filters. Note that 0 does NOT mean success!
2301  **/
2302 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2303 i40e_update_filter_state(int count,
2304 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2305 			 struct i40e_new_mac_filter *add_head)
2306 {
2307 	int retval = 0;
2308 	int i;
2309 
2310 	for (i = 0; i < count; i++) {
2311 		/* Always check status of each filter. We don't need to check
2312 		 * the firmware return status because we pre-set the filter
2313 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2314 		 * request to the adminq. Thus, if it no longer matches then
2315 		 * we know the filter is active.
2316 		 */
2317 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2318 			add_head->state = I40E_FILTER_FAILED;
2319 		} else {
2320 			add_head->state = I40E_FILTER_ACTIVE;
2321 			retval++;
2322 		}
2323 
2324 		add_head = i40e_next_filter(add_head);
2325 		if (!add_head)
2326 			break;
2327 	}
2328 
2329 	return retval;
2330 }
2331 
2332 /**
2333  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2334  * @vsi: ptr to the VSI
2335  * @vsi_name: name to display in messages
2336  * @list: the list of filters to send to firmware
2337  * @num_del: the number of filters to delete
2338  * @retval: Set to -EIO on failure to delete
2339  *
2340  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2341  * *retval instead of a return value so that success does not force ret_val to
2342  * be set to 0. This ensures that a sequence of calls to this function
2343  * preserve the previous value of *retval on successful delete.
2344  */
2345 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)2346 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2347 			  struct i40e_aqc_remove_macvlan_element_data *list,
2348 			  int num_del, int *retval)
2349 {
2350 	struct i40e_hw *hw = &vsi->back->hw;
2351 	enum i40e_admin_queue_err aq_status;
2352 	i40e_status aq_ret;
2353 
2354 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2355 					   &aq_status);
2356 
2357 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2358 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2359 		*retval = -EIO;
2360 		dev_info(&vsi->back->pdev->dev,
2361 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2362 			 vsi_name, i40e_stat_str(hw, aq_ret),
2363 			 i40e_aq_str(hw, aq_status));
2364 	}
2365 }
2366 
2367 /**
2368  * i40e_aqc_add_filters - Request firmware to add a set of filters
2369  * @vsi: ptr to the VSI
2370  * @vsi_name: name to display in messages
2371  * @list: the list of filters to send to firmware
2372  * @add_head: Position in the add hlist
2373  * @num_add: the number of filters to add
2374  *
2375  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2376  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2377  * space for more filters.
2378  */
2379 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)2380 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2381 			  struct i40e_aqc_add_macvlan_element_data *list,
2382 			  struct i40e_new_mac_filter *add_head,
2383 			  int num_add)
2384 {
2385 	struct i40e_hw *hw = &vsi->back->hw;
2386 	enum i40e_admin_queue_err aq_status;
2387 	int fcnt;
2388 
2389 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2390 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2391 
2392 	if (fcnt != num_add) {
2393 		if (vsi->type == I40E_VSI_MAIN) {
2394 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2395 			dev_warn(&vsi->back->pdev->dev,
2396 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2397 				 i40e_aq_str(hw, aq_status), vsi_name);
2398 		} else if (vsi->type == I40E_VSI_SRIOV ||
2399 			   vsi->type == I40E_VSI_VMDQ1 ||
2400 			   vsi->type == I40E_VSI_VMDQ2) {
2401 			dev_warn(&vsi->back->pdev->dev,
2402 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2403 				 i40e_aq_str(hw, aq_status), vsi_name,
2404 					     vsi_name);
2405 		} else {
2406 			dev_warn(&vsi->back->pdev->dev,
2407 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2408 				 i40e_aq_str(hw, aq_status), vsi_name,
2409 					     vsi->type);
2410 		}
2411 	}
2412 }
2413 
2414 /**
2415  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2416  * @vsi: pointer to the VSI
2417  * @vsi_name: the VSI name
2418  * @f: filter data
2419  *
2420  * This function sets or clears the promiscuous broadcast flags for VLAN
2421  * filters in order to properly receive broadcast frames. Assumes that only
2422  * broadcast filters are passed.
2423  *
2424  * Returns status indicating success or failure;
2425  **/
2426 static i40e_status
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2427 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2428 			  struct i40e_mac_filter *f)
2429 {
2430 	bool enable = f->state == I40E_FILTER_NEW;
2431 	struct i40e_hw *hw = &vsi->back->hw;
2432 	i40e_status aq_ret;
2433 
2434 	if (f->vlan == I40E_VLAN_ANY) {
2435 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2436 						   vsi->seid,
2437 						   enable,
2438 						   NULL);
2439 	} else {
2440 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2441 							    vsi->seid,
2442 							    enable,
2443 							    f->vlan,
2444 							    NULL);
2445 	}
2446 
2447 	if (aq_ret) {
2448 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2449 		dev_warn(&vsi->back->pdev->dev,
2450 			 "Error %s, forcing overflow promiscuous on %s\n",
2451 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2452 			 vsi_name);
2453 	}
2454 
2455 	return aq_ret;
2456 }
2457 
2458 /**
2459  * i40e_set_promiscuous - set promiscuous mode
2460  * @pf: board private structure
2461  * @promisc: promisc on or off
2462  *
2463  * There are different ways of setting promiscuous mode on a PF depending on
2464  * what state/environment we're in.  This identifies and sets it appropriately.
2465  * Returns 0 on success.
2466  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2467 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2468 {
2469 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2470 	struct i40e_hw *hw = &pf->hw;
2471 	i40e_status aq_ret;
2472 
2473 	if (vsi->type == I40E_VSI_MAIN &&
2474 	    pf->lan_veb != I40E_NO_VEB &&
2475 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2476 		/* set defport ON for Main VSI instead of true promisc
2477 		 * this way we will get all unicast/multicast and VLAN
2478 		 * promisc behavior but will not get VF or VMDq traffic
2479 		 * replicated on the Main VSI.
2480 		 */
2481 		if (promisc)
2482 			aq_ret = i40e_aq_set_default_vsi(hw,
2483 							 vsi->seid,
2484 							 NULL);
2485 		else
2486 			aq_ret = i40e_aq_clear_default_vsi(hw,
2487 							   vsi->seid,
2488 							   NULL);
2489 		if (aq_ret) {
2490 			dev_info(&pf->pdev->dev,
2491 				 "Set default VSI failed, err %s, aq_err %s\n",
2492 				 i40e_stat_str(hw, aq_ret),
2493 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2494 		}
2495 	} else {
2496 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2497 						  hw,
2498 						  vsi->seid,
2499 						  promisc, NULL,
2500 						  true);
2501 		if (aq_ret) {
2502 			dev_info(&pf->pdev->dev,
2503 				 "set unicast promisc failed, err %s, aq_err %s\n",
2504 				 i40e_stat_str(hw, aq_ret),
2505 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2506 		}
2507 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2508 						  hw,
2509 						  vsi->seid,
2510 						  promisc, NULL);
2511 		if (aq_ret) {
2512 			dev_info(&pf->pdev->dev,
2513 				 "set multicast promisc failed, err %s, aq_err %s\n",
2514 				 i40e_stat_str(hw, aq_ret),
2515 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2516 		}
2517 	}
2518 
2519 	if (!aq_ret)
2520 		pf->cur_promisc = promisc;
2521 
2522 	return aq_ret;
2523 }
2524 
2525 /**
2526  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2527  * @vsi: ptr to the VSI
2528  *
2529  * Push any outstanding VSI filter changes through the AdminQ.
2530  *
2531  * Returns 0 or error value
2532  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2533 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2534 {
2535 	struct hlist_head tmp_add_list, tmp_del_list;
2536 	struct i40e_mac_filter *f;
2537 	struct i40e_new_mac_filter *new, *add_head = NULL;
2538 	struct i40e_hw *hw = &vsi->back->hw;
2539 	bool old_overflow, new_overflow;
2540 	unsigned int failed_filters = 0;
2541 	unsigned int vlan_filters = 0;
2542 	char vsi_name[16] = "PF";
2543 	int filter_list_len = 0;
2544 	i40e_status aq_ret = 0;
2545 	u32 changed_flags = 0;
2546 	struct hlist_node *h;
2547 	struct i40e_pf *pf;
2548 	int num_add = 0;
2549 	int num_del = 0;
2550 	int retval = 0;
2551 	u16 cmd_flags;
2552 	int list_size;
2553 	int bkt;
2554 
2555 	/* empty array typed pointers, kcalloc later */
2556 	struct i40e_aqc_add_macvlan_element_data *add_list;
2557 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2558 
2559 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2560 		usleep_range(1000, 2000);
2561 	pf = vsi->back;
2562 
2563 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2564 
2565 	if (vsi->netdev) {
2566 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2567 		vsi->current_netdev_flags = vsi->netdev->flags;
2568 	}
2569 
2570 	INIT_HLIST_HEAD(&tmp_add_list);
2571 	INIT_HLIST_HEAD(&tmp_del_list);
2572 
2573 	if (vsi->type == I40E_VSI_SRIOV)
2574 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2575 	else if (vsi->type != I40E_VSI_MAIN)
2576 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2577 
2578 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2579 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2580 
2581 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2582 		/* Create a list of filters to delete. */
2583 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2584 			if (f->state == I40E_FILTER_REMOVE) {
2585 				/* Move the element into temporary del_list */
2586 				hash_del(&f->hlist);
2587 				hlist_add_head(&f->hlist, &tmp_del_list);
2588 
2589 				/* Avoid counting removed filters */
2590 				continue;
2591 			}
2592 			if (f->state == I40E_FILTER_NEW) {
2593 				/* Create a temporary i40e_new_mac_filter */
2594 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2595 				if (!new)
2596 					goto err_no_memory_locked;
2597 
2598 				/* Store pointer to the real filter */
2599 				new->f = f;
2600 				new->state = f->state;
2601 
2602 				/* Add it to the hash list */
2603 				hlist_add_head(&new->hlist, &tmp_add_list);
2604 			}
2605 
2606 			/* Count the number of active (current and new) VLAN
2607 			 * filters we have now. Does not count filters which
2608 			 * are marked for deletion.
2609 			 */
2610 			if (f->vlan > 0)
2611 				vlan_filters++;
2612 		}
2613 
2614 		if (vsi->type != I40E_VSI_SRIOV)
2615 			retval = i40e_correct_mac_vlan_filters
2616 				(vsi, &tmp_add_list, &tmp_del_list,
2617 				 vlan_filters);
2618 		else
2619 			retval = i40e_correct_vf_mac_vlan_filters
2620 				(vsi, &tmp_add_list, &tmp_del_list,
2621 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2622 
2623 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2624 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2625 
2626 		if (retval)
2627 			goto err_no_memory_locked;
2628 
2629 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2630 	}
2631 
2632 	/* Now process 'del_list' outside the lock */
2633 	if (!hlist_empty(&tmp_del_list)) {
2634 		filter_list_len = hw->aq.asq_buf_size /
2635 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2636 		list_size = filter_list_len *
2637 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2638 		del_list = kzalloc(list_size, GFP_ATOMIC);
2639 		if (!del_list)
2640 			goto err_no_memory;
2641 
2642 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2643 			cmd_flags = 0;
2644 
2645 			/* handle broadcast filters by updating the broadcast
2646 			 * promiscuous flag and release filter list.
2647 			 */
2648 			if (is_broadcast_ether_addr(f->macaddr)) {
2649 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2650 
2651 				hlist_del(&f->hlist);
2652 				kfree(f);
2653 				continue;
2654 			}
2655 
2656 			/* add to delete list */
2657 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2658 			if (f->vlan == I40E_VLAN_ANY) {
2659 				del_list[num_del].vlan_tag = 0;
2660 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2661 			} else {
2662 				del_list[num_del].vlan_tag =
2663 					cpu_to_le16((u16)(f->vlan));
2664 			}
2665 
2666 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2667 			del_list[num_del].flags = cmd_flags;
2668 			num_del++;
2669 
2670 			/* flush a full buffer */
2671 			if (num_del == filter_list_len) {
2672 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2673 						     num_del, &retval);
2674 				memset(del_list, 0, list_size);
2675 				num_del = 0;
2676 			}
2677 			/* Release memory for MAC filter entries which were
2678 			 * synced up with HW.
2679 			 */
2680 			hlist_del(&f->hlist);
2681 			kfree(f);
2682 		}
2683 
2684 		if (num_del) {
2685 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2686 					     num_del, &retval);
2687 		}
2688 
2689 		kfree(del_list);
2690 		del_list = NULL;
2691 	}
2692 
2693 	if (!hlist_empty(&tmp_add_list)) {
2694 		/* Do all the adds now. */
2695 		filter_list_len = hw->aq.asq_buf_size /
2696 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2697 		list_size = filter_list_len *
2698 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2699 		add_list = kzalloc(list_size, GFP_ATOMIC);
2700 		if (!add_list)
2701 			goto err_no_memory;
2702 
2703 		num_add = 0;
2704 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2705 			/* handle broadcast filters by updating the broadcast
2706 			 * promiscuous flag instead of adding a MAC filter.
2707 			 */
2708 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2709 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2710 							      new->f))
2711 					new->state = I40E_FILTER_FAILED;
2712 				else
2713 					new->state = I40E_FILTER_ACTIVE;
2714 				continue;
2715 			}
2716 
2717 			/* add to add array */
2718 			if (num_add == 0)
2719 				add_head = new;
2720 			cmd_flags = 0;
2721 			ether_addr_copy(add_list[num_add].mac_addr,
2722 					new->f->macaddr);
2723 			if (new->f->vlan == I40E_VLAN_ANY) {
2724 				add_list[num_add].vlan_tag = 0;
2725 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2726 			} else {
2727 				add_list[num_add].vlan_tag =
2728 					cpu_to_le16((u16)(new->f->vlan));
2729 			}
2730 			add_list[num_add].queue_number = 0;
2731 			/* set invalid match method for later detection */
2732 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2733 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2734 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2735 			num_add++;
2736 
2737 			/* flush a full buffer */
2738 			if (num_add == filter_list_len) {
2739 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2740 						     add_head, num_add);
2741 				memset(add_list, 0, list_size);
2742 				num_add = 0;
2743 			}
2744 		}
2745 		if (num_add) {
2746 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2747 					     num_add);
2748 		}
2749 		/* Now move all of the filters from the temp add list back to
2750 		 * the VSI's list.
2751 		 */
2752 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2753 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2754 			/* Only update the state if we're still NEW */
2755 			if (new->f->state == I40E_FILTER_NEW)
2756 				new->f->state = new->state;
2757 			hlist_del(&new->hlist);
2758 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2759 			kfree(new);
2760 		}
2761 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2762 		kfree(add_list);
2763 		add_list = NULL;
2764 	}
2765 
2766 	/* Determine the number of active and failed filters. */
2767 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2768 	vsi->active_filters = 0;
2769 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2770 		if (f->state == I40E_FILTER_ACTIVE)
2771 			vsi->active_filters++;
2772 		else if (f->state == I40E_FILTER_FAILED)
2773 			failed_filters++;
2774 	}
2775 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2776 
2777 	/* Check if we are able to exit overflow promiscuous mode. We can
2778 	 * safely exit if we didn't just enter, we no longer have any failed
2779 	 * filters, and we have reduced filters below the threshold value.
2780 	 */
2781 	if (old_overflow && !failed_filters &&
2782 	    vsi->active_filters < vsi->promisc_threshold) {
2783 		dev_info(&pf->pdev->dev,
2784 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2785 			 vsi_name);
2786 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2787 		vsi->promisc_threshold = 0;
2788 	}
2789 
2790 	/* if the VF is not trusted do not do promisc */
2791 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2792 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2793 		goto out;
2794 	}
2795 
2796 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2797 
2798 	/* If we are entering overflow promiscuous, we need to calculate a new
2799 	 * threshold for when we are safe to exit
2800 	 */
2801 	if (!old_overflow && new_overflow)
2802 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2803 
2804 	/* check for changes in promiscuous modes */
2805 	if (changed_flags & IFF_ALLMULTI) {
2806 		bool cur_multipromisc;
2807 
2808 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2809 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2810 							       vsi->seid,
2811 							       cur_multipromisc,
2812 							       NULL);
2813 		if (aq_ret) {
2814 			retval = i40e_aq_rc_to_posix(aq_ret,
2815 						     hw->aq.asq_last_status);
2816 			dev_info(&pf->pdev->dev,
2817 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2818 				 vsi_name,
2819 				 i40e_stat_str(hw, aq_ret),
2820 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2821 		} else {
2822 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2823 				 cur_multipromisc ? "entering" : "leaving");
2824 		}
2825 	}
2826 
2827 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2828 		bool cur_promisc;
2829 
2830 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2831 			       new_overflow);
2832 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2833 		if (aq_ret) {
2834 			retval = i40e_aq_rc_to_posix(aq_ret,
2835 						     hw->aq.asq_last_status);
2836 			dev_info(&pf->pdev->dev,
2837 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2838 				 cur_promisc ? "on" : "off",
2839 				 vsi_name,
2840 				 i40e_stat_str(hw, aq_ret),
2841 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2842 		}
2843 	}
2844 out:
2845 	/* if something went wrong then set the changed flag so we try again */
2846 	if (retval)
2847 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2848 
2849 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2850 	return retval;
2851 
2852 err_no_memory:
2853 	/* Restore elements on the temporary add and delete lists */
2854 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2855 err_no_memory_locked:
2856 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2857 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2858 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2859 
2860 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2861 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2862 	return -ENOMEM;
2863 }
2864 
2865 /**
2866  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2867  * @pf: board private structure
2868  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2869 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2870 {
2871 	int v;
2872 
2873 	if (!pf)
2874 		return;
2875 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2876 		return;
2877 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2878 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2879 		return;
2880 	}
2881 
2882 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2883 		if (pf->vsi[v] &&
2884 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2885 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2886 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2887 
2888 			if (ret) {
2889 				/* come back and try again later */
2890 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2891 					pf->state);
2892 				break;
2893 			}
2894 		}
2895 	}
2896 }
2897 
2898 /**
2899  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2900  * @vsi: the vsi
2901  **/
i40e_max_xdp_frame_size(struct i40e_vsi * vsi)2902 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2903 {
2904 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2905 		return I40E_RXBUFFER_2048;
2906 	else
2907 		return I40E_RXBUFFER_3072;
2908 }
2909 
2910 /**
2911  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2912  * @netdev: network interface device structure
2913  * @new_mtu: new value for maximum frame size
2914  *
2915  * Returns 0 on success, negative on failure
2916  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2917 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2918 {
2919 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2920 	struct i40e_vsi *vsi = np->vsi;
2921 	struct i40e_pf *pf = vsi->back;
2922 
2923 	if (i40e_enabled_xdp_vsi(vsi)) {
2924 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2925 
2926 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2927 			return -EINVAL;
2928 	}
2929 
2930 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2931 		   netdev->mtu, new_mtu);
2932 	netdev->mtu = new_mtu;
2933 	if (netif_running(netdev))
2934 		i40e_vsi_reinit_locked(vsi);
2935 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2936 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2937 	return 0;
2938 }
2939 
2940 /**
2941  * i40e_ioctl - Access the hwtstamp interface
2942  * @netdev: network interface device structure
2943  * @ifr: interface request data
2944  * @cmd: ioctl command
2945  **/
i40e_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)2946 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2947 {
2948 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2949 	struct i40e_pf *pf = np->vsi->back;
2950 
2951 	switch (cmd) {
2952 	case SIOCGHWTSTAMP:
2953 		return i40e_ptp_get_ts_config(pf, ifr);
2954 	case SIOCSHWTSTAMP:
2955 		return i40e_ptp_set_ts_config(pf, ifr);
2956 	default:
2957 		return -EOPNOTSUPP;
2958 	}
2959 }
2960 
2961 /**
2962  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2963  * @vsi: the vsi being adjusted
2964  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)2965 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2966 {
2967 	struct i40e_vsi_context ctxt;
2968 	i40e_status ret;
2969 
2970 	/* Don't modify stripping options if a port VLAN is active */
2971 	if (vsi->info.pvid)
2972 		return;
2973 
2974 	if ((vsi->info.valid_sections &
2975 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2976 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2977 		return;  /* already enabled */
2978 
2979 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2980 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2981 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2982 
2983 	ctxt.seid = vsi->seid;
2984 	ctxt.info = vsi->info;
2985 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2986 	if (ret) {
2987 		dev_info(&vsi->back->pdev->dev,
2988 			 "update vlan stripping failed, err %s aq_err %s\n",
2989 			 i40e_stat_str(&vsi->back->hw, ret),
2990 			 i40e_aq_str(&vsi->back->hw,
2991 				     vsi->back->hw.aq.asq_last_status));
2992 	}
2993 }
2994 
2995 /**
2996  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2997  * @vsi: the vsi being adjusted
2998  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)2999 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3000 {
3001 	struct i40e_vsi_context ctxt;
3002 	i40e_status ret;
3003 
3004 	/* Don't modify stripping options if a port VLAN is active */
3005 	if (vsi->info.pvid)
3006 		return;
3007 
3008 	if ((vsi->info.valid_sections &
3009 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3010 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3011 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3012 		return;  /* already disabled */
3013 
3014 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3015 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3016 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3017 
3018 	ctxt.seid = vsi->seid;
3019 	ctxt.info = vsi->info;
3020 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3021 	if (ret) {
3022 		dev_info(&vsi->back->pdev->dev,
3023 			 "update vlan stripping failed, err %s aq_err %s\n",
3024 			 i40e_stat_str(&vsi->back->hw, ret),
3025 			 i40e_aq_str(&vsi->back->hw,
3026 				     vsi->back->hw.aq.asq_last_status));
3027 	}
3028 }
3029 
3030 /**
3031  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3032  * @vsi: the vsi being configured
3033  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3034  *
3035  * This is a helper function for adding a new MAC/VLAN filter with the
3036  * specified VLAN for each existing MAC address already in the hash table.
3037  * This function does *not* perform any accounting to update filters based on
3038  * VLAN mode.
3039  *
3040  * NOTE: this function expects to be called while under the
3041  * mac_filter_hash_lock
3042  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3043 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3044 {
3045 	struct i40e_mac_filter *f, *add_f;
3046 	struct hlist_node *h;
3047 	int bkt;
3048 
3049 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3050 		/* If we're asked to add a filter that has been marked for
3051 		 * removal, it is safe to simply restore it to active state.
3052 		 * __i40e_del_filter will have simply deleted any filters which
3053 		 * were previously marked NEW or FAILED, so if it is currently
3054 		 * marked REMOVE it must have previously been ACTIVE. Since we
3055 		 * haven't yet run the sync filters task, just restore this
3056 		 * filter to the ACTIVE state so that the sync task leaves it
3057 		 * in place.
3058 		 */
3059 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3060 			f->state = I40E_FILTER_ACTIVE;
3061 			continue;
3062 		} else if (f->state == I40E_FILTER_REMOVE) {
3063 			continue;
3064 		}
3065 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3066 		if (!add_f) {
3067 			dev_info(&vsi->back->pdev->dev,
3068 				 "Could not add vlan filter %d for %pM\n",
3069 				 vid, f->macaddr);
3070 			return -ENOMEM;
3071 		}
3072 	}
3073 
3074 	return 0;
3075 }
3076 
3077 /**
3078  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3079  * @vsi: the VSI being configured
3080  * @vid: VLAN id to be added
3081  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)3082 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3083 {
3084 	int err;
3085 
3086 	if (vsi->info.pvid)
3087 		return -EINVAL;
3088 
3089 	/* The network stack will attempt to add VID=0, with the intention to
3090 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3091 	 * these packets by default when configured to receive untagged
3092 	 * packets, so we don't need to add a filter for this case.
3093 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3094 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3095 	 * Thus, we do not want to actually add a filter for VID=0
3096 	 */
3097 	if (!vid)
3098 		return 0;
3099 
3100 	/* Locked once because all functions invoked below iterates list*/
3101 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3102 	err = i40e_add_vlan_all_mac(vsi, vid);
3103 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3104 	if (err)
3105 		return err;
3106 
3107 	/* schedule our worker thread which will take care of
3108 	 * applying the new filter changes
3109 	 */
3110 	i40e_service_event_schedule(vsi->back);
3111 	return 0;
3112 }
3113 
3114 /**
3115  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3116  * @vsi: the vsi being configured
3117  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3118  *
3119  * This function should be used to remove all VLAN filters which match the
3120  * given VID. It does not schedule the service event and does not take the
3121  * mac_filter_hash_lock so it may be combined with other operations under
3122  * a single invocation of the mac_filter_hash_lock.
3123  *
3124  * NOTE: this function expects to be called while under the
3125  * mac_filter_hash_lock
3126  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3127 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3128 {
3129 	struct i40e_mac_filter *f;
3130 	struct hlist_node *h;
3131 	int bkt;
3132 
3133 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3134 		if (f->vlan == vid)
3135 			__i40e_del_filter(vsi, f);
3136 	}
3137 }
3138 
3139 /**
3140  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3141  * @vsi: the VSI being configured
3142  * @vid: VLAN id to be removed
3143  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)3144 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3145 {
3146 	if (!vid || vsi->info.pvid)
3147 		return;
3148 
3149 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3150 	i40e_rm_vlan_all_mac(vsi, vid);
3151 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3152 
3153 	/* schedule our worker thread which will take care of
3154 	 * applying the new filter changes
3155 	 */
3156 	i40e_service_event_schedule(vsi->back);
3157 }
3158 
3159 /**
3160  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3161  * @netdev: network interface to be adjusted
3162  * @proto: unused protocol value
3163  * @vid: vlan id to be added
3164  *
3165  * net_device_ops implementation for adding vlan ids
3166  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3167 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3168 				__always_unused __be16 proto, u16 vid)
3169 {
3170 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3171 	struct i40e_vsi *vsi = np->vsi;
3172 	int ret = 0;
3173 
3174 	if (vid >= VLAN_N_VID)
3175 		return -EINVAL;
3176 
3177 	ret = i40e_vsi_add_vlan(vsi, vid);
3178 	if (!ret)
3179 		set_bit(vid, vsi->active_vlans);
3180 
3181 	return ret;
3182 }
3183 
3184 /**
3185  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3186  * @netdev: network interface to be adjusted
3187  * @proto: unused protocol value
3188  * @vid: vlan id to be added
3189  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3190 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3191 				    __always_unused __be16 proto, u16 vid)
3192 {
3193 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3194 	struct i40e_vsi *vsi = np->vsi;
3195 
3196 	if (vid >= VLAN_N_VID)
3197 		return;
3198 	set_bit(vid, vsi->active_vlans);
3199 }
3200 
3201 /**
3202  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3203  * @netdev: network interface to be adjusted
3204  * @proto: unused protocol value
3205  * @vid: vlan id to be removed
3206  *
3207  * net_device_ops implementation for removing vlan ids
3208  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3209 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3210 				 __always_unused __be16 proto, u16 vid)
3211 {
3212 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3213 	struct i40e_vsi *vsi = np->vsi;
3214 
3215 	/* return code is ignored as there is nothing a user
3216 	 * can do about failure to remove and a log message was
3217 	 * already printed from the other function
3218 	 */
3219 	i40e_vsi_kill_vlan(vsi, vid);
3220 
3221 	clear_bit(vid, vsi->active_vlans);
3222 
3223 	return 0;
3224 }
3225 
3226 /**
3227  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3228  * @vsi: the vsi being brought back up
3229  **/
i40e_restore_vlan(struct i40e_vsi * vsi)3230 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3231 {
3232 	u16 vid;
3233 
3234 	if (!vsi->netdev)
3235 		return;
3236 
3237 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3238 		i40e_vlan_stripping_enable(vsi);
3239 	else
3240 		i40e_vlan_stripping_disable(vsi);
3241 
3242 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3243 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3244 					vid);
3245 }
3246 
3247 /**
3248  * i40e_vsi_add_pvid - Add pvid for the VSI
3249  * @vsi: the vsi being adjusted
3250  * @vid: the vlan id to set as a PVID
3251  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)3252 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3253 {
3254 	struct i40e_vsi_context ctxt;
3255 	i40e_status ret;
3256 
3257 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3258 	vsi->info.pvid = cpu_to_le16(vid);
3259 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3260 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3261 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3262 
3263 	ctxt.seid = vsi->seid;
3264 	ctxt.info = vsi->info;
3265 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3266 	if (ret) {
3267 		dev_info(&vsi->back->pdev->dev,
3268 			 "add pvid failed, err %s aq_err %s\n",
3269 			 i40e_stat_str(&vsi->back->hw, ret),
3270 			 i40e_aq_str(&vsi->back->hw,
3271 				     vsi->back->hw.aq.asq_last_status));
3272 		return -ENOENT;
3273 	}
3274 
3275 	return 0;
3276 }
3277 
3278 /**
3279  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3280  * @vsi: the vsi being adjusted
3281  *
3282  * Just use the vlan_rx_register() service to put it back to normal
3283  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3284 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3285 {
3286 	vsi->info.pvid = 0;
3287 
3288 	i40e_vlan_stripping_disable(vsi);
3289 }
3290 
3291 /**
3292  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3293  * @vsi: ptr to the VSI
3294  *
3295  * If this function returns with an error, then it's possible one or
3296  * more of the rings is populated (while the rest are not).  It is the
3297  * callers duty to clean those orphaned rings.
3298  *
3299  * Return 0 on success, negative on failure
3300  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3301 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3302 {
3303 	int i, err = 0;
3304 
3305 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3306 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3307 
3308 	if (!i40e_enabled_xdp_vsi(vsi))
3309 		return err;
3310 
3311 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3312 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3313 
3314 	return err;
3315 }
3316 
3317 /**
3318  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3319  * @vsi: ptr to the VSI
3320  *
3321  * Free VSI's transmit software resources
3322  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3323 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3324 {
3325 	int i;
3326 
3327 	if (vsi->tx_rings) {
3328 		for (i = 0; i < vsi->num_queue_pairs; i++)
3329 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3330 				i40e_free_tx_resources(vsi->tx_rings[i]);
3331 	}
3332 
3333 	if (vsi->xdp_rings) {
3334 		for (i = 0; i < vsi->num_queue_pairs; i++)
3335 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3336 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3337 	}
3338 }
3339 
3340 /**
3341  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3342  * @vsi: ptr to the VSI
3343  *
3344  * If this function returns with an error, then it's possible one or
3345  * more of the rings is populated (while the rest are not).  It is the
3346  * callers duty to clean those orphaned rings.
3347  *
3348  * Return 0 on success, negative on failure
3349  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3350 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3351 {
3352 	int i, err = 0;
3353 
3354 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3355 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3356 	return err;
3357 }
3358 
3359 /**
3360  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3361  * @vsi: ptr to the VSI
3362  *
3363  * Free all receive software resources
3364  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3365 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3366 {
3367 	int i;
3368 
3369 	if (!vsi->rx_rings)
3370 		return;
3371 
3372 	for (i = 0; i < vsi->num_queue_pairs; i++)
3373 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3374 			i40e_free_rx_resources(vsi->rx_rings[i]);
3375 }
3376 
3377 /**
3378  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3379  * @ring: The Tx ring to configure
3380  *
3381  * This enables/disables XPS for a given Tx descriptor ring
3382  * based on the TCs enabled for the VSI that ring belongs to.
3383  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3384 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3385 {
3386 	int cpu;
3387 
3388 	if (!ring->q_vector || !ring->netdev || ring->ch)
3389 		return;
3390 
3391 	/* We only initialize XPS once, so as not to overwrite user settings */
3392 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3393 		return;
3394 
3395 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3396 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3397 			    ring->queue_index);
3398 }
3399 
3400 /**
3401  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3402  * @ring: The Tx or Rx ring
3403  *
3404  * Returns the AF_XDP buffer pool or NULL.
3405  **/
i40e_xsk_pool(struct i40e_ring * ring)3406 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3407 {
3408 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3409 	int qid = ring->queue_index;
3410 
3411 	if (ring_is_xdp(ring))
3412 		qid -= ring->vsi->alloc_queue_pairs;
3413 
3414 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3415 		return NULL;
3416 
3417 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3418 }
3419 
3420 /**
3421  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3422  * @ring: The Tx ring to configure
3423  *
3424  * Configure the Tx descriptor ring in the HMC context.
3425  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3426 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3427 {
3428 	struct i40e_vsi *vsi = ring->vsi;
3429 	u16 pf_q = vsi->base_queue + ring->queue_index;
3430 	struct i40e_hw *hw = &vsi->back->hw;
3431 	struct i40e_hmc_obj_txq tx_ctx;
3432 	i40e_status err = 0;
3433 	u32 qtx_ctl = 0;
3434 
3435 	if (ring_is_xdp(ring))
3436 		ring->xsk_pool = i40e_xsk_pool(ring);
3437 
3438 	/* some ATR related tx ring init */
3439 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3440 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3441 		ring->atr_count = 0;
3442 	} else {
3443 		ring->atr_sample_rate = 0;
3444 	}
3445 
3446 	/* configure XPS */
3447 	i40e_config_xps_tx_ring(ring);
3448 
3449 	/* clear the context structure first */
3450 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3451 
3452 	tx_ctx.new_context = 1;
3453 	tx_ctx.base = (ring->dma / 128);
3454 	tx_ctx.qlen = ring->count;
3455 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3456 					       I40E_FLAG_FD_ATR_ENABLED));
3457 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3458 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3459 	if (vsi->type != I40E_VSI_FDIR)
3460 		tx_ctx.head_wb_ena = 1;
3461 	tx_ctx.head_wb_addr = ring->dma +
3462 			      (ring->count * sizeof(struct i40e_tx_desc));
3463 
3464 	/* As part of VSI creation/update, FW allocates certain
3465 	 * Tx arbitration queue sets for each TC enabled for
3466 	 * the VSI. The FW returns the handles to these queue
3467 	 * sets as part of the response buffer to Add VSI,
3468 	 * Update VSI, etc. AQ commands. It is expected that
3469 	 * these queue set handles be associated with the Tx
3470 	 * queues by the driver as part of the TX queue context
3471 	 * initialization. This has to be done regardless of
3472 	 * DCB as by default everything is mapped to TC0.
3473 	 */
3474 
3475 	if (ring->ch)
3476 		tx_ctx.rdylist =
3477 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3478 
3479 	else
3480 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3481 
3482 	tx_ctx.rdylist_act = 0;
3483 
3484 	/* clear the context in the HMC */
3485 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3486 	if (err) {
3487 		dev_info(&vsi->back->pdev->dev,
3488 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3489 			 ring->queue_index, pf_q, err);
3490 		return -ENOMEM;
3491 	}
3492 
3493 	/* set the context in the HMC */
3494 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3495 	if (err) {
3496 		dev_info(&vsi->back->pdev->dev,
3497 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3498 			 ring->queue_index, pf_q, err);
3499 		return -ENOMEM;
3500 	}
3501 
3502 	/* Now associate this queue with this PCI function */
3503 	if (ring->ch) {
3504 		if (ring->ch->type == I40E_VSI_VMDQ2)
3505 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3506 		else
3507 			return -EINVAL;
3508 
3509 		qtx_ctl |= (ring->ch->vsi_number <<
3510 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3511 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3512 	} else {
3513 		if (vsi->type == I40E_VSI_VMDQ2) {
3514 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3515 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3516 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3517 		} else {
3518 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3519 		}
3520 	}
3521 
3522 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3523 		    I40E_QTX_CTL_PF_INDX_MASK);
3524 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3525 	i40e_flush(hw);
3526 
3527 	/* cache tail off for easier writes later */
3528 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3529 
3530 	return 0;
3531 }
3532 
3533 /**
3534  * i40e_rx_offset - Return expected offset into page to access data
3535  * @rx_ring: Ring we are requesting offset of
3536  *
3537  * Returns the offset value for ring into the data buffer.
3538  */
i40e_rx_offset(struct i40e_ring * rx_ring)3539 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3540 {
3541 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3542 }
3543 
3544 /**
3545  * i40e_configure_rx_ring - Configure a receive ring context
3546  * @ring: The Rx ring to configure
3547  *
3548  * Configure the Rx descriptor ring in the HMC context.
3549  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3550 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3551 {
3552 	struct i40e_vsi *vsi = ring->vsi;
3553 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3554 	u16 pf_q = vsi->base_queue + ring->queue_index;
3555 	struct i40e_hw *hw = &vsi->back->hw;
3556 	struct i40e_hmc_obj_rxq rx_ctx;
3557 	i40e_status err = 0;
3558 	bool ok;
3559 	int ret;
3560 
3561 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3562 
3563 	/* clear the context structure first */
3564 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3565 
3566 	if (ring->vsi->type == I40E_VSI_MAIN)
3567 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3568 
3569 	ring->xsk_pool = i40e_xsk_pool(ring);
3570 	if (ring->xsk_pool) {
3571 		ring->rx_buf_len =
3572 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3573 		/* For AF_XDP ZC, we disallow packets to span on
3574 		 * multiple buffers, thus letting us skip that
3575 		 * handling in the fast-path.
3576 		 */
3577 		chain_len = 1;
3578 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3579 						 MEM_TYPE_XSK_BUFF_POOL,
3580 						 NULL);
3581 		if (ret)
3582 			return ret;
3583 		dev_info(&vsi->back->pdev->dev,
3584 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3585 			 ring->queue_index);
3586 
3587 	} else {
3588 		ring->rx_buf_len = vsi->rx_buf_len;
3589 		if (ring->vsi->type == I40E_VSI_MAIN) {
3590 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3591 							 MEM_TYPE_PAGE_SHARED,
3592 							 NULL);
3593 			if (ret)
3594 				return ret;
3595 		}
3596 	}
3597 
3598 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3599 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3600 
3601 	rx_ctx.base = (ring->dma / 128);
3602 	rx_ctx.qlen = ring->count;
3603 
3604 	/* use 16 byte descriptors */
3605 	rx_ctx.dsize = 0;
3606 
3607 	/* descriptor type is always zero
3608 	 * rx_ctx.dtype = 0;
3609 	 */
3610 	rx_ctx.hsplit_0 = 0;
3611 
3612 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3613 	if (hw->revision_id == 0)
3614 		rx_ctx.lrxqthresh = 0;
3615 	else
3616 		rx_ctx.lrxqthresh = 1;
3617 	rx_ctx.crcstrip = 1;
3618 	rx_ctx.l2tsel = 1;
3619 	/* this controls whether VLAN is stripped from inner headers */
3620 	rx_ctx.showiv = 0;
3621 	/* set the prefena field to 1 because the manual says to */
3622 	rx_ctx.prefena = 1;
3623 
3624 	/* clear the context in the HMC */
3625 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3626 	if (err) {
3627 		dev_info(&vsi->back->pdev->dev,
3628 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3629 			 ring->queue_index, pf_q, err);
3630 		return -ENOMEM;
3631 	}
3632 
3633 	/* set the context in the HMC */
3634 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3635 	if (err) {
3636 		dev_info(&vsi->back->pdev->dev,
3637 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3638 			 ring->queue_index, pf_q, err);
3639 		return -ENOMEM;
3640 	}
3641 
3642 	/* configure Rx buffer alignment */
3643 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3644 		clear_ring_build_skb_enabled(ring);
3645 	else
3646 		set_ring_build_skb_enabled(ring);
3647 
3648 	ring->rx_offset = i40e_rx_offset(ring);
3649 
3650 	/* cache tail for quicker writes, and clear the reg before use */
3651 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3652 	writel(0, ring->tail);
3653 
3654 	if (ring->xsk_pool) {
3655 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3656 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3657 	} else {
3658 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3659 	}
3660 	if (!ok) {
3661 		/* Log this in case the user has forgotten to give the kernel
3662 		 * any buffers, even later in the application.
3663 		 */
3664 		dev_info(&vsi->back->pdev->dev,
3665 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3666 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3667 			 ring->queue_index, pf_q);
3668 	}
3669 
3670 	return 0;
3671 }
3672 
3673 /**
3674  * i40e_vsi_configure_tx - Configure the VSI for Tx
3675  * @vsi: VSI structure describing this set of rings and resources
3676  *
3677  * Configure the Tx VSI for operation.
3678  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3679 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3680 {
3681 	int err = 0;
3682 	u16 i;
3683 
3684 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3685 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3686 
3687 	if (err || !i40e_enabled_xdp_vsi(vsi))
3688 		return err;
3689 
3690 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3691 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3692 
3693 	return err;
3694 }
3695 
3696 /**
3697  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
3698  *
3699  * @vsi: VSI to calculate rx_buf_len from
3700  */
i40e_calculate_vsi_rx_buf_len(struct i40e_vsi * vsi)3701 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
3702 {
3703 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3704 		return I40E_RXBUFFER_2048;
3705 
3706 #if (PAGE_SIZE < 8192)
3707 	if (!I40E_2K_TOO_SMALL_WITH_PADDING && vsi->netdev->mtu <= ETH_DATA_LEN)
3708 		return I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3709 #endif
3710 
3711 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
3712 }
3713 
3714 /**
3715  * i40e_vsi_configure_rx - Configure the VSI for Rx
3716  * @vsi: the VSI being configured
3717  *
3718  * Configure the Rx VSI for operation.
3719  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3720 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3721 {
3722 	int err = 0;
3723 	u16 i;
3724 
3725 	vsi->max_frame = I40E_MAX_RXBUFFER;
3726 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3727 
3728 #if (PAGE_SIZE < 8192)
3729 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3730 	    vsi->netdev->mtu <= ETH_DATA_LEN)
3731 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3732 #endif
3733 
3734 	/* set up individual rings */
3735 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3736 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3737 
3738 	return err;
3739 }
3740 
3741 /**
3742  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3743  * @vsi: ptr to the VSI
3744  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3745 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3746 {
3747 	struct i40e_ring *tx_ring, *rx_ring;
3748 	u16 qoffset, qcount;
3749 	int i, n;
3750 
3751 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3752 		/* Reset the TC information */
3753 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3754 			rx_ring = vsi->rx_rings[i];
3755 			tx_ring = vsi->tx_rings[i];
3756 			rx_ring->dcb_tc = 0;
3757 			tx_ring->dcb_tc = 0;
3758 		}
3759 		return;
3760 	}
3761 
3762 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3763 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3764 			continue;
3765 
3766 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3767 		qcount = vsi->tc_config.tc_info[n].qcount;
3768 		for (i = qoffset; i < (qoffset + qcount); i++) {
3769 			rx_ring = vsi->rx_rings[i];
3770 			tx_ring = vsi->tx_rings[i];
3771 			rx_ring->dcb_tc = n;
3772 			tx_ring->dcb_tc = n;
3773 		}
3774 	}
3775 }
3776 
3777 /**
3778  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3779  * @vsi: ptr to the VSI
3780  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3781 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3782 {
3783 	if (vsi->netdev)
3784 		i40e_set_rx_mode(vsi->netdev);
3785 }
3786 
3787 /**
3788  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3789  * @pf: Pointer to the targeted PF
3790  *
3791  * Set all flow director counters to 0.
3792  */
i40e_reset_fdir_filter_cnt(struct i40e_pf * pf)3793 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3794 {
3795 	pf->fd_tcp4_filter_cnt = 0;
3796 	pf->fd_udp4_filter_cnt = 0;
3797 	pf->fd_sctp4_filter_cnt = 0;
3798 	pf->fd_ip4_filter_cnt = 0;
3799 	pf->fd_tcp6_filter_cnt = 0;
3800 	pf->fd_udp6_filter_cnt = 0;
3801 	pf->fd_sctp6_filter_cnt = 0;
3802 	pf->fd_ip6_filter_cnt = 0;
3803 }
3804 
3805 /**
3806  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3807  * @vsi: Pointer to the targeted VSI
3808  *
3809  * This function replays the hlist on the hw where all the SB Flow Director
3810  * filters were saved.
3811  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3812 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3813 {
3814 	struct i40e_fdir_filter *filter;
3815 	struct i40e_pf *pf = vsi->back;
3816 	struct hlist_node *node;
3817 
3818 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3819 		return;
3820 
3821 	/* Reset FDir counters as we're replaying all existing filters */
3822 	i40e_reset_fdir_filter_cnt(pf);
3823 
3824 	hlist_for_each_entry_safe(filter, node,
3825 				  &pf->fdir_filter_list, fdir_node) {
3826 		i40e_add_del_fdir(vsi, filter, true);
3827 	}
3828 }
3829 
3830 /**
3831  * i40e_vsi_configure - Set up the VSI for action
3832  * @vsi: the VSI being configured
3833  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3834 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3835 {
3836 	int err;
3837 
3838 	i40e_set_vsi_rx_mode(vsi);
3839 	i40e_restore_vlan(vsi);
3840 	i40e_vsi_config_dcb_rings(vsi);
3841 	err = i40e_vsi_configure_tx(vsi);
3842 	if (!err)
3843 		err = i40e_vsi_configure_rx(vsi);
3844 
3845 	return err;
3846 }
3847 
3848 /**
3849  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3850  * @vsi: the VSI being configured
3851  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3852 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3853 {
3854 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3855 	struct i40e_pf *pf = vsi->back;
3856 	struct i40e_hw *hw = &pf->hw;
3857 	u16 vector;
3858 	int i, q;
3859 	u32 qp;
3860 
3861 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3862 	 * and PFINT_LNKLSTn registers, e.g.:
3863 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3864 	 */
3865 	qp = vsi->base_queue;
3866 	vector = vsi->base_vector;
3867 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3868 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3869 
3870 		q_vector->rx.next_update = jiffies + 1;
3871 		q_vector->rx.target_itr =
3872 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3873 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3874 		     q_vector->rx.target_itr >> 1);
3875 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3876 
3877 		q_vector->tx.next_update = jiffies + 1;
3878 		q_vector->tx.target_itr =
3879 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3880 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3881 		     q_vector->tx.target_itr >> 1);
3882 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3883 
3884 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3885 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3886 
3887 		/* begin of linked list for RX queue assigned to this vector */
3888 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3889 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3890 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3891 			u32 val;
3892 
3893 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3894 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3895 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3896 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3897 			      (I40E_QUEUE_TYPE_TX <<
3898 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3899 
3900 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3901 
3902 			if (has_xdp) {
3903 				/* TX queue with next queue set to TX */
3904 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3905 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3906 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3907 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3908 				      (I40E_QUEUE_TYPE_TX <<
3909 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3910 
3911 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3912 			}
3913 			/* TX queue with next RX or end of linked list */
3914 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3915 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3916 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3917 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3918 			      (I40E_QUEUE_TYPE_RX <<
3919 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3920 
3921 			/* Terminate the linked list */
3922 			if (q == (q_vector->num_ringpairs - 1))
3923 				val |= (I40E_QUEUE_END_OF_LIST <<
3924 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3925 
3926 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3927 			qp++;
3928 		}
3929 	}
3930 
3931 	i40e_flush(hw);
3932 }
3933 
3934 /**
3935  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3936  * @pf: pointer to private device data structure
3937  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3938 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3939 {
3940 	struct i40e_hw *hw = &pf->hw;
3941 	u32 val;
3942 
3943 	/* clear things first */
3944 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3945 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3946 
3947 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3948 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3949 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3950 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3951 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3952 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3953 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3954 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3955 
3956 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3957 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3958 
3959 	if (pf->flags & I40E_FLAG_PTP)
3960 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3961 
3962 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3963 
3964 	/* SW_ITR_IDX = 0, but don't change INTENA */
3965 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3966 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3967 
3968 	/* OTHER_ITR_IDX = 0 */
3969 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3970 }
3971 
3972 /**
3973  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3974  * @vsi: the VSI being configured
3975  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)3976 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3977 {
3978 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3979 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3980 	struct i40e_pf *pf = vsi->back;
3981 	struct i40e_hw *hw = &pf->hw;
3982 
3983 	/* set the ITR configuration */
3984 	q_vector->rx.next_update = jiffies + 1;
3985 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3986 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3987 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3988 	q_vector->tx.next_update = jiffies + 1;
3989 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3990 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3991 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3992 
3993 	i40e_enable_misc_int_causes(pf);
3994 
3995 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3996 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3997 
3998 	/* Associate the queue pair to the vector and enable the queue
3999 	 * interrupt RX queue in linked list with next queue set to TX
4000 	 */
4001 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4002 
4003 	if (i40e_enabled_xdp_vsi(vsi)) {
4004 		/* TX queue in linked list with next queue set to TX */
4005 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4006 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4007 	}
4008 
4009 	/* last TX queue so the next RX queue doesn't matter */
4010 	wr32(hw, I40E_QINT_TQCTL(0),
4011 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4012 	i40e_flush(hw);
4013 }
4014 
4015 /**
4016  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4017  * @pf: board private structure
4018  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)4019 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4020 {
4021 	struct i40e_hw *hw = &pf->hw;
4022 
4023 	wr32(hw, I40E_PFINT_DYN_CTL0,
4024 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4025 	i40e_flush(hw);
4026 }
4027 
4028 /**
4029  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4030  * @pf: board private structure
4031  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)4032 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4033 {
4034 	struct i40e_hw *hw = &pf->hw;
4035 	u32 val;
4036 
4037 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4038 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4039 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4040 
4041 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4042 	i40e_flush(hw);
4043 }
4044 
4045 /**
4046  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4047  * @irq: interrupt number
4048  * @data: pointer to a q_vector
4049  **/
i40e_msix_clean_rings(int irq,void * data)4050 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4051 {
4052 	struct i40e_q_vector *q_vector = data;
4053 
4054 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4055 		return IRQ_HANDLED;
4056 
4057 	napi_schedule_irqoff(&q_vector->napi);
4058 
4059 	return IRQ_HANDLED;
4060 }
4061 
4062 /**
4063  * i40e_irq_affinity_notify - Callback for affinity changes
4064  * @notify: context as to what irq was changed
4065  * @mask: the new affinity mask
4066  *
4067  * This is a callback function used by the irq_set_affinity_notifier function
4068  * so that we may register to receive changes to the irq affinity masks.
4069  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)4070 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4071 				     const cpumask_t *mask)
4072 {
4073 	struct i40e_q_vector *q_vector =
4074 		container_of(notify, struct i40e_q_vector, affinity_notify);
4075 
4076 	cpumask_copy(&q_vector->affinity_mask, mask);
4077 }
4078 
4079 /**
4080  * i40e_irq_affinity_release - Callback for affinity notifier release
4081  * @ref: internal core kernel usage
4082  *
4083  * This is a callback function used by the irq_set_affinity_notifier function
4084  * to inform the current notification subscriber that they will no longer
4085  * receive notifications.
4086  **/
i40e_irq_affinity_release(struct kref * ref)4087 static void i40e_irq_affinity_release(struct kref *ref) {}
4088 
4089 /**
4090  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4091  * @vsi: the VSI being configured
4092  * @basename: name for the vector
4093  *
4094  * Allocates MSI-X vectors and requests interrupts from the kernel.
4095  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)4096 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4097 {
4098 	int q_vectors = vsi->num_q_vectors;
4099 	struct i40e_pf *pf = vsi->back;
4100 	int base = vsi->base_vector;
4101 	int rx_int_idx = 0;
4102 	int tx_int_idx = 0;
4103 	int vector, err;
4104 	int irq_num;
4105 	int cpu;
4106 
4107 	for (vector = 0; vector < q_vectors; vector++) {
4108 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4109 
4110 		irq_num = pf->msix_entries[base + vector].vector;
4111 
4112 		if (q_vector->tx.ring && q_vector->rx.ring) {
4113 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4114 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4115 			tx_int_idx++;
4116 		} else if (q_vector->rx.ring) {
4117 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4118 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4119 		} else if (q_vector->tx.ring) {
4120 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4121 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4122 		} else {
4123 			/* skip this unused q_vector */
4124 			continue;
4125 		}
4126 		err = request_irq(irq_num,
4127 				  vsi->irq_handler,
4128 				  0,
4129 				  q_vector->name,
4130 				  q_vector);
4131 		if (err) {
4132 			dev_info(&pf->pdev->dev,
4133 				 "MSIX request_irq failed, error: %d\n", err);
4134 			goto free_queue_irqs;
4135 		}
4136 
4137 		/* register for affinity change notifications */
4138 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4139 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4140 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4141 		/* Spread affinity hints out across online CPUs.
4142 		 *
4143 		 * get_cpu_mask returns a static constant mask with
4144 		 * a permanent lifetime so it's ok to pass to
4145 		 * irq_update_affinity_hint without making a copy.
4146 		 */
4147 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4148 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4149 	}
4150 
4151 	vsi->irqs_ready = true;
4152 	return 0;
4153 
4154 free_queue_irqs:
4155 	while (vector) {
4156 		vector--;
4157 		irq_num = pf->msix_entries[base + vector].vector;
4158 		irq_set_affinity_notifier(irq_num, NULL);
4159 		irq_update_affinity_hint(irq_num, NULL);
4160 		free_irq(irq_num, &vsi->q_vectors[vector]);
4161 	}
4162 	return err;
4163 }
4164 
4165 /**
4166  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4167  * @vsi: the VSI being un-configured
4168  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)4169 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4170 {
4171 	struct i40e_pf *pf = vsi->back;
4172 	struct i40e_hw *hw = &pf->hw;
4173 	int base = vsi->base_vector;
4174 	int i;
4175 
4176 	/* disable interrupt causation from each queue */
4177 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4178 		u32 val;
4179 
4180 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4181 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4182 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4183 
4184 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4185 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4186 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4187 
4188 		if (!i40e_enabled_xdp_vsi(vsi))
4189 			continue;
4190 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4191 	}
4192 
4193 	/* disable each interrupt */
4194 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4195 		for (i = vsi->base_vector;
4196 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4197 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4198 
4199 		i40e_flush(hw);
4200 		for (i = 0; i < vsi->num_q_vectors; i++)
4201 			synchronize_irq(pf->msix_entries[i + base].vector);
4202 	} else {
4203 		/* Legacy and MSI mode - this stops all interrupt handling */
4204 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4205 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4206 		i40e_flush(hw);
4207 		synchronize_irq(pf->pdev->irq);
4208 	}
4209 }
4210 
4211 /**
4212  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4213  * @vsi: the VSI being configured
4214  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)4215 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4216 {
4217 	struct i40e_pf *pf = vsi->back;
4218 	int i;
4219 
4220 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4221 		for (i = 0; i < vsi->num_q_vectors; i++)
4222 			i40e_irq_dynamic_enable(vsi, i);
4223 	} else {
4224 		i40e_irq_dynamic_enable_icr0(pf);
4225 	}
4226 
4227 	i40e_flush(&pf->hw);
4228 	return 0;
4229 }
4230 
4231 /**
4232  * i40e_free_misc_vector - Free the vector that handles non-queue events
4233  * @pf: board private structure
4234  **/
i40e_free_misc_vector(struct i40e_pf * pf)4235 static void i40e_free_misc_vector(struct i40e_pf *pf)
4236 {
4237 	/* Disable ICR 0 */
4238 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4239 	i40e_flush(&pf->hw);
4240 
4241 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4242 		free_irq(pf->msix_entries[0].vector, pf);
4243 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4244 	}
4245 }
4246 
4247 /**
4248  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4249  * @irq: interrupt number
4250  * @data: pointer to a q_vector
4251  *
4252  * This is the handler used for all MSI/Legacy interrupts, and deals
4253  * with both queue and non-queue interrupts.  This is also used in
4254  * MSIX mode to handle the non-queue interrupts.
4255  **/
i40e_intr(int irq,void * data)4256 static irqreturn_t i40e_intr(int irq, void *data)
4257 {
4258 	struct i40e_pf *pf = (struct i40e_pf *)data;
4259 	struct i40e_hw *hw = &pf->hw;
4260 	irqreturn_t ret = IRQ_NONE;
4261 	u32 icr0, icr0_remaining;
4262 	u32 val, ena_mask;
4263 
4264 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4265 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4266 
4267 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4268 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4269 		goto enable_intr;
4270 
4271 	/* if interrupt but no bits showing, must be SWINT */
4272 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4273 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4274 		pf->sw_int_count++;
4275 
4276 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4277 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4278 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4279 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4280 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4281 	}
4282 
4283 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4284 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4285 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4286 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4287 
4288 		/* We do not have a way to disarm Queue causes while leaving
4289 		 * interrupt enabled for all other causes, ideally
4290 		 * interrupt should be disabled while we are in NAPI but
4291 		 * this is not a performance path and napi_schedule()
4292 		 * can deal with rescheduling.
4293 		 */
4294 		if (!test_bit(__I40E_DOWN, pf->state))
4295 			napi_schedule_irqoff(&q_vector->napi);
4296 	}
4297 
4298 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4299 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4300 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4301 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4302 	}
4303 
4304 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4305 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4306 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4307 	}
4308 
4309 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4310 		/* disable any further VFLR event notifications */
4311 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4312 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4313 
4314 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4315 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4316 		} else {
4317 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4318 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4319 		}
4320 	}
4321 
4322 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4323 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4324 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4325 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4326 		val = rd32(hw, I40E_GLGEN_RSTAT);
4327 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4328 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4329 		if (val == I40E_RESET_CORER) {
4330 			pf->corer_count++;
4331 		} else if (val == I40E_RESET_GLOBR) {
4332 			pf->globr_count++;
4333 		} else if (val == I40E_RESET_EMPR) {
4334 			pf->empr_count++;
4335 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4336 		}
4337 	}
4338 
4339 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4340 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4341 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4342 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4343 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4344 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4345 	}
4346 
4347 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4348 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4349 
4350 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4351 			schedule_work(&pf->ptp_extts0_work);
4352 
4353 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4354 			i40e_ptp_tx_hwtstamp(pf);
4355 
4356 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4357 	}
4358 
4359 	/* If a critical error is pending we have no choice but to reset the
4360 	 * device.
4361 	 * Report and mask out any remaining unexpected interrupts.
4362 	 */
4363 	icr0_remaining = icr0 & ena_mask;
4364 	if (icr0_remaining) {
4365 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4366 			 icr0_remaining);
4367 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4368 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4369 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4370 			dev_info(&pf->pdev->dev, "device will be reset\n");
4371 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4372 			i40e_service_event_schedule(pf);
4373 		}
4374 		ena_mask &= ~icr0_remaining;
4375 	}
4376 	ret = IRQ_HANDLED;
4377 
4378 enable_intr:
4379 	/* re-enable interrupt causes */
4380 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4381 	if (!test_bit(__I40E_DOWN, pf->state) ||
4382 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4383 		i40e_service_event_schedule(pf);
4384 		i40e_irq_dynamic_enable_icr0(pf);
4385 	}
4386 
4387 	return ret;
4388 }
4389 
4390 /**
4391  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4392  * @tx_ring:  tx ring to clean
4393  * @budget:   how many cleans we're allowed
4394  *
4395  * Returns true if there's any budget left (e.g. the clean is finished)
4396  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4397 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4398 {
4399 	struct i40e_vsi *vsi = tx_ring->vsi;
4400 	u16 i = tx_ring->next_to_clean;
4401 	struct i40e_tx_buffer *tx_buf;
4402 	struct i40e_tx_desc *tx_desc;
4403 
4404 	tx_buf = &tx_ring->tx_bi[i];
4405 	tx_desc = I40E_TX_DESC(tx_ring, i);
4406 	i -= tx_ring->count;
4407 
4408 	do {
4409 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4410 
4411 		/* if next_to_watch is not set then there is no work pending */
4412 		if (!eop_desc)
4413 			break;
4414 
4415 		/* prevent any other reads prior to eop_desc */
4416 		smp_rmb();
4417 
4418 		/* if the descriptor isn't done, no work yet to do */
4419 		if (!(eop_desc->cmd_type_offset_bsz &
4420 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4421 			break;
4422 
4423 		/* clear next_to_watch to prevent false hangs */
4424 		tx_buf->next_to_watch = NULL;
4425 
4426 		tx_desc->buffer_addr = 0;
4427 		tx_desc->cmd_type_offset_bsz = 0;
4428 		/* move past filter desc */
4429 		tx_buf++;
4430 		tx_desc++;
4431 		i++;
4432 		if (unlikely(!i)) {
4433 			i -= tx_ring->count;
4434 			tx_buf = tx_ring->tx_bi;
4435 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4436 		}
4437 		/* unmap skb header data */
4438 		dma_unmap_single(tx_ring->dev,
4439 				 dma_unmap_addr(tx_buf, dma),
4440 				 dma_unmap_len(tx_buf, len),
4441 				 DMA_TO_DEVICE);
4442 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4443 			kfree(tx_buf->raw_buf);
4444 
4445 		tx_buf->raw_buf = NULL;
4446 		tx_buf->tx_flags = 0;
4447 		tx_buf->next_to_watch = NULL;
4448 		dma_unmap_len_set(tx_buf, len, 0);
4449 		tx_desc->buffer_addr = 0;
4450 		tx_desc->cmd_type_offset_bsz = 0;
4451 
4452 		/* move us past the eop_desc for start of next FD desc */
4453 		tx_buf++;
4454 		tx_desc++;
4455 		i++;
4456 		if (unlikely(!i)) {
4457 			i -= tx_ring->count;
4458 			tx_buf = tx_ring->tx_bi;
4459 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4460 		}
4461 
4462 		/* update budget accounting */
4463 		budget--;
4464 	} while (likely(budget));
4465 
4466 	i += tx_ring->count;
4467 	tx_ring->next_to_clean = i;
4468 
4469 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4470 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4471 
4472 	return budget > 0;
4473 }
4474 
4475 /**
4476  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4477  * @irq: interrupt number
4478  * @data: pointer to a q_vector
4479  **/
i40e_fdir_clean_ring(int irq,void * data)4480 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4481 {
4482 	struct i40e_q_vector *q_vector = data;
4483 	struct i40e_vsi *vsi;
4484 
4485 	if (!q_vector->tx.ring)
4486 		return IRQ_HANDLED;
4487 
4488 	vsi = q_vector->tx.ring->vsi;
4489 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4490 
4491 	return IRQ_HANDLED;
4492 }
4493 
4494 /**
4495  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4496  * @vsi: the VSI being configured
4497  * @v_idx: vector index
4498  * @qp_idx: queue pair index
4499  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4500 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4501 {
4502 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4503 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4504 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4505 
4506 	tx_ring->q_vector = q_vector;
4507 	tx_ring->next = q_vector->tx.ring;
4508 	q_vector->tx.ring = tx_ring;
4509 	q_vector->tx.count++;
4510 
4511 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4512 	if (i40e_enabled_xdp_vsi(vsi)) {
4513 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4514 
4515 		xdp_ring->q_vector = q_vector;
4516 		xdp_ring->next = q_vector->tx.ring;
4517 		q_vector->tx.ring = xdp_ring;
4518 		q_vector->tx.count++;
4519 	}
4520 
4521 	rx_ring->q_vector = q_vector;
4522 	rx_ring->next = q_vector->rx.ring;
4523 	q_vector->rx.ring = rx_ring;
4524 	q_vector->rx.count++;
4525 }
4526 
4527 /**
4528  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4529  * @vsi: the VSI being configured
4530  *
4531  * This function maps descriptor rings to the queue-specific vectors
4532  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4533  * one vector per queue pair, but on a constrained vector budget, we
4534  * group the queue pairs as "efficiently" as possible.
4535  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4536 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4537 {
4538 	int qp_remaining = vsi->num_queue_pairs;
4539 	int q_vectors = vsi->num_q_vectors;
4540 	int num_ringpairs;
4541 	int v_start = 0;
4542 	int qp_idx = 0;
4543 
4544 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4545 	 * group them so there are multiple queues per vector.
4546 	 * It is also important to go through all the vectors available to be
4547 	 * sure that if we don't use all the vectors, that the remaining vectors
4548 	 * are cleared. This is especially important when decreasing the
4549 	 * number of queues in use.
4550 	 */
4551 	for (; v_start < q_vectors; v_start++) {
4552 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4553 
4554 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4555 
4556 		q_vector->num_ringpairs = num_ringpairs;
4557 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4558 
4559 		q_vector->rx.count = 0;
4560 		q_vector->tx.count = 0;
4561 		q_vector->rx.ring = NULL;
4562 		q_vector->tx.ring = NULL;
4563 
4564 		while (num_ringpairs--) {
4565 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4566 			qp_idx++;
4567 			qp_remaining--;
4568 		}
4569 	}
4570 }
4571 
4572 /**
4573  * i40e_vsi_request_irq - Request IRQ from the OS
4574  * @vsi: the VSI being configured
4575  * @basename: name for the vector
4576  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4577 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4578 {
4579 	struct i40e_pf *pf = vsi->back;
4580 	int err;
4581 
4582 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4583 		err = i40e_vsi_request_irq_msix(vsi, basename);
4584 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4585 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4586 				  pf->int_name, pf);
4587 	else
4588 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4589 				  pf->int_name, pf);
4590 
4591 	if (err)
4592 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4593 
4594 	return err;
4595 }
4596 
4597 #ifdef CONFIG_NET_POLL_CONTROLLER
4598 /**
4599  * i40e_netpoll - A Polling 'interrupt' handler
4600  * @netdev: network interface device structure
4601  *
4602  * This is used by netconsole to send skbs without having to re-enable
4603  * interrupts.  It's not called while the normal interrupt routine is executing.
4604  **/
i40e_netpoll(struct net_device * netdev)4605 static void i40e_netpoll(struct net_device *netdev)
4606 {
4607 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4608 	struct i40e_vsi *vsi = np->vsi;
4609 	struct i40e_pf *pf = vsi->back;
4610 	int i;
4611 
4612 	/* if interface is down do nothing */
4613 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4614 		return;
4615 
4616 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4617 		for (i = 0; i < vsi->num_q_vectors; i++)
4618 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4619 	} else {
4620 		i40e_intr(pf->pdev->irq, netdev);
4621 	}
4622 }
4623 #endif
4624 
4625 #define I40E_QTX_ENA_WAIT_COUNT 50
4626 
4627 /**
4628  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4629  * @pf: the PF being configured
4630  * @pf_q: the PF queue
4631  * @enable: enable or disable state of the queue
4632  *
4633  * This routine will wait for the given Tx queue of the PF to reach the
4634  * enabled or disabled state.
4635  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4636  * multiple retries; else will return 0 in case of success.
4637  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4638 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4639 {
4640 	int i;
4641 	u32 tx_reg;
4642 
4643 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4644 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4645 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4646 			break;
4647 
4648 		usleep_range(10, 20);
4649 	}
4650 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4651 		return -ETIMEDOUT;
4652 
4653 	return 0;
4654 }
4655 
4656 /**
4657  * i40e_control_tx_q - Start or stop a particular Tx queue
4658  * @pf: the PF structure
4659  * @pf_q: the PF queue to configure
4660  * @enable: start or stop the queue
4661  *
4662  * This function enables or disables a single queue. Note that any delay
4663  * required after the operation is expected to be handled by the caller of
4664  * this function.
4665  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4666 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4667 {
4668 	struct i40e_hw *hw = &pf->hw;
4669 	u32 tx_reg;
4670 	int i;
4671 
4672 	/* warn the TX unit of coming changes */
4673 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4674 	if (!enable)
4675 		usleep_range(10, 20);
4676 
4677 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4678 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4679 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4680 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4681 			break;
4682 		usleep_range(1000, 2000);
4683 	}
4684 
4685 	/* Skip if the queue is already in the requested state */
4686 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4687 		return;
4688 
4689 	/* turn on/off the queue */
4690 	if (enable) {
4691 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4692 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4693 	} else {
4694 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4695 	}
4696 
4697 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4698 }
4699 
4700 /**
4701  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4702  * @seid: VSI SEID
4703  * @pf: the PF structure
4704  * @pf_q: the PF queue to configure
4705  * @is_xdp: true if the queue is used for XDP
4706  * @enable: start or stop the queue
4707  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4708 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4709 			   bool is_xdp, bool enable)
4710 {
4711 	int ret;
4712 
4713 	i40e_control_tx_q(pf, pf_q, enable);
4714 
4715 	/* wait for the change to finish */
4716 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4717 	if (ret) {
4718 		dev_info(&pf->pdev->dev,
4719 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4720 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4721 			 (enable ? "en" : "dis"));
4722 	}
4723 
4724 	return ret;
4725 }
4726 
4727 /**
4728  * i40e_vsi_enable_tx - Start a VSI's rings
4729  * @vsi: the VSI being configured
4730  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4731 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4732 {
4733 	struct i40e_pf *pf = vsi->back;
4734 	int i, pf_q, ret = 0;
4735 
4736 	pf_q = vsi->base_queue;
4737 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4738 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4739 					     pf_q,
4740 					     false /*is xdp*/, true);
4741 		if (ret)
4742 			break;
4743 
4744 		if (!i40e_enabled_xdp_vsi(vsi))
4745 			continue;
4746 
4747 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4748 					     pf_q + vsi->alloc_queue_pairs,
4749 					     true /*is xdp*/, true);
4750 		if (ret)
4751 			break;
4752 	}
4753 	return ret;
4754 }
4755 
4756 /**
4757  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4758  * @pf: the PF being configured
4759  * @pf_q: the PF queue
4760  * @enable: enable or disable state of the queue
4761  *
4762  * This routine will wait for the given Rx queue of the PF to reach the
4763  * enabled or disabled state.
4764  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4765  * multiple retries; else will return 0 in case of success.
4766  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4767 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4768 {
4769 	int i;
4770 	u32 rx_reg;
4771 
4772 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4773 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4774 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4775 			break;
4776 
4777 		usleep_range(10, 20);
4778 	}
4779 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4780 		return -ETIMEDOUT;
4781 
4782 	return 0;
4783 }
4784 
4785 /**
4786  * i40e_control_rx_q - Start or stop a particular Rx queue
4787  * @pf: the PF structure
4788  * @pf_q: the PF queue to configure
4789  * @enable: start or stop the queue
4790  *
4791  * This function enables or disables a single queue. Note that
4792  * any delay required after the operation is expected to be
4793  * handled by the caller of this function.
4794  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4795 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4796 {
4797 	struct i40e_hw *hw = &pf->hw;
4798 	u32 rx_reg;
4799 	int i;
4800 
4801 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4802 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4803 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4804 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4805 			break;
4806 		usleep_range(1000, 2000);
4807 	}
4808 
4809 	/* Skip if the queue is already in the requested state */
4810 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4811 		return;
4812 
4813 	/* turn on/off the queue */
4814 	if (enable)
4815 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4816 	else
4817 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4818 
4819 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4820 }
4821 
4822 /**
4823  * i40e_control_wait_rx_q
4824  * @pf: the PF structure
4825  * @pf_q: queue being configured
4826  * @enable: start or stop the rings
4827  *
4828  * This function enables or disables a single queue along with waiting
4829  * for the change to finish. The caller of this function should handle
4830  * the delays needed in the case of disabling queues.
4831  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4832 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4833 {
4834 	int ret = 0;
4835 
4836 	i40e_control_rx_q(pf, pf_q, enable);
4837 
4838 	/* wait for the change to finish */
4839 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4840 	if (ret)
4841 		return ret;
4842 
4843 	return ret;
4844 }
4845 
4846 /**
4847  * i40e_vsi_enable_rx - Start a VSI's rings
4848  * @vsi: the VSI being configured
4849  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4850 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4851 {
4852 	struct i40e_pf *pf = vsi->back;
4853 	int i, pf_q, ret = 0;
4854 
4855 	pf_q = vsi->base_queue;
4856 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4857 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4858 		if (ret) {
4859 			dev_info(&pf->pdev->dev,
4860 				 "VSI seid %d Rx ring %d enable timeout\n",
4861 				 vsi->seid, pf_q);
4862 			break;
4863 		}
4864 	}
4865 
4866 	return ret;
4867 }
4868 
4869 /**
4870  * i40e_vsi_start_rings - Start a VSI's rings
4871  * @vsi: the VSI being configured
4872  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4873 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4874 {
4875 	int ret = 0;
4876 
4877 	/* do rx first for enable and last for disable */
4878 	ret = i40e_vsi_enable_rx(vsi);
4879 	if (ret)
4880 		return ret;
4881 	ret = i40e_vsi_enable_tx(vsi);
4882 
4883 	return ret;
4884 }
4885 
4886 #define I40E_DISABLE_TX_GAP_MSEC	50
4887 
4888 /**
4889  * i40e_vsi_stop_rings - Stop a VSI's rings
4890  * @vsi: the VSI being configured
4891  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4892 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4893 {
4894 	struct i40e_pf *pf = vsi->back;
4895 	int pf_q, err, q_end;
4896 
4897 	/* When port TX is suspended, don't wait */
4898 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4899 		return i40e_vsi_stop_rings_no_wait(vsi);
4900 
4901 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4902 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4903 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4904 
4905 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4906 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4907 		if (err)
4908 			dev_info(&pf->pdev->dev,
4909 				 "VSI seid %d Rx ring %d disable timeout\n",
4910 				 vsi->seid, pf_q);
4911 	}
4912 
4913 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4914 	pf_q = vsi->base_queue;
4915 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4916 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4917 
4918 	i40e_vsi_wait_queues_disabled(vsi);
4919 }
4920 
4921 /**
4922  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4923  * @vsi: the VSI being shutdown
4924  *
4925  * This function stops all the rings for a VSI but does not delay to verify
4926  * that rings have been disabled. It is expected that the caller is shutting
4927  * down multiple VSIs at once and will delay together for all the VSIs after
4928  * initiating the shutdown. This is particularly useful for shutting down lots
4929  * of VFs together. Otherwise, a large delay can be incurred while configuring
4930  * each VSI in serial.
4931  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4932 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4933 {
4934 	struct i40e_pf *pf = vsi->back;
4935 	int i, pf_q;
4936 
4937 	pf_q = vsi->base_queue;
4938 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4939 		i40e_control_tx_q(pf, pf_q, false);
4940 		i40e_control_rx_q(pf, pf_q, false);
4941 	}
4942 }
4943 
4944 /**
4945  * i40e_vsi_free_irq - Free the irq association with the OS
4946  * @vsi: the VSI being configured
4947  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)4948 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4949 {
4950 	struct i40e_pf *pf = vsi->back;
4951 	struct i40e_hw *hw = &pf->hw;
4952 	int base = vsi->base_vector;
4953 	u32 val, qp;
4954 	int i;
4955 
4956 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4957 		if (!vsi->q_vectors)
4958 			return;
4959 
4960 		if (!vsi->irqs_ready)
4961 			return;
4962 
4963 		vsi->irqs_ready = false;
4964 		for (i = 0; i < vsi->num_q_vectors; i++) {
4965 			int irq_num;
4966 			u16 vector;
4967 
4968 			vector = i + base;
4969 			irq_num = pf->msix_entries[vector].vector;
4970 
4971 			/* free only the irqs that were actually requested */
4972 			if (!vsi->q_vectors[i] ||
4973 			    !vsi->q_vectors[i]->num_ringpairs)
4974 				continue;
4975 
4976 			/* clear the affinity notifier in the IRQ descriptor */
4977 			irq_set_affinity_notifier(irq_num, NULL);
4978 			/* remove our suggested affinity mask for this IRQ */
4979 			irq_update_affinity_hint(irq_num, NULL);
4980 			free_irq(irq_num, vsi->q_vectors[i]);
4981 
4982 			/* Tear down the interrupt queue link list
4983 			 *
4984 			 * We know that they come in pairs and always
4985 			 * the Rx first, then the Tx.  To clear the
4986 			 * link list, stick the EOL value into the
4987 			 * next_q field of the registers.
4988 			 */
4989 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4990 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4991 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4992 			val |= I40E_QUEUE_END_OF_LIST
4993 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4994 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4995 
4996 			while (qp != I40E_QUEUE_END_OF_LIST) {
4997 				u32 next;
4998 
4999 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5000 
5001 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5002 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5003 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5004 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5005 
5006 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5007 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5008 
5009 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5010 
5011 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5012 
5013 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5014 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5015 
5016 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5017 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5018 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5019 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5020 
5021 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5022 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5023 
5024 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5025 				qp = next;
5026 			}
5027 		}
5028 	} else {
5029 		free_irq(pf->pdev->irq, pf);
5030 
5031 		val = rd32(hw, I40E_PFINT_LNKLST0);
5032 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5033 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5034 		val |= I40E_QUEUE_END_OF_LIST
5035 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5036 		wr32(hw, I40E_PFINT_LNKLST0, val);
5037 
5038 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5039 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5040 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5041 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5042 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5043 
5044 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5045 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5046 
5047 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5048 
5049 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5050 
5051 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5052 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5053 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5054 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5055 
5056 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5057 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5058 
5059 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5060 	}
5061 }
5062 
5063 /**
5064  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5065  * @vsi: the VSI being configured
5066  * @v_idx: Index of vector to be freed
5067  *
5068  * This function frees the memory allocated to the q_vector.  In addition if
5069  * NAPI is enabled it will delete any references to the NAPI struct prior
5070  * to freeing the q_vector.
5071  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)5072 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5073 {
5074 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5075 	struct i40e_ring *ring;
5076 
5077 	if (!q_vector)
5078 		return;
5079 
5080 	/* disassociate q_vector from rings */
5081 	i40e_for_each_ring(ring, q_vector->tx)
5082 		ring->q_vector = NULL;
5083 
5084 	i40e_for_each_ring(ring, q_vector->rx)
5085 		ring->q_vector = NULL;
5086 
5087 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5088 	if (vsi->netdev)
5089 		netif_napi_del(&q_vector->napi);
5090 
5091 	vsi->q_vectors[v_idx] = NULL;
5092 
5093 	kfree_rcu(q_vector, rcu);
5094 }
5095 
5096 /**
5097  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5098  * @vsi: the VSI being un-configured
5099  *
5100  * This frees the memory allocated to the q_vectors and
5101  * deletes references to the NAPI struct.
5102  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)5103 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5104 {
5105 	int v_idx;
5106 
5107 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5108 		i40e_free_q_vector(vsi, v_idx);
5109 }
5110 
5111 /**
5112  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5113  * @pf: board private structure
5114  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)5115 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5116 {
5117 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5118 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5119 		pci_disable_msix(pf->pdev);
5120 		kfree(pf->msix_entries);
5121 		pf->msix_entries = NULL;
5122 		kfree(pf->irq_pile);
5123 		pf->irq_pile = NULL;
5124 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5125 		pci_disable_msi(pf->pdev);
5126 	}
5127 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5128 }
5129 
5130 /**
5131  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5132  * @pf: board private structure
5133  *
5134  * We go through and clear interrupt specific resources and reset the structure
5135  * to pre-load conditions
5136  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)5137 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5138 {
5139 	int i;
5140 
5141 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5142 		i40e_free_misc_vector(pf);
5143 
5144 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5145 		      I40E_IWARP_IRQ_PILE_ID);
5146 
5147 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5148 	for (i = 0; i < pf->num_alloc_vsi; i++)
5149 		if (pf->vsi[i])
5150 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5151 	i40e_reset_interrupt_capability(pf);
5152 }
5153 
5154 /**
5155  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5156  * @vsi: the VSI being configured
5157  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)5158 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5159 {
5160 	int q_idx;
5161 
5162 	if (!vsi->netdev)
5163 		return;
5164 
5165 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5166 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5167 
5168 		if (q_vector->rx.ring || q_vector->tx.ring)
5169 			napi_enable(&q_vector->napi);
5170 	}
5171 }
5172 
5173 /**
5174  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5175  * @vsi: the VSI being configured
5176  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)5177 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5178 {
5179 	int q_idx;
5180 
5181 	if (!vsi->netdev)
5182 		return;
5183 
5184 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5185 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5186 
5187 		if (q_vector->rx.ring || q_vector->tx.ring)
5188 			napi_disable(&q_vector->napi);
5189 	}
5190 }
5191 
5192 /**
5193  * i40e_vsi_close - Shut down a VSI
5194  * @vsi: the vsi to be quelled
5195  **/
i40e_vsi_close(struct i40e_vsi * vsi)5196 static void i40e_vsi_close(struct i40e_vsi *vsi)
5197 {
5198 	struct i40e_pf *pf = vsi->back;
5199 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5200 		i40e_down(vsi);
5201 	i40e_vsi_free_irq(vsi);
5202 	i40e_vsi_free_tx_resources(vsi);
5203 	i40e_vsi_free_rx_resources(vsi);
5204 	vsi->current_netdev_flags = 0;
5205 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5206 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5207 		set_bit(__I40E_CLIENT_RESET, pf->state);
5208 }
5209 
5210 /**
5211  * i40e_quiesce_vsi - Pause a given VSI
5212  * @vsi: the VSI being paused
5213  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)5214 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5215 {
5216 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5217 		return;
5218 
5219 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5220 	if (vsi->netdev && netif_running(vsi->netdev))
5221 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5222 	else
5223 		i40e_vsi_close(vsi);
5224 }
5225 
5226 /**
5227  * i40e_unquiesce_vsi - Resume a given VSI
5228  * @vsi: the VSI being resumed
5229  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)5230 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5231 {
5232 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5233 		return;
5234 
5235 	if (vsi->netdev && netif_running(vsi->netdev))
5236 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5237 	else
5238 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5239 }
5240 
5241 /**
5242  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5243  * @pf: the PF
5244  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)5245 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5246 {
5247 	int v;
5248 
5249 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5250 		if (pf->vsi[v])
5251 			i40e_quiesce_vsi(pf->vsi[v]);
5252 	}
5253 }
5254 
5255 /**
5256  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5257  * @pf: the PF
5258  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5259 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5260 {
5261 	int v;
5262 
5263 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5264 		if (pf->vsi[v])
5265 			i40e_unquiesce_vsi(pf->vsi[v]);
5266 	}
5267 }
5268 
5269 /**
5270  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5271  * @vsi: the VSI being configured
5272  *
5273  * Wait until all queues on a given VSI have been disabled.
5274  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5275 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5276 {
5277 	struct i40e_pf *pf = vsi->back;
5278 	int i, pf_q, ret;
5279 
5280 	pf_q = vsi->base_queue;
5281 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5282 		/* Check and wait for the Tx queue */
5283 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5284 		if (ret) {
5285 			dev_info(&pf->pdev->dev,
5286 				 "VSI seid %d Tx ring %d disable timeout\n",
5287 				 vsi->seid, pf_q);
5288 			return ret;
5289 		}
5290 
5291 		if (!i40e_enabled_xdp_vsi(vsi))
5292 			goto wait_rx;
5293 
5294 		/* Check and wait for the XDP Tx queue */
5295 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5296 				       false);
5297 		if (ret) {
5298 			dev_info(&pf->pdev->dev,
5299 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5300 				 vsi->seid, pf_q);
5301 			return ret;
5302 		}
5303 wait_rx:
5304 		/* Check and wait for the Rx queue */
5305 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5306 		if (ret) {
5307 			dev_info(&pf->pdev->dev,
5308 				 "VSI seid %d Rx ring %d disable timeout\n",
5309 				 vsi->seid, pf_q);
5310 			return ret;
5311 		}
5312 	}
5313 
5314 	return 0;
5315 }
5316 
5317 #ifdef CONFIG_I40E_DCB
5318 /**
5319  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5320  * @pf: the PF
5321  *
5322  * This function waits for the queues to be in disabled state for all the
5323  * VSIs that are managed by this PF.
5324  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5325 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5326 {
5327 	int v, ret = 0;
5328 
5329 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5330 		if (pf->vsi[v]) {
5331 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5332 			if (ret)
5333 				break;
5334 		}
5335 	}
5336 
5337 	return ret;
5338 }
5339 
5340 #endif
5341 
5342 /**
5343  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5344  * @pf: pointer to PF
5345  *
5346  * Get TC map for ISCSI PF type that will include iSCSI TC
5347  * and LAN TC.
5348  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5349 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5350 {
5351 	struct i40e_dcb_app_priority_table app;
5352 	struct i40e_hw *hw = &pf->hw;
5353 	u8 enabled_tc = 1; /* TC0 is always enabled */
5354 	u8 tc, i;
5355 	/* Get the iSCSI APP TLV */
5356 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5357 
5358 	for (i = 0; i < dcbcfg->numapps; i++) {
5359 		app = dcbcfg->app[i];
5360 		if (app.selector == I40E_APP_SEL_TCPIP &&
5361 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5362 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5363 			enabled_tc |= BIT(tc);
5364 			break;
5365 		}
5366 	}
5367 
5368 	return enabled_tc;
5369 }
5370 
5371 /**
5372  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5373  * @dcbcfg: the corresponding DCBx configuration structure
5374  *
5375  * Return the number of TCs from given DCBx configuration
5376  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5377 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5378 {
5379 	int i, tc_unused = 0;
5380 	u8 num_tc = 0;
5381 	u8 ret = 0;
5382 
5383 	/* Scan the ETS Config Priority Table to find
5384 	 * traffic class enabled for a given priority
5385 	 * and create a bitmask of enabled TCs
5386 	 */
5387 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5388 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5389 
5390 	/* Now scan the bitmask to check for
5391 	 * contiguous TCs starting with TC0
5392 	 */
5393 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5394 		if (num_tc & BIT(i)) {
5395 			if (!tc_unused) {
5396 				ret++;
5397 			} else {
5398 				pr_err("Non-contiguous TC - Disabling DCB\n");
5399 				return 1;
5400 			}
5401 		} else {
5402 			tc_unused = 1;
5403 		}
5404 	}
5405 
5406 	/* There is always at least TC0 */
5407 	if (!ret)
5408 		ret = 1;
5409 
5410 	return ret;
5411 }
5412 
5413 /**
5414  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5415  * @dcbcfg: the corresponding DCBx configuration structure
5416  *
5417  * Query the current DCB configuration and return the number of
5418  * traffic classes enabled from the given DCBX config
5419  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5420 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5421 {
5422 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5423 	u8 enabled_tc = 1;
5424 	u8 i;
5425 
5426 	for (i = 0; i < num_tc; i++)
5427 		enabled_tc |= BIT(i);
5428 
5429 	return enabled_tc;
5430 }
5431 
5432 /**
5433  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5434  * @pf: PF being queried
5435  *
5436  * Query the current MQPRIO configuration and return the number of
5437  * traffic classes enabled.
5438  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5439 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5440 {
5441 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5442 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5443 	u8 enabled_tc = 1, i;
5444 
5445 	for (i = 1; i < num_tc; i++)
5446 		enabled_tc |= BIT(i);
5447 	return enabled_tc;
5448 }
5449 
5450 /**
5451  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5452  * @pf: PF being queried
5453  *
5454  * Return number of traffic classes enabled for the given PF
5455  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5456 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5457 {
5458 	struct i40e_hw *hw = &pf->hw;
5459 	u8 i, enabled_tc = 1;
5460 	u8 num_tc = 0;
5461 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5462 
5463 	if (i40e_is_tc_mqprio_enabled(pf))
5464 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5465 
5466 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5467 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5468 		return 1;
5469 
5470 	/* SFP mode will be enabled for all TCs on port */
5471 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5472 		return i40e_dcb_get_num_tc(dcbcfg);
5473 
5474 	/* MFP mode return count of enabled TCs for this PF */
5475 	if (pf->hw.func_caps.iscsi)
5476 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5477 	else
5478 		return 1; /* Only TC0 */
5479 
5480 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5481 		if (enabled_tc & BIT(i))
5482 			num_tc++;
5483 	}
5484 	return num_tc;
5485 }
5486 
5487 /**
5488  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5489  * @pf: PF being queried
5490  *
5491  * Return a bitmap for enabled traffic classes for this PF.
5492  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5493 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5494 {
5495 	if (i40e_is_tc_mqprio_enabled(pf))
5496 		return i40e_mqprio_get_enabled_tc(pf);
5497 
5498 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5499 	 * default TC
5500 	 */
5501 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5502 		return I40E_DEFAULT_TRAFFIC_CLASS;
5503 
5504 	/* SFP mode we want PF to be enabled for all TCs */
5505 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5506 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5507 
5508 	/* MFP enabled and iSCSI PF type */
5509 	if (pf->hw.func_caps.iscsi)
5510 		return i40e_get_iscsi_tc_map(pf);
5511 	else
5512 		return I40E_DEFAULT_TRAFFIC_CLASS;
5513 }
5514 
5515 /**
5516  * i40e_vsi_get_bw_info - Query VSI BW Information
5517  * @vsi: the VSI being queried
5518  *
5519  * Returns 0 on success, negative value on failure
5520  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5521 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5522 {
5523 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5524 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5525 	struct i40e_pf *pf = vsi->back;
5526 	struct i40e_hw *hw = &pf->hw;
5527 	i40e_status ret;
5528 	u32 tc_bw_max;
5529 	int i;
5530 
5531 	/* Get the VSI level BW configuration */
5532 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5533 	if (ret) {
5534 		dev_info(&pf->pdev->dev,
5535 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5536 			 i40e_stat_str(&pf->hw, ret),
5537 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5538 		return -EINVAL;
5539 	}
5540 
5541 	/* Get the VSI level BW configuration per TC */
5542 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5543 					       NULL);
5544 	if (ret) {
5545 		dev_info(&pf->pdev->dev,
5546 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5547 			 i40e_stat_str(&pf->hw, ret),
5548 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5549 		return -EINVAL;
5550 	}
5551 
5552 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5553 		dev_info(&pf->pdev->dev,
5554 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5555 			 bw_config.tc_valid_bits,
5556 			 bw_ets_config.tc_valid_bits);
5557 		/* Still continuing */
5558 	}
5559 
5560 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5561 	vsi->bw_max_quanta = bw_config.max_bw;
5562 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5563 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5564 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5565 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5566 		vsi->bw_ets_limit_credits[i] =
5567 					le16_to_cpu(bw_ets_config.credits[i]);
5568 		/* 3 bits out of 4 for each TC */
5569 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5570 	}
5571 
5572 	return 0;
5573 }
5574 
5575 /**
5576  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5577  * @vsi: the VSI being configured
5578  * @enabled_tc: TC bitmap
5579  * @bw_share: BW shared credits per TC
5580  *
5581  * Returns 0 on success, negative value on failure
5582  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5583 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5584 				       u8 *bw_share)
5585 {
5586 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5587 	struct i40e_pf *pf = vsi->back;
5588 	i40e_status ret;
5589 	int i;
5590 
5591 	/* There is no need to reset BW when mqprio mode is on.  */
5592 	if (i40e_is_tc_mqprio_enabled(pf))
5593 		return 0;
5594 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5595 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5596 		if (ret)
5597 			dev_info(&pf->pdev->dev,
5598 				 "Failed to reset tx rate for vsi->seid %u\n",
5599 				 vsi->seid);
5600 		return ret;
5601 	}
5602 	memset(&bw_data, 0, sizeof(bw_data));
5603 	bw_data.tc_valid_bits = enabled_tc;
5604 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5605 		bw_data.tc_bw_credits[i] = bw_share[i];
5606 
5607 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5608 	if (ret) {
5609 		dev_info(&pf->pdev->dev,
5610 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5611 			 pf->hw.aq.asq_last_status);
5612 		return -EINVAL;
5613 	}
5614 
5615 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5616 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5617 
5618 	return 0;
5619 }
5620 
5621 /**
5622  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5623  * @vsi: the VSI being configured
5624  * @enabled_tc: TC map to be enabled
5625  *
5626  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5627 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5628 {
5629 	struct net_device *netdev = vsi->netdev;
5630 	struct i40e_pf *pf = vsi->back;
5631 	struct i40e_hw *hw = &pf->hw;
5632 	u8 netdev_tc = 0;
5633 	int i;
5634 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5635 
5636 	if (!netdev)
5637 		return;
5638 
5639 	if (!enabled_tc) {
5640 		netdev_reset_tc(netdev);
5641 		return;
5642 	}
5643 
5644 	/* Set up actual enabled TCs on the VSI */
5645 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5646 		return;
5647 
5648 	/* set per TC queues for the VSI */
5649 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5650 		/* Only set TC queues for enabled tcs
5651 		 *
5652 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5653 		 * enabled_tc bitmap would be 0x00001001; the driver
5654 		 * will set the numtc for netdev as 2 that will be
5655 		 * referenced by the netdev layer as TC 0 and 1.
5656 		 */
5657 		if (vsi->tc_config.enabled_tc & BIT(i))
5658 			netdev_set_tc_queue(netdev,
5659 					vsi->tc_config.tc_info[i].netdev_tc,
5660 					vsi->tc_config.tc_info[i].qcount,
5661 					vsi->tc_config.tc_info[i].qoffset);
5662 	}
5663 
5664 	if (i40e_is_tc_mqprio_enabled(pf))
5665 		return;
5666 
5667 	/* Assign UP2TC map for the VSI */
5668 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5669 		/* Get the actual TC# for the UP */
5670 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5671 		/* Get the mapped netdev TC# for the UP */
5672 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5673 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5674 	}
5675 }
5676 
5677 /**
5678  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5679  * @vsi: the VSI being configured
5680  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5681  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5682 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5683 				      struct i40e_vsi_context *ctxt)
5684 {
5685 	/* copy just the sections touched not the entire info
5686 	 * since not all sections are valid as returned by
5687 	 * update vsi params
5688 	 */
5689 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5690 	memcpy(&vsi->info.queue_mapping,
5691 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5692 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5693 	       sizeof(vsi->info.tc_mapping));
5694 }
5695 
5696 /**
5697  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5698  * @vsi: the VSI being reconfigured
5699  * @vsi_offset: offset from main VF VSI
5700  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5701 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5702 {
5703 	struct i40e_vsi_context ctxt = {};
5704 	struct i40e_pf *pf;
5705 	struct i40e_hw *hw;
5706 	int ret;
5707 
5708 	if (!vsi)
5709 		return I40E_ERR_PARAM;
5710 	pf = vsi->back;
5711 	hw = &pf->hw;
5712 
5713 	ctxt.seid = vsi->seid;
5714 	ctxt.pf_num = hw->pf_id;
5715 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5716 	ctxt.uplink_seid = vsi->uplink_seid;
5717 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5718 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5719 	ctxt.info = vsi->info;
5720 
5721 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5722 				 false);
5723 	if (vsi->reconfig_rss) {
5724 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5725 				      vsi->num_queue_pairs);
5726 		ret = i40e_vsi_config_rss(vsi);
5727 		if (ret) {
5728 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5729 			return ret;
5730 		}
5731 		vsi->reconfig_rss = false;
5732 	}
5733 
5734 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5735 	if (ret) {
5736 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5737 			 i40e_stat_str(hw, ret),
5738 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5739 		return ret;
5740 	}
5741 	/* update the local VSI info with updated queue map */
5742 	i40e_vsi_update_queue_map(vsi, &ctxt);
5743 	vsi->info.valid_sections = 0;
5744 
5745 	return ret;
5746 }
5747 
5748 /**
5749  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5750  * @vsi: VSI to be configured
5751  * @enabled_tc: TC bitmap
5752  *
5753  * This configures a particular VSI for TCs that are mapped to the
5754  * given TC bitmap. It uses default bandwidth share for TCs across
5755  * VSIs to configure TC for a particular VSI.
5756  *
5757  * NOTE:
5758  * It is expected that the VSI queues have been quisced before calling
5759  * this function.
5760  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5761 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5762 {
5763 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5764 	struct i40e_pf *pf = vsi->back;
5765 	struct i40e_hw *hw = &pf->hw;
5766 	struct i40e_vsi_context ctxt;
5767 	int ret = 0;
5768 	int i;
5769 
5770 	/* Check if enabled_tc is same as existing or new TCs */
5771 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5772 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5773 		return ret;
5774 
5775 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5776 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5777 		if (enabled_tc & BIT(i))
5778 			bw_share[i] = 1;
5779 	}
5780 
5781 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5782 	if (ret) {
5783 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5784 
5785 		dev_info(&pf->pdev->dev,
5786 			 "Failed configuring TC map %d for VSI %d\n",
5787 			 enabled_tc, vsi->seid);
5788 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5789 						  &bw_config, NULL);
5790 		if (ret) {
5791 			dev_info(&pf->pdev->dev,
5792 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5793 				 i40e_stat_str(hw, ret),
5794 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5795 			goto out;
5796 		}
5797 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5798 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5799 
5800 			if (!valid_tc)
5801 				valid_tc = bw_config.tc_valid_bits;
5802 			/* Always enable TC0, no matter what */
5803 			valid_tc |= 1;
5804 			dev_info(&pf->pdev->dev,
5805 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5806 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5807 			enabled_tc = valid_tc;
5808 		}
5809 
5810 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5811 		if (ret) {
5812 			dev_err(&pf->pdev->dev,
5813 				"Unable to  configure TC map %d for VSI %d\n",
5814 				enabled_tc, vsi->seid);
5815 			goto out;
5816 		}
5817 	}
5818 
5819 	/* Update Queue Pairs Mapping for currently enabled UPs */
5820 	ctxt.seid = vsi->seid;
5821 	ctxt.pf_num = vsi->back->hw.pf_id;
5822 	ctxt.vf_num = 0;
5823 	ctxt.uplink_seid = vsi->uplink_seid;
5824 	ctxt.info = vsi->info;
5825 	if (i40e_is_tc_mqprio_enabled(pf)) {
5826 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5827 		if (ret)
5828 			goto out;
5829 	} else {
5830 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5831 	}
5832 
5833 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5834 	 * queues changed.
5835 	 */
5836 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5837 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5838 				      vsi->num_queue_pairs);
5839 		ret = i40e_vsi_config_rss(vsi);
5840 		if (ret) {
5841 			dev_info(&vsi->back->pdev->dev,
5842 				 "Failed to reconfig rss for num_queues\n");
5843 			return ret;
5844 		}
5845 		vsi->reconfig_rss = false;
5846 	}
5847 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5848 		ctxt.info.valid_sections |=
5849 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5850 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5851 	}
5852 
5853 	/* Update the VSI after updating the VSI queue-mapping
5854 	 * information
5855 	 */
5856 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5857 	if (ret) {
5858 		dev_info(&pf->pdev->dev,
5859 			 "Update vsi tc config failed, err %s aq_err %s\n",
5860 			 i40e_stat_str(hw, ret),
5861 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5862 		goto out;
5863 	}
5864 	/* update the local VSI info with updated queue map */
5865 	i40e_vsi_update_queue_map(vsi, &ctxt);
5866 	vsi->info.valid_sections = 0;
5867 
5868 	/* Update current VSI BW information */
5869 	ret = i40e_vsi_get_bw_info(vsi);
5870 	if (ret) {
5871 		dev_info(&pf->pdev->dev,
5872 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5873 			 i40e_stat_str(hw, ret),
5874 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5875 		goto out;
5876 	}
5877 
5878 	/* Update the netdev TC setup */
5879 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5880 out:
5881 	return ret;
5882 }
5883 
5884 /**
5885  * i40e_get_link_speed - Returns link speed for the interface
5886  * @vsi: VSI to be configured
5887  *
5888  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5889 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5890 {
5891 	struct i40e_pf *pf = vsi->back;
5892 
5893 	switch (pf->hw.phy.link_info.link_speed) {
5894 	case I40E_LINK_SPEED_40GB:
5895 		return 40000;
5896 	case I40E_LINK_SPEED_25GB:
5897 		return 25000;
5898 	case I40E_LINK_SPEED_20GB:
5899 		return 20000;
5900 	case I40E_LINK_SPEED_10GB:
5901 		return 10000;
5902 	case I40E_LINK_SPEED_1GB:
5903 		return 1000;
5904 	default:
5905 		return -EINVAL;
5906 	}
5907 }
5908 
5909 /**
5910  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5911  * @vsi: Pointer to vsi structure
5912  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5913  *
5914  * Helper function to convert units before send to set BW limit
5915  **/
i40e_bw_bytes_to_mbits(struct i40e_vsi * vsi,u64 max_tx_rate)5916 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5917 {
5918 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5919 		dev_warn(&vsi->back->pdev->dev,
5920 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5921 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5922 	} else {
5923 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5924 	}
5925 
5926 	return max_tx_rate;
5927 }
5928 
5929 /**
5930  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5931  * @vsi: VSI to be configured
5932  * @seid: seid of the channel/VSI
5933  * @max_tx_rate: max TX rate to be configured as BW limit
5934  *
5935  * Helper function to set BW limit for a given VSI
5936  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5937 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5938 {
5939 	struct i40e_pf *pf = vsi->back;
5940 	u64 credits = 0;
5941 	int speed = 0;
5942 	int ret = 0;
5943 
5944 	speed = i40e_get_link_speed(vsi);
5945 	if (max_tx_rate > speed) {
5946 		dev_err(&pf->pdev->dev,
5947 			"Invalid max tx rate %llu specified for VSI seid %d.",
5948 			max_tx_rate, seid);
5949 		return -EINVAL;
5950 	}
5951 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5952 		dev_warn(&pf->pdev->dev,
5953 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5954 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5955 	}
5956 
5957 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5958 	credits = max_tx_rate;
5959 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5960 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5961 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5962 	if (ret)
5963 		dev_err(&pf->pdev->dev,
5964 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5965 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5966 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5967 	return ret;
5968 }
5969 
5970 /**
5971  * i40e_remove_queue_channels - Remove queue channels for the TCs
5972  * @vsi: VSI to be configured
5973  *
5974  * Remove queue channels for the TCs
5975  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)5976 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5977 {
5978 	enum i40e_admin_queue_err last_aq_status;
5979 	struct i40e_cloud_filter *cfilter;
5980 	struct i40e_channel *ch, *ch_tmp;
5981 	struct i40e_pf *pf = vsi->back;
5982 	struct hlist_node *node;
5983 	int ret, i;
5984 
5985 	/* Reset rss size that was stored when reconfiguring rss for
5986 	 * channel VSIs with non-power-of-2 queue count.
5987 	 */
5988 	vsi->current_rss_size = 0;
5989 
5990 	/* perform cleanup for channels if they exist */
5991 	if (list_empty(&vsi->ch_list))
5992 		return;
5993 
5994 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5995 		struct i40e_vsi *p_vsi;
5996 
5997 		list_del(&ch->list);
5998 		p_vsi = ch->parent_vsi;
5999 		if (!p_vsi || !ch->initialized) {
6000 			kfree(ch);
6001 			continue;
6002 		}
6003 		/* Reset queue contexts */
6004 		for (i = 0; i < ch->num_queue_pairs; i++) {
6005 			struct i40e_ring *tx_ring, *rx_ring;
6006 			u16 pf_q;
6007 
6008 			pf_q = ch->base_queue + i;
6009 			tx_ring = vsi->tx_rings[pf_q];
6010 			tx_ring->ch = NULL;
6011 
6012 			rx_ring = vsi->rx_rings[pf_q];
6013 			rx_ring->ch = NULL;
6014 		}
6015 
6016 		/* Reset BW configured for this VSI via mqprio */
6017 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6018 		if (ret)
6019 			dev_info(&vsi->back->pdev->dev,
6020 				 "Failed to reset tx rate for ch->seid %u\n",
6021 				 ch->seid);
6022 
6023 		/* delete cloud filters associated with this channel */
6024 		hlist_for_each_entry_safe(cfilter, node,
6025 					  &pf->cloud_filter_list, cloud_node) {
6026 			if (cfilter->seid != ch->seid)
6027 				continue;
6028 
6029 			hash_del(&cfilter->cloud_node);
6030 			if (cfilter->dst_port)
6031 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6032 									cfilter,
6033 									false);
6034 			else
6035 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6036 								false);
6037 			last_aq_status = pf->hw.aq.asq_last_status;
6038 			if (ret)
6039 				dev_info(&pf->pdev->dev,
6040 					 "Failed to delete cloud filter, err %s aq_err %s\n",
6041 					 i40e_stat_str(&pf->hw, ret),
6042 					 i40e_aq_str(&pf->hw, last_aq_status));
6043 			kfree(cfilter);
6044 		}
6045 
6046 		/* delete VSI from FW */
6047 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6048 					     NULL);
6049 		if (ret)
6050 			dev_err(&vsi->back->pdev->dev,
6051 				"unable to remove channel (%d) for parent VSI(%d)\n",
6052 				ch->seid, p_vsi->seid);
6053 		kfree(ch);
6054 	}
6055 	INIT_LIST_HEAD(&vsi->ch_list);
6056 }
6057 
6058 /**
6059  * i40e_get_max_queues_for_channel
6060  * @vsi: ptr to VSI to which channels are associated with
6061  *
6062  * Helper function which returns max value among the queue counts set on the
6063  * channels/TCs created.
6064  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)6065 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6066 {
6067 	struct i40e_channel *ch, *ch_tmp;
6068 	int max = 0;
6069 
6070 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6071 		if (!ch->initialized)
6072 			continue;
6073 		if (ch->num_queue_pairs > max)
6074 			max = ch->num_queue_pairs;
6075 	}
6076 
6077 	return max;
6078 }
6079 
6080 /**
6081  * i40e_validate_num_queues - validate num_queues w.r.t channel
6082  * @pf: ptr to PF device
6083  * @num_queues: number of queues
6084  * @vsi: the parent VSI
6085  * @reconfig_rss: indicates should the RSS be reconfigured or not
6086  *
6087  * This function validates number of queues in the context of new channel
6088  * which is being established and determines if RSS should be reconfigured
6089  * or not for parent VSI.
6090  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)6091 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6092 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6093 {
6094 	int max_ch_queues;
6095 
6096 	if (!reconfig_rss)
6097 		return -EINVAL;
6098 
6099 	*reconfig_rss = false;
6100 	if (vsi->current_rss_size) {
6101 		if (num_queues > vsi->current_rss_size) {
6102 			dev_dbg(&pf->pdev->dev,
6103 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6104 				num_queues, vsi->current_rss_size);
6105 			return -EINVAL;
6106 		} else if ((num_queues < vsi->current_rss_size) &&
6107 			   (!is_power_of_2(num_queues))) {
6108 			dev_dbg(&pf->pdev->dev,
6109 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6110 				num_queues, vsi->current_rss_size);
6111 			return -EINVAL;
6112 		}
6113 	}
6114 
6115 	if (!is_power_of_2(num_queues)) {
6116 		/* Find the max num_queues configured for channel if channel
6117 		 * exist.
6118 		 * if channel exist, then enforce 'num_queues' to be more than
6119 		 * max ever queues configured for channel.
6120 		 */
6121 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6122 		if (num_queues < max_ch_queues) {
6123 			dev_dbg(&pf->pdev->dev,
6124 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6125 				num_queues, max_ch_queues);
6126 			return -EINVAL;
6127 		}
6128 		*reconfig_rss = true;
6129 	}
6130 
6131 	return 0;
6132 }
6133 
6134 /**
6135  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6136  * @vsi: the VSI being setup
6137  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6138  *
6139  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6140  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)6141 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6142 {
6143 	struct i40e_pf *pf = vsi->back;
6144 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6145 	struct i40e_hw *hw = &pf->hw;
6146 	int local_rss_size;
6147 	u8 *lut;
6148 	int ret;
6149 
6150 	if (!vsi->rss_size)
6151 		return -EINVAL;
6152 
6153 	if (rss_size > vsi->rss_size)
6154 		return -EINVAL;
6155 
6156 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6157 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6158 	if (!lut)
6159 		return -ENOMEM;
6160 
6161 	/* Ignoring user configured lut if there is one */
6162 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6163 
6164 	/* Use user configured hash key if there is one, otherwise
6165 	 * use default.
6166 	 */
6167 	if (vsi->rss_hkey_user)
6168 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6169 	else
6170 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6171 
6172 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6173 	if (ret) {
6174 		dev_info(&pf->pdev->dev,
6175 			 "Cannot set RSS lut, err %s aq_err %s\n",
6176 			 i40e_stat_str(hw, ret),
6177 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6178 		kfree(lut);
6179 		return ret;
6180 	}
6181 	kfree(lut);
6182 
6183 	/* Do the update w.r.t. storing rss_size */
6184 	if (!vsi->orig_rss_size)
6185 		vsi->orig_rss_size = vsi->rss_size;
6186 	vsi->current_rss_size = local_rss_size;
6187 
6188 	return ret;
6189 }
6190 
6191 /**
6192  * i40e_channel_setup_queue_map - Setup a channel queue map
6193  * @pf: ptr to PF device
6194  * @ctxt: VSI context structure
6195  * @ch: ptr to channel structure
6196  *
6197  * Setup queue map for a specific channel
6198  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)6199 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6200 					 struct i40e_vsi_context *ctxt,
6201 					 struct i40e_channel *ch)
6202 {
6203 	u16 qcount, qmap, sections = 0;
6204 	u8 offset = 0;
6205 	int pow;
6206 
6207 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6208 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6209 
6210 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6211 	ch->num_queue_pairs = qcount;
6212 
6213 	/* find the next higher power-of-2 of num queue pairs */
6214 	pow = ilog2(qcount);
6215 	if (!is_power_of_2(qcount))
6216 		pow++;
6217 
6218 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6219 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6220 
6221 	/* Setup queue TC[0].qmap for given VSI context */
6222 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6223 
6224 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6225 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6226 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6227 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6228 }
6229 
6230 /**
6231  * i40e_add_channel - add a channel by adding VSI
6232  * @pf: ptr to PF device
6233  * @uplink_seid: underlying HW switching element (VEB) ID
6234  * @ch: ptr to channel structure
6235  *
6236  * Add a channel (VSI) using add_vsi and queue_map
6237  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)6238 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6239 			    struct i40e_channel *ch)
6240 {
6241 	struct i40e_hw *hw = &pf->hw;
6242 	struct i40e_vsi_context ctxt;
6243 	u8 enabled_tc = 0x1; /* TC0 enabled */
6244 	int ret;
6245 
6246 	if (ch->type != I40E_VSI_VMDQ2) {
6247 		dev_info(&pf->pdev->dev,
6248 			 "add new vsi failed, ch->type %d\n", ch->type);
6249 		return -EINVAL;
6250 	}
6251 
6252 	memset(&ctxt, 0, sizeof(ctxt));
6253 	ctxt.pf_num = hw->pf_id;
6254 	ctxt.vf_num = 0;
6255 	ctxt.uplink_seid = uplink_seid;
6256 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6257 	if (ch->type == I40E_VSI_VMDQ2)
6258 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6259 
6260 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6261 		ctxt.info.valid_sections |=
6262 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6263 		ctxt.info.switch_id =
6264 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6265 	}
6266 
6267 	/* Set queue map for a given VSI context */
6268 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6269 
6270 	/* Now time to create VSI */
6271 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6272 	if (ret) {
6273 		dev_info(&pf->pdev->dev,
6274 			 "add new vsi failed, err %s aq_err %s\n",
6275 			 i40e_stat_str(&pf->hw, ret),
6276 			 i40e_aq_str(&pf->hw,
6277 				     pf->hw.aq.asq_last_status));
6278 		return -ENOENT;
6279 	}
6280 
6281 	/* Success, update channel, set enabled_tc only if the channel
6282 	 * is not a macvlan
6283 	 */
6284 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6285 	ch->seid = ctxt.seid;
6286 	ch->vsi_number = ctxt.vsi_number;
6287 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6288 
6289 	/* copy just the sections touched not the entire info
6290 	 * since not all sections are valid as returned by
6291 	 * update vsi params
6292 	 */
6293 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6294 	memcpy(&ch->info.queue_mapping,
6295 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6296 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6297 	       sizeof(ctxt.info.tc_mapping));
6298 
6299 	return 0;
6300 }
6301 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6302 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6303 				  u8 *bw_share)
6304 {
6305 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6306 	i40e_status ret;
6307 	int i;
6308 
6309 	memset(&bw_data, 0, sizeof(bw_data));
6310 	bw_data.tc_valid_bits = ch->enabled_tc;
6311 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6312 		bw_data.tc_bw_credits[i] = bw_share[i];
6313 
6314 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6315 				       &bw_data, NULL);
6316 	if (ret) {
6317 		dev_info(&vsi->back->pdev->dev,
6318 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6319 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6320 		return -EINVAL;
6321 	}
6322 
6323 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6324 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6325 
6326 	return 0;
6327 }
6328 
6329 /**
6330  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6331  * @pf: ptr to PF device
6332  * @vsi: the VSI being setup
6333  * @ch: ptr to channel structure
6334  *
6335  * Configure TX rings associated with channel (VSI) since queues are being
6336  * from parent VSI.
6337  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6338 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6339 				       struct i40e_vsi *vsi,
6340 				       struct i40e_channel *ch)
6341 {
6342 	i40e_status ret;
6343 	int i;
6344 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6345 
6346 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6347 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6348 		if (ch->enabled_tc & BIT(i))
6349 			bw_share[i] = 1;
6350 	}
6351 
6352 	/* configure BW for new VSI */
6353 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6354 	if (ret) {
6355 		dev_info(&vsi->back->pdev->dev,
6356 			 "Failed configuring TC map %d for channel (seid %u)\n",
6357 			 ch->enabled_tc, ch->seid);
6358 		return ret;
6359 	}
6360 
6361 	for (i = 0; i < ch->num_queue_pairs; i++) {
6362 		struct i40e_ring *tx_ring, *rx_ring;
6363 		u16 pf_q;
6364 
6365 		pf_q = ch->base_queue + i;
6366 
6367 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6368 		 * context
6369 		 */
6370 		tx_ring = vsi->tx_rings[pf_q];
6371 		tx_ring->ch = ch;
6372 
6373 		/* Get the RX ring ptr */
6374 		rx_ring = vsi->rx_rings[pf_q];
6375 		rx_ring->ch = ch;
6376 	}
6377 
6378 	return 0;
6379 }
6380 
6381 /**
6382  * i40e_setup_hw_channel - setup new channel
6383  * @pf: ptr to PF device
6384  * @vsi: the VSI being setup
6385  * @ch: ptr to channel structure
6386  * @uplink_seid: underlying HW switching element (VEB) ID
6387  * @type: type of channel to be created (VMDq2/VF)
6388  *
6389  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6390  * and configures TX rings accordingly
6391  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6392 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6393 					struct i40e_vsi *vsi,
6394 					struct i40e_channel *ch,
6395 					u16 uplink_seid, u8 type)
6396 {
6397 	int ret;
6398 
6399 	ch->initialized = false;
6400 	ch->base_queue = vsi->next_base_queue;
6401 	ch->type = type;
6402 
6403 	/* Proceed with creation of channel (VMDq2) VSI */
6404 	ret = i40e_add_channel(pf, uplink_seid, ch);
6405 	if (ret) {
6406 		dev_info(&pf->pdev->dev,
6407 			 "failed to add_channel using uplink_seid %u\n",
6408 			 uplink_seid);
6409 		return ret;
6410 	}
6411 
6412 	/* Mark the successful creation of channel */
6413 	ch->initialized = true;
6414 
6415 	/* Reconfigure TX queues using QTX_CTL register */
6416 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6417 	if (ret) {
6418 		dev_info(&pf->pdev->dev,
6419 			 "failed to configure TX rings for channel %u\n",
6420 			 ch->seid);
6421 		return ret;
6422 	}
6423 
6424 	/* update 'next_base_queue' */
6425 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6426 	dev_dbg(&pf->pdev->dev,
6427 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6428 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6429 		ch->num_queue_pairs,
6430 		vsi->next_base_queue);
6431 	return ret;
6432 }
6433 
6434 /**
6435  * i40e_setup_channel - setup new channel using uplink element
6436  * @pf: ptr to PF device
6437  * @vsi: pointer to the VSI to set up the channel within
6438  * @ch: ptr to channel structure
6439  *
6440  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6441  * and uplink switching element (uplink_seid)
6442  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6443 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6444 			       struct i40e_channel *ch)
6445 {
6446 	u8 vsi_type;
6447 	u16 seid;
6448 	int ret;
6449 
6450 	if (vsi->type == I40E_VSI_MAIN) {
6451 		vsi_type = I40E_VSI_VMDQ2;
6452 	} else {
6453 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6454 			vsi->type);
6455 		return false;
6456 	}
6457 
6458 	/* underlying switching element */
6459 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6460 
6461 	/* create channel (VSI), configure TX rings */
6462 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6463 	if (ret) {
6464 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6465 		return false;
6466 	}
6467 
6468 	return ch->initialized ? true : false;
6469 }
6470 
6471 /**
6472  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6473  * @vsi: ptr to VSI which has PF backing
6474  *
6475  * Sets up switch mode correctly if it needs to be changed and perform
6476  * what are allowed modes.
6477  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6478 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6479 {
6480 	u8 mode;
6481 	struct i40e_pf *pf = vsi->back;
6482 	struct i40e_hw *hw = &pf->hw;
6483 	int ret;
6484 
6485 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6486 	if (ret)
6487 		return -EINVAL;
6488 
6489 	if (hw->dev_caps.switch_mode) {
6490 		/* if switch mode is set, support mode2 (non-tunneled for
6491 		 * cloud filter) for now
6492 		 */
6493 		u32 switch_mode = hw->dev_caps.switch_mode &
6494 				  I40E_SWITCH_MODE_MASK;
6495 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6496 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6497 				return 0;
6498 			dev_err(&pf->pdev->dev,
6499 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6500 				hw->dev_caps.switch_mode);
6501 			return -EINVAL;
6502 		}
6503 	}
6504 
6505 	/* Set Bit 7 to be valid */
6506 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6507 
6508 	/* Set L4type for TCP support */
6509 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6510 
6511 	/* Set cloud filter mode */
6512 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6513 
6514 	/* Prep mode field for set_switch_config */
6515 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6516 					pf->last_sw_conf_valid_flags,
6517 					mode, NULL);
6518 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6519 		dev_err(&pf->pdev->dev,
6520 			"couldn't set switch config bits, err %s aq_err %s\n",
6521 			i40e_stat_str(hw, ret),
6522 			i40e_aq_str(hw,
6523 				    hw->aq.asq_last_status));
6524 
6525 	return ret;
6526 }
6527 
6528 /**
6529  * i40e_create_queue_channel - function to create channel
6530  * @vsi: VSI to be configured
6531  * @ch: ptr to channel (it contains channel specific params)
6532  *
6533  * This function creates channel (VSI) using num_queues specified by user,
6534  * reconfigs RSS if needed.
6535  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6536 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6537 			      struct i40e_channel *ch)
6538 {
6539 	struct i40e_pf *pf = vsi->back;
6540 	bool reconfig_rss;
6541 	int err;
6542 
6543 	if (!ch)
6544 		return -EINVAL;
6545 
6546 	if (!ch->num_queue_pairs) {
6547 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6548 			ch->num_queue_pairs);
6549 		return -EINVAL;
6550 	}
6551 
6552 	/* validate user requested num_queues for channel */
6553 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6554 				       &reconfig_rss);
6555 	if (err) {
6556 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6557 			 ch->num_queue_pairs);
6558 		return -EINVAL;
6559 	}
6560 
6561 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6562 	 * VSI to be added switch to VEB mode.
6563 	 */
6564 
6565 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6566 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6567 
6568 		if (vsi->type == I40E_VSI_MAIN) {
6569 			if (i40e_is_tc_mqprio_enabled(pf))
6570 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6571 			else
6572 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6573 		}
6574 		/* now onwards for main VSI, number of queues will be value
6575 		 * of TC0's queue count
6576 		 */
6577 	}
6578 
6579 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6580 	 * it should be more than num_queues
6581 	 */
6582 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6583 		dev_dbg(&pf->pdev->dev,
6584 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6585 			vsi->cnt_q_avail, ch->num_queue_pairs);
6586 		return -EINVAL;
6587 	}
6588 
6589 	/* reconfig_rss only if vsi type is MAIN_VSI */
6590 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6591 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6592 		if (err) {
6593 			dev_info(&pf->pdev->dev,
6594 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6595 				 ch->num_queue_pairs);
6596 			return -EINVAL;
6597 		}
6598 	}
6599 
6600 	if (!i40e_setup_channel(pf, vsi, ch)) {
6601 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6602 		return -EINVAL;
6603 	}
6604 
6605 	dev_info(&pf->pdev->dev,
6606 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6607 		 ch->seid, ch->num_queue_pairs);
6608 
6609 	/* configure VSI for BW limit */
6610 	if (ch->max_tx_rate) {
6611 		u64 credits = ch->max_tx_rate;
6612 
6613 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6614 			return -EINVAL;
6615 
6616 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6617 		dev_dbg(&pf->pdev->dev,
6618 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6619 			ch->max_tx_rate,
6620 			credits,
6621 			ch->seid);
6622 	}
6623 
6624 	/* in case of VF, this will be main SRIOV VSI */
6625 	ch->parent_vsi = vsi;
6626 
6627 	/* and update main_vsi's count for queue_available to use */
6628 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6629 
6630 	return 0;
6631 }
6632 
6633 /**
6634  * i40e_configure_queue_channels - Add queue channel for the given TCs
6635  * @vsi: VSI to be configured
6636  *
6637  * Configures queue channel mapping to the given TCs
6638  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6639 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6640 {
6641 	struct i40e_channel *ch;
6642 	u64 max_rate = 0;
6643 	int ret = 0, i;
6644 
6645 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6646 	vsi->tc_seid_map[0] = vsi->seid;
6647 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6648 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6649 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6650 			if (!ch) {
6651 				ret = -ENOMEM;
6652 				goto err_free;
6653 			}
6654 
6655 			INIT_LIST_HEAD(&ch->list);
6656 			ch->num_queue_pairs =
6657 				vsi->tc_config.tc_info[i].qcount;
6658 			ch->base_queue =
6659 				vsi->tc_config.tc_info[i].qoffset;
6660 
6661 			/* Bandwidth limit through tc interface is in bytes/s,
6662 			 * change to Mbit/s
6663 			 */
6664 			max_rate = vsi->mqprio_qopt.max_rate[i];
6665 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6666 			ch->max_tx_rate = max_rate;
6667 
6668 			list_add_tail(&ch->list, &vsi->ch_list);
6669 
6670 			ret = i40e_create_queue_channel(vsi, ch);
6671 			if (ret) {
6672 				dev_err(&vsi->back->pdev->dev,
6673 					"Failed creating queue channel with TC%d: queues %d\n",
6674 					i, ch->num_queue_pairs);
6675 				goto err_free;
6676 			}
6677 			vsi->tc_seid_map[i] = ch->seid;
6678 		}
6679 	}
6680 
6681 	/* reset to reconfigure TX queue contexts */
6682 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6683 	return ret;
6684 
6685 err_free:
6686 	i40e_remove_queue_channels(vsi);
6687 	return ret;
6688 }
6689 
6690 /**
6691  * i40e_veb_config_tc - Configure TCs for given VEB
6692  * @veb: given VEB
6693  * @enabled_tc: TC bitmap
6694  *
6695  * Configures given TC bitmap for VEB (switching) element
6696  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6697 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6698 {
6699 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6700 	struct i40e_pf *pf = veb->pf;
6701 	int ret = 0;
6702 	int i;
6703 
6704 	/* No TCs or already enabled TCs just return */
6705 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6706 		return ret;
6707 
6708 	bw_data.tc_valid_bits = enabled_tc;
6709 	/* bw_data.absolute_credits is not set (relative) */
6710 
6711 	/* Enable ETS TCs with equal BW Share for now */
6712 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6713 		if (enabled_tc & BIT(i))
6714 			bw_data.tc_bw_share_credits[i] = 1;
6715 	}
6716 
6717 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6718 						   &bw_data, NULL);
6719 	if (ret) {
6720 		dev_info(&pf->pdev->dev,
6721 			 "VEB bw config failed, err %s aq_err %s\n",
6722 			 i40e_stat_str(&pf->hw, ret),
6723 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6724 		goto out;
6725 	}
6726 
6727 	/* Update the BW information */
6728 	ret = i40e_veb_get_bw_info(veb);
6729 	if (ret) {
6730 		dev_info(&pf->pdev->dev,
6731 			 "Failed getting veb bw config, err %s aq_err %s\n",
6732 			 i40e_stat_str(&pf->hw, ret),
6733 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6734 	}
6735 
6736 out:
6737 	return ret;
6738 }
6739 
6740 #ifdef CONFIG_I40E_DCB
6741 /**
6742  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6743  * @pf: PF struct
6744  *
6745  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6746  * the caller would've quiesce all the VSIs before calling
6747  * this function
6748  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6749 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6750 {
6751 	u8 tc_map = 0;
6752 	int ret;
6753 	u8 v;
6754 
6755 	/* Enable the TCs available on PF to all VEBs */
6756 	tc_map = i40e_pf_get_tc_map(pf);
6757 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6758 		return;
6759 
6760 	for (v = 0; v < I40E_MAX_VEB; v++) {
6761 		if (!pf->veb[v])
6762 			continue;
6763 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6764 		if (ret) {
6765 			dev_info(&pf->pdev->dev,
6766 				 "Failed configuring TC for VEB seid=%d\n",
6767 				 pf->veb[v]->seid);
6768 			/* Will try to configure as many components */
6769 		}
6770 	}
6771 
6772 	/* Update each VSI */
6773 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6774 		if (!pf->vsi[v])
6775 			continue;
6776 
6777 		/* - Enable all TCs for the LAN VSI
6778 		 * - For all others keep them at TC0 for now
6779 		 */
6780 		if (v == pf->lan_vsi)
6781 			tc_map = i40e_pf_get_tc_map(pf);
6782 		else
6783 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6784 
6785 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6786 		if (ret) {
6787 			dev_info(&pf->pdev->dev,
6788 				 "Failed configuring TC for VSI seid=%d\n",
6789 				 pf->vsi[v]->seid);
6790 			/* Will try to configure as many components */
6791 		} else {
6792 			/* Re-configure VSI vectors based on updated TC map */
6793 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6794 			if (pf->vsi[v]->netdev)
6795 				i40e_dcbnl_set_all(pf->vsi[v]);
6796 		}
6797 	}
6798 }
6799 
6800 /**
6801  * i40e_resume_port_tx - Resume port Tx
6802  * @pf: PF struct
6803  *
6804  * Resume a port's Tx and issue a PF reset in case of failure to
6805  * resume.
6806  **/
i40e_resume_port_tx(struct i40e_pf * pf)6807 static int i40e_resume_port_tx(struct i40e_pf *pf)
6808 {
6809 	struct i40e_hw *hw = &pf->hw;
6810 	int ret;
6811 
6812 	ret = i40e_aq_resume_port_tx(hw, NULL);
6813 	if (ret) {
6814 		dev_info(&pf->pdev->dev,
6815 			 "Resume Port Tx failed, err %s aq_err %s\n",
6816 			  i40e_stat_str(&pf->hw, ret),
6817 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6818 		/* Schedule PF reset to recover */
6819 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6820 		i40e_service_event_schedule(pf);
6821 	}
6822 
6823 	return ret;
6824 }
6825 
6826 /**
6827  * i40e_suspend_port_tx - Suspend port Tx
6828  * @pf: PF struct
6829  *
6830  * Suspend a port's Tx and issue a PF reset in case of failure.
6831  **/
i40e_suspend_port_tx(struct i40e_pf * pf)6832 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6833 {
6834 	struct i40e_hw *hw = &pf->hw;
6835 	int ret;
6836 
6837 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6838 	if (ret) {
6839 		dev_info(&pf->pdev->dev,
6840 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6841 			 i40e_stat_str(&pf->hw, ret),
6842 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6843 		/* Schedule PF reset to recover */
6844 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6845 		i40e_service_event_schedule(pf);
6846 	}
6847 
6848 	return ret;
6849 }
6850 
6851 /**
6852  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6853  * @pf: PF being configured
6854  * @new_cfg: New DCBX configuration
6855  *
6856  * Program DCB settings into HW and reconfigure VEB/VSIs on
6857  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6858  **/
i40e_hw_set_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6859 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6860 				  struct i40e_dcbx_config *new_cfg)
6861 {
6862 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6863 	int ret;
6864 
6865 	/* Check if need reconfiguration */
6866 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6867 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6868 		return 0;
6869 	}
6870 
6871 	/* Config change disable all VSIs */
6872 	i40e_pf_quiesce_all_vsi(pf);
6873 
6874 	/* Copy the new config to the current config */
6875 	*old_cfg = *new_cfg;
6876 	old_cfg->etsrec = old_cfg->etscfg;
6877 	ret = i40e_set_dcb_config(&pf->hw);
6878 	if (ret) {
6879 		dev_info(&pf->pdev->dev,
6880 			 "Set DCB Config failed, err %s aq_err %s\n",
6881 			 i40e_stat_str(&pf->hw, ret),
6882 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6883 		goto out;
6884 	}
6885 
6886 	/* Changes in configuration update VEB/VSI */
6887 	i40e_dcb_reconfigure(pf);
6888 out:
6889 	/* In case of reset do not try to resume anything */
6890 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6891 		/* Re-start the VSIs if disabled */
6892 		ret = i40e_resume_port_tx(pf);
6893 		/* In case of error no point in resuming VSIs */
6894 		if (ret)
6895 			goto err;
6896 		i40e_pf_unquiesce_all_vsi(pf);
6897 	}
6898 err:
6899 	return ret;
6900 }
6901 
6902 /**
6903  * i40e_hw_dcb_config - Program new DCBX settings into HW
6904  * @pf: PF being configured
6905  * @new_cfg: New DCBX configuration
6906  *
6907  * Program DCB settings into HW and reconfigure VEB/VSIs on
6908  * given PF
6909  **/
i40e_hw_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6910 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6911 {
6912 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6913 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6914 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6915 	struct i40e_dcbx_config *old_cfg;
6916 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6917 	struct i40e_rx_pb_config pb_cfg;
6918 	struct i40e_hw *hw = &pf->hw;
6919 	u8 num_ports = hw->num_ports;
6920 	bool need_reconfig;
6921 	int ret = -EINVAL;
6922 	u8 lltc_map = 0;
6923 	u8 tc_map = 0;
6924 	u8 new_numtc;
6925 	u8 i;
6926 
6927 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6928 	/* Un-pack information to Program ETS HW via shared API
6929 	 * numtc, tcmap
6930 	 * LLTC map
6931 	 * ETS/NON-ETS arbiter mode
6932 	 * max exponent (credit refills)
6933 	 * Total number of ports
6934 	 * PFC priority bit-map
6935 	 * Priority Table
6936 	 * BW % per TC
6937 	 * Arbiter mode between UPs sharing same TC
6938 	 * TSA table (ETS or non-ETS)
6939 	 * EEE enabled or not
6940 	 * MFS TC table
6941 	 */
6942 
6943 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6944 
6945 	memset(&ets_data, 0, sizeof(ets_data));
6946 	for (i = 0; i < new_numtc; i++) {
6947 		tc_map |= BIT(i);
6948 		switch (new_cfg->etscfg.tsatable[i]) {
6949 		case I40E_IEEE_TSA_ETS:
6950 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6951 			ets_data.tc_bw_share_credits[i] =
6952 					new_cfg->etscfg.tcbwtable[i];
6953 			break;
6954 		case I40E_IEEE_TSA_STRICT:
6955 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6956 			lltc_map |= BIT(i);
6957 			ets_data.tc_bw_share_credits[i] =
6958 					I40E_DCB_STRICT_PRIO_CREDITS;
6959 			break;
6960 		default:
6961 			/* Invalid TSA type */
6962 			need_reconfig = false;
6963 			goto out;
6964 		}
6965 	}
6966 
6967 	old_cfg = &hw->local_dcbx_config;
6968 	/* Check if need reconfiguration */
6969 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6970 
6971 	/* If needed, enable/disable frame tagging, disable all VSIs
6972 	 * and suspend port tx
6973 	 */
6974 	if (need_reconfig) {
6975 		/* Enable DCB tagging only when more than one TC */
6976 		if (new_numtc > 1)
6977 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6978 		else
6979 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6980 
6981 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6982 		/* Reconfiguration needed quiesce all VSIs */
6983 		i40e_pf_quiesce_all_vsi(pf);
6984 		ret = i40e_suspend_port_tx(pf);
6985 		if (ret)
6986 			goto err;
6987 	}
6988 
6989 	/* Configure Port ETS Tx Scheduler */
6990 	ets_data.tc_valid_bits = tc_map;
6991 	ets_data.tc_strict_priority_flags = lltc_map;
6992 	ret = i40e_aq_config_switch_comp_ets
6993 		(hw, pf->mac_seid, &ets_data,
6994 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6995 	if (ret) {
6996 		dev_info(&pf->pdev->dev,
6997 			 "Modify Port ETS failed, err %s aq_err %s\n",
6998 			 i40e_stat_str(&pf->hw, ret),
6999 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7000 		goto out;
7001 	}
7002 
7003 	/* Configure Rx ETS HW */
7004 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7005 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7006 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7007 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7008 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7009 				   lltc_map);
7010 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7011 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7012 				     prio_type);
7013 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7014 			       new_cfg->etscfg.prioritytable);
7015 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7016 
7017 	/* Configure Rx Packet Buffers in HW */
7018 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7019 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7020 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7021 	}
7022 
7023 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7024 					 false, new_cfg->pfc.pfcenable,
7025 					 mfs_tc, &pb_cfg);
7026 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7027 
7028 	/* Update the local Rx Packet buffer config */
7029 	pf->pb_cfg = pb_cfg;
7030 
7031 	/* Inform the FW about changes to DCB configuration */
7032 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7033 	if (ret) {
7034 		dev_info(&pf->pdev->dev,
7035 			 "DCB Updated failed, err %s aq_err %s\n",
7036 			 i40e_stat_str(&pf->hw, ret),
7037 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7038 		goto out;
7039 	}
7040 
7041 	/* Update the port DCBx configuration */
7042 	*old_cfg = *new_cfg;
7043 
7044 	/* Changes in configuration update VEB/VSI */
7045 	i40e_dcb_reconfigure(pf);
7046 out:
7047 	/* Re-start the VSIs if disabled */
7048 	if (need_reconfig) {
7049 		ret = i40e_resume_port_tx(pf);
7050 
7051 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7052 		/* In case of error no point in resuming VSIs */
7053 		if (ret)
7054 			goto err;
7055 
7056 		/* Wait for the PF's queues to be disabled */
7057 		ret = i40e_pf_wait_queues_disabled(pf);
7058 		if (ret) {
7059 			/* Schedule PF reset to recover */
7060 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7061 			i40e_service_event_schedule(pf);
7062 			goto err;
7063 		} else {
7064 			i40e_pf_unquiesce_all_vsi(pf);
7065 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7066 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7067 		}
7068 		/* registers are set, lets apply */
7069 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7070 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7071 	}
7072 
7073 err:
7074 	return ret;
7075 }
7076 
7077 /**
7078  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7079  * @pf: PF being queried
7080  *
7081  * Set default DCB configuration in case DCB is to be done in SW.
7082  **/
i40e_dcb_sw_default_config(struct i40e_pf * pf)7083 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7084 {
7085 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7086 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7087 	struct i40e_hw *hw = &pf->hw;
7088 	int err;
7089 
7090 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7091 		/* Update the local cached instance with TC0 ETS */
7092 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7093 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7094 		pf->tmp_cfg.etscfg.maxtcs = 0;
7095 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7096 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7097 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7098 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7099 		/* FW needs one App to configure HW */
7100 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7101 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7102 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7103 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7104 
7105 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7106 	}
7107 
7108 	memset(&ets_data, 0, sizeof(ets_data));
7109 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7110 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7111 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7112 
7113 	/* Enable ETS on the Physical port */
7114 	err = i40e_aq_config_switch_comp_ets
7115 		(hw, pf->mac_seid, &ets_data,
7116 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7117 	if (err) {
7118 		dev_info(&pf->pdev->dev,
7119 			 "Enable Port ETS failed, err %s aq_err %s\n",
7120 			 i40e_stat_str(&pf->hw, err),
7121 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7122 		err = -ENOENT;
7123 		goto out;
7124 	}
7125 
7126 	/* Update the local cached instance with TC0 ETS */
7127 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7128 	dcb_cfg->etscfg.cbs = 0;
7129 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7130 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7131 
7132 out:
7133 	return err;
7134 }
7135 
7136 /**
7137  * i40e_init_pf_dcb - Initialize DCB configuration
7138  * @pf: PF being configured
7139  *
7140  * Query the current DCB configuration and cache it
7141  * in the hardware structure
7142  **/
i40e_init_pf_dcb(struct i40e_pf * pf)7143 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7144 {
7145 	struct i40e_hw *hw = &pf->hw;
7146 	int err;
7147 
7148 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7149 	 * Also do not enable DCBx if FW LLDP agent is disabled
7150 	 */
7151 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7152 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7153 		err = I40E_NOT_SUPPORTED;
7154 		goto out;
7155 	}
7156 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7157 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7158 		err = i40e_dcb_sw_default_config(pf);
7159 		if (err) {
7160 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7161 			goto out;
7162 		}
7163 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7164 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7165 			       DCB_CAP_DCBX_VER_IEEE;
7166 		/* at init capable but disabled */
7167 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7168 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7169 		goto out;
7170 	}
7171 	err = i40e_init_dcb(hw, true);
7172 	if (!err) {
7173 		/* Device/Function is not DCBX capable */
7174 		if ((!hw->func_caps.dcb) ||
7175 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7176 			dev_info(&pf->pdev->dev,
7177 				 "DCBX offload is not supported or is disabled for this PF.\n");
7178 		} else {
7179 			/* When status is not DISABLED then DCBX in FW */
7180 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7181 				       DCB_CAP_DCBX_VER_IEEE;
7182 
7183 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7184 			/* Enable DCB tagging only when more than one TC
7185 			 * or explicitly disable if only one TC
7186 			 */
7187 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7188 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7189 			else
7190 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7191 			dev_dbg(&pf->pdev->dev,
7192 				"DCBX offload is supported for this PF.\n");
7193 		}
7194 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7195 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7196 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7197 	} else {
7198 		dev_info(&pf->pdev->dev,
7199 			 "Query for DCB configuration failed, err %s aq_err %s\n",
7200 			 i40e_stat_str(&pf->hw, err),
7201 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7202 	}
7203 
7204 out:
7205 	return err;
7206 }
7207 #endif /* CONFIG_I40E_DCB */
7208 
7209 /**
7210  * i40e_print_link_message - print link up or down
7211  * @vsi: the VSI for which link needs a message
7212  * @isup: true of link is up, false otherwise
7213  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)7214 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7215 {
7216 	enum i40e_aq_link_speed new_speed;
7217 	struct i40e_pf *pf = vsi->back;
7218 	char *speed = "Unknown";
7219 	char *fc = "Unknown";
7220 	char *fec = "";
7221 	char *req_fec = "";
7222 	char *an = "";
7223 
7224 	if (isup)
7225 		new_speed = pf->hw.phy.link_info.link_speed;
7226 	else
7227 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7228 
7229 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7230 		return;
7231 	vsi->current_isup = isup;
7232 	vsi->current_speed = new_speed;
7233 	if (!isup) {
7234 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7235 		return;
7236 	}
7237 
7238 	/* Warn user if link speed on NPAR enabled partition is not at
7239 	 * least 10GB
7240 	 */
7241 	if (pf->hw.func_caps.npar_enable &&
7242 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7243 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7244 		netdev_warn(vsi->netdev,
7245 			    "The partition detected link speed that is less than 10Gbps\n");
7246 
7247 	switch (pf->hw.phy.link_info.link_speed) {
7248 	case I40E_LINK_SPEED_40GB:
7249 		speed = "40 G";
7250 		break;
7251 	case I40E_LINK_SPEED_20GB:
7252 		speed = "20 G";
7253 		break;
7254 	case I40E_LINK_SPEED_25GB:
7255 		speed = "25 G";
7256 		break;
7257 	case I40E_LINK_SPEED_10GB:
7258 		speed = "10 G";
7259 		break;
7260 	case I40E_LINK_SPEED_5GB:
7261 		speed = "5 G";
7262 		break;
7263 	case I40E_LINK_SPEED_2_5GB:
7264 		speed = "2.5 G";
7265 		break;
7266 	case I40E_LINK_SPEED_1GB:
7267 		speed = "1000 M";
7268 		break;
7269 	case I40E_LINK_SPEED_100MB:
7270 		speed = "100 M";
7271 		break;
7272 	default:
7273 		break;
7274 	}
7275 
7276 	switch (pf->hw.fc.current_mode) {
7277 	case I40E_FC_FULL:
7278 		fc = "RX/TX";
7279 		break;
7280 	case I40E_FC_TX_PAUSE:
7281 		fc = "TX";
7282 		break;
7283 	case I40E_FC_RX_PAUSE:
7284 		fc = "RX";
7285 		break;
7286 	default:
7287 		fc = "None";
7288 		break;
7289 	}
7290 
7291 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7292 		req_fec = "None";
7293 		fec = "None";
7294 		an = "False";
7295 
7296 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7297 			an = "True";
7298 
7299 		if (pf->hw.phy.link_info.fec_info &
7300 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7301 			fec = "CL74 FC-FEC/BASE-R";
7302 		else if (pf->hw.phy.link_info.fec_info &
7303 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7304 			fec = "CL108 RS-FEC";
7305 
7306 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7307 		 * both RS and FC are requested
7308 		 */
7309 		if (vsi->back->hw.phy.link_info.req_fec_info &
7310 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7311 			if (vsi->back->hw.phy.link_info.req_fec_info &
7312 			    I40E_AQ_REQUEST_FEC_RS)
7313 				req_fec = "CL108 RS-FEC";
7314 			else
7315 				req_fec = "CL74 FC-FEC/BASE-R";
7316 		}
7317 		netdev_info(vsi->netdev,
7318 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7319 			    speed, req_fec, fec, an, fc);
7320 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7321 		req_fec = "None";
7322 		fec = "None";
7323 		an = "False";
7324 
7325 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7326 			an = "True";
7327 
7328 		if (pf->hw.phy.link_info.fec_info &
7329 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7330 			fec = "CL74 FC-FEC/BASE-R";
7331 
7332 		if (pf->hw.phy.link_info.req_fec_info &
7333 		    I40E_AQ_REQUEST_FEC_KR)
7334 			req_fec = "CL74 FC-FEC/BASE-R";
7335 
7336 		netdev_info(vsi->netdev,
7337 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7338 			    speed, req_fec, fec, an, fc);
7339 	} else {
7340 		netdev_info(vsi->netdev,
7341 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7342 			    speed, fc);
7343 	}
7344 
7345 }
7346 
7347 /**
7348  * i40e_up_complete - Finish the last steps of bringing up a connection
7349  * @vsi: the VSI being configured
7350  **/
i40e_up_complete(struct i40e_vsi * vsi)7351 static int i40e_up_complete(struct i40e_vsi *vsi)
7352 {
7353 	struct i40e_pf *pf = vsi->back;
7354 	int err;
7355 
7356 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7357 		i40e_vsi_configure_msix(vsi);
7358 	else
7359 		i40e_configure_msi_and_legacy(vsi);
7360 
7361 	/* start rings */
7362 	err = i40e_vsi_start_rings(vsi);
7363 	if (err)
7364 		return err;
7365 
7366 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7367 	i40e_napi_enable_all(vsi);
7368 	i40e_vsi_enable_irq(vsi);
7369 
7370 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7371 	    (vsi->netdev)) {
7372 		i40e_print_link_message(vsi, true);
7373 		netif_tx_start_all_queues(vsi->netdev);
7374 		netif_carrier_on(vsi->netdev);
7375 	}
7376 
7377 	/* replay FDIR SB filters */
7378 	if (vsi->type == I40E_VSI_FDIR) {
7379 		/* reset fd counters */
7380 		pf->fd_add_err = 0;
7381 		pf->fd_atr_cnt = 0;
7382 		i40e_fdir_filter_restore(vsi);
7383 	}
7384 
7385 	/* On the next run of the service_task, notify any clients of the new
7386 	 * opened netdev
7387 	 */
7388 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7389 	i40e_service_event_schedule(pf);
7390 
7391 	return 0;
7392 }
7393 
7394 /**
7395  * i40e_vsi_reinit_locked - Reset the VSI
7396  * @vsi: the VSI being configured
7397  *
7398  * Rebuild the ring structs after some configuration
7399  * has changed, e.g. MTU size.
7400  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)7401 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7402 {
7403 	struct i40e_pf *pf = vsi->back;
7404 
7405 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7406 		usleep_range(1000, 2000);
7407 	i40e_down(vsi);
7408 
7409 	i40e_up(vsi);
7410 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7411 }
7412 
7413 /**
7414  * i40e_force_link_state - Force the link status
7415  * @pf: board private structure
7416  * @is_up: whether the link state should be forced up or down
7417  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)7418 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7419 {
7420 	struct i40e_aq_get_phy_abilities_resp abilities;
7421 	struct i40e_aq_set_phy_config config = {0};
7422 	bool non_zero_phy_type = is_up;
7423 	struct i40e_hw *hw = &pf->hw;
7424 	i40e_status err;
7425 	u64 mask;
7426 	u8 speed;
7427 
7428 	/* Card might've been put in an unstable state by other drivers
7429 	 * and applications, which causes incorrect speed values being
7430 	 * set on startup. In order to clear speed registers, we call
7431 	 * get_phy_capabilities twice, once to get initial state of
7432 	 * available speeds, and once to get current PHY config.
7433 	 */
7434 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7435 					   NULL);
7436 	if (err) {
7437 		dev_err(&pf->pdev->dev,
7438 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7439 			i40e_stat_str(hw, err),
7440 			i40e_aq_str(hw, hw->aq.asq_last_status));
7441 		return err;
7442 	}
7443 	speed = abilities.link_speed;
7444 
7445 	/* Get the current phy config */
7446 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7447 					   NULL);
7448 	if (err) {
7449 		dev_err(&pf->pdev->dev,
7450 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7451 			i40e_stat_str(hw, err),
7452 			i40e_aq_str(hw, hw->aq.asq_last_status));
7453 		return err;
7454 	}
7455 
7456 	/* If link needs to go up, but was not forced to go down,
7457 	 * and its speed values are OK, no need for a flap
7458 	 * if non_zero_phy_type was set, still need to force up
7459 	 */
7460 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7461 		non_zero_phy_type = true;
7462 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7463 		return I40E_SUCCESS;
7464 
7465 	/* To force link we need to set bits for all supported PHY types,
7466 	 * but there are now more than 32, so we need to split the bitmap
7467 	 * across two fields.
7468 	 */
7469 	mask = I40E_PHY_TYPES_BITMASK;
7470 	config.phy_type =
7471 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7472 	config.phy_type_ext =
7473 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7474 	/* Copy the old settings, except of phy_type */
7475 	config.abilities = abilities.abilities;
7476 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7477 		if (is_up)
7478 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7479 		else
7480 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7481 	}
7482 	if (abilities.link_speed != 0)
7483 		config.link_speed = abilities.link_speed;
7484 	else
7485 		config.link_speed = speed;
7486 	config.eee_capability = abilities.eee_capability;
7487 	config.eeer = abilities.eeer_val;
7488 	config.low_power_ctrl = abilities.d3_lpan;
7489 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7490 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7491 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7492 
7493 	if (err) {
7494 		dev_err(&pf->pdev->dev,
7495 			"set phy config ret =  %s last_status =  %s\n",
7496 			i40e_stat_str(&pf->hw, err),
7497 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7498 		return err;
7499 	}
7500 
7501 	/* Update the link info */
7502 	err = i40e_update_link_info(hw);
7503 	if (err) {
7504 		/* Wait a little bit (on 40G cards it sometimes takes a really
7505 		 * long time for link to come back from the atomic reset)
7506 		 * and try once more
7507 		 */
7508 		msleep(1000);
7509 		i40e_update_link_info(hw);
7510 	}
7511 
7512 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7513 
7514 	return I40E_SUCCESS;
7515 }
7516 
7517 /**
7518  * i40e_up - Bring the connection back up after being down
7519  * @vsi: the VSI being configured
7520  **/
i40e_up(struct i40e_vsi * vsi)7521 int i40e_up(struct i40e_vsi *vsi)
7522 {
7523 	int err;
7524 
7525 	if (vsi->type == I40E_VSI_MAIN &&
7526 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7527 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7528 		i40e_force_link_state(vsi->back, true);
7529 
7530 	err = i40e_vsi_configure(vsi);
7531 	if (!err)
7532 		err = i40e_up_complete(vsi);
7533 
7534 	return err;
7535 }
7536 
7537 /**
7538  * i40e_down - Shutdown the connection processing
7539  * @vsi: the VSI being stopped
7540  **/
i40e_down(struct i40e_vsi * vsi)7541 void i40e_down(struct i40e_vsi *vsi)
7542 {
7543 	int i;
7544 
7545 	/* It is assumed that the caller of this function
7546 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7547 	 */
7548 	if (vsi->netdev) {
7549 		netif_carrier_off(vsi->netdev);
7550 		netif_tx_disable(vsi->netdev);
7551 	}
7552 	i40e_vsi_disable_irq(vsi);
7553 	i40e_vsi_stop_rings(vsi);
7554 	if (vsi->type == I40E_VSI_MAIN &&
7555 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7556 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7557 		i40e_force_link_state(vsi->back, false);
7558 	i40e_napi_disable_all(vsi);
7559 
7560 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7561 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7562 		if (i40e_enabled_xdp_vsi(vsi)) {
7563 			/* Make sure that in-progress ndo_xdp_xmit and
7564 			 * ndo_xsk_wakeup calls are completed.
7565 			 */
7566 			synchronize_rcu();
7567 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7568 		}
7569 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7570 	}
7571 
7572 }
7573 
7574 /**
7575  * i40e_validate_mqprio_qopt- validate queue mapping info
7576  * @vsi: the VSI being configured
7577  * @mqprio_qopt: queue parametrs
7578  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7579 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7580 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7581 {
7582 	u64 sum_max_rate = 0;
7583 	u64 max_rate = 0;
7584 	int i;
7585 
7586 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7587 	    mqprio_qopt->qopt.num_tc < 1 ||
7588 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7589 		return -EINVAL;
7590 	for (i = 0; ; i++) {
7591 		if (!mqprio_qopt->qopt.count[i])
7592 			return -EINVAL;
7593 		if (mqprio_qopt->min_rate[i]) {
7594 			dev_err(&vsi->back->pdev->dev,
7595 				"Invalid min tx rate (greater than 0) specified\n");
7596 			return -EINVAL;
7597 		}
7598 		max_rate = mqprio_qopt->max_rate[i];
7599 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7600 		sum_max_rate += max_rate;
7601 
7602 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7603 			break;
7604 		if (mqprio_qopt->qopt.offset[i + 1] !=
7605 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7606 			return -EINVAL;
7607 	}
7608 	if (vsi->num_queue_pairs <
7609 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7610 		dev_err(&vsi->back->pdev->dev,
7611 			"Failed to create traffic channel, insufficient number of queues.\n");
7612 		return -EINVAL;
7613 	}
7614 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7615 		dev_err(&vsi->back->pdev->dev,
7616 			"Invalid max tx rate specified\n");
7617 		return -EINVAL;
7618 	}
7619 	return 0;
7620 }
7621 
7622 /**
7623  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7624  * @vsi: the VSI being configured
7625  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7626 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7627 {
7628 	u16 qcount;
7629 	int i;
7630 
7631 	/* Only TC0 is enabled */
7632 	vsi->tc_config.numtc = 1;
7633 	vsi->tc_config.enabled_tc = 1;
7634 	qcount = min_t(int, vsi->alloc_queue_pairs,
7635 		       i40e_pf_get_max_q_per_tc(vsi->back));
7636 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7637 		/* For the TC that is not enabled set the offset to default
7638 		 * queue and allocate one queue for the given TC.
7639 		 */
7640 		vsi->tc_config.tc_info[i].qoffset = 0;
7641 		if (i == 0)
7642 			vsi->tc_config.tc_info[i].qcount = qcount;
7643 		else
7644 			vsi->tc_config.tc_info[i].qcount = 1;
7645 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7646 	}
7647 }
7648 
7649 /**
7650  * i40e_del_macvlan_filter
7651  * @hw: pointer to the HW structure
7652  * @seid: seid of the channel VSI
7653  * @macaddr: the mac address to apply as a filter
7654  * @aq_err: store the admin Q error
7655  *
7656  * This function deletes a mac filter on the channel VSI which serves as the
7657  * macvlan. Returns 0 on success.
7658  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7659 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7660 					   const u8 *macaddr, int *aq_err)
7661 {
7662 	struct i40e_aqc_remove_macvlan_element_data element;
7663 	i40e_status status;
7664 
7665 	memset(&element, 0, sizeof(element));
7666 	ether_addr_copy(element.mac_addr, macaddr);
7667 	element.vlan_tag = 0;
7668 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7669 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7670 	*aq_err = hw->aq.asq_last_status;
7671 
7672 	return status;
7673 }
7674 
7675 /**
7676  * i40e_add_macvlan_filter
7677  * @hw: pointer to the HW structure
7678  * @seid: seid of the channel VSI
7679  * @macaddr: the mac address to apply as a filter
7680  * @aq_err: store the admin Q error
7681  *
7682  * This function adds a mac filter on the channel VSI which serves as the
7683  * macvlan. Returns 0 on success.
7684  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7685 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7686 					   const u8 *macaddr, int *aq_err)
7687 {
7688 	struct i40e_aqc_add_macvlan_element_data element;
7689 	i40e_status status;
7690 	u16 cmd_flags = 0;
7691 
7692 	ether_addr_copy(element.mac_addr, macaddr);
7693 	element.vlan_tag = 0;
7694 	element.queue_number = 0;
7695 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7696 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7697 	element.flags = cpu_to_le16(cmd_flags);
7698 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7699 	*aq_err = hw->aq.asq_last_status;
7700 
7701 	return status;
7702 }
7703 
7704 /**
7705  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7706  * @vsi: the VSI we want to access
7707  * @ch: the channel we want to access
7708  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7709 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7710 {
7711 	struct i40e_ring *tx_ring, *rx_ring;
7712 	u16 pf_q;
7713 	int i;
7714 
7715 	for (i = 0; i < ch->num_queue_pairs; i++) {
7716 		pf_q = ch->base_queue + i;
7717 		tx_ring = vsi->tx_rings[pf_q];
7718 		tx_ring->ch = NULL;
7719 		rx_ring = vsi->rx_rings[pf_q];
7720 		rx_ring->ch = NULL;
7721 	}
7722 }
7723 
7724 /**
7725  * i40e_free_macvlan_channels
7726  * @vsi: the VSI we want to access
7727  *
7728  * This function frees the Qs of the channel VSI from
7729  * the stack and also deletes the channel VSIs which
7730  * serve as macvlans.
7731  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7732 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7733 {
7734 	struct i40e_channel *ch, *ch_tmp;
7735 	int ret;
7736 
7737 	if (list_empty(&vsi->macvlan_list))
7738 		return;
7739 
7740 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7741 		struct i40e_vsi *parent_vsi;
7742 
7743 		if (i40e_is_channel_macvlan(ch)) {
7744 			i40e_reset_ch_rings(vsi, ch);
7745 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7746 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7747 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7748 			kfree(ch->fwd);
7749 			ch->fwd = NULL;
7750 		}
7751 
7752 		list_del(&ch->list);
7753 		parent_vsi = ch->parent_vsi;
7754 		if (!parent_vsi || !ch->initialized) {
7755 			kfree(ch);
7756 			continue;
7757 		}
7758 
7759 		/* remove the VSI */
7760 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7761 					     NULL);
7762 		if (ret)
7763 			dev_err(&vsi->back->pdev->dev,
7764 				"unable to remove channel (%d) for parent VSI(%d)\n",
7765 				ch->seid, parent_vsi->seid);
7766 		kfree(ch);
7767 	}
7768 	vsi->macvlan_cnt = 0;
7769 }
7770 
7771 /**
7772  * i40e_fwd_ring_up - bring the macvlan device up
7773  * @vsi: the VSI we want to access
7774  * @vdev: macvlan netdevice
7775  * @fwd: the private fwd structure
7776  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7777 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7778 			    struct i40e_fwd_adapter *fwd)
7779 {
7780 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7781 	int ret = 0, num_tc = 1,  i, aq_err;
7782 	struct i40e_pf *pf = vsi->back;
7783 	struct i40e_hw *hw = &pf->hw;
7784 
7785 	/* Go through the list and find an available channel */
7786 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7787 		if (!i40e_is_channel_macvlan(iter)) {
7788 			iter->fwd = fwd;
7789 			/* record configuration for macvlan interface in vdev */
7790 			for (i = 0; i < num_tc; i++)
7791 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7792 							     i,
7793 							     iter->num_queue_pairs,
7794 							     iter->base_queue);
7795 			for (i = 0; i < iter->num_queue_pairs; i++) {
7796 				struct i40e_ring *tx_ring, *rx_ring;
7797 				u16 pf_q;
7798 
7799 				pf_q = iter->base_queue + i;
7800 
7801 				/* Get to TX ring ptr */
7802 				tx_ring = vsi->tx_rings[pf_q];
7803 				tx_ring->ch = iter;
7804 
7805 				/* Get the RX ring ptr */
7806 				rx_ring = vsi->rx_rings[pf_q];
7807 				rx_ring->ch = iter;
7808 			}
7809 			ch = iter;
7810 			break;
7811 		}
7812 	}
7813 
7814 	if (!ch)
7815 		return -EINVAL;
7816 
7817 	/* Guarantee all rings are updated before we update the
7818 	 * MAC address filter.
7819 	 */
7820 	wmb();
7821 
7822 	/* Add a mac filter */
7823 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7824 	if (ret) {
7825 		/* if we cannot add the MAC rule then disable the offload */
7826 		macvlan_release_l2fw_offload(vdev);
7827 		for (i = 0; i < ch->num_queue_pairs; i++) {
7828 			struct i40e_ring *rx_ring;
7829 			u16 pf_q;
7830 
7831 			pf_q = ch->base_queue + i;
7832 			rx_ring = vsi->rx_rings[pf_q];
7833 			rx_ring->netdev = NULL;
7834 		}
7835 		dev_info(&pf->pdev->dev,
7836 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7837 			  i40e_stat_str(hw, ret),
7838 			  i40e_aq_str(hw, aq_err));
7839 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7840 	}
7841 
7842 	return ret;
7843 }
7844 
7845 /**
7846  * i40e_setup_macvlans - create the channels which will be macvlans
7847  * @vsi: the VSI we want to access
7848  * @macvlan_cnt: no. of macvlans to be setup
7849  * @qcnt: no. of Qs per macvlan
7850  * @vdev: macvlan netdevice
7851  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7852 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7853 			       struct net_device *vdev)
7854 {
7855 	struct i40e_pf *pf = vsi->back;
7856 	struct i40e_hw *hw = &pf->hw;
7857 	struct i40e_vsi_context ctxt;
7858 	u16 sections, qmap, num_qps;
7859 	struct i40e_channel *ch;
7860 	int i, pow, ret = 0;
7861 	u8 offset = 0;
7862 
7863 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7864 		return -EINVAL;
7865 
7866 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7867 
7868 	/* find the next higher power-of-2 of num queue pairs */
7869 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7870 
7871 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7872 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7873 
7874 	/* Setup context bits for the main VSI */
7875 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7876 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7877 	memset(&ctxt, 0, sizeof(ctxt));
7878 	ctxt.seid = vsi->seid;
7879 	ctxt.pf_num = vsi->back->hw.pf_id;
7880 	ctxt.vf_num = 0;
7881 	ctxt.uplink_seid = vsi->uplink_seid;
7882 	ctxt.info = vsi->info;
7883 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7884 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7885 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7886 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7887 
7888 	/* Reconfigure RSS for main VSI with new max queue count */
7889 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7890 	ret = i40e_vsi_config_rss(vsi);
7891 	if (ret) {
7892 		dev_info(&pf->pdev->dev,
7893 			 "Failed to reconfig RSS for num_queues (%u)\n",
7894 			 vsi->rss_size);
7895 		return ret;
7896 	}
7897 	vsi->reconfig_rss = true;
7898 	dev_dbg(&vsi->back->pdev->dev,
7899 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7900 	vsi->next_base_queue = num_qps;
7901 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7902 
7903 	/* Update the VSI after updating the VSI queue-mapping
7904 	 * information
7905 	 */
7906 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7907 	if (ret) {
7908 		dev_info(&pf->pdev->dev,
7909 			 "Update vsi tc config failed, err %s aq_err %s\n",
7910 			 i40e_stat_str(hw, ret),
7911 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7912 		return ret;
7913 	}
7914 	/* update the local VSI info with updated queue map */
7915 	i40e_vsi_update_queue_map(vsi, &ctxt);
7916 	vsi->info.valid_sections = 0;
7917 
7918 	/* Create channels for macvlans */
7919 	INIT_LIST_HEAD(&vsi->macvlan_list);
7920 	for (i = 0; i < macvlan_cnt; i++) {
7921 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7922 		if (!ch) {
7923 			ret = -ENOMEM;
7924 			goto err_free;
7925 		}
7926 		INIT_LIST_HEAD(&ch->list);
7927 		ch->num_queue_pairs = qcnt;
7928 		if (!i40e_setup_channel(pf, vsi, ch)) {
7929 			ret = -EINVAL;
7930 			kfree(ch);
7931 			goto err_free;
7932 		}
7933 		ch->parent_vsi = vsi;
7934 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7935 		vsi->macvlan_cnt++;
7936 		list_add_tail(&ch->list, &vsi->macvlan_list);
7937 	}
7938 
7939 	return ret;
7940 
7941 err_free:
7942 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7943 	i40e_free_macvlan_channels(vsi);
7944 
7945 	return ret;
7946 }
7947 
7948 /**
7949  * i40e_fwd_add - configure macvlans
7950  * @netdev: net device to configure
7951  * @vdev: macvlan netdevice
7952  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)7953 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7954 {
7955 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7956 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7957 	struct i40e_vsi *vsi = np->vsi;
7958 	struct i40e_pf *pf = vsi->back;
7959 	struct i40e_fwd_adapter *fwd;
7960 	int avail_macvlan, ret;
7961 
7962 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7963 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7964 		return ERR_PTR(-EINVAL);
7965 	}
7966 	if (i40e_is_tc_mqprio_enabled(pf)) {
7967 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7968 		return ERR_PTR(-EINVAL);
7969 	}
7970 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7971 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7972 		return ERR_PTR(-EINVAL);
7973 	}
7974 
7975 	/* The macvlan device has to be a single Q device so that the
7976 	 * tc_to_txq field can be reused to pick the tx queue.
7977 	 */
7978 	if (netif_is_multiqueue(vdev))
7979 		return ERR_PTR(-ERANGE);
7980 
7981 	if (!vsi->macvlan_cnt) {
7982 		/* reserve bit 0 for the pf device */
7983 		set_bit(0, vsi->fwd_bitmask);
7984 
7985 		/* Try to reserve as many queues as possible for macvlans. First
7986 		 * reserve 3/4th of max vectors, then half, then quarter and
7987 		 * calculate Qs per macvlan as you go
7988 		 */
7989 		vectors = pf->num_lan_msix;
7990 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7991 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7992 			q_per_macvlan = 4;
7993 			macvlan_cnt = (vectors - 32) / 4;
7994 		} else if (vectors <= 64 && vectors > 32) {
7995 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7996 			q_per_macvlan = 2;
7997 			macvlan_cnt = (vectors - 16) / 2;
7998 		} else if (vectors <= 32 && vectors > 16) {
7999 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8000 			q_per_macvlan = 1;
8001 			macvlan_cnt = vectors - 16;
8002 		} else if (vectors <= 16 && vectors > 8) {
8003 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8004 			q_per_macvlan = 1;
8005 			macvlan_cnt = vectors - 8;
8006 		} else {
8007 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8008 			q_per_macvlan = 1;
8009 			macvlan_cnt = vectors - 1;
8010 		}
8011 
8012 		if (macvlan_cnt == 0)
8013 			return ERR_PTR(-EBUSY);
8014 
8015 		/* Quiesce VSI queues */
8016 		i40e_quiesce_vsi(vsi);
8017 
8018 		/* sets up the macvlans but does not "enable" them */
8019 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8020 					  vdev);
8021 		if (ret)
8022 			return ERR_PTR(ret);
8023 
8024 		/* Unquiesce VSI */
8025 		i40e_unquiesce_vsi(vsi);
8026 	}
8027 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8028 					    vsi->macvlan_cnt);
8029 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8030 		return ERR_PTR(-EBUSY);
8031 
8032 	/* create the fwd struct */
8033 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8034 	if (!fwd)
8035 		return ERR_PTR(-ENOMEM);
8036 
8037 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8038 	fwd->bit_no = avail_macvlan;
8039 	netdev_set_sb_channel(vdev, avail_macvlan);
8040 	fwd->netdev = vdev;
8041 
8042 	if (!netif_running(netdev))
8043 		return fwd;
8044 
8045 	/* Set fwd ring up */
8046 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8047 	if (ret) {
8048 		/* unbind the queues and drop the subordinate channel config */
8049 		netdev_unbind_sb_channel(netdev, vdev);
8050 		netdev_set_sb_channel(vdev, 0);
8051 
8052 		kfree(fwd);
8053 		return ERR_PTR(-EINVAL);
8054 	}
8055 
8056 	return fwd;
8057 }
8058 
8059 /**
8060  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8061  * @vsi: the VSI we want to access
8062  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)8063 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8064 {
8065 	struct i40e_channel *ch, *ch_tmp;
8066 	struct i40e_pf *pf = vsi->back;
8067 	struct i40e_hw *hw = &pf->hw;
8068 	int aq_err, ret = 0;
8069 
8070 	if (list_empty(&vsi->macvlan_list))
8071 		return;
8072 
8073 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8074 		if (i40e_is_channel_macvlan(ch)) {
8075 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8076 						      i40e_channel_mac(ch),
8077 						      &aq_err);
8078 			if (!ret) {
8079 				/* Reset queue contexts */
8080 				i40e_reset_ch_rings(vsi, ch);
8081 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8082 				netdev_unbind_sb_channel(vsi->netdev,
8083 							 ch->fwd->netdev);
8084 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8085 				kfree(ch->fwd);
8086 				ch->fwd = NULL;
8087 			}
8088 		}
8089 	}
8090 }
8091 
8092 /**
8093  * i40e_fwd_del - delete macvlan interfaces
8094  * @netdev: net device to configure
8095  * @vdev: macvlan netdevice
8096  */
i40e_fwd_del(struct net_device * netdev,void * vdev)8097 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8098 {
8099 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8100 	struct i40e_fwd_adapter *fwd = vdev;
8101 	struct i40e_channel *ch, *ch_tmp;
8102 	struct i40e_vsi *vsi = np->vsi;
8103 	struct i40e_pf *pf = vsi->back;
8104 	struct i40e_hw *hw = &pf->hw;
8105 	int aq_err, ret = 0;
8106 
8107 	/* Find the channel associated with the macvlan and del mac filter */
8108 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8109 		if (i40e_is_channel_macvlan(ch) &&
8110 		    ether_addr_equal(i40e_channel_mac(ch),
8111 				     fwd->netdev->dev_addr)) {
8112 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8113 						      i40e_channel_mac(ch),
8114 						      &aq_err);
8115 			if (!ret) {
8116 				/* Reset queue contexts */
8117 				i40e_reset_ch_rings(vsi, ch);
8118 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8119 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8120 				netdev_set_sb_channel(fwd->netdev, 0);
8121 				kfree(ch->fwd);
8122 				ch->fwd = NULL;
8123 			} else {
8124 				dev_info(&pf->pdev->dev,
8125 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8126 					  i40e_stat_str(hw, ret),
8127 					  i40e_aq_str(hw, aq_err));
8128 			}
8129 			break;
8130 		}
8131 	}
8132 }
8133 
8134 /**
8135  * i40e_setup_tc - configure multiple traffic classes
8136  * @netdev: net device to configure
8137  * @type_data: tc offload data
8138  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)8139 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8140 {
8141 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8142 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8143 	struct i40e_vsi *vsi = np->vsi;
8144 	struct i40e_pf *pf = vsi->back;
8145 	u8 enabled_tc = 0, num_tc, hw;
8146 	bool need_reset = false;
8147 	int old_queue_pairs;
8148 	int ret = -EINVAL;
8149 	u16 mode;
8150 	int i;
8151 
8152 	old_queue_pairs = vsi->num_queue_pairs;
8153 	num_tc = mqprio_qopt->qopt.num_tc;
8154 	hw = mqprio_qopt->qopt.hw;
8155 	mode = mqprio_qopt->mode;
8156 	if (!hw) {
8157 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8158 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8159 		goto config_tc;
8160 	}
8161 
8162 	/* Check if MFP enabled */
8163 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8164 		netdev_info(netdev,
8165 			    "Configuring TC not supported in MFP mode\n");
8166 		return ret;
8167 	}
8168 	switch (mode) {
8169 	case TC_MQPRIO_MODE_DCB:
8170 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8171 
8172 		/* Check if DCB enabled to continue */
8173 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8174 			netdev_info(netdev,
8175 				    "DCB is not enabled for adapter\n");
8176 			return ret;
8177 		}
8178 
8179 		/* Check whether tc count is within enabled limit */
8180 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8181 			netdev_info(netdev,
8182 				    "TC count greater than enabled on link for adapter\n");
8183 			return ret;
8184 		}
8185 		break;
8186 	case TC_MQPRIO_MODE_CHANNEL:
8187 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8188 			netdev_info(netdev,
8189 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8190 			return ret;
8191 		}
8192 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8193 			return ret;
8194 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8195 		if (ret)
8196 			return ret;
8197 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8198 		       sizeof(*mqprio_qopt));
8199 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8200 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8201 		break;
8202 	default:
8203 		return -EINVAL;
8204 	}
8205 
8206 config_tc:
8207 	/* Generate TC map for number of tc requested */
8208 	for (i = 0; i < num_tc; i++)
8209 		enabled_tc |= BIT(i);
8210 
8211 	/* Requesting same TC configuration as already enabled */
8212 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8213 	    mode != TC_MQPRIO_MODE_CHANNEL)
8214 		return 0;
8215 
8216 	/* Quiesce VSI queues */
8217 	i40e_quiesce_vsi(vsi);
8218 
8219 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8220 		i40e_remove_queue_channels(vsi);
8221 
8222 	/* Configure VSI for enabled TCs */
8223 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8224 	if (ret) {
8225 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8226 			    vsi->seid);
8227 		need_reset = true;
8228 		goto exit;
8229 	} else if (enabled_tc &&
8230 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8231 		netdev_info(netdev,
8232 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8233 			    vsi->tc_config.tc_info[0].qcount);
8234 		ret = -EINVAL;
8235 		need_reset = true;
8236 		goto exit;
8237 	}
8238 
8239 	dev_info(&vsi->back->pdev->dev,
8240 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8241 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8242 
8243 	if (i40e_is_tc_mqprio_enabled(pf)) {
8244 		if (vsi->mqprio_qopt.max_rate[0]) {
8245 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8246 						  vsi->mqprio_qopt.max_rate[0]);
8247 
8248 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8249 			if (!ret) {
8250 				u64 credits = max_tx_rate;
8251 
8252 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8253 				dev_dbg(&vsi->back->pdev->dev,
8254 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8255 					max_tx_rate,
8256 					credits,
8257 					vsi->seid);
8258 			} else {
8259 				need_reset = true;
8260 				goto exit;
8261 			}
8262 		}
8263 		ret = i40e_configure_queue_channels(vsi);
8264 		if (ret) {
8265 			vsi->num_queue_pairs = old_queue_pairs;
8266 			netdev_info(netdev,
8267 				    "Failed configuring queue channels\n");
8268 			need_reset = true;
8269 			goto exit;
8270 		}
8271 	}
8272 
8273 exit:
8274 	/* Reset the configuration data to defaults, only TC0 is enabled */
8275 	if (need_reset) {
8276 		i40e_vsi_set_default_tc_config(vsi);
8277 		need_reset = false;
8278 	}
8279 
8280 	/* Unquiesce VSI */
8281 	i40e_unquiesce_vsi(vsi);
8282 	return ret;
8283 }
8284 
8285 /**
8286  * i40e_set_cld_element - sets cloud filter element data
8287  * @filter: cloud filter rule
8288  * @cld: ptr to cloud filter element data
8289  *
8290  * This is helper function to copy data into cloud filter element
8291  **/
8292 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)8293 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8294 		     struct i40e_aqc_cloud_filters_element_data *cld)
8295 {
8296 	u32 ipa;
8297 	int i;
8298 
8299 	memset(cld, 0, sizeof(*cld));
8300 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8301 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8302 
8303 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8304 		return;
8305 
8306 	if (filter->n_proto == ETH_P_IPV6) {
8307 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8308 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8309 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8310 
8311 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8312 		}
8313 	} else {
8314 		ipa = be32_to_cpu(filter->dst_ipv4);
8315 
8316 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8317 	}
8318 
8319 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8320 
8321 	/* tenant_id is not supported by FW now, once the support is enabled
8322 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8323 	 */
8324 	if (filter->tenant_id)
8325 		return;
8326 }
8327 
8328 /**
8329  * i40e_add_del_cloud_filter - Add/del cloud filter
8330  * @vsi: pointer to VSI
8331  * @filter: cloud filter rule
8332  * @add: if true, add, if false, delete
8333  *
8334  * Add or delete a cloud filter for a specific flow spec.
8335  * Returns 0 if the filter were successfully added.
8336  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8337 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8338 			      struct i40e_cloud_filter *filter, bool add)
8339 {
8340 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8341 	struct i40e_pf *pf = vsi->back;
8342 	int ret;
8343 	static const u16 flag_table[128] = {
8344 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8345 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8346 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8347 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8348 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8349 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8350 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8351 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8352 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8353 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8354 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8355 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8356 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8357 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8358 	};
8359 
8360 	if (filter->flags >= ARRAY_SIZE(flag_table))
8361 		return I40E_ERR_CONFIG;
8362 
8363 	memset(&cld_filter, 0, sizeof(cld_filter));
8364 
8365 	/* copy element needed to add cloud filter from filter */
8366 	i40e_set_cld_element(filter, &cld_filter);
8367 
8368 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8369 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8370 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8371 
8372 	if (filter->n_proto == ETH_P_IPV6)
8373 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8374 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8375 	else
8376 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8377 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8378 
8379 	if (add)
8380 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8381 						&cld_filter, 1);
8382 	else
8383 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8384 						&cld_filter, 1);
8385 	if (ret)
8386 		dev_dbg(&pf->pdev->dev,
8387 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8388 			add ? "add" : "delete", filter->dst_port, ret,
8389 			pf->hw.aq.asq_last_status);
8390 	else
8391 		dev_info(&pf->pdev->dev,
8392 			 "%s cloud filter for VSI: %d\n",
8393 			 add ? "Added" : "Deleted", filter->seid);
8394 	return ret;
8395 }
8396 
8397 /**
8398  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8399  * @vsi: pointer to VSI
8400  * @filter: cloud filter rule
8401  * @add: if true, add, if false, delete
8402  *
8403  * Add or delete a cloud filter for a specific flow spec using big buffer.
8404  * Returns 0 if the filter were successfully added.
8405  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8406 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8407 				      struct i40e_cloud_filter *filter,
8408 				      bool add)
8409 {
8410 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8411 	struct i40e_pf *pf = vsi->back;
8412 	int ret;
8413 
8414 	/* Both (src/dst) valid mac_addr are not supported */
8415 	if ((is_valid_ether_addr(filter->dst_mac) &&
8416 	     is_valid_ether_addr(filter->src_mac)) ||
8417 	    (is_multicast_ether_addr(filter->dst_mac) &&
8418 	     is_multicast_ether_addr(filter->src_mac)))
8419 		return -EOPNOTSUPP;
8420 
8421 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8422 	 * ports are not supported via big buffer now.
8423 	 */
8424 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8425 		return -EOPNOTSUPP;
8426 
8427 	/* adding filter using src_port/src_ip is not supported at this stage */
8428 	if (filter->src_port ||
8429 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8430 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8431 		return -EOPNOTSUPP;
8432 
8433 	memset(&cld_filter, 0, sizeof(cld_filter));
8434 
8435 	/* copy element needed to add cloud filter from filter */
8436 	i40e_set_cld_element(filter, &cld_filter.element);
8437 
8438 	if (is_valid_ether_addr(filter->dst_mac) ||
8439 	    is_valid_ether_addr(filter->src_mac) ||
8440 	    is_multicast_ether_addr(filter->dst_mac) ||
8441 	    is_multicast_ether_addr(filter->src_mac)) {
8442 		/* MAC + IP : unsupported mode */
8443 		if (filter->dst_ipv4)
8444 			return -EOPNOTSUPP;
8445 
8446 		/* since we validated that L4 port must be valid before
8447 		 * we get here, start with respective "flags" value
8448 		 * and update if vlan is present or not
8449 		 */
8450 		cld_filter.element.flags =
8451 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8452 
8453 		if (filter->vlan_id) {
8454 			cld_filter.element.flags =
8455 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8456 		}
8457 
8458 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8459 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8460 		cld_filter.element.flags =
8461 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8462 		if (filter->n_proto == ETH_P_IPV6)
8463 			cld_filter.element.flags |=
8464 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8465 		else
8466 			cld_filter.element.flags |=
8467 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8468 	} else {
8469 		dev_err(&pf->pdev->dev,
8470 			"either mac or ip has to be valid for cloud filter\n");
8471 		return -EINVAL;
8472 	}
8473 
8474 	/* Now copy L4 port in Byte 6..7 in general fields */
8475 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8476 						be16_to_cpu(filter->dst_port);
8477 
8478 	if (add) {
8479 		/* Validate current device switch mode, change if necessary */
8480 		ret = i40e_validate_and_set_switch_mode(vsi);
8481 		if (ret) {
8482 			dev_err(&pf->pdev->dev,
8483 				"failed to set switch mode, ret %d\n",
8484 				ret);
8485 			return ret;
8486 		}
8487 
8488 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8489 						   &cld_filter, 1);
8490 	} else {
8491 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8492 						   &cld_filter, 1);
8493 	}
8494 
8495 	if (ret)
8496 		dev_dbg(&pf->pdev->dev,
8497 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8498 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8499 	else
8500 		dev_info(&pf->pdev->dev,
8501 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8502 			 add ? "add" : "delete", filter->seid,
8503 			 ntohs(filter->dst_port));
8504 	return ret;
8505 }
8506 
8507 /**
8508  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8509  * @vsi: Pointer to VSI
8510  * @f: Pointer to struct flow_cls_offload
8511  * @filter: Pointer to cloud filter structure
8512  *
8513  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)8514 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8515 				 struct flow_cls_offload *f,
8516 				 struct i40e_cloud_filter *filter)
8517 {
8518 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8519 	struct flow_dissector *dissector = rule->match.dissector;
8520 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8521 	struct i40e_pf *pf = vsi->back;
8522 	u8 field_flags = 0;
8523 
8524 	if (dissector->used_keys &
8525 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8526 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8527 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8528 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8529 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8530 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8531 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8532 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8533 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8534 			dissector->used_keys);
8535 		return -EOPNOTSUPP;
8536 	}
8537 
8538 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8539 		struct flow_match_enc_keyid match;
8540 
8541 		flow_rule_match_enc_keyid(rule, &match);
8542 		if (match.mask->keyid != 0)
8543 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8544 
8545 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8546 	}
8547 
8548 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8549 		struct flow_match_basic match;
8550 
8551 		flow_rule_match_basic(rule, &match);
8552 		n_proto_key = ntohs(match.key->n_proto);
8553 		n_proto_mask = ntohs(match.mask->n_proto);
8554 
8555 		if (n_proto_key == ETH_P_ALL) {
8556 			n_proto_key = 0;
8557 			n_proto_mask = 0;
8558 		}
8559 		filter->n_proto = n_proto_key & n_proto_mask;
8560 		filter->ip_proto = match.key->ip_proto;
8561 	}
8562 
8563 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8564 		struct flow_match_eth_addrs match;
8565 
8566 		flow_rule_match_eth_addrs(rule, &match);
8567 
8568 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8569 		if (!is_zero_ether_addr(match.mask->dst)) {
8570 			if (is_broadcast_ether_addr(match.mask->dst)) {
8571 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8572 			} else {
8573 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8574 					match.mask->dst);
8575 				return I40E_ERR_CONFIG;
8576 			}
8577 		}
8578 
8579 		if (!is_zero_ether_addr(match.mask->src)) {
8580 			if (is_broadcast_ether_addr(match.mask->src)) {
8581 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8582 			} else {
8583 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8584 					match.mask->src);
8585 				return I40E_ERR_CONFIG;
8586 			}
8587 		}
8588 		ether_addr_copy(filter->dst_mac, match.key->dst);
8589 		ether_addr_copy(filter->src_mac, match.key->src);
8590 	}
8591 
8592 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8593 		struct flow_match_vlan match;
8594 
8595 		flow_rule_match_vlan(rule, &match);
8596 		if (match.mask->vlan_id) {
8597 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8598 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8599 
8600 			} else {
8601 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8602 					match.mask->vlan_id);
8603 				return I40E_ERR_CONFIG;
8604 			}
8605 		}
8606 
8607 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8608 	}
8609 
8610 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8611 		struct flow_match_control match;
8612 
8613 		flow_rule_match_control(rule, &match);
8614 		addr_type = match.key->addr_type;
8615 	}
8616 
8617 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8618 		struct flow_match_ipv4_addrs match;
8619 
8620 		flow_rule_match_ipv4_addrs(rule, &match);
8621 		if (match.mask->dst) {
8622 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8623 				field_flags |= I40E_CLOUD_FIELD_IIP;
8624 			} else {
8625 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8626 					&match.mask->dst);
8627 				return I40E_ERR_CONFIG;
8628 			}
8629 		}
8630 
8631 		if (match.mask->src) {
8632 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8633 				field_flags |= I40E_CLOUD_FIELD_IIP;
8634 			} else {
8635 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8636 					&match.mask->src);
8637 				return I40E_ERR_CONFIG;
8638 			}
8639 		}
8640 
8641 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8642 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8643 			return I40E_ERR_CONFIG;
8644 		}
8645 		filter->dst_ipv4 = match.key->dst;
8646 		filter->src_ipv4 = match.key->src;
8647 	}
8648 
8649 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8650 		struct flow_match_ipv6_addrs match;
8651 
8652 		flow_rule_match_ipv6_addrs(rule, &match);
8653 
8654 		/* src and dest IPV6 address should not be LOOPBACK
8655 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8656 		 */
8657 		if (ipv6_addr_loopback(&match.key->dst) ||
8658 		    ipv6_addr_loopback(&match.key->src)) {
8659 			dev_err(&pf->pdev->dev,
8660 				"Bad ipv6, addr is LOOPBACK\n");
8661 			return I40E_ERR_CONFIG;
8662 		}
8663 		if (!ipv6_addr_any(&match.mask->dst) ||
8664 		    !ipv6_addr_any(&match.mask->src))
8665 			field_flags |= I40E_CLOUD_FIELD_IIP;
8666 
8667 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8668 		       sizeof(filter->src_ipv6));
8669 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8670 		       sizeof(filter->dst_ipv6));
8671 	}
8672 
8673 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8674 		struct flow_match_ports match;
8675 
8676 		flow_rule_match_ports(rule, &match);
8677 		if (match.mask->src) {
8678 			if (match.mask->src == cpu_to_be16(0xffff)) {
8679 				field_flags |= I40E_CLOUD_FIELD_IIP;
8680 			} else {
8681 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8682 					be16_to_cpu(match.mask->src));
8683 				return I40E_ERR_CONFIG;
8684 			}
8685 		}
8686 
8687 		if (match.mask->dst) {
8688 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8689 				field_flags |= I40E_CLOUD_FIELD_IIP;
8690 			} else {
8691 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8692 					be16_to_cpu(match.mask->dst));
8693 				return I40E_ERR_CONFIG;
8694 			}
8695 		}
8696 
8697 		filter->dst_port = match.key->dst;
8698 		filter->src_port = match.key->src;
8699 
8700 		switch (filter->ip_proto) {
8701 		case IPPROTO_TCP:
8702 		case IPPROTO_UDP:
8703 			break;
8704 		default:
8705 			dev_err(&pf->pdev->dev,
8706 				"Only UDP and TCP transport are supported\n");
8707 			return -EINVAL;
8708 		}
8709 	}
8710 	filter->flags = field_flags;
8711 	return 0;
8712 }
8713 
8714 /**
8715  * i40e_handle_tclass: Forward to a traffic class on the device
8716  * @vsi: Pointer to VSI
8717  * @tc: traffic class index on the device
8718  * @filter: Pointer to cloud filter structure
8719  *
8720  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8721 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8722 			      struct i40e_cloud_filter *filter)
8723 {
8724 	struct i40e_channel *ch, *ch_tmp;
8725 
8726 	/* direct to a traffic class on the same device */
8727 	if (tc == 0) {
8728 		filter->seid = vsi->seid;
8729 		return 0;
8730 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8731 		if (!filter->dst_port) {
8732 			dev_err(&vsi->back->pdev->dev,
8733 				"Specify destination port to direct to traffic class that is not default\n");
8734 			return -EINVAL;
8735 		}
8736 		if (list_empty(&vsi->ch_list))
8737 			return -EINVAL;
8738 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8739 					 list) {
8740 			if (ch->seid == vsi->tc_seid_map[tc])
8741 				filter->seid = ch->seid;
8742 		}
8743 		return 0;
8744 	}
8745 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8746 	return -EINVAL;
8747 }
8748 
8749 /**
8750  * i40e_configure_clsflower - Configure tc flower filters
8751  * @vsi: Pointer to VSI
8752  * @cls_flower: Pointer to struct flow_cls_offload
8753  *
8754  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8755 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8756 				    struct flow_cls_offload *cls_flower)
8757 {
8758 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8759 	struct i40e_cloud_filter *filter = NULL;
8760 	struct i40e_pf *pf = vsi->back;
8761 	int err = 0;
8762 
8763 	if (tc < 0) {
8764 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8765 		return -EOPNOTSUPP;
8766 	}
8767 
8768 	if (!tc) {
8769 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8770 		return -EINVAL;
8771 	}
8772 
8773 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8774 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8775 		return -EBUSY;
8776 
8777 	if (pf->fdir_pf_active_filters ||
8778 	    (!hlist_empty(&pf->fdir_filter_list))) {
8779 		dev_err(&vsi->back->pdev->dev,
8780 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8781 		return -EINVAL;
8782 	}
8783 
8784 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8785 		dev_err(&vsi->back->pdev->dev,
8786 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8787 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8788 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8789 	}
8790 
8791 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8792 	if (!filter)
8793 		return -ENOMEM;
8794 
8795 	filter->cookie = cls_flower->cookie;
8796 
8797 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8798 	if (err < 0)
8799 		goto err;
8800 
8801 	err = i40e_handle_tclass(vsi, tc, filter);
8802 	if (err < 0)
8803 		goto err;
8804 
8805 	/* Add cloud filter */
8806 	if (filter->dst_port)
8807 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8808 	else
8809 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8810 
8811 	if (err) {
8812 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8813 			err);
8814 		goto err;
8815 	}
8816 
8817 	/* add filter to the ordered list */
8818 	INIT_HLIST_NODE(&filter->cloud_node);
8819 
8820 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8821 
8822 	pf->num_cloud_filters++;
8823 
8824 	return err;
8825 err:
8826 	kfree(filter);
8827 	return err;
8828 }
8829 
8830 /**
8831  * i40e_find_cloud_filter - Find the could filter in the list
8832  * @vsi: Pointer to VSI
8833  * @cookie: filter specific cookie
8834  *
8835  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8836 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8837 							unsigned long *cookie)
8838 {
8839 	struct i40e_cloud_filter *filter = NULL;
8840 	struct hlist_node *node2;
8841 
8842 	hlist_for_each_entry_safe(filter, node2,
8843 				  &vsi->back->cloud_filter_list, cloud_node)
8844 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8845 			return filter;
8846 	return NULL;
8847 }
8848 
8849 /**
8850  * i40e_delete_clsflower - Remove tc flower filters
8851  * @vsi: Pointer to VSI
8852  * @cls_flower: Pointer to struct flow_cls_offload
8853  *
8854  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8855 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8856 				 struct flow_cls_offload *cls_flower)
8857 {
8858 	struct i40e_cloud_filter *filter = NULL;
8859 	struct i40e_pf *pf = vsi->back;
8860 	int err = 0;
8861 
8862 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8863 
8864 	if (!filter)
8865 		return -EINVAL;
8866 
8867 	hash_del(&filter->cloud_node);
8868 
8869 	if (filter->dst_port)
8870 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8871 	else
8872 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8873 
8874 	kfree(filter);
8875 	if (err) {
8876 		dev_err(&pf->pdev->dev,
8877 			"Failed to delete cloud filter, err %s\n",
8878 			i40e_stat_str(&pf->hw, err));
8879 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8880 	}
8881 
8882 	pf->num_cloud_filters--;
8883 	if (!pf->num_cloud_filters)
8884 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8885 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8886 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8887 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8888 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8889 		}
8890 	return 0;
8891 }
8892 
8893 /**
8894  * i40e_setup_tc_cls_flower - flower classifier offloads
8895  * @np: net device to configure
8896  * @cls_flower: offload data
8897  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8898 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8899 				    struct flow_cls_offload *cls_flower)
8900 {
8901 	struct i40e_vsi *vsi = np->vsi;
8902 
8903 	switch (cls_flower->command) {
8904 	case FLOW_CLS_REPLACE:
8905 		return i40e_configure_clsflower(vsi, cls_flower);
8906 	case FLOW_CLS_DESTROY:
8907 		return i40e_delete_clsflower(vsi, cls_flower);
8908 	case FLOW_CLS_STATS:
8909 		return -EOPNOTSUPP;
8910 	default:
8911 		return -EOPNOTSUPP;
8912 	}
8913 }
8914 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8915 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8916 				  void *cb_priv)
8917 {
8918 	struct i40e_netdev_priv *np = cb_priv;
8919 
8920 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8921 		return -EOPNOTSUPP;
8922 
8923 	switch (type) {
8924 	case TC_SETUP_CLSFLOWER:
8925 		return i40e_setup_tc_cls_flower(np, type_data);
8926 
8927 	default:
8928 		return -EOPNOTSUPP;
8929 	}
8930 }
8931 
8932 static LIST_HEAD(i40e_block_cb_list);
8933 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8934 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8935 			   void *type_data)
8936 {
8937 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8938 
8939 	switch (type) {
8940 	case TC_SETUP_QDISC_MQPRIO:
8941 		return i40e_setup_tc(netdev, type_data);
8942 	case TC_SETUP_BLOCK:
8943 		return flow_block_cb_setup_simple(type_data,
8944 						  &i40e_block_cb_list,
8945 						  i40e_setup_tc_block_cb,
8946 						  np, np, true);
8947 	default:
8948 		return -EOPNOTSUPP;
8949 	}
8950 }
8951 
8952 /**
8953  * i40e_open - Called when a network interface is made active
8954  * @netdev: network interface device structure
8955  *
8956  * The open entry point is called when a network interface is made
8957  * active by the system (IFF_UP).  At this point all resources needed
8958  * for transmit and receive operations are allocated, the interrupt
8959  * handler is registered with the OS, the netdev watchdog subtask is
8960  * enabled, and the stack is notified that the interface is ready.
8961  *
8962  * Returns 0 on success, negative value on failure
8963  **/
i40e_open(struct net_device * netdev)8964 int i40e_open(struct net_device *netdev)
8965 {
8966 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8967 	struct i40e_vsi *vsi = np->vsi;
8968 	struct i40e_pf *pf = vsi->back;
8969 	int err;
8970 
8971 	/* disallow open during test or if eeprom is broken */
8972 	if (test_bit(__I40E_TESTING, pf->state) ||
8973 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8974 		return -EBUSY;
8975 
8976 	netif_carrier_off(netdev);
8977 
8978 	if (i40e_force_link_state(pf, true))
8979 		return -EAGAIN;
8980 
8981 	err = i40e_vsi_open(vsi);
8982 	if (err)
8983 		return err;
8984 
8985 	/* configure global TSO hardware offload settings */
8986 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8987 						       TCP_FLAG_FIN) >> 16);
8988 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8989 						       TCP_FLAG_FIN |
8990 						       TCP_FLAG_CWR) >> 16);
8991 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8992 	udp_tunnel_get_rx_info(netdev);
8993 
8994 	return 0;
8995 }
8996 
8997 /**
8998  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8999  * @vsi: vsi structure
9000  *
9001  * This updates netdev's number of tx/rx queues
9002  *
9003  * Returns status of setting tx/rx queues
9004  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)9005 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9006 {
9007 	int ret;
9008 
9009 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9010 					   vsi->num_queue_pairs);
9011 	if (ret)
9012 		return ret;
9013 
9014 	return netif_set_real_num_tx_queues(vsi->netdev,
9015 					    vsi->num_queue_pairs);
9016 }
9017 
9018 /**
9019  * i40e_vsi_open -
9020  * @vsi: the VSI to open
9021  *
9022  * Finish initialization of the VSI.
9023  *
9024  * Returns 0 on success, negative value on failure
9025  *
9026  * Note: expects to be called while under rtnl_lock()
9027  **/
i40e_vsi_open(struct i40e_vsi * vsi)9028 int i40e_vsi_open(struct i40e_vsi *vsi)
9029 {
9030 	struct i40e_pf *pf = vsi->back;
9031 	char int_name[I40E_INT_NAME_STR_LEN];
9032 	int err;
9033 
9034 	/* allocate descriptors */
9035 	err = i40e_vsi_setup_tx_resources(vsi);
9036 	if (err)
9037 		goto err_setup_tx;
9038 	err = i40e_vsi_setup_rx_resources(vsi);
9039 	if (err)
9040 		goto err_setup_rx;
9041 
9042 	err = i40e_vsi_configure(vsi);
9043 	if (err)
9044 		goto err_setup_rx;
9045 
9046 	if (vsi->netdev) {
9047 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9048 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9049 		err = i40e_vsi_request_irq(vsi, int_name);
9050 		if (err)
9051 			goto err_setup_rx;
9052 
9053 		/* Notify the stack of the actual queue counts. */
9054 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9055 		if (err)
9056 			goto err_set_queues;
9057 
9058 	} else if (vsi->type == I40E_VSI_FDIR) {
9059 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9060 			 dev_driver_string(&pf->pdev->dev),
9061 			 dev_name(&pf->pdev->dev));
9062 		err = i40e_vsi_request_irq(vsi, int_name);
9063 		if (err)
9064 			goto err_setup_rx;
9065 
9066 	} else {
9067 		err = -EINVAL;
9068 		goto err_setup_rx;
9069 	}
9070 
9071 	err = i40e_up_complete(vsi);
9072 	if (err)
9073 		goto err_up_complete;
9074 
9075 	return 0;
9076 
9077 err_up_complete:
9078 	i40e_down(vsi);
9079 err_set_queues:
9080 	i40e_vsi_free_irq(vsi);
9081 err_setup_rx:
9082 	i40e_vsi_free_rx_resources(vsi);
9083 err_setup_tx:
9084 	i40e_vsi_free_tx_resources(vsi);
9085 	if (vsi == pf->vsi[pf->lan_vsi])
9086 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9087 
9088 	return err;
9089 }
9090 
9091 /**
9092  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9093  * @pf: Pointer to PF
9094  *
9095  * This function destroys the hlist where all the Flow Director
9096  * filters were saved.
9097  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)9098 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9099 {
9100 	struct i40e_fdir_filter *filter;
9101 	struct i40e_flex_pit *pit_entry, *tmp;
9102 	struct hlist_node *node2;
9103 
9104 	hlist_for_each_entry_safe(filter, node2,
9105 				  &pf->fdir_filter_list, fdir_node) {
9106 		hlist_del(&filter->fdir_node);
9107 		kfree(filter);
9108 	}
9109 
9110 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9111 		list_del(&pit_entry->list);
9112 		kfree(pit_entry);
9113 	}
9114 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9115 
9116 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9117 		list_del(&pit_entry->list);
9118 		kfree(pit_entry);
9119 	}
9120 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9121 
9122 	pf->fdir_pf_active_filters = 0;
9123 	i40e_reset_fdir_filter_cnt(pf);
9124 
9125 	/* Reprogram the default input set for TCP/IPv4 */
9126 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9127 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9128 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9129 
9130 	/* Reprogram the default input set for TCP/IPv6 */
9131 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9132 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9133 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9134 
9135 	/* Reprogram the default input set for UDP/IPv4 */
9136 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9137 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9138 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9139 
9140 	/* Reprogram the default input set for UDP/IPv6 */
9141 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9142 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9143 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9144 
9145 	/* Reprogram the default input set for SCTP/IPv4 */
9146 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9147 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9148 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9149 
9150 	/* Reprogram the default input set for SCTP/IPv6 */
9151 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9152 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9153 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9154 
9155 	/* Reprogram the default input set for Other/IPv4 */
9156 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9157 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9158 
9159 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9160 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9161 
9162 	/* Reprogram the default input set for Other/IPv6 */
9163 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9164 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9165 
9166 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9167 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9168 }
9169 
9170 /**
9171  * i40e_cloud_filter_exit - Cleans up the cloud filters
9172  * @pf: Pointer to PF
9173  *
9174  * This function destroys the hlist where all the cloud filters
9175  * were saved.
9176  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)9177 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9178 {
9179 	struct i40e_cloud_filter *cfilter;
9180 	struct hlist_node *node;
9181 
9182 	hlist_for_each_entry_safe(cfilter, node,
9183 				  &pf->cloud_filter_list, cloud_node) {
9184 		hlist_del(&cfilter->cloud_node);
9185 		kfree(cfilter);
9186 	}
9187 	pf->num_cloud_filters = 0;
9188 
9189 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9190 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9191 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9192 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9193 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9194 	}
9195 }
9196 
9197 /**
9198  * i40e_close - Disables a network interface
9199  * @netdev: network interface device structure
9200  *
9201  * The close entry point is called when an interface is de-activated
9202  * by the OS.  The hardware is still under the driver's control, but
9203  * this netdev interface is disabled.
9204  *
9205  * Returns 0, this is not allowed to fail
9206  **/
i40e_close(struct net_device * netdev)9207 int i40e_close(struct net_device *netdev)
9208 {
9209 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9210 	struct i40e_vsi *vsi = np->vsi;
9211 
9212 	i40e_vsi_close(vsi);
9213 
9214 	return 0;
9215 }
9216 
9217 /**
9218  * i40e_do_reset - Start a PF or Core Reset sequence
9219  * @pf: board private structure
9220  * @reset_flags: which reset is requested
9221  * @lock_acquired: indicates whether or not the lock has been acquired
9222  * before this function was called.
9223  *
9224  * The essential difference in resets is that the PF Reset
9225  * doesn't clear the packet buffers, doesn't reset the PE
9226  * firmware, and doesn't bother the other PFs on the chip.
9227  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)9228 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9229 {
9230 	u32 val;
9231 
9232 	/* do the biggest reset indicated */
9233 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9234 
9235 		/* Request a Global Reset
9236 		 *
9237 		 * This will start the chip's countdown to the actual full
9238 		 * chip reset event, and a warning interrupt to be sent
9239 		 * to all PFs, including the requestor.  Our handler
9240 		 * for the warning interrupt will deal with the shutdown
9241 		 * and recovery of the switch setup.
9242 		 */
9243 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9244 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9245 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9246 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9247 
9248 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9249 
9250 		/* Request a Core Reset
9251 		 *
9252 		 * Same as Global Reset, except does *not* include the MAC/PHY
9253 		 */
9254 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9255 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9256 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9257 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9258 		i40e_flush(&pf->hw);
9259 
9260 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9261 
9262 		/* Request a PF Reset
9263 		 *
9264 		 * Resets only the PF-specific registers
9265 		 *
9266 		 * This goes directly to the tear-down and rebuild of
9267 		 * the switch, since we need to do all the recovery as
9268 		 * for the Core Reset.
9269 		 */
9270 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9271 		i40e_handle_reset_warning(pf, lock_acquired);
9272 
9273 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9274 		/* Request a PF Reset
9275 		 *
9276 		 * Resets PF and reinitializes PFs VSI.
9277 		 */
9278 		i40e_prep_for_reset(pf);
9279 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9280 		dev_info(&pf->pdev->dev,
9281 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9282 			 "FW LLDP is disabled\n" :
9283 			 "FW LLDP is enabled\n");
9284 
9285 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9286 		int v;
9287 
9288 		/* Find the VSI(s) that requested a re-init */
9289 		dev_info(&pf->pdev->dev,
9290 			 "VSI reinit requested\n");
9291 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9292 			struct i40e_vsi *vsi = pf->vsi[v];
9293 
9294 			if (vsi != NULL &&
9295 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9296 					       vsi->state))
9297 				i40e_vsi_reinit_locked(pf->vsi[v]);
9298 		}
9299 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9300 		int v;
9301 
9302 		/* Find the VSI(s) that needs to be brought down */
9303 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9304 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9305 			struct i40e_vsi *vsi = pf->vsi[v];
9306 
9307 			if (vsi != NULL &&
9308 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9309 					       vsi->state)) {
9310 				set_bit(__I40E_VSI_DOWN, vsi->state);
9311 				i40e_down(vsi);
9312 			}
9313 		}
9314 	} else {
9315 		dev_info(&pf->pdev->dev,
9316 			 "bad reset request 0x%08x\n", reset_flags);
9317 	}
9318 }
9319 
9320 #ifdef CONFIG_I40E_DCB
9321 /**
9322  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9323  * @pf: board private structure
9324  * @old_cfg: current DCB config
9325  * @new_cfg: new DCB config
9326  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)9327 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9328 			    struct i40e_dcbx_config *old_cfg,
9329 			    struct i40e_dcbx_config *new_cfg)
9330 {
9331 	bool need_reconfig = false;
9332 
9333 	/* Check if ETS configuration has changed */
9334 	if (memcmp(&new_cfg->etscfg,
9335 		   &old_cfg->etscfg,
9336 		   sizeof(new_cfg->etscfg))) {
9337 		/* If Priority Table has changed reconfig is needed */
9338 		if (memcmp(&new_cfg->etscfg.prioritytable,
9339 			   &old_cfg->etscfg.prioritytable,
9340 			   sizeof(new_cfg->etscfg.prioritytable))) {
9341 			need_reconfig = true;
9342 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9343 		}
9344 
9345 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9346 			   &old_cfg->etscfg.tcbwtable,
9347 			   sizeof(new_cfg->etscfg.tcbwtable)))
9348 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9349 
9350 		if (memcmp(&new_cfg->etscfg.tsatable,
9351 			   &old_cfg->etscfg.tsatable,
9352 			   sizeof(new_cfg->etscfg.tsatable)))
9353 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9354 	}
9355 
9356 	/* Check if PFC configuration has changed */
9357 	if (memcmp(&new_cfg->pfc,
9358 		   &old_cfg->pfc,
9359 		   sizeof(new_cfg->pfc))) {
9360 		need_reconfig = true;
9361 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9362 	}
9363 
9364 	/* Check if APP Table has changed */
9365 	if (memcmp(&new_cfg->app,
9366 		   &old_cfg->app,
9367 		   sizeof(new_cfg->app))) {
9368 		need_reconfig = true;
9369 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9370 	}
9371 
9372 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9373 	return need_reconfig;
9374 }
9375 
9376 /**
9377  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9378  * @pf: board private structure
9379  * @e: event info posted on ARQ
9380  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9381 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9382 				  struct i40e_arq_event_info *e)
9383 {
9384 	struct i40e_aqc_lldp_get_mib *mib =
9385 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9386 	struct i40e_hw *hw = &pf->hw;
9387 	struct i40e_dcbx_config tmp_dcbx_cfg;
9388 	bool need_reconfig = false;
9389 	int ret = 0;
9390 	u8 type;
9391 
9392 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9393 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9394 	    (hw->phy.link_info.link_speed &
9395 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9396 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9397 		/* let firmware decide if the DCB should be disabled */
9398 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9399 
9400 	/* Not DCB capable or capability disabled */
9401 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9402 		return ret;
9403 
9404 	/* Ignore if event is not for Nearest Bridge */
9405 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9406 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9407 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9408 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9409 		return ret;
9410 
9411 	/* Check MIB Type and return if event for Remote MIB update */
9412 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9413 	dev_dbg(&pf->pdev->dev,
9414 		"LLDP event mib type %s\n", type ? "remote" : "local");
9415 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9416 		/* Update the remote cached instance and return */
9417 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9418 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9419 				&hw->remote_dcbx_config);
9420 		goto exit;
9421 	}
9422 
9423 	/* Store the old configuration */
9424 	tmp_dcbx_cfg = hw->local_dcbx_config;
9425 
9426 	/* Reset the old DCBx configuration data */
9427 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9428 	/* Get updated DCBX data from firmware */
9429 	ret = i40e_get_dcb_config(&pf->hw);
9430 	if (ret) {
9431 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9432 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9433 		    (hw->phy.link_info.link_speed &
9434 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9435 			dev_warn(&pf->pdev->dev,
9436 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9437 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9438 		} else {
9439 			dev_info(&pf->pdev->dev,
9440 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9441 				 i40e_stat_str(&pf->hw, ret),
9442 				 i40e_aq_str(&pf->hw,
9443 					     pf->hw.aq.asq_last_status));
9444 		}
9445 		goto exit;
9446 	}
9447 
9448 	/* No change detected in DCBX configs */
9449 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9450 		    sizeof(tmp_dcbx_cfg))) {
9451 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9452 		goto exit;
9453 	}
9454 
9455 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9456 					       &hw->local_dcbx_config);
9457 
9458 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9459 
9460 	if (!need_reconfig)
9461 		goto exit;
9462 
9463 	/* Enable DCB tagging only when more than one TC */
9464 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9465 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9466 	else
9467 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9468 
9469 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9470 	/* Reconfiguration needed quiesce all VSIs */
9471 	i40e_pf_quiesce_all_vsi(pf);
9472 
9473 	/* Changes in configuration update VEB/VSI */
9474 	i40e_dcb_reconfigure(pf);
9475 
9476 	ret = i40e_resume_port_tx(pf);
9477 
9478 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9479 	/* In case of error no point in resuming VSIs */
9480 	if (ret)
9481 		goto exit;
9482 
9483 	/* Wait for the PF's queues to be disabled */
9484 	ret = i40e_pf_wait_queues_disabled(pf);
9485 	if (ret) {
9486 		/* Schedule PF reset to recover */
9487 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9488 		i40e_service_event_schedule(pf);
9489 	} else {
9490 		i40e_pf_unquiesce_all_vsi(pf);
9491 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9492 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9493 	}
9494 
9495 exit:
9496 	return ret;
9497 }
9498 #endif /* CONFIG_I40E_DCB */
9499 
9500 /**
9501  * i40e_do_reset_safe - Protected reset path for userland calls.
9502  * @pf: board private structure
9503  * @reset_flags: which reset is requested
9504  *
9505  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)9506 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9507 {
9508 	rtnl_lock();
9509 	i40e_do_reset(pf, reset_flags, true);
9510 	rtnl_unlock();
9511 }
9512 
9513 /**
9514  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9515  * @pf: board private structure
9516  * @e: event info posted on ARQ
9517  *
9518  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9519  * and VF queues
9520  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9521 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9522 					   struct i40e_arq_event_info *e)
9523 {
9524 	struct i40e_aqc_lan_overflow *data =
9525 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9526 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9527 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9528 	struct i40e_hw *hw = &pf->hw;
9529 	struct i40e_vf *vf;
9530 	u16 vf_id;
9531 
9532 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9533 		queue, qtx_ctl);
9534 
9535 	/* Queue belongs to VF, find the VF and issue VF reset */
9536 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9537 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9538 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9539 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9540 		vf_id -= hw->func_caps.vf_base_id;
9541 		vf = &pf->vf[vf_id];
9542 		i40e_vc_notify_vf_reset(vf);
9543 		/* Allow VF to process pending reset notification */
9544 		msleep(20);
9545 		i40e_reset_vf(vf, false);
9546 	}
9547 }
9548 
9549 /**
9550  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9551  * @pf: board private structure
9552  **/
i40e_get_cur_guaranteed_fd_count(struct i40e_pf * pf)9553 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9554 {
9555 	u32 val, fcnt_prog;
9556 
9557 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9558 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9559 	return fcnt_prog;
9560 }
9561 
9562 /**
9563  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9564  * @pf: board private structure
9565  **/
i40e_get_current_fd_count(struct i40e_pf * pf)9566 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9567 {
9568 	u32 val, fcnt_prog;
9569 
9570 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9571 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9572 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9573 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9574 	return fcnt_prog;
9575 }
9576 
9577 /**
9578  * i40e_get_global_fd_count - Get total FD filters programmed on device
9579  * @pf: board private structure
9580  **/
i40e_get_global_fd_count(struct i40e_pf * pf)9581 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9582 {
9583 	u32 val, fcnt_prog;
9584 
9585 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9586 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9587 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9588 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9589 	return fcnt_prog;
9590 }
9591 
9592 /**
9593  * i40e_reenable_fdir_sb - Restore FDir SB capability
9594  * @pf: board private structure
9595  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)9596 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9597 {
9598 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9599 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9600 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9601 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9602 }
9603 
9604 /**
9605  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9606  * @pf: board private structure
9607  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)9608 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9609 {
9610 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9611 		/* ATR uses the same filtering logic as SB rules. It only
9612 		 * functions properly if the input set mask is at the default
9613 		 * settings. It is safe to restore the default input set
9614 		 * because there are no active TCPv4 filter rules.
9615 		 */
9616 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9617 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9618 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9619 
9620 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9621 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9622 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9623 	}
9624 }
9625 
9626 /**
9627  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9628  * @pf: board private structure
9629  * @filter: FDir filter to remove
9630  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)9631 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9632 				       struct i40e_fdir_filter *filter)
9633 {
9634 	/* Update counters */
9635 	pf->fdir_pf_active_filters--;
9636 	pf->fd_inv = 0;
9637 
9638 	switch (filter->flow_type) {
9639 	case TCP_V4_FLOW:
9640 		pf->fd_tcp4_filter_cnt--;
9641 		break;
9642 	case UDP_V4_FLOW:
9643 		pf->fd_udp4_filter_cnt--;
9644 		break;
9645 	case SCTP_V4_FLOW:
9646 		pf->fd_sctp4_filter_cnt--;
9647 		break;
9648 	case TCP_V6_FLOW:
9649 		pf->fd_tcp6_filter_cnt--;
9650 		break;
9651 	case UDP_V6_FLOW:
9652 		pf->fd_udp6_filter_cnt--;
9653 		break;
9654 	case SCTP_V6_FLOW:
9655 		pf->fd_udp6_filter_cnt--;
9656 		break;
9657 	case IP_USER_FLOW:
9658 		switch (filter->ipl4_proto) {
9659 		case IPPROTO_TCP:
9660 			pf->fd_tcp4_filter_cnt--;
9661 			break;
9662 		case IPPROTO_UDP:
9663 			pf->fd_udp4_filter_cnt--;
9664 			break;
9665 		case IPPROTO_SCTP:
9666 			pf->fd_sctp4_filter_cnt--;
9667 			break;
9668 		case IPPROTO_IP:
9669 			pf->fd_ip4_filter_cnt--;
9670 			break;
9671 		}
9672 		break;
9673 	case IPV6_USER_FLOW:
9674 		switch (filter->ipl4_proto) {
9675 		case IPPROTO_TCP:
9676 			pf->fd_tcp6_filter_cnt--;
9677 			break;
9678 		case IPPROTO_UDP:
9679 			pf->fd_udp6_filter_cnt--;
9680 			break;
9681 		case IPPROTO_SCTP:
9682 			pf->fd_sctp6_filter_cnt--;
9683 			break;
9684 		case IPPROTO_IP:
9685 			pf->fd_ip6_filter_cnt--;
9686 			break;
9687 		}
9688 		break;
9689 	}
9690 
9691 	/* Remove the filter from the list and free memory */
9692 	hlist_del(&filter->fdir_node);
9693 	kfree(filter);
9694 }
9695 
9696 /**
9697  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9698  * @pf: board private structure
9699  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9700 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9701 {
9702 	struct i40e_fdir_filter *filter;
9703 	u32 fcnt_prog, fcnt_avail;
9704 	struct hlist_node *node;
9705 
9706 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9707 		return;
9708 
9709 	/* Check if we have enough room to re-enable FDir SB capability. */
9710 	fcnt_prog = i40e_get_global_fd_count(pf);
9711 	fcnt_avail = pf->fdir_pf_filter_count;
9712 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9713 	    (pf->fd_add_err == 0) ||
9714 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9715 		i40e_reenable_fdir_sb(pf);
9716 
9717 	/* We should wait for even more space before re-enabling ATR.
9718 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9719 	 * rules active.
9720 	 */
9721 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9722 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9723 		i40e_reenable_fdir_atr(pf);
9724 
9725 	/* if hw had a problem adding a filter, delete it */
9726 	if (pf->fd_inv > 0) {
9727 		hlist_for_each_entry_safe(filter, node,
9728 					  &pf->fdir_filter_list, fdir_node)
9729 			if (filter->fd_id == pf->fd_inv)
9730 				i40e_delete_invalid_filter(pf, filter);
9731 	}
9732 }
9733 
9734 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9735 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9736 /**
9737  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9738  * @pf: board private structure
9739  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9740 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9741 {
9742 	unsigned long min_flush_time;
9743 	int flush_wait_retry = 50;
9744 	bool disable_atr = false;
9745 	int fd_room;
9746 	int reg;
9747 
9748 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9749 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9750 		return;
9751 
9752 	/* If the flush is happening too quick and we have mostly SB rules we
9753 	 * should not re-enable ATR for some time.
9754 	 */
9755 	min_flush_time = pf->fd_flush_timestamp +
9756 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9757 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9758 
9759 	if (!(time_after(jiffies, min_flush_time)) &&
9760 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9761 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9762 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9763 		disable_atr = true;
9764 	}
9765 
9766 	pf->fd_flush_timestamp = jiffies;
9767 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9768 	/* flush all filters */
9769 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9770 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9771 	i40e_flush(&pf->hw);
9772 	pf->fd_flush_cnt++;
9773 	pf->fd_add_err = 0;
9774 	do {
9775 		/* Check FD flush status every 5-6msec */
9776 		usleep_range(5000, 6000);
9777 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9778 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9779 			break;
9780 	} while (flush_wait_retry--);
9781 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9782 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9783 	} else {
9784 		/* replay sideband filters */
9785 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9786 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9787 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9788 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9789 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9790 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9791 	}
9792 }
9793 
9794 /**
9795  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9796  * @pf: board private structure
9797  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9798 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9799 {
9800 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9801 }
9802 
9803 /**
9804  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9805  * @pf: board private structure
9806  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9807 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9808 {
9809 
9810 	/* if interface is down do nothing */
9811 	if (test_bit(__I40E_DOWN, pf->state))
9812 		return;
9813 
9814 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9815 		i40e_fdir_flush_and_replay(pf);
9816 
9817 	i40e_fdir_check_and_reenable(pf);
9818 
9819 }
9820 
9821 /**
9822  * i40e_vsi_link_event - notify VSI of a link event
9823  * @vsi: vsi to be notified
9824  * @link_up: link up or down
9825  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9826 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9827 {
9828 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9829 		return;
9830 
9831 	switch (vsi->type) {
9832 	case I40E_VSI_MAIN:
9833 		if (!vsi->netdev || !vsi->netdev_registered)
9834 			break;
9835 
9836 		if (link_up) {
9837 			netif_carrier_on(vsi->netdev);
9838 			netif_tx_wake_all_queues(vsi->netdev);
9839 		} else {
9840 			netif_carrier_off(vsi->netdev);
9841 			netif_tx_stop_all_queues(vsi->netdev);
9842 		}
9843 		break;
9844 
9845 	case I40E_VSI_SRIOV:
9846 	case I40E_VSI_VMDQ2:
9847 	case I40E_VSI_CTRL:
9848 	case I40E_VSI_IWARP:
9849 	case I40E_VSI_MIRROR:
9850 	default:
9851 		/* there is no notification for other VSIs */
9852 		break;
9853 	}
9854 }
9855 
9856 /**
9857  * i40e_veb_link_event - notify elements on the veb of a link event
9858  * @veb: veb to be notified
9859  * @link_up: link up or down
9860  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9861 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9862 {
9863 	struct i40e_pf *pf;
9864 	int i;
9865 
9866 	if (!veb || !veb->pf)
9867 		return;
9868 	pf = veb->pf;
9869 
9870 	/* depth first... */
9871 	for (i = 0; i < I40E_MAX_VEB; i++)
9872 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9873 			i40e_veb_link_event(pf->veb[i], link_up);
9874 
9875 	/* ... now the local VSIs */
9876 	for (i = 0; i < pf->num_alloc_vsi; i++)
9877 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9878 			i40e_vsi_link_event(pf->vsi[i], link_up);
9879 }
9880 
9881 /**
9882  * i40e_link_event - Update netif_carrier status
9883  * @pf: board private structure
9884  **/
i40e_link_event(struct i40e_pf * pf)9885 static void i40e_link_event(struct i40e_pf *pf)
9886 {
9887 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9888 	u8 new_link_speed, old_link_speed;
9889 	i40e_status status;
9890 	bool new_link, old_link;
9891 #ifdef CONFIG_I40E_DCB
9892 	int err;
9893 #endif /* CONFIG_I40E_DCB */
9894 
9895 	/* set this to force the get_link_status call to refresh state */
9896 	pf->hw.phy.get_link_info = true;
9897 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9898 	status = i40e_get_link_status(&pf->hw, &new_link);
9899 
9900 	/* On success, disable temp link polling */
9901 	if (status == I40E_SUCCESS) {
9902 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9903 	} else {
9904 		/* Enable link polling temporarily until i40e_get_link_status
9905 		 * returns I40E_SUCCESS
9906 		 */
9907 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9908 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9909 			status);
9910 		return;
9911 	}
9912 
9913 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9914 	new_link_speed = pf->hw.phy.link_info.link_speed;
9915 
9916 	if (new_link == old_link &&
9917 	    new_link_speed == old_link_speed &&
9918 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9919 	     new_link == netif_carrier_ok(vsi->netdev)))
9920 		return;
9921 
9922 	i40e_print_link_message(vsi, new_link);
9923 
9924 	/* Notify the base of the switch tree connected to
9925 	 * the link.  Floating VEBs are not notified.
9926 	 */
9927 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9928 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9929 	else
9930 		i40e_vsi_link_event(vsi, new_link);
9931 
9932 	if (pf->vf)
9933 		i40e_vc_notify_link_state(pf);
9934 
9935 	if (pf->flags & I40E_FLAG_PTP)
9936 		i40e_ptp_set_increment(pf);
9937 #ifdef CONFIG_I40E_DCB
9938 	if (new_link == old_link)
9939 		return;
9940 	/* Not SW DCB so firmware will take care of default settings */
9941 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9942 		return;
9943 
9944 	/* We cover here only link down, as after link up in case of SW DCB
9945 	 * SW LLDP agent will take care of setting it up
9946 	 */
9947 	if (!new_link) {
9948 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9949 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9950 		err = i40e_dcb_sw_default_config(pf);
9951 		if (err) {
9952 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9953 				       I40E_FLAG_DCB_ENABLED);
9954 		} else {
9955 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9956 				       DCB_CAP_DCBX_VER_IEEE;
9957 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9958 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9959 		}
9960 	}
9961 #endif /* CONFIG_I40E_DCB */
9962 }
9963 
9964 /**
9965  * i40e_watchdog_subtask - periodic checks not using event driven response
9966  * @pf: board private structure
9967  **/
i40e_watchdog_subtask(struct i40e_pf * pf)9968 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9969 {
9970 	int i;
9971 
9972 	/* if interface is down do nothing */
9973 	if (test_bit(__I40E_DOWN, pf->state) ||
9974 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9975 		return;
9976 
9977 	/* make sure we don't do these things too often */
9978 	if (time_before(jiffies, (pf->service_timer_previous +
9979 				  pf->service_timer_period)))
9980 		return;
9981 	pf->service_timer_previous = jiffies;
9982 
9983 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9984 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9985 		i40e_link_event(pf);
9986 
9987 	/* Update the stats for active netdevs so the network stack
9988 	 * can look at updated numbers whenever it cares to
9989 	 */
9990 	for (i = 0; i < pf->num_alloc_vsi; i++)
9991 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9992 			i40e_update_stats(pf->vsi[i]);
9993 
9994 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9995 		/* Update the stats for the active switching components */
9996 		for (i = 0; i < I40E_MAX_VEB; i++)
9997 			if (pf->veb[i])
9998 				i40e_update_veb_stats(pf->veb[i]);
9999 	}
10000 
10001 	i40e_ptp_rx_hang(pf);
10002 	i40e_ptp_tx_hang(pf);
10003 }
10004 
10005 /**
10006  * i40e_reset_subtask - Set up for resetting the device and driver
10007  * @pf: board private structure
10008  **/
i40e_reset_subtask(struct i40e_pf * pf)10009 static void i40e_reset_subtask(struct i40e_pf *pf)
10010 {
10011 	u32 reset_flags = 0;
10012 
10013 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10014 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10015 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10016 	}
10017 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10018 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10019 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10020 	}
10021 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10022 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10023 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10024 	}
10025 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10026 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10027 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10028 	}
10029 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10030 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10031 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10032 	}
10033 
10034 	/* If there's a recovery already waiting, it takes
10035 	 * precedence before starting a new reset sequence.
10036 	 */
10037 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10038 		i40e_prep_for_reset(pf);
10039 		i40e_reset(pf);
10040 		i40e_rebuild(pf, false, false);
10041 	}
10042 
10043 	/* If we're already down or resetting, just bail */
10044 	if (reset_flags &&
10045 	    !test_bit(__I40E_DOWN, pf->state) &&
10046 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10047 		i40e_do_reset(pf, reset_flags, false);
10048 	}
10049 }
10050 
10051 /**
10052  * i40e_handle_link_event - Handle link event
10053  * @pf: board private structure
10054  * @e: event info posted on ARQ
10055  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)10056 static void i40e_handle_link_event(struct i40e_pf *pf,
10057 				   struct i40e_arq_event_info *e)
10058 {
10059 	struct i40e_aqc_get_link_status *status =
10060 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10061 
10062 	/* Do a new status request to re-enable LSE reporting
10063 	 * and load new status information into the hw struct
10064 	 * This completely ignores any state information
10065 	 * in the ARQ event info, instead choosing to always
10066 	 * issue the AQ update link status command.
10067 	 */
10068 	i40e_link_event(pf);
10069 
10070 	/* Check if module meets thermal requirements */
10071 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10072 		dev_err(&pf->pdev->dev,
10073 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10074 		dev_err(&pf->pdev->dev,
10075 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10076 	} else {
10077 		/* check for unqualified module, if link is down, suppress
10078 		 * the message if link was forced to be down.
10079 		 */
10080 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10081 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10082 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10083 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10084 			dev_err(&pf->pdev->dev,
10085 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10086 			dev_err(&pf->pdev->dev,
10087 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10088 		}
10089 	}
10090 }
10091 
10092 /**
10093  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10094  * @pf: board private structure
10095  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)10096 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10097 {
10098 	struct i40e_arq_event_info event;
10099 	struct i40e_hw *hw = &pf->hw;
10100 	u16 pending, i = 0;
10101 	i40e_status ret;
10102 	u16 opcode;
10103 	u32 oldval;
10104 	u32 val;
10105 
10106 	/* Do not run clean AQ when PF reset fails */
10107 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10108 		return;
10109 
10110 	/* check for error indications */
10111 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10112 	oldval = val;
10113 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10114 		if (hw->debug_mask & I40E_DEBUG_AQ)
10115 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10116 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10117 	}
10118 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10119 		if (hw->debug_mask & I40E_DEBUG_AQ)
10120 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10121 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10122 		pf->arq_overflows++;
10123 	}
10124 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10125 		if (hw->debug_mask & I40E_DEBUG_AQ)
10126 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10127 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10128 	}
10129 	if (oldval != val)
10130 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10131 
10132 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10133 	oldval = val;
10134 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10135 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10136 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10137 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10138 	}
10139 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10140 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10141 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10142 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10143 	}
10144 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10145 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10146 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10147 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10148 	}
10149 	if (oldval != val)
10150 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10151 
10152 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10153 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10154 	if (!event.msg_buf)
10155 		return;
10156 
10157 	do {
10158 		ret = i40e_clean_arq_element(hw, &event, &pending);
10159 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10160 			break;
10161 		else if (ret) {
10162 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10163 			break;
10164 		}
10165 
10166 		opcode = le16_to_cpu(event.desc.opcode);
10167 		switch (opcode) {
10168 
10169 		case i40e_aqc_opc_get_link_status:
10170 			rtnl_lock();
10171 			i40e_handle_link_event(pf, &event);
10172 			rtnl_unlock();
10173 			break;
10174 		case i40e_aqc_opc_send_msg_to_pf:
10175 			ret = i40e_vc_process_vf_msg(pf,
10176 					le16_to_cpu(event.desc.retval),
10177 					le32_to_cpu(event.desc.cookie_high),
10178 					le32_to_cpu(event.desc.cookie_low),
10179 					event.msg_buf,
10180 					event.msg_len);
10181 			break;
10182 		case i40e_aqc_opc_lldp_update_mib:
10183 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10184 #ifdef CONFIG_I40E_DCB
10185 			rtnl_lock();
10186 			i40e_handle_lldp_event(pf, &event);
10187 			rtnl_unlock();
10188 #endif /* CONFIG_I40E_DCB */
10189 			break;
10190 		case i40e_aqc_opc_event_lan_overflow:
10191 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10192 			i40e_handle_lan_overflow_event(pf, &event);
10193 			break;
10194 		case i40e_aqc_opc_send_msg_to_peer:
10195 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10196 			break;
10197 		case i40e_aqc_opc_nvm_erase:
10198 		case i40e_aqc_opc_nvm_update:
10199 		case i40e_aqc_opc_oem_post_update:
10200 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10201 				   "ARQ NVM operation 0x%04x completed\n",
10202 				   opcode);
10203 			break;
10204 		default:
10205 			dev_info(&pf->pdev->dev,
10206 				 "ARQ: Unknown event 0x%04x ignored\n",
10207 				 opcode);
10208 			break;
10209 		}
10210 	} while (i++ < pf->adminq_work_limit);
10211 
10212 	if (i < pf->adminq_work_limit)
10213 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10214 
10215 	/* re-enable Admin queue interrupt cause */
10216 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10217 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10218 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10219 	i40e_flush(hw);
10220 
10221 	kfree(event.msg_buf);
10222 }
10223 
10224 /**
10225  * i40e_verify_eeprom - make sure eeprom is good to use
10226  * @pf: board private structure
10227  **/
i40e_verify_eeprom(struct i40e_pf * pf)10228 static void i40e_verify_eeprom(struct i40e_pf *pf)
10229 {
10230 	int err;
10231 
10232 	err = i40e_diag_eeprom_test(&pf->hw);
10233 	if (err) {
10234 		/* retry in case of garbage read */
10235 		err = i40e_diag_eeprom_test(&pf->hw);
10236 		if (err) {
10237 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10238 				 err);
10239 			set_bit(__I40E_BAD_EEPROM, pf->state);
10240 		}
10241 	}
10242 
10243 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10244 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10245 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10246 	}
10247 }
10248 
10249 /**
10250  * i40e_enable_pf_switch_lb
10251  * @pf: pointer to the PF structure
10252  *
10253  * enable switch loop back or die - no point in a return value
10254  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)10255 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10256 {
10257 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10258 	struct i40e_vsi_context ctxt;
10259 	int ret;
10260 
10261 	ctxt.seid = pf->main_vsi_seid;
10262 	ctxt.pf_num = pf->hw.pf_id;
10263 	ctxt.vf_num = 0;
10264 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10265 	if (ret) {
10266 		dev_info(&pf->pdev->dev,
10267 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10268 			 i40e_stat_str(&pf->hw, ret),
10269 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10270 		return;
10271 	}
10272 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10273 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10274 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10275 
10276 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10277 	if (ret) {
10278 		dev_info(&pf->pdev->dev,
10279 			 "update vsi switch failed, err %s aq_err %s\n",
10280 			 i40e_stat_str(&pf->hw, ret),
10281 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10282 	}
10283 }
10284 
10285 /**
10286  * i40e_disable_pf_switch_lb
10287  * @pf: pointer to the PF structure
10288  *
10289  * disable switch loop back or die - no point in a return value
10290  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)10291 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10292 {
10293 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10294 	struct i40e_vsi_context ctxt;
10295 	int ret;
10296 
10297 	ctxt.seid = pf->main_vsi_seid;
10298 	ctxt.pf_num = pf->hw.pf_id;
10299 	ctxt.vf_num = 0;
10300 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10301 	if (ret) {
10302 		dev_info(&pf->pdev->dev,
10303 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10304 			 i40e_stat_str(&pf->hw, ret),
10305 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10306 		return;
10307 	}
10308 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10309 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10310 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10311 
10312 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10313 	if (ret) {
10314 		dev_info(&pf->pdev->dev,
10315 			 "update vsi switch failed, err %s aq_err %s\n",
10316 			 i40e_stat_str(&pf->hw, ret),
10317 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10318 	}
10319 }
10320 
10321 /**
10322  * i40e_config_bridge_mode - Configure the HW bridge mode
10323  * @veb: pointer to the bridge instance
10324  *
10325  * Configure the loop back mode for the LAN VSI that is downlink to the
10326  * specified HW bridge instance. It is expected this function is called
10327  * when a new HW bridge is instantiated.
10328  **/
i40e_config_bridge_mode(struct i40e_veb * veb)10329 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10330 {
10331 	struct i40e_pf *pf = veb->pf;
10332 
10333 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10334 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10335 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10336 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10337 		i40e_disable_pf_switch_lb(pf);
10338 	else
10339 		i40e_enable_pf_switch_lb(pf);
10340 }
10341 
10342 /**
10343  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10344  * @veb: pointer to the VEB instance
10345  *
10346  * This is a recursive function that first builds the attached VSIs then
10347  * recurses in to build the next layer of VEB.  We track the connections
10348  * through our own index numbers because the seid's from the HW could
10349  * change across the reset.
10350  **/
i40e_reconstitute_veb(struct i40e_veb * veb)10351 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10352 {
10353 	struct i40e_vsi *ctl_vsi = NULL;
10354 	struct i40e_pf *pf = veb->pf;
10355 	int v, veb_idx;
10356 	int ret;
10357 
10358 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10359 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10360 		if (pf->vsi[v] &&
10361 		    pf->vsi[v]->veb_idx == veb->idx &&
10362 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10363 			ctl_vsi = pf->vsi[v];
10364 			break;
10365 		}
10366 	}
10367 	if (!ctl_vsi) {
10368 		dev_info(&pf->pdev->dev,
10369 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10370 		ret = -ENOENT;
10371 		goto end_reconstitute;
10372 	}
10373 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10374 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10375 	ret = i40e_add_vsi(ctl_vsi);
10376 	if (ret) {
10377 		dev_info(&pf->pdev->dev,
10378 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10379 			 veb->idx, ret);
10380 		goto end_reconstitute;
10381 	}
10382 	i40e_vsi_reset_stats(ctl_vsi);
10383 
10384 	/* create the VEB in the switch and move the VSI onto the VEB */
10385 	ret = i40e_add_veb(veb, ctl_vsi);
10386 	if (ret)
10387 		goto end_reconstitute;
10388 
10389 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10390 		veb->bridge_mode = BRIDGE_MODE_VEB;
10391 	else
10392 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10393 	i40e_config_bridge_mode(veb);
10394 
10395 	/* create the remaining VSIs attached to this VEB */
10396 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10397 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10398 			continue;
10399 
10400 		if (pf->vsi[v]->veb_idx == veb->idx) {
10401 			struct i40e_vsi *vsi = pf->vsi[v];
10402 
10403 			vsi->uplink_seid = veb->seid;
10404 			ret = i40e_add_vsi(vsi);
10405 			if (ret) {
10406 				dev_info(&pf->pdev->dev,
10407 					 "rebuild of vsi_idx %d failed: %d\n",
10408 					 v, ret);
10409 				goto end_reconstitute;
10410 			}
10411 			i40e_vsi_reset_stats(vsi);
10412 		}
10413 	}
10414 
10415 	/* create any VEBs attached to this VEB - RECURSION */
10416 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10417 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10418 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10419 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10420 			if (ret)
10421 				break;
10422 		}
10423 	}
10424 
10425 end_reconstitute:
10426 	return ret;
10427 }
10428 
10429 /**
10430  * i40e_get_capabilities - get info about the HW
10431  * @pf: the PF struct
10432  * @list_type: AQ capability to be queried
10433  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)10434 static int i40e_get_capabilities(struct i40e_pf *pf,
10435 				 enum i40e_admin_queue_opc list_type)
10436 {
10437 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10438 	u16 data_size;
10439 	int buf_len;
10440 	int err;
10441 
10442 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10443 	do {
10444 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10445 		if (!cap_buf)
10446 			return -ENOMEM;
10447 
10448 		/* this loads the data into the hw struct for us */
10449 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10450 						    &data_size, list_type,
10451 						    NULL);
10452 		/* data loaded, buffer no longer needed */
10453 		kfree(cap_buf);
10454 
10455 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10456 			/* retry with a larger buffer */
10457 			buf_len = data_size;
10458 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10459 			dev_info(&pf->pdev->dev,
10460 				 "capability discovery failed, err %s aq_err %s\n",
10461 				 i40e_stat_str(&pf->hw, err),
10462 				 i40e_aq_str(&pf->hw,
10463 					     pf->hw.aq.asq_last_status));
10464 			return -ENODEV;
10465 		}
10466 	} while (err);
10467 
10468 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10469 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10470 			dev_info(&pf->pdev->dev,
10471 				 "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",
10472 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10473 				 pf->hw.func_caps.num_msix_vectors,
10474 				 pf->hw.func_caps.num_msix_vectors_vf,
10475 				 pf->hw.func_caps.fd_filters_guaranteed,
10476 				 pf->hw.func_caps.fd_filters_best_effort,
10477 				 pf->hw.func_caps.num_tx_qp,
10478 				 pf->hw.func_caps.num_vsis);
10479 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10480 			dev_info(&pf->pdev->dev,
10481 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10482 				 pf->hw.dev_caps.switch_mode,
10483 				 pf->hw.dev_caps.valid_functions);
10484 			dev_info(&pf->pdev->dev,
10485 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10486 				 pf->hw.dev_caps.sr_iov_1_1,
10487 				 pf->hw.dev_caps.num_vfs);
10488 			dev_info(&pf->pdev->dev,
10489 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10490 				 pf->hw.dev_caps.num_vsis,
10491 				 pf->hw.dev_caps.num_rx_qp,
10492 				 pf->hw.dev_caps.num_tx_qp);
10493 		}
10494 	}
10495 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10496 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10497 		       + pf->hw.func_caps.num_vfs)
10498 		if (pf->hw.revision_id == 0 &&
10499 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10500 			dev_info(&pf->pdev->dev,
10501 				 "got num_vsis %d, setting num_vsis to %d\n",
10502 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10503 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10504 		}
10505 	}
10506 	return 0;
10507 }
10508 
10509 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10510 
10511 /**
10512  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10513  * @pf: board private structure
10514  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)10515 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10516 {
10517 	struct i40e_vsi *vsi;
10518 
10519 	/* quick workaround for an NVM issue that leaves a critical register
10520 	 * uninitialized
10521 	 */
10522 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10523 		static const u32 hkey[] = {
10524 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10525 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10526 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10527 			0x95b3a76d};
10528 		int i;
10529 
10530 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10531 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10532 	}
10533 
10534 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10535 		return;
10536 
10537 	/* find existing VSI and see if it needs configuring */
10538 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10539 
10540 	/* create a new VSI if none exists */
10541 	if (!vsi) {
10542 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10543 				     pf->vsi[pf->lan_vsi]->seid, 0);
10544 		if (!vsi) {
10545 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10546 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10547 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10548 			return;
10549 		}
10550 	}
10551 
10552 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10553 }
10554 
10555 /**
10556  * i40e_fdir_teardown - release the Flow Director resources
10557  * @pf: board private structure
10558  **/
i40e_fdir_teardown(struct i40e_pf * pf)10559 static void i40e_fdir_teardown(struct i40e_pf *pf)
10560 {
10561 	struct i40e_vsi *vsi;
10562 
10563 	i40e_fdir_filter_exit(pf);
10564 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10565 	if (vsi)
10566 		i40e_vsi_release(vsi);
10567 }
10568 
10569 /**
10570  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10571  * @vsi: PF main vsi
10572  * @seid: seid of main or channel VSIs
10573  *
10574  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10575  * existed before reset
10576  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)10577 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10578 {
10579 	struct i40e_cloud_filter *cfilter;
10580 	struct i40e_pf *pf = vsi->back;
10581 	struct hlist_node *node;
10582 	i40e_status ret;
10583 
10584 	/* Add cloud filters back if they exist */
10585 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10586 				  cloud_node) {
10587 		if (cfilter->seid != seid)
10588 			continue;
10589 
10590 		if (cfilter->dst_port)
10591 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10592 								true);
10593 		else
10594 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10595 
10596 		if (ret) {
10597 			dev_dbg(&pf->pdev->dev,
10598 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10599 				i40e_stat_str(&pf->hw, ret),
10600 				i40e_aq_str(&pf->hw,
10601 					    pf->hw.aq.asq_last_status));
10602 			return ret;
10603 		}
10604 	}
10605 	return 0;
10606 }
10607 
10608 /**
10609  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10610  * @vsi: PF main vsi
10611  *
10612  * Rebuilds channel VSIs if they existed before reset
10613  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)10614 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10615 {
10616 	struct i40e_channel *ch, *ch_tmp;
10617 	i40e_status ret;
10618 
10619 	if (list_empty(&vsi->ch_list))
10620 		return 0;
10621 
10622 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10623 		if (!ch->initialized)
10624 			break;
10625 		/* Proceed with creation of channel (VMDq2) VSI */
10626 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10627 		if (ret) {
10628 			dev_info(&vsi->back->pdev->dev,
10629 				 "failed to rebuild channels using uplink_seid %u\n",
10630 				 vsi->uplink_seid);
10631 			return ret;
10632 		}
10633 		/* Reconfigure TX queues using QTX_CTL register */
10634 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10635 		if (ret) {
10636 			dev_info(&vsi->back->pdev->dev,
10637 				 "failed to configure TX rings for channel %u\n",
10638 				 ch->seid);
10639 			return ret;
10640 		}
10641 		/* update 'next_base_queue' */
10642 		vsi->next_base_queue = vsi->next_base_queue +
10643 							ch->num_queue_pairs;
10644 		if (ch->max_tx_rate) {
10645 			u64 credits = ch->max_tx_rate;
10646 
10647 			if (i40e_set_bw_limit(vsi, ch->seid,
10648 					      ch->max_tx_rate))
10649 				return -EINVAL;
10650 
10651 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10652 			dev_dbg(&vsi->back->pdev->dev,
10653 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10654 				ch->max_tx_rate,
10655 				credits,
10656 				ch->seid);
10657 		}
10658 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10659 		if (ret) {
10660 			dev_dbg(&vsi->back->pdev->dev,
10661 				"Failed to rebuild cloud filters for channel VSI %u\n",
10662 				ch->seid);
10663 			return ret;
10664 		}
10665 	}
10666 	return 0;
10667 }
10668 
10669 /**
10670  * i40e_clean_xps_state - clean xps state for every tx_ring
10671  * @vsi: ptr to the VSI
10672  **/
i40e_clean_xps_state(struct i40e_vsi * vsi)10673 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10674 {
10675 	int i;
10676 
10677 	if (vsi->tx_rings)
10678 		for (i = 0; i < vsi->num_queue_pairs; i++)
10679 			if (vsi->tx_rings[i])
10680 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10681 					  vsi->tx_rings[i]->state);
10682 }
10683 
10684 /**
10685  * i40e_prep_for_reset - prep for the core to reset
10686  * @pf: board private structure
10687  *
10688  * Close up the VFs and other things in prep for PF Reset.
10689   **/
i40e_prep_for_reset(struct i40e_pf * pf)10690 static void i40e_prep_for_reset(struct i40e_pf *pf)
10691 {
10692 	struct i40e_hw *hw = &pf->hw;
10693 	i40e_status ret = 0;
10694 	u32 v;
10695 
10696 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10697 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10698 		return;
10699 	if (i40e_check_asq_alive(&pf->hw))
10700 		i40e_vc_notify_reset(pf);
10701 
10702 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10703 
10704 	/* quiesce the VSIs and their queues that are not already DOWN */
10705 	i40e_pf_quiesce_all_vsi(pf);
10706 
10707 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10708 		if (pf->vsi[v]) {
10709 			i40e_clean_xps_state(pf->vsi[v]);
10710 			pf->vsi[v]->seid = 0;
10711 		}
10712 	}
10713 
10714 	i40e_shutdown_adminq(&pf->hw);
10715 
10716 	/* call shutdown HMC */
10717 	if (hw->hmc.hmc_obj) {
10718 		ret = i40e_shutdown_lan_hmc(hw);
10719 		if (ret)
10720 			dev_warn(&pf->pdev->dev,
10721 				 "shutdown_lan_hmc failed: %d\n", ret);
10722 	}
10723 
10724 	/* Save the current PTP time so that we can restore the time after the
10725 	 * reset completes.
10726 	 */
10727 	i40e_ptp_save_hw_time(pf);
10728 }
10729 
10730 /**
10731  * i40e_send_version - update firmware with driver version
10732  * @pf: PF struct
10733  */
i40e_send_version(struct i40e_pf * pf)10734 static void i40e_send_version(struct i40e_pf *pf)
10735 {
10736 	struct i40e_driver_version dv;
10737 
10738 	dv.major_version = 0xff;
10739 	dv.minor_version = 0xff;
10740 	dv.build_version = 0xff;
10741 	dv.subbuild_version = 0;
10742 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10743 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10744 }
10745 
10746 /**
10747  * i40e_get_oem_version - get OEM specific version information
10748  * @hw: pointer to the hardware structure
10749  **/
i40e_get_oem_version(struct i40e_hw * hw)10750 static void i40e_get_oem_version(struct i40e_hw *hw)
10751 {
10752 	u16 block_offset = 0xffff;
10753 	u16 block_length = 0;
10754 	u16 capabilities = 0;
10755 	u16 gen_snap = 0;
10756 	u16 release = 0;
10757 
10758 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10759 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10760 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10761 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10762 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10763 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10764 #define I40E_NVM_OEM_LENGTH			3
10765 
10766 	/* Check if pointer to OEM version block is valid. */
10767 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10768 	if (block_offset == 0xffff)
10769 		return;
10770 
10771 	/* Check if OEM version block has correct length. */
10772 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10773 			   &block_length);
10774 	if (block_length < I40E_NVM_OEM_LENGTH)
10775 		return;
10776 
10777 	/* Check if OEM version format is as expected. */
10778 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10779 			   &capabilities);
10780 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10781 		return;
10782 
10783 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10784 			   &gen_snap);
10785 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10786 			   &release);
10787 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10788 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10789 }
10790 
10791 /**
10792  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10793  * @pf: board private structure
10794  **/
i40e_reset(struct i40e_pf * pf)10795 static int i40e_reset(struct i40e_pf *pf)
10796 {
10797 	struct i40e_hw *hw = &pf->hw;
10798 	i40e_status ret;
10799 
10800 	ret = i40e_pf_reset(hw);
10801 	if (ret) {
10802 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10803 		set_bit(__I40E_RESET_FAILED, pf->state);
10804 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10805 	} else {
10806 		pf->pfr_count++;
10807 	}
10808 	return ret;
10809 }
10810 
10811 /**
10812  * i40e_rebuild - rebuild using a saved config
10813  * @pf: board private structure
10814  * @reinit: if the Main VSI needs to re-initialized.
10815  * @lock_acquired: indicates whether or not the lock has been acquired
10816  * before this function was called.
10817  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10818 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10819 {
10820 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10821 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10822 	struct i40e_hw *hw = &pf->hw;
10823 	i40e_status ret;
10824 	u32 val;
10825 	int v;
10826 
10827 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10828 	    is_recovery_mode_reported)
10829 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10830 
10831 	if (test_bit(__I40E_DOWN, pf->state) &&
10832 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10833 		goto clear_recovery;
10834 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10835 
10836 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10837 	ret = i40e_init_adminq(&pf->hw);
10838 	if (ret) {
10839 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10840 			 i40e_stat_str(&pf->hw, ret),
10841 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10842 		goto clear_recovery;
10843 	}
10844 	i40e_get_oem_version(&pf->hw);
10845 
10846 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10847 		/* The following delay is necessary for firmware update. */
10848 		mdelay(1000);
10849 	}
10850 
10851 	/* re-verify the eeprom if we just had an EMP reset */
10852 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10853 		i40e_verify_eeprom(pf);
10854 
10855 	/* if we are going out of or into recovery mode we have to act
10856 	 * accordingly with regard to resources initialization
10857 	 * and deinitialization
10858 	 */
10859 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10860 		if (i40e_get_capabilities(pf,
10861 					  i40e_aqc_opc_list_func_capabilities))
10862 			goto end_unlock;
10863 
10864 		if (is_recovery_mode_reported) {
10865 			/* we're staying in recovery mode so we'll reinitialize
10866 			 * misc vector here
10867 			 */
10868 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10869 				goto end_unlock;
10870 		} else {
10871 			if (!lock_acquired)
10872 				rtnl_lock();
10873 			/* we're going out of recovery mode so we'll free
10874 			 * the IRQ allocated specifically for recovery mode
10875 			 * and restore the interrupt scheme
10876 			 */
10877 			free_irq(pf->pdev->irq, pf);
10878 			i40e_clear_interrupt_scheme(pf);
10879 			if (i40e_restore_interrupt_scheme(pf))
10880 				goto end_unlock;
10881 		}
10882 
10883 		/* tell the firmware that we're starting */
10884 		i40e_send_version(pf);
10885 
10886 		/* bail out in case recovery mode was detected, as there is
10887 		 * no need for further configuration.
10888 		 */
10889 		goto end_unlock;
10890 	}
10891 
10892 	i40e_clear_pxe_mode(hw);
10893 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10894 	if (ret)
10895 		goto end_core_reset;
10896 
10897 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10898 				hw->func_caps.num_rx_qp, 0, 0);
10899 	if (ret) {
10900 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10901 		goto end_core_reset;
10902 	}
10903 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10904 	if (ret) {
10905 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10906 		goto end_core_reset;
10907 	}
10908 
10909 #ifdef CONFIG_I40E_DCB
10910 	/* Enable FW to write a default DCB config on link-up
10911 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10912 	 * is not supported with new link speed
10913 	 */
10914 	if (i40e_is_tc_mqprio_enabled(pf)) {
10915 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10916 	} else {
10917 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10918 		    (hw->phy.link_info.link_speed &
10919 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10920 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10921 			dev_warn(&pf->pdev->dev,
10922 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10923 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10924 		} else {
10925 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10926 			ret = i40e_init_pf_dcb(pf);
10927 			if (ret) {
10928 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10929 					 ret);
10930 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10931 				/* Continue without DCB enabled */
10932 			}
10933 		}
10934 	}
10935 
10936 #endif /* CONFIG_I40E_DCB */
10937 	if (!lock_acquired)
10938 		rtnl_lock();
10939 	ret = i40e_setup_pf_switch(pf, reinit, true);
10940 	if (ret)
10941 		goto end_unlock;
10942 
10943 	/* The driver only wants link up/down and module qualification
10944 	 * reports from firmware.  Note the negative logic.
10945 	 */
10946 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10947 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10948 					 I40E_AQ_EVENT_MEDIA_NA |
10949 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10950 	if (ret)
10951 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10952 			 i40e_stat_str(&pf->hw, ret),
10953 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10954 
10955 	/* Rebuild the VSIs and VEBs that existed before reset.
10956 	 * They are still in our local switch element arrays, so only
10957 	 * need to rebuild the switch model in the HW.
10958 	 *
10959 	 * If there were VEBs but the reconstitution failed, we'll try
10960 	 * to recover minimal use by getting the basic PF VSI working.
10961 	 */
10962 	if (vsi->uplink_seid != pf->mac_seid) {
10963 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10964 		/* find the one VEB connected to the MAC, and find orphans */
10965 		for (v = 0; v < I40E_MAX_VEB; v++) {
10966 			if (!pf->veb[v])
10967 				continue;
10968 
10969 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10970 			    pf->veb[v]->uplink_seid == 0) {
10971 				ret = i40e_reconstitute_veb(pf->veb[v]);
10972 
10973 				if (!ret)
10974 					continue;
10975 
10976 				/* If Main VEB failed, we're in deep doodoo,
10977 				 * so give up rebuilding the switch and set up
10978 				 * for minimal rebuild of PF VSI.
10979 				 * If orphan failed, we'll report the error
10980 				 * but try to keep going.
10981 				 */
10982 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10983 					dev_info(&pf->pdev->dev,
10984 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10985 						 ret);
10986 					vsi->uplink_seid = pf->mac_seid;
10987 					break;
10988 				} else if (pf->veb[v]->uplink_seid == 0) {
10989 					dev_info(&pf->pdev->dev,
10990 						 "rebuild of orphan VEB failed: %d\n",
10991 						 ret);
10992 				}
10993 			}
10994 		}
10995 	}
10996 
10997 	if (vsi->uplink_seid == pf->mac_seid) {
10998 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10999 		/* no VEB, so rebuild only the Main VSI */
11000 		ret = i40e_add_vsi(vsi);
11001 		if (ret) {
11002 			dev_info(&pf->pdev->dev,
11003 				 "rebuild of Main VSI failed: %d\n", ret);
11004 			goto end_unlock;
11005 		}
11006 	}
11007 
11008 	if (vsi->mqprio_qopt.max_rate[0]) {
11009 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11010 						  vsi->mqprio_qopt.max_rate[0]);
11011 		u64 credits = 0;
11012 
11013 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11014 		if (ret)
11015 			goto end_unlock;
11016 
11017 		credits = max_tx_rate;
11018 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11019 		dev_dbg(&vsi->back->pdev->dev,
11020 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11021 			max_tx_rate,
11022 			credits,
11023 			vsi->seid);
11024 	}
11025 
11026 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11027 	if (ret)
11028 		goto end_unlock;
11029 
11030 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11031 	 * for this main VSI if they exist
11032 	 */
11033 	ret = i40e_rebuild_channels(vsi);
11034 	if (ret)
11035 		goto end_unlock;
11036 
11037 	/* Reconfigure hardware for allowing smaller MSS in the case
11038 	 * of TSO, so that we avoid the MDD being fired and causing
11039 	 * a reset in the case of small MSS+TSO.
11040 	 */
11041 #define I40E_REG_MSS          0x000E64DC
11042 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11043 #define I40E_64BYTE_MSS       0x400000
11044 	val = rd32(hw, I40E_REG_MSS);
11045 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11046 		val &= ~I40E_REG_MSS_MIN_MASK;
11047 		val |= I40E_64BYTE_MSS;
11048 		wr32(hw, I40E_REG_MSS, val);
11049 	}
11050 
11051 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11052 		msleep(75);
11053 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11054 		if (ret)
11055 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11056 				 i40e_stat_str(&pf->hw, ret),
11057 				 i40e_aq_str(&pf->hw,
11058 					     pf->hw.aq.asq_last_status));
11059 	}
11060 	/* reinit the misc interrupt */
11061 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11062 		ret = i40e_setup_misc_vector(pf);
11063 
11064 	/* Add a filter to drop all Flow control frames from any VSI from being
11065 	 * transmitted. By doing so we stop a malicious VF from sending out
11066 	 * PAUSE or PFC frames and potentially controlling traffic for other
11067 	 * PF/VF VSIs.
11068 	 * The FW can still send Flow control frames if enabled.
11069 	 */
11070 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11071 						       pf->main_vsi_seid);
11072 
11073 	/* restart the VSIs that were rebuilt and running before the reset */
11074 	i40e_pf_unquiesce_all_vsi(pf);
11075 
11076 	/* Release the RTNL lock before we start resetting VFs */
11077 	if (!lock_acquired)
11078 		rtnl_unlock();
11079 
11080 	/* Restore promiscuous settings */
11081 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11082 	if (ret)
11083 		dev_warn(&pf->pdev->dev,
11084 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
11085 			 pf->cur_promisc ? "on" : "off",
11086 			 i40e_stat_str(&pf->hw, ret),
11087 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11088 
11089 	i40e_reset_all_vfs(pf, true);
11090 
11091 	/* tell the firmware that we're starting */
11092 	i40e_send_version(pf);
11093 
11094 	/* We've already released the lock, so don't do it again */
11095 	goto end_core_reset;
11096 
11097 end_unlock:
11098 	if (!lock_acquired)
11099 		rtnl_unlock();
11100 end_core_reset:
11101 	clear_bit(__I40E_RESET_FAILED, pf->state);
11102 clear_recovery:
11103 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11104 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11105 }
11106 
11107 /**
11108  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11109  * @pf: board private structure
11110  * @reinit: if the Main VSI needs to re-initialized.
11111  * @lock_acquired: indicates whether or not the lock has been acquired
11112  * before this function was called.
11113  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)11114 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11115 				   bool lock_acquired)
11116 {
11117 	int ret;
11118 
11119 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11120 		return;
11121 	/* Now we wait for GRST to settle out.
11122 	 * We don't have to delete the VEBs or VSIs from the hw switch
11123 	 * because the reset will make them disappear.
11124 	 */
11125 	ret = i40e_reset(pf);
11126 	if (!ret)
11127 		i40e_rebuild(pf, reinit, lock_acquired);
11128 }
11129 
11130 /**
11131  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11132  * @pf: board private structure
11133  *
11134  * Close up the VFs and other things in prep for a Core Reset,
11135  * then get ready to rebuild the world.
11136  * @lock_acquired: indicates whether or not the lock has been acquired
11137  * before this function was called.
11138  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)11139 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11140 {
11141 	i40e_prep_for_reset(pf);
11142 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11143 }
11144 
11145 /**
11146  * i40e_handle_mdd_event
11147  * @pf: pointer to the PF structure
11148  *
11149  * Called from the MDD irq handler to identify possibly malicious vfs
11150  **/
i40e_handle_mdd_event(struct i40e_pf * pf)11151 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11152 {
11153 	struct i40e_hw *hw = &pf->hw;
11154 	bool mdd_detected = false;
11155 	struct i40e_vf *vf;
11156 	u32 reg;
11157 	int i;
11158 
11159 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11160 		return;
11161 
11162 	/* find what triggered the MDD event */
11163 	reg = rd32(hw, I40E_GL_MDET_TX);
11164 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11165 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11166 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11167 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11168 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11169 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11170 				I40E_GL_MDET_TX_EVENT_SHIFT;
11171 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11172 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11173 				pf->hw.func_caps.base_queue;
11174 		if (netif_msg_tx_err(pf))
11175 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11176 				 event, queue, pf_num, vf_num);
11177 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11178 		mdd_detected = true;
11179 	}
11180 	reg = rd32(hw, I40E_GL_MDET_RX);
11181 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11182 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11183 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11184 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11185 				I40E_GL_MDET_RX_EVENT_SHIFT;
11186 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11187 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11188 				pf->hw.func_caps.base_queue;
11189 		if (netif_msg_rx_err(pf))
11190 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11191 				 event, queue, func);
11192 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11193 		mdd_detected = true;
11194 	}
11195 
11196 	if (mdd_detected) {
11197 		reg = rd32(hw, I40E_PF_MDET_TX);
11198 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11199 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11200 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11201 		}
11202 		reg = rd32(hw, I40E_PF_MDET_RX);
11203 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11204 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11205 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11206 		}
11207 	}
11208 
11209 	/* see if one of the VFs needs its hand slapped */
11210 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11211 		vf = &(pf->vf[i]);
11212 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11213 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11214 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11215 			vf->num_mdd_events++;
11216 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11217 				 i);
11218 			dev_info(&pf->pdev->dev,
11219 				 "Use PF Control I/F to re-enable the VF\n");
11220 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11221 		}
11222 
11223 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11224 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11225 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11226 			vf->num_mdd_events++;
11227 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11228 				 i);
11229 			dev_info(&pf->pdev->dev,
11230 				 "Use PF Control I/F to re-enable the VF\n");
11231 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11232 		}
11233 	}
11234 
11235 	/* re-enable mdd interrupt cause */
11236 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11237 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11238 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11239 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11240 	i40e_flush(hw);
11241 }
11242 
11243 /**
11244  * i40e_service_task - Run the driver's async subtasks
11245  * @work: pointer to work_struct containing our data
11246  **/
i40e_service_task(struct work_struct * work)11247 static void i40e_service_task(struct work_struct *work)
11248 {
11249 	struct i40e_pf *pf = container_of(work,
11250 					  struct i40e_pf,
11251 					  service_task);
11252 	unsigned long start_time = jiffies;
11253 
11254 	/* don't bother with service tasks if a reset is in progress */
11255 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11256 	    test_bit(__I40E_SUSPENDED, pf->state))
11257 		return;
11258 
11259 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11260 		return;
11261 
11262 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11263 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11264 		i40e_sync_filters_subtask(pf);
11265 		i40e_reset_subtask(pf);
11266 		i40e_handle_mdd_event(pf);
11267 		i40e_vc_process_vflr_event(pf);
11268 		i40e_watchdog_subtask(pf);
11269 		i40e_fdir_reinit_subtask(pf);
11270 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11271 			/* Client subtask will reopen next time through. */
11272 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11273 							   true);
11274 		} else {
11275 			i40e_client_subtask(pf);
11276 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11277 					       pf->state))
11278 				i40e_notify_client_of_l2_param_changes(
11279 								pf->vsi[pf->lan_vsi]);
11280 		}
11281 		i40e_sync_filters_subtask(pf);
11282 	} else {
11283 		i40e_reset_subtask(pf);
11284 	}
11285 
11286 	i40e_clean_adminq_subtask(pf);
11287 
11288 	/* flush memory to make sure state is correct before next watchdog */
11289 	smp_mb__before_atomic();
11290 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11291 
11292 	/* If the tasks have taken longer than one timer cycle or there
11293 	 * is more work to be done, reschedule the service task now
11294 	 * rather than wait for the timer to tick again.
11295 	 */
11296 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11297 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11298 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11299 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11300 		i40e_service_event_schedule(pf);
11301 }
11302 
11303 /**
11304  * i40e_service_timer - timer callback
11305  * @t: timer list pointer
11306  **/
i40e_service_timer(struct timer_list * t)11307 static void i40e_service_timer(struct timer_list *t)
11308 {
11309 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11310 
11311 	mod_timer(&pf->service_timer,
11312 		  round_jiffies(jiffies + pf->service_timer_period));
11313 	i40e_service_event_schedule(pf);
11314 }
11315 
11316 /**
11317  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11318  * @vsi: the VSI being configured
11319  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)11320 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11321 {
11322 	struct i40e_pf *pf = vsi->back;
11323 
11324 	switch (vsi->type) {
11325 	case I40E_VSI_MAIN:
11326 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11327 		if (!vsi->num_tx_desc)
11328 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11329 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11330 		if (!vsi->num_rx_desc)
11331 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11332 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11333 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11334 			vsi->num_q_vectors = pf->num_lan_msix;
11335 		else
11336 			vsi->num_q_vectors = 1;
11337 
11338 		break;
11339 
11340 	case I40E_VSI_FDIR:
11341 		vsi->alloc_queue_pairs = 1;
11342 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11343 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11344 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11345 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11346 		vsi->num_q_vectors = pf->num_fdsb_msix;
11347 		break;
11348 
11349 	case I40E_VSI_VMDQ2:
11350 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11351 		if (!vsi->num_tx_desc)
11352 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11353 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11354 		if (!vsi->num_rx_desc)
11355 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11356 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11357 		vsi->num_q_vectors = pf->num_vmdq_msix;
11358 		break;
11359 
11360 	case I40E_VSI_SRIOV:
11361 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11362 		if (!vsi->num_tx_desc)
11363 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11364 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11365 		if (!vsi->num_rx_desc)
11366 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11367 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11368 		break;
11369 
11370 	default:
11371 		WARN_ON(1);
11372 		return -ENODATA;
11373 	}
11374 
11375 	if (is_kdump_kernel()) {
11376 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11377 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11378 	}
11379 
11380 	return 0;
11381 }
11382 
11383 /**
11384  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11385  * @vsi: VSI pointer
11386  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11387  *
11388  * On error: returns error code (negative)
11389  * On success: returns 0
11390  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)11391 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11392 {
11393 	struct i40e_ring **next_rings;
11394 	int size;
11395 	int ret = 0;
11396 
11397 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11398 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11399 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11400 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11401 	if (!vsi->tx_rings)
11402 		return -ENOMEM;
11403 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11404 	if (i40e_enabled_xdp_vsi(vsi)) {
11405 		vsi->xdp_rings = next_rings;
11406 		next_rings += vsi->alloc_queue_pairs;
11407 	}
11408 	vsi->rx_rings = next_rings;
11409 
11410 	if (alloc_qvectors) {
11411 		/* allocate memory for q_vector pointers */
11412 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11413 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11414 		if (!vsi->q_vectors) {
11415 			ret = -ENOMEM;
11416 			goto err_vectors;
11417 		}
11418 	}
11419 	return ret;
11420 
11421 err_vectors:
11422 	kfree(vsi->tx_rings);
11423 	return ret;
11424 }
11425 
11426 /**
11427  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11428  * @pf: board private structure
11429  * @type: type of VSI
11430  *
11431  * On error: returns error code (negative)
11432  * On success: returns vsi index in PF (positive)
11433  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)11434 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11435 {
11436 	int ret = -ENODEV;
11437 	struct i40e_vsi *vsi;
11438 	int vsi_idx;
11439 	int i;
11440 
11441 	/* Need to protect the allocation of the VSIs at the PF level */
11442 	mutex_lock(&pf->switch_mutex);
11443 
11444 	/* VSI list may be fragmented if VSI creation/destruction has
11445 	 * been happening.  We can afford to do a quick scan to look
11446 	 * for any free VSIs in the list.
11447 	 *
11448 	 * find next empty vsi slot, looping back around if necessary
11449 	 */
11450 	i = pf->next_vsi;
11451 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11452 		i++;
11453 	if (i >= pf->num_alloc_vsi) {
11454 		i = 0;
11455 		while (i < pf->next_vsi && pf->vsi[i])
11456 			i++;
11457 	}
11458 
11459 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11460 		vsi_idx = i;             /* Found one! */
11461 	} else {
11462 		ret = -ENODEV;
11463 		goto unlock_pf;  /* out of VSI slots! */
11464 	}
11465 	pf->next_vsi = ++i;
11466 
11467 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11468 	if (!vsi) {
11469 		ret = -ENOMEM;
11470 		goto unlock_pf;
11471 	}
11472 	vsi->type = type;
11473 	vsi->back = pf;
11474 	set_bit(__I40E_VSI_DOWN, vsi->state);
11475 	vsi->flags = 0;
11476 	vsi->idx = vsi_idx;
11477 	vsi->int_rate_limit = 0;
11478 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11479 				pf->rss_table_size : 64;
11480 	vsi->netdev_registered = false;
11481 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11482 	hash_init(vsi->mac_filter_hash);
11483 	vsi->irqs_ready = false;
11484 
11485 	if (type == I40E_VSI_MAIN) {
11486 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11487 		if (!vsi->af_xdp_zc_qps)
11488 			goto err_rings;
11489 	}
11490 
11491 	ret = i40e_set_num_rings_in_vsi(vsi);
11492 	if (ret)
11493 		goto err_rings;
11494 
11495 	ret = i40e_vsi_alloc_arrays(vsi, true);
11496 	if (ret)
11497 		goto err_rings;
11498 
11499 	/* Setup default MSIX irq handler for VSI */
11500 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11501 
11502 	/* Initialize VSI lock */
11503 	spin_lock_init(&vsi->mac_filter_hash_lock);
11504 	pf->vsi[vsi_idx] = vsi;
11505 	ret = vsi_idx;
11506 	goto unlock_pf;
11507 
11508 err_rings:
11509 	bitmap_free(vsi->af_xdp_zc_qps);
11510 	pf->next_vsi = i - 1;
11511 	kfree(vsi);
11512 unlock_pf:
11513 	mutex_unlock(&pf->switch_mutex);
11514 	return ret;
11515 }
11516 
11517 /**
11518  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11519  * @vsi: VSI pointer
11520  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11521  *
11522  * On error: returns error code (negative)
11523  * On success: returns 0
11524  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)11525 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11526 {
11527 	/* free the ring and vector containers */
11528 	if (free_qvectors) {
11529 		kfree(vsi->q_vectors);
11530 		vsi->q_vectors = NULL;
11531 	}
11532 	kfree(vsi->tx_rings);
11533 	vsi->tx_rings = NULL;
11534 	vsi->rx_rings = NULL;
11535 	vsi->xdp_rings = NULL;
11536 }
11537 
11538 /**
11539  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11540  * and lookup table
11541  * @vsi: Pointer to VSI structure
11542  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)11543 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11544 {
11545 	if (!vsi)
11546 		return;
11547 
11548 	kfree(vsi->rss_hkey_user);
11549 	vsi->rss_hkey_user = NULL;
11550 
11551 	kfree(vsi->rss_lut_user);
11552 	vsi->rss_lut_user = NULL;
11553 }
11554 
11555 /**
11556  * i40e_vsi_clear - Deallocate the VSI provided
11557  * @vsi: the VSI being un-configured
11558  **/
i40e_vsi_clear(struct i40e_vsi * vsi)11559 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11560 {
11561 	struct i40e_pf *pf;
11562 
11563 	if (!vsi)
11564 		return 0;
11565 
11566 	if (!vsi->back)
11567 		goto free_vsi;
11568 	pf = vsi->back;
11569 
11570 	mutex_lock(&pf->switch_mutex);
11571 	if (!pf->vsi[vsi->idx]) {
11572 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11573 			vsi->idx, vsi->idx, vsi->type);
11574 		goto unlock_vsi;
11575 	}
11576 
11577 	if (pf->vsi[vsi->idx] != vsi) {
11578 		dev_err(&pf->pdev->dev,
11579 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11580 			pf->vsi[vsi->idx]->idx,
11581 			pf->vsi[vsi->idx]->type,
11582 			vsi->idx, vsi->type);
11583 		goto unlock_vsi;
11584 	}
11585 
11586 	/* updates the PF for this cleared vsi */
11587 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11588 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11589 
11590 	bitmap_free(vsi->af_xdp_zc_qps);
11591 	i40e_vsi_free_arrays(vsi, true);
11592 	i40e_clear_rss_config_user(vsi);
11593 
11594 	pf->vsi[vsi->idx] = NULL;
11595 	if (vsi->idx < pf->next_vsi)
11596 		pf->next_vsi = vsi->idx;
11597 
11598 unlock_vsi:
11599 	mutex_unlock(&pf->switch_mutex);
11600 free_vsi:
11601 	kfree(vsi);
11602 
11603 	return 0;
11604 }
11605 
11606 /**
11607  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11608  * @vsi: the VSI being cleaned
11609  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)11610 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11611 {
11612 	int i;
11613 
11614 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11615 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11616 			kfree_rcu(vsi->tx_rings[i], rcu);
11617 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11618 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11619 			if (vsi->xdp_rings)
11620 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11621 		}
11622 	}
11623 }
11624 
11625 /**
11626  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11627  * @vsi: the VSI being configured
11628  **/
i40e_alloc_rings(struct i40e_vsi * vsi)11629 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11630 {
11631 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11632 	struct i40e_pf *pf = vsi->back;
11633 	struct i40e_ring *ring;
11634 
11635 	/* Set basic values in the rings to be used later during open() */
11636 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11637 		/* allocate space for both Tx and Rx in one shot */
11638 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11639 		if (!ring)
11640 			goto err_out;
11641 
11642 		ring->queue_index = i;
11643 		ring->reg_idx = vsi->base_queue + i;
11644 		ring->ring_active = false;
11645 		ring->vsi = vsi;
11646 		ring->netdev = vsi->netdev;
11647 		ring->dev = &pf->pdev->dev;
11648 		ring->count = vsi->num_tx_desc;
11649 		ring->size = 0;
11650 		ring->dcb_tc = 0;
11651 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11652 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11653 		ring->itr_setting = pf->tx_itr_default;
11654 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11655 
11656 		if (!i40e_enabled_xdp_vsi(vsi))
11657 			goto setup_rx;
11658 
11659 		ring->queue_index = vsi->alloc_queue_pairs + i;
11660 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11661 		ring->ring_active = false;
11662 		ring->vsi = vsi;
11663 		ring->netdev = NULL;
11664 		ring->dev = &pf->pdev->dev;
11665 		ring->count = vsi->num_tx_desc;
11666 		ring->size = 0;
11667 		ring->dcb_tc = 0;
11668 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11669 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11670 		set_ring_xdp(ring);
11671 		ring->itr_setting = pf->tx_itr_default;
11672 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11673 
11674 setup_rx:
11675 		ring->queue_index = i;
11676 		ring->reg_idx = vsi->base_queue + i;
11677 		ring->ring_active = false;
11678 		ring->vsi = vsi;
11679 		ring->netdev = vsi->netdev;
11680 		ring->dev = &pf->pdev->dev;
11681 		ring->count = vsi->num_rx_desc;
11682 		ring->size = 0;
11683 		ring->dcb_tc = 0;
11684 		ring->itr_setting = pf->rx_itr_default;
11685 		WRITE_ONCE(vsi->rx_rings[i], ring);
11686 	}
11687 
11688 	return 0;
11689 
11690 err_out:
11691 	i40e_vsi_clear_rings(vsi);
11692 	return -ENOMEM;
11693 }
11694 
11695 /**
11696  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11697  * @pf: board private structure
11698  * @vectors: the number of MSI-X vectors to request
11699  *
11700  * Returns the number of vectors reserved, or error
11701  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)11702 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11703 {
11704 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11705 					I40E_MIN_MSIX, vectors);
11706 	if (vectors < 0) {
11707 		dev_info(&pf->pdev->dev,
11708 			 "MSI-X vector reservation failed: %d\n", vectors);
11709 		vectors = 0;
11710 	}
11711 
11712 	return vectors;
11713 }
11714 
11715 /**
11716  * i40e_init_msix - Setup the MSIX capability
11717  * @pf: board private structure
11718  *
11719  * Work with the OS to set up the MSIX vectors needed.
11720  *
11721  * Returns the number of vectors reserved or negative on failure
11722  **/
i40e_init_msix(struct i40e_pf * pf)11723 static int i40e_init_msix(struct i40e_pf *pf)
11724 {
11725 	struct i40e_hw *hw = &pf->hw;
11726 	int cpus, extra_vectors;
11727 	int vectors_left;
11728 	int v_budget, i;
11729 	int v_actual;
11730 	int iwarp_requested = 0;
11731 
11732 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11733 		return -ENODEV;
11734 
11735 	/* The number of vectors we'll request will be comprised of:
11736 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11737 	 *   - The number of LAN queue pairs
11738 	 *	- Queues being used for RSS.
11739 	 *		We don't need as many as max_rss_size vectors.
11740 	 *		use rss_size instead in the calculation since that
11741 	 *		is governed by number of cpus in the system.
11742 	 *	- assumes symmetric Tx/Rx pairing
11743 	 *   - The number of VMDq pairs
11744 	 *   - The CPU count within the NUMA node if iWARP is enabled
11745 	 * Once we count this up, try the request.
11746 	 *
11747 	 * If we can't get what we want, we'll simplify to nearly nothing
11748 	 * and try again.  If that still fails, we punt.
11749 	 */
11750 	vectors_left = hw->func_caps.num_msix_vectors;
11751 	v_budget = 0;
11752 
11753 	/* reserve one vector for miscellaneous handler */
11754 	if (vectors_left) {
11755 		v_budget++;
11756 		vectors_left--;
11757 	}
11758 
11759 	/* reserve some vectors for the main PF traffic queues. Initially we
11760 	 * only reserve at most 50% of the available vectors, in the case that
11761 	 * the number of online CPUs is large. This ensures that we can enable
11762 	 * extra features as well. Once we've enabled the other features, we
11763 	 * will use any remaining vectors to reach as close as we can to the
11764 	 * number of online CPUs.
11765 	 */
11766 	cpus = num_online_cpus();
11767 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11768 	vectors_left -= pf->num_lan_msix;
11769 
11770 	/* reserve one vector for sideband flow director */
11771 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11772 		if (vectors_left) {
11773 			pf->num_fdsb_msix = 1;
11774 			v_budget++;
11775 			vectors_left--;
11776 		} else {
11777 			pf->num_fdsb_msix = 0;
11778 		}
11779 	}
11780 
11781 	/* can we reserve enough for iWARP? */
11782 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11783 		iwarp_requested = pf->num_iwarp_msix;
11784 
11785 		if (!vectors_left)
11786 			pf->num_iwarp_msix = 0;
11787 		else if (vectors_left < pf->num_iwarp_msix)
11788 			pf->num_iwarp_msix = 1;
11789 		v_budget += pf->num_iwarp_msix;
11790 		vectors_left -= pf->num_iwarp_msix;
11791 	}
11792 
11793 	/* any vectors left over go for VMDq support */
11794 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11795 		if (!vectors_left) {
11796 			pf->num_vmdq_msix = 0;
11797 			pf->num_vmdq_qps = 0;
11798 		} else {
11799 			int vmdq_vecs_wanted =
11800 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11801 			int vmdq_vecs =
11802 				min_t(int, vectors_left, vmdq_vecs_wanted);
11803 
11804 			/* if we're short on vectors for what's desired, we limit
11805 			 * the queues per vmdq.  If this is still more than are
11806 			 * available, the user will need to change the number of
11807 			 * queues/vectors used by the PF later with the ethtool
11808 			 * channels command
11809 			 */
11810 			if (vectors_left < vmdq_vecs_wanted) {
11811 				pf->num_vmdq_qps = 1;
11812 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11813 				vmdq_vecs = min_t(int,
11814 						  vectors_left,
11815 						  vmdq_vecs_wanted);
11816 			}
11817 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11818 
11819 			v_budget += vmdq_vecs;
11820 			vectors_left -= vmdq_vecs;
11821 		}
11822 	}
11823 
11824 	/* On systems with a large number of SMP cores, we previously limited
11825 	 * the number of vectors for num_lan_msix to be at most 50% of the
11826 	 * available vectors, to allow for other features. Now, we add back
11827 	 * the remaining vectors. However, we ensure that the total
11828 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11829 	 * calculate the number of vectors we can add without going over the
11830 	 * cap of CPUs. For systems with a small number of CPUs this will be
11831 	 * zero.
11832 	 */
11833 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11834 	pf->num_lan_msix += extra_vectors;
11835 	vectors_left -= extra_vectors;
11836 
11837 	WARN(vectors_left < 0,
11838 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11839 
11840 	v_budget += pf->num_lan_msix;
11841 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11842 				   GFP_KERNEL);
11843 	if (!pf->msix_entries)
11844 		return -ENOMEM;
11845 
11846 	for (i = 0; i < v_budget; i++)
11847 		pf->msix_entries[i].entry = i;
11848 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11849 
11850 	if (v_actual < I40E_MIN_MSIX) {
11851 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11852 		kfree(pf->msix_entries);
11853 		pf->msix_entries = NULL;
11854 		pci_disable_msix(pf->pdev);
11855 		return -ENODEV;
11856 
11857 	} else if (v_actual == I40E_MIN_MSIX) {
11858 		/* Adjust for minimal MSIX use */
11859 		pf->num_vmdq_vsis = 0;
11860 		pf->num_vmdq_qps = 0;
11861 		pf->num_lan_qps = 1;
11862 		pf->num_lan_msix = 1;
11863 
11864 	} else if (v_actual != v_budget) {
11865 		/* If we have limited resources, we will start with no vectors
11866 		 * for the special features and then allocate vectors to some
11867 		 * of these features based on the policy and at the end disable
11868 		 * the features that did not get any vectors.
11869 		 */
11870 		int vec;
11871 
11872 		dev_info(&pf->pdev->dev,
11873 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11874 			 v_actual, v_budget);
11875 		/* reserve the misc vector */
11876 		vec = v_actual - 1;
11877 
11878 		/* Scale vector usage down */
11879 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11880 		pf->num_vmdq_vsis = 1;
11881 		pf->num_vmdq_qps = 1;
11882 
11883 		/* partition out the remaining vectors */
11884 		switch (vec) {
11885 		case 2:
11886 			pf->num_lan_msix = 1;
11887 			break;
11888 		case 3:
11889 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11890 				pf->num_lan_msix = 1;
11891 				pf->num_iwarp_msix = 1;
11892 			} else {
11893 				pf->num_lan_msix = 2;
11894 			}
11895 			break;
11896 		default:
11897 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11898 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11899 						 iwarp_requested);
11900 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11901 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11902 			} else {
11903 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11904 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11905 			}
11906 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11907 				pf->num_fdsb_msix = 1;
11908 				vec--;
11909 			}
11910 			pf->num_lan_msix = min_t(int,
11911 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11912 							      pf->num_lan_msix);
11913 			pf->num_lan_qps = pf->num_lan_msix;
11914 			break;
11915 		}
11916 	}
11917 
11918 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11919 	    (pf->num_fdsb_msix == 0)) {
11920 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11921 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11922 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11923 	}
11924 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11925 	    (pf->num_vmdq_msix == 0)) {
11926 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11927 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11928 	}
11929 
11930 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11931 	    (pf->num_iwarp_msix == 0)) {
11932 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11933 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11934 	}
11935 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11936 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11937 		   pf->num_lan_msix,
11938 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11939 		   pf->num_fdsb_msix,
11940 		   pf->num_iwarp_msix);
11941 
11942 	return v_actual;
11943 }
11944 
11945 /**
11946  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11947  * @vsi: the VSI being configured
11948  * @v_idx: index of the vector in the vsi struct
11949  *
11950  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11951  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)11952 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11953 {
11954 	struct i40e_q_vector *q_vector;
11955 
11956 	/* allocate q_vector */
11957 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11958 	if (!q_vector)
11959 		return -ENOMEM;
11960 
11961 	q_vector->vsi = vsi;
11962 	q_vector->v_idx = v_idx;
11963 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11964 
11965 	if (vsi->netdev)
11966 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11967 
11968 	/* tie q_vector and vsi together */
11969 	vsi->q_vectors[v_idx] = q_vector;
11970 
11971 	return 0;
11972 }
11973 
11974 /**
11975  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11976  * @vsi: the VSI being configured
11977  *
11978  * We allocate one q_vector per queue interrupt.  If allocation fails we
11979  * return -ENOMEM.
11980  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)11981 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11982 {
11983 	struct i40e_pf *pf = vsi->back;
11984 	int err, v_idx, num_q_vectors;
11985 
11986 	/* if not MSIX, give the one vector only to the LAN VSI */
11987 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11988 		num_q_vectors = vsi->num_q_vectors;
11989 	else if (vsi == pf->vsi[pf->lan_vsi])
11990 		num_q_vectors = 1;
11991 	else
11992 		return -EINVAL;
11993 
11994 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11995 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11996 		if (err)
11997 			goto err_out;
11998 	}
11999 
12000 	return 0;
12001 
12002 err_out:
12003 	while (v_idx--)
12004 		i40e_free_q_vector(vsi, v_idx);
12005 
12006 	return err;
12007 }
12008 
12009 /**
12010  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12011  * @pf: board private structure to initialize
12012  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)12013 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12014 {
12015 	int vectors = 0;
12016 	ssize_t size;
12017 
12018 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12019 		vectors = i40e_init_msix(pf);
12020 		if (vectors < 0) {
12021 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
12022 				       I40E_FLAG_IWARP_ENABLED	|
12023 				       I40E_FLAG_RSS_ENABLED	|
12024 				       I40E_FLAG_DCB_CAPABLE	|
12025 				       I40E_FLAG_DCB_ENABLED	|
12026 				       I40E_FLAG_SRIOV_ENABLED	|
12027 				       I40E_FLAG_FD_SB_ENABLED	|
12028 				       I40E_FLAG_FD_ATR_ENABLED	|
12029 				       I40E_FLAG_VMDQ_ENABLED);
12030 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12031 
12032 			/* rework the queue expectations without MSIX */
12033 			i40e_determine_queue_usage(pf);
12034 		}
12035 	}
12036 
12037 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12038 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12039 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12040 		vectors = pci_enable_msi(pf->pdev);
12041 		if (vectors < 0) {
12042 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12043 				 vectors);
12044 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12045 		}
12046 		vectors = 1;  /* one MSI or Legacy vector */
12047 	}
12048 
12049 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12050 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12051 
12052 	/* set up vector assignment tracking */
12053 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12054 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12055 	if (!pf->irq_pile)
12056 		return -ENOMEM;
12057 
12058 	pf->irq_pile->num_entries = vectors;
12059 
12060 	/* track first vector for misc interrupts, ignore return */
12061 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12062 
12063 	return 0;
12064 }
12065 
12066 /**
12067  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12068  * @pf: private board data structure
12069  *
12070  * Restore the interrupt scheme that was cleared when we suspended the
12071  * device. This should be called during resume to re-allocate the q_vectors
12072  * and reacquire IRQs.
12073  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)12074 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12075 {
12076 	int err, i;
12077 
12078 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12079 	 * scheme. We need to re-enabled them here in order to attempt to
12080 	 * re-acquire the MSI or MSI-X vectors
12081 	 */
12082 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12083 
12084 	err = i40e_init_interrupt_scheme(pf);
12085 	if (err)
12086 		return err;
12087 
12088 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12089 	 * rings together again.
12090 	 */
12091 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12092 		if (pf->vsi[i]) {
12093 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12094 			if (err)
12095 				goto err_unwind;
12096 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12097 		}
12098 	}
12099 
12100 	err = i40e_setup_misc_vector(pf);
12101 	if (err)
12102 		goto err_unwind;
12103 
12104 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12105 		i40e_client_update_msix_info(pf);
12106 
12107 	return 0;
12108 
12109 err_unwind:
12110 	while (i--) {
12111 		if (pf->vsi[i])
12112 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12113 	}
12114 
12115 	return err;
12116 }
12117 
12118 /**
12119  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12120  * non queue events in recovery mode
12121  * @pf: board private structure
12122  *
12123  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12124  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12125  * This is handled differently than in recovery mode since no Tx/Rx resources
12126  * are being allocated.
12127  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)12128 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12129 {
12130 	int err;
12131 
12132 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12133 		err = i40e_setup_misc_vector(pf);
12134 
12135 		if (err) {
12136 			dev_info(&pf->pdev->dev,
12137 				 "MSI-X misc vector request failed, error %d\n",
12138 				 err);
12139 			return err;
12140 		}
12141 	} else {
12142 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12143 
12144 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12145 				  pf->int_name, pf);
12146 
12147 		if (err) {
12148 			dev_info(&pf->pdev->dev,
12149 				 "MSI/legacy misc vector request failed, error %d\n",
12150 				 err);
12151 			return err;
12152 		}
12153 		i40e_enable_misc_int_causes(pf);
12154 		i40e_irq_dynamic_enable_icr0(pf);
12155 	}
12156 
12157 	return 0;
12158 }
12159 
12160 /**
12161  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12162  * @pf: board private structure
12163  *
12164  * This sets up the handler for MSIX 0, which is used to manage the
12165  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12166  * when in MSI or Legacy interrupt mode.
12167  **/
i40e_setup_misc_vector(struct i40e_pf * pf)12168 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12169 {
12170 	struct i40e_hw *hw = &pf->hw;
12171 	int err = 0;
12172 
12173 	/* Only request the IRQ once, the first time through. */
12174 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12175 		err = request_irq(pf->msix_entries[0].vector,
12176 				  i40e_intr, 0, pf->int_name, pf);
12177 		if (err) {
12178 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12179 			dev_info(&pf->pdev->dev,
12180 				 "request_irq for %s failed: %d\n",
12181 				 pf->int_name, err);
12182 			return -EFAULT;
12183 		}
12184 	}
12185 
12186 	i40e_enable_misc_int_causes(pf);
12187 
12188 	/* associate no queues to the misc vector */
12189 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12190 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12191 
12192 	i40e_flush(hw);
12193 
12194 	i40e_irq_dynamic_enable_icr0(pf);
12195 
12196 	return err;
12197 }
12198 
12199 /**
12200  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12201  * @vsi: Pointer to vsi structure
12202  * @seed: Buffter to store the hash keys
12203  * @lut: Buffer to store the lookup table entries
12204  * @lut_size: Size of buffer to store the lookup table entries
12205  *
12206  * Return 0 on success, negative on failure
12207  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)12208 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12209 			   u8 *lut, u16 lut_size)
12210 {
12211 	struct i40e_pf *pf = vsi->back;
12212 	struct i40e_hw *hw = &pf->hw;
12213 	int ret = 0;
12214 
12215 	if (seed) {
12216 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12217 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12218 		if (ret) {
12219 			dev_info(&pf->pdev->dev,
12220 				 "Cannot get RSS key, err %s aq_err %s\n",
12221 				 i40e_stat_str(&pf->hw, ret),
12222 				 i40e_aq_str(&pf->hw,
12223 					     pf->hw.aq.asq_last_status));
12224 			return ret;
12225 		}
12226 	}
12227 
12228 	if (lut) {
12229 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12230 
12231 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12232 		if (ret) {
12233 			dev_info(&pf->pdev->dev,
12234 				 "Cannot get RSS lut, err %s aq_err %s\n",
12235 				 i40e_stat_str(&pf->hw, ret),
12236 				 i40e_aq_str(&pf->hw,
12237 					     pf->hw.aq.asq_last_status));
12238 			return ret;
12239 		}
12240 	}
12241 
12242 	return ret;
12243 }
12244 
12245 /**
12246  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12247  * @vsi: Pointer to vsi structure
12248  * @seed: RSS hash seed
12249  * @lut: Lookup table
12250  * @lut_size: Lookup table size
12251  *
12252  * Returns 0 on success, negative on failure
12253  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)12254 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12255 			       const u8 *lut, u16 lut_size)
12256 {
12257 	struct i40e_pf *pf = vsi->back;
12258 	struct i40e_hw *hw = &pf->hw;
12259 	u16 vf_id = vsi->vf_id;
12260 	u8 i;
12261 
12262 	/* Fill out hash function seed */
12263 	if (seed) {
12264 		u32 *seed_dw = (u32 *)seed;
12265 
12266 		if (vsi->type == I40E_VSI_MAIN) {
12267 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12268 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12269 		} else if (vsi->type == I40E_VSI_SRIOV) {
12270 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12271 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12272 		} else {
12273 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12274 		}
12275 	}
12276 
12277 	if (lut) {
12278 		u32 *lut_dw = (u32 *)lut;
12279 
12280 		if (vsi->type == I40E_VSI_MAIN) {
12281 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12282 				return -EINVAL;
12283 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12284 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12285 		} else if (vsi->type == I40E_VSI_SRIOV) {
12286 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12287 				return -EINVAL;
12288 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12289 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12290 		} else {
12291 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12292 		}
12293 	}
12294 	i40e_flush(hw);
12295 
12296 	return 0;
12297 }
12298 
12299 /**
12300  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12301  * @vsi: Pointer to VSI structure
12302  * @seed: Buffer to store the keys
12303  * @lut: Buffer to store the lookup table entries
12304  * @lut_size: Size of buffer to store the lookup table entries
12305  *
12306  * Returns 0 on success, negative on failure
12307  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12308 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12309 			    u8 *lut, u16 lut_size)
12310 {
12311 	struct i40e_pf *pf = vsi->back;
12312 	struct i40e_hw *hw = &pf->hw;
12313 	u16 i;
12314 
12315 	if (seed) {
12316 		u32 *seed_dw = (u32 *)seed;
12317 
12318 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12319 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12320 	}
12321 	if (lut) {
12322 		u32 *lut_dw = (u32 *)lut;
12323 
12324 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12325 			return -EINVAL;
12326 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12327 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12328 	}
12329 
12330 	return 0;
12331 }
12332 
12333 /**
12334  * i40e_config_rss - Configure RSS keys and lut
12335  * @vsi: Pointer to VSI structure
12336  * @seed: RSS hash seed
12337  * @lut: Lookup table
12338  * @lut_size: Lookup table size
12339  *
12340  * Returns 0 on success, negative on failure
12341  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12342 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12343 {
12344 	struct i40e_pf *pf = vsi->back;
12345 
12346 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12347 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12348 	else
12349 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12350 }
12351 
12352 /**
12353  * i40e_get_rss - Get RSS keys and lut
12354  * @vsi: Pointer to VSI structure
12355  * @seed: Buffer to store the keys
12356  * @lut: Buffer to store the lookup table entries
12357  * @lut_size: Size of buffer to store the lookup table entries
12358  *
12359  * Returns 0 on success, negative on failure
12360  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12361 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12362 {
12363 	struct i40e_pf *pf = vsi->back;
12364 
12365 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12366 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12367 	else
12368 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12369 }
12370 
12371 /**
12372  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12373  * @pf: Pointer to board private structure
12374  * @lut: Lookup table
12375  * @rss_table_size: Lookup table size
12376  * @rss_size: Range of queue number for hashing
12377  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)12378 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12379 		       u16 rss_table_size, u16 rss_size)
12380 {
12381 	u16 i;
12382 
12383 	for (i = 0; i < rss_table_size; i++)
12384 		lut[i] = i % rss_size;
12385 }
12386 
12387 /**
12388  * i40e_pf_config_rss - Prepare for RSS if used
12389  * @pf: board private structure
12390  **/
i40e_pf_config_rss(struct i40e_pf * pf)12391 static int i40e_pf_config_rss(struct i40e_pf *pf)
12392 {
12393 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12394 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12395 	u8 *lut;
12396 	struct i40e_hw *hw = &pf->hw;
12397 	u32 reg_val;
12398 	u64 hena;
12399 	int ret;
12400 
12401 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12402 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12403 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12404 	hena |= i40e_pf_get_default_rss_hena(pf);
12405 
12406 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12407 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12408 
12409 	/* Determine the RSS table size based on the hardware capabilities */
12410 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12411 	reg_val = (pf->rss_table_size == 512) ?
12412 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12413 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12414 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12415 
12416 	/* Determine the RSS size of the VSI */
12417 	if (!vsi->rss_size) {
12418 		u16 qcount;
12419 		/* If the firmware does something weird during VSI init, we
12420 		 * could end up with zero TCs. Check for that to avoid
12421 		 * divide-by-zero. It probably won't pass traffic, but it also
12422 		 * won't panic.
12423 		 */
12424 		qcount = vsi->num_queue_pairs /
12425 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12426 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12427 	}
12428 	if (!vsi->rss_size)
12429 		return -EINVAL;
12430 
12431 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12432 	if (!lut)
12433 		return -ENOMEM;
12434 
12435 	/* Use user configured lut if there is one, otherwise use default */
12436 	if (vsi->rss_lut_user)
12437 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12438 	else
12439 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12440 
12441 	/* Use user configured hash key if there is one, otherwise
12442 	 * use default.
12443 	 */
12444 	if (vsi->rss_hkey_user)
12445 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12446 	else
12447 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12448 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12449 	kfree(lut);
12450 
12451 	return ret;
12452 }
12453 
12454 /**
12455  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12456  * @pf: board private structure
12457  * @queue_count: the requested queue count for rss.
12458  *
12459  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12460  * count which may be different from the requested queue count.
12461  * Note: expects to be called while under rtnl_lock()
12462  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)12463 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12464 {
12465 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12466 	int new_rss_size;
12467 
12468 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12469 		return 0;
12470 
12471 	queue_count = min_t(int, queue_count, num_online_cpus());
12472 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12473 
12474 	if (queue_count != vsi->num_queue_pairs) {
12475 		u16 qcount;
12476 
12477 		vsi->req_queue_pairs = queue_count;
12478 		i40e_prep_for_reset(pf);
12479 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12480 			return pf->alloc_rss_size;
12481 
12482 		pf->alloc_rss_size = new_rss_size;
12483 
12484 		i40e_reset_and_rebuild(pf, true, true);
12485 
12486 		/* Discard the user configured hash keys and lut, if less
12487 		 * queues are enabled.
12488 		 */
12489 		if (queue_count < vsi->rss_size) {
12490 			i40e_clear_rss_config_user(vsi);
12491 			dev_dbg(&pf->pdev->dev,
12492 				"discard user configured hash keys and lut\n");
12493 		}
12494 
12495 		/* Reset vsi->rss_size, as number of enabled queues changed */
12496 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12497 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12498 
12499 		i40e_pf_config_rss(pf);
12500 	}
12501 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12502 		 vsi->req_queue_pairs, pf->rss_size_max);
12503 	return pf->alloc_rss_size;
12504 }
12505 
12506 /**
12507  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12508  * @pf: board private structure
12509  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)12510 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12511 {
12512 	i40e_status status;
12513 	bool min_valid, max_valid;
12514 	u32 max_bw, min_bw;
12515 
12516 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12517 					   &min_valid, &max_valid);
12518 
12519 	if (!status) {
12520 		if (min_valid)
12521 			pf->min_bw = min_bw;
12522 		if (max_valid)
12523 			pf->max_bw = max_bw;
12524 	}
12525 
12526 	return status;
12527 }
12528 
12529 /**
12530  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12531  * @pf: board private structure
12532  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)12533 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12534 {
12535 	struct i40e_aqc_configure_partition_bw_data bw_data;
12536 	i40e_status status;
12537 
12538 	memset(&bw_data, 0, sizeof(bw_data));
12539 
12540 	/* Set the valid bit for this PF */
12541 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12542 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12543 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12544 
12545 	/* Set the new bandwidths */
12546 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12547 
12548 	return status;
12549 }
12550 
12551 /**
12552  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12553  * @pf: board private structure
12554  **/
i40e_commit_partition_bw_setting(struct i40e_pf * pf)12555 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12556 {
12557 	/* Commit temporary BW setting to permanent NVM image */
12558 	enum i40e_admin_queue_err last_aq_status;
12559 	i40e_status ret;
12560 	u16 nvm_word;
12561 
12562 	if (pf->hw.partition_id != 1) {
12563 		dev_info(&pf->pdev->dev,
12564 			 "Commit BW only works on partition 1! This is partition %d",
12565 			 pf->hw.partition_id);
12566 		ret = I40E_NOT_SUPPORTED;
12567 		goto bw_commit_out;
12568 	}
12569 
12570 	/* Acquire NVM for read access */
12571 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12572 	last_aq_status = pf->hw.aq.asq_last_status;
12573 	if (ret) {
12574 		dev_info(&pf->pdev->dev,
12575 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12576 			 i40e_stat_str(&pf->hw, ret),
12577 			 i40e_aq_str(&pf->hw, last_aq_status));
12578 		goto bw_commit_out;
12579 	}
12580 
12581 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12582 	ret = i40e_aq_read_nvm(&pf->hw,
12583 			       I40E_SR_NVM_CONTROL_WORD,
12584 			       0x10, sizeof(nvm_word), &nvm_word,
12585 			       false, NULL);
12586 	/* Save off last admin queue command status before releasing
12587 	 * the NVM
12588 	 */
12589 	last_aq_status = pf->hw.aq.asq_last_status;
12590 	i40e_release_nvm(&pf->hw);
12591 	if (ret) {
12592 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12593 			 i40e_stat_str(&pf->hw, ret),
12594 			 i40e_aq_str(&pf->hw, last_aq_status));
12595 		goto bw_commit_out;
12596 	}
12597 
12598 	/* Wait a bit for NVM release to complete */
12599 	msleep(50);
12600 
12601 	/* Acquire NVM for write access */
12602 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12603 	last_aq_status = pf->hw.aq.asq_last_status;
12604 	if (ret) {
12605 		dev_info(&pf->pdev->dev,
12606 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12607 			 i40e_stat_str(&pf->hw, ret),
12608 			 i40e_aq_str(&pf->hw, last_aq_status));
12609 		goto bw_commit_out;
12610 	}
12611 	/* Write it back out unchanged to initiate update NVM,
12612 	 * which will force a write of the shadow (alt) RAM to
12613 	 * the NVM - thus storing the bandwidth values permanently.
12614 	 */
12615 	ret = i40e_aq_update_nvm(&pf->hw,
12616 				 I40E_SR_NVM_CONTROL_WORD,
12617 				 0x10, sizeof(nvm_word),
12618 				 &nvm_word, true, 0, NULL);
12619 	/* Save off last admin queue command status before releasing
12620 	 * the NVM
12621 	 */
12622 	last_aq_status = pf->hw.aq.asq_last_status;
12623 	i40e_release_nvm(&pf->hw);
12624 	if (ret)
12625 		dev_info(&pf->pdev->dev,
12626 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12627 			 i40e_stat_str(&pf->hw, ret),
12628 			 i40e_aq_str(&pf->hw, last_aq_status));
12629 bw_commit_out:
12630 
12631 	return ret;
12632 }
12633 
12634 /**
12635  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12636  * if total port shutdown feature is enabled for this PF
12637  * @pf: board private structure
12638  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)12639 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12640 {
12641 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12642 #define I40E_FEATURES_ENABLE_PTR		0x2A
12643 #define I40E_CURRENT_SETTING_PTR		0x2B
12644 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12645 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12646 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12647 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12648 	i40e_status read_status = I40E_SUCCESS;
12649 	u16 sr_emp_sr_settings_ptr = 0;
12650 	u16 features_enable = 0;
12651 	u16 link_behavior = 0;
12652 	bool ret = false;
12653 
12654 	read_status = i40e_read_nvm_word(&pf->hw,
12655 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12656 					 &sr_emp_sr_settings_ptr);
12657 	if (read_status)
12658 		goto err_nvm;
12659 	read_status = i40e_read_nvm_word(&pf->hw,
12660 					 sr_emp_sr_settings_ptr +
12661 					 I40E_FEATURES_ENABLE_PTR,
12662 					 &features_enable);
12663 	if (read_status)
12664 		goto err_nvm;
12665 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12666 		read_status = i40e_read_nvm_module_data(&pf->hw,
12667 							I40E_SR_EMP_SR_SETTINGS_PTR,
12668 							I40E_CURRENT_SETTING_PTR,
12669 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12670 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12671 							&link_behavior);
12672 		if (read_status)
12673 			goto err_nvm;
12674 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12675 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12676 	}
12677 	return ret;
12678 
12679 err_nvm:
12680 	dev_warn(&pf->pdev->dev,
12681 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12682 		 i40e_stat_str(&pf->hw, read_status));
12683 	return ret;
12684 }
12685 
12686 /**
12687  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12688  * @pf: board private structure to initialize
12689  *
12690  * i40e_sw_init initializes the Adapter private data structure.
12691  * Fields are initialized based on PCI device information and
12692  * OS network device settings (MTU size).
12693  **/
i40e_sw_init(struct i40e_pf * pf)12694 static int i40e_sw_init(struct i40e_pf *pf)
12695 {
12696 	int err = 0;
12697 	int size;
12698 	u16 pow;
12699 
12700 	/* Set default capability flags */
12701 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12702 		    I40E_FLAG_MSI_ENABLED     |
12703 		    I40E_FLAG_MSIX_ENABLED;
12704 
12705 	/* Set default ITR */
12706 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12707 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12708 
12709 	/* Depending on PF configurations, it is possible that the RSS
12710 	 * maximum might end up larger than the available queues
12711 	 */
12712 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12713 	pf->alloc_rss_size = 1;
12714 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12715 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12716 				 pf->hw.func_caps.num_tx_qp);
12717 
12718 	/* find the next higher power-of-2 of num cpus */
12719 	pow = roundup_pow_of_two(num_online_cpus());
12720 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12721 
12722 	if (pf->hw.func_caps.rss) {
12723 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12724 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12725 					   num_online_cpus());
12726 	}
12727 
12728 	/* MFP mode enabled */
12729 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12730 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12731 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12732 		if (i40e_get_partition_bw_setting(pf)) {
12733 			dev_warn(&pf->pdev->dev,
12734 				 "Could not get partition bw settings\n");
12735 		} else {
12736 			dev_info(&pf->pdev->dev,
12737 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12738 				 pf->min_bw, pf->max_bw);
12739 
12740 			/* nudge the Tx scheduler */
12741 			i40e_set_partition_bw_setting(pf);
12742 		}
12743 	}
12744 
12745 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12746 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12747 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12748 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12749 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12750 		    pf->hw.num_partitions > 1)
12751 			dev_info(&pf->pdev->dev,
12752 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12753 		else
12754 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12755 		pf->fdir_pf_filter_count =
12756 				 pf->hw.func_caps.fd_filters_guaranteed;
12757 		pf->hw.fdir_shared_filter_count =
12758 				 pf->hw.func_caps.fd_filters_best_effort;
12759 	}
12760 
12761 	if (pf->hw.mac.type == I40E_MAC_X722) {
12762 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12763 				    I40E_HW_128_QP_RSS_CAPABLE |
12764 				    I40E_HW_ATR_EVICT_CAPABLE |
12765 				    I40E_HW_WB_ON_ITR_CAPABLE |
12766 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12767 				    I40E_HW_NO_PCI_LINK_CHECK |
12768 				    I40E_HW_USE_SET_LLDP_MIB |
12769 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12770 				    I40E_HW_PTP_L4_CAPABLE |
12771 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12772 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12773 
12774 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12775 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12776 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12777 			dev_warn(&pf->pdev->dev,
12778 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12779 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12780 		}
12781 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12782 		   ((pf->hw.aq.api_maj_ver == 1) &&
12783 		    (pf->hw.aq.api_min_ver > 4))) {
12784 		/* Supported in FW API version higher than 1.4 */
12785 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12786 	}
12787 
12788 	/* Enable HW ATR eviction if possible */
12789 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12790 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12791 
12792 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12793 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12794 	    (pf->hw.aq.fw_maj_ver < 4))) {
12795 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12796 		/* No DCB support  for FW < v4.33 */
12797 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12798 	}
12799 
12800 	/* Disable FW LLDP if FW < v4.3 */
12801 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12802 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12803 	    (pf->hw.aq.fw_maj_ver < 4)))
12804 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12805 
12806 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12807 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12808 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12809 	    (pf->hw.aq.fw_maj_ver >= 5)))
12810 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12811 
12812 	/* Enable PTP L4 if FW > v6.0 */
12813 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12814 	    pf->hw.aq.fw_maj_ver >= 6)
12815 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12816 
12817 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12818 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12819 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12820 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12821 	}
12822 
12823 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12824 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12825 		/* IWARP needs one extra vector for CQP just like MISC.*/
12826 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12827 	}
12828 	/* Stopping FW LLDP engine is supported on XL710 and X722
12829 	 * starting from FW versions determined in i40e_init_adminq.
12830 	 * Stopping the FW LLDP engine is not supported on XL710
12831 	 * if NPAR is functioning so unset this hw flag in this case.
12832 	 */
12833 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12834 	    pf->hw.func_caps.npar_enable &&
12835 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12836 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12837 
12838 #ifdef CONFIG_PCI_IOV
12839 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12840 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12841 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12842 		pf->num_req_vfs = min_t(int,
12843 					pf->hw.func_caps.num_vfs,
12844 					I40E_MAX_VF_COUNT);
12845 	}
12846 #endif /* CONFIG_PCI_IOV */
12847 	pf->eeprom_version = 0xDEAD;
12848 	pf->lan_veb = I40E_NO_VEB;
12849 	pf->lan_vsi = I40E_NO_VSI;
12850 
12851 	/* By default FW has this off for performance reasons */
12852 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12853 
12854 	/* set up queue assignment tracking */
12855 	size = sizeof(struct i40e_lump_tracking)
12856 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12857 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12858 	if (!pf->qp_pile) {
12859 		err = -ENOMEM;
12860 		goto sw_init_done;
12861 	}
12862 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12863 
12864 	pf->tx_timeout_recovery_level = 1;
12865 
12866 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12867 	    i40e_is_total_port_shutdown_enabled(pf)) {
12868 		/* Link down on close must be on when total port shutdown
12869 		 * is enabled for a given port
12870 		 */
12871 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12872 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12873 		dev_info(&pf->pdev->dev,
12874 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12875 	}
12876 	mutex_init(&pf->switch_mutex);
12877 
12878 sw_init_done:
12879 	return err;
12880 }
12881 
12882 /**
12883  * i40e_set_ntuple - set the ntuple feature flag and take action
12884  * @pf: board private structure to initialize
12885  * @features: the feature set that the stack is suggesting
12886  *
12887  * returns a bool to indicate if reset needs to happen
12888  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12889 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12890 {
12891 	bool need_reset = false;
12892 
12893 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12894 	 * the state changed, we need to reset.
12895 	 */
12896 	if (features & NETIF_F_NTUPLE) {
12897 		/* Enable filters and mark for reset */
12898 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12899 			need_reset = true;
12900 		/* enable FD_SB only if there is MSI-X vector and no cloud
12901 		 * filters exist
12902 		 */
12903 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12904 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12905 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12906 		}
12907 	} else {
12908 		/* turn off filters, mark for reset and clear SW filter list */
12909 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12910 			need_reset = true;
12911 			i40e_fdir_filter_exit(pf);
12912 		}
12913 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12914 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12915 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12916 
12917 		/* reset fd counters */
12918 		pf->fd_add_err = 0;
12919 		pf->fd_atr_cnt = 0;
12920 		/* if ATR was auto disabled it can be re-enabled. */
12921 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12922 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12923 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12924 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12925 	}
12926 	return need_reset;
12927 }
12928 
12929 /**
12930  * i40e_clear_rss_lut - clear the rx hash lookup table
12931  * @vsi: the VSI being configured
12932  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12933 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12934 {
12935 	struct i40e_pf *pf = vsi->back;
12936 	struct i40e_hw *hw = &pf->hw;
12937 	u16 vf_id = vsi->vf_id;
12938 	u8 i;
12939 
12940 	if (vsi->type == I40E_VSI_MAIN) {
12941 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12942 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12943 	} else if (vsi->type == I40E_VSI_SRIOV) {
12944 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12945 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12946 	} else {
12947 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12948 	}
12949 }
12950 
12951 /**
12952  * i40e_set_features - set the netdev feature flags
12953  * @netdev: ptr to the netdev being adjusted
12954  * @features: the feature set that the stack is suggesting
12955  * Note: expects to be called while under rtnl_lock()
12956  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)12957 static int i40e_set_features(struct net_device *netdev,
12958 			     netdev_features_t features)
12959 {
12960 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12961 	struct i40e_vsi *vsi = np->vsi;
12962 	struct i40e_pf *pf = vsi->back;
12963 	bool need_reset;
12964 
12965 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12966 		i40e_pf_config_rss(pf);
12967 	else if (!(features & NETIF_F_RXHASH) &&
12968 		 netdev->features & NETIF_F_RXHASH)
12969 		i40e_clear_rss_lut(vsi);
12970 
12971 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12972 		i40e_vlan_stripping_enable(vsi);
12973 	else
12974 		i40e_vlan_stripping_disable(vsi);
12975 
12976 	if (!(features & NETIF_F_HW_TC) &&
12977 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12978 		dev_err(&pf->pdev->dev,
12979 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12980 		return -EINVAL;
12981 	}
12982 
12983 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12984 		i40e_del_all_macvlans(vsi);
12985 
12986 	need_reset = i40e_set_ntuple(pf, features);
12987 
12988 	if (need_reset)
12989 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12990 
12991 	return 0;
12992 }
12993 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12994 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12995 				    unsigned int table, unsigned int idx,
12996 				    struct udp_tunnel_info *ti)
12997 {
12998 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12999 	struct i40e_hw *hw = &np->vsi->back->hw;
13000 	u8 type, filter_index;
13001 	i40e_status ret;
13002 
13003 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13004 						   I40E_AQC_TUNNEL_TYPE_NGE;
13005 
13006 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13007 				     NULL);
13008 	if (ret) {
13009 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
13010 			    i40e_stat_str(hw, ret),
13011 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13012 		return -EIO;
13013 	}
13014 
13015 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13016 	return 0;
13017 }
13018 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)13019 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13020 				      unsigned int table, unsigned int idx,
13021 				      struct udp_tunnel_info *ti)
13022 {
13023 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13024 	struct i40e_hw *hw = &np->vsi->back->hw;
13025 	i40e_status ret;
13026 
13027 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13028 	if (ret) {
13029 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
13030 			    i40e_stat_str(hw, ret),
13031 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13032 		return -EIO;
13033 	}
13034 
13035 	return 0;
13036 }
13037 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)13038 static int i40e_get_phys_port_id(struct net_device *netdev,
13039 				 struct netdev_phys_item_id *ppid)
13040 {
13041 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13042 	struct i40e_pf *pf = np->vsi->back;
13043 	struct i40e_hw *hw = &pf->hw;
13044 
13045 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13046 		return -EOPNOTSUPP;
13047 
13048 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13049 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13050 
13051 	return 0;
13052 }
13053 
13054 /**
13055  * i40e_ndo_fdb_add - add an entry to the hardware database
13056  * @ndm: the input from the stack
13057  * @tb: pointer to array of nladdr (unused)
13058  * @dev: the net device pointer
13059  * @addr: the MAC address entry being added
13060  * @vid: VLAN ID
13061  * @flags: instructions from stack about fdb operation
13062  * @extack: netlink extended ack, unused currently
13063  */
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)13064 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13065 			    struct net_device *dev,
13066 			    const unsigned char *addr, u16 vid,
13067 			    u16 flags,
13068 			    struct netlink_ext_ack *extack)
13069 {
13070 	struct i40e_netdev_priv *np = netdev_priv(dev);
13071 	struct i40e_pf *pf = np->vsi->back;
13072 	int err = 0;
13073 
13074 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13075 		return -EOPNOTSUPP;
13076 
13077 	if (vid) {
13078 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13079 		return -EINVAL;
13080 	}
13081 
13082 	/* Hardware does not support aging addresses so if a
13083 	 * ndm_state is given only allow permanent addresses
13084 	 */
13085 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13086 		netdev_info(dev, "FDB only supports static addresses\n");
13087 		return -EINVAL;
13088 	}
13089 
13090 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13091 		err = dev_uc_add_excl(dev, addr);
13092 	else if (is_multicast_ether_addr(addr))
13093 		err = dev_mc_add_excl(dev, addr);
13094 	else
13095 		err = -EINVAL;
13096 
13097 	/* Only return duplicate errors if NLM_F_EXCL is set */
13098 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13099 		err = 0;
13100 
13101 	return err;
13102 }
13103 
13104 /**
13105  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13106  * @dev: the netdev being configured
13107  * @nlh: RTNL message
13108  * @flags: bridge flags
13109  * @extack: netlink extended ack
13110  *
13111  * Inserts a new hardware bridge if not already created and
13112  * enables the bridging mode requested (VEB or VEPA). If the
13113  * hardware bridge has already been inserted and the request
13114  * is to change the mode then that requires a PF reset to
13115  * allow rebuild of the components with required hardware
13116  * bridge mode enabled.
13117  *
13118  * Note: expects to be called while under rtnl_lock()
13119  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)13120 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13121 				   struct nlmsghdr *nlh,
13122 				   u16 flags,
13123 				   struct netlink_ext_ack *extack)
13124 {
13125 	struct i40e_netdev_priv *np = netdev_priv(dev);
13126 	struct i40e_vsi *vsi = np->vsi;
13127 	struct i40e_pf *pf = vsi->back;
13128 	struct i40e_veb *veb = NULL;
13129 	struct nlattr *attr, *br_spec;
13130 	int i, rem;
13131 
13132 	/* Only for PF VSI for now */
13133 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13134 		return -EOPNOTSUPP;
13135 
13136 	/* Find the HW bridge for PF VSI */
13137 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13138 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13139 			veb = pf->veb[i];
13140 	}
13141 
13142 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13143 
13144 	nla_for_each_nested(attr, br_spec, rem) {
13145 		__u16 mode;
13146 
13147 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13148 			continue;
13149 
13150 		mode = nla_get_u16(attr);
13151 		if ((mode != BRIDGE_MODE_VEPA) &&
13152 		    (mode != BRIDGE_MODE_VEB))
13153 			return -EINVAL;
13154 
13155 		/* Insert a new HW bridge */
13156 		if (!veb) {
13157 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13158 					     vsi->tc_config.enabled_tc);
13159 			if (veb) {
13160 				veb->bridge_mode = mode;
13161 				i40e_config_bridge_mode(veb);
13162 			} else {
13163 				/* No Bridge HW offload available */
13164 				return -ENOENT;
13165 			}
13166 			break;
13167 		} else if (mode != veb->bridge_mode) {
13168 			/* Existing HW bridge but different mode needs reset */
13169 			veb->bridge_mode = mode;
13170 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13171 			if (mode == BRIDGE_MODE_VEB)
13172 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13173 			else
13174 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13175 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13176 			break;
13177 		}
13178 	}
13179 
13180 	return 0;
13181 }
13182 
13183 /**
13184  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13185  * @skb: skb buff
13186  * @pid: process id
13187  * @seq: RTNL message seq #
13188  * @dev: the netdev being configured
13189  * @filter_mask: unused
13190  * @nlflags: netlink flags passed in
13191  *
13192  * Return the mode in which the hardware bridge is operating in
13193  * i.e VEB or VEPA.
13194  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)13195 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13196 				   struct net_device *dev,
13197 				   u32 __always_unused filter_mask,
13198 				   int nlflags)
13199 {
13200 	struct i40e_netdev_priv *np = netdev_priv(dev);
13201 	struct i40e_vsi *vsi = np->vsi;
13202 	struct i40e_pf *pf = vsi->back;
13203 	struct i40e_veb *veb = NULL;
13204 	int i;
13205 
13206 	/* Only for PF VSI for now */
13207 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13208 		return -EOPNOTSUPP;
13209 
13210 	/* Find the HW bridge for the PF VSI */
13211 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13212 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13213 			veb = pf->veb[i];
13214 	}
13215 
13216 	if (!veb)
13217 		return 0;
13218 
13219 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13220 				       0, 0, nlflags, filter_mask, NULL);
13221 }
13222 
13223 /**
13224  * i40e_features_check - Validate encapsulated packet conforms to limits
13225  * @skb: skb buff
13226  * @dev: This physical port's netdev
13227  * @features: Offload features that the stack believes apply
13228  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13229 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13230 					     struct net_device *dev,
13231 					     netdev_features_t features)
13232 {
13233 	size_t len;
13234 
13235 	/* No point in doing any of this if neither checksum nor GSO are
13236 	 * being requested for this frame.  We can rule out both by just
13237 	 * checking for CHECKSUM_PARTIAL
13238 	 */
13239 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13240 		return features;
13241 
13242 	/* We cannot support GSO if the MSS is going to be less than
13243 	 * 64 bytes.  If it is then we need to drop support for GSO.
13244 	 */
13245 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13246 		features &= ~NETIF_F_GSO_MASK;
13247 
13248 	/* MACLEN can support at most 63 words */
13249 	len = skb_network_header(skb) - skb->data;
13250 	if (len & ~(63 * 2))
13251 		goto out_err;
13252 
13253 	/* IPLEN and EIPLEN can support at most 127 dwords */
13254 	len = skb_transport_header(skb) - skb_network_header(skb);
13255 	if (len & ~(127 * 4))
13256 		goto out_err;
13257 
13258 	if (skb->encapsulation) {
13259 		/* L4TUNLEN can support 127 words */
13260 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13261 		if (len & ~(127 * 2))
13262 			goto out_err;
13263 
13264 		/* IPLEN can support at most 127 dwords */
13265 		len = skb_inner_transport_header(skb) -
13266 		      skb_inner_network_header(skb);
13267 		if (len & ~(127 * 4))
13268 			goto out_err;
13269 	}
13270 
13271 	/* No need to validate L4LEN as TCP is the only protocol with a
13272 	 * flexible value and we support all possible values supported
13273 	 * by TCP, which is at most 15 dwords
13274 	 */
13275 
13276 	return features;
13277 out_err:
13278 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13279 }
13280 
13281 /**
13282  * i40e_xdp_setup - add/remove an XDP program
13283  * @vsi: VSI to changed
13284  * @prog: XDP program
13285  * @extack: netlink extended ack
13286  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)13287 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13288 			  struct netlink_ext_ack *extack)
13289 {
13290 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13291 	struct i40e_pf *pf = vsi->back;
13292 	struct bpf_prog *old_prog;
13293 	bool need_reset;
13294 	int i;
13295 
13296 	/* Don't allow frames that span over multiple buffers */
13297 	if (frame_size > i40e_calculate_vsi_rx_buf_len(vsi)) {
13298 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13299 		return -EINVAL;
13300 	}
13301 
13302 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13303 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13304 
13305 	if (need_reset)
13306 		i40e_prep_for_reset(pf);
13307 
13308 	/* VSI shall be deleted in a moment, just return EINVAL */
13309 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13310 		return -EINVAL;
13311 
13312 	old_prog = xchg(&vsi->xdp_prog, prog);
13313 
13314 	if (need_reset) {
13315 		if (!prog)
13316 			/* Wait until ndo_xsk_wakeup completes. */
13317 			synchronize_rcu();
13318 		i40e_reset_and_rebuild(pf, true, true);
13319 	}
13320 
13321 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13322 		if (i40e_realloc_rx_bi_zc(vsi, true))
13323 			return -ENOMEM;
13324 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13325 		if (i40e_realloc_rx_bi_zc(vsi, false))
13326 			return -ENOMEM;
13327 	}
13328 
13329 	for (i = 0; i < vsi->num_queue_pairs; i++)
13330 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13331 
13332 	if (old_prog)
13333 		bpf_prog_put(old_prog);
13334 
13335 	/* Kick start the NAPI context if there is an AF_XDP socket open
13336 	 * on that queue id. This so that receiving will start.
13337 	 */
13338 	if (need_reset && prog)
13339 		for (i = 0; i < vsi->num_queue_pairs; i++)
13340 			if (vsi->xdp_rings[i]->xsk_pool)
13341 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13342 						      XDP_WAKEUP_RX);
13343 
13344 	return 0;
13345 }
13346 
13347 /**
13348  * i40e_enter_busy_conf - Enters busy config state
13349  * @vsi: vsi
13350  *
13351  * Returns 0 on success, <0 for failure.
13352  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)13353 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13354 {
13355 	struct i40e_pf *pf = vsi->back;
13356 	int timeout = 50;
13357 
13358 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13359 		timeout--;
13360 		if (!timeout)
13361 			return -EBUSY;
13362 		usleep_range(1000, 2000);
13363 	}
13364 
13365 	return 0;
13366 }
13367 
13368 /**
13369  * i40e_exit_busy_conf - Exits busy config state
13370  * @vsi: vsi
13371  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)13372 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13373 {
13374 	struct i40e_pf *pf = vsi->back;
13375 
13376 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13377 }
13378 
13379 /**
13380  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13381  * @vsi: vsi
13382  * @queue_pair: queue pair
13383  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)13384 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13385 {
13386 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13387 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13388 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13389 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13390 	if (i40e_enabled_xdp_vsi(vsi)) {
13391 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13392 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13393 	}
13394 }
13395 
13396 /**
13397  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13398  * @vsi: vsi
13399  * @queue_pair: queue pair
13400  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)13401 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13402 {
13403 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13404 	if (i40e_enabled_xdp_vsi(vsi)) {
13405 		/* Make sure that in-progress ndo_xdp_xmit calls are
13406 		 * completed.
13407 		 */
13408 		synchronize_rcu();
13409 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13410 	}
13411 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13412 }
13413 
13414 /**
13415  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13416  * @vsi: vsi
13417  * @queue_pair: queue pair
13418  * @enable: true for enable, false for disable
13419  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)13420 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13421 					bool enable)
13422 {
13423 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13424 	struct i40e_q_vector *q_vector = rxr->q_vector;
13425 
13426 	if (!vsi->netdev)
13427 		return;
13428 
13429 	/* All rings in a qp belong to the same qvector. */
13430 	if (q_vector->rx.ring || q_vector->tx.ring) {
13431 		if (enable)
13432 			napi_enable(&q_vector->napi);
13433 		else
13434 			napi_disable(&q_vector->napi);
13435 	}
13436 }
13437 
13438 /**
13439  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13440  * @vsi: vsi
13441  * @queue_pair: queue pair
13442  * @enable: true for enable, false for disable
13443  *
13444  * Returns 0 on success, <0 on failure.
13445  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)13446 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13447 					bool enable)
13448 {
13449 	struct i40e_pf *pf = vsi->back;
13450 	int pf_q, ret = 0;
13451 
13452 	pf_q = vsi->base_queue + queue_pair;
13453 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13454 				     false /*is xdp*/, enable);
13455 	if (ret) {
13456 		dev_info(&pf->pdev->dev,
13457 			 "VSI seid %d Tx ring %d %sable timeout\n",
13458 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13459 		return ret;
13460 	}
13461 
13462 	i40e_control_rx_q(pf, pf_q, enable);
13463 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13464 	if (ret) {
13465 		dev_info(&pf->pdev->dev,
13466 			 "VSI seid %d Rx ring %d %sable timeout\n",
13467 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13468 		return ret;
13469 	}
13470 
13471 	/* Due to HW errata, on Rx disable only, the register can
13472 	 * indicate done before it really is. Needs 50ms to be sure
13473 	 */
13474 	if (!enable)
13475 		mdelay(50);
13476 
13477 	if (!i40e_enabled_xdp_vsi(vsi))
13478 		return ret;
13479 
13480 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13481 				     pf_q + vsi->alloc_queue_pairs,
13482 				     true /*is xdp*/, enable);
13483 	if (ret) {
13484 		dev_info(&pf->pdev->dev,
13485 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13486 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13487 	}
13488 
13489 	return ret;
13490 }
13491 
13492 /**
13493  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13494  * @vsi: vsi
13495  * @queue_pair: queue_pair
13496  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)13497 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13498 {
13499 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13500 	struct i40e_pf *pf = vsi->back;
13501 	struct i40e_hw *hw = &pf->hw;
13502 
13503 	/* All rings in a qp belong to the same qvector. */
13504 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13505 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13506 	else
13507 		i40e_irq_dynamic_enable_icr0(pf);
13508 
13509 	i40e_flush(hw);
13510 }
13511 
13512 /**
13513  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13514  * @vsi: vsi
13515  * @queue_pair: queue_pair
13516  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)13517 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13518 {
13519 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13520 	struct i40e_pf *pf = vsi->back;
13521 	struct i40e_hw *hw = &pf->hw;
13522 
13523 	/* For simplicity, instead of removing the qp interrupt causes
13524 	 * from the interrupt linked list, we simply disable the interrupt, and
13525 	 * leave the list intact.
13526 	 *
13527 	 * All rings in a qp belong to the same qvector.
13528 	 */
13529 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13530 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13531 
13532 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13533 		i40e_flush(hw);
13534 		synchronize_irq(pf->msix_entries[intpf].vector);
13535 	} else {
13536 		/* Legacy and MSI mode - this stops all interrupt handling */
13537 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13538 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13539 		i40e_flush(hw);
13540 		synchronize_irq(pf->pdev->irq);
13541 	}
13542 }
13543 
13544 /**
13545  * i40e_queue_pair_disable - Disables a queue pair
13546  * @vsi: vsi
13547  * @queue_pair: queue pair
13548  *
13549  * Returns 0 on success, <0 on failure.
13550  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)13551 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13552 {
13553 	int err;
13554 
13555 	err = i40e_enter_busy_conf(vsi);
13556 	if (err)
13557 		return err;
13558 
13559 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13560 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13561 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13562 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13563 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13564 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13565 
13566 	return err;
13567 }
13568 
13569 /**
13570  * i40e_queue_pair_enable - Enables a queue pair
13571  * @vsi: vsi
13572  * @queue_pair: queue pair
13573  *
13574  * Returns 0 on success, <0 on failure.
13575  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)13576 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13577 {
13578 	int err;
13579 
13580 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13581 	if (err)
13582 		return err;
13583 
13584 	if (i40e_enabled_xdp_vsi(vsi)) {
13585 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13586 		if (err)
13587 			return err;
13588 	}
13589 
13590 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13591 	if (err)
13592 		return err;
13593 
13594 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13595 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13596 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13597 
13598 	i40e_exit_busy_conf(vsi);
13599 
13600 	return err;
13601 }
13602 
13603 /**
13604  * i40e_xdp - implements ndo_bpf for i40e
13605  * @dev: netdevice
13606  * @xdp: XDP command
13607  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)13608 static int i40e_xdp(struct net_device *dev,
13609 		    struct netdev_bpf *xdp)
13610 {
13611 	struct i40e_netdev_priv *np = netdev_priv(dev);
13612 	struct i40e_vsi *vsi = np->vsi;
13613 
13614 	if (vsi->type != I40E_VSI_MAIN)
13615 		return -EINVAL;
13616 
13617 	switch (xdp->command) {
13618 	case XDP_SETUP_PROG:
13619 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13620 	case XDP_SETUP_XSK_POOL:
13621 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13622 					   xdp->xsk.queue_id);
13623 	default:
13624 		return -EINVAL;
13625 	}
13626 }
13627 
13628 static const struct net_device_ops i40e_netdev_ops = {
13629 	.ndo_open		= i40e_open,
13630 	.ndo_stop		= i40e_close,
13631 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13632 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13633 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13634 	.ndo_validate_addr	= eth_validate_addr,
13635 	.ndo_set_mac_address	= i40e_set_mac,
13636 	.ndo_change_mtu		= i40e_change_mtu,
13637 	.ndo_eth_ioctl		= i40e_ioctl,
13638 	.ndo_tx_timeout		= i40e_tx_timeout,
13639 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13640 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13641 #ifdef CONFIG_NET_POLL_CONTROLLER
13642 	.ndo_poll_controller	= i40e_netpoll,
13643 #endif
13644 	.ndo_setup_tc		= __i40e_setup_tc,
13645 	.ndo_select_queue	= i40e_lan_select_queue,
13646 	.ndo_set_features	= i40e_set_features,
13647 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13648 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13649 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13650 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13651 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13652 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13653 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13654 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13655 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13656 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13657 	.ndo_features_check	= i40e_features_check,
13658 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13659 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13660 	.ndo_bpf		= i40e_xdp,
13661 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13662 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13663 	.ndo_dfwd_add_station	= i40e_fwd_add,
13664 	.ndo_dfwd_del_station	= i40e_fwd_del,
13665 };
13666 
13667 /**
13668  * i40e_config_netdev - Setup the netdev flags
13669  * @vsi: the VSI being configured
13670  *
13671  * Returns 0 on success, negative value on failure
13672  **/
i40e_config_netdev(struct i40e_vsi * vsi)13673 static int i40e_config_netdev(struct i40e_vsi *vsi)
13674 {
13675 	struct i40e_pf *pf = vsi->back;
13676 	struct i40e_hw *hw = &pf->hw;
13677 	struct i40e_netdev_priv *np;
13678 	struct net_device *netdev;
13679 	u8 broadcast[ETH_ALEN];
13680 	u8 mac_addr[ETH_ALEN];
13681 	int etherdev_size;
13682 	netdev_features_t hw_enc_features;
13683 	netdev_features_t hw_features;
13684 
13685 	etherdev_size = sizeof(struct i40e_netdev_priv);
13686 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13687 	if (!netdev)
13688 		return -ENOMEM;
13689 
13690 	vsi->netdev = netdev;
13691 	np = netdev_priv(netdev);
13692 	np->vsi = vsi;
13693 
13694 	hw_enc_features = NETIF_F_SG			|
13695 			  NETIF_F_HW_CSUM		|
13696 			  NETIF_F_HIGHDMA		|
13697 			  NETIF_F_SOFT_FEATURES		|
13698 			  NETIF_F_TSO			|
13699 			  NETIF_F_TSO_ECN		|
13700 			  NETIF_F_TSO6			|
13701 			  NETIF_F_GSO_GRE		|
13702 			  NETIF_F_GSO_GRE_CSUM		|
13703 			  NETIF_F_GSO_PARTIAL		|
13704 			  NETIF_F_GSO_IPXIP4		|
13705 			  NETIF_F_GSO_IPXIP6		|
13706 			  NETIF_F_GSO_UDP_TUNNEL	|
13707 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13708 			  NETIF_F_GSO_UDP_L4		|
13709 			  NETIF_F_SCTP_CRC		|
13710 			  NETIF_F_RXHASH		|
13711 			  NETIF_F_RXCSUM		|
13712 			  0;
13713 
13714 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13715 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13716 
13717 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13718 
13719 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13720 
13721 	netdev->hw_enc_features |= hw_enc_features;
13722 
13723 	/* record features VLANs can make use of */
13724 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13725 
13726 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13727 				   NETIF_F_GSO_GRE_CSUM |	\
13728 				   NETIF_F_GSO_IPXIP4 |		\
13729 				   NETIF_F_GSO_IPXIP6 |		\
13730 				   NETIF_F_GSO_UDP_TUNNEL |	\
13731 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13732 
13733 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13734 	netdev->features |= NETIF_F_GSO_PARTIAL |
13735 			    I40E_GSO_PARTIAL_FEATURES;
13736 
13737 	netdev->mpls_features |= NETIF_F_SG;
13738 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13739 	netdev->mpls_features |= NETIF_F_TSO;
13740 	netdev->mpls_features |= NETIF_F_TSO6;
13741 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13742 
13743 	/* enable macvlan offloads */
13744 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13745 
13746 	hw_features = hw_enc_features		|
13747 		      NETIF_F_HW_VLAN_CTAG_TX	|
13748 		      NETIF_F_HW_VLAN_CTAG_RX;
13749 
13750 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13751 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13752 
13753 	netdev->hw_features |= hw_features;
13754 
13755 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13756 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13757 
13758 	netdev->features &= ~NETIF_F_HW_TC;
13759 
13760 	if (vsi->type == I40E_VSI_MAIN) {
13761 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13762 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13763 		/* The following steps are necessary for two reasons. First,
13764 		 * some older NVM configurations load a default MAC-VLAN
13765 		 * filter that will accept any tagged packet, and we want to
13766 		 * replace this with a normal filter. Additionally, it is
13767 		 * possible our MAC address was provided by the platform using
13768 		 * Open Firmware or similar.
13769 		 *
13770 		 * Thus, we need to remove the default filter and install one
13771 		 * specific to the MAC address.
13772 		 */
13773 		i40e_rm_default_mac_filter(vsi, mac_addr);
13774 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13775 		i40e_add_mac_filter(vsi, mac_addr);
13776 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13777 	} else {
13778 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13779 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13780 		 * the end, which is 4 bytes long, so force truncation of the
13781 		 * original name by IFNAMSIZ - 4
13782 		 */
13783 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13784 			 IFNAMSIZ - 4,
13785 			 pf->vsi[pf->lan_vsi]->netdev->name);
13786 		eth_random_addr(mac_addr);
13787 
13788 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13789 		i40e_add_mac_filter(vsi, mac_addr);
13790 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13791 	}
13792 
13793 	/* Add the broadcast filter so that we initially will receive
13794 	 * broadcast packets. Note that when a new VLAN is first added the
13795 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13796 	 * specific filters as part of transitioning into "vlan" operation.
13797 	 * When more VLANs are added, the driver will copy each existing MAC
13798 	 * filter and add it for the new VLAN.
13799 	 *
13800 	 * Broadcast filters are handled specially by
13801 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13802 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13803 	 * filter. The subtask will update the correct broadcast promiscuous
13804 	 * bits as VLANs become active or inactive.
13805 	 */
13806 	eth_broadcast_addr(broadcast);
13807 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13808 	i40e_add_mac_filter(vsi, broadcast);
13809 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13810 
13811 	eth_hw_addr_set(netdev, mac_addr);
13812 	ether_addr_copy(netdev->perm_addr, mac_addr);
13813 
13814 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13815 	netdev->neigh_priv_len = sizeof(u32) * 4;
13816 
13817 	netdev->priv_flags |= IFF_UNICAST_FLT;
13818 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13819 	/* Setup netdev TC information */
13820 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13821 
13822 	netdev->netdev_ops = &i40e_netdev_ops;
13823 	netdev->watchdog_timeo = 5 * HZ;
13824 	i40e_set_ethtool_ops(netdev);
13825 
13826 	/* MTU range: 68 - 9706 */
13827 	netdev->min_mtu = ETH_MIN_MTU;
13828 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13829 
13830 	return 0;
13831 }
13832 
13833 /**
13834  * i40e_vsi_delete - Delete a VSI from the switch
13835  * @vsi: the VSI being removed
13836  *
13837  * Returns 0 on success, negative value on failure
13838  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13839 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13840 {
13841 	/* remove default VSI is not allowed */
13842 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13843 		return;
13844 
13845 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13846 }
13847 
13848 /**
13849  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13850  * @vsi: the VSI being queried
13851  *
13852  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13853  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13854 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13855 {
13856 	struct i40e_veb *veb;
13857 	struct i40e_pf *pf = vsi->back;
13858 
13859 	/* Uplink is not a bridge so default to VEB */
13860 	if (vsi->veb_idx >= I40E_MAX_VEB)
13861 		return 1;
13862 
13863 	veb = pf->veb[vsi->veb_idx];
13864 	if (!veb) {
13865 		dev_info(&pf->pdev->dev,
13866 			 "There is no veb associated with the bridge\n");
13867 		return -ENOENT;
13868 	}
13869 
13870 	/* Uplink is a bridge in VEPA mode */
13871 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13872 		return 0;
13873 	} else {
13874 		/* Uplink is a bridge in VEB mode */
13875 		return 1;
13876 	}
13877 
13878 	/* VEPA is now default bridge, so return 0 */
13879 	return 0;
13880 }
13881 
13882 /**
13883  * i40e_add_vsi - Add a VSI to the switch
13884  * @vsi: the VSI being configured
13885  *
13886  * This initializes a VSI context depending on the VSI type to be added and
13887  * passes it down to the add_vsi aq command.
13888  **/
i40e_add_vsi(struct i40e_vsi * vsi)13889 static int i40e_add_vsi(struct i40e_vsi *vsi)
13890 {
13891 	int ret = -ENODEV;
13892 	struct i40e_pf *pf = vsi->back;
13893 	struct i40e_hw *hw = &pf->hw;
13894 	struct i40e_vsi_context ctxt;
13895 	struct i40e_mac_filter *f;
13896 	struct hlist_node *h;
13897 	int bkt;
13898 
13899 	u8 enabled_tc = 0x1; /* TC0 enabled */
13900 	int f_count = 0;
13901 
13902 	memset(&ctxt, 0, sizeof(ctxt));
13903 	switch (vsi->type) {
13904 	case I40E_VSI_MAIN:
13905 		/* The PF's main VSI is already setup as part of the
13906 		 * device initialization, so we'll not bother with
13907 		 * the add_vsi call, but we will retrieve the current
13908 		 * VSI context.
13909 		 */
13910 		ctxt.seid = pf->main_vsi_seid;
13911 		ctxt.pf_num = pf->hw.pf_id;
13912 		ctxt.vf_num = 0;
13913 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13914 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13915 		if (ret) {
13916 			dev_info(&pf->pdev->dev,
13917 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13918 				 i40e_stat_str(&pf->hw, ret),
13919 				 i40e_aq_str(&pf->hw,
13920 					     pf->hw.aq.asq_last_status));
13921 			return -ENOENT;
13922 		}
13923 		vsi->info = ctxt.info;
13924 		vsi->info.valid_sections = 0;
13925 
13926 		vsi->seid = ctxt.seid;
13927 		vsi->id = ctxt.vsi_number;
13928 
13929 		enabled_tc = i40e_pf_get_tc_map(pf);
13930 
13931 		/* Source pruning is enabled by default, so the flag is
13932 		 * negative logic - if it's set, we need to fiddle with
13933 		 * the VSI to disable source pruning.
13934 		 */
13935 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13936 			memset(&ctxt, 0, sizeof(ctxt));
13937 			ctxt.seid = pf->main_vsi_seid;
13938 			ctxt.pf_num = pf->hw.pf_id;
13939 			ctxt.vf_num = 0;
13940 			ctxt.info.valid_sections |=
13941 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13942 			ctxt.info.switch_id =
13943 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13944 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13945 			if (ret) {
13946 				dev_info(&pf->pdev->dev,
13947 					 "update vsi failed, err %s aq_err %s\n",
13948 					 i40e_stat_str(&pf->hw, ret),
13949 					 i40e_aq_str(&pf->hw,
13950 						     pf->hw.aq.asq_last_status));
13951 				ret = -ENOENT;
13952 				goto err;
13953 			}
13954 		}
13955 
13956 		/* MFP mode setup queue map and update VSI */
13957 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13958 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13959 			memset(&ctxt, 0, sizeof(ctxt));
13960 			ctxt.seid = pf->main_vsi_seid;
13961 			ctxt.pf_num = pf->hw.pf_id;
13962 			ctxt.vf_num = 0;
13963 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13964 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13965 			if (ret) {
13966 				dev_info(&pf->pdev->dev,
13967 					 "update vsi failed, err %s aq_err %s\n",
13968 					 i40e_stat_str(&pf->hw, ret),
13969 					 i40e_aq_str(&pf->hw,
13970 						    pf->hw.aq.asq_last_status));
13971 				ret = -ENOENT;
13972 				goto err;
13973 			}
13974 			/* update the local VSI info queue map */
13975 			i40e_vsi_update_queue_map(vsi, &ctxt);
13976 			vsi->info.valid_sections = 0;
13977 		} else {
13978 			/* Default/Main VSI is only enabled for TC0
13979 			 * reconfigure it to enable all TCs that are
13980 			 * available on the port in SFP mode.
13981 			 * For MFP case the iSCSI PF would use this
13982 			 * flow to enable LAN+iSCSI TC.
13983 			 */
13984 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13985 			if (ret) {
13986 				/* Single TC condition is not fatal,
13987 				 * message and continue
13988 				 */
13989 				dev_info(&pf->pdev->dev,
13990 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13991 					 enabled_tc,
13992 					 i40e_stat_str(&pf->hw, ret),
13993 					 i40e_aq_str(&pf->hw,
13994 						    pf->hw.aq.asq_last_status));
13995 			}
13996 		}
13997 		break;
13998 
13999 	case I40E_VSI_FDIR:
14000 		ctxt.pf_num = hw->pf_id;
14001 		ctxt.vf_num = 0;
14002 		ctxt.uplink_seid = vsi->uplink_seid;
14003 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14004 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14005 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
14006 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14007 			ctxt.info.valid_sections |=
14008 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14009 			ctxt.info.switch_id =
14010 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14011 		}
14012 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14013 		break;
14014 
14015 	case I40E_VSI_VMDQ2:
14016 		ctxt.pf_num = hw->pf_id;
14017 		ctxt.vf_num = 0;
14018 		ctxt.uplink_seid = vsi->uplink_seid;
14019 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14020 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14021 
14022 		/* This VSI is connected to VEB so the switch_id
14023 		 * should be set to zero by default.
14024 		 */
14025 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14026 			ctxt.info.valid_sections |=
14027 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14028 			ctxt.info.switch_id =
14029 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14030 		}
14031 
14032 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14033 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14034 		break;
14035 
14036 	case I40E_VSI_SRIOV:
14037 		ctxt.pf_num = hw->pf_id;
14038 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14039 		ctxt.uplink_seid = vsi->uplink_seid;
14040 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14041 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14042 
14043 		/* This VSI is connected to VEB so the switch_id
14044 		 * should be set to zero by default.
14045 		 */
14046 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14047 			ctxt.info.valid_sections |=
14048 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14049 			ctxt.info.switch_id =
14050 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14051 		}
14052 
14053 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14054 			ctxt.info.valid_sections |=
14055 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14056 			ctxt.info.queueing_opt_flags |=
14057 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14058 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14059 		}
14060 
14061 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14062 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14063 		if (pf->vf[vsi->vf_id].spoofchk) {
14064 			ctxt.info.valid_sections |=
14065 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14066 			ctxt.info.sec_flags |=
14067 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14068 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14069 		}
14070 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14071 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14072 		break;
14073 
14074 	case I40E_VSI_IWARP:
14075 		/* send down message to iWARP */
14076 		break;
14077 
14078 	default:
14079 		return -ENODEV;
14080 	}
14081 
14082 	if (vsi->type != I40E_VSI_MAIN) {
14083 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14084 		if (ret) {
14085 			dev_info(&vsi->back->pdev->dev,
14086 				 "add vsi failed, err %s aq_err %s\n",
14087 				 i40e_stat_str(&pf->hw, ret),
14088 				 i40e_aq_str(&pf->hw,
14089 					     pf->hw.aq.asq_last_status));
14090 			ret = -ENOENT;
14091 			goto err;
14092 		}
14093 		vsi->info = ctxt.info;
14094 		vsi->info.valid_sections = 0;
14095 		vsi->seid = ctxt.seid;
14096 		vsi->id = ctxt.vsi_number;
14097 	}
14098 
14099 	vsi->active_filters = 0;
14100 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14101 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14102 	/* If macvlan filters already exist, force them to get loaded */
14103 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14104 		f->state = I40E_FILTER_NEW;
14105 		f_count++;
14106 	}
14107 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14108 
14109 	if (f_count) {
14110 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14111 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14112 	}
14113 
14114 	/* Update VSI BW information */
14115 	ret = i40e_vsi_get_bw_info(vsi);
14116 	if (ret) {
14117 		dev_info(&pf->pdev->dev,
14118 			 "couldn't get vsi bw info, err %s aq_err %s\n",
14119 			 i40e_stat_str(&pf->hw, ret),
14120 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14121 		/* VSI is already added so not tearing that up */
14122 		ret = 0;
14123 	}
14124 
14125 err:
14126 	return ret;
14127 }
14128 
14129 /**
14130  * i40e_vsi_release - Delete a VSI and free its resources
14131  * @vsi: the VSI being removed
14132  *
14133  * Returns 0 on success or < 0 on error
14134  **/
i40e_vsi_release(struct i40e_vsi * vsi)14135 int i40e_vsi_release(struct i40e_vsi *vsi)
14136 {
14137 	struct i40e_mac_filter *f;
14138 	struct hlist_node *h;
14139 	struct i40e_veb *veb = NULL;
14140 	struct i40e_pf *pf;
14141 	u16 uplink_seid;
14142 	int i, n, bkt;
14143 
14144 	pf = vsi->back;
14145 
14146 	/* release of a VEB-owner or last VSI is not allowed */
14147 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14148 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14149 			 vsi->seid, vsi->uplink_seid);
14150 		return -ENODEV;
14151 	}
14152 	if (vsi == pf->vsi[pf->lan_vsi] &&
14153 	    !test_bit(__I40E_DOWN, pf->state)) {
14154 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14155 		return -ENODEV;
14156 	}
14157 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14158 	uplink_seid = vsi->uplink_seid;
14159 	if (vsi->type != I40E_VSI_SRIOV) {
14160 		if (vsi->netdev_registered) {
14161 			vsi->netdev_registered = false;
14162 			if (vsi->netdev) {
14163 				/* results in a call to i40e_close() */
14164 				unregister_netdev(vsi->netdev);
14165 			}
14166 		} else {
14167 			i40e_vsi_close(vsi);
14168 		}
14169 		i40e_vsi_disable_irq(vsi);
14170 	}
14171 
14172 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14173 
14174 	/* clear the sync flag on all filters */
14175 	if (vsi->netdev) {
14176 		__dev_uc_unsync(vsi->netdev, NULL);
14177 		__dev_mc_unsync(vsi->netdev, NULL);
14178 	}
14179 
14180 	/* make sure any remaining filters are marked for deletion */
14181 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14182 		__i40e_del_filter(vsi, f);
14183 
14184 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14185 
14186 	i40e_sync_vsi_filters(vsi);
14187 
14188 	i40e_vsi_delete(vsi);
14189 	i40e_vsi_free_q_vectors(vsi);
14190 	if (vsi->netdev) {
14191 		free_netdev(vsi->netdev);
14192 		vsi->netdev = NULL;
14193 	}
14194 	i40e_vsi_clear_rings(vsi);
14195 	i40e_vsi_clear(vsi);
14196 
14197 	/* If this was the last thing on the VEB, except for the
14198 	 * controlling VSI, remove the VEB, which puts the controlling
14199 	 * VSI onto the next level down in the switch.
14200 	 *
14201 	 * Well, okay, there's one more exception here: don't remove
14202 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14203 	 * from up the network stack.
14204 	 */
14205 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14206 		if (pf->vsi[i] &&
14207 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14208 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14209 			n++;      /* count the VSIs */
14210 		}
14211 	}
14212 	for (i = 0; i < I40E_MAX_VEB; i++) {
14213 		if (!pf->veb[i])
14214 			continue;
14215 		if (pf->veb[i]->uplink_seid == uplink_seid)
14216 			n++;     /* count the VEBs */
14217 		if (pf->veb[i]->seid == uplink_seid)
14218 			veb = pf->veb[i];
14219 	}
14220 	if (n == 0 && veb && veb->uplink_seid != 0)
14221 		i40e_veb_release(veb);
14222 
14223 	return 0;
14224 }
14225 
14226 /**
14227  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14228  * @vsi: ptr to the VSI
14229  *
14230  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14231  * corresponding SW VSI structure and initializes num_queue_pairs for the
14232  * newly allocated VSI.
14233  *
14234  * Returns 0 on success or negative on failure
14235  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)14236 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14237 {
14238 	int ret = -ENOENT;
14239 	struct i40e_pf *pf = vsi->back;
14240 
14241 	if (vsi->q_vectors[0]) {
14242 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14243 			 vsi->seid);
14244 		return -EEXIST;
14245 	}
14246 
14247 	if (vsi->base_vector) {
14248 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14249 			 vsi->seid, vsi->base_vector);
14250 		return -EEXIST;
14251 	}
14252 
14253 	ret = i40e_vsi_alloc_q_vectors(vsi);
14254 	if (ret) {
14255 		dev_info(&pf->pdev->dev,
14256 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14257 			 vsi->num_q_vectors, vsi->seid, ret);
14258 		vsi->num_q_vectors = 0;
14259 		goto vector_setup_out;
14260 	}
14261 
14262 	/* In Legacy mode, we do not have to get any other vector since we
14263 	 * piggyback on the misc/ICR0 for queue interrupts.
14264 	*/
14265 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14266 		return ret;
14267 	if (vsi->num_q_vectors)
14268 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14269 						 vsi->num_q_vectors, vsi->idx);
14270 	if (vsi->base_vector < 0) {
14271 		dev_info(&pf->pdev->dev,
14272 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14273 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14274 		i40e_vsi_free_q_vectors(vsi);
14275 		ret = -ENOENT;
14276 		goto vector_setup_out;
14277 	}
14278 
14279 vector_setup_out:
14280 	return ret;
14281 }
14282 
14283 /**
14284  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14285  * @vsi: pointer to the vsi.
14286  *
14287  * This re-allocates a vsi's queue resources.
14288  *
14289  * Returns pointer to the successfully allocated and configured VSI sw struct
14290  * on success, otherwise returns NULL on failure.
14291  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)14292 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14293 {
14294 	u16 alloc_queue_pairs;
14295 	struct i40e_pf *pf;
14296 	u8 enabled_tc;
14297 	int ret;
14298 
14299 	if (!vsi)
14300 		return NULL;
14301 
14302 	pf = vsi->back;
14303 
14304 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14305 	i40e_vsi_clear_rings(vsi);
14306 
14307 	i40e_vsi_free_arrays(vsi, false);
14308 	i40e_set_num_rings_in_vsi(vsi);
14309 	ret = i40e_vsi_alloc_arrays(vsi, false);
14310 	if (ret)
14311 		goto err_vsi;
14312 
14313 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14314 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14315 
14316 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14317 	if (ret < 0) {
14318 		dev_info(&pf->pdev->dev,
14319 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14320 			 alloc_queue_pairs, vsi->seid, ret);
14321 		goto err_vsi;
14322 	}
14323 	vsi->base_queue = ret;
14324 
14325 	/* Update the FW view of the VSI. Force a reset of TC and queue
14326 	 * layout configurations.
14327 	 */
14328 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14329 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14330 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14331 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14332 	if (vsi->type == I40E_VSI_MAIN)
14333 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14334 
14335 	/* assign it some queues */
14336 	ret = i40e_alloc_rings(vsi);
14337 	if (ret)
14338 		goto err_rings;
14339 
14340 	/* map all of the rings to the q_vectors */
14341 	i40e_vsi_map_rings_to_vectors(vsi);
14342 	return vsi;
14343 
14344 err_rings:
14345 	i40e_vsi_free_q_vectors(vsi);
14346 	if (vsi->netdev_registered) {
14347 		vsi->netdev_registered = false;
14348 		unregister_netdev(vsi->netdev);
14349 		free_netdev(vsi->netdev);
14350 		vsi->netdev = NULL;
14351 	}
14352 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14353 err_vsi:
14354 	i40e_vsi_clear(vsi);
14355 	return NULL;
14356 }
14357 
14358 /**
14359  * i40e_vsi_setup - Set up a VSI by a given type
14360  * @pf: board private structure
14361  * @type: VSI type
14362  * @uplink_seid: the switch element to link to
14363  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14364  *
14365  * This allocates the sw VSI structure and its queue resources, then add a VSI
14366  * to the identified VEB.
14367  *
14368  * Returns pointer to the successfully allocated and configure VSI sw struct on
14369  * success, otherwise returns NULL on failure.
14370  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)14371 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14372 				u16 uplink_seid, u32 param1)
14373 {
14374 	struct i40e_vsi *vsi = NULL;
14375 	struct i40e_veb *veb = NULL;
14376 	u16 alloc_queue_pairs;
14377 	int ret, i;
14378 	int v_idx;
14379 
14380 	/* The requested uplink_seid must be either
14381 	 *     - the PF's port seid
14382 	 *              no VEB is needed because this is the PF
14383 	 *              or this is a Flow Director special case VSI
14384 	 *     - seid of an existing VEB
14385 	 *     - seid of a VSI that owns an existing VEB
14386 	 *     - seid of a VSI that doesn't own a VEB
14387 	 *              a new VEB is created and the VSI becomes the owner
14388 	 *     - seid of the PF VSI, which is what creates the first VEB
14389 	 *              this is a special case of the previous
14390 	 *
14391 	 * Find which uplink_seid we were given and create a new VEB if needed
14392 	 */
14393 	for (i = 0; i < I40E_MAX_VEB; i++) {
14394 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14395 			veb = pf->veb[i];
14396 			break;
14397 		}
14398 	}
14399 
14400 	if (!veb && uplink_seid != pf->mac_seid) {
14401 
14402 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14403 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14404 				vsi = pf->vsi[i];
14405 				break;
14406 			}
14407 		}
14408 		if (!vsi) {
14409 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14410 				 uplink_seid);
14411 			return NULL;
14412 		}
14413 
14414 		if (vsi->uplink_seid == pf->mac_seid)
14415 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14416 					     vsi->tc_config.enabled_tc);
14417 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14418 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14419 					     vsi->tc_config.enabled_tc);
14420 		if (veb) {
14421 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14422 				dev_info(&vsi->back->pdev->dev,
14423 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14424 				return NULL;
14425 			}
14426 			/* We come up by default in VEPA mode if SRIOV is not
14427 			 * already enabled, in which case we can't force VEPA
14428 			 * mode.
14429 			 */
14430 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14431 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14432 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14433 			}
14434 			i40e_config_bridge_mode(veb);
14435 		}
14436 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14437 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14438 				veb = pf->veb[i];
14439 		}
14440 		if (!veb) {
14441 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14442 			return NULL;
14443 		}
14444 
14445 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14446 		uplink_seid = veb->seid;
14447 	}
14448 
14449 	/* get vsi sw struct */
14450 	v_idx = i40e_vsi_mem_alloc(pf, type);
14451 	if (v_idx < 0)
14452 		goto err_alloc;
14453 	vsi = pf->vsi[v_idx];
14454 	if (!vsi)
14455 		goto err_alloc;
14456 	vsi->type = type;
14457 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14458 
14459 	if (type == I40E_VSI_MAIN)
14460 		pf->lan_vsi = v_idx;
14461 	else if (type == I40E_VSI_SRIOV)
14462 		vsi->vf_id = param1;
14463 	/* assign it some queues */
14464 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14465 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14466 
14467 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14468 	if (ret < 0) {
14469 		dev_info(&pf->pdev->dev,
14470 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14471 			 alloc_queue_pairs, vsi->seid, ret);
14472 		goto err_vsi;
14473 	}
14474 	vsi->base_queue = ret;
14475 
14476 	/* get a VSI from the hardware */
14477 	vsi->uplink_seid = uplink_seid;
14478 	ret = i40e_add_vsi(vsi);
14479 	if (ret)
14480 		goto err_vsi;
14481 
14482 	switch (vsi->type) {
14483 	/* setup the netdev if needed */
14484 	case I40E_VSI_MAIN:
14485 	case I40E_VSI_VMDQ2:
14486 		ret = i40e_config_netdev(vsi);
14487 		if (ret)
14488 			goto err_netdev;
14489 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14490 		if (ret)
14491 			goto err_netdev;
14492 		ret = register_netdev(vsi->netdev);
14493 		if (ret)
14494 			goto err_netdev;
14495 		vsi->netdev_registered = true;
14496 		netif_carrier_off(vsi->netdev);
14497 #ifdef CONFIG_I40E_DCB
14498 		/* Setup DCB netlink interface */
14499 		i40e_dcbnl_setup(vsi);
14500 #endif /* CONFIG_I40E_DCB */
14501 		fallthrough;
14502 	case I40E_VSI_FDIR:
14503 		/* set up vectors and rings if needed */
14504 		ret = i40e_vsi_setup_vectors(vsi);
14505 		if (ret)
14506 			goto err_msix;
14507 
14508 		ret = i40e_alloc_rings(vsi);
14509 		if (ret)
14510 			goto err_rings;
14511 
14512 		/* map all of the rings to the q_vectors */
14513 		i40e_vsi_map_rings_to_vectors(vsi);
14514 
14515 		i40e_vsi_reset_stats(vsi);
14516 		break;
14517 	default:
14518 		/* no netdev or rings for the other VSI types */
14519 		break;
14520 	}
14521 
14522 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14523 	    (vsi->type == I40E_VSI_VMDQ2)) {
14524 		ret = i40e_vsi_config_rss(vsi);
14525 	}
14526 	return vsi;
14527 
14528 err_rings:
14529 	i40e_vsi_free_q_vectors(vsi);
14530 err_msix:
14531 	if (vsi->netdev_registered) {
14532 		vsi->netdev_registered = false;
14533 		unregister_netdev(vsi->netdev);
14534 		free_netdev(vsi->netdev);
14535 		vsi->netdev = NULL;
14536 	}
14537 err_netdev:
14538 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14539 err_vsi:
14540 	i40e_vsi_clear(vsi);
14541 err_alloc:
14542 	return NULL;
14543 }
14544 
14545 /**
14546  * i40e_veb_get_bw_info - Query VEB BW information
14547  * @veb: the veb to query
14548  *
14549  * Query the Tx scheduler BW configuration data for given VEB
14550  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)14551 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14552 {
14553 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14554 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14555 	struct i40e_pf *pf = veb->pf;
14556 	struct i40e_hw *hw = &pf->hw;
14557 	u32 tc_bw_max;
14558 	int ret = 0;
14559 	int i;
14560 
14561 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14562 						  &bw_data, NULL);
14563 	if (ret) {
14564 		dev_info(&pf->pdev->dev,
14565 			 "query veb bw config failed, err %s aq_err %s\n",
14566 			 i40e_stat_str(&pf->hw, ret),
14567 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14568 		goto out;
14569 	}
14570 
14571 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14572 						   &ets_data, NULL);
14573 	if (ret) {
14574 		dev_info(&pf->pdev->dev,
14575 			 "query veb bw ets config failed, err %s aq_err %s\n",
14576 			 i40e_stat_str(&pf->hw, ret),
14577 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14578 		goto out;
14579 	}
14580 
14581 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14582 	veb->bw_max_quanta = ets_data.tc_bw_max;
14583 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14584 	veb->enabled_tc = ets_data.tc_valid_bits;
14585 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14586 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14587 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14588 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14589 		veb->bw_tc_limit_credits[i] =
14590 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14591 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14592 	}
14593 
14594 out:
14595 	return ret;
14596 }
14597 
14598 /**
14599  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14600  * @pf: board private structure
14601  *
14602  * On error: returns error code (negative)
14603  * On success: returns vsi index in PF (positive)
14604  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)14605 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14606 {
14607 	int ret = -ENOENT;
14608 	struct i40e_veb *veb;
14609 	int i;
14610 
14611 	/* Need to protect the allocation of switch elements at the PF level */
14612 	mutex_lock(&pf->switch_mutex);
14613 
14614 	/* VEB list may be fragmented if VEB creation/destruction has
14615 	 * been happening.  We can afford to do a quick scan to look
14616 	 * for any free slots in the list.
14617 	 *
14618 	 * find next empty veb slot, looping back around if necessary
14619 	 */
14620 	i = 0;
14621 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14622 		i++;
14623 	if (i >= I40E_MAX_VEB) {
14624 		ret = -ENOMEM;
14625 		goto err_alloc_veb;  /* out of VEB slots! */
14626 	}
14627 
14628 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14629 	if (!veb) {
14630 		ret = -ENOMEM;
14631 		goto err_alloc_veb;
14632 	}
14633 	veb->pf = pf;
14634 	veb->idx = i;
14635 	veb->enabled_tc = 1;
14636 
14637 	pf->veb[i] = veb;
14638 	ret = i;
14639 err_alloc_veb:
14640 	mutex_unlock(&pf->switch_mutex);
14641 	return ret;
14642 }
14643 
14644 /**
14645  * i40e_switch_branch_release - Delete a branch of the switch tree
14646  * @branch: where to start deleting
14647  *
14648  * This uses recursion to find the tips of the branch to be
14649  * removed, deleting until we get back to and can delete this VEB.
14650  **/
i40e_switch_branch_release(struct i40e_veb * branch)14651 static void i40e_switch_branch_release(struct i40e_veb *branch)
14652 {
14653 	struct i40e_pf *pf = branch->pf;
14654 	u16 branch_seid = branch->seid;
14655 	u16 veb_idx = branch->idx;
14656 	int i;
14657 
14658 	/* release any VEBs on this VEB - RECURSION */
14659 	for (i = 0; i < I40E_MAX_VEB; i++) {
14660 		if (!pf->veb[i])
14661 			continue;
14662 		if (pf->veb[i]->uplink_seid == branch->seid)
14663 			i40e_switch_branch_release(pf->veb[i]);
14664 	}
14665 
14666 	/* Release the VSIs on this VEB, but not the owner VSI.
14667 	 *
14668 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14669 	 *       the VEB itself, so don't use (*branch) after this loop.
14670 	 */
14671 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14672 		if (!pf->vsi[i])
14673 			continue;
14674 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14675 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14676 			i40e_vsi_release(pf->vsi[i]);
14677 		}
14678 	}
14679 
14680 	/* There's one corner case where the VEB might not have been
14681 	 * removed, so double check it here and remove it if needed.
14682 	 * This case happens if the veb was created from the debugfs
14683 	 * commands and no VSIs were added to it.
14684 	 */
14685 	if (pf->veb[veb_idx])
14686 		i40e_veb_release(pf->veb[veb_idx]);
14687 }
14688 
14689 /**
14690  * i40e_veb_clear - remove veb struct
14691  * @veb: the veb to remove
14692  **/
i40e_veb_clear(struct i40e_veb * veb)14693 static void i40e_veb_clear(struct i40e_veb *veb)
14694 {
14695 	if (!veb)
14696 		return;
14697 
14698 	if (veb->pf) {
14699 		struct i40e_pf *pf = veb->pf;
14700 
14701 		mutex_lock(&pf->switch_mutex);
14702 		if (pf->veb[veb->idx] == veb)
14703 			pf->veb[veb->idx] = NULL;
14704 		mutex_unlock(&pf->switch_mutex);
14705 	}
14706 
14707 	kfree(veb);
14708 }
14709 
14710 /**
14711  * i40e_veb_release - Delete a VEB and free its resources
14712  * @veb: the VEB being removed
14713  **/
i40e_veb_release(struct i40e_veb * veb)14714 void i40e_veb_release(struct i40e_veb *veb)
14715 {
14716 	struct i40e_vsi *vsi = NULL;
14717 	struct i40e_pf *pf;
14718 	int i, n = 0;
14719 
14720 	pf = veb->pf;
14721 
14722 	/* find the remaining VSI and check for extras */
14723 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14724 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14725 			n++;
14726 			vsi = pf->vsi[i];
14727 		}
14728 	}
14729 	if (n != 1) {
14730 		dev_info(&pf->pdev->dev,
14731 			 "can't remove VEB %d with %d VSIs left\n",
14732 			 veb->seid, n);
14733 		return;
14734 	}
14735 
14736 	/* move the remaining VSI to uplink veb */
14737 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14738 	if (veb->uplink_seid) {
14739 		vsi->uplink_seid = veb->uplink_seid;
14740 		if (veb->uplink_seid == pf->mac_seid)
14741 			vsi->veb_idx = I40E_NO_VEB;
14742 		else
14743 			vsi->veb_idx = veb->veb_idx;
14744 	} else {
14745 		/* floating VEB */
14746 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14747 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14748 	}
14749 
14750 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14751 	i40e_veb_clear(veb);
14752 }
14753 
14754 /**
14755  * i40e_add_veb - create the VEB in the switch
14756  * @veb: the VEB to be instantiated
14757  * @vsi: the controlling VSI
14758  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)14759 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14760 {
14761 	struct i40e_pf *pf = veb->pf;
14762 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14763 	int ret;
14764 
14765 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14766 			      veb->enabled_tc, false,
14767 			      &veb->seid, enable_stats, NULL);
14768 
14769 	/* get a VEB from the hardware */
14770 	if (ret) {
14771 		dev_info(&pf->pdev->dev,
14772 			 "couldn't add VEB, err %s aq_err %s\n",
14773 			 i40e_stat_str(&pf->hw, ret),
14774 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14775 		return -EPERM;
14776 	}
14777 
14778 	/* get statistics counter */
14779 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14780 					 &veb->stats_idx, NULL, NULL, NULL);
14781 	if (ret) {
14782 		dev_info(&pf->pdev->dev,
14783 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14784 			 i40e_stat_str(&pf->hw, ret),
14785 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14786 		return -EPERM;
14787 	}
14788 	ret = i40e_veb_get_bw_info(veb);
14789 	if (ret) {
14790 		dev_info(&pf->pdev->dev,
14791 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14792 			 i40e_stat_str(&pf->hw, ret),
14793 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14794 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14795 		return -ENOENT;
14796 	}
14797 
14798 	vsi->uplink_seid = veb->seid;
14799 	vsi->veb_idx = veb->idx;
14800 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14801 
14802 	return 0;
14803 }
14804 
14805 /**
14806  * i40e_veb_setup - Set up a VEB
14807  * @pf: board private structure
14808  * @flags: VEB setup flags
14809  * @uplink_seid: the switch element to link to
14810  * @vsi_seid: the initial VSI seid
14811  * @enabled_tc: Enabled TC bit-map
14812  *
14813  * This allocates the sw VEB structure and links it into the switch
14814  * It is possible and legal for this to be a duplicate of an already
14815  * existing VEB.  It is also possible for both uplink and vsi seids
14816  * to be zero, in order to create a floating VEB.
14817  *
14818  * Returns pointer to the successfully allocated VEB sw struct on
14819  * success, otherwise returns NULL on failure.
14820  **/
i40e_veb_setup(struct i40e_pf * pf,u16 flags,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14821 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14822 				u16 uplink_seid, u16 vsi_seid,
14823 				u8 enabled_tc)
14824 {
14825 	struct i40e_veb *veb, *uplink_veb = NULL;
14826 	int vsi_idx, veb_idx;
14827 	int ret;
14828 
14829 	/* if one seid is 0, the other must be 0 to create a floating relay */
14830 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14831 	    (uplink_seid + vsi_seid != 0)) {
14832 		dev_info(&pf->pdev->dev,
14833 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14834 			 uplink_seid, vsi_seid);
14835 		return NULL;
14836 	}
14837 
14838 	/* make sure there is such a vsi and uplink */
14839 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14840 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14841 			break;
14842 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14843 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14844 			 vsi_seid);
14845 		return NULL;
14846 	}
14847 
14848 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14849 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14850 			if (pf->veb[veb_idx] &&
14851 			    pf->veb[veb_idx]->seid == uplink_seid) {
14852 				uplink_veb = pf->veb[veb_idx];
14853 				break;
14854 			}
14855 		}
14856 		if (!uplink_veb) {
14857 			dev_info(&pf->pdev->dev,
14858 				 "uplink seid %d not found\n", uplink_seid);
14859 			return NULL;
14860 		}
14861 	}
14862 
14863 	/* get veb sw struct */
14864 	veb_idx = i40e_veb_mem_alloc(pf);
14865 	if (veb_idx < 0)
14866 		goto err_alloc;
14867 	veb = pf->veb[veb_idx];
14868 	veb->flags = flags;
14869 	veb->uplink_seid = uplink_seid;
14870 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14871 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14872 
14873 	/* create the VEB in the switch */
14874 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14875 	if (ret)
14876 		goto err_veb;
14877 	if (vsi_idx == pf->lan_vsi)
14878 		pf->lan_veb = veb->idx;
14879 
14880 	return veb;
14881 
14882 err_veb:
14883 	i40e_veb_clear(veb);
14884 err_alloc:
14885 	return NULL;
14886 }
14887 
14888 /**
14889  * i40e_setup_pf_switch_element - set PF vars based on switch type
14890  * @pf: board private structure
14891  * @ele: element we are building info from
14892  * @num_reported: total number of elements
14893  * @printconfig: should we print the contents
14894  *
14895  * helper function to assist in extracting a few useful SEID values.
14896  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14897 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14898 				struct i40e_aqc_switch_config_element_resp *ele,
14899 				u16 num_reported, bool printconfig)
14900 {
14901 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14902 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14903 	u8 element_type = ele->element_type;
14904 	u16 seid = le16_to_cpu(ele->seid);
14905 
14906 	if (printconfig)
14907 		dev_info(&pf->pdev->dev,
14908 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14909 			 element_type, seid, uplink_seid, downlink_seid);
14910 
14911 	switch (element_type) {
14912 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14913 		pf->mac_seid = seid;
14914 		break;
14915 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14916 		/* Main VEB? */
14917 		if (uplink_seid != pf->mac_seid)
14918 			break;
14919 		if (pf->lan_veb >= I40E_MAX_VEB) {
14920 			int v;
14921 
14922 			/* find existing or else empty VEB */
14923 			for (v = 0; v < I40E_MAX_VEB; v++) {
14924 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14925 					pf->lan_veb = v;
14926 					break;
14927 				}
14928 			}
14929 			if (pf->lan_veb >= I40E_MAX_VEB) {
14930 				v = i40e_veb_mem_alloc(pf);
14931 				if (v < 0)
14932 					break;
14933 				pf->lan_veb = v;
14934 			}
14935 		}
14936 		if (pf->lan_veb >= I40E_MAX_VEB)
14937 			break;
14938 
14939 		pf->veb[pf->lan_veb]->seid = seid;
14940 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14941 		pf->veb[pf->lan_veb]->pf = pf;
14942 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14943 		break;
14944 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14945 		if (num_reported != 1)
14946 			break;
14947 		/* This is immediately after a reset so we can assume this is
14948 		 * the PF's VSI
14949 		 */
14950 		pf->mac_seid = uplink_seid;
14951 		pf->pf_seid = downlink_seid;
14952 		pf->main_vsi_seid = seid;
14953 		if (printconfig)
14954 			dev_info(&pf->pdev->dev,
14955 				 "pf_seid=%d main_vsi_seid=%d\n",
14956 				 pf->pf_seid, pf->main_vsi_seid);
14957 		break;
14958 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14959 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14960 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14961 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14962 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14963 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14964 		/* ignore these for now */
14965 		break;
14966 	default:
14967 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14968 			 element_type, seid);
14969 		break;
14970 	}
14971 }
14972 
14973 /**
14974  * i40e_fetch_switch_configuration - Get switch config from firmware
14975  * @pf: board private structure
14976  * @printconfig: should we print the contents
14977  *
14978  * Get the current switch configuration from the device and
14979  * extract a few useful SEID values.
14980  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)14981 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14982 {
14983 	struct i40e_aqc_get_switch_config_resp *sw_config;
14984 	u16 next_seid = 0;
14985 	int ret = 0;
14986 	u8 *aq_buf;
14987 	int i;
14988 
14989 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14990 	if (!aq_buf)
14991 		return -ENOMEM;
14992 
14993 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14994 	do {
14995 		u16 num_reported, num_total;
14996 
14997 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14998 						I40E_AQ_LARGE_BUF,
14999 						&next_seid, NULL);
15000 		if (ret) {
15001 			dev_info(&pf->pdev->dev,
15002 				 "get switch config failed err %s aq_err %s\n",
15003 				 i40e_stat_str(&pf->hw, ret),
15004 				 i40e_aq_str(&pf->hw,
15005 					     pf->hw.aq.asq_last_status));
15006 			kfree(aq_buf);
15007 			return -ENOENT;
15008 		}
15009 
15010 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15011 		num_total = le16_to_cpu(sw_config->header.num_total);
15012 
15013 		if (printconfig)
15014 			dev_info(&pf->pdev->dev,
15015 				 "header: %d reported %d total\n",
15016 				 num_reported, num_total);
15017 
15018 		for (i = 0; i < num_reported; i++) {
15019 			struct i40e_aqc_switch_config_element_resp *ele =
15020 				&sw_config->element[i];
15021 
15022 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15023 						     printconfig);
15024 		}
15025 	} while (next_seid != 0);
15026 
15027 	kfree(aq_buf);
15028 	return ret;
15029 }
15030 
15031 /**
15032  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15033  * @pf: board private structure
15034  * @reinit: if the Main VSI needs to re-initialized.
15035  * @lock_acquired: indicates whether or not the lock has been acquired
15036  *
15037  * Returns 0 on success, negative value on failure
15038  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)15039 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15040 {
15041 	u16 flags = 0;
15042 	int ret;
15043 
15044 	/* find out what's out there already */
15045 	ret = i40e_fetch_switch_configuration(pf, false);
15046 	if (ret) {
15047 		dev_info(&pf->pdev->dev,
15048 			 "couldn't fetch switch config, err %s aq_err %s\n",
15049 			 i40e_stat_str(&pf->hw, ret),
15050 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15051 		return ret;
15052 	}
15053 	i40e_pf_reset_stats(pf);
15054 
15055 	/* set the switch config bit for the whole device to
15056 	 * support limited promisc or true promisc
15057 	 * when user requests promisc. The default is limited
15058 	 * promisc.
15059 	*/
15060 
15061 	if ((pf->hw.pf_id == 0) &&
15062 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15063 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15064 		pf->last_sw_conf_flags = flags;
15065 	}
15066 
15067 	if (pf->hw.pf_id == 0) {
15068 		u16 valid_flags;
15069 
15070 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15071 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15072 						NULL);
15073 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15074 			dev_info(&pf->pdev->dev,
15075 				 "couldn't set switch config bits, err %s aq_err %s\n",
15076 				 i40e_stat_str(&pf->hw, ret),
15077 				 i40e_aq_str(&pf->hw,
15078 					     pf->hw.aq.asq_last_status));
15079 			/* not a fatal problem, just keep going */
15080 		}
15081 		pf->last_sw_conf_valid_flags = valid_flags;
15082 	}
15083 
15084 	/* first time setup */
15085 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15086 		struct i40e_vsi *vsi = NULL;
15087 		u16 uplink_seid;
15088 
15089 		/* Set up the PF VSI associated with the PF's main VSI
15090 		 * that is already in the HW switch
15091 		 */
15092 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15093 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15094 		else
15095 			uplink_seid = pf->mac_seid;
15096 		if (pf->lan_vsi == I40E_NO_VSI)
15097 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15098 		else if (reinit)
15099 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15100 		if (!vsi) {
15101 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15102 			i40e_cloud_filter_exit(pf);
15103 			i40e_fdir_teardown(pf);
15104 			return -EAGAIN;
15105 		}
15106 	} else {
15107 		/* force a reset of TC and queue layout configurations */
15108 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15109 
15110 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15111 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15112 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15113 	}
15114 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15115 
15116 	i40e_fdir_sb_setup(pf);
15117 
15118 	/* Setup static PF queue filter control settings */
15119 	ret = i40e_setup_pf_filter_control(pf);
15120 	if (ret) {
15121 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15122 			 ret);
15123 		/* Failure here should not stop continuing other steps */
15124 	}
15125 
15126 	/* enable RSS in the HW, even for only one queue, as the stack can use
15127 	 * the hash
15128 	 */
15129 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15130 		i40e_pf_config_rss(pf);
15131 
15132 	/* fill in link information and enable LSE reporting */
15133 	i40e_link_event(pf);
15134 
15135 	/* Initialize user-specific link properties */
15136 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15137 				  I40E_AQ_AN_COMPLETED) ? true : false);
15138 
15139 	i40e_ptp_init(pf);
15140 
15141 	if (!lock_acquired)
15142 		rtnl_lock();
15143 
15144 	/* repopulate tunnel port filters */
15145 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15146 
15147 	if (!lock_acquired)
15148 		rtnl_unlock();
15149 
15150 	return ret;
15151 }
15152 
15153 /**
15154  * i40e_determine_queue_usage - Work out queue distribution
15155  * @pf: board private structure
15156  **/
i40e_determine_queue_usage(struct i40e_pf * pf)15157 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15158 {
15159 	int queues_left;
15160 	int q_max;
15161 
15162 	pf->num_lan_qps = 0;
15163 
15164 	/* Find the max queues to be put into basic use.  We'll always be
15165 	 * using TC0, whether or not DCB is running, and TC0 will get the
15166 	 * big RSS set.
15167 	 */
15168 	queues_left = pf->hw.func_caps.num_tx_qp;
15169 
15170 	if ((queues_left == 1) ||
15171 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15172 		/* one qp for PF, no queues for anything else */
15173 		queues_left = 0;
15174 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15175 
15176 		/* make sure all the fancies are disabled */
15177 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15178 			       I40E_FLAG_IWARP_ENABLED	|
15179 			       I40E_FLAG_FD_SB_ENABLED	|
15180 			       I40E_FLAG_FD_ATR_ENABLED	|
15181 			       I40E_FLAG_DCB_CAPABLE	|
15182 			       I40E_FLAG_DCB_ENABLED	|
15183 			       I40E_FLAG_SRIOV_ENABLED	|
15184 			       I40E_FLAG_VMDQ_ENABLED);
15185 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15186 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15187 				  I40E_FLAG_FD_SB_ENABLED |
15188 				  I40E_FLAG_FD_ATR_ENABLED |
15189 				  I40E_FLAG_DCB_CAPABLE))) {
15190 		/* one qp for PF */
15191 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15192 		queues_left -= pf->num_lan_qps;
15193 
15194 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15195 			       I40E_FLAG_IWARP_ENABLED	|
15196 			       I40E_FLAG_FD_SB_ENABLED	|
15197 			       I40E_FLAG_FD_ATR_ENABLED	|
15198 			       I40E_FLAG_DCB_ENABLED	|
15199 			       I40E_FLAG_VMDQ_ENABLED);
15200 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15201 	} else {
15202 		/* Not enough queues for all TCs */
15203 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15204 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15205 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15206 					I40E_FLAG_DCB_ENABLED);
15207 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15208 		}
15209 
15210 		/* limit lan qps to the smaller of qps, cpus or msix */
15211 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15212 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15213 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15214 		pf->num_lan_qps = q_max;
15215 
15216 		queues_left -= pf->num_lan_qps;
15217 	}
15218 
15219 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15220 		if (queues_left > 1) {
15221 			queues_left -= 1; /* save 1 queue for FD */
15222 		} else {
15223 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15224 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15225 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15226 		}
15227 	}
15228 
15229 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15230 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15231 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15232 					(queues_left / pf->num_vf_qps));
15233 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15234 	}
15235 
15236 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15237 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15238 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15239 					  (queues_left / pf->num_vmdq_qps));
15240 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15241 	}
15242 
15243 	pf->queues_left = queues_left;
15244 	dev_dbg(&pf->pdev->dev,
15245 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15246 		pf->hw.func_caps.num_tx_qp,
15247 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15248 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15249 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15250 		queues_left);
15251 }
15252 
15253 /**
15254  * i40e_setup_pf_filter_control - Setup PF static filter control
15255  * @pf: PF to be setup
15256  *
15257  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15258  * settings. If PE/FCoE are enabled then it will also set the per PF
15259  * based filter sizes required for them. It also enables Flow director,
15260  * ethertype and macvlan type filter settings for the pf.
15261  *
15262  * Returns 0 on success, negative on failure
15263  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)15264 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15265 {
15266 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15267 
15268 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15269 
15270 	/* Flow Director is enabled */
15271 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15272 		settings->enable_fdir = true;
15273 
15274 	/* Ethtype and MACVLAN filters enabled for PF */
15275 	settings->enable_ethtype = true;
15276 	settings->enable_macvlan = true;
15277 
15278 	if (i40e_set_filter_control(&pf->hw, settings))
15279 		return -ENOENT;
15280 
15281 	return 0;
15282 }
15283 
15284 #define INFO_STRING_LEN 255
15285 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)15286 static void i40e_print_features(struct i40e_pf *pf)
15287 {
15288 	struct i40e_hw *hw = &pf->hw;
15289 	char *buf;
15290 	int i;
15291 
15292 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15293 	if (!buf)
15294 		return;
15295 
15296 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15297 #ifdef CONFIG_PCI_IOV
15298 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15299 #endif
15300 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15301 		      pf->hw.func_caps.num_vsis,
15302 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15303 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15304 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15305 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15306 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15307 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15308 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15309 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15310 	}
15311 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15312 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15313 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15314 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15315 	if (pf->flags & I40E_FLAG_PTP)
15316 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15317 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15318 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15319 	else
15320 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15321 
15322 	dev_info(&pf->pdev->dev, "%s\n", buf);
15323 	kfree(buf);
15324 	WARN_ON(i > INFO_STRING_LEN);
15325 }
15326 
15327 /**
15328  * i40e_get_platform_mac_addr - get platform-specific MAC address
15329  * @pdev: PCI device information struct
15330  * @pf: board private structure
15331  *
15332  * Look up the MAC address for the device. First we'll try
15333  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15334  * specific fallback. Otherwise, we'll default to the stored value in
15335  * firmware.
15336  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)15337 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15338 {
15339 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15340 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15341 }
15342 
15343 /**
15344  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15345  * @fec_cfg: FEC option to set in flags
15346  * @flags: ptr to flags in which we set FEC option
15347  **/
i40e_set_fec_in_flags(u8 fec_cfg,u32 * flags)15348 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15349 {
15350 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15351 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15352 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15353 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15354 		*flags |= I40E_FLAG_RS_FEC;
15355 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15356 	}
15357 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15358 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15359 		*flags |= I40E_FLAG_BASE_R_FEC;
15360 		*flags &= ~I40E_FLAG_RS_FEC;
15361 	}
15362 	if (fec_cfg == 0)
15363 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15364 }
15365 
15366 /**
15367  * i40e_check_recovery_mode - check if we are running transition firmware
15368  * @pf: board private structure
15369  *
15370  * Check registers indicating the firmware runs in recovery mode. Sets the
15371  * appropriate driver state.
15372  *
15373  * Returns true if the recovery mode was detected, false otherwise
15374  **/
i40e_check_recovery_mode(struct i40e_pf * pf)15375 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15376 {
15377 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15378 
15379 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15380 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15381 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15382 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15383 
15384 		return true;
15385 	}
15386 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15387 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15388 
15389 	return false;
15390 }
15391 
15392 /**
15393  * i40e_pf_loop_reset - perform reset in a loop.
15394  * @pf: board private structure
15395  *
15396  * This function is useful when a NIC is about to enter recovery mode.
15397  * When a NIC's internal data structures are corrupted the NIC's
15398  * firmware is going to enter recovery mode.
15399  * Right after a POR it takes about 7 minutes for firmware to enter
15400  * recovery mode. Until that time a NIC is in some kind of intermediate
15401  * state. After that time period the NIC almost surely enters
15402  * recovery mode. The only way for a driver to detect intermediate
15403  * state is to issue a series of pf-resets and check a return value.
15404  * If a PF reset returns success then the firmware could be in recovery
15405  * mode so the caller of this code needs to check for recovery mode
15406  * if this function returns success. There is a little chance that
15407  * firmware will hang in intermediate state forever.
15408  * Since waiting 7 minutes is quite a lot of time this function waits
15409  * 10 seconds and then gives up by returning an error.
15410  *
15411  * Return 0 on success, negative on failure.
15412  **/
i40e_pf_loop_reset(struct i40e_pf * pf)15413 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15414 {
15415 	/* wait max 10 seconds for PF reset to succeed */
15416 	const unsigned long time_end = jiffies + 10 * HZ;
15417 
15418 	struct i40e_hw *hw = &pf->hw;
15419 	i40e_status ret;
15420 
15421 	ret = i40e_pf_reset(hw);
15422 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15423 		usleep_range(10000, 20000);
15424 		ret = i40e_pf_reset(hw);
15425 	}
15426 
15427 	if (ret == I40E_SUCCESS)
15428 		pf->pfr_count++;
15429 	else
15430 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15431 
15432 	return ret;
15433 }
15434 
15435 /**
15436  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15437  * @pf: board private structure
15438  *
15439  * Check FW registers to determine if FW issued unexpected EMP Reset.
15440  * Every time when unexpected EMP Reset occurs the FW increments
15441  * a counter of unexpected EMP Resets. When the counter reaches 10
15442  * the FW should enter the Recovery mode
15443  *
15444  * Returns true if FW issued unexpected EMP Reset
15445  **/
i40e_check_fw_empr(struct i40e_pf * pf)15446 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15447 {
15448 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15449 			   I40E_GL_FWSTS_FWS1B_MASK;
15450 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15451 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15452 }
15453 
15454 /**
15455  * i40e_handle_resets - handle EMP resets and PF resets
15456  * @pf: board private structure
15457  *
15458  * Handle both EMP resets and PF resets and conclude whether there are
15459  * any issues regarding these resets. If there are any issues then
15460  * generate log entry.
15461  *
15462  * Return 0 if NIC is healthy or negative value when there are issues
15463  * with resets
15464  **/
i40e_handle_resets(struct i40e_pf * pf)15465 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15466 {
15467 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15468 	const bool is_empr = i40e_check_fw_empr(pf);
15469 
15470 	if (is_empr || pfr != I40E_SUCCESS)
15471 		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");
15472 
15473 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15474 }
15475 
15476 /**
15477  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15478  * @pf: board private structure
15479  * @hw: ptr to the hardware info
15480  *
15481  * This function does a minimal setup of all subsystems needed for running
15482  * recovery mode.
15483  *
15484  * Returns 0 on success, negative on failure
15485  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)15486 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15487 {
15488 	struct i40e_vsi *vsi;
15489 	int err;
15490 	int v_idx;
15491 
15492 	pci_save_state(pf->pdev);
15493 
15494 	/* set up periodic task facility */
15495 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15496 	pf->service_timer_period = HZ;
15497 
15498 	INIT_WORK(&pf->service_task, i40e_service_task);
15499 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15500 
15501 	err = i40e_init_interrupt_scheme(pf);
15502 	if (err)
15503 		goto err_switch_setup;
15504 
15505 	/* The number of VSIs reported by the FW is the minimum guaranteed
15506 	 * to us; HW supports far more and we share the remaining pool with
15507 	 * the other PFs. We allocate space for more than the guarantee with
15508 	 * the understanding that we might not get them all later.
15509 	 */
15510 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15511 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15512 	else
15513 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15514 
15515 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15516 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15517 			  GFP_KERNEL);
15518 	if (!pf->vsi) {
15519 		err = -ENOMEM;
15520 		goto err_switch_setup;
15521 	}
15522 
15523 	/* We allocate one VSI which is needed as absolute minimum
15524 	 * in order to register the netdev
15525 	 */
15526 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15527 	if (v_idx < 0) {
15528 		err = v_idx;
15529 		goto err_switch_setup;
15530 	}
15531 	pf->lan_vsi = v_idx;
15532 	vsi = pf->vsi[v_idx];
15533 	if (!vsi) {
15534 		err = -EFAULT;
15535 		goto err_switch_setup;
15536 	}
15537 	vsi->alloc_queue_pairs = 1;
15538 	err = i40e_config_netdev(vsi);
15539 	if (err)
15540 		goto err_switch_setup;
15541 	err = register_netdev(vsi->netdev);
15542 	if (err)
15543 		goto err_switch_setup;
15544 	vsi->netdev_registered = true;
15545 	i40e_dbg_pf_init(pf);
15546 
15547 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15548 	if (err)
15549 		goto err_switch_setup;
15550 
15551 	/* tell the firmware that we're starting */
15552 	i40e_send_version(pf);
15553 
15554 	/* since everything's happy, start the service_task timer */
15555 	mod_timer(&pf->service_timer,
15556 		  round_jiffies(jiffies + pf->service_timer_period));
15557 
15558 	return 0;
15559 
15560 err_switch_setup:
15561 	i40e_reset_interrupt_capability(pf);
15562 	del_timer_sync(&pf->service_timer);
15563 	i40e_shutdown_adminq(hw);
15564 	iounmap(hw->hw_addr);
15565 	pci_disable_pcie_error_reporting(pf->pdev);
15566 	pci_release_mem_regions(pf->pdev);
15567 	pci_disable_device(pf->pdev);
15568 	kfree(pf);
15569 
15570 	return err;
15571 }
15572 
15573 /**
15574  * i40e_set_subsystem_device_id - set subsystem device id
15575  * @hw: pointer to the hardware info
15576  *
15577  * Set PCI subsystem device id either from a pci_dev structure or
15578  * a specific FW register.
15579  **/
i40e_set_subsystem_device_id(struct i40e_hw * hw)15580 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15581 {
15582 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15583 
15584 	hw->subsystem_device_id = pdev->subsystem_device ?
15585 		pdev->subsystem_device :
15586 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15587 }
15588 
15589 /**
15590  * i40e_probe - Device initialization routine
15591  * @pdev: PCI device information struct
15592  * @ent: entry in i40e_pci_tbl
15593  *
15594  * i40e_probe initializes a PF identified by a pci_dev structure.
15595  * The OS initialization, configuring of the PF private structure,
15596  * and a hardware reset occur.
15597  *
15598  * Returns 0 on success, negative on failure
15599  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)15600 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15601 {
15602 	struct i40e_aq_get_phy_abilities_resp abilities;
15603 #ifdef CONFIG_I40E_DCB
15604 	enum i40e_get_fw_lldp_status_resp lldp_status;
15605 	i40e_status status;
15606 #endif /* CONFIG_I40E_DCB */
15607 	struct i40e_pf *pf;
15608 	struct i40e_hw *hw;
15609 	static u16 pfs_found;
15610 	u16 wol_nvm_bits;
15611 	u16 link_status;
15612 	int err;
15613 	u32 val;
15614 	u32 i;
15615 
15616 	err = pci_enable_device_mem(pdev);
15617 	if (err)
15618 		return err;
15619 
15620 	/* set up for high or low dma */
15621 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15622 	if (err) {
15623 		dev_err(&pdev->dev,
15624 			"DMA configuration failed: 0x%x\n", err);
15625 		goto err_dma;
15626 	}
15627 
15628 	/* set up pci connections */
15629 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15630 	if (err) {
15631 		dev_info(&pdev->dev,
15632 			 "pci_request_selected_regions failed %d\n", err);
15633 		goto err_pci_reg;
15634 	}
15635 
15636 	pci_enable_pcie_error_reporting(pdev);
15637 	pci_set_master(pdev);
15638 
15639 	/* Now that we have a PCI connection, we need to do the
15640 	 * low level device setup.  This is primarily setting up
15641 	 * the Admin Queue structures and then querying for the
15642 	 * device's current profile information.
15643 	 */
15644 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15645 	if (!pf) {
15646 		err = -ENOMEM;
15647 		goto err_pf_alloc;
15648 	}
15649 	pf->next_vsi = 0;
15650 	pf->pdev = pdev;
15651 	set_bit(__I40E_DOWN, pf->state);
15652 
15653 	hw = &pf->hw;
15654 	hw->back = pf;
15655 
15656 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15657 				I40E_MAX_CSR_SPACE);
15658 	/* We believe that the highest register to read is
15659 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15660 	 * is not less than that before mapping to prevent a
15661 	 * kernel panic.
15662 	 */
15663 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15664 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15665 			pf->ioremap_len);
15666 		err = -ENOMEM;
15667 		goto err_ioremap;
15668 	}
15669 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15670 	if (!hw->hw_addr) {
15671 		err = -EIO;
15672 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15673 			 (unsigned int)pci_resource_start(pdev, 0),
15674 			 pf->ioremap_len, err);
15675 		goto err_ioremap;
15676 	}
15677 	hw->vendor_id = pdev->vendor;
15678 	hw->device_id = pdev->device;
15679 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15680 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15681 	i40e_set_subsystem_device_id(hw);
15682 	hw->bus.device = PCI_SLOT(pdev->devfn);
15683 	hw->bus.func = PCI_FUNC(pdev->devfn);
15684 	hw->bus.bus_id = pdev->bus->number;
15685 	pf->instance = pfs_found;
15686 
15687 	/* Select something other than the 802.1ad ethertype for the
15688 	 * switch to use internally and drop on ingress.
15689 	 */
15690 	hw->switch_tag = 0xffff;
15691 	hw->first_tag = ETH_P_8021AD;
15692 	hw->second_tag = ETH_P_8021Q;
15693 
15694 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15695 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15696 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15697 
15698 	/* set up the locks for the AQ, do this only once in probe
15699 	 * and destroy them only once in remove
15700 	 */
15701 	mutex_init(&hw->aq.asq_mutex);
15702 	mutex_init(&hw->aq.arq_mutex);
15703 
15704 	pf->msg_enable = netif_msg_init(debug,
15705 					NETIF_MSG_DRV |
15706 					NETIF_MSG_PROBE |
15707 					NETIF_MSG_LINK);
15708 	if (debug < -1)
15709 		pf->hw.debug_mask = debug;
15710 
15711 	/* do a special CORER for clearing PXE mode once at init */
15712 	if (hw->revision_id == 0 &&
15713 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15714 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15715 		i40e_flush(hw);
15716 		msleep(200);
15717 		pf->corer_count++;
15718 
15719 		i40e_clear_pxe_mode(hw);
15720 	}
15721 
15722 	/* Reset here to make sure all is clean and to define PF 'n' */
15723 	i40e_clear_hw(hw);
15724 
15725 	err = i40e_set_mac_type(hw);
15726 	if (err) {
15727 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15728 			 err);
15729 		goto err_pf_reset;
15730 	}
15731 
15732 	err = i40e_handle_resets(pf);
15733 	if (err)
15734 		goto err_pf_reset;
15735 
15736 	i40e_check_recovery_mode(pf);
15737 
15738 	if (is_kdump_kernel()) {
15739 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15740 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15741 	} else {
15742 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15743 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15744 	}
15745 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15746 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15747 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15748 
15749 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15750 		 "%s-%s:misc",
15751 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15752 
15753 	err = i40e_init_shared_code(hw);
15754 	if (err) {
15755 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15756 			 err);
15757 		goto err_pf_reset;
15758 	}
15759 
15760 	/* set up a default setting for link flow control */
15761 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15762 
15763 	err = i40e_init_adminq(hw);
15764 	if (err) {
15765 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15766 			dev_info(&pdev->dev,
15767 				 "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",
15768 				 hw->aq.api_maj_ver,
15769 				 hw->aq.api_min_ver,
15770 				 I40E_FW_API_VERSION_MAJOR,
15771 				 I40E_FW_MINOR_VERSION(hw));
15772 		else
15773 			dev_info(&pdev->dev,
15774 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15775 
15776 		goto err_pf_reset;
15777 	}
15778 	i40e_get_oem_version(hw);
15779 
15780 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15781 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15782 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15783 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15784 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15785 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15786 
15787 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15788 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15789 		dev_dbg(&pdev->dev,
15790 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15791 			 hw->aq.api_maj_ver,
15792 			 hw->aq.api_min_ver,
15793 			 I40E_FW_API_VERSION_MAJOR,
15794 			 I40E_FW_MINOR_VERSION(hw));
15795 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15796 		dev_info(&pdev->dev,
15797 			 "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",
15798 			 hw->aq.api_maj_ver,
15799 			 hw->aq.api_min_ver,
15800 			 I40E_FW_API_VERSION_MAJOR,
15801 			 I40E_FW_MINOR_VERSION(hw));
15802 
15803 	i40e_verify_eeprom(pf);
15804 
15805 	/* Rev 0 hardware was never productized */
15806 	if (hw->revision_id < 1)
15807 		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");
15808 
15809 	i40e_clear_pxe_mode(hw);
15810 
15811 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15812 	if (err)
15813 		goto err_adminq_setup;
15814 
15815 	err = i40e_sw_init(pf);
15816 	if (err) {
15817 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15818 		goto err_sw_init;
15819 	}
15820 
15821 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15822 		return i40e_init_recovery_mode(pf, hw);
15823 
15824 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15825 				hw->func_caps.num_rx_qp, 0, 0);
15826 	if (err) {
15827 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15828 		goto err_init_lan_hmc;
15829 	}
15830 
15831 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15832 	if (err) {
15833 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15834 		err = -ENOENT;
15835 		goto err_configure_lan_hmc;
15836 	}
15837 
15838 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15839 	 * Ignore error return codes because if it was already disabled via
15840 	 * hardware settings this will fail
15841 	 */
15842 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15843 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15844 		i40e_aq_stop_lldp(hw, true, false, NULL);
15845 	}
15846 
15847 	/* allow a platform config to override the HW addr */
15848 	i40e_get_platform_mac_addr(pdev, pf);
15849 
15850 	if (!is_valid_ether_addr(hw->mac.addr)) {
15851 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15852 		err = -EIO;
15853 		goto err_mac_addr;
15854 	}
15855 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15856 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15857 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15858 	if (is_valid_ether_addr(hw->mac.port_addr))
15859 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15860 
15861 	i40e_ptp_alloc_pins(pf);
15862 	pci_set_drvdata(pdev, pf);
15863 	pci_save_state(pdev);
15864 
15865 #ifdef CONFIG_I40E_DCB
15866 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15867 	(!status &&
15868 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15869 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15870 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15871 	dev_info(&pdev->dev,
15872 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15873 			"FW LLDP is disabled\n" :
15874 			"FW LLDP is enabled\n");
15875 
15876 	/* Enable FW to write default DCB config on link-up */
15877 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15878 
15879 	err = i40e_init_pf_dcb(pf);
15880 	if (err) {
15881 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15882 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15883 		/* Continue without DCB enabled */
15884 	}
15885 #endif /* CONFIG_I40E_DCB */
15886 
15887 	/* set up periodic task facility */
15888 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15889 	pf->service_timer_period = HZ;
15890 
15891 	INIT_WORK(&pf->service_task, i40e_service_task);
15892 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15893 
15894 	/* NVM bit on means WoL disabled for the port */
15895 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15896 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15897 		pf->wol_en = false;
15898 	else
15899 		pf->wol_en = true;
15900 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15901 
15902 	/* set up the main switch operations */
15903 	i40e_determine_queue_usage(pf);
15904 	err = i40e_init_interrupt_scheme(pf);
15905 	if (err)
15906 		goto err_switch_setup;
15907 
15908 	/* Reduce Tx and Rx pairs for kdump
15909 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15910 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15911 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15912 	 */
15913 	if (is_kdump_kernel())
15914 		pf->num_lan_msix = 1;
15915 
15916 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15917 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15918 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15919 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15920 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15921 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15922 						    UDP_TUNNEL_TYPE_GENEVE;
15923 
15924 	/* The number of VSIs reported by the FW is the minimum guaranteed
15925 	 * to us; HW supports far more and we share the remaining pool with
15926 	 * the other PFs. We allocate space for more than the guarantee with
15927 	 * the understanding that we might not get them all later.
15928 	 */
15929 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15930 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15931 	else
15932 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15933 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15934 		dev_warn(&pf->pdev->dev,
15935 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15936 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15937 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15938 	}
15939 
15940 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15941 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15942 			  GFP_KERNEL);
15943 	if (!pf->vsi) {
15944 		err = -ENOMEM;
15945 		goto err_switch_setup;
15946 	}
15947 
15948 #ifdef CONFIG_PCI_IOV
15949 	/* prep for VF support */
15950 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15951 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15952 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15953 		if (pci_num_vf(pdev))
15954 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15955 	}
15956 #endif
15957 	err = i40e_setup_pf_switch(pf, false, false);
15958 	if (err) {
15959 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15960 		goto err_vsis;
15961 	}
15962 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15963 
15964 	/* if FDIR VSI was set up, start it now */
15965 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15966 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15967 			i40e_vsi_open(pf->vsi[i]);
15968 			break;
15969 		}
15970 	}
15971 
15972 	/* The driver only wants link up/down and module qualification
15973 	 * reports from firmware.  Note the negative logic.
15974 	 */
15975 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15976 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15977 					 I40E_AQ_EVENT_MEDIA_NA |
15978 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15979 	if (err)
15980 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15981 			 i40e_stat_str(&pf->hw, err),
15982 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15983 
15984 	/* Reconfigure hardware for allowing smaller MSS in the case
15985 	 * of TSO, so that we avoid the MDD being fired and causing
15986 	 * a reset in the case of small MSS+TSO.
15987 	 */
15988 	val = rd32(hw, I40E_REG_MSS);
15989 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15990 		val &= ~I40E_REG_MSS_MIN_MASK;
15991 		val |= I40E_64BYTE_MSS;
15992 		wr32(hw, I40E_REG_MSS, val);
15993 	}
15994 
15995 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15996 		msleep(75);
15997 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15998 		if (err)
15999 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
16000 				 i40e_stat_str(&pf->hw, err),
16001 				 i40e_aq_str(&pf->hw,
16002 					     pf->hw.aq.asq_last_status));
16003 	}
16004 	/* The main driver is (mostly) up and happy. We need to set this state
16005 	 * before setting up the misc vector or we get a race and the vector
16006 	 * ends up disabled forever.
16007 	 */
16008 	clear_bit(__I40E_DOWN, pf->state);
16009 
16010 	/* In case of MSIX we are going to setup the misc vector right here
16011 	 * to handle admin queue events etc. In case of legacy and MSI
16012 	 * the misc functionality and queue processing is combined in
16013 	 * the same vector and that gets setup at open.
16014 	 */
16015 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
16016 		err = i40e_setup_misc_vector(pf);
16017 		if (err) {
16018 			dev_info(&pdev->dev,
16019 				 "setup of misc vector failed: %d\n", err);
16020 			i40e_cloud_filter_exit(pf);
16021 			i40e_fdir_teardown(pf);
16022 			goto err_vsis;
16023 		}
16024 	}
16025 
16026 #ifdef CONFIG_PCI_IOV
16027 	/* prep for VF support */
16028 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16029 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16030 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16031 		/* disable link interrupts for VFs */
16032 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16033 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16034 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16035 		i40e_flush(hw);
16036 
16037 		if (pci_num_vf(pdev)) {
16038 			dev_info(&pdev->dev,
16039 				 "Active VFs found, allocating resources.\n");
16040 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16041 			if (err)
16042 				dev_info(&pdev->dev,
16043 					 "Error %d allocating resources for existing VFs\n",
16044 					 err);
16045 		}
16046 	}
16047 #endif /* CONFIG_PCI_IOV */
16048 
16049 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16050 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16051 						      pf->num_iwarp_msix,
16052 						      I40E_IWARP_IRQ_PILE_ID);
16053 		if (pf->iwarp_base_vector < 0) {
16054 			dev_info(&pdev->dev,
16055 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16056 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16057 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16058 		}
16059 	}
16060 
16061 	i40e_dbg_pf_init(pf);
16062 
16063 	/* tell the firmware that we're starting */
16064 	i40e_send_version(pf);
16065 
16066 	/* since everything's happy, start the service_task timer */
16067 	mod_timer(&pf->service_timer,
16068 		  round_jiffies(jiffies + pf->service_timer_period));
16069 
16070 	/* add this PF to client device list and launch a client service task */
16071 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16072 		err = i40e_lan_add_device(pf);
16073 		if (err)
16074 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16075 				 err);
16076 	}
16077 
16078 #define PCI_SPEED_SIZE 8
16079 #define PCI_WIDTH_SIZE 8
16080 	/* Devices on the IOSF bus do not have this information
16081 	 * and will report PCI Gen 1 x 1 by default so don't bother
16082 	 * checking them.
16083 	 */
16084 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16085 		char speed[PCI_SPEED_SIZE] = "Unknown";
16086 		char width[PCI_WIDTH_SIZE] = "Unknown";
16087 
16088 		/* Get the negotiated link width and speed from PCI config
16089 		 * space
16090 		 */
16091 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16092 					  &link_status);
16093 
16094 		i40e_set_pci_config_data(hw, link_status);
16095 
16096 		switch (hw->bus.speed) {
16097 		case i40e_bus_speed_8000:
16098 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16099 		case i40e_bus_speed_5000:
16100 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16101 		case i40e_bus_speed_2500:
16102 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16103 		default:
16104 			break;
16105 		}
16106 		switch (hw->bus.width) {
16107 		case i40e_bus_width_pcie_x8:
16108 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16109 		case i40e_bus_width_pcie_x4:
16110 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16111 		case i40e_bus_width_pcie_x2:
16112 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16113 		case i40e_bus_width_pcie_x1:
16114 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16115 		default:
16116 			break;
16117 		}
16118 
16119 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16120 			 speed, width);
16121 
16122 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16123 		    hw->bus.speed < i40e_bus_speed_8000) {
16124 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16125 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16126 		}
16127 	}
16128 
16129 	/* get the requested speeds from the fw */
16130 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16131 	if (err)
16132 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
16133 			i40e_stat_str(&pf->hw, err),
16134 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16135 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16136 
16137 	/* set the FEC config due to the board capabilities */
16138 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16139 
16140 	/* get the supported phy types from the fw */
16141 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16142 	if (err)
16143 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
16144 			i40e_stat_str(&pf->hw, err),
16145 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16146 
16147 	/* make sure the MFS hasn't been set lower than the default */
16148 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16149 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16150 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16151 	if (val < MAX_FRAME_SIZE_DEFAULT)
16152 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16153 			 i, val);
16154 
16155 	/* Add a filter to drop all Flow control frames from any VSI from being
16156 	 * transmitted. By doing so we stop a malicious VF from sending out
16157 	 * PAUSE or PFC frames and potentially controlling traffic for other
16158 	 * PF/VF VSIs.
16159 	 * The FW can still send Flow control frames if enabled.
16160 	 */
16161 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16162 						       pf->main_vsi_seid);
16163 
16164 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16165 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16166 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16167 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16168 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16169 	/* print a string summarizing features */
16170 	i40e_print_features(pf);
16171 
16172 	return 0;
16173 
16174 	/* Unwind what we've done if something failed in the setup */
16175 err_vsis:
16176 	set_bit(__I40E_DOWN, pf->state);
16177 	i40e_clear_interrupt_scheme(pf);
16178 	kfree(pf->vsi);
16179 err_switch_setup:
16180 	i40e_reset_interrupt_capability(pf);
16181 	del_timer_sync(&pf->service_timer);
16182 err_mac_addr:
16183 err_configure_lan_hmc:
16184 	(void)i40e_shutdown_lan_hmc(hw);
16185 err_init_lan_hmc:
16186 	kfree(pf->qp_pile);
16187 err_sw_init:
16188 err_adminq_setup:
16189 err_pf_reset:
16190 	iounmap(hw->hw_addr);
16191 err_ioremap:
16192 	kfree(pf);
16193 err_pf_alloc:
16194 	pci_disable_pcie_error_reporting(pdev);
16195 	pci_release_mem_regions(pdev);
16196 err_pci_reg:
16197 err_dma:
16198 	pci_disable_device(pdev);
16199 	return err;
16200 }
16201 
16202 /**
16203  * i40e_remove - Device removal routine
16204  * @pdev: PCI device information struct
16205  *
16206  * i40e_remove is called by the PCI subsystem to alert the driver
16207  * that is should release a PCI device.  This could be caused by a
16208  * Hot-Plug event, or because the driver is going to be removed from
16209  * memory.
16210  **/
i40e_remove(struct pci_dev * pdev)16211 static void i40e_remove(struct pci_dev *pdev)
16212 {
16213 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16214 	struct i40e_hw *hw = &pf->hw;
16215 	i40e_status ret_code;
16216 	int i;
16217 
16218 	i40e_dbg_pf_exit(pf);
16219 
16220 	i40e_ptp_stop(pf);
16221 
16222 	/* Disable RSS in hw */
16223 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16224 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16225 
16226 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16227 	 * flags, once they are set, i40e_rebuild should not be called as
16228 	 * i40e_prep_for_reset always returns early.
16229 	 */
16230 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16231 		usleep_range(1000, 2000);
16232 	set_bit(__I40E_IN_REMOVE, pf->state);
16233 
16234 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16235 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16236 		i40e_free_vfs(pf);
16237 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16238 	}
16239 	/* no more scheduling of any task */
16240 	set_bit(__I40E_SUSPENDED, pf->state);
16241 	set_bit(__I40E_DOWN, pf->state);
16242 	if (pf->service_timer.function)
16243 		del_timer_sync(&pf->service_timer);
16244 	if (pf->service_task.func)
16245 		cancel_work_sync(&pf->service_task);
16246 
16247 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16248 		struct i40e_vsi *vsi = pf->vsi[0];
16249 
16250 		/* We know that we have allocated only one vsi for this PF,
16251 		 * it was just for registering netdevice, so the interface
16252 		 * could be visible in the 'ifconfig' output
16253 		 */
16254 		unregister_netdev(vsi->netdev);
16255 		free_netdev(vsi->netdev);
16256 
16257 		goto unmap;
16258 	}
16259 
16260 	/* Client close must be called explicitly here because the timer
16261 	 * has been stopped.
16262 	 */
16263 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16264 
16265 	i40e_fdir_teardown(pf);
16266 
16267 	/* If there is a switch structure or any orphans, remove them.
16268 	 * This will leave only the PF's VSI remaining.
16269 	 */
16270 	for (i = 0; i < I40E_MAX_VEB; i++) {
16271 		if (!pf->veb[i])
16272 			continue;
16273 
16274 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16275 		    pf->veb[i]->uplink_seid == 0)
16276 			i40e_switch_branch_release(pf->veb[i]);
16277 	}
16278 
16279 	/* Now we can shutdown the PF's VSI, just before we kill
16280 	 * adminq and hmc.
16281 	 */
16282 	if (pf->vsi[pf->lan_vsi])
16283 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16284 
16285 	i40e_cloud_filter_exit(pf);
16286 
16287 	/* remove attached clients */
16288 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16289 		ret_code = i40e_lan_del_device(pf);
16290 		if (ret_code)
16291 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16292 				 ret_code);
16293 	}
16294 
16295 	/* shutdown and destroy the HMC */
16296 	if (hw->hmc.hmc_obj) {
16297 		ret_code = i40e_shutdown_lan_hmc(hw);
16298 		if (ret_code)
16299 			dev_warn(&pdev->dev,
16300 				 "Failed to destroy the HMC resources: %d\n",
16301 				 ret_code);
16302 	}
16303 
16304 unmap:
16305 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16306 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16307 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16308 		free_irq(pf->pdev->irq, pf);
16309 
16310 	/* shutdown the adminq */
16311 	i40e_shutdown_adminq(hw);
16312 
16313 	/* destroy the locks only once, here */
16314 	mutex_destroy(&hw->aq.arq_mutex);
16315 	mutex_destroy(&hw->aq.asq_mutex);
16316 
16317 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16318 	rtnl_lock();
16319 	i40e_clear_interrupt_scheme(pf);
16320 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16321 		if (pf->vsi[i]) {
16322 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16323 				i40e_vsi_clear_rings(pf->vsi[i]);
16324 			i40e_vsi_clear(pf->vsi[i]);
16325 			pf->vsi[i] = NULL;
16326 		}
16327 	}
16328 	rtnl_unlock();
16329 
16330 	for (i = 0; i < I40E_MAX_VEB; i++) {
16331 		kfree(pf->veb[i]);
16332 		pf->veb[i] = NULL;
16333 	}
16334 
16335 	kfree(pf->qp_pile);
16336 	kfree(pf->vsi);
16337 
16338 	iounmap(hw->hw_addr);
16339 	kfree(pf);
16340 	pci_release_mem_regions(pdev);
16341 
16342 	pci_disable_pcie_error_reporting(pdev);
16343 	pci_disable_device(pdev);
16344 }
16345 
16346 /**
16347  * i40e_pci_error_detected - warning that something funky happened in PCI land
16348  * @pdev: PCI device information struct
16349  * @error: the type of PCI error
16350  *
16351  * Called to warn that something happened and the error handling steps
16352  * are in progress.  Allows the driver to quiesce things, be ready for
16353  * remediation.
16354  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)16355 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16356 						pci_channel_state_t error)
16357 {
16358 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16359 
16360 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16361 
16362 	if (!pf) {
16363 		dev_info(&pdev->dev,
16364 			 "Cannot recover - error happened during device probe\n");
16365 		return PCI_ERS_RESULT_DISCONNECT;
16366 	}
16367 
16368 	/* shutdown all operations */
16369 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16370 		i40e_prep_for_reset(pf);
16371 
16372 	/* Request a slot reset */
16373 	return PCI_ERS_RESULT_NEED_RESET;
16374 }
16375 
16376 /**
16377  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16378  * @pdev: PCI device information struct
16379  *
16380  * Called to find if the driver can work with the device now that
16381  * the pci slot has been reset.  If a basic connection seems good
16382  * (registers are readable and have sane content) then return a
16383  * happy little PCI_ERS_RESULT_xxx.
16384  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)16385 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16386 {
16387 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16388 	pci_ers_result_t result;
16389 	u32 reg;
16390 
16391 	dev_dbg(&pdev->dev, "%s\n", __func__);
16392 	if (pci_enable_device_mem(pdev)) {
16393 		dev_info(&pdev->dev,
16394 			 "Cannot re-enable PCI device after reset.\n");
16395 		result = PCI_ERS_RESULT_DISCONNECT;
16396 	} else {
16397 		pci_set_master(pdev);
16398 		pci_restore_state(pdev);
16399 		pci_save_state(pdev);
16400 		pci_wake_from_d3(pdev, false);
16401 
16402 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16403 		if (reg == 0)
16404 			result = PCI_ERS_RESULT_RECOVERED;
16405 		else
16406 			result = PCI_ERS_RESULT_DISCONNECT;
16407 	}
16408 
16409 	return result;
16410 }
16411 
16412 /**
16413  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16414  * @pdev: PCI device information struct
16415  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)16416 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16417 {
16418 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16419 
16420 	i40e_prep_for_reset(pf);
16421 }
16422 
16423 /**
16424  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16425  * @pdev: PCI device information struct
16426  */
i40e_pci_error_reset_done(struct pci_dev * pdev)16427 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16428 {
16429 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16430 
16431 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16432 		return;
16433 
16434 	i40e_reset_and_rebuild(pf, false, false);
16435 }
16436 
16437 /**
16438  * i40e_pci_error_resume - restart operations after PCI error recovery
16439  * @pdev: PCI device information struct
16440  *
16441  * Called to allow the driver to bring things back up after PCI error
16442  * and/or reset recovery has finished.
16443  **/
i40e_pci_error_resume(struct pci_dev * pdev)16444 static void i40e_pci_error_resume(struct pci_dev *pdev)
16445 {
16446 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16447 
16448 	dev_dbg(&pdev->dev, "%s\n", __func__);
16449 	if (test_bit(__I40E_SUSPENDED, pf->state))
16450 		return;
16451 
16452 	i40e_handle_reset_warning(pf, false);
16453 }
16454 
16455 /**
16456  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16457  * using the mac_address_write admin q function
16458  * @pf: pointer to i40e_pf struct
16459  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)16460 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16461 {
16462 	struct i40e_hw *hw = &pf->hw;
16463 	i40e_status ret;
16464 	u8 mac_addr[6];
16465 	u16 flags = 0;
16466 
16467 	/* Get current MAC address in case it's an LAA */
16468 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16469 		ether_addr_copy(mac_addr,
16470 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16471 	} else {
16472 		dev_err(&pf->pdev->dev,
16473 			"Failed to retrieve MAC address; using default\n");
16474 		ether_addr_copy(mac_addr, hw->mac.addr);
16475 	}
16476 
16477 	/* The FW expects the mac address write cmd to first be called with
16478 	 * one of these flags before calling it again with the multicast
16479 	 * enable flags.
16480 	 */
16481 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16482 
16483 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16484 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16485 
16486 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16487 	if (ret) {
16488 		dev_err(&pf->pdev->dev,
16489 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16490 		return;
16491 	}
16492 
16493 	flags = I40E_AQC_MC_MAG_EN
16494 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16495 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16496 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16497 	if (ret)
16498 		dev_err(&pf->pdev->dev,
16499 			"Failed to enable Multicast Magic Packet wake up\n");
16500 }
16501 
16502 /**
16503  * i40e_shutdown - PCI callback for shutting down
16504  * @pdev: PCI device information struct
16505  **/
i40e_shutdown(struct pci_dev * pdev)16506 static void i40e_shutdown(struct pci_dev *pdev)
16507 {
16508 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16509 	struct i40e_hw *hw = &pf->hw;
16510 
16511 	set_bit(__I40E_SUSPENDED, pf->state);
16512 	set_bit(__I40E_DOWN, pf->state);
16513 
16514 	del_timer_sync(&pf->service_timer);
16515 	cancel_work_sync(&pf->service_task);
16516 	i40e_cloud_filter_exit(pf);
16517 	i40e_fdir_teardown(pf);
16518 
16519 	/* Client close must be called explicitly here because the timer
16520 	 * has been stopped.
16521 	 */
16522 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16523 
16524 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16525 		i40e_enable_mc_magic_wake(pf);
16526 
16527 	i40e_prep_for_reset(pf);
16528 
16529 	wr32(hw, I40E_PFPM_APM,
16530 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16531 	wr32(hw, I40E_PFPM_WUFC,
16532 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16533 
16534 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16535 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16536 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16537 		free_irq(pf->pdev->irq, pf);
16538 
16539 	/* Since we're going to destroy queues during the
16540 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16541 	 * whole section
16542 	 */
16543 	rtnl_lock();
16544 	i40e_clear_interrupt_scheme(pf);
16545 	rtnl_unlock();
16546 
16547 	if (system_state == SYSTEM_POWER_OFF) {
16548 		pci_wake_from_d3(pdev, pf->wol_en);
16549 		pci_set_power_state(pdev, PCI_D3hot);
16550 	}
16551 }
16552 
16553 /**
16554  * i40e_suspend - PM callback for moving to D3
16555  * @dev: generic device information structure
16556  **/
i40e_suspend(struct device * dev)16557 static int __maybe_unused i40e_suspend(struct device *dev)
16558 {
16559 	struct i40e_pf *pf = dev_get_drvdata(dev);
16560 	struct i40e_hw *hw = &pf->hw;
16561 
16562 	/* If we're already suspended, then there is nothing to do */
16563 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16564 		return 0;
16565 
16566 	set_bit(__I40E_DOWN, pf->state);
16567 
16568 	/* Ensure service task will not be running */
16569 	del_timer_sync(&pf->service_timer);
16570 	cancel_work_sync(&pf->service_task);
16571 
16572 	/* Client close must be called explicitly here because the timer
16573 	 * has been stopped.
16574 	 */
16575 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16576 
16577 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16578 		i40e_enable_mc_magic_wake(pf);
16579 
16580 	/* Since we're going to destroy queues during the
16581 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16582 	 * whole section
16583 	 */
16584 	rtnl_lock();
16585 
16586 	i40e_prep_for_reset(pf);
16587 
16588 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16589 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16590 
16591 	/* Clear the interrupt scheme and release our IRQs so that the system
16592 	 * can safely hibernate even when there are a large number of CPUs.
16593 	 * Otherwise hibernation might fail when mapping all the vectors back
16594 	 * to CPU0.
16595 	 */
16596 	i40e_clear_interrupt_scheme(pf);
16597 
16598 	rtnl_unlock();
16599 
16600 	return 0;
16601 }
16602 
16603 /**
16604  * i40e_resume - PM callback for waking up from D3
16605  * @dev: generic device information structure
16606  **/
i40e_resume(struct device * dev)16607 static int __maybe_unused i40e_resume(struct device *dev)
16608 {
16609 	struct i40e_pf *pf = dev_get_drvdata(dev);
16610 	int err;
16611 
16612 	/* If we're not suspended, then there is nothing to do */
16613 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16614 		return 0;
16615 
16616 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16617 	 * since we're going to be restoring queues
16618 	 */
16619 	rtnl_lock();
16620 
16621 	/* We cleared the interrupt scheme when we suspended, so we need to
16622 	 * restore it now to resume device functionality.
16623 	 */
16624 	err = i40e_restore_interrupt_scheme(pf);
16625 	if (err) {
16626 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16627 			err);
16628 	}
16629 
16630 	clear_bit(__I40E_DOWN, pf->state);
16631 	i40e_reset_and_rebuild(pf, false, true);
16632 
16633 	rtnl_unlock();
16634 
16635 	/* Clear suspended state last after everything is recovered */
16636 	clear_bit(__I40E_SUSPENDED, pf->state);
16637 
16638 	/* Restart the service task */
16639 	mod_timer(&pf->service_timer,
16640 		  round_jiffies(jiffies + pf->service_timer_period));
16641 
16642 	return 0;
16643 }
16644 
16645 static const struct pci_error_handlers i40e_err_handler = {
16646 	.error_detected = i40e_pci_error_detected,
16647 	.slot_reset = i40e_pci_error_slot_reset,
16648 	.reset_prepare = i40e_pci_error_reset_prepare,
16649 	.reset_done = i40e_pci_error_reset_done,
16650 	.resume = i40e_pci_error_resume,
16651 };
16652 
16653 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16654 
16655 static struct pci_driver i40e_driver = {
16656 	.name     = i40e_driver_name,
16657 	.id_table = i40e_pci_tbl,
16658 	.probe    = i40e_probe,
16659 	.remove   = i40e_remove,
16660 	.driver   = {
16661 		.pm = &i40e_pm_ops,
16662 	},
16663 	.shutdown = i40e_shutdown,
16664 	.err_handler = &i40e_err_handler,
16665 	.sriov_configure = i40e_pci_sriov_configure,
16666 };
16667 
16668 /**
16669  * i40e_init_module - Driver registration routine
16670  *
16671  * i40e_init_module is the first routine called when the driver is
16672  * loaded. All it does is register with the PCI subsystem.
16673  **/
i40e_init_module(void)16674 static int __init i40e_init_module(void)
16675 {
16676 	int err;
16677 
16678 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16679 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16680 
16681 	/* There is no need to throttle the number of active tasks because
16682 	 * each device limits its own task using a state bit for scheduling
16683 	 * the service task, and the device tasks do not interfere with each
16684 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16685 	 * since we need to be able to guarantee forward progress even under
16686 	 * memory pressure.
16687 	 */
16688 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16689 	if (!i40e_wq) {
16690 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16691 		return -ENOMEM;
16692 	}
16693 
16694 	i40e_dbg_init();
16695 	err = pci_register_driver(&i40e_driver);
16696 	if (err) {
16697 		destroy_workqueue(i40e_wq);
16698 		i40e_dbg_exit();
16699 		return err;
16700 	}
16701 
16702 	return 0;
16703 }
16704 module_init(i40e_init_module);
16705 
16706 /**
16707  * i40e_exit_module - Driver exit cleanup routine
16708  *
16709  * i40e_exit_module is called just before the driver is removed
16710  * from memory.
16711  **/
i40e_exit_module(void)16712 static void __exit i40e_exit_module(void)
16713 {
16714 	pci_unregister_driver(&i40e_driver);
16715 	destroy_workqueue(i40e_wq);
16716 	ida_destroy(&i40e_client_ida);
16717 	i40e_dbg_exit();
16718 }
16719 module_exit(i40e_exit_module);
16720