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_vsi_configure_rx - Configure the VSI for Rx
3698  * @vsi: the VSI being configured
3699  *
3700  * Configure the Rx VSI for operation.
3701  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3702 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3703 {
3704 	int err = 0;
3705 	u16 i;
3706 
3707 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3708 		vsi->max_frame = I40E_MAX_RXBUFFER;
3709 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3710 #if (PAGE_SIZE < 8192)
3711 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3712 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3713 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3714 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3715 #endif
3716 	} else {
3717 		vsi->max_frame = I40E_MAX_RXBUFFER;
3718 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3719 						       I40E_RXBUFFER_2048;
3720 	}
3721 
3722 	/* set up individual rings */
3723 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3724 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3725 
3726 	return err;
3727 }
3728 
3729 /**
3730  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3731  * @vsi: ptr to the VSI
3732  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3733 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3734 {
3735 	struct i40e_ring *tx_ring, *rx_ring;
3736 	u16 qoffset, qcount;
3737 	int i, n;
3738 
3739 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3740 		/* Reset the TC information */
3741 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3742 			rx_ring = vsi->rx_rings[i];
3743 			tx_ring = vsi->tx_rings[i];
3744 			rx_ring->dcb_tc = 0;
3745 			tx_ring->dcb_tc = 0;
3746 		}
3747 		return;
3748 	}
3749 
3750 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3751 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3752 			continue;
3753 
3754 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3755 		qcount = vsi->tc_config.tc_info[n].qcount;
3756 		for (i = qoffset; i < (qoffset + qcount); i++) {
3757 			rx_ring = vsi->rx_rings[i];
3758 			tx_ring = vsi->tx_rings[i];
3759 			rx_ring->dcb_tc = n;
3760 			tx_ring->dcb_tc = n;
3761 		}
3762 	}
3763 }
3764 
3765 /**
3766  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3767  * @vsi: ptr to the VSI
3768  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3769 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3770 {
3771 	if (vsi->netdev)
3772 		i40e_set_rx_mode(vsi->netdev);
3773 }
3774 
3775 /**
3776  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3777  * @pf: Pointer to the targeted PF
3778  *
3779  * Set all flow director counters to 0.
3780  */
i40e_reset_fdir_filter_cnt(struct i40e_pf * pf)3781 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3782 {
3783 	pf->fd_tcp4_filter_cnt = 0;
3784 	pf->fd_udp4_filter_cnt = 0;
3785 	pf->fd_sctp4_filter_cnt = 0;
3786 	pf->fd_ip4_filter_cnt = 0;
3787 	pf->fd_tcp6_filter_cnt = 0;
3788 	pf->fd_udp6_filter_cnt = 0;
3789 	pf->fd_sctp6_filter_cnt = 0;
3790 	pf->fd_ip6_filter_cnt = 0;
3791 }
3792 
3793 /**
3794  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3795  * @vsi: Pointer to the targeted VSI
3796  *
3797  * This function replays the hlist on the hw where all the SB Flow Director
3798  * filters were saved.
3799  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3800 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3801 {
3802 	struct i40e_fdir_filter *filter;
3803 	struct i40e_pf *pf = vsi->back;
3804 	struct hlist_node *node;
3805 
3806 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3807 		return;
3808 
3809 	/* Reset FDir counters as we're replaying all existing filters */
3810 	i40e_reset_fdir_filter_cnt(pf);
3811 
3812 	hlist_for_each_entry_safe(filter, node,
3813 				  &pf->fdir_filter_list, fdir_node) {
3814 		i40e_add_del_fdir(vsi, filter, true);
3815 	}
3816 }
3817 
3818 /**
3819  * i40e_vsi_configure - Set up the VSI for action
3820  * @vsi: the VSI being configured
3821  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3822 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3823 {
3824 	int err;
3825 
3826 	i40e_set_vsi_rx_mode(vsi);
3827 	i40e_restore_vlan(vsi);
3828 	i40e_vsi_config_dcb_rings(vsi);
3829 	err = i40e_vsi_configure_tx(vsi);
3830 	if (!err)
3831 		err = i40e_vsi_configure_rx(vsi);
3832 
3833 	return err;
3834 }
3835 
3836 /**
3837  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3838  * @vsi: the VSI being configured
3839  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3840 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3841 {
3842 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3843 	struct i40e_pf *pf = vsi->back;
3844 	struct i40e_hw *hw = &pf->hw;
3845 	u16 vector;
3846 	int i, q;
3847 	u32 qp;
3848 
3849 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3850 	 * and PFINT_LNKLSTn registers, e.g.:
3851 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3852 	 */
3853 	qp = vsi->base_queue;
3854 	vector = vsi->base_vector;
3855 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3856 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3857 
3858 		q_vector->rx.next_update = jiffies + 1;
3859 		q_vector->rx.target_itr =
3860 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3861 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3862 		     q_vector->rx.target_itr >> 1);
3863 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3864 
3865 		q_vector->tx.next_update = jiffies + 1;
3866 		q_vector->tx.target_itr =
3867 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3868 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3869 		     q_vector->tx.target_itr >> 1);
3870 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3871 
3872 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3873 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3874 
3875 		/* begin of linked list for RX queue assigned to this vector */
3876 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3877 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3878 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3879 			u32 val;
3880 
3881 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3882 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3883 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3884 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3885 			      (I40E_QUEUE_TYPE_TX <<
3886 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3887 
3888 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3889 
3890 			if (has_xdp) {
3891 				/* TX queue with next queue set to TX */
3892 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3893 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3894 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3895 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3896 				      (I40E_QUEUE_TYPE_TX <<
3897 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3898 
3899 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3900 			}
3901 			/* TX queue with next RX or end of linked list */
3902 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3903 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3904 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3905 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3906 			      (I40E_QUEUE_TYPE_RX <<
3907 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3908 
3909 			/* Terminate the linked list */
3910 			if (q == (q_vector->num_ringpairs - 1))
3911 				val |= (I40E_QUEUE_END_OF_LIST <<
3912 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3913 
3914 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3915 			qp++;
3916 		}
3917 	}
3918 
3919 	i40e_flush(hw);
3920 }
3921 
3922 /**
3923  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3924  * @pf: pointer to private device data structure
3925  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3926 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3927 {
3928 	struct i40e_hw *hw = &pf->hw;
3929 	u32 val;
3930 
3931 	/* clear things first */
3932 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3933 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3934 
3935 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3936 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3937 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3938 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3939 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3940 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3941 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3942 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3943 
3944 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3945 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3946 
3947 	if (pf->flags & I40E_FLAG_PTP)
3948 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3949 
3950 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3951 
3952 	/* SW_ITR_IDX = 0, but don't change INTENA */
3953 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3954 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3955 
3956 	/* OTHER_ITR_IDX = 0 */
3957 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3958 }
3959 
3960 /**
3961  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3962  * @vsi: the VSI being configured
3963  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)3964 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3965 {
3966 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3967 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3968 	struct i40e_pf *pf = vsi->back;
3969 	struct i40e_hw *hw = &pf->hw;
3970 
3971 	/* set the ITR configuration */
3972 	q_vector->rx.next_update = jiffies + 1;
3973 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3974 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3975 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3976 	q_vector->tx.next_update = jiffies + 1;
3977 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3978 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3979 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3980 
3981 	i40e_enable_misc_int_causes(pf);
3982 
3983 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3984 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3985 
3986 	/* Associate the queue pair to the vector and enable the queue
3987 	 * interrupt RX queue in linked list with next queue set to TX
3988 	 */
3989 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
3990 
3991 	if (i40e_enabled_xdp_vsi(vsi)) {
3992 		/* TX queue in linked list with next queue set to TX */
3993 		wr32(hw, I40E_QINT_TQCTL(nextqp),
3994 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
3995 	}
3996 
3997 	/* last TX queue so the next RX queue doesn't matter */
3998 	wr32(hw, I40E_QINT_TQCTL(0),
3999 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4000 	i40e_flush(hw);
4001 }
4002 
4003 /**
4004  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4005  * @pf: board private structure
4006  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)4007 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4008 {
4009 	struct i40e_hw *hw = &pf->hw;
4010 
4011 	wr32(hw, I40E_PFINT_DYN_CTL0,
4012 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4013 	i40e_flush(hw);
4014 }
4015 
4016 /**
4017  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4018  * @pf: board private structure
4019  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)4020 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4021 {
4022 	struct i40e_hw *hw = &pf->hw;
4023 	u32 val;
4024 
4025 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4026 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4027 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4028 
4029 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4030 	i40e_flush(hw);
4031 }
4032 
4033 /**
4034  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4035  * @irq: interrupt number
4036  * @data: pointer to a q_vector
4037  **/
i40e_msix_clean_rings(int irq,void * data)4038 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4039 {
4040 	struct i40e_q_vector *q_vector = data;
4041 
4042 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4043 		return IRQ_HANDLED;
4044 
4045 	napi_schedule_irqoff(&q_vector->napi);
4046 
4047 	return IRQ_HANDLED;
4048 }
4049 
4050 /**
4051  * i40e_irq_affinity_notify - Callback for affinity changes
4052  * @notify: context as to what irq was changed
4053  * @mask: the new affinity mask
4054  *
4055  * This is a callback function used by the irq_set_affinity_notifier function
4056  * so that we may register to receive changes to the irq affinity masks.
4057  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)4058 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4059 				     const cpumask_t *mask)
4060 {
4061 	struct i40e_q_vector *q_vector =
4062 		container_of(notify, struct i40e_q_vector, affinity_notify);
4063 
4064 	cpumask_copy(&q_vector->affinity_mask, mask);
4065 }
4066 
4067 /**
4068  * i40e_irq_affinity_release - Callback for affinity notifier release
4069  * @ref: internal core kernel usage
4070  *
4071  * This is a callback function used by the irq_set_affinity_notifier function
4072  * to inform the current notification subscriber that they will no longer
4073  * receive notifications.
4074  **/
i40e_irq_affinity_release(struct kref * ref)4075 static void i40e_irq_affinity_release(struct kref *ref) {}
4076 
4077 /**
4078  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4079  * @vsi: the VSI being configured
4080  * @basename: name for the vector
4081  *
4082  * Allocates MSI-X vectors and requests interrupts from the kernel.
4083  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)4084 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4085 {
4086 	int q_vectors = vsi->num_q_vectors;
4087 	struct i40e_pf *pf = vsi->back;
4088 	int base = vsi->base_vector;
4089 	int rx_int_idx = 0;
4090 	int tx_int_idx = 0;
4091 	int vector, err;
4092 	int irq_num;
4093 	int cpu;
4094 
4095 	for (vector = 0; vector < q_vectors; vector++) {
4096 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4097 
4098 		irq_num = pf->msix_entries[base + vector].vector;
4099 
4100 		if (q_vector->tx.ring && q_vector->rx.ring) {
4101 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4102 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4103 			tx_int_idx++;
4104 		} else if (q_vector->rx.ring) {
4105 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4106 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4107 		} else if (q_vector->tx.ring) {
4108 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4109 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4110 		} else {
4111 			/* skip this unused q_vector */
4112 			continue;
4113 		}
4114 		err = request_irq(irq_num,
4115 				  vsi->irq_handler,
4116 				  0,
4117 				  q_vector->name,
4118 				  q_vector);
4119 		if (err) {
4120 			dev_info(&pf->pdev->dev,
4121 				 "MSIX request_irq failed, error: %d\n", err);
4122 			goto free_queue_irqs;
4123 		}
4124 
4125 		/* register for affinity change notifications */
4126 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4127 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4128 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4129 		/* Spread affinity hints out across online CPUs.
4130 		 *
4131 		 * get_cpu_mask returns a static constant mask with
4132 		 * a permanent lifetime so it's ok to pass to
4133 		 * irq_update_affinity_hint without making a copy.
4134 		 */
4135 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4136 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4137 	}
4138 
4139 	vsi->irqs_ready = true;
4140 	return 0;
4141 
4142 free_queue_irqs:
4143 	while (vector) {
4144 		vector--;
4145 		irq_num = pf->msix_entries[base + vector].vector;
4146 		irq_set_affinity_notifier(irq_num, NULL);
4147 		irq_update_affinity_hint(irq_num, NULL);
4148 		free_irq(irq_num, &vsi->q_vectors[vector]);
4149 	}
4150 	return err;
4151 }
4152 
4153 /**
4154  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4155  * @vsi: the VSI being un-configured
4156  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)4157 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4158 {
4159 	struct i40e_pf *pf = vsi->back;
4160 	struct i40e_hw *hw = &pf->hw;
4161 	int base = vsi->base_vector;
4162 	int i;
4163 
4164 	/* disable interrupt causation from each queue */
4165 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4166 		u32 val;
4167 
4168 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4169 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4170 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4171 
4172 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4173 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4174 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4175 
4176 		if (!i40e_enabled_xdp_vsi(vsi))
4177 			continue;
4178 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4179 	}
4180 
4181 	/* disable each interrupt */
4182 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4183 		for (i = vsi->base_vector;
4184 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4185 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4186 
4187 		i40e_flush(hw);
4188 		for (i = 0; i < vsi->num_q_vectors; i++)
4189 			synchronize_irq(pf->msix_entries[i + base].vector);
4190 	} else {
4191 		/* Legacy and MSI mode - this stops all interrupt handling */
4192 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4193 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4194 		i40e_flush(hw);
4195 		synchronize_irq(pf->pdev->irq);
4196 	}
4197 }
4198 
4199 /**
4200  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4201  * @vsi: the VSI being configured
4202  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)4203 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4204 {
4205 	struct i40e_pf *pf = vsi->back;
4206 	int i;
4207 
4208 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4209 		for (i = 0; i < vsi->num_q_vectors; i++)
4210 			i40e_irq_dynamic_enable(vsi, i);
4211 	} else {
4212 		i40e_irq_dynamic_enable_icr0(pf);
4213 	}
4214 
4215 	i40e_flush(&pf->hw);
4216 	return 0;
4217 }
4218 
4219 /**
4220  * i40e_free_misc_vector - Free the vector that handles non-queue events
4221  * @pf: board private structure
4222  **/
i40e_free_misc_vector(struct i40e_pf * pf)4223 static void i40e_free_misc_vector(struct i40e_pf *pf)
4224 {
4225 	/* Disable ICR 0 */
4226 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4227 	i40e_flush(&pf->hw);
4228 
4229 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4230 		free_irq(pf->msix_entries[0].vector, pf);
4231 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4232 	}
4233 }
4234 
4235 /**
4236  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4237  * @irq: interrupt number
4238  * @data: pointer to a q_vector
4239  *
4240  * This is the handler used for all MSI/Legacy interrupts, and deals
4241  * with both queue and non-queue interrupts.  This is also used in
4242  * MSIX mode to handle the non-queue interrupts.
4243  **/
i40e_intr(int irq,void * data)4244 static irqreturn_t i40e_intr(int irq, void *data)
4245 {
4246 	struct i40e_pf *pf = (struct i40e_pf *)data;
4247 	struct i40e_hw *hw = &pf->hw;
4248 	irqreturn_t ret = IRQ_NONE;
4249 	u32 icr0, icr0_remaining;
4250 	u32 val, ena_mask;
4251 
4252 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4253 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4254 
4255 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4256 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4257 		goto enable_intr;
4258 
4259 	/* if interrupt but no bits showing, must be SWINT */
4260 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4261 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4262 		pf->sw_int_count++;
4263 
4264 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4265 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4266 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4267 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4268 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4269 	}
4270 
4271 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4272 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4273 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4274 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4275 
4276 		/* We do not have a way to disarm Queue causes while leaving
4277 		 * interrupt enabled for all other causes, ideally
4278 		 * interrupt should be disabled while we are in NAPI but
4279 		 * this is not a performance path and napi_schedule()
4280 		 * can deal with rescheduling.
4281 		 */
4282 		if (!test_bit(__I40E_DOWN, pf->state))
4283 			napi_schedule_irqoff(&q_vector->napi);
4284 	}
4285 
4286 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4287 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4288 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4289 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4290 	}
4291 
4292 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4293 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4294 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4295 	}
4296 
4297 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4298 		/* disable any further VFLR event notifications */
4299 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4300 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4301 
4302 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4303 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4304 		} else {
4305 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4306 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4307 		}
4308 	}
4309 
4310 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4311 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4312 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4313 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4314 		val = rd32(hw, I40E_GLGEN_RSTAT);
4315 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4316 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4317 		if (val == I40E_RESET_CORER) {
4318 			pf->corer_count++;
4319 		} else if (val == I40E_RESET_GLOBR) {
4320 			pf->globr_count++;
4321 		} else if (val == I40E_RESET_EMPR) {
4322 			pf->empr_count++;
4323 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4324 		}
4325 	}
4326 
4327 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4328 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4329 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4330 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4331 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4332 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4333 	}
4334 
4335 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4336 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4337 
4338 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4339 			schedule_work(&pf->ptp_extts0_work);
4340 
4341 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4342 			i40e_ptp_tx_hwtstamp(pf);
4343 
4344 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4345 	}
4346 
4347 	/* If a critical error is pending we have no choice but to reset the
4348 	 * device.
4349 	 * Report and mask out any remaining unexpected interrupts.
4350 	 */
4351 	icr0_remaining = icr0 & ena_mask;
4352 	if (icr0_remaining) {
4353 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4354 			 icr0_remaining);
4355 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4356 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4357 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4358 			dev_info(&pf->pdev->dev, "device will be reset\n");
4359 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4360 			i40e_service_event_schedule(pf);
4361 		}
4362 		ena_mask &= ~icr0_remaining;
4363 	}
4364 	ret = IRQ_HANDLED;
4365 
4366 enable_intr:
4367 	/* re-enable interrupt causes */
4368 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4369 	if (!test_bit(__I40E_DOWN, pf->state) ||
4370 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4371 		i40e_service_event_schedule(pf);
4372 		i40e_irq_dynamic_enable_icr0(pf);
4373 	}
4374 
4375 	return ret;
4376 }
4377 
4378 /**
4379  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4380  * @tx_ring:  tx ring to clean
4381  * @budget:   how many cleans we're allowed
4382  *
4383  * Returns true if there's any budget left (e.g. the clean is finished)
4384  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4385 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4386 {
4387 	struct i40e_vsi *vsi = tx_ring->vsi;
4388 	u16 i = tx_ring->next_to_clean;
4389 	struct i40e_tx_buffer *tx_buf;
4390 	struct i40e_tx_desc *tx_desc;
4391 
4392 	tx_buf = &tx_ring->tx_bi[i];
4393 	tx_desc = I40E_TX_DESC(tx_ring, i);
4394 	i -= tx_ring->count;
4395 
4396 	do {
4397 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4398 
4399 		/* if next_to_watch is not set then there is no work pending */
4400 		if (!eop_desc)
4401 			break;
4402 
4403 		/* prevent any other reads prior to eop_desc */
4404 		smp_rmb();
4405 
4406 		/* if the descriptor isn't done, no work yet to do */
4407 		if (!(eop_desc->cmd_type_offset_bsz &
4408 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4409 			break;
4410 
4411 		/* clear next_to_watch to prevent false hangs */
4412 		tx_buf->next_to_watch = NULL;
4413 
4414 		tx_desc->buffer_addr = 0;
4415 		tx_desc->cmd_type_offset_bsz = 0;
4416 		/* move past filter desc */
4417 		tx_buf++;
4418 		tx_desc++;
4419 		i++;
4420 		if (unlikely(!i)) {
4421 			i -= tx_ring->count;
4422 			tx_buf = tx_ring->tx_bi;
4423 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4424 		}
4425 		/* unmap skb header data */
4426 		dma_unmap_single(tx_ring->dev,
4427 				 dma_unmap_addr(tx_buf, dma),
4428 				 dma_unmap_len(tx_buf, len),
4429 				 DMA_TO_DEVICE);
4430 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4431 			kfree(tx_buf->raw_buf);
4432 
4433 		tx_buf->raw_buf = NULL;
4434 		tx_buf->tx_flags = 0;
4435 		tx_buf->next_to_watch = NULL;
4436 		dma_unmap_len_set(tx_buf, len, 0);
4437 		tx_desc->buffer_addr = 0;
4438 		tx_desc->cmd_type_offset_bsz = 0;
4439 
4440 		/* move us past the eop_desc for start of next FD desc */
4441 		tx_buf++;
4442 		tx_desc++;
4443 		i++;
4444 		if (unlikely(!i)) {
4445 			i -= tx_ring->count;
4446 			tx_buf = tx_ring->tx_bi;
4447 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4448 		}
4449 
4450 		/* update budget accounting */
4451 		budget--;
4452 	} while (likely(budget));
4453 
4454 	i += tx_ring->count;
4455 	tx_ring->next_to_clean = i;
4456 
4457 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4458 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4459 
4460 	return budget > 0;
4461 }
4462 
4463 /**
4464  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4465  * @irq: interrupt number
4466  * @data: pointer to a q_vector
4467  **/
i40e_fdir_clean_ring(int irq,void * data)4468 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4469 {
4470 	struct i40e_q_vector *q_vector = data;
4471 	struct i40e_vsi *vsi;
4472 
4473 	if (!q_vector->tx.ring)
4474 		return IRQ_HANDLED;
4475 
4476 	vsi = q_vector->tx.ring->vsi;
4477 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4478 
4479 	return IRQ_HANDLED;
4480 }
4481 
4482 /**
4483  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4484  * @vsi: the VSI being configured
4485  * @v_idx: vector index
4486  * @qp_idx: queue pair index
4487  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4488 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4489 {
4490 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4491 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4492 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4493 
4494 	tx_ring->q_vector = q_vector;
4495 	tx_ring->next = q_vector->tx.ring;
4496 	q_vector->tx.ring = tx_ring;
4497 	q_vector->tx.count++;
4498 
4499 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4500 	if (i40e_enabled_xdp_vsi(vsi)) {
4501 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4502 
4503 		xdp_ring->q_vector = q_vector;
4504 		xdp_ring->next = q_vector->tx.ring;
4505 		q_vector->tx.ring = xdp_ring;
4506 		q_vector->tx.count++;
4507 	}
4508 
4509 	rx_ring->q_vector = q_vector;
4510 	rx_ring->next = q_vector->rx.ring;
4511 	q_vector->rx.ring = rx_ring;
4512 	q_vector->rx.count++;
4513 }
4514 
4515 /**
4516  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4517  * @vsi: the VSI being configured
4518  *
4519  * This function maps descriptor rings to the queue-specific vectors
4520  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4521  * one vector per queue pair, but on a constrained vector budget, we
4522  * group the queue pairs as "efficiently" as possible.
4523  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4524 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4525 {
4526 	int qp_remaining = vsi->num_queue_pairs;
4527 	int q_vectors = vsi->num_q_vectors;
4528 	int num_ringpairs;
4529 	int v_start = 0;
4530 	int qp_idx = 0;
4531 
4532 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4533 	 * group them so there are multiple queues per vector.
4534 	 * It is also important to go through all the vectors available to be
4535 	 * sure that if we don't use all the vectors, that the remaining vectors
4536 	 * are cleared. This is especially important when decreasing the
4537 	 * number of queues in use.
4538 	 */
4539 	for (; v_start < q_vectors; v_start++) {
4540 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4541 
4542 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4543 
4544 		q_vector->num_ringpairs = num_ringpairs;
4545 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4546 
4547 		q_vector->rx.count = 0;
4548 		q_vector->tx.count = 0;
4549 		q_vector->rx.ring = NULL;
4550 		q_vector->tx.ring = NULL;
4551 
4552 		while (num_ringpairs--) {
4553 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4554 			qp_idx++;
4555 			qp_remaining--;
4556 		}
4557 	}
4558 }
4559 
4560 /**
4561  * i40e_vsi_request_irq - Request IRQ from the OS
4562  * @vsi: the VSI being configured
4563  * @basename: name for the vector
4564  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4565 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4566 {
4567 	struct i40e_pf *pf = vsi->back;
4568 	int err;
4569 
4570 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4571 		err = i40e_vsi_request_irq_msix(vsi, basename);
4572 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4573 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4574 				  pf->int_name, pf);
4575 	else
4576 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4577 				  pf->int_name, pf);
4578 
4579 	if (err)
4580 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4581 
4582 	return err;
4583 }
4584 
4585 #ifdef CONFIG_NET_POLL_CONTROLLER
4586 /**
4587  * i40e_netpoll - A Polling 'interrupt' handler
4588  * @netdev: network interface device structure
4589  *
4590  * This is used by netconsole to send skbs without having to re-enable
4591  * interrupts.  It's not called while the normal interrupt routine is executing.
4592  **/
i40e_netpoll(struct net_device * netdev)4593 static void i40e_netpoll(struct net_device *netdev)
4594 {
4595 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4596 	struct i40e_vsi *vsi = np->vsi;
4597 	struct i40e_pf *pf = vsi->back;
4598 	int i;
4599 
4600 	/* if interface is down do nothing */
4601 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4602 		return;
4603 
4604 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4605 		for (i = 0; i < vsi->num_q_vectors; i++)
4606 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4607 	} else {
4608 		i40e_intr(pf->pdev->irq, netdev);
4609 	}
4610 }
4611 #endif
4612 
4613 #define I40E_QTX_ENA_WAIT_COUNT 50
4614 
4615 /**
4616  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4617  * @pf: the PF being configured
4618  * @pf_q: the PF queue
4619  * @enable: enable or disable state of the queue
4620  *
4621  * This routine will wait for the given Tx queue of the PF to reach the
4622  * enabled or disabled state.
4623  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4624  * multiple retries; else will return 0 in case of success.
4625  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4626 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4627 {
4628 	int i;
4629 	u32 tx_reg;
4630 
4631 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4632 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4633 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4634 			break;
4635 
4636 		usleep_range(10, 20);
4637 	}
4638 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4639 		return -ETIMEDOUT;
4640 
4641 	return 0;
4642 }
4643 
4644 /**
4645  * i40e_control_tx_q - Start or stop a particular Tx queue
4646  * @pf: the PF structure
4647  * @pf_q: the PF queue to configure
4648  * @enable: start or stop the queue
4649  *
4650  * This function enables or disables a single queue. Note that any delay
4651  * required after the operation is expected to be handled by the caller of
4652  * this function.
4653  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4654 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4655 {
4656 	struct i40e_hw *hw = &pf->hw;
4657 	u32 tx_reg;
4658 	int i;
4659 
4660 	/* warn the TX unit of coming changes */
4661 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4662 	if (!enable)
4663 		usleep_range(10, 20);
4664 
4665 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4666 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4667 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4668 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4669 			break;
4670 		usleep_range(1000, 2000);
4671 	}
4672 
4673 	/* Skip if the queue is already in the requested state */
4674 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4675 		return;
4676 
4677 	/* turn on/off the queue */
4678 	if (enable) {
4679 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4680 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4681 	} else {
4682 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4683 	}
4684 
4685 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4686 }
4687 
4688 /**
4689  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4690  * @seid: VSI SEID
4691  * @pf: the PF structure
4692  * @pf_q: the PF queue to configure
4693  * @is_xdp: true if the queue is used for XDP
4694  * @enable: start or stop the queue
4695  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4696 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4697 			   bool is_xdp, bool enable)
4698 {
4699 	int ret;
4700 
4701 	i40e_control_tx_q(pf, pf_q, enable);
4702 
4703 	/* wait for the change to finish */
4704 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4705 	if (ret) {
4706 		dev_info(&pf->pdev->dev,
4707 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4708 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4709 			 (enable ? "en" : "dis"));
4710 	}
4711 
4712 	return ret;
4713 }
4714 
4715 /**
4716  * i40e_vsi_enable_tx - Start a VSI's rings
4717  * @vsi: the VSI being configured
4718  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4719 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4720 {
4721 	struct i40e_pf *pf = vsi->back;
4722 	int i, pf_q, ret = 0;
4723 
4724 	pf_q = vsi->base_queue;
4725 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4726 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4727 					     pf_q,
4728 					     false /*is xdp*/, true);
4729 		if (ret)
4730 			break;
4731 
4732 		if (!i40e_enabled_xdp_vsi(vsi))
4733 			continue;
4734 
4735 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4736 					     pf_q + vsi->alloc_queue_pairs,
4737 					     true /*is xdp*/, true);
4738 		if (ret)
4739 			break;
4740 	}
4741 	return ret;
4742 }
4743 
4744 /**
4745  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4746  * @pf: the PF being configured
4747  * @pf_q: the PF queue
4748  * @enable: enable or disable state of the queue
4749  *
4750  * This routine will wait for the given Rx queue of the PF to reach the
4751  * enabled or disabled state.
4752  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4753  * multiple retries; else will return 0 in case of success.
4754  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4755 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4756 {
4757 	int i;
4758 	u32 rx_reg;
4759 
4760 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4761 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4762 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4763 			break;
4764 
4765 		usleep_range(10, 20);
4766 	}
4767 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4768 		return -ETIMEDOUT;
4769 
4770 	return 0;
4771 }
4772 
4773 /**
4774  * i40e_control_rx_q - Start or stop a particular Rx queue
4775  * @pf: the PF structure
4776  * @pf_q: the PF queue to configure
4777  * @enable: start or stop the queue
4778  *
4779  * This function enables or disables a single queue. Note that
4780  * any delay required after the operation is expected to be
4781  * handled by the caller of this function.
4782  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4783 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4784 {
4785 	struct i40e_hw *hw = &pf->hw;
4786 	u32 rx_reg;
4787 	int i;
4788 
4789 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4790 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4791 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4792 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4793 			break;
4794 		usleep_range(1000, 2000);
4795 	}
4796 
4797 	/* Skip if the queue is already in the requested state */
4798 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4799 		return;
4800 
4801 	/* turn on/off the queue */
4802 	if (enable)
4803 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4804 	else
4805 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4806 
4807 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4808 }
4809 
4810 /**
4811  * i40e_control_wait_rx_q
4812  * @pf: the PF structure
4813  * @pf_q: queue being configured
4814  * @enable: start or stop the rings
4815  *
4816  * This function enables or disables a single queue along with waiting
4817  * for the change to finish. The caller of this function should handle
4818  * the delays needed in the case of disabling queues.
4819  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4820 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4821 {
4822 	int ret = 0;
4823 
4824 	i40e_control_rx_q(pf, pf_q, enable);
4825 
4826 	/* wait for the change to finish */
4827 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4828 	if (ret)
4829 		return ret;
4830 
4831 	return ret;
4832 }
4833 
4834 /**
4835  * i40e_vsi_enable_rx - Start a VSI's rings
4836  * @vsi: the VSI being configured
4837  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4838 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4839 {
4840 	struct i40e_pf *pf = vsi->back;
4841 	int i, pf_q, ret = 0;
4842 
4843 	pf_q = vsi->base_queue;
4844 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4845 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4846 		if (ret) {
4847 			dev_info(&pf->pdev->dev,
4848 				 "VSI seid %d Rx ring %d enable timeout\n",
4849 				 vsi->seid, pf_q);
4850 			break;
4851 		}
4852 	}
4853 
4854 	return ret;
4855 }
4856 
4857 /**
4858  * i40e_vsi_start_rings - Start a VSI's rings
4859  * @vsi: the VSI being configured
4860  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4861 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4862 {
4863 	int ret = 0;
4864 
4865 	/* do rx first for enable and last for disable */
4866 	ret = i40e_vsi_enable_rx(vsi);
4867 	if (ret)
4868 		return ret;
4869 	ret = i40e_vsi_enable_tx(vsi);
4870 
4871 	return ret;
4872 }
4873 
4874 #define I40E_DISABLE_TX_GAP_MSEC	50
4875 
4876 /**
4877  * i40e_vsi_stop_rings - Stop a VSI's rings
4878  * @vsi: the VSI being configured
4879  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4880 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4881 {
4882 	struct i40e_pf *pf = vsi->back;
4883 	int pf_q, err, q_end;
4884 
4885 	/* When port TX is suspended, don't wait */
4886 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4887 		return i40e_vsi_stop_rings_no_wait(vsi);
4888 
4889 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4890 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4891 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4892 
4893 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4894 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4895 		if (err)
4896 			dev_info(&pf->pdev->dev,
4897 				 "VSI seid %d Rx ring %d disable timeout\n",
4898 				 vsi->seid, pf_q);
4899 	}
4900 
4901 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4902 	pf_q = vsi->base_queue;
4903 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4904 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4905 
4906 	i40e_vsi_wait_queues_disabled(vsi);
4907 }
4908 
4909 /**
4910  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4911  * @vsi: the VSI being shutdown
4912  *
4913  * This function stops all the rings for a VSI but does not delay to verify
4914  * that rings have been disabled. It is expected that the caller is shutting
4915  * down multiple VSIs at once and will delay together for all the VSIs after
4916  * initiating the shutdown. This is particularly useful for shutting down lots
4917  * of VFs together. Otherwise, a large delay can be incurred while configuring
4918  * each VSI in serial.
4919  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4920 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4921 {
4922 	struct i40e_pf *pf = vsi->back;
4923 	int i, pf_q;
4924 
4925 	pf_q = vsi->base_queue;
4926 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4927 		i40e_control_tx_q(pf, pf_q, false);
4928 		i40e_control_rx_q(pf, pf_q, false);
4929 	}
4930 }
4931 
4932 /**
4933  * i40e_vsi_free_irq - Free the irq association with the OS
4934  * @vsi: the VSI being configured
4935  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)4936 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4937 {
4938 	struct i40e_pf *pf = vsi->back;
4939 	struct i40e_hw *hw = &pf->hw;
4940 	int base = vsi->base_vector;
4941 	u32 val, qp;
4942 	int i;
4943 
4944 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4945 		if (!vsi->q_vectors)
4946 			return;
4947 
4948 		if (!vsi->irqs_ready)
4949 			return;
4950 
4951 		vsi->irqs_ready = false;
4952 		for (i = 0; i < vsi->num_q_vectors; i++) {
4953 			int irq_num;
4954 			u16 vector;
4955 
4956 			vector = i + base;
4957 			irq_num = pf->msix_entries[vector].vector;
4958 
4959 			/* free only the irqs that were actually requested */
4960 			if (!vsi->q_vectors[i] ||
4961 			    !vsi->q_vectors[i]->num_ringpairs)
4962 				continue;
4963 
4964 			/* clear the affinity notifier in the IRQ descriptor */
4965 			irq_set_affinity_notifier(irq_num, NULL);
4966 			/* remove our suggested affinity mask for this IRQ */
4967 			irq_update_affinity_hint(irq_num, NULL);
4968 			free_irq(irq_num, vsi->q_vectors[i]);
4969 
4970 			/* Tear down the interrupt queue link list
4971 			 *
4972 			 * We know that they come in pairs and always
4973 			 * the Rx first, then the Tx.  To clear the
4974 			 * link list, stick the EOL value into the
4975 			 * next_q field of the registers.
4976 			 */
4977 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4978 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4979 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4980 			val |= I40E_QUEUE_END_OF_LIST
4981 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4982 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4983 
4984 			while (qp != I40E_QUEUE_END_OF_LIST) {
4985 				u32 next;
4986 
4987 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4988 
4989 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4990 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4991 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4992 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4993 
4994 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4995 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4996 
4997 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4998 
4999 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5000 
5001 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5002 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5003 
5004 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5005 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5006 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5007 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5008 
5009 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5010 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5011 
5012 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5013 				qp = next;
5014 			}
5015 		}
5016 	} else {
5017 		free_irq(pf->pdev->irq, pf);
5018 
5019 		val = rd32(hw, I40E_PFINT_LNKLST0);
5020 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5021 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5022 		val |= I40E_QUEUE_END_OF_LIST
5023 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5024 		wr32(hw, I40E_PFINT_LNKLST0, val);
5025 
5026 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5027 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5028 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5029 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5030 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5031 
5032 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5033 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5034 
5035 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5036 
5037 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5038 
5039 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5040 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5041 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5042 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5043 
5044 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5045 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5046 
5047 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5048 	}
5049 }
5050 
5051 /**
5052  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5053  * @vsi: the VSI being configured
5054  * @v_idx: Index of vector to be freed
5055  *
5056  * This function frees the memory allocated to the q_vector.  In addition if
5057  * NAPI is enabled it will delete any references to the NAPI struct prior
5058  * to freeing the q_vector.
5059  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)5060 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5061 {
5062 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5063 	struct i40e_ring *ring;
5064 
5065 	if (!q_vector)
5066 		return;
5067 
5068 	/* disassociate q_vector from rings */
5069 	i40e_for_each_ring(ring, q_vector->tx)
5070 		ring->q_vector = NULL;
5071 
5072 	i40e_for_each_ring(ring, q_vector->rx)
5073 		ring->q_vector = NULL;
5074 
5075 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5076 	if (vsi->netdev)
5077 		netif_napi_del(&q_vector->napi);
5078 
5079 	vsi->q_vectors[v_idx] = NULL;
5080 
5081 	kfree_rcu(q_vector, rcu);
5082 }
5083 
5084 /**
5085  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5086  * @vsi: the VSI being un-configured
5087  *
5088  * This frees the memory allocated to the q_vectors and
5089  * deletes references to the NAPI struct.
5090  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)5091 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5092 {
5093 	int v_idx;
5094 
5095 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5096 		i40e_free_q_vector(vsi, v_idx);
5097 }
5098 
5099 /**
5100  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5101  * @pf: board private structure
5102  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)5103 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5104 {
5105 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5106 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5107 		pci_disable_msix(pf->pdev);
5108 		kfree(pf->msix_entries);
5109 		pf->msix_entries = NULL;
5110 		kfree(pf->irq_pile);
5111 		pf->irq_pile = NULL;
5112 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5113 		pci_disable_msi(pf->pdev);
5114 	}
5115 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5116 }
5117 
5118 /**
5119  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5120  * @pf: board private structure
5121  *
5122  * We go through and clear interrupt specific resources and reset the structure
5123  * to pre-load conditions
5124  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)5125 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5126 {
5127 	int i;
5128 
5129 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5130 		i40e_free_misc_vector(pf);
5131 
5132 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5133 		      I40E_IWARP_IRQ_PILE_ID);
5134 
5135 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5136 	for (i = 0; i < pf->num_alloc_vsi; i++)
5137 		if (pf->vsi[i])
5138 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5139 	i40e_reset_interrupt_capability(pf);
5140 }
5141 
5142 /**
5143  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5144  * @vsi: the VSI being configured
5145  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)5146 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5147 {
5148 	int q_idx;
5149 
5150 	if (!vsi->netdev)
5151 		return;
5152 
5153 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5154 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5155 
5156 		if (q_vector->rx.ring || q_vector->tx.ring)
5157 			napi_enable(&q_vector->napi);
5158 	}
5159 }
5160 
5161 /**
5162  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5163  * @vsi: the VSI being configured
5164  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)5165 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5166 {
5167 	int q_idx;
5168 
5169 	if (!vsi->netdev)
5170 		return;
5171 
5172 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5173 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5174 
5175 		if (q_vector->rx.ring || q_vector->tx.ring)
5176 			napi_disable(&q_vector->napi);
5177 	}
5178 }
5179 
5180 /**
5181  * i40e_vsi_close - Shut down a VSI
5182  * @vsi: the vsi to be quelled
5183  **/
i40e_vsi_close(struct i40e_vsi * vsi)5184 static void i40e_vsi_close(struct i40e_vsi *vsi)
5185 {
5186 	struct i40e_pf *pf = vsi->back;
5187 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5188 		i40e_down(vsi);
5189 	i40e_vsi_free_irq(vsi);
5190 	i40e_vsi_free_tx_resources(vsi);
5191 	i40e_vsi_free_rx_resources(vsi);
5192 	vsi->current_netdev_flags = 0;
5193 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5194 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5195 		set_bit(__I40E_CLIENT_RESET, pf->state);
5196 }
5197 
5198 /**
5199  * i40e_quiesce_vsi - Pause a given VSI
5200  * @vsi: the VSI being paused
5201  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)5202 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5203 {
5204 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5205 		return;
5206 
5207 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5208 	if (vsi->netdev && netif_running(vsi->netdev))
5209 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5210 	else
5211 		i40e_vsi_close(vsi);
5212 }
5213 
5214 /**
5215  * i40e_unquiesce_vsi - Resume a given VSI
5216  * @vsi: the VSI being resumed
5217  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)5218 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5219 {
5220 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5221 		return;
5222 
5223 	if (vsi->netdev && netif_running(vsi->netdev))
5224 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5225 	else
5226 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5227 }
5228 
5229 /**
5230  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5231  * @pf: the PF
5232  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)5233 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5234 {
5235 	int v;
5236 
5237 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5238 		if (pf->vsi[v])
5239 			i40e_quiesce_vsi(pf->vsi[v]);
5240 	}
5241 }
5242 
5243 /**
5244  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5245  * @pf: the PF
5246  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5247 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5248 {
5249 	int v;
5250 
5251 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5252 		if (pf->vsi[v])
5253 			i40e_unquiesce_vsi(pf->vsi[v]);
5254 	}
5255 }
5256 
5257 /**
5258  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5259  * @vsi: the VSI being configured
5260  *
5261  * Wait until all queues on a given VSI have been disabled.
5262  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5263 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5264 {
5265 	struct i40e_pf *pf = vsi->back;
5266 	int i, pf_q, ret;
5267 
5268 	pf_q = vsi->base_queue;
5269 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5270 		/* Check and wait for the Tx queue */
5271 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5272 		if (ret) {
5273 			dev_info(&pf->pdev->dev,
5274 				 "VSI seid %d Tx ring %d disable timeout\n",
5275 				 vsi->seid, pf_q);
5276 			return ret;
5277 		}
5278 
5279 		if (!i40e_enabled_xdp_vsi(vsi))
5280 			goto wait_rx;
5281 
5282 		/* Check and wait for the XDP Tx queue */
5283 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5284 				       false);
5285 		if (ret) {
5286 			dev_info(&pf->pdev->dev,
5287 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5288 				 vsi->seid, pf_q);
5289 			return ret;
5290 		}
5291 wait_rx:
5292 		/* Check and wait for the Rx queue */
5293 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5294 		if (ret) {
5295 			dev_info(&pf->pdev->dev,
5296 				 "VSI seid %d Rx ring %d disable timeout\n",
5297 				 vsi->seid, pf_q);
5298 			return ret;
5299 		}
5300 	}
5301 
5302 	return 0;
5303 }
5304 
5305 #ifdef CONFIG_I40E_DCB
5306 /**
5307  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5308  * @pf: the PF
5309  *
5310  * This function waits for the queues to be in disabled state for all the
5311  * VSIs that are managed by this PF.
5312  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5313 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5314 {
5315 	int v, ret = 0;
5316 
5317 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5318 		if (pf->vsi[v]) {
5319 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5320 			if (ret)
5321 				break;
5322 		}
5323 	}
5324 
5325 	return ret;
5326 }
5327 
5328 #endif
5329 
5330 /**
5331  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5332  * @pf: pointer to PF
5333  *
5334  * Get TC map for ISCSI PF type that will include iSCSI TC
5335  * and LAN TC.
5336  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5337 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5338 {
5339 	struct i40e_dcb_app_priority_table app;
5340 	struct i40e_hw *hw = &pf->hw;
5341 	u8 enabled_tc = 1; /* TC0 is always enabled */
5342 	u8 tc, i;
5343 	/* Get the iSCSI APP TLV */
5344 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5345 
5346 	for (i = 0; i < dcbcfg->numapps; i++) {
5347 		app = dcbcfg->app[i];
5348 		if (app.selector == I40E_APP_SEL_TCPIP &&
5349 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5350 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5351 			enabled_tc |= BIT(tc);
5352 			break;
5353 		}
5354 	}
5355 
5356 	return enabled_tc;
5357 }
5358 
5359 /**
5360  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5361  * @dcbcfg: the corresponding DCBx configuration structure
5362  *
5363  * Return the number of TCs from given DCBx configuration
5364  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5365 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5366 {
5367 	int i, tc_unused = 0;
5368 	u8 num_tc = 0;
5369 	u8 ret = 0;
5370 
5371 	/* Scan the ETS Config Priority Table to find
5372 	 * traffic class enabled for a given priority
5373 	 * and create a bitmask of enabled TCs
5374 	 */
5375 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5376 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5377 
5378 	/* Now scan the bitmask to check for
5379 	 * contiguous TCs starting with TC0
5380 	 */
5381 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5382 		if (num_tc & BIT(i)) {
5383 			if (!tc_unused) {
5384 				ret++;
5385 			} else {
5386 				pr_err("Non-contiguous TC - Disabling DCB\n");
5387 				return 1;
5388 			}
5389 		} else {
5390 			tc_unused = 1;
5391 		}
5392 	}
5393 
5394 	/* There is always at least TC0 */
5395 	if (!ret)
5396 		ret = 1;
5397 
5398 	return ret;
5399 }
5400 
5401 /**
5402  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5403  * @dcbcfg: the corresponding DCBx configuration structure
5404  *
5405  * Query the current DCB configuration and return the number of
5406  * traffic classes enabled from the given DCBX config
5407  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5408 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5409 {
5410 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5411 	u8 enabled_tc = 1;
5412 	u8 i;
5413 
5414 	for (i = 0; i < num_tc; i++)
5415 		enabled_tc |= BIT(i);
5416 
5417 	return enabled_tc;
5418 }
5419 
5420 /**
5421  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5422  * @pf: PF being queried
5423  *
5424  * Query the current MQPRIO configuration and return the number of
5425  * traffic classes enabled.
5426  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5427 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5428 {
5429 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5430 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5431 	u8 enabled_tc = 1, i;
5432 
5433 	for (i = 1; i < num_tc; i++)
5434 		enabled_tc |= BIT(i);
5435 	return enabled_tc;
5436 }
5437 
5438 /**
5439  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5440  * @pf: PF being queried
5441  *
5442  * Return number of traffic classes enabled for the given PF
5443  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5444 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5445 {
5446 	struct i40e_hw *hw = &pf->hw;
5447 	u8 i, enabled_tc = 1;
5448 	u8 num_tc = 0;
5449 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5450 
5451 	if (i40e_is_tc_mqprio_enabled(pf))
5452 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5453 
5454 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5455 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5456 		return 1;
5457 
5458 	/* SFP mode will be enabled for all TCs on port */
5459 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5460 		return i40e_dcb_get_num_tc(dcbcfg);
5461 
5462 	/* MFP mode return count of enabled TCs for this PF */
5463 	if (pf->hw.func_caps.iscsi)
5464 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5465 	else
5466 		return 1; /* Only TC0 */
5467 
5468 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5469 		if (enabled_tc & BIT(i))
5470 			num_tc++;
5471 	}
5472 	return num_tc;
5473 }
5474 
5475 /**
5476  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5477  * @pf: PF being queried
5478  *
5479  * Return a bitmap for enabled traffic classes for this PF.
5480  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5481 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5482 {
5483 	if (i40e_is_tc_mqprio_enabled(pf))
5484 		return i40e_mqprio_get_enabled_tc(pf);
5485 
5486 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5487 	 * default TC
5488 	 */
5489 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5490 		return I40E_DEFAULT_TRAFFIC_CLASS;
5491 
5492 	/* SFP mode we want PF to be enabled for all TCs */
5493 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5494 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5495 
5496 	/* MFP enabled and iSCSI PF type */
5497 	if (pf->hw.func_caps.iscsi)
5498 		return i40e_get_iscsi_tc_map(pf);
5499 	else
5500 		return I40E_DEFAULT_TRAFFIC_CLASS;
5501 }
5502 
5503 /**
5504  * i40e_vsi_get_bw_info - Query VSI BW Information
5505  * @vsi: the VSI being queried
5506  *
5507  * Returns 0 on success, negative value on failure
5508  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5509 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5510 {
5511 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5512 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5513 	struct i40e_pf *pf = vsi->back;
5514 	struct i40e_hw *hw = &pf->hw;
5515 	i40e_status ret;
5516 	u32 tc_bw_max;
5517 	int i;
5518 
5519 	/* Get the VSI level BW configuration */
5520 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5521 	if (ret) {
5522 		dev_info(&pf->pdev->dev,
5523 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5524 			 i40e_stat_str(&pf->hw, ret),
5525 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5526 		return -EINVAL;
5527 	}
5528 
5529 	/* Get the VSI level BW configuration per TC */
5530 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5531 					       NULL);
5532 	if (ret) {
5533 		dev_info(&pf->pdev->dev,
5534 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5535 			 i40e_stat_str(&pf->hw, ret),
5536 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5537 		return -EINVAL;
5538 	}
5539 
5540 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5541 		dev_info(&pf->pdev->dev,
5542 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5543 			 bw_config.tc_valid_bits,
5544 			 bw_ets_config.tc_valid_bits);
5545 		/* Still continuing */
5546 	}
5547 
5548 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5549 	vsi->bw_max_quanta = bw_config.max_bw;
5550 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5551 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5552 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5553 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5554 		vsi->bw_ets_limit_credits[i] =
5555 					le16_to_cpu(bw_ets_config.credits[i]);
5556 		/* 3 bits out of 4 for each TC */
5557 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5558 	}
5559 
5560 	return 0;
5561 }
5562 
5563 /**
5564  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5565  * @vsi: the VSI being configured
5566  * @enabled_tc: TC bitmap
5567  * @bw_share: BW shared credits per TC
5568  *
5569  * Returns 0 on success, negative value on failure
5570  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5571 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5572 				       u8 *bw_share)
5573 {
5574 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5575 	struct i40e_pf *pf = vsi->back;
5576 	i40e_status ret;
5577 	int i;
5578 
5579 	/* There is no need to reset BW when mqprio mode is on.  */
5580 	if (i40e_is_tc_mqprio_enabled(pf))
5581 		return 0;
5582 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5583 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5584 		if (ret)
5585 			dev_info(&pf->pdev->dev,
5586 				 "Failed to reset tx rate for vsi->seid %u\n",
5587 				 vsi->seid);
5588 		return ret;
5589 	}
5590 	memset(&bw_data, 0, sizeof(bw_data));
5591 	bw_data.tc_valid_bits = enabled_tc;
5592 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5593 		bw_data.tc_bw_credits[i] = bw_share[i];
5594 
5595 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5596 	if (ret) {
5597 		dev_info(&pf->pdev->dev,
5598 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5599 			 pf->hw.aq.asq_last_status);
5600 		return -EINVAL;
5601 	}
5602 
5603 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5604 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5605 
5606 	return 0;
5607 }
5608 
5609 /**
5610  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5611  * @vsi: the VSI being configured
5612  * @enabled_tc: TC map to be enabled
5613  *
5614  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5615 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5616 {
5617 	struct net_device *netdev = vsi->netdev;
5618 	struct i40e_pf *pf = vsi->back;
5619 	struct i40e_hw *hw = &pf->hw;
5620 	u8 netdev_tc = 0;
5621 	int i;
5622 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5623 
5624 	if (!netdev)
5625 		return;
5626 
5627 	if (!enabled_tc) {
5628 		netdev_reset_tc(netdev);
5629 		return;
5630 	}
5631 
5632 	/* Set up actual enabled TCs on the VSI */
5633 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5634 		return;
5635 
5636 	/* set per TC queues for the VSI */
5637 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5638 		/* Only set TC queues for enabled tcs
5639 		 *
5640 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5641 		 * enabled_tc bitmap would be 0x00001001; the driver
5642 		 * will set the numtc for netdev as 2 that will be
5643 		 * referenced by the netdev layer as TC 0 and 1.
5644 		 */
5645 		if (vsi->tc_config.enabled_tc & BIT(i))
5646 			netdev_set_tc_queue(netdev,
5647 					vsi->tc_config.tc_info[i].netdev_tc,
5648 					vsi->tc_config.tc_info[i].qcount,
5649 					vsi->tc_config.tc_info[i].qoffset);
5650 	}
5651 
5652 	if (i40e_is_tc_mqprio_enabled(pf))
5653 		return;
5654 
5655 	/* Assign UP2TC map for the VSI */
5656 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5657 		/* Get the actual TC# for the UP */
5658 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5659 		/* Get the mapped netdev TC# for the UP */
5660 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5661 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5662 	}
5663 }
5664 
5665 /**
5666  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5667  * @vsi: the VSI being configured
5668  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5669  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5670 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5671 				      struct i40e_vsi_context *ctxt)
5672 {
5673 	/* copy just the sections touched not the entire info
5674 	 * since not all sections are valid as returned by
5675 	 * update vsi params
5676 	 */
5677 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5678 	memcpy(&vsi->info.queue_mapping,
5679 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5680 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5681 	       sizeof(vsi->info.tc_mapping));
5682 }
5683 
5684 /**
5685  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5686  * @vsi: the VSI being reconfigured
5687  * @vsi_offset: offset from main VF VSI
5688  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5689 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5690 {
5691 	struct i40e_vsi_context ctxt = {};
5692 	struct i40e_pf *pf;
5693 	struct i40e_hw *hw;
5694 	int ret;
5695 
5696 	if (!vsi)
5697 		return I40E_ERR_PARAM;
5698 	pf = vsi->back;
5699 	hw = &pf->hw;
5700 
5701 	ctxt.seid = vsi->seid;
5702 	ctxt.pf_num = hw->pf_id;
5703 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5704 	ctxt.uplink_seid = vsi->uplink_seid;
5705 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5706 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5707 	ctxt.info = vsi->info;
5708 
5709 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5710 				 false);
5711 	if (vsi->reconfig_rss) {
5712 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5713 				      vsi->num_queue_pairs);
5714 		ret = i40e_vsi_config_rss(vsi);
5715 		if (ret) {
5716 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5717 			return ret;
5718 		}
5719 		vsi->reconfig_rss = false;
5720 	}
5721 
5722 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5723 	if (ret) {
5724 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5725 			 i40e_stat_str(hw, ret),
5726 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5727 		return ret;
5728 	}
5729 	/* update the local VSI info with updated queue map */
5730 	i40e_vsi_update_queue_map(vsi, &ctxt);
5731 	vsi->info.valid_sections = 0;
5732 
5733 	return ret;
5734 }
5735 
5736 /**
5737  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5738  * @vsi: VSI to be configured
5739  * @enabled_tc: TC bitmap
5740  *
5741  * This configures a particular VSI for TCs that are mapped to the
5742  * given TC bitmap. It uses default bandwidth share for TCs across
5743  * VSIs to configure TC for a particular VSI.
5744  *
5745  * NOTE:
5746  * It is expected that the VSI queues have been quisced before calling
5747  * this function.
5748  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5749 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5750 {
5751 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5752 	struct i40e_pf *pf = vsi->back;
5753 	struct i40e_hw *hw = &pf->hw;
5754 	struct i40e_vsi_context ctxt;
5755 	int ret = 0;
5756 	int i;
5757 
5758 	/* Check if enabled_tc is same as existing or new TCs */
5759 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5760 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5761 		return ret;
5762 
5763 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5764 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5765 		if (enabled_tc & BIT(i))
5766 			bw_share[i] = 1;
5767 	}
5768 
5769 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5770 	if (ret) {
5771 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5772 
5773 		dev_info(&pf->pdev->dev,
5774 			 "Failed configuring TC map %d for VSI %d\n",
5775 			 enabled_tc, vsi->seid);
5776 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5777 						  &bw_config, NULL);
5778 		if (ret) {
5779 			dev_info(&pf->pdev->dev,
5780 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5781 				 i40e_stat_str(hw, ret),
5782 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5783 			goto out;
5784 		}
5785 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5786 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5787 
5788 			if (!valid_tc)
5789 				valid_tc = bw_config.tc_valid_bits;
5790 			/* Always enable TC0, no matter what */
5791 			valid_tc |= 1;
5792 			dev_info(&pf->pdev->dev,
5793 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5794 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5795 			enabled_tc = valid_tc;
5796 		}
5797 
5798 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5799 		if (ret) {
5800 			dev_err(&pf->pdev->dev,
5801 				"Unable to  configure TC map %d for VSI %d\n",
5802 				enabled_tc, vsi->seid);
5803 			goto out;
5804 		}
5805 	}
5806 
5807 	/* Update Queue Pairs Mapping for currently enabled UPs */
5808 	ctxt.seid = vsi->seid;
5809 	ctxt.pf_num = vsi->back->hw.pf_id;
5810 	ctxt.vf_num = 0;
5811 	ctxt.uplink_seid = vsi->uplink_seid;
5812 	ctxt.info = vsi->info;
5813 	if (i40e_is_tc_mqprio_enabled(pf)) {
5814 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5815 		if (ret)
5816 			goto out;
5817 	} else {
5818 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5819 	}
5820 
5821 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5822 	 * queues changed.
5823 	 */
5824 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5825 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5826 				      vsi->num_queue_pairs);
5827 		ret = i40e_vsi_config_rss(vsi);
5828 		if (ret) {
5829 			dev_info(&vsi->back->pdev->dev,
5830 				 "Failed to reconfig rss for num_queues\n");
5831 			return ret;
5832 		}
5833 		vsi->reconfig_rss = false;
5834 	}
5835 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5836 		ctxt.info.valid_sections |=
5837 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5838 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5839 	}
5840 
5841 	/* Update the VSI after updating the VSI queue-mapping
5842 	 * information
5843 	 */
5844 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5845 	if (ret) {
5846 		dev_info(&pf->pdev->dev,
5847 			 "Update vsi tc config failed, err %s aq_err %s\n",
5848 			 i40e_stat_str(hw, ret),
5849 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5850 		goto out;
5851 	}
5852 	/* update the local VSI info with updated queue map */
5853 	i40e_vsi_update_queue_map(vsi, &ctxt);
5854 	vsi->info.valid_sections = 0;
5855 
5856 	/* Update current VSI BW information */
5857 	ret = i40e_vsi_get_bw_info(vsi);
5858 	if (ret) {
5859 		dev_info(&pf->pdev->dev,
5860 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5861 			 i40e_stat_str(hw, ret),
5862 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5863 		goto out;
5864 	}
5865 
5866 	/* Update the netdev TC setup */
5867 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5868 out:
5869 	return ret;
5870 }
5871 
5872 /**
5873  * i40e_get_link_speed - Returns link speed for the interface
5874  * @vsi: VSI to be configured
5875  *
5876  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5877 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5878 {
5879 	struct i40e_pf *pf = vsi->back;
5880 
5881 	switch (pf->hw.phy.link_info.link_speed) {
5882 	case I40E_LINK_SPEED_40GB:
5883 		return 40000;
5884 	case I40E_LINK_SPEED_25GB:
5885 		return 25000;
5886 	case I40E_LINK_SPEED_20GB:
5887 		return 20000;
5888 	case I40E_LINK_SPEED_10GB:
5889 		return 10000;
5890 	case I40E_LINK_SPEED_1GB:
5891 		return 1000;
5892 	default:
5893 		return -EINVAL;
5894 	}
5895 }
5896 
5897 /**
5898  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5899  * @vsi: Pointer to vsi structure
5900  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5901  *
5902  * Helper function to convert units before send to set BW limit
5903  **/
i40e_bw_bytes_to_mbits(struct i40e_vsi * vsi,u64 max_tx_rate)5904 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5905 {
5906 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5907 		dev_warn(&vsi->back->pdev->dev,
5908 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5909 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5910 	} else {
5911 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5912 	}
5913 
5914 	return max_tx_rate;
5915 }
5916 
5917 /**
5918  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5919  * @vsi: VSI to be configured
5920  * @seid: seid of the channel/VSI
5921  * @max_tx_rate: max TX rate to be configured as BW limit
5922  *
5923  * Helper function to set BW limit for a given VSI
5924  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5925 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5926 {
5927 	struct i40e_pf *pf = vsi->back;
5928 	u64 credits = 0;
5929 	int speed = 0;
5930 	int ret = 0;
5931 
5932 	speed = i40e_get_link_speed(vsi);
5933 	if (max_tx_rate > speed) {
5934 		dev_err(&pf->pdev->dev,
5935 			"Invalid max tx rate %llu specified for VSI seid %d.",
5936 			max_tx_rate, seid);
5937 		return -EINVAL;
5938 	}
5939 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5940 		dev_warn(&pf->pdev->dev,
5941 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5942 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5943 	}
5944 
5945 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5946 	credits = max_tx_rate;
5947 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5948 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5949 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5950 	if (ret)
5951 		dev_err(&pf->pdev->dev,
5952 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5953 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5954 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5955 	return ret;
5956 }
5957 
5958 /**
5959  * i40e_remove_queue_channels - Remove queue channels for the TCs
5960  * @vsi: VSI to be configured
5961  *
5962  * Remove queue channels for the TCs
5963  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)5964 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5965 {
5966 	enum i40e_admin_queue_err last_aq_status;
5967 	struct i40e_cloud_filter *cfilter;
5968 	struct i40e_channel *ch, *ch_tmp;
5969 	struct i40e_pf *pf = vsi->back;
5970 	struct hlist_node *node;
5971 	int ret, i;
5972 
5973 	/* Reset rss size that was stored when reconfiguring rss for
5974 	 * channel VSIs with non-power-of-2 queue count.
5975 	 */
5976 	vsi->current_rss_size = 0;
5977 
5978 	/* perform cleanup for channels if they exist */
5979 	if (list_empty(&vsi->ch_list))
5980 		return;
5981 
5982 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5983 		struct i40e_vsi *p_vsi;
5984 
5985 		list_del(&ch->list);
5986 		p_vsi = ch->parent_vsi;
5987 		if (!p_vsi || !ch->initialized) {
5988 			kfree(ch);
5989 			continue;
5990 		}
5991 		/* Reset queue contexts */
5992 		for (i = 0; i < ch->num_queue_pairs; i++) {
5993 			struct i40e_ring *tx_ring, *rx_ring;
5994 			u16 pf_q;
5995 
5996 			pf_q = ch->base_queue + i;
5997 			tx_ring = vsi->tx_rings[pf_q];
5998 			tx_ring->ch = NULL;
5999 
6000 			rx_ring = vsi->rx_rings[pf_q];
6001 			rx_ring->ch = NULL;
6002 		}
6003 
6004 		/* Reset BW configured for this VSI via mqprio */
6005 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6006 		if (ret)
6007 			dev_info(&vsi->back->pdev->dev,
6008 				 "Failed to reset tx rate for ch->seid %u\n",
6009 				 ch->seid);
6010 
6011 		/* delete cloud filters associated with this channel */
6012 		hlist_for_each_entry_safe(cfilter, node,
6013 					  &pf->cloud_filter_list, cloud_node) {
6014 			if (cfilter->seid != ch->seid)
6015 				continue;
6016 
6017 			hash_del(&cfilter->cloud_node);
6018 			if (cfilter->dst_port)
6019 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6020 									cfilter,
6021 									false);
6022 			else
6023 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6024 								false);
6025 			last_aq_status = pf->hw.aq.asq_last_status;
6026 			if (ret)
6027 				dev_info(&pf->pdev->dev,
6028 					 "Failed to delete cloud filter, err %s aq_err %s\n",
6029 					 i40e_stat_str(&pf->hw, ret),
6030 					 i40e_aq_str(&pf->hw, last_aq_status));
6031 			kfree(cfilter);
6032 		}
6033 
6034 		/* delete VSI from FW */
6035 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6036 					     NULL);
6037 		if (ret)
6038 			dev_err(&vsi->back->pdev->dev,
6039 				"unable to remove channel (%d) for parent VSI(%d)\n",
6040 				ch->seid, p_vsi->seid);
6041 		kfree(ch);
6042 	}
6043 	INIT_LIST_HEAD(&vsi->ch_list);
6044 }
6045 
6046 /**
6047  * i40e_get_max_queues_for_channel
6048  * @vsi: ptr to VSI to which channels are associated with
6049  *
6050  * Helper function which returns max value among the queue counts set on the
6051  * channels/TCs created.
6052  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)6053 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6054 {
6055 	struct i40e_channel *ch, *ch_tmp;
6056 	int max = 0;
6057 
6058 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6059 		if (!ch->initialized)
6060 			continue;
6061 		if (ch->num_queue_pairs > max)
6062 			max = ch->num_queue_pairs;
6063 	}
6064 
6065 	return max;
6066 }
6067 
6068 /**
6069  * i40e_validate_num_queues - validate num_queues w.r.t channel
6070  * @pf: ptr to PF device
6071  * @num_queues: number of queues
6072  * @vsi: the parent VSI
6073  * @reconfig_rss: indicates should the RSS be reconfigured or not
6074  *
6075  * This function validates number of queues in the context of new channel
6076  * which is being established and determines if RSS should be reconfigured
6077  * or not for parent VSI.
6078  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)6079 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6080 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6081 {
6082 	int max_ch_queues;
6083 
6084 	if (!reconfig_rss)
6085 		return -EINVAL;
6086 
6087 	*reconfig_rss = false;
6088 	if (vsi->current_rss_size) {
6089 		if (num_queues > vsi->current_rss_size) {
6090 			dev_dbg(&pf->pdev->dev,
6091 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6092 				num_queues, vsi->current_rss_size);
6093 			return -EINVAL;
6094 		} else if ((num_queues < vsi->current_rss_size) &&
6095 			   (!is_power_of_2(num_queues))) {
6096 			dev_dbg(&pf->pdev->dev,
6097 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6098 				num_queues, vsi->current_rss_size);
6099 			return -EINVAL;
6100 		}
6101 	}
6102 
6103 	if (!is_power_of_2(num_queues)) {
6104 		/* Find the max num_queues configured for channel if channel
6105 		 * exist.
6106 		 * if channel exist, then enforce 'num_queues' to be more than
6107 		 * max ever queues configured for channel.
6108 		 */
6109 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6110 		if (num_queues < max_ch_queues) {
6111 			dev_dbg(&pf->pdev->dev,
6112 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6113 				num_queues, max_ch_queues);
6114 			return -EINVAL;
6115 		}
6116 		*reconfig_rss = true;
6117 	}
6118 
6119 	return 0;
6120 }
6121 
6122 /**
6123  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6124  * @vsi: the VSI being setup
6125  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6126  *
6127  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6128  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)6129 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6130 {
6131 	struct i40e_pf *pf = vsi->back;
6132 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6133 	struct i40e_hw *hw = &pf->hw;
6134 	int local_rss_size;
6135 	u8 *lut;
6136 	int ret;
6137 
6138 	if (!vsi->rss_size)
6139 		return -EINVAL;
6140 
6141 	if (rss_size > vsi->rss_size)
6142 		return -EINVAL;
6143 
6144 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6145 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6146 	if (!lut)
6147 		return -ENOMEM;
6148 
6149 	/* Ignoring user configured lut if there is one */
6150 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6151 
6152 	/* Use user configured hash key if there is one, otherwise
6153 	 * use default.
6154 	 */
6155 	if (vsi->rss_hkey_user)
6156 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6157 	else
6158 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6159 
6160 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6161 	if (ret) {
6162 		dev_info(&pf->pdev->dev,
6163 			 "Cannot set RSS lut, err %s aq_err %s\n",
6164 			 i40e_stat_str(hw, ret),
6165 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6166 		kfree(lut);
6167 		return ret;
6168 	}
6169 	kfree(lut);
6170 
6171 	/* Do the update w.r.t. storing rss_size */
6172 	if (!vsi->orig_rss_size)
6173 		vsi->orig_rss_size = vsi->rss_size;
6174 	vsi->current_rss_size = local_rss_size;
6175 
6176 	return ret;
6177 }
6178 
6179 /**
6180  * i40e_channel_setup_queue_map - Setup a channel queue map
6181  * @pf: ptr to PF device
6182  * @ctxt: VSI context structure
6183  * @ch: ptr to channel structure
6184  *
6185  * Setup queue map for a specific channel
6186  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)6187 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6188 					 struct i40e_vsi_context *ctxt,
6189 					 struct i40e_channel *ch)
6190 {
6191 	u16 qcount, qmap, sections = 0;
6192 	u8 offset = 0;
6193 	int pow;
6194 
6195 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6196 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6197 
6198 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6199 	ch->num_queue_pairs = qcount;
6200 
6201 	/* find the next higher power-of-2 of num queue pairs */
6202 	pow = ilog2(qcount);
6203 	if (!is_power_of_2(qcount))
6204 		pow++;
6205 
6206 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6207 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6208 
6209 	/* Setup queue TC[0].qmap for given VSI context */
6210 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6211 
6212 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6213 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6214 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6215 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6216 }
6217 
6218 /**
6219  * i40e_add_channel - add a channel by adding VSI
6220  * @pf: ptr to PF device
6221  * @uplink_seid: underlying HW switching element (VEB) ID
6222  * @ch: ptr to channel structure
6223  *
6224  * Add a channel (VSI) using add_vsi and queue_map
6225  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)6226 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6227 			    struct i40e_channel *ch)
6228 {
6229 	struct i40e_hw *hw = &pf->hw;
6230 	struct i40e_vsi_context ctxt;
6231 	u8 enabled_tc = 0x1; /* TC0 enabled */
6232 	int ret;
6233 
6234 	if (ch->type != I40E_VSI_VMDQ2) {
6235 		dev_info(&pf->pdev->dev,
6236 			 "add new vsi failed, ch->type %d\n", ch->type);
6237 		return -EINVAL;
6238 	}
6239 
6240 	memset(&ctxt, 0, sizeof(ctxt));
6241 	ctxt.pf_num = hw->pf_id;
6242 	ctxt.vf_num = 0;
6243 	ctxt.uplink_seid = uplink_seid;
6244 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6245 	if (ch->type == I40E_VSI_VMDQ2)
6246 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6247 
6248 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6249 		ctxt.info.valid_sections |=
6250 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6251 		ctxt.info.switch_id =
6252 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6253 	}
6254 
6255 	/* Set queue map for a given VSI context */
6256 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6257 
6258 	/* Now time to create VSI */
6259 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6260 	if (ret) {
6261 		dev_info(&pf->pdev->dev,
6262 			 "add new vsi failed, err %s aq_err %s\n",
6263 			 i40e_stat_str(&pf->hw, ret),
6264 			 i40e_aq_str(&pf->hw,
6265 				     pf->hw.aq.asq_last_status));
6266 		return -ENOENT;
6267 	}
6268 
6269 	/* Success, update channel, set enabled_tc only if the channel
6270 	 * is not a macvlan
6271 	 */
6272 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6273 	ch->seid = ctxt.seid;
6274 	ch->vsi_number = ctxt.vsi_number;
6275 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6276 
6277 	/* copy just the sections touched not the entire info
6278 	 * since not all sections are valid as returned by
6279 	 * update vsi params
6280 	 */
6281 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6282 	memcpy(&ch->info.queue_mapping,
6283 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6284 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6285 	       sizeof(ctxt.info.tc_mapping));
6286 
6287 	return 0;
6288 }
6289 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6290 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6291 				  u8 *bw_share)
6292 {
6293 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6294 	i40e_status ret;
6295 	int i;
6296 
6297 	memset(&bw_data, 0, sizeof(bw_data));
6298 	bw_data.tc_valid_bits = ch->enabled_tc;
6299 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6300 		bw_data.tc_bw_credits[i] = bw_share[i];
6301 
6302 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6303 				       &bw_data, NULL);
6304 	if (ret) {
6305 		dev_info(&vsi->back->pdev->dev,
6306 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6307 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6308 		return -EINVAL;
6309 	}
6310 
6311 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6312 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6313 
6314 	return 0;
6315 }
6316 
6317 /**
6318  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6319  * @pf: ptr to PF device
6320  * @vsi: the VSI being setup
6321  * @ch: ptr to channel structure
6322  *
6323  * Configure TX rings associated with channel (VSI) since queues are being
6324  * from parent VSI.
6325  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6326 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6327 				       struct i40e_vsi *vsi,
6328 				       struct i40e_channel *ch)
6329 {
6330 	i40e_status ret;
6331 	int i;
6332 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6333 
6334 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6335 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6336 		if (ch->enabled_tc & BIT(i))
6337 			bw_share[i] = 1;
6338 	}
6339 
6340 	/* configure BW for new VSI */
6341 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6342 	if (ret) {
6343 		dev_info(&vsi->back->pdev->dev,
6344 			 "Failed configuring TC map %d for channel (seid %u)\n",
6345 			 ch->enabled_tc, ch->seid);
6346 		return ret;
6347 	}
6348 
6349 	for (i = 0; i < ch->num_queue_pairs; i++) {
6350 		struct i40e_ring *tx_ring, *rx_ring;
6351 		u16 pf_q;
6352 
6353 		pf_q = ch->base_queue + i;
6354 
6355 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6356 		 * context
6357 		 */
6358 		tx_ring = vsi->tx_rings[pf_q];
6359 		tx_ring->ch = ch;
6360 
6361 		/* Get the RX ring ptr */
6362 		rx_ring = vsi->rx_rings[pf_q];
6363 		rx_ring->ch = ch;
6364 	}
6365 
6366 	return 0;
6367 }
6368 
6369 /**
6370  * i40e_setup_hw_channel - setup new channel
6371  * @pf: ptr to PF device
6372  * @vsi: the VSI being setup
6373  * @ch: ptr to channel structure
6374  * @uplink_seid: underlying HW switching element (VEB) ID
6375  * @type: type of channel to be created (VMDq2/VF)
6376  *
6377  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6378  * and configures TX rings accordingly
6379  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6380 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6381 					struct i40e_vsi *vsi,
6382 					struct i40e_channel *ch,
6383 					u16 uplink_seid, u8 type)
6384 {
6385 	int ret;
6386 
6387 	ch->initialized = false;
6388 	ch->base_queue = vsi->next_base_queue;
6389 	ch->type = type;
6390 
6391 	/* Proceed with creation of channel (VMDq2) VSI */
6392 	ret = i40e_add_channel(pf, uplink_seid, ch);
6393 	if (ret) {
6394 		dev_info(&pf->pdev->dev,
6395 			 "failed to add_channel using uplink_seid %u\n",
6396 			 uplink_seid);
6397 		return ret;
6398 	}
6399 
6400 	/* Mark the successful creation of channel */
6401 	ch->initialized = true;
6402 
6403 	/* Reconfigure TX queues using QTX_CTL register */
6404 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6405 	if (ret) {
6406 		dev_info(&pf->pdev->dev,
6407 			 "failed to configure TX rings for channel %u\n",
6408 			 ch->seid);
6409 		return ret;
6410 	}
6411 
6412 	/* update 'next_base_queue' */
6413 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6414 	dev_dbg(&pf->pdev->dev,
6415 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6416 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6417 		ch->num_queue_pairs,
6418 		vsi->next_base_queue);
6419 	return ret;
6420 }
6421 
6422 /**
6423  * i40e_setup_channel - setup new channel using uplink element
6424  * @pf: ptr to PF device
6425  * @vsi: pointer to the VSI to set up the channel within
6426  * @ch: ptr to channel structure
6427  *
6428  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6429  * and uplink switching element (uplink_seid)
6430  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6431 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6432 			       struct i40e_channel *ch)
6433 {
6434 	u8 vsi_type;
6435 	u16 seid;
6436 	int ret;
6437 
6438 	if (vsi->type == I40E_VSI_MAIN) {
6439 		vsi_type = I40E_VSI_VMDQ2;
6440 	} else {
6441 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6442 			vsi->type);
6443 		return false;
6444 	}
6445 
6446 	/* underlying switching element */
6447 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6448 
6449 	/* create channel (VSI), configure TX rings */
6450 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6451 	if (ret) {
6452 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6453 		return false;
6454 	}
6455 
6456 	return ch->initialized ? true : false;
6457 }
6458 
6459 /**
6460  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6461  * @vsi: ptr to VSI which has PF backing
6462  *
6463  * Sets up switch mode correctly if it needs to be changed and perform
6464  * what are allowed modes.
6465  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6466 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6467 {
6468 	u8 mode;
6469 	struct i40e_pf *pf = vsi->back;
6470 	struct i40e_hw *hw = &pf->hw;
6471 	int ret;
6472 
6473 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6474 	if (ret)
6475 		return -EINVAL;
6476 
6477 	if (hw->dev_caps.switch_mode) {
6478 		/* if switch mode is set, support mode2 (non-tunneled for
6479 		 * cloud filter) for now
6480 		 */
6481 		u32 switch_mode = hw->dev_caps.switch_mode &
6482 				  I40E_SWITCH_MODE_MASK;
6483 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6484 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6485 				return 0;
6486 			dev_err(&pf->pdev->dev,
6487 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6488 				hw->dev_caps.switch_mode);
6489 			return -EINVAL;
6490 		}
6491 	}
6492 
6493 	/* Set Bit 7 to be valid */
6494 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6495 
6496 	/* Set L4type for TCP support */
6497 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6498 
6499 	/* Set cloud filter mode */
6500 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6501 
6502 	/* Prep mode field for set_switch_config */
6503 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6504 					pf->last_sw_conf_valid_flags,
6505 					mode, NULL);
6506 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6507 		dev_err(&pf->pdev->dev,
6508 			"couldn't set switch config bits, err %s aq_err %s\n",
6509 			i40e_stat_str(hw, ret),
6510 			i40e_aq_str(hw,
6511 				    hw->aq.asq_last_status));
6512 
6513 	return ret;
6514 }
6515 
6516 /**
6517  * i40e_create_queue_channel - function to create channel
6518  * @vsi: VSI to be configured
6519  * @ch: ptr to channel (it contains channel specific params)
6520  *
6521  * This function creates channel (VSI) using num_queues specified by user,
6522  * reconfigs RSS if needed.
6523  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6524 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6525 			      struct i40e_channel *ch)
6526 {
6527 	struct i40e_pf *pf = vsi->back;
6528 	bool reconfig_rss;
6529 	int err;
6530 
6531 	if (!ch)
6532 		return -EINVAL;
6533 
6534 	if (!ch->num_queue_pairs) {
6535 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6536 			ch->num_queue_pairs);
6537 		return -EINVAL;
6538 	}
6539 
6540 	/* validate user requested num_queues for channel */
6541 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6542 				       &reconfig_rss);
6543 	if (err) {
6544 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6545 			 ch->num_queue_pairs);
6546 		return -EINVAL;
6547 	}
6548 
6549 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6550 	 * VSI to be added switch to VEB mode.
6551 	 */
6552 
6553 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6554 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6555 
6556 		if (vsi->type == I40E_VSI_MAIN) {
6557 			if (i40e_is_tc_mqprio_enabled(pf))
6558 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6559 			else
6560 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6561 		}
6562 		/* now onwards for main VSI, number of queues will be value
6563 		 * of TC0's queue count
6564 		 */
6565 	}
6566 
6567 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6568 	 * it should be more than num_queues
6569 	 */
6570 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6571 		dev_dbg(&pf->pdev->dev,
6572 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6573 			vsi->cnt_q_avail, ch->num_queue_pairs);
6574 		return -EINVAL;
6575 	}
6576 
6577 	/* reconfig_rss only if vsi type is MAIN_VSI */
6578 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6579 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6580 		if (err) {
6581 			dev_info(&pf->pdev->dev,
6582 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6583 				 ch->num_queue_pairs);
6584 			return -EINVAL;
6585 		}
6586 	}
6587 
6588 	if (!i40e_setup_channel(pf, vsi, ch)) {
6589 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6590 		return -EINVAL;
6591 	}
6592 
6593 	dev_info(&pf->pdev->dev,
6594 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6595 		 ch->seid, ch->num_queue_pairs);
6596 
6597 	/* configure VSI for BW limit */
6598 	if (ch->max_tx_rate) {
6599 		u64 credits = ch->max_tx_rate;
6600 
6601 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6602 			return -EINVAL;
6603 
6604 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6605 		dev_dbg(&pf->pdev->dev,
6606 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6607 			ch->max_tx_rate,
6608 			credits,
6609 			ch->seid);
6610 	}
6611 
6612 	/* in case of VF, this will be main SRIOV VSI */
6613 	ch->parent_vsi = vsi;
6614 
6615 	/* and update main_vsi's count for queue_available to use */
6616 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6617 
6618 	return 0;
6619 }
6620 
6621 /**
6622  * i40e_configure_queue_channels - Add queue channel for the given TCs
6623  * @vsi: VSI to be configured
6624  *
6625  * Configures queue channel mapping to the given TCs
6626  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6627 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6628 {
6629 	struct i40e_channel *ch;
6630 	u64 max_rate = 0;
6631 	int ret = 0, i;
6632 
6633 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6634 	vsi->tc_seid_map[0] = vsi->seid;
6635 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6636 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6637 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6638 			if (!ch) {
6639 				ret = -ENOMEM;
6640 				goto err_free;
6641 			}
6642 
6643 			INIT_LIST_HEAD(&ch->list);
6644 			ch->num_queue_pairs =
6645 				vsi->tc_config.tc_info[i].qcount;
6646 			ch->base_queue =
6647 				vsi->tc_config.tc_info[i].qoffset;
6648 
6649 			/* Bandwidth limit through tc interface is in bytes/s,
6650 			 * change to Mbit/s
6651 			 */
6652 			max_rate = vsi->mqprio_qopt.max_rate[i];
6653 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6654 			ch->max_tx_rate = max_rate;
6655 
6656 			list_add_tail(&ch->list, &vsi->ch_list);
6657 
6658 			ret = i40e_create_queue_channel(vsi, ch);
6659 			if (ret) {
6660 				dev_err(&vsi->back->pdev->dev,
6661 					"Failed creating queue channel with TC%d: queues %d\n",
6662 					i, ch->num_queue_pairs);
6663 				goto err_free;
6664 			}
6665 			vsi->tc_seid_map[i] = ch->seid;
6666 		}
6667 	}
6668 
6669 	/* reset to reconfigure TX queue contexts */
6670 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6671 	return ret;
6672 
6673 err_free:
6674 	i40e_remove_queue_channels(vsi);
6675 	return ret;
6676 }
6677 
6678 /**
6679  * i40e_veb_config_tc - Configure TCs for given VEB
6680  * @veb: given VEB
6681  * @enabled_tc: TC bitmap
6682  *
6683  * Configures given TC bitmap for VEB (switching) element
6684  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6685 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6686 {
6687 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6688 	struct i40e_pf *pf = veb->pf;
6689 	int ret = 0;
6690 	int i;
6691 
6692 	/* No TCs or already enabled TCs just return */
6693 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6694 		return ret;
6695 
6696 	bw_data.tc_valid_bits = enabled_tc;
6697 	/* bw_data.absolute_credits is not set (relative) */
6698 
6699 	/* Enable ETS TCs with equal BW Share for now */
6700 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6701 		if (enabled_tc & BIT(i))
6702 			bw_data.tc_bw_share_credits[i] = 1;
6703 	}
6704 
6705 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6706 						   &bw_data, NULL);
6707 	if (ret) {
6708 		dev_info(&pf->pdev->dev,
6709 			 "VEB bw config failed, err %s aq_err %s\n",
6710 			 i40e_stat_str(&pf->hw, ret),
6711 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6712 		goto out;
6713 	}
6714 
6715 	/* Update the BW information */
6716 	ret = i40e_veb_get_bw_info(veb);
6717 	if (ret) {
6718 		dev_info(&pf->pdev->dev,
6719 			 "Failed getting veb bw config, err %s aq_err %s\n",
6720 			 i40e_stat_str(&pf->hw, ret),
6721 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6722 	}
6723 
6724 out:
6725 	return ret;
6726 }
6727 
6728 #ifdef CONFIG_I40E_DCB
6729 /**
6730  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6731  * @pf: PF struct
6732  *
6733  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6734  * the caller would've quiesce all the VSIs before calling
6735  * this function
6736  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6737 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6738 {
6739 	u8 tc_map = 0;
6740 	int ret;
6741 	u8 v;
6742 
6743 	/* Enable the TCs available on PF to all VEBs */
6744 	tc_map = i40e_pf_get_tc_map(pf);
6745 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6746 		return;
6747 
6748 	for (v = 0; v < I40E_MAX_VEB; v++) {
6749 		if (!pf->veb[v])
6750 			continue;
6751 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6752 		if (ret) {
6753 			dev_info(&pf->pdev->dev,
6754 				 "Failed configuring TC for VEB seid=%d\n",
6755 				 pf->veb[v]->seid);
6756 			/* Will try to configure as many components */
6757 		}
6758 	}
6759 
6760 	/* Update each VSI */
6761 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6762 		if (!pf->vsi[v])
6763 			continue;
6764 
6765 		/* - Enable all TCs for the LAN VSI
6766 		 * - For all others keep them at TC0 for now
6767 		 */
6768 		if (v == pf->lan_vsi)
6769 			tc_map = i40e_pf_get_tc_map(pf);
6770 		else
6771 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6772 
6773 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6774 		if (ret) {
6775 			dev_info(&pf->pdev->dev,
6776 				 "Failed configuring TC for VSI seid=%d\n",
6777 				 pf->vsi[v]->seid);
6778 			/* Will try to configure as many components */
6779 		} else {
6780 			/* Re-configure VSI vectors based on updated TC map */
6781 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6782 			if (pf->vsi[v]->netdev)
6783 				i40e_dcbnl_set_all(pf->vsi[v]);
6784 		}
6785 	}
6786 }
6787 
6788 /**
6789  * i40e_resume_port_tx - Resume port Tx
6790  * @pf: PF struct
6791  *
6792  * Resume a port's Tx and issue a PF reset in case of failure to
6793  * resume.
6794  **/
i40e_resume_port_tx(struct i40e_pf * pf)6795 static int i40e_resume_port_tx(struct i40e_pf *pf)
6796 {
6797 	struct i40e_hw *hw = &pf->hw;
6798 	int ret;
6799 
6800 	ret = i40e_aq_resume_port_tx(hw, NULL);
6801 	if (ret) {
6802 		dev_info(&pf->pdev->dev,
6803 			 "Resume Port Tx failed, err %s aq_err %s\n",
6804 			  i40e_stat_str(&pf->hw, ret),
6805 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6806 		/* Schedule PF reset to recover */
6807 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6808 		i40e_service_event_schedule(pf);
6809 	}
6810 
6811 	return ret;
6812 }
6813 
6814 /**
6815  * i40e_suspend_port_tx - Suspend port Tx
6816  * @pf: PF struct
6817  *
6818  * Suspend a port's Tx and issue a PF reset in case of failure.
6819  **/
i40e_suspend_port_tx(struct i40e_pf * pf)6820 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6821 {
6822 	struct i40e_hw *hw = &pf->hw;
6823 	int ret;
6824 
6825 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6826 	if (ret) {
6827 		dev_info(&pf->pdev->dev,
6828 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6829 			 i40e_stat_str(&pf->hw, ret),
6830 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6831 		/* Schedule PF reset to recover */
6832 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6833 		i40e_service_event_schedule(pf);
6834 	}
6835 
6836 	return ret;
6837 }
6838 
6839 /**
6840  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6841  * @pf: PF being configured
6842  * @new_cfg: New DCBX configuration
6843  *
6844  * Program DCB settings into HW and reconfigure VEB/VSIs on
6845  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6846  **/
i40e_hw_set_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6847 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6848 				  struct i40e_dcbx_config *new_cfg)
6849 {
6850 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6851 	int ret;
6852 
6853 	/* Check if need reconfiguration */
6854 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6855 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6856 		return 0;
6857 	}
6858 
6859 	/* Config change disable all VSIs */
6860 	i40e_pf_quiesce_all_vsi(pf);
6861 
6862 	/* Copy the new config to the current config */
6863 	*old_cfg = *new_cfg;
6864 	old_cfg->etsrec = old_cfg->etscfg;
6865 	ret = i40e_set_dcb_config(&pf->hw);
6866 	if (ret) {
6867 		dev_info(&pf->pdev->dev,
6868 			 "Set DCB Config failed, err %s aq_err %s\n",
6869 			 i40e_stat_str(&pf->hw, ret),
6870 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6871 		goto out;
6872 	}
6873 
6874 	/* Changes in configuration update VEB/VSI */
6875 	i40e_dcb_reconfigure(pf);
6876 out:
6877 	/* In case of reset do not try to resume anything */
6878 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6879 		/* Re-start the VSIs if disabled */
6880 		ret = i40e_resume_port_tx(pf);
6881 		/* In case of error no point in resuming VSIs */
6882 		if (ret)
6883 			goto err;
6884 		i40e_pf_unquiesce_all_vsi(pf);
6885 	}
6886 err:
6887 	return ret;
6888 }
6889 
6890 /**
6891  * i40e_hw_dcb_config - Program new DCBX settings into HW
6892  * @pf: PF being configured
6893  * @new_cfg: New DCBX configuration
6894  *
6895  * Program DCB settings into HW and reconfigure VEB/VSIs on
6896  * given PF
6897  **/
i40e_hw_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6898 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6899 {
6900 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6901 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6902 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6903 	struct i40e_dcbx_config *old_cfg;
6904 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6905 	struct i40e_rx_pb_config pb_cfg;
6906 	struct i40e_hw *hw = &pf->hw;
6907 	u8 num_ports = hw->num_ports;
6908 	bool need_reconfig;
6909 	int ret = -EINVAL;
6910 	u8 lltc_map = 0;
6911 	u8 tc_map = 0;
6912 	u8 new_numtc;
6913 	u8 i;
6914 
6915 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6916 	/* Un-pack information to Program ETS HW via shared API
6917 	 * numtc, tcmap
6918 	 * LLTC map
6919 	 * ETS/NON-ETS arbiter mode
6920 	 * max exponent (credit refills)
6921 	 * Total number of ports
6922 	 * PFC priority bit-map
6923 	 * Priority Table
6924 	 * BW % per TC
6925 	 * Arbiter mode between UPs sharing same TC
6926 	 * TSA table (ETS or non-ETS)
6927 	 * EEE enabled or not
6928 	 * MFS TC table
6929 	 */
6930 
6931 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6932 
6933 	memset(&ets_data, 0, sizeof(ets_data));
6934 	for (i = 0; i < new_numtc; i++) {
6935 		tc_map |= BIT(i);
6936 		switch (new_cfg->etscfg.tsatable[i]) {
6937 		case I40E_IEEE_TSA_ETS:
6938 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6939 			ets_data.tc_bw_share_credits[i] =
6940 					new_cfg->etscfg.tcbwtable[i];
6941 			break;
6942 		case I40E_IEEE_TSA_STRICT:
6943 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6944 			lltc_map |= BIT(i);
6945 			ets_data.tc_bw_share_credits[i] =
6946 					I40E_DCB_STRICT_PRIO_CREDITS;
6947 			break;
6948 		default:
6949 			/* Invalid TSA type */
6950 			need_reconfig = false;
6951 			goto out;
6952 		}
6953 	}
6954 
6955 	old_cfg = &hw->local_dcbx_config;
6956 	/* Check if need reconfiguration */
6957 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6958 
6959 	/* If needed, enable/disable frame tagging, disable all VSIs
6960 	 * and suspend port tx
6961 	 */
6962 	if (need_reconfig) {
6963 		/* Enable DCB tagging only when more than one TC */
6964 		if (new_numtc > 1)
6965 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6966 		else
6967 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6968 
6969 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6970 		/* Reconfiguration needed quiesce all VSIs */
6971 		i40e_pf_quiesce_all_vsi(pf);
6972 		ret = i40e_suspend_port_tx(pf);
6973 		if (ret)
6974 			goto err;
6975 	}
6976 
6977 	/* Configure Port ETS Tx Scheduler */
6978 	ets_data.tc_valid_bits = tc_map;
6979 	ets_data.tc_strict_priority_flags = lltc_map;
6980 	ret = i40e_aq_config_switch_comp_ets
6981 		(hw, pf->mac_seid, &ets_data,
6982 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6983 	if (ret) {
6984 		dev_info(&pf->pdev->dev,
6985 			 "Modify Port ETS failed, err %s aq_err %s\n",
6986 			 i40e_stat_str(&pf->hw, ret),
6987 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6988 		goto out;
6989 	}
6990 
6991 	/* Configure Rx ETS HW */
6992 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6993 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6994 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6995 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6996 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6997 				   lltc_map);
6998 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6999 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7000 				     prio_type);
7001 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7002 			       new_cfg->etscfg.prioritytable);
7003 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7004 
7005 	/* Configure Rx Packet Buffers in HW */
7006 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7007 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7008 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7009 	}
7010 
7011 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7012 					 false, new_cfg->pfc.pfcenable,
7013 					 mfs_tc, &pb_cfg);
7014 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7015 
7016 	/* Update the local Rx Packet buffer config */
7017 	pf->pb_cfg = pb_cfg;
7018 
7019 	/* Inform the FW about changes to DCB configuration */
7020 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7021 	if (ret) {
7022 		dev_info(&pf->pdev->dev,
7023 			 "DCB Updated failed, err %s aq_err %s\n",
7024 			 i40e_stat_str(&pf->hw, ret),
7025 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7026 		goto out;
7027 	}
7028 
7029 	/* Update the port DCBx configuration */
7030 	*old_cfg = *new_cfg;
7031 
7032 	/* Changes in configuration update VEB/VSI */
7033 	i40e_dcb_reconfigure(pf);
7034 out:
7035 	/* Re-start the VSIs if disabled */
7036 	if (need_reconfig) {
7037 		ret = i40e_resume_port_tx(pf);
7038 
7039 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7040 		/* In case of error no point in resuming VSIs */
7041 		if (ret)
7042 			goto err;
7043 
7044 		/* Wait for the PF's queues to be disabled */
7045 		ret = i40e_pf_wait_queues_disabled(pf);
7046 		if (ret) {
7047 			/* Schedule PF reset to recover */
7048 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7049 			i40e_service_event_schedule(pf);
7050 			goto err;
7051 		} else {
7052 			i40e_pf_unquiesce_all_vsi(pf);
7053 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7054 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7055 		}
7056 		/* registers are set, lets apply */
7057 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7058 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7059 	}
7060 
7061 err:
7062 	return ret;
7063 }
7064 
7065 /**
7066  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7067  * @pf: PF being queried
7068  *
7069  * Set default DCB configuration in case DCB is to be done in SW.
7070  **/
i40e_dcb_sw_default_config(struct i40e_pf * pf)7071 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7072 {
7073 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7074 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7075 	struct i40e_hw *hw = &pf->hw;
7076 	int err;
7077 
7078 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7079 		/* Update the local cached instance with TC0 ETS */
7080 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7081 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7082 		pf->tmp_cfg.etscfg.maxtcs = 0;
7083 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7084 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7085 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7086 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7087 		/* FW needs one App to configure HW */
7088 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7089 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7090 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7091 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7092 
7093 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7094 	}
7095 
7096 	memset(&ets_data, 0, sizeof(ets_data));
7097 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7098 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7099 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7100 
7101 	/* Enable ETS on the Physical port */
7102 	err = i40e_aq_config_switch_comp_ets
7103 		(hw, pf->mac_seid, &ets_data,
7104 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7105 	if (err) {
7106 		dev_info(&pf->pdev->dev,
7107 			 "Enable Port ETS failed, err %s aq_err %s\n",
7108 			 i40e_stat_str(&pf->hw, err),
7109 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7110 		err = -ENOENT;
7111 		goto out;
7112 	}
7113 
7114 	/* Update the local cached instance with TC0 ETS */
7115 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7116 	dcb_cfg->etscfg.cbs = 0;
7117 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7118 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7119 
7120 out:
7121 	return err;
7122 }
7123 
7124 /**
7125  * i40e_init_pf_dcb - Initialize DCB configuration
7126  * @pf: PF being configured
7127  *
7128  * Query the current DCB configuration and cache it
7129  * in the hardware structure
7130  **/
i40e_init_pf_dcb(struct i40e_pf * pf)7131 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7132 {
7133 	struct i40e_hw *hw = &pf->hw;
7134 	int err;
7135 
7136 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7137 	 * Also do not enable DCBx if FW LLDP agent is disabled
7138 	 */
7139 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7140 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7141 		err = I40E_NOT_SUPPORTED;
7142 		goto out;
7143 	}
7144 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7145 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7146 		err = i40e_dcb_sw_default_config(pf);
7147 		if (err) {
7148 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7149 			goto out;
7150 		}
7151 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7152 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7153 			       DCB_CAP_DCBX_VER_IEEE;
7154 		/* at init capable but disabled */
7155 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7156 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7157 		goto out;
7158 	}
7159 	err = i40e_init_dcb(hw, true);
7160 	if (!err) {
7161 		/* Device/Function is not DCBX capable */
7162 		if ((!hw->func_caps.dcb) ||
7163 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7164 			dev_info(&pf->pdev->dev,
7165 				 "DCBX offload is not supported or is disabled for this PF.\n");
7166 		} else {
7167 			/* When status is not DISABLED then DCBX in FW */
7168 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7169 				       DCB_CAP_DCBX_VER_IEEE;
7170 
7171 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7172 			/* Enable DCB tagging only when more than one TC
7173 			 * or explicitly disable if only one TC
7174 			 */
7175 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7176 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7177 			else
7178 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7179 			dev_dbg(&pf->pdev->dev,
7180 				"DCBX offload is supported for this PF.\n");
7181 		}
7182 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7183 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7184 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7185 	} else {
7186 		dev_info(&pf->pdev->dev,
7187 			 "Query for DCB configuration failed, err %s aq_err %s\n",
7188 			 i40e_stat_str(&pf->hw, err),
7189 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7190 	}
7191 
7192 out:
7193 	return err;
7194 }
7195 #endif /* CONFIG_I40E_DCB */
7196 
7197 /**
7198  * i40e_print_link_message - print link up or down
7199  * @vsi: the VSI for which link needs a message
7200  * @isup: true of link is up, false otherwise
7201  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)7202 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7203 {
7204 	enum i40e_aq_link_speed new_speed;
7205 	struct i40e_pf *pf = vsi->back;
7206 	char *speed = "Unknown";
7207 	char *fc = "Unknown";
7208 	char *fec = "";
7209 	char *req_fec = "";
7210 	char *an = "";
7211 
7212 	if (isup)
7213 		new_speed = pf->hw.phy.link_info.link_speed;
7214 	else
7215 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7216 
7217 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7218 		return;
7219 	vsi->current_isup = isup;
7220 	vsi->current_speed = new_speed;
7221 	if (!isup) {
7222 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7223 		return;
7224 	}
7225 
7226 	/* Warn user if link speed on NPAR enabled partition is not at
7227 	 * least 10GB
7228 	 */
7229 	if (pf->hw.func_caps.npar_enable &&
7230 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7231 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7232 		netdev_warn(vsi->netdev,
7233 			    "The partition detected link speed that is less than 10Gbps\n");
7234 
7235 	switch (pf->hw.phy.link_info.link_speed) {
7236 	case I40E_LINK_SPEED_40GB:
7237 		speed = "40 G";
7238 		break;
7239 	case I40E_LINK_SPEED_20GB:
7240 		speed = "20 G";
7241 		break;
7242 	case I40E_LINK_SPEED_25GB:
7243 		speed = "25 G";
7244 		break;
7245 	case I40E_LINK_SPEED_10GB:
7246 		speed = "10 G";
7247 		break;
7248 	case I40E_LINK_SPEED_5GB:
7249 		speed = "5 G";
7250 		break;
7251 	case I40E_LINK_SPEED_2_5GB:
7252 		speed = "2.5 G";
7253 		break;
7254 	case I40E_LINK_SPEED_1GB:
7255 		speed = "1000 M";
7256 		break;
7257 	case I40E_LINK_SPEED_100MB:
7258 		speed = "100 M";
7259 		break;
7260 	default:
7261 		break;
7262 	}
7263 
7264 	switch (pf->hw.fc.current_mode) {
7265 	case I40E_FC_FULL:
7266 		fc = "RX/TX";
7267 		break;
7268 	case I40E_FC_TX_PAUSE:
7269 		fc = "TX";
7270 		break;
7271 	case I40E_FC_RX_PAUSE:
7272 		fc = "RX";
7273 		break;
7274 	default:
7275 		fc = "None";
7276 		break;
7277 	}
7278 
7279 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7280 		req_fec = "None";
7281 		fec = "None";
7282 		an = "False";
7283 
7284 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7285 			an = "True";
7286 
7287 		if (pf->hw.phy.link_info.fec_info &
7288 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7289 			fec = "CL74 FC-FEC/BASE-R";
7290 		else if (pf->hw.phy.link_info.fec_info &
7291 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7292 			fec = "CL108 RS-FEC";
7293 
7294 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7295 		 * both RS and FC are requested
7296 		 */
7297 		if (vsi->back->hw.phy.link_info.req_fec_info &
7298 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7299 			if (vsi->back->hw.phy.link_info.req_fec_info &
7300 			    I40E_AQ_REQUEST_FEC_RS)
7301 				req_fec = "CL108 RS-FEC";
7302 			else
7303 				req_fec = "CL74 FC-FEC/BASE-R";
7304 		}
7305 		netdev_info(vsi->netdev,
7306 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7307 			    speed, req_fec, fec, an, fc);
7308 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7309 		req_fec = "None";
7310 		fec = "None";
7311 		an = "False";
7312 
7313 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7314 			an = "True";
7315 
7316 		if (pf->hw.phy.link_info.fec_info &
7317 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7318 			fec = "CL74 FC-FEC/BASE-R";
7319 
7320 		if (pf->hw.phy.link_info.req_fec_info &
7321 		    I40E_AQ_REQUEST_FEC_KR)
7322 			req_fec = "CL74 FC-FEC/BASE-R";
7323 
7324 		netdev_info(vsi->netdev,
7325 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7326 			    speed, req_fec, fec, an, fc);
7327 	} else {
7328 		netdev_info(vsi->netdev,
7329 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7330 			    speed, fc);
7331 	}
7332 
7333 }
7334 
7335 /**
7336  * i40e_up_complete - Finish the last steps of bringing up a connection
7337  * @vsi: the VSI being configured
7338  **/
i40e_up_complete(struct i40e_vsi * vsi)7339 static int i40e_up_complete(struct i40e_vsi *vsi)
7340 {
7341 	struct i40e_pf *pf = vsi->back;
7342 	int err;
7343 
7344 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7345 		i40e_vsi_configure_msix(vsi);
7346 	else
7347 		i40e_configure_msi_and_legacy(vsi);
7348 
7349 	/* start rings */
7350 	err = i40e_vsi_start_rings(vsi);
7351 	if (err)
7352 		return err;
7353 
7354 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7355 	i40e_napi_enable_all(vsi);
7356 	i40e_vsi_enable_irq(vsi);
7357 
7358 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7359 	    (vsi->netdev)) {
7360 		i40e_print_link_message(vsi, true);
7361 		netif_tx_start_all_queues(vsi->netdev);
7362 		netif_carrier_on(vsi->netdev);
7363 	}
7364 
7365 	/* replay FDIR SB filters */
7366 	if (vsi->type == I40E_VSI_FDIR) {
7367 		/* reset fd counters */
7368 		pf->fd_add_err = 0;
7369 		pf->fd_atr_cnt = 0;
7370 		i40e_fdir_filter_restore(vsi);
7371 	}
7372 
7373 	/* On the next run of the service_task, notify any clients of the new
7374 	 * opened netdev
7375 	 */
7376 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7377 	i40e_service_event_schedule(pf);
7378 
7379 	return 0;
7380 }
7381 
7382 /**
7383  * i40e_vsi_reinit_locked - Reset the VSI
7384  * @vsi: the VSI being configured
7385  *
7386  * Rebuild the ring structs after some configuration
7387  * has changed, e.g. MTU size.
7388  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)7389 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7390 {
7391 	struct i40e_pf *pf = vsi->back;
7392 
7393 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7394 		usleep_range(1000, 2000);
7395 	i40e_down(vsi);
7396 
7397 	i40e_up(vsi);
7398 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7399 }
7400 
7401 /**
7402  * i40e_force_link_state - Force the link status
7403  * @pf: board private structure
7404  * @is_up: whether the link state should be forced up or down
7405  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)7406 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7407 {
7408 	struct i40e_aq_get_phy_abilities_resp abilities;
7409 	struct i40e_aq_set_phy_config config = {0};
7410 	bool non_zero_phy_type = is_up;
7411 	struct i40e_hw *hw = &pf->hw;
7412 	i40e_status err;
7413 	u64 mask;
7414 	u8 speed;
7415 
7416 	/* Card might've been put in an unstable state by other drivers
7417 	 * and applications, which causes incorrect speed values being
7418 	 * set on startup. In order to clear speed registers, we call
7419 	 * get_phy_capabilities twice, once to get initial state of
7420 	 * available speeds, and once to get current PHY config.
7421 	 */
7422 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7423 					   NULL);
7424 	if (err) {
7425 		dev_err(&pf->pdev->dev,
7426 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7427 			i40e_stat_str(hw, err),
7428 			i40e_aq_str(hw, hw->aq.asq_last_status));
7429 		return err;
7430 	}
7431 	speed = abilities.link_speed;
7432 
7433 	/* Get the current phy config */
7434 	err = i40e_aq_get_phy_capabilities(hw, false, false, &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 
7444 	/* If link needs to go up, but was not forced to go down,
7445 	 * and its speed values are OK, no need for a flap
7446 	 * if non_zero_phy_type was set, still need to force up
7447 	 */
7448 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7449 		non_zero_phy_type = true;
7450 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7451 		return I40E_SUCCESS;
7452 
7453 	/* To force link we need to set bits for all supported PHY types,
7454 	 * but there are now more than 32, so we need to split the bitmap
7455 	 * across two fields.
7456 	 */
7457 	mask = I40E_PHY_TYPES_BITMASK;
7458 	config.phy_type =
7459 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7460 	config.phy_type_ext =
7461 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7462 	/* Copy the old settings, except of phy_type */
7463 	config.abilities = abilities.abilities;
7464 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7465 		if (is_up)
7466 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7467 		else
7468 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7469 	}
7470 	if (abilities.link_speed != 0)
7471 		config.link_speed = abilities.link_speed;
7472 	else
7473 		config.link_speed = speed;
7474 	config.eee_capability = abilities.eee_capability;
7475 	config.eeer = abilities.eeer_val;
7476 	config.low_power_ctrl = abilities.d3_lpan;
7477 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7478 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7479 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7480 
7481 	if (err) {
7482 		dev_err(&pf->pdev->dev,
7483 			"set phy config ret =  %s last_status =  %s\n",
7484 			i40e_stat_str(&pf->hw, err),
7485 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7486 		return err;
7487 	}
7488 
7489 	/* Update the link info */
7490 	err = i40e_update_link_info(hw);
7491 	if (err) {
7492 		/* Wait a little bit (on 40G cards it sometimes takes a really
7493 		 * long time for link to come back from the atomic reset)
7494 		 * and try once more
7495 		 */
7496 		msleep(1000);
7497 		i40e_update_link_info(hw);
7498 	}
7499 
7500 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7501 
7502 	return I40E_SUCCESS;
7503 }
7504 
7505 /**
7506  * i40e_up - Bring the connection back up after being down
7507  * @vsi: the VSI being configured
7508  **/
i40e_up(struct i40e_vsi * vsi)7509 int i40e_up(struct i40e_vsi *vsi)
7510 {
7511 	int err;
7512 
7513 	if (vsi->type == I40E_VSI_MAIN &&
7514 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7515 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7516 		i40e_force_link_state(vsi->back, true);
7517 
7518 	err = i40e_vsi_configure(vsi);
7519 	if (!err)
7520 		err = i40e_up_complete(vsi);
7521 
7522 	return err;
7523 }
7524 
7525 /**
7526  * i40e_down - Shutdown the connection processing
7527  * @vsi: the VSI being stopped
7528  **/
i40e_down(struct i40e_vsi * vsi)7529 void i40e_down(struct i40e_vsi *vsi)
7530 {
7531 	int i;
7532 
7533 	/* It is assumed that the caller of this function
7534 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7535 	 */
7536 	if (vsi->netdev) {
7537 		netif_carrier_off(vsi->netdev);
7538 		netif_tx_disable(vsi->netdev);
7539 	}
7540 	i40e_vsi_disable_irq(vsi);
7541 	i40e_vsi_stop_rings(vsi);
7542 	if (vsi->type == I40E_VSI_MAIN &&
7543 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7544 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7545 		i40e_force_link_state(vsi->back, false);
7546 	i40e_napi_disable_all(vsi);
7547 
7548 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7549 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7550 		if (i40e_enabled_xdp_vsi(vsi)) {
7551 			/* Make sure that in-progress ndo_xdp_xmit and
7552 			 * ndo_xsk_wakeup calls are completed.
7553 			 */
7554 			synchronize_rcu();
7555 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7556 		}
7557 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7558 	}
7559 
7560 }
7561 
7562 /**
7563  * i40e_validate_mqprio_qopt- validate queue mapping info
7564  * @vsi: the VSI being configured
7565  * @mqprio_qopt: queue parametrs
7566  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7567 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7568 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7569 {
7570 	u64 sum_max_rate = 0;
7571 	u64 max_rate = 0;
7572 	int i;
7573 
7574 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7575 	    mqprio_qopt->qopt.num_tc < 1 ||
7576 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7577 		return -EINVAL;
7578 	for (i = 0; ; i++) {
7579 		if (!mqprio_qopt->qopt.count[i])
7580 			return -EINVAL;
7581 		if (mqprio_qopt->min_rate[i]) {
7582 			dev_err(&vsi->back->pdev->dev,
7583 				"Invalid min tx rate (greater than 0) specified\n");
7584 			return -EINVAL;
7585 		}
7586 		max_rate = mqprio_qopt->max_rate[i];
7587 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7588 		sum_max_rate += max_rate;
7589 
7590 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7591 			break;
7592 		if (mqprio_qopt->qopt.offset[i + 1] !=
7593 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7594 			return -EINVAL;
7595 	}
7596 	if (vsi->num_queue_pairs <
7597 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7598 		dev_err(&vsi->back->pdev->dev,
7599 			"Failed to create traffic channel, insufficient number of queues.\n");
7600 		return -EINVAL;
7601 	}
7602 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7603 		dev_err(&vsi->back->pdev->dev,
7604 			"Invalid max tx rate specified\n");
7605 		return -EINVAL;
7606 	}
7607 	return 0;
7608 }
7609 
7610 /**
7611  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7612  * @vsi: the VSI being configured
7613  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7614 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7615 {
7616 	u16 qcount;
7617 	int i;
7618 
7619 	/* Only TC0 is enabled */
7620 	vsi->tc_config.numtc = 1;
7621 	vsi->tc_config.enabled_tc = 1;
7622 	qcount = min_t(int, vsi->alloc_queue_pairs,
7623 		       i40e_pf_get_max_q_per_tc(vsi->back));
7624 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7625 		/* For the TC that is not enabled set the offset to default
7626 		 * queue and allocate one queue for the given TC.
7627 		 */
7628 		vsi->tc_config.tc_info[i].qoffset = 0;
7629 		if (i == 0)
7630 			vsi->tc_config.tc_info[i].qcount = qcount;
7631 		else
7632 			vsi->tc_config.tc_info[i].qcount = 1;
7633 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7634 	}
7635 }
7636 
7637 /**
7638  * i40e_del_macvlan_filter
7639  * @hw: pointer to the HW structure
7640  * @seid: seid of the channel VSI
7641  * @macaddr: the mac address to apply as a filter
7642  * @aq_err: store the admin Q error
7643  *
7644  * This function deletes a mac filter on the channel VSI which serves as the
7645  * macvlan. Returns 0 on success.
7646  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7647 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7648 					   const u8 *macaddr, int *aq_err)
7649 {
7650 	struct i40e_aqc_remove_macvlan_element_data element;
7651 	i40e_status status;
7652 
7653 	memset(&element, 0, sizeof(element));
7654 	ether_addr_copy(element.mac_addr, macaddr);
7655 	element.vlan_tag = 0;
7656 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7657 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7658 	*aq_err = hw->aq.asq_last_status;
7659 
7660 	return status;
7661 }
7662 
7663 /**
7664  * i40e_add_macvlan_filter
7665  * @hw: pointer to the HW structure
7666  * @seid: seid of the channel VSI
7667  * @macaddr: the mac address to apply as a filter
7668  * @aq_err: store the admin Q error
7669  *
7670  * This function adds a mac filter on the channel VSI which serves as the
7671  * macvlan. Returns 0 on success.
7672  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7673 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7674 					   const u8 *macaddr, int *aq_err)
7675 {
7676 	struct i40e_aqc_add_macvlan_element_data element;
7677 	i40e_status status;
7678 	u16 cmd_flags = 0;
7679 
7680 	ether_addr_copy(element.mac_addr, macaddr);
7681 	element.vlan_tag = 0;
7682 	element.queue_number = 0;
7683 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7684 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7685 	element.flags = cpu_to_le16(cmd_flags);
7686 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7687 	*aq_err = hw->aq.asq_last_status;
7688 
7689 	return status;
7690 }
7691 
7692 /**
7693  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7694  * @vsi: the VSI we want to access
7695  * @ch: the channel we want to access
7696  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7697 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7698 {
7699 	struct i40e_ring *tx_ring, *rx_ring;
7700 	u16 pf_q;
7701 	int i;
7702 
7703 	for (i = 0; i < ch->num_queue_pairs; i++) {
7704 		pf_q = ch->base_queue + i;
7705 		tx_ring = vsi->tx_rings[pf_q];
7706 		tx_ring->ch = NULL;
7707 		rx_ring = vsi->rx_rings[pf_q];
7708 		rx_ring->ch = NULL;
7709 	}
7710 }
7711 
7712 /**
7713  * i40e_free_macvlan_channels
7714  * @vsi: the VSI we want to access
7715  *
7716  * This function frees the Qs of the channel VSI from
7717  * the stack and also deletes the channel VSIs which
7718  * serve as macvlans.
7719  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7720 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7721 {
7722 	struct i40e_channel *ch, *ch_tmp;
7723 	int ret;
7724 
7725 	if (list_empty(&vsi->macvlan_list))
7726 		return;
7727 
7728 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7729 		struct i40e_vsi *parent_vsi;
7730 
7731 		if (i40e_is_channel_macvlan(ch)) {
7732 			i40e_reset_ch_rings(vsi, ch);
7733 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7734 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7735 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7736 			kfree(ch->fwd);
7737 			ch->fwd = NULL;
7738 		}
7739 
7740 		list_del(&ch->list);
7741 		parent_vsi = ch->parent_vsi;
7742 		if (!parent_vsi || !ch->initialized) {
7743 			kfree(ch);
7744 			continue;
7745 		}
7746 
7747 		/* remove the VSI */
7748 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7749 					     NULL);
7750 		if (ret)
7751 			dev_err(&vsi->back->pdev->dev,
7752 				"unable to remove channel (%d) for parent VSI(%d)\n",
7753 				ch->seid, parent_vsi->seid);
7754 		kfree(ch);
7755 	}
7756 	vsi->macvlan_cnt = 0;
7757 }
7758 
7759 /**
7760  * i40e_fwd_ring_up - bring the macvlan device up
7761  * @vsi: the VSI we want to access
7762  * @vdev: macvlan netdevice
7763  * @fwd: the private fwd structure
7764  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7765 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7766 			    struct i40e_fwd_adapter *fwd)
7767 {
7768 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7769 	int ret = 0, num_tc = 1,  i, aq_err;
7770 	struct i40e_pf *pf = vsi->back;
7771 	struct i40e_hw *hw = &pf->hw;
7772 
7773 	/* Go through the list and find an available channel */
7774 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7775 		if (!i40e_is_channel_macvlan(iter)) {
7776 			iter->fwd = fwd;
7777 			/* record configuration for macvlan interface in vdev */
7778 			for (i = 0; i < num_tc; i++)
7779 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7780 							     i,
7781 							     iter->num_queue_pairs,
7782 							     iter->base_queue);
7783 			for (i = 0; i < iter->num_queue_pairs; i++) {
7784 				struct i40e_ring *tx_ring, *rx_ring;
7785 				u16 pf_q;
7786 
7787 				pf_q = iter->base_queue + i;
7788 
7789 				/* Get to TX ring ptr */
7790 				tx_ring = vsi->tx_rings[pf_q];
7791 				tx_ring->ch = iter;
7792 
7793 				/* Get the RX ring ptr */
7794 				rx_ring = vsi->rx_rings[pf_q];
7795 				rx_ring->ch = iter;
7796 			}
7797 			ch = iter;
7798 			break;
7799 		}
7800 	}
7801 
7802 	if (!ch)
7803 		return -EINVAL;
7804 
7805 	/* Guarantee all rings are updated before we update the
7806 	 * MAC address filter.
7807 	 */
7808 	wmb();
7809 
7810 	/* Add a mac filter */
7811 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7812 	if (ret) {
7813 		/* if we cannot add the MAC rule then disable the offload */
7814 		macvlan_release_l2fw_offload(vdev);
7815 		for (i = 0; i < ch->num_queue_pairs; i++) {
7816 			struct i40e_ring *rx_ring;
7817 			u16 pf_q;
7818 
7819 			pf_q = ch->base_queue + i;
7820 			rx_ring = vsi->rx_rings[pf_q];
7821 			rx_ring->netdev = NULL;
7822 		}
7823 		dev_info(&pf->pdev->dev,
7824 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7825 			  i40e_stat_str(hw, ret),
7826 			  i40e_aq_str(hw, aq_err));
7827 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7828 	}
7829 
7830 	return ret;
7831 }
7832 
7833 /**
7834  * i40e_setup_macvlans - create the channels which will be macvlans
7835  * @vsi: the VSI we want to access
7836  * @macvlan_cnt: no. of macvlans to be setup
7837  * @qcnt: no. of Qs per macvlan
7838  * @vdev: macvlan netdevice
7839  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7840 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7841 			       struct net_device *vdev)
7842 {
7843 	struct i40e_pf *pf = vsi->back;
7844 	struct i40e_hw *hw = &pf->hw;
7845 	struct i40e_vsi_context ctxt;
7846 	u16 sections, qmap, num_qps;
7847 	struct i40e_channel *ch;
7848 	int i, pow, ret = 0;
7849 	u8 offset = 0;
7850 
7851 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7852 		return -EINVAL;
7853 
7854 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7855 
7856 	/* find the next higher power-of-2 of num queue pairs */
7857 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7858 
7859 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7860 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7861 
7862 	/* Setup context bits for the main VSI */
7863 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7864 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7865 	memset(&ctxt, 0, sizeof(ctxt));
7866 	ctxt.seid = vsi->seid;
7867 	ctxt.pf_num = vsi->back->hw.pf_id;
7868 	ctxt.vf_num = 0;
7869 	ctxt.uplink_seid = vsi->uplink_seid;
7870 	ctxt.info = vsi->info;
7871 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7872 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7873 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7874 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7875 
7876 	/* Reconfigure RSS for main VSI with new max queue count */
7877 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7878 	ret = i40e_vsi_config_rss(vsi);
7879 	if (ret) {
7880 		dev_info(&pf->pdev->dev,
7881 			 "Failed to reconfig RSS for num_queues (%u)\n",
7882 			 vsi->rss_size);
7883 		return ret;
7884 	}
7885 	vsi->reconfig_rss = true;
7886 	dev_dbg(&vsi->back->pdev->dev,
7887 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7888 	vsi->next_base_queue = num_qps;
7889 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7890 
7891 	/* Update the VSI after updating the VSI queue-mapping
7892 	 * information
7893 	 */
7894 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7895 	if (ret) {
7896 		dev_info(&pf->pdev->dev,
7897 			 "Update vsi tc config failed, err %s aq_err %s\n",
7898 			 i40e_stat_str(hw, ret),
7899 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7900 		return ret;
7901 	}
7902 	/* update the local VSI info with updated queue map */
7903 	i40e_vsi_update_queue_map(vsi, &ctxt);
7904 	vsi->info.valid_sections = 0;
7905 
7906 	/* Create channels for macvlans */
7907 	INIT_LIST_HEAD(&vsi->macvlan_list);
7908 	for (i = 0; i < macvlan_cnt; i++) {
7909 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7910 		if (!ch) {
7911 			ret = -ENOMEM;
7912 			goto err_free;
7913 		}
7914 		INIT_LIST_HEAD(&ch->list);
7915 		ch->num_queue_pairs = qcnt;
7916 		if (!i40e_setup_channel(pf, vsi, ch)) {
7917 			ret = -EINVAL;
7918 			kfree(ch);
7919 			goto err_free;
7920 		}
7921 		ch->parent_vsi = vsi;
7922 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7923 		vsi->macvlan_cnt++;
7924 		list_add_tail(&ch->list, &vsi->macvlan_list);
7925 	}
7926 
7927 	return ret;
7928 
7929 err_free:
7930 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7931 	i40e_free_macvlan_channels(vsi);
7932 
7933 	return ret;
7934 }
7935 
7936 /**
7937  * i40e_fwd_add - configure macvlans
7938  * @netdev: net device to configure
7939  * @vdev: macvlan netdevice
7940  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)7941 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7942 {
7943 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7944 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7945 	struct i40e_vsi *vsi = np->vsi;
7946 	struct i40e_pf *pf = vsi->back;
7947 	struct i40e_fwd_adapter *fwd;
7948 	int avail_macvlan, ret;
7949 
7950 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7951 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7952 		return ERR_PTR(-EINVAL);
7953 	}
7954 	if (i40e_is_tc_mqprio_enabled(pf)) {
7955 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7956 		return ERR_PTR(-EINVAL);
7957 	}
7958 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7959 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7960 		return ERR_PTR(-EINVAL);
7961 	}
7962 
7963 	/* The macvlan device has to be a single Q device so that the
7964 	 * tc_to_txq field can be reused to pick the tx queue.
7965 	 */
7966 	if (netif_is_multiqueue(vdev))
7967 		return ERR_PTR(-ERANGE);
7968 
7969 	if (!vsi->macvlan_cnt) {
7970 		/* reserve bit 0 for the pf device */
7971 		set_bit(0, vsi->fwd_bitmask);
7972 
7973 		/* Try to reserve as many queues as possible for macvlans. First
7974 		 * reserve 3/4th of max vectors, then half, then quarter and
7975 		 * calculate Qs per macvlan as you go
7976 		 */
7977 		vectors = pf->num_lan_msix;
7978 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7979 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7980 			q_per_macvlan = 4;
7981 			macvlan_cnt = (vectors - 32) / 4;
7982 		} else if (vectors <= 64 && vectors > 32) {
7983 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7984 			q_per_macvlan = 2;
7985 			macvlan_cnt = (vectors - 16) / 2;
7986 		} else if (vectors <= 32 && vectors > 16) {
7987 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7988 			q_per_macvlan = 1;
7989 			macvlan_cnt = vectors - 16;
7990 		} else if (vectors <= 16 && vectors > 8) {
7991 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7992 			q_per_macvlan = 1;
7993 			macvlan_cnt = vectors - 8;
7994 		} else {
7995 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7996 			q_per_macvlan = 1;
7997 			macvlan_cnt = vectors - 1;
7998 		}
7999 
8000 		if (macvlan_cnt == 0)
8001 			return ERR_PTR(-EBUSY);
8002 
8003 		/* Quiesce VSI queues */
8004 		i40e_quiesce_vsi(vsi);
8005 
8006 		/* sets up the macvlans but does not "enable" them */
8007 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8008 					  vdev);
8009 		if (ret)
8010 			return ERR_PTR(ret);
8011 
8012 		/* Unquiesce VSI */
8013 		i40e_unquiesce_vsi(vsi);
8014 	}
8015 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8016 					    vsi->macvlan_cnt);
8017 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8018 		return ERR_PTR(-EBUSY);
8019 
8020 	/* create the fwd struct */
8021 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8022 	if (!fwd)
8023 		return ERR_PTR(-ENOMEM);
8024 
8025 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8026 	fwd->bit_no = avail_macvlan;
8027 	netdev_set_sb_channel(vdev, avail_macvlan);
8028 	fwd->netdev = vdev;
8029 
8030 	if (!netif_running(netdev))
8031 		return fwd;
8032 
8033 	/* Set fwd ring up */
8034 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8035 	if (ret) {
8036 		/* unbind the queues and drop the subordinate channel config */
8037 		netdev_unbind_sb_channel(netdev, vdev);
8038 		netdev_set_sb_channel(vdev, 0);
8039 
8040 		kfree(fwd);
8041 		return ERR_PTR(-EINVAL);
8042 	}
8043 
8044 	return fwd;
8045 }
8046 
8047 /**
8048  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8049  * @vsi: the VSI we want to access
8050  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)8051 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8052 {
8053 	struct i40e_channel *ch, *ch_tmp;
8054 	struct i40e_pf *pf = vsi->back;
8055 	struct i40e_hw *hw = &pf->hw;
8056 	int aq_err, ret = 0;
8057 
8058 	if (list_empty(&vsi->macvlan_list))
8059 		return;
8060 
8061 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8062 		if (i40e_is_channel_macvlan(ch)) {
8063 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8064 						      i40e_channel_mac(ch),
8065 						      &aq_err);
8066 			if (!ret) {
8067 				/* Reset queue contexts */
8068 				i40e_reset_ch_rings(vsi, ch);
8069 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8070 				netdev_unbind_sb_channel(vsi->netdev,
8071 							 ch->fwd->netdev);
8072 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8073 				kfree(ch->fwd);
8074 				ch->fwd = NULL;
8075 			}
8076 		}
8077 	}
8078 }
8079 
8080 /**
8081  * i40e_fwd_del - delete macvlan interfaces
8082  * @netdev: net device to configure
8083  * @vdev: macvlan netdevice
8084  */
i40e_fwd_del(struct net_device * netdev,void * vdev)8085 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8086 {
8087 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8088 	struct i40e_fwd_adapter *fwd = vdev;
8089 	struct i40e_channel *ch, *ch_tmp;
8090 	struct i40e_vsi *vsi = np->vsi;
8091 	struct i40e_pf *pf = vsi->back;
8092 	struct i40e_hw *hw = &pf->hw;
8093 	int aq_err, ret = 0;
8094 
8095 	/* Find the channel associated with the macvlan and del mac filter */
8096 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8097 		if (i40e_is_channel_macvlan(ch) &&
8098 		    ether_addr_equal(i40e_channel_mac(ch),
8099 				     fwd->netdev->dev_addr)) {
8100 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8101 						      i40e_channel_mac(ch),
8102 						      &aq_err);
8103 			if (!ret) {
8104 				/* Reset queue contexts */
8105 				i40e_reset_ch_rings(vsi, ch);
8106 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8107 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8108 				netdev_set_sb_channel(fwd->netdev, 0);
8109 				kfree(ch->fwd);
8110 				ch->fwd = NULL;
8111 			} else {
8112 				dev_info(&pf->pdev->dev,
8113 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8114 					  i40e_stat_str(hw, ret),
8115 					  i40e_aq_str(hw, aq_err));
8116 			}
8117 			break;
8118 		}
8119 	}
8120 }
8121 
8122 /**
8123  * i40e_setup_tc - configure multiple traffic classes
8124  * @netdev: net device to configure
8125  * @type_data: tc offload data
8126  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)8127 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8128 {
8129 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8130 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8131 	struct i40e_vsi *vsi = np->vsi;
8132 	struct i40e_pf *pf = vsi->back;
8133 	u8 enabled_tc = 0, num_tc, hw;
8134 	bool need_reset = false;
8135 	int old_queue_pairs;
8136 	int ret = -EINVAL;
8137 	u16 mode;
8138 	int i;
8139 
8140 	old_queue_pairs = vsi->num_queue_pairs;
8141 	num_tc = mqprio_qopt->qopt.num_tc;
8142 	hw = mqprio_qopt->qopt.hw;
8143 	mode = mqprio_qopt->mode;
8144 	if (!hw) {
8145 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8146 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8147 		goto config_tc;
8148 	}
8149 
8150 	/* Check if MFP enabled */
8151 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8152 		netdev_info(netdev,
8153 			    "Configuring TC not supported in MFP mode\n");
8154 		return ret;
8155 	}
8156 	switch (mode) {
8157 	case TC_MQPRIO_MODE_DCB:
8158 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8159 
8160 		/* Check if DCB enabled to continue */
8161 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8162 			netdev_info(netdev,
8163 				    "DCB is not enabled for adapter\n");
8164 			return ret;
8165 		}
8166 
8167 		/* Check whether tc count is within enabled limit */
8168 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8169 			netdev_info(netdev,
8170 				    "TC count greater than enabled on link for adapter\n");
8171 			return ret;
8172 		}
8173 		break;
8174 	case TC_MQPRIO_MODE_CHANNEL:
8175 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8176 			netdev_info(netdev,
8177 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8178 			return ret;
8179 		}
8180 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8181 			return ret;
8182 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8183 		if (ret)
8184 			return ret;
8185 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8186 		       sizeof(*mqprio_qopt));
8187 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8188 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8189 		break;
8190 	default:
8191 		return -EINVAL;
8192 	}
8193 
8194 config_tc:
8195 	/* Generate TC map for number of tc requested */
8196 	for (i = 0; i < num_tc; i++)
8197 		enabled_tc |= BIT(i);
8198 
8199 	/* Requesting same TC configuration as already enabled */
8200 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8201 	    mode != TC_MQPRIO_MODE_CHANNEL)
8202 		return 0;
8203 
8204 	/* Quiesce VSI queues */
8205 	i40e_quiesce_vsi(vsi);
8206 
8207 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8208 		i40e_remove_queue_channels(vsi);
8209 
8210 	/* Configure VSI for enabled TCs */
8211 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8212 	if (ret) {
8213 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8214 			    vsi->seid);
8215 		need_reset = true;
8216 		goto exit;
8217 	} else if (enabled_tc &&
8218 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8219 		netdev_info(netdev,
8220 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8221 			    vsi->tc_config.tc_info[0].qcount);
8222 		ret = -EINVAL;
8223 		need_reset = true;
8224 		goto exit;
8225 	}
8226 
8227 	dev_info(&vsi->back->pdev->dev,
8228 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8229 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8230 
8231 	if (i40e_is_tc_mqprio_enabled(pf)) {
8232 		if (vsi->mqprio_qopt.max_rate[0]) {
8233 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8234 						  vsi->mqprio_qopt.max_rate[0]);
8235 
8236 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8237 			if (!ret) {
8238 				u64 credits = max_tx_rate;
8239 
8240 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8241 				dev_dbg(&vsi->back->pdev->dev,
8242 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8243 					max_tx_rate,
8244 					credits,
8245 					vsi->seid);
8246 			} else {
8247 				need_reset = true;
8248 				goto exit;
8249 			}
8250 		}
8251 		ret = i40e_configure_queue_channels(vsi);
8252 		if (ret) {
8253 			vsi->num_queue_pairs = old_queue_pairs;
8254 			netdev_info(netdev,
8255 				    "Failed configuring queue channels\n");
8256 			need_reset = true;
8257 			goto exit;
8258 		}
8259 	}
8260 
8261 exit:
8262 	/* Reset the configuration data to defaults, only TC0 is enabled */
8263 	if (need_reset) {
8264 		i40e_vsi_set_default_tc_config(vsi);
8265 		need_reset = false;
8266 	}
8267 
8268 	/* Unquiesce VSI */
8269 	i40e_unquiesce_vsi(vsi);
8270 	return ret;
8271 }
8272 
8273 /**
8274  * i40e_set_cld_element - sets cloud filter element data
8275  * @filter: cloud filter rule
8276  * @cld: ptr to cloud filter element data
8277  *
8278  * This is helper function to copy data into cloud filter element
8279  **/
8280 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)8281 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8282 		     struct i40e_aqc_cloud_filters_element_data *cld)
8283 {
8284 	u32 ipa;
8285 	int i;
8286 
8287 	memset(cld, 0, sizeof(*cld));
8288 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8289 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8290 
8291 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8292 		return;
8293 
8294 	if (filter->n_proto == ETH_P_IPV6) {
8295 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8296 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8297 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8298 
8299 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8300 		}
8301 	} else {
8302 		ipa = be32_to_cpu(filter->dst_ipv4);
8303 
8304 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8305 	}
8306 
8307 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8308 
8309 	/* tenant_id is not supported by FW now, once the support is enabled
8310 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8311 	 */
8312 	if (filter->tenant_id)
8313 		return;
8314 }
8315 
8316 /**
8317  * i40e_add_del_cloud_filter - Add/del cloud filter
8318  * @vsi: pointer to VSI
8319  * @filter: cloud filter rule
8320  * @add: if true, add, if false, delete
8321  *
8322  * Add or delete a cloud filter for a specific flow spec.
8323  * Returns 0 if the filter were successfully added.
8324  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8325 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8326 			      struct i40e_cloud_filter *filter, bool add)
8327 {
8328 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8329 	struct i40e_pf *pf = vsi->back;
8330 	int ret;
8331 	static const u16 flag_table[128] = {
8332 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8333 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8334 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8335 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8336 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8337 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8338 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8339 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8340 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8341 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8342 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8343 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8344 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8345 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8346 	};
8347 
8348 	if (filter->flags >= ARRAY_SIZE(flag_table))
8349 		return I40E_ERR_CONFIG;
8350 
8351 	memset(&cld_filter, 0, sizeof(cld_filter));
8352 
8353 	/* copy element needed to add cloud filter from filter */
8354 	i40e_set_cld_element(filter, &cld_filter);
8355 
8356 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8357 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8358 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8359 
8360 	if (filter->n_proto == ETH_P_IPV6)
8361 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8362 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8363 	else
8364 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8365 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8366 
8367 	if (add)
8368 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8369 						&cld_filter, 1);
8370 	else
8371 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8372 						&cld_filter, 1);
8373 	if (ret)
8374 		dev_dbg(&pf->pdev->dev,
8375 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8376 			add ? "add" : "delete", filter->dst_port, ret,
8377 			pf->hw.aq.asq_last_status);
8378 	else
8379 		dev_info(&pf->pdev->dev,
8380 			 "%s cloud filter for VSI: %d\n",
8381 			 add ? "Added" : "Deleted", filter->seid);
8382 	return ret;
8383 }
8384 
8385 /**
8386  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8387  * @vsi: pointer to VSI
8388  * @filter: cloud filter rule
8389  * @add: if true, add, if false, delete
8390  *
8391  * Add or delete a cloud filter for a specific flow spec using big buffer.
8392  * Returns 0 if the filter were successfully added.
8393  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8394 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8395 				      struct i40e_cloud_filter *filter,
8396 				      bool add)
8397 {
8398 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8399 	struct i40e_pf *pf = vsi->back;
8400 	int ret;
8401 
8402 	/* Both (src/dst) valid mac_addr are not supported */
8403 	if ((is_valid_ether_addr(filter->dst_mac) &&
8404 	     is_valid_ether_addr(filter->src_mac)) ||
8405 	    (is_multicast_ether_addr(filter->dst_mac) &&
8406 	     is_multicast_ether_addr(filter->src_mac)))
8407 		return -EOPNOTSUPP;
8408 
8409 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8410 	 * ports are not supported via big buffer now.
8411 	 */
8412 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8413 		return -EOPNOTSUPP;
8414 
8415 	/* adding filter using src_port/src_ip is not supported at this stage */
8416 	if (filter->src_port ||
8417 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8418 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8419 		return -EOPNOTSUPP;
8420 
8421 	memset(&cld_filter, 0, sizeof(cld_filter));
8422 
8423 	/* copy element needed to add cloud filter from filter */
8424 	i40e_set_cld_element(filter, &cld_filter.element);
8425 
8426 	if (is_valid_ether_addr(filter->dst_mac) ||
8427 	    is_valid_ether_addr(filter->src_mac) ||
8428 	    is_multicast_ether_addr(filter->dst_mac) ||
8429 	    is_multicast_ether_addr(filter->src_mac)) {
8430 		/* MAC + IP : unsupported mode */
8431 		if (filter->dst_ipv4)
8432 			return -EOPNOTSUPP;
8433 
8434 		/* since we validated that L4 port must be valid before
8435 		 * we get here, start with respective "flags" value
8436 		 * and update if vlan is present or not
8437 		 */
8438 		cld_filter.element.flags =
8439 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8440 
8441 		if (filter->vlan_id) {
8442 			cld_filter.element.flags =
8443 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8444 		}
8445 
8446 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8447 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8448 		cld_filter.element.flags =
8449 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8450 		if (filter->n_proto == ETH_P_IPV6)
8451 			cld_filter.element.flags |=
8452 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8453 		else
8454 			cld_filter.element.flags |=
8455 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8456 	} else {
8457 		dev_err(&pf->pdev->dev,
8458 			"either mac or ip has to be valid for cloud filter\n");
8459 		return -EINVAL;
8460 	}
8461 
8462 	/* Now copy L4 port in Byte 6..7 in general fields */
8463 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8464 						be16_to_cpu(filter->dst_port);
8465 
8466 	if (add) {
8467 		/* Validate current device switch mode, change if necessary */
8468 		ret = i40e_validate_and_set_switch_mode(vsi);
8469 		if (ret) {
8470 			dev_err(&pf->pdev->dev,
8471 				"failed to set switch mode, ret %d\n",
8472 				ret);
8473 			return ret;
8474 		}
8475 
8476 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8477 						   &cld_filter, 1);
8478 	} else {
8479 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8480 						   &cld_filter, 1);
8481 	}
8482 
8483 	if (ret)
8484 		dev_dbg(&pf->pdev->dev,
8485 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8486 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8487 	else
8488 		dev_info(&pf->pdev->dev,
8489 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8490 			 add ? "add" : "delete", filter->seid,
8491 			 ntohs(filter->dst_port));
8492 	return ret;
8493 }
8494 
8495 /**
8496  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8497  * @vsi: Pointer to VSI
8498  * @f: Pointer to struct flow_cls_offload
8499  * @filter: Pointer to cloud filter structure
8500  *
8501  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)8502 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8503 				 struct flow_cls_offload *f,
8504 				 struct i40e_cloud_filter *filter)
8505 {
8506 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8507 	struct flow_dissector *dissector = rule->match.dissector;
8508 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8509 	struct i40e_pf *pf = vsi->back;
8510 	u8 field_flags = 0;
8511 
8512 	if (dissector->used_keys &
8513 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8514 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8515 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8516 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8517 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8518 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8519 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8520 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8521 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8522 			dissector->used_keys);
8523 		return -EOPNOTSUPP;
8524 	}
8525 
8526 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8527 		struct flow_match_enc_keyid match;
8528 
8529 		flow_rule_match_enc_keyid(rule, &match);
8530 		if (match.mask->keyid != 0)
8531 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8532 
8533 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8534 	}
8535 
8536 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8537 		struct flow_match_basic match;
8538 
8539 		flow_rule_match_basic(rule, &match);
8540 		n_proto_key = ntohs(match.key->n_proto);
8541 		n_proto_mask = ntohs(match.mask->n_proto);
8542 
8543 		if (n_proto_key == ETH_P_ALL) {
8544 			n_proto_key = 0;
8545 			n_proto_mask = 0;
8546 		}
8547 		filter->n_proto = n_proto_key & n_proto_mask;
8548 		filter->ip_proto = match.key->ip_proto;
8549 	}
8550 
8551 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8552 		struct flow_match_eth_addrs match;
8553 
8554 		flow_rule_match_eth_addrs(rule, &match);
8555 
8556 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8557 		if (!is_zero_ether_addr(match.mask->dst)) {
8558 			if (is_broadcast_ether_addr(match.mask->dst)) {
8559 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8560 			} else {
8561 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8562 					match.mask->dst);
8563 				return I40E_ERR_CONFIG;
8564 			}
8565 		}
8566 
8567 		if (!is_zero_ether_addr(match.mask->src)) {
8568 			if (is_broadcast_ether_addr(match.mask->src)) {
8569 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8570 			} else {
8571 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8572 					match.mask->src);
8573 				return I40E_ERR_CONFIG;
8574 			}
8575 		}
8576 		ether_addr_copy(filter->dst_mac, match.key->dst);
8577 		ether_addr_copy(filter->src_mac, match.key->src);
8578 	}
8579 
8580 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8581 		struct flow_match_vlan match;
8582 
8583 		flow_rule_match_vlan(rule, &match);
8584 		if (match.mask->vlan_id) {
8585 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8586 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8587 
8588 			} else {
8589 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8590 					match.mask->vlan_id);
8591 				return I40E_ERR_CONFIG;
8592 			}
8593 		}
8594 
8595 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8596 	}
8597 
8598 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8599 		struct flow_match_control match;
8600 
8601 		flow_rule_match_control(rule, &match);
8602 		addr_type = match.key->addr_type;
8603 	}
8604 
8605 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8606 		struct flow_match_ipv4_addrs match;
8607 
8608 		flow_rule_match_ipv4_addrs(rule, &match);
8609 		if (match.mask->dst) {
8610 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8611 				field_flags |= I40E_CLOUD_FIELD_IIP;
8612 			} else {
8613 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8614 					&match.mask->dst);
8615 				return I40E_ERR_CONFIG;
8616 			}
8617 		}
8618 
8619 		if (match.mask->src) {
8620 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8621 				field_flags |= I40E_CLOUD_FIELD_IIP;
8622 			} else {
8623 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8624 					&match.mask->src);
8625 				return I40E_ERR_CONFIG;
8626 			}
8627 		}
8628 
8629 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8630 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8631 			return I40E_ERR_CONFIG;
8632 		}
8633 		filter->dst_ipv4 = match.key->dst;
8634 		filter->src_ipv4 = match.key->src;
8635 	}
8636 
8637 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8638 		struct flow_match_ipv6_addrs match;
8639 
8640 		flow_rule_match_ipv6_addrs(rule, &match);
8641 
8642 		/* src and dest IPV6 address should not be LOOPBACK
8643 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8644 		 */
8645 		if (ipv6_addr_loopback(&match.key->dst) ||
8646 		    ipv6_addr_loopback(&match.key->src)) {
8647 			dev_err(&pf->pdev->dev,
8648 				"Bad ipv6, addr is LOOPBACK\n");
8649 			return I40E_ERR_CONFIG;
8650 		}
8651 		if (!ipv6_addr_any(&match.mask->dst) ||
8652 		    !ipv6_addr_any(&match.mask->src))
8653 			field_flags |= I40E_CLOUD_FIELD_IIP;
8654 
8655 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8656 		       sizeof(filter->src_ipv6));
8657 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8658 		       sizeof(filter->dst_ipv6));
8659 	}
8660 
8661 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8662 		struct flow_match_ports match;
8663 
8664 		flow_rule_match_ports(rule, &match);
8665 		if (match.mask->src) {
8666 			if (match.mask->src == cpu_to_be16(0xffff)) {
8667 				field_flags |= I40E_CLOUD_FIELD_IIP;
8668 			} else {
8669 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8670 					be16_to_cpu(match.mask->src));
8671 				return I40E_ERR_CONFIG;
8672 			}
8673 		}
8674 
8675 		if (match.mask->dst) {
8676 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8677 				field_flags |= I40E_CLOUD_FIELD_IIP;
8678 			} else {
8679 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8680 					be16_to_cpu(match.mask->dst));
8681 				return I40E_ERR_CONFIG;
8682 			}
8683 		}
8684 
8685 		filter->dst_port = match.key->dst;
8686 		filter->src_port = match.key->src;
8687 
8688 		switch (filter->ip_proto) {
8689 		case IPPROTO_TCP:
8690 		case IPPROTO_UDP:
8691 			break;
8692 		default:
8693 			dev_err(&pf->pdev->dev,
8694 				"Only UDP and TCP transport are supported\n");
8695 			return -EINVAL;
8696 		}
8697 	}
8698 	filter->flags = field_flags;
8699 	return 0;
8700 }
8701 
8702 /**
8703  * i40e_handle_tclass: Forward to a traffic class on the device
8704  * @vsi: Pointer to VSI
8705  * @tc: traffic class index on the device
8706  * @filter: Pointer to cloud filter structure
8707  *
8708  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8709 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8710 			      struct i40e_cloud_filter *filter)
8711 {
8712 	struct i40e_channel *ch, *ch_tmp;
8713 
8714 	/* direct to a traffic class on the same device */
8715 	if (tc == 0) {
8716 		filter->seid = vsi->seid;
8717 		return 0;
8718 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8719 		if (!filter->dst_port) {
8720 			dev_err(&vsi->back->pdev->dev,
8721 				"Specify destination port to direct to traffic class that is not default\n");
8722 			return -EINVAL;
8723 		}
8724 		if (list_empty(&vsi->ch_list))
8725 			return -EINVAL;
8726 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8727 					 list) {
8728 			if (ch->seid == vsi->tc_seid_map[tc])
8729 				filter->seid = ch->seid;
8730 		}
8731 		return 0;
8732 	}
8733 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8734 	return -EINVAL;
8735 }
8736 
8737 /**
8738  * i40e_configure_clsflower - Configure tc flower filters
8739  * @vsi: Pointer to VSI
8740  * @cls_flower: Pointer to struct flow_cls_offload
8741  *
8742  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8743 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8744 				    struct flow_cls_offload *cls_flower)
8745 {
8746 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8747 	struct i40e_cloud_filter *filter = NULL;
8748 	struct i40e_pf *pf = vsi->back;
8749 	int err = 0;
8750 
8751 	if (tc < 0) {
8752 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8753 		return -EOPNOTSUPP;
8754 	}
8755 
8756 	if (!tc) {
8757 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8758 		return -EINVAL;
8759 	}
8760 
8761 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8762 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8763 		return -EBUSY;
8764 
8765 	if (pf->fdir_pf_active_filters ||
8766 	    (!hlist_empty(&pf->fdir_filter_list))) {
8767 		dev_err(&vsi->back->pdev->dev,
8768 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8769 		return -EINVAL;
8770 	}
8771 
8772 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8773 		dev_err(&vsi->back->pdev->dev,
8774 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8775 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8776 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8777 	}
8778 
8779 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8780 	if (!filter)
8781 		return -ENOMEM;
8782 
8783 	filter->cookie = cls_flower->cookie;
8784 
8785 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8786 	if (err < 0)
8787 		goto err;
8788 
8789 	err = i40e_handle_tclass(vsi, tc, filter);
8790 	if (err < 0)
8791 		goto err;
8792 
8793 	/* Add cloud filter */
8794 	if (filter->dst_port)
8795 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8796 	else
8797 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8798 
8799 	if (err) {
8800 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8801 			err);
8802 		goto err;
8803 	}
8804 
8805 	/* add filter to the ordered list */
8806 	INIT_HLIST_NODE(&filter->cloud_node);
8807 
8808 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8809 
8810 	pf->num_cloud_filters++;
8811 
8812 	return err;
8813 err:
8814 	kfree(filter);
8815 	return err;
8816 }
8817 
8818 /**
8819  * i40e_find_cloud_filter - Find the could filter in the list
8820  * @vsi: Pointer to VSI
8821  * @cookie: filter specific cookie
8822  *
8823  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8824 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8825 							unsigned long *cookie)
8826 {
8827 	struct i40e_cloud_filter *filter = NULL;
8828 	struct hlist_node *node2;
8829 
8830 	hlist_for_each_entry_safe(filter, node2,
8831 				  &vsi->back->cloud_filter_list, cloud_node)
8832 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8833 			return filter;
8834 	return NULL;
8835 }
8836 
8837 /**
8838  * i40e_delete_clsflower - Remove tc flower filters
8839  * @vsi: Pointer to VSI
8840  * @cls_flower: Pointer to struct flow_cls_offload
8841  *
8842  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8843 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8844 				 struct flow_cls_offload *cls_flower)
8845 {
8846 	struct i40e_cloud_filter *filter = NULL;
8847 	struct i40e_pf *pf = vsi->back;
8848 	int err = 0;
8849 
8850 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8851 
8852 	if (!filter)
8853 		return -EINVAL;
8854 
8855 	hash_del(&filter->cloud_node);
8856 
8857 	if (filter->dst_port)
8858 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8859 	else
8860 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8861 
8862 	kfree(filter);
8863 	if (err) {
8864 		dev_err(&pf->pdev->dev,
8865 			"Failed to delete cloud filter, err %s\n",
8866 			i40e_stat_str(&pf->hw, err));
8867 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8868 	}
8869 
8870 	pf->num_cloud_filters--;
8871 	if (!pf->num_cloud_filters)
8872 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8873 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8874 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8875 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8876 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8877 		}
8878 	return 0;
8879 }
8880 
8881 /**
8882  * i40e_setup_tc_cls_flower - flower classifier offloads
8883  * @np: net device to configure
8884  * @cls_flower: offload data
8885  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8886 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8887 				    struct flow_cls_offload *cls_flower)
8888 {
8889 	struct i40e_vsi *vsi = np->vsi;
8890 
8891 	switch (cls_flower->command) {
8892 	case FLOW_CLS_REPLACE:
8893 		return i40e_configure_clsflower(vsi, cls_flower);
8894 	case FLOW_CLS_DESTROY:
8895 		return i40e_delete_clsflower(vsi, cls_flower);
8896 	case FLOW_CLS_STATS:
8897 		return -EOPNOTSUPP;
8898 	default:
8899 		return -EOPNOTSUPP;
8900 	}
8901 }
8902 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8903 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8904 				  void *cb_priv)
8905 {
8906 	struct i40e_netdev_priv *np = cb_priv;
8907 
8908 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8909 		return -EOPNOTSUPP;
8910 
8911 	switch (type) {
8912 	case TC_SETUP_CLSFLOWER:
8913 		return i40e_setup_tc_cls_flower(np, type_data);
8914 
8915 	default:
8916 		return -EOPNOTSUPP;
8917 	}
8918 }
8919 
8920 static LIST_HEAD(i40e_block_cb_list);
8921 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8922 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8923 			   void *type_data)
8924 {
8925 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8926 
8927 	switch (type) {
8928 	case TC_SETUP_QDISC_MQPRIO:
8929 		return i40e_setup_tc(netdev, type_data);
8930 	case TC_SETUP_BLOCK:
8931 		return flow_block_cb_setup_simple(type_data,
8932 						  &i40e_block_cb_list,
8933 						  i40e_setup_tc_block_cb,
8934 						  np, np, true);
8935 	default:
8936 		return -EOPNOTSUPP;
8937 	}
8938 }
8939 
8940 /**
8941  * i40e_open - Called when a network interface is made active
8942  * @netdev: network interface device structure
8943  *
8944  * The open entry point is called when a network interface is made
8945  * active by the system (IFF_UP).  At this point all resources needed
8946  * for transmit and receive operations are allocated, the interrupt
8947  * handler is registered with the OS, the netdev watchdog subtask is
8948  * enabled, and the stack is notified that the interface is ready.
8949  *
8950  * Returns 0 on success, negative value on failure
8951  **/
i40e_open(struct net_device * netdev)8952 int i40e_open(struct net_device *netdev)
8953 {
8954 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8955 	struct i40e_vsi *vsi = np->vsi;
8956 	struct i40e_pf *pf = vsi->back;
8957 	int err;
8958 
8959 	/* disallow open during test or if eeprom is broken */
8960 	if (test_bit(__I40E_TESTING, pf->state) ||
8961 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8962 		return -EBUSY;
8963 
8964 	netif_carrier_off(netdev);
8965 
8966 	if (i40e_force_link_state(pf, true))
8967 		return -EAGAIN;
8968 
8969 	err = i40e_vsi_open(vsi);
8970 	if (err)
8971 		return err;
8972 
8973 	/* configure global TSO hardware offload settings */
8974 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8975 						       TCP_FLAG_FIN) >> 16);
8976 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8977 						       TCP_FLAG_FIN |
8978 						       TCP_FLAG_CWR) >> 16);
8979 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8980 	udp_tunnel_get_rx_info(netdev);
8981 
8982 	return 0;
8983 }
8984 
8985 /**
8986  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8987  * @vsi: vsi structure
8988  *
8989  * This updates netdev's number of tx/rx queues
8990  *
8991  * Returns status of setting tx/rx queues
8992  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)8993 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8994 {
8995 	int ret;
8996 
8997 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8998 					   vsi->num_queue_pairs);
8999 	if (ret)
9000 		return ret;
9001 
9002 	return netif_set_real_num_tx_queues(vsi->netdev,
9003 					    vsi->num_queue_pairs);
9004 }
9005 
9006 /**
9007  * i40e_vsi_open -
9008  * @vsi: the VSI to open
9009  *
9010  * Finish initialization of the VSI.
9011  *
9012  * Returns 0 on success, negative value on failure
9013  *
9014  * Note: expects to be called while under rtnl_lock()
9015  **/
i40e_vsi_open(struct i40e_vsi * vsi)9016 int i40e_vsi_open(struct i40e_vsi *vsi)
9017 {
9018 	struct i40e_pf *pf = vsi->back;
9019 	char int_name[I40E_INT_NAME_STR_LEN];
9020 	int err;
9021 
9022 	/* allocate descriptors */
9023 	err = i40e_vsi_setup_tx_resources(vsi);
9024 	if (err)
9025 		goto err_setup_tx;
9026 	err = i40e_vsi_setup_rx_resources(vsi);
9027 	if (err)
9028 		goto err_setup_rx;
9029 
9030 	err = i40e_vsi_configure(vsi);
9031 	if (err)
9032 		goto err_setup_rx;
9033 
9034 	if (vsi->netdev) {
9035 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9036 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9037 		err = i40e_vsi_request_irq(vsi, int_name);
9038 		if (err)
9039 			goto err_setup_rx;
9040 
9041 		/* Notify the stack of the actual queue counts. */
9042 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9043 		if (err)
9044 			goto err_set_queues;
9045 
9046 	} else if (vsi->type == I40E_VSI_FDIR) {
9047 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9048 			 dev_driver_string(&pf->pdev->dev),
9049 			 dev_name(&pf->pdev->dev));
9050 		err = i40e_vsi_request_irq(vsi, int_name);
9051 		if (err)
9052 			goto err_setup_rx;
9053 
9054 	} else {
9055 		err = -EINVAL;
9056 		goto err_setup_rx;
9057 	}
9058 
9059 	err = i40e_up_complete(vsi);
9060 	if (err)
9061 		goto err_up_complete;
9062 
9063 	return 0;
9064 
9065 err_up_complete:
9066 	i40e_down(vsi);
9067 err_set_queues:
9068 	i40e_vsi_free_irq(vsi);
9069 err_setup_rx:
9070 	i40e_vsi_free_rx_resources(vsi);
9071 err_setup_tx:
9072 	i40e_vsi_free_tx_resources(vsi);
9073 	if (vsi == pf->vsi[pf->lan_vsi])
9074 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9075 
9076 	return err;
9077 }
9078 
9079 /**
9080  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9081  * @pf: Pointer to PF
9082  *
9083  * This function destroys the hlist where all the Flow Director
9084  * filters were saved.
9085  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)9086 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9087 {
9088 	struct i40e_fdir_filter *filter;
9089 	struct i40e_flex_pit *pit_entry, *tmp;
9090 	struct hlist_node *node2;
9091 
9092 	hlist_for_each_entry_safe(filter, node2,
9093 				  &pf->fdir_filter_list, fdir_node) {
9094 		hlist_del(&filter->fdir_node);
9095 		kfree(filter);
9096 	}
9097 
9098 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9099 		list_del(&pit_entry->list);
9100 		kfree(pit_entry);
9101 	}
9102 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9103 
9104 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9105 		list_del(&pit_entry->list);
9106 		kfree(pit_entry);
9107 	}
9108 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9109 
9110 	pf->fdir_pf_active_filters = 0;
9111 	i40e_reset_fdir_filter_cnt(pf);
9112 
9113 	/* Reprogram the default input set for TCP/IPv4 */
9114 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9115 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9116 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9117 
9118 	/* Reprogram the default input set for TCP/IPv6 */
9119 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9120 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9121 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9122 
9123 	/* Reprogram the default input set for UDP/IPv4 */
9124 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9125 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9126 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9127 
9128 	/* Reprogram the default input set for UDP/IPv6 */
9129 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9130 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9131 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9132 
9133 	/* Reprogram the default input set for SCTP/IPv4 */
9134 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9135 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9136 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9137 
9138 	/* Reprogram the default input set for SCTP/IPv6 */
9139 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9140 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9141 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9142 
9143 	/* Reprogram the default input set for Other/IPv4 */
9144 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9145 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9146 
9147 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9148 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9149 
9150 	/* Reprogram the default input set for Other/IPv6 */
9151 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9152 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9153 
9154 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9155 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9156 }
9157 
9158 /**
9159  * i40e_cloud_filter_exit - Cleans up the cloud filters
9160  * @pf: Pointer to PF
9161  *
9162  * This function destroys the hlist where all the cloud filters
9163  * were saved.
9164  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)9165 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9166 {
9167 	struct i40e_cloud_filter *cfilter;
9168 	struct hlist_node *node;
9169 
9170 	hlist_for_each_entry_safe(cfilter, node,
9171 				  &pf->cloud_filter_list, cloud_node) {
9172 		hlist_del(&cfilter->cloud_node);
9173 		kfree(cfilter);
9174 	}
9175 	pf->num_cloud_filters = 0;
9176 
9177 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9178 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9179 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9180 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9181 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9182 	}
9183 }
9184 
9185 /**
9186  * i40e_close - Disables a network interface
9187  * @netdev: network interface device structure
9188  *
9189  * The close entry point is called when an interface is de-activated
9190  * by the OS.  The hardware is still under the driver's control, but
9191  * this netdev interface is disabled.
9192  *
9193  * Returns 0, this is not allowed to fail
9194  **/
i40e_close(struct net_device * netdev)9195 int i40e_close(struct net_device *netdev)
9196 {
9197 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9198 	struct i40e_vsi *vsi = np->vsi;
9199 
9200 	i40e_vsi_close(vsi);
9201 
9202 	return 0;
9203 }
9204 
9205 /**
9206  * i40e_do_reset - Start a PF or Core Reset sequence
9207  * @pf: board private structure
9208  * @reset_flags: which reset is requested
9209  * @lock_acquired: indicates whether or not the lock has been acquired
9210  * before this function was called.
9211  *
9212  * The essential difference in resets is that the PF Reset
9213  * doesn't clear the packet buffers, doesn't reset the PE
9214  * firmware, and doesn't bother the other PFs on the chip.
9215  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)9216 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9217 {
9218 	u32 val;
9219 
9220 	/* do the biggest reset indicated */
9221 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9222 
9223 		/* Request a Global Reset
9224 		 *
9225 		 * This will start the chip's countdown to the actual full
9226 		 * chip reset event, and a warning interrupt to be sent
9227 		 * to all PFs, including the requestor.  Our handler
9228 		 * for the warning interrupt will deal with the shutdown
9229 		 * and recovery of the switch setup.
9230 		 */
9231 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9232 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9233 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9234 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9235 
9236 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9237 
9238 		/* Request a Core Reset
9239 		 *
9240 		 * Same as Global Reset, except does *not* include the MAC/PHY
9241 		 */
9242 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9243 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9244 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9245 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9246 		i40e_flush(&pf->hw);
9247 
9248 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9249 
9250 		/* Request a PF Reset
9251 		 *
9252 		 * Resets only the PF-specific registers
9253 		 *
9254 		 * This goes directly to the tear-down and rebuild of
9255 		 * the switch, since we need to do all the recovery as
9256 		 * for the Core Reset.
9257 		 */
9258 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9259 		i40e_handle_reset_warning(pf, lock_acquired);
9260 
9261 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9262 		/* Request a PF Reset
9263 		 *
9264 		 * Resets PF and reinitializes PFs VSI.
9265 		 */
9266 		i40e_prep_for_reset(pf);
9267 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9268 		dev_info(&pf->pdev->dev,
9269 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9270 			 "FW LLDP is disabled\n" :
9271 			 "FW LLDP is enabled\n");
9272 
9273 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9274 		int v;
9275 
9276 		/* Find the VSI(s) that requested a re-init */
9277 		dev_info(&pf->pdev->dev,
9278 			 "VSI reinit requested\n");
9279 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9280 			struct i40e_vsi *vsi = pf->vsi[v];
9281 
9282 			if (vsi != NULL &&
9283 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9284 					       vsi->state))
9285 				i40e_vsi_reinit_locked(pf->vsi[v]);
9286 		}
9287 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9288 		int v;
9289 
9290 		/* Find the VSI(s) that needs to be brought down */
9291 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9292 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9293 			struct i40e_vsi *vsi = pf->vsi[v];
9294 
9295 			if (vsi != NULL &&
9296 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9297 					       vsi->state)) {
9298 				set_bit(__I40E_VSI_DOWN, vsi->state);
9299 				i40e_down(vsi);
9300 			}
9301 		}
9302 	} else {
9303 		dev_info(&pf->pdev->dev,
9304 			 "bad reset request 0x%08x\n", reset_flags);
9305 	}
9306 }
9307 
9308 #ifdef CONFIG_I40E_DCB
9309 /**
9310  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9311  * @pf: board private structure
9312  * @old_cfg: current DCB config
9313  * @new_cfg: new DCB config
9314  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)9315 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9316 			    struct i40e_dcbx_config *old_cfg,
9317 			    struct i40e_dcbx_config *new_cfg)
9318 {
9319 	bool need_reconfig = false;
9320 
9321 	/* Check if ETS configuration has changed */
9322 	if (memcmp(&new_cfg->etscfg,
9323 		   &old_cfg->etscfg,
9324 		   sizeof(new_cfg->etscfg))) {
9325 		/* If Priority Table has changed reconfig is needed */
9326 		if (memcmp(&new_cfg->etscfg.prioritytable,
9327 			   &old_cfg->etscfg.prioritytable,
9328 			   sizeof(new_cfg->etscfg.prioritytable))) {
9329 			need_reconfig = true;
9330 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9331 		}
9332 
9333 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9334 			   &old_cfg->etscfg.tcbwtable,
9335 			   sizeof(new_cfg->etscfg.tcbwtable)))
9336 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9337 
9338 		if (memcmp(&new_cfg->etscfg.tsatable,
9339 			   &old_cfg->etscfg.tsatable,
9340 			   sizeof(new_cfg->etscfg.tsatable)))
9341 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9342 	}
9343 
9344 	/* Check if PFC configuration has changed */
9345 	if (memcmp(&new_cfg->pfc,
9346 		   &old_cfg->pfc,
9347 		   sizeof(new_cfg->pfc))) {
9348 		need_reconfig = true;
9349 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9350 	}
9351 
9352 	/* Check if APP Table has changed */
9353 	if (memcmp(&new_cfg->app,
9354 		   &old_cfg->app,
9355 		   sizeof(new_cfg->app))) {
9356 		need_reconfig = true;
9357 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9358 	}
9359 
9360 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9361 	return need_reconfig;
9362 }
9363 
9364 /**
9365  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9366  * @pf: board private structure
9367  * @e: event info posted on ARQ
9368  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9369 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9370 				  struct i40e_arq_event_info *e)
9371 {
9372 	struct i40e_aqc_lldp_get_mib *mib =
9373 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9374 	struct i40e_hw *hw = &pf->hw;
9375 	struct i40e_dcbx_config tmp_dcbx_cfg;
9376 	bool need_reconfig = false;
9377 	int ret = 0;
9378 	u8 type;
9379 
9380 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9381 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9382 	    (hw->phy.link_info.link_speed &
9383 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9384 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9385 		/* let firmware decide if the DCB should be disabled */
9386 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9387 
9388 	/* Not DCB capable or capability disabled */
9389 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9390 		return ret;
9391 
9392 	/* Ignore if event is not for Nearest Bridge */
9393 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9394 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9395 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9396 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9397 		return ret;
9398 
9399 	/* Check MIB Type and return if event for Remote MIB update */
9400 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9401 	dev_dbg(&pf->pdev->dev,
9402 		"LLDP event mib type %s\n", type ? "remote" : "local");
9403 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9404 		/* Update the remote cached instance and return */
9405 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9406 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9407 				&hw->remote_dcbx_config);
9408 		goto exit;
9409 	}
9410 
9411 	/* Store the old configuration */
9412 	tmp_dcbx_cfg = hw->local_dcbx_config;
9413 
9414 	/* Reset the old DCBx configuration data */
9415 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9416 	/* Get updated DCBX data from firmware */
9417 	ret = i40e_get_dcb_config(&pf->hw);
9418 	if (ret) {
9419 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9420 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9421 		    (hw->phy.link_info.link_speed &
9422 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9423 			dev_warn(&pf->pdev->dev,
9424 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9425 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9426 		} else {
9427 			dev_info(&pf->pdev->dev,
9428 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9429 				 i40e_stat_str(&pf->hw, ret),
9430 				 i40e_aq_str(&pf->hw,
9431 					     pf->hw.aq.asq_last_status));
9432 		}
9433 		goto exit;
9434 	}
9435 
9436 	/* No change detected in DCBX configs */
9437 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9438 		    sizeof(tmp_dcbx_cfg))) {
9439 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9440 		goto exit;
9441 	}
9442 
9443 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9444 					       &hw->local_dcbx_config);
9445 
9446 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9447 
9448 	if (!need_reconfig)
9449 		goto exit;
9450 
9451 	/* Enable DCB tagging only when more than one TC */
9452 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9453 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9454 	else
9455 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9456 
9457 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9458 	/* Reconfiguration needed quiesce all VSIs */
9459 	i40e_pf_quiesce_all_vsi(pf);
9460 
9461 	/* Changes in configuration update VEB/VSI */
9462 	i40e_dcb_reconfigure(pf);
9463 
9464 	ret = i40e_resume_port_tx(pf);
9465 
9466 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9467 	/* In case of error no point in resuming VSIs */
9468 	if (ret)
9469 		goto exit;
9470 
9471 	/* Wait for the PF's queues to be disabled */
9472 	ret = i40e_pf_wait_queues_disabled(pf);
9473 	if (ret) {
9474 		/* Schedule PF reset to recover */
9475 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9476 		i40e_service_event_schedule(pf);
9477 	} else {
9478 		i40e_pf_unquiesce_all_vsi(pf);
9479 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9480 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9481 	}
9482 
9483 exit:
9484 	return ret;
9485 }
9486 #endif /* CONFIG_I40E_DCB */
9487 
9488 /**
9489  * i40e_do_reset_safe - Protected reset path for userland calls.
9490  * @pf: board private structure
9491  * @reset_flags: which reset is requested
9492  *
9493  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)9494 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9495 {
9496 	rtnl_lock();
9497 	i40e_do_reset(pf, reset_flags, true);
9498 	rtnl_unlock();
9499 }
9500 
9501 /**
9502  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9503  * @pf: board private structure
9504  * @e: event info posted on ARQ
9505  *
9506  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9507  * and VF queues
9508  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9509 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9510 					   struct i40e_arq_event_info *e)
9511 {
9512 	struct i40e_aqc_lan_overflow *data =
9513 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9514 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9515 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9516 	struct i40e_hw *hw = &pf->hw;
9517 	struct i40e_vf *vf;
9518 	u16 vf_id;
9519 
9520 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9521 		queue, qtx_ctl);
9522 
9523 	/* Queue belongs to VF, find the VF and issue VF reset */
9524 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9525 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9526 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9527 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9528 		vf_id -= hw->func_caps.vf_base_id;
9529 		vf = &pf->vf[vf_id];
9530 		i40e_vc_notify_vf_reset(vf);
9531 		/* Allow VF to process pending reset notification */
9532 		msleep(20);
9533 		i40e_reset_vf(vf, false);
9534 	}
9535 }
9536 
9537 /**
9538  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9539  * @pf: board private structure
9540  **/
i40e_get_cur_guaranteed_fd_count(struct i40e_pf * pf)9541 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9542 {
9543 	u32 val, fcnt_prog;
9544 
9545 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9546 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9547 	return fcnt_prog;
9548 }
9549 
9550 /**
9551  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9552  * @pf: board private structure
9553  **/
i40e_get_current_fd_count(struct i40e_pf * pf)9554 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9555 {
9556 	u32 val, fcnt_prog;
9557 
9558 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9559 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9560 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9561 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9562 	return fcnt_prog;
9563 }
9564 
9565 /**
9566  * i40e_get_global_fd_count - Get total FD filters programmed on device
9567  * @pf: board private structure
9568  **/
i40e_get_global_fd_count(struct i40e_pf * pf)9569 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9570 {
9571 	u32 val, fcnt_prog;
9572 
9573 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9574 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9575 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9576 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9577 	return fcnt_prog;
9578 }
9579 
9580 /**
9581  * i40e_reenable_fdir_sb - Restore FDir SB capability
9582  * @pf: board private structure
9583  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)9584 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9585 {
9586 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9587 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9588 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9589 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9590 }
9591 
9592 /**
9593  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9594  * @pf: board private structure
9595  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)9596 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9597 {
9598 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9599 		/* ATR uses the same filtering logic as SB rules. It only
9600 		 * functions properly if the input set mask is at the default
9601 		 * settings. It is safe to restore the default input set
9602 		 * because there are no active TCPv4 filter rules.
9603 		 */
9604 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9605 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9606 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9607 
9608 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9609 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9610 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9611 	}
9612 }
9613 
9614 /**
9615  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9616  * @pf: board private structure
9617  * @filter: FDir filter to remove
9618  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)9619 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9620 				       struct i40e_fdir_filter *filter)
9621 {
9622 	/* Update counters */
9623 	pf->fdir_pf_active_filters--;
9624 	pf->fd_inv = 0;
9625 
9626 	switch (filter->flow_type) {
9627 	case TCP_V4_FLOW:
9628 		pf->fd_tcp4_filter_cnt--;
9629 		break;
9630 	case UDP_V4_FLOW:
9631 		pf->fd_udp4_filter_cnt--;
9632 		break;
9633 	case SCTP_V4_FLOW:
9634 		pf->fd_sctp4_filter_cnt--;
9635 		break;
9636 	case TCP_V6_FLOW:
9637 		pf->fd_tcp6_filter_cnt--;
9638 		break;
9639 	case UDP_V6_FLOW:
9640 		pf->fd_udp6_filter_cnt--;
9641 		break;
9642 	case SCTP_V6_FLOW:
9643 		pf->fd_udp6_filter_cnt--;
9644 		break;
9645 	case IP_USER_FLOW:
9646 		switch (filter->ipl4_proto) {
9647 		case IPPROTO_TCP:
9648 			pf->fd_tcp4_filter_cnt--;
9649 			break;
9650 		case IPPROTO_UDP:
9651 			pf->fd_udp4_filter_cnt--;
9652 			break;
9653 		case IPPROTO_SCTP:
9654 			pf->fd_sctp4_filter_cnt--;
9655 			break;
9656 		case IPPROTO_IP:
9657 			pf->fd_ip4_filter_cnt--;
9658 			break;
9659 		}
9660 		break;
9661 	case IPV6_USER_FLOW:
9662 		switch (filter->ipl4_proto) {
9663 		case IPPROTO_TCP:
9664 			pf->fd_tcp6_filter_cnt--;
9665 			break;
9666 		case IPPROTO_UDP:
9667 			pf->fd_udp6_filter_cnt--;
9668 			break;
9669 		case IPPROTO_SCTP:
9670 			pf->fd_sctp6_filter_cnt--;
9671 			break;
9672 		case IPPROTO_IP:
9673 			pf->fd_ip6_filter_cnt--;
9674 			break;
9675 		}
9676 		break;
9677 	}
9678 
9679 	/* Remove the filter from the list and free memory */
9680 	hlist_del(&filter->fdir_node);
9681 	kfree(filter);
9682 }
9683 
9684 /**
9685  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9686  * @pf: board private structure
9687  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9688 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9689 {
9690 	struct i40e_fdir_filter *filter;
9691 	u32 fcnt_prog, fcnt_avail;
9692 	struct hlist_node *node;
9693 
9694 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9695 		return;
9696 
9697 	/* Check if we have enough room to re-enable FDir SB capability. */
9698 	fcnt_prog = i40e_get_global_fd_count(pf);
9699 	fcnt_avail = pf->fdir_pf_filter_count;
9700 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9701 	    (pf->fd_add_err == 0) ||
9702 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9703 		i40e_reenable_fdir_sb(pf);
9704 
9705 	/* We should wait for even more space before re-enabling ATR.
9706 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9707 	 * rules active.
9708 	 */
9709 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9710 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9711 		i40e_reenable_fdir_atr(pf);
9712 
9713 	/* if hw had a problem adding a filter, delete it */
9714 	if (pf->fd_inv > 0) {
9715 		hlist_for_each_entry_safe(filter, node,
9716 					  &pf->fdir_filter_list, fdir_node)
9717 			if (filter->fd_id == pf->fd_inv)
9718 				i40e_delete_invalid_filter(pf, filter);
9719 	}
9720 }
9721 
9722 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9723 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9724 /**
9725  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9726  * @pf: board private structure
9727  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9728 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9729 {
9730 	unsigned long min_flush_time;
9731 	int flush_wait_retry = 50;
9732 	bool disable_atr = false;
9733 	int fd_room;
9734 	int reg;
9735 
9736 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9737 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9738 		return;
9739 
9740 	/* If the flush is happening too quick and we have mostly SB rules we
9741 	 * should not re-enable ATR for some time.
9742 	 */
9743 	min_flush_time = pf->fd_flush_timestamp +
9744 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9745 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9746 
9747 	if (!(time_after(jiffies, min_flush_time)) &&
9748 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9749 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9750 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9751 		disable_atr = true;
9752 	}
9753 
9754 	pf->fd_flush_timestamp = jiffies;
9755 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9756 	/* flush all filters */
9757 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9758 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9759 	i40e_flush(&pf->hw);
9760 	pf->fd_flush_cnt++;
9761 	pf->fd_add_err = 0;
9762 	do {
9763 		/* Check FD flush status every 5-6msec */
9764 		usleep_range(5000, 6000);
9765 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9766 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9767 			break;
9768 	} while (flush_wait_retry--);
9769 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9770 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9771 	} else {
9772 		/* replay sideband filters */
9773 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9774 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9775 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9776 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9777 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9778 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9779 	}
9780 }
9781 
9782 /**
9783  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9784  * @pf: board private structure
9785  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9786 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9787 {
9788 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9789 }
9790 
9791 /**
9792  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9793  * @pf: board private structure
9794  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9795 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9796 {
9797 
9798 	/* if interface is down do nothing */
9799 	if (test_bit(__I40E_DOWN, pf->state))
9800 		return;
9801 
9802 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9803 		i40e_fdir_flush_and_replay(pf);
9804 
9805 	i40e_fdir_check_and_reenable(pf);
9806 
9807 }
9808 
9809 /**
9810  * i40e_vsi_link_event - notify VSI of a link event
9811  * @vsi: vsi to be notified
9812  * @link_up: link up or down
9813  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9814 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9815 {
9816 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9817 		return;
9818 
9819 	switch (vsi->type) {
9820 	case I40E_VSI_MAIN:
9821 		if (!vsi->netdev || !vsi->netdev_registered)
9822 			break;
9823 
9824 		if (link_up) {
9825 			netif_carrier_on(vsi->netdev);
9826 			netif_tx_wake_all_queues(vsi->netdev);
9827 		} else {
9828 			netif_carrier_off(vsi->netdev);
9829 			netif_tx_stop_all_queues(vsi->netdev);
9830 		}
9831 		break;
9832 
9833 	case I40E_VSI_SRIOV:
9834 	case I40E_VSI_VMDQ2:
9835 	case I40E_VSI_CTRL:
9836 	case I40E_VSI_IWARP:
9837 	case I40E_VSI_MIRROR:
9838 	default:
9839 		/* there is no notification for other VSIs */
9840 		break;
9841 	}
9842 }
9843 
9844 /**
9845  * i40e_veb_link_event - notify elements on the veb of a link event
9846  * @veb: veb to be notified
9847  * @link_up: link up or down
9848  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9849 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9850 {
9851 	struct i40e_pf *pf;
9852 	int i;
9853 
9854 	if (!veb || !veb->pf)
9855 		return;
9856 	pf = veb->pf;
9857 
9858 	/* depth first... */
9859 	for (i = 0; i < I40E_MAX_VEB; i++)
9860 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9861 			i40e_veb_link_event(pf->veb[i], link_up);
9862 
9863 	/* ... now the local VSIs */
9864 	for (i = 0; i < pf->num_alloc_vsi; i++)
9865 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9866 			i40e_vsi_link_event(pf->vsi[i], link_up);
9867 }
9868 
9869 /**
9870  * i40e_link_event - Update netif_carrier status
9871  * @pf: board private structure
9872  **/
i40e_link_event(struct i40e_pf * pf)9873 static void i40e_link_event(struct i40e_pf *pf)
9874 {
9875 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9876 	u8 new_link_speed, old_link_speed;
9877 	i40e_status status;
9878 	bool new_link, old_link;
9879 #ifdef CONFIG_I40E_DCB
9880 	int err;
9881 #endif /* CONFIG_I40E_DCB */
9882 
9883 	/* set this to force the get_link_status call to refresh state */
9884 	pf->hw.phy.get_link_info = true;
9885 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9886 	status = i40e_get_link_status(&pf->hw, &new_link);
9887 
9888 	/* On success, disable temp link polling */
9889 	if (status == I40E_SUCCESS) {
9890 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9891 	} else {
9892 		/* Enable link polling temporarily until i40e_get_link_status
9893 		 * returns I40E_SUCCESS
9894 		 */
9895 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9896 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9897 			status);
9898 		return;
9899 	}
9900 
9901 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9902 	new_link_speed = pf->hw.phy.link_info.link_speed;
9903 
9904 	if (new_link == old_link &&
9905 	    new_link_speed == old_link_speed &&
9906 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9907 	     new_link == netif_carrier_ok(vsi->netdev)))
9908 		return;
9909 
9910 	i40e_print_link_message(vsi, new_link);
9911 
9912 	/* Notify the base of the switch tree connected to
9913 	 * the link.  Floating VEBs are not notified.
9914 	 */
9915 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9916 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9917 	else
9918 		i40e_vsi_link_event(vsi, new_link);
9919 
9920 	if (pf->vf)
9921 		i40e_vc_notify_link_state(pf);
9922 
9923 	if (pf->flags & I40E_FLAG_PTP)
9924 		i40e_ptp_set_increment(pf);
9925 #ifdef CONFIG_I40E_DCB
9926 	if (new_link == old_link)
9927 		return;
9928 	/* Not SW DCB so firmware will take care of default settings */
9929 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9930 		return;
9931 
9932 	/* We cover here only link down, as after link up in case of SW DCB
9933 	 * SW LLDP agent will take care of setting it up
9934 	 */
9935 	if (!new_link) {
9936 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9937 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9938 		err = i40e_dcb_sw_default_config(pf);
9939 		if (err) {
9940 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9941 				       I40E_FLAG_DCB_ENABLED);
9942 		} else {
9943 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9944 				       DCB_CAP_DCBX_VER_IEEE;
9945 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9946 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9947 		}
9948 	}
9949 #endif /* CONFIG_I40E_DCB */
9950 }
9951 
9952 /**
9953  * i40e_watchdog_subtask - periodic checks not using event driven response
9954  * @pf: board private structure
9955  **/
i40e_watchdog_subtask(struct i40e_pf * pf)9956 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9957 {
9958 	int i;
9959 
9960 	/* if interface is down do nothing */
9961 	if (test_bit(__I40E_DOWN, pf->state) ||
9962 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9963 		return;
9964 
9965 	/* make sure we don't do these things too often */
9966 	if (time_before(jiffies, (pf->service_timer_previous +
9967 				  pf->service_timer_period)))
9968 		return;
9969 	pf->service_timer_previous = jiffies;
9970 
9971 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9972 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9973 		i40e_link_event(pf);
9974 
9975 	/* Update the stats for active netdevs so the network stack
9976 	 * can look at updated numbers whenever it cares to
9977 	 */
9978 	for (i = 0; i < pf->num_alloc_vsi; i++)
9979 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9980 			i40e_update_stats(pf->vsi[i]);
9981 
9982 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9983 		/* Update the stats for the active switching components */
9984 		for (i = 0; i < I40E_MAX_VEB; i++)
9985 			if (pf->veb[i])
9986 				i40e_update_veb_stats(pf->veb[i]);
9987 	}
9988 
9989 	i40e_ptp_rx_hang(pf);
9990 	i40e_ptp_tx_hang(pf);
9991 }
9992 
9993 /**
9994  * i40e_reset_subtask - Set up for resetting the device and driver
9995  * @pf: board private structure
9996  **/
i40e_reset_subtask(struct i40e_pf * pf)9997 static void i40e_reset_subtask(struct i40e_pf *pf)
9998 {
9999 	u32 reset_flags = 0;
10000 
10001 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10002 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10003 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10004 	}
10005 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10006 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10007 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10008 	}
10009 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10010 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10011 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10012 	}
10013 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10014 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10015 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10016 	}
10017 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10018 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10019 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10020 	}
10021 
10022 	/* If there's a recovery already waiting, it takes
10023 	 * precedence before starting a new reset sequence.
10024 	 */
10025 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10026 		i40e_prep_for_reset(pf);
10027 		i40e_reset(pf);
10028 		i40e_rebuild(pf, false, false);
10029 	}
10030 
10031 	/* If we're already down or resetting, just bail */
10032 	if (reset_flags &&
10033 	    !test_bit(__I40E_DOWN, pf->state) &&
10034 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10035 		i40e_do_reset(pf, reset_flags, false);
10036 	}
10037 }
10038 
10039 /**
10040  * i40e_handle_link_event - Handle link event
10041  * @pf: board private structure
10042  * @e: event info posted on ARQ
10043  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)10044 static void i40e_handle_link_event(struct i40e_pf *pf,
10045 				   struct i40e_arq_event_info *e)
10046 {
10047 	struct i40e_aqc_get_link_status *status =
10048 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10049 
10050 	/* Do a new status request to re-enable LSE reporting
10051 	 * and load new status information into the hw struct
10052 	 * This completely ignores any state information
10053 	 * in the ARQ event info, instead choosing to always
10054 	 * issue the AQ update link status command.
10055 	 */
10056 	i40e_link_event(pf);
10057 
10058 	/* Check if module meets thermal requirements */
10059 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10060 		dev_err(&pf->pdev->dev,
10061 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10062 		dev_err(&pf->pdev->dev,
10063 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10064 	} else {
10065 		/* check for unqualified module, if link is down, suppress
10066 		 * the message if link was forced to be down.
10067 		 */
10068 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10069 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10070 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10071 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10072 			dev_err(&pf->pdev->dev,
10073 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\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 		}
10077 	}
10078 }
10079 
10080 /**
10081  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10082  * @pf: board private structure
10083  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)10084 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10085 {
10086 	struct i40e_arq_event_info event;
10087 	struct i40e_hw *hw = &pf->hw;
10088 	u16 pending, i = 0;
10089 	i40e_status ret;
10090 	u16 opcode;
10091 	u32 oldval;
10092 	u32 val;
10093 
10094 	/* Do not run clean AQ when PF reset fails */
10095 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10096 		return;
10097 
10098 	/* check for error indications */
10099 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10100 	oldval = val;
10101 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10102 		if (hw->debug_mask & I40E_DEBUG_AQ)
10103 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10104 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10105 	}
10106 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10107 		if (hw->debug_mask & I40E_DEBUG_AQ)
10108 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10109 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10110 		pf->arq_overflows++;
10111 	}
10112 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10113 		if (hw->debug_mask & I40E_DEBUG_AQ)
10114 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10115 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10116 	}
10117 	if (oldval != val)
10118 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10119 
10120 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10121 	oldval = val;
10122 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10123 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10124 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10125 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10126 	}
10127 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10128 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10129 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10130 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10131 	}
10132 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10133 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10134 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10135 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10136 	}
10137 	if (oldval != val)
10138 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10139 
10140 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10141 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10142 	if (!event.msg_buf)
10143 		return;
10144 
10145 	do {
10146 		ret = i40e_clean_arq_element(hw, &event, &pending);
10147 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10148 			break;
10149 		else if (ret) {
10150 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10151 			break;
10152 		}
10153 
10154 		opcode = le16_to_cpu(event.desc.opcode);
10155 		switch (opcode) {
10156 
10157 		case i40e_aqc_opc_get_link_status:
10158 			rtnl_lock();
10159 			i40e_handle_link_event(pf, &event);
10160 			rtnl_unlock();
10161 			break;
10162 		case i40e_aqc_opc_send_msg_to_pf:
10163 			ret = i40e_vc_process_vf_msg(pf,
10164 					le16_to_cpu(event.desc.retval),
10165 					le32_to_cpu(event.desc.cookie_high),
10166 					le32_to_cpu(event.desc.cookie_low),
10167 					event.msg_buf,
10168 					event.msg_len);
10169 			break;
10170 		case i40e_aqc_opc_lldp_update_mib:
10171 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10172 #ifdef CONFIG_I40E_DCB
10173 			rtnl_lock();
10174 			i40e_handle_lldp_event(pf, &event);
10175 			rtnl_unlock();
10176 #endif /* CONFIG_I40E_DCB */
10177 			break;
10178 		case i40e_aqc_opc_event_lan_overflow:
10179 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10180 			i40e_handle_lan_overflow_event(pf, &event);
10181 			break;
10182 		case i40e_aqc_opc_send_msg_to_peer:
10183 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10184 			break;
10185 		case i40e_aqc_opc_nvm_erase:
10186 		case i40e_aqc_opc_nvm_update:
10187 		case i40e_aqc_opc_oem_post_update:
10188 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10189 				   "ARQ NVM operation 0x%04x completed\n",
10190 				   opcode);
10191 			break;
10192 		default:
10193 			dev_info(&pf->pdev->dev,
10194 				 "ARQ: Unknown event 0x%04x ignored\n",
10195 				 opcode);
10196 			break;
10197 		}
10198 	} while (i++ < pf->adminq_work_limit);
10199 
10200 	if (i < pf->adminq_work_limit)
10201 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10202 
10203 	/* re-enable Admin queue interrupt cause */
10204 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10205 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10206 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10207 	i40e_flush(hw);
10208 
10209 	kfree(event.msg_buf);
10210 }
10211 
10212 /**
10213  * i40e_verify_eeprom - make sure eeprom is good to use
10214  * @pf: board private structure
10215  **/
i40e_verify_eeprom(struct i40e_pf * pf)10216 static void i40e_verify_eeprom(struct i40e_pf *pf)
10217 {
10218 	int err;
10219 
10220 	err = i40e_diag_eeprom_test(&pf->hw);
10221 	if (err) {
10222 		/* retry in case of garbage read */
10223 		err = i40e_diag_eeprom_test(&pf->hw);
10224 		if (err) {
10225 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10226 				 err);
10227 			set_bit(__I40E_BAD_EEPROM, pf->state);
10228 		}
10229 	}
10230 
10231 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10232 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10233 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10234 	}
10235 }
10236 
10237 /**
10238  * i40e_enable_pf_switch_lb
10239  * @pf: pointer to the PF structure
10240  *
10241  * enable switch loop back or die - no point in a return value
10242  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)10243 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10244 {
10245 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10246 	struct i40e_vsi_context ctxt;
10247 	int ret;
10248 
10249 	ctxt.seid = pf->main_vsi_seid;
10250 	ctxt.pf_num = pf->hw.pf_id;
10251 	ctxt.vf_num = 0;
10252 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10253 	if (ret) {
10254 		dev_info(&pf->pdev->dev,
10255 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10256 			 i40e_stat_str(&pf->hw, ret),
10257 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10258 		return;
10259 	}
10260 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10261 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10262 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10263 
10264 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10265 	if (ret) {
10266 		dev_info(&pf->pdev->dev,
10267 			 "update vsi switch failed, 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 	}
10271 }
10272 
10273 /**
10274  * i40e_disable_pf_switch_lb
10275  * @pf: pointer to the PF structure
10276  *
10277  * disable switch loop back or die - no point in a return value
10278  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)10279 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10280 {
10281 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10282 	struct i40e_vsi_context ctxt;
10283 	int ret;
10284 
10285 	ctxt.seid = pf->main_vsi_seid;
10286 	ctxt.pf_num = pf->hw.pf_id;
10287 	ctxt.vf_num = 0;
10288 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10289 	if (ret) {
10290 		dev_info(&pf->pdev->dev,
10291 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10292 			 i40e_stat_str(&pf->hw, ret),
10293 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10294 		return;
10295 	}
10296 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10297 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10298 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10299 
10300 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10301 	if (ret) {
10302 		dev_info(&pf->pdev->dev,
10303 			 "update vsi switch failed, 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 	}
10307 }
10308 
10309 /**
10310  * i40e_config_bridge_mode - Configure the HW bridge mode
10311  * @veb: pointer to the bridge instance
10312  *
10313  * Configure the loop back mode for the LAN VSI that is downlink to the
10314  * specified HW bridge instance. It is expected this function is called
10315  * when a new HW bridge is instantiated.
10316  **/
i40e_config_bridge_mode(struct i40e_veb * veb)10317 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10318 {
10319 	struct i40e_pf *pf = veb->pf;
10320 
10321 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10322 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10323 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10324 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10325 		i40e_disable_pf_switch_lb(pf);
10326 	else
10327 		i40e_enable_pf_switch_lb(pf);
10328 }
10329 
10330 /**
10331  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10332  * @veb: pointer to the VEB instance
10333  *
10334  * This is a recursive function that first builds the attached VSIs then
10335  * recurses in to build the next layer of VEB.  We track the connections
10336  * through our own index numbers because the seid's from the HW could
10337  * change across the reset.
10338  **/
i40e_reconstitute_veb(struct i40e_veb * veb)10339 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10340 {
10341 	struct i40e_vsi *ctl_vsi = NULL;
10342 	struct i40e_pf *pf = veb->pf;
10343 	int v, veb_idx;
10344 	int ret;
10345 
10346 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10347 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10348 		if (pf->vsi[v] &&
10349 		    pf->vsi[v]->veb_idx == veb->idx &&
10350 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10351 			ctl_vsi = pf->vsi[v];
10352 			break;
10353 		}
10354 	}
10355 	if (!ctl_vsi) {
10356 		dev_info(&pf->pdev->dev,
10357 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10358 		ret = -ENOENT;
10359 		goto end_reconstitute;
10360 	}
10361 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10362 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10363 	ret = i40e_add_vsi(ctl_vsi);
10364 	if (ret) {
10365 		dev_info(&pf->pdev->dev,
10366 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10367 			 veb->idx, ret);
10368 		goto end_reconstitute;
10369 	}
10370 	i40e_vsi_reset_stats(ctl_vsi);
10371 
10372 	/* create the VEB in the switch and move the VSI onto the VEB */
10373 	ret = i40e_add_veb(veb, ctl_vsi);
10374 	if (ret)
10375 		goto end_reconstitute;
10376 
10377 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10378 		veb->bridge_mode = BRIDGE_MODE_VEB;
10379 	else
10380 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10381 	i40e_config_bridge_mode(veb);
10382 
10383 	/* create the remaining VSIs attached to this VEB */
10384 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10385 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10386 			continue;
10387 
10388 		if (pf->vsi[v]->veb_idx == veb->idx) {
10389 			struct i40e_vsi *vsi = pf->vsi[v];
10390 
10391 			vsi->uplink_seid = veb->seid;
10392 			ret = i40e_add_vsi(vsi);
10393 			if (ret) {
10394 				dev_info(&pf->pdev->dev,
10395 					 "rebuild of vsi_idx %d failed: %d\n",
10396 					 v, ret);
10397 				goto end_reconstitute;
10398 			}
10399 			i40e_vsi_reset_stats(vsi);
10400 		}
10401 	}
10402 
10403 	/* create any VEBs attached to this VEB - RECURSION */
10404 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10405 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10406 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10407 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10408 			if (ret)
10409 				break;
10410 		}
10411 	}
10412 
10413 end_reconstitute:
10414 	return ret;
10415 }
10416 
10417 /**
10418  * i40e_get_capabilities - get info about the HW
10419  * @pf: the PF struct
10420  * @list_type: AQ capability to be queried
10421  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)10422 static int i40e_get_capabilities(struct i40e_pf *pf,
10423 				 enum i40e_admin_queue_opc list_type)
10424 {
10425 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10426 	u16 data_size;
10427 	int buf_len;
10428 	int err;
10429 
10430 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10431 	do {
10432 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10433 		if (!cap_buf)
10434 			return -ENOMEM;
10435 
10436 		/* this loads the data into the hw struct for us */
10437 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10438 						    &data_size, list_type,
10439 						    NULL);
10440 		/* data loaded, buffer no longer needed */
10441 		kfree(cap_buf);
10442 
10443 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10444 			/* retry with a larger buffer */
10445 			buf_len = data_size;
10446 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10447 			dev_info(&pf->pdev->dev,
10448 				 "capability discovery failed, err %s aq_err %s\n",
10449 				 i40e_stat_str(&pf->hw, err),
10450 				 i40e_aq_str(&pf->hw,
10451 					     pf->hw.aq.asq_last_status));
10452 			return -ENODEV;
10453 		}
10454 	} while (err);
10455 
10456 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10457 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10458 			dev_info(&pf->pdev->dev,
10459 				 "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",
10460 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10461 				 pf->hw.func_caps.num_msix_vectors,
10462 				 pf->hw.func_caps.num_msix_vectors_vf,
10463 				 pf->hw.func_caps.fd_filters_guaranteed,
10464 				 pf->hw.func_caps.fd_filters_best_effort,
10465 				 pf->hw.func_caps.num_tx_qp,
10466 				 pf->hw.func_caps.num_vsis);
10467 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10468 			dev_info(&pf->pdev->dev,
10469 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10470 				 pf->hw.dev_caps.switch_mode,
10471 				 pf->hw.dev_caps.valid_functions);
10472 			dev_info(&pf->pdev->dev,
10473 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10474 				 pf->hw.dev_caps.sr_iov_1_1,
10475 				 pf->hw.dev_caps.num_vfs);
10476 			dev_info(&pf->pdev->dev,
10477 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10478 				 pf->hw.dev_caps.num_vsis,
10479 				 pf->hw.dev_caps.num_rx_qp,
10480 				 pf->hw.dev_caps.num_tx_qp);
10481 		}
10482 	}
10483 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10484 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10485 		       + pf->hw.func_caps.num_vfs)
10486 		if (pf->hw.revision_id == 0 &&
10487 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10488 			dev_info(&pf->pdev->dev,
10489 				 "got num_vsis %d, setting num_vsis to %d\n",
10490 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10491 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10492 		}
10493 	}
10494 	return 0;
10495 }
10496 
10497 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10498 
10499 /**
10500  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10501  * @pf: board private structure
10502  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)10503 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10504 {
10505 	struct i40e_vsi *vsi;
10506 
10507 	/* quick workaround for an NVM issue that leaves a critical register
10508 	 * uninitialized
10509 	 */
10510 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10511 		static const u32 hkey[] = {
10512 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10513 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10514 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10515 			0x95b3a76d};
10516 		int i;
10517 
10518 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10519 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10520 	}
10521 
10522 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10523 		return;
10524 
10525 	/* find existing VSI and see if it needs configuring */
10526 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10527 
10528 	/* create a new VSI if none exists */
10529 	if (!vsi) {
10530 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10531 				     pf->vsi[pf->lan_vsi]->seid, 0);
10532 		if (!vsi) {
10533 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10534 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10535 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10536 			return;
10537 		}
10538 	}
10539 
10540 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10541 }
10542 
10543 /**
10544  * i40e_fdir_teardown - release the Flow Director resources
10545  * @pf: board private structure
10546  **/
i40e_fdir_teardown(struct i40e_pf * pf)10547 static void i40e_fdir_teardown(struct i40e_pf *pf)
10548 {
10549 	struct i40e_vsi *vsi;
10550 
10551 	i40e_fdir_filter_exit(pf);
10552 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10553 	if (vsi)
10554 		i40e_vsi_release(vsi);
10555 }
10556 
10557 /**
10558  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10559  * @vsi: PF main vsi
10560  * @seid: seid of main or channel VSIs
10561  *
10562  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10563  * existed before reset
10564  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)10565 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10566 {
10567 	struct i40e_cloud_filter *cfilter;
10568 	struct i40e_pf *pf = vsi->back;
10569 	struct hlist_node *node;
10570 	i40e_status ret;
10571 
10572 	/* Add cloud filters back if they exist */
10573 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10574 				  cloud_node) {
10575 		if (cfilter->seid != seid)
10576 			continue;
10577 
10578 		if (cfilter->dst_port)
10579 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10580 								true);
10581 		else
10582 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10583 
10584 		if (ret) {
10585 			dev_dbg(&pf->pdev->dev,
10586 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10587 				i40e_stat_str(&pf->hw, ret),
10588 				i40e_aq_str(&pf->hw,
10589 					    pf->hw.aq.asq_last_status));
10590 			return ret;
10591 		}
10592 	}
10593 	return 0;
10594 }
10595 
10596 /**
10597  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10598  * @vsi: PF main vsi
10599  *
10600  * Rebuilds channel VSIs if they existed before reset
10601  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)10602 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10603 {
10604 	struct i40e_channel *ch, *ch_tmp;
10605 	i40e_status ret;
10606 
10607 	if (list_empty(&vsi->ch_list))
10608 		return 0;
10609 
10610 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10611 		if (!ch->initialized)
10612 			break;
10613 		/* Proceed with creation of channel (VMDq2) VSI */
10614 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10615 		if (ret) {
10616 			dev_info(&vsi->back->pdev->dev,
10617 				 "failed to rebuild channels using uplink_seid %u\n",
10618 				 vsi->uplink_seid);
10619 			return ret;
10620 		}
10621 		/* Reconfigure TX queues using QTX_CTL register */
10622 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10623 		if (ret) {
10624 			dev_info(&vsi->back->pdev->dev,
10625 				 "failed to configure TX rings for channel %u\n",
10626 				 ch->seid);
10627 			return ret;
10628 		}
10629 		/* update 'next_base_queue' */
10630 		vsi->next_base_queue = vsi->next_base_queue +
10631 							ch->num_queue_pairs;
10632 		if (ch->max_tx_rate) {
10633 			u64 credits = ch->max_tx_rate;
10634 
10635 			if (i40e_set_bw_limit(vsi, ch->seid,
10636 					      ch->max_tx_rate))
10637 				return -EINVAL;
10638 
10639 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10640 			dev_dbg(&vsi->back->pdev->dev,
10641 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10642 				ch->max_tx_rate,
10643 				credits,
10644 				ch->seid);
10645 		}
10646 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10647 		if (ret) {
10648 			dev_dbg(&vsi->back->pdev->dev,
10649 				"Failed to rebuild cloud filters for channel VSI %u\n",
10650 				ch->seid);
10651 			return ret;
10652 		}
10653 	}
10654 	return 0;
10655 }
10656 
10657 /**
10658  * i40e_clean_xps_state - clean xps state for every tx_ring
10659  * @vsi: ptr to the VSI
10660  **/
i40e_clean_xps_state(struct i40e_vsi * vsi)10661 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10662 {
10663 	int i;
10664 
10665 	if (vsi->tx_rings)
10666 		for (i = 0; i < vsi->num_queue_pairs; i++)
10667 			if (vsi->tx_rings[i])
10668 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10669 					  vsi->tx_rings[i]->state);
10670 }
10671 
10672 /**
10673  * i40e_prep_for_reset - prep for the core to reset
10674  * @pf: board private structure
10675  *
10676  * Close up the VFs and other things in prep for PF Reset.
10677   **/
i40e_prep_for_reset(struct i40e_pf * pf)10678 static void i40e_prep_for_reset(struct i40e_pf *pf)
10679 {
10680 	struct i40e_hw *hw = &pf->hw;
10681 	i40e_status ret = 0;
10682 	u32 v;
10683 
10684 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10685 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10686 		return;
10687 	if (i40e_check_asq_alive(&pf->hw))
10688 		i40e_vc_notify_reset(pf);
10689 
10690 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10691 
10692 	/* quiesce the VSIs and their queues that are not already DOWN */
10693 	i40e_pf_quiesce_all_vsi(pf);
10694 
10695 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10696 		if (pf->vsi[v]) {
10697 			i40e_clean_xps_state(pf->vsi[v]);
10698 			pf->vsi[v]->seid = 0;
10699 		}
10700 	}
10701 
10702 	i40e_shutdown_adminq(&pf->hw);
10703 
10704 	/* call shutdown HMC */
10705 	if (hw->hmc.hmc_obj) {
10706 		ret = i40e_shutdown_lan_hmc(hw);
10707 		if (ret)
10708 			dev_warn(&pf->pdev->dev,
10709 				 "shutdown_lan_hmc failed: %d\n", ret);
10710 	}
10711 
10712 	/* Save the current PTP time so that we can restore the time after the
10713 	 * reset completes.
10714 	 */
10715 	i40e_ptp_save_hw_time(pf);
10716 }
10717 
10718 /**
10719  * i40e_send_version - update firmware with driver version
10720  * @pf: PF struct
10721  */
i40e_send_version(struct i40e_pf * pf)10722 static void i40e_send_version(struct i40e_pf *pf)
10723 {
10724 	struct i40e_driver_version dv;
10725 
10726 	dv.major_version = 0xff;
10727 	dv.minor_version = 0xff;
10728 	dv.build_version = 0xff;
10729 	dv.subbuild_version = 0;
10730 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10731 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10732 }
10733 
10734 /**
10735  * i40e_get_oem_version - get OEM specific version information
10736  * @hw: pointer to the hardware structure
10737  **/
i40e_get_oem_version(struct i40e_hw * hw)10738 static void i40e_get_oem_version(struct i40e_hw *hw)
10739 {
10740 	u16 block_offset = 0xffff;
10741 	u16 block_length = 0;
10742 	u16 capabilities = 0;
10743 	u16 gen_snap = 0;
10744 	u16 release = 0;
10745 
10746 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10747 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10748 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10749 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10750 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10751 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10752 #define I40E_NVM_OEM_LENGTH			3
10753 
10754 	/* Check if pointer to OEM version block is valid. */
10755 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10756 	if (block_offset == 0xffff)
10757 		return;
10758 
10759 	/* Check if OEM version block has correct length. */
10760 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10761 			   &block_length);
10762 	if (block_length < I40E_NVM_OEM_LENGTH)
10763 		return;
10764 
10765 	/* Check if OEM version format is as expected. */
10766 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10767 			   &capabilities);
10768 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10769 		return;
10770 
10771 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10772 			   &gen_snap);
10773 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10774 			   &release);
10775 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10776 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10777 }
10778 
10779 /**
10780  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10781  * @pf: board private structure
10782  **/
i40e_reset(struct i40e_pf * pf)10783 static int i40e_reset(struct i40e_pf *pf)
10784 {
10785 	struct i40e_hw *hw = &pf->hw;
10786 	i40e_status ret;
10787 
10788 	ret = i40e_pf_reset(hw);
10789 	if (ret) {
10790 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10791 		set_bit(__I40E_RESET_FAILED, pf->state);
10792 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10793 	} else {
10794 		pf->pfr_count++;
10795 	}
10796 	return ret;
10797 }
10798 
10799 /**
10800  * i40e_rebuild - rebuild using a saved config
10801  * @pf: board private structure
10802  * @reinit: if the Main VSI needs to re-initialized.
10803  * @lock_acquired: indicates whether or not the lock has been acquired
10804  * before this function was called.
10805  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10806 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10807 {
10808 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10809 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10810 	struct i40e_hw *hw = &pf->hw;
10811 	i40e_status ret;
10812 	u32 val;
10813 	int v;
10814 
10815 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10816 	    is_recovery_mode_reported)
10817 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10818 
10819 	if (test_bit(__I40E_DOWN, pf->state) &&
10820 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10821 		goto clear_recovery;
10822 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10823 
10824 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10825 	ret = i40e_init_adminq(&pf->hw);
10826 	if (ret) {
10827 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10828 			 i40e_stat_str(&pf->hw, ret),
10829 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10830 		goto clear_recovery;
10831 	}
10832 	i40e_get_oem_version(&pf->hw);
10833 
10834 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10835 		/* The following delay is necessary for firmware update. */
10836 		mdelay(1000);
10837 	}
10838 
10839 	/* re-verify the eeprom if we just had an EMP reset */
10840 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10841 		i40e_verify_eeprom(pf);
10842 
10843 	/* if we are going out of or into recovery mode we have to act
10844 	 * accordingly with regard to resources initialization
10845 	 * and deinitialization
10846 	 */
10847 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10848 		if (i40e_get_capabilities(pf,
10849 					  i40e_aqc_opc_list_func_capabilities))
10850 			goto end_unlock;
10851 
10852 		if (is_recovery_mode_reported) {
10853 			/* we're staying in recovery mode so we'll reinitialize
10854 			 * misc vector here
10855 			 */
10856 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10857 				goto end_unlock;
10858 		} else {
10859 			if (!lock_acquired)
10860 				rtnl_lock();
10861 			/* we're going out of recovery mode so we'll free
10862 			 * the IRQ allocated specifically for recovery mode
10863 			 * and restore the interrupt scheme
10864 			 */
10865 			free_irq(pf->pdev->irq, pf);
10866 			i40e_clear_interrupt_scheme(pf);
10867 			if (i40e_restore_interrupt_scheme(pf))
10868 				goto end_unlock;
10869 		}
10870 
10871 		/* tell the firmware that we're starting */
10872 		i40e_send_version(pf);
10873 
10874 		/* bail out in case recovery mode was detected, as there is
10875 		 * no need for further configuration.
10876 		 */
10877 		goto end_unlock;
10878 	}
10879 
10880 	i40e_clear_pxe_mode(hw);
10881 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10882 	if (ret)
10883 		goto end_core_reset;
10884 
10885 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10886 				hw->func_caps.num_rx_qp, 0, 0);
10887 	if (ret) {
10888 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10889 		goto end_core_reset;
10890 	}
10891 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10892 	if (ret) {
10893 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10894 		goto end_core_reset;
10895 	}
10896 
10897 #ifdef CONFIG_I40E_DCB
10898 	/* Enable FW to write a default DCB config on link-up
10899 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10900 	 * is not supported with new link speed
10901 	 */
10902 	if (i40e_is_tc_mqprio_enabled(pf)) {
10903 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10904 	} else {
10905 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10906 		    (hw->phy.link_info.link_speed &
10907 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10908 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10909 			dev_warn(&pf->pdev->dev,
10910 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10911 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10912 		} else {
10913 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10914 			ret = i40e_init_pf_dcb(pf);
10915 			if (ret) {
10916 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10917 					 ret);
10918 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10919 				/* Continue without DCB enabled */
10920 			}
10921 		}
10922 	}
10923 
10924 #endif /* CONFIG_I40E_DCB */
10925 	if (!lock_acquired)
10926 		rtnl_lock();
10927 	ret = i40e_setup_pf_switch(pf, reinit, true);
10928 	if (ret)
10929 		goto end_unlock;
10930 
10931 	/* The driver only wants link up/down and module qualification
10932 	 * reports from firmware.  Note the negative logic.
10933 	 */
10934 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10935 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10936 					 I40E_AQ_EVENT_MEDIA_NA |
10937 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10938 	if (ret)
10939 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10940 			 i40e_stat_str(&pf->hw, ret),
10941 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10942 
10943 	/* Rebuild the VSIs and VEBs that existed before reset.
10944 	 * They are still in our local switch element arrays, so only
10945 	 * need to rebuild the switch model in the HW.
10946 	 *
10947 	 * If there were VEBs but the reconstitution failed, we'll try
10948 	 * to recover minimal use by getting the basic PF VSI working.
10949 	 */
10950 	if (vsi->uplink_seid != pf->mac_seid) {
10951 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10952 		/* find the one VEB connected to the MAC, and find orphans */
10953 		for (v = 0; v < I40E_MAX_VEB; v++) {
10954 			if (!pf->veb[v])
10955 				continue;
10956 
10957 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10958 			    pf->veb[v]->uplink_seid == 0) {
10959 				ret = i40e_reconstitute_veb(pf->veb[v]);
10960 
10961 				if (!ret)
10962 					continue;
10963 
10964 				/* If Main VEB failed, we're in deep doodoo,
10965 				 * so give up rebuilding the switch and set up
10966 				 * for minimal rebuild of PF VSI.
10967 				 * If orphan failed, we'll report the error
10968 				 * but try to keep going.
10969 				 */
10970 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10971 					dev_info(&pf->pdev->dev,
10972 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10973 						 ret);
10974 					vsi->uplink_seid = pf->mac_seid;
10975 					break;
10976 				} else if (pf->veb[v]->uplink_seid == 0) {
10977 					dev_info(&pf->pdev->dev,
10978 						 "rebuild of orphan VEB failed: %d\n",
10979 						 ret);
10980 				}
10981 			}
10982 		}
10983 	}
10984 
10985 	if (vsi->uplink_seid == pf->mac_seid) {
10986 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10987 		/* no VEB, so rebuild only the Main VSI */
10988 		ret = i40e_add_vsi(vsi);
10989 		if (ret) {
10990 			dev_info(&pf->pdev->dev,
10991 				 "rebuild of Main VSI failed: %d\n", ret);
10992 			goto end_unlock;
10993 		}
10994 	}
10995 
10996 	if (vsi->mqprio_qopt.max_rate[0]) {
10997 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
10998 						  vsi->mqprio_qopt.max_rate[0]);
10999 		u64 credits = 0;
11000 
11001 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11002 		if (ret)
11003 			goto end_unlock;
11004 
11005 		credits = max_tx_rate;
11006 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11007 		dev_dbg(&vsi->back->pdev->dev,
11008 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11009 			max_tx_rate,
11010 			credits,
11011 			vsi->seid);
11012 	}
11013 
11014 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11015 	if (ret)
11016 		goto end_unlock;
11017 
11018 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11019 	 * for this main VSI if they exist
11020 	 */
11021 	ret = i40e_rebuild_channels(vsi);
11022 	if (ret)
11023 		goto end_unlock;
11024 
11025 	/* Reconfigure hardware for allowing smaller MSS in the case
11026 	 * of TSO, so that we avoid the MDD being fired and causing
11027 	 * a reset in the case of small MSS+TSO.
11028 	 */
11029 #define I40E_REG_MSS          0x000E64DC
11030 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11031 #define I40E_64BYTE_MSS       0x400000
11032 	val = rd32(hw, I40E_REG_MSS);
11033 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11034 		val &= ~I40E_REG_MSS_MIN_MASK;
11035 		val |= I40E_64BYTE_MSS;
11036 		wr32(hw, I40E_REG_MSS, val);
11037 	}
11038 
11039 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11040 		msleep(75);
11041 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11042 		if (ret)
11043 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11044 				 i40e_stat_str(&pf->hw, ret),
11045 				 i40e_aq_str(&pf->hw,
11046 					     pf->hw.aq.asq_last_status));
11047 	}
11048 	/* reinit the misc interrupt */
11049 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11050 		ret = i40e_setup_misc_vector(pf);
11051 
11052 	/* Add a filter to drop all Flow control frames from any VSI from being
11053 	 * transmitted. By doing so we stop a malicious VF from sending out
11054 	 * PAUSE or PFC frames and potentially controlling traffic for other
11055 	 * PF/VF VSIs.
11056 	 * The FW can still send Flow control frames if enabled.
11057 	 */
11058 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11059 						       pf->main_vsi_seid);
11060 
11061 	/* restart the VSIs that were rebuilt and running before the reset */
11062 	i40e_pf_unquiesce_all_vsi(pf);
11063 
11064 	/* Release the RTNL lock before we start resetting VFs */
11065 	if (!lock_acquired)
11066 		rtnl_unlock();
11067 
11068 	/* Restore promiscuous settings */
11069 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11070 	if (ret)
11071 		dev_warn(&pf->pdev->dev,
11072 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
11073 			 pf->cur_promisc ? "on" : "off",
11074 			 i40e_stat_str(&pf->hw, ret),
11075 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11076 
11077 	i40e_reset_all_vfs(pf, true);
11078 
11079 	/* tell the firmware that we're starting */
11080 	i40e_send_version(pf);
11081 
11082 	/* We've already released the lock, so don't do it again */
11083 	goto end_core_reset;
11084 
11085 end_unlock:
11086 	if (!lock_acquired)
11087 		rtnl_unlock();
11088 end_core_reset:
11089 	clear_bit(__I40E_RESET_FAILED, pf->state);
11090 clear_recovery:
11091 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11092 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11093 }
11094 
11095 /**
11096  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11097  * @pf: board private structure
11098  * @reinit: if the Main VSI needs to re-initialized.
11099  * @lock_acquired: indicates whether or not the lock has been acquired
11100  * before this function was called.
11101  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)11102 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11103 				   bool lock_acquired)
11104 {
11105 	int ret;
11106 
11107 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11108 		return;
11109 	/* Now we wait for GRST to settle out.
11110 	 * We don't have to delete the VEBs or VSIs from the hw switch
11111 	 * because the reset will make them disappear.
11112 	 */
11113 	ret = i40e_reset(pf);
11114 	if (!ret)
11115 		i40e_rebuild(pf, reinit, lock_acquired);
11116 }
11117 
11118 /**
11119  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11120  * @pf: board private structure
11121  *
11122  * Close up the VFs and other things in prep for a Core Reset,
11123  * then get ready to rebuild the world.
11124  * @lock_acquired: indicates whether or not the lock has been acquired
11125  * before this function was called.
11126  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)11127 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11128 {
11129 	i40e_prep_for_reset(pf);
11130 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11131 }
11132 
11133 /**
11134  * i40e_handle_mdd_event
11135  * @pf: pointer to the PF structure
11136  *
11137  * Called from the MDD irq handler to identify possibly malicious vfs
11138  **/
i40e_handle_mdd_event(struct i40e_pf * pf)11139 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11140 {
11141 	struct i40e_hw *hw = &pf->hw;
11142 	bool mdd_detected = false;
11143 	struct i40e_vf *vf;
11144 	u32 reg;
11145 	int i;
11146 
11147 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11148 		return;
11149 
11150 	/* find what triggered the MDD event */
11151 	reg = rd32(hw, I40E_GL_MDET_TX);
11152 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11153 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11154 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11155 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11156 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11157 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11158 				I40E_GL_MDET_TX_EVENT_SHIFT;
11159 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11160 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11161 				pf->hw.func_caps.base_queue;
11162 		if (netif_msg_tx_err(pf))
11163 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11164 				 event, queue, pf_num, vf_num);
11165 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11166 		mdd_detected = true;
11167 	}
11168 	reg = rd32(hw, I40E_GL_MDET_RX);
11169 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11170 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11171 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11172 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11173 				I40E_GL_MDET_RX_EVENT_SHIFT;
11174 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11175 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11176 				pf->hw.func_caps.base_queue;
11177 		if (netif_msg_rx_err(pf))
11178 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11179 				 event, queue, func);
11180 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11181 		mdd_detected = true;
11182 	}
11183 
11184 	if (mdd_detected) {
11185 		reg = rd32(hw, I40E_PF_MDET_TX);
11186 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11187 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11188 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11189 		}
11190 		reg = rd32(hw, I40E_PF_MDET_RX);
11191 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11192 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11193 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11194 		}
11195 	}
11196 
11197 	/* see if one of the VFs needs its hand slapped */
11198 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11199 		vf = &(pf->vf[i]);
11200 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11201 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11202 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11203 			vf->num_mdd_events++;
11204 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11205 				 i);
11206 			dev_info(&pf->pdev->dev,
11207 				 "Use PF Control I/F to re-enable the VF\n");
11208 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11209 		}
11210 
11211 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11212 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11213 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11214 			vf->num_mdd_events++;
11215 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11216 				 i);
11217 			dev_info(&pf->pdev->dev,
11218 				 "Use PF Control I/F to re-enable the VF\n");
11219 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11220 		}
11221 	}
11222 
11223 	/* re-enable mdd interrupt cause */
11224 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11225 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11226 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11227 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11228 	i40e_flush(hw);
11229 }
11230 
11231 /**
11232  * i40e_service_task - Run the driver's async subtasks
11233  * @work: pointer to work_struct containing our data
11234  **/
i40e_service_task(struct work_struct * work)11235 static void i40e_service_task(struct work_struct *work)
11236 {
11237 	struct i40e_pf *pf = container_of(work,
11238 					  struct i40e_pf,
11239 					  service_task);
11240 	unsigned long start_time = jiffies;
11241 
11242 	/* don't bother with service tasks if a reset is in progress */
11243 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11244 	    test_bit(__I40E_SUSPENDED, pf->state))
11245 		return;
11246 
11247 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11248 		return;
11249 
11250 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11251 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11252 		i40e_sync_filters_subtask(pf);
11253 		i40e_reset_subtask(pf);
11254 		i40e_handle_mdd_event(pf);
11255 		i40e_vc_process_vflr_event(pf);
11256 		i40e_watchdog_subtask(pf);
11257 		i40e_fdir_reinit_subtask(pf);
11258 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11259 			/* Client subtask will reopen next time through. */
11260 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11261 							   true);
11262 		} else {
11263 			i40e_client_subtask(pf);
11264 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11265 					       pf->state))
11266 				i40e_notify_client_of_l2_param_changes(
11267 								pf->vsi[pf->lan_vsi]);
11268 		}
11269 		i40e_sync_filters_subtask(pf);
11270 	} else {
11271 		i40e_reset_subtask(pf);
11272 	}
11273 
11274 	i40e_clean_adminq_subtask(pf);
11275 
11276 	/* flush memory to make sure state is correct before next watchdog */
11277 	smp_mb__before_atomic();
11278 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11279 
11280 	/* If the tasks have taken longer than one timer cycle or there
11281 	 * is more work to be done, reschedule the service task now
11282 	 * rather than wait for the timer to tick again.
11283 	 */
11284 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11285 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11286 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11287 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11288 		i40e_service_event_schedule(pf);
11289 }
11290 
11291 /**
11292  * i40e_service_timer - timer callback
11293  * @t: timer list pointer
11294  **/
i40e_service_timer(struct timer_list * t)11295 static void i40e_service_timer(struct timer_list *t)
11296 {
11297 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11298 
11299 	mod_timer(&pf->service_timer,
11300 		  round_jiffies(jiffies + pf->service_timer_period));
11301 	i40e_service_event_schedule(pf);
11302 }
11303 
11304 /**
11305  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11306  * @vsi: the VSI being configured
11307  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)11308 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11309 {
11310 	struct i40e_pf *pf = vsi->back;
11311 
11312 	switch (vsi->type) {
11313 	case I40E_VSI_MAIN:
11314 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11315 		if (!vsi->num_tx_desc)
11316 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11317 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11318 		if (!vsi->num_rx_desc)
11319 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11320 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11321 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11322 			vsi->num_q_vectors = pf->num_lan_msix;
11323 		else
11324 			vsi->num_q_vectors = 1;
11325 
11326 		break;
11327 
11328 	case I40E_VSI_FDIR:
11329 		vsi->alloc_queue_pairs = 1;
11330 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11331 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11332 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11333 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11334 		vsi->num_q_vectors = pf->num_fdsb_msix;
11335 		break;
11336 
11337 	case I40E_VSI_VMDQ2:
11338 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11339 		if (!vsi->num_tx_desc)
11340 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11341 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11342 		if (!vsi->num_rx_desc)
11343 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11344 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11345 		vsi->num_q_vectors = pf->num_vmdq_msix;
11346 		break;
11347 
11348 	case I40E_VSI_SRIOV:
11349 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11350 		if (!vsi->num_tx_desc)
11351 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11352 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11353 		if (!vsi->num_rx_desc)
11354 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11355 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11356 		break;
11357 
11358 	default:
11359 		WARN_ON(1);
11360 		return -ENODATA;
11361 	}
11362 
11363 	if (is_kdump_kernel()) {
11364 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11365 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11366 	}
11367 
11368 	return 0;
11369 }
11370 
11371 /**
11372  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11373  * @vsi: VSI pointer
11374  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11375  *
11376  * On error: returns error code (negative)
11377  * On success: returns 0
11378  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)11379 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11380 {
11381 	struct i40e_ring **next_rings;
11382 	int size;
11383 	int ret = 0;
11384 
11385 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11386 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11387 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11388 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11389 	if (!vsi->tx_rings)
11390 		return -ENOMEM;
11391 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11392 	if (i40e_enabled_xdp_vsi(vsi)) {
11393 		vsi->xdp_rings = next_rings;
11394 		next_rings += vsi->alloc_queue_pairs;
11395 	}
11396 	vsi->rx_rings = next_rings;
11397 
11398 	if (alloc_qvectors) {
11399 		/* allocate memory for q_vector pointers */
11400 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11401 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11402 		if (!vsi->q_vectors) {
11403 			ret = -ENOMEM;
11404 			goto err_vectors;
11405 		}
11406 	}
11407 	return ret;
11408 
11409 err_vectors:
11410 	kfree(vsi->tx_rings);
11411 	return ret;
11412 }
11413 
11414 /**
11415  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11416  * @pf: board private structure
11417  * @type: type of VSI
11418  *
11419  * On error: returns error code (negative)
11420  * On success: returns vsi index in PF (positive)
11421  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)11422 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11423 {
11424 	int ret = -ENODEV;
11425 	struct i40e_vsi *vsi;
11426 	int vsi_idx;
11427 	int i;
11428 
11429 	/* Need to protect the allocation of the VSIs at the PF level */
11430 	mutex_lock(&pf->switch_mutex);
11431 
11432 	/* VSI list may be fragmented if VSI creation/destruction has
11433 	 * been happening.  We can afford to do a quick scan to look
11434 	 * for any free VSIs in the list.
11435 	 *
11436 	 * find next empty vsi slot, looping back around if necessary
11437 	 */
11438 	i = pf->next_vsi;
11439 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11440 		i++;
11441 	if (i >= pf->num_alloc_vsi) {
11442 		i = 0;
11443 		while (i < pf->next_vsi && pf->vsi[i])
11444 			i++;
11445 	}
11446 
11447 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11448 		vsi_idx = i;             /* Found one! */
11449 	} else {
11450 		ret = -ENODEV;
11451 		goto unlock_pf;  /* out of VSI slots! */
11452 	}
11453 	pf->next_vsi = ++i;
11454 
11455 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11456 	if (!vsi) {
11457 		ret = -ENOMEM;
11458 		goto unlock_pf;
11459 	}
11460 	vsi->type = type;
11461 	vsi->back = pf;
11462 	set_bit(__I40E_VSI_DOWN, vsi->state);
11463 	vsi->flags = 0;
11464 	vsi->idx = vsi_idx;
11465 	vsi->int_rate_limit = 0;
11466 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11467 				pf->rss_table_size : 64;
11468 	vsi->netdev_registered = false;
11469 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11470 	hash_init(vsi->mac_filter_hash);
11471 	vsi->irqs_ready = false;
11472 
11473 	if (type == I40E_VSI_MAIN) {
11474 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11475 		if (!vsi->af_xdp_zc_qps)
11476 			goto err_rings;
11477 	}
11478 
11479 	ret = i40e_set_num_rings_in_vsi(vsi);
11480 	if (ret)
11481 		goto err_rings;
11482 
11483 	ret = i40e_vsi_alloc_arrays(vsi, true);
11484 	if (ret)
11485 		goto err_rings;
11486 
11487 	/* Setup default MSIX irq handler for VSI */
11488 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11489 
11490 	/* Initialize VSI lock */
11491 	spin_lock_init(&vsi->mac_filter_hash_lock);
11492 	pf->vsi[vsi_idx] = vsi;
11493 	ret = vsi_idx;
11494 	goto unlock_pf;
11495 
11496 err_rings:
11497 	bitmap_free(vsi->af_xdp_zc_qps);
11498 	pf->next_vsi = i - 1;
11499 	kfree(vsi);
11500 unlock_pf:
11501 	mutex_unlock(&pf->switch_mutex);
11502 	return ret;
11503 }
11504 
11505 /**
11506  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11507  * @vsi: VSI pointer
11508  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11509  *
11510  * On error: returns error code (negative)
11511  * On success: returns 0
11512  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)11513 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11514 {
11515 	/* free the ring and vector containers */
11516 	if (free_qvectors) {
11517 		kfree(vsi->q_vectors);
11518 		vsi->q_vectors = NULL;
11519 	}
11520 	kfree(vsi->tx_rings);
11521 	vsi->tx_rings = NULL;
11522 	vsi->rx_rings = NULL;
11523 	vsi->xdp_rings = NULL;
11524 }
11525 
11526 /**
11527  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11528  * and lookup table
11529  * @vsi: Pointer to VSI structure
11530  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)11531 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11532 {
11533 	if (!vsi)
11534 		return;
11535 
11536 	kfree(vsi->rss_hkey_user);
11537 	vsi->rss_hkey_user = NULL;
11538 
11539 	kfree(vsi->rss_lut_user);
11540 	vsi->rss_lut_user = NULL;
11541 }
11542 
11543 /**
11544  * i40e_vsi_clear - Deallocate the VSI provided
11545  * @vsi: the VSI being un-configured
11546  **/
i40e_vsi_clear(struct i40e_vsi * vsi)11547 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11548 {
11549 	struct i40e_pf *pf;
11550 
11551 	if (!vsi)
11552 		return 0;
11553 
11554 	if (!vsi->back)
11555 		goto free_vsi;
11556 	pf = vsi->back;
11557 
11558 	mutex_lock(&pf->switch_mutex);
11559 	if (!pf->vsi[vsi->idx]) {
11560 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11561 			vsi->idx, vsi->idx, vsi->type);
11562 		goto unlock_vsi;
11563 	}
11564 
11565 	if (pf->vsi[vsi->idx] != vsi) {
11566 		dev_err(&pf->pdev->dev,
11567 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11568 			pf->vsi[vsi->idx]->idx,
11569 			pf->vsi[vsi->idx]->type,
11570 			vsi->idx, vsi->type);
11571 		goto unlock_vsi;
11572 	}
11573 
11574 	/* updates the PF for this cleared vsi */
11575 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11576 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11577 
11578 	bitmap_free(vsi->af_xdp_zc_qps);
11579 	i40e_vsi_free_arrays(vsi, true);
11580 	i40e_clear_rss_config_user(vsi);
11581 
11582 	pf->vsi[vsi->idx] = NULL;
11583 	if (vsi->idx < pf->next_vsi)
11584 		pf->next_vsi = vsi->idx;
11585 
11586 unlock_vsi:
11587 	mutex_unlock(&pf->switch_mutex);
11588 free_vsi:
11589 	kfree(vsi);
11590 
11591 	return 0;
11592 }
11593 
11594 /**
11595  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11596  * @vsi: the VSI being cleaned
11597  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)11598 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11599 {
11600 	int i;
11601 
11602 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11603 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11604 			kfree_rcu(vsi->tx_rings[i], rcu);
11605 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11606 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11607 			if (vsi->xdp_rings)
11608 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11609 		}
11610 	}
11611 }
11612 
11613 /**
11614  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11615  * @vsi: the VSI being configured
11616  **/
i40e_alloc_rings(struct i40e_vsi * vsi)11617 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11618 {
11619 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11620 	struct i40e_pf *pf = vsi->back;
11621 	struct i40e_ring *ring;
11622 
11623 	/* Set basic values in the rings to be used later during open() */
11624 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11625 		/* allocate space for both Tx and Rx in one shot */
11626 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11627 		if (!ring)
11628 			goto err_out;
11629 
11630 		ring->queue_index = i;
11631 		ring->reg_idx = vsi->base_queue + i;
11632 		ring->ring_active = false;
11633 		ring->vsi = vsi;
11634 		ring->netdev = vsi->netdev;
11635 		ring->dev = &pf->pdev->dev;
11636 		ring->count = vsi->num_tx_desc;
11637 		ring->size = 0;
11638 		ring->dcb_tc = 0;
11639 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11640 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11641 		ring->itr_setting = pf->tx_itr_default;
11642 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11643 
11644 		if (!i40e_enabled_xdp_vsi(vsi))
11645 			goto setup_rx;
11646 
11647 		ring->queue_index = vsi->alloc_queue_pairs + i;
11648 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11649 		ring->ring_active = false;
11650 		ring->vsi = vsi;
11651 		ring->netdev = NULL;
11652 		ring->dev = &pf->pdev->dev;
11653 		ring->count = vsi->num_tx_desc;
11654 		ring->size = 0;
11655 		ring->dcb_tc = 0;
11656 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11657 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11658 		set_ring_xdp(ring);
11659 		ring->itr_setting = pf->tx_itr_default;
11660 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11661 
11662 setup_rx:
11663 		ring->queue_index = i;
11664 		ring->reg_idx = vsi->base_queue + i;
11665 		ring->ring_active = false;
11666 		ring->vsi = vsi;
11667 		ring->netdev = vsi->netdev;
11668 		ring->dev = &pf->pdev->dev;
11669 		ring->count = vsi->num_rx_desc;
11670 		ring->size = 0;
11671 		ring->dcb_tc = 0;
11672 		ring->itr_setting = pf->rx_itr_default;
11673 		WRITE_ONCE(vsi->rx_rings[i], ring);
11674 	}
11675 
11676 	return 0;
11677 
11678 err_out:
11679 	i40e_vsi_clear_rings(vsi);
11680 	return -ENOMEM;
11681 }
11682 
11683 /**
11684  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11685  * @pf: board private structure
11686  * @vectors: the number of MSI-X vectors to request
11687  *
11688  * Returns the number of vectors reserved, or error
11689  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)11690 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11691 {
11692 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11693 					I40E_MIN_MSIX, vectors);
11694 	if (vectors < 0) {
11695 		dev_info(&pf->pdev->dev,
11696 			 "MSI-X vector reservation failed: %d\n", vectors);
11697 		vectors = 0;
11698 	}
11699 
11700 	return vectors;
11701 }
11702 
11703 /**
11704  * i40e_init_msix - Setup the MSIX capability
11705  * @pf: board private structure
11706  *
11707  * Work with the OS to set up the MSIX vectors needed.
11708  *
11709  * Returns the number of vectors reserved or negative on failure
11710  **/
i40e_init_msix(struct i40e_pf * pf)11711 static int i40e_init_msix(struct i40e_pf *pf)
11712 {
11713 	struct i40e_hw *hw = &pf->hw;
11714 	int cpus, extra_vectors;
11715 	int vectors_left;
11716 	int v_budget, i;
11717 	int v_actual;
11718 	int iwarp_requested = 0;
11719 
11720 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11721 		return -ENODEV;
11722 
11723 	/* The number of vectors we'll request will be comprised of:
11724 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11725 	 *   - The number of LAN queue pairs
11726 	 *	- Queues being used for RSS.
11727 	 *		We don't need as many as max_rss_size vectors.
11728 	 *		use rss_size instead in the calculation since that
11729 	 *		is governed by number of cpus in the system.
11730 	 *	- assumes symmetric Tx/Rx pairing
11731 	 *   - The number of VMDq pairs
11732 	 *   - The CPU count within the NUMA node if iWARP is enabled
11733 	 * Once we count this up, try the request.
11734 	 *
11735 	 * If we can't get what we want, we'll simplify to nearly nothing
11736 	 * and try again.  If that still fails, we punt.
11737 	 */
11738 	vectors_left = hw->func_caps.num_msix_vectors;
11739 	v_budget = 0;
11740 
11741 	/* reserve one vector for miscellaneous handler */
11742 	if (vectors_left) {
11743 		v_budget++;
11744 		vectors_left--;
11745 	}
11746 
11747 	/* reserve some vectors for the main PF traffic queues. Initially we
11748 	 * only reserve at most 50% of the available vectors, in the case that
11749 	 * the number of online CPUs is large. This ensures that we can enable
11750 	 * extra features as well. Once we've enabled the other features, we
11751 	 * will use any remaining vectors to reach as close as we can to the
11752 	 * number of online CPUs.
11753 	 */
11754 	cpus = num_online_cpus();
11755 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11756 	vectors_left -= pf->num_lan_msix;
11757 
11758 	/* reserve one vector for sideband flow director */
11759 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11760 		if (vectors_left) {
11761 			pf->num_fdsb_msix = 1;
11762 			v_budget++;
11763 			vectors_left--;
11764 		} else {
11765 			pf->num_fdsb_msix = 0;
11766 		}
11767 	}
11768 
11769 	/* can we reserve enough for iWARP? */
11770 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11771 		iwarp_requested = pf->num_iwarp_msix;
11772 
11773 		if (!vectors_left)
11774 			pf->num_iwarp_msix = 0;
11775 		else if (vectors_left < pf->num_iwarp_msix)
11776 			pf->num_iwarp_msix = 1;
11777 		v_budget += pf->num_iwarp_msix;
11778 		vectors_left -= pf->num_iwarp_msix;
11779 	}
11780 
11781 	/* any vectors left over go for VMDq support */
11782 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11783 		if (!vectors_left) {
11784 			pf->num_vmdq_msix = 0;
11785 			pf->num_vmdq_qps = 0;
11786 		} else {
11787 			int vmdq_vecs_wanted =
11788 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11789 			int vmdq_vecs =
11790 				min_t(int, vectors_left, vmdq_vecs_wanted);
11791 
11792 			/* if we're short on vectors for what's desired, we limit
11793 			 * the queues per vmdq.  If this is still more than are
11794 			 * available, the user will need to change the number of
11795 			 * queues/vectors used by the PF later with the ethtool
11796 			 * channels command
11797 			 */
11798 			if (vectors_left < vmdq_vecs_wanted) {
11799 				pf->num_vmdq_qps = 1;
11800 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11801 				vmdq_vecs = min_t(int,
11802 						  vectors_left,
11803 						  vmdq_vecs_wanted);
11804 			}
11805 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11806 
11807 			v_budget += vmdq_vecs;
11808 			vectors_left -= vmdq_vecs;
11809 		}
11810 	}
11811 
11812 	/* On systems with a large number of SMP cores, we previously limited
11813 	 * the number of vectors for num_lan_msix to be at most 50% of the
11814 	 * available vectors, to allow for other features. Now, we add back
11815 	 * the remaining vectors. However, we ensure that the total
11816 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11817 	 * calculate the number of vectors we can add without going over the
11818 	 * cap of CPUs. For systems with a small number of CPUs this will be
11819 	 * zero.
11820 	 */
11821 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11822 	pf->num_lan_msix += extra_vectors;
11823 	vectors_left -= extra_vectors;
11824 
11825 	WARN(vectors_left < 0,
11826 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11827 
11828 	v_budget += pf->num_lan_msix;
11829 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11830 				   GFP_KERNEL);
11831 	if (!pf->msix_entries)
11832 		return -ENOMEM;
11833 
11834 	for (i = 0; i < v_budget; i++)
11835 		pf->msix_entries[i].entry = i;
11836 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11837 
11838 	if (v_actual < I40E_MIN_MSIX) {
11839 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11840 		kfree(pf->msix_entries);
11841 		pf->msix_entries = NULL;
11842 		pci_disable_msix(pf->pdev);
11843 		return -ENODEV;
11844 
11845 	} else if (v_actual == I40E_MIN_MSIX) {
11846 		/* Adjust for minimal MSIX use */
11847 		pf->num_vmdq_vsis = 0;
11848 		pf->num_vmdq_qps = 0;
11849 		pf->num_lan_qps = 1;
11850 		pf->num_lan_msix = 1;
11851 
11852 	} else if (v_actual != v_budget) {
11853 		/* If we have limited resources, we will start with no vectors
11854 		 * for the special features and then allocate vectors to some
11855 		 * of these features based on the policy and at the end disable
11856 		 * the features that did not get any vectors.
11857 		 */
11858 		int vec;
11859 
11860 		dev_info(&pf->pdev->dev,
11861 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11862 			 v_actual, v_budget);
11863 		/* reserve the misc vector */
11864 		vec = v_actual - 1;
11865 
11866 		/* Scale vector usage down */
11867 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11868 		pf->num_vmdq_vsis = 1;
11869 		pf->num_vmdq_qps = 1;
11870 
11871 		/* partition out the remaining vectors */
11872 		switch (vec) {
11873 		case 2:
11874 			pf->num_lan_msix = 1;
11875 			break;
11876 		case 3:
11877 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11878 				pf->num_lan_msix = 1;
11879 				pf->num_iwarp_msix = 1;
11880 			} else {
11881 				pf->num_lan_msix = 2;
11882 			}
11883 			break;
11884 		default:
11885 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11886 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11887 						 iwarp_requested);
11888 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11889 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11890 			} else {
11891 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11892 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11893 			}
11894 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11895 				pf->num_fdsb_msix = 1;
11896 				vec--;
11897 			}
11898 			pf->num_lan_msix = min_t(int,
11899 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11900 							      pf->num_lan_msix);
11901 			pf->num_lan_qps = pf->num_lan_msix;
11902 			break;
11903 		}
11904 	}
11905 
11906 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11907 	    (pf->num_fdsb_msix == 0)) {
11908 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11909 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11910 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11911 	}
11912 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11913 	    (pf->num_vmdq_msix == 0)) {
11914 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11915 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11916 	}
11917 
11918 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11919 	    (pf->num_iwarp_msix == 0)) {
11920 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11921 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11922 	}
11923 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11924 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11925 		   pf->num_lan_msix,
11926 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11927 		   pf->num_fdsb_msix,
11928 		   pf->num_iwarp_msix);
11929 
11930 	return v_actual;
11931 }
11932 
11933 /**
11934  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11935  * @vsi: the VSI being configured
11936  * @v_idx: index of the vector in the vsi struct
11937  *
11938  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11939  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)11940 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11941 {
11942 	struct i40e_q_vector *q_vector;
11943 
11944 	/* allocate q_vector */
11945 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11946 	if (!q_vector)
11947 		return -ENOMEM;
11948 
11949 	q_vector->vsi = vsi;
11950 	q_vector->v_idx = v_idx;
11951 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11952 
11953 	if (vsi->netdev)
11954 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11955 
11956 	/* tie q_vector and vsi together */
11957 	vsi->q_vectors[v_idx] = q_vector;
11958 
11959 	return 0;
11960 }
11961 
11962 /**
11963  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11964  * @vsi: the VSI being configured
11965  *
11966  * We allocate one q_vector per queue interrupt.  If allocation fails we
11967  * return -ENOMEM.
11968  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)11969 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11970 {
11971 	struct i40e_pf *pf = vsi->back;
11972 	int err, v_idx, num_q_vectors;
11973 
11974 	/* if not MSIX, give the one vector only to the LAN VSI */
11975 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11976 		num_q_vectors = vsi->num_q_vectors;
11977 	else if (vsi == pf->vsi[pf->lan_vsi])
11978 		num_q_vectors = 1;
11979 	else
11980 		return -EINVAL;
11981 
11982 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11983 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11984 		if (err)
11985 			goto err_out;
11986 	}
11987 
11988 	return 0;
11989 
11990 err_out:
11991 	while (v_idx--)
11992 		i40e_free_q_vector(vsi, v_idx);
11993 
11994 	return err;
11995 }
11996 
11997 /**
11998  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11999  * @pf: board private structure to initialize
12000  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)12001 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12002 {
12003 	int vectors = 0;
12004 	ssize_t size;
12005 
12006 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12007 		vectors = i40e_init_msix(pf);
12008 		if (vectors < 0) {
12009 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
12010 				       I40E_FLAG_IWARP_ENABLED	|
12011 				       I40E_FLAG_RSS_ENABLED	|
12012 				       I40E_FLAG_DCB_CAPABLE	|
12013 				       I40E_FLAG_DCB_ENABLED	|
12014 				       I40E_FLAG_SRIOV_ENABLED	|
12015 				       I40E_FLAG_FD_SB_ENABLED	|
12016 				       I40E_FLAG_FD_ATR_ENABLED	|
12017 				       I40E_FLAG_VMDQ_ENABLED);
12018 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12019 
12020 			/* rework the queue expectations without MSIX */
12021 			i40e_determine_queue_usage(pf);
12022 		}
12023 	}
12024 
12025 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12026 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12027 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12028 		vectors = pci_enable_msi(pf->pdev);
12029 		if (vectors < 0) {
12030 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12031 				 vectors);
12032 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12033 		}
12034 		vectors = 1;  /* one MSI or Legacy vector */
12035 	}
12036 
12037 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12038 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12039 
12040 	/* set up vector assignment tracking */
12041 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12042 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12043 	if (!pf->irq_pile)
12044 		return -ENOMEM;
12045 
12046 	pf->irq_pile->num_entries = vectors;
12047 
12048 	/* track first vector for misc interrupts, ignore return */
12049 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12050 
12051 	return 0;
12052 }
12053 
12054 /**
12055  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12056  * @pf: private board data structure
12057  *
12058  * Restore the interrupt scheme that was cleared when we suspended the
12059  * device. This should be called during resume to re-allocate the q_vectors
12060  * and reacquire IRQs.
12061  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)12062 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12063 {
12064 	int err, i;
12065 
12066 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12067 	 * scheme. We need to re-enabled them here in order to attempt to
12068 	 * re-acquire the MSI or MSI-X vectors
12069 	 */
12070 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12071 
12072 	err = i40e_init_interrupt_scheme(pf);
12073 	if (err)
12074 		return err;
12075 
12076 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12077 	 * rings together again.
12078 	 */
12079 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12080 		if (pf->vsi[i]) {
12081 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12082 			if (err)
12083 				goto err_unwind;
12084 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12085 		}
12086 	}
12087 
12088 	err = i40e_setup_misc_vector(pf);
12089 	if (err)
12090 		goto err_unwind;
12091 
12092 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12093 		i40e_client_update_msix_info(pf);
12094 
12095 	return 0;
12096 
12097 err_unwind:
12098 	while (i--) {
12099 		if (pf->vsi[i])
12100 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12101 	}
12102 
12103 	return err;
12104 }
12105 
12106 /**
12107  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12108  * non queue events in recovery mode
12109  * @pf: board private structure
12110  *
12111  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12112  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12113  * This is handled differently than in recovery mode since no Tx/Rx resources
12114  * are being allocated.
12115  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)12116 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12117 {
12118 	int err;
12119 
12120 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12121 		err = i40e_setup_misc_vector(pf);
12122 
12123 		if (err) {
12124 			dev_info(&pf->pdev->dev,
12125 				 "MSI-X misc vector request failed, error %d\n",
12126 				 err);
12127 			return err;
12128 		}
12129 	} else {
12130 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12131 
12132 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12133 				  pf->int_name, pf);
12134 
12135 		if (err) {
12136 			dev_info(&pf->pdev->dev,
12137 				 "MSI/legacy misc vector request failed, error %d\n",
12138 				 err);
12139 			return err;
12140 		}
12141 		i40e_enable_misc_int_causes(pf);
12142 		i40e_irq_dynamic_enable_icr0(pf);
12143 	}
12144 
12145 	return 0;
12146 }
12147 
12148 /**
12149  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12150  * @pf: board private structure
12151  *
12152  * This sets up the handler for MSIX 0, which is used to manage the
12153  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12154  * when in MSI or Legacy interrupt mode.
12155  **/
i40e_setup_misc_vector(struct i40e_pf * pf)12156 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12157 {
12158 	struct i40e_hw *hw = &pf->hw;
12159 	int err = 0;
12160 
12161 	/* Only request the IRQ once, the first time through. */
12162 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12163 		err = request_irq(pf->msix_entries[0].vector,
12164 				  i40e_intr, 0, pf->int_name, pf);
12165 		if (err) {
12166 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12167 			dev_info(&pf->pdev->dev,
12168 				 "request_irq for %s failed: %d\n",
12169 				 pf->int_name, err);
12170 			return -EFAULT;
12171 		}
12172 	}
12173 
12174 	i40e_enable_misc_int_causes(pf);
12175 
12176 	/* associate no queues to the misc vector */
12177 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12178 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12179 
12180 	i40e_flush(hw);
12181 
12182 	i40e_irq_dynamic_enable_icr0(pf);
12183 
12184 	return err;
12185 }
12186 
12187 /**
12188  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12189  * @vsi: Pointer to vsi structure
12190  * @seed: Buffter to store the hash keys
12191  * @lut: Buffer to store the lookup table entries
12192  * @lut_size: Size of buffer to store the lookup table entries
12193  *
12194  * Return 0 on success, negative on failure
12195  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)12196 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12197 			   u8 *lut, u16 lut_size)
12198 {
12199 	struct i40e_pf *pf = vsi->back;
12200 	struct i40e_hw *hw = &pf->hw;
12201 	int ret = 0;
12202 
12203 	if (seed) {
12204 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12205 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12206 		if (ret) {
12207 			dev_info(&pf->pdev->dev,
12208 				 "Cannot get RSS key, err %s aq_err %s\n",
12209 				 i40e_stat_str(&pf->hw, ret),
12210 				 i40e_aq_str(&pf->hw,
12211 					     pf->hw.aq.asq_last_status));
12212 			return ret;
12213 		}
12214 	}
12215 
12216 	if (lut) {
12217 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12218 
12219 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12220 		if (ret) {
12221 			dev_info(&pf->pdev->dev,
12222 				 "Cannot get RSS lut, err %s aq_err %s\n",
12223 				 i40e_stat_str(&pf->hw, ret),
12224 				 i40e_aq_str(&pf->hw,
12225 					     pf->hw.aq.asq_last_status));
12226 			return ret;
12227 		}
12228 	}
12229 
12230 	return ret;
12231 }
12232 
12233 /**
12234  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12235  * @vsi: Pointer to vsi structure
12236  * @seed: RSS hash seed
12237  * @lut: Lookup table
12238  * @lut_size: Lookup table size
12239  *
12240  * Returns 0 on success, negative on failure
12241  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)12242 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12243 			       const u8 *lut, u16 lut_size)
12244 {
12245 	struct i40e_pf *pf = vsi->back;
12246 	struct i40e_hw *hw = &pf->hw;
12247 	u16 vf_id = vsi->vf_id;
12248 	u8 i;
12249 
12250 	/* Fill out hash function seed */
12251 	if (seed) {
12252 		u32 *seed_dw = (u32 *)seed;
12253 
12254 		if (vsi->type == I40E_VSI_MAIN) {
12255 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12256 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12257 		} else if (vsi->type == I40E_VSI_SRIOV) {
12258 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12259 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12260 		} else {
12261 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12262 		}
12263 	}
12264 
12265 	if (lut) {
12266 		u32 *lut_dw = (u32 *)lut;
12267 
12268 		if (vsi->type == I40E_VSI_MAIN) {
12269 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12270 				return -EINVAL;
12271 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12272 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12273 		} else if (vsi->type == I40E_VSI_SRIOV) {
12274 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12275 				return -EINVAL;
12276 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12277 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12278 		} else {
12279 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12280 		}
12281 	}
12282 	i40e_flush(hw);
12283 
12284 	return 0;
12285 }
12286 
12287 /**
12288  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12289  * @vsi: Pointer to VSI structure
12290  * @seed: Buffer to store the keys
12291  * @lut: Buffer to store the lookup table entries
12292  * @lut_size: Size of buffer to store the lookup table entries
12293  *
12294  * Returns 0 on success, negative on failure
12295  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12296 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12297 			    u8 *lut, u16 lut_size)
12298 {
12299 	struct i40e_pf *pf = vsi->back;
12300 	struct i40e_hw *hw = &pf->hw;
12301 	u16 i;
12302 
12303 	if (seed) {
12304 		u32 *seed_dw = (u32 *)seed;
12305 
12306 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12307 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12308 	}
12309 	if (lut) {
12310 		u32 *lut_dw = (u32 *)lut;
12311 
12312 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12313 			return -EINVAL;
12314 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12315 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12316 	}
12317 
12318 	return 0;
12319 }
12320 
12321 /**
12322  * i40e_config_rss - Configure RSS keys and lut
12323  * @vsi: Pointer to VSI structure
12324  * @seed: RSS hash seed
12325  * @lut: Lookup table
12326  * @lut_size: Lookup table size
12327  *
12328  * Returns 0 on success, negative on failure
12329  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12330 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12331 {
12332 	struct i40e_pf *pf = vsi->back;
12333 
12334 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12335 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12336 	else
12337 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12338 }
12339 
12340 /**
12341  * i40e_get_rss - Get RSS keys and lut
12342  * @vsi: Pointer to VSI structure
12343  * @seed: Buffer to store the keys
12344  * @lut: Buffer to store the lookup table entries
12345  * @lut_size: Size of buffer to store the lookup table entries
12346  *
12347  * Returns 0 on success, negative on failure
12348  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12349 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12350 {
12351 	struct i40e_pf *pf = vsi->back;
12352 
12353 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12354 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12355 	else
12356 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12357 }
12358 
12359 /**
12360  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12361  * @pf: Pointer to board private structure
12362  * @lut: Lookup table
12363  * @rss_table_size: Lookup table size
12364  * @rss_size: Range of queue number for hashing
12365  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)12366 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12367 		       u16 rss_table_size, u16 rss_size)
12368 {
12369 	u16 i;
12370 
12371 	for (i = 0; i < rss_table_size; i++)
12372 		lut[i] = i % rss_size;
12373 }
12374 
12375 /**
12376  * i40e_pf_config_rss - Prepare for RSS if used
12377  * @pf: board private structure
12378  **/
i40e_pf_config_rss(struct i40e_pf * pf)12379 static int i40e_pf_config_rss(struct i40e_pf *pf)
12380 {
12381 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12382 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12383 	u8 *lut;
12384 	struct i40e_hw *hw = &pf->hw;
12385 	u32 reg_val;
12386 	u64 hena;
12387 	int ret;
12388 
12389 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12390 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12391 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12392 	hena |= i40e_pf_get_default_rss_hena(pf);
12393 
12394 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12395 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12396 
12397 	/* Determine the RSS table size based on the hardware capabilities */
12398 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12399 	reg_val = (pf->rss_table_size == 512) ?
12400 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12401 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12402 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12403 
12404 	/* Determine the RSS size of the VSI */
12405 	if (!vsi->rss_size) {
12406 		u16 qcount;
12407 		/* If the firmware does something weird during VSI init, we
12408 		 * could end up with zero TCs. Check for that to avoid
12409 		 * divide-by-zero. It probably won't pass traffic, but it also
12410 		 * won't panic.
12411 		 */
12412 		qcount = vsi->num_queue_pairs /
12413 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12414 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12415 	}
12416 	if (!vsi->rss_size)
12417 		return -EINVAL;
12418 
12419 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12420 	if (!lut)
12421 		return -ENOMEM;
12422 
12423 	/* Use user configured lut if there is one, otherwise use default */
12424 	if (vsi->rss_lut_user)
12425 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12426 	else
12427 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12428 
12429 	/* Use user configured hash key if there is one, otherwise
12430 	 * use default.
12431 	 */
12432 	if (vsi->rss_hkey_user)
12433 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12434 	else
12435 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12436 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12437 	kfree(lut);
12438 
12439 	return ret;
12440 }
12441 
12442 /**
12443  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12444  * @pf: board private structure
12445  * @queue_count: the requested queue count for rss.
12446  *
12447  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12448  * count which may be different from the requested queue count.
12449  * Note: expects to be called while under rtnl_lock()
12450  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)12451 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12452 {
12453 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12454 	int new_rss_size;
12455 
12456 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12457 		return 0;
12458 
12459 	queue_count = min_t(int, queue_count, num_online_cpus());
12460 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12461 
12462 	if (queue_count != vsi->num_queue_pairs) {
12463 		u16 qcount;
12464 
12465 		vsi->req_queue_pairs = queue_count;
12466 		i40e_prep_for_reset(pf);
12467 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12468 			return pf->alloc_rss_size;
12469 
12470 		pf->alloc_rss_size = new_rss_size;
12471 
12472 		i40e_reset_and_rebuild(pf, true, true);
12473 
12474 		/* Discard the user configured hash keys and lut, if less
12475 		 * queues are enabled.
12476 		 */
12477 		if (queue_count < vsi->rss_size) {
12478 			i40e_clear_rss_config_user(vsi);
12479 			dev_dbg(&pf->pdev->dev,
12480 				"discard user configured hash keys and lut\n");
12481 		}
12482 
12483 		/* Reset vsi->rss_size, as number of enabled queues changed */
12484 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12485 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12486 
12487 		i40e_pf_config_rss(pf);
12488 	}
12489 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12490 		 vsi->req_queue_pairs, pf->rss_size_max);
12491 	return pf->alloc_rss_size;
12492 }
12493 
12494 /**
12495  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12496  * @pf: board private structure
12497  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)12498 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12499 {
12500 	i40e_status status;
12501 	bool min_valid, max_valid;
12502 	u32 max_bw, min_bw;
12503 
12504 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12505 					   &min_valid, &max_valid);
12506 
12507 	if (!status) {
12508 		if (min_valid)
12509 			pf->min_bw = min_bw;
12510 		if (max_valid)
12511 			pf->max_bw = max_bw;
12512 	}
12513 
12514 	return status;
12515 }
12516 
12517 /**
12518  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12519  * @pf: board private structure
12520  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)12521 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12522 {
12523 	struct i40e_aqc_configure_partition_bw_data bw_data;
12524 	i40e_status status;
12525 
12526 	memset(&bw_data, 0, sizeof(bw_data));
12527 
12528 	/* Set the valid bit for this PF */
12529 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12530 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12531 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12532 
12533 	/* Set the new bandwidths */
12534 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12535 
12536 	return status;
12537 }
12538 
12539 /**
12540  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12541  * @pf: board private structure
12542  **/
i40e_commit_partition_bw_setting(struct i40e_pf * pf)12543 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12544 {
12545 	/* Commit temporary BW setting to permanent NVM image */
12546 	enum i40e_admin_queue_err last_aq_status;
12547 	i40e_status ret;
12548 	u16 nvm_word;
12549 
12550 	if (pf->hw.partition_id != 1) {
12551 		dev_info(&pf->pdev->dev,
12552 			 "Commit BW only works on partition 1! This is partition %d",
12553 			 pf->hw.partition_id);
12554 		ret = I40E_NOT_SUPPORTED;
12555 		goto bw_commit_out;
12556 	}
12557 
12558 	/* Acquire NVM for read access */
12559 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12560 	last_aq_status = pf->hw.aq.asq_last_status;
12561 	if (ret) {
12562 		dev_info(&pf->pdev->dev,
12563 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12564 			 i40e_stat_str(&pf->hw, ret),
12565 			 i40e_aq_str(&pf->hw, last_aq_status));
12566 		goto bw_commit_out;
12567 	}
12568 
12569 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12570 	ret = i40e_aq_read_nvm(&pf->hw,
12571 			       I40E_SR_NVM_CONTROL_WORD,
12572 			       0x10, sizeof(nvm_word), &nvm_word,
12573 			       false, NULL);
12574 	/* Save off last admin queue command status before releasing
12575 	 * the NVM
12576 	 */
12577 	last_aq_status = pf->hw.aq.asq_last_status;
12578 	i40e_release_nvm(&pf->hw);
12579 	if (ret) {
12580 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12581 			 i40e_stat_str(&pf->hw, ret),
12582 			 i40e_aq_str(&pf->hw, last_aq_status));
12583 		goto bw_commit_out;
12584 	}
12585 
12586 	/* Wait a bit for NVM release to complete */
12587 	msleep(50);
12588 
12589 	/* Acquire NVM for write access */
12590 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12591 	last_aq_status = pf->hw.aq.asq_last_status;
12592 	if (ret) {
12593 		dev_info(&pf->pdev->dev,
12594 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12595 			 i40e_stat_str(&pf->hw, ret),
12596 			 i40e_aq_str(&pf->hw, last_aq_status));
12597 		goto bw_commit_out;
12598 	}
12599 	/* Write it back out unchanged to initiate update NVM,
12600 	 * which will force a write of the shadow (alt) RAM to
12601 	 * the NVM - thus storing the bandwidth values permanently.
12602 	 */
12603 	ret = i40e_aq_update_nvm(&pf->hw,
12604 				 I40E_SR_NVM_CONTROL_WORD,
12605 				 0x10, sizeof(nvm_word),
12606 				 &nvm_word, true, 0, NULL);
12607 	/* Save off last admin queue command status before releasing
12608 	 * the NVM
12609 	 */
12610 	last_aq_status = pf->hw.aq.asq_last_status;
12611 	i40e_release_nvm(&pf->hw);
12612 	if (ret)
12613 		dev_info(&pf->pdev->dev,
12614 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12615 			 i40e_stat_str(&pf->hw, ret),
12616 			 i40e_aq_str(&pf->hw, last_aq_status));
12617 bw_commit_out:
12618 
12619 	return ret;
12620 }
12621 
12622 /**
12623  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12624  * if total port shutdown feature is enabled for this PF
12625  * @pf: board private structure
12626  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)12627 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12628 {
12629 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12630 #define I40E_FEATURES_ENABLE_PTR		0x2A
12631 #define I40E_CURRENT_SETTING_PTR		0x2B
12632 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12633 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12634 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12635 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12636 	i40e_status read_status = I40E_SUCCESS;
12637 	u16 sr_emp_sr_settings_ptr = 0;
12638 	u16 features_enable = 0;
12639 	u16 link_behavior = 0;
12640 	bool ret = false;
12641 
12642 	read_status = i40e_read_nvm_word(&pf->hw,
12643 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12644 					 &sr_emp_sr_settings_ptr);
12645 	if (read_status)
12646 		goto err_nvm;
12647 	read_status = i40e_read_nvm_word(&pf->hw,
12648 					 sr_emp_sr_settings_ptr +
12649 					 I40E_FEATURES_ENABLE_PTR,
12650 					 &features_enable);
12651 	if (read_status)
12652 		goto err_nvm;
12653 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12654 		read_status = i40e_read_nvm_module_data(&pf->hw,
12655 							I40E_SR_EMP_SR_SETTINGS_PTR,
12656 							I40E_CURRENT_SETTING_PTR,
12657 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12658 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12659 							&link_behavior);
12660 		if (read_status)
12661 			goto err_nvm;
12662 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12663 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12664 	}
12665 	return ret;
12666 
12667 err_nvm:
12668 	dev_warn(&pf->pdev->dev,
12669 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12670 		 i40e_stat_str(&pf->hw, read_status));
12671 	return ret;
12672 }
12673 
12674 /**
12675  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12676  * @pf: board private structure to initialize
12677  *
12678  * i40e_sw_init initializes the Adapter private data structure.
12679  * Fields are initialized based on PCI device information and
12680  * OS network device settings (MTU size).
12681  **/
i40e_sw_init(struct i40e_pf * pf)12682 static int i40e_sw_init(struct i40e_pf *pf)
12683 {
12684 	int err = 0;
12685 	int size;
12686 	u16 pow;
12687 
12688 	/* Set default capability flags */
12689 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12690 		    I40E_FLAG_MSI_ENABLED     |
12691 		    I40E_FLAG_MSIX_ENABLED;
12692 
12693 	/* Set default ITR */
12694 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12695 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12696 
12697 	/* Depending on PF configurations, it is possible that the RSS
12698 	 * maximum might end up larger than the available queues
12699 	 */
12700 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12701 	pf->alloc_rss_size = 1;
12702 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12703 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12704 				 pf->hw.func_caps.num_tx_qp);
12705 
12706 	/* find the next higher power-of-2 of num cpus */
12707 	pow = roundup_pow_of_two(num_online_cpus());
12708 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12709 
12710 	if (pf->hw.func_caps.rss) {
12711 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12712 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12713 					   num_online_cpus());
12714 	}
12715 
12716 	/* MFP mode enabled */
12717 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12718 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12719 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12720 		if (i40e_get_partition_bw_setting(pf)) {
12721 			dev_warn(&pf->pdev->dev,
12722 				 "Could not get partition bw settings\n");
12723 		} else {
12724 			dev_info(&pf->pdev->dev,
12725 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12726 				 pf->min_bw, pf->max_bw);
12727 
12728 			/* nudge the Tx scheduler */
12729 			i40e_set_partition_bw_setting(pf);
12730 		}
12731 	}
12732 
12733 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12734 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12735 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12736 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12737 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12738 		    pf->hw.num_partitions > 1)
12739 			dev_info(&pf->pdev->dev,
12740 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12741 		else
12742 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12743 		pf->fdir_pf_filter_count =
12744 				 pf->hw.func_caps.fd_filters_guaranteed;
12745 		pf->hw.fdir_shared_filter_count =
12746 				 pf->hw.func_caps.fd_filters_best_effort;
12747 	}
12748 
12749 	if (pf->hw.mac.type == I40E_MAC_X722) {
12750 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12751 				    I40E_HW_128_QP_RSS_CAPABLE |
12752 				    I40E_HW_ATR_EVICT_CAPABLE |
12753 				    I40E_HW_WB_ON_ITR_CAPABLE |
12754 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12755 				    I40E_HW_NO_PCI_LINK_CHECK |
12756 				    I40E_HW_USE_SET_LLDP_MIB |
12757 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12758 				    I40E_HW_PTP_L4_CAPABLE |
12759 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12760 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12761 
12762 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12763 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12764 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12765 			dev_warn(&pf->pdev->dev,
12766 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12767 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12768 		}
12769 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12770 		   ((pf->hw.aq.api_maj_ver == 1) &&
12771 		    (pf->hw.aq.api_min_ver > 4))) {
12772 		/* Supported in FW API version higher than 1.4 */
12773 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12774 	}
12775 
12776 	/* Enable HW ATR eviction if possible */
12777 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12778 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12779 
12780 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12781 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12782 	    (pf->hw.aq.fw_maj_ver < 4))) {
12783 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12784 		/* No DCB support  for FW < v4.33 */
12785 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12786 	}
12787 
12788 	/* Disable FW LLDP if FW < v4.3 */
12789 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12790 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12791 	    (pf->hw.aq.fw_maj_ver < 4)))
12792 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12793 
12794 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12795 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12796 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12797 	    (pf->hw.aq.fw_maj_ver >= 5)))
12798 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12799 
12800 	/* Enable PTP L4 if FW > v6.0 */
12801 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12802 	    pf->hw.aq.fw_maj_ver >= 6)
12803 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12804 
12805 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12806 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12807 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12808 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12809 	}
12810 
12811 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12812 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12813 		/* IWARP needs one extra vector for CQP just like MISC.*/
12814 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12815 	}
12816 	/* Stopping FW LLDP engine is supported on XL710 and X722
12817 	 * starting from FW versions determined in i40e_init_adminq.
12818 	 * Stopping the FW LLDP engine is not supported on XL710
12819 	 * if NPAR is functioning so unset this hw flag in this case.
12820 	 */
12821 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12822 	    pf->hw.func_caps.npar_enable &&
12823 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12824 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12825 
12826 #ifdef CONFIG_PCI_IOV
12827 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12828 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12829 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12830 		pf->num_req_vfs = min_t(int,
12831 					pf->hw.func_caps.num_vfs,
12832 					I40E_MAX_VF_COUNT);
12833 	}
12834 #endif /* CONFIG_PCI_IOV */
12835 	pf->eeprom_version = 0xDEAD;
12836 	pf->lan_veb = I40E_NO_VEB;
12837 	pf->lan_vsi = I40E_NO_VSI;
12838 
12839 	/* By default FW has this off for performance reasons */
12840 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12841 
12842 	/* set up queue assignment tracking */
12843 	size = sizeof(struct i40e_lump_tracking)
12844 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12845 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12846 	if (!pf->qp_pile) {
12847 		err = -ENOMEM;
12848 		goto sw_init_done;
12849 	}
12850 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12851 
12852 	pf->tx_timeout_recovery_level = 1;
12853 
12854 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12855 	    i40e_is_total_port_shutdown_enabled(pf)) {
12856 		/* Link down on close must be on when total port shutdown
12857 		 * is enabled for a given port
12858 		 */
12859 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12860 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12861 		dev_info(&pf->pdev->dev,
12862 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12863 	}
12864 	mutex_init(&pf->switch_mutex);
12865 
12866 sw_init_done:
12867 	return err;
12868 }
12869 
12870 /**
12871  * i40e_set_ntuple - set the ntuple feature flag and take action
12872  * @pf: board private structure to initialize
12873  * @features: the feature set that the stack is suggesting
12874  *
12875  * returns a bool to indicate if reset needs to happen
12876  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12877 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12878 {
12879 	bool need_reset = false;
12880 
12881 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12882 	 * the state changed, we need to reset.
12883 	 */
12884 	if (features & NETIF_F_NTUPLE) {
12885 		/* Enable filters and mark for reset */
12886 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12887 			need_reset = true;
12888 		/* enable FD_SB only if there is MSI-X vector and no cloud
12889 		 * filters exist
12890 		 */
12891 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12892 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12893 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12894 		}
12895 	} else {
12896 		/* turn off filters, mark for reset and clear SW filter list */
12897 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12898 			need_reset = true;
12899 			i40e_fdir_filter_exit(pf);
12900 		}
12901 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12902 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12903 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12904 
12905 		/* reset fd counters */
12906 		pf->fd_add_err = 0;
12907 		pf->fd_atr_cnt = 0;
12908 		/* if ATR was auto disabled it can be re-enabled. */
12909 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12910 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12911 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12912 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12913 	}
12914 	return need_reset;
12915 }
12916 
12917 /**
12918  * i40e_clear_rss_lut - clear the rx hash lookup table
12919  * @vsi: the VSI being configured
12920  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12921 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12922 {
12923 	struct i40e_pf *pf = vsi->back;
12924 	struct i40e_hw *hw = &pf->hw;
12925 	u16 vf_id = vsi->vf_id;
12926 	u8 i;
12927 
12928 	if (vsi->type == I40E_VSI_MAIN) {
12929 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12930 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12931 	} else if (vsi->type == I40E_VSI_SRIOV) {
12932 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12933 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12934 	} else {
12935 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12936 	}
12937 }
12938 
12939 /**
12940  * i40e_set_features - set the netdev feature flags
12941  * @netdev: ptr to the netdev being adjusted
12942  * @features: the feature set that the stack is suggesting
12943  * Note: expects to be called while under rtnl_lock()
12944  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)12945 static int i40e_set_features(struct net_device *netdev,
12946 			     netdev_features_t features)
12947 {
12948 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12949 	struct i40e_vsi *vsi = np->vsi;
12950 	struct i40e_pf *pf = vsi->back;
12951 	bool need_reset;
12952 
12953 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12954 		i40e_pf_config_rss(pf);
12955 	else if (!(features & NETIF_F_RXHASH) &&
12956 		 netdev->features & NETIF_F_RXHASH)
12957 		i40e_clear_rss_lut(vsi);
12958 
12959 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12960 		i40e_vlan_stripping_enable(vsi);
12961 	else
12962 		i40e_vlan_stripping_disable(vsi);
12963 
12964 	if (!(features & NETIF_F_HW_TC) &&
12965 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12966 		dev_err(&pf->pdev->dev,
12967 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12968 		return -EINVAL;
12969 	}
12970 
12971 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12972 		i40e_del_all_macvlans(vsi);
12973 
12974 	need_reset = i40e_set_ntuple(pf, features);
12975 
12976 	if (need_reset)
12977 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12978 
12979 	return 0;
12980 }
12981 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12982 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12983 				    unsigned int table, unsigned int idx,
12984 				    struct udp_tunnel_info *ti)
12985 {
12986 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12987 	struct i40e_hw *hw = &np->vsi->back->hw;
12988 	u8 type, filter_index;
12989 	i40e_status ret;
12990 
12991 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12992 						   I40E_AQC_TUNNEL_TYPE_NGE;
12993 
12994 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12995 				     NULL);
12996 	if (ret) {
12997 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12998 			    i40e_stat_str(hw, ret),
12999 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13000 		return -EIO;
13001 	}
13002 
13003 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13004 	return 0;
13005 }
13006 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)13007 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13008 				      unsigned int table, unsigned int idx,
13009 				      struct udp_tunnel_info *ti)
13010 {
13011 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13012 	struct i40e_hw *hw = &np->vsi->back->hw;
13013 	i40e_status ret;
13014 
13015 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13016 	if (ret) {
13017 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
13018 			    i40e_stat_str(hw, ret),
13019 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13020 		return -EIO;
13021 	}
13022 
13023 	return 0;
13024 }
13025 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)13026 static int i40e_get_phys_port_id(struct net_device *netdev,
13027 				 struct netdev_phys_item_id *ppid)
13028 {
13029 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13030 	struct i40e_pf *pf = np->vsi->back;
13031 	struct i40e_hw *hw = &pf->hw;
13032 
13033 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13034 		return -EOPNOTSUPP;
13035 
13036 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13037 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13038 
13039 	return 0;
13040 }
13041 
13042 /**
13043  * i40e_ndo_fdb_add - add an entry to the hardware database
13044  * @ndm: the input from the stack
13045  * @tb: pointer to array of nladdr (unused)
13046  * @dev: the net device pointer
13047  * @addr: the MAC address entry being added
13048  * @vid: VLAN ID
13049  * @flags: instructions from stack about fdb operation
13050  * @extack: netlink extended ack, unused currently
13051  */
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)13052 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13053 			    struct net_device *dev,
13054 			    const unsigned char *addr, u16 vid,
13055 			    u16 flags,
13056 			    struct netlink_ext_ack *extack)
13057 {
13058 	struct i40e_netdev_priv *np = netdev_priv(dev);
13059 	struct i40e_pf *pf = np->vsi->back;
13060 	int err = 0;
13061 
13062 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13063 		return -EOPNOTSUPP;
13064 
13065 	if (vid) {
13066 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13067 		return -EINVAL;
13068 	}
13069 
13070 	/* Hardware does not support aging addresses so if a
13071 	 * ndm_state is given only allow permanent addresses
13072 	 */
13073 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13074 		netdev_info(dev, "FDB only supports static addresses\n");
13075 		return -EINVAL;
13076 	}
13077 
13078 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13079 		err = dev_uc_add_excl(dev, addr);
13080 	else if (is_multicast_ether_addr(addr))
13081 		err = dev_mc_add_excl(dev, addr);
13082 	else
13083 		err = -EINVAL;
13084 
13085 	/* Only return duplicate errors if NLM_F_EXCL is set */
13086 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13087 		err = 0;
13088 
13089 	return err;
13090 }
13091 
13092 /**
13093  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13094  * @dev: the netdev being configured
13095  * @nlh: RTNL message
13096  * @flags: bridge flags
13097  * @extack: netlink extended ack
13098  *
13099  * Inserts a new hardware bridge if not already created and
13100  * enables the bridging mode requested (VEB or VEPA). If the
13101  * hardware bridge has already been inserted and the request
13102  * is to change the mode then that requires a PF reset to
13103  * allow rebuild of the components with required hardware
13104  * bridge mode enabled.
13105  *
13106  * Note: expects to be called while under rtnl_lock()
13107  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)13108 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13109 				   struct nlmsghdr *nlh,
13110 				   u16 flags,
13111 				   struct netlink_ext_ack *extack)
13112 {
13113 	struct i40e_netdev_priv *np = netdev_priv(dev);
13114 	struct i40e_vsi *vsi = np->vsi;
13115 	struct i40e_pf *pf = vsi->back;
13116 	struct i40e_veb *veb = NULL;
13117 	struct nlattr *attr, *br_spec;
13118 	int i, rem;
13119 
13120 	/* Only for PF VSI for now */
13121 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13122 		return -EOPNOTSUPP;
13123 
13124 	/* Find the HW bridge for PF VSI */
13125 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13126 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13127 			veb = pf->veb[i];
13128 	}
13129 
13130 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13131 
13132 	nla_for_each_nested(attr, br_spec, rem) {
13133 		__u16 mode;
13134 
13135 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13136 			continue;
13137 
13138 		mode = nla_get_u16(attr);
13139 		if ((mode != BRIDGE_MODE_VEPA) &&
13140 		    (mode != BRIDGE_MODE_VEB))
13141 			return -EINVAL;
13142 
13143 		/* Insert a new HW bridge */
13144 		if (!veb) {
13145 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13146 					     vsi->tc_config.enabled_tc);
13147 			if (veb) {
13148 				veb->bridge_mode = mode;
13149 				i40e_config_bridge_mode(veb);
13150 			} else {
13151 				/* No Bridge HW offload available */
13152 				return -ENOENT;
13153 			}
13154 			break;
13155 		} else if (mode != veb->bridge_mode) {
13156 			/* Existing HW bridge but different mode needs reset */
13157 			veb->bridge_mode = mode;
13158 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13159 			if (mode == BRIDGE_MODE_VEB)
13160 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13161 			else
13162 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13163 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13164 			break;
13165 		}
13166 	}
13167 
13168 	return 0;
13169 }
13170 
13171 /**
13172  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13173  * @skb: skb buff
13174  * @pid: process id
13175  * @seq: RTNL message seq #
13176  * @dev: the netdev being configured
13177  * @filter_mask: unused
13178  * @nlflags: netlink flags passed in
13179  *
13180  * Return the mode in which the hardware bridge is operating in
13181  * i.e VEB or VEPA.
13182  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)13183 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13184 				   struct net_device *dev,
13185 				   u32 __always_unused filter_mask,
13186 				   int nlflags)
13187 {
13188 	struct i40e_netdev_priv *np = netdev_priv(dev);
13189 	struct i40e_vsi *vsi = np->vsi;
13190 	struct i40e_pf *pf = vsi->back;
13191 	struct i40e_veb *veb = NULL;
13192 	int i;
13193 
13194 	/* Only for PF VSI for now */
13195 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13196 		return -EOPNOTSUPP;
13197 
13198 	/* Find the HW bridge for the PF VSI */
13199 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13200 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13201 			veb = pf->veb[i];
13202 	}
13203 
13204 	if (!veb)
13205 		return 0;
13206 
13207 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13208 				       0, 0, nlflags, filter_mask, NULL);
13209 }
13210 
13211 /**
13212  * i40e_features_check - Validate encapsulated packet conforms to limits
13213  * @skb: skb buff
13214  * @dev: This physical port's netdev
13215  * @features: Offload features that the stack believes apply
13216  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13217 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13218 					     struct net_device *dev,
13219 					     netdev_features_t features)
13220 {
13221 	size_t len;
13222 
13223 	/* No point in doing any of this if neither checksum nor GSO are
13224 	 * being requested for this frame.  We can rule out both by just
13225 	 * checking for CHECKSUM_PARTIAL
13226 	 */
13227 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13228 		return features;
13229 
13230 	/* We cannot support GSO if the MSS is going to be less than
13231 	 * 64 bytes.  If it is then we need to drop support for GSO.
13232 	 */
13233 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13234 		features &= ~NETIF_F_GSO_MASK;
13235 
13236 	/* MACLEN can support at most 63 words */
13237 	len = skb_network_header(skb) - skb->data;
13238 	if (len & ~(63 * 2))
13239 		goto out_err;
13240 
13241 	/* IPLEN and EIPLEN can support at most 127 dwords */
13242 	len = skb_transport_header(skb) - skb_network_header(skb);
13243 	if (len & ~(127 * 4))
13244 		goto out_err;
13245 
13246 	if (skb->encapsulation) {
13247 		/* L4TUNLEN can support 127 words */
13248 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13249 		if (len & ~(127 * 2))
13250 			goto out_err;
13251 
13252 		/* IPLEN can support at most 127 dwords */
13253 		len = skb_inner_transport_header(skb) -
13254 		      skb_inner_network_header(skb);
13255 		if (len & ~(127 * 4))
13256 			goto out_err;
13257 	}
13258 
13259 	/* No need to validate L4LEN as TCP is the only protocol with a
13260 	 * flexible value and we support all possible values supported
13261 	 * by TCP, which is at most 15 dwords
13262 	 */
13263 
13264 	return features;
13265 out_err:
13266 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13267 }
13268 
13269 /**
13270  * i40e_xdp_setup - add/remove an XDP program
13271  * @vsi: VSI to changed
13272  * @prog: XDP program
13273  * @extack: netlink extended ack
13274  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)13275 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13276 			  struct netlink_ext_ack *extack)
13277 {
13278 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13279 	struct i40e_pf *pf = vsi->back;
13280 	struct bpf_prog *old_prog;
13281 	bool need_reset;
13282 	int i;
13283 
13284 	/* Don't allow frames that span over multiple buffers */
13285 	if (frame_size > vsi->rx_buf_len) {
13286 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13287 		return -EINVAL;
13288 	}
13289 
13290 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13291 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13292 
13293 	if (need_reset)
13294 		i40e_prep_for_reset(pf);
13295 
13296 	/* VSI shall be deleted in a moment, just return EINVAL */
13297 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13298 		return -EINVAL;
13299 
13300 	old_prog = xchg(&vsi->xdp_prog, prog);
13301 
13302 	if (need_reset) {
13303 		if (!prog)
13304 			/* Wait until ndo_xsk_wakeup completes. */
13305 			synchronize_rcu();
13306 		i40e_reset_and_rebuild(pf, true, true);
13307 	}
13308 
13309 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13310 		if (i40e_realloc_rx_bi_zc(vsi, true))
13311 			return -ENOMEM;
13312 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13313 		if (i40e_realloc_rx_bi_zc(vsi, false))
13314 			return -ENOMEM;
13315 	}
13316 
13317 	for (i = 0; i < vsi->num_queue_pairs; i++)
13318 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13319 
13320 	if (old_prog)
13321 		bpf_prog_put(old_prog);
13322 
13323 	/* Kick start the NAPI context if there is an AF_XDP socket open
13324 	 * on that queue id. This so that receiving will start.
13325 	 */
13326 	if (need_reset && prog)
13327 		for (i = 0; i < vsi->num_queue_pairs; i++)
13328 			if (vsi->xdp_rings[i]->xsk_pool)
13329 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13330 						      XDP_WAKEUP_RX);
13331 
13332 	return 0;
13333 }
13334 
13335 /**
13336  * i40e_enter_busy_conf - Enters busy config state
13337  * @vsi: vsi
13338  *
13339  * Returns 0 on success, <0 for failure.
13340  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)13341 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13342 {
13343 	struct i40e_pf *pf = vsi->back;
13344 	int timeout = 50;
13345 
13346 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13347 		timeout--;
13348 		if (!timeout)
13349 			return -EBUSY;
13350 		usleep_range(1000, 2000);
13351 	}
13352 
13353 	return 0;
13354 }
13355 
13356 /**
13357  * i40e_exit_busy_conf - Exits busy config state
13358  * @vsi: vsi
13359  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)13360 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13361 {
13362 	struct i40e_pf *pf = vsi->back;
13363 
13364 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13365 }
13366 
13367 /**
13368  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13369  * @vsi: vsi
13370  * @queue_pair: queue pair
13371  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)13372 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13373 {
13374 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13375 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13376 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13377 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13378 	if (i40e_enabled_xdp_vsi(vsi)) {
13379 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13380 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13381 	}
13382 }
13383 
13384 /**
13385  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13386  * @vsi: vsi
13387  * @queue_pair: queue pair
13388  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)13389 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13390 {
13391 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13392 	if (i40e_enabled_xdp_vsi(vsi)) {
13393 		/* Make sure that in-progress ndo_xdp_xmit calls are
13394 		 * completed.
13395 		 */
13396 		synchronize_rcu();
13397 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13398 	}
13399 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13400 }
13401 
13402 /**
13403  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13404  * @vsi: vsi
13405  * @queue_pair: queue pair
13406  * @enable: true for enable, false for disable
13407  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)13408 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13409 					bool enable)
13410 {
13411 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13412 	struct i40e_q_vector *q_vector = rxr->q_vector;
13413 
13414 	if (!vsi->netdev)
13415 		return;
13416 
13417 	/* All rings in a qp belong to the same qvector. */
13418 	if (q_vector->rx.ring || q_vector->tx.ring) {
13419 		if (enable)
13420 			napi_enable(&q_vector->napi);
13421 		else
13422 			napi_disable(&q_vector->napi);
13423 	}
13424 }
13425 
13426 /**
13427  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13428  * @vsi: vsi
13429  * @queue_pair: queue pair
13430  * @enable: true for enable, false for disable
13431  *
13432  * Returns 0 on success, <0 on failure.
13433  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)13434 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13435 					bool enable)
13436 {
13437 	struct i40e_pf *pf = vsi->back;
13438 	int pf_q, ret = 0;
13439 
13440 	pf_q = vsi->base_queue + queue_pair;
13441 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13442 				     false /*is xdp*/, enable);
13443 	if (ret) {
13444 		dev_info(&pf->pdev->dev,
13445 			 "VSI seid %d Tx ring %d %sable timeout\n",
13446 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13447 		return ret;
13448 	}
13449 
13450 	i40e_control_rx_q(pf, pf_q, enable);
13451 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13452 	if (ret) {
13453 		dev_info(&pf->pdev->dev,
13454 			 "VSI seid %d Rx ring %d %sable timeout\n",
13455 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13456 		return ret;
13457 	}
13458 
13459 	/* Due to HW errata, on Rx disable only, the register can
13460 	 * indicate done before it really is. Needs 50ms to be sure
13461 	 */
13462 	if (!enable)
13463 		mdelay(50);
13464 
13465 	if (!i40e_enabled_xdp_vsi(vsi))
13466 		return ret;
13467 
13468 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13469 				     pf_q + vsi->alloc_queue_pairs,
13470 				     true /*is xdp*/, enable);
13471 	if (ret) {
13472 		dev_info(&pf->pdev->dev,
13473 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13474 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13475 	}
13476 
13477 	return ret;
13478 }
13479 
13480 /**
13481  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13482  * @vsi: vsi
13483  * @queue_pair: queue_pair
13484  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)13485 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13486 {
13487 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13488 	struct i40e_pf *pf = vsi->back;
13489 	struct i40e_hw *hw = &pf->hw;
13490 
13491 	/* All rings in a qp belong to the same qvector. */
13492 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13493 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13494 	else
13495 		i40e_irq_dynamic_enable_icr0(pf);
13496 
13497 	i40e_flush(hw);
13498 }
13499 
13500 /**
13501  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13502  * @vsi: vsi
13503  * @queue_pair: queue_pair
13504  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)13505 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13506 {
13507 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13508 	struct i40e_pf *pf = vsi->back;
13509 	struct i40e_hw *hw = &pf->hw;
13510 
13511 	/* For simplicity, instead of removing the qp interrupt causes
13512 	 * from the interrupt linked list, we simply disable the interrupt, and
13513 	 * leave the list intact.
13514 	 *
13515 	 * All rings in a qp belong to the same qvector.
13516 	 */
13517 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13518 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13519 
13520 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13521 		i40e_flush(hw);
13522 		synchronize_irq(pf->msix_entries[intpf].vector);
13523 	} else {
13524 		/* Legacy and MSI mode - this stops all interrupt handling */
13525 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13526 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13527 		i40e_flush(hw);
13528 		synchronize_irq(pf->pdev->irq);
13529 	}
13530 }
13531 
13532 /**
13533  * i40e_queue_pair_disable - Disables a queue pair
13534  * @vsi: vsi
13535  * @queue_pair: queue pair
13536  *
13537  * Returns 0 on success, <0 on failure.
13538  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)13539 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13540 {
13541 	int err;
13542 
13543 	err = i40e_enter_busy_conf(vsi);
13544 	if (err)
13545 		return err;
13546 
13547 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13548 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13549 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13550 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13551 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13552 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13553 
13554 	return err;
13555 }
13556 
13557 /**
13558  * i40e_queue_pair_enable - Enables a queue pair
13559  * @vsi: vsi
13560  * @queue_pair: queue pair
13561  *
13562  * Returns 0 on success, <0 on failure.
13563  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)13564 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13565 {
13566 	int err;
13567 
13568 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13569 	if (err)
13570 		return err;
13571 
13572 	if (i40e_enabled_xdp_vsi(vsi)) {
13573 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13574 		if (err)
13575 			return err;
13576 	}
13577 
13578 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13579 	if (err)
13580 		return err;
13581 
13582 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13583 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13584 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13585 
13586 	i40e_exit_busy_conf(vsi);
13587 
13588 	return err;
13589 }
13590 
13591 /**
13592  * i40e_xdp - implements ndo_bpf for i40e
13593  * @dev: netdevice
13594  * @xdp: XDP command
13595  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)13596 static int i40e_xdp(struct net_device *dev,
13597 		    struct netdev_bpf *xdp)
13598 {
13599 	struct i40e_netdev_priv *np = netdev_priv(dev);
13600 	struct i40e_vsi *vsi = np->vsi;
13601 
13602 	if (vsi->type != I40E_VSI_MAIN)
13603 		return -EINVAL;
13604 
13605 	switch (xdp->command) {
13606 	case XDP_SETUP_PROG:
13607 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13608 	case XDP_SETUP_XSK_POOL:
13609 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13610 					   xdp->xsk.queue_id);
13611 	default:
13612 		return -EINVAL;
13613 	}
13614 }
13615 
13616 static const struct net_device_ops i40e_netdev_ops = {
13617 	.ndo_open		= i40e_open,
13618 	.ndo_stop		= i40e_close,
13619 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13620 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13621 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13622 	.ndo_validate_addr	= eth_validate_addr,
13623 	.ndo_set_mac_address	= i40e_set_mac,
13624 	.ndo_change_mtu		= i40e_change_mtu,
13625 	.ndo_eth_ioctl		= i40e_ioctl,
13626 	.ndo_tx_timeout		= i40e_tx_timeout,
13627 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13628 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13629 #ifdef CONFIG_NET_POLL_CONTROLLER
13630 	.ndo_poll_controller	= i40e_netpoll,
13631 #endif
13632 	.ndo_setup_tc		= __i40e_setup_tc,
13633 	.ndo_select_queue	= i40e_lan_select_queue,
13634 	.ndo_set_features	= i40e_set_features,
13635 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13636 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13637 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13638 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13639 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13640 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13641 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13642 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13643 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13644 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13645 	.ndo_features_check	= i40e_features_check,
13646 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13647 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13648 	.ndo_bpf		= i40e_xdp,
13649 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13650 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13651 	.ndo_dfwd_add_station	= i40e_fwd_add,
13652 	.ndo_dfwd_del_station	= i40e_fwd_del,
13653 };
13654 
13655 /**
13656  * i40e_config_netdev - Setup the netdev flags
13657  * @vsi: the VSI being configured
13658  *
13659  * Returns 0 on success, negative value on failure
13660  **/
i40e_config_netdev(struct i40e_vsi * vsi)13661 static int i40e_config_netdev(struct i40e_vsi *vsi)
13662 {
13663 	struct i40e_pf *pf = vsi->back;
13664 	struct i40e_hw *hw = &pf->hw;
13665 	struct i40e_netdev_priv *np;
13666 	struct net_device *netdev;
13667 	u8 broadcast[ETH_ALEN];
13668 	u8 mac_addr[ETH_ALEN];
13669 	int etherdev_size;
13670 	netdev_features_t hw_enc_features;
13671 	netdev_features_t hw_features;
13672 
13673 	etherdev_size = sizeof(struct i40e_netdev_priv);
13674 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13675 	if (!netdev)
13676 		return -ENOMEM;
13677 
13678 	vsi->netdev = netdev;
13679 	np = netdev_priv(netdev);
13680 	np->vsi = vsi;
13681 
13682 	hw_enc_features = NETIF_F_SG			|
13683 			  NETIF_F_HW_CSUM		|
13684 			  NETIF_F_HIGHDMA		|
13685 			  NETIF_F_SOFT_FEATURES		|
13686 			  NETIF_F_TSO			|
13687 			  NETIF_F_TSO_ECN		|
13688 			  NETIF_F_TSO6			|
13689 			  NETIF_F_GSO_GRE		|
13690 			  NETIF_F_GSO_GRE_CSUM		|
13691 			  NETIF_F_GSO_PARTIAL		|
13692 			  NETIF_F_GSO_IPXIP4		|
13693 			  NETIF_F_GSO_IPXIP6		|
13694 			  NETIF_F_GSO_UDP_TUNNEL	|
13695 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13696 			  NETIF_F_GSO_UDP_L4		|
13697 			  NETIF_F_SCTP_CRC		|
13698 			  NETIF_F_RXHASH		|
13699 			  NETIF_F_RXCSUM		|
13700 			  0;
13701 
13702 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13703 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13704 
13705 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13706 
13707 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13708 
13709 	netdev->hw_enc_features |= hw_enc_features;
13710 
13711 	/* record features VLANs can make use of */
13712 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13713 
13714 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13715 				   NETIF_F_GSO_GRE_CSUM |	\
13716 				   NETIF_F_GSO_IPXIP4 |		\
13717 				   NETIF_F_GSO_IPXIP6 |		\
13718 				   NETIF_F_GSO_UDP_TUNNEL |	\
13719 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13720 
13721 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13722 	netdev->features |= NETIF_F_GSO_PARTIAL |
13723 			    I40E_GSO_PARTIAL_FEATURES;
13724 
13725 	netdev->mpls_features |= NETIF_F_SG;
13726 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13727 	netdev->mpls_features |= NETIF_F_TSO;
13728 	netdev->mpls_features |= NETIF_F_TSO6;
13729 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13730 
13731 	/* enable macvlan offloads */
13732 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13733 
13734 	hw_features = hw_enc_features		|
13735 		      NETIF_F_HW_VLAN_CTAG_TX	|
13736 		      NETIF_F_HW_VLAN_CTAG_RX;
13737 
13738 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13739 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13740 
13741 	netdev->hw_features |= hw_features;
13742 
13743 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13744 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13745 
13746 	netdev->features &= ~NETIF_F_HW_TC;
13747 
13748 	if (vsi->type == I40E_VSI_MAIN) {
13749 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13750 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13751 		/* The following steps are necessary for two reasons. First,
13752 		 * some older NVM configurations load a default MAC-VLAN
13753 		 * filter that will accept any tagged packet, and we want to
13754 		 * replace this with a normal filter. Additionally, it is
13755 		 * possible our MAC address was provided by the platform using
13756 		 * Open Firmware or similar.
13757 		 *
13758 		 * Thus, we need to remove the default filter and install one
13759 		 * specific to the MAC address.
13760 		 */
13761 		i40e_rm_default_mac_filter(vsi, mac_addr);
13762 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13763 		i40e_add_mac_filter(vsi, mac_addr);
13764 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13765 	} else {
13766 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13767 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13768 		 * the end, which is 4 bytes long, so force truncation of the
13769 		 * original name by IFNAMSIZ - 4
13770 		 */
13771 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13772 			 IFNAMSIZ - 4,
13773 			 pf->vsi[pf->lan_vsi]->netdev->name);
13774 		eth_random_addr(mac_addr);
13775 
13776 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13777 		i40e_add_mac_filter(vsi, mac_addr);
13778 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13779 	}
13780 
13781 	/* Add the broadcast filter so that we initially will receive
13782 	 * broadcast packets. Note that when a new VLAN is first added the
13783 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13784 	 * specific filters as part of transitioning into "vlan" operation.
13785 	 * When more VLANs are added, the driver will copy each existing MAC
13786 	 * filter and add it for the new VLAN.
13787 	 *
13788 	 * Broadcast filters are handled specially by
13789 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13790 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13791 	 * filter. The subtask will update the correct broadcast promiscuous
13792 	 * bits as VLANs become active or inactive.
13793 	 */
13794 	eth_broadcast_addr(broadcast);
13795 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13796 	i40e_add_mac_filter(vsi, broadcast);
13797 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13798 
13799 	eth_hw_addr_set(netdev, mac_addr);
13800 	ether_addr_copy(netdev->perm_addr, mac_addr);
13801 
13802 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13803 	netdev->neigh_priv_len = sizeof(u32) * 4;
13804 
13805 	netdev->priv_flags |= IFF_UNICAST_FLT;
13806 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13807 	/* Setup netdev TC information */
13808 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13809 
13810 	netdev->netdev_ops = &i40e_netdev_ops;
13811 	netdev->watchdog_timeo = 5 * HZ;
13812 	i40e_set_ethtool_ops(netdev);
13813 
13814 	/* MTU range: 68 - 9706 */
13815 	netdev->min_mtu = ETH_MIN_MTU;
13816 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13817 
13818 	return 0;
13819 }
13820 
13821 /**
13822  * i40e_vsi_delete - Delete a VSI from the switch
13823  * @vsi: the VSI being removed
13824  *
13825  * Returns 0 on success, negative value on failure
13826  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13827 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13828 {
13829 	/* remove default VSI is not allowed */
13830 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13831 		return;
13832 
13833 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13834 }
13835 
13836 /**
13837  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13838  * @vsi: the VSI being queried
13839  *
13840  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13841  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13842 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13843 {
13844 	struct i40e_veb *veb;
13845 	struct i40e_pf *pf = vsi->back;
13846 
13847 	/* Uplink is not a bridge so default to VEB */
13848 	if (vsi->veb_idx >= I40E_MAX_VEB)
13849 		return 1;
13850 
13851 	veb = pf->veb[vsi->veb_idx];
13852 	if (!veb) {
13853 		dev_info(&pf->pdev->dev,
13854 			 "There is no veb associated with the bridge\n");
13855 		return -ENOENT;
13856 	}
13857 
13858 	/* Uplink is a bridge in VEPA mode */
13859 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13860 		return 0;
13861 	} else {
13862 		/* Uplink is a bridge in VEB mode */
13863 		return 1;
13864 	}
13865 
13866 	/* VEPA is now default bridge, so return 0 */
13867 	return 0;
13868 }
13869 
13870 /**
13871  * i40e_add_vsi - Add a VSI to the switch
13872  * @vsi: the VSI being configured
13873  *
13874  * This initializes a VSI context depending on the VSI type to be added and
13875  * passes it down to the add_vsi aq command.
13876  **/
i40e_add_vsi(struct i40e_vsi * vsi)13877 static int i40e_add_vsi(struct i40e_vsi *vsi)
13878 {
13879 	int ret = -ENODEV;
13880 	struct i40e_pf *pf = vsi->back;
13881 	struct i40e_hw *hw = &pf->hw;
13882 	struct i40e_vsi_context ctxt;
13883 	struct i40e_mac_filter *f;
13884 	struct hlist_node *h;
13885 	int bkt;
13886 
13887 	u8 enabled_tc = 0x1; /* TC0 enabled */
13888 	int f_count = 0;
13889 
13890 	memset(&ctxt, 0, sizeof(ctxt));
13891 	switch (vsi->type) {
13892 	case I40E_VSI_MAIN:
13893 		/* The PF's main VSI is already setup as part of the
13894 		 * device initialization, so we'll not bother with
13895 		 * the add_vsi call, but we will retrieve the current
13896 		 * VSI context.
13897 		 */
13898 		ctxt.seid = pf->main_vsi_seid;
13899 		ctxt.pf_num = pf->hw.pf_id;
13900 		ctxt.vf_num = 0;
13901 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13902 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13903 		if (ret) {
13904 			dev_info(&pf->pdev->dev,
13905 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13906 				 i40e_stat_str(&pf->hw, ret),
13907 				 i40e_aq_str(&pf->hw,
13908 					     pf->hw.aq.asq_last_status));
13909 			return -ENOENT;
13910 		}
13911 		vsi->info = ctxt.info;
13912 		vsi->info.valid_sections = 0;
13913 
13914 		vsi->seid = ctxt.seid;
13915 		vsi->id = ctxt.vsi_number;
13916 
13917 		enabled_tc = i40e_pf_get_tc_map(pf);
13918 
13919 		/* Source pruning is enabled by default, so the flag is
13920 		 * negative logic - if it's set, we need to fiddle with
13921 		 * the VSI to disable source pruning.
13922 		 */
13923 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13924 			memset(&ctxt, 0, sizeof(ctxt));
13925 			ctxt.seid = pf->main_vsi_seid;
13926 			ctxt.pf_num = pf->hw.pf_id;
13927 			ctxt.vf_num = 0;
13928 			ctxt.info.valid_sections |=
13929 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13930 			ctxt.info.switch_id =
13931 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13932 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13933 			if (ret) {
13934 				dev_info(&pf->pdev->dev,
13935 					 "update vsi failed, err %s aq_err %s\n",
13936 					 i40e_stat_str(&pf->hw, ret),
13937 					 i40e_aq_str(&pf->hw,
13938 						     pf->hw.aq.asq_last_status));
13939 				ret = -ENOENT;
13940 				goto err;
13941 			}
13942 		}
13943 
13944 		/* MFP mode setup queue map and update VSI */
13945 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13946 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13947 			memset(&ctxt, 0, sizeof(ctxt));
13948 			ctxt.seid = pf->main_vsi_seid;
13949 			ctxt.pf_num = pf->hw.pf_id;
13950 			ctxt.vf_num = 0;
13951 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13952 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13953 			if (ret) {
13954 				dev_info(&pf->pdev->dev,
13955 					 "update vsi failed, err %s aq_err %s\n",
13956 					 i40e_stat_str(&pf->hw, ret),
13957 					 i40e_aq_str(&pf->hw,
13958 						    pf->hw.aq.asq_last_status));
13959 				ret = -ENOENT;
13960 				goto err;
13961 			}
13962 			/* update the local VSI info queue map */
13963 			i40e_vsi_update_queue_map(vsi, &ctxt);
13964 			vsi->info.valid_sections = 0;
13965 		} else {
13966 			/* Default/Main VSI is only enabled for TC0
13967 			 * reconfigure it to enable all TCs that are
13968 			 * available on the port in SFP mode.
13969 			 * For MFP case the iSCSI PF would use this
13970 			 * flow to enable LAN+iSCSI TC.
13971 			 */
13972 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13973 			if (ret) {
13974 				/* Single TC condition is not fatal,
13975 				 * message and continue
13976 				 */
13977 				dev_info(&pf->pdev->dev,
13978 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13979 					 enabled_tc,
13980 					 i40e_stat_str(&pf->hw, ret),
13981 					 i40e_aq_str(&pf->hw,
13982 						    pf->hw.aq.asq_last_status));
13983 			}
13984 		}
13985 		break;
13986 
13987 	case I40E_VSI_FDIR:
13988 		ctxt.pf_num = hw->pf_id;
13989 		ctxt.vf_num = 0;
13990 		ctxt.uplink_seid = vsi->uplink_seid;
13991 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13992 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13993 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13994 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13995 			ctxt.info.valid_sections |=
13996 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13997 			ctxt.info.switch_id =
13998 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13999 		}
14000 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14001 		break;
14002 
14003 	case I40E_VSI_VMDQ2:
14004 		ctxt.pf_num = hw->pf_id;
14005 		ctxt.vf_num = 0;
14006 		ctxt.uplink_seid = vsi->uplink_seid;
14007 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14008 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14009 
14010 		/* This VSI is connected to VEB so the switch_id
14011 		 * should be set to zero by default.
14012 		 */
14013 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14014 			ctxt.info.valid_sections |=
14015 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14016 			ctxt.info.switch_id =
14017 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14018 		}
14019 
14020 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14021 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14022 		break;
14023 
14024 	case I40E_VSI_SRIOV:
14025 		ctxt.pf_num = hw->pf_id;
14026 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14027 		ctxt.uplink_seid = vsi->uplink_seid;
14028 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14029 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14030 
14031 		/* This VSI is connected to VEB so the switch_id
14032 		 * should be set to zero by default.
14033 		 */
14034 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14035 			ctxt.info.valid_sections |=
14036 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14037 			ctxt.info.switch_id =
14038 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14039 		}
14040 
14041 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14042 			ctxt.info.valid_sections |=
14043 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14044 			ctxt.info.queueing_opt_flags |=
14045 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14046 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14047 		}
14048 
14049 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14050 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14051 		if (pf->vf[vsi->vf_id].spoofchk) {
14052 			ctxt.info.valid_sections |=
14053 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14054 			ctxt.info.sec_flags |=
14055 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14056 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14057 		}
14058 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14059 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14060 		break;
14061 
14062 	case I40E_VSI_IWARP:
14063 		/* send down message to iWARP */
14064 		break;
14065 
14066 	default:
14067 		return -ENODEV;
14068 	}
14069 
14070 	if (vsi->type != I40E_VSI_MAIN) {
14071 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14072 		if (ret) {
14073 			dev_info(&vsi->back->pdev->dev,
14074 				 "add vsi failed, err %s aq_err %s\n",
14075 				 i40e_stat_str(&pf->hw, ret),
14076 				 i40e_aq_str(&pf->hw,
14077 					     pf->hw.aq.asq_last_status));
14078 			ret = -ENOENT;
14079 			goto err;
14080 		}
14081 		vsi->info = ctxt.info;
14082 		vsi->info.valid_sections = 0;
14083 		vsi->seid = ctxt.seid;
14084 		vsi->id = ctxt.vsi_number;
14085 	}
14086 
14087 	vsi->active_filters = 0;
14088 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14089 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14090 	/* If macvlan filters already exist, force them to get loaded */
14091 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14092 		f->state = I40E_FILTER_NEW;
14093 		f_count++;
14094 	}
14095 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14096 
14097 	if (f_count) {
14098 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14099 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14100 	}
14101 
14102 	/* Update VSI BW information */
14103 	ret = i40e_vsi_get_bw_info(vsi);
14104 	if (ret) {
14105 		dev_info(&pf->pdev->dev,
14106 			 "couldn't get vsi bw info, err %s aq_err %s\n",
14107 			 i40e_stat_str(&pf->hw, ret),
14108 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14109 		/* VSI is already added so not tearing that up */
14110 		ret = 0;
14111 	}
14112 
14113 err:
14114 	return ret;
14115 }
14116 
14117 /**
14118  * i40e_vsi_release - Delete a VSI and free its resources
14119  * @vsi: the VSI being removed
14120  *
14121  * Returns 0 on success or < 0 on error
14122  **/
i40e_vsi_release(struct i40e_vsi * vsi)14123 int i40e_vsi_release(struct i40e_vsi *vsi)
14124 {
14125 	struct i40e_mac_filter *f;
14126 	struct hlist_node *h;
14127 	struct i40e_veb *veb = NULL;
14128 	struct i40e_pf *pf;
14129 	u16 uplink_seid;
14130 	int i, n, bkt;
14131 
14132 	pf = vsi->back;
14133 
14134 	/* release of a VEB-owner or last VSI is not allowed */
14135 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14136 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14137 			 vsi->seid, vsi->uplink_seid);
14138 		return -ENODEV;
14139 	}
14140 	if (vsi == pf->vsi[pf->lan_vsi] &&
14141 	    !test_bit(__I40E_DOWN, pf->state)) {
14142 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14143 		return -ENODEV;
14144 	}
14145 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14146 	uplink_seid = vsi->uplink_seid;
14147 	if (vsi->type != I40E_VSI_SRIOV) {
14148 		if (vsi->netdev_registered) {
14149 			vsi->netdev_registered = false;
14150 			if (vsi->netdev) {
14151 				/* results in a call to i40e_close() */
14152 				unregister_netdev(vsi->netdev);
14153 			}
14154 		} else {
14155 			i40e_vsi_close(vsi);
14156 		}
14157 		i40e_vsi_disable_irq(vsi);
14158 	}
14159 
14160 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14161 
14162 	/* clear the sync flag on all filters */
14163 	if (vsi->netdev) {
14164 		__dev_uc_unsync(vsi->netdev, NULL);
14165 		__dev_mc_unsync(vsi->netdev, NULL);
14166 	}
14167 
14168 	/* make sure any remaining filters are marked for deletion */
14169 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14170 		__i40e_del_filter(vsi, f);
14171 
14172 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14173 
14174 	i40e_sync_vsi_filters(vsi);
14175 
14176 	i40e_vsi_delete(vsi);
14177 	i40e_vsi_free_q_vectors(vsi);
14178 	if (vsi->netdev) {
14179 		free_netdev(vsi->netdev);
14180 		vsi->netdev = NULL;
14181 	}
14182 	i40e_vsi_clear_rings(vsi);
14183 	i40e_vsi_clear(vsi);
14184 
14185 	/* If this was the last thing on the VEB, except for the
14186 	 * controlling VSI, remove the VEB, which puts the controlling
14187 	 * VSI onto the next level down in the switch.
14188 	 *
14189 	 * Well, okay, there's one more exception here: don't remove
14190 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14191 	 * from up the network stack.
14192 	 */
14193 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14194 		if (pf->vsi[i] &&
14195 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14196 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14197 			n++;      /* count the VSIs */
14198 		}
14199 	}
14200 	for (i = 0; i < I40E_MAX_VEB; i++) {
14201 		if (!pf->veb[i])
14202 			continue;
14203 		if (pf->veb[i]->uplink_seid == uplink_seid)
14204 			n++;     /* count the VEBs */
14205 		if (pf->veb[i]->seid == uplink_seid)
14206 			veb = pf->veb[i];
14207 	}
14208 	if (n == 0 && veb && veb->uplink_seid != 0)
14209 		i40e_veb_release(veb);
14210 
14211 	return 0;
14212 }
14213 
14214 /**
14215  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14216  * @vsi: ptr to the VSI
14217  *
14218  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14219  * corresponding SW VSI structure and initializes num_queue_pairs for the
14220  * newly allocated VSI.
14221  *
14222  * Returns 0 on success or negative on failure
14223  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)14224 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14225 {
14226 	int ret = -ENOENT;
14227 	struct i40e_pf *pf = vsi->back;
14228 
14229 	if (vsi->q_vectors[0]) {
14230 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14231 			 vsi->seid);
14232 		return -EEXIST;
14233 	}
14234 
14235 	if (vsi->base_vector) {
14236 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14237 			 vsi->seid, vsi->base_vector);
14238 		return -EEXIST;
14239 	}
14240 
14241 	ret = i40e_vsi_alloc_q_vectors(vsi);
14242 	if (ret) {
14243 		dev_info(&pf->pdev->dev,
14244 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14245 			 vsi->num_q_vectors, vsi->seid, ret);
14246 		vsi->num_q_vectors = 0;
14247 		goto vector_setup_out;
14248 	}
14249 
14250 	/* In Legacy mode, we do not have to get any other vector since we
14251 	 * piggyback on the misc/ICR0 for queue interrupts.
14252 	*/
14253 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14254 		return ret;
14255 	if (vsi->num_q_vectors)
14256 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14257 						 vsi->num_q_vectors, vsi->idx);
14258 	if (vsi->base_vector < 0) {
14259 		dev_info(&pf->pdev->dev,
14260 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14261 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14262 		i40e_vsi_free_q_vectors(vsi);
14263 		ret = -ENOENT;
14264 		goto vector_setup_out;
14265 	}
14266 
14267 vector_setup_out:
14268 	return ret;
14269 }
14270 
14271 /**
14272  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14273  * @vsi: pointer to the vsi.
14274  *
14275  * This re-allocates a vsi's queue resources.
14276  *
14277  * Returns pointer to the successfully allocated and configured VSI sw struct
14278  * on success, otherwise returns NULL on failure.
14279  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)14280 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14281 {
14282 	u16 alloc_queue_pairs;
14283 	struct i40e_pf *pf;
14284 	u8 enabled_tc;
14285 	int ret;
14286 
14287 	if (!vsi)
14288 		return NULL;
14289 
14290 	pf = vsi->back;
14291 
14292 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14293 	i40e_vsi_clear_rings(vsi);
14294 
14295 	i40e_vsi_free_arrays(vsi, false);
14296 	i40e_set_num_rings_in_vsi(vsi);
14297 	ret = i40e_vsi_alloc_arrays(vsi, false);
14298 	if (ret)
14299 		goto err_vsi;
14300 
14301 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14302 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14303 
14304 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14305 	if (ret < 0) {
14306 		dev_info(&pf->pdev->dev,
14307 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14308 			 alloc_queue_pairs, vsi->seid, ret);
14309 		goto err_vsi;
14310 	}
14311 	vsi->base_queue = ret;
14312 
14313 	/* Update the FW view of the VSI. Force a reset of TC and queue
14314 	 * layout configurations.
14315 	 */
14316 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14317 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14318 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14319 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14320 	if (vsi->type == I40E_VSI_MAIN)
14321 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14322 
14323 	/* assign it some queues */
14324 	ret = i40e_alloc_rings(vsi);
14325 	if (ret)
14326 		goto err_rings;
14327 
14328 	/* map all of the rings to the q_vectors */
14329 	i40e_vsi_map_rings_to_vectors(vsi);
14330 	return vsi;
14331 
14332 err_rings:
14333 	i40e_vsi_free_q_vectors(vsi);
14334 	if (vsi->netdev_registered) {
14335 		vsi->netdev_registered = false;
14336 		unregister_netdev(vsi->netdev);
14337 		free_netdev(vsi->netdev);
14338 		vsi->netdev = NULL;
14339 	}
14340 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14341 err_vsi:
14342 	i40e_vsi_clear(vsi);
14343 	return NULL;
14344 }
14345 
14346 /**
14347  * i40e_vsi_setup - Set up a VSI by a given type
14348  * @pf: board private structure
14349  * @type: VSI type
14350  * @uplink_seid: the switch element to link to
14351  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14352  *
14353  * This allocates the sw VSI structure and its queue resources, then add a VSI
14354  * to the identified VEB.
14355  *
14356  * Returns pointer to the successfully allocated and configure VSI sw struct on
14357  * success, otherwise returns NULL on failure.
14358  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)14359 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14360 				u16 uplink_seid, u32 param1)
14361 {
14362 	struct i40e_vsi *vsi = NULL;
14363 	struct i40e_veb *veb = NULL;
14364 	u16 alloc_queue_pairs;
14365 	int ret, i;
14366 	int v_idx;
14367 
14368 	/* The requested uplink_seid must be either
14369 	 *     - the PF's port seid
14370 	 *              no VEB is needed because this is the PF
14371 	 *              or this is a Flow Director special case VSI
14372 	 *     - seid of an existing VEB
14373 	 *     - seid of a VSI that owns an existing VEB
14374 	 *     - seid of a VSI that doesn't own a VEB
14375 	 *              a new VEB is created and the VSI becomes the owner
14376 	 *     - seid of the PF VSI, which is what creates the first VEB
14377 	 *              this is a special case of the previous
14378 	 *
14379 	 * Find which uplink_seid we were given and create a new VEB if needed
14380 	 */
14381 	for (i = 0; i < I40E_MAX_VEB; i++) {
14382 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14383 			veb = pf->veb[i];
14384 			break;
14385 		}
14386 	}
14387 
14388 	if (!veb && uplink_seid != pf->mac_seid) {
14389 
14390 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14391 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14392 				vsi = pf->vsi[i];
14393 				break;
14394 			}
14395 		}
14396 		if (!vsi) {
14397 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14398 				 uplink_seid);
14399 			return NULL;
14400 		}
14401 
14402 		if (vsi->uplink_seid == pf->mac_seid)
14403 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14404 					     vsi->tc_config.enabled_tc);
14405 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14406 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14407 					     vsi->tc_config.enabled_tc);
14408 		if (veb) {
14409 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14410 				dev_info(&vsi->back->pdev->dev,
14411 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14412 				return NULL;
14413 			}
14414 			/* We come up by default in VEPA mode if SRIOV is not
14415 			 * already enabled, in which case we can't force VEPA
14416 			 * mode.
14417 			 */
14418 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14419 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14420 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14421 			}
14422 			i40e_config_bridge_mode(veb);
14423 		}
14424 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14425 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14426 				veb = pf->veb[i];
14427 		}
14428 		if (!veb) {
14429 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14430 			return NULL;
14431 		}
14432 
14433 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14434 		uplink_seid = veb->seid;
14435 	}
14436 
14437 	/* get vsi sw struct */
14438 	v_idx = i40e_vsi_mem_alloc(pf, type);
14439 	if (v_idx < 0)
14440 		goto err_alloc;
14441 	vsi = pf->vsi[v_idx];
14442 	if (!vsi)
14443 		goto err_alloc;
14444 	vsi->type = type;
14445 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14446 
14447 	if (type == I40E_VSI_MAIN)
14448 		pf->lan_vsi = v_idx;
14449 	else if (type == I40E_VSI_SRIOV)
14450 		vsi->vf_id = param1;
14451 	/* assign it some queues */
14452 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14453 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14454 
14455 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14456 	if (ret < 0) {
14457 		dev_info(&pf->pdev->dev,
14458 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14459 			 alloc_queue_pairs, vsi->seid, ret);
14460 		goto err_vsi;
14461 	}
14462 	vsi->base_queue = ret;
14463 
14464 	/* get a VSI from the hardware */
14465 	vsi->uplink_seid = uplink_seid;
14466 	ret = i40e_add_vsi(vsi);
14467 	if (ret)
14468 		goto err_vsi;
14469 
14470 	switch (vsi->type) {
14471 	/* setup the netdev if needed */
14472 	case I40E_VSI_MAIN:
14473 	case I40E_VSI_VMDQ2:
14474 		ret = i40e_config_netdev(vsi);
14475 		if (ret)
14476 			goto err_netdev;
14477 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14478 		if (ret)
14479 			goto err_netdev;
14480 		ret = register_netdev(vsi->netdev);
14481 		if (ret)
14482 			goto err_netdev;
14483 		vsi->netdev_registered = true;
14484 		netif_carrier_off(vsi->netdev);
14485 #ifdef CONFIG_I40E_DCB
14486 		/* Setup DCB netlink interface */
14487 		i40e_dcbnl_setup(vsi);
14488 #endif /* CONFIG_I40E_DCB */
14489 		fallthrough;
14490 	case I40E_VSI_FDIR:
14491 		/* set up vectors and rings if needed */
14492 		ret = i40e_vsi_setup_vectors(vsi);
14493 		if (ret)
14494 			goto err_msix;
14495 
14496 		ret = i40e_alloc_rings(vsi);
14497 		if (ret)
14498 			goto err_rings;
14499 
14500 		/* map all of the rings to the q_vectors */
14501 		i40e_vsi_map_rings_to_vectors(vsi);
14502 
14503 		i40e_vsi_reset_stats(vsi);
14504 		break;
14505 	default:
14506 		/* no netdev or rings for the other VSI types */
14507 		break;
14508 	}
14509 
14510 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14511 	    (vsi->type == I40E_VSI_VMDQ2)) {
14512 		ret = i40e_vsi_config_rss(vsi);
14513 	}
14514 	return vsi;
14515 
14516 err_rings:
14517 	i40e_vsi_free_q_vectors(vsi);
14518 err_msix:
14519 	if (vsi->netdev_registered) {
14520 		vsi->netdev_registered = false;
14521 		unregister_netdev(vsi->netdev);
14522 		free_netdev(vsi->netdev);
14523 		vsi->netdev = NULL;
14524 	}
14525 err_netdev:
14526 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14527 err_vsi:
14528 	i40e_vsi_clear(vsi);
14529 err_alloc:
14530 	return NULL;
14531 }
14532 
14533 /**
14534  * i40e_veb_get_bw_info - Query VEB BW information
14535  * @veb: the veb to query
14536  *
14537  * Query the Tx scheduler BW configuration data for given VEB
14538  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)14539 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14540 {
14541 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14542 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14543 	struct i40e_pf *pf = veb->pf;
14544 	struct i40e_hw *hw = &pf->hw;
14545 	u32 tc_bw_max;
14546 	int ret = 0;
14547 	int i;
14548 
14549 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14550 						  &bw_data, NULL);
14551 	if (ret) {
14552 		dev_info(&pf->pdev->dev,
14553 			 "query veb bw config failed, err %s aq_err %s\n",
14554 			 i40e_stat_str(&pf->hw, ret),
14555 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14556 		goto out;
14557 	}
14558 
14559 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14560 						   &ets_data, NULL);
14561 	if (ret) {
14562 		dev_info(&pf->pdev->dev,
14563 			 "query veb bw ets config failed, err %s aq_err %s\n",
14564 			 i40e_stat_str(&pf->hw, ret),
14565 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14566 		goto out;
14567 	}
14568 
14569 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14570 	veb->bw_max_quanta = ets_data.tc_bw_max;
14571 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14572 	veb->enabled_tc = ets_data.tc_valid_bits;
14573 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14574 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14575 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14576 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14577 		veb->bw_tc_limit_credits[i] =
14578 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14579 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14580 	}
14581 
14582 out:
14583 	return ret;
14584 }
14585 
14586 /**
14587  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14588  * @pf: board private structure
14589  *
14590  * On error: returns error code (negative)
14591  * On success: returns vsi index in PF (positive)
14592  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)14593 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14594 {
14595 	int ret = -ENOENT;
14596 	struct i40e_veb *veb;
14597 	int i;
14598 
14599 	/* Need to protect the allocation of switch elements at the PF level */
14600 	mutex_lock(&pf->switch_mutex);
14601 
14602 	/* VEB list may be fragmented if VEB creation/destruction has
14603 	 * been happening.  We can afford to do a quick scan to look
14604 	 * for any free slots in the list.
14605 	 *
14606 	 * find next empty veb slot, looping back around if necessary
14607 	 */
14608 	i = 0;
14609 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14610 		i++;
14611 	if (i >= I40E_MAX_VEB) {
14612 		ret = -ENOMEM;
14613 		goto err_alloc_veb;  /* out of VEB slots! */
14614 	}
14615 
14616 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14617 	if (!veb) {
14618 		ret = -ENOMEM;
14619 		goto err_alloc_veb;
14620 	}
14621 	veb->pf = pf;
14622 	veb->idx = i;
14623 	veb->enabled_tc = 1;
14624 
14625 	pf->veb[i] = veb;
14626 	ret = i;
14627 err_alloc_veb:
14628 	mutex_unlock(&pf->switch_mutex);
14629 	return ret;
14630 }
14631 
14632 /**
14633  * i40e_switch_branch_release - Delete a branch of the switch tree
14634  * @branch: where to start deleting
14635  *
14636  * This uses recursion to find the tips of the branch to be
14637  * removed, deleting until we get back to and can delete this VEB.
14638  **/
i40e_switch_branch_release(struct i40e_veb * branch)14639 static void i40e_switch_branch_release(struct i40e_veb *branch)
14640 {
14641 	struct i40e_pf *pf = branch->pf;
14642 	u16 branch_seid = branch->seid;
14643 	u16 veb_idx = branch->idx;
14644 	int i;
14645 
14646 	/* release any VEBs on this VEB - RECURSION */
14647 	for (i = 0; i < I40E_MAX_VEB; i++) {
14648 		if (!pf->veb[i])
14649 			continue;
14650 		if (pf->veb[i]->uplink_seid == branch->seid)
14651 			i40e_switch_branch_release(pf->veb[i]);
14652 	}
14653 
14654 	/* Release the VSIs on this VEB, but not the owner VSI.
14655 	 *
14656 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14657 	 *       the VEB itself, so don't use (*branch) after this loop.
14658 	 */
14659 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14660 		if (!pf->vsi[i])
14661 			continue;
14662 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14663 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14664 			i40e_vsi_release(pf->vsi[i]);
14665 		}
14666 	}
14667 
14668 	/* There's one corner case where the VEB might not have been
14669 	 * removed, so double check it here and remove it if needed.
14670 	 * This case happens if the veb was created from the debugfs
14671 	 * commands and no VSIs were added to it.
14672 	 */
14673 	if (pf->veb[veb_idx])
14674 		i40e_veb_release(pf->veb[veb_idx]);
14675 }
14676 
14677 /**
14678  * i40e_veb_clear - remove veb struct
14679  * @veb: the veb to remove
14680  **/
i40e_veb_clear(struct i40e_veb * veb)14681 static void i40e_veb_clear(struct i40e_veb *veb)
14682 {
14683 	if (!veb)
14684 		return;
14685 
14686 	if (veb->pf) {
14687 		struct i40e_pf *pf = veb->pf;
14688 
14689 		mutex_lock(&pf->switch_mutex);
14690 		if (pf->veb[veb->idx] == veb)
14691 			pf->veb[veb->idx] = NULL;
14692 		mutex_unlock(&pf->switch_mutex);
14693 	}
14694 
14695 	kfree(veb);
14696 }
14697 
14698 /**
14699  * i40e_veb_release - Delete a VEB and free its resources
14700  * @veb: the VEB being removed
14701  **/
i40e_veb_release(struct i40e_veb * veb)14702 void i40e_veb_release(struct i40e_veb *veb)
14703 {
14704 	struct i40e_vsi *vsi = NULL;
14705 	struct i40e_pf *pf;
14706 	int i, n = 0;
14707 
14708 	pf = veb->pf;
14709 
14710 	/* find the remaining VSI and check for extras */
14711 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14712 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14713 			n++;
14714 			vsi = pf->vsi[i];
14715 		}
14716 	}
14717 	if (n != 1) {
14718 		dev_info(&pf->pdev->dev,
14719 			 "can't remove VEB %d with %d VSIs left\n",
14720 			 veb->seid, n);
14721 		return;
14722 	}
14723 
14724 	/* move the remaining VSI to uplink veb */
14725 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14726 	if (veb->uplink_seid) {
14727 		vsi->uplink_seid = veb->uplink_seid;
14728 		if (veb->uplink_seid == pf->mac_seid)
14729 			vsi->veb_idx = I40E_NO_VEB;
14730 		else
14731 			vsi->veb_idx = veb->veb_idx;
14732 	} else {
14733 		/* floating VEB */
14734 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14735 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14736 	}
14737 
14738 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14739 	i40e_veb_clear(veb);
14740 }
14741 
14742 /**
14743  * i40e_add_veb - create the VEB in the switch
14744  * @veb: the VEB to be instantiated
14745  * @vsi: the controlling VSI
14746  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)14747 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14748 {
14749 	struct i40e_pf *pf = veb->pf;
14750 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14751 	int ret;
14752 
14753 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14754 			      veb->enabled_tc, false,
14755 			      &veb->seid, enable_stats, NULL);
14756 
14757 	/* get a VEB from the hardware */
14758 	if (ret) {
14759 		dev_info(&pf->pdev->dev,
14760 			 "couldn't add VEB, err %s aq_err %s\n",
14761 			 i40e_stat_str(&pf->hw, ret),
14762 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14763 		return -EPERM;
14764 	}
14765 
14766 	/* get statistics counter */
14767 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14768 					 &veb->stats_idx, NULL, NULL, NULL);
14769 	if (ret) {
14770 		dev_info(&pf->pdev->dev,
14771 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14772 			 i40e_stat_str(&pf->hw, ret),
14773 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14774 		return -EPERM;
14775 	}
14776 	ret = i40e_veb_get_bw_info(veb);
14777 	if (ret) {
14778 		dev_info(&pf->pdev->dev,
14779 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14780 			 i40e_stat_str(&pf->hw, ret),
14781 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14782 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14783 		return -ENOENT;
14784 	}
14785 
14786 	vsi->uplink_seid = veb->seid;
14787 	vsi->veb_idx = veb->idx;
14788 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14789 
14790 	return 0;
14791 }
14792 
14793 /**
14794  * i40e_veb_setup - Set up a VEB
14795  * @pf: board private structure
14796  * @flags: VEB setup flags
14797  * @uplink_seid: the switch element to link to
14798  * @vsi_seid: the initial VSI seid
14799  * @enabled_tc: Enabled TC bit-map
14800  *
14801  * This allocates the sw VEB structure and links it into the switch
14802  * It is possible and legal for this to be a duplicate of an already
14803  * existing VEB.  It is also possible for both uplink and vsi seids
14804  * to be zero, in order to create a floating VEB.
14805  *
14806  * Returns pointer to the successfully allocated VEB sw struct on
14807  * success, otherwise returns NULL on failure.
14808  **/
i40e_veb_setup(struct i40e_pf * pf,u16 flags,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14809 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14810 				u16 uplink_seid, u16 vsi_seid,
14811 				u8 enabled_tc)
14812 {
14813 	struct i40e_veb *veb, *uplink_veb = NULL;
14814 	int vsi_idx, veb_idx;
14815 	int ret;
14816 
14817 	/* if one seid is 0, the other must be 0 to create a floating relay */
14818 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14819 	    (uplink_seid + vsi_seid != 0)) {
14820 		dev_info(&pf->pdev->dev,
14821 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14822 			 uplink_seid, vsi_seid);
14823 		return NULL;
14824 	}
14825 
14826 	/* make sure there is such a vsi and uplink */
14827 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14828 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14829 			break;
14830 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14831 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14832 			 vsi_seid);
14833 		return NULL;
14834 	}
14835 
14836 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14837 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14838 			if (pf->veb[veb_idx] &&
14839 			    pf->veb[veb_idx]->seid == uplink_seid) {
14840 				uplink_veb = pf->veb[veb_idx];
14841 				break;
14842 			}
14843 		}
14844 		if (!uplink_veb) {
14845 			dev_info(&pf->pdev->dev,
14846 				 "uplink seid %d not found\n", uplink_seid);
14847 			return NULL;
14848 		}
14849 	}
14850 
14851 	/* get veb sw struct */
14852 	veb_idx = i40e_veb_mem_alloc(pf);
14853 	if (veb_idx < 0)
14854 		goto err_alloc;
14855 	veb = pf->veb[veb_idx];
14856 	veb->flags = flags;
14857 	veb->uplink_seid = uplink_seid;
14858 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14859 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14860 
14861 	/* create the VEB in the switch */
14862 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14863 	if (ret)
14864 		goto err_veb;
14865 	if (vsi_idx == pf->lan_vsi)
14866 		pf->lan_veb = veb->idx;
14867 
14868 	return veb;
14869 
14870 err_veb:
14871 	i40e_veb_clear(veb);
14872 err_alloc:
14873 	return NULL;
14874 }
14875 
14876 /**
14877  * i40e_setup_pf_switch_element - set PF vars based on switch type
14878  * @pf: board private structure
14879  * @ele: element we are building info from
14880  * @num_reported: total number of elements
14881  * @printconfig: should we print the contents
14882  *
14883  * helper function to assist in extracting a few useful SEID values.
14884  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14885 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14886 				struct i40e_aqc_switch_config_element_resp *ele,
14887 				u16 num_reported, bool printconfig)
14888 {
14889 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14890 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14891 	u8 element_type = ele->element_type;
14892 	u16 seid = le16_to_cpu(ele->seid);
14893 
14894 	if (printconfig)
14895 		dev_info(&pf->pdev->dev,
14896 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14897 			 element_type, seid, uplink_seid, downlink_seid);
14898 
14899 	switch (element_type) {
14900 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14901 		pf->mac_seid = seid;
14902 		break;
14903 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14904 		/* Main VEB? */
14905 		if (uplink_seid != pf->mac_seid)
14906 			break;
14907 		if (pf->lan_veb >= I40E_MAX_VEB) {
14908 			int v;
14909 
14910 			/* find existing or else empty VEB */
14911 			for (v = 0; v < I40E_MAX_VEB; v++) {
14912 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14913 					pf->lan_veb = v;
14914 					break;
14915 				}
14916 			}
14917 			if (pf->lan_veb >= I40E_MAX_VEB) {
14918 				v = i40e_veb_mem_alloc(pf);
14919 				if (v < 0)
14920 					break;
14921 				pf->lan_veb = v;
14922 			}
14923 		}
14924 		if (pf->lan_veb >= I40E_MAX_VEB)
14925 			break;
14926 
14927 		pf->veb[pf->lan_veb]->seid = seid;
14928 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14929 		pf->veb[pf->lan_veb]->pf = pf;
14930 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14931 		break;
14932 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14933 		if (num_reported != 1)
14934 			break;
14935 		/* This is immediately after a reset so we can assume this is
14936 		 * the PF's VSI
14937 		 */
14938 		pf->mac_seid = uplink_seid;
14939 		pf->pf_seid = downlink_seid;
14940 		pf->main_vsi_seid = seid;
14941 		if (printconfig)
14942 			dev_info(&pf->pdev->dev,
14943 				 "pf_seid=%d main_vsi_seid=%d\n",
14944 				 pf->pf_seid, pf->main_vsi_seid);
14945 		break;
14946 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14947 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14948 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14949 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14950 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14951 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14952 		/* ignore these for now */
14953 		break;
14954 	default:
14955 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14956 			 element_type, seid);
14957 		break;
14958 	}
14959 }
14960 
14961 /**
14962  * i40e_fetch_switch_configuration - Get switch config from firmware
14963  * @pf: board private structure
14964  * @printconfig: should we print the contents
14965  *
14966  * Get the current switch configuration from the device and
14967  * extract a few useful SEID values.
14968  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)14969 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14970 {
14971 	struct i40e_aqc_get_switch_config_resp *sw_config;
14972 	u16 next_seid = 0;
14973 	int ret = 0;
14974 	u8 *aq_buf;
14975 	int i;
14976 
14977 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14978 	if (!aq_buf)
14979 		return -ENOMEM;
14980 
14981 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14982 	do {
14983 		u16 num_reported, num_total;
14984 
14985 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14986 						I40E_AQ_LARGE_BUF,
14987 						&next_seid, NULL);
14988 		if (ret) {
14989 			dev_info(&pf->pdev->dev,
14990 				 "get switch config failed err %s aq_err %s\n",
14991 				 i40e_stat_str(&pf->hw, ret),
14992 				 i40e_aq_str(&pf->hw,
14993 					     pf->hw.aq.asq_last_status));
14994 			kfree(aq_buf);
14995 			return -ENOENT;
14996 		}
14997 
14998 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14999 		num_total = le16_to_cpu(sw_config->header.num_total);
15000 
15001 		if (printconfig)
15002 			dev_info(&pf->pdev->dev,
15003 				 "header: %d reported %d total\n",
15004 				 num_reported, num_total);
15005 
15006 		for (i = 0; i < num_reported; i++) {
15007 			struct i40e_aqc_switch_config_element_resp *ele =
15008 				&sw_config->element[i];
15009 
15010 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15011 						     printconfig);
15012 		}
15013 	} while (next_seid != 0);
15014 
15015 	kfree(aq_buf);
15016 	return ret;
15017 }
15018 
15019 /**
15020  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15021  * @pf: board private structure
15022  * @reinit: if the Main VSI needs to re-initialized.
15023  * @lock_acquired: indicates whether or not the lock has been acquired
15024  *
15025  * Returns 0 on success, negative value on failure
15026  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)15027 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15028 {
15029 	u16 flags = 0;
15030 	int ret;
15031 
15032 	/* find out what's out there already */
15033 	ret = i40e_fetch_switch_configuration(pf, false);
15034 	if (ret) {
15035 		dev_info(&pf->pdev->dev,
15036 			 "couldn't fetch switch config, err %s aq_err %s\n",
15037 			 i40e_stat_str(&pf->hw, ret),
15038 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15039 		return ret;
15040 	}
15041 	i40e_pf_reset_stats(pf);
15042 
15043 	/* set the switch config bit for the whole device to
15044 	 * support limited promisc or true promisc
15045 	 * when user requests promisc. The default is limited
15046 	 * promisc.
15047 	*/
15048 
15049 	if ((pf->hw.pf_id == 0) &&
15050 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15051 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15052 		pf->last_sw_conf_flags = flags;
15053 	}
15054 
15055 	if (pf->hw.pf_id == 0) {
15056 		u16 valid_flags;
15057 
15058 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15059 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15060 						NULL);
15061 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15062 			dev_info(&pf->pdev->dev,
15063 				 "couldn't set switch config bits, err %s aq_err %s\n",
15064 				 i40e_stat_str(&pf->hw, ret),
15065 				 i40e_aq_str(&pf->hw,
15066 					     pf->hw.aq.asq_last_status));
15067 			/* not a fatal problem, just keep going */
15068 		}
15069 		pf->last_sw_conf_valid_flags = valid_flags;
15070 	}
15071 
15072 	/* first time setup */
15073 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15074 		struct i40e_vsi *vsi = NULL;
15075 		u16 uplink_seid;
15076 
15077 		/* Set up the PF VSI associated with the PF's main VSI
15078 		 * that is already in the HW switch
15079 		 */
15080 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15081 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15082 		else
15083 			uplink_seid = pf->mac_seid;
15084 		if (pf->lan_vsi == I40E_NO_VSI)
15085 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15086 		else if (reinit)
15087 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15088 		if (!vsi) {
15089 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15090 			i40e_cloud_filter_exit(pf);
15091 			i40e_fdir_teardown(pf);
15092 			return -EAGAIN;
15093 		}
15094 	} else {
15095 		/* force a reset of TC and queue layout configurations */
15096 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15097 
15098 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15099 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15100 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15101 	}
15102 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15103 
15104 	i40e_fdir_sb_setup(pf);
15105 
15106 	/* Setup static PF queue filter control settings */
15107 	ret = i40e_setup_pf_filter_control(pf);
15108 	if (ret) {
15109 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15110 			 ret);
15111 		/* Failure here should not stop continuing other steps */
15112 	}
15113 
15114 	/* enable RSS in the HW, even for only one queue, as the stack can use
15115 	 * the hash
15116 	 */
15117 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15118 		i40e_pf_config_rss(pf);
15119 
15120 	/* fill in link information and enable LSE reporting */
15121 	i40e_link_event(pf);
15122 
15123 	/* Initialize user-specific link properties */
15124 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15125 				  I40E_AQ_AN_COMPLETED) ? true : false);
15126 
15127 	i40e_ptp_init(pf);
15128 
15129 	if (!lock_acquired)
15130 		rtnl_lock();
15131 
15132 	/* repopulate tunnel port filters */
15133 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15134 
15135 	if (!lock_acquired)
15136 		rtnl_unlock();
15137 
15138 	return ret;
15139 }
15140 
15141 /**
15142  * i40e_determine_queue_usage - Work out queue distribution
15143  * @pf: board private structure
15144  **/
i40e_determine_queue_usage(struct i40e_pf * pf)15145 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15146 {
15147 	int queues_left;
15148 	int q_max;
15149 
15150 	pf->num_lan_qps = 0;
15151 
15152 	/* Find the max queues to be put into basic use.  We'll always be
15153 	 * using TC0, whether or not DCB is running, and TC0 will get the
15154 	 * big RSS set.
15155 	 */
15156 	queues_left = pf->hw.func_caps.num_tx_qp;
15157 
15158 	if ((queues_left == 1) ||
15159 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15160 		/* one qp for PF, no queues for anything else */
15161 		queues_left = 0;
15162 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15163 
15164 		/* make sure all the fancies are disabled */
15165 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15166 			       I40E_FLAG_IWARP_ENABLED	|
15167 			       I40E_FLAG_FD_SB_ENABLED	|
15168 			       I40E_FLAG_FD_ATR_ENABLED	|
15169 			       I40E_FLAG_DCB_CAPABLE	|
15170 			       I40E_FLAG_DCB_ENABLED	|
15171 			       I40E_FLAG_SRIOV_ENABLED	|
15172 			       I40E_FLAG_VMDQ_ENABLED);
15173 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15174 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15175 				  I40E_FLAG_FD_SB_ENABLED |
15176 				  I40E_FLAG_FD_ATR_ENABLED |
15177 				  I40E_FLAG_DCB_CAPABLE))) {
15178 		/* one qp for PF */
15179 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15180 		queues_left -= pf->num_lan_qps;
15181 
15182 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15183 			       I40E_FLAG_IWARP_ENABLED	|
15184 			       I40E_FLAG_FD_SB_ENABLED	|
15185 			       I40E_FLAG_FD_ATR_ENABLED	|
15186 			       I40E_FLAG_DCB_ENABLED	|
15187 			       I40E_FLAG_VMDQ_ENABLED);
15188 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15189 	} else {
15190 		/* Not enough queues for all TCs */
15191 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15192 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15193 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15194 					I40E_FLAG_DCB_ENABLED);
15195 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15196 		}
15197 
15198 		/* limit lan qps to the smaller of qps, cpus or msix */
15199 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15200 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15201 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15202 		pf->num_lan_qps = q_max;
15203 
15204 		queues_left -= pf->num_lan_qps;
15205 	}
15206 
15207 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15208 		if (queues_left > 1) {
15209 			queues_left -= 1; /* save 1 queue for FD */
15210 		} else {
15211 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15212 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15213 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15214 		}
15215 	}
15216 
15217 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15218 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15219 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15220 					(queues_left / pf->num_vf_qps));
15221 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15222 	}
15223 
15224 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15225 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15226 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15227 					  (queues_left / pf->num_vmdq_qps));
15228 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15229 	}
15230 
15231 	pf->queues_left = queues_left;
15232 	dev_dbg(&pf->pdev->dev,
15233 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15234 		pf->hw.func_caps.num_tx_qp,
15235 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15236 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15237 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15238 		queues_left);
15239 }
15240 
15241 /**
15242  * i40e_setup_pf_filter_control - Setup PF static filter control
15243  * @pf: PF to be setup
15244  *
15245  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15246  * settings. If PE/FCoE are enabled then it will also set the per PF
15247  * based filter sizes required for them. It also enables Flow director,
15248  * ethertype and macvlan type filter settings for the pf.
15249  *
15250  * Returns 0 on success, negative on failure
15251  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)15252 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15253 {
15254 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15255 
15256 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15257 
15258 	/* Flow Director is enabled */
15259 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15260 		settings->enable_fdir = true;
15261 
15262 	/* Ethtype and MACVLAN filters enabled for PF */
15263 	settings->enable_ethtype = true;
15264 	settings->enable_macvlan = true;
15265 
15266 	if (i40e_set_filter_control(&pf->hw, settings))
15267 		return -ENOENT;
15268 
15269 	return 0;
15270 }
15271 
15272 #define INFO_STRING_LEN 255
15273 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)15274 static void i40e_print_features(struct i40e_pf *pf)
15275 {
15276 	struct i40e_hw *hw = &pf->hw;
15277 	char *buf;
15278 	int i;
15279 
15280 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15281 	if (!buf)
15282 		return;
15283 
15284 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15285 #ifdef CONFIG_PCI_IOV
15286 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15287 #endif
15288 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15289 		      pf->hw.func_caps.num_vsis,
15290 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15291 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15292 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15293 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15294 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15295 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15296 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15297 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15298 	}
15299 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15300 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15301 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15302 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15303 	if (pf->flags & I40E_FLAG_PTP)
15304 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15305 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15306 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15307 	else
15308 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15309 
15310 	dev_info(&pf->pdev->dev, "%s\n", buf);
15311 	kfree(buf);
15312 	WARN_ON(i > INFO_STRING_LEN);
15313 }
15314 
15315 /**
15316  * i40e_get_platform_mac_addr - get platform-specific MAC address
15317  * @pdev: PCI device information struct
15318  * @pf: board private structure
15319  *
15320  * Look up the MAC address for the device. First we'll try
15321  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15322  * specific fallback. Otherwise, we'll default to the stored value in
15323  * firmware.
15324  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)15325 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15326 {
15327 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15328 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15329 }
15330 
15331 /**
15332  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15333  * @fec_cfg: FEC option to set in flags
15334  * @flags: ptr to flags in which we set FEC option
15335  **/
i40e_set_fec_in_flags(u8 fec_cfg,u32 * flags)15336 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15337 {
15338 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15339 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15340 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15341 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15342 		*flags |= I40E_FLAG_RS_FEC;
15343 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15344 	}
15345 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15346 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15347 		*flags |= I40E_FLAG_BASE_R_FEC;
15348 		*flags &= ~I40E_FLAG_RS_FEC;
15349 	}
15350 	if (fec_cfg == 0)
15351 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15352 }
15353 
15354 /**
15355  * i40e_check_recovery_mode - check if we are running transition firmware
15356  * @pf: board private structure
15357  *
15358  * Check registers indicating the firmware runs in recovery mode. Sets the
15359  * appropriate driver state.
15360  *
15361  * Returns true if the recovery mode was detected, false otherwise
15362  **/
i40e_check_recovery_mode(struct i40e_pf * pf)15363 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15364 {
15365 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15366 
15367 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15368 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15369 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15370 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15371 
15372 		return true;
15373 	}
15374 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15375 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15376 
15377 	return false;
15378 }
15379 
15380 /**
15381  * i40e_pf_loop_reset - perform reset in a loop.
15382  * @pf: board private structure
15383  *
15384  * This function is useful when a NIC is about to enter recovery mode.
15385  * When a NIC's internal data structures are corrupted the NIC's
15386  * firmware is going to enter recovery mode.
15387  * Right after a POR it takes about 7 minutes for firmware to enter
15388  * recovery mode. Until that time a NIC is in some kind of intermediate
15389  * state. After that time period the NIC almost surely enters
15390  * recovery mode. The only way for a driver to detect intermediate
15391  * state is to issue a series of pf-resets and check a return value.
15392  * If a PF reset returns success then the firmware could be in recovery
15393  * mode so the caller of this code needs to check for recovery mode
15394  * if this function returns success. There is a little chance that
15395  * firmware will hang in intermediate state forever.
15396  * Since waiting 7 minutes is quite a lot of time this function waits
15397  * 10 seconds and then gives up by returning an error.
15398  *
15399  * Return 0 on success, negative on failure.
15400  **/
i40e_pf_loop_reset(struct i40e_pf * pf)15401 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15402 {
15403 	/* wait max 10 seconds for PF reset to succeed */
15404 	const unsigned long time_end = jiffies + 10 * HZ;
15405 
15406 	struct i40e_hw *hw = &pf->hw;
15407 	i40e_status ret;
15408 
15409 	ret = i40e_pf_reset(hw);
15410 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15411 		usleep_range(10000, 20000);
15412 		ret = i40e_pf_reset(hw);
15413 	}
15414 
15415 	if (ret == I40E_SUCCESS)
15416 		pf->pfr_count++;
15417 	else
15418 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15419 
15420 	return ret;
15421 }
15422 
15423 /**
15424  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15425  * @pf: board private structure
15426  *
15427  * Check FW registers to determine if FW issued unexpected EMP Reset.
15428  * Every time when unexpected EMP Reset occurs the FW increments
15429  * a counter of unexpected EMP Resets. When the counter reaches 10
15430  * the FW should enter the Recovery mode
15431  *
15432  * Returns true if FW issued unexpected EMP Reset
15433  **/
i40e_check_fw_empr(struct i40e_pf * pf)15434 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15435 {
15436 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15437 			   I40E_GL_FWSTS_FWS1B_MASK;
15438 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15439 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15440 }
15441 
15442 /**
15443  * i40e_handle_resets - handle EMP resets and PF resets
15444  * @pf: board private structure
15445  *
15446  * Handle both EMP resets and PF resets and conclude whether there are
15447  * any issues regarding these resets. If there are any issues then
15448  * generate log entry.
15449  *
15450  * Return 0 if NIC is healthy or negative value when there are issues
15451  * with resets
15452  **/
i40e_handle_resets(struct i40e_pf * pf)15453 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15454 {
15455 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15456 	const bool is_empr = i40e_check_fw_empr(pf);
15457 
15458 	if (is_empr || pfr != I40E_SUCCESS)
15459 		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");
15460 
15461 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15462 }
15463 
15464 /**
15465  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15466  * @pf: board private structure
15467  * @hw: ptr to the hardware info
15468  *
15469  * This function does a minimal setup of all subsystems needed for running
15470  * recovery mode.
15471  *
15472  * Returns 0 on success, negative on failure
15473  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)15474 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15475 {
15476 	struct i40e_vsi *vsi;
15477 	int err;
15478 	int v_idx;
15479 
15480 	pci_save_state(pf->pdev);
15481 
15482 	/* set up periodic task facility */
15483 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15484 	pf->service_timer_period = HZ;
15485 
15486 	INIT_WORK(&pf->service_task, i40e_service_task);
15487 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15488 
15489 	err = i40e_init_interrupt_scheme(pf);
15490 	if (err)
15491 		goto err_switch_setup;
15492 
15493 	/* The number of VSIs reported by the FW is the minimum guaranteed
15494 	 * to us; HW supports far more and we share the remaining pool with
15495 	 * the other PFs. We allocate space for more than the guarantee with
15496 	 * the understanding that we might not get them all later.
15497 	 */
15498 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15499 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15500 	else
15501 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15502 
15503 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15504 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15505 			  GFP_KERNEL);
15506 	if (!pf->vsi) {
15507 		err = -ENOMEM;
15508 		goto err_switch_setup;
15509 	}
15510 
15511 	/* We allocate one VSI which is needed as absolute minimum
15512 	 * in order to register the netdev
15513 	 */
15514 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15515 	if (v_idx < 0) {
15516 		err = v_idx;
15517 		goto err_switch_setup;
15518 	}
15519 	pf->lan_vsi = v_idx;
15520 	vsi = pf->vsi[v_idx];
15521 	if (!vsi) {
15522 		err = -EFAULT;
15523 		goto err_switch_setup;
15524 	}
15525 	vsi->alloc_queue_pairs = 1;
15526 	err = i40e_config_netdev(vsi);
15527 	if (err)
15528 		goto err_switch_setup;
15529 	err = register_netdev(vsi->netdev);
15530 	if (err)
15531 		goto err_switch_setup;
15532 	vsi->netdev_registered = true;
15533 	i40e_dbg_pf_init(pf);
15534 
15535 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15536 	if (err)
15537 		goto err_switch_setup;
15538 
15539 	/* tell the firmware that we're starting */
15540 	i40e_send_version(pf);
15541 
15542 	/* since everything's happy, start the service_task timer */
15543 	mod_timer(&pf->service_timer,
15544 		  round_jiffies(jiffies + pf->service_timer_period));
15545 
15546 	return 0;
15547 
15548 err_switch_setup:
15549 	i40e_reset_interrupt_capability(pf);
15550 	del_timer_sync(&pf->service_timer);
15551 	i40e_shutdown_adminq(hw);
15552 	iounmap(hw->hw_addr);
15553 	pci_disable_pcie_error_reporting(pf->pdev);
15554 	pci_release_mem_regions(pf->pdev);
15555 	pci_disable_device(pf->pdev);
15556 	kfree(pf);
15557 
15558 	return err;
15559 }
15560 
15561 /**
15562  * i40e_set_subsystem_device_id - set subsystem device id
15563  * @hw: pointer to the hardware info
15564  *
15565  * Set PCI subsystem device id either from a pci_dev structure or
15566  * a specific FW register.
15567  **/
i40e_set_subsystem_device_id(struct i40e_hw * hw)15568 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15569 {
15570 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15571 
15572 	hw->subsystem_device_id = pdev->subsystem_device ?
15573 		pdev->subsystem_device :
15574 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15575 }
15576 
15577 /**
15578  * i40e_probe - Device initialization routine
15579  * @pdev: PCI device information struct
15580  * @ent: entry in i40e_pci_tbl
15581  *
15582  * i40e_probe initializes a PF identified by a pci_dev structure.
15583  * The OS initialization, configuring of the PF private structure,
15584  * and a hardware reset occur.
15585  *
15586  * Returns 0 on success, negative on failure
15587  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)15588 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15589 {
15590 	struct i40e_aq_get_phy_abilities_resp abilities;
15591 #ifdef CONFIG_I40E_DCB
15592 	enum i40e_get_fw_lldp_status_resp lldp_status;
15593 	i40e_status status;
15594 #endif /* CONFIG_I40E_DCB */
15595 	struct i40e_pf *pf;
15596 	struct i40e_hw *hw;
15597 	static u16 pfs_found;
15598 	u16 wol_nvm_bits;
15599 	u16 link_status;
15600 	int err;
15601 	u32 val;
15602 	u32 i;
15603 
15604 	err = pci_enable_device_mem(pdev);
15605 	if (err)
15606 		return err;
15607 
15608 	/* set up for high or low dma */
15609 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15610 	if (err) {
15611 		dev_err(&pdev->dev,
15612 			"DMA configuration failed: 0x%x\n", err);
15613 		goto err_dma;
15614 	}
15615 
15616 	/* set up pci connections */
15617 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15618 	if (err) {
15619 		dev_info(&pdev->dev,
15620 			 "pci_request_selected_regions failed %d\n", err);
15621 		goto err_pci_reg;
15622 	}
15623 
15624 	pci_enable_pcie_error_reporting(pdev);
15625 	pci_set_master(pdev);
15626 
15627 	/* Now that we have a PCI connection, we need to do the
15628 	 * low level device setup.  This is primarily setting up
15629 	 * the Admin Queue structures and then querying for the
15630 	 * device's current profile information.
15631 	 */
15632 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15633 	if (!pf) {
15634 		err = -ENOMEM;
15635 		goto err_pf_alloc;
15636 	}
15637 	pf->next_vsi = 0;
15638 	pf->pdev = pdev;
15639 	set_bit(__I40E_DOWN, pf->state);
15640 
15641 	hw = &pf->hw;
15642 	hw->back = pf;
15643 
15644 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15645 				I40E_MAX_CSR_SPACE);
15646 	/* We believe that the highest register to read is
15647 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15648 	 * is not less than that before mapping to prevent a
15649 	 * kernel panic.
15650 	 */
15651 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15652 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15653 			pf->ioremap_len);
15654 		err = -ENOMEM;
15655 		goto err_ioremap;
15656 	}
15657 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15658 	if (!hw->hw_addr) {
15659 		err = -EIO;
15660 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15661 			 (unsigned int)pci_resource_start(pdev, 0),
15662 			 pf->ioremap_len, err);
15663 		goto err_ioremap;
15664 	}
15665 	hw->vendor_id = pdev->vendor;
15666 	hw->device_id = pdev->device;
15667 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15668 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15669 	i40e_set_subsystem_device_id(hw);
15670 	hw->bus.device = PCI_SLOT(pdev->devfn);
15671 	hw->bus.func = PCI_FUNC(pdev->devfn);
15672 	hw->bus.bus_id = pdev->bus->number;
15673 	pf->instance = pfs_found;
15674 
15675 	/* Select something other than the 802.1ad ethertype for the
15676 	 * switch to use internally and drop on ingress.
15677 	 */
15678 	hw->switch_tag = 0xffff;
15679 	hw->first_tag = ETH_P_8021AD;
15680 	hw->second_tag = ETH_P_8021Q;
15681 
15682 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15683 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15684 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15685 
15686 	/* set up the locks for the AQ, do this only once in probe
15687 	 * and destroy them only once in remove
15688 	 */
15689 	mutex_init(&hw->aq.asq_mutex);
15690 	mutex_init(&hw->aq.arq_mutex);
15691 
15692 	pf->msg_enable = netif_msg_init(debug,
15693 					NETIF_MSG_DRV |
15694 					NETIF_MSG_PROBE |
15695 					NETIF_MSG_LINK);
15696 	if (debug < -1)
15697 		pf->hw.debug_mask = debug;
15698 
15699 	/* do a special CORER for clearing PXE mode once at init */
15700 	if (hw->revision_id == 0 &&
15701 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15702 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15703 		i40e_flush(hw);
15704 		msleep(200);
15705 		pf->corer_count++;
15706 
15707 		i40e_clear_pxe_mode(hw);
15708 	}
15709 
15710 	/* Reset here to make sure all is clean and to define PF 'n' */
15711 	i40e_clear_hw(hw);
15712 
15713 	err = i40e_set_mac_type(hw);
15714 	if (err) {
15715 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15716 			 err);
15717 		goto err_pf_reset;
15718 	}
15719 
15720 	err = i40e_handle_resets(pf);
15721 	if (err)
15722 		goto err_pf_reset;
15723 
15724 	i40e_check_recovery_mode(pf);
15725 
15726 	if (is_kdump_kernel()) {
15727 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15728 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15729 	} else {
15730 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15731 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15732 	}
15733 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15734 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15735 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15736 
15737 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15738 		 "%s-%s:misc",
15739 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15740 
15741 	err = i40e_init_shared_code(hw);
15742 	if (err) {
15743 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15744 			 err);
15745 		goto err_pf_reset;
15746 	}
15747 
15748 	/* set up a default setting for link flow control */
15749 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15750 
15751 	err = i40e_init_adminq(hw);
15752 	if (err) {
15753 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15754 			dev_info(&pdev->dev,
15755 				 "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",
15756 				 hw->aq.api_maj_ver,
15757 				 hw->aq.api_min_ver,
15758 				 I40E_FW_API_VERSION_MAJOR,
15759 				 I40E_FW_MINOR_VERSION(hw));
15760 		else
15761 			dev_info(&pdev->dev,
15762 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15763 
15764 		goto err_pf_reset;
15765 	}
15766 	i40e_get_oem_version(hw);
15767 
15768 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15769 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15770 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15771 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15772 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15773 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15774 
15775 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15776 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15777 		dev_dbg(&pdev->dev,
15778 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15779 			 hw->aq.api_maj_ver,
15780 			 hw->aq.api_min_ver,
15781 			 I40E_FW_API_VERSION_MAJOR,
15782 			 I40E_FW_MINOR_VERSION(hw));
15783 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15784 		dev_info(&pdev->dev,
15785 			 "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",
15786 			 hw->aq.api_maj_ver,
15787 			 hw->aq.api_min_ver,
15788 			 I40E_FW_API_VERSION_MAJOR,
15789 			 I40E_FW_MINOR_VERSION(hw));
15790 
15791 	i40e_verify_eeprom(pf);
15792 
15793 	/* Rev 0 hardware was never productized */
15794 	if (hw->revision_id < 1)
15795 		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");
15796 
15797 	i40e_clear_pxe_mode(hw);
15798 
15799 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15800 	if (err)
15801 		goto err_adminq_setup;
15802 
15803 	err = i40e_sw_init(pf);
15804 	if (err) {
15805 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15806 		goto err_sw_init;
15807 	}
15808 
15809 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15810 		return i40e_init_recovery_mode(pf, hw);
15811 
15812 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15813 				hw->func_caps.num_rx_qp, 0, 0);
15814 	if (err) {
15815 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15816 		goto err_init_lan_hmc;
15817 	}
15818 
15819 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15820 	if (err) {
15821 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15822 		err = -ENOENT;
15823 		goto err_configure_lan_hmc;
15824 	}
15825 
15826 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15827 	 * Ignore error return codes because if it was already disabled via
15828 	 * hardware settings this will fail
15829 	 */
15830 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15831 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15832 		i40e_aq_stop_lldp(hw, true, false, NULL);
15833 	}
15834 
15835 	/* allow a platform config to override the HW addr */
15836 	i40e_get_platform_mac_addr(pdev, pf);
15837 
15838 	if (!is_valid_ether_addr(hw->mac.addr)) {
15839 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15840 		err = -EIO;
15841 		goto err_mac_addr;
15842 	}
15843 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15844 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15845 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15846 	if (is_valid_ether_addr(hw->mac.port_addr))
15847 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15848 
15849 	i40e_ptp_alloc_pins(pf);
15850 	pci_set_drvdata(pdev, pf);
15851 	pci_save_state(pdev);
15852 
15853 #ifdef CONFIG_I40E_DCB
15854 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15855 	(!status &&
15856 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15857 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15858 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15859 	dev_info(&pdev->dev,
15860 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15861 			"FW LLDP is disabled\n" :
15862 			"FW LLDP is enabled\n");
15863 
15864 	/* Enable FW to write default DCB config on link-up */
15865 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15866 
15867 	err = i40e_init_pf_dcb(pf);
15868 	if (err) {
15869 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15870 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15871 		/* Continue without DCB enabled */
15872 	}
15873 #endif /* CONFIG_I40E_DCB */
15874 
15875 	/* set up periodic task facility */
15876 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15877 	pf->service_timer_period = HZ;
15878 
15879 	INIT_WORK(&pf->service_task, i40e_service_task);
15880 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15881 
15882 	/* NVM bit on means WoL disabled for the port */
15883 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15884 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15885 		pf->wol_en = false;
15886 	else
15887 		pf->wol_en = true;
15888 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15889 
15890 	/* set up the main switch operations */
15891 	i40e_determine_queue_usage(pf);
15892 	err = i40e_init_interrupt_scheme(pf);
15893 	if (err)
15894 		goto err_switch_setup;
15895 
15896 	/* Reduce Tx and Rx pairs for kdump
15897 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15898 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15899 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15900 	 */
15901 	if (is_kdump_kernel())
15902 		pf->num_lan_msix = 1;
15903 
15904 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15905 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15906 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15907 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15908 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15909 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15910 						    UDP_TUNNEL_TYPE_GENEVE;
15911 
15912 	/* The number of VSIs reported by the FW is the minimum guaranteed
15913 	 * to us; HW supports far more and we share the remaining pool with
15914 	 * the other PFs. We allocate space for more than the guarantee with
15915 	 * the understanding that we might not get them all later.
15916 	 */
15917 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15918 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15919 	else
15920 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15921 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15922 		dev_warn(&pf->pdev->dev,
15923 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15924 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15925 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15926 	}
15927 
15928 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15929 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15930 			  GFP_KERNEL);
15931 	if (!pf->vsi) {
15932 		err = -ENOMEM;
15933 		goto err_switch_setup;
15934 	}
15935 
15936 #ifdef CONFIG_PCI_IOV
15937 	/* prep for VF support */
15938 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15939 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15940 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15941 		if (pci_num_vf(pdev))
15942 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15943 	}
15944 #endif
15945 	err = i40e_setup_pf_switch(pf, false, false);
15946 	if (err) {
15947 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15948 		goto err_vsis;
15949 	}
15950 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15951 
15952 	/* if FDIR VSI was set up, start it now */
15953 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15954 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15955 			i40e_vsi_open(pf->vsi[i]);
15956 			break;
15957 		}
15958 	}
15959 
15960 	/* The driver only wants link up/down and module qualification
15961 	 * reports from firmware.  Note the negative logic.
15962 	 */
15963 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15964 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15965 					 I40E_AQ_EVENT_MEDIA_NA |
15966 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15967 	if (err)
15968 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15969 			 i40e_stat_str(&pf->hw, err),
15970 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15971 
15972 	/* Reconfigure hardware for allowing smaller MSS in the case
15973 	 * of TSO, so that we avoid the MDD being fired and causing
15974 	 * a reset in the case of small MSS+TSO.
15975 	 */
15976 	val = rd32(hw, I40E_REG_MSS);
15977 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15978 		val &= ~I40E_REG_MSS_MIN_MASK;
15979 		val |= I40E_64BYTE_MSS;
15980 		wr32(hw, I40E_REG_MSS, val);
15981 	}
15982 
15983 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15984 		msleep(75);
15985 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15986 		if (err)
15987 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15988 				 i40e_stat_str(&pf->hw, err),
15989 				 i40e_aq_str(&pf->hw,
15990 					     pf->hw.aq.asq_last_status));
15991 	}
15992 	/* The main driver is (mostly) up and happy. We need to set this state
15993 	 * before setting up the misc vector or we get a race and the vector
15994 	 * ends up disabled forever.
15995 	 */
15996 	clear_bit(__I40E_DOWN, pf->state);
15997 
15998 	/* In case of MSIX we are going to setup the misc vector right here
15999 	 * to handle admin queue events etc. In case of legacy and MSI
16000 	 * the misc functionality and queue processing is combined in
16001 	 * the same vector and that gets setup at open.
16002 	 */
16003 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
16004 		err = i40e_setup_misc_vector(pf);
16005 		if (err) {
16006 			dev_info(&pdev->dev,
16007 				 "setup of misc vector failed: %d\n", err);
16008 			i40e_cloud_filter_exit(pf);
16009 			i40e_fdir_teardown(pf);
16010 			goto err_vsis;
16011 		}
16012 	}
16013 
16014 #ifdef CONFIG_PCI_IOV
16015 	/* prep for VF support */
16016 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16017 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16018 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16019 		/* disable link interrupts for VFs */
16020 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16021 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16022 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16023 		i40e_flush(hw);
16024 
16025 		if (pci_num_vf(pdev)) {
16026 			dev_info(&pdev->dev,
16027 				 "Active VFs found, allocating resources.\n");
16028 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16029 			if (err)
16030 				dev_info(&pdev->dev,
16031 					 "Error %d allocating resources for existing VFs\n",
16032 					 err);
16033 		}
16034 	}
16035 #endif /* CONFIG_PCI_IOV */
16036 
16037 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16038 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16039 						      pf->num_iwarp_msix,
16040 						      I40E_IWARP_IRQ_PILE_ID);
16041 		if (pf->iwarp_base_vector < 0) {
16042 			dev_info(&pdev->dev,
16043 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16044 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16045 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16046 		}
16047 	}
16048 
16049 	i40e_dbg_pf_init(pf);
16050 
16051 	/* tell the firmware that we're starting */
16052 	i40e_send_version(pf);
16053 
16054 	/* since everything's happy, start the service_task timer */
16055 	mod_timer(&pf->service_timer,
16056 		  round_jiffies(jiffies + pf->service_timer_period));
16057 
16058 	/* add this PF to client device list and launch a client service task */
16059 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16060 		err = i40e_lan_add_device(pf);
16061 		if (err)
16062 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16063 				 err);
16064 	}
16065 
16066 #define PCI_SPEED_SIZE 8
16067 #define PCI_WIDTH_SIZE 8
16068 	/* Devices on the IOSF bus do not have this information
16069 	 * and will report PCI Gen 1 x 1 by default so don't bother
16070 	 * checking them.
16071 	 */
16072 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16073 		char speed[PCI_SPEED_SIZE] = "Unknown";
16074 		char width[PCI_WIDTH_SIZE] = "Unknown";
16075 
16076 		/* Get the negotiated link width and speed from PCI config
16077 		 * space
16078 		 */
16079 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16080 					  &link_status);
16081 
16082 		i40e_set_pci_config_data(hw, link_status);
16083 
16084 		switch (hw->bus.speed) {
16085 		case i40e_bus_speed_8000:
16086 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16087 		case i40e_bus_speed_5000:
16088 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16089 		case i40e_bus_speed_2500:
16090 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16091 		default:
16092 			break;
16093 		}
16094 		switch (hw->bus.width) {
16095 		case i40e_bus_width_pcie_x8:
16096 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16097 		case i40e_bus_width_pcie_x4:
16098 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16099 		case i40e_bus_width_pcie_x2:
16100 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16101 		case i40e_bus_width_pcie_x1:
16102 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16103 		default:
16104 			break;
16105 		}
16106 
16107 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16108 			 speed, width);
16109 
16110 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16111 		    hw->bus.speed < i40e_bus_speed_8000) {
16112 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16113 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16114 		}
16115 	}
16116 
16117 	/* get the requested speeds from the fw */
16118 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16119 	if (err)
16120 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
16121 			i40e_stat_str(&pf->hw, err),
16122 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16123 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16124 
16125 	/* set the FEC config due to the board capabilities */
16126 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16127 
16128 	/* get the supported phy types from the fw */
16129 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16130 	if (err)
16131 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
16132 			i40e_stat_str(&pf->hw, err),
16133 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16134 
16135 	/* make sure the MFS hasn't been set lower than the default */
16136 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16137 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16138 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16139 	if (val < MAX_FRAME_SIZE_DEFAULT)
16140 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16141 			 i, val);
16142 
16143 	/* Add a filter to drop all Flow control frames from any VSI from being
16144 	 * transmitted. By doing so we stop a malicious VF from sending out
16145 	 * PAUSE or PFC frames and potentially controlling traffic for other
16146 	 * PF/VF VSIs.
16147 	 * The FW can still send Flow control frames if enabled.
16148 	 */
16149 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16150 						       pf->main_vsi_seid);
16151 
16152 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16153 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16154 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16155 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16156 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16157 	/* print a string summarizing features */
16158 	i40e_print_features(pf);
16159 
16160 	return 0;
16161 
16162 	/* Unwind what we've done if something failed in the setup */
16163 err_vsis:
16164 	set_bit(__I40E_DOWN, pf->state);
16165 	i40e_clear_interrupt_scheme(pf);
16166 	kfree(pf->vsi);
16167 err_switch_setup:
16168 	i40e_reset_interrupt_capability(pf);
16169 	del_timer_sync(&pf->service_timer);
16170 err_mac_addr:
16171 err_configure_lan_hmc:
16172 	(void)i40e_shutdown_lan_hmc(hw);
16173 err_init_lan_hmc:
16174 	kfree(pf->qp_pile);
16175 err_sw_init:
16176 err_adminq_setup:
16177 err_pf_reset:
16178 	iounmap(hw->hw_addr);
16179 err_ioremap:
16180 	kfree(pf);
16181 err_pf_alloc:
16182 	pci_disable_pcie_error_reporting(pdev);
16183 	pci_release_mem_regions(pdev);
16184 err_pci_reg:
16185 err_dma:
16186 	pci_disable_device(pdev);
16187 	return err;
16188 }
16189 
16190 /**
16191  * i40e_remove - Device removal routine
16192  * @pdev: PCI device information struct
16193  *
16194  * i40e_remove is called by the PCI subsystem to alert the driver
16195  * that is should release a PCI device.  This could be caused by a
16196  * Hot-Plug event, or because the driver is going to be removed from
16197  * memory.
16198  **/
i40e_remove(struct pci_dev * pdev)16199 static void i40e_remove(struct pci_dev *pdev)
16200 {
16201 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16202 	struct i40e_hw *hw = &pf->hw;
16203 	i40e_status ret_code;
16204 	int i;
16205 
16206 	i40e_dbg_pf_exit(pf);
16207 
16208 	i40e_ptp_stop(pf);
16209 
16210 	/* Disable RSS in hw */
16211 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16212 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16213 
16214 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16215 	 * flags, once they are set, i40e_rebuild should not be called as
16216 	 * i40e_prep_for_reset always returns early.
16217 	 */
16218 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16219 		usleep_range(1000, 2000);
16220 	set_bit(__I40E_IN_REMOVE, pf->state);
16221 
16222 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16223 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16224 		i40e_free_vfs(pf);
16225 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16226 	}
16227 	/* no more scheduling of any task */
16228 	set_bit(__I40E_SUSPENDED, pf->state);
16229 	set_bit(__I40E_DOWN, pf->state);
16230 	if (pf->service_timer.function)
16231 		del_timer_sync(&pf->service_timer);
16232 	if (pf->service_task.func)
16233 		cancel_work_sync(&pf->service_task);
16234 
16235 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16236 		struct i40e_vsi *vsi = pf->vsi[0];
16237 
16238 		/* We know that we have allocated only one vsi for this PF,
16239 		 * it was just for registering netdevice, so the interface
16240 		 * could be visible in the 'ifconfig' output
16241 		 */
16242 		unregister_netdev(vsi->netdev);
16243 		free_netdev(vsi->netdev);
16244 
16245 		goto unmap;
16246 	}
16247 
16248 	/* Client close must be called explicitly here because the timer
16249 	 * has been stopped.
16250 	 */
16251 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16252 
16253 	i40e_fdir_teardown(pf);
16254 
16255 	/* If there is a switch structure or any orphans, remove them.
16256 	 * This will leave only the PF's VSI remaining.
16257 	 */
16258 	for (i = 0; i < I40E_MAX_VEB; i++) {
16259 		if (!pf->veb[i])
16260 			continue;
16261 
16262 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16263 		    pf->veb[i]->uplink_seid == 0)
16264 			i40e_switch_branch_release(pf->veb[i]);
16265 	}
16266 
16267 	/* Now we can shutdown the PF's VSI, just before we kill
16268 	 * adminq and hmc.
16269 	 */
16270 	if (pf->vsi[pf->lan_vsi])
16271 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16272 
16273 	i40e_cloud_filter_exit(pf);
16274 
16275 	/* remove attached clients */
16276 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16277 		ret_code = i40e_lan_del_device(pf);
16278 		if (ret_code)
16279 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16280 				 ret_code);
16281 	}
16282 
16283 	/* shutdown and destroy the HMC */
16284 	if (hw->hmc.hmc_obj) {
16285 		ret_code = i40e_shutdown_lan_hmc(hw);
16286 		if (ret_code)
16287 			dev_warn(&pdev->dev,
16288 				 "Failed to destroy the HMC resources: %d\n",
16289 				 ret_code);
16290 	}
16291 
16292 unmap:
16293 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16294 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16295 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16296 		free_irq(pf->pdev->irq, pf);
16297 
16298 	/* shutdown the adminq */
16299 	i40e_shutdown_adminq(hw);
16300 
16301 	/* destroy the locks only once, here */
16302 	mutex_destroy(&hw->aq.arq_mutex);
16303 	mutex_destroy(&hw->aq.asq_mutex);
16304 
16305 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16306 	rtnl_lock();
16307 	i40e_clear_interrupt_scheme(pf);
16308 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16309 		if (pf->vsi[i]) {
16310 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16311 				i40e_vsi_clear_rings(pf->vsi[i]);
16312 			i40e_vsi_clear(pf->vsi[i]);
16313 			pf->vsi[i] = NULL;
16314 		}
16315 	}
16316 	rtnl_unlock();
16317 
16318 	for (i = 0; i < I40E_MAX_VEB; i++) {
16319 		kfree(pf->veb[i]);
16320 		pf->veb[i] = NULL;
16321 	}
16322 
16323 	kfree(pf->qp_pile);
16324 	kfree(pf->vsi);
16325 
16326 	iounmap(hw->hw_addr);
16327 	kfree(pf);
16328 	pci_release_mem_regions(pdev);
16329 
16330 	pci_disable_pcie_error_reporting(pdev);
16331 	pci_disable_device(pdev);
16332 }
16333 
16334 /**
16335  * i40e_pci_error_detected - warning that something funky happened in PCI land
16336  * @pdev: PCI device information struct
16337  * @error: the type of PCI error
16338  *
16339  * Called to warn that something happened and the error handling steps
16340  * are in progress.  Allows the driver to quiesce things, be ready for
16341  * remediation.
16342  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)16343 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16344 						pci_channel_state_t error)
16345 {
16346 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16347 
16348 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16349 
16350 	if (!pf) {
16351 		dev_info(&pdev->dev,
16352 			 "Cannot recover - error happened during device probe\n");
16353 		return PCI_ERS_RESULT_DISCONNECT;
16354 	}
16355 
16356 	/* shutdown all operations */
16357 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16358 		i40e_prep_for_reset(pf);
16359 
16360 	/* Request a slot reset */
16361 	return PCI_ERS_RESULT_NEED_RESET;
16362 }
16363 
16364 /**
16365  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16366  * @pdev: PCI device information struct
16367  *
16368  * Called to find if the driver can work with the device now that
16369  * the pci slot has been reset.  If a basic connection seems good
16370  * (registers are readable and have sane content) then return a
16371  * happy little PCI_ERS_RESULT_xxx.
16372  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)16373 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16374 {
16375 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16376 	pci_ers_result_t result;
16377 	u32 reg;
16378 
16379 	dev_dbg(&pdev->dev, "%s\n", __func__);
16380 	if (pci_enable_device_mem(pdev)) {
16381 		dev_info(&pdev->dev,
16382 			 "Cannot re-enable PCI device after reset.\n");
16383 		result = PCI_ERS_RESULT_DISCONNECT;
16384 	} else {
16385 		pci_set_master(pdev);
16386 		pci_restore_state(pdev);
16387 		pci_save_state(pdev);
16388 		pci_wake_from_d3(pdev, false);
16389 
16390 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16391 		if (reg == 0)
16392 			result = PCI_ERS_RESULT_RECOVERED;
16393 		else
16394 			result = PCI_ERS_RESULT_DISCONNECT;
16395 	}
16396 
16397 	return result;
16398 }
16399 
16400 /**
16401  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16402  * @pdev: PCI device information struct
16403  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)16404 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16405 {
16406 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16407 
16408 	i40e_prep_for_reset(pf);
16409 }
16410 
16411 /**
16412  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16413  * @pdev: PCI device information struct
16414  */
i40e_pci_error_reset_done(struct pci_dev * pdev)16415 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16416 {
16417 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16418 
16419 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16420 		return;
16421 
16422 	i40e_reset_and_rebuild(pf, false, false);
16423 }
16424 
16425 /**
16426  * i40e_pci_error_resume - restart operations after PCI error recovery
16427  * @pdev: PCI device information struct
16428  *
16429  * Called to allow the driver to bring things back up after PCI error
16430  * and/or reset recovery has finished.
16431  **/
i40e_pci_error_resume(struct pci_dev * pdev)16432 static void i40e_pci_error_resume(struct pci_dev *pdev)
16433 {
16434 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16435 
16436 	dev_dbg(&pdev->dev, "%s\n", __func__);
16437 	if (test_bit(__I40E_SUSPENDED, pf->state))
16438 		return;
16439 
16440 	i40e_handle_reset_warning(pf, false);
16441 }
16442 
16443 /**
16444  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16445  * using the mac_address_write admin q function
16446  * @pf: pointer to i40e_pf struct
16447  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)16448 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16449 {
16450 	struct i40e_hw *hw = &pf->hw;
16451 	i40e_status ret;
16452 	u8 mac_addr[6];
16453 	u16 flags = 0;
16454 
16455 	/* Get current MAC address in case it's an LAA */
16456 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16457 		ether_addr_copy(mac_addr,
16458 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16459 	} else {
16460 		dev_err(&pf->pdev->dev,
16461 			"Failed to retrieve MAC address; using default\n");
16462 		ether_addr_copy(mac_addr, hw->mac.addr);
16463 	}
16464 
16465 	/* The FW expects the mac address write cmd to first be called with
16466 	 * one of these flags before calling it again with the multicast
16467 	 * enable flags.
16468 	 */
16469 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16470 
16471 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16472 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16473 
16474 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16475 	if (ret) {
16476 		dev_err(&pf->pdev->dev,
16477 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16478 		return;
16479 	}
16480 
16481 	flags = I40E_AQC_MC_MAG_EN
16482 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16483 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16484 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16485 	if (ret)
16486 		dev_err(&pf->pdev->dev,
16487 			"Failed to enable Multicast Magic Packet wake up\n");
16488 }
16489 
16490 /**
16491  * i40e_shutdown - PCI callback for shutting down
16492  * @pdev: PCI device information struct
16493  **/
i40e_shutdown(struct pci_dev * pdev)16494 static void i40e_shutdown(struct pci_dev *pdev)
16495 {
16496 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16497 	struct i40e_hw *hw = &pf->hw;
16498 
16499 	set_bit(__I40E_SUSPENDED, pf->state);
16500 	set_bit(__I40E_DOWN, pf->state);
16501 
16502 	del_timer_sync(&pf->service_timer);
16503 	cancel_work_sync(&pf->service_task);
16504 	i40e_cloud_filter_exit(pf);
16505 	i40e_fdir_teardown(pf);
16506 
16507 	/* Client close must be called explicitly here because the timer
16508 	 * has been stopped.
16509 	 */
16510 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16511 
16512 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16513 		i40e_enable_mc_magic_wake(pf);
16514 
16515 	i40e_prep_for_reset(pf);
16516 
16517 	wr32(hw, I40E_PFPM_APM,
16518 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16519 	wr32(hw, I40E_PFPM_WUFC,
16520 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16521 
16522 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16523 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16524 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16525 		free_irq(pf->pdev->irq, pf);
16526 
16527 	/* Since we're going to destroy queues during the
16528 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16529 	 * whole section
16530 	 */
16531 	rtnl_lock();
16532 	i40e_clear_interrupt_scheme(pf);
16533 	rtnl_unlock();
16534 
16535 	if (system_state == SYSTEM_POWER_OFF) {
16536 		pci_wake_from_d3(pdev, pf->wol_en);
16537 		pci_set_power_state(pdev, PCI_D3hot);
16538 	}
16539 }
16540 
16541 /**
16542  * i40e_suspend - PM callback for moving to D3
16543  * @dev: generic device information structure
16544  **/
i40e_suspend(struct device * dev)16545 static int __maybe_unused i40e_suspend(struct device *dev)
16546 {
16547 	struct i40e_pf *pf = dev_get_drvdata(dev);
16548 	struct i40e_hw *hw = &pf->hw;
16549 
16550 	/* If we're already suspended, then there is nothing to do */
16551 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16552 		return 0;
16553 
16554 	set_bit(__I40E_DOWN, pf->state);
16555 
16556 	/* Ensure service task will not be running */
16557 	del_timer_sync(&pf->service_timer);
16558 	cancel_work_sync(&pf->service_task);
16559 
16560 	/* Client close must be called explicitly here because the timer
16561 	 * has been stopped.
16562 	 */
16563 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16564 
16565 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16566 		i40e_enable_mc_magic_wake(pf);
16567 
16568 	/* Since we're going to destroy queues during the
16569 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16570 	 * whole section
16571 	 */
16572 	rtnl_lock();
16573 
16574 	i40e_prep_for_reset(pf);
16575 
16576 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16577 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16578 
16579 	/* Clear the interrupt scheme and release our IRQs so that the system
16580 	 * can safely hibernate even when there are a large number of CPUs.
16581 	 * Otherwise hibernation might fail when mapping all the vectors back
16582 	 * to CPU0.
16583 	 */
16584 	i40e_clear_interrupt_scheme(pf);
16585 
16586 	rtnl_unlock();
16587 
16588 	return 0;
16589 }
16590 
16591 /**
16592  * i40e_resume - PM callback for waking up from D3
16593  * @dev: generic device information structure
16594  **/
i40e_resume(struct device * dev)16595 static int __maybe_unused i40e_resume(struct device *dev)
16596 {
16597 	struct i40e_pf *pf = dev_get_drvdata(dev);
16598 	int err;
16599 
16600 	/* If we're not suspended, then there is nothing to do */
16601 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16602 		return 0;
16603 
16604 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16605 	 * since we're going to be restoring queues
16606 	 */
16607 	rtnl_lock();
16608 
16609 	/* We cleared the interrupt scheme when we suspended, so we need to
16610 	 * restore it now to resume device functionality.
16611 	 */
16612 	err = i40e_restore_interrupt_scheme(pf);
16613 	if (err) {
16614 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16615 			err);
16616 	}
16617 
16618 	clear_bit(__I40E_DOWN, pf->state);
16619 	i40e_reset_and_rebuild(pf, false, true);
16620 
16621 	rtnl_unlock();
16622 
16623 	/* Clear suspended state last after everything is recovered */
16624 	clear_bit(__I40E_SUSPENDED, pf->state);
16625 
16626 	/* Restart the service task */
16627 	mod_timer(&pf->service_timer,
16628 		  round_jiffies(jiffies + pf->service_timer_period));
16629 
16630 	return 0;
16631 }
16632 
16633 static const struct pci_error_handlers i40e_err_handler = {
16634 	.error_detected = i40e_pci_error_detected,
16635 	.slot_reset = i40e_pci_error_slot_reset,
16636 	.reset_prepare = i40e_pci_error_reset_prepare,
16637 	.reset_done = i40e_pci_error_reset_done,
16638 	.resume = i40e_pci_error_resume,
16639 };
16640 
16641 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16642 
16643 static struct pci_driver i40e_driver = {
16644 	.name     = i40e_driver_name,
16645 	.id_table = i40e_pci_tbl,
16646 	.probe    = i40e_probe,
16647 	.remove   = i40e_remove,
16648 	.driver   = {
16649 		.pm = &i40e_pm_ops,
16650 	},
16651 	.shutdown = i40e_shutdown,
16652 	.err_handler = &i40e_err_handler,
16653 	.sriov_configure = i40e_pci_sriov_configure,
16654 };
16655 
16656 /**
16657  * i40e_init_module - Driver registration routine
16658  *
16659  * i40e_init_module is the first routine called when the driver is
16660  * loaded. All it does is register with the PCI subsystem.
16661  **/
i40e_init_module(void)16662 static int __init i40e_init_module(void)
16663 {
16664 	int err;
16665 
16666 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16667 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16668 
16669 	/* There is no need to throttle the number of active tasks because
16670 	 * each device limits its own task using a state bit for scheduling
16671 	 * the service task, and the device tasks do not interfere with each
16672 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16673 	 * since we need to be able to guarantee forward progress even under
16674 	 * memory pressure.
16675 	 */
16676 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16677 	if (!i40e_wq) {
16678 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16679 		return -ENOMEM;
16680 	}
16681 
16682 	i40e_dbg_init();
16683 	err = pci_register_driver(&i40e_driver);
16684 	if (err) {
16685 		destroy_workqueue(i40e_wq);
16686 		i40e_dbg_exit();
16687 		return err;
16688 	}
16689 
16690 	return 0;
16691 }
16692 module_init(i40e_init_module);
16693 
16694 /**
16695  * i40e_exit_module - Driver exit cleanup routine
16696  *
16697  * i40e_exit_module is called just before the driver is removed
16698  * from memory.
16699  **/
i40e_exit_module(void)16700 static void __exit i40e_exit_module(void)
16701 {
16702 	pci_unregister_driver(&i40e_driver);
16703 	destroy_workqueue(i40e_wq);
16704 	ida_destroy(&i40e_client_ida);
16705 	i40e_dbg_exit();
16706 }
16707 module_exit(i40e_exit_module);
16708